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