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