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