Index: gold/mips.cc |
=================================================================== |
new file mode 100755 |
--- /dev/null |
+++ b/gold/mips.cc |
@@ -0,0 +1,4671 @@ |
+// mips.cc -- mips target support for gold. |
+ |
+// Copyright 2011 Free Software Foundation, Inc. |
+// Written by Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com> |
+// based on the i386 code by Ian Lance Taylor <iant@google.com>. |
+// This file also contains borrowed and adapted code from |
+// bfd/elf32-mips.c. |
+ |
+// 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 <set> |
+#include <sstream> |
+ |
+#include "gold.h" |
+ |
+#include "elfcpp.h" |
+#include "parameters.h" |
+#include "reloc.h" |
+#include "mips.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<int size, bool big_endian> |
+class Mips_output_data_plt; |
+ |
+template<int size, bool big_endian> |
+class Mips_output_data_got; |
+ |
+template<int size, bool big_endian> |
+class Target_mips; |
+ |
+template<int size> |
+class Mips_output_section_reginfo; |
+ |
+template<int size, bool big_endian> |
+class Mips_output_data_stub; |
+ |
+template<int size, bool big_endian> |
+class Mips_output_data_mips_stubs; |
+ |
+// Mips_output_data_got class. We derive this from Output_data_got to add |
+// extra methods to handle TLS relocations. |
+ |
+template<int size, bool big_endian> |
+class Mips_output_data_got : public Output_data_got<size, big_endian> |
+{ |
+ public: |
+ typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> |
+ Reloc_section; |
+ |
+ Mips_output_data_got(Symbol_table* symtab, Layout* layout) |
+ : Output_data_got<size, big_endian>(), symbol_table_(symtab), |
+ layout_(layout), tls_entries_(0), tls_entries_set_(false) |
+ { |
+ // According to MIPS O32 ABI zero entry of GOT is reserved. |
+ this->add_constant(0); |
+ // Module pointer (GNU extension) |
+ this->add_constant(0x80000000); |
+ } |
+ |
+ // Add a static entry for the GOT entry at OFFSET. GSYM is a global |
+ // symbol and R_TYPE is the code of a dynamic relocation that needs to be |
+ // applied in a static link. |
+ void |
+ add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym) |
+ { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); } |
+ |
+ // Add a static reloc for the GOT entry at OFFSET. RELOBJ is an object |
+ // defining a local symbol with INDEX. R_TYPE is the code of a dynamic |
+ // relocation that needs to be applied in a static link. |
+ void |
+ add_static_reloc(unsigned int got_offset, unsigned int r_type, |
+ Sized_relobj_file<size, big_endian>* relobj, |
+ unsigned int index) |
+ { |
+ this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj, |
+ index)); |
+ } |
+ |
+ void |
+ add_tls_gd_with_static_reloc(unsigned int got_type, Symbol* gsym); |
+ |
+ // Same as the above but for a local symbol in OBJECT with INDEX. |
+ void |
+ add_tls_gd_local_reloc(unsigned int got_type, |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int index); |
+ |
+ void |
+ add_got16_view(std::string key, Mips_output_data_got<size, big_endian>* got) |
+ |
+ { |
+ if(got16_views_.count(key) == 0) |
+ got16_views_[key] = got->add_constant(0); |
+ } |
+ |
+ unsigned int |
+ got16_offset(std::string key) |
+ { |
+ if(got16_views_.count(key) == 0) |
+ gold_error("internal error in GOT16 relocation"); |
+ |
+ return got16_views_[key]; |
+ } |
+ |
+ unsigned char* |
+ got_view() |
+ { return this->got_view_; } |
+ |
+ Symbol_table* |
+ symbol_table() |
+ { return this->symbol_table_; } |
+ |
+ Layout* |
+ layout() |
+ { return this->layout_; } |
+ |
+ void |
+ add_global_with_reloc_mips(Symbol* gsym) |
+ { |
+ this->global_relocs_.push(gsym); |
+ } |
+ |
+ void |
+ add_global_tls_with_reloc_mips(Symbol* gsym, Reloc_section* rel_dyn, |
+ unsigned int r_type) |
+ { |
+ this->tls_relocs_.push(Tls_reloc(gsym, rel_dyn, r_type)); |
+ } |
+ |
+ void |
+ add_global_relocs() |
+ { |
+ while(!this->global_relocs_.empty()) |
+ { |
+ Symbol *gsym = this->global_relocs_.front(); |
+ |
+ this->add_global(gsym, 0); |
+ gsym->set_needs_dynsym_entry(); |
+ this->global_relocs_.pop(); |
+ } |
+ } |
+ |
+ void |
+ add_global_tls_relocs() |
+ { |
+ while(!this->tls_relocs_.empty()) |
+ { |
+ Tls_reloc tls_rel = this->tls_relocs_.front(); |
+ Symbol *gsym = tls_rel.symbol(); |
+ |
+ if(!gsym->has_got_offset(1)) |
+ { |
+ unsigned int r_type = tls_rel.r_type(); |
+ Reloc_section* rel_dyn = tls_rel.rel_dyn(); |
+ |
+ this->tls_entries_++; |
+ |
+ if(parameters->options().output_is_position_independent()) |
+ { |
+ this->add_global(gsym, 1); |
+ rel_dyn->add_absolute(r_type, this, gsym->got_offset(1)); |
+ } |
+ else |
+ { |
+ this->add_global_with_rel(gsym, 1, rel_dyn, r_type); |
+ } |
+ |
+ gsym->set_needs_dynsym_entry(); |
+ } |
+ |
+ this->tls_relocs_.pop(); |
+ } |
+ |
+ this->tls_entries_set_ = true; |
+ } |
+ |
+ unsigned int |
+ tls_entries() |
+ { |
+ if(this->tls_entries_set_) |
+ return this->tls_entries_; |
+ |
+ return 0; |
+ } |
+ protected: |
+ // Write out the GOT table. |
+ void |
+ do_write(Output_file*); |
+ |
+ private: |
+ class Tls_reloc |
+ { |
+ public: |
+ Tls_reloc(Symbol* gsym, Reloc_section* rel_dyn, unsigned int r_type) |
+ : symbol_(gsym), rel_dyn_(rel_dyn), r_type_(r_type) |
+ { } |
+ |
+ Symbol* |
+ symbol() |
+ { return this->symbol_; } |
+ |
+ Reloc_section* |
+ rel_dyn() |
+ { return this->rel_dyn_; } |
+ |
+ unsigned int |
+ r_type() |
+ { return this->r_type_; } |
+ |
+ private: |
+ Symbol* symbol_; |
+ Reloc_section* rel_dyn_; |
+ unsigned int r_type_; |
+ }; |
+ |
+ // This class represent dynamic relocations that need to be applied by |
+ // gold because we are using TLS relocations in a static link. |
+ class Static_reloc |
+ { |
+ public: |
+ Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym) |
+ : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true) |
+ { this->u_.global.symbol = gsym; } |
+ |
+ Static_reloc(unsigned int got_offset, unsigned int r_type, |
+ Sized_relobj_file<size, big_endian>* relobj, unsigned int index) |
+ : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false) |
+ { |
+ this->u_.local.relobj = relobj; |
+ this->u_.local.index = index; |
+ } |
+ |
+ // Return the GOT offset. |
+ unsigned int |
+ got_offset() const |
+ { return this->got_offset_; } |
+ |
+ // Relocation type. |
+ unsigned int |
+ r_type() const |
+ { return this->r_type_; } |
+ |
+ // Whether the symbol is global or not. |
+ bool |
+ symbol_is_global() const |
+ { return this->symbol_is_global_; } |
+ |
+ // For a relocation against a global symbol, the global symbol. |
+ Symbol* |
+ symbol() const |
+ { |
+ gold_assert(this->symbol_is_global_); |
+ return this->u_.global.symbol; |
+ } |
+ |
+ // For a relocation against a local symbol, the defining object. |
+ Sized_relobj_file<size, big_endian>* |
+ relobj() const |
+ { |
+ gold_assert(!this->symbol_is_global_); |
+ return this->u_.local.relobj; |
+ } |
+ |
+ // For a relocation against a local symbol, the local symbol index. |
+ unsigned int |
+ index() const |
+ { |
+ gold_assert(!this->symbol_is_global_); |
+ return this->u_.local.index; |
+ } |
+ |
+ private: |
+ // GOT offset of the entry to which this relocation is applied. |
+ unsigned int got_offset_; |
+ // Type of relocation. |
+ unsigned int r_type_; |
+ // Whether this relocation is against a global symbol. |
+ bool symbol_is_global_; |
+ // A global or local symbol. |
+ union |
+ { |
+ struct |
+ { |
+ // For a global symbol, the symbol itself. |
+ Symbol* symbol; |
+ } global; |
+ struct |
+ { |
+ // For a local symbol, the object defining object. |
+ Sized_relobj_file<size, big_endian>* relobj; |
+ // For a local symbol, the symbol index. |
+ unsigned int index; |
+ } local; |
+ } u_; |
+ }; |
+ |
+ // Symbol table of the output object. |
+ Symbol_table* symbol_table_; |
+ // Layout of the output object. |
+ Layout* layout_; |
+ // Static relocs to be applied to the GOT. |
+ std::vector<Static_reloc> static_relocs_; |
+ // Got16 relocs that are related to the GOT |
+ std::map<std::string, unsigned int> got16_views_; |
+ // .got section view |
+ unsigned char* got_view_; |
+ // List of global relocs that should be at the end of .got section |
+ std::queue<Symbol *> global_relocs_; |
+ // List of global tls relocs that should be at the end of .got section |
+ std::queue<Tls_reloc> tls_relocs_; |
+ // Number of TLS entries |
+ unsigned int tls_entries_; |
+ // Is number of TLS entries set |
+ bool tls_entries_set_; |
+}; |
+ |
+// Mips reginfo output section class |
+ |
+template<int size> |
+class Mips_output_section_reginfo : public Output_section |
+{ |
+ public: |
+ Mips_output_section_reginfo (const char *name, elfcpp::Elf_Word type, |
+ elfcpp::Elf_Xword flags):Output_section (name, type, flags) |
+ { |
+ this->set_always_keeps_input_sections(); |
+ |
+ gp_ = NULL; |
+ |
+ for(size_t i = 0; i < 24; i++) |
+ data_[i] = 0; |
+ } |
+ |
+ ~Mips_output_section_reginfo() |
+ { }; |
+ |
+ void |
+ set_gp(Sized_symbol<size> *gp) |
+ { this->gp_ = gp; } |
+ |
+ // Downcast a base pointer to an Mips_output_section_reginfo pointer. |
+ static Mips_output_section_reginfo<size>* |
+ as_mips_output_section_reginfo(Output_section* os) |
+ { return static_cast<Mips_output_section_reginfo<size>*>(os); } |
+ |
+ protected: |
+ // Set the final data size. |
+ void set_final_data_size(); |
+ |
+ // Write reginfo section into file OF. |
+ void do_write (Output_file * of); |
+ |
+ private: |
+ // Data. |
+ unsigned char data_[24]; |
+ |
+ // _gp symbol |
+ Sized_symbol<size>* gp_; |
+}; |
+ |
+// The MIPS target has many relocation types with odd-sizes or noncontiguous |
+// bits. The default handling of relocatable relocation cannot process these |
+// relocations. So we have to extend the default code. |
+ |
+template<bool big_endian, int sh_type, typename Classify_reloc> |
+class Mips_scan_relocatable_relocs : |
+ public Default_scan_relocatable_relocs<sh_type, Classify_reloc> |
+{ |
+ public: |
+ // Return the strategy to use for a local symbol which is a section |
+ // symbol, given the relocation type. |
+ inline Relocatable_relocs::Reloc_strategy |
+ local_section_strategy(unsigned int r_type, Relobj *object) |
+ { |
+ switch(r_type) |
+ { |
+ case elfcpp::R_MIPS_26: |
+ return Relocatable_relocs::RELOC_SPECIAL; |
+ |
+ default: |
+ return Default_scan_relocatable_relocs<sh_type, Classify_reloc>::local_section_strategy(r_type, object); |
+ } |
+ } |
+}; |
+ |
+template<int size, bool big_endian> |
+class Target_mips : public Sized_target<size, big_endian> |
+{ |
+ public: |
+ typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian> |
+ Reloc_section; |
+ typedef Unordered_map<const char*, Symbol*> Stub_symbol_info; |
+ |
+ Target_mips() |
+ : Sized_target<size, big_endian>(&mips_info), |
+ got_(NULL), plt_(NULL), got_plt_(NULL), |
+ rel_dyn_(NULL), copy_relocs_(elfcpp::R_MIPS_COPY), got_mod_index_offset_(-1U), |
+ gp_(NULL), stub_(NULL), stub_flag_(false), |
+ processor_specific_flags_(0), mips_stubs_(NULL), text_section_(NULL), |
+ layout_(NULL), symtab_(NULL) |
+ { |
+ add_extension(elfcpp::E_MIPS_ARCH_64R2, elfcpp::E_MIPS_MACH_OCTEON); |
+ add_extension(elfcpp::E_MIPS_ARCH_64, elfcpp::E_MIPS_ARCH_64R2); |
+ add_extension(elfcpp::E_MIPS_ARCH_64, elfcpp::E_MIPS_ARCH_64 |
+ | elfcpp::E_MIPS_MACH_SB1); |
+ add_extension(elfcpp::E_MIPS_ARCH_64, elfcpp::E_MIPS_ARCH_64 |
+ | elfcpp::E_MIPS_MACH_XLR); |
+ add_extension(elfcpp::E_MIPS_ARCH_64, elfcpp::E_MIPS_ARCH_64 |
+ | elfcpp::E_MIPS_MACH_LS3A); |
+ add_extension(elfcpp::E_MIPS_ARCH_5, elfcpp::E_MIPS_ARCH_64); |
+ add_extension(elfcpp::E_MIPS_ARCH_4 | elfcpp::E_MIPS_MACH_5400, |
+ elfcpp::E_MIPS_ARCH_4 | elfcpp::E_MIPS_MACH_5500); |
+ add_extension(elfcpp::E_MIPS_ARCH_4, elfcpp::E_MIPS_ARCH_4 |
+ | elfcpp::E_MIPS_MACH_5400); |
+ add_extension(elfcpp::E_MIPS_ARCH_4, elfcpp::E_MIPS_ARCH_5); |
+ add_extension(elfcpp::E_MIPS_ARCH_4, elfcpp::E_MIPS_ARCH_4 |
+ | elfcpp::E_MIPS_MACH_9000); |
+ add_extension(elfcpp::E_MIPS_ARCH_3 | elfcpp::E_MIPS_MACH_4100, |
+ elfcpp::E_MIPS_ARCH_3 | elfcpp::E_MIPS_MACH_4120); |
+ add_extension(elfcpp::E_MIPS_ARCH_3 | elfcpp::E_MIPS_MACH_4100, |
+ elfcpp::E_MIPS_ARCH_3 | elfcpp::E_MIPS_MACH_4111); |
+ add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3 |
+ | elfcpp::E_MIPS_MACH_LS2F); |
+ add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3 |
+ | elfcpp::E_MIPS_MACH_LS2F); |
+ add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_4); |
+ add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3 |
+ | elfcpp::E_MIPS_MACH_4650); |
+ add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3 |
+ | elfcpp::E_MIPS_MACH_4100); |
+ add_extension(elfcpp::E_MIPS_ARCH_3, elfcpp::E_MIPS_ARCH_3 |
+ | elfcpp::E_MIPS_MACH_4111); |
+ add_extension(elfcpp::E_MIPS_ARCH_32, elfcpp::E_MIPS_ARCH_32R2); |
+ add_extension(elfcpp::E_MIPS_ARCH_2, elfcpp::E_MIPS_ARCH_3); |
+ add_extension(elfcpp::E_MIPS_ARCH_2, elfcpp::E_MIPS_ARCH_32); |
+ add_extension(elfcpp::E_MIPS_ARCH_1, elfcpp::E_MIPS_ARCH_2); |
+ add_extension(elfcpp::E_MIPS_ARCH_1, elfcpp::E_MIPS_ARCH_1 |
+ | elfcpp::E_MIPS_MACH_3900); |
+ } |
+ |
+ // Process the relocations to determine unreferenced sections for |
+ // garbage collection. |
+ void |
+ gc_process_relocs(Symbol_table* symtab, |
+ Layout* layout, |
+ Sized_relobj_file<size, big_endian>* 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<size, big_endian>* 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); |
+ |
+ // Finalize the sections. |
+ void |
+ do_finalize_sections (Layout *, const Input_objects *, Symbol_table *); |
+ |
+ // Fix values that are known only after sections are finalized. |
+ void |
+ do_fix_sections(Layout*, Symbol_table*); |
+ |
+ // Relocate a section. |
+ void |
+ relocate_section(const Relocate_info<size, big_endian>*, |
+ unsigned int sh_type, |
+ const unsigned char* prelocs, |
+ size_t reloc_count, |
+ Output_section* output_section, |
+ bool needs_special_offset_handling, |
+ unsigned char* view, |
+ typename elfcpp::Elf_types<size>::Elf_Addr 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<size, big_endian>* 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*); |
+ |
+ // Relocate a section during a relocatable link. |
+ void |
+ relocate_for_relocatable(const Relocate_info<size, big_endian>*, |
+ unsigned int sh_type, |
+ const unsigned char* prelocs, |
+ size_t reloc_count, |
+ Output_section* output_section, |
+ off_t offset_in_output_section, |
+ const Relocatable_relocs*, |
+ unsigned char* view, |
+ typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
+ section_size_type view_size, |
+ unsigned char* reloc_view, |
+ section_size_type reloc_view_size); |
+ |
+ // Perform target-specific processing in a relocatable link. This is |
+ // only used if we use the relocation strategy RELOC_SPECIAL. |
+ void |
+ relocate_special_relocatable(const Relocate_info<size, big_endian>* relinfo, |
+ unsigned int sh_type, |
+ const unsigned char* preloc_in, |
+ size_t relnum, |
+ Output_section* output_section, |
+ off_t offset_in_output_section, |
+ unsigned char* view, |
+ typename elfcpp::Elf_types<32>::Elf_Addr |
+ view_address, |
+ section_size_type view_size, |
+ unsigned char* preloc_out); |
+ |
+ // Return whether SYM is defined by the ABI. |
+ bool |
+ do_is_defined_by_abi(const Symbol* sym) const |
+ { |
+ return ((strcmp(sym->name(), "__gnu_local_gp") == 0) |
+ || (strcmp(sym->name(), "_gp_disp") == 0) |
+ || (strcmp(sym->name(), "___tls_get_addr") == 0)); |
+ } |
+ |
+ // Return the number of entries in the GOT. |
+ unsigned int |
+ got_entry_count() const |
+ { |
+ if (!this->has_got_section()) |
+ return 0; |
+ return this->got_size() / 4; |
+ } |
+ |
+ // Return the number of entries in the PLT. |
+ unsigned int |
+ plt_entry_count() const |
+ { |
+ if (this->plt_ == NULL) |
+ return 0; |
+ return this->plt_->entry_count(); |
+ } |
+ |
+ // Return the offset of the first non-reserved PLT entry. |
+ unsigned int |
+ first_plt_entry_offset() const |
+ { return Mips_output_data_plt<size, big_endian>::first_plt_entry_offset(); } |
+ |
+ // Return the size of each PLT entry. |
+ unsigned int |
+ plt_entry_size() const |
+ { return Mips_output_data_plt<size, big_endian>::plt_entry_size(); } |
+ |
+ // Get stub section |
+ Mips_output_data_stub<size, big_endian>* |
+ stub() |
+ { return this->stub_; } |
+ |
+ // Add stub symbol to map |
+ void |
+ add_stub_symbol(const char* name, Symbol *gsym) |
+ { this->stub_symbol_[name] = gsym; } |
+ |
+ // Get stub symbol from map |
+ Symbol* |
+ stub_symbol(const char* name) |
+ { return this->stub_symbol_[name]; } |
+ |
+ // Check if symbol is in .plt section |
+ bool |
+ is_plt_symbol(Sized_symbol<size> *sym) |
+ { |
+ if(this->mips_stubs_ == NULL) |
+ return true; |
+ |
+ return this->mips_stubs_->is_plt_symbol(sym); |
+ } |
+ |
+ // Return .MIPS.stubs output section. |
+ Mips_output_data_mips_stubs<size, big_endian> * |
+ mips_stubs() const |
+ { return this->mips_stubs_; } |
+ |
+ // Retrun address of .text section |
+ uint64_t |
+ text_section_address() const |
+ { |
+ if(this->text_section_ == 0) |
+ return 0; |
+ |
+ return this->text_section_->address(); |
+ } |
+ |
+protected: |
+ // Return the value to use for a dynamic symbol which requires special |
+ // treatment. This is how we support equality comparisons of function |
+ // pointers across shared library boundaries, as described in the |
+ // processor specific ABI supplement. |
+ uint64_t |
+ do_dynsym_value(const Symbol* gsym) const; |
+ |
+ // Make an output section. |
+ Output_section* |
+ do_make_output_section (const char *name, elfcpp::Elf_Word type, |
+ elfcpp::Elf_Xword flags) |
+ { |
+ if(type == elfcpp::SHT_MIPS_REGINFO) |
+ return new Mips_output_section_reginfo<size>(name, type, flags); |
+ else |
+ return new Output_section(name, type, flags); |
+ } |
+ |
+ void |
+ do_adjust_elf_header(unsigned char* view, int len) const; |
+ |
+ // MIPS specific dynamic tags. |
+ unsigned int |
+ do_dynamic_tag_value(elfcpp::DT) const; |
+ |
+private: |
+ // The class which scans relocations. |
+ class Scan |
+ { |
+ public: |
+ Scan() |
+ : issued_non_pic_error_(false) |
+ { } |
+ |
+ static inline int |
+ get_reference_flags(unsigned int r_type); |
+ |
+ inline void |
+ local(Symbol_table* symtab, Layout* layout, Target_mips* target, |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int data_shndx, |
+ Output_section* output_section, |
+ const elfcpp::Rel<size, big_endian>& reloc, unsigned int r_type, |
+ const elfcpp::Sym<size, big_endian>& lsym); |
+ |
+ inline void |
+ global(Symbol_table* symtab, Layout* layout, Target_mips* target, |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int data_shndx, |
+ Output_section* output_section, |
+ const elfcpp::Rel<size, big_endian>& reloc, unsigned int r_type, |
+ Symbol* gsym); |
+ |
+ inline bool |
+ local_reloc_may_be_function_pointer(Symbol_table* , Layout* , |
+ Target_mips* , |
+ Sized_relobj_file<size, big_endian>* , |
+ unsigned int , |
+ Output_section* , |
+ const elfcpp::Rel<size, big_endian>& , |
+ unsigned int , |
+ const elfcpp::Sym<size, big_endian>&) |
+ { return false; } |
+ |
+ inline bool |
+ global_reloc_may_be_function_pointer(Symbol_table* , Layout* , |
+ Target_mips* , |
+ Sized_relobj_file<size, big_endian>* , |
+ unsigned int , |
+ Output_section* , |
+ const elfcpp::Rel<size, big_endian>& , |
+ unsigned int , Symbol*) |
+ { return false; } |
+ |
+ private: |
+ static void |
+ unsupported_reloc_local(Sized_relobj_file<size, big_endian>*, |
+ unsigned int r_type); |
+ |
+ static void |
+ unsupported_reloc_global(Sized_relobj_file<size, big_endian>*, |
+ unsigned int r_type, Symbol*); |
+ |
+ static void |
+ generate_tls_call(Symbol_table* symtab, Layout* layout, |
+ Target_mips* target); |
+ |
+ void |
+ check_non_pic(Relobj*, unsigned int r_type); |
+ |
+ static bool |
+ symbol_needs_plt_entry(const Symbol* sym) |
+ { |
+ // An undefined symbol from an executable does not need a PLT entry. |
+ if (sym->is_undefined() && !parameters->options().shared()) |
+ return false; |
+ |
+ return (!parameters->doing_static_link() |
+ && sym->is_func() |
+ && (!sym->is_undef_binding_weak()) |
+ && (sym->is_from_dynobj() |
+ || sym->is_undefined() |
+ || sym->is_preemptible())); |
+ } |
+ |
+ // Whether we have issued an error about a non-PIC compilation. |
+ bool issued_non_pic_error_; |
+ }; |
+ |
+ // The class which implements relocation. |
+ class Relocate |
+ { |
+ public: |
+ // Do a relocation. Return false if the caller should not issue |
+ // any warnings about this relocation. |
+ inline bool |
+ relocate(const Relocate_info<size, big_endian>*, Target_mips*, |
+ Output_section*, size_t relnum, |
+ const elfcpp::Rel<size, big_endian>&, |
+ unsigned int r_type, const Sized_symbol<size>*, |
+ const Symbol_value<size>*, |
+ unsigned char*, |
+ typename elfcpp::Elf_types<size>::Elf_Addr, |
+ section_size_type); |
+ |
+ private: |
+ // Do a TLS relocation. |
+ inline void |
+ relocate_tls(const Relocate_info<size, big_endian>*, |
+ Target_mips* target, |
+ size_t relnum, const elfcpp::Rel<size, big_endian>&, |
+ unsigned int r_type, const Sized_symbol<size>*, |
+ const Symbol_value<size>*, |
+ unsigned char*, |
+ typename elfcpp::Elf_types<size>::Elf_Addr, |
+ section_size_type); |
+ }; |
+ |
+ // 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*); |
+ }; |
+ |
+ // Adjust TLS relocation type based on the options and whether this |
+ // is a local symbol. |
+ static tls::Tls_optimization |
+ optimize_tls_reloc(bool is_final, int r_type); |
+ |
+ // Return whether there is a GOT section. |
+ bool |
+ has_got_section() const |
+ { return this->got_ != NULL; } |
+ |
+ // Get the GOT section, creating it if necessary. |
+ Mips_output_data_got<size, big_endian>* |
+ got_section(Symbol_table*, Layout*); |
+ |
+ Mips_output_data_got<size, big_endian>* |
+ got_section() |
+ { return this->got_; } |
+ |
+ // Return processor specific flags |
+ elfcpp::Elf_Word |
+ processor_specific_flags() const |
+ { return this->processor_specific_flags_; } |
+ |
+ // Set processor specific flags |
+ void |
+ set_processor_specific_flags(elfcpp::Elf_Word psf) |
+ { this->processor_specific_flags_ = psf; } |
+ |
+ // Check if the given ELF header glags describe a 32-bit binary. |
+ bool |
+ mips_32bit_flags(elfcpp::Elf_Word); |
+ |
+ // Check if new architecture extends previous one. |
+ bool |
+ mips_mach_extends(elfcpp::Elf_Word, elfcpp::Elf_Word); |
+ |
+ // Merge processor specific flags. |
+ void |
+ merge_processor_specific_flags(const char*, elfcpp::Elf_Word); |
+ |
+ // Return the size of the GOT section. |
+ section_size_type |
+ got_size() const |
+ { |
+ gold_assert(this->got_ != NULL); |
+ return this->got_->data_size(); |
+ } |
+ |
+ // Create a GOT entry for the TLS module index. |
+ unsigned int |
+ got_mod_index_entry(Symbol_table* symtab, Layout* layout, |
+ Sized_relobj_file<size, big_endian>* object); |
+ |
+ // Create a PLT entry for a global symbol. |
+ void |
+ make_plt_entry(Symbol_table*, Layout*, Symbol*); |
+ |
+ // Create a .MIPS.stubs entry for a global symbol. |
+ void |
+ make_mips_stubs_entry(Symbol_table*, Layout*, Symbol*); |
+ |
+ // Remove symbol that should not have entry. |
+ void |
+ remove_stub_entry(Symbol *sym) |
+ { |
+ if(this->mips_stubs_ != NULL) |
+ this->mips_stubs_->remove(sym); |
+ } |
+ |
+ // Create a STUB entry for a global symbol. |
+ void |
+ make_stub_entry(Layout*); |
+ |
+ void |
+ set_stub_flag() |
+ { this->stub_flag_ = true; } |
+ |
+ bool |
+ stub_flag() const |
+ { return this->stub_flag_; } |
+ |
+ // Get the PLT section. |
+ const Mips_output_data_plt<size, big_endian>* |
+ plt_section() const |
+ { |
+ gold_assert(this->plt_ != NULL); |
+ return this->plt_; |
+ } |
+ |
+ // Get the GOT PLT section. |
+ const Mips_output_data_plt<size, big_endian>* |
+ got_plt_section() const |
+ { |
+ gold_assert(this->got_plt_ != NULL); |
+ return this->got_plt_; |
+ } |
+ |
+ // Get _gp. |
+ Sized_symbol<size>* |
+ gp() |
+ { return this->gp_; } |
+ |
+ // Get value of _gp. |
+ const typename elfcpp::Elf_types<size>::Elf_Addr |
+ gp_address() const |
+ { return this->gp_->value(); } |
+ |
+ // Copy a relocation against a global symbol. |
+ void |
+ copy_reloc(Symbol_table* symtab, Layout* layout, |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int shndx, Output_section* output_section, |
+ Symbol* sym, const elfcpp::Rel<size, big_endian>& reloc) |
+ { |
+ this->copy_relocs_.copy_reloc(symtab, layout, |
+ symtab->get_sized_symbol<size>(sym), |
+ object, shndx, output_section, |
+ reloc, this->rel_dyn_section(layout)); |
+ } |
+ |
+ // Calculate value of _gp. |
+ void |
+ set_gp(Layout*, Symbol_table*); |
+ |
+ // Get the dynamic reloc section, creating it if necessary. |
+ Reloc_section* |
+ rel_dyn_section(Layout*); |
+ |
+ // Add value to MIPS extenstions. |
+ void |
+ add_extension(unsigned long base, unsigned long extension) |
+ { |
+ std::pair<unsigned long, unsigned long> ext(base, extension); |
+ this->extensions_.push_back(ext); |
+ } |
+ |
+ // 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 = 0, // GOT entry for a regular symbol |
+ GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset |
+ GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair |
+ }; |
+ |
+ // Information about this specific target which we pass to the |
+ // general Target structure. |
+ static Target::Target_info mips_info; |
+ // The GOT section. |
+ Mips_output_data_got<size, big_endian>* got_; |
+ // The PLT section. |
+ Mips_output_data_plt<size, big_endian>* plt_; |
+ // The GOT PLT section. |
+ Output_data_space* got_plt_; |
+ // The dynamic reloc section. |
+ Reloc_section* rel_dyn_; |
+ // Relocs saved to avoid a COPY reloc. |
+ Copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_; |
+ // Offset of the GOT entry for the TLS module index. |
+ unsigned int got_mod_index_offset_; |
+ // Value of the _gp |
+ Sized_symbol<size>* gp_; |
+ // The stub section. |
+ Mips_output_data_stub<size, big_endian>* stub_; |
+ // Stub flag |
+ bool stub_flag_; |
+ // Map of stub symbols |
+ Stub_symbol_info stub_symbol_; |
+ // processor-specific flags in ELF file header. |
+ elfcpp::Elf_Word processor_specific_flags_; |
+ // Architecture extensions. |
+ std::vector<std::pair<unsigned long, unsigned long> > extensions_; |
+ // .MIPS.stubs |
+ Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_; |
+ // .text section |
+ Output_section *text_section_; |
+ // Layout |
+ Layout* layout_; |
+ // Symbol table |
+ Symbol_table* symtab_; |
+}; |
+ |
+// Helper structure for R_MIPS_HI/LO16 and R_MIPS16_HI/LO16 relocations |
+ |
+template<int size, bool big_endian> |
+struct hi16 |
+{ |
+ unsigned char* view; |
+ typename elfcpp::Swap<32, big_endian>::Valtype symval; |
+}; |
+ |
+template<int size, bool big_endian> |
+class Mips_relocate_functions : public Relocate_functions<size, big_endian> |
+{ |
+public: |
+ typedef enum |
+ { |
+ STATUS_OKAY, // No error during relocation. |
+ STATUS_OVERFLOW, // Relocation overflow. |
+ STATUS_BAD_RELOC // Relocation cannot be applied. |
+ } Status; |
+ |
+private: |
+ typedef Relocate_functions<size, big_endian> Base; |
+ typedef Mips_relocate_functions<size, big_endian> This; |
+ |
+public: |
+ // R_MIPS_16 |
+ static inline typename This::Status |
+ rel16(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); |
+ Reltype addend = Bits<16>::sign_extend32(val); |
+ Reltype x = psymval->value(object, addend); |
+ val = Bits<16>::bit_select32(val, x, 0x0000ffffU); |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ return (Bits<16>::has_signed_unsigned_overflow32(x) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+ |
+ // R_MIPS_32 |
+ static inline typename This::Status |
+ rel32(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); |
+ Valtype x = psymval->value(object, addend); |
+ elfcpp::Swap<32, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_JALR |
+ static inline typename This::Status |
+ reljalr(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ typename elfcpp::Elf_types<size>::Elf_Addr address) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype instr = elfcpp::Swap<32, big_endian>::readval(wv); |
+ Valtype x = psymval->value(object, 0); |
+ Valtype offset = x - address - 4; |
+ |
+ if(!Bits<17>::has_signed_unsigned_overflow32(offset)) |
+ { |
+ if(instr == 0x0320f809) // jalr t9 |
+ { |
+ // bal addr |
+ Valtype jalr = 0x04110000 | ((offset >> 2) & 0x0000ffffU); |
+ elfcpp::Swap<32, big_endian>::writeval(wv, jalr); |
+ } |
+ else if(instr == 0x03200008) //jr t9 |
+ { |
+ // b addr |
+ Valtype jalr = 0x10000000 | ((offset >> 2) & 0x0000ffffU); |
+ elfcpp::Swap<32, big_endian>::writeval(wv, jalr); |
+ } |
+ } |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_REL32, R_MIPS_PC32 |
+ static inline typename This::Status |
+ relrel32(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ typename elfcpp::Elf_types<size>::Elf_Addr address) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); |
+ Valtype x = (psymval->value(object, addend)) - address; |
+ elfcpp::Swap<32, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_26 |
+ static inline typename This::Status |
+ rel26(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ typename elfcpp::Elf_types<size>::Elf_Addr address, |
+ bool local) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<32, big_endian>::readval(wv); |
+ Valtype addend; |
+ |
+ if(local) |
+ addend = ((val & 0x03ffffffU) | ((address + 4) & 0xf0000000U)); |
+ else |
+ addend = Bits<26>::sign_extend32(val & 0x03ffffffU); |
+ |
+ Valtype x = psymval->value(object, addend << 2); |
+ val = Bits<32>::bit_select32(val, x >> 2, 0x03ffffffU); |
+ elfcpp::Swap<32, big_endian>::writeval(wv, val); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_SHIFT5 |
+ static inline typename This::Status |
+ relsh5(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); |
+ Reltype addend = Bits<11>::sign_extend32(val & 0x000007c0U); |
+ Reltype x = psymval->value(object, addend); |
+ val = Bits<11>::bit_select32(val, x, 0x000007c0U); |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ return ((Bits<11>::has_signed_unsigned_overflow32(x) || ((x & 0x3fU) != 0)) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+ |
+ // R_MIPS_SHIFT6 |
+ static inline typename This::Status |
+ relsh6(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); |
+ Reltype addend = Bits<11>::sign_extend32(val & 0x000007c0U) |
+ || ((val & 0x00004U) << 2); |
+ Reltype x = psymval->value(object, addend); |
+ x = (x & ~0x00000020U) | ((x & 0x00000020U) >> 2); |
+ val = Bits<16>::bit_select32(val, x, 0x000007c4U); |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ return ((Bits<11>::has_signed_unsigned_overflow32(x) || ((x & 0x37U) != 0)) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+ |
+ // R_MIPS_SUB |
+ static inline typename This::Status |
+ relsub(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<64, big_endian>::readval(wv); |
+ Valtype x = (psymval->value(object, -addend)); |
+ elfcpp::Swap<64, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_HIGHER |
+ static inline typename This::Status |
+ relhigher(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<64, big_endian>::readval(wv); |
+ Valtype x = (((Valtype)(psymval->value(object, addend) + 0x80008000) >> 32) |
+ & 0xffffU); |
+ elfcpp::Swap<64, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_HIGHEST |
+ static inline typename This::Status |
+ relhighest(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<64, big_endian>::readval(wv); |
+ Valtype x = (((Valtype)(psymval->value(object, addend) + 0x800080008000) |
+ >> 48) & 0xffffU); |
+ elfcpp::Swap<64, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_SCN_DISP |
+ static inline typename This::Status |
+ relscndisp(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ typename elfcpp::Elf_types<size>::Elf_Off offset) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); |
+ Valtype x = (psymval->value(object, addend)) - offset; |
+ elfcpp::Swap<32, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_PC16, R_MIPS_GNU_REL16_S2 |
+ static inline typename This::Status |
+ relpc16(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ typename elfcpp::Elf_types<size>::Elf_Addr address) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); |
+ Reltype addend = Bits<16>::sign_extend32(val); |
+ Reltype x = psymval->value(object, addend) - address; |
+ val = Bits<16>::bit_select32(val, x >> 2, 0x0000ffffU); |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ return (Bits<18>::has_signed_unsigned_overflow32(x) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+ |
+ // R_MIPS_HI16, R_MIPS16_HI16, R_MIPS_GOT16, R_MIPS16_GOT16 |
+ static inline typename This::Status |
+ relhi16(unsigned char* view, |
+ typename elfcpp::Swap<32, big_endian>::Valtype symval) |
+ { |
+ struct hi16<size, big_endian>* _hi16 = new hi16<size, big_endian>; |
+ |
+ if(!_hi16) |
+ return This::STATUS_OVERFLOW; |
+ |
+ _hi16->view = view; |
+ _hi16->symval = symval; |
+ |
+ This::hi16_list.push_back(_hi16); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_LO16, R_MIPS16_LO16 |
+ static inline typename This::Status |
+ rello16(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wvlo = reinterpret_cast<Valtype*>(view), *wv; |
+ Valtype vallo = elfcpp::Swap<16, big_endian>::readval(wvlo), val; |
+ Reltype addendlo = Bits<16>::sign_extend32(vallo), addend; |
+ Reltype x, xlo = psymval->value(object, addendlo); |
+ vallo = Bits<16>::bit_select32(vallo, xlo, 0x0000ffffU); |
+ |
+ while(hi16_list.size()) |
+ { |
+ struct hi16<size, big_endian>* _hi16 = hi16_list.back(); |
+ wv = reinterpret_cast<Valtype*>(_hi16->view); |
+ addend = elfcpp::Swap<16, big_endian>::readval(wv); |
+ |
+ x = _hi16->symval + addendlo + (addend << 16); |
+ x += 0x00008000U; |
+ x >>= 16; |
+ |
+ val = Bits<16>::bit_select32(val, x, 0x0000ffffU); |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ |
+ free(_hi16); |
+ This::hi16_list.pop_back(); |
+ }; |
+ |
+ elfcpp::Swap<16, big_endian>::writeval(wvlo, vallo); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ //R_MIPS_64 |
+ static inline typename This::Status |
+ rel64(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<64, big_endian>::readval(wv); |
+ Valtype x = psymval->value(object, addend); |
+ elfcpp::Swap<64, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ static typename std::vector<struct hi16<size, big_endian> *> hi16_list; |
+ |
+ // R_MIPS_GOT_DISP, R_MIPS_GOT_PAGE, |
+ // R_MIPS_TLS_GOTTPREL, R_MIPS_TLS_GD, R_MIPS_TLS_LDM |
+ static inline typename This::Status |
+ relgotofst(unsigned char* view, |
+ typename elfcpp::Swap<32, big_endian>::Valtype got_offset) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Reltype x = got_offset; |
+ elfcpp::Swap<16, big_endian>::writeval(wv, x); |
+ return (Bits<16>::has_signed_unsigned_overflow32(x) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+ |
+ // R_MIPS_GOT16, R_MIPS16_GOT16 |
+ static inline typename This::Status |
+ relgot16(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ const typename elfcpp::Swap<size, big_endian>::Valtype _gp, |
+ unsigned char* got_view, |
+ const typename elfcpp::Swap<size, big_endian>::Valtype got_address, |
+ const typename elfcpp::Swap<size, big_endian>::Valtype got_offset, |
+ const Sized_symbol<size>* gsym) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); |
+ Reltype x = got_address - _gp; |
+ |
+ if(gsym != NULL) |
+ // TODO AS: Constant GOT_TYPE_STANDARD should be used. |
+ x += gsym->got_offset(0); |
+ else |
+ { |
+ Valtype addend = elfcpp::Swap<16, big_endian>::readval(wv); |
+ |
+ // got_view points to beggining of location, data is written |
+ // in the second half |
+ relhi16(got_view + 2, psymval->value(object, addend << 16)); |
+ x = got_address + got_offset - _gp; |
+ } |
+ |
+ val = Bits<16>::bit_select32(val, x, 0x0000ffffU); |
+ |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ |
+ return (Bits<16>::has_signed_unsigned_overflow32(x) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+ |
+ // R_MIPS_GPREL16, R_MIPS16_GPREL and R_MIPS_LITERAL |
+ static inline typename This::Status |
+ relgprel(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ const typename elfcpp::Swap<size, big_endian>::Valtype _gp) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); |
+ Reltype addend = Bits<16>::sign_extend32(val); |
+ Reltype x = psymval->value(object, addend) - _gp; |
+ val = Bits<16>::bit_select32(val, x, 0x0000ffffU); |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ return (Bits<16>::has_signed_unsigned_overflow32(x) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+ |
+ // R_MIPS_GPREL32 |
+ static inline typename This::Status |
+ relgprel32(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ const typename elfcpp::Swap<size, big_endian>::Valtype gp) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ size_t sec_num; |
+ for(sec_num = 0; sec_num < object->shnum(); sec_num++) |
+ if(((Object*)object)->section_type(sec_num) == elfcpp::SHT_MIPS_REGINFO) |
+ break; |
+ |
+ gold::section_size_type section_size = 24; |
+ const unsigned char* reginfo = ((Object*)object)->section_contents(sec_num, |
+ §ion_size, false) + 20; |
+ const Valtype* ri = reinterpret_cast<const Valtype*>(reginfo); |
+ Valtype gp0 = elfcpp::Swap<32, big_endian>::readval(ri); |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); |
+ Valtype x = (psymval->value(object, addend)) + gp0 - gp; |
+ elfcpp::Swap<32, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_GLOB_DAT |
+ static inline typename This::Status |
+ relglob(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ elfcpp::Swap<32, big_endian>::writeval(wv, psymval->value(object, 0)); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_TLS_TPREL32, R_MIPS_TLS_DTPREL32 |
+ static inline typename This::Status |
+ tlsrel32(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ const typename elfcpp::Swap<32, big_endian>::Valtype offset) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv); |
+ Valtype x = psymval->value(object, addend) - offset; |
+ elfcpp::Swap<32, big_endian>::writeval(wv, x); |
+ return This::STATUS_OKAY; |
+ } |
+ |
+ // R_MIPS_TLS_TPREL_LO16, R_MIPS_TLS_DTPREL_LO16 |
+ static inline typename This::Status |
+ tlsrello16(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ const typename elfcpp::Swap<32, big_endian>::Valtype offset) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); |
+ Reltype addend = Bits<16>::sign_extend32(val); |
+ Reltype x = psymval->value(object, addend) - offset; |
+ val = Bits<16>::bit_select32(val, x, 0x0000ffffU); |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ return (Bits<16>::has_signed_unsigned_overflow32(x) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+ |
+ // R_MIPS_TLS_TPREL_HI16, R_MIPS_TLS_DTPREL_HI16 |
+ static inline typename This::Status |
+ tlsrelhi16(unsigned char* view, |
+ const Sized_relobj_file<size, big_endian>* object, |
+ const Symbol_value<size>* psymval, |
+ const typename elfcpp::Swap<32, big_endian>::Valtype offset) |
+ { |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype val = elfcpp::Swap<16, big_endian>::readval(wv); |
+ Reltype addend = Bits<16>::sign_extend32(val); |
+ Reltype x = ((psymval->value(object, addend) - offset) + 0x8000U) >> 16; |
+ val = Bits<16>::bit_select32(val, x, 0x0000ffffU); |
+ elfcpp::Swap<16, big_endian>::writeval(wv, val); |
+ return (Bits<16>::has_signed_unsigned_overflow32(x) |
+ ? This::STATUS_OVERFLOW |
+ : This::STATUS_OKAY); |
+ } |
+}; |
+ |
+template<int size> |
+void |
+Mips_output_section_reginfo<size>::set_final_data_size() |
+{ |
+ // Set data size. |
+ this->set_data_size(24); |
+} |
+ |
+template<int size> |
+void |
+Mips_output_section_reginfo<size>::do_write(Output_file* of) |
+{ |
+ // Set output data |
+ for (Input_section_list::const_iterator p = this->input_sections().begin(); |
+ p != this->input_sections().end(); |
+ ++p) |
+ { |
+ unsigned int shndx = p->shndx(); |
+ Relobj* relobj = p->relobj(); |
+ |
+ section_size_type section_size; |
+ const unsigned char* section_contents = |
+ relobj->section_contents(shndx, §ion_size, false); |
+ |
+ for(size_t i = 0; i < 20; i++) |
+ this->data_[i] |= section_contents[i]; |
+ } |
+ |
+ unsigned long gp_val = 0; |
+ if(this->gp_ != NULL) |
+ gp_val = this->gp_->value(); |
+ |
+ this->data_[20]=gp_val & 0xff; |
+ this->data_[21]=(gp_val >> 8) & 0xff; |
+ this->data_[22]=(gp_val >> 16) & 0xff; |
+ this->data_[23]=(gp_val >> 24) & 0xff; |
+ |
+ off_t offset = this->offset(); |
+ off_t data_size = this->data_size(); |
+ |
+ unsigned char* view = of->get_output_view(offset, data_size); |
+ memcpy(view, this->data_, data_size); |
+ of->write_output_view(offset, data_size, view); |
+} |
+ |
+// Return the value to use for a dynamic symbol which requires special |
+// treatment. This is how we support equality comparisons of function |
+// pointers across shared library boundaries, as described in the |
+// processor specific ABI supplement. |
+ |
+template<int size, bool big_endian> |
+uint64_t |
+Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const |
+{ |
+ gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); |
+ |
+ if(gsym->plt_offset() == 0) |
+ return 0; |
+ |
+ if((this->mips_stubs() == NULL) || |
+ (this->mips_stubs()->is_plt_symbol((Sized_symbol<size> *)gsym))) |
+ return this->plt_section()->address() + gsym->plt_offset(); |
+ else |
+ return this->mips_stubs()->address() + |
+ gsym->plt_offset() * (this->mips_stubs()->big_dyn_symtab() ? 20 : 16); |
+} |
+ |
+template<int size, bool big_endian> |
+typename std::vector<struct hi16<size, big_endian> *> |
+ Mips_relocate_functions<size, big_endian>::hi16_list; |
+ |
+// Get the dynamic reloc section, creating it if necessary. |
+ |
+template<int size, bool big_endian> |
+typename Target_mips<size, big_endian>::Reloc_section* |
+Target_mips<size, big_endian>::rel_dyn_section(Layout* layout) |
+{ |
+ if (this->rel_dyn_ == NULL) |
+ { |
+ gold_assert(layout != NULL); |
+ this->rel_dyn_ = new Reloc_section(parameters->options().combreloc()); |
+ layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL, |
+ elfcpp::SHF_ALLOC, this->rel_dyn_, |
+ ORDER_DYNAMIC_RELOCS, false); |
+ |
+ // The first two entries are reserved. |
+ this->rel_dyn_->add_absolute_null(elfcpp::R_MIPS_NONE, this->rel_dyn_, 0); |
+ } |
+ return this->rel_dyn_; |
+} |
+ |
+// Get the GOT section, creating it if necessary. |
+ |
+template<int size, bool big_endian> |
+Mips_output_data_got<size, big_endian>* |
+Target_mips<size, big_endian>::got_section(Symbol_table* symtab, |
+ Layout* layout) |
+{ |
+ if (this->got_ == NULL) |
+ { |
+ gold_assert(symtab != NULL && layout != NULL); |
+ |
+ this->got_ = new Mips_output_data_got<size, big_endian>(symtab, layout); |
+ |
+ this->got_plt_ = new Output_data_space(4, "** GOT PLT"); |
+ |
+ layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, |
+ elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE | |
+ elfcpp::SHF_MIPS_GPREL, |
+ this->got_, ORDER_DATA, false); |
+ |
+ // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section. |
+ symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, |
+ Symbol_table::PREDEFINED, |
+ this->got_, |
+ 0, 0, elfcpp::STT_OBJECT, |
+ elfcpp::STB_GLOBAL, |
+ elfcpp::STV_DEFAULT, 0, |
+ false, false); |
+ } |
+ |
+ // Set _gp value, too. |
+ this->set_gp(layout, symtab); |
+ |
+ return this->got_; |
+} |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_got<size, big_endian>::add_tls_gd_with_static_reloc( |
+ unsigned int got_type, |
+ Symbol* gsym) |
+{ |
+ if (gsym->has_got_offset(got_type)) |
+ return; |
+ |
+ // We are doing a static link. Just mark it as belong to module 1, |
+ // the executable. |
+ unsigned int got_offset = this->add_constant(1); |
+ gsym->set_got_offset(got_type, got_offset); |
+ got_offset = this->add_constant(0); |
+ this->static_relocs_.push_back(Static_reloc(got_offset, |
+ size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 : elfcpp::R_MIPS_TLS_DTPREL64, |
+ gsym)); |
+} |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_got<size, big_endian>::add_tls_gd_local_reloc( |
+ unsigned int got_type, |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int index) |
+{ |
+ if (object->local_has_got_offset(index, got_type)) |
+ return; |
+ |
+ unsigned int got_offset = this->add_constant(1); |
+ object->set_local_got_offset(index, got_type, got_offset); |
+ got_offset = this->add_constant(0); |
+ this->static_relocs_.push_back(Static_reloc(got_offset, |
+ size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 : elfcpp::R_MIPS_TLS_DTPREL64, |
+ object, index)); |
+} |
+ |
+// Calculate value of gp_. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab) |
+{ |
+ if(gp_ == NULL) |
+ { |
+ Sized_symbol<size>* sym = |
+ static_cast<Sized_symbol<size>*>(symtab->lookup("_gp")); |
+ |
+ if((sym == NULL) || (sym->source() != Symbol::IS_CONSTANT)) |
+ { |
+ Output_data *got_section = layout->find_output_section(".got"); |
+ |
+ gold_assert(got_section != NULL); |
+ |
+ if(sym == NULL) |
+ sym =static_cast<Sized_symbol<size>*>(symtab->define_in_output_data( |
+ "_gp", NULL, Symbol_table::PREDEFINED, |
+ got_section, 0x7ff0, 0, |
+ elfcpp::STT_OBJECT, |
+ elfcpp::STB_GLOBAL, |
+ elfcpp::STV_DEFAULT, |
+ 0, false, false)); |
+ else |
+ { |
+ sym->init_output_data("_gp", NULL, got_section, 0x7ff0, 0, |
+ elfcpp::STT_OBJECT, |
+ elfcpp::STB_GLOBAL, |
+ elfcpp::STV_DEFAULT, 0, |
+ false, false); |
+ } |
+ gold_assert(sym != NULL); |
+ } |
+ |
+ this->gp_ = sym; |
+ } |
+} |
+ |
+// A class to handle the STUB data. |
+ |
+template<int size, bool big_endian> |
+class Mips_output_data_stub : public Output_section_data |
+{ |
+ public: |
+ typedef std::vector<Symbol *> symbol_list; |
+ Mips_output_data_stub(); |
+ |
+ // Add an entry to the STUB. |
+ void add_entry(Symbol_table* symtab, |
+ Target_mips<size, big_endian>* target, |
+ Symbol* gsym); |
+ |
+ // Return the number of STUB entries. |
+ unsigned int |
+ entry_count() const |
+ { return this->count_; } |
+ |
+ // Add stub symbol |
+ void |
+ add_symbol(Symbol *sym) |
+ { symbols.push_back(sym); } |
+ |
+ // Get symbol on position n |
+ Symbol * |
+ symbol(unsigned int n) |
+ { return symbols[n]; } |
+ |
+ protected: |
+ void do_adjust_output_section(Output_section* os); |
+ |
+ private: |
+ // Template for subsequent STUB entries. |
+ static const uint32_t stub_entry[4]; |
+ |
+ // Set the final size. |
+ void |
+ set_final_data_size() |
+ { |
+ unsigned int full_count = this->count_ * 4; |
+ |
+ this->set_data_size(full_count * 4); |
+ } |
+ |
+ // Write out the STUB data. |
+ void |
+ do_write(Output_file*); |
+ |
+ // The number of STUB entries. |
+ unsigned int count_; |
+ |
+ // List of symbols used in stubs |
+ symbol_list symbols; |
+}; |
+ |
+// A class to handle the PLT data. |
+ |
+template<int size, bool big_endian> |
+class Mips_output_data_plt : public Output_section_data |
+{ |
+ public: |
+ typedef Output_data_reloc<elfcpp::SHT_REL, true, |
+ size, big_endian> Reloc_section; |
+ |
+ Mips_output_data_plt(Layout*, Output_data_space*); |
+ |
+ // Add an entry to the PLT. |
+ void add_entry(Symbol* gsym); |
+ |
+ // Return the .rel.plt section data. |
+ const Reloc_section* rel_plt() const |
+ { return this->rel_; } |
+ |
+ // Return the number of PLT entries. |
+ unsigned int |
+ entry_count() const |
+ { return this->count_; } |
+ |
+ // Return the offset of the first non-reserved PLT entry. |
+ static unsigned int |
+ first_plt_entry_offset() |
+ { return sizeof(first_plt_entry[0]); } |
+ |
+ // Return the size of a PLT entry. |
+ static unsigned int |
+ plt_entry_size() |
+ { return sizeof(plt_entry); } |
+ |
+ protected: |
+ void do_adjust_output_section(Output_section* os); |
+ |
+ // Write to a map file. |
+ void |
+ do_print_to_mapfile(Mapfile* mapfile) const |
+ { mapfile->print_output_data(this, _(".plt")); } |
+ |
+ private: |
+ // Template for the first PLT entry. |
+ static const uint32_t first_plt_entry[3][8]; |
+ |
+ // Template for subsequent PLT entries. |
+ static const uint32_t plt_entry[4]; |
+ |
+ // Set the final size. |
+ void |
+ set_final_data_size() |
+ { |
+ unsigned int full_count = this->count_ * 4 + 8; |
+ |
+ this->set_data_size(full_count * 4); |
+ } |
+ |
+ // MIPS abi types |
+ enum Abi_type |
+ { |
+ MIPS_ABI_ERROR = -1, // Error when getting ABI type |
+ MIPS_O32 = 0, // Mips O32 ABI |
+ MIPS_N32 = 1, // Mips N32 ABI |
+ MIPS_N64 = 2, // Mips N64 ABI |
+ MIPS_O64 = 3, // Mips O64 ABI |
+ MIPS_EABI32 = 4, // Mips 32 EABI |
+ MIPS_EABI64 = 5, // Mips 64 EABI |
+ }; |
+ |
+ // Get MIPS abi |
+ Abi_type |
+ abi(elfcpp::Elf_Word /*flags*/) const |
+ { |
+#if 0 |
+ // MIPS ABI2 |
+ if((flags & elfcpp::EF_MIPS_ABI2) != 0) |
+ { |
+ switch(flags & elfcpp::EF_MIPS_ABI) |
+ { |
+ case elfcpp::E_MIPS_ABI_O32: |
+ return MIPS_N32; |
+ case elfcpp::E_MIPS_ABI_O64: |
+ return MIPS_N64; |
+ case elfcpp::E_MIPS_ABI_EABI32: |
+ return MIPS_EABI32; // TODO AS: Check if this is correct!!!! |
+ case elfcpp::E_MIPS_ABI_EABI64: |
+ return MIPS_EABI64; // TODO AS: Check if this is correct!!!! |
+ default: |
+ return MIPS_ABI_ERROR; |
+ } |
+ } |
+ else |
+ { |
+ switch(flags & elfcpp::EF_MIPS_ABI) |
+ { |
+ case elfcpp::E_MIPS_ABI_O32: |
+ return MIPS_O32; |
+ case elfcpp::E_MIPS_ABI_O64: |
+ return MIPS_O64; |
+ case elfcpp::E_MIPS_ABI_EABI32: |
+ return MIPS_EABI32; // TODO AS: Check if this is correct!!!! |
+ case elfcpp:: E_MIPS_ABI_EABI64: |
+ return MIPS_EABI64; // TODO AS: Check if this is correct!!!! |
+ default: |
+ return MIPS_ABI_ERROR; |
+ } |
+ } |
+#endif |
+ // TODO AS: For now only support O32 ABI |
+ return MIPS_O32; |
+ } |
+ |
+ // Write out the PLT data. |
+ void |
+ do_write(Output_file*); |
+ |
+ // The reloc section. |
+ Reloc_section* rel_; |
+ // The .got.plt section. |
+ Output_data_space* got_plt_; |
+ // The number of PLT entries. |
+ unsigned int count_; |
+}; |
+ |
+// A class to handle the .MIPS.stubs data. |
+ |
+template<int size, bool big_endian> |
+class Mips_output_data_mips_stubs : public Output_section_data |
+{ |
+ public: |
+ Mips_output_data_mips_stubs(Layout* layout, |
+ Mips_output_data_got<size, big_endian>* got, Sized_symbol<size>* gp) |
+ : Output_section_data(size == 32 ? 4 : 8), count_(1), got_(got), gp_(gp), |
+ layout_(layout), big_dyn_symtab_(false) |
+ { }; |
+ |
+ // Add an entry to the .MIPS.stubs. |
+ void add_entry(Symbol* gsym); |
+ |
+ // Return the number of .MIPS.stubs entries. |
+ unsigned int |
+ entry_count() const |
+ { return this->count_; } |
+ |
+ // Return if symbol is in .plt |
+ bool |
+ is_plt_symbol(Sized_symbol<size> *sym) |
+ { |
+ if(this->symbols_.size() == 0) |
+ return true; |
+ |
+ return (this->symbols_.count(sym) == 0); |
+ } |
+ |
+ // Remove symbol that should not have entry. |
+ void |
+ remove(Symbol *sym) |
+ { |
+ if(this->symbols_.size() == 0) |
+ return; |
+ |
+ if(this->symbols_.count(sym) == 0) |
+ return; |
+ |
+ this->symbols_.erase(sym); |
+ --this->count_; |
+ } |
+ |
+ // Set if dynamic symbole table has more then 0x10000 entries. |
+ void |
+ set_big_dyn_symtab(bool big_dyn_symtab) |
+ { big_dyn_symtab_ = big_dyn_symtab; } |
+ |
+ // If dynamic symbole table has more then 0x10000 entries. |
+ bool |
+ big_dyn_symtab() |
+ { return this->big_dyn_symtab_; } |
+ |
+ // Fix values of used symbols in symbol table. |
+ void |
+ fix_symbol_values(); |
+ |
+ protected: |
+ void do_adjust_output_section(Output_section* os); |
+ |
+ // Write to a map file. |
+ void |
+ do_print_to_mapfile(Mapfile* mapfile) const |
+ { mapfile->print_output_data(this, _(".MIPS.stubs")); } |
+ |
+ private: |
+ // Template for the .MIPS.stubs instructions. |
+ // lw t9, .got offset from _gp |
+ unsigned long |
+ stub_lw() |
+ { return 0x8f990000 | ((this->got_->address() - this->gp_->value()) & 0xffff); } |
+ |
+ // addu t7,ra |
+ unsigned long |
+ stub_move() |
+ { return 0x03e07821; } |
+ |
+ // lui t8,val |
+ unsigned long |
+ stub_lui(int val) |
+ { return (0x3c180000 + (val & 0xffff)); } |
+ |
+ //jalr t9,ra |
+ unsigned long |
+ stub_jalr() |
+ { return 0x0320f809; } |
+ |
+ // ori t8,t8,val |
+ unsigned long |
+ stub_ori(int val) |
+ { return (0x37180000 + (val & 0xffff)); } |
+ |
+ // ori t8,zero,val unsigned |
+ unsigned long |
+ stub_li16u(int val) |
+ { return (0x34180000 + (val & 0xffff)); } |
+ |
+ // addiu t8,zero,val sign extended |
+ unsigned long |
+ stub_li16s(int val) |
+ { return (0x24180000 + (val & 0xffff)); } |
+ |
+ // Set the final size. |
+ void |
+ set_final_data_size() |
+ { |
+ unsigned int full_count = this->count_ * (this->big_dyn_symtab_ ? 5 : 4); |
+ |
+ this->set_data_size(full_count * 4); |
+ } |
+ |
+ // Write out the .MIPS.stubs data. |
+ void |
+ do_write(Output_file*); |
+ |
+ // The number of .MIPS.stubs entries. |
+ unsigned int count_; |
+ // .got section |
+ Mips_output_data_got<size, big_endian>* got_; |
+ // _gp symbol |
+ Sized_symbol<size>* gp_; |
+ // Layout |
+ Layout* layout_; |
+ // Set if dynamic symbol table has more then 0x10000 entries. |
+ bool big_dyn_symtab_; |
+ // .MIPS.stubs symbols |
+ std::set<Symbol *> symbols_; |
+}; |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_mips_stubs<size, big_endian>::do_adjust_output_section( |
+ Output_section* os) |
+{ os->set_entsize(0); } |
+ |
+// Add an entry to the .MIPS.stubs. |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_mips_stubs<size, big_endian>::add_entry(Symbol* sym) |
+{ |
+ gold_assert(!sym->has_plt_offset()); |
+ sym->set_plt_offset(0*this->count_); |
+ |
+ this->symbols_.insert(sym); |
+ |
+ sym->set_needs_dynsym_entry(); |
+ ++this->count_; |
+} |
+ |
+// Fix values of used symbols in symbol table. |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_mips_stubs<size, big_endian>::fix_symbol_values() |
+{ |
+ int i = 0; |
+ for (std::set<Symbol *>::const_iterator p = this->symbols_.begin(); |
+ p != this->symbols_.end(); ++p, i++) |
+ { |
+ Sized_symbol<size> *sym = static_cast<Sized_symbol<size>*> (*p); |
+ elfcpp::Elf_Xword value = this->address() + i |
+ * (this->big_dyn_symtab_ ? 20 : 16); |
+ sym->set_value(value); |
+ } |
+} |
+ |
+// Write out the .MIPS.stubs. This uses the hand-coded instructions and |
+// adjusts them as needed. |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of) |
+{ |
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; |
+ |
+ // Read processor-specific flags in ELF file header. |
+ const unsigned char* pehdr = of->get_output_view(elfcpp::file_header_offset, |
+ elfcpp::Elf_sizes<size>::ehdr_size); |
+ elfcpp::Ehdr<size, big_endian> ehdr(pehdr); |
+ |
+ const off_t offset = this->offset(); |
+ const section_size_type oview_size = |
+ convert_to_section_size_type(this->data_size()) + |
+ (this->big_dyn_symtab_ * (this->symbols_.size() + 1) * 4); |
+ unsigned char* const oview = of->get_output_view(offset, oview_size); |
+ |
+ unsigned char* pov = oview; |
+ |
+ for (std::set<Symbol *>::const_iterator p = this->symbols_.begin(); |
+ p != this->symbols_.end(); ++p) |
+ { |
+ // Fill the stub. |
+ uint32_t stub_insn0 = stub_lw(); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn0); |
+ pov += 4; |
+ uint32_t stub_insn1 = stub_move(); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn1); |
+ pov += 4; |
+ if(this->big_dyn_symtab_) |
+ { |
+ uint32_t stub_insn2 = stub_lui(((*p)->dynsym_index() >> 16) & 0x7fff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn2); |
+ pov += 4; |
+ } |
+ uint32_t stub_insn3 = stub_jalr(); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn3); |
+ pov += 4; |
+ if(this->big_dyn_symtab_) |
+ { |
+ uint32_t stub_insn4 = stub_ori((*p)->dynsym_index() & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn4); |
+ } |
+ else if ((*p)->dynsym_index() & ~0x7fff) |
+ { |
+ uint32_t stub_insn5 = stub_li16u((*p)->dynsym_index() & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn5); |
+ } |
+ else |
+ { |
+ uint32_t stub_insn6 = stub_li16s((*p)->dynsym_index()); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn6); |
+ } |
+ pov += 4; |
+ } |
+ |
+ elfcpp::Swap<32, big_endian>::writeval(pov, 0UL); |
+ pov += 4; |
+ elfcpp::Swap<32, big_endian>::writeval(pov, 0UL); |
+ pov += 4; |
+ elfcpp::Swap<32, big_endian>::writeval(pov, 0UL); |
+ pov += 4; |
+ elfcpp::Swap<32, big_endian>::writeval(pov, 0UL); |
+ pov += 4; |
+ |
+ if(this->big_dyn_symtab_) |
+ { |
+ elfcpp::Swap<32, big_endian>::writeval(pov, 0UL); |
+ pov += 4; |
+ } |
+ |
+ gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); |
+ |
+ of->write_output_view(offset, oview_size, oview); |
+} |
+ |
+// Create the PLT section. The ordinary .got section is an argument, |
+// since we need to refer to the start. |
+ |
+template<int size, bool big_endian> |
+Mips_output_data_plt<size, big_endian>::Mips_output_data_plt(Layout* layout, |
+ Output_data_space* got_plt) |
+ : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), count_(0) |
+{ |
+ this->rel_ = new Reloc_section(false); |
+ layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL, |
+ elfcpp::SHF_ALLOC, this->rel_, |
+ ORDER_DYNAMIC_PLT_RELOCS, false); |
+} |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_plt<size, big_endian>::do_adjust_output_section( |
+ Output_section* os) |
+{ os->set_entsize(0); } |
+ |
+// Add an entry to the PLT. |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_plt<size, big_endian>::add_entry(Symbol* gsym) |
+{ |
+ gold_assert(!gsym->has_plt_offset()); |
+ section_offset_type plt_offset = (this->count_) * sizeof(plt_entry) |
+ + sizeof(first_plt_entry[0]); |
+ gsym->set_plt_offset(plt_offset); |
+ |
+ ++this->count_; |
+ |
+ section_offset_type got_offset = this->got_plt_->current_data_size(); |
+ |
+ // Every PLT entry needs a GOT entry which points back to the PLT |
+ // entry (this will be changed by the dynamic linker, normally |
+ // lazily when the function is called). |
+ this->got_plt_->set_current_data_size(got_offset + 4); |
+ |
+ gsym->set_needs_dynsym_entry(); |
+ this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_, |
+ got_offset); |
+} |
+ |
+// Mips PLTs. |
+ |
+// The first entry in the PLT. |
+ |
+template<int size, bool big_endian> |
+const uint32_t Mips_output_data_plt<size, big_endian>::first_plt_entry[3][8] = |
+{ |
+ // The format of the first PLT entry in an O32 executable. |
+ { |
+ 0x3c1c0000, // lui $28, %hi(&GOTPLT[0]) |
+ 0x8f990000, // lw $25, %lo(&GOTPLT[0])($28) |
+ 0x279c0000, // addiu $28, $28, %lo(&GOTPLT[0]) |
+ 0x031cc023, // subu $24, $24, $28 |
+ 0x03e07821, // move $15, $31 |
+ 0x0018c082, // srl $24, $24, 2 |
+ 0x0320f809, // jalr $25 |
+ 0x2718fffe // subu $24, $24, 2 |
+ }, |
+ |
+ // The format of the first PLT entry in an N32 executable. Different |
+ // because gp ($28) is not available; we use t2 ($14) instead. |
+ { |
+ 0x3c0e0000, // lui $14, %hi(&GOTPLT[0]) |
+ 0x8dd90000, // lw $25, %lo(&GOTPLT[0])($14) |
+ 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0]) |
+ 0x030ec023, // subu $24, $24, $14 |
+ 0x03e07821, // move $15, $31 |
+ 0x0018c082, // srl $24, $24, 2 |
+ 0x0320f809, // jalr $25 |
+ 0x2718fffe // subu $24, $24, 2 |
+ }, |
+ |
+ // The format of the first PLT entry in an N64 executable. Different |
+ // from N32 because of the increased size of GOT entries. |
+ { |
+ 0x3c0e0000, // lui $14, %hi(&GOTPLT[0]) |
+ 0xddd90000, // ld $25, %lo(&GOTPLT[0])($14) |
+ 0x25ce0000, // addiu $14, $14, %lo(&GOTPLT[0]) |
+ 0x030ec023, // subu $24, $24, $14 |
+ 0x03e07821, // move $15, $31 |
+ 0x0018c0c2, // srl $24, $24, 3 |
+ 0x0320f809, // jalr $25 |
+ 0x2718fffe // subu $24, $24, 2 |
+ } |
+}; |
+ |
+// Subsequent entries in the PLT. |
+ |
+template<int size, bool big_endian> |
+const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[4] = |
+{ |
+ 0x3c0f0000, // lui $15, %hi(.got.plt entry) |
+ 0x8df90000, // l[wd] $25, %lo(.got.plt entry)($15) |
+ 0x03200008, // jr $25 |
+ 0x25f80000 // addiu $24, $15, %lo(.got.plt entry) |
+}; |
+ |
+// Write out the PLT. This uses the hand-coded instructions above, |
+// and adjusts them as needed. |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_plt<size, big_endian>::do_write(Output_file* of) |
+{ |
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; |
+ |
+ // Read processor-specific flags in ELF file header. |
+ const unsigned char* pehdr = of->get_output_view(elfcpp::file_header_offset, |
+ elfcpp::Elf_sizes<size>::ehdr_size); |
+ elfcpp::Ehdr<size, big_endian> ehdr(pehdr); |
+ const size_t abi = this->abi(ehdr.get_e_flags()); |
+ |
+ const off_t offset = this->offset(); |
+ const section_size_type oview_size = |
+ convert_to_section_size_type(this->data_size()); |
+ unsigned char* const oview = of->get_output_view(offset, oview_size); |
+ |
+ const off_t got_file_offset = this->got_plt_->offset(); |
+ const section_size_type got_size = |
+ convert_to_section_size_type(this->got_plt_->data_size()); |
+ unsigned char* const got_view = of->get_output_view(got_file_offset, |
+ got_size); |
+ unsigned char* pov = oview; |
+ |
+ Mips_address plt_address = this->address(); |
+ Mips_address got_address = this->got_plt_->address(); |
+ |
+ uint32_t plt_insn0 = first_plt_entry[abi][0] | (((got_address + 0x8000) >> 16) & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0); |
+ uint32_t plt_insn1 = first_plt_entry[abi][1] | (got_address & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1); |
+ uint32_t plt_insn2 = first_plt_entry[abi][2] | (got_address & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2); |
+ |
+ for(size_t i = 3; i < 8; i++) |
+ elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, |
+ first_plt_entry[abi][i]); |
+ |
+ pov += sizeof(first_plt_entry[abi]); |
+ |
+ unsigned char* got_pov = got_view; |
+ |
+ memset(got_pov, 0, 8); |
+ got_pov += 8; |
+ |
+ const int rel_size = elfcpp::Elf_sizes<32>::rel_size; |
+ unsigned int plt_offset = sizeof(first_plt_entry[abi]); |
+ unsigned int plt_rel_offset = 0; |
+ unsigned int got_offset = 8; |
+ const unsigned int count = this->count_; |
+ for (unsigned int i = 0; |
+ i < count; |
+ ++i, |
+ pov += sizeof(plt_entry), |
+ got_pov += 4, |
+ plt_offset += sizeof(plt_entry), |
+ plt_rel_offset += rel_size, |
+ got_offset += 4) |
+ { |
+ // Set and adjust the PLT entry itself. |
+ int32_t offset = (got_address + got_offset); |
+ |
+ uint32_t plt_insn0 = plt_entry[0] | (((offset + 0x8000) >> 16) & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0); |
+ uint32_t plt_insn1 = plt_entry[1] | (offset & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1); |
+ uint32_t plt_insn2 = plt_entry[2]; |
+ elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2); |
+ uint32_t plt_insn3 = plt_entry[3] | (offset & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov + 12, plt_insn3); |
+ |
+ // Set the entry in the GOT. |
+ elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address); |
+ } |
+ |
+ gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); |
+ gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); |
+ |
+ of->write_output_view(offset, oview_size, oview); |
+ of->write_output_view(got_file_offset, got_size, got_view); |
+} |
+ |
+// Create a GOT entry for the TLS module index. |
+ |
+template<int size, bool big_endian> |
+unsigned int |
+Target_mips<size, big_endian>::got_mod_index_entry( |
+ Symbol_table* symtab, |
+ Layout* layout, |
+ Sized_relobj_file<size, big_endian>* object) |
+{ |
+ if (this->got_mod_index_offset_ == -1U) |
+ { |
+ gold_assert(symtab != NULL && layout != NULL && object != NULL); |
+ Mips_output_data_got<size, big_endian>* got = |
+ this->got_section(symtab, layout); |
+ unsigned int got_offset; |
+ if (!parameters->doing_static_link()) |
+ { |
+ got_offset = got->add_constant(0); |
+ |
+ Reloc_section* rel_dyn = this->rel_dyn_section(layout); |
+ rel_dyn->add_local(object, 0, |
+ size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 : |
+ elfcpp::R_MIPS_TLS_DTPMOD64, got, |
+ got_offset); |
+ } |
+ else |
+ { |
+ // We are doing a static link. Just mark it as belong to module 1, |
+ // the executable. |
+ got_offset = got->add_constant(1); |
+ } |
+ |
+ got->add_constant(0); |
+ this->got_mod_index_offset_ = got_offset; |
+ } |
+ |
+ return this->got_mod_index_offset_; |
+} |
+ |
+// Create a PLT entry for a global symbol. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab, |
+ Layout* layout, |
+ Symbol* gsym) |
+{ |
+ if (gsym->has_plt_offset()) |
+ return; |
+ |
+ if (this->plt_ == NULL) |
+ { |
+ // Create the GOT section first. |
+ this->got_section(symtab, layout); |
+ |
+ // Create .rel.dyn section. |
+ this->rel_dyn_section(layout); |
+ |
+ layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, |
+ (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE), |
+ this->got_plt_, ORDER_DATA, false); |
+ |
+ // The first two entries are reserved. |
+ this->got_plt_->set_current_data_size(2 * 4); |
+ |
+ this->plt_ = new Mips_output_data_plt<size, big_endian>(layout, |
+ this->got_plt_); |
+ layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, |
+ (elfcpp::SHF_ALLOC |
+ | elfcpp::SHF_EXECINSTR), |
+ this->plt_, ORDER_PLT, false); |
+ |
+ // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section. |
+ symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL, |
+ Symbol_table::PREDEFINED, |
+ this->plt_, |
+ 0, 0, elfcpp::STT_FUNC, |
+ elfcpp::STB_LOCAL, |
+ elfcpp::STV_DEFAULT, 0, |
+ false, false); |
+ } |
+ |
+ this->plt_->add_entry(gsym); |
+} |
+ |
+ |
+// Create a .MIPS.stubs entry for a global symbol. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::make_mips_stubs_entry(Symbol_table* symtab, |
+ Layout* layout, |
+ Symbol* gsym) |
+{ |
+ if (gsym->has_plt_offset()) |
+ return; |
+ |
+ if (this->mips_stubs_ == NULL) |
+ { |
+ // Create the .MIPS.stubs section first. |
+ |
+ this->mips_stubs_ = new Mips_output_data_mips_stubs<size, big_endian> |
+ (layout, this->got_section(symtab, layout), this->gp_); |
+ |
+ layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS, |
+ (elfcpp::SHF_ALLOC |
+ | elfcpp::SHF_EXECINSTR), |
+ this->mips_stubs_, ORDER_PLT, false); |
+ } |
+ this->mips_stubs_->add_entry(gsym); |
+} |
+ |
+template<int size, bool big_endian> |
+Mips_output_data_stub<size, big_endian>::Mips_output_data_stub() |
+ : Output_section_data(size == 32 ? 4 : 8), count_(0) |
+{} |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_stub<size, big_endian>::do_adjust_output_section( |
+ Output_section* os) |
+{ |
+ os->set_entsize(0); |
+} |
+ |
+// Add an entry to the STUB. |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_stub<size, big_endian>::add_entry(Symbol_table* symtab, |
+ Target_mips<size, big_endian>* target, |
+ Symbol* gsym) |
+{ |
+ Symbol* sym; |
+ char* buffer = new char[strlen(gsym->name()) + 6]; |
+ gold_assert(buffer != NULL); |
+ |
+ section_offset_type stub_offset = this->count_ * 4 * 4; |
+ |
+ ++this->count_; |
+ |
+ sprintf(buffer, ".pic.%s", gsym->name()); |
+ |
+ sym = symtab->define_in_output_data(buffer, NULL, |
+ Symbol_table::PREDEFINED, |
+ target->stub(), |
+ stub_offset, 0x10, elfcpp::STT_FUNC, |
+ elfcpp::STB_GLOBAL, |
+ elfcpp::STV_DEFAULT, 0, |
+ false, false); |
+ |
+ target->add_stub_symbol(gsym->name(), sym); |
+ this->add_symbol(gsym); |
+ |
+ delete [] buffer; |
+} |
+ |
+ |
+// Create a STUB entry for a global symbol. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::make_stub_entry(Layout* layout) |
+{ |
+ if (this->stub_ == NULL) |
+ { |
+ this->stub_ = new Mips_output_data_stub<size, big_endian>(); |
+ layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS, |
+ (elfcpp::SHF_ALLOC |
+ | elfcpp::SHF_EXECINSTR |
+ | elfcpp::SHF_WRITE), |
+ this->stub_, ORDER_TEXT, false); |
+ } |
+} |
+ |
+template<int size, bool big_endian> |
+const uint32_t Mips_output_data_stub<size, big_endian>::stub_entry[4] = |
+{ |
+ 0x3c190000, // lui $25, %hi(func) |
+ 0x08000000, // j func |
+ 0x27390000, // add $25, $25, %lo(func) |
+ 0x00000000 // nop |
+}; |
+ |
+// Write out the STUB. This uses the hand-coded instructions above, |
+// and adjusts them as needed. |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_stub<size, big_endian>::do_write(Output_file* of) |
+{ |
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; |
+ |
+ const off_t offset = this->offset(); |
+ const section_size_type oview_size = |
+ convert_to_section_size_type(this->data_size()); |
+ unsigned char* const oview = of->get_output_view(offset, oview_size); |
+ |
+ unsigned char* pov = oview; |
+ |
+ const unsigned int count = this->count_; |
+ for (unsigned int i = 0; |
+ i < count; |
+ ++i, |
+ pov += 4*4) |
+ { |
+ Sized_symbol<size> *sym = static_cast<Sized_symbol<size>*> |
+ (this->symbol(i)); |
+ |
+ unsigned int value = (unsigned int)sym->value(); |
+ gold_assert(offset >= 0 && offset < 0x0fffffff); |
+ uint32_t stub_insn0 = stub_entry[0] | (((value + 0x8000) >> 16) & 0xffff); |
+ elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn0); |
+ uint32_t stub_insn1 = stub_entry[1] | (((value >> 2) & 0x03ffffff)); |
+ elfcpp::Swap<32, big_endian>::writeval(pov + 4, stub_insn1); |
+ uint32_t stub_insn2 = stub_entry[2] | ((value & 0x0000ffff)); |
+ elfcpp::Swap<32, big_endian>::writeval(pov + 8, stub_insn2); |
+ uint32_t stub_insn3 = stub_entry[3]; |
+ elfcpp::Swap<32, big_endian>::writeval(pov + 12, stub_insn3); |
+ } |
+ gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); |
+ |
+ of->write_output_view(offset, oview_size, oview); |
+} |
+ |
+// Process the relocations to determine unreferenced sections for |
+// garbage collection. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::gc_process_relocs( |
+ Symbol_table* symtab, |
+ Layout* layout, |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int data_shndx, |
+ unsigned int, |
+ 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) |
+{ |
+ typedef Target_mips<size, big_endian> Mips; |
+ typedef typename Target_mips<size, big_endian>::Scan Scan; |
+ |
+ gold::gc_process_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan, |
+ typename Target_mips::Relocatable_size_for_reloc>( |
+ symtab, |
+ layout, |
+ this, |
+ object, |
+ data_shndx, |
+ prelocs, |
+ reloc_count, |
+ output_section, |
+ needs_special_offset_handling, |
+ local_symbol_count, |
+ plocal_symbols); |
+} |
+ |
+// Scan the relocations to look for symbol adjustments. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::scan_relocs( |
+ Symbol_table* symtab, |
+ Layout* layout, |
+ Sized_relobj_file<size, big_endian>* 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) |
+{ |
+ typedef Target_mips<size, big_endian> Mips; |
+ typedef typename Target_mips<size, big_endian>::Scan Scan; |
+ |
+ if (sh_type == elfcpp::SHT_RELA) |
+ { |
+ gold_error(_("%s: unsupported RELA reloc section"), |
+ object->name().c_str()); |
+ return; |
+ } |
+ |
+ gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan>( |
+ symtab, |
+ layout, |
+ this, |
+ object, |
+ data_shndx, |
+ prelocs, |
+ reloc_count, |
+ output_section, |
+ needs_special_offset_handling, |
+ local_symbol_count, |
+ plocal_symbols); |
+} |
+ |
+template<int size, bool big_endian> |
+bool |
+Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags) |
+{ |
+ return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0 |
+ || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32 |
+ || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32 |
+ || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1 |
+ || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2 |
+ || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32 |
+ || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2); |
+} |
+ |
+template<int size, bool big_endian> |
+bool |
+Target_mips<size, big_endian>::mips_mach_extends(elfcpp::Elf_Word base, |
+ elfcpp::Elf_Word extension) |
+{ |
+ unsigned int i; |
+ |
+ if(base == extension) |
+ return true; |
+ |
+ if ((base == elfcpp::E_MIPS_ARCH_32) |
+ && mips_mach_extends(elfcpp::E_MIPS_ARCH_64, extension)) |
+ return true; |
+ |
+ if ((base == elfcpp::E_MIPS_ARCH_32R2) |
+ && mips_mach_extends(elfcpp::E_MIPS_ARCH_64R2, extension)) |
+ return true; |
+ |
+ for (i = 0; i < this->extensions_.size(); i++) |
+ if (extension == this->extensions_[i].second) |
+ { |
+ extension = this->extensions_[i].first; |
+ if (extension == base) |
+ return true; |
+ } |
+ |
+ return false; |
+} |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::merge_processor_specific_flags(const char* name, |
+ elfcpp::Elf_Word in_flags) |
+{ |
+ elfcpp::Elf_Word out_flags = this->processor_specific_flags(); |
+ |
+ // If flags are not set yet, just copy them. |
+ if(out_flags == 0) |
+ this->set_processor_specific_flags(in_flags); |
+ else |
+ { |
+ elfcpp::Elf_Word new_flags = out_flags; |
+ new_flags |= in_flags & elfcpp::EF_MIPS_NOREORDER; |
+ |
+ // Check flag compatibility. |
+ in_flags &= ~elfcpp::EF_MIPS_NOREORDER; |
+ out_flags &= ~elfcpp::EF_MIPS_NOREORDER; |
+ |
+ // Some IRIX 6 BSD-compatibility objects have this bit set. It |
+ // doesn't seem to matter. |
+ in_flags &= ~elfcpp::EF_MIPS_XGOT; |
+ out_flags &= ~elfcpp::EF_MIPS_XGOT; |
+ |
+ // MIPSpro generates ucode info in n64 objects. Again, we should |
+ // just be able to ignore this. |
+ in_flags &= ~elfcpp::EF_MIPS_UCODE; |
+ out_flags &= ~elfcpp::EF_MIPS_UCODE; |
+ |
+ if((in_flags & elfcpp::EF_MIPS_DYNAMIC) != 0) |
+ in_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC; |
+ |
+ // Input flags are same as output, do nothing. |
+ if(in_flags == out_flags) |
+ return; |
+ |
+ // TODO AS: If input file has no sections, its flags are not important. |
+ |
+ if (((in_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0) |
+ != ((out_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)) |
+ gold_warning(_("Linking abicalls files with non-abicalls files.")); |
+ |
+ if (in_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) |
+ new_flags |= elfcpp::EF_MIPS_CPIC; |
+ if (!(in_flags & elfcpp::EF_MIPS_PIC)) |
+ new_flags &= ~elfcpp::EF_MIPS_PIC; |
+ |
+ in_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC); |
+ out_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC); |
+ |
+ // Compare the ISAs. |
+ if (mips_32bit_flags(out_flags) != mips_32bit_flags(in_flags)) |
+ { |
+ gold_error(_("Source object %s is %d-bit, but output is %d-bit"), |
+ name, mips_32bit_flags(in_flags) ? 32 : 64, |
+ mips_32bit_flags(out_flags) ? 32 : 64); |
+ } |
+ else if(!mips_mach_extends(in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH), |
+ out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH))) |
+ { |
+ // Input ISA is not set. Just skip it. |
+ if((in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)) == 0); |
+ // ISA is not set yet, do it now. |
+ else if((out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)) == 0) |
+ new_flags |= in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE); |
+ // OBFD's ISA isn't the same as, or an extension of, IBFD's. |
+ else if(mips_mach_extends(out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH), |
+ in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH))) |
+ { |
+ // Copy the architecture info from IBFD to OBFD. Also copy |
+ // the 32-bit flag (if set) so that we continue to recognise |
+ // OBFD as a 32-bit binary. |
+ new_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH); |
+ new_flags |= |
+ in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE); |
+ |
+ // Copy across the ABI flags if OBFD doesn't use them |
+ // and if that was what caused us to treat IBFD as 32-bit. |
+ if((out_flags & elfcpp::EF_MIPS_ABI) == 0 |
+ && mips_32bit_flags(in_flags) |
+ && !mips_32bit_flags(in_flags & ~elfcpp::EF_MIPS_ABI)) |
+ new_flags |= in_flags & elfcpp::EF_MIPS_ABI; |
+ } |
+ else |
+ { |
+ // The ISAs aren't compatible. |
+ gold_error(_("%s ISA is 0x%x and it is not compatible with current" |
+ " ISA 0x%x"), |
+ name, in_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH), |
+ out_flags & (elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH)); |
+ } |
+ } |
+ |
+ in_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE); |
+ out_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE); |
+ |
+ // Compare ABIs. TODO AS: The 64-bit ABI does not use EF_MIPS_ABI. |
+ // But, it does set EI_CLASS differently from any 32-bit ABI. |
+ if ((in_flags & elfcpp::EF_MIPS_ABI) != (out_flags & elfcpp::EF_MIPS_ABI)) |
+ { |
+ // Only error if both are set (to different values). |
+ if ((in_flags & elfcpp::EF_MIPS_ABI) && (out_flags & elfcpp::EF_MIPS_ABI)) |
+ { |
+ gold_error(_("%s ABI %d is different then current ABI %d"), |
+ name, in_flags & elfcpp::EF_MIPS_ABI, |
+ out_flags & elfcpp::EF_MIPS_ABI); |
+ } |
+ |
+ in_flags &= ~elfcpp::EF_MIPS_ABI; |
+ out_flags &= ~elfcpp::EF_MIPS_ABI; |
+ } |
+ |
+ // Compare ABIs. |
+ if ((in_flags & elfcpp::EF_MIPS_ABI2) != (out_flags & elfcpp::EF_MIPS_ABI2)) |
+ { |
+ gold_error(_("%s is ABI%s while output is ABI%s"), |
+ name, ((in_flags & elfcpp::EF_MIPS_ABI2) ? "2" : ""), |
+ ((out_flags & elfcpp::EF_MIPS_ABI2) ? "2" : "")); |
+ |
+ in_flags &= ~elfcpp::EF_MIPS_ABI2; |
+ out_flags &= ~elfcpp::EF_MIPS_ABI2; |
+ } |
+ |
+ // Compare ASEs. Forbid linking MIPS16 and microMIPS ASE modules together |
+ // and allow arbitrary mixing of the remaining ASEs (retain the union). |
+ if ((in_flags & elfcpp::EF_MIPS_ARCH_ASE) != (out_flags & elfcpp::EF_MIPS_ARCH_ASE)) |
+ { |
+ int old_micro = out_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS; |
+ int new_micro = in_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS; |
+ int old_m16 = out_flags & elfcpp::EF_MIPS_ARCH_ASE_M16; |
+ int new_m16 = in_flags & elfcpp::EF_MIPS_ARCH_ASE_M16; |
+ int micro_mis = old_m16 && new_micro; |
+ int m16_mis = old_micro && new_m16; |
+ |
+ if (m16_mis || micro_mis) |
+ { |
+ gold_error(_("%s ASE is %s while output ASE is %s"), |
+ name, m16_mis ? "MIPS16" : "microMIPS", |
+ m16_mis ? "microMIPS" : "MIPS16"); |
+ } |
+ |
+ new_flags |= in_flags & elfcpp::EF_MIPS_ARCH_ASE; |
+ |
+ in_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE; |
+ out_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE; |
+ } |
+ |
+ // Warn about any other mismatches |
+ if (in_flags != out_flags) |
+ { |
+ gold_error(_("%s uses different flags 0x%x then output 0x%x"), |
+ name, in_flags, out_flags); |
+ } |
+ |
+ this->set_processor_specific_flags(new_flags); |
+ } |
+} |
+ |
+// Finalize the sections. |
+ |
+template <int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::do_finalize_sections(Layout* layout, |
+ const Input_objects* input_objects, |
+ Symbol_table* symtab) |
+{ |
+ // Add NULL segment. |
+ if(!parameters->options().relocatable()) |
+ layout->make_output_segment(elfcpp::PT_NULL, 0); |
+ |
+ for (Layout::Section_list::const_iterator p = layout->section_list().begin(); |
+ p != layout->section_list().end(); ++p) |
+ if ((*p)->type() == elfcpp::SHT_MIPS_REGINFO) |
+ { |
+ Mips_output_section_reginfo<size>* reginfo = |
+ Mips_output_section_reginfo<size>::as_mips_output_section_reginfo(*p); |
+ |
+ if(this->gp() == NULL) |
+ reginfo->set_gp(0); |
+ else |
+ reginfo->set_gp(this->gp()); |
+ |
+ if(!parameters->options().relocatable()) |
+ { |
+ Output_segment *reginfo_segment = |
+ layout->make_output_segment(elfcpp::PT_MIPS_REGINFO, elfcpp::PF_R); |
+ reginfo_segment->add_output_section_to_nonload(reginfo, elfcpp::PF_R); |
+ } |
+ |
+ // There is only one .reginfo section |
+ break; |
+ } |
+ |
+ if(this->got_section()) |
+ { |
+ this->got_section()->add_global_relocs(); |
+ this->got_section()->add_global_tls_relocs(); |
+ } |
+ |
+ // Merge processor-specific flags. |
+ for (Input_objects::Relobj_iterator p = input_objects->relobj_begin(); |
+ p != input_objects->relobj_end(); |
+ ++p) |
+ { |
+ Sized_relobj_file<size, big_endian>* relobj = |
+ static_cast<const Sized_relobj_file<size, big_endian>*>(*p); |
+ |
+ Input_file::Format format = relobj->input_file()->format(); |
+ if (format == Input_file::FORMAT_ELF) |
+ { |
+ // Read processor-specific flags in ELF file header. |
+ const unsigned char* pehdr = relobj-> |
+ get_view(elfcpp::file_header_offset, |
+ elfcpp::Elf_sizes<size>::ehdr_size, |
+ true, false); |
+ |
+ elfcpp::Ehdr<size, big_endian> ehdr(pehdr); |
+ elfcpp::Elf_Word in_flags = ehdr.get_e_flags(); |
+ |
+ this->merge_processor_specific_flags(relobj->name().c_str(), in_flags); |
+ } |
+ } |
+ |
+ for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin(); |
+ p != input_objects->dynobj_end(); |
+ ++p) |
+ { |
+ Sized_dynobj<size, big_endian>* dynobj = |
+ static_cast<const Sized_dynobj<size, big_endian>*>(*p); |
+ |
+ // Read processor-specific flags. |
+ const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset, |
+ elfcpp::Elf_sizes<size>::ehdr_size, |
+ true, false); |
+ |
+ elfcpp::Ehdr<size, big_endian> ehdr(pehdr); |
+ elfcpp::Elf_Word in_flags = ehdr.get_e_flags(); |
+ |
+ this->merge_processor_specific_flags(dynobj->name().c_str(), in_flags); |
+ } |
+ |
+ // Fill in some more dynamic tags. |
+ const Reloc_section* rel_plt = (this->plt_ == NULL |
+ ? NULL |
+ : this->plt_->rel_plt()); |
+ layout->add_target_dynamic_tags(true, this->got_, rel_plt, |
+ this->rel_dyn_, true, false); |
+ |
+ Output_data_dynamic* const odyn = layout->dynamic_data(); |
+ if((odyn) && |
+ (!parameters->options().relocatable() && !parameters->doing_static_link())) |
+ { |
+ // In case there is no .got section, create one. |
+ this->got_section(symtab, layout); |
+ |
+ unsigned int d_val; |
+ // This element holds a 32-bit version id for the Runtime |
+ // Linker Interface. This will start at integer value 1. |
+ d_val = 0x01; |
+ odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val); |
+ |
+ // Dynamic flags |
+ d_val = elfcpp::RHF_NOTPOT; |
+ odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val); |
+ |
+ // This member holds the base address of the segment. |
+ odyn->add_target_specific(elfcpp::DT_MIPS_BASE_ADDRESS); |
+ |
+ // This member holds the number of entries in the .dynsym section. |
+ odyn->add_target_specific(elfcpp::DT_MIPS_SYMTABNO); |
+ |
+ // This member holds the index of the first dynamic symbol |
+ // table entry that corresponds to an entry in the global offset table. |
+ odyn->add_target_specific(elfcpp::DT_MIPS_GOTSYM); |
+ |
+ // This member holds the number of local global offset table entries. |
+ odyn->add_target_specific(elfcpp::DT_MIPS_LOCAL_GOTNO); |
+ |
+ if(this->plt_ != NULL) |
+ // DT_MIPS_PLTGOT dynamic tag |
+ odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_); |
+ } |
+ |
+ // Set the .text section. |
+ this->text_section_ = layout->find_output_section(".text"); |
+ // Set the symbol table |
+ this->symtab_ = symtab; |
+ // Set the layout |
+ this->layout_ = layout; |
+ |
+ // Emit any relocs we saved in an attempt to avoid generating COPY |
+ // relocs. |
+ if (this->copy_relocs_.any_saved_relocs()) |
+ this->copy_relocs_.emit(this->rel_dyn_section(layout)); |
+ |
+ Target::do_finalize_sections (layout, input_objects, symtab); |
+ } |
+ |
+// Functor class for processing the global symbol table. Processes the |
+// index in dynamic section if the symbol has GOT entry, and finds |
+// minimum index value. |
+ |
+template<int size, bool big_endian> |
+class Symbol_visitor_gotsym |
+{ |
+ public: |
+ Symbol_visitor_gotsym() |
+ { } |
+ |
+ void |
+ operator()(const Sized_symbol<size>* sym) |
+ { |
+ if (sym->has_got_offset(0) && sym->needs_dynsym_entry() && sym->dynsym_index() < this->min_index_) |
+ this->min_index_ = sym->dynsym_index(); |
+ } |
+ |
+ unsigned int min_dynsym_index() |
+ { |
+ return this->min_index_; |
+ } |
+ private: |
+ static unsigned int min_index_; |
+}; |
+ |
+template<int size, bool big_endian> |
+unsigned int Symbol_visitor_gotsym<size, big_endian>::min_index_ = -1U; |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::do_fix_sections(Layout*, Symbol_table* ) |
+{ |
+ Output_section *dynsym = this->layout_->find_output_section(".dynsym"); |
+ |
+ if((dynsym != NULL) && (this->mips_stubs_ != NULL)) |
+ { |
+ this->mips_stubs_->set_big_dyn_symtab(((unsigned int)dynsym->data_size()/16) > 0x10000); |
+ |
+ this->mips_stubs_->fix_symbol_values(); |
+ } |
+} |
+ |
+// MIPS specific dynamic tags. |
+template<int size, bool big_endian> |
+unsigned int |
+Target_mips<size, big_endian>::do_dynamic_tag_value(elfcpp::DT tag) const |
+{ |
+ Symbol_table *symtab = this->symtab_; |
+ Layout *layout = this->layout_; |
+ |
+ Output_section *dynsym = layout->find_output_section(".dynsym"); |
+ gold_assert(dynsym != NULL); |
+ |
+ switch(tag) |
+ { |
+ case elfcpp::DT_MIPS_BASE_ADDRESS: |
+ { |
+ typedef std::vector<Output_segment*> Segment_list; |
+ const Segment_list segment_list_ = layout->segment_list(); |
+ unsigned int vaddr = -1U; |
+ for (Segment_list::const_iterator p = segment_list_.begin(); |
+ p != segment_list_.end(); |
+ ++p) |
+ { |
+ if(((*p)->flags() & elfcpp::PF_R) != 0) |
+ { |
+ if((*p)->vaddr() < vaddr) |
+ vaddr = (*p)->vaddr(); |
+ } |
+ } |
+ return vaddr; |
+ } |
+ case elfcpp::DT_MIPS_SYMTABNO: |
+ return (unsigned int)dynsym->data_size()/16; |
+ case elfcpp::DT_MIPS_GOTSYM: |
+ { |
+ unsigned int min_index = symtab->global_got_index(); |
+ |
+ if (min_index == -1U) |
+ min_index = (unsigned int)dynsym->data_size()/16; |
+ |
+ return min_index; |
+ } |
+ case elfcpp::DT_MIPS_LOCAL_GOTNO: |
+ |
+ return(unsigned int)this->got_->data_size()/4 - |
+ ((unsigned int)dynsym->data_size()/16 - symtab->global_got_index()) - |
+ this->got_->tls_entries(); |
+ default: |
+ gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag); |
+ } |
+ |
+ return (unsigned int)-1; |
+} |
+ |
+// Relocate a section. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::relocate_section( |
+ const Relocate_info<size, big_endian>* relinfo, |
+ unsigned int sh_type, |
+ const unsigned char* prelocs, |
+ size_t reloc_count, |
+ Output_section* output_section, |
+ bool needs_special_offset_handling, |
+ unsigned char* view, |
+ typename elfcpp::Elf_types<size>::Elf_Addr address, |
+ section_size_type view_size, |
+ const Reloc_symbol_changes* reloc_symbol_changes) |
+{ |
+ typedef Target_mips<size, big_endian> Mips; |
+ typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate; |
+ |
+ gold_assert(sh_type == elfcpp::SHT_REL); |
+ |
+ gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_REL, |
+ Mips_relocate>( |
+ relinfo, |
+ this, |
+ prelocs, |
+ reloc_count, |
+ output_section, |
+ needs_special_offset_handling, |
+ view, |
+ address, |
+ view_size, |
+ reloc_symbol_changes); |
+} |
+ |
+// Return the size of a relocation while scanning during a relocatable |
+// link. |
+ |
+template<int size, bool big_endian> |
+unsigned int |
+Target_mips<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc( |
+ unsigned int r_type, |
+ Relobj* object) |
+{ |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_NONE: |
+ case elfcpp::R_MIPS_UNUSED1: |
+ case elfcpp::R_MIPS_UNUSED2: |
+ case elfcpp::R_MIPS_UNUSED3: |
+ case elfcpp::R_MIPS_INSERT_A: |
+ case elfcpp::R_MIPS_INSERT_B: |
+ case elfcpp::R_MIPS_DELETE: |
+ case elfcpp::R_MIPS_TLS_DTPMOD64: |
+ case elfcpp::R_MIPS_TLS_DTPREL64: |
+ case elfcpp::R_MIPS_REL16: |
+ case elfcpp::R_MIPS_ADD_IMMEDIATE: |
+ case elfcpp::R_MIPS_PJUMP: |
+ case elfcpp::R_MIPS_RELGOT: |
+ case elfcpp::R_MIPS_TLS_TPREL64: |
+ case elfcpp::R_MIPS_GNU_VTINHERIT: |
+ case elfcpp::R_MIPS_GNU_VTENTRY: |
+ return 0; |
+ |
+ case elfcpp::R_MIPS_64: |
+ case elfcpp::R_MIPS_SUB: |
+ return 8; |
+ |
+ case elfcpp::R_MIPS_32: |
+ case elfcpp::R_MIPS_TLS_DTPMOD32: |
+ case elfcpp::R_MIPS_TLS_DTPREL32: |
+ case elfcpp::R_MIPS_TLS_TPREL32: |
+ case elfcpp::R_MIPS_REL32: |
+ case elfcpp::R_MIPS_PC32: |
+ case elfcpp::R_MIPS_GPREL32: |
+ case elfcpp::R_MIPS_JALR: |
+ case elfcpp::R_MIPS_SCN_DISP: |
+ return 4; |
+ |
+ case elfcpp::R_MIPS_16: |
+ case elfcpp::R_MIPS_HI16: |
+ case elfcpp::R_MIPS_LO16: |
+ case elfcpp::R_MIPS_HIGHER: |
+ case elfcpp::R_MIPS_HIGHEST: |
+ case elfcpp::R_MIPS_GPREL16: |
+ case elfcpp::R_MIPS16_HI16: |
+ case elfcpp::R_MIPS16_LO16: |
+ case elfcpp::R_MIPS_PC16: |
+ case elfcpp::R_MIPS_GNU_REL16_S2: |
+ case elfcpp::R_MIPS_GOT16: |
+ case elfcpp::R_MIPS16_GOT16: |
+ case elfcpp::R_MIPS_CALL16: |
+ case elfcpp::R_MIPS16_CALL16: |
+ case elfcpp::R_MIPS_GOT_HI16: |
+ case elfcpp::R_MIPS_CALL_HI16: |
+ case elfcpp::R_MIPS_GOT_LO16: |
+ case elfcpp::R_MIPS_CALL_LO16: |
+ case elfcpp::R_MIPS_TLS_DTPREL_HI16: |
+ case elfcpp::R_MIPS_TLS_DTPREL_LO16: |
+ case elfcpp::R_MIPS_TLS_TPREL_HI16: |
+ case elfcpp::R_MIPS_TLS_TPREL_LO16: |
+ case elfcpp::R_MIPS16_GPREL: |
+ case elfcpp::R_MIPS_GOT_DISP: |
+ case elfcpp::R_MIPS_LITERAL: |
+ case elfcpp::R_MIPS_GOT_PAGE: |
+ case elfcpp::R_MIPS_GOT_OFST: |
+ case elfcpp::R_MIPS_TLS_GD: |
+ case elfcpp::R_MIPS_TLS_LDM: |
+ case elfcpp::R_MIPS_TLS_GOTTPREL: |
+ return 2; |
+ |
+ // These relocations are not byte sized |
+ case elfcpp::R_MIPS_26: |
+ case elfcpp::R_MIPS16_26: |
+ return 4; |
+ |
+ // These relocations are not byte sized |
+ case elfcpp::R_MIPS_SHIFT5: |
+ case elfcpp::R_MIPS_SHIFT6: |
+ return 1; |
+ |
+ case elfcpp::R_MIPS_COPY: |
+ case elfcpp::R_MIPS_GLOB_DAT: |
+ case elfcpp::R_MIPS_JUMP_SLOT: |
+ object->error(_("unexpected reloc %u in object file"), r_type); |
+ return 0; |
+ |
+ default: |
+ object->error(_("unsupported reloc %u in object file"), r_type); |
+ return 0; |
+ } |
+} |
+ |
+// Scan the relocs during a relocatable link. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::scan_relocatable_relocs( |
+ Symbol_table* symtab, |
+ Layout* layout, |
+ Sized_relobj_file<size, big_endian>* 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* rr) |
+{ |
+ gold_assert(sh_type == elfcpp::SHT_REL); |
+ |
+ typedef Mips_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL, |
+ Relocatable_size_for_reloc> Scan_relocatable_relocs; |
+ |
+ gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_REL, |
+ Scan_relocatable_relocs>( |
+ symtab, |
+ layout, |
+ object, |
+ data_shndx, |
+ prelocs, |
+ reloc_count, |
+ output_section, |
+ needs_special_offset_handling, |
+ local_symbol_count, |
+ plocal_symbols, |
+ rr); |
+} |
+ |
+// Relocate a section during a relocatable link. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::relocate_for_relocatable( |
+ const Relocate_info<size, big_endian>* relinfo, |
+ unsigned int sh_type, |
+ const unsigned char* prelocs, |
+ size_t reloc_count, |
+ Output_section* output_section, |
+ off_t offset_in_output_section, |
+ const Relocatable_relocs* rr, |
+ unsigned char* view, |
+ typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
+ section_size_type view_size, |
+ unsigned char* reloc_view, |
+ section_size_type reloc_view_size) |
+{ |
+ gold_assert(sh_type == elfcpp::SHT_REL); |
+ |
+ gold::relocate_for_relocatable<size, big_endian, elfcpp::SHT_REL>( |
+ relinfo, |
+ prelocs, |
+ reloc_count, |
+ output_section, |
+ offset_in_output_section, |
+ rr, |
+ view, |
+ view_address, |
+ view_size, |
+ reloc_view, |
+ reloc_view_size); |
+} |
+ |
+// Perform target-specific processing in a relocatable link. This is |
+// only used if we use the relocation strategy RELOC_SPECIAL. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::relocate_special_relocatable( |
+ const Relocate_info<size, big_endian>* relinfo, |
+ unsigned int sh_type, |
+ const unsigned char* preloc_in, |
+ size_t relnum, |
+ Output_section* output_section, |
+ off_t offset_in_output_section, |
+ unsigned char* view, |
+ elfcpp::Elf_types<32>::Elf_Addr view_address, |
+ section_size_type, |
+ unsigned char* preloc_out) |
+{ |
+ // We can only handle REL type relocation sections. |
+ gold_assert(sh_type == elfcpp::SHT_REL); |
+ |
+ typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc Reltype; |
+ typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write |
+ Reltype_write; |
+ typedef typename elfcpp::Elf_types<32>::Elf_Addr Mips_address; |
+ typedef Mips_relocate_functions<size, big_endian> Reloc_funcs; |
+ |
+ const Mips_address invalid_address = static_cast<Mips_address>(0) - 1; |
+ |
+ const Sized_relobj_file<size, big_endian>* object = relinfo->object; |
+ const unsigned int local_count = object->local_symbol_count(); |
+ |
+ Reltype reloc(preloc_in); |
+ Reltype_write reloc_write(preloc_out); |
+ |
+ elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info(); |
+ const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); |
+ const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); |
+ |
+ // Get the new symbol index. |
+ // We only use RELOC_SPECIAL strategy in local relocations. |
+ gold_assert(r_sym < local_count); |
+ |
+ // We are adjusting a section symbol. We need to find |
+ // the symbol table index of the section symbol for |
+ // the output section corresponding to input section |
+ // in which this symbol is defined. |
+ bool is_ordinary; |
+ unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary); |
+ gold_assert(is_ordinary); |
+ Output_section* os = object->output_section(shndx); |
+ gold_assert(os != NULL); |
+ gold_assert(os->needs_symtab_index()); |
+ unsigned int new_symndx = os->symtab_index(); |
+ |
+ // Get the new offset--the location in the output section where |
+ // this relocation should be applied. |
+ |
+ Mips_address offset = reloc.get_r_offset(); |
+ Mips_address new_offset; |
+ if (offset_in_output_section != invalid_address) |
+ new_offset = offset + offset_in_output_section; |
+ else |
+ { |
+ section_offset_type sot_offset = |
+ convert_types<section_offset_type, Mips_address>(offset); |
+ section_offset_type new_sot_offset = |
+ output_section->output_offset(object, relinfo->data_shndx, |
+ sot_offset); |
+ gold_assert(new_sot_offset != -1); |
+ new_offset = new_sot_offset; |
+ } |
+ |
+ // In an object file, r_offset is an offset within the section. |
+ // In an executable or dynamic object, generated by |
+ // --emit-relocs, r_offset is an absolute address. |
+ if (!parameters->options().relocatable()) |
+ { |
+ new_offset += view_address; |
+ if (offset_in_output_section != invalid_address) |
+ new_offset -= offset_in_output_section; |
+ } |
+ |
+ reloc_write.put_r_offset(new_offset); |
+ reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type)); |
+ |
+ // Handle the reloc addend. |
+ // The relocation uses a section symbol in the input file. |
+ // We are adjusting it to use a section symbol in the output |
+ // file. The input section symbol refers to some address in |
+ // the input section. We need the relocation in the output |
+ // file to refer to that same address. This adjustment to |
+ // the addend is the same calculation we use for a simple |
+ // absolute relocation for the input section symbol. |
+ |
+ unsigned char* paddend = view + offset; |
+ typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY; |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_26: |
+ reloc_status = Reloc_funcs::rel26(paddend, object, object->local_symbol(r_sym), |
+ offset_in_output_section, false); |
+ |
+ break; |
+ |
+ default: |
+ gold_unreachable(); |
+ } |
+ |
+ // Report any errors. |
+ switch (reloc_status) |
+ { |
+ case Reloc_funcs::STATUS_OKAY: |
+ break; |
+ case Reloc_funcs::STATUS_OVERFLOW: |
+ gold_error_at_location(relinfo, relnum, new_offset, |
+ _("relocation overflow")); |
+ break; |
+ case Reloc_funcs::STATUS_BAD_RELOC: |
+ gold_error_at_location( |
+ relinfo, |
+ relnum, |
+ new_offset , |
+ _("unexpected opcode while processing relocation")); |
+ break; |
+ default: |
+ gold_unreachable(); |
+ } |
+} |
+ |
+// 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. |
+ |
+template<int size, bool big_endian> |
+tls::Tls_optimization |
+Target_mips<size, big_endian>::optimize_tls_reloc(bool, int) |
+{ |
+ // FIXME: Currently we do not do any TLS optimization. |
+ return tls::TLSOPT_NONE; |
+} |
+ |
+// We are about to emit a dynamic relocation of type R_TYPE. If the |
+// dynamic linker does not support it, issue an error. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::Scan::check_non_pic(Relobj* object, |
+ unsigned int r_type) |
+{ |
+ switch (r_type) |
+ { |
+ // These are the relocation types supported by glibc for MIPS. |
+ case elfcpp::R_MIPS_TLS_DTPMOD32: |
+ case elfcpp::R_MIPS_TLS_DTPREL32: |
+ case elfcpp::R_MIPS_TLS_TPREL32: |
+ case elfcpp::R_MIPS_REL32: |
+ case elfcpp::R_MIPS_GLOB_DAT: |
+ case elfcpp::R_MIPS_64: |
+ return; |
+ |
+ default: |
+ { |
+ // This prevents us from issuing more than one error per reloc |
+ // section. But we can still wind up issuing more than one |
+ // error per object file. |
+ if (this->issued_non_pic_error_) |
+ return; |
+ gold_assert(parameters->options().output_is_position_independent()); |
+ object->error(_("requires unsupported dynamic reloc; " |
+ "recompile with -fPIC")); |
+ this->issued_non_pic_error_ = true; |
+ return; |
+ } |
+ |
+ case elfcpp::R_MIPS_NONE: |
+ gold_unreachable(); |
+ } |
+} |
+ |
+// Scan a relocation for a local symbol. |
+ |
+template<int size, bool big_endian> |
+inline void |
+Target_mips<size, big_endian>::Scan::local( |
+ Symbol_table* symtab, |
+ Layout* layout, |
+ Target_mips<size, big_endian>* target, |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int data_shndx, |
+ Output_section* output_section, |
+ const elfcpp::Rel<size, big_endian>& reloc, |
+ unsigned int r_type, |
+ const elfcpp::Sym<size, big_endian>& lsym) |
+{ |
+ switch (r_type) |
+ { |
+ // Global offset table relocations |
+ case elfcpp::R_MIPS_CALL16: |
+ case elfcpp::R_MIPS16_CALL16: |
+ case elfcpp::R_MIPS_GOT16: |
+ case elfcpp::R_MIPS16_GOT16: |
+ { |
+ Mips_output_data_got<size, big_endian>* got = |
+ target->got_section(symtab, layout); |
+ |
+ std::stringstream tmp; |
+ tmp << static_cast<unsigned int> (reloc.get_r_info()) |
+ << static_cast<unsigned int> (reinterpret_cast<unsigned long long> (object)) |
+ << static_cast<unsigned int> (reloc.get_r_offset()); |
+ std::string key = tmp.str(); |
+ |
+ got->add_got16_view(key, got); |
+ } |
+ break; |
+ case elfcpp::R_MIPS_GOT_DISP: |
+ case elfcpp::R_MIPS_GOT_HI16: |
+ case elfcpp::R_MIPS_CALL_HI16: |
+ case elfcpp::R_MIPS_GOT_LO16: |
+ case elfcpp::R_MIPS_CALL_LO16: |
+ { |
+ // The symbol requires a GOT entry. |
+ Mips_output_data_got<size, big_endian>* got = |
+ target->got_section(symtab, layout); |
+ |
+ unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); |
+ |
+ if (got->add_local(object, r_sym, GOT_TYPE_STANDARD)) |
+ { |
+ // If we are generating a shared object, we need to add a |
+ // dynamic RELATIVE relocation for this symbol's GOT entry. |
+/* if (parameters->options().output_is_position_independent()) |
+ { |
+ Reloc_section* rel_dyn = target->rel_dyn_section(layout); |
+ unsigned int r_sym = elfcpp::elf_r_sym<size>( |
+ reloc.get_r_info()); |
+ rel_dyn->add_local_relative( |
+ object, r_sym, elfcpp::R_MIPS_GOT16, got, |
+ object->local_got_offset(r_sym, GOT_TYPE_STANDARD)); |
+ }*/ |
+ } |
+ } |
+ break; |
+ case elfcpp::R_MIPS_GPREL32: |
+ case elfcpp::R_MIPS_GPREL16: |
+ case elfcpp::R_MIPS16_GPREL: |
+ case elfcpp::R_MIPS_LITERAL: |
+ break; |
+ case elfcpp::R_MIPS_NONE: |
+ case elfcpp::R_MIPS_16: |
+ break; |
+ case elfcpp::R_MIPS_32: |
+ { |
+ if (parameters->options().output_is_position_independent()) |
+ { |
+ Reloc_section* rel_dyn = target->rel_dyn_section(layout); |
+ unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info()); |
+ |
+ rel_dyn->add_symbolless_local_addend(object, r_sym, elfcpp::R_MIPS_REL32, |
+ output_section, data_shndx, |
+ reloc.get_r_offset()); |
+ } |
+ } |
+ case elfcpp::R_MIPS_REL32: |
+ case elfcpp::R_MIPS_26: |
+ case elfcpp::R_MIPS_HI16: |
+ case elfcpp::R_MIPS_LO16: |
+ case elfcpp::R_MIPS_PC16: |
+ case elfcpp::R_MIPS_UNUSED1: |
+ case elfcpp::R_MIPS_UNUSED2: |
+ case elfcpp::R_MIPS_UNUSED3: |
+ case elfcpp::R_MIPS_SHIFT5: |
+ case elfcpp::R_MIPS_SHIFT6: |
+ case elfcpp::R_MIPS_64: |
+ case elfcpp::R_MIPS_GOT_PAGE: |
+ case elfcpp::R_MIPS_GOT_OFST: |
+ case elfcpp::R_MIPS_SUB: |
+ case elfcpp::R_MIPS_INSERT_A: |
+ case elfcpp::R_MIPS_INSERT_B: |
+ case elfcpp::R_MIPS_DELETE: |
+ case elfcpp::R_MIPS_HIGHER: |
+ case elfcpp::R_MIPS_HIGHEST: |
+ case elfcpp::R_MIPS_SCN_DISP: |
+ case elfcpp::R_MIPS_REL16: |
+ case elfcpp::R_MIPS_ADD_IMMEDIATE: |
+ case elfcpp::R_MIPS_PJUMP: |
+ case elfcpp::R_MIPS_RELGOT: |
+ case elfcpp::R_MIPS_JALR: |
+ case elfcpp::R_MIPS_GLOB_DAT: |
+ case elfcpp::R_MIPS16_26: |
+ case elfcpp::R_MIPS16_HI16: |
+ case elfcpp::R_MIPS16_LO16: |
+ case elfcpp::R_MIPS_COPY: |
+ case elfcpp::R_MIPS_JUMP_SLOT: |
+ case elfcpp::R_MIPS_PC32: |
+ case elfcpp::R_MIPS_GNU_REL16_S2: |
+ case elfcpp::R_MIPS_GNU_VTINHERIT: |
+ case elfcpp::R_MIPS_GNU_VTENTRY: |
+ break; |
+ |
+ case elfcpp::R_MIPS_TLS_DTPMOD32: |
+ case elfcpp::R_MIPS_TLS_DTPREL32: |
+ case elfcpp::R_MIPS_TLS_DTPMOD64: |
+ case elfcpp::R_MIPS_TLS_DTPREL64: |
+ case elfcpp::R_MIPS_TLS_GD: |
+ case elfcpp::R_MIPS_TLS_LDM: |
+ case elfcpp::R_MIPS_TLS_DTPREL_HI16: |
+ case elfcpp::R_MIPS_TLS_DTPREL_LO16: |
+ case elfcpp::R_MIPS_TLS_GOTTPREL: |
+ case elfcpp::R_MIPS_TLS_TPREL32: |
+ case elfcpp::R_MIPS_TLS_TPREL64: |
+ case elfcpp::R_MIPS_TLS_TPREL_HI16: |
+ case elfcpp::R_MIPS_TLS_TPREL_LO16: |
+ { |
+ unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); |
+ if(object->local_has_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)) |
+ break; |
+ |
+ bool output_is_shared = parameters->options().shared(); |
+ const tls::Tls_optimization optimized_type |
+ = Target_mips<size, big_endian>::optimize_tls_reloc( |
+ !output_is_shared, r_type); |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_TLS_GD: |
+ if (optimized_type == tls::TLSOPT_NONE) |
+ { |
+ // Create a pair of GOT entries for the module index and |
+ // dtv-relative offset. |
+ Mips_output_data_got<size, big_endian>* got |
+ = target->got_section(symtab, layout); |
+ unsigned int shndx = lsym.get_st_shndx(); |
+ bool is_ordinary; |
+ shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); |
+ if (!is_ordinary) |
+ { |
+ object->error(_("local symbol %u has bad shndx %u"), |
+ r_sym, shndx); |
+ break; |
+ } |
+ |
+ got->add_tls_gd_local_reloc(GOT_TYPE_TLS_PAIR, object, r_sym); |
+ } |
+ else |
+ // FIXME: TLS optimization not supported yet. |
+ gold_unreachable(); |
+ break; |
+ |
+ case elfcpp::R_MIPS_TLS_LDM: |
+ if (optimized_type == tls::TLSOPT_NONE) |
+ { |
+ // Create a GOT entry for the module index. |
+ target->got_mod_index_entry(symtab, layout, object); |
+ } |
+ else |
+ // FIXME: TLS optimization not supported yet. |
+ gold_unreachable(); |
+ break; |
+ case elfcpp::R_MIPS_TLS_GOTTPREL: |
+ layout->set_has_static_tls(); |
+ if (optimized_type == tls::TLSOPT_NONE) |
+ { |
+ // Create a GOT entry for the tp-relative offset. |
+ Mips_output_data_got<size, big_endian>* got |
+ = target->got_section(symtab, layout); |
+ if (!parameters->doing_static_link()) |
+ got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET, |
+ target->rel_dyn_section(layout), |
+ size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 : |
+ elfcpp::R_MIPS_TLS_TPREL64); |
+ else if (!object->local_has_got_offset(r_sym, |
+ GOT_TYPE_TLS_OFFSET)) |
+ { |
+ got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET); |
+ |
+ unsigned int got_offset = |
+ object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET); |
+ got->add_static_reloc(got_offset, |
+ size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 : |
+ elfcpp::R_MIPS_TLS_TPREL64, object, |
+ r_sym); |
+ } |
+ } |
+ else |
+ // FIXME: TLS optimization not supported yet. |
+ gold_unreachable(); |
+ break; |
+ |
+ default: // TODO AS: Check if this is correct at all |
+ layout->set_has_static_tls(); |
+ if (output_is_shared) |
+ { |
+ // We need to create a dynamic relocation. |
+ gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION); |
+ Reloc_section* rel_dyn = target->rel_dyn_section(layout); |
+ rel_dyn->add_local(object, r_sym, size == 32 ? |
+ elfcpp::R_MIPS_TLS_TPREL32 : |
+ elfcpp::R_MIPS_TLS_TPREL64, |
+ output_section, data_shndx, |
+ reloc.get_r_offset()); |
+ } |
+ break; |
+ } |
+ } |
+ break; |
+ |
+ default: |
+ unsupported_reloc_local(object, r_type); |
+ break; |
+ } |
+} |
+ |
+template<int size, bool big_endian> |
+void |
+Mips_output_data_got<size, big_endian>::do_write(Output_file* of) |
+{ |
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address; |
+ |
+ // Call parent to write out GOT. |
+ Output_data_got<size, big_endian>::do_write(of); |
+ |
+// Collect views for later fixing values of .got section |
+ if(!this->got16_views_.empty()) |
+ { |
+ const off_t offset = this->offset(); |
+ const section_size_type oview_size = |
+ convert_to_section_size_type(this->data_size()); |
+ unsigned char* const oview = of->get_output_view(offset, oview_size); |
+ |
+ this->got_view_ = oview; |
+ } |
+ |
+ // We are done if there is no fix up. |
+ if (this->static_relocs_.empty()) |
+ return; |
+ |
+ const off_t offset = this->offset(); |
+ const section_size_type oview_size = |
+ convert_to_section_size_type(this->data_size()); |
+ unsigned char* const oview = of->get_output_view(offset, oview_size); |
+ |
+ Output_segment* tls_segment = this->layout_->tls_segment(); |
+ gold_assert(tls_segment != NULL); |
+ |
+ for (size_t i = 0; i < this->static_relocs_.size(); ++i) |
+ { |
+ Static_reloc& reloc(this->static_relocs_[i]); |
+ |
+ Mips_address value; |
+ if (!reloc.symbol_is_global()) |
+ { |
+ Sized_relobj_file<size, big_endian>* object = reloc.relobj(); |
+ const Symbol_value<size>* psymval = |
+ reloc.relobj()->local_symbol(reloc.index()); |
+ |
+ // We are doing static linking. Issue an error and skip this |
+ // relocation if the symbol is undefined or in a discarded_section. |
+ bool is_ordinary; |
+ unsigned int shndx = psymval->input_shndx(&is_ordinary); |
+ if ((shndx == elfcpp::SHN_UNDEF) |
+ || (is_ordinary |
+ && shndx != elfcpp::SHN_UNDEF |
+ && !object->is_section_included(shndx) |
+ && !this->symbol_table_->is_section_folded(object, shndx))) |
+ { |
+ gold_error(_("undefined or discarded local symbol %u from " |
+ " object %s in GOT"), |
+ reloc.index(), reloc.relobj()->name().c_str()); |
+ continue; |
+ } |
+ |
+ value = psymval->value(object, 0); |
+ } |
+ else |
+ { |
+ const Symbol* gsym = reloc.symbol(); |
+ gold_assert(gsym != NULL); |
+ if (gsym->is_forwarder()) |
+ gsym = this->symbol_table_->resolve_forwards(gsym); |
+ |
+ // We are doing static linking. Issue an error and skip this |
+ // relocation if the symbol is undefined or in a discarded_section |
+ // unless it is a weakly_undefined symbol. |
+ if ((gsym->is_defined_in_discarded_section() |
+ || gsym->is_undefined()) |
+ && !gsym->is_weak_undefined()) |
+ { |
+ gold_error(_("undefined or discarded symbol %s in GOT"), |
+ gsym->name()); |
+ continue; |
+ } |
+ |
+ if (!gsym->is_weak_undefined()) |
+ { |
+ const Sized_symbol<size>* sym = |
+ static_cast<const Sized_symbol<size>*>(gsym); |
+ value = sym->value(); |
+ } |
+ else |
+ value = 0; |
+ } |
+ |
+ unsigned got_offset = reloc.got_offset(); |
+ gold_assert(got_offset < oview_size); |
+ |
+ typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset); |
+ Valtype x; |
+ |
+ switch (reloc.r_type()) |
+ { |
+ case elfcpp::R_MIPS_TLS_DTPMOD32: |
+ x = value; |
+ break; |
+ case elfcpp::R_MIPS_TLS_TPREL32: |
+ x = value - elfcpp::TP_OFFSET; |
+ break; |
+ case elfcpp::R_MIPS_TLS_DTPREL32: |
+ x = value - elfcpp::DTP_OFFSET; |
+ break; |
+ default: |
+ gold_unreachable(); |
+ break; |
+ } |
+ |
+ elfcpp::Swap<size, big_endian>::writeval(wv, x); |
+ } |
+ |
+ of->write_output_view(offset, oview_size, oview); |
+} |
+ |
+// Scan a relocation for a global symbol. |
+ |
+template<int size, bool big_endian> |
+inline void |
+Target_mips<size, big_endian>::Scan::global( |
+ Symbol_table* symtab, |
+ Layout* layout, |
+ Target_mips<size, big_endian>* target, |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int data_shndx, |
+ Output_section* output_section, |
+ const elfcpp::Rel<size, big_endian>& reloc, |
+ unsigned int r_type, |
+ Symbol* gsym) |
+{ |
+ // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got |
+ // section. We check here to avoid creating a dynamic reloc against |
+ // _GLOBAL_OFFSET_TABLE_. |
+ if (!target->has_got_section() |
+ && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0) |
+ target->got_section(symtab, layout); |
+ |
+ // TODO AS: Check where to put this code!!!!! |
+ if(!parameters->options().shared()) |
+ if((this->symbol_needs_plt_entry(gsym)) && (!gsym->has_plt_offset())) |
+ { |
+ if (gsym->object() != object) |
+ { |
+ if(parameters->options().output_is_position_independent()) |
+ { |
+ gold_assert(!gsym->has_dynsym_index()); |
+ target->make_mips_stubs_entry(symtab, layout, gsym); |
+ } |
+ else if(gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) |
+ { |
+ target->make_plt_entry(symtab, layout, gsym); |
+ } |
+ } |
+ |
+ // Since this is not a PC-relative relocation, we may be |
+ // taking the address of a function. In that case we need to |
+ // set the entry in the dynamic symbol table to the address of |
+ // the PLT entry. |
+ if(((gsym->is_from_dynobj() && !parameters->options().shared()) || |
+ ((this->get_reference_flags(r_type) == Symbol::ABSOLUTE_REF) |
+ && !(parameters->options().output_is_position_independent()))) |
+ && (gsym->has_plt_offset())) |
+ { |
+ gsym->set_needs_dynsym_value(); |
+ gsym->set_nonvis(elfcpp::STO_MIPS_PLT>>2); |
+ } |
+ } |
+ |
+ if((gsym->has_plt_offset()) |
+ && (parameters->options().output_is_position_independent())) |
+ switch(r_type) |
+ { |
+ case elfcpp::R_MIPS_HI16: |
+ case elfcpp::R_MIPS_LO16: |
+ case elfcpp::R_MIPS_CALL16: |
+ case elfcpp::R_MIPS_JALR: |
+ break; |
+ default: |
+ target->remove_stub_entry(gsym); |
+ break; |
+ } |
+ |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_NONE: |
+ case elfcpp::R_MIPS_UNUSED1: |
+ case elfcpp::R_MIPS_UNUSED2: |
+ case elfcpp::R_MIPS_UNUSED3: |
+ case elfcpp::R_MIPS_INSERT_A: |
+ case elfcpp::R_MIPS_INSERT_B: |
+ case elfcpp::R_MIPS_DELETE: |
+ case elfcpp::R_MIPS_TLS_DTPMOD64: |
+ case elfcpp::R_MIPS_TLS_DTPREL64: |
+ case elfcpp::R_MIPS_REL16: |
+ case elfcpp::R_MIPS_ADD_IMMEDIATE: |
+ case elfcpp::R_MIPS_PJUMP: |
+ case elfcpp::R_MIPS_RELGOT: |
+ case elfcpp::R_MIPS_TLS_TPREL64: |
+ case elfcpp::R_MIPS_GNU_VTINHERIT: |
+ case elfcpp::R_MIPS_GNU_VTENTRY: |
+ break; |
+ case elfcpp::R_MIPS_GOT16: |
+ case elfcpp::R_MIPS16_GOT16: |
+ case elfcpp::R_MIPS_CALL16: |
+ case elfcpp::R_MIPS16_CALL16: |
+ case elfcpp::R_MIPS_GOT_DISP: |
+ case elfcpp::R_MIPS_GOT_HI16: |
+ case elfcpp::R_MIPS_CALL_HI16: |
+ case elfcpp::R_MIPS_GOT_LO16: |
+ case elfcpp::R_MIPS_CALL_LO16: |
+ case elfcpp::R_MIPS_GOT_PAGE: |
+ case elfcpp::R_MIPS_GOT_OFST: |
+ { |
+ // The symbol requires a GOT entry. |
+ Mips_output_data_got<size, big_endian>* got = |
+ target->got_section(symtab, layout); |
+ if(!gsym->final_value_is_known() |
+ || (parameters->options().output_is_position_independent())) |
+ got->add_global_with_reloc_mips(gsym); |
+ else |
+ { |
+ got->add_global(gsym, GOT_TYPE_STANDARD); |
+ gsym->set_needs_dynsym_entry(); |
+ } |
+ |
+ } |
+ break; |
+ case elfcpp::R_MIPS_LITERAL: |
+ gold_error(_("%s:R_MIPS_LITERAL is defined only for local symbols"), |
+ object->name().c_str()); |
+ break; |
+ case elfcpp::R_MIPS_GPREL32: |
+ gold_error(_("%s:R_MIPS_GPREL32 not defined for global symbols"), |
+ object->name().c_str()); |
+ case elfcpp::R_MIPS_GPREL16: |
+ case elfcpp::R_MIPS16_GPREL: |
+ case elfcpp::R_MIPS16_26: |
+ break; |
+ case elfcpp::R_MIPS_26: |
+ case elfcpp::R_MIPS_PC16: |
+ { |
+ const unsigned char* pehdr = gsym->object()->get_view( |
+ elfcpp::file_header_offset, |
+ elfcpp::Elf_sizes<size>::ehdr_size, |
+ true, false); |
+ |
+ elfcpp::Ehdr<size, big_endian> ehdr(pehdr); |
+ |
+ if(((ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0) |
+ && (gsym->is_defined_in_discarded_section() == false) |
+ && (gsym->is_func()) |
+ && (!gsym->has_plt_offset()) |
+ && (gsym->object() != object)) |
+ { |
+ bool is_ordinary = false; |
+ |
+ target->make_stub_entry(layout); |
+ target->set_stub_flag(); |
+ |
+ // If the function is at the beginning of the section and if we |
+ // would need no more than 2 nops. |
+ // TODO AS: For now we only support stubs of second type |
+ if((((Sized_symbol<size> *)gsym)->value() == 0) |
+ &&(gsym->object()->section_addralign(gsym->shndx(&is_ordinary)) |
+ <= 16)) |
+ { |
+ if(target->stub_symbol(gsym->name()) == NULL) |
+ target->stub_->add_entry(symtab, target, gsym); |
+ } |
+ else |
+ { |
+ if(target->stub_symbol(gsym->name()) == NULL) |
+ target->stub_->add_entry(symtab, target, gsym); |
+ } |
+ } |
+ break; |
+ } |
+ case elfcpp::R_MIPS_16: |
+ case elfcpp::R_MIPS_SHIFT5: |
+ case elfcpp::R_MIPS_SHIFT6: |
+ case elfcpp::R_MIPS_SUB: |
+ case elfcpp::R_MIPS_HIGHER: |
+ case elfcpp::R_MIPS_HIGHEST: |
+ case elfcpp::R_MIPS_SCN_DISP: |
+ case elfcpp::R_MIPS16_HI16: |
+ case elfcpp::R_MIPS16_LO16: |
+ case elfcpp::R_MIPS_GNU_REL16_S2: |
+ case elfcpp::R_MIPS_PC32: |
+ break; |
+ case elfcpp::R_MIPS_HI16: |
+ case elfcpp::R_MIPS_LO16: |
+ case elfcpp::R_MIPS_32: |
+ case elfcpp::R_MIPS_64: |
+ // Absolute addressing relocations. |
+ { |
+ // Make a dynamic relocation if necessary. |
+ if(gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) |
+ { |
+ if(gsym->may_need_copy_reloc() && gsym->in_dyn() |
+ && !((r_type == elfcpp::R_MIPS_32) || (r_type == elfcpp::R_MIPS_64))) |
+ { |
+ target->copy_reloc(symtab, layout, object, |
+ data_shndx, output_section, gsym, reloc); |
+ } |
+ else if((r_type == elfcpp::R_MIPS_32) || (r_type == elfcpp::R_MIPS_64)) |
+ { |
+ Reloc_section* rel_dyn = target->rel_dyn_section(layout); |
+ Mips_output_data_got<size, big_endian>* |
+ got = target->got_section(symtab, layout); |
+ |
+ if(!gsym->final_value_is_known() |
+ || (parameters->options().output_is_position_independent())) |
+ got->add_global_with_reloc_mips(gsym); |
+ |
+ rel_dyn->add_global(gsym, elfcpp::R_MIPS_REL32, output_section, object, |
+ data_shndx, reloc.get_r_offset()); |
+ } |
+ } |
+ } |
+ break; |
+ case elfcpp::R_MIPS_TLS_DTPREL32: |
+ case elfcpp::R_MIPS_TLS_GD: |
+ case elfcpp::R_MIPS_TLS_LDM: |
+ case elfcpp::R_MIPS_TLS_DTPREL_HI16: |
+ case elfcpp::R_MIPS_TLS_DTPREL_LO16: |
+ case elfcpp::R_MIPS_TLS_GOTTPREL: |
+ case elfcpp::R_MIPS_TLS_TPREL32: |
+ case elfcpp::R_MIPS_TLS_TPREL_HI16: |
+ case elfcpp::R_MIPS_TLS_TPREL_LO16: |
+ { |
+ if(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET)) |
+ break; |
+ |
+ const bool is_final = gsym->final_value_is_known(); |
+ const tls::Tls_optimization optimized_type = |
+ Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type); |
+ //unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); |
+ |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_TLS_GD: |
+ if (optimized_type == tls::TLSOPT_NONE) |
+ { |
+ // Create a pair of GOT entries for the module index and |
+ // dtv-relative offset. |
+ Mips_output_data_got<size, big_endian>* got |
+ = target->got_section(symtab, layout); |
+ if (!parameters->doing_static_link()) |
+ got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR, |
+ target->rel_dyn_section(layout), |
+ size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32 : |
+ elfcpp::R_MIPS_TLS_DTPMOD64, |
+ size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32 : |
+ elfcpp::R_MIPS_TLS_DTPREL64); |
+ else |
+ got->add_tls_gd_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym); |
+ } |
+ else |
+ // FIXME: TLS optimization not supported yet. |
+ gold_unreachable(); |
+ break; |
+ |
+ case elfcpp::R_MIPS_TLS_LDM: |
+ if (optimized_type == tls::TLSOPT_NONE) |
+ // Create a GOT entry for the module index. |
+ target->got_mod_index_entry(symtab, layout, object); |
+ else |
+ // FIXME: TLS optimization not supported yet. |
+ gold_unreachable(); |
+ break; |
+ case elfcpp::R_MIPS_TLS_GOTTPREL: |
+ layout->set_has_static_tls(); |
+ if (optimized_type == tls::TLSOPT_NONE) |
+ { |
+ // Create a GOT entry for the tp-relative offset. |
+ Mips_output_data_got<size, big_endian>* got |
+ = target->got_section(symtab, layout); |
+ if (!parameters->doing_static_link()) |
+ got->add_global_tls_with_reloc_mips(gsym, target->rel_dyn_section(layout), |
+ elfcpp::R_MIPS_TLS_TPREL32); |
+ else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET)) |
+ { |
+ got->add_global(gsym, GOT_TYPE_TLS_OFFSET); |
+ unsigned int got_offset = |
+ gsym->got_offset(GOT_TYPE_TLS_OFFSET); |
+ got->add_static_reloc(got_offset, |
+ size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 : |
+ elfcpp::R_MIPS_TLS_TPREL64, gsym); |
+ } |
+ } |
+ else |
+ // FIXME: TLS optimization not supported yet. |
+ gold_unreachable(); |
+ break; |
+ |
+ default: |
+ layout->set_has_static_tls(); |
+ if (parameters->options().shared()) |
+ { |
+ // We need to create a dynamic relocation. |
+ Reloc_section* rel_dyn = target->rel_dyn_section(layout); |
+ rel_dyn->add_global(gsym, |
+ size == 32 ? elfcpp::R_MIPS_TLS_TPREL32 : |
+ elfcpp::R_MIPS_TLS_TPREL64, |
+ output_section, object, |
+ data_shndx, reloc.get_r_offset()); |
+ } |
+ break; |
+ } |
+ } |
+ break; |
+ case elfcpp::R_MIPS_REL32: |
+ // Relative addressing relocations. |
+ { |
+ // Make a dynamic relocation if necessary. |
+ if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) |
+ { |
+ if(gsym->may_need_copy_reloc()) |
+ target->copy_reloc(symtab, layout, object, |
+ data_shndx, output_section, gsym, reloc); |
+ else |
+ { |
+ check_non_pic(object, r_type); |
+ Reloc_section* rel_dyn = target->rel_dyn_section(layout); |
+ rel_dyn->add_global(gsym, r_type, output_section, object, |
+ data_shndx, reloc.get_r_offset()); |
+ } |
+ } |
+ } |
+ break; |
+ case elfcpp::R_MIPS_JALR: |
+ break; |
+ case elfcpp::R_MIPS_COPY: |
+ case elfcpp::R_MIPS_GLOB_DAT: |
+ case elfcpp::R_MIPS_JUMP_SLOT: |
+ // These are relocations which should only be seen by the |
+ // dynamic linker, and should never be seen here. |
+ gold_error(_("%s: unexpected reloc %u in object file"), |
+ object->name().c_str(), r_type); |
+ break; |
+ |
+ default: |
+ unsupported_reloc_global(object, r_type, gsym); |
+ break; |
+ } |
+} |
+ |
+// Perform a relocation. |
+ |
+template<int size, bool big_endian> |
+inline bool |
+Target_mips<size, big_endian>::Relocate::relocate( |
+ const Relocate_info<size, big_endian>* relinfo, |
+ Target_mips* target, |
+ Output_section* output_section, |
+ size_t relnum, |
+ const elfcpp::Rel<size, big_endian>& rel, |
+ unsigned int r_type, |
+ const Sized_symbol<size>* gsym, |
+ const Symbol_value<size>* psymval, |
+ unsigned char* view, |
+ typename elfcpp::Elf_types<size>::Elf_Addr address, |
+ section_size_type) |
+{ |
+ typedef Mips_relocate_functions<size, big_endian> Reloc_funcs; |
+ typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY; |
+ |
+ unsigned int got_offset = 0; |
+ const Sized_relobj_file<size, big_endian>* object = relinfo->object; |
+ |
+ Symbol_value<size> symval; |
+ |
+ // __gnu_local_gp is _gp symbol, just swich it |
+ if(gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0)) |
+ { |
+ gsym = target->gp(); |
+ |
+ gold_assert(gsym != NULL); |
+ |
+ symval.set_output_value(target->gp_address()); |
+ |
+ psymval = &symval; |
+ } |
+ else if(gsym && (strcmp(gsym->name(), "_gp_disp") == 0)) |
+ { |
+ if(r_type != elfcpp::R_MIPS_HI16 && r_type != elfcpp::R_MIPS16_HI16 && |
+ r_type != elfcpp::R_MIPS_LO16 && r_type != elfcpp::R_MIPS16_LO16) |
+ gold_error_at_location(relinfo, relnum, rel.get_r_offset(), |
+ _("relocations against _gp_disp are permitted only" |
+ " with R_MIPS_HI16 and R_MIPS_LO16 relocations.")); |
+ |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Valtype addend = (elfcpp::Swap<32, big_endian>::readval(wv)) & 0xffffU; |
+ Valtype value; |
+ |
+ if (r_type == elfcpp::R_MIPS16_HI16) |
+ value = addend + target->gp_address() - address - 4; |
+ else if(r_type == elfcpp::R_MIPS_HI16) |
+ value = addend + target->gp_address() - address; |
+ else if (r_type == elfcpp::R_MIPS16_LO16) |
+ value = addend + target->gp_address() - address; |
+ else if(r_type == elfcpp::R_MIPS_LO16) |
+ value = addend + target->gp_address() - address + 4; |
+ else |
+ gold_unreachable(); |
+ |
+ symval.set_output_value(value); |
+ |
+ psymval = &symval; |
+ } |
+ |
+ // Get the GOT offset. |
+ // The GOT pointer points to the end of the GOT section. |
+ // We need to subtract the size of the GOT section to get |
+ // the actual offset to use in the relocation. |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_GOT_DISP: |
+ case elfcpp::R_MIPS_GOT_HI16: |
+ case elfcpp::R_MIPS_CALL_HI16: |
+ case elfcpp::R_MIPS_GOT_LO16: |
+ case elfcpp::R_MIPS_CALL_LO16: |
+ if(gsym != NULL) |
+ { |
+ gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); |
+ got_offset = (gsym->got_offset(GOT_TYPE_STANDARD) |
+ - target->got_size()); |
+ } |
+ else |
+ { |
+ unsigned int r_sym = elfcpp::elf_r_sym<size>(rel.get_r_info()); |
+ gold_assert(relinfo->object->local_has_got_offset |
+ (r_sym, GOT_TYPE_STANDARD)); |
+ got_offset = (relinfo->object->local_got_offset |
+ (r_sym, GOT_TYPE_STANDARD) - target->got_size()); |
+ } |
+ break; |
+ case elfcpp::R_MIPS_TLS_GD: |
+ if(gsym != NULL) |
+ { |
+ got_offset = target->got_section()->address() + |
+ gsym->got_offset(GOT_TYPE_TLS_PAIR) - |
+ target->gp_address(); |
+ } |
+ else |
+ { |
+ unsigned int r_sym = elfcpp::elf_r_sym<size>(rel.get_r_info()); |
+ gold_assert(relinfo->object->local_has_got_offset |
+ (r_sym, GOT_TYPE_TLS_PAIR)); |
+ got_offset = target->got_section()->address() + |
+ relinfo->object->local_got_offset(r_sym, GOT_TYPE_TLS_PAIR) - |
+ target->gp_address(); |
+ } |
+ break; |
+ case elfcpp::R_MIPS_TLS_GOTTPREL: |
+ if(gsym != NULL) |
+ { |
+ got_offset = target->got_section()->address() + |
+ gsym->got_offset(GOT_TYPE_TLS_OFFSET) - |
+ target->gp_address(); |
+ } |
+ else |
+ { |
+ unsigned int r_sym = elfcpp::elf_r_sym<size>(rel.get_r_info()); |
+ gold_assert(relinfo->object->local_has_got_offset |
+ (r_sym, GOT_TYPE_TLS_OFFSET)); |
+ got_offset = target->got_section()->address() + |
+ relinfo->object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET) - |
+ target->gp_address(); |
+ } |
+ break; |
+ case elfcpp::R_MIPS_TLS_LDM: |
+ // Relocate the field with the offset of the GOT entry for |
+ // the module index. |
+ got_offset = target->got_section()->address() + |
+ (target->got_mod_index_entry(NULL, NULL, NULL)) - |
+ target->gp_address(); |
+ break; |
+ case elfcpp::R_MIPS_GOT_OFST: |
+ if((gsym != NULL) && (!gsym->is_forced_local())) |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Reltype addend = elfcpp::Swap<32, big_endian>::readval(wv); |
+ addend &= 0x0000ffffU; |
+ |
+ got_offset = addend; |
+ break; |
+ } |
+ case elfcpp::R_MIPS_GOT_PAGE: |
+ { |
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype; |
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype; |
+ Valtype* wv = reinterpret_cast<Valtype*>(view); |
+ Reltype addend = elfcpp::Swap<32, big_endian>::readval(wv); |
+ addend &= 0x0000ffffU; |
+ got_offset = target->got_section()->add_constant(psymval->value(object, |
+ addend) + 0x00008000U) & ~0x0000ffffU; |
+ } |
+ break; |
+ default: |
+ break; |
+ } |
+ |
+ // Pick the value to use for symbols defined in shared objects. |
+ if (gsym != NULL |
+ && gsym->use_plt_offset(Scan::get_reference_flags(r_type))) |
+ { |
+ elfcpp::Elf_Xword value; |
+ |
+ // To avoid problems with symbols that are no longer in .MIPS.stubs |
+ if(gsym->plt_offset() > 0) |
+ { |
+ if(target->is_plt_symbol((Sized_symbol<size> *)gsym)) |
+ value = target->plt_section()->address() + gsym->plt_offset(); |
+ else |
+ value = target->mips_stubs()->address() + |
+ gsym->plt_offset() * (target->mips_stubs()->big_dyn_symtab() ? 20 : 16); |
+ |
+ symval.set_output_value(value); |
+ |
+ psymval = &symval; |
+ } |
+ } |
+ |
+ // Set symbol for use with stub |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_26: |
+ case elfcpp::R_MIPS_PC16: |
+ case elfcpp::R_MIPS16_26: |
+ { |
+ if(target->stub_flag()) |
+ { |
+ if(gsym == NULL) |
+ break; |
+ |
+ Sized_symbol<size> * sym = static_cast<Sized_symbol<size>*> |
+ (target->stub_symbol(gsym->name())); |
+ if(sym != NULL) |
+ { |
+ symval.set_output_value(sym->value()); |
+ psymval = &symval; |
+ } |
+ } |
+ } |
+ break; |
+ default: |
+ break; |
+ } |
+ |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_NONE: |
+ case elfcpp::R_MIPS_GNU_VTINHERIT: |
+ case elfcpp::R_MIPS_GNU_VTENTRY: |
+ break; |
+ case elfcpp::R_MIPS_16: |
+ reloc_status = Reloc_funcs::rel16(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_32: |
+ if((gsym != NULL) |
+ && ((parameters->options().shared()) |
+ || (parameters->options().pie()))) |
+ break; |
+ |
+ reloc_status = Reloc_funcs::rel32(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_REL32: |
+ if(parameters->options().shared() && (gsym != NULL)) |
+ break; |
+ case elfcpp::R_MIPS_PC32: |
+ reloc_status = Reloc_funcs::relrel32(view, object, psymval, address); |
+ break; |
+ case elfcpp::R_MIPS_26: |
+ // TODO AS: Check if this is correct. |
+ if(gsym != NULL) |
+ reloc_status = Reloc_funcs::rel26(view, object, psymval, address, |
+ gsym->is_forced_local()); |
+ else |
+ reloc_status = Reloc_funcs::rel26(view, object, psymval, address, |
+ false); |
+ break; |
+ case elfcpp::R_MIPS_HI16: |
+ case elfcpp::R_MIPS16_HI16: |
+ reloc_status = Reloc_funcs::relhi16(view, psymval->value(object, 0)); |
+ break; |
+ case elfcpp::R_MIPS_LO16: |
+ case elfcpp::R_MIPS16_LO16: |
+ reloc_status = Reloc_funcs::rello16(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_GPREL16: |
+ case elfcpp::R_MIPS16_GPREL: |
+ case elfcpp::R_MIPS_LITERAL: |
+ reloc_status = Reloc_funcs::relgprel(view, object, psymval, |
+ target->gp_address()); |
+ break; |
+ case elfcpp::R_MIPS_GOT16: |
+ case elfcpp::R_MIPS_CALL16: |
+ case elfcpp::R_MIPS16_GOT16: |
+ case elfcpp::R_MIPS16_CALL16: |
+ { |
+ typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype; |
+ Mips_output_data_got<size, big_endian>* got = target->got_section(); |
+ Valtype got_offset; |
+ |
+ if (gsym == NULL) |
+ { |
+ std::stringstream tmp; |
+ tmp << static_cast<unsigned int> (rel.get_r_info()) |
+ << static_cast<unsigned int> (reinterpret_cast<unsigned long long> (object)) |
+ << static_cast<unsigned int> (rel.get_r_offset()); |
+ std::string key = tmp.str(); |
+ |
+ got_offset = got->got16_offset(key); |
+ } |
+ else |
+ got_offset = gsym->got_offset(GOT_TYPE_STANDARD); |
+ |
+ unsigned char* got_view = got_offset + |
+ (unsigned char* const)got->got_view(); |
+ |
+ reloc_status = Reloc_funcs::relgot16(view, object, psymval, |
+ target->gp_address(), got_view, |
+ got->address(), got_offset, |
+ gsym); |
+ } |
+ break; |
+ case elfcpp::R_MIPS_PC16: |
+ case elfcpp::R_MIPS_GNU_REL16_S2: |
+ reloc_status = Reloc_funcs::relpc16(view, object, psymval, address); |
+ break; |
+ case elfcpp::R_MIPS_GPREL32: |
+ reloc_status = Reloc_funcs::relgprel32(view, object, psymval, |
+ target->gp_address()); |
+ break; |
+ case elfcpp::R_MIPS_UNUSED1: |
+ case elfcpp::R_MIPS_UNUSED2: |
+ case elfcpp::R_MIPS_UNUSED3: |
+ break; |
+ case elfcpp::R_MIPS_SHIFT5: |
+ reloc_status = Reloc_funcs::relsh5(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_SHIFT6: |
+ reloc_status = Reloc_funcs::relsh6(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_64: |
+ if(parameters->options().shared() && (gsym != NULL)) |
+ break; |
+ |
+ reloc_status = Reloc_funcs::rel64(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_GOT_HI16: |
+ case elfcpp::R_MIPS_CALL_HI16: |
+ got_offset = ((got_offset + 0x8000) >> 16); |
+ case elfcpp::R_MIPS_GOT_LO16: |
+ case elfcpp::R_MIPS_CALL_LO16: |
+ case elfcpp::R_MIPS_GOT_OFST: |
+ case elfcpp::R_MIPS_GOT_PAGE: |
+ case elfcpp::R_MIPS_GOT_DISP: |
+ case elfcpp::R_MIPS_TLS_GOTTPREL: |
+ case elfcpp::R_MIPS_TLS_GD: |
+ case elfcpp::R_MIPS_TLS_LDM: |
+ reloc_status = Reloc_funcs::relgotofst(view, got_offset); |
+ break; |
+ case elfcpp::R_MIPS_SUB: |
+ reloc_status = Reloc_funcs::relsub(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_INSERT_A: |
+ case elfcpp::R_MIPS_INSERT_B: |
+ case elfcpp::R_MIPS_DELETE: |
+ break; |
+ case elfcpp::R_MIPS_HIGHER: |
+ reloc_status = Reloc_funcs::relhigher(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_HIGHEST: |
+ reloc_status = Reloc_funcs::relhighest(view, object, psymval); |
+ break; |
+ case elfcpp::R_MIPS_SCN_DISP: |
+ reloc_status = Reloc_funcs::relscndisp(view, object, psymval, |
+ output_section->offset()); |
+ break; |
+ case elfcpp::R_MIPS_REL16: |
+ case elfcpp::R_MIPS_ADD_IMMEDIATE: |
+ case elfcpp::R_MIPS_PJUMP: |
+ case elfcpp::R_MIPS_RELGOT: |
+ break; |
+ case elfcpp::R_MIPS_JALR: |
+ if((gsym != NULL) && !(gsym->final_value_is_known())) |
+ break; |
+ |
+ reloc_status = Reloc_funcs::reljalr(view, object, psymval, address); |
+ break; |
+ case elfcpp::R_MIPS_TLS_DTPREL32: |
+ reloc_status = Reloc_funcs::tlsrel32(view, object, psymval, elfcpp::DTP_OFFSET); |
+ break; |
+ case elfcpp::R_MIPS_TLS_DTPREL_LO16: |
+ reloc_status = Reloc_funcs::tlsrello16(view, object, psymval, elfcpp::DTP_OFFSET); |
+ break; |
+ case elfcpp::R_MIPS_TLS_DTPREL_HI16: |
+ reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval, elfcpp::DTP_OFFSET); |
+ break; |
+ case elfcpp::R_MIPS_TLS_TPREL32: |
+ reloc_status = Reloc_funcs::tlsrel32(view, object, psymval, elfcpp::TP_OFFSET); |
+ break; |
+ case elfcpp::R_MIPS_TLS_TPREL_LO16: |
+ reloc_status = Reloc_funcs::tlsrello16(view, object, psymval, elfcpp::TP_OFFSET); |
+ break; |
+ case elfcpp::R_MIPS_TLS_TPREL_HI16: |
+ reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval, elfcpp::TP_OFFSET); |
+ break; |
+ case elfcpp::R_MIPS_TLS_DTPMOD64: |
+ case elfcpp::R_MIPS_TLS_DTPREL64: |
+ case elfcpp::R_MIPS_TLS_TPREL64: |
+ case elfcpp::R_MIPS_TLS_DTPMOD32: // TODO AS! For now this reloc do nothing. |
+ break; |
+ case elfcpp::R_MIPS_GLOB_DAT: |
+ reloc_status = Reloc_funcs::relglob(view, object, psymval); |
+ break; |
+ default: |
+ gold_error_at_location(relinfo, relnum, rel.get_r_offset(), |
+ _("unsupported reloc %u"), r_type); |
+ break; |
+ } |
+ |
+ // Report any errors. |
+ switch (reloc_status) |
+ { |
+ case Reloc_funcs::STATUS_OKAY: |
+ break; |
+ case Reloc_funcs::STATUS_OVERFLOW: |
+ gold_error_at_location(relinfo, relnum, rel.get_r_offset(), |
+ _("relocation overflow")); |
+ break; |
+ case Reloc_funcs::STATUS_BAD_RELOC: |
+ gold_error_at_location( |
+ relinfo, |
+ relnum, |
+ rel.get_r_offset(), |
+ _("unexpected opcode while processing relocation")); |
+ break; |
+ default: |
+ gold_unreachable(); |
+ } |
+ |
+ return true; |
+} |
+ |
+// Get the Reference_flags for a particular relocation. |
+ |
+template<int size, bool big_endian> |
+int |
+Target_mips<size, big_endian>::Scan::get_reference_flags( |
+ unsigned int r_type) |
+{ |
+ switch (r_type) |
+ { |
+ case elfcpp::R_MIPS_NONE: |
+ case elfcpp::R_MIPS_GNU_VTINHERIT: |
+ case elfcpp::R_MIPS_GNU_VTENTRY: |
+ case elfcpp::R_MIPS_INSERT_A: |
+ case elfcpp::R_MIPS_INSERT_B: |
+ case elfcpp::R_MIPS_DELETE: |
+ case elfcpp::R_MIPS_PJUMP: |
+ case elfcpp::R_MIPS_RELGOT: |
+ case elfcpp::R_MIPS_REL16: |
+ case elfcpp::R_MIPS_ADD_IMMEDIATE: |
+ // No symbol reference. |
+ return 0; |
+ |
+ case elfcpp::R_MIPS_16: |
+ case elfcpp::R_MIPS_32: |
+ case elfcpp::R_MIPS_64: |
+ case elfcpp::R_MIPS_HI16: |
+ case elfcpp::R_MIPS_LO16: |
+ case elfcpp::R_MIPS_SHIFT5: |
+ case elfcpp::R_MIPS_SHIFT6: |
+ case elfcpp::R_MIPS_SUB: |
+ case elfcpp::R_MIPS_HIGHER: |
+ case elfcpp::R_MIPS_HIGHEST: |
+ case elfcpp::R_MIPS_SCN_DISP: |
+ case elfcpp::R_MIPS16_HI16: |
+ case elfcpp::R_MIPS16_LO16: |
+ return Symbol::ABSOLUTE_REF; |
+ |
+ case elfcpp::R_MIPS_26: |
+ case elfcpp::R_MIPS16_26: |
+ case elfcpp::R_MIPS_GPREL32: |
+ case elfcpp::R_MIPS_GPREL16: |
+ case elfcpp::R_MIPS16_GPREL: |
+ case elfcpp::R_MIPS_REL32: |
+ return Symbol::RELATIVE_REF; |
+ |
+ case elfcpp::R_MIPS_PC16: |
+ case elfcpp::R_MIPS_PC32: |
+ case elfcpp::R_MIPS_GNU_REL16_S2: |
+ case elfcpp::R_MIPS_JALR: |
+ return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF; |
+ |
+ case elfcpp::R_MIPS_GOT16: |
+ case elfcpp::R_MIPS16_GOT16: |
+ case elfcpp::R_MIPS_CALL16: |
+ case elfcpp::R_MIPS16_CALL16: |
+ case elfcpp::R_MIPS_GOT_DISP: |
+ case elfcpp::R_MIPS_GOT_HI16: |
+ case elfcpp::R_MIPS_CALL_HI16: |
+ case elfcpp::R_MIPS_GOT_LO16: |
+ case elfcpp::R_MIPS_CALL_LO16: |
+ case elfcpp::R_MIPS_LITERAL: |
+ case elfcpp::R_MIPS_GOT_PAGE: |
+ case elfcpp::R_MIPS_GOT_OFST: |
+ // Absolute in GOT. |
+ return Symbol::RELATIVE_REF; |
+ |
+ case elfcpp::R_MIPS_TLS_DTPMOD32: |
+ case elfcpp::R_MIPS_TLS_DTPREL32: |
+ case elfcpp::R_MIPS_TLS_DTPMOD64: |
+ case elfcpp::R_MIPS_TLS_DTPREL64: |
+ case elfcpp::R_MIPS_TLS_GD: |
+ case elfcpp::R_MIPS_TLS_LDM: |
+ case elfcpp::R_MIPS_TLS_DTPREL_HI16: |
+ case elfcpp::R_MIPS_TLS_DTPREL_LO16: |
+ case elfcpp::R_MIPS_TLS_GOTTPREL: |
+ case elfcpp::R_MIPS_TLS_TPREL32: |
+ case elfcpp::R_MIPS_TLS_TPREL64: |
+ case elfcpp::R_MIPS_TLS_TPREL_HI16: |
+ case elfcpp::R_MIPS_TLS_TPREL_LO16: |
+ return Symbol::TLS_REF; |
+ |
+ case elfcpp::R_MIPS_COPY: |
+ case elfcpp::R_MIPS_UNUSED1: |
+ case elfcpp::R_MIPS_UNUSED2: |
+ case elfcpp::R_MIPS_UNUSED3: |
+ case elfcpp::R_MIPS_GLOB_DAT: |
+ case elfcpp::R_MIPS_JUMP_SLOT: |
+ default: |
+ // Not expected. We will give an error later. |
+ return 0; |
+ } |
+} |
+ |
+// Report an unsupported relocation against a local symbol. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::Scan::unsupported_reloc_local( |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int r_type) |
+{ |
+ gold_error(_("%s: unsupported reloc %u against local symbol"), |
+ object->name().c_str(), r_type); |
+} |
+ |
+// Report an unsupported relocation against a global symbol. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::Scan::unsupported_reloc_global( |
+ Sized_relobj_file<size, big_endian>* object, |
+ unsigned int r_type, |
+ Symbol* gsym) |
+{ |
+ gold_error(_("%s: unsupported reloc %u against global symbol %s"), |
+ object->name().c_str(), r_type, gsym->demangled_name().c_str()); |
+} |
+ |
+template<int size, bool big_endian> |
+Target::Target_info Target_mips<size, big_endian>::mips_info = |
+{ |
+ size, // size |
+ big_endian, // is_big_endian |
+ elfcpp::EM_MIPS, // 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 |
+ "/lib/ld.so.1", // dynamic_linker |
+ 0x400000, // default_text_segment_address |
+ 64 * 1024, // abi_pagesize (overridable by -z max-page-size) |
+ 4 * 1024, // common_pagesize (overridable by -z common-page-size) |
+ 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 |
+}; |
+ |
+// Adjust ELF file header. |
+ |
+template<int size, bool big_endian> |
+void |
+Target_mips<size, big_endian>::do_adjust_elf_header( |
+ unsigned char* view, |
+ int len) const |
+{ |
+ gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size); |
+ |
+ elfcpp::Ehdr<size, big_endian> ehdr(view); |
+ unsigned char e_ident[elfcpp::EI_NIDENT]; |
+ memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT); |
+ |
+ e_ident[elfcpp::EI_VERSION] = elfcpp::EV_CURRENT; |
+ e_ident[elfcpp::EI_ABIVERSION] = elfcpp::EV_NONE; |
+ |
+ elfcpp::Ehdr_write<size, big_endian> oehdr(view); |
+ oehdr.put_e_ident(e_ident); |
+ oehdr.put_e_flags(this->processor_specific_flags_); |
+} |
+ |
+// The selector for mips object files. |
+ |
+template<int size, bool big_endian> |
+class Target_selector_mips : public Target_selector |
+{ |
+public: |
+ Target_selector_mips() |
+ : Target_selector(elfcpp::EM_MIPS, size, big_endian, |
+ (size == 64 ? |
+ (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") : |
+ (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")), |
+ (size == 64 ? |
+ (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") : |
+ (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips"))) |
+ { } |
+ |
+ Target* do_instantiate_target() |
+ { return new Target_mips<size, big_endian>(); } |
+}; |
+ |
+Target_selector_mips<32, true> target_selector_mips32be; |
+Target_selector_mips<32, false> target_selector_mips32; |
+Target_selector_mips<64, true> target_selector_mips64be; |
+Target_selector_mips<64, false> target_selector_mips64; |
+ |
+ |
+} // End anonymous namespace. |