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