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