9 kx // layout.cc -- lay out output file sections for gold
9 kx
9 kx // Copyright (C) 2006-2023 Free Software Foundation, Inc.
9 kx // Written by Ian Lance Taylor <iant@google.com>.
9 kx
9 kx // This file is part of gold.
9 kx
9 kx // This program is free software; you can redistribute it and/or modify
9 kx // it under the terms of the GNU General Public License as published by
9 kx // the Free Software Foundation; either version 3 of the License, or
9 kx // (at your option) any later version.
9 kx
9 kx // This program is distributed in the hope that it will be useful,
9 kx // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 kx // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 kx // GNU General Public License for more details.
9 kx
9 kx // You should have received a copy of the GNU General Public License
9 kx // along with this program; if not, write to the Free Software
9 kx // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
9 kx // MA 02110-1301, USA.
9 kx
9 kx #include "gold.h"
9 kx
9 kx #include <cerrno>
9 kx #include <cstring>
9 kx #include <algorithm>
9 kx #include <iostream>
9 kx #include <fstream>
9 kx #include <utility>
9 kx #include <fcntl.h>
9 kx #include <fnmatch.h>
9 kx #include <unistd.h>
9 kx #include "libiberty.h"
9 kx #include "md5.h"
9 kx #include "sha1.h"
9 kx #ifdef __MINGW32__
9 kx #include <windows.h>
9 kx #include <rpcdce.h>
9 kx #endif
9 kx #ifdef HAVE_JANSSON
9 kx #include <jansson.h>
9 kx #endif
9 kx
9 kx #include "parameters.h"
9 kx #include "options.h"
9 kx #include "mapfile.h"
9 kx #include "script.h"
9 kx #include "script-sections.h"
9 kx #include "output.h"
9 kx #include "symtab.h"
9 kx #include "dynobj.h"
9 kx #include "ehframe.h"
9 kx #include "gdb-index.h"
9 kx #include "compressed_output.h"
9 kx #include "reduced_debug_output.h"
9 kx #include "object.h"
9 kx #include "reloc.h"
9 kx #include "descriptors.h"
9 kx #include "plugin.h"
9 kx #include "incremental.h"
9 kx #include "layout.h"
9 kx
9 kx namespace gold
9 kx {
9 kx
9 kx // Class Free_list.
9 kx
9 kx // The total number of free lists used.
9 kx unsigned int Free_list::num_lists = 0;
9 kx // The total number of free list nodes used.
9 kx unsigned int Free_list::num_nodes = 0;
9 kx // The total number of calls to Free_list::remove.
9 kx unsigned int Free_list::num_removes = 0;
9 kx // The total number of nodes visited during calls to Free_list::remove.
9 kx unsigned int Free_list::num_remove_visits = 0;
9 kx // The total number of calls to Free_list::allocate.
9 kx unsigned int Free_list::num_allocates = 0;
9 kx // The total number of nodes visited during calls to Free_list::allocate.
9 kx unsigned int Free_list::num_allocate_visits = 0;
9 kx
9 kx // Initialize the free list. Creates a single free list node that
9 kx // describes the entire region of length LEN. If EXTEND is true,
9 kx // allocate() is allowed to extend the region beyond its initial
9 kx // length.
9 kx
9 kx void
9 kx Free_list::init(off_t len, bool extend)
9 kx {
9 kx this->list_.push_front(Free_list_node(0, len));
9 kx this->last_remove_ = this->list_.begin();
9 kx this->extend_ = extend;
9 kx this->length_ = len;
9 kx ++Free_list::num_lists;
9 kx ++Free_list::num_nodes;
9 kx }
9 kx
9 kx // Remove a chunk from the free list. Because we start with a single
9 kx // node that covers the entire section, and remove chunks from it one
9 kx // at a time, we do not need to coalesce chunks or handle cases that
9 kx // span more than one free node. We expect to remove chunks from the
9 kx // free list in order, and we expect to have only a few chunks of free
9 kx // space left (corresponding to files that have changed since the last
9 kx // incremental link), so a simple linear list should provide sufficient
9 kx // performance.
9 kx
9 kx void
9 kx Free_list::remove(off_t start, off_t end)
9 kx {
9 kx if (start == end)
9 kx return;
9 kx gold_assert(start < end);
9 kx
9 kx ++Free_list::num_removes;
9 kx
9 kx Iterator p = this->last_remove_;
9 kx if (p->start_ > start)
9 kx p = this->list_.begin();
9 kx
9 kx for (; p != this->list_.end(); ++p)
9 kx {
9 kx ++Free_list::num_remove_visits;
9 kx // Find a node that wholly contains the indicated region.
9 kx if (p->start_ <= start && p->end_ >= end)
9 kx {
9 kx // Case 1: the indicated region spans the whole node.
9 kx // Add some fuzz to avoid creating tiny free chunks.
9 kx if (p->start_ + 3 >= start && p->end_ <= end + 3)
9 kx p = this->list_.erase(p);
9 kx // Case 2: remove a chunk from the start of the node.
9 kx else if (p->start_ + 3 >= start)
9 kx p->start_ = end;
9 kx // Case 3: remove a chunk from the end of the node.
9 kx else if (p->end_ <= end + 3)
9 kx p->end_ = start;
9 kx // Case 4: remove a chunk from the middle, and split
9 kx // the node into two.
9 kx else
9 kx {
9 kx Free_list_node newnode(p->start_, start);
9 kx p->start_ = end;
9 kx this->list_.insert(p, newnode);
9 kx ++Free_list::num_nodes;
9 kx }
9 kx this->last_remove_ = p;
9 kx return;
9 kx }
9 kx }
9 kx
9 kx // Did not find a node containing the given chunk. This could happen
9 kx // because a small chunk was already removed due to the fuzz.
9 kx gold_debug(DEBUG_INCREMENTAL,
9 kx "Free_list::remove(%d,%d) not found",
9 kx static_cast<int>(start), static_cast<int>(end));
9 kx }
9 kx
9 kx // Allocate a chunk of size LEN from the free list. Returns -1ULL
9 kx // if a sufficiently large chunk of free space is not found.
9 kx // We use a simple first-fit algorithm.
9 kx
9 kx off_t
9 kx Free_list::allocate(off_t len, uint64_t align, off_t minoff)
9 kx {
9 kx gold_debug(DEBUG_INCREMENTAL,
9 kx "Free_list::allocate(%08lx, %d, %08lx)",
9 kx static_cast<long>(len), static_cast<int>(align),
9 kx static_cast<long>(minoff));
9 kx if (len == 0)
9 kx return align_address(minoff, align);
9 kx
9 kx ++Free_list::num_allocates;
9 kx
9 kx // We usually want to drop free chunks smaller than 4 bytes.
9 kx // If we need to guarantee a minimum hole size, though, we need
9 kx // to keep track of all free chunks.
9 kx const int fuzz = this->min_hole_ > 0 ? 0 : 3;
9 kx
9 kx for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
9 kx {
9 kx ++Free_list::num_allocate_visits;
9 kx off_t start = p->start_ > minoff ? p->start_ : minoff;
9 kx start = align_address(start, align);
9 kx off_t end = start + len;
9 kx if (end > p->end_ && p->end_ == this->length_ && this->extend_)
9 kx {
9 kx this->length_ = end;
9 kx p->end_ = end;
9 kx }
9 kx if (end == p->end_ || (end <= p->end_ - this->min_hole_))
9 kx {
9 kx if (p->start_ + fuzz >= start && p->end_ <= end + fuzz)
9 kx this->list_.erase(p);
9 kx else if (p->start_ + fuzz >= start)
9 kx p->start_ = end;
9 kx else if (p->end_ <= end + fuzz)
9 kx p->end_ = start;
9 kx else
9 kx {
9 kx Free_list_node newnode(p->start_, start);
9 kx p->start_ = end;
9 kx this->list_.insert(p, newnode);
9 kx ++Free_list::num_nodes;
9 kx }
9 kx return start;
9 kx }
9 kx }
9 kx if (this->extend_)
9 kx {
9 kx off_t start = align_address(this->length_, align);
9 kx this->length_ = start + len;
9 kx return start;
9 kx }
9 kx return -1;
9 kx }
9 kx
9 kx // Dump the free list (for debugging).
9 kx void
9 kx Free_list::dump()
9 kx {
9 kx gold_info("Free list:\n start end length\n");
9 kx for (Iterator p = this->list_.begin(); p != this->list_.end(); ++p)
9 kx gold_info(" %08lx %08lx %08lx", static_cast<long>(p->start_),
9 kx static_cast<long>(p->end_),
9 kx static_cast<long>(p->end_ - p->start_));
9 kx }
9 kx
9 kx // Print the statistics for the free lists.
9 kx void
9 kx Free_list::print_stats()
9 kx {
9 kx fprintf(stderr, _("%s: total free lists: %u\n"),
9 kx program_name, Free_list::num_lists);
9 kx fprintf(stderr, _("%s: total free list nodes: %u\n"),
9 kx program_name, Free_list::num_nodes);
9 kx fprintf(stderr, _("%s: calls to Free_list::remove: %u\n"),
9 kx program_name, Free_list::num_removes);
9 kx fprintf(stderr, _("%s: nodes visited: %u\n"),
9 kx program_name, Free_list::num_remove_visits);
9 kx fprintf(stderr, _("%s: calls to Free_list::allocate: %u\n"),
9 kx program_name, Free_list::num_allocates);
9 kx fprintf(stderr, _("%s: nodes visited: %u\n"),
9 kx program_name, Free_list::num_allocate_visits);
9 kx }
9 kx
9 kx // A Hash_task computes the MD5 checksum of an array of char.
9 kx
9 kx class Hash_task : public Task
9 kx {
9 kx public:
9 kx Hash_task(Output_file* of,
9 kx size_t offset,
9 kx size_t size,
9 kx unsigned char* dst,
9 kx Task_token* final_blocker)
9 kx : of_(of), offset_(offset), size_(size), dst_(dst),
9 kx final_blocker_(final_blocker)
9 kx { }
9 kx
9 kx void
9 kx run(Workqueue*)
9 kx {
9 kx const unsigned char* iv =
9 kx this->of_->get_input_view(this->offset_, this->size_);
9 kx md5_buffer(reinterpret_cast<const char*>(iv), this->size_, this->dst_);
9 kx this->of_->free_input_view(this->offset_, this->size_, iv);
9 kx }
9 kx
9 kx Task_token*
9 kx is_runnable()
9 kx { return NULL; }
9 kx
9 kx // Unblock FINAL_BLOCKER_ when done.
9 kx void
9 kx locks(Task_locker* tl)
9 kx { tl->add(this, this->final_blocker_); }
9 kx
9 kx std::string
9 kx get_name() const
9 kx { return "Hash_task"; }
9 kx
9 kx private:
9 kx Output_file* of_;
9 kx const size_t offset_;
9 kx const size_t size_;
9 kx unsigned char* const dst_;
9 kx Task_token* const final_blocker_;
9 kx };
9 kx
9 kx // Layout::Relaxation_debug_check methods.
9 kx
9 kx // Check that sections and special data are in reset states.
9 kx // We do not save states for Output_sections and special Output_data.
9 kx // So we check that they have not assigned any addresses or offsets.
9 kx // clean_up_after_relaxation simply resets their addresses and offsets.
9 kx void
9 kx Layout::Relaxation_debug_check::check_output_data_for_reset_values(
9 kx const Layout::Section_list& sections,
9 kx const Layout::Data_list& special_outputs,
9 kx const Layout::Data_list& relax_outputs)
9 kx {
9 kx for(Layout::Section_list::const_iterator p = sections.begin();
9 kx p != sections.end();
9 kx ++p)
9 kx gold_assert((*p)->address_and_file_offset_have_reset_values());
9 kx
9 kx for(Layout::Data_list::const_iterator p = special_outputs.begin();
9 kx p != special_outputs.end();
9 kx ++p)
9 kx gold_assert((*p)->address_and_file_offset_have_reset_values());
9 kx
9 kx gold_assert(relax_outputs.empty());
9 kx }
9 kx
9 kx // Save information of SECTIONS for checking later.
9 kx
9 kx void
9 kx Layout::Relaxation_debug_check::read_sections(
9 kx const Layout::Section_list& sections)
9 kx {
9 kx for(Layout::Section_list::const_iterator p = sections.begin();
9 kx p != sections.end();
9 kx ++p)
9 kx {
9 kx Output_section* os = *p;
9 kx Section_info info;
9 kx info.output_section = os;
9 kx info.address = os->is_address_valid() ? os->address() : 0;
9 kx info.data_size = os->is_data_size_valid() ? os->data_size() : -1;
9 kx info.offset = os->is_offset_valid()? os->offset() : -1 ;
9 kx this->section_infos_.push_back(info);
9 kx }
9 kx }
9 kx
9 kx // Verify SECTIONS using previously recorded information.
9 kx
9 kx void
9 kx Layout::Relaxation_debug_check::verify_sections(
9 kx const Layout::Section_list& sections)
9 kx {
9 kx size_t i = 0;
9 kx for(Layout::Section_list::const_iterator p = sections.begin();
9 kx p != sections.end();
9 kx ++p, ++i)
9 kx {
9 kx Output_section* os = *p;
9 kx uint64_t address = os->is_address_valid() ? os->address() : 0;
9 kx off_t data_size = os->is_data_size_valid() ? os->data_size() : -1;
9 kx off_t offset = os->is_offset_valid()? os->offset() : -1 ;
9 kx
9 kx if (i >= this->section_infos_.size())
9 kx {
9 kx gold_fatal("Section_info of %s missing.\n", os->name());
9 kx }
9 kx const Section_info& info = this->section_infos_[i];
9 kx if (os != info.output_section)
9 kx gold_fatal("Section order changed. Expecting %s but see %s\n",
9 kx info.output_section->name(), os->name());
9 kx if (address != info.address
9 kx || data_size != info.data_size
9 kx || offset != info.offset)
9 kx gold_fatal("Section %s changed.\n", os->name());
9 kx }
9 kx }
9 kx
9 kx // Layout_task_runner methods.
9 kx
9 kx // Lay out the sections. This is called after all the input objects
9 kx // have been read.
9 kx
9 kx void
9 kx Layout_task_runner::run(Workqueue* workqueue, const Task* task)
9 kx {
9 kx // See if any of the input definitions violate the One Definition Rule.
9 kx // TODO: if this is too slow, do this as a task, rather than inline.
9 kx this->symtab_->detect_odr_violations(task, this->options_.output_file_name());
9 kx
9 kx Layout* layout = this->layout_;
9 kx off_t file_size = layout->finalize(this->input_objects_,
9 kx this->symtab_,
9 kx this->target_,
9 kx task);
9 kx
9 kx // Now we know the final size of the output file and we know where
9 kx // each piece of information goes.
9 kx
9 kx if (this->mapfile_ != NULL)
9 kx {
9 kx this->mapfile_->print_discarded_sections(this->input_objects_);
9 kx layout->print_to_mapfile(this->mapfile_);
9 kx }
9 kx
9 kx Output_file* of;
9 kx if (layout->incremental_base() == NULL)
9 kx {
9 kx of = new Output_file(parameters->options().output_file_name());
9 kx if (this->options_.oformat_enum() != General_options::OBJECT_FORMAT_ELF)
9 kx of->set_is_temporary();
9 kx of->open(file_size);
9 kx }
9 kx else
9 kx {
9 kx of = layout->incremental_base()->output_file();
9 kx
9 kx // Apply the incremental relocations for symbols whose values
9 kx // have changed. We do this before we resize the file and start
9 kx // writing anything else to it, so that we can read the old
9 kx // incremental information from the file before (possibly)
9 kx // overwriting it.
9 kx if (parameters->incremental_update())
9 kx layout->incremental_base()->apply_incremental_relocs(this->symtab_,
9 kx this->layout_,
9 kx of);
9 kx
9 kx of->resize(file_size);
9 kx }
9 kx
9 kx // Queue up the final set of tasks.
9 kx gold::queue_final_tasks(this->options_, this->input_objects_,
9 kx this->symtab_, layout, workqueue, of);
9 kx }
9 kx
9 kx // Layout methods.
9 kx
9 kx Layout::Layout(int number_of_input_files, Script_options* script_options)
9 kx : number_of_input_files_(number_of_input_files),
9 kx script_options_(script_options),
9 kx namepool_(),
9 kx sympool_(),
9 kx dynpool_(),
9 kx signatures_(),
9 kx section_name_map_(),
9 kx segment_list_(),
9 kx section_list_(),
9 kx unattached_section_list_(),
9 kx special_output_list_(),
9 kx relax_output_list_(),
9 kx section_headers_(NULL),
9 kx tls_segment_(NULL),
9 kx relro_segment_(NULL),
9 kx interp_segment_(NULL),
9 kx increase_relro_(0),
9 kx symtab_section_(NULL),
9 kx symtab_xindex_(NULL),
9 kx dynsym_section_(NULL),
9 kx dynsym_xindex_(NULL),
9 kx dynamic_section_(NULL),
9 kx dynamic_symbol_(NULL),
9 kx dynamic_data_(NULL),
9 kx eh_frame_section_(NULL),
9 kx eh_frame_data_(NULL),
9 kx added_eh_frame_data_(false),
9 kx eh_frame_hdr_section_(NULL),
9 kx gdb_index_data_(NULL),
9 kx build_id_note_(NULL),
9 kx debug_abbrev_(NULL),
9 kx debug_info_(NULL),
9 kx group_signatures_(),
9 kx output_file_size_(-1),
9 kx have_added_input_section_(false),
9 kx sections_are_attached_(false),
9 kx input_requires_executable_stack_(false),
9 kx input_with_gnu_stack_note_(false),
9 kx input_without_gnu_stack_note_(false),
9 kx has_static_tls_(false),
9 kx any_postprocessing_sections_(false),
9 kx resized_signatures_(false),
9 kx have_stabstr_section_(false),
9 kx section_ordering_specified_(false),
9 kx unique_segment_for_sections_specified_(false),
9 kx incremental_inputs_(NULL),
9 kx record_output_section_data_from_script_(false),
9 kx lto_slim_object_(false),
9 kx script_output_section_data_list_(),
9 kx segment_states_(NULL),
9 kx relaxation_debug_check_(NULL),
9 kx section_order_map_(),
9 kx section_segment_map_(),
9 kx input_section_position_(),
9 kx input_section_glob_(),
9 kx incremental_base_(NULL),
9 kx free_list_(),
9 kx gnu_properties_()
9 kx {
9 kx // Make space for more than enough segments for a typical file.
9 kx // This is just for efficiency--it's OK if we wind up needing more.
9 kx this->segment_list_.reserve(12);
9 kx
9 kx // We expect two unattached Output_data objects: the file header and
9 kx // the segment headers.
9 kx this->special_output_list_.reserve(2);
9 kx
9 kx // Initialize structure needed for an incremental build.
9 kx if (parameters->incremental())
9 kx this->incremental_inputs_ = new Incremental_inputs;
9 kx
9 kx // The section name pool is worth optimizing in all cases, because
9 kx // it is small, but there are often overlaps due to .rel sections.
9 kx this->namepool_.set_optimize();
9 kx }
9 kx
9 kx // For incremental links, record the base file to be modified.
9 kx
9 kx void
9 kx Layout::set_incremental_base(Incremental_binary* base)
9 kx {
9 kx this->incremental_base_ = base;
9 kx this->free_list_.init(base->output_file()->filesize(), true);
9 kx }
9 kx
9 kx // Hash a key we use to look up an output section mapping.
9 kx
9 kx size_t
9 kx Layout::Hash_key::operator()(const Layout::Key& k) const
9 kx {
9 kx return k.first + k.second.first + k.second.second;
9 kx }
9 kx
9 kx // These are the debug sections that are actually used by gdb.
9 kx // Currently, we've checked versions of gdb up to and including 7.4.
9 kx // We only check the part of the name that follows ".debug_" or
9 kx // ".zdebug_".
9 kx
9 kx static const char* gdb_sections[] =
9 kx {
9 kx "abbrev",
9 kx "addr", // Fission extension
9 kx // "aranges", // not used by gdb as of 7.4
9 kx "frame",
9 kx "gdb_scripts",
9 kx "info",
9 kx "types",
9 kx "line",
9 kx "loc",
9 kx "macinfo",
9 kx "macro",
9 kx // "pubnames", // not used by gdb as of 7.4
9 kx // "pubtypes", // not used by gdb as of 7.4
9 kx // "gnu_pubnames", // Fission extension
9 kx // "gnu_pubtypes", // Fission extension
9 kx "ranges",
9 kx "str",
9 kx "str_offsets",
9 kx };
9 kx
9 kx // This is the minimum set of sections needed for line numbers.
9 kx
9 kx static const char* lines_only_debug_sections[] =
9 kx {
9 kx "abbrev",
9 kx // "addr", // Fission extension
9 kx // "aranges", // not used by gdb as of 7.4
9 kx // "frame",
9 kx // "gdb_scripts",
9 kx "info",
9 kx // "types",
9 kx "line",
9 kx // "loc",
9 kx // "macinfo",
9 kx // "macro",
9 kx // "pubnames", // not used by gdb as of 7.4
9 kx // "pubtypes", // not used by gdb as of 7.4
9 kx // "gnu_pubnames", // Fission extension
9 kx // "gnu_pubtypes", // Fission extension
9 kx // "ranges",
9 kx "str",
9 kx "str_offsets", // Fission extension
9 kx };
9 kx
9 kx // These sections are the DWARF fast-lookup tables, and are not needed
9 kx // when building a .gdb_index section.
9 kx
9 kx static const char* gdb_fast_lookup_sections[] =
9 kx {
9 kx "aranges",
9 kx "pubnames",
9 kx "gnu_pubnames",
9 kx "pubtypes",
9 kx "gnu_pubtypes",
9 kx };
9 kx
9 kx // Returns whether the given debug section is in the list of
9 kx // debug-sections-used-by-some-version-of-gdb. SUFFIX is the
9 kx // portion of the name following ".debug_" or ".zdebug_".
9 kx
9 kx static inline bool
9 kx is_gdb_debug_section(const char* suffix)
9 kx {
9 kx // We can do this faster: binary search or a hashtable. But why bother?
9 kx for (size_t i = 0; i < sizeof(gdb_sections)/sizeof(*gdb_sections); ++i)
9 kx if (strcmp(suffix, gdb_sections[i]) == 0)
9 kx return true;
9 kx return false;
9 kx }
9 kx
9 kx // Returns whether the given section is needed for lines-only debugging.
9 kx
9 kx static inline bool
9 kx is_lines_only_debug_section(const char* suffix)
9 kx {
9 kx // We can do this faster: binary search or a hashtable. But why bother?
9 kx for (size_t i = 0;
9 kx i < sizeof(lines_only_debug_sections)/sizeof(*lines_only_debug_sections);
9 kx ++i)
9 kx if (strcmp(suffix, lines_only_debug_sections[i]) == 0)
9 kx return true;
9 kx return false;
9 kx }
9 kx
9 kx // Returns whether the given section is a fast-lookup section that
9 kx // will not be needed when building a .gdb_index section.
9 kx
9 kx static inline bool
9 kx is_gdb_fast_lookup_section(const char* suffix)
9 kx {
9 kx // We can do this faster: binary search or a hashtable. But why bother?
9 kx for (size_t i = 0;
9 kx i < sizeof(gdb_fast_lookup_sections)/sizeof(*gdb_fast_lookup_sections);
9 kx ++i)
9 kx if (strcmp(suffix, gdb_fast_lookup_sections[i]) == 0)
9 kx return true;
9 kx return false;
9 kx }
9 kx
9 kx // Sometimes we compress sections. This is typically done for
9 kx // sections that are not part of normal program execution (such as
9 kx // .debug_* sections), and where the readers of these sections know
9 kx // how to deal with compressed sections. This routine doesn't say for
9 kx // certain whether we'll compress -- it depends on commandline options
9 kx // as well -- just whether this section is a candidate for compression.
9 kx // (The Output_compressed_section class decides whether to compress
9 kx // a given section, and picks the name of the compressed section.)
9 kx
9 kx static bool
9 kx is_compressible_debug_section(const char* secname)
9 kx {
9 kx return (is_prefix_of(".debug", secname));
9 kx }
9 kx
9 kx // We may see compressed debug sections in input files. Return TRUE
9 kx // if this is the name of a compressed debug section.
9 kx
9 kx bool
9 kx is_compressed_debug_section(const char* secname)
9 kx {
9 kx return (is_prefix_of(".zdebug", secname));
9 kx }
9 kx
9 kx std::string
9 kx corresponding_uncompressed_section_name(std::string secname)
9 kx {
9 kx gold_assert(secname[0] == '.' && secname[1] == 'z');
9 kx std::string ret(".");
9 kx ret.append(secname, 2, std::string::npos);
9 kx return ret;
9 kx }
9 kx
9 kx // Whether to include this section in the link.
9 kx
9 kx template<int size, bool big_endian>
9 kx bool
9 kx Layout::include_section(Sized_relobj_file<size, big_endian>*, const char* name,
9 kx const elfcpp::Shdr<size, big_endian>& shdr)
9 kx {
9 kx if (!parameters->options().relocatable()
9 kx && (shdr.get_sh_flags() & elfcpp::SHF_EXCLUDE))
9 kx return false;
9 kx
9 kx elfcpp::Elf_Word sh_type = shdr.get_sh_type();
9 kx
9 kx if ((sh_type >= elfcpp::SHT_LOOS && sh_type <= elfcpp::SHT_HIOS)
9 kx || (sh_type >= elfcpp::SHT_LOPROC && sh_type <= elfcpp::SHT_HIPROC))
9 kx return parameters->target().should_include_section(sh_type);
9 kx
9 kx switch (sh_type)
9 kx {
9 kx case elfcpp::SHT_NULL:
9 kx case elfcpp::SHT_SYMTAB:
9 kx case elfcpp::SHT_DYNSYM:
9 kx case elfcpp::SHT_HASH:
9 kx case elfcpp::SHT_DYNAMIC:
9 kx case elfcpp::SHT_SYMTAB_SHNDX:
9 kx return false;
9 kx
9 kx case elfcpp::SHT_STRTAB:
9 kx // Discard the sections which have special meanings in the ELF
9 kx // ABI. Keep others (e.g., .stabstr). We could also do this by
9 kx // checking the sh_link fields of the appropriate sections.
9 kx return (strcmp(name, ".dynstr") != 0
9 kx && strcmp(name, ".strtab") != 0
9 kx && strcmp(name, ".shstrtab") != 0);
9 kx
9 kx case elfcpp::SHT_RELA:
9 kx case elfcpp::SHT_REL:
9 kx case elfcpp::SHT_GROUP:
9 kx // If we are emitting relocations these should be handled
9 kx // elsewhere.
9 kx gold_assert(!parameters->options().relocatable());
9 kx return false;
9 kx
9 kx case elfcpp::SHT_PROGBITS:
9 kx if (parameters->options().strip_debug()
9 kx && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
9 kx {
9 kx if (is_debug_info_section(name))
9 kx return false;
9 kx }
9 kx if (parameters->options().strip_debug_non_line()
9 kx && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
9 kx {
9 kx // Debugging sections can only be recognized by name.
9 kx if (is_prefix_of(".debug_", name)
9 kx && !is_lines_only_debug_section(name + 7))
9 kx return false;
9 kx if (is_prefix_of(".zdebug_", name)
9 kx && !is_lines_only_debug_section(name + 8))
9 kx return false;
9 kx }
9 kx if (parameters->options().strip_debug_gdb()
9 kx && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
9 kx {
9 kx // Debugging sections can only be recognized by name.
9 kx if (is_prefix_of(".debug_", name)
9 kx && !is_gdb_debug_section(name + 7))
9 kx return false;
9 kx if (is_prefix_of(".zdebug_", name)
9 kx && !is_gdb_debug_section(name + 8))
9 kx return false;
9 kx }
9 kx if (parameters->options().gdb_index()
9 kx && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
9 kx {
9 kx // When building .gdb_index, we can strip .debug_pubnames,
9 kx // .debug_pubtypes, and .debug_aranges sections.
9 kx if (is_prefix_of(".debug_", name)
9 kx && is_gdb_fast_lookup_section(name + 7))
9 kx return false;
9 kx if (is_prefix_of(".zdebug_", name)
9 kx && is_gdb_fast_lookup_section(name + 8))
9 kx return false;
9 kx }
9 kx if (parameters->options().strip_lto_sections()
9 kx && !parameters->options().relocatable()
9 kx && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0)
9 kx {
9 kx // Ignore LTO sections containing intermediate code.
9 kx if (is_prefix_of(".gnu.lto_", name))
9 kx return false;
9 kx }
9 kx // The GNU linker strips .gnu_debuglink sections, so we do too.
9 kx // This is a feature used to keep debugging information in
9 kx // separate files.
9 kx if (strcmp(name, ".gnu_debuglink") == 0)
9 kx return false;
9 kx return true;
9 kx
9 kx default:
9 kx return true;
9 kx }
9 kx }
9 kx
9 kx // Return an output section named NAME, or NULL if there is none.
9 kx
9 kx Output_section*
9 kx Layout::find_output_section(const char* name) const
9 kx {
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx if (strcmp((*p)->name(), name) == 0)
9 kx return *p;
9 kx return NULL;
9 kx }
9 kx
9 kx // Return an output segment of type TYPE, with segment flags SET set
9 kx // and segment flags CLEAR clear. Return NULL if there is none.
9 kx
9 kx Output_segment*
9 kx Layout::find_output_segment(elfcpp::PT type, elfcpp::Elf_Word set,
9 kx elfcpp::Elf_Word clear) const
9 kx {
9 kx for (Segment_list::const_iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx if (static_cast<elfcpp::PT>((*p)->type()) == type
9 kx && ((*p)->flags() & set) == set
9 kx && ((*p)->flags() & clear) == 0)
9 kx return *p;
9 kx return NULL;
9 kx }
9 kx
9 kx // When we put a .ctors or .dtors section with more than one word into
9 kx // a .init_array or .fini_array section, we need to reverse the words
9 kx // in the .ctors/.dtors section. This is because .init_array executes
9 kx // constructors front to back, where .ctors executes them back to
9 kx // front, and vice-versa for .fini_array/.dtors. Although we do want
9 kx // to remap .ctors/.dtors into .init_array/.fini_array because it can
9 kx // be more efficient, we don't want to change the order in which
9 kx // constructors/destructors are run. This set just keeps track of
9 kx // these sections which need to be reversed. It is only changed by
9 kx // Layout::layout. It should be a private member of Layout, but that
9 kx // would require layout.h to #include object.h to get the definition
9 kx // of Section_id.
9 kx static Unordered_set<Section_id, Section_id_hash> ctors_sections_in_init_array;
9 kx
9 kx // Return whether OBJECT/SHNDX is a .ctors/.dtors section mapped to a
9 kx // .init_array/.fini_array section.
9 kx
9 kx bool
9 kx Layout::is_ctors_in_init_array(Relobj* relobj, unsigned int shndx) const
9 kx {
9 kx return (ctors_sections_in_init_array.find(Section_id(relobj, shndx))
9 kx != ctors_sections_in_init_array.end());
9 kx }
9 kx
9 kx // Return the output section to use for section NAME with type TYPE
9 kx // and section flags FLAGS. NAME must be canonicalized in the string
9 kx // pool, and NAME_KEY is the key. ORDER is where this should appear
9 kx // in the output sections. IS_RELRO is true for a relro section.
9 kx
9 kx Output_section*
9 kx Layout::get_output_section(const char* name, Stringpool::Key name_key,
9 kx elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
9 kx Output_section_order order, bool is_relro)
9 kx {
9 kx elfcpp::Elf_Word lookup_type = type;
9 kx
9 kx // For lookup purposes, treat INIT_ARRAY, FINI_ARRAY, and
9 kx // PREINIT_ARRAY like PROGBITS. This ensures that we combine
9 kx // .init_array, .fini_array, and .preinit_array sections by name
9 kx // whatever their type in the input file. We do this because the
9 kx // types are not always right in the input files.
9 kx if (lookup_type == elfcpp::SHT_INIT_ARRAY
9 kx || lookup_type == elfcpp::SHT_FINI_ARRAY
9 kx || lookup_type == elfcpp::SHT_PREINIT_ARRAY)
9 kx lookup_type = elfcpp::SHT_PROGBITS;
9 kx
9 kx elfcpp::Elf_Xword lookup_flags = flags;
9 kx
9 kx // Ignoring SHF_WRITE and SHF_EXECINSTR here means that we combine
9 kx // read-write with read-only sections. Some other ELF linkers do
9 kx // not do this. FIXME: Perhaps there should be an option
9 kx // controlling this.
9 kx lookup_flags &= ~(elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
9 kx
9 kx const Key key(name_key, std::make_pair(lookup_type, lookup_flags));
9 kx const std::pair<Key, Output_section*> v(key, NULL);
9 kx std::pair<Section_name_map::iterator, bool> ins(
9 kx this->section_name_map_.insert(v));
9 kx
9 kx if (!ins.second)
9 kx return ins.first->second;
9 kx else
9 kx {
9 kx // This is the first time we've seen this name/type/flags
9 kx // combination. For compatibility with the GNU linker, we
9 kx // combine sections with contents and zero flags with sections
9 kx // with non-zero flags. This is a workaround for cases where
9 kx // assembler code forgets to set section flags. FIXME: Perhaps
9 kx // there should be an option to control this.
9 kx Output_section* os = NULL;
9 kx
9 kx if (lookup_type == elfcpp::SHT_PROGBITS)
9 kx {
9 kx if (flags == 0)
9 kx {
9 kx Output_section* same_name = this->find_output_section(name);
9 kx if (same_name != NULL
9 kx && (same_name->type() == elfcpp::SHT_PROGBITS
9 kx || same_name->type() == elfcpp::SHT_INIT_ARRAY
9 kx || same_name->type() == elfcpp::SHT_FINI_ARRAY
9 kx || same_name->type() == elfcpp::SHT_PREINIT_ARRAY)
9 kx && (same_name->flags() & elfcpp::SHF_TLS) == 0)
9 kx os = same_name;
9 kx }
9 kx #if 0 /* BZ 1722715, PR 17556. */
9 kx else if ((flags & elfcpp::SHF_TLS) == 0)
9 kx {
9 kx elfcpp::Elf_Xword zero_flags = 0;
9 kx const Key zero_key(name_key, std::make_pair(lookup_type,
9 kx zero_flags));
9 kx Section_name_map::iterator p =
9 kx this->section_name_map_.find(zero_key);
9 kx if (p != this->section_name_map_.end())
9 kx os = p->second;
9 kx }
9 kx #endif
9 kx }
9 kx
9 kx if (os == NULL)
9 kx os = this->make_output_section(name, type, flags, order, is_relro);
9 kx
9 kx ins.first->second = os;
9 kx return os;
9 kx }
9 kx }
9 kx
9 kx // Returns TRUE iff NAME (an input section from RELOBJ) will
9 kx // be mapped to an output section that should be KEPT.
9 kx
9 kx bool
9 kx Layout::keep_input_section(const Relobj* relobj, const char* name)
9 kx {
9 kx if (! this->script_options_->saw_sections_clause())
9 kx return false;
9 kx
9 kx Script_sections* ss = this->script_options_->script_sections();
9 kx const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
9 kx Output_section** output_section_slot;
9 kx Script_sections::Section_type script_section_type;
9 kx bool keep;
9 kx
9 kx name = ss->output_section_name(file_name, name, &output_section_slot,
9 kx &script_section_type, &keep, true);
9 kx return name != NULL && keep;
9 kx }
9 kx
9 kx // Clear the input section flags that should not be copied to the
9 kx // output section.
9 kx
9 kx elfcpp::Elf_Xword
9 kx Layout::get_output_section_flags(elfcpp::Elf_Xword input_section_flags)
9 kx {
9 kx // Some flags in the input section should not be automatically
9 kx // copied to the output section.
9 kx input_section_flags &= ~ (elfcpp::SHF_INFO_LINK
9 kx | elfcpp::SHF_GROUP
9 kx | elfcpp::SHF_COMPRESSED
9 kx | elfcpp::SHF_MERGE
9 kx | elfcpp::SHF_STRINGS);
9 kx
9 kx // We only clear the SHF_LINK_ORDER flag in for
9 kx // a non-relocatable link.
9 kx if (!parameters->options().relocatable())
9 kx input_section_flags &= ~elfcpp::SHF_LINK_ORDER;
9 kx
9 kx return input_section_flags;
9 kx }
9 kx
9 kx // Pick the output section to use for section NAME, in input file
9 kx // RELOBJ, with type TYPE and flags FLAGS. RELOBJ may be NULL for a
9 kx // linker created section. IS_INPUT_SECTION is true if we are
9 kx // choosing an output section for an input section found in a input
9 kx // file. ORDER is where this section should appear in the output
9 kx // sections. IS_RELRO is true for a relro section. This will return
9 kx // NULL if the input section should be discarded. MATCH_INPUT_SPEC
9 kx // is true if the section name should be matched against input specs
9 kx // in a linker script.
9 kx
9 kx Output_section*
9 kx Layout::choose_output_section(const Relobj* relobj, const char* name,
9 kx elfcpp::Elf_Word type, elfcpp::Elf_Xword flags,
9 kx bool is_input_section, Output_section_order order,
9 kx bool is_relro, bool is_reloc,
9 kx bool match_input_spec)
9 kx {
9 kx // We should not see any input sections after we have attached
9 kx // sections to segments.
9 kx gold_assert(!is_input_section || !this->sections_are_attached_);
9 kx
9 kx flags = this->get_output_section_flags(flags);
9 kx
9 kx if (this->script_options_->saw_sections_clause() && !is_reloc)
9 kx {
9 kx // We are using a SECTIONS clause, so the output section is
9 kx // chosen based only on the name.
9 kx
9 kx Script_sections* ss = this->script_options_->script_sections();
9 kx const char* file_name = relobj == NULL ? NULL : relobj->name().c_str();
9 kx Output_section** output_section_slot;
9 kx Script_sections::Section_type script_section_type;
9 kx const char* orig_name = name;
9 kx bool keep;
9 kx name = ss->output_section_name(file_name, name, &output_section_slot,
9 kx &script_section_type, &keep,
9 kx match_input_spec);
9 kx
9 kx if (name == NULL)
9 kx {
9 kx gold_debug(DEBUG_SCRIPT, _("Unable to create output section '%s' "
9 kx "because it is not allowed by the "
9 kx "SECTIONS clause of the linker script"),
9 kx orig_name);
9 kx // The SECTIONS clause says to discard this input section.
9 kx return NULL;
9 kx }
9 kx
9 kx // We can only handle script section types ST_NONE and ST_NOLOAD.
9 kx switch (script_section_type)
9 kx {
9 kx case Script_sections::ST_NONE:
9 kx break;
9 kx case Script_sections::ST_NOLOAD:
9 kx flags &= elfcpp::SHF_ALLOC;
9 kx break;
9 kx default:
9 kx gold_unreachable();
9 kx }
9 kx
9 kx // If this is an orphan section--one not mentioned in the linker
9 kx // script--then OUTPUT_SECTION_SLOT will be NULL, and we do the
9 kx // default processing below.
9 kx
9 kx if (output_section_slot != NULL)
9 kx {
9 kx if (*output_section_slot != NULL)
9 kx {
9 kx (*output_section_slot)->update_flags_for_input_section(flags);
9 kx return *output_section_slot;
9 kx }
9 kx
9 kx // We don't put sections found in the linker script into
9 kx // SECTION_NAME_MAP_. That keeps us from getting confused
9 kx // if an orphan section is mapped to a section with the same
9 kx // name as one in the linker script.
9 kx
9 kx name = this->namepool_.add(name, false, NULL);
9 kx
9 kx Output_section* os = this->make_output_section(name, type, flags,
9 kx order, is_relro);
9 kx
9 kx os->set_found_in_sections_clause();
9 kx
9 kx // Special handling for NOLOAD sections.
9 kx if (script_section_type == Script_sections::ST_NOLOAD)
9 kx {
9 kx os->set_is_noload();
9 kx
9 kx // The constructor of Output_section sets addresses of non-ALLOC
9 kx // sections to 0 by default. We don't want that for NOLOAD
9 kx // sections even if they have no SHF_ALLOC flag.
9 kx if ((os->flags() & elfcpp::SHF_ALLOC) == 0
9 kx && os->is_address_valid())
9 kx {
9 kx gold_assert(os->address() == 0
9 kx && !os->is_offset_valid()
9 kx && !os->is_data_size_valid());
9 kx os->reset_address_and_file_offset();
9 kx }
9 kx }
9 kx
9 kx *output_section_slot = os;
9 kx return os;
9 kx }
9 kx }
9 kx
9 kx // FIXME: Handle SHF_OS_NONCONFORMING somewhere.
9 kx
9 kx size_t len = strlen(name);
9 kx std::string uncompressed_name;
9 kx
9 kx // Compressed debug sections should be mapped to the corresponding
9 kx // uncompressed section.
9 kx if (is_compressed_debug_section(name))
9 kx {
9 kx uncompressed_name =
9 kx corresponding_uncompressed_section_name(std::string(name, len));
9 kx name = uncompressed_name.c_str();
9 kx len = uncompressed_name.length();
9 kx }
9 kx
9 kx // Turn NAME from the name of the input section into the name of the
9 kx // output section.
9 kx if (is_input_section
9 kx && !this->script_options_->saw_sections_clause()
9 kx && !parameters->options().relocatable())
9 kx {
9 kx const char *orig_name = name;
9 kx name = parameters->target().output_section_name(relobj, name, &len);
9 kx if (name == NULL)
9 kx name = Layout::output_section_name(relobj, orig_name, &len);
9 kx }
9 kx
9 kx Stringpool::Key name_key;
9 kx name = this->namepool_.add_with_length(name, len, true, &name_key);
9 kx
9 kx // Find or make the output section. The output section is selected
9 kx // based on the section name, type, and flags.
9 kx return this->get_output_section(name, name_key, type, flags, order, is_relro);
9 kx }
9 kx
9 kx // For incremental links, record the initial fixed layout of a section
9 kx // from the base file, and return a pointer to the Output_section.
9 kx
9 kx template<int size, bool big_endian>
9 kx Output_section*
9 kx Layout::init_fixed_output_section(const char* name,
9 kx elfcpp::Shdr<size, big_endian>& shdr)
9 kx {
9 kx unsigned int sh_type = shdr.get_sh_type();
9 kx
9 kx // We preserve the layout of PROGBITS, NOBITS, INIT_ARRAY, FINI_ARRAY,
9 kx // PRE_INIT_ARRAY, and NOTE sections.
9 kx // All others will be created from scratch and reallocated.
9 kx if (!can_incremental_update(sh_type))
9 kx return NULL;
9 kx
9 kx // If we're generating a .gdb_index section, we need to regenerate
9 kx // it from scratch.
9 kx if (parameters->options().gdb_index()
9 kx && sh_type == elfcpp::SHT_PROGBITS
9 kx && strcmp(name, ".gdb_index") == 0)
9 kx return NULL;
9 kx
9 kx typename elfcpp::Elf_types<size>::Elf_Addr sh_addr = shdr.get_sh_addr();
9 kx typename elfcpp::Elf_types<size>::Elf_Off sh_offset = shdr.get_sh_offset();
9 kx typename elfcpp::Elf_types<size>::Elf_WXword sh_size = shdr.get_sh_size();
9 kx typename elfcpp::Elf_types<size>::Elf_WXword sh_flags =
9 kx this->get_output_section_flags(shdr.get_sh_flags());
9 kx typename elfcpp::Elf_types<size>::Elf_WXword sh_addralign =
9 kx shdr.get_sh_addralign();
9 kx
9 kx // Make the output section.
9 kx Stringpool::Key name_key;
9 kx name = this->namepool_.add(name, true, &name_key);
9 kx Output_section* os = this->get_output_section(name, name_key, sh_type,
9 kx sh_flags, ORDER_INVALID, false);
9 kx os->set_fixed_layout(sh_addr, sh_offset, sh_size, sh_addralign);
9 kx if (sh_type != elfcpp::SHT_NOBITS)
9 kx this->free_list_.remove(sh_offset, sh_offset + sh_size);
9 kx return os;
9 kx }
9 kx
9 kx // Return the index by which an input section should be ordered. This
9 kx // is used to sort some .text sections, for compatibility with GNU ld.
9 kx
9 kx int
9 kx Layout::special_ordering_of_input_section(const char* name)
9 kx {
9 kx // The GNU linker has some special handling for some sections that
9 kx // wind up in the .text section. Sections that start with these
9 kx // prefixes must appear first, and must appear in the order listed
9 kx // here.
9 kx static const char* const text_section_sort[] =
9 kx {
9 kx ".text.unlikely",
9 kx ".text.exit",
9 kx ".text.startup",
9 kx ".text.hot",
9 kx ".text.sorted"
9 kx };
9 kx
9 kx for (size_t i = 0;
9 kx i < sizeof(text_section_sort) / sizeof(text_section_sort[0]);
9 kx i++)
9 kx if (is_prefix_of(text_section_sort[i], name))
9 kx return i;
9 kx
9 kx return -1;
9 kx }
9 kx
9 kx // Return the output section to use for input section SHNDX, with name
9 kx // NAME, with header HEADER, from object OBJECT. RELOC_SHNDX is the
9 kx // index of a relocation section which applies to this section, or 0
9 kx // if none, or -1U if more than one. RELOC_TYPE is the type of the
9 kx // relocation section if there is one. Set *OFF to the offset of this
9 kx // input section without the output section. Return NULL if the
9 kx // section should be discarded. Set *OFF to -1 if the section
9 kx // contents should not be written directly to the output file, but
9 kx // will instead receive special handling.
9 kx
9 kx template<int size, bool big_endian>
9 kx Output_section*
9 kx Layout::layout(Sized_relobj_file<size, big_endian>* object, unsigned int shndx,
9 kx const char* name, const elfcpp::Shdr<size, big_endian>& shdr,
9 kx unsigned int sh_type, unsigned int reloc_shndx,
9 kx unsigned int, off_t* off)
9 kx {
9 kx *off = 0;
9 kx
9 kx if (!this->include_section(object, name, shdr))
9 kx return NULL;
9 kx
9 kx // In a relocatable link a grouped section must not be combined with
9 kx // any other sections.
9 kx Output_section* os;
9 kx if (parameters->options().relocatable()
9 kx && (shdr.get_sh_flags() & elfcpp::SHF_GROUP) != 0)
9 kx {
9 kx // Some flags in the input section should not be automatically
9 kx // copied to the output section.
9 kx elfcpp::Elf_Xword sh_flags = (shdr.get_sh_flags()
9 kx & ~ elfcpp::SHF_COMPRESSED);
9 kx name = this->namepool_.add(name, true, NULL);
9 kx os = this->make_output_section(name, sh_type, sh_flags, ORDER_INVALID,
9 kx false);
9 kx }
9 kx else
9 kx {
9 kx // Get the section flags and mask out any flags that do not
9 kx // take part in section matching.
9 kx elfcpp::Elf_Xword sh_flags
9 kx = (this->get_output_section_flags(shdr.get_sh_flags())
9 kx & ~object->osabi().ignored_sh_flags());
9 kx
9 kx // All ".text.unlikely.*" sections can be moved to a unique
9 kx // segment with --text-unlikely-segment option.
9 kx bool text_unlikely_segment
9 kx = (parameters->options().text_unlikely_segment()
9 kx && is_prefix_of(".text.unlikely",
9 kx object->section_name(shndx).c_str()));
9 kx if (text_unlikely_segment)
9 kx {
9 kx Stringpool::Key name_key;
9 kx const char* os_name = this->namepool_.add(".text.unlikely", true,
9 kx &name_key);
9 kx os = this->get_output_section(os_name, name_key, sh_type, sh_flags,
9 kx ORDER_INVALID, false);
9 kx // Map this output section to a unique segment. This is done to
9 kx // separate "text" that is not likely to be executed from "text"
9 kx // that is likely executed.
9 kx os->set_is_unique_segment();
9 kx }
9 kx else
9 kx {
9 kx // Plugins can choose to place one or more subsets of sections in
9 kx // unique segments and this is done by mapping these section subsets
9 kx // to unique output sections. Check if this section needs to be
9 kx // remapped to a unique output section.
9 kx Section_segment_map::iterator it
9 kx = this->section_segment_map_.find(Const_section_id(object, shndx));
9 kx if (it == this->section_segment_map_.end())
9 kx {
9 kx os = this->choose_output_section(object, name, sh_type,
9 kx sh_flags, true, ORDER_INVALID,
9 kx false, false, true);
9 kx }
9 kx else
9 kx {
9 kx // We know the name of the output section, directly call
9 kx // get_output_section here by-passing choose_output_section.
9 kx const char* os_name = it->second->name;
9 kx Stringpool::Key name_key;
9 kx os_name = this->namepool_.add(os_name, true, &name_key);
9 kx os = this->get_output_section(os_name, name_key, sh_type,
9 kx sh_flags, ORDER_INVALID, false);
9 kx if (!os->is_unique_segment())
9 kx {
9 kx os->set_is_unique_segment();
9 kx os->set_extra_segment_flags(it->second->flags);
9 kx os->set_segment_alignment(it->second->align);
9 kx }
9 kx }
9 kx }
9 kx if (os == NULL)
9 kx return NULL;
9 kx }
9 kx
9 kx // By default the GNU linker sorts input sections whose names match
9 kx // .ctors.*, .dtors.*, .init_array.*, or .fini_array.*. The
9 kx // sections are sorted by name. This is used to implement
9 kx // constructor priority ordering. We are compatible. When we put
9 kx // .ctor sections in .init_array and .dtor sections in .fini_array,
9 kx // we must also sort plain .ctor and .dtor sections.
9 kx if (!this->script_options_->saw_sections_clause()
9 kx && !parameters->options().relocatable()
9 kx && (is_prefix_of(".ctors.", name)
9 kx || is_prefix_of(".dtors.", name)
9 kx || is_prefix_of(".init_array.", name)
9 kx || is_prefix_of(".fini_array.", name)
9 kx || (parameters->options().ctors_in_init_array()
9 kx && (strcmp(name, ".ctors") == 0
9 kx || strcmp(name, ".dtors") == 0))))
9 kx os->set_must_sort_attached_input_sections();
9 kx
9 kx // By default the GNU linker sorts some special text sections ahead
9 kx // of others. We are compatible.
9 kx if (parameters->options().text_reorder()
9 kx && !this->script_options_->saw_sections_clause()
9 kx && !this->is_section_ordering_specified()
9 kx && !parameters->options().relocatable()
9 kx && Layout::special_ordering_of_input_section(name) >= 0)
9 kx os->set_must_sort_attached_input_sections();
9 kx
9 kx // If this is a .ctors or .ctors.* section being mapped to a
9 kx // .init_array section, or a .dtors or .dtors.* section being mapped
9 kx // to a .fini_array section, we will need to reverse the words if
9 kx // there is more than one. Record this section for later. See
9 kx // ctors_sections_in_init_array above.
9 kx if (!this->script_options_->saw_sections_clause()
9 kx && !parameters->options().relocatable()
9 kx && shdr.get_sh_size() > size / 8
9 kx && (((strcmp(name, ".ctors") == 0
9 kx || is_prefix_of(".ctors.", name))
9 kx && strcmp(os->name(), ".init_array") == 0)
9 kx || ((strcmp(name, ".dtors") == 0
9 kx || is_prefix_of(".dtors.", name))
9 kx && strcmp(os->name(), ".fini_array") == 0)))
9 kx ctors_sections_in_init_array.insert(Section_id(object, shndx));
9 kx
9 kx // FIXME: Handle SHF_LINK_ORDER somewhere.
9 kx
9 kx elfcpp::Elf_Xword orig_flags = os->flags();
9 kx
9 kx *off = os->add_input_section(this, object, shndx, name, shdr, reloc_shndx,
9 kx this->script_options_->saw_sections_clause());
9 kx
9 kx // If the flags changed, we may have to change the order.
9 kx if ((orig_flags & elfcpp::SHF_ALLOC) != 0)
9 kx {
9 kx orig_flags &= (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
9 kx elfcpp::Elf_Xword new_flags =
9 kx os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR);
9 kx if (orig_flags != new_flags)
9 kx os->set_order(this->default_section_order(os, false));
9 kx }
9 kx
9 kx this->have_added_input_section_ = true;
9 kx
9 kx return os;
9 kx }
9 kx
9 kx // Maps section SECN to SEGMENT s.
9 kx void
9 kx Layout::insert_section_segment_map(Const_section_id secn,
9 kx Unique_segment_info *s)
9 kx {
9 kx gold_assert(this->unique_segment_for_sections_specified_);
9 kx this->section_segment_map_[secn] = s;
9 kx }
9 kx
9 kx // Handle a relocation section when doing a relocatable link.
9 kx
9 kx template<int size, bool big_endian>
9 kx Output_section*
9 kx Layout::layout_reloc(Sized_relobj_file<size, big_endian>*,
9 kx unsigned int,
9 kx const elfcpp::Shdr<size, big_endian>& shdr,
9 kx Output_section* data_section,
9 kx Relocatable_relocs* rr)
9 kx {
9 kx gold_assert(parameters->options().relocatable()
9 kx || parameters->options().emit_relocs());
9 kx
9 kx int sh_type = shdr.get_sh_type();
9 kx
9 kx std::string name;
9 kx if (sh_type == elfcpp::SHT_REL)
9 kx name = ".rel";
9 kx else if (sh_type == elfcpp::SHT_RELA)
9 kx name = ".rela";
9 kx else
9 kx gold_unreachable();
9 kx name += data_section->name();
9 kx
9 kx // If the output data section already has a reloc section, use that;
9 kx // otherwise, make a new one.
9 kx Output_section* os = data_section->reloc_section();
9 kx if (os == NULL)
9 kx {
9 kx const char* n = this->namepool_.add(name.c_str(), true, NULL);
9 kx os = this->make_output_section(n, sh_type, shdr.get_sh_flags(),
9 kx ORDER_INVALID, false);
9 kx os->set_should_link_to_symtab();
9 kx os->set_info_section(data_section);
9 kx data_section->set_reloc_section(os);
9 kx }
9 kx
9 kx Output_section_data* posd;
9 kx if (sh_type == elfcpp::SHT_REL)
9 kx {
9 kx os->set_entsize(elfcpp::Elf_sizes<size>::rel_size);
9 kx posd = new Output_relocatable_relocs<elfcpp::SHT_REL,
9 kx size,
9 kx big_endian>(rr);
9 kx }
9 kx else if (sh_type == elfcpp::SHT_RELA)
9 kx {
9 kx os->set_entsize(elfcpp::Elf_sizes<size>::rela_size);
9 kx posd = new Output_relocatable_relocs<elfcpp::SHT_RELA,
9 kx size,
9 kx big_endian>(rr);
9 kx }
9 kx else
9 kx gold_unreachable();
9 kx
9 kx os->add_output_section_data(posd);
9 kx rr->set_output_data(posd);
9 kx
9 kx return os;
9 kx }
9 kx
9 kx // Handle a group section when doing a relocatable link.
9 kx
9 kx template<int size, bool big_endian>
9 kx void
9 kx Layout::layout_group(Symbol_table* symtab,
9 kx Sized_relobj_file<size, big_endian>* object,
9 kx unsigned int,
9 kx const char* group_section_name,
9 kx const char* signature,
9 kx const elfcpp::Shdr<size, big_endian>& shdr,
9 kx elfcpp::Elf_Word flags,
9 kx std::vector<unsigned int>* shndxes)
9 kx {
9 kx gold_assert(parameters->options().relocatable());
9 kx gold_assert(shdr.get_sh_type() == elfcpp::SHT_GROUP);
9 kx group_section_name = this->namepool_.add(group_section_name, true, NULL);
9 kx Output_section* os = this->make_output_section(group_section_name,
9 kx elfcpp::SHT_GROUP,
9 kx shdr.get_sh_flags(),
9 kx ORDER_INVALID, false);
9 kx
9 kx // We need to find a symbol with the signature in the symbol table.
9 kx // If we don't find one now, we need to look again later.
9 kx Symbol* sym = symtab->lookup(signature, NULL);
9 kx if (sym != NULL)
9 kx os->set_info_symndx(sym);
9 kx else
9 kx {
9 kx // Reserve some space to minimize reallocations.
9 kx if (this->group_signatures_.empty())
9 kx this->group_signatures_.reserve(this->number_of_input_files_ * 16);
9 kx
9 kx // We will wind up using a symbol whose name is the signature.
9 kx // So just put the signature in the symbol name pool to save it.
9 kx signature = symtab->canonicalize_name(signature);
9 kx this->group_signatures_.push_back(Group_signature(os, signature));
9 kx }
9 kx
9 kx os->set_should_link_to_symtab();
9 kx os->set_entsize(4);
9 kx
9 kx section_size_type entry_count =
9 kx convert_to_section_size_type(shdr.get_sh_size() / 4);
9 kx Output_section_data* posd =
9 kx new Output_data_group<size, big_endian>(object, entry_count, flags,
9 kx shndxes);
9 kx os->add_output_section_data(posd);
9 kx }
9 kx
9 kx // Special GNU handling of sections name .eh_frame. They will
9 kx // normally hold exception frame data as defined by the C++ ABI
9 kx // (http://codesourcery.com/cxx-abi/).
9 kx
9 kx template<int size, bool big_endian>
9 kx Output_section*
9 kx Layout::layout_eh_frame(Sized_relobj_file<size, big_endian>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx const unsigned char* symbol_names,
9 kx off_t symbol_names_size,
9 kx unsigned int shndx,
9 kx const elfcpp::Shdr<size, big_endian>& shdr,
9 kx unsigned int reloc_shndx, unsigned int reloc_type,
9 kx off_t* off)
9 kx {
9 kx const unsigned int unwind_section_type =
9 kx parameters->target().unwind_section_type();
9 kx
9 kx gold_assert(shdr.get_sh_type() == elfcpp::SHT_PROGBITS
9 kx || shdr.get_sh_type() == unwind_section_type);
9 kx gold_assert((shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0);
9 kx
9 kx Output_section* os = this->make_eh_frame_section(object);
9 kx if (os == NULL)
9 kx return NULL;
9 kx
9 kx gold_assert(this->eh_frame_section_ == os);
9 kx
9 kx elfcpp::Elf_Xword orig_flags = os->flags();
9 kx
9 kx Eh_frame::Eh_frame_section_disposition disp =
9 kx Eh_frame::EH_UNRECOGNIZED_SECTION;
9 kx if (!parameters->incremental())
9 kx {
9 kx disp = this->eh_frame_data_->add_ehframe_input_section(object,
9 kx symbols,
9 kx symbols_size,
9 kx symbol_names,
9 kx symbol_names_size,
9 kx shndx,
9 kx reloc_shndx,
9 kx reloc_type);
9 kx }
9 kx
9 kx if (disp == Eh_frame::EH_OPTIMIZABLE_SECTION)
9 kx {
9 kx os->update_flags_for_input_section(shdr.get_sh_flags());
9 kx
9 kx // A writable .eh_frame section is a RELRO section.
9 kx if ((orig_flags & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR))
9 kx != (os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR)))
9 kx {
9 kx os->set_is_relro();
9 kx os->set_order(ORDER_RELRO);
9 kx }
9 kx
9 kx *off = -1;
9 kx return os;
9 kx }
9 kx
9 kx if (disp == Eh_frame::EH_END_MARKER_SECTION && !this->added_eh_frame_data_)
9 kx {
9 kx // We found the end marker section, so now we can add the set of
9 kx // optimized sections to the output section. We need to postpone
9 kx // adding this until we've found a section we can optimize so that
9 kx // the .eh_frame section in crtbeginT.o winds up at the start of
9 kx // the output section.
9 kx os->add_output_section_data(this->eh_frame_data_);
9 kx this->added_eh_frame_data_ = true;
9 kx }
9 kx
9 kx // We couldn't handle this .eh_frame section for some reason.
9 kx // Add it as a normal section.
9 kx bool saw_sections_clause = this->script_options_->saw_sections_clause();
9 kx *off = os->add_input_section(this, object, shndx, ".eh_frame", shdr,
9 kx reloc_shndx, saw_sections_clause);
9 kx this->have_added_input_section_ = true;
9 kx
9 kx if ((orig_flags & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR))
9 kx != (os->flags() & (elfcpp::SHF_WRITE | elfcpp::SHF_EXECINSTR)))
9 kx os->set_order(this->default_section_order(os, false));
9 kx
9 kx return os;
9 kx }
9 kx
9 kx void
9 kx Layout::finalize_eh_frame_section()
9 kx {
9 kx // If we never found an end marker section, we need to add the
9 kx // optimized eh sections to the output section now.
9 kx if (!parameters->incremental()
9 kx && this->eh_frame_section_ != NULL
9 kx && !this->added_eh_frame_data_)
9 kx {
9 kx this->eh_frame_section_->add_output_section_data(this->eh_frame_data_);
9 kx this->added_eh_frame_data_ = true;
9 kx }
9 kx }
9 kx
9 kx // Create and return the magic .eh_frame section. Create
9 kx // .eh_frame_hdr also if appropriate. OBJECT is the object with the
9 kx // input .eh_frame section; it may be NULL.
9 kx
9 kx Output_section*
9 kx Layout::make_eh_frame_section(const Relobj* object)
9 kx {
9 kx const unsigned int unwind_section_type =
9 kx parameters->target().unwind_section_type();
9 kx
9 kx Output_section* os = this->choose_output_section(object, ".eh_frame",
9 kx unwind_section_type,
9 kx elfcpp::SHF_ALLOC, false,
9 kx ORDER_EHFRAME, false, false,
9 kx false);
9 kx if (os == NULL)
9 kx return NULL;
9 kx
9 kx if (this->eh_frame_section_ == NULL)
9 kx {
9 kx this->eh_frame_section_ = os;
9 kx this->eh_frame_data_ = new Eh_frame();
9 kx
9 kx // For incremental linking, we do not optimize .eh_frame sections
9 kx // or create a .eh_frame_hdr section.
9 kx if (parameters->options().eh_frame_hdr() && !parameters->incremental())
9 kx {
9 kx Output_section* hdr_os =
9 kx this->choose_output_section(NULL, ".eh_frame_hdr",
9 kx unwind_section_type,
9 kx elfcpp::SHF_ALLOC, false,
9 kx ORDER_EHFRAME, false, false,
9 kx false);
9 kx
9 kx if (hdr_os != NULL)
9 kx {
9 kx Eh_frame_hdr* hdr_posd = new Eh_frame_hdr(os,
9 kx this->eh_frame_data_);
9 kx hdr_os->add_output_section_data(hdr_posd);
9 kx
9 kx hdr_os->set_after_input_sections();
9 kx
9 kx if (!this->script_options_->saw_phdrs_clause())
9 kx {
9 kx Output_segment* hdr_oseg;
9 kx hdr_oseg = this->make_output_segment(elfcpp::PT_GNU_EH_FRAME,
9 kx elfcpp::PF_R);
9 kx hdr_oseg->add_output_section_to_nonload(hdr_os,
9 kx elfcpp::PF_R);
9 kx }
9 kx
9 kx this->eh_frame_data_->set_eh_frame_hdr(hdr_posd);
9 kx }
9 kx }
9 kx }
9 kx
9 kx return os;
9 kx }
9 kx
9 kx // Add an exception frame for a PLT. This is called from target code.
9 kx
9 kx void
9 kx Layout::add_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
9 kx size_t cie_length, const unsigned char* fde_data,
9 kx size_t fde_length)
9 kx {
9 kx if (parameters->incremental())
9 kx {
9 kx // FIXME: Maybe this could work some day....
9 kx return;
9 kx }
9 kx Output_section* os = this->make_eh_frame_section(NULL);
9 kx if (os == NULL)
9 kx return;
9 kx this->eh_frame_data_->add_ehframe_for_plt(plt, cie_data, cie_length,
9 kx fde_data, fde_length);
9 kx if (!this->added_eh_frame_data_)
9 kx {
9 kx os->add_output_section_data(this->eh_frame_data_);
9 kx this->added_eh_frame_data_ = true;
9 kx }
9 kx }
9 kx
9 kx // Remove all post-map .eh_frame information for a PLT.
9 kx
9 kx void
9 kx Layout::remove_eh_frame_for_plt(Output_data* plt, const unsigned char* cie_data,
9 kx size_t cie_length)
9 kx {
9 kx if (parameters->incremental())
9 kx {
9 kx // FIXME: Maybe this could work some day....
9 kx return;
9 kx }
9 kx this->eh_frame_data_->remove_ehframe_for_plt(plt, cie_data, cie_length);
9 kx }
9 kx
9 kx // Scan a .debug_info or .debug_types section, and add summary
9 kx // information to the .gdb_index section.
9 kx
9 kx template<int size, bool big_endian>
9 kx void
9 kx Layout::add_to_gdb_index(bool is_type_unit,
9 kx Sized_relobj<size, big_endian>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx unsigned int shndx,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type)
9 kx {
9 kx if (this->gdb_index_data_ == NULL)
9 kx {
9 kx Output_section* os = this->choose_output_section(NULL, ".gdb_index",
9 kx elfcpp::SHT_PROGBITS, 0,
9 kx false, ORDER_INVALID,
9 kx false, false, false);
9 kx if (os == NULL)
9 kx return;
9 kx
9 kx this->gdb_index_data_ = new Gdb_index(os);
9 kx os->add_output_section_data(this->gdb_index_data_);
9 kx os->set_after_input_sections();
9 kx }
9 kx
9 kx this->gdb_index_data_->scan_debug_info(is_type_unit, object, symbols,
9 kx symbols_size, shndx, reloc_shndx,
9 kx reloc_type);
9 kx }
9 kx
9 kx // Add POSD to an output section using NAME, TYPE, and FLAGS. Return
9 kx // the output section.
9 kx
9 kx Output_section*
9 kx Layout::add_output_section_data(const char* name, elfcpp::Elf_Word type,
9 kx elfcpp::Elf_Xword flags,
9 kx Output_section_data* posd,
9 kx Output_section_order order, bool is_relro)
9 kx {
9 kx Output_section* os = this->choose_output_section(NULL, name, type, flags,
9 kx false, order, is_relro,
9 kx false, false);
9 kx if (os != NULL)
9 kx os->add_output_section_data(posd);
9 kx return os;
9 kx }
9 kx
9 kx // Map section flags to segment flags.
9 kx
9 kx elfcpp::Elf_Word
9 kx Layout::section_flags_to_segment(elfcpp::Elf_Xword flags)
9 kx {
9 kx elfcpp::Elf_Word ret = elfcpp::PF_R;
9 kx if ((flags & elfcpp::SHF_WRITE) != 0)
9 kx ret |= elfcpp::PF_W;
9 kx if ((flags & elfcpp::SHF_EXECINSTR) != 0)
9 kx ret |= elfcpp::PF_X;
9 kx return ret;
9 kx }
9 kx
9 kx // Make a new Output_section, and attach it to segments as
9 kx // appropriate. ORDER is the order in which this section should
9 kx // appear in the output segment. IS_RELRO is true if this is a relro
9 kx // (read-only after relocations) section.
9 kx
9 kx Output_section*
9 kx Layout::make_output_section(const char* name, elfcpp::Elf_Word type,
9 kx elfcpp::Elf_Xword flags,
9 kx Output_section_order order, bool is_relro)
9 kx {
9 kx Output_section* os;
9 kx if ((flags & elfcpp::SHF_ALLOC) == 0
9 kx && strcmp(parameters->options().compress_debug_sections(), "none") != 0
9 kx && is_compressible_debug_section(name))
9 kx os = new Output_compressed_section(¶meters->options(), name, type,
9 kx flags);
9 kx else if ((flags & elfcpp::SHF_ALLOC) == 0
9 kx && parameters->options().strip_debug_non_line()
9 kx && strcmp(".debug_abbrev", name) == 0)
9 kx {
9 kx os = this->debug_abbrev_ = new Output_reduced_debug_abbrev_section(
9 kx name, type, flags);
9 kx if (this->debug_info_)
9 kx this->debug_info_->set_abbreviations(this->debug_abbrev_);
9 kx }
9 kx else if ((flags & elfcpp::SHF_ALLOC) == 0
9 kx && parameters->options().strip_debug_non_line()
9 kx && strcmp(".debug_info", name) == 0)
9 kx {
9 kx os = this->debug_info_ = new Output_reduced_debug_info_section(
9 kx name, type, flags);
9 kx if (this->debug_abbrev_)
9 kx this->debug_info_->set_abbreviations(this->debug_abbrev_);
9 kx }
9 kx else
9 kx {
9 kx // Sometimes .init_array*, .preinit_array* and .fini_array* do
9 kx // not have correct section types. Force them here.
9 kx if (type == elfcpp::SHT_PROGBITS)
9 kx {
9 kx if (is_prefix_of(".init_array", name))
9 kx type = elfcpp::SHT_INIT_ARRAY;
9 kx else if (is_prefix_of(".preinit_array", name))
9 kx type = elfcpp::SHT_PREINIT_ARRAY;
9 kx else if (is_prefix_of(".fini_array", name))
9 kx type = elfcpp::SHT_FINI_ARRAY;
9 kx }
9 kx
9 kx // FIXME: const_cast is ugly.
9 kx Target* target = const_cast<Target*>(¶meters->target());
9 kx os = target->make_output_section(name, type, flags);
9 kx }
9 kx
9 kx // With -z relro, we have to recognize the special sections by name.
9 kx // There is no other way.
9 kx bool is_relro_local = false;
9 kx if (!this->script_options_->saw_sections_clause()
9 kx && parameters->options().relro()
9 kx && (flags & elfcpp::SHF_ALLOC) != 0
9 kx && (flags & elfcpp::SHF_WRITE) != 0)
9 kx {
9 kx if (type == elfcpp::SHT_PROGBITS)
9 kx {
9 kx if ((flags & elfcpp::SHF_TLS) != 0)
9 kx is_relro = true;
9 kx else if (strcmp(name, ".data.rel.ro") == 0)
9 kx is_relro = true;
9 kx else if (strcmp(name, ".data.rel.ro.local") == 0)
9 kx {
9 kx is_relro = true;
9 kx is_relro_local = true;
9 kx }
9 kx else if (strcmp(name, ".ctors") == 0
9 kx || strcmp(name, ".dtors") == 0
9 kx || strcmp(name, ".jcr") == 0)
9 kx is_relro = true;
9 kx }
9 kx else if (type == elfcpp::SHT_INIT_ARRAY
9 kx || type == elfcpp::SHT_FINI_ARRAY
9 kx || type == elfcpp::SHT_PREINIT_ARRAY)
9 kx is_relro = true;
9 kx }
9 kx
9 kx if (is_relro)
9 kx os->set_is_relro();
9 kx
9 kx if (order == ORDER_INVALID && (flags & elfcpp::SHF_ALLOC) != 0)
9 kx order = this->default_section_order(os, is_relro_local);
9 kx
9 kx os->set_order(order);
9 kx
9 kx parameters->target().new_output_section(os);
9 kx
9 kx this->section_list_.push_back(os);
9 kx
9 kx // The GNU linker by default sorts some sections by priority, so we
9 kx // do the same. We need to know that this might happen before we
9 kx // attach any input sections.
9 kx if (!this->script_options_->saw_sections_clause()
9 kx && !parameters->options().relocatable()
9 kx && (strcmp(name, ".init_array") == 0
9 kx || strcmp(name, ".fini_array") == 0
9 kx || (!parameters->options().ctors_in_init_array()
9 kx && (strcmp(name, ".ctors") == 0
9 kx || strcmp(name, ".dtors") == 0))))
9 kx os->set_may_sort_attached_input_sections();
9 kx
9 kx // The GNU linker by default sorts .text.{unlikely,exit,startup,hot}
9 kx // sections before other .text sections. We are compatible. We
9 kx // need to know that this might happen before we attach any input
9 kx // sections.
9 kx if (parameters->options().text_reorder()
9 kx && !this->script_options_->saw_sections_clause()
9 kx && !this->is_section_ordering_specified()
9 kx && !parameters->options().relocatable()
9 kx && strcmp(name, ".text") == 0)
9 kx os->set_may_sort_attached_input_sections();
9 kx
9 kx // GNU linker sorts section by name with --sort-section=name.
9 kx if (strcmp(parameters->options().sort_section(), "name") == 0)
9 kx os->set_must_sort_attached_input_sections();
9 kx
9 kx // Check for .stab*str sections, as .stab* sections need to link to
9 kx // them.
9 kx if (type == elfcpp::SHT_STRTAB
9 kx && !this->have_stabstr_section_
9 kx && strncmp(name, ".stab", 5) == 0
9 kx && strcmp(name + strlen(name) - 3, "str") == 0)
9 kx this->have_stabstr_section_ = true;
9 kx
9 kx // During a full incremental link, we add patch space to most
9 kx // PROGBITS and NOBITS sections. Flag those that may be
9 kx // arbitrarily padded.
9 kx if ((type == elfcpp::SHT_PROGBITS || type == elfcpp::SHT_NOBITS)
9 kx && order != ORDER_INTERP
9 kx && order != ORDER_INIT
9 kx && order != ORDER_PLT
9 kx && order != ORDER_FINI
9 kx && order != ORDER_RELRO_LAST
9 kx && order != ORDER_NON_RELRO_FIRST
9 kx && strcmp(name, ".eh_frame") != 0
9 kx && strcmp(name, ".ctors") != 0
9 kx && strcmp(name, ".dtors") != 0
9 kx && strcmp(name, ".jcr") != 0)
9 kx {
9 kx os->set_is_patch_space_allowed();
9 kx
9 kx // Certain sections require "holes" to be filled with
9 kx // specific fill patterns. These fill patterns may have
9 kx // a minimum size, so we must prevent allocations from the
9 kx // free list that leave a hole smaller than the minimum.
9 kx if (strcmp(name, ".debug_info") == 0)
9 kx os->set_free_space_fill(new Output_fill_debug_info(false));
9 kx else if (strcmp(name, ".debug_types") == 0)
9 kx os->set_free_space_fill(new Output_fill_debug_info(true));
9 kx else if (strcmp(name, ".debug_line") == 0)
9 kx os->set_free_space_fill(new Output_fill_debug_line());
9 kx }
9 kx
9 kx // If we have already attached the sections to segments, then we
9 kx // need to attach this one now. This happens for sections created
9 kx // directly by the linker.
9 kx if (this->sections_are_attached_)
9 kx this->attach_section_to_segment(¶meters->target(), os);
9 kx
9 kx return os;
9 kx }
9 kx
9 kx // Return the default order in which a section should be placed in an
9 kx // output segment. This function captures a lot of the ideas in
9 kx // ld/scripttempl/elf.sc in the GNU linker. Note that the order of a
9 kx // linker created section is normally set when the section is created;
9 kx // this function is used for input sections.
9 kx
9 kx Output_section_order
9 kx Layout::default_section_order(Output_section* os, bool is_relro_local)
9 kx {
9 kx gold_assert((os->flags() & elfcpp::SHF_ALLOC) != 0);
9 kx bool is_write = (os->flags() & elfcpp::SHF_WRITE) != 0;
9 kx bool is_execinstr = (os->flags() & elfcpp::SHF_EXECINSTR) != 0;
9 kx bool is_bss = false;
9 kx
9 kx switch (os->type())
9 kx {
9 kx default:
9 kx case elfcpp::SHT_PROGBITS:
9 kx break;
9 kx case elfcpp::SHT_NOBITS:
9 kx is_bss = true;
9 kx break;
9 kx case elfcpp::SHT_RELA:
9 kx case elfcpp::SHT_REL:
9 kx if (!is_write)
9 kx return ORDER_DYNAMIC_RELOCS;
9 kx break;
9 kx case elfcpp::SHT_HASH:
9 kx case elfcpp::SHT_DYNAMIC:
9 kx case elfcpp::SHT_SHLIB:
9 kx case elfcpp::SHT_DYNSYM:
9 kx case elfcpp::SHT_GNU_HASH:
9 kx case elfcpp::SHT_GNU_verdef:
9 kx case elfcpp::SHT_GNU_verneed:
9 kx case elfcpp::SHT_GNU_versym:
9 kx if (!is_write)
9 kx return ORDER_DYNAMIC_LINKER;
9 kx break;
9 kx case elfcpp::SHT_NOTE:
9 kx return is_write ? ORDER_RW_NOTE : ORDER_RO_NOTE;
9 kx }
9 kx
9 kx if ((os->flags() & elfcpp::SHF_TLS) != 0)
9 kx return is_bss ? ORDER_TLS_BSS : ORDER_TLS_DATA;
9 kx
9 kx if (!is_bss && !is_write)
9 kx {
9 kx if (is_execinstr)
9 kx {
9 kx if (strcmp(os->name(), ".init") == 0)
9 kx return ORDER_INIT;
9 kx else if (strcmp(os->name(), ".fini") == 0)
9 kx return ORDER_FINI;
9 kx else if (parameters->options().keep_text_section_prefix())
9 kx {
9 kx // -z,keep-text-section-prefix introduces additional
9 kx // output sections.
9 kx if (strcmp(os->name(), ".text.hot") == 0)
9 kx return ORDER_TEXT_HOT;
9 kx else if (strcmp(os->name(), ".text.startup") == 0)
9 kx return ORDER_TEXT_STARTUP;
9 kx else if (strcmp(os->name(), ".text.exit") == 0)
9 kx return ORDER_TEXT_EXIT;
9 kx else if (strcmp(os->name(), ".text.unlikely") == 0)
9 kx return ORDER_TEXT_UNLIKELY;
9 kx }
9 kx }
9 kx return is_execinstr ? ORDER_TEXT : ORDER_READONLY;
9 kx }
9 kx
9 kx if (os->is_relro())
9 kx return is_relro_local ? ORDER_RELRO_LOCAL : ORDER_RELRO;
9 kx
9 kx if (os->is_small_section())
9 kx return is_bss ? ORDER_SMALL_BSS : ORDER_SMALL_DATA;
9 kx if (os->is_large_section())
9 kx return is_bss ? ORDER_LARGE_BSS : ORDER_LARGE_DATA;
9 kx
9 kx return is_bss ? ORDER_BSS : ORDER_DATA;
9 kx }
9 kx
9 kx // Attach output sections to segments. This is called after we have
9 kx // seen all the input sections.
9 kx
9 kx void
9 kx Layout::attach_sections_to_segments(const Target* target)
9 kx {
9 kx for (Section_list::iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx this->attach_section_to_segment(target, *p);
9 kx
9 kx this->sections_are_attached_ = true;
9 kx }
9 kx
9 kx // Attach an output section to a segment.
9 kx
9 kx void
9 kx Layout::attach_section_to_segment(const Target* target, Output_section* os)
9 kx {
9 kx if ((os->flags() & elfcpp::SHF_ALLOC) == 0)
9 kx this->unattached_section_list_.push_back(os);
9 kx else
9 kx this->attach_allocated_section_to_segment(target, os);
9 kx }
9 kx
9 kx // Attach an allocated output section to a segment.
9 kx
9 kx void
9 kx Layout::attach_allocated_section_to_segment(const Target* target,
9 kx Output_section* os)
9 kx {
9 kx elfcpp::Elf_Xword flags = os->flags();
9 kx gold_assert((flags & elfcpp::SHF_ALLOC) != 0);
9 kx
9 kx if (parameters->options().relocatable())
9 kx return;
9 kx
9 kx // If we have a SECTIONS clause, we can't handle the attachment to
9 kx // segments until after we've seen all the sections.
9 kx if (this->script_options_->saw_sections_clause())
9 kx return;
9 kx
9 kx gold_assert(!this->script_options_->saw_phdrs_clause());
9 kx
9 kx // This output section goes into a PT_LOAD segment.
9 kx
9 kx elfcpp::Elf_Word seg_flags = Layout::section_flags_to_segment(flags);
9 kx
9 kx // If this output section's segment has extra flags that need to be set,
9 kx // coming from a linker plugin, do that.
9 kx seg_flags |= os->extra_segment_flags();
9 kx
9 kx // Check for --section-start.
9 kx uint64_t addr;
9 kx bool is_address_set = parameters->options().section_start(os->name(), &addr);
9 kx
9 kx // In general the only thing we really care about for PT_LOAD
9 kx // segments is whether or not they are writable or executable,
9 kx // so that is how we search for them.
9 kx // Large data sections also go into their own PT_LOAD segment.
9 kx // People who need segments sorted on some other basis will
9 kx // have to use a linker script.
9 kx
9 kx Segment_list::const_iterator p;
9 kx if (!os->is_unique_segment())
9 kx {
9 kx for (p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->type() != elfcpp::PT_LOAD)
9 kx continue;
9 kx if ((*p)->is_unique_segment())
9 kx continue;
9 kx if (!parameters->options().omagic()
9 kx && ((*p)->flags() & elfcpp::PF_W) != (seg_flags & elfcpp::PF_W))
9 kx continue;
9 kx if ((target->isolate_execinstr() || parameters->options().rosegment())
9 kx && ((*p)->flags() & elfcpp::PF_X) != (seg_flags & elfcpp::PF_X))
9 kx continue;
9 kx // If -Tbss was specified, we need to separate the data and BSS
9 kx // segments.
9 kx if (parameters->options().user_set_Tbss())
9 kx {
9 kx if ((os->type() == elfcpp::SHT_NOBITS)
9 kx == (*p)->has_any_data_sections())
9 kx continue;
9 kx }
9 kx if (os->is_large_data_section() && !(*p)->is_large_data_segment())
9 kx continue;
9 kx
9 kx if (is_address_set)
9 kx {
9 kx if ((*p)->are_addresses_set())
9 kx continue;
9 kx
9 kx (*p)->add_initial_output_data(os);
9 kx (*p)->update_flags_for_output_section(seg_flags);
9 kx (*p)->set_addresses(addr, addr);
9 kx break;
9 kx }
9 kx
9 kx (*p)->add_output_section_to_load(this, os, seg_flags);
9 kx break;
9 kx }
9 kx }
9 kx
9 kx if (p == this->segment_list_.end()
9 kx || os->is_unique_segment())
9 kx {
9 kx Output_segment* oseg = this->make_output_segment(elfcpp::PT_LOAD,
9 kx seg_flags);
9 kx if (os->is_large_data_section())
9 kx oseg->set_is_large_data_segment();
9 kx oseg->add_output_section_to_load(this, os, seg_flags);
9 kx if (is_address_set)
9 kx oseg->set_addresses(addr, addr);
9 kx // Check if segment should be marked unique. For segments marked
9 kx // unique by linker plugins, set the new alignment if specified.
9 kx if (os->is_unique_segment())
9 kx {
9 kx oseg->set_is_unique_segment();
9 kx if (os->segment_alignment() != 0)
9 kx oseg->set_minimum_p_align(os->segment_alignment());
9 kx }
9 kx }
9 kx
9 kx // If we see a loadable SHT_NOTE section, we create a PT_NOTE
9 kx // segment.
9 kx if (os->type() == elfcpp::SHT_NOTE)
9 kx {
9 kx uint64_t os_align = os->addralign();
9 kx
9 kx // See if we already have an equivalent PT_NOTE segment.
9 kx for (p = this->segment_list_.begin();
9 kx p != segment_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->type() == elfcpp::PT_NOTE
9 kx && (*p)->align() == os_align
9 kx && (((*p)->flags() & elfcpp::PF_W)
9 kx == (seg_flags & elfcpp::PF_W)))
9 kx {
9 kx (*p)->add_output_section_to_nonload(os, seg_flags);
9 kx break;
9 kx }
9 kx }
9 kx
9 kx if (p == this->segment_list_.end())
9 kx {
9 kx Output_segment* oseg = this->make_output_segment(elfcpp::PT_NOTE,
9 kx seg_flags);
9 kx oseg->add_output_section_to_nonload(os, seg_flags);
9 kx oseg->set_align(os_align);
9 kx }
9 kx }
9 kx
9 kx // If we see a loadable SHF_TLS section, we create a PT_TLS
9 kx // segment. There can only be one such segment.
9 kx if ((flags & elfcpp::SHF_TLS) != 0)
9 kx {
9 kx if (this->tls_segment_ == NULL)
9 kx this->make_output_segment(elfcpp::PT_TLS, seg_flags);
9 kx this->tls_segment_->add_output_section_to_nonload(os, seg_flags);
9 kx }
9 kx
9 kx // If -z relro is in effect, and we see a relro section, we create a
9 kx // PT_GNU_RELRO segment. There can only be one such segment.
9 kx if (os->is_relro() && parameters->options().relro())
9 kx {
9 kx gold_assert(seg_flags == (elfcpp::PF_R | elfcpp::PF_W));
9 kx if (this->relro_segment_ == NULL)
9 kx this->make_output_segment(elfcpp::PT_GNU_RELRO, seg_flags);
9 kx this->relro_segment_->add_output_section_to_nonload(os, seg_flags);
9 kx }
9 kx
9 kx // If we see a section named .interp, put it into a PT_INTERP
9 kx // segment. This seems broken to me, but this is what GNU ld does,
9 kx // and glibc expects it.
9 kx if (strcmp(os->name(), ".interp") == 0
9 kx && !this->script_options_->saw_phdrs_clause())
9 kx {
9 kx if (this->interp_segment_ == NULL)
9 kx this->make_output_segment(elfcpp::PT_INTERP, seg_flags);
9 kx else
9 kx gold_warning(_("multiple '.interp' sections in input files "
9 kx "may cause confusing PT_INTERP segment"));
9 kx this->interp_segment_->add_output_section_to_nonload(os, seg_flags);
9 kx }
9 kx }
9 kx
9 kx // Make an output section for a script.
9 kx
9 kx Output_section*
9 kx Layout::make_output_section_for_script(
9 kx const char* name,
9 kx Script_sections::Section_type section_type)
9 kx {
9 kx name = this->namepool_.add(name, false, NULL);
9 kx elfcpp::Elf_Xword sh_flags = elfcpp::SHF_ALLOC;
9 kx if (section_type == Script_sections::ST_NOLOAD)
9 kx sh_flags = 0;
9 kx Output_section* os = this->make_output_section(name, elfcpp::SHT_PROGBITS,
9 kx sh_flags, ORDER_INVALID,
9 kx false);
9 kx os->set_found_in_sections_clause();
9 kx if (section_type == Script_sections::ST_NOLOAD)
9 kx os->set_is_noload();
9 kx return os;
9 kx }
9 kx
9 kx // Return the number of segments we expect to see.
9 kx
9 kx size_t
9 kx Layout::expected_segment_count() const
9 kx {
9 kx size_t ret = this->segment_list_.size();
9 kx
9 kx // If we didn't see a SECTIONS clause in a linker script, we should
9 kx // already have the complete list of segments. Otherwise we ask the
9 kx // SECTIONS clause how many segments it expects, and add in the ones
9 kx // we already have (PT_GNU_STACK, PT_GNU_EH_FRAME, etc.)
9 kx
9 kx if (!this->script_options_->saw_sections_clause())
9 kx return ret;
9 kx else
9 kx {
9 kx const Script_sections* ss = this->script_options_->script_sections();
9 kx return ret + ss->expected_segment_count(this);
9 kx }
9 kx }
9 kx
9 kx // Handle the .note.GNU-stack section at layout time. SEEN_GNU_STACK
9 kx // is whether we saw a .note.GNU-stack section in the object file.
9 kx // GNU_STACK_FLAGS is the section flags. The flags give the
9 kx // protection required for stack memory. We record this in an
9 kx // executable as a PT_GNU_STACK segment. If an object file does not
9 kx // have a .note.GNU-stack segment, we must assume that it is an old
9 kx // object. On some targets that will force an executable stack.
9 kx
9 kx void
9 kx Layout::layout_gnu_stack(bool seen_gnu_stack, uint64_t gnu_stack_flags,
9 kx const Object* obj)
9 kx {
9 kx if (!seen_gnu_stack)
9 kx {
9 kx this->input_without_gnu_stack_note_ = true;
9 kx if (parameters->options().warn_execstack()
9 kx && parameters->target().is_default_stack_executable())
9 kx gold_warning(_("%s: missing .note.GNU-stack section"
9 kx " implies executable stack"),
9 kx obj->name().c_str());
9 kx }
9 kx else
9 kx {
9 kx this->input_with_gnu_stack_note_ = true;
9 kx if ((gnu_stack_flags & elfcpp::SHF_EXECINSTR) != 0)
9 kx {
9 kx this->input_requires_executable_stack_ = true;
9 kx if (parameters->options().warn_execstack())
9 kx gold_warning(_("%s: requires executable stack"),
9 kx obj->name().c_str());
9 kx }
9 kx }
9 kx }
9 kx
9 kx // Read a value with given size and endianness.
9 kx
9 kx static inline uint64_t
9 kx read_sized_value(size_t size, const unsigned char* buf, bool is_big_endian,
9 kx const Object* object)
9 kx {
9 kx uint64_t val = 0;
9 kx if (size == 4)
9 kx {
9 kx if (is_big_endian)
9 kx val = elfcpp::Swap<32, true>::readval(buf);
9 kx else
9 kx val = elfcpp::Swap<32, false>::readval(buf);
9 kx }
9 kx else if (size == 8)
9 kx {
9 kx if (is_big_endian)
9 kx val = elfcpp::Swap<64, true>::readval(buf);
9 kx else
9 kx val = elfcpp::Swap<64, false>::readval(buf);
9 kx }
9 kx else
9 kx {
9 kx gold_warning(_("%s: in .note.gnu.property section, "
9 kx "pr_datasz must be 4 or 8"),
9 kx object->name().c_str());
9 kx }
9 kx return val;
9 kx }
9 kx
9 kx // Write a value with given size and endianness.
9 kx
9 kx static inline void
9 kx write_sized_value(uint64_t value, size_t size, unsigned char* buf,
9 kx bool is_big_endian)
9 kx {
9 kx if (size == 4)
9 kx {
9 kx if (is_big_endian)
9 kx elfcpp::Swap<32, true>::writeval(buf, static_cast<uint32_t>(value));
9 kx else
9 kx elfcpp::Swap<32, false>::writeval(buf, static_cast<uint32_t>(value));
9 kx }
9 kx else if (size == 8)
9 kx {
9 kx if (is_big_endian)
9 kx elfcpp::Swap<64, true>::writeval(buf, value);
9 kx else
9 kx elfcpp::Swap<64, false>::writeval(buf, value);
9 kx }
9 kx else
9 kx {
9 kx // We will have already complained about this.
9 kx }
9 kx }
9 kx
9 kx // Handle the .note.gnu.property section at layout time.
9 kx
9 kx void
9 kx Layout::layout_gnu_property(unsigned int note_type,
9 kx unsigned int pr_type,
9 kx size_t pr_datasz,
9 kx const unsigned char* pr_data,
9 kx const Object* object)
9 kx {
9 kx // We currently support only the one note type.
9 kx gold_assert(note_type == elfcpp::NT_GNU_PROPERTY_TYPE_0);
9 kx
9 kx if (pr_type >= elfcpp::GNU_PROPERTY_LOPROC
9 kx && pr_type < elfcpp::GNU_PROPERTY_HIPROC)
9 kx {
9 kx // Target-dependent property value; call the target to record.
9 kx const int size = parameters->target().get_size();
9 kx const bool is_big_endian = parameters->target().is_big_endian();
9 kx if (size == 32)
9 kx {
9 kx if (is_big_endian)
9 kx {
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx parameters->sized_target<32, true>()->
9 kx record_gnu_property(note_type, pr_type, pr_datasz, pr_data,
9 kx object);
9 kx #else
9 kx gold_unreachable();
9 kx #endif
9 kx }
9 kx else
9 kx {
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx parameters->sized_target<32, false>()->
9 kx record_gnu_property(note_type, pr_type, pr_datasz, pr_data,
9 kx object);
9 kx #else
9 kx gold_unreachable();
9 kx #endif
9 kx }
9 kx }
9 kx else if (size == 64)
9 kx {
9 kx if (is_big_endian)
9 kx {
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx parameters->sized_target<64, true>()->
9 kx record_gnu_property(note_type, pr_type, pr_datasz, pr_data,
9 kx object);
9 kx #else
9 kx gold_unreachable();
9 kx #endif
9 kx }
9 kx else
9 kx {
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx parameters->sized_target<64, false>()->
9 kx record_gnu_property(note_type, pr_type, pr_datasz, pr_data,
9 kx object);
9 kx #else
9 kx gold_unreachable();
9 kx #endif
9 kx }
9 kx }
9 kx else
9 kx gold_unreachable();
9 kx return;
9 kx }
9 kx
9 kx Gnu_properties::iterator pprop = this->gnu_properties_.find(pr_type);
9 kx if (pprop == this->gnu_properties_.end())
9 kx {
9 kx Gnu_property prop;
9 kx prop.pr_datasz = pr_datasz;
9 kx prop.pr_data = new unsigned char[pr_datasz];
9 kx memcpy(prop.pr_data, pr_data, pr_datasz);
9 kx this->gnu_properties_[pr_type] = prop;
9 kx }
9 kx else
9 kx {
9 kx const bool is_big_endian = parameters->target().is_big_endian();
9 kx switch (pr_type)
9 kx {
9 kx case elfcpp::GNU_PROPERTY_STACK_SIZE:
9 kx // Record the maximum value seen.
9 kx {
9 kx uint64_t val1 = read_sized_value(pprop->second.pr_datasz,
9 kx pprop->second.pr_data,
9 kx is_big_endian, object);
9 kx uint64_t val2 = read_sized_value(pr_datasz, pr_data,
9 kx is_big_endian, object);
9 kx if (val2 > val1)
9 kx write_sized_value(val2, pprop->second.pr_datasz,
9 kx pprop->second.pr_data, is_big_endian);
9 kx }
9 kx break;
9 kx case elfcpp::GNU_PROPERTY_NO_COPY_ON_PROTECTED:
9 kx // No data to merge.
9 kx break;
9 kx default:
9 kx gold_warning(_("%s: unknown program property type %d "
9 kx "in .note.gnu.property section"),
9 kx object->name().c_str(), pr_type);
9 kx }
9 kx }
9 kx }
9 kx
9 kx // Merge per-object properties with program properties.
9 kx // This lets the target identify objects that are missing certain
9 kx // properties, in cases where properties must be ANDed together.
9 kx
9 kx void
9 kx Layout::merge_gnu_properties(const Object* object)
9 kx {
9 kx const int size = parameters->target().get_size();
9 kx const bool is_big_endian = parameters->target().is_big_endian();
9 kx if (size == 32)
9 kx {
9 kx if (is_big_endian)
9 kx {
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx parameters->sized_target<32, true>()->merge_gnu_properties(object);
9 kx #else
9 kx gold_unreachable();
9 kx #endif
9 kx }
9 kx else
9 kx {
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx parameters->sized_target<32, false>()->merge_gnu_properties(object);
9 kx #else
9 kx gold_unreachable();
9 kx #endif
9 kx }
9 kx }
9 kx else if (size == 64)
9 kx {
9 kx if (is_big_endian)
9 kx {
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx parameters->sized_target<64, true>()->merge_gnu_properties(object);
9 kx #else
9 kx gold_unreachable();
9 kx #endif
9 kx }
9 kx else
9 kx {
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx parameters->sized_target<64, false>()->merge_gnu_properties(object);
9 kx #else
9 kx gold_unreachable();
9 kx #endif
9 kx }
9 kx }
9 kx else
9 kx gold_unreachable();
9 kx }
9 kx
9 kx // Add a target-specific property for the output .note.gnu.property section.
9 kx
9 kx void
9 kx Layout::add_gnu_property(unsigned int note_type,
9 kx unsigned int pr_type,
9 kx size_t pr_datasz,
9 kx const unsigned char* pr_data)
9 kx {
9 kx gold_assert(note_type == elfcpp::NT_GNU_PROPERTY_TYPE_0);
9 kx
9 kx Gnu_property prop;
9 kx prop.pr_datasz = pr_datasz;
9 kx prop.pr_data = new unsigned char[pr_datasz];
9 kx memcpy(prop.pr_data, pr_data, pr_datasz);
9 kx this->gnu_properties_[pr_type] = prop;
9 kx }
9 kx
9 kx // Create automatic note sections.
9 kx
9 kx void
9 kx Layout::create_notes()
9 kx {
9 kx this->create_gnu_properties_note();
9 kx this->create_gold_note();
9 kx this->create_stack_segment();
9 kx this->create_build_id();
9 kx this->create_package_metadata();
9 kx }
9 kx
9 kx // Create the dynamic sections which are needed before we read the
9 kx // relocs.
9 kx
9 kx void
9 kx Layout::create_initial_dynamic_sections(Symbol_table* symtab)
9 kx {
9 kx if (parameters->doing_static_link())
9 kx return;
9 kx
9 kx this->dynamic_section_ = this->choose_output_section(NULL, ".dynamic",
9 kx elfcpp::SHT_DYNAMIC,
9 kx (elfcpp::SHF_ALLOC
9 kx | elfcpp::SHF_WRITE),
9 kx false, ORDER_RELRO,
9 kx true, false, false);
9 kx
9 kx // A linker script may discard .dynamic, so check for NULL.
9 kx if (this->dynamic_section_ != NULL)
9 kx {
9 kx this->dynamic_symbol_ =
9 kx symtab->define_in_output_data("_DYNAMIC", NULL,
9 kx Symbol_table::PREDEFINED,
9 kx this->dynamic_section_, 0, 0,
9 kx elfcpp::STT_OBJECT, elfcpp::STB_LOCAL,
9 kx elfcpp::STV_HIDDEN, 0, false, false);
9 kx
9 kx this->dynamic_data_ = new Output_data_dynamic(&this->dynpool_);
9 kx
9 kx this->dynamic_section_->add_output_section_data(this->dynamic_data_);
9 kx }
9 kx }
9 kx
9 kx // For each output section whose name can be represented as C symbol,
9 kx // define __start and __stop symbols for the section. This is a GNU
9 kx // extension.
9 kx
9 kx void
9 kx Layout::define_section_symbols(Symbol_table* symtab)
9 kx {
9 kx const elfcpp::STV visibility = parameters->options().start_stop_visibility_enum();
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx const char* const name = (*p)->name();
9 kx if (is_cident(name))
9 kx {
9 kx const std::string name_string(name);
9 kx const std::string start_name(cident_section_start_prefix
9 kx + name_string);
9 kx const std::string stop_name(cident_section_stop_prefix
9 kx + name_string);
9 kx
9 kx symtab->define_in_output_data(start_name.c_str(),
9 kx NULL, // version
9 kx Symbol_table::PREDEFINED,
9 kx *p,
9 kx 0, // value
9 kx 0, // symsize
9 kx elfcpp::STT_NOTYPE,
9 kx elfcpp::STB_GLOBAL,
9 kx visibility,
9 kx 0, // nonvis
9 kx false, // offset_is_from_end
9 kx true); // only_if_ref
9 kx
9 kx symtab->define_in_output_data(stop_name.c_str(),
9 kx NULL, // version
9 kx Symbol_table::PREDEFINED,
9 kx *p,
9 kx 0, // value
9 kx 0, // symsize
9 kx elfcpp::STT_NOTYPE,
9 kx elfcpp::STB_GLOBAL,
9 kx visibility,
9 kx 0, // nonvis
9 kx true, // offset_is_from_end
9 kx true); // only_if_ref
9 kx }
9 kx }
9 kx }
9 kx
9 kx // Define symbols for group signatures.
9 kx
9 kx void
9 kx Layout::define_group_signatures(Symbol_table* symtab)
9 kx {
9 kx for (Group_signatures::iterator p = this->group_signatures_.begin();
9 kx p != this->group_signatures_.end();
9 kx ++p)
9 kx {
9 kx Symbol* sym = symtab->lookup(p->signature, NULL);
9 kx if (sym != NULL)
9 kx p->section->set_info_symndx(sym);
9 kx else
9 kx {
9 kx // Force the name of the group section to the group
9 kx // signature, and use the group's section symbol as the
9 kx // signature symbol.
9 kx if (strcmp(p->section->name(), p->signature) != 0)
9 kx {
9 kx const char* name = this->namepool_.add(p->signature,
9 kx true, NULL);
9 kx p->section->set_name(name);
9 kx }
9 kx p->section->set_needs_symtab_index();
9 kx p->section->set_info_section_symndx(p->section);
9 kx }
9 kx }
9 kx
9 kx this->group_signatures_.clear();
9 kx }
9 kx
9 kx // Find the first read-only PT_LOAD segment, creating one if
9 kx // necessary.
9 kx
9 kx Output_segment*
9 kx Layout::find_first_load_seg(const Target* target)
9 kx {
9 kx Output_segment* best = NULL;
9 kx for (Segment_list::const_iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->type() == elfcpp::PT_LOAD
9 kx && ((*p)->flags() & elfcpp::PF_R) != 0
9 kx && (parameters->options().omagic()
9 kx || ((*p)->flags() & elfcpp::PF_W) == 0)
9 kx && (!target->isolate_execinstr()
9 kx || ((*p)->flags() & elfcpp::PF_X) == 0))
9 kx {
9 kx if (best == NULL || this->segment_precedes(*p, best))
9 kx best = *p;
9 kx }
9 kx }
9 kx if (best != NULL)
9 kx return best;
9 kx
9 kx gold_assert(!this->script_options_->saw_phdrs_clause());
9 kx
9 kx Output_segment* load_seg = this->make_output_segment(elfcpp::PT_LOAD,
9 kx elfcpp::PF_R);
9 kx return load_seg;
9 kx }
9 kx
9 kx // Save states of all current output segments. Store saved states
9 kx // in SEGMENT_STATES.
9 kx
9 kx void
9 kx Layout::save_segments(Segment_states* segment_states)
9 kx {
9 kx for (Segment_list::const_iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx {
9 kx Output_segment* segment = *p;
9 kx // Shallow copy.
9 kx Output_segment* copy = new Output_segment(*segment);
9 kx (*segment_states)[segment] = copy;
9 kx }
9 kx }
9 kx
9 kx // Restore states of output segments and delete any segment not found in
9 kx // SEGMENT_STATES.
9 kx
9 kx void
9 kx Layout::restore_segments(const Segment_states* segment_states)
9 kx {
9 kx // Go through the segment list and remove any segment added in the
9 kx // relaxation loop.
9 kx this->tls_segment_ = NULL;
9 kx this->relro_segment_ = NULL;
9 kx Segment_list::iterator list_iter = this->segment_list_.begin();
9 kx while (list_iter != this->segment_list_.end())
9 kx {
9 kx Output_segment* segment = *list_iter;
9 kx Segment_states::const_iterator states_iter =
9 kx segment_states->find(segment);
9 kx if (states_iter != segment_states->end())
9 kx {
9 kx const Output_segment* copy = states_iter->second;
9 kx // Shallow copy to restore states.
9 kx *segment = *copy;
9 kx
9 kx // Also fix up TLS and RELRO segment pointers as appropriate.
9 kx if (segment->type() == elfcpp::PT_TLS)
9 kx this->tls_segment_ = segment;
9 kx else if (segment->type() == elfcpp::PT_GNU_RELRO)
9 kx this->relro_segment_ = segment;
9 kx
9 kx ++list_iter;
9 kx }
9 kx else
9 kx {
9 kx list_iter = this->segment_list_.erase(list_iter);
9 kx // This is a segment created during section layout. It should be
9 kx // safe to remove it since we should have removed all pointers to it.
9 kx delete segment;
9 kx }
9 kx }
9 kx }
9 kx
9 kx // Clean up after relaxation so that sections can be laid out again.
9 kx
9 kx void
9 kx Layout::clean_up_after_relaxation()
9 kx {
9 kx // Restore the segments to point state just prior to the relaxation loop.
9 kx Script_sections* script_section = this->script_options_->script_sections();
9 kx script_section->release_segments();
9 kx this->restore_segments(this->segment_states_);
9 kx
9 kx // Reset section addresses and file offsets
9 kx for (Section_list::iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx (*p)->restore_states();
9 kx
9 kx // If an input section changes size because of relaxation,
9 kx // we need to adjust the section offsets of all input sections.
9 kx // after such a section.
9 kx if ((*p)->section_offsets_need_adjustment())
9 kx (*p)->adjust_section_offsets();
9 kx
9 kx (*p)->reset_address_and_file_offset();
9 kx }
9 kx
9 kx // Reset special output object address and file offsets.
9 kx for (Data_list::iterator p = this->special_output_list_.begin();
9 kx p != this->special_output_list_.end();
9 kx ++p)
9 kx (*p)->reset_address_and_file_offset();
9 kx
9 kx // A linker script may have created some output section data objects.
9 kx // They are useless now.
9 kx for (Output_section_data_list::const_iterator p =
9 kx this->script_output_section_data_list_.begin();
9 kx p != this->script_output_section_data_list_.end();
9 kx ++p)
9 kx delete *p;
9 kx this->script_output_section_data_list_.clear();
9 kx
9 kx // Special-case fill output objects are recreated each time through
9 kx // the relaxation loop.
9 kx this->reset_relax_output();
9 kx }
9 kx
9 kx void
9 kx Layout::reset_relax_output()
9 kx {
9 kx for (Data_list::const_iterator p = this->relax_output_list_.begin();
9 kx p != this->relax_output_list_.end();
9 kx ++p)
9 kx delete *p;
9 kx this->relax_output_list_.clear();
9 kx }
9 kx
9 kx // Prepare for relaxation.
9 kx
9 kx void
9 kx Layout::prepare_for_relaxation()
9 kx {
9 kx // Create an relaxation debug check if in debugging mode.
9 kx if (is_debugging_enabled(DEBUG_RELAXATION))
9 kx this->relaxation_debug_check_ = new Relaxation_debug_check();
9 kx
9 kx // Save segment states.
9 kx this->segment_states_ = new Segment_states();
9 kx this->save_segments(this->segment_states_);
9 kx
9 kx for(Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx (*p)->save_states();
9 kx
9 kx if (is_debugging_enabled(DEBUG_RELAXATION))
9 kx this->relaxation_debug_check_->check_output_data_for_reset_values(
9 kx this->section_list_, this->special_output_list_,
9 kx this->relax_output_list_);
9 kx
9 kx // Also enable recording of output section data from scripts.
9 kx this->record_output_section_data_from_script_ = true;
9 kx }
9 kx
9 kx // If the user set the address of the text segment, that may not be
9 kx // compatible with putting the segment headers and file headers into
9 kx // that segment. For isolate_execinstr() targets, it's the rodata
9 kx // segment rather than text where we might put the headers.
9 kx static inline bool
9 kx load_seg_unusable_for_headers(const Target* target)
9 kx {
9 kx const General_options& options = parameters->options();
9 kx if (target->isolate_execinstr())
9 kx return (options.user_set_Trodata_segment()
9 kx && options.Trodata_segment() % target->abi_pagesize() != 0);
9 kx else
9 kx return (options.user_set_Ttext()
9 kx && options.Ttext() % target->abi_pagesize() != 0);
9 kx }
9 kx
9 kx // Relaxation loop body: If target has no relaxation, this runs only once
9 kx // Otherwise, the target relaxation hook is called at the end of
9 kx // each iteration. If the hook returns true, it means re-layout of
9 kx // section is required.
9 kx //
9 kx // The number of segments created by a linking script without a PHDRS
9 kx // clause may be affected by section sizes and alignments. There is
9 kx // a remote chance that relaxation causes different number of PT_LOAD
9 kx // segments are created and sections are attached to different segments.
9 kx // Therefore, we always throw away all segments created during section
9 kx // layout. In order to be able to restart the section layout, we keep
9 kx // a copy of the segment list right before the relaxation loop and use
9 kx // that to restore the segments.
9 kx //
9 kx // PASS is the current relaxation pass number.
9 kx // SYMTAB is a symbol table.
9 kx // PLOAD_SEG is the address of a pointer for the load segment.
9 kx // PHDR_SEG is a pointer to the PHDR segment.
9 kx // SEGMENT_HEADERS points to the output segment header.
9 kx // FILE_HEADER points to the output file header.
9 kx // PSHNDX is the address to store the output section index.
9 kx
9 kx off_t inline
9 kx Layout::relaxation_loop_body(
9 kx int pass,
9 kx Target* target,
9 kx Symbol_table* symtab,
9 kx Output_segment** pload_seg,
9 kx Output_segment* phdr_seg,
9 kx Output_segment_headers* segment_headers,
9 kx Output_file_header* file_header,
9 kx unsigned int* pshndx)
9 kx {
9 kx // If this is not the first iteration, we need to clean up after
9 kx // relaxation so that we can lay out the sections again.
9 kx if (pass != 0)
9 kx this->clean_up_after_relaxation();
9 kx
9 kx // If there is a SECTIONS clause, put all the input sections into
9 kx // the required order.
9 kx Output_segment* load_seg;
9 kx if (this->script_options_->saw_sections_clause())
9 kx load_seg = this->set_section_addresses_from_script(symtab);
9 kx else if (parameters->options().relocatable())
9 kx load_seg = NULL;
9 kx else
9 kx load_seg = this->find_first_load_seg(target);
9 kx
9 kx if (parameters->options().oformat_enum()
9 kx != General_options::OBJECT_FORMAT_ELF)
9 kx load_seg = NULL;
9 kx
9 kx if (load_seg_unusable_for_headers(target))
9 kx {
9 kx load_seg = NULL;
9 kx phdr_seg = NULL;
9 kx }
9 kx
9 kx gold_assert(phdr_seg == NULL
9 kx || load_seg != NULL
9 kx || this->script_options_->saw_sections_clause());
9 kx
9 kx // If the address of the load segment we found has been set by
9 kx // --section-start rather than by a script, then adjust the VMA and
9 kx // LMA downward if possible to include the file and section headers.
9 kx uint64_t header_gap = 0;
9 kx if (load_seg != NULL
9 kx && load_seg->are_addresses_set()
9 kx && !this->script_options_->saw_sections_clause()
9 kx && !parameters->options().relocatable())
9 kx {
9 kx file_header->finalize_data_size();
9 kx segment_headers->finalize_data_size();
9 kx size_t sizeof_headers = (file_header->data_size()
9 kx + segment_headers->data_size());
9 kx const uint64_t abi_pagesize = target->abi_pagesize();
9 kx uint64_t hdr_paddr = load_seg->paddr() - sizeof_headers;
9 kx hdr_paddr &= ~(abi_pagesize - 1);
9 kx uint64_t subtract = load_seg->paddr() - hdr_paddr;
9 kx if (load_seg->paddr() < subtract || load_seg->vaddr() < subtract)
9 kx load_seg = NULL;
9 kx else
9 kx {
9 kx load_seg->set_addresses(load_seg->vaddr() - subtract,
9 kx load_seg->paddr() - subtract);
9 kx header_gap = subtract - sizeof_headers;
9 kx }
9 kx }
9 kx
9 kx // Lay out the segment headers.
9 kx if (!parameters->options().relocatable())
9 kx {
9 kx gold_assert(segment_headers != NULL);
9 kx if (header_gap != 0 && load_seg != NULL)
9 kx {
9 kx Output_data_zero_fill* z = new Output_data_zero_fill(header_gap, 1);
9 kx load_seg->add_initial_output_data(z);
9 kx }
9 kx if (load_seg != NULL)
9 kx load_seg->add_initial_output_data(segment_headers);
9 kx if (phdr_seg != NULL)
9 kx phdr_seg->add_initial_output_data(segment_headers);
9 kx }
9 kx
9 kx // Lay out the file header.
9 kx if (load_seg != NULL)
9 kx load_seg->add_initial_output_data(file_header);
9 kx
9 kx if (this->script_options_->saw_phdrs_clause()
9 kx && !parameters->options().relocatable())
9 kx {
9 kx // Support use of FILEHDRS and PHDRS attachments in a PHDRS
9 kx // clause in a linker script.
9 kx Script_sections* ss = this->script_options_->script_sections();
9 kx ss->put_headers_in_phdrs(file_header, segment_headers);
9 kx }
9 kx
9 kx // We set the output section indexes in set_segment_offsets and
9 kx // set_section_indexes.
9 kx *pshndx = 1;
9 kx
9 kx // Set the file offsets of all the segments, and all the sections
9 kx // they contain.
9 kx off_t off;
9 kx if (!parameters->options().relocatable())
9 kx off = this->set_segment_offsets(target, load_seg, pshndx);
9 kx else
9 kx off = this->set_relocatable_section_offsets(file_header, pshndx);
9 kx
9 kx // Verify that the dummy relaxation does not change anything.
9 kx if (is_debugging_enabled(DEBUG_RELAXATION))
9 kx {
9 kx if (pass == 0)
9 kx this->relaxation_debug_check_->read_sections(this->section_list_);
9 kx else
9 kx this->relaxation_debug_check_->verify_sections(this->section_list_);
9 kx }
9 kx
9 kx *pload_seg = load_seg;
9 kx return off;
9 kx }
9 kx
9 kx // Search the list of patterns and find the position of the given section
9 kx // name in the output section. If the section name matches a glob
9 kx // pattern and a non-glob name, then the non-glob position takes
9 kx // precedence. Return 0 if no match is found.
9 kx
9 kx unsigned int
9 kx Layout::find_section_order_index(const std::string& section_name)
9 kx {
9 kx Unordered_map<std::string, unsigned int>::iterator map_it;
9 kx map_it = this->input_section_position_.find(section_name);
9 kx if (map_it != this->input_section_position_.end())
9 kx return map_it->second;
9 kx
9 kx // Absolute match failed. Linear search the glob patterns.
9 kx std::vector<std::string>::iterator it;
9 kx for (it = this->input_section_glob_.begin();
9 kx it != this->input_section_glob_.end();
9 kx ++it)
9 kx {
9 kx if (fnmatch((*it).c_str(), section_name.c_str(), FNM_NOESCAPE) == 0)
9 kx {
9 kx map_it = this->input_section_position_.find(*it);
9 kx gold_assert(map_it != this->input_section_position_.end());
9 kx return map_it->second;
9 kx }
9 kx }
9 kx return 0;
9 kx }
9 kx
9 kx // Read the sequence of input sections from the file specified with
9 kx // option --section-ordering-file.
9 kx
9 kx void
9 kx Layout::read_layout_from_file()
9 kx {
9 kx const char* filename = parameters->options().section_ordering_file();
9 kx std::ifstream in;
9 kx std::string line;
9 kx
9 kx in.open(filename);
9 kx if (!in)
9 kx gold_fatal(_("unable to open --section-ordering-file file %s: %s"),
9 kx filename, strerror(errno));
9 kx
9 kx File_read::record_file_read(filename);
9 kx
9 kx std::getline(in, line); // this chops off the trailing \n, if any
9 kx unsigned int position = 1;
9 kx this->set_section_ordering_specified();
9 kx
9 kx while (in)
9 kx {
9 kx if (!line.empty() && line[line.length() - 1] == '\r') // Windows
9 kx line.resize(line.length() - 1);
9 kx // Ignore comments, beginning with '#'
9 kx if (line[0] == '#')
9 kx {
9 kx std::getline(in, line);
9 kx continue;
9 kx }
9 kx this->input_section_position_[line] = position;
9 kx // Store all glob patterns in a vector.
9 kx if (is_wildcard_string(line.c_str()))
9 kx this->input_section_glob_.push_back(line);
9 kx position++;
9 kx std::getline(in, line);
9 kx }
9 kx }
9 kx
9 kx // Finalize the layout. When this is called, we have created all the
9 kx // output sections and all the output segments which are based on
9 kx // input sections. We have several things to do, and we have to do
9 kx // them in the right order, so that we get the right results correctly
9 kx // and efficiently.
9 kx
9 kx // 1) Finalize the list of output segments and create the segment
9 kx // table header.
9 kx
9 kx // 2) Finalize the dynamic symbol table and associated sections.
9 kx
9 kx // 3) Determine the final file offset of all the output segments.
9 kx
9 kx // 4) Determine the final file offset of all the SHF_ALLOC output
9 kx // sections.
9 kx
9 kx // 5) Create the symbol table sections and the section name table
9 kx // section.
9 kx
9 kx // 6) Finalize the symbol table: set symbol values to their final
9 kx // value and make a final determination of which symbols are going
9 kx // into the output symbol table.
9 kx
9 kx // 7) Create the section table header.
9 kx
9 kx // 8) Determine the final file offset of all the output sections which
9 kx // are not SHF_ALLOC, including the section table header.
9 kx
9 kx // 9) Finalize the ELF file header.
9 kx
9 kx // This function returns the size of the output file.
9 kx
9 kx off_t
9 kx Layout::finalize(const Input_objects* input_objects, Symbol_table* symtab,
9 kx Target* target, const Task* task)
9 kx {
9 kx unsigned int local_dynamic_count = 0;
9 kx unsigned int forced_local_dynamic_count = 0;
9 kx
9 kx target->finalize_sections(this, input_objects, symtab);
9 kx
9 kx this->count_local_symbols(task, input_objects);
9 kx
9 kx this->link_stabs_sections();
9 kx
9 kx Output_segment* phdr_seg = NULL;
9 kx if (!parameters->options().relocatable() && !parameters->doing_static_link())
9 kx {
9 kx // There was a dynamic object in the link. We need to create
9 kx // some information for the dynamic linker.
9 kx
9 kx // Create the PT_PHDR segment which will hold the program
9 kx // headers.
9 kx if (!this->script_options_->saw_phdrs_clause())
9 kx phdr_seg = this->make_output_segment(elfcpp::PT_PHDR, elfcpp::PF_R);
9 kx
9 kx // Create the dynamic symbol table, including the hash table.
9 kx Output_section* dynstr;
9 kx std::vector<Symbol*> dynamic_symbols;
9 kx Versions versions(*this->script_options()->version_script_info(),
9 kx &this->dynpool_);
9 kx this->create_dynamic_symtab(input_objects, symtab, &dynstr,
9 kx &local_dynamic_count,
9 kx &forced_local_dynamic_count,
9 kx &dynamic_symbols,
9 kx &versions);
9 kx
9 kx // Create the .interp section to hold the name of the
9 kx // interpreter, and put it in a PT_INTERP segment. Don't do it
9 kx // if we saw a .interp section in an input file.
9 kx if ((!parameters->options().shared()
9 kx || parameters->options().dynamic_linker() != NULL)
9 kx && this->interp_segment_ == NULL)
9 kx this->create_interp(target);
9 kx
9 kx // Finish the .dynamic section to hold the dynamic data, and put
9 kx // it in a PT_DYNAMIC segment.
9 kx this->finish_dynamic_section(input_objects, symtab);
9 kx
9 kx // We should have added everything we need to the dynamic string
9 kx // table.
9 kx this->dynpool_.set_string_offsets();
9 kx
9 kx // Create the version sections. We can't do this until the
9 kx // dynamic string table is complete.
9 kx this->create_version_sections(&versions, symtab,
9 kx (local_dynamic_count
9 kx + forced_local_dynamic_count),
9 kx dynamic_symbols, dynstr);
9 kx
9 kx // Set the size of the _DYNAMIC symbol. We can't do this until
9 kx // after we call create_version_sections.
9 kx this->set_dynamic_symbol_size(symtab);
9 kx }
9 kx
9 kx // Create segment headers.
9 kx Output_segment_headers* segment_headers =
9 kx (parameters->options().relocatable()
9 kx ? NULL
9 kx : new Output_segment_headers(this->segment_list_));
9 kx
9 kx // Lay out the file header.
9 kx Output_file_header* file_header = new Output_file_header(target, symtab,
9 kx segment_headers);
9 kx
9 kx this->special_output_list_.push_back(file_header);
9 kx if (segment_headers != NULL)
9 kx this->special_output_list_.push_back(segment_headers);
9 kx
9 kx // Find approriate places for orphan output sections if we are using
9 kx // a linker script.
9 kx if (this->script_options_->saw_sections_clause())
9 kx this->place_orphan_sections_in_script();
9 kx
9 kx Output_segment* load_seg;
9 kx off_t off;
9 kx unsigned int shndx;
9 kx int pass = 0;
9 kx
9 kx // Take a snapshot of the section layout as needed.
9 kx if (target->may_relax())
9 kx this->prepare_for_relaxation();
9 kx
9 kx // Run the relaxation loop to lay out sections.
9 kx do
9 kx {
9 kx off = this->relaxation_loop_body(pass, target, symtab, &load_seg,
9 kx phdr_seg, segment_headers, file_header,
9 kx &shndx);
9 kx pass++;
9 kx }
9 kx while (target->may_relax()
9 kx && target->relax(pass, input_objects, symtab, this, task));
9 kx
9 kx // If there is a load segment that contains the file and program headers,
9 kx // provide a symbol __ehdr_start pointing there.
9 kx // A program can use this to examine itself robustly.
9 kx Symbol *ehdr_start = symtab->lookup("__ehdr_start");
9 kx if (ehdr_start != NULL && ehdr_start->is_predefined())
9 kx {
9 kx if (load_seg != NULL)
9 kx ehdr_start->set_output_segment(load_seg, Symbol::SEGMENT_START);
9 kx else
9 kx ehdr_start->set_undefined();
9 kx }
9 kx
9 kx // Set the file offsets of all the non-data sections we've seen so
9 kx // far which don't have to wait for the input sections. We need
9 kx // this in order to finalize local symbols in non-allocated
9 kx // sections.
9 kx off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
9 kx
9 kx // Set the section indexes of all unallocated sections seen so far,
9 kx // in case any of them are somehow referenced by a symbol.
9 kx shndx = this->set_section_indexes(shndx);
9 kx
9 kx // Create the symbol table sections.
9 kx this->create_symtab_sections(input_objects, symtab, shndx, &off,
9 kx local_dynamic_count);
9 kx if (!parameters->doing_static_link())
9 kx this->assign_local_dynsym_offsets(input_objects);
9 kx
9 kx // Process any symbol assignments from a linker script. This must
9 kx // be called after the symbol table has been finalized.
9 kx this->script_options_->finalize_symbols(symtab, this);
9 kx
9 kx // Create the incremental inputs sections.
9 kx if (this->incremental_inputs_)
9 kx {
9 kx this->incremental_inputs_->finalize();
9 kx this->create_incremental_info_sections(symtab);
9 kx }
9 kx
9 kx // Create the .shstrtab section.
9 kx Output_section* shstrtab_section = this->create_shstrtab();
9 kx
9 kx // Set the file offsets of the rest of the non-data sections which
9 kx // don't have to wait for the input sections.
9 kx off = this->set_section_offsets(off, BEFORE_INPUT_SECTIONS_PASS);
9 kx
9 kx // Now that all sections have been created, set the section indexes
9 kx // for any sections which haven't been done yet.
9 kx shndx = this->set_section_indexes(shndx);
9 kx
9 kx // Create the section table header.
9 kx this->create_shdrs(shstrtab_section, &off);
9 kx
9 kx // If there are no sections which require postprocessing, we can
9 kx // handle the section names now, and avoid a resize later.
9 kx if (!this->any_postprocessing_sections_)
9 kx {
9 kx off = this->set_section_offsets(off,
9 kx POSTPROCESSING_SECTIONS_PASS);
9 kx off =
9 kx this->set_section_offsets(off,
9 kx STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
9 kx }
9 kx
9 kx file_header->set_section_info(this->section_headers_, shstrtab_section);
9 kx
9 kx // Now we know exactly where everything goes in the output file
9 kx // (except for non-allocated sections which require postprocessing).
9 kx Output_data::layout_complete();
9 kx
9 kx this->output_file_size_ = off;
9 kx
9 kx return off;
9 kx }
9 kx
9 kx // Create a note header following the format defined in the ELF ABI.
9 kx // NAME is the name, NOTE_TYPE is the type, SECTION_NAME is the name
9 kx // of the section to create, DESCSZ is the size of the descriptor.
9 kx // ALLOCATE is true if the section should be allocated in memory.
9 kx // This returns the new note section. It sets *TRAILING_PADDING to
9 kx // the number of trailing zero bytes required.
9 kx
9 kx Output_section*
9 kx Layout::create_note(const char* name, int note_type,
9 kx const char* section_name, size_t descsz,
9 kx bool allocate, size_t* trailing_padding)
9 kx {
9 kx // Authorities all agree that the values in a .note field should
9 kx // be aligned on 4-byte boundaries for 32-bit binaries. However,
9 kx // they differ on what the alignment is for 64-bit binaries.
9 kx // The GABI says unambiguously they take 8-byte alignment:
9 kx // http://sco.com/developers/gabi/latest/ch5.pheader.html#note_section
9 kx // Other documentation says alignment should always be 4 bytes:
9 kx // http://www.netbsd.org/docs/kernel/elf-notes.html#note-format
9 kx // GNU ld and GNU readelf both support the latter (at least as of
9 kx // version 2.16.91), and glibc always generates the latter for
9 kx // .note.ABI-tag (as of version 1.6), so that's the one we go with
9 kx // here.
9 kx #ifdef GABI_FORMAT_FOR_DOTNOTE_SECTION // This is not defined by default.
9 kx const int size = parameters->target().get_size();
9 kx #else
9 kx const int size = 32;
9 kx #endif
9 kx // The NT_GNU_PROPERTY_TYPE_0 note is aligned to the pointer size.
9 kx const int addralign = ((note_type == elfcpp::NT_GNU_PROPERTY_TYPE_0
9 kx ? parameters->target().get_size()
9 kx : size) / 8);
9 kx
9 kx // The contents of the .note section.
9 kx size_t namesz = strlen(name) + 1;
9 kx size_t aligned_namesz = align_address(namesz, size / 8);
9 kx size_t aligned_descsz = align_address(descsz, size / 8);
9 kx
9 kx size_t notehdrsz = 3 * (size / 8) + aligned_namesz;
9 kx
9 kx unsigned char* buffer = new unsigned char[notehdrsz];
9 kx memset(buffer, 0, notehdrsz);
9 kx
9 kx bool is_big_endian = parameters->target().is_big_endian();
9 kx
9 kx if (size == 32)
9 kx {
9 kx if (!is_big_endian)
9 kx {
9 kx elfcpp::Swap<32, false>::writeval(buffer, namesz);
9 kx elfcpp::Swap<32, false>::writeval(buffer + 4, descsz);
9 kx elfcpp::Swap<32, false>::writeval(buffer + 8, note_type);
9 kx }
9 kx else
9 kx {
9 kx elfcpp::Swap<32, true>::writeval(buffer, namesz);
9 kx elfcpp::Swap<32, true>::writeval(buffer + 4, descsz);
9 kx elfcpp::Swap<32, true>::writeval(buffer + 8, note_type);
9 kx }
9 kx }
9 kx else if (size == 64)
9 kx {
9 kx if (!is_big_endian)
9 kx {
9 kx elfcpp::Swap<64, false>::writeval(buffer, namesz);
9 kx elfcpp::Swap<64, false>::writeval(buffer + 8, descsz);
9 kx elfcpp::Swap<64, false>::writeval(buffer + 16, note_type);
9 kx }
9 kx else
9 kx {
9 kx elfcpp::Swap<64, true>::writeval(buffer, namesz);
9 kx elfcpp::Swap<64, true>::writeval(buffer + 8, descsz);
9 kx elfcpp::Swap<64, true>::writeval(buffer + 16, note_type);
9 kx }
9 kx }
9 kx else
9 kx gold_unreachable();
9 kx
9 kx memcpy(buffer + 3 * (size / 8), name, namesz);
9 kx
9 kx elfcpp::Elf_Xword flags = 0;
9 kx Output_section_order order = ORDER_INVALID;
9 kx if (allocate)
9 kx {
9 kx flags = elfcpp::SHF_ALLOC;
9 kx order = (note_type == elfcpp::NT_GNU_PROPERTY_TYPE_0
9 kx ? ORDER_PROPERTY_NOTE : ORDER_RO_NOTE);
9 kx }
9 kx Output_section* os = this->choose_output_section(NULL, section_name,
9 kx elfcpp::SHT_NOTE,
9 kx flags, false, order, false,
9 kx false, true);
9 kx if (os == NULL)
9 kx return NULL;
9 kx
9 kx Output_section_data* posd = new Output_data_const_buffer(buffer, notehdrsz,
9 kx addralign,
9 kx "** note header");
9 kx os->add_output_section_data(posd);
9 kx
9 kx *trailing_padding = aligned_descsz - descsz;
9 kx
9 kx return os;
9 kx }
9 kx
9 kx // Create a .note.gnu.property section to record program properties
9 kx // accumulated from the input files.
9 kx
9 kx void
9 kx Layout::create_gnu_properties_note()
9 kx {
9 kx parameters->target().finalize_gnu_properties(this);
9 kx
9 kx if (this->gnu_properties_.empty())
9 kx return;
9 kx
9 kx const unsigned int size = parameters->target().get_size();
9 kx const bool is_big_endian = parameters->target().is_big_endian();
9 kx
9 kx // Compute the total size of the properties array.
9 kx size_t descsz = 0;
9 kx for (Gnu_properties::const_iterator prop = this->gnu_properties_.begin();
9 kx prop != this->gnu_properties_.end();
9 kx ++prop)
9 kx {
9 kx descsz = align_address(descsz + 8 + prop->second.pr_datasz, size / 8);
9 kx }
9 kx
9 kx // Create the note section.
9 kx size_t trailing_padding;
9 kx Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_PROPERTY_TYPE_0,
9 kx ".note.gnu.property", descsz,
9 kx true, &trailing_padding);
9 kx if (os == NULL)
9 kx return;
9 kx gold_assert(trailing_padding == 0);
9 kx
9 kx // Allocate and fill the properties array.
9 kx unsigned char* desc = new unsigned char[descsz];
9 kx unsigned char* p = desc;
9 kx for (Gnu_properties::const_iterator prop = this->gnu_properties_.begin();
9 kx prop != this->gnu_properties_.end();
9 kx ++prop)
9 kx {
9 kx size_t datasz = prop->second.pr_datasz;
9 kx size_t aligned_datasz = align_address(prop->second.pr_datasz, size / 8);
9 kx write_sized_value(prop->first, 4, p, is_big_endian);
9 kx write_sized_value(datasz, 4, p + 4, is_big_endian);
9 kx memcpy(p + 8, prop->second.pr_data, datasz);
9 kx if (aligned_datasz > datasz)
9 kx memset(p + 8 + datasz, 0, aligned_datasz - datasz);
9 kx p += 8 + aligned_datasz;
9 kx }
9 kx Output_section_data* posd = new Output_data_const(desc, descsz, 4);
9 kx os->add_output_section_data(posd);
9 kx }
9 kx
9 kx // For an executable or shared library, create a note to record the
9 kx // version of gold used to create the binary.
9 kx
9 kx void
9 kx Layout::create_gold_note()
9 kx {
9 kx if (parameters->options().relocatable()
9 kx || parameters->incremental_update())
9 kx return;
9 kx
9 kx std::string desc = std::string("gold ") + gold::get_version_string();
9 kx
9 kx size_t trailing_padding;
9 kx Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_GOLD_VERSION,
9 kx ".note.gnu.gold-version", desc.size(),
9 kx false, &trailing_padding);
9 kx if (os == NULL)
9 kx return;
9 kx
9 kx Output_section_data* posd = new Output_data_const(desc, 4);
9 kx os->add_output_section_data(posd);
9 kx
9 kx if (trailing_padding > 0)
9 kx {
9 kx posd = new Output_data_zero_fill(trailing_padding, 0);
9 kx os->add_output_section_data(posd);
9 kx }
9 kx }
9 kx
9 kx // Record whether the stack should be executable. This can be set
9 kx // from the command line using the -z execstack or -z noexecstack
9 kx // options. Otherwise, if any input file has a .note.GNU-stack
9 kx // section with the SHF_EXECINSTR flag set, the stack should be
9 kx // executable. Otherwise, if at least one input file a
9 kx // .note.GNU-stack section, and some input file has no .note.GNU-stack
9 kx // section, we use the target default for whether the stack should be
9 kx // executable. If -z stack-size was used to set a p_memsz value for
9 kx // PT_GNU_STACK, we generate the segment regardless. Otherwise, we
9 kx // don't generate a stack note. When generating a object file, we
9 kx // create a .note.GNU-stack section with the appropriate marking.
9 kx // When generating an executable or shared library, we create a
9 kx // PT_GNU_STACK segment.
9 kx
9 kx void
9 kx Layout::create_stack_segment()
9 kx {
9 kx bool is_stack_executable;
9 kx if (parameters->options().is_execstack_set())
9 kx {
9 kx is_stack_executable = parameters->options().is_stack_executable();
9 kx if (!is_stack_executable
9 kx && this->input_requires_executable_stack_
9 kx && parameters->options().warn_execstack())
9 kx gold_warning(_("one or more inputs require executable stack, "
9 kx "but -z noexecstack was given"));
9 kx }
9 kx else if (!this->input_with_gnu_stack_note_
9 kx && (!parameters->options().user_set_stack_size()
9 kx || parameters->options().relocatable()))
9 kx return;
9 kx else
9 kx {
9 kx if (this->input_requires_executable_stack_)
9 kx is_stack_executable = true;
9 kx else if (this->input_without_gnu_stack_note_)
9 kx is_stack_executable =
9 kx parameters->target().is_default_stack_executable();
9 kx else
9 kx is_stack_executable = false;
9 kx }
9 kx
9 kx if (parameters->options().relocatable())
9 kx {
9 kx const char* name = this->namepool_.add(".note.GNU-stack", false, NULL);
9 kx elfcpp::Elf_Xword flags = 0;
9 kx if (is_stack_executable)
9 kx flags |= elfcpp::SHF_EXECINSTR;
9 kx this->make_output_section(name, elfcpp::SHT_PROGBITS, flags,
9 kx ORDER_INVALID, false);
9 kx }
9 kx else
9 kx {
9 kx if (this->script_options_->saw_phdrs_clause())
9 kx return;
9 kx int flags = elfcpp::PF_R | elfcpp::PF_W;
9 kx if (is_stack_executable)
9 kx flags |= elfcpp::PF_X;
9 kx Output_segment* seg =
9 kx this->make_output_segment(elfcpp::PT_GNU_STACK, flags);
9 kx seg->set_size(parameters->options().stack_size());
9 kx // BFD lets targets override this default alignment, but the only
9 kx // targets that do so are ones that Gold does not support so far.
9 kx seg->set_minimum_p_align(16);
9 kx }
9 kx }
9 kx
9 kx // If --build-id was used, set up the build ID note.
9 kx
9 kx void
9 kx Layout::create_build_id()
9 kx {
9 kx if (!parameters->options().user_set_build_id())
9 kx return;
9 kx
9 kx const char* style = parameters->options().build_id();
9 kx if (strcmp(style, "none") == 0)
9 kx return;
9 kx
9 kx // Set DESCSZ to the size of the note descriptor. When possible,
9 kx // set DESC to the note descriptor contents.
9 kx size_t descsz;
9 kx std::string desc;
9 kx if (strcmp(style, "md5") == 0)
9 kx descsz = 128 / 8;
9 kx else if ((strcmp(style, "sha1") == 0) || (strcmp(style, "tree") == 0))
9 kx descsz = 160 / 8;
9 kx else if (strcmp(style, "uuid") == 0)
9 kx {
9 kx #ifndef __MINGW32__
9 kx const size_t uuidsz = 128 / 8;
9 kx
9 kx char buffer[uuidsz];
9 kx memset(buffer, 0, uuidsz);
9 kx
9 kx int descriptor = open_descriptor(-1, "/dev/urandom", O_RDONLY);
9 kx if (descriptor < 0)
9 kx gold_error(_("--build-id=uuid failed: could not open /dev/urandom: %s"),
9 kx strerror(errno));
9 kx else
9 kx {
9 kx ssize_t got = ::read(descriptor, buffer, uuidsz);
9 kx release_descriptor(descriptor, true);
9 kx if (got < 0)
9 kx gold_error(_("/dev/urandom: read failed: %s"), strerror(errno));
9 kx else if (static_cast<size_t>(got) != uuidsz)
9 kx gold_error(_("/dev/urandom: expected %zu bytes, got %zd bytes"),
9 kx uuidsz, got);
9 kx }
9 kx
9 kx desc.assign(buffer, uuidsz);
9 kx descsz = uuidsz;
9 kx #else // __MINGW32__
9 kx UUID uuid;
9 kx typedef RPC_STATUS (RPC_ENTRY *UuidCreateFn)(UUID *Uuid);
9 kx
9 kx HMODULE rpc_library = LoadLibrary("rpcrt4.dll");
9 kx if (!rpc_library)
9 kx gold_error(_("--build-id=uuid failed: could not load rpcrt4.dll"));
9 kx else
9 kx {
9 kx UuidCreateFn uuid_create = reinterpret_cast<UuidCreateFn>(
9 kx GetProcAddress(rpc_library, "UuidCreate"));
9 kx if (!uuid_create)
9 kx gold_error(_("--build-id=uuid failed: could not find UuidCreate"));
9 kx else if (uuid_create(&uuid) != RPC_S_OK)
9 kx gold_error(_("__build_id=uuid failed: call UuidCreate() failed"));
9 kx FreeLibrary(rpc_library);
9 kx }
9 kx desc.assign(reinterpret_cast<const char *>(&uuid), sizeof(UUID));
9 kx descsz = sizeof(UUID);
9 kx #endif // __MINGW32__
9 kx }
9 kx else if (strncmp(style, "0x", 2) == 0)
9 kx {
9 kx hex_init();
9 kx const char* p = style + 2;
9 kx while (*p != '\0')
9 kx {
9 kx if (hex_p(p[0]) && hex_p(p[1]))
9 kx {
9 kx char c = (hex_value(p[0]) << 4) | hex_value(p[1]);
9 kx desc += c;
9 kx p += 2;
9 kx }
9 kx else if (*p == '-' || *p == ':')
9 kx ++p;
9 kx else
9 kx gold_fatal(_("--build-id argument '%s' not a valid hex number"),
9 kx style);
9 kx }
9 kx descsz = desc.size();
9 kx }
9 kx else
9 kx gold_fatal(_("unrecognized --build-id argument '%s'"), style);
9 kx
9 kx // Create the note.
9 kx size_t trailing_padding;
9 kx Output_section* os = this->create_note("GNU", elfcpp::NT_GNU_BUILD_ID,
9 kx ".note.gnu.build-id", descsz, true,
9 kx &trailing_padding);
9 kx if (os == NULL)
9 kx return;
9 kx
9 kx if (!desc.empty())
9 kx {
9 kx // We know the value already, so we fill it in now.
9 kx gold_assert(desc.size() == descsz);
9 kx
9 kx Output_section_data* posd = new Output_data_const(desc, 4);
9 kx os->add_output_section_data(posd);
9 kx
9 kx if (trailing_padding != 0)
9 kx {
9 kx posd = new Output_data_zero_fill(trailing_padding, 0);
9 kx os->add_output_section_data(posd);
9 kx }
9 kx }
9 kx else
9 kx {
9 kx // We need to compute a checksum after we have completed the
9 kx // link.
9 kx gold_assert(trailing_padding == 0);
9 kx this->build_id_note_ = new Output_data_zero_fill(descsz, 4);
9 kx os->add_output_section_data(this->build_id_note_);
9 kx }
9 kx }
9 kx
9 kx // If --package-metadata was used, set up the package metadata note.
9 kx // https://systemd.io/ELF_PACKAGE_METADATA/
9 kx
9 kx void
9 kx Layout::create_package_metadata()
9 kx {
9 kx if (!parameters->options().user_set_package_metadata())
9 kx return;
9 kx
9 kx const char* desc = parameters->options().package_metadata();
9 kx if (strcmp(desc, "") == 0)
9 kx return;
9 kx
9 kx #ifdef HAVE_JANSSON
9 kx json_error_t json_error;
9 kx json_t *json = json_loads(desc, 0, &json_error);
9 kx if (json)
9 kx json_decref(json);
9 kx else
9 kx {
9 kx gold_fatal(_("error: --package-metadata=%s does not contain valid "
9 kx "JSON: %s\n"),
9 kx desc, json_error.text);
9 kx }
9 kx #endif
9 kx
9 kx // Create the note.
9 kx size_t trailing_padding;
9 kx // Ensure the trailing NULL byte is always included, as per specification.
9 kx size_t descsz = strlen(desc) + 1;
9 kx Output_section* os = this->create_note("FDO", elfcpp::FDO_PACKAGING_METADATA,
9 kx ".note.package", descsz, true,
9 kx &trailing_padding);
9 kx if (os == NULL)
9 kx return;
9 kx
9 kx Output_section_data* posd = new Output_data_const(desc, descsz, 4);
9 kx os->add_output_section_data(posd);
9 kx
9 kx if (trailing_padding != 0)
9 kx {
9 kx posd = new Output_data_zero_fill(trailing_padding, 0);
9 kx os->add_output_section_data(posd);
9 kx }
9 kx }
9 kx
9 kx // If we have both .stabXX and .stabXXstr sections, then the sh_link
9 kx // field of the former should point to the latter. I'm not sure who
9 kx // started this, but the GNU linker does it, and some tools depend
9 kx // upon it.
9 kx
9 kx void
9 kx Layout::link_stabs_sections()
9 kx {
9 kx if (!this->have_stabstr_section_)
9 kx return;
9 kx
9 kx for (Section_list::iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->type() != elfcpp::SHT_STRTAB)
9 kx continue;
9 kx
9 kx const char* name = (*p)->name();
9 kx if (strncmp(name, ".stab", 5) != 0)
9 kx continue;
9 kx
9 kx size_t len = strlen(name);
9 kx if (strcmp(name + len - 3, "str") != 0)
9 kx continue;
9 kx
9 kx std::string stab_name(name, len - 3);
9 kx Output_section* stab_sec;
9 kx stab_sec = this->find_output_section(stab_name.c_str());
9 kx if (stab_sec != NULL)
9 kx stab_sec->set_link_section(*p);
9 kx }
9 kx }
9 kx
9 kx // Create .gnu_incremental_inputs and related sections needed
9 kx // for the next run of incremental linking to check what has changed.
9 kx
9 kx void
9 kx Layout::create_incremental_info_sections(Symbol_table* symtab)
9 kx {
9 kx Incremental_inputs* incr = this->incremental_inputs_;
9 kx
9 kx gold_assert(incr != NULL);
9 kx
9 kx // Create the .gnu_incremental_inputs, _symtab, and _relocs input sections.
9 kx incr->create_data_sections(symtab);
9 kx
9 kx // Add the .gnu_incremental_inputs section.
9 kx const char* incremental_inputs_name =
9 kx this->namepool_.add(".gnu_incremental_inputs", false, NULL);
9 kx Output_section* incremental_inputs_os =
9 kx this->make_output_section(incremental_inputs_name,
9 kx elfcpp::SHT_GNU_INCREMENTAL_INPUTS, 0,
9 kx ORDER_INVALID, false);
9 kx incremental_inputs_os->add_output_section_data(incr->inputs_section());
9 kx
9 kx // Add the .gnu_incremental_symtab section.
9 kx const char* incremental_symtab_name =
9 kx this->namepool_.add(".gnu_incremental_symtab", false, NULL);
9 kx Output_section* incremental_symtab_os =
9 kx this->make_output_section(incremental_symtab_name,
9 kx elfcpp::SHT_GNU_INCREMENTAL_SYMTAB, 0,
9 kx ORDER_INVALID, false);
9 kx incremental_symtab_os->add_output_section_data(incr->symtab_section());
9 kx incremental_symtab_os->set_entsize(4);
9 kx
9 kx // Add the .gnu_incremental_relocs section.
9 kx const char* incremental_relocs_name =
9 kx this->namepool_.add(".gnu_incremental_relocs", false, NULL);
9 kx Output_section* incremental_relocs_os =
9 kx this->make_output_section(incremental_relocs_name,
9 kx elfcpp::SHT_GNU_INCREMENTAL_RELOCS, 0,
9 kx ORDER_INVALID, false);
9 kx incremental_relocs_os->add_output_section_data(incr->relocs_section());
9 kx incremental_relocs_os->set_entsize(incr->relocs_entsize());
9 kx
9 kx // Add the .gnu_incremental_got_plt section.
9 kx const char* incremental_got_plt_name =
9 kx this->namepool_.add(".gnu_incremental_got_plt", false, NULL);
9 kx Output_section* incremental_got_plt_os =
9 kx this->make_output_section(incremental_got_plt_name,
9 kx elfcpp::SHT_GNU_INCREMENTAL_GOT_PLT, 0,
9 kx ORDER_INVALID, false);
9 kx incremental_got_plt_os->add_output_section_data(incr->got_plt_section());
9 kx
9 kx // Add the .gnu_incremental_strtab section.
9 kx const char* incremental_strtab_name =
9 kx this->namepool_.add(".gnu_incremental_strtab", false, NULL);
9 kx Output_section* incremental_strtab_os = this->make_output_section(incremental_strtab_name,
9 kx elfcpp::SHT_STRTAB, 0,
9 kx ORDER_INVALID, false);
9 kx Output_data_strtab* strtab_data =
9 kx new Output_data_strtab(incr->get_stringpool());
9 kx incremental_strtab_os->add_output_section_data(strtab_data);
9 kx
9 kx incremental_inputs_os->set_after_input_sections();
9 kx incremental_symtab_os->set_after_input_sections();
9 kx incremental_relocs_os->set_after_input_sections();
9 kx incremental_got_plt_os->set_after_input_sections();
9 kx
9 kx incremental_inputs_os->set_link_section(incremental_strtab_os);
9 kx incremental_symtab_os->set_link_section(incremental_inputs_os);
9 kx incremental_relocs_os->set_link_section(incremental_inputs_os);
9 kx incremental_got_plt_os->set_link_section(incremental_inputs_os);
9 kx }
9 kx
9 kx // Return whether SEG1 should be before SEG2 in the output file. This
9 kx // is based entirely on the segment type and flags. When this is
9 kx // called the segment addresses have normally not yet been set.
9 kx
9 kx bool
9 kx Layout::segment_precedes(const Output_segment* seg1,
9 kx const Output_segment* seg2)
9 kx {
9 kx // In order to produce a stable ordering if we're called with the same pointer
9 kx // return false.
9 kx if (seg1 == seg2)
9 kx return false;
9 kx
9 kx elfcpp::Elf_Word type1 = seg1->type();
9 kx elfcpp::Elf_Word type2 = seg2->type();
9 kx
9 kx // The single PT_PHDR segment is required to precede any loadable
9 kx // segment. We simply make it always first.
9 kx if (type1 == elfcpp::PT_PHDR)
9 kx {
9 kx gold_assert(type2 != elfcpp::PT_PHDR);
9 kx return true;
9 kx }
9 kx if (type2 == elfcpp::PT_PHDR)
9 kx return false;
9 kx
9 kx // The single PT_INTERP segment is required to precede any loadable
9 kx // segment. We simply make it always second.
9 kx if (type1 == elfcpp::PT_INTERP)
9 kx {
9 kx gold_assert(type2 != elfcpp::PT_INTERP);
9 kx return true;
9 kx }
9 kx if (type2 == elfcpp::PT_INTERP)
9 kx return false;
9 kx
9 kx // We then put PT_LOAD segments before any other segments.
9 kx if (type1 == elfcpp::PT_LOAD && type2 != elfcpp::PT_LOAD)
9 kx return true;
9 kx if (type2 == elfcpp::PT_LOAD && type1 != elfcpp::PT_LOAD)
9 kx return false;
9 kx
9 kx // We put the PT_TLS segment last except for the PT_GNU_RELRO
9 kx // segment, because that is where the dynamic linker expects to find
9 kx // it (this is just for efficiency; other positions would also work
9 kx // correctly).
9 kx if (type1 == elfcpp::PT_TLS
9 kx && type2 != elfcpp::PT_TLS
9 kx && type2 != elfcpp::PT_GNU_RELRO)
9 kx return false;
9 kx if (type2 == elfcpp::PT_TLS
9 kx && type1 != elfcpp::PT_TLS
9 kx && type1 != elfcpp::PT_GNU_RELRO)
9 kx return true;
9 kx
9 kx // We put the PT_GNU_RELRO segment last, because that is where the
9 kx // dynamic linker expects to find it (as with PT_TLS, this is just
9 kx // for efficiency).
9 kx if (type1 == elfcpp::PT_GNU_RELRO && type2 != elfcpp::PT_GNU_RELRO)
9 kx return false;
9 kx if (type2 == elfcpp::PT_GNU_RELRO && type1 != elfcpp::PT_GNU_RELRO)
9 kx return true;
9 kx
9 kx const elfcpp::Elf_Word flags1 = seg1->flags();
9 kx const elfcpp::Elf_Word flags2 = seg2->flags();
9 kx
9 kx // The order of non-PT_LOAD segments is unimportant. We simply sort
9 kx // by the numeric segment type and flags values. There should not
9 kx // be more than one segment with the same type and flags, except
9 kx // when a linker script specifies such.
9 kx if (type1 != elfcpp::PT_LOAD)
9 kx {
9 kx if (type1 != type2)
9 kx return type1 < type2;
9 kx uint64_t align1 = seg1->align();
9 kx uint64_t align2 = seg2->align();
9 kx // Place segments with larger alignments first.
9 kx if (align1 != align2)
9 kx return align1 > align2;
9 kx gold_assert(flags1 != flags2
9 kx || this->script_options_->saw_phdrs_clause());
9 kx return flags1 < flags2;
9 kx }
9 kx
9 kx // If the addresses are set already, sort by load address.
9 kx if (seg1->are_addresses_set())
9 kx {
9 kx if (!seg2->are_addresses_set())
9 kx return true;
9 kx
9 kx unsigned int section_count1 = seg1->output_section_count();
9 kx unsigned int section_count2 = seg2->output_section_count();
9 kx if (section_count1 == 0 && section_count2 > 0)
9 kx return true;
9 kx if (section_count1 > 0 && section_count2 == 0)
9 kx return false;
9 kx
9 kx uint64_t paddr1 = (seg1->are_addresses_set()
9 kx ? seg1->paddr()
9 kx : seg1->first_section_load_address());
9 kx uint64_t paddr2 = (seg2->are_addresses_set()
9 kx ? seg2->paddr()
9 kx : seg2->first_section_load_address());
9 kx
9 kx if (paddr1 != paddr2)
9 kx return paddr1 < paddr2;
9 kx }
9 kx else if (seg2->are_addresses_set())
9 kx return false;
9 kx
9 kx // A segment which holds large data comes after a segment which does
9 kx // not hold large data.
9 kx if (seg1->is_large_data_segment())
9 kx {
9 kx if (!seg2->is_large_data_segment())
9 kx return false;
9 kx }
9 kx else if (seg2->is_large_data_segment())
9 kx return true;
9 kx
9 kx // Otherwise, we sort PT_LOAD segments based on the flags. Readonly
9 kx // segments come before writable segments. Then writable segments
9 kx // with data come before writable segments without data. Then
9 kx // executable segments come before non-executable segments. Then
9 kx // the unlikely case of a non-readable segment comes before the
9 kx // normal case of a readable segment. If there are multiple
9 kx // segments with the same type and flags, we require that the
9 kx // address be set, and we sort by virtual address and then physical
9 kx // address.
9 kx if ((flags1 & elfcpp::PF_W) != (flags2 & elfcpp::PF_W))
9 kx return (flags1 & elfcpp::PF_W) == 0;
9 kx if ((flags1 & elfcpp::PF_W) != 0
9 kx && seg1->has_any_data_sections() != seg2->has_any_data_sections())
9 kx return seg1->has_any_data_sections();
9 kx if ((flags1 & elfcpp::PF_X) != (flags2 & elfcpp::PF_X))
9 kx return (flags1 & elfcpp::PF_X) != 0;
9 kx if ((flags1 & elfcpp::PF_R) != (flags2 & elfcpp::PF_R))
9 kx return (flags1 & elfcpp::PF_R) == 0;
9 kx
9 kx // We shouldn't get here--we shouldn't create segments which we
9 kx // can't distinguish. Unless of course we are using a weird linker
9 kx // script or overlapping --section-start options. We could also get
9 kx // here if plugins want unique segments for subsets of sections.
9 kx gold_assert(this->script_options_->saw_phdrs_clause()
9 kx || parameters->options().any_section_start()
9 kx || this->is_unique_segment_for_sections_specified()
9 kx || parameters->options().text_unlikely_segment());
9 kx return false;
9 kx }
9 kx
9 kx // Increase OFF so that it is congruent to ADDR modulo ABI_PAGESIZE.
9 kx
9 kx static off_t
9 kx align_file_offset(off_t off, uint64_t addr, uint64_t abi_pagesize)
9 kx {
9 kx uint64_t unsigned_off = off;
9 kx uint64_t aligned_off = ((unsigned_off & ~(abi_pagesize - 1))
9 kx | (addr & (abi_pagesize - 1)));
9 kx if (aligned_off < unsigned_off)
9 kx aligned_off += abi_pagesize;
9 kx return aligned_off;
9 kx }
9 kx
9 kx // On targets where the text segment contains only executable code,
9 kx // a non-executable segment is never the text segment.
9 kx
9 kx static inline bool
9 kx is_text_segment(const Target* target, const Output_segment* seg)
9 kx {
9 kx elfcpp::Elf_Xword flags = seg->flags();
9 kx if ((flags & elfcpp::PF_W) != 0)
9 kx return false;
9 kx if ((flags & elfcpp::PF_X) == 0)
9 kx return !target->isolate_execinstr();
9 kx return true;
9 kx }
9 kx
9 kx // Set the file offsets of all the segments, and all the sections they
9 kx // contain. They have all been created. LOAD_SEG must be laid out
9 kx // first. Return the offset of the data to follow.
9 kx
9 kx off_t
9 kx Layout::set_segment_offsets(const Target* target, Output_segment* load_seg,
9 kx unsigned int* pshndx)
9 kx {
9 kx // Sort them into the final order. We use a stable sort so that we
9 kx // don't randomize the order of indistinguishable segments created
9 kx // by linker scripts.
9 kx std::stable_sort(this->segment_list_.begin(), this->segment_list_.end(),
9 kx Layout::Compare_segments(this));
9 kx
9 kx // Find the PT_LOAD segments, and set their addresses and offsets
9 kx // and their section's addresses and offsets.
9 kx uint64_t start_addr;
9 kx if (parameters->options().user_set_Ttext())
9 kx start_addr = parameters->options().Ttext();
9 kx else if (parameters->options().output_is_position_independent())
9 kx start_addr = 0;
9 kx else
9 kx start_addr = target->default_text_segment_address();
9 kx
9 kx uint64_t addr = start_addr;
9 kx off_t off = 0;
9 kx
9 kx // If LOAD_SEG is NULL, then the file header and segment headers
9 kx // will not be loadable. But they still need to be at offset 0 in
9 kx // the file. Set their offsets now.
9 kx if (load_seg == NULL)
9 kx {
9 kx for (Data_list::iterator p = this->special_output_list_.begin();
9 kx p != this->special_output_list_.end();
9 kx ++p)
9 kx {
9 kx off = align_address(off, (*p)->addralign());
9 kx (*p)->set_address_and_file_offset(0, off);
9 kx off += (*p)->data_size();
9 kx }
9 kx }
9 kx
9 kx unsigned int increase_relro = this->increase_relro_;
9 kx if (this->script_options_->saw_sections_clause())
9 kx increase_relro = 0;
9 kx
9 kx const bool check_sections = parameters->options().check_sections();
9 kx Output_segment* last_load_segment = NULL;
9 kx
9 kx unsigned int shndx_begin = *pshndx;
9 kx unsigned int shndx_load_seg = *pshndx;
9 kx
9 kx for (Segment_list::iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->type() == elfcpp::PT_LOAD)
9 kx {
9 kx if (target->isolate_execinstr())
9 kx {
9 kx // When we hit the segment that should contain the
9 kx // file headers, reset the file offset so we place
9 kx // it and subsequent segments appropriately.
9 kx // We'll fix up the preceding segments below.
9 kx if (load_seg == *p)
9 kx {
9 kx if (off == 0)
9 kx load_seg = NULL;
9 kx else
9 kx {
9 kx off = 0;
9 kx shndx_load_seg = *pshndx;
9 kx }
9 kx }
9 kx }
9 kx else
9 kx {
9 kx // Verify that the file headers fall into the first segment.
9 kx if (load_seg != NULL && load_seg != *p)
9 kx gold_unreachable();
9 kx load_seg = NULL;
9 kx }
9 kx
9 kx bool are_addresses_set = (*p)->are_addresses_set();
9 kx if (are_addresses_set)
9 kx {
9 kx // When it comes to setting file offsets, we care about
9 kx // the physical address.
9 kx addr = (*p)->paddr();
9 kx }
9 kx else if (parameters->options().user_set_Ttext()
9 kx && (parameters->options().omagic()
9 kx || is_text_segment(target, *p)))
9 kx {
9 kx are_addresses_set = true;
9 kx }
9 kx else if (parameters->options().user_set_Trodata_segment()
9 kx && ((*p)->flags() & (elfcpp::PF_W | elfcpp::PF_X)) == 0)
9 kx {
9 kx addr = parameters->options().Trodata_segment();
9 kx are_addresses_set = true;
9 kx }
9 kx else if (parameters->options().user_set_Tdata()
9 kx && ((*p)->flags() & elfcpp::PF_W) != 0
9 kx && (!parameters->options().user_set_Tbss()
9 kx || (*p)->has_any_data_sections()))
9 kx {
9 kx addr = parameters->options().Tdata();
9 kx are_addresses_set = true;
9 kx }
9 kx else if (parameters->options().user_set_Tbss()
9 kx && ((*p)->flags() & elfcpp::PF_W) != 0
9 kx && !(*p)->has_any_data_sections())
9 kx {
9 kx addr = parameters->options().Tbss();
9 kx are_addresses_set = true;
9 kx }
9 kx
9 kx uint64_t orig_addr = addr;
9 kx uint64_t orig_off = off;
9 kx
9 kx uint64_t aligned_addr = 0;
9 kx uint64_t abi_pagesize = target->abi_pagesize();
9 kx uint64_t common_pagesize = target->common_pagesize();
9 kx
9 kx if (!parameters->options().nmagic()
9 kx && !parameters->options().omagic())
9 kx (*p)->set_minimum_p_align(abi_pagesize);
9 kx
9 kx if (!are_addresses_set)
9 kx {
9 kx // Skip the address forward one page, maintaining the same
9 kx // position within the page. This lets us store both segments
9 kx // overlapping on a single page in the file, but the loader will
9 kx // put them on different pages in memory. We will revisit this
9 kx // decision once we know the size of the segment.
9 kx
9 kx uint64_t max_align = (*p)->maximum_alignment();
9 kx if (max_align > abi_pagesize)
9 kx addr = align_address(addr, max_align);
9 kx aligned_addr = addr;
9 kx
9 kx if (load_seg == *p)
9 kx {
9 kx // This is the segment that will contain the file
9 kx // headers, so its offset will have to be exactly zero.
9 kx gold_assert(orig_off == 0);
9 kx
9 kx // If the target wants a fixed minimum distance from the
9 kx // text segment to the read-only segment, move up now.
9 kx uint64_t min_addr =
9 kx start_addr + (parameters->options().user_set_rosegment_gap()
9 kx ? parameters->options().rosegment_gap()
9 kx : target->rosegment_gap());
9 kx if (addr < min_addr)
9 kx addr = min_addr;
9 kx
9 kx // But this is not the first segment! To make its
9 kx // address congruent with its offset, that address better
9 kx // be aligned to the ABI-mandated page size.
9 kx addr = align_address(addr, abi_pagesize);
9 kx aligned_addr = addr;
9 kx }
9 kx else
9 kx {
9 kx if ((addr & (abi_pagesize - 1)) != 0)
9 kx addr = addr + abi_pagesize;
9 kx
9 kx off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
9 kx }
9 kx }
9 kx
9 kx if (!parameters->options().nmagic()
9 kx && !parameters->options().omagic())
9 kx {
9 kx // Here we are also taking care of the case when
9 kx // the maximum segment alignment is larger than the page size.
9 kx off = align_file_offset(off, addr,
9 kx std::max(abi_pagesize,
9 kx (*p)->maximum_alignment()));
9 kx }
9 kx else
9 kx {
9 kx // This is -N or -n with a section script which prevents
9 kx // us from using a load segment. We need to ensure that
9 kx // the file offset is aligned to the alignment of the
9 kx // segment. This is because the linker script
9 kx // implicitly assumed a zero offset. If we don't align
9 kx // here, then the alignment of the sections in the
9 kx // linker script may not match the alignment of the
9 kx // sections in the set_section_addresses call below,
9 kx // causing an error about dot moving backward.
9 kx off = align_address(off, (*p)->maximum_alignment());
9 kx }
9 kx
9 kx unsigned int shndx_hold = *pshndx;
9 kx bool has_relro = false;
9 kx uint64_t new_addr = (*p)->set_section_addresses(target, this,
9 kx false, addr,
9 kx &increase_relro,
9 kx &has_relro,
9 kx &off, pshndx);
9 kx
9 kx // Now that we know the size of this segment, we may be able
9 kx // to save a page in memory, at the cost of wasting some
9 kx // file space, by instead aligning to the start of a new
9 kx // page. Here we use the real machine page size rather than
9 kx // the ABI mandated page size. If the segment has been
9 kx // aligned so that the relro data ends at a page boundary,
9 kx // we do not try to realign it.
9 kx
9 kx if (!are_addresses_set
9 kx && !has_relro
9 kx && aligned_addr != addr
9 kx && !parameters->incremental())
9 kx {
9 kx uint64_t first_off = (common_pagesize
9 kx - (aligned_addr
9 kx & (common_pagesize - 1)));
9 kx uint64_t last_off = new_addr & (common_pagesize - 1);
9 kx if (first_off > 0
9 kx && last_off > 0
9 kx && ((aligned_addr & ~ (common_pagesize - 1))
9 kx != (new_addr & ~ (common_pagesize - 1)))
9 kx && first_off + last_off <= common_pagesize)
9 kx {
9 kx *pshndx = shndx_hold;
9 kx addr = align_address(aligned_addr, common_pagesize);
9 kx addr = align_address(addr, (*p)->maximum_alignment());
9 kx if ((addr & (abi_pagesize - 1)) != 0)
9 kx addr = addr + abi_pagesize;
9 kx off = orig_off + ((addr - orig_addr) & (abi_pagesize - 1));
9 kx off = align_file_offset(off, addr, abi_pagesize);
9 kx
9 kx increase_relro = this->increase_relro_;
9 kx if (this->script_options_->saw_sections_clause())
9 kx increase_relro = 0;
9 kx has_relro = false;
9 kx
9 kx new_addr = (*p)->set_section_addresses(target, this,
9 kx true, addr,
9 kx &increase_relro,
9 kx &has_relro,
9 kx &off, pshndx);
9 kx }
9 kx }
9 kx
9 kx addr = new_addr;
9 kx
9 kx // Implement --check-sections. We know that the segments
9 kx // are sorted by LMA.
9 kx if (check_sections && last_load_segment != NULL)
9 kx {
9 kx gold_assert(last_load_segment->paddr() <= (*p)->paddr());
9 kx if (last_load_segment->paddr() + last_load_segment->memsz()
9 kx > (*p)->paddr())
9 kx {
9 kx unsigned long long lb1 = last_load_segment->paddr();
9 kx unsigned long long le1 = lb1 + last_load_segment->memsz();
9 kx unsigned long long lb2 = (*p)->paddr();
9 kx unsigned long long le2 = lb2 + (*p)->memsz();
9 kx gold_error(_("load segment overlap [0x%llx -> 0x%llx] and "
9 kx "[0x%llx -> 0x%llx]"),
9 kx lb1, le1, lb2, le2);
9 kx }
9 kx }
9 kx last_load_segment = *p;
9 kx }
9 kx }
9 kx
9 kx if (load_seg != NULL && target->isolate_execinstr())
9 kx {
9 kx // Process the early segments again, setting their file offsets
9 kx // so they land after the segments starting at LOAD_SEG.
9 kx off = align_file_offset(off, 0, target->abi_pagesize());
9 kx
9 kx this->reset_relax_output();
9 kx
9 kx for (Segment_list::iterator p = this->segment_list_.begin();
9 kx *p != load_seg;
9 kx ++p)
9 kx {
9 kx if ((*p)->type() == elfcpp::PT_LOAD)
9 kx {
9 kx // We repeat the whole job of assigning addresses and
9 kx // offsets, but we really only want to change the offsets and
9 kx // must ensure that the addresses all come out the same as
9 kx // they did the first time through.
9 kx bool has_relro = false;
9 kx const uint64_t old_addr = (*p)->vaddr();
9 kx const uint64_t old_end = old_addr + (*p)->memsz();
9 kx uint64_t new_addr = (*p)->set_section_addresses(target, this,
9 kx true, old_addr,
9 kx &increase_relro,
9 kx &has_relro,
9 kx &off,
9 kx &shndx_begin);
9 kx gold_assert(new_addr == old_end);
9 kx }
9 kx }
9 kx
9 kx gold_assert(shndx_begin == shndx_load_seg);
9 kx }
9 kx
9 kx // Handle the non-PT_LOAD segments, setting their offsets from their
9 kx // section's offsets.
9 kx for (Segment_list::iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx {
9 kx // PT_GNU_STACK was set up correctly when it was created.
9 kx if ((*p)->type() != elfcpp::PT_LOAD
9 kx && (*p)->type() != elfcpp::PT_GNU_STACK)
9 kx (*p)->set_offset((*p)->type() == elfcpp::PT_GNU_RELRO
9 kx ? increase_relro
9 kx : 0);
9 kx }
9 kx
9 kx // Set the TLS offsets for each section in the PT_TLS segment.
9 kx if (this->tls_segment_ != NULL)
9 kx this->tls_segment_->set_tls_offsets();
9 kx
9 kx return off;
9 kx }
9 kx
9 kx // Set the offsets of all the allocated sections when doing a
9 kx // relocatable link. This does the same jobs as set_segment_offsets,
9 kx // only for a relocatable link.
9 kx
9 kx off_t
9 kx Layout::set_relocatable_section_offsets(Output_data* file_header,
9 kx unsigned int* pshndx)
9 kx {
9 kx off_t off = 0;
9 kx
9 kx file_header->set_address_and_file_offset(0, 0);
9 kx off += file_header->data_size();
9 kx
9 kx for (Section_list::iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx // We skip unallocated sections here, except that group sections
9 kx // have to come first.
9 kx if (((*p)->flags() & elfcpp::SHF_ALLOC) == 0
9 kx && (*p)->type() != elfcpp::SHT_GROUP)
9 kx continue;
9 kx
9 kx off = align_address(off, (*p)->addralign());
9 kx
9 kx // The linker script might have set the address.
9 kx if (!(*p)->is_address_valid())
9 kx (*p)->set_address(0);
9 kx (*p)->set_file_offset(off);
9 kx (*p)->finalize_data_size();
9 kx if ((*p)->type() != elfcpp::SHT_NOBITS)
9 kx off += (*p)->data_size();
9 kx
9 kx (*p)->set_out_shndx(*pshndx);
9 kx ++*pshndx;
9 kx }
9 kx
9 kx return off;
9 kx }
9 kx
9 kx // Set the file offset of all the sections not associated with a
9 kx // segment.
9 kx
9 kx off_t
9 kx Layout::set_section_offsets(off_t off, Layout::Section_offset_pass pass)
9 kx {
9 kx off_t startoff = off;
9 kx off_t maxoff = off;
9 kx
9 kx for (Section_list::iterator p = this->unattached_section_list_.begin();
9 kx p != this->unattached_section_list_.end();
9 kx ++p)
9 kx {
9 kx // The symtab section is handled in create_symtab_sections.
9 kx if (*p == this->symtab_section_)
9 kx continue;
9 kx
9 kx // If we've already set the data size, don't set it again.
9 kx if ((*p)->is_offset_valid() && (*p)->is_data_size_valid())
9 kx continue;
9 kx
9 kx if (pass == BEFORE_INPUT_SECTIONS_PASS
9 kx && (*p)->requires_postprocessing())
9 kx {
9 kx (*p)->create_postprocessing_buffer();
9 kx this->any_postprocessing_sections_ = true;
9 kx }
9 kx
9 kx if (pass == BEFORE_INPUT_SECTIONS_PASS
9 kx && (*p)->after_input_sections())
9 kx continue;
9 kx else if (pass == POSTPROCESSING_SECTIONS_PASS
9 kx && (!(*p)->after_input_sections()
9 kx || (*p)->type() == elfcpp::SHT_STRTAB))
9 kx continue;
9 kx else if (pass == STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS
9 kx && (!(*p)->after_input_sections()
9 kx || (*p)->type() != elfcpp::SHT_STRTAB))
9 kx continue;
9 kx
9 kx if (!parameters->incremental_update())
9 kx {
9 kx off = align_address(off, (*p)->addralign());
9 kx (*p)->set_file_offset(off);
9 kx (*p)->finalize_data_size();
9 kx }
9 kx else
9 kx {
9 kx // Incremental update: allocate file space from free list.
9 kx (*p)->pre_finalize_data_size();
9 kx off_t current_size = (*p)->current_data_size();
9 kx off = this->allocate(current_size, (*p)->addralign(), startoff);
9 kx if (off == -1)
9 kx {
9 kx if (is_debugging_enabled(DEBUG_INCREMENTAL))
9 kx this->free_list_.dump();
9 kx gold_assert((*p)->output_section() != NULL);
9 kx gold_fallback(_("out of patch space for section %s; "
9 kx "relink with --incremental-full"),
9 kx (*p)->output_section()->name());
9 kx }
9 kx (*p)->set_file_offset(off);
9 kx (*p)->finalize_data_size();
9 kx if ((*p)->data_size() > current_size)
9 kx {
9 kx gold_assert((*p)->output_section() != NULL);
9 kx gold_fallback(_("%s: section changed size; "
9 kx "relink with --incremental-full"),
9 kx (*p)->output_section()->name());
9 kx }
9 kx gold_debug(DEBUG_INCREMENTAL,
9 kx "set_section_offsets: %08lx %08lx %s",
9 kx static_cast<long>(off),
9 kx static_cast<long>((*p)->data_size()),
9 kx ((*p)->output_section() != NULL
9 kx ? (*p)->output_section()->name() : "(special)"));
9 kx }
9 kx
9 kx off += (*p)->data_size();
9 kx if (off > maxoff)
9 kx maxoff = off;
9 kx
9 kx // At this point the name must be set.
9 kx if (pass != STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS)
9 kx this->namepool_.add((*p)->name(), false, NULL);
9 kx }
9 kx return maxoff;
9 kx }
9 kx
9 kx // Set the section indexes of all the sections not associated with a
9 kx // segment.
9 kx
9 kx unsigned int
9 kx Layout::set_section_indexes(unsigned int shndx)
9 kx {
9 kx for (Section_list::iterator p = this->unattached_section_list_.begin();
9 kx p != this->unattached_section_list_.end();
9 kx ++p)
9 kx {
9 kx if (!(*p)->has_out_shndx())
9 kx {
9 kx (*p)->set_out_shndx(shndx);
9 kx ++shndx;
9 kx }
9 kx }
9 kx return shndx;
9 kx }
9 kx
9 kx // Set the section addresses according to the linker script. This is
9 kx // only called when we see a SECTIONS clause. This returns the
9 kx // program segment which should hold the file header and segment
9 kx // headers, if any. It will return NULL if they should not be in a
9 kx // segment.
9 kx
9 kx Output_segment*
9 kx Layout::set_section_addresses_from_script(Symbol_table* symtab)
9 kx {
9 kx Script_sections* ss = this->script_options_->script_sections();
9 kx gold_assert(ss->saw_sections_clause());
9 kx return this->script_options_->set_section_addresses(symtab, this);
9 kx }
9 kx
9 kx // Place the orphan sections in the linker script.
9 kx
9 kx void
9 kx Layout::place_orphan_sections_in_script()
9 kx {
9 kx Script_sections* ss = this->script_options_->script_sections();
9 kx gold_assert(ss->saw_sections_clause());
9 kx
9 kx // Place each orphaned output section in the script.
9 kx for (Section_list::iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if (!(*p)->found_in_sections_clause())
9 kx ss->place_orphan(*p);
9 kx }
9 kx }
9 kx
9 kx // Count the local symbols in the regular symbol table and the dynamic
9 kx // symbol table, and build the respective string pools.
9 kx
9 kx void
9 kx Layout::count_local_symbols(const Task* task,
9 kx const Input_objects* input_objects)
9 kx {
9 kx // First, figure out an upper bound on the number of symbols we'll
9 kx // be inserting into each pool. This helps us create the pools with
9 kx // the right size, to avoid unnecessary hashtable resizing.
9 kx unsigned int symbol_count = 0;
9 kx for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9 kx p != input_objects->relobj_end();
9 kx ++p)
9 kx symbol_count += (*p)->local_symbol_count();
9 kx
9 kx // Go from "upper bound" to "estimate." We overcount for two
9 kx // reasons: we double-count symbols that occur in more than one
9 kx // object file, and we count symbols that are dropped from the
9 kx // output. Add it all together and assume we overcount by 100%.
9 kx symbol_count /= 2;
9 kx
9 kx // We assume all symbols will go into both the sympool and dynpool.
9 kx this->sympool_.reserve(symbol_count);
9 kx this->dynpool_.reserve(symbol_count);
9 kx
9 kx for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9 kx p != input_objects->relobj_end();
9 kx ++p)
9 kx {
9 kx Task_lock_obj<Object> tlo(task, *p);
9 kx (*p)->count_local_symbols(&this->sympool_, &this->dynpool_);
9 kx }
9 kx }
9 kx
9 kx // Create the symbol table sections. Here we also set the final
9 kx // values of the symbols. At this point all the loadable sections are
9 kx // fully laid out. SHNUM is the number of sections so far.
9 kx
9 kx void
9 kx Layout::create_symtab_sections(const Input_objects* input_objects,
9 kx Symbol_table* symtab,
9 kx unsigned int shnum,
9 kx off_t* poff,
9 kx unsigned int local_dynamic_count)
9 kx {
9 kx int symsize;
9 kx unsigned int align;
9 kx if (parameters->target().get_size() == 32)
9 kx {
9 kx symsize = elfcpp::Elf_sizes<32>::sym_size;
9 kx align = 4;
9 kx }
9 kx else if (parameters->target().get_size() == 64)
9 kx {
9 kx symsize = elfcpp::Elf_sizes<64>::sym_size;
9 kx align = 8;
9 kx }
9 kx else
9 kx gold_unreachable();
9 kx
9 kx // Compute file offsets relative to the start of the symtab section.
9 kx off_t off = 0;
9 kx
9 kx // Save space for the dummy symbol at the start of the section. We
9 kx // never bother to write this out--it will just be left as zero.
9 kx off += symsize;
9 kx unsigned int local_symbol_index = 1;
9 kx
9 kx // Add STT_SECTION symbols for each Output section which needs one.
9 kx for (Section_list::iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if (!(*p)->needs_symtab_index())
9 kx (*p)->set_symtab_index(-1U);
9 kx else
9 kx {
9 kx (*p)->set_symtab_index(local_symbol_index);
9 kx ++local_symbol_index;
9 kx off += symsize;
9 kx }
9 kx }
9 kx
9 kx for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9 kx p != input_objects->relobj_end();
9 kx ++p)
9 kx {
9 kx unsigned int index = (*p)->finalize_local_symbols(local_symbol_index,
9 kx off, symtab);
9 kx off += (index - local_symbol_index) * symsize;
9 kx local_symbol_index = index;
9 kx }
9 kx
9 kx unsigned int local_symcount = local_symbol_index;
9 kx gold_assert(static_cast<off_t>(local_symcount * symsize) == off);
9 kx
9 kx off_t dynoff;
9 kx size_t dyncount;
9 kx if (this->dynsym_section_ == NULL)
9 kx {
9 kx dynoff = 0;
9 kx dyncount = 0;
9 kx }
9 kx else
9 kx {
9 kx off_t locsize = local_dynamic_count * this->dynsym_section_->entsize();
9 kx dynoff = this->dynsym_section_->offset() + locsize;
9 kx dyncount = (this->dynsym_section_->data_size() - locsize) / symsize;
9 kx gold_assert(static_cast<off_t>(dyncount * symsize)
9 kx == this->dynsym_section_->data_size() - locsize);
9 kx }
9 kx
9 kx off_t global_off = off;
9 kx off = symtab->finalize(off, dynoff, local_dynamic_count, dyncount,
9 kx &this->sympool_, &local_symcount);
9 kx
9 kx if (!parameters->options().strip_all())
9 kx {
9 kx this->sympool_.set_string_offsets();
9 kx
9 kx const char* symtab_name = this->namepool_.add(".symtab", false, NULL);
9 kx Output_section* osymtab = this->make_output_section(symtab_name,
9 kx elfcpp::SHT_SYMTAB,
9 kx 0, ORDER_INVALID,
9 kx false);
9 kx this->symtab_section_ = osymtab;
9 kx
9 kx Output_section_data* pos = new Output_data_fixed_space(off, align,
9 kx "** symtab");
9 kx osymtab->add_output_section_data(pos);
9 kx
9 kx // We generate a .symtab_shndx section if we have more than
9 kx // SHN_LORESERVE sections. Technically it is possible that we
9 kx // don't need one, because it is possible that there are no
9 kx // symbols in any of sections with indexes larger than
9 kx // SHN_LORESERVE. That is probably unusual, though, and it is
9 kx // easier to always create one than to compute section indexes
9 kx // twice (once here, once when writing out the symbols).
9 kx if (shnum >= elfcpp::SHN_LORESERVE)
9 kx {
9 kx const char* symtab_xindex_name = this->namepool_.add(".symtab_shndx",
9 kx false, NULL);
9 kx Output_section* osymtab_xindex =
9 kx this->make_output_section(symtab_xindex_name,
9 kx elfcpp::SHT_SYMTAB_SHNDX, 0,
9 kx ORDER_INVALID, false);
9 kx
9 kx size_t symcount = off / symsize;
9 kx this->symtab_xindex_ = new Output_symtab_xindex(symcount);
9 kx
9 kx osymtab_xindex->add_output_section_data(this->symtab_xindex_);
9 kx
9 kx osymtab_xindex->set_link_section(osymtab);
9 kx osymtab_xindex->set_addralign(4);
9 kx osymtab_xindex->set_entsize(4);
9 kx
9 kx osymtab_xindex->set_after_input_sections();
9 kx
9 kx // This tells the driver code to wait until the symbol table
9 kx // has written out before writing out the postprocessing
9 kx // sections, including the .symtab_shndx section.
9 kx this->any_postprocessing_sections_ = true;
9 kx }
9 kx
9 kx const char* strtab_name = this->namepool_.add(".strtab", false, NULL);
9 kx Output_section* ostrtab = this->make_output_section(strtab_name,
9 kx elfcpp::SHT_STRTAB,
9 kx 0, ORDER_INVALID,
9 kx false);
9 kx
9 kx Output_section_data* pstr = new Output_data_strtab(&this->sympool_);
9 kx ostrtab->add_output_section_data(pstr);
9 kx
9 kx off_t symtab_off;
9 kx if (!parameters->incremental_update())
9 kx symtab_off = align_address(*poff, align);
9 kx else
9 kx {
9 kx symtab_off = this->allocate(off, align, *poff);
9 kx if (off == -1)
9 kx gold_fallback(_("out of patch space for symbol table; "
9 kx "relink with --incremental-full"));
9 kx gold_debug(DEBUG_INCREMENTAL,
9 kx "create_symtab_sections: %08lx %08lx .symtab",
9 kx static_cast<long>(symtab_off),
9 kx static_cast<long>(off));
9 kx }
9 kx
9 kx symtab->set_file_offset(symtab_off + global_off);
9 kx osymtab->set_file_offset(symtab_off);
9 kx osymtab->finalize_data_size();
9 kx osymtab->set_link_section(ostrtab);
9 kx osymtab->set_info(local_symcount);
9 kx osymtab->set_entsize(symsize);
9 kx
9 kx if (symtab_off + off > *poff)
9 kx *poff = symtab_off + off;
9 kx }
9 kx }
9 kx
9 kx // Create the .shstrtab section, which holds the names of the
9 kx // sections. At the time this is called, we have created all the
9 kx // output sections except .shstrtab itself.
9 kx
9 kx Output_section*
9 kx Layout::create_shstrtab()
9 kx {
9 kx // FIXME: We don't need to create a .shstrtab section if we are
9 kx // stripping everything.
9 kx
9 kx const char* name = this->namepool_.add(".shstrtab", false, NULL);
9 kx
9 kx Output_section* os = this->make_output_section(name, elfcpp::SHT_STRTAB, 0,
9 kx ORDER_INVALID, false);
9 kx
9 kx if (strcmp(parameters->options().compress_debug_sections(), "none") != 0)
9 kx {
9 kx // We can't write out this section until we've set all the
9 kx // section names, and we don't set the names of compressed
9 kx // output sections until relocations are complete. FIXME: With
9 kx // the current names we use, this is unnecessary.
9 kx os->set_after_input_sections();
9 kx }
9 kx
9 kx Output_section_data* posd = new Output_data_strtab(&this->namepool_);
9 kx os->add_output_section_data(posd);
9 kx
9 kx return os;
9 kx }
9 kx
9 kx // Create the section headers. SIZE is 32 or 64. OFF is the file
9 kx // offset.
9 kx
9 kx void
9 kx Layout::create_shdrs(const Output_section* shstrtab_section, off_t* poff)
9 kx {
9 kx Output_section_headers* oshdrs;
9 kx oshdrs = new Output_section_headers(this,
9 kx &this->segment_list_,
9 kx &this->section_list_,
9 kx &this->unattached_section_list_,
9 kx &this->namepool_,
9 kx shstrtab_section);
9 kx off_t off;
9 kx if (!parameters->incremental_update())
9 kx off = align_address(*poff, oshdrs->addralign());
9 kx else
9 kx {
9 kx oshdrs->pre_finalize_data_size();
9 kx off = this->allocate(oshdrs->data_size(), oshdrs->addralign(), *poff);
9 kx if (off == -1)
9 kx gold_fallback(_("out of patch space for section header table; "
9 kx "relink with --incremental-full"));
9 kx gold_debug(DEBUG_INCREMENTAL,
9 kx "create_shdrs: %08lx %08lx (section header table)",
9 kx static_cast<long>(off),
9 kx static_cast<long>(off + oshdrs->data_size()));
9 kx }
9 kx oshdrs->set_address_and_file_offset(0, off);
9 kx off += oshdrs->data_size();
9 kx if (off > *poff)
9 kx *poff = off;
9 kx this->section_headers_ = oshdrs;
9 kx }
9 kx
9 kx // Count the allocated sections.
9 kx
9 kx size_t
9 kx Layout::allocated_output_section_count() const
9 kx {
9 kx size_t section_count = 0;
9 kx for (Segment_list::const_iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx section_count += (*p)->output_section_count();
9 kx return section_count;
9 kx }
9 kx
9 kx // Create the dynamic symbol table.
9 kx // *PLOCAL_DYNAMIC_COUNT will be set to the number of local symbols
9 kx // from input objects, and *PFORCED_LOCAL_DYNAMIC_COUNT will be set
9 kx // to the number of global symbols that have been forced local.
9 kx // We need to remember the former because the forced-local symbols are
9 kx // written along with the global symbols in Symtab::write_globals().
9 kx
9 kx void
9 kx Layout::create_dynamic_symtab(const Input_objects* input_objects,
9 kx Symbol_table* symtab,
9 kx Output_section** pdynstr,
9 kx unsigned int* plocal_dynamic_count,
9 kx unsigned int* pforced_local_dynamic_count,
9 kx std::vector<Symbol*>* pdynamic_symbols,
9 kx Versions* pversions)
9 kx {
9 kx // Count all the symbols in the dynamic symbol table, and set the
9 kx // dynamic symbol indexes.
9 kx
9 kx // Skip symbol 0, which is always all zeroes.
9 kx unsigned int index = 1;
9 kx
9 kx // Add STT_SECTION symbols for each Output section which needs one.
9 kx for (Section_list::iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if (!(*p)->needs_dynsym_index())
9 kx (*p)->set_dynsym_index(-1U);
9 kx else
9 kx {
9 kx (*p)->set_dynsym_index(index);
9 kx ++index;
9 kx }
9 kx }
9 kx
9 kx // Count the local symbols that need to go in the dynamic symbol table,
9 kx // and set the dynamic symbol indexes.
9 kx for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9 kx p != input_objects->relobj_end();
9 kx ++p)
9 kx {
9 kx unsigned int new_index = (*p)->set_local_dynsym_indexes(index);
9 kx index = new_index;
9 kx }
9 kx
9 kx unsigned int local_symcount = index;
9 kx unsigned int forced_local_count = 0;
9 kx
9 kx index = symtab->set_dynsym_indexes(index, &forced_local_count,
9 kx pdynamic_symbols, &this->dynpool_,
9 kx pversions);
9 kx
9 kx *plocal_dynamic_count = local_symcount;
9 kx *pforced_local_dynamic_count = forced_local_count;
9 kx
9 kx int symsize;
9 kx unsigned int align;
9 kx const int size = parameters->target().get_size();
9 kx if (size == 32)
9 kx {
9 kx symsize = elfcpp::Elf_sizes<32>::sym_size;
9 kx align = 4;
9 kx }
9 kx else if (size == 64)
9 kx {
9 kx symsize = elfcpp::Elf_sizes<64>::sym_size;
9 kx align = 8;
9 kx }
9 kx else
9 kx gold_unreachable();
9 kx
9 kx // Create the dynamic symbol table section.
9 kx
9 kx Output_section* dynsym = this->choose_output_section(NULL, ".dynsym",
9 kx elfcpp::SHT_DYNSYM,
9 kx elfcpp::SHF_ALLOC,
9 kx false,
9 kx ORDER_DYNAMIC_LINKER,
9 kx false, false, false);
9 kx
9 kx // Check for NULL as a linker script may discard .dynsym.
9 kx if (dynsym != NULL)
9 kx {
9 kx Output_section_data* odata = new Output_data_fixed_space(index * symsize,
9 kx align,
9 kx "** dynsym");
9 kx dynsym->add_output_section_data(odata);
9 kx
9 kx dynsym->set_info(local_symcount + forced_local_count);
9 kx dynsym->set_entsize(symsize);
9 kx dynsym->set_addralign(align);
9 kx
9 kx this->dynsym_section_ = dynsym;
9 kx }
9 kx
9 kx Output_data_dynamic* const odyn = this->dynamic_data_;
9 kx if (odyn != NULL)
9 kx {
9 kx odyn->add_section_address(elfcpp::DT_SYMTAB, dynsym);
9 kx odyn->add_constant(elfcpp::DT_SYMENT, symsize);
9 kx }
9 kx
9 kx // If there are more than SHN_LORESERVE allocated sections, we
9 kx // create a .dynsym_shndx section. It is possible that we don't
9 kx // need one, because it is possible that there are no dynamic
9 kx // symbols in any of the sections with indexes larger than
9 kx // SHN_LORESERVE. This is probably unusual, though, and at this
9 kx // time we don't know the actual section indexes so it is
9 kx // inconvenient to check.
9 kx if (this->allocated_output_section_count() >= elfcpp::SHN_LORESERVE)
9 kx {
9 kx Output_section* dynsym_xindex =
9 kx this->choose_output_section(NULL, ".dynsym_shndx",
9 kx elfcpp::SHT_SYMTAB_SHNDX,
9 kx elfcpp::SHF_ALLOC,
9 kx false, ORDER_DYNAMIC_LINKER, false, false,
9 kx false);
9 kx
9 kx if (dynsym_xindex != NULL)
9 kx {
9 kx this->dynsym_xindex_ = new Output_symtab_xindex(index);
9 kx
9 kx dynsym_xindex->add_output_section_data(this->dynsym_xindex_);
9 kx
9 kx dynsym_xindex->set_link_section(dynsym);
9 kx dynsym_xindex->set_addralign(4);
9 kx dynsym_xindex->set_entsize(4);
9 kx
9 kx dynsym_xindex->set_after_input_sections();
9 kx
9 kx // This tells the driver code to wait until the symbol table
9 kx // has written out before writing out the postprocessing
9 kx // sections, including the .dynsym_shndx section.
9 kx this->any_postprocessing_sections_ = true;
9 kx }
9 kx }
9 kx
9 kx // Create the dynamic string table section.
9 kx
9 kx Output_section* dynstr = this->choose_output_section(NULL, ".dynstr",
9 kx elfcpp::SHT_STRTAB,
9 kx elfcpp::SHF_ALLOC,
9 kx false,
9 kx ORDER_DYNAMIC_LINKER,
9 kx false, false, false);
9 kx *pdynstr = dynstr;
9 kx if (dynstr != NULL)
9 kx {
9 kx Output_section_data* strdata = new Output_data_strtab(&this->dynpool_);
9 kx dynstr->add_output_section_data(strdata);
9 kx
9 kx if (dynsym != NULL)
9 kx dynsym->set_link_section(dynstr);
9 kx if (this->dynamic_section_ != NULL)
9 kx this->dynamic_section_->set_link_section(dynstr);
9 kx
9 kx if (odyn != NULL)
9 kx {
9 kx odyn->add_section_address(elfcpp::DT_STRTAB, dynstr);
9 kx odyn->add_section_size(elfcpp::DT_STRSZ, dynstr);
9 kx }
9 kx }
9 kx
9 kx // Create the hash tables. The Gnu-style hash table must be
9 kx // built first, because it changes the order of the symbols
9 kx // in the dynamic symbol table.
9 kx
9 kx if (strcmp(parameters->options().hash_style(), "gnu") == 0
9 kx || strcmp(parameters->options().hash_style(), "both") == 0)
9 kx {
9 kx unsigned char* phash;
9 kx unsigned int hashlen;
9 kx Dynobj::create_gnu_hash_table(*pdynamic_symbols,
9 kx local_symcount + forced_local_count,
9 kx &phash, &hashlen);
9 kx
9 kx Output_section* hashsec =
9 kx this->choose_output_section(NULL, ".gnu.hash", elfcpp::SHT_GNU_HASH,
9 kx elfcpp::SHF_ALLOC, false,
9 kx ORDER_DYNAMIC_LINKER, false, false,
9 kx false);
9 kx
9 kx Output_section_data* hashdata = new Output_data_const_buffer(phash,
9 kx hashlen,
9 kx align,
9 kx "** hash");
9 kx if (hashsec != NULL && hashdata != NULL)
9 kx hashsec->add_output_section_data(hashdata);
9 kx
9 kx if (hashsec != NULL)
9 kx {
9 kx if (dynsym != NULL)
9 kx hashsec->set_link_section(dynsym);
9 kx
9 kx // For a 64-bit target, the entries in .gnu.hash do not have
9 kx // a uniform size, so we only set the entry size for a
9 kx // 32-bit target.
9 kx if (parameters->target().get_size() == 32)
9 kx hashsec->set_entsize(4);
9 kx
9 kx if (odyn != NULL)
9 kx odyn->add_section_address(elfcpp::DT_GNU_HASH, hashsec);
9 kx }
9 kx }
9 kx
9 kx if (strcmp(parameters->options().hash_style(), "sysv") == 0
9 kx || strcmp(parameters->options().hash_style(), "both") == 0)
9 kx {
9 kx unsigned char* phash;
9 kx unsigned int hashlen;
9 kx Dynobj::create_elf_hash_table(*pdynamic_symbols,
9 kx local_symcount + forced_local_count,
9 kx &phash, &hashlen);
9 kx
9 kx Output_section* hashsec =
9 kx this->choose_output_section(NULL, ".hash", elfcpp::SHT_HASH,
9 kx elfcpp::SHF_ALLOC, false,
9 kx ORDER_DYNAMIC_LINKER, false, false,
9 kx false);
9 kx
9 kx Output_section_data* hashdata = new Output_data_const_buffer(phash,
9 kx hashlen,
9 kx align,
9 kx "** hash");
9 kx if (hashsec != NULL && hashdata != NULL)
9 kx hashsec->add_output_section_data(hashdata);
9 kx
9 kx if (hashsec != NULL)
9 kx {
9 kx if (dynsym != NULL)
9 kx hashsec->set_link_section(dynsym);
9 kx hashsec->set_entsize(parameters->target().hash_entry_size() / 8);
9 kx }
9 kx
9 kx if (odyn != NULL)
9 kx odyn->add_section_address(elfcpp::DT_HASH, hashsec);
9 kx }
9 kx }
9 kx
9 kx // Assign offsets to each local portion of the dynamic symbol table.
9 kx
9 kx void
9 kx Layout::assign_local_dynsym_offsets(const Input_objects* input_objects)
9 kx {
9 kx Output_section* dynsym = this->dynsym_section_;
9 kx if (dynsym == NULL)
9 kx return;
9 kx
9 kx off_t off = dynsym->offset();
9 kx
9 kx // Skip the dummy symbol at the start of the section.
9 kx off += dynsym->entsize();
9 kx
9 kx for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
9 kx p != input_objects->relobj_end();
9 kx ++p)
9 kx {
9 kx unsigned int count = (*p)->set_local_dynsym_offset(off);
9 kx off += count * dynsym->entsize();
9 kx }
9 kx }
9 kx
9 kx // Create the version sections.
9 kx
9 kx void
9 kx Layout::create_version_sections(const Versions* versions,
9 kx const Symbol_table* symtab,
9 kx unsigned int local_symcount,
9 kx const std::vector<Symbol*>& dynamic_symbols,
9 kx const Output_section* dynstr)
9 kx {
9 kx if (!versions->any_defs() && !versions->any_needs())
9 kx return;
9 kx
9 kx switch (parameters->size_and_endianness())
9 kx {
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx case Parameters::TARGET_32_LITTLE:
9 kx this->sized_create_version_sections<32, false>(versions, symtab,
9 kx local_symcount,
9 kx dynamic_symbols, dynstr);
9 kx break;
9 kx #endif
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx case Parameters::TARGET_32_BIG:
9 kx this->sized_create_version_sections<32, true>(versions, symtab,
9 kx local_symcount,
9 kx dynamic_symbols, dynstr);
9 kx break;
9 kx #endif
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx case Parameters::TARGET_64_LITTLE:
9 kx this->sized_create_version_sections<64, false>(versions, symtab,
9 kx local_symcount,
9 kx dynamic_symbols, dynstr);
9 kx break;
9 kx #endif
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx case Parameters::TARGET_64_BIG:
9 kx this->sized_create_version_sections<64, true>(versions, symtab,
9 kx local_symcount,
9 kx dynamic_symbols, dynstr);
9 kx break;
9 kx #endif
9 kx default:
9 kx gold_unreachable();
9 kx }
9 kx }
9 kx
9 kx // Create the version sections, sized version.
9 kx
9 kx template<int size, bool big_endian>
9 kx void
9 kx Layout::sized_create_version_sections(
9 kx const Versions* versions,
9 kx const Symbol_table* symtab,
9 kx unsigned int local_symcount,
9 kx const std::vector<Symbol*>& dynamic_symbols,
9 kx const Output_section* dynstr)
9 kx {
9 kx Output_section* vsec = this->choose_output_section(NULL, ".gnu.version",
9 kx elfcpp::SHT_GNU_versym,
9 kx elfcpp::SHF_ALLOC,
9 kx false,
9 kx ORDER_DYNAMIC_LINKER,
9 kx false, false, false);
9 kx
9 kx // Check for NULL since a linker script may discard this section.
9 kx if (vsec != NULL)
9 kx {
9 kx unsigned char* vbuf;
9 kx unsigned int vsize;
9 kx versions->symbol_section_contents<size, big_endian>(symtab,
9 kx &this->dynpool_,
9 kx local_symcount,
9 kx dynamic_symbols,
9 kx &vbuf, &vsize);
9 kx
9 kx Output_section_data* vdata = new Output_data_const_buffer(vbuf, vsize, 2,
9 kx "** versions");
9 kx
9 kx vsec->add_output_section_data(vdata);
9 kx vsec->set_entsize(2);
9 kx vsec->set_link_section(this->dynsym_section_);
9 kx }
9 kx
9 kx Output_data_dynamic* const odyn = this->dynamic_data_;
9 kx if (odyn != NULL && vsec != NULL)
9 kx odyn->add_section_address(elfcpp::DT_VERSYM, vsec);
9 kx
9 kx if (versions->any_defs())
9 kx {
9 kx Output_section* vdsec;
9 kx vdsec = this->choose_output_section(NULL, ".gnu.version_d",
9 kx elfcpp::SHT_GNU_verdef,
9 kx elfcpp::SHF_ALLOC,
9 kx false, ORDER_DYNAMIC_LINKER, false,
9 kx false, false);
9 kx
9 kx if (vdsec != NULL)
9 kx {
9 kx unsigned char* vdbuf;
9 kx unsigned int vdsize;
9 kx unsigned int vdentries;
9 kx versions->def_section_contents<size, big_endian>(&this->dynpool_,
9 kx &vdbuf, &vdsize,
9 kx &vdentries);
9 kx
9 kx Output_section_data* vddata =
9 kx new Output_data_const_buffer(vdbuf, vdsize, 4, "** version defs");
9 kx
9 kx vdsec->add_output_section_data(vddata);
9 kx vdsec->set_link_section(dynstr);
9 kx vdsec->set_info(vdentries);
9 kx
9 kx if (odyn != NULL)
9 kx {
9 kx odyn->add_section_address(elfcpp::DT_VERDEF, vdsec);
9 kx odyn->add_constant(elfcpp::DT_VERDEFNUM, vdentries);
9 kx }
9 kx }
9 kx }
9 kx
9 kx if (versions->any_needs())
9 kx {
9 kx Output_section* vnsec;
9 kx vnsec = this->choose_output_section(NULL, ".gnu.version_r",
9 kx elfcpp::SHT_GNU_verneed,
9 kx elfcpp::SHF_ALLOC,
9 kx false, ORDER_DYNAMIC_LINKER, false,
9 kx false, false);
9 kx
9 kx if (vnsec != NULL)
9 kx {
9 kx unsigned char* vnbuf;
9 kx unsigned int vnsize;
9 kx unsigned int vnentries;
9 kx versions->need_section_contents<size, big_endian>(&this->dynpool_,
9 kx &vnbuf, &vnsize,
9 kx &vnentries);
9 kx
9 kx Output_section_data* vndata =
9 kx new Output_data_const_buffer(vnbuf, vnsize, 4, "** version refs");
9 kx
9 kx vnsec->add_output_section_data(vndata);
9 kx vnsec->set_link_section(dynstr);
9 kx vnsec->set_info(vnentries);
9 kx
9 kx if (odyn != NULL)
9 kx {
9 kx odyn->add_section_address(elfcpp::DT_VERNEED, vnsec);
9 kx odyn->add_constant(elfcpp::DT_VERNEEDNUM, vnentries);
9 kx }
9 kx }
9 kx }
9 kx }
9 kx
9 kx // Create the .interp section and PT_INTERP segment.
9 kx
9 kx void
9 kx Layout::create_interp(const Target* target)
9 kx {
9 kx gold_assert(this->interp_segment_ == NULL);
9 kx
9 kx const char* interp = parameters->options().dynamic_linker();
9 kx if (interp == NULL)
9 kx {
9 kx interp = target->dynamic_linker();
9 kx gold_assert(interp != NULL);
9 kx }
9 kx
9 kx size_t len = strlen(interp) + 1;
9 kx
9 kx Output_section_data* odata = new Output_data_const(interp, len, 1);
9 kx
9 kx Output_section* osec = this->choose_output_section(NULL, ".interp",
9 kx elfcpp::SHT_PROGBITS,
9 kx elfcpp::SHF_ALLOC,
9 kx false, ORDER_INTERP,
9 kx false, false, false);
9 kx if (osec != NULL)
9 kx osec->add_output_section_data(odata);
9 kx }
9 kx
9 kx // Add dynamic tags for the PLT and the dynamic relocs. This is
9 kx // called by the target-specific code. This does nothing if not doing
9 kx // a dynamic link.
9 kx
9 kx // USE_REL is true for REL relocs rather than RELA relocs.
9 kx
9 kx // If PLT_GOT is not NULL, then DT_PLTGOT points to it.
9 kx
9 kx // If PLT_REL is not NULL, it is used for DT_PLTRELSZ, and DT_JMPREL,
9 kx // and we also set DT_PLTREL. We use PLT_REL's output section, since
9 kx // some targets have multiple reloc sections in PLT_REL.
9 kx
9 kx // If DYN_REL is not NULL, it is used for DT_REL/DT_RELA,
9 kx // DT_RELSZ/DT_RELASZ, DT_RELENT/DT_RELAENT. Again we use the output
9 kx // section.
9 kx
9 kx // If ADD_DEBUG is true, we add a DT_DEBUG entry when generating an
9 kx // executable.
9 kx
9 kx void
9 kx Layout::add_target_dynamic_tags(bool use_rel, const Output_data* plt_got,
9 kx const Output_data* plt_rel,
9 kx const Output_data_reloc_generic* dyn_rel,
9 kx bool add_debug, bool dynrel_includes_plt)
9 kx {
9 kx Output_data_dynamic* odyn = this->dynamic_data_;
9 kx if (odyn == NULL)
9 kx return;
9 kx
9 kx if (plt_got != NULL && plt_got->output_section() != NULL)
9 kx odyn->add_section_address(elfcpp::DT_PLTGOT, plt_got);
9 kx
9 kx if (plt_rel != NULL && plt_rel->output_section() != NULL)
9 kx {
9 kx odyn->add_section_size(elfcpp::DT_PLTRELSZ, plt_rel->output_section());
9 kx odyn->add_section_address(elfcpp::DT_JMPREL, plt_rel->output_section());
9 kx odyn->add_constant(elfcpp::DT_PLTREL,
9 kx use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA);
9 kx }
9 kx
9 kx if ((dyn_rel != NULL && dyn_rel->output_section() != NULL)
9 kx || (dynrel_includes_plt
9 kx && plt_rel != NULL
9 kx && plt_rel->output_section() != NULL))
9 kx {
9 kx bool have_dyn_rel = dyn_rel != NULL && dyn_rel->output_section() != NULL;
9 kx bool have_plt_rel = plt_rel != NULL && plt_rel->output_section() != NULL;
9 kx odyn->add_section_address(use_rel ? elfcpp::DT_REL : elfcpp::DT_RELA,
9 kx (have_dyn_rel
9 kx ? dyn_rel->output_section()
9 kx : plt_rel->output_section()));
9 kx elfcpp::DT size_tag = use_rel ? elfcpp::DT_RELSZ : elfcpp::DT_RELASZ;
9 kx if (have_dyn_rel && have_plt_rel && dynrel_includes_plt)
9 kx odyn->add_section_size(size_tag,
9 kx dyn_rel->output_section(),
9 kx plt_rel->output_section());
9 kx else if (have_dyn_rel)
9 kx odyn->add_section_size(size_tag, dyn_rel->output_section());
9 kx else
9 kx odyn->add_section_size(size_tag, plt_rel->output_section());
9 kx const int size = parameters->target().get_size();
9 kx elfcpp::DT rel_tag;
9 kx int rel_size;
9 kx if (use_rel)
9 kx {
9 kx rel_tag = elfcpp::DT_RELENT;
9 kx if (size == 32)
9 kx rel_size = Reloc_types<elfcpp::SHT_REL, 32, false>::reloc_size;
9 kx else if (size == 64)
9 kx rel_size = Reloc_types<elfcpp::SHT_REL, 64, false>::reloc_size;
9 kx else
9 kx gold_unreachable();
9 kx }
9 kx else
9 kx {
9 kx rel_tag = elfcpp::DT_RELAENT;
9 kx if (size == 32)
9 kx rel_size = Reloc_types<elfcpp::SHT_RELA, 32, false>::reloc_size;
9 kx else if (size == 64)
9 kx rel_size = Reloc_types<elfcpp::SHT_RELA, 64, false>::reloc_size;
9 kx else
9 kx gold_unreachable();
9 kx }
9 kx odyn->add_constant(rel_tag, rel_size);
9 kx
9 kx if (parameters->options().combreloc() && have_dyn_rel)
9 kx {
9 kx size_t c = dyn_rel->relative_reloc_count();
9 kx if (c > 0)
9 kx odyn->add_constant((use_rel
9 kx ? elfcpp::DT_RELCOUNT
9 kx : elfcpp::DT_RELACOUNT),
9 kx c);
9 kx }
9 kx }
9 kx
9 kx if (add_debug && !parameters->options().shared())
9 kx {
9 kx // The value of the DT_DEBUG tag is filled in by the dynamic
9 kx // linker at run time, and used by the debugger.
9 kx odyn->add_constant(elfcpp::DT_DEBUG, 0);
9 kx }
9 kx }
9 kx
9 kx void
9 kx Layout::add_target_specific_dynamic_tag(elfcpp::DT tag, unsigned int val)
9 kx {
9 kx Output_data_dynamic* odyn = this->dynamic_data_;
9 kx if (odyn == NULL)
9 kx return;
9 kx odyn->add_constant(tag, val);
9 kx }
9 kx
9 kx // Finish the .dynamic section and PT_DYNAMIC segment.
9 kx
9 kx void
9 kx Layout::finish_dynamic_section(const Input_objects* input_objects,
9 kx const Symbol_table* symtab)
9 kx {
9 kx if (!this->script_options_->saw_phdrs_clause()
9 kx && this->dynamic_section_ != NULL)
9 kx {
9 kx Output_segment* oseg = this->make_output_segment(elfcpp::PT_DYNAMIC,
9 kx (elfcpp::PF_R
9 kx | elfcpp::PF_W));
9 kx oseg->add_output_section_to_nonload(this->dynamic_section_,
9 kx elfcpp::PF_R | elfcpp::PF_W);
9 kx }
9 kx
9 kx Output_data_dynamic* const odyn = this->dynamic_data_;
9 kx if (odyn == NULL)
9 kx return;
9 kx
9 kx for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
9 kx p != input_objects->dynobj_end();
9 kx ++p)
9 kx {
9 kx if (!(*p)->is_needed() && (*p)->as_needed())
9 kx {
9 kx // This dynamic object was linked with --as-needed, but it
9 kx // is not needed.
9 kx continue;
9 kx }
9 kx
9 kx odyn->add_string(elfcpp::DT_NEEDED, (*p)->soname());
9 kx }
9 kx
9 kx if (parameters->options().shared())
9 kx {
9 kx const char* soname = parameters->options().soname();
9 kx if (soname != NULL)
9 kx odyn->add_string(elfcpp::DT_SONAME, soname);
9 kx }
9 kx
9 kx Symbol* sym = symtab->lookup(parameters->options().init());
9 kx if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
9 kx odyn->add_symbol(elfcpp::DT_INIT, sym);
9 kx
9 kx sym = symtab->lookup(parameters->options().fini());
9 kx if (sym != NULL && sym->is_defined() && !sym->is_from_dynobj())
9 kx odyn->add_symbol(elfcpp::DT_FINI, sym);
9 kx
9 kx // Look for .init_array, .preinit_array and .fini_array by checking
9 kx // section types.
9 kx for(Layout::Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx switch((*p)->type())
9 kx {
9 kx case elfcpp::SHT_FINI_ARRAY:
9 kx odyn->add_section_address(elfcpp::DT_FINI_ARRAY, *p);
9 kx odyn->add_section_size(elfcpp::DT_FINI_ARRAYSZ, *p);
9 kx break;
9 kx case elfcpp::SHT_INIT_ARRAY:
9 kx odyn->add_section_address(elfcpp::DT_INIT_ARRAY, *p);
9 kx odyn->add_section_size(elfcpp::DT_INIT_ARRAYSZ, *p);
9 kx break;
9 kx case elfcpp::SHT_PREINIT_ARRAY:
9 kx odyn->add_section_address(elfcpp::DT_PREINIT_ARRAY, *p);
9 kx odyn->add_section_size(elfcpp::DT_PREINIT_ARRAYSZ, *p);
9 kx break;
9 kx default:
9 kx break;
9 kx }
9 kx
9 kx // Add a DT_RPATH entry if needed.
9 kx const General_options::Dir_list& rpath(parameters->options().rpath());
9 kx if (!rpath.empty())
9 kx {
9 kx std::string rpath_val;
9 kx for (General_options::Dir_list::const_iterator p = rpath.begin();
9 kx p != rpath.end();
9 kx ++p)
9 kx {
9 kx if (rpath_val.empty())
9 kx rpath_val = p->name();
9 kx else
9 kx {
9 kx // Eliminate duplicates.
9 kx General_options::Dir_list::const_iterator q;
9 kx for (q = rpath.begin(); q != p; ++q)
9 kx if (q->name() == p->name())
9 kx break;
9 kx if (q == p)
9 kx {
9 kx rpath_val += ':';
9 kx rpath_val += p->name();
9 kx }
9 kx }
9 kx }
9 kx
9 kx if (!parameters->options().enable_new_dtags())
9 kx odyn->add_string(elfcpp::DT_RPATH, rpath_val);
9 kx else
9 kx odyn->add_string(elfcpp::DT_RUNPATH, rpath_val);
9 kx }
9 kx
9 kx // Look for text segments that have dynamic relocations.
9 kx bool have_textrel = false;
9 kx if (!this->script_options_->saw_sections_clause())
9 kx {
9 kx for (Segment_list::const_iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->type() == elfcpp::PT_LOAD
9 kx && ((*p)->flags() & elfcpp::PF_W) == 0
9 kx && (*p)->has_dynamic_reloc())
9 kx {
9 kx have_textrel = true;
9 kx break;
9 kx }
9 kx }
9 kx }
9 kx else
9 kx {
9 kx // We don't know the section -> segment mapping, so we are
9 kx // conservative and just look for readonly sections with
9 kx // relocations. If those sections wind up in writable segments,
9 kx // then we have created an unnecessary DT_TEXTREL entry.
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0
9 kx && ((*p)->flags() & elfcpp::SHF_WRITE) == 0
9 kx && (*p)->has_dynamic_reloc())
9 kx {
9 kx have_textrel = true;
9 kx break;
9 kx }
9 kx }
9 kx }
9 kx
9 kx if (parameters->options().filter() != NULL)
9 kx odyn->add_string(elfcpp::DT_FILTER, parameters->options().filter());
9 kx if (parameters->options().any_auxiliary())
9 kx {
9 kx for (options::String_set::const_iterator p =
9 kx parameters->options().auxiliary_begin();
9 kx p != parameters->options().auxiliary_end();
9 kx ++p)
9 kx odyn->add_string(elfcpp::DT_AUXILIARY, *p);
9 kx }
9 kx
9 kx // Add a DT_FLAGS entry if necessary.
9 kx unsigned int flags = 0;
9 kx if (have_textrel)
9 kx {
9 kx // Add a DT_TEXTREL for compatibility with older loaders.
9 kx odyn->add_constant(elfcpp::DT_TEXTREL, 0);
9 kx flags |= elfcpp::DF_TEXTREL;
9 kx
9 kx if (parameters->options().text())
9 kx gold_error(_("read-only segment has dynamic relocations"));
9 kx else if (parameters->options().warn_shared_textrel()
9 kx && parameters->options().shared())
9 kx gold_warning(_("shared library text segment is not shareable"));
9 kx }
9 kx if (parameters->options().shared() && this->has_static_tls())
9 kx flags |= elfcpp::DF_STATIC_TLS;
9 kx if (parameters->options().origin())
9 kx flags |= elfcpp::DF_ORIGIN;
9 kx if (parameters->options().Bsymbolic()
9 kx && !parameters->options().have_dynamic_list())
9 kx {
9 kx flags |= elfcpp::DF_SYMBOLIC;
9 kx // Add DT_SYMBOLIC for compatibility with older loaders.
9 kx odyn->add_constant(elfcpp::DT_SYMBOLIC, 0);
9 kx }
9 kx if (parameters->options().now())
9 kx flags |= elfcpp::DF_BIND_NOW;
9 kx if (flags != 0)
9 kx odyn->add_constant(elfcpp::DT_FLAGS, flags);
9 kx
9 kx flags = 0;
9 kx if (parameters->options().global())
9 kx flags |= elfcpp::DF_1_GLOBAL;
9 kx if (parameters->options().initfirst())
9 kx flags |= elfcpp::DF_1_INITFIRST;
9 kx if (parameters->options().interpose())
9 kx flags |= elfcpp::DF_1_INTERPOSE;
9 kx if (parameters->options().loadfltr())
9 kx flags |= elfcpp::DF_1_LOADFLTR;
9 kx if (parameters->options().nodefaultlib())
9 kx flags |= elfcpp::DF_1_NODEFLIB;
9 kx if (parameters->options().nodelete())
9 kx flags |= elfcpp::DF_1_NODELETE;
9 kx if (parameters->options().nodlopen())
9 kx flags |= elfcpp::DF_1_NOOPEN;
9 kx if (parameters->options().nodump())
9 kx flags |= elfcpp::DF_1_NODUMP;
9 kx if (!parameters->options().shared())
9 kx flags &= ~(elfcpp::DF_1_INITFIRST
9 kx | elfcpp::DF_1_NODELETE
9 kx | elfcpp::DF_1_NOOPEN);
9 kx if (parameters->options().origin())
9 kx flags |= elfcpp::DF_1_ORIGIN;
9 kx if (parameters->options().now())
9 kx flags |= elfcpp::DF_1_NOW;
9 kx if (parameters->options().Bgroup())
9 kx flags |= elfcpp::DF_1_GROUP;
9 kx if (parameters->options().pie())
9 kx flags |= elfcpp::DF_1_PIE;
9 kx if (flags != 0)
9 kx odyn->add_constant(elfcpp::DT_FLAGS_1, flags);
9 kx
9 kx flags = 0;
9 kx if (parameters->options().unique())
9 kx flags |= elfcpp::DF_GNU_1_UNIQUE;
9 kx if (flags != 0)
9 kx odyn->add_constant(elfcpp::DT_GNU_FLAGS_1, flags);
9 kx }
9 kx
9 kx // Set the size of the _DYNAMIC symbol table to be the size of the
9 kx // dynamic data.
9 kx
9 kx void
9 kx Layout::set_dynamic_symbol_size(const Symbol_table* symtab)
9 kx {
9 kx Output_data_dynamic* const odyn = this->dynamic_data_;
9 kx if (odyn == NULL)
9 kx return;
9 kx odyn->finalize_data_size();
9 kx if (this->dynamic_symbol_ == NULL)
9 kx return;
9 kx off_t data_size = odyn->data_size();
9 kx const int size = parameters->target().get_size();
9 kx if (size == 32)
9 kx symtab->get_sized_symbol<32>(this->dynamic_symbol_)->set_symsize(data_size);
9 kx else if (size == 64)
9 kx symtab->get_sized_symbol<64>(this->dynamic_symbol_)->set_symsize(data_size);
9 kx else
9 kx gold_unreachable();
9 kx }
9 kx
9 kx // The mapping of input section name prefixes to output section names.
9 kx // In some cases one prefix is itself a prefix of another prefix; in
9 kx // such a case the longer prefix must come first. These prefixes are
9 kx // based on the GNU linker default ELF linker script.
9 kx
9 kx #define MAPPING_INIT(f, t) { f, sizeof(f) - 1, t, sizeof(t) - 1 }
9 kx #define MAPPING_INIT_EXACT(f, t) { f, 0, t, sizeof(t) - 1 }
9 kx const Layout::Section_name_mapping Layout::section_name_mapping[] =
9 kx {
9 kx MAPPING_INIT(".text.", ".text"),
9 kx MAPPING_INIT(".rodata.", ".rodata"),
9 kx MAPPING_INIT(".data.rel.ro.local.", ".data.rel.ro.local"),
9 kx MAPPING_INIT_EXACT(".data.rel.ro.local", ".data.rel.ro.local"),
9 kx MAPPING_INIT(".data.rel.ro.", ".data.rel.ro"),
9 kx MAPPING_INIT_EXACT(".data.rel.ro", ".data.rel.ro"),
9 kx MAPPING_INIT(".data.", ".data"),
9 kx MAPPING_INIT(".bss.", ".bss"),
9 kx MAPPING_INIT(".tdata.", ".tdata"),
9 kx MAPPING_INIT(".tbss.", ".tbss"),
9 kx MAPPING_INIT(".init_array.", ".init_array"),
9 kx MAPPING_INIT(".fini_array.", ".fini_array"),
9 kx MAPPING_INIT(".sdata.", ".sdata"),
9 kx MAPPING_INIT(".sbss.", ".sbss"),
9 kx // FIXME: In the GNU linker, .sbss2 and .sdata2 are handled
9 kx // differently depending on whether it is creating a shared library.
9 kx MAPPING_INIT(".sdata2.", ".sdata"),
9 kx MAPPING_INIT(".sbss2.", ".sbss"),
9 kx MAPPING_INIT(".lrodata.", ".lrodata"),
9 kx MAPPING_INIT(".ldata.", ".ldata"),
9 kx MAPPING_INIT(".lbss.", ".lbss"),
9 kx MAPPING_INIT(".gcc_except_table.", ".gcc_except_table"),
9 kx MAPPING_INIT(".gnu.linkonce.d.rel.ro.local.", ".data.rel.ro.local"),
9 kx MAPPING_INIT(".gnu.linkonce.d.rel.ro.", ".data.rel.ro"),
9 kx MAPPING_INIT(".gnu.linkonce.t.", ".text"),
9 kx MAPPING_INIT(".gnu.linkonce.r.", ".rodata"),
9 kx MAPPING_INIT(".gnu.linkonce.d.", ".data"),
9 kx MAPPING_INIT(".gnu.linkonce.b.", ".bss"),
9 kx MAPPING_INIT(".gnu.linkonce.s.", ".sdata"),
9 kx MAPPING_INIT(".gnu.linkonce.sb.", ".sbss"),
9 kx MAPPING_INIT(".gnu.linkonce.s2.", ".sdata"),
9 kx MAPPING_INIT(".gnu.linkonce.sb2.", ".sbss"),
9 kx MAPPING_INIT(".gnu.linkonce.wi.", ".debug_info"),
9 kx MAPPING_INIT(".gnu.linkonce.td.", ".tdata"),
9 kx MAPPING_INIT(".gnu.linkonce.tb.", ".tbss"),
9 kx MAPPING_INIT(".gnu.linkonce.lr.", ".lrodata"),
9 kx MAPPING_INIT(".gnu.linkonce.l.", ".ldata"),
9 kx MAPPING_INIT(".gnu.linkonce.lb.", ".lbss"),
9 kx MAPPING_INIT(".ARM.extab", ".ARM.extab"),
9 kx MAPPING_INIT(".gnu.linkonce.armextab.", ".ARM.extab"),
9 kx MAPPING_INIT(".ARM.exidx", ".ARM.exidx"),
9 kx MAPPING_INIT(".gnu.linkonce.armexidx.", ".ARM.exidx"),
9 kx MAPPING_INIT(".gnu.build.attributes.", ".gnu.build.attributes"),
9 kx };
9 kx
9 kx // Mapping for ".text" section prefixes with -z,keep-text-section-prefix.
9 kx const Layout::Section_name_mapping Layout::text_section_name_mapping[] =
9 kx {
9 kx MAPPING_INIT(".text.hot.", ".text.hot"),
9 kx MAPPING_INIT_EXACT(".text.hot", ".text.hot"),
9 kx MAPPING_INIT(".text.unlikely.", ".text.unlikely"),
9 kx MAPPING_INIT_EXACT(".text.unlikely", ".text.unlikely"),
9 kx MAPPING_INIT(".text.startup.", ".text.startup"),
9 kx MAPPING_INIT_EXACT(".text.startup", ".text.startup"),
9 kx MAPPING_INIT(".text.exit.", ".text.exit"),
9 kx MAPPING_INIT_EXACT(".text.exit", ".text.exit"),
9 kx MAPPING_INIT(".text.", ".text"),
9 kx };
9 kx #undef MAPPING_INIT
9 kx #undef MAPPING_INIT_EXACT
9 kx
9 kx const int Layout::section_name_mapping_count =
9 kx (sizeof(Layout::section_name_mapping)
9 kx / sizeof(Layout::section_name_mapping[0]));
9 kx
9 kx const int Layout::text_section_name_mapping_count =
9 kx (sizeof(Layout::text_section_name_mapping)
9 kx / sizeof(Layout::text_section_name_mapping[0]));
9 kx
9 kx // Find section name NAME in PSNM and return the mapped name if found
9 kx // with the length set in PLEN.
9 kx const char *
9 kx Layout::match_section_name(const Layout::Section_name_mapping* psnm,
9 kx const int count,
9 kx const char* name, size_t* plen)
9 kx {
9 kx for (int i = 0; i < count; ++i, ++psnm)
9 kx {
9 kx if (psnm->fromlen > 0)
9 kx {
9 kx if (strncmp(name, psnm->from, psnm->fromlen) == 0)
9 kx {
9 kx *plen = psnm->tolen;
9 kx return psnm->to;
9 kx }
9 kx }
9 kx else
9 kx {
9 kx if (strcmp(name, psnm->from) == 0)
9 kx {
9 kx *plen = psnm->tolen;
9 kx return psnm->to;
9 kx }
9 kx }
9 kx }
9 kx return NULL;
9 kx }
9 kx
9 kx // Choose the output section name to use given an input section name.
9 kx // Set *PLEN to the length of the name. *PLEN is initialized to the
9 kx // length of NAME.
9 kx
9 kx const char*
9 kx Layout::output_section_name(const Relobj* relobj, const char* name,
9 kx size_t* plen)
9 kx {
9 kx // gcc 4.3 generates the following sorts of section names when it
9 kx // needs a section name specific to a function:
9 kx // .text.FN
9 kx // .rodata.FN
9 kx // .sdata2.FN
9 kx // .data.FN
9 kx // .data.rel.FN
9 kx // .data.rel.local.FN
9 kx // .data.rel.ro.FN
9 kx // .data.rel.ro.local.FN
9 kx // .sdata.FN
9 kx // .bss.FN
9 kx // .sbss.FN
9 kx // .tdata.FN
9 kx // .tbss.FN
9 kx
9 kx // The GNU linker maps all of those to the part before the .FN,
9 kx // except that .data.rel.local.FN is mapped to .data, and
9 kx // .data.rel.ro.local.FN is mapped to .data.rel.ro. The sections
9 kx // beginning with .data.rel.ro.local are grouped together.
9 kx
9 kx // For an anonymous namespace, the string FN can contain a '.'.
9 kx
9 kx // Also of interest: .rodata.strN.N, .rodata.cstN, both of which the
9 kx // GNU linker maps to .rodata.
9 kx
9 kx // The .data.rel.ro sections are used with -z relro. The sections
9 kx // are recognized by name. We use the same names that the GNU
9 kx // linker does for these sections.
9 kx
9 kx // It is hard to handle this in a principled way, so we don't even
9 kx // try. We use a table of mappings. If the input section name is
9 kx // not found in the table, we simply use it as the output section
9 kx // name.
9 kx
9 kx if (parameters->options().keep_text_section_prefix()
9 kx && is_prefix_of(".text", name))
9 kx {
9 kx const char* match = match_section_name(text_section_name_mapping,
9 kx text_section_name_mapping_count,
9 kx name, plen);
9 kx if (match != NULL)
9 kx return match;
9 kx }
9 kx
9 kx const char* match = match_section_name(section_name_mapping,
9 kx section_name_mapping_count, name, plen);
9 kx if (match != NULL)
9 kx return match;
9 kx
9 kx // As an additional complication, .ctors sections are output in
9 kx // either .ctors or .init_array sections, and .dtors sections are
9 kx // output in either .dtors or .fini_array sections.
9 kx if (is_prefix_of(".ctors.", name) || is_prefix_of(".dtors.", name))
9 kx {
9 kx if (parameters->options().ctors_in_init_array())
9 kx {
9 kx *plen = 11;
9 kx return name[1] == 'c' ? ".init_array" : ".fini_array";
9 kx }
9 kx else
9 kx {
9 kx *plen = 6;
9 kx return name[1] == 'c' ? ".ctors" : ".dtors";
9 kx }
9 kx }
9 kx if (parameters->options().ctors_in_init_array()
9 kx && (strcmp(name, ".ctors") == 0 || strcmp(name, ".dtors") == 0))
9 kx {
9 kx // To make .init_array/.fini_array work with gcc we must exclude
9 kx // .ctors and .dtors sections from the crtbegin and crtend
9 kx // files.
9 kx if (relobj == NULL
9 kx || (!Layout::match_file_name(relobj, "crtbegin")
9 kx && !Layout::match_file_name(relobj, "crtend")))
9 kx {
9 kx *plen = 11;
9 kx return name[1] == 'c' ? ".init_array" : ".fini_array";
9 kx }
9 kx }
9 kx
9 kx return name;
9 kx }
9 kx
9 kx // Return true if RELOBJ is an input file whose base name matches
9 kx // FILE_NAME. The base name must have an extension of ".o", and must
9 kx // be exactly FILE_NAME.o or FILE_NAME, one character, ".o". This is
9 kx // to match crtbegin.o as well as crtbeginS.o without getting confused
9 kx // by other possibilities. Overall matching the file name this way is
9 kx // a dreadful hack, but the GNU linker does it in order to better
9 kx // support gcc, and we need to be compatible.
9 kx
9 kx bool
9 kx Layout::match_file_name(const Relobj* relobj, const char* match)
9 kx {
9 kx const std::string& file_name(relobj->name());
9 kx const char* base_name = lbasename(file_name.c_str());
9 kx size_t match_len = strlen(match);
9 kx if (strncmp(base_name, match, match_len) != 0)
9 kx return false;
9 kx size_t base_len = strlen(base_name);
9 kx if (base_len != match_len + 2 && base_len != match_len + 3)
9 kx return false;
9 kx return memcmp(base_name + base_len - 2, ".o", 2) == 0;
9 kx }
9 kx
9 kx // Check if a comdat group or .gnu.linkonce section with the given
9 kx // NAME is selected for the link. If there is already a section,
9 kx // *KEPT_SECTION is set to point to the existing section and the
9 kx // function returns false. Otherwise, OBJECT, SHNDX, IS_COMDAT, and
9 kx // IS_GROUP_NAME are recorded for this NAME in the layout object,
9 kx // *KEPT_SECTION is set to the internal copy and the function returns
9 kx // true.
9 kx
9 kx bool
9 kx Layout::find_or_add_kept_section(const std::string& name,
9 kx Relobj* object,
9 kx unsigned int shndx,
9 kx bool is_comdat,
9 kx bool is_group_name,
9 kx Kept_section** kept_section)
9 kx {
9 kx // It's normal to see a couple of entries here, for the x86 thunk
9 kx // sections. If we see more than a few, we're linking a C++
9 kx // program, and we resize to get more space to minimize rehashing.
9 kx if (this->signatures_.size() > 4
9 kx && !this->resized_signatures_)
9 kx {
9 kx reserve_unordered_map(&this->signatures_,
9 kx this->number_of_input_files_ * 64);
9 kx this->resized_signatures_ = true;
9 kx }
9 kx
9 kx Kept_section candidate;
9 kx std::pair<Signatures::iterator, bool> ins =
9 kx this->signatures_.insert(std::make_pair(name, candidate));
9 kx
9 kx if (kept_section != NULL)
9 kx *kept_section = &ins.first->second;
9 kx if (ins.second)
9 kx {
9 kx // This is the first time we've seen this signature.
9 kx ins.first->second.set_object(object);
9 kx ins.first->second.set_shndx(shndx);
9 kx if (is_comdat)
9 kx ins.first->second.set_is_comdat();
9 kx if (is_group_name)
9 kx ins.first->second.set_is_group_name();
9 kx return true;
9 kx }
9 kx
9 kx // We have already seen this signature.
9 kx
9 kx if (ins.first->second.is_group_name())
9 kx {
9 kx // We've already seen a real section group with this signature.
9 kx // If the kept group is from a plugin object, and we're in the
9 kx // replacement phase, accept the new one as a replacement.
9 kx if (ins.first->second.object() == NULL
9 kx && parameters->options().plugins()->in_replacement_phase())
9 kx {
9 kx ins.first->second.set_object(object);
9 kx ins.first->second.set_shndx(shndx);
9 kx return true;
9 kx }
9 kx return false;
9 kx }
9 kx else if (is_group_name)
9 kx {
9 kx // This is a real section group, and we've already seen a
9 kx // linkonce section with this signature. Record that we've seen
9 kx // a section group, and don't include this section group.
9 kx ins.first->second.set_is_group_name();
9 kx return false;
9 kx }
9 kx else
9 kx {
9 kx // We've already seen a linkonce section and this is a linkonce
9 kx // section. These don't block each other--this may be the same
9 kx // symbol name with different section types.
9 kx return true;
9 kx }
9 kx }
9 kx
9 kx // Store the allocated sections into the section list.
9 kx
9 kx void
9 kx Layout::get_allocated_sections(Section_list* section_list) const
9 kx {
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx if (((*p)->flags() & elfcpp::SHF_ALLOC) != 0)
9 kx section_list->push_back(*p);
9 kx }
9 kx
9 kx // Store the executable sections into the section list.
9 kx
9 kx void
9 kx Layout::get_executable_sections(Section_list* section_list) const
9 kx {
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx if (((*p)->flags() & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
9 kx == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
9 kx section_list->push_back(*p);
9 kx }
9 kx
9 kx // Create an output segment.
9 kx
9 kx Output_segment*
9 kx Layout::make_output_segment(elfcpp::Elf_Word type, elfcpp::Elf_Word flags)
9 kx {
9 kx gold_assert(!parameters->options().relocatable());
9 kx Output_segment* oseg = new Output_segment(type, flags);
9 kx this->segment_list_.push_back(oseg);
9 kx
9 kx if (type == elfcpp::PT_TLS)
9 kx this->tls_segment_ = oseg;
9 kx else if (type == elfcpp::PT_GNU_RELRO)
9 kx this->relro_segment_ = oseg;
9 kx else if (type == elfcpp::PT_INTERP)
9 kx this->interp_segment_ = oseg;
9 kx
9 kx return oseg;
9 kx }
9 kx
9 kx // Return the file offset of the normal symbol table.
9 kx
9 kx off_t
9 kx Layout::symtab_section_offset() const
9 kx {
9 kx if (this->symtab_section_ != NULL)
9 kx return this->symtab_section_->offset();
9 kx return 0;
9 kx }
9 kx
9 kx // Return the section index of the normal symbol table. It may have
9 kx // been stripped by the -s/--strip-all option.
9 kx
9 kx unsigned int
9 kx Layout::symtab_section_shndx() const
9 kx {
9 kx if (this->symtab_section_ != NULL)
9 kx return this->symtab_section_->out_shndx();
9 kx return 0;
9 kx }
9 kx
9 kx // Write out the Output_sections. Most won't have anything to write,
9 kx // since most of the data will come from input sections which are
9 kx // handled elsewhere. But some Output_sections do have Output_data.
9 kx
9 kx void
9 kx Layout::write_output_sections(Output_file* of) const
9 kx {
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if (!(*p)->after_input_sections())
9 kx (*p)->write(of);
9 kx }
9 kx }
9 kx
9 kx // Write out data not associated with a section or the symbol table.
9 kx
9 kx void
9 kx Layout::write_data(const Symbol_table* symtab, Output_file* of) const
9 kx {
9 kx if (!parameters->options().strip_all())
9 kx {
9 kx const Output_section* symtab_section = this->symtab_section_;
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->needs_symtab_index())
9 kx {
9 kx gold_assert(symtab_section != NULL);
9 kx unsigned int index = (*p)->symtab_index();
9 kx gold_assert(index > 0 && index != -1U);
9 kx off_t off = (symtab_section->offset()
9 kx + index * symtab_section->entsize());
9 kx symtab->write_section_symbol(*p, this->symtab_xindex_, of, off);
9 kx }
9 kx }
9 kx }
9 kx
9 kx const Output_section* dynsym_section = this->dynsym_section_;
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->needs_dynsym_index())
9 kx {
9 kx gold_assert(dynsym_section != NULL);
9 kx unsigned int index = (*p)->dynsym_index();
9 kx gold_assert(index > 0 && index != -1U);
9 kx off_t off = (dynsym_section->offset()
9 kx + index * dynsym_section->entsize());
9 kx symtab->write_section_symbol(*p, this->dynsym_xindex_, of, off);
9 kx }
9 kx }
9 kx
9 kx // Write out the Output_data which are not in an Output_section.
9 kx for (Data_list::const_iterator p = this->special_output_list_.begin();
9 kx p != this->special_output_list_.end();
9 kx ++p)
9 kx (*p)->write(of);
9 kx
9 kx // Write out the Output_data which are not in an Output_section
9 kx // and are regenerated in each iteration of relaxation.
9 kx for (Data_list::const_iterator p = this->relax_output_list_.begin();
9 kx p != this->relax_output_list_.end();
9 kx ++p)
9 kx (*p)->write(of);
9 kx }
9 kx
9 kx // Write out the Output_sections which can only be written after the
9 kx // input sections are complete.
9 kx
9 kx void
9 kx Layout::write_sections_after_input_sections(Output_file* of)
9 kx {
9 kx // Determine the final section offsets, and thus the final output
9 kx // file size. Note we finalize the .shstrab last, to allow the
9 kx // after_input_section sections to modify their section-names before
9 kx // writing.
9 kx if (this->any_postprocessing_sections_)
9 kx {
9 kx off_t off = this->output_file_size_;
9 kx off = this->set_section_offsets(off, POSTPROCESSING_SECTIONS_PASS);
9 kx
9 kx // Now that we've finalized the names, we can finalize the shstrab.
9 kx off =
9 kx this->set_section_offsets(off,
9 kx STRTAB_AFTER_POSTPROCESSING_SECTIONS_PASS);
9 kx
9 kx if (off > this->output_file_size_)
9 kx {
9 kx of->resize(off);
9 kx this->output_file_size_ = off;
9 kx }
9 kx }
9 kx
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->after_input_sections())
9 kx (*p)->write(of);
9 kx }
9 kx
9 kx this->section_headers_->write(of);
9 kx }
9 kx
9 kx // If a tree-style build ID was requested, the parallel part of that computation
9 kx // is already done, and the final hash-of-hashes is computed here. For other
9 kx // types of build IDs, all the work is done here.
9 kx
9 kx void
9 kx Layout::write_build_id(Output_file* of, unsigned char* array_of_hashes,
9 kx size_t size_of_hashes) const
9 kx {
9 kx if (this->build_id_note_ == NULL)
9 kx return;
9 kx
9 kx unsigned char* ov = of->get_output_view(this->build_id_note_->offset(),
9 kx this->build_id_note_->data_size());
9 kx
9 kx if (array_of_hashes == NULL)
9 kx {
9 kx const size_t output_file_size = this->output_file_size();
9 kx const unsigned char* iv = of->get_input_view(0, output_file_size);
9 kx const char* style = parameters->options().build_id();
9 kx
9 kx // If we get here with style == "tree" then the output must be
9 kx // too small for chunking, and we use SHA-1 in that case.
9 kx if ((strcmp(style, "sha1") == 0) || (strcmp(style, "tree") == 0))
9 kx sha1_buffer(reinterpret_cast<const char*>(iv), output_file_size, ov);
9 kx else if (strcmp(style, "md5") == 0)
9 kx md5_buffer(reinterpret_cast<const char*>(iv), output_file_size, ov);
9 kx else
9 kx gold_unreachable();
9 kx
9 kx of->free_input_view(0, output_file_size, iv);
9 kx }
9 kx else
9 kx {
9 kx // Non-overlapping substrings of the output file have been hashed.
9 kx // Compute SHA-1 hash of the hashes.
9 kx sha1_buffer(reinterpret_cast<const char*>(array_of_hashes),
9 kx size_of_hashes, ov);
9 kx delete[] array_of_hashes;
9 kx }
9 kx
9 kx of->write_output_view(this->build_id_note_->offset(),
9 kx this->build_id_note_->data_size(),
9 kx ov);
9 kx }
9 kx
9 kx // Write out a binary file. This is called after the link is
9 kx // complete. IN is the temporary output file we used to generate the
9 kx // ELF code. We simply walk through the segments, read them from
9 kx // their file offset in IN, and write them to their load address in
9 kx // the output file. FIXME: with a bit more work, we could support
9 kx // S-records and/or Intel hex format here.
9 kx
9 kx void
9 kx Layout::write_binary(Output_file* in) const
9 kx {
9 kx gold_assert(parameters->options().oformat_enum()
9 kx == General_options::OBJECT_FORMAT_BINARY);
9 kx
9 kx // Get the size of the binary file.
9 kx uint64_t max_load_address = 0;
9 kx for (Segment_list::const_iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
9 kx {
9 kx uint64_t max_paddr = (*p)->paddr() + (*p)->filesz();
9 kx if (max_paddr > max_load_address)
9 kx max_load_address = max_paddr;
9 kx }
9 kx }
9 kx
9 kx Output_file out(parameters->options().output_file_name());
9 kx out.open(max_load_address);
9 kx
9 kx for (Segment_list::const_iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx {
9 kx if ((*p)->type() == elfcpp::PT_LOAD && (*p)->filesz() > 0)
9 kx {
9 kx const unsigned char* vin = in->get_input_view((*p)->offset(),
9 kx (*p)->filesz());
9 kx unsigned char* vout = out.get_output_view((*p)->paddr(),
9 kx (*p)->filesz());
9 kx memcpy(vout, vin, (*p)->filesz());
9 kx out.write_output_view((*p)->paddr(), (*p)->filesz(), vout);
9 kx in->free_input_view((*p)->offset(), (*p)->filesz(), vin);
9 kx }
9 kx }
9 kx
9 kx out.close();
9 kx }
9 kx
9 kx // Print the output sections to the map file.
9 kx
9 kx void
9 kx Layout::print_to_mapfile(Mapfile* mapfile) const
9 kx {
9 kx for (Segment_list::const_iterator p = this->segment_list_.begin();
9 kx p != this->segment_list_.end();
9 kx ++p)
9 kx (*p)->print_sections_to_mapfile(mapfile);
9 kx for (Section_list::const_iterator p = this->unattached_section_list_.begin();
9 kx p != this->unattached_section_list_.end();
9 kx ++p)
9 kx (*p)->print_to_mapfile(mapfile);
9 kx }
9 kx
9 kx // Print statistical information to stderr. This is used for --stats.
9 kx
9 kx void
9 kx Layout::print_stats() const
9 kx {
9 kx this->namepool_.print_stats("section name pool");
9 kx this->sympool_.print_stats("output symbol name pool");
9 kx this->dynpool_.print_stats("dynamic name pool");
9 kx
9 kx for (Section_list::const_iterator p = this->section_list_.begin();
9 kx p != this->section_list_.end();
9 kx ++p)
9 kx (*p)->print_merge_stats();
9 kx }
9 kx
9 kx // Write_sections_task methods.
9 kx
9 kx // We can always run this task.
9 kx
9 kx Task_token*
9 kx Write_sections_task::is_runnable()
9 kx {
9 kx return NULL;
9 kx }
9 kx
9 kx // We need to unlock both OUTPUT_SECTIONS_BLOCKER and FINAL_BLOCKER
9 kx // when finished.
9 kx
9 kx void
9 kx Write_sections_task::locks(Task_locker* tl)
9 kx {
9 kx tl->add(this, this->output_sections_blocker_);
9 kx if (this->input_sections_blocker_ != NULL)
9 kx tl->add(this, this->input_sections_blocker_);
9 kx tl->add(this, this->final_blocker_);
9 kx }
9 kx
9 kx // Run the task--write out the data.
9 kx
9 kx void
9 kx Write_sections_task::run(Workqueue*)
9 kx {
9 kx this->layout_->write_output_sections(this->of_);
9 kx }
9 kx
9 kx // Write_data_task methods.
9 kx
9 kx // We can always run this task.
9 kx
9 kx Task_token*
9 kx Write_data_task::is_runnable()
9 kx {
9 kx return NULL;
9 kx }
9 kx
9 kx // We need to unlock FINAL_BLOCKER when finished.
9 kx
9 kx void
9 kx Write_data_task::locks(Task_locker* tl)
9 kx {
9 kx tl->add(this, this->final_blocker_);
9 kx }
9 kx
9 kx // Run the task--write out the data.
9 kx
9 kx void
9 kx Write_data_task::run(Workqueue*)
9 kx {
9 kx this->layout_->write_data(this->symtab_, this->of_);
9 kx }
9 kx
9 kx // Write_symbols_task methods.
9 kx
9 kx // We can always run this task.
9 kx
9 kx Task_token*
9 kx Write_symbols_task::is_runnable()
9 kx {
9 kx return NULL;
9 kx }
9 kx
9 kx // We need to unlock FINAL_BLOCKER when finished.
9 kx
9 kx void
9 kx Write_symbols_task::locks(Task_locker* tl)
9 kx {
9 kx tl->add(this, this->final_blocker_);
9 kx }
9 kx
9 kx // Run the task--write out the symbols.
9 kx
9 kx void
9 kx Write_symbols_task::run(Workqueue*)
9 kx {
9 kx this->symtab_->write_globals(this->sympool_, this->dynpool_,
9 kx this->layout_->symtab_xindex(),
9 kx this->layout_->dynsym_xindex(), this->of_);
9 kx }
9 kx
9 kx // Write_after_input_sections_task methods.
9 kx
9 kx // We can only run this task after the input sections have completed.
9 kx
9 kx Task_token*
9 kx Write_after_input_sections_task::is_runnable()
9 kx {
9 kx if (this->input_sections_blocker_->is_blocked())
9 kx return this->input_sections_blocker_;
9 kx return NULL;
9 kx }
9 kx
9 kx // We need to unlock FINAL_BLOCKER when finished.
9 kx
9 kx void
9 kx Write_after_input_sections_task::locks(Task_locker* tl)
9 kx {
9 kx tl->add(this, this->final_blocker_);
9 kx }
9 kx
9 kx // Run the task.
9 kx
9 kx void
9 kx Write_after_input_sections_task::run(Workqueue*)
9 kx {
9 kx this->layout_->write_sections_after_input_sections(this->of_);
9 kx }
9 kx
9 kx // Build IDs can be computed as a "flat" sha1 or md5 of a string of bytes,
9 kx // or as a "tree" where each chunk of the string is hashed and then those
9 kx // hashes are put into a (much smaller) string which is hashed with sha1.
9 kx // We compute a checksum over the entire file because that is simplest.
9 kx
9 kx void
9 kx Build_id_task_runner::run(Workqueue* workqueue, const Task*)
9 kx {
9 kx Task_token* post_hash_tasks_blocker = new Task_token(true);
9 kx const Layout* layout = this->layout_;
9 kx Output_file* of = this->of_;
9 kx const size_t filesize = (layout->output_file_size() <= 0 ? 0
9 kx : static_cast<size_t>(layout->output_file_size()));
9 kx unsigned char* array_of_hashes = NULL;
9 kx size_t size_of_hashes = 0;
9 kx
9 kx if (strcmp(this->options_->build_id(), "tree") == 0
9 kx && this->options_->build_id_chunk_size_for_treehash() > 0
9 kx && filesize > 0
9 kx && (filesize >= this->options_->build_id_min_file_size_for_treehash()))
9 kx {
9 kx static const size_t MD5_OUTPUT_SIZE_IN_BYTES = 16;
9 kx const size_t chunk_size =
9 kx this->options_->build_id_chunk_size_for_treehash();
9 kx const size_t num_hashes = ((filesize - 1) / chunk_size) + 1;
9 kx post_hash_tasks_blocker->add_blockers(num_hashes);
9 kx size_of_hashes = num_hashes * MD5_OUTPUT_SIZE_IN_BYTES;
9 kx array_of_hashes = new unsigned char[size_of_hashes];
9 kx unsigned char *dst = array_of_hashes;
9 kx for (size_t i = 0, src_offset = 0; i < num_hashes;
9 kx i++, dst += MD5_OUTPUT_SIZE_IN_BYTES, src_offset += chunk_size)
9 kx {
9 kx size_t size = std::min(chunk_size, filesize - src_offset);
9 kx workqueue->queue(new Hash_task(of,
9 kx src_offset,
9 kx size,
9 kx dst,
9 kx post_hash_tasks_blocker));
9 kx }
9 kx }
9 kx
9 kx // Queue the final task to write the build id and close the output file.
9 kx workqueue->queue(new Task_function(new Close_task_runner(this->options_,
9 kx layout,
9 kx of,
9 kx array_of_hashes,
9 kx size_of_hashes),
9 kx post_hash_tasks_blocker,
9 kx "Task_function Close_task_runner"));
9 kx }
9 kx
9 kx // Close_task_runner methods.
9 kx
9 kx // Finish up the build ID computation, if necessary, and write a binary file,
9 kx // if necessary. Then close the output file.
9 kx
9 kx void
9 kx Close_task_runner::run(Workqueue*, const Task*)
9 kx {
9 kx // At this point the multi-threaded part of the build ID computation,
9 kx // if any, is done. See Build_id_task_runner.
9 kx this->layout_->write_build_id(this->of_, this->array_of_hashes_,
9 kx this->size_of_hashes_);
9 kx
9 kx // If we've been asked to create a binary file, we do so here.
9 kx if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
9 kx this->layout_->write_binary(this->of_);
9 kx
9 kx if (this->options_->dependency_file())
9 kx File_read::write_dependency_file(this->options_->dependency_file(),
9 kx this->options_->output_file_name());
9 kx
9 kx this->of_->close();
9 kx }
9 kx
9 kx // Instantiate the templates we need. We could use the configure
9 kx // script to restrict this to only the ones for implemented targets.
9 kx
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx template
9 kx Output_section*
9 kx Layout::init_fixed_output_section<32, false>(
9 kx const char* name,
9 kx elfcpp::Shdr<32, false>& shdr);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx template
9 kx Output_section*
9 kx Layout::init_fixed_output_section<32, true>(
9 kx const char* name,
9 kx elfcpp::Shdr<32, true>& shdr);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx template
9 kx Output_section*
9 kx Layout::init_fixed_output_section<64, false>(
9 kx const char* name,
9 kx elfcpp::Shdr<64, false>& shdr);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx template
9 kx Output_section*
9 kx Layout::init_fixed_output_section<64, true>(
9 kx const char* name,
9 kx elfcpp::Shdr<64, true>& shdr);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx template
9 kx Output_section*
9 kx Layout::layout<32, false>(Sized_relobj_file<32, false>* object,
9 kx unsigned int shndx,
9 kx const char* name,
9 kx const elfcpp::Shdr<32, false>& shdr,
9 kx unsigned int, unsigned int, unsigned int, off_t*);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx template
9 kx Output_section*
9 kx Layout::layout<32, true>(Sized_relobj_file<32, true>* object,
9 kx unsigned int shndx,
9 kx const char* name,
9 kx const elfcpp::Shdr<32, true>& shdr,
9 kx unsigned int, unsigned int, unsigned int, off_t*);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx template
9 kx Output_section*
9 kx Layout::layout<64, false>(Sized_relobj_file<64, false>* object,
9 kx unsigned int shndx,
9 kx const char* name,
9 kx const elfcpp::Shdr<64, false>& shdr,
9 kx unsigned int, unsigned int, unsigned int, off_t*);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx template
9 kx Output_section*
9 kx Layout::layout<64, true>(Sized_relobj_file<64, true>* object,
9 kx unsigned int shndx,
9 kx const char* name,
9 kx const elfcpp::Shdr<64, true>& shdr,
9 kx unsigned int, unsigned int, unsigned int, off_t*);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx template
9 kx Output_section*
9 kx Layout::layout_reloc<32, false>(Sized_relobj_file<32, false>* object,
9 kx unsigned int reloc_shndx,
9 kx const elfcpp::Shdr<32, false>& shdr,
9 kx Output_section* data_section,
9 kx Relocatable_relocs* rr);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx template
9 kx Output_section*
9 kx Layout::layout_reloc<32, true>(Sized_relobj_file<32, true>* object,
9 kx unsigned int reloc_shndx,
9 kx const elfcpp::Shdr<32, true>& shdr,
9 kx Output_section* data_section,
9 kx Relocatable_relocs* rr);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx template
9 kx Output_section*
9 kx Layout::layout_reloc<64, false>(Sized_relobj_file<64, false>* object,
9 kx unsigned int reloc_shndx,
9 kx const elfcpp::Shdr<64, false>& shdr,
9 kx Output_section* data_section,
9 kx Relocatable_relocs* rr);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx template
9 kx Output_section*
9 kx Layout::layout_reloc<64, true>(Sized_relobj_file<64, true>* object,
9 kx unsigned int reloc_shndx,
9 kx const elfcpp::Shdr<64, true>& shdr,
9 kx Output_section* data_section,
9 kx Relocatable_relocs* rr);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx template
9 kx void
9 kx Layout::layout_group<32, false>(Symbol_table* symtab,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int,
9 kx const char* group_section_name,
9 kx const char* signature,
9 kx const elfcpp::Shdr<32, false>& shdr,
9 kx elfcpp::Elf_Word flags,
9 kx std::vector<unsigned int>* shndxes);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx template
9 kx void
9 kx Layout::layout_group<32, true>(Symbol_table* symtab,
9 kx Sized_relobj_file<32, true>* object,
9 kx unsigned int,
9 kx const char* group_section_name,
9 kx const char* signature,
9 kx const elfcpp::Shdr<32, true>& shdr,
9 kx elfcpp::Elf_Word flags,
9 kx std::vector<unsigned int>* shndxes);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx template
9 kx void
9 kx Layout::layout_group<64, false>(Symbol_table* symtab,
9 kx Sized_relobj_file<64, false>* object,
9 kx unsigned int,
9 kx const char* group_section_name,
9 kx const char* signature,
9 kx const elfcpp::Shdr<64, false>& shdr,
9 kx elfcpp::Elf_Word flags,
9 kx std::vector<unsigned int>* shndxes);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx template
9 kx void
9 kx Layout::layout_group<64, true>(Symbol_table* symtab,
9 kx Sized_relobj_file<64, true>* object,
9 kx unsigned int,
9 kx const char* group_section_name,
9 kx const char* signature,
9 kx const elfcpp::Shdr<64, true>& shdr,
9 kx elfcpp::Elf_Word flags,
9 kx std::vector<unsigned int>* shndxes);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx template
9 kx Output_section*
9 kx Layout::layout_eh_frame<32, false>(Sized_relobj_file<32, false>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx const unsigned char* symbol_names,
9 kx off_t symbol_names_size,
9 kx unsigned int shndx,
9 kx const elfcpp::Shdr<32, false>& shdr,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type,
9 kx off_t* off);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx template
9 kx Output_section*
9 kx Layout::layout_eh_frame<32, true>(Sized_relobj_file<32, true>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx const unsigned char* symbol_names,
9 kx off_t symbol_names_size,
9 kx unsigned int shndx,
9 kx const elfcpp::Shdr<32, true>& shdr,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type,
9 kx off_t* off);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx template
9 kx Output_section*
9 kx Layout::layout_eh_frame<64, false>(Sized_relobj_file<64, false>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx const unsigned char* symbol_names,
9 kx off_t symbol_names_size,
9 kx unsigned int shndx,
9 kx const elfcpp::Shdr<64, false>& shdr,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type,
9 kx off_t* off);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx template
9 kx Output_section*
9 kx Layout::layout_eh_frame<64, true>(Sized_relobj_file<64, true>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx const unsigned char* symbol_names,
9 kx off_t symbol_names_size,
9 kx unsigned int shndx,
9 kx const elfcpp::Shdr<64, true>& shdr,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type,
9 kx off_t* off);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_LITTLE
9 kx template
9 kx void
9 kx Layout::add_to_gdb_index(bool is_type_unit,
9 kx Sized_relobj<32, false>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx unsigned int shndx,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_32_BIG
9 kx template
9 kx void
9 kx Layout::add_to_gdb_index(bool is_type_unit,
9 kx Sized_relobj<32, true>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx unsigned int shndx,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_LITTLE
9 kx template
9 kx void
9 kx Layout::add_to_gdb_index(bool is_type_unit,
9 kx Sized_relobj<64, false>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx unsigned int shndx,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type);
9 kx #endif
9 kx
9 kx #ifdef HAVE_TARGET_64_BIG
9 kx template
9 kx void
9 kx Layout::add_to_gdb_index(bool is_type_unit,
9 kx Sized_relobj<64, true>* object,
9 kx const unsigned char* symbols,
9 kx off_t symbols_size,
9 kx unsigned int shndx,
9 kx unsigned int reloc_shndx,
9 kx unsigned int reloc_type);
9 kx #endif
9 kx
9 kx } // End namespace gold.