HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Nougat 7.0
|
7.0.0_r31
下载
查看原文件
收藏
根目录
toolchain
binutils
binutils-2.25
gold
powerpc.cc
// powerpc.cc -- powerpc target support for gold. // Copyright (C) 2008-2014 Free Software Foundation, Inc. // Written by David S. Miller
// and David Edelsohn
// This file is part of gold. // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, // MA 02110-1301, USA. #include "gold.h" #include
#include
#include "elfcpp.h" #include "dwarf.h" #include "parameters.h" #include "reloc.h" #include "powerpc.h" #include "object.h" #include "symtab.h" #include "layout.h" #include "output.h" #include "copy-relocs.h" #include "target.h" #include "target-reloc.h" #include "target-select.h" #include "tls.h" #include "errors.h" #include "gc.h" namespace { using namespace gold; template
class Output_data_plt_powerpc; template
class Output_data_brlt_powerpc; template
class Output_data_got_powerpc; template
class Output_data_glink; template
class Stub_table; template
class Target_powerpc; struct Stub_table_owner { Output_section* output_section; const Output_section::Input_section* owner; }; inline bool is_branch_reloc(unsigned int r_type); template
class Powerpc_relobj : public Sized_relobj_file
{ public: typedef typename elfcpp::Elf_types
::Elf_Addr Address; typedef Unordered_set
Section_refs; typedef Unordered_map
Access_from; Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset, const typename elfcpp::Ehdr
& ehdr) : Sized_relobj_file
(name, input_file, offset, ehdr), special_(0), has_small_toc_reloc_(false), opd_valid_(false), opd_ent_(), access_from_map_(), has14_(), stub_table_index_(), e_flags_(ehdr.get_e_flags()), st_other_() { this->set_abiversion(0); } ~Powerpc_relobj() { } // Read the symbols then set up st_other vector. void do_read_symbols(Read_symbols_data*); // The .got2 section shndx. unsigned int got2_shndx() const { if (size == 32) return this->special_; else return 0; } // The .opd section shndx. unsigned int opd_shndx() const { if (size == 32) return 0; else return this->special_; } // Init OPD entry arrays. void init_opd(size_t opd_size) { size_t count = this->opd_ent_ndx(opd_size); this->opd_ent_.resize(count); } // Return section and offset of function entry for .opd + R_OFF. unsigned int get_opd_ent(Address r_off, Address* value = NULL) const { size_t ndx = this->opd_ent_ndx(r_off); gold_assert(ndx < this->opd_ent_.size()); gold_assert(this->opd_ent_[ndx].shndx != 0); if (value != NULL) *value = this->opd_ent_[ndx].off; return this->opd_ent_[ndx].shndx; } // Set section and offset of function entry for .opd + R_OFF. void set_opd_ent(Address r_off, unsigned int shndx, Address value) { size_t ndx = this->opd_ent_ndx(r_off); gold_assert(ndx < this->opd_ent_.size()); this->opd_ent_[ndx].shndx = shndx; this->opd_ent_[ndx].off = value; } // Return discard flag for .opd + R_OFF. bool get_opd_discard(Address r_off) const { size_t ndx = this->opd_ent_ndx(r_off); gold_assert(ndx < this->opd_ent_.size()); return this->opd_ent_[ndx].discard; } // Set discard flag for .opd + R_OFF. void set_opd_discard(Address r_off) { size_t ndx = this->opd_ent_ndx(r_off); gold_assert(ndx < this->opd_ent_.size()); this->opd_ent_[ndx].discard = true; } bool opd_valid() const { return this->opd_valid_; } void set_opd_valid() { this->opd_valid_ = true; } // Examine .rela.opd to build info about function entry points. void scan_opd_relocs(size_t reloc_count, const unsigned char* prelocs, const unsigned char* plocal_syms); // Perform the Sized_relobj_file method, then set up opd info from // .opd relocs. void do_read_relocs(Read_relocs_data*); bool do_find_special_sections(Read_symbols_data* sd); // Adjust this local symbol value. Return false if the symbol // should be discarded from the output file. bool do_adjust_local_symbol(Symbol_value
* lv) const { if (size == 64 && this->opd_shndx() != 0) { bool is_ordinary; if (lv->input_shndx(&is_ordinary) != this->opd_shndx()) return true; if (this->get_opd_discard(lv->input_value())) return false; } return true; } Access_from* access_from_map() { return &this->access_from_map_; } // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd // section at DST_OFF. void add_reference(Object* src_obj, unsigned int src_indx, typename elfcpp::Elf_types
::Elf_Addr dst_off) { Section_id src_id(src_obj, src_indx); this->access_from_map_[dst_off].insert(src_id); } // Add a reference to the code section specified by the .opd entry // at DST_OFF void add_gc_mark(typename elfcpp::Elf_types
::Elf_Addr dst_off) { size_t ndx = this->opd_ent_ndx(dst_off); if (ndx >= this->opd_ent_.size()) this->opd_ent_.resize(ndx + 1); this->opd_ent_[ndx].gc_mark = true; } void process_gc_mark(Symbol_table* symtab) { for (size_t i = 0; i < this->opd_ent_.size(); i++) if (this->opd_ent_[i].gc_mark) { unsigned int shndx = this->opd_ent_[i].shndx; symtab->gc()->worklist().push(Section_id(this, shndx)); } } // Return offset in output GOT section that this object will use // as a TOC pointer. Won't be just a constant with multi-toc support. Address toc_base_offset() const { return 0x8000; } void set_has_small_toc_reloc() { has_small_toc_reloc_ = true; } bool has_small_toc_reloc() const { return has_small_toc_reloc_; } void set_has_14bit_branch(unsigned int shndx) { if (shndx >= this->has14_.size()) this->has14_.resize(shndx + 1); this->has14_[shndx] = true; } bool has_14bit_branch(unsigned int shndx) const { return shndx < this->has14_.size() && this->has14_[shndx]; } void set_stub_table(unsigned int shndx, unsigned int stub_index) { if (shndx >= this->stub_table_index_.size()) this->stub_table_index_.resize(shndx + 1); this->stub_table_index_[shndx] = stub_index; } Stub_table
* stub_table(unsigned int shndx) { if (shndx < this->stub_table_index_.size()) { Target_powerpc
* target = static_cast
*>( parameters->sized_target
()); unsigned int indx = this->stub_table_index_[shndx]; gold_assert(indx < target->stub_tables().size()); return target->stub_tables()[indx]; } return NULL; } void clear_stub_table() { this->stub_table_index_.clear(); } int abiversion() const { return this->e_flags_ & elfcpp::EF_PPC64_ABI; } // Set ABI version for input and output void set_abiversion(int ver); unsigned int ppc64_local_entry_offset(const Symbol* sym) const { return elfcpp::ppc64_decode_local_entry(sym->nonvis() >> 3); } unsigned int ppc64_local_entry_offset(unsigned int symndx) const { return elfcpp::ppc64_decode_local_entry(this->st_other_[symndx] >> 5); } private: struct Opd_ent { unsigned int shndx; bool discard : 1; bool gc_mark : 1; Address off; }; // Return index into opd_ent_ array for .opd entry at OFF. // .opd entries are 24 bytes long, but they can be spaced 16 bytes // apart when the language doesn't use the last 8-byte word, the // environment pointer. Thus dividing the entry section offset by // 16 will give an index into opd_ent_ that works for either layout // of .opd. (It leaves some elements of the vector unused when .opd // entries are spaced 24 bytes apart, but we don't know the spacing // until relocations are processed, and in any case it is possible // for an object to have some entries spaced 16 bytes apart and // others 24 bytes apart.) size_t opd_ent_ndx(size_t off) const { return off >> 4;} // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx. unsigned int special_; // For 64-bit, whether this object uses small model relocs to access // the toc. bool has_small_toc_reloc_; // Set at the start of gc_process_relocs, when we know opd_ent_ // vector is valid. The flag could be made atomic and set in // do_read_relocs with memory_order_release and then tested with // memory_order_acquire, potentially resulting in fewer entries in // access_from_map_. bool opd_valid_; // The first 8-byte word of an OPD entry gives the address of the // entry point of the function. Relocatable object files have a // relocation on this word. The following vector records the // section and offset specified by these relocations. std::vector
opd_ent_; // References made to this object's .opd section when running // gc_process_relocs for another object, before the opd_ent_ vector // is valid for this object. Access_from access_from_map_; // Whether input section has a 14-bit branch reloc. std::vector
has14_; // The stub table to use for a given input section. std::vector
stub_table_index_; // Header e_flags elfcpp::Elf_Word e_flags_; // ELF st_other field for local symbols. std::vector
st_other_; }; template
class Powerpc_dynobj : public Sized_dynobj
{ public: typedef typename elfcpp::Elf_types
::Elf_Addr Address; Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset, const typename elfcpp::Ehdr
& ehdr) : Sized_dynobj
(name, input_file, offset, ehdr), opd_shndx_(0), opd_ent_(), e_flags_(ehdr.get_e_flags()) { this->set_abiversion(0); } ~Powerpc_dynobj() { } // Call Sized_dynobj::do_read_symbols to read the symbols then // read .opd from a dynamic object, filling in opd_ent_ vector, void do_read_symbols(Read_symbols_data*); // The .opd section shndx. unsigned int opd_shndx() const { return this->opd_shndx_; } // The .opd section address. Address opd_address() const { return this->opd_address_; } // Init OPD entry arrays. void init_opd(size_t opd_size) { size_t count = this->opd_ent_ndx(opd_size); this->opd_ent_.resize(count); } // Return section and offset of function entry for .opd + R_OFF. unsigned int get_opd_ent(Address r_off, Address* value = NULL) const { size_t ndx = this->opd_ent_ndx(r_off); gold_assert(ndx < this->opd_ent_.size()); gold_assert(this->opd_ent_[ndx].shndx != 0); if (value != NULL) *value = this->opd_ent_[ndx].off; return this->opd_ent_[ndx].shndx; } // Set section and offset of function entry for .opd + R_OFF. void set_opd_ent(Address r_off, unsigned int shndx, Address value) { size_t ndx = this->opd_ent_ndx(r_off); gold_assert(ndx < this->opd_ent_.size()); this->opd_ent_[ndx].shndx = shndx; this->opd_ent_[ndx].off = value; } int abiversion() const { return this->e_flags_ & elfcpp::EF_PPC64_ABI; } // Set ABI version for input and output. void set_abiversion(int ver); private: // Used to specify extent of executable sections. struct Sec_info { Sec_info(Address start_, Address len_, unsigned int shndx_) : start(start_), len(len_), shndx(shndx_) { } bool operator<(const Sec_info& that) const { return this->start < that.start; } Address start; Address len; unsigned int shndx; }; struct Opd_ent { unsigned int shndx; Address off; }; // Return index into opd_ent_ array for .opd entry at OFF. size_t opd_ent_ndx(size_t off) const { return off >> 4;} // For 64-bit the .opd section shndx and address. unsigned int opd_shndx_; Address opd_address_; // The first 8-byte word of an OPD entry gives the address of the // entry point of the function. Records the section and offset // corresponding to the address. Note that in dynamic objects, // offset is *not* relative to the section. std::vector
opd_ent_; // Header e_flags elfcpp::Elf_Word e_flags_; }; template
class Target_powerpc : public Sized_target
{ public: typedef Output_data_reloc
Reloc_section; typedef typename elfcpp::Elf_types
::Elf_Addr Address; typedef typename elfcpp::Elf_types
::Elf_Swxword Signed_address; static const Address invalid_address = static_cast
(0) - 1; // Offset of tp and dtp pointers from start of TLS block. static const Address tp_offset = 0x7000; static const Address dtp_offset = 0x8000; Target_powerpc() : Sized_target
(&powerpc_info), got_(NULL), plt_(NULL), iplt_(NULL), brlt_section_(NULL), glink_(NULL), rela_dyn_(NULL), copy_relocs_(elfcpp::R_POWERPC_COPY), tlsld_got_offset_(-1U), stub_tables_(), branch_lookup_table_(), branch_info_(), plt_thread_safe_(false), relax_failed_(false), relax_fail_count_(0), stub_group_size_(0) { } // Process the relocations to determine unreferenced sections for // garbage collection. void gc_process_relocs(Symbol_table* symtab, Layout* layout, Sized_relobj_file
* object, unsigned int data_shndx, unsigned int sh_type, const unsigned char* prelocs, size_t reloc_count, Output_section* output_section, bool needs_special_offset_handling, size_t local_symbol_count, const unsigned char* plocal_symbols); // Scan the relocations to look for symbol adjustments. void scan_relocs(Symbol_table* symtab, Layout* layout, Sized_relobj_file
* object, unsigned int data_shndx, unsigned int sh_type, const unsigned char* prelocs, size_t reloc_count, Output_section* output_section, bool needs_special_offset_handling, size_t local_symbol_count, const unsigned char* plocal_symbols); // Map input .toc section to output .got section. const char* do_output_section_name(const Relobj*, const char* name, size_t* plen) const { if (size == 64 && strcmp(name, ".toc") == 0) { *plen = 4; return ".got"; } return NULL; } // Provide linker defined save/restore functions. void define_save_restore_funcs(Layout*, Symbol_table*); // No stubs unless a final link. bool do_may_relax() const { return !parameters->options().relocatable(); } bool do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*); void do_plt_fde_location(const Output_data*, unsigned char*, uint64_t*, off_t*) const; // Stash info about branches, for stub generation. void push_branch(Powerpc_relobj
* ppc_object, unsigned int data_shndx, Address r_offset, unsigned int r_type, unsigned int r_sym, Address addend) { Branch_info info(ppc_object, data_shndx, r_offset, r_type, r_sym, addend); this->branch_info_.push_back(info); if (r_type == elfcpp::R_POWERPC_REL14 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN) ppc_object->set_has_14bit_branch(data_shndx); } void do_define_standard_symbols(Symbol_table*, Layout*); // Finalize the sections. void do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); // Return the value to use for a dynamic which requires special // treatment. uint64_t do_dynsym_value(const Symbol*) const; // Return the PLT address to use for a local symbol. uint64_t do_plt_address_for_local(const Relobj*, unsigned int) const; // Return the PLT address to use for a global symbol. uint64_t do_plt_address_for_global(const Symbol*) const; // Return the offset to use for the GOT_INDX'th got entry which is // for a local tls symbol specified by OBJECT, SYMNDX. int64_t do_tls_offset_for_local(const Relobj* object, unsigned int symndx, unsigned int got_indx) const; // Return the offset to use for the GOT_INDX'th got entry which is // for global tls symbol GSYM. int64_t do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const; void do_function_location(Symbol_location*) const; bool do_can_check_for_function_pointers() const { return true; } // Relocate a section. void relocate_section(const Relocate_info
*, unsigned int sh_type, const unsigned char* prelocs, size_t reloc_count, Output_section* output_section, bool needs_special_offset_handling, unsigned char* view, Address view_address, section_size_type view_size, const Reloc_symbol_changes*); // Scan the relocs during a relocatable link. void scan_relocatable_relocs(Symbol_table* symtab, Layout* layout, Sized_relobj_file
* object, unsigned int data_shndx, unsigned int sh_type, const unsigned char* prelocs, size_t reloc_count, Output_section* output_section, bool needs_special_offset_handling, size_t local_symbol_count, const unsigned char* plocal_symbols, Relocatable_relocs*); // Emit relocations for a section. void relocate_relocs(const Relocate_info
*, unsigned int sh_type, const unsigned char* prelocs, size_t reloc_count, Output_section* output_section, typename elfcpp::Elf_types
::Elf_Off offset_in_output_section, const Relocatable_relocs*, unsigned char*, Address view_address, section_size_type, unsigned char* reloc_view, section_size_type reloc_view_size); // Return whether SYM is defined by the ABI. bool do_is_defined_by_abi(const Symbol* sym) const { return strcmp(sym->name(), "__tls_get_addr") == 0; } // Return the size of the GOT section. section_size_type got_size() const { gold_assert(this->got_ != NULL); return this->got_->data_size(); } // Get the PLT section. const Output_data_plt_powerpc
* plt_section() const { gold_assert(this->plt_ != NULL); return this->plt_; } // Get the IPLT section. const Output_data_plt_powerpc
* iplt_section() const { gold_assert(this->iplt_ != NULL); return this->iplt_; } // Get the .glink section. const Output_data_glink
* glink_section() const { gold_assert(this->glink_ != NULL); return this->glink_; } Output_data_glink
* glink_section() { gold_assert(this->glink_ != NULL); return this->glink_; } bool has_glink() const { return this->glink_ != NULL; } // Get the GOT section. const Output_data_got_powerpc
* got_section() const { gold_assert(this->got_ != NULL); return this->got_; } // Get the GOT section, creating it if necessary. Output_data_got_powerpc
* got_section(Symbol_table*, Layout*); Object* do_make_elf_object(const std::string&, Input_file*, off_t, const elfcpp::Ehdr
&); // Return the number of entries in the GOT. unsigned int got_entry_count() const { if (this->got_ == NULL) return 0; return this->got_size() / (size / 8); } // Return the number of entries in the PLT. unsigned int plt_entry_count() const; // Return the offset of the first non-reserved PLT entry. unsigned int first_plt_entry_offset() const { if (size == 32) return 0; if (this->abiversion() >= 2) return 16; return 24; } // Return the size of each PLT entry. unsigned int plt_entry_size() const { if (size == 32) return 4; if (this->abiversion() >= 2) return 8; return 24; } // Add any special sections for this symbol to the gc work list. // For powerpc64, this adds the code section of a function // descriptor. void do_gc_mark_symbol(Symbol_table* symtab, Symbol* sym) const; // Handle target specific gc actions when adding a gc reference from // SRC_OBJ, SRC_SHNDX to a location specified by DST_OBJ, DST_SHNDX // and DST_OFF. For powerpc64, this adds a referenc to the code // section of a function descriptor. void do_gc_add_reference(Symbol_table* symtab, Object* src_obj, unsigned int src_shndx, Object* dst_obj, unsigned int dst_shndx, Address dst_off) const; typedef std::vector
*> Stub_tables; const Stub_tables& stub_tables() const { return this->stub_tables_; } const Output_data_brlt_powerpc
* brlt_section() const { return this->brlt_section_; } void add_branch_lookup_table(Address to) { unsigned int off = this->branch_lookup_table_.size() * (size / 8); this->branch_lookup_table_.insert(std::make_pair(to, off)); } Address find_branch_lookup_table(Address to) { typename Branch_lookup_table::const_iterator p = this->branch_lookup_table_.find(to); return p == this->branch_lookup_table_.end() ? invalid_address : p->second; } void write_branch_lookup_table(unsigned char *oview) { for (typename Branch_lookup_table::const_iterator p = this->branch_lookup_table_.begin(); p != this->branch_lookup_table_.end(); ++p) { elfcpp::Swap
::writeval(oview + p->second, p->first); } } bool plt_thread_safe() const { return this->plt_thread_safe_; } int abiversion () const { return this->processor_specific_flags() & elfcpp::EF_PPC64_ABI; } void set_abiversion (int ver) { elfcpp::Elf_Word flags = this->processor_specific_flags(); flags &= ~elfcpp::EF_PPC64_ABI; flags |= ver & elfcpp::EF_PPC64_ABI; this->set_processor_specific_flags(flags); } // Offset to to save stack slot int stk_toc () const { return this->abiversion() < 2 ? 40 : 24; } private: class Track_tls { public: enum Tls_get_addr { NOT_EXPECTED = 0, EXPECTED = 1, SKIP = 2, NORMAL = 3 }; Track_tls() : tls_get_addr_(NOT_EXPECTED), relinfo_(NULL), relnum_(0), r_offset_(0) { } ~Track_tls() { if (this->tls_get_addr_ != NOT_EXPECTED) this->missing(); } void missing(void) { if (this->relinfo_ != NULL) gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_, _("missing expected __tls_get_addr call")); } void expect_tls_get_addr_call( const Relocate_info
* relinfo, size_t relnum, Address r_offset) { this->tls_get_addr_ = EXPECTED; this->relinfo_ = relinfo; this->relnum_ = relnum; this->r_offset_ = r_offset; } void expect_tls_get_addr_call() { this->tls_get_addr_ = EXPECTED; } void skip_next_tls_get_addr_call() {this->tls_get_addr_ = SKIP; } Tls_get_addr maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym) { bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24 || r_type == elfcpp::R_PPC_PLTREL24) && gsym != NULL && strcmp(gsym->name(), "__tls_get_addr") == 0); Tls_get_addr last_tls = this->tls_get_addr_; this->tls_get_addr_ = NOT_EXPECTED; if (is_tls_call && last_tls != EXPECTED) return last_tls; else if (!is_tls_call && last_tls != NOT_EXPECTED) { this->missing(); return EXPECTED; } return NORMAL; } private: // What we're up to regarding calls to __tls_get_addr. // On powerpc, the branch and link insn making a call to // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD, // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call. // The marker relocation always comes first, and has the same // symbol as the reloc on the insn setting up the __tls_get_addr // argument. This ties the arg setup insn with the call insn, // allowing ld to safely optimize away the call. We check that // every call to __tls_get_addr has a marker relocation, and that // every marker relocation is on a call to __tls_get_addr. Tls_get_addr tls_get_addr_; // Info about the last reloc for error message. const Relocate_info
* relinfo_; size_t relnum_; Address r_offset_; }; // The class which scans relocations. class Scan : protected Track_tls { public: typedef typename elfcpp::Elf_types
::Elf_Addr Address; Scan() : Track_tls(), issued_non_pic_error_(false) { } static inline int get_reference_flags(unsigned int r_type, const Target_powerpc* target); inline void local(Symbol_table* symtab, Layout* layout, Target_powerpc* target, Sized_relobj_file
* object, unsigned int data_shndx, Output_section* output_section, const elfcpp::Rela
& reloc, unsigned int r_type, const elfcpp::Sym
& lsym, bool is_discarded); inline void global(Symbol_table* symtab, Layout* layout, Target_powerpc* target, Sized_relobj_file
* object, unsigned int data_shndx, Output_section* output_section, const elfcpp::Rela
& reloc, unsigned int r_type, Symbol* gsym); inline bool local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_powerpc* , Sized_relobj_file
* relobj, unsigned int , Output_section* , const elfcpp::Rela
& , unsigned int r_type, const elfcpp::Sym
&) { // PowerPC64 .opd is not folded, so any identical function text // may be folded and we'll still keep function addresses distinct. // That means no reloc is of concern here. if (size == 64) { Powerpc_relobj
* ppcobj = static_cast
*>(relobj); if (ppcobj->abiversion() == 1) return false; } // For 32-bit and ELFv2, conservatively assume anything but calls to // function code might be taking the address of the function. return !is_branch_reloc(r_type); } inline bool global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_powerpc* , Sized_relobj_file
* relobj, unsigned int , Output_section* , const elfcpp::Rela
& , unsigned int r_type, Symbol*) { // As above. if (size == 64) { Powerpc_relobj
* ppcobj = static_cast
*>(relobj); if (ppcobj->abiversion() == 1) return false; } return !is_branch_reloc(r_type); } static bool reloc_needs_plt_for_ifunc(Target_powerpc
* target, Sized_relobj_file
* object, unsigned int r_type, bool report_err); private: static void unsupported_reloc_local(Sized_relobj_file
*, unsigned int r_type); static void unsupported_reloc_global(Sized_relobj_file
*, unsigned int r_type, Symbol*); static void generate_tls_call(Symbol_table* symtab, Layout* layout, Target_powerpc* target); void check_non_pic(Relobj*, unsigned int r_type); // Whether we have issued an error about a non-PIC compilation. bool issued_non_pic_error_; }; bool symval_for_branch(const Symbol_table* symtab, const Sized_symbol
* gsym, Powerpc_relobj
* object, Address *value, unsigned int *dest_shndx); // The class which implements relocation. class Relocate : protected Track_tls { public: // Use 'at' branch hints when true, 'y' when false. // FIXME maybe: set this with an option. static const bool is_isa_v2 = true; Relocate() : Track_tls() { } // Do a relocation. Return false if the caller should not issue // any warnings about this relocation. inline bool relocate(const Relocate_info
*, Target_powerpc*, Output_section*, size_t relnum, const elfcpp::Rela
&, unsigned int r_type, const Sized_symbol
*, const Symbol_value
*, unsigned char*, typename elfcpp::Elf_types
::Elf_Addr, section_size_type); }; class Relocate_comdat_behavior { public: // Decide what the linker should do for relocations that refer to // discarded comdat sections. inline Comdat_behavior get(const char* name) { gold::Default_comdat_behavior default_behavior; Comdat_behavior ret = default_behavior.get(name); if (ret == CB_WARNING) { if (size == 32 && (strcmp(name, ".fixup") == 0 || strcmp(name, ".got2") == 0)) ret = CB_IGNORE; if (size == 64 && (strcmp(name, ".opd") == 0 || strcmp(name, ".toc") == 0 || strcmp(name, ".toc1") == 0)) ret = CB_IGNORE; } return ret; } }; // A class which returns the size required for a relocation type, // used while scanning relocs during a relocatable link. class Relocatable_size_for_reloc { public: unsigned int get_size_for_reloc(unsigned int, Relobj*) { gold_unreachable(); return 0; } }; // Optimize the TLS relocation type based on what we know about the // symbol. IS_FINAL is true if the final address of this symbol is // known at link time. tls::Tls_optimization optimize_tls_gd(bool is_final) { // If we are generating a shared library, then we can't do anything // in the linker. if (parameters->options().shared()) return tls::TLSOPT_NONE; if (!is_final) return tls::TLSOPT_TO_IE; return tls::TLSOPT_TO_LE; } tls::Tls_optimization optimize_tls_ld() { if (parameters->options().shared()) return tls::TLSOPT_NONE; return tls::TLSOPT_TO_LE; } tls::Tls_optimization optimize_tls_ie(bool is_final) { if (!is_final || parameters->options().shared()) return tls::TLSOPT_NONE; return tls::TLSOPT_TO_LE; } // Create glink. void make_glink_section(Layout*); // Create the PLT section. void make_plt_section(Symbol_table*, Layout*); void make_iplt_section(Symbol_table*, Layout*); void make_brlt_section(Layout*); // Create a PLT entry for a global symbol. void make_plt_entry(Symbol_table*, Layout*, Symbol*); // Create a PLT entry for a local IFUNC symbol. void make_local_ifunc_plt_entry(Symbol_table*, Layout*, Sized_relobj_file
*, unsigned int); // Create a GOT entry for local dynamic __tls_get_addr. unsigned int tlsld_got_offset(Symbol_table* symtab, Layout* layout, Sized_relobj_file
* object); unsigned int tlsld_got_offset() const { return this->tlsld_got_offset_; } // Get the dynamic reloc section, creating it if necessary. Reloc_section* rela_dyn_section(Layout*); // Similarly, but for ifunc symbols get the one for ifunc. Reloc_section* rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc); // Copy a relocation against a global symbol. void copy_reloc(Symbol_table* symtab, Layout* layout, Sized_relobj_file
* object, unsigned int shndx, Output_section* output_section, Symbol* sym, const elfcpp::Rela
& reloc) { this->copy_relocs_.copy_reloc(symtab, layout, symtab->get_sized_symbol
(sym), object, shndx, output_section, reloc, this->rela_dyn_section(layout)); } // Look over all the input sections, deciding where to place stubs. void group_sections(Layout*, const Task*, bool); // Sort output sections by address. struct Sort_sections { bool operator()(const Output_section* sec1, const Output_section* sec2) { return sec1->address() < sec2->address(); } }; class Branch_info { public: Branch_info(Powerpc_relobj
* ppc_object, unsigned int data_shndx, Address r_offset, unsigned int r_type, unsigned int r_sym, Address addend) : object_(ppc_object), shndx_(data_shndx), offset_(r_offset), r_type_(r_type), r_sym_(r_sym), addend_(addend) { } ~Branch_info() { } // If this branch needs a plt call stub, or a long branch stub, make one. bool make_stub(Stub_table
*, Stub_table
*, Symbol_table*) const; private: // The branch location.. Powerpc_relobj
* object_; unsigned int shndx_; Address offset_; // ..and the branch type and destination. unsigned int r_type_; unsigned int r_sym_; Address addend_; }; // Information about this specific target which we pass to the // general Target structure. static Target::Target_info powerpc_info; // The types of GOT entries needed for this platform. // These values are exposed to the ABI in an incremental link. // Do not renumber existing values without changing the version // number of the .gnu_incremental_inputs section. enum Got_type { GOT_TYPE_STANDARD, GOT_TYPE_TLSGD, // double entry for @got@tlsgd GOT_TYPE_DTPREL, // entry for @got@dtprel GOT_TYPE_TPREL // entry for @got@tprel }; // The GOT section. Output_data_got_powerpc
* got_; // The PLT section. This is a container for a table of addresses, // and their relocations. Each address in the PLT has a dynamic // relocation (R_*_JMP_SLOT) and each address will have a // corresponding entry in .glink for lazy resolution of the PLT. // ppc32 initialises the PLT to point at the .glink entry, while // ppc64 leaves this to ld.so. To make a call via the PLT, the // linker adds a stub that loads the PLT entry into ctr then // branches to ctr. There may be more than one stub for each PLT // entry. DT_JMPREL points at the first PLT dynamic relocation and // DT_PLTRELSZ gives the total size of PLT dynamic relocations. Output_data_plt_powerpc
* plt_; // The IPLT section. Like plt_, this is a container for a table of // addresses and their relocations, specifically for STT_GNU_IFUNC // functions that resolve locally (STT_GNU_IFUNC functions that // don't resolve locally go in PLT). Unlike plt_, these have no // entry in .glink for lazy resolution, and the relocation section // does not have a 1-1 correspondence with IPLT addresses. In fact, // the relocation section may contain relocations against // STT_GNU_IFUNC symbols at locations outside of IPLT. The // relocation section will appear at the end of other dynamic // relocations, so that ld.so applies these relocations after other // dynamic relocations. In a static executable, the relocation // section is emitted and marked with __rela_iplt_start and // __rela_iplt_end symbols. Output_data_plt_powerpc
* iplt_; // Section holding long branch destinations. Output_data_brlt_powerpc
* brlt_section_; // The .glink section. Output_data_glink
* glink_; // The dynamic reloc section. Reloc_section* rela_dyn_; // Relocs saved to avoid a COPY reloc. Copy_relocs
copy_relocs_; // Offset of the GOT entry for local dynamic __tls_get_addr calls. unsigned int tlsld_got_offset_; Stub_tables stub_tables_; typedef Unordered_map
Branch_lookup_table; Branch_lookup_table branch_lookup_table_; typedef std::vector
Branches; Branches branch_info_; bool plt_thread_safe_; bool relax_failed_; int relax_fail_count_; int32_t stub_group_size_; }; template<> Target::Target_info Target_powerpc<32, true>::powerpc_info = { 32, // size true, // is_big_endian elfcpp::EM_PPC, // machine_code false, // has_make_symbol false, // has_resolve false, // has_code_fill true, // is_default_stack_executable false, // can_icf_inline_merge_sections '\0', // wrap_char "/usr/lib/ld.so.1", // dynamic_linker 0x10000000, // default_text_segment_address 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 4 * 1024, // common_pagesize (overridable by -z common-page-size) false, // isolate_execinstr 0, // rosegment_gap elfcpp::SHN_UNDEF, // small_common_shndx elfcpp::SHN_UNDEF, // large_common_shndx 0, // small_common_section_flags 0, // large_common_section_flags NULL, // attributes_section NULL, // attributes_vendor "_start" // entry_symbol_name }; template<> Target::Target_info Target_powerpc<32, false>::powerpc_info = { 32, // size false, // is_big_endian elfcpp::EM_PPC, // machine_code false, // has_make_symbol false, // has_resolve false, // has_code_fill true, // is_default_stack_executable false, // can_icf_inline_merge_sections '\0', // wrap_char "/usr/lib/ld.so.1", // dynamic_linker 0x10000000, // default_text_segment_address 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 4 * 1024, // common_pagesize (overridable by -z common-page-size) false, // isolate_execinstr 0, // rosegment_gap elfcpp::SHN_UNDEF, // small_common_shndx elfcpp::SHN_UNDEF, // large_common_shndx 0, // small_common_section_flags 0, // large_common_section_flags NULL, // attributes_section NULL, // attributes_vendor "_start" // entry_symbol_name }; template<> Target::Target_info Target_powerpc<64, true>::powerpc_info = { 64, // size true, // is_big_endian elfcpp::EM_PPC64, // machine_code false, // has_make_symbol false, // has_resolve false, // has_code_fill true, // is_default_stack_executable false, // can_icf_inline_merge_sections '\0', // wrap_char "/usr/lib/ld.so.1", // dynamic_linker 0x10000000, // default_text_segment_address 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 4 * 1024, // common_pagesize (overridable by -z common-page-size) false, // isolate_execinstr 0, // rosegment_gap elfcpp::SHN_UNDEF, // small_common_shndx elfcpp::SHN_UNDEF, // large_common_shndx 0, // small_common_section_flags 0, // large_common_section_flags NULL, // attributes_section NULL, // attributes_vendor "_start" // entry_symbol_name }; template<> Target::Target_info Target_powerpc<64, false>::powerpc_info = { 64, // size false, // is_big_endian elfcpp::EM_PPC64, // machine_code false, // has_make_symbol false, // has_resolve false, // has_code_fill true, // is_default_stack_executable false, // can_icf_inline_merge_sections '\0', // wrap_char "/usr/lib/ld.so.1", // dynamic_linker 0x10000000, // default_text_segment_address 64 * 1024, // abi_pagesize (overridable by -z max-page-size) 4 * 1024, // common_pagesize (overridable by -z common-page-size) false, // isolate_execinstr 0, // rosegment_gap elfcpp::SHN_UNDEF, // small_common_shndx elfcpp::SHN_UNDEF, // large_common_shndx 0, // small_common_section_flags 0, // large_common_section_flags NULL, // attributes_section NULL, // attributes_vendor "_start" // entry_symbol_name }; inline bool is_branch_reloc(unsigned int r_type) { return (r_type == elfcpp::R_POWERPC_REL24 || r_type == elfcpp::R_PPC_PLTREL24 || r_type == elfcpp::R_PPC_LOCAL24PC || r_type == elfcpp::R_POWERPC_REL14 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN || r_type == elfcpp::R_POWERPC_ADDR24 || r_type == elfcpp::R_POWERPC_ADDR14 || r_type == elfcpp::R_POWERPC_ADDR14_BRTAKEN || r_type == elfcpp::R_POWERPC_ADDR14_BRNTAKEN); } // If INSN is an opcode that may be used with an @tls operand, return // the transformed insn for TLS optimisation, otherwise return 0. If // REG is non-zero only match an insn with RB or RA equal to REG. uint32_t at_tls_transform(uint32_t insn, unsigned int reg) { if ((insn & (0x3f << 26)) != 31 << 26) return 0; unsigned int rtra; if (reg == 0 || ((insn >> 11) & 0x1f) == reg) rtra = insn & ((1 << 26) - (1 << 16)); else if (((insn >> 16) & 0x1f) == reg) rtra = (insn & (0x1f << 21)) | ((insn & (0x1f << 11)) << 5); else return 0; if ((insn & (0x3ff << 1)) == 266 << 1) // add -> addi insn = 14 << 26; else if ((insn & (0x1f << 1)) == 23 << 1 && ((insn & (0x1f << 6)) < 14 << 6 || ((insn & (0x1f << 6)) >= 16 << 6 && (insn & (0x1f << 6)) < 24 << 6))) // load and store indexed -> dform insn = (32 | ((insn >> 6) & 0x1f)) << 26; else if ((insn & (((0x1a << 5) | 0x1f) << 1)) == 21 << 1) // ldx, ldux, stdx, stdux -> ld, ldu, std, stdu insn = ((58 | ((insn >> 6) & 4)) << 26) | ((insn >> 6) & 1); else if ((insn & (((0x1f << 5) | 0x1f) << 1)) == 341 << 1) // lwax -> lwa insn = (58 << 26) | 2; else return 0; insn |= rtra; return insn; } template
class Powerpc_relocate_functions { public: enum Overflow_check { CHECK_NONE, CHECK_SIGNED, CHECK_UNSIGNED, CHECK_BITFIELD, CHECK_LOW_INSN, CHECK_HIGH_INSN }; enum Status { STATUS_OK, STATUS_OVERFLOW }; private: typedef Powerpc_relocate_functions
This; typedef typename elfcpp::Elf_types
::Elf_Addr Address; template
static inline bool has_overflow_signed(Address value) { // limit = 1 << (valsize - 1) without shift count exceeding size of type Address limit = static_cast
(1) << ((valsize - 1) >> 1); limit <<= ((valsize - 1) >> 1); limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1)); return value + limit > (limit << 1) - 1; } template
static inline bool has_overflow_unsigned(Address value) { Address limit = static_cast
(1) << ((valsize - 1) >> 1); limit <<= ((valsize - 1) >> 1); limit <<= ((valsize - 1) - 2 * ((valsize - 1) >> 1)); return value > (limit << 1) - 1; } template
static inline bool has_overflow_bitfield(Address value) { return (has_overflow_unsigned
(value) && has_overflow_signed
(value)); } template
static inline Status overflowed(Address value, Overflow_check overflow) { if (overflow == CHECK_SIGNED) { if (has_overflow_signed
(value)) return STATUS_OVERFLOW; } else if (overflow == CHECK_UNSIGNED) { if (has_overflow_unsigned
(value)) return STATUS_OVERFLOW; } else if (overflow == CHECK_BITFIELD) { if (has_overflow_bitfield
(value)) return STATUS_OVERFLOW; } return STATUS_OK; } // Do a simple RELA relocation template
static inline Status rela(unsigned char* view, Address value, Overflow_check overflow) { typedef typename elfcpp::Swap
::Valtype Valtype; Valtype* wv = reinterpret_cast
(view); elfcpp::Swap
::writeval(wv, value); return overflowed
(value, overflow); } template
static inline Status rela(unsigned char* view, unsigned int right_shift, typename elfcpp::Valtype_base
::Valtype dst_mask, Address value, Overflow_check overflow) { typedef typename elfcpp::Swap
::Valtype Valtype; Valtype* wv = reinterpret_cast
(view); Valtype val = elfcpp::Swap
::readval(wv); Valtype reloc = value >> right_shift; val &= ~dst_mask; reloc &= dst_mask; elfcpp::Swap
::writeval(wv, val | reloc); return overflowed
(value >> right_shift, overflow); } // Do a simple RELA relocation, unaligned. template
static inline Status rela_ua(unsigned char* view, Address value, Overflow_check overflow) { elfcpp::Swap_unaligned
::writeval(view, value); return overflowed
(value, overflow); } template
static inline Status rela_ua(unsigned char* view, unsigned int right_shift, typename elfcpp::Valtype_base
::Valtype dst_mask, Address value, Overflow_check overflow) { typedef typename elfcpp::Swap_unaligned
::Valtype Valtype; Valtype val = elfcpp::Swap
::readval(view); Valtype reloc = value >> right_shift; val &= ~dst_mask; reloc &= dst_mask; elfcpp::Swap_unaligned
::writeval(view, val | reloc); return overflowed
(value >> right_shift, overflow); } public: // R_PPC64_ADDR64: (Symbol + Addend) static inline void addr64(unsigned char* view, Address value) { This::template rela<64,64>(view, value, CHECK_NONE); } // R_PPC64_UADDR64: (Symbol + Addend) unaligned static inline void addr64_u(unsigned char* view, Address value) { This::template rela_ua<64,64>(view, value, CHECK_NONE); } // R_POWERPC_ADDR32: (Symbol + Addend) static inline Status addr32(unsigned char* view, Address value, Overflow_check overflow) { return This::template rela<32,32>(view, value, overflow); } // R_POWERPC_UADDR32: (Symbol + Addend) unaligned static inline Status addr32_u(unsigned char* view, Address value, Overflow_check overflow) { return This::template rela_ua<32,32>(view, value, overflow); } // R_POWERPC_ADDR24: (Symbol + Addend) & 0x3fffffc static inline Status addr24(unsigned char* view, Address value, Overflow_check overflow) { Status stat = This::template rela<32,26>(view, 0, 0x03fffffc, value, overflow); if (overflow != CHECK_NONE && (value & 3) != 0) stat = STATUS_OVERFLOW; return stat; } // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff static inline Status addr16(unsigned char* view, Address value, Overflow_check overflow) { return This::template rela<16,16>(view, value, overflow); } // R_POWERPC_ADDR16: (Symbol + Addend) & 0xffff, unaligned static inline Status addr16_u(unsigned char* view, Address value, Overflow_check overflow) { return This::template rela_ua<16,16>(view, value, overflow); } // R_POWERPC_ADDR16_DS: (Symbol + Addend) & 0xfffc static inline Status addr16_ds(unsigned char* view, Address value, Overflow_check overflow) { Status stat = This::template rela<16,16>(view, 0, 0xfffc, value, overflow); if (overflow != CHECK_NONE && (value & 3) != 0) stat = STATUS_OVERFLOW; return stat; } // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff static inline void addr16_hi(unsigned char* view, Address value) { This::template rela<16,16>(view, 16, 0xffff, value, CHECK_NONE); } // R_POWERPC_ADDR16_HA: ((Symbol + Addend + 0x8000) >> 16) & 0xffff static inline void addr16_ha(unsigned char* view, Address value) { This::addr16_hi(view, value + 0x8000); } // R_POWERPC_ADDR16_HIGHER: ((Symbol + Addend) >> 32) & 0xffff static inline void addr16_hi2(unsigned char* view, Address value) { This::template rela<16,16>(view, 32, 0xffff, value, CHECK_NONE); } // R_POWERPC_ADDR16_HIGHERA: ((Symbol + Addend + 0x8000) >> 32) & 0xffff static inline void addr16_ha2(unsigned char* view, Address value) { This::addr16_hi2(view, value + 0x8000); } // R_POWERPC_ADDR16_HIGHEST: ((Symbol + Addend) >> 48) & 0xffff static inline void addr16_hi3(unsigned char* view, Address value) { This::template rela<16,16>(view, 48, 0xffff, value, CHECK_NONE); } // R_POWERPC_ADDR16_HIGHESTA: ((Symbol + Addend + 0x8000) >> 48) & 0xffff static inline void addr16_ha3(unsigned char* view, Address value) { This::addr16_hi3(view, value + 0x8000); } // R_POWERPC_ADDR14: (Symbol + Addend) & 0xfffc static inline Status addr14(unsigned char* view, Address value, Overflow_check overflow) { Status stat = This::template rela<32,16>(view, 0, 0xfffc, value, overflow); if (overflow != CHECK_NONE && (value & 3) != 0) stat = STATUS_OVERFLOW; return stat; } }; // Set ABI version for input and output. template
void Powerpc_relobj
::set_abiversion(int ver) { this->e_flags_ |= ver; if (this->abiversion() != 0) { Target_powerpc
* target = static_cast
*>( parameters->sized_target
()); if (target->abiversion() == 0) target->set_abiversion(this->abiversion()); else if (target->abiversion() != this->abiversion()) gold_error(_("%s: ABI version %d is not compatible " "with ABI version %d output"), this->name().c_str(), this->abiversion(), target->abiversion()); } } // Stash away the index of .got2 or .opd in a relocatable object, if // such a section exists. template
bool Powerpc_relobj
::do_find_special_sections( Read_symbols_data* sd) { const unsigned char* const pshdrs = sd->section_headers->data(); const unsigned char* namesu = sd->section_names->data(); const char* names = reinterpret_cast
(namesu); section_size_type names_size = sd->section_names_size; const unsigned char* s; s = this->template find_shdr
(pshdrs, size == 32 ? ".got2" : ".opd", names, names_size, NULL); if (s != NULL) { unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes
::shdr_size; this->special_ = ndx; if (size == 64) { if (this->abiversion() == 0) this->set_abiversion(1); else if (this->abiversion() > 1) gold_error(_("%s: .opd invalid in abiv%d"), this->name().c_str(), this->abiversion()); } } return Sized_relobj_file
::do_find_special_sections(sd); } // Examine .rela.opd to build info about function entry points. template
void Powerpc_relobj
::scan_opd_relocs( size_t reloc_count, const unsigned char* prelocs, const unsigned char* plocal_syms) { if (size == 64) { typedef typename Reloc_types
::Reloc Reltype; const int reloc_size = Reloc_types
::reloc_size; const int sym_size = elfcpp::Elf_sizes
::sym_size; Address expected_off = 0; bool regular = true; unsigned int opd_ent_size = 0; for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) { Reltype reloc(prelocs); typename elfcpp::Elf_types
::Elf_WXword r_info = reloc.get_r_info(); unsigned int r_type = elfcpp::elf_r_type
(r_info); if (r_type == elfcpp::R_PPC64_ADDR64) { unsigned int r_sym = elfcpp::elf_r_sym
(r_info); typename elfcpp::Elf_types
::Elf_Addr value; bool is_ordinary; unsigned int shndx; if (r_sym < this->local_symbol_count()) { typename elfcpp::Sym
lsym(plocal_syms + r_sym * sym_size); shndx = lsym.get_st_shndx(); shndx = this->adjust_sym_shndx(r_sym, shndx, &is_ordinary); value = lsym.get_st_value(); } else shndx = this->symbol_section_and_value(r_sym, &value, &is_ordinary); this->set_opd_ent(reloc.get_r_offset(), shndx, value + reloc.get_r_addend()); if (i == 2) { expected_off = reloc.get_r_offset(); opd_ent_size = expected_off; } else if (expected_off != reloc.get_r_offset()) regular = false; expected_off += opd_ent_size; } else if (r_type == elfcpp::R_PPC64_TOC) { if (expected_off - opd_ent_size + 8 != reloc.get_r_offset()) regular = false; } else { gold_warning(_("%s: unexpected reloc type %u in .opd section"), this->name().c_str(), r_type); regular = false; } } if (reloc_count <= 2) opd_ent_size = this->section_size(this->opd_shndx()); if (opd_ent_size != 24 && opd_ent_size != 16) regular = false; if (!regular) { gold_warning(_("%s: .opd is not a regular array of opd entries"), this->name().c_str()); opd_ent_size = 0; } } } template
void Powerpc_relobj
::do_read_relocs(Read_relocs_data* rd) { Sized_relobj_file
::do_read_relocs(rd); if (size == 64) { for (Read_relocs_data::Relocs_list::iterator p = rd->relocs.begin(); p != rd->relocs.end(); ++p) { if (p->data_shndx == this->opd_shndx()) { uint64_t opd_size = this->section_size(this->opd_shndx()); gold_assert(opd_size == static_cast
(opd_size)); if (opd_size != 0) { this->init_opd(opd_size); this->scan_opd_relocs(p->reloc_count, p->contents->data(), rd->local_symbols->data()); } break; } } } } // Read the symbols then set up st_other vector. template
void Powerpc_relobj
::do_read_symbols(Read_symbols_data* sd) { this->base_read_symbols(sd); if (size == 64) { const int shdr_size = elfcpp::Elf_sizes
::shdr_size; const unsigned char* const pshdrs = sd->section_headers->data(); const unsigned int loccount = this->do_local_symbol_count(); if (loccount != 0) { this->st_other_.resize(loccount); const int sym_size = elfcpp::Elf_sizes
::sym_size; off_t locsize = loccount * sym_size; const unsigned int symtab_shndx = this->symtab_shndx(); const unsigned char *psymtab = pshdrs + symtab_shndx * shdr_size; typename elfcpp::Shdr
shdr(psymtab); const unsigned char* psyms = this->get_view(shdr.get_sh_offset(), locsize, true, false); psyms += sym_size; for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size) { elfcpp::Sym
sym(psyms); unsigned char st_other = sym.get_st_other(); this->st_other_[i] = st_other; if ((st_other & elfcpp::STO_PPC64_LOCAL_MASK) != 0) { if (this->abiversion() == 0) this->set_abiversion(2); else if (this->abiversion() < 2) gold_error(_("%s: local symbol %d has invalid st_other" " for ABI version 1"), this->name().c_str(), i); } } } } } template
void Powerpc_dynobj
::set_abiversion(int ver) { this->e_flags_ |= ver; if (this->abiversion() != 0) { Target_powerpc
* target = static_cast
*>( parameters->sized_target
()); if (target->abiversion() == 0) target->set_abiversion(this->abiversion()); else if (target->abiversion() != this->abiversion()) gold_error(_("%s: ABI version %d is not compatible " "with ABI version %d output"), this->name().c_str(), this->abiversion(), target->abiversion()); } } // Call Sized_dynobj::base_read_symbols to read the symbols then // read .opd from a dynamic object, filling in opd_ent_ vector, template
void Powerpc_dynobj
::do_read_symbols(Read_symbols_data* sd) { this->base_read_symbols(sd); if (size == 64) { const int shdr_size = elfcpp::Elf_sizes
::shdr_size; const unsigned char* const pshdrs = sd->section_headers->data(); const unsigned char* namesu = sd->section_names->data(); const char* names = reinterpret_cast
(namesu); const unsigned char* s = NULL; const unsigned char* opd; section_size_type opd_size; // Find and read .opd section. while (1) { s = this->template find_shdr
(pshdrs, ".opd", names, sd->section_names_size, s); if (s == NULL) return; typename elfcpp::Shdr
shdr(s); if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0) { if (this->abiversion() == 0) this->set_abiversion(1); else if (this->abiversion() > 1) gold_error(_("%s: .opd invalid in abiv%d"), this->name().c_str(), this->abiversion()); this->opd_shndx_ = (s - pshdrs) / shdr_size; this->opd_address_ = shdr.get_sh_addr(); opd_size = convert_to_section_size_type(shdr.get_sh_size()); opd = this->get_view(shdr.get_sh_offset(), opd_size, true, false); break; } } // Build set of executable sections. // Using a set is probably overkill. There is likely to be only // a few executable sections, typically .init, .text and .fini, // and they are generally grouped together. typedef std::set
Exec_sections; Exec_sections exec_sections; s = pshdrs; for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size) { typename elfcpp::Shdr
shdr(s); if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS && ((shdr.get_sh_flags() & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR)) && shdr.get_sh_size() != 0) { exec_sections.insert(Sec_info(shdr.get_sh_addr(), shdr.get_sh_size(), i)); } } if (exec_sections.empty()) return; // Look over the OPD entries. This is complicated by the fact // that some binaries will use two-word entries while others // will use the standard three-word entries. In most cases // the third word (the environment pointer for languages like // Pascal) is unused and will be zero. If the third word is // used it should not be pointing into executable sections, // I think. this->init_opd(opd_size); for (const unsigned char* p = opd; p < opd + opd_size; p += 8) { typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype; const Valtype* valp = reinterpret_cast
(p); Valtype val = elfcpp::Swap<64, big_endian>::readval(valp); if (val == 0) // Chances are that this is the third word of an OPD entry. continue; typename Exec_sections::const_iterator e = exec_sections.upper_bound(Sec_info(val, 0, 0)); if (e != exec_sections.begin()) { --e; if (e->start <= val && val < e->start + e->len) { // We have an address in an executable section. // VAL ought to be the function entry, set it up. this->set_opd_ent(p - opd, e->shndx, val); // Skip second word of OPD entry, the TOC pointer. p += 8; } } // If we didn't match any executable sections, we likely // have a non-zero third word in the OPD entry. } } } // Set up some symbols. template
void Target_powerpc
::do_define_standard_symbols( Symbol_table* symtab, Layout* layout) { if (size == 32) { // Define _GLOBAL_OFFSET_TABLE_ to ensure it isn't seen as // undefined when scanning relocs (and thus requires // non-relative dynamic relocs). The proper value will be // updated later. Symbol *gotsym = symtab->lookup("_GLOBAL_OFFSET_TABLE_", NULL); if (gotsym != NULL && gotsym->is_undefined()) { Target_powerpc
* target = static_cast
*>( parameters->sized_target
()); Output_data_got_powerpc
* got = target->got_section(symtab, layout); symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, Symbol_table::PREDEFINED, got, 0, 0, elfcpp::STT_OBJECT, elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0, false, false); } // Define _SDA_BASE_ at the start of the .sdata section + 32768. Symbol *sdasym = symtab->lookup("_SDA_BASE_", NULL); if (sdasym != NULL && sdasym->is_undefined()) { Output_data_space* sdata = new Output_data_space(4, "** sdata"); Output_section* os = layout->add_output_section_data(".sdata", 0, elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, sdata, ORDER_SMALL_DATA, false); symtab->define_in_output_data("_SDA_BASE_", NULL, Symbol_table::PREDEFINED, os, 32768, 0, elfcpp::STT_OBJECT, elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0, false, false); } } else { // Define .TOC. as for 32-bit _GLOBAL_OFFSET_TABLE_ Symbol *gotsym = symtab->lookup(".TOC.", NULL); if (gotsym != NULL && gotsym->is_undefined()) { Target_powerpc
* target = static_cast
*>( parameters->sized_target
()); Output_data_got_powerpc
* got = target->got_section(symtab, layout); symtab->define_in_output_data(".TOC.", NULL, Symbol_table::PREDEFINED, got, 0x8000, 0, elfcpp::STT_OBJECT, elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0, false, false); } } } // Set up PowerPC target specific relobj. template
Object* Target_powerpc
::do_make_elf_object( const std::string& name, Input_file* input_file, off_t offset, const elfcpp::Ehdr
& ehdr) { int et = ehdr.get_e_type(); // ET_EXEC files are valid input for --just-symbols/-R, // and we treat them as relocatable objects. if (et == elfcpp::ET_REL || (et == elfcpp::ET_EXEC && input_file->just_symbols())) { Powerpc_relobj
* obj = new Powerpc_relobj
(name, input_file, offset, ehdr); obj->setup(); return obj; } else if (et == elfcpp::ET_DYN) { Powerpc_dynobj
* obj = new Powerpc_dynobj
(name, input_file, offset, ehdr); obj->setup(); return obj; } else { gold_error(_("%s: unsupported ELF file type %d"), name.c_str(), et); return NULL; } } template
class Output_data_got_powerpc : public Output_data_got
{ public: typedef typename elfcpp::Elf_types
::Elf_Addr Valtype; typedef Output_data_reloc
Rela_dyn; Output_data_got_powerpc(Symbol_table* symtab, Layout* layout) : Output_data_got
(), symtab_(symtab), layout_(layout), header_ent_cnt_(size == 32 ? 3 : 1), header_index_(size == 32 ? 0x2000 : 0) { } // Override all the Output_data_got methods we use so as to first call // reserve_ent(). bool add_global(Symbol* gsym, unsigned int got_type) { this->reserve_ent(); return Output_data_got
::add_global(gsym, got_type); } bool add_global_plt(Symbol* gsym, unsigned int got_type) { this->reserve_ent(); return Output_data_got
::add_global_plt(gsym, got_type); } bool add_global_tls(Symbol* gsym, unsigned int got_type) { return this->add_global_plt(gsym, got_type); } void add_global_with_rel(Symbol* gsym, unsigned int got_type, Output_data_reloc_generic* rel_dyn, unsigned int r_type) { this->reserve_ent(); Output_data_got
:: add_global_with_rel(gsym, got_type, rel_dyn, r_type); } void add_global_pair_with_rel(Symbol* gsym, unsigned int got_type, Output_data_reloc_generic* rel_dyn, unsigned int r_type_1, unsigned int r_type_2) { this->reserve_ent(2); Output_data_got
:: add_global_pair_with_rel(gsym, got_type, rel_dyn, r_type_1, r_type_2); } bool add_local(Relobj* object, unsigned int sym_index, unsigned int got_type) { this->reserve_ent(); return Output_data_got
::add_local(object, sym_index, got_type); } bool add_local_plt(Relobj* object, unsigned int sym_index, unsigned int got_type) { this->reserve_ent(); return Output_data_got
::add_local_plt(object, sym_index, got_type); } bool add_local_tls(Relobj* object, unsigned int sym_index, unsigned int got_type) { return this->add_local_plt(object, sym_index, got_type); } void add_local_tls_pair(Relobj* object, unsigned int sym_index, unsigned int got_type, Output_data_reloc_generic* rel_dyn, unsigned int r_type) { this->reserve_ent(2); Output_data_got
:: add_local_tls_pair(object, sym_index, got_type, rel_dyn, r_type); } unsigned int add_constant(Valtype constant) { this->reserve_ent(); return Output_data_got
::add_constant(constant); } unsigned int add_constant_pair(Valtype c1, Valtype c2) { this->reserve_ent(2); return Output_data_got
::add_constant_pair(c1, c2); } // Offset of _GLOBAL_OFFSET_TABLE_. unsigned int g_o_t() const { return this->got_offset(this->header_index_); } // Offset of base used to access the GOT/TOC. // The got/toc pointer reg will be set to this value. Valtype got_base_offset(const Powerpc_relobj
* object) const { if (size == 32) return this->g_o_t(); else return (this->output_section()->address() + object->toc_base_offset() - this->address()); } // Ensure our GOT has a header. void set_final_data_size() { if (this->header_ent_cnt_ != 0) this->make_header(); Output_data_got
::set_final_data_size(); } // First word of GOT header needs some values that are not // handled by Output_data_got so poke them in here. // For 32-bit, address of .dynamic, for 64-bit, address of TOCbase. void do_write(Output_file* of) { Valtype val = 0; if (size == 32 && this->layout_->dynamic_data() != NULL) val = this->layout_->dynamic_section()->address(); if (size == 64) val = this->output_section()->address() + 0x8000; this->replace_constant(this->header_index_, val); Output_data_got
::do_write(of); } private: void reserve_ent(unsigned int cnt = 1) { if (this->header_ent_cnt_ == 0) return; if (this->num_entries() + cnt > this->header_index_) this->make_header(); } void make_header() { this->header_ent_cnt_ = 0; this->header_index_ = this->num_entries(); if (size == 32) { Output_data_got
::add_constant(0); Output_data_got
::add_constant(0); Output_data_got
::add_constant(0); // Define _GLOBAL_OFFSET_TABLE_ at the header Symbol *gotsym = this->symtab_->lookup("_GLOBAL_OFFSET_TABLE_", NULL); if (gotsym != NULL) { Sized_symbol
* sym = static_cast
*>(gotsym); sym->set_value(this->g_o_t()); } else this->symtab_->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, Symbol_table::PREDEFINED, this, this->g_o_t(), 0, elfcpp::STT_OBJECT, elfcpp::STB_LOCAL, elfcpp::STV_HIDDEN, 0, false, false); } else Output_data_got
::add_constant(0); } // Stashed pointers. Symbol_table* symtab_; Layout* layout_; // GOT header size. unsigned int header_ent_cnt_; // GOT header index. unsigned int header_index_; }; // Get the GOT section, creating it if necessary. template
Output_data_got_powerpc
* Target_powerpc
::got_section(Symbol_table* symtab, Layout* layout) { if (this->got_ == NULL) { gold_assert(symtab != NULL && layout != NULL); this->got_ = new Output_data_got_powerpc
(symtab, layout); layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, this->got_, ORDER_DATA, false); } return this->got_; } // Get the dynamic reloc section, creating it if necessary. template
typename Target_powerpc
::Reloc_section* Target_powerpc
::rela_dyn_section(Layout* layout) { if (this->rela_dyn_ == NULL) { gold_assert(layout != NULL); this->rela_dyn_ = new Reloc_section(parameters->options().combreloc()); layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, elfcpp::SHF_ALLOC, this->rela_dyn_, ORDER_DYNAMIC_RELOCS, false); } return this->rela_dyn_; } // Similarly, but for ifunc symbols get the one for ifunc. template
typename Target_powerpc
::Reloc_section* Target_powerpc
::rela_dyn_section(Symbol_table* symtab, Layout* layout, bool for_ifunc) { if (!for_ifunc) return this->rela_dyn_section(layout); if (this->iplt_ == NULL) this->make_iplt_section(symtab, layout); return this->iplt_->rel_plt(); } class Stub_control { public: // Determine the stub group size. The group size is the absolute // value of the parameter --stub-group-size. If --stub-group-size // is passed a negative value, we restrict stubs to be always before // the stubbed branches. Stub_control(int32_t size, bool no_size_errors) : state_(NO_GROUP), stub_group_size_(abs(size)), stub14_group_size_(abs(size) >> 10), stubs_always_before_branch_(size < 0), suppress_size_errors_(no_size_errors), group_end_addr_(0), owner_(NULL), output_section_(NULL) { } // Return true iff input section can be handled by current stub // group. bool can_add_to_stub_group(Output_section* o, const Output_section::Input_section* i, bool has14); const Output_section::Input_section* owner() { return owner_; } Output_section* output_section() { return output_section_; } void set_output_and_owner(Output_section* o, const Output_section::Input_section* i) { this->output_section_ = o; this->owner_ = i; } private: typedef enum { NO_GROUP, FINDING_STUB_SECTION, HAS_STUB_SECTION } State; State state_; uint32_t stub_group_size_; uint32_t stub14_group_size_; bool stubs_always_before_branch_; bool suppress_size_errors_; uint64_t group_end_addr_; const Output_section::Input_section* owner_; Output_section* output_section_; }; // Return true iff input section can be handled by current stub // group. bool Stub_control::can_add_to_stub_group(Output_section* o, const Output_section::Input_section* i, bool has14) { uint32_t group_size = has14 ? this->stub14_group_size_ : this->stub_group_size_; bool whole_sec = o->order() == ORDER_INIT || o->order() == ORDER_FINI; uint64_t this_size; uint64_t start_addr = o->address(); if (whole_sec) // .init and .fini sections are pasted together to form a single // function. We can't be adding stubs in the middle of the function. this_size = o->data_size(); else { start_addr += i->relobj()->output_section_offset(i->shndx()); this_size = i->data_size(); } uint64_t end_addr = start_addr + this_size; bool toobig = this_size > group_size; if (toobig && !this->suppress_size_errors_) gold_warning(_("%s:%s exceeds group size"), i->relobj()->name().c_str(), i->relobj()->section_name(i->shndx()).c_str()); if (this->state_ != HAS_STUB_SECTION && (!whole_sec || this->output_section_ != o) && (this->state_ == NO_GROUP || this->group_end_addr_ - end_addr < group_size)) { this->owner_ = i; this->output_section_ = o; } if (this->state_ == NO_GROUP) { this->state_ = FINDING_STUB_SECTION; this->group_end_addr_ = end_addr; } else if (this->group_end_addr_ - start_addr < group_size) ; // Adding this section would make the group larger than GROUP_SIZE. else if (this->state_ == FINDING_STUB_SECTION && !this->stubs_always_before_branch_ && !toobig) { // But wait, there's more! Input sections up to GROUP_SIZE // bytes before the stub table can be handled by it too. this->state_ = HAS_STUB_SECTION; this->group_end_addr_ = end_addr; } else { this->state_ = NO_GROUP; return false; } return true; } // Look over all the input sections, deciding where to place stubs. template
void Target_powerpc
::group_sections(Layout* layout, const Task*, bool no_size_errors) { Stub_control stub_control(this->stub_group_size_, no_size_errors); // Group input sections and insert stub table Stub_table_owner* table_owner = NULL; std::vector
tables; Layout::Section_list section_list; layout->get_executable_sections(§ion_list); std::stable_sort(section_list.begin(), section_list.end(), Sort_sections()); for (Layout::Section_list::reverse_iterator o = section_list.rbegin(); o != section_list.rend(); ++o) { typedef Output_section::Input_section_list Input_section_list; for (Input_section_list::const_reverse_iterator i = (*o)->input_sections().rbegin(); i != (*o)->input_sections().rend(); ++i) { if (i->is_input_section() || i->is_relaxed_input_section()) { Powerpc_relobj
* ppcobj = static_cast
*>(i->relobj()); bool has14 = ppcobj->has_14bit_branch(i->shndx()); if (!stub_control.can_add_to_stub_group(*o, &*i, has14)) { table_owner->output_section = stub_control.output_section(); table_owner->owner = stub_control.owner(); stub_control.set_output_and_owner(*o, &*i); table_owner = NULL; } if (table_owner == NULL) { table_owner = new Stub_table_owner; tables.push_back(table_owner); } ppcobj->set_stub_table(i->shndx(), tables.size() - 1); } } } if (table_owner != NULL) { const Output_section::Input_section* i = stub_control.owner(); if (tables.size() >= 2 && tables[tables.size() - 2]->owner == i) { // Corner case. A new stub group was made for the first // section (last one looked at here) for some reason, but // the first section is already being used as the owner for // a stub table for following sections. Force it into that // stub group. tables.pop_back(); delete table_owner; Powerpc_relobj
* ppcobj = static_cast
*>(i->relobj()); ppcobj->set_stub_table(i->shndx(), tables.size() - 1); } else { table_owner->output_section = stub_control.output_section(); table_owner->owner = i; } } for (typename std::vector
::iterator t = tables.begin(); t != tables.end(); ++t) { Stub_table
* stub_table; if ((*t)->owner->is_input_section()) stub_table = new Stub_table
(this, (*t)->output_section, (*t)->owner); else if ((*t)->owner->is_relaxed_input_section()) stub_table = static_cast
*>( (*t)->owner->relaxed_input_section()); else gold_unreachable(); this->stub_tables_.push_back(stub_table); delete *t; } } static unsigned long max_branch_delta (unsigned int r_type) { if (r_type == elfcpp::R_POWERPC_REL14 || r_type == elfcpp::R_POWERPC_REL14_BRTAKEN || r_type == elfcpp::R_POWERPC_REL14_BRNTAKEN) return 1L << 15; if (r_type == elfcpp::R_POWERPC_REL24 || r_type == elfcpp::R_PPC_PLTREL24 || r_type == elfcpp::R_PPC_LOCAL24PC) return 1L << 25; return 0; } // If this branch needs a plt call stub, or a long branch stub, make one. template
bool Target_powerpc
::Branch_info::make_stub( Stub_table
* stub_table, Stub_table
* ifunc_stub_table, Symbol_table* symtab) const { Symbol* sym = this->object_->global_symbol(this->r_sym_); if (sym != NULL && sym->is_forwarder()) sym = symtab->resolve_forwards(sym); const Sized_symbol
* gsym = static_cast
*>(sym); Target_powerpc
* target = static_cast
*>( parameters->sized_target
()); if (gsym != NULL ? gsym->use_plt_offset(Scan::get_reference_flags(this->r_type_, target)) : this->object_->local_has_plt_offset(this->r_sym_)) { if (size == 64 && gsym != NULL && target->abiversion() >= 2 && !parameters->options().output_is_position_independent() && !is_branch_reloc(this->r_type_)) target->glink_section()->add_global_entry(gsym); else { if (stub_table == NULL) stub_table = this->object_->stub_table(this->shndx_); if (stub_table == NULL) { // This is a ref from a data section to an ifunc symbol. stub_table = ifunc_stub_table; } gold_assert(stub_table != NULL); Address from = this->object_->get_output_section_offset(this->shndx_); if (from != invalid_address) from += (this->object_->output_section(this->shndx_)->address() + this->offset_); if (gsym != NULL) return stub_table->add_plt_call_entry(from, this->object_, gsym, this->r_type_, this->addend_); else return stub_table->add_plt_call_entry(from, this->object_, this->r_sym_, this->r_type_, this->addend_); } } else { Address max_branch_offset = max_branch_delta(this->r_type_); if (max_branch_offset == 0) return true; Address from = this->object_->get_output_section_offset(this->shndx_); gold_assert(from != invalid_address); from += (this->object_->output_section(this->shndx_)->address() + this->offset_); Address to; if (gsym != NULL) { switch (gsym->source()) { case Symbol::FROM_OBJECT: { Object* symobj = gsym->object(); if (symobj->is_dynamic() || symobj->pluginobj() != NULL) return true; bool is_ordinary; unsigned int shndx = gsym->shndx(&is_ordinary); if (shndx == elfcpp::SHN_UNDEF) return true; } break; case Symbol::IS_UNDEFINED: return true; default: break; } Symbol_table::Compute_final_value_status status; to = symtab->compute_final_value