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