1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008 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.
32 #include "libiberty.h"
36 #include "parameters.h"
40 #include "script-sections.h"
45 #include "compressed_output.h"
46 #include "reduced_debug_output.h"
53 // Layout_task_runner methods.
55 // Lay out the sections. This is called after all the input objects
59 Layout_task_runner::run(Workqueue* workqueue, const Task* task)
61 off_t file_size = this->layout_->finalize(this->input_objects_,
66 // Now we know the final size of the output file and we know where
67 // each piece of information goes.
69 if (this->mapfile_ != NULL)
71 this->mapfile_->print_discarded_sections(this->input_objects_);
72 this->layout_->print_to_mapfile(this->mapfile_);
75 Output_file* of = new Output_file(parameters->options().output_file_name());
76 if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
77 of->set_is_temporary();
80 // Queue up the final set of tasks.
81 gold::queue_final_tasks(this->options_, this->input_objects_,
82 this->symtab_, this->layout_, workqueue, of);
87 Layout::Layout(const General_options& options, Script_options* script_options)
89 script_options_(script_options),
97 unattached_section_list_(),
98 sections_are_attached_(false),
99 special_output_list_(),
100 section_headers_(NULL),
102 relro_segment_(NULL),
103 symtab_section_(NULL),
104 symtab_xindex_(NULL),
105 dynsym_section_(NULL),
106 dynsym_xindex_(NULL),
107 dynamic_section_(NULL),
109 eh_frame_section_(NULL),
110 eh_frame_data_(NULL),
111 added_eh_frame_data_(false),
112 eh_frame_hdr_section_(NULL),
113 build_id_note_(NULL),
117 output_file_size_(-1),
118 input_requires_executable_stack_(false),
119 input_with_gnu_stack_note_(false),
120 input_without_gnu_stack_note_(false),
121 has_static_tls_(false),
122 any_postprocessing_sections_(false)
124 // Make space for more than enough segments for a typical file.
125 // This is just for efficiency--it's OK if we wind up needing more.
126 this->segment_list_.reserve(12);
128 // We expect two unattached Output_data objects: the file header and
129 // the segment headers.
130 this->special_output_list_.reserve(2);
133 // Hash a key we use to look up an output section mapping.
136 Layout::Hash_key::operator()(const Layout::Key& k) const
138 return k.first + k.second.first + k.second.second;
141 // Return whether PREFIX is a prefix of STR.
144 is_prefix_of(const char* prefix, const char* str)
146 return strncmp(prefix, str, strlen(prefix)) == 0;
149 // Returns whether the given section is in the list of
150 // debug-sections-used-by-some-version-of-gdb. Currently,
151 // we've checked versions of gdb up to and including 6.7.1.
153 static const char* gdb_sections[] =
155 // ".debug_aranges", // not used by gdb as of 6.7.1
161 // ".debug_pubnames", // not used by gdb as of 6.7.1
166 static const char* lines_only_debug_sections[] =
168 // ".debug_aranges", // not used by gdb as of 6.7.1
174 // ".debug_pubnames", // not used by gdb as of 6.7.1
180 is_gdb_debug_section(const char* str)
182 // We can do this faster: binary search or a hashtable. But why bother?
183 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
184 if (strcmp(str, gdb_sections[i]) == 0)
190 is_lines_only_debug_section(const char* str)
192 // We can do this faster: binary search or a hashtable. But why bother?
194 i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections);
196 if (strcmp(str, lines_only_debug_sections[i]) == 0)
201 // Whether to include this section in the link.
203 template<int size, bool big_endian>
205 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
206 const elfcpp::Shdr<size, big_endian>& shdr)
208 switch (shdr.get_sh_type())
210 case elfcpp::SHT_NULL:
211 case elfcpp::SHT_SYMTAB:
212 case elfcpp::SHT_DYNSYM:
213 case elfcpp::SHT_HASH:
214 case elfcpp::SHT_DYNAMIC:
215 case elfcpp::SHT_SYMTAB_SHNDX:
218 case elfcpp::SHT_STRTAB:
219 // Discard the sections which have special meanings in the ELF
220 // ABI. Keep others (e.g., .stabstr). We could also do this by
221 // checking the sh_link fields of the appropriate sections.
222 return (strcmp(name, ".dynstr") != 0
223 && strcmp(name, ".strtab") != 0
224 && strcmp(name, ".shstrtab") != 0);
226 case elfcpp::SHT_RELA:
227 case elfcpp::SHT_REL:
228 case elfcpp::SHT_GROUP:
229 // If we are emitting relocations these should be handled
231 gold_assert(!parameters->options().relocatable()
232 && !parameters->options().emit_relocs());
235 case elfcpp::SHT_PROGBITS:
236 if (parameters->options().strip_debug()
237 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
239 if (is_debug_info_section(name))
242 if (parameters->options().strip_debug_non_line()
243 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
245 // Debugging sections can only be recognized by name.
246 if (is_prefix_of(".debug", name)
247 && !is_lines_only_debug_section(name))
250 if (parameters->options().strip_debug_gdb()
251 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
253 // Debugging sections can only be recognized by name.
254 if (is_prefix_of(".debug", name)
255 && !is_gdb_debug_section(name))
265 // Return an output section named NAME, or NULL if there is none.
268 Layout::find_output_section(const char* name) const
270 for (Section_list::const_iterator p = this->section_list_.begin();
271 p != this->section_list_.end();
273 if (strcmp((*p)->name(), name) == 0)
278 // Return an output segment of type TYPE, with segment flags SET set
279 // and segment flags CLEAR clear. Return NULL if there is none.
282 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
283 elfcpp::Elf_Word clear) const
285 for (Segment_list::const_iterator p = this->segment_list_.begin();
286 p != this->segment_list_.end();
288 if (static_cast<elfcpp::PT>((*p)->type()) == type
289 && ((*p)->flags() & set) == set
290 && ((*p)->flags() & clear) == 0)
295 // Return the output section to use for section NAME with type TYPE
296 // and section flags FLAGS. NAME must be canonicalized in the string
297 // pool, and NAME_KEY is the key.
300 Layout::get_output_section(const char* name, Stringpool::Key name_key,
301 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
303 elfcpp::Elf_Xword lookup_flags = flags;
305 // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
306 // read-write with read-only sections. Some other ELF linkers do
307 // not do this. FIXME: Perhaps there should be an option
309 lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
311 const Key key(name_key, std::make_pair(type, lookup_flags));
312 const std::pair<Key, Output_section*> v(key, NULL);
313 std::pair<Section_name_map::iterator, bool> ins(
314 this->section_name_map_.insert(v));
317 return ins.first->second;
320 // This is the first time we've seen this name/type/flags
321 // combination. For compatibility with the GNU linker, we
322 // combine sections with contents and zero flags with sections
323 // with non-zero flags. This is a workaround for cases where
324 // assembler code forgets to set section flags. FIXME: Perhaps
325 // there should be an option to control this.
326 Output_section* os = NULL;
328 if (type == elfcpp::SHT_PROGBITS)
332 Output_section* same_name = this->find_output_section(name);
333 if (same_name != NULL
334 && same_name->type() == elfcpp::SHT_PROGBITS
335 && (same_name->flags() & elfcpp::SHF_TLS) == 0)
338 else if ((flags & elfcpp::SHF_TLS) == 0)
340 elfcpp::Elf_Xword zero_flags = 0;
341 const Key zero_key(name_key, std::make_pair(type, zero_flags));
342 Section_name_map::iterator p =
343 this->section_name_map_.find(zero_key);
344 if (p != this->section_name_map_.end())
350 os = this->make_output_section(name, type, flags);
351 ins.first->second = os;
356 // Pick the output section to use for section NAME, in input file
357 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
358 // linker created section. IS_INPUT_SECTION is true if we are
359 // choosing an output section for an input section found in a input
360 // file. This will return NULL if the input section should be
364 Layout::choose_output_section(const Relobj* relobj, const char* name,
365 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
366 bool is_input_section)
368 // We should not see any input sections after we have attached
369 // sections to segments.
370 gold_assert(!is_input_section || !this->sections_are_attached_);
372 // Some flags in the input section should not be automatically
373 // copied to the output section.
374 flags &= ~ (elfcpp::SHF_INFO_LINK
375 | elfcpp::SHF_LINK_ORDER
378 | elfcpp::SHF_STRINGS);
380 if (this->script_options_->saw_sections_clause())
382 // We are using a SECTIONS clause, so the output section is
383 // chosen based only on the name.
385 Script_sections* ss = this->script_options_->script_sections();
386 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
387 Output_section** output_section_slot;
388 name = ss->output_section_name(file_name, name, &output_section_slot);
391 // The SECTIONS clause says to discard this input section.
395 // If this is an orphan section--one not mentioned in the linker
396 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
397 // default processing below.
399 if (output_section_slot != NULL)
401 if (*output_section_slot != NULL)
402 return *output_section_slot;
404 // We don't put sections found in the linker script into
405 // SECTION_NAME_MAP_. That keeps us from getting confused
406 // if an orphan section is mapped to a section with the same
407 // name as one in the linker script.
409 name = this->namepool_.add(name, false, NULL);
411 Output_section* os = this->make_output_section(name, type, flags);
412 os->set_found_in_sections_clause();
413 *output_section_slot = os;
418 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
420 // Turn NAME from the name of the input section into the name of the
423 size_t len = strlen(name);
424 if (is_input_section && !parameters->options().relocatable())
425 name = Layout::output_section_name(name, &len);
427 Stringpool::Key name_key;
428 name = this->namepool_.add_with_length(name, len, true, &name_key);
430 // Find or make the output section. The output section is selected
431 // based on the section name, type, and flags.
432 return this->get_output_section(name, name_key, type, flags);
435 // Return the output section to use for input section SHNDX, with name
436 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
437 // index of a relocation section which applies to this section, or 0
438 // if none, or -1U if more than one. RELOC_TYPE is the type of the
439 // relocation section if there is one. Set *OFF to the offset of this
440 // input section without the output section. Return NULL if the
441 // section should be discarded. Set *OFF to -1 if the section
442 // contents should not be written directly to the output file, but
443 // will instead receive special handling.
445 template<int size, bool big_endian>
447 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
448 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
449 unsigned int reloc_shndx, unsigned int, off_t* off)
453 if (!this->include_section(object, name, shdr))
458 // In a relocatable link a grouped section must not be combined with
459 // any other sections.
460 if (parameters->options().relocatable()
461 && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
463 name = this->namepool_.add(name, true, NULL);
464 os = this->make_output_section(name, shdr.get_sh_type(),
465 shdr.get_sh_flags());
469 os = this->choose_output_section(object, name, shdr.get_sh_type(),
470 shdr.get_sh_flags(), true);
475 // By default the GNU linker sorts input sections whose names match
476 // .ctor.*, .dtor.*, .init_array.*, or .fini_array.*. The sections
477 // are sorted by name. This is used to implement constructor
478 // priority ordering. We are compatible.
479 if (!this->script_options_->saw_sections_clause()
480 && (is_prefix_of(".ctors.", name)
481 || is_prefix_of(".dtors.", name)
482 || is_prefix_of(".init_array.", name)
483 || is_prefix_of(".fini_array.", name)))
484 os->set_must_sort_attached_input_sections();
486 // FIXME: Handle SHF_LINK_ORDER somewhere.
488 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
489 this->script_options_->saw_sections_clause());
494 // Handle a relocation section when doing a relocatable link.
496 template<int size, bool big_endian>
498 Layout::layout_reloc(Sized_relobj<size, big_endian>* object,
500 const elfcpp::Shdr<size, big_endian>& shdr,
501 Output_section* data_section,
502 Relocatable_relocs* rr)
504 gold_assert(parameters->options().relocatable()
505 || parameters->options().emit_relocs());
507 int sh_type = shdr.get_sh_type();
510 if (sh_type == elfcpp::SHT_REL)
512 else if (sh_type == elfcpp::SHT_RELA)
516 name += data_section->name();
518 Output_section* os = this->choose_output_section(object, name.c_str(),
523 os->set_should_link_to_symtab();
524 os->set_info_section(data_section);
526 Output_section_data* posd;
527 if (sh_type == elfcpp::SHT_REL)
529 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
530 posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
534 else if (sh_type == elfcpp::SHT_RELA)
536 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
537 posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
544 os->add_output_section_data(posd);
545 rr->set_output_data(posd);
550 // Handle a group section when doing a relocatable link.
552 template<int size, bool big_endian>
554 Layout::layout_group(Symbol_table* symtab,
555 Sized_relobj<size, big_endian>* object,
557 const char* group_section_name,
558 const char* signature,
559 const elfcpp::Shdr<size, big_endian>& shdr,
560 elfcpp::Elf_Word flags,
561 std::vector<unsigned int>* shndxes)
563 gold_assert(parameters->options().relocatable());
564 gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
565 group_section_name = this->namepool_.add(group_section_name, true, NULL);
566 Output_section* os = this->make_output_section(group_section_name,
568 shdr.get_sh_flags());
570 // We need to find a symbol with the signature in the symbol table.
571 // If we don't find one now, we need to look again later.
572 Symbol* sym = symtab->lookup(signature, NULL);
574 os->set_info_symndx(sym);
577 // We will wind up using a symbol whose name is the signature.
578 // So just put the signature in the symbol name pool to save it.
579 signature = symtab->canonicalize_name(signature);
580 this->group_signatures_.push_back(Group_signature(os, signature));
583 os->set_should_link_to_symtab();
586 section_size_type entry_count =
587 convert_to_section_size_type(shdr.get_sh_size() / 4);
588 Output_section_data* posd =
589 new Output_data_group<size, big_endian>(object, entry_count, flags,
591 os->add_output_section_data(posd);
594 // Special GNU handling of sections name .eh_frame. They will
595 // normally hold exception frame data as defined by the C++ ABI
596 // (http://codesourcery.com/cxx-abi/).
598 template<int size, bool big_endian>
600 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
601 const unsigned char* symbols,
603 const unsigned char* symbol_names,
604 off_t symbol_names_size,
606 const elfcpp::Shdr<size, big_endian>& shdr,
607 unsigned int reloc_shndx, unsigned int reloc_type,
610 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
611 gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
613 const char* const name = ".eh_frame";
614 Output_section* os = this->choose_output_section(object,
616 elfcpp::SHT_PROGBITS,
622 if (this->eh_frame_section_ == NULL)
624 this->eh_frame_section_ = os;
625 this->eh_frame_data_ = new Eh_frame();
627 if (this->options_.eh_frame_hdr())
629 Output_section* hdr_os =
630 this->choose_output_section(NULL,
632 elfcpp::SHT_PROGBITS,
638 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
639 this->eh_frame_data_);
640 hdr_os->add_output_section_data(hdr_posd);
642 hdr_os->set_after_input_sections();
644 if (!this->script_options_->saw_phdrs_clause())
646 Output_segment* hdr_oseg;
647 hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
649 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
652 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
657 gold_assert(this->eh_frame_section_ == os);
659 if (this->eh_frame_data_->add_ehframe_input_section(object,
668 os->update_flags_for_input_section(shdr.get_sh_flags());
670 // We found a .eh_frame section we are going to optimize, so now
671 // we can add the set of optimized sections to the output
672 // section. We need to postpone adding this until we've found a
673 // section we can optimize so that the .eh_frame section in
674 // crtbegin.o winds up at the start of the output section.
675 if (!this->added_eh_frame_data_)
677 os->add_output_section_data(this->eh_frame_data_);
678 this->added_eh_frame_data_ = true;
684 // We couldn't handle this .eh_frame section for some reason.
685 // Add it as a normal section.
686 bool saw_sections_clause = this->script_options_->saw_sections_clause();
687 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
688 saw_sections_clause);
694 // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
695 // the output section.
698 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
699 elfcpp::Elf_Xword flags,
700 Output_section_data* posd)
702 Output_section* os = this->choose_output_section(NULL, name, type, flags,
705 os->add_output_section_data(posd);
709 // Map section flags to segment flags.
712 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
714 elfcpp::Elf_Word ret = elfcpp::PF_R;
715 if ((flags & elfcpp::SHF_WRITE) != 0)
717 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
722 // Sometimes we compress sections. This is typically done for
723 // sections that are not part of normal program execution (such as
724 // .debug_* sections), and where the readers of these sections know
725 // how to deal with compressed sections. (To make it easier for them,
726 // we will rename the ouput section in such cases from .foo to
727 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
728 // doesn't say for certain whether we'll compress -- it depends on
729 // commandline options as well -- just whether this section is a
730 // candidate for compression.
733 is_compressible_debug_section(const char* secname)
735 return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
738 // Make a new Output_section, and attach it to segments as
742 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
743 elfcpp::Elf_Xword flags)
746 if ((flags & elfcpp::SHF_ALLOC) == 0
747 && strcmp(this->options_.compress_debug_sections(), "none") != 0
748 && is_compressible_debug_section(name))
749 os = new Output_compressed_section(&this->options_, name, type, flags);
751 else if ((flags & elfcpp::SHF_ALLOC) == 0
752 && this->options_.strip_debug_non_line()
753 && strcmp(".debug_abbrev", name) == 0)
755 os = this->debug_abbrev_ = new Output_reduced_debug_abbrev_section(
757 if (this->debug_info_)
758 this->debug_info_->set_abbreviations(this->debug_abbrev_);
760 else if ((flags & elfcpp::SHF_ALLOC) == 0
761 && this->options_.strip_debug_non_line()
762 && strcmp(".debug_info", name) == 0)
764 os = this->debug_info_ = new Output_reduced_debug_info_section(
766 if (this->debug_abbrev_)
767 this->debug_info_->set_abbreviations(this->debug_abbrev_);
770 os = new Output_section(name, type, flags);
772 this->section_list_.push_back(os);
774 // The GNU linker by default sorts some sections by priority, so we
775 // do the same. We need to know that this might happen before we
776 // attach any input sections.
777 if (!this->script_options_->saw_sections_clause()
778 && (strcmp(name, ".ctors") == 0
779 || strcmp(name, ".dtors") == 0
780 || strcmp(name, ".init_array") == 0
781 || strcmp(name, ".fini_array") == 0))
782 os->set_may_sort_attached_input_sections();
784 // With -z relro, we have to recognize the special sections by name.
785 // There is no other way.
786 if (!this->script_options_->saw_sections_clause()
787 && parameters->options().relro()
788 && type == elfcpp::SHT_PROGBITS
789 && (flags & elfcpp::SHF_ALLOC) != 0
790 && (flags & elfcpp::SHF_WRITE) != 0)
792 if (strcmp(name, ".data.rel.ro") == 0)
794 else if (strcmp(name, ".data.rel.ro.local") == 0)
797 os->set_is_relro_local();
801 // If we have already attached the sections to segments, then we
802 // need to attach this one now. This happens for sections created
803 // directly by the linker.
804 if (this->sections_are_attached_)
805 this->attach_section_to_segment(os);
810 // Attach output sections to segments. This is called after we have
811 // seen all the input sections.
814 Layout::attach_sections_to_segments()
816 for (Section_list::iterator p = this->section_list_.begin();
817 p != this->section_list_.end();
819 this->attach_section_to_segment(*p);
821 this->sections_are_attached_ = true;
824 // Attach an output section to a segment.
827 Layout::attach_section_to_segment(Output_section* os)
829 if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
830 this->unattached_section_list_.push_back(os);
832 this->attach_allocated_section_to_segment(os);
835 // Attach an allocated output section to a segment.
838 Layout::attach_allocated_section_to_segment(Output_section* os)
840 elfcpp::Elf_Xword flags = os->flags();
841 gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
843 if (parameters->options().relocatable())
846 // If we have a SECTIONS clause, we can't handle the attachment to
847 // segments until after we've seen all the sections.
848 if (this->script_options_->saw_sections_clause())
851 gold_assert(!this->script_options_->saw_phdrs_clause());
853 // This output section goes into a PT_LOAD segment.
855 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
857 // In general the only thing we really care about for PT_LOAD
858 // segments is whether or not they are writable, so that is how we
859 // search for them. People who need segments sorted on some other
860 // basis will have to use a linker script.
862 Segment_list::const_iterator p;
863 for (p = this->segment_list_.begin();
864 p != this->segment_list_.end();
867 if ((*p)->type() == elfcpp::PT_LOAD
868 && (parameters->options().omagic()
869 || ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W)))
871 // If -Tbss was specified, we need to separate the data
873 if (this->options_.user_set_Tbss())
875 if ((os->type() == elfcpp::SHT_NOBITS)
876 == (*p)->has_any_data_sections())
880 (*p)->add_output_section(os, seg_flags);
885 if (p == this->segment_list_.end())
887 Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
889 oseg->add_output_section(os, seg_flags);
892 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
894 if (os->type() == elfcpp::SHT_NOTE)
896 // See if we already have an equivalent PT_NOTE segment.
897 for (p = this->segment_list_.begin();
898 p != segment_list_.end();
901 if ((*p)->type() == elfcpp::PT_NOTE
902 && (((*p)->flags() & elfcpp::PF_W)
903 == (seg_flags & elfcpp::PF_W)))
905 (*p)->add_output_section(os, seg_flags);
910 if (p == this->segment_list_.end())
912 Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
914 oseg->add_output_section(os, seg_flags);
918 // If we see a loadable SHF_TLS section, we create a PT_TLS
919 // segment. There can only be one such segment.
920 if ((flags & elfcpp::SHF_TLS) != 0)
922 if (this->tls_segment_ == NULL)
923 this->tls_segment_ = this->make_output_segment(elfcpp::PT_TLS,
925 this->tls_segment_->add_output_section(os, seg_flags);
928 // If -z relro is in effect, and we see a relro section, we create a
929 // PT_GNU_RELRO segment. There can only be one such segment.
930 if (os->is_relro() && parameters->options().relro())
932 gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W));
933 if (this->relro_segment_ == NULL)
934 this->relro_segment_ = this->make_output_segment(elfcpp::PT_GNU_RELRO,
936 this->relro_segment_->add_output_section(os, seg_flags);
940 // Make an output section for a script.
943 Layout::make_output_section_for_script(const char* name)
945 name = this->namepool_.add(name, false, NULL);
946 Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
948 os->set_found_in_sections_clause();
952 // Return the number of segments we expect to see.
955 Layout::expected_segment_count() const
957 size_t ret = this->segment_list_.size();
959 // If we didn't see a SECTIONS clause in a linker script, we should
960 // already have the complete list of segments. Otherwise we ask the
961 // SECTIONS clause how many segments it expects, and add in the ones
962 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
964 if (!this->script_options_->saw_sections_clause())
968 const Script_sections* ss = this->script_options_->script_sections();
969 return ret + ss->expected_segment_count(this);
973 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
974 // is whether we saw a .note.GNU-stack section in the object file.
975 // GNU_STACK_FLAGS is the section flags. The flags give the
976 // protection required for stack memory. We record this in an
977 // executable as a PT_GNU_STACK segment. If an object file does not
978 // have a .note.GNU-stack segment, we must assume that it is an old
979 // object. On some targets that will force an executable stack.
982 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
985 this->input_without_gnu_stack_note_ = true;
988 this->input_with_gnu_stack_note_ = true;
989 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
990 this->input_requires_executable_stack_ = true;
994 // Create the dynamic sections which are needed before we read the
998 Layout::create_initial_dynamic_sections(Symbol_table* symtab)
1000 if (parameters->doing_static_link())
1003 this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
1004 elfcpp::SHT_DYNAMIC,
1006 | elfcpp::SHF_WRITE),
1008 this->dynamic_section_->set_is_relro();
1010 symtab->define_in_output_data("_DYNAMIC", NULL, this->dynamic_section_, 0, 0,
1011 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
1012 elfcpp::STV_HIDDEN, 0, false, false);
1014 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
1016 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
1019 // For each output section whose name can be represented as C symbol,
1020 // define __start and __stop symbols for the section. This is a GNU
1024 Layout::define_section_symbols(Symbol_table* symtab)
1026 for (Section_list::const_iterator p = this->section_list_.begin();
1027 p != this->section_list_.end();
1030 const char* const name = (*p)->name();
1031 if (name[strspn(name,
1033 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
1034 "abcdefghijklmnopqrstuvwxyz"
1038 const std::string name_string(name);
1039 const std::string start_name("__start_" + name_string);
1040 const std::string stop_name("__stop_" + name_string);
1042 symtab->define_in_output_data(start_name.c_str(),
1049 elfcpp::STV_DEFAULT,
1051 false, // offset_is_from_end
1052 true); // only_if_ref
1054 symtab->define_in_output_data(stop_name.c_str(),
1061 elfcpp::STV_DEFAULT,
1063 true, // offset_is_from_end
1064 true); // only_if_ref
1069 // Define symbols for group signatures.
1072 Layout::define_group_signatures(Symbol_table* symtab)
1074 for (Group_signatures::iterator p = this->group_signatures_.begin();
1075 p != this->group_signatures_.end();
1078 Symbol* sym = symtab->lookup(p->signature, NULL);
1080 p->section->set_info_symndx(sym);
1083 // Force the name of the group section to the group
1084 // signature, and use the group's section symbol as the
1085 // signature symbol.
1086 if (strcmp(p->section->name(), p->signature) != 0)
1088 const char* name = this->namepool_.add(p->signature,
1090 p->section->set_name(name);
1092 p->section->set_needs_symtab_index();
1093 p->section->set_info_section_symndx(p->section);
1097 this->group_signatures_.clear();
1100 // Find the first read-only PT_LOAD segment, creating one if
1104 Layout::find_first_load_seg()
1106 for (Segment_list::const_iterator p = this->segment_list_.begin();
1107 p != this->segment_list_.end();
1110 if ((*p)->type() == elfcpp::PT_LOAD
1111 && ((*p)->flags() & elfcpp::PF_R) != 0
1112 && (parameters->options().omagic()
1113 || ((*p)->flags() & elfcpp::PF_W) == 0))
1117 gold_assert(!this->script_options_->saw_phdrs_clause());
1119 Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
1124 // Finalize the layout. When this is called, we have created all the
1125 // output sections and all the output segments which are based on
1126 // input sections. We have several things to do, and we have to do
1127 // them in the right order, so that we get the right results correctly
1130 // 1) Finalize the list of output segments and create the segment
1133 // 2) Finalize the dynamic symbol table and associated sections.
1135 // 3) Determine the final file offset of all the output segments.
1137 // 4) Determine the final file offset of all the SHF_ALLOC output
1140 // 5) Create the symbol table sections and the section name table
1143 // 6) Finalize the symbol table: set symbol values to their final
1144 // value and make a final determination of which symbols are going
1145 // into the output symbol table.
1147 // 7) Create the section table header.
1149 // 8) Determine the final file offset of all the output sections which
1150 // are not SHF_ALLOC, including the section table header.
1152 // 9) Finalize the ELF file header.
1154 // This function returns the size of the output file.
1157 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
1158 Target* target, const Task* task)
1160 target->finalize_sections(this);
1162 this->count_local_symbols(task, input_objects);
1164 this->create_gold_note();
1165 this->create_executable_stack_info(target);
1166 this->create_build_id();
1168 Output_segment* phdr_seg = NULL;
1169 if (!parameters->options().relocatable() && !parameters->doing_static_link())
1171 // There was a dynamic object in the link. We need to create
1172 // some information for the dynamic linker.
1174 // Create the PT_PHDR segment which will hold the program
1176 if (!this->script_options_->saw_phdrs_clause())
1177 phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
1179 // Create the dynamic symbol table, including the hash table.
1180 Output_section* dynstr;
1181 std::vector<Symbol*> dynamic_symbols;
1182 unsigned int local_dynamic_count;
1183 Versions versions(*this->script_options()->version_script_info(),
1185 this->create_dynamic_symtab(input_objects, symtab, &dynstr,
1186 &local_dynamic_count, &dynamic_symbols,
1189 // Create the .interp section to hold the name of the
1190 // interpreter, and put it in a PT_INTERP segment.
1191 if (!parameters->options().shared())
1192 this->create_interp(target);
1194 // Finish the .dynamic section to hold the dynamic data, and put
1195 // it in a PT_DYNAMIC segment.
1196 this->finish_dynamic_section(input_objects, symtab);
1198 // We should have added everything we need to the dynamic string
1200 this->dynpool_.set_string_offsets();
1202 // Create the version sections. We can't do this until the
1203 // dynamic string table is complete.
1204 this->create_version_sections(&versions, symtab, local_dynamic_count,
1205 dynamic_symbols, dynstr);
1208 // If there is a SECTIONS clause, put all the input sections into
1209 // the required order.
1210 Output_segment* load_seg;
1211 if (this->script_options_->saw_sections_clause())
1212 load_seg = this->set_section_addresses_from_script(symtab);
1213 else if (parameters->options().relocatable())
1216 load_seg = this->find_first_load_seg();
1218 if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
1221 gold_assert(phdr_seg == NULL || load_seg != NULL);
1223 // Lay out the segment headers.
1224 Output_segment_headers* segment_headers;
1225 if (parameters->options().relocatable())
1226 segment_headers = NULL;
1229 segment_headers = new Output_segment_headers(this->segment_list_);
1230 if (load_seg != NULL)
1231 load_seg->add_initial_output_data(segment_headers);
1232 if (phdr_seg != NULL)
1233 phdr_seg->add_initial_output_data(segment_headers);
1236 // Lay out the file header.
1237 Output_file_header* file_header;
1238 file_header = new Output_file_header(target, symtab, segment_headers,
1239 this->options_.entry());
1240 if (load_seg != NULL)
1241 load_seg->add_initial_output_data(file_header);
1243 this->special_output_list_.push_back(file_header);
1244 if (segment_headers != NULL)
1245 this->special_output_list_.push_back(segment_headers);
1247 if (this->script_options_->saw_phdrs_clause()
1248 && !parameters->options().relocatable())
1250 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
1251 // clause in a linker script.
1252 Script_sections* ss = this->script_options_->script_sections();
1253 ss->put_headers_in_phdrs(file_header, segment_headers);
1256 // We set the output section indexes in set_segment_offsets and
1257 // set_section_indexes.
1258 unsigned int shndx = 1;
1260 // Set the file offsets of all the segments, and all the sections
1263 if (!parameters->options().relocatable())
1264 off = this->set_segment_offsets(target, load_seg, &shndx);
1266 off = this->set_relocatable_section_offsets(file_header, &shndx);
1268 // Set the file offsets of all the non-data sections we've seen so
1269 // far which don't have to wait for the input sections. We need
1270 // this in order to finalize local symbols in non-allocated
1272 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1274 // Set the section indexes of all unallocated sections seen so far,
1275 // in case any of them are somehow referenced by a symbol.
1276 shndx = this->set_section_indexes(shndx);
1278 // Create the symbol table sections.
1279 this->create_symtab_sections(input_objects, symtab, shndx, &off);
1280 if (!parameters->doing_static_link())
1281 this->assign_local_dynsym_offsets(input_objects);
1283 // Process any symbol assignments from a linker script. This must
1284 // be called after the symbol table has been finalized.
1285 this->script_options_->finalize_symbols(symtab, this);
1287 // Create the .shstrtab section.
1288 Output_section* shstrtab_section = this->create_shstrtab();
1290 // Set the file offsets of the rest of the non-data sections which
1291 // don't have to wait for the input sections.
1292 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
1294 // Now that all sections have been created, set the section indexes
1295 // for any sections which haven't been done yet.
1296 shndx = this->set_section_indexes(shndx);
1298 // Create the section table header.
1299 this->create_shdrs(shstrtab_section, &off);
1301 // If there are no sections which require postprocessing, we can
1302 // handle the section names now, and avoid a resize later.
1303 if (!this->any_postprocessing_sections_)
1304 off = this->set_section_offsets(off,
1305 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
1307 file_header->set_section_info(this->section_headers_, shstrtab_section);
1309 // Now we know exactly where everything goes in the output file
1310 // (except for non-allocated sections which require postprocessing).
1311 Output_data::layout_complete();
1313 this->output_file_size_ = off;
1318 // Create a note header following the format defined in the ELF ABI.
1319 // NAME is the name, NOTE_TYPE is the type, DESCSZ is the size of the
1320 // descriptor. ALLOCATE is true if the section should be allocated in
1321 // memory. This returns the new note section. It sets
1322 // *TRAILING_PADDING to the number of trailing zero bytes required.
1325 Layout::create_note(const char* name, int note_type, size_t descsz,
1326 bool allocate, size_t* trailing_padding)
1328 // Authorities all agree that the values in a .note field should
1329 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1330 // they differ on what the alignment is for 64-bit binaries.
1331 // The GABI says unambiguously they take 8-byte alignment:
1332 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1333 // Other documentation says alignment should always be 4 bytes:
1334 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1335 // GNU ld and GNU readelf both support the latter (at least as of
1336 // version 2.16.91), and glibc always generates the latter for
1337 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1339 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1340 const int size = parameters->target().get_size();
1342 const int size = 32;
1345 // The contents of the .note section.
1346 size_t namesz = strlen(name) + 1;
1347 size_t aligned_namesz = align_address(namesz, size / 8);
1348 size_t aligned_descsz = align_address(descsz, size / 8);
1350 size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
1352 unsigned char* buffer = new unsigned char[notehdrsz];
1353 memset(buffer, 0, notehdrsz);
1355 bool is_big_endian = parameters->target().is_big_endian();
1361 elfcpp::Swap<32, false>::writeval(buffer, namesz);
1362 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
1363 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
1367 elfcpp::Swap<32, true>::writeval(buffer, namesz);
1368 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
1369 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
1372 else if (size == 64)
1376 elfcpp::Swap<64, false>::writeval(buffer, namesz);
1377 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
1378 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
1382 elfcpp::Swap<64, true>::writeval(buffer, namesz);
1383 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
1384 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
1390 memcpy(buffer + 3 * (size / 8), name, namesz);
1392 const char* note_name = this->namepool_.add(".note", false, NULL);
1393 elfcpp::Elf_Xword flags = 0;
1395 flags = elfcpp::SHF_ALLOC;
1396 Output_section* os = this->make_output_section(note_name,
1399 Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
1402 os->add_output_section_data(posd);
1404 *trailing_padding = aligned_descsz - descsz;
1409 // For an executable or shared library, create a note to record the
1410 // version of gold used to create the binary.
1413 Layout::create_gold_note()
1415 if (parameters->options().relocatable())
1418 std::string desc = std::string("gold ") + gold::get_version_string();
1420 size_t trailing_padding;
1421 Output_section *os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
1422 desc.size(), false, &trailing_padding);
1424 Output_section_data* posd = new Output_data_const(desc, 4);
1425 os->add_output_section_data(posd);
1427 if (trailing_padding > 0)
1429 posd = new Output_data_zero_fill(trailing_padding, 0);
1430 os->add_output_section_data(posd);
1434 // Record whether the stack should be executable. This can be set
1435 // from the command line using the -z execstack or -z noexecstack
1436 // options. Otherwise, if any input file has a .note.GNU-stack
1437 // section with the SHF_EXECINSTR flag set, the stack should be
1438 // executable. Otherwise, if at least one input file a
1439 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1440 // section, we use the target default for whether the stack should be
1441 // executable. Otherwise, we don't generate a stack note. When
1442 // generating a object file, we create a .note.GNU-stack section with
1443 // the appropriate marking. When generating an executable or shared
1444 // library, we create a PT_GNU_STACK segment.
1447 Layout::create_executable_stack_info(const Target* target)
1449 bool is_stack_executable;
1450 if (this->options_.is_execstack_set())
1451 is_stack_executable = this->options_.is_stack_executable();
1452 else if (!this->input_with_gnu_stack_note_)
1456 if (this->input_requires_executable_stack_)
1457 is_stack_executable = true;
1458 else if (this->input_without_gnu_stack_note_)
1459 is_stack_executable = target->is_default_stack_executable();
1461 is_stack_executable = false;
1464 if (parameters->options().relocatable())
1466 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
1467 elfcpp::Elf_Xword flags = 0;
1468 if (is_stack_executable)
1469 flags |= elfcpp::SHF_EXECINSTR;
1470 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
1474 if (this->script_options_->saw_phdrs_clause())
1476 int flags = elfcpp::PF_R | elfcpp::PF_W;
1477 if (is_stack_executable)
1478 flags |= elfcpp::PF_X;
1479 this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
1483 // If --build-id was used, set up the build ID note.
1486 Layout::create_build_id()
1488 if (!parameters->options().user_set_build_id())
1491 const char* style = parameters->options().build_id();
1492 if (strcmp(style, "none") == 0)
1495 // Set DESCSZ to the size of the note descriptor. When possible,
1496 // set DESC to the note descriptor contents.
1499 if (strcmp(style, "md5") == 0)
1501 else if (strcmp(style, "sha1") == 0)
1503 else if (strcmp(style, "uuid") == 0)
1505 const size_t uuidsz = 128 / 8;
1507 char buffer[uuidsz];
1508 memset(buffer, 0, uuidsz);
1510 int descriptor = ::open("/dev/urandom", O_RDONLY);
1512 gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
1516 ssize_t got = ::read(descriptor, buffer, uuidsz);
1517 ::close(descriptor);
1519 gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
1520 else if (static_cast<size_t>(got) != uuidsz)
1521 gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
1525 desc.assign(buffer, uuidsz);
1528 else if (strncmp(style, "0x", 2) == 0)
1531 const char* p = style + 2;
1534 if (hex_p(p[0]) && hex_p(p[1]))
1536 char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
1540 else if (*p == '-' || *p == ':')
1543 gold_fatal(_("--build-id argument '%s' not a valid hex number"),
1546 descsz = desc.size();
1549 gold_fatal(_("unrecognized --build-id argument '%s'"), style);
1552 size_t trailing_padding;
1553 Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
1554 descsz, true, &trailing_padding);
1558 // We know the value already, so we fill it in now.
1559 gold_assert(desc.size() == descsz);
1561 Output_section_data* posd = new Output_data_const(desc, 4);
1562 os->add_output_section_data(posd);
1564 if (trailing_padding != 0)
1566 posd = new Output_data_zero_fill(trailing_padding, 0);
1567 os->add_output_section_data(posd);
1572 // We need to compute a checksum after we have completed the
1574 gold_assert(trailing_padding == 0);
1575 this->build_id_note_ = new Output_data_zero_fill(descsz, 4);
1576 os->add_output_section_data(this->build_id_note_);
1577 os->set_after_input_sections();
1581 // Return whether SEG1 should be before SEG2 in the output file. This
1582 // is based entirely on the segment type and flags. When this is
1583 // called the segment addresses has normally not yet been set.
1586 Layout::segment_precedes(const Output_segment* seg1,
1587 const Output_segment* seg2)
1589 elfcpp::Elf_Word type1 = seg1->type();
1590 elfcpp::Elf_Word type2 = seg2->type();
1592 // The single PT_PHDR segment is required to precede any loadable
1593 // segment. We simply make it always first.
1594 if (type1 == elfcpp::PT_PHDR)
1596 gold_assert(type2 != elfcpp::PT_PHDR);
1599 if (type2 == elfcpp::PT_PHDR)
1602 // The single PT_INTERP segment is required to precede any loadable
1603 // segment. We simply make it always second.
1604 if (type1 == elfcpp::PT_INTERP)
1606 gold_assert(type2 != elfcpp::PT_INTERP);
1609 if (type2 == elfcpp::PT_INTERP)
1612 // We then put PT_LOAD segments before any other segments.
1613 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
1615 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
1618 // We put the PT_TLS segment last except for the PT_GNU_RELRO
1619 // segment, because that is where the dynamic linker expects to find
1620 // it (this is just for efficiency; other positions would also work
1622 if (type1 == elfcpp::PT_TLS
1623 && type2 != elfcpp::PT_TLS
1624 && type2 != elfcpp::PT_GNU_RELRO)
1626 if (type2 == elfcpp::PT_TLS
1627 && type1 != elfcpp::PT_TLS
1628 && type1 != elfcpp::PT_GNU_RELRO)
1631 // We put the PT_GNU_RELRO segment last, because that is where the
1632 // dynamic linker expects to find it (as with PT_TLS, this is just
1634 if (type1 == elfcpp::PT_GNU_RELRO && type2 != elfcpp::PT_GNU_RELRO)
1636 if (type2 == elfcpp::PT_GNU_RELRO && type1 != elfcpp::PT_GNU_RELRO)
1639 const elfcpp::Elf_Word flags1 = seg1->flags();
1640 const elfcpp::Elf_Word flags2 = seg2->flags();
1642 // The order of non-PT_LOAD segments is unimportant. We simply sort
1643 // by the numeric segment type and flags values. There should not
1644 // be more than one segment with the same type and flags.
1645 if (type1 != elfcpp::PT_LOAD)
1648 return type1 < type2;
1649 gold_assert(flags1 != flags2);
1650 return flags1 < flags2;
1653 // If the addresses are set already, sort by load address.
1654 if (seg1->are_addresses_set())
1656 if (!seg2->are_addresses_set())
1659 unsigned int section_count1 = seg1->output_section_count();
1660 unsigned int section_count2 = seg2->output_section_count();
1661 if (section_count1 == 0 && section_count2 > 0)
1663 if (section_count1 > 0 && section_count2 == 0)
1666 uint64_t paddr1 = seg1->first_section_load_address();
1667 uint64_t paddr2 = seg2->first_section_load_address();
1668 if (paddr1 != paddr2)
1669 return paddr1 < paddr2;
1671 else if (seg2->are_addresses_set())
1674 // We sort PT_LOAD segments based on the flags. Readonly segments
1675 // come before writable segments. Then writable segments with data
1676 // come before writable segments without data. Then executable
1677 // segments come before non-executable segments. Then the unlikely
1678 // case of a non-readable segment comes before the normal case of a
1679 // readable segment. If there are multiple segments with the same
1680 // type and flags, we require that the address be set, and we sort
1681 // by virtual address and then physical address.
1682 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
1683 return (flags1 & elfcpp::PF_W) == 0;
1684 if ((flags1 & elfcpp::PF_W) != 0
1685 && seg1->has_any_data_sections() != seg2->has_any_data_sections())
1686 return seg1->has_any_data_sections();
1687 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
1688 return (flags1 & elfcpp::PF_X) != 0;
1689 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
1690 return (flags1 & elfcpp::PF_R) == 0;
1692 // We shouldn't get here--we shouldn't create segments which we
1693 // can't distinguish.
1697 // Set the file offsets of all the segments, and all the sections they
1698 // contain. They have all been created. LOAD_SEG must be be laid out
1699 // first. Return the offset of the data to follow.
1702 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
1703 unsigned int *pshndx)
1705 // Sort them into the final order.
1706 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
1707 Layout::Compare_segments());
1709 // Find the PT_LOAD segments, and set their addresses and offsets
1710 // and their section's addresses and offsets.
1712 if (this->options_.user_set_Ttext())
1713 addr = this->options_.Ttext();
1714 else if (parameters->options().shared())
1717 addr = target->default_text_segment_address();
1720 // If LOAD_SEG is NULL, then the file header and segment headers
1721 // will not be loadable. But they still need to be at offset 0 in
1722 // the file. Set their offsets now.
1723 if (load_seg == NULL)
1725 for (Data_list::iterator p = this->special_output_list_.begin();
1726 p != this->special_output_list_.end();
1729 off = align_address(off, (*p)->addralign());
1730 (*p)->set_address_and_file_offset(0, off);
1731 off += (*p)->data_size();
1735 const bool check_sections = parameters->options().check_sections();
1736 Output_segment* last_load_segment = NULL;
1738 bool was_readonly = false;
1739 for (Segment_list::iterator p = this->segment_list_.begin();
1740 p != this->segment_list_.end();
1743 if ((*p)->type() == elfcpp::PT_LOAD)
1745 if (load_seg != NULL && load_seg != *p)
1749 bool are_addresses_set = (*p)->are_addresses_set();
1750 if (are_addresses_set)
1752 // When it comes to setting file offsets, we care about
1753 // the physical address.
1754 addr = (*p)->paddr();
1756 else if (this->options_.user_set_Tdata()
1757 && ((*p)->flags() & elfcpp::PF_W) != 0
1758 && (!this->options_.user_set_Tbss()
1759 || (*p)->has_any_data_sections()))
1761 addr = this->options_.Tdata();
1762 are_addresses_set = true;
1764 else if (this->options_.user_set_Tbss()
1765 && ((*p)->flags() & elfcpp::PF_W) != 0
1766 && !(*p)->has_any_data_sections())
1768 addr = this->options_.Tbss();
1769 are_addresses_set = true;
1772 uint64_t orig_addr = addr;
1773 uint64_t orig_off = off;
1775 uint64_t aligned_addr = 0;
1776 uint64_t abi_pagesize = target->abi_pagesize();
1777 uint64_t common_pagesize = target->common_pagesize();
1779 if (!parameters->options().nmagic()
1780 && !parameters->options().omagic())
1781 (*p)->set_minimum_p_align(common_pagesize);
1783 if (are_addresses_set)
1785 if (!parameters->options().nmagic()
1786 && !parameters->options().omagic())
1788 // Adjust the file offset to the same address modulo
1790 uint64_t unsigned_off = off;
1791 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
1792 | (addr & (abi_pagesize - 1)));
1793 if (aligned_off < unsigned_off)
1794 aligned_off += abi_pagesize;
1800 // If the last segment was readonly, and this one is
1801 // not, then skip the address forward one page,
1802 // maintaining the same position within the page. This
1803 // lets us store both segments overlapping on a single
1804 // page in the file, but the loader will put them on
1805 // different pages in memory.
1807 addr = align_address(addr, (*p)->maximum_alignment());
1808 aligned_addr = addr;
1810 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
1812 if ((addr & (abi_pagesize - 1)) != 0)
1813 addr = addr + abi_pagesize;
1816 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1819 unsigned int shndx_hold = *pshndx;
1820 uint64_t new_addr = (*p)->set_section_addresses(this, false, addr,
1823 // Now that we know the size of this segment, we may be able
1824 // to save a page in memory, at the cost of wasting some
1825 // file space, by instead aligning to the start of a new
1826 // page. Here we use the real machine page size rather than
1827 // the ABI mandated page size.
1829 if (!are_addresses_set && aligned_addr != addr)
1831 uint64_t first_off = (common_pagesize
1833 & (common_pagesize - 1)));
1834 uint64_t last_off = new_addr & (common_pagesize - 1);
1837 && ((aligned_addr & ~ (common_pagesize - 1))
1838 != (new_addr & ~ (common_pagesize - 1)))
1839 && first_off + last_off <= common_pagesize)
1841 *pshndx = shndx_hold;
1842 addr = align_address(aligned_addr, common_pagesize);
1843 addr = align_address(addr, (*p)->maximum_alignment());
1844 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1845 new_addr = (*p)->set_section_addresses(this, true, addr,
1852 if (((*p)->flags() & elfcpp::PF_W) == 0)
1853 was_readonly = true;
1855 // Implement --check-sections. We know that the segments
1856 // are sorted by LMA.
1857 if (check_sections && last_load_segment != NULL)
1859 gold_assert(last_load_segment->paddr() <= (*p)->paddr());
1860 if (last_load_segment->paddr() + last_load_segment->memsz()
1863 unsigned long long lb1 = last_load_segment->paddr();
1864 unsigned long long le1 = lb1 + last_load_segment->memsz();
1865 unsigned long long lb2 = (*p)->paddr();
1866 unsigned long long le2 = lb2 + (*p)->memsz();
1867 gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
1868 "[0x%llx -> 0x%llx]"),
1869 lb1, le1, lb2, le2);
1872 last_load_segment = *p;
1876 // Handle the non-PT_LOAD segments, setting their offsets from their
1877 // section's offsets.
1878 for (Segment_list::iterator p = this->segment_list_.begin();
1879 p != this->segment_list_.end();
1882 if ((*p)->type() != elfcpp::PT_LOAD)
1886 // Set the TLS offsets for each section in the PT_TLS segment.
1887 if (this->tls_segment_ != NULL)
1888 this->tls_segment_->set_tls_offsets();
1893 // Set the offsets of all the allocated sections when doing a
1894 // relocatable link. This does the same jobs as set_segment_offsets,
1895 // only for a relocatable link.
1898 Layout::set_relocatable_section_offsets(Output_data* file_header,
1899 unsigned int *pshndx)
1903 file_header->set_address_and_file_offset(0, 0);
1904 off += file_header->data_size();
1906 for (Section_list::iterator p = this->section_list_.begin();
1907 p != this->section_list_.end();
1910 // We skip unallocated sections here, except that group sections
1911 // have to come first.
1912 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
1913 && (*p)->type() != elfcpp::SHT_GROUP)
1916 off = align_address(off, (*p)->addralign());
1918 // The linker script might have set the address.
1919 if (!(*p)->is_address_valid())
1920 (*p)->set_address(0);
1921 (*p)->set_file_offset(off);
1922 (*p)->finalize_data_size();
1923 off += (*p)->data_size();
1925 (*p)->set_out_shndx(*pshndx);
1932 // Set the file offset of all the sections not associated with a
1936 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
1938 for (Section_list::iterator p = this->unattached_section_list_.begin();
1939 p != this->unattached_section_list_.end();
1942 // The symtab section is handled in create_symtab_sections.
1943 if (*p == this->symtab_section_)
1946 // If we've already set the data size, don't set it again.
1947 if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
1950 if (pass == BEFORE_INPUT_SECTIONS_PASS
1951 && (*p)->requires_postprocessing())
1953 (*p)->create_postprocessing_buffer();
1954 this->any_postprocessing_sections_ = true;
1957 if (pass == BEFORE_INPUT_SECTIONS_PASS
1958 && (*p)->after_input_sections())
1960 else if (pass == POSTPROCESSING_SECTIONS_PASS
1961 && (!(*p)->after_input_sections()
1962 || (*p)->type() == elfcpp::SHT_STRTAB))
1964 else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1965 && (!(*p)->after_input_sections()
1966 || (*p)->type() != elfcpp::SHT_STRTAB))
1969 off = align_address(off, (*p)->addralign());
1970 (*p)->set_file_offset(off);
1971 (*p)->finalize_data_size();
1972 off += (*p)->data_size();
1974 // At this point the name must be set.
1975 if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
1976 this->namepool_.add((*p)->name(), false, NULL);
1981 // Set the section indexes of all the sections not associated with a
1985 Layout::set_section_indexes(unsigned int shndx)
1987 for (Section_list::iterator p = this->unattached_section_list_.begin();
1988 p != this->unattached_section_list_.end();
1991 if (!(*p)->has_out_shndx())
1993 (*p)->set_out_shndx(shndx);
2000 // Set the section addresses according to the linker script. This is
2001 // only called when we see a SECTIONS clause. This returns the
2002 // program segment which should hold the file header and segment
2003 // headers, if any. It will return NULL if they should not be in a
2007 Layout::set_section_addresses_from_script(Symbol_table* symtab)
2009 Script_sections* ss = this->script_options_->script_sections();
2010 gold_assert(ss->saw_sections_clause());
2012 // Place each orphaned output section in the script.
2013 for (Section_list::iterator p = this->section_list_.begin();
2014 p != this->section_list_.end();
2017 if (!(*p)->found_in_sections_clause())
2018 ss->place_orphan(*p);
2021 return this->script_options_->set_section_addresses(symtab, this);
2024 // Count the local symbols in the regular symbol table and the dynamic
2025 // symbol table, and build the respective string pools.
2028 Layout::count_local_symbols(const Task* task,
2029 const Input_objects* input_objects)
2031 // First, figure out an upper bound on the number of symbols we'll
2032 // be inserting into each pool. This helps us create the pools with
2033 // the right size, to avoid unnecessary hashtable resizing.
2034 unsigned int symbol_count = 0;
2035 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2036 p != input_objects->relobj_end();
2038 symbol_count += (*p)->local_symbol_count();
2040 // Go from "upper bound" to "estimate." We overcount for two
2041 // reasons: we double-count symbols that occur in more than one
2042 // object file, and we count symbols that are dropped from the
2043 // output. Add it all together and assume we overcount by 100%.
2046 // We assume all symbols will go into both the sympool and dynpool.
2047 this->sympool_.reserve(symbol_count);
2048 this->dynpool_.reserve(symbol_count);
2050 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2051 p != input_objects->relobj_end();
2054 Task_lock_obj<Object> tlo(task, *p);
2055 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
2059 // Create the symbol table sections. Here we also set the final
2060 // values of the symbols. At this point all the loadable sections are
2061 // fully laid out. SHNUM is the number of sections so far.
2064 Layout::create_symtab_sections(const Input_objects* input_objects,
2065 Symbol_table* symtab,
2071 if (parameters->target().get_size() == 32)
2073 symsize = elfcpp::Elf_sizes<32>::sym_size;
2076 else if (parameters->target().get_size() == 64)
2078 symsize = elfcpp::Elf_sizes<64>::sym_size;
2085 off = align_address(off, align);
2086 off_t startoff = off;
2088 // Save space for the dummy symbol at the start of the section. We
2089 // never bother to write this out--it will just be left as zero.
2091 unsigned int local_symbol_index = 1;
2093 // Add STT_SECTION symbols for each Output section which needs one.
2094 for (Section_list::iterator p = this->section_list_.begin();
2095 p != this->section_list_.end();
2098 if (!(*p)->needs_symtab_index())
2099 (*p)->set_symtab_index(-1U);
2102 (*p)->set_symtab_index(local_symbol_index);
2103 ++local_symbol_index;
2108 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2109 p != input_objects->relobj_end();
2112 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
2114 off += (index - local_symbol_index) * symsize;
2115 local_symbol_index = index;
2118 unsigned int local_symcount = local_symbol_index;
2119 gold_assert(local_symcount * symsize == off - startoff);
2122 size_t dyn_global_index;
2124 if (this->dynsym_section_ == NULL)
2127 dyn_global_index = 0;
2132 dyn_global_index = this->dynsym_section_->info();
2133 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
2134 dynoff = this->dynsym_section_->offset() + locsize;
2135 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
2136 gold_assert(static_cast<off_t>(dyncount * symsize)
2137 == this->dynsym_section_->data_size() - locsize);
2140 off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
2141 &this->sympool_, &local_symcount);
2143 if (!parameters->options().strip_all())
2145 this->sympool_.set_string_offsets();
2147 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
2148 Output_section* osymtab = this->make_output_section(symtab_name,
2151 this->symtab_section_ = osymtab;
2153 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
2156 osymtab->add_output_section_data(pos);
2158 // We generate a .symtab_shndx section if we have more than
2159 // SHN_LORESERVE sections. Technically it is possible that we
2160 // don't need one, because it is possible that there are no
2161 // symbols in any of sections with indexes larger than
2162 // SHN_LORESERVE. That is probably unusual, though, and it is
2163 // easier to always create one than to compute section indexes
2164 // twice (once here, once when writing out the symbols).
2165 if (shnum >= elfcpp::SHN_LORESERVE)
2167 const char* symtab_xindex_name = this->namepool_.add(".symtab_shndx",
2169 Output_section* osymtab_xindex =
2170 this->make_output_section(symtab_xindex_name,
2171 elfcpp::SHT_SYMTAB_SHNDX, 0);
2173 size_t symcount = (off - startoff) / symsize;
2174 this->symtab_xindex_ = new Output_symtab_xindex(symcount);
2176 osymtab_xindex->add_output_section_data(this->symtab_xindex_);
2178 osymtab_xindex->set_link_section(osymtab);
2179 osymtab_xindex->set_addralign(4);
2180 osymtab_xindex->set_entsize(4);
2182 osymtab_xindex->set_after_input_sections();
2184 // This tells the driver code to wait until the symbol table
2185 // has written out before writing out the postprocessing
2186 // sections, including the .symtab_shndx section.
2187 this->any_postprocessing_sections_ = true;
2190 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
2191 Output_section* ostrtab = this->make_output_section(strtab_name,
2195 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
2196 ostrtab->add_output_section_data(pstr);
2198 osymtab->set_file_offset(startoff);
2199 osymtab->finalize_data_size();
2200 osymtab->set_link_section(ostrtab);
2201 osymtab->set_info(local_symcount);
2202 osymtab->set_entsize(symsize);
2208 // Create the .shstrtab section, which holds the names of the
2209 // sections. At the time this is called, we have created all the
2210 // output sections except .shstrtab itself.
2213 Layout::create_shstrtab()
2215 // FIXME: We don't need to create a .shstrtab section if we are
2216 // stripping everything.
2218 const char* name = this->namepool_.add(".shstrtab", false, NULL);
2220 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
2222 // We can't write out this section until we've set all the section
2223 // names, and we don't set the names of compressed output sections
2224 // until relocations are complete.
2225 os->set_after_input_sections();
2227 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
2228 os->add_output_section_data(posd);
2233 // Create the section headers. SIZE is 32 or 64. OFF is the file
2237 Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff)
2239 Output_section_headers* oshdrs;
2240 oshdrs = new Output_section_headers(this,
2241 &this->segment_list_,
2242 &this->section_list_,
2243 &this->unattached_section_list_,
2246 off_t off = align_address(*poff, oshdrs->addralign());
2247 oshdrs->set_address_and_file_offset(0, off);
2248 off += oshdrs->data_size();
2250 this->section_headers_ = oshdrs;
2253 // Count the allocated sections.
2256 Layout::allocated_output_section_count() const
2258 size_t section_count = 0;
2259 for (Segment_list::const_iterator p = this->segment_list_.begin();
2260 p != this->segment_list_.end();
2262 section_count += (*p)->output_section_count();
2263 return section_count;
2266 // Create the dynamic symbol table.
2269 Layout::create_dynamic_symtab(const Input_objects* input_objects,
2270 Symbol_table* symtab,
2271 Output_section **pdynstr,
2272 unsigned int* plocal_dynamic_count,
2273 std::vector<Symbol*>* pdynamic_symbols,
2274 Versions* pversions)
2276 // Count all the symbols in the dynamic symbol table, and set the
2277 // dynamic symbol indexes.
2279 // Skip symbol 0, which is always all zeroes.
2280 unsigned int index = 1;
2282 // Add STT_SECTION symbols for each Output section which needs one.
2283 for (Section_list::iterator p = this->section_list_.begin();
2284 p != this->section_list_.end();
2287 if (!(*p)->needs_dynsym_index())
2288 (*p)->set_dynsym_index(-1U);
2291 (*p)->set_dynsym_index(index);
2296 // Count the local symbols that need to go in the dynamic symbol table,
2297 // and set the dynamic symbol indexes.
2298 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2299 p != input_objects->relobj_end();
2302 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
2306 unsigned int local_symcount = index;
2307 *plocal_dynamic_count = local_symcount;
2309 index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
2310 &this->dynpool_, pversions);
2314 const int size = parameters->target().get_size();
2317 symsize = elfcpp::Elf_sizes<32>::sym_size;
2320 else if (size == 64)
2322 symsize = elfcpp::Elf_sizes<64>::sym_size;
2328 // Create the dynamic symbol table section.
2330 Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
2335 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
2338 dynsym->add_output_section_data(odata);
2340 dynsym->set_info(local_symcount);
2341 dynsym->set_entsize(symsize);
2342 dynsym->set_addralign(align);
2344 this->dynsym_section_ = dynsym;
2346 Output_data_dynamic* const odyn = this->dynamic_data_;
2347 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
2348 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
2350 // If there are more than SHN_LORESERVE allocated sections, we
2351 // create a .dynsym_shndx section. It is possible that we don't
2352 // need one, because it is possible that there are no dynamic
2353 // symbols in any of the sections with indexes larger than
2354 // SHN_LORESERVE. This is probably unusual, though, and at this
2355 // time we don't know the actual section indexes so it is
2356 // inconvenient to check.
2357 if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE)
2359 Output_section* dynsym_xindex =
2360 this->choose_output_section(NULL, ".dynsym_shndx",
2361 elfcpp::SHT_SYMTAB_SHNDX,
2365 this->dynsym_xindex_ = new Output_symtab_xindex(index);
2367 dynsym_xindex->add_output_section_data(this->dynsym_xindex_);
2369 dynsym_xindex->set_link_section(dynsym);
2370 dynsym_xindex->set_addralign(4);
2371 dynsym_xindex->set_entsize(4);
2373 dynsym_xindex->set_after_input_sections();
2375 // This tells the driver code to wait until the symbol table has
2376 // written out before writing out the postprocessing sections,
2377 // including the .dynsym_shndx section.
2378 this->any_postprocessing_sections_ = true;
2381 // Create the dynamic string table section.
2383 Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
2388 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
2389 dynstr->add_output_section_data(strdata);
2391 dynsym->set_link_section(dynstr);
2392 this->dynamic_section_->set_link_section(dynstr);
2394 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
2395 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
2399 // Create the hash tables.
2401 if (strcmp(parameters->options().hash_style(), "sysv") == 0
2402 || strcmp(parameters->options().hash_style(), "both") == 0)
2404 unsigned char* phash;
2405 unsigned int hashlen;
2406 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
2409 Output_section* hashsec = this->choose_output_section(NULL, ".hash",
2414 Output_section_data* hashdata = new Output_data_const_buffer(phash,
2418 hashsec->add_output_section_data(hashdata);
2420 hashsec->set_link_section(dynsym);
2421 hashsec->set_entsize(4);
2423 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
2426 if (strcmp(parameters->options().hash_style(), "gnu") == 0
2427 || strcmp(parameters->options().hash_style(), "both") == 0)
2429 unsigned char* phash;
2430 unsigned int hashlen;
2431 Dynobj::create_gnu_hash_table(*pdynamic_symbols, local_symcount,
2434 Output_section* hashsec = this->choose_output_section(NULL, ".gnu.hash",
2435 elfcpp::SHT_GNU_HASH,
2439 Output_section_data* hashdata = new Output_data_const_buffer(phash,
2443 hashsec->add_output_section_data(hashdata);
2445 hashsec->set_link_section(dynsym);
2446 hashsec->set_entsize(4);
2448 odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
2452 // Assign offsets to each local portion of the dynamic symbol table.
2455 Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
2457 Output_section* dynsym = this->dynsym_section_;
2458 gold_assert(dynsym != NULL);
2460 off_t off = dynsym->offset();
2462 // Skip the dummy symbol at the start of the section.
2463 off += dynsym->entsize();
2465 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
2466 p != input_objects->relobj_end();
2469 unsigned int count = (*p)->set_local_dynsym_offset(off);
2470 off += count * dynsym->entsize();
2474 // Create the version sections.
2477 Layout::create_version_sections(const Versions* versions,
2478 const Symbol_table* symtab,
2479 unsigned int local_symcount,
2480 const std::vector<Symbol*>& dynamic_symbols,
2481 const Output_section* dynstr)
2483 if (!versions->any_defs() && !versions->any_needs())
2486 switch (parameters->size_and_endianness())
2488 #ifdef HAVE_TARGET_32_LITTLE
2489 case Parameters::TARGET_32_LITTLE:
2490 this->sized_create_version_sections<32, false>(versions, symtab,
2492 dynamic_symbols, dynstr);
2495 #ifdef HAVE_TARGET_32_BIG
2496 case Parameters::TARGET_32_BIG:
2497 this->sized_create_version_sections<32, true>(versions, symtab,
2499 dynamic_symbols, dynstr);
2502 #ifdef HAVE_TARGET_64_LITTLE
2503 case Parameters::TARGET_64_LITTLE:
2504 this->sized_create_version_sections<64, false>(versions, symtab,
2506 dynamic_symbols, dynstr);
2509 #ifdef HAVE_TARGET_64_BIG
2510 case Parameters::TARGET_64_BIG:
2511 this->sized_create_version_sections<64, true>(versions, symtab,
2513 dynamic_symbols, dynstr);
2521 // Create the version sections, sized version.
2523 template<int size, bool big_endian>
2525 Layout::sized_create_version_sections(
2526 const Versions* versions,
2527 const Symbol_table* symtab,
2528 unsigned int local_symcount,
2529 const std::vector<Symbol*>& dynamic_symbols,
2530 const Output_section* dynstr)
2532 Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
2533 elfcpp::SHT_GNU_versym,
2537 unsigned char* vbuf;
2539 versions->symbol_section_contents<size, big_endian>(symtab, &this->dynpool_,
2544 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2,
2547 vsec->add_output_section_data(vdata);
2548 vsec->set_entsize(2);
2549 vsec->set_link_section(this->dynsym_section_);
2551 Output_data_dynamic* const odyn = this->dynamic_data_;
2552 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
2554 if (versions->any_defs())
2556 Output_section* vdsec;
2557 vdsec= this->choose_output_section(NULL, ".gnu.version_d",
2558 elfcpp::SHT_GNU_verdef,
2562 unsigned char* vdbuf;
2563 unsigned int vdsize;
2564 unsigned int vdentries;
2565 versions->def_section_contents<size, big_endian>(&this->dynpool_, &vdbuf,
2566 &vdsize, &vdentries);
2568 Output_section_data* vddata =
2569 new Output_data_const_buffer(vdbuf, vdsize, 4, "** version defs");
2571 vdsec->add_output_section_data(vddata);
2572 vdsec->set_link_section(dynstr);
2573 vdsec->set_info(vdentries);
2575 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
2576 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
2579 if (versions->any_needs())
2581 Output_section* vnsec;
2582 vnsec = this->choose_output_section(NULL, ".gnu.version_r",
2583 elfcpp::SHT_GNU_verneed,
2587 unsigned char* vnbuf;
2588 unsigned int vnsize;
2589 unsigned int vnentries;
2590 versions->need_section_contents<size, big_endian>(&this->dynpool_,
2594 Output_section_data* vndata =
2595 new Output_data_const_buffer(vnbuf, vnsize, 4, "** version refs");
2597 vnsec->add_output_section_data(vndata);
2598 vnsec->set_link_section(dynstr);
2599 vnsec->set_info(vnentries);
2601 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
2602 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
2606 // Create the .interp section and PT_INTERP segment.
2609 Layout::create_interp(const Target* target)
2611 const char* interp = this->options_.dynamic_linker();
2614 interp = target->dynamic_linker();
2615 gold_assert(interp != NULL);
2618 size_t len = strlen(interp) + 1;
2620 Output_section_data* odata = new Output_data_const(interp, len, 1);
2622 Output_section* osec = this->choose_output_section(NULL, ".interp",
2623 elfcpp::SHT_PROGBITS,
2626 osec->add_output_section_data(odata);
2628 if (!this->script_options_->saw_phdrs_clause())
2630 Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP,
2632 oseg->add_output_section(osec, elfcpp::PF_R);
2636 // Finish the .dynamic section and PT_DYNAMIC segment.
2639 Layout::finish_dynamic_section(const Input_objects* input_objects,
2640 const Symbol_table* symtab)
2642 if (!this->script_options_->saw_phdrs_clause())
2644 Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
2647 oseg->add_output_section(this->dynamic_section_,
2648 elfcpp::PF_R | elfcpp::PF_W);
2651 Output_data_dynamic* const odyn = this->dynamic_data_;
2653 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
2654 p != input_objects->dynobj_end();
2657 // FIXME: Handle --as-needed.
2658 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
2661 if (parameters->options().shared())
2663 const char* soname = this->options_.soname();
2665 odyn->add_string(elfcpp::DT_SONAME, soname);
2668 // FIXME: Support --init and --fini.
2669 Symbol* sym = symtab->lookup("_init");
2670 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2671 odyn->add_symbol(elfcpp::DT_INIT, sym);
2673 sym = symtab->lookup("_fini");
2674 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2675 odyn->add_symbol(elfcpp::DT_FINI, sym);
2677 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2679 // Add a DT_RPATH entry if needed.
2680 const General_options::Dir_list& rpath(this->options_.rpath());
2683 std::string rpath_val;
2684 for (General_options::Dir_list::const_iterator p = rpath.begin();
2688 if (rpath_val.empty())
2689 rpath_val = p->name();
2692 // Eliminate duplicates.
2693 General_options::Dir_list::const_iterator q;
2694 for (q = rpath.begin(); q != p; ++q)
2695 if (q->name() == p->name())
2700 rpath_val += p->name();
2705 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
2706 if (parameters->options().enable_new_dtags())
2707 odyn->add_string(elfcpp::DT_RUNPATH, rpath_val);
2710 // Look for text segments that have dynamic relocations.
2711 bool have_textrel = false;
2712 if (!this->script_options_->saw_sections_clause())
2714 for (Segment_list::const_iterator p = this->segment_list_.begin();
2715 p != this->segment_list_.end();
2718 if (((*p)->flags() & elfcpp::PF_W) == 0
2719 && (*p)->dynamic_reloc_count() > 0)
2721 have_textrel = true;
2728 // We don't know the section -> segment mapping, so we are
2729 // conservative and just look for readonly sections with
2730 // relocations. If those sections wind up in writable segments,
2731 // then we have created an unnecessary DT_TEXTREL entry.
2732 for (Section_list::const_iterator p = this->section_list_.begin();
2733 p != this->section_list_.end();
2736 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
2737 && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
2738 && ((*p)->dynamic_reloc_count() > 0))
2740 have_textrel = true;
2746 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2747 // post-link tools can easily modify these flags if desired.
2748 unsigned int flags = 0;
2751 // Add a DT_TEXTREL for compatibility with older loaders.
2752 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
2753 flags |= elfcpp::DF_TEXTREL;
2755 if (parameters->options().shared() && this->has_static_tls())
2756 flags |= elfcpp::DF_STATIC_TLS;
2757 odyn->add_constant(elfcpp::DT_FLAGS, flags);
2760 if (parameters->options().initfirst())
2761 flags |= elfcpp::DF_1_INITFIRST;
2762 if (parameters->options().interpose())
2763 flags |= elfcpp::DF_1_INTERPOSE;
2764 if (parameters->options().loadfltr())
2765 flags |= elfcpp::DF_1_LOADFLTR;
2766 if (parameters->options().nodefaultlib())
2767 flags |= elfcpp::DF_1_NODEFLIB;
2768 if (parameters->options().nodelete())
2769 flags |= elfcpp::DF_1_NODELETE;
2770 if (parameters->options().nodlopen())
2771 flags |= elfcpp::DF_1_NOOPEN;
2772 if (parameters->options().nodump())
2773 flags |= elfcpp::DF_1_NODUMP;
2774 if (!parameters->options().shared())
2775 flags &= ~(elfcpp::DF_1_INITFIRST
2776 | elfcpp::DF_1_NODELETE
2777 | elfcpp::DF_1_NOOPEN);
2779 odyn->add_constant(elfcpp::DT_FLAGS_1, flags);
2782 // The mapping of .gnu.linkonce section names to real section names.
2784 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2785 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
2787 MAPPING_INIT("d.rel.ro.local", ".data.rel.ro.local"), // Before "d.rel.ro".
2788 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Before "d".
2789 MAPPING_INIT("t", ".text"),
2790 MAPPING_INIT("r", ".rodata"),
2791 MAPPING_INIT("d", ".data"),
2792 MAPPING_INIT("b", ".bss"),
2793 MAPPING_INIT("s", ".sdata"),
2794 MAPPING_INIT("sb", ".sbss"),
2795 MAPPING_INIT("s2", ".sdata2"),
2796 MAPPING_INIT("sb2", ".sbss2"),
2797 MAPPING_INIT("wi", ".debug_info"),
2798 MAPPING_INIT("td", ".tdata"),
2799 MAPPING_INIT("tb", ".tbss"),
2800 MAPPING_INIT("lr", ".lrodata"),
2801 MAPPING_INIT("l", ".ldata"),
2802 MAPPING_INIT("lb", ".lbss"),
2806 const int Layout::linkonce_mapping_count =
2807 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
2809 // Return the name of the output section to use for a .gnu.linkonce
2810 // section. This is based on the default ELF linker script of the old
2811 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2812 // to ".text". Set *PLEN to the length of the name. *PLEN is
2813 // initialized to the length of NAME.
2816 Layout::linkonce_output_name(const char* name, size_t *plen)
2818 const char* s = name + sizeof(".gnu.linkonce") - 1;
2822 const Linkonce_mapping* plm = linkonce_mapping;
2823 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
2825 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
2834 // Choose the output section name to use given an input section name.
2835 // Set *PLEN to the length of the name. *PLEN is initialized to the
2839 Layout::output_section_name(const char* name, size_t* plen)
2841 if (Layout::is_linkonce(name))
2843 // .gnu.linkonce sections are laid out as though they were named
2844 // for the sections are placed into.
2845 return Layout::linkonce_output_name(name, plen);
2848 // gcc 4.3 generates the following sorts of section names when it
2849 // needs a section name specific to a function:
2855 // .data.rel.local.FN
2857 // .data.rel.ro.local.FN
2864 // The GNU linker maps all of those to the part before the .FN,
2865 // except that .data.rel.local.FN is mapped to .data, and
2866 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2867 // beginning with .data.rel.ro.local are grouped together.
2869 // For an anonymous namespace, the string FN can contain a '.'.
2871 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2872 // GNU linker maps to .rodata.
2874 // The .data.rel.ro sections enable a security feature triggered by
2875 // the -z relro option. Section which need to be relocated at
2876 // program startup time but which may be readonly after startup are
2877 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2878 // segment. The dynamic linker will make that segment writable,
2879 // perform relocations, and then make it read-only. FIXME: We do
2880 // not yet implement this optimization.
2882 // It is hard to handle this in a principled way.
2884 // These are the rules we follow:
2886 // If the section name has no initial '.', or no dot other than an
2887 // initial '.', we use the name unchanged (i.e., "mysection" and
2888 // ".text" are unchanged).
2890 // If the name starts with ".data.rel.ro.local" we use
2891 // ".data.rel.ro.local".
2893 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2895 // Otherwise, we drop the second '.' and everything that comes after
2896 // it (i.e., ".text.XXX" becomes ".text").
2898 const char* s = name;
2902 const char* sdot = strchr(s, '.');
2906 const char* const data_rel_ro_local = ".data.rel.ro.local";
2907 if (strncmp(name, data_rel_ro_local, strlen(data_rel_ro_local)) == 0)
2909 *plen = strlen(data_rel_ro_local);
2910 return data_rel_ro_local;
2913 const char* const data_rel_ro = ".data.rel.ro";
2914 if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
2916 *plen = strlen(data_rel_ro);
2920 *plen = sdot - name;
2924 // Record the signature of a comdat section, and return whether to
2925 // include it in the link. If GROUP is true, this is a regular
2926 // section group. If GROUP is false, this is a group signature
2927 // derived from the name of a linkonce section. We want linkonce
2928 // signatures and group signatures to block each other, but we don't
2929 // want a linkonce signature to block another linkonce signature.
2932 Layout::add_comdat(Relobj* object, unsigned int shndx,
2933 const std::string& signature, bool group)
2935 Kept_section kept(object, shndx, group);
2936 std::pair<Signatures::iterator, bool> ins(
2937 this->signatures_.insert(std::make_pair(signature, kept)));
2941 // This is the first time we've seen this signature.
2945 if (ins.first->second.group_)
2947 // We've already seen a real section group with this signature.
2952 // This is a real section group, and we've already seen a
2953 // linkonce section with this signature. Record that we've seen
2954 // a section group, and don't include this section group.
2955 ins.first->second.group_ = true;
2960 // We've already seen a linkonce section and this is a linkonce
2961 // section. These don't block each other--this may be the same
2962 // symbol name with different section types.
2967 // Find the given comdat signature, and return the object and section
2968 // index of the kept group.
2970 Layout::find_kept_object(const std::string& signature,
2971 unsigned int* pshndx) const
2973 Signatures::const_iterator p = this->signatures_.find(signature);
2974 if (p == this->signatures_.end())
2977 *pshndx = p->second.shndx_;
2978 return p->second.object_;
2981 // Store the allocated sections into the section list.
2984 Layout::get_allocated_sections(Section_list* section_list) const
2986 for (Section_list::const_iterator p = this->section_list_.begin();
2987 p != this->section_list_.end();
2989 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
2990 section_list->push_back(*p);
2993 // Create an output segment.
2996 Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2998 gold_assert(!parameters->options().relocatable());
2999 Output_segment* oseg = new Output_segment(type, flags);
3000 this->segment_list_.push_back(oseg);
3004 // Write out the Output_sections. Most won't have anything to write,
3005 // since most of the data will come from input sections which are
3006 // handled elsewhere. But some Output_sections do have Output_data.
3009 Layout::write_output_sections(Output_file* of) const
3011 for (Section_list::const_iterator p = this->section_list_.begin();
3012 p != this->section_list_.end();
3015 if (!(*p)->after_input_sections())
3020 // Write out data not associated with a section or the symbol table.
3023 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
3025 if (!parameters->options().strip_all())
3027 const Output_section* symtab_section = this->symtab_section_;
3028 for (Section_list::const_iterator p = this->section_list_.begin();
3029 p != this->section_list_.end();
3032 if ((*p)->needs_symtab_index())
3034 gold_assert(symtab_section != NULL);
3035 unsigned int index = (*p)->symtab_index();
3036 gold_assert(index > 0 && index != -1U);
3037 off_t off = (symtab_section->offset()
3038 + index * symtab_section->entsize());
3039 symtab->write_section_symbol(*p, this->symtab_xindex_, of, off);
3044 const Output_section* dynsym_section = this->dynsym_section_;
3045 for (Section_list::const_iterator p = this->section_list_.begin();
3046 p != this->section_list_.end();
3049 if ((*p)->needs_dynsym_index())
3051 gold_assert(dynsym_section != NULL);
3052 unsigned int index = (*p)->dynsym_index();
3053 gold_assert(index > 0 && index != -1U);
3054 off_t off = (dynsym_section->offset()
3055 + index * dynsym_section->entsize());
3056 symtab->write_section_symbol(*p, this->dynsym_xindex_, of, off);
3060 // Write out the Output_data which are not in an Output_section.
3061 for (Data_list::const_iterator p = this->special_output_list_.begin();
3062 p != this->special_output_list_.end();
3067 // Write out the Output_sections which can only be written after the
3068 // input sections are complete.
3071 Layout::write_sections_after_input_sections(Output_file* of)
3073 // Determine the final section offsets, and thus the final output
3074 // file size. Note we finalize the .shstrab last, to allow the
3075 // after_input_section sections to modify their section-names before
3077 if (this->any_postprocessing_sections_)
3079 off_t off = this->output_file_size_;
3080 off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
3082 // Now that we've finalized the names, we can finalize the shstrab.
3084 this->set_section_offsets(off,
3085 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
3087 if (off > this->output_file_size_)
3090 this->output_file_size_ = off;
3094 for (Section_list::const_iterator p = this->section_list_.begin();
3095 p != this->section_list_.end();
3098 if ((*p)->after_input_sections())
3102 this->section_headers_->write(of);
3105 // If the build ID requires computing a checksum, do so here, and
3106 // write it out. We compute a checksum over the entire file because
3107 // that is simplest.
3110 Layout::write_build_id(Output_file* of) const
3112 if (this->build_id_note_ == NULL)
3115 const unsigned char* iv = of->get_input_view(0, this->output_file_size_);
3117 unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
3118 this->build_id_note_->data_size());
3120 const char* style = parameters->options().build_id();
3121 if (strcmp(style, "sha1") == 0)
3124 sha1_init_ctx(&ctx);
3125 sha1_process_bytes(iv, this->output_file_size_, &ctx);
3126 sha1_finish_ctx(&ctx, ov);
3128 else if (strcmp(style, "md5") == 0)
3132 md5_process_bytes(iv, this->output_file_size_, &ctx);
3133 md5_finish_ctx(&ctx, ov);
3138 of->write_output_view(this->build_id_note_->offset(),
3139 this->build_id_note_->data_size(),
3142 of->free_input_view(0, this->output_file_size_, iv);
3145 // Write out a binary file. This is called after the link is
3146 // complete. IN is the temporary output file we used to generate the
3147 // ELF code. We simply walk through the segments, read them from
3148 // their file offset in IN, and write them to their load address in
3149 // the output file. FIXME: with a bit more work, we could support
3150 // S-records and/or Intel hex format here.
3153 Layout::write_binary(Output_file* in) const
3155 gold_assert(this->options_.oformat_enum()
3156 == General_options::OBJECT_FORMAT_BINARY);
3158 // Get the size of the binary file.
3159 uint64_t max_load_address = 0;
3160 for (Segment_list::const_iterator p = this->segment_list_.begin();
3161 p != this->segment_list_.end();
3164 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
3166 uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
3167 if (max_paddr > max_load_address)
3168 max_load_address = max_paddr;
3172 Output_file out(parameters->options().output_file_name());
3173 out.open(max_load_address);
3175 for (Segment_list::const_iterator p = this->segment_list_.begin();
3176 p != this->segment_list_.end();
3179 if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
3181 const unsigned char* vin = in->get_input_view((*p)->offset(),
3183 unsigned char* vout = out.get_output_view((*p)->paddr(),
3185 memcpy(vout, vin, (*p)->filesz());
3186 out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
3187 in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
3194 // Print the output sections to the map file.
3197 Layout::print_to_mapfile(Mapfile* mapfile) const
3199 for (Segment_list::const_iterator p = this->segment_list_.begin();
3200 p != this->segment_list_.end();
3202 (*p)->print_sections_to_mapfile(mapfile);
3205 // Print statistical information to stderr. This is used for --stats.
3208 Layout::print_stats() const
3210 this->namepool_.print_stats("section name pool");
3211 this->sympool_.print_stats("output symbol name pool");
3212 this->dynpool_.print_stats("dynamic name pool");
3214 for (Section_list::const_iterator p = this->section_list_.begin();
3215 p != this->section_list_.end();
3217 (*p)->print_merge_stats();
3220 // Write_sections_task methods.
3222 // We can always run this task.
3225 Write_sections_task::is_runnable()
3230 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
3234 Write_sections_task::locks(Task_locker* tl)
3236 tl->add(this, this->output_sections_blocker_);
3237 tl->add(this, this->final_blocker_);
3240 // Run the task--write out the data.
3243 Write_sections_task::run(Workqueue*)
3245 this->layout_->write_output_sections(this->of_);
3248 // Write_data_task methods.
3250 // We can always run this task.
3253 Write_data_task::is_runnable()
3258 // We need to unlock FINAL_BLOCKER when finished.
3261 Write_data_task::locks(Task_locker* tl)
3263 tl->add(this, this->final_blocker_);
3266 // Run the task--write out the data.
3269 Write_data_task::run(Workqueue*)
3271 this->layout_->write_data(this->symtab_, this->of_);
3274 // Write_symbols_task methods.
3276 // We can always run this task.
3279 Write_symbols_task::is_runnable()
3284 // We need to unlock FINAL_BLOCKER when finished.
3287 Write_symbols_task::locks(Task_locker* tl)
3289 tl->add(this, this->final_blocker_);
3292 // Run the task--write out the symbols.
3295 Write_symbols_task::run(Workqueue*)
3297 this->symtab_->write_globals(this->input_objects_, this->sympool_,
3298 this->dynpool_, this->layout_->symtab_xindex(),
3299 this->layout_->dynsym_xindex(), this->of_);
3302 // Write_after_input_sections_task methods.
3304 // We can only run this task after the input sections have completed.
3307 Write_after_input_sections_task::is_runnable()
3309 if (this->input_sections_blocker_->is_blocked())
3310 return this->input_sections_blocker_;
3314 // We need to unlock FINAL_BLOCKER when finished.
3317 Write_after_input_sections_task::locks(Task_locker* tl)
3319 tl->add(this, this->final_blocker_);
3325 Write_after_input_sections_task::run(Workqueue*)
3327 this->layout_->write_sections_after_input_sections(this->of_);
3330 // Close_task_runner methods.
3332 // Run the task--close the file.
3335 Close_task_runner::run(Workqueue*, const Task*)
3337 // If we need to compute a checksum for the BUILD if, we do so here.
3338 this->layout_->write_build_id(this->of_);
3340 // If we've been asked to create a binary file, we do so here.
3341 if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
3342 this->layout_->write_binary(this->of_);
3347 // Instantiate the templates we need. We could use the configure
3348 // script to restrict this to only the ones for implemented targets.
3350 #ifdef HAVE_TARGET_32_LITTLE
3353 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
3355 const elfcpp::Shdr<32, false>& shdr,
3356 unsigned int, unsigned int, off_t*);
3359 #ifdef HAVE_TARGET_32_BIG
3362 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
3364 const elfcpp::Shdr<32, true>& shdr,
3365 unsigned int, unsigned int, off_t*);
3368 #ifdef HAVE_TARGET_64_LITTLE
3371 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
3373 const elfcpp::Shdr<64, false>& shdr,
3374 unsigned int, unsigned int, off_t*);
3377 #ifdef HAVE_TARGET_64_BIG
3380 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
3382 const elfcpp::Shdr<64, true>& shdr,
3383 unsigned int, unsigned int, off_t*);
3386 #ifdef HAVE_TARGET_32_LITTLE
3389 Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object,
3390 unsigned int reloc_shndx,
3391 const elfcpp::Shdr<32, false>& shdr,
3392 Output_section* data_section,
3393 Relocatable_relocs* rr);
3396 #ifdef HAVE_TARGET_32_BIG
3399 Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object,
3400 unsigned int reloc_shndx,
3401 const elfcpp::Shdr<32, true>& shdr,
3402 Output_section* data_section,
3403 Relocatable_relocs* rr);
3406 #ifdef HAVE_TARGET_64_LITTLE
3409 Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object,
3410 unsigned int reloc_shndx,
3411 const elfcpp::Shdr<64, false>& shdr,
3412 Output_section* data_section,
3413 Relocatable_relocs* rr);
3416 #ifdef HAVE_TARGET_64_BIG
3419 Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object,
3420 unsigned int reloc_shndx,
3421 const elfcpp::Shdr<64, true>& shdr,
3422 Output_section* data_section,
3423 Relocatable_relocs* rr);
3426 #ifdef HAVE_TARGET_32_LITTLE
3429 Layout::layout_group<32, false>(Symbol_table* symtab,
3430 Sized_relobj<32, false>* object,
3432 const char* group_section_name,
3433 const char* signature,
3434 const elfcpp::Shdr<32, false>& shdr,
3435 elfcpp::Elf_Word flags,
3436 std::vector<unsigned int>* shndxes);
3439 #ifdef HAVE_TARGET_32_BIG
3442 Layout::layout_group<32, true>(Symbol_table* symtab,
3443 Sized_relobj<32, true>* object,
3445 const char* group_section_name,
3446 const char* signature,
3447 const elfcpp::Shdr<32, true>& shdr,
3448 elfcpp::Elf_Word flags,
3449 std::vector<unsigned int>* shndxes);
3452 #ifdef HAVE_TARGET_64_LITTLE
3455 Layout::layout_group<64, false>(Symbol_table* symtab,
3456 Sized_relobj<64, false>* object,
3458 const char* group_section_name,
3459 const char* signature,
3460 const elfcpp::Shdr<64, false>& shdr,
3461 elfcpp::Elf_Word flags,
3462 std::vector<unsigned int>* shndxes);
3465 #ifdef HAVE_TARGET_64_BIG
3468 Layout::layout_group<64, true>(Symbol_table* symtab,
3469 Sized_relobj<64, true>* object,
3471 const char* group_section_name,
3472 const char* signature,
3473 const elfcpp::Shdr<64, true>& shdr,
3474 elfcpp::Elf_Word flags,
3475 std::vector<unsigned int>* shndxes);
3478 #ifdef HAVE_TARGET_32_LITTLE
3481 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
3482 const unsigned char* symbols,
3484 const unsigned char* symbol_names,
3485 off_t symbol_names_size,
3487 const elfcpp::Shdr<32, false>& shdr,
3488 unsigned int reloc_shndx,
3489 unsigned int reloc_type,
3493 #ifdef HAVE_TARGET_32_BIG
3496 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
3497 const unsigned char* symbols,
3499 const unsigned char* symbol_names,
3500 off_t symbol_names_size,
3502 const elfcpp::Shdr<32, true>& shdr,
3503 unsigned int reloc_shndx,
3504 unsigned int reloc_type,
3508 #ifdef HAVE_TARGET_64_LITTLE
3511 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
3512 const unsigned char* symbols,
3514 const unsigned char* symbol_names,
3515 off_t symbol_names_size,
3517 const elfcpp::Shdr<64, false>& shdr,
3518 unsigned int reloc_shndx,
3519 unsigned int reloc_type,
3523 #ifdef HAVE_TARGET_64_BIG
3526 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
3527 const unsigned char* symbols,
3529 const unsigned char* symbol_names,
3530 off_t symbol_names_size,
3532 const elfcpp::Shdr<64, true>& shdr,
3533 unsigned int reloc_shndx,
3534 unsigned int reloc_type,
3538 } // End namespace gold.