daily update
[binutils/dougsmingw.git] / gold / layout.cc
blob2ffbdf49770288e42a7cb2ab9c0825adaf260e56
1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <cerrno>
26 #include <cstring>
27 #include <algorithm>
28 #include <iostream>
29 #include <utility>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include "libiberty.h"
33 #include "md5.h"
34 #include "sha1.h"
36 #include "parameters.h"
37 #include "options.h"
38 #include "mapfile.h"
39 #include "script.h"
40 #include "script-sections.h"
41 #include "output.h"
42 #include "symtab.h"
43 #include "dynobj.h"
44 #include "ehframe.h"
45 #include "compressed_output.h"
46 #include "reduced_debug_output.h"
47 #include "reloc.h"
48 #include "descriptors.h"
49 #include "plugin.h"
50 #include "incremental.h"
51 #include "layout.h"
53 namespace gold
56 // Layout::Relaxation_debug_check methods.
58 // Check that sections and special data are in reset states.
59 // We do not save states for Output_sections and special Output_data.
60 // So we check that they have not assigned any addresses or offsets.
61 // clean_up_after_relaxation simply resets their addresses and offsets.
62 void
63 Layout::Relaxation_debug_check::check_output_data_for_reset_values(
64 const Layout::Section_list& sections,
65 const Layout::Data_list& special_outputs)
67 for(Layout::Section_list::const_iterator p = sections.begin();
68 p != sections.end();
69 ++p)
70 gold_assert((*p)->address_and_file_offset_have_reset_values());
72 for(Layout::Data_list::const_iterator p = special_outputs.begin();
73 p != special_outputs.end();
74 ++p)
75 gold_assert((*p)->address_and_file_offset_have_reset_values());
78 // Save information of SECTIONS for checking later.
80 void
81 Layout::Relaxation_debug_check::read_sections(
82 const Layout::Section_list& sections)
84 for(Layout::Section_list::const_iterator p = sections.begin();
85 p != sections.end();
86 ++p)
88 Output_section* os = *p;
89 Section_info info;
90 info.output_section = os;
91 info.address = os->is_address_valid() ? os->address() : 0;
92 info.data_size = os->is_data_size_valid() ? os->data_size() : -1;
93 info.offset = os->is_offset_valid()? os->offset() : -1 ;
94 this->section_infos_.push_back(info);
98 // Verify SECTIONS using previously recorded information.
100 void
101 Layout::Relaxation_debug_check::verify_sections(
102 const Layout::Section_list& sections)
104 size_t i = 0;
105 for(Layout::Section_list::const_iterator p = sections.begin();
106 p != sections.end();
107 ++p, ++i)
109 Output_section* os = *p;
110 uint64_t address = os->is_address_valid() ? os->address() : 0;
111 off_t data_size = os->is_data_size_valid() ? os->data_size() : -1;
112 off_t offset = os->is_offset_valid()? os->offset() : -1 ;
114 if (i >= this->section_infos_.size())
116 gold_fatal("Section_info of %s missing.\n", os->name());
118 const Section_info& info = this->section_infos_[i];
119 if (os != info.output_section)
120 gold_fatal("Section order changed. Expecting %s but see %s\n",
121 info.output_section->name(), os->name());
122 if (address != info.address
123 || data_size != info.data_size
124 || offset != info.offset)
125 gold_fatal("Section %s changed.\n", os->name());
129 // Layout_task_runner methods.
131 // Lay out the sections. This is called after all the input objects
132 // have been read.
134 void
135 Layout_task_runner::run(Workqueue* workqueue, const Task* task)
137 off_t file_size = this->layout_->finalize(this->input_objects_,
138 this->symtab_,
139 this->target_,
140 task);
142 // Now we know the final size of the output file and we know where
143 // each piece of information goes.
145 if (this->mapfile_ != NULL)
147 this->mapfile_->print_discarded_sections(this->input_objects_);
148 this->layout_->print_to_mapfile(this->mapfile_);
151 Output_file* of = new Output_file(parameters->options().output_file_name());
152 if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
153 of->set_is_temporary();
154 of->open(file_size);
156 // Queue up the final set of tasks.
157 gold::queue_final_tasks(this->options_, this->input_objects_,
158 this->symtab_, this->layout_, workqueue, of);
161 // Layout methods.
163 Layout::Layout(int number_of_input_files, Script_options* script_options)
164 : number_of_input_files_(number_of_input_files),
165 script_options_(script_options),
166 namepool_(),
167 sympool_(),
168 dynpool_(),
169 signatures_(),
170 section_name_map_(),
171 segment_list_(),
172 section_list_(),
173 unattached_section_list_(),
174 special_output_list_(),
175 section_headers_(NULL),
176 tls_segment_(NULL),
177 relro_segment_(NULL),
178 increase_relro_(0),
179 symtab_section_(NULL),
180 symtab_xindex_(NULL),
181 dynsym_section_(NULL),
182 dynsym_xindex_(NULL),
183 dynamic_section_(NULL),
184 dynamic_symbol_(NULL),
185 dynamic_data_(NULL),
186 eh_frame_section_(NULL),
187 eh_frame_data_(NULL),
188 added_eh_frame_data_(false),
189 eh_frame_hdr_section_(NULL),
190 build_id_note_(NULL),
191 debug_abbrev_(NULL),
192 debug_info_(NULL),
193 group_signatures_(),
194 output_file_size_(-1),
195 have_added_input_section_(false),
196 sections_are_attached_(false),
197 input_requires_executable_stack_(false),
198 input_with_gnu_stack_note_(false),
199 input_without_gnu_stack_note_(false),
200 has_static_tls_(false),
201 any_postprocessing_sections_(false),
202 resized_signatures_(false),
203 have_stabstr_section_(false),
204 incremental_inputs_(NULL),
205 record_output_section_data_from_script_(false),
206 script_output_section_data_list_(),
207 segment_states_(NULL),
208 relaxation_debug_check_(NULL)
210 // Make space for more than enough segments for a typical file.
211 // This is just for efficiency--it's OK if we wind up needing more.
212 this->segment_list_.reserve(12);
214 // We expect two unattached Output_data objects: the file header and
215 // the segment headers.
216 this->special_output_list_.reserve(2);
218 // Initialize structure needed for an incremental build.
219 if (parameters->options().incremental())
220 this->incremental_inputs_ = new Incremental_inputs;
222 // The section name pool is worth optimizing in all cases, because
223 // it is small, but there are often overlaps due to .rel sections.
224 this->namepool_.set_optimize();
227 // Hash a key we use to look up an output section mapping.
229 size_t
230 Layout::Hash_key::operator()(const Layout::Key& k) const
232 return k.first + k.second.first + k.second.second;
235 // Returns whether the given section is in the list of
236 // debug-sections-used-by-some-version-of-gdb. Currently,
237 // we've checked versions of gdb up to and including 6.7.1.
239 static const char* gdb_sections[] =
240 { ".debug_abbrev",
241 // ".debug_aranges", // not used by gdb as of 6.7.1
242 ".debug_frame",
243 ".debug_info",
244 ".debug_line",
245 ".debug_loc",
246 ".debug_macinfo",
247 // ".debug_pubnames", // not used by gdb as of 6.7.1
248 ".debug_ranges",
249 ".debug_str",
252 static const char* lines_only_debug_sections[] =
253 { ".debug_abbrev",
254 // ".debug_aranges", // not used by gdb as of 6.7.1
255 // ".debug_frame",
256 ".debug_info",
257 ".debug_line",
258 // ".debug_loc",
259 // ".debug_macinfo",
260 // ".debug_pubnames", // not used by gdb as of 6.7.1
261 // ".debug_ranges",
262 ".debug_str",
265 static inline bool
266 is_gdb_debug_section(const char* str)
268 // We can do this faster: binary search or a hashtable. But why bother?
269 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
270 if (strcmp(str, gdb_sections[i]) == 0)
271 return true;
272 return false;
275 static inline bool
276 is_lines_only_debug_section(const char* str)
278 // We can do this faster: binary search or a hashtable. But why bother?
279 for (size_t i = 0;
280 i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections);
281 ++i)
282 if (strcmp(str, lines_only_debug_sections[i]) == 0)
283 return true;
284 return false;
287 // Whether to include this section in the link.
289 template<int size, bool big_endian>
290 bool
291 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
292 const elfcpp::Shdr<size, big_endian>& shdr)
294 if (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE)
295 return false;
297 switch (shdr.get_sh_type())
299 case elfcpp::SHT_NULL:
300 case elfcpp::SHT_SYMTAB:
301 case elfcpp::SHT_DYNSYM:
302 case elfcpp::SHT_HASH:
303 case elfcpp::SHT_DYNAMIC:
304 case elfcpp::SHT_SYMTAB_SHNDX:
305 return false;
307 case elfcpp::SHT_STRTAB:
308 // Discard the sections which have special meanings in the ELF
309 // ABI. Keep others (e.g., .stabstr). We could also do this by
310 // checking the sh_link fields of the appropriate sections.
311 return (strcmp(name, ".dynstr") != 0
312 && strcmp(name, ".strtab") != 0
313 && strcmp(name, ".shstrtab") != 0);
315 case elfcpp::SHT_RELA:
316 case elfcpp::SHT_REL:
317 case elfcpp::SHT_GROUP:
318 // If we are emitting relocations these should be handled
319 // elsewhere.
320 gold_assert(!parameters->options().relocatable()
321 && !parameters->options().emit_relocs());
322 return false;
324 case elfcpp::SHT_PROGBITS:
325 if (parameters->options().strip_debug()
326 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
328 if (is_debug_info_section(name))
329 return false;
331 if (parameters->options().strip_debug_non_line()
332 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
334 // Debugging sections can only be recognized by name.
335 if (is_prefix_of(".debug", name)
336 && !is_lines_only_debug_section(name))
337 return false;
339 if (parameters->options().strip_debug_gdb()
340 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
342 // Debugging sections can only be recognized by name.
343 if (is_prefix_of(".debug", name)
344 && !is_gdb_debug_section(name))
345 return false;
347 if (parameters->options().strip_lto_sections()
348 && !parameters->options().relocatable()
349 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
351 // Ignore LTO sections containing intermediate code.
352 if (is_prefix_of(".gnu.lto_", name))
353 return false;
355 // The GNU linker strips .gnu_debuglink sections, so we do too.
356 // This is a feature used to keep debugging information in
357 // separate files.
358 if (strcmp(name, ".gnu_debuglink") == 0)
359 return false;
360 return true;
362 default:
363 return true;
367 // Return an output section named NAME, or NULL if there is none.
369 Output_section*
370 Layout::find_output_section(const char* name) const
372 for (Section_list::const_iterator p = this->section_list_.begin();
373 p != this->section_list_.end();
374 ++p)
375 if (strcmp((*p)->name(), name) == 0)
376 return *p;
377 return NULL;
380 // Return an output segment of type TYPE, with segment flags SET set
381 // and segment flags CLEAR clear. Return NULL if there is none.
383 Output_segment*
384 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
385 elfcpp::Elf_Word clear) const
387 for (Segment_list::const_iterator p = this->segment_list_.begin();
388 p != this->segment_list_.end();
389 ++p)
390 if (static_cast<elfcpp::PT>((*p)->type()) == type
391 && ((*p)->flags() & set) == set
392 && ((*p)->flags() & clear) == 0)
393 return *p;
394 return NULL;
397 // Return the output section to use for section NAME with type TYPE
398 // and section flags FLAGS. NAME must be canonicalized in the string
399 // pool, and NAME_KEY is the key. IS_INTERP is true if this is the
400 // .interp section. IS_DYNAMIC_LINKER_SECTION is true if this section
401 // is used by the dynamic linker. IS_RELRO is true for a relro
402 // section. IS_LAST_RELRO is true for the last relro section.
403 // IS_FIRST_NON_RELRO is true for the first non-relro section.
405 Output_section*
406 Layout::get_output_section(const char* name, Stringpool::Key name_key,
407 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
408 bool is_interp, bool is_dynamic_linker_section,
409 bool is_relro, bool is_last_relro,
410 bool is_first_non_relro)
412 elfcpp::Elf_Xword lookup_flags = flags;
414 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
415 // read-write with read-only sections. Some other ELF linkers do
416 // not do this. FIXME: Perhaps there should be an option
417 // controlling this.
418 lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
420 const Key key(name_key, std::make_pair(type, lookup_flags));
421 const std::pair<Key, Output_section*> v(key, NULL);
422 std::pair<Section_name_map::iterator, bool> ins(
423 this->section_name_map_.insert(v));
425 if (!ins.second)
426 return ins.first->second;
427 else
429 // This is the first time we've seen this name/type/flags
430 // combination. For compatibility with the GNU linker, we
431 // combine sections with contents and zero flags with sections
432 // with non-zero flags. This is a workaround for cases where
433 // assembler code forgets to set section flags. FIXME: Perhaps
434 // there should be an option to control this.
435 Output_section* os = NULL;
437 if (type == elfcpp::SHT_PROGBITS)
439 if (flags == 0)
441 Output_section* same_name = this->find_output_section(name);
442 if (same_name != NULL
443 && same_name->type() == elfcpp::SHT_PROGBITS
444 && (same_name->flags() & elfcpp::SHF_TLS) == 0)
445 os = same_name;
447 else if ((flags & elfcpp::SHF_TLS) == 0)
449 elfcpp::Elf_Xword zero_flags = 0;
450 const Key zero_key(name_key, std::make_pair(type, zero_flags));
451 Section_name_map::iterator p =
452 this->section_name_map_.find(zero_key);
453 if (p != this->section_name_map_.end())
454 os = p->second;
458 if (os == NULL)
459 os = this->make_output_section(name, type, flags, is_interp,
460 is_dynamic_linker_section, is_relro,
461 is_last_relro, is_first_non_relro);
462 ins.first->second = os;
463 return os;
467 // Pick the output section to use for section NAME, in input file
468 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
469 // linker created section. IS_INPUT_SECTION is true if we are
470 // choosing an output section for an input section found in a input
471 // file. IS_INTERP is true if this is the .interp section.
472 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
473 // dynamic linker. IS_RELRO is true for a relro section.
474 // IS_LAST_RELRO is true for the last relro section.
475 // IS_FIRST_NON_RELRO is true for the first non-relro section. This
476 // will return NULL if the input section should be discarded.
478 Output_section*
479 Layout::choose_output_section(const Relobj* relobj, const char* name,
480 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
481 bool is_input_section, bool is_interp,
482 bool is_dynamic_linker_section, bool is_relro,
483 bool is_last_relro, bool is_first_non_relro)
485 // We should not see any input sections after we have attached
486 // sections to segments.
487 gold_assert(!is_input_section || !this->sections_are_attached_);
489 // Some flags in the input section should not be automatically
490 // copied to the output section.
491 flags &= ~ (elfcpp::SHF_INFO_LINK
492 | elfcpp::SHF_LINK_ORDER
493 | elfcpp::SHF_GROUP
494 | elfcpp::SHF_MERGE
495 | elfcpp::SHF_STRINGS);
497 if (this->script_options_->saw_sections_clause())
499 // We are using a SECTIONS clause, so the output section is
500 // chosen based only on the name.
502 Script_sections* ss = this->script_options_->script_sections();
503 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
504 Output_section** output_section_slot;
505 Script_sections::Section_type script_section_type;
506 name = ss->output_section_name(file_name, name, &output_section_slot,
507 &script_section_type);
508 if (name == NULL)
510 // The SECTIONS clause says to discard this input section.
511 return NULL;
514 // We can only handle script section types ST_NONE and ST_NOLOAD.
515 switch (script_section_type)
517 case Script_sections::ST_NONE:
518 break;
519 case Script_sections::ST_NOLOAD:
520 flags &= elfcpp::SHF_ALLOC;
521 break;
522 default:
523 gold_unreachable();
526 // If this is an orphan section--one not mentioned in the linker
527 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
528 // default processing below.
530 if (output_section_slot != NULL)
532 if (*output_section_slot != NULL)
534 (*output_section_slot)->update_flags_for_input_section(flags);
535 return *output_section_slot;
538 // We don't put sections found in the linker script into
539 // SECTION_NAME_MAP_. That keeps us from getting confused
540 // if an orphan section is mapped to a section with the same
541 // name as one in the linker script.
543 name = this->namepool_.add(name, false, NULL);
545 Output_section* os =
546 this->make_output_section(name, type, flags, is_interp,
547 is_dynamic_linker_section, is_relro,
548 is_last_relro, is_first_non_relro);
549 os->set_found_in_sections_clause();
551 // Special handling for NOLOAD sections.
552 if (script_section_type == Script_sections::ST_NOLOAD)
554 os->set_is_noload();
556 // The constructor of Output_section sets addresses of non-ALLOC
557 // sections to 0 by default. We don't want that for NOLOAD
558 // sections even if they have no SHF_ALLOC flag.
559 if ((os->flags() & elfcpp::SHF_ALLOC) == 0
560 && os->is_address_valid())
562 gold_assert(os->address() == 0
563 && !os->is_offset_valid()
564 && !os->is_data_size_valid());
565 os->reset_address_and_file_offset();
569 *output_section_slot = os;
570 return os;
574 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
576 // Turn NAME from the name of the input section into the name of the
577 // output section.
579 size_t len = strlen(name);
580 if (is_input_section
581 && !this->script_options_->saw_sections_clause()
582 && !parameters->options().relocatable())
583 name = Layout::output_section_name(name, &len);
585 Stringpool::Key name_key;
586 name = this->namepool_.add_with_length(name, len, true, &name_key);
588 // Find or make the output section. The output section is selected
589 // based on the section name, type, and flags.
590 return this->get_output_section(name, name_key, type, flags, is_interp,
591 is_dynamic_linker_section, is_relro,
592 is_last_relro, is_first_non_relro);
595 // Return the output section to use for input section SHNDX, with name
596 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
597 // index of a relocation section which applies to this section, or 0
598 // if none, or -1U if more than one. RELOC_TYPE is the type of the
599 // relocation section if there is one. Set *OFF to the offset of this
600 // input section without the output section. Return NULL if the
601 // section should be discarded. Set *OFF to -1 if the section
602 // contents should not be written directly to the output file, but
603 // will instead receive special handling.
605 template<int size, bool big_endian>
606 Output_section*
607 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
608 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
609 unsigned int reloc_shndx, unsigned int, off_t* off)
611 *off = 0;
613 if (!this->include_section(object, name, shdr))
614 return NULL;
616 Output_section* os;
618 // Sometimes .init_array*, .preinit_array* and .fini_array* do not have
619 // correct section types. Force them here.
620 elfcpp::Elf_Word sh_type = shdr.get_sh_type();
621 if (sh_type == elfcpp::SHT_PROGBITS)
623 static const char init_array_prefix[] = ".init_array";
624 static const char preinit_array_prefix[] = ".preinit_array";
625 static const char fini_array_prefix[] = ".fini_array";
626 static size_t init_array_prefix_size = sizeof(init_array_prefix) - 1;
627 static size_t preinit_array_prefix_size =
628 sizeof(preinit_array_prefix) - 1;
629 static size_t fini_array_prefix_size = sizeof(fini_array_prefix) - 1;
631 if (strncmp(name, init_array_prefix, init_array_prefix_size) == 0)
632 sh_type = elfcpp::SHT_INIT_ARRAY;
633 else if (strncmp(name, preinit_array_prefix, preinit_array_prefix_size)
634 == 0)
635 sh_type = elfcpp::SHT_PREINIT_ARRAY;
636 else if (strncmp(name, fini_array_prefix, fini_array_prefix_size) == 0)
637 sh_type = elfcpp::SHT_FINI_ARRAY;
640 // In a relocatable link a grouped section must not be combined with
641 // any other sections.
642 if (parameters->options().relocatable()
643 && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
645 name = this->namepool_.add(name, true, NULL);
646 os = this->make_output_section(name, sh_type, shdr.get_sh_flags(), false,
647 false, false, false, false);
649 else
651 os = this->choose_output_section(object, name, sh_type,
652 shdr.get_sh_flags(), true, false,
653 false, false, false, false);
654 if (os == NULL)
655 return NULL;
658 // By default the GNU linker sorts input sections whose names match
659 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
660 // are sorted by name. This is used to implement constructor
661 // priority ordering. We are compatible.
662 if (!this->script_options_->saw_sections_clause()
663 && (is_prefix_of(".ctors.", name)
664 || is_prefix_of(".dtors.", name)
665 || is_prefix_of(".init_array.", name)
666 || is_prefix_of(".fini_array.", name)))
667 os->set_must_sort_attached_input_sections();
669 // FIXME: Handle SHF_LINK_ORDER somewhere.
671 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
672 this->script_options_->saw_sections_clause());
673 this->have_added_input_section_ = true;
675 return os;
678 // Handle a relocation section when doing a relocatable link.
680 template<int size, bool big_endian>
681 Output_section*
682 Layout::layout_reloc(Sized_relobj<size, big_endian>* object,
683 unsigned int,
684 const elfcpp::Shdr<size, big_endian>& shdr,
685 Output_section* data_section,
686 Relocatable_relocs* rr)
688 gold_assert(parameters->options().relocatable()
689 || parameters->options().emit_relocs());
691 int sh_type = shdr.get_sh_type();
693 std::string name;
694 if (sh_type == elfcpp::SHT_REL)
695 name = ".rel";
696 else if (sh_type == elfcpp::SHT_RELA)
697 name = ".rela";
698 else
699 gold_unreachable();
700 name += data_section->name();
702 Output_section* os = this->choose_output_section(object, name.c_str(),
703 sh_type,
704 shdr.get_sh_flags(),
705 false, false, false,
706 false, false, false);
708 os->set_should_link_to_symtab();
709 os->set_info_section(data_section);
711 Output_section_data* posd;
712 if (sh_type == elfcpp::SHT_REL)
714 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
715 posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
716 size,
717 big_endian>(rr);
719 else if (sh_type == elfcpp::SHT_RELA)
721 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
722 posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
723 size,
724 big_endian>(rr);
726 else
727 gold_unreachable();
729 os->add_output_section_data(posd);
730 rr->set_output_data(posd);
732 return os;
735 // Handle a group section when doing a relocatable link.
737 template<int size, bool big_endian>
738 void
739 Layout::layout_group(Symbol_table* symtab,
740 Sized_relobj<size, big_endian>* object,
741 unsigned int,
742 const char* group_section_name,
743 const char* signature,
744 const elfcpp::Shdr<size, big_endian>& shdr,
745 elfcpp::Elf_Word flags,
746 std::vector<unsigned int>* shndxes)
748 gold_assert(parameters->options().relocatable());
749 gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
750 group_section_name = this->namepool_.add(group_section_name, true, NULL);
751 Output_section* os = this->make_output_section(group_section_name,
752 elfcpp::SHT_GROUP,
753 shdr.get_sh_flags(),
754 false, false, false,
755 false, false);
757 // We need to find a symbol with the signature in the symbol table.
758 // If we don't find one now, we need to look again later.
759 Symbol* sym = symtab->lookup(signature, NULL);
760 if (sym != NULL)
761 os->set_info_symndx(sym);
762 else
764 // Reserve some space to minimize reallocations.
765 if (this->group_signatures_.empty())
766 this->group_signatures_.reserve(this->number_of_input_files_ * 16);
768 // We will wind up using a symbol whose name is the signature.
769 // So just put the signature in the symbol name pool to save it.
770 signature = symtab->canonicalize_name(signature);
771 this->group_signatures_.push_back(Group_signature(os, signature));
774 os->set_should_link_to_symtab();
775 os->set_entsize(4);
777 section_size_type entry_count =
778 convert_to_section_size_type(shdr.get_sh_size() / 4);
779 Output_section_data* posd =
780 new Output_data_group<size, big_endian>(object, entry_count, flags,
781 shndxes);
782 os->add_output_section_data(posd);
785 // Special GNU handling of sections name .eh_frame. They will
786 // normally hold exception frame data as defined by the C++ ABI
787 // (http://codesourcery.com/cxx-abi/).
789 template<int size, bool big_endian>
790 Output_section*
791 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
792 const unsigned char* symbols,
793 off_t symbols_size,
794 const unsigned char* symbol_names,
795 off_t symbol_names_size,
796 unsigned int shndx,
797 const elfcpp::Shdr<size, big_endian>& shdr,
798 unsigned int reloc_shndx, unsigned int reloc_type,
799 off_t* off)
801 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
802 gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
804 const char* const name = ".eh_frame";
805 Output_section* os = this->choose_output_section(object,
806 name,
807 elfcpp::SHT_PROGBITS,
808 elfcpp::SHF_ALLOC,
809 false, false, false,
810 false, false, false);
811 if (os == NULL)
812 return NULL;
814 if (this->eh_frame_section_ == NULL)
816 this->eh_frame_section_ = os;
817 this->eh_frame_data_ = new Eh_frame();
819 if (parameters->options().eh_frame_hdr())
821 Output_section* hdr_os =
822 this->choose_output_section(NULL,
823 ".eh_frame_hdr",
824 elfcpp::SHT_PROGBITS,
825 elfcpp::SHF_ALLOC,
826 false, false, false,
827 false, false, false);
829 if (hdr_os != NULL)
831 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
832 this->eh_frame_data_);
833 hdr_os->add_output_section_data(hdr_posd);
835 hdr_os->set_after_input_sections();
837 if (!this->script_options_->saw_phdrs_clause())
839 Output_segment* hdr_oseg;
840 hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
841 elfcpp::PF_R);
842 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R, false);
845 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
850 gold_assert(this->eh_frame_section_ == os);
852 if (this->eh_frame_data_->add_ehframe_input_section(object,
853 symbols,
854 symbols_size,
855 symbol_names,
856 symbol_names_size,
857 shndx,
858 reloc_shndx,
859 reloc_type))
861 os->update_flags_for_input_section(shdr.get_sh_flags());
863 // We found a .eh_frame section we are going to optimize, so now
864 // we can add the set of optimized sections to the output
865 // section. We need to postpone adding this until we've found a
866 // section we can optimize so that the .eh_frame section in
867 // crtbegin.o winds up at the start of the output section.
868 if (!this->added_eh_frame_data_)
870 os->add_output_section_data(this->eh_frame_data_);
871 this->added_eh_frame_data_ = true;
873 *off = -1;
875 else
877 // We couldn't handle this .eh_frame section for some reason.
878 // Add it as a normal section.
879 bool saw_sections_clause = this->script_options_->saw_sections_clause();
880 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
881 saw_sections_clause);
882 this->have_added_input_section_ = true;
885 return os;
888 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
889 // the output section.
891 Output_section*
892 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
893 elfcpp::Elf_Xword flags,
894 Output_section_data* posd,
895 bool is_dynamic_linker_section,
896 bool is_relro, bool is_last_relro,
897 bool is_first_non_relro)
899 Output_section* os = this->choose_output_section(NULL, name, type, flags,
900 false, false,
901 is_dynamic_linker_section,
902 is_relro, is_last_relro,
903 is_first_non_relro);
904 if (os != NULL)
905 os->add_output_section_data(posd);
906 return os;
909 // Map section flags to segment flags.
911 elfcpp::Elf_Word
912 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
914 elfcpp::Elf_Word ret = elfcpp::PF_R;
915 if ((flags & elfcpp::SHF_WRITE) != 0)
916 ret |= elfcpp::PF_W;
917 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
918 ret |= elfcpp::PF_X;
919 return ret;
922 // Sometimes we compress sections. This is typically done for
923 // sections that are not part of normal program execution (such as
924 // .debug_* sections), and where the readers of these sections know
925 // how to deal with compressed sections. This routine doesn't say for
926 // certain whether we'll compress -- it depends on commandline options
927 // as well -- just whether this section is a candidate for compression.
928 // (The Output_compressed_section class decides whether to compress
929 // a given section, and picks the name of the compressed section.)
931 static bool
932 is_compressible_debug_section(const char* secname)
934 return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
937 // Make a new Output_section, and attach it to segments as
938 // appropriate. IS_INTERP is true if this is the .interp section.
939 // IS_DYNAMIC_LINKER_SECTION is true if this section is used by the
940 // dynamic linker. IS_RELRO is true if this is a relro section.
941 // IS_LAST_RELRO is true if this is the last relro section.
942 // IS_FIRST_NON_RELRO is true if this is the first non relro section.
944 Output_section*
945 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
946 elfcpp::Elf_Xword flags, bool is_interp,
947 bool is_dynamic_linker_section, bool is_relro,
948 bool is_last_relro, bool is_first_non_relro)
950 Output_section* os;
951 if ((flags & elfcpp::SHF_ALLOC) == 0
952 && strcmp(parameters->options().compress_debug_sections(), "none") != 0
953 && is_compressible_debug_section(name))
954 os = new Output_compressed_section(&parameters->options(), name, type,
955 flags);
956 else if ((flags & elfcpp::SHF_ALLOC) == 0
957 && parameters->options().strip_debug_non_line()
958 && strcmp(".debug_abbrev", name) == 0)
960 os = this->debug_abbrev_ = new Output_reduced_debug_abbrev_section(
961 name, type, flags);
962 if (this->debug_info_)
963 this->debug_info_->set_abbreviations(this->debug_abbrev_);
965 else if ((flags & elfcpp::SHF_ALLOC) == 0
966 && parameters->options().strip_debug_non_line()
967 && strcmp(".debug_info", name) == 0)
969 os = this->debug_info_ = new Output_reduced_debug_info_section(
970 name, type, flags);
971 if (this->debug_abbrev_)
972 this->debug_info_->set_abbreviations(this->debug_abbrev_);
974 else
976 // FIXME: const_cast is ugly.
977 Target* target = const_cast<Target*>(&parameters->target());
978 os = target->make_output_section(name, type, flags);
981 if (is_interp)
982 os->set_is_interp();
983 if (is_dynamic_linker_section)
984 os->set_is_dynamic_linker_section();
985 if (is_relro)
986 os->set_is_relro();
987 if (is_last_relro)
988 os->set_is_last_relro();
989 if (is_first_non_relro)
990 os->set_is_first_non_relro();
992 parameters->target().new_output_section(os);
994 this->section_list_.push_back(os);
996 // The GNU linker by default sorts some sections by priority, so we
997 // do the same. We need to know that this might happen before we
998 // attach any input sections.
999 if (!this->script_options_->saw_sections_clause()
1000 && (strcmp(name, ".ctors") == 0
1001 || strcmp(name, ".dtors") == 0
1002 || strcmp(name, ".init_array") == 0
1003 || strcmp(name, ".fini_array") == 0))
1004 os->set_may_sort_attached_input_sections();
1006 // With -z relro, we have to recognize the special sections by name.
1007 // There is no other way.
1008 if (!this->script_options_->saw_sections_clause()
1009 && parameters->options().relro()
1010 && type == elfcpp::SHT_PROGBITS
1011 && (flags & elfcpp::SHF_ALLOC) != 0
1012 && (flags & elfcpp::SHF_WRITE) != 0)
1014 if (strcmp(name, ".data.rel.ro") == 0)
1015 os->set_is_relro();
1016 else if (strcmp(name, ".data.rel.ro.local") == 0)
1018 os->set_is_relro();
1019 os->set_is_relro_local();
1023 // Check for .stab*str sections, as .stab* sections need to link to
1024 // them.
1025 if (type == elfcpp::SHT_STRTAB
1026 && !this->have_stabstr_section_
1027 && strncmp(name, ".stab", 5) == 0
1028 && strcmp(name + strlen(name) - 3, "str") == 0)
1029 this->have_stabstr_section_ = true;
1031 // If we have already attached the sections to segments, then we
1032 // need to attach this one now. This happens for sections created
1033 // directly by the linker.
1034 if (this->sections_are_attached_)
1035 this->attach_section_to_segment(os);
1037 return os;
1040 // Attach output sections to segments. This is called after we have
1041 // seen all the input sections.
1043 void
1044 Layout::attach_sections_to_segments()
1046 for (Section_list::iterator p = this->section_list_.begin();
1047 p != this->section_list_.end();
1048 ++p)
1049 this->attach_section_to_segment(*p);
1051 this->sections_are_attached_ = true;
1054 // Attach an output section to a segment.
1056 void
1057 Layout::attach_section_to_segment(Output_section* os)
1059 if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
1060 this->unattached_section_list_.push_back(os);
1061 else
1062 this->attach_allocated_section_to_segment(os);
1065 // Attach an allocated output section to a segment.
1067 void
1068 Layout::attach_allocated_section_to_segment(Output_section* os)
1070 elfcpp::Elf_Xword flags = os->flags();
1071 gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
1073 if (parameters->options().relocatable())
1074 return;
1076 // If we have a SECTIONS clause, we can't handle the attachment to
1077 // segments until after we've seen all the sections.
1078 if (this->script_options_->saw_sections_clause())
1079 return;
1081 gold_assert(!this->script_options_->saw_phdrs_clause());
1083 // This output section goes into a PT_LOAD segment.
1085 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
1087 // Check for --section-start.
1088 uint64_t addr;
1089 bool is_address_set = parameters->options().section_start(os->name(), &addr);
1091 // In general the only thing we really care about for PT_LOAD
1092 // segments is whether or not they are writable, so that is how we
1093 // search for them. Large data sections also go into their own
1094 // PT_LOAD segment. People who need segments sorted on some other
1095 // basis will have to use a linker script.
1097 Segment_list::const_iterator p;
1098 for (p = this->segment_list_.begin();
1099 p != this->segment_list_.end();
1100 ++p)
1102 if ((*p)->type() != elfcpp::PT_LOAD)
1103 continue;
1104 if (!parameters->options().omagic()
1105 && ((*p)->flags() & elfcpp::PF_W) != (seg_flags & elfcpp::PF_W))
1106 continue;
1107 // If -Tbss was specified, we need to separate the data and BSS
1108 // segments.
1109 if (parameters->options().user_set_Tbss())
1111 if ((os->type() == elfcpp::SHT_NOBITS)
1112 == (*p)->has_any_data_sections())
1113 continue;
1115 if (os->is_large_data_section() && !(*p)->is_large_data_segment())
1116 continue;
1118 if (is_address_set)
1120 if ((*p)->are_addresses_set())
1121 continue;
1123 (*p)->add_initial_output_data(os);
1124 (*p)->update_flags_for_output_section(seg_flags);
1125 (*p)->set_addresses(addr, addr);
1126 break;
1129 (*p)->add_output_section(os, seg_flags, true);
1130 break;
1133 if (p == this->segment_list_.end())
1135 Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
1136 seg_flags);
1137 if (os->is_large_data_section())
1138 oseg->set_is_large_data_segment();
1139 oseg->add_output_section(os, seg_flags, true);
1140 if (is_address_set)
1141 oseg->set_addresses(addr, addr);
1144 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
1145 // segment.
1146 if (os->type() == elfcpp::SHT_NOTE)
1148 // See if we already have an equivalent PT_NOTE segment.
1149 for (p = this->segment_list_.begin();
1150 p != segment_list_.end();
1151 ++p)
1153 if ((*p)->type() == elfcpp::PT_NOTE
1154 && (((*p)->flags() & elfcpp::PF_W)
1155 == (seg_flags & elfcpp::PF_W)))
1157 (*p)->add_output_section(os, seg_flags, false);
1158 break;
1162 if (p == this->segment_list_.end())
1164 Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
1165 seg_flags);
1166 oseg->add_output_section(os, seg_flags, false);
1170 // If we see a loadable SHF_TLS section, we create a PT_TLS
1171 // segment. There can only be one such segment.
1172 if ((flags & elfcpp::SHF_TLS) != 0)
1174 if (this->tls_segment_ == NULL)
1175 this->make_output_segment(elfcpp::PT_TLS, seg_flags);
1176 this->tls_segment_->add_output_section(os, seg_flags, false);
1179 // If -z relro is in effect, and we see a relro section, we create a
1180 // PT_GNU_RELRO segment. There can only be one such segment.
1181 if (os->is_relro() && parameters->options().relro())
1183 gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W));
1184 if (this->relro_segment_ == NULL)
1185 this->make_output_segment(elfcpp::PT_GNU_RELRO, seg_flags);
1186 this->relro_segment_->add_output_section(os, seg_flags, false);
1190 // Make an output section for a script.
1192 Output_section*
1193 Layout::make_output_section_for_script(
1194 const char* name,
1195 Script_sections::Section_type section_type)
1197 name = this->namepool_.add(name, false, NULL);
1198 elfcpp::Elf_Xword sh_flags = elfcpp::SHF_ALLOC;
1199 if (section_type == Script_sections::ST_NOLOAD)
1200 sh_flags = 0;
1201 Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
1202 sh_flags, false,
1203 false, false, false, false);
1204 os->set_found_in_sections_clause();
1205 if (section_type == Script_sections::ST_NOLOAD)
1206 os->set_is_noload();
1207 return os;
1210 // Return the number of segments we expect to see.
1212 size_t
1213 Layout::expected_segment_count() const
1215 size_t ret = this->segment_list_.size();
1217 // If we didn't see a SECTIONS clause in a linker script, we should
1218 // already have the complete list of segments. Otherwise we ask the
1219 // SECTIONS clause how many segments it expects, and add in the ones
1220 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
1222 if (!this->script_options_->saw_sections_clause())
1223 return ret;
1224 else
1226 const Script_sections* ss = this->script_options_->script_sections();
1227 return ret + ss->expected_segment_count(this);
1231 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
1232 // is whether we saw a .note.GNU-stack section in the object file.
1233 // GNU_STACK_FLAGS is the section flags. The flags give the
1234 // protection required for stack memory. We record this in an
1235 // executable as a PT_GNU_STACK segment. If an object file does not
1236 // have a .note.GNU-stack segment, we must assume that it is an old
1237 // object. On some targets that will force an executable stack.
1239 void
1240 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
1242 if (!seen_gnu_stack)
1243 this->input_without_gnu_stack_note_ = true;
1244 else
1246 this->input_with_gnu_stack_note_ = true;
1247 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
1248 this->input_requires_executable_stack_ = true;
1252 // Create automatic note sections.
1254 void
1255 Layout::create_notes()
1257 this->create_gold_note();
1258 this->create_executable_stack_info();
1259 this->create_build_id();
1262 // Create the dynamic sections which are needed before we read the
1263 // relocs.
1265 void
1266 Layout::create_initial_dynamic_sections(Symbol_table* symtab)
1268 if (parameters->doing_static_link())
1269 return;
1271 this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
1272 elfcpp::SHT_DYNAMIC,
1273 (elfcpp::SHF_ALLOC
1274 | elfcpp::SHF_WRITE),
1275 false, false, true,
1276 true, false, false);
1278 this->dynamic_symbol_ =
1279 symtab->define_in_output_data("_DYNAMIC", NULL, Symbol_table::PREDEFINED,
1280 this->dynamic_section_, 0, 0,
1281 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
1282 elfcpp::STV_HIDDEN, 0, false, false);
1284 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
1286 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
1289 // For each output section whose name can be represented as C symbol,
1290 // define __start and __stop symbols for the section. This is a GNU
1291 // extension.
1293 void
1294 Layout::define_section_symbols(Symbol_table* symtab)
1296 for (Section_list::const_iterator p = this->section_list_.begin();
1297 p != this->section_list_.end();
1298 ++p)
1300 const char* const name = (*p)->name();
1301 if (is_cident(name))
1303 const std::string name_string(name);
1304 const std::string start_name(cident_section_start_prefix
1305 + name_string);
1306 const std::string stop_name(cident_section_stop_prefix
1307 + name_string);
1309 symtab->define_in_output_data(start_name.c_str(),
1310 NULL, // version
1311 Symbol_table::PREDEFINED,
1313 0, // value
1314 0, // symsize
1315 elfcpp::STT_NOTYPE,
1316 elfcpp::STB_GLOBAL,
1317 elfcpp::STV_DEFAULT,
1318 0, // nonvis
1319 false, // offset_is_from_end
1320 true); // only_if_ref
1322 symtab->define_in_output_data(stop_name.c_str(),
1323 NULL, // version
1324 Symbol_table::PREDEFINED,
1326 0, // value
1327 0, // symsize
1328 elfcpp::STT_NOTYPE,
1329 elfcpp::STB_GLOBAL,
1330 elfcpp::STV_DEFAULT,
1331 0, // nonvis
1332 true, // offset_is_from_end
1333 true); // only_if_ref
1338 // Define symbols for group signatures.
1340 void
1341 Layout::define_group_signatures(Symbol_table* symtab)
1343 for (Group_signatures::iterator p = this->group_signatures_.begin();
1344 p != this->group_signatures_.end();
1345 ++p)
1347 Symbol* sym = symtab->lookup(p->signature, NULL);
1348 if (sym != NULL)
1349 p->section->set_info_symndx(sym);
1350 else
1352 // Force the name of the group section to the group
1353 // signature, and use the group's section symbol as the
1354 // signature symbol.
1355 if (strcmp(p->section->name(), p->signature) != 0)
1357 const char* name = this->namepool_.add(p->signature,
1358 true, NULL);
1359 p->section->set_name(name);
1361 p->section->set_needs_symtab_index();
1362 p->section->set_info_section_symndx(p->section);
1366 this->group_signatures_.clear();
1369 // Find the first read-only PT_LOAD segment, creating one if
1370 // necessary.
1372 Output_segment*
1373 Layout::find_first_load_seg()
1375 for (Segment_list::const_iterator p = this->segment_list_.begin();
1376 p != this->segment_list_.end();
1377 ++p)
1379 if ((*p)->type() == elfcpp::PT_LOAD
1380 && ((*p)->flags() & elfcpp::PF_R) != 0
1381 && (parameters->options().omagic()
1382 || ((*p)->flags() & elfcpp::PF_W) == 0))
1383 return *p;
1386 gold_assert(!this->script_options_->saw_phdrs_clause());
1388 Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
1389 elfcpp::PF_R);
1390 return load_seg;
1393 // Save states of all current output segments. Store saved states
1394 // in SEGMENT_STATES.
1396 void
1397 Layout::save_segments(Segment_states* segment_states)
1399 for (Segment_list::const_iterator p = this->segment_list_.begin();
1400 p != this->segment_list_.end();
1401 ++p)
1403 Output_segment* segment = *p;
1404 // Shallow copy.
1405 Output_segment* copy = new Output_segment(*segment);
1406 (*segment_states)[segment] = copy;
1410 // Restore states of output segments and delete any segment not found in
1411 // SEGMENT_STATES.
1413 void
1414 Layout::restore_segments(const Segment_states* segment_states)
1416 // Go through the segment list and remove any segment added in the
1417 // relaxation loop.
1418 this->tls_segment_ = NULL;
1419 this->relro_segment_ = NULL;
1420 Segment_list::iterator list_iter = this->segment_list_.begin();
1421 while (list_iter != this->segment_list_.end())
1423 Output_segment* segment = *list_iter;
1424 Segment_states::const_iterator states_iter =
1425 segment_states->find(segment);
1426 if (states_iter != segment_states->end())
1428 const Output_segment* copy = states_iter->second;
1429 // Shallow copy to restore states.
1430 *segment = *copy;
1432 // Also fix up TLS and RELRO segment pointers as appropriate.
1433 if (segment->type() == elfcpp::PT_TLS)
1434 this->tls_segment_ = segment;
1435 else if (segment->type() == elfcpp::PT_GNU_RELRO)
1436 this->relro_segment_ = segment;
1438 ++list_iter;
1440 else
1442 list_iter = this->segment_list_.erase(list_iter);
1443 // This is a segment created during section layout. It should be
1444 // safe to remove it since we should have removed all pointers to it.
1445 delete segment;
1450 // Clean up after relaxation so that sections can be laid out again.
1452 void
1453 Layout::clean_up_after_relaxation()
1455 // Restore the segments to point state just prior to the relaxation loop.
1456 Script_sections* script_section = this->script_options_->script_sections();
1457 script_section->release_segments();
1458 this->restore_segments(this->segment_states_);
1460 // Reset section addresses and file offsets
1461 for (Section_list::iterator p = this->section_list_.begin();
1462 p != this->section_list_.end();
1463 ++p)
1465 (*p)->restore_states();
1467 // If an input section changes size because of relaxation,
1468 // we need to adjust the section offsets of all input sections.
1469 // after such a section.
1470 if ((*p)->section_offsets_need_adjustment())
1471 (*p)->adjust_section_offsets();
1473 (*p)->reset_address_and_file_offset();
1476 // Reset special output object address and file offsets.
1477 for (Data_list::iterator p = this->special_output_list_.begin();
1478 p != this->special_output_list_.end();
1479 ++p)
1480 (*p)->reset_address_and_file_offset();
1482 // A linker script may have created some output section data objects.
1483 // They are useless now.
1484 for (Output_section_data_list::const_iterator p =
1485 this->script_output_section_data_list_.begin();
1486 p != this->script_output_section_data_list_.end();
1487 ++p)
1488 delete *p;
1489 this->script_output_section_data_list_.clear();
1492 // Prepare for relaxation.
1494 void
1495 Layout::prepare_for_relaxation()
1497 // Create an relaxation debug check if in debugging mode.
1498 if (is_debugging_enabled(DEBUG_RELAXATION))
1499 this->relaxation_debug_check_ = new Relaxation_debug_check();
1501 // Save segment states.
1502 this->segment_states_ = new Segment_states();
1503 this->save_segments(this->segment_states_);
1505 for(Section_list::const_iterator p = this->section_list_.begin();
1506 p != this->section_list_.end();
1507 ++p)
1508 (*p)->save_states();
1510 if (is_debugging_enabled(DEBUG_RELAXATION))
1511 this->relaxation_debug_check_->check_output_data_for_reset_values(
1512 this->section_list_, this->special_output_list_);
1514 // Also enable recording of output section data from scripts.
1515 this->record_output_section_data_from_script_ = true;
1518 // Relaxation loop body: If target has no relaxation, this runs only once
1519 // Otherwise, the target relaxation hook is called at the end of
1520 // each iteration. If the hook returns true, it means re-layout of
1521 // section is required.
1523 // The number of segments created by a linking script without a PHDRS
1524 // clause may be affected by section sizes and alignments. There is
1525 // a remote chance that relaxation causes different number of PT_LOAD
1526 // segments are created and sections are attached to different segments.
1527 // Therefore, we always throw away all segments created during section
1528 // layout. In order to be able to restart the section layout, we keep
1529 // a copy of the segment list right before the relaxation loop and use
1530 // that to restore the segments.
1532 // PASS is the current relaxation pass number.
1533 // SYMTAB is a symbol table.
1534 // PLOAD_SEG is the address of a pointer for the load segment.
1535 // PHDR_SEG is a pointer to the PHDR segment.
1536 // SEGMENT_HEADERS points to the output segment header.
1537 // FILE_HEADER points to the output file header.
1538 // PSHNDX is the address to store the output section index.
1540 off_t inline
1541 Layout::relaxation_loop_body(
1542 int pass,
1543 Target* target,
1544 Symbol_table* symtab,
1545 Output_segment** pload_seg,
1546 Output_segment* phdr_seg,
1547 Output_segment_headers* segment_headers,
1548 Output_file_header* file_header,
1549 unsigned int* pshndx)
1551 // If this is not the first iteration, we need to clean up after
1552 // relaxation so that we can lay out the sections again.
1553 if (pass != 0)
1554 this->clean_up_after_relaxation();
1556 // If there is a SECTIONS clause, put all the input sections into
1557 // the required order.
1558 Output_segment* load_seg;
1559 if (this->script_options_->saw_sections_clause())
1560 load_seg = this->set_section_addresses_from_script(symtab);
1561 else if (parameters->options().relocatable())
1562 load_seg = NULL;
1563 else
1564 load_seg = this->find_first_load_seg();
1566 if (parameters->options().oformat_enum()
1567 != General_options::OBJECT_FORMAT_ELF)
1568 load_seg = NULL;
1570 // If the user set the address of the text segment, that may not be
1571 // compatible with putting the segment headers and file headers into
1572 // that segment.
1573 if (parameters->options().user_set_Ttext())
1574 load_seg = NULL;
1576 gold_assert(phdr_seg == NULL
1577 || load_seg != NULL
1578 || this->script_options_->saw_sections_clause());
1580 // If the address of the load segment we found has been set by
1581 // --section-start rather than by a script, then we don't want to
1582 // use it for the file and segment headers.
1583 if (load_seg != NULL
1584 && load_seg->are_addresses_set()
1585 && !this->script_options_->saw_sections_clause())
1586 load_seg = NULL;
1588 // Lay out the segment headers.
1589 if (!parameters->options().relocatable())
1591 gold_assert(segment_headers != NULL);
1592 if (load_seg != NULL)
1593 load_seg->add_initial_output_data(segment_headers);
1594 if (phdr_seg != NULL)
1595 phdr_seg->add_initial_output_data(segment_headers);
1598 // Lay out the file header.
1599 if (load_seg != NULL)
1600 load_seg->add_initial_output_data(file_header);
1602 if (this->script_options_->saw_phdrs_clause()
1603 && !parameters->options().relocatable())
1605 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1606 // clause in a linker script.
1607 Script_sections* ss = this->script_options_->script_sections();
1608 ss->put_headers_in_phdrs(file_header, segment_headers);
1611 // We set the output section indexes in set_segment_offsets and
1612 // set_section_indexes.
1613 *pshndx = 1;
1615 // Set the file offsets of all the segments, and all the sections
1616 // they contain.
1617 off_t off;
1618 if (!parameters->options().relocatable())
1619 off = this->set_segment_offsets(target, load_seg, pshndx);
1620 else
1621 off = this->set_relocatable_section_offsets(file_header, pshndx);
1623 // Verify that the dummy relaxation does not change anything.
1624 if (is_debugging_enabled(DEBUG_RELAXATION))
1626 if (pass == 0)
1627 this->relaxation_debug_check_->read_sections(this->section_list_);
1628 else
1629 this->relaxation_debug_check_->verify_sections(this->section_list_);
1632 *pload_seg = load_seg;
1633 return off;
1636 // Finalize the layout. When this is called, we have created all the
1637 // output sections and all the output segments which are based on
1638 // input sections. We have several things to do, and we have to do
1639 // them in the right order, so that we get the right results correctly
1640 // and efficiently.
1642 // 1) Finalize the list of output segments and create the segment
1643 // table header.
1645 // 2) Finalize the dynamic symbol table and associated sections.
1647 // 3) Determine the final file offset of all the output segments.
1649 // 4) Determine the final file offset of all the SHF_ALLOC output
1650 // sections.
1652 // 5) Create the symbol table sections and the section name table
1653 // section.
1655 // 6) Finalize the symbol table: set symbol values to their final
1656 // value and make a final determination of which symbols are going
1657 // into the output symbol table.
1659 // 7) Create the section table header.
1661 // 8) Determine the final file offset of all the output sections which
1662 // are not SHF_ALLOC, including the section table header.
1664 // 9) Finalize the ELF file header.
1666 // This function returns the size of the output file.
1668 off_t
1669 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
1670 Target* target, const Task* task)
1672 target->finalize_sections(this, input_objects, symtab);
1674 this->count_local_symbols(task, input_objects);
1676 this->link_stabs_sections();
1678 Output_segment* phdr_seg = NULL;
1679 if (!parameters->options().relocatable() && !parameters->doing_static_link())
1681 // There was a dynamic object in the link. We need to create
1682 // some information for the dynamic linker.
1684 // Create the PT_PHDR segment which will hold the program
1685 // headers.
1686 if (!this->script_options_->saw_phdrs_clause())
1687 phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
1689 // Create the dynamic symbol table, including the hash table.
1690 Output_section* dynstr;
1691 std::vector<Symbol*> dynamic_symbols;
1692 unsigned int local_dynamic_count;
1693 Versions versions(*this->script_options()->version_script_info(),
1694 &this->dynpool_);
1695 this->create_dynamic_symtab(input_objects, symtab, &dynstr,
1696 &local_dynamic_count, &dynamic_symbols,
1697 &versions);
1699 // Create the .interp section to hold the name of the
1700 // interpreter, and put it in a PT_INTERP segment.
1701 if (!parameters->options().shared())
1702 this->create_interp(target);
1704 // Finish the .dynamic section to hold the dynamic data, and put
1705 // it in a PT_DYNAMIC segment.
1706 this->finish_dynamic_section(input_objects, symtab);
1708 // We should have added everything we need to the dynamic string
1709 // table.
1710 this->dynpool_.set_string_offsets();
1712 // Create the version sections. We can't do this until the
1713 // dynamic string table is complete.
1714 this->create_version_sections(&versions, symtab, local_dynamic_count,
1715 dynamic_symbols, dynstr);
1717 // Set the size of the _DYNAMIC symbol. We can't do this until
1718 // after we call create_version_sections.
1719 this->set_dynamic_symbol_size(symtab);
1722 if (this->incremental_inputs_)
1724 this->incremental_inputs_->finalize();
1725 this->create_incremental_info_sections();
1728 // Create segment headers.
1729 Output_segment_headers* segment_headers =
1730 (parameters->options().relocatable()
1731 ? NULL
1732 : new Output_segment_headers(this->segment_list_));
1734 // Lay out the file header.
1735 Output_file_header* file_header
1736 = new Output_file_header(target, symtab, segment_headers,
1737 parameters->options().entry());
1739 this->special_output_list_.push_back(file_header);
1740 if (segment_headers != NULL)
1741 this->special_output_list_.push_back(segment_headers);
1743 // Find approriate places for orphan output sections if we are using
1744 // a linker script.
1745 if (this->script_options_->saw_sections_clause())
1746 this->place_orphan_sections_in_script();
1748 Output_segment* load_seg;
1749 off_t off;
1750 unsigned int shndx;
1751 int pass = 0;
1753 // Take a snapshot of the section layout as needed.
1754 if (target->may_relax())
1755 this->prepare_for_relaxation();
1757 // Run the relaxation loop to lay out sections.
1760 off = this->relaxation_loop_body(pass, target, symtab, &load_seg,
1761 phdr_seg, segment_headers, file_header,
1762 &shndx);
1763 pass++;
1765 while (target->may_relax()
1766 && target->relax(pass, input_objects, symtab, this));
1768 // Set the file offsets of all the non-data sections we've seen so
1769 // far which don't have to wait for the input sections. We need
1770 // this in order to finalize local symbols in non-allocated
1771 // sections.
1772 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1774 // Set the section indexes of all unallocated sections seen so far,
1775 // in case any of them are somehow referenced by a symbol.
1776 shndx = this->set_section_indexes(shndx);
1778 // Create the symbol table sections.
1779 this->create_symtab_sections(input_objects, symtab, shndx, &off);
1780 if (!parameters->doing_static_link())
1781 this->assign_local_dynsym_offsets(input_objects);
1783 // Process any symbol assignments from a linker script. This must
1784 // be called after the symbol table has been finalized.
1785 this->script_options_->finalize_symbols(symtab, this);
1787 // Create the .shstrtab section.
1788 Output_section* shstrtab_section = this->create_shstrtab();
1790 // Set the file offsets of the rest of the non-data sections which
1791 // don't have to wait for the input sections.
1792 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1794 // Now that all sections have been created, set the section indexes
1795 // for any sections which haven't been done yet.
1796 shndx = this->set_section_indexes(shndx);
1798 // Create the section table header.
1799 this->create_shdrs(shstrtab_section, &off);
1801 // If there are no sections which require postprocessing, we can
1802 // handle the section names now, and avoid a resize later.
1803 if (!this->any_postprocessing_sections_)
1804 off = this->set_section_offsets(off,
1805 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
1807 file_header->set_section_info(this->section_headers_, shstrtab_section);
1809 // Now we know exactly where everything goes in the output file
1810 // (except for non-allocated sections which require postprocessing).
1811 Output_data::layout_complete();
1813 this->output_file_size_ = off;
1815 return off;
1818 // Create a note header following the format defined in the ELF ABI.
1819 // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
1820 // of the section to create, DESCSZ is the size of the descriptor.
1821 // ALLOCATE is true if the section should be allocated in memory.
1822 // This returns the new note section. It sets *TRAILING_PADDING to
1823 // the number of trailing zero bytes required.
1825 Output_section*
1826 Layout::create_note(const char* name, int note_type,
1827 const char* section_name, size_t descsz,
1828 bool allocate, size_t* trailing_padding)
1830 // Authorities all agree that the values in a .note field should
1831 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1832 // they differ on what the alignment is for 64-bit binaries.
1833 // The GABI says unambiguously they take 8-byte alignment:
1834 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1835 // Other documentation says alignment should always be 4 bytes:
1836 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1837 // GNU ld and GNU readelf both support the latter (at least as of
1838 // version 2.16.91), and glibc always generates the latter for
1839 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1840 // here.
1841 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1842 const int size = parameters->target().get_size();
1843 #else
1844 const int size = 32;
1845 #endif
1847 // The contents of the .note section.
1848 size_t namesz = strlen(name) + 1;
1849 size_t aligned_namesz = align_address(namesz, size / 8);
1850 size_t aligned_descsz = align_address(descsz, size / 8);
1852 size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
1854 unsigned char* buffer = new unsigned char[notehdrsz];
1855 memset(buffer, 0, notehdrsz);
1857 bool is_big_endian = parameters->target().is_big_endian();
1859 if (size == 32)
1861 if (!is_big_endian)
1863 elfcpp::Swap<32, false>::writeval(buffer, namesz);
1864 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
1865 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
1867 else
1869 elfcpp::Swap<32, true>::writeval(buffer, namesz);
1870 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
1871 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
1874 else if (size == 64)
1876 if (!is_big_endian)
1878 elfcpp::Swap<64, false>::writeval(buffer, namesz);
1879 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
1880 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
1882 else
1884 elfcpp::Swap<64, true>::writeval(buffer, namesz);
1885 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
1886 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
1889 else
1890 gold_unreachable();
1892 memcpy(buffer + 3 * (size / 8), name, namesz);
1894 elfcpp::Elf_Xword flags = 0;
1895 if (allocate)
1896 flags = elfcpp::SHF_ALLOC;
1897 Output_section* os = this->choose_output_section(NULL, section_name,
1898 elfcpp::SHT_NOTE,
1899 flags, false, false,
1900 false, false, false, false);
1901 if (os == NULL)
1902 return NULL;
1904 Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
1905 size / 8,
1906 "** note header");
1907 os->add_output_section_data(posd);
1909 *trailing_padding = aligned_descsz - descsz;
1911 return os;
1914 // For an executable or shared library, create a note to record the
1915 // version of gold used to create the binary.
1917 void
1918 Layout::create_gold_note()
1920 if (parameters->options().relocatable())
1921 return;
1923 std::string desc = std::string("gold ") + gold::get_version_string();
1925 size_t trailing_padding;
1926 Output_section *os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
1927 ".note.gnu.gold-version", desc.size(),
1928 false, &trailing_padding);
1929 if (os == NULL)
1930 return;
1932 Output_section_data* posd = new Output_data_const(desc, 4);
1933 os->add_output_section_data(posd);
1935 if (trailing_padding > 0)
1937 posd = new Output_data_zero_fill(trailing_padding, 0);
1938 os->add_output_section_data(posd);
1942 // Record whether the stack should be executable. This can be set
1943 // from the command line using the -z execstack or -z noexecstack
1944 // options. Otherwise, if any input file has a .note.GNU-stack
1945 // section with the SHF_EXECINSTR flag set, the stack should be
1946 // executable. Otherwise, if at least one input file a
1947 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1948 // section, we use the target default for whether the stack should be
1949 // executable. Otherwise, we don't generate a stack note. When
1950 // generating a object file, we create a .note.GNU-stack section with
1951 // the appropriate marking. When generating an executable or shared
1952 // library, we create a PT_GNU_STACK segment.
1954 void
1955 Layout::create_executable_stack_info()
1957 bool is_stack_executable;
1958 if (parameters->options().is_execstack_set())
1959 is_stack_executable = parameters->options().is_stack_executable();
1960 else if (!this->input_with_gnu_stack_note_)
1961 return;
1962 else
1964 if (this->input_requires_executable_stack_)
1965 is_stack_executable = true;
1966 else if (this->input_without_gnu_stack_note_)
1967 is_stack_executable =
1968 parameters->target().is_default_stack_executable();
1969 else
1970 is_stack_executable = false;
1973 if (parameters->options().relocatable())
1975 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
1976 elfcpp::Elf_Xword flags = 0;
1977 if (is_stack_executable)
1978 flags |= elfcpp::SHF_EXECINSTR;
1979 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags, false,
1980 false, false, false, false);
1982 else
1984 if (this->script_options_->saw_phdrs_clause())
1985 return;
1986 int flags = elfcpp::PF_R | elfcpp::PF_W;
1987 if (is_stack_executable)
1988 flags |= elfcpp::PF_X;
1989 this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
1993 // If --build-id was used, set up the build ID note.
1995 void
1996 Layout::create_build_id()
1998 if (!parameters->options().user_set_build_id())
1999 return;
2001 const char* style = parameters->options().build_id();
2002 if (strcmp(style, "none") == 0)
2003 return;
2005 // Set DESCSZ to the size of the note descriptor. When possible,
2006 // set DESC to the note descriptor contents.
2007 size_t descsz;
2008 std::string desc;
2009 if (strcmp(style, "md5") == 0)
2010 descsz = 128 / 8;
2011 else if (strcmp(style, "sha1") == 0)
2012 descsz = 160 / 8;
2013 else if (strcmp(style, "uuid") == 0)
2015 const size_t uuidsz = 128 / 8;
2017 char buffer[uuidsz];
2018 memset(buffer, 0, uuidsz);
2020 int descriptor = open_descriptor(-1, "/dev/urandom", O_RDONLY);
2021 if (descriptor < 0)
2022 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
2023 strerror(errno));
2024 else
2026 ssize_t got = ::read(descriptor, buffer, uuidsz);
2027 release_descriptor(descriptor, true);
2028 if (got < 0)
2029 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
2030 else if (static_cast<size_t>(got) != uuidsz)
2031 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
2032 uuidsz, got);
2035 desc.assign(buffer, uuidsz);
2036 descsz = uuidsz;
2038 else if (strncmp(style, "0x", 2) == 0)
2040 hex_init();
2041 const char* p = style + 2;
2042 while (*p != '\0')
2044 if (hex_p(p[0]) && hex_p(p[1]))
2046 char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
2047 desc += c;
2048 p += 2;
2050 else if (*p == '-' || *p == ':')
2051 ++p;
2052 else
2053 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
2054 style);
2056 descsz = desc.size();
2058 else
2059 gold_fatal(_("unrecognized --build-id argument '%s'"), style);
2061 // Create the note.
2062 size_t trailing_padding;
2063 Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
2064 ".note.gnu.build-id", descsz, true,
2065 &trailing_padding);
2066 if (os == NULL)
2067 return;
2069 if (!desc.empty())
2071 // We know the value already, so we fill it in now.
2072 gold_assert(desc.size() == descsz);
2074 Output_section_data* posd = new Output_data_const(desc, 4);
2075 os->add_output_section_data(posd);
2077 if (trailing_padding != 0)
2079 posd = new Output_data_zero_fill(trailing_padding, 0);
2080 os->add_output_section_data(posd);
2083 else
2085 // We need to compute a checksum after we have completed the
2086 // link.
2087 gold_assert(trailing_padding == 0);
2088 this->build_id_note_ = new Output_data_zero_fill(descsz, 4);
2089 os->add_output_section_data(this->build_id_note_);
2093 // If we have both .stabXX and .stabXXstr sections, then the sh_link
2094 // field of the former should point to the latter. I'm not sure who
2095 // started this, but the GNU linker does it, and some tools depend
2096 // upon it.
2098 void
2099 Layout::link_stabs_sections()
2101 if (!this->have_stabstr_section_)
2102 return;
2104 for (Section_list::iterator p = this->section_list_.begin();
2105 p != this->section_list_.end();
2106 ++p)
2108 if ((*p)->type() != elfcpp::SHT_STRTAB)
2109 continue;
2111 const char* name = (*p)->name();
2112 if (strncmp(name, ".stab", 5) != 0)
2113 continue;
2115 size_t len = strlen(name);
2116 if (strcmp(name + len - 3, "str") != 0)
2117 continue;
2119 std::string stab_name(name, len - 3);
2120 Output_section* stab_sec;
2121 stab_sec = this->find_output_section(stab_name.c_str());
2122 if (stab_sec != NULL)
2123 stab_sec->set_link_section(*p);
2127 // Create .gnu_incremental_inputs and .gnu_incremental_strtab sections needed
2128 // for the next run of incremental linking to check what has changed.
2130 void
2131 Layout::create_incremental_info_sections()
2133 gold_assert(this->incremental_inputs_ != NULL);
2135 // Add the .gnu_incremental_inputs section.
2136 const char *incremental_inputs_name =
2137 this->namepool_.add(".gnu_incremental_inputs", false, NULL);
2138 Output_section* inputs_os =
2139 this->make_output_section(incremental_inputs_name,
2140 elfcpp::SHT_GNU_INCREMENTAL_INPUTS, 0,
2141 false, false, false, false, false);
2142 Output_section_data* posd =
2143 this->incremental_inputs_->create_incremental_inputs_section_data();
2144 inputs_os->add_output_section_data(posd);
2146 // Add the .gnu_incremental_strtab section.
2147 const char *incremental_strtab_name =
2148 this->namepool_.add(".gnu_incremental_strtab", false, NULL);
2149 Output_section* strtab_os = this->make_output_section(incremental_strtab_name,
2150 elfcpp::SHT_STRTAB,
2151 0, false, false,
2152 false, false, false);
2153 Output_data_strtab* strtab_data =
2154 new Output_data_strtab(this->incremental_inputs_->get_stringpool());
2155 strtab_os->add_output_section_data(strtab_data);
2157 inputs_os->set_link_section(strtab_data);
2160 // Return whether SEG1 should be before SEG2 in the output file. This
2161 // is based entirely on the segment type and flags. When this is
2162 // called the segment addresses has normally not yet been set.
2164 bool
2165 Layout::segment_precedes(const Output_segment* seg1,
2166 const Output_segment* seg2)
2168 elfcpp::Elf_Word type1 = seg1->type();
2169 elfcpp::Elf_Word type2 = seg2->type();
2171 // The single PT_PHDR segment is required to precede any loadable
2172 // segment. We simply make it always first.
2173 if (type1 == elfcpp::PT_PHDR)
2175 gold_assert(type2 != elfcpp::PT_PHDR);
2176 return true;
2178 if (type2 == elfcpp::PT_PHDR)
2179 return false;
2181 // The single PT_INTERP segment is required to precede any loadable
2182 // segment. We simply make it always second.
2183 if (type1 == elfcpp::PT_INTERP)
2185 gold_assert(type2 != elfcpp::PT_INTERP);
2186 return true;
2188 if (type2 == elfcpp::PT_INTERP)
2189 return false;
2191 // We then put PT_LOAD segments before any other segments.
2192 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
2193 return true;
2194 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
2195 return false;
2197 // We put the PT_TLS segment last except for the PT_GNU_RELRO
2198 // segment, because that is where the dynamic linker expects to find
2199 // it (this is just for efficiency; other positions would also work
2200 // correctly).
2201 if (type1 == elfcpp::PT_TLS
2202 && type2 != elfcpp::PT_TLS
2203 && type2 != elfcpp::PT_GNU_RELRO)
2204 return false;
2205 if (type2 == elfcpp::PT_TLS
2206 && type1 != elfcpp::PT_TLS
2207 && type1 != elfcpp::PT_GNU_RELRO)
2208 return true;
2210 // We put the PT_GNU_RELRO segment last, because that is where the
2211 // dynamic linker expects to find it (as with PT_TLS, this is just
2212 // for efficiency).
2213 if (type1 == elfcpp::PT_GNU_RELRO && type2 != elfcpp::PT_GNU_RELRO)
2214 return false;
2215 if (type2 == elfcpp::PT_GNU_RELRO && type1 != elfcpp::PT_GNU_RELRO)
2216 return true;
2218 const elfcpp::Elf_Word flags1 = seg1->flags();
2219 const elfcpp::Elf_Word flags2 = seg2->flags();
2221 // The order of non-PT_LOAD segments is unimportant. We simply sort
2222 // by the numeric segment type and flags values. There should not
2223 // be more than one segment with the same type and flags.
2224 if (type1 != elfcpp::PT_LOAD)
2226 if (type1 != type2)
2227 return type1 < type2;
2228 gold_assert(flags1 != flags2);
2229 return flags1 < flags2;
2232 // If the addresses are set already, sort by load address.
2233 if (seg1->are_addresses_set())
2235 if (!seg2->are_addresses_set())
2236 return true;
2238 unsigned int section_count1 = seg1->output_section_count();
2239 unsigned int section_count2 = seg2->output_section_count();
2240 if (section_count1 == 0 && section_count2 > 0)
2241 return true;
2242 if (section_count1 > 0 && section_count2 == 0)
2243 return false;
2245 uint64_t paddr1 = seg1->first_section_load_address();
2246 uint64_t paddr2 = seg2->first_section_load_address();
2247 if (paddr1 != paddr2)
2248 return paddr1 < paddr2;
2250 else if (seg2->are_addresses_set())
2251 return false;
2253 // A segment which holds large data comes after a segment which does
2254 // not hold large data.
2255 if (seg1->is_large_data_segment())
2257 if (!seg2->is_large_data_segment())
2258 return false;
2260 else if (seg2->is_large_data_segment())
2261 return true;
2263 // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
2264 // segments come before writable segments. Then writable segments
2265 // with data come before writable segments without data. Then
2266 // executable segments come before non-executable segments. Then
2267 // the unlikely case of a non-readable segment comes before the
2268 // normal case of a readable segment. If there are multiple
2269 // segments with the same type and flags, we require that the
2270 // address be set, and we sort by virtual address and then physical
2271 // address.
2272 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
2273 return (flags1 & elfcpp::PF_W) == 0;
2274 if ((flags1 & elfcpp::PF_W) != 0
2275 && seg1->has_any_data_sections() != seg2->has_any_data_sections())
2276 return seg1->has_any_data_sections();
2277 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
2278 return (flags1 & elfcpp::PF_X) != 0;
2279 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
2280 return (flags1 & elfcpp::PF_R) == 0;
2282 // We shouldn't get here--we shouldn't create segments which we
2283 // can't distinguish.
2284 gold_unreachable();
2287 // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
2289 static off_t
2290 align_file_offset(off_t off, uint64_t addr, uint64_t abi_pagesize)
2292 uint64_t unsigned_off = off;
2293 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
2294 | (addr & (abi_pagesize - 1)));
2295 if (aligned_off < unsigned_off)
2296 aligned_off += abi_pagesize;
2297 return aligned_off;
2300 // Set the file offsets of all the segments, and all the sections they
2301 // contain. They have all been created. LOAD_SEG must be be laid out
2302 // first. Return the offset of the data to follow.
2304 off_t
2305 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
2306 unsigned int *pshndx)
2308 // Sort them into the final order.
2309 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
2310 Layout::Compare_segments());
2312 // Find the PT_LOAD segments, and set their addresses and offsets
2313 // and their section's addresses and offsets.
2314 uint64_t addr;
2315 if (parameters->options().user_set_Ttext())
2316 addr = parameters->options().Ttext();
2317 else if (parameters->options().output_is_position_independent())
2318 addr = 0;
2319 else
2320 addr = target->default_text_segment_address();
2321 off_t off = 0;
2323 // If LOAD_SEG is NULL, then the file header and segment headers
2324 // will not be loadable. But they still need to be at offset 0 in
2325 // the file. Set their offsets now.
2326 if (load_seg == NULL)
2328 for (Data_list::iterator p = this->special_output_list_.begin();
2329 p != this->special_output_list_.end();
2330 ++p)
2332 off = align_address(off, (*p)->addralign());
2333 (*p)->set_address_and_file_offset(0, off);
2334 off += (*p)->data_size();
2338 unsigned int increase_relro = this->increase_relro_;
2339 if (this->script_options_->saw_sections_clause())
2340 increase_relro = 0;
2342 const bool check_sections = parameters->options().check_sections();
2343 Output_segment* last_load_segment = NULL;
2345 bool was_readonly = false;
2346 for (Segment_list::iterator p = this->segment_list_.begin();
2347 p != this->segment_list_.end();
2348 ++p)
2350 if ((*p)->type() == elfcpp::PT_LOAD)
2352 if (load_seg != NULL && load_seg != *p)
2353 gold_unreachable();
2354 load_seg = NULL;
2356 bool are_addresses_set = (*p)->are_addresses_set();
2357 if (are_addresses_set)
2359 // When it comes to setting file offsets, we care about
2360 // the physical address.
2361 addr = (*p)->paddr();
2363 else if (parameters->options().user_set_Tdata()
2364 && ((*p)->flags() & elfcpp::PF_W) != 0
2365 && (!parameters->options().user_set_Tbss()
2366 || (*p)->has_any_data_sections()))
2368 addr = parameters->options().Tdata();
2369 are_addresses_set = true;
2371 else if (parameters->options().user_set_Tbss()
2372 && ((*p)->flags() & elfcpp::PF_W) != 0
2373 && !(*p)->has_any_data_sections())
2375 addr = parameters->options().Tbss();
2376 are_addresses_set = true;
2379 uint64_t orig_addr = addr;
2380 uint64_t orig_off = off;
2382 uint64_t aligned_addr = 0;
2383 uint64_t abi_pagesize = target->abi_pagesize();
2384 uint64_t common_pagesize = target->common_pagesize();
2386 if (!parameters->options().nmagic()
2387 && !parameters->options().omagic())
2388 (*p)->set_minimum_p_align(common_pagesize);
2390 if (!are_addresses_set)
2392 // If the last segment was readonly, and this one is
2393 // not, then skip the address forward one page,
2394 // maintaining the same position within the page. This
2395 // lets us store both segments overlapping on a single
2396 // page in the file, but the loader will put them on
2397 // different pages in memory.
2399 addr = align_address(addr, (*p)->maximum_alignment());
2400 aligned_addr = addr;
2402 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
2404 if ((addr & (abi_pagesize - 1)) != 0)
2405 addr = addr + abi_pagesize;
2408 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
2411 if (!parameters->options().nmagic()
2412 && !parameters->options().omagic())
2413 off = align_file_offset(off, addr, abi_pagesize);
2414 else if (load_seg == NULL)
2416 // This is -N or -n with a section script which prevents
2417 // us from using a load segment. We need to ensure that
2418 // the file offset is aligned to the alignment of the
2419 // segment. This is because the linker script
2420 // implicitly assumed a zero offset. If we don't align
2421 // here, then the alignment of the sections in the
2422 // linker script may not match the alignment of the
2423 // sections in the set_section_addresses call below,
2424 // causing an error about dot moving backward.
2425 off = align_address(off, (*p)->maximum_alignment());
2428 unsigned int shndx_hold = *pshndx;
2429 uint64_t new_addr = (*p)->set_section_addresses(this, false, addr,
2430 increase_relro,
2431 &off, pshndx);
2433 // Now that we know the size of this segment, we may be able
2434 // to save a page in memory, at the cost of wasting some
2435 // file space, by instead aligning to the start of a new
2436 // page. Here we use the real machine page size rather than
2437 // the ABI mandated page size.
2439 if (!are_addresses_set && aligned_addr != addr)
2441 uint64_t first_off = (common_pagesize
2442 - (aligned_addr
2443 & (common_pagesize - 1)));
2444 uint64_t last_off = new_addr & (common_pagesize - 1);
2445 if (first_off > 0
2446 && last_off > 0
2447 && ((aligned_addr & ~ (common_pagesize - 1))
2448 != (new_addr & ~ (common_pagesize - 1)))
2449 && first_off + last_off <= common_pagesize)
2451 *pshndx = shndx_hold;
2452 addr = align_address(aligned_addr, common_pagesize);
2453 addr = align_address(addr, (*p)->maximum_alignment());
2454 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
2455 off = align_file_offset(off, addr, abi_pagesize);
2456 new_addr = (*p)->set_section_addresses(this, true, addr,
2457 increase_relro,
2458 &off, pshndx);
2462 addr = new_addr;
2464 if (((*p)->flags() & elfcpp::PF_W) == 0)
2465 was_readonly = true;
2467 // Implement --check-sections. We know that the segments
2468 // are sorted by LMA.
2469 if (check_sections && last_load_segment != NULL)
2471 gold_assert(last_load_segment->paddr() <= (*p)->paddr());
2472 if (last_load_segment->paddr() + last_load_segment->memsz()
2473 > (*p)->paddr())
2475 unsigned long long lb1 = last_load_segment->paddr();
2476 unsigned long long le1 = lb1 + last_load_segment->memsz();
2477 unsigned long long lb2 = (*p)->paddr();
2478 unsigned long long le2 = lb2 + (*p)->memsz();
2479 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
2480 "[0x%llx -> 0x%llx]"),
2481 lb1, le1, lb2, le2);
2484 last_load_segment = *p;
2488 // Handle the non-PT_LOAD segments, setting their offsets from their
2489 // section's offsets.
2490 for (Segment_list::iterator p = this->segment_list_.begin();
2491 p != this->segment_list_.end();
2492 ++p)
2494 if ((*p)->type() != elfcpp::PT_LOAD)
2495 (*p)->set_offset((*p)->type() == elfcpp::PT_GNU_RELRO
2496 ? increase_relro
2497 : 0);
2500 // Set the TLS offsets for each section in the PT_TLS segment.
2501 if (this->tls_segment_ != NULL)
2502 this->tls_segment_->set_tls_offsets();
2504 return off;
2507 // Set the offsets of all the allocated sections when doing a
2508 // relocatable link. This does the same jobs as set_segment_offsets,
2509 // only for a relocatable link.
2511 off_t
2512 Layout::set_relocatable_section_offsets(Output_data* file_header,
2513 unsigned int *pshndx)
2515 off_t off = 0;
2517 file_header->set_address_and_file_offset(0, 0);
2518 off += file_header->data_size();
2520 for (Section_list::iterator p = this->section_list_.begin();
2521 p != this->section_list_.end();
2522 ++p)
2524 // We skip unallocated sections here, except that group sections
2525 // have to come first.
2526 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
2527 && (*p)->type() != elfcpp::SHT_GROUP)
2528 continue;
2530 off = align_address(off, (*p)->addralign());
2532 // The linker script might have set the address.
2533 if (!(*p)->is_address_valid())
2534 (*p)->set_address(0);
2535 (*p)->set_file_offset(off);
2536 (*p)->finalize_data_size();
2537 off += (*p)->data_size();
2539 (*p)->set_out_shndx(*pshndx);
2540 ++*pshndx;
2543 return off;
2546 // Set the file offset of all the sections not associated with a
2547 // segment.
2549 off_t
2550 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
2552 for (Section_list::iterator p = this->unattached_section_list_.begin();
2553 p != this->unattached_section_list_.end();
2554 ++p)
2556 // The symtab section is handled in create_symtab_sections.
2557 if (*p == this->symtab_section_)
2558 continue;
2560 // If we've already set the data size, don't set it again.
2561 if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
2562 continue;
2564 if (pass == BEFORE_INPUT_SECTIONS_PASS
2565 && (*p)->requires_postprocessing())
2567 (*p)->create_postprocessing_buffer();
2568 this->any_postprocessing_sections_ = true;
2571 if (pass == BEFORE_INPUT_SECTIONS_PASS
2572 && (*p)->after_input_sections())
2573 continue;
2574 else if (pass == POSTPROCESSING_SECTIONS_PASS
2575 && (!(*p)->after_input_sections()
2576 || (*p)->type() == elfcpp::SHT_STRTAB))
2577 continue;
2578 else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
2579 && (!(*p)->after_input_sections()
2580 || (*p)->type() != elfcpp::SHT_STRTAB))
2581 continue;
2583 off = align_address(off, (*p)->addralign());
2584 (*p)->set_file_offset(off);
2585 (*p)->finalize_data_size();
2586 off += (*p)->data_size();
2588 // At this point the name must be set.
2589 if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
2590 this->namepool_.add((*p)->name(), false, NULL);
2592 return off;
2595 // Set the section indexes of all the sections not associated with a
2596 // segment.
2598 unsigned int
2599 Layout::set_section_indexes(unsigned int shndx)
2601 for (Section_list::iterator p = this->unattached_section_list_.begin();
2602 p != this->unattached_section_list_.end();
2603 ++p)
2605 if (!(*p)->has_out_shndx())
2607 (*p)->set_out_shndx(shndx);
2608 ++shndx;
2611 return shndx;
2614 // Set the section addresses according to the linker script. This is
2615 // only called when we see a SECTIONS clause. This returns the
2616 // program segment which should hold the file header and segment
2617 // headers, if any. It will return NULL if they should not be in a
2618 // segment.
2620 Output_segment*
2621 Layout::set_section_addresses_from_script(Symbol_table* symtab)
2623 Script_sections* ss = this->script_options_->script_sections();
2624 gold_assert(ss->saw_sections_clause());
2625 return this->script_options_->set_section_addresses(symtab, this);
2628 // Place the orphan sections in the linker script.
2630 void
2631 Layout::place_orphan_sections_in_script()
2633 Script_sections* ss = this->script_options_->script_sections();
2634 gold_assert(ss->saw_sections_clause());
2636 // Place each orphaned output section in the script.
2637 for (Section_list::iterator p = this->section_list_.begin();
2638 p != this->section_list_.end();
2639 ++p)
2641 if (!(*p)->found_in_sections_clause())
2642 ss->place_orphan(*p);
2646 // Count the local symbols in the regular symbol table and the dynamic
2647 // symbol table, and build the respective string pools.
2649 void
2650 Layout::count_local_symbols(const Task* task,
2651 const Input_objects* input_objects)
2653 // First, figure out an upper bound on the number of symbols we'll
2654 // be inserting into each pool. This helps us create the pools with
2655 // the right size, to avoid unnecessary hashtable resizing.
2656 unsigned int symbol_count = 0;
2657 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2658 p != input_objects->relobj_end();
2659 ++p)
2660 symbol_count += (*p)->local_symbol_count();
2662 // Go from "upper bound" to "estimate." We overcount for two
2663 // reasons: we double-count symbols that occur in more than one
2664 // object file, and we count symbols that are dropped from the
2665 // output. Add it all together and assume we overcount by 100%.
2666 symbol_count /= 2;
2668 // We assume all symbols will go into both the sympool and dynpool.
2669 this->sympool_.reserve(symbol_count);
2670 this->dynpool_.reserve(symbol_count);
2672 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2673 p != input_objects->relobj_end();
2674 ++p)
2676 Task_lock_obj<Object> tlo(task, *p);
2677 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
2681 // Create the symbol table sections. Here we also set the final
2682 // values of the symbols. At this point all the loadable sections are
2683 // fully laid out. SHNUM is the number of sections so far.
2685 void
2686 Layout::create_symtab_sections(const Input_objects* input_objects,
2687 Symbol_table* symtab,
2688 unsigned int shnum,
2689 off_t* poff)
2691 int symsize;
2692 unsigned int align;
2693 if (parameters->target().get_size() == 32)
2695 symsize = elfcpp::Elf_sizes<32>::sym_size;
2696 align = 4;
2698 else if (parameters->target().get_size() == 64)
2700 symsize = elfcpp::Elf_sizes<64>::sym_size;
2701 align = 8;
2703 else
2704 gold_unreachable();
2706 off_t off = *poff;
2707 off = align_address(off, align);
2708 off_t startoff = off;
2710 // Save space for the dummy symbol at the start of the section. We
2711 // never bother to write this out--it will just be left as zero.
2712 off += symsize;
2713 unsigned int local_symbol_index = 1;
2715 // Add STT_SECTION symbols for each Output section which needs one.
2716 for (Section_list::iterator p = this->section_list_.begin();
2717 p != this->section_list_.end();
2718 ++p)
2720 if (!(*p)->needs_symtab_index())
2721 (*p)->set_symtab_index(-1U);
2722 else
2724 (*p)->set_symtab_index(local_symbol_index);
2725 ++local_symbol_index;
2726 off += symsize;
2730 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2731 p != input_objects->relobj_end();
2732 ++p)
2734 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
2735 off, symtab);
2736 off += (index - local_symbol_index) * symsize;
2737 local_symbol_index = index;
2740 unsigned int local_symcount = local_symbol_index;
2741 gold_assert(static_cast<off_t>(local_symcount * symsize) == off - startoff);
2743 off_t dynoff;
2744 size_t dyn_global_index;
2745 size_t dyncount;
2746 if (this->dynsym_section_ == NULL)
2748 dynoff = 0;
2749 dyn_global_index = 0;
2750 dyncount = 0;
2752 else
2754 dyn_global_index = this->dynsym_section_->info();
2755 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
2756 dynoff = this->dynsym_section_->offset() + locsize;
2757 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
2758 gold_assert(static_cast<off_t>(dyncount * symsize)
2759 == this->dynsym_section_->data_size() - locsize);
2762 off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
2763 &this->sympool_, &local_symcount);
2765 if (!parameters->options().strip_all())
2767 this->sympool_.set_string_offsets();
2769 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
2770 Output_section* osymtab = this->make_output_section(symtab_name,
2771 elfcpp::SHT_SYMTAB,
2772 0, false, false,
2773 false, false, false);
2774 this->symtab_section_ = osymtab;
2776 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
2777 align,
2778 "** symtab");
2779 osymtab->add_output_section_data(pos);
2781 // We generate a .symtab_shndx section if we have more than
2782 // SHN_LORESERVE sections. Technically it is possible that we
2783 // don't need one, because it is possible that there are no
2784 // symbols in any of sections with indexes larger than
2785 // SHN_LORESERVE. That is probably unusual, though, and it is
2786 // easier to always create one than to compute section indexes
2787 // twice (once here, once when writing out the symbols).
2788 if (shnum >= elfcpp::SHN_LORESERVE)
2790 const char* symtab_xindex_name = this->namepool_.add(".symtab_shndx",
2791 false, NULL);
2792 Output_section* osymtab_xindex =
2793 this->make_output_section(symtab_xindex_name,
2794 elfcpp::SHT_SYMTAB_SHNDX, 0, false,
2795 false, false, false, false);
2797 size_t symcount = (off - startoff) / symsize;
2798 this->symtab_xindex_ = new Output_symtab_xindex(symcount);
2800 osymtab_xindex->add_output_section_data(this->symtab_xindex_);
2802 osymtab_xindex->set_link_section(osymtab);
2803 osymtab_xindex->set_addralign(4);
2804 osymtab_xindex->set_entsize(4);
2806 osymtab_xindex->set_after_input_sections();
2808 // This tells the driver code to wait until the symbol table
2809 // has written out before writing out the postprocessing
2810 // sections, including the .symtab_shndx section.
2811 this->any_postprocessing_sections_ = true;
2814 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
2815 Output_section* ostrtab = this->make_output_section(strtab_name,
2816 elfcpp::SHT_STRTAB,
2817 0, false, false,
2818 false, false, false);
2820 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
2821 ostrtab->add_output_section_data(pstr);
2823 osymtab->set_file_offset(startoff);
2824 osymtab->finalize_data_size();
2825 osymtab->set_link_section(ostrtab);
2826 osymtab->set_info(local_symcount);
2827 osymtab->set_entsize(symsize);
2829 *poff = off;
2833 // Create the .shstrtab section, which holds the names of the
2834 // sections. At the time this is called, we have created all the
2835 // output sections except .shstrtab itself.
2837 Output_section*
2838 Layout::create_shstrtab()
2840 // FIXME: We don't need to create a .shstrtab section if we are
2841 // stripping everything.
2843 const char* name = this->namepool_.add(".shstrtab", false, NULL);
2845 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0,
2846 false, false, false, false,
2847 false);
2849 if (strcmp(parameters->options().compress_debug_sections(), "none") != 0)
2851 // We can't write out this section until we've set all the
2852 // section names, and we don't set the names of compressed
2853 // output sections until relocations are complete. FIXME: With
2854 // the current names we use, this is unnecessary.
2855 os->set_after_input_sections();
2858 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
2859 os->add_output_section_data(posd);
2861 return os;
2864 // Create the section headers. SIZE is 32 or 64. OFF is the file
2865 // offset.
2867 void
2868 Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff)
2870 Output_section_headers* oshdrs;
2871 oshdrs = new Output_section_headers(this,
2872 &this->segment_list_,
2873 &this->section_list_,
2874 &this->unattached_section_list_,
2875 &this->namepool_,
2876 shstrtab_section);
2877 off_t off = align_address(*poff, oshdrs->addralign());
2878 oshdrs->set_address_and_file_offset(0, off);
2879 off += oshdrs->data_size();
2880 *poff = off;
2881 this->section_headers_ = oshdrs;
2884 // Count the allocated sections.
2886 size_t
2887 Layout::allocated_output_section_count() const
2889 size_t section_count = 0;
2890 for (Segment_list::const_iterator p = this->segment_list_.begin();
2891 p != this->segment_list_.end();
2892 ++p)
2893 section_count += (*p)->output_section_count();
2894 return section_count;
2897 // Create the dynamic symbol table.
2899 void
2900 Layout::create_dynamic_symtab(const Input_objects* input_objects,
2901 Symbol_table* symtab,
2902 Output_section **pdynstr,
2903 unsigned int* plocal_dynamic_count,
2904 std::vector<Symbol*>* pdynamic_symbols,
2905 Versions* pversions)
2907 // Count all the symbols in the dynamic symbol table, and set the
2908 // dynamic symbol indexes.
2910 // Skip symbol 0, which is always all zeroes.
2911 unsigned int index = 1;
2913 // Add STT_SECTION symbols for each Output section which needs one.
2914 for (Section_list::iterator p = this->section_list_.begin();
2915 p != this->section_list_.end();
2916 ++p)
2918 if (!(*p)->needs_dynsym_index())
2919 (*p)->set_dynsym_index(-1U);
2920 else
2922 (*p)->set_dynsym_index(index);
2923 ++index;
2927 // Count the local symbols that need to go in the dynamic symbol table,
2928 // and set the dynamic symbol indexes.
2929 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2930 p != input_objects->relobj_end();
2931 ++p)
2933 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
2934 index = new_index;
2937 unsigned int local_symcount = index;
2938 *plocal_dynamic_count = local_symcount;
2940 index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
2941 &this->dynpool_, pversions);
2943 int symsize;
2944 unsigned int align;
2945 const int size = parameters->target().get_size();
2946 if (size == 32)
2948 symsize = elfcpp::Elf_sizes<32>::sym_size;
2949 align = 4;
2951 else if (size == 64)
2953 symsize = elfcpp::Elf_sizes<64>::sym_size;
2954 align = 8;
2956 else
2957 gold_unreachable();
2959 // Create the dynamic symbol table section.
2961 Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
2962 elfcpp::SHT_DYNSYM,
2963 elfcpp::SHF_ALLOC,
2964 false, false, true,
2965 false, false, false);
2967 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
2968 align,
2969 "** dynsym");
2970 dynsym->add_output_section_data(odata);
2972 dynsym->set_info(local_symcount);
2973 dynsym->set_entsize(symsize);
2974 dynsym->set_addralign(align);
2976 this->dynsym_section_ = dynsym;
2978 Output_data_dynamic* const odyn = this->dynamic_data_;
2979 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
2980 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
2982 // If there are more than SHN_LORESERVE allocated sections, we
2983 // create a .dynsym_shndx section. It is possible that we don't
2984 // need one, because it is possible that there are no dynamic
2985 // symbols in any of the sections with indexes larger than
2986 // SHN_LORESERVE. This is probably unusual, though, and at this
2987 // time we don't know the actual section indexes so it is
2988 // inconvenient to check.
2989 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE)
2991 Output_section* dynsym_xindex =
2992 this->choose_output_section(NULL, ".dynsym_shndx",
2993 elfcpp::SHT_SYMTAB_SHNDX,
2994 elfcpp::SHF_ALLOC,
2995 false, false, true, false, false, false);
2997 this->dynsym_xindex_ = new Output_symtab_xindex(index);
2999 dynsym_xindex->add_output_section_data(this->dynsym_xindex_);
3001 dynsym_xindex->set_link_section(dynsym);
3002 dynsym_xindex->set_addralign(4);
3003 dynsym_xindex->set_entsize(4);
3005 dynsym_xindex->set_after_input_sections();
3007 // This tells the driver code to wait until the symbol table has
3008 // written out before writing out the postprocessing sections,
3009 // including the .dynsym_shndx section.
3010 this->any_postprocessing_sections_ = true;
3013 // Create the dynamic string table section.
3015 Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
3016 elfcpp::SHT_STRTAB,
3017 elfcpp::SHF_ALLOC,
3018 false, false, true,
3019 false, false, false);
3021 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
3022 dynstr->add_output_section_data(strdata);
3024 dynsym->set_link_section(dynstr);
3025 this->dynamic_section_->set_link_section(dynstr);
3027 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
3028 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
3030 *pdynstr = dynstr;
3032 // Create the hash tables.
3034 if (strcmp(parameters->options().hash_style(), "sysv") == 0
3035 || strcmp(parameters->options().hash_style(), "both") == 0)
3037 unsigned char* phash;
3038 unsigned int hashlen;
3039 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
3040 &phash, &hashlen);
3042 Output_section* hashsec = this->choose_output_section(NULL, ".hash",
3043 elfcpp::SHT_HASH,
3044 elfcpp::SHF_ALLOC,
3045 false, false, true,
3046 false, false,
3047 false);
3049 Output_section_data* hashdata = new Output_data_const_buffer(phash,
3050 hashlen,
3051 align,
3052 "** hash");
3053 hashsec->add_output_section_data(hashdata);
3055 hashsec->set_link_section(dynsym);
3056 hashsec->set_entsize(4);
3058 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
3061 if (strcmp(parameters->options().hash_style(), "gnu") == 0
3062 || strcmp(parameters->options().hash_style(), "both") == 0)
3064 unsigned char* phash;
3065 unsigned int hashlen;
3066 Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount,
3067 &phash, &hashlen);
3069 Output_section* hashsec = this->choose_output_section(NULL, ".gnu.hash",
3070 elfcpp::SHT_GNU_HASH,
3071 elfcpp::SHF_ALLOC,
3072 false, false, true,
3073 false, false,
3074 false);
3076 Output_section_data* hashdata = new Output_data_const_buffer(phash,
3077 hashlen,
3078 align,
3079 "** hash");
3080 hashsec->add_output_section_data(hashdata);
3082 hashsec->set_link_section(dynsym);
3084 // For a 64-bit target, the entries in .gnu.hash do not have a
3085 // uniform size, so we only set the entry size for a 32-bit
3086 // target.
3087 if (parameters->target().get_size() == 32)
3088 hashsec->set_entsize(4);
3090 odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
3094 // Assign offsets to each local portion of the dynamic symbol table.
3096 void
3097 Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
3099 Output_section* dynsym = this->dynsym_section_;
3100 gold_assert(dynsym != NULL);
3102 off_t off = dynsym->offset();
3104 // Skip the dummy symbol at the start of the section.
3105 off += dynsym->entsize();
3107 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
3108 p != input_objects->relobj_end();
3109 ++p)
3111 unsigned int count = (*p)->set_local_dynsym_offset(off);
3112 off += count * dynsym->entsize();
3116 // Create the version sections.
3118 void
3119 Layout::create_version_sections(const Versions* versions,
3120 const Symbol_table* symtab,
3121 unsigned int local_symcount,
3122 const std::vector<Symbol*>& dynamic_symbols,
3123 const Output_section* dynstr)
3125 if (!versions->any_defs() && !versions->any_needs())
3126 return;
3128 switch (parameters->size_and_endianness())
3130 #ifdef HAVE_TARGET_32_LITTLE
3131 case Parameters::TARGET_32_LITTLE:
3132 this->sized_create_version_sections<32, false>(versions, symtab,
3133 local_symcount,
3134 dynamic_symbols, dynstr);
3135 break;
3136 #endif
3137 #ifdef HAVE_TARGET_32_BIG
3138 case Parameters::TARGET_32_BIG:
3139 this->sized_create_version_sections<32, true>(versions, symtab,
3140 local_symcount,
3141 dynamic_symbols, dynstr);
3142 break;
3143 #endif
3144 #ifdef HAVE_TARGET_64_LITTLE
3145 case Parameters::TARGET_64_LITTLE:
3146 this->sized_create_version_sections<64, false>(versions, symtab,
3147 local_symcount,
3148 dynamic_symbols, dynstr);
3149 break;
3150 #endif
3151 #ifdef HAVE_TARGET_64_BIG
3152 case Parameters::TARGET_64_BIG:
3153 this->sized_create_version_sections<64, true>(versions, symtab,
3154 local_symcount,
3155 dynamic_symbols, dynstr);
3156 break;
3157 #endif
3158 default:
3159 gold_unreachable();
3163 // Create the version sections, sized version.
3165 template<int size, bool big_endian>
3166 void
3167 Layout::sized_create_version_sections(
3168 const Versions* versions,
3169 const Symbol_table* symtab,
3170 unsigned int local_symcount,
3171 const std::vector<Symbol*>& dynamic_symbols,
3172 const Output_section* dynstr)
3174 Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
3175 elfcpp::SHT_GNU_versym,
3176 elfcpp::SHF_ALLOC,
3177 false, false, true,
3178 false, false, false);
3180 unsigned char* vbuf;
3181 unsigned int vsize;
3182 versions->symbol_section_contents<size, big_endian>(symtab, &this->dynpool_,
3183 local_symcount,
3184 dynamic_symbols,
3185 &vbuf, &vsize);
3187 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2,
3188 "** versions");
3190 vsec->add_output_section_data(vdata);
3191 vsec->set_entsize(2);
3192 vsec->set_link_section(this->dynsym_section_);
3194 Output_data_dynamic* const odyn = this->dynamic_data_;
3195 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
3197 if (versions->any_defs())
3199 Output_section* vdsec;
3200 vdsec= this->choose_output_section(NULL, ".gnu.version_d",
3201 elfcpp::SHT_GNU_verdef,
3202 elfcpp::SHF_ALLOC,
3203 false, false, true, false, false,
3204 false);
3206 unsigned char* vdbuf;
3207 unsigned int vdsize;
3208 unsigned int vdentries;
3209 versions->def_section_contents<size, big_endian>(&this->dynpool_, &vdbuf,
3210 &vdsize, &vdentries);
3212 Output_section_data* vddata =
3213 new Output_data_const_buffer(vdbuf, vdsize, 4, "** version defs");
3215 vdsec->add_output_section_data(vddata);
3216 vdsec->set_link_section(dynstr);
3217 vdsec->set_info(vdentries);
3219 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
3220 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
3223 if (versions->any_needs())
3225 Output_section* vnsec;
3226 vnsec = this->choose_output_section(NULL, ".gnu.version_r",
3227 elfcpp::SHT_GNU_verneed,
3228 elfcpp::SHF_ALLOC,
3229 false, false, true, false, false,
3230 false);
3232 unsigned char* vnbuf;
3233 unsigned int vnsize;
3234 unsigned int vnentries;
3235 versions->need_section_contents<size, big_endian>(&this->dynpool_,
3236 &vnbuf, &vnsize,
3237 &vnentries);
3239 Output_section_data* vndata =
3240 new Output_data_const_buffer(vnbuf, vnsize, 4, "** version refs");
3242 vnsec->add_output_section_data(vndata);
3243 vnsec->set_link_section(dynstr);
3244 vnsec->set_info(vnentries);
3246 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
3247 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
3251 // Create the .interp section and PT_INTERP segment.
3253 void
3254 Layout::create_interp(const Target* target)
3256 const char* interp = parameters->options().dynamic_linker();
3257 if (interp == NULL)
3259 interp = target->dynamic_linker();
3260 gold_assert(interp != NULL);
3263 size_t len = strlen(interp) + 1;
3265 Output_section_data* odata = new Output_data_const(interp, len, 1);
3267 Output_section* osec = this->choose_output_section(NULL, ".interp",
3268 elfcpp::SHT_PROGBITS,
3269 elfcpp::SHF_ALLOC,
3270 false, true, true,
3271 false, false, false);
3272 osec->add_output_section_data(odata);
3274 if (!this->script_options_->saw_phdrs_clause())
3276 Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP,
3277 elfcpp::PF_R);
3278 oseg->add_output_section(osec, elfcpp::PF_R, false);
3282 // Add dynamic tags for the PLT and the dynamic relocs. This is
3283 // called by the target-specific code. This does nothing if not doing
3284 // a dynamic link.
3286 // USE_REL is true for REL relocs rather than RELA relocs.
3288 // If PLT_GOT is not NULL, then DT_PLTGOT points to it.
3290 // If PLT_REL is not NULL, it is used for DT_PLTRELSZ, and DT_JMPREL,
3291 // and we also set DT_PLTREL. We use PLT_REL's output section, since
3292 // some targets have multiple reloc sections in PLT_REL.
3294 // If DYN_REL is not NULL, it is used for DT_REL/DT_RELA,
3295 // DT_RELSZ/DT_RELASZ, DT_RELENT/DT_RELAENT.
3297 // If ADD_DEBUG is true, we add a DT_DEBUG entry when generating an
3298 // executable.
3300 void
3301 Layout::add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
3302 const Output_data* plt_rel,
3303 const Output_data_reloc_generic* dyn_rel,
3304 bool add_debug, bool dynrel_includes_plt)
3306 Output_data_dynamic* odyn = this->dynamic_data_;
3307 if (odyn == NULL)
3308 return;
3310 if (plt_got != NULL && plt_got->output_section() != NULL)
3311 odyn->add_section_address(elfcpp::DT_PLTGOT, plt_got);
3313 if (plt_rel != NULL && plt_rel->output_section() != NULL)
3315 odyn->add_section_size(elfcpp::DT_PLTRELSZ, plt_rel->output_section());
3316 odyn->add_section_address(elfcpp::DT_JMPREL, plt_rel->output_section());
3317 odyn->add_constant(elfcpp::DT_PLTREL,
3318 use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA);
3321 if (dyn_rel != NULL && dyn_rel->output_section() != NULL)
3323 odyn->add_section_address(use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA,
3324 dyn_rel);
3325 if (plt_rel != NULL && dynrel_includes_plt)
3326 odyn->add_section_size(use_rel ? elfcpp::DT_RELSZ : elfcpp::DT_RELASZ,
3327 dyn_rel, plt_rel);
3328 else
3329 odyn->add_section_size(use_rel ? elfcpp::DT_RELSZ : elfcpp::DT_RELASZ,
3330 dyn_rel);
3331 const int size = parameters->target().get_size();
3332 elfcpp::DT rel_tag;
3333 int rel_size;
3334 if (use_rel)
3336 rel_tag = elfcpp::DT_RELENT;
3337 if (size == 32)
3338 rel_size = Reloc_types<elfcpp::SHT_REL, 32, false>::reloc_size;
3339 else if (size == 64)
3340 rel_size = Reloc_types<elfcpp::SHT_REL, 64, false>::reloc_size;
3341 else
3342 gold_unreachable();
3344 else
3346 rel_tag = elfcpp::DT_RELAENT;
3347 if (size == 32)
3348 rel_size = Reloc_types<elfcpp::SHT_RELA, 32, false>::reloc_size;
3349 else if (size == 64)
3350 rel_size = Reloc_types<elfcpp::SHT_RELA, 64, false>::reloc_size;
3351 else
3352 gold_unreachable();
3354 odyn->add_constant(rel_tag, rel_size);
3356 if (parameters->options().combreloc())
3358 size_t c = dyn_rel->relative_reloc_count();
3359 if (c > 0)
3360 odyn->add_constant((use_rel
3361 ? elfcpp::DT_RELCOUNT
3362 : elfcpp::DT_RELACOUNT),
3367 if (add_debug && !parameters->options().shared())
3369 // The value of the DT_DEBUG tag is filled in by the dynamic
3370 // linker at run time, and used by the debugger.
3371 odyn->add_constant(elfcpp::DT_DEBUG, 0);
3375 // Finish the .dynamic section and PT_DYNAMIC segment.
3377 void
3378 Layout::finish_dynamic_section(const Input_objects* input_objects,
3379 const Symbol_table* symtab)
3381 if (!this->script_options_->saw_phdrs_clause())
3383 Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
3384 (elfcpp::PF_R
3385 | elfcpp::PF_W));
3386 oseg->add_output_section(this->dynamic_section_,
3387 elfcpp::PF_R | elfcpp::PF_W,
3388 false);
3391 Output_data_dynamic* const odyn = this->dynamic_data_;
3393 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
3394 p != input_objects->dynobj_end();
3395 ++p)
3397 if (!(*p)->is_needed()
3398 && (*p)->input_file()->options().as_needed())
3400 // This dynamic object was linked with --as-needed, but it
3401 // is not needed.
3402 continue;
3405 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
3408 if (parameters->options().shared())
3410 const char* soname = parameters->options().soname();
3411 if (soname != NULL)
3412 odyn->add_string(elfcpp::DT_SONAME, soname);
3415 Symbol* sym = symtab->lookup(parameters->options().init());
3416 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
3417 odyn->add_symbol(elfcpp::DT_INIT, sym);
3419 sym = symtab->lookup(parameters->options().fini());
3420 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
3421 odyn->add_symbol(elfcpp::DT_FINI, sym);
3423 // Look for .init_array, .preinit_array and .fini_array by checking
3424 // section types.
3425 for(Layout::Section_list::const_iterator p = this->section_list_.begin();
3426 p != this->section_list_.end();
3427 ++p)
3428 switch((*p)->type())
3430 case elfcpp::SHT_FINI_ARRAY:
3431 odyn->add_section_address(elfcpp::DT_FINI_ARRAY, *p);
3432 odyn->add_section_size(elfcpp::DT_FINI_ARRAYSZ, *p);
3433 break;
3434 case elfcpp::SHT_INIT_ARRAY:
3435 odyn->add_section_address(elfcpp::DT_INIT_ARRAY, *p);
3436 odyn->add_section_size(elfcpp::DT_INIT_ARRAYSZ, *p);
3437 break;
3438 case elfcpp::SHT_PREINIT_ARRAY:
3439 odyn->add_section_address(elfcpp::DT_PREINIT_ARRAY, *p);
3440 odyn->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ, *p);
3441 break;
3442 default:
3443 break;
3446 // Add a DT_RPATH entry if needed.
3447 const General_options::Dir_list& rpath(parameters->options().rpath());
3448 if (!rpath.empty())
3450 std::string rpath_val;
3451 for (General_options::Dir_list::const_iterator p = rpath.begin();
3452 p != rpath.end();
3453 ++p)
3455 if (rpath_val.empty())
3456 rpath_val = p->name();
3457 else
3459 // Eliminate duplicates.
3460 General_options::Dir_list::const_iterator q;
3461 for (q = rpath.begin(); q != p; ++q)
3462 if (q->name() == p->name())
3463 break;
3464 if (q == p)
3466 rpath_val += ':';
3467 rpath_val += p->name();
3472 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
3473 if (parameters->options().enable_new_dtags())
3474 odyn->add_string(elfcpp::DT_RUNPATH, rpath_val);
3477 // Look for text segments that have dynamic relocations.
3478 bool have_textrel = false;
3479 if (!this->script_options_->saw_sections_clause())
3481 for (Segment_list::const_iterator p = this->segment_list_.begin();
3482 p != this->segment_list_.end();
3483 ++p)
3485 if (((*p)->flags() & elfcpp::PF_W) == 0
3486 && (*p)->dynamic_reloc_count() > 0)
3488 have_textrel = true;
3489 break;
3493 else
3495 // We don't know the section -> segment mapping, so we are
3496 // conservative and just look for readonly sections with
3497 // relocations. If those sections wind up in writable segments,
3498 // then we have created an unnecessary DT_TEXTREL entry.
3499 for (Section_list::const_iterator p = this->section_list_.begin();
3500 p != this->section_list_.end();
3501 ++p)
3503 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
3504 && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
3505 && ((*p)->dynamic_reloc_count() > 0))
3507 have_textrel = true;
3508 break;
3513 // Add a DT_FLAGS entry. We add it even if no flags are set so that
3514 // post-link tools can easily modify these flags if desired.
3515 unsigned int flags = 0;
3516 if (have_textrel)
3518 // Add a DT_TEXTREL for compatibility with older loaders.
3519 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
3520 flags |= elfcpp::DF_TEXTREL;
3522 if (parameters->options().text())
3523 gold_error(_("read-only segment has dynamic relocations"));
3524 else if (parameters->options().warn_shared_textrel()
3525 && parameters->options().shared())
3526 gold_warning(_("shared library text segment is not shareable"));
3528 if (parameters->options().shared() && this->has_static_tls())
3529 flags |= elfcpp::DF_STATIC_TLS;
3530 if (parameters->options().origin())
3531 flags |= elfcpp::DF_ORIGIN;
3532 if (parameters->options().Bsymbolic())
3534 flags |= elfcpp::DF_SYMBOLIC;
3535 // Add DT_SYMBOLIC for compatibility with older loaders.
3536 odyn->add_constant(elfcpp::DT_SYMBOLIC, 0);
3538 if (parameters->options().now())
3539 flags |= elfcpp::DF_BIND_NOW;
3540 odyn->add_constant(elfcpp::DT_FLAGS, flags);
3542 flags = 0;
3543 if (parameters->options().initfirst())
3544 flags |= elfcpp::DF_1_INITFIRST;
3545 if (parameters->options().interpose())
3546 flags |= elfcpp::DF_1_INTERPOSE;
3547 if (parameters->options().loadfltr())
3548 flags |= elfcpp::DF_1_LOADFLTR;
3549 if (parameters->options().nodefaultlib())
3550 flags |= elfcpp::DF_1_NODEFLIB;
3551 if (parameters->options().nodelete())
3552 flags |= elfcpp::DF_1_NODELETE;
3553 if (parameters->options().nodlopen())
3554 flags |= elfcpp::DF_1_NOOPEN;
3555 if (parameters->options().nodump())
3556 flags |= elfcpp::DF_1_NODUMP;
3557 if (!parameters->options().shared())
3558 flags &= ~(elfcpp::DF_1_INITFIRST
3559 | elfcpp::DF_1_NODELETE
3560 | elfcpp::DF_1_NOOPEN);
3561 if (parameters->options().origin())
3562 flags |= elfcpp::DF_1_ORIGIN;
3563 if (parameters->options().now())
3564 flags |= elfcpp::DF_1_NOW;
3565 if (flags)
3566 odyn->add_constant(elfcpp::DT_FLAGS_1, flags);
3569 // Set the size of the _DYNAMIC symbol table to be the size of the
3570 // dynamic data.
3572 void
3573 Layout::set_dynamic_symbol_size(const Symbol_table* symtab)
3575 Output_data_dynamic* const odyn = this->dynamic_data_;
3576 odyn->finalize_data_size();
3577 off_t data_size = odyn->data_size();
3578 const int size = parameters->target().get_size();
3579 if (size == 32)
3580 symtab->get_sized_symbol<32>(this->dynamic_symbol_)->set_symsize(data_size);
3581 else if (size == 64)
3582 symtab->get_sized_symbol<64>(this->dynamic_symbol_)->set_symsize(data_size);
3583 else
3584 gold_unreachable();
3587 // The mapping of input section name prefixes to output section names.
3588 // In some cases one prefix is itself a prefix of another prefix; in
3589 // such a case the longer prefix must come first. These prefixes are
3590 // based on the GNU linker default ELF linker script.
3592 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
3593 const Layout::Section_name_mapping Layout::section_name_mapping[] =
3595 MAPPING_INIT(".text.", ".text"),
3596 MAPPING_INIT(".ctors.", ".ctors"),
3597 MAPPING_INIT(".dtors.", ".dtors"),
3598 MAPPING_INIT(".rodata.", ".rodata"),
3599 MAPPING_INIT(".data.rel.ro.local", ".data.rel.ro.local"),
3600 MAPPING_INIT(".data.rel.ro", ".data.rel.ro"),
3601 MAPPING_INIT(".data.", ".data"),
3602 MAPPING_INIT(".bss.", ".bss"),
3603 MAPPING_INIT(".tdata.", ".tdata"),
3604 MAPPING_INIT(".tbss.", ".tbss"),
3605 MAPPING_INIT(".init_array.", ".init_array"),
3606 MAPPING_INIT(".fini_array.", ".fini_array"),
3607 MAPPING_INIT(".sdata.", ".sdata"),
3608 MAPPING_INIT(".sbss.", ".sbss"),
3609 // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
3610 // differently depending on whether it is creating a shared library.
3611 MAPPING_INIT(".sdata2.", ".sdata"),
3612 MAPPING_INIT(".sbss2.", ".sbss"),
3613 MAPPING_INIT(".lrodata.", ".lrodata"),
3614 MAPPING_INIT(".ldata.", ".ldata"),
3615 MAPPING_INIT(".lbss.", ".lbss"),
3616 MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
3617 MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
3618 MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
3619 MAPPING_INIT(".gnu.linkonce.t.", ".text"),
3620 MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
3621 MAPPING_INIT(".gnu.linkonce.d.", ".data"),
3622 MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
3623 MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
3624 MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
3625 MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
3626 MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
3627 MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
3628 MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
3629 MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
3630 MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
3631 MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
3632 MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
3633 MAPPING_INIT(".ARM.extab", ".ARM.extab"),
3634 MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
3635 MAPPING_INIT(".ARM.exidx", ".ARM.exidx"),
3636 MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
3638 #undef MAPPING_INIT
3640 const int Layout::section_name_mapping_count =
3641 (sizeof(Layout::section_name_mapping)
3642 / sizeof(Layout::section_name_mapping[0]));
3644 // Choose the output section name to use given an input section name.
3645 // Set *PLEN to the length of the name. *PLEN is initialized to the
3646 // length of NAME.
3648 const char*
3649 Layout::output_section_name(const char* name, size_t* plen)
3651 // gcc 4.3 generates the following sorts of section names when it
3652 // needs a section name specific to a function:
3653 // .text.FN
3654 // .rodata.FN
3655 // .sdata2.FN
3656 // .data.FN
3657 // .data.rel.FN
3658 // .data.rel.local.FN
3659 // .data.rel.ro.FN
3660 // .data.rel.ro.local.FN
3661 // .sdata.FN
3662 // .bss.FN
3663 // .sbss.FN
3664 // .tdata.FN
3665 // .tbss.FN
3667 // The GNU linker maps all of those to the part before the .FN,
3668 // except that .data.rel.local.FN is mapped to .data, and
3669 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
3670 // beginning with .data.rel.ro.local are grouped together.
3672 // For an anonymous namespace, the string FN can contain a '.'.
3674 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
3675 // GNU linker maps to .rodata.
3677 // The .data.rel.ro sections are used with -z relro. The sections
3678 // are recognized by name. We use the same names that the GNU
3679 // linker does for these sections.
3681 // It is hard to handle this in a principled way, so we don't even
3682 // try. We use a table of mappings. If the input section name is
3683 // not found in the table, we simply use it as the output section
3684 // name.
3686 const Section_name_mapping* psnm = section_name_mapping;
3687 for (int i = 0; i < section_name_mapping_count; ++i, ++psnm)
3689 if (strncmp(name, psnm->from, psnm->fromlen) == 0)
3691 *plen = psnm->tolen;
3692 return psnm->to;
3696 return name;
3699 // Check if a comdat group or .gnu.linkonce section with the given
3700 // NAME is selected for the link. If there is already a section,
3701 // *KEPT_SECTION is set to point to the existing section and the
3702 // function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
3703 // IS_GROUP_NAME are recorded for this NAME in the layout object,
3704 // *KEPT_SECTION is set to the internal copy and the function returns
3705 // true.
3707 bool
3708 Layout::find_or_add_kept_section(const std::string& name,
3709 Relobj* object,
3710 unsigned int shndx,
3711 bool is_comdat,
3712 bool is_group_name,
3713 Kept_section** kept_section)
3715 // It's normal to see a couple of entries here, for the x86 thunk
3716 // sections. If we see more than a few, we're linking a C++
3717 // program, and we resize to get more space to minimize rehashing.
3718 if (this->signatures_.size() > 4
3719 && !this->resized_signatures_)
3721 reserve_unordered_map(&this->signatures_,
3722 this->number_of_input_files_ * 64);
3723 this->resized_signatures_ = true;
3726 Kept_section candidate;
3727 std::pair<Signatures::iterator, bool> ins =
3728 this->signatures_.insert(std::make_pair(name, candidate));
3730 if (kept_section != NULL)
3731 *kept_section = &ins.first->second;
3732 if (ins.second)
3734 // This is the first time we've seen this signature.
3735 ins.first->second.set_object(object);
3736 ins.first->second.set_shndx(shndx);
3737 if (is_comdat)
3738 ins.first->second.set_is_comdat();
3739 if (is_group_name)
3740 ins.first->second.set_is_group_name();
3741 return true;
3744 // We have already seen this signature.
3746 if (ins.first->second.is_group_name())
3748 // We've already seen a real section group with this signature.
3749 // If the kept group is from a plugin object, and we're in the
3750 // replacement phase, accept the new one as a replacement.
3751 if (ins.first->second.object() == NULL
3752 && parameters->options().plugins()->in_replacement_phase())
3754 ins.first->second.set_object(object);
3755 ins.first->second.set_shndx(shndx);
3756 return true;
3758 return false;
3760 else if (is_group_name)
3762 // This is a real section group, and we've already seen a
3763 // linkonce section with this signature. Record that we've seen
3764 // a section group, and don't include this section group.
3765 ins.first->second.set_is_group_name();
3766 return false;
3768 else
3770 // We've already seen a linkonce section and this is a linkonce
3771 // section. These don't block each other--this may be the same
3772 // symbol name with different section types.
3773 return true;
3777 // Store the allocated sections into the section list.
3779 void
3780 Layout::get_allocated_sections(Section_list* section_list) const
3782 for (Section_list::const_iterator p = this->section_list_.begin();
3783 p != this->section_list_.end();
3784 ++p)
3785 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
3786 section_list->push_back(*p);
3789 // Create an output segment.
3791 Output_segment*
3792 Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
3794 gold_assert(!parameters->options().relocatable());
3795 Output_segment* oseg = new Output_segment(type, flags);
3796 this->segment_list_.push_back(oseg);
3798 if (type == elfcpp::PT_TLS)
3799 this->tls_segment_ = oseg;
3800 else if (type == elfcpp::PT_GNU_RELRO)
3801 this->relro_segment_ = oseg;
3803 return oseg;
3806 // Write out the Output_sections. Most won't have anything to write,
3807 // since most of the data will come from input sections which are
3808 // handled elsewhere. But some Output_sections do have Output_data.
3810 void
3811 Layout::write_output_sections(Output_file* of) const
3813 for (Section_list::const_iterator p = this->section_list_.begin();
3814 p != this->section_list_.end();
3815 ++p)
3817 if (!(*p)->after_input_sections())
3818 (*p)->write(of);
3822 // Write out data not associated with a section or the symbol table.
3824 void
3825 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
3827 if (!parameters->options().strip_all())
3829 const Output_section* symtab_section = this->symtab_section_;
3830 for (Section_list::const_iterator p = this->section_list_.begin();
3831 p != this->section_list_.end();
3832 ++p)
3834 if ((*p)->needs_symtab_index())
3836 gold_assert(symtab_section != NULL);
3837 unsigned int index = (*p)->symtab_index();
3838 gold_assert(index > 0 && index != -1U);
3839 off_t off = (symtab_section->offset()
3840 + index * symtab_section->entsize());
3841 symtab->write_section_symbol(*p, this->symtab_xindex_, of, off);
3846 const Output_section* dynsym_section = this->dynsym_section_;
3847 for (Section_list::const_iterator p = this->section_list_.begin();
3848 p != this->section_list_.end();
3849 ++p)
3851 if ((*p)->needs_dynsym_index())
3853 gold_assert(dynsym_section != NULL);
3854 unsigned int index = (*p)->dynsym_index();
3855 gold_assert(index > 0 && index != -1U);
3856 off_t off = (dynsym_section->offset()
3857 + index * dynsym_section->entsize());
3858 symtab->write_section_symbol(*p, this->dynsym_xindex_, of, off);
3862 // Write out the Output_data which are not in an Output_section.
3863 for (Data_list::const_iterator p = this->special_output_list_.begin();
3864 p != this->special_output_list_.end();
3865 ++p)
3866 (*p)->write(of);
3869 // Write out the Output_sections which can only be written after the
3870 // input sections are complete.
3872 void
3873 Layout::write_sections_after_input_sections(Output_file* of)
3875 // Determine the final section offsets, and thus the final output
3876 // file size. Note we finalize the .shstrab last, to allow the
3877 // after_input_section sections to modify their section-names before
3878 // writing.
3879 if (this->any_postprocessing_sections_)
3881 off_t off = this->output_file_size_;
3882 off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
3884 // Now that we've finalized the names, we can finalize the shstrab.
3885 off =
3886 this->set_section_offsets(off,
3887 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
3889 if (off > this->output_file_size_)
3891 of->resize(off);
3892 this->output_file_size_ = off;
3896 for (Section_list::const_iterator p = this->section_list_.begin();
3897 p != this->section_list_.end();
3898 ++p)
3900 if ((*p)->after_input_sections())
3901 (*p)->write(of);
3904 this->section_headers_->write(of);
3907 // If the build ID requires computing a checksum, do so here, and
3908 // write it out. We compute a checksum over the entire file because
3909 // that is simplest.
3911 void
3912 Layout::write_build_id(Output_file* of) const
3914 if (this->build_id_note_ == NULL)
3915 return;
3917 const unsigned char* iv = of->get_input_view(0, this->output_file_size_);
3919 unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
3920 this->build_id_note_->data_size());
3922 const char* style = parameters->options().build_id();
3923 if (strcmp(style, "sha1") == 0)
3925 sha1_ctx ctx;
3926 sha1_init_ctx(&ctx);
3927 sha1_process_bytes(iv, this->output_file_size_, &ctx);
3928 sha1_finish_ctx(&ctx, ov);
3930 else if (strcmp(style, "md5") == 0)
3932 md5_ctx ctx;
3933 md5_init_ctx(&ctx);
3934 md5_process_bytes(iv, this->output_file_size_, &ctx);
3935 md5_finish_ctx(&ctx, ov);
3937 else
3938 gold_unreachable();
3940 of->write_output_view(this->build_id_note_->offset(),
3941 this->build_id_note_->data_size(),
3942 ov);
3944 of->free_input_view(0, this->output_file_size_, iv);
3947 // Write out a binary file. This is called after the link is
3948 // complete. IN is the temporary output file we used to generate the
3949 // ELF code. We simply walk through the segments, read them from
3950 // their file offset in IN, and write them to their load address in
3951 // the output file. FIXME: with a bit more work, we could support
3952 // S-records and/or Intel hex format here.
3954 void
3955 Layout::write_binary(Output_file* in) const
3957 gold_assert(parameters->options().oformat_enum()
3958 == General_options::OBJECT_FORMAT_BINARY);
3960 // Get the size of the binary file.
3961 uint64_t max_load_address = 0;
3962 for (Segment_list::const_iterator p = this->segment_list_.begin();
3963 p != this->segment_list_.end();
3964 ++p)
3966 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
3968 uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
3969 if (max_paddr > max_load_address)
3970 max_load_address = max_paddr;
3974 Output_file out(parameters->options().output_file_name());
3975 out.open(max_load_address);
3977 for (Segment_list::const_iterator p = this->segment_list_.begin();
3978 p != this->segment_list_.end();
3979 ++p)
3981 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
3983 const unsigned char* vin = in->get_input_view((*p)->offset(),
3984 (*p)->filesz());
3985 unsigned char* vout = out.get_output_view((*p)->paddr(),
3986 (*p)->filesz());
3987 memcpy(vout, vin, (*p)->filesz());
3988 out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
3989 in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
3993 out.close();
3996 // Print the output sections to the map file.
3998 void
3999 Layout::print_to_mapfile(Mapfile* mapfile) const
4001 for (Segment_list::const_iterator p = this->segment_list_.begin();
4002 p != this->segment_list_.end();
4003 ++p)
4004 (*p)->print_sections_to_mapfile(mapfile);
4007 // Print statistical information to stderr. This is used for --stats.
4009 void
4010 Layout::print_stats() const
4012 this->namepool_.print_stats("section name pool");
4013 this->sympool_.print_stats("output symbol name pool");
4014 this->dynpool_.print_stats("dynamic name pool");
4016 for (Section_list::const_iterator p = this->section_list_.begin();
4017 p != this->section_list_.end();
4018 ++p)
4019 (*p)->print_merge_stats();
4022 // Write_sections_task methods.
4024 // We can always run this task.
4026 Task_token*
4027 Write_sections_task::is_runnable()
4029 return NULL;
4032 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
4033 // when finished.
4035 void
4036 Write_sections_task::locks(Task_locker* tl)
4038 tl->add(this, this->output_sections_blocker_);
4039 tl->add(this, this->final_blocker_);
4042 // Run the task--write out the data.
4044 void
4045 Write_sections_task::run(Workqueue*)
4047 this->layout_->write_output_sections(this->of_);
4050 // Write_data_task methods.
4052 // We can always run this task.
4054 Task_token*
4055 Write_data_task::is_runnable()
4057 return NULL;
4060 // We need to unlock FINAL_BLOCKER when finished.
4062 void
4063 Write_data_task::locks(Task_locker* tl)
4065 tl->add(this, this->final_blocker_);
4068 // Run the task--write out the data.
4070 void
4071 Write_data_task::run(Workqueue*)
4073 this->layout_->write_data(this->symtab_, this->of_);
4076 // Write_symbols_task methods.
4078 // We can always run this task.
4080 Task_token*
4081 Write_symbols_task::is_runnable()
4083 return NULL;
4086 // We need to unlock FINAL_BLOCKER when finished.
4088 void
4089 Write_symbols_task::locks(Task_locker* tl)
4091 tl->add(this, this->final_blocker_);
4094 // Run the task--write out the symbols.
4096 void
4097 Write_symbols_task::run(Workqueue*)
4099 this->symtab_->write_globals(this->sympool_, this->dynpool_,
4100 this->layout_->symtab_xindex(),
4101 this->layout_->dynsym_xindex(), this->of_);
4104 // Write_after_input_sections_task methods.
4106 // We can only run this task after the input sections have completed.
4108 Task_token*
4109 Write_after_input_sections_task::is_runnable()
4111 if (this->input_sections_blocker_->is_blocked())
4112 return this->input_sections_blocker_;
4113 return NULL;
4116 // We need to unlock FINAL_BLOCKER when finished.
4118 void
4119 Write_after_input_sections_task::locks(Task_locker* tl)
4121 tl->add(this, this->final_blocker_);
4124 // Run the task.
4126 void
4127 Write_after_input_sections_task::run(Workqueue*)
4129 this->layout_->write_sections_after_input_sections(this->of_);
4132 // Close_task_runner methods.
4134 // Run the task--close the file.
4136 void
4137 Close_task_runner::run(Workqueue*, const Task*)
4139 // If we need to compute a checksum for the BUILD if, we do so here.
4140 this->layout_->write_build_id(this->of_);
4142 // If we've been asked to create a binary file, we do so here.
4143 if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
4144 this->layout_->write_binary(this->of_);
4146 this->of_->close();
4149 // Instantiate the templates we need. We could use the configure
4150 // script to restrict this to only the ones for implemented targets.
4152 #ifdef HAVE_TARGET_32_LITTLE
4153 template
4154 Output_section*
4155 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
4156 const char* name,
4157 const elfcpp::Shdr<32, false>& shdr,
4158 unsigned int, unsigned int, off_t*);
4159 #endif
4161 #ifdef HAVE_TARGET_32_BIG
4162 template
4163 Output_section*
4164 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
4165 const char* name,
4166 const elfcpp::Shdr<32, true>& shdr,
4167 unsigned int, unsigned int, off_t*);
4168 #endif
4170 #ifdef HAVE_TARGET_64_LITTLE
4171 template
4172 Output_section*
4173 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
4174 const char* name,
4175 const elfcpp::Shdr<64, false>& shdr,
4176 unsigned int, unsigned int, off_t*);
4177 #endif
4179 #ifdef HAVE_TARGET_64_BIG
4180 template
4181 Output_section*
4182 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
4183 const char* name,
4184 const elfcpp::Shdr<64, true>& shdr,
4185 unsigned int, unsigned int, off_t*);
4186 #endif
4188 #ifdef HAVE_TARGET_32_LITTLE
4189 template
4190 Output_section*
4191 Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object,
4192 unsigned int reloc_shndx,
4193 const elfcpp::Shdr<32, false>& shdr,
4194 Output_section* data_section,
4195 Relocatable_relocs* rr);
4196 #endif
4198 #ifdef HAVE_TARGET_32_BIG
4199 template
4200 Output_section*
4201 Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object,
4202 unsigned int reloc_shndx,
4203 const elfcpp::Shdr<32, true>& shdr,
4204 Output_section* data_section,
4205 Relocatable_relocs* rr);
4206 #endif
4208 #ifdef HAVE_TARGET_64_LITTLE
4209 template
4210 Output_section*
4211 Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object,
4212 unsigned int reloc_shndx,
4213 const elfcpp::Shdr<64, false>& shdr,
4214 Output_section* data_section,
4215 Relocatable_relocs* rr);
4216 #endif
4218 #ifdef HAVE_TARGET_64_BIG
4219 template
4220 Output_section*
4221 Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object,
4222 unsigned int reloc_shndx,
4223 const elfcpp::Shdr<64, true>& shdr,
4224 Output_section* data_section,
4225 Relocatable_relocs* rr);
4226 #endif
4228 #ifdef HAVE_TARGET_32_LITTLE
4229 template
4230 void
4231 Layout::layout_group<32, false>(Symbol_table* symtab,
4232 Sized_relobj<32, false>* object,
4233 unsigned int,
4234 const char* group_section_name,
4235 const char* signature,
4236 const elfcpp::Shdr<32, false>& shdr,
4237 elfcpp::Elf_Word flags,
4238 std::vector<unsigned int>* shndxes);
4239 #endif
4241 #ifdef HAVE_TARGET_32_BIG
4242 template
4243 void
4244 Layout::layout_group<32, true>(Symbol_table* symtab,
4245 Sized_relobj<32, true>* object,
4246 unsigned int,
4247 const char* group_section_name,
4248 const char* signature,
4249 const elfcpp::Shdr<32, true>& shdr,
4250 elfcpp::Elf_Word flags,
4251 std::vector<unsigned int>* shndxes);
4252 #endif
4254 #ifdef HAVE_TARGET_64_LITTLE
4255 template
4256 void
4257 Layout::layout_group<64, false>(Symbol_table* symtab,
4258 Sized_relobj<64, false>* object,
4259 unsigned int,
4260 const char* group_section_name,
4261 const char* signature,
4262 const elfcpp::Shdr<64, false>& shdr,
4263 elfcpp::Elf_Word flags,
4264 std::vector<unsigned int>* shndxes);
4265 #endif
4267 #ifdef HAVE_TARGET_64_BIG
4268 template
4269 void
4270 Layout::layout_group<64, true>(Symbol_table* symtab,
4271 Sized_relobj<64, true>* object,
4272 unsigned int,
4273 const char* group_section_name,
4274 const char* signature,
4275 const elfcpp::Shdr<64, true>& shdr,
4276 elfcpp::Elf_Word flags,
4277 std::vector<unsigned int>* shndxes);
4278 #endif
4280 #ifdef HAVE_TARGET_32_LITTLE
4281 template
4282 Output_section*
4283 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
4284 const unsigned char* symbols,
4285 off_t symbols_size,
4286 const unsigned char* symbol_names,
4287 off_t symbol_names_size,
4288 unsigned int shndx,
4289 const elfcpp::Shdr<32, false>& shdr,
4290 unsigned int reloc_shndx,
4291 unsigned int reloc_type,
4292 off_t* off);
4293 #endif
4295 #ifdef HAVE_TARGET_32_BIG
4296 template
4297 Output_section*
4298 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
4299 const unsigned char* symbols,
4300 off_t symbols_size,
4301 const unsigned char* symbol_names,
4302 off_t symbol_names_size,
4303 unsigned int shndx,
4304 const elfcpp::Shdr<32, true>& shdr,
4305 unsigned int reloc_shndx,
4306 unsigned int reloc_type,
4307 off_t* off);
4308 #endif
4310 #ifdef HAVE_TARGET_64_LITTLE
4311 template
4312 Output_section*
4313 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
4314 const unsigned char* symbols,
4315 off_t symbols_size,
4316 const unsigned char* symbol_names,
4317 off_t symbol_names_size,
4318 unsigned int shndx,
4319 const elfcpp::Shdr<64, false>& shdr,
4320 unsigned int reloc_shndx,
4321 unsigned int reloc_type,
4322 off_t* off);
4323 #endif
4325 #ifdef HAVE_TARGET_64_BIG
4326 template
4327 Output_section*
4328 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
4329 const unsigned char* symbols,
4330 off_t symbols_size,
4331 const unsigned char* symbol_names,
4332 off_t symbol_names_size,
4333 unsigned int shndx,
4334 const elfcpp::Shdr<64, true>& shdr,
4335 unsigned int reloc_shndx,
4336 unsigned int reloc_type,
4337 off_t* off);
4338 #endif
4340 } // End namespace gold.