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