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