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