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