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