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