OSDN Git Service

2009-11-15 Doug Kwan <dougkwan@google.com>
[pf3gnuchains/pf3gnuchains3x.git] / gold / arm.cc
index 420769f..5b702f2 100644 (file)
@@ -572,7 +572,7 @@ class Reloc_stub : public Stub
     unsigned int r_sym_;
     // If r_sym_ is invalid index.  This points to a global symbol.
     // Otherwise, this points a relobj.  We used the unsized and target
-    // independent Symbol and Relobj classes instead of Arm_symbol and  
+    // independent Symbol and Relobj classes instead of Sized_symbol<32> and  
     // Arm_relobj.  This is done to avoid making the stub class a template
     // as most of the stub machinery is endianity-neutral.  However, it
     // may require a bit of casting done by users of this class.
@@ -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);
 
@@ -1042,8 +1041,7 @@ struct Stub_addend_reader
     unsigned int /* r_type */,
     const unsigned char* /* view */,
     const typename Reloc_types<sh_type,
-                              32, big_endian>::Reloc& /* reloc */) const
-  { gold_unreachable(); }
+                              32, big_endian>::Reloc& /* reloc */) const;
 };
 
 // Specialized Stub_addend_reader for SHT_REL type relocation sections.
@@ -1071,7 +1069,8 @@ struct Stub_addend_reader<elfcpp::SHT_RELA, big_endian>
     unsigned int,
     const unsigned char*,
     const typename Reloc_types<elfcpp::SHT_RELA, 32,
-                              big_endian>::Reloc& reloc) const;
+                              big_endian>::Reloc& reloc) const
+  { return reloc.get_r_addend(); }
 };
 
 // Utilities for manipulating integers of up to 32-bits
@@ -1140,11 +1139,16 @@ class Target_arm : public Sized_target<32, big_endian>
   typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
     Reloc_section;
 
+  // When were are relocating a stub, we pass this as the relocation number.
+  static const size_t fake_relnum_for_stubs = static_cast<size_t>(-1);
+
   Target_arm()
     : Sized_target<32, big_endian>(&arm_info),
       got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
-      copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL),
-      may_use_blx_(true), should_force_pic_veneer_(false)
+      copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), stub_tables_(),
+      stub_factory_(Stub_factory::get_instance()),
+      may_use_blx_(true), should_force_pic_veneer_(false),
+      arm_input_section_map_()
   { }
 
   // Whether we can use BLX.
@@ -1183,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
@@ -1281,19 +1293,94 @@ class Target_arm : public Sized_target<32, big_endian>
   static unsigned int
   get_real_reloc_type (unsigned int r_type);
 
+  //
+  // Methods to support stub-generations.
+  //
+  
+  // Return the stub factory
+  const Stub_factory&
+  stub_factory() const
+  { return this->stub_factory_; }
+
+  // Make a new Arm_input_section object.
+  Arm_input_section<big_endian>*
+  new_arm_input_section(Relobj*, unsigned int);
+
+  // Find the Arm_input_section object corresponding to the SHNDX-th input
+  // section of RELOBJ.
+  Arm_input_section<big_endian>*
+  find_arm_input_section(Relobj* relobj, unsigned int shndx) const;
+
+  // Make a new Stub_table
+  Stub_table<big_endian>*
+  new_stub_table(Arm_input_section<big_endian>*);
+
+  // Scan a section for stub generation.
+  void
+  scan_section_for_stubs(const Relocate_info<32, big_endian>*, unsigned int,
+                        const unsigned char*, size_t, Output_section*,
+                        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.
+  static bool
+  reloc_uses_thumb_bit(unsigned int r_type);
+
  protected:
+  // Make an ELF object.
+  Object*
+  do_make_elf_object(const std::string&, Input_file*, off_t,
+                    const elfcpp::Ehdr<32, big_endian>& ehdr);
+
+  Object*
+  do_make_elf_object(const std::string&, Input_file*, off_t,
+                    const elfcpp::Ehdr<32, !big_endian>&)
+  { gold_unreachable(); }
+
+  Object*
+  do_make_elf_object(const std::string&, Input_file*, off_t,
+                     const elfcpp::Ehdr<64, false>&)
+  { gold_unreachable(); }
+
+  Object*
+  do_make_elf_object(const std::string&, Input_file*, off_t,
+                    const elfcpp::Ehdr<64, true>&)
+  { gold_unreachable(); }
+
+  // Make an output section.
+  Output_section*
+  do_make_output_section(const char* name, elfcpp::Elf_Word type,
+                        elfcpp::Elf_Xword flags)
+  { return new Arm_output_section<big_endian>(name, type, flags); }
+
   void
   do_adjust_elf_header(unsigned char* view, int len) const;
 
+  // We only need to generate stubs, and hence perform relaxation if we are
+  // not doing relocatable linking.
+  bool
+  do_may_relax() const
+  { return !parameters->options().relocatable(); }
+
+  bool
+  do_relax(int, const Input_objects*, Symbol_table*, Layout*);
+
  private:
   // The class which scans relocations.
   class Scan
@@ -1473,24 +1560,33 @@ class Target_arm : public Sized_target<32, big_endian>
   void
   merge_processor_specific_flags(const std::string&, elfcpp::Elf_Word);
 
-  Object*
-  do_make_elf_object(const std::string&, Input_file*, off_t,
-                    const elfcpp::Ehdr<32, big_endian>& ehdr);
+  //
+  // Methods to support stub-generations.
+  //
 
-  Object*
-  do_make_elf_object(const std::string&, Input_file*, off_t,
-                    const elfcpp::Ehdr<32, !big_endian>&)
-  { gold_unreachable(); }
+  // Group input sections for stub generation.
+  void
+  group_sections(Layout*, section_size_type, bool);
 
-  Object*
-  do_make_elf_object(const std::string&, Input_file*, off_t,
-                     const elfcpp::Ehdr<64, false>&)
-  { gold_unreachable(); }
+  // Scan a relocation for stub generation.
+  void
+  scan_reloc_for_stub(const Relocate_info<32, big_endian>*, unsigned int,
+                     const Sized_symbol<32>*, unsigned int,
+                     const Symbol_value<32>*,
+                     elfcpp::Elf_types<32>::Elf_Swxword, Arm_address);
 
-  Object*
-  do_make_elf_object(const std::string&, Input_file*, off_t,
-                    const elfcpp::Ehdr<64, true>&)
-  { gold_unreachable(); }
+  // Scan a relocation section for stub.
+  template<int sh_type>
+  void
+  scan_reloc_section_for_stubs(
+      const Relocate_info<32, big_endian>* relinfo,
+      const unsigned char* prelocs,
+      size_t reloc_count,
+      Output_section* output_section,
+      bool needs_special_offset_handling,
+      const unsigned char* view,
+      elfcpp::Elf_types<32>::Elf_Addr view_address,
+      section_size_type);
 
   // Information about this specific target which we pass to the
   // general Target structure.
@@ -1502,6 +1598,15 @@ class Target_arm : public Sized_target<32, big_endian>
     GOT_TYPE_STANDARD = 0      // GOT entry for a regular symbol
   };
 
+  typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
+
+  // Map input section to Arm_input_section.
+  typedef Unordered_map<Input_section_specifier,
+                       Arm_input_section<big_endian>*,
+                       Input_section_specifier::hash,
+                       Input_section_specifier::equal_to>
+         Arm_input_section_map;
+    
   // The GOT section.
   Output_data_got<32, big_endian>* got_;
   // The PLT section.
@@ -1514,10 +1619,16 @@ class Target_arm : public Sized_target<32, big_endian>
   Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
   // Space for variables copied with a COPY reloc.
   Output_data_space* dynbss_;
+  // Vector of Stub_tables created.
+  Stub_table_list stub_tables_;
+  // Stub factory.
+  const Stub_factory &stub_factory_;
   // Whether we can use BLX.
   bool may_use_blx_;
   // Whether we force PIC branch veneers.
   bool should_force_pic_veneer_;
+  // Map for locating Arm_input_sections.
+  Arm_input_section_map arm_input_section_map_;
 };
 
 template<bool big_endian>
@@ -1559,27 +1670,6 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
   typedef Relocate_functions<32, big_endian> Base;
   typedef Arm_relocate_functions<big_endian> This;
 
-  // Get an symbol value of *PSYMVAL with an ADDEND.  This is a wrapper
-  // to Symbol_value::value().  If HAS_THUMB_BIT is true, that LSB is used
-  // to distinguish ARM and THUMB functions and it is treated specially.
-  static inline Symbol_value<32>::Value
-  arm_symbol_value (const Sized_relobj<32, big_endian> *object,
-                   const Symbol_value<32>* psymval,
-                   Symbol_value<32>::Value addend,
-                   bool has_thumb_bit)
-  {
-    typedef Symbol_value<32>::Value Valtype;
-
-    if (has_thumb_bit)
-      {
-       Valtype raw = psymval->value(object, 0);
-       Valtype thumb_bit = raw & 1;
-       return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
-      }
-    else
-      return psymval->value(object, addend);
-  }
-
   // Encoding of imm16 argument for movt and movw ARM instructions
   // from ARM ARM:
   //     
@@ -1653,68 +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,
-                   bool has_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 = (This::arm_symbol_value(object, psymval, addend, has_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:
 
@@ -1729,7 +1763,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
     Reltype addend = utils::sign_extend<8>(val);
-    Reltype x = This::arm_symbol_value(object, psymval, addend, false);
+    Reltype x = psymval->value(object, addend);
     val = utils::bit_select(val, x, 0xffU);
     elfcpp::Swap<8, big_endian>::writeval(wv, val);
     return (utils::has_signed_unsigned_overflow<8>(x)
@@ -1748,7 +1782,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
     Reltype addend = (val & 0x7e0U) >> 6;
-    Reltype x = This::arm_symbol_value(object, psymval, addend, false);
+    Reltype x = psymval->value(object, addend);
     val = utils::bit_select(val, x << 6, 0x7e0U);
     elfcpp::Swap<16, big_endian>::writeval(wv, val);
     return (utils::has_overflow<5>(x)
@@ -1767,7 +1801,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
     Reltype addend = val & 0x0fffU;
-    Reltype x = This::arm_symbol_value(object, psymval, addend, false);
+    Reltype x = psymval->value(object, addend);
     val = utils::bit_select(val, x, 0x0fffU);
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
     return (utils::has_overflow<12>(x)
@@ -1786,7 +1820,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
     Reltype addend = utils::sign_extend<16>(val);
-    Reltype x = This::arm_symbol_value(object, psymval, addend, false);
+    Reltype x = psymval->value(object, addend);
     val = utils::bit_select(val, x, 0xffffU);
     elfcpp::Swap<16, big_endian>::writeval(wv, val);
     return (utils::has_signed_unsigned_overflow<16>(x)
@@ -1799,12 +1833,12 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
   abs32(unsigned char *view,
        const Sized_relobj<32, big_endian>* object,
        const Symbol_value<32>* psymval,
-       bool has_thumb_bit)
+       Arm_address thumb_bit)
   {
     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
-    Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
+    Valtype x = psymval->value(object, addend) | thumb_bit;
     elfcpp::Swap<32, big_endian>::writeval(wv, x);
     return This::STATUS_OKAY;
   }
@@ -1815,13 +1849,12 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
        const Sized_relobj<32, big_endian>* object,
        const Symbol_value<32>* psymval,
        Arm_address address,
-       bool has_thumb_bit)
+       Arm_address thumb_bit)
   {
     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
-    Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit) 
-                - address);
+    Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
     elfcpp::Swap<32, big_endian>::writeval(wv, x);
     return This::STATUS_OKAY;
   }
@@ -1832,7 +1865,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
           const Sized_relobj<32, big_endian>* object,
           const Symbol_value<32>* psymval,
           Arm_address address,
-          bool has_thumb_bit)
+          Arm_address thumb_bit)
   {
     // A thumb call consists of two instructions.
     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
@@ -1844,8 +1877,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     gold_assert((lo & 0xf800) == 0xf800);
     Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
                                           | ((lo & 0x7ff) << 1));
-    Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
-                - address);
+    Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
 
     // If target has no thumb bit set, we need to either turn the BL
     // into a BLX (for ARMv5 or above) or generate a stub.
@@ -1903,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,
+       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,
-       bool has_thumb_bit)
+       Arm_address thumb_bit,
+       bool is_weakly_undefined_without_plt)
   {
-    return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
-                                                 address, has_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,
-       bool has_thumb_bit)
+       Arm_address thumb_bit,
+       bool is_weakly_undefined_without_plt)
   {
-    return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
-                                                address, has_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,
-        bool has_thumb_bit)
+        Arm_address thumb_bit,
+        bool is_weakly_undefined_without_plt)
   {
-    return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
-                                                  address, has_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
@@ -1943,14 +2007,13 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
         const Sized_relobj<32, big_endian>* object,
         const Symbol_value<32>* psymval,
         Arm_address address,
-        bool has_thumb_bit)
+        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);
     Valtype addend = utils::sign_extend<31>(val);
-    Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
-                - address);
+    Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
     val = utils::bit_select(val, x, 0x7fffffffU);
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
     return (utils::has_overflow<31>(x) ?
@@ -1962,13 +2025,13 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
   movw_abs_nc(unsigned char *view,
              const Sized_relobj<32, big_endian>* object,
              const Symbol_value<32>* psymval,
-             bool has_thumb_bit)
+             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);
     Valtype addend =  This::extract_arm_movw_movt_addend(val);
-    Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
+    Valtype x = psymval->value(object, addend) | thumb_bit;
     val = This::insert_val_arm_movw_movt(val, x);
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
     return This::STATUS_OKAY;
@@ -1984,7 +2047,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
     Valtype addend = This::extract_arm_movw_movt_addend(val);
-    Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
+    Valtype x = psymval->value(object, addend) >> 16;
     val = This::insert_val_arm_movw_movt(val, x);
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
     return This::STATUS_OKAY;
@@ -1995,7 +2058,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
   thm_movw_abs_nc(unsigned char *view,
                  const Sized_relobj<32, big_endian>* object,
                  const Symbol_value<32>* psymval,
-                 bool has_thumb_bit)
+                 Arm_address thumb_bit)
   {
     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
@@ -2003,7 +2066,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
                   | elfcpp::Swap<16, big_endian>::readval(wv + 1));
     Reltype addend = extract_thumb_movw_movt_addend(val);
-    Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
+    Reltype x = psymval->value(object, addend) | thumb_bit;
     val = This::insert_val_thumb_movw_movt(val, x);
     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
@@ -2022,7 +2085,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
                   | elfcpp::Swap<16, big_endian>::readval(wv + 1));
     Reltype addend = This::extract_thumb_movw_movt_addend(val);
-    Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
+    Reltype x = psymval->value(object, addend) >> 16;
     val = This::insert_val_thumb_movw_movt(val, x);
     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
@@ -2035,14 +2098,13 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
               const Sized_relobj<32, big_endian>* object,
               const Symbol_value<32>* psymval,
               Arm_address address,
-              bool has_thumb_bit)
+              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);
     Valtype addend = This::extract_arm_movw_movt_addend(val);
-    Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
-                 - address);
+    Valtype x = (psymval->value(object, addend) | thumb_bit) - address;
     val = This::insert_val_arm_movw_movt(val, x);
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
     return This::STATUS_OKAY;
@@ -2059,8 +2121,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
     Valtype addend = This::extract_arm_movw_movt_addend(val);
-    Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
-                 - address) >> 16;
+    Valtype x = (psymval->value(object, addend) - address) >> 16;
     val = This::insert_val_arm_movw_movt(val, x);
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
     return This::STATUS_OKAY;
@@ -2072,7 +2133,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
                   const Sized_relobj<32, big_endian>* object,
                   const Symbol_value<32>* psymval,
                   Arm_address address,
-                  bool has_thumb_bit)
+                  Arm_address thumb_bit)
   {
     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
@@ -2080,8 +2141,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
                  | elfcpp::Swap<16, big_endian>::readval(wv + 1);
     Reltype addend = This::extract_thumb_movw_movt_addend(val);
-    Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
-                 - address);
+    Reltype x = (psymval->value(object, addend) | thumb_bit) - address;
     val = This::insert_val_thumb_movw_movt(val, x);
     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
@@ -2101,8 +2161,7 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
                  | elfcpp::Swap<16, big_endian>::readval(wv + 1);
     Reltype addend = This::extract_thumb_movw_movt_addend(val);
-    Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
-                 - address) >> 16;
+    Reltype x = (psymval->value(object, addend) - address) >> 16;
     val = This::insert_val_thumb_movw_movt(val, x);
     elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
     elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
@@ -2110,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>
@@ -2126,7 +2308,7 @@ Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
                                           (elfcpp::SHF_ALLOC
                                            | elfcpp::SHF_WRITE),
-                                          this->got_);
+                                          this->got_, false);
       os->set_is_relro();
 
       // The old GNU linker creates a .got.plt section.  We just
@@ -2137,7 +2319,7 @@ Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
                                           (elfcpp::SHF_ALLOC
                                            | elfcpp::SHF_WRITE),
-                                          this->got_plt_);
+                                          this->got_plt_, false);
       os->set_is_relro();
 
       // The first three entries are reserved.
@@ -2165,7 +2347,7 @@ Target_arm<big_endian>::rel_dyn_section(Layout* layout)
       gold_assert(layout != NULL);
       this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
       layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
-                                     elfcpp::SHF_ALLOC, this->rel_dyn_);
+                                     elfcpp::SHF_ALLOC, this->rel_dyn_, true);
     }
   return this->rel_dyn_;
 }
@@ -2320,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;
@@ -3312,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())
@@ -3333,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;
@@ -3548,7 +3728,7 @@ Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
 {
   this->rel_ = new Reloc_section(false);
   layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
-                                 elfcpp::SHF_ALLOC, this->rel_);
+                                 elfcpp::SHF_ALLOC, this->rel_, true);
 }
 
 template<bool big_endian>
@@ -3710,7 +3890,7 @@ Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
                                      (elfcpp::SHF_ALLOC
                                       | elfcpp::SHF_EXECINSTR),
-                                     this->plt_);
+                                     this->plt_, false);
     }
   this->plt_->add_entry(gsym);
 }
@@ -4212,10 +4392,12 @@ Target_arm<big_endian>::do_finalize_sections(
   Output_data_dynamic* const odyn = layout->dynamic_data();
   if (odyn != NULL)
     {
-      if (this->got_plt_ != NULL)
+      if (this->got_plt_ != NULL
+         && this->got_plt_->output_section() != NULL)
        odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
 
-      if (this->plt_ != NULL)
+      if (this->plt_ != NULL
+         && this->plt_->output_section() != NULL)
        {
          const Output_data* od = this->plt_->rel_plt();
          odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
@@ -4223,7 +4405,8 @@ Target_arm<big_endian>::do_finalize_sections(
          odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
        }
 
-      if (this->rel_dyn_ != NULL)
+      if (this->rel_dyn_ != NULL
+         && this->rel_dyn_->output_section() != NULL)
        {
          const Output_data* od = this->rel_dyn_;
          odyn->add_section_address(elfcpp::DT_REL, od);
@@ -4260,7 +4443,8 @@ Target_arm<big_endian>::do_finalize_sections(
                      == NULL);
          Output_segment*  exidx_segment =
            layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
-         exidx_segment->add_output_section(exidx_section, elfcpp::PF_R);
+         exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
+                                           false);
        }
     }
 }
@@ -4326,24 +4510,76 @@ Target_arm<big_endian>::Relocate::relocate(
 
   r_type = get_real_reloc_type(r_type);
 
-  // If this the symbol may be a Thumb function, set thumb bit to 1.
-  bool has_thumb_bit = ((gsym != NULL)
-                       && (gsym->type() == elfcpp::STT_FUNC
-                           || gsym->type() == elfcpp::STT_ARM_TFUNC));
+  const Arm_relobj<big_endian>* object =
+    Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
 
-  // Pick the value to use for symbols defined in shared objects.
+  // If the final branch target of a relocation is THUMB instruction, this
+  // is 1.  Otherwise it is 0.
+  Arm_address thumb_bit = 0;
   Symbol_value<32> symval;
-  if (gsym != NULL
-      && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
+  bool is_weakly_undefined_without_plt = false;
+  if (relnum != Target_arm<big_endian>::fake_relnum_for_stubs)
     {
-      symval.set_output_value(target->plt_section()->address()
-                             + gsym->plt_offset());
-      psymval = &symval;
-      has_thumb_bit = 0;
+      if (gsym != NULL)
+       {
+         // This is a global symbol.  Determine if we use PLT and if the
+         // final target is THUMB.
+         if (gsym->use_plt_offset(reloc_is_non_pic(r_type)))
+           {
+             // This uses a PLT, change the symbol value.
+             symval.set_output_value(target->plt_section()->address()
+                                     + 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:
+             // -Has type STT_ARM_TFUNC or
+             // -Has type STT_FUNC, is defined and with LSB in value set.
+             thumb_bit =
+               (((gsym->type() == elfcpp::STT_ARM_TFUNC)
+                || (gsym->type() == elfcpp::STT_FUNC
+                    && !gsym->is_undefined()
+                    && ((psymval->value(object, 0) & 1) != 0)))
+               ? 1
+               : 0);
+           }
+       }
+      else
+       {
+          // This is a local symbol.  Determine if the final target is THUMB.
+          // We saved this information when all the local symbols were read.
+         elfcpp::Elf_types<32>::Elf_WXword r_info = rel.get_r_info();
+         unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
+         thumb_bit = object->local_symbol_is_thumb_function(r_sym) ? 1 : 0;
+       }
+    }
+  else
+    {
+      // This is a fake relocation synthesized for a stub.  It does not have
+      // a real symbol.  We just look at the LSB of the symbol value to
+      // determine if the target is THUMB or not.
+      thumb_bit = ((psymval->value(object, 0) & 1) != 0);
     }
 
-  const Sized_relobj<32, big_endian>* object = relinfo->object;
-  
+  // Strip LSB if this points to a THUMB target.
+  if (thumb_bit != 0
+      && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type) 
+      && ((psymval->value(object, 0) & 1) != 0))
+    {
+      Arm_address stripped_value =
+       psymval->value(object, 0) & ~static_cast<Arm_address>(1);
+      symval.set_output_value(stripped_value);
+      psymval = &symval;
+    } 
+
   // Get the GOT offset if needed.
   // The GOT pointer points to the end of the GOT section.
   // We need to subtract the size of the GOT section to get
@@ -4374,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)
@@ -4403,7 +4643,7 @@ Target_arm<big_endian>::Relocate::relocate(
       if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
                                    output_section))
        reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
-                                                    has_thumb_bit);
+                                                    thumb_bit);
       break;
 
     case elfcpp::R_ARM_ABS32_NOI:
@@ -4419,7 +4659,7 @@ Target_arm<big_endian>::Relocate::relocate(
                                    output_section))
        reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
                                                           psymval,
-                                                                  has_thumb_bit);
+                                                                  thumb_bit);
       else
        gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
                     "a shared object; recompile with -fPIC"));
@@ -4439,7 +4679,7 @@ Target_arm<big_endian>::Relocate::relocate(
                                    output_section))
        reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
                                                               psymval,
-                                                                      has_thumb_bit);
+                                                                      thumb_bit);
       else
        gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
                     "making a shared object; recompile with -fPIC"));
@@ -4458,7 +4698,7 @@ Target_arm<big_endian>::Relocate::relocate(
     case elfcpp::R_ARM_MOVW_PREL_NC:
       reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
                                                          psymval, address,
-                                                         has_thumb_bit);
+                                                         thumb_bit);
       break;
 
     case elfcpp::R_ARM_MOVT_PREL:
@@ -4469,7 +4709,7 @@ Target_arm<big_endian>::Relocate::relocate(
     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
       reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
                                                              psymval, address,
-                                                             has_thumb_bit);
+                                                             thumb_bit);
       break;
 
     case elfcpp::R_ARM_THM_MOVT_PREL:
@@ -4479,7 +4719,7 @@ Target_arm<big_endian>::Relocate::relocate(
        
     case elfcpp::R_ARM_REL32:
       reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
-                                                  address, has_thumb_bit);
+                                                  address, thumb_bit);
       break;
 
     case elfcpp::R_ARM_THM_ABS5:
@@ -4490,7 +4730,14 @@ Target_arm<big_endian>::Relocate::relocate(
 
     case elfcpp::R_ARM_THM_CALL:
       reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
-                                                     address, has_thumb_bit);
+                                                     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:
@@ -4498,7 +4745,7 @@ Target_arm<big_endian>::Relocate::relocate(
        Arm_address got_origin;
        got_origin = target->got_plt_section()->address();
        reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
-                                                    got_origin, has_thumb_bit);
+                                                    got_origin, thumb_bit);
       }
       break;
 
@@ -4575,23 +4822,29 @@ 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, has_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, has_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, has_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:
       reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
-                                                   address, has_thumb_bit);
+                                                   address, thumb_bit);
       break;
 
     case elfcpp::R_ARM_TARGET1:
@@ -4659,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,
@@ -4968,6 +5243,581 @@ Target_arm<big_endian>::do_make_elf_object(
     }
 }
 
+// Return whether a relocation type used the LSB to distinguish THUMB
+// addresses.
+template<bool big_endian>
+bool
+Target_arm<big_endian>::reloc_uses_thumb_bit(unsigned int r_type)
+{
+  switch (r_type)
+    {
+    case elfcpp::R_ARM_PC24:
+    case elfcpp::R_ARM_ABS32:
+    case elfcpp::R_ARM_REL32:
+    case elfcpp::R_ARM_SBREL32:
+    case elfcpp::R_ARM_THM_CALL:
+    case elfcpp::R_ARM_GLOB_DAT:
+    case elfcpp::R_ARM_JUMP_SLOT:
+    case elfcpp::R_ARM_GOTOFF32:
+    case elfcpp::R_ARM_PLT32:
+    case elfcpp::R_ARM_CALL:
+    case elfcpp::R_ARM_JUMP24:
+    case elfcpp::R_ARM_THM_JUMP24:
+    case elfcpp::R_ARM_SBREL31:
+    case elfcpp::R_ARM_PREL31:
+    case elfcpp::R_ARM_MOVW_ABS_NC:
+    case elfcpp::R_ARM_MOVW_PREL_NC:
+    case elfcpp::R_ARM_THM_MOVW_ABS_NC:
+    case elfcpp::R_ARM_THM_MOVW_PREL_NC:
+    case elfcpp::R_ARM_THM_JUMP19:
+    case elfcpp::R_ARM_THM_ALU_PREL_11_0:
+    case elfcpp::R_ARM_ALU_PC_G0_NC:
+    case elfcpp::R_ARM_ALU_PC_G0:
+    case elfcpp::R_ARM_ALU_PC_G1_NC:
+    case elfcpp::R_ARM_ALU_PC_G1:
+    case elfcpp::R_ARM_ALU_PC_G2:
+    case elfcpp::R_ARM_ALU_SB_G0_NC:
+    case elfcpp::R_ARM_ALU_SB_G0:
+    case elfcpp::R_ARM_ALU_SB_G1_NC:
+    case elfcpp::R_ARM_ALU_SB_G1:
+    case elfcpp::R_ARM_ALU_SB_G2:
+    case elfcpp::R_ARM_MOVW_BREL_NC:
+    case elfcpp::R_ARM_MOVW_BREL:
+    case elfcpp::R_ARM_THM_MOVW_BREL_NC:
+    case elfcpp::R_ARM_THM_MOVW_BREL:
+      return true;
+    default:
+      return false;
+    }
+}
+
+// Stub-generation methods for Target_arm.
+
+// Make a new Arm_input_section object.
+
+template<bool big_endian>
+Arm_input_section<big_endian>*
+Target_arm<big_endian>::new_arm_input_section(
+    Relobj* relobj,
+    unsigned int shndx)
+{
+  Input_section_specifier iss(relobj, shndx);
+
+  Arm_input_section<big_endian>* arm_input_section =
+    new Arm_input_section<big_endian>(relobj, shndx);
+  arm_input_section->init();
+
+  // Register new Arm_input_section in map for look-up.
+  std::pair<typename Arm_input_section_map::iterator, bool> ins =
+    this->arm_input_section_map_.insert(std::make_pair(iss, arm_input_section));
+
+  // Make sure that it we have not created another Arm_input_section
+  // for this input section already.
+  gold_assert(ins.second);
+
+  return arm_input_section; 
+}
+
+// Find the Arm_input_section object corresponding to the SHNDX-th input
+// section of RELOBJ.
+
+template<bool big_endian>
+Arm_input_section<big_endian>*
+Target_arm<big_endian>::find_arm_input_section(
+    Relobj* relobj,
+    unsigned int shndx) const
+{
+  Input_section_specifier iss(relobj, shndx);
+  typename Arm_input_section_map::const_iterator p =
+    this->arm_input_section_map_.find(iss);
+  return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
+}
+
+// Make a new stub table.
+
+template<bool big_endian>
+Stub_table<big_endian>*
+Target_arm<big_endian>::new_stub_table(Arm_input_section<big_endian>* owner)
+{
+  Stub_table<big_endian>* stub_table =
+    new Stub_table<big_endian>(owner);
+  this->stub_tables_.push_back(stub_table);
+
+  stub_table->set_address(owner->address() + owner->data_size());
+  stub_table->set_file_offset(owner->offset() + owner->data_size());
+  stub_table->finalize_data_size();
+
+  return stub_table;
+}
+
+// Scan a relocation for stub generation.
+
+template<bool big_endian>
+void
+Target_arm<big_endian>::scan_reloc_for_stub(
+    const Relocate_info<32, big_endian>* relinfo,
+    unsigned int r_type,
+    const Sized_symbol<32>* gsym,
+    unsigned int r_sym,
+    const Symbol_value<32>* psymval,
+    elfcpp::Elf_types<32>::Elf_Swxword addend,
+    Arm_address address)
+{
+  typedef typename Target_arm<big_endian>::Relocate Relocate;
+
+  const Arm_relobj<big_endian>* arm_relobj =
+    Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
+
+  bool target_is_thumb;
+  Symbol_value<32> symval;
+  if (gsym != NULL)
+    {
+      // This is a global symbol.  Determine if we use PLT and if the
+      // final target is THUMB.
+      if (gsym->use_plt_offset(Relocate::reloc_is_non_pic(r_type)))
+       {
+         // This uses a PLT, change the symbol value.
+         symval.set_output_value(this->plt_section()->address()
+                                 + gsym->plt_offset());
+         psymval = &symval;
+         target_is_thumb = false;
+       }
+      else if (gsym->is_undefined())
+       // There is no need to generate a stub symbol is undefined.
+       return;
+      else
+       {
+         target_is_thumb =
+           ((gsym->type() == elfcpp::STT_ARM_TFUNC)
+            || (gsym->type() == elfcpp::STT_FUNC
+                && !gsym->is_undefined()
+                && ((psymval->value(arm_relobj, 0) & 1) != 0)));
+       }
+    }
+  else
+    {
+      // This is a local symbol.  Determine if the final target is THUMB.
+      target_is_thumb = arm_relobj->local_symbol_is_thumb_function(r_sym);
+    }
+
+  // Strip LSB if this points to a THUMB target.
+  if (target_is_thumb
+      && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
+      && ((psymval->value(arm_relobj, 0) & 1) != 0))
+    {
+      Arm_address stripped_value =
+       psymval->value(arm_relobj, 0) & ~static_cast<Arm_address>(1);
+      symval.set_output_value(stripped_value);
+      psymval = &symval;
+    } 
+
+  // Get the symbol value.
+  Symbol_value<32>::Value value = psymval->value(arm_relobj, 0);
+
+  // Owing to pipelining, the PC relative branches below actually skip
+  // two instructions when the branch offset is 0.
+  Arm_address destination;
+  switch (r_type)
+    {
+    case elfcpp::R_ARM_CALL:
+    case elfcpp::R_ARM_JUMP24:
+    case elfcpp::R_ARM_PLT32:
+      // ARM branches.
+      destination = value + addend + 8;
+      break;
+    case elfcpp::R_ARM_THM_CALL:
+    case elfcpp::R_ARM_THM_XPC22:
+    case elfcpp::R_ARM_THM_JUMP24:
+    case elfcpp::R_ARM_THM_JUMP19:
+      // THUMB branches.
+      destination = value + addend + 4;
+      break;
+    default:
+      gold_unreachable();
+    }
+
+  Stub_type stub_type =
+    Reloc_stub::stub_type_for_reloc(r_type, address, destination,
+                                   target_is_thumb);
+
+  // This reloc does not need a stub.
+  if (stub_type == arm_stub_none)
+    return;
+
+  // Try looking up an existing stub from a stub table.
+  Stub_table<big_endian>* stub_table = 
+    arm_relobj->stub_table(relinfo->data_shndx);
+  gold_assert(stub_table != NULL);
+   
+  // Locate stub by destination.
+  Reloc_stub::Key stub_key(stub_type, gsym, arm_relobj, r_sym, addend);
+
+  // Create a stub if there is not one already
+  Reloc_stub* stub = stub_table->find_reloc_stub(stub_key);
+  if (stub == NULL)
+    {
+      // create a new stub and add it to stub table.
+      stub = this->stub_factory().make_reloc_stub(stub_type);
+      stub_table->add_reloc_stub(stub, stub_key);
+    }
+
+  // Record the destination address.
+  stub->set_destination_address(destination
+                               | (target_is_thumb ? 1 : 0));
+}
+
+// This function scans a relocation sections for stub generation.
+// The template parameter Relocate must be a class type which provides
+// a single function, relocate(), which implements the machine
+// specific part of a relocation.
+
+// BIG_ENDIAN is the endianness of the data.  SH_TYPE is the section type:
+// SHT_REL or SHT_RELA.
+
+// PRELOCS points to the relocation data.  RELOC_COUNT is the number
+// of relocs.  OUTPUT_SECTION is the output section.
+// NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be
+// mapped to output offsets.
+
+// VIEW is the section data, VIEW_ADDRESS is its memory address, and
+// VIEW_SIZE is the size.  These refer to the input section, unless
+// NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to
+// the output section.
+
+template<bool big_endian>
+template<int sh_type>
+void inline
+Target_arm<big_endian>::scan_reloc_section_for_stubs(
+    const Relocate_info<32, big_endian>* relinfo,
+    const unsigned char* prelocs,
+    size_t reloc_count,
+    Output_section* output_section,
+    bool needs_special_offset_handling,
+    const unsigned char* view,
+    elfcpp::Elf_types<32>::Elf_Addr view_address,
+    section_size_type)
+{
+  typedef typename Reloc_types<sh_type, 32, big_endian>::Reloc Reltype;
+  const int reloc_size =
+    Reloc_types<sh_type, 32, big_endian>::reloc_size;
+
+  Arm_relobj<big_endian>* arm_object =
+    Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
+  unsigned int local_count = arm_object->local_symbol_count();
+
+  Comdat_behavior comdat_behavior = CB_UNDETERMINED;
+
+  for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
+    {
+      Reltype reloc(prelocs);
+
+      typename elfcpp::Elf_types<32>::Elf_WXword r_info = reloc.get_r_info();
+      unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
+      unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
+
+      r_type = this->get_real_reloc_type(r_type);
+
+      // Only a few relocation types need stubs.
+      if ((r_type != elfcpp::R_ARM_CALL)
+         && (r_type != elfcpp::R_ARM_JUMP24)
+         && (r_type != elfcpp::R_ARM_PLT32)
+         && (r_type != elfcpp::R_ARM_THM_CALL)
+         && (r_type != elfcpp::R_ARM_THM_XPC22)
+         && (r_type != elfcpp::R_ARM_THM_JUMP24)
+         && (r_type != elfcpp::R_ARM_THM_JUMP19))
+       continue;
+
+      section_offset_type offset =
+       convert_to_section_size_type(reloc.get_r_offset());
+
+      if (needs_special_offset_handling)
+       {
+         offset = output_section->output_offset(relinfo->object,
+                                                relinfo->data_shndx,
+                                                offset);
+         if (offset == -1)
+           continue;
+       }
+
+      // Get the addend.
+      Stub_addend_reader<sh_type, big_endian> stub_addend_reader;
+      elfcpp::Elf_types<32>::Elf_Swxword addend =
+       stub_addend_reader(r_type, view + offset, reloc);
+
+      const Sized_symbol<32>* sym;
+
+      Symbol_value<32> symval;
+      const Symbol_value<32> *psymval;
+      if (r_sym < local_count)
+       {
+         sym = NULL;
+         psymval = arm_object->local_symbol(r_sym);
+
+          // If the local symbol belongs to a section we are discarding,
+          // and that section is a debug section, try to find the
+          // corresponding kept section and map this symbol to its
+          // counterpart in the kept section.  The symbol must not 
+          // correspond to a section we are folding.
+         bool is_ordinary;
+         unsigned int shndx = psymval->input_shndx(&is_ordinary);
+         if (is_ordinary
+             && shndx != elfcpp::SHN_UNDEF
+             && !arm_object->is_section_included(shndx) 
+              && !(relinfo->symtab->is_section_folded(arm_object, shndx)))
+           {
+             if (comdat_behavior == CB_UNDETERMINED)
+               {
+                 std::string name =
+                   arm_object->section_name(relinfo->data_shndx);
+                 comdat_behavior = get_comdat_behavior(name.c_str());
+               }
+             if (comdat_behavior == CB_PRETEND)
+               {
+                  bool found;
+                 typename elfcpp::Elf_types<32>::Elf_Addr value =
+                   arm_object->map_to_kept_section(shndx, &found);
+                 if (found)
+                   symval.set_output_value(value + psymval->input_value());
+                  else
+                    symval.set_output_value(0);
+               }
+             else
+               {
+                  symval.set_output_value(0);
+               }
+             symval.set_no_output_symtab_entry();
+             psymval = &symval;
+           }
+       }
+      else
+       {
+         const Symbol* gsym = arm_object->global_symbol(r_sym);
+         gold_assert(gsym != NULL);
+         if (gsym->is_forwarder())
+           gsym = relinfo->symtab->resolve_forwards(gsym);
+
+         sym = static_cast<const Sized_symbol<32>*>(gsym);
+         if (sym->has_symtab_index())
+           symval.set_output_symtab_index(sym->symtab_index());
+         else
+           symval.set_no_output_symtab_entry();
+
+         // We need to compute the would-be final value of this global
+         // symbol.
+         const Symbol_table* symtab = relinfo->symtab;
+         const Sized_symbol<32>* sized_symbol =
+           symtab->get_sized_symbol<32>(gsym);
+         Symbol_table::Compute_final_value_status status;
+         Arm_address value =
+           symtab->compute_final_value<32>(sized_symbol, &status);
+
+         // Skip this if the symbol has not output section.
+         if (status == Symbol_table::CFVS_NO_OUTPUT_SECTION)
+           continue;
+
+         symval.set_output_value(value);
+         psymval = &symval;
+       }
+
+      // If symbol is a section symbol, we don't know the actual type of
+      // destination.  Give up.
+      if (psymval->is_section_symbol())
+       continue;
+
+      this->scan_reloc_for_stub(relinfo, r_type, sym, r_sym, psymval,
+                               addend, view_address + offset);
+    }
+}
+
+// Scan an input section for stub generation.
+
+template<bool big_endian>
+void
+Target_arm<big_endian>::scan_section_for_stubs(
+    const Relocate_info<32, big_endian>* relinfo,
+    unsigned int sh_type,
+    const unsigned char* prelocs,
+    size_t reloc_count,
+    Output_section* output_section,
+    bool needs_special_offset_handling,
+    const unsigned char* view,
+    Arm_address view_address,
+    section_size_type view_size)
+{
+  if (sh_type == elfcpp::SHT_REL)
+    this->scan_reloc_section_for_stubs<elfcpp::SHT_REL>(
+       relinfo,
+       prelocs,
+       reloc_count,
+       output_section,
+       needs_special_offset_handling,
+       view,
+       view_address,
+       view_size);
+  else if (sh_type == elfcpp::SHT_RELA)
+    // We do not support RELA type relocations yet.  This is provided for
+    // completeness.
+    this->scan_reloc_section_for_stubs<elfcpp::SHT_RELA>(
+       relinfo,
+       prelocs,
+       reloc_count,
+       output_section,
+       needs_special_offset_handling,
+       view,
+       view_address,
+       view_size);
+  else
+    gold_unreachable();
+}
+
+// Group input sections for stub generation.
+//
+// We goup input sections in an output sections so that the total size,
+// including any padding space due to alignment is smaller than GROUP_SIZE
+// unless the only input section in group is bigger than GROUP_SIZE already.
+// Then an ARM stub table is created to follow the last input section
+// in group.  For each group an ARM stub table is created an is placed
+// after the last group.  If STUB_ALWATS_AFTER_BRANCH is false, we further
+// extend the group after the stub table.
+
+template<bool big_endian>
+void
+Target_arm<big_endian>::group_sections(
+    Layout* layout,
+    section_size_type group_size,
+    bool stubs_always_after_branch)
+{
+  // Group input sections and insert stub table
+  Layout::Section_list section_list;
+  layout->get_allocated_sections(&section_list);
+  for (Layout::Section_list::const_iterator p = section_list.begin();
+       p != section_list.end();
+       ++p)
+    {
+      Arm_output_section<big_endian>* output_section =
+       Arm_output_section<big_endian>::as_arm_output_section(*p);
+      output_section->group_sections(group_size, stubs_always_after_branch,
+                                    this);
+    }
+}
+
+// Relaxation hook.  This is where we do stub generation.
+
+template<bool big_endian>
+bool
+Target_arm<big_endian>::do_relax(
+    int pass,
+    const Input_objects* input_objects,
+    Symbol_table* symtab,
+    Layout* layout)
+{
+  // No need to generate stubs if this is a relocatable link.
+  gold_assert(!parameters->options().relocatable());
+
+  // If this is the first pass, we need to group input sections into
+  // stub groups.
+  if (pass == 1)
+    {
+      // Determine the stub group size.  The group size is the absolute
+      // value of the parameter --stub-group-size.  If --stub-group-size
+      // is passed a negative value, we restict stubs to be always after
+      // the stubbed branches.
+      int32_t stub_group_size_param =
+       parameters->options().stub_group_size();
+      bool stubs_always_after_branch = stub_group_size_param < 0;
+      section_size_type stub_group_size = abs(stub_group_size_param);
+
+      if (stub_group_size == 1)
+       {
+         // Default value.
+         // Thumb branch range is +-4MB has to be used as the default
+         // maximum size (a given section can contain both ARM and Thumb
+         // code, so the worst case has to be taken into account).
+         //
+         // This value is 24K less than that, which allows for 2025
+         // 12-byte stubs.  If we exceed that, then we will fail to link.
+         // The user will have to relink with an explicit group size
+         // option.
+         stub_group_size = 4170000;
+       }
+
+      group_sections(layout, stub_group_size, stubs_always_after_branch);
+    }
+
+  // clear changed flags for all stub_tables
+  typedef typename Stub_table_list::iterator Stub_table_iterator;
+  for (Stub_table_iterator sp = this->stub_tables_.begin();
+       sp != this->stub_tables_.end();
+       ++sp)
+    (*sp)->set_has_been_changed(false);
+
+  // scan relocs for stubs
+  for (Input_objects::Relobj_iterator op = input_objects->relobj_begin();
+       op != input_objects->relobj_end();
+       ++op)
+    {
+      Arm_relobj<big_endian>* arm_relobj =
+       Arm_relobj<big_endian>::as_arm_relobj(*op);
+      arm_relobj->scan_sections_for_stubs(this, symtab, layout);
+    }
+
+  bool any_stub_table_changed = false;
+  for (Stub_table_iterator sp = this->stub_tables_.begin();
+       (sp != this->stub_tables_.end()) && !any_stub_table_changed;
+       ++sp)
+    {
+      if ((*sp)->has_been_changed())
+       any_stub_table_changed = true;
+    }
+
+  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>