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