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