Lay out object file sections when we add the symbols to the symbol
[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 "options.h"
12 #include "workqueue.h"
13 #include "object.h"
14 #include "stringpool.h"
15
16 namespace gold
17 {
18
19 class Input_objects;
20 class Symbol_table;
21 class Output_section;
22 class Output_section_symtab;
23 class Output_section_headers;
24 class Output_segment;
25 class Output_data;
26 class Target;
27
28 // This Task handles mapping the input sections to output sections and
29 // laying them out in memory.
30
31 class Layout_task : public Task
32 {
33  public:
34   // OPTIONS is the command line options, INPUT_OBJECTS is the list of
35   // input objects, THIS_BLOCKER is a token which blocks this task
36   // from executing until all the input symbols have been read.
37   Layout_task(const General_options& options,
38               const Input_objects* input_objects,
39               Symbol_table* symtab,
40               Layout* layout,
41               Task_token* this_blocker)
42     : options_(options), input_objects_(input_objects), symtab_(symtab),
43       layout_(layout), this_blocker_(this_blocker)
44   { }
45
46   ~Layout_task();
47
48   // The standard Task methods.
49
50   Is_runnable_type
51   is_runnable(Workqueue*);
52
53   Task_locker*
54   locks(Workqueue*);
55
56   void
57   run(Workqueue*);
58
59  private:
60   Layout_task(const Layout_task&);
61   Layout_task& operator=(const Layout_task&);
62
63   const General_options& options_;
64   const Input_objects* input_objects_;
65   Symbol_table* symtab_;
66   Layout* layout_;
67   Task_token* this_blocker_;
68 };
69
70 // This class handles the details of laying out input sections.
71
72 class Layout
73 {
74  public:
75   Layout(const General_options& options);
76
77   // Given an input section named NAME with data in SHDR from the
78   // object file OBJECT, return the output section where this input
79   // section should go.  Set *OFFSET to the offset within the output
80   // section.
81   template<int size, bool big_endian>
82   Output_section*
83   layout(Object *object, const char* name,
84          const elfcpp::Shdr<size, big_endian>& shdr, off_t* offset);
85
86   // Return the Stringpool used for symbol names.
87   const Stringpool*
88   sympool() const
89   { return &this->sympool_; }
90
91   // Return whether a section is a .gnu.linkonce section, given the
92   // section name.
93   static inline bool
94   is_linkonce(const char* name)
95   { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; }
96
97   // Record the signature of a comdat section, and return whether to
98   // include it in the link.  The GROUP parameter is true for a
99   // section group signature, false for a signature derived from a
100   // .gnu.linkonce section.
101   bool
102   add_comdat(const char*, bool group);
103
104   // Finalize the layout after all the input sections have been added.
105   off_t
106   finalize(const Input_objects*, Symbol_table*);
107
108   // Write out data not associated with an input file or the symbol
109   // table.
110   void
111   write_data(Output_file*) const;
112
113   // The list of segments.
114
115   typedef std::vector<Output_segment*> Segment_list;
116
117   // The list of sections not attached to a segment.
118
119   typedef std::list<Output_section*> Section_list;
120
121   // The list of information to write out which is not attached to
122   // either a section or a segment.
123   typedef std::list<Output_data*> Data_list;
124
125  private:
126   Layout(const Layout&);
127   Layout& operator=(const Layout&);
128
129   // Mapping from .gnu.linkonce section names to output section names.
130   struct Linkonce_mapping
131   {
132     const char* from;
133     int fromlen;
134     const char* to;
135   };
136   static const Linkonce_mapping linkonce_mapping[];
137   static const int linkonce_mapping_count;
138
139   // Find the first read-only PT_LOAD segment, creating one if
140   // necessary.
141   Output_segment*
142   find_first_load_seg();
143
144   // Set the final file offsets of all the segments.
145   off_t
146   set_segment_offsets(const Target*, Output_segment*);
147
148   // Set the final file offsets of all the sections not associated
149   // with a segment.
150   off_t
151   set_section_offsets(off_t);
152
153   // Create the output sections for the symbol table.
154   void
155   create_symtab_sections(int size, const Input_objects*, Symbol_table*, off_t*,
156                          Output_section** osymtab,
157                          Output_section** ostrtab);
158
159   // Create the .shstrtab section.
160   Output_section*
161   create_shstrtab();
162
163   // Create the section header table.
164   Output_section_headers*
165   create_shdrs(int size, bool big_endian, off_t*);
166
167   // Return whether to include this section in the link.
168   template<int size, bool big_endian>
169   bool
170   include_section(Object* object, const char* name,
171                   const elfcpp::Shdr<size, big_endian>&);
172
173   // Return the output section name to use for a linkonce section
174   // name.
175   static const char*
176   linkonce_output_name(const char* name);
177
178   // Create a new Output_section.
179   Output_section*
180   make_output_section(const char* name, elfcpp::Elf_Word type,
181                       elfcpp::Elf_Xword flags);
182
183   // Return whether SEG1 comes before SEG2 in the output file.
184   static bool
185   segment_precedes(const Output_segment* seg1, const Output_segment* seg2);
186
187   // Map from section flags to segment flags.
188   static elfcpp::Elf_Word
189   section_flags_to_segment(elfcpp::Elf_Xword flags);
190
191   // A mapping used for group signatures.
192   typedef Unordered_map<std::string, bool> Signatures;
193
194   // Mapping from input section name/type/flags to output section.  We
195   // use canonicalized strings here.
196
197   typedef std::pair<const char*,
198                     std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key;
199
200   struct Hash_key
201   {
202     size_t
203     operator()(const Key& k) const;
204   };
205
206   typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map;
207
208   // A comparison class for segments.
209
210   struct Compare_segments
211   {
212     bool
213     operator()(const Output_segment* seg1, const Output_segment* seg2)
214     { return Layout::segment_precedes(seg1, seg2); }
215   };
216
217   // A reference to the options on the command line.
218   const General_options& options_;
219   // The index of the last output section.
220   unsigned int last_shndx_;
221   // The output section names.
222   Stringpool namepool_;
223   // The output symbol names.
224   Stringpool sympool_;
225   // The list of group sections and linkonce sections which we have seen.
226   Signatures signatures_;
227   // The mapping from input section name/type/flags to output sections.
228   Section_name_map section_name_map_;
229   // The list of output segments.
230   Segment_list segment_list_;
231   // The list of output sections which are not attached to any output
232   // segment.
233   Section_list section_list_;
234   // The list of sections which require special output because they
235   // are not comprised of input sections.
236   Data_list special_output_list_;
237 };
238
239 // This task handles writing out data which is not part of a section
240 // or segment.
241
242 class Write_data_task : public Task
243 {
244  public:
245   Write_data_task(const Layout* layout, Output_file* of,
246                   Task_token* final_blocker)
247     : layout_(layout), of_(of), final_blocker_(final_blocker)
248   { }
249
250   // The standard Task methods.
251
252   Is_runnable_type
253   is_runnable(Workqueue*);
254
255   Task_locker*
256   locks(Workqueue*);
257
258   void
259   run(Workqueue*);
260
261  private:
262   const Layout* layout_;
263   Output_file* of_;
264   Task_token* final_blocker_;
265 };
266
267 // This task handles writing out the global symbols.
268
269 class Write_symbols_task : public Task
270 {
271  public:
272   Write_symbols_task(const Symbol_table* symtab, const Target* target,
273                      const Stringpool* sympool, Output_file* of,
274                      Task_token* final_blocker)
275     : symtab_(symtab), target_(target), sympool_(sympool), of_(of),
276       final_blocker_(final_blocker)
277   { }
278
279   // The standard Task methods.
280
281   Is_runnable_type
282   is_runnable(Workqueue*);
283
284   Task_locker*
285   locks(Workqueue*);
286
287   void
288   run(Workqueue*);
289
290  private:
291   const Symbol_table* symtab_;
292   const Target* target_;
293   const Stringpool* sympool_;
294   Output_file* of_;
295   Task_token* final_blocker_;
296 };
297
298 // This task handles closing the file.
299
300 class Close_task : public Task
301 {
302  public:
303   Close_task(Output_file* of, Task_token* final_blocker)
304     : of_(of), final_blocker_(final_blocker)
305   { }
306
307   // The standard task methods.
308
309   Is_runnable_type
310   is_runnable(Workqueue*);
311
312   Task_locker*
313   locks(Workqueue*);
314
315   void
316   run(Workqueue*);
317
318  private:
319   Output_file* of_;
320   Task_token* final_blocker_;
321 };
322
323 } // End namespace gold.
324
325 #endif // !defined(GOLD_LAYOUT_H)