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