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