Refactor gold to enable support for MIPS-64 relocation format.
[deliverable/binutils-gdb.git] / gold / powerpc.cc
index e99137169959e1763ee944c78f242bdd13b32edf..e26a198671128cd54d66eee446aa0bc4d239805f 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>
 
@@ -631,6 +631,7 @@ class Target_powerpc : public Sized_target<size, big_endian>
   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;
 
@@ -662,6 +663,21 @@ class Target_powerpc : public Sized_target<size, big_endian>
                          const unsigned char* plocal_symbols,
                          Relocatable_relocs*);
 
+  // Scan the relocs for --emit-relocs.
+  void
+  emit_relocs_scan(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_syms,
+                  Relocatable_relocs* rr);
+
   // Emit relocations for a section.
   void
   relocate_relocs(const Relocate_info<size, big_endian>*,
@@ -671,7 +687,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,
@@ -1072,13 +1087,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);
   };
 
@@ -1108,19 +1120,6 @@ class Target_powerpc : public Sized_target<size, big_endian>
     }
   };
 
-  // A class which returns the size required for a relocation type,
-  // used while scanning relocs during a relocatable link.
-  class Relocatable_size_for_reloc
-  {
-   public:
-    unsigned int
-    get_size_for_reloc(unsigned int, Relobj*)
-    {
-      gold_unreachable();
-      return 0;
-    }
-  };
-
   // Optimize the TLS relocation type based on what we know about the
   // symbol.  IS_FINAL is true if the final address of this symbol is
   // known at link time.
@@ -1520,6 +1519,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
@@ -1680,6 +1680,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)
@@ -1719,6 +1729,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.
@@ -2167,7 +2191,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().
@@ -3196,6 +3223,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;
@@ -3230,6 +3258,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;
@@ -3239,6 +3268,7 @@ 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            = 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;
@@ -5301,8 +5331,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:
@@ -5580,6 +5609,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:
@@ -5689,6 +5719,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:
@@ -5954,6 +5985,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:
@@ -6138,6 +6170,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:
@@ -6399,7 +6432,9 @@ Target_powerpc<size, big_endian>::gc_process_relocs(
     const unsigned char* plocal_symbols)
 {
   typedef Target_powerpc<size, big_endian> Powerpc;
-  typedef typename Target_powerpc<size, big_endian>::Scan Scan;
+  typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
+      Classify_reloc;
+
   Powerpc_relobj<size, big_endian>* ppc_object
     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
   if (size == 64)
@@ -6429,8 +6464,7 @@ Target_powerpc<size, big_endian>::gc_process_relocs(
       return;
     }
 
-  gold::gc_process_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan,
-                         typename Target_powerpc::Relocatable_size_for_reloc>(
+  gold::gc_process_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
     symtab,
     layout,
     this,
@@ -6560,6 +6594,8 @@ Target_powerpc<size, big_endian>::do_calls_non_split(
     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,
@@ -6570,7 +6606,8 @@ Target_powerpc<size, big_endian>::do_calls_non_split(
     {
       // warn
       Target::do_calls_non_split(object, shndx, fnoffset, fnsize,
-                                view, view_size, from, to);
+                                prelocs, reloc_count, view, view_size,
+                                from, to);
       return;
     }
 
@@ -6673,7 +6710,8 @@ Target_powerpc<size, big_endian>::scan_relocs(
     const unsigned char* plocal_symbols)
 {
   typedef Target_powerpc<size, big_endian> Powerpc;
-  typedef typename Target_powerpc<size, big_endian>::Scan Scan;
+  typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
+      Classify_reloc;
 
   if (sh_type == elfcpp::SHT_REL)
     {
@@ -6682,7 +6720,7 @@ Target_powerpc<size, big_endian>::scan_relocs(
       return;
     }
 
-  gold::scan_relocs<size, big_endian, Powerpc, elfcpp::SHT_RELA, Scan>(
+  gold::scan_relocs<size, big_endian, Powerpc, Scan, Classify_reloc>(
     symtab,
     layout,
     this,
@@ -6964,11 +7002,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,
@@ -6978,6 +7016,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:
@@ -6995,6 +7035,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;
@@ -7178,7 +7223,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)
@@ -7195,7 +7240,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)
@@ -7208,7 +7253,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;
@@ -7233,7 +7278,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)
@@ -7246,7 +7291,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;
@@ -7302,7 +7347,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)
@@ -7315,7 +7360,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;
@@ -7347,7 +7392,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();
@@ -7367,7 +7412,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;
        }
     }
@@ -7385,7 +7430,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());
        }
     }
@@ -7441,6 +7486,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:
@@ -7577,7 +7623,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 */)
@@ -7602,7 +7648,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(),
@@ -7625,6 +7671,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;
        }
     }
 
@@ -7639,6 +7776,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;
@@ -7719,11 +7857,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;
@@ -7739,6 +7879,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)
@@ -7791,7 +7932,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:
@@ -7812,7 +7953,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:
@@ -7859,6 +8003,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
@@ -7913,7 +8061,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:
@@ -7978,6 +8126,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
@@ -8013,11 +8181,13 @@ Target_powerpc<size, big_endian>::relocate_section(
   typedef typename Target_powerpc<size, big_endian>::Relocate Powerpc_relocate;
   typedef typename Target_powerpc<size, big_endian>::Relocate_comdat_behavior
     Powerpc_comdat_behavior;
+  typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
+      Classify_reloc;
 
   gold_assert(sh_type == elfcpp::SHT_RELA);
 
-  gold::relocate_section<size, big_endian, Powerpc, elfcpp::SHT_RELA,
-                        Powerpc_relocate, Powerpc_comdat_behavior>(
+  gold::relocate_section<size, big_endian, Powerpc, Powerpc_relocate,
+                        Powerpc_comdat_behavior, Classify_reloc>(
     relinfo,
     this,
     prelocs,
@@ -8030,9 +8200,26 @@ Target_powerpc<size, big_endian>::relocate_section(
     reloc_symbol_changes);
 }
 
+template<int size, bool big_endian>
 class Powerpc_scan_relocatable_reloc
 {
 public:
+  typedef typename Reloc_types<elfcpp::SHT_RELA, size, big_endian>::Reloc
+      Reltype;
+  static const int reloc_size =
+      Reloc_types<elfcpp::SHT_RELA, size, big_endian>::reloc_size;
+  static const int sh_type = elfcpp::SHT_RELA;
+
+  // Return the symbol referred to by the relocation.
+  static inline unsigned int
+  get_r_sym(const Reltype* reloc)
+  { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); }
+
+  // Return the type of the relocation.
+  static inline unsigned int
+  get_r_type(const Reltype* reloc)
+  { return elfcpp::elf_r_type<size>(reloc->get_r_info()); }
+
   // Return the strategy to use for a local symbol which is not a
   // section symbol, given the relocation type.
   inline Relocatable_relocs::Reloc_strategy
@@ -8080,10 +8267,11 @@ Target_powerpc<size, big_endian>::scan_relocatable_relocs(
     const unsigned char* plocal_symbols,
     Relocatable_relocs* rr)
 {
+  typedef Powerpc_scan_relocatable_reloc<size, big_endian> Scan_strategy;
+
   gold_assert(sh_type == elfcpp::SHT_RELA);
 
-  gold::scan_relocatable_relocs<size, big_endian, elfcpp::SHT_RELA,
-                               Powerpc_scan_relocatable_reloc>(
+  gold::scan_relocatable_relocs<size, big_endian, Scan_strategy>(
     symtab,
     layout,
     object,
@@ -8097,6 +8285,45 @@ Target_powerpc<size, big_endian>::scan_relocatable_relocs(
     rr);
 }
 
+// Scan the relocs for --emit-relocs.
+
+template<int size, bool big_endian>
+void
+Target_powerpc<size, big_endian>::emit_relocs_scan(
+    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_syms,
+    Relocatable_relocs* rr)
+{
+  typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, big_endian>
+      Classify_reloc;
+  typedef gold::Default_emit_relocs_strategy<Classify_reloc>
+      Emit_relocs_strategy;
+
+  gold_assert(sh_type == elfcpp::SHT_RELA);
+
+  gold::scan_relocatable_relocs<size, big_endian, Emit_relocs_strategy>(
+    symtab,
+    layout,
+    object,
+    data_shndx,
+    prelocs,
+    reloc_count,
+    output_section,
+    needs_special_offset_handling,
+    local_symbol_count,
+    plocal_syms,
+    rr);
+}
+
 // Emit relocations for a section.
 // This is a modified version of the function by the same name in
 // target-reloc.h.  Using relocate_special_relocatable for
@@ -8112,7 +8339,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,
@@ -8127,6 +8353,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);
@@ -8143,7 +8371,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;
 
@@ -8252,8 +8480,21 @@ Target_powerpc<size, big_endian>::relocate_relocs(
        }
       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();
@@ -8281,7 +8522,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                  else
                    {
                      r_type = elfcpp::R_POWERPC_NONE;
-                     offset -= 2 * big_endian;
+                     offset -= d_offset;
                    }
                  break;
                default:
@@ -8311,7 +8552,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                  else
                    {
                      r_type = elfcpp::R_POWERPC_NONE;
-                     offset -= 2 * big_endian;
+                     offset -= d_offset;
                    }
                }
            }
@@ -8330,7 +8571,7 @@ Target_powerpc<size, big_endian>::relocate_relocs(
                  else
                    {
                      r_type = elfcpp::R_POWERPC_NONE;
-                     offset -= 2 * big_endian;
+                     offset -= d_offset;
                    }
                }
            }
@@ -8348,7 +8589,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:
@@ -8369,7 +8610,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;
                }
            }
@@ -8380,7 +8621,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;
                }
            }
        }
This page took 0.035787 seconds and 4 git commands to generate.