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