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