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