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