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