Commit | Line | Data |
---|---|---|
a2fb1b05 ILT |
1 | // layout.cc -- lay out output file sections for gold |
2 | ||
e5756efb | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
4 | // Written by Ian Lance Taylor <iant@google.com>. |
5 | ||
6 | // This file is part of gold. | |
7 | ||
8 | // This program is free software; you can redistribute it and/or modify | |
9 | // it under the terms of the GNU General Public License as published by | |
10 | // the Free Software Foundation; either version 3 of the License, or | |
11 | // (at your option) any later version. | |
12 | ||
13 | // This program is distributed in the hope that it will be useful, | |
14 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | // GNU General Public License for more details. | |
17 | ||
18 | // You should have received a copy of the GNU General Public License | |
19 | // along with this program; if not, write to the Free Software | |
20 | // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, | |
21 | // MA 02110-1301, USA. | |
22 | ||
a2fb1b05 ILT |
23 | #include "gold.h" |
24 | ||
8ed814a9 | 25 | #include <cerrno> |
a2fb1b05 | 26 | #include <cstring> |
54dc6425 | 27 | #include <algorithm> |
a2fb1b05 ILT |
28 | #include <iostream> |
29 | #include <utility> | |
8ed814a9 ILT |
30 | #include <fcntl.h> |
31 | #include <unistd.h> | |
32 | #include "libiberty.h" | |
33 | #include "md5.h" | |
34 | #include "sha1.h" | |
a2fb1b05 | 35 | |
7e1edb90 | 36 | #include "parameters.h" |
14144f39 | 37 | #include "options.h" |
7d9e3d98 | 38 | #include "mapfile.h" |
a445fddf ILT |
39 | #include "script.h" |
40 | #include "script-sections.h" | |
a2fb1b05 | 41 | #include "output.h" |
f6ce93d6 | 42 | #include "symtab.h" |
a3ad94ed | 43 | #include "dynobj.h" |
3151305a | 44 | #include "ehframe.h" |
96803768 | 45 | #include "compressed_output.h" |
62b01cb5 | 46 | #include "reduced_debug_output.h" |
6a74a719 | 47 | #include "reloc.h" |
2a00e4fb | 48 | #include "descriptors.h" |
a2fb1b05 | 49 | #include "layout.h" |
2756a258 | 50 | #include "plugin.h" |
a2fb1b05 ILT |
51 | |
52 | namespace gold | |
53 | { | |
54 | ||
92e059d8 | 55 | // Layout_task_runner methods. |
a2fb1b05 ILT |
56 | |
57 | // Lay out the sections. This is called after all the input objects | |
58 | // have been read. | |
59 | ||
60 | void | |
17a1d0a9 | 61 | Layout_task_runner::run(Workqueue* workqueue, const Task* task) |
a2fb1b05 | 62 | { |
12e14209 | 63 | off_t file_size = this->layout_->finalize(this->input_objects_, |
17a1d0a9 | 64 | this->symtab_, |
8851ecca | 65 | this->target_, |
17a1d0a9 | 66 | task); |
61ba1cf9 ILT |
67 | |
68 | // Now we know the final size of the output file and we know where | |
69 | // each piece of information goes. | |
7d9e3d98 ILT |
70 | |
71 | if (this->mapfile_ != NULL) | |
72 | { | |
73 | this->mapfile_->print_discarded_sections(this->input_objects_); | |
74 | this->layout_->print_to_mapfile(this->mapfile_); | |
75 | } | |
76 | ||
8851ecca | 77 | Output_file* of = new Output_file(parameters->options().output_file_name()); |
7cc619c3 | 78 | if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF) |
516cb3d0 | 79 | of->set_is_temporary(); |
61ba1cf9 ILT |
80 | of->open(file_size); |
81 | ||
82 | // Queue up the final set of tasks. | |
83 | gold::queue_final_tasks(this->options_, this->input_objects_, | |
12e14209 | 84 | this->symtab_, this->layout_, workqueue, of); |
a2fb1b05 ILT |
85 | } |
86 | ||
87 | // Layout methods. | |
88 | ||
e5756efb | 89 | Layout::Layout(const General_options& options, Script_options* script_options) |
d491d34e ILT |
90 | : options_(options), |
91 | script_options_(script_options), | |
92 | namepool_(), | |
93 | sympool_(), | |
94 | dynpool_(), | |
95 | signatures_(), | |
96 | section_name_map_(), | |
97 | segment_list_(), | |
98 | section_list_(), | |
99 | unattached_section_list_(), | |
100 | sections_are_attached_(false), | |
101 | special_output_list_(), | |
102 | section_headers_(NULL), | |
103 | tls_segment_(NULL), | |
9f1d377b | 104 | relro_segment_(NULL), |
d491d34e ILT |
105 | symtab_section_(NULL), |
106 | symtab_xindex_(NULL), | |
107 | dynsym_section_(NULL), | |
108 | dynsym_xindex_(NULL), | |
109 | dynamic_section_(NULL), | |
110 | dynamic_data_(NULL), | |
111 | eh_frame_section_(NULL), | |
112 | eh_frame_data_(NULL), | |
113 | added_eh_frame_data_(false), | |
114 | eh_frame_hdr_section_(NULL), | |
115 | build_id_note_(NULL), | |
62b01cb5 ILT |
116 | debug_abbrev_(NULL), |
117 | debug_info_(NULL), | |
d491d34e ILT |
118 | group_signatures_(), |
119 | output_file_size_(-1), | |
35cdfc9a ILT |
120 | input_requires_executable_stack_(false), |
121 | input_with_gnu_stack_note_(false), | |
535890bb | 122 | input_without_gnu_stack_note_(false), |
17a1d0a9 ILT |
123 | has_static_tls_(false), |
124 | any_postprocessing_sections_(false) | |
54dc6425 ILT |
125 | { |
126 | // Make space for more than enough segments for a typical file. | |
127 | // This is just for efficiency--it's OK if we wind up needing more. | |
a3ad94ed ILT |
128 | this->segment_list_.reserve(12); |
129 | ||
27bc2bce ILT |
130 | // We expect two unattached Output_data objects: the file header and |
131 | // the segment headers. | |
132 | this->special_output_list_.reserve(2); | |
54dc6425 ILT |
133 | } |
134 | ||
a2fb1b05 ILT |
135 | // Hash a key we use to look up an output section mapping. |
136 | ||
137 | size_t | |
138 | Layout::Hash_key::operator()(const Layout::Key& k) const | |
139 | { | |
f0641a0b | 140 | return k.first + k.second.first + k.second.second; |
a2fb1b05 ILT |
141 | } |
142 | ||
9e2dcb77 ILT |
143 | // Return whether PREFIX is a prefix of STR. |
144 | ||
145 | static inline bool | |
146 | is_prefix_of(const char* prefix, const char* str) | |
147 | { | |
148 | return strncmp(prefix, str, strlen(prefix)) == 0; | |
149 | } | |
150 | ||
02d2ba74 ILT |
151 | // Returns whether the given section is in the list of |
152 | // debug-sections-used-by-some-version-of-gdb. Currently, | |
153 | // we've checked versions of gdb up to and including 6.7.1. | |
154 | ||
155 | static const char* gdb_sections[] = | |
156 | { ".debug_abbrev", | |
157 | // ".debug_aranges", // not used by gdb as of 6.7.1 | |
158 | ".debug_frame", | |
159 | ".debug_info", | |
160 | ".debug_line", | |
161 | ".debug_loc", | |
162 | ".debug_macinfo", | |
163 | // ".debug_pubnames", // not used by gdb as of 6.7.1 | |
164 | ".debug_ranges", | |
165 | ".debug_str", | |
166 | }; | |
167 | ||
62b01cb5 ILT |
168 | static const char* lines_only_debug_sections[] = |
169 | { ".debug_abbrev", | |
170 | // ".debug_aranges", // not used by gdb as of 6.7.1 | |
171 | // ".debug_frame", | |
172 | ".debug_info", | |
173 | ".debug_line", | |
174 | // ".debug_loc", | |
175 | // ".debug_macinfo", | |
176 | // ".debug_pubnames", // not used by gdb as of 6.7.1 | |
177 | // ".debug_ranges", | |
178 | ".debug_str", | |
179 | }; | |
180 | ||
02d2ba74 ILT |
181 | static inline bool |
182 | is_gdb_debug_section(const char* str) | |
183 | { | |
184 | // We can do this faster: binary search or a hashtable. But why bother? | |
185 | for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i) | |
186 | if (strcmp(str, gdb_sections[i]) == 0) | |
187 | return true; | |
188 | return false; | |
189 | } | |
190 | ||
62b01cb5 ILT |
191 | static inline bool |
192 | is_lines_only_debug_section(const char* str) | |
193 | { | |
194 | // We can do this faster: binary search or a hashtable. But why bother? | |
195 | for (size_t i = 0; | |
196 | i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections); | |
197 | ++i) | |
198 | if (strcmp(str, lines_only_debug_sections[i]) == 0) | |
199 | return true; | |
200 | return false; | |
201 | } | |
202 | ||
a2fb1b05 ILT |
203 | // Whether to include this section in the link. |
204 | ||
205 | template<int size, bool big_endian> | |
206 | bool | |
730cdc88 | 207 | Layout::include_section(Sized_relobj<size, big_endian>*, const char* name, |
a2fb1b05 ILT |
208 | const elfcpp::Shdr<size, big_endian>& shdr) |
209 | { | |
fd06b4aa CC |
210 | if (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE) |
211 | return false; | |
212 | ||
a2fb1b05 ILT |
213 | switch (shdr.get_sh_type()) |
214 | { | |
215 | case elfcpp::SHT_NULL: | |
216 | case elfcpp::SHT_SYMTAB: | |
217 | case elfcpp::SHT_DYNSYM: | |
a2fb1b05 ILT |
218 | case elfcpp::SHT_HASH: |
219 | case elfcpp::SHT_DYNAMIC: | |
220 | case elfcpp::SHT_SYMTAB_SHNDX: | |
221 | return false; | |
222 | ||
5cb66f97 ILT |
223 | case elfcpp::SHT_STRTAB: |
224 | // Discard the sections which have special meanings in the ELF | |
225 | // ABI. Keep others (e.g., .stabstr). We could also do this by | |
226 | // checking the sh_link fields of the appropriate sections. | |
227 | return (strcmp(name, ".dynstr") != 0 | |
228 | && strcmp(name, ".strtab") != 0 | |
229 | && strcmp(name, ".shstrtab") != 0); | |
230 | ||
a2fb1b05 ILT |
231 | case elfcpp::SHT_RELA: |
232 | case elfcpp::SHT_REL: | |
233 | case elfcpp::SHT_GROUP: | |
7019cd25 ILT |
234 | // If we are emitting relocations these should be handled |
235 | // elsewhere. | |
8851ecca ILT |
236 | gold_assert(!parameters->options().relocatable() |
237 | && !parameters->options().emit_relocs()); | |
6a74a719 | 238 | return false; |
a2fb1b05 | 239 | |
9e2dcb77 | 240 | case elfcpp::SHT_PROGBITS: |
8851ecca | 241 | if (parameters->options().strip_debug() |
9e2dcb77 ILT |
242 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) |
243 | { | |
e94cf127 | 244 | if (is_debug_info_section(name)) |
9e2dcb77 ILT |
245 | return false; |
246 | } | |
62b01cb5 ILT |
247 | if (parameters->options().strip_debug_non_line() |
248 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
249 | { | |
250 | // Debugging sections can only be recognized by name. | |
251 | if (is_prefix_of(".debug", name) | |
252 | && !is_lines_only_debug_section(name)) | |
253 | return false; | |
254 | } | |
8851ecca | 255 | if (parameters->options().strip_debug_gdb() |
02d2ba74 ILT |
256 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) |
257 | { | |
258 | // Debugging sections can only be recognized by name. | |
259 | if (is_prefix_of(".debug", name) | |
260 | && !is_gdb_debug_section(name)) | |
261 | return false; | |
262 | } | |
fd06b4aa CC |
263 | if (parameters->options().strip_lto_sections() |
264 | && !parameters->options().relocatable() | |
265 | && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0) | |
266 | { | |
267 | // Ignore LTO sections containing intermediate code. | |
268 | if (is_prefix_of(".gnu.lto_", name)) | |
269 | return false; | |
270 | } | |
9e2dcb77 ILT |
271 | return true; |
272 | ||
a2fb1b05 | 273 | default: |
a2fb1b05 ILT |
274 | return true; |
275 | } | |
276 | } | |
277 | ||
ead1e424 | 278 | // Return an output section named NAME, or NULL if there is none. |
a2fb1b05 | 279 | |
a2fb1b05 | 280 | Output_section* |
ead1e424 | 281 | Layout::find_output_section(const char* name) const |
a2fb1b05 | 282 | { |
a445fddf ILT |
283 | for (Section_list::const_iterator p = this->section_list_.begin(); |
284 | p != this->section_list_.end(); | |
ead1e424 | 285 | ++p) |
a445fddf ILT |
286 | if (strcmp((*p)->name(), name) == 0) |
287 | return *p; | |
ead1e424 ILT |
288 | return NULL; |
289 | } | |
a2fb1b05 | 290 | |
ead1e424 ILT |
291 | // Return an output segment of type TYPE, with segment flags SET set |
292 | // and segment flags CLEAR clear. Return NULL if there is none. | |
a2fb1b05 | 293 | |
ead1e424 ILT |
294 | Output_segment* |
295 | Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set, | |
296 | elfcpp::Elf_Word clear) const | |
297 | { | |
298 | for (Segment_list::const_iterator p = this->segment_list_.begin(); | |
299 | p != this->segment_list_.end(); | |
300 | ++p) | |
301 | if (static_cast<elfcpp::PT>((*p)->type()) == type | |
302 | && ((*p)->flags() & set) == set | |
303 | && ((*p)->flags() & clear) == 0) | |
304 | return *p; | |
305 | return NULL; | |
306 | } | |
a2fb1b05 | 307 | |
ead1e424 | 308 | // Return the output section to use for section NAME with type TYPE |
a445fddf ILT |
309 | // and section flags FLAGS. NAME must be canonicalized in the string |
310 | // pool, and NAME_KEY is the key. | |
a2fb1b05 | 311 | |
ead1e424 | 312 | Output_section* |
f0641a0b ILT |
313 | Layout::get_output_section(const char* name, Stringpool::Key name_key, |
314 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags) | |
ead1e424 | 315 | { |
154e0e9a ILT |
316 | elfcpp::Elf_Xword lookup_flags = flags; |
317 | ||
318 | // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine | |
319 | // read-write with read-only sections. Some other ELF linkers do | |
320 | // not do this. FIXME: Perhaps there should be an option | |
321 | // controlling this. | |
322 | lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR); | |
323 | ||
324 | const Key key(name_key, std::make_pair(type, lookup_flags)); | |
a2fb1b05 ILT |
325 | const std::pair<Key, Output_section*> v(key, NULL); |
326 | std::pair<Section_name_map::iterator, bool> ins( | |
327 | this->section_name_map_.insert(v)); | |
328 | ||
a2fb1b05 | 329 | if (!ins.second) |
ead1e424 | 330 | return ins.first->second; |
a2fb1b05 ILT |
331 | else |
332 | { | |
333 | // This is the first time we've seen this name/type/flags | |
4e2b1697 ILT |
334 | // combination. For compatibility with the GNU linker, we |
335 | // combine sections with contents and zero flags with sections | |
336 | // with non-zero flags. This is a workaround for cases where | |
337 | // assembler code forgets to set section flags. FIXME: Perhaps | |
338 | // there should be an option to control this. | |
15cf077e | 339 | Output_section* os = NULL; |
4e2b1697 ILT |
340 | |
341 | if (type == elfcpp::SHT_PROGBITS) | |
15cf077e | 342 | { |
4e2b1697 ILT |
343 | if (flags == 0) |
344 | { | |
345 | Output_section* same_name = this->find_output_section(name); | |
346 | if (same_name != NULL | |
347 | && same_name->type() == elfcpp::SHT_PROGBITS | |
348 | && (same_name->flags() & elfcpp::SHF_TLS) == 0) | |
349 | os = same_name; | |
350 | } | |
351 | else if ((flags & elfcpp::SHF_TLS) == 0) | |
352 | { | |
353 | elfcpp::Elf_Xword zero_flags = 0; | |
354 | const Key zero_key(name_key, std::make_pair(type, zero_flags)); | |
355 | Section_name_map::iterator p = | |
356 | this->section_name_map_.find(zero_key); | |
357 | if (p != this->section_name_map_.end()) | |
154e0e9a | 358 | os = p->second; |
4e2b1697 | 359 | } |
15cf077e | 360 | } |
4e2b1697 | 361 | |
15cf077e ILT |
362 | if (os == NULL) |
363 | os = this->make_output_section(name, type, flags); | |
a2fb1b05 | 364 | ins.first->second = os; |
ead1e424 | 365 | return os; |
a2fb1b05 | 366 | } |
ead1e424 ILT |
367 | } |
368 | ||
a445fddf ILT |
369 | // Pick the output section to use for section NAME, in input file |
370 | // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a | |
154e0e9a ILT |
371 | // linker created section. IS_INPUT_SECTION is true if we are |
372 | // choosing an output section for an input section found in a input | |
373 | // file. This will return NULL if the input section should be | |
374 | // discarded. | |
a445fddf ILT |
375 | |
376 | Output_section* | |
377 | Layout::choose_output_section(const Relobj* relobj, const char* name, | |
378 | elfcpp::Elf_Word type, elfcpp::Elf_Xword flags, | |
154e0e9a | 379 | bool is_input_section) |
a445fddf | 380 | { |
154e0e9a ILT |
381 | // We should not see any input sections after we have attached |
382 | // sections to segments. | |
383 | gold_assert(!is_input_section || !this->sections_are_attached_); | |
384 | ||
385 | // Some flags in the input section should not be automatically | |
386 | // copied to the output section. | |
a445fddf ILT |
387 | flags &= ~ (elfcpp::SHF_INFO_LINK |
388 | | elfcpp::SHF_LINK_ORDER | |
389 | | elfcpp::SHF_GROUP | |
390 | | elfcpp::SHF_MERGE | |
391 | | elfcpp::SHF_STRINGS); | |
392 | ||
393 | if (this->script_options_->saw_sections_clause()) | |
394 | { | |
395 | // We are using a SECTIONS clause, so the output section is | |
396 | // chosen based only on the name. | |
397 | ||
398 | Script_sections* ss = this->script_options_->script_sections(); | |
399 | const char* file_name = relobj == NULL ? NULL : relobj->name().c_str(); | |
400 | Output_section** output_section_slot; | |
401 | name = ss->output_section_name(file_name, name, &output_section_slot); | |
402 | if (name == NULL) | |
403 | { | |
404 | // The SECTIONS clause says to discard this input section. | |
405 | return NULL; | |
406 | } | |
407 | ||
408 | // If this is an orphan section--one not mentioned in the linker | |
409 | // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the | |
410 | // default processing below. | |
411 | ||
412 | if (output_section_slot != NULL) | |
413 | { | |
414 | if (*output_section_slot != NULL) | |
154e0e9a | 415 | return *output_section_slot; |
a445fddf ILT |
416 | |
417 | // We don't put sections found in the linker script into | |
418 | // SECTION_NAME_MAP_. That keeps us from getting confused | |
419 | // if an orphan section is mapped to a section with the same | |
420 | // name as one in the linker script. | |
421 | ||
422 | name = this->namepool_.add(name, false, NULL); | |
423 | ||
424 | Output_section* os = this->make_output_section(name, type, flags); | |
425 | os->set_found_in_sections_clause(); | |
426 | *output_section_slot = os; | |
427 | return os; | |
428 | } | |
429 | } | |
430 | ||
431 | // FIXME: Handle SHF_OS_NONCONFORMING somewhere. | |
432 | ||
433 | // Turn NAME from the name of the input section into the name of the | |
434 | // output section. | |
435 | ||
436 | size_t len = strlen(name); | |
154e0e9a | 437 | if (is_input_section && !parameters->options().relocatable()) |
a445fddf ILT |
438 | name = Layout::output_section_name(name, &len); |
439 | ||
440 | Stringpool::Key name_key; | |
441 | name = this->namepool_.add_with_length(name, len, true, &name_key); | |
442 | ||
443 | // Find or make the output section. The output section is selected | |
444 | // based on the section name, type, and flags. | |
445 | return this->get_output_section(name, name_key, type, flags); | |
446 | } | |
447 | ||
ead1e424 | 448 | // Return the output section to use for input section SHNDX, with name |
730cdc88 ILT |
449 | // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the |
450 | // index of a relocation section which applies to this section, or 0 | |
451 | // if none, or -1U if more than one. RELOC_TYPE is the type of the | |
452 | // relocation section if there is one. Set *OFF to the offset of this | |
453 | // input section without the output section. Return NULL if the | |
454 | // section should be discarded. Set *OFF to -1 if the section | |
455 | // contents should not be written directly to the output file, but | |
456 | // will instead receive special handling. | |
ead1e424 ILT |
457 | |
458 | template<int size, bool big_endian> | |
459 | Output_section* | |
730cdc88 ILT |
460 | Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx, |
461 | const char* name, const elfcpp::Shdr<size, big_endian>& shdr, | |
462 | unsigned int reloc_shndx, unsigned int, off_t* off) | |
ead1e424 | 463 | { |
ef9beddf ILT |
464 | *off = 0; |
465 | ||
ead1e424 ILT |
466 | if (!this->include_section(object, name, shdr)) |
467 | return NULL; | |
468 | ||
6a74a719 ILT |
469 | Output_section* os; |
470 | ||
471 | // In a relocatable link a grouped section must not be combined with | |
472 | // any other sections. | |
8851ecca | 473 | if (parameters->options().relocatable() |
6a74a719 ILT |
474 | && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0) |
475 | { | |
476 | name = this->namepool_.add(name, true, NULL); | |
477 | os = this->make_output_section(name, shdr.get_sh_type(), | |
478 | shdr.get_sh_flags()); | |
479 | } | |
480 | else | |
481 | { | |
482 | os = this->choose_output_section(object, name, shdr.get_sh_type(), | |
483 | shdr.get_sh_flags(), true); | |
484 | if (os == NULL) | |
485 | return NULL; | |
486 | } | |
a2fb1b05 | 487 | |
2fd32231 ILT |
488 | // By default the GNU linker sorts input sections whose names match |
489 | // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections | |
490 | // are sorted by name. This is used to implement constructor | |
491 | // priority ordering. We are compatible. | |
492 | if (!this->script_options_->saw_sections_clause() | |
493 | && (is_prefix_of(".ctors.", name) | |
494 | || is_prefix_of(".dtors.", name) | |
495 | || is_prefix_of(".init_array.", name) | |
496 | || is_prefix_of(".fini_array.", name))) | |
497 | os->set_must_sort_attached_input_sections(); | |
498 | ||
a2fb1b05 ILT |
499 | // FIXME: Handle SHF_LINK_ORDER somewhere. |
500 | ||
a445fddf ILT |
501 | *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx, |
502 | this->script_options_->saw_sections_clause()); | |
a2fb1b05 ILT |
503 | |
504 | return os; | |
505 | } | |
506 | ||
6a74a719 ILT |
507 | // Handle a relocation section when doing a relocatable link. |
508 | ||
509 | template<int size, bool big_endian> | |
510 | Output_section* | |
511 | Layout::layout_reloc(Sized_relobj<size, big_endian>* object, | |
512 | unsigned int, | |
513 | const elfcpp::Shdr<size, big_endian>& shdr, | |
514 | Output_section* data_section, | |
515 | Relocatable_relocs* rr) | |
516 | { | |
8851ecca ILT |
517 | gold_assert(parameters->options().relocatable() |
518 | || parameters->options().emit_relocs()); | |
6a74a719 ILT |
519 | |
520 | int sh_type = shdr.get_sh_type(); | |
521 | ||
522 | std::string name; | |
523 | if (sh_type == elfcpp::SHT_REL) | |
524 | name = ".rel"; | |
525 | else if (sh_type == elfcpp::SHT_RELA) | |
526 | name = ".rela"; | |
527 | else | |
528 | gold_unreachable(); | |
529 | name += data_section->name(); | |
530 | ||
531 | Output_section* os = this->choose_output_section(object, name.c_str(), | |
532 | sh_type, | |
533 | shdr.get_sh_flags(), | |
534 | false); | |
535 | ||
536 | os->set_should_link_to_symtab(); | |
537 | os->set_info_section(data_section); | |
538 | ||
539 | Output_section_data* posd; | |
540 | if (sh_type == elfcpp::SHT_REL) | |
541 | { | |
542 | os->set_entsize(elfcpp::Elf_sizes<size>::rel_size); | |
543 | posd = new Output_relocatable_relocs<elfcpp::SHT_REL, | |
544 | size, | |
545 | big_endian>(rr); | |
546 | } | |
547 | else if (sh_type == elfcpp::SHT_RELA) | |
548 | { | |
549 | os->set_entsize(elfcpp::Elf_sizes<size>::rela_size); | |
550 | posd = new Output_relocatable_relocs<elfcpp::SHT_RELA, | |
551 | size, | |
552 | big_endian>(rr); | |
553 | } | |
554 | else | |
555 | gold_unreachable(); | |
556 | ||
557 | os->add_output_section_data(posd); | |
558 | rr->set_output_data(posd); | |
559 | ||
560 | return os; | |
561 | } | |
562 | ||
563 | // Handle a group section when doing a relocatable link. | |
564 | ||
565 | template<int size, bool big_endian> | |
566 | void | |
567 | Layout::layout_group(Symbol_table* symtab, | |
568 | Sized_relobj<size, big_endian>* object, | |
569 | unsigned int, | |
570 | const char* group_section_name, | |
571 | const char* signature, | |
572 | const elfcpp::Shdr<size, big_endian>& shdr, | |
8825ac63 ILT |
573 | elfcpp::Elf_Word flags, |
574 | std::vector<unsigned int>* shndxes) | |
6a74a719 | 575 | { |
8851ecca | 576 | gold_assert(parameters->options().relocatable()); |
6a74a719 ILT |
577 | gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP); |
578 | group_section_name = this->namepool_.add(group_section_name, true, NULL); | |
579 | Output_section* os = this->make_output_section(group_section_name, | |
580 | elfcpp::SHT_GROUP, | |
581 | shdr.get_sh_flags()); | |
582 | ||
583 | // We need to find a symbol with the signature in the symbol table. | |
755ab8af | 584 | // If we don't find one now, we need to look again later. |
6a74a719 | 585 | Symbol* sym = symtab->lookup(signature, NULL); |
755ab8af ILT |
586 | if (sym != NULL) |
587 | os->set_info_symndx(sym); | |
588 | else | |
589 | { | |
590 | // We will wind up using a symbol whose name is the signature. | |
591 | // So just put the signature in the symbol name pool to save it. | |
592 | signature = symtab->canonicalize_name(signature); | |
593 | this->group_signatures_.push_back(Group_signature(os, signature)); | |
594 | } | |
6a74a719 ILT |
595 | |
596 | os->set_should_link_to_symtab(); | |
6a74a719 ILT |
597 | os->set_entsize(4); |
598 | ||
599 | section_size_type entry_count = | |
600 | convert_to_section_size_type(shdr.get_sh_size() / 4); | |
601 | Output_section_data* posd = | |
8825ac63 ILT |
602 | new Output_data_group<size, big_endian>(object, entry_count, flags, |
603 | shndxes); | |
6a74a719 ILT |
604 | os->add_output_section_data(posd); |
605 | } | |
606 | ||
730cdc88 ILT |
607 | // Special GNU handling of sections name .eh_frame. They will |
608 | // normally hold exception frame data as defined by the C++ ABI | |
609 | // (http://codesourcery.com/cxx-abi/). | |
3151305a ILT |
610 | |
611 | template<int size, bool big_endian> | |
730cdc88 ILT |
612 | Output_section* |
613 | Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object, | |
614 | const unsigned char* symbols, | |
615 | off_t symbols_size, | |
616 | const unsigned char* symbol_names, | |
617 | off_t symbol_names_size, | |
3151305a | 618 | unsigned int shndx, |
3151305a | 619 | const elfcpp::Shdr<size, big_endian>& shdr, |
730cdc88 ILT |
620 | unsigned int reloc_shndx, unsigned int reloc_type, |
621 | off_t* off) | |
3151305a | 622 | { |
730cdc88 | 623 | gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS); |
1650c4ff | 624 | gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0); |
730cdc88 | 625 | |
a445fddf ILT |
626 | const char* const name = ".eh_frame"; |
627 | Output_section* os = this->choose_output_section(object, | |
628 | name, | |
629 | elfcpp::SHT_PROGBITS, | |
630 | elfcpp::SHF_ALLOC, | |
631 | false); | |
632 | if (os == NULL) | |
633 | return NULL; | |
730cdc88 | 634 | |
3151305a ILT |
635 | if (this->eh_frame_section_ == NULL) |
636 | { | |
637 | this->eh_frame_section_ = os; | |
730cdc88 | 638 | this->eh_frame_data_ = new Eh_frame(); |
3151305a | 639 | |
45aa233b | 640 | if (this->options_.eh_frame_hdr()) |
3151305a | 641 | { |
3151305a | 642 | Output_section* hdr_os = |
a445fddf ILT |
643 | this->choose_output_section(NULL, |
644 | ".eh_frame_hdr", | |
645 | elfcpp::SHT_PROGBITS, | |
646 | elfcpp::SHF_ALLOC, | |
647 | false); | |
3151305a | 648 | |
a445fddf ILT |
649 | if (hdr_os != NULL) |
650 | { | |
651 | Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os, | |
652 | this->eh_frame_data_); | |
653 | hdr_os->add_output_section_data(hdr_posd); | |
3151305a | 654 | |
a445fddf | 655 | hdr_os->set_after_input_sections(); |
730cdc88 | 656 | |
1c4f3631 ILT |
657 | if (!this->script_options_->saw_phdrs_clause()) |
658 | { | |
659 | Output_segment* hdr_oseg; | |
660 | hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME, | |
661 | elfcpp::PF_R); | |
662 | hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R); | |
663 | } | |
730cdc88 | 664 | |
a445fddf ILT |
665 | this->eh_frame_data_->set_eh_frame_hdr(hdr_posd); |
666 | } | |
3151305a ILT |
667 | } |
668 | } | |
669 | ||
670 | gold_assert(this->eh_frame_section_ == os); | |
671 | ||
730cdc88 ILT |
672 | if (this->eh_frame_data_->add_ehframe_input_section(object, |
673 | symbols, | |
674 | symbols_size, | |
675 | symbol_names, | |
676 | symbol_names_size, | |
677 | shndx, | |
678 | reloc_shndx, | |
679 | reloc_type)) | |
2c38906f | 680 | { |
154e0e9a ILT |
681 | os->update_flags_for_input_section(shdr.get_sh_flags()); |
682 | ||
2c38906f ILT |
683 | // We found a .eh_frame section we are going to optimize, so now |
684 | // we can add the set of optimized sections to the output | |
685 | // section. We need to postpone adding this until we've found a | |
686 | // section we can optimize so that the .eh_frame section in | |
687 | // crtbegin.o winds up at the start of the output section. | |
688 | if (!this->added_eh_frame_data_) | |
689 | { | |
690 | os->add_output_section_data(this->eh_frame_data_); | |
691 | this->added_eh_frame_data_ = true; | |
692 | } | |
693 | *off = -1; | |
694 | } | |
730cdc88 ILT |
695 | else |
696 | { | |
697 | // We couldn't handle this .eh_frame section for some reason. | |
698 | // Add it as a normal section. | |
a445fddf ILT |
699 | bool saw_sections_clause = this->script_options_->saw_sections_clause(); |
700 | *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx, | |
701 | saw_sections_clause); | |
730cdc88 ILT |
702 | } |
703 | ||
704 | return os; | |
3151305a ILT |
705 | } |
706 | ||
9f1d377b ILT |
707 | // Add POSD to an output section using NAME, TYPE, and FLAGS. Return |
708 | // the output section. | |
ead1e424 | 709 | |
9f1d377b | 710 | Output_section* |
ead1e424 ILT |
711 | Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type, |
712 | elfcpp::Elf_Xword flags, | |
713 | Output_section_data* posd) | |
714 | { | |
a445fddf ILT |
715 | Output_section* os = this->choose_output_section(NULL, name, type, flags, |
716 | false); | |
717 | if (os != NULL) | |
718 | os->add_output_section_data(posd); | |
9f1d377b | 719 | return os; |
ead1e424 ILT |
720 | } |
721 | ||
a2fb1b05 ILT |
722 | // Map section flags to segment flags. |
723 | ||
724 | elfcpp::Elf_Word | |
725 | Layout::section_flags_to_segment(elfcpp::Elf_Xword flags) | |
726 | { | |
727 | elfcpp::Elf_Word ret = elfcpp::PF_R; | |
728 | if ((flags & elfcpp::SHF_WRITE) != 0) | |
729 | ret |= elfcpp::PF_W; | |
730 | if ((flags & elfcpp::SHF_EXECINSTR) != 0) | |
731 | ret |= elfcpp::PF_X; | |
732 | return ret; | |
733 | } | |
734 | ||
96803768 ILT |
735 | // Sometimes we compress sections. This is typically done for |
736 | // sections that are not part of normal program execution (such as | |
737 | // .debug_* sections), and where the readers of these sections know | |
738 | // how to deal with compressed sections. (To make it easier for them, | |
739 | // we will rename the ouput section in such cases from .foo to | |
740 | // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine | |
741 | // doesn't say for certain whether we'll compress -- it depends on | |
742 | // commandline options as well -- just whether this section is a | |
743 | // candidate for compression. | |
744 | ||
745 | static bool | |
746 | is_compressible_debug_section(const char* secname) | |
747 | { | |
748 | return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0); | |
749 | } | |
750 | ||
a2fb1b05 ILT |
751 | // Make a new Output_section, and attach it to segments as |
752 | // appropriate. | |
753 | ||
754 | Output_section* | |
755 | Layout::make_output_section(const char* name, elfcpp::Elf_Word type, | |
756 | elfcpp::Elf_Xword flags) | |
757 | { | |
96803768 ILT |
758 | Output_section* os; |
759 | if ((flags & elfcpp::SHF_ALLOC) == 0 | |
ee1fe73e | 760 | && strcmp(this->options_.compress_debug_sections(), "none") != 0 |
96803768 ILT |
761 | && is_compressible_debug_section(name)) |
762 | os = new Output_compressed_section(&this->options_, name, type, flags); | |
62b01cb5 ILT |
763 | |
764 | else if ((flags & elfcpp::SHF_ALLOC) == 0 | |
765 | && this->options_.strip_debug_non_line() | |
766 | && strcmp(".debug_abbrev", name) == 0) | |
767 | { | |
768 | os = this->debug_abbrev_ = new Output_reduced_debug_abbrev_section( | |
769 | name, type, flags); | |
770 | if (this->debug_info_) | |
771 | this->debug_info_->set_abbreviations(this->debug_abbrev_); | |
772 | } | |
773 | else if ((flags & elfcpp::SHF_ALLOC) == 0 | |
774 | && this->options_.strip_debug_non_line() | |
775 | && strcmp(".debug_info", name) == 0) | |
776 | { | |
777 | os = this->debug_info_ = new Output_reduced_debug_info_section( | |
778 | name, type, flags); | |
779 | if (this->debug_abbrev_) | |
780 | this->debug_info_->set_abbreviations(this->debug_abbrev_); | |
781 | } | |
782 | else | |
96803768 ILT |
783 | os = new Output_section(name, type, flags); |
784 | ||
a3ad94ed | 785 | this->section_list_.push_back(os); |
a2fb1b05 | 786 | |
2fd32231 ILT |
787 | // The GNU linker by default sorts some sections by priority, so we |
788 | // do the same. We need to know that this might happen before we | |
789 | // attach any input sections. | |
790 | if (!this->script_options_->saw_sections_clause() | |
791 | && (strcmp(name, ".ctors") == 0 | |
792 | || strcmp(name, ".dtors") == 0 | |
793 | || strcmp(name, ".init_array") == 0 | |
794 | || strcmp(name, ".fini_array") == 0)) | |
795 | os->set_may_sort_attached_input_sections(); | |
796 | ||
9f1d377b ILT |
797 | // With -z relro, we have to recognize the special sections by name. |
798 | // There is no other way. | |
799 | if (!this->script_options_->saw_sections_clause() | |
800 | && parameters->options().relro() | |
801 | && type == elfcpp::SHT_PROGBITS | |
802 | && (flags & elfcpp::SHF_ALLOC) != 0 | |
803 | && (flags & elfcpp::SHF_WRITE) != 0) | |
804 | { | |
805 | if (strcmp(name, ".data.rel.ro") == 0) | |
806 | os->set_is_relro(); | |
807 | else if (strcmp(name, ".data.rel.ro.local") == 0) | |
808 | { | |
809 | os->set_is_relro(); | |
810 | os->set_is_relro_local(); | |
811 | } | |
812 | } | |
813 | ||
154e0e9a ILT |
814 | // If we have already attached the sections to segments, then we |
815 | // need to attach this one now. This happens for sections created | |
816 | // directly by the linker. | |
817 | if (this->sections_are_attached_) | |
818 | this->attach_section_to_segment(os); | |
819 | ||
4e2b1697 ILT |
820 | return os; |
821 | } | |
a445fddf | 822 | |
154e0e9a ILT |
823 | // Attach output sections to segments. This is called after we have |
824 | // seen all the input sections. | |
825 | ||
826 | void | |
827 | Layout::attach_sections_to_segments() | |
828 | { | |
829 | for (Section_list::iterator p = this->section_list_.begin(); | |
830 | p != this->section_list_.end(); | |
831 | ++p) | |
832 | this->attach_section_to_segment(*p); | |
833 | ||
834 | this->sections_are_attached_ = true; | |
835 | } | |
836 | ||
837 | // Attach an output section to a segment. | |
838 | ||
839 | void | |
840 | Layout::attach_section_to_segment(Output_section* os) | |
841 | { | |
842 | if ((os->flags() & elfcpp::SHF_ALLOC) == 0) | |
843 | this->unattached_section_list_.push_back(os); | |
844 | else | |
845 | this->attach_allocated_section_to_segment(os); | |
846 | } | |
847 | ||
4e2b1697 | 848 | // Attach an allocated output section to a segment. |
1c4f3631 | 849 | |
4e2b1697 | 850 | void |
154e0e9a | 851 | Layout::attach_allocated_section_to_segment(Output_section* os) |
4e2b1697 | 852 | { |
154e0e9a | 853 | elfcpp::Elf_Xword flags = os->flags(); |
4e2b1697 | 854 | gold_assert((flags & elfcpp::SHF_ALLOC) != 0); |
a2fb1b05 | 855 | |
4e2b1697 ILT |
856 | if (parameters->options().relocatable()) |
857 | return; | |
a2fb1b05 | 858 | |
4e2b1697 ILT |
859 | // If we have a SECTIONS clause, we can't handle the attachment to |
860 | // segments until after we've seen all the sections. | |
861 | if (this->script_options_->saw_sections_clause()) | |
862 | return; | |
a2fb1b05 | 863 | |
4e2b1697 | 864 | gold_assert(!this->script_options_->saw_phdrs_clause()); |
756ac4a8 | 865 | |
4e2b1697 | 866 | // This output section goes into a PT_LOAD segment. |
a2fb1b05 | 867 | |
4e2b1697 | 868 | elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags); |
a2fb1b05 | 869 | |
4e2b1697 ILT |
870 | // In general the only thing we really care about for PT_LOAD |
871 | // segments is whether or not they are writable, so that is how we | |
872 | // search for them. People who need segments sorted on some other | |
873 | // basis will have to use a linker script. | |
a2fb1b05 | 874 | |
4e2b1697 ILT |
875 | Segment_list::const_iterator p; |
876 | for (p = this->segment_list_.begin(); | |
877 | p != this->segment_list_.end(); | |
878 | ++p) | |
879 | { | |
880 | if ((*p)->type() == elfcpp::PT_LOAD | |
af6156ef ILT |
881 | && (parameters->options().omagic() |
882 | || ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))) | |
4e2b1697 ILT |
883 | { |
884 | // If -Tbss was specified, we need to separate the data | |
885 | // and BSS segments. | |
886 | if (this->options_.user_set_Tbss()) | |
887 | { | |
888 | if ((os->type() == elfcpp::SHT_NOBITS) | |
889 | == (*p)->has_any_data_sections()) | |
890 | continue; | |
891 | } | |
892 | ||
893 | (*p)->add_output_section(os, seg_flags); | |
894 | break; | |
895 | } | |
896 | } | |
54dc6425 | 897 | |
4e2b1697 ILT |
898 | if (p == this->segment_list_.end()) |
899 | { | |
900 | Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD, | |
901 | seg_flags); | |
902 | oseg->add_output_section(os, seg_flags); | |
a2fb1b05 ILT |
903 | } |
904 | ||
4e2b1697 ILT |
905 | // If we see a loadable SHT_NOTE section, we create a PT_NOTE |
906 | // segment. | |
907 | if (os->type() == elfcpp::SHT_NOTE) | |
908 | { | |
909 | // See if we already have an equivalent PT_NOTE segment. | |
910 | for (p = this->segment_list_.begin(); | |
911 | p != segment_list_.end(); | |
912 | ++p) | |
913 | { | |
914 | if ((*p)->type() == elfcpp::PT_NOTE | |
915 | && (((*p)->flags() & elfcpp::PF_W) | |
916 | == (seg_flags & elfcpp::PF_W))) | |
917 | { | |
918 | (*p)->add_output_section(os, seg_flags); | |
919 | break; | |
920 | } | |
921 | } | |
922 | ||
923 | if (p == this->segment_list_.end()) | |
924 | { | |
925 | Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE, | |
926 | seg_flags); | |
927 | oseg->add_output_section(os, seg_flags); | |
928 | } | |
929 | } | |
930 | ||
931 | // If we see a loadable SHF_TLS section, we create a PT_TLS | |
932 | // segment. There can only be one such segment. | |
933 | if ((flags & elfcpp::SHF_TLS) != 0) | |
934 | { | |
935 | if (this->tls_segment_ == NULL) | |
2d924fd9 | 936 | this->make_output_segment(elfcpp::PT_TLS, seg_flags); |
4e2b1697 ILT |
937 | this->tls_segment_->add_output_section(os, seg_flags); |
938 | } | |
9f1d377b ILT |
939 | |
940 | // If -z relro is in effect, and we see a relro section, we create a | |
941 | // PT_GNU_RELRO segment. There can only be one such segment. | |
942 | if (os->is_relro() && parameters->options().relro()) | |
943 | { | |
944 | gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W)); | |
945 | if (this->relro_segment_ == NULL) | |
2d924fd9 | 946 | this->make_output_segment(elfcpp::PT_GNU_RELRO, seg_flags); |
9f1d377b ILT |
947 | this->relro_segment_->add_output_section(os, seg_flags); |
948 | } | |
a2fb1b05 ILT |
949 | } |
950 | ||
919ed24c ILT |
951 | // Make an output section for a script. |
952 | ||
953 | Output_section* | |
954 | Layout::make_output_section_for_script(const char* name) | |
955 | { | |
956 | name = this->namepool_.add(name, false, NULL); | |
957 | Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS, | |
958 | elfcpp::SHF_ALLOC); | |
959 | os->set_found_in_sections_clause(); | |
960 | return os; | |
961 | } | |
962 | ||
3802b2dd ILT |
963 | // Return the number of segments we expect to see. |
964 | ||
965 | size_t | |
966 | Layout::expected_segment_count() const | |
967 | { | |
968 | size_t ret = this->segment_list_.size(); | |
969 | ||
970 | // If we didn't see a SECTIONS clause in a linker script, we should | |
971 | // already have the complete list of segments. Otherwise we ask the | |
972 | // SECTIONS clause how many segments it expects, and add in the ones | |
973 | // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.) | |
974 | ||
975 | if (!this->script_options_->saw_sections_clause()) | |
976 | return ret; | |
977 | else | |
978 | { | |
979 | const Script_sections* ss = this->script_options_->script_sections(); | |
980 | return ret + ss->expected_segment_count(this); | |
981 | } | |
982 | } | |
983 | ||
35cdfc9a ILT |
984 | // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK |
985 | // is whether we saw a .note.GNU-stack section in the object file. | |
986 | // GNU_STACK_FLAGS is the section flags. The flags give the | |
987 | // protection required for stack memory. We record this in an | |
988 | // executable as a PT_GNU_STACK segment. If an object file does not | |
989 | // have a .note.GNU-stack segment, we must assume that it is an old | |
990 | // object. On some targets that will force an executable stack. | |
991 | ||
992 | void | |
993 | Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags) | |
994 | { | |
995 | if (!seen_gnu_stack) | |
996 | this->input_without_gnu_stack_note_ = true; | |
997 | else | |
998 | { | |
999 | this->input_with_gnu_stack_note_ = true; | |
1000 | if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0) | |
1001 | this->input_requires_executable_stack_ = true; | |
1002 | } | |
1003 | } | |
1004 | ||
a3ad94ed ILT |
1005 | // Create the dynamic sections which are needed before we read the |
1006 | // relocs. | |
1007 | ||
1008 | void | |
9b07f471 | 1009 | Layout::create_initial_dynamic_sections(Symbol_table* symtab) |
a3ad94ed | 1010 | { |
436ca963 | 1011 | if (parameters->doing_static_link()) |
a3ad94ed ILT |
1012 | return; |
1013 | ||
3802b2dd ILT |
1014 | this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic", |
1015 | elfcpp::SHT_DYNAMIC, | |
1016 | (elfcpp::SHF_ALLOC | |
1017 | | elfcpp::SHF_WRITE), | |
1018 | false); | |
9f1d377b | 1019 | this->dynamic_section_->set_is_relro(); |
a3ad94ed | 1020 | |
9b07f471 | 1021 | symtab->define_in_output_data("_DYNAMIC", NULL, this->dynamic_section_, 0, 0, |
a3ad94ed ILT |
1022 | elfcpp::STT_OBJECT, elfcpp::STB_LOCAL, |
1023 | elfcpp::STV_HIDDEN, 0, false, false); | |
16649710 | 1024 | |
9025d29d | 1025 | this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_); |
16649710 ILT |
1026 | |
1027 | this->dynamic_section_->add_output_section_data(this->dynamic_data_); | |
a3ad94ed ILT |
1028 | } |
1029 | ||
bfd58944 ILT |
1030 | // For each output section whose name can be represented as C symbol, |
1031 | // define __start and __stop symbols for the section. This is a GNU | |
1032 | // extension. | |
1033 | ||
1034 | void | |
9b07f471 | 1035 | Layout::define_section_symbols(Symbol_table* symtab) |
bfd58944 ILT |
1036 | { |
1037 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
1038 | p != this->section_list_.end(); | |
1039 | ++p) | |
1040 | { | |
1041 | const char* const name = (*p)->name(); | |
1042 | if (name[strspn(name, | |
1043 | ("0123456789" | |
1044 | "ABCDEFGHIJKLMNOPWRSTUVWXYZ" | |
1045 | "abcdefghijklmnopqrstuvwxyz" | |
1046 | "_"))] | |
1047 | == '\0') | |
1048 | { | |
1049 | const std::string name_string(name); | |
1050 | const std::string start_name("__start_" + name_string); | |
1051 | const std::string stop_name("__stop_" + name_string); | |
1052 | ||
9b07f471 | 1053 | symtab->define_in_output_data(start_name.c_str(), |
bfd58944 ILT |
1054 | NULL, // version |
1055 | *p, | |
1056 | 0, // value | |
1057 | 0, // symsize | |
1058 | elfcpp::STT_NOTYPE, | |
1059 | elfcpp::STB_GLOBAL, | |
1060 | elfcpp::STV_DEFAULT, | |
1061 | 0, // nonvis | |
1062 | false, // offset_is_from_end | |
a445fddf | 1063 | true); // only_if_ref |
bfd58944 | 1064 | |
9b07f471 | 1065 | symtab->define_in_output_data(stop_name.c_str(), |
bfd58944 ILT |
1066 | NULL, // version |
1067 | *p, | |
1068 | 0, // value | |
1069 | 0, // symsize | |
1070 | elfcpp::STT_NOTYPE, | |
1071 | elfcpp::STB_GLOBAL, | |
1072 | elfcpp::STV_DEFAULT, | |
1073 | 0, // nonvis | |
1074 | true, // offset_is_from_end | |
a445fddf | 1075 | true); // only_if_ref |
bfd58944 ILT |
1076 | } |
1077 | } | |
1078 | } | |
1079 | ||
755ab8af ILT |
1080 | // Define symbols for group signatures. |
1081 | ||
1082 | void | |
1083 | Layout::define_group_signatures(Symbol_table* symtab) | |
1084 | { | |
1085 | for (Group_signatures::iterator p = this->group_signatures_.begin(); | |
1086 | p != this->group_signatures_.end(); | |
1087 | ++p) | |
1088 | { | |
1089 | Symbol* sym = symtab->lookup(p->signature, NULL); | |
1090 | if (sym != NULL) | |
1091 | p->section->set_info_symndx(sym); | |
1092 | else | |
1093 | { | |
1094 | // Force the name of the group section to the group | |
1095 | // signature, and use the group's section symbol as the | |
1096 | // signature symbol. | |
1097 | if (strcmp(p->section->name(), p->signature) != 0) | |
1098 | { | |
1099 | const char* name = this->namepool_.add(p->signature, | |
1100 | true, NULL); | |
1101 | p->section->set_name(name); | |
1102 | } | |
1103 | p->section->set_needs_symtab_index(); | |
1104 | p->section->set_info_section_symndx(p->section); | |
1105 | } | |
1106 | } | |
1107 | ||
1108 | this->group_signatures_.clear(); | |
1109 | } | |
1110 | ||
75f65a3e ILT |
1111 | // Find the first read-only PT_LOAD segment, creating one if |
1112 | // necessary. | |
54dc6425 | 1113 | |
75f65a3e ILT |
1114 | Output_segment* |
1115 | Layout::find_first_load_seg() | |
54dc6425 | 1116 | { |
75f65a3e ILT |
1117 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
1118 | p != this->segment_list_.end(); | |
1119 | ++p) | |
1120 | { | |
1121 | if ((*p)->type() == elfcpp::PT_LOAD | |
1122 | && ((*p)->flags() & elfcpp::PF_R) != 0 | |
af6156ef ILT |
1123 | && (parameters->options().omagic() |
1124 | || ((*p)->flags() & elfcpp::PF_W) == 0)) | |
75f65a3e ILT |
1125 | return *p; |
1126 | } | |
1127 | ||
1c4f3631 ILT |
1128 | gold_assert(!this->script_options_->saw_phdrs_clause()); |
1129 | ||
3802b2dd ILT |
1130 | Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD, |
1131 | elfcpp::PF_R); | |
75f65a3e | 1132 | return load_seg; |
54dc6425 ILT |
1133 | } |
1134 | ||
1135 | // Finalize the layout. When this is called, we have created all the | |
1136 | // output sections and all the output segments which are based on | |
1137 | // input sections. We have several things to do, and we have to do | |
1138 | // them in the right order, so that we get the right results correctly | |
1139 | // and efficiently. | |
1140 | ||
1141 | // 1) Finalize the list of output segments and create the segment | |
1142 | // table header. | |
1143 | ||
1144 | // 2) Finalize the dynamic symbol table and associated sections. | |
1145 | ||
1146 | // 3) Determine the final file offset of all the output segments. | |
1147 | ||
1148 | // 4) Determine the final file offset of all the SHF_ALLOC output | |
1149 | // sections. | |
1150 | ||
75f65a3e ILT |
1151 | // 5) Create the symbol table sections and the section name table |
1152 | // section. | |
1153 | ||
1154 | // 6) Finalize the symbol table: set symbol values to their final | |
54dc6425 ILT |
1155 | // value and make a final determination of which symbols are going |
1156 | // into the output symbol table. | |
1157 | ||
54dc6425 ILT |
1158 | // 7) Create the section table header. |
1159 | ||
1160 | // 8) Determine the final file offset of all the output sections which | |
1161 | // are not SHF_ALLOC, including the section table header. | |
1162 | ||
1163 | // 9) Finalize the ELF file header. | |
1164 | ||
75f65a3e ILT |
1165 | // This function returns the size of the output file. |
1166 | ||
1167 | off_t | |
17a1d0a9 | 1168 | Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab, |
8851ecca | 1169 | Target* target, const Task* task) |
54dc6425 | 1170 | { |
7e1edb90 | 1171 | target->finalize_sections(this); |
5a6f7e2d | 1172 | |
17a1d0a9 | 1173 | this->count_local_symbols(task, input_objects); |
7bf1f802 | 1174 | |
35cdfc9a ILT |
1175 | this->create_gold_note(); |
1176 | this->create_executable_stack_info(target); | |
8ed814a9 | 1177 | this->create_build_id(); |
4f211c8b | 1178 | |
3802b2dd | 1179 | Output_segment* phdr_seg = NULL; |
8851ecca | 1180 | if (!parameters->options().relocatable() && !parameters->doing_static_link()) |
54dc6425 | 1181 | { |
dbe717ef ILT |
1182 | // There was a dynamic object in the link. We need to create |
1183 | // some information for the dynamic linker. | |
1184 | ||
3802b2dd ILT |
1185 | // Create the PT_PHDR segment which will hold the program |
1186 | // headers. | |
1c4f3631 ILT |
1187 | if (!this->script_options_->saw_phdrs_clause()) |
1188 | phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R); | |
3802b2dd | 1189 | |
14b31740 ILT |
1190 | // Create the dynamic symbol table, including the hash table. |
1191 | Output_section* dynstr; | |
1192 | std::vector<Symbol*> dynamic_symbols; | |
1193 | unsigned int local_dynamic_count; | |
a5dc0706 ILT |
1194 | Versions versions(*this->script_options()->version_script_info(), |
1195 | &this->dynpool_); | |
9b07f471 | 1196 | this->create_dynamic_symtab(input_objects, symtab, &dynstr, |
14b31740 ILT |
1197 | &local_dynamic_count, &dynamic_symbols, |
1198 | &versions); | |
dbe717ef ILT |
1199 | |
1200 | // Create the .interp section to hold the name of the | |
1201 | // interpreter, and put it in a PT_INTERP segment. | |
8851ecca | 1202 | if (!parameters->options().shared()) |
96f2030e | 1203 | this->create_interp(target); |
a3ad94ed ILT |
1204 | |
1205 | // Finish the .dynamic section to hold the dynamic data, and put | |
1206 | // it in a PT_DYNAMIC segment. | |
16649710 | 1207 | this->finish_dynamic_section(input_objects, symtab); |
14b31740 ILT |
1208 | |
1209 | // We should have added everything we need to the dynamic string | |
1210 | // table. | |
1211 | this->dynpool_.set_string_offsets(); | |
1212 | ||
1213 | // Create the version sections. We can't do this until the | |
1214 | // dynamic string table is complete. | |
46fe1623 | 1215 | this->create_version_sections(&versions, symtab, local_dynamic_count, |
14b31740 | 1216 | dynamic_symbols, dynstr); |
54dc6425 ILT |
1217 | } |
1218 | ||
a445fddf ILT |
1219 | // If there is a SECTIONS clause, put all the input sections into |
1220 | // the required order. | |
1221 | Output_segment* load_seg; | |
88dd47ac | 1222 | if (this->script_options_->saw_sections_clause()) |
a445fddf | 1223 | load_seg = this->set_section_addresses_from_script(symtab); |
8851ecca | 1224 | else if (parameters->options().relocatable()) |
88dd47ac | 1225 | load_seg = NULL; |
a445fddf ILT |
1226 | else |
1227 | load_seg = this->find_first_load_seg(); | |
54dc6425 | 1228 | |
7cc619c3 | 1229 | if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF) |
516cb3d0 ILT |
1230 | load_seg = NULL; |
1231 | ||
3802b2dd | 1232 | gold_assert(phdr_seg == NULL || load_seg != NULL); |
75f65a3e ILT |
1233 | |
1234 | // Lay out the segment headers. | |
75f65a3e | 1235 | Output_segment_headers* segment_headers; |
8851ecca | 1236 | if (parameters->options().relocatable()) |
6a74a719 ILT |
1237 | segment_headers = NULL; |
1238 | else | |
1239 | { | |
1240 | segment_headers = new Output_segment_headers(this->segment_list_); | |
1241 | if (load_seg != NULL) | |
1242 | load_seg->add_initial_output_data(segment_headers); | |
1243 | if (phdr_seg != NULL) | |
1244 | phdr_seg->add_initial_output_data(segment_headers); | |
1245 | } | |
75f65a3e ILT |
1246 | |
1247 | // Lay out the file header. | |
1248 | Output_file_header* file_header; | |
d391083d | 1249 | file_header = new Output_file_header(target, symtab, segment_headers, |
a5dc0706 | 1250 | this->options_.entry()); |
a445fddf ILT |
1251 | if (load_seg != NULL) |
1252 | load_seg->add_initial_output_data(file_header); | |
1253 | ||
61ba1cf9 | 1254 | this->special_output_list_.push_back(file_header); |
6a74a719 ILT |
1255 | if (segment_headers != NULL) |
1256 | this->special_output_list_.push_back(segment_headers); | |
75f65a3e | 1257 | |
6a74a719 | 1258 | if (this->script_options_->saw_phdrs_clause() |
8851ecca | 1259 | && !parameters->options().relocatable()) |
1c4f3631 ILT |
1260 | { |
1261 | // Support use of FILEHDRS and PHDRS attachments in a PHDRS | |
1262 | // clause in a linker script. | |
1263 | Script_sections* ss = this->script_options_->script_sections(); | |
1264 | ss->put_headers_in_phdrs(file_header, segment_headers); | |
1265 | } | |
1266 | ||
ead1e424 | 1267 | // We set the output section indexes in set_segment_offsets and |
27bc2bce | 1268 | // set_section_indexes. |
ead1e424 ILT |
1269 | unsigned int shndx = 1; |
1270 | ||
1271 | // Set the file offsets of all the segments, and all the sections | |
1272 | // they contain. | |
6a74a719 | 1273 | off_t off; |
8851ecca | 1274 | if (!parameters->options().relocatable()) |
6a74a719 ILT |
1275 | off = this->set_segment_offsets(target, load_seg, &shndx); |
1276 | else | |
1277 | off = this->set_relocatable_section_offsets(file_header, &shndx); | |
75f65a3e | 1278 | |
a9a60db6 ILT |
1279 | // Set the file offsets of all the non-data sections we've seen so |
1280 | // far which don't have to wait for the input sections. We need | |
1281 | // this in order to finalize local symbols in non-allocated | |
1282 | // sections. | |
1283 | off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS); | |
1284 | ||
d491d34e ILT |
1285 | // Set the section indexes of all unallocated sections seen so far, |
1286 | // in case any of them are somehow referenced by a symbol. | |
1287 | shndx = this->set_section_indexes(shndx); | |
1288 | ||
75f65a3e | 1289 | // Create the symbol table sections. |
d491d34e | 1290 | this->create_symtab_sections(input_objects, symtab, shndx, &off); |
7bf1f802 ILT |
1291 | if (!parameters->doing_static_link()) |
1292 | this->assign_local_dynsym_offsets(input_objects); | |
75f65a3e | 1293 | |
e5756efb ILT |
1294 | // Process any symbol assignments from a linker script. This must |
1295 | // be called after the symbol table has been finalized. | |
1296 | this->script_options_->finalize_symbols(symtab, this); | |
1297 | ||
75f65a3e ILT |
1298 | // Create the .shstrtab section. |
1299 | Output_section* shstrtab_section = this->create_shstrtab(); | |
1300 | ||
a9a60db6 ILT |
1301 | // Set the file offsets of the rest of the non-data sections which |
1302 | // don't have to wait for the input sections. | |
9a0910c3 | 1303 | off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS); |
86887060 | 1304 | |
d491d34e ILT |
1305 | // Now that all sections have been created, set the section indexes |
1306 | // for any sections which haven't been done yet. | |
86887060 | 1307 | shndx = this->set_section_indexes(shndx); |
ead1e424 | 1308 | |
75f65a3e | 1309 | // Create the section table header. |
d491d34e | 1310 | this->create_shdrs(shstrtab_section, &off); |
75f65a3e | 1311 | |
17a1d0a9 ILT |
1312 | // If there are no sections which require postprocessing, we can |
1313 | // handle the section names now, and avoid a resize later. | |
1314 | if (!this->any_postprocessing_sections_) | |
1315 | off = this->set_section_offsets(off, | |
1316 | STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS); | |
1317 | ||
27bc2bce | 1318 | file_header->set_section_info(this->section_headers_, shstrtab_section); |
75f65a3e | 1319 | |
27bc2bce ILT |
1320 | // Now we know exactly where everything goes in the output file |
1321 | // (except for non-allocated sections which require postprocessing). | |
a3ad94ed | 1322 | Output_data::layout_complete(); |
75f65a3e | 1323 | |
e44fcf3b ILT |
1324 | this->output_file_size_ = off; |
1325 | ||
75f65a3e ILT |
1326 | return off; |
1327 | } | |
1328 | ||
8ed814a9 ILT |
1329 | // Create a note header following the format defined in the ELF ABI. |
1330 | // NAME is the name, NOTE_TYPE is the type, DESCSZ is the size of the | |
1331 | // descriptor. ALLOCATE is true if the section should be allocated in | |
1332 | // memory. This returns the new note section. It sets | |
1333 | // *TRAILING_PADDING to the number of trailing zero bytes required. | |
4f211c8b | 1334 | |
8ed814a9 ILT |
1335 | Output_section* |
1336 | Layout::create_note(const char* name, int note_type, size_t descsz, | |
1337 | bool allocate, size_t* trailing_padding) | |
4f211c8b | 1338 | { |
e2305dc0 ILT |
1339 | // Authorities all agree that the values in a .note field should |
1340 | // be aligned on 4-byte boundaries for 32-bit binaries. However, | |
1341 | // they differ on what the alignment is for 64-bit binaries. | |
1342 | // The GABI says unambiguously they take 8-byte alignment: | |
1343 | // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section | |
1344 | // Other documentation says alignment should always be 4 bytes: | |
1345 | // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format | |
1346 | // GNU ld and GNU readelf both support the latter (at least as of | |
1347 | // version 2.16.91), and glibc always generates the latter for | |
1348 | // .note.ABI-tag (as of version 1.6), so that's the one we go with | |
1349 | // here. | |
35cdfc9a | 1350 | #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default. |
8851ecca | 1351 | const int size = parameters->target().get_size(); |
e2305dc0 ILT |
1352 | #else |
1353 | const int size = 32; | |
1354 | #endif | |
4f211c8b ILT |
1355 | |
1356 | // The contents of the .note section. | |
4f211c8b ILT |
1357 | size_t namesz = strlen(name) + 1; |
1358 | size_t aligned_namesz = align_address(namesz, size / 8); | |
4f211c8b | 1359 | size_t aligned_descsz = align_address(descsz, size / 8); |
4f211c8b | 1360 | |
8ed814a9 | 1361 | size_t notehdrsz = 3 * (size / 8) + aligned_namesz; |
4f211c8b | 1362 | |
8ed814a9 ILT |
1363 | unsigned char* buffer = new unsigned char[notehdrsz]; |
1364 | memset(buffer, 0, notehdrsz); | |
4f211c8b | 1365 | |
8851ecca | 1366 | bool is_big_endian = parameters->target().is_big_endian(); |
4f211c8b ILT |
1367 | |
1368 | if (size == 32) | |
1369 | { | |
1370 | if (!is_big_endian) | |
1371 | { | |
1372 | elfcpp::Swap<32, false>::writeval(buffer, namesz); | |
1373 | elfcpp::Swap<32, false>::writeval(buffer + 4, descsz); | |
1374 | elfcpp::Swap<32, false>::writeval(buffer + 8, note_type); | |
1375 | } | |
1376 | else | |
1377 | { | |
1378 | elfcpp::Swap<32, true>::writeval(buffer, namesz); | |
1379 | elfcpp::Swap<32, true>::writeval(buffer + 4, descsz); | |
1380 | elfcpp::Swap<32, true>::writeval(buffer + 8, note_type); | |
1381 | } | |
1382 | } | |
1383 | else if (size == 64) | |
1384 | { | |
1385 | if (!is_big_endian) | |
1386 | { | |
1387 | elfcpp::Swap<64, false>::writeval(buffer, namesz); | |
1388 | elfcpp::Swap<64, false>::writeval(buffer + 8, descsz); | |
1389 | elfcpp::Swap<64, false>::writeval(buffer + 16, note_type); | |
1390 | } | |
1391 | else | |
1392 | { | |
1393 | elfcpp::Swap<64, true>::writeval(buffer, namesz); | |
1394 | elfcpp::Swap<64, true>::writeval(buffer + 8, descsz); | |
1395 | elfcpp::Swap<64, true>::writeval(buffer + 16, note_type); | |
1396 | } | |
1397 | } | |
1398 | else | |
1399 | gold_unreachable(); | |
1400 | ||
1401 | memcpy(buffer + 3 * (size / 8), name, namesz); | |
4f211c8b | 1402 | |
cfd73a4e | 1403 | const char* note_name = this->namepool_.add(".note", false, NULL); |
8ed814a9 ILT |
1404 | elfcpp::Elf_Xword flags = 0; |
1405 | if (allocate) | |
1406 | flags = elfcpp::SHF_ALLOC; | |
4f211c8b ILT |
1407 | Output_section* os = this->make_output_section(note_name, |
1408 | elfcpp::SHT_NOTE, | |
8ed814a9 ILT |
1409 | flags); |
1410 | Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz, | |
7d9e3d98 ILT |
1411 | size / 8, |
1412 | "** note header"); | |
8ed814a9 ILT |
1413 | os->add_output_section_data(posd); |
1414 | ||
1415 | *trailing_padding = aligned_descsz - descsz; | |
1416 | ||
1417 | return os; | |
1418 | } | |
1419 | ||
1420 | // For an executable or shared library, create a note to record the | |
1421 | // version of gold used to create the binary. | |
1422 | ||
1423 | void | |
1424 | Layout::create_gold_note() | |
1425 | { | |
1426 | if (parameters->options().relocatable()) | |
1427 | return; | |
1428 | ||
1429 | std::string desc = std::string("gold ") + gold::get_version_string(); | |
1430 | ||
1431 | size_t trailing_padding; | |
1432 | Output_section *os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION, | |
1433 | desc.size(), false, &trailing_padding); | |
1434 | ||
1435 | Output_section_data* posd = new Output_data_const(desc, 4); | |
4f211c8b | 1436 | os->add_output_section_data(posd); |
8ed814a9 ILT |
1437 | |
1438 | if (trailing_padding > 0) | |
1439 | { | |
7d9e3d98 | 1440 | posd = new Output_data_zero_fill(trailing_padding, 0); |
8ed814a9 ILT |
1441 | os->add_output_section_data(posd); |
1442 | } | |
4f211c8b ILT |
1443 | } |
1444 | ||
35cdfc9a ILT |
1445 | // Record whether the stack should be executable. This can be set |
1446 | // from the command line using the -z execstack or -z noexecstack | |
1447 | // options. Otherwise, if any input file has a .note.GNU-stack | |
1448 | // section with the SHF_EXECINSTR flag set, the stack should be | |
1449 | // executable. Otherwise, if at least one input file a | |
1450 | // .note.GNU-stack section, and some input file has no .note.GNU-stack | |
1451 | // section, we use the target default for whether the stack should be | |
1452 | // executable. Otherwise, we don't generate a stack note. When | |
1453 | // generating a object file, we create a .note.GNU-stack section with | |
1454 | // the appropriate marking. When generating an executable or shared | |
1455 | // library, we create a PT_GNU_STACK segment. | |
1456 | ||
1457 | void | |
1458 | Layout::create_executable_stack_info(const Target* target) | |
1459 | { | |
1460 | bool is_stack_executable; | |
1461 | if (this->options_.is_execstack_set()) | |
1462 | is_stack_executable = this->options_.is_stack_executable(); | |
1463 | else if (!this->input_with_gnu_stack_note_) | |
1464 | return; | |
1465 | else | |
1466 | { | |
1467 | if (this->input_requires_executable_stack_) | |
1468 | is_stack_executable = true; | |
1469 | else if (this->input_without_gnu_stack_note_) | |
1470 | is_stack_executable = target->is_default_stack_executable(); | |
1471 | else | |
1472 | is_stack_executable = false; | |
1473 | } | |
1474 | ||
8851ecca | 1475 | if (parameters->options().relocatable()) |
35cdfc9a ILT |
1476 | { |
1477 | const char* name = this->namepool_.add(".note.GNU-stack", false, NULL); | |
1478 | elfcpp::Elf_Xword flags = 0; | |
1479 | if (is_stack_executable) | |
1480 | flags |= elfcpp::SHF_EXECINSTR; | |
1481 | this->make_output_section(name, elfcpp::SHT_PROGBITS, flags); | |
1482 | } | |
1483 | else | |
1484 | { | |
1c4f3631 ILT |
1485 | if (this->script_options_->saw_phdrs_clause()) |
1486 | return; | |
35cdfc9a ILT |
1487 | int flags = elfcpp::PF_R | elfcpp::PF_W; |
1488 | if (is_stack_executable) | |
1489 | flags |= elfcpp::PF_X; | |
3802b2dd | 1490 | this->make_output_segment(elfcpp::PT_GNU_STACK, flags); |
35cdfc9a ILT |
1491 | } |
1492 | } | |
1493 | ||
8ed814a9 ILT |
1494 | // If --build-id was used, set up the build ID note. |
1495 | ||
1496 | void | |
1497 | Layout::create_build_id() | |
1498 | { | |
1499 | if (!parameters->options().user_set_build_id()) | |
1500 | return; | |
1501 | ||
1502 | const char* style = parameters->options().build_id(); | |
1503 | if (strcmp(style, "none") == 0) | |
1504 | return; | |
1505 | ||
1506 | // Set DESCSZ to the size of the note descriptor. When possible, | |
1507 | // set DESC to the note descriptor contents. | |
1508 | size_t descsz; | |
1509 | std::string desc; | |
1510 | if (strcmp(style, "md5") == 0) | |
1511 | descsz = 128 / 8; | |
1512 | else if (strcmp(style, "sha1") == 0) | |
1513 | descsz = 160 / 8; | |
1514 | else if (strcmp(style, "uuid") == 0) | |
1515 | { | |
1516 | const size_t uuidsz = 128 / 8; | |
1517 | ||
1518 | char buffer[uuidsz]; | |
1519 | memset(buffer, 0, uuidsz); | |
1520 | ||
2a00e4fb | 1521 | int descriptor = open_descriptor(-1, "/dev/urandom", O_RDONLY); |
8ed814a9 ILT |
1522 | if (descriptor < 0) |
1523 | gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"), | |
1524 | strerror(errno)); | |
1525 | else | |
1526 | { | |
1527 | ssize_t got = ::read(descriptor, buffer, uuidsz); | |
2a00e4fb | 1528 | release_descriptor(descriptor, true); |
8ed814a9 ILT |
1529 | if (got < 0) |
1530 | gold_error(_("/dev/urandom: read failed: %s"), strerror(errno)); | |
1531 | else if (static_cast<size_t>(got) != uuidsz) | |
1532 | gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"), | |
1533 | uuidsz, got); | |
1534 | } | |
1535 | ||
1536 | desc.assign(buffer, uuidsz); | |
1537 | descsz = uuidsz; | |
1538 | } | |
1539 | else if (strncmp(style, "0x", 2) == 0) | |
1540 | { | |
1541 | hex_init(); | |
1542 | const char* p = style + 2; | |
1543 | while (*p != '\0') | |
1544 | { | |
1545 | if (hex_p(p[0]) && hex_p(p[1])) | |
1546 | { | |
1547 | char c = (hex_value(p[0]) << 4) | hex_value(p[1]); | |
1548 | desc += c; | |
1549 | p += 2; | |
1550 | } | |
1551 | else if (*p == '-' || *p == ':') | |
1552 | ++p; | |
1553 | else | |
1554 | gold_fatal(_("--build-id argument '%s' not a valid hex number"), | |
1555 | style); | |
1556 | } | |
1557 | descsz = desc.size(); | |
1558 | } | |
1559 | else | |
1560 | gold_fatal(_("unrecognized --build-id argument '%s'"), style); | |
1561 | ||
1562 | // Create the note. | |
1563 | size_t trailing_padding; | |
1564 | Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID, | |
1565 | descsz, true, &trailing_padding); | |
1566 | ||
1567 | if (!desc.empty()) | |
1568 | { | |
1569 | // We know the value already, so we fill it in now. | |
1570 | gold_assert(desc.size() == descsz); | |
1571 | ||
1572 | Output_section_data* posd = new Output_data_const(desc, 4); | |
1573 | os->add_output_section_data(posd); | |
1574 | ||
1575 | if (trailing_padding != 0) | |
1576 | { | |
7d9e3d98 | 1577 | posd = new Output_data_zero_fill(trailing_padding, 0); |
8ed814a9 ILT |
1578 | os->add_output_section_data(posd); |
1579 | } | |
1580 | } | |
1581 | else | |
1582 | { | |
1583 | // We need to compute a checksum after we have completed the | |
1584 | // link. | |
1585 | gold_assert(trailing_padding == 0); | |
7d9e3d98 | 1586 | this->build_id_note_ = new Output_data_zero_fill(descsz, 4); |
8ed814a9 ILT |
1587 | os->add_output_section_data(this->build_id_note_); |
1588 | os->set_after_input_sections(); | |
1589 | } | |
1590 | } | |
1591 | ||
75f65a3e ILT |
1592 | // Return whether SEG1 should be before SEG2 in the output file. This |
1593 | // is based entirely on the segment type and flags. When this is | |
1594 | // called the segment addresses has normally not yet been set. | |
1595 | ||
1596 | bool | |
1597 | Layout::segment_precedes(const Output_segment* seg1, | |
1598 | const Output_segment* seg2) | |
1599 | { | |
1600 | elfcpp::Elf_Word type1 = seg1->type(); | |
1601 | elfcpp::Elf_Word type2 = seg2->type(); | |
1602 | ||
1603 | // The single PT_PHDR segment is required to precede any loadable | |
1604 | // segment. We simply make it always first. | |
1605 | if (type1 == elfcpp::PT_PHDR) | |
1606 | { | |
a3ad94ed | 1607 | gold_assert(type2 != elfcpp::PT_PHDR); |
75f65a3e ILT |
1608 | return true; |
1609 | } | |
1610 | if (type2 == elfcpp::PT_PHDR) | |
1611 | return false; | |
1612 | ||
1613 | // The single PT_INTERP segment is required to precede any loadable | |
1614 | // segment. We simply make it always second. | |
1615 | if (type1 == elfcpp::PT_INTERP) | |
1616 | { | |
a3ad94ed | 1617 | gold_assert(type2 != elfcpp::PT_INTERP); |
75f65a3e ILT |
1618 | return true; |
1619 | } | |
1620 | if (type2 == elfcpp::PT_INTERP) | |
1621 | return false; | |
1622 | ||
1623 | // We then put PT_LOAD segments before any other segments. | |
1624 | if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD) | |
1625 | return true; | |
1626 | if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD) | |
1627 | return false; | |
1628 | ||
9f1d377b ILT |
1629 | // We put the PT_TLS segment last except for the PT_GNU_RELRO |
1630 | // segment, because that is where the dynamic linker expects to find | |
1631 | // it (this is just for efficiency; other positions would also work | |
1632 | // correctly). | |
1633 | if (type1 == elfcpp::PT_TLS | |
1634 | && type2 != elfcpp::PT_TLS | |
1635 | && type2 != elfcpp::PT_GNU_RELRO) | |
1636 | return false; | |
1637 | if (type2 == elfcpp::PT_TLS | |
1638 | && type1 != elfcpp::PT_TLS | |
1639 | && type1 != elfcpp::PT_GNU_RELRO) | |
1640 | return true; | |
1641 | ||
1642 | // We put the PT_GNU_RELRO segment last, because that is where the | |
1643 | // dynamic linker expects to find it (as with PT_TLS, this is just | |
1644 | // for efficiency). | |
1645 | if (type1 == elfcpp::PT_GNU_RELRO && type2 != elfcpp::PT_GNU_RELRO) | |
92e059d8 | 1646 | return false; |
9f1d377b | 1647 | if (type2 == elfcpp::PT_GNU_RELRO && type1 != elfcpp::PT_GNU_RELRO) |
92e059d8 ILT |
1648 | return true; |
1649 | ||
75f65a3e ILT |
1650 | const elfcpp::Elf_Word flags1 = seg1->flags(); |
1651 | const elfcpp::Elf_Word flags2 = seg2->flags(); | |
1652 | ||
1653 | // The order of non-PT_LOAD segments is unimportant. We simply sort | |
1654 | // by the numeric segment type and flags values. There should not | |
1655 | // be more than one segment with the same type and flags. | |
1656 | if (type1 != elfcpp::PT_LOAD) | |
1657 | { | |
1658 | if (type1 != type2) | |
1659 | return type1 < type2; | |
a3ad94ed | 1660 | gold_assert(flags1 != flags2); |
75f65a3e ILT |
1661 | return flags1 < flags2; |
1662 | } | |
1663 | ||
a445fddf ILT |
1664 | // If the addresses are set already, sort by load address. |
1665 | if (seg1->are_addresses_set()) | |
1666 | { | |
1667 | if (!seg2->are_addresses_set()) | |
1668 | return true; | |
1669 | ||
1670 | unsigned int section_count1 = seg1->output_section_count(); | |
1671 | unsigned int section_count2 = seg2->output_section_count(); | |
1672 | if (section_count1 == 0 && section_count2 > 0) | |
1673 | return true; | |
1674 | if (section_count1 > 0 && section_count2 == 0) | |
1675 | return false; | |
1676 | ||
1677 | uint64_t paddr1 = seg1->first_section_load_address(); | |
1678 | uint64_t paddr2 = seg2->first_section_load_address(); | |
1679 | if (paddr1 != paddr2) | |
1680 | return paddr1 < paddr2; | |
1681 | } | |
1682 | else if (seg2->are_addresses_set()) | |
1683 | return false; | |
1684 | ||
75f65a3e | 1685 | // We sort PT_LOAD segments based on the flags. Readonly segments |
756ac4a8 ILT |
1686 | // come before writable segments. Then writable segments with data |
1687 | // come before writable segments without data. Then executable | |
1688 | // segments come before non-executable segments. Then the unlikely | |
1689 | // case of a non-readable segment comes before the normal case of a | |
1690 | // readable segment. If there are multiple segments with the same | |
1691 | // type and flags, we require that the address be set, and we sort | |
1692 | // by virtual address and then physical address. | |
75f65a3e ILT |
1693 | if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W)) |
1694 | return (flags1 & elfcpp::PF_W) == 0; | |
756ac4a8 ILT |
1695 | if ((flags1 & elfcpp::PF_W) != 0 |
1696 | && seg1->has_any_data_sections() != seg2->has_any_data_sections()) | |
1697 | return seg1->has_any_data_sections(); | |
75f65a3e ILT |
1698 | if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X)) |
1699 | return (flags1 & elfcpp::PF_X) != 0; | |
1700 | if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R)) | |
1701 | return (flags1 & elfcpp::PF_R) == 0; | |
1702 | ||
a445fddf ILT |
1703 | // We shouldn't get here--we shouldn't create segments which we |
1704 | // can't distinguish. | |
1705 | gold_unreachable(); | |
75f65a3e ILT |
1706 | } |
1707 | ||
ead1e424 ILT |
1708 | // Set the file offsets of all the segments, and all the sections they |
1709 | // contain. They have all been created. LOAD_SEG must be be laid out | |
1710 | // first. Return the offset of the data to follow. | |
75f65a3e ILT |
1711 | |
1712 | off_t | |
ead1e424 ILT |
1713 | Layout::set_segment_offsets(const Target* target, Output_segment* load_seg, |
1714 | unsigned int *pshndx) | |
75f65a3e ILT |
1715 | { |
1716 | // Sort them into the final order. | |
54dc6425 ILT |
1717 | std::sort(this->segment_list_.begin(), this->segment_list_.end(), |
1718 | Layout::Compare_segments()); | |
1719 | ||
75f65a3e ILT |
1720 | // Find the PT_LOAD segments, and set their addresses and offsets |
1721 | // and their section's addresses and offsets. | |
0c5e9c22 | 1722 | uint64_t addr; |
45aa233b ILT |
1723 | if (this->options_.user_set_Ttext()) |
1724 | addr = this->options_.Ttext(); | |
8851ecca | 1725 | else if (parameters->options().shared()) |
a445fddf | 1726 | addr = 0; |
0c5e9c22 ILT |
1727 | else |
1728 | addr = target->default_text_segment_address(); | |
75f65a3e | 1729 | off_t off = 0; |
a445fddf ILT |
1730 | |
1731 | // If LOAD_SEG is NULL, then the file header and segment headers | |
1732 | // will not be loadable. But they still need to be at offset 0 in | |
1733 | // the file. Set their offsets now. | |
1734 | if (load_seg == NULL) | |
1735 | { | |
1736 | for (Data_list::iterator p = this->special_output_list_.begin(); | |
1737 | p != this->special_output_list_.end(); | |
1738 | ++p) | |
1739 | { | |
1740 | off = align_address(off, (*p)->addralign()); | |
1741 | (*p)->set_address_and_file_offset(0, off); | |
1742 | off += (*p)->data_size(); | |
1743 | } | |
1744 | } | |
1745 | ||
34810851 ILT |
1746 | const bool check_sections = parameters->options().check_sections(); |
1747 | Output_segment* last_load_segment = NULL; | |
1748 | ||
75f65a3e ILT |
1749 | bool was_readonly = false; |
1750 | for (Segment_list::iterator p = this->segment_list_.begin(); | |
1751 | p != this->segment_list_.end(); | |
1752 | ++p) | |
1753 | { | |
1754 | if ((*p)->type() == elfcpp::PT_LOAD) | |
1755 | { | |
1756 | if (load_seg != NULL && load_seg != *p) | |
a3ad94ed | 1757 | gold_unreachable(); |
75f65a3e ILT |
1758 | load_seg = NULL; |
1759 | ||
756ac4a8 ILT |
1760 | bool are_addresses_set = (*p)->are_addresses_set(); |
1761 | if (are_addresses_set) | |
1762 | { | |
1763 | // When it comes to setting file offsets, we care about | |
1764 | // the physical address. | |
1765 | addr = (*p)->paddr(); | |
1766 | } | |
45aa233b | 1767 | else if (this->options_.user_set_Tdata() |
756ac4a8 | 1768 | && ((*p)->flags() & elfcpp::PF_W) != 0 |
45aa233b | 1769 | && (!this->options_.user_set_Tbss() |
756ac4a8 ILT |
1770 | || (*p)->has_any_data_sections())) |
1771 | { | |
45aa233b | 1772 | addr = this->options_.Tdata(); |
756ac4a8 ILT |
1773 | are_addresses_set = true; |
1774 | } | |
45aa233b | 1775 | else if (this->options_.user_set_Tbss() |
756ac4a8 ILT |
1776 | && ((*p)->flags() & elfcpp::PF_W) != 0 |
1777 | && !(*p)->has_any_data_sections()) | |
1778 | { | |
45aa233b | 1779 | addr = this->options_.Tbss(); |
756ac4a8 ILT |
1780 | are_addresses_set = true; |
1781 | } | |
1782 | ||
75f65a3e ILT |
1783 | uint64_t orig_addr = addr; |
1784 | uint64_t orig_off = off; | |
1785 | ||
a445fddf | 1786 | uint64_t aligned_addr = 0; |
75f65a3e | 1787 | uint64_t abi_pagesize = target->abi_pagesize(); |
af6156ef | 1788 | uint64_t common_pagesize = target->common_pagesize(); |
0496d5e5 | 1789 | |
af6156ef ILT |
1790 | if (!parameters->options().nmagic() |
1791 | && !parameters->options().omagic()) | |
1792 | (*p)->set_minimum_p_align(common_pagesize); | |
0496d5e5 | 1793 | |
a445fddf | 1794 | if (are_addresses_set) |
75f65a3e | 1795 | { |
af6156ef ILT |
1796 | if (!parameters->options().nmagic() |
1797 | && !parameters->options().omagic()) | |
1798 | { | |
1799 | // Adjust the file offset to the same address modulo | |
1800 | // the page size. | |
1801 | uint64_t unsigned_off = off; | |
1802 | uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1)) | |
1803 | | (addr & (abi_pagesize - 1))); | |
1804 | if (aligned_off < unsigned_off) | |
1805 | aligned_off += abi_pagesize; | |
1806 | off = aligned_off; | |
1807 | } | |
a445fddf ILT |
1808 | } |
1809 | else | |
1810 | { | |
1811 | // If the last segment was readonly, and this one is | |
1812 | // not, then skip the address forward one page, | |
1813 | // maintaining the same position within the page. This | |
1814 | // lets us store both segments overlapping on a single | |
1815 | // page in the file, but the loader will put them on | |
1816 | // different pages in memory. | |
1817 | ||
1818 | addr = align_address(addr, (*p)->maximum_alignment()); | |
75f65a3e | 1819 | aligned_addr = addr; |
a445fddf ILT |
1820 | |
1821 | if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0) | |
1822 | { | |
1823 | if ((addr & (abi_pagesize - 1)) != 0) | |
1824 | addr = addr + abi_pagesize; | |
1825 | } | |
1826 | ||
1827 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); | |
75f65a3e ILT |
1828 | } |
1829 | ||
ead1e424 | 1830 | unsigned int shndx_hold = *pshndx; |
96a2b4e4 ILT |
1831 | uint64_t new_addr = (*p)->set_section_addresses(this, false, addr, |
1832 | &off, pshndx); | |
75f65a3e ILT |
1833 | |
1834 | // Now that we know the size of this segment, we may be able | |
1835 | // to save a page in memory, at the cost of wasting some | |
1836 | // file space, by instead aligning to the start of a new | |
1837 | // page. Here we use the real machine page size rather than | |
1838 | // the ABI mandated page size. | |
1839 | ||
a445fddf | 1840 | if (!are_addresses_set && aligned_addr != addr) |
75f65a3e | 1841 | { |
75f65a3e ILT |
1842 | uint64_t first_off = (common_pagesize |
1843 | - (aligned_addr | |
1844 | & (common_pagesize - 1))); | |
1845 | uint64_t last_off = new_addr & (common_pagesize - 1); | |
1846 | if (first_off > 0 | |
1847 | && last_off > 0 | |
1848 | && ((aligned_addr & ~ (common_pagesize - 1)) | |
1849 | != (new_addr & ~ (common_pagesize - 1))) | |
1850 | && first_off + last_off <= common_pagesize) | |
1851 | { | |
ead1e424 ILT |
1852 | *pshndx = shndx_hold; |
1853 | addr = align_address(aligned_addr, common_pagesize); | |
a445fddf | 1854 | addr = align_address(addr, (*p)->maximum_alignment()); |
75f65a3e | 1855 | off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1)); |
96a2b4e4 ILT |
1856 | new_addr = (*p)->set_section_addresses(this, true, addr, |
1857 | &off, pshndx); | |
75f65a3e ILT |
1858 | } |
1859 | } | |
1860 | ||
1861 | addr = new_addr; | |
1862 | ||
1863 | if (((*p)->flags() & elfcpp::PF_W) == 0) | |
1864 | was_readonly = true; | |
34810851 ILT |
1865 | |
1866 | // Implement --check-sections. We know that the segments | |
1867 | // are sorted by LMA. | |
1868 | if (check_sections && last_load_segment != NULL) | |
1869 | { | |
1870 | gold_assert(last_load_segment->paddr() <= (*p)->paddr()); | |
1871 | if (last_load_segment->paddr() + last_load_segment->memsz() | |
1872 | > (*p)->paddr()) | |
1873 | { | |
1874 | unsigned long long lb1 = last_load_segment->paddr(); | |
1875 | unsigned long long le1 = lb1 + last_load_segment->memsz(); | |
1876 | unsigned long long lb2 = (*p)->paddr(); | |
1877 | unsigned long long le2 = lb2 + (*p)->memsz(); | |
1878 | gold_error(_("load segment overlap [0x%llx -> 0x%llx] and " | |
1879 | "[0x%llx -> 0x%llx]"), | |
1880 | lb1, le1, lb2, le2); | |
1881 | } | |
1882 | } | |
1883 | last_load_segment = *p; | |
75f65a3e ILT |
1884 | } |
1885 | } | |
1886 | ||
1887 | // Handle the non-PT_LOAD segments, setting their offsets from their | |
1888 | // section's offsets. | |
1889 | for (Segment_list::iterator p = this->segment_list_.begin(); | |
1890 | p != this->segment_list_.end(); | |
1891 | ++p) | |
1892 | { | |
1893 | if ((*p)->type() != elfcpp::PT_LOAD) | |
1894 | (*p)->set_offset(); | |
1895 | } | |
1896 | ||
7bf1f802 ILT |
1897 | // Set the TLS offsets for each section in the PT_TLS segment. |
1898 | if (this->tls_segment_ != NULL) | |
1899 | this->tls_segment_->set_tls_offsets(); | |
1900 | ||
75f65a3e ILT |
1901 | return off; |
1902 | } | |
1903 | ||
6a74a719 ILT |
1904 | // Set the offsets of all the allocated sections when doing a |
1905 | // relocatable link. This does the same jobs as set_segment_offsets, | |
1906 | // only for a relocatable link. | |
1907 | ||
1908 | off_t | |
1909 | Layout::set_relocatable_section_offsets(Output_data* file_header, | |
1910 | unsigned int *pshndx) | |
1911 | { | |
1912 | off_t off = 0; | |
1913 | ||
1914 | file_header->set_address_and_file_offset(0, 0); | |
1915 | off += file_header->data_size(); | |
1916 | ||
1917 | for (Section_list::iterator p = this->section_list_.begin(); | |
1918 | p != this->section_list_.end(); | |
1919 | ++p) | |
1920 | { | |
1921 | // We skip unallocated sections here, except that group sections | |
1922 | // have to come first. | |
1923 | if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0 | |
1924 | && (*p)->type() != elfcpp::SHT_GROUP) | |
1925 | continue; | |
1926 | ||
1927 | off = align_address(off, (*p)->addralign()); | |
1928 | ||
1929 | // The linker script might have set the address. | |
1930 | if (!(*p)->is_address_valid()) | |
1931 | (*p)->set_address(0); | |
1932 | (*p)->set_file_offset(off); | |
1933 | (*p)->finalize_data_size(); | |
1934 | off += (*p)->data_size(); | |
1935 | ||
1936 | (*p)->set_out_shndx(*pshndx); | |
1937 | ++*pshndx; | |
1938 | } | |
1939 | ||
1940 | return off; | |
1941 | } | |
1942 | ||
75f65a3e ILT |
1943 | // Set the file offset of all the sections not associated with a |
1944 | // segment. | |
1945 | ||
1946 | off_t | |
9a0910c3 | 1947 | Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass) |
75f65a3e | 1948 | { |
a3ad94ed ILT |
1949 | for (Section_list::iterator p = this->unattached_section_list_.begin(); |
1950 | p != this->unattached_section_list_.end(); | |
75f65a3e ILT |
1951 | ++p) |
1952 | { | |
27bc2bce ILT |
1953 | // The symtab section is handled in create_symtab_sections. |
1954 | if (*p == this->symtab_section_) | |
61ba1cf9 | 1955 | continue; |
27bc2bce | 1956 | |
a9a60db6 ILT |
1957 | // If we've already set the data size, don't set it again. |
1958 | if ((*p)->is_offset_valid() && (*p)->is_data_size_valid()) | |
1959 | continue; | |
1960 | ||
96803768 ILT |
1961 | if (pass == BEFORE_INPUT_SECTIONS_PASS |
1962 | && (*p)->requires_postprocessing()) | |
17a1d0a9 ILT |
1963 | { |
1964 | (*p)->create_postprocessing_buffer(); | |
1965 | this->any_postprocessing_sections_ = true; | |
1966 | } | |
96803768 | 1967 | |
9a0910c3 ILT |
1968 | if (pass == BEFORE_INPUT_SECTIONS_PASS |
1969 | && (*p)->after_input_sections()) | |
1970 | continue; | |
17a1d0a9 | 1971 | else if (pass == POSTPROCESSING_SECTIONS_PASS |
9a0910c3 ILT |
1972 | && (!(*p)->after_input_sections() |
1973 | || (*p)->type() == elfcpp::SHT_STRTAB)) | |
1974 | continue; | |
17a1d0a9 | 1975 | else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS |
9a0910c3 ILT |
1976 | && (!(*p)->after_input_sections() |
1977 | || (*p)->type() != elfcpp::SHT_STRTAB)) | |
1978 | continue; | |
27bc2bce | 1979 | |
ead1e424 | 1980 | off = align_address(off, (*p)->addralign()); |
27bc2bce ILT |
1981 | (*p)->set_file_offset(off); |
1982 | (*p)->finalize_data_size(); | |
75f65a3e | 1983 | off += (*p)->data_size(); |
96803768 ILT |
1984 | |
1985 | // At this point the name must be set. | |
17a1d0a9 | 1986 | if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS) |
96803768 | 1987 | this->namepool_.add((*p)->name(), false, NULL); |
75f65a3e ILT |
1988 | } |
1989 | return off; | |
1990 | } | |
1991 | ||
86887060 ILT |
1992 | // Set the section indexes of all the sections not associated with a |
1993 | // segment. | |
1994 | ||
1995 | unsigned int | |
1996 | Layout::set_section_indexes(unsigned int shndx) | |
1997 | { | |
1998 | for (Section_list::iterator p = this->unattached_section_list_.begin(); | |
1999 | p != this->unattached_section_list_.end(); | |
2000 | ++p) | |
2001 | { | |
d491d34e ILT |
2002 | if (!(*p)->has_out_shndx()) |
2003 | { | |
2004 | (*p)->set_out_shndx(shndx); | |
2005 | ++shndx; | |
2006 | } | |
86887060 ILT |
2007 | } |
2008 | return shndx; | |
2009 | } | |
2010 | ||
a445fddf ILT |
2011 | // Set the section addresses according to the linker script. This is |
2012 | // only called when we see a SECTIONS clause. This returns the | |
2013 | // program segment which should hold the file header and segment | |
2014 | // headers, if any. It will return NULL if they should not be in a | |
2015 | // segment. | |
2016 | ||
2017 | Output_segment* | |
2018 | Layout::set_section_addresses_from_script(Symbol_table* symtab) | |
2019 | { | |
2020 | Script_sections* ss = this->script_options_->script_sections(); | |
2021 | gold_assert(ss->saw_sections_clause()); | |
2022 | ||
2023 | // Place each orphaned output section in the script. | |
2024 | for (Section_list::iterator p = this->section_list_.begin(); | |
2025 | p != this->section_list_.end(); | |
2026 | ++p) | |
2027 | { | |
2028 | if (!(*p)->found_in_sections_clause()) | |
2029 | ss->place_orphan(*p); | |
2030 | } | |
2031 | ||
2032 | return this->script_options_->set_section_addresses(symtab, this); | |
2033 | } | |
2034 | ||
7bf1f802 ILT |
2035 | // Count the local symbols in the regular symbol table and the dynamic |
2036 | // symbol table, and build the respective string pools. | |
2037 | ||
2038 | void | |
17a1d0a9 ILT |
2039 | Layout::count_local_symbols(const Task* task, |
2040 | const Input_objects* input_objects) | |
7bf1f802 | 2041 | { |
6d013333 ILT |
2042 | // First, figure out an upper bound on the number of symbols we'll |
2043 | // be inserting into each pool. This helps us create the pools with | |
2044 | // the right size, to avoid unnecessary hashtable resizing. | |
2045 | unsigned int symbol_count = 0; | |
2046 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
2047 | p != input_objects->relobj_end(); | |
2048 | ++p) | |
2049 | symbol_count += (*p)->local_symbol_count(); | |
2050 | ||
2051 | // Go from "upper bound" to "estimate." We overcount for two | |
2052 | // reasons: we double-count symbols that occur in more than one | |
2053 | // object file, and we count symbols that are dropped from the | |
2054 | // output. Add it all together and assume we overcount by 100%. | |
2055 | symbol_count /= 2; | |
2056 | ||
2057 | // We assume all symbols will go into both the sympool and dynpool. | |
2058 | this->sympool_.reserve(symbol_count); | |
2059 | this->dynpool_.reserve(symbol_count); | |
2060 | ||
7bf1f802 ILT |
2061 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
2062 | p != input_objects->relobj_end(); | |
2063 | ++p) | |
2064 | { | |
17a1d0a9 | 2065 | Task_lock_obj<Object> tlo(task, *p); |
7bf1f802 ILT |
2066 | (*p)->count_local_symbols(&this->sympool_, &this->dynpool_); |
2067 | } | |
2068 | } | |
2069 | ||
b8e6aad9 ILT |
2070 | // Create the symbol table sections. Here we also set the final |
2071 | // values of the symbols. At this point all the loadable sections are | |
d491d34e | 2072 | // fully laid out. SHNUM is the number of sections so far. |
75f65a3e ILT |
2073 | |
2074 | void | |
9025d29d | 2075 | Layout::create_symtab_sections(const Input_objects* input_objects, |
75f65a3e | 2076 | Symbol_table* symtab, |
d491d34e | 2077 | unsigned int shnum, |
16649710 | 2078 | off_t* poff) |
75f65a3e | 2079 | { |
61ba1cf9 ILT |
2080 | int symsize; |
2081 | unsigned int align; | |
8851ecca | 2082 | if (parameters->target().get_size() == 32) |
61ba1cf9 ILT |
2083 | { |
2084 | symsize = elfcpp::Elf_sizes<32>::sym_size; | |
2085 | align = 4; | |
2086 | } | |
8851ecca | 2087 | else if (parameters->target().get_size() == 64) |
61ba1cf9 ILT |
2088 | { |
2089 | symsize = elfcpp::Elf_sizes<64>::sym_size; | |
2090 | align = 8; | |
2091 | } | |
2092 | else | |
a3ad94ed | 2093 | gold_unreachable(); |
61ba1cf9 ILT |
2094 | |
2095 | off_t off = *poff; | |
ead1e424 | 2096 | off = align_address(off, align); |
61ba1cf9 ILT |
2097 | off_t startoff = off; |
2098 | ||
2099 | // Save space for the dummy symbol at the start of the section. We | |
2100 | // never bother to write this out--it will just be left as zero. | |
2101 | off += symsize; | |
c06b7b0b | 2102 | unsigned int local_symbol_index = 1; |
61ba1cf9 | 2103 | |
a3ad94ed ILT |
2104 | // Add STT_SECTION symbols for each Output section which needs one. |
2105 | for (Section_list::iterator p = this->section_list_.begin(); | |
2106 | p != this->section_list_.end(); | |
2107 | ++p) | |
2108 | { | |
2109 | if (!(*p)->needs_symtab_index()) | |
2110 | (*p)->set_symtab_index(-1U); | |
2111 | else | |
2112 | { | |
2113 | (*p)->set_symtab_index(local_symbol_index); | |
2114 | ++local_symbol_index; | |
2115 | off += symsize; | |
2116 | } | |
2117 | } | |
2118 | ||
f6ce93d6 ILT |
2119 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
2120 | p != input_objects->relobj_end(); | |
75f65a3e ILT |
2121 | ++p) |
2122 | { | |
c06b7b0b | 2123 | unsigned int index = (*p)->finalize_local_symbols(local_symbol_index, |
7bf1f802 | 2124 | off); |
c06b7b0b ILT |
2125 | off += (index - local_symbol_index) * symsize; |
2126 | local_symbol_index = index; | |
75f65a3e ILT |
2127 | } |
2128 | ||
c06b7b0b | 2129 | unsigned int local_symcount = local_symbol_index; |
a3ad94ed | 2130 | gold_assert(local_symcount * symsize == off - startoff); |
61ba1cf9 | 2131 | |
16649710 ILT |
2132 | off_t dynoff; |
2133 | size_t dyn_global_index; | |
2134 | size_t dyncount; | |
2135 | if (this->dynsym_section_ == NULL) | |
2136 | { | |
2137 | dynoff = 0; | |
2138 | dyn_global_index = 0; | |
2139 | dyncount = 0; | |
2140 | } | |
2141 | else | |
2142 | { | |
2143 | dyn_global_index = this->dynsym_section_->info(); | |
2144 | off_t locsize = dyn_global_index * this->dynsym_section_->entsize(); | |
2145 | dynoff = this->dynsym_section_->offset() + locsize; | |
2146 | dyncount = (this->dynsym_section_->data_size() - locsize) / symsize; | |
f5c3f225 | 2147 | gold_assert(static_cast<off_t>(dyncount * symsize) |
16649710 ILT |
2148 | == this->dynsym_section_->data_size() - locsize); |
2149 | } | |
2150 | ||
55a93433 ILT |
2151 | off = symtab->finalize(off, dynoff, dyn_global_index, dyncount, |
2152 | &this->sympool_, &local_symcount); | |
75f65a3e | 2153 | |
8851ecca | 2154 | if (!parameters->options().strip_all()) |
9e2dcb77 ILT |
2155 | { |
2156 | this->sympool_.set_string_offsets(); | |
61ba1cf9 | 2157 | |
cfd73a4e | 2158 | const char* symtab_name = this->namepool_.add(".symtab", false, NULL); |
9e2dcb77 ILT |
2159 | Output_section* osymtab = this->make_output_section(symtab_name, |
2160 | elfcpp::SHT_SYMTAB, | |
2161 | 0); | |
2162 | this->symtab_section_ = osymtab; | |
a3ad94ed | 2163 | |
27bc2bce | 2164 | Output_section_data* pos = new Output_data_fixed_space(off - startoff, |
7d9e3d98 ILT |
2165 | align, |
2166 | "** symtab"); | |
9e2dcb77 | 2167 | osymtab->add_output_section_data(pos); |
61ba1cf9 | 2168 | |
d491d34e ILT |
2169 | // We generate a .symtab_shndx section if we have more than |
2170 | // SHN_LORESERVE sections. Technically it is possible that we | |
2171 | // don't need one, because it is possible that there are no | |
2172 | // symbols in any of sections with indexes larger than | |
2173 | // SHN_LORESERVE. That is probably unusual, though, and it is | |
2174 | // easier to always create one than to compute section indexes | |
2175 | // twice (once here, once when writing out the symbols). | |
2176 | if (shnum >= elfcpp::SHN_LORESERVE) | |
2177 | { | |
2178 | const char* symtab_xindex_name = this->namepool_.add(".symtab_shndx", | |
2179 | false, NULL); | |
2180 | Output_section* osymtab_xindex = | |
2181 | this->make_output_section(symtab_xindex_name, | |
2182 | elfcpp::SHT_SYMTAB_SHNDX, 0); | |
2183 | ||
2184 | size_t symcount = (off - startoff) / symsize; | |
2185 | this->symtab_xindex_ = new Output_symtab_xindex(symcount); | |
2186 | ||
2187 | osymtab_xindex->add_output_section_data(this->symtab_xindex_); | |
2188 | ||
2189 | osymtab_xindex->set_link_section(osymtab); | |
2190 | osymtab_xindex->set_addralign(4); | |
2191 | osymtab_xindex->set_entsize(4); | |
2192 | ||
2193 | osymtab_xindex->set_after_input_sections(); | |
2194 | ||
2195 | // This tells the driver code to wait until the symbol table | |
2196 | // has written out before writing out the postprocessing | |
2197 | // sections, including the .symtab_shndx section. | |
2198 | this->any_postprocessing_sections_ = true; | |
2199 | } | |
2200 | ||
cfd73a4e | 2201 | const char* strtab_name = this->namepool_.add(".strtab", false, NULL); |
9e2dcb77 ILT |
2202 | Output_section* ostrtab = this->make_output_section(strtab_name, |
2203 | elfcpp::SHT_STRTAB, | |
2204 | 0); | |
a3ad94ed | 2205 | |
9e2dcb77 ILT |
2206 | Output_section_data* pstr = new Output_data_strtab(&this->sympool_); |
2207 | ostrtab->add_output_section_data(pstr); | |
61ba1cf9 | 2208 | |
27bc2bce ILT |
2209 | osymtab->set_file_offset(startoff); |
2210 | osymtab->finalize_data_size(); | |
9e2dcb77 ILT |
2211 | osymtab->set_link_section(ostrtab); |
2212 | osymtab->set_info(local_symcount); | |
2213 | osymtab->set_entsize(symsize); | |
61ba1cf9 | 2214 | |
9e2dcb77 ILT |
2215 | *poff = off; |
2216 | } | |
75f65a3e ILT |
2217 | } |
2218 | ||
2219 | // Create the .shstrtab section, which holds the names of the | |
2220 | // sections. At the time this is called, we have created all the | |
2221 | // output sections except .shstrtab itself. | |
2222 | ||
2223 | Output_section* | |
2224 | Layout::create_shstrtab() | |
2225 | { | |
2226 | // FIXME: We don't need to create a .shstrtab section if we are | |
2227 | // stripping everything. | |
2228 | ||
cfd73a4e | 2229 | const char* name = this->namepool_.add(".shstrtab", false, NULL); |
75f65a3e | 2230 | |
a3ad94ed | 2231 | Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0); |
75f65a3e | 2232 | |
27bc2bce ILT |
2233 | // We can't write out this section until we've set all the section |
2234 | // names, and we don't set the names of compressed output sections | |
2235 | // until relocations are complete. | |
2236 | os->set_after_input_sections(); | |
2237 | ||
a3ad94ed ILT |
2238 | Output_section_data* posd = new Output_data_strtab(&this->namepool_); |
2239 | os->add_output_section_data(posd); | |
75f65a3e ILT |
2240 | |
2241 | return os; | |
2242 | } | |
2243 | ||
2244 | // Create the section headers. SIZE is 32 or 64. OFF is the file | |
2245 | // offset. | |
2246 | ||
27bc2bce | 2247 | void |
d491d34e | 2248 | Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff) |
75f65a3e ILT |
2249 | { |
2250 | Output_section_headers* oshdrs; | |
9025d29d | 2251 | oshdrs = new Output_section_headers(this, |
16649710 | 2252 | &this->segment_list_, |
6a74a719 | 2253 | &this->section_list_, |
16649710 | 2254 | &this->unattached_section_list_, |
d491d34e ILT |
2255 | &this->namepool_, |
2256 | shstrtab_section); | |
ead1e424 | 2257 | off_t off = align_address(*poff, oshdrs->addralign()); |
27bc2bce | 2258 | oshdrs->set_address_and_file_offset(0, off); |
61ba1cf9 ILT |
2259 | off += oshdrs->data_size(); |
2260 | *poff = off; | |
27bc2bce | 2261 | this->section_headers_ = oshdrs; |
54dc6425 ILT |
2262 | } |
2263 | ||
d491d34e ILT |
2264 | // Count the allocated sections. |
2265 | ||
2266 | size_t | |
2267 | Layout::allocated_output_section_count() const | |
2268 | { | |
2269 | size_t section_count = 0; | |
2270 | for (Segment_list::const_iterator p = this->segment_list_.begin(); | |
2271 | p != this->segment_list_.end(); | |
2272 | ++p) | |
2273 | section_count += (*p)->output_section_count(); | |
2274 | return section_count; | |
2275 | } | |
2276 | ||
dbe717ef ILT |
2277 | // Create the dynamic symbol table. |
2278 | ||
2279 | void | |
7bf1f802 | 2280 | Layout::create_dynamic_symtab(const Input_objects* input_objects, |
9b07f471 | 2281 | Symbol_table* symtab, |
14b31740 ILT |
2282 | Output_section **pdynstr, |
2283 | unsigned int* plocal_dynamic_count, | |
2284 | std::vector<Symbol*>* pdynamic_symbols, | |
2285 | Versions* pversions) | |
dbe717ef | 2286 | { |
a3ad94ed ILT |
2287 | // Count all the symbols in the dynamic symbol table, and set the |
2288 | // dynamic symbol indexes. | |
dbe717ef | 2289 | |
a3ad94ed ILT |
2290 | // Skip symbol 0, which is always all zeroes. |
2291 | unsigned int index = 1; | |
dbe717ef | 2292 | |
a3ad94ed ILT |
2293 | // Add STT_SECTION symbols for each Output section which needs one. |
2294 | for (Section_list::iterator p = this->section_list_.begin(); | |
2295 | p != this->section_list_.end(); | |
2296 | ++p) | |
2297 | { | |
2298 | if (!(*p)->needs_dynsym_index()) | |
2299 | (*p)->set_dynsym_index(-1U); | |
2300 | else | |
2301 | { | |
2302 | (*p)->set_dynsym_index(index); | |
2303 | ++index; | |
2304 | } | |
2305 | } | |
2306 | ||
7bf1f802 ILT |
2307 | // Count the local symbols that need to go in the dynamic symbol table, |
2308 | // and set the dynamic symbol indexes. | |
2309 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
2310 | p != input_objects->relobj_end(); | |
2311 | ++p) | |
2312 | { | |
2313 | unsigned int new_index = (*p)->set_local_dynsym_indexes(index); | |
2314 | index = new_index; | |
2315 | } | |
a3ad94ed ILT |
2316 | |
2317 | unsigned int local_symcount = index; | |
14b31740 | 2318 | *plocal_dynamic_count = local_symcount; |
a3ad94ed | 2319 | |
9b07f471 | 2320 | index = symtab->set_dynsym_indexes(index, pdynamic_symbols, |
35cdfc9a | 2321 | &this->dynpool_, pversions); |
a3ad94ed ILT |
2322 | |
2323 | int symsize; | |
2324 | unsigned int align; | |
8851ecca | 2325 | const int size = parameters->target().get_size(); |
a3ad94ed ILT |
2326 | if (size == 32) |
2327 | { | |
2328 | symsize = elfcpp::Elf_sizes<32>::sym_size; | |
2329 | align = 4; | |
2330 | } | |
2331 | else if (size == 64) | |
2332 | { | |
2333 | symsize = elfcpp::Elf_sizes<64>::sym_size; | |
2334 | align = 8; | |
2335 | } | |
2336 | else | |
2337 | gold_unreachable(); | |
2338 | ||
14b31740 ILT |
2339 | // Create the dynamic symbol table section. |
2340 | ||
3802b2dd ILT |
2341 | Output_section* dynsym = this->choose_output_section(NULL, ".dynsym", |
2342 | elfcpp::SHT_DYNSYM, | |
2343 | elfcpp::SHF_ALLOC, | |
2344 | false); | |
a3ad94ed | 2345 | |
27bc2bce | 2346 | Output_section_data* odata = new Output_data_fixed_space(index * symsize, |
7d9e3d98 ILT |
2347 | align, |
2348 | "** dynsym"); | |
a3ad94ed ILT |
2349 | dynsym->add_output_section_data(odata); |
2350 | ||
2351 | dynsym->set_info(local_symcount); | |
2352 | dynsym->set_entsize(symsize); | |
2353 | dynsym->set_addralign(align); | |
2354 | ||
2355 | this->dynsym_section_ = dynsym; | |
2356 | ||
16649710 | 2357 | Output_data_dynamic* const odyn = this->dynamic_data_; |
a3ad94ed ILT |
2358 | odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym); |
2359 | odyn->add_constant(elfcpp::DT_SYMENT, symsize); | |
2360 | ||
d491d34e ILT |
2361 | // If there are more than SHN_LORESERVE allocated sections, we |
2362 | // create a .dynsym_shndx section. It is possible that we don't | |
2363 | // need one, because it is possible that there are no dynamic | |
2364 | // symbols in any of the sections with indexes larger than | |
2365 | // SHN_LORESERVE. This is probably unusual, though, and at this | |
2366 | // time we don't know the actual section indexes so it is | |
2367 | // inconvenient to check. | |
2368 | if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE) | |
2369 | { | |
2370 | Output_section* dynsym_xindex = | |
2371 | this->choose_output_section(NULL, ".dynsym_shndx", | |
2372 | elfcpp::SHT_SYMTAB_SHNDX, | |
2373 | elfcpp::SHF_ALLOC, | |
2374 | false); | |
2375 | ||
2376 | this->dynsym_xindex_ = new Output_symtab_xindex(index); | |
2377 | ||
2378 | dynsym_xindex->add_output_section_data(this->dynsym_xindex_); | |
2379 | ||
2380 | dynsym_xindex->set_link_section(dynsym); | |
2381 | dynsym_xindex->set_addralign(4); | |
2382 | dynsym_xindex->set_entsize(4); | |
2383 | ||
2384 | dynsym_xindex->set_after_input_sections(); | |
2385 | ||
2386 | // This tells the driver code to wait until the symbol table has | |
2387 | // written out before writing out the postprocessing sections, | |
2388 | // including the .dynsym_shndx section. | |
2389 | this->any_postprocessing_sections_ = true; | |
2390 | } | |
2391 | ||
14b31740 ILT |
2392 | // Create the dynamic string table section. |
2393 | ||
3802b2dd ILT |
2394 | Output_section* dynstr = this->choose_output_section(NULL, ".dynstr", |
2395 | elfcpp::SHT_STRTAB, | |
2396 | elfcpp::SHF_ALLOC, | |
2397 | false); | |
a3ad94ed ILT |
2398 | |
2399 | Output_section_data* strdata = new Output_data_strtab(&this->dynpool_); | |
2400 | dynstr->add_output_section_data(strdata); | |
2401 | ||
16649710 ILT |
2402 | dynsym->set_link_section(dynstr); |
2403 | this->dynamic_section_->set_link_section(dynstr); | |
2404 | ||
a3ad94ed ILT |
2405 | odyn->add_section_address(elfcpp::DT_STRTAB, dynstr); |
2406 | odyn->add_section_size(elfcpp::DT_STRSZ, dynstr); | |
2407 | ||
14b31740 ILT |
2408 | *pdynstr = dynstr; |
2409 | ||
2410 | // Create the hash tables. | |
2411 | ||
13670ee6 ILT |
2412 | if (strcmp(parameters->options().hash_style(), "sysv") == 0 |
2413 | || strcmp(parameters->options().hash_style(), "both") == 0) | |
2414 | { | |
2415 | unsigned char* phash; | |
2416 | unsigned int hashlen; | |
2417 | Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount, | |
2418 | &phash, &hashlen); | |
2419 | ||
2420 | Output_section* hashsec = this->choose_output_section(NULL, ".hash", | |
2421 | elfcpp::SHT_HASH, | |
2422 | elfcpp::SHF_ALLOC, | |
2423 | false); | |
2424 | ||
2425 | Output_section_data* hashdata = new Output_data_const_buffer(phash, | |
2426 | hashlen, | |
7d9e3d98 ILT |
2427 | align, |
2428 | "** hash"); | |
13670ee6 ILT |
2429 | hashsec->add_output_section_data(hashdata); |
2430 | ||
2431 | hashsec->set_link_section(dynsym); | |
2432 | hashsec->set_entsize(4); | |
a3ad94ed | 2433 | |
13670ee6 ILT |
2434 | odyn->add_section_address(elfcpp::DT_HASH, hashsec); |
2435 | } | |
2436 | ||
2437 | if (strcmp(parameters->options().hash_style(), "gnu") == 0 | |
2438 | || strcmp(parameters->options().hash_style(), "both") == 0) | |
2439 | { | |
2440 | unsigned char* phash; | |
2441 | unsigned int hashlen; | |
2442 | Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount, | |
2443 | &phash, &hashlen); | |
a3ad94ed | 2444 | |
13670ee6 ILT |
2445 | Output_section* hashsec = this->choose_output_section(NULL, ".gnu.hash", |
2446 | elfcpp::SHT_GNU_HASH, | |
2447 | elfcpp::SHF_ALLOC, | |
2448 | false); | |
a3ad94ed | 2449 | |
13670ee6 ILT |
2450 | Output_section_data* hashdata = new Output_data_const_buffer(phash, |
2451 | hashlen, | |
7d9e3d98 ILT |
2452 | align, |
2453 | "** hash"); | |
13670ee6 | 2454 | hashsec->add_output_section_data(hashdata); |
a3ad94ed | 2455 | |
13670ee6 ILT |
2456 | hashsec->set_link_section(dynsym); |
2457 | hashsec->set_entsize(4); | |
a3ad94ed | 2458 | |
13670ee6 ILT |
2459 | odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec); |
2460 | } | |
dbe717ef ILT |
2461 | } |
2462 | ||
7bf1f802 ILT |
2463 | // Assign offsets to each local portion of the dynamic symbol table. |
2464 | ||
2465 | void | |
2466 | Layout::assign_local_dynsym_offsets(const Input_objects* input_objects) | |
2467 | { | |
2468 | Output_section* dynsym = this->dynsym_section_; | |
2469 | gold_assert(dynsym != NULL); | |
2470 | ||
2471 | off_t off = dynsym->offset(); | |
2472 | ||
2473 | // Skip the dummy symbol at the start of the section. | |
2474 | off += dynsym->entsize(); | |
2475 | ||
2476 | for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); | |
2477 | p != input_objects->relobj_end(); | |
2478 | ++p) | |
2479 | { | |
2480 | unsigned int count = (*p)->set_local_dynsym_offset(off); | |
2481 | off += count * dynsym->entsize(); | |
2482 | } | |
2483 | } | |
2484 | ||
14b31740 ILT |
2485 | // Create the version sections. |
2486 | ||
2487 | void | |
9025d29d | 2488 | Layout::create_version_sections(const Versions* versions, |
46fe1623 | 2489 | const Symbol_table* symtab, |
14b31740 ILT |
2490 | unsigned int local_symcount, |
2491 | const std::vector<Symbol*>& dynamic_symbols, | |
2492 | const Output_section* dynstr) | |
2493 | { | |
2494 | if (!versions->any_defs() && !versions->any_needs()) | |
2495 | return; | |
2496 | ||
8851ecca | 2497 | switch (parameters->size_and_endianness()) |
14b31740 | 2498 | { |
193a53d9 | 2499 | #ifdef HAVE_TARGET_32_LITTLE |
8851ecca | 2500 | case Parameters::TARGET_32_LITTLE: |
7d1a9ebb ILT |
2501 | this->sized_create_version_sections<32, false>(versions, symtab, |
2502 | local_symcount, | |
2503 | dynamic_symbols, dynstr); | |
8851ecca | 2504 | break; |
193a53d9 | 2505 | #endif |
8851ecca ILT |
2506 | #ifdef HAVE_TARGET_32_BIG |
2507 | case Parameters::TARGET_32_BIG: | |
7d1a9ebb ILT |
2508 | this->sized_create_version_sections<32, true>(versions, symtab, |
2509 | local_symcount, | |
2510 | dynamic_symbols, dynstr); | |
8851ecca | 2511 | break; |
193a53d9 | 2512 | #endif |
193a53d9 | 2513 | #ifdef HAVE_TARGET_64_LITTLE |
8851ecca | 2514 | case Parameters::TARGET_64_LITTLE: |
7d1a9ebb ILT |
2515 | this->sized_create_version_sections<64, false>(versions, symtab, |
2516 | local_symcount, | |
2517 | dynamic_symbols, dynstr); | |
8851ecca | 2518 | break; |
193a53d9 | 2519 | #endif |
8851ecca ILT |
2520 | #ifdef HAVE_TARGET_64_BIG |
2521 | case Parameters::TARGET_64_BIG: | |
7d1a9ebb ILT |
2522 | this->sized_create_version_sections<64, true>(versions, symtab, |
2523 | local_symcount, | |
2524 | dynamic_symbols, dynstr); | |
8851ecca ILT |
2525 | break; |
2526 | #endif | |
2527 | default: | |
2528 | gold_unreachable(); | |
14b31740 | 2529 | } |
14b31740 ILT |
2530 | } |
2531 | ||
2532 | // Create the version sections, sized version. | |
2533 | ||
2534 | template<int size, bool big_endian> | |
2535 | void | |
2536 | Layout::sized_create_version_sections( | |
2537 | const Versions* versions, | |
46fe1623 | 2538 | const Symbol_table* symtab, |
14b31740 ILT |
2539 | unsigned int local_symcount, |
2540 | const std::vector<Symbol*>& dynamic_symbols, | |
7d1a9ebb | 2541 | const Output_section* dynstr) |
14b31740 | 2542 | { |
3802b2dd ILT |
2543 | Output_section* vsec = this->choose_output_section(NULL, ".gnu.version", |
2544 | elfcpp::SHT_GNU_versym, | |
2545 | elfcpp::SHF_ALLOC, | |
2546 | false); | |
14b31740 ILT |
2547 | |
2548 | unsigned char* vbuf; | |
2549 | unsigned int vsize; | |
7d1a9ebb ILT |
2550 | versions->symbol_section_contents<size, big_endian>(symtab, &this->dynpool_, |
2551 | local_symcount, | |
2552 | dynamic_symbols, | |
2553 | &vbuf, &vsize); | |
14b31740 | 2554 | |
7d9e3d98 ILT |
2555 | Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2, |
2556 | "** versions"); | |
14b31740 ILT |
2557 | |
2558 | vsec->add_output_section_data(vdata); | |
2559 | vsec->set_entsize(2); | |
2560 | vsec->set_link_section(this->dynsym_section_); | |
2561 | ||
2562 | Output_data_dynamic* const odyn = this->dynamic_data_; | |
2563 | odyn->add_section_address(elfcpp::DT_VERSYM, vsec); | |
2564 | ||
2565 | if (versions->any_defs()) | |
2566 | { | |
3802b2dd ILT |
2567 | Output_section* vdsec; |
2568 | vdsec= this->choose_output_section(NULL, ".gnu.version_d", | |
2569 | elfcpp::SHT_GNU_verdef, | |
2570 | elfcpp::SHF_ALLOC, | |
2571 | false); | |
14b31740 ILT |
2572 | |
2573 | unsigned char* vdbuf; | |
2574 | unsigned int vdsize; | |
2575 | unsigned int vdentries; | |
7d1a9ebb ILT |
2576 | versions->def_section_contents<size, big_endian>(&this->dynpool_, &vdbuf, |
2577 | &vdsize, &vdentries); | |
14b31740 | 2578 | |
7d9e3d98 ILT |
2579 | Output_section_data* vddata = |
2580 | new Output_data_const_buffer(vdbuf, vdsize, 4, "** version defs"); | |
14b31740 ILT |
2581 | |
2582 | vdsec->add_output_section_data(vddata); | |
2583 | vdsec->set_link_section(dynstr); | |
2584 | vdsec->set_info(vdentries); | |
2585 | ||
2586 | odyn->add_section_address(elfcpp::DT_VERDEF, vdsec); | |
2587 | odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries); | |
2588 | } | |
2589 | ||
2590 | if (versions->any_needs()) | |
2591 | { | |
14b31740 | 2592 | Output_section* vnsec; |
3802b2dd ILT |
2593 | vnsec = this->choose_output_section(NULL, ".gnu.version_r", |
2594 | elfcpp::SHT_GNU_verneed, | |
2595 | elfcpp::SHF_ALLOC, | |
2596 | false); | |
14b31740 ILT |
2597 | |
2598 | unsigned char* vnbuf; | |
2599 | unsigned int vnsize; | |
2600 | unsigned int vnentries; | |
7d1a9ebb ILT |
2601 | versions->need_section_contents<size, big_endian>(&this->dynpool_, |
2602 | &vnbuf, &vnsize, | |
2603 | &vnentries); | |
14b31740 | 2604 | |
7d9e3d98 ILT |
2605 | Output_section_data* vndata = |
2606 | new Output_data_const_buffer(vnbuf, vnsize, 4, "** version refs"); | |
14b31740 ILT |
2607 | |
2608 | vnsec->add_output_section_data(vndata); | |
2609 | vnsec->set_link_section(dynstr); | |
2610 | vnsec->set_info(vnentries); | |
2611 | ||
2612 | odyn->add_section_address(elfcpp::DT_VERNEED, vnsec); | |
2613 | odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries); | |
2614 | } | |
2615 | } | |
2616 | ||
dbe717ef ILT |
2617 | // Create the .interp section and PT_INTERP segment. |
2618 | ||
2619 | void | |
2620 | Layout::create_interp(const Target* target) | |
2621 | { | |
2622 | const char* interp = this->options_.dynamic_linker(); | |
2623 | if (interp == NULL) | |
2624 | { | |
2625 | interp = target->dynamic_linker(); | |
a3ad94ed | 2626 | gold_assert(interp != NULL); |
dbe717ef ILT |
2627 | } |
2628 | ||
2629 | size_t len = strlen(interp) + 1; | |
2630 | ||
2631 | Output_section_data* odata = new Output_data_const(interp, len, 1); | |
2632 | ||
3802b2dd ILT |
2633 | Output_section* osec = this->choose_output_section(NULL, ".interp", |
2634 | elfcpp::SHT_PROGBITS, | |
2635 | elfcpp::SHF_ALLOC, | |
2636 | false); | |
dbe717ef ILT |
2637 | osec->add_output_section_data(odata); |
2638 | ||
1c4f3631 ILT |
2639 | if (!this->script_options_->saw_phdrs_clause()) |
2640 | { | |
2641 | Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP, | |
2642 | elfcpp::PF_R); | |
01676dcd | 2643 | oseg->add_output_section(osec, elfcpp::PF_R); |
1c4f3631 | 2644 | } |
dbe717ef ILT |
2645 | } |
2646 | ||
a3ad94ed ILT |
2647 | // Finish the .dynamic section and PT_DYNAMIC segment. |
2648 | ||
2649 | void | |
2650 | Layout::finish_dynamic_section(const Input_objects* input_objects, | |
16649710 | 2651 | const Symbol_table* symtab) |
a3ad94ed | 2652 | { |
1c4f3631 ILT |
2653 | if (!this->script_options_->saw_phdrs_clause()) |
2654 | { | |
2655 | Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC, | |
2656 | (elfcpp::PF_R | |
2657 | | elfcpp::PF_W)); | |
01676dcd ILT |
2658 | oseg->add_output_section(this->dynamic_section_, |
2659 | elfcpp::PF_R | elfcpp::PF_W); | |
1c4f3631 | 2660 | } |
a3ad94ed | 2661 | |
16649710 ILT |
2662 | Output_data_dynamic* const odyn = this->dynamic_data_; |
2663 | ||
a3ad94ed ILT |
2664 | for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin(); |
2665 | p != input_objects->dynobj_end(); | |
2666 | ++p) | |
2667 | { | |
2668 | // FIXME: Handle --as-needed. | |
2669 | odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname()); | |
2670 | } | |
2671 | ||
8851ecca | 2672 | if (parameters->options().shared()) |
fced7afd ILT |
2673 | { |
2674 | const char* soname = this->options_.soname(); | |
2675 | if (soname != NULL) | |
2676 | odyn->add_string(elfcpp::DT_SONAME, soname); | |
2677 | } | |
2678 | ||
a3ad94ed ILT |
2679 | // FIXME: Support --init and --fini. |
2680 | Symbol* sym = symtab->lookup("_init"); | |
14b31740 | 2681 | if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj()) |
a3ad94ed ILT |
2682 | odyn->add_symbol(elfcpp::DT_INIT, sym); |
2683 | ||
2684 | sym = symtab->lookup("_fini"); | |
14b31740 | 2685 | if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj()) |
a3ad94ed ILT |
2686 | odyn->add_symbol(elfcpp::DT_FINI, sym); |
2687 | ||
2688 | // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY. | |
41f542e7 ILT |
2689 | |
2690 | // Add a DT_RPATH entry if needed. | |
2691 | const General_options::Dir_list& rpath(this->options_.rpath()); | |
2692 | if (!rpath.empty()) | |
2693 | { | |
2694 | std::string rpath_val; | |
2695 | for (General_options::Dir_list::const_iterator p = rpath.begin(); | |
2696 | p != rpath.end(); | |
2697 | ++p) | |
2698 | { | |
2699 | if (rpath_val.empty()) | |
ad2d6943 | 2700 | rpath_val = p->name(); |
41f542e7 ILT |
2701 | else |
2702 | { | |
2703 | // Eliminate duplicates. | |
2704 | General_options::Dir_list::const_iterator q; | |
2705 | for (q = rpath.begin(); q != p; ++q) | |
ad2d6943 | 2706 | if (q->name() == p->name()) |
41f542e7 ILT |
2707 | break; |
2708 | if (q == p) | |
2709 | { | |
2710 | rpath_val += ':'; | |
ad2d6943 | 2711 | rpath_val += p->name(); |
41f542e7 ILT |
2712 | } |
2713 | } | |
2714 | } | |
2715 | ||
2716 | odyn->add_string(elfcpp::DT_RPATH, rpath_val); | |
7c414435 DM |
2717 | if (parameters->options().enable_new_dtags()) |
2718 | odyn->add_string(elfcpp::DT_RUNPATH, rpath_val); | |
41f542e7 | 2719 | } |
4f4c5f80 ILT |
2720 | |
2721 | // Look for text segments that have dynamic relocations. | |
2722 | bool have_textrel = false; | |
4e8fe71f | 2723 | if (!this->script_options_->saw_sections_clause()) |
4f4c5f80 | 2724 | { |
4e8fe71f ILT |
2725 | for (Segment_list::const_iterator p = this->segment_list_.begin(); |
2726 | p != this->segment_list_.end(); | |
2727 | ++p) | |
2728 | { | |
2729 | if (((*p)->flags() & elfcpp::PF_W) == 0 | |
2730 | && (*p)->dynamic_reloc_count() > 0) | |
2731 | { | |
2732 | have_textrel = true; | |
2733 | break; | |
2734 | } | |
2735 | } | |
2736 | } | |
2737 | else | |
2738 | { | |
2739 | // We don't know the section -> segment mapping, so we are | |
2740 | // conservative and just look for readonly sections with | |
2741 | // relocations. If those sections wind up in writable segments, | |
2742 | // then we have created an unnecessary DT_TEXTREL entry. | |
2743 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
2744 | p != this->section_list_.end(); | |
2745 | ++p) | |
2746 | { | |
2747 | if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0 | |
2748 | && ((*p)->flags() & elfcpp::SHF_WRITE) == 0 | |
2749 | && ((*p)->dynamic_reloc_count() > 0)) | |
2750 | { | |
2751 | have_textrel = true; | |
2752 | break; | |
2753 | } | |
2754 | } | |
4f4c5f80 ILT |
2755 | } |
2756 | ||
2757 | // Add a DT_FLAGS entry. We add it even if no flags are set so that | |
2758 | // post-link tools can easily modify these flags if desired. | |
2759 | unsigned int flags = 0; | |
2760 | if (have_textrel) | |
6a41d30b ILT |
2761 | { |
2762 | // Add a DT_TEXTREL for compatibility with older loaders. | |
2763 | odyn->add_constant(elfcpp::DT_TEXTREL, 0); | |
2764 | flags |= elfcpp::DF_TEXTREL; | |
2765 | } | |
8851ecca | 2766 | if (parameters->options().shared() && this->has_static_tls()) |
535890bb | 2767 | flags |= elfcpp::DF_STATIC_TLS; |
7be8330a CD |
2768 | if (parameters->options().origin()) |
2769 | flags |= elfcpp::DF_ORIGIN; | |
4f4c5f80 | 2770 | odyn->add_constant(elfcpp::DT_FLAGS, flags); |
7c414435 DM |
2771 | |
2772 | flags = 0; | |
2773 | if (parameters->options().initfirst()) | |
2774 | flags |= elfcpp::DF_1_INITFIRST; | |
2775 | if (parameters->options().interpose()) | |
2776 | flags |= elfcpp::DF_1_INTERPOSE; | |
2777 | if (parameters->options().loadfltr()) | |
2778 | flags |= elfcpp::DF_1_LOADFLTR; | |
2779 | if (parameters->options().nodefaultlib()) | |
2780 | flags |= elfcpp::DF_1_NODEFLIB; | |
2781 | if (parameters->options().nodelete()) | |
2782 | flags |= elfcpp::DF_1_NODELETE; | |
2783 | if (parameters->options().nodlopen()) | |
2784 | flags |= elfcpp::DF_1_NOOPEN; | |
2785 | if (parameters->options().nodump()) | |
2786 | flags |= elfcpp::DF_1_NODUMP; | |
2787 | if (!parameters->options().shared()) | |
2788 | flags &= ~(elfcpp::DF_1_INITFIRST | |
2789 | | elfcpp::DF_1_NODELETE | |
2790 | | elfcpp::DF_1_NOOPEN); | |
7be8330a CD |
2791 | if (parameters->options().origin()) |
2792 | flags |= elfcpp::DF_1_ORIGIN; | |
7c414435 DM |
2793 | if (flags) |
2794 | odyn->add_constant(elfcpp::DT_FLAGS_1, flags); | |
a3ad94ed ILT |
2795 | } |
2796 | ||
a2fb1b05 ILT |
2797 | // The mapping of .gnu.linkonce section names to real section names. |
2798 | ||
ead1e424 | 2799 | #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 } |
a2fb1b05 ILT |
2800 | const Layout::Linkonce_mapping Layout::linkonce_mapping[] = |
2801 | { | |
9f1d377b ILT |
2802 | MAPPING_INIT("d.rel.ro.local", ".data.rel.ro.local"), // Before "d.rel.ro". |
2803 | MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Before "d". | |
a2fb1b05 ILT |
2804 | MAPPING_INIT("t", ".text"), |
2805 | MAPPING_INIT("r", ".rodata"), | |
2806 | MAPPING_INIT("d", ".data"), | |
2807 | MAPPING_INIT("b", ".bss"), | |
2808 | MAPPING_INIT("s", ".sdata"), | |
2809 | MAPPING_INIT("sb", ".sbss"), | |
2810 | MAPPING_INIT("s2", ".sdata2"), | |
2811 | MAPPING_INIT("sb2", ".sbss2"), | |
2812 | MAPPING_INIT("wi", ".debug_info"), | |
2813 | MAPPING_INIT("td", ".tdata"), | |
2814 | MAPPING_INIT("tb", ".tbss"), | |
2815 | MAPPING_INIT("lr", ".lrodata"), | |
2816 | MAPPING_INIT("l", ".ldata"), | |
2817 | MAPPING_INIT("lb", ".lbss"), | |
2818 | }; | |
2819 | #undef MAPPING_INIT | |
2820 | ||
2821 | const int Layout::linkonce_mapping_count = | |
2822 | sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]); | |
2823 | ||
2824 | // Return the name of the output section to use for a .gnu.linkonce | |
2825 | // section. This is based on the default ELF linker script of the old | |
2826 | // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo" | |
ead1e424 ILT |
2827 | // to ".text". Set *PLEN to the length of the name. *PLEN is |
2828 | // initialized to the length of NAME. | |
a2fb1b05 ILT |
2829 | |
2830 | const char* | |
ead1e424 | 2831 | Layout::linkonce_output_name(const char* name, size_t *plen) |
a2fb1b05 ILT |
2832 | { |
2833 | const char* s = name + sizeof(".gnu.linkonce") - 1; | |
2834 | if (*s != '.') | |
2835 | return name; | |
2836 | ++s; | |
2837 | const Linkonce_mapping* plm = linkonce_mapping; | |
2838 | for (int i = 0; i < linkonce_mapping_count; ++i, ++plm) | |
2839 | { | |
2840 | if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.') | |
ead1e424 ILT |
2841 | { |
2842 | *plen = plm->tolen; | |
2843 | return plm->to; | |
2844 | } | |
a2fb1b05 ILT |
2845 | } |
2846 | return name; | |
2847 | } | |
2848 | ||
ead1e424 ILT |
2849 | // Choose the output section name to use given an input section name. |
2850 | // Set *PLEN to the length of the name. *PLEN is initialized to the | |
2851 | // length of NAME. | |
2852 | ||
2853 | const char* | |
2854 | Layout::output_section_name(const char* name, size_t* plen) | |
2855 | { | |
2856 | if (Layout::is_linkonce(name)) | |
2857 | { | |
2858 | // .gnu.linkonce sections are laid out as though they were named | |
2859 | // for the sections are placed into. | |
2860 | return Layout::linkonce_output_name(name, plen); | |
2861 | } | |
2862 | ||
af4a8a83 ILT |
2863 | // gcc 4.3 generates the following sorts of section names when it |
2864 | // needs a section name specific to a function: | |
2865 | // .text.FN | |
2866 | // .rodata.FN | |
2867 | // .sdata2.FN | |
2868 | // .data.FN | |
2869 | // .data.rel.FN | |
2870 | // .data.rel.local.FN | |
2871 | // .data.rel.ro.FN | |
2872 | // .data.rel.ro.local.FN | |
2873 | // .sdata.FN | |
2874 | // .bss.FN | |
2875 | // .sbss.FN | |
2876 | // .tdata.FN | |
2877 | // .tbss.FN | |
2878 | ||
2879 | // The GNU linker maps all of those to the part before the .FN, | |
2880 | // except that .data.rel.local.FN is mapped to .data, and | |
2881 | // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections | |
2882 | // beginning with .data.rel.ro.local are grouped together. | |
2883 | ||
2884 | // For an anonymous namespace, the string FN can contain a '.'. | |
2885 | ||
2886 | // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the | |
2887 | // GNU linker maps to .rodata. | |
2888 | ||
2889 | // The .data.rel.ro sections enable a security feature triggered by | |
2890 | // the -z relro option. Section which need to be relocated at | |
2891 | // program startup time but which may be readonly after startup are | |
2892 | // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO | |
2893 | // segment. The dynamic linker will make that segment writable, | |
2894 | // perform relocations, and then make it read-only. FIXME: We do | |
2895 | // not yet implement this optimization. | |
2896 | ||
2897 | // It is hard to handle this in a principled way. | |
2898 | ||
2899 | // These are the rules we follow: | |
2900 | ||
2901 | // If the section name has no initial '.', or no dot other than an | |
2902 | // initial '.', we use the name unchanged (i.e., "mysection" and | |
2903 | // ".text" are unchanged). | |
2904 | ||
9f1d377b ILT |
2905 | // If the name starts with ".data.rel.ro.local" we use |
2906 | // ".data.rel.ro.local". | |
2907 | ||
af4a8a83 ILT |
2908 | // If the name starts with ".data.rel.ro" we use ".data.rel.ro". |
2909 | ||
2910 | // Otherwise, we drop the second '.' and everything that comes after | |
2911 | // it (i.e., ".text.XXX" becomes ".text"). | |
ead1e424 ILT |
2912 | |
2913 | const char* s = name; | |
af4a8a83 ILT |
2914 | if (*s != '.') |
2915 | return name; | |
2916 | ++s; | |
ead1e424 ILT |
2917 | const char* sdot = strchr(s, '.'); |
2918 | if (sdot == NULL) | |
2919 | return name; | |
2920 | ||
9f1d377b ILT |
2921 | const char* const data_rel_ro_local = ".data.rel.ro.local"; |
2922 | if (strncmp(name, data_rel_ro_local, strlen(data_rel_ro_local)) == 0) | |
2923 | { | |
2924 | *plen = strlen(data_rel_ro_local); | |
2925 | return data_rel_ro_local; | |
2926 | } | |
2927 | ||
af4a8a83 ILT |
2928 | const char* const data_rel_ro = ".data.rel.ro"; |
2929 | if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0) | |
ead1e424 | 2930 | { |
af4a8a83 ILT |
2931 | *plen = strlen(data_rel_ro); |
2932 | return data_rel_ro; | |
ead1e424 ILT |
2933 | } |
2934 | ||
ead1e424 ILT |
2935 | *plen = sdot - name; |
2936 | return name; | |
2937 | } | |
2938 | ||
a2fb1b05 ILT |
2939 | // Record the signature of a comdat section, and return whether to |
2940 | // include it in the link. If GROUP is true, this is a regular | |
2941 | // section group. If GROUP is false, this is a group signature | |
2942 | // derived from the name of a linkonce section. We want linkonce | |
2943 | // signatures and group signatures to block each other, but we don't | |
2944 | // want a linkonce signature to block another linkonce signature. | |
2945 | ||
2946 | bool | |
e94cf127 CC |
2947 | Layout::add_comdat(Relobj* object, unsigned int shndx, |
2948 | const std::string& signature, bool group) | |
a2fb1b05 | 2949 | { |
e94cf127 | 2950 | Kept_section kept(object, shndx, group); |
a2fb1b05 | 2951 | std::pair<Signatures::iterator, bool> ins( |
e94cf127 | 2952 | this->signatures_.insert(std::make_pair(signature, kept))); |
a2fb1b05 ILT |
2953 | |
2954 | if (ins.second) | |
2955 | { | |
2956 | // This is the first time we've seen this signature. | |
2957 | return true; | |
2958 | } | |
2959 | ||
e94cf127 | 2960 | if (ins.first->second.group_) |
a2fb1b05 ILT |
2961 | { |
2962 | // We've already seen a real section group with this signature. | |
2756a258 CC |
2963 | // If the kept group is from a plugin object, and we're in |
2964 | // the replacement phase, accept the new one as a replacement. | |
2965 | if (ins.first->second.object_ == NULL | |
2966 | && parameters->options().plugins()->in_replacement_phase()) | |
2967 | { | |
2968 | ins.first->second = kept; | |
2969 | return true; | |
2970 | } | |
a2fb1b05 ILT |
2971 | return false; |
2972 | } | |
2973 | else if (group) | |
2974 | { | |
2975 | // This is a real section group, and we've already seen a | |
a0fa0c07 | 2976 | // linkonce section with this signature. Record that we've seen |
a2fb1b05 | 2977 | // a section group, and don't include this section group. |
e94cf127 | 2978 | ins.first->second.group_ = true; |
a2fb1b05 ILT |
2979 | return false; |
2980 | } | |
2981 | else | |
2982 | { | |
2983 | // We've already seen a linkonce section and this is a linkonce | |
2984 | // section. These don't block each other--this may be the same | |
2985 | // symbol name with different section types. | |
2986 | return true; | |
2987 | } | |
2988 | } | |
2989 | ||
e94cf127 CC |
2990 | // Find the given comdat signature, and return the object and section |
2991 | // index of the kept group. | |
2992 | Relobj* | |
2993 | Layout::find_kept_object(const std::string& signature, | |
2994 | unsigned int* pshndx) const | |
2995 | { | |
2996 | Signatures::const_iterator p = this->signatures_.find(signature); | |
2997 | if (p == this->signatures_.end()) | |
2998 | return NULL; | |
2999 | if (pshndx != NULL) | |
3000 | *pshndx = p->second.shndx_; | |
3001 | return p->second.object_; | |
3002 | } | |
3003 | ||
a445fddf ILT |
3004 | // Store the allocated sections into the section list. |
3005 | ||
3006 | void | |
3007 | Layout::get_allocated_sections(Section_list* section_list) const | |
3008 | { | |
3009 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
3010 | p != this->section_list_.end(); | |
3011 | ++p) | |
3012 | if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0) | |
3013 | section_list->push_back(*p); | |
3014 | } | |
3015 | ||
3016 | // Create an output segment. | |
3017 | ||
3018 | Output_segment* | |
3019 | Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags) | |
3020 | { | |
8851ecca | 3021 | gold_assert(!parameters->options().relocatable()); |
a445fddf ILT |
3022 | Output_segment* oseg = new Output_segment(type, flags); |
3023 | this->segment_list_.push_back(oseg); | |
2d924fd9 ILT |
3024 | |
3025 | if (type == elfcpp::PT_TLS) | |
3026 | this->tls_segment_ = oseg; | |
3027 | else if (type == elfcpp::PT_GNU_RELRO) | |
3028 | this->relro_segment_ = oseg; | |
3029 | ||
a445fddf ILT |
3030 | return oseg; |
3031 | } | |
3032 | ||
730cdc88 ILT |
3033 | // Write out the Output_sections. Most won't have anything to write, |
3034 | // since most of the data will come from input sections which are | |
3035 | // handled elsewhere. But some Output_sections do have Output_data. | |
3036 | ||
3037 | void | |
3038 | Layout::write_output_sections(Output_file* of) const | |
3039 | { | |
3040 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
3041 | p != this->section_list_.end(); | |
3042 | ++p) | |
3043 | { | |
3044 | if (!(*p)->after_input_sections()) | |
3045 | (*p)->write(of); | |
3046 | } | |
3047 | } | |
3048 | ||
61ba1cf9 ILT |
3049 | // Write out data not associated with a section or the symbol table. |
3050 | ||
3051 | void | |
9025d29d | 3052 | Layout::write_data(const Symbol_table* symtab, Output_file* of) const |
61ba1cf9 | 3053 | { |
8851ecca | 3054 | if (!parameters->options().strip_all()) |
a3ad94ed | 3055 | { |
9e2dcb77 ILT |
3056 | const Output_section* symtab_section = this->symtab_section_; |
3057 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
3058 | p != this->section_list_.end(); | |
3059 | ++p) | |
a3ad94ed | 3060 | { |
9e2dcb77 ILT |
3061 | if ((*p)->needs_symtab_index()) |
3062 | { | |
3063 | gold_assert(symtab_section != NULL); | |
3064 | unsigned int index = (*p)->symtab_index(); | |
3065 | gold_assert(index > 0 && index != -1U); | |
3066 | off_t off = (symtab_section->offset() | |
3067 | + index * symtab_section->entsize()); | |
d491d34e | 3068 | symtab->write_section_symbol(*p, this->symtab_xindex_, of, off); |
9e2dcb77 | 3069 | } |
a3ad94ed ILT |
3070 | } |
3071 | } | |
3072 | ||
3073 | const Output_section* dynsym_section = this->dynsym_section_; | |
3074 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
3075 | p != this->section_list_.end(); | |
3076 | ++p) | |
3077 | { | |
3078 | if ((*p)->needs_dynsym_index()) | |
3079 | { | |
3080 | gold_assert(dynsym_section != NULL); | |
3081 | unsigned int index = (*p)->dynsym_index(); | |
3082 | gold_assert(index > 0 && index != -1U); | |
3083 | off_t off = (dynsym_section->offset() | |
3084 | + index * dynsym_section->entsize()); | |
d491d34e | 3085 | symtab->write_section_symbol(*p, this->dynsym_xindex_, of, off); |
a3ad94ed ILT |
3086 | } |
3087 | } | |
3088 | ||
a3ad94ed | 3089 | // Write out the Output_data which are not in an Output_section. |
61ba1cf9 ILT |
3090 | for (Data_list::const_iterator p = this->special_output_list_.begin(); |
3091 | p != this->special_output_list_.end(); | |
3092 | ++p) | |
3093 | (*p)->write(of); | |
3094 | } | |
3095 | ||
730cdc88 ILT |
3096 | // Write out the Output_sections which can only be written after the |
3097 | // input sections are complete. | |
3098 | ||
3099 | void | |
27bc2bce | 3100 | Layout::write_sections_after_input_sections(Output_file* of) |
730cdc88 | 3101 | { |
27bc2bce | 3102 | // Determine the final section offsets, and thus the final output |
9a0910c3 ILT |
3103 | // file size. Note we finalize the .shstrab last, to allow the |
3104 | // after_input_section sections to modify their section-names before | |
3105 | // writing. | |
17a1d0a9 | 3106 | if (this->any_postprocessing_sections_) |
27bc2bce | 3107 | { |
17a1d0a9 ILT |
3108 | off_t off = this->output_file_size_; |
3109 | off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS); | |
3110 | ||
3111 | // Now that we've finalized the names, we can finalize the shstrab. | |
3112 | off = | |
3113 | this->set_section_offsets(off, | |
3114 | STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS); | |
3115 | ||
3116 | if (off > this->output_file_size_) | |
3117 | { | |
3118 | of->resize(off); | |
3119 | this->output_file_size_ = off; | |
3120 | } | |
27bc2bce ILT |
3121 | } |
3122 | ||
730cdc88 ILT |
3123 | for (Section_list::const_iterator p = this->section_list_.begin(); |
3124 | p != this->section_list_.end(); | |
3125 | ++p) | |
3126 | { | |
3127 | if ((*p)->after_input_sections()) | |
3128 | (*p)->write(of); | |
3129 | } | |
27bc2bce | 3130 | |
27bc2bce | 3131 | this->section_headers_->write(of); |
730cdc88 ILT |
3132 | } |
3133 | ||
8ed814a9 ILT |
3134 | // If the build ID requires computing a checksum, do so here, and |
3135 | // write it out. We compute a checksum over the entire file because | |
3136 | // that is simplest. | |
3137 | ||
3138 | void | |
3139 | Layout::write_build_id(Output_file* of) const | |
3140 | { | |
3141 | if (this->build_id_note_ == NULL) | |
3142 | return; | |
3143 | ||
3144 | const unsigned char* iv = of->get_input_view(0, this->output_file_size_); | |
3145 | ||
3146 | unsigned char* ov = of->get_output_view(this->build_id_note_->offset(), | |
3147 | this->build_id_note_->data_size()); | |
3148 | ||
3149 | const char* style = parameters->options().build_id(); | |
3150 | if (strcmp(style, "sha1") == 0) | |
3151 | { | |
3152 | sha1_ctx ctx; | |
3153 | sha1_init_ctx(&ctx); | |
3154 | sha1_process_bytes(iv, this->output_file_size_, &ctx); | |
3155 | sha1_finish_ctx(&ctx, ov); | |
3156 | } | |
3157 | else if (strcmp(style, "md5") == 0) | |
3158 | { | |
3159 | md5_ctx ctx; | |
3160 | md5_init_ctx(&ctx); | |
3161 | md5_process_bytes(iv, this->output_file_size_, &ctx); | |
3162 | md5_finish_ctx(&ctx, ov); | |
3163 | } | |
3164 | else | |
3165 | gold_unreachable(); | |
3166 | ||
3167 | of->write_output_view(this->build_id_note_->offset(), | |
3168 | this->build_id_note_->data_size(), | |
3169 | ov); | |
3170 | ||
3171 | of->free_input_view(0, this->output_file_size_, iv); | |
3172 | } | |
3173 | ||
516cb3d0 ILT |
3174 | // Write out a binary file. This is called after the link is |
3175 | // complete. IN is the temporary output file we used to generate the | |
3176 | // ELF code. We simply walk through the segments, read them from | |
3177 | // their file offset in IN, and write them to their load address in | |
3178 | // the output file. FIXME: with a bit more work, we could support | |
3179 | // S-records and/or Intel hex format here. | |
3180 | ||
3181 | void | |
3182 | Layout::write_binary(Output_file* in) const | |
3183 | { | |
7cc619c3 | 3184 | gold_assert(this->options_.oformat_enum() |
bc644c6c | 3185 | == General_options::OBJECT_FORMAT_BINARY); |
516cb3d0 ILT |
3186 | |
3187 | // Get the size of the binary file. | |
3188 | uint64_t max_load_address = 0; | |
3189 | for (Segment_list::const_iterator p = this->segment_list_.begin(); | |
3190 | p != this->segment_list_.end(); | |
3191 | ++p) | |
3192 | { | |
3193 | if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0) | |
3194 | { | |
3195 | uint64_t max_paddr = (*p)->paddr() + (*p)->filesz(); | |
3196 | if (max_paddr > max_load_address) | |
3197 | max_load_address = max_paddr; | |
3198 | } | |
3199 | } | |
3200 | ||
8851ecca | 3201 | Output_file out(parameters->options().output_file_name()); |
516cb3d0 ILT |
3202 | out.open(max_load_address); |
3203 | ||
3204 | for (Segment_list::const_iterator p = this->segment_list_.begin(); | |
3205 | p != this->segment_list_.end(); | |
3206 | ++p) | |
3207 | { | |
3208 | if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0) | |
3209 | { | |
3210 | const unsigned char* vin = in->get_input_view((*p)->offset(), | |
3211 | (*p)->filesz()); | |
3212 | unsigned char* vout = out.get_output_view((*p)->paddr(), | |
3213 | (*p)->filesz()); | |
3214 | memcpy(vout, vin, (*p)->filesz()); | |
3215 | out.write_output_view((*p)->paddr(), (*p)->filesz(), vout); | |
3216 | in->free_input_view((*p)->offset(), (*p)->filesz(), vin); | |
3217 | } | |
3218 | } | |
3219 | ||
3220 | out.close(); | |
3221 | } | |
3222 | ||
7d9e3d98 ILT |
3223 | // Print the output sections to the map file. |
3224 | ||
3225 | void | |
3226 | Layout::print_to_mapfile(Mapfile* mapfile) const | |
3227 | { | |
3228 | for (Segment_list::const_iterator p = this->segment_list_.begin(); | |
3229 | p != this->segment_list_.end(); | |
3230 | ++p) | |
3231 | (*p)->print_sections_to_mapfile(mapfile); | |
3232 | } | |
3233 | ||
ad8f37d1 ILT |
3234 | // Print statistical information to stderr. This is used for --stats. |
3235 | ||
3236 | void | |
3237 | Layout::print_stats() const | |
3238 | { | |
3239 | this->namepool_.print_stats("section name pool"); | |
3240 | this->sympool_.print_stats("output symbol name pool"); | |
3241 | this->dynpool_.print_stats("dynamic name pool"); | |
38c5e8b4 ILT |
3242 | |
3243 | for (Section_list::const_iterator p = this->section_list_.begin(); | |
3244 | p != this->section_list_.end(); | |
3245 | ++p) | |
3246 | (*p)->print_merge_stats(); | |
ad8f37d1 ILT |
3247 | } |
3248 | ||
730cdc88 ILT |
3249 | // Write_sections_task methods. |
3250 | ||
3251 | // We can always run this task. | |
3252 | ||
17a1d0a9 ILT |
3253 | Task_token* |
3254 | Write_sections_task::is_runnable() | |
730cdc88 | 3255 | { |
17a1d0a9 | 3256 | return NULL; |
730cdc88 ILT |
3257 | } |
3258 | ||
3259 | // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER | |
3260 | // when finished. | |
3261 | ||
17a1d0a9 ILT |
3262 | void |
3263 | Write_sections_task::locks(Task_locker* tl) | |
730cdc88 | 3264 | { |
17a1d0a9 ILT |
3265 | tl->add(this, this->output_sections_blocker_); |
3266 | tl->add(this, this->final_blocker_); | |
730cdc88 ILT |
3267 | } |
3268 | ||
3269 | // Run the task--write out the data. | |
3270 | ||
3271 | void | |
3272 | Write_sections_task::run(Workqueue*) | |
3273 | { | |
3274 | this->layout_->write_output_sections(this->of_); | |
3275 | } | |
3276 | ||
61ba1cf9 ILT |
3277 | // Write_data_task methods. |
3278 | ||
3279 | // We can always run this task. | |
3280 | ||
17a1d0a9 ILT |
3281 | Task_token* |
3282 | Write_data_task::is_runnable() | |
61ba1cf9 | 3283 | { |
17a1d0a9 | 3284 | return NULL; |
61ba1cf9 ILT |
3285 | } |
3286 | ||
3287 | // We need to unlock FINAL_BLOCKER when finished. | |
3288 | ||
17a1d0a9 ILT |
3289 | void |
3290 | Write_data_task::locks(Task_locker* tl) | |
61ba1cf9 | 3291 | { |
17a1d0a9 | 3292 | tl->add(this, this->final_blocker_); |
61ba1cf9 ILT |
3293 | } |
3294 | ||
3295 | // Run the task--write out the data. | |
3296 | ||
3297 | void | |
3298 | Write_data_task::run(Workqueue*) | |
3299 | { | |
9025d29d | 3300 | this->layout_->write_data(this->symtab_, this->of_); |
61ba1cf9 ILT |
3301 | } |
3302 | ||
3303 | // Write_symbols_task methods. | |
3304 | ||
3305 | // We can always run this task. | |
3306 | ||
17a1d0a9 ILT |
3307 | Task_token* |
3308 | Write_symbols_task::is_runnable() | |
61ba1cf9 | 3309 | { |
17a1d0a9 | 3310 | return NULL; |
61ba1cf9 ILT |
3311 | } |
3312 | ||
3313 | // We need to unlock FINAL_BLOCKER when finished. | |
3314 | ||
17a1d0a9 ILT |
3315 | void |
3316 | Write_symbols_task::locks(Task_locker* tl) | |
61ba1cf9 | 3317 | { |
17a1d0a9 | 3318 | tl->add(this, this->final_blocker_); |
61ba1cf9 ILT |
3319 | } |
3320 | ||
3321 | // Run the task--write out the symbols. | |
3322 | ||
3323 | void | |
3324 | Write_symbols_task::run(Workqueue*) | |
3325 | { | |
9a2d6984 | 3326 | this->symtab_->write_globals(this->input_objects_, this->sympool_, |
d491d34e ILT |
3327 | this->dynpool_, this->layout_->symtab_xindex(), |
3328 | this->layout_->dynsym_xindex(), this->of_); | |
61ba1cf9 ILT |
3329 | } |
3330 | ||
730cdc88 ILT |
3331 | // Write_after_input_sections_task methods. |
3332 | ||
3333 | // We can only run this task after the input sections have completed. | |
3334 | ||
17a1d0a9 ILT |
3335 | Task_token* |
3336 | Write_after_input_sections_task::is_runnable() | |
730cdc88 ILT |
3337 | { |
3338 | if (this->input_sections_blocker_->is_blocked()) | |
17a1d0a9 ILT |
3339 | return this->input_sections_blocker_; |
3340 | return NULL; | |
730cdc88 ILT |
3341 | } |
3342 | ||
3343 | // We need to unlock FINAL_BLOCKER when finished. | |
3344 | ||
17a1d0a9 ILT |
3345 | void |
3346 | Write_after_input_sections_task::locks(Task_locker* tl) | |
730cdc88 | 3347 | { |
17a1d0a9 | 3348 | tl->add(this, this->final_blocker_); |
730cdc88 ILT |
3349 | } |
3350 | ||
3351 | // Run the task. | |
3352 | ||
3353 | void | |
3354 | Write_after_input_sections_task::run(Workqueue*) | |
3355 | { | |
3356 | this->layout_->write_sections_after_input_sections(this->of_); | |
3357 | } | |
3358 | ||
92e059d8 | 3359 | // Close_task_runner methods. |
61ba1cf9 ILT |
3360 | |
3361 | // Run the task--close the file. | |
3362 | ||
3363 | void | |
17a1d0a9 | 3364 | Close_task_runner::run(Workqueue*, const Task*) |
61ba1cf9 | 3365 | { |
8ed814a9 ILT |
3366 | // If we need to compute a checksum for the BUILD if, we do so here. |
3367 | this->layout_->write_build_id(this->of_); | |
3368 | ||
516cb3d0 | 3369 | // If we've been asked to create a binary file, we do so here. |
7cc619c3 | 3370 | if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF) |
516cb3d0 ILT |
3371 | this->layout_->write_binary(this->of_); |
3372 | ||
61ba1cf9 ILT |
3373 | this->of_->close(); |
3374 | } | |
3375 | ||
a2fb1b05 ILT |
3376 | // Instantiate the templates we need. We could use the configure |
3377 | // script to restrict this to only the ones for implemented targets. | |
3378 | ||
193a53d9 | 3379 | #ifdef HAVE_TARGET_32_LITTLE |
a2fb1b05 ILT |
3380 | template |
3381 | Output_section* | |
730cdc88 ILT |
3382 | Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx, |
3383 | const char* name, | |
3384 | const elfcpp::Shdr<32, false>& shdr, | |
3385 | unsigned int, unsigned int, off_t*); | |
193a53d9 | 3386 | #endif |
a2fb1b05 | 3387 | |
193a53d9 | 3388 | #ifdef HAVE_TARGET_32_BIG |
a2fb1b05 ILT |
3389 | template |
3390 | Output_section* | |
730cdc88 ILT |
3391 | Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx, |
3392 | const char* name, | |
3393 | const elfcpp::Shdr<32, true>& shdr, | |
3394 | unsigned int, unsigned int, off_t*); | |
193a53d9 | 3395 | #endif |
a2fb1b05 | 3396 | |
193a53d9 | 3397 | #ifdef HAVE_TARGET_64_LITTLE |
a2fb1b05 ILT |
3398 | template |
3399 | Output_section* | |
730cdc88 ILT |
3400 | Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx, |
3401 | const char* name, | |
3402 | const elfcpp::Shdr<64, false>& shdr, | |
3403 | unsigned int, unsigned int, off_t*); | |
193a53d9 | 3404 | #endif |
a2fb1b05 | 3405 | |
193a53d9 | 3406 | #ifdef HAVE_TARGET_64_BIG |
a2fb1b05 ILT |
3407 | template |
3408 | Output_section* | |
730cdc88 ILT |
3409 | Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx, |
3410 | const char* name, | |
3411 | const elfcpp::Shdr<64, true>& shdr, | |
3412 | unsigned int, unsigned int, off_t*); | |
193a53d9 | 3413 | #endif |
a2fb1b05 | 3414 | |
6a74a719 ILT |
3415 | #ifdef HAVE_TARGET_32_LITTLE |
3416 | template | |
3417 | Output_section* | |
3418 | Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object, | |
3419 | unsigned int reloc_shndx, | |
3420 | const elfcpp::Shdr<32, false>& shdr, | |
3421 | Output_section* data_section, | |
3422 | Relocatable_relocs* rr); | |
3423 | #endif | |
3424 | ||
3425 | #ifdef HAVE_TARGET_32_BIG | |
3426 | template | |
3427 | Output_section* | |
3428 | Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object, | |
3429 | unsigned int reloc_shndx, | |
3430 | const elfcpp::Shdr<32, true>& shdr, | |
3431 | Output_section* data_section, | |
3432 | Relocatable_relocs* rr); | |
3433 | #endif | |
3434 | ||
3435 | #ifdef HAVE_TARGET_64_LITTLE | |
3436 | template | |
3437 | Output_section* | |
3438 | Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object, | |
3439 | unsigned int reloc_shndx, | |
3440 | const elfcpp::Shdr<64, false>& shdr, | |
3441 | Output_section* data_section, | |
3442 | Relocatable_relocs* rr); | |
3443 | #endif | |
3444 | ||
3445 | #ifdef HAVE_TARGET_64_BIG | |
3446 | template | |
3447 | Output_section* | |
3448 | Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object, | |
3449 | unsigned int reloc_shndx, | |
3450 | const elfcpp::Shdr<64, true>& shdr, | |
3451 | Output_section* data_section, | |
3452 | Relocatable_relocs* rr); | |
3453 | #endif | |
3454 | ||
3455 | #ifdef HAVE_TARGET_32_LITTLE | |
3456 | template | |
3457 | void | |
3458 | Layout::layout_group<32, false>(Symbol_table* symtab, | |
3459 | Sized_relobj<32, false>* object, | |
3460 | unsigned int, | |
3461 | const char* group_section_name, | |
3462 | const char* signature, | |
3463 | const elfcpp::Shdr<32, false>& shdr, | |
8825ac63 ILT |
3464 | elfcpp::Elf_Word flags, |
3465 | std::vector<unsigned int>* shndxes); | |
6a74a719 ILT |
3466 | #endif |
3467 | ||
3468 | #ifdef HAVE_TARGET_32_BIG | |
3469 | template | |
3470 | void | |
3471 | Layout::layout_group<32, true>(Symbol_table* symtab, | |
3472 | Sized_relobj<32, true>* object, | |
3473 | unsigned int, | |
3474 | const char* group_section_name, | |
3475 | const char* signature, | |
3476 | const elfcpp::Shdr<32, true>& shdr, | |
8825ac63 ILT |
3477 | elfcpp::Elf_Word flags, |
3478 | std::vector<unsigned int>* shndxes); | |
6a74a719 ILT |
3479 | #endif |
3480 | ||
3481 | #ifdef HAVE_TARGET_64_LITTLE | |
3482 | template | |
3483 | void | |
3484 | Layout::layout_group<64, false>(Symbol_table* symtab, | |
3485 | Sized_relobj<64, false>* object, | |
3486 | unsigned int, | |
3487 | const char* group_section_name, | |
3488 | const char* signature, | |
3489 | const elfcpp::Shdr<64, false>& shdr, | |
8825ac63 ILT |
3490 | elfcpp::Elf_Word flags, |
3491 | std::vector<unsigned int>* shndxes); | |
6a74a719 ILT |
3492 | #endif |
3493 | ||
3494 | #ifdef HAVE_TARGET_64_BIG | |
3495 | template | |
3496 | void | |
3497 | Layout::layout_group<64, true>(Symbol_table* symtab, | |
3498 | Sized_relobj<64, true>* object, | |
3499 | unsigned int, | |
3500 | const char* group_section_name, | |
3501 | const char* signature, | |
3502 | const elfcpp::Shdr<64, true>& shdr, | |
8825ac63 ILT |
3503 | elfcpp::Elf_Word flags, |
3504 | std::vector<unsigned int>* shndxes); | |
6a74a719 ILT |
3505 | #endif |
3506 | ||
730cdc88 ILT |
3507 | #ifdef HAVE_TARGET_32_LITTLE |
3508 | template | |
3509 | Output_section* | |
3510 | Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object, | |
3511 | const unsigned char* symbols, | |
3512 | off_t symbols_size, | |
3513 | const unsigned char* symbol_names, | |
3514 | off_t symbol_names_size, | |
3515 | unsigned int shndx, | |
3516 | const elfcpp::Shdr<32, false>& shdr, | |
3517 | unsigned int reloc_shndx, | |
3518 | unsigned int reloc_type, | |
3519 | off_t* off); | |
3520 | #endif | |
3521 | ||
3522 | #ifdef HAVE_TARGET_32_BIG | |
3523 | template | |
3524 | Output_section* | |
3525 | Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object, | |
3526 | const unsigned char* symbols, | |
3527 | off_t symbols_size, | |
3528 | const unsigned char* symbol_names, | |
3529 | off_t symbol_names_size, | |
3530 | unsigned int shndx, | |
3531 | const elfcpp::Shdr<32, true>& shdr, | |
3532 | unsigned int reloc_shndx, | |
3533 | unsigned int reloc_type, | |
3534 | off_t* off); | |
3535 | #endif | |
3536 | ||
3537 | #ifdef HAVE_TARGET_64_LITTLE | |
3538 | template | |
3539 | Output_section* | |
3540 | Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object, | |
3541 | const unsigned char* symbols, | |
3542 | off_t symbols_size, | |
3543 | const unsigned char* symbol_names, | |
3544 | off_t symbol_names_size, | |
3545 | unsigned int shndx, | |
3546 | const elfcpp::Shdr<64, false>& shdr, | |
3547 | unsigned int reloc_shndx, | |
3548 | unsigned int reloc_type, | |
3549 | off_t* off); | |
3550 | #endif | |
3551 | ||
3552 | #ifdef HAVE_TARGET_64_BIG | |
3553 | template | |
3554 | Output_section* | |
3555 | Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object, | |
3556 | const unsigned char* symbols, | |
3557 | off_t symbols_size, | |
3558 | const unsigned char* symbol_names, | |
3559 | off_t symbol_names_size, | |
3560 | unsigned int shndx, | |
3561 | const elfcpp::Shdr<64, true>& shdr, | |
3562 | unsigned int reloc_shndx, | |
3563 | unsigned int reloc_type, | |
3564 | off_t* off); | |
3565 | #endif | |
a2fb1b05 ILT |
3566 | |
3567 | } // End namespace gold. |