This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
Mips target in gold - revision 2 - part 2
- From: Sasa Stankovic <Sasa dot Stankovic at imgtec dot com>
- Cc: "binutils at sourceware dot org" <binutils at sourceware dot org>, "iant at google dot com" <iant at google dot com>, Petar Jovanovic <Petar dot Jovanovic at imgtec dot com>
- Date: Thu, 30 Jan 2014 14:26:07 +0000
- Subject: Mips target in gold - revision 2 - part 2
- Authentication-results: sourceware.org; auth=none
Attached is the second part of the patch that implements Mips target in gold. This part contains first part of the mips.cc file.
Regards,
Sasa
diff --git a/gold/mips.cc b/gold/mips.cc
new file mode 100644
index 0000000..66a4466
--- /dev/null
+++ b/gold/mips.cc
@@ -0,0 +1,5279 @@
+// mips.cc -- mips target support for gold.
+
+// Copyright 2011, 2012, 2013 Free Software Foundation, Inc.
+// Written by Sasa Stankovic <sasa.stankovic@rt-rk.com>
+// and Aleksandar Simeonov <aleksandar.simeonov@rt-rk.com>.
+// This file contains borrowed and adapted code from bfd/elfxx-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 "gold.h"
+
+#include <algorithm>
+#include <set>
+#include <sstream>
+#include "demangle.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 "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, bool big_endian>
+class Mips_output_section_reginfo;
+
+template<int size, bool big_endian>
+class Mips_output_data_la25_stub;
+
+template<int size, bool big_endian>
+class Mips_output_data_mips_stubs;
+
+template<int size>
+class Mips_symbol;
+
+template<int size, bool big_endian>
+class Mips_got_info;
+
+template<int size, bool big_endian>
+class Mips_relobj;
+
+class Mips16_stub_section_base;
+
+template<int size, bool big_endian>
+class Mips16_stub_section;
+
+// The ABI says that every symbol used by dynamic relocations must have
+// a global GOT entry. Among other things, this provides the dynamic
+// linker with a free, directly-indexed cache. The GOT can therefore
+// contain symbols that are not referenced by GOT relocations themselves
+// (in other words, it may have symbols that are not referenced by things
+// like R_MIPS_GOT16 and R_MIPS_GOT_PAGE).
+
+// GOT relocations are less likely to overflow if we put the associated
+// GOT entries towards the beginning. We therefore divide the global
+// GOT entries into two areas: "normal" and "reloc-only". Entries in
+// the first area can be used for both dynamic relocations and GP-relative
+// accesses, while those in the "reloc-only" area are for dynamic
+// relocations only.
+
+// These GGA_* ("Global GOT Area") values are organised so that lower
+// values are more general than higher values. Also, non-GGA_NONE
+// values are ordered by the position of the area in the GOT.
+
+enum Global_got_area
+{
+ GGA_NORMAL = 0,
+ GGA_RELOC_ONLY = 1,
+ GGA_NONE = 2
+};
+
+// 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
+
+ // GOT entries for multi-GOT. We support up to 1024 GOTs in multi-GOT links.
+ GOT_TYPE_STANDARD_MULTIGOT = 3,
+ GOT_TYPE_TLS_OFFSET_MULTIGOT = GOT_TYPE_STANDARD_MULTIGOT + 1024,
+ GOT_TYPE_TLS_PAIR_MULTIGOT = GOT_TYPE_TLS_OFFSET_MULTIGOT + 1024
+};
+
+// TLS type of GOT entry.
+enum Got_tls_type
+{
+ GOT_TLS_NONE = 0,
+ GOT_TLS_GD = 1,
+ GOT_TLS_LDM = 2,
+ GOT_TLS_IE = 4
+};
+
+// This class is used to hold information about one GOT entry.
+// There are three types of entry:
+//
+// (1) a SYMBOL + OFFSET address, where SYMBOL is local to an input object
+// (object != NULL, symndx >= 0, tls_type != GOT_TLS_LDM)
+// (2) a SYMBOL address, where SYMBOL is not local to an input object
+// (object != NULL, symndx == -1)
+// (3) a TLS LDM slot
+// (object != NULL, symndx == 0, tls_type == GOT_TLS_LDM)
+
+template<int size, bool big_endian>
+class Mips_got_entry
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+ public:
+ Mips_got_entry(Mips_relobj<size, big_endian>* object, unsigned int symndx,
+ Mips_address addend, unsigned char tls_type,
+ unsigned int shndx)
+ : object_(object), symndx_(symndx), tls_type_(tls_type), shndx_(shndx)
+ { this->d.addend = addend; }
+
+ Mips_got_entry(Mips_relobj<size, big_endian>* object, Mips_symbol<size>* sym,
+ unsigned char tls_type)
+ : object_(object), symndx_(-1U), tls_type_(tls_type), shndx_(-1U)
+ { this->d.sym = sym; }
+
+ // Return whether this entry is for a local symbol.
+ bool
+ is_for_local_symbol() const
+ { return this->symndx_ != -1U; }
+
+ // Return whether this entry is for a global symbol.
+ bool
+ is_for_global_symbol() const
+ { return this->symndx_ == -1U; }
+
+ // Return the hash of this entry.
+ size_t
+ hash() const
+ {
+ if (this->tls_type_ == GOT_TLS_LDM)
+ return this->symndx_ + (1 << 18);
+ if (this->symndx_ != -1U)
+ {
+ uintptr_t object_id = reinterpret_cast<uintptr_t>(this->object());
+ return this->symndx_ + object_id + this->d.addend;
+ }
+ else
+ {
+ uintptr_t sym_id = reinterpret_cast<uintptr_t>(this->d.sym);
+ return this->symndx_ + sym_id;
+ }
+ }
+
+ // Return whether this entry is equal to OTHER.
+ bool
+ equals(Mips_got_entry<size, big_endian>* other) const
+ {
+ if (this->symndx_ != other->symndx_
+ || this->tls_type_ != other->tls_type_)
+ return false;
+ if (this->tls_type_ == GOT_TLS_LDM)
+ return true;
+ if (this->symndx_ != -1U)
+ return (this->object() == other->object()
+ && this->d.addend == other->d.addend);
+ else
+ return this->d.sym == other->d.sym;
+ }
+
+ // Return input object that needs this GOT entry.
+ Mips_relobj<size, big_endian>*
+ object() const
+ {
+ gold_assert(this->object_ != NULL);
+ return this->object_;
+ }
+
+ // Return local symbol index for local GOT entries.
+ unsigned int
+ symndx() const
+ {
+ gold_assert(this->symndx_ != -1U);
+ return this->symndx_;
+ }
+
+ // Return the relocation addend for local GOT entries.
+ Mips_address
+ addend() const
+ {
+ gold_assert(this->symndx_ != -1U);
+ return this->d.addend;
+ }
+
+ // Return global symbol for global GOT entries.
+ Mips_symbol<size>*
+ sym() const
+ {
+ gold_assert(this->symndx_ == -1U);
+ return this->d.sym;
+ }
+
+ // Return whether this is a TLS GOT entry.
+ bool
+ is_tls_entry() const
+ { return this->tls_type_ != GOT_TLS_NONE; }
+
+ // Return TLS type of this GOT entry.
+ unsigned char
+ tls_type() const
+ { return this->tls_type_; }
+
+ // Return section index of the local symbol for local GOT entries.
+ unsigned int
+ shndx() const
+ { return this->shndx_; }
+
+ private:
+ // The input object that needs the GOT entry.
+ Mips_relobj<size, big_endian>* object_;
+ // The index of the symbol if we have a local symbol; -1 otherwise.
+ unsigned int symndx_;
+
+ union
+ {
+ // If symndx != -1, the addend of the relocation that should be added to the
+ // symbol value.
+ Mips_address addend;
+ // If symndx == -1, the global symbol corresponding to this GOT entry. The
+ // symbol's entry is in the local area if mips_sym->global_got_area is
+ // GGA_NONE, otherwise it is in the global area.
+ Mips_symbol<size>* sym;
+ } d;
+
+ // The TLS type of this GOT entry. An LDM GOT entry will be a local
+ // symbol entry with r_symndx == 0.
+ unsigned char tls_type_;
+
+ // For local GOT entries, section index of the local symbol.
+ unsigned int shndx_;
+};
+
+// Hash for Mips_got_entry.
+
+template<int size, bool big_endian>
+class Mips_got_entry_hash
+{
+ public:
+ size_t
+ operator()(Mips_got_entry<size, big_endian>* entry) const
+ { return entry->hash(); }
+};
+
+// Equality for Mips_got_entry.
+
+template<int size, bool big_endian>
+class Mips_got_entry_eq
+{
+ public:
+ bool
+ operator()(Mips_got_entry<size, big_endian>* e1,
+ Mips_got_entry<size, big_endian>* e2) const
+ { return e1->equals(e2); }
+};
+
+// Got_page_range. This class describes a range of addends: [MIN_ADDEND,
+// MAX_ADDEND]. The instances form a non-overlapping list that is sorted by
+// increasing MIN_ADDEND.
+
+struct Got_page_range
+{
+ Got_page_range()
+ : next(NULL), min_addend(0), max_addend(0)
+ { }
+
+ Got_page_range* next;
+ int min_addend;
+ int max_addend;
+
+ // Return the maximum number of GOT page entries required.
+ int
+ get_max_pages()
+ { return (this->max_addend - this->min_addend + 0x1ffff) >> 16; }
+};
+
+// Got_page_entry. This class describes the range of addends that are applied
+// to page relocations against a given symbol.
+
+struct Got_page_entry
+{
+ Got_page_entry()
+ : object(NULL), symndx(-1U), ranges(NULL), num_pages(0)
+ { }
+
+ Got_page_entry(Object* object_, unsigned int symndx_)
+ : object(object_), symndx(symndx_), ranges(NULL), num_pages(0)
+ { }
+
+ // The input object that needs the GOT page entry.
+ Object* object;
+ // The index of the symbol, as stored in the relocation r_info.
+ unsigned int symndx;
+ // The ranges for this page entry.
+ Got_page_range* ranges;
+ // The maximum number of page entries needed for RANGES.
+ unsigned int num_pages;
+};
+
+// Hash for Got_page_entry.
+
+struct Got_page_entry_hash
+{
+ size_t
+ operator()(Got_page_entry* entry) const
+ { return reinterpret_cast<uintptr_t>(entry->object) + entry->symndx; }
+};
+
+// Equality for Got_page_entry.
+
+struct Got_page_entry_eq
+{
+ bool
+ operator()(Got_page_entry* entry1, Got_page_entry* entry2) const
+ {
+ return entry1->object == entry2->object && entry1->symndx == entry2->symndx;
+ }
+};
+
+// This class is used to hold .got information when linking.
+
+template<int size, bool big_endian>
+class Mips_got_info
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+ typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
+ Reloc_section;
+ typedef Unordered_map<unsigned int, unsigned int> Got_page_offsets;
+
+ // Unordered set of GOT entries.
+ typedef Unordered_set<Mips_got_entry<size, big_endian>*,
+ Mips_got_entry_hash<size, big_endian>,
+ Mips_got_entry_eq<size, big_endian> > Got_entry_set;
+
+ // Unordered set of GOT page entries.
+ typedef Unordered_set<Got_page_entry*,
+ Got_page_entry_hash, Got_page_entry_eq> Got_page_entry_set;
+
+ public:
+ Mips_got_info()
+ : local_gotno_(0), page_gotno_(0), global_gotno_(0), reloc_only_gotno_(0),
+ tls_gotno_(0), tls_ldm_offset_(-1U), global_got_symbols_(),
+ got_entries_(), got_page_entries_(), got_page_offset_start_(0),
+ got_page_offset_next_(0), got_page_offsets_(), next_(NULL), index_(-1U),
+ offset_(0)
+ { }
+
+ // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
+ // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
+ void
+ record_local_got_symbol(Mips_relobj<size, big_endian>* object,
+ unsigned int symndx, Mips_address addend,
+ unsigned int r_type, unsigned int shndx);
+
+ // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
+ // in OBJECT. FOR_CALL is true if the caller is only interested in
+ // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
+ // relocation.
+ void
+ record_global_got_symbol(Mips_symbol<size>* mips_sym,
+ Mips_relobj<size, big_endian>* object,
+ unsigned int r_type, bool dyn_reloc, bool for_call);
+
+ // Add ENTRY to master GOT and to OBJECT's GOT.
+ void
+ record_got_entry(Mips_got_entry<size, big_endian>* entry,
+ Mips_relobj<size, big_endian>* object);
+
+ // Record that OBJECT has a page relocation against symbol SYMNDX and
+ // that ADDEND is the addend for that relocation.
+ void
+ record_got_page_entry(Mips_relobj<size, big_endian>* object,
+ unsigned int symndx, int addend);
+
+ // Create all entries that should be in the local part of the GOT.
+ void
+ add_local_entries(Target_mips<size, big_endian>* target, Layout* layout);
+
+ // Create GOT page entries.
+ void
+ add_page_entries(Target_mips<size, big_endian>* target, Layout* layout);
+
+ // Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
+ void
+ add_global_entries(Target_mips<size, big_endian>* target, Layout* layout,
+ unsigned int non_reloc_only_global_gotno);
+
+ // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
+ void
+ add_reloc_only_entries(Mips_output_data_got<size, big_endian>* got);
+
+ // Create TLS GOT entries.
+ void
+ add_tls_entries(Target_mips<size, big_endian>* target, Layout* layout);
+
+ // Decide whether the symbol needs an entry in the global part of the primary
+ // GOT, setting global_got_area accordingly. Count the number of global
+ // symbols that are in the primary GOT only because they have dynamic
+ // relocations R_MIPS_REL32 against them (reloc_only_gotno).
+ void
+ count_got_symbols(Symbol_table* symtab);
+
+ // Return the offset of GOT page entry for VALUE.
+ unsigned int
+ get_got_page_offset(Mips_address value,
+ Mips_output_data_got<size, big_endian>* got);
+
+ // Count the number of GOT entries required.
+ void
+ count_got_entries();
+
+ // Count the number of GOT entries required by ENTRY. Accumulate the result.
+ void
+ count_got_entry(Mips_got_entry<size, big_endian>* entry);
+
+ // Add FROM's GOT entries.
+ void
+ add_got_entries(Mips_got_info<size, big_endian>* from);
+
+ // Add FROM's GOT page entries.
+ void
+ add_got_page_entries(Mips_got_info<size, big_endian>* from);
+
+ // Return GOT size.
+ unsigned int
+ got_size() const
+ { return ((2 + this->local_gotno_ + this->page_gotno_ + this->global_gotno_
+ + this->tls_gotno_) * size/8);
+ }
+
+ // Return the number of local GOT entries.
+ unsigned int
+ local_gotno() const
+ { return this->local_gotno_; }
+
+ // Return the maximum number of page GOT entries needed.
+ unsigned int
+ page_gotno() const
+ { return this->page_gotno_; }
+
+ // Return the number of global GOT entries.
+ unsigned int
+ global_gotno() const
+ { return this->global_gotno_; }
+
+ // Set the number of global GOT entries.
+ void
+ set_global_gotno(unsigned int global_gotno)
+ { this->global_gotno_ = global_gotno; }
+
+ // Return the number of GGA_RELOC_ONLY global GOT entries.
+ unsigned int
+ reloc_only_gotno() const
+ { return this->reloc_only_gotno_; }
+
+ // Return the number of TLS GOT entries.
+ unsigned int
+ tls_gotno() const
+ { return this->tls_gotno_; }
+
+ // Return the GOT type for this GOT. Used for multi-GOT links only.
+ unsigned int
+ multigot_got_type(unsigned int got_type) const
+ {
+ switch (got_type)
+ {
+ case GOT_TYPE_STANDARD:
+ return GOT_TYPE_STANDARD_MULTIGOT + this->index_;
+ case GOT_TYPE_TLS_OFFSET:
+ return GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
+ case GOT_TYPE_TLS_PAIR:
+ return GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
+ default:
+ gold_unreachable();
+ }
+ }
+
+ // Remove lazy-binding stubs for global symbols in this GOT.
+ void
+ remove_lazy_stubs(Target_mips<size, big_endian>* target);
+
+ // Return offset of this GOT from the start of .got section.
+ unsigned int
+ offset() const
+ { return this->offset_; }
+
+ // Set offset of this GOT from the start of .got section.
+ void
+ set_offset(unsigned int offset)
+ { this->offset_ = offset; }
+
+ // Set index of this GOT in multi-GOT links.
+ void
+ set_index(unsigned int index)
+ { this->index_ = index; }
+
+ // Return next GOT in multi-GOT links.
+ Mips_got_info<size, big_endian>*
+ next() const
+ { return this->next_; }
+
+ // Set next GOT in multi-GOT links.
+ void
+ set_next(Mips_got_info<size, big_endian>* next)
+ { this->next_ = next; }
+
+ // Return the offset of TLS LDM entry for this GOT.
+ unsigned int
+ tls_ldm_offset() const
+ { return this->tls_ldm_offset_; }
+
+ // Set the offset of TLS LDM entry for this GOT.
+ void
+ set_tls_ldm_offset(unsigned int tls_ldm_offset)
+ { this->tls_ldm_offset_ = tls_ldm_offset; }
+
+ Unordered_set<Mips_symbol<size>*>&
+ global_got_symbols()
+ { return this->global_got_symbols_; }
+
+ // Return the GOT_TLS_* type required by relocation type R_TYPE.
+ static int
+ mips_elf_reloc_tls_type(unsigned int r_type)
+ {
+ if (Target_mips<size, big_endian>::tls_gd_reloc(r_type))
+ return GOT_TLS_GD;
+
+ if (Target_mips<size, big_endian>::tls_ldm_reloc(r_type))
+ return GOT_TLS_LDM;
+
+ if (Target_mips<size, big_endian>::tls_gottprel_reloc(r_type))
+ return GOT_TLS_IE;
+
+ return GOT_TLS_NONE;
+ }
+
+ // Return the number of GOT slots needed for GOT TLS type TYPE.
+ static int
+ mips_tls_got_entries(unsigned int type)
+ {
+ switch (type)
+ {
+ case GOT_TLS_GD:
+ case GOT_TLS_LDM:
+ return 2;
+
+ case GOT_TLS_IE:
+ return 1;
+
+ case GOT_TLS_NONE:
+ return 0;
+
+ default:
+ gold_unreachable();
+ }
+ }
+
+ private:
+ // The number of local GOT entries.
+ unsigned int local_gotno_;
+ // The maximum number of page GOT entries needed.
+ unsigned int page_gotno_;
+ // The number of global GOT entries.
+ unsigned int global_gotno_;
+ // The number of global GOT entries that are in the GGA_RELOC_ONLY area.
+ unsigned int reloc_only_gotno_;
+ // The number of TLS GOT entries.
+ unsigned int tls_gotno_;
+ // The offset of TLS LDM entry for this GOT.
+ unsigned int tls_ldm_offset_;
+ // All symbols that have global GOT entry.
+ Unordered_set<Mips_symbol<size>*> global_got_symbols_;
+ // A hash table holding GOT entries.
+ Got_entry_set got_entries_;
+ // A hash table of GOT page entries.
+ Got_page_entry_set got_page_entries_;
+ // The offset of first GOT page entry for this GOT.
+ unsigned int got_page_offset_start_;
+ // The offset of next available GOT page entry for this GOT.
+ unsigned int got_page_offset_next_;
+ // A hash table that maps GOT page entry value to the GOT offset where
+ // the entry is located.
+ Got_page_offsets got_page_offsets_;
+ // In multi-GOT links, a pointer to the next GOT.
+ Mips_got_info<size, big_endian>* next_;
+ // Index of this GOT in multi-GOT links.
+ unsigned int index_;
+ // The offset of this GOT in multi-GOT links.
+ unsigned int offset_;
+};
+
+// This is a helper class used during relocation scan. It records GOT16 addend.
+
+template<int size, bool big_endian>
+struct got16_addend
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+ got16_addend(const Sized_relobj_file<size, big_endian>* _object,
+ unsigned int _shndx, unsigned int _r_type, unsigned int _r_sym,
+ Mips_address _addend)
+ : object(_object), shndx(_shndx), r_type(_r_type), r_sym(_r_sym),
+ addend(_addend)
+ { }
+
+ const Sized_relobj_file<size, big_endian>* object;
+ unsigned int shndx;
+ unsigned int r_type;
+ unsigned int r_sym;
+ Mips_address addend;
+};
+
+// Mips_symbol class. Holds additional symbol information needed for Mips.
+
+template<int size>
+class Mips_symbol : public Sized_symbol<size>
+{
+ public:
+ Mips_symbol()
+ : need_fn_stub_(false), has_nonpic_branches_(false), la25_stub_offset_(-1U),
+ has_static_relocs_(false), no_lazy_stub_(false), lazy_stub_offset_(0),
+ pointer_equality_needed_(false), global_got_area_(GGA_NONE),
+ global_gotoffset_(-1U), got_only_for_calls_(true), has_lazy_stub_(false),
+ needs_mips_plt_(false), needs_comp_plt_(false), mips_plt_offset_(-1U),
+ comp_plt_offset_(-1U), mips16_fn_stub_(NULL), mips16_call_stub_(NULL),
+ mips16_call_fp_stub_(NULL), applied_secondary_got_fixup_(false)
+ { }
+
+ // Return whether this is a MIPS16 symbol.
+ bool
+ is_mips16() const
+ {
+ // (st_other & STO_MIPS16) == STO_MIPS16
+ return ((this->nonvis() & (elfcpp::STO_MIPS16 >> 2))
+ == elfcpp::STO_MIPS16 >> 2);
+ }
+
+ // Return whether this is a microMIPS symbol.
+ bool
+ is_micromips() const
+ {
+ // (st_other & STO_MIPS_ISA) == STO_MICROMIPS
+ return ((this->nonvis() & (elfcpp::STO_MIPS_ISA >> 2))
+ == elfcpp::STO_MICROMIPS >> 2);
+ }
+
+ // Return whether the symbol needs MIPS16 fn_stub.
+ bool
+ need_fn_stub() const
+ { return this->need_fn_stub_; }
+
+ // Set that the symbol needs MIPS16 fn_stub.
+ void
+ set_need_fn_stub()
+ { this->need_fn_stub_ = true; }
+
+ // Return whether this symbol is referenced by branch relocations from
+ // any non-PIC input file.
+ bool
+ has_nonpic_branches() const
+ { return this->has_nonpic_branches_; }
+
+ // Set that this symbol is referenced by branch relocations from
+ // any non-PIC input file.
+ void
+ set_has_nonpic_branches()
+ { this->has_nonpic_branches_ = true; }
+
+ // Return the offset of the la25 stub for this symbol from the start of the
+ // la25 stub section.
+ unsigned int
+ la25_stub_offset() const
+ { return this->la25_stub_offset_; }
+
+ // Set the offset of the la25 stub for this symbol from the start of the
+ // la25 stub section.
+ void
+ set_la25_stub_offset(unsigned int offset)
+ { this->la25_stub_offset_ = offset; }
+
+ // Return whether the symbol has la25 stub. This is true if this symbol is
+ // for a PIC function, and there are non-PIC branches and jumps to it.
+ bool
+ has_la25_stub() const
+ { return this->la25_stub_offset_ != -1U; }
+
+ // Return whether there is a relocation against this symbol that must be
+ // resolved by the static linker (that is, the relocation cannot possibly
+ // be made dynamic).
+ bool
+ has_static_relocs() const
+ { return this->has_static_relocs_; }
+
+ // Set that there is a relocation against this symbol that must be resolved
+ // by the static linker (that is, the relocation cannot possibly be made
+ // dynamic).
+ void
+ set_has_static_relocs()
+ { this->has_static_relocs_ = true; }
+
+ // Return whether we must not create a lazy-binding stub for this symbol.
+ bool
+ no_lazy_stub() const
+ { return this->no_lazy_stub_; }
+
+ // Set that we must not create a lazy-binding stub for this symbol.
+ void
+ set_no_lazy_stub()
+ { this->no_lazy_stub_ = true; }
+
+ // Return the offset of the lazy-binding stub for this symbol from the start
+ // of .MIPS.stubs section.
+ unsigned int
+ lazy_stub_offset() const
+ { return this->lazy_stub_offset_; }
+
+ // Set the offset of the lazy-binding stub for this symbol from the start
+ // of .MIPS.stubs section.
+ void
+ set_lazy_stub_offset(unsigned int offset)
+ { this->lazy_stub_offset_ = offset; }
+
+ // Return whether there are any relocations for this symbol where
+ // pointer equality matters.
+ bool
+ pointer_equality_needed() const
+ { return this->pointer_equality_needed_; }
+
+ // Set that there are relocations for this symbol where pointer equality
+ // matters.
+ void
+ set_pointer_equality_needed()
+ { this->pointer_equality_needed_ = true; }
+
+ // Return global GOT area where this symbol in located.
+ Global_got_area
+ global_got_area() const
+ { return this->global_got_area_; }
+
+ // Set global GOT area where this symbol in located.
+ void
+ set_global_got_area(Global_got_area global_got_area)
+ { this->global_got_area_ = global_got_area; }
+
+ // Return the global GOT offset for this symbol. For multi-GOT links, this
+ // returns the offset from the start of .got section to the first GOT entry
+ // for the symbol. Note that in multi-GOT links the symbol can have entry
+ // in more than one GOT.
+ unsigned int
+ global_gotoffset() const
+ { return this->global_gotoffset_; }
+
+ // Set the global GOT offset for this symbol. Note that in multi-GOT links
+ // the symbol can have entry in more than one GOT. This method will set
+ // the offset only if it is less than current offset.
+ void
+ set_global_gotoffset(unsigned int offset)
+ {
+ if (this->global_gotoffset_ == -1U || offset < this->global_gotoffset_)
+ this->global_gotoffset_ = offset;
+ }
+
+ // Return whether all GOT relocations for this symbol are for calls.
+ bool
+ got_only_for_calls() const
+ { return this->got_only_for_calls_; }
+
+ // Set that there is a GOT relocation for this symbol that is not for call.
+ void
+ set_got_not_only_for_calls()
+ { this->got_only_for_calls_ = false; }
+
+ // Return whether this is a PIC symbol.
+ bool
+ is_pic() const
+ {
+ // (st_other & STO_MIPS_FLAGS) == STO_MIPS_PIC
+ return ((this->nonvis() & (elfcpp::STO_MIPS_FLAGS >> 2))
+ == (elfcpp::STO_MIPS_PIC >> 2));
+ }
+
+ // Set the flag in st_other field that marks this symbol as PIC.
+ void
+ set_pic()
+ {
+ if (this->is_mips16())
+ // (st_other & ~(STO_MIPS16 | STO_MIPS_FLAGS)) | STO_MIPS_PIC
+ this->set_nonvis((this->nonvis()
+ & ~((elfcpp::STO_MIPS16 >> 2)
+ | (elfcpp::STO_MIPS_FLAGS >> 2)))
+ | (elfcpp::STO_MIPS_PIC >> 2));
+ else
+ // (other & ~STO_MIPS_FLAGS) | STO_MIPS_PIC
+ this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
+ | (elfcpp::STO_MIPS_PIC >> 2));
+ }
+
+ // Set the flag in st_other field that marks this symbol as PLT.
+ void
+ set_mips_plt()
+ {
+ if (this->is_mips16())
+ // (st_other & (STO_MIPS16 | ~STO_MIPS_FLAGS)) | STO_MIPS_PLT
+ this->set_nonvis((this->nonvis()
+ & ((elfcpp::STO_MIPS16 >> 2)
+ | ~(elfcpp::STO_MIPS_FLAGS >> 2)))
+ | (elfcpp::STO_MIPS_PLT >> 2));
+
+ else
+ // (st_other & ~STO_MIPS_FLAGS) | STO_MIPS_PLT
+ this->set_nonvis((this->nonvis() & ~(elfcpp::STO_MIPS_FLAGS >> 2))
+ | (elfcpp::STO_MIPS_PLT >> 2));
+ }
+
+ // Downcast a base pointer to a Mips_symbol pointer.
+ static Mips_symbol<size>*
+ as_mips_sym(Symbol* sym)
+ { return static_cast<Mips_symbol<size>*>(sym); }
+
+ // Downcast a base pointer to a Mips_symbol pointer.
+ static const Mips_symbol<size>*
+ as_mips_sym(const Symbol* sym)
+ { return static_cast<const Mips_symbol<size>*>(sym); }
+
+ // Return whether the symbol has lazy-binding stub.
+ bool
+ has_lazy_stub() const
+ { return this->has_lazy_stub_; }
+
+ // Set whether the symbol has lazy-binding stub.
+ void
+ set_has_lazy_stub(bool has_lazy_stub)
+ { this->has_lazy_stub_ = has_lazy_stub; }
+
+ // Return whether the symbol needs a standard PLT entry.
+ bool
+ needs_mips_plt() const
+ { return this->needs_mips_plt_; }
+
+ // Set whether the symbol needs a standard PLT entry.
+ void
+ set_needs_mips_plt(bool needs_mips_plt)
+ { this->needs_mips_plt_ = needs_mips_plt; }
+
+ // Return whether the symbol needs a compressed (MIPS16 or microMIPS) PLT
+ // entry.
+ bool
+ needs_comp_plt() const
+ { return this->needs_comp_plt_; }
+
+ // Set whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
+ void
+ set_needs_comp_plt(bool needs_comp_plt)
+ { this->needs_comp_plt_ = needs_comp_plt; }
+
+ // Return standard PLT entry offset, or -1 if none.
+ unsigned int
+ mips_plt_offset() const
+ { return this->mips_plt_offset_; }
+
+ // Set standard PLT entry offset.
+ void
+ set_mips_plt_offset(unsigned int mips_plt_offset)
+ { this->mips_plt_offset_ = mips_plt_offset; }
+
+ // Return whether the symbol has standard PLT entry.
+ bool
+ has_mips_plt_offset() const
+ { return this->mips_plt_offset_ != -1U; }
+
+ // Return compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
+ unsigned int
+ comp_plt_offset() const
+ { return this->comp_plt_offset_; }
+
+ // Set compressed (MIPS16 or microMIPS) PLT entry offset.
+ void
+ set_comp_plt_offset(unsigned int comp_plt_offset)
+ { this->comp_plt_offset_ = comp_plt_offset; }
+
+ // Return whether the symbol has compressed (MIPS16 or microMIPS) PLT entry.
+ bool
+ has_comp_plt_offset() const
+ { return this->comp_plt_offset_ != -1U; }
+
+ // Return MIPS16 fn stub for a symbol.
+ template<bool big_endian>
+ Mips16_stub_section<size, big_endian>*
+ get_mips16_fn_stub() const
+ {
+ return static_cast<Mips16_stub_section<size, big_endian>*>(mips16_fn_stub_);
+ }
+
+ // Set MIPS16 fn stub for a symbol.
+ void
+ set_mips16_fn_stub(Mips16_stub_section_base* stub)
+ { this->mips16_fn_stub_ = stub; }
+
+ // Return whether symbol has MIPS16 fn stub.
+ bool
+ has_mips16_fn_stub() const
+ { return this->mips16_fn_stub_ != NULL; }
+
+ // Return MIPS16 call stub for a symbol.
+ template<bool big_endian>
+ Mips16_stub_section<size, big_endian>*
+ get_mips16_call_stub() const
+ {
+ return static_cast<Mips16_stub_section<size, big_endian>*>(
+ mips16_call_stub_);
+ }
+
+ // Set MIPS16 call stub for a symbol.
+ void
+ set_mips16_call_stub(Mips16_stub_section_base* stub)
+ { this->mips16_call_stub_ = stub; }
+
+ // Return whether symbol has MIPS16 call stub.
+ bool
+ has_mips16_call_stub() const
+ { return this->mips16_call_stub_ != NULL; }
+
+ // Return MIPS16 call_fp stub for a symbol.
+ template<bool big_endian>
+ Mips16_stub_section<size, big_endian>*
+ get_mips16_call_fp_stub() const
+ {
+ return static_cast<Mips16_stub_section<size, big_endian>*>(
+ mips16_call_fp_stub_);
+ }
+
+ // Set MIPS16 call_fp stub for a symbol.
+ void
+ set_mips16_call_fp_stub(Mips16_stub_section_base* stub)
+ { this->mips16_call_fp_stub_ = stub; }
+
+ // Return whether symbol has MIPS16 call_fp stub.
+ bool
+ has_mips16_call_fp_stub() const
+ { return this->mips16_call_fp_stub_ != NULL; }
+
+ bool
+ get_applied_secondary_got_fixup() const
+ { return applied_secondary_got_fixup_; }
+
+ void
+ set_applied_secondary_got_fixup()
+ { this->applied_secondary_got_fixup_ = true; }
+
+ private:
+ // Whether the symbol needs MIPS16 fn_stub. This is true if this symbol
+ // appears in any relocs other than a 16 bit call.
+ bool need_fn_stub_;
+
+ // True if this symbol is referenced by branch relocations from
+ // any non-PIC input file. This is used to determine whether an
+ // la25 stub is required.
+ bool has_nonpic_branches_;
+
+ // The offset of the la25 stub for this symbol from the start of the
+ // la25 stub section.
+ unsigned int la25_stub_offset_;
+
+ // True if there is a relocation against this symbol that must be
+ // resolved by the static linker (that is, the relocation cannot
+ // possibly be made dynamic).
+ bool has_static_relocs_;
+
+ // Whether we must not create a lazy-binding stub for this symbol.
+ // This is true if the symbol has relocations related to taking the
+ // function's address.
+ bool no_lazy_stub_;
+
+ // The offset of the lazy-binding stub for this symbol from the start of
+ // .MIPS.stubs section.
+ unsigned int lazy_stub_offset_;
+
+ // True if there are any relocations for this symbol where pointer equality
+ // matters.
+ bool pointer_equality_needed_;
+
+ // Global GOT area where this symbol in located, or GGA_NONE if symbol is not
+ // in the global part of the GOT.
+ Global_got_area global_got_area_;
+
+ // The global GOT offset for this symbol. For multi-GOT links, this is offset
+ // from the start of .got section to the first GOT entry for the symbol.
+ // Note that in multi-GOT links the symbol can have entry in more than one GOT.
+ unsigned int global_gotoffset_;
+
+ // Whether all GOT relocations for this symbol are for calls.
+ bool got_only_for_calls_;
+ // Whether the symbol has lazy-binding stub.
+ bool has_lazy_stub_;
+ // Whether the symbol needs a standard PLT entry.
+ bool needs_mips_plt_;
+ // Whether the symbol needs a compressed (MIPS16 or microMIPS) PLT entry.
+ bool needs_comp_plt_;
+ // Standard PLT entry offset, or -1 if none.
+ unsigned int mips_plt_offset_;
+ // Compressed (MIPS16 or microMIPS) PLT entry offset, or -1 if none.
+ unsigned int comp_plt_offset_;
+ // MIPS16 fn stub for a symbol.
+ Mips16_stub_section_base* mips16_fn_stub_;
+ // MIPS16 call stub for a symbol.
+ Mips16_stub_section_base* mips16_call_stub_;
+ // MIPS16 call_fp stub for a symbol.
+ Mips16_stub_section_base* mips16_call_fp_stub_;
+
+ bool applied_secondary_got_fixup_;
+};
+
+// Mips16_stub_section class.
+
+// The mips16 compiler uses a couple of special sections to handle
+// floating point arguments.
+
+// Section names that look like .mips16.fn.FNNAME contain stubs that
+// copy floating point arguments from the fp regs to the gp regs and
+// then jump to FNNAME. If any 32 bit function calls FNNAME, the
+// call should be redirected to the stub instead. If no 32 bit
+// function calls FNNAME, the stub should be discarded. We need to
+// consider any reference to the function, not just a call, because
+// if the address of the function is taken we will need the stub,
+// since the address might be passed to a 32 bit function.
+
+// Section names that look like .mips16.call.FNNAME contain stubs
+// that copy floating point arguments from the gp regs to the fp
+// regs and then jump to FNNAME. If FNNAME is a 32 bit function,
+// then any 16 bit function that calls FNNAME should be redirected
+// to the stub instead. If FNNAME is not a 32 bit function, the
+// stub should be discarded.
+
+// .mips16.call.fp.FNNAME sections are similar, but contain stubs
+// which call FNNAME and then copy the return value from the fp regs
+// to the gp regs. These stubs store the return address in $18 while
+// calling FNNAME; any function which might call one of these stubs
+// must arrange to save $18 around the call. (This case is not
+// needed for 32 bit functions that call 16 bit functions, because
+// 16 bit functions always return floating point values in both
+// $f0/$f1 and $2/$3.)
+
+// Note that in all cases FNNAME might be defined statically.
+// Therefore, FNNAME is not used literally. Instead, the relocation
+// information will indicate which symbol the section is for.
+
+// We record any stubs that we find in the symbol table.
+
+// TODO(sasa): All mips16 stub sections should be emitted in the .text section.
+
+class Mips16_stub_section_base { };
+
+template<int size, bool big_endian>
+class Mips16_stub_section : public Mips16_stub_section_base
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+ public:
+ Mips16_stub_section(Mips_relobj<size, big_endian>* object, unsigned int shndx)
+ : object_(object), shndx_(shndx), r_sym_(0), gsym_(NULL),
+ found_r_mips_none_(false)
+ {
+ gold_assert(object->is_mips16_fn_stub_section(shndx)
+ || object->is_mips16_call_stub_section(shndx)
+ || object->is_mips16_call_fp_stub_section(shndx));
+ }
+
+ // Return the object of this stub section.
+ Mips_relobj<size, big_endian>*
+ object() const
+ { return this->object_; }
+
+ // Return the size of a section.
+ uint64_t
+ section_size() const
+ { return this->object_->section_size(this->shndx_); }
+
+ // Return section index of this stub section.
+ unsigned int
+ shndx() const
+ { return this->shndx_; }
+
+ // Return symbol index, if stub is for a local function.
+ unsigned int
+ r_sym() const
+ { return this->r_sym_; }
+
+ // Return symbol, if stub is for a global function.
+ Mips_symbol<size>*
+ gsym() const
+ { return this->gsym_; }
+
+ // Return whether stub is for a local function.
+ bool
+ is_for_local_function() const
+ { return this->gsym_ == NULL; }
+
+ // This method is called when a new relocation R_TYPE for local symbol R_SYM
+ // is found in the stub section. Try to find stub target.
+ void
+ new_local_reloc_found(unsigned int r_type, unsigned int r_sym)
+ {
+ // To find target symbol for this stub, trust the first R_MIPS_NONE
+ // relocation, if any. Otherwise trust the first relocation, whatever
+ // its kind.
+ if (this->found_r_mips_none_)
+ return;
+ if (r_type == elfcpp::R_MIPS_NONE)
+ {
+ this->r_sym_ = r_sym;
+ this->gsym_ = NULL;
+ this->found_r_mips_none_ = true;
+ }
+ else if (!is_target_found())
+ this->r_sym_ = r_sym;
+ }
+
+ // This method is called when a new relocation R_TYPE for global symbol GSYM
+ // is found in the stub section. Try to find stub target.
+ void
+ new_global_reloc_found(unsigned int r_type, Mips_symbol<size>* gsym)
+ {
+ // To find target symbol for this stub, trust the first R_MIPS_NONE
+ // relocation, if any. Otherwise trust the first relocation, whatever
+ // its kind.
+ if (this->found_r_mips_none_)
+ return;
+ if (r_type == elfcpp::R_MIPS_NONE)
+ {
+ this->gsym_ = gsym;
+ this->r_sym_ = 0;
+ this->found_r_mips_none_ = true;
+ }
+ else if (!is_target_found())
+ this->gsym_ = gsym;
+ }
+
+ // Return whether we found the stub target.
+ bool
+ is_target_found() const
+ { return this->r_sym_ != 0 || this->gsym_ != NULL; }
+
+ // Return whether this is a fn stub.
+ bool
+ is_fn_stub() const
+ { return this->object_->is_mips16_fn_stub_section(this->shndx_); }
+
+ // Return whether this is a call stub.
+ bool
+ is_call_stub() const
+ { return this->object_->is_mips16_call_stub_section(this->shndx_); }
+
+ // Return whether this is a call_fp stub.
+ bool
+ is_call_fp_stub() const
+ { return this->object_->is_mips16_call_fp_stub_section(this->shndx_); }
+
+ // Return the output address.
+ Mips_address
+ output_address() const
+ {
+ return (this->object_->output_section(this->shndx_)->address()
+ + this->object_->output_section_offset(this->shndx_));
+ }
+
+ private:
+ // The object of this stub section.
+ Mips_relobj<size, big_endian>* object_;
+ // The section index of this stub section.
+ unsigned int shndx_;
+ // The symbol index, if stub is for a local function.
+ unsigned int r_sym_;
+ // The symbol, if stub is for a global function.
+ Mips_symbol<size>* gsym_;
+ // True if we found R_MIPS_NONE relocation in this stub.
+ bool found_r_mips_none_;
+};
+
+// Mips_relobj class.
+
+template<int size, bool big_endian>
+class Mips_relobj : public Sized_relobj_file<size, big_endian>
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+ typedef std::map<unsigned int, Mips16_stub_section<size, big_endian>*>
+ Mips16_stubs_int_map;
+
+ public:
+ Mips_relobj(const std::string& name, Input_file* input_file, off_t offset,
+ const typename elfcpp::Ehdr<size, big_endian>& ehdr)
+ : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
+ processor_specific_flags_(0), local_symbol_is_mips16_(),
+ local_symbol_is_micromips_(), mips16_stub_sections_(),
+ local_non_16bit_calls_(), local_16bit_calls_(), local_mips16_fn_stubs_(),
+ local_mips16_call_stubs_(), gp_(0), got_info_(NULL),
+ section_is_mips16_fn_stub_(), section_is_mips16_call_stub_(),
+ section_is_mips16_call_fp_stub_(), pdr_shndx_(-1U)
+ {
+ this->is_pic_ = (ehdr.get_e_flags() & elfcpp::EF_MIPS_PIC) != 0;
+ this->is_n32_ = elfcpp::abi_n32(ehdr.get_e_flags());
+ this->is_n64_ = elfcpp::abi_64(ehdr.get_e_ident()[elfcpp::EI_CLASS]);
+ }
+
+ ~Mips_relobj()
+ { }
+
+ // Downcast a base pointer to a Mips_relobj pointer. This is
+ // not type-safe but we only use Mips_relobj not the base class.
+ static Mips_relobj<size, big_endian>*
+ as_mips_relobj(Relobj* relobj)
+ { return static_cast<Mips_relobj<size, big_endian>*>(relobj); }
+
+ // Downcast a base pointer to a Mips_relobj pointer. This is
+ // not type-safe but we only use Mips_relobj not the base class.
+ static const Mips_relobj<size, big_endian>*
+ as_mips_relobj(const Relobj* relobj)
+ { return static_cast<const Mips_relobj<size, big_endian>*>(relobj); }
+
+ // Processor-specific flags in ELF file header. This is valid only after
+ // reading symbols.
+ elfcpp::Elf_Word
+ processor_specific_flags() const
+ { return this->processor_specific_flags_; }
+
+ // Whether a local symbol is MIPS16 symbol. R_SYM is the symbol table
+ // index. This is only valid after do_count_local_symbol is called.
+ bool
+ local_symbol_is_mips16(unsigned int r_sym) const
+ {
+ gold_assert(r_sym < this->local_symbol_is_mips16_.size());
+ return this->local_symbol_is_mips16_[r_sym];
+ }
+
+ // Whether a local symbol is microMIPS symbol. R_SYM is the symbol table
+ // index. This is only valid after do_count_local_symbol is called.
+ bool
+ local_symbol_is_micromips(unsigned int r_sym) const
+ {
+ gold_assert(r_sym < this->local_symbol_is_micromips_.size());
+ return this->local_symbol_is_micromips_[r_sym];
+ }
+
+ // Get or create MIPS16 stub section.
+ Mips16_stub_section<size, big_endian>*
+ get_mips16_stub_section(unsigned int shndx)
+ {
+ typename Mips16_stubs_int_map::const_iterator it =
+ this->mips16_stub_sections_.find(shndx);
+ if (it != this->mips16_stub_sections_.end())
+ return (*it).second;
+
+ Mips16_stub_section<size, big_endian>* stub_section =
+ new Mips16_stub_section<size, big_endian>(this, shndx);
+ this->mips16_stub_sections_.insert(
+ std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
+ stub_section->shndx(), stub_section));
+ return stub_section;
+ }
+
+ // Return MIPS16 fn stub section for local symbol R_SYM, or NULL if this
+ // object doesn't have fn stub for R_SYM.
+ Mips16_stub_section<size, big_endian>*
+ get_local_mips16_fn_stub(unsigned int r_sym) const
+ {
+ typename Mips16_stubs_int_map::const_iterator it =
+ this->local_mips16_fn_stubs_.find(r_sym);
+ if (it != this->local_mips16_fn_stubs_.end())
+ return (*it).second;
+ return NULL;
+ }
+
+ // Record that this object has MIPS16 fn stub for local symbol. This method
+ // is only called if we decided not to discard the stub.
+ void
+ add_local_mips16_fn_stub(Mips16_stub_section<size, big_endian>* stub)
+ {
+ gold_assert(stub->is_for_local_function());
+ unsigned int r_sym = stub->r_sym();
+ this->local_mips16_fn_stubs_.insert(
+ std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
+ r_sym, stub));
+ }
+
+ // Return MIPS16 call stub section for local symbol R_SYM, or NULL if this
+ // object doesn't have call stub for R_SYM.
+ Mips16_stub_section<size, big_endian>*
+ get_local_mips16_call_stub(unsigned int r_sym) const
+ {
+ typename Mips16_stubs_int_map::const_iterator it =
+ this->local_mips16_call_stubs_.find(r_sym);
+ if (it != this->local_mips16_call_stubs_.end())
+ return (*it).second;
+ return NULL;
+ }
+
+ // Record that this object has MIPS16 call stub for local symbol. This method
+ // is only called if we decided not to discard the stub.
+ void
+ add_local_mips16_call_stub(Mips16_stub_section<size, big_endian>* stub)
+ {
+ gold_assert(stub->is_for_local_function());
+ unsigned int r_sym = stub->r_sym();
+ this->local_mips16_call_stubs_.insert(
+ std::pair<unsigned int, Mips16_stub_section<size, big_endian>*>(
+ r_sym, stub));
+ }
+
+ // Record that we found "non 16-bit" call relocation against local symbol
+ // SYMNDX. This reloc would need to refer to a MIPS16 fn stub, if there
+ // is one.
+ void
+ add_local_non_16bit_call(unsigned int symndx)
+ { this->local_non_16bit_calls_.insert(symndx); }
+
+ // Return true if there is any "non 16-bit" call relocation against local
+ // symbol SYMNDX in this object.
+ bool
+ has_local_non_16bit_call_relocs(unsigned int symndx)
+ {
+ return (this->local_non_16bit_calls_.find(symndx)
+ != this->local_non_16bit_calls_.end());
+ }
+
+ // Record that we found 16-bit call relocation R_MIPS16_26 against local
+ // symbol SYMNDX. Local MIPS16 call or call_fp stubs will only be needed
+ // if there is some R_MIPS16_26 relocation that refers to the stub symbol.
+ void
+ add_local_16bit_call(unsigned int symndx)
+ { this->local_16bit_calls_.insert(symndx); }
+
+ // Return true if there is any 16-bit call relocation R_MIPS16_26 against local
+ // symbol SYMNDX in this object.
+ bool
+ has_local_16bit_call_relocs(unsigned int symndx)
+ {
+ return (this->local_16bit_calls_.find(symndx)
+ != this->local_16bit_calls_.end());
+ }
+
+ // Get gp value that was used to create this object.
+ Mips_address
+ gp_value() const
+ { return this->gp_; }
+
+ // Return whether the object is a PIC object.
+ bool
+ is_pic() const
+ { return this->is_pic_; }
+
+ // Return whether the object uses N32 ABI.
+ bool
+ is_n32() const
+ { return this->is_n32_; }
+
+ // Return whether the object uses N64 ABI.
+ bool
+ is_n64() const
+ { return this->is_n64_; }
+
+ // Return whether the object uses NewABI conventions.
+ bool
+ is_newabi() const
+ { return this->is_n32_ || this->is_n64_; }
+
+ // Return Mips_got_info for this object.
+ Mips_got_info<size, big_endian>*
+ get_got_info() const
+ { return this->got_info_; }
+
+ // Return Mips_got_info for this object. Create new info if it doesn't exist.
+ Mips_got_info<size, big_endian>*
+ get_or_create_got_info()
+ {
+ if (!this->got_info_)
+ this->got_info_ = new Mips_got_info<size, big_endian>();
+ return this->got_info_;
+ }
+
+ // Set Mips_got_info for this object.
+ void
+ set_got_info(Mips_got_info<size, big_endian>* got_info)
+ { this->got_info_ = got_info; }
+
+ // Whether a section SHDNX is a MIPS16 stub section. This is only valid
+ // after do_read_symbols is called.
+ bool
+ is_mips16_stub_section(unsigned int shndx)
+ {
+ return (is_mips16_fn_stub_section(shndx)
+ || is_mips16_call_stub_section(shndx)
+ || is_mips16_call_fp_stub_section(shndx));
+ }
+
+ // Return TRUE if relocations in section SHNDX can refer directly to a
+ // MIPS16 function rather than to a hard-float stub. This is only valid
+ // after do_read_symbols is called.
+ bool
+ section_allows_mips16_refs(unsigned int shndx)
+ {
+ return (this->is_mips16_stub_section(shndx) || shndx == this->pdr_shndx_);
+ }
+
+ // Whether a section SHDNX is a MIPS16 fn stub section. This is only valid
+ // after do_read_symbols is called.
+ bool
+ is_mips16_fn_stub_section(unsigned int shndx)
+ {
+ gold_assert(shndx < this->section_is_mips16_fn_stub_.size());
+ return this->section_is_mips16_fn_stub_[shndx];
+ }
+
+ // Whether a section SHDNX is a MIPS16 call stub section. This is only valid
+ // after do_read_symbols is called.
+ bool
+ is_mips16_call_stub_section(unsigned int shndx)
+ {
+ gold_assert(shndx < this->section_is_mips16_call_stub_.size());
+ return this->section_is_mips16_call_stub_[shndx];
+ }
+
+ // Whether a section SHDNX is a MIPS16 call_fp stub section. This is only
+ // valid after do_read_symbols is called.
+ bool
+ is_mips16_call_fp_stub_section(unsigned int shndx)
+ {
+ gold_assert(shndx < this->section_is_mips16_call_fp_stub_.size());
+ return this->section_is_mips16_call_fp_stub_[shndx];
+ }
+
+ // Discard MIPS16 stub secions that are not needed.
+ void
+ discard_mips16_stub_sections(Symbol_table* symtab)
+ {
+ for (typename Mips16_stubs_int_map::const_iterator
+ it = this->mips16_stub_sections_.begin();
+ it != this->mips16_stub_sections_.end(); ++it)
+ {
+ Mips16_stub_section<size, big_endian>* stub_section = it->second;
+ if (!stub_section->is_target_found())
+ {
+ gold_error(_("no relocation found in mips16 stub section '%s'"),
+ stub_section->object()->section_name(stub_section->shndx()).c_str());
+ }
+
+ bool discard = false;
+ if (stub_section->is_for_local_function())
+ {
+ if (stub_section->is_fn_stub())
+ {
+ // This stub is for a local symbol. This stub will only
+ // be needed if there is some relocation in this object,
+ // other than a 16 bit function call, which refers to this
+ // symbol.
+ if (!this->has_local_non_16bit_call_relocs(
+ stub_section->r_sym()))
+ discard = true;
+ else
+ this->add_local_mips16_fn_stub(stub_section);
+ }
+ else
+ {
+ // This stub is for a local symbol. This stub will only
+ // be needed if there is some relocation (R_MIPS16_26) in
+ // this object that refers to this symbol.
+ gold_assert(stub_section->is_call_stub()
+ || stub_section->is_call_fp_stub());
+ if (!this->has_local_16bit_call_relocs(
+ stub_section->r_sym()))
+ discard = true;
+ else
+ this->add_local_mips16_call_stub(stub_section);
+ }
+ }
+ else
+ {
+ Mips_symbol<size>* gsym = stub_section->gsym();
+ if (stub_section->is_fn_stub())
+ {
+ if (gsym->has_mips16_fn_stub())
+ // We already have a stub for this function.
+ discard = true;
+ else
+ {
+ gsym->set_mips16_fn_stub(stub_section);
+ if (gsym->should_add_dynsym_entry(symtab))
+ {
+ // If we have a MIPS16 function with a stub, the
+ // dynamic symbol must refer to the stub, since only
+ // the stub uses the standard calling conventions.
+ gsym->set_need_fn_stub();
+ if (gsym->is_from_dynobj())
+ gsym->set_needs_dynsym_value();
+ }
+ }
+ if (!gsym->need_fn_stub())
+ discard = true;
+ }
+ else if (stub_section->is_call_stub())
+ {
+ if (gsym->is_mips16())
+ // We don't need the call_stub; this is a 16 bit
+ // function, so calls from other 16 bit functions are
+ // OK.
+ discard = true;
+ else if (gsym->has_mips16_call_stub())
+ // We already have a stub for this function.
+ discard = true;
+ else
+ gsym->set_mips16_call_stub(stub_section);
+ }
+ else
+ {
+ gold_assert(stub_section->is_call_fp_stub());
+ if (gsym->is_mips16())
+ // We don't need the call_stub; this is a 16 bit
+ // function, so calls from other 16 bit functions are
+ // OK.
+ discard = true;
+ else if (gsym->has_mips16_call_fp_stub())
+ // We already have a stub for this function.
+ discard = true;
+ else
+ gsym->set_mips16_call_fp_stub(stub_section);
+ }
+ }
+ if (discard)
+ this->set_output_section(stub_section->shndx(), NULL);
+ }
+ }
+
+ protected:
+ // Count the local symbols.
+ void
+ do_count_local_symbols(Stringpool_template<char>*,
+ Stringpool_template<char>*);
+
+ // Read the symbol information.
+ void
+ do_read_symbols(Read_symbols_data* sd);
+
+ private:
+ // processor-specific flags in ELF file header.
+ elfcpp::Elf_Word processor_specific_flags_;
+
+ // Bit vector to tell if a local symbol is a MIPS16 symbol or not.
+ // This is only valid after do_count_local_symbol is called.
+ std::vector<bool> local_symbol_is_mips16_;
+
+ // Bit vector to tell if a local symbol is a microMIPS symbol or not.
+ // This is only valid after do_count_local_symbol is called.
+ std::vector<bool> local_symbol_is_micromips_;
+
+ // Map from section index to the MIPS16 stub for that section. This contains
+ // all stubs found in this object.
+ Mips16_stubs_int_map mips16_stub_sections_;
+
+ // Local symbols that have "non 16-bit" call relocation. This relocation
+ // would need to refer to a MIPS16 fn stub, if there is one.
+ std::set<unsigned int> local_non_16bit_calls_;
+
+ // Local symbols that have 16-bit call relocation R_MIPS16_26. Local MIPS16
+ // call or call_fp stubs will only be needed if there is some R_MIPS16_26
+ // relocation that refers to the stub symbol.
+ std::set<unsigned int> local_16bit_calls_;
+
+ // Map from local symbol index to the MIPS16 fn stub for that symbol.
+ // This contains only the stubs that we decided not to discard.
+ Mips16_stubs_int_map local_mips16_fn_stubs_;
+
+ // Map from local symbol index to the MIPS16 call stub for that symbol.
+ // This contains only the stubs that we decided not to discard.
+ Mips16_stubs_int_map local_mips16_call_stubs_;
+
+ // gp value that was used to create this object.
+ Mips_address gp_;
+ // Whether the object is a PIC object.
+ bool is_pic_;
+ // Whether the object uses N32 ABI.
+ bool is_n32_;
+ // Whether the object uses N64 ABI.
+ bool is_n64_;
+ // The Mips_got_info for this object.
+ Mips_got_info<size, big_endian>* got_info_;
+
+ // Bit vector to tell if a section is a MIPS16 fn stub section or not.
+ // This is only valid after do_read_symbols is called.
+ std::vector<bool> section_is_mips16_fn_stub_;
+
+ // Bit vector to tell if a section is a MIPS16 call stub section or not.
+ // This is only valid after do_read_symbols is called.
+ std::vector<bool> section_is_mips16_call_stub_;
+
+ // Bit vector to tell if a section is a MIPS16 call_fp stub section or not.
+ // This is only valid after do_read_symbols is called.
+ std::vector<bool> section_is_mips16_call_fp_stub_;
+
+ // .pdr section index.
+ unsigned int pdr_shndx_;
+};
+
+// Mips_output_data_got class.
+
+template<int size, bool big_endian>
+class Mips_output_data_got : public Output_data_got<size, big_endian>
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+ typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
+ Reloc_section;
+ typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
+
+ public:
+ Mips_output_data_got(Target_mips<size, big_endian>* target,
+ Symbol_table* symtab, Layout* layout)
+ : Output_data_got<size, big_endian>(), target_(target),
+ symbol_table_(symtab), layout_(layout), static_relocs_(), got_view_(NULL),
+ first_global_got_dynsym_index_(-1U), primary_got_(NULL),
+ secondary_got_relocs_()
+ {
+ this->master_got_info_ = new Mips_got_info<size, big_endian>();
+ this->set_addralign(16);
+ }
+
+ // Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
+ // SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
+ void
+ record_local_got_symbol(Mips_relobj<size, big_endian>* object,
+ unsigned int symndx, Mips_address addend,
+ unsigned int r_type, unsigned int shndx)
+ {
+ this->master_got_info_->record_local_got_symbol(object, symndx, addend,
+ r_type, shndx);
+ }
+
+ // Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
+ // in OBJECT. FOR_CALL is true if the caller is only interested in
+ // using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
+ // relocation.
+ void
+ record_global_got_symbol(Mips_symbol<size>* mips_sym,
+ Mips_relobj<size, big_endian>* object,
+ unsigned int r_type, bool dyn_reloc, bool for_call)
+ {
+ this->master_got_info_->record_global_got_symbol(mips_sym, object, r_type,
+ dyn_reloc, for_call);
+ }
+
+ // Record that OBJECT has a page relocation against symbol SYMNDX and
+ // that ADDEND is the addend for that relocation.
+ void
+ record_got_page_entry(Mips_relobj<size, big_endian>* object,
+ unsigned int symndx, int addend)
+ { this->master_got_info_->record_got_page_entry(object, symndx, addend); }
+
+ // 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,
+ Mips_symbol<size>* 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));
+ }
+
+ // Record that global symbol GSYM has R_TYPE dynamic relocation in the
+ // secondary GOT at OFFSET.
+ void
+ add_secondary_got_reloc(unsigned int got_offset, unsigned int r_type,
+ Mips_symbol<size>* gsym)
+ {
+ this->secondary_got_relocs_.push_back(Static_reloc(got_offset,
+ r_type, gsym));
+ }
+
+ // Update GOT entry at OFFSET with VALUE.
+ void
+ update_got_entry(unsigned int offset, Mips_address value)
+ {
+ elfcpp::Swap<size, big_endian>::writeval(this->got_view_ + offset, value);
+ }
+
+ // Return the number of entries in local part of the GOT. This includes
+ // local entries, page entries and 2 reserved entries.
+ unsigned int
+ get_local_gotno() const
+ {
+ if (!this->multi_got())
+ {
+ return (2 + this->master_got_info_->local_gotno()
+ + this->master_got_info_->page_gotno());
+ }
+ else
+ return 2 + this->primary_got_->local_gotno() + this->primary_got_->page_gotno();
+ }
+
+ // Return dynamic symbol table index of the first symbol with global GOT
+ // entry.
+ unsigned int
+ first_global_got_dynsym_index() const
+ { return this->first_global_got_dynsym_index_; }
+
+ // Set dynamic symbol table index of the first symbol with global GOT entry.
+ void
+ set_first_global_got_dynsym_index(unsigned int index)
+ { this->first_global_got_dynsym_index_ = index; }
+
+ // Lay out the GOT. Add local, global and TLS entries. If GOT is
+ // larger than 64K, create multi-GOT.
+ void
+ lay_out_got(Layout* layout, Symbol_table* symtab,
+ const Input_objects* input_objects);
+
+ // Create multi-GOT. For every GOT, add local, global and TLS entries.
+ void
+ lay_out_multi_got(Layout* layout, const Input_objects* input_objects);
+
+ // Attempt to merge GOTs of different input objects.
+ void
+ merge_gots(const Input_objects* input_objects);
+
+ // Consider merging FROM, which is OBJECT's GOT, into TO. Return false if
+ // this would lead to overflow, true if they were merged successfully.
+ bool
+ merge_got_with(Mips_got_info<size, big_endian>* from,
+ Mips_relobj<size, big_endian>* object,
+ Mips_got_info<size, big_endian>* to);
+
+ // Return the offset of GOT page entry for VALUE. For multi-GOT links,
+ // use OBJECT's GOT.
+ unsigned int
+ get_got_page_offset(Mips_address value,
+ const Mips_relobj<size, big_endian>* object)
+ {
+ Mips_got_info<size, big_endian>* g = (!this->multi_got()
+ ? this->master_got_info_
+ : object->get_got_info());
+ gold_assert(g != NULL);
+ return g->get_got_page_offset(value, this);
+ }
+
+ // Return the GOT offset of type GOT_TYPE of the global symbol
+ // GSYM. For multi-GOT links, use OBJECT's GOT.
+ unsigned int got_offset(const Symbol* gsym, unsigned int got_type,
+ Mips_relobj<size, big_endian>* object) const
+ {
+ if (!this->multi_got())
+ return gsym->got_offset(got_type);
+ else
+ {
+ Mips_got_info<size, big_endian>* g = object->get_got_info();
+ gold_assert(g != NULL);
+ return gsym->got_offset(g->multigot_got_type(got_type));
+ }
+ }
+
+ // Return the GOT offset of type GOT_TYPE of the local symbol
+ // SYMNDX.
+ unsigned int
+ got_offset(unsigned int symndx, unsigned int got_type,
+ Sized_relobj_file<size, big_endian>* object) const
+ { return object->local_got_offset(symndx, got_type); }
+
+ // Return the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
+ unsigned int
+ tls_ldm_offset(Mips_relobj<size, big_endian>* object) const
+ {
+ Mips_got_info<size, big_endian>* g = (!this->multi_got()
+ ? this->master_got_info_
+ : object->get_got_info());
+ gold_assert(g != NULL);
+ return g->tls_ldm_offset();
+ }
+
+ // Set the offset of TLS LDM entry. For multi-GOT links, use OBJECT's GOT.
+ void
+ set_tls_ldm_offset(unsigned int tls_ldm_offset,
+ Mips_relobj<size, big_endian>* object)
+ {
+ Mips_got_info<size, big_endian>* g = (!this->multi_got()
+ ? this->master_got_info_
+ : object->get_got_info());
+ gold_assert(g != NULL);
+ g->set_tls_ldm_offset(tls_ldm_offset);
+ }
+
+ // Return true for multi-GOT links.
+ bool
+ multi_got() const
+ { return this->primary_got_ != NULL; }
+
+ // Return the offset of OBJECT's GOT from the start of .got section.
+ unsigned int
+ get_got_offset(const Mips_relobj<size, big_endian>* object)
+ {
+ if (!this->multi_got())
+ return 0;
+ else
+ {
+ Mips_got_info<size, big_endian>* g = object->get_got_info();
+ return g != NULL ? g->offset() : 0;
+ }
+ }
+
+ // Create global GOT entries that should be in the GGA_RELOC_ONLY area.
+ void
+ add_reloc_only_entries()
+ { this->master_got_info_->add_reloc_only_entries(this); }
+
+ // Return offset of the primary GOT's entry for global symbol.
+ unsigned int
+ get_primary_got_offset(const Mips_symbol<size>* sym) const
+ {
+ gold_assert(sym->global_got_area() != GGA_NONE);
+ return (this->get_local_gotno() + sym->dynsym_index()
+ - this->first_global_got_dynsym_index()) * size/8;
+ }
+
+ // For the entry at offset GOT_OFFSET, return its offset from the gp.
+ // Input argument GOT_OFFSET is always global offset from the start of
+ // .got section, for both single and multi-GOT links.
+ // For single GOT links, this returns GOT_OFFSET - 0x7FF0. For multi-GOT
+ // links, the return value is object_got_offset - 0x7FF0, where
+ // object_got_offset is offset in the OBJECT's GOT.
+ int
+ gp_offset(unsigned int got_offset,
+ const Mips_relobj<size, big_endian>* object) const
+ {
+ return (this->address() + got_offset
+ - this->target_->adjusted_gp_value(object));
+ }
+
+ protected:
+ // Write out the GOT table.
+ void
+ do_write(Output_file*);
+
+ private:
+
+ // 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,
+ Mips_symbol<size>* 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.
+ Mips_symbol<size>*
+ 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.
+ Mips_symbol<size>* 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_;
+ };
+
+ // The target.
+ Target_mips<size, big_endian>* target_;
+ // The symbol table.
+ Symbol_table* symbol_table_;
+ // The layout.
+ Layout* layout_;
+ // Static relocs to be applied to the GOT.
+ std::vector<Static_reloc> static_relocs_;
+ // .got section view.
+ unsigned char* got_view_;
+ // The dynamic symbol table index of the first symbol with global GOT entry.
+ unsigned int first_global_got_dynsym_index_;
+ // The master GOT information.
+ Mips_got_info<size, big_endian>* master_got_info_;
+ // The primary GOT information.
+ Mips_got_info<size, big_endian>* primary_got_;
+ // Secondary GOT fixups.
+ std::vector<Static_reloc> secondary_got_relocs_;
+};
+
+// A class to handle LA25 stubs - non-PIC interface to a PIC function. There are
+// two ways of creating these interfaces. The first is to add:
+//
+// lui $25,%hi(func)
+// j func
+// addiu $25,$25,%lo(func)
+//
+// to a separate trampoline section. The second is to add:
+//
+// lui $25,%hi(func)
+// addiu $25,$25,%lo(func)
+//
+// immediately before a PIC function "func", but only if a function is at the
+// beginning of the section, and the section is not too heavily aligned (i.e we
+// would need to add no more than 2 nops before the stub.)
+//
+// We only create stubs of the first type.
+
+template<int size, bool big_endian>
+class Mips_output_data_la25_stub : public Output_section_data
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+ public:
+ Mips_output_data_la25_stub()
+ : Output_section_data(size == 32 ? 4 : 8), symbols_()
+ { }
+
+ // Create LA25 stub for a symbol.
+ void
+ create_la25_stub(Symbol_table* symtab, Target_mips<size, big_endian>* target,
+ Mips_symbol<size>* gsym);
+
+ // Return output address of a stub.
+ Mips_address
+ stub_address(const Mips_symbol<size>* sym) const
+ {
+ gold_assert(sym->has_la25_stub());
+ return this->address() + sym->la25_stub_offset();
+ }
+
+ protected:
+ void
+ do_adjust_output_section(Output_section* os)
+ { os->set_entsize(0); }
+
+ private:
+ // Template for standard LA25 stub.
+ static const uint32_t la25_stub_entry[];
+ // Template for microMIPS LA25 stub.
+ static const uint32_t la25_stub_micromips_entry[];
+
+ // Set the final size.
+ void
+ set_final_data_size()
+ { this->set_data_size(this->symbols_.size() * 16); }
+
+ // Create a symbol for SYM stub's value and size, to help make the
+ // disassembly easier to read.
+ void
+ create_stub_symbol(Mips_symbol<size>* sym, Symbol_table* symtab,
+ Target_mips<size, big_endian>* target, uint64_t symsize);
+
+ // Write out the LA25 stub section.
+ void
+ do_write(Output_file*);
+
+ // Symbols that have LA25 stubs.
+ Unordered_set<Mips_symbol<size>*> symbols_;
+};
+
+// A class to handle the PLT data.
+
+template<int size, bool big_endian>
+class Mips_output_data_plt : public Output_section_data
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+ typedef Output_data_reloc<elfcpp::SHT_REL, true,
+ size, big_endian> Reloc_section;
+
+ public:
+ // Create the PLT section. The ordinary .got section is an argument,
+ // since we need to refer to the start.
+ Mips_output_data_plt(Layout* layout, Output_data_space* got_plt,
+ Target_mips<size, big_endian>* target)
+ : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), symbols_(),
+ plt_mips_offset_(0), plt_comp_offset_(0), plt_header_size_(0),
+ target_(target)
+ {
+ 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);
+ }
+
+ // Add an entry to the PLT for a symbol referenced by r_type relocation.
+ void
+ add_entry(Mips_symbol<size>* gsym, unsigned int r_type);
+
+ // 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->symbols_.size(); }
+
+ // Return the offset of the first non-reserved PLT entry.
+ unsigned int
+ first_plt_entry_offset() const
+ { return sizeof(plt0_entry_o32); }
+
+ // Return the size of a PLT entry.
+ unsigned int
+ plt_entry_size() const
+ { return sizeof(plt_entry); }
+
+ // Set final PLT offsets. For each symbol, determine whether standard or
+ // compressed (MIPS16 or microMIPS) PLT entry is used.
+ void
+ set_plt_offsets();
+
+ // Return the offset of the first standard PLT entry.
+ unsigned int
+ first_mips_plt_offset() const
+ { return this->plt_header_size_; }
+
+ // Return the offset of the first compressed PLT entry.
+ unsigned int
+ first_comp_plt_offset() const
+ { return this->plt_header_size_ + this->plt_mips_offset_; }
+
+ // Return whether there are any standard PLT entries.
+ bool
+ has_standard_entries() const
+ { return this->plt_mips_offset_ > 0; }
+
+ // Return the output address of standard PLT entry.
+ Mips_address
+ mips_entry_address(const Mips_symbol<size>* sym) const
+ {
+ gold_assert (sym->has_mips_plt_offset());
+ return (this->address() + this->first_mips_plt_offset()
+ + sym->mips_plt_offset());
+ }
+
+ // Return the output address of compressed (MIPS16 or microMIPS) PLT entry.
+ Mips_address
+ comp_entry_address(const Mips_symbol<size>* sym) const
+ {
+ gold_assert (sym->has_comp_plt_offset());
+ return (this->address() + this->first_comp_plt_offset()
+ + sym->comp_plt_offset());
+ }
+
+ protected:
+ void
+ do_adjust_output_section(Output_section* os)
+ { os->set_entsize(0); }
+
+ // 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 plt0_entry_o32[];
+ static const uint32_t plt0_entry_n32[];
+ static const uint32_t plt0_entry_n64[];
+ static const uint32_t plt0_entry_micromips_o32[];
+ static const uint32_t plt0_entry_micromips32_o32[];
+
+ // Template for subsequent PLT entries.
+ static const uint32_t plt_entry[];
+ static const uint32_t plt_entry_mips16_o32[];
+ static const uint32_t plt_entry_micromips_o32[];
+ static const uint32_t plt_entry_micromips32_o32[];
+
+ // Set the final size.
+ void
+ set_final_data_size()
+ {
+ this->set_data_size(this->plt_header_size_ + this->plt_mips_offset_
+ + this->plt_comp_offset_);
+ }
+
+ // Write out the PLT data.
+ void
+ do_write(Output_file*);
+
+ // Return whether the plt header contains microMIPS code. For the sake of
+ // cache alignment always use a standard header whenever any standard entries
+ // are present even if microMIPS entries are present as well. This also lets
+ // the microMIPS header rely on the value of $v0 only set by microMIPS
+ // entries, for a small size reduction.
+ bool
+ is_plt_header_compressed() const
+ {
+ gold_assert(this->plt_mips_offset_ + this->plt_comp_offset_ != 0);
+ return this->target_->is_output_micromips() && this->plt_mips_offset_ == 0;
+ }
+
+ // Return the size of the PLT header.
+ unsigned int
+ get_plt_header_size() const
+ {
+ if (this->target_->is_output_n64())
+ return 4 * sizeof(plt0_entry_n64) / sizeof(plt0_entry_n64[0]);
+ else if (this->target_->is_output_n32())
+ return 4 * sizeof(plt0_entry_n32) / sizeof(plt0_entry_n32[0]);
+ else if (!this->is_plt_header_compressed())
+ return 4 * sizeof(plt0_entry_o32) / sizeof(plt0_entry_o32[0]);
+ else if (this->target_->use_32bit_micromips_instructions())
+ return (2 * sizeof(plt0_entry_micromips32_o32)
+ / sizeof(plt0_entry_micromips32_o32[0]));
+ else
+ return (2 * sizeof(plt0_entry_micromips_o32)
+ / sizeof(plt0_entry_micromips_o32[0]));
+ }
+
+ // Return the PLT header entry.
+ const uint32_t*
+ get_plt_header_entry() const
+ {
+ if (this->target_->is_output_n64())
+ return plt0_entry_n64;
+ else if (this->target_->is_output_n32())
+ return plt0_entry_n32;
+ else if (!this->is_plt_header_compressed())
+ return plt0_entry_o32;
+ else if (this->target_->use_32bit_micromips_instructions())
+ return plt0_entry_micromips32_o32;
+ else
+ return plt0_entry_micromips_o32;
+ }
+
+ // Return the size of the standard PLT entry.
+ unsigned int
+ standard_plt_entry_size() const
+ { return 4 * sizeof(plt_entry) / sizeof(plt_entry[0]); }
+
+ // Return the size of the compressed PLT entry.
+ unsigned int
+ compressed_plt_entry_size() const
+ {
+ gold_assert(!this->target_->is_output_newabi());
+
+ if (!this->target_->is_output_micromips())
+ return (2 * sizeof(plt_entry_mips16_o32)
+ / sizeof(plt_entry_mips16_o32[0]));
+ else if (this->target_->use_32bit_micromips_instructions())
+ return (2 * sizeof(plt_entry_micromips32_o32)
+ / sizeof(plt_entry_micromips32_o32[0]));
+ else
+ return (2 * sizeof(plt_entry_micromips_o32)
+ / sizeof(plt_entry_micromips_o32[0]));
+ }
+
+ // The reloc section.
+ Reloc_section* rel_;
+ // The .got.plt section.
+ Output_data_space* got_plt_;
+ // Symbols that have PLT entry.
+ std::vector<Mips_symbol<size>*> symbols_;
+ // The offset of the next standard PLT entry to create.
+ unsigned int plt_mips_offset_;
+ // The offset of the next compressed PLT entry to create.
+ unsigned int plt_comp_offset_;
+ // The size of the PLT header in bytes.
+ unsigned int plt_header_size_;
+ // The target.
+ Target_mips<size, big_endian>* target_;
+};
+
+// A class to handle the .MIPS.stubs data.
+
+template<int size, bool big_endian>
+class Mips_output_data_mips_stubs : public Output_section_data
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+ public:
+ Mips_output_data_mips_stubs(Target_mips<size, big_endian>* target)
+ : Output_section_data(size == 32 ? 4 : 8), symbols_(), dynsym_count_(-1U),
+ stub_offsets_are_set_(false), target_(target)
+ { }
+
+ // Create entry for a symbol.
+ void
+ make_entry(Mips_symbol<size>*);
+
+ // Remove entry for a symbol.
+ void
+ remove_entry(Mips_symbol<size>* gsym);
+
+ // Set stub offsets for symbols. This method expects that the number of
+ // entries in dynamic symbol table is set.
+ void
+ set_lazy_stub_offsets();
+
+ // Set the number of entries in dynamic symbol table.
+ void
+ set_dynsym_count(unsigned int dynsym_count)
+ { this->dynsym_count_ = dynsym_count; }
+
+ // Return maximum size of the stub, ie. the stub size if the dynamic symbol
+ // count is greater than 0x10000. If the dynamic symbol count is less than
+ // 0x10000, the stub will be 4 bytes smaller.
+ // There's no disadvantage from using microMIPS code here, so for the sake of
+ // pure-microMIPS binaries we prefer it whenever there's any microMIPS code in
+ // output produced at all. This has a benefit of stubs being shorter by
+ // 4 bytes each too, unless in the insn32 mode.
+ unsigned int
+ stub_max_size() const
+ {
+ if (!this->target_->is_output_micromips()
+ || this->target_->use_32bit_micromips_instructions())
+ return 20;
+ else
+ return 16;
+ }
+
+ // Return output address of a stub.
+ Mips_address
+ stub_address(const Mips_symbol<size>* sym) const
+ {
+ gold_assert(sym->has_lazy_stub());
+ return this->address() + sym->lazy_stub_offset();
+ }
+
+ protected:
+ void
+ do_adjust_output_section(Output_section* os)
+ { os->set_entsize(0); }
+
+ // Write to a map file.
+ void
+ do_print_to_mapfile(Mapfile* mapfile) const
+ { mapfile->print_output_data(this, _(".MIPS.stubs")); }
+
+ private:
+ static const uint32_t lazy_stub_normal_1[4];
+ static const uint32_t lazy_stub_normal_1_n64[4];
+ static const uint32_t lazy_stub_normal_2[4];
+ static const uint32_t lazy_stub_normal_2_n64[4];
+ static const uint32_t lazy_stub_big[5];
+ static const uint32_t lazy_stub_big_n64[5];
+
+ static const uint32_t lazy_stub_micromips_normal_1[];
+ static const uint32_t lazy_stub_micromips_normal_1_n64[];
+ static const uint32_t lazy_stub_micromips_normal_2[];
+ static const uint32_t lazy_stub_micromips_normal_2_n64[];
+ static const uint32_t lazy_stub_micromips_big[];
+ static const uint32_t lazy_stub_micromips_big_n64[];
+
+ static const uint32_t lazy_stub_micromips32_normal_1[];
+ static const uint32_t lazy_stub_micromips32_normal_1_n64[];
+ static const uint32_t lazy_stub_micromips32_normal_2[];
+ static const uint32_t lazy_stub_micromips32_normal_2_n64[];
+ static const uint32_t lazy_stub_micromips32_big[];
+ static const uint32_t lazy_stub_micromips32_big_n64[];
+
+ // Set the final size.
+ void
+ set_final_data_size()
+ { this->set_data_size(this->symbols_.size() * this->stub_max_size()); }
+
+ // Write out the .MIPS.stubs data.
+ void
+ do_write(Output_file*);
+
+ // .MIPS.stubs symbols
+ Unordered_set<Mips_symbol<size>*> symbols_;
+ // Number of entries in dynamic symbol table.
+ unsigned int dynsym_count_;
+ // Whether the stub offsets are set.
+ bool stub_offsets_are_set_;
+ // The target.
+ Target_mips<size, big_endian>* target_;
+};
+
+// This class handles Mips .reginfo output section.
+
+template<int size, bool big_endian>
+class Mips_output_section_reginfo : public Output_section
+{
+ typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
+
+ public:
+ Mips_output_section_reginfo(const char* name, elfcpp::Elf_Word type,
+ elfcpp::Elf_Xword flags,
+ Target_mips<size, big_endian>* target)
+ : Output_section(name, type, flags), target_(target)
+ {
+ this->set_always_keeps_input_sections();
+ }
+
+ // Downcast a base pointer to a Mips_output_section_reginfo pointer.
+ static Mips_output_section_reginfo<size, big_endian>*
+ as_mips_output_section_reginfo(Output_section* os)
+ { return static_cast<Mips_output_section_reginfo<size, big_endian>*>(os); }
+
+ protected:
+ // Set the final data size.
+ void
+ set_final_data_size()
+ { this->set_data_size(24); }
+
+ // Write out reginfo section.
+ void
+ do_write(Output_file* of);
+
+ private:
+ Target_mips<size, big_endian>* target_;
+};
+
+// The MIPS target has relocation types which default handling of relocatable
+// relocation cannot process. 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)
+ {
+ if (sh_type == elfcpp::SHT_RELA)
+ return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
+ else
+ {
+ 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);
+ }
+ }
+ }
+};
+
+// Mips_copy_relocs class. This class is almost identical to the class
+// Copy_relocs from copy-relocs.h. The only difference is method
+// Copy_reloc_entry::emit.
+
+// This class is used to manage COPY relocations. We try to avoid
+// them when possible. A COPY relocation may be required when an
+// executable refers to a variable defined in a shared library. COPY
+// relocations are problematic because they tie the executable to the
+// exact size of the variable in the shared library. We can avoid
+// them if all the references to the variable are in a writeable
+// section. In that case we can simply use dynamic relocations.
+// However, when scanning relocs, we don't know when we see the
+// relocation whether we will be forced to use a COPY relocation or
+// not. So we have to save the relocation during the reloc scanning,
+// and then emit it as a dynamic relocation if necessary. This class
+// implements that. It is used by the target specific code.
+
+// The template parameter SH_TYPE is the type of the reloc section to
+// be used for COPY relocs: elfcpp::SHT_REL or elfcpp::SHT_RELA.
+
+template<int sh_type, int size, bool big_endian>
+class Mips_copy_relocs
+{
+ private:
+ typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reloc;
+
+ public:
+ Mips_copy_relocs(unsigned int copy_reloc_type)
+ : copy_reloc_type_(copy_reloc_type), dynbss_(NULL), entries_()
+ { }
+
+ // This is called while scanning relocs if we see a relocation
+ // against a symbol which may force us to generate a COPY reloc.
+ // SYM is the symbol. OBJECT is the object whose relocs we are
+ // scanning. The relocation is being applied to section SHNDX in
+ // OBJECT. OUTPUT_SECTION is the output section where section SHNDX
+ // will wind up. REL is the reloc itself. The Output_data_reloc
+ // section is where the dynamic relocs are put.
+ void
+ copy_reloc(Symbol_table*, Layout*, Sized_symbol<size>* sym,
+ Sized_relobj_file<size, big_endian>* object,
+ unsigned int shndx, Output_section* output_section,
+ const Reloc& rel,
+ Output_data_reloc<sh_type, true, size, big_endian>*);
+
+ // Return whether there are any saved relocations.
+ bool
+ any_saved_relocs() const
+ { return !this->entries_.empty(); }
+
+ // Emit any saved relocations which turn out to be needed. This is
+ // called after all the relocs have been scanned.
+ void
+ emit(Output_data_reloc<sh_type, true, size, big_endian>*,
+ Symbol_table*, Layout*, Target_mips<size, big_endian>*);
+
+ // Emit a COPY reloc.
+ void
+ emit_copy_reloc(Symbol_table*, Sized_symbol<size>*,
+ Output_data*, off_t,
+ Output_data_reloc<sh_type, true, size, big_endian>*);
+
+ private:
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Addend;
+
+ // This POD class holds the relocations we are saving. We will emit
+ // these relocations if it turns out that the symbol does not
+ // require a COPY relocation.
+ class Copy_reloc_entry
+ {
+ public:
+ Copy_reloc_entry(Symbol* sym, unsigned int reloc_type,
+ Sized_relobj_file<size, big_endian>* relobj,
+ unsigned int shndx,
+ Output_section* output_section,
+ Address address, Addend addend)
+ : sym_(sym), reloc_type_(reloc_type), relobj_(relobj),
+ shndx_(shndx), output_section_(output_section),
+ address_(address), addend_(addend)
+ { }
+
+ // Emit this reloc if appropriate. This is called after we have
+ // scanned all the relocations, so we know whether we emitted a
+ // COPY relocation for SYM_.
+ void
+ emit(Output_data_reloc<sh_type, true, size, big_endian>*,
+ Mips_copy_relocs<sh_type, size, big_endian>* copy_relocs,
+ Symbol_table*, Layout*, Target_mips<size, big_endian>*);
+
+ private:
+ Symbol* sym_;
+ unsigned int reloc_type_;
+ Sized_relobj_file<size, big_endian>* relobj_;
+ unsigned int shndx_;
+ Output_section* output_section_;
+ Address address_;
+ Addend addend_;
+ };
+
+ // A list of relocs to be saved.
+ typedef std::vector<Copy_reloc_entry> Copy_reloc_entries;
+
+ // Return whether we need a COPY reloc.
+ bool
+ need_copy_reloc(Sized_symbol<size>* gsym,
+ Sized_relobj_file<size, big_endian>* object,
+ unsigned int shndx) const;
+
+ // Make a new COPY reloc and emit it.
+ void
+ make_copy_reloc(Symbol_table*, Layout*, Sized_symbol<size>*,
+ Output_data_reloc<sh_type, true, size, big_endian>*);
+
+ // Save a reloc against SYM for possible emission later.
+ void
+ save(Symbol*, Sized_relobj_file<size, big_endian>*, unsigned int shndx,
+ Output_section*, const Reloc& rel);
+
+ // The target specific relocation type of the COPY relocation.
+ const unsigned int copy_reloc_type_;
+ // The dynamic BSS data which goes into the .bss section. This is
+ // where variables which require COPY relocations are placed.
+ Output_data_space* dynbss_;
+ // The list of relocs we are saving.
+ Copy_reloc_entries entries_;
+};
+
+
+// Return true if the symbol SYM should be considered to resolve local
+// to the current module, and false otherwise. The logic is taken from
+// GNU ld's method _bfd_elf_symbol_refs_local_p.
+static bool
+symbol_refs_local(const Symbol* sym, bool has_dynsym_entry,
+ bool local_protected)
+{
+ // If it's a local sym, of course we resolve locally.
+ if (sym == NULL)
+ return true;
+
+ // STV_HIDDEN or STV_INTERNAL ones must be local.
+ if (sym->visibility() == elfcpp::STV_HIDDEN
+ || sym->visibility() == elfcpp::STV_INTERNAL)
+ return true;
+
+ // If we don't have a definition in a regular file, then we can't
+ // resolve locally. The sym is either undefined or dynamic.
+ if (sym->source() != Symbol::FROM_OBJECT || sym->object()->is_dynamic()
+ || sym->is_undefined())
+ return false;
+
+ // Forced local symbols resolve locally.
+ if (sym->is_forced_local())
+ return true;
+
+ // As do non-dynamic symbols.
+ if (!has_dynsym_entry)
+ return true;
+
+ // At this point, we know the symbol is defined and dynamic. In an
+ // executable it must resolve locally, likewise when building symbolic
+ // shared libraries.
+ if (parameters->options().output_is_executable()
+ || parameters->options().Bsymbolic())
+ return true;
+
+ // Now deal with defined dynamic symbols in shared libraries. Ones
+ // with default visibility might not resolve locally.
+ if (sym->visibility() == elfcpp::STV_DEFAULT)
+ return false;
+
+ // STV_PROTECTED non-function symbols are local.
+ if (sym->type() != elfcpp::STT_FUNC)
+ return true;
+
+ // Function pointer equality tests may require that STV_PROTECTED
+ // symbols be treated as dynamic symbols. If the address of a
+ // function not defined in an executable is set to that function's
+ // plt entry in the executable, then the address of the function in
+ // a shared library must also be the plt entry in the executable.
+ return local_protected;
+}
+
+// Return TRUE if references to this symbol always reference the symbol in this
+// object.
+static bool
+symbol_references_local(const Symbol* sym, bool has_dynsym_entry)
+{
+ return symbol_refs_local(sym, has_dynsym_entry, false);
+}
+
+// Return TRUE if calls to this symbol always call the version in this object.
+static bool
+symbol_calls_local(const Symbol* sym, bool has_dynsym_entry)
+{
+ return symbol_refs_local(sym, has_dynsym_entry, true);
+}
+
+// Compare GOT offsets of two symbols.
+
+template<int size, bool big_endian>
+bool
+got_offset_compare(Symbol* sym1, Symbol* sym2)
+{
+ Mips_symbol<size>* mips_sym1 = Mips_symbol<size>::as_mips_sym(sym1);
+ Mips_symbol<size>* mips_sym2 = Mips_symbol<size>::as_mips_sym(sym2);
+ unsigned int area1 = mips_sym1->global_got_area();
+ unsigned int area2 = mips_sym2->global_got_area();
+ gold_assert(area1 != GGA_NONE && area1 != GGA_NONE);
+
+ // GGA_NORMAL entries always come before GGA_RELOC_ONLY.
+ if (area1 != area2)
+ return area1 < area2;
+
+ return mips_sym1->global_gotoffset() < mips_sym2->global_gotoffset();
+}
+
+// Functor class for processing the global symbol table.
+
+template<int size, bool big_endian>
+class Symbol_visitor_check_symbols
+{
+ public:
+ Symbol_visitor_check_symbols(Target_mips<size, big_endian>* target,
+ Layout* layout, Symbol_table* symtab)
+ : target_(target), layout_(layout), symtab_(symtab)
+ { }
+
+ void
+ operator()(Sized_symbol<size>* sym)
+ {
+ Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
+ if (this->target_->local_pic_function(mips_sym))
+ {
+ // SYM is a function that might need $25 to be valid on entry.
+ // If we're creating a non-PIC relocatable object, mark SYM as
+ // being PIC. If we're creating a non-relocatable object with
+ // non-PIC branches and jumps to SYM, make sure that SYM has an la25
+ // stub.
+ if (parameters->options().relocatable())
+ {
+ if (!parameters->options().output_is_position_independent())
+ mips_sym->set_pic();
+ }
+ else if (mips_sym->has_nonpic_branches())
+ {
+ this->target_->la25_stub_section(layout_)
+ ->create_la25_stub(this->symtab_, this->target_, mips_sym);
+ }
+ }
+ }
+
+ private:
+ Target_mips<size, big_endian>* target_;
+ Layout* layout_;
+ Symbol_table* symtab_;
+};
+
+template<int size, bool big_endian>
+class Target_mips : public Sized_target<size, big_endian>
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+ typedef Output_data_reloc<elfcpp::SHT_REL, true, size, big_endian>
+ Reloc_section;
+ typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, big_endian>
+ Reloca_section;
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
+
+ public:
+ Target_mips(const Target::Target_info* info = &mips_info)
+ : Sized_target<size, big_endian>(info), got_(NULL), gp_(NULL), plt_(NULL),
+ got_plt_(NULL), rel_dyn_(NULL), rela_dyn_(NULL),
+ copy_relocs_(elfcpp::R_MIPS_COPY), copy_relocsa_(elfcpp::R_MIPS_COPY),
+ dyn_relocs_(), la25_stub_(NULL), mips_mach_extensions_(),
+ mips_stubs_(NULL), ei_class_(0), mach_(0), layout_(NULL),
+ got16_addends_(), entry_symbol_is_compressed_(false), insn32_(false)
+ {
+ this->add_machine_extensions();
+ }
+
+ // The offset of $gp from the beginning of the .got section.
+ static const unsigned int MIPS_GP_OFFSET = 0x7ff0;
+
+ // The maximum size of the GOT for it to be addressable using 16-bit
+ // offsets from $gp.
+ static const unsigned int MIPS_GOT_MAX_SIZE = MIPS_GP_OFFSET + 0x7fff;
+
+ // Make a new symbol table entry for the Mips target.
+ Sized_symbol<size>*
+ make_symbol() const
+ { return new Mips_symbol<size>(); }
+
+ // 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*);
+
+ // 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,
+ Mips_address view_address,
+ section_size_type view_size,
+ const Reloc_symbol_changes*);
+
+ // Scan the relocs during a relocatable link.
+ void
+ scan_relocatable_relocs(Symbol_table* symtab,
+ Layout* layout,
+ Sized_relobj_file<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*);
+
+ // Emit relocations for a section.
+ void
+ relocate_relocs(const Relocate_info<size, big_endian>*,
+ unsigned int sh_type,
+ const unsigned char* prelocs,
+ size_t reloc_count,
+ Output_section* output_section,
+ typename elfcpp::Elf_types<size>::Elf_Off
+ offset_in_output_section,
+ const Relocatable_relocs*,
+ unsigned char* view,
+ Mips_address 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,
+ typename elfcpp::Elf_types<size>::Elf_Off
+ offset_in_output_section,
+ unsigned char* view,
+ Mips_address 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() / (size / 8);
+ }
+
+ // 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 this->plt_->first_plt_entry_offset(); }
+
+ // Return the size of each PLT entry.
+ unsigned int
+ plt_entry_size() const
+ { return this->plt_->plt_entry_size(); }
+
+ // Return TRUE if a relocation of type R_TYPE from OBJECT might
+ // require an la25 stub. See also local_pic_function, which determines
+ // whether the destination function ever requires a stub.
+ bool
+ relocation_needs_la25_stub(Mips_relobj<size, big_endian>* object,
+ unsigned int r_type, bool target_is_16_bit_code)
+ {
+ // We specifically ignore branches and jumps from EF_PIC objects,
+ // where the onus is on the compiler or programmer to perform any
+ // necessary initialization of $25. Sometimes such initialization
+ // is unnecessary; for example, -mno-shared functions do not use
+ // the incoming value of $25, and may therefore be called directly.
+ if (object->is_pic())
+ return false;
+
+ switch (r_type)
+ {
+ case elfcpp::R_MIPS_26:
+ case elfcpp::R_MIPS_PC16:
+ case elfcpp::R_MICROMIPS_26_S1:
+ case elfcpp::R_MICROMIPS_PC7_S1:
+ case elfcpp::R_MICROMIPS_PC10_S1:
+ case elfcpp::R_MICROMIPS_PC16_S1:
+ case elfcpp::R_MICROMIPS_PC23_S2:
+ return true;
+
+ case elfcpp::R_MIPS16_26:
+ return !target_is_16_bit_code;
+
+ default:
+ return false;
+ }
+ }
+
+ // Return true if SYM is a locally-defined PIC function, in the sense
+ // that it or its fn_stub might need $25 to be valid on entry.
+ // Note that MIPS16 functions set up $gp using PC-relative instructions,
+ // so they themselves never need $25 to be valid. Only non-MIPS16
+ // entry points are of interest here.
+ bool
+ local_pic_function(Mips_symbol<size>* sym)
+ {
+ bool def_regular = (sym->source() == Symbol::FROM_OBJECT
+ && !sym->object()->is_dynamic()
+ && !sym->is_undefined());
+
+ if (sym->is_defined() && def_regular)
+ {
+ Mips_relobj<size, big_endian>* object =
+ static_cast<Mips_relobj<size, big_endian>*>(sym->object());
+
+ if ((object->is_pic() || sym->is_pic())
+ && (!sym->is_mips16()
+ || (sym->has_mips16_fn_stub() && sym->need_fn_stub())))
+ return true;
+ }
+ return false;
+ }
+
+ // Get the GOT section, creating it if necessary.
+ Mips_output_data_got<size, big_endian>*
+ got_section(Symbol_table*, Layout*);
+
+ // Get the GOT section.
+ Mips_output_data_got<size, big_endian>*
+ got_section() const
+ {
+ gold_assert(this->got_ != NULL);
+ return this->got_;
+ }
+
+ // Get the .MIPS.stubs section, creating it if necessary.
+ Mips_output_data_mips_stubs<size, big_endian>*
+ mips_stubs_section(Layout* layout);
+
+ // Get the .MIPS.stubs section.
+ Mips_output_data_mips_stubs<size, big_endian>*
+ mips_stubs_section() const
+ {
+ gold_assert(this->mips_stubs_ != NULL);
+ return this->mips_stubs_;
+ }
+
+ // Get the LA25 stub section, creating it if necessary.
+ Mips_output_data_la25_stub<size, big_endian>*
+ la25_stub_section(Layout*);
+
+ // Get the LA25 stub section.
+ Mips_output_data_la25_stub<size, big_endian>*
+ la25_stub_section()
+ {
+ gold_assert(this->la25_stub_ != NULL);
+ return this->la25_stub_;
+ }
+
+ // Get gp value. It has the value of .got + 0x7FF0.
+ Mips_address
+ gp_value() const
+ {
+ if (this->gp_ != NULL)
+ return this->gp_->value();
+ return 0;
+ }
+
+ // Get gp value. It has the value of .got + 0x7FF0. Adjust it for
+ // multi-GOT links so that OBJECT's GOT + 0x7FF0 is returned.
+ Mips_address
+ adjusted_gp_value(const Mips_relobj<size, big_endian>* object)
+ {
+ if (this->gp_ == NULL)
+ return 0;
+
+ bool multi_got = false;
+ if (this->has_got_section())
+ multi_got = this->got_section()->multi_got();
+ if (!multi_got)
+ return this->gp_->value();
+ else
+ return this->gp_->value() + this->got_section()->get_got_offset(object);
+ }
+
+ // Get the dynamic reloc section, creating it if necessary.
+ Reloc_section*
+ rel_dyn_section(Layout*);
+
+ // Get the dynamic rela reloc section, creating it if necessary.
+ Reloca_section*
+ rela_dyn_section(Layout*);
+
+ static inline bool
+ hi16_reloc(int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_HI16
+ || r_type == elfcpp::R_MIPS16_HI16
+ || r_type == elfcpp::R_MICROMIPS_HI16);
+ }
+
+ static inline bool
+ lo16_reloc(int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_LO16
+ || r_type == elfcpp::R_MIPS16_LO16
+ || r_type == elfcpp::R_MICROMIPS_LO16);
+ }
+
+ static inline bool
+ got16_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_GOT16
+ || r_type == elfcpp::R_MIPS16_GOT16
+ || r_type == elfcpp::R_MICROMIPS_GOT16);
+ }
+
+ static inline bool
+ call_lo16_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_CALL_LO16
+ || r_type == elfcpp::R_MICROMIPS_CALL_LO16);
+ }
+
+ static inline bool
+ got_lo16_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_GOT_LO16
+ || r_type == elfcpp::R_MICROMIPS_GOT_LO16);
+ }
+
+ static inline bool
+ got_disp_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_GOT_DISP
+ || r_type == elfcpp::R_MICROMIPS_GOT_DISP);
+ }
+
+ static inline bool
+ got_page_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_GOT_PAGE
+ || r_type == elfcpp::R_MICROMIPS_GOT_PAGE);
+ }
+
+ static inline bool
+ tls_gd_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_TLS_GD
+ || r_type == elfcpp::R_MIPS16_TLS_GD
+ || r_type == elfcpp::R_MICROMIPS_TLS_GD);
+ }
+
+ static inline bool
+ tls_gottprel_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_TLS_GOTTPREL
+ || r_type == elfcpp::R_MIPS16_TLS_GOTTPREL
+ || r_type == elfcpp::R_MICROMIPS_TLS_GOTTPREL);
+ }
+
+ static inline bool
+ tls_ldm_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_TLS_LDM
+ || r_type == elfcpp::R_MIPS16_TLS_LDM
+ || r_type == elfcpp::R_MICROMIPS_TLS_LDM);
+ }
+
+ static inline bool
+ mips16_call_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS16_26
+ || r_type == elfcpp::R_MIPS16_CALL16);
+ }
+
+ static inline bool
+ jal_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MIPS_26
+ || r_type == elfcpp::R_MIPS16_26
+ || r_type == elfcpp::R_MICROMIPS_26_S1);
+ }
+
+ static inline bool
+ micromips_branch_reloc(unsigned int r_type)
+ {
+ return (r_type == elfcpp::R_MICROMIPS_26_S1
+ || r_type == elfcpp::R_MICROMIPS_PC16_S1
+ || r_type == elfcpp::R_MICROMIPS_PC10_S1
+ || r_type == elfcpp::R_MICROMIPS_PC7_S1);
+ }
+
+ // Check if R_TYPE is a MIPS16 reloc.
+ static inline bool
+ mips16_reloc(unsigned int r_type)
+ {
+ switch (r_type)
+ {
+ case elfcpp::R_MIPS16_26:
+ case elfcpp::R_MIPS16_GPREL:
+ case elfcpp::R_MIPS16_GOT16:
+ case elfcpp::R_MIPS16_CALL16:
+ case elfcpp::R_MIPS16_HI16:
+ case elfcpp::R_MIPS16_LO16:
+ case elfcpp::R_MIPS16_TLS_GD:
+ case elfcpp::R_MIPS16_TLS_LDM:
+ case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
+ case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
+ case elfcpp::R_MIPS16_TLS_GOTTPREL:
+ case elfcpp::R_MIPS16_TLS_TPREL_HI16:
+ case elfcpp::R_MIPS16_TLS_TPREL_LO16:
+ return true;
+
+ default:
+ return false;
+ }
+ }
+
+ // Check if R_TYPE is a microMIPS reloc.
+ static inline bool
+ micromips_reloc(unsigned int r_type)
+ {
+ switch (r_type)
+ {
+ case elfcpp::R_MICROMIPS_26_S1:
+ case elfcpp::R_MICROMIPS_HI16:
+ case elfcpp::R_MICROMIPS_LO16:
+ case elfcpp::R_MICROMIPS_GPREL16:
+ case elfcpp::R_MICROMIPS_LITERAL:
+ case elfcpp::R_MICROMIPS_GOT16:
+ case elfcpp::R_MICROMIPS_PC7_S1:
+ case elfcpp::R_MICROMIPS_PC10_S1:
+ case elfcpp::R_MICROMIPS_PC16_S1:
+ case elfcpp::R_MICROMIPS_CALL16:
+ case elfcpp::R_MICROMIPS_GOT_DISP:
+ case elfcpp::R_MICROMIPS_GOT_PAGE:
+ case elfcpp::R_MICROMIPS_GOT_OFST:
+ case elfcpp::R_MICROMIPS_GOT_HI16:
+ case elfcpp::R_MICROMIPS_GOT_LO16:
+ case elfcpp::R_MICROMIPS_SUB:
+ case elfcpp::R_MICROMIPS_HIGHER:
+ case elfcpp::R_MICROMIPS_HIGHEST:
+ case elfcpp::R_MICROMIPS_CALL_HI16:
+ case elfcpp::R_MICROMIPS_CALL_LO16:
+ case elfcpp::R_MICROMIPS_SCN_DISP:
+ case elfcpp::R_MICROMIPS_JALR:
+ case elfcpp::R_MICROMIPS_HI0_LO16:
+ case elfcpp::R_MICROMIPS_TLS_GD:
+ case elfcpp::R_MICROMIPS_TLS_LDM:
+ case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
+ case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
+ case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
+ case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
+ case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
+ case elfcpp::R_MICROMIPS_GPREL7_S2:
+ case elfcpp::R_MICROMIPS_PC23_S2:
+ return true;
+
+ default:
+ return false;
+ }
+ }
+
+ bool
+ has_custom_set_dynsym_indexes() const
+ { return true; }
+
+ void
+ reorder_dyn_symbols(std::vector<Symbol*>* dyn_symbols,
+ std::vector<Symbol*>* non_got_symbols,
+ std::vector<Symbol*>* got_symbols) const
+ {
+ // Mips ABI requires that symbols with the GOT entry must be at the end of
+ // dynamic symbol table, and the order in dynamic symbol table must match
+ // the order in GOT.
+ for (std::vector<Symbol*>::iterator p = dyn_symbols->begin();
+ p != dyn_symbols->end();
+ ++p)
+ {
+ Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
+ if (mips_sym->global_got_area() == GGA_NORMAL
+ || mips_sym->global_got_area() == GGA_RELOC_ONLY)
+ got_symbols->push_back(mips_sym);
+ else
+ non_got_symbols->push_back(mips_sym);
+ }
+
+ std::sort(got_symbols->begin(), got_symbols->end(),
+ got_offset_compare<size, big_endian>);
+ }
+
+ // Set the dynamic symbol indexes. INDEX is the index of the first
+ // global dynamic symbol. Pointers to the symbols are stored into the
+ // vector SYMS. The names are added to DYNPOOL. This returns an
+ // updated dynamic symbol index.
+ unsigned int
+ set_dynsym_indexes(std::vector<Symbol*>* dyn_symbols, unsigned int index,
+ std::vector<Symbol*>* syms, Stringpool* dynpool,
+ Versions* versions, Symbol_table* symtab) const
+ {
+ std::vector<Symbol*> non_got_symbols;
+ std::vector<Symbol*> got_symbols;
+
+ reorder_dyn_symbols(dyn_symbols, &non_got_symbols, &got_symbols);
+
+ for (std::vector<Symbol*>::iterator p = non_got_symbols.begin();
+ p != non_got_symbols.end();
+ ++p)
+ {
+ Symbol* sym = *p;
+
+ // Note that SYM may already have a dynamic symbol index, since
+ // some symbols appear more than once in the symbol table, with
+ // and without a version.
+
+ if (!sym->has_dynsym_index())
+ {
+ sym->set_dynsym_index(index);
+ ++index;
+ syms->push_back(sym);
+ dynpool->add(sym->name(), false, NULL);
+
+ // Record any version information.
+ if (sym->version() != NULL)
+ versions->record_version(symtab, dynpool, sym);
+
+ // If the symbol is defined in a dynamic object and is
+ // referenced in a regular object, then mark the dynamic
+ // object as needed. This is used to implement --as-needed.
+ if (sym->is_from_dynobj() && sym->in_reg())
+ sym->object()->set_is_needed();
+ }
+ }
+
+ for (std::vector<Symbol*>::iterator p = got_symbols.begin();
+ p != got_symbols.end();
+ ++p)
+ {
+ Symbol* sym = *p;
+ if (!sym->has_dynsym_index())
+ {
+ // Record any version information.
+ if (sym->version() != NULL)
+ versions->record_version(symtab, dynpool, sym);
+ }
+ }
+
+ index = versions->finalize(symtab, index, syms);
+
+ int got_sym_count = 0;
+ for (std::vector<Symbol*>::iterator p = got_symbols.begin();
+ p != got_symbols.end();
+ ++p)
+ {
+ Symbol* sym = *p;
+
+ if (!sym->has_dynsym_index())
+ {
+ ++got_sym_count;
+ sym->set_dynsym_index(index);
+ ++index;
+ syms->push_back(sym);
+ dynpool->add(sym->name(), false, NULL);
+
+ // If the symbol is defined in a dynamic object and is
+ // referenced in a regular object, then mark the dynamic
+ // object as needed. This is used to implement --as-needed.
+ if (sym->is_from_dynobj() && sym->in_reg())
+ sym->object()->set_is_needed();
+ }
+ }
+
+ // Set index of the first symbol that has .got entry.
+ this->got_->set_first_global_got_dynsym_index(
+ got_sym_count > 0 ? index - got_sym_count : -1U);
+
+ if (this->mips_stubs_ != NULL)
+ this->mips_stubs_->set_dynsym_count(index);
+
+ return index;
+ }
+
+ // Remove .MIPS.stubs entry for a symbol.
+ void
+ remove_lazy_stub_entry(Mips_symbol<size>* sym)
+ {
+ if (this->mips_stubs_ != NULL)
+ this->mips_stubs_->remove_entry(sym);
+ }
+
+ // The value to write into got[1] for SVR4 targets, to identify it is
+ // a GNU object. The dynamic linker can then use got[1] to store the
+ // module pointer.
+ uint64_t
+ mips_elf_gnu_got1_mask()
+ {
+ if (this->is_output_n64())
+ return (uint64_t)1 << 63;
+ else
+ return 1 << 31;
+ }
+
+ // Whether the output has microMIPS code. This is valid only after
+ // merge_processor_specific_flags() is called.
+ bool
+ is_output_micromips() const
+ {
+ gold_assert(this->are_processor_specific_flags_set());
+ return elfcpp::is_micromips(this->processor_specific_flags());
+ }
+
+ // Whether the output uses N32 ABI. This is valid only after
+ // merge_processor_specific_flags() is called.
+ bool
+ is_output_n32() const
+ {
+ gold_assert(this->are_processor_specific_flags_set());
+ return elfcpp::abi_n32(this->processor_specific_flags());
+ }
+
+ // Whether the output uses N64 ABI. This is valid only after
+ // merge_processor_specific_flags() is called.
+ bool
+ is_output_n64() const
+ {
+ gold_assert(this->are_processor_specific_flags_set());
+ return elfcpp::abi_64(this->ei_class_);
+ }
+
+ // Whether the output uses NEWABI. This is valid only after
+ // merge_processor_specific_flags() is called.
+ bool
+ is_output_newabi() const
+ { return this->is_output_n32() || this->is_output_n64(); }
+
+ // Whether we can only use 32-bit microMIPS instructions.
+ bool
+ use_32bit_micromips_instructions() const
+ { return insn32_; }
+
+ 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 ELF object.
+ Object*
+ do_make_elf_object(const std::string&, Input_file*, off_t,
+ const elfcpp::Ehdr<size, big_endian>& ehdr);
+
+ Object*
+ do_make_elf_object(const std::string&, Input_file*, off_t,
+ const elfcpp::Ehdr<size, !big_endian>&)
+ { gold_unreachable(); }
+
+ // 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, big_endian>(name, type,
+ flags, this);
+ else
+ return new Output_section(name, type, flags);
+ }
+
+ // Adjust ELF file header.
+ void
+ do_adjust_elf_header(unsigned char* view, int len) const;
+
+ // Get the custom dynamic tag value.
+ unsigned int
+ dynamic_tag_custom_value(elfcpp::DT) const;
+
+ // Adjust the value written to the dynamic symbol table.
+ virtual void
+ adjust_dyn_symbol(const Symbol* sym, unsigned char* view) const
+ {
+ elfcpp::Sym<size, big_endian> isym(view);
+ elfcpp::Sym_write<size, big_endian> osym(view);
+ const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
+
+ // Keep dynamic compressed symbols odd. This allows the dynamic linker
+ // to treat compressed symbols like any other.
+ Mips_address value = isym.get_st_value();
+ if (mips_sym->is_mips16() && value != 0)
+ {
+ if (!mips_sym->has_mips16_fn_stub())
+ value |= 1;
+ else
+ {
+ // If we have a MIPS16 function with a stub, the dynamic symbol
+ // must refer to the stub, since only the stub uses the standard
+ // calling conventions. Stub contains MIPS32 code, so don't add +1
+ // in this case.
+
+ // There is a code which does this in the method
+ // Target_mips::do_dynsym_value, but that code will only be
+ // executed if the symbol is from dynobj.
+ // TODO(sasa): GNU ld also changes the value in non-dynamic symbol
+ // table.
+
+ Mips16_stub_section<size, big_endian>* fn_stub =
+ mips_sym->template get_mips16_fn_stub<big_endian>();
+ value = fn_stub->output_address();
+ osym.put_st_size(fn_stub->section_size());
+ }
+
+ osym.put_st_value(value);
+ osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
+ mips_sym->nonvis() - (elfcpp::STO_MIPS16 >> 2)));
+ }
+ else if ((mips_sym->is_micromips()
+ // Stubs are always microMIPS if there is any microMIPS code in
+ // the output.
+ || (this->is_output_micromips() && mips_sym->has_lazy_stub()))
+ && value != 0)
+ {
+ osym.put_st_value(value | 1);
+ osym.put_st_other(elfcpp::elf_st_other(sym->visibility(),
+ mips_sym->nonvis() - (elfcpp::STO_MICROMIPS >> 2)));
+ }
+ }
+
+ private:
+ // The class which scans relocations.
+ class Scan
+ {
+ public:
+ Scan()
+ { }
+
+ 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,
+ bool is_discarded);
+
+ 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::Rela<size, big_endian>& reloc, unsigned int r_type,
+ const elfcpp::Sym<size, big_endian>& lsym,
+ bool is_discarded);
+
+ 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::Rela<size, big_endian>* rela,
+ const elfcpp::Rel<size, big_endian>* rel,
+ unsigned int rel_type,
+ unsigned int r_type,
+ const elfcpp::Sym<size, big_endian>& lsym,
+ bool is_discarded);
+
+ 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 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::Rela<size, big_endian>& reloc, unsigned int r_type,
+ Symbol* gsym);
+
+ 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::Rela<size, big_endian>* rela,
+ const elfcpp::Rel<size, big_endian>* rel,
+ unsigned int rel_type,
+ 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; }
+
+ 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::Rela<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::Rela<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*);
+ };
+
+ // The class which implements relocation.
+ class Relocate
+ {
+ public:
+ Relocate()
+ { }
+
+ ~Relocate()
+ { }
+
+ // Return whether the R_MIPS_32 relocation needs to be applied.
+ inline bool
+ should_apply_r_mips_32_reloc(const Mips_symbol<size>* gsym,
+ unsigned int r_type,
+ Output_section* output_section,
+ Target_mips* target);
+
+ // 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::Rela<size, big_endian>*,
+ const elfcpp::Rel<size, big_endian>*,
+ unsigned int,
+ unsigned int, const Sized_symbol<size>*,
+ const Symbol_value<size>*,
+ unsigned char*,
+ Mips_address,
+ section_size_type);
+
+ inline bool
+ relocate(const Relocate_info<size, big_endian>*, Target_mips*,
+ Output_section*, size_t relnum,
+ const elfcpp::Rel<size, big_endian>&,
+ unsigned int, const Sized_symbol<size>*,
+ const Symbol_value<size>*,
+ unsigned char*,
+ Mips_address,
+ section_size_type);
+
+ inline bool
+ relocate(const Relocate_info<size, big_endian>*, Target_mips*,
+ Output_section*, size_t relnum,
+ const elfcpp::Rela<size, big_endian>&,
+ unsigned int, const Sized_symbol<size>*,
+ const Symbol_value<size>*,
+ unsigned char*,
+ Mips_address,
+ 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*);
+ };
+
+ // This POD class holds the dynamic relocations that should be emitted instead
+ // of R_MIPS_32, R_MIPS_REL32 and R_MIPS_64 relocations. We will emit these
+ // relocations if it turns out that the symbol does not have static
+ // relocations.
+ class Dyn_reloc
+ {
+ public:
+ Dyn_reloc(Mips_symbol<size>* sym, unsigned int r_type,
+ Mips_relobj<size, big_endian>* relobj, unsigned int shndx,
+ Output_section* output_section, Mips_address r_offset)
+ : sym_(sym), r_type_(r_type), relobj_(relobj),
+ shndx_(shndx), output_section_(output_section),
+ r_offset_(r_offset)
+ { }
+
+ // Emit this reloc if appropriate. This is called after we have
+ // scanned all the relocations, so we know whether the symbol has
+ // static relocations.
+ void
+ emit(Reloc_section* rel_dyn, Mips_output_data_got<size, big_endian>* got,
+ Symbol_table* symtab)
+ {
+ if (!this->sym_->has_static_relocs())
+ {
+ got->record_global_got_symbol(this->sym_, this->relobj_,
+ this->r_type_, true, false);
+ if (!symbol_references_local(this->sym_,
+ this->sym_->should_add_dynsym_entry(symtab)))
+ rel_dyn->add_global(this->sym_, this->r_type_,
+ this->output_section_, this->relobj_,
+ this->shndx_, this->r_offset_);
+ else
+ rel_dyn->add_symbolless_global_addend(this->sym_, this->r_type_,
+ this->output_section_, this->relobj_,
+ this->shndx_, this->r_offset_);
+ }
+ }
+
+ private:
+ Mips_symbol<size>* sym_;
+ unsigned int r_type_;
+ Mips_relobj<size, big_endian>* relobj_;
+ unsigned int shndx_;
+ Output_section* output_section_;
+ Mips_address r_offset_;
+ };
+
+ // 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; }
+
+ // Check whether the given ELF header flags describe a 32-bit binary.
+ bool
+ mips_32bit_flags(elfcpp::Elf_Word);
+
+ enum Mips_mach {
+ mach_mips3000 = 3000,
+ mach_mips3900 = 3900,
+ mach_mips4000 = 4000,
+ mach_mips4010 = 4010,
+ mach_mips4100 = 4100,
+ mach_mips4111 = 4111,
+ mach_mips4120 = 4120,
+ mach_mips4300 = 4300,
+ mach_mips4400 = 4400,
+ mach_mips4600 = 4600,
+ mach_mips4650 = 4650,
+ mach_mips5000 = 5000,
+ mach_mips5400 = 5400,
+ mach_mips5500 = 5500,
+ mach_mips6000 = 6000,
+ mach_mips7000 = 7000,
+ mach_mips8000 = 8000,
+ mach_mips9000 = 9000,
+ mach_mips10000 = 10000,
+ mach_mips12000 = 12000,
+ mach_mips14000 = 14000,
+ mach_mips16000 = 16000,
+ mach_mips16 = 16,
+ mach_mips5 = 5,
+ mach_mips_loongson_2e = 3001,
+ mach_mips_loongson_2f = 3002,
+ mach_mips_loongson_3a = 3003,
+ mach_mips_sb1 = 12310201, // octal 'SB', 01
+ mach_mips_octeon = 6501,
+ mach_mips_octeonp = 6601,
+ mach_mips_octeon2 = 6502,
+ mach_mips_xlr = 887682, // decimal 'XLR'
+ mach_mipsisa32 = 32,
+ mach_mipsisa32r2 = 33,
+ mach_mipsisa64 = 64,
+ mach_mipsisa64r2 = 65,
+ mach_mips_micromips = 96
+ };
+
+ // Return the MACH for a MIPS e_flags value.
+ unsigned int
+ elf_mips_mach(elfcpp::Elf_Word);
+
+ // Check whether machine EXTENSION is an extension of machine BASE.
+ bool
+ mips_mach_extends(unsigned int, unsigned int);
+
+ // Merge processor specific flags.
+ void
+ merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word,
+ unsigned char, bool);
+
+ // True if we are linking for CPUs that are faster if JAL is converted to BAL.
+ static inline bool
+ jal_to_bal()
+ { return false; }
+
+ // True if we are linking for CPUs that are faster if JALR is converted to
+ // BAL. This should be safe for all architectures. We enable this predicate
+ // for all CPUs.
+ static inline bool
+ jalr_to_bal()
+ { return true; }
+
+ // True if we are linking for CPUs that are faster if JR is converted to B.
+ // This should be safe for all architectures. We enable this predicate for
+ // all CPUs.
+ static inline bool
+ jr_to_b()
+ { return true; }
+
+ // 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 PLT entry for a global symbol referenced by r_type relocation.
+ void
+ make_plt_entry(Symbol_table*, Layout*, Mips_symbol<size>*,
+ unsigned int r_type);
+
+ // Get the PLT section.
+ 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_;
+ }
+
+ // 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));
+ }
+
+ // 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::Rela<size, big_endian>& reloc)
+ {
+ this->copy_relocsa_.copy_reloc(symtab, layout,
+ symtab->get_sized_symbol<size>(sym),
+ object, shndx, output_section,
+ reloc, this->rela_dyn_section(layout));
+ }
+
+ void
+ dynamic_reloc(Mips_symbol<size>* sym, unsigned int r_type,
+ Mips_relobj<size, big_endian>* relobj,
+ unsigned int shndx, Output_section* output_section,
+ Mips_address r_offset)
+ {
+ this->dyn_relocs_.push_back(Dyn_reloc(sym, r_type, relobj, shndx,
+ output_section, r_offset));
+ }
+
+ // Calculate value of _gp symbol.
+ void
+ set_gp(Layout*, Symbol_table*);
+
+ const char*
+ elf_mips_abi_name(elfcpp::Elf_Word e_flags, unsigned char ei_class);
+ const char*
+ elf_mips_mach_name(elfcpp::Elf_Word e_flags);
+
+ // Adds entries that describe how machines relate to one another. The entries
+ // are ordered topologically with MIPS I extensions listed last. First
+ // element is extension, second element is base.
+ void
+ add_machine_extensions()
+ {
+ // MIPS64r2 extensions.
+ this->add_extension(mach_mips_octeon2, mach_mips_octeonp);
+ this->add_extension(mach_mips_octeonp, mach_mips_octeon);
+ this->add_extension(mach_mips_octeon, mach_mipsisa64r2);
+
+ // MIPS64 extensions.
+ this->add_extension(mach_mipsisa64r2, mach_mipsisa64);
+ this->add_extension(mach_mips_sb1, mach_mipsisa64);
+ this->add_extension(mach_mips_xlr, mach_mipsisa64);
+ this->add_extension(mach_mips_loongson_3a, mach_mipsisa64);
+
+ // MIPS V extensions.
+ this->add_extension(mach_mipsisa64, mach_mips5);
+
+ // R10000 extensions.
+ this->add_extension(mach_mips12000, mach_mips10000);
+ this->add_extension(mach_mips14000, mach_mips10000);
+ this->add_extension(mach_mips16000, mach_mips10000);
+
+ // R5000 extensions. Note: the vr5500 ISA is an extension of the core
+ // vr5400 ISA, but doesn't include the multimedia stuff. It seems
+ // better to allow vr5400 and vr5500 code to be merged anyway, since
+ // many libraries will just use the core ISA. Perhaps we could add
+ // some sort of ASE flag if this ever proves a problem.
+ this->add_extension(mach_mips5500, mach_mips5400);
+ this->add_extension(mach_mips5400, mach_mips5000);
+
+ // MIPS IV extensions.
+ this->add_extension(mach_mips5, mach_mips8000);
+ this->add_extension(mach_mips10000, mach_mips8000);
+ this->add_extension(mach_mips5000, mach_mips8000);
+ this->add_extension(mach_mips7000, mach_mips8000);
+ this->add_extension(mach_mips9000, mach_mips8000);
+
+ // VR4100 extensions.
+ this->add_extension(mach_mips4120, mach_mips4100);
+ this->add_extension(mach_mips4111, mach_mips4100);
+
+ // MIPS III extensions.
+ this->add_extension(mach_mips_loongson_2e, mach_mips4000);
+ this->add_extension(mach_mips_loongson_2f, mach_mips4000);
+ this->add_extension(mach_mips8000, mach_mips4000);
+ this->add_extension(mach_mips4650, mach_mips4000);
+ this->add_extension(mach_mips4600, mach_mips4000);
+ this->add_extension(mach_mips4400, mach_mips4000);
+ this->add_extension(mach_mips4300, mach_mips4000);
+ this->add_extension(mach_mips4100, mach_mips4000);
+ this->add_extension(mach_mips4010, mach_mips4000);
+
+ // MIPS32 extensions.
+ this->add_extension(mach_mipsisa32r2, mach_mipsisa32);
+
+ // MIPS II extensions.
+ this->add_extension(mach_mips4000, mach_mips6000);
+ this->add_extension(mach_mipsisa32, mach_mips6000);
+
+ // MIPS I extensions.
+ this->add_extension(mach_mips6000, mach_mips3000);
+ this->add_extension(mach_mips3900, mach_mips3000);
+ }
+
+ // Add value to MIPS extenstions.
+ void
+ add_extension(unsigned int base, unsigned int extension)
+ {
+ std::pair<unsigned int, unsigned int> ext(base, extension);
+ this->mips_mach_extensions_.push_back(ext);
+ }
+
+ // 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_;
+ // gp symbol. It has the value of .got + 0x7FF0.
+ Sized_symbol<size>* gp_;
+ // 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_;
+ Reloca_section* rela_dyn_;
+ // Relocs saved to avoid a COPY reloc.
+ Mips_copy_relocs<elfcpp::SHT_REL, size, big_endian> copy_relocs_;
+ Mips_copy_relocs<elfcpp::SHT_RELA, size, big_endian> copy_relocsa_;
+
+ // A list of dyn relocs to be saved.
+ std::vector<Dyn_reloc> dyn_relocs_;
+
+ // The LA25 stub section.
+ Mips_output_data_la25_stub<size, big_endian>* la25_stub_;
+ // Architecture extensions.
+ std::vector<std::pair<unsigned int, unsigned int> > mips_mach_extensions_;
+ // .MIPS.stubs
+ Mips_output_data_mips_stubs<size, big_endian>* mips_stubs_;
+
+ unsigned char ei_class_;
+ unsigned int mach_;
+ Layout* layout_;
+
+ typename std::list<got16_addend<size, big_endian> > got16_addends_;
+
+ // Whether the entry symbol is mips16 or micromips.
+ bool entry_symbol_is_compressed_;
+
+ // Whether we can use only 32-bit microMIPS instructions.
+ // TODO(sasa): This should be a linker option.
+ bool insn32_;
+};
+
+
+// Helper structure for R_MIPS*_HI16/LO16 and R_MIPS*_GOT16/LO16 relocations.
+// It records high part of the relocation pair.
+
+template<int size, bool big_endian>
+struct reloc_high
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+ reloc_high(unsigned char* _view, const Mips_relobj<size, big_endian>* _object,
+ const Symbol_value<size>* _psymval, Mips_address _addend,
+ unsigned int _r_type, bool _extract_addend,
+ Mips_address _address = 0, bool _gp_disp = false)
+ : view(_view), object(_object), psymval(_psymval), addend(_addend),
+ r_type(_r_type), extract_addend(_extract_addend), address(_address),
+ gp_disp(_gp_disp)
+ { }
+
+ unsigned char* view;
+ const Mips_relobj<size, big_endian>* object;
+ const Symbol_value<size>* psymval;
+ Mips_address addend;
+ unsigned int r_type;
+ bool extract_addend;
+ Mips_address address;
+ bool gp_disp;
+};
+
+template<int size, bool big_endian>
+class Mips_relocate_functions : public Relocate_functions<size, big_endian>
+{
+ typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+ typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype16;
+ typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype32;
+
+ 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;
+
+ static typename std::list<reloc_high<size, big_endian> > hi16_relocs;
+ static typename std::list<reloc_high<size, big_endian> > got16_relocs;
+
+ // R_MIPS16_26 is used for the mips16 jal and jalx instructions.
+ // Most mips16 instructions are 16 bits, but these instructions
+ // are 32 bits.
+ //
+ // The format of these instructions is:
+ //
+ // +--------------+--------------------------------+
+ // | JALX | X| Imm 20:16 | Imm 25:21 |
+ // +--------------+--------------------------------+
+ // | Immediate 15:0 |
+ // +-----------------------------------------------+
+ //
+ // JALX is the 5-bit value 00011. X is 0 for jal, 1 for jalx.
+ // Note that the immediate value in the first word is swapped.
+ //
+ // When producing a relocatable object file, R_MIPS16_26 is
+ // handled mostly like R_MIPS_26. In particular, the addend is
+ // stored as a straight 26-bit value in a 32-bit instruction.
+ // (gas makes life simpler for itself by never adjusting a
+ // R_MIPS16_26 reloc to be against a section, so the addend is
+ // always zero). However, the 32 bit instruction is stored as 2
+ // 16-bit values, rather than a single 32-bit value. In a
+ // big-endian file, the result is the same; in a little-endian
+ // file, the two 16-bit halves of the 32 bit value are swapped.
+ // This is so that a disassembler can recognize the jal
+ // instruction.
+ //
+ // When doing a final link, R_MIPS16_26 is treated as a 32 bit
+ // instruction stored as two 16-bit values. The addend A is the
+ // contents of the targ26 field. The calculation is the same as
+ // R_MIPS_26. When storing the calculated value, reorder the
+ // immediate value as shown above, and don't forget to store the
+ // value as two 16-bit values.
+ //
+ // To put it in MIPS ABI terms, the relocation field is T-targ26-16,
+ // defined as
+ //
+ // big-endian:
+ // +--------+----------------------+
+ // | | |
+ // | | targ26-16 |
+ // |31 26|25 0|
+ // +--------+----------------------+
+ //
+ // little-endian:
+ // +----------+------+-------------+
+ // | | | |
+ // | sub1 | | sub2 |
+ // |0 9|10 15|16 31|
+ // +----------+--------------------+
+ // where targ26-16 is sub1 followed by sub2 (i.e., the addend field A is
+ // ((sub1 << 16) | sub2)).
+ //
+ // When producing a relocatable object file, the calculation is
+ // (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
+ // When producing a fully linked file, the calculation is
+ // let R = (((A < 2) | ((P + 4) & 0xf0000000) + S) >> 2)
+ // ((R & 0x1f0000) << 5) | ((R & 0x3e00000) >> 5) | (R & 0xffff)
+ //
+ // The table below lists the other MIPS16 instruction relocations.
+ // Each one is calculated in the same way as the non-MIPS16 relocation
+ // given on the right, but using the extended MIPS16 layout of 16-bit
+ // immediate fields:
+ //
+ // R_MIPS16_GPREL R_MIPS_GPREL16
+ // R_MIPS16_GOT16 R_MIPS_GOT16
+ // R_MIPS16_CALL16 R_MIPS_CALL16
+ // R_MIPS16_HI16 R_MIPS_HI16
+ // R_MIPS16_LO16 R_MIPS_LO16
+ //
+ // A typical instruction will have a format like this:
+ //
+ // +--------------+--------------------------------+
+ // | EXTEND | Imm 10:5 | Imm 15:11 |
+ // +--------------+--------------------------------+
+ // | Major | rx | ry | Imm 4:0 |
+ // +--------------+--------------------------------+
+ //
+ // EXTEND is the five bit value 11110. Major is the instruction
+ // opcode.
+ //
+ // All we need to do here is shuffle the bits appropriately.
+ // As above, the two 16-bit halves must be swapped on a
+ // little-endian system.
+
+ // Similar to MIPS16, the two 16-bit halves in microMIPS must be swapped
+ // on a little-endian system. This does not apply to R_MICROMIPS_PC7_S1
+ // and R_MICROMIPS_PC10_S1 relocs that apply to 16-bit instructions.
+
+ static inline bool
+ micromips_reloc_shuffle(unsigned int r_type)
+ {
+ return (Target_mips<size, big_endian>::micromips_reloc(r_type)
+ && r_type != elfcpp::R_MICROMIPS_PC7_S1
+ && r_type != elfcpp::R_MICROMIPS_PC10_S1);
+ }
+
+ static void
+ mips_reloc_unshuffle(unsigned char* view, unsigned int r_type,
+ bool jal_shuffle)
+ {
+ if (!Target_mips<size, big_endian>::mips16_reloc(r_type)
+ && !micromips_reloc_shuffle(r_type))
+ return;
+
+ // Pick up the first and second halfwords of the instruction.
+ Valtype16 first = elfcpp::Swap<16, big_endian>::readval(view);
+ Valtype16 second = elfcpp::Swap<16, big_endian>::readval(view + 2);
+ Valtype32 val;
+
+ if (Target_mips<size, big_endian>::micromips_reloc(r_type)
+ || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
+ val = first << 16 | second;
+ else if (r_type != elfcpp::R_MIPS16_26)
+ val = (((first & 0xf800) << 16) | ((second & 0xffe0) << 11)
+ | ((first & 0x1f) << 11) | (first & 0x7e0) | (second & 0x1f));
+ else
+ val = (((first & 0xfc00) << 16) | ((first & 0x3e0) << 11)
+ | ((first & 0x1f) << 21) | second);
+
+ elfcpp::Swap<32, big_endian>::writeval(view, val);
+ }
+
+ static void
+ mips_reloc_shuffle(unsigned char* view, unsigned int r_type, bool jal_shuffle)
+ {
+ if (!Target_mips<size, big_endian>::mips16_reloc(r_type)
+ && !micromips_reloc_shuffle(r_type))
+ return;
+
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
+ Valtype16 first, second;
+
+ if (Target_mips<size, big_endian>::micromips_reloc(r_type)
+ || (r_type == elfcpp::R_MIPS16_26 && !jal_shuffle))
+ {
+ second = val & 0xffff;
+ first = val >> 16;
+ }
+ else if (r_type != elfcpp::R_MIPS16_26)
+ {
+ second = ((val >> 11) & 0xffe0) | (val & 0x1f);
+ first = ((val >> 16) & 0xf800) | ((val >> 11) & 0x1f) | (val & 0x7e0);
+ }
+ else
+ {
+ second = val & 0xffff;
+ first = ((val >> 16) & 0xfc00) | ((val >> 11) & 0x3e0)
+ | ((val >> 21) & 0x1f);
+ }
+
+ elfcpp::Swap<16, big_endian>::writeval(view + 2, second);
+ elfcpp::Swap<16, big_endian>::writeval(view, first);
+ }
+
+ public:
+ // R_MIPS_16: S + sign-extend(A)
+ static inline typename This::Status
+ rel16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_a,
+ bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype16* wv = reinterpret_cast<Valtype16*>(view);
+ Valtype16 val = elfcpp::Swap<16, big_endian>::readval(wv);
+
+ Valtype32 addend = (extract_addend ? Bits<16>::sign_extend32(val)
+ : Bits<16>::sign_extend32(addend_a));
+
+ Valtype32 x = psymval->value(object, addend);
+ val = Bits<16>::bit_select32(val, x, 0xffffU);
+ elfcpp::Swap<16, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<16>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MIPS_32: S + A
+ static inline typename This::Status
+ rel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_a,
+ bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 addend = (extract_addend
+ ? elfcpp::Swap<32, big_endian>::readval(wv)
+ : Bits<32>::sign_extend32(addend_a));
+ Valtype32 x = psymval->value(object, addend);
+ elfcpp::Swap<32, big_endian>::writeval(wv, x);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_JALR, R_MICROMIPS_JALR
+ static inline typename This::Status
+ reljalr(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address address,
+ bool cross_mode_jump, unsigned int r_type, bool jalr_to_bal,
+ bool jr_to_b)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ // Try converting J(AL)R to B(AL), if the target is in range.
+ if (!parameters->options().relocatable()
+ && r_type == elfcpp::R_MIPS_JALR
+ && !cross_mode_jump
+ && ((jalr_to_bal && val == 0x0320f809) // jalr t9
+ || (jr_to_b && val == 0x03200008))) // jr t9
+ {
+ int offset = psymval->value(object, 0) - (address + 4);
+ if (!Bits<18>::has_overflow32(offset))
+ {
+ if (val == 0x03200008) // jr t9
+ val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
+ else
+ val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
+ }
+ }
+
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_PC32: S + A - P
+ static inline typename This::Status
+ relpc32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address address,
+ Mips_address addend_a, bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 addend = (extract_addend
+ ? elfcpp::Swap<32, big_endian>::readval(wv)
+ : Bits<32>::sign_extend32(addend_a));
+ Valtype32 x = psymval->value(object, addend) - address;
+ elfcpp::Swap<32, big_endian>::writeval(wv, x);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_26, R_MIPS16_26, R_MICROMIPS_26_S1
+ static inline typename This::Status
+ rel26(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address address,
+ bool local, Mips_address addend_a, bool extract_addend,
+ const Symbol* gsym, bool cross_mode_jump, unsigned int r_type,
+ bool jal_to_bal)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend;
+ if (extract_addend)
+ {
+ if (r_type == elfcpp::R_MICROMIPS_26_S1)
+ addend = (val & 0x03ffffff) << 1;
+ else
+ addend = (val & 0x03ffffff) << 2;
+ }
+ else
+ addend = addend_a;
+
+ // Make sure the target of JALX is word-aligned. Bit 0 must be
+ // the correct ISA mode selector and bit 1 must be 0.
+ if (cross_mode_jump
+ && (psymval->value(object, 0) & 3) != (r_type == elfcpp::R_MIPS_26))
+ {
+ gold_warning(_("JALX to a non-word-aligned address"));
+ mips_reloc_shuffle(view, r_type, !parameters->options().relocatable());
+ return This::STATUS_BAD_RELOC;
+ }
+
+ // Shift is 2, unusually, for microMIPS JALX.
+ unsigned int shift =
+ (!cross_mode_jump && r_type == elfcpp::R_MICROMIPS_26_S1) ? 1 : 2;
+
+ Valtype32 x;
+ if (local)
+ x = addend | ((address + 4) & (0xfc000000 << shift));
+ else
+ {
+ if (shift == 1)
+ x = Bits<27>::sign_extend32(addend);
+ else
+ x = Bits<28>::sign_extend32(addend);
+ }
+ x = psymval->value(object, x) >> shift;
+
+ if (!local && !gsym->is_weak_undefined())
+ {
+ if ((x >> 26) != ((address + 4) >> (26 + shift)))
+ {
+ gold_error(_("relocation truncated to fit: %u against '%s'"),
+ r_type, gsym->name());
+ return This::STATUS_OVERFLOW;
+ }
+ }
+
+ val = Bits<32>::bit_select32(val, x, 0x03ffffff);
+
+ // If required, turn JAL into JALX.
+ if (cross_mode_jump)
+ {
+ bool ok;
+ Valtype32 opcode = val >> 26;
+ Valtype32 jalx_opcode;
+
+ // Check to see if the opcode is already JAL or JALX.
+ if (r_type == elfcpp::R_MIPS16_26)
+ {
+ ok = (opcode == 0x6) || (opcode == 0x7);
+ jalx_opcode = 0x7;
+ }
+ else if (r_type == elfcpp::R_MICROMIPS_26_S1)
+ {
+ ok = (opcode == 0x3d) || (opcode == 0x3c);
+ jalx_opcode = 0x3c;
+ }
+ else
+ {
+ ok = (opcode == 0x3) || (opcode == 0x1d);
+ jalx_opcode = 0x1d;
+ }
+
+ // If the opcode is not JAL or JALX, there's a problem. We cannot
+ // convert J or JALS to JALX.
+ if (!ok)
+ {
+ gold_error(_("Unsupported jump between ISA modes; consider "
+ "recompiling with interlinking enabled."));
+ return This::STATUS_BAD_RELOC;
+ }
+
+ // Make this the JALX opcode.
+ val = (val & ~(0x3f << 26)) | (jalx_opcode << 26);
+ }
+
+ // Try converting JAL to BAL, if the target is in range.
+ if (!parameters->options().relocatable()
+ && !cross_mode_jump
+ && ((jal_to_bal
+ && r_type == elfcpp::R_MIPS_26
+ && (val >> 26) == 0x3))) // jal addr
+ {
+ Valtype32 dest = (x << 2) | (((address + 4) >> 28) << 28);
+ int offset = dest - (address + 4);
+ if (!Bits<18>::has_overflow32(offset))
+ {
+ if (val == 0x03200008) // jr t9
+ val = 0x10000000 | (((Valtype32)offset >> 2) & 0xffff); // b addr
+ else
+ val = 0x04110000 | (((Valtype32)offset >> 2) & 0xffff); //bal addr
+ }
+ }
+
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, !parameters->options().relocatable());
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_PC16
+ static inline typename This::Status
+ relpc16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address address,
+ Mips_address addend_a, bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend = extract_addend ? (val & 0xffff) << 2 : addend_a;
+ addend = Bits<18>::sign_extend32(addend);
+
+ Valtype32 x = psymval->value(object, addend) - address;
+ val = Bits<16>::bit_select32(val, x >> 2, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<18>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MICROMIPS_PC7_S1
+ static inline typename This::Status
+ relmicromips_pc7_s1(unsigned char* view,
+ const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address address,
+ Mips_address addend_a, bool extract_addend,
+ unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend = extract_addend ? (val & 0x7f) << 1 : addend_a;
+ addend = Bits<8>::sign_extend32(addend);
+
+ Valtype32 x = psymval->value(object, addend) - address;
+ val = Bits<16>::bit_select32(val, x >> 1, 0x7f);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<8>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MICROMIPS_PC10_S1
+ static inline typename This::Status
+ relmicromips_pc10_s1(unsigned char* view,
+ const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address address,
+ Mips_address addend_a, bool extract_addend,
+ unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend = extract_addend ? (val & 0x3ff) << 1 : addend_a;
+ addend = Bits<11>::sign_extend32(addend);
+
+ Valtype32 x = psymval->value(object, addend) - address;
+ val = Bits<16>::bit_select32(val, x >> 1, 0x3ff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<11>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MICROMIPS_PC16_S1
+ static inline typename This::Status
+ relmicromips_pc16_s1(unsigned char* view,
+ const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address address,
+ Mips_address addend_a, bool extract_addend,
+ unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend = extract_addend ? (val & 0xffff) << 1 : addend_a;
+ addend = Bits<17>::sign_extend32(addend);
+
+ Valtype32 x = psymval->value(object, addend) - address;
+ val = Bits<16>::bit_select32(val, x >> 1, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<17>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
+ static inline typename This::Status
+ relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend,
+ Mips_address address, bool gp_disp, unsigned int r_type,
+ bool extract_addend)
+ {
+ // Record the relocation. It will be resolved when we find lo16 part.
+ hi16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
+ addend, r_type, extract_addend, address, gp_disp));
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_HI16, R_MIPS16_HI16, R_MICROMIPS_HI16,
+ static inline typename This::Status
+ do_relhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_hi,
+ Mips_address address, bool is_gp_disp, unsigned int r_type,
+ bool extract_addend, Valtype32 addend_lo,
+ Target_mips<size, big_endian>* target)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
+ : addend_hi);
+
+ Valtype32 value;
+ if (!is_gp_disp)
+ value = psymval->value(object, addend);
+ else
+ {
+ // For MIPS16 ABI code we generate this sequence
+ // 0: li $v0,%hi(_gp_disp)
+ // 4: addiupc $v1,%lo(_gp_disp)
+ // 8: sll $v0,16
+ // 12: addu $v0,$v1
+ // 14: move $gp,$v0
+ // So the offsets of hi and lo relocs are the same, but the
+ // base $pc is that used by the ADDIUPC instruction at $t9 + 4.
+ // ADDIUPC clears the low two bits of the instruction address,
+ // so the base is ($t9 + 4) & ~3.
+ Valtype32 gp_disp;
+ if (r_type == elfcpp::R_MIPS16_HI16)
+ gp_disp = (target->adjusted_gp_value(object)
+ - ((address + 4) & ~0x3));
+ // The microMIPS .cpload sequence uses the same assembly
+ // instructions as the traditional psABI version, but the
+ // incoming $t9 has the low bit set.
+ else if (r_type == elfcpp::R_MICROMIPS_HI16)
+ gp_disp = target->adjusted_gp_value(object) - address - 1;
+ else
+ gp_disp = target->adjusted_gp_value(object) - address;
+ value = gp_disp + addend;
+ }
+ Valtype32 x = ((value + 0x8000) >> 16) & 0xffff;
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (is_gp_disp && Bits<16>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
+ static inline typename This::Status
+ relgot16_local(unsigned char* view,
+ const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_a,
+ bool extract_addend, unsigned int r_type)
+ {
+ // Record the relocation. It will be resolved when we find lo16 part.
+ got16_relocs.push_back(reloc_high<size, big_endian>(view, object, psymval,
+ addend_a, r_type, extract_addend));
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
+ static inline typename This::Status
+ do_relgot16_local(unsigned char* view,
+ const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_hi,
+ unsigned int r_type, bool extract_addend,
+ Valtype32 addend_lo, Target_mips<size, big_endian>* target)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend = (extract_addend ? ((val & 0xffff) << 16) + addend_lo
+ : addend_hi);
+
+ // Find GOT page entry.
+ Mips_address value = ((psymval->value(object, addend) + 0x8000) >> 16)
+ & 0xffff;
+ value <<= 16;
+ unsigned int got_offset =
+ target->got_section()->get_got_page_offset(value, object);
+
+ // Resolve the relocation.
+ Valtype32 x = target->got_section()->gp_offset(got_offset, object);
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<16>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MIPS_LO16, R_MIPS16_LO16, R_MICROMIPS_LO16, R_MICROMIPS_HI0_LO16
+ static inline typename This::Status
+ rello16(Target_mips<size, big_endian>* target, unsigned char* view,
+ const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_a,
+ bool extract_addend, Mips_address address, bool is_gp_disp,
+ unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend = (extract_addend ? Bits<16>::sign_extend32(val & 0xffff)
+ : addend_a);
+
+ // Resolve pending R_MIPS_HI16 relocations.
+ typename std::list<reloc_high<size, big_endian> >::iterator it =
+ hi16_relocs.begin();
+ while (it != hi16_relocs.end())
+ {
+ reloc_high<size, big_endian> hi16 = *it;
+ if (hi16.psymval->value(hi16.object, 0) == psymval->value(object, 0))
+ {
+ if (do_relhi16(hi16.view, hi16.object, hi16.psymval, hi16.addend,
+ hi16.address, hi16.gp_disp, hi16.r_type,
+ hi16.extract_addend, addend, target)
+ == This::STATUS_OVERFLOW)
+ return This::STATUS_OVERFLOW;
+ it = hi16_relocs.erase(it);
+ }
+ else
+ ++it;
+ }
+
+ // Resolve pending local R_MIPS_GOT16 relocations.
+ typename std::list<reloc_high<size, big_endian> >::iterator it2 =
+ got16_relocs.begin();
+ while (it2 != got16_relocs.end())
+ {
+ reloc_high<size, big_endian> got16 = *it2;
+ if (got16.psymval->value(got16.object, 0) == psymval->value(object, 0))
+ {
+ if (do_relgot16_local(got16.view, got16.object, got16.psymval,
+ got16.addend, got16.r_type,
+ got16.extract_addend, addend,
+ target) == This::STATUS_OVERFLOW)
+ return This::STATUS_OVERFLOW;
+ it2 = got16_relocs.erase(it2);
+ }
+ else
+ ++it2;
+ }
+
+ // Resolve R_MIPS_LO16 relocation.
+ Valtype32 x;
+ if (!is_gp_disp)
+ x = psymval->value(object, addend);
+ else
+ {
+ // See the comment for R_MIPS16_HI16 above for the reason
+ // for this conditional.
+ Valtype32 gp_disp;
+ if (r_type == elfcpp::R_MIPS16_LO16)
+ gp_disp = target->adjusted_gp_value(object) - (address & ~0x3);
+ else if (r_type == elfcpp::R_MICROMIPS_LO16
+ || r_type == elfcpp::R_MICROMIPS_HI0_LO16)
+ gp_disp = target->adjusted_gp_value(object) - address + 3;
+ else
+ gp_disp = target->adjusted_gp_value(object) - address + 4;
+ // The MIPS ABI requires checking the R_MIPS_LO16 relocation
+ // for overflow. Relocations against _gp_disp are normally
+ // generated from the .cpload pseudo-op. It generates code
+ // that normally looks like this:
+
+ // lui $gp,%hi(_gp_disp)
+ // addiu $gp,$gp,%lo(_gp_disp)
+ // addu $gp,$gp,$t9
+
+ // Here $t9 holds the address of the function being called,
+ // as required by the MIPS ELF ABI. The R_MIPS_LO16
+ // relocation can easily overflow in this situation, but the
+ // R_MIPS_HI16 relocation will handle the overflow.
+ // Therefore, we consider this a bug in the MIPS ABI, and do
+ // not check for overflow here.
+ x = gp_disp + addend;
+ }
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_CALL16, R_MIPS16_CALL16, R_MICROMIPS_CALL16
+ // R_MIPS_GOT16, R_MIPS16_GOT16, R_MICROMIPS_GOT16
+ // R_MIPS_TLS_GD, R_MIPS16_TLS_GD, R_MICROMIPS_TLS_GD
+ // R_MIPS_TLS_GOTTPREL, R_MIPS16_TLS_GOTTPREL, R_MICROMIPS_TLS_GOTTPREL
+ // R_MIPS_TLS_LDM, R_MIPS16_TLS_LDM, R_MICROMIPS_TLS_LDM
+ // R_MIPS_GOT_DISP, R_MICROMIPS_GOT_DISP
+ static inline typename This::Status
+ relgot(unsigned char* view, int gp_offset, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+ Valtype32 x = gp_offset;
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<16>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MIPS_GOT_PAGE, R_MICROMIPS_GOT_PAGE
+ static inline typename This::Status
+ relgotpage(Target_mips<size, big_endian>* target, unsigned char* view,
+ const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_a,
+ bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
+ Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
+
+ // Find a GOT page entry that points to within 32KB of symbol + addend.
+ Mips_address value = (psymval->value(object, addend) + 0x8000) & ~0xffff;
+ unsigned int got_offset =
+ target->got_section()->get_got_page_offset(value, object);
+
+ Valtype32 x = target->got_section()->gp_offset(got_offset, object);
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<16>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MIPS_GOT_OFST, R_MICROMIPS_GOT_OFST
+ static inline typename This::Status
+ relgotofst(Target_mips<size, big_endian>* target, unsigned char* view,
+ const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_a,
+ bool extract_addend, bool local, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(view);
+ Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
+
+ // For a local symbol, find a GOT page entry that points to within 32KB of
+ // symbol + addend. Relocation value is the offset of the GOT page entry's
+ // value from symbol + addend.
+ // For a global symbol, relocation value is addend.
+ Valtype32 x;
+ if (local)
+ {
+ // Find GOT page entry.
+ Mips_address value = ((psymval->value(object, addend) + 0x8000)
+ & ~0xffff);
+ target->got_section()->get_got_page_offset(value, object);
+
+ x = psymval->value(object, addend) - value;
+ }
+ else
+ x = addend;
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return (Bits<16>::has_overflow32(x)
+ ? This::STATUS_OVERFLOW
+ : This::STATUS_OKAY);
+ }
+
+ // R_MIPS_GOT_HI16, R_MIPS_CALL_HI16,
+ // R_MICROMIPS_GOT_HI16, R_MICROMIPS_CALL_HI16
+ static inline typename This::Status
+ relgot_hi16(unsigned char* view, int gp_offset, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+ Valtype32 x = gp_offset;
+ x = ((x + 0x8000) >> 16) & 0xffff;
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_GOT_LO16, R_MIPS_CALL_LO16,
+ // R_MICROMIPS_GOT_LO16, R_MICROMIPS_CALL_LO16
+ static inline typename This::Status
+ relgot_lo16(unsigned char* view, int gp_offset, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+ Valtype32 x = gp_offset;
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_GPREL16, R_MIPS16_GPREL, R_MIPS_LITERAL, R_MICROMIPS_LITERAL
+ // R_MICROMIPS_GPREL7_S2, R_MICROMIPS_GPREL16
+ static inline typename This::Status
+ relgprel(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address gp,
+ Mips_address addend_a, bool extract_addend, bool local,
+ unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+
+ Valtype32 addend;
+ if (extract_addend)
+ {
+ if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
+ addend = (val & 0x7f) << 2;
+ else
+ addend = val & 0xffff;
+ // Only sign-extend the addend if it was extracted from the
+ // instruction. If the addend was separate, leave it alone,
+ // otherwise we may lose significant bits.
+ addend = Bits<16>::sign_extend32(addend);
+ }
+ else
+ addend = addend_a;
+
+ Valtype32 x = psymval->value(object, addend) - gp;
+
+ // If the symbol was local, any earlier relocatable links will
+ // have adjusted its addend with the gp offset, so compensate
+ // for that now. Don't do it for symbols forced local in this
+ // link, though, since they won't have had the gp offset applied
+ // to them before.
+ if (local)
+ x += object->gp_value();
+
+ if (r_type == elfcpp::R_MICROMIPS_GPREL7_S2)
+ val = Bits<32>::bit_select32(val, x, 0x7f);
+ else
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ if (Bits<16>::has_overflow32(x))
+ {
+ gold_error(_("small-data section exceeds 64KB; lower small-data size "
+ "limit (see option -G)"));
+ return This::STATUS_OVERFLOW;
+ }
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_GPREL32
+ static inline typename This::Status
+ relgprel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address gp,
+ Mips_address addend_a, bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+ Valtype32 addend = extract_addend ? val : addend_a;
+
+ // R_MIPS_GPREL32 relocations are defined for local symbols only.
+ Valtype32 x = psymval->value(object, addend) + object->gp_value() - gp;
+ elfcpp::Swap<32, big_endian>::writeval(wv, x);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_TLS_TPREL_HI16, R_MIPS16_TLS_TPREL_HI16, R_MICROMIPS_TLS_TPREL_HI16
+ // R_MIPS_TLS_DTPREL_HI16, R_MIPS16_TLS_DTPREL_HI16,
+ // R_MICROMIPS_TLS_DTPREL_HI16
+ static inline typename This::Status
+ tlsrelhi16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Valtype32 tp_offset,
+ Mips_address addend_a, bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+ Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
+
+ // tls symbol values are relative to tls_segment()->vaddr()
+ Valtype32 x = ((psymval->value(object, addend) - tp_offset) + 0x8000) >> 16;
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_TLS_TPREL_LO16, R_MIPS16_TLS_TPREL_LO16, R_MICROMIPS_TLS_TPREL_LO16,
+ // R_MIPS_TLS_DTPREL_LO16, R_MIPS16_TLS_DTPREL_LO16,
+ // R_MICROMIPS_TLS_DTPREL_LO16,
+ static inline typename This::Status
+ tlsrello16(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Valtype32 tp_offset,
+ Mips_address addend_a, bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+ Valtype32 addend = extract_addend ? val & 0xffff : addend_a;
+
+ // tls symbol values are relative to tls_segment()->vaddr()
+ Valtype32 x = psymval->value(object, addend) - tp_offset;
+ val = Bits<32>::bit_select32(val, x, 0xffff);
+ elfcpp::Swap<32, big_endian>::writeval(wv, val);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_TLS_TPREL32, R_MIPS_TLS_TPREL64,
+ // R_MIPS_TLS_DTPREL32, R_MIPS_TLS_DTPREL64
+ static inline typename This::Status
+ tlsrel32(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Valtype32 tp_offset,
+ Mips_address addend_a, bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+ Valtype32 addend = extract_addend ? val : addend_a;
+
+ // tls symbol values are relative to tls_segment()->vaddr()
+ Valtype32 x = psymval->value(object, addend) - tp_offset;
+ elfcpp::Swap<32, big_endian>::writeval(wv, x);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+
+ // R_MIPS_SUB, R_MICROMIPS_SUB
+ static inline typename This::Status
+ relsub(unsigned char* view, const Mips_relobj<size, big_endian>* object,
+ const Symbol_value<size>* psymval, Mips_address addend_a,
+ bool extract_addend, unsigned int r_type)
+ {
+ mips_reloc_unshuffle(view, r_type, false);
+ Valtype32* wv = reinterpret_cast<Valtype32*>(view);
+ Valtype32 val = elfcpp::Swap<32, big_endian>::readval(wv);
+ Valtype32 addend = extract_addend ? val : addend_a;
+
+ Valtype32 x = psymval->value(object, -addend);
+ elfcpp::Swap<32, big_endian>::writeval(wv, x);
+ mips_reloc_shuffle(view, r_type, false);
+ return This::STATUS_OKAY;
+ }
+};
+
+template<int size, bool big_endian>
+typename std::list<reloc_high<size, big_endian> >
+ Mips_relocate_functions<size, big_endian>::hi16_relocs;
+
+template<int size, bool big_endian>
+typename std::list<reloc_high<size, big_endian> >
+ Mips_relocate_functions<size, big_endian>::got16_relocs;
+
+// Mips_got_info methods.
+
+// Reserve GOT entry for a GOT relocation of type R_TYPE against symbol
+// SYMNDX + ADDEND, where SYMNDX is a local symbol in section SHNDX in OBJECT.
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::record_local_got_symbol(
+ Mips_relobj<size, big_endian>* object, unsigned int symndx,
+ Mips_address addend, unsigned int r_type, unsigned int shndx)
+{
+ Mips_got_entry<size, big_endian>* entry =
+ new Mips_got_entry<size, big_endian>(object, symndx, addend,
+ mips_elf_reloc_tls_type(r_type),
+ shndx);
+ this->record_got_entry(entry, object);
+}
+
+// Reserve GOT entry for a GOT relocation of type R_TYPE against MIPS_SYM,
+// in OBJECT. FOR_CALL is true if the caller is only interested in
+// using the GOT entry for calls. DYN_RELOC is true if R_TYPE is a dynamic
+// relocation.
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::record_global_got_symbol(
+ Mips_symbol<size>* mips_sym, Mips_relobj<size, big_endian>* object,
+ unsigned int r_type, bool dyn_reloc, bool for_call)
+{
+ if (!for_call)
+ mips_sym->set_got_not_only_for_calls();
+
+ // A global symbol in the GOT must also be in the dynamic symbol table.
+ if (!mips_sym->needs_dynsym_entry())
+ {
+ switch (mips_sym->visibility())
+ {
+ case elfcpp::STV_INTERNAL:
+ case elfcpp::STV_HIDDEN:
+ mips_sym->set_is_forced_local();
+ break;
+ default:
+ mips_sym->set_needs_dynsym_entry();
+ break;
+ }
+ }
+
+ unsigned char tls_type = mips_elf_reloc_tls_type(r_type);
+ if (tls_type == GOT_TLS_NONE)
+ this->global_got_symbols_.insert(mips_sym);
+
+ if (dyn_reloc)
+ {
+ if (mips_sym->global_got_area() == GGA_NONE)
+ mips_sym->set_global_got_area(GGA_RELOC_ONLY);
+ return;
+ }
+
+ Mips_got_entry<size, big_endian>* entry =
+ new Mips_got_entry<size, big_endian>(object, mips_sym, tls_type);
+
+ this->record_got_entry(entry, object);
+}
+
+// Add ENTRY to master GOT and to OBJECT's GOT.
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::record_got_entry(
+ Mips_got_entry<size, big_endian>* entry,
+ Mips_relobj<size, big_endian>* object)
+{
+ if (this->got_entries_.find(entry) == this->got_entries_.end())
+ this->got_entries_.insert(entry);
+
+ // Create the GOT entry for the OBJECT's GOT.
+ Mips_got_info<size, big_endian>* g = object->get_or_create_got_info();
+ Mips_got_entry<size, big_endian>* entry2 =
+ new Mips_got_entry<size, big_endian>(*entry);
+
+ if (g->got_entries_.find(entry2) == g->got_entries_.end())
+ g->got_entries_.insert(entry2);
+}
+
+// Record that OBJECT has a page relocation against symbol SYMNDX and
+// that ADDEND is the addend for that relocation.
+// This function creates an upper bound on the number of GOT slots
+// required; no attempt is made to combine references to non-overridable
+// global symbols across multiple input files.
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::record_got_page_entry(
+ Mips_relobj<size, big_endian>* object, unsigned int symndx, int addend)
+{
+ struct Got_page_range **range_ptr, *range;
+ int old_pages, new_pages;
+
+ // Find the Got_page_entry for this symbol.
+ Got_page_entry* entry = new Got_page_entry(object, symndx);
+ typename Got_page_entry_set::iterator it =
+ this->got_page_entries_.find(entry);
+ if (it != this->got_page_entries_.end())
+ entry = *it;
+ else
+ this->got_page_entries_.insert(entry);
+
+ // Add the same entry to the OBJECT's GOT.
+ Got_page_entry* entry2 = NULL;
+ Mips_got_info<size, big_endian>* g2 = object->get_or_create_got_info();
+ if (g2->got_page_entries_.find(entry) == g2->got_page_entries_.end())
+ {
+ entry2 = new Got_page_entry(*entry);
+ g2->got_page_entries_.insert(entry2);
+ }
+
+ // Skip over ranges whose maximum extent cannot share a page entry
+ // with ADDEND.
+ range_ptr = &entry->ranges;
+ while (*range_ptr && addend > (*range_ptr)->max_addend + 0xffff)
+ range_ptr = &(*range_ptr)->next;
+
+ // If we scanned to the end of the list, or found a range whose
+ // minimum extent cannot share a page entry with ADDEND, create
+ // a new singleton range.
+ range = *range_ptr;
+ if (!range || addend < range->min_addend - 0xffff)
+ {
+ range = new Got_page_range();
+ range->next = *range_ptr;
+ range->min_addend = addend;
+ range->max_addend = addend;
+
+ *range_ptr = range;
+ ++entry->num_pages;
+ if (entry2 != NULL)
+ ++entry2->num_pages;
+ ++this->page_gotno_;
+ ++g2->page_gotno_;
+ return;
+ }
+
+ // Remember how many pages the old range contributed.
+ old_pages = range->get_max_pages();
+
+ // Update the ranges.
+ if (addend < range->min_addend)
+ range->min_addend = addend;
+ else if (addend > range->max_addend)
+ {
+ if (range->next && addend >= range->next->min_addend - 0xffff)
+ {
+ old_pages += range->next->get_max_pages();
+ range->max_addend = range->next->max_addend;
+ range->next = range->next->next;
+ }
+ else
+ range->max_addend = addend;
+ }
+
+ // Record any change in the total estimate.
+ new_pages = range->get_max_pages();
+ if (old_pages != new_pages)
+ {
+ entry->num_pages += new_pages - old_pages;
+ if (entry2 != NULL)
+ entry2->num_pages += new_pages - old_pages;
+ this->page_gotno_ += new_pages - old_pages;
+ g2->page_gotno_ += new_pages - old_pages;
+ }
+}
+
+// Create all entries that should be in the local part of the GOT.
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::add_local_entries(
+ Target_mips<size, big_endian>* target, Layout* layout)
+{
+ Mips_output_data_got<size, big_endian>* got = target->got_section();
+ // First two GOT entries are reserved. The first entry will be filled at
+ // runtime. The second entry will be used by some runtime loaders.
+ got->add_constant(0);
+ got->add_constant(target->mips_elf_gnu_got1_mask());
+
+ for (typename Got_entry_set::iterator
+ p = this->got_entries_.begin();
+ p != this->got_entries_.end();
+ ++p)
+ {
+ Mips_got_entry<size, big_endian>* entry = *p;
+ if (entry->is_for_local_symbol() && !entry->is_tls_entry())
+ {
+ got->add_local(entry->object(), entry->symndx(),
+ GOT_TYPE_STANDARD);
+ unsigned int got_offset = entry->object()->local_got_offset(
+ entry->symndx(), GOT_TYPE_STANDARD);
+ if (got->multi_got() && this->index_ > 0
+ && parameters->options().output_is_position_independent())
+ target->rel_dyn_section(layout)->add_local(entry->object(),
+ entry->symndx(), elfcpp::R_MIPS_REL32, got, got_offset);
+ }
+ }
+
+ this->add_page_entries(target, layout);
+
+ // Add global entries that should be in the local area.
+ for (typename Got_entry_set::iterator
+ p = this->got_entries_.begin();
+ p != this->got_entries_.end();
+ ++p)
+ {
+ Mips_got_entry<size, big_endian>* entry = *p;
+ if (!entry->is_for_global_symbol())
+ continue;
+
+ Mips_symbol<size>* mips_sym = entry->sym();
+ if (mips_sym->global_got_area() == GGA_NONE && !entry->is_tls_entry())
+ {
+ unsigned int got_type;
+ if (!got->multi_got())
+ got_type = GOT_TYPE_STANDARD;
+ else
+ got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
+ if (got->add_global(mips_sym, got_type))
+ {
+ mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
+ if (got->multi_got() && this->index_ > 0
+ && parameters->options().output_is_position_independent())
+ target->rel_dyn_section(layout)->add_symbolless_global_addend(
+ mips_sym, elfcpp::R_MIPS_REL32, got,
+ mips_sym->got_offset(got_type));
+ }
+ }
+ }
+}
+
+// Create GOT page entries.
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::add_page_entries(
+ Target_mips<size, big_endian>* target, Layout* layout)
+{
+ if (this->page_gotno_ == 0)
+ return;
+
+ Mips_output_data_got<size, big_endian>* got = target->got_section();
+ this->got_page_offset_start_ = got->add_constant(0);
+ if (got->multi_got() && this->index_ > 0
+ && parameters->options().output_is_position_independent())
+ target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
+ this->got_page_offset_start_);
+ int num_entries = this->page_gotno_;
+ unsigned int prev_offset = this->got_page_offset_start_;
+ while (--num_entries > 0)
+ {
+ unsigned int next_offset = got->add_constant(0);
+ if (got->multi_got() && this->index_ > 0
+ && parameters->options().output_is_position_independent())
+ target->rel_dyn_section(layout)->add_absolute(elfcpp::R_MIPS_REL32, got,
+ next_offset);
+ gold_assert(next_offset == prev_offset + size/8);
+ prev_offset = next_offset;
+ }
+ this->got_page_offset_next_ = this->got_page_offset_start_;
+}
+
+// Create global GOT entries, both GGA_NORMAL and GGA_RELOC_ONLY.
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::add_global_entries(
+ Target_mips<size, big_endian>* target, Layout* layout,
+ unsigned int non_reloc_only_global_gotno)
+{
+ Mips_output_data_got<size, big_endian>* got = target->got_section();
+ // Add GGA_NORMAL entries.
+ unsigned int count = 0;
+ for (typename Got_entry_set::iterator
+ p = this->got_entries_.begin();
+ p != this->got_entries_.end();
+ ++p)
+ {
+ Mips_got_entry<size, big_endian>* entry = *p;
+ if (!entry->is_for_global_symbol())
+ continue;
+
+ Mips_symbol<size>* mips_sym = entry->sym();
+ if (mips_sym->global_got_area() != GGA_NORMAL)
+ continue;
+
+ unsigned int got_type;
+ if (!got->multi_got())
+ got_type = GOT_TYPE_STANDARD;
+ else
+ // In multi-GOT links, global symbol can be in both primary and
+ // secondary GOT(s). By creating custom GOT type
+ // (GOT_TYPE_STANDARD_MULTIGOT + got_index) we ensure that symbol
+ // is added to secondary GOT(s).
+ got_type = GOT_TYPE_STANDARD_MULTIGOT + this->index_;
+ if (!got->add_global(mips_sym, got_type))
+ continue;
+
+ mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
+ if (got->multi_got() && this->index_ == 0)
+ count++;
+ if (got->multi_got() && this->index_ > 0)
+ {
+ if (parameters->options().output_is_position_independent()
+ || (!parameters->doing_static_link()
+ && mips_sym->is_from_dynobj() && !mips_sym->is_undefined()))
+ {
+ target->rel_dyn_section(layout)->add_global(
+ mips_sym, elfcpp::R_MIPS_REL32, got,
+ mips_sym->got_offset(got_type));
+ got->add_secondary_got_reloc(mips_sym->got_offset(got_type),
+ elfcpp::R_MIPS_REL32, mips_sym);
+ }
+ }
+ }
+
+ if (!got->multi_got() || this->index_ == 0)
+ {
+ if (got->multi_got())
+ {
+ // We need to allocate space in the primary GOT for GGA_NORMAL entries
+ // of secondary GOTs, to ensure that GOT offsets of GGA_RELOC_ONLY
+ // entries correspond to dynamic symbol indexes.
+ while (count < non_reloc_only_global_gotno)
+ {
+ got->add_constant(0);
+ ++count;
+ }
+ }
+
+ // Add GGA_RELOC_ONLY entries.
+ got->add_reloc_only_entries();
+ }
+}
+
+// Create global GOT entries that should be in the GGA_RELOC_ONLY area.
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::add_reloc_only_entries(
+ Mips_output_data_got<size, big_endian>* got)
+{
+ for (typename Unordered_set<Mips_symbol<size>*>::iterator
+ p = this->global_got_symbols_.begin();
+ p != this->global_got_symbols_.end();
+ ++p)
+ {
+ Mips_symbol<size>* mips_sym = *p;
+ if (mips_sym->global_got_area() == GGA_RELOC_ONLY)
+ {
+ unsigned int got_type;
+ if (!got->multi_got())
+ got_type = GOT_TYPE_STANDARD;
+ else
+ got_type = GOT_TYPE_STANDARD_MULTIGOT;
+ if (got->add_global(mips_sym, got_type))
+ mips_sym->set_global_gotoffset(mips_sym->got_offset(got_type));
+ }
+ }
+}