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