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