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