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