gcc 3.2.2 and 4.1.0 portability hacks.
[external/binutils.git] / gold / layout.h
1 // layout.h -- lay out output file sections for gold  -*- C++ -*-
2
3 #ifndef GOLD_LAYOUT_H
4 #define GOLD_LAYOUT_H
5
6 #include <list>
7 #include <string>
8 #include <utility>
9 #include <vector>
10
11 #include "workqueue.h"
12 #include "object.h"
13 #include "dynobj.h"
14 #include "stringpool.h"
15
16 namespace gold
17 {
18
19 class General_options;
20 class Input_objects;
21 class Symbol_table;
22 class Output_section_data;
23 class Output_section;
24 class Output_section_headers;
25 class Output_segment;
26 class Output_data;
27 class Output_data_dynamic;
28 class Target;
29
30 // This task function handles mapping the input sections to output
31 // sections and laying them out in memory.
32
33 class Layout_task_runner : public Task_function_runner
34 {
35  public:
36   // OPTIONS is the command line options, INPUT_OBJECTS is the list of
37   // input objects, SYMTAB is the symbol table, LAYOUT is the layout
38   // object.
39   Layout_task_runner(const General_options& options,
40                      const Input_objects* input_objects,
41                      Symbol_table* symtab,
42                      Layout* layout)
43     : options_(options), input_objects_(input_objects), symtab_(symtab),
44       layout_(layout)
45   { }
46
47   // Run the operation.
48   void
49   run(Workqueue*);
50
51  private:
52   Layout_task_runner(const Layout_task_runner&);
53   Layout_task_runner& operator=(const Layout_task_runner&);
54
55   const General_options& options_;
56   const Input_objects* input_objects_;
57   Symbol_table* symtab_;
58   Layout* layout_;
59 };
60
61 // This class handles the details of laying out input sections.
62
63 class Layout
64 {
65  public:
66   Layout(const General_options& options);
67
68   // Given an input section SHNDX, named NAME, with data in SHDR, from
69   // the object file OBJECT, return the output section where this
70   // input section should go.  Set *OFFSET to the offset within the
71   // output section.
72   template<int size, bool big_endian>
73   Output_section*
74   layout(Relobj *object, unsigned int shndx, const char* name,
75          const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
76
77   // Add an Output_section_data to the layout.  This is used for
78   // special sections like the GOT section.
79   void
80   add_output_section_data(const char* name, elfcpp::Elf_Word type,
81                           elfcpp::Elf_Xword flags,
82                           Output_section_data*);
83
84   // Create dynamic sections if necessary.
85   void
86   create_initial_dynamic_sections(const Input_objects*, Symbol_table*);
87
88   // Return the Stringpool used for symbol names.
89   const Stringpool*
90   sympool() const
91   { return &this->sympool_; }
92
93   // Return the Stringpool used for dynamic symbol names and dynamic
94   // tags.
95   const Stringpool*
96   dynpool() const
97   { return &this->dynpool_; }
98
99   // Return whether a section is a .gnu.linkonce section, given the
100   // section name.
101   static inline bool
102   is_linkonce(const char* name)
103   { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
104
105   // Record the signature of a comdat section, and return whether to
106   // include it in the link.  The GROUP parameter is true for a
107   // section group signature, false for a signature derived from a
108   // .gnu.linkonce section.
109   bool
110   add_comdat(const char*, bool group);
111
112   // Finalize the layout after all the input sections have been added.
113   off_t
114   finalize(const Input_objects*, Symbol_table*);
115
116   // Return the TLS segment.  This will return NULL if there isn't
117   // one.
118   Output_segment*
119   tls_segment() const
120   { return this->tls_segment_; }
121
122   // Return the normal symbol table.
123   Output_section*
124   symtab_section() const
125   {
126     gold_assert(this->symtab_section_ != NULL);
127     return this->symtab_section_;
128   }
129
130   // Return the dynamic symbol table.
131   Output_section*
132   dynsym_section() const
133   {
134     gold_assert(this->dynsym_section_ != NULL);
135     return this->dynsym_section_;
136   }
137
138   // Return the dynamic tags.
139   Output_data_dynamic*
140   dynamic_data() const
141   { return this->dynamic_data_; }
142
143   // Write out data not associated with an input file or the symbol
144   // table.
145   void
146   write_data(const Symbol_table*, const Target*, Output_file*) const;
147
148   // Return an output section named NAME, or NULL if there is none.
149   Output_section*
150   find_output_section(const char* name) const;
151
152   // Return an output segment of type TYPE, with segment flags SET set
153   // and segment flags CLEAR clear.  Return NULL if there is none.
154   Output_segment*
155   find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
156                       elfcpp::Elf_Word clear) const;
157
158   // The list of segments.
159
160   typedef std::vector<Output_segment*> Segment_list;
161
162   // The list of sections not attached to a segment.
163
164   typedef std::vector<Output_section*> Section_list;
165
166   // The list of information to write out which is not attached to
167   // either a section or a segment.
168   typedef std::vector<Output_data*> Data_list;
169
170  private:
171   Layout(const Layout&);
172   Layout& operator=(const Layout&);
173
174   // Mapping from .gnu.linkonce section names to output section names.
175   struct Linkonce_mapping
176   {
177     const char* from;
178     int fromlen;
179     const char* to;
180     int tolen;
181   };
182   static const Linkonce_mapping linkonce_mapping[];
183   static const int linkonce_mapping_count;
184
185   // Find the first read-only PT_LOAD segment, creating one if
186   // necessary.
187   Output_segment*
188   find_first_load_seg();
189
190   // Create the output sections for the symbol table.
191   void
192   create_symtab_sections(int size, const Input_objects*, Symbol_table*,
193                          off_t*);
194
195   // Create the .shstrtab section.
196   Output_section*
197   create_shstrtab();
198
199   // Create the section header table.
200   Output_section_headers*
201   create_shdrs(int size, bool big_endian, off_t*);
202
203   // Create the dynamic symbol table.
204   void
205   create_dynamic_symtab(const Target*, Symbol_table*, Output_section** pdynstr,
206                         unsigned int* plocal_dynamic_count,
207                         std::vector<Symbol*>* pdynamic_symbols,
208                         Versions* versions);
209
210   // Finish the .dynamic section and PT_DYNAMIC segment.
211   void
212   finish_dynamic_section(const Input_objects*, const Symbol_table*);
213
214   // Create the .interp section and PT_INTERP segment.
215   void
216   create_interp(const Target* target);
217
218   // Create the version sections.
219   void
220   create_version_sections(const Target*, const Versions*,
221                           unsigned int local_symcount,
222                           const std::vector<Symbol*>& dynamic_symbols,
223                           const Output_section* dynstr);
224
225   template<int size, bool big_endian>
226   void
227   sized_create_version_sections(const Versions* versions,
228                                 unsigned int local_symcount,
229                                 const std::vector<Symbol*>& dynamic_symbols,
230                                 const Output_section* dynstr
231                                 ACCEPT_SIZE_ENDIAN);
232
233   // Return whether to include this section in the link.
234   template<int size, bool big_endian>
235   bool
236   include_section(Object* object, const char* name,
237                   const elfcpp::Shdr<size, big_endian>&);
238
239   // Return the output section name to use given an input section
240   // name.  Set *PLEN to the length of the name.  *PLEN must be
241   // initialized to the length of NAME.
242   static const char*
243   output_section_name(const char* name, size_t* plen);
244
245   // Return the output section name to use for a linkonce section
246   // name.  PLEN is as for output_section_name.
247   static const char*
248   linkonce_output_name(const char* name, size_t* plen);
249
250   // Return the output section for NAME, TYPE and FLAGS.
251   Output_section*
252   get_output_section(const char* name, Stringpool::Key name_key,
253                      elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
254
255   // Create a new Output_section.
256   Output_section*
257   make_output_section(const char* name, elfcpp::Elf_Word type,
258                       elfcpp::Elf_Xword flags);
259
260   // Set the final file offsets of all the segments.
261   off_t
262   set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
263
264   // Set the final file offsets and section indexes of all the
265   // sections not associated with a segment.
266   off_t
267   set_section_offsets(off_t, unsigned int *pshndx);
268
269   // Return whether SEG1 comes before SEG2 in the output file.
270   static bool
271   segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
272
273   // Map from section flags to segment flags.
274   static elfcpp::Elf_Word
275   section_flags_to_segment(elfcpp::Elf_Xword flags);
276
277   // A mapping used for group signatures.
278   typedef Unordered_map<std::string, bool> Signatures;
279
280   // Mapping from input section name/type/flags to output section.  We
281   // use canonicalized strings here.
282
283   typedef std::pair<Stringpool::Key,
284                     std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
285
286   struct Hash_key
287   {
288     size_t
289     operator()(const Key& k) const;
290   };
291
292   typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
293
294   // A comparison class for segments.
295
296   struct Compare_segments
297   {
298     bool
299     operator()(const Output_segment* seg1, const Output_segment* seg2)
300     { return Layout::segment_precedes(seg1, seg2); }
301   };
302
303   // A reference to the options on the command line.
304   const General_options& options_;
305   // The output section names.
306   Stringpool namepool_;
307   // The output symbol names.
308   Stringpool sympool_;
309   // The dynamic strings, if needed.
310   Stringpool dynpool_;
311   // The list of group sections and linkonce sections which we have seen.
312   Signatures signatures_;
313   // The mapping from input section name/type/flags to output sections.
314   Section_name_map section_name_map_;
315   // The list of output segments.
316   Segment_list segment_list_;
317   // The list of output sections.
318   Section_list section_list_;
319   // The list of output sections which are not attached to any output
320   // segment.
321   Section_list unattached_section_list_;
322   // The list of unattached Output_data objects which require special
323   // handling because they are not Output_sections.
324   Data_list special_output_list_;
325   // A pointer to the PT_TLS segment if there is one.
326   Output_segment* tls_segment_;
327   // The SHT_SYMTAB output section.
328   Output_section* symtab_section_;
329   // The SHT_DYNSYM output section if there is one.
330   Output_section* dynsym_section_;
331   // The SHT_DYNAMIC output section if there is one.
332   Output_section* dynamic_section_;
333   // The dynamic data which goes into dynamic_section_.
334   Output_data_dynamic* dynamic_data_;
335 };
336
337 // This task handles writing out data which is not part of a section
338 // or segment.
339
340 class Write_data_task : public Task
341 {
342  public:
343   Write_data_task(const Layout* layout, const Symbol_table* symtab,
344                   const Target* target, Output_file* of,
345                   Task_token* final_blocker)
346     : layout_(layout), symtab_(symtab), target_(target), of_(of),
347       final_blocker_(final_blocker)
348   { }
349
350   // The standard Task methods.
351
352   Is_runnable_type
353   is_runnable(Workqueue*);
354
355   Task_locker*
356   locks(Workqueue*);
357
358   void
359   run(Workqueue*);
360
361  private:
362   const Layout* layout_;
363   const Symbol_table* symtab_;
364   const Target* target_;
365   Output_file* of_;
366   Task_token* final_blocker_;
367 };
368
369 // This task handles writing out the global symbols.
370
371 class Write_symbols_task : public Task
372 {
373  public:
374   Write_symbols_task(const Symbol_table* symtab, const Target* target,
375                      const Stringpool* sympool, const Stringpool* dynpool,
376                      Output_file* of, Task_token* final_blocker)
377     : symtab_(symtab), target_(target), sympool_(sympool), dynpool_(dynpool),
378       of_(of), final_blocker_(final_blocker)
379   { }
380
381   // The standard Task methods.
382
383   Is_runnable_type
384   is_runnable(Workqueue*);
385
386   Task_locker*
387   locks(Workqueue*);
388
389   void
390   run(Workqueue*);
391
392  private:
393   const Symbol_table* symtab_;
394   const Target* target_;
395   const Stringpool* sympool_;
396   const Stringpool* dynpool_;
397   Output_file* of_;
398   Task_token* final_blocker_;
399 };
400
401 // This task function handles closing the file.
402
403 class Close_task_runner : public Task_function_runner
404 {
405  public:
406   Close_task_runner(Output_file* of)
407     : of_(of)
408   { }
409
410   // Run the operation.
411   void
412   run(Workqueue*);
413
414  private:
415   Output_file* of_;
416 };
417
418 // A small helper function to align an address.
419
420 inline uint64_t
421 align_address(uint64_t address, uint64_t addralign)
422 {
423   if (addralign != 0)
424     address = (address + addralign - 1) &~ (addralign - 1);
425   return address;
426 }
427
428 } // End namespace gold.
429
430 #endif // !defined(GOLD_LAYOUT_H)