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