Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // layout.h -- lay out output file sections for gold -*- C++ -*- |
2 | ||
e5756efb | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
a2fb1b05 ILT |
23 | #ifndef GOLD_LAYOUT_H |
24 | #define GOLD_LAYOUT_H | |
25 | ||
e94cf127 | 26 | #include <cstring> |
a2fb1b05 ILT |
27 | #include <list> |
28 | #include <string> | |
29 | #include <utility> | |
30 | #include <vector> | |
31 | ||
e5756efb | 32 | #include "script.h" |
a2fb1b05 ILT |
33 | #include "workqueue.h" |
34 | #include "object.h" | |
14b31740 | 35 | #include "dynobj.h" |
a2fb1b05 ILT |
36 | #include "stringpool.h" |
37 | ||
38 | namespace gold | |
39 | { | |
40 | ||
ead1e424 | 41 | class General_options; |
54dc6425 | 42 | class Input_objects; |
75f65a3e | 43 | class Symbol_table; |
ead1e424 | 44 | class Output_section_data; |
a2fb1b05 | 45 | class Output_section; |
75f65a3e | 46 | class Output_section_headers; |
a2fb1b05 | 47 | class Output_segment; |
54dc6425 | 48 | class Output_data; |
a3ad94ed | 49 | class Output_data_dynamic; |
d491d34e | 50 | class Output_symtab_xindex; |
730cdc88 | 51 | class Eh_frame; |
61ba1cf9 | 52 | class Target; |
a2fb1b05 | 53 | |
92e059d8 ILT |
54 | // This task function handles mapping the input sections to output |
55 | // sections and laying them out in memory. | |
a2fb1b05 | 56 | |
92e059d8 | 57 | class Layout_task_runner : public Task_function_runner |
a2fb1b05 ILT |
58 | { |
59 | public: | |
60 | // OPTIONS is the command line options, INPUT_OBJECTS is the list of | |
92e059d8 ILT |
61 | // input objects, SYMTAB is the symbol table, LAYOUT is the layout |
62 | // object. | |
63 | Layout_task_runner(const General_options& options, | |
64 | const Input_objects* input_objects, | |
65 | Symbol_table* symtab, | |
8851ecca | 66 | Target* target, |
92e059d8 | 67 | Layout* layout) |
75f65a3e | 68 | : options_(options), input_objects_(input_objects), symtab_(symtab), |
8851ecca | 69 | target_(target), layout_(layout) |
a2fb1b05 ILT |
70 | { } |
71 | ||
92e059d8 | 72 | // Run the operation. |
a2fb1b05 | 73 | void |
17a1d0a9 | 74 | run(Workqueue*, const Task*); |
a2fb1b05 ILT |
75 | |
76 | private: | |
92e059d8 ILT |
77 | Layout_task_runner(const Layout_task_runner&); |
78 | Layout_task_runner& operator=(const Layout_task_runner&); | |
a2fb1b05 ILT |
79 | |
80 | const General_options& options_; | |
54dc6425 | 81 | const Input_objects* input_objects_; |
75f65a3e | 82 | Symbol_table* symtab_; |
8851ecca | 83 | Target* target_; |
12e14209 | 84 | Layout* layout_; |
a2fb1b05 ILT |
85 | }; |
86 | ||
87 | // This class handles the details of laying out input sections. | |
88 | ||
89 | class Layout | |
90 | { | |
91 | public: | |
e5756efb | 92 | Layout(const General_options& options, Script_options*); |
54dc6425 | 93 | |
ead1e424 ILT |
94 | // Given an input section SHNDX, named NAME, with data in SHDR, from |
95 | // the object file OBJECT, return the output section where this | |
730cdc88 ILT |
96 | // input section should go. RELOC_SHNDX is the index of a |
97 | // relocation section which applies to this section, or 0 if none, | |
98 | // or -1U if more than one. RELOC_TYPE is the type of the | |
99 | // relocation section if there is one. Set *OFFSET to the offset | |
100 | // within the output section. | |
a2fb1b05 ILT |
101 | template<int size, bool big_endian> |
102 | Output_section* | |
730cdc88 ILT |
103 | layout(Sized_relobj<size, big_endian> *object, unsigned int shndx, |
104 | const char* name, const elfcpp::Shdr<size, big_endian>& shdr, | |
105 | unsigned int reloc_shndx, unsigned int reloc_type, off_t* offset); | |
106 | ||
6a74a719 ILT |
107 | // Layout an input reloc section when doing a relocatable link. The |
108 | // section is RELOC_SHNDX in OBJECT, with data in SHDR. | |
109 | // DATA_SECTION is the reloc section to which it refers. RR is the | |
110 | // relocatable information. | |
111 | template<int size, bool big_endian> | |
112 | Output_section* | |
113 | layout_reloc(Sized_relobj<size, big_endian>* object, | |
114 | unsigned int reloc_shndx, | |
115 | const elfcpp::Shdr<size, big_endian>& shdr, | |
116 | Output_section* data_section, | |
117 | Relocatable_relocs* rr); | |
118 | ||
119 | // Layout a group section when doing a relocatable link. | |
120 | template<int size, bool big_endian> | |
121 | void | |
122 | layout_group(Symbol_table* symtab, | |
123 | Sized_relobj<size, big_endian>* object, | |
124 | unsigned int group_shndx, | |
125 | const char* group_section_name, | |
126 | const char* signature, | |
127 | const elfcpp::Shdr<size, big_endian>& shdr, | |
128 | const elfcpp::Elf_Word* contents); | |
129 | ||
730cdc88 ILT |
130 | // Like layout, only for exception frame sections. OBJECT is an |
131 | // object file. SYMBOLS is the contents of the symbol table | |
132 | // section, with size SYMBOLS_SIZE. SYMBOL_NAMES is the contents of | |
133 | // the symbol name section, with size SYMBOL_NAMES_SIZE. SHNDX is a | |
134 | // .eh_frame section in OBJECT. SHDR is the section header. | |
135 | // RELOC_SHNDX is the index of a relocation section which applies to | |
136 | // this section, or 0 if none, or -1U if more than one. RELOC_TYPE | |
137 | // is the type of the relocation section if there is one. This | |
138 | // returns the output section, and sets *OFFSET to the offset. | |
139 | template<int size, bool big_endian> | |
140 | Output_section* | |
141 | layout_eh_frame(Sized_relobj<size, big_endian>* object, | |
142 | const unsigned char* symbols, | |
143 | off_t symbols_size, | |
144 | const unsigned char* symbol_names, | |
145 | off_t symbol_names_size, | |
146 | unsigned int shndx, | |
147 | const elfcpp::Shdr<size, big_endian>& shdr, | |
148 | unsigned int reloc_shndx, unsigned int reloc_type, | |
149 | off_t* offset); | |
a2fb1b05 | 150 | |
35cdfc9a ILT |
151 | // Handle a GNU stack note. This is called once per input object |
152 | // file. SEEN_GNU_STACK is true if the object file has a | |
153 | // .note.GNU-stack section. GNU_STACK_FLAGS is the section flags | |
154 | // from that section if there was one. | |
155 | void | |
156 | layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags); | |
157 | ||
ead1e424 ILT |
158 | // Add an Output_section_data to the layout. This is used for |
159 | // special sections like the GOT section. | |
160 | void | |
161 | add_output_section_data(const char* name, elfcpp::Elf_Word type, | |
162 | elfcpp::Elf_Xword flags, | |
163 | Output_section_data*); | |
164 | ||
a3ad94ed ILT |
165 | // Create dynamic sections if necessary. |
166 | void | |
9b07f471 | 167 | create_initial_dynamic_sections(Symbol_table*); |
a3ad94ed | 168 | |
bfd58944 ILT |
169 | // Define __start and __stop symbols for output sections. |
170 | void | |
9b07f471 | 171 | define_section_symbols(Symbol_table*); |
bfd58944 | 172 | |
919ed24c ILT |
173 | // Create sections for linker scripts. |
174 | void | |
175 | create_script_sections() | |
176 | { this->script_options_->create_script_sections(this); } | |
177 | ||
e5756efb ILT |
178 | // Define symbols from any linker script. |
179 | void | |
9b07f471 ILT |
180 | define_script_symbols(Symbol_table* symtab) |
181 | { this->script_options_->add_symbols_to_table(symtab); } | |
e5756efb | 182 | |
755ab8af ILT |
183 | // Define symbols for group signatures. |
184 | void | |
185 | define_group_signatures(Symbol_table*); | |
186 | ||
61ba1cf9 ILT |
187 | // Return the Stringpool used for symbol names. |
188 | const Stringpool* | |
189 | sympool() const | |
190 | { return &this->sympool_; } | |
191 | ||
16649710 ILT |
192 | // Return the Stringpool used for dynamic symbol names and dynamic |
193 | // tags. | |
194 | const Stringpool* | |
195 | dynpool() const | |
196 | { return &this->dynpool_; } | |
197 | ||
d491d34e ILT |
198 | // Return the symtab_xindex section used to hold large section |
199 | // indexes for the normal symbol table. | |
200 | Output_symtab_xindex* | |
201 | symtab_xindex() const | |
202 | { return this->symtab_xindex_; } | |
203 | ||
204 | // Return the dynsym_xindex section used to hold large section | |
205 | // indexes for the dynamic symbol table. | |
206 | Output_symtab_xindex* | |
207 | dynsym_xindex() const | |
208 | { return this->dynsym_xindex_; } | |
209 | ||
a2fb1b05 ILT |
210 | // Return whether a section is a .gnu.linkonce section, given the |
211 | // section name. | |
212 | static inline bool | |
213 | is_linkonce(const char* name) | |
214 | { return strncmp(name, ".gnu.linkonce", sizeof(".gnu.linkonce") - 1) == 0; } | |
215 | ||
e94cf127 CC |
216 | // Return true if a section is a debugging section. |
217 | static inline bool | |
218 | is_debug_info_section(const char* name) | |
219 | { | |
220 | // Debugging sections can only be recognized by name. | |
221 | return (strncmp(name, ".debug", sizeof(".debug") - 1) == 0 | |
222 | || strncmp(name, ".gnu.linkonce.wi.", | |
223 | sizeof(".gnu.linkonce.wi.") - 1) == 0 | |
224 | || strncmp(name, ".line", sizeof(".line") - 1) == 0 | |
225 | || strncmp(name, ".stab", sizeof(".stab") - 1) == 0); | |
226 | } | |
227 | ||
a2fb1b05 ILT |
228 | // Record the signature of a comdat section, and return whether to |
229 | // include it in the link. The GROUP parameter is true for a | |
230 | // section group signature, false for a signature derived from a | |
231 | // .gnu.linkonce section. | |
232 | bool | |
e94cf127 CC |
233 | add_comdat(Relobj*, unsigned int, const std::string&, bool group); |
234 | ||
235 | // Find the given comdat signature, and return the object and section | |
236 | // index of the kept group. | |
237 | Relobj* | |
238 | find_kept_object(const std::string&, unsigned int*) const; | |
a2fb1b05 | 239 | |
54dc6425 | 240 | // Finalize the layout after all the input sections have been added. |
75f65a3e | 241 | off_t |
8851ecca | 242 | finalize(const Input_objects*, Symbol_table*, Target*, const Task*); |
17a1d0a9 ILT |
243 | |
244 | // Return whether any sections require postprocessing. | |
245 | bool | |
246 | any_postprocessing_sections() const | |
247 | { return this->any_postprocessing_sections_; } | |
54dc6425 | 248 | |
e44fcf3b ILT |
249 | // Return the size of the output file. |
250 | off_t | |
251 | output_file_size() const | |
252 | { return this->output_file_size_; } | |
253 | ||
16649710 ILT |
254 | // Return the TLS segment. This will return NULL if there isn't |
255 | // one. | |
92e059d8 ILT |
256 | Output_segment* |
257 | tls_segment() const | |
258 | { return this->tls_segment_; } | |
259 | ||
16649710 ILT |
260 | // Return the normal symbol table. |
261 | Output_section* | |
262 | symtab_section() const | |
263 | { | |
264 | gold_assert(this->symtab_section_ != NULL); | |
265 | return this->symtab_section_; | |
266 | } | |
267 | ||
268 | // Return the dynamic symbol table. | |
269 | Output_section* | |
270 | dynsym_section() const | |
271 | { | |
272 | gold_assert(this->dynsym_section_ != NULL); | |
273 | return this->dynsym_section_; | |
274 | } | |
275 | ||
276 | // Return the dynamic tags. | |
277 | Output_data_dynamic* | |
278 | dynamic_data() const | |
279 | { return this->dynamic_data_; } | |
280 | ||
730cdc88 ILT |
281 | // Write out the output sections. |
282 | void | |
283 | write_output_sections(Output_file* of) const; | |
284 | ||
61ba1cf9 ILT |
285 | // Write out data not associated with an input file or the symbol |
286 | // table. | |
287 | void | |
9025d29d | 288 | write_data(const Symbol_table*, Output_file*) const; |
61ba1cf9 | 289 | |
730cdc88 ILT |
290 | // Write out output sections which can not be written until all the |
291 | // input sections are complete. | |
292 | void | |
27bc2bce | 293 | write_sections_after_input_sections(Output_file* of); |
730cdc88 | 294 | |
ead1e424 ILT |
295 | // Return an output section named NAME, or NULL if there is none. |
296 | Output_section* | |
297 | find_output_section(const char* name) const; | |
298 | ||
299 | // Return an output segment of type TYPE, with segment flags SET set | |
300 | // and segment flags CLEAR clear. Return NULL if there is none. | |
301 | Output_segment* | |
302 | find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set, | |
303 | elfcpp::Elf_Word clear) const; | |
304 | ||
3802b2dd ILT |
305 | // Return the number of segments we expect to produce. |
306 | size_t | |
307 | expected_segment_count() const; | |
308 | ||
535890bb ILT |
309 | // Set a flag to indicate that an object file uses the static TLS model. |
310 | void | |
311 | set_has_static_tls() | |
312 | { this->has_static_tls_ = true; } | |
313 | ||
314 | // Return true if any object file uses the static TLS model. | |
315 | bool | |
316 | has_static_tls() const | |
317 | { return this->has_static_tls_; } | |
318 | ||
e5756efb ILT |
319 | // Return the options which may be set by a linker script. |
320 | Script_options* | |
321 | script_options() | |
322 | { return this->script_options_; } | |
323 | ||
324 | const Script_options* | |
325 | script_options() const | |
326 | { return this->script_options_; } | |
d391083d | 327 | |
8ed814a9 ILT |
328 | // Compute and write out the build ID if needed. |
329 | void | |
330 | write_build_id(Output_file*) const; | |
331 | ||
516cb3d0 ILT |
332 | // Rewrite output file in binary format. |
333 | void | |
334 | write_binary(Output_file* in) const; | |
335 | ||
ad8f37d1 ILT |
336 | // Dump statistical information to stderr. |
337 | void | |
338 | print_stats() const; | |
339 | ||
a445fddf | 340 | // A list of segments. |
54dc6425 ILT |
341 | |
342 | typedef std::vector<Output_segment*> Segment_list; | |
343 | ||
a445fddf | 344 | // A list of sections. |
54dc6425 | 345 | |
a3ad94ed | 346 | typedef std::vector<Output_section*> Section_list; |
54dc6425 ILT |
347 | |
348 | // The list of information to write out which is not attached to | |
349 | // either a section or a segment. | |
a3ad94ed | 350 | typedef std::vector<Output_data*> Data_list; |
54dc6425 | 351 | |
a445fddf ILT |
352 | // Store the allocated sections into the section list. This is used |
353 | // by the linker script code. | |
354 | void | |
355 | get_allocated_sections(Section_list*) const; | |
356 | ||
919ed24c ILT |
357 | // Make a section for a linker script to hold data. |
358 | Output_section* | |
359 | make_output_section_for_script(const char* name); | |
360 | ||
a445fddf ILT |
361 | // Make a segment. This is used by the linker script code. |
362 | Output_segment* | |
363 | make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags); | |
364 | ||
365 | // Return the number of segments. | |
366 | size_t | |
367 | segment_count() const | |
368 | { return this->segment_list_.size(); } | |
369 | ||
370 | // Map from section flags to segment flags. | |
371 | static elfcpp::Elf_Word | |
372 | section_flags_to_segment(elfcpp::Elf_Xword flags); | |
373 | ||
154e0e9a ILT |
374 | // Attach sections to segments. |
375 | void | |
376 | attach_sections_to_segments(); | |
377 | ||
a2fb1b05 ILT |
378 | private: |
379 | Layout(const Layout&); | |
380 | Layout& operator=(const Layout&); | |
381 | ||
382 | // Mapping from .gnu.linkonce section names to output section names. | |
383 | struct Linkonce_mapping | |
384 | { | |
385 | const char* from; | |
386 | int fromlen; | |
387 | const char* to; | |
ead1e424 | 388 | int tolen; |
a2fb1b05 ILT |
389 | }; |
390 | static const Linkonce_mapping linkonce_mapping[]; | |
391 | static const int linkonce_mapping_count; | |
392 | ||
755ab8af ILT |
393 | // During a relocatable link, a list of group sections and |
394 | // signatures. | |
395 | struct Group_signature | |
396 | { | |
397 | // The group section. | |
398 | Output_section* section; | |
399 | // The signature. | |
400 | const char* signature; | |
401 | ||
402 | Group_signature() | |
403 | : section(NULL), signature(NULL) | |
404 | { } | |
405 | ||
406 | Group_signature(Output_section* sectiona, const char* signaturea) | |
407 | : section(sectiona), signature(signaturea) | |
408 | { } | |
409 | }; | |
410 | typedef std::vector<Group_signature> Group_signatures; | |
411 | ||
8ed814a9 ILT |
412 | // Create a .note section, filling in the header. |
413 | Output_section* | |
414 | create_note(const char* name, int note_type, size_t descsz, | |
415 | bool allocate, size_t* trailing_padding); | |
416 | ||
4f211c8b ILT |
417 | // Create a .note section for gold. |
418 | void | |
35cdfc9a ILT |
419 | create_gold_note(); |
420 | ||
421 | // Record whether the stack must be executable. | |
422 | void | |
423 | create_executable_stack_info(const Target*); | |
4f211c8b | 424 | |
8ed814a9 ILT |
425 | // Create a build ID note if needed. |
426 | void | |
427 | create_build_id(); | |
428 | ||
75f65a3e ILT |
429 | // Find the first read-only PT_LOAD segment, creating one if |
430 | // necessary. | |
431 | Output_segment* | |
432 | find_first_load_seg(); | |
433 | ||
7bf1f802 ILT |
434 | // Count the local symbols in the regular symbol table and the dynamic |
435 | // symbol table, and build the respective string pools. | |
436 | void | |
17a1d0a9 | 437 | count_local_symbols(const Task*, const Input_objects*); |
7bf1f802 | 438 | |
54dc6425 ILT |
439 | // Create the output sections for the symbol table. |
440 | void | |
d491d34e ILT |
441 | create_symtab_sections(const Input_objects*, Symbol_table*, |
442 | unsigned int, off_t*); | |
54dc6425 | 443 | |
75f65a3e ILT |
444 | // Create the .shstrtab section. |
445 | Output_section* | |
446 | create_shstrtab(); | |
447 | ||
448 | // Create the section header table. | |
27bc2bce | 449 | void |
d491d34e | 450 | create_shdrs(const Output_section* shstrtab_section, off_t*); |
54dc6425 | 451 | |
dbe717ef ILT |
452 | // Create the dynamic symbol table. |
453 | void | |
9b07f471 ILT |
454 | create_dynamic_symtab(const Input_objects*, Symbol_table*, |
455 | Output_section** pdynstr, | |
14b31740 ILT |
456 | unsigned int* plocal_dynamic_count, |
457 | std::vector<Symbol*>* pdynamic_symbols, | |
458 | Versions* versions); | |
dbe717ef | 459 | |
7bf1f802 ILT |
460 | // Assign offsets to each local portion of the dynamic symbol table. |
461 | void | |
462 | assign_local_dynsym_offsets(const Input_objects*); | |
463 | ||
a3ad94ed | 464 | // Finish the .dynamic section and PT_DYNAMIC segment. |
dbe717ef | 465 | void |
16649710 | 466 | finish_dynamic_section(const Input_objects*, const Symbol_table*); |
dbe717ef ILT |
467 | |
468 | // Create the .interp section and PT_INTERP segment. | |
469 | void | |
470 | create_interp(const Target* target); | |
471 | ||
14b31740 ILT |
472 | // Create the version sections. |
473 | void | |
9025d29d | 474 | create_version_sections(const Versions*, |
46fe1623 | 475 | const Symbol_table*, |
14b31740 ILT |
476 | unsigned int local_symcount, |
477 | const std::vector<Symbol*>& dynamic_symbols, | |
478 | const Output_section* dynstr); | |
479 | ||
480 | template<int size, bool big_endian> | |
481 | void | |
482 | sized_create_version_sections(const Versions* versions, | |
46fe1623 | 483 | const Symbol_table*, |
14b31740 ILT |
484 | unsigned int local_symcount, |
485 | const std::vector<Symbol*>& dynamic_symbols, | |
7d1a9ebb | 486 | const Output_section* dynstr); |
14b31740 | 487 | |
a2fb1b05 ILT |
488 | // Return whether to include this section in the link. |
489 | template<int size, bool big_endian> | |
490 | bool | |
730cdc88 | 491 | include_section(Sized_relobj<size, big_endian>* object, const char* name, |
a2fb1b05 ILT |
492 | const elfcpp::Shdr<size, big_endian>&); |
493 | ||
ead1e424 ILT |
494 | // Return the output section name to use given an input section |
495 | // name. Set *PLEN to the length of the name. *PLEN must be | |
496 | // initialized to the length of NAME. | |
497 | static const char* | |
498 | output_section_name(const char* name, size_t* plen); | |
499 | ||
a2fb1b05 | 500 | // Return the output section name to use for a linkonce section |
ead1e424 | 501 | // name. PLEN is as for output_section_name. |
a2fb1b05 | 502 | static const char* |
ead1e424 ILT |
503 | linkonce_output_name(const char* name, size_t* plen); |
504 | ||
d491d34e ILT |
505 | // Return the number of allocated output sections. |
506 | size_t | |
507 | allocated_output_section_count() const; | |
508 | ||
ead1e424 ILT |
509 | // Return the output section for NAME, TYPE and FLAGS. |
510 | Output_section* | |
f0641a0b ILT |
511 | get_output_section(const char* name, Stringpool::Key name_key, |
512 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags); | |
a2fb1b05 | 513 | |
a445fddf ILT |
514 | // Choose the output section for NAME in RELOBJ. |
515 | Output_section* | |
516 | choose_output_section(const Relobj* relobj, const char* name, | |
517 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags, | |
154e0e9a | 518 | bool is_input_section); |
a445fddf | 519 | |
a2fb1b05 ILT |
520 | // Create a new Output_section. |
521 | Output_section* | |
522 | make_output_section(const char* name, elfcpp::Elf_Word type, | |
523 | elfcpp::Elf_Xword flags); | |
524 | ||
4e2b1697 ILT |
525 | // Attach a section to a segment. |
526 | void | |
154e0e9a | 527 | attach_section_to_segment(Output_section*); |
4e2b1697 | 528 | |
154e0e9a | 529 | // Attach an allocated section to a segment. |
1650c4ff | 530 | void |
154e0e9a | 531 | attach_allocated_section_to_segment(Output_section*); |
1650c4ff | 532 | |
dbe717ef ILT |
533 | // Set the final file offsets of all the segments. |
534 | off_t | |
535 | set_segment_offsets(const Target*, Output_segment*, unsigned int* pshndx); | |
536 | ||
6a74a719 ILT |
537 | // Set the file offsets of the sections when doing a relocatable |
538 | // link. | |
539 | off_t | |
540 | set_relocatable_section_offsets(Output_data*, unsigned int* pshndx); | |
541 | ||
86887060 | 542 | // Set the final file offsets of all the sections not associated |
9a0910c3 ILT |
543 | // with a segment. We set section offsets in three passes: the |
544 | // first handles all allocated sections, the second sections that | |
17a1d0a9 ILT |
545 | // require postprocessing, and the last the late-bound STRTAB |
546 | // sections (probably only shstrtab, which is the one we care about | |
547 | // because it holds section names). | |
9a0910c3 ILT |
548 | enum Section_offset_pass |
549 | { | |
550 | BEFORE_INPUT_SECTIONS_PASS, | |
17a1d0a9 ILT |
551 | POSTPROCESSING_SECTIONS_PASS, |
552 | STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS | |
9a0910c3 | 553 | }; |
dbe717ef | 554 | off_t |
9a0910c3 ILT |
555 | set_section_offsets(off_t, Section_offset_pass pass); |
556 | ||
86887060 ILT |
557 | // Set the final section indexes of all the sections not associated |
558 | // with a segment. Returns the next unused index. | |
559 | unsigned int | |
560 | set_section_indexes(unsigned int pshndx); | |
dbe717ef | 561 | |
a445fddf ILT |
562 | // Set the section addresses when using a script. |
563 | Output_segment* | |
564 | set_section_addresses_from_script(Symbol_table*); | |
565 | ||
a2fb1b05 ILT |
566 | // Return whether SEG1 comes before SEG2 in the output file. |
567 | static bool | |
b3168e9d | 568 | segment_precedes(const Output_segment* seg1, const Output_segment* seg2); |
a2fb1b05 | 569 | |
a2fb1b05 | 570 | // A mapping used for group signatures. |
e94cf127 CC |
571 | struct Kept_section |
572 | { | |
573 | Kept_section() | |
574 | : object_(NULL), shndx_(0), group_(false) | |
575 | { } | |
576 | Kept_section(Relobj* object, unsigned int shndx, bool group) | |
577 | : object_(object), shndx_(shndx), group_(group) | |
578 | { } | |
579 | Relobj* object_; | |
580 | unsigned int shndx_; | |
581 | bool group_; | |
582 | }; | |
583 | typedef Unordered_map<std::string, Kept_section> Signatures; | |
a2fb1b05 ILT |
584 | |
585 | // Mapping from input section name/type/flags to output section. We | |
586 | // use canonicalized strings here. | |
587 | ||
f0641a0b | 588 | typedef std::pair<Stringpool::Key, |
a2fb1b05 ILT |
589 | std::pair<elfcpp::Elf_Word, elfcpp::Elf_Xword> > Key; |
590 | ||
591 | struct Hash_key | |
592 | { | |
593 | size_t | |
594 | operator()(const Key& k) const; | |
595 | }; | |
596 | ||
597 | typedef Unordered_map<Key, Output_section*, Hash_key> Section_name_map; | |
598 | ||
599 | // A comparison class for segments. | |
600 | ||
601 | struct Compare_segments | |
602 | { | |
603 | bool | |
604 | operator()(const Output_segment* seg1, const Output_segment* seg2) | |
605 | { return Layout::segment_precedes(seg1, seg2); } | |
606 | }; | |
607 | ||
a2fb1b05 ILT |
608 | // A reference to the options on the command line. |
609 | const General_options& options_; | |
e5756efb ILT |
610 | // Information set by scripts or by command line options. |
611 | Script_options* script_options_; | |
a2fb1b05 ILT |
612 | // The output section names. |
613 | Stringpool namepool_; | |
75f65a3e ILT |
614 | // The output symbol names. |
615 | Stringpool sympool_; | |
a3ad94ed ILT |
616 | // The dynamic strings, if needed. |
617 | Stringpool dynpool_; | |
a2fb1b05 ILT |
618 | // The list of group sections and linkonce sections which we have seen. |
619 | Signatures signatures_; | |
620 | // The mapping from input section name/type/flags to output sections. | |
621 | Section_name_map section_name_map_; | |
622 | // The list of output segments. | |
623 | Segment_list segment_list_; | |
a3ad94ed ILT |
624 | // The list of output sections. |
625 | Section_list section_list_; | |
a2fb1b05 ILT |
626 | // The list of output sections which are not attached to any output |
627 | // segment. | |
a3ad94ed | 628 | Section_list unattached_section_list_; |
154e0e9a ILT |
629 | // Whether we have attached the sections to the segments. |
630 | bool sections_are_attached_; | |
a3ad94ed ILT |
631 | // The list of unattached Output_data objects which require special |
632 | // handling because they are not Output_sections. | |
61ba1cf9 | 633 | Data_list special_output_list_; |
27bc2bce ILT |
634 | // The section headers. |
635 | Output_section_headers* section_headers_; | |
92e059d8 ILT |
636 | // A pointer to the PT_TLS segment if there is one. |
637 | Output_segment* tls_segment_; | |
a3ad94ed ILT |
638 | // The SHT_SYMTAB output section. |
639 | Output_section* symtab_section_; | |
d491d34e ILT |
640 | // The SHT_SYMTAB_SHNDX for the regular symbol table if there is one. |
641 | Output_symtab_xindex* symtab_xindex_; | |
a3ad94ed ILT |
642 | // The SHT_DYNSYM output section if there is one. |
643 | Output_section* dynsym_section_; | |
d491d34e ILT |
644 | // The SHT_SYMTAB_SHNDX for the dynamic symbol table if there is one. |
645 | Output_symtab_xindex* dynsym_xindex_; | |
a3ad94ed ILT |
646 | // The SHT_DYNAMIC output section if there is one. |
647 | Output_section* dynamic_section_; | |
16649710 ILT |
648 | // The dynamic data which goes into dynamic_section_. |
649 | Output_data_dynamic* dynamic_data_; | |
730cdc88 | 650 | // The exception frame output section if there is one. |
3151305a | 651 | Output_section* eh_frame_section_; |
730cdc88 ILT |
652 | // The exception frame data for eh_frame_section_. |
653 | Eh_frame* eh_frame_data_; | |
2c38906f ILT |
654 | // Whether we have added eh_frame_data_ to the .eh_frame section. |
655 | bool added_eh_frame_data_; | |
730cdc88 ILT |
656 | // The exception frame header output section if there is one. |
657 | Output_section* eh_frame_hdr_section_; | |
8ed814a9 ILT |
658 | // The space for the build ID checksum if there is one. |
659 | Output_section_data* build_id_note_; | |
755ab8af ILT |
660 | // A list of group sections and their signatures. |
661 | Group_signatures group_signatures_; | |
e44fcf3b ILT |
662 | // The size of the output file. |
663 | off_t output_file_size_; | |
35cdfc9a ILT |
664 | // Whether we have seen an object file marked to require an |
665 | // executable stack. | |
666 | bool input_requires_executable_stack_; | |
667 | // Whether we have seen at least one object file with an executable | |
668 | // stack marker. | |
669 | bool input_with_gnu_stack_note_; | |
670 | // Whether we have seen at least one object file without an | |
671 | // executable stack marker. | |
672 | bool input_without_gnu_stack_note_; | |
535890bb ILT |
673 | // Whether we have seen an object file that uses the static TLS model. |
674 | bool has_static_tls_; | |
17a1d0a9 ILT |
675 | // Whether any sections require postprocessing. |
676 | bool any_postprocessing_sections_; | |
61ba1cf9 ILT |
677 | }; |
678 | ||
730cdc88 ILT |
679 | // This task handles writing out data in output sections which is not |
680 | // part of an input section, or which requires special handling. When | |
681 | // this is done, it unblocks both output_sections_blocker and | |
682 | // final_blocker. | |
683 | ||
684 | class Write_sections_task : public Task | |
685 | { | |
686 | public: | |
687 | Write_sections_task(const Layout* layout, Output_file* of, | |
688 | Task_token* output_sections_blocker, | |
689 | Task_token* final_blocker) | |
690 | : layout_(layout), of_(of), | |
691 | output_sections_blocker_(output_sections_blocker), | |
692 | final_blocker_(final_blocker) | |
693 | { } | |
694 | ||
695 | // The standard Task methods. | |
696 | ||
17a1d0a9 ILT |
697 | Task_token* |
698 | is_runnable(); | |
730cdc88 | 699 | |
17a1d0a9 ILT |
700 | void |
701 | locks(Task_locker*); | |
730cdc88 ILT |
702 | |
703 | void | |
704 | run(Workqueue*); | |
705 | ||
c7912668 ILT |
706 | std::string |
707 | get_name() const | |
708 | { return "Write_sections_task"; } | |
709 | ||
730cdc88 ILT |
710 | private: |
711 | class Write_sections_locker; | |
712 | ||
713 | const Layout* layout_; | |
714 | Output_file* of_; | |
715 | Task_token* output_sections_blocker_; | |
716 | Task_token* final_blocker_; | |
717 | }; | |
718 | ||
61ba1cf9 ILT |
719 | // This task handles writing out data which is not part of a section |
720 | // or segment. | |
721 | ||
722 | class Write_data_task : public Task | |
723 | { | |
724 | public: | |
a3ad94ed | 725 | Write_data_task(const Layout* layout, const Symbol_table* symtab, |
9025d29d ILT |
726 | Output_file* of, Task_token* final_blocker) |
727 | : layout_(layout), symtab_(symtab), of_(of), final_blocker_(final_blocker) | |
61ba1cf9 ILT |
728 | { } |
729 | ||
730 | // The standard Task methods. | |
731 | ||
17a1d0a9 ILT |
732 | Task_token* |
733 | is_runnable(); | |
61ba1cf9 | 734 | |
17a1d0a9 ILT |
735 | void |
736 | locks(Task_locker*); | |
61ba1cf9 ILT |
737 | |
738 | void | |
739 | run(Workqueue*); | |
740 | ||
c7912668 ILT |
741 | std::string |
742 | get_name() const | |
743 | { return "Write_data_task"; } | |
744 | ||
61ba1cf9 ILT |
745 | private: |
746 | const Layout* layout_; | |
a3ad94ed | 747 | const Symbol_table* symtab_; |
61ba1cf9 ILT |
748 | Output_file* of_; |
749 | Task_token* final_blocker_; | |
750 | }; | |
751 | ||
752 | // This task handles writing out the global symbols. | |
753 | ||
754 | class Write_symbols_task : public Task | |
755 | { | |
756 | public: | |
d491d34e | 757 | Write_symbols_task(const Layout* layout, const Symbol_table* symtab, |
9a2d6984 | 758 | const Input_objects* input_objects, |
16649710 ILT |
759 | const Stringpool* sympool, const Stringpool* dynpool, |
760 | Output_file* of, Task_token* final_blocker) | |
d491d34e ILT |
761 | : layout_(layout), symtab_(symtab), input_objects_(input_objects), |
762 | sympool_(sympool), dynpool_(dynpool), of_(of), | |
763 | final_blocker_(final_blocker) | |
61ba1cf9 ILT |
764 | { } |
765 | ||
766 | // The standard Task methods. | |
767 | ||
17a1d0a9 ILT |
768 | Task_token* |
769 | is_runnable(); | |
61ba1cf9 | 770 | |
17a1d0a9 ILT |
771 | void |
772 | locks(Task_locker*); | |
61ba1cf9 ILT |
773 | |
774 | void | |
775 | run(Workqueue*); | |
776 | ||
c7912668 ILT |
777 | std::string |
778 | get_name() const | |
779 | { return "Write_symbols_task"; } | |
780 | ||
61ba1cf9 | 781 | private: |
d491d34e | 782 | const Layout* layout_; |
61ba1cf9 | 783 | const Symbol_table* symtab_; |
9a2d6984 | 784 | const Input_objects* input_objects_; |
61ba1cf9 | 785 | const Stringpool* sympool_; |
16649710 | 786 | const Stringpool* dynpool_; |
61ba1cf9 ILT |
787 | Output_file* of_; |
788 | Task_token* final_blocker_; | |
789 | }; | |
790 | ||
730cdc88 ILT |
791 | // This task handles writing out data in output sections which can't |
792 | // be written out until all the input sections have been handled. | |
793 | // This is for sections whose contents is based on the contents of | |
794 | // other output sections. | |
795 | ||
796 | class Write_after_input_sections_task : public Task | |
797 | { | |
798 | public: | |
27bc2bce | 799 | Write_after_input_sections_task(Layout* layout, Output_file* of, |
730cdc88 ILT |
800 | Task_token* input_sections_blocker, |
801 | Task_token* final_blocker) | |
802 | : layout_(layout), of_(of), | |
803 | input_sections_blocker_(input_sections_blocker), | |
804 | final_blocker_(final_blocker) | |
805 | { } | |
806 | ||
807 | // The standard Task methods. | |
808 | ||
17a1d0a9 ILT |
809 | Task_token* |
810 | is_runnable(); | |
730cdc88 | 811 | |
17a1d0a9 ILT |
812 | void |
813 | locks(Task_locker*); | |
730cdc88 ILT |
814 | |
815 | void | |
816 | run(Workqueue*); | |
817 | ||
c7912668 ILT |
818 | std::string |
819 | get_name() const | |
820 | { return "Write_after_input_sections_task"; } | |
821 | ||
730cdc88 | 822 | private: |
27bc2bce | 823 | Layout* layout_; |
730cdc88 ILT |
824 | Output_file* of_; |
825 | Task_token* input_sections_blocker_; | |
826 | Task_token* final_blocker_; | |
827 | }; | |
828 | ||
92e059d8 | 829 | // This task function handles closing the file. |
61ba1cf9 | 830 | |
92e059d8 | 831 | class Close_task_runner : public Task_function_runner |
61ba1cf9 ILT |
832 | { |
833 | public: | |
516cb3d0 ILT |
834 | Close_task_runner(const General_options* options, const Layout* layout, |
835 | Output_file* of) | |
836 | : options_(options), layout_(layout), of_(of) | |
61ba1cf9 ILT |
837 | { } |
838 | ||
92e059d8 | 839 | // Run the operation. |
61ba1cf9 | 840 | void |
17a1d0a9 | 841 | run(Workqueue*, const Task*); |
61ba1cf9 ILT |
842 | |
843 | private: | |
516cb3d0 ILT |
844 | const General_options* options_; |
845 | const Layout* layout_; | |
61ba1cf9 | 846 | Output_file* of_; |
a2fb1b05 ILT |
847 | }; |
848 | ||
ead1e424 ILT |
849 | // A small helper function to align an address. |
850 | ||
851 | inline uint64_t | |
852 | align_address(uint64_t address, uint64_t addralign) | |
853 | { | |
854 | if (addralign != 0) | |
855 | address = (address + addralign - 1) &~ (addralign - 1); | |
856 | return address; | |
857 | } | |
858 | ||
a2fb1b05 ILT |
859 | } // End namespace gold. |
860 | ||
861 | #endif // !defined(GOLD_LAYOUT_H) |