OSDN Git Service

2009-11-15 Doug Kwan <dougkwan@google.com>
[pf3gnuchains/pf3gnuchains3x.git] / gold / arm.cc
index 4e6c6a1..5b702f2 100644 (file)
@@ -972,8 +972,7 @@ class Arm_relobj : public Sized_relobj<32, big_endian>
                          Stringpool_template<char>*);
 
   void
-  do_relocate_sections(const General_options& options,
-                      const Symbol_table* symtab, const Layout* layout,
+  do_relocate_sections(const Symbol_table* symtab, const Layout* layout,
                       const unsigned char* pshdrs,
                       typename Sized_relobj<32, big_endian>::Views* pivews);
 
@@ -1188,6 +1187,14 @@ class Target_arm : public Sized_target<32, big_endian>
     return false;
   }
 
+  // Whether we have an NOP instruction.  If not, use mov r0, r0 instead.
+  bool
+  may_use_arm_nop() const
+  {
+    // FIXME:  This should not hard-coded.
+    return false;
+  }
+
   // Process the relocations to determine unreferenced sections for 
   // garbage collection.
   void
@@ -1315,13 +1322,20 @@ class Target_arm : public Sized_target<32, big_endian>
                         bool, const unsigned char*, Arm_address,
                         section_size_type);
 
+  // Relocate a stub. 
+  void
+  relocate_stub(Reloc_stub*, const Relocate_info<32, big_endian>*,
+               Output_section*, unsigned char*, Arm_address,
+               section_size_type);
   // Get the default ARM target.
-  static const Target_arm<big_endian>&
+  static Target_arm<big_endian>*
   default_target()
   {
     gold_assert(parameters->target().machine_code() == elfcpp::EM_ARM
                && parameters->target().is_big_endian() == big_endian);
-    return static_cast<const Target_arm<big_endian>&>(parameters->target());
+    return static_cast<Target_arm<big_endian>*>(
+            parameters->sized_target<32, big_endian>());
   }
 
   // Whether relocation type uses LSB to distinguish THUMB addresses.
@@ -1729,67 +1743,12 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     return val;
   }
 
-  // FIXME: This probably only works for Android on ARM v5te. We should
-  // following GNU ld for the general case.
-  template<unsigned r_type>
-  static inline typename This::Status
-  arm_branch_common(unsigned char *view,
-                   const Sized_relobj<32, big_endian>* object,
-                   const Symbol_value<32>* psymval,
-                   Arm_address address,
-                   Arm_address thumb_bit)
-  {
-    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
-    Valtype* wv = reinterpret_cast<Valtype*>(view);
-    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
-     
-    bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
-                     && ((val & 0x0f000000UL) == 0x0a000000UL);
-    bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
-    bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
-                           && ((val & 0x0f000000UL) == 0x0b000000UL);
-    bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
-    bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
-
-    if (r_type == elfcpp::R_ARM_CALL)
-      {
-       if (!insn_is_uncond_bl && !insn_is_blx)
-         return This::STATUS_BAD_RELOC;
-      }
-    else if (r_type == elfcpp::R_ARM_JUMP24)
-      {
-       if (!insn_is_b && !insn_is_cond_bl)
-         return This::STATUS_BAD_RELOC;
-      }
-    else if (r_type == elfcpp::R_ARM_PLT32)
-      {
-       if (!insn_is_any_branch)
-         return This::STATUS_BAD_RELOC;
-      }
-    else
-      gold_unreachable();
-
-    Valtype addend = utils::sign_extend<26>(val << 2);
-    Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
-
-    // If target has thumb bit set, we need to either turn the BL
-    // into a BLX (for ARMv5 or above) or generate a stub.
-    if (x & 1)
-      {
-       // Turn BL to BLX.
-       if (insn_is_uncond_bl)
-         val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
-       else
-         return This::STATUS_BAD_RELOC;
-      }
-    else
-      gold_assert(!insn_is_blx);
-
-    val = utils::bit_select(val, (x >> 2), 0xffffffUL);
-    elfcpp::Swap<32, big_endian>::writeval(wv, val);
-    return (utils::has_overflow<26>(x)
-           ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
-  }
+  // Handle ARM long branches.
+  static typename This::Status
+  arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
+                   unsigned char *, const Sized_symbol<32>*,
+                   const Arm_relobj<big_endian>*, unsigned int,
+                   const Symbol_value<32>*, Arm_address, Arm_address, bool);
 
  public:
 
@@ -1976,38 +1935,70 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
 
   // R_ARM_PLT32: (S + A) | T - P
   static inline typename This::Status
-  plt32(unsigned char *view,
-       const Sized_relobj<32, big_endian>* object,
+  plt32(const Relocate_info<32, big_endian>* relinfo,
+       unsigned char *view,
+       const Sized_symbol<32>* gsym,
+       const Arm_relobj<big_endian>* object,
+       unsigned int r_sym,
        const Symbol_value<32>* psymval,
        Arm_address address,
-       Arm_address thumb_bit)
+       Arm_address thumb_bit,
+       bool is_weakly_undefined_without_plt)
+  {
+    return arm_branch_common(elfcpp::R_ARM_PLT32, relinfo, view, gsym,
+                            object, r_sym, psymval, address, thumb_bit,
+                            is_weakly_undefined_without_plt);
+  }
+
+  // R_ARM_XPC25: (S + A) | T - P
+  static inline typename This::Status
+  xpc25(const Relocate_info<32, big_endian>* relinfo,
+       unsigned char *view,
+       const Sized_symbol<32>* gsym,
+       const Arm_relobj<big_endian>* object,
+       unsigned int r_sym,
+       const Symbol_value<32>* psymval,
+       Arm_address address,
+       Arm_address thumb_bit,
+       bool is_weakly_undefined_without_plt)
   {
-    return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
-                                                 address, thumb_bit);
+    return arm_branch_common(elfcpp::R_ARM_XPC25, relinfo, view, gsym,
+                            object, r_sym, psymval, address, thumb_bit,
+                            is_weakly_undefined_without_plt);
   }
 
   // R_ARM_CALL: (S + A) | T - P
   static inline typename This::Status
-  call(unsigned char *view,
-       const Sized_relobj<32, big_endian>* object,
+  call(const Relocate_info<32, big_endian>* relinfo,
+       unsigned char *view,
+       const Sized_symbol<32>* gsym,
+       const Arm_relobj<big_endian>* object,
+       unsigned int r_sym,
        const Symbol_value<32>* psymval,
        Arm_address address,
-       Arm_address thumb_bit)
+       Arm_address thumb_bit,
+       bool is_weakly_undefined_without_plt)
   {
-    return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
-                                                address, thumb_bit);
+    return arm_branch_common(elfcpp::R_ARM_CALL, relinfo, view, gsym,
+                            object, r_sym, psymval, address, thumb_bit,
+                            is_weakly_undefined_without_plt);
   }
 
   // R_ARM_JUMP24: (S + A) | T - P
   static inline typename This::Status
-  jump24(unsigned char *view,
-        const Sized_relobj<32, big_endian>* object,
+  jump24(const Relocate_info<32, big_endian>* relinfo,
+        unsigned char *view,
+        const Sized_symbol<32>* gsym,
+        const Arm_relobj<big_endian>* object,
+        unsigned int r_sym,
         const Symbol_value<32>* psymval,
         Arm_address address,
-        Arm_address thumb_bit)
+        Arm_address thumb_bit,
+        bool is_weakly_undefined_without_plt)
   {
-    return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
-                                                  address, thumb_bit);
+    return arm_branch_common(elfcpp::R_ARM_JUMP24, relinfo, view, gsym,
+                            object, r_sym, psymval, address, thumb_bit,
+                            is_weakly_undefined_without_plt);
   }
 
   // R_ARM_PREL: (S + A) | T - P
@@ -2178,6 +2169,129 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
   }
 };
 
+// Relocate ARM long branches.  This handles relocation types
+// R_ARM_CALL, R_ARM_JUMP24, R_ARM_PLT32 and R_ARM_XPC25.
+// If IS_WEAK_UNDEFINED_WITH_PLT is true.  The target symbol is weakly
+// undefined and we do not use PLT in this relocation.  In such a case,
+// the branch is converted into an NOP.
+
+template<bool big_endian>
+typename Arm_relocate_functions<big_endian>::Status
+Arm_relocate_functions<big_endian>::arm_branch_common(
+    unsigned int r_type,
+    const Relocate_info<32, big_endian>* relinfo,
+    unsigned char *view,
+    const Sized_symbol<32>* gsym,
+    const Arm_relobj<big_endian>* object,
+    unsigned int r_sym,
+    const Symbol_value<32>* psymval,
+    Arm_address address,
+    Arm_address thumb_bit,
+    bool is_weakly_undefined_without_plt)
+{
+  typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+  Valtype* wv = reinterpret_cast<Valtype*>(view);
+  Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
+     
+  bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
+                   && ((val & 0x0f000000UL) == 0x0a000000UL);
+  bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
+  bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
+                         && ((val & 0x0f000000UL) == 0x0b000000UL);
+  bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
+  bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
+
+  // Check that the instruction is valid.
+  if (r_type == elfcpp::R_ARM_CALL)
+    {
+      if (!insn_is_uncond_bl && !insn_is_blx)
+       return This::STATUS_BAD_RELOC;
+    }
+  else if (r_type == elfcpp::R_ARM_JUMP24)
+    {
+      if (!insn_is_b && !insn_is_cond_bl)
+       return This::STATUS_BAD_RELOC;
+    }
+  else if (r_type == elfcpp::R_ARM_PLT32)
+    {
+      if (!insn_is_any_branch)
+       return This::STATUS_BAD_RELOC;
+    }
+  else if (r_type == elfcpp::R_ARM_XPC25)
+    {
+      // FIXME: AAELF document IH0044C does not say much about it other
+      // than it being obsolete.
+      if (!insn_is_any_branch)
+       return This::STATUS_BAD_RELOC;
+    }
+  else
+    gold_unreachable();
+
+  // A branch to an undefined weak symbol is turned into a jump to
+  // the next instruction unless a PLT entry will be created.
+  // Do the same for local undefined symbols.
+  // The jump to the next instruction is optimized as a NOP depending
+  // on the architecture.
+  const Target_arm<big_endian>* arm_target =
+    Target_arm<big_endian>::default_target();
+  if (is_weakly_undefined_without_plt)
+    {
+      Valtype cond = val & 0xf0000000U;
+      if (arm_target->may_use_arm_nop())
+       val = cond | 0x0320f000;
+      else
+       val = cond | 0x01a00000;        // Using pre-UAL nop: mov r0, r0.
+      elfcpp::Swap<32, big_endian>::writeval(wv, val);
+      return This::STATUS_OKAY;
+    }
+  Valtype addend = utils::sign_extend<26>(val << 2);
+  Valtype branch_target = psymval->value(object, addend);
+  int32_t branch_offset = branch_target - address;
+
+  // We need a stub if the branch offset is too large or if we need
+  // to switch mode.
+  bool may_use_blx = arm_target->may_use_blx();
+  Reloc_stub* stub = NULL;
+  if ((branch_offset > ARM_MAX_FWD_BRANCH_OFFSET)
+      || (branch_offset < ARM_MAX_BWD_BRANCH_OFFSET)
+      || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
+    {
+      Stub_type stub_type =
+       Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
+                                       (thumb_bit != 0));
+      if (stub_type != arm_stub_none)
+       {
+         Stub_table<big_endian>* stub_table =
+           object->stub_table(relinfo->data_shndx);
+         gold_assert(stub_table != NULL);
+
+         Reloc_stub::Key stub_key(stub_type, gsym, object, r_sym, addend);
+         stub = stub_table->find_reloc_stub(stub_key);
+         gold_assert(stub != NULL);
+         thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
+         branch_target = stub_table->address() + stub->offset() + addend;
+         branch_offset = branch_target - address;
+         gold_assert((branch_offset <= ARM_MAX_FWD_BRANCH_OFFSET)
+                     && (branch_offset >= ARM_MAX_BWD_BRANCH_OFFSET));
+       }
+    }
+
+  // At this point, if we still need to switch mode, the instruction
+  // must either be a BLX or a BL that can be converted to a BLX.
+  if (thumb_bit != 0)
+    {
+      // Turn BL to BLX.
+      gold_assert(may_use_blx && r_type == elfcpp::R_ARM_CALL);
+      val = (val & 0xffffff) | 0xfa000000 | ((branch_offset & 2) << 23);
+    }
+
+  val = utils::bit_select(val, (branch_offset >> 2), 0xffffffUL);
+  elfcpp::Swap<32, big_endian>::writeval(wv, val);
+  return (utils::has_overflow<26>(branch_offset)
+         ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
+}
+
 // Get the GOT section, creating it if necessary.
 
 template<bool big_endian>
@@ -2388,21 +2502,21 @@ Reloc_stub::stub_type_for_reloc(
   bool thumb_only;
   if (parameters->target().is_big_endian())
     {
-      const Target_arm<true>& big_endian_target =
+      const Target_arm<true>* big_endian_target =
        Target_arm<true>::default_target();
-      may_use_blx = big_endian_target.may_use_blx();
-      should_force_pic_veneer = big_endian_target.should_force_pic_veneer();
-      thumb2 = big_endian_target.using_thumb2();
-      thumb_only = big_endian_target.using_thumb_only();
+      may_use_blx = big_endian_target->may_use_blx();
+      should_force_pic_veneer = big_endian_target->should_force_pic_veneer();
+      thumb2 = big_endian_target->using_thumb2();
+      thumb_only = big_endian_target->using_thumb_only();
     }
   else
     {
-      const Target_arm<false>& little_endian_target =
+      const Target_arm<false>* little_endian_target =
        Target_arm<false>::default_target();
-      may_use_blx = little_endian_target.may_use_blx();
-      should_force_pic_veneer = little_endian_target.should_force_pic_veneer();
-      thumb2 = little_endian_target.using_thumb2();
-      thumb_only = little_endian_target.using_thumb_only();
+      may_use_blx = little_endian_target->may_use_blx();
+      should_force_pic_veneer = little_endian_target->should_force_pic_veneer();
+      thumb2 = little_endian_target->using_thumb2();
+      thumb_only = little_endian_target->using_thumb_only();
     }
 
   int64_t branch_offset = (int64_t)destination - location;
@@ -3380,15 +3494,14 @@ Arm_relobj<big_endian>::do_count_local_symbols(
 template<bool big_endian>
 void
 Arm_relobj<big_endian>::do_relocate_sections(
-    const General_options& options,
     const Symbol_table* symtab,
     const Layout* layout,
     const unsigned char* pshdrs,
     typename Sized_relobj<32, big_endian>::Views* pviews)
 {
   // Call parent to relocate sections.
-  Sized_relobj<32, big_endian>::do_relocate_sections(options, symtab, layout,
-                                                    pshdrs, pviews); 
+  Sized_relobj<32, big_endian>::do_relocate_sections(symtab, layout, pshdrs,
+                                                    pviews); 
 
   // We do not generate stubs if doing a relocatable link.
   if (parameters->options().relocatable())
@@ -3401,7 +3514,6 @@ Arm_relobj<big_endian>::do_relocate_sections(
     Target_arm<big_endian>::default_target();
 
   Relocate_info<32, big_endian> relinfo;
-  relinfo.options = &options;
   relinfo.symtab = symtab;
   relinfo.layout = layout;
   relinfo.object = this;
@@ -4405,6 +4517,7 @@ Target_arm<big_endian>::Relocate::relocate(
   // is 1.  Otherwise it is 0.
   Arm_address thumb_bit = 0;
   Symbol_value<32> symval;
+  bool is_weakly_undefined_without_plt = false;
   if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
     {
       if (gsym != NULL)
@@ -4418,6 +4531,13 @@ Target_arm<big_endian>::Relocate::relocate(
                                      + gsym->plt_offset());
              psymval = &symval;
            }
+         else if (gsym->is_weak_undefined())
+           {
+             // This is a weakly undefined symbol and we do not use PLT
+             // for this relocation.  A branch targeting this symbol will
+             // be converted into an NOP.
+             is_weakly_undefined_without_plt = true;
+           }
          else
            {
              // Set thumb bit if symbol:
@@ -4490,6 +4610,10 @@ Target_arm<big_endian>::Relocate::relocate(
       break;
     }
 
+  // To look up relocation stubs, we need to pass the symbol table index of
+  // a local symbol.
+  unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
+
   typename Arm_relocate_functions::Status reloc_status =
        Arm_relocate_functions::STATUS_OKAY;
   switch (r_type)
@@ -4609,6 +4733,13 @@ Target_arm<big_endian>::Relocate::relocate(
                                                      address, thumb_bit);
       break;
 
+    case elfcpp::R_ARM_XPC25:
+      reloc_status =
+       Arm_relocate_functions::xpc25(relinfo, view, gsym, object, r_sym,
+                                     psymval, address, thumb_bit,
+                                     is_weakly_undefined_without_plt);
+      break;
+
     case elfcpp::R_ARM_GOTOFF32:
       {
        Arm_address got_origin;
@@ -4691,18 +4822,24 @@ Target_arm<big_endian>::Relocate::relocate(
                  || (gsym->is_defined()
                      && !gsym->is_from_dynobj()
                      && !gsym->is_preemptible()));
-      reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
-                                                  address, thumb_bit);
+      reloc_status =
+       Arm_relocate_functions::plt32(relinfo, view, gsym, object, r_sym,
+                                     psymval, address, thumb_bit,
+                                     is_weakly_undefined_without_plt);
       break;
 
     case elfcpp::R_ARM_CALL:
-      reloc_status = Arm_relocate_functions::call(view, object, psymval,
-                                                 address, thumb_bit);
+      reloc_status =
+       Arm_relocate_functions::call(relinfo, view, gsym, object, r_sym,
+                                    psymval, address, thumb_bit,
+                                    is_weakly_undefined_without_plt);
       break;
 
     case elfcpp::R_ARM_JUMP24:
-      reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
-                                                   address, thumb_bit);
+      reloc_status =
+       Arm_relocate_functions::jump24(relinfo, view, gsym, object, r_sym,
+                                      psymval, address, thumb_bit,
+                                      is_weakly_undefined_without_plt);
       break;
 
     case elfcpp::R_ARM_PREL31:
@@ -4775,6 +4912,28 @@ Target_arm<big_endian>::relocate_section(
   typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
   gold_assert(sh_type == elfcpp::SHT_REL);
 
+  Arm_input_section<big_endian>* arm_input_section =
+    this->find_arm_input_section(relinfo->object, relinfo->data_shndx);
+
+  // This is an ARM input section and the view covers the whole output
+  // section.
+  if (arm_input_section != NULL)
+    {
+      gold_assert(needs_special_offset_handling);
+      Arm_address section_address = arm_input_section->address();
+      section_size_type section_size = arm_input_section->data_size();
+
+      gold_assert((arm_input_section->address() >= address)
+                 && ((arm_input_section->address()
+                      + arm_input_section->data_size())
+                     <= (address + view_size)));
+
+      off_t offset = section_address - address;
+      view += offset;
+      address += offset;
+      view_size = section_size;
+    }
+
   gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
                         Arm_relocate>(
     relinfo,
@@ -5614,6 +5773,51 @@ Target_arm<big_endian>::do_relax(
   return any_stub_table_changed;
 }
 
+// Relocate a stub.
+
+template<bool big_endian>
+void
+Target_arm<big_endian>::relocate_stub(
+    Reloc_stub* stub,
+    const Relocate_info<32, big_endian>* relinfo,
+    Output_section* output_section,
+    unsigned char* view,
+    Arm_address address,
+    section_size_type view_size)
+{
+  Relocate relocate;
+  const Stub_template* stub_template = stub->stub_template();
+  for (size_t i = 0; i < stub_template->reloc_count(); i++)
+    {
+      size_t reloc_insn_index = stub_template->reloc_insn_index(i);
+      const Insn_template* insn = &stub_template->insns()[reloc_insn_index];
+
+      unsigned int r_type = insn->r_type();
+      section_size_type reloc_offset = stub_template->reloc_offset(i);
+      section_size_type reloc_size = insn->size();
+      gold_assert(reloc_offset + reloc_size <= view_size);
+
+      // This is the address of the stub destination.
+      Arm_address target = stub->reloc_target(i);
+      Symbol_value<32> symval;
+      symval.set_output_value(target);
+
+      // Synthesize a fake reloc just in case.  We don't have a symbol so
+      // we use 0.
+      unsigned char reloc_buffer[elfcpp::Elf_sizes<32>::rel_size];
+      memset(reloc_buffer, 0, sizeof(reloc_buffer));
+      elfcpp::Rel_write<32, big_endian> reloc_write(reloc_buffer);
+      reloc_write.put_r_offset(reloc_offset);
+      reloc_write.put_r_info(elfcpp::elf_r_info<32>(0, r_type));
+      elfcpp::Rel<32, big_endian> rel(reloc_buffer);
+
+      relocate.relocate(relinfo, this, output_section,
+                       this->fake_relnum_for_stubs, rel, r_type,
+                       NULL, &symval, view + reloc_offset,
+                       address + reloc_offset, reloc_size);
+    }
+}
+
 // The selector for arm object files.
 
 template<bool big_endian>