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