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