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