This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Mips target in gold - part 2


 Hi all,

Please find attached second of the patches that implement Mips target in  gold. This patch contains second part of the mips.cc file. (mips.cc is split in two parts because binutils mailing list doesn't accept messages larger than 400000 bytes.)

Regards,
Sasa
diff --git a/gold/mips.cc b/gold/mips.cc
index a380560..c558457 100644
--- a/gold/mips.cc
+++ b/gold/mips.cc
@@ -4440,3 +4440,4923 @@ 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 space for a GOT entry containing the value of symbol
+// SYMNDX in input object OBJECT, plus ADDEND.
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::record_local_got_symbol(
+    Sized_relobj_file<size, big_endian>* object,
+    unsigned int symndx,
+    typename elfcpp::Elf_types<size>::Elf_Addr 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);
+
+  typename Got_entry_set::iterator it = this->got_entries_.find(entry);
+  if (it != this->got_entries_.end())
+    return;
+
+  this->record_got_entry(object, entry);
+}
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::record_global_got_symbol(
+    Mips_symbol<size>* mips_sym,
+    Sized_relobj_file<size, big_endian>* object,
+    unsigned int r_type, bool reloc, bool for_call)
+{
+  if (!for_call)
+    mips_sym->set_got_only_for_calls(false);
+
+  // 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 (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(object, entry);
+
+  //if (tls_type == 0)
+    //mips_sym->set_global_got_area(GGA_NORMAL);
+}
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::record_got_entry(
+    Sized_relobj_file<size, big_endian> *object,
+    Mips_got_entry<size, big_endian> *entry)
+{
+  if (this->got_entries_.find(entry) == this->got_entries_.end())
+    this->got_entries_.insert(entry);
+
+  // Reuse the same GOT entry for the OBJECT's GOT.
+  Mips_got_info<size, big_endian> *g = this->get_got_info(object, true);
+  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);
+}
+
+template<int size, bool big_endian>
+Mips_got_info<size, big_endian>*
+Mips_got_info<size, big_endian>::get_got_info(
+    const Sized_relobj_file<size, big_endian> *object, bool create)
+{
+  Mips_relobj<size, big_endian>* mips_relobj =
+    Mips_relobj<size, big_endian>::as_mips_relobj(
+      const_cast<Sized_relobj_file<size, big_endian>*>(object));
+
+  if (!mips_relobj->get_got_info() && create)
+    mips_relobj->set_got_info(new Mips_got_info<size, big_endian>());
+  return mips_relobj->get_got_info();
+}
+
+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->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 which 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->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));
+            }
+        }
+    }
+}
+
+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_;
+}
+
+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
+        // For multi-got links, global symbol can be in both primary and
+        // secondary got(s), so we need to define custom got type
+        // (GOT_TYPE_STANDARD_MULTIGOT + got-index) to 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();
+    }
+}
+
+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));
+        }
+    }
+}
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::add_tls_entries(
+    Target_mips<size, big_endian> *target, Layout* layout)
+{
+  Mips_output_data_got<size, big_endian> *got = target->got_section();
+  // Add local tls entries.
+  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->tls_entry() || !entry->is_for_local_symbol())
+        continue;
+
+      if (entry->tls_type() == GOT_TLS_GD)
+        {
+          unsigned int got_type = GOT_TYPE_TLS_PAIR;
+          unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
+                                             : elfcpp::R_MIPS_TLS_DTPMOD64);
+          unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
+                                             : elfcpp::R_MIPS_TLS_DTPREL64);
+
+          if (!parameters->doing_static_link())
+            {
+              got->add_local_pair_with_rel(entry->object(), entry->symndx(),
+                                           entry->shndx(), got_type,
+                                           target->rel_dyn_section(layout),
+                                           r_type1);
+              unsigned int got_offset =
+                entry->object()->local_got_offset(entry->symndx(), got_type);
+              got->add_static_reloc(got_offset + size/8, r_type2,
+                                    entry->object(), entry->symndx());
+            }
+          else
+            {
+              // We are doing a static link.  Mark it as belong to module 1,
+              // the executable.
+              unsigned int got_offset = got->add_constant(1);
+              entry->object()->set_local_got_offset(entry->symndx(), got_type,
+                                                    got_offset);
+              got->add_constant(0);
+              got->add_static_reloc(got_offset + size/8, r_type2,
+                                    entry->object(), entry->symndx());
+            }
+        }
+      else if (entry->tls_type() == GOT_TLS_IE)
+        {
+          unsigned int got_type = GOT_TYPE_TLS_OFFSET;
+          unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
+                                            : elfcpp::R_MIPS_TLS_TPREL64);
+          if (!parameters->doing_static_link())
+            got->add_local_with_rel(entry->object(), entry->symndx(), got_type,
+                                    target->rel_dyn_section(layout), r_type);
+          else
+            {
+              got->add_local(entry->object(), entry->symndx(), got_type);
+              unsigned int got_offset =
+                  entry->object()->local_got_offset(entry->symndx(), got_type);
+              got->add_static_reloc(got_offset, r_type, entry->object(),
+                                    entry->symndx());
+            }
+        }
+      else if (entry->tls_type() == GOT_TLS_LDM)
+        {
+          unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
+                                            : elfcpp::R_MIPS_TLS_DTPMOD64);
+          unsigned int got_offset;
+          if (!parameters->doing_static_link())
+            {
+              got_offset = got->add_constant(0);
+              target->rel_dyn_section(layout)->add_local(
+                  entry->object(), 0, r_type, got, got_offset);
+            }
+          else
+            // We are doing a static link.  Just mark it as belong to module 1,
+            // the executable.
+            got_offset = got->add_constant(1);
+
+          got->add_constant(0);
+          got->set_tls_ldm_offset(got_offset, entry->object());
+        }
+      else
+        gold_unreachable();
+    }
+
+  // Add global tls entries.
+  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->tls_entry() || !entry->is_for_global_symbol())
+        continue;
+
+      Mips_symbol<size>* mips_sym = entry->sym();
+      if (entry->tls_type() == GOT_TLS_GD)
+        {
+          unsigned int got_type;
+          if (!got->multi_got())
+            got_type = GOT_TYPE_TLS_PAIR;
+          else
+            got_type = GOT_TYPE_TLS_PAIR_MULTIGOT + this->index_;
+          unsigned int r_type1 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPMOD32
+                                             : elfcpp::R_MIPS_TLS_DTPMOD64);
+          unsigned int r_type2 = (size == 32 ? elfcpp::R_MIPS_TLS_DTPREL32
+                                             : elfcpp::R_MIPS_TLS_DTPREL64);
+          if (!parameters->doing_static_link())
+            got->add_global_pair_with_rel(mips_sym, got_type,
+                             target->rel_dyn_section(layout), r_type1, r_type2);
+          else
+            {
+              // Add a GOT pair for for R_MIPS_TLS_GD.  The creates a pair of
+              // GOT entries. The first one is initialized to be 1, which is the
+              // module index for the main executable and the second one 0.  A
+              // reloc of the type R_MIPS_TLS_DTPREL32/64 will be created for
+              // the second GOT entry and will be applied by gold.
+              unsigned int got_offset = got->add_constant(1);
+              mips_sym->set_got_offset(got_type, got_offset);
+              got->add_constant(0);
+              got->add_static_reloc(got_offset + size/8, r_type2, mips_sym);
+            }
+        }
+      else if (entry->tls_type() == GOT_TLS_IE)
+        {
+          unsigned int got_type;
+          if (!got->multi_got())
+            got_type = GOT_TYPE_TLS_OFFSET;
+          else
+            got_type = GOT_TYPE_TLS_OFFSET_MULTIGOT + this->index_;
+          unsigned int r_type = (size == 32 ? elfcpp::R_MIPS_TLS_TPREL32
+                                            : elfcpp::R_MIPS_TLS_TPREL64);
+          if (!parameters->doing_static_link())
+            got->add_global_with_rel(mips_sym, got_type,
+                                     target->rel_dyn_section(layout), r_type);
+          else
+            {
+              got->add_global(mips_sym, got_type);
+              unsigned int got_offset = mips_sym->got_offset(got_type);
+              got->add_static_reloc(got_offset, r_type, mips_sym);
+            }
+        }
+      else
+        gold_unreachable();
+    }
+}
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::lay_out_got(
+    Target_mips<size, big_endian> *target, Layout* layout, Symbol_table* symtab,
+    const Input_objects* input_objects)
+{
+  this->local_gotno_ += 2;
+  this->local_gotno_ += this->page_gotno_;
+
+  // Decide which symbols need to go in the global part of the GOT and
+  // count the number of reloc-only GOT symbols.
+  this->count_got_symbols(symtab);
+
+  // Count the number of GOT entries.
+  this->count_got_entries(this);
+
+  unsigned int got_size = ((this->local_gotno_ + this->global_gotno_
+                           + this->tls_gotno_) * size/8);
+  if (got_size > Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE)
+    this->lay_out_multi_got(target, layout, input_objects);
+  else
+    {
+      // Record that all objects use single got.
+      for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+           p != input_objects->relobj_end();
+           ++p)
+        {
+          Mips_relobj<size, big_endian>* mips_relobj =
+            Mips_relobj<size, big_endian>::as_mips_relobj(*p);
+          if (this->get_got_info(mips_relobj, false) != NULL)
+            this->replace_object_got(mips_relobj, this);
+        }
+
+      this->add_local_entries(target, layout);
+      this->add_global_entries(target, layout, /*not used*/-1U);
+      this->add_tls_entries(target, 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 relocations
+// against them (reloc_only_gotno).
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::count_got_symbols(Symbol_table* symtab)
+{
+  for (typename Unordered_set<Mips_symbol<size>*>::iterator
+       p = this->global_got_symbols_.begin();
+       p != this->global_got_symbols_.end();
+       ++p)
+    {
+      Mips_symbol<size>* sym = *p;
+      // Make a final decision about whether the symbol belongs in the
+      // local or global GOT.  Symbols that bind locally can (and in the
+      // case of forced-local symbols, must) live in the local GOT.
+      // Those that are aren't in the dynamic symbol table must also
+      // live in the local GOT.
+
+      if (!should_add_dynsym_entry(sym, symtab)
+          || (sym->got_only_for_calls()
+              ? symbol_calls_local(sym, should_add_dynsym_entry(sym, symtab))
+              : symbol_references_local(sym,
+                                        should_add_dynsym_entry(sym, symtab))))
+        // The symbol belongs in the local GOT.  We no longer need this
+        // entry if it was only used for relocations; those relocations
+        // will be against the null or section symbol instead.
+        sym->set_global_got_area(GGA_NONE);
+      else if (sym->global_got_area() == GGA_RELOC_ONLY)
+        {
+          ++this->reloc_only_gotno_;
+          ++this->global_gotno_ ;
+        }
+    }
+}
+
+template<int size, bool big_endian>
+unsigned int
+Mips_got_info<size, big_endian>::get_got_page_offset(unsigned int value,
+                                                     unsigned char *got_view)
+{
+  typename Got_page_offsets::iterator it = this->got_page_offsets_.find(value);
+  if (it != this->got_page_offsets_.end())
+    return it->second;
+
+  gold_assert(this->got_page_offset_next_ < this->got_page_offset_start_
+              + (size/8) * this->page_gotno_);
+
+  unsigned int got_offset = this->got_page_offset_next_;
+  elfcpp::Swap<size, big_endian>::writeval(got_view + got_offset, value);
+  this->got_page_offsets_[value] = got_offset;
+  this->got_page_offset_next_ += size/8;
+  return got_offset;
+}
+
+// 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(
+    Sized_relobj_file<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 = this->get_got_info(object, true);
+  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;
+    }
+}
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::lay_out_multi_got(
+    Target_mips<size, big_endian> *target, Layout* layout,
+    const Input_objects* input_objects)
+{
+  target->got_section()->set_multi_got();
+
+  // Try to merge the GOTs of input objects together, as long as they
+  // don't seem to exceed the maximum GOT size, choosing one of them
+  // to be the primary GOT.
+  this->merge_gots(input_objects);
+
+  // G is the primary GOT.
+  Mips_got_info<size, big_endian> *g = this->next_;
+
+  // Every symbol that is referenced in a dynamic relocation must be
+  // present in the primary GOT.
+  g->global_gotno_ = this->global_gotno_;
+
+  // Add got entries.
+  unsigned int i = 0;
+  unsigned int offset = 0;
+  do
+    {
+      g->index_ = i;
+      g->add_local_entries(target, layout);
+      if (i == 0)
+        g->add_global_entries(target, layout,
+                              this->global_gotno_ - this->reloc_only_gotno_);
+      else
+        g->add_global_entries(target, layout,
+                              /*not used*/-1U);
+      g->add_tls_entries(target, layout);
+      g->offset_ = offset;
+      offset += ((2 + g->local_gotno_ + g->page_gotno_ + g->global_gotno_
+                 + g->tls_gotno_) * size/8);
+
+      if (i > 0)
+        {
+          // Forbid global symbols in every non-primary GOT from having
+          // lazy-binding stubs.
+          for (typename Got_entry_set::iterator
+               p = g->got_entries_.begin();
+               p != g->got_entries_.end();
+               ++p)
+            {
+              Mips_got_entry<size, big_endian> *entry = *p;
+              if (entry->is_for_global_symbol())
+                target->remove_lazy_stub_entry(entry->sym());
+            }
+        }
+
+      g = g->next_;
+      ++i;
+    }
+  while (g);
+
+}
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::count_got_entries(
+    Mips_got_info<size, big_endian> *g)
+{
+  for (typename Got_entry_set::iterator
+       p = g->got_entries_.begin();
+       p != g->got_entries_.end();
+       ++p)
+    {
+      Mips_got_entry<size, big_endian> *entry = *p;
+      count_got_entry(entry, g);
+    }
+}
+
+// Add the number of GOT entries required by ENTRY to G.
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::count_got_entry(
+    Mips_got_entry<size, big_endian> *entry,
+    Mips_got_info<size, big_endian> *g)
+{
+  if (entry->tls_entry())
+    g->tls_gotno_ += mips_tls_got_entries(entry->tls_type());
+  else if (entry->is_for_local_symbol()
+           || entry->sym()->global_got_area() == GGA_NONE)
+    ++g->local_gotno_;
+  else
+    ++g->global_gotno_;
+}
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::add_got_entries(
+    Mips_got_info<size, big_endian> *from,
+    Mips_got_info<size, big_endian> *to)
+{
+  for (typename Got_entry_set::iterator
+       p = from->got_entries_.begin();
+       p != from->got_entries_.end();
+       ++p)
+    {
+      Mips_got_entry<size, big_endian> *entry = *p;
+      if (to->got_entries_.find(entry) != to->got_entries_.end())
+        continue;
+
+      // Insert the GOT entry in the object's got entry hash table.
+      Mips_got_entry<size, big_endian> *entry2 =
+        new Mips_got_entry<size, big_endian>(*entry);
+      to->got_entries_.insert(entry2);
+      this->count_got_entry(entry, to);
+    }
+}
+
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::add_got_page_entries(
+    Mips_got_info<size, big_endian> *from,
+    Mips_got_info<size, big_endian> *to)
+{
+  for (typename Got_page_entry_set::iterator
+       p = from->got_page_entries_.begin();
+       p != from->got_page_entries_.end();
+       ++p)
+    {
+      Got_page_entry *entry = *p;
+
+      if (to->got_page_entries_.find(entry) != to->got_page_entries_.end())
+        continue;
+
+      // Insert the GOT entry in the object's got entry hash table.
+      Got_page_entry *entry2 = new Got_page_entry(*entry);
+      to->got_page_entries_.insert(entry2);
+      to->page_gotno_ += entry->num_pages;
+    }
+}
+
+// Record that OBJECT should use output GOT G.
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::replace_object_got(
+    Mips_relobj<size, big_endian>* mips_relobj,
+    Mips_got_info<size, big_endian> *g)
+{
+  mips_relobj->set_got_info(g);
+}
+
+// Attempt to merge gots of different input objects.  Try to use as much
+// as possible of the primary got, since it doesn't require explicit
+// dynamic relocations, but don't use objects that would reference global
+// symbols out of the addressable range.  Failing the primary got,
+// attempt to merge with the current got, or finish the current got
+// and then make make the new got current.
+template<int size, bool big_endian>
+void
+Mips_got_info<size, big_endian>::merge_gots(const Input_objects* input_objects)
+{
+  Mips_got_info<size, big_endian> *primary = NULL;
+  Mips_got_info<size, big_endian> *current = NULL;
+
+  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+       p != input_objects->relobj_end();
+       ++p)
+    {
+      Mips_relobj<size, big_endian>* mips_relobj =
+        Mips_relobj<size, big_endian>::as_mips_relobj(*p);
+
+      Mips_got_info<size, big_endian> *g =
+        this->get_got_info(mips_relobj, false);
+      if (g == NULL)
+        continue;
+
+      this->count_got_entries(g);
+
+      // Work out the number of page, local and TLS entries.
+      unsigned int estimate = this->page_gotno_;
+      if (estimate > g->page_gotno_)
+        estimate = g->page_gotno_;
+      estimate += g->local_gotno_ + g->tls_gotno_;
+
+      // We place TLS GOT entries after both locals and globals.  The globals
+      // for the primary GOT may overflow the normal GOT size limit, so be
+      // sure not to merge a GOT which requires TLS with the primary GOT in that
+      // case.  This doesn't affect non-primary GOTs.
+      estimate += (g->tls_gotno_ > 0 ? this->global_gotno_ : g->global_gotno_);
+
+      unsigned int max_count =
+        Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
+      if (estimate <= max_count)
+        {
+          // If we don't have a primary GOT, use it as
+          // a starting point for the primary GOT.
+          if (!primary)
+            {
+              primary = g;
+              continue;
+            }
+
+          // Try merging with the primary GOT.
+          if (this->merge_got_with(mips_relobj, g, primary, primary))
+            continue;
+        }
+
+      // If we can merge with the last-created got, do it.
+      if (current && this->merge_got_with(mips_relobj, g, current, primary))
+        continue;
+
+      // Well, we couldn't merge, so create a new GOT.  Don't check if it
+      // fits; if it turns out that it doesn't, we'll get relocation
+      // overflows anyway.
+      g->next_ = current;
+      current = g;
+    }
+
+  // If we do not find any suitable primary GOT, create an empty one.
+  if (primary == NULL)
+    this->next_ = new Mips_got_info<size, big_endian>();
+  else
+    this->next_ = primary;
+  this->next_->next_ = current;
+}
+
+// Consider merging FROM, which is OBJECT's GOT, into TO.  Returns false if
+// this would lead to overflow, true if they were merged successfully.
+template<int size, bool big_endian>
+bool
+Mips_got_info<size, big_endian>::merge_got_with(
+    Mips_relobj<size, big_endian>* mips_relobj,
+    Mips_got_info<size, big_endian> *from,
+    Mips_got_info<size, big_endian> *to,
+    Mips_got_info<size, big_endian> *primary)
+{
+  unsigned int estimate;
+
+  // Work out how many page entries we would need for the combined GOT.
+  estimate = this->page_gotno_;
+  if (estimate >= from->page_gotno_ + to->page_gotno_)
+    estimate = from->page_gotno_ + to->page_gotno_;
+
+  // And conservatively estimate how many local and TLS entries
+  // would be needed.
+  estimate += from->local_gotno_ + to->local_gotno_;
+  estimate += from->tls_gotno_ + to->tls_gotno_;
+
+  // If we're merging with the primary got, any TLS relocations will
+  // come after the full set of global entries.  Otherwise estimate those
+  // conservatively as well.
+  if (to == primary && (from->tls_gotno_ + to->tls_gotno_) > 0)
+    estimate += this->global_gotno_;
+  else
+    estimate += from->global_gotno_ + to->global_gotno_;
+
+  // Bail out if the combined GOT might be too big.
+  unsigned int max_count =
+    Target_mips<size, big_endian>::MIPS_GOT_MAX_SIZE / (size/8) - 2;
+  if (estimate > max_count)
+    return false;
+
+  // Transfer the object's got information from FROM to TO.
+  this->add_got_entries(from, to);
+  this->add_got_page_entries(from, to);
+
+  this->replace_object_got(mips_relobj, to);
+
+  return true;
+}
+
+// Mips_output_data_got methods.
+
+template<int size, bool big_endian>
+void
+Mips_output_data_got<size, big_endian>::do_write(Output_file* of)
+{
+  typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+  // Call parent to write out GOT.
+  Output_data_got<size, big_endian>::do_write(of);
+
+  const off_t offset = this->offset();
+  const section_size_type oview_size =
+    convert_to_section_size_type(this->data_size());
+  unsigned char* const oview = of->get_output_view(offset, oview_size);
+
+  // Needed for fixing values of .got section.
+  this->got_view_ = oview;
+
+  // Write lazy stub addresses.
+  for (typename Unordered_set<Mips_symbol<size>*>::iterator
+       p = got_info->global_got_symbols().begin();
+       p != got_info->global_got_symbols().end();
+       ++p)
+    {
+      Mips_symbol<size>* mips_sym = *p;
+      if (mips_sym->has_lazy_stub())
+        {
+          typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
+          Valtype* wv = reinterpret_cast<Valtype*>(
+            (oview + (this->local_gotno() + mips_sym->dynsym_index()
+             - this->global_got_index()) * size/8));
+          Valtype value = (target_->mips_stubs()->address()
+                           + mips_sym->lazy_stub_offset());
+          elfcpp::Swap<size, big_endian>::writeval(wv, value);
+        }
+    }
+
+  if (!this->secondary_got_relocs_.empty())
+    {
+      // Fixup for the secondary got R_MIPS_REL32 relocs.  Since glibc's ld.so
+      // when resolving R_MIPS_REL32 relocs just adds the final primary got
+      // entry value to the relocation field, we need to copy initial value of
+      // secondary got entry to corresponding primary got entry so that primary
+      // entry is correctly resolved, and we need to initialize secondary got
+      // entry to 0.
+      for (size_t i = 0; i < this->secondary_got_relocs_.size(); ++i)
+        {
+          Static_reloc& reloc(this->secondary_got_relocs_[i]);
+          if (reloc.symbol_is_global())
+            {
+              const Symbol* gsym = reloc.symbol();
+              gold_assert(gsym != NULL);
+
+              unsigned got_offset = reloc.got_offset();
+              gold_assert(got_offset < oview_size);
+
+              typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
+              Valtype* wv_prim = reinterpret_cast<Valtype*>(
+                (oview + (this->local_gotno() + gsym->dynsym_index()
+                 - this->global_got_index()) * size/8));
+              Valtype* wv_sec = reinterpret_cast<Valtype*>(oview + got_offset);
+              Valtype value = elfcpp::Swap<size, big_endian>::readval(wv_sec);
+              elfcpp::Swap<size, big_endian>::writeval(wv_prim, value);
+              elfcpp::Swap<size, big_endian>::writeval(wv_sec, 0);
+            }
+        }
+
+      of->write_output_view(offset, oview_size, oview);
+    }
+
+  // We are done if there is no fix up.
+  if (this->static_relocs_.empty())
+    return;
+
+  Output_segment* tls_segment = this->layout_->tls_segment();
+  gold_assert(tls_segment != NULL);
+
+  for (size_t i = 0; i < this->static_relocs_.size(); ++i)
+    {
+      Static_reloc& reloc(this->static_relocs_[i]);
+
+      Mips_address value;
+      if (!reloc.symbol_is_global())
+        {
+          Sized_relobj_file<size, big_endian>* object = reloc.relobj();
+          const Symbol_value<size>* psymval =
+            object->local_symbol(reloc.index());
+
+          // We are doing static linking.  Issue an error and skip this
+          // relocation if the symbol is undefined or in a discarded_section.
+          bool is_ordinary;
+          unsigned int shndx = psymval->input_shndx(&is_ordinary);
+          if ((shndx == elfcpp::SHN_UNDEF)
+              || (is_ordinary
+                  && shndx != elfcpp::SHN_UNDEF
+                  && !object->is_section_included(shndx)
+                  && !this->symbol_table_->is_section_folded(object, shndx)))
+            {
+              gold_error(_("undefined or discarded local symbol %u from "
+                           " object %s in GOT"),
+                         reloc.index(), reloc.relobj()->name().c_str());
+              continue;
+            }
+
+          value = psymval->value(object, 0);
+        }
+      else
+        {
+          const Symbol* gsym = reloc.symbol();
+          gold_assert(gsym != NULL);
+
+          // We are doing static linking.  Issue an error and skip this
+          // relocation if the symbol is undefined or in a discarded_section
+          // unless it is a weakly_undefined symbol.
+          if ((gsym->is_defined_in_discarded_section() || gsym->is_undefined())
+              && !gsym->is_weak_undefined())
+            {
+              gold_error(_("undefined or discarded symbol %s in GOT"),
+                         gsym->name());
+              continue;
+            }
+
+          if (!gsym->is_weak_undefined())
+            value = Mips_symbol<size>::as_mips_sym(gsym)->value();
+          else
+            value = 0;
+        }
+
+      unsigned got_offset = reloc.got_offset();
+      gold_assert(got_offset < oview_size);
+
+      typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
+      Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
+      Valtype x;
+
+      switch (reloc.r_type())
+        {
+        case elfcpp::R_MIPS_TLS_DTPMOD32:
+          x = value;
+          break;
+        case elfcpp::R_MIPS_TLS_TPREL32:
+          x = value - elfcpp::TP_OFFSET;
+          break;
+        case elfcpp::R_MIPS_TLS_DTPREL32:
+          x = value - elfcpp::DTP_OFFSET;
+          break;
+        default:
+          gold_unreachable();
+          break;
+        }
+
+      elfcpp::Swap<size, big_endian>::writeval(wv, x);
+    }
+
+  of->write_output_view(offset, oview_size, oview);
+}
+
+// Mips_relobj methods.
+
+// Count the local symbols.  The Mips backend needs to know if a symbol
+// is a MIPS16 or microMIPS function or not.  For global symbols, it is easy
+// because the Symbol object keeps the ELF symbol type and st_other field.
+// For local symbol it is harder because we cannot access this information.
+// So we override the do_count_local_symbol in parent and scan local symbols to
+// mark MIPS16 and microMIPS functions.  This is not the most efficient way but
+// I do not want to slow down other ports by calling a per symbol target hook
+// inside Sized_relobj_file<size, big_endian>::do_count_local_symbols.
+
+template<int size, bool big_endian>
+void
+Mips_relobj<size, big_endian>::do_count_local_symbols(
+    Stringpool_template<char>* pool,
+    Stringpool_template<char>* dynpool)
+{
+  // Ask parent to count the local symbols.
+  Sized_relobj_file<size, big_endian>::do_count_local_symbols(pool, dynpool);
+  const unsigned int loccount = this->local_symbol_count();
+  if (loccount == 0)
+    return;
+
+  // Initialize the mips16 and micromips function bit-vector.
+  std::vector<bool> empty_vector(loccount, false);
+  std::vector<bool> empty_vector2(loccount, false);
+  this->local_symbol_is_mips16_.swap(empty_vector);
+  this->local_symbol_is_micromips_.swap(empty_vector2);
+
+  // Read the symbol table section header.
+  const unsigned int symtab_shndx = this->symtab_shndx();
+  elfcpp::Shdr<size, big_endian>
+    symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
+  gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
+
+  // Read the local symbols.
+  const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
+  gold_assert(loccount == symtabshdr.get_sh_info());
+  off_t locsize = loccount * sym_size;
+  const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
+                                              locsize, true, true);
+
+  // Loop over the local symbols and mark any MIPS16 or microMIPS local symbols.
+
+  // Skip the first dummy symbol.
+  psyms += sym_size;
+  for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
+    {
+      elfcpp::Sym<size, big_endian> sym(psyms);
+      unsigned char st_other = sym.get_st_other();
+      this->local_symbol_is_mips16_[i] = elfcpp::elf_st_is_mips16(st_other);
+      this->local_symbol_is_micromips_[i] =
+        elfcpp::elf_st_is_micromips(st_other);
+    }
+}
+
+// Read the symbol information.
+
+template<int size, bool big_endian>
+void
+Mips_relobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
+{
+  // Call parent class to read symbol information.
+  Sized_relobj_file<size, big_endian>::do_read_symbols(sd);
+
+  // Read processor-specific flags in ELF file header.
+  const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
+                                            elfcpp::Elf_sizes<size>::ehdr_size,
+                                            true, false);
+  elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
+  this->processor_specific_flags_ = ehdr.get_e_flags();
+
+  if (size == 32)
+    {
+      // Go over the section headers and look for .reginfo section.
+      // The .reginfo section is not used in the 64-bit MIPS ELF ABI.
+      const size_t shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
+      const unsigned char* pshdrs = sd->section_headers->data();
+      const unsigned char* ps = pshdrs + shdr_size;
+      for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
+        {
+          elfcpp::Shdr<size, big_endian> shdr(ps);
+
+          if (shdr.get_sh_type() == elfcpp::SHT_MIPS_REGINFO)
+            {
+              section_offset_type section_offset = shdr.get_sh_offset();
+              section_size_type section_size =
+                convert_to_section_size_type(shdr.get_sh_size());
+              const unsigned char* view =
+                 this->get_view(section_offset, section_size, true, false);
+
+              // Read gp value that was used to create this object.
+              this->gp_ = elfcpp::Swap<size, big_endian>::readval(view + 20);
+            }
+        }
+    }
+  else if (size == 64)
+    {
+      // TODO(sasa): In the 64 bit ABI, the .MIPS.options section holds register
+      // information.
+    }
+}
+
+// Mips_output_data_la25_stub methods.
+
+// TODO(sasa): Micromips has different la25 stubs.
+template<int size, bool big_endian>
+const uint32_t
+Mips_output_data_la25_stub<size, big_endian>::la25_stub_entry[4] =
+{
+  0x3c190000,           // lui $25,%hi(func)
+  0x08000000,           // j func
+  0x27390000,           // add $25,$25,%lo(func)
+  0x00000000            // nop
+};
+
+// Add la25 stub for a symbol.
+
+template<int size, bool big_endian>
+void
+Mips_output_data_la25_stub<size, big_endian>::add_la25_stub(
+    Symbol_table* symtab, Target_mips<size, big_endian>* target, Symbol* gsym)
+{
+  Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
+  if (!mips_sym->has_la25_stub())
+    {
+      mips_sym->set_has_la25_stub();
+      mips_sym->set_la25_stub_offset(this->symbols_.size() * 16);
+      this->symbols_.insert(gsym);
+      this->create_stub_symbol(mips_sym, symtab, target, 16);
+    }
+}
+
+// We're going to create a stub for MIPS_SYM.  Create a symbol for the stub's
+// value and size, to help make the disassembly easier to read.
+
+template<int size, bool big_endian>
+Symbol*
+Mips_output_data_la25_stub<size, big_endian>::create_stub_symbol(
+    Mips_symbol<size>* mips_sym, Symbol_table* symtab,
+    Target_mips<size, big_endian>* target, uint64_t symsize)
+{
+  Symbol* sym;
+  char* buffer = new char[strlen(mips_sym->name()) + 6];
+  sprintf(buffer, ".pic.%s", mips_sym->name());
+
+  unsigned int offset = mips_sym->la25_stub_offset();
+  if (mips_sym->is_micromips())
+    offset |= 1;
+
+  // Make it a local function.
+  sym = symtab->define_in_output_data(buffer, NULL,
+                                      Symbol_table::PREDEFINED,
+                                      target->la25_stub_section(),
+                                      offset, symsize, elfcpp::STT_FUNC,
+                                      elfcpp::STB_LOCAL,
+                                      elfcpp::STV_DEFAULT, 0,
+                                      false, false);
+  sym->set_is_forced_local();
+  return sym;
+}
+
+// Write out the STUB.  This uses the hand-coded instructions above,
+// and adjusts them as needed.
+
+template<int size, bool big_endian>
+void
+Mips_output_data_la25_stub<size, big_endian>::do_write(Output_file* of)
+{
+  typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+  const off_t offset = this->offset();
+  const section_size_type oview_size =
+    convert_to_section_size_type(this->data_size());
+  unsigned char* const oview = of->get_output_view(offset, oview_size);
+
+  for (typename Unordered_set<Symbol*>::iterator
+       p = this->symbols_.begin();
+       p != this->symbols_.end();
+       ++p)
+    {
+      Mips_symbol<size> *sym = Mips_symbol<size>::as_mips_sym(*p);
+      unsigned char* pov = oview + sym->la25_stub_offset();
+
+      unsigned int value = (unsigned int)sym->value();
+      gold_assert(offset >= 0 && offset < 0x0fffffff);
+      uint32_t stub_insn0 =
+        la25_stub_entry[0] | (((value + 0x8000) >> 16) & 0xffff);
+      elfcpp::Swap<32, big_endian>::writeval(pov, stub_insn0);
+      uint32_t stub_insn1 = la25_stub_entry[1] | (((value >> 2) & 0x03ffffff));
+      elfcpp::Swap<32, big_endian>::writeval(pov + 4, stub_insn1);
+      uint32_t stub_insn2 = la25_stub_entry[2] | ((value & 0x0000ffff));
+      elfcpp::Swap<32, big_endian>::writeval(pov + 8, stub_insn2);
+      uint32_t stub_insn3 = la25_stub_entry[3];
+      elfcpp::Swap<32, big_endian>::writeval(pov + 12, stub_insn3);
+    }
+
+  of->write_output_view(offset, oview_size, oview);
+}
+
+// Mips_output_data_plt methods.
+
+// The format of the first PLT entry in an O32 executable.
+template<int size, bool big_endian>
+const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_o32[8] =
+{
+  0x3c1c0000,         // lui $28, %hi(&GOTPLT[0])
+  0x8f990000,         // lw $25, %lo(&GOTPLT[0])($28)
+  0x279c0000,         // addiu $28, $28, %lo(&GOTPLT[0])
+  0x031cc023,         // subu $24, $24, $28
+  0x03e07821,         // move $15, $31        # 32-bit move (addu)
+  0x0018c082,         // srl $24, $24, 2
+  0x0320f809,         // jalr $25
+  0x2718fffe          // subu $24, $24, 2
+};
+
+// The format of the first PLT entry in an N32 executable.  Different
+// because gp ($28) is not available; we use t2 ($14) instead.
+template<int size, bool big_endian>
+const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n32[8] =
+{
+  0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
+  0x8dd90000,         // lw $25, %lo(&GOTPLT[0])($14)
+  0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
+  0x030ec023,         // subu $24, $24, $14
+  0x03e07821,         // move $15, $31        # 32-bit move (addu)
+  0x0018c082,         // srl $24, $24, 2
+  0x0320f809,         // jalr $25
+  0x2718fffe          // subu $24, $24, 2
+};
+
+// The format of the first PLT entry in an N64 executable.  Different
+// from N32 because of the increased size of GOT entries.
+template<int size, bool big_endian>
+const uint32_t Mips_output_data_plt<size, big_endian>::plt0_entry_n64[8] =
+{
+  0x3c0e0000,         // lui $14, %hi(&GOTPLT[0])
+  0xddd90000,         // ld $25, %lo(&GOTPLT[0])($14)
+  0x25ce0000,         // addiu $14, $14, %lo(&GOTPLT[0])
+  0x030ec023,         // subu $24, $24, $14
+  0x03e07821,         // move $15, $31        # 64-bit move (daddu)
+  0x0018c0c2,         // srl $24, $24, 3
+  0x0320f809,         // jalr $25
+  0x2718fffe          // subu $24, $24, 2
+};
+
+// Subsequent entries in the PLT.
+
+template<int size, bool big_endian>
+const uint32_t Mips_output_data_plt<size, big_endian>::plt_entry[4] =
+{
+  0x3c0f0000,           // lui $15, %hi(.got.plt entry)
+  0x8df90000,           // l[wd] $25, %lo(.got.plt entry)($15)
+  0x03200008,           // jr $25
+  0x25f80000            // addiu $24, $15, %lo(.got.plt entry)
+};
+
+// Create the PLT section.  The ordinary .got section is an argument,
+// since we need to refer to the start.
+
+template<int size, bool big_endian>
+Mips_output_data_plt<size, big_endian>::Mips_output_data_plt(Layout* layout,
+                                                     Output_data_space* got_plt)
+  : Output_section_data(size == 32 ? 4 : 8), got_plt_(got_plt), count_(0)
+{
+  this->rel_ = new Reloc_section(false);
+  layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
+                                  elfcpp::SHF_ALLOC, this->rel_,
+                                  ORDER_DYNAMIC_PLT_RELOCS, false);
+}
+
+template<int size, bool big_endian>
+void
+Mips_output_data_plt<size, big_endian>::do_adjust_output_section(
+    Output_section* os)
+{ os->set_entsize(0); }
+
+// Add an entry to the PLT.
+
+template<int size, bool big_endian>
+void
+Mips_output_data_plt<size, big_endian>::add_entry(Symbol* gsym)
+{
+  gold_assert(!gsym->has_plt_offset());
+
+  gsym->set_plt_offset(this->count_ * sizeof(plt_entry)
+                       + sizeof(plt0_entry_o32));
+
+  ++this->count_;
+
+  section_offset_type got_offset = this->got_plt_->current_data_size();
+
+  // Every PLT entry needs a GOT entry which points back to the PLT
+  // entry (this will be changed by the dynamic linker, normally
+  // lazily when the function is called).
+  this->got_plt_->set_current_data_size(got_offset + 4);
+
+  gsym->set_needs_dynsym_entry();
+  this->rel_->add_global(gsym, elfcpp::R_MIPS_JUMP_SLOT, this->got_plt_,
+                         got_offset);
+}
+
+// Write out the PLT.  This uses the hand-coded instructions above,
+// and adjusts them as needed.
+
+template<int size, bool big_endian>
+void
+Mips_output_data_plt<size, big_endian>::do_write(Output_file* of)
+{
+  typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+  // Read processor-specific flags in ELF file header.
+  const unsigned char* pehdr = of->get_output_view(elfcpp::file_header_offset,
+                                   elfcpp::Elf_sizes<size>::ehdr_size);
+  elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
+  bool n32 = elfcpp::abi_n32(ehdr.get_e_flags());
+  bool n64 = elfcpp::abi_64(ehdr.get_e_ident()[elfcpp::EI_CLASS]);
+
+  const off_t offset = this->offset();
+  const section_size_type oview_size =
+    convert_to_section_size_type(this->data_size());
+  unsigned char* const oview = of->get_output_view(offset, oview_size);
+
+  const off_t got_file_offset = this->got_plt_->offset();
+  const section_size_type got_size =
+    convert_to_section_size_type(this->got_plt_->data_size());
+  unsigned char* const got_view = of->get_output_view(got_file_offset,
+                                                      got_size);
+  unsigned char* pov = oview;
+
+  Mips_address plt_address = this->address();
+  Mips_address got_address = this->got_plt_->address();
+
+  const uint32_t *plt0_entry;
+  if (n64)
+    plt0_entry = plt0_entry_n64;
+  else if (n32)
+    plt0_entry = plt0_entry_n32;
+  else
+    plt0_entry = plt0_entry_o32;
+
+  uint32_t plt_insn0 =
+    plt0_entry[0] | (((got_address + 0x8000) >> 16) & 0xffff);
+  elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
+  uint32_t plt_insn1 = plt0_entry[1] | (got_address & 0xffff);
+  elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
+  uint32_t plt_insn2 = plt0_entry[2] | (got_address & 0xffff);
+  elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
+
+  for (size_t i = 3; i < 8; i++)
+    elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, plt0_entry[i]);
+
+  pov += sizeof(plt0_entry_o32);
+
+  unsigned char* got_pov = got_view;
+
+  memset(got_pov, 0, 8);
+  got_pov += 8;
+
+  const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
+  unsigned int plt_offset = sizeof(plt0_entry_o32);
+  unsigned int plt_rel_offset = 0;
+  unsigned int got_offset = 8;
+  const unsigned int count = this->count_;
+  for (unsigned int i = 0;
+       i < count;
+       ++i,
+         pov += sizeof(plt_entry),
+         got_pov += 4,
+         plt_offset += sizeof(plt_entry),
+         plt_rel_offset += rel_size,
+         got_offset += 4)
+    {
+      // Set and adjust the PLT entry itself.
+      int32_t offset = (got_address + got_offset);
+
+      uint32_t plt_insn0 = plt_entry[0] | (((offset + 0x8000) >> 16) & 0xffff);
+      elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
+      uint32_t plt_insn1 = plt_entry[1] | (offset & 0xffff);
+      elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
+      uint32_t plt_insn2 = plt_entry[2];
+      elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
+      uint32_t plt_insn3 = plt_entry[3] | (offset & 0xffff);
+      elfcpp::Swap<32, big_endian>::writeval(pov + 12, plt_insn3);
+
+      // Set the entry in the GOT.
+      elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
+    }
+
+  gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
+  gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
+
+  of->write_output_view(offset, oview_size, oview);
+  of->write_output_view(got_file_offset, got_size, got_view);
+}
+
+// Mips_output_data_mips_stubs methods.
+
+// The format of the lazy binding stub when dynamic symbol count is less than
+// 64K, dynamic symbol index is less than 32K, and ABI is not N64.
+template<int size, bool big_endian>
+const uint32_t
+Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1[4] =
+{
+  0x8f998010,         // lw t9,0x8010(gp)
+  0x03e07821,         // addu t7,ra,zero
+  0x0320f809,         // jalr t9,ra
+  0x24180000          // addiu t8,zero,DYN_INDEX sign extended
+};
+
+// Same as above, except that ABI is N64.
+template<int size, bool big_endian>
+const uint32_t
+Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_1_n64[4] =
+{
+  0xdf998010,         // ld t9,0x8010(gp)
+  0x03e0782d,         // daddu t7,ra,zero
+  0x0320f809,         // jalr t9,ra
+  0x64180000          // daddiu t8,zero,DYN_INDEX sign extended
+};
+
+// The format of the lazy binding stub when dynamic symbol count is less than
+// 64K, dynamic symbol index is between 32K and 64K, and ABI is not N64.
+template<int size, bool big_endian>
+const uint32_t
+Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2[4] =
+{
+  0x8f998010,         // lw t9,0x8010(gp)
+  0x03e07821,         // addu t7,ra,zero
+  0x0320f809,         // jalr t9,ra
+  0x34180000          // ori t8,zero,DYN_INDEX unsigned
+};
+
+// Same as above, except that ABI is N64.
+template<int size, bool big_endian>
+const uint32_t
+Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_normal_2_n64[4] =
+{
+  0xdf998010,         // ld t9,0x8010(gp)
+  0x03e0782d,         // daddu t7,ra,zero
+  0x0320f809,         // jalr t9,ra
+  0x34180000          // ori t8,zero,DYN_INDEX unsigned
+};
+
+// The format of the lazy binding stub when dynamic symbol count is greater than
+// 64K, and ABI is not N64.
+template<int size, bool big_endian>
+const uint32_t Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big[5] =
+{
+  0x8f998010,         // lw t9,0x8010(gp)
+  0x03e07821,         // addu t7,ra,zero
+  0x3c180000,         // lui t8,DYN_INDEX
+  0x0320f809,         // jalr t9,ra
+  0x37180000          // ori t8,t8,DYN_INDEX
+};
+
+// Same as above, except that ABI is N64.
+template<int size, bool big_endian>
+const uint32_t
+Mips_output_data_mips_stubs<size, big_endian>::lazy_stub_big_n64[5] =
+{
+  0xdf998010,         // ld t9,0x8010(gp)
+  0x03e0782d,         // daddu t7,ra,zero
+  0x3c180000,         // lui t8,DYN_INDEX
+  0x0320f809,         // jalr t9,ra
+  0x37180000          // ori t8,t8,DYN_INDEX
+};
+
+template<int size, bool big_endian>
+void
+Mips_output_data_mips_stubs<size, big_endian>::do_adjust_output_section(
+    Output_section* os)
+{ os->set_entsize(0); }
+
+// Add an entry to the .MIPS.stubs.
+
+template<int size, bool big_endian>
+void
+Mips_output_data_mips_stubs<size, big_endian>::add_entry(Symbol* sym)
+{
+  Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
+  gold_assert(!mips_sym->has_plt_offset());
+  if (!mips_sym->has_lazy_stub())
+    {
+      mips_sym->set_has_lazy_stub(true);
+      this->symbols_.insert(mips_sym);
+    }
+}
+
+// Remove entry for a symbol.
+template<int size, bool big_endian>
+void
+Mips_output_data_mips_stubs<size, big_endian>::remove_entry(Symbol *sym)
+{
+  Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(sym);
+  if (mips_sym->has_lazy_stub())
+    {
+      mips_sym->set_has_lazy_stub(false);
+      this->symbols_.erase(mips_sym);
+    }
+}
+
+template<int size, bool big_endian>
+void
+Mips_output_data_mips_stubs<size, big_endian>::set_lazy_stub_offsets()
+{
+  if (this->stub_offsets_are_set_)
+    return;
+
+  gold_assert(this->dynsym_count_ != -1U);
+
+  int i = 0;
+  for (Unordered_set<Symbol*>::const_iterator p = this->symbols_.begin();
+       p != this->symbols_.end(); ++p, ++i)
+    {
+      Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(*p);
+      if (this->dynsym_count_ > 0x10000)
+        mips_sym->set_lazy_stub_offset(20 * i);
+      else
+        mips_sym->set_lazy_stub_offset(16 * i);
+
+      this->stub_offsets_are_set_ = true;
+    }
+}
+
+template<int size, bool big_endian>
+void
+Mips_output_data_mips_stubs<size, big_endian>::set_needs_dynsym_value()
+{
+  for (Unordered_set<Symbol*>::const_iterator p = this->symbols_.begin();
+       p != this->symbols_.end(); ++p)
+    {
+      Symbol *sym = *p;
+      if (sym->is_from_dynobj())
+        sym->set_needs_dynsym_value();
+    }
+}
+
+// Write out the .MIPS.stubs.  This uses the hand-coded instructions and
+// adjusts them as needed.
+
+template<int size, bool big_endian>
+void
+Mips_output_data_mips_stubs<size, big_endian>::do_write(Output_file* of)
+{
+  typedef typename elfcpp::Elf_types<size>::Elf_Addr Mips_address;
+
+  // Read processor-specific flags in ELF file header.
+  const unsigned char* pehdr = of->get_output_view(elfcpp::file_header_offset,
+                                   elfcpp::Elf_sizes<size>::ehdr_size);
+  elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
+  bool n64 = elfcpp::abi_64(ehdr.get_e_ident()[elfcpp::EI_CLASS]);
+
+  const off_t offset = this->offset();
+  const section_size_type oview_size =
+    convert_to_section_size_type(this->data_size());
+  unsigned char* const oview = of->get_output_view(offset, oview_size);
+
+  bool big_stub = this->dynsym_count_ > 0x10000;
+  for (Unordered_set<Symbol*>::const_iterator p = this->symbols_.begin();
+       p != this->symbols_.end(); ++p)
+    {
+      Mips_symbol<size> *sym = Mips_symbol<size>::as_mips_sym(*p);
+      const uint32_t *lazy_stub;
+      if (!big_stub)
+        {
+          if (sym->dynsym_index() & ~0x7fff)
+            // Dynsym index is between 32K and 64K.
+            lazy_stub = n64 ? lazy_stub_normal_2_n64 : lazy_stub_normal_2;
+          else
+            // Dynsym index is less than 32K.
+            lazy_stub = n64 ? lazy_stub_normal_1_n64 : lazy_stub_normal_1;
+        }
+      else
+        lazy_stub = n64 ? lazy_stub_big_n64 : lazy_stub_big;
+
+      unsigned char *pov = oview + sym->lazy_stub_offset();
+      unsigned int off = 0;
+      elfcpp::Swap<32, big_endian>::writeval(pov + off, lazy_stub[off/4]);
+      off += 4;
+      elfcpp::Swap<32, big_endian>::writeval(pov + off, lazy_stub[off/4]);
+      off += 4;
+      uint32_t inst;
+      if (big_stub)
+        {
+          // LUI instruction of the big stub. Paste high 16 bits of the
+          // dynsym index.
+          inst = lazy_stub[off/4] | ((sym->dynsym_index() >> 16) & 0x7fff);
+          elfcpp::Swap<32, big_endian>::writeval(pov + off, inst);
+          off += 4;
+        }
+      elfcpp::Swap<32, big_endian>::writeval(pov + off, lazy_stub[off/4]);
+      off += 4;
+      // Last stub instruction. Paste low 16 bits of the dynsym index.
+      inst = lazy_stub[off/4] | (sym->dynsym_index() & 0xffff);
+      elfcpp::Swap<32, big_endian>::writeval(pov + off, inst);
+    }
+
+  // We always allocate 20 bytes for every stub, because final dynsym count is
+  // not known in method do_finalize_sections. There are 4 unused bytes per stub
+  // if final dynsym count is less than 0x10000.
+  // TODO(sasa): Can we strip unused bytes during the relaxation?
+  unsigned int used = this->symbols_.size() * (big_stub ? 20 : 16);
+  unsigned int unused = big_stub ? 0 : this->symbols_.size() * 4;
+  gold_assert(static_cast<section_size_type>(used + unused) == oview_size);
+
+  of->write_output_view(offset, oview_size, oview);
+}
+
+// Mips_output_section_reginfo methods.
+
+template<int size, bool big_endian>
+void
+Mips_output_section_reginfo<size, big_endian>::do_write(Output_file* of)
+{
+  typedef typename elfcpp::Swap<size, big_endian>::Valtype Valtype;
+  Valtype gprmask = 0, cprmask1 = 0, cprmask2 = 0, cprmask3 = 0, cprmask4 = 0;
+
+  for (Input_section_list::const_iterator p = this->input_sections().begin();
+       p != this->input_sections().end();
+       ++p)
+    {
+      Relobj* relobj = p->relobj();
+      unsigned int shndx = p->shndx();
+
+      section_size_type section_size;
+      const unsigned char* section_contents =
+        relobj->section_contents(shndx, &section_size, false);
+
+      gprmask |= elfcpp::Swap<size, big_endian>::readval(section_contents);
+      cprmask1 |= elfcpp::Swap<size, big_endian>::readval(section_contents + 4);
+      cprmask2 |= elfcpp::Swap<size, big_endian>::readval(section_contents + 8);
+      cprmask3 |=
+        elfcpp::Swap<size, big_endian>::readval(section_contents + 12);
+      cprmask4 |=
+        elfcpp::Swap<size, big_endian>::readval(section_contents + 16);
+    }
+
+  off_t offset = this->offset();
+  off_t data_size = this->data_size();
+
+  unsigned char* view = of->get_output_view(offset, data_size);
+  elfcpp::Swap<size, big_endian>::writeval(view, gprmask);
+  elfcpp::Swap<size, big_endian>::writeval(view + 4, cprmask1);
+  elfcpp::Swap<size, big_endian>::writeval(view + 8, cprmask2);
+  elfcpp::Swap<size, big_endian>::writeval(view + 12, cprmask3);
+  elfcpp::Swap<size, big_endian>::writeval(view + 16, cprmask4);
+  // Write the gp value.
+  elfcpp::Swap<size, big_endian>::writeval(view + 20, target_->gp_value());
+
+  of->write_output_view(offset, data_size, view);
+}
+
+// Target_mips methods.
+
+// Return the value to use for a dynamic symbol which requires special
+// treatment.  This is how we support equality comparisons of function
+// pointers across shared library boundaries, as described in the
+// processor specific ABI supplement.
+
+template<int size, bool big_endian>
+uint64_t
+Target_mips<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
+{
+  uint64_t value = 0;
+  const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
+
+  if (!mips_sym->has_lazy_stub())
+    {
+      if (mips_sym->has_plt_offset())
+        {
+          // We distinguish between PLT entries and lazy-binding stubs by
+          // giving the former an st_other value of STO_MIPS_PLT.  Set the
+          // value to the stub address if there are any relocations in the
+          // binary where pointer equality matters.
+          if (mips_sym->pointer_equality_needed())
+            value = this->plt_section()->address() + mips_sym->plt_offset();
+          else
+            value = 0;
+        }
+    }
+  else
+    {
+      this->mips_stubs()->set_lazy_stub_offsets();
+      // The run-time linker uses the st_value field of the symbol
+      // to reset the global offset table entry for this external
+      // to its stub address when unlinking a shared object.
+      value = this->mips_stubs()->address() + mips_sym->lazy_stub_offset();
+    }
+
+  Mips16_stub_section *fn_stub_section = this->get_mips16_fn_stub(mips_sym);
+  if (fn_stub_section != NULL)
+    // 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.
+    value = (fn_stub_section->output_section()->address()
+             + fn_stub_section->output_section_offset());
+
+  return value;
+}
+
+// Get the dynamic reloc section, creating it if necessary.
+
+template<int size, bool big_endian>
+typename Target_mips<size, big_endian>::Reloc_section*
+Target_mips<size, big_endian>::rel_dyn_section(Layout* layout)
+{
+  if (this->rel_dyn_ == NULL)
+    {
+      gold_assert(layout != NULL);
+      this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
+      layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
+                                      elfcpp::SHF_ALLOC, this->rel_dyn_,
+                                      ORDER_DYNAMIC_RELOCS, false);
+
+      // First entry in .rel.dyn has to be null.
+      // This is hack - we define dummy output data and set its address to 0,
+      // and define absolute R_MIPS_NONE relocation with offset 0 against it.
+      // This ensures that the entry is null.
+      Output_data *od = new Output_data_zero_fill(0, 0);
+      od->set_address(0);
+      this->rel_dyn_->add_absolute(elfcpp::R_MIPS_NONE, od, 0);
+    }
+  return this->rel_dyn_;
+}
+
+template<int size, bool big_endian>
+typename Target_mips<size, big_endian>::Reloca_section*
+Target_mips<size, big_endian>::rela_dyn_section(Layout* layout)
+{
+  if (this->rela_dyn_ == NULL)
+    {
+      gold_assert(layout != NULL);
+      this->rela_dyn_ = new Reloca_section(parameters->options().combreloc());
+      layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
+                                      elfcpp::SHF_ALLOC, this->rela_dyn_,
+                                      ORDER_DYNAMIC_RELOCS, false);
+    }
+  return this->rela_dyn_;
+}
+
+// Get the GOT section, creating it if necessary.
+
+template<int size, bool big_endian>
+Mips_output_data_got<size, big_endian>*
+Target_mips<size, big_endian>::got_section(Symbol_table* symtab,
+                                           Layout* layout)
+{
+  if (this->got_ == NULL)
+    {
+      gold_assert(symtab != NULL && layout != NULL);
+
+      this->got_ = new Mips_output_data_got<size, big_endian>(this, symtab,
+                                                              layout);
+      layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
+                                      (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE |
+                                      elfcpp::SHF_MIPS_GPREL),
+                                      this->got_, ORDER_DATA, false);
+
+      // Define _GLOBAL_OFFSET_TABLE_ at the start of the .got section.
+      symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
+                                    Symbol_table::PREDEFINED,
+                                    this->got_,
+                                    0, 0, elfcpp::STT_OBJECT,
+                                    elfcpp::STB_GLOBAL,
+                                    elfcpp::STV_DEFAULT, 0,
+                                    false, false);
+    }
+
+  return this->got_;
+}
+
+// Calculate value of _gp symbol.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::set_gp(Layout* layout, Symbol_table* symtab)
+{
+  if (this->gp_ != NULL)
+    return;
+
+  Output_data *section = layout->find_output_section(".got");
+  if (section == NULL)
+    {
+      // If there is no .got section, gp should be based on .sdata.
+      // TODO(sasa): If there are both .got and .sdata sections, they must be
+      // together, with .got comming first.
+      for (Layout::Section_list::const_iterator p = layout->section_list().begin();
+           p != layout->section_list().end();
+           ++p)
+        {
+          if (strcmp((*p)->name(), ".sdata") == 0)
+            {
+              section = *p;
+              break;
+            }
+        }
+    }
+
+  Sized_symbol<size>* gp =
+    static_cast<Sized_symbol<size>*>(symtab->lookup("_gp"));
+  if (gp != NULL)
+    {
+      if (gp->source() != Symbol::IS_CONSTANT && section != NULL)
+        gp->init_output_data(gp->name(), NULL, section, MIPS_GP_OFFSET, 0,
+                             elfcpp::STT_OBJECT,
+                             elfcpp::STB_GLOBAL,
+                             elfcpp::STV_DEFAULT, 0,
+                             false, false);
+      this->gp_ = gp;
+    }
+  else if (section != NULL)
+    {
+      gp = static_cast<Sized_symbol<size>*>(symtab->define_in_output_data(
+                                      "_gp", NULL, Symbol_table::PREDEFINED,
+                                      section, MIPS_GP_OFFSET, 0,
+                                      elfcpp::STT_OBJECT,
+                                      elfcpp::STB_GLOBAL,
+                                      elfcpp::STV_DEFAULT,
+                                      0, false, false));
+      this->gp_ = gp;
+    }
+}
+
+// Create a PLT entry for a global symbol.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::make_plt_entry(Symbol_table* symtab,
+                                              Layout* layout,
+                                              Symbol* gsym)
+{
+  Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
+  if (mips_sym->has_lazy_stub() || gsym->has_plt_offset())
+    return;
+
+  if (this->plt_ == NULL)
+    {
+      // Create the GOT section first.
+      this->got_section(symtab, layout);
+
+      this->got_plt_ = new Output_data_space(4, "** GOT PLT");
+      layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
+                                      (elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE),
+                                      this->got_plt_, ORDER_DATA, false);
+
+      // The first two entries are reserved.
+      this->got_plt_->set_current_data_size(2 * 4);
+
+      this->plt_ = new Mips_output_data_plt<size, big_endian>(layout,
+                                                              this->got_plt_);
+      layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
+                                      (elfcpp::SHF_ALLOC
+                                       | elfcpp::SHF_EXECINSTR),
+                                      this->plt_, ORDER_PLT, false);
+
+      // Define _PROCEDURE_LINKAGE_TABLE_ at the start of the .plt section.
+      symtab->define_in_output_data("_PROCEDURE_LINKAGE_TABLE_", NULL,
+                                    Symbol_table::PREDEFINED,
+                                    this->plt_,
+                                    0, 0, elfcpp::STT_FUNC,
+                                    elfcpp::STB_LOCAL,
+                                    elfcpp::STV_DEFAULT, 0,
+                                    false, false);
+    }
+
+  this->plt_->add_entry(gsym);
+}
+
+
+// Create a .MIPS.stubs entry for a global symbol.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::make_lazy_stub_entry(Layout* layout,
+                                                    Symbol* gsym)
+{
+  Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
+  if (mips_sym->has_lazy_stub())
+    return;
+  if (mips_sym->has_plt_offset())
+    return;
+
+  if (this->mips_stubs_ == NULL)
+    {
+      // Create the .MIPS.stubs section first.
+      this->mips_stubs_ =
+        new Mips_output_data_mips_stubs<size, big_endian>();
+
+      layout->add_output_section_data(".MIPS.stubs", elfcpp::SHT_PROGBITS,
+                                      (elfcpp::SHF_ALLOC
+                                       | elfcpp::SHF_EXECINSTR),
+                                      this->mips_stubs_, ORDER_PLT, false);
+    }
+  this->mips_stubs_->add_entry(mips_sym);
+}
+
+// Create LA25 stub section.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::create_la25_stub_section(Layout* layout)
+{
+  if (this->la25_stub_ == NULL)
+    {
+      this->la25_stub_ = new Mips_output_data_la25_stub<size, big_endian>();
+      layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
+                                      (elfcpp::SHF_ALLOC
+                                       | elfcpp::SHF_EXECINSTR
+                                       | elfcpp::SHF_WRITE),
+                                      this->la25_stub_, ORDER_TEXT, false);
+    }
+}
+
+// Process the relocations to determine unreferenced sections for
+// garbage collection.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::gc_process_relocs(
+                        Symbol_table* symtab,
+                        Layout* layout,
+                        Sized_relobj_file<size, big_endian>* object,
+                        unsigned int data_shndx,
+                        unsigned int,
+                        const unsigned char* prelocs,
+                        size_t reloc_count,
+                        Output_section* output_section,
+                        bool needs_special_offset_handling,
+                        size_t local_symbol_count,
+                        const unsigned char* plocal_symbols)
+{
+  typedef Target_mips<size, big_endian> Mips;
+  typedef typename Target_mips<size, big_endian>::Scan Scan;
+
+  gold::gc_process_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan,
+                          typename Target_mips::Relocatable_size_for_reloc>(
+    symtab,
+    layout,
+    this,
+    object,
+    data_shndx,
+    prelocs,
+    reloc_count,
+    output_section,
+    needs_special_offset_handling,
+    local_symbol_count,
+    plocal_symbols);
+}
+
+// Scan the relocations to look for symbol adjustments.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::scan_relocs(
+                        Symbol_table* symtab,
+                        Layout* layout,
+                        Sized_relobj_file<size, big_endian>* object,
+                        unsigned int data_shndx,
+                        unsigned int sh_type,
+                        const unsigned char* prelocs,
+                        size_t reloc_count,
+                        Output_section* output_section,
+                        bool needs_special_offset_handling,
+                        size_t local_symbol_count,
+                        const unsigned char* plocal_symbols)
+{
+  typedef Target_mips<size, big_endian> Mips;
+  typedef typename Target_mips<size, big_endian>::Scan Scan;
+
+  if (sh_type == elfcpp::SHT_REL)
+    gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_REL, Scan>(
+      symtab,
+      layout,
+      this,
+      object,
+      data_shndx,
+      prelocs,
+      reloc_count,
+      output_section,
+      needs_special_offset_handling,
+      local_symbol_count,
+      plocal_symbols);
+  else if (sh_type == elfcpp::SHT_RELA)
+    gold::scan_relocs<size, big_endian, Mips, elfcpp::SHT_RELA, Scan>(
+      symtab,
+      layout,
+      this,
+      object,
+      data_shndx,
+      prelocs,
+      reloc_count,
+      output_section,
+      needs_special_offset_handling,
+      local_symbol_count,
+      plocal_symbols);
+}
+
+template<int size, bool big_endian>
+bool
+Target_mips<size, big_endian>::mips_32bit_flags(elfcpp::Elf_Word flags)
+{
+  return ((flags & elfcpp::EF_MIPS_32BITMODE) != 0
+          || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_O32
+          || (flags & elfcpp::EF_MIPS_ABI) == elfcpp::E_MIPS_ABI_EABI32
+          || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_1
+          || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_2
+          || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32
+          || (flags & elfcpp::EF_MIPS_ARCH) == elfcpp::E_MIPS_ARCH_32R2);
+}
+
+// Return the MACH for a MIPS e_flags value.
+template<int size, bool big_endian>
+unsigned int
+Target_mips<size, big_endian>::elf_mips_mach(elfcpp::Elf_Word flags)
+{
+  switch (flags & elfcpp::EF_MIPS_MACH)
+    {
+    case elfcpp::E_MIPS_MACH_3900:
+      return mach_mips3900;
+
+    case elfcpp::E_MIPS_MACH_4010:
+      return mach_mips4010;
+
+    case elfcpp::E_MIPS_MACH_4100:
+      return mach_mips4100;
+
+    case elfcpp::E_MIPS_MACH_4111:
+      return mach_mips4111;
+
+    case elfcpp::E_MIPS_MACH_4120:
+      return mach_mips4120;
+
+    case elfcpp::E_MIPS_MACH_4650:
+      return mach_mips4650;
+
+    case elfcpp::E_MIPS_MACH_5400:
+      return mach_mips5400;
+
+    case elfcpp::E_MIPS_MACH_5500:
+      return mach_mips5500;
+
+    case elfcpp::E_MIPS_MACH_9000:
+      return mach_mips9000;
+
+    case elfcpp::E_MIPS_MACH_SB1:
+      return mach_mips_sb1;
+
+    case elfcpp::E_MIPS_MACH_LS2E:
+      return mach_mips_loongson_2e;
+
+    case elfcpp::E_MIPS_MACH_LS2F:
+      return mach_mips_loongson_2f;
+
+    case elfcpp::E_MIPS_MACH_LS3A:
+      return mach_mips_loongson_3a;
+
+    case elfcpp::E_MIPS_MACH_OCTEON2:
+      return mach_mips_octeon2;
+
+    case elfcpp::E_MIPS_MACH_OCTEON:
+      return mach_mips_octeon;
+
+    case elfcpp::E_MIPS_MACH_XLR:
+      return mach_mips_xlr;
+
+    default:
+      switch (flags & elfcpp::EF_MIPS_ARCH)
+        {
+        default:
+        case elfcpp::E_MIPS_ARCH_1:
+          return mach_mips3000;
+
+        case elfcpp::E_MIPS_ARCH_2:
+          return mach_mips6000;
+
+        case elfcpp::E_MIPS_ARCH_3:
+          return mach_mips4000;
+
+        case elfcpp::E_MIPS_ARCH_4:
+          return mach_mips8000;
+
+        case elfcpp::E_MIPS_ARCH_5:
+          return mach_mips5;
+
+        case elfcpp::E_MIPS_ARCH_32:
+          return mach_mipsisa32;
+
+        case elfcpp::E_MIPS_ARCH_64:
+          return mach_mipsisa64;
+
+        case elfcpp::E_MIPS_ARCH_32R2:
+          return mach_mipsisa32r2;
+
+        case elfcpp::E_MIPS_ARCH_64R2:
+          return mach_mipsisa64r2;
+        }
+    }
+
+  return 0;
+}
+
+// Check whether machine EXTENSION is an extension of machine BASE.
+template<int size, bool big_endian>
+bool
+Target_mips<size, big_endian>::mips_mach_extends(unsigned int base,
+                                                 unsigned int extension)
+{
+  if (extension == base)
+    return true;
+
+  if ((base == mach_mipsisa32)
+      && this->mips_mach_extends(mach_mipsisa64, extension))
+    return true;
+
+  if ((base == mach_mipsisa32r2)
+      && this->mips_mach_extends(mach_mipsisa64r2, extension))
+    return true;
+
+  for (unsigned int i = 0; i < this->mips_mach_extensions_.size(); ++i)
+    if (extension == this->mips_mach_extensions_[i].first)
+      {
+        extension = this->mips_mach_extensions_[i].second;
+        if (extension == base)
+          return true;
+      }
+
+  return false;
+}
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::merge_processor_specific_flags(
+    const std::string& name, elfcpp::Elf_Word in_flags,
+    unsigned char in_ei_class, bool dyn_obj)
+{
+  // If flags are not set yet, just copy them.
+  if (!this->are_processor_specific_flags_set())
+    {
+      this->set_processor_specific_flags(in_flags);
+      this->ei_class_ = in_ei_class;
+      this->mach_ = this->elf_mips_mach(in_flags);
+      return;
+    }
+
+  elfcpp::Elf_Word new_flags = in_flags;
+  elfcpp::Elf_Word old_flags = this->processor_specific_flags();
+  elfcpp::Elf_Word merged_flags = this->processor_specific_flags();
+  merged_flags |= new_flags & elfcpp::EF_MIPS_NOREORDER;
+
+  // Check flag compatibility.
+  new_flags &= ~elfcpp::EF_MIPS_NOREORDER;
+  old_flags &= ~elfcpp::EF_MIPS_NOREORDER;
+
+  // Some IRIX 6 BSD-compatibility objects have this bit set.  It
+  // doesn't seem to matter.
+  new_flags &= ~elfcpp::EF_MIPS_XGOT;
+  old_flags &= ~elfcpp::EF_MIPS_XGOT;
+
+  // MIPSpro generates ucode info in n64 objects.  Again, we should
+  // just be able to ignore this.
+  new_flags &= ~elfcpp::EF_MIPS_UCODE;
+  old_flags &= ~elfcpp::EF_MIPS_UCODE;
+
+  // DSOs should only be linked with CPIC code.
+  if (dyn_obj)
+    new_flags |= elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC;
+
+  if (new_flags == old_flags)
+    {
+      this->set_processor_specific_flags(merged_flags);
+      return;
+    }
+
+  if (((new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0)
+      != ((old_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC)) != 0))
+    gold_warning(_("%s: linking abicalls files with non-abicalls files"),
+                 name.c_str());
+
+  if (new_flags & (elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC))
+    merged_flags |= elfcpp::EF_MIPS_CPIC;
+  if (!(new_flags & elfcpp::EF_MIPS_PIC))
+    merged_flags &= ~elfcpp::EF_MIPS_PIC;
+
+  new_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
+  old_flags &= ~(elfcpp::EF_MIPS_PIC | elfcpp::EF_MIPS_CPIC);
+
+  // Compare the ISAs.
+  if (mips_32bit_flags(old_flags) != mips_32bit_flags(new_flags))
+    gold_error(_("%s: linking 32-bit code with 64-bit code"), name.c_str());
+  else if (!this->mips_mach_extends(this->elf_mips_mach(in_flags), this->mach_))
+    {
+      // Output ISA isn't the same as, or an extension of, input ISA.
+      if (this->mips_mach_extends(this->mach_, this->elf_mips_mach(in_flags)))
+        {
+          // Copy the architecture info from input object to output.  Also copy
+          // the 32-bit flag (if set) so that we continue to recognise
+          // output as a 32-bit binary.
+          this->mach_ = this->elf_mips_mach(in_flags);
+          merged_flags &= ~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH);
+          merged_flags |= (new_flags & (elfcpp::EF_MIPS_ARCH
+                           | elfcpp::EF_MIPS_MACH | elfcpp::EF_MIPS_32BITMODE));
+
+          // Copy across the ABI flags if output doesn't use them
+          // and if that was what caused us to treat input object as 32-bit.
+          if ((old_flags & elfcpp::EF_MIPS_ABI) == 0
+              && this->mips_32bit_flags(new_flags)
+              && !this->mips_32bit_flags(new_flags & ~elfcpp::EF_MIPS_ABI))
+            merged_flags |= new_flags & elfcpp::EF_MIPS_ABI;
+        }
+      else
+        // The ISAs aren't compatible.
+        gold_error(_("%s: linking %s module with previous %s modules"),
+                   name.c_str(), this->elf_mips_mach_name(in_flags),
+                   this->elf_mips_mach_name(merged_flags));
+    }
+
+  new_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
+                | elfcpp::EF_MIPS_32BITMODE));
+  old_flags &= (~(elfcpp::EF_MIPS_ARCH | elfcpp::EF_MIPS_MACH
+                | elfcpp::EF_MIPS_32BITMODE));
+
+  // Compare ABIs.  The 64-bit ABI does not use EF_MIPS_ABI. But, it does set
+  // EI_CLASS differently from any 32-bit ABI.
+  if ((new_flags & elfcpp::EF_MIPS_ABI) != (old_flags & elfcpp::EF_MIPS_ABI)
+      || (in_ei_class != this->ei_class_))
+    {
+      // Only error if both are set (to different values).
+      if (((new_flags & elfcpp::EF_MIPS_ABI)
+           && (old_flags & elfcpp::EF_MIPS_ABI))
+          || (in_ei_class != this->ei_class_))
+        gold_error(_("%s: ABI mismatch: linking %s module with "
+                     "previous %s modules"), name.c_str(),
+                   this->elf_mips_abi_name(in_flags, in_ei_class),
+                   this->elf_mips_abi_name(merged_flags, this->ei_class_));
+
+      new_flags &= ~elfcpp::EF_MIPS_ABI;
+      old_flags &= ~elfcpp::EF_MIPS_ABI;
+    }
+
+  // Compare ASEs.  Forbid linking MIPS16 and microMIPS ASE modules together
+  // and allow arbitrary mixing of the remaining ASEs (retain the union).
+  if ((new_flags & elfcpp::EF_MIPS_ARCH_ASE)
+      != (old_flags & elfcpp::EF_MIPS_ARCH_ASE))
+    {
+      int old_micro = old_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
+      int new_micro = new_flags & elfcpp::EF_MIPS_ARCH_ASE_MICROMIPS;
+      int old_m16 = old_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
+      int new_m16 = new_flags & elfcpp::EF_MIPS_ARCH_ASE_M16;
+      int micro_mis = old_m16 && new_micro;
+      int m16_mis = old_micro && new_m16;
+
+      if (m16_mis || micro_mis)
+        gold_error(_("%s: ASE mismatch: linking %s module with "
+                     "previous %s modules"), name.c_str(),
+                   m16_mis ? "MIPS16" : "microMIPS",
+                   m16_mis ? "microMIPS" : "MIPS16");
+
+      merged_flags |= new_flags & elfcpp::EF_MIPS_ARCH_ASE;
+
+      new_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
+      old_flags &= ~ elfcpp::EF_MIPS_ARCH_ASE;
+    }
+
+  // Warn about any other mismatches.
+  if (new_flags != old_flags)
+    gold_error(_("%s: uses different e_flags (0x%x) fields than previous "
+                 "modules (0x%x)"), name.c_str(), new_flags, old_flags);
+
+  this->set_processor_specific_flags(merged_flags);
+}
+
+// Adjust ELF file header.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::do_adjust_elf_header(
+    unsigned char* view,
+    int len) const
+{
+  gold_assert(len == elfcpp::Elf_sizes<size>::ehdr_size);
+
+  elfcpp::Ehdr<size, big_endian> ehdr(view);
+  unsigned char e_ident[elfcpp::EI_NIDENT];
+  memcpy(e_ident, ehdr.get_e_ident(), elfcpp::EI_NIDENT);
+
+  e_ident[elfcpp::EI_CLASS] = this->ei_class_;
+
+  elfcpp::Ehdr_write<size, big_endian> oehdr(view);
+  oehdr.put_e_ident(e_ident);
+}
+
+// do_make_elf_object to override the same function in the base class.
+// We need to use a target-specific sub-class of
+// Sized_relobj_file<size, big_endian> to store Mips specific information.
+// Hence we need to have our own ELF object creation.
+
+template<int size, bool big_endian>
+Object*
+Target_mips<size, big_endian>::do_make_elf_object(
+    const std::string& name,
+    Input_file* input_file,
+    off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
+{
+  int et = ehdr.get_e_type();
+  // ET_EXEC files are valid input for --just-symbols/-R,
+  // and we treat them as relocatable objects.
+  if (et == elfcpp::ET_REL
+      || (et == elfcpp::ET_EXEC && input_file->just_symbols()))
+    {
+      Mips_relobj<size, big_endian>* obj =
+        new Mips_relobj<size, big_endian>(name, input_file, offset, ehdr);
+      obj->setup();
+      return obj;
+    }
+  else if (et == elfcpp::ET_DYN)
+    {
+      // TODO(sasa): Should we create Mips_dynobj?
+      return Target::do_make_elf_object(name, input_file, offset, ehdr);
+    }
+  else
+    {
+      gold_error(_("%s: unsupported ELF file type %d"),
+                 name.c_str(), et);
+      return NULL;
+    }
+}
+
+// Finalize the sections.
+
+template <int size, bool big_endian>
+void
+Target_mips<size, big_endian>::do_finalize_sections(Layout* layout,
+                                        const Input_objects* input_objects,
+                                        Symbol_table* symtab)
+{
+  if (!parameters->doing_static_link()
+      && (strcmp(parameters->options().hash_style(), "gnu") == 0
+          || strcmp(parameters->options().hash_style(), "both") == 0))
+    {
+      // .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
+      // ways.  .gnu.hash needs symbols to be grouped by hash code whereas the
+      // MIPS ABI requires a mapping between the GOT and the symbol table.
+      gold_error(".gnu.hash is incompatible with the MIPS ABI");
+    }
+
+  // Set _gp value.
+  this->set_gp(layout, symtab);
+
+  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+       p != input_objects->relobj_end();
+       ++p)
+    {
+      Mips_relobj<size, big_endian>* mips_relobj =
+        Mips_relobj<size, big_endian>::as_mips_relobj(*p);
+      for (unsigned int i = 1; i < mips_relobj->shnum(); ++i)
+        {
+          const char *section_name = mips_relobj->section_name(i).c_str();
+          if (strcmp(section_name, ".reginfo") == 0)
+            {
+              mips_relobj->set_output_section(i, NULL);
+              break;
+            }
+        }
+    }
+
+  // Check for any mips16 stub sections that we can discard.
+  if (!parameters->options().relocatable())
+    {
+      for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+          p != input_objects->relobj_end();
+          ++p)
+        {
+          Mips_relobj<size, big_endian>* mips_relobj =
+            Mips_relobj<size, big_endian>::as_mips_relobj(*p);
+          std::map<unsigned int, Mips16_stub_section*>&
+            mips16_stub_sections = mips_relobj->get_mips16_stub_sections();
+          typename std::map<unsigned int, Mips16_stub_section*>
+            ::const_iterator it;
+          for (it = mips16_stub_sections.begin();
+               it != mips16_stub_sections.end(); ++it)
+            {
+              Mips16_stub_section* stub_section = it->second;
+              stub_section->find_target_from_relocs();
+              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 (!mips_relobj->has_local_non_16bit_call_relocs(
+                          stub_section->r_sym()))
+                        discard = true;
+                      else
+                        mips_relobj->set_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 (!mips_relobj->has_local_16bit_call_relocs(
+                          stub_section->r_sym()))
+                        discard = true;
+                      else
+                        mips_relobj->set_local_mips16_call_stub(stub_section);
+                    }
+                }
+              else
+                {
+                  Mips_symbol<size>* gsym =
+                    Mips_symbol<size>::as_mips_sym(stub_section->gsym());
+                  if (stub_section->is_fn_stub())
+                    {
+                      if (!this->add_mips16_fn_stub(gsym, stub_section))
+                        // We already have a stub for this function.
+                        discard = true;
+                      else if (should_add_dynsym_entry(gsym, 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(true);
+                          if (gsym->is_from_dynobj())
+                            gsym->set_needs_dynsym_value();
+
+                          gsym->set_symsize(
+                            mips_relobj->section_size(stub_section->shndx()));
+                          // Clear MIPS16 flag from st_other.
+                          gsym->set_nonvis(0);
+
+                        }
+                      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 (!this->add_mips16_call_stub(gsym, stub_section))
+                        // We already have a stub for this function.
+                        discard = true;
+                    }
+                  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 (!this->add_mips16_call_fp_stub(gsym,
+                                                              stub_section))
+                        // We already have a stub for this function.
+                        discard = true;
+                    }
+                }
+              if (discard)
+                mips_relobj->set_output_section(stub_section->shndx(), NULL);
+            }
+        }
+    }
+
+  // Merge processor-specific flags.
+  for (Input_objects::Relobj_iterator p = input_objects->relobj_begin();
+       p != input_objects->relobj_end();
+       ++p)
+    {
+      Mips_relobj<size, big_endian>* relobj =
+        Mips_relobj<size, big_endian>::as_mips_relobj(*p);
+
+      Input_file::Format format = relobj->input_file()->format();
+      if (format == Input_file::FORMAT_ELF)
+        {
+          // Read processor-specific flags in ELF file header.
+          const unsigned char* pehdr = relobj->get_view(
+                                            elfcpp::file_header_offset,
+                                            elfcpp::Elf_sizes<size>::ehdr_size,
+                                            true, false);
+
+          elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
+          elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
+          unsigned char ei_class = ehdr.get_e_ident()[elfcpp::EI_CLASS];
+
+          this->merge_processor_specific_flags(relobj->name(), in_flags,
+                                               ei_class, false);
+        }
+    }
+
+  for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
+       p != input_objects->dynobj_end();
+       ++p)
+    {
+      Sized_dynobj<size, big_endian>* dynobj =
+        static_cast<Sized_dynobj<size, big_endian>*>(*p);
+
+      // Read processor-specific flags.
+      const unsigned char* pehdr = dynobj->get_view(elfcpp::file_header_offset,
+                                           elfcpp::Elf_sizes<size>::ehdr_size,
+                                           true, false);
+
+      elfcpp::Ehdr<size, big_endian> ehdr(pehdr);
+      elfcpp::Elf_Word in_flags = ehdr.get_e_flags();
+      unsigned char ei_class = ehdr.get_e_ident()[elfcpp::EI_CLASS];
+
+      this->merge_processor_specific_flags(dynobj->name(), in_flags, ei_class,
+                                           true);
+    }
+
+  if (!parameters->options().relocatable() && !parameters->doing_static_link())
+    // In case there is no .got section, create one.
+    this->got_section(symtab, layout);
+
+  // Emit any relocs we saved in an attempt to avoid generating COPY
+  // relocs.
+  if (this->copy_relocs_.any_saved_relocs())
+    this->copy_relocs_.emit(this->rel_dyn_section(layout), symtab, layout,
+                            this);
+
+  if (this->copy_relocsa_.any_saved_relocs())
+    this->copy_relocsa_.emit(this->rela_dyn_section(layout), symtab, layout,
+                             this);
+
+  // Emit dynamic relocs.
+  for (typename std::vector<Dyn_reloc>::iterator p = this->dyn_relocs_.begin();
+       p != this->dyn_relocs_.end();
+       ++p)
+    p->emit(this->rel_dyn_section(layout), this->got_section(), symtab);
+
+  if (this->has_got_section())
+    this->got_section()->lay_out_got(this, layout, symtab, input_objects);
+
+  if (this->mips_stubs() != NULL)
+    this->mips_stubs()->set_needs_dynsym_value();
+
+  // Check for functions that might need $25 to be valid on entry.
+  // TODO(sasa): Can we do this without iterating over all symbols?
+  typedef Symbol_visitor_check_symbols<size, big_endian> Symbol_visitor;
+  symtab->for_all_symbols<size, Symbol_visitor>(Symbol_visitor(this, layout,
+                                                               symtab));
+
+  // Add NULL segment.
+  if (!parameters->options().relocatable())
+    layout->make_output_segment(elfcpp::PT_NULL, 0);
+
+  for (Layout::Section_list::const_iterator p = layout->section_list().begin();
+       p != layout->section_list().end();
+       ++p)
+    {
+      if ((*p)->type() == elfcpp::SHT_MIPS_REGINFO)
+        {
+          Mips_output_section_reginfo<size, big_endian>* reginfo =
+            Mips_output_section_reginfo<size, big_endian>::
+              as_mips_output_section_reginfo(*p);
+
+          if (!parameters->options().relocatable())
+            {
+              Output_segment *reginfo_segment =
+                layout->make_output_segment(elfcpp::PT_MIPS_REGINFO,
+                                            elfcpp::PF_R);
+              reginfo_segment->add_output_section_to_nonload(reginfo,
+                                                             elfcpp::PF_R);
+            }
+        }
+    }
+
+  // Fill in some more dynamic tags.
+  // TODO(sasa): Add more dynamic tags.
+  const Reloc_section* rel_plt = (this->plt_ == NULL
+                                  ? NULL : this->plt_->rel_plt());
+  layout->add_target_dynamic_tags(true, this->got_, rel_plt,
+                                  this->rel_dyn_, true, false);
+
+  Output_data_dynamic* const odyn = layout->dynamic_data();
+  if (odyn != NULL
+      && !parameters->options().relocatable()
+      && !parameters->doing_static_link())
+  {
+    unsigned int d_val;
+    // This element holds a 32-bit version id for the Runtime
+    // Linker Interface.  This will start at integer value 1.
+    d_val = 0x01;
+    odyn->add_constant(elfcpp::DT_MIPS_RLD_VERSION, d_val);
+
+    // Dynamic flags
+    d_val = elfcpp::RHF_NOTPOT;
+    odyn->add_constant(elfcpp::DT_MIPS_FLAGS, d_val);
+
+    // Save layout for using when emiting custom dynamic tags.
+    this->layout_ = layout;
+
+    // This member holds the base address of the segment.
+    odyn->add_custom(elfcpp::DT_MIPS_BASE_ADDRESS);
+
+    // This member holds the number of entries in the .dynsym section.
+    odyn->add_custom(elfcpp::DT_MIPS_SYMTABNO);
+
+    // This member holds the index of the first dynamic symbol
+    // table entry that corresponds to an entry in the global offset table.
+    odyn->add_custom(elfcpp::DT_MIPS_GOTSYM);
+
+    // This member holds the number of local got entries.
+    odyn->add_constant(elfcpp::DT_MIPS_LOCAL_GOTNO, this->got_->local_gotno());
+
+    if (this->plt_ != NULL)
+      // DT_MIPS_PLTGOT dynamic tag
+      odyn->add_section_address(elfcpp::DT_MIPS_PLTGOT, this->got_plt_);
+  }
+
+  Target::do_finalize_sections (layout, input_objects, symtab);
+ }
+
+// Get the custom dynamic tag value.
+template<int size, bool big_endian>
+unsigned int
+Target_mips<size, big_endian>::dynamic_tag_custom_value(elfcpp::DT tag) const
+{
+  Output_section *dynsym = this->layout_->find_output_section(".dynsym");
+  gold_assert(dynsym != NULL);
+
+  switch (tag)
+    {
+    case elfcpp::DT_MIPS_BASE_ADDRESS:
+      {
+        // The base address of the segment.
+        typedef std::vector<Output_segment*> Segment_list;
+        Segment_list segment_list;
+
+        // Find all readable PT_LOAD segments.
+        segment_list.push_back(this->layout_->find_output_segment(
+            elfcpp::PT_LOAD, elfcpp::PF_R, elfcpp::PF_W | elfcpp::PF_X));
+        segment_list.push_back(this->layout_->find_output_segment(
+            elfcpp::PT_LOAD, elfcpp::PF_R | elfcpp::PF_X, elfcpp::PF_W));
+        segment_list.push_back(this->layout_->find_output_segment(
+            elfcpp::PT_LOAD, elfcpp::PF_R | elfcpp::PF_W, elfcpp::PF_X));
+        segment_list.push_back(this->layout_->find_output_segment(
+            elfcpp::PT_LOAD, elfcpp::PF_R | elfcpp::PF_W | elfcpp::PF_X, 0));
+
+        unsigned int base_addr = -1U;
+        for (Segment_list::const_iterator p = segment_list.begin();
+             p != segment_list.end();
+             ++p)
+          {
+            if (*p != NULL && (*p)->vaddr() < base_addr)
+              base_addr = (*p)->vaddr();
+          }
+
+        return base_addr;
+      }
+
+    case elfcpp::DT_MIPS_SYMTABNO:
+      // The number of entries in the .dynsym section.
+      return (unsigned int)dynsym->data_size()/16;
+
+    case elfcpp::DT_MIPS_GOTSYM:
+      {
+        // The index of the first dynamic symbol table entry that corresponds
+        // to an entry in the global offset table.
+        unsigned int min_index = this->got_->global_got_index();
+        if (min_index == -1U)
+          min_index = (unsigned int)dynsym->data_size()/16;
+        return min_index;
+      }
+
+    default:
+      gold_error(_("Unknown dynamic tag 0x%x"), (unsigned int)tag);
+    }
+
+  return (unsigned int)-1;
+}
+
+// Relocate a section.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::relocate_section(
+                        const Relocate_info<size, big_endian>* relinfo,
+                        unsigned int sh_type,
+                        const unsigned char* prelocs,
+                        size_t reloc_count,
+                        Output_section* output_section,
+                        bool needs_special_offset_handling,
+                        unsigned char* view,
+                        typename elfcpp::Elf_types<size>::Elf_Addr address,
+                        section_size_type view_size,
+                        const Reloc_symbol_changes* reloc_symbol_changes)
+{
+  typedef Target_mips<size, big_endian> Mips;
+  typedef typename Target_mips<size, big_endian>::Relocate Mips_relocate;
+
+  if (sh_type == elfcpp::SHT_REL)
+    gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_REL,
+      Mips_relocate, gold::Default_comdat_behavior>(
+      relinfo,
+      this,
+      prelocs,
+      reloc_count,
+      output_section,
+      needs_special_offset_handling,
+      view,
+      address,
+      view_size,
+      reloc_symbol_changes);
+  else if (sh_type == elfcpp::SHT_RELA)
+    gold::relocate_section<size, big_endian, Mips, elfcpp::SHT_RELA,
+      Mips_relocate, gold::Default_comdat_behavior>(
+      relinfo,
+      this,
+      prelocs,
+      reloc_count,
+      output_section,
+      needs_special_offset_handling,
+      view,
+      address,
+      view_size,
+     reloc_symbol_changes);
+}
+
+// Return the size of a relocation while scanning during a relocatable
+// link.
+
+template<int size, bool big_endian>
+unsigned int
+Target_mips<size, big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
+    unsigned int r_type,
+    Relobj* object)
+{
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_NONE:
+    case elfcpp::R_MIPS_TLS_DTPMOD64:
+    case elfcpp::R_MIPS_TLS_DTPREL64:
+    case elfcpp::R_MIPS_TLS_TPREL64:
+      return 0;
+
+    case elfcpp::R_MIPS_32:
+    case elfcpp::R_MIPS_TLS_DTPMOD32:
+    case elfcpp::R_MIPS_TLS_DTPREL32:
+    case elfcpp::R_MIPS_TLS_TPREL32:
+    case elfcpp::R_MIPS_REL32:
+    case elfcpp::R_MIPS_PC32:
+    case elfcpp::R_MIPS_GPREL32:
+    case elfcpp::R_MIPS_JALR:
+      return 4;
+
+    case elfcpp::R_MIPS_16:
+    case elfcpp::R_MIPS_HI16:
+    case elfcpp::R_MIPS_LO16:
+    case elfcpp::R_MIPS_GPREL16:
+    case elfcpp::R_MIPS16_HI16:
+    case elfcpp::R_MIPS16_LO16:
+    case elfcpp::R_MIPS_PC16:
+    case elfcpp::R_MIPS_GOT16:
+    case elfcpp::R_MIPS16_GOT16:
+    case elfcpp::R_MIPS_CALL16:
+    case elfcpp::R_MIPS16_CALL16:
+    case elfcpp::R_MIPS_GOT_HI16:
+    case elfcpp::R_MIPS_CALL_HI16:
+    case elfcpp::R_MIPS_GOT_LO16:
+    case elfcpp::R_MIPS_CALL_LO16:
+    case elfcpp::R_MIPS_TLS_DTPREL_HI16:
+    case elfcpp::R_MIPS_TLS_DTPREL_LO16:
+    case elfcpp::R_MIPS_TLS_TPREL_HI16:
+    case elfcpp::R_MIPS_TLS_TPREL_LO16:
+    case elfcpp::R_MIPS16_GPREL:
+    case elfcpp::R_MIPS_GOT_DISP:
+    case elfcpp::R_MIPS_LITERAL:
+    case elfcpp::R_MIPS_GOT_PAGE:
+    case elfcpp::R_MIPS_GOT_OFST:
+    case elfcpp::R_MIPS_TLS_GD:
+    case elfcpp::R_MIPS_TLS_LDM:
+    case elfcpp::R_MIPS_TLS_GOTTPREL:
+      return 2;
+
+    // These relocations are not byte sized
+    case elfcpp::R_MIPS_26:
+    case elfcpp::R_MIPS16_26:
+      return 4;
+
+    case elfcpp::R_MIPS_COPY:
+    case elfcpp::R_MIPS_JUMP_SLOT:
+      object->error(_("unexpected reloc %u in object file"), r_type);
+      return 0;
+
+    default:
+      object->error(_("unsupported reloc %u in object file"), r_type);
+      return 0;
+  }
+}
+
+// Scan the relocs during a relocatable link.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::scan_relocatable_relocs(
+                        Symbol_table* symtab,
+                        Layout* layout,
+                        Sized_relobj_file<size, big_endian>* object,
+                        unsigned int data_shndx,
+                        unsigned int sh_type,
+                        const unsigned char* prelocs,
+                        size_t reloc_count,
+                        Output_section* output_section,
+                        bool needs_special_offset_handling,
+                        size_t local_symbol_count,
+                        const unsigned char* plocal_symbols,
+                        Relocatable_relocs* rr)
+{
+  gold_assert(sh_type == elfcpp::SHT_REL);
+
+  typedef Mips_scan_relocatable_relocs<big_endian, elfcpp::SHT_REL,
+    Relocatable_size_for_reloc> Scan_relocatable_relocs;
+
+  gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_REL,
+    Scan_relocatable_relocs>(
+    symtab,
+    layout,
+    object,
+    data_shndx,
+    prelocs,
+    reloc_count,
+    output_section,
+    needs_special_offset_handling,
+    local_symbol_count,
+    plocal_symbols,
+    rr);
+}
+
+// Relocate a section during a relocatable link.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::relocate_relocs(
+                        const Relocate_info<size, big_endian>* relinfo,
+                        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* rr,
+                        unsigned char* view,
+                        typename elfcpp::Elf_types<size>::Elf_Addr view_address,
+                        section_size_type view_size,
+                        unsigned char* reloc_view,
+                        section_size_type reloc_view_size)
+{
+  gold_assert(sh_type == elfcpp::SHT_REL);
+
+  gold::relocate_relocs<size, big_endian, elfcpp::SHT_REL>(
+    relinfo,
+    prelocs,
+    reloc_count,
+    output_section,
+    offset_in_output_section,
+    rr,
+    view,
+    view_address,
+    view_size,
+    reloc_view,
+    reloc_view_size);
+}
+
+// Perform target-specific processing in a relocatable link.  This is
+// only used if we use the relocation strategy RELOC_SPECIAL.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::relocate_special_relocatable(
+    const Relocate_info<size, big_endian>* relinfo,
+    unsigned int sh_type,
+    const unsigned char* preloc_in,
+    size_t relnum,
+    Output_section* output_section,
+    typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
+    unsigned char* view,
+    elfcpp::Elf_types<32>::Elf_Addr view_address,
+    section_size_type,
+    unsigned char* preloc_out)
+{
+  // We can only handle REL type relocation sections.
+  gold_assert(sh_type == elfcpp::SHT_REL);
+
+  typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc
+    Reltype;
+  typedef typename Reloc_types<elfcpp::SHT_REL, size, big_endian>::Reloc_write
+    Reltype_write;
+
+  typedef typename elfcpp::Elf_types<32>::Elf_Addr Mips_address;
+  typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
+
+  const Mips_address invalid_address = static_cast<Mips_address>(0) - 1;
+
+  Sized_relobj_file<size, big_endian>* object = relinfo->object;
+  const unsigned int local_count = object->local_symbol_count();
+
+  Reltype reloc(preloc_in);
+  Reltype_write reloc_write(preloc_out);
+
+  elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
+  const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
+  const unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
+
+  // Get the new symbol index.
+  // We only use RELOC_SPECIAL strategy in local relocations.
+  gold_assert(r_sym < local_count);
+
+  // We are adjusting a section symbol.  We need to find
+  // the symbol table index of the section symbol for
+  // the output section corresponding to input section
+  // in which this symbol is defined.
+  bool is_ordinary;
+  unsigned int shndx = object->local_symbol_input_shndx(r_sym, &is_ordinary);
+  gold_assert(is_ordinary);
+  Output_section* os = object->output_section(shndx);
+  gold_assert(os != NULL);
+  gold_assert(os->needs_symtab_index());
+  unsigned int new_symndx = os->symtab_index();
+
+  // Get the new offset--the location in the output section where
+  // this relocation should be applied.
+
+  Mips_address offset = reloc.get_r_offset();
+  Mips_address new_offset;
+  if (offset_in_output_section != invalid_address)
+    new_offset = offset + offset_in_output_section;
+  else
+    {
+      section_offset_type sot_offset =
+        convert_types<section_offset_type, Mips_address>(offset);
+      section_offset_type new_sot_offset =
+        output_section->output_offset(object, relinfo->data_shndx,
+                                      sot_offset);
+      gold_assert(new_sot_offset != -1);
+      new_offset = new_sot_offset;
+    }
+
+  // In an object file, r_offset is an offset within the section.
+  // In an executable or dynamic object, generated by
+  // --emit-relocs, r_offset is an absolute address.
+  if (!parameters->options().relocatable())
+    {
+      new_offset += view_address;
+      if (offset_in_output_section != invalid_address)
+        new_offset -= offset_in_output_section;
+    }
+
+  reloc_write.put_r_offset(new_offset);
+  reloc_write.put_r_info(elfcpp::elf_r_info<32>(new_symndx, r_type));
+
+  // Handle the reloc addend.
+  // The relocation uses a section symbol in the input file.
+  // We are adjusting it to use a section symbol in the output
+  // file.  The input section symbol refers to some address in
+  // the input section.  We need the relocation in the output
+  // file to refer to that same address.  This adjustment to
+  // the addend is the same calculation we use for a simple
+  // absolute relocation for the input section symbol.
+
+  const Symbol_value<size>* psymval = object->local_symbol(r_sym);
+
+  unsigned char* paddend = view + offset;
+  typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_26:
+      reloc_status = Reloc_funcs::rel26(paddend, object, psymval,
+          offset_in_output_section, true, 0, sh_type, NULL,
+          false /*TODO(sasa): cross mode jump*/, r_type, this->jal_to_bal());
+      break;
+
+    default:
+      gold_unreachable();
+    }
+
+  // Report any errors.
+  switch (reloc_status)
+    {
+    case Reloc_funcs::STATUS_OKAY:
+      break;
+    case Reloc_funcs::STATUS_OVERFLOW:
+      gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
+                             _("relocation overflow"));
+      break;
+    case Reloc_funcs::STATUS_BAD_RELOC:
+      gold_error_at_location(relinfo, relnum, reloc.get_r_offset(),
+        _("unexpected opcode while processing relocation"));
+      break;
+    default:
+      gold_unreachable();
+    }
+}
+
+// Optimize the TLS relocation type based on what we know about the
+// symbol.  IS_FINAL is true if the final address of this symbol is
+// known at link time.
+
+template<int size, bool big_endian>
+tls::Tls_optimization
+Target_mips<size, big_endian>::optimize_tls_reloc(bool, int)
+{
+  // FIXME: Currently we do not do any TLS optimization.
+  return tls::TLSOPT_NONE;
+}
+
+// Scan a relocation for a local symbol.
+
+template<int size, bool big_endian>
+inline void
+Target_mips<size, big_endian>::Scan::local(
+                        Symbol_table* symtab,
+                        Layout* layout,
+                        Target_mips<size, big_endian>* target,
+                        Sized_relobj_file<size, big_endian>* object,
+                        unsigned int data_shndx,
+                        Output_section* output_section,
+                        const elfcpp::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)
+{
+  if (is_discarded)
+    return;
+
+  typename elfcpp::Elf_types<size>::Elf_Addr r_offset;
+  typename elfcpp::Elf_types<size>::Elf_WXword r_info;
+  typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
+
+  if (rel_type == elfcpp::SHT_RELA)
+    {
+      r_offset = rela->get_r_offset();
+      r_info = rela->get_r_info();
+      r_addend = rela->get_r_addend();
+    }
+  else
+    {
+      r_offset = rel->get_r_offset();
+      r_info = rel->get_r_info();
+      r_addend = 0;
+    }
+
+  unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
+  Mips_relobj<size, big_endian>* mips_object =
+    Mips_relobj<size, big_endian>::as_mips_relobj(object);
+
+  const char *section_name = object->section_name(data_shndx).c_str();
+  if (is_prefix_of(".mips16.fn", section_name)
+      || is_prefix_of(".mips16.call.", section_name)
+      || is_prefix_of(".mips16.call.fp.", section_name))
+    {
+      Mips16_stub_section *stub_section =
+        mips_object->get_mips16_stub_section(data_shndx);
+      if (stub_section == NULL)
+        {
+          stub_section = new Mips16_stub_section(mips_object, data_shndx);
+          mips_object->add_mips16_stub_section(stub_section);
+        }
+
+      mips16_stub_reloc* reloc = new mips16_stub_reloc(r_type, r_sym, NULL);
+      stub_section->add_stub_reloc(reloc);
+    }
+
+  if (r_type == elfcpp::R_MIPS_NONE)
+    // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
+    // mips16 stub.
+    return;
+
+  if (!mips16_call_reloc(r_type) && !section_allows_mips16_refs(section_name))
+    // This reloc would need to refer to a MIPS16 hard-float stub, if
+    // there is one. We ignore MIPS16 stub sections and .pdr section when
+    // looking for relocs that would need to refer to MIPS16 stubs.
+    mips_object->add_local_non_16bit_call(r_sym);
+
+  if (r_type == elfcpp::R_MIPS16_26
+      && !section_allows_mips16_refs(section_name))
+    mips_object->add_local_16bit_call(r_sym);
+
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_GOT16:
+    case elfcpp::R_MIPS_CALL16:
+    case elfcpp::R_MIPS_CALL_HI16:
+    case elfcpp::R_MIPS_CALL_LO16:
+    case elfcpp::R_MIPS_GOT_HI16:
+    case elfcpp::R_MIPS_GOT_LO16:
+    case elfcpp::R_MIPS_GOT_PAGE:
+    case elfcpp::R_MIPS_GOT_OFST:
+    case elfcpp::R_MIPS_GOT_DISP:
+    case elfcpp::R_MIPS_TLS_GOTTPREL:
+    case elfcpp::R_MIPS_TLS_GD:
+    case elfcpp::R_MIPS_TLS_LDM:
+    case elfcpp::R_MIPS16_GOT16:
+    case elfcpp::R_MIPS16_CALL16:
+    case elfcpp::R_MIPS16_TLS_GOTTPREL:
+    case elfcpp::R_MIPS16_TLS_GD:
+    case elfcpp::R_MIPS16_TLS_LDM:
+    case elfcpp::R_MICROMIPS_GOT16:
+    case elfcpp::R_MICROMIPS_CALL16:
+    case elfcpp::R_MICROMIPS_CALL_HI16:
+    case elfcpp::R_MICROMIPS_CALL_LO16:
+    case elfcpp::R_MICROMIPS_GOT_HI16:
+    case elfcpp::R_MICROMIPS_GOT_LO16:
+    case elfcpp::R_MICROMIPS_GOT_PAGE:
+    case elfcpp::R_MICROMIPS_GOT_OFST:
+    case elfcpp::R_MICROMIPS_GOT_DISP:
+    case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
+    case elfcpp::R_MICROMIPS_TLS_GD:
+    case elfcpp::R_MICROMIPS_TLS_LDM:
+      // We need a GOT section.
+      target->got_section(symtab, layout);
+      break;
+
+    default:
+      break;
+    }
+
+    if (target->call_lo16_reloc(r_type)
+        || target->got_lo16_reloc(r_type)
+        || target->got_disp_reloc(r_type))
+      {
+        // We may need a local GOT entry for this relocation.  We
+        // don't count R_MIPS_GOT_PAGE because we can estimate the
+        // maximum number of pages needed by looking at the size of
+        // the segment.  Similar comments apply to R_MIPS*_GOT16 and
+        // R_MIPS*_CALL16.  We don't count R_MIPS_GOT_HI16, or
+        // R_MIPS_CALL_HI16 because these are always followed by an
+        // R_MIPS_GOT_LO16 or R_MIPS_CALL_LO16.
+        Mips_output_data_got<size, big_endian>* got =
+          target->got_section(symtab, layout);
+        unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
+        got->record_local_got_symbol(object, r_sym, r_addend, r_type, -1U);
+      }
+
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_CALL16:
+    case elfcpp::R_MIPS16_CALL16:
+    case elfcpp::R_MICROMIPS_CALL16:
+      gold_error(_("CALL16 reloc at 0x%lx not against global symbol "),
+                 (unsigned long)r_offset);
+      return;
+
+    case elfcpp::R_MIPS_GOT_PAGE:
+    case elfcpp::R_MICROMIPS_GOT_PAGE:
+    case elfcpp::R_MIPS16_GOT16:
+    case elfcpp::R_MIPS_GOT16:
+    case elfcpp::R_MIPS_GOT_HI16:
+    case elfcpp::R_MIPS_GOT_LO16:
+    case elfcpp::R_MICROMIPS_GOT16:
+    case elfcpp::R_MICROMIPS_GOT_HI16:
+    case elfcpp::R_MICROMIPS_GOT_LO16:
+      {
+        // This relocation needs a page entry in the GOT.
+        typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+
+        // Get the section contents.
+        section_size_type view_size = 0;
+        const unsigned char *view =
+          object->section_contents(data_shndx, &view_size, false);
+        view += r_offset;
+
+        Valtype val = elfcpp::Swap<32, big_endian>::readval(view);
+        Valtype addend = rel_type == elfcpp::SHT_REL ? val & 0xffff : r_addend;
+
+        if (rel_type == elfcpp::SHT_REL && target->got16_reloc(r_type))
+          target->got16_addends.push_back(got16_addend<size, big_endian>(
+              object, data_shndx, r_type, r_sym, addend));
+        else
+          target->got_section()->record_got_page_entry(object, r_sym, addend);
+        break;
+      }
+
+    case elfcpp::R_MIPS_LO16:
+    case elfcpp::R_MIPS16_LO16:
+    case elfcpp::R_MICROMIPS_LO16:
+      {
+        if (rel_type != elfcpp::SHT_REL)
+          break;
+
+        // Find corresponding R_MIPS_GOT16 relocation, calculate combined addend
+        // and record got page entry.
+        typename std::list<got16_addend<size, big_endian> >::iterator it =
+          target->got16_addends.begin();
+        while (it != target->got16_addends.end())
+          {
+            got16_addend<size, big_endian> _got16_addend = *it;
+            if (_got16_addend.object != object
+                || _got16_addend.shndx != data_shndx)
+              {
+                gold_error("Can't find matching LO16 reloc");
+                break;
+              }
+
+            unsigned int got16_type;
+            if (r_type == elfcpp::R_MIPS16_LO16)
+              got16_type = elfcpp::R_MIPS16_GOT16;
+            else if (r_type == elfcpp::R_MICROMIPS_LO16)
+              got16_type = elfcpp::R_MICROMIPS_GOT16;
+            else
+              got16_type = elfcpp::R_MIPS_GOT16;
+
+            if (_got16_addend.r_sym != r_sym
+                || _got16_addend.r_type != got16_type)
+              {
+                ++it;
+                continue;
+              }
+
+            // The combined value is the sum of the HI16 addend, left-shifted by
+            // sixteen bits, and the LO16 addend, sign extended.  (Usually, the
+            // code does a `lui' of the HI16 value, and then an `addiu' of the
+            // LO16 value.)
+
+            // According to the MIPS ELF ABI, the R_MIPS_LO16 relocation must
+            // be immediately following.  However, for the IRIX6 ABI, the next
+            // relocation may be a composed relocation consisting of several
+            // relocations for the same address.  In that case, the R_MIPS_LO16
+            // relocation may occur as one of these.  We permit a similar
+            // extension in general, as that is useful for GCC.
+
+            // In some cases GCC dead code elimination removes the LO16 but keeps
+            // the corresponding HI16.  This is strictly speaking a violation of
+            // the ABI but not immediately harmful.
+            typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+
+            // Get the section contents.
+            section_size_type view_size = 0;
+            const unsigned char *view =
+              object->section_contents(data_shndx, &view_size, false);
+            view += r_offset;
+
+            Valtype val = elfcpp::Swap<32, big_endian>::readval(view);
+            int32_t addend = Bits<16>::sign_extend32(val & 0xffff);
+
+            addend = (_got16_addend.addend << 16) + addend;
+            target->got_section()->record_got_page_entry(object, r_sym, addend);
+
+            it = target->got16_addends.erase(it);
+          }
+        break;
+      }
+    }
+
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_32:
+    case elfcpp::R_MIPS_REL32:
+    case elfcpp::R_MIPS_64:
+      {
+        if (parameters->options().output_is_position_independent())
+          {
+            // If building a shared library (or a position-independent
+            // executable), we need to create a dynamic relocation for
+            // this location.
+            Reloc_section* rel_dyn = target->rel_dyn_section(layout);
+            unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
+            if (lsym.get_st_type() != elfcpp::STT_SECTION)
+              rel_dyn->add_local(object, r_sym, elfcpp::R_MIPS_REL32,
+                                 output_section, data_shndx, r_offset);
+            else
+              {
+                gold_assert(lsym.get_st_value() == 0);
+                rel_dyn->add_symbolless_local_addend(object, r_sym,
+                                                     elfcpp::R_MIPS_REL32,
+                                                     output_section, data_shndx,
+                                                     r_offset);
+              }
+          }
+        break;
+      }
+
+    case elfcpp::R_MIPS_TLS_GOTTPREL:
+    case elfcpp::R_MIPS16_TLS_GOTTPREL:
+    case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
+    case elfcpp::R_MIPS_TLS_LDM:
+    case elfcpp::R_MIPS16_TLS_LDM:
+    case elfcpp::R_MICROMIPS_TLS_LDM:
+    case elfcpp::R_MIPS_TLS_GD:
+    case elfcpp::R_MIPS16_TLS_GD:
+    case elfcpp::R_MICROMIPS_TLS_GD:
+      {
+        unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
+        bool output_is_shared = parameters->options().shared();
+        const tls::Tls_optimization optimized_type
+            = Target_mips<size, big_endian>::optimize_tls_reloc(
+                                             !output_is_shared, r_type);
+        switch (r_type)
+          {
+          case elfcpp::R_MIPS_TLS_GD:
+          case elfcpp::R_MIPS16_TLS_GD:
+          case elfcpp::R_MICROMIPS_TLS_GD:
+            if (optimized_type == tls::TLSOPT_NONE)
+              {
+                // Create a pair of GOT entries for the module index and
+                // dtv-relative offset.
+                Mips_output_data_got<size, big_endian>* got =
+                  target->got_section(symtab, layout);
+                unsigned int shndx = lsym.get_st_shndx();
+                bool is_ordinary;
+                shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
+                if (!is_ordinary)
+                  {
+                    object->error(_("local symbol %u has bad shndx %u"),
+                                  r_sym, shndx);
+                    break;
+                  }
+                got->record_local_got_symbol(object, r_sym, r_addend, r_type,
+                                             shndx);
+              }
+            else
+              // FIXME: TLS optimization not supported yet.
+              gold_unreachable();
+            break;
+
+          case elfcpp::R_MIPS_TLS_LDM:
+          case elfcpp::R_MIPS16_TLS_LDM:
+          case elfcpp::R_MICROMIPS_TLS_LDM:
+            if (optimized_type == tls::TLSOPT_NONE)
+              // We always record LDM symbols as local with index 0.
+              target->got_section()->record_local_got_symbol(object, 0,
+                                                             r_addend, r_type,
+                                                             -1U);
+            else
+              // FIXME: TLS optimization not supported yet.
+              gold_unreachable();
+            break;
+          case elfcpp::R_MIPS_TLS_GOTTPREL:
+          case elfcpp::R_MIPS16_TLS_GOTTPREL:
+          case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
+            layout->set_has_static_tls();
+            if (optimized_type == tls::TLSOPT_NONE)
+              {
+                // Create a GOT entry for the tp-relative offset.
+                Mips_output_data_got<size, big_endian>* got =
+                  target->got_section(symtab, layout);
+                got->record_local_got_symbol(object, r_sym, r_addend, r_type,
+                                             -1U);
+              }
+            else
+              // FIXME: TLS optimization not supported yet.
+              gold_unreachable();
+            break;
+
+          default:
+            gold_unreachable();
+        }
+      }
+      break;
+
+    default:
+      break;
+    }
+
+  // Refuse some position-dependent relocations when creating a
+  // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
+  // not PIC, but we can create dynamic relocations and the result
+  // will be fine.  Also do not refuse R_MIPS_LO16, which can be
+  // combined with R_MIPS_GOT16.
+  if (parameters->options().shared())
+    {
+      switch (r_type)
+        {
+        case elfcpp::R_MIPS16_HI16:
+        case elfcpp::R_MIPS_HI16:
+        case elfcpp::R_MICROMIPS_HI16:
+          // Don't refuse a high part relocation if it's against
+          // no symbol (e.g. part of a compound relocation).
+          if (r_sym == 0)
+            break;
+
+          // FALLTHROUGH
+
+        case elfcpp::R_MIPS16_26:
+        case elfcpp::R_MIPS_26:
+        case elfcpp::R_MICROMIPS_26_S1:
+          gold_error(_("%s: relocation %u against `%s' can not be used when "
+                       "making a shared object; recompile with -fPIC"),
+                     object->name().c_str(), r_type, "a local symbol");
+        default:
+          break;
+        }
+    }
+}
+
+template<int size, bool big_endian>
+inline void
+Target_mips<size, big_endian>::Scan::local(
+                        Symbol_table* symtab,
+                        Layout* layout,
+                        Target_mips<size, big_endian>* target,
+                        Sized_relobj_file<size, big_endian>* object,
+                        unsigned int data_shndx,
+                        Output_section* output_section,
+                        const elfcpp::Rel<size, big_endian>& reloc,
+                        unsigned int r_type,
+                        const elfcpp::Sym<size, big_endian>& lsym,
+                        bool is_discarded)
+{
+  if (is_discarded)
+    return;
+
+  local(
+    symtab,
+    layout,
+    target,
+    object,
+    data_shndx,
+    output_section,
+    (const elfcpp::Rela<size, big_endian>*) NULL,
+    &reloc,
+    elfcpp::SHT_REL,
+    r_type,
+    lsym, is_discarded);
+}
+
+
+template<int size, bool big_endian>
+inline void
+Target_mips<size, big_endian>::Scan::local(
+                        Symbol_table* symtab,
+                        Layout* layout,
+                        Target_mips<size, big_endian>* target,
+                        Sized_relobj_file<size, big_endian>* object,
+                        unsigned int data_shndx,
+                        Output_section* output_section,
+                        const elfcpp::Rela<size, big_endian>& reloc,
+                        unsigned int r_type,
+                        const elfcpp::Sym<size, big_endian>& lsym,
+                        bool is_discarded)
+{
+  if (is_discarded)
+    return;
+
+  local(
+    symtab,
+    layout,
+    target,
+    object,
+    data_shndx,
+    output_section,
+    &reloc,
+    (const elfcpp::Rel<size, big_endian>*) NULL,
+    elfcpp::SHT_RELA,
+    r_type,
+    lsym, is_discarded);
+}
+
+// Scan a relocation for a global symbol.
+
+template<int size, bool big_endian>
+inline void
+Target_mips<size, big_endian>::Scan::global(
+                                Symbol_table* symtab,
+                                Layout* layout,
+                                Target_mips<size, big_endian>* target,
+                                Sized_relobj_file<size, big_endian>* object,
+                                unsigned int data_shndx,
+                                Output_section* output_section,
+                                const elfcpp::Rela<size, big_endian>* rela,
+                                const elfcpp::Rel<size, big_endian>* rel,
+                                unsigned int rel_type,
+                                unsigned int r_type,
+                                Symbol* gsym)
+{
+  typename elfcpp::Elf_types<size>::Elf_Addr r_offset;
+  typename elfcpp::Elf_types<size>::Elf_WXword r_info;
+  typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
+
+  if (rel_type == elfcpp::SHT_RELA)
+    {
+      r_offset = rela->get_r_offset();
+      r_info = rela->get_r_info();
+      r_addend = rela->get_r_addend();
+    }
+  else
+    {
+      r_offset = rel->get_r_offset();
+      r_info = rel->get_r_info();
+      r_addend = 0;
+    }
+
+  unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
+  Mips_relobj<size, big_endian>* mips_object =
+    Mips_relobj<size, big_endian>::as_mips_relobj(object);
+  Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
+
+  const char *section_name = object->section_name(data_shndx).c_str();
+  if (is_prefix_of(".mips16.fn", section_name)
+      || is_prefix_of(".mips16.call.", section_name)
+      || is_prefix_of(".mips16.call.fp.", section_name))
+    {
+      Mips16_stub_section *stub_section =
+        mips_object->get_mips16_stub_section(data_shndx);
+      if (stub_section == NULL)
+        {
+          stub_section = new Mips16_stub_section(mips_object, data_shndx);
+          mips_object->add_mips16_stub_section(stub_section);
+        }
+
+      mips16_stub_reloc* reloc = new mips16_stub_reloc(r_type, r_sym, gsym);
+      stub_section->add_stub_reloc(reloc);
+    }
+
+  if (r_type == elfcpp::R_MIPS_NONE)
+    // R_MIPS_NONE is used in mips16 stub sections, to define the target of the
+    // mips16 stub.
+    return;
+
+  if (!mips16_call_reloc(r_type) && !section_allows_mips16_refs(section_name))
+    // This reloc would need to refer to a MIPS16 hard-float stub, if
+    // there is one. We ignore MIPS16 stub sections and .pdr section when
+    // looking for relocs that would need to refer to MIPS16 stubs.
+    mips_sym->set_need_fn_stub(true);
+
+  // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
+  // section.  We check here to avoid creating a dynamic reloc against
+  // _GLOBAL_OFFSET_TABLE_.
+  if (!target->has_got_section()
+      && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
+    target->got_section(symtab, layout);
+
+  // We need PLT entries if there
+  // are static-only relocations against an externally-defined function.
+  // This can technically occur for shared libraries if there are
+  // branches to the symbol, although it is unlikely that this will be
+  // used in practice due to the short ranges involved.  It can occur
+  // for any relative or absolute relocation in executables; in that
+  // case, the PLT entry becomes the function's canonical address.
+  bool static_reloc = false;
+
+  // Set CAN_MAKE_DYNAMIC to true if we can convert this
+  // relocation into a dynamic one.
+  bool can_make_dynamic = false;
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_GOT16:
+    case elfcpp::R_MIPS_CALL16:
+    case elfcpp::R_MIPS_CALL_HI16:
+    case elfcpp::R_MIPS_CALL_LO16:
+    case elfcpp::R_MIPS_GOT_HI16:
+    case elfcpp::R_MIPS_GOT_LO16:
+    case elfcpp::R_MIPS_GOT_PAGE:
+    case elfcpp::R_MIPS_GOT_OFST:
+    case elfcpp::R_MIPS_GOT_DISP:
+    case elfcpp::R_MIPS_TLS_GOTTPREL:
+    case elfcpp::R_MIPS_TLS_GD:
+    case elfcpp::R_MIPS_TLS_LDM:
+    case elfcpp::R_MIPS16_GOT16:
+    case elfcpp::R_MIPS16_CALL16:
+    case elfcpp::R_MIPS16_TLS_GOTTPREL:
+    case elfcpp::R_MIPS16_TLS_GD:
+    case elfcpp::R_MIPS16_TLS_LDM:
+    case elfcpp::R_MICROMIPS_GOT16:
+    case elfcpp::R_MICROMIPS_CALL16:
+    case elfcpp::R_MICROMIPS_CALL_HI16:
+    case elfcpp::R_MICROMIPS_CALL_LO16:
+    case elfcpp::R_MICROMIPS_GOT_HI16:
+    case elfcpp::R_MICROMIPS_GOT_LO16:
+    case elfcpp::R_MICROMIPS_GOT_PAGE:
+    case elfcpp::R_MICROMIPS_GOT_OFST:
+    case elfcpp::R_MICROMIPS_GOT_DISP:
+    case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
+    case elfcpp::R_MICROMIPS_TLS_GD:
+    case elfcpp::R_MICROMIPS_TLS_LDM:
+      // We need a GOT section.
+      target->got_section(symtab, layout);
+      break;
+
+    // This is just a hint; it can safely be ignored.  Don't set
+    // has_static_relocs for the corresponding symbol.
+    case elfcpp::R_MIPS_JALR:
+    case elfcpp::R_MICROMIPS_JALR:
+      break;
+
+    case elfcpp::R_MIPS_32:
+    case elfcpp::R_MIPS_REL32:
+    case elfcpp::R_MIPS_64:
+      if (parameters->options().shared()
+          || strcmp(gsym->name(), "__gnu_local_gp") != 0)
+        {
+          can_make_dynamic = true;
+          break;
+        }
+      // Fall through.
+
+    default:
+      // Most static relocations require pointer equality, except
+      // for branches.
+      mips_sym->set_pointer_equality_needed();
+
+      // Fall through.
+
+    case elfcpp::R_MIPS_26:
+    case elfcpp::R_MIPS_PC16:
+    case elfcpp::R_MIPS16_26:
+    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:
+      static_reloc = true;
+      mips_sym->set_has_static_relocs();
+      break;
+    }
+
+  // If there are call relocations against an externally-defined symbol,
+  // see whether we can create a MIPS lazy-binding stub for it.  We can
+  // only do this if all references to the function are through call
+  // relocations, and in that case, the traditional lazy-binding stubs
+  // are much more efficient than PLT entries.
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS16_CALL16:
+    case elfcpp::R_MIPS_CALL16:
+    case elfcpp::R_MIPS_CALL_HI16:
+    case elfcpp::R_MIPS_CALL_LO16:
+    case elfcpp::R_MIPS_JALR:
+    case elfcpp::R_MICROMIPS_CALL16:
+    case elfcpp::R_MICROMIPS_CALL_HI16:
+    case elfcpp::R_MICROMIPS_CALL_LO16:
+    case elfcpp::R_MICROMIPS_JALR:
+      if (!mips_sym->no_lazy_stub())
+        {
+          if ((gsym->needs_plt_entry() && gsym->is_from_dynobj())
+              // Calls from shared objects to undefined symbols of type
+              // STT_NOTYPE need lazy-binding stub.
+              || (gsym->is_undefined() && parameters->options().shared()))
+            target->make_lazy_stub_entry(layout, gsym);
+        }
+      break;
+    default:
+      {
+        // We must not create a stub for a symbol that has relocations
+        // related to taking the function's address.
+        mips_sym->set_no_lazy_stub();
+        target->remove_lazy_stub_entry(mips_sym);
+        break;
+      }
+  }
+
+  if (target->relocation_needs_la25_stub (mips_object, r_type,
+      mips_sym->is_mips16()))
+    mips_sym->set_has_nonpic_branches(true);
+
+  // R_MIPS_HI16 against _gp_disp is used for $gp setup,
+  // and has a special meaning.
+  bool gp_disp_against_hi16 = (!mips_object->is_newabi()
+                               && strcmp(gsym->name(), "_gp_disp") == 0
+                               && (hi16_reloc(r_type) || lo16_reloc(r_type)));
+  if (static_reloc && gsym->needs_plt_entry())
+    {
+      target->make_plt_entry(symtab, layout, gsym);
+
+      // Since this is not a PC-relative relocation, we may be
+      // taking the address of a function.  In that case we need to
+      // set the entry in the dynamic symbol table to the address of
+      // the PLT entry.
+      if (gsym->is_from_dynobj() && !parameters->options().shared())
+        {
+          gsym->set_needs_dynsym_value();
+          // We distinguish between PLT entries and lazy-binding stubs by
+          // giving the former an st_other value of STO_MIPS_PLT.  Set the
+          // flag if there are any relocations in the binary where pointer
+          // equality matters.
+          if (mips_sym->pointer_equality_needed())
+            gsym->set_nonvis(elfcpp::STO_MIPS_PLT>>2);
+        }
+    }
+  if ((static_reloc || can_make_dynamic) && !gp_disp_against_hi16)
+    {
+      // Absolute addressing relocations.
+      // Make a dynamic relocation if necessary.
+      if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
+        {
+          if (gsym->may_need_copy_reloc())
+            {
+              if (rel_type == elfcpp::SHT_RELA)
+                target->copy_reloc(symtab, layout, object,
+                                   data_shndx, output_section, gsym, *rela);
+              else
+                target->copy_reloc(symtab, layout, object,
+                                   data_shndx, output_section, gsym, *rel);
+            }
+          else if (can_make_dynamic)
+            {
+              // Create .rel.dyn section.
+              target->rel_dyn_section(layout);
+              target->dynamic_reloc(mips_sym, elfcpp::R_MIPS_REL32,
+                                    object, data_shndx, output_section,
+                                    r_offset);
+            }
+          else
+            gold_error(_("non-dynamic relocations refer to dynamic symbol %s"),
+                       gsym->name());
+        }
+    }
+
+  bool for_call = false;
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_CALL16:
+    case elfcpp::R_MIPS16_CALL16:
+    case elfcpp::R_MICROMIPS_CALL16:
+    case elfcpp::R_MIPS_CALL_HI16:
+    case elfcpp::R_MIPS_CALL_LO16:
+    case elfcpp::R_MICROMIPS_CALL_HI16:
+    case elfcpp::R_MICROMIPS_CALL_LO16:
+      for_call = true;
+      // Fall through.
+
+    case elfcpp::R_MIPS16_GOT16:
+    case elfcpp::R_MIPS_GOT16:
+    case elfcpp::R_MIPS_GOT_HI16:
+    case elfcpp::R_MIPS_GOT_LO16:
+    case elfcpp::R_MICROMIPS_GOT16:
+    case elfcpp::R_MICROMIPS_GOT_HI16:
+    case elfcpp::R_MICROMIPS_GOT_LO16:
+    case elfcpp::R_MIPS_GOT_DISP:
+    case elfcpp::R_MICROMIPS_GOT_DISP:
+      {
+        // The symbol requires a GOT entry.
+        Mips_output_data_got<size, big_endian> *got =
+          target->got_section(symtab, layout);
+        got->record_global_got_symbol(mips_sym, object, r_type, false,
+                                      for_call);
+        mips_sym->set_global_got_area(GGA_NORMAL);
+      }
+      break;
+
+    case elfcpp::R_MIPS_GOT_PAGE:
+    case elfcpp::R_MICROMIPS_GOT_PAGE:
+      {
+        // This relocation needs a page entry in the GOT.
+        typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+
+        // Get the section contents.
+        section_size_type view_size = 0;
+        const unsigned char *view =
+          object->section_contents(data_shndx, &view_size, false);
+        view += r_offset;
+
+        Valtype val = elfcpp::Swap<32, big_endian>::readval(view);
+        Valtype addend = rel_type == elfcpp::SHT_REL ? val & 0xffff : r_addend;
+        Mips_output_data_got<size, big_endian> *got =
+          target->got_section(symtab, layout);
+        got->record_got_page_entry(object, r_sym, addend);
+
+        // If this is a global, overridable symbol, GOT_PAGE will
+        // decay to GOT_DISP, so we'll need a GOT entry for it.
+        bool def_regular = (mips_sym->source() == Symbol::FROM_OBJECT
+                            && !mips_sym->object()->is_dynamic()
+                            && !mips_sym->is_undefined());
+        if (!def_regular
+            || (parameters->options().output_is_position_independent()
+                && !parameters->options().Bsymbolic()
+                && !mips_sym->is_forced_local()))
+          {
+            got->record_global_got_symbol(mips_sym, object, r_type, false,
+                                          for_call);
+            mips_sym->set_global_got_area(GGA_NORMAL);
+          }
+      }
+      break;
+
+    case elfcpp::R_MIPS_TLS_GOTTPREL:
+    case elfcpp::R_MIPS16_TLS_GOTTPREL:
+    case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
+    case elfcpp::R_MIPS_TLS_LDM:
+    case elfcpp::R_MIPS16_TLS_LDM:
+    case elfcpp::R_MICROMIPS_TLS_LDM:
+    case elfcpp::R_MIPS_TLS_GD:
+    case elfcpp::R_MIPS16_TLS_GD:
+    case elfcpp::R_MICROMIPS_TLS_GD:
+      {
+        const bool is_final = gsym->final_value_is_known();
+        const tls::Tls_optimization optimized_type =
+          Target_mips<size, big_endian>::optimize_tls_reloc(is_final, r_type);
+
+        switch (r_type)
+          {
+          case elfcpp::R_MIPS_TLS_GD:
+          case elfcpp::R_MIPS16_TLS_GD:
+          case elfcpp::R_MICROMIPS_TLS_GD:
+            if (optimized_type == tls::TLSOPT_NONE)
+              {
+                // Create a pair of GOT entries for the module index and
+                // dtv-relative offset.
+                Mips_output_data_got<size, big_endian>* got =
+                  target->got_section(symtab, layout);
+                got->record_global_got_symbol(mips_sym, object, r_type, false,
+                                              false);
+              }
+            else
+              // FIXME: TLS optimization not supported yet.
+              gold_unreachable();
+            break;
+
+          case elfcpp::R_MIPS_TLS_LDM:
+          case elfcpp::R_MIPS16_TLS_LDM:
+          case elfcpp::R_MICROMIPS_TLS_LDM:
+            if (optimized_type == tls::TLSOPT_NONE)
+              // We always record LDM symbols as local with index 0.
+              target->got_section()->record_local_got_symbol(object, 0,
+                                                             r_addend, r_type,
+                                                             -1U);
+            else
+              // FIXME: TLS optimization not supported yet.
+              gold_unreachable();
+            break;
+          case elfcpp::R_MIPS_TLS_GOTTPREL:
+          case elfcpp::R_MIPS16_TLS_GOTTPREL:
+          case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
+            layout->set_has_static_tls();
+            if (optimized_type == tls::TLSOPT_NONE)
+              {
+                // Create a GOT entry for the tp-relative offset.
+                Mips_output_data_got<size, big_endian>* got =
+                  target->got_section(symtab, layout);
+                got->record_global_got_symbol(mips_sym, object, r_type, false,
+                                              false);
+              }
+            else
+              // FIXME: TLS optimization not supported yet.
+              gold_unreachable();
+            break;
+
+          default:
+            gold_unreachable();
+        }
+      }
+      break;
+    case elfcpp::R_MIPS_COPY:
+    case elfcpp::R_MIPS_JUMP_SLOT:
+      // These are relocations which should only be seen by the
+      // dynamic linker, and should never be seen here.
+      gold_error(_("%s: unexpected reloc %u in object file"),
+                 object->name().c_str(), r_type);
+      break;
+
+    default:
+      break;
+    }
+
+  // Refuse some position-dependent relocations when creating a
+  // shared library.  Do not refuse R_MIPS_32 / R_MIPS_64; they're
+  // not PIC, but we can create dynamic relocations and the result
+  // will be fine.  Also do not refuse R_MIPS_LO16, which can be
+  // combined with R_MIPS_GOT16.
+  if (parameters->options().shared())
+    {
+      switch (r_type)
+        {
+        case elfcpp::R_MIPS16_HI16:
+        case elfcpp::R_MIPS_HI16:
+        case elfcpp::R_MICROMIPS_HI16:
+          // Don't refuse a high part relocation if it's against
+          // no symbol (e.g. part of a compound relocation).
+          if (r_sym == 0)
+            break;
+
+          // R_MIPS_HI16 against _gp_disp is used for $gp setup,
+          // and has a special meaning.
+          if (!mips_object->is_newabi()
+              && strcmp(gsym->name(), "_gp_disp") == 0)
+            break;
+
+          // FALLTHROUGH
+
+        case elfcpp::R_MIPS16_26:
+        case elfcpp::R_MIPS_26:
+        case elfcpp::R_MICROMIPS_26_S1:
+          gold_error(_("%s: relocation %u against `%s' can not be used when "
+                       "making a shared object; recompile with -fPIC"),
+                     object->name().c_str(), r_type, gsym->name());
+        default:
+          break;
+        }
+    }
+}
+
+template<int size, bool big_endian>
+inline void
+Target_mips<size, big_endian>::Scan::global(
+                                Symbol_table* symtab,
+                                Layout* layout,
+                                Target_mips<size, big_endian>* target,
+                                Sized_relobj_file<size, big_endian>* object,
+                                unsigned int data_shndx,
+                                Output_section* output_section,
+                                const elfcpp::Rela<size, big_endian>& reloc,
+                                unsigned int r_type,
+                                Symbol* gsym)
+{
+  global(
+    symtab,
+    layout,
+    target,
+    object,
+    data_shndx,
+    output_section,
+    &reloc,
+    (const elfcpp::Rel<size, big_endian>*) NULL,
+    elfcpp::SHT_RELA,
+    r_type,
+    gsym);
+}
+
+template<int size, bool big_endian>
+inline void
+Target_mips<size, big_endian>::Scan::global(
+                                Symbol_table* symtab,
+                                Layout* layout,
+                                Target_mips<size, big_endian>* target,
+                                Sized_relobj_file<size, big_endian>* object,
+                                unsigned int data_shndx,
+                                Output_section* output_section,
+                                const elfcpp::Rel<size, big_endian>& reloc,
+                                unsigned int r_type,
+                                Symbol* gsym)
+{
+  global(
+    symtab,
+    layout,
+    target,
+    object,
+    data_shndx,
+    output_section,
+    (const elfcpp::Rela<size, big_endian>*) NULL,
+    &reloc,
+    elfcpp::SHT_REL,
+    r_type,
+    gsym);
+}
+
+// Return whether a R_MIPS_32 relocation needs to be applied.
+
+template<int size, bool big_endian>
+inline bool
+Target_mips<size, big_endian>::Relocate::should_apply_r_mips_32_reloc(
+    const Mips_symbol<size>* gsym,
+    unsigned int r_type,
+    Output_section* output_section,
+    Target_mips* target)
+{
+  // If the output section is not allocated, then we didn't call
+  // scan_relocs, we didn't create a dynamic reloc, and we must apply
+  // the reloc here.
+  if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
+      return true;
+
+  if (gsym == NULL)
+    return true;
+  else
+    {
+      // For global symbols, we use the same helper routines used in the
+      // scan pass.
+      if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
+          && !gsym->may_need_copy_reloc())
+        {
+          // We have generated dynamic reloc (R_MIPS_REL32).
+
+          bool multi_got = false;
+          if (target->has_got_section())
+            multi_got = target->got_section()->multi_got();
+          bool has_got_offset;
+          if (!multi_got)
+            has_got_offset = gsym->has_got_offset(GOT_TYPE_STANDARD);
+          else
+            has_got_offset = gsym->global_gotoffset() != -1U;
+          if (!has_got_offset)
+            return true;
+          else
+            // Apply the relocation only if the symbol is in the local got.
+            // Do not apply the relocation if the symbol is in the global
+            // got.
+            return symbol_references_local(gsym, gsym->has_dynsym_index());
+        }
+      else
+        // We have not generated dynamic reloc.
+        return true;
+    }
+}
+
+// Perform a relocation.
+
+template<int size, bool big_endian>
+inline bool
+Target_mips<size, big_endian>::Relocate::relocate(
+                        const Relocate_info<size, big_endian>* relinfo,
+                        Target_mips* target,
+                        Output_section* output_section,
+                        size_t relnum,
+                        const elfcpp::Rela<size, big_endian>* rela,
+                        const elfcpp::Rel<size, big_endian>* rel,
+                        unsigned int rel_type,
+                        unsigned int r_type,
+                        const Sized_symbol<size>* gsym,
+                        const Symbol_value<size>* psymval,
+                        unsigned char* view,
+                        typename elfcpp::Elf_types<size>::Elf_Addr address,
+                        section_size_type)
+{
+  typename elfcpp::Elf_types<size>::Elf_Addr r_offset;
+  typename elfcpp::Elf_types<size>::Elf_WXword r_info;
+  typename elfcpp::Elf_types<size>::Elf_Swxword r_addend;
+
+  if (rel_type == elfcpp::SHT_RELA)
+    {
+      r_offset = rela->get_r_offset();
+      r_info = rela->get_r_info();
+      r_addend = rela->get_r_addend();
+    }
+  else
+    {
+      r_offset = rel->get_r_offset();
+      r_info = rel->get_r_info();
+      r_addend = 0;
+    }
+
+  typedef Mips_relocate_functions<size, big_endian> Reloc_funcs;
+  typename Reloc_funcs::Status reloc_status = Reloc_funcs::STATUS_OKAY;
+
+  unsigned int got_offset = 0;
+  Sized_relobj_file<size, big_endian>* object = relinfo->object;
+
+  unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
+  bool target_is_16_bit_code = false;
+  bool target_is_micromips_code = false;
+  bool cross_mode_jump;
+
+  Symbol_value<size> symval;
+
+  Mips_relobj<size, big_endian>* mips_object =
+    Mips_relobj<size, big_endian>::as_mips_relobj(object);
+  const Mips_symbol<size>* mips_sym = Mips_symbol<size>::as_mips_sym(gsym);
+
+  bool changed_mips16_value = false;
+  if (gsym == NULL)
+    {
+      target_is_16_bit_code = mips_object->local_symbol_is_mips16(r_sym);
+      target_is_micromips_code = mips_object->local_symbol_is_micromips(r_sym);
+      if (target_is_16_bit_code || target_is_micromips_code)
+        {
+          // MIPS16/microMIPS text labels should be treated as odd.
+          symval.set_output_value(psymval->value(object, 1));
+          psymval = &symval;
+          changed_mips16_value = true;
+        }
+    }
+  else
+    {
+      target_is_16_bit_code = mips_sym->is_mips16();
+      // If the output section is the PLT section, then the target is not
+      // microMIPS.
+      target_is_micromips_code = (mips_sym->is_micromips()
+          && !gsym->use_plt_offset(Scan::get_reference_flags(r_type)));
+
+      // If this is a mips16/microMIPS text symbol, add 1 to the value to make
+      // it odd.  This will cause something like .word SYM to come up with
+      // the right value when it is loaded into the PC.
+
+      if (mips_sym->is_mips16() || mips_sym->is_micromips())
+        {
+          symval.set_output_value(psymval->value(object, 1));
+          psymval = &symval;
+          changed_mips16_value = true;
+        }
+
+      // Pick the value to use for symbols defined in shared objects.
+      if (mips_sym->use_plt_offset(Scan::get_reference_flags(r_type))
+          || mips_sym->has_lazy_stub())
+        {
+          elfcpp::Elf_Xword value;
+
+          if (!mips_sym->has_lazy_stub())
+            value = target->plt_section()->address() + mips_sym->plt_offset();
+          else
+            value = (target->mips_stubs()->address()
+                     + mips_sym->lazy_stub_offset());
+
+          symval.set_output_value(value);
+          psymval = &symval;
+        }
+    }
+
+  // TRUE if the symbol referred to by this relocation is "_gp_disp".
+  // Note that such a symbol must always be a global symbol.
+  bool gp_disp = (gsym != NULL && (strcmp(gsym->name(), "_gp_disp") == 0)
+                  && !mips_object->is_newabi());
+
+  // TRUE if the symbol referred to by this relocation is "__gnu_local_gp".
+  // Note that such a symbol must always be a global symbol.
+  bool gnu_local_gp = gsym && (strcmp(gsym->name(), "__gnu_local_gp") == 0);
+
+
+  if (gp_disp)
+    {
+      if (!target->hi16_reloc(r_type) && !target->lo16_reloc(r_type))
+        gold_error_at_location(relinfo, relnum, r_offset,
+          _("relocations against _gp_disp are permitted only"
+            " with R_MIPS_HI16 and R_MIPS_LO16 relocations."));
+    }
+  else if (gnu_local_gp)
+    {
+      // __gnu_local_gp is _gp symbol.
+      symval.set_output_value(target->adjusted_gp_value(object));
+      psymval = &symval;
+    }
+
+  // If this is a reference to a 16-bit function with a stub, we need
+  // to redirect the relocation to the stub unless:
+  //
+  // (a) the relocation is for a MIPS16 JAL;
+  //
+  // (b) the relocation is for a MIPS16 PIC call, and there are no
+  //     non-MIPS16 uses of the GOT slot; or
+  //
+  // (c) the section allows direct references to MIPS16 functions.
+  if (r_type != elfcpp::R_MIPS16_26
+      && !parameters->options().relocatable()
+      && ((mips_sym != NULL
+           && target->get_mips16_fn_stub(mips_sym) != NULL
+           && (r_type != elfcpp::R_MIPS16_CALL16 || mips_sym->need_fn_stub()))
+          || (mips_sym == NULL
+              && mips_object->get_local_mips16_fn_stub(r_sym) != NULL))
+      && !section_allows_mips16_refs(object->section_name(
+                                                relinfo->data_shndx).c_str()))
+    {
+      // This is a 32- or 64-bit call to a 16-bit function.  We should
+      // have already noticed that we were going to need the
+      // stub.
+      elfcpp::Elf_Xword value;
+      if (mips_sym == NULL)
+        {
+          Mips16_stub_section *fn_stub_section =
+            mips_object->get_local_mips16_fn_stub(r_sym);
+          value = (fn_stub_section->output_section()->address()
+                   + fn_stub_section->output_section_offset());
+        }
+      else
+        {
+          gold_assert(mips_sym->need_fn_stub());
+          if (mips_sym->has_la25_stub())
+            {
+              // If a LA25 header for the stub itself exists, point to the
+              // prepended LUI/ADDIU sequence.
+              value = (target->la25_stub_section()->address()
+                       + mips_sym->la25_stub_offset());
+            }
+          else
+            {
+              Mips16_stub_section *fn_stub_section =
+                target->get_mips16_fn_stub(mips_sym);
+              value = (fn_stub_section->output_section()->address()
+                       + fn_stub_section->output_section_offset());
+            }
+          }
+      symval.set_output_value(value);
+      psymval = &symval;
+      changed_mips16_value = true;
+
+      // The target is 16-bit, but the stub isn't.
+      target_is_16_bit_code = false;
+    }
+  // If this is a 16-bit call to a 32- or 64-bit function with a stub, we
+  // need to redirect the call to the stub.  Note that we specifically
+  // exclude R_MIPS16_CALL16 from this behavior; indirect calls should
+  // use an indirect stub instead.
+  else if (r_type == elfcpp::R_MIPS16_26 && !parameters->options().relocatable()
+           && ((mips_sym != NULL
+                && (target->get_mips16_call_stub(mips_sym) != NULL
+                    || target->get_mips16_call_fp_stub(mips_sym) != NULL))
+               || (mips_sym == NULL
+                   && mips_object->get_local_mips16_call_stub(r_sym) != NULL))
+           && !target_is_16_bit_code)
+    {
+      elfcpp::Elf_Xword value;
+      Mips16_stub_section *call_stub_section;
+      if (mips_sym == NULL)
+        call_stub_section = mips_object->get_local_mips16_call_stub(r_sym);
+      else
+        {
+          // If both call_stub and call_fp_stub are defined, we can figure
+          // out which one to use by checking which one appears in the input
+          // file.
+          if (target->get_mips16_call_stub(mips_sym) != NULL
+              && target->get_mips16_call_fp_stub(mips_sym) != NULL)
+            {
+              call_stub_section = NULL;
+              for (unsigned int i = 1; i < mips_object->shnum(); ++i)
+                {
+                  const char *section_name =
+                    mips_object->section_name(i).c_str();
+                  if (is_prefix_of(".mips16.call.fp.", section_name))
+                    {
+                      call_stub_section =
+                        target->get_mips16_call_fp_stub(mips_sym);
+                      break;
+                    }
+
+                }
+              if (call_stub_section == NULL)
+                call_stub_section = target->get_mips16_call_stub(mips_sym);
+            }
+          else if (target->get_mips16_call_stub(mips_sym) != NULL)
+            call_stub_section = target->get_mips16_call_stub(mips_sym);
+          else
+            call_stub_section = target->get_mips16_call_fp_stub(mips_sym);
+        }
+
+      value = (call_stub_section->output_section()->address()
+               + call_stub_section->output_section_offset());
+      symval.set_output_value(value);
+      psymval = &symval;
+      changed_mips16_value = true;
+    }
+  // If this is a direct call to a PIC function, redirect to the
+  // non-PIC stub.
+  else if (mips_sym != NULL
+           && mips_sym->has_la25_stub()
+           && target->relocation_needs_la25_stub (mips_object, r_type,
+                                                  target_is_16_bit_code))
+    {
+      elfcpp::Elf_Xword value;
+      value = (target->la25_stub_section()->address()
+               + mips_sym->la25_stub_offset());
+      symval.set_output_value(value);
+      psymval = &symval;
+    }
+
+  // Calls from 16-bit code to 32-bit code and vice versa require the
+  // mode change.  However, we can ignore calls to undefined weak symbols,
+  // which should never be executed at runtime.  This exception is important
+  // because the assembly writer may have "known" that any definition of the
+  // symbol would be 16-bit code, and that direct jumps were therefore
+  // acceptable.
+  cross_mode_jump =
+    (!parameters->options().relocatable()
+     && !(gsym != NULL && gsym->is_weak_undefined())
+     && ((r_type == elfcpp::R_MIPS16_26 && !target_is_16_bit_code)
+         || (r_type == elfcpp::R_MICROMIPS_26_S1 && !target_is_micromips_code)
+         || ((r_type == elfcpp::R_MIPS_26 || r_type == elfcpp::R_MIPS_JALR)
+             && (target_is_16_bit_code || target_is_micromips_code))));
+
+  bool local = (mips_sym == NULL
+                || (mips_sym->got_only_for_calls()
+                    ? symbol_calls_local(mips_sym, mips_sym->has_dynsym_index())
+                    : symbol_references_local(mips_sym,
+                                              mips_sym->has_dynsym_index())));
+
+  // Global R_MIPS_GOT_PAGE/R_MICROMIPS_GOT_PAGE relocations are equivalent
+  // to R_MIPS_GOT_DISP/R_MICROMIPS_GOT_DISP.  The addend is applied by the
+  // corresponding R_MIPS_GOT_OFST/R_MICROMIPS_GOT_OFST.
+  if (got_page_reloc(r_type) && !local)
+    r_type = (micromips_reloc(r_type) ? elfcpp::R_MICROMIPS_GOT_DISP
+                                      : elfcpp::R_MIPS_GOT_DISP);
+
+  bool update_got_entry = false;
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_NONE:
+      break;
+    case elfcpp::R_MIPS_16:
+      reloc_status = Reloc_funcs::rel16(view, object, psymval, r_addend,
+                                        rel_type);
+      break;
+
+    case elfcpp::R_MIPS_32:
+      if (should_apply_r_mips_32_reloc(mips_sym, r_type, output_section,
+                                       target))
+        reloc_status = Reloc_funcs::rel32(view, object, psymval, r_addend,
+                                          rel_type);
+      if (mips_sym != NULL
+          && (mips_sym->is_mips16() || mips_sym->is_micromips())
+          && mips_sym->global_got_area() == GGA_RELOC_ONLY)
+        {
+          // If target->get_mips16_fn_stub(mips_sym) is NULL, symbol value is
+          // already updated by adding +1.
+          if (target->get_mips16_fn_stub(mips_sym) != NULL)
+            {
+              gold_assert(mips_sym->need_fn_stub());
+              Mips16_stub_section *fn_stub_section =
+                target->get_mips16_fn_stub(mips_sym);
+              elfcpp::Elf_Xword value =
+                (fn_stub_section->output_section()->address()
+                + fn_stub_section->output_section_offset());
+              symval.set_output_value(value);
+              psymval = &symval;
+            }
+          got_offset = mips_sym->global_gotoffset();
+          update_got_entry = true;
+        }
+      break;
+
+    case elfcpp::R_MIPS_REL32:
+      gold_unreachable();
+
+    case elfcpp::R_MIPS_PC32:
+      reloc_status = Reloc_funcs::relpc32(view, object, psymval, address,
+                                          r_addend, rel_type);
+      break;
+
+    case elfcpp::R_MIPS16_26:
+      // The calculation for R_MIPS16_26 is just the same as for an
+      // R_MIPS_26.  It's only the storage of the relocated field into
+      // the output file that's different.  So, we just fall through to the
+      // R_MIPS_26 case here.
+    case elfcpp::R_MIPS_26:
+    case elfcpp::R_MICROMIPS_26_S1:
+      reloc_status = Reloc_funcs::rel26(view, object, psymval, address,
+          gsym == NULL, r_addend, rel_type, gsym, cross_mode_jump, r_type,
+          target->jal_to_bal());
+      break;
+
+    case elfcpp::R_MIPS_HI16:
+    case elfcpp::R_MIPS16_HI16:
+    case elfcpp::R_MICROMIPS_HI16:
+      reloc_status = Reloc_funcs::relhi16(view, object, psymval, r_addend,
+                                          address, gp_disp, r_type, rel_type);
+      break;
+
+    case elfcpp::R_MIPS_LO16:
+    case elfcpp::R_MIPS16_LO16:
+    case elfcpp::R_MICROMIPS_LO16:
+    case elfcpp::R_MICROMIPS_HI0_LO16:
+      reloc_status = Reloc_funcs::rello16(target, view, object, psymval,
+                                          r_addend, rel_type, address,
+                                          gp_disp, r_type);
+      break;
+
+    case elfcpp::R_MIPS_LITERAL:
+    case elfcpp::R_MICROMIPS_LITERAL:
+      // Because we don't merge literal sections, we can handle this
+      // just like R_MIPS_GPREL16.  In the long run, we should merge
+      // shared literals, and then we will need to additional work
+      // here.
+
+      // Fall through.
+
+    case elfcpp::R_MIPS_GPREL16:
+    case elfcpp::R_MIPS16_GPREL:
+    case elfcpp::R_MICROMIPS_GPREL7_S2:
+    case elfcpp::R_MICROMIPS_GPREL16:
+
+      reloc_status = Reloc_funcs::relgprel(view, object, psymval,
+                                           target->adjusted_gp_value(object),
+                                           r_addend, rel_type, gsym == NULL,
+                                           r_type);
+      break;
+
+    case elfcpp::R_MIPS_PC16:
+      reloc_status = Reloc_funcs::relpc16(view, object, psymval, address,
+                                          r_addend, rel_type);
+      break;
+    case elfcpp::R_MIPS_GPREL32:
+      reloc_status = Reloc_funcs::relgprel32(view, object, psymval,
+                                             target->adjusted_gp_value(object),
+                                             r_addend, rel_type);
+      break;
+    case elfcpp::R_MIPS_GOT_HI16:
+    case elfcpp::R_MIPS_CALL_HI16:
+    case elfcpp::R_MICROMIPS_GOT_HI16:
+    case elfcpp::R_MICROMIPS_CALL_HI16:
+      if (gsym != NULL)
+        got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
+                                                       object);
+      else
+        got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD,
+                                                       object);
+      reloc_status = Reloc_funcs::relgot_hi16(target, view, object, got_offset);
+      break;
+
+    case elfcpp::R_MIPS_GOT_LO16:
+    case elfcpp::R_MIPS_CALL_LO16:
+    case elfcpp::R_MICROMIPS_GOT_LO16:
+    case elfcpp::R_MICROMIPS_CALL_LO16:
+      if (gsym != NULL)
+        got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
+                                                       object);
+      else
+        got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_STANDARD,
+                                                       object);
+      reloc_status = Reloc_funcs::relgot_lo16(target, view, object, got_offset);
+      break;
+
+    case elfcpp::R_MIPS_GOT_DISP:
+    case elfcpp::R_MICROMIPS_GOT_DISP:
+      gold_assert(gsym != NULL);
+      got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
+                                                     object);
+      reloc_status = Reloc_funcs::relgot(target, view, object, got_offset,
+                                         r_type);
+      break;
+
+    case elfcpp::R_MIPS_CALL16:
+    case elfcpp::R_MIPS16_CALL16:
+    case elfcpp::R_MICROMIPS_CALL16:
+      gold_assert(gsym != NULL);
+      got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_STANDARD,
+                                                     object);
+      reloc_status = Reloc_funcs::relgot(target, view, object, got_offset,
+                                         r_type);
+      // TODO(sasa): We should also initialize update_got_entry in other places
+      // where relgot is called.
+      update_got_entry = changed_mips16_value;
+      break;
+
+    case elfcpp::R_MIPS_GOT16:
+    case elfcpp::R_MIPS16_GOT16:
+    case elfcpp::R_MICROMIPS_GOT16:
+      if (gsym != NULL)
+        {
+          got_offset = target->got_section()->got_offset(gsym,
+                                                         GOT_TYPE_STANDARD,
+                                                         object);
+          reloc_status = Reloc_funcs::relgot(target, view, object, got_offset,
+                                             r_type);
+        }
+      else
+        reloc_status = Reloc_funcs::relgot16_local(view, object, psymval,
+                                                   r_addend, rel_type, r_type);
+      break;
+
+    case elfcpp::R_MIPS_TLS_GD:
+    case elfcpp::R_MIPS16_TLS_GD:
+    case elfcpp::R_MICROMIPS_TLS_GD:
+      if (gsym != NULL)
+        got_offset = target->got_section()->got_offset(gsym, GOT_TYPE_TLS_PAIR,
+                                                       object);
+      else
+        got_offset = target->got_section()->got_offset(r_sym, GOT_TYPE_TLS_PAIR,
+                                                       object);
+      reloc_status = Reloc_funcs::relgot(target, view, object, got_offset,
+                                         r_type);
+      break;
+
+    case elfcpp::R_MIPS_TLS_GOTTPREL:
+    case elfcpp::R_MIPS16_TLS_GOTTPREL:
+    case elfcpp::R_MICROMIPS_TLS_GOTTPREL:
+      if (gsym != NULL)
+        got_offset = target->got_section()->got_offset(gsym,
+                                                       GOT_TYPE_TLS_OFFSET,
+                                                       object);
+      else
+        got_offset = target->got_section()->got_offset(r_sym,
+                                                       GOT_TYPE_TLS_OFFSET,
+                                                       object);
+      reloc_status = Reloc_funcs::relgot(target, view, object, got_offset,
+                                         r_type);
+      break;
+
+    case elfcpp::R_MIPS_TLS_LDM:
+    case elfcpp::R_MIPS16_TLS_LDM:
+    case elfcpp::R_MICROMIPS_TLS_LDM:
+      // Relocate the field with the offset of the GOT entry for
+      // the module index.
+      got_offset = target->got_section()->tls_ldm_offset(object);
+      reloc_status = Reloc_funcs::relgot(target, view, object, got_offset,
+                                         r_type);
+      break;
+
+    case elfcpp::R_MIPS_GOT_PAGE:
+    case elfcpp::R_MICROMIPS_GOT_PAGE:
+      reloc_status = Reloc_funcs::relgotpage(target, view, object, psymval,
+                                             r_addend, rel_type);
+      break;
+
+    case elfcpp::R_MIPS_GOT_OFST:
+    case elfcpp::R_MICROMIPS_GOT_OFST:
+      reloc_status = Reloc_funcs::relgotofst(target, view, object, psymval,
+                                             r_addend, rel_type, local);
+      break;
+
+    case elfcpp::R_MIPS_JALR:
+    case elfcpp::R_MICROMIPS_JALR:
+      // This relocation is only a hint.  In some cases, we optimize
+      // it into a bal instruction.  But we don't try to optimize
+      // when the symbol does not resolve locally.
+      if (gsym == NULL || symbol_calls_local(gsym, gsym->has_dynsym_index()))
+        reloc_status = Reloc_funcs::reljalr(view, object, psymval, address,
+                                            cross_mode_jump, r_type,
+                                            target->jalr_to_bal(),
+                                            target->jr_to_b());
+      break;
+
+    case elfcpp::R_MIPS_TLS_DTPREL_HI16:
+    case elfcpp::R_MIPS16_TLS_DTPREL_HI16:
+    case elfcpp::R_MICROMIPS_TLS_DTPREL_HI16:
+      reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
+                                             elfcpp::DTP_OFFSET, r_addend,
+                                             rel_type);
+      break;
+    case elfcpp::R_MIPS_TLS_DTPREL_LO16:
+    case elfcpp::R_MIPS16_TLS_DTPREL_LO16:
+    case elfcpp::R_MICROMIPS_TLS_DTPREL_LO16:
+    case elfcpp::R_MIPS_TLS_DTPREL32:
+    case elfcpp::R_MIPS_TLS_DTPREL64:
+      reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
+                                             elfcpp::DTP_OFFSET, r_addend,
+                                             rel_type);
+      break;
+    case elfcpp::R_MIPS_TLS_TPREL_HI16:
+    case elfcpp::R_MIPS16_TLS_TPREL_HI16:
+    case elfcpp::R_MICROMIPS_TLS_TPREL_HI16:
+      reloc_status = Reloc_funcs::tlsrelhi16(view, object, psymval,
+                                             elfcpp::TP_OFFSET, r_addend,
+                                             rel_type);
+      break;
+    case elfcpp::R_MIPS_TLS_TPREL_LO16:
+    case elfcpp::R_MIPS16_TLS_TPREL_LO16:
+    case elfcpp::R_MICROMIPS_TLS_TPREL_LO16:
+    case elfcpp::R_MIPS_TLS_TPREL32:
+    case elfcpp::R_MIPS_TLS_TPREL64:
+      reloc_status = Reloc_funcs::tlsrello16(view, object, psymval,
+                                             elfcpp::TP_OFFSET, r_addend,
+                                             rel_type);
+      break;
+    default:
+      gold_error_at_location(relinfo, relnum, r_offset,
+                             _("unsupported reloc %u"), r_type);
+      break;
+    }
+
+  if (update_got_entry)
+    elfcpp::Swap<32, big_endian>::writeval(
+      target->got_section()->got_view() + got_offset,
+      psymval->value(object, 0));
+
+  // Report any errors.
+  switch (reloc_status)
+    {
+    case Reloc_funcs::STATUS_OKAY:
+      break;
+    case Reloc_funcs::STATUS_OVERFLOW:
+      gold_error_at_location(relinfo, relnum, r_offset,
+                             _("relocation overflow"));
+      break;
+    case Reloc_funcs::STATUS_BAD_RELOC:
+      gold_error_at_location(relinfo, relnum, r_offset,
+        _("unexpected opcode while processing relocation"));
+      break;
+    default:
+      gold_unreachable();
+    }
+
+  return true;
+}
+
+template<int size, bool big_endian>
+inline bool
+Target_mips<size, big_endian>::Relocate::relocate(
+                        const Relocate_info<size, big_endian>* relinfo,
+                        Target_mips* target,
+                        Output_section* output_section,
+                        size_t relnum,
+                        const elfcpp::Rela<size, big_endian>& reloc,
+                        unsigned int r_type,
+                        const Sized_symbol<size>* gsym,
+                        const Symbol_value<size>* psymval,
+                        unsigned char* view,
+                        typename elfcpp::Elf_types<size>::Elf_Addr address,
+                        section_size_type view_size)
+{
+  return relocate(
+    relinfo,
+    target,
+    output_section,
+    relnum,
+    &reloc,
+    (const elfcpp::Rel<size, big_endian>*) NULL,
+    elfcpp::SHT_RELA,
+    r_type,
+    gsym,
+    psymval,
+    view,
+    address,
+    view_size);
+}
+
+template<int size, bool big_endian>
+inline bool
+Target_mips<size, big_endian>::Relocate::relocate(
+                        const Relocate_info<size, big_endian>* relinfo,
+                        Target_mips* target,
+                        Output_section* output_section,
+                        size_t relnum,
+                        const elfcpp::Rel<size, big_endian>& reloc,
+                        unsigned int r_type,
+                        const Sized_symbol<size>* gsym,
+                        const Symbol_value<size>* psymval,
+                        unsigned char* view,
+                        typename elfcpp::Elf_types<size>::Elf_Addr address,
+                        section_size_type view_size)
+{
+  return relocate(
+    relinfo,
+    target,
+    output_section,
+    relnum,
+    (const elfcpp::Rela<size, big_endian>*) NULL,
+    &reloc,
+    elfcpp::SHT_REL,
+    r_type,
+    gsym,
+    psymval,
+    view,
+    address,
+    view_size);
+}
+
+// Get the Reference_flags for a particular relocation.
+
+template<int size, bool big_endian>
+int
+Target_mips<size, big_endian>::Scan::get_reference_flags(
+                       unsigned int r_type)
+{
+  switch (r_type)
+    {
+    case elfcpp::R_MIPS_NONE:
+      // No symbol reference.
+      return 0;
+
+    case elfcpp::R_MIPS_16:
+    case elfcpp::R_MIPS_32:
+    case elfcpp::R_MIPS_64:
+    case elfcpp::R_MIPS_HI16:
+    case elfcpp::R_MIPS_LO16:
+    case elfcpp::R_MIPS16_HI16:
+    case elfcpp::R_MIPS16_LO16:
+      return Symbol::ABSOLUTE_REF;
+
+    case elfcpp::R_MIPS_26:
+    case elfcpp::R_MIPS16_26:
+      return Symbol::FUNCTION_CALL | Symbol::ABSOLUTE_REF;
+
+    case elfcpp::R_MIPS_GPREL32:
+    case elfcpp::R_MIPS_GPREL16:
+    case elfcpp::R_MIPS16_GPREL:
+    case elfcpp::R_MIPS_REL32:
+      return Symbol::RELATIVE_REF;
+
+    case elfcpp::R_MIPS_PC16:
+    case elfcpp::R_MIPS_PC32:
+    case elfcpp::R_MIPS_JALR:
+      return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
+
+    case elfcpp::R_MIPS_GOT16:
+    case elfcpp::R_MIPS16_GOT16:
+    case elfcpp::R_MIPS_CALL16:
+    case elfcpp::R_MIPS16_CALL16:
+    case elfcpp::R_MIPS_GOT_DISP:
+    case elfcpp::R_MIPS_GOT_HI16:
+    case elfcpp::R_MIPS_CALL_HI16:
+    case elfcpp::R_MIPS_GOT_LO16:
+    case elfcpp::R_MIPS_CALL_LO16:
+    case elfcpp::R_MIPS_LITERAL:
+    case elfcpp::R_MIPS_GOT_PAGE:
+    case elfcpp::R_MIPS_GOT_OFST:
+      // Absolute in GOT.
+      return Symbol::RELATIVE_REF;
+
+    case elfcpp::R_MIPS_TLS_DTPMOD32:
+    case elfcpp::R_MIPS_TLS_DTPREL32:
+    case elfcpp::R_MIPS_TLS_DTPMOD64:
+    case elfcpp::R_MIPS_TLS_DTPREL64:
+    case elfcpp::R_MIPS_TLS_GD:
+    case elfcpp::R_MIPS_TLS_LDM:
+    case elfcpp::R_MIPS_TLS_DTPREL_HI16:
+    case elfcpp::R_MIPS_TLS_DTPREL_LO16:
+    case elfcpp::R_MIPS_TLS_GOTTPREL:
+    case elfcpp::R_MIPS_TLS_TPREL32:
+    case elfcpp::R_MIPS_TLS_TPREL64:
+    case elfcpp::R_MIPS_TLS_TPREL_HI16:
+    case elfcpp::R_MIPS_TLS_TPREL_LO16:
+      return Symbol::TLS_REF;
+
+    case elfcpp::R_MIPS_COPY:
+    case elfcpp::R_MIPS_JUMP_SLOT:
+    default:
+      // Not expected.  We will give an error later.
+      return 0;
+    }
+}
+
+// Report an unsupported relocation against a local symbol.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::Scan::unsupported_reloc_local(
+                        Sized_relobj_file<size, big_endian>* object,
+                        unsigned int r_type)
+{
+  gold_error(_("%s: unsupported reloc %u against local symbol"),
+             object->name().c_str(), r_type);
+}
+
+// Report an unsupported relocation against a global symbol.
+
+template<int size, bool big_endian>
+void
+Target_mips<size, big_endian>::Scan::unsupported_reloc_global(
+                        Sized_relobj_file<size, big_endian>* object,
+                        unsigned int r_type,
+                        Symbol* gsym)
+{
+  gold_error(_("%s: unsupported reloc %u against global symbol %s"),
+             object->name().c_str(), r_type, gsym->demangled_name().c_str());
+}
+
+// Return printable name for ABI.
+template<int size, bool big_endian>
+const char *
+Target_mips<size, big_endian>::elf_mips_abi_name(elfcpp::Elf_Word e_flags,
+                                                 unsigned char ei_class)
+{
+  switch (e_flags & elfcpp::EF_MIPS_ABI)
+    {
+    case 0:
+      if ((e_flags & elfcpp::EF_MIPS_ABI2) != 0)
+        return "N32";
+      else if (elfcpp::abi_64(ei_class))
+        return "64";
+      else
+        return "none";
+    case elfcpp::E_MIPS_ABI_O32:
+      return "O32";
+    case elfcpp::E_MIPS_ABI_O64:
+      return "O64";
+    case elfcpp::E_MIPS_ABI_EABI32:
+      return "EABI32";
+    case elfcpp::E_MIPS_ABI_EABI64:
+      return "EABI64";
+    default:
+      return "unknown abi";
+    }
+}
+
+template<int size, bool big_endian>
+const char *
+Target_mips<size, big_endian>::elf_mips_mach_name(elfcpp::Elf_Word e_flags)
+{
+  switch (e_flags & elfcpp::EF_MIPS_MACH)
+    {
+    case elfcpp::E_MIPS_MACH_3900:
+      return "mips:3900";
+    case elfcpp::E_MIPS_MACH_4010:
+      return "mips:4010";
+    case elfcpp::E_MIPS_MACH_4100:
+      return "mips:4100";
+    case elfcpp::E_MIPS_MACH_4111:
+      return "mips:4111";
+    case elfcpp::E_MIPS_MACH_4120:
+      return "mips:4120";
+    case elfcpp::E_MIPS_MACH_4650:
+      return "mips:4650";
+    case elfcpp::E_MIPS_MACH_5400:
+      return "mips:5400";
+    case elfcpp::E_MIPS_MACH_5500:
+      return "mips:5500";
+    case elfcpp::E_MIPS_MACH_SB1:
+      return "mips:sb1";
+    case elfcpp::E_MIPS_MACH_9000:
+      return "mips:9000";
+    case elfcpp::E_MIPS_MACH_LS2E:
+      return "mips:loongson-2e";
+    case elfcpp::E_MIPS_MACH_LS2F:
+      return "mips:loongson-2f";
+    case elfcpp::E_MIPS_MACH_LS3A:
+      return "mips:loongson-3a";
+    case elfcpp::E_MIPS_MACH_OCTEON:
+      return "mips:octeon";
+    case elfcpp::E_MIPS_MACH_OCTEON2:
+      return "mips:octeon2";
+    case elfcpp::E_MIPS_MACH_XLR:
+      return "mips:xlr";
+    default:
+      switch (e_flags & elfcpp::EF_MIPS_ARCH)
+        {
+        default:
+        case elfcpp::E_MIPS_ARCH_1:
+          return "mips:3000";
+
+        case elfcpp::E_MIPS_ARCH_2:
+          return "mips:6000";
+
+        case elfcpp::E_MIPS_ARCH_3:
+          return "mips:4000";
+
+        case elfcpp::E_MIPS_ARCH_4:
+          return "mips:8000";
+
+        case elfcpp::E_MIPS_ARCH_5:
+          return "mips:mips5";
+
+        case elfcpp::E_MIPS_ARCH_32:
+          return "mips:isa32";
+
+        case elfcpp::E_MIPS_ARCH_64:
+          return "mips:isa64";
+
+        case elfcpp::E_MIPS_ARCH_32R2:
+          return "mips:isa32r2";
+
+        case elfcpp::E_MIPS_ARCH_64R2:
+          return "mips:isa64r2";
+        }
+    }
+    return "unknown CPU";
+}
+
+template<int size, bool big_endian>
+Target::Target_info Target_mips<size, big_endian>::mips_info =
+{
+  size,                 // size
+  big_endian,           // is_big_endian
+  elfcpp::EM_MIPS,      // machine_code
+  true,                 // has_make_symbol
+  false,                // has_resolve
+  false,                // has_code_fill
+  true,                 // is_default_stack_executable
+  false,                // can_icf_inline_merge_sections
+  '\0',                 // wrap_char
+  "/lib/ld.so.1",       // dynamic_linker
+  0x400000,             // default_text_segment_address
+  64 * 1024,            // abi_pagesize (overridable by -z max-page-size)
+  4 * 1024,             // common_pagesize (overridable by -z common-page-size)
+  false,                // isolate_execinstr
+  0,                    // rosegment_gap
+  elfcpp::SHN_UNDEF,    // small_common_shndx
+  elfcpp::SHN_UNDEF,    // large_common_shndx
+  0,                    // small_common_section_flags
+  0,                    // large_common_section_flags
+  NULL,                 // attributes_section
+  NULL                  // attributes_vendor
+};
+
+// The selector for mips object files.
+
+template<int size, bool big_endian>
+class Target_selector_mips : public Target_selector
+{
+public:
+  Target_selector_mips()
+    : Target_selector(elfcpp::EM_MIPS, size, big_endian,
+                (size == 64 ?
+                  (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
+                  (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")),
+                (size == 64 ?
+                  (big_endian ? "elf64-tradbigmips" : "elf64-tradlittlemips") :
+                  (big_endian ? "elf32-tradbigmips" : "elf32-tradlittlemips")))
+  { }
+
+  Target* do_instantiate_target()
+  { return new Target_mips<size, big_endian>(); }
+};
+
+Target_selector_mips<32, true> target_selector_mips32be;
+Target_selector_mips<32, false> target_selector_mips32;
+Target_selector_mips<64, true> target_selector_mips64be;
+Target_selector_mips<64, false> target_selector_mips64;
+
+
+} // End anonymous namespace.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]