Can now dynamically link hello, world.
[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 the Stringpool used for dynamic symbol names and dynamic
93   // tags.
94   const Stringpool*
95   dynpool() const
96   { return &this->dynpool_; }
97
98   // Return whether a section is a .gnu.linkonce section, given the
99   // section name.
100   static inline bool
101   is_linkonce(const char* name)
102   { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
103
104   // Record the signature of a comdat section, and return whether to
105   // include it in the link.  The GROUP parameter is true for a
106   // section group signature, false for a signature derived from a
107   // .gnu.linkonce section.
108   bool
109   add_comdat(const char*, bool group);
110
111   // Finalize the layout after all the input sections have been added.
112   off_t
113   finalize(const Input_objects*, Symbol_table*);
114
115   // Return the TLS segment.  This will return NULL if there isn't
116   // one.
117   Output_segment*
118   tls_segment() const
119   { return this->tls_segment_; }
120
121   // Return the normal symbol table.
122   Output_section*
123   symtab_section() const
124   {
125     gold_assert(this->symtab_section_ != NULL);
126     return this->symtab_section_;
127   }
128
129   // Return the dynamic symbol table.
130   Output_section*
131   dynsym_section() const
132   {
133     gold_assert(this->dynsym_section_ != NULL);
134     return this->dynsym_section_;
135   }
136
137   // Return the dynamic tags.
138   Output_data_dynamic*
139   dynamic_data() const
140   { return this->dynamic_data_; }
141
142   // Write out data not associated with an input file or the symbol
143   // table.
144   void
145   write_data(const Symbol_table*, const Target*, Output_file*) const;
146
147   // Return an output section named NAME, or NULL if there is none.
148   Output_section*
149   find_output_section(const char* name) const;
150
151   // Return an output segment of type TYPE, with segment flags SET set
152   // and segment flags CLEAR clear.  Return NULL if there is none.
153   Output_segment*
154   find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
155                       elfcpp::Elf_Word clear) const;
156
157   // The list of segments.
158
159   typedef std::vector<Output_segment*> Segment_list;
160
161   // The list of sections not attached to a segment.
162
163   typedef std::vector<Output_section*> Section_list;
164
165   // The list of information to write out which is not attached to
166   // either a section or a segment.
167   typedef std::vector<Output_data*> Data_list;
168
169  private:
170   Layout(const Layout&);
171   Layout& operator=(const Layout&);
172
173   // Mapping from .gnu.linkonce section names to output section names.
174   struct Linkonce_mapping
175   {
176     const char* from;
177     int fromlen;
178     const char* to;
179     int tolen;
180   };
181   static const Linkonce_mapping linkonce_mapping[];
182   static const int linkonce_mapping_count;
183
184   // Find the first read-only PT_LOAD segment, creating one if
185   // necessary.
186   Output_segment*
187   find_first_load_seg();
188
189   // Create the output sections for the symbol table.
190   void
191   create_symtab_sections(int size, const Input_objects*, Symbol_table*,
192                          off_t*);
193
194   // Create the .shstrtab section.
195   Output_section*
196   create_shstrtab();
197
198   // Create the section header table.
199   Output_section_headers*
200   create_shdrs(int size, bool big_endian, off_t*);
201
202   // Create the dynamic symbol table.
203   void
204   create_dynamic_symtab(const Target*, Symbol_table*);
205
206   // Finish the .dynamic section and PT_DYNAMIC segment.
207   void
208   finish_dynamic_section(const Input_objects*, const Symbol_table*);
209
210   // Create the .interp section and PT_INTERP segment.
211   void
212   create_interp(const Target* target);
213
214   // Return whether to include this section in the link.
215   template<int size, bool big_endian>
216   bool
217   include_section(Object* object, const char* name,
218                   const elfcpp::Shdr<size, big_endian>&);
219
220   // Return the output section name to use given an input section
221   // name.  Set *PLEN to the length of the name.  *PLEN must be
222   // initialized to the length of NAME.
223   static const char*
224   output_section_name(const char* name, size_t* plen);
225
226   // Return the output section name to use for a linkonce section
227   // name.  PLEN is as for output_section_name.
228   static const char*
229   linkonce_output_name(const char* name, size_t* plen);
230
231   // Return the output section for NAME, TYPE and FLAGS.
232   Output_section*
233   get_output_section(const char* name, Stringpool::Key name_key,
234                      elfcpp::Elf_Word type, elfcpp::Elf_Xword flags);
235
236   // Create a new Output_section.
237   Output_section*
238   make_output_section(const char* name, elfcpp::Elf_Word type,
239                       elfcpp::Elf_Xword flags);
240
241   // Set the final file offsets of all the segments.
242   off_t
243   set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx);
244
245   // Set the final file offsets and section indices of all the
246   // sections not associated with a segment.
247   off_t
248   set_section_offsets(off_t, unsigned int *pshndx);
249
250   // Return whether SEG1 comes before SEG2 in the output file.
251   static bool
252   segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
253
254   // Map from section flags to segment flags.
255   static elfcpp::Elf_Word
256   section_flags_to_segment(elfcpp::Elf_Xword flags);
257
258   // A mapping used for group signatures.
259   typedef Unordered_map<std::string, bool> Signatures;
260
261   // Mapping from input section name/type/flags to output section.  We
262   // use canonicalized strings here.
263
264   typedef std::pair<Stringpool::Key,
265                     std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
266
267   struct Hash_key
268   {
269     size_t
270     operator()(const Key& k) const;
271   };
272
273   typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
274
275   // A comparison class for segments.
276
277   struct Compare_segments
278   {
279     bool
280     operator()(const Output_segment* seg1, const Output_segment* seg2)
281     { return Layout::segment_precedes(seg1, seg2); }
282   };
283
284   // A reference to the options on the command line.
285   const General_options& options_;
286   // The output section names.
287   Stringpool namepool_;
288   // The output symbol names.
289   Stringpool sympool_;
290   // The dynamic strings, if needed.
291   Stringpool dynpool_;
292   // The list of group sections and linkonce sections which we have seen.
293   Signatures signatures_;
294   // The mapping from input section name/type/flags to output sections.
295   Section_name_map section_name_map_;
296   // The list of output segments.
297   Segment_list segment_list_;
298   // The list of output sections.
299   Section_list section_list_;
300   // The list of output sections which are not attached to any output
301   // segment.
302   Section_list unattached_section_list_;
303   // The list of unattached Output_data objects which require special
304   // handling because they are not Output_sections.
305   Data_list special_output_list_;
306   // A pointer to the PT_TLS segment if there is one.
307   Output_segment* tls_segment_;
308   // The SHT_SYMTAB output section.
309   Output_section* symtab_section_;
310   // The SHT_DYNSYM output section if there is one.
311   Output_section* dynsym_section_;
312   // The SHT_DYNAMIC output section if there is one.
313   Output_section* dynamic_section_;
314   // The dynamic data which goes into dynamic_section_.
315   Output_data_dynamic* dynamic_data_;
316 };
317
318 // This task handles writing out data which is not part of a section
319 // or segment.
320
321 class Write_data_task : public Task
322 {
323  public:
324   Write_data_task(const Layout* layout, const Symbol_table* symtab,
325                   const Target* target, Output_file* of,
326                   Task_token* final_blocker)
327     : layout_(layout), symtab_(symtab), target_(target), of_(of),
328       final_blocker_(final_blocker)
329   { }
330
331   // The standard Task methods.
332
333   Is_runnable_type
334   is_runnable(Workqueue*);
335
336   Task_locker*
337   locks(Workqueue*);
338
339   void
340   run(Workqueue*);
341
342  private:
343   const Layout* layout_;
344   const Symbol_table* symtab_;
345   const Target* target_;
346   Output_file* of_;
347   Task_token* final_blocker_;
348 };
349
350 // This task handles writing out the global symbols.
351
352 class Write_symbols_task : public Task
353 {
354  public:
355   Write_symbols_task(const Symbol_table* symtab, const Target* target,
356                      const Stringpool* sympool, const Stringpool* dynpool,
357                      Output_file* of, Task_token* final_blocker)
358     : symtab_(symtab), target_(target), sympool_(sympool), dynpool_(dynpool),
359       of_(of), final_blocker_(final_blocker)
360   { }
361
362   // The standard Task methods.
363
364   Is_runnable_type
365   is_runnable(Workqueue*);
366
367   Task_locker*
368   locks(Workqueue*);
369
370   void
371   run(Workqueue*);
372
373  private:
374   const Symbol_table* symtab_;
375   const Target* target_;
376   const Stringpool* sympool_;
377   const Stringpool* dynpool_;
378   Output_file* of_;
379   Task_token* final_blocker_;
380 };
381
382 // This task function handles closing the file.
383
384 class Close_task_runner : public Task_function_runner
385 {
386  public:
387   Close_task_runner(Output_file* of)
388     : of_(of)
389   { }
390
391   // Run the operation.
392   void
393   run(Workqueue*);
394
395  private:
396   Output_file* of_;
397 };
398
399 // A small helper function to align an address.
400
401 inline uint64_t
402 align_address(uint64_t address, uint64_t addralign)
403 {
404   if (addralign != 0)
405     address = (address + addralign - 1) &~ (addralign - 1);
406   return address;
407 }
408
409 } // End namespace gold.
410
411 #endif // !defined(GOLD_LAYOUT_H)