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