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