9 kx // i386.cc -- i386 target support 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 <cstring>
9 kx
9 kx #include "elfcpp.h"
9 kx #include "dwarf.h"
9 kx #include "parameters.h"
9 kx #include "reloc.h"
9 kx #include "i386.h"
9 kx #include "object.h"
9 kx #include "symtab.h"
9 kx #include "layout.h"
9 kx #include "output.h"
9 kx #include "copy-relocs.h"
9 kx #include "target.h"
9 kx #include "target-reloc.h"
9 kx #include "target-select.h"
9 kx #include "tls.h"
9 kx #include "freebsd.h"
9 kx #include "nacl.h"
9 kx #include "gc.h"
9 kx
9 kx namespace
9 kx {
9 kx
9 kx using namespace gold;
9 kx
9 kx // A class to handle the .got.plt section.
9 kx
9 kx class Output_data_got_plt_i386 : public Output_section_data_build
9 kx {
9 kx public:
9 kx Output_data_got_plt_i386(Layout* layout)
9 kx : Output_section_data_build(4),
9 kx layout_(layout)
9 kx { }
9 kx
9 kx protected:
9 kx // Write out the PLT data.
9 kx void
9 kx do_write(Output_file*);
9 kx
9 kx // Write to a map file.
9 kx void
9 kx do_print_to_mapfile(Mapfile* mapfile) const
9 kx { mapfile->print_output_data(this, "** GOT PLT"); }
9 kx
9 kx private:
9 kx // A pointer to the Layout class, so that we can find the .dynamic
9 kx // section when we write out the GOT PLT section.
9 kx Layout* layout_;
9 kx };
9 kx
9 kx // A class to handle the PLT data.
9 kx // This is an abstract base class that handles most of the linker details
9 kx // but does not know the actual contents of PLT entries. The derived
9 kx // classes below fill in those details.
9 kx
9 kx class Output_data_plt_i386 : public Output_section_data
9 kx {
9 kx public:
9 kx typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
9 kx
9 kx Output_data_plt_i386(Layout*, uint64_t addralign,
9 kx Output_data_got_plt_i386*, Output_data_space*);
9 kx
9 kx // Add an entry to the PLT.
9 kx void
9 kx add_entry(Symbol_table*, Layout*, Symbol* gsym);
9 kx
9 kx // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
9 kx unsigned int
9 kx add_local_ifunc_entry(Symbol_table*, Layout*,
9 kx Sized_relobj_file<32, false>* relobj,
9 kx unsigned int local_sym_index);
9 kx
9 kx // Return the .rel.plt section data.
9 kx Reloc_section*
9 kx rel_plt() const
9 kx { return this->rel_; }
9 kx
9 kx // Return where the TLS_DESC relocations should go.
9 kx Reloc_section*
9 kx rel_tls_desc(Layout*);
9 kx
9 kx // Return where the IRELATIVE relocations should go.
9 kx Reloc_section*
9 kx rel_irelative(Symbol_table*, Layout*);
9 kx
9 kx // Return whether we created a section for IRELATIVE relocations.
9 kx bool
9 kx has_irelative_section() const
9 kx { return this->irelative_rel_ != NULL; }
9 kx
9 kx // Return the number of PLT entries.
9 kx unsigned int
9 kx entry_count() const
9 kx { return this->count_ + this->irelative_count_; }
9 kx
9 kx // Return the offset of the first non-reserved PLT entry.
9 kx unsigned int
9 kx first_plt_entry_offset()
9 kx { return this->get_plt_entry_size(); }
9 kx
9 kx // Return the size of a PLT entry.
9 kx unsigned int
9 kx get_plt_entry_size() const
9 kx { return this->do_get_plt_entry_size(); }
9 kx
9 kx // Return the PLT address to use for a global symbol.
9 kx uint64_t
9 kx address_for_global(const Symbol*);
9 kx
9 kx // Return the PLT address to use for a local symbol.
9 kx uint64_t
9 kx address_for_local(const Relobj*, unsigned int symndx);
9 kx
9 kx // Add .eh_frame information for the PLT.
9 kx void
9 kx add_eh_frame(Layout* layout)
9 kx { this->do_add_eh_frame(layout); }
9 kx
9 kx protected:
9 kx // Fill the first PLT entry, given the pointer to the PLT section data
9 kx // and the runtime address of the GOT.
9 kx void
9 kx fill_first_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address)
9 kx { this->do_fill_first_plt_entry(pov, got_address); }
9 kx
9 kx // Fill a normal PLT entry, given the pointer to the entry's data in the
9 kx // section, the runtime address of the GOT, the offset into the GOT of
9 kx // the corresponding slot, the offset into the relocation section of the
9 kx // corresponding reloc, and the offset of this entry within the whole
9 kx // PLT. Return the offset from this PLT entry's runtime address that
9 kx // should be used to compute the initial value of the GOT slot.
9 kx unsigned int
9 kx fill_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset)
9 kx {
9 kx return this->do_fill_plt_entry(pov, got_address, got_offset,
9 kx plt_offset, plt_rel_offset);
9 kx }
9 kx
9 kx virtual unsigned int
9 kx do_get_plt_entry_size() const = 0;
9 kx
9 kx virtual void
9 kx do_fill_first_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address) = 0;
9 kx
9 kx virtual unsigned int
9 kx do_fill_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset) = 0;
9 kx
9 kx virtual void
9 kx do_add_eh_frame(Layout*) = 0;
9 kx
9 kx void
9 kx do_adjust_output_section(Output_section* os);
9 kx
9 kx // Write to a map file.
9 kx void
9 kx do_print_to_mapfile(Mapfile* mapfile) const
9 kx { mapfile->print_output_data(this, _("** PLT")); }
9 kx
9 kx // The .eh_frame unwind information for the PLT.
9 kx // The CIE is common across variants of the PLT format.
9 kx static const int plt_eh_frame_cie_size = 16;
9 kx static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
9 kx
9 kx private:
9 kx // Set the final size.
9 kx void
9 kx set_final_data_size()
9 kx {
9 kx this->set_data_size((this->count_ + this->irelative_count_ + 1)
9 kx * this->get_plt_entry_size());
9 kx }
9 kx
9 kx // Write out the PLT data.
9 kx void
9 kx do_write(Output_file*);
9 kx
9 kx // We keep a list of global STT_GNU_IFUNC symbols, each with its
9 kx // offset in the GOT.
9 kx struct Global_ifunc
9 kx {
9 kx Symbol* sym;
9 kx unsigned int got_offset;
9 kx };
9 kx
9 kx // We keep a list of local STT_GNU_IFUNC symbols, each with its
9 kx // offset in the GOT.
9 kx struct Local_ifunc
9 kx {
9 kx Sized_relobj_file<32, false>* object;
9 kx unsigned int local_sym_index;
9 kx unsigned int got_offset;
9 kx };
9 kx
9 kx // The reloc section.
9 kx Reloc_section* rel_;
9 kx // The TLS_DESC relocations, if necessary. These must follow the
9 kx // regular PLT relocs.
9 kx Reloc_section* tls_desc_rel_;
9 kx // The IRELATIVE relocations, if necessary. These must follow the
9 kx // regular relocatoins and the TLS_DESC relocations.
9 kx Reloc_section* irelative_rel_;
9 kx // The .got.plt section.
9 kx Output_data_got_plt_i386* got_plt_;
9 kx // The part of the .got.plt section used for IRELATIVE relocs.
9 kx Output_data_space* got_irelative_;
9 kx // The number of PLT entries.
9 kx unsigned int count_;
9 kx // Number of PLT entries with R_386_IRELATIVE relocs. These follow
9 kx // the regular PLT entries.
9 kx unsigned int irelative_count_;
9 kx // Global STT_GNU_IFUNC symbols.
9 kx std::vector<Global_ifunc> global_ifuncs_;
9 kx // Local STT_GNU_IFUNC symbols.
9 kx std::vector<Local_ifunc> local_ifuncs_;
9 kx };
9 kx
9 kx // This is an abstract class for the standard PLT layout.
9 kx // The derived classes below handle the actual PLT contents
9 kx // for the executable (non-PIC) and shared-library (PIC) cases.
9 kx // The unwind information is uniform across those two, so it's here.
9 kx
9 kx class Output_data_plt_i386_standard : public Output_data_plt_i386
9 kx {
9 kx public:
9 kx Output_data_plt_i386_standard(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative)
9 kx : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
9 kx { }
9 kx
9 kx protected:
9 kx virtual unsigned int
9 kx do_get_plt_entry_size() const
9 kx { return plt_entry_size; }
9 kx
9 kx virtual void
9 kx do_add_eh_frame(Layout* layout)
9 kx {
9 kx layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
9 kx plt_eh_frame_fde, plt_eh_frame_fde_size);
9 kx }
9 kx
9 kx // The size of an entry in the PLT.
9 kx static const int plt_entry_size = 16;
9 kx
9 kx // The .eh_frame unwind information for the PLT.
9 kx static const int plt_eh_frame_fde_size = 32;
9 kx static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
9 kx };
9 kx
9 kx // Actually fill the PLT contents for an executable (non-PIC).
9 kx
9 kx class Output_data_plt_i386_exec : public Output_data_plt_i386_standard
9 kx {
9 kx public:
9 kx Output_data_plt_i386_exec(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative)
9 kx : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
9 kx { }
9 kx
9 kx protected:
9 kx virtual void
9 kx do_fill_first_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address);
9 kx
9 kx virtual unsigned int
9 kx do_fill_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset);
9 kx
9 kx private:
9 kx // The first entry in the PLT for an executable.
9 kx static const unsigned char first_plt_entry[plt_entry_size];
9 kx
9 kx // Other entries in the PLT for an executable.
9 kx static const unsigned char plt_entry[plt_entry_size];
9 kx };
9 kx
9 kx // Actually fill the PLT contents for a shared library (PIC).
9 kx
9 kx class Output_data_plt_i386_dyn : public Output_data_plt_i386_standard
9 kx {
9 kx public:
9 kx Output_data_plt_i386_dyn(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative)
9 kx : Output_data_plt_i386_standard(layout, got_plt, got_irelative)
9 kx { }
9 kx
9 kx protected:
9 kx virtual void
9 kx do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
9 kx
9 kx virtual unsigned int
9 kx do_fill_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset);
9 kx
9 kx private:
9 kx // The first entry in the PLT for a shared object.
9 kx static const unsigned char first_plt_entry[plt_entry_size];
9 kx
9 kx // Other entries in the PLT for a shared object.
9 kx static const unsigned char plt_entry[plt_entry_size];
9 kx };
9 kx
9 kx // The i386 target class.
9 kx // TLS info comes from
9 kx // http://people.redhat.com/drepper/tls.pdf
9 kx // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
9 kx
9 kx class Target_i386 : public Sized_target<32, false>
9 kx {
9 kx public:
9 kx typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
9 kx
9 kx Target_i386(const Target::Target_info* info = &i386_info)
9 kx : Sized_target<32, false>(info),
9 kx got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
9 kx got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
9 kx rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY),
9 kx got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
9 kx isa_1_used_(0), isa_1_needed_(0),
9 kx feature_1_(0), feature_2_used_(0), feature_2_needed_(0),
9 kx object_isa_1_used_(0), object_feature_1_(0),
9 kx object_feature_2_used_(0), seen_first_object_(false)
9 kx { }
9 kx
9 kx // Process the relocations to determine unreferenced sections for
9 kx // garbage collection.
9 kx void
9 kx gc_process_relocs(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx size_t local_symbol_count,
9 kx const unsigned char* plocal_symbols);
9 kx
9 kx // Scan the relocations to look for symbol adjustments.
9 kx void
9 kx scan_relocs(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx size_t local_symbol_count,
9 kx const unsigned char* plocal_symbols);
9 kx
9 kx // Finalize the sections.
9 kx void
9 kx do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
9 kx
9 kx // Return the value to use for a dynamic which requires special
9 kx // treatment.
9 kx uint64_t
9 kx do_dynsym_value(const Symbol*) const;
9 kx
9 kx // Relocate a section.
9 kx void
9 kx relocate_section(const Relocate_info<32, false>*,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx unsigned char* view,
9 kx elfcpp::Elf_types<32>::Elf_Addr view_address,
9 kx section_size_type view_size,
9 kx const Reloc_symbol_changes*);
9 kx
9 kx // Scan the relocs during a relocatable link.
9 kx void
9 kx scan_relocatable_relocs(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx size_t local_symbol_count,
9 kx const unsigned char* plocal_symbols,
9 kx Relocatable_relocs*);
9 kx
9 kx // Scan the relocs for --emit-relocs.
9 kx void
9 kx emit_relocs_scan(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx size_t local_symbol_count,
9 kx const unsigned char* plocal_syms,
9 kx Relocatable_relocs* rr);
9 kx
9 kx // Emit relocations for a section.
9 kx void
9 kx relocate_relocs(const Relocate_info<32, false>*,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
9 kx unsigned char* view,
9 kx elfcpp::Elf_types<32>::Elf_Addr view_address,
9 kx section_size_type view_size,
9 kx unsigned char* reloc_view,
9 kx section_size_type reloc_view_size);
9 kx
9 kx // Return a string used to fill a code section with nops.
9 kx std::string
9 kx do_code_fill(section_size_type length) const;
9 kx
9 kx // Return whether SYM is defined by the ABI.
9 kx bool
9 kx do_is_defined_by_abi(const Symbol* sym) const
9 kx { return strcmp(sym->name(), "___tls_get_addr") == 0; }
9 kx
9 kx // Return whether a symbol name implies a local label. The UnixWare
9 kx // 2.1 cc generates temporary symbols that start with .X, so we
9 kx // recognize them here. FIXME: do other SVR4 compilers also use .X?.
9 kx // If so, we should move the .X recognition into
9 kx // Target::do_is_local_label_name.
9 kx bool
9 kx do_is_local_label_name(const char* name) const
9 kx {
9 kx if (name[0] == '.' && name[1] == 'X')
9 kx return true;
9 kx return Target::do_is_local_label_name(name);
9 kx }
9 kx
9 kx // Return the PLT address to use for a global symbol.
9 kx uint64_t
9 kx do_plt_address_for_global(const Symbol* gsym) const
9 kx { return this->plt_section()->address_for_global(gsym); }
9 kx
9 kx uint64_t
9 kx do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
9 kx { return this->plt_section()->address_for_local(relobj, symndx); }
9 kx
9 kx // We can tell whether we take the address of a function.
9 kx inline bool
9 kx do_can_check_for_function_pointers() const
9 kx { return true; }
9 kx
9 kx // Return the base for a DW_EH_PE_datarel encoding.
9 kx uint64_t
9 kx do_ehframe_datarel_base() const;
9 kx
9 kx // Return whether SYM is call to a non-split function.
9 kx bool
9 kx do_is_call_to_non_split(const Symbol* sym, const unsigned char*,
9 kx const unsigned char*, section_size_type) const;
9 kx
9 kx // Adjust -fsplit-stack code which calls non-split-stack code.
9 kx void
9 kx do_calls_non_split(Relobj* object, unsigned int shndx,
9 kx section_offset_type fnoffset, section_size_type fnsize,
9 kx const unsigned char* prelocs, size_t reloc_count,
9 kx unsigned char* view, section_size_type view_size,
9 kx std::string* from, std::string* to) const;
9 kx
9 kx // Return the size of the GOT section.
9 kx section_size_type
9 kx got_size() const
9 kx {
9 kx gold_assert(this->got_ != NULL);
9 kx return this->got_->data_size();
9 kx }
9 kx
9 kx // Return the number of entries in the GOT.
9 kx unsigned int
9 kx got_entry_count() const
9 kx {
9 kx if (this->got_ == NULL)
9 kx return 0;
9 kx return this->got_size() / 4;
9 kx }
9 kx
9 kx // Return the number of entries in the PLT.
9 kx unsigned int
9 kx plt_entry_count() const;
9 kx
9 kx // Return the offset of the first non-reserved PLT entry.
9 kx unsigned int
9 kx first_plt_entry_offset() const;
9 kx
9 kx // Return the size of each PLT entry.
9 kx unsigned int
9 kx plt_entry_size() const;
9 kx
9 kx protected:
9 kx // Instantiate the plt_ member.
9 kx // This chooses the right PLT flavor for an executable or a shared object.
9 kx Output_data_plt_i386*
9 kx make_data_plt(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative,
9 kx bool dyn)
9 kx { return this->do_make_data_plt(layout, got_plt, got_irelative, dyn); }
9 kx
9 kx virtual Output_data_plt_i386*
9 kx do_make_data_plt(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative,
9 kx bool dyn)
9 kx {
9 kx if (dyn)
9 kx return new Output_data_plt_i386_dyn(layout, got_plt, got_irelative);
9 kx else
9 kx return new Output_data_plt_i386_exec(layout, got_plt, got_irelative);
9 kx }
9 kx
9 kx private:
9 kx // The class which scans relocations.
9 kx struct Scan
9 kx {
9 kx static inline int
9 kx
9 kx get_reference_flags(unsigned int r_type);
9 kx
9 kx inline void
9 kx local(Symbol_table* symtab, Layout* layout, Target_i386* target,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx Output_section* output_section,
9 kx const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
9 kx const elfcpp::Sym<32, false>& lsym,
9 kx bool is_discarded);
9 kx
9 kx inline void
9 kx global(Symbol_table* symtab, Layout* layout, Target_i386* target,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx Output_section* output_section,
9 kx const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
9 kx Symbol* gsym);
9 kx
9 kx inline bool
9 kx local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
9 kx Target_i386* target,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx Output_section* output_section,
9 kx const elfcpp::Rel<32, false>& reloc,
9 kx unsigned int r_type,
9 kx const elfcpp::Sym<32, false>& lsym);
9 kx
9 kx inline bool
9 kx global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
9 kx Target_i386* target,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx Output_section* output_section,
9 kx const elfcpp::Rel<32, false>& reloc,
9 kx unsigned int r_type,
9 kx Symbol* gsym);
9 kx
9 kx inline bool
9 kx possible_function_pointer_reloc(unsigned int r_type);
9 kx
9 kx bool
9 kx reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
9 kx unsigned int r_type);
9 kx
9 kx static void
9 kx unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
9 kx
9 kx static void
9 kx unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
9 kx Symbol*);
9 kx };
9 kx
9 kx // The class which implements relocation.
9 kx class Relocate
9 kx {
9 kx public:
9 kx Relocate()
9 kx : skip_call_tls_get_addr_(false),
9 kx local_dynamic_type_(LOCAL_DYNAMIC_NONE)
9 kx { }
9 kx
9 kx ~Relocate()
9 kx {
9 kx if (this->skip_call_tls_get_addr_)
9 kx {
9 kx // FIXME: This needs to specify the location somehow.
9 kx gold_error(_("missing expected TLS relocation"));
9 kx }
9 kx }
9 kx
9 kx // Return whether the static relocation needs to be applied.
9 kx inline bool
9 kx should_apply_static_reloc(const Sized_symbol<32>* gsym,
9 kx unsigned int r_type,
9 kx bool is_32bit,
9 kx Output_section* output_section);
9 kx
9 kx // Do a relocation. Return false if the caller should not issue
9 kx // any warnings about this relocation.
9 kx inline bool
9 kx relocate(const Relocate_info<32, false>*, unsigned int,
9 kx Target_i386*, Output_section*, size_t, const unsigned char*,
9 kx const Sized_symbol<32>*, const Symbol_value<32>*,
9 kx unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
9 kx section_size_type);
9 kx
9 kx private:
9 kx // Do a TLS relocation.
9 kx inline void
9 kx relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
9 kx size_t relnum, const elfcpp::Rel<32, false>&,
9 kx unsigned int r_type, const Sized_symbol<32>*,
9 kx const Symbol_value<32>*,
9 kx unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
9 kx section_size_type);
9 kx
9 kx // Do a TLS General-Dynamic to Initial-Exec transition.
9 kx inline void
9 kx tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
9 kx const elfcpp::Rel<32, false>&, unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size);
9 kx
9 kx // Do a TLS General-Dynamic to Local-Exec transition.
9 kx inline void
9 kx tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
9 kx Output_segment* tls_segment,
9 kx const elfcpp::Rel<32, false>&, unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size);
9 kx
9 kx // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
9 kx // transition.
9 kx inline void
9 kx tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
9 kx const elfcpp::Rel<32, false>&, unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size);
9 kx
9 kx // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
9 kx // transition.
9 kx inline void
9 kx tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
9 kx Output_segment* tls_segment,
9 kx const elfcpp::Rel<32, false>&, unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size);
9 kx
9 kx // Do a TLS Local-Dynamic to Local-Exec transition.
9 kx inline void
9 kx tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
9 kx Output_segment* tls_segment,
9 kx const elfcpp::Rel<32, false>&, unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size);
9 kx
9 kx // Do a TLS Initial-Exec to Local-Exec transition.
9 kx static inline void
9 kx tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
9 kx Output_segment* tls_segment,
9 kx const elfcpp::Rel<32, false>&, unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size);
9 kx
9 kx // We need to keep track of which type of local dynamic relocation
9 kx // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
9 kx enum Local_dynamic_type
9 kx {
9 kx LOCAL_DYNAMIC_NONE,
9 kx LOCAL_DYNAMIC_SUN,
9 kx LOCAL_DYNAMIC_GNU
9 kx };
9 kx
9 kx // This is set if we should skip the next reloc, which should be a
9 kx // PLT32 reloc against ___tls_get_addr.
9 kx bool skip_call_tls_get_addr_;
9 kx // The type of local dynamic relocation we have seen in the section
9 kx // being relocated, if any.
9 kx Local_dynamic_type local_dynamic_type_;
9 kx };
9 kx
9 kx // A class for inquiring about properties of a relocation,
9 kx // used while scanning relocs during a relocatable link and
9 kx // garbage collection.
9 kx class Classify_reloc :
9 kx public gold::Default_classify_reloc<elfcpp::SHT_REL, 32, false>
9 kx {
9 kx public:
9 kx typedef Reloc_types<elfcpp::SHT_REL, 32, false>::Reloc Reltype;
9 kx
9 kx // Return the explicit addend of the relocation (return 0 for SHT_REL).
9 kx static elfcpp::Elf_types<32>::Elf_Swxword
9 kx get_r_addend(const Reltype*)
9 kx { return 0; }
9 kx
9 kx // Return the size of the addend of the relocation (only used for SHT_REL).
9 kx static unsigned int
9 kx get_size_for_reloc(unsigned int, Relobj*);
9 kx };
9 kx
9 kx // Adjust TLS relocation type based on the options and whether this
9 kx // is a local symbol.
9 kx static tls::Tls_optimization
9 kx optimize_tls_reloc(bool is_final, int r_type);
9 kx
9 kx // Check if relocation against this symbol is a candidate for
9 kx // conversion from
9 kx // mov foo@GOT(%reg), %reg
9 kx // to
9 kx // lea foo@GOTOFF(%reg), %reg.
9 kx static bool
9 kx can_convert_mov_to_lea(const Symbol* gsym)
9 kx {
9 kx gold_assert(gsym != NULL);
9 kx return (gsym->type() != elfcpp::STT_GNU_IFUNC
9 kx && !gsym->is_undefined ()
9 kx && !gsym->is_from_dynobj()
9 kx && !gsym->is_preemptible()
9 kx && (!parameters->options().shared()
9 kx || (gsym->visibility() != elfcpp::STV_DEFAULT
9 kx && gsym->visibility() != elfcpp::STV_PROTECTED)
9 kx || parameters->options().Bsymbolic())
9 kx && strcmp(gsym->name(), "_DYNAMIC") != 0);
9 kx }
9 kx
9 kx // Get the GOT section, creating it if necessary.
9 kx Output_data_got<32, false>*
9 kx got_section(Symbol_table*, Layout*);
9 kx
9 kx // Get the GOT PLT section.
9 kx Output_data_got_plt_i386*
9 kx got_plt_section() const
9 kx {
9 kx gold_assert(this->got_plt_ != NULL);
9 kx return this->got_plt_;
9 kx }
9 kx
9 kx // Get the GOT section for TLSDESC entries.
9 kx Output_data_got<32, false>*
9 kx got_tlsdesc_section() const
9 kx {
9 kx gold_assert(this->got_tlsdesc_ != NULL);
9 kx return this->got_tlsdesc_;
9 kx }
9 kx
9 kx // Create the PLT section.
9 kx void
9 kx make_plt_section(Symbol_table* symtab, Layout* layout);
9 kx
9 kx // Create a PLT entry for a global symbol.
9 kx void
9 kx make_plt_entry(Symbol_table*, Layout*, Symbol*);
9 kx
9 kx // Create a PLT entry for a local STT_GNU_IFUNC symbol.
9 kx void
9 kx make_local_ifunc_plt_entry(Symbol_table*, Layout*,
9 kx Sized_relobj_file<32, false>* relobj,
9 kx unsigned int local_sym_index);
9 kx
9 kx // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
9 kx void
9 kx define_tls_base_symbol(Symbol_table*, Layout*);
9 kx
9 kx // Create a GOT entry for the TLS module index.
9 kx unsigned int
9 kx got_mod_index_entry(Symbol_table* symtab, Layout* layout,
9 kx Sized_relobj_file<32, false>* object);
9 kx
9 kx // Get the PLT section.
9 kx Output_data_plt_i386*
9 kx plt_section() const
9 kx {
9 kx gold_assert(this->plt_ != NULL);
9 kx return this->plt_;
9 kx }
9 kx
9 kx // Get the dynamic reloc section, creating it if necessary.
9 kx Reloc_section*
9 kx rel_dyn_section(Layout*);
9 kx
9 kx // Get the section to use for TLS_DESC relocations.
9 kx Reloc_section*
9 kx rel_tls_desc_section(Layout*) const;
9 kx
9 kx // Get the section to use for IRELATIVE relocations.
9 kx Reloc_section*
9 kx rel_irelative_section(Layout*);
9 kx
9 kx // Add a potential copy relocation.
9 kx void
9 kx copy_reloc(Symbol_table* symtab, Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int shndx, Output_section* output_section,
9 kx Symbol* sym, const elfcpp::Rel<32, false>& reloc)
9 kx {
9 kx unsigned int r_type = elfcpp::elf_r_type<32>(reloc.get_r_info());
9 kx this->copy_relocs_.copy_reloc(symtab, layout,
9 kx symtab->get_sized_symbol<32>(sym),
9 kx object, shndx, output_section,
9 kx r_type, reloc.get_r_offset(), 0,
9 kx this->rel_dyn_section(layout));
9 kx }
9 kx
9 kx // Record a target-specific program property in the .note.gnu.property
9 kx // section.
9 kx void
9 kx record_gnu_property(unsigned int, unsigned int, size_t,
9 kx const unsigned char*, const Object*);
9 kx
9 kx // Merge the target-specific program properties from the current object.
9 kx void
9 kx merge_gnu_properties(const Object*);
9 kx
9 kx // Finalize the target-specific program properties and add them back to
9 kx // the layout.
9 kx void
9 kx do_finalize_gnu_properties(Layout*) const;
9 kx
9 kx // Information about this specific target which we pass to the
9 kx // general Target structure.
9 kx static const Target::Target_info i386_info;
9 kx
9 kx // The types of GOT entries needed for this platform.
9 kx // These values are exposed to the ABI in an incremental link.
9 kx // Do not renumber existing values without changing the version
9 kx // number of the .gnu_incremental_inputs section.
9 kx enum Got_type
9 kx {
9 kx GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
9 kx GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
9 kx GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
9 kx GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
9 kx GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
9 kx };
9 kx
9 kx // The GOT section.
9 kx Output_data_got<32, false>* got_;
9 kx // The PLT section.
9 kx Output_data_plt_i386* plt_;
9 kx // The GOT PLT section.
9 kx Output_data_got_plt_i386* got_plt_;
9 kx // The GOT section for IRELATIVE relocations.
9 kx Output_data_space* got_irelative_;
9 kx // The GOT section for TLSDESC relocations.
9 kx Output_data_got<32, false>* got_tlsdesc_;
9 kx // The _GLOBAL_OFFSET_TABLE_ symbol.
9 kx Symbol* global_offset_table_;
9 kx // The dynamic reloc section.
9 kx Reloc_section* rel_dyn_;
9 kx // The section to use for IRELATIVE relocs.
9 kx Reloc_section* rel_irelative_;
9 kx // Relocs saved to avoid a COPY reloc.
9 kx Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
9 kx // Offset of the GOT entry for the TLS module index.
9 kx unsigned int got_mod_index_offset_;
9 kx // True if the _TLS_MODULE_BASE_ symbol has been defined.
9 kx bool tls_base_symbol_defined_;
9 kx
9 kx // Target-specific program properties, from .note.gnu.property section.
9 kx // Each bit represents a specific feature.
9 kx uint32_t isa_1_used_;
9 kx uint32_t isa_1_needed_;
9 kx uint32_t feature_1_;
9 kx uint32_t feature_2_used_;
9 kx uint32_t feature_2_needed_;
9 kx // Target-specific properties from the current object.
9 kx // These bits get ORed into ISA_1_USED_ after all properties for the object
9 kx // have been processed. But if either is all zeroes (as when the property
9 kx // is absent from an object), the result should be all zeroes.
9 kx // (See PR ld/23486.)
9 kx uint32_t object_isa_1_used_;
9 kx // These bits get ANDed into FEATURE_1_ after all properties for the object
9 kx // have been processed.
9 kx uint32_t object_feature_1_;
9 kx uint32_t object_feature_2_used_;
9 kx // Whether we have seen our first object, for use in initializing FEATURE_1_.
9 kx bool seen_first_object_;
9 kx };
9 kx
9 kx const Target::Target_info Target_i386::i386_info =
9 kx {
9 kx 32, // size
9 kx false, // is_big_endian
9 kx elfcpp::EM_386, // machine_code
9 kx false, // has_make_symbol
9 kx false, // has_resolve
9 kx true, // has_code_fill
9 kx true, // is_default_stack_executable
9 kx true, // can_icf_inline_merge_sections
9 kx '\0', // wrap_char
9 kx "/usr/lib/libc.so.1", // dynamic_linker
9 kx 0x08048000, // default_text_segment_address
9 kx 0x1000, // abi_pagesize (overridable by -z max-page-size)
9 kx 0x1000, // common_pagesize (overridable by -z common-page-size)
9 kx false, // isolate_execinstr
9 kx 0, // rosegment_gap
9 kx elfcpp::SHN_UNDEF, // small_common_shndx
9 kx elfcpp::SHN_UNDEF, // large_common_shndx
9 kx 0, // small_common_section_flags
9 kx 0, // large_common_section_flags
9 kx NULL, // attributes_section
9 kx NULL, // attributes_vendor
9 kx "_start", // entry_symbol_name
9 kx 32, // hash_entry_size
9 kx elfcpp::SHT_PROGBITS, // unwind_section_type
9 kx };
9 kx
9 kx // Get the GOT section, creating it if necessary.
9 kx
9 kx Output_data_got<32, false>*
9 kx Target_i386::got_section(Symbol_table* symtab, Layout* layout)
9 kx {
9 kx if (this->got_ == NULL)
9 kx {
9 kx gold_assert(symtab != NULL && layout != NULL);
9 kx
9 kx this->got_ = new Output_data_got<32, false>();
9 kx
9 kx // When using -z now, we can treat .got.plt as a relro section.
9 kx // Without -z now, it is modified after program startup by lazy
9 kx // PLT relocations.
9 kx bool is_got_plt_relro = parameters->options().now();
9 kx Output_section_order got_order = (is_got_plt_relro
9 kx ? ORDER_RELRO
9 kx : ORDER_RELRO_LAST);
9 kx Output_section_order got_plt_order = (is_got_plt_relro
9 kx ? ORDER_RELRO
9 kx : ORDER_NON_RELRO_FIRST);
9 kx
9 kx layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
9 kx (elfcpp::SHF_ALLOC
9 kx | elfcpp::SHF_WRITE),
9 kx this->got_, got_order, true);
9 kx
9 kx this->got_plt_ = new Output_data_got_plt_i386(layout);
9 kx layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
9 kx (elfcpp::SHF_ALLOC
9 kx | elfcpp::SHF_WRITE),
9 kx this->got_plt_, got_plt_order,
9 kx is_got_plt_relro);
9 kx
9 kx // The first three entries are reserved.
9 kx this->got_plt_->set_current_data_size(3 * 4);
9 kx
9 kx if (!is_got_plt_relro)
9 kx {
9 kx // Those bytes can go into the relro segment.
9 kx layout->increase_relro(3 * 4);
9 kx }
9 kx
9 kx // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
9 kx this->global_offset_table_ =
9 kx symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
9 kx Symbol_table::PREDEFINED,
9 kx this->got_plt_,
9 kx 0, 0, elfcpp::STT_OBJECT,
9 kx elfcpp::STB_LOCAL,
9 kx elfcpp::STV_HIDDEN, 0,
9 kx false, false);
9 kx
9 kx // If there are any IRELATIVE relocations, they get GOT entries
9 kx // in .got.plt after the jump slot relocations.
9 kx this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
9 kx layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
9 kx (elfcpp::SHF_ALLOC
9 kx | elfcpp::SHF_WRITE),
9 kx this->got_irelative_,
9 kx got_plt_order, is_got_plt_relro);
9 kx
9 kx // If there are any TLSDESC relocations, they get GOT entries in
9 kx // .got.plt after the jump slot entries.
9 kx this->got_tlsdesc_ = new Output_data_got<32, false>();
9 kx layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
9 kx (elfcpp::SHF_ALLOC
9 kx | elfcpp::SHF_WRITE),
9 kx this->got_tlsdesc_,
9 kx got_plt_order, is_got_plt_relro);
9 kx }
9 kx
9 kx return this->got_;
9 kx }
9 kx
9 kx // Get the dynamic reloc section, creating it if necessary.
9 kx
9 kx Target_i386::Reloc_section*
9 kx Target_i386::rel_dyn_section(Layout* layout)
9 kx {
9 kx if (this->rel_dyn_ == NULL)
9 kx {
9 kx gold_assert(layout != NULL);
9 kx this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
9 kx layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
9 kx elfcpp::SHF_ALLOC, this->rel_dyn_,
9 kx ORDER_DYNAMIC_RELOCS, false);
9 kx }
9 kx return this->rel_dyn_;
9 kx }
9 kx
9 kx // Get the section to use for IRELATIVE relocs, creating it if
9 kx // necessary. These go in .rel.dyn, but only after all other dynamic
9 kx // relocations. They need to follow the other dynamic relocations so
9 kx // that they can refer to global variables initialized by those
9 kx // relocs.
9 kx
9 kx Target_i386::Reloc_section*
9 kx Target_i386::rel_irelative_section(Layout* layout)
9 kx {
9 kx if (this->rel_irelative_ == NULL)
9 kx {
9 kx // Make sure we have already create the dynamic reloc section.
9 kx this->rel_dyn_section(layout);
9 kx this->rel_irelative_ = new Reloc_section(false);
9 kx layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
9 kx elfcpp::SHF_ALLOC, this->rel_irelative_,
9 kx ORDER_DYNAMIC_RELOCS, false);
9 kx gold_assert(this->rel_dyn_->output_section()
9 kx == this->rel_irelative_->output_section());
9 kx }
9 kx return this->rel_irelative_;
9 kx }
9 kx
9 kx // Record a target-specific program property from the .note.gnu.property
9 kx // section.
9 kx void
9 kx Target_i386::record_gnu_property(
9 kx unsigned int, unsigned int pr_type,
9 kx size_t pr_datasz, const unsigned char* pr_data,
9 kx const Object* object)
9 kx {
9 kx uint32_t val = 0;
9 kx
9 kx switch (pr_type)
9 kx {
9 kx case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_USED:
9 kx case elfcpp::GNU_PROPERTY_X86_COMPAT_ISA_1_NEEDED:
9 kx case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_USED:
9 kx case elfcpp::GNU_PROPERTY_X86_COMPAT_2_ISA_1_NEEDED:
9 kx case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
9 kx case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
9 kx case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
9 kx case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
9 kx case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
9 kx if (pr_datasz != 4)
9 kx {
9 kx gold_warning(_("%s: corrupt .note.gnu.property section "
9 kx "(pr_datasz for property %d is not 4)"),
9 kx object->name().c_str(), pr_type);
9 kx return;
9 kx }
9 kx val = elfcpp::Swap<32, false>::readval(pr_data);
9 kx break;
9 kx default:
9 kx gold_warning(_("%s: unknown program property type 0x%x "
9 kx "in .note.gnu.property section"),
9 kx object->name().c_str(), pr_type);
9 kx break;
9 kx }
9 kx
9 kx switch (pr_type)
9 kx {
9 kx case elfcpp::GNU_PROPERTY_X86_ISA_1_USED:
9 kx this->object_isa_1_used_ |= val;
9 kx break;
9 kx case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED:
9 kx this->isa_1_needed_ |= val;
9 kx break;
9 kx case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND:
9 kx // If we see multiple feature props in one object, OR them together.
9 kx this->object_feature_1_ |= val;
9 kx break;
9 kx case elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED:
9 kx this->object_feature_2_used_ |= val;
9 kx break;
9 kx case elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED:
9 kx this->feature_2_needed_ |= val;
9 kx break;
9 kx }
9 kx }
9 kx
9 kx // Merge the target-specific program properties from the current object.
9 kx void
9 kx Target_i386::merge_gnu_properties(const Object*)
9 kx {
9 kx if (this->seen_first_object_)
9 kx {
9 kx // If any object is missing the ISA_1_USED property, we must omit
9 kx // it from the output file.
9 kx if (this->object_isa_1_used_ == 0)
9 kx this->isa_1_used_ = 0;
9 kx else if (this->isa_1_used_ != 0)
9 kx this->isa_1_used_ |= this->object_isa_1_used_;
9 kx this->feature_1_ &= this->object_feature_1_;
9 kx // If any object is missing the FEATURE_2_USED property, we must
9 kx // omit it from the output file.
9 kx if (this->object_feature_2_used_ == 0)
9 kx this->feature_2_used_ = 0;
9 kx else if (this->feature_2_used_ != 0)
9 kx this->feature_2_used_ |= this->object_feature_2_used_;
9 kx }
9 kx else
9 kx {
9 kx this->isa_1_used_ = this->object_isa_1_used_;
9 kx this->feature_1_ = this->object_feature_1_;
9 kx this->feature_2_used_ = this->object_feature_2_used_;
9 kx this->seen_first_object_ = true;
9 kx }
9 kx this->object_isa_1_used_ = 0;
9 kx this->object_feature_1_ = 0;
9 kx this->object_feature_2_used_ = 0;
9 kx }
9 kx
9 kx static inline void
9 kx add_property(Layout* layout, unsigned int pr_type, uint32_t val)
9 kx {
9 kx unsigned char buf[4];
9 kx elfcpp::Swap<32, false>::writeval(buf, val);
9 kx layout->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0, pr_type, 4, buf);
9 kx }
9 kx
9 kx // Finalize the target-specific program properties and add them back to
9 kx // the layout.
9 kx void
9 kx Target_i386::do_finalize_gnu_properties(Layout* layout) const
9 kx {
9 kx if (this->isa_1_used_ != 0)
9 kx add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_USED,
9 kx this->isa_1_used_);
9 kx if (this->isa_1_needed_ != 0)
9 kx add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED,
9 kx this->isa_1_needed_);
9 kx if (this->feature_1_ != 0)
9 kx add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND,
9 kx this->feature_1_);
9 kx if (this->feature_2_used_ != 0)
9 kx add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_USED,
9 kx this->feature_2_used_);
9 kx if (this->feature_2_needed_ != 0)
9 kx add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_2_NEEDED,
9 kx this->feature_2_needed_);
9 kx }
9 kx
9 kx // Write the first three reserved words of the .got.plt section.
9 kx // The remainder of the section is written while writing the PLT
9 kx // in Output_data_plt_i386::do_write.
9 kx
9 kx void
9 kx Output_data_got_plt_i386::do_write(Output_file* of)
9 kx {
9 kx // The first entry in the GOT is the address of the .dynamic section
9 kx // aka the PT_DYNAMIC segment. The next two entries are reserved.
9 kx // We saved space for them when we created the section in
9 kx // Target_i386::got_section.
9 kx const off_t got_file_offset = this->offset();
9 kx gold_assert(this->data_size() >= 12);
9 kx unsigned char* const got_view = of->get_output_view(got_file_offset, 12);
9 kx Output_section* dynamic = this->layout_->dynamic_section();
9 kx uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
9 kx elfcpp::Swap<32, false>::writeval(got_view, dynamic_addr);
9 kx memset(got_view + 4, 0, 8);
9 kx of->write_output_view(got_file_offset, 12, got_view);
9 kx }
9 kx
9 kx // Create the PLT section. The ordinary .got section is an argument,
9 kx // since we need to refer to the start. We also create our own .got
9 kx // section just for PLT entries.
9 kx
9 kx Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
9 kx uint64_t addralign,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative)
9 kx : Output_section_data(addralign),
9 kx tls_desc_rel_(NULL), irelative_rel_(NULL), got_plt_(got_plt),
9 kx got_irelative_(got_irelative), count_(0), irelative_count_(0),
9 kx global_ifuncs_(), local_ifuncs_()
9 kx {
9 kx this->rel_ = new Reloc_section(false);
9 kx layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
9 kx elfcpp::SHF_ALLOC, this->rel_,
9 kx ORDER_DYNAMIC_PLT_RELOCS, false);
9 kx }
9 kx
9 kx void
9 kx Output_data_plt_i386::do_adjust_output_section(Output_section* os)
9 kx {
9 kx // UnixWare sets the entsize of .plt to 4, and so does the old GNU
9 kx // linker, and so do we.
9 kx os->set_entsize(4);
9 kx }
9 kx
9 kx // Add an entry to the PLT.
9 kx
9 kx void
9 kx Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
9 kx Symbol* gsym)
9 kx {
9 kx gold_assert(!gsym->has_plt_offset());
9 kx
9 kx // Every PLT entry needs a reloc.
9 kx if (gsym->type() == elfcpp::STT_GNU_IFUNC
9 kx && gsym->can_use_relative_reloc(false))
9 kx {
9 kx gsym->set_plt_offset(this->irelative_count_ * this->get_plt_entry_size());
9 kx ++this->irelative_count_;
9 kx section_offset_type got_offset =
9 kx this->got_irelative_->current_data_size();
9 kx this->got_irelative_->set_current_data_size(got_offset + 4);
9 kx Reloc_section* rel = this->rel_irelative(symtab, layout);
9 kx rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
9 kx this->got_irelative_, got_offset);
9 kx struct Global_ifunc gi;
9 kx gi.sym = gsym;
9 kx gi.got_offset = got_offset;
9 kx this->global_ifuncs_.push_back(gi);
9 kx }
9 kx else
9 kx {
9 kx // When setting the PLT offset we skip the initial reserved PLT
9 kx // entry.
9 kx gsym->set_plt_offset((this->count_ + 1) * this->get_plt_entry_size());
9 kx
9 kx ++this->count_;
9 kx
9 kx section_offset_type got_offset = this->got_plt_->current_data_size();
9 kx
9 kx // Every PLT entry needs a GOT entry which points back to the
9 kx // PLT entry (this will be changed by the dynamic linker,
9 kx // normally lazily when the function is called).
9 kx this->got_plt_->set_current_data_size(got_offset + 4);
9 kx
9 kx gsym->set_needs_dynsym_entry();
9 kx this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
9 kx got_offset);
9 kx }
9 kx
9 kx // Note that we don't need to save the symbol. The contents of the
9 kx // PLT are independent of which symbols are used. The symbols only
9 kx // appear in the relocations.
9 kx }
9 kx
9 kx // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
9 kx // the PLT offset.
9 kx
9 kx unsigned int
9 kx Output_data_plt_i386::add_local_ifunc_entry(
9 kx Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* relobj,
9 kx unsigned int local_sym_index)
9 kx {
9 kx unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
9 kx ++this->irelative_count_;
9 kx
9 kx section_offset_type got_offset = this->got_irelative_->current_data_size();
9 kx
9 kx // Every PLT entry needs a GOT entry which points back to the PLT
9 kx // entry.
9 kx this->got_irelative_->set_current_data_size(got_offset + 4);
9 kx
9 kx // Every PLT entry needs a reloc.
9 kx Reloc_section* rel = this->rel_irelative(symtab, layout);
9 kx rel->add_symbolless_local_addend(relobj, local_sym_index,
9 kx elfcpp::R_386_IRELATIVE,
9 kx this->got_irelative_, got_offset);
9 kx
9 kx struct Local_ifunc li;
9 kx li.object = relobj;
9 kx li.local_sym_index = local_sym_index;
9 kx li.got_offset = got_offset;
9 kx this->local_ifuncs_.push_back(li);
9 kx
9 kx return plt_offset;
9 kx }
9 kx
9 kx // Return where the TLS_DESC relocations should go, creating it if
9 kx // necessary. These follow the JUMP_SLOT relocations.
9 kx
9 kx Output_data_plt_i386::Reloc_section*
9 kx Output_data_plt_i386::rel_tls_desc(Layout* layout)
9 kx {
9 kx if (this->tls_desc_rel_ == NULL)
9 kx {
9 kx this->tls_desc_rel_ = new Reloc_section(false);
9 kx layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
9 kx elfcpp::SHF_ALLOC, this->tls_desc_rel_,
9 kx ORDER_DYNAMIC_PLT_RELOCS, false);
9 kx gold_assert(this->tls_desc_rel_->output_section()
9 kx == this->rel_->output_section());
9 kx }
9 kx return this->tls_desc_rel_;
9 kx }
9 kx
9 kx // Return where the IRELATIVE relocations should go in the PLT. These
9 kx // follow the JUMP_SLOT and TLS_DESC relocations.
9 kx
9 kx Output_data_plt_i386::Reloc_section*
9 kx Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
9 kx {
9 kx if (this->irelative_rel_ == NULL)
9 kx {
9 kx // Make sure we have a place for the TLS_DESC relocations, in
9 kx // case we see any later on.
9 kx this->rel_tls_desc(layout);
9 kx this->irelative_rel_ = new Reloc_section(false);
9 kx layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
9 kx elfcpp::SHF_ALLOC, this->irelative_rel_,
9 kx ORDER_DYNAMIC_PLT_RELOCS, false);
9 kx gold_assert(this->irelative_rel_->output_section()
9 kx == this->rel_->output_section());
9 kx
9 kx if (parameters->doing_static_link())
9 kx {
9 kx // A statically linked executable will only have a .rel.plt
9 kx // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
9 kx // symbols. The library will use these symbols to locate
9 kx // the IRELATIVE relocs at program startup time.
9 kx symtab->define_in_output_data("__rel_iplt_start", NULL,
9 kx Symbol_table::PREDEFINED,
9 kx this->irelative_rel_, 0, 0,
9 kx elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9 kx elfcpp::STV_HIDDEN, 0, false, true);
9 kx symtab->define_in_output_data("__rel_iplt_end", NULL,
9 kx Symbol_table::PREDEFINED,
9 kx this->irelative_rel_, 0, 0,
9 kx elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
9 kx elfcpp::STV_HIDDEN, 0, true, true);
9 kx }
9 kx }
9 kx return this->irelative_rel_;
9 kx }
9 kx
9 kx // Return the PLT address to use for a global symbol.
9 kx
9 kx uint64_t
9 kx Output_data_plt_i386::address_for_global(const Symbol* gsym)
9 kx {
9 kx uint64_t offset = 0;
9 kx if (gsym->type() == elfcpp::STT_GNU_IFUNC
9 kx && gsym->can_use_relative_reloc(false))
9 kx offset = (this->count_ + 1) * this->get_plt_entry_size();
9 kx return this->address() + offset + gsym->plt_offset();
9 kx }
9 kx
9 kx // Return the PLT address to use for a local symbol. These are always
9 kx // IRELATIVE relocs.
9 kx
9 kx uint64_t
9 kx Output_data_plt_i386::address_for_local(const Relobj* object,
9 kx unsigned int r_sym)
9 kx {
9 kx return (this->address()
9 kx + (this->count_ + 1) * this->get_plt_entry_size()
9 kx + object->local_plt_offset(r_sym));
9 kx }
9 kx
9 kx // The first entry in the PLT for an executable.
9 kx
9 kx const unsigned char Output_data_plt_i386_exec::first_plt_entry[plt_entry_size] =
9 kx {
9 kx 0xff, 0x35, // pushl contents of memory address
9 kx 0, 0, 0, 0, // replaced with address of .got + 4
9 kx 0xff, 0x25, // jmp indirect
9 kx 0, 0, 0, 0, // replaced with address of .got + 8
9 kx 0, 0, 0, 0 // unused
9 kx };
9 kx
9 kx void
9 kx Output_data_plt_i386_exec::do_fill_first_plt_entry(
9 kx unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address)
9 kx {
9 kx memcpy(pov, first_plt_entry, plt_entry_size);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
9 kx elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
9 kx }
9 kx
9 kx // The first entry in the PLT for a shared object.
9 kx
9 kx const unsigned char Output_data_plt_i386_dyn::first_plt_entry[plt_entry_size] =
9 kx {
9 kx 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
9 kx 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
9 kx 0, 0, 0, 0 // unused
9 kx };
9 kx
9 kx void
9 kx Output_data_plt_i386_dyn::do_fill_first_plt_entry(
9 kx unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr)
9 kx {
9 kx memcpy(pov, first_plt_entry, plt_entry_size);
9 kx }
9 kx
9 kx // Subsequent entries in the PLT for an executable.
9 kx
9 kx const unsigned char Output_data_plt_i386_exec::plt_entry[plt_entry_size] =
9 kx {
9 kx 0xff, 0x25, // jmp indirect
9 kx 0, 0, 0, 0, // replaced with address of symbol in .got
9 kx 0x68, // pushl immediate
9 kx 0, 0, 0, 0, // replaced with offset into relocation table
9 kx 0xe9, // jmp relative
9 kx 0, 0, 0, 0 // replaced with offset to start of .plt
9 kx };
9 kx
9 kx unsigned int
9 kx Output_data_plt_i386_exec::do_fill_plt_entry(
9 kx unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset)
9 kx {
9 kx memcpy(pov, plt_entry, plt_entry_size);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
9 kx got_address + got_offset);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
9 kx elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
9 kx return 6;
9 kx }
9 kx
9 kx // Subsequent entries in the PLT for a shared object.
9 kx
9 kx const unsigned char Output_data_plt_i386_dyn::plt_entry[plt_entry_size] =
9 kx {
9 kx 0xff, 0xa3, // jmp *offset(%ebx)
9 kx 0, 0, 0, 0, // replaced with offset of symbol in .got
9 kx 0x68, // pushl immediate
9 kx 0, 0, 0, 0, // replaced with offset into relocation table
9 kx 0xe9, // jmp relative
9 kx 0, 0, 0, 0 // replaced with offset to start of .plt
9 kx };
9 kx
9 kx unsigned int
9 kx Output_data_plt_i386_dyn::do_fill_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset)
9 kx {
9 kx memcpy(pov, plt_entry, plt_entry_size);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
9 kx elfcpp::Swap<32, false>::writeval(pov + 12, - (plt_offset + 12 + 4));
9 kx return 6;
9 kx }
9 kx
9 kx // The .eh_frame unwind information for the PLT.
9 kx
9 kx const unsigned char
9 kx Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
9 kx {
9 kx 1, // CIE version.
9 kx 'z', // Augmentation: augmentation size included.
9 kx 'R', // Augmentation: FDE encoding included.
9 kx '\0', // End of augmentation string.
9 kx 1, // Code alignment factor.
9 kx 0x7c, // Data alignment factor.
9 kx 8, // Return address column.
9 kx 1, // Augmentation size.
9 kx (elfcpp::DW_EH_PE_pcrel // FDE encoding.
9 kx | elfcpp::DW_EH_PE_sdata4),
9 kx elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
9 kx elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
9 kx elfcpp::DW_CFA_nop, // Align to 16 bytes.
9 kx elfcpp::DW_CFA_nop
9 kx };
9 kx
9 kx const unsigned char
9 kx Output_data_plt_i386_standard::plt_eh_frame_fde[plt_eh_frame_fde_size] =
9 kx {
9 kx 0, 0, 0, 0, // Replaced with offset to .plt.
9 kx 0, 0, 0, 0, // Replaced with size of .plt.
9 kx 0, // Augmentation size.
9 kx elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
9 kx elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
9 kx elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
9 kx elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
9 kx elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
9 kx 11, // Block length.
9 kx elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
9 kx elfcpp::DW_OP_breg8, 0, // Push %eip.
9 kx elfcpp::DW_OP_lit15, // Push 0xf.
9 kx elfcpp::DW_OP_and, // & (%eip & 0xf).
9 kx elfcpp::DW_OP_lit11, // Push 0xb.
9 kx elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
9 kx elfcpp::DW_OP_lit2, // Push 2.
9 kx elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
9 kx elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
9 kx elfcpp::DW_CFA_nop, // Align to 32 bytes.
9 kx elfcpp::DW_CFA_nop,
9 kx elfcpp::DW_CFA_nop,
9 kx elfcpp::DW_CFA_nop
9 kx };
9 kx
9 kx // Write out the PLT. This uses the hand-coded instructions above,
9 kx // and adjusts them as needed. This is all specified by the i386 ELF
9 kx // Processor Supplement.
9 kx
9 kx void
9 kx Output_data_plt_i386::do_write(Output_file* of)
9 kx {
9 kx const off_t offset = this->offset();
9 kx const section_size_type oview_size =
9 kx convert_to_section_size_type(this->data_size());
9 kx unsigned char* const oview = of->get_output_view(offset, oview_size);
9 kx
9 kx const off_t got_file_offset = this->got_plt_->offset();
9 kx gold_assert(parameters->incremental_update()
9 kx || (got_file_offset + this->got_plt_->data_size()
9 kx == this->got_irelative_->offset()));
9 kx const section_size_type got_size =
9 kx convert_to_section_size_type(this->got_plt_->data_size()
9 kx + this->got_irelative_->data_size());
9 kx
9 kx unsigned char* const got_view = of->get_output_view(got_file_offset,
9 kx got_size);
9 kx
9 kx unsigned char* pov = oview;
9 kx
9 kx elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
9 kx
9 kx this->fill_first_plt_entry(pov, got_address);
9 kx pov += this->get_plt_entry_size();
9 kx
9 kx // The first three entries in the GOT are reserved, and are written
9 kx // by Output_data_got_plt_i386::do_write.
9 kx unsigned char* got_pov = got_view + 12;
9 kx
9 kx const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
9 kx
9 kx unsigned int plt_offset = this->get_plt_entry_size();
9 kx unsigned int plt_rel_offset = 0;
9 kx unsigned int got_offset = 12;
9 kx const unsigned int count = this->count_ + this->irelative_count_;
9 kx for (unsigned int i = 0;
9 kx i < count;
9 kx ++i,
9 kx pov += this->get_plt_entry_size(),
9 kx got_pov += 4,
9 kx plt_offset += this->get_plt_entry_size(),
9 kx plt_rel_offset += rel_size,
9 kx got_offset += 4)
9 kx {
9 kx // Set and adjust the PLT entry itself.
9 kx unsigned int lazy_offset = this->fill_plt_entry(pov,
9 kx got_address,
9 kx got_offset,
9 kx plt_offset,
9 kx plt_rel_offset);
9 kx
9 kx // Set the entry in the GOT.
9 kx elfcpp::Swap<32, false>::writeval(got_pov,
9 kx plt_address + plt_offset + lazy_offset);
9 kx }
9 kx
9 kx // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
9 kx // the GOT to point to the actual symbol value, rather than point to
9 kx // the PLT entry. That will let the dynamic linker call the right
9 kx // function when resolving IRELATIVE relocations.
9 kx unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
9 kx for (std::vector<Global_ifunc>::const_iterator p =
9 kx this->global_ifuncs_.begin();
9 kx p != this->global_ifuncs_.end();
9 kx ++p)
9 kx {
9 kx const Sized_symbol<32>* ssym =
9 kx static_cast<const Sized_symbol<32>*>(p->sym);
9 kx elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
9 kx ssym->value());
9 kx }
9 kx
9 kx for (std::vector<Local_ifunc>::const_iterator p =
9 kx this->local_ifuncs_.begin();
9 kx p != this->local_ifuncs_.end();
9 kx ++p)
9 kx {
9 kx const Symbol_value<32>* psymval =
9 kx p->object->local_symbol(p->local_sym_index);
9 kx elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
9 kx psymval->value(p->object, 0));
9 kx }
9 kx
9 kx gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
9 kx gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
9 kx
9 kx of->write_output_view(offset, oview_size, oview);
9 kx of->write_output_view(got_file_offset, got_size, got_view);
9 kx }
9 kx
9 kx // Create the PLT section.
9 kx
9 kx void
9 kx Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
9 kx {
9 kx if (this->plt_ == NULL)
9 kx {
9 kx // Create the GOT sections first.
9 kx this->got_section(symtab, layout);
9 kx
9 kx const bool dyn = parameters->options().output_is_position_independent();
9 kx this->plt_ = this->make_data_plt(layout,
9 kx this->got_plt_,
9 kx this->got_irelative_,
9 kx dyn);
9 kx
9 kx // Add unwind information if requested.
9 kx if (parameters->options().ld_generated_unwind_info())
9 kx this->plt_->add_eh_frame(layout);
9 kx
9 kx layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
9 kx (elfcpp::SHF_ALLOC
9 kx | elfcpp::SHF_EXECINSTR),
9 kx this->plt_, ORDER_PLT, false);
9 kx
9 kx // Make the sh_info field of .rel.plt point to .plt.
9 kx Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
9 kx rel_plt_os->set_info_section(this->plt_->output_section());
9 kx }
9 kx }
9 kx
9 kx // Create a PLT entry for a global symbol.
9 kx
9 kx void
9 kx Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
9 kx {
9 kx if (gsym->has_plt_offset())
9 kx return;
9 kx if (this->plt_ == NULL)
9 kx this->make_plt_section(symtab, layout);
9 kx this->plt_->add_entry(symtab, layout, gsym);
9 kx }
9 kx
9 kx // Make a PLT entry for a local STT_GNU_IFUNC symbol.
9 kx
9 kx void
9 kx Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
9 kx Sized_relobj_file<32, false>* relobj,
9 kx unsigned int local_sym_index)
9 kx {
9 kx if (relobj->local_has_plt_offset(local_sym_index))
9 kx return;
9 kx if (this->plt_ == NULL)
9 kx this->make_plt_section(symtab, layout);
9 kx unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
9 kx relobj,
9 kx local_sym_index);
9 kx relobj->set_local_plt_offset(local_sym_index, plt_offset);
9 kx }
9 kx
9 kx // Return the number of entries in the PLT.
9 kx
9 kx unsigned int
9 kx Target_i386::plt_entry_count() const
9 kx {
9 kx if (this->plt_ == NULL)
9 kx return 0;
9 kx return this->plt_->entry_count();
9 kx }
9 kx
9 kx // Return the offset of the first non-reserved PLT entry.
9 kx
9 kx unsigned int
9 kx Target_i386::first_plt_entry_offset() const
9 kx {
9 kx if (this->plt_ == NULL)
9 kx return 0;
9 kx return this->plt_->first_plt_entry_offset();
9 kx }
9 kx
9 kx // Return the size of each PLT entry.
9 kx
9 kx unsigned int
9 kx Target_i386::plt_entry_size() const
9 kx {
9 kx if (this->plt_ == NULL)
9 kx return 0;
9 kx return this->plt_->get_plt_entry_size();
9 kx }
9 kx
9 kx // Get the section to use for TLS_DESC relocations.
9 kx
9 kx Target_i386::Reloc_section*
9 kx Target_i386::rel_tls_desc_section(Layout* layout) const
9 kx {
9 kx return this->plt_section()->rel_tls_desc(layout);
9 kx }
9 kx
9 kx // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
9 kx
9 kx void
9 kx Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
9 kx {
9 kx if (this->tls_base_symbol_defined_)
9 kx return;
9 kx
9 kx Output_segment* tls_segment = layout->tls_segment();
9 kx if (tls_segment != NULL)
9 kx {
9 kx bool is_exec = parameters->options().output_is_executable();
9 kx symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
9 kx Symbol_table::PREDEFINED,
9 kx tls_segment, 0, 0,
9 kx elfcpp::STT_TLS,
9 kx elfcpp::STB_LOCAL,
9 kx elfcpp::STV_HIDDEN, 0,
9 kx (is_exec
9 kx ? Symbol::SEGMENT_END
9 kx : Symbol::SEGMENT_START),
9 kx true);
9 kx }
9 kx this->tls_base_symbol_defined_ = true;
9 kx }
9 kx
9 kx // Create a GOT entry for the TLS module index.
9 kx
9 kx unsigned int
9 kx Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
9 kx Sized_relobj_file<32, false>* object)
9 kx {
9 kx if (this->got_mod_index_offset_ == -1U)
9 kx {
9 kx gold_assert(symtab != NULL && layout != NULL && object != NULL);
9 kx Reloc_section* rel_dyn = this->rel_dyn_section(layout);
9 kx Output_data_got<32, false>* got = this->got_section(symtab, layout);
9 kx unsigned int got_offset = got->add_constant(0);
9 kx rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
9 kx got_offset);
9 kx got->add_constant(0);
9 kx this->got_mod_index_offset_ = got_offset;
9 kx }
9 kx return this->got_mod_index_offset_;
9 kx }
9 kx
9 kx // Optimize the TLS relocation type based on what we know about the
9 kx // symbol. IS_FINAL is true if the final address of this symbol is
9 kx // known at link time.
9 kx
9 kx tls::Tls_optimization
9 kx Target_i386::optimize_tls_reloc(bool is_final, int r_type)
9 kx {
9 kx // If we are generating a shared library, then we can't do anything
9 kx // in the linker.
9 kx if (parameters->options().shared())
9 kx return tls::TLSOPT_NONE;
9 kx
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_TLS_GD:
9 kx case elfcpp::R_386_TLS_GOTDESC:
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx // These are General-Dynamic which permits fully general TLS
9 kx // access. Since we know that we are generating an executable,
9 kx // we can convert this to Initial-Exec. If we also know that
9 kx // this is a local symbol, we can further switch to Local-Exec.
9 kx if (is_final)
9 kx return tls::TLSOPT_TO_LE;
9 kx return tls::TLSOPT_TO_IE;
9 kx
9 kx case elfcpp::R_386_TLS_LDM:
9 kx // This is Local-Dynamic, which refers to a local symbol in the
9 kx // dynamic TLS block. Since we know that we generating an
9 kx // executable, we can switch to Local-Exec.
9 kx return tls::TLSOPT_TO_LE;
9 kx
9 kx case elfcpp::R_386_TLS_LDO_32:
9 kx // Another type of Local-Dynamic relocation.
9 kx return tls::TLSOPT_TO_LE;
9 kx
9 kx case elfcpp::R_386_TLS_IE:
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx // These are Initial-Exec relocs which get the thread offset
9 kx // from the GOT. If we know that we are linking against the
9 kx // local symbol, we can switch to Local-Exec, which links the
9 kx // thread offset into the instruction.
9 kx if (is_final)
9 kx return tls::TLSOPT_TO_LE;
9 kx return tls::TLSOPT_NONE;
9 kx
9 kx case elfcpp::R_386_TLS_LE:
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx // When we already have Local-Exec, there is nothing further we
9 kx // can do.
9 kx return tls::TLSOPT_NONE;
9 kx
9 kx default:
9 kx gold_unreachable();
9 kx }
9 kx }
9 kx
9 kx // Get the Reference_flags for a particular relocation.
9 kx
9 kx int
9 kx Target_i386::Scan::get_reference_flags(unsigned int r_type)
9 kx {
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_NONE:
9 kx case elfcpp::R_386_GNU_VTINHERIT:
9 kx case elfcpp::R_386_GNU_VTENTRY:
9 kx case elfcpp::R_386_GOTPC:
9 kx // No symbol reference.
9 kx return 0;
9 kx
9 kx case elfcpp::R_386_32:
9 kx case elfcpp::R_386_16:
9 kx case elfcpp::R_386_8:
9 kx return Symbol::ABSOLUTE_REF;
9 kx
9 kx case elfcpp::R_386_PC32:
9 kx case elfcpp::R_386_PC16:
9 kx case elfcpp::R_386_PC8:
9 kx case elfcpp::R_386_GOTOFF:
9 kx return Symbol::RELATIVE_REF;
9 kx
9 kx case elfcpp::R_386_PLT32:
9 kx return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
9 kx
9 kx case elfcpp::R_386_GOT32:
9 kx case elfcpp::R_386_GOT32X:
9 kx // Absolute in GOT.
9 kx return Symbol::ABSOLUTE_REF;
9 kx
9 kx case elfcpp::R_386_TLS_GD: // Global-dynamic
9 kx case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx case elfcpp::R_386_TLS_LDM: // Local-dynamic
9 kx case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
9 kx case elfcpp::R_386_TLS_IE: // Initial-exec
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx case elfcpp::R_386_TLS_LE: // Local-exec
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx return Symbol::TLS_REF;
9 kx
9 kx case elfcpp::R_386_COPY:
9 kx case elfcpp::R_386_GLOB_DAT:
9 kx case elfcpp::R_386_JUMP_SLOT:
9 kx case elfcpp::R_386_RELATIVE:
9 kx case elfcpp::R_386_IRELATIVE:
9 kx case elfcpp::R_386_TLS_TPOFF:
9 kx case elfcpp::R_386_TLS_DTPMOD32:
9 kx case elfcpp::R_386_TLS_DTPOFF32:
9 kx case elfcpp::R_386_TLS_TPOFF32:
9 kx case elfcpp::R_386_TLS_DESC:
9 kx case elfcpp::R_386_32PLT:
9 kx case elfcpp::R_386_TLS_GD_32:
9 kx case elfcpp::R_386_TLS_GD_PUSH:
9 kx case elfcpp::R_386_TLS_GD_CALL:
9 kx case elfcpp::R_386_TLS_GD_POP:
9 kx case elfcpp::R_386_TLS_LDM_32:
9 kx case elfcpp::R_386_TLS_LDM_PUSH:
9 kx case elfcpp::R_386_TLS_LDM_CALL:
9 kx case elfcpp::R_386_TLS_LDM_POP:
9 kx case elfcpp::R_386_USED_BY_INTEL_200:
9 kx default:
9 kx // Not expected. We will give an error later.
9 kx return 0;
9 kx }
9 kx }
9 kx
9 kx // Report an unsupported relocation against a local symbol.
9 kx
9 kx void
9 kx Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
9 kx unsigned int r_type)
9 kx {
9 kx gold_error(_("%s: unsupported reloc %u against local symbol"),
9 kx object->name().c_str(), r_type);
9 kx }
9 kx
9 kx // Return whether we need to make a PLT entry for a relocation of a
9 kx // given type against a STT_GNU_IFUNC symbol.
9 kx
9 kx bool
9 kx Target_i386::Scan::reloc_needs_plt_for_ifunc(
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int r_type)
9 kx {
9 kx int flags = Scan::get_reference_flags(r_type);
9 kx if (flags & Symbol::TLS_REF)
9 kx gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
9 kx object->name().c_str(), r_type);
9 kx return flags != 0;
9 kx }
9 kx
9 kx // Scan a relocation for a local symbol.
9 kx
9 kx inline void
9 kx Target_i386::Scan::local(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Target_i386* target,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx Output_section* output_section,
9 kx const elfcpp::Rel<32, false>& reloc,
9 kx unsigned int r_type,
9 kx const elfcpp::Sym<32, false>& lsym,
9 kx bool is_discarded)
9 kx {
9 kx if (is_discarded)
9 kx return;
9 kx
9 kx // A local STT_GNU_IFUNC symbol may require a PLT entry.
9 kx if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
9 kx && this->reloc_needs_plt_for_ifunc(object, r_type))
9 kx {
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
9 kx }
9 kx
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_NONE:
9 kx case elfcpp::R_386_GNU_VTINHERIT:
9 kx case elfcpp::R_386_GNU_VTENTRY:
9 kx break;
9 kx
9 kx case elfcpp::R_386_32:
9 kx // If building a shared library (or a position-independent
9 kx // executable), we need to create a dynamic relocation for
9 kx // this location. The relocation applied at link time will
9 kx // apply the link-time value, so we flag the location with
9 kx // an R_386_RELATIVE relocation so the dynamic loader can
9 kx // relocate it easily.
9 kx if (parameters->options().output_is_position_independent())
9 kx {
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
9 kx output_section, data_shndx,
9 kx reloc.get_r_offset());
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_16:
9 kx case elfcpp::R_386_8:
9 kx // If building a shared library (or a position-independent
9 kx // executable), we need to create a dynamic relocation for
9 kx // this location. Because the addend needs to remain in the
9 kx // data section, we need to be careful not to apply this
9 kx // relocation statically.
9 kx if (parameters->options().output_is_position_independent())
9 kx {
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx if (lsym.get_st_type() != elfcpp::STT_SECTION)
9 kx rel_dyn->add_local(object, r_sym, r_type, output_section,
9 kx data_shndx, reloc.get_r_offset());
9 kx else
9 kx {
9 kx gold_assert(lsym.get_st_value() == 0);
9 kx unsigned int shndx = lsym.get_st_shndx();
9 kx bool is_ordinary;
9 kx shndx = object->adjust_sym_shndx(r_sym, shndx,
9 kx &is_ordinary);
9 kx if (!is_ordinary)
9 kx object->error(_("section symbol %u has bad shndx %u"),
9 kx r_sym, shndx);
9 kx else
9 kx rel_dyn->add_local_section(object, shndx,
9 kx r_type, output_section,
9 kx data_shndx, reloc.get_r_offset());
9 kx }
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_PC32:
9 kx case elfcpp::R_386_PC16:
9 kx case elfcpp::R_386_PC8:
9 kx break;
9 kx
9 kx case elfcpp::R_386_PLT32:
9 kx // Since we know this is a local symbol, we can handle this as a
9 kx // PC32 reloc.
9 kx break;
9 kx
9 kx case elfcpp::R_386_GOTOFF:
9 kx case elfcpp::R_386_GOTPC:
9 kx // We need a GOT section.
9 kx target->got_section(symtab, layout);
9 kx break;
9 kx
9 kx case elfcpp::R_386_GOT32:
9 kx case elfcpp::R_386_GOT32X:
9 kx {
9 kx // We need GOT section.
9 kx Output_data_got<32, false>* got = target->got_section(symtab, layout);
9 kx
9 kx // If the relocation symbol isn't IFUNC,
9 kx // and is local, then we will convert
9 kx // mov foo@GOT(%reg), %reg
9 kx // to
9 kx // lea foo@GOTOFF(%reg), %reg
9 kx // in Relocate::relocate.
9 kx if (reloc.get_r_offset() >= 2
9 kx && lsym.get_st_type() != elfcpp::STT_GNU_IFUNC)
9 kx {
9 kx section_size_type stype;
9 kx const unsigned char* view = object->section_contents(data_shndx,
9 kx &stype, true);
9 kx if (view[reloc.get_r_offset() - 2] == 0x8b)
9 kx break;
9 kx }
9 kx
9 kx // Otherwise, the symbol requires a GOT entry.
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx
9 kx // For a STT_GNU_IFUNC symbol we want the PLT offset. That
9 kx // lets function pointers compare correctly with shared
9 kx // libraries. Otherwise we would need an IRELATIVE reloc.
9 kx bool is_new;
9 kx if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
9 kx is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
9 kx else
9 kx is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
9 kx if (is_new)
9 kx {
9 kx // If we are generating a shared object, we need to add a
9 kx // dynamic RELATIVE relocation for this symbol's GOT entry.
9 kx if (parameters->options().output_is_position_independent())
9 kx {
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx unsigned int got_offset =
9 kx object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
9 kx rel_dyn->add_local_relative(object, r_sym,
9 kx elfcpp::R_386_RELATIVE,
9 kx got, got_offset);
9 kx }
9 kx }
9 kx }
9 kx break;
9 kx
9 kx // These are relocations which should only be seen by the
9 kx // dynamic linker, and should never be seen here.
9 kx case elfcpp::R_386_COPY:
9 kx case elfcpp::R_386_GLOB_DAT:
9 kx case elfcpp::R_386_JUMP_SLOT:
9 kx case elfcpp::R_386_RELATIVE:
9 kx case elfcpp::R_386_IRELATIVE:
9 kx case elfcpp::R_386_TLS_TPOFF:
9 kx case elfcpp::R_386_TLS_DTPMOD32:
9 kx case elfcpp::R_386_TLS_DTPOFF32:
9 kx case elfcpp::R_386_TLS_TPOFF32:
9 kx case elfcpp::R_386_TLS_DESC:
9 kx gold_error(_("%s: unexpected reloc %u in object file"),
9 kx object->name().c_str(), r_type);
9 kx break;
9 kx
9 kx // These are initial TLS relocs, which are expected when
9 kx // linking.
9 kx case elfcpp::R_386_TLS_GD: // Global-dynamic
9 kx case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx case elfcpp::R_386_TLS_LDM: // Local-dynamic
9 kx case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
9 kx case elfcpp::R_386_TLS_IE: // Initial-exec
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx case elfcpp::R_386_TLS_LE: // Local-exec
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx {
9 kx bool output_is_shared = parameters->options().shared();
9 kx const tls::Tls_optimization optimized_type
9 kx = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_TLS_GD: // Global-dynamic
9 kx if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Create a pair of GOT entries for the module index and
9 kx // dtv-relative offset.
9 kx Output_data_got<32, false>* got
9 kx = target->got_section(symtab, layout);
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx unsigned int shndx = lsym.get_st_shndx();
9 kx bool is_ordinary;
9 kx shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
9 kx if (!is_ordinary)
9 kx object->error(_("local symbol %u has bad shndx %u"),
9 kx r_sym, shndx);
9 kx else
9 kx got->add_local_pair_with_rel(object, r_sym, shndx,
9 kx GOT_TYPE_TLS_PAIR,
9 kx target->rel_dyn_section(layout),
9 kx elfcpp::R_386_TLS_DTPMOD32);
9 kx }
9 kx else if (optimized_type != tls::TLSOPT_TO_LE)
9 kx unsupported_reloc_local(object, r_type);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
9 kx target->define_tls_base_symbol(symtab, layout);
9 kx if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Create a double GOT entry with an R_386_TLS_DESC
9 kx // reloc. The R_386_TLS_DESC reloc is resolved
9 kx // lazily, so the GOT entry needs to be in an area in
9 kx // .got.plt, not .got. Call got_section to make sure
9 kx // the section has been created.
9 kx target->got_section(symtab, layout);
9 kx Output_data_got<32, false>* got = target->got_tlsdesc_section();
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
9 kx {
9 kx unsigned int got_offset = got->add_constant(0);
9 kx // The local symbol value is stored in the second
9 kx // GOT entry.
9 kx got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
9 kx // That set the GOT offset of the local symbol to
9 kx // point to the second entry, but we want it to
9 kx // point to the first.
9 kx object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
9 kx got_offset);
9 kx Reloc_section* rt = target->rel_tls_desc_section(layout);
9 kx rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
9 kx }
9 kx }
9 kx else if (optimized_type != tls::TLSOPT_TO_LE)
9 kx unsupported_reloc_local(object, r_type);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LDM: // Local-dynamic
9 kx if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Create a GOT entry for the module index.
9 kx target->got_mod_index_entry(symtab, layout, object);
9 kx }
9 kx else if (optimized_type != tls::TLSOPT_TO_LE)
9 kx unsupported_reloc_local(object, r_type);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_IE: // Initial-exec
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx layout->set_has_static_tls();
9 kx if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // For the R_386_TLS_IE relocation, we need to create a
9 kx // dynamic relocation when building a shared library.
9 kx if (r_type == elfcpp::R_386_TLS_IE
9 kx && parameters->options().shared())
9 kx {
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx unsigned int r_sym
9 kx = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx rel_dyn->add_local_relative(object, r_sym,
9 kx elfcpp::R_386_RELATIVE,
9 kx output_section, data_shndx,
9 kx reloc.get_r_offset());
9 kx }
9 kx // Create a GOT entry for the tp-relative offset.
9 kx Output_data_got<32, false>* got
9 kx = target->got_section(symtab, layout);
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
9 kx ? elfcpp::R_386_TLS_TPOFF32
9 kx : elfcpp::R_386_TLS_TPOFF);
9 kx unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
9 kx ? GOT_TYPE_TLS_OFFSET
9 kx : GOT_TYPE_TLS_NOFFSET);
9 kx got->add_local_with_rel(object, r_sym, got_type,
9 kx target->rel_dyn_section(layout),
9 kx dyn_r_type);
9 kx }
9 kx else if (optimized_type != tls::TLSOPT_TO_LE)
9 kx unsupported_reloc_local(object, r_type);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LE: // Local-exec
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx layout->set_has_static_tls();
9 kx if (output_is_shared)
9 kx {
9 kx // We need to create a dynamic relocation.
9 kx gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
9 kx unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
9 kx ? elfcpp::R_386_TLS_TPOFF32
9 kx : elfcpp::R_386_TLS_TPOFF);
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
9 kx data_shndx, reloc.get_r_offset());
9 kx }
9 kx break;
9 kx
9 kx default:
9 kx gold_unreachable();
9 kx }
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_32PLT:
9 kx case elfcpp::R_386_TLS_GD_32:
9 kx case elfcpp::R_386_TLS_GD_PUSH:
9 kx case elfcpp::R_386_TLS_GD_CALL:
9 kx case elfcpp::R_386_TLS_GD_POP:
9 kx case elfcpp::R_386_TLS_LDM_32:
9 kx case elfcpp::R_386_TLS_LDM_PUSH:
9 kx case elfcpp::R_386_TLS_LDM_CALL:
9 kx case elfcpp::R_386_TLS_LDM_POP:
9 kx case elfcpp::R_386_USED_BY_INTEL_200:
9 kx default:
9 kx unsupported_reloc_local(object, r_type);
9 kx break;
9 kx }
9 kx }
9 kx
9 kx // Report an unsupported relocation against a global symbol.
9 kx
9 kx void
9 kx Target_i386::Scan::unsupported_reloc_global(
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int r_type,
9 kx Symbol* gsym)
9 kx {
9 kx gold_error(_("%s: unsupported reloc %u against global symbol %s"),
9 kx object->name().c_str(), r_type, gsym->demangled_name().c_str());
9 kx }
9 kx
9 kx inline bool
9 kx Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
9 kx {
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_32:
9 kx case elfcpp::R_386_16:
9 kx case elfcpp::R_386_8:
9 kx case elfcpp::R_386_GOTOFF:
9 kx case elfcpp::R_386_GOT32:
9 kx case elfcpp::R_386_GOT32X:
9 kx {
9 kx return true;
9 kx }
9 kx default:
9 kx return false;
9 kx }
9 kx return false;
9 kx }
9 kx
9 kx inline bool
9 kx Target_i386::Scan::local_reloc_may_be_function_pointer(
9 kx Symbol_table* ,
9 kx Layout* ,
9 kx Target_i386* ,
9 kx Sized_relobj_file<32, false>* ,
9 kx unsigned int ,
9 kx Output_section* ,
9 kx const elfcpp::Rel<32, false>& ,
9 kx unsigned int r_type,
9 kx const elfcpp::Sym<32, false>&)
9 kx {
9 kx return possible_function_pointer_reloc(r_type);
9 kx }
9 kx
9 kx inline bool
9 kx Target_i386::Scan::global_reloc_may_be_function_pointer(
9 kx Symbol_table* ,
9 kx Layout* ,
9 kx Target_i386* ,
9 kx Sized_relobj_file<32, false>* ,
9 kx unsigned int ,
9 kx Output_section* ,
9 kx const elfcpp::Rel<32, false>& ,
9 kx unsigned int r_type,
9 kx Symbol*)
9 kx {
9 kx return possible_function_pointer_reloc(r_type);
9 kx }
9 kx
9 kx // Scan a relocation for a global symbol.
9 kx
9 kx inline void
9 kx Target_i386::Scan::global(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Target_i386* target,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx Output_section* output_section,
9 kx const elfcpp::Rel<32, false>& reloc,
9 kx unsigned int r_type,
9 kx Symbol* gsym)
9 kx {
9 kx // A STT_GNU_IFUNC symbol may require a PLT entry.
9 kx if (gsym->type() == elfcpp::STT_GNU_IFUNC
9 kx && this->reloc_needs_plt_for_ifunc(object, r_type))
9 kx target->make_plt_entry(symtab, layout, gsym);
9 kx
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_NONE:
9 kx case elfcpp::R_386_GNU_VTINHERIT:
9 kx case elfcpp::R_386_GNU_VTENTRY:
9 kx break;
9 kx
9 kx case elfcpp::R_386_32:
9 kx case elfcpp::R_386_16:
9 kx case elfcpp::R_386_8:
9 kx {
9 kx // Make a PLT entry if necessary.
9 kx if (gsym->needs_plt_entry())
9 kx {
9 kx target->make_plt_entry(symtab, layout, gsym);
9 kx // Since this is not a PC-relative relocation, we may be
9 kx // taking the address of a function. In that case we need to
9 kx // set the entry in the dynamic symbol table to the address of
9 kx // the PLT entry.
9 kx if (gsym->is_from_dynobj() && !parameters->options().shared())
9 kx gsym->set_needs_dynsym_value();
9 kx }
9 kx // Make a dynamic relocation if necessary.
9 kx if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
9 kx {
9 kx if (!parameters->options().output_is_position_independent()
9 kx && gsym->may_need_copy_reloc())
9 kx {
9 kx target->copy_reloc(symtab, layout, object,
9 kx data_shndx, output_section, gsym, reloc);
9 kx }
9 kx else if (r_type == elfcpp::R_386_32
9 kx && gsym->type() == elfcpp::STT_GNU_IFUNC
9 kx && gsym->can_use_relative_reloc(false)
9 kx && !gsym->is_from_dynobj()
9 kx && !gsym->is_undefined()
9 kx && !gsym->is_preemptible())
9 kx {
9 kx // Use an IRELATIVE reloc for a locally defined
9 kx // STT_GNU_IFUNC symbol. This makes a function
9 kx // address in a PIE executable match the address in a
9 kx // shared library that it links against.
9 kx Reloc_section* rel_dyn = target->rel_irelative_section(layout);
9 kx rel_dyn->add_symbolless_global_addend(gsym,
9 kx elfcpp::R_386_IRELATIVE,
9 kx output_section,
9 kx object, data_shndx,
9 kx reloc.get_r_offset());
9 kx }
9 kx else if (r_type == elfcpp::R_386_32
9 kx && gsym->can_use_relative_reloc(false))
9 kx {
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
9 kx output_section, object,
9 kx data_shndx, reloc.get_r_offset());
9 kx }
9 kx else
9 kx {
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx rel_dyn->add_global(gsym, r_type, output_section, object,
9 kx data_shndx, reloc.get_r_offset());
9 kx }
9 kx }
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_PC32:
9 kx case elfcpp::R_386_PC16:
9 kx case elfcpp::R_386_PC8:
9 kx {
9 kx // Make a PLT entry if necessary.
9 kx if (gsym->needs_plt_entry())
9 kx {
9 kx // These relocations are used for function calls only in
9 kx // non-PIC code. For a 32-bit relocation in a shared library,
9 kx // we'll need a text relocation anyway, so we can skip the
9 kx // PLT entry and let the dynamic linker bind the call directly
9 kx // to the target. For smaller relocations, we should use a
9 kx // PLT entry to ensure that the call can reach.
9 kx if (!parameters->options().shared()
9 kx || r_type != elfcpp::R_386_PC32)
9 kx target->make_plt_entry(symtab, layout, gsym);
9 kx }
9 kx // Make a dynamic relocation if necessary.
9 kx if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
9 kx {
9 kx if (parameters->options().output_is_executable()
9 kx && gsym->may_need_copy_reloc())
9 kx {
9 kx target->copy_reloc(symtab, layout, object,
9 kx data_shndx, output_section, gsym, reloc);
9 kx }
9 kx else
9 kx {
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx rel_dyn->add_global(gsym, r_type, output_section, object,
9 kx data_shndx, reloc.get_r_offset());
9 kx }
9 kx }
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_GOT32:
9 kx case elfcpp::R_386_GOT32X:
9 kx {
9 kx // The symbol requires a GOT section.
9 kx Output_data_got<32, false>* got = target->got_section(symtab, layout);
9 kx
9 kx // If we convert this from
9 kx // mov foo@GOT(%reg), %reg
9 kx // to
9 kx // lea foo@GOTOFF(%reg), %reg
9 kx // in Relocate::relocate, then there is nothing to do here.
9 kx if (reloc.get_r_offset() >= 2
9 kx && Target_i386::can_convert_mov_to_lea(gsym))
9 kx {
9 kx section_size_type stype;
9 kx const unsigned char* view = object->section_contents(data_shndx,
9 kx &stype, true);
9 kx if (view[reloc.get_r_offset() - 2] == 0x8b)
9 kx break;
9 kx }
9 kx
9 kx if (gsym->final_value_is_known())
9 kx {
9 kx // For a STT_GNU_IFUNC symbol we want the PLT address.
9 kx if (gsym->type() == elfcpp::STT_GNU_IFUNC)
9 kx got->add_global_plt(gsym, GOT_TYPE_STANDARD);
9 kx else
9 kx got->add_global(gsym, GOT_TYPE_STANDARD);
9 kx }
9 kx else
9 kx {
9 kx // If this symbol is not fully resolved, we need to add a
9 kx // GOT entry with a dynamic relocation.
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx
9 kx // Use a GLOB_DAT rather than a RELATIVE reloc if:
9 kx //
9 kx // 1) The symbol may be defined in some other module.
9 kx //
9 kx // 2) We are building a shared library and this is a
9 kx // protected symbol; using GLOB_DAT means that the dynamic
9 kx // linker can use the address of the PLT in the main
9 kx // executable when appropriate so that function address
9 kx // comparisons work.
9 kx //
9 kx // 3) This is a STT_GNU_IFUNC symbol in position dependent
9 kx // code, again so that function address comparisons work.
9 kx if (gsym->is_from_dynobj()
9 kx || gsym->is_undefined()
9 kx || gsym->is_preemptible()
9 kx || (gsym->visibility() == elfcpp::STV_PROTECTED
9 kx && parameters->options().shared())
9 kx || (gsym->type() == elfcpp::STT_GNU_IFUNC
9 kx && parameters->options().output_is_position_independent()))
9 kx got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
9 kx rel_dyn, elfcpp::R_386_GLOB_DAT);
9 kx else
9 kx {
9 kx // For a STT_GNU_IFUNC symbol we want to write the PLT
9 kx // offset into the GOT, so that function pointer
9 kx // comparisons work correctly.
9 kx bool is_new;
9 kx if (gsym->type() != elfcpp::STT_GNU_IFUNC)
9 kx is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
9 kx else
9 kx {
9 kx is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
9 kx // Tell the dynamic linker to use the PLT address
9 kx // when resolving relocations.
9 kx if (gsym->is_from_dynobj()
9 kx && !parameters->options().shared())
9 kx gsym->set_needs_dynsym_value();
9 kx }
9 kx if (is_new)
9 kx {
9 kx unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
9 kx rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
9 kx got, got_off);
9 kx }
9 kx }
9 kx }
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_PLT32:
9 kx // If the symbol is fully resolved, this is just a PC32 reloc.
9 kx // Otherwise we need a PLT entry.
9 kx if (gsym->final_value_is_known())
9 kx break;
9 kx // If building a shared library, we can also skip the PLT entry
9 kx // if the symbol is defined in the output file and is protected
9 kx // or hidden.
9 kx if (gsym->is_defined()
9 kx && !gsym->is_from_dynobj()
9 kx && !gsym->is_preemptible())
9 kx break;
9 kx target->make_plt_entry(symtab, layout, gsym);
9 kx break;
9 kx
9 kx case elfcpp::R_386_GOTOFF:
9 kx // A GOT-relative reference must resolve locally.
9 kx if (!gsym->is_defined())
9 kx gold_error(_("%s: relocation R_386_GOTOFF against undefined symbol %s"
9 kx " cannot be used when making a shared object"),
9 kx object->name().c_str(), gsym->name());
9 kx else if (gsym->is_from_dynobj())
9 kx gold_error(_("%s: relocation R_386_GOTOFF against external symbol %s"
9 kx " cannot be used when making a shared object"),
9 kx object->name().c_str(), gsym->name());
9 kx else if (gsym->is_preemptible())
9 kx gold_error(_("%s: relocation R_386_GOTOFF against preemptible symbol %s"
9 kx " cannot be used when making a shared object"),
9 kx object->name().c_str(), gsym->name());
9 kx // We need a GOT section.
9 kx target->got_section(symtab, layout);
9 kx break;
9 kx
9 kx case elfcpp::R_386_GOTPC:
9 kx // We need a GOT section.
9 kx target->got_section(symtab, layout);
9 kx break;
9 kx
9 kx // These are relocations which should only be seen by the
9 kx // dynamic linker, and should never be seen here.
9 kx case elfcpp::R_386_COPY:
9 kx case elfcpp::R_386_GLOB_DAT:
9 kx case elfcpp::R_386_JUMP_SLOT:
9 kx case elfcpp::R_386_RELATIVE:
9 kx case elfcpp::R_386_IRELATIVE:
9 kx case elfcpp::R_386_TLS_TPOFF:
9 kx case elfcpp::R_386_TLS_DTPMOD32:
9 kx case elfcpp::R_386_TLS_DTPOFF32:
9 kx case elfcpp::R_386_TLS_TPOFF32:
9 kx case elfcpp::R_386_TLS_DESC:
9 kx gold_error(_("%s: unexpected reloc %u in object file"),
9 kx object->name().c_str(), r_type);
9 kx break;
9 kx
9 kx // These are initial tls relocs, which are expected when
9 kx // linking.
9 kx case elfcpp::R_386_TLS_GD: // Global-dynamic
9 kx case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx case elfcpp::R_386_TLS_LDM: // Local-dynamic
9 kx case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
9 kx case elfcpp::R_386_TLS_IE: // Initial-exec
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx case elfcpp::R_386_TLS_LE: // Local-exec
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx {
9 kx const bool is_final = gsym->final_value_is_known();
9 kx const tls::Tls_optimization optimized_type
9 kx = Target_i386::optimize_tls_reloc(is_final, r_type);
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_TLS_GD: // Global-dynamic
9 kx if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Create a pair of GOT entries for the module index and
9 kx // dtv-relative offset.
9 kx Output_data_got<32, false>* got
9 kx = target->got_section(symtab, layout);
9 kx got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
9 kx target->rel_dyn_section(layout),
9 kx elfcpp::R_386_TLS_DTPMOD32,
9 kx elfcpp::R_386_TLS_DTPOFF32);
9 kx }
9 kx else if (optimized_type == tls::TLSOPT_TO_IE)
9 kx {
9 kx // Create a GOT entry for the tp-relative offset.
9 kx Output_data_got<32, false>* got
9 kx = target->got_section(symtab, layout);
9 kx got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
9 kx target->rel_dyn_section(layout),
9 kx elfcpp::R_386_TLS_TPOFF);
9 kx }
9 kx else if (optimized_type != tls::TLSOPT_TO_LE)
9 kx unsupported_reloc_global(object, r_type, gsym);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
9 kx target->define_tls_base_symbol(symtab, layout);
9 kx if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Create a double GOT entry with an R_386_TLS_DESC
9 kx // reloc. The R_386_TLS_DESC reloc is resolved
9 kx // lazily, so the GOT entry needs to be in an area in
9 kx // .got.plt, not .got. Call got_section to make sure
9 kx // the section has been created.
9 kx target->got_section(symtab, layout);
9 kx Output_data_got<32, false>* got = target->got_tlsdesc_section();
9 kx Reloc_section* rt = target->rel_tls_desc_section(layout);
9 kx got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
9 kx elfcpp::R_386_TLS_DESC, 0);
9 kx }
9 kx else if (optimized_type == tls::TLSOPT_TO_IE)
9 kx {
9 kx // Create a GOT entry for the tp-relative offset.
9 kx Output_data_got<32, false>* got
9 kx = target->got_section(symtab, layout);
9 kx got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
9 kx target->rel_dyn_section(layout),
9 kx elfcpp::R_386_TLS_TPOFF);
9 kx }
9 kx else if (optimized_type != tls::TLSOPT_TO_LE)
9 kx unsupported_reloc_global(object, r_type, gsym);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LDM: // Local-dynamic
9 kx if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Create a GOT entry for the module index.
9 kx target->got_mod_index_entry(symtab, layout, object);
9 kx }
9 kx else if (optimized_type != tls::TLSOPT_TO_LE)
9 kx unsupported_reloc_global(object, r_type, gsym);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_IE: // Initial-exec
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx layout->set_has_static_tls();
9 kx if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // For the R_386_TLS_IE relocation, we need to create a
9 kx // dynamic relocation when building a shared library.
9 kx if (r_type == elfcpp::R_386_TLS_IE
9 kx && parameters->options().shared())
9 kx {
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
9 kx output_section, object,
9 kx data_shndx,
9 kx reloc.get_r_offset());
9 kx }
9 kx // Create a GOT entry for the tp-relative offset.
9 kx Output_data_got<32, false>* got
9 kx = target->got_section(symtab, layout);
9 kx unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
9 kx ? elfcpp::R_386_TLS_TPOFF32
9 kx : elfcpp::R_386_TLS_TPOFF);
9 kx unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
9 kx ? GOT_TYPE_TLS_OFFSET
9 kx : GOT_TYPE_TLS_NOFFSET);
9 kx got->add_global_with_rel(gsym, got_type,
9 kx target->rel_dyn_section(layout),
9 kx dyn_r_type);
9 kx }
9 kx else if (optimized_type != tls::TLSOPT_TO_LE)
9 kx unsupported_reloc_global(object, r_type, gsym);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LE: // Local-exec
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx layout->set_has_static_tls();
9 kx if (parameters->options().shared())
9 kx {
9 kx // We need to create a dynamic relocation.
9 kx unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
9 kx ? elfcpp::R_386_TLS_TPOFF32
9 kx : elfcpp::R_386_TLS_TPOFF);
9 kx Reloc_section* rel_dyn = target->rel_dyn_section(layout);
9 kx rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
9 kx data_shndx, reloc.get_r_offset());
9 kx }
9 kx break;
9 kx
9 kx default:
9 kx gold_unreachable();
9 kx }
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_32PLT:
9 kx case elfcpp::R_386_TLS_GD_32:
9 kx case elfcpp::R_386_TLS_GD_PUSH:
9 kx case elfcpp::R_386_TLS_GD_CALL:
9 kx case elfcpp::R_386_TLS_GD_POP:
9 kx case elfcpp::R_386_TLS_LDM_32:
9 kx case elfcpp::R_386_TLS_LDM_PUSH:
9 kx case elfcpp::R_386_TLS_LDM_CALL:
9 kx case elfcpp::R_386_TLS_LDM_POP:
9 kx case elfcpp::R_386_USED_BY_INTEL_200:
9 kx default:
9 kx unsupported_reloc_global(object, r_type, gsym);
9 kx break;
9 kx }
9 kx }
9 kx
9 kx // Process relocations for gc.
9 kx
9 kx void
9 kx Target_i386::gc_process_relocs(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx unsigned int,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx size_t local_symbol_count,
9 kx const unsigned char* plocal_symbols)
9 kx {
9 kx gold::gc_process_relocs<32, false, Target_i386, Scan, Classify_reloc>(
9 kx symtab,
9 kx layout,
9 kx this,
9 kx object,
9 kx data_shndx,
9 kx prelocs,
9 kx reloc_count,
9 kx output_section,
9 kx needs_special_offset_handling,
9 kx local_symbol_count,
9 kx plocal_symbols);
9 kx }
9 kx
9 kx // Scan relocations for a section.
9 kx
9 kx void
9 kx Target_i386::scan_relocs(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx size_t local_symbol_count,
9 kx const unsigned char* plocal_symbols)
9 kx {
9 kx if (sh_type == elfcpp::SHT_RELA)
9 kx {
9 kx gold_error(_("%s: unsupported RELA reloc section"),
9 kx object->name().c_str());
9 kx return;
9 kx }
9 kx
9 kx gold::scan_relocs<32, false, Target_i386, Scan, Classify_reloc>(
9 kx symtab,
9 kx layout,
9 kx this,
9 kx object,
9 kx data_shndx,
9 kx prelocs,
9 kx reloc_count,
9 kx output_section,
9 kx needs_special_offset_handling,
9 kx local_symbol_count,
9 kx plocal_symbols);
9 kx }
9 kx
9 kx // Finalize the sections.
9 kx
9 kx void
9 kx Target_i386::do_finalize_sections(
9 kx Layout* layout,
9 kx const Input_objects*,
9 kx Symbol_table* symtab)
9 kx {
9 kx const Reloc_section* rel_plt = (this->plt_ == NULL
9 kx ? NULL
9 kx : this->plt_->rel_plt());
9 kx layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
9 kx this->rel_dyn_, true, false);
9 kx
9 kx // Emit any relocs we saved in an attempt to avoid generating COPY
9 kx // relocs.
9 kx if (this->copy_relocs_.any_saved_relocs())
9 kx this->copy_relocs_.emit(this->rel_dyn_section(layout));
9 kx
9 kx // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
9 kx // the .got.plt section.
9 kx Symbol* sym = this->global_offset_table_;
9 kx if (sym != NULL)
9 kx {
9 kx uint32_t data_size = this->got_plt_->current_data_size();
9 kx symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
9 kx }
9 kx
9 kx if (parameters->doing_static_link()
9 kx && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
9 kx {
9 kx // If linking statically, make sure that the __rel_iplt symbols
9 kx // were defined if necessary, even if we didn't create a PLT.
9 kx static const Define_symbol_in_segment syms[] =
9 kx {
9 kx {
9 kx "__rel_iplt_start", // name
9 kx elfcpp::PT_LOAD, // segment_type
9 kx elfcpp::PF_W, // segment_flags_set
9 kx elfcpp::PF(0), // segment_flags_clear
9 kx 0, // value
9 kx 0, // size
9 kx elfcpp::STT_NOTYPE, // type
9 kx elfcpp::STB_GLOBAL, // binding
9 kx elfcpp::STV_HIDDEN, // visibility
9 kx 0, // nonvis
9 kx Symbol::SEGMENT_START, // offset_from_base
9 kx true // only_if_ref
9 kx },
9 kx {
9 kx "__rel_iplt_end", // name
9 kx elfcpp::PT_LOAD, // segment_type
9 kx elfcpp::PF_W, // segment_flags_set
9 kx elfcpp::PF(0), // segment_flags_clear
9 kx 0, // value
9 kx 0, // size
9 kx elfcpp::STT_NOTYPE, // type
9 kx elfcpp::STB_GLOBAL, // binding
9 kx elfcpp::STV_HIDDEN, // visibility
9 kx 0, // nonvis
9 kx Symbol::SEGMENT_START, // offset_from_base
9 kx true // only_if_ref
9 kx }
9 kx };
9 kx
9 kx symtab->define_symbols(layout, 2, syms,
9 kx layout->script_options()->saw_sections_clause());
9 kx }
9 kx }
9 kx
9 kx // Return whether a direct absolute static relocation needs to be applied.
9 kx // In cases where Scan::local() or Scan::global() has created
9 kx // a dynamic relocation other than R_386_RELATIVE, the addend
9 kx // of the relocation is carried in the data, and we must not
9 kx // apply the static relocation.
9 kx
9 kx inline bool
9 kx Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
9 kx unsigned int r_type,
9 kx bool is_32bit,
9 kx Output_section* output_section)
9 kx {
9 kx // If the output section is not allocated, then we didn't call
9 kx // scan_relocs, we didn't create a dynamic reloc, and we must apply
9 kx // the reloc here.
9 kx if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
9 kx return true;
9 kx
9 kx int ref_flags = Scan::get_reference_flags(r_type);
9 kx
9 kx // For local symbols, we will have created a non-RELATIVE dynamic
9 kx // relocation only if (a) the output is position independent,
9 kx // (b) the relocation is absolute (not pc- or segment-relative), and
9 kx // (c) the relocation is not 32 bits wide.
9 kx if (gsym == NULL)
9 kx return !(parameters->options().output_is_position_independent()
9 kx && (ref_flags & Symbol::ABSOLUTE_REF)
9 kx && !is_32bit);
9 kx
9 kx // For global symbols, we use the same helper routines used in the
9 kx // scan pass. If we did not create a dynamic relocation, or if we
9 kx // created a RELATIVE dynamic relocation, we should apply the static
9 kx // relocation.
9 kx bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
9 kx bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
9 kx && gsym->can_use_relative_reloc(ref_flags
9 kx & Symbol::FUNCTION_CALL);
9 kx return !has_dyn || is_rel;
9 kx }
9 kx
9 kx // Perform a relocation.
9 kx
9 kx inline bool
9 kx Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
9 kx unsigned int,
9 kx Target_i386* target,
9 kx Output_section* output_section,
9 kx size_t relnum,
9 kx const unsigned char* preloc,
9 kx const Sized_symbol<32>* gsym,
9 kx const Symbol_value<32>* psymval,
9 kx unsigned char* view,
9 kx elfcpp::Elf_types<32>::Elf_Addr address,
9 kx section_size_type view_size)
9 kx {
9 kx const elfcpp::Rel<32, false> rel(preloc);
9 kx unsigned int r_type = elfcpp::elf_r_type<32>(rel.get_r_info());
9 kx
9 kx if (this->skip_call_tls_get_addr_)
9 kx {
9 kx if ((r_type != elfcpp::R_386_PLT32
9 kx && r_type != elfcpp::R_386_GOT32X
9 kx && r_type != elfcpp::R_386_PC32)
9 kx || gsym == NULL
9 kx || strcmp(gsym->name(), "___tls_get_addr") != 0)
9 kx {
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("missing expected TLS relocation"));
9 kx this->skip_call_tls_get_addr_ = false;
9 kx }
9 kx else
9 kx {
9 kx this->skip_call_tls_get_addr_ = false;
9 kx return false;
9 kx }
9 kx }
9 kx
9 kx if (view == NULL)
9 kx return true;
9 kx
9 kx const Sized_relobj_file<32, false>* object = relinfo->object;
9 kx
9 kx // Pick the value to use for symbols defined in shared objects.
9 kx Symbol_value<32> symval;
9 kx if (gsym != NULL
9 kx && gsym->type() == elfcpp::STT_GNU_IFUNC
9 kx && r_type == elfcpp::R_386_32
9 kx && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
9 kx && gsym->can_use_relative_reloc(false)
9 kx && !gsym->is_from_dynobj()
9 kx && !gsym->is_undefined()
9 kx && !gsym->is_preemptible())
9 kx {
9 kx // In this case we are generating a R_386_IRELATIVE reloc. We
9 kx // want to use the real value of the symbol, not the PLT offset.
9 kx }
9 kx else if (gsym != NULL
9 kx && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
9 kx {
9 kx symval.set_output_value(target->plt_address_for_global(gsym));
9 kx psymval = &symval;
9 kx }
9 kx else if (gsym == NULL && psymval->is_ifunc_symbol())
9 kx {
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9 kx if (object->local_has_plt_offset(r_sym))
9 kx {
9 kx symval.set_output_value(target->plt_address_for_local(object, r_sym));
9 kx psymval = &symval;
9 kx }
9 kx }
9 kx
9 kx bool baseless;
9 kx
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_NONE:
9 kx case elfcpp::R_386_GNU_VTINHERIT:
9 kx case elfcpp::R_386_GNU_VTENTRY:
9 kx break;
9 kx
9 kx case elfcpp::R_386_32:
9 kx if (should_apply_static_reloc(gsym, r_type, true, output_section))
9 kx Relocate_functions<32, false>::rel32(view, object, psymval);
9 kx break;
9 kx
9 kx case elfcpp::R_386_PC32:
9 kx if (should_apply_static_reloc(gsym, r_type, true, output_section))
9 kx Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
9 kx break;
9 kx
9 kx case elfcpp::R_386_16:
9 kx if (should_apply_static_reloc(gsym, r_type, false, output_section))
9 kx Relocate_functions<32, false>::rel16(view, object, psymval);
9 kx break;
9 kx
9 kx case elfcpp::R_386_PC16:
9 kx if (should_apply_static_reloc(gsym, r_type, false, output_section))
9 kx Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
9 kx break;
9 kx
9 kx case elfcpp::R_386_8:
9 kx if (should_apply_static_reloc(gsym, r_type, false, output_section))
9 kx Relocate_functions<32, false>::rel8(view, object, psymval);
9 kx break;
9 kx
9 kx case elfcpp::R_386_PC8:
9 kx if (should_apply_static_reloc(gsym, r_type, false, output_section))
9 kx Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
9 kx break;
9 kx
9 kx case elfcpp::R_386_PLT32:
9 kx gold_assert(gsym == NULL
9 kx || gsym->has_plt_offset()
9 kx || gsym->final_value_is_known()
9 kx || (gsym->is_defined()
9 kx && !gsym->is_from_dynobj()
9 kx && !gsym->is_preemptible()));
9 kx Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
9 kx break;
9 kx
9 kx case elfcpp::R_386_GOT32:
9 kx case elfcpp::R_386_GOT32X:
9 kx baseless = (view[-1] & 0xc7) == 0x5;
9 kx // R_386_GOT32 and R_386_GOT32X don't work without base register
9 kx // when generating a position-independent output file.
9 kx if (baseless
9 kx && parameters->options().output_is_position_independent())
9 kx {
9 kx if(gsym)
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("unexpected reloc %u against global symbol %s without base register in object file when generating a position-independent output file"),
9 kx r_type, gsym->demangled_name().c_str());
9 kx else
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("unexpected reloc %u against local symbol without base register in object file when generating a position-independent output file"),
9 kx r_type);
9 kx }
9 kx
9 kx // Convert
9 kx // mov foo@GOT(%reg), %reg
9 kx // to
9 kx // lea foo@GOTOFF(%reg), %reg
9 kx // if possible.
9 kx if (rel.get_r_offset() >= 2
9 kx && view[-2] == 0x8b
9 kx && ((gsym == NULL && !psymval->is_ifunc_symbol())
9 kx || (gsym != NULL
9 kx && Target_i386::can_convert_mov_to_lea(gsym))))
9 kx {
9 kx view[-2] = 0x8d;
9 kx elfcpp::Elf_types<32>::Elf_Addr value;
9 kx value = psymval->value(object, 0);
9 kx // Don't subtract the .got.plt section address for baseless
9 kx // addressing.
9 kx if (!baseless)
9 kx value -= target->got_plt_section()->address();
9 kx Relocate_functions<32, false>::rel32(view, value);
9 kx }
9 kx else
9 kx {
9 kx // The GOT pointer points to the end of the GOT section.
9 kx // We need to subtract the size of the GOT section to get
9 kx // the actual offset to use in the relocation.
9 kx unsigned int got_offset = 0;
9 kx if (gsym != NULL)
9 kx {
9 kx gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
9 kx got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
9 kx - target->got_size());
9 kx }
9 kx else
9 kx {
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9 kx gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
9 kx got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
9 kx - target->got_size());
9 kx }
9 kx // Add the .got.plt section address for baseless addressing.
9 kx if (baseless)
9 kx got_offset += target->got_plt_section()->address();
9 kx Relocate_functions<32, false>::rel32(view, got_offset);
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_GOTOFF:
9 kx {
9 kx elfcpp::Elf_types<32>::Elf_Addr reladdr;
9 kx reladdr = target->got_plt_section()->address();
9 kx Relocate_functions<32, false>::pcrel32(view, object, psymval, reladdr);
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_GOTPC:
9 kx {
9 kx elfcpp::Elf_types<32>::Elf_Addr value;
9 kx value = target->got_plt_section()->address();
9 kx Relocate_functions<32, false>::pcrel32(view, value, address);
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_COPY:
9 kx case elfcpp::R_386_GLOB_DAT:
9 kx case elfcpp::R_386_JUMP_SLOT:
9 kx case elfcpp::R_386_RELATIVE:
9 kx case elfcpp::R_386_IRELATIVE:
9 kx // These are outstanding tls relocs, which are unexpected when
9 kx // linking.
9 kx case elfcpp::R_386_TLS_TPOFF:
9 kx case elfcpp::R_386_TLS_DTPMOD32:
9 kx case elfcpp::R_386_TLS_DTPOFF32:
9 kx case elfcpp::R_386_TLS_TPOFF32:
9 kx case elfcpp::R_386_TLS_DESC:
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("unexpected reloc %u in object file"),
9 kx r_type);
9 kx break;
9 kx
9 kx // These are initial tls relocs, which are expected when
9 kx // linking.
9 kx case elfcpp::R_386_TLS_GD: // Global-dynamic
9 kx case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx case elfcpp::R_386_TLS_LDM: // Local-dynamic
9 kx case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
9 kx case elfcpp::R_386_TLS_IE: // Initial-exec
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx case elfcpp::R_386_TLS_LE: // Local-exec
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
9 kx view, address, view_size);
9 kx break;
9 kx
9 kx case elfcpp::R_386_32PLT:
9 kx case elfcpp::R_386_TLS_GD_32:
9 kx case elfcpp::R_386_TLS_GD_PUSH:
9 kx case elfcpp::R_386_TLS_GD_CALL:
9 kx case elfcpp::R_386_TLS_GD_POP:
9 kx case elfcpp::R_386_TLS_LDM_32:
9 kx case elfcpp::R_386_TLS_LDM_PUSH:
9 kx case elfcpp::R_386_TLS_LDM_CALL:
9 kx case elfcpp::R_386_TLS_LDM_POP:
9 kx case elfcpp::R_386_USED_BY_INTEL_200:
9 kx default:
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("unsupported reloc %u"),
9 kx r_type);
9 kx break;
9 kx }
9 kx
9 kx return true;
9 kx }
9 kx
9 kx // Perform a TLS relocation.
9 kx
9 kx inline void
9 kx Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
9 kx Target_i386* target,
9 kx size_t relnum,
9 kx const elfcpp::Rel<32, false>& rel,
9 kx unsigned int r_type,
9 kx const Sized_symbol<32>* gsym,
9 kx const Symbol_value<32>* psymval,
9 kx unsigned char* view,
9 kx elfcpp::Elf_types<32>::Elf_Addr,
9 kx section_size_type view_size)
9 kx {
9 kx Output_segment* tls_segment = relinfo->layout->tls_segment();
9 kx
9 kx const Sized_relobj_file<32, false>* object = relinfo->object;
9 kx
9 kx elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
9 kx
9 kx const bool is_final = (gsym == NULL
9 kx ? !parameters->options().shared()
9 kx : gsym->final_value_is_known());
9 kx const tls::Tls_optimization optimized_type
9 kx = Target_i386::optimize_tls_reloc(is_final, r_type);
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_TLS_GD: // Global-dynamic
9 kx if (optimized_type == tls::TLSOPT_TO_LE)
9 kx {
9 kx if (tls_segment == NULL)
9 kx {
9 kx gold_assert(parameters->errors()->error_count() > 0
9 kx || issue_undefined_symbol_error(gsym));
9 kx return;
9 kx }
9 kx this->tls_gd_to_le(relinfo, relnum, tls_segment,
9 kx rel, r_type, value, view,
9 kx view_size);
9 kx break;
9 kx }
9 kx else
9 kx {
9 kx unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
9 kx ? GOT_TYPE_TLS_NOFFSET
9 kx : GOT_TYPE_TLS_PAIR);
9 kx unsigned int got_offset;
9 kx if (gsym != NULL)
9 kx {
9 kx gold_assert(gsym->has_got_offset(got_type));
9 kx got_offset = gsym->got_offset(got_type) - target->got_size();
9 kx }
9 kx else
9 kx {
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9 kx gold_assert(object->local_has_got_offset(r_sym, got_type));
9 kx got_offset = (object->local_got_offset(r_sym, got_type)
9 kx - target->got_size());
9 kx }
9 kx if (optimized_type == tls::TLSOPT_TO_IE)
9 kx {
9 kx this->tls_gd_to_ie(relinfo, relnum, rel, r_type,
9 kx got_offset, view, view_size);
9 kx break;
9 kx }
9 kx else if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Relocate the field with the offset of the pair of GOT
9 kx // entries.
9 kx Relocate_functions<32, false>::rel32(view, got_offset);
9 kx break;
9 kx }
9 kx }
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("unsupported reloc %u"),
9 kx r_type);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
9 kx if (optimized_type == tls::TLSOPT_TO_LE)
9 kx {
9 kx if (tls_segment == NULL)
9 kx {
9 kx gold_assert(parameters->errors()->error_count() > 0
9 kx || issue_undefined_symbol_error(gsym));
9 kx return;
9 kx }
9 kx this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
9 kx rel, r_type, value, view,
9 kx view_size);
9 kx break;
9 kx }
9 kx else
9 kx {
9 kx unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
9 kx ? GOT_TYPE_TLS_NOFFSET
9 kx : GOT_TYPE_TLS_DESC);
9 kx unsigned int got_offset = 0;
9 kx if (r_type == elfcpp::R_386_TLS_GOTDESC
9 kx && optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // We created GOT entries in the .got.tlsdesc portion of
9 kx // the .got.plt section, but the offset stored in the
9 kx // symbol is the offset within .got.tlsdesc.
9 kx got_offset = (target->got_size()
9 kx + target->got_plt_section()->data_size());
9 kx }
9 kx if (gsym != NULL)
9 kx {
9 kx gold_assert(gsym->has_got_offset(got_type));
9 kx got_offset += gsym->got_offset(got_type) - target->got_size();
9 kx }
9 kx else
9 kx {
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9 kx gold_assert(object->local_has_got_offset(r_sym, got_type));
9 kx got_offset += (object->local_got_offset(r_sym, got_type)
9 kx - target->got_size());
9 kx }
9 kx if (optimized_type == tls::TLSOPT_TO_IE)
9 kx {
9 kx this->tls_desc_gd_to_ie(relinfo, relnum, rel, r_type,
9 kx got_offset, view, view_size);
9 kx break;
9 kx }
9 kx else if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx if (r_type == elfcpp::R_386_TLS_GOTDESC)
9 kx {
9 kx // Relocate the field with the offset of the pair of GOT
9 kx // entries.
9 kx Relocate_functions<32, false>::rel32(view, got_offset);
9 kx }
9 kx break;
9 kx }
9 kx }
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("unsupported reloc %u"),
9 kx r_type);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LDM: // Local-dynamic
9 kx if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
9 kx {
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("both SUN and GNU model "
9 kx "TLS relocations"));
9 kx break;
9 kx }
9 kx this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
9 kx if (optimized_type == tls::TLSOPT_TO_LE)
9 kx {
9 kx if (tls_segment == NULL)
9 kx {
9 kx gold_assert(parameters->errors()->error_count() > 0
9 kx || issue_undefined_symbol_error(gsym));
9 kx return;
9 kx }
9 kx this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
9 kx value, view, view_size);
9 kx break;
9 kx }
9 kx else if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Relocate the field with the offset of the GOT entry for
9 kx // the module index.
9 kx unsigned int got_offset;
9 kx got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
9 kx - target->got_size());
9 kx Relocate_functions<32, false>::rel32(view, got_offset);
9 kx break;
9 kx }
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("unsupported reloc %u"),
9 kx r_type);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
9 kx if (optimized_type == tls::TLSOPT_TO_LE)
9 kx {
9 kx // This reloc can appear in debugging sections, in which
9 kx // case we must not convert to local-exec. We decide what
9 kx // to do based on whether the section is marked as
9 kx // containing executable code. That is what the GNU linker
9 kx // does as well.
9 kx elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
9 kx if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
9 kx {
9 kx if (tls_segment == NULL)
9 kx {
9 kx gold_assert(parameters->errors()->error_count() > 0
9 kx || issue_undefined_symbol_error(gsym));
9 kx return;
9 kx }
9 kx value -= tls_segment->memsz();
9 kx }
9 kx }
9 kx Relocate_functions<32, false>::rel32(view, value);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_IE: // Initial-exec
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx if (optimized_type == tls::TLSOPT_TO_LE)
9 kx {
9 kx if (tls_segment == NULL)
9 kx {
9 kx gold_assert(parameters->errors()->error_count() > 0
9 kx || issue_undefined_symbol_error(gsym));
9 kx return;
9 kx }
9 kx Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
9 kx rel, r_type, value, view,
9 kx view_size);
9 kx break;
9 kx }
9 kx else if (optimized_type == tls::TLSOPT_NONE)
9 kx {
9 kx // Relocate the field with the offset of the GOT entry for
9 kx // the tp-relative offset of the symbol.
9 kx unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
9 kx ? GOT_TYPE_TLS_OFFSET
9 kx : GOT_TYPE_TLS_NOFFSET);
9 kx unsigned int got_offset;
9 kx if (gsym != NULL)
9 kx {
9 kx gold_assert(gsym->has_got_offset(got_type));
9 kx got_offset = gsym->got_offset(got_type);
9 kx }
9 kx else
9 kx {
9 kx unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
9 kx gold_assert(object->local_has_got_offset(r_sym, got_type));
9 kx got_offset = object->local_got_offset(r_sym, got_type);
9 kx }
9 kx // For the R_386_TLS_IE relocation, we need to apply the
9 kx // absolute address of the GOT entry.
9 kx if (r_type == elfcpp::R_386_TLS_IE)
9 kx got_offset += target->got_plt_section()->address();
9 kx // All GOT offsets are relative to the end of the GOT.
9 kx got_offset -= target->got_size();
9 kx Relocate_functions<32, false>::rel32(view, got_offset);
9 kx break;
9 kx }
9 kx gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
9 kx _("unsupported reloc %u"),
9 kx r_type);
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LE: // Local-exec
9 kx // If we're creating a shared library, a dynamic relocation will
9 kx // have been created for this location, so do not apply it now.
9 kx if (!parameters->options().shared())
9 kx {
9 kx if (tls_segment == NULL)
9 kx {
9 kx gold_assert(parameters->errors()->error_count() > 0
9 kx || issue_undefined_symbol_error(gsym));
9 kx return;
9 kx }
9 kx value -= tls_segment->memsz();
9 kx Relocate_functions<32, false>::rel32(view, value);
9 kx }
9 kx break;
9 kx
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx // If we're creating a shared library, a dynamic relocation will
9 kx // have been created for this location, so do not apply it now.
9 kx if (!parameters->options().shared())
9 kx {
9 kx if (tls_segment == NULL)
9 kx {
9 kx gold_assert(parameters->errors()->error_count() > 0
9 kx || issue_undefined_symbol_error(gsym));
9 kx return;
9 kx }
9 kx value = tls_segment->memsz() - value;
9 kx Relocate_functions<32, false>::rel32(view, value);
9 kx }
9 kx break;
9 kx }
9 kx }
9 kx
9 kx // Do a relocation in which we convert a TLS General-Dynamic to a
9 kx // Local-Exec.
9 kx
9 kx inline void
9 kx Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
9 kx size_t relnum,
9 kx Output_segment* tls_segment,
9 kx const elfcpp::Rel<32, false>& rel,
9 kx unsigned int,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size)
9 kx {
9 kx // leal foo(,%ebx,1),%eax; call ___tls_get_addr@PLT
9 kx // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
9 kx // leal foo(%ebx),%eax; call ___tls_get_addr@PLT
9 kx // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
9 kx // leal foo(%reg),%eax; call *___tls_get_addr@GOT(%reg)
9 kx // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
9 kx
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
9 kx
9 kx unsigned char op1 = view[-1];
9 kx unsigned char op2 = view[-2];
9 kx unsigned char op3 = view[4];
9 kx
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx op2 == 0x8d || op2 == 0x04);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx op3 == 0xe8 || op3 == 0xff);
9 kx
9 kx int roff = 5;
9 kx
9 kx if (op2 == 0x04)
9 kx {
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
9 kx memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
9 kx }
9 kx else
9 kx {
9 kx unsigned char reg = op1 & 7;
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx ((op1 & 0xf8) == 0x80
9 kx && reg != 4
9 kx && reg != 0
9 kx && (op3 == 0xe8 || (view[5] & 0x7) == reg)));
9 kx if (op3 == 0xff
9 kx || (rel.get_r_offset() + 9 < view_size
9 kx && view[9] == 0x90))
9 kx {
9 kx // There is an indirect call or a trailing nop. Use the size
9 kx // byte subl.
9 kx memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
9 kx roff = 6;
9 kx }
9 kx else
9 kx {
9 kx // Use the five byte subl.
9 kx memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
9 kx }
9 kx }
9 kx
9 kx value = tls_segment->memsz() - value;
9 kx Relocate_functions<32, false>::rel32(view + roff, value);
9 kx
9 kx // The next reloc should be a PLT32 reloc against __tls_get_addr.
9 kx // We can skip it.
9 kx this->skip_call_tls_get_addr_ = true;
9 kx }
9 kx
9 kx // Do a relocation in which we convert a TLS General-Dynamic to an
9 kx // Initial-Exec.
9 kx
9 kx inline void
9 kx Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
9 kx size_t relnum,
9 kx const elfcpp::Rel<32, false>& rel,
9 kx unsigned int,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size)
9 kx {
9 kx // leal foo(,%ebx,1),%eax; call ___tls_get_addr@PLT
9 kx // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
9 kx // leal foo(%ebx),%eax; call ___tls_get_addr@PLT; nop
9 kx // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
9 kx // leal foo(%reg),%eax; call *___tls_get_addr@GOT(%reg)
9 kx // ==> movl %gs:0,%eax; addl foo@gotntpoff(%reg),%eax
9 kx
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
9 kx
9 kx unsigned char op1 = view[-1];
9 kx unsigned char op2 = view[-2];
9 kx unsigned char op3 = view[4];
9 kx
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx op2 == 0x8d || op2 == 0x04);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx op3 == 0xe8 || op3 == 0xff);
9 kx
9 kx int roff;
9 kx
9 kx if (op2 == 0x04)
9 kx {
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
9 kx roff = 5;
9 kx }
9 kx else
9 kx {
9 kx unsigned char reg = op1 & 7;
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 10);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx ((op1 & 0xf8) == 0x80
9 kx && reg != 4
9 kx && reg != 0
9 kx && ((op3 == 0xe8 && view[9] == 0x90)
9 kx || (view[5] & 0x7) == reg)));
9 kx roff = 6;
9 kx }
9 kx
9 kx memcpy(view + roff - 8, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
9 kx Relocate_functions<32, false>::rel32(view + roff, value);
9 kx
9 kx // The next reloc should be a PLT32 reloc against __tls_get_addr.
9 kx // We can skip it.
9 kx this->skip_call_tls_get_addr_ = true;
9 kx }
9 kx
9 kx // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
9 kx // General-Dynamic to a Local-Exec.
9 kx
9 kx inline void
9 kx Target_i386::Relocate::tls_desc_gd_to_le(
9 kx const Relocate_info<32, false>* relinfo,
9 kx size_t relnum,
9 kx Output_segment* tls_segment,
9 kx const elfcpp::Rel<32, false>& rel,
9 kx unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size)
9 kx {
9 kx if (r_type == elfcpp::R_386_TLS_GOTDESC)
9 kx {
9 kx // leal foo@TLSDESC(%ebx), %eax
9 kx // ==> leal foo@NTPOFF, %eax
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx view[-2] == 0x8d && view[-1] == 0x83);
9 kx view[-1] = 0x05;
9 kx value -= tls_segment->memsz();
9 kx Relocate_functions<32, false>::rel32(view, value);
9 kx }
9 kx else
9 kx {
9 kx // call *foo@TLSCALL(%eax)
9 kx // ==> nop; nop
9 kx gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx view[0] == 0xff && view[1] == 0x10);
9 kx view[0] = 0x66;
9 kx view[1] = 0x90;
9 kx }
9 kx }
9 kx
9 kx // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
9 kx // General-Dynamic to an Initial-Exec.
9 kx
9 kx inline void
9 kx Target_i386::Relocate::tls_desc_gd_to_ie(
9 kx const Relocate_info<32, false>* relinfo,
9 kx size_t relnum,
9 kx const elfcpp::Rel<32, false>& rel,
9 kx unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size)
9 kx {
9 kx if (r_type == elfcpp::R_386_TLS_GOTDESC)
9 kx {
9 kx // leal foo@TLSDESC(%ebx), %eax
9 kx // ==> movl foo@GOTNTPOFF(%ebx), %eax
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx view[-2] == 0x8d && view[-1] == 0x83);
9 kx view[-2] = 0x8b;
9 kx Relocate_functions<32, false>::rel32(view, value);
9 kx }
9 kx else
9 kx {
9 kx // call *foo@TLSCALL(%eax)
9 kx // ==> nop; nop
9 kx gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx view[0] == 0xff && view[1] == 0x10);
9 kx view[0] = 0x66;
9 kx view[1] = 0x90;
9 kx }
9 kx }
9 kx
9 kx // Do a relocation in which we convert a TLS Local-Dynamic to a
9 kx // Local-Exec.
9 kx
9 kx inline void
9 kx Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
9 kx size_t relnum,
9 kx Output_segment*,
9 kx const elfcpp::Rel<32, false>& rel,
9 kx unsigned int,
9 kx elfcpp::Elf_types<32>::Elf_Addr,
9 kx unsigned char* view,
9 kx section_size_type view_size)
9 kx {
9 kx // leal foo(%ebx), %eax; call ___tls_get_addr@PLT
9 kx // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
9 kx // leal foo(%reg), %eax; call call *___tls_get_addr@GOT(%reg)
9 kx // ==> movl %gs:0,%eax; leal (%esi),%esi
9 kx
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
9 kx
9 kx unsigned char op1 = view[-1];
9 kx unsigned char op2 = view[-2];
9 kx unsigned char op3 = view[4];
9 kx
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx op3 == 0xe8 || op3 == 0xff);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size,
9 kx op3 == 0xe8 ? 9 : 10);
9 kx
9 kx // FIXME: Does this test really always pass?
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x8d);
9 kx
9 kx unsigned char reg = op1 & 7;
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx ((op1 & 0xf8) == 0x80
9 kx && reg != 4
9 kx && reg != 0
9 kx && (op3 == 0xe8 || (view[5] & 0x7) == reg)));
9 kx
9 kx if (op3 == 0xe8)
9 kx memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
9 kx else
9 kx memcpy(view - 2, "\x65\xa1\0\0\0\0\x8d\xb6\0\0\0\0", 12);
9 kx
9 kx // The next reloc should be a PLT32 reloc against __tls_get_addr.
9 kx // We can skip it.
9 kx this->skip_call_tls_get_addr_ = true;
9 kx }
9 kx
9 kx // Do a relocation in which we convert a TLS Initial-Exec to a
9 kx // Local-Exec.
9 kx
9 kx inline void
9 kx Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
9 kx size_t relnum,
9 kx Output_segment* tls_segment,
9 kx const elfcpp::Rel<32, false>& rel,
9 kx unsigned int r_type,
9 kx elfcpp::Elf_types<32>::Elf_Addr value,
9 kx unsigned char* view,
9 kx section_size_type view_size)
9 kx {
9 kx // We have to actually change the instructions, which means that we
9 kx // need to examine the opcodes to figure out which instruction we
9 kx // are looking at.
9 kx if (r_type == elfcpp::R_386_TLS_IE)
9 kx {
9 kx // movl %gs:XX,%eax ==> movl $YY,%eax
9 kx // movl %gs:XX,%reg ==> movl $YY,%reg
9 kx // addl %gs:XX,%reg ==> addl $YY,%reg
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
9 kx
9 kx unsigned char op1 = view[-1];
9 kx if (op1 == 0xa1)
9 kx {
9 kx // movl XX,%eax ==> movl $YY,%eax
9 kx view[-1] = 0xb8;
9 kx }
9 kx else
9 kx {
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
9 kx
9 kx unsigned char op2 = view[-2];
9 kx if (op2 == 0x8b)
9 kx {
9 kx // movl XX,%reg ==> movl $YY,%reg
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx (op1 & 0xc7) == 0x05);
9 kx view[-2] = 0xc7;
9 kx view[-1] = 0xc0 | ((op1 >> 3) & 7);
9 kx }
9 kx else if (op2 == 0x03)
9 kx {
9 kx // addl XX,%reg ==> addl $YY,%reg
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx (op1 & 0xc7) == 0x05);
9 kx view[-2] = 0x81;
9 kx view[-1] = 0xc0 | ((op1 >> 3) & 7);
9 kx }
9 kx else
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
9 kx }
9 kx }
9 kx else
9 kx {
9 kx // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
9 kx // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
9 kx // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
9 kx tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
9 kx
9 kx unsigned char op1 = view[-1];
9 kx unsigned char op2 = view[-2];
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(),
9 kx (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
9 kx if (op2 == 0x8b)
9 kx {
9 kx // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
9 kx view[-2] = 0xc7;
9 kx view[-1] = 0xc0 | ((op1 >> 3) & 7);
9 kx }
9 kx else if (op2 == 0x2b)
9 kx {
9 kx // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
9 kx view[-2] = 0x81;
9 kx view[-1] = 0xe8 | ((op1 >> 3) & 7);
9 kx }
9 kx else if (op2 == 0x03)
9 kx {
9 kx // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
9 kx view[-2] = 0x81;
9 kx view[-1] = 0xc0 | ((op1 >> 3) & 7);
9 kx }
9 kx else
9 kx tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
9 kx }
9 kx
9 kx value = tls_segment->memsz() - value;
9 kx if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
9 kx value = - value;
9 kx
9 kx Relocate_functions<32, false>::rel32(view, value);
9 kx }
9 kx
9 kx // Relocate section data.
9 kx
9 kx void
9 kx Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx unsigned char* view,
9 kx elfcpp::Elf_types<32>::Elf_Addr address,
9 kx section_size_type view_size,
9 kx const Reloc_symbol_changes* reloc_symbol_changes)
9 kx {
9 kx gold_assert(sh_type == elfcpp::SHT_REL);
9 kx
9 kx gold::relocate_section<32, false, Target_i386, Relocate,
9 kx gold::Default_comdat_behavior, Classify_reloc>(
9 kx relinfo,
9 kx this,
9 kx prelocs,
9 kx reloc_count,
9 kx output_section,
9 kx needs_special_offset_handling,
9 kx view,
9 kx address,
9 kx view_size,
9 kx reloc_symbol_changes);
9 kx }
9 kx
9 kx // Return the size of a relocation while scanning during a relocatable
9 kx // link.
9 kx
9 kx unsigned int
9 kx Target_i386::Classify_reloc::get_size_for_reloc(
9 kx unsigned int r_type,
9 kx Relobj* object)
9 kx {
9 kx switch (r_type)
9 kx {
9 kx case elfcpp::R_386_NONE:
9 kx case elfcpp::R_386_GNU_VTINHERIT:
9 kx case elfcpp::R_386_GNU_VTENTRY:
9 kx case elfcpp::R_386_TLS_GD: // Global-dynamic
9 kx case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
9 kx case elfcpp::R_386_TLS_DESC_CALL:
9 kx case elfcpp::R_386_TLS_LDM: // Local-dynamic
9 kx case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
9 kx case elfcpp::R_386_TLS_IE: // Initial-exec
9 kx case elfcpp::R_386_TLS_IE_32:
9 kx case elfcpp::R_386_TLS_GOTIE:
9 kx case elfcpp::R_386_TLS_LE: // Local-exec
9 kx case elfcpp::R_386_TLS_LE_32:
9 kx return 0;
9 kx
9 kx case elfcpp::R_386_32:
9 kx case elfcpp::R_386_PC32:
9 kx case elfcpp::R_386_GOT32:
9 kx case elfcpp::R_386_GOT32X:
9 kx case elfcpp::R_386_PLT32:
9 kx case elfcpp::R_386_GOTOFF:
9 kx case elfcpp::R_386_GOTPC:
9 kx return 4;
9 kx
9 kx case elfcpp::R_386_16:
9 kx case elfcpp::R_386_PC16:
9 kx return 2;
9 kx
9 kx case elfcpp::R_386_8:
9 kx case elfcpp::R_386_PC8:
9 kx return 1;
9 kx
9 kx // These are relocations which should only be seen by the
9 kx // dynamic linker, and should never be seen here.
9 kx case elfcpp::R_386_COPY:
9 kx case elfcpp::R_386_GLOB_DAT:
9 kx case elfcpp::R_386_JUMP_SLOT:
9 kx case elfcpp::R_386_RELATIVE:
9 kx case elfcpp::R_386_IRELATIVE:
9 kx case elfcpp::R_386_TLS_TPOFF:
9 kx case elfcpp::R_386_TLS_DTPMOD32:
9 kx case elfcpp::R_386_TLS_DTPOFF32:
9 kx case elfcpp::R_386_TLS_TPOFF32:
9 kx case elfcpp::R_386_TLS_DESC:
9 kx object->error(_("unexpected reloc %u in object file"), r_type);
9 kx return 0;
9 kx
9 kx case elfcpp::R_386_32PLT:
9 kx case elfcpp::R_386_TLS_GD_32:
9 kx case elfcpp::R_386_TLS_GD_PUSH:
9 kx case elfcpp::R_386_TLS_GD_CALL:
9 kx case elfcpp::R_386_TLS_GD_POP:
9 kx case elfcpp::R_386_TLS_LDM_32:
9 kx case elfcpp::R_386_TLS_LDM_PUSH:
9 kx case elfcpp::R_386_TLS_LDM_CALL:
9 kx case elfcpp::R_386_TLS_LDM_POP:
9 kx case elfcpp::R_386_USED_BY_INTEL_200:
9 kx default:
9 kx object->error(_("unsupported reloc %u in object file"), r_type);
9 kx return 0;
9 kx }
9 kx }
9 kx
9 kx // Scan the relocs during a relocatable link.
9 kx
9 kx void
9 kx Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx size_t local_symbol_count,
9 kx const unsigned char* plocal_symbols,
9 kx Relocatable_relocs* rr)
9 kx {
9 kx typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
9 kx Scan_relocatable_relocs;
9 kx
9 kx gold_assert(sh_type == elfcpp::SHT_REL);
9 kx
9 kx gold::scan_relocatable_relocs<32, false, Scan_relocatable_relocs>(
9 kx symtab,
9 kx layout,
9 kx object,
9 kx data_shndx,
9 kx prelocs,
9 kx reloc_count,
9 kx output_section,
9 kx needs_special_offset_handling,
9 kx local_symbol_count,
9 kx plocal_symbols,
9 kx rr);
9 kx }
9 kx
9 kx // Scan the relocs for --emit-relocs.
9 kx
9 kx void
9 kx Target_i386::emit_relocs_scan(Symbol_table* symtab,
9 kx Layout* layout,
9 kx Sized_relobj_file<32, false>* object,
9 kx unsigned int data_shndx,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx bool needs_special_offset_handling,
9 kx size_t local_symbol_count,
9 kx const unsigned char* plocal_syms,
9 kx Relocatable_relocs* rr)
9 kx {
9 kx typedef gold::Default_classify_reloc<elfcpp::SHT_REL, 32, false>
9 kx Classify_reloc;
9 kx typedef gold::Default_emit_relocs_strategy<Classify_reloc>
9 kx Emit_relocs_strategy;
9 kx
9 kx gold_assert(sh_type == elfcpp::SHT_REL);
9 kx
9 kx gold::scan_relocatable_relocs<32, false, Emit_relocs_strategy>(
9 kx symtab,
9 kx layout,
9 kx object,
9 kx data_shndx,
9 kx prelocs,
9 kx reloc_count,
9 kx output_section,
9 kx needs_special_offset_handling,
9 kx local_symbol_count,
9 kx plocal_syms,
9 kx rr);
9 kx }
9 kx
9 kx // Emit relocations for a section.
9 kx
9 kx void
9 kx Target_i386::relocate_relocs(
9 kx const Relocate_info<32, false>* relinfo,
9 kx unsigned int sh_type,
9 kx const unsigned char* prelocs,
9 kx size_t reloc_count,
9 kx Output_section* output_section,
9 kx elfcpp::Elf_types<32>::Elf_Off offset_in_output_section,
9 kx unsigned char* view,
9 kx elfcpp::Elf_types<32>::Elf_Addr view_address,
9 kx section_size_type view_size,
9 kx unsigned char* reloc_view,
9 kx section_size_type reloc_view_size)
9 kx {
9 kx gold_assert(sh_type == elfcpp::SHT_REL);
9 kx
9 kx gold::relocate_relocs<32, false, Classify_reloc>(
9 kx relinfo,
9 kx prelocs,
9 kx reloc_count,
9 kx output_section,
9 kx offset_in_output_section,
9 kx view,
9 kx view_address,
9 kx view_size,
9 kx reloc_view,
9 kx reloc_view_size);
9 kx }
9 kx
9 kx // Return the value to use for a dynamic which requires special
9 kx // treatment. This is how we support equality comparisons of function
9 kx // pointers across shared library boundaries, as described in the
9 kx // processor specific ABI supplement.
9 kx
9 kx uint64_t
9 kx Target_i386::do_dynsym_value(const Symbol* gsym) const
9 kx {
9 kx gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
9 kx return this->plt_address_for_global(gsym);
9 kx }
9 kx
9 kx // Return a string used to fill a code section with nops to take up
9 kx // the specified length.
9 kx
9 kx std::string
9 kx Target_i386::do_code_fill(section_size_type length) const
9 kx {
9 kx if (length >= 16)
9 kx {
9 kx // Build a jmp instruction to skip over the bytes.
9 kx unsigned char jmp[5];
9 kx jmp[0] = 0xe9;
9 kx elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
9 kx return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
9 kx + std::string(length - 5, static_cast<char>(0x90)));
9 kx }
9 kx
9 kx // Nop sequences of various lengths.
9 kx const char nop1[1] = { '\x90' }; // nop
9 kx const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
9 kx const char nop3[3] = { '\x8d', '\x76', '\x00' }; // leal 0(%esi),%esi
9 kx const char nop4[4] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
9 kx '\x00'};
9 kx const char nop5[5] = { '\x90', '\x8d', '\x74', // nop
9 kx '\x26', '\x00' }; // leal 0(%esi,1),%esi
9 kx const char nop6[6] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
9 kx '\x00', '\x00', '\x00' };
9 kx const char nop7[7] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
9 kx '\x00', '\x00', '\x00',
9 kx '\x00' };
9 kx const char nop8[8] = { '\x90', '\x8d', '\xb4', // nop
9 kx '\x26', '\x00', '\x00', // leal 0L(%esi,1),%esi
9 kx '\x00', '\x00' };
9 kx const char nop9[9] = { '\x89', '\xf6', '\x8d', // movl %esi,%esi
9 kx '\xbc', '\x27', '\x00', // leal 0L(%edi,1),%edi
9 kx '\x00', '\x00', '\x00' };
9 kx const char nop10[10] = { '\x8d', '\x76', '\x00', // leal 0(%esi),%esi
9 kx '\x8d', '\xbc', '\x27', // leal 0L(%edi,1),%edi
9 kx '\x00', '\x00', '\x00',
9 kx '\x00' };
9 kx const char nop11[11] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
9 kx '\x00', '\x8d', '\xbc', // leal 0L(%edi,1),%edi
9 kx '\x27', '\x00', '\x00',
9 kx '\x00', '\x00' };
9 kx const char nop12[12] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
9 kx '\x00', '\x00', '\x00', // leal 0L(%edi),%edi
9 kx '\x8d', '\xbf', '\x00',
9 kx '\x00', '\x00', '\x00' };
9 kx const char nop13[13] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
9 kx '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
9 kx '\x8d', '\xbc', '\x27',
9 kx '\x00', '\x00', '\x00',
9 kx '\x00' };
9 kx const char nop14[14] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
9 kx '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
9 kx '\x00', '\x8d', '\xbc',
9 kx '\x27', '\x00', '\x00',
9 kx '\x00', '\x00' };
9 kx const char nop15[15] = { '\xeb', '\x0d', '\x90', // jmp .+15
9 kx '\x90', '\x90', '\x90', // nop,nop,nop,...
9 kx '\x90', '\x90', '\x90',
9 kx '\x90', '\x90', '\x90',
9 kx '\x90', '\x90', '\x90' };
9 kx
9 kx const char* nops[16] = {
9 kx NULL,
9 kx nop1, nop2, nop3, nop4, nop5, nop6, nop7,
9 kx nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
9 kx };
9 kx
9 kx return std::string(nops[length], length);
9 kx }
9 kx
9 kx // Return the value to use for the base of a DW_EH_PE_datarel offset
9 kx // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
9 kx // assembler can not write out the difference between two labels in
9 kx // different sections, so instead of using a pc-relative value they
9 kx // use an offset from the GOT.
9 kx
9 kx uint64_t
9 kx Target_i386::do_ehframe_datarel_base() const
9 kx {
9 kx gold_assert(this->global_offset_table_ != NULL);
9 kx Symbol* sym = this->global_offset_table_;
9 kx Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
9 kx return ssym->value();
9 kx }
9 kx
9 kx // Return whether SYM should be treated as a call to a non-split
9 kx // function. We don't want that to be true of a call to a
9 kx // get_pc_thunk function.
9 kx
9 kx bool
9 kx Target_i386::do_is_call_to_non_split(const Symbol* sym,
9 kx const unsigned char*,
9 kx const unsigned char*,
9 kx section_size_type) const
9 kx {
9 kx return (sym->type() == elfcpp::STT_FUNC
9 kx && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
9 kx }
9 kx
9 kx // FNOFFSET in section SHNDX in OBJECT is the start of a function
9 kx // compiled with -fsplit-stack. The function calls non-split-stack
9 kx // code. We have to change the function so that it always ensures
9 kx // that it has enough stack space to run some random function.
9 kx
9 kx void
9 kx Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
9 kx section_offset_type fnoffset,
9 kx section_size_type fnsize,
9 kx const unsigned char*,
9 kx size_t,
9 kx unsigned char* view,
9 kx section_size_type view_size,
9 kx std::string* from,
9 kx std::string* to) const
9 kx {
9 kx // The function starts with a comparison of the stack pointer and a
9 kx // field in the TCB. This is followed by a jump.
9 kx
9 kx // cmp %gs:NN,%esp
9 kx if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
9 kx && fnsize > 7)
9 kx {
9 kx // We will call __morestack if the carry flag is set after this
9 kx // comparison. We turn the comparison into an stc instruction
9 kx // and some nops.
9 kx view[fnoffset] = '\xf9';
9 kx this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
9 kx }
9 kx // lea NN(%esp),%ecx
9 kx // lea NN(%esp),%edx
9 kx else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
9 kx || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
9 kx && fnsize > 7)
9 kx {
9 kx // This is loading an offset from the stack pointer for a
9 kx // comparison. The offset is negative, so we decrease the
9 kx // offset by the amount of space we need for the stack. This
9 kx // means we will avoid calling __morestack if there happens to
9 kx // be plenty of space on the stack already.
9 kx unsigned char* pval = view + fnoffset + 3;
9 kx uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
9 kx val -= parameters->options().split_stack_adjust_size();
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
9 kx }
9 kx else
9 kx {
9 kx if (!object->has_no_split_stack())
9 kx object->error(_("failed to match split-stack sequence at "
9 kx "section %u offset %0zx"),
9 kx shndx, static_cast<size_t>(fnoffset));
9 kx return;
9 kx }
9 kx
9 kx // We have to change the function so that it calls
9 kx // __morestack_non_split instead of __morestack. The former will
9 kx // allocate additional stack space.
9 kx *from = "__morestack";
9 kx *to = "__morestack_non_split";
9 kx }
9 kx
9 kx // The selector for i386 object files. Note this is never instantiated
9 kx // directly. It's only used in Target_selector_i386_nacl, below.
9 kx
9 kx class Target_selector_i386 : public Target_selector_freebsd
9 kx {
9 kx public:
9 kx Target_selector_i386()
9 kx : Target_selector_freebsd(elfcpp::EM_386, 32, false,
9 kx "elf32-i386", "elf32-i386-freebsd",
9 kx "elf_i386")
9 kx { }
9 kx
9 kx Target*
9 kx do_instantiate_target()
9 kx { return new Target_i386(); }
9 kx };
9 kx
9 kx // NaCl variant. It uses different PLT contents.
9 kx
9 kx class Output_data_plt_i386_nacl : public Output_data_plt_i386
9 kx {
9 kx public:
9 kx Output_data_plt_i386_nacl(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative)
9 kx : Output_data_plt_i386(layout, plt_entry_size, got_plt, got_irelative)
9 kx { }
9 kx
9 kx protected:
9 kx virtual unsigned int
9 kx do_get_plt_entry_size() const
9 kx { return plt_entry_size; }
9 kx
9 kx virtual void
9 kx do_add_eh_frame(Layout* layout)
9 kx {
9 kx layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
9 kx plt_eh_frame_fde, plt_eh_frame_fde_size);
9 kx }
9 kx
9 kx // The size of an entry in the PLT.
9 kx static const int plt_entry_size = 64;
9 kx
9 kx // The .eh_frame unwind information for the PLT.
9 kx static const int plt_eh_frame_fde_size = 32;
9 kx static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
9 kx };
9 kx
9 kx class Output_data_plt_i386_nacl_exec : public Output_data_plt_i386_nacl
9 kx {
9 kx public:
9 kx Output_data_plt_i386_nacl_exec(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative)
9 kx : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
9 kx { }
9 kx
9 kx protected:
9 kx virtual void
9 kx do_fill_first_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address);
9 kx
9 kx virtual unsigned int
9 kx do_fill_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset);
9 kx
9 kx private:
9 kx // The first entry in the PLT for an executable.
9 kx static const unsigned char first_plt_entry[plt_entry_size];
9 kx
9 kx // Other entries in the PLT for an executable.
9 kx static const unsigned char plt_entry[plt_entry_size];
9 kx };
9 kx
9 kx class Output_data_plt_i386_nacl_dyn : public Output_data_plt_i386_nacl
9 kx {
9 kx public:
9 kx Output_data_plt_i386_nacl_dyn(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative)
9 kx : Output_data_plt_i386_nacl(layout, got_plt, got_irelative)
9 kx { }
9 kx
9 kx protected:
9 kx virtual void
9 kx do_fill_first_plt_entry(unsigned char* pov, elfcpp::Elf_types<32>::Elf_Addr);
9 kx
9 kx virtual unsigned int
9 kx do_fill_plt_entry(unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset);
9 kx
9 kx private:
9 kx // The first entry in the PLT for a shared object.
9 kx static const unsigned char first_plt_entry[plt_entry_size];
9 kx
9 kx // Other entries in the PLT for a shared object.
9 kx static const unsigned char plt_entry[plt_entry_size];
9 kx };
9 kx
9 kx class Target_i386_nacl : public Target_i386
9 kx {
9 kx public:
9 kx Target_i386_nacl()
9 kx : Target_i386(&i386_nacl_info)
9 kx { }
9 kx
9 kx protected:
9 kx virtual Output_data_plt_i386*
9 kx do_make_data_plt(Layout* layout,
9 kx Output_data_got_plt_i386* got_plt,
9 kx Output_data_space* got_irelative,
9 kx bool dyn)
9 kx {
9 kx if (dyn)
9 kx return new Output_data_plt_i386_nacl_dyn(layout, got_plt, got_irelative);
9 kx else
9 kx return new Output_data_plt_i386_nacl_exec(layout, got_plt, got_irelative);
9 kx }
9 kx
9 kx virtual std::string
9 kx do_code_fill(section_size_type length) const;
9 kx
9 kx private:
9 kx static const Target::Target_info i386_nacl_info;
9 kx };
9 kx
9 kx const Target::Target_info Target_i386_nacl::i386_nacl_info =
9 kx {
9 kx 32, // size
9 kx false, // is_big_endian
9 kx elfcpp::EM_386, // machine_code
9 kx false, // has_make_symbol
9 kx false, // has_resolve
9 kx true, // has_code_fill
9 kx true, // is_default_stack_executable
9 kx true, // can_icf_inline_merge_sections
9 kx '\0', // wrap_char
9 kx "/lib/ld-nacl-x86-32.so.1", // dynamic_linker
9 kx 0x20000, // default_text_segment_address
9 kx 0x10000, // abi_pagesize (overridable by -z max-page-size)
9 kx 0x10000, // common_pagesize (overridable by -z common-page-size)
9 kx true, // isolate_execinstr
9 kx 0x10000000, // rosegment_gap
9 kx elfcpp::SHN_UNDEF, // small_common_shndx
9 kx elfcpp::SHN_UNDEF, // large_common_shndx
9 kx 0, // small_common_section_flags
9 kx 0, // large_common_section_flags
9 kx NULL, // attributes_section
9 kx NULL, // attributes_vendor
9 kx "_start", // entry_symbol_name
9 kx 32, // hash_entry_size
9 kx elfcpp::SHT_PROGBITS, // unwind_section_type
9 kx };
9 kx
9 kx #define NACLMASK 0xe0 // 32-byte alignment mask
9 kx
9 kx const unsigned char
9 kx Output_data_plt_i386_nacl_exec::first_plt_entry[plt_entry_size] =
9 kx {
9 kx 0xff, 0x35, // pushl contents of memory address
9 kx 0, 0, 0, 0, // replaced with address of .got + 4
9 kx 0x8b, 0x0d, // movl contents of address, %ecx
9 kx 0, 0, 0, 0, // replaced with address of .got + 8
9 kx 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
9 kx 0xff, 0xe1, // jmp *%ecx
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90
9 kx };
9 kx
9 kx void
9 kx Output_data_plt_i386_nacl_exec::do_fill_first_plt_entry(
9 kx unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address)
9 kx {
9 kx memcpy(pov, first_plt_entry, plt_entry_size);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
9 kx elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
9 kx }
9 kx
9 kx // The first entry in the PLT for a shared object.
9 kx
9 kx const unsigned char
9 kx Output_data_plt_i386_nacl_dyn::first_plt_entry[plt_entry_size] =
9 kx {
9 kx 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
9 kx 0x8b, 0x4b, 0x08, // mov 0x8(%ebx), %ecx
9 kx 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
9 kx 0xff, 0xe1, // jmp *%ecx
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, // nops
9 kx 0x90, 0x90, 0x90, 0x90, 0x90 // nops
9 kx };
9 kx
9 kx void
9 kx Output_data_plt_i386_nacl_dyn::do_fill_first_plt_entry(
9 kx unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr)
9 kx {
9 kx memcpy(pov, first_plt_entry, plt_entry_size);
9 kx }
9 kx
9 kx // Subsequent entries in the PLT for an executable.
9 kx
9 kx const unsigned char
9 kx Output_data_plt_i386_nacl_exec::plt_entry[plt_entry_size] =
9 kx {
9 kx 0x8b, 0x0d, // movl contents of address, %ecx */
9 kx 0, 0, 0, 0, // replaced with address of symbol in .got
9 kx 0x83, 0xe1, NACLMASK, // andl $NACLMASK, %ecx
9 kx 0xff, 0xe1, // jmp *%ecx
9 kx
9 kx // Pad to the next 32-byte boundary with nop instructions.
9 kx 0x90,
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
9 kx
9 kx // Lazy GOT entries point here (32-byte aligned).
9 kx 0x68, // pushl immediate
9 kx 0, 0, 0, 0, // replaced with offset into relocation table
9 kx 0xe9, // jmp relative
9 kx 0, 0, 0, 0, // replaced with offset to start of .plt
9 kx
9 kx // Pad to the next 32-byte boundary with nop instructions.
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
9 kx 0x90, 0x90
9 kx };
9 kx
9 kx unsigned int
9 kx Output_data_plt_i386_nacl_exec::do_fill_plt_entry(
9 kx unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr got_address,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset)
9 kx {
9 kx memcpy(pov, plt_entry, plt_entry_size);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
9 kx got_address + got_offset);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
9 kx elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
9 kx return 32;
9 kx }
9 kx
9 kx // Subsequent entries in the PLT for a shared object.
9 kx
9 kx const unsigned char
9 kx Output_data_plt_i386_nacl_dyn::plt_entry[plt_entry_size] =
9 kx {
9 kx 0x8b, 0x8b, // movl offset(%ebx), %ecx
9 kx 0, 0, 0, 0, // replaced with offset of symbol in .got
9 kx 0x83, 0xe1, 0xe0, // andl $NACLMASK, %ecx
9 kx 0xff, 0xe1, // jmp *%ecx
9 kx
9 kx // Pad to the next 32-byte boundary with nop instructions.
9 kx 0x90,
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
9 kx
9 kx // Lazy GOT entries point here (32-byte aligned).
9 kx 0x68, // pushl immediate
9 kx 0, 0, 0, 0, // replaced with offset into relocation table.
9 kx 0xe9, // jmp relative
9 kx 0, 0, 0, 0, // replaced with offset to start of .plt.
9 kx
9 kx // Pad to the next 32-byte boundary with nop instructions.
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
9 kx 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
9 kx 0x90, 0x90
9 kx };
9 kx
9 kx unsigned int
9 kx Output_data_plt_i386_nacl_dyn::do_fill_plt_entry(
9 kx unsigned char* pov,
9 kx elfcpp::Elf_types<32>::Elf_Addr,
9 kx unsigned int got_offset,
9 kx unsigned int plt_offset,
9 kx unsigned int plt_rel_offset)
9 kx {
9 kx memcpy(pov, plt_entry, plt_entry_size);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
9 kx elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_rel_offset);
9 kx elfcpp::Swap<32, false>::writeval(pov + 38, - (plt_offset + 38 + 4));
9 kx return 32;
9 kx }
9 kx
9 kx const unsigned char
9 kx Output_data_plt_i386_nacl::plt_eh_frame_fde[plt_eh_frame_fde_size] =
9 kx {
9 kx 0, 0, 0, 0, // Replaced with offset to .plt.
9 kx 0, 0, 0, 0, // Replaced with size of .plt.
9 kx 0, // Augmentation size.
9 kx elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
9 kx elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
9 kx elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
9 kx elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64.
9 kx elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
9 kx 13, // Block length.
9 kx elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
9 kx elfcpp::DW_OP_breg8, 0, // Push %eip.
9 kx elfcpp::DW_OP_const1u, 63, // Push 0x3f.
9 kx elfcpp::DW_OP_and, // & (%eip & 0x3f).
9 kx elfcpp::DW_OP_const1u, 37, // Push 0x25.
9 kx elfcpp::DW_OP_ge, // >= ((%eip & 0x3f) >= 0x25)
9 kx elfcpp::DW_OP_lit2, // Push 2.
9 kx elfcpp::DW_OP_shl, // << (((%eip & 0x3f) >= 0x25) << 2)
9 kx elfcpp::DW_OP_plus, // + ((((%eip&0x3f)>=0x25)<<2)+%esp+4
9 kx elfcpp::DW_CFA_nop, // Align to 32 bytes.
9 kx elfcpp::DW_CFA_nop
9 kx };
9 kx
9 kx // Return a string used to fill a code section with nops.
9 kx // For NaCl, long NOPs are only valid if they do not cross
9 kx // bundle alignment boundaries, so keep it simple with one-byte NOPs.
9 kx std::string
9 kx Target_i386_nacl::do_code_fill(section_size_type length) const
9 kx {
9 kx return std::string(length, static_cast<char>(0x90));
9 kx }
9 kx
9 kx // The selector for i386-nacl object files.
9 kx
9 kx class Target_selector_i386_nacl
9 kx : public Target_selector_nacl<Target_selector_i386, Target_i386_nacl>
9 kx {
9 kx public:
9 kx Target_selector_i386_nacl()
9 kx : Target_selector_nacl<Target_selector_i386,
9 kx Target_i386_nacl>("x86-32",
9 kx "elf32-i386-nacl",
9 kx "elf_i386_nacl")
9 kx { }
9 kx };
9 kx
9 kx Target_selector_i386_nacl target_selector_i386;
9 kx
9 kx // IAMCU variant. It uses EM_IAMCU, not EM_386.
9 kx
9 kx class Target_iamcu : public Target_i386
9 kx {
9 kx public:
9 kx Target_iamcu()
9 kx : Target_i386(&iamcu_info)
9 kx { }
9 kx
9 kx private:
9 kx // Information about this specific target which we pass to the
9 kx // general Target structure.
9 kx static const Target::Target_info iamcu_info;
9 kx };
9 kx
9 kx const Target::Target_info Target_iamcu::iamcu_info =
9 kx {
9 kx 32, // size
9 kx false, // is_big_endian
9 kx elfcpp::EM_IAMCU, // machine_code
9 kx false, // has_make_symbol
9 kx false, // has_resolve
9 kx true, // has_code_fill
9 kx true, // is_default_stack_executable
9 kx true, // can_icf_inline_merge_sections
9 kx '\0', // wrap_char
9 kx "/usr/lib/libc.so.1", // dynamic_linker
9 kx 0x08048000, // default_text_segment_address
9 kx 0x1000, // abi_pagesize (overridable by -z max-page-size)
9 kx 0x1000, // common_pagesize (overridable by -z common-page-size)
9 kx false, // isolate_execinstr
9 kx 0, // rosegment_gap
9 kx elfcpp::SHN_UNDEF, // small_common_shndx
9 kx elfcpp::SHN_UNDEF, // large_common_shndx
9 kx 0, // small_common_section_flags
9 kx 0, // large_common_section_flags
9 kx NULL, // attributes_section
9 kx NULL, // attributes_vendor
9 kx "_start", // entry_symbol_name
9 kx 32, // hash_entry_size
9 kx elfcpp::SHT_PROGBITS, // unwind_section_type
9 kx };
9 kx
9 kx class Target_selector_iamcu : public Target_selector
9 kx {
9 kx public:
9 kx Target_selector_iamcu()
9 kx : Target_selector(elfcpp::EM_IAMCU, 32, false, "elf32-iamcu",
9 kx "elf_iamcu")
9 kx { }
9 kx
9 kx Target*
9 kx do_instantiate_target()
9 kx { return new Target_iamcu(); }
9 kx };
9 kx
9 kx Target_selector_iamcu target_selector_iamcu;
9 kx
9 kx } // End anonymous namespace.