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