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