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