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