1 // layout.cc -- lay out output file sections for gold
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
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.
30 #include "parameters.h"
33 #include "script-sections.h"
38 #include "compressed_output.h"
45 // Layout_task_runner methods.
47 // Lay out the sections. This is called after all the input objects
51 Layout_task_runner::run(Workqueue* workqueue, const Task* task)
53 off_t file_size = this->layout_->finalize(this->input_objects_,
57 // Now we know the final size of the output file and we know where
58 // each piece of information goes.
59 Output_file* of = new Output_file(parameters->output_file_name());
62 // Queue up the final set of tasks.
63 gold::queue_final_tasks(this->options_, this->input_objects_,
64 this->symtab_, this->layout_, workqueue, of);
69 Layout::Layout(const General_options& options, Script_options* script_options)
70 : options_(options), script_options_(script_options), namepool_(),
71 sympool_(), dynpool_(), signatures_(),
72 section_name_map_(), segment_list_(), section_list_(),
73 unattached_section_list_(), special_output_list_(),
74 section_headers_(NULL), tls_segment_(NULL), symtab_section_(NULL),
75 dynsym_section_(NULL), dynamic_section_(NULL), dynamic_data_(NULL),
76 eh_frame_section_(NULL), output_file_size_(-1),
77 input_requires_executable_stack_(false),
78 input_with_gnu_stack_note_(false),
79 input_without_gnu_stack_note_(false),
80 has_static_tls_(false),
81 any_postprocessing_sections_(false)
83 // Make space for more than enough segments for a typical file.
84 // This is just for efficiency--it's OK if we wind up needing more.
85 this->segment_list_.reserve(12);
87 // We expect two unattached Output_data objects: the file header and
88 // the segment headers.
89 this->special_output_list_.reserve(2);
92 // Hash a key we use to look up an output section mapping.
95 Layout::Hash_key::operator()(const Layout::Key& k) const
97 return k.first + k.second.first + k.second.second;
100 // Return whether PREFIX is a prefix of STR.
103 is_prefix_of(const char* prefix, const char* str)
105 return strncmp(prefix, str, strlen(prefix)) == 0;
108 // Returns whether the given section is in the list of
109 // debug-sections-used-by-some-version-of-gdb. Currently,
110 // we've checked versions of gdb up to and including 6.7.1.
112 static const char* gdb_sections[] =
114 // ".debug_aranges", // not used by gdb as of 6.7.1
120 // ".debug_pubnames", // not used by gdb as of 6.7.1
126 is_gdb_debug_section(const char* str)
128 // We can do this faster: binary search or a hashtable. But why bother?
129 for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
130 if (strcmp(str, gdb_sections[i]) == 0)
135 // Whether to include this section in the link.
137 template<int size, bool big_endian>
139 Layout::include_section(Sized_relobj<size, big_endian>*, const char* name,
140 const elfcpp::Shdr<size, big_endian>& shdr)
142 switch (shdr.get_sh_type())
144 case elfcpp::SHT_NULL:
145 case elfcpp::SHT_SYMTAB:
146 case elfcpp::SHT_DYNSYM:
147 case elfcpp::SHT_STRTAB:
148 case elfcpp::SHT_HASH:
149 case elfcpp::SHT_DYNAMIC:
150 case elfcpp::SHT_SYMTAB_SHNDX:
153 case elfcpp::SHT_RELA:
154 case elfcpp::SHT_REL:
155 case elfcpp::SHT_GROUP:
156 // For a relocatable link these should be handled elsewhere.
157 gold_assert(!parameters->output_is_object());
160 case elfcpp::SHT_PROGBITS:
161 if (parameters->strip_debug()
162 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
164 // Debugging sections can only be recognized by name.
165 if (is_prefix_of(".debug", name)
166 || is_prefix_of(".gnu.linkonce.wi.", name)
167 || is_prefix_of(".line", name)
168 || is_prefix_of(".stab", name))
171 if (parameters->strip_debug_gdb()
172 && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
174 // Debugging sections can only be recognized by name.
175 if (is_prefix_of(".debug", name)
176 && !is_gdb_debug_section(name))
186 // Return an output section named NAME, or NULL if there is none.
189 Layout::find_output_section(const char* name) const
191 for (Section_list::const_iterator p = this->section_list_.begin();
192 p != this->section_list_.end();
194 if (strcmp((*p)->name(), name) == 0)
199 // Return an output segment of type TYPE, with segment flags SET set
200 // and segment flags CLEAR clear. Return NULL if there is none.
203 Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
204 elfcpp::Elf_Word clear) const
206 for (Segment_list::const_iterator p = this->segment_list_.begin();
207 p != this->segment_list_.end();
209 if (static_cast<elfcpp::PT>((*p)->type()) == type
210 && ((*p)->flags() & set) == set
211 && ((*p)->flags() & clear) == 0)
216 // Return the output section to use for section NAME with type TYPE
217 // and section flags FLAGS. NAME must be canonicalized in the string
218 // pool, and NAME_KEY is the key.
221 Layout::get_output_section(const char* name, Stringpool::Key name_key,
222 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags)
224 const Key key(name_key, std::make_pair(type, flags));
225 const std::pair<Key, Output_section*> v(key, NULL);
226 std::pair<Section_name_map::iterator, bool> ins(
227 this->section_name_map_.insert(v));
230 return ins.first->second;
233 // This is the first time we've seen this name/type/flags
235 Output_section* os = this->make_output_section(name, type, flags);
236 ins.first->second = os;
241 // Pick the output section to use for section NAME, in input file
242 // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
243 // linker created section. ADJUST_NAME is true if we should apply the
244 // standard name mappings in Layout::output_section_name. This will
245 // return NULL if the input section should be discarded.
248 Layout::choose_output_section(const Relobj* relobj, const char* name,
249 elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
252 // We should ignore some flags. FIXME: This will need some
253 // adjustment for ld -r.
254 flags &= ~ (elfcpp::SHF_INFO_LINK
255 | elfcpp::SHF_LINK_ORDER
258 | elfcpp::SHF_STRINGS);
260 if (this->script_options_->saw_sections_clause())
262 // We are using a SECTIONS clause, so the output section is
263 // chosen based only on the name.
265 Script_sections* ss = this->script_options_->script_sections();
266 const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
267 Output_section** output_section_slot;
268 name = ss->output_section_name(file_name, name, &output_section_slot);
271 // The SECTIONS clause says to discard this input section.
275 // If this is an orphan section--one not mentioned in the linker
276 // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
277 // default processing below.
279 if (output_section_slot != NULL)
281 if (*output_section_slot != NULL)
282 return *output_section_slot;
284 // We don't put sections found in the linker script into
285 // SECTION_NAME_MAP_. That keeps us from getting confused
286 // if an orphan section is mapped to a section with the same
287 // name as one in the linker script.
289 name = this->namepool_.add(name, false, NULL);
291 Output_section* os = this->make_output_section(name, type, flags);
292 os->set_found_in_sections_clause();
293 *output_section_slot = os;
298 // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
300 // Turn NAME from the name of the input section into the name of the
303 size_t len = strlen(name);
304 if (adjust_name && !parameters->output_is_object())
305 name = Layout::output_section_name(name, &len);
307 Stringpool::Key name_key;
308 name = this->namepool_.add_with_length(name, len, true, &name_key);
310 // Find or make the output section. The output section is selected
311 // based on the section name, type, and flags.
312 return this->get_output_section(name, name_key, type, flags);
315 // Return the output section to use for input section SHNDX, with name
316 // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
317 // index of a relocation section which applies to this section, or 0
318 // if none, or -1U if more than one. RELOC_TYPE is the type of the
319 // relocation section if there is one. Set *OFF to the offset of this
320 // input section without the output section. Return NULL if the
321 // section should be discarded. Set *OFF to -1 if the section
322 // contents should not be written directly to the output file, but
323 // will instead receive special handling.
325 template<int size, bool big_endian>
327 Layout::layout(Sized_relobj<size, big_endian>* object, unsigned int shndx,
328 const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
329 unsigned int reloc_shndx, unsigned int, off_t* off)
331 if (!this->include_section(object, name, shdr))
336 // In a relocatable link a grouped section must not be combined with
337 // any other sections.
338 if (parameters->output_is_object()
339 && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
341 name = this->namepool_.add(name, true, NULL);
342 os = this->make_output_section(name, shdr.get_sh_type(),
343 shdr.get_sh_flags());
347 os = this->choose_output_section(object, name, shdr.get_sh_type(),
348 shdr.get_sh_flags(), true);
353 // FIXME: Handle SHF_LINK_ORDER somewhere.
355 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
356 this->script_options_->saw_sections_clause());
361 // Handle a relocation section when doing a relocatable link.
363 template<int size, bool big_endian>
365 Layout::layout_reloc(Sized_relobj<size, big_endian>* object,
367 const elfcpp::Shdr<size, big_endian>& shdr,
368 Output_section* data_section,
369 Relocatable_relocs* rr)
371 gold_assert(parameters->output_is_object());
373 int sh_type = shdr.get_sh_type();
376 if (sh_type == elfcpp::SHT_REL)
378 else if (sh_type == elfcpp::SHT_RELA)
382 name += data_section->name();
384 Output_section* os = this->choose_output_section(object, name.c_str(),
389 os->set_should_link_to_symtab();
390 os->set_info_section(data_section);
392 Output_section_data* posd;
393 if (sh_type == elfcpp::SHT_REL)
395 os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
396 posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
400 else if (sh_type == elfcpp::SHT_RELA)
402 os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
403 posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
410 os->add_output_section_data(posd);
411 rr->set_output_data(posd);
416 // Handle a group section when doing a relocatable link.
418 template<int size, bool big_endian>
420 Layout::layout_group(Symbol_table* symtab,
421 Sized_relobj<size, big_endian>* object,
423 const char* group_section_name,
424 const char* signature,
425 const elfcpp::Shdr<size, big_endian>& shdr,
426 const elfcpp::Elf_Word* contents)
428 gold_assert(parameters->output_is_object());
429 gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
430 group_section_name = this->namepool_.add(group_section_name, true, NULL);
431 Output_section* os = this->make_output_section(group_section_name,
433 shdr.get_sh_flags());
435 // We need to find a symbol with the signature in the symbol table.
436 // This is a hack to force that to happen.
437 Symbol* sym = symtab->lookup(signature, NULL);
439 sym = symtab->define_as_constant(signature, NULL, 0, 0,
442 elfcpp::STV_HIDDEN, 0, false);
444 os->set_should_link_to_symtab();
445 os->set_info_symndx(sym);
448 section_size_type entry_count =
449 convert_to_section_size_type(shdr.get_sh_size() / 4);
450 Output_section_data* posd =
451 new Output_data_group<size, big_endian>(object, entry_count, contents);
452 os->add_output_section_data(posd);
455 // Special GNU handling of sections name .eh_frame. They will
456 // normally hold exception frame data as defined by the C++ ABI
457 // (http://codesourcery.com/cxx-abi/).
459 template<int size, bool big_endian>
461 Layout::layout_eh_frame(Sized_relobj<size, big_endian>* object,
462 const unsigned char* symbols,
464 const unsigned char* symbol_names,
465 off_t symbol_names_size,
467 const elfcpp::Shdr<size, big_endian>& shdr,
468 unsigned int reloc_shndx, unsigned int reloc_type,
471 gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS);
472 gold_assert(shdr.get_sh_flags() == elfcpp::SHF_ALLOC);
474 const char* const name = ".eh_frame";
475 Output_section* os = this->choose_output_section(object,
477 elfcpp::SHT_PROGBITS,
483 if (this->eh_frame_section_ == NULL)
485 this->eh_frame_section_ = os;
486 this->eh_frame_data_ = new Eh_frame();
487 os->add_output_section_data(this->eh_frame_data_);
489 if (this->options_.create_eh_frame_hdr())
491 Output_section* hdr_os =
492 this->choose_output_section(NULL,
494 elfcpp::SHT_PROGBITS,
500 Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
501 this->eh_frame_data_);
502 hdr_os->add_output_section_data(hdr_posd);
504 hdr_os->set_after_input_sections();
506 if (!this->script_options_->saw_phdrs_clause())
508 Output_segment* hdr_oseg;
509 hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
511 hdr_oseg->add_output_section(hdr_os, elfcpp::PF_R);
514 this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
519 gold_assert(this->eh_frame_section_ == os);
521 if (this->eh_frame_data_->add_ehframe_input_section(object,
532 // We couldn't handle this .eh_frame section for some reason.
533 // Add it as a normal section.
534 bool saw_sections_clause = this->script_options_->saw_sections_clause();
535 *off = os->add_input_section(object, shndx, name, shdr, reloc_shndx,
536 saw_sections_clause);
542 // Add POSD to an output section using NAME, TYPE, and FLAGS.
545 Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
546 elfcpp::Elf_Xword flags,
547 Output_section_data* posd)
549 Output_section* os = this->choose_output_section(NULL, name, type, flags,
552 os->add_output_section_data(posd);
555 // Map section flags to segment flags.
558 Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
560 elfcpp::Elf_Word ret = elfcpp::PF_R;
561 if ((flags & elfcpp::SHF_WRITE) != 0)
563 if ((flags & elfcpp::SHF_EXECINSTR) != 0)
568 // Sometimes we compress sections. This is typically done for
569 // sections that are not part of normal program execution (such as
570 // .debug_* sections), and where the readers of these sections know
571 // how to deal with compressed sections. (To make it easier for them,
572 // we will rename the ouput section in such cases from .foo to
573 // .foo.zlib.nnnn, where nnnn is the uncompressed size.) This routine
574 // doesn't say for certain whether we'll compress -- it depends on
575 // commandline options as well -- just whether this section is a
576 // candidate for compression.
579 is_compressible_debug_section(const char* secname)
581 return (strncmp(secname, ".debug", sizeof(".debug") - 1) == 0);
584 // Make a new Output_section, and attach it to segments as
588 Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
589 elfcpp::Elf_Xword flags)
592 if ((flags & elfcpp::SHF_ALLOC) == 0
593 && this->options_.compress_debug_sections()
594 && is_compressible_debug_section(name))
595 os = new Output_compressed_section(&this->options_, name, type, flags);
597 os = new Output_section(name, type, flags);
599 this->section_list_.push_back(os);
601 if ((flags & elfcpp::SHF_ALLOC) == 0)
602 this->unattached_section_list_.push_back(os);
605 if (parameters->output_is_object())
608 // If we have a SECTIONS clause, we can't handle the attachment
609 // to segments until after we've seen all the sections.
610 if (this->script_options_->saw_sections_clause())
613 gold_assert(!this->script_options_->saw_phdrs_clause());
615 // This output section goes into a PT_LOAD segment.
617 elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
619 // The only thing we really care about for PT_LOAD segments is
620 // whether or not they are writable, so that is how we search
621 // for them. People who need segments sorted on some other
622 // basis will have to wait until we implement a mechanism for
623 // them to describe the segments they want.
625 Segment_list::const_iterator p;
626 for (p = this->segment_list_.begin();
627 p != this->segment_list_.end();
630 if ((*p)->type() == elfcpp::PT_LOAD
631 && ((*p)->flags() & elfcpp::PF_W) == (seg_flags & elfcpp::PF_W))
633 (*p)->add_output_section(os, seg_flags);
638 if (p == this->segment_list_.end())
640 Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
642 oseg->add_output_section(os, seg_flags);
645 // If we see a loadable SHT_NOTE section, we create a PT_NOTE
647 if (type == elfcpp::SHT_NOTE)
649 // See if we already have an equivalent PT_NOTE segment.
650 for (p = this->segment_list_.begin();
651 p != segment_list_.end();
654 if ((*p)->type() == elfcpp::PT_NOTE
655 && (((*p)->flags() & elfcpp::PF_W)
656 == (seg_flags & elfcpp::PF_W)))
658 (*p)->add_output_section(os, seg_flags);
663 if (p == this->segment_list_.end())
665 Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
667 oseg->add_output_section(os, seg_flags);
671 // If we see a loadable SHF_TLS section, we create a PT_TLS
672 // segment. There can only be one such segment.
673 if ((flags & elfcpp::SHF_TLS) != 0)
675 if (this->tls_segment_ == NULL)
676 this->tls_segment_ = this->make_output_segment(elfcpp::PT_TLS,
678 this->tls_segment_->add_output_section(os, seg_flags);
685 // Return the number of segments we expect to see.
688 Layout::expected_segment_count() const
690 size_t ret = this->segment_list_.size();
692 // If we didn't see a SECTIONS clause in a linker script, we should
693 // already have the complete list of segments. Otherwise we ask the
694 // SECTIONS clause how many segments it expects, and add in the ones
695 // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
697 if (!this->script_options_->saw_sections_clause())
701 const Script_sections* ss = this->script_options_->script_sections();
702 return ret + ss->expected_segment_count(this);
706 // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
707 // is whether we saw a .note.GNU-stack section in the object file.
708 // GNU_STACK_FLAGS is the section flags. The flags give the
709 // protection required for stack memory. We record this in an
710 // executable as a PT_GNU_STACK segment. If an object file does not
711 // have a .note.GNU-stack segment, we must assume that it is an old
712 // object. On some targets that will force an executable stack.
715 Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags)
718 this->input_without_gnu_stack_note_ = true;
721 this->input_with_gnu_stack_note_ = true;
722 if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
723 this->input_requires_executable_stack_ = true;
727 // Create the dynamic sections which are needed before we read the
731 Layout::create_initial_dynamic_sections(Symbol_table* symtab)
733 if (parameters->doing_static_link())
736 this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
739 | elfcpp::SHF_WRITE),
742 symtab->define_in_output_data("_DYNAMIC", NULL, this->dynamic_section_, 0, 0,
743 elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
744 elfcpp::STV_HIDDEN, 0, false, false);
746 this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
748 this->dynamic_section_->add_output_section_data(this->dynamic_data_);
751 // For each output section whose name can be represented as C symbol,
752 // define __start and __stop symbols for the section. This is a GNU
756 Layout::define_section_symbols(Symbol_table* symtab)
758 for (Section_list::const_iterator p = this->section_list_.begin();
759 p != this->section_list_.end();
762 const char* const name = (*p)->name();
763 if (name[strspn(name,
765 "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
766 "abcdefghijklmnopqrstuvwxyz"
770 const std::string name_string(name);
771 const std::string start_name("__start_" + name_string);
772 const std::string stop_name("__stop_" + name_string);
774 symtab->define_in_output_data(start_name.c_str(),
783 false, // offset_is_from_end
784 true); // only_if_ref
786 symtab->define_in_output_data(stop_name.c_str(),
795 true, // offset_is_from_end
796 true); // only_if_ref
801 // Find the first read-only PT_LOAD segment, creating one if
805 Layout::find_first_load_seg()
807 for (Segment_list::const_iterator p = this->segment_list_.begin();
808 p != this->segment_list_.end();
811 if ((*p)->type() == elfcpp::PT_LOAD
812 && ((*p)->flags() & elfcpp::PF_R) != 0
813 && ((*p)->flags() & elfcpp::PF_W) == 0)
817 gold_assert(!this->script_options_->saw_phdrs_clause());
819 Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
824 // Finalize the layout. When this is called, we have created all the
825 // output sections and all the output segments which are based on
826 // input sections. We have several things to do, and we have to do
827 // them in the right order, so that we get the right results correctly
830 // 1) Finalize the list of output segments and create the segment
833 // 2) Finalize the dynamic symbol table and associated sections.
835 // 3) Determine the final file offset of all the output segments.
837 // 4) Determine the final file offset of all the SHF_ALLOC output
840 // 5) Create the symbol table sections and the section name table
843 // 6) Finalize the symbol table: set symbol values to their final
844 // value and make a final determination of which symbols are going
845 // into the output symbol table.
847 // 7) Create the section table header.
849 // 8) Determine the final file offset of all the output sections which
850 // are not SHF_ALLOC, including the section table header.
852 // 9) Finalize the ELF file header.
854 // This function returns the size of the output file.
857 Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
860 Target* const target = input_objects->target();
862 target->finalize_sections(this);
864 this->count_local_symbols(task, input_objects);
866 this->create_gold_note();
867 this->create_executable_stack_info(target);
869 Output_segment* phdr_seg = NULL;
870 if (!parameters->output_is_object() && !parameters->doing_static_link())
872 // There was a dynamic object in the link. We need to create
873 // some information for the dynamic linker.
875 // Create the PT_PHDR segment which will hold the program
877 if (!this->script_options_->saw_phdrs_clause())
878 phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
880 // Create the dynamic symbol table, including the hash table.
881 Output_section* dynstr;
882 std::vector<Symbol*> dynamic_symbols;
883 unsigned int local_dynamic_count;
884 Versions versions(this->options_, &this->dynpool_);
885 this->create_dynamic_symtab(input_objects, symtab, &dynstr,
886 &local_dynamic_count, &dynamic_symbols,
889 // Create the .interp section to hold the name of the
890 // interpreter, and put it in a PT_INTERP segment.
891 if (!parameters->output_is_shared())
892 this->create_interp(target);
894 // Finish the .dynamic section to hold the dynamic data, and put
895 // it in a PT_DYNAMIC segment.
896 this->finish_dynamic_section(input_objects, symtab);
898 // We should have added everything we need to the dynamic string
900 this->dynpool_.set_string_offsets();
902 // Create the version sections. We can't do this until the
903 // dynamic string table is complete.
904 this->create_version_sections(&versions, symtab, local_dynamic_count,
905 dynamic_symbols, dynstr);
908 // If there is a SECTIONS clause, put all the input sections into
909 // the required order.
910 Output_segment* load_seg;
911 if (parameters->output_is_object())
913 else if (this->script_options_->saw_sections_clause())
914 load_seg = this->set_section_addresses_from_script(symtab);
916 load_seg = this->find_first_load_seg();
918 gold_assert(phdr_seg == NULL || load_seg != NULL);
920 // Lay out the segment headers.
921 Output_segment_headers* segment_headers;
922 if (parameters->output_is_object())
923 segment_headers = NULL;
926 segment_headers = new Output_segment_headers(this->segment_list_);
927 if (load_seg != NULL)
928 load_seg->add_initial_output_data(segment_headers);
929 if (phdr_seg != NULL)
930 phdr_seg->add_initial_output_data(segment_headers);
933 // Lay out the file header.
934 Output_file_header* file_header;
935 file_header = new Output_file_header(target, symtab, segment_headers,
936 this->script_options_->entry());
937 if (load_seg != NULL)
938 load_seg->add_initial_output_data(file_header);
940 this->special_output_list_.push_back(file_header);
941 if (segment_headers != NULL)
942 this->special_output_list_.push_back(segment_headers);
944 if (this->script_options_->saw_phdrs_clause()
945 && !parameters->output_is_object())
947 // Support use of FILEHDRS and PHDRS attachments in a PHDRS
948 // clause in a linker script.
949 Script_sections* ss = this->script_options_->script_sections();
950 ss->put_headers_in_phdrs(file_header, segment_headers);
953 // We set the output section indexes in set_segment_offsets and
954 // set_section_indexes.
955 unsigned int shndx = 1;
957 // Set the file offsets of all the segments, and all the sections
960 if (!parameters->output_is_object())
961 off = this->set_segment_offsets(target, load_seg, &shndx);
963 off = this->set_relocatable_section_offsets(file_header, &shndx);
965 // Set the file offsets of all the non-data sections we've seen so
966 // far which don't have to wait for the input sections. We need
967 // this in order to finalize local symbols in non-allocated
969 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
971 // Create the symbol table sections.
972 this->create_symtab_sections(input_objects, symtab, &off);
973 if (!parameters->doing_static_link())
974 this->assign_local_dynsym_offsets(input_objects);
976 // Process any symbol assignments from a linker script. This must
977 // be called after the symbol table has been finalized.
978 this->script_options_->finalize_symbols(symtab, this);
980 // Create the .shstrtab section.
981 Output_section* shstrtab_section = this->create_shstrtab();
983 // Set the file offsets of the rest of the non-data sections which
984 // don't have to wait for the input sections.
985 off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
987 // Now that all sections have been created, set the section indexes.
988 shndx = this->set_section_indexes(shndx);
990 // Create the section table header.
991 this->create_shdrs(&off);
993 // If there are no sections which require postprocessing, we can
994 // handle the section names now, and avoid a resize later.
995 if (!this->any_postprocessing_sections_)
996 off = this->set_section_offsets(off,
997 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
999 file_header->set_section_info(this->section_headers_, shstrtab_section);
1001 // Now we know exactly where everything goes in the output file
1002 // (except for non-allocated sections which require postprocessing).
1003 Output_data::layout_complete();
1005 this->output_file_size_ = off;
1010 // Create a .note section for an executable or shared library. This
1011 // records the version of gold used to create the binary.
1014 Layout::create_gold_note()
1016 if (parameters->output_is_object())
1019 // Authorities all agree that the values in a .note field should
1020 // be aligned on 4-byte boundaries for 32-bit binaries. However,
1021 // they differ on what the alignment is for 64-bit binaries.
1022 // The GABI says unambiguously they take 8-byte alignment:
1023 // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
1024 // Other documentation says alignment should always be 4 bytes:
1025 // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
1026 // GNU ld and GNU readelf both support the latter (at least as of
1027 // version 2.16.91), and glibc always generates the latter for
1028 // .note.ABI-tag (as of version 1.6), so that's the one we go with
1030 #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
1031 const int size = parameters->get_size();
1033 const int size = 32;
1036 // The contents of the .note section.
1037 const char* name = "GNU";
1038 std::string desc(std::string("gold ") + gold::get_version_string());
1039 size_t namesz = strlen(name) + 1;
1040 size_t aligned_namesz = align_address(namesz, size / 8);
1041 size_t descsz = desc.length() + 1;
1042 size_t aligned_descsz = align_address(descsz, size / 8);
1043 const int note_type = 4;
1045 size_t notesz = 3 * (size / 8) + aligned_namesz + aligned_descsz;
1047 unsigned char buffer[128];
1048 gold_assert(sizeof buffer >= notesz);
1049 memset(buffer, 0, notesz);
1051 bool is_big_endian = parameters->is_big_endian();
1057 elfcpp::Swap<32, false>::writeval(buffer, namesz);
1058 elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
1059 elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
1063 elfcpp::Swap<32, true>::writeval(buffer, namesz);
1064 elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
1065 elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
1068 else if (size == 64)
1072 elfcpp::Swap<64, false>::writeval(buffer, namesz);
1073 elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
1074 elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
1078 elfcpp::Swap<64, true>::writeval(buffer, namesz);
1079 elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
1080 elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
1086 memcpy(buffer + 3 * (size / 8), name, namesz);
1087 memcpy(buffer + 3 * (size / 8) + aligned_namesz, desc.data(), descsz);
1089 const char* note_name = this->namepool_.add(".note", false, NULL);
1090 Output_section* os = this->make_output_section(note_name,
1093 Output_section_data* posd = new Output_data_const(buffer, notesz,
1095 os->add_output_section_data(posd);
1098 // Record whether the stack should be executable. This can be set
1099 // from the command line using the -z execstack or -z noexecstack
1100 // options. Otherwise, if any input file has a .note.GNU-stack
1101 // section with the SHF_EXECINSTR flag set, the stack should be
1102 // executable. Otherwise, if at least one input file a
1103 // .note.GNU-stack section, and some input file has no .note.GNU-stack
1104 // section, we use the target default for whether the stack should be
1105 // executable. Otherwise, we don't generate a stack note. When
1106 // generating a object file, we create a .note.GNU-stack section with
1107 // the appropriate marking. When generating an executable or shared
1108 // library, we create a PT_GNU_STACK segment.
1111 Layout::create_executable_stack_info(const Target* target)
1113 bool is_stack_executable;
1114 if (this->options_.is_execstack_set())
1115 is_stack_executable = this->options_.is_stack_executable();
1116 else if (!this->input_with_gnu_stack_note_)
1120 if (this->input_requires_executable_stack_)
1121 is_stack_executable = true;
1122 else if (this->input_without_gnu_stack_note_)
1123 is_stack_executable = target->is_default_stack_executable();
1125 is_stack_executable = false;
1128 if (parameters->output_is_object())
1130 const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
1131 elfcpp::Elf_Xword flags = 0;
1132 if (is_stack_executable)
1133 flags |= elfcpp::SHF_EXECINSTR;
1134 this->make_output_section(name, elfcpp::SHT_PROGBITS, flags);
1138 if (this->script_options_->saw_phdrs_clause())
1140 int flags = elfcpp::PF_R | elfcpp::PF_W;
1141 if (is_stack_executable)
1142 flags |= elfcpp::PF_X;
1143 this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
1147 // Return whether SEG1 should be before SEG2 in the output file. This
1148 // is based entirely on the segment type and flags. When this is
1149 // called the segment addresses has normally not yet been set.
1152 Layout::segment_precedes(const Output_segment* seg1,
1153 const Output_segment* seg2)
1155 elfcpp::Elf_Word type1 = seg1->type();
1156 elfcpp::Elf_Word type2 = seg2->type();
1158 // The single PT_PHDR segment is required to precede any loadable
1159 // segment. We simply make it always first.
1160 if (type1 == elfcpp::PT_PHDR)
1162 gold_assert(type2 != elfcpp::PT_PHDR);
1165 if (type2 == elfcpp::PT_PHDR)
1168 // The single PT_INTERP segment is required to precede any loadable
1169 // segment. We simply make it always second.
1170 if (type1 == elfcpp::PT_INTERP)
1172 gold_assert(type2 != elfcpp::PT_INTERP);
1175 if (type2 == elfcpp::PT_INTERP)
1178 // We then put PT_LOAD segments before any other segments.
1179 if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
1181 if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
1184 // We put the PT_TLS segment last, because that is where the dynamic
1185 // linker expects to find it (this is just for efficiency; other
1186 // positions would also work correctly).
1187 if (type1 == elfcpp::PT_TLS && type2 != elfcpp::PT_TLS)
1189 if (type2 == elfcpp::PT_TLS && type1 != elfcpp::PT_TLS)
1192 const elfcpp::Elf_Word flags1 = seg1->flags();
1193 const elfcpp::Elf_Word flags2 = seg2->flags();
1195 // The order of non-PT_LOAD segments is unimportant. We simply sort
1196 // by the numeric segment type and flags values. There should not
1197 // be more than one segment with the same type and flags.
1198 if (type1 != elfcpp::PT_LOAD)
1201 return type1 < type2;
1202 gold_assert(flags1 != flags2);
1203 return flags1 < flags2;
1206 // If the addresses are set already, sort by load address.
1207 if (seg1->are_addresses_set())
1209 if (!seg2->are_addresses_set())
1212 unsigned int section_count1 = seg1->output_section_count();
1213 unsigned int section_count2 = seg2->output_section_count();
1214 if (section_count1 == 0 && section_count2 > 0)
1216 if (section_count1 > 0 && section_count2 == 0)
1219 uint64_t paddr1 = seg1->first_section_load_address();
1220 uint64_t paddr2 = seg2->first_section_load_address();
1221 if (paddr1 != paddr2)
1222 return paddr1 < paddr2;
1224 else if (seg2->are_addresses_set())
1227 // We sort PT_LOAD segments based on the flags. Readonly segments
1228 // come before writable segments. Then executable segments come
1229 // before non-executable segments. Then the unlikely case of a
1230 // non-readable segment comes before the normal case of a readable
1231 // segment. If there are multiple segments with the same type and
1232 // flags, we require that the address be set, and we sort by
1233 // virtual address and then physical address.
1234 if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
1235 return (flags1 & elfcpp::PF_W) == 0;
1236 if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
1237 return (flags1 & elfcpp::PF_X) != 0;
1238 if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
1239 return (flags1 & elfcpp::PF_R) == 0;
1241 // We shouldn't get here--we shouldn't create segments which we
1242 // can't distinguish.
1246 // Set the file offsets of all the segments, and all the sections they
1247 // contain. They have all been created. LOAD_SEG must be be laid out
1248 // first. Return the offset of the data to follow.
1251 Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
1252 unsigned int *pshndx)
1254 // Sort them into the final order.
1255 std::sort(this->segment_list_.begin(), this->segment_list_.end(),
1256 Layout::Compare_segments());
1258 // Find the PT_LOAD segments, and set their addresses and offsets
1259 // and their section's addresses and offsets.
1261 if (this->options_.user_set_text_segment_address())
1262 addr = options_.text_segment_address();
1263 else if (parameters->output_is_shared())
1266 addr = target->default_text_segment_address();
1269 // If LOAD_SEG is NULL, then the file header and segment headers
1270 // will not be loadable. But they still need to be at offset 0 in
1271 // the file. Set their offsets now.
1272 if (load_seg == NULL)
1274 for (Data_list::iterator p = this->special_output_list_.begin();
1275 p != this->special_output_list_.end();
1278 off = align_address(off, (*p)->addralign());
1279 (*p)->set_address_and_file_offset(0, off);
1280 off += (*p)->data_size();
1284 bool was_readonly = false;
1285 for (Segment_list::iterator p = this->segment_list_.begin();
1286 p != this->segment_list_.end();
1289 if ((*p)->type() == elfcpp::PT_LOAD)
1291 if (load_seg != NULL && load_seg != *p)
1295 uint64_t orig_addr = addr;
1296 uint64_t orig_off = off;
1298 uint64_t aligned_addr = 0;
1299 uint64_t abi_pagesize = target->abi_pagesize();
1301 // FIXME: This should depend on the -n and -N options.
1302 (*p)->set_minimum_p_align(target->common_pagesize());
1304 bool are_addresses_set = (*p)->are_addresses_set();
1305 if (are_addresses_set)
1307 // When it comes to setting file offsets, we care about
1308 // the physical address.
1309 addr = (*p)->paddr();
1311 // Adjust the file offset to the same address modulo the
1313 uint64_t unsigned_off = off;
1314 uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
1315 | (addr & (abi_pagesize - 1)));
1316 if (aligned_off < unsigned_off)
1317 aligned_off += abi_pagesize;
1322 // If the last segment was readonly, and this one is
1323 // not, then skip the address forward one page,
1324 // maintaining the same position within the page. This
1325 // lets us store both segments overlapping on a single
1326 // page in the file, but the loader will put them on
1327 // different pages in memory.
1329 addr = align_address(addr, (*p)->maximum_alignment());
1330 aligned_addr = addr;
1332 if (was_readonly && ((*p)->flags() & elfcpp::PF_W) != 0)
1334 if ((addr & (abi_pagesize - 1)) != 0)
1335 addr = addr + abi_pagesize;
1338 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1341 unsigned int shndx_hold = *pshndx;
1342 uint64_t new_addr = (*p)->set_section_addresses(false, addr, &off,
1345 // Now that we know the size of this segment, we may be able
1346 // to save a page in memory, at the cost of wasting some
1347 // file space, by instead aligning to the start of a new
1348 // page. Here we use the real machine page size rather than
1349 // the ABI mandated page size.
1351 if (!are_addresses_set && aligned_addr != addr)
1353 uint64_t common_pagesize = target->common_pagesize();
1354 uint64_t first_off = (common_pagesize
1356 & (common_pagesize - 1)));
1357 uint64_t last_off = new_addr & (common_pagesize - 1);
1360 && ((aligned_addr & ~ (common_pagesize - 1))
1361 != (new_addr & ~ (common_pagesize - 1)))
1362 && first_off + last_off <= common_pagesize)
1364 *pshndx = shndx_hold;
1365 addr = align_address(aligned_addr, common_pagesize);
1366 addr = align_address(addr, (*p)->maximum_alignment());
1367 off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
1368 new_addr = (*p)->set_section_addresses(true, addr, &off,
1375 if (((*p)->flags() & elfcpp::PF_W) == 0)
1376 was_readonly = true;
1380 // Handle the non-PT_LOAD segments, setting their offsets from their
1381 // section's offsets.
1382 for (Segment_list::iterator p = this->segment_list_.begin();
1383 p != this->segment_list_.end();
1386 if ((*p)->type() != elfcpp::PT_LOAD)
1390 // Set the TLS offsets for each section in the PT_TLS segment.
1391 if (this->tls_segment_ != NULL)
1392 this->tls_segment_->set_tls_offsets();
1397 // Set the offsets of all the allocated sections when doing a
1398 // relocatable link. This does the same jobs as set_segment_offsets,
1399 // only for a relocatable link.
1402 Layout::set_relocatable_section_offsets(Output_data* file_header,
1403 unsigned int *pshndx)
1407 file_header->set_address_and_file_offset(0, 0);
1408 off += file_header->data_size();
1410 for (Section_list::iterator p = this->section_list_.begin();
1411 p != this->section_list_.end();
1414 // We skip unallocated sections here, except that group sections
1415 // have to come first.
1416 if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
1417 && (*p)->type() != elfcpp::SHT_GROUP)
1420 off = align_address(off, (*p)->addralign());
1422 // The linker script might have set the address.
1423 if (!(*p)->is_address_valid())
1424 (*p)->set_address(0);
1425 (*p)->set_file_offset(off);
1426 (*p)->finalize_data_size();
1427 off += (*p)->data_size();
1429 (*p)->set_out_shndx(*pshndx);
1436 // Set the file offset of all the sections not associated with a
1440 Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
1442 for (Section_list::iterator p = this->unattached_section_list_.begin();
1443 p != this->unattached_section_list_.end();
1446 // The symtab section is handled in create_symtab_sections.
1447 if (*p == this->symtab_section_)
1450 // If we've already set the data size, don't set it again.
1451 if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
1454 if (pass == BEFORE_INPUT_SECTIONS_PASS
1455 && (*p)->requires_postprocessing())
1457 (*p)->create_postprocessing_buffer();
1458 this->any_postprocessing_sections_ = true;
1461 if (pass == BEFORE_INPUT_SECTIONS_PASS
1462 && (*p)->after_input_sections())
1464 else if (pass == POSTPROCESSING_SECTIONS_PASS
1465 && (!(*p)->after_input_sections()
1466 || (*p)->type() == elfcpp::SHT_STRTAB))
1468 else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
1469 && (!(*p)->after_input_sections()
1470 || (*p)->type() != elfcpp::SHT_STRTAB))
1473 off = align_address(off, (*p)->addralign());
1474 (*p)->set_file_offset(off);
1475 (*p)->finalize_data_size();
1476 off += (*p)->data_size();
1478 // At this point the name must be set.
1479 if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
1480 this->namepool_.add((*p)->name(), false, NULL);
1485 // Set the section indexes of all the sections not associated with a
1489 Layout::set_section_indexes(unsigned int shndx)
1491 const bool output_is_object = parameters->output_is_object();
1492 for (Section_list::iterator p = this->unattached_section_list_.begin();
1493 p != this->unattached_section_list_.end();
1496 // In a relocatable link, we already did group sections.
1497 if (output_is_object
1498 && (*p)->type() == elfcpp::SHT_GROUP)
1501 (*p)->set_out_shndx(shndx);
1507 // Set the section addresses according to the linker script. This is
1508 // only called when we see a SECTIONS clause. This returns the
1509 // program segment which should hold the file header and segment
1510 // headers, if any. It will return NULL if they should not be in a
1514 Layout::set_section_addresses_from_script(Symbol_table* symtab)
1516 Script_sections* ss = this->script_options_->script_sections();
1517 gold_assert(ss->saw_sections_clause());
1519 // Place each orphaned output section in the script.
1520 for (Section_list::iterator p = this->section_list_.begin();
1521 p != this->section_list_.end();
1524 if (!(*p)->found_in_sections_clause())
1525 ss->place_orphan(*p);
1528 return this->script_options_->set_section_addresses(symtab, this);
1531 // Count the local symbols in the regular symbol table and the dynamic
1532 // symbol table, and build the respective string pools.
1535 Layout::count_local_symbols(const Task* task,
1536 const Input_objects* input_objects)
1538 // First, figure out an upper bound on the number of symbols we'll
1539 // be inserting into each pool. This helps us create the pools with
1540 // the right size, to avoid unnecessary hashtable resizing.
1541 unsigned int symbol_count = 0;
1542 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1543 p != input_objects->relobj_end();
1545 symbol_count += (*p)->local_symbol_count();
1547 // Go from "upper bound" to "estimate." We overcount for two
1548 // reasons: we double-count symbols that occur in more than one
1549 // object file, and we count symbols that are dropped from the
1550 // output. Add it all together and assume we overcount by 100%.
1553 // We assume all symbols will go into both the sympool and dynpool.
1554 this->sympool_.reserve(symbol_count);
1555 this->dynpool_.reserve(symbol_count);
1557 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1558 p != input_objects->relobj_end();
1561 Task_lock_obj<Object> tlo(task, *p);
1562 (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
1566 // Create the symbol table sections. Here we also set the final
1567 // values of the symbols. At this point all the loadable sections are
1571 Layout::create_symtab_sections(const Input_objects* input_objects,
1572 Symbol_table* symtab,
1577 if (parameters->get_size() == 32)
1579 symsize = elfcpp::Elf_sizes<32>::sym_size;
1582 else if (parameters->get_size() == 64)
1584 symsize = elfcpp::Elf_sizes<64>::sym_size;
1591 off = align_address(off, align);
1592 off_t startoff = off;
1594 // Save space for the dummy symbol at the start of the section. We
1595 // never bother to write this out--it will just be left as zero.
1597 unsigned int local_symbol_index = 1;
1599 // Add STT_SECTION symbols for each Output section which needs one.
1600 for (Section_list::iterator p = this->section_list_.begin();
1601 p != this->section_list_.end();
1604 if (!(*p)->needs_symtab_index())
1605 (*p)->set_symtab_index(-1U);
1608 (*p)->set_symtab_index(local_symbol_index);
1609 ++local_symbol_index;
1614 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1615 p != input_objects->relobj_end();
1618 unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
1620 off += (index - local_symbol_index) * symsize;
1621 local_symbol_index = index;
1624 unsigned int local_symcount = local_symbol_index;
1625 gold_assert(local_symcount * symsize == off - startoff);
1628 size_t dyn_global_index;
1630 if (this->dynsym_section_ == NULL)
1633 dyn_global_index = 0;
1638 dyn_global_index = this->dynsym_section_->info();
1639 off_t locsize = dyn_global_index * this->dynsym_section_->entsize();
1640 dynoff = this->dynsym_section_->offset() + locsize;
1641 dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
1642 gold_assert(static_cast<off_t>(dyncount * symsize)
1643 == this->dynsym_section_->data_size() - locsize);
1646 off = symtab->finalize(off, dynoff, dyn_global_index, dyncount,
1647 &this->sympool_, &local_symcount);
1649 if (!parameters->strip_all())
1651 this->sympool_.set_string_offsets();
1653 const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
1654 Output_section* osymtab = this->make_output_section(symtab_name,
1657 this->symtab_section_ = osymtab;
1659 Output_section_data* pos = new Output_data_fixed_space(off - startoff,
1661 osymtab->add_output_section_data(pos);
1663 const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
1664 Output_section* ostrtab = this->make_output_section(strtab_name,
1668 Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
1669 ostrtab->add_output_section_data(pstr);
1671 osymtab->set_file_offset(startoff);
1672 osymtab->finalize_data_size();
1673 osymtab->set_link_section(ostrtab);
1674 osymtab->set_info(local_symcount);
1675 osymtab->set_entsize(symsize);
1681 // Create the .shstrtab section, which holds the names of the
1682 // sections. At the time this is called, we have created all the
1683 // output sections except .shstrtab itself.
1686 Layout::create_shstrtab()
1688 // FIXME: We don't need to create a .shstrtab section if we are
1689 // stripping everything.
1691 const char* name = this->namepool_.add(".shstrtab", false, NULL);
1693 Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0);
1695 // We can't write out this section until we've set all the section
1696 // names, and we don't set the names of compressed output sections
1697 // until relocations are complete.
1698 os->set_after_input_sections();
1700 Output_section_data* posd = new Output_data_strtab(&this->namepool_);
1701 os->add_output_section_data(posd);
1706 // Create the section headers. SIZE is 32 or 64. OFF is the file
1710 Layout::create_shdrs(off_t* poff)
1712 Output_section_headers* oshdrs;
1713 oshdrs = new Output_section_headers(this,
1714 &this->segment_list_,
1715 &this->section_list_,
1716 &this->unattached_section_list_,
1718 off_t off = align_address(*poff, oshdrs->addralign());
1719 oshdrs->set_address_and_file_offset(0, off);
1720 off += oshdrs->data_size();
1722 this->section_headers_ = oshdrs;
1725 // Create the dynamic symbol table.
1728 Layout::create_dynamic_symtab(const Input_objects* input_objects,
1729 Symbol_table* symtab,
1730 Output_section **pdynstr,
1731 unsigned int* plocal_dynamic_count,
1732 std::vector<Symbol*>* pdynamic_symbols,
1733 Versions* pversions)
1735 // Count all the symbols in the dynamic symbol table, and set the
1736 // dynamic symbol indexes.
1738 // Skip symbol 0, which is always all zeroes.
1739 unsigned int index = 1;
1741 // Add STT_SECTION symbols for each Output section which needs one.
1742 for (Section_list::iterator p = this->section_list_.begin();
1743 p != this->section_list_.end();
1746 if (!(*p)->needs_dynsym_index())
1747 (*p)->set_dynsym_index(-1U);
1750 (*p)->set_dynsym_index(index);
1755 // Count the local symbols that need to go in the dynamic symbol table,
1756 // and set the dynamic symbol indexes.
1757 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1758 p != input_objects->relobj_end();
1761 unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
1765 unsigned int local_symcount = index;
1766 *plocal_dynamic_count = local_symcount;
1768 // FIXME: We have to tell set_dynsym_indexes whether the
1769 // -E/--export-dynamic option was used.
1770 index = symtab->set_dynsym_indexes(index, pdynamic_symbols,
1771 &this->dynpool_, pversions);
1775 const int size = parameters->get_size();
1778 symsize = elfcpp::Elf_sizes<32>::sym_size;
1781 else if (size == 64)
1783 symsize = elfcpp::Elf_sizes<64>::sym_size;
1789 // Create the dynamic symbol table section.
1791 Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
1796 Output_section_data* odata = new Output_data_fixed_space(index * symsize,
1798 dynsym->add_output_section_data(odata);
1800 dynsym->set_info(local_symcount);
1801 dynsym->set_entsize(symsize);
1802 dynsym->set_addralign(align);
1804 this->dynsym_section_ = dynsym;
1806 Output_data_dynamic* const odyn = this->dynamic_data_;
1807 odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
1808 odyn->add_constant(elfcpp::DT_SYMENT, symsize);
1810 // Create the dynamic string table section.
1812 Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
1817 Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
1818 dynstr->add_output_section_data(strdata);
1820 dynsym->set_link_section(dynstr);
1821 this->dynamic_section_->set_link_section(dynstr);
1823 odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
1824 odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
1828 // Create the hash tables.
1830 // FIXME: We need an option to create a GNU hash table.
1832 unsigned char* phash;
1833 unsigned int hashlen;
1834 Dynobj::create_elf_hash_table(*pdynamic_symbols, local_symcount,
1837 Output_section* hashsec = this->choose_output_section(NULL, ".hash",
1842 Output_section_data* hashdata = new Output_data_const_buffer(phash,
1845 hashsec->add_output_section_data(hashdata);
1847 hashsec->set_link_section(dynsym);
1848 hashsec->set_entsize(4);
1850 odyn->add_section_address(elfcpp::DT_HASH, hashsec);
1853 // Assign offsets to each local portion of the dynamic symbol table.
1856 Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
1858 Output_section* dynsym = this->dynsym_section_;
1859 gold_assert(dynsym != NULL);
1861 off_t off = dynsym->offset();
1863 // Skip the dummy symbol at the start of the section.
1864 off += dynsym->entsize();
1866 for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
1867 p != input_objects->relobj_end();
1870 unsigned int count = (*p)->set_local_dynsym_offset(off);
1871 off += count * dynsym->entsize();
1875 // Create the version sections.
1878 Layout::create_version_sections(const Versions* versions,
1879 const Symbol_table* symtab,
1880 unsigned int local_symcount,
1881 const std::vector<Symbol*>& dynamic_symbols,
1882 const Output_section* dynstr)
1884 if (!versions->any_defs() && !versions->any_needs())
1887 if (parameters->get_size() == 32)
1889 if (parameters->is_big_endian())
1891 #ifdef HAVE_TARGET_32_BIG
1892 this->sized_create_version_sections
1893 SELECT_SIZE_ENDIAN_NAME(32, true)(
1894 versions, symtab, local_symcount, dynamic_symbols, dynstr
1895 SELECT_SIZE_ENDIAN(32, true));
1902 #ifdef HAVE_TARGET_32_LITTLE
1903 this->sized_create_version_sections
1904 SELECT_SIZE_ENDIAN_NAME(32, false)(
1905 versions, symtab, local_symcount, dynamic_symbols, dynstr
1906 SELECT_SIZE_ENDIAN(32, false));
1912 else if (parameters->get_size() == 64)
1914 if (parameters->is_big_endian())
1916 #ifdef HAVE_TARGET_64_BIG
1917 this->sized_create_version_sections
1918 SELECT_SIZE_ENDIAN_NAME(64, true)(
1919 versions, symtab, local_symcount, dynamic_symbols, dynstr
1920 SELECT_SIZE_ENDIAN(64, true));
1927 #ifdef HAVE_TARGET_64_LITTLE
1928 this->sized_create_version_sections
1929 SELECT_SIZE_ENDIAN_NAME(64, false)(
1930 versions, symtab, local_symcount, dynamic_symbols, dynstr
1931 SELECT_SIZE_ENDIAN(64, false));
1941 // Create the version sections, sized version.
1943 template<int size, bool big_endian>
1945 Layout::sized_create_version_sections(
1946 const Versions* versions,
1947 const Symbol_table* symtab,
1948 unsigned int local_symcount,
1949 const std::vector<Symbol*>& dynamic_symbols,
1950 const Output_section* dynstr
1953 Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
1954 elfcpp::SHT_GNU_versym,
1958 unsigned char* vbuf;
1960 versions->symbol_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1961 symtab, &this->dynpool_, local_symcount, dynamic_symbols, &vbuf, &vsize
1962 SELECT_SIZE_ENDIAN(size, big_endian));
1964 Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2);
1966 vsec->add_output_section_data(vdata);
1967 vsec->set_entsize(2);
1968 vsec->set_link_section(this->dynsym_section_);
1970 Output_data_dynamic* const odyn = this->dynamic_data_;
1971 odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
1973 if (versions->any_defs())
1975 Output_section* vdsec;
1976 vdsec= this->choose_output_section(NULL, ".gnu.version_d",
1977 elfcpp::SHT_GNU_verdef,
1981 unsigned char* vdbuf;
1982 unsigned int vdsize;
1983 unsigned int vdentries;
1984 versions->def_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)(
1985 &this->dynpool_, &vdbuf, &vdsize, &vdentries
1986 SELECT_SIZE_ENDIAN(size, big_endian));
1988 Output_section_data* vddata = new Output_data_const_buffer(vdbuf,
1992 vdsec->add_output_section_data(vddata);
1993 vdsec->set_link_section(dynstr);
1994 vdsec->set_info(vdentries);
1996 odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
1997 odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
2000 if (versions->any_needs())
2002 Output_section* vnsec;
2003 vnsec = this->choose_output_section(NULL, ".gnu.version_r",
2004 elfcpp::SHT_GNU_verneed,
2008 unsigned char* vnbuf;
2009 unsigned int vnsize;
2010 unsigned int vnentries;
2011 versions->need_section_contents SELECT_SIZE_ENDIAN_NAME(size, big_endian)
2012 (&this->dynpool_, &vnbuf, &vnsize, &vnentries
2013 SELECT_SIZE_ENDIAN(size, big_endian));
2015 Output_section_data* vndata = new Output_data_const_buffer(vnbuf,
2019 vnsec->add_output_section_data(vndata);
2020 vnsec->set_link_section(dynstr);
2021 vnsec->set_info(vnentries);
2023 odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
2024 odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
2028 // Create the .interp section and PT_INTERP segment.
2031 Layout::create_interp(const Target* target)
2033 const char* interp = this->options_.dynamic_linker();
2036 interp = target->dynamic_linker();
2037 gold_assert(interp != NULL);
2040 size_t len = strlen(interp) + 1;
2042 Output_section_data* odata = new Output_data_const(interp, len, 1);
2044 Output_section* osec = this->choose_output_section(NULL, ".interp",
2045 elfcpp::SHT_PROGBITS,
2048 osec->add_output_section_data(odata);
2050 if (!this->script_options_->saw_phdrs_clause())
2052 Output_segment* oseg = this->make_output_segment(elfcpp::PT_INTERP,
2054 oseg->add_initial_output_section(osec, elfcpp::PF_R);
2058 // Finish the .dynamic section and PT_DYNAMIC segment.
2061 Layout::finish_dynamic_section(const Input_objects* input_objects,
2062 const Symbol_table* symtab)
2064 if (!this->script_options_->saw_phdrs_clause())
2066 Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
2069 oseg->add_initial_output_section(this->dynamic_section_,
2070 elfcpp::PF_R | elfcpp::PF_W);
2073 Output_data_dynamic* const odyn = this->dynamic_data_;
2075 for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
2076 p != input_objects->dynobj_end();
2079 // FIXME: Handle --as-needed.
2080 odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
2083 if (parameters->output_is_shared())
2085 const char* soname = this->options_.soname();
2087 odyn->add_string(elfcpp::DT_SONAME, soname);
2090 // FIXME: Support --init and --fini.
2091 Symbol* sym = symtab->lookup("_init");
2092 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2093 odyn->add_symbol(elfcpp::DT_INIT, sym);
2095 sym = symtab->lookup("_fini");
2096 if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
2097 odyn->add_symbol(elfcpp::DT_FINI, sym);
2099 // FIXME: Support DT_INIT_ARRAY and DT_FINI_ARRAY.
2101 // Add a DT_RPATH entry if needed.
2102 const General_options::Dir_list& rpath(this->options_.rpath());
2105 std::string rpath_val;
2106 for (General_options::Dir_list::const_iterator p = rpath.begin();
2110 if (rpath_val.empty())
2111 rpath_val = p->name();
2114 // Eliminate duplicates.
2115 General_options::Dir_list::const_iterator q;
2116 for (q = rpath.begin(); q != p; ++q)
2117 if (q->name() == p->name())
2122 rpath_val += p->name();
2127 odyn->add_string(elfcpp::DT_RPATH, rpath_val);
2130 // Look for text segments that have dynamic relocations.
2131 bool have_textrel = false;
2132 for (Segment_list::const_iterator p = this->segment_list_.begin();
2133 p != this->segment_list_.end();
2136 if (((*p)->flags() & elfcpp::PF_W) == 0
2137 && (*p)->dynamic_reloc_count() > 0)
2139 have_textrel = true;
2144 // Add a DT_FLAGS entry. We add it even if no flags are set so that
2145 // post-link tools can easily modify these flags if desired.
2146 unsigned int flags = 0;
2149 // Add a DT_TEXTREL for compatibility with older loaders.
2150 odyn->add_constant(elfcpp::DT_TEXTREL, 0);
2151 flags |= elfcpp::DF_TEXTREL;
2153 if (parameters->output_is_shared() && this->has_static_tls())
2154 flags |= elfcpp::DF_STATIC_TLS;
2155 odyn->add_constant(elfcpp::DT_FLAGS, flags);
2158 // The mapping of .gnu.linkonce section names to real section names.
2160 #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
2161 const Layout::Linkonce_mapping Layout::linkonce_mapping[] =
2163 MAPPING_INIT("d.rel.ro", ".data.rel.ro"), // Must be before "d".
2164 MAPPING_INIT("t", ".text"),
2165 MAPPING_INIT("r", ".rodata"),
2166 MAPPING_INIT("d", ".data"),
2167 MAPPING_INIT("b", ".bss"),
2168 MAPPING_INIT("s", ".sdata"),
2169 MAPPING_INIT("sb", ".sbss"),
2170 MAPPING_INIT("s2", ".sdata2"),
2171 MAPPING_INIT("sb2", ".sbss2"),
2172 MAPPING_INIT("wi", ".debug_info"),
2173 MAPPING_INIT("td", ".tdata"),
2174 MAPPING_INIT("tb", ".tbss"),
2175 MAPPING_INIT("lr", ".lrodata"),
2176 MAPPING_INIT("l", ".ldata"),
2177 MAPPING_INIT("lb", ".lbss"),
2181 const int Layout::linkonce_mapping_count =
2182 sizeof(Layout::linkonce_mapping) / sizeof(Layout::linkonce_mapping[0]);
2184 // Return the name of the output section to use for a .gnu.linkonce
2185 // section. This is based on the default ELF linker script of the old
2186 // GNU linker. For example, we map a name like ".gnu.linkonce.t.foo"
2187 // to ".text". Set *PLEN to the length of the name. *PLEN is
2188 // initialized to the length of NAME.
2191 Layout::linkonce_output_name(const char* name, size_t *plen)
2193 const char* s = name + sizeof(".gnu.linkonce") - 1;
2197 const Linkonce_mapping* plm = linkonce_mapping;
2198 for (int i = 0; i < linkonce_mapping_count; ++i, ++plm)
2200 if (strncmp(s, plm->from, plm->fromlen) == 0 && s[plm->fromlen] == '.')
2209 // Choose the output section name to use given an input section name.
2210 // Set *PLEN to the length of the name. *PLEN is initialized to the
2214 Layout::output_section_name(const char* name, size_t* plen)
2216 if (Layout::is_linkonce(name))
2218 // .gnu.linkonce sections are laid out as though they were named
2219 // for the sections are placed into.
2220 return Layout::linkonce_output_name(name, plen);
2223 // gcc 4.3 generates the following sorts of section names when it
2224 // needs a section name specific to a function:
2230 // .data.rel.local.FN
2232 // .data.rel.ro.local.FN
2239 // The GNU linker maps all of those to the part before the .FN,
2240 // except that .data.rel.local.FN is mapped to .data, and
2241 // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
2242 // beginning with .data.rel.ro.local are grouped together.
2244 // For an anonymous namespace, the string FN can contain a '.'.
2246 // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
2247 // GNU linker maps to .rodata.
2249 // The .data.rel.ro sections enable a security feature triggered by
2250 // the -z relro option. Section which need to be relocated at
2251 // program startup time but which may be readonly after startup are
2252 // grouped into .data.rel.ro. They are then put into a PT_GNU_RELRO
2253 // segment. The dynamic linker will make that segment writable,
2254 // perform relocations, and then make it read-only. FIXME: We do
2255 // not yet implement this optimization.
2257 // It is hard to handle this in a principled way.
2259 // These are the rules we follow:
2261 // If the section name has no initial '.', or no dot other than an
2262 // initial '.', we use the name unchanged (i.e., "mysection" and
2263 // ".text" are unchanged).
2265 // If the name starts with ".data.rel.ro" we use ".data.rel.ro".
2267 // Otherwise, we drop the second '.' and everything that comes after
2268 // it (i.e., ".text.XXX" becomes ".text").
2270 const char* s = name;
2274 const char* sdot = strchr(s, '.');
2278 const char* const data_rel_ro = ".data.rel.ro";
2279 if (strncmp(name, data_rel_ro, strlen(data_rel_ro)) == 0)
2281 *plen = strlen(data_rel_ro);
2285 *plen = sdot - name;
2289 // Record the signature of a comdat section, and return whether to
2290 // include it in the link. If GROUP is true, this is a regular
2291 // section group. If GROUP is false, this is a group signature
2292 // derived from the name of a linkonce section. We want linkonce
2293 // signatures and group signatures to block each other, but we don't
2294 // want a linkonce signature to block another linkonce signature.
2297 Layout::add_comdat(const char* signature, bool group)
2299 std::string sig(signature);
2300 std::pair<Signatures::iterator, bool> ins(
2301 this->signatures_.insert(std::make_pair(sig, group)));
2305 // This is the first time we've seen this signature.
2309 if (ins.first->second)
2311 // We've already seen a real section group with this signature.
2316 // This is a real section group, and we've already seen a
2317 // linkonce section with this signature. Record that we've seen
2318 // a section group, and don't include this section group.
2319 ins.first->second = true;
2324 // We've already seen a linkonce section and this is a linkonce
2325 // section. These don't block each other--this may be the same
2326 // symbol name with different section types.
2331 // Store the allocated sections into the section list.
2334 Layout::get_allocated_sections(Section_list* section_list) const
2336 for (Section_list::const_iterator p = this->section_list_.begin();
2337 p != this->section_list_.end();
2339 if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
2340 section_list->push_back(*p);
2343 // Create an output segment.
2346 Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
2348 gold_assert(!parameters->output_is_object());
2349 Output_segment* oseg = new Output_segment(type, flags);
2350 this->segment_list_.push_back(oseg);
2354 // Write out the Output_sections. Most won't have anything to write,
2355 // since most of the data will come from input sections which are
2356 // handled elsewhere. But some Output_sections do have Output_data.
2359 Layout::write_output_sections(Output_file* of) const
2361 for (Section_list::const_iterator p = this->section_list_.begin();
2362 p != this->section_list_.end();
2365 if (!(*p)->after_input_sections())
2370 // Write out data not associated with a section or the symbol table.
2373 Layout::write_data(const Symbol_table* symtab, Output_file* of) const
2375 if (!parameters->strip_all())
2377 const Output_section* symtab_section = this->symtab_section_;
2378 for (Section_list::const_iterator p = this->section_list_.begin();
2379 p != this->section_list_.end();
2382 if ((*p)->needs_symtab_index())
2384 gold_assert(symtab_section != NULL);
2385 unsigned int index = (*p)->symtab_index();
2386 gold_assert(index > 0 && index != -1U);
2387 off_t off = (symtab_section->offset()
2388 + index * symtab_section->entsize());
2389 symtab->write_section_symbol(*p, of, off);
2394 const Output_section* dynsym_section = this->dynsym_section_;
2395 for (Section_list::const_iterator p = this->section_list_.begin();
2396 p != this->section_list_.end();
2399 if ((*p)->needs_dynsym_index())
2401 gold_assert(dynsym_section != NULL);
2402 unsigned int index = (*p)->dynsym_index();
2403 gold_assert(index > 0 && index != -1U);
2404 off_t off = (dynsym_section->offset()
2405 + index * dynsym_section->entsize());
2406 symtab->write_section_symbol(*p, of, off);
2410 // Write out the Output_data which are not in an Output_section.
2411 for (Data_list::const_iterator p = this->special_output_list_.begin();
2412 p != this->special_output_list_.end();
2417 // Write out the Output_sections which can only be written after the
2418 // input sections are complete.
2421 Layout::write_sections_after_input_sections(Output_file* of)
2423 // Determine the final section offsets, and thus the final output
2424 // file size. Note we finalize the .shstrab last, to allow the
2425 // after_input_section sections to modify their section-names before
2427 if (this->any_postprocessing_sections_)
2429 off_t off = this->output_file_size_;
2430 off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
2432 // Now that we've finalized the names, we can finalize the shstrab.
2434 this->set_section_offsets(off,
2435 STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
2437 if (off > this->output_file_size_)
2440 this->output_file_size_ = off;
2444 for (Section_list::const_iterator p = this->section_list_.begin();
2445 p != this->section_list_.end();
2448 if ((*p)->after_input_sections())
2452 this->section_headers_->write(of);
2455 // Print statistical information to stderr. This is used for --stats.
2458 Layout::print_stats() const
2460 this->namepool_.print_stats("section name pool");
2461 this->sympool_.print_stats("output symbol name pool");
2462 this->dynpool_.print_stats("dynamic name pool");
2464 for (Section_list::const_iterator p = this->section_list_.begin();
2465 p != this->section_list_.end();
2467 (*p)->print_merge_stats();
2470 // Write_sections_task methods.
2472 // We can always run this task.
2475 Write_sections_task::is_runnable()
2480 // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
2484 Write_sections_task::locks(Task_locker* tl)
2486 tl->add(this, this->output_sections_blocker_);
2487 tl->add(this, this->final_blocker_);
2490 // Run the task--write out the data.
2493 Write_sections_task::run(Workqueue*)
2495 this->layout_->write_output_sections(this->of_);
2498 // Write_data_task methods.
2500 // We can always run this task.
2503 Write_data_task::is_runnable()
2508 // We need to unlock FINAL_BLOCKER when finished.
2511 Write_data_task::locks(Task_locker* tl)
2513 tl->add(this, this->final_blocker_);
2516 // Run the task--write out the data.
2519 Write_data_task::run(Workqueue*)
2521 this->layout_->write_data(this->symtab_, this->of_);
2524 // Write_symbols_task methods.
2526 // We can always run this task.
2529 Write_symbols_task::is_runnable()
2534 // We need to unlock FINAL_BLOCKER when finished.
2537 Write_symbols_task::locks(Task_locker* tl)
2539 tl->add(this, this->final_blocker_);
2542 // Run the task--write out the symbols.
2545 Write_symbols_task::run(Workqueue*)
2547 this->symtab_->write_globals(this->input_objects_, this->sympool_,
2548 this->dynpool_, this->of_);
2551 // Write_after_input_sections_task methods.
2553 // We can only run this task after the input sections have completed.
2556 Write_after_input_sections_task::is_runnable()
2558 if (this->input_sections_blocker_->is_blocked())
2559 return this->input_sections_blocker_;
2563 // We need to unlock FINAL_BLOCKER when finished.
2566 Write_after_input_sections_task::locks(Task_locker* tl)
2568 tl->add(this, this->final_blocker_);
2574 Write_after_input_sections_task::run(Workqueue*)
2576 this->layout_->write_sections_after_input_sections(this->of_);
2579 // Close_task_runner methods.
2581 // Run the task--close the file.
2584 Close_task_runner::run(Workqueue*, const Task*)
2589 // Instantiate the templates we need. We could use the configure
2590 // script to restrict this to only the ones for implemented targets.
2592 #ifdef HAVE_TARGET_32_LITTLE
2595 Layout::layout<32, false>(Sized_relobj<32, false>* object, unsigned int shndx,
2597 const elfcpp::Shdr<32, false>& shdr,
2598 unsigned int, unsigned int, off_t*);
2601 #ifdef HAVE_TARGET_32_BIG
2604 Layout::layout<32, true>(Sized_relobj<32, true>* object, unsigned int shndx,
2606 const elfcpp::Shdr<32, true>& shdr,
2607 unsigned int, unsigned int, off_t*);
2610 #ifdef HAVE_TARGET_64_LITTLE
2613 Layout::layout<64, false>(Sized_relobj<64, false>* object, unsigned int shndx,
2615 const elfcpp::Shdr<64, false>& shdr,
2616 unsigned int, unsigned int, off_t*);
2619 #ifdef HAVE_TARGET_64_BIG
2622 Layout::layout<64, true>(Sized_relobj<64, true>* object, unsigned int shndx,
2624 const elfcpp::Shdr<64, true>& shdr,
2625 unsigned int, unsigned int, off_t*);
2628 #ifdef HAVE_TARGET_32_LITTLE
2631 Layout::layout_reloc<32, false>(Sized_relobj<32, false>* object,
2632 unsigned int reloc_shndx,
2633 const elfcpp::Shdr<32, false>& shdr,
2634 Output_section* data_section,
2635 Relocatable_relocs* rr);
2638 #ifdef HAVE_TARGET_32_BIG
2641 Layout::layout_reloc<32, true>(Sized_relobj<32, true>* object,
2642 unsigned int reloc_shndx,
2643 const elfcpp::Shdr<32, true>& shdr,
2644 Output_section* data_section,
2645 Relocatable_relocs* rr);
2648 #ifdef HAVE_TARGET_64_LITTLE
2651 Layout::layout_reloc<64, false>(Sized_relobj<64, false>* object,
2652 unsigned int reloc_shndx,
2653 const elfcpp::Shdr<64, false>& shdr,
2654 Output_section* data_section,
2655 Relocatable_relocs* rr);
2658 #ifdef HAVE_TARGET_64_BIG
2661 Layout::layout_reloc<64, true>(Sized_relobj<64, true>* object,
2662 unsigned int reloc_shndx,
2663 const elfcpp::Shdr<64, true>& shdr,
2664 Output_section* data_section,
2665 Relocatable_relocs* rr);
2668 #ifdef HAVE_TARGET_32_LITTLE
2671 Layout::layout_group<32, false>(Symbol_table* symtab,
2672 Sized_relobj<32, false>* object,
2674 const char* group_section_name,
2675 const char* signature,
2676 const elfcpp::Shdr<32, false>& shdr,
2677 const elfcpp::Elf_Word* contents);
2680 #ifdef HAVE_TARGET_32_BIG
2683 Layout::layout_group<32, true>(Symbol_table* symtab,
2684 Sized_relobj<32, true>* object,
2686 const char* group_section_name,
2687 const char* signature,
2688 const elfcpp::Shdr<32, true>& shdr,
2689 const elfcpp::Elf_Word* contents);
2692 #ifdef HAVE_TARGET_64_LITTLE
2695 Layout::layout_group<64, false>(Symbol_table* symtab,
2696 Sized_relobj<64, false>* object,
2698 const char* group_section_name,
2699 const char* signature,
2700 const elfcpp::Shdr<64, false>& shdr,
2701 const elfcpp::Elf_Word* contents);
2704 #ifdef HAVE_TARGET_64_BIG
2707 Layout::layout_group<64, true>(Symbol_table* symtab,
2708 Sized_relobj<64, true>* object,
2710 const char* group_section_name,
2711 const char* signature,
2712 const elfcpp::Shdr<64, true>& shdr,
2713 const elfcpp::Elf_Word* contents);
2716 #ifdef HAVE_TARGET_32_LITTLE
2719 Layout::layout_eh_frame<32, false>(Sized_relobj<32, false>* object,
2720 const unsigned char* symbols,
2722 const unsigned char* symbol_names,
2723 off_t symbol_names_size,
2725 const elfcpp::Shdr<32, false>& shdr,
2726 unsigned int reloc_shndx,
2727 unsigned int reloc_type,
2731 #ifdef HAVE_TARGET_32_BIG
2734 Layout::layout_eh_frame<32, true>(Sized_relobj<32, true>* object,
2735 const unsigned char* symbols,
2737 const unsigned char* symbol_names,
2738 off_t symbol_names_size,
2740 const elfcpp::Shdr<32, true>& shdr,
2741 unsigned int reloc_shndx,
2742 unsigned int reloc_type,
2746 #ifdef HAVE_TARGET_64_LITTLE
2749 Layout::layout_eh_frame<64, false>(Sized_relobj<64, false>* object,
2750 const unsigned char* symbols,
2752 const unsigned char* symbol_names,
2753 off_t symbol_names_size,
2755 const elfcpp::Shdr<64, false>& shdr,
2756 unsigned int reloc_shndx,
2757 unsigned int reloc_type,
2761 #ifdef HAVE_TARGET_64_BIG
2764 Layout::layout_eh_frame<64, true>(Sized_relobj<64, true>* object,
2765 const unsigned char* symbols,
2767 const unsigned char* symbol_names,
2768 off_t symbol_names_size,
2770 const elfcpp::Shdr<64, true>& shdr,
2771 unsigned int reloc_shndx,
2772 unsigned int reloc_type,
2776 } // End namespace gold.