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