]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - gold/powerpc.cc
Copyright update for binutils
[thirdparty/binutils-gdb.git] / gold / powerpc.cc
index 3d753b54f4eb9b78f4a8ab2a38f2f7e760d5d9f4..b1c5aeaa5f53f48b522cc544b8e9821ebd4c3d7a 100644 (file)
@@ -1,6 +1,6 @@
 // powerpc.cc -- powerpc target support for gold.
 
-// Copyright (C) 2008-2015 Free Software Foundation, Inc.
+// Copyright (C) 2008-2016 Free Software Foundation, Inc.
 // Written by David S. Miller <davem@davemloft.net>
 //        and David Edelsohn <edelsohn@gnu.org>
 
@@ -62,6 +62,9 @@ class Output_data_glink;
 template<int size, bool big_endian>
 class Stub_table;
 
+template<int size, bool big_endian>
+class Output_data_save_res;
+
 template<int size, bool big_endian>
 class Target_powerpc;
 
@@ -212,7 +215,7 @@ public:
   // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
   // section at DST_OFF.
   void
-  add_reference(Object* src_obj,
+  add_reference(Relobj* src_obj,
                unsigned int src_indx,
                typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
   {
@@ -510,7 +513,7 @@ class Target_powerpc : public Sized_target<size, big_endian>
       tlsld_got_offset_(-1U),
       stub_tables_(), branch_lookup_table_(), branch_info_(),
       plt_thread_safe_(false), relax_failed_(false), relax_fail_count_(0),
-      stub_group_size_(0)
+      stub_group_size_(0), savres_section_(0)
   {
   }
 
@@ -624,6 +627,14 @@ class Target_powerpc : public Sized_target<size, big_endian>
   do_can_check_for_function_pointers() const
   { return true; }
 
+  // Adjust -fsplit-stack code which calls non-split-stack code.
+  void
+  do_calls_non_split(Relobj* object, unsigned int shndx,
+                    section_offset_type fnoffset, section_size_type fnsize,
+                    const unsigned char* prelocs, size_t reloc_count,
+                    unsigned char* view, section_size_type view_size,
+                    std::string* from, std::string* to) const;
+
   // Relocate a section.
   void
   relocate_section(const Relocate_info<size, big_endian>*,
@@ -661,7 +672,6 @@ class Target_powerpc : public Sized_target<size, big_endian>
                  Output_section* output_section,
                  typename elfcpp::Elf_types<size>::Elf_Off
                     offset_in_output_section,
-                 const Relocatable_relocs*,
                  unsigned char*,
                  Address view_address,
                  section_size_type,
@@ -768,6 +778,12 @@ class Target_powerpc : public Sized_target<size, big_endian>
     return 24;
   }
 
+  Output_data_save_res<size, big_endian>*
+  savres_section() const
+  {
+    return this->savres_section_;
+  }
+
   // Add any special sections for this symbol to the gc work list.
   // For powerpc64, this adds the code section of a function
   // descriptor.
@@ -780,9 +796,9 @@ class Target_powerpc : public Sized_target<size, big_endian>
   // section of a function descriptor.
   void
   do_gc_add_reference(Symbol_table* symtab,
-                     Object* src_obj,
+                     Relobj* src_obj,
                      unsigned int src_shndx,
-                     Object* dst_obj,
+                     Relobj* dst_obj,
                      unsigned int dst_shndx,
                      Address dst_off) const;
 
@@ -1056,13 +1072,10 @@ class Target_powerpc : public Sized_target<size, big_endian>
     // Do a relocation.  Return false if the caller should not issue
     // any warnings about this relocation.
     inline bool
-    relocate(const Relocate_info<size, big_endian>*, Target_powerpc*,
-            Output_section*, size_t relnum,
-            const elfcpp::Rela<size, big_endian>&,
-            unsigned int r_type, const Sized_symbol<size>*,
-            const Symbol_value<size>*,
-            unsigned char*,
-            typename elfcpp::Elf_types<size>::Elf_Addr,
+    relocate(const Relocate_info<size, big_endian>*, unsigned int,
+            Target_powerpc*, Output_section*, size_t, const unsigned char*,
+            const Sized_symbol<size>*, const Symbol_value<size>*,
+            unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
             section_size_type);
   };
 
@@ -1191,10 +1204,13 @@ class Target_powerpc : public Sized_target<size, big_endian>
             unsigned int shndx, Output_section* output_section,
             Symbol* sym, const elfcpp::Rela<size, big_endian>& reloc)
   {
+    unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
     this->copy_relocs_.copy_reloc(symtab, layout,
                                  symtab->get_sized_symbol<size>(sym),
                                  object, shndx, output_section,
-                                 reloc, this->rela_dyn_section(layout));
+                                 r_type, reloc.get_r_offset(),
+                                 reloc.get_r_addend(),
+                                 this->rela_dyn_section(layout));
   }
 
   // Look over all the input sections, deciding where to place stubs.
@@ -1308,6 +1324,8 @@ class Target_powerpc : public Sized_target<size, big_endian>
   bool relax_failed_;
   int relax_fail_count_;
   int32_t stub_group_size_;
+
+  Output_data_save_res<size, big_endian> *savres_section_;
 };
 
 template<>
@@ -1334,7 +1352,8 @@ Target::Target_info Target_powerpc<32, true>::powerpc_info =
   0,                   // large_common_section_flags
   NULL,                        // attributes_section
   NULL,                        // attributes_vendor
-  "_start"             // entry_symbol_name
+  "_start",            // entry_symbol_name
+  32,                  // hash_entry_size
 };
 
 template<>
@@ -1361,7 +1380,8 @@ Target::Target_info Target_powerpc<32, false>::powerpc_info =
   0,                   // large_common_section_flags
   NULL,                        // attributes_section
   NULL,                        // attributes_vendor
-  "_start"             // entry_symbol_name
+  "_start",            // entry_symbol_name
+  32,                  // hash_entry_size
 };
 
 template<>
@@ -1388,7 +1408,8 @@ Target::Target_info Target_powerpc<64, true>::powerpc_info =
   0,                   // large_common_section_flags
   NULL,                        // attributes_section
   NULL,                        // attributes_vendor
-  "_start"             // entry_symbol_name
+  "_start",            // entry_symbol_name
+  32,                  // hash_entry_size
 };
 
 template<>
@@ -1415,7 +1436,8 @@ Target::Target_info Target_powerpc<64, false>::powerpc_info =
   0,                   // large_common_section_flags
   NULL,                        // attributes_section
   NULL,                        // attributes_vendor
-  "_start"             // entry_symbol_name
+  "_start",            // entry_symbol_name
+  32,                  // hash_entry_size
 };
 
 inline bool
@@ -1495,6 +1517,7 @@ public:
 private:
   typedef Powerpc_relocate_functions<size, big_endian> This;
   typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
+  typedef typename elfcpp::Elf_types<size>::Elf_Swxword SignedAddress;
 
   template<int valsize>
   static inline bool
@@ -1655,6 +1678,16 @@ public:
     return stat;
   }
 
+  // R_POWERPC_ADDR16_DQ: (Symbol + Addend) & 0xfff0
+  static inline Status
+  addr16_dq(unsigned char* view, Address value, Overflow_check overflow)
+  {
+    Status stat = This::template rela<16,16>(view, 0, 0xfff0, value, overflow);
+    if ((value & 15) != 0)
+      stat = STATUS_OVERFLOW;
+    return stat;
+  }
+
   // R_POWERPC_ADDR16_HI: ((Symbol + Addend) >> 16) & 0xffff
   static inline void
   addr16_hi(unsigned char* view, Address value)
@@ -1694,6 +1727,20 @@ public:
       stat = STATUS_OVERFLOW;
     return stat;
   }
+
+  // R_POWERPC_REL16DX_HA
+  static inline Status
+  addr16dx_ha(unsigned char *view, Address value, Overflow_check overflow)
+  {
+    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+    Valtype* wv = reinterpret_cast<Valtype*>(view);
+    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
+    value += 0x8000;
+    value = static_cast<SignedAddress>(value) >> 16;
+    val |= (value & 0xffc1) | ((value & 0x3e) << 15);
+    elfcpp::Swap<32, big_endian>::writeval(wv, val);
+    return overflowed<16>(value, overflow);
+  }
 };
 
 // Set ABI version for input and output.
@@ -2142,7 +2189,10 @@ public:
       symtab_(symtab), layout_(layout),
       header_ent_cnt_(size == 32 ? 3 : 1),
       header_index_(size == 32 ? 0x2000 : 0)
-  { }
+  {
+    if (size == 64)
+      this->set_addralign(256);
+  }
 
   // Override all the Output_data_got methods we use so as to first call
   // reserve_ent().
@@ -2733,8 +2783,13 @@ Target_powerpc<size, big_endian>::Branch_info::make_stub(
                           this->object_->section_name(this->shndx_).c_str());
              return true;
            }
+         bool save_res = (size == 64
+                          && gsym != NULL
+                          && gsym->source() == Symbol::IN_OUTPUT_DATA
+                          && gsym->output_data() == target->savres_section());
          return stub_table->add_long_branch_entry(this->object_,
-                                                  this->r_type_, from, to);
+                                                  this->r_type_,
+                                                  from, to, save_res);
        }
     }
   return true;
@@ -3166,6 +3221,7 @@ Output_data_plt_powerpc<size, big_endian>::add_local_ifunc_entry(
 
 static const uint32_t add_0_11_11      = 0x7c0b5a14;
 static const uint32_t add_2_2_11       = 0x7c425a14;
+static const uint32_t add_2_2_12       = 0x7c426214;
 static const uint32_t add_3_3_2                = 0x7c631214;
 static const uint32_t add_3_3_13       = 0x7c636a14;
 static const uint32_t add_11_0_11      = 0x7d605a14;
@@ -3175,12 +3231,15 @@ static const uint32_t addi_0_12         = 0x380c0000;
 static const uint32_t addi_2_2         = 0x38420000;
 static const uint32_t addi_3_3         = 0x38630000;
 static const uint32_t addi_11_11       = 0x396b0000;
+static const uint32_t addi_12_1                = 0x39810000;
 static const uint32_t addi_12_12       = 0x398c0000;
 static const uint32_t addis_0_2                = 0x3c020000;
 static const uint32_t addis_0_13       = 0x3c0d0000;
+static const uint32_t addis_2_12       = 0x3c4c0000;
 static const uint32_t addis_11_2       = 0x3d620000;
 static const uint32_t addis_11_11      = 0x3d6b0000;
 static const uint32_t addis_11_30      = 0x3d7e0000;
+static const uint32_t addis_12_1       = 0x3d810000;
 static const uint32_t addis_12_2       = 0x3d820000;
 static const uint32_t addis_12_12      = 0x3d8c0000;
 static const uint32_t b                        = 0x48000000;
@@ -3188,6 +3247,7 @@ static const uint32_t bcl_20_31           = 0x429f0005;
 static const uint32_t bctr             = 0x4e800420;
 static const uint32_t blr              = 0x4e800020;
 static const uint32_t bnectr_p4                = 0x4ce20420;
+static const uint32_t cmpld_7_12_0     = 0x7fac0040;
 static const uint32_t cmpldi_2_0       = 0x28220000;
 static const uint32_t cror_15_15_15    = 0x4def7b82;
 static const uint32_t cror_31_31_31    = 0x4ffffb82;
@@ -3196,6 +3256,7 @@ static const uint32_t ld_0_12             = 0xe80c0000;
 static const uint32_t ld_2_1           = 0xe8410000;
 static const uint32_t ld_2_2           = 0xe8420000;
 static const uint32_t ld_2_11          = 0xe84b0000;
+static const uint32_t ld_2_12          = 0xe84c0000;
 static const uint32_t ld_11_2          = 0xe9620000;
 static const uint32_t ld_11_11         = 0xe96b0000;
 static const uint32_t ld_12_2          = 0xe9820000;
@@ -3204,7 +3265,8 @@ static const uint32_t ld_12_12            = 0xe98c0000;
 static const uint32_t lfd_0_1          = 0xc8010000;
 static const uint32_t li_0_0           = 0x38000000;
 static const uint32_t li_12_0          = 0x39800000;
-static const uint32_t lis_0_0          = 0x3c000000;
+static const uint32_t lis_0            = 0x3c000000;
+static const uint32_t lis_2            = 0x3c400000;
 static const uint32_t lis_11           = 0x3d600000;
 static const uint32_t lis_12           = 0x3d800000;
 static const uint32_t lvx_0_12_0       = 0x7c0c00ce;
@@ -3570,7 +3632,8 @@ class Stub_table : public Output_relaxed_input_section
       targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
       orig_data_size_(owner->current_data_size()),
       plt_size_(0), last_plt_size_(0),
-      branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
+      branch_size_(0), last_branch_size_(0), eh_frame_added_(false),
+      need_save_res_(false)
   {
     this->set_output_section(output_section);
 
@@ -3617,7 +3680,7 @@ class Stub_table : public Output_relaxed_input_section
   // Add a long branch stub.
   bool
   add_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
-                       unsigned int, Address, Address);
+                       unsigned int, Address, Address, bool);
 
   Address
   find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
@@ -3641,6 +3704,7 @@ class Stub_table : public Output_relaxed_input_section
     this->plt_size_ = 0;
     this->long_branch_stubs_.clear();
     this->branch_size_ = 0;
+    this->need_save_res_ = false;
     if (all)
       {
        this->last_plt_size_ = 0;
@@ -3654,6 +3718,8 @@ class Stub_table : public Output_relaxed_input_section
     Address start_off = off;
     off += this->orig_data_size_;
     Address my_size = this->plt_size_ + this->branch_size_;
+    if (this->need_save_res_)
+      my_size += this->targ_->savres_section()->data_size();
     if (my_size != 0)
       off = align_address(off, this->stub_align());
     // Include original section size and alignment padding in size
@@ -3908,8 +3974,9 @@ class Stub_table : public Output_relaxed_input_section
   class Branch_stub_ent
   {
   public:
-    Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj, Address to)
-      : dest_(to), toc_base_off_(0)
+    Branch_stub_ent(const Powerpc_relobj<size, big_endian>* obj,
+                   Address to, bool save_res)
+      : dest_(to), toc_base_off_(0), save_res_(save_res)
     {
       if (size == 64)
        toc_base_off_ = obj->toc_base_offset();
@@ -3924,6 +3991,7 @@ class Stub_table : public Output_relaxed_input_section
 
     Address dest_;
     unsigned int toc_base_off_;
+    bool save_res_;
   };
 
   class Branch_stub_ent_hash
@@ -3947,6 +4015,9 @@ class Stub_table : public Output_relaxed_input_section
   section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
   // Whether .eh_frame info has been created for this stub section.
   bool eh_frame_added_;
+  // Set if this stub group needs a copy of out-of-line register
+  // save/restore functions.
+  bool need_save_res_;
 };
 
 // Add a plt call stub, if we do not already have one for this
@@ -4045,21 +4116,27 @@ Stub_table<size, big_endian>::add_long_branch_entry(
     const Powerpc_relobj<size, big_endian>* object,
     unsigned int r_type,
     Address from,
-    Address to)
+    Address to,
+    bool save_res)
 {
-  Branch_stub_ent ent(object, to);
+  Branch_stub_ent ent(object, to, save_res);
   Address off = this->branch_size_;
   if (this->long_branch_stubs_.insert(std::make_pair(ent, off)).second)
     {
-      unsigned int stub_size = this->branch_stub_size(to);
-      this->branch_size_ = off + stub_size;
-      if (size == 64 && stub_size != 4)
-       this->targ_->add_branch_lookup_table(to);
+      if (save_res)
+       this->need_save_res_ = true;
+      else
+       {
+         unsigned int stub_size = this->branch_stub_size(to);
+         this->branch_size_ = off + stub_size;
+         if (size == 64 && stub_size != 4)
+           this->targ_->add_branch_lookup_table(to);
+       }
     }
   return this->can_reach_stub(from, off, r_type);
 }
 
-// Find long branch stub.
+// Find long branch stub offset.
 
 template<int size, bool big_endian>
 typename Stub_table<size, big_endian>::Address
@@ -4067,10 +4144,14 @@ Stub_table<size, big_endian>::find_long_branch_entry(
     const Powerpc_relobj<size, big_endian>* object,
     Address to) const
 {
-  Branch_stub_ent ent(object, to);
+  Branch_stub_ent ent(object, to, false);
   typename Branch_stub_entries::const_iterator p
     = this->long_branch_stubs_.find(ent);
-  return p == this->long_branch_stubs_.end() ? invalid_address : p->second;
+  if (p == this->long_branch_stubs_.end())
+    return invalid_address;
+  if (p->first.save_res_)
+    return to - this->targ_->savres_section()->address() + this->branch_size_;
+  return p->second;
 }
 
 // A class to handle .glink.
@@ -4409,6 +4490,8 @@ Stub_table<size, big_endian>::do_write(Output_file* of)
           bs != this->long_branch_stubs_.end();
           ++bs)
        {
+         if (bs->first.save_res_)
+           continue;
          p = oview + this->plt_size_ + bs->second;
          Address loc = this->stub_address() + this->plt_size_ + bs->second;
          Address delta = bs->first.dest_ - loc;
@@ -4520,6 +4603,8 @@ Stub_table<size, big_endian>::do_write(Output_file* of)
           bs != this->long_branch_stubs_.end();
           ++bs)
        {
+         if (bs->first.save_res_)
+           continue;
          p = oview + this->plt_size_ + bs->second;
          Address loc = this->stub_address() + this->plt_size_ + bs->second;
          Address delta = bs->first.dest_ - loc;
@@ -4546,6 +4631,12 @@ Stub_table<size, big_endian>::do_write(Output_file* of)
            }
        }
     }
+  if (this->need_save_res_)
+    {
+      p = oview + this->plt_size_ + this->branch_size_;
+      memcpy (p, this->targ_->savres_section()->contents(),
+             this->targ_->savres_section()->data_size());
+    }
 }
 
 // Write out .glink.
@@ -4619,7 +4710,7 @@ Output_data_glink<size, big_endian>::do_write(Output_file* of)
                    }
                  else
                    {
-                     write_insn<big_endian>(p, lis_0_0 + hi(indx)),    p += 4;
+                     write_insn<big_endian>(p, lis_0 + hi(indx)),      p += 4;
                      write_insn<big_endian>(p, ori_0_0_0 + l(indx)),   p += 4;
                    }
                }
@@ -4756,6 +4847,12 @@ class Output_data_save_res : public Output_section_data_build
  public:
   Output_data_save_res(Symbol_table* symtab);
 
+  const unsigned char*
+  contents() const
+  {
+    return contents_;
+  }
+
  protected:
   // Write to a map file.
   void
@@ -5232,8 +5329,7 @@ Target_powerpc<size, big_endian>::Scan::get_reference_flags(
     case elfcpp::R_PPC64_TOC16_HA:
     case elfcpp::R_PPC64_TOC16_DS:
     case elfcpp::R_PPC64_TOC16_LO_DS:
-      // Absolute in GOT.
-      ref = Symbol::ABSOLUTE_REF;
+      ref = Symbol::RELATIVE_REF;
       break;
 
     case elfcpp::R_POWERPC_GOT_TPREL16:
@@ -5511,6 +5607,7 @@ Target_powerpc<size, big_endian>::Scan::local(
     case elfcpp::R_POWERPC_GNU_VTENTRY:
     case elfcpp::R_PPC64_TOCSAVE:
     case elfcpp::R_POWERPC_TLS:
+    case elfcpp::R_PPC64_ENTRY:
       break;
 
     case elfcpp::R_PPC64_TOC:
@@ -5620,6 +5717,7 @@ Target_powerpc<size, big_endian>::Scan::local(
     case elfcpp::R_POWERPC_REL16_LO:
     case elfcpp::R_POWERPC_REL16_HI:
     case elfcpp::R_POWERPC_REL16_HA:
+    case elfcpp::R_POWERPC_REL16DX_HA:
     case elfcpp::R_POWERPC_SECTOFF:
     case elfcpp::R_POWERPC_SECTOFF_LO:
     case elfcpp::R_POWERPC_SECTOFF_HI:
@@ -5885,6 +5983,7 @@ Target_powerpc<size, big_endian>::Scan::global(
     case elfcpp::R_POWERPC_GNU_VTENTRY:
     case elfcpp::R_PPC_LOCAL24PC:
     case elfcpp::R_POWERPC_TLS:
+    case elfcpp::R_PPC64_ENTRY:
       break;
 
     case elfcpp::R_PPC64_TOC:
@@ -6069,6 +6168,7 @@ Target_powerpc<size, big_endian>::Scan::global(
     case elfcpp::R_POWERPC_REL16_LO:
     case elfcpp::R_POWERPC_REL16_HI:
     case elfcpp::R_POWERPC_REL16_HA:
+    case elfcpp::R_POWERPC_REL16DX_HA:
     case elfcpp::R_POWERPC_SECTOFF:
     case elfcpp::R_POWERPC_SECTOFF_LO:
     case elfcpp::R_POWERPC_SECTOFF_HI:
@@ -6347,7 +6447,7 @@ Target_powerpc<size, big_endian>::gc_process_relocs(
          typename Powerpc_relobj<size, big_endian>::Section_refs::iterator s;
          for (s = p->second.begin(); s != p->second.end(); ++s)
            {
-             Object* src_obj = s->first;
+             Relobj* src_obj = s->first;
              unsigned int src_indx = s->second;
              symtab->gc()->add_reference(src_obj, src_indx,
                                          ppc_object, dst_indx);
@@ -6384,9 +6484,9 @@ template<int size, bool big_endian>
 void
 Target_powerpc<size, big_endian>::do_gc_add_reference(
     Symbol_table* symtab,
-    Object* src_obj,
+    Relobj* src_obj,
     unsigned int src_shndx,
-    Object* dst_obj,
+    Relobj* dst_obj,
     unsigned int dst_shndx,
     Address dst_off) const
 {
@@ -6479,6 +6579,116 @@ Target_powerpc<size, big_endian>::do_function_location(
     }
 }
 
+// FNOFFSET in section SHNDX in OBJECT is the start of a function
+// compiled with -fsplit-stack.  The function calls non-split-stack
+// code.  Change the function to ensure it has enough stack space to
+// call some random function.
+
+template<int size, bool big_endian>
+void
+Target_powerpc<size, big_endian>::do_calls_non_split(
+    Relobj* object,
+    unsigned int shndx,
+    section_offset_type fnoffset,
+    section_size_type fnsize,
+    const unsigned char* prelocs,
+    size_t reloc_count,
+    unsigned char* view,
+    section_size_type view_size,
+    std::string* from,
+    std::string* to) const
+{
+  // 32-bit not supported.
+  if (size == 32)
+    {
+      // warn
+      Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
+                                prelocs, reloc_count, view, view_size,
+                                from, to);
+      return;
+    }
+
+  // The function always starts with
+  //   ld %r0,-0x7000-64(%r13)  # tcbhead_t.__private_ss
+  //   addis %r12,%r1,-allocate@ha
+  //   addi %r12,%r12,-allocate@l
+  //   cmpld %r12,%r0
+  // but note that the addis or addi may be replaced with a nop
+
+  unsigned char *entry = view + fnoffset;
+  uint32_t insn = elfcpp::Swap<32, big_endian>::readval(entry);
+
+  if ((insn & 0xffff0000) == addis_2_12)
+    {
+      /* Skip ELFv2 global entry code.  */
+      entry += 8;
+      insn = elfcpp::Swap<32, big_endian>::readval(entry);
+    }
+
+  unsigned char *pinsn = entry;
+  bool ok = false;
+  const uint32_t ld_private_ss = 0xe80d8fc0;
+  if (insn == ld_private_ss)
+    {
+      int32_t allocate = 0;
+      while (1)
+       {
+         pinsn += 4;
+         insn = elfcpp::Swap<32, big_endian>::readval(pinsn);
+         if ((insn & 0xffff0000) == addis_12_1)
+           allocate += (insn & 0xffff) << 16;
+         else if ((insn & 0xffff0000) == addi_12_1
+                  || (insn & 0xffff0000) == addi_12_12)
+           allocate += ((insn & 0xffff) ^ 0x8000) - 0x8000;
+         else if (insn != nop)
+           break;
+       }
+      if (insn == cmpld_7_12_0 && pinsn == entry + 12)
+       {
+         int extra = parameters->options().split_stack_adjust_size();
+         allocate -= extra;
+         if (allocate >= 0 || extra < 0)
+           {
+             object->error(_("split-stack stack size overflow at "
+                             "section %u offset %0zx"),
+                           shndx, static_cast<size_t>(fnoffset));
+             return;
+           }
+         pinsn = entry + 4;
+         insn = addis_12_1 | (((allocate + 0x8000) >> 16) & 0xffff);
+         if (insn != addis_12_1)
+           {
+             elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
+             pinsn += 4;
+             insn = addi_12_12 | (allocate & 0xffff);
+             if (insn != addi_12_12)
+               {
+                 elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
+                 pinsn += 4;
+               }
+           }
+         else
+           {
+             insn = addi_12_1 | (allocate & 0xffff);
+             elfcpp::Swap<32, big_endian>::writeval(pinsn, insn);
+             pinsn += 4;
+           }
+         if (pinsn != entry + 12)
+           elfcpp::Swap<32, big_endian>::writeval(pinsn, nop);
+
+         ok = true;
+       }
+    }
+
+  if (!ok)
+    {
+      if (!object->has_no_split_stack())
+       object->error(_("failed to match split-stack sequence at "
+                       "section %u offset %0zx"),
+                     shndx, static_cast<size_t>(fnoffset));
+    }
+}
+
 // Scan relocations for a section.
 
 template<int size, bool big_endian>
@@ -6567,8 +6777,9 @@ Target_powerpc<size, big_endian>::define_save_restore_funcs(
 {
   if (size == 64)
     {
-      Output_data_save_res<64, big_endian>* savres
-       = new Output_data_save_res<64, big_endian>(symtab);
+      Output_data_save_res<size, big_endian>* savres
+       = new Output_data_save_res<size, big_endian>(symtab);
+      this->savres_section_ = savres;
       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
                                      elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
                                      savres, ORDER_TEXT, false);
@@ -6787,11 +6998,11 @@ template<int size, bool big_endian>
 inline bool
 Target_powerpc<size, big_endian>::Relocate::relocate(
     const Relocate_info<size, big_endian>* relinfo,
+    unsigned int,
     Target_powerpc* target,
     Output_section* os,
     size_t relnum,
-    const elfcpp::Rela<size, big_endian>& rela,
-    unsigned int r_type,
+    const unsigned char* preloc,
     const Sized_symbol<size>* gsym,
     const Symbol_value<size>* psymval,
     unsigned char* view,
@@ -6801,6 +7012,8 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
   if (view == NULL)
     return true;
 
+  const elfcpp::Rela<size, big_endian> rela(preloc);
+  unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
   switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
     {
     case Track_tls::NOT_EXPECTED:
@@ -6818,6 +7031,11 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
 
   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
+  typedef typename Reloc_types<elfcpp::SHT_RELA,
+                              size, big_endian>::Reloc Reltype;
+  // Offset from start of insn to d-field reloc.
+  const int d_offset = big_endian ? 2 : 0;
+
   Powerpc_relobj<size, big_endian>* const object
     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
   Address value = 0;
@@ -6835,8 +7053,8 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
          && !parameters->options().output_is_position_independent()
          && !is_branch_reloc(r_type))
        {
-         unsigned int off = target->glink_section()->find_global_entry(gsym);
-         if (off != (unsigned int)-1)
+         Address off = target->glink_section()->find_global_entry(gsym);
+         if (off != invalid_address)
            {
              value = target->glink_section()->global_entry_address() + off;
              has_stub_value = true;
@@ -6852,18 +7070,26 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
              if (target->stub_tables().size() != 0)
                stub_table = target->stub_tables()[0];
            }
-         gold_assert(stub_table != NULL);
-         Address off;
-         if (gsym != NULL)
-           off = stub_table->find_plt_call_entry(object, gsym, r_type,
-                                                 rela.get_r_addend());
-         else
-           off = stub_table->find_plt_call_entry(object, r_sym, r_type,
-                                                 rela.get_r_addend());
-         gold_assert(off != invalid_address);
-         value = stub_table->stub_address() + off;
-         has_stub_value = true;
+         if (stub_table != NULL)
+           {
+             Address off;
+             if (gsym != NULL)
+               off = stub_table->find_plt_call_entry(object, gsym, r_type,
+                                                     rela.get_r_addend());
+             else
+               off = stub_table->find_plt_call_entry(object, r_sym, r_type,
+                                                     rela.get_r_addend());
+             if (off != invalid_address)
+               {
+                 value = stub_table->stub_address() + off;
+                 has_stub_value = true;
+               }
+           }
        }
+      // We don't care too much about bogus debug references to
+      // non-local functions, but otherwise there had better be a plt
+      // call stub or global entry stub as appropriate.
+      gold_assert(has_stub_value || !(os->flags() & elfcpp::SHF_ALLOC));
     }
 
   if (r_type == elfcpp::R_POWERPC_GOT16
@@ -6993,7 +7219,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
          if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
              || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
              insn &= (1 << 26) - (1 << 16); // extract rt,ra from addi
              if (size == 32)
@@ -7010,7 +7236,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
          if (r_type == elfcpp::R_POWERPC_GOT_TLSGD16
              || r_type == elfcpp::R_POWERPC_GOT_TLSGD16_LO)
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
              insn &= (1 << 26) - (1 << 21); // extract rt
              if (size == 32)
@@ -7023,7 +7249,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
            }
          else
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = nop;
              elfcpp::Swap<32, big_endian>::writeval(iview, insn);
              r_type = elfcpp::R_POWERPC_NONE;
@@ -7048,7 +7274,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
          if (r_type == elfcpp::R_POWERPC_GOT_TLSLD16
              || r_type == elfcpp::R_POWERPC_GOT_TLSLD16_LO)
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
              insn &= (1 << 26) - (1 << 21); // extract rt
              if (size == 32)
@@ -7061,7 +7287,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
            }
          else
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = nop;
              elfcpp::Swap<32, big_endian>::writeval(iview, insn);
              r_type = elfcpp::R_POWERPC_NONE;
@@ -7117,7 +7343,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
          if (r_type == elfcpp::R_POWERPC_GOT_TPREL16
              || r_type == elfcpp::R_POWERPC_GOT_TPREL16_LO)
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
              insn &= (1 << 26) - (1 << 21); // extract rt from ld
              if (size == 32)
@@ -7130,7 +7356,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
            }
          else
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = nop;
              elfcpp::Swap<32, big_endian>::writeval(iview, insn);
              r_type = elfcpp::R_POWERPC_NONE;
@@ -7162,7 +7388,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
              Insn insn = addi_3_3;
              elfcpp::Swap<32, big_endian>::writeval(iview, insn);
              r_type = elfcpp::R_POWERPC_TPREL16_LO;
-             view += 2 * big_endian;
+             view += d_offset;
              value = psymval->value(object, rela.get_r_addend());
            }
          this->skip_next_tls_get_addr_call();
@@ -7182,7 +7408,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
          elfcpp::Swap<32, big_endian>::writeval(iview, insn);
          this->skip_next_tls_get_addr_call();
          r_type = elfcpp::R_POWERPC_TPREL16_LO;
-         view += 2 * big_endian;
+         view += d_offset;
          value = dtp_offset;
        }
     }
@@ -7200,7 +7426,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
          gold_assert(insn != 0);
          elfcpp::Swap<32, big_endian>::writeval(iview, insn);
          r_type = elfcpp::R_POWERPC_TPREL16_LO;
-         view += 2 * big_endian;
+         view += d_offset;
          value = psymval->value(object, rela.get_r_addend());
        }
     }
@@ -7256,6 +7482,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
     case elfcpp::R_POWERPC_REL16_LO:
     case elfcpp::R_POWERPC_REL16_HI:
     case elfcpp::R_POWERPC_REL16_HA:
+    case elfcpp::R_POWERPC_REL16DX_HA:
     case elfcpp::R_POWERPC_REL14:
     case elfcpp::R_POWERPC_REL14_BRTAKEN:
     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
@@ -7392,7 +7619,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
        case elfcpp::R_PPC64_TOC16_HA:
          if (parameters->options().toc_optimize())
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
              if ((insn & ((0x3f << 26) | 0x1f << 16))
                  != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
@@ -7417,7 +7644,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
        case elfcpp::R_PPC64_TOC16_LO_DS:
          if (parameters->options().toc_optimize())
            {
-             Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
+             Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
              Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
              if (!ok_lo_toc_insn(insn))
                gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
@@ -7440,6 +7667,97 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
                }
            }
          break;
+
+       case elfcpp::R_PPC64_ENTRY:
+         value = (target->got_section()->output_section()->address()
+                  + object->toc_base_offset());
+         if (value + 0x80008000 <= 0xffffffff
+             && !parameters->options().output_is_position_independent())
+           {
+             Insn* iview = reinterpret_cast<Insn*>(view);
+             Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
+             Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
+
+             if ((insn1 & ~0xfffc) == ld_2_12
+                 && insn2 == add_2_2_12)
+               {
+                 insn1 = lis_2 + ha(value);
+                 elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
+                 insn2 = addi_2_2 + l(value);
+                 elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
+                 return true;
+               }
+           }
+         else
+           {
+             value -= address;
+             if (value + 0x80008000 <= 0xffffffff)
+               {
+                 Insn* iview = reinterpret_cast<Insn*>(view);
+                 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview);
+                 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview + 1);
+
+                 if ((insn1 & ~0xfffc) == ld_2_12
+                     && insn2 == add_2_2_12)
+                   {
+                     insn1 = addis_2_12 + ha(value);
+                     elfcpp::Swap<32, big_endian>::writeval(iview, insn1);
+                     insn2 = addi_2_2 + l(value);
+                     elfcpp::Swap<32, big_endian>::writeval(iview + 1, insn2);
+                     return true;
+                   }
+               }
+           }
+         break;
+
+       case elfcpp::R_POWERPC_REL16_LO:
+         // If we are generating a non-PIC executable, edit
+         //    0:      addis 2,12,.TOC.-0b@ha
+         //            addi 2,2,.TOC.-0b@l
+         // used by ELFv2 global entry points to set up r2, to
+         //            lis 2,.TOC.@ha
+         //            addi 2,2,.TOC.@l
+         // if .TOC. is in range.  */
+         if (value + address - 4 + 0x80008000 <= 0xffffffff
+             && relnum != 0
+             && preloc != NULL
+             && target->abiversion() >= 2
+             && !parameters->options().output_is_position_independent()
+             && gsym != NULL
+             && strcmp(gsym->name(), ".TOC.") == 0)
+           {
+             const int reloc_size
+               = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
+             Reltype prev_rela(preloc - reloc_size);
+             if ((prev_rela.get_r_info()
+                  == elfcpp::elf_r_info<size>(r_sym,
+                                              elfcpp::R_POWERPC_REL16_HA))
+                 && prev_rela.get_r_offset() + 4 == rela.get_r_offset()
+                 && prev_rela.get_r_addend() + 4 == rela.get_r_addend())
+               {
+                 Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
+                 Insn insn1 = elfcpp::Swap<32, big_endian>::readval(iview - 1);
+                 Insn insn2 = elfcpp::Swap<32, big_endian>::readval(iview);
+
+                 if ((insn1 & 0xffff0000) == addis_2_12
+                     && (insn2 & 0xffff0000) == addi_2_2)
+                   {
+                     insn1 = lis_2 + ha(value + address - 4);
+                     elfcpp::Swap<32, big_endian>::writeval(iview - 1, insn1);
+                     insn2 = addi_2_2 + l(value + address - 4);
+                     elfcpp::Swap<32, big_endian>::writeval(iview, insn2);
+                     if (relinfo->rr)
+                       {
+                         relinfo->rr->set_strategy(relnum - 1,
+                                                   Relocatable_relocs::RELOC_SPECIAL);
+                         relinfo->rr->set_strategy(relnum,
+                                                   Relocatable_relocs::RELOC_SPECIAL);
+                       }
+                     return true;
+                   }
+               }
+           }
+         break;
        }
     }
 
@@ -7454,6 +7772,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
       break;
 
     case elfcpp::R_POWERPC_REL32:
+    case elfcpp::R_POWERPC_REL16DX_HA:
       if (size == 64)
        overflow = Reloc::CHECK_SIGNED;
       break;
@@ -7534,11 +7853,13 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
       break;
     }
 
+  Insn* iview = reinterpret_cast<Insn*>(view - d_offset);
+  Insn insn = 0;
+
   if (overflow == Reloc::CHECK_LOW_INSN
       || overflow == Reloc::CHECK_HIGH_INSN)
     {
-      Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
-      Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
+      insn = elfcpp::Swap<32, big_endian>::readval(iview);
 
       if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
        overflow = Reloc::CHECK_BITFIELD;
@@ -7554,6 +7875,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
        overflow = Reloc::CHECK_SIGNED;
     }
 
+  bool maybe_dq_reloc = false;
   typename Powerpc_relocate_functions<size, big_endian>::Status status
     = Powerpc_relocate_functions<size, big_endian>::STATUS_OK;
   switch (r_type)
@@ -7606,7 +7928,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
       if (size == 64)
        {
          // On ppc64 these are all ds form
-         status = Reloc::addr16_ds(view, value, overflow);
+         maybe_dq_reloc = true;
          break;
        }
     case elfcpp::R_POWERPC_ADDR16:
@@ -7627,7 +7949,10 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
     case elfcpp::R_POWERPC_DTPREL16_LO:
     case elfcpp::R_POWERPC_GOT_TLSGD16_LO:
     case elfcpp::R_POWERPC_GOT_TLSLD16_LO:
-      status = Reloc::addr16(view, value, overflow);
+      if (size == 64)
+       status = Reloc::addr16(view, value, overflow);
+      else
+       maybe_dq_reloc = true;
       break;
 
     case elfcpp::R_POWERPC_UADDR16:
@@ -7674,6 +7999,10 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
       Reloc::addr16_ha(view, value);
       break;
 
+    case elfcpp::R_POWERPC_REL16DX_HA:
+      status = Reloc::addr16dx_ha(view, value, overflow);
+      break;
+
     case elfcpp::R_PPC64_DTPREL16_HIGHER:
       if (size == 32)
        // R_PPC_EMB_NADDR16_LO
@@ -7728,7 +8057,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
     case elfcpp::R_PPC64_GOT16_LO_DS:
     case elfcpp::R_PPC64_SECTOFF_DS:
     case elfcpp::R_PPC64_SECTOFF_LO_DS:
-      status = Reloc::addr16_ds(view, value, overflow);
+      maybe_dq_reloc = true;
       break;
 
     case elfcpp::R_POWERPC_ADDR14:
@@ -7793,6 +8122,26 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
                             r_type);
       break;
     }
+
+  if (maybe_dq_reloc)
+    {
+      if (insn == 0)
+       insn = elfcpp::Swap<32, big_endian>::readval(iview);
+
+      if ((insn & (0x3f << 26)) == 56u << 26 /* lq */
+         || ((insn & (0x3f << 26)) == (61u << 26) /* lxv, stxv */
+             && (insn & 3) == 1))
+       status = Reloc::addr16_dq(view, value, overflow);
+      else if (size == 64
+              || (insn & (0x3f << 26)) == 58u << 26 /* ld,ldu,lwa */
+              || (insn & (0x3f << 26)) == 62u << 26 /* std,stdu,stq */
+              || (insn & (0x3f << 26)) == 57u << 26 /* lfdp */
+              || (insn & (0x3f << 26)) == 61u << 26 /* stfdp */)
+       status = Reloc::addr16_ds(view, value, overflow);
+      else
+       status = Reloc::addr16(view, value, overflow);
+    }
+
   if (status != Powerpc_relocate_functions<size, big_endian>::STATUS_OK
       && (has_stub_value
          || !(gsym != NULL
@@ -7927,7 +8276,6 @@ Target_powerpc<size, big_endian>::relocate_relocs(
     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*,
     Address view_address,
     section_size_type,
@@ -7942,6 +8290,8 @@ Target_powerpc<size, big_endian>::relocate_relocs(
     Reltype_write;
   const int reloc_size
     = Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
+  // Offset from start of insn to d-field reloc.
+  const int d_offset = big_endian ? 2 : 0;
 
   Powerpc_relobj<size, big_endian>* const object
     = static_cast<Powerpc_relobj<size, big_endian>*>(relinfo->object);
@@ -7958,7 +8308,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
   bool zap_next = false;
   for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
     {
-      Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i);
+      Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i);
       if (strategy == Relocatable_relocs::RELOC_DISCARD)
        continue;
 
@@ -7984,6 +8334,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
        }
 
       // Get the new symbol index.
+      Output_section* os = NULL;
       if (r_sym < local_count)
        {
          switch (strategy)
@@ -8008,7 +8359,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                unsigned int shndx =
                  object->local_symbol_input_shndx(r_sym, &is_ordinary);
                gold_assert(is_ordinary);
-               Output_section* os = object->output_section(shndx);
+               os = object->output_section(shndx);
                gold_assert(os != NULL);
                gold_assert(os->needs_symtab_index());
                r_sym = os->symtab_index();
@@ -8061,12 +8412,26 @@ Target_powerpc<size, big_endian>::relocate_relocs(
       else if (strategy == Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA)
        {
          const Symbol_value<size>* psymval = object->local_symbol(orig_r_sym);
-         addend = psymval->value(object, addend);
+         gold_assert(os != NULL);
+         addend = psymval->value(object, addend) - os->address();
        }
       else if (strategy == Relocatable_relocs::RELOC_SPECIAL)
        {
-         if (addend >= 32768)
-           addend += got2_addend;
+         if (size == 32)
+           {
+             if (addend >= 32768)
+               addend += got2_addend;
+           }
+         else if (r_type == elfcpp::R_POWERPC_REL16_HA)
+           {
+             r_type = elfcpp::R_POWERPC_ADDR16_HA;
+             addend -= d_offset;
+           }
+         else if (r_type == elfcpp::R_POWERPC_REL16_LO)
+           {
+             r_type = elfcpp::R_POWERPC_ADDR16_LO;
+             addend -= d_offset + 4;
+           }
        }
       else
        gold_unreachable();
@@ -8094,7 +8459,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                  else
                    {
                      r_type = elfcpp::R_POWERPC_NONE;
-                     offset -= 2 * big_endian;
+                     offset -= d_offset;
                    }
                  break;
                default:
@@ -8124,7 +8489,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                  else
                    {
                      r_type = elfcpp::R_POWERPC_NONE;
-                     offset -= 2 * big_endian;
+                     offset -= d_offset;
                    }
                }
            }
@@ -8143,7 +8508,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                  else
                    {
                      r_type = elfcpp::R_POWERPC_NONE;
-                     offset -= 2 * big_endian;
+                     offset -= d_offset;
                    }
                }
            }
@@ -8161,7 +8526,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                  break;
                case tls::TLSOPT_TO_LE:
                  r_type = elfcpp::R_POWERPC_TPREL16_LO;
-                 offset += 2 * big_endian;
+                 offset += d_offset;
                  zap_next = true;
                  break;
                default:
@@ -8182,7 +8547,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                  r_sym = os->symtab_index();
                  addend = dtp_offset;
                  r_type = elfcpp::R_POWERPC_TPREL16_LO;
-                 offset += 2 * big_endian;
+                 offset += d_offset;
                  zap_next = true;
                }
            }
@@ -8193,7 +8558,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
              if (this->optimize_tls_ie(final) == tls::TLSOPT_TO_LE)
                {
                  r_type = elfcpp::R_POWERPC_TPREL16_LO;
-                 offset += 2 * big_endian;
+                 offset += d_offset;
                }
            }
        }
@@ -8232,8 +8597,8 @@ Target_powerpc<size, big_endian>::do_dynsym_value(const Symbol* gsym) const
     }
   else if (this->abiversion() >= 2)
     {
-      unsigned int off = this->glink_section()->find_global_entry(gsym);
-      if (off != (unsigned int)-1)
+      Address off = this->glink_section()->find_global_entry(gsym);
+      if (off != invalid_address)
        return this->glink_section()->global_entry_address() + off;
     }
   gold_unreachable();
@@ -8282,8 +8647,8 @@ Target_powerpc<size, big_endian>::do_plt_address_for_global(
     }
   else if (this->abiversion() >= 2)
     {
-      unsigned int off = this->glink_section()->find_global_entry(gsym);
-      if (off != (unsigned int)-1)
+      Address off = this->glink_section()->find_global_entry(gsym);
+      if (off != invalid_address)
        return this->glink_section()->global_entry_address() + off;
     }
   gold_unreachable();