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