OSDN Git Service

2010-05-12 Doug Kwan <dougkwan@google.com>
[pf3gnuchains/pf3gnuchains4x.git] / gold / arm.cc
index 87459a5..c304d2a 100644 (file)
@@ -1,6 +1,6 @@
 // arm.cc -- arm target support for gold.
 
-// Copyright 2009 Free Software Foundation, Inc.
+// Copyright 2009, 2010 Free Software Foundation, Inc.
 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
 // by Ian Lance Taylor <iant@google.com>.
 // This file also contains borrowed and adapted code from
@@ -32,6 +32,7 @@
 #include <algorithm>
 #include <map>
 #include <utility>
+#include <set>
 
 #include "elfcpp.h"
 #include "parameters.h"
@@ -49,6 +50,7 @@
 #include "defstd.h"
 #include "gc.h"
 #include "attributes.h"
+#include "arm-reloc-property.h"
 
 namespace
 {
@@ -64,13 +66,27 @@ class Stub_table;
 template<bool big_endian>
 class Arm_input_section;
 
+class Arm_exidx_cantunwind;
+
+class Arm_exidx_merged_section;
+
+class Arm_exidx_fixup;
+
 template<bool big_endian>
 class Arm_output_section;
 
+class Arm_exidx_input_section;
+
 template<bool big_endian>
 class Arm_relobj;
 
 template<bool big_endian>
+class Arm_relocate_functions;
+
+template<bool big_endian>
+class Arm_output_data_got;
+
+template<bool big_endian>
 class Target_arm;
 
 // For convenience.
@@ -84,55 +100,34 @@ const int32_t THM_MAX_BWD_BRANCH_OFFSET = (-(1 << 22) + 4);
 const int32_t THM2_MAX_FWD_BRANCH_OFFSET = (((1 << 24) - 2) + 4);
 const int32_t THM2_MAX_BWD_BRANCH_OFFSET = (-(1 << 24) + 4);
 
+// Thread Control Block size.
+const size_t ARM_TCB_SIZE = 8;
+
 // The arm target class.
 //
 // This is a very simple port of gold for ARM-EABI.  It is intended for
-// supporting Android only for the time being.  Only these relocation types
-// are supported.
-//
-// R_ARM_NONE
-// R_ARM_ABS32
-// R_ARM_ABS32_NOI
-// R_ARM_ABS16
-// R_ARM_ABS12
-// R_ARM_ABS8
-// R_ARM_THM_ABS5
-// R_ARM_BASE_ABS
-// R_ARM_REL32
-// R_ARM_THM_CALL
-// R_ARM_COPY
-// R_ARM_GLOB_DAT
-// R_ARM_BASE_PREL
-// R_ARM_JUMP_SLOT
-// R_ARM_RELATIVE
-// R_ARM_GOTOFF32
-// R_ARM_GOT_BREL
-// R_ARM_GOT_PREL
-// R_ARM_PLT32
-// R_ARM_CALL
-// R_ARM_JUMP24
-// R_ARM_TARGET1
-// R_ARM_PREL31
-// R_ARM_ABS8
-// R_ARM_MOVW_ABS_NC
-// R_ARM_MOVT_ABS
-// R_ARM_THM_MOVW_ABS_NC
-// R_ARM_THM_MOVT_ABS
-// R_ARM_MOVW_PREL_NC
-// R_ARM_MOVT_PREL
-// R_ARM_THM_MOVW_PREL_NC
-// R_ARM_THM_MOVT_PREL
-// R_ARM_V4BX
-// R_ARM_THM_JUMP6
-// R_ARM_THM_JUMP8
-// R_ARM_THM_JUMP11
+// supporting Android only for the time being.
 // 
 // TODOs:
-// - Support more relocation types as needed. 
+// - Implement all static relocation types documented in arm-reloc.def.
 // - Make PLTs more flexible for different architecture features like
 //   Thumb-2 and BE8.
 // There are probably a lot more.
 
+// Ideally we would like to avoid using global variables but this is used
+// very in many places and sometimes in loops.  If we use a function
+// returning a static instance of Arm_reloc_property_table, it will very
+// slow in an threaded environment since the static instance needs to be
+// locked.  The pointer is below initialized in the
+// Target::do_select_as_default_target() hook so that we do not spend time
+// building the table if we are not linking ARM objects.
+//
+// An alternative is to to process the information in arm-reloc.def in
+// compilation time and generate a representation of it in PODs only.  That
+// way we can avoid initialization when the linker starts.
+
+Arm_reloc_property_table *arm_reloc_property_table = NULL;
+
 // Instruction template class.  This class is similar to the insn_sequence
 // struct in bfd/elf32-arm.c.
 
@@ -607,7 +602,7 @@ class Reloc_stub : public Stub
     // Otherwise, this points a relobj.  We used the unsized and target
     // 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
+    // as most of the stub machinery is endianness-neutral.  However, it
     // may require a bit of casting done by users of this class.
     union
     {
@@ -873,8 +868,9 @@ class Stub_table : public Output_data
 {
  public:
   Stub_table(Arm_input_section<big_endian>* owner)
-    : Output_data(), owner_(owner), reloc_stubs_(), cortex_a8_stubs_(),
-      arm_v4bx_stubs_(0xf), prev_data_size_(0), prev_addralign_(1)
+    : Output_data(), owner_(owner), reloc_stubs_(), reloc_stubs_size_(0),
+      reloc_stubs_addralign_(1), cortex_a8_stubs_(), arm_v4bx_stubs_(0xf),
+      prev_data_size_(0), prev_addralign_(1)
   { }
 
   ~Stub_table()
@@ -907,6 +903,15 @@ class Stub_table : public Output_data
     const Stub_template* stub_template = stub->stub_template();
     gold_assert(stub_template->type() == key.stub_type());
     this->reloc_stubs_[key] = stub;
+
+    // Assign stub offset early.  We can do this because we never remove
+    // reloc stubs and they are in the beginning of the stub table.
+    uint64_t align = stub_template->alignment();
+    this->reloc_stubs_size_ = align_address(this->reloc_stubs_size_, align);
+    stub->set_offset(this->reloc_stubs_size_);
+    this->reloc_stubs_size_ += stub_template->size();
+    this->reloc_stubs_addralign_ =
+      std::max(this->reloc_stubs_addralign_, align);
   }
 
   // Add a Cortex-A8 STUB that fixes up a THUMB branch at ADDRESS.
@@ -1015,6 +1020,10 @@ class Stub_table : public Output_data
   Arm_input_section<big_endian>* owner_;
   // The relocation stubs.
   Reloc_stub_map reloc_stubs_;
+  // Size of reloc stubs.
+  off_t reloc_stubs_size_;
+  // Maximum address alignment of reloc stubs.
+  uint64_t reloc_stubs_addralign_;
   // The cortex_a8_stubs.
   Cortex_a8_stub_list cortex_a8_stubs_;
   // The Arm V4BX relocation stubs.
@@ -1025,6 +1034,97 @@ class Stub_table : public Output_data
   uint64_t prev_addralign_;
 };
 
+// Arm_exidx_cantunwind class.  This represents an EXIDX_CANTUNWIND entry
+// we add to the end of an EXIDX input section that goes into the output.
+
+class Arm_exidx_cantunwind : public Output_section_data
+{
+ public:
+  Arm_exidx_cantunwind(Relobj* relobj, unsigned int shndx)
+    : Output_section_data(8, 4, true), relobj_(relobj), shndx_(shndx)
+  { }
+
+  // Return the object containing the section pointed by this.
+  Relobj*
+  relobj() const
+  { return this->relobj_; }
+
+  // Return the section index of the section pointed by this.
+  unsigned int
+  shndx() const
+  { return this->shndx_; }
+
+ protected:
+  void
+  do_write(Output_file* of)
+  {
+    if (parameters->target().is_big_endian())
+      this->do_fixed_endian_write<true>(of);
+    else
+      this->do_fixed_endian_write<false>(of);
+  }
+
+ private:
+  // Implement do_write for a given endianness.
+  template<bool big_endian>
+  void inline
+  do_fixed_endian_write(Output_file*);
+  
+  // The object containing the section pointed by this.
+  Relobj* relobj_;
+  // The section index of the section pointed by this.
+  unsigned int shndx_;
+};
+
+// During EXIDX coverage fix-up, we compact an EXIDX section.  The
+// Offset map is used to map input section offset within the EXIDX section
+// to the output offset from the start of this EXIDX section. 
+
+typedef std::map<section_offset_type, section_offset_type>
+       Arm_exidx_section_offset_map;
+
+// Arm_exidx_merged_section class.  This represents an EXIDX input section
+// with some of its entries merged.
+
+class Arm_exidx_merged_section : public Output_relaxed_input_section
+{
+ public:
+  // Constructor for Arm_exidx_merged_section.
+  // EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
+  // SECTION_OFFSET_MAP points to a section offset map describing how
+  // parts of the input section are mapped to output.  DELETED_BYTES is
+  // the number of bytes deleted from the EXIDX input section.
+  Arm_exidx_merged_section(
+      const Arm_exidx_input_section& exidx_input_section,
+      const Arm_exidx_section_offset_map& section_offset_map,
+      uint32_t deleted_bytes);
+
+  // Return the original EXIDX input section.
+  const Arm_exidx_input_section&
+  exidx_input_section() const
+  { return this->exidx_input_section_; }
+
+  // Return the section offset map.
+  const Arm_exidx_section_offset_map&
+  section_offset_map() const
+  { return this->section_offset_map_; }
+
+ protected:
+  // Write merged section into file OF.
+  void
+  do_write(Output_file* of);
+
+  bool
+  do_output_offset(const Relobj*, unsigned int, section_offset_type,
+                 section_offset_type*) const;
+
+ private:
+  // Original EXIDX input section.
+  const Arm_exidx_input_section& exidx_input_section_;
+  // Section offset map.
+  const Arm_exidx_section_offset_map& section_offset_map_;
+};
+
 // A class to wrap an ordinary input section containing executable code.
 
 template<bool big_endian>
@@ -1120,6 +1220,93 @@ class Arm_input_section : public Output_relaxed_input_section
   Stub_table<big_endian>* stub_table_;
 };
 
+// Arm_exidx_fixup class.  This is used to define a number of methods
+// and keep states for fixing up EXIDX coverage.
+
+class Arm_exidx_fixup
+{
+ public:
+  Arm_exidx_fixup(Output_section* exidx_output_section,
+                 bool merge_exidx_entries = true)
+    : exidx_output_section_(exidx_output_section), last_unwind_type_(UT_NONE),
+      last_inlined_entry_(0), last_input_section_(NULL),
+      section_offset_map_(NULL), first_output_text_section_(NULL),
+      merge_exidx_entries_(merge_exidx_entries)
+  { }
+
+  ~Arm_exidx_fixup()
+  { delete this->section_offset_map_; }
+
+  // Process an EXIDX section for entry merging.  Return  number of bytes to
+  // be deleted in output.  If parts of the input EXIDX section are merged
+  // a heap allocated Arm_exidx_section_offset_map is store in the located
+  // PSECTION_OFFSET_MAP.  The caller owns the map and is reponsible for
+  // releasing it.
+  template<bool big_endian>
+  uint32_t
+  process_exidx_section(const Arm_exidx_input_section* exidx_input_section,
+                       Arm_exidx_section_offset_map** psection_offset_map);
+  
+  // Append an EXIDX_CANTUNWIND entry pointing at the end of the last
+  // input section, if there is not one already.
+  void
+  add_exidx_cantunwind_as_needed();
+
+  // Return the output section for the text section which is linked to the
+  // first exidx input in output.
+  Output_section*
+  first_output_text_section() const
+  { return this->first_output_text_section_; }
+
+ private:
+  // Copying is not allowed.
+  Arm_exidx_fixup(const Arm_exidx_fixup&);
+  Arm_exidx_fixup& operator=(const Arm_exidx_fixup&);
+
+  // Type of EXIDX unwind entry.
+  enum Unwind_type
+  {
+    // No type.
+    UT_NONE,
+    // EXIDX_CANTUNWIND.
+    UT_EXIDX_CANTUNWIND,
+    // Inlined entry.
+    UT_INLINED_ENTRY,
+    // Normal entry.
+    UT_NORMAL_ENTRY,
+  };
+
+  // Process an EXIDX entry.  We only care about the second word of the
+  // entry.  Return true if the entry can be deleted.
+  bool
+  process_exidx_entry(uint32_t second_word);
+
+  // Update the current section offset map during EXIDX section fix-up.
+  // If there is no map, create one.  INPUT_OFFSET is the offset of a
+  // reference point, DELETED_BYTES is the number of deleted by in the
+  // section so far.  If DELETE_ENTRY is true, the reference point and
+  // all offsets after the previous reference point are discarded.
+  void
+  update_offset_map(section_offset_type input_offset,
+                   section_size_type deleted_bytes, bool delete_entry);
+
+  // EXIDX output section.
+  Output_section* exidx_output_section_;
+  // Unwind type of the last EXIDX entry processed.
+  Unwind_type last_unwind_type_;
+  // Last seen inlined EXIDX entry.
+  uint32_t last_inlined_entry_;
+  // Last processed EXIDX input section.
+  const Arm_exidx_input_section* last_input_section_;
+  // Section offset map created in process_exidx_section.
+  Arm_exidx_section_offset_map* section_offset_map_;
+  // Output section for the text section which is linked to the first exidx
+  // input in output.
+  Output_section* first_output_text_section_;
+
+  bool merge_exidx_entries_;
+};
+
 // Arm output section class.  This is defined mainly to add a number of
 // stub generation methods.
 
@@ -1127,6 +1314,8 @@ template<bool big_endian>
 class Arm_output_section : public Output_section
 {
  public:
+  typedef std::vector<std::pair<Relobj*, unsigned int> > Text_section_list;
+
   Arm_output_section(const char* name, elfcpp::Elf_Word type,
                     elfcpp::Elf_Xword flags)
     : Output_section(name, type, flags)
@@ -1145,6 +1334,19 @@ class Arm_output_section : public Output_section
   as_arm_output_section(Output_section* os)
   { return static_cast<Arm_output_section<big_endian>*>(os); }
 
+  // Append all input text sections in this into LIST.
+  void
+  append_text_sections_to_list(Text_section_list* list);
+
+  // Fix EXIDX coverage of this EXIDX output section.  SORTED_TEXT_SECTION
+  // is a list of text input sections sorted in ascending order of their
+  // output addresses.
+  void
+  fix_exidx_coverage(Layout* layout,
+                    const Text_section_list& sorted_text_section,
+                    Symbol_table* symtab,
+                    bool merge_exidx_entries);
+
  private:
   // For convenience.
   typedef Output_section::Input_section Input_section;
@@ -1158,6 +1360,63 @@ class Arm_output_section : public Output_section
                         std::vector<Output_relaxed_input_section*>*);
 };
 
+// Arm_exidx_input_section class.  This represents an EXIDX input section.
+
+class Arm_exidx_input_section
+{
+ public:
+  static const section_offset_type invalid_offset =
+    static_cast<section_offset_type>(-1);
+
+  Arm_exidx_input_section(Relobj* relobj, unsigned int shndx,
+                         unsigned int link, uint32_t size, uint32_t addralign)
+    : relobj_(relobj), shndx_(shndx), link_(link), size_(size),
+      addralign_(addralign)
+  { }
+
+  ~Arm_exidx_input_section()
+  { }
+       
+  // Accessors:  This is a read-only class.
+
+  // Return the object containing this EXIDX input section.
+  Relobj*
+  relobj() const
+  { return this->relobj_; }
+
+  // Return the section index of this EXIDX input section.
+  unsigned int
+  shndx() const
+  { return this->shndx_; }
+
+  // Return the section index of linked text section in the same object.
+  unsigned int
+  link() const
+  { return this->link_; }
+
+  // Return size of the EXIDX input section.
+  uint32_t
+  size() const
+  { return this->size_; }
+
+  // Reutnr address alignment of EXIDX input section.
+  uint32_t
+  addralign() const
+  { return this->addralign_; }
+
+ private:
+  // Object containing this.
+  Relobj* relobj_;
+  // Section index of this.
+  unsigned int shndx_;
+  // text section linked to this in the same object.
+  unsigned int link_;
+  // Size of this.  For ARM 32-bit is sufficient.
+  uint32_t size_;
+  // Address alignment of this.  For ARM 32-bit is sufficient.
+  uint32_t addralign_;
+};
+
 // Arm_relobj class.
 
 template<bool big_endian>
@@ -1171,7 +1430,9 @@ class Arm_relobj : public Sized_relobj<32, big_endian>
     : Sized_relobj<32, big_endian>(name, input_file, offset, ehdr),
       stub_tables_(), local_symbol_is_thumb_function_(),
       attributes_section_data_(NULL), mapping_symbols_info_(),
-      section_has_cortex_a8_workaround_(NULL)
+      section_has_cortex_a8_workaround_(NULL), exidx_section_map_(),
+      output_local_symbol_count_needs_update_(false),
+      merge_flags_and_attributes_(true)
   { }
 
   ~Arm_relobj()
@@ -1213,7 +1474,7 @@ class Arm_relobj : public Sized_relobj<32, big_endian>
   {
     // The stubs have relocations and we need to process them after writing
     // out the stubs.  So relocation now must follow section write.
-    this->invalidate_section_offset(shndx);
+    this->set_section_offset(shndx, -1ULL);
     this->set_relocs_must_follow_section_writes();
   }
 
@@ -1273,6 +1534,48 @@ class Arm_relobj : public Sized_relobj<32, big_endian>
     (*this->section_has_cortex_a8_workaround_)[shndx] = true;
   }
 
+  // Return the EXIDX section of an text section with index SHNDX or NULL
+  // if the text section has no associated EXIDX section.
+  const Arm_exidx_input_section*
+  exidx_input_section_by_link(unsigned int shndx) const
+  {
+    Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
+    return ((p != this->exidx_section_map_.end()
+            && p->second->link() == shndx)
+           ? p->second
+           : NULL);
+  }
+
+  // Return the EXIDX section with index SHNDX or NULL if there is none.
+  const Arm_exidx_input_section*
+  exidx_input_section_by_shndx(unsigned shndx) const
+  {
+    Exidx_section_map::const_iterator p = this->exidx_section_map_.find(shndx);
+    return ((p != this->exidx_section_map_.end()
+            && p->second->shndx() == shndx)
+           ? p->second
+           : NULL);
+  }
+
+  // Whether output local symbol count needs updating.
+  bool
+  output_local_symbol_count_needs_update() const
+  { return this->output_local_symbol_count_needs_update_; }
+
+  // Set output_local_symbol_count_needs_update flag to be true.
+  void
+  set_output_local_symbol_count_needs_update()
+  { this->output_local_symbol_count_needs_update_ = true; }
+  
+  // Update output local symbol count at the end of relaxation.
+  void
+  update_output_local_symbol_count();
+
+  // Whether we want to merge processor-specific flags and attributes.
+  bool
+  merge_flags_and_attributes() const
+  { return this->merge_flags_and_attributes_; }
+  
  protected:
   // Post constructor setup.
   void
@@ -1310,7 +1613,12 @@ class Arm_relobj : public Sized_relobj<32, big_endian>
   bool
   section_needs_reloc_stub_scanning(const elfcpp::Shdr<32, big_endian>&,
                                    const Relobj::Output_sections&,
-                                   const Symbol_table *);
+                                   const Symbol_table *, const unsigned char*);
+
+  // Whether a section is a scannable text section.
+  bool
+  section_is_scannable(const elfcpp::Shdr<32, big_endian>&, unsigned int,
+                      const Output_section*, const Symbol_table *);
 
   // Whether a section needs to be scanned for the Cortex-A8 erratum.
   bool
@@ -1324,8 +1632,34 @@ class Arm_relobj : public Sized_relobj<32, big_endian>
                                     unsigned int, Output_section*,
                                     Target_arm<big_endian>*);
 
-  // List of stub tables.
+  // Find the linked text section of an EXIDX section by looking at the
+  // first reloction of the EXIDX section.  PSHDR points to the section
+  // headers of a relocation section and PSYMS points to the local symbols.
+  // PSHNDX points to a location storing the text section index if found.
+  // Return whether we can find the linked section.
+  bool
+  find_linked_text_section(const unsigned char* pshdr,
+                          const unsigned char* psyms, unsigned int* pshndx);
+
+  //
+  // Make a new Arm_exidx_input_section object for EXIDX section with
+  // index SHNDX and section header SHDR.  TEXT_SHNDX is the section
+  // index of the linked text section.
+  void
+  make_exidx_input_section(unsigned int shndx,
+                          const elfcpp::Shdr<32, big_endian>& shdr,
+                          unsigned int text_shndx);
+
+  // Return the output address of either a plain input section or a
+  // relaxed input section.  SHNDX is the section index.
+  Arm_address
+  simple_input_section_output_address(unsigned int, Output_section*);
+
   typedef std::vector<Stub_table<big_endian>*> Stub_table_list;
+  typedef Unordered_map<unsigned int, const Arm_exidx_input_section*>
+    Exidx_section_map;
+
+  // List of stub tables.
   Stub_table_list stub_tables_;
   // Bit vector to tell if a local symbol is a thumb function or not.
   // This is only valid after do_count_local_symbol is called.
@@ -1338,6 +1672,13 @@ class Arm_relobj : public Sized_relobj<32, big_endian>
   Mapping_symbols_info mapping_symbols_info_;
   // Bitmap to indicate sections with Cortex-A8 workaround or NULL.
   std::vector<bool>* section_has_cortex_a8_workaround_;
+  // Map a text section to its associated .ARM.exidx section, if there is one.
+  Exidx_section_map exidx_section_map_;
+  // Whether output local symbol count needs updating.
+  bool output_local_symbol_count_needs_update_;
+  // Whether we merge processor flags and attributes of this object to
+  // output.
+  bool merge_flags_and_attributes_;
 };
 
 // Arm_dynobj class.
@@ -1472,6 +1813,144 @@ class Cortex_a8_reloc
   Arm_address destination_;
 };
 
+// Arm_output_data_got class.  We derive this from Output_data_got to add
+// extra methods to handle TLS relocations in a static link.
+
+template<bool big_endian>
+class Arm_output_data_got : public Output_data_got<32, big_endian>
+{
+ public:
+  Arm_output_data_got(Symbol_table* symtab, Layout* layout)
+    : Output_data_got<32, big_endian>(), symbol_table_(symtab), layout_(layout)
+  { }
+
+  // Add a static entry for the GOT entry at OFFSET.  GSYM is a global
+  // symbol and R_TYPE is the code of a dynamic relocation that needs to be
+  // applied in a static link.
+  void
+  add_static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
+  { this->static_relocs_.push_back(Static_reloc(got_offset, r_type, gsym)); }
+
+  // Add a static reloc for the GOT entry at OFFSET.  RELOBJ is an object
+  // defining a local symbol with INDEX.  R_TYPE is the code of a dynamic
+  // relocation that needs to be applied in a static link.
+  void
+  add_static_reloc(unsigned int got_offset, unsigned int r_type,
+                  Sized_relobj<32, big_endian>* relobj, unsigned int index)
+  {
+    this->static_relocs_.push_back(Static_reloc(got_offset, r_type, relobj,
+                                               index));
+  }
+
+  // Add a GOT pair for R_ARM_TLS_GD32.  The creates a pair of GOT entries.
+  // The first one is initialized to be 1, which is the module index for
+  // the main executable and the second one 0.  A reloc of the type
+  // R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
+  // be applied by gold.  GSYM is a global symbol.
+  void
+  add_tls_gd32_with_static_reloc(unsigned int got_type, Symbol* gsym);
+
+  // Same as the above but for a local symbol in OBJECT with INDEX.
+  void
+  add_tls_gd32_with_static_reloc(unsigned int got_type,
+                                Sized_relobj<32, big_endian>* object,
+                                unsigned int index);
+
+ protected:
+  // Write out the GOT table.
+  void
+  do_write(Output_file*);
+
+ private:
+  // This class represent dynamic relocations that need to be applied by
+  // gold because we are using TLS relocations in a static link.
+  class Static_reloc
+  {
+   public:
+    Static_reloc(unsigned int got_offset, unsigned int r_type, Symbol* gsym)
+      : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(true)
+    { this->u_.global.symbol = gsym; }
+
+    Static_reloc(unsigned int got_offset, unsigned int r_type,
+         Sized_relobj<32, big_endian>* relobj, unsigned int index)
+      : got_offset_(got_offset), r_type_(r_type), symbol_is_global_(false)
+    {
+      this->u_.local.relobj = relobj;
+      this->u_.local.index = index;
+    }
+
+    // Return the GOT offset.
+    unsigned int
+    got_offset() const
+    { return this->got_offset_; }
+
+    // Relocation type.
+    unsigned int
+    r_type() const
+    { return this->r_type_; }
+
+    // Whether the symbol is global or not.
+    bool
+    symbol_is_global() const
+    { return this->symbol_is_global_; }
+
+    // For a relocation against a global symbol, the global symbol.
+    Symbol*
+    symbol() const
+    {
+      gold_assert(this->symbol_is_global_);
+      return this->u_.global.symbol;
+    }
+
+    // For a relocation against a local symbol, the defining object.
+    Sized_relobj<32, big_endian>*
+    relobj() const
+    {
+      gold_assert(!this->symbol_is_global_);
+      return this->u_.local.relobj;
+    }
+
+    // For a relocation against a local symbol, the local symbol index.
+    unsigned int
+    index() const
+    {
+      gold_assert(!this->symbol_is_global_);
+      return this->u_.local.index;
+    }
+
+   private:
+    // GOT offset of the entry to which this relocation is applied.
+    unsigned int got_offset_;
+    // Type of relocation.
+    unsigned int r_type_;
+    // Whether this relocation is against a global symbol.
+    bool symbol_is_global_;
+    // A global or local symbol.
+    union
+    {
+      struct
+      {
+       // For a global symbol, the symbol itself.
+       Symbol* symbol;
+      } global;
+      struct
+      {
+       // For a local symbol, the object defining object.
+       Sized_relobj<32, big_endian>* relobj;
+       // For a local symbol, the symbol index.
+       unsigned int index;
+      } local;
+    } u_;
+  };
+
+  // Symbol table of the output object.
+  Symbol_table* symbol_table_;
+  // Layout of the output object.
+  Layout* layout_;
+  // Static relocs to be applied to the GOT.
+  std::vector<Static_reloc> static_relocs_;
+};
+
 // Utilities for manipulating integers of up to 32-bits
 
 namespace utils
@@ -1544,11 +2023,12 @@ class Target_arm : public Sized_target<32, big_endian>
   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), stub_tables_(),
-      stub_factory_(Stub_factory::get_instance()), may_use_blx_(false),
-      should_force_pic_veneer_(false), arm_input_section_map_(),
-      attributes_section_data_(NULL), fix_cortex_a8_(false),
-      cortex_a8_relocs_info_(), fix_v4bx_(0)
+      copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL), 
+      got_mod_index_offset_(-1U), tls_base_symbol_defined_(false),
+      stub_tables_(), stub_factory_(Stub_factory::get_instance()),
+      may_use_blx_(false), should_force_pic_veneer_(false),
+      arm_input_section_map_(), attributes_section_data_(NULL),
+      fix_cortex_a8_(false), cortex_a8_relocs_info_()
   { }
 
   // Whether we can use BLX.
@@ -1587,6 +2067,10 @@ class Target_arm : public Sized_target<32, big_endian>
   {
     Object_attribute* attr =
       this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
+
+    if (attr->int_value() == elfcpp::TAG_CPU_ARCH_V6_M
+       || attr->int_value() == elfcpp::TAG_CPU_ARCH_V6S_M)
+      return true;
     if (attr->int_value() != elfcpp::TAG_CPU_ARCH_V7
        && attr->int_value() != elfcpp::TAG_CPU_ARCH_V7E_M)
       return false;
@@ -1705,6 +2189,11 @@ class Target_arm : public Sized_target<32, big_endian>
   do_is_defined_by_abi(Symbol* sym) const
   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
 
+  // Return whether there is a GOT section.
+  bool
+  has_got_section() const
+  { return this->got_ != NULL; }
+
   // Return the size of the GOT section.
   section_size_type
   got_size()
@@ -1762,10 +2251,6 @@ class Target_arm : public Sized_target<32, 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);
-
   // Whether NAME belongs to a mapping symbol.
   static bool
   is_mapping_symbol_name(const char* name)
@@ -1781,13 +2266,18 @@ class Target_arm : public Sized_target<32, big_endian>
   fix_cortex_a8() const
   { return this->fix_cortex_a8_; }
 
+  // Whether we merge exidx entries in debuginfo.
+  bool
+  merge_exidx_entries() const
+  { return parameters->options().merge_exidx_entries(); }
+
   // Whether we fix R_ARM_V4BX relocation.
   // 0 - do not fix
   // 1 - replace with MOV instruction (armv4 target)
   // 2 - make interworking veneer (>= armv4t targets only)
-  int
+  General_options::Fix_v4bx
   fix_v4bx() const
-  { return this->fix_v4bx_; }
+  { return parameters->options().fix_v4bx(); }
 
   // Scan a span of THUMB code section for Cortex-A8 erratum.
   void
@@ -1848,6 +2338,17 @@ class Target_arm : public Sized_target<32, big_endian>
   int
   do_attributes_order(int num) const;
 
+  // This is called when the target is selected as the default.
+  void
+  do_select_as_default_target()
+  {
+    // No locking is required since there should only be one default target.
+    // We cannot have both the big-endian and little-endian ARM targets
+    // as the default.
+    gold_assert(arm_reloc_property_table == NULL);
+    arm_reloc_property_table = new Arm_reloc_property_table();
+  }
+
  private:
   // The class which scans relocations.
   class Scan
@@ -1873,6 +2374,25 @@ class Target_arm : public Sized_target<32, big_endian>
           const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
           Symbol* gsym);
 
+    inline bool
+    local_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
+                                       Sized_relobj<32, big_endian>* ,
+                                       unsigned int ,
+                                       Output_section* ,
+                                       const elfcpp::Rel<32, big_endian>& ,
+                                       unsigned int ,
+                                       const elfcpp::Sym<32, big_endian>&)
+    { return false; }
+
+    inline bool
+    global_reloc_may_be_function_pointer(Symbol_table* , Layout* , Target_arm* ,
+                                        Sized_relobj<32, big_endian>* ,
+                                        unsigned int ,
+                                        Output_section* ,
+                                        const elfcpp::Rel<32, big_endian>& ,
+                                        unsigned int , Symbol*)
+    { return false; }
+
    private:
     static void
     unsupported_reloc_local(Sized_relobj<32, big_endian>*,
@@ -1961,12 +2481,24 @@ class Target_arm : public Sized_target<32, big_endian>
        case elfcpp::R_ARM_THM_JUMP19:
        case elfcpp::R_ARM_PLT32:
        case elfcpp::R_ARM_THM_XPC22:
+       case elfcpp::R_ARM_PREL31:
+       case elfcpp::R_ARM_SBREL31:
          return false;
 
        default:
          return true;
        }
     }
+
+   private:
+    // Do a TLS relocation.
+    inline typename Arm_relocate_functions<big_endian>::Status
+    relocate_tls(const Relocate_info<32, big_endian>*, Target_arm<big_endian>*,
+                 size_t, const elfcpp::Rel<32, big_endian>&, unsigned int,
+                const Sized_symbol<32>*, const Symbol_value<32>*,
+                unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
+                section_size_type);
+
   };
 
   // A class which returns the size required for a relocation type,
@@ -1978,8 +2510,13 @@ class Target_arm : public Sized_target<32, big_endian>
     get_size_for_reloc(unsigned int, Relobj*);
   };
 
+  // Adjust TLS relocation type based on the options and whether this
+  // is a local symbol.
+  static tls::Tls_optimization
+  optimize_tls_reloc(bool is_final, int r_type);
+
   // Get the GOT section, creating it if necessary.
-  Output_data_got<32, big_endian>*
+  Arm_output_data_got<big_endian>*
   got_section(Symbol_table*, Layout*);
 
   // Get the GOT PLT section.
@@ -1994,6 +2531,15 @@ class Target_arm : public Sized_target<32, big_endian>
   void
   make_plt_entry(Symbol_table*, Layout*, Symbol*);
 
+  // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
+  void
+  define_tls_base_symbol(Symbol_table*, Layout*);
+
+  // Create a GOT entry for the TLS module index.
+  unsigned int
+  got_mod_index_entry(Symbol_table* symtab, Layout* layout,
+                     Sized_relobj<32, big_endian>* object);
+
   // Get the PLT section.
   const Output_data_plt_arm<big_endian>*
   plt_section() const
@@ -2006,6 +2552,10 @@ class Target_arm : public Sized_target<32, big_endian>
   Reloc_section*
   rel_dyn_section(Layout*);
 
+  // Get the section to use for TLS_DESC relocations.
+  Reloc_section*
+  rel_tls_desc_section(Layout*) const;
+
   // Return true if the symbol may need a COPY relocation.
   // References from an executable object to non-function symbols
   // defined in a dynamic object may need a COPY relocation.
@@ -2101,6 +2651,18 @@ class Target_arm : public Sized_target<32, big_endian>
       elfcpp::Elf_types<32>::Elf_Addr view_address,
       section_size_type);
 
+  // Fix .ARM.exidx section coverage.
+  void
+  fix_exidx_coverage(Layout*, Arm_output_section<big_endian>*, Symbol_table*);
+
+  // Functors for STL set.
+  struct output_section_address_less_than
+  {
+    bool
+    operator()(const Output_section* s1, const Output_section* s2) const
+    { return s1->address() < s2->address(); }
+  };
+
   // Information about this specific target which we pass to the
   // general Target structure.
   static const Target::Target_info arm_info;
@@ -2108,16 +2670,19 @@ class Target_arm : public Sized_target<32, big_endian>
   // The types of GOT entries needed for this platform.
   enum Got_type
   {
-    GOT_TYPE_STANDARD = 0      // GOT entry for a regular symbol
+    GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
+    GOT_TYPE_TLS_NOFFSET = 1,   // GOT entry for negative TLS offset
+    GOT_TYPE_TLS_OFFSET = 2,    // GOT entry for positive TLS offset
+    GOT_TYPE_TLS_PAIR = 3,      // GOT entry for TLS module/offset pair
+    GOT_TYPE_TLS_DESC = 4       // GOT entry for TLS_DESC pair
   };
 
   typedef typename std::vector<Stub_table<big_endian>*> Stub_table_list;
 
   // Map input section to Arm_input_section.
-  typedef Unordered_map<Input_section_specifier,
+  typedef Unordered_map<Section_id,
                        Arm_input_section<big_endian>*,
-                       Input_section_specifier::hash,
-                       Input_section_specifier::equal_to>
+                       Section_id_hash>
          Arm_input_section_map;
     
   // Map output addresses to relocs for Cortex-A8 erratum.
@@ -2125,7 +2690,7 @@ class Target_arm : public Sized_target<32, big_endian>
          Cortex_a8_relocs_info;
 
   // The GOT section.
-  Output_data_got<32, big_endian>* got_;
+  Arm_output_data_got<big_endian>* got_;
   // The PLT section.
   Output_data_plt_arm<big_endian>* plt_;
   // The GOT PLT section.
@@ -2136,6 +2701,10 @@ 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_;
+  // Offset of the GOT entry for the TLS module index.
+  unsigned int got_mod_index_offset_;
+  // True if the _TLS_MODULE_BASE_ symbol has been defined.
+  bool tls_base_symbol_defined_;
   // Vector of Stub_tables created.
   Stub_table_list stub_tables_;
   // Stub factory.
@@ -2152,8 +2721,6 @@ class Target_arm : public Sized_target<32, big_endian>
   bool fix_cortex_a8_;
   // Map addresses to relocs for Cortex-A8 erratum.
   Cortex_a8_relocs_info cortex_a8_relocs_info_;
-  // Whether we need to fix code for V4BX relocations.
-  int fix_v4bx_;
 };
 
 template<bool big_endian>
@@ -2270,6 +2837,67 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     return val;
   }
 
+  // Calculate the smallest constant Kn for the specified residual.
+  // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
+  static uint32_t
+  calc_grp_kn(typename elfcpp::Swap<32, big_endian>::Valtype residual)
+  {
+    int32_t msb;
+
+    if (residual == 0)
+      return 0;
+    // Determine the most significant bit in the residual and
+    // align the resulting value to a 2-bit boundary.
+    for (msb = 30; (msb >= 0) && !(residual & (3 << msb)); msb -= 2)
+      ;
+    // The desired shift is now (msb - 6), or zero, whichever
+    // is the greater.
+    return (((msb - 6) < 0) ? 0 : (msb - 6));
+  }
+
+  // Calculate the final residual for the specified group index.
+  // If the passed group index is less than zero, the method will return
+  // the value of the specified residual without any change.
+  // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
+  static typename elfcpp::Swap<32, big_endian>::Valtype
+  calc_grp_residual(typename elfcpp::Swap<32, big_endian>::Valtype residual,
+                   const int group)
+  {
+    for (int n = 0; n <= group; n++)
+      {
+       // Calculate which part of the value to mask.
+       uint32_t shift = calc_grp_kn(residual);
+       // Calculate the residual for the next time around.
+       residual &= ~(residual & (0xff << shift));
+      }
+
+    return residual;
+  }
+
+  // Calculate the value of Gn for the specified group index.
+  // We return it in the form of an encoded constant-and-rotation.
+  // (see (AAELF 4.6.1.4 Static ARM relocations, Group Relocations, p.32)
+  static typename elfcpp::Swap<32, big_endian>::Valtype
+  calc_grp_gn(typename elfcpp::Swap<32, big_endian>::Valtype residual,
+             const int group)
+  {
+    typename elfcpp::Swap<32, big_endian>::Valtype gn = 0;
+    uint32_t shift = 0;
+
+    for (int n = 0; n <= group; n++)
+      {
+       // Calculate which part of the value to mask.
+       shift = calc_grp_kn(residual);
+       // Calculate Gn in 32-bit as well as encoded constant-and-rotation form.
+       gn = residual & (0xff << shift);
+       // Calculate the residual for the next time around.
+       residual &= ~gn;
+      }
+    // Return Gn in the form of an encoded constant-and-rotation.
+    return ((gn >> shift) | ((gn <= 0xff ? 0 : (32 - shift) / 2) << 8));
+  }
+
+ public:
   // Handle ARM long branches.
   static typename This::Status
   arm_branch_common(unsigned int, const Relocate_info<32, big_endian>*,
@@ -2284,7 +2912,6 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
                      const Arm_relobj<big_endian>*, unsigned int,
                      const Symbol_value<32>*, Arm_address, Arm_address, bool);
 
- public:
 
   // Return the branch offset of a 32-bit THUMB branch.
   static inline int32_t
@@ -2381,7 +3008,10 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     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)
+
+    // R_ARM_ABS8 permits signed or unsigned results.
+    int signed_x = static_cast<int32_t>(x);
+    return ((signed_x < -128 || signed_x > 255)
            ? This::STATUS_OVERFLOW
            : This::STATUS_OKAY);
   }
@@ -2400,7 +3030,10 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     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)
+
+    // R_ARM_ABS16 permits signed or unsigned results.
+    int signed_x = static_cast<int32_t>(x);
+    return ((signed_x < -32768 || signed_x > 65535)
            ? This::STATUS_OVERFLOW
            : This::STATUS_OKAY);
   }
@@ -2474,51 +3107,12 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     return This::STATUS_OKAY;
   }
 
-  // R_ARM_THM_CALL: (S + A) | T - P
-  static inline typename This::Status
-  thm_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,
-          bool is_weakly_undefined_without_plt)
-  {
-    return thumb_branch_common(elfcpp::R_ARM_THM_CALL, relinfo, view, gsym,
-                              object, r_sym, psymval, address, thumb_bit,
-                              is_weakly_undefined_without_plt);
-  }
-
-  // R_ARM_THM_JUMP24: (S + A) | T - P
-  static inline typename This::Status
-  thm_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,
-            bool is_weakly_undefined_without_plt)
-  {
-    return thumb_branch_common(elfcpp::R_ARM_THM_JUMP24, relinfo, view, gsym,
-                              object, r_sym, psymval, address, thumb_bit,
-                              is_weakly_undefined_without_plt);
-  }
-
   // R_ARM_THM_JUMP24: (S + A) | T - P
   static typename This::Status
   thm_jump19(unsigned char *view, const Arm_relobj<big_endian>* object,
             const Symbol_value<32>* psymval, Arm_address address,
             Arm_address thumb_bit);
 
-  // R_ARM_THM_XPC22: (S + A) | T - P
-  static inline typename This::Status
-  thm_xpc22(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 thumb_branch_common(elfcpp::R_ARM_THM_XPC22, relinfo, view, gsym,
-                              object, r_sym, psymval, address, thumb_bit,
-                              is_weakly_undefined_without_plt);
-  }
-
   // R_ARM_THM_JUMP6: S + A – P
   static inline typename This::Status
   thm_jump6(unsigned char *view,
@@ -2617,74 +3211,6 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     return This::STATUS_OKAY;
   }
 
-  // R_ARM_PLT32: (S + A) | T - P
-  static inline typename This::Status
-  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,
-       Arm_address thumb_bit,
-       bool is_weakly_undefined_without_plt)
-  {
-    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(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_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(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_JUMP24, relinfo, view, gsym,
-                            object, r_sym, psymval, address, thumb_bit,
-                            is_weakly_undefined_without_plt);
-  }
-
   // R_ARM_PREL: (S + A) | T - P
   static inline typename This::Status
   prel31(unsigned char *view,
@@ -2704,152 +3230,208 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
            This::STATUS_OVERFLOW : This::STATUS_OKAY);
   }
 
-  // R_ARM_MOVW_ABS_NC: (S + A) | T
-  static inline typename This::Status 
-  movw_abs_nc(unsigned char *view,
-             const Sized_relobj<32, big_endian>* object,
-             const Symbol_value<32>* psymval,
-             Arm_address thumb_bit)
+  // R_ARM_MOVW_ABS_NC: (S + A) | T    (relative address base is )
+  // R_ARM_MOVW_PREL_NC: (S + A) | T - P
+  // R_ARM_MOVW_BREL_NC: ((S + A) | T) - B(S)
+  // R_ARM_MOVW_BREL: ((S + A) | T) - B(S)
+  static inline typename This::Status
+  movw(unsigned char* view,
+       const Sized_relobj<32, big_endian>* object,
+       const Symbol_value<32>* psymval,
+       Arm_address relative_address_base,
+       Arm_address thumb_bit,
+       bool check_overflow)
   {
     typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
     Valtype* wv = reinterpret_cast<Valtype*>(view);
     Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
-    Valtype addend =  This::extract_arm_movw_movt_addend(val);
-    Valtype x = psymval->value(object, addend) | thumb_bit;
+    Valtype addend = This::extract_arm_movw_movt_addend(val);
+    Valtype x = ((psymval->value(object, addend) | thumb_bit)
+                - relative_address_base);
     val = This::insert_val_arm_movw_movt(val, x);
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
-    return This::STATUS_OKAY;
+    return ((check_overflow && utils::has_overflow<16>(x))
+           ? This::STATUS_OVERFLOW
+           : This::STATUS_OKAY);
   }
 
-  // R_ARM_MOVT_ABS: S + A
+  // R_ARM_MOVT_ABS: S + A     (relative address base is 0)
+  // R_ARM_MOVT_PREL: S + A - P
+  // R_ARM_MOVT_BREL: S + A - B(S)
   static inline typename This::Status
-  movt_abs(unsigned char *view,
-          const Sized_relobj<32, big_endian>* object,
-           const Symbol_value<32>* psymval)
+  movt(unsigned char* view,
+       const Sized_relobj<32, big_endian>* object,
+       const Symbol_value<32>* psymval,
+       Arm_address relative_address_base)
   {
     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 = psymval->value(object, addend) >> 16;
+    Valtype x = (psymval->value(object, addend) - relative_address_base) >> 16;
     val = This::insert_val_arm_movw_movt(val, x);
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
+    // FIXME: IHI0044D says that we should check for overflow.
     return This::STATUS_OKAY;
   }
 
-  //  R_ARM_THM_MOVW_ABS_NC: S + A | T
-  static inline typename This::Status 
-  thm_movw_abs_nc(unsigned char *view,
-                 const Sized_relobj<32, big_endian>* object,
-                 const Symbol_value<32>* psymval,
-                 Arm_address thumb_bit)
+  // R_ARM_THM_MOVW_ABS_NC: S + A | T          (relative_address_base is 0)
+  // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
+  // R_ARM_THM_MOVW_BREL_NC: ((S + A) | T) - B(S)
+  // R_ARM_THM_MOVW_BREL: ((S + A) | T) - B(S)
+  static inline typename This::Status
+  thm_movw(unsigned char *view,
+          const Sized_relobj<32, big_endian>* object,
+          const Symbol_value<32>* psymval,
+          Arm_address relative_address_base,
+          Arm_address thumb_bit,
+          bool check_overflow)
   {
     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
     Valtype* wv = reinterpret_cast<Valtype*>(view);
-    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 = psymval->value(object, addend) | thumb_bit;
+    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 =
+      (psymval->value(object, addend) | thumb_bit) - relative_address_base;
     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);
-    return This::STATUS_OKAY;
+    return ((check_overflow && utils::has_overflow<16>(x))
+           ? This::STATUS_OVERFLOW
+           : This::STATUS_OKAY);
   }
 
-  //  R_ARM_THM_MOVT_ABS: S + A
-  static inline typename This::Status 
-  thm_movt_abs(unsigned char *view,
-              const Sized_relobj<32, big_endian>* object,
-              const Symbol_value<32>* psymval)
+  // R_ARM_THM_MOVT_ABS: S + A         (relative address base is 0)
+  // R_ARM_THM_MOVT_PREL: S + A - P
+  // R_ARM_THM_MOVT_BREL: S + A - B(S)
+  static inline typename This::Status
+  thm_movt(unsigned char* view,
+          const Sized_relobj<32, big_endian>* object,
+          const Symbol_value<32>* psymval,
+          Arm_address relative_address_base)
   {
     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
     Valtype* wv = reinterpret_cast<Valtype*>(view);
-    Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
-                  | elfcpp::Swap<16, big_endian>::readval(wv + 1));
+    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 = psymval->value(object, addend) >> 16;
+    Reltype x = (psymval->value(object, addend) - relative_address_base) >> 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);
     return This::STATUS_OKAY;
   }
 
-  // R_ARM_MOVW_PREL_NC: (S + A) | T - P
-  static inline typename This::Status
-  movw_prel_nc(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);
-    Valtype addend = This::extract_arm_movw_movt_addend(val);
-    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;
-  }
-
-  // R_ARM_MOVT_PREL: S + A - P
+  // R_ARM_THM_ALU_PREL_11_0: ((S + A) | T) - Pa (Thumb32)
   static inline typename This::Status
-  movt_prel(unsigned char *view,
+  thm_alu11(unsigned char* view,
            const Sized_relobj<32, big_endian>* object,
            const Symbol_value<32>* psymval,
-            Arm_address address)
+           Arm_address address,
+           Arm_address thumb_bit)
   {
-    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+    typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
+    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
     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 = (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;
+    Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
+                  | elfcpp::Swap<16, big_endian>::readval(wv + 1);
+
+    //       f e d c b|a|9|8 7 6 5|4|3 2 1 0||f|e d c|b a 9 8|7 6 5 4 3 2 1 0
+    // -----------------------------------------------------------------------
+    // ADD{S} 1 1 1 1 0|i|0|1 0 0 0|S|1 1 0 1||0|imm3 |Rd     |imm8
+    // ADDW   1 1 1 1 0|i|1|0 0 0 0|0|1 1 0 1||0|imm3 |Rd     |imm8
+    // ADR[+] 1 1 1 1 0|i|1|0 0 0 0|0|1 1 1 1||0|imm3 |Rd     |imm8
+    // SUB{S} 1 1 1 1 0|i|0|1 1 0 1|S|1 1 0 1||0|imm3 |Rd     |imm8
+    // SUBW   1 1 1 1 0|i|1|0 1 0 1|0|1 1 0 1||0|imm3 |Rd     |imm8
+    // ADR[-] 1 1 1 1 0|i|1|0 1 0 1|0|1 1 1 1||0|imm3 |Rd     |imm8
+
+    // Determine a sign for the addend.
+    const int sign = ((insn & 0xf8ef0000) == 0xf0ad0000
+                     || (insn & 0xf8ef0000) == 0xf0af0000) ? -1 : 1;
+    // Thumb2 addend encoding:
+    // imm12 := i | imm3 | imm8
+    int32_t addend = (insn & 0xff)
+                    | ((insn & 0x00007000) >> 4)
+                    | ((insn & 0x04000000) >> 15);
+    // Apply a sign to the added.
+    addend *= sign;
+
+    int32_t x = (psymval->value(object, addend) | thumb_bit)
+               - (address & 0xfffffffc);
+    Reltype val = abs(x);
+    // Mask out the value and a distinct part of the ADD/SUB opcode
+    // (bits 7:5 of opword).
+    insn = (insn & 0xfb0f8f00)
+          | (val & 0xff)
+          | ((val & 0x700) << 4)
+          | ((val & 0x800) << 15);
+    // Set the opcode according to whether the value to go in the
+    // place is negative.
+    if (x < 0)
+      insn |= 0x00a00000;
+
+    elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
+    elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
+    return ((val > 0xfff) ?
+           This::STATUS_OVERFLOW : This::STATUS_OKAY);
   }
 
-  // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
+  // R_ARM_THM_PC8: S + A - Pa (Thumb)
   static inline typename This::Status
-  thm_movw_prel_nc(unsigned char *view,
-                  const Sized_relobj<32, big_endian>* object,
-                  const Symbol_value<32>* psymval,
-                  Arm_address address,
-                  Arm_address thumb_bit)
+  thm_pc8(unsigned char* view,
+         const Sized_relobj<32, big_endian>* object,
+         const Symbol_value<32>* psymval,
+         Arm_address address)
   {
     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
-    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
+    typedef typename elfcpp::Swap<16, big_endian>::Valtype Reltype;
     Valtype* wv = reinterpret_cast<Valtype*>(view);
-    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 = (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);
-    return This::STATUS_OKAY;
+    Valtype insn = elfcpp::Swap<16, big_endian>::readval(wv);
+    Reltype addend = ((insn & 0x00ff) << 2);
+    int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
+    Reltype val = abs(x);
+    insn = (insn & 0xff00) | ((val & 0x03fc) >> 2);
+
+    elfcpp::Swap<16, big_endian>::writeval(wv, insn);
+    return ((val > 0x03fc)
+           ? This::STATUS_OVERFLOW
+           : This::STATUS_OKAY);
   }
 
-  // R_ARM_THM_MOVT_PREL: S + A - P
+  // R_ARM_THM_PC12: S + A - Pa (Thumb32)
   static inline typename This::Status
-  thm_movt_prel(unsigned char *view,
-               const Sized_relobj<32, big_endian>* object,
-               const Symbol_value<32>* psymval,
-               Arm_address address)
+  thm_pc12(unsigned char* view,
+          const Sized_relobj<32, big_endian>* object,
+          const Symbol_value<32>* psymval,
+          Arm_address address)
   {
     typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
     typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
     Valtype* wv = reinterpret_cast<Valtype*>(view);
-    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 = (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);
-    return This::STATUS_OKAY;
+    Reltype insn = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
+                  | elfcpp::Swap<16, big_endian>::readval(wv + 1);
+    // Determine a sign for the addend (positive if the U bit is 1).
+    const int sign = (insn & 0x00800000) ? 1 : -1;
+    int32_t addend = (insn & 0xfff);
+    // Apply a sign to the added.
+    addend *= sign;
+
+    int32_t x = (psymval->value(object, addend) - (address & 0xfffffffc));
+    Reltype val = abs(x);
+    // Mask out and apply the value and the U bit.
+    insn = (insn & 0xff7ff000) | (val & 0xfff);
+    // Set the U bit according to whether the value to go in the
+    // place is positive.
+    if (x >= 0)
+      insn |= 0x00800000;
+
+    elfcpp::Swap<16, big_endian>::writeval(wv, insn >> 16);
+    elfcpp::Swap<16, big_endian>::writeval(wv + 1, insn & 0xffff);
+    return ((val > 0xfff) ?
+           This::STATUS_OVERFLOW : This::STATUS_OKAY);
   }
 
   // R_ARM_V4BX
@@ -2894,6 +3476,181 @@ class Arm_relocate_functions : public Relocate_functions<32, big_endian>
     elfcpp::Swap<32, big_endian>::writeval(wv, val);
     return This::STATUS_OKAY;
   }
+
+  // R_ARM_ALU_PC_G0_NC: ((S + A) | T) - P
+  // R_ARM_ALU_PC_G0:    ((S + A) | T) - P
+  // R_ARM_ALU_PC_G1_NC: ((S + A) | T) - P
+  // R_ARM_ALU_PC_G1:    ((S + A) | T) - P
+  // R_ARM_ALU_PC_G2:    ((S + A) | T) - P
+  // R_ARM_ALU_SB_G0_NC: ((S + A) | T) - B(S)
+  // R_ARM_ALU_SB_G0:    ((S + A) | T) - B(S)
+  // R_ARM_ALU_SB_G1_NC: ((S + A) | T) - B(S)
+  // R_ARM_ALU_SB_G1:    ((S + A) | T) - B(S)
+  // R_ARM_ALU_SB_G2:    ((S + A) | T) - B(S)
+  static inline typename This::Status
+  arm_grp_alu(unsigned char* view,
+       const Sized_relobj<32, big_endian>* object,
+       const Symbol_value<32>* psymval,
+       const int group,
+       Arm_address address,
+       Arm_address thumb_bit,
+       bool check_overflow)
+  {
+    gold_assert(group >= 0 && group < 3);
+    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+    Valtype* wv = reinterpret_cast<Valtype*>(view);
+    Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
+
+    // ALU group relocations are allowed only for the ADD/SUB instructions.
+    // (0x00800000 - ADD, 0x00400000 - SUB)
+    const Valtype opcode = insn & 0x01e00000;
+    if (opcode != 0x00800000 && opcode != 0x00400000)
+      return This::STATUS_BAD_RELOC;
+
+    // Determine a sign for the addend.
+    const int sign = (opcode == 0x00800000) ? 1 : -1;
+    // shifter = rotate_imm * 2
+    const uint32_t shifter = (insn & 0xf00) >> 7;
+    // Initial addend value.
+    int32_t addend = insn & 0xff;
+    // Rotate addend right by shifter.
+    addend = (addend >> shifter) | (addend << (32 - shifter));
+    // Apply a sign to the added.
+    addend *= sign;
+
+    int32_t x = ((psymval->value(object, addend) | thumb_bit) - address);
+    Valtype gn = Arm_relocate_functions::calc_grp_gn(abs(x), group);
+    // Check for overflow if required
+    if (check_overflow
+       && (Arm_relocate_functions::calc_grp_residual(abs(x), group) != 0))
+      return This::STATUS_OVERFLOW;
+
+    // Mask out the value and the ADD/SUB part of the opcode; take care
+    // not to destroy the S bit.
+    insn &= 0xff1ff000;
+    // Set the opcode according to whether the value to go in the
+    // place is negative.
+    insn |= ((x < 0) ? 0x00400000 : 0x00800000);
+    // Encode the offset (encoded Gn).
+    insn |= gn;
+
+    elfcpp::Swap<32, big_endian>::writeval(wv, insn);
+    return This::STATUS_OKAY;
+  }
+
+  // R_ARM_LDR_PC_G0: S + A - P
+  // R_ARM_LDR_PC_G1: S + A - P
+  // R_ARM_LDR_PC_G2: S + A - P
+  // R_ARM_LDR_SB_G0: S + A - B(S)
+  // R_ARM_LDR_SB_G1: S + A - B(S)
+  // R_ARM_LDR_SB_G2: S + A - B(S)
+  static inline typename This::Status
+  arm_grp_ldr(unsigned char* view,
+       const Sized_relobj<32, big_endian>* object,
+       const Symbol_value<32>* psymval,
+       const int group,
+       Arm_address address)
+  {
+    gold_assert(group >= 0 && group < 3);
+    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+    Valtype* wv = reinterpret_cast<Valtype*>(view);
+    Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
+
+    const int sign = (insn & 0x00800000) ? 1 : -1;
+    int32_t addend = (insn & 0xfff) * sign;
+    int32_t x = (psymval->value(object, addend) - address);
+    // Calculate the relevant G(n-1) value to obtain this stage residual.
+    Valtype residual =
+       Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
+    if (residual >= 0x1000)
+      return This::STATUS_OVERFLOW;
+
+    // Mask out the value and U bit.
+    insn &= 0xff7ff000;
+    // Set the U bit for non-negative values.
+    if (x >= 0)
+      insn |= 0x00800000;
+    insn |= residual;
+
+    elfcpp::Swap<32, big_endian>::writeval(wv, insn);
+    return This::STATUS_OKAY;
+  }
+
+  // R_ARM_LDRS_PC_G0: S + A - P
+  // R_ARM_LDRS_PC_G1: S + A - P
+  // R_ARM_LDRS_PC_G2: S + A - P
+  // R_ARM_LDRS_SB_G0: S + A - B(S)
+  // R_ARM_LDRS_SB_G1: S + A - B(S)
+  // R_ARM_LDRS_SB_G2: S + A - B(S)
+  static inline typename This::Status
+  arm_grp_ldrs(unsigned char* view,
+       const Sized_relobj<32, big_endian>* object,
+       const Symbol_value<32>* psymval,
+       const int group,
+       Arm_address address)
+  {
+    gold_assert(group >= 0 && group < 3);
+    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+    Valtype* wv = reinterpret_cast<Valtype*>(view);
+    Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
+
+    const int sign = (insn & 0x00800000) ? 1 : -1;
+    int32_t addend = (((insn & 0xf00) >> 4) + (insn & 0xf)) * sign;
+    int32_t x = (psymval->value(object, addend) - address);
+    // Calculate the relevant G(n-1) value to obtain this stage residual.
+    Valtype residual =
+       Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
+   if (residual >= 0x100)
+      return This::STATUS_OVERFLOW;
+
+    // Mask out the value and U bit.
+    insn &= 0xff7ff0f0;
+    // Set the U bit for non-negative values.
+    if (x >= 0)
+      insn |= 0x00800000;
+    insn |= ((residual & 0xf0) << 4) | (residual & 0xf);
+
+    elfcpp::Swap<32, big_endian>::writeval(wv, insn);
+    return This::STATUS_OKAY;
+  }
+
+  // R_ARM_LDC_PC_G0: S + A - P
+  // R_ARM_LDC_PC_G1: S + A - P
+  // R_ARM_LDC_PC_G2: S + A - P
+  // R_ARM_LDC_SB_G0: S + A - B(S)
+  // R_ARM_LDC_SB_G1: S + A - B(S)
+  // R_ARM_LDC_SB_G2: S + A - B(S)
+  static inline typename This::Status
+  arm_grp_ldc(unsigned char* view,
+      const Sized_relobj<32, big_endian>* object,
+      const Symbol_value<32>* psymval,
+      const int group,
+      Arm_address address)
+  {
+    gold_assert(group >= 0 && group < 3);
+    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+    Valtype* wv = reinterpret_cast<Valtype*>(view);
+    Valtype insn = elfcpp::Swap<32, big_endian>::readval(wv);
+
+    const int sign = (insn & 0x00800000) ? 1 : -1;
+    int32_t addend = ((insn & 0xff) << 2) * sign;
+    int32_t x = (psymval->value(object, addend) - address);
+    // Calculate the relevant G(n-1) value to obtain this stage residual.
+    Valtype residual =
+      Arm_relocate_functions::calc_grp_residual(abs(x), group - 1);
+    if ((residual & 0x3) != 0 || residual >= 0x400)
+      return This::STATUS_OVERFLOW;
+
+    // Mask out the value and U bit.
+    insn &= 0xff7fff00;
+    // Set the U bit for non-negative values.
+    if (x >= 0)
+      insn |= 0x00800000;
+    insn |= (residual >> 2);
+
+    elfcpp::Swap<32, big_endian>::writeval(wv, insn);
+    return This::STATUS_OKAY;
+  }
 };
 
 // Relocate ARM long branches.  This handles relocation types
@@ -2980,12 +3737,14 @@ Arm_relocate_functions<big_endian>::arm_branch_common(
   // 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)
+  if (utils::has_overflow<26>(branch_offset)
       || ((thumb_bit != 0) && !(may_use_blx && r_type == elfcpp::R_ARM_CALL)))
     {
+      Valtype unadjusted_branch_target = psymval->value(object, 0);
+
       Stub_type stub_type =
-       Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
+       Reloc_stub::stub_type_for_reloc(r_type, address,
+                                       unadjusted_branch_target,
                                        (thumb_bit != 0));
       if (stub_type != arm_stub_none)
        {
@@ -2999,8 +3758,7 @@ Arm_relocate_functions<big_endian>::arm_branch_common(
          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));
+         gold_assert(!utils::has_overflow<26>(branch_offset));
        }
     }
 
@@ -3102,25 +3860,30 @@ Arm_relocate_functions<big_endian>::thumb_branch_common(
  
   int32_t addend = This::thumb32_branch_offset(upper_insn, lower_insn);
   Arm_address branch_target = psymval->value(object, addend);
+
+  // For BLX, bit 1 of target address comes from bit 1 of base address.
+  bool may_use_blx = arm_target->may_use_blx();
+  if (thumb_bit == 0 && may_use_blx)
+    branch_target = utils::bit_select(branch_target, address, 0x2);
+
   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();
   bool thumb2 = arm_target->using_thumb2();
-  if ((!thumb2
-       && (branch_offset > THM_MAX_FWD_BRANCH_OFFSET
-          || (branch_offset < THM_MAX_BWD_BRANCH_OFFSET)))
-      || (thumb2
-         && (branch_offset > THM2_MAX_FWD_BRANCH_OFFSET
-             || (branch_offset < THM2_MAX_BWD_BRANCH_OFFSET)))
+  if ((!thumb2 && utils::has_overflow<23>(branch_offset))
+      || (thumb2 && utils::has_overflow<25>(branch_offset))
       || ((thumb_bit == 0)
           && (((r_type == elfcpp::R_ARM_THM_CALL) && !may_use_blx)
              || r_type == elfcpp::R_ARM_THM_JUMP24)))
     {
+      Arm_address unadjusted_branch_target = psymval->value(object, 0);
+
       Stub_type stub_type =
-       Reloc_stub::stub_type_for_reloc(r_type, address, branch_target,
+       Reloc_stub::stub_type_for_reloc(r_type, address,
+                                       unadjusted_branch_target,
                                        (thumb_bit != 0));
+
       if (stub_type != arm_stub_none)
        {
          Stub_table<big_endian>* stub_table =
@@ -3132,6 +3895,8 @@ Arm_relocate_functions<big_endian>::thumb_branch_common(
          gold_assert(stub != NULL);
          thumb_bit = stub->stub_template()->entry_in_thumb_mode() ? 1 : 0;
          branch_target = stub_table->address() + stub->offset() + addend;
+         if (thumb_bit == 0 && may_use_blx) 
+           branch_target = utils::bit_select(branch_target, address, 0x2);
          branch_offset = branch_target - address;
        }
     }
@@ -3152,12 +3917,12 @@ Arm_relocate_functions<big_endian>::thumb_branch_common(
       lower_insn |= 0x1000U;
     }
 
+  // For a BLX instruction, make sure that the relocation is rounded up
+  // to a word boundary.  This follows the semantics of the instruction
+  // which specifies that bit 1 of the target address will come from bit
+  // 1 of the base address.
   if ((lower_insn & 0x5000U) == 0x4000U)
-    // For a BLX instruction, make sure that the relocation is rounded up
-    // to a word boundary.  This follows the semantics of the instruction
-    // which specifies that bit 1 of the target address will come from bit
-    // 1 of the base address.
-    branch_offset = (branch_offset + 2) & ~3;
+    gold_assert((branch_offset & 3) == 0);
 
   // Put BRANCH_OFFSET back into the insn.  Assumes two's complement.
   // We use the Thumb-2 encoding, which is safe even if dealing with
@@ -3168,6 +3933,8 @@ Arm_relocate_functions<big_endian>::thumb_branch_common(
   elfcpp::Swap<16, big_endian>::writeval(wv, upper_insn);
   elfcpp::Swap<16, big_endian>::writeval(wv + 1, lower_insn);
 
+  gold_assert(!utils::has_overflow<25>(branch_offset));
+
   return ((thumb2
           ? utils::has_overflow<25>(branch_offset)
           : utils::has_overflow<23>(branch_offset))
@@ -3223,22 +3990,21 @@ Arm_relocate_functions<big_endian>::thm_jump19(
 // Get the GOT section, creating it if necessary.
 
 template<bool big_endian>
-Output_data_got<32, big_endian>*
+Arm_output_data_got<big_endian>*
 Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
 {
   if (this->got_ == NULL)
     {
       gold_assert(symtab != NULL && layout != NULL);
 
-      this->got_ = new Output_data_got<32, big_endian>();
+      this->got_ = new Arm_output_data_got<big_endian>(symtab, layout);
 
       Output_section* os;
       os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
                                           (elfcpp::SHF_ALLOC
                                            | elfcpp::SHF_WRITE),
-                                          this->got_, false, true, true,
-                                          false);
-
+                                          this->got_, false, false, false,
+                                          true);
       // The old GNU linker creates a .got.plt section.  We just
       // create another set of data in the .got section.  Note that we
       // always create a PLT if we create a GOT, although the PLT
@@ -3248,7 +4014,7 @@ Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
                                           (elfcpp::SHF_ALLOC
                                            | elfcpp::SHF_WRITE),
                                           this->got_plt_, false, false,
-                                          false, true);
+                                          false, false);
 
       // The first three entries are reserved.
       this->got_plt_->set_current_data_size(3 * 4);
@@ -3377,7 +4143,7 @@ Stub_template::Stub_template(
 
 // Stub methods.
 
-// Template to implement do_write for a specific target endianity.
+// Template to implement do_write for a specific target endianness.
 
 template<bool big_endian>
 void inline
@@ -3497,10 +4263,15 @@ Reloc_stub::stub_type_for_reloc(
       thumb_only = little_endian_target->using_thumb_only();
     }
 
-  int64_t branch_offset = (int64_t)destination - location;
-
+  int64_t branch_offset;
   if (r_type == elfcpp::R_ARM_THM_CALL || r_type == elfcpp::R_ARM_THM_JUMP24)
     {
+      // For THUMB BLX instruction, bit 1 of target comes from bit 1 of the
+      // base address (instruction address + 4).
+      if ((r_type == elfcpp::R_ARM_THM_CALL) && may_use_blx && !target_is_thumb)
+       destination = utils::bit_select(destination, location, 0x2);
+      branch_offset = static_cast<int64_t>(destination) - location;
+       
       // Handle cases where:
       // - this call goes too far (different Thumb/Thumb2 max
       //   distance)
@@ -3581,6 +4352,7 @@ Reloc_stub::stub_type_for_reloc(
           || r_type == elfcpp::R_ARM_JUMP24
           || r_type == elfcpp::R_ARM_PLT32)
     {
+      branch_offset = static_cast<int64_t>(destination) - location;
       if (target_is_thumb)
        {
          // Arm to thumb.
@@ -4006,20 +4778,9 @@ template<bool big_endian>
 bool
 Stub_table<big_endian>::update_data_size_and_addralign()
 {
-  off_t size = 0;
-  unsigned addralign = 1;
-
   // Go over all stubs in table to compute data size and address alignment.
-  
-  for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
-      p != this->reloc_stubs_.end();
-      ++p)
-    {
-      const Stub_template* stub_template = p->second->stub_template();
-      addralign = std::max(addralign, stub_template->alignment());
-      size = (align_address(size, stub_template->alignment())
-             + stub_template->size());
-    }
+  off_t size = this->reloc_stubs_size_;
+  unsigned addralign = this->reloc_stubs_addralign_;
 
   for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
        p != this->cortex_a8_stubs_.end();
@@ -4064,19 +4825,7 @@ template<bool big_endian>
 void
 Stub_table<big_endian>::finalize_stubs()
 {
-  off_t off = 0;
-  for (typename Reloc_stub_map::const_iterator p = this->reloc_stubs_.begin();
-      p != this->reloc_stubs_.end();
-      ++p)
-    {
-      Reloc_stub* stub = p->second;
-      const Stub_template* stub_template = stub->stub_template();
-      uint64_t stub_addralign = stub_template->alignment();
-      off = align_address(off, stub_addralign);
-      stub->set_offset(off);
-      off += stub_template->size();
-    }
-
+  off_t off = this->reloc_stubs_size_;
   for (Cortex_a8_stub_list::const_iterator p = this->cortex_a8_stubs_.begin();
        p != this->cortex_a8_stubs_.end();
        ++p)
@@ -4196,21 +4945,17 @@ template<bool big_endian>
 void
 Arm_input_section<big_endian>::set_final_data_size()
 {
-  // If this owns a stub table, finalize its data size as well.
+  off_t off = convert_types<off_t, uint64_t>(this->original_size_);
+
   if (this->is_stub_table_owner())
     {
-      uint64_t address = this->address();
-
       // The stub table comes after the original section contents.
-      address += this->original_size_;
-      address = align_address(address, this->stub_table_->addralign());
-      off_t offset = this->offset() + (address - this->address());
-      this->stub_table_->set_address_and_file_offset(address, offset);
-      address += this->stub_table_->data_size();
-      gold_assert(address == this->address() + this->current_data_size());
+      off = align_address(off, this->stub_table_->addralign());
+      this->stub_table_->set_address_and_file_offset(this->address() + off,
+                                                    this->offset() + off);
+      off += this->stub_table_->data_size();
     }
-
-  this->set_data_size(this->current_data_size());
+  this->set_data_size(off);
 }
 
 // Reset address and file offset.
@@ -4237,60 +4982,401 @@ Arm_input_section<big_endian>::do_reset_address_and_file_offset()
   this->set_current_data_size(off);
 }
 
-// Arm_output_section methods.
+// Arm_exidx_cantunwind methods.
 
-// Create a stub group for input sections from BEGIN to END.  OWNER
-// points to the input section to be the owner a new stub table.
+// Write this to Output file OF for a fixed endianness.
 
 template<bool big_endian>
 void
-Arm_output_section<big_endian>::create_stub_group(
-  Input_section_list::const_iterator begin,
-  Input_section_list::const_iterator end,
-  Input_section_list::const_iterator owner,
-  Target_arm<big_endian>* target,
-  std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
+Arm_exidx_cantunwind::do_fixed_endian_write(Output_file* of)
 {
-  // Currently we convert ordinary input sections into relaxed sections only
-  // at this point but we may want to support creating relaxed input section
-  // very early.  So we check here to see if owner is already a relaxed
-  // section.
+  off_t offset = this->offset();
+  const section_size_type oview_size = 8;
+  unsigned char* const oview = of->get_output_view(offset, oview_size);
   
-  Arm_input_section<big_endian>* arm_input_section;
-  if (owner->is_relaxed_input_section())
+  typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+  Valtype* wv = reinterpret_cast<Valtype*>(oview);
+
+  Output_section* os = this->relobj_->output_section(this->shndx_);
+  gold_assert(os != NULL);
+
+  Arm_relobj<big_endian>* arm_relobj =
+    Arm_relobj<big_endian>::as_arm_relobj(this->relobj_);
+  Arm_address output_offset =
+    arm_relobj->get_output_section_offset(this->shndx_);
+  Arm_address section_start;
+  if (output_offset != Arm_relobj<big_endian>::invalid_address)
+    section_start = os->address() + output_offset;
+  else
     {
-      arm_input_section =
-       Arm_input_section<big_endian>::as_arm_input_section(
-         owner->relaxed_input_section());
+      // Currently this only happens for a relaxed section.
+      const Output_relaxed_input_section* poris =
+       os->find_relaxed_input_section(this->relobj_, this->shndx_);
+      gold_assert(poris != NULL);
+      section_start = poris->address();
     }
+
+  // We always append this to the end of an EXIDX section.
+  Arm_address output_address =
+    section_start + this->relobj_->section_size(this->shndx_);
+
+  // Write out the entry.  The first word either points to the beginning
+  // or after the end of a text section.  The second word is the special
+  // EXIDX_CANTUNWIND value.
+  uint32_t prel31_offset = output_address - this->address();
+  if (utils::has_overflow<31>(offset))
+    gold_error(_("PREL31 overflow in EXIDX_CANTUNWIND entry"));
+  elfcpp::Swap<32, big_endian>::writeval(wv, prel31_offset & 0x7fffffffU);
+  elfcpp::Swap<32, big_endian>::writeval(wv + 1, elfcpp::EXIDX_CANTUNWIND);
+
+  of->write_output_view(this->offset(), oview_size, oview);
+}
+
+// Arm_exidx_merged_section methods.
+
+// Constructor for Arm_exidx_merged_section.
+// EXIDX_INPUT_SECTION points to the unmodified EXIDX input section.
+// SECTION_OFFSET_MAP points to a section offset map describing how
+// parts of the input section are mapped to output.  DELETED_BYTES is
+// the number of bytes deleted from the EXIDX input section.
+
+Arm_exidx_merged_section::Arm_exidx_merged_section(
+    const Arm_exidx_input_section& exidx_input_section,
+    const Arm_exidx_section_offset_map& section_offset_map,
+    uint32_t deleted_bytes)
+  : Output_relaxed_input_section(exidx_input_section.relobj(),
+                                exidx_input_section.shndx(),
+                                exidx_input_section.addralign()),
+    exidx_input_section_(exidx_input_section),
+    section_offset_map_(section_offset_map)
+{
+  // Fix size here so that we do not need to implement set_final_data_size.
+  this->set_data_size(exidx_input_section.size() - deleted_bytes);
+  this->fix_data_size();
+}
+
+// Given an input OBJECT, an input section index SHNDX within that
+// object, and an OFFSET relative to the start of that input
+// section, return whether or not the corresponding offset within
+// the output section is known.  If this function returns true, it
+// sets *POUTPUT to the output offset.  The value -1 indicates that
+// this input offset is being discarded.
+
+bool
+Arm_exidx_merged_section::do_output_offset(
+    const Relobj* relobj,
+    unsigned int shndx,
+    section_offset_type offset,
+    section_offset_type* poutput) const
+{
+  // We only handle offsets for the original EXIDX input section.
+  if (relobj != this->exidx_input_section_.relobj()
+      || shndx != this->exidx_input_section_.shndx())
+    return false;
+
+  section_offset_type section_size =
+    convert_types<section_offset_type>(this->exidx_input_section_.size());
+  if (offset < 0 || offset >= section_size)
+    // Input offset is out of valid range.
+    *poutput = -1;
   else
     {
-      gold_assert(owner->is_input_section());
-      // Create a new relaxed input section.
-      arm_input_section =
-       target->new_arm_input_section(owner->relobj(), owner->shndx());
-      new_relaxed_sections->push_back(arm_input_section);
+      // We need to look up the section offset map to determine the output
+      // offset.  Find the reference point in map that is first offset
+      // bigger than or equal to this offset.
+      Arm_exidx_section_offset_map::const_iterator p =
+       this->section_offset_map_.lower_bound(offset);
+
+      // The section offset maps are build such that this should not happen if
+      // input offset is in the valid range.
+      gold_assert(p != this->section_offset_map_.end());
+
+      // We need to check if this is dropped.
+     section_offset_type ref = p->first;
+     section_offset_type mapped_ref = p->second;
+
+      if (mapped_ref != Arm_exidx_input_section::invalid_offset)
+       // Offset is present in output.
+       *poutput = mapped_ref + (offset - ref);
+      else
+       // Offset is discarded owing to EXIDX entry merging.
+       *poutput = -1;
     }
+  
+  return true;
+}
 
-  // Create a stub table.
-  Stub_table<big_endian>* stub_table =
-    target->new_stub_table(arm_input_section);
+// Write this to output file OF.
 
-  arm_input_section->set_stub_table(stub_table);
+void
+Arm_exidx_merged_section::do_write(Output_file* of)
+{
+  // If we retain or discard the whole EXIDX input section,  we would
+  // not be here.
+  gold_assert(this->data_size() != this->exidx_input_section_.size()
+             && this->data_size() != 0);
+
+  off_t offset = this->offset();
+  const section_size_type oview_size = this->data_size();
+  unsigned char* const oview = of->get_output_view(offset, oview_size);
   
-  Input_section_list::const_iterator p = begin;
-  Input_section_list::const_iterator prev_p;
+  Output_section* os = this->relobj()->output_section(this->shndx());
+  gold_assert(os != NULL);
 
-  // Look for input sections or relaxed input sections in [begin ... end].
-  do
+  // Get contents of EXIDX input section.
+  section_size_type section_size;
+  const unsigned char* section_contents =
+    this->relobj()->section_contents(this->shndx(), &section_size, false); 
+  gold_assert(section_size == this->exidx_input_section_.size());
+
+  // Go over spans of input offsets and write only those that are not
+  // discarded.
+  section_offset_type in_start = 0;
+  section_offset_type out_start = 0;
+  for(Arm_exidx_section_offset_map::const_iterator p =
+        this->section_offset_map_.begin();
+      p != this->section_offset_map_.end();
+      ++p)
     {
-      if (p->is_input_section() || p->is_relaxed_input_section())
+      section_offset_type in_end = p->first;
+      gold_assert(in_end >= in_start);
+      section_offset_type out_end = p->second;
+      size_t in_chunk_size = convert_types<size_t>(in_end - in_start + 1);
+      if (out_end != -1)
        {
-         // The stub table information for input sections live
-         // in their objects.
-         Arm_relobj<big_endian>* arm_relobj =
-           Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
-         arm_relobj->set_stub_table(p->shndx(), stub_table);
+         size_t out_chunk_size =
+           convert_types<size_t>(out_end - out_start + 1);
+         gold_assert(out_chunk_size == in_chunk_size);
+         memcpy(oview + out_start, section_contents + in_start,
+                out_chunk_size);
+         out_start += out_chunk_size;
+       }
+      in_start += in_chunk_size;
+    }
+
+  gold_assert(convert_to_section_size_type(out_start) == oview_size);
+  of->write_output_view(this->offset(), oview_size, oview);
+}
+
+// Arm_exidx_fixup methods.
+
+// Append an EXIDX_CANTUNWIND in the current output section if the last entry
+// is not an EXIDX_CANTUNWIND entry already.  The new EXIDX_CANTUNWIND entry
+// points to the end of the last seen EXIDX section.
+
+void
+Arm_exidx_fixup::add_exidx_cantunwind_as_needed()
+{
+  if (this->last_unwind_type_ != UT_EXIDX_CANTUNWIND
+      && this->last_input_section_ != NULL)
+    {
+      Relobj* relobj = this->last_input_section_->relobj();
+      unsigned int text_shndx = this->last_input_section_->link();
+      Arm_exidx_cantunwind* cantunwind =
+       new Arm_exidx_cantunwind(relobj, text_shndx);
+      this->exidx_output_section_->add_output_section_data(cantunwind);
+      this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
+    }
+}
+
+// Process an EXIDX section entry in input.  Return whether this entry
+// can be deleted in the output.  SECOND_WORD in the second word of the
+// EXIDX entry.
+
+bool
+Arm_exidx_fixup::process_exidx_entry(uint32_t second_word)
+{
+  bool delete_entry;
+  if (second_word == elfcpp::EXIDX_CANTUNWIND)
+    {
+      // Merge if previous entry is also an EXIDX_CANTUNWIND.
+      delete_entry = this->last_unwind_type_ == UT_EXIDX_CANTUNWIND;
+      this->last_unwind_type_ = UT_EXIDX_CANTUNWIND;
+    }
+  else if ((second_word & 0x80000000) != 0)
+    {
+      // Inlined unwinding data.  Merge if equal to previous.
+      delete_entry = (merge_exidx_entries_
+                     && this->last_unwind_type_ == UT_INLINED_ENTRY
+                     && this->last_inlined_entry_ == second_word);
+      this->last_unwind_type_ = UT_INLINED_ENTRY;
+      this->last_inlined_entry_ = second_word;
+    }
+  else
+    {
+      // Normal table entry.  In theory we could merge these too,
+      // but duplicate entries are likely to be much less common.
+      delete_entry = false;
+      this->last_unwind_type_ = UT_NORMAL_ENTRY;
+    }
+  return delete_entry;
+}
+
+// Update the current section offset map during EXIDX section fix-up.
+// If there is no map, create one.  INPUT_OFFSET is the offset of a
+// reference point, DELETED_BYTES is the number of deleted by in the
+// section so far.  If DELETE_ENTRY is true, the reference point and
+// all offsets after the previous reference point are discarded.
+
+void
+Arm_exidx_fixup::update_offset_map(
+    section_offset_type input_offset,
+    section_size_type deleted_bytes,
+    bool delete_entry)
+{
+  if (this->section_offset_map_ == NULL)
+    this->section_offset_map_ = new Arm_exidx_section_offset_map();
+  section_offset_type output_offset;
+  if (delete_entry)
+    output_offset = Arm_exidx_input_section::invalid_offset;
+  else
+    output_offset = input_offset - deleted_bytes;
+  (*this->section_offset_map_)[input_offset] = output_offset;
+}
+
+// Process EXIDX_INPUT_SECTION for EXIDX entry merging.  Return the number of
+// bytes deleted.  If some entries are merged, also store a pointer to a newly
+// created Arm_exidx_section_offset_map object in *PSECTION_OFFSET_MAP.  The
+// caller owns the map and is responsible for releasing it after use.
+
+template<bool big_endian>
+uint32_t
+Arm_exidx_fixup::process_exidx_section(
+    const Arm_exidx_input_section* exidx_input_section,
+    Arm_exidx_section_offset_map** psection_offset_map)
+{
+  Relobj* relobj = exidx_input_section->relobj();
+  unsigned shndx = exidx_input_section->shndx();
+  section_size_type section_size;
+  const unsigned char* section_contents =
+    relobj->section_contents(shndx, &section_size, false);
+
+  if ((section_size % 8) != 0)
+    {
+      // Something is wrong with this section.  Better not touch it.
+      gold_error(_("uneven .ARM.exidx section size in %s section %u"),
+                relobj->name().c_str(), shndx);
+      this->last_input_section_ = exidx_input_section;
+      this->last_unwind_type_ = UT_NONE;
+      return 0;
+    }
+  
+  uint32_t deleted_bytes = 0;
+  bool prev_delete_entry = false;
+  gold_assert(this->section_offset_map_ == NULL);
+
+  for (section_size_type i = 0; i < section_size; i += 8)
+    {
+      typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+      const Valtype* wv =
+         reinterpret_cast<const Valtype*>(section_contents + i + 4);
+      uint32_t second_word = elfcpp::Swap<32, big_endian>::readval(wv);
+
+      bool delete_entry = this->process_exidx_entry(second_word);
+
+      // Entry deletion causes changes in output offsets.  We use a std::map
+      // to record these.  And entry (x, y) means input offset x
+      // is mapped to output offset y.  If y is invalid_offset, then x is
+      // dropped in the output.  Because of the way std::map::lower_bound
+      // works, we record the last offset in a region w.r.t to keeping or
+      // dropping.  If there is no entry (x0, y0) for an input offset x0,
+      // the output offset y0 of it is determined by the output offset y1 of
+      // the smallest input offset x1 > x0 that there is an (x1, y1) entry
+      // in the map.  If y1 is not -1, then y0 = y1 + x0 - x1.  Othewise, y1
+      // y0 is also -1.
+      if (delete_entry != prev_delete_entry && i != 0)
+       this->update_offset_map(i - 1, deleted_bytes, prev_delete_entry);
+
+      // Update total deleted bytes for this entry.
+      if (delete_entry)
+       deleted_bytes += 8;
+
+      prev_delete_entry = delete_entry;
+    }
+  
+  // If section offset map is not NULL, make an entry for the end of
+  // section.
+  if (this->section_offset_map_ != NULL)
+    update_offset_map(section_size - 1, deleted_bytes, prev_delete_entry);
+
+  *psection_offset_map = this->section_offset_map_;
+  this->section_offset_map_ = NULL;
+  this->last_input_section_ = exidx_input_section;
+  
+  // Set the first output text section so that we can link the EXIDX output
+  // section to it.  Ignore any EXIDX input section that is completely merged.
+  if (this->first_output_text_section_ == NULL
+      && deleted_bytes != section_size)
+    {
+      unsigned int link = exidx_input_section->link();
+      Output_section* os = relobj->output_section(link);
+      gold_assert(os != NULL);
+      this->first_output_text_section_ = os;
+    }
+
+  return deleted_bytes;
+}
+
+// Arm_output_section methods.
+
+// Create a stub group for input sections from BEGIN to END.  OWNER
+// points to the input section to be the owner a new stub table.
+
+template<bool big_endian>
+void
+Arm_output_section<big_endian>::create_stub_group(
+  Input_section_list::const_iterator begin,
+  Input_section_list::const_iterator end,
+  Input_section_list::const_iterator owner,
+  Target_arm<big_endian>* target,
+  std::vector<Output_relaxed_input_section*>* new_relaxed_sections)
+{
+  // We use a different kind of relaxed section in an EXIDX section.
+  // The static casting from Output_relaxed_input_section to
+  // Arm_input_section is invalid in an EXIDX section.  We are okay
+  // because we should not be calling this for an EXIDX section. 
+  gold_assert(this->type() != elfcpp::SHT_ARM_EXIDX);
+
+  // Currently we convert ordinary input sections into relaxed sections only
+  // at this point but we may want to support creating relaxed input section
+  // very early.  So we check here to see if owner is already a relaxed
+  // section.
+  
+  Arm_input_section<big_endian>* arm_input_section;
+  if (owner->is_relaxed_input_section())
+    {
+      arm_input_section =
+       Arm_input_section<big_endian>::as_arm_input_section(
+         owner->relaxed_input_section());
+    }
+  else
+    {
+      gold_assert(owner->is_input_section());
+      // Create a new relaxed input section.
+      arm_input_section =
+       target->new_arm_input_section(owner->relobj(), owner->shndx());
+      new_relaxed_sections->push_back(arm_input_section);
+    }
+
+  // Create a stub table.
+  Stub_table<big_endian>* stub_table =
+    target->new_stub_table(arm_input_section);
+
+  arm_input_section->set_stub_table(stub_table);
+  
+  Input_section_list::const_iterator p = begin;
+  Input_section_list::const_iterator prev_p;
+
+  // Look for input sections or relaxed input sections in [begin ... end].
+  do
+    {
+      if (p->is_input_section() || p->is_relaxed_input_section())
+       {
+         // The stub table information for input sections live
+         // in their objects.
+         Arm_relobj<big_endian>* arm_relobj =
+           Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
+         arm_relobj->set_stub_table(p->shndx(), stub_table);
        }
       prev_p = p++;
     }
@@ -4446,8 +5532,239 @@ Arm_output_section<big_endian>::group_sections(
     }
 }
 
+// Append non empty text sections in this to LIST in ascending
+// order of their position in this.
+
+template<bool big_endian>
+void
+Arm_output_section<big_endian>::append_text_sections_to_list(
+    Text_section_list* list)
+{
+  // We only care about text sections.
+  if ((this->flags() & elfcpp::SHF_EXECINSTR) == 0)
+    return;
+
+  gold_assert((this->flags() & elfcpp::SHF_ALLOC) != 0);
+
+  for (Input_section_list::const_iterator p = this->input_sections().begin();
+       p != this->input_sections().end();
+       ++p)
+    {
+      // We only care about plain or relaxed input sections.  We also
+      // ignore any merged sections.
+      if ((p->is_input_section() || p->is_relaxed_input_section())
+         && p->data_size() != 0)
+       list->push_back(Text_section_list::value_type(p->relobj(),
+                                                     p->shndx()));
+    }
+}
+
+template<bool big_endian>
+void
+Arm_output_section<big_endian>::fix_exidx_coverage(
+    Layout* layout,
+    const Text_section_list& sorted_text_sections,
+    Symbol_table* symtab,
+    bool merge_exidx_entries)
+{
+  // We should only do this for the EXIDX output section.
+  gold_assert(this->type() == elfcpp::SHT_ARM_EXIDX);
+
+  // We don't want the relaxation loop to undo these changes, so we discard
+  // the current saved states and take another one after the fix-up.
+  this->discard_states();
+
+  // Remove all input sections.
+  uint64_t address = this->address();
+  typedef std::list<Simple_input_section> Simple_input_section_list;
+  Simple_input_section_list input_sections;
+  this->reset_address_and_file_offset();
+  this->get_input_sections(address, std::string(""), &input_sections);
+
+  if (!this->input_sections().empty())
+    gold_error(_("Found non-EXIDX input sections in EXIDX output section"));
+  
+  // Go through all the known input sections and record them.
+  typedef Unordered_set<Section_id, Section_id_hash> Section_id_set;
+  Section_id_set known_input_sections;
+  for (Simple_input_section_list::const_iterator p = input_sections.begin();
+       p != input_sections.end();
+       ++p)
+    {
+      // This should never happen.  At this point, we should only see
+      // plain EXIDX input sections.
+      gold_assert(!p->is_relaxed_input_section());
+      known_input_sections.insert(Section_id(p->relobj(), p->shndx()));
+    }
+
+  Arm_exidx_fixup exidx_fixup(this, merge_exidx_entries);
+
+  // Go over the sorted text sections.
+  Section_id_set processed_input_sections;
+  for (Text_section_list::const_iterator p = sorted_text_sections.begin();
+       p != sorted_text_sections.end();
+       ++p)
+    {
+      Relobj* relobj = p->first;
+      unsigned int shndx = p->second;
+
+      Arm_relobj<big_endian>* arm_relobj =
+        Arm_relobj<big_endian>::as_arm_relobj(relobj);
+      const Arm_exidx_input_section* exidx_input_section =
+        arm_relobj->exidx_input_section_by_link(shndx);
+
+      // If this text section has no EXIDX section, force an EXIDX_CANTUNWIND
+      // entry pointing to the end of the last seen EXIDX section.
+      if (exidx_input_section == NULL)
+       {
+         exidx_fixup.add_exidx_cantunwind_as_needed();
+         continue;
+       }
+
+      Relobj* exidx_relobj = exidx_input_section->relobj();
+      unsigned int exidx_shndx = exidx_input_section->shndx();
+      Section_id sid(exidx_relobj, exidx_shndx);
+      if (known_input_sections.find(sid) == known_input_sections.end())
+       {
+         // This is odd.  We have not seen this EXIDX input section before.
+         // We cannot do fix-up.  If we saw a SECTIONS clause in a script,
+         // issue a warning instead.  We assume the user knows what he
+         // or she is doing.  Otherwise, this is an error.
+         if (layout->script_options()->saw_sections_clause())
+           gold_warning(_("unwinding may not work because EXIDX input section"
+                          " %u of %s is not in EXIDX output section"),
+                        exidx_shndx, exidx_relobj->name().c_str());
+         else
+           gold_error(_("unwinding may not work because EXIDX input section"
+                        " %u of %s is not in EXIDX output section"),
+                      exidx_shndx, exidx_relobj->name().c_str());
+
+         exidx_fixup.add_exidx_cantunwind_as_needed();
+         continue;
+       }
+
+      // Fix up coverage and append input section to output data list.
+      Arm_exidx_section_offset_map* section_offset_map = NULL;
+      uint32_t deleted_bytes =
+        exidx_fixup.process_exidx_section<big_endian>(exidx_input_section,
+                                                     &section_offset_map);
+
+      if (deleted_bytes == exidx_input_section->size())
+       {
+         // The whole EXIDX section got merged.  Remove it from output.
+         gold_assert(section_offset_map == NULL);
+         exidx_relobj->set_output_section(exidx_shndx, NULL);
+
+         // All local symbols defined in this input section will be dropped.
+         // We need to adjust output local symbol count.
+         arm_relobj->set_output_local_symbol_count_needs_update();
+       }
+      else if (deleted_bytes > 0)
+       {
+         // Some entries are merged.  We need to convert this EXIDX input
+         // section into a relaxed section.
+         gold_assert(section_offset_map != NULL);
+         Arm_exidx_merged_section* merged_section =
+           new Arm_exidx_merged_section(*exidx_input_section,
+                                        *section_offset_map, deleted_bytes);
+         this->add_relaxed_input_section(merged_section);
+         arm_relobj->convert_input_section_to_relaxed_section(exidx_shndx);
+
+         // All local symbols defined in discarded portions of this input
+         // section will be dropped.  We need to adjust output local symbol
+         // count.
+         arm_relobj->set_output_local_symbol_count_needs_update();
+       }
+      else
+       {
+         // Just add back the EXIDX input section.
+         gold_assert(section_offset_map == NULL);
+         Output_section::Simple_input_section sis(exidx_relobj, exidx_shndx);
+         this->add_simple_input_section(sis, exidx_input_section->size(),
+                                        exidx_input_section->addralign());
+       }
+
+      processed_input_sections.insert(Section_id(exidx_relobj, exidx_shndx)); 
+    }
+
+  // Insert an EXIDX_CANTUNWIND entry at the end of output if necessary.
+  exidx_fixup.add_exidx_cantunwind_as_needed();
+
+  // Remove any known EXIDX input sections that are not processed.
+  for (Simple_input_section_list::const_iterator p = input_sections.begin();
+       p != input_sections.end();
+       ++p)
+    {
+      if (processed_input_sections.find(Section_id(p->relobj(), p->shndx()))
+         == processed_input_sections.end())
+       {
+         // We only discard a known EXIDX section because its linked
+         // text section has been folded by ICF.
+         Arm_relobj<big_endian>* arm_relobj =
+           Arm_relobj<big_endian>::as_arm_relobj(p->relobj());
+         const Arm_exidx_input_section* exidx_input_section =
+           arm_relobj->exidx_input_section_by_shndx(p->shndx());
+         gold_assert(exidx_input_section != NULL);
+         unsigned int text_shndx = exidx_input_section->link();
+         gold_assert(symtab->is_section_folded(p->relobj(), text_shndx));
+
+         // Remove this from link.  We also need to recount the
+         // local symbols.
+         p->relobj()->set_output_section(p->shndx(), NULL);
+         arm_relobj->set_output_local_symbol_count_needs_update();
+       }
+    }
+    
+  // Link exidx output section to the first seen output section and
+  // set correct entry size.
+  this->set_link_section(exidx_fixup.first_output_text_section());
+  this->set_entsize(8);
+
+  // Make changes permanent.
+  this->save_states();
+  this->set_section_offsets_need_adjustment();
+}
+
 // Arm_relobj methods.
 
+// Determine if an input section is scannable for stub processing.  SHDR is
+// the header of the section and SHNDX is the section index.  OS is the output
+// section for the input section and SYMTAB is the global symbol table used to
+// look up ICF information.
+
+template<bool big_endian>
+bool
+Arm_relobj<big_endian>::section_is_scannable(
+    const elfcpp::Shdr<32, big_endian>& shdr,
+    unsigned int shndx,
+    const Output_section* os,
+    const Symbol_table *symtab)
+{
+  // Skip any empty sections, unallocated sections or sections whose
+  // type are not SHT_PROGBITS.
+  if (shdr.get_sh_size() == 0
+      || (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) == 0
+      || shdr.get_sh_type() != elfcpp::SHT_PROGBITS)
+    return false;
+
+  // Skip any discarded or ICF'ed sections.
+  if (os == NULL || symtab->is_section_folded(this, shndx))
+    return false;
+
+  // If this requires special offset handling, check to see if it is
+  // a relaxed section.  If this is not, then it is a merged section that
+  // we cannot handle.
+  if (this->is_output_section_offset_invalid(shndx))
+    {
+      const Output_relaxed_input_section* poris =
+       os->find_relaxed_input_section(this, shndx);
+      if (poris == NULL)
+       return false;
+    }
+
+  return true;
+}
+
 // Determine if we want to scan the SHNDX-th section for relocation stubs.
 // This is a helper for Arm_relobj::scan_sections_for_stubs() below.
 
@@ -4456,7 +5773,8 @@ bool
 Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
     const elfcpp::Shdr<32, big_endian>& shdr,
     const Relobj::Output_sections& out_sections,
-    const Symbol_table *symtab)
+    const Symbol_table *symtab,
+    const unsigned char* pshdrs)
 {
   unsigned int sh_type = shdr.get_sh_type();
   if (sh_type != elfcpp::SHT_REL && sh_type != elfcpp::SHT_RELA)
@@ -4467,18 +5785,6 @@ Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
   if (sh_size == 0)
     return false;
 
-  // Ignore reloc section with bad info.  This error will be
-  // reported in the final link.
-  unsigned int index = this->adjust_shndx(shdr.get_sh_info());
-  if (index >= this->shnum())
-    return false;
-
-  // This relocation section is against a section which we
-  // discarded or if the section is folded into another
-  // section due to ICF.
-  if (out_sections[index] == NULL || symtab->is_section_folded(this, index))
-    return false;
-
   // Ignore reloc section with unexpected symbol table.  The
   // error will be reported in the final link.
   if (this->adjust_shndx(shdr.get_sh_link()) != this->symtab_shndx())
@@ -4495,7 +5801,39 @@ Arm_relobj<big_endian>::section_needs_reloc_stub_scanning(
   if (reloc_size != shdr.get_sh_entsize() || sh_size % reloc_size != 0)
     return false;
 
-  return true;
+  // Ignore reloc section with bad info.  This error will be
+  // reported in the final link.
+  unsigned int index = this->adjust_shndx(shdr.get_sh_info());
+  if (index >= this->shnum())
+    return false;
+
+  const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
+  const elfcpp::Shdr<32, big_endian> text_shdr(pshdrs + index * shdr_size);
+  return this->section_is_scannable(text_shdr, index,
+                                  out_sections[index], symtab);
+}
+
+// Return the output address of either a plain input section or a relaxed
+// input section.  SHNDX is the section index.  We define and use this
+// instead of calling Output_section::output_address because that is slow
+// for large output.
+
+template<bool big_endian>
+Arm_address
+Arm_relobj<big_endian>::simple_input_section_output_address(
+    unsigned int shndx,
+    Output_section* os)
+{
+  if (this->is_output_section_offset_invalid(shndx))
+    {
+      const Output_relaxed_input_section* poris =
+       os->find_relaxed_input_section(this, shndx);
+      // We do not handle merged sections here.
+      gold_assert(poris != NULL);
+      return poris->address();
+    }
+  else
+    return os->address() + this->get_output_section_offset(shndx);
 }
 
 // Determine if we want to scan the SHNDX-th section for non-relocation stubs.
@@ -4509,20 +5847,12 @@ Arm_relobj<big_endian>::section_needs_cortex_a8_stub_scanning(
     Output_section* os,
     const Symbol_table* symtab)
 {
-  // We only scan non-empty code sections.
-  if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) == 0
-      || shdr.get_sh_size() == 0)
-    return false;
-
-  // Ignore discarded or ICF'ed sections.
-  if (os == NULL || symtab->is_section_folded(this, shndx))
+  if (!this->section_is_scannable(shdr, shndx, os, symtab))
     return false;
-  
-  // Find output address of section.
-  Arm_address address = os->output_address(this, shndx, 0);
 
   // If the section does not cross any 4K-boundaries, it does not need to
   // be scanned.
+  Arm_address address = this->simple_input_section_output_address(shndx, os);
   if ((address & ~0xfffU) == ((address + shdr.get_sh_size() - 1) & ~0xfffU))
     return false;
 
@@ -4539,7 +5869,26 @@ Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
     Output_section* os,
     Target_arm<big_endian>* arm_target)
 {
-  Arm_address output_address = os->output_address(this, shndx, 0);
+  // Look for the first mapping symbol in this section.  It should be
+  // at (shndx, 0).
+  Mapping_symbol_position section_start(shndx, 0);
+  typename Mapping_symbols_info::const_iterator p =
+    this->mapping_symbols_info_.lower_bound(section_start);
+
+  // There are no mapping symbols for this section.  Treat it as a data-only
+  // section.  Issue a warning if section is marked as containing
+  // instructions.
+  if (p == this->mapping_symbols_info_.end() || p->first.first != shndx)
+    {
+      if ((this->section_flags(shndx) & elfcpp::SHF_EXECINSTR) != 0)
+       gold_warning(_("cannot scan executable section %u of %s for Cortex-A8 "
+                      "erratum because it has no mapping symbols."),
+                    shndx, this->name().c_str());
+      return;
+    }
+
+  Arm_address output_address =
+    this->simple_input_section_output_address(shndx, os);
 
   // Get the section contents.
   section_size_type input_view_size = 0;
@@ -4551,21 +5900,6 @@ Arm_relobj<big_endian>::scan_section_for_cortex_a8_erratum(
   // THUMB code only.  Second, we only want to look at the 4K-page boundary
   // to speed up the scanning.
   
-  // Look for the first mapping symbol in this section.  It should be
-  // at (shndx, 0).
-  Mapping_symbol_position section_start(shndx, 0);
-  typename Mapping_symbols_info::const_iterator p =
-    this->mapping_symbols_info_.lower_bound(section_start);
-
-  if (p == this->mapping_symbols_info_.end()
-      || p->first != section_start)
-    {
-      gold_warning(_("Cortex-A8 erratum scanning failed because there "
-                    "is no mapping symbols for section %u of %s"),
-                  shndx, this->name().c_str());
-      return;
-    }
   while (p != this->mapping_symbols_info_.end()
        && p->first.first == shndx)
     {
@@ -4632,12 +5966,13 @@ Arm_relobj<big_endian>::scan_sections_for_stubs(
   for (unsigned int i = 1; i < shnum; ++i, p += shdr_size)
     {
       const elfcpp::Shdr<32, big_endian> shdr(p);
-      if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab))
+      if (this->section_needs_reloc_stub_scanning(shdr, out_sections, symtab,
+                                                 pshdrs))
        {
          unsigned int index = this->adjust_shndx(shdr.get_sh_info());
          Arm_address output_offset = this->get_output_section_offset(index);
          Arm_address output_address;
-         if(output_offset != invalid_address)
+         if (output_offset != invalid_address)
            output_address = out_sections[index]->address() + output_offset;
          else
            {
@@ -4783,7 +6118,10 @@ Arm_relobj<big_endian>::do_count_local_symbols(
       const char* sym_name = pnames + sym.get_st_name();
       if (Target_arm<big_endian>::is_mapping_symbol_name(sym_name))
        {
-         unsigned int input_shndx = sym.get_st_shndx();  
+         bool is_ordinary;
+         unsigned int input_shndx =
+           this->adjust_sym_shndx(i, sym.get_st_shndx(), &is_ordinary);
+         gold_assert(is_ordinary);
 
          // Strip of LSB in case this is a THUMB symbol.
          Mapping_symbol_position msp(input_shndx, input_value & ~1U);
@@ -4877,7 +6215,8 @@ Arm_relobj<big_endian>::do_relocate_sections(
          // Adjust view to cover section.
          Output_section* os = this->output_section(i);
          gold_assert(os != NULL);
-         Arm_address section_address = os->output_address(this, i, 0);
+         Arm_address section_address =
+           this->simple_input_section_output_address(i, os);
          uint64_t section_size = this->section_size(i);
 
          gold_assert(section_address >= view_address
@@ -4897,35 +6236,118 @@ Arm_relobj<big_endian>::do_relocate_sections(
     }
 }
 
-// Helper functions for both Arm_relobj and Arm_dynobj to read ARM
-// ABI information.
+// Find the linked text section of an EXIDX section by looking the the first
+// relocation.  4.4.1 of the EHABI specifications says that an EXIDX section
+// must be linked to to its associated code section via the sh_link field of
+// its section header.  However, some tools are broken and the link is not
+// always set.  LD just drops such an EXIDX section silently, causing the
+// associated code not unwindabled.   Here we try a little bit harder to
+// discover the linked code section.
+//
+// PSHDR points to the section header of a relocation section of an EXIDX
+// section.  If we can find a linked text section, return true and
+// store the text section index in the location PSHNDX.  Otherwise
+// return false.
 
 template<bool big_endian>
-Attributes_section_data*
-read_arm_attributes_section(
-    Object* object,
-    Read_symbols_data *sd)
+bool
+Arm_relobj<big_endian>::find_linked_text_section(
+    const unsigned char* pshdr,
+    const unsigned char* psyms,
+    unsigned int* pshndx)
 {
-  // Read the attributes section if there is one.
-  // We read from the end because gas seems to put it near the end of
-  // the section headers.
-  const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
-  const unsigned char *ps =
-    sd->section_headers->data() + shdr_size * (object->shnum() - 1);
-  for (unsigned int i = object->shnum(); i > 0; --i, ps -= shdr_size)
+  elfcpp::Shdr<32, big_endian> shdr(pshdr);
+  
+  // If there is no relocation, we cannot find the linked text section.
+  size_t reloc_size;
+  if (shdr.get_sh_type() == elfcpp::SHT_REL)
+      reloc_size = elfcpp::Elf_sizes<32>::rel_size;
+  else
+      reloc_size = elfcpp::Elf_sizes<32>::rela_size;
+  size_t reloc_count = shdr.get_sh_size() / reloc_size;
+  // Get the relocations.
+  const unsigned char* prelocs =
+      this->get_view(shdr.get_sh_offset(), shdr.get_sh_size(), true, false); 
+
+  // Find the REL31 relocation for the first word of the first EXIDX entry.
+  for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
     {
-      elfcpp::Shdr<32, big_endian> shdr(ps);
-      if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
+      Arm_address r_offset;
+      typename elfcpp::Elf_types<32>::Elf_WXword r_info;
+      if (shdr.get_sh_type() == elfcpp::SHT_REL)
        {
-         section_offset_type section_offset = shdr.get_sh_offset();
-         section_size_type section_size =
-           convert_to_section_size_type(shdr.get_sh_size());
-         File_view* view = object->get_lasting_view(section_offset,
-                                                    section_size, true, false);
-         return new Attributes_section_data(view->data(), section_size);
+         typename elfcpp::Rel<32, big_endian> reloc(prelocs);
+         r_info = reloc.get_r_info();
+         r_offset = reloc.get_r_offset();
        }
+      else
+       {
+         typename elfcpp::Rela<32, big_endian> reloc(prelocs);
+         r_info = reloc.get_r_info();
+         r_offset = reloc.get_r_offset();
+       }
+
+      unsigned int r_type = elfcpp::elf_r_type<32>(r_info);
+      if (r_type != elfcpp::R_ARM_PREL31 && r_type != elfcpp::R_ARM_SBREL31)
+       continue;
+
+      unsigned int r_sym = elfcpp::elf_r_sym<32>(r_info);
+      if (r_sym == 0
+         || r_sym >= this->local_symbol_count()
+         || r_offset != 0)
+       continue;
+
+      // This is the relocation for the first word of the first EXIDX entry.
+      // We expect to see a local section symbol.
+      const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
+      elfcpp::Sym<32, big_endian> sym(psyms + r_sym * sym_size);
+      if (sym.get_st_type() == elfcpp::STT_SECTION)
+       {
+         bool is_ordinary;
+         *pshndx =
+           this->adjust_sym_shndx(r_sym, sym.get_st_shndx(), &is_ordinary);
+         gold_assert(is_ordinary);
+         return true;
+       }
+      else
+       return false;
+    }
+
+  return false;
+}
+
+// Make an EXIDX input section object for an EXIDX section whose index is
+// SHNDX.  SHDR is the section header of the EXIDX section and TEXT_SHNDX
+// is the section index of the linked text section.
+
+template<bool big_endian>
+void
+Arm_relobj<big_endian>::make_exidx_input_section(
+    unsigned int shndx,
+    const elfcpp::Shdr<32, big_endian>& shdr,
+    unsigned int text_shndx)
+{
+  // Issue an error and ignore this EXIDX section if it points to a text
+  // section already has an EXIDX section.
+  if (this->exidx_section_map_[text_shndx] != NULL)
+    {
+      gold_error(_("EXIDX sections %u and %u both link to text section %u "
+                  "in %s"),
+                shndx, this->exidx_section_map_[text_shndx]->shndx(),
+                text_shndx, this->name().c_str());
+      return;
     }
-  return NULL;
+
+  // Create an Arm_exidx_input_section object for this EXIDX section.
+  Arm_exidx_input_section* exidx_input_section =
+    new Arm_exidx_input_section(this, shndx, text_shndx, shdr.get_sh_size(),
+                               shdr.get_sh_addralign());
+  this->exidx_section_map_[text_shndx] = exidx_input_section;
+
+  // Also map the EXIDX section index to this.
+  gold_assert(this->exidx_section_map_[shndx] == NULL);
+  this->exidx_section_map_[shndx] = exidx_input_section;
 }
 
 // Read the symbol information.
@@ -4937,14 +6359,143 @@ Arm_relobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
   // Call parent class to read symbol information.
   Sized_relobj<32, big_endian>::do_read_symbols(sd);
 
+  // If this input file is a binary file, it has no processor
+  // specific flags and attributes section.
+  Input_file::Format format = this->input_file()->format();
+  if (format != Input_file::FORMAT_ELF)
+    {
+      gold_assert(format == Input_file::FORMAT_BINARY);
+      this->merge_flags_and_attributes_ = false;
+      return;
+    }
+
   // Read processor-specific flags in ELF file header.
   const unsigned char* pehdr = this->get_view(elfcpp::file_header_offset,
                                              elfcpp::Elf_sizes<32>::ehdr_size,
                                              true, false);
   elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
   this->processor_specific_flags_ = ehdr.get_e_flags();
-  this->attributes_section_data_ =
-    read_arm_attributes_section<big_endian>(this, sd); 
+
+  // Go over the section headers and look for .ARM.attributes and .ARM.exidx
+  // sections.
+  std::vector<unsigned int> deferred_exidx_sections;
+  const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
+  const unsigned char* pshdrs = sd->section_headers->data();
+  const unsigned char *ps = pshdrs + shdr_size;
+  bool must_merge_flags_and_attributes = false;
+  for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
+    {
+      elfcpp::Shdr<32, big_endian> shdr(ps);
+
+      // Sometimes an object has no contents except the section name string
+      // table and an empty symbol table with the undefined symbol.  We
+      // don't want to merge processor-specific flags from such an object.
+      if (shdr.get_sh_type() == elfcpp::SHT_SYMTAB)
+       {
+         // Symbol table is not empty.
+         const elfcpp::Elf_types<32>::Elf_WXword sym_size =
+            elfcpp::Elf_sizes<32>::sym_size;
+         if (shdr.get_sh_size() > sym_size)
+           must_merge_flags_and_attributes = true;
+       }
+      else if (shdr.get_sh_type() != elfcpp::SHT_STRTAB)
+       // If this is neither an empty symbol table nor a string table,
+       // be conservative.
+       must_merge_flags_and_attributes = true;
+
+      if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
+       {
+         gold_assert(this->attributes_section_data_ == NULL);
+         section_offset_type section_offset = shdr.get_sh_offset();
+         section_size_type section_size =
+           convert_to_section_size_type(shdr.get_sh_size());
+         File_view* view = this->get_lasting_view(section_offset,
+                                                  section_size, true, false);
+         this->attributes_section_data_ =
+           new Attributes_section_data(view->data(), section_size);
+       }
+      else if (shdr.get_sh_type() == elfcpp::SHT_ARM_EXIDX)
+       {
+         unsigned int text_shndx = this->adjust_shndx(shdr.get_sh_link());
+         if (text_shndx >= this->shnum())
+           gold_error(_("EXIDX section %u linked to invalid section %u"),
+                      i, text_shndx);
+         else if (text_shndx == elfcpp::SHN_UNDEF)
+           deferred_exidx_sections.push_back(i);
+         else
+           this->make_exidx_input_section(i, shdr, text_shndx);
+       }
+    }
+
+  // This is rare.
+  if (!must_merge_flags_and_attributes)
+    {
+      this->merge_flags_and_attributes_ = false;
+      return;
+    }
+
+  // Some tools are broken and they do not set the link of EXIDX sections. 
+  // We look at the first relocation to figure out the linked sections.
+  if (!deferred_exidx_sections.empty())
+    {
+      // We need to go over the section headers again to find the mapping
+      // from sections being relocated to their relocation sections.  This is
+      // a bit inefficient as we could do that in the loop above.  However,
+      // we do not expect any deferred EXIDX sections normally.  So we do not
+      // want to slow down the most common path.
+      typedef Unordered_map<unsigned int, unsigned int> Reloc_map;
+      Reloc_map reloc_map;
+      ps = pshdrs + shdr_size;
+      for (unsigned int i = 1; i < this->shnum(); ++i, ps += shdr_size)
+       {
+         elfcpp::Shdr<32, big_endian> shdr(ps);
+         elfcpp::Elf_Word sh_type = shdr.get_sh_type();
+         if (sh_type == elfcpp::SHT_REL || sh_type == elfcpp::SHT_RELA)
+           {
+             unsigned int info_shndx = this->adjust_shndx(shdr.get_sh_info());
+             if (info_shndx >= this->shnum())
+               gold_error(_("relocation section %u has invalid info %u"),
+                          i, info_shndx);
+             Reloc_map::value_type value(info_shndx, i);
+             std::pair<Reloc_map::iterator, bool> result =
+               reloc_map.insert(value);
+             if (!result.second)
+               gold_error(_("section %u has multiple relocation sections "
+                            "%u and %u"),
+                          info_shndx, i, reloc_map[info_shndx]);
+           }
+       }
+
+      // Read the symbol table section header.
+      const unsigned int symtab_shndx = this->symtab_shndx();
+      elfcpp::Shdr<32, big_endian>
+         symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
+      gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
+
+      // Read the local symbols.
+      const int sym_size =elfcpp::Elf_sizes<32>::sym_size;
+      const unsigned int loccount = this->local_symbol_count();
+      gold_assert(loccount == symtabshdr.get_sh_info());
+      off_t locsize = loccount * sym_size;
+      const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
+                                                 locsize, true, true);
+
+      // Process the deferred EXIDX sections. 
+      for(unsigned int i = 0; i < deferred_exidx_sections.size(); ++i)
+       {
+         unsigned int shndx = deferred_exidx_sections[i];
+         elfcpp::Shdr<32, big_endian> shdr(pshdrs + shndx * shdr_size);
+         unsigned int text_shndx;
+         Reloc_map::const_iterator it = reloc_map.find(shndx);
+         if (it != reloc_map.end()
+             && find_linked_text_section(pshdrs + it->second * shdr_size,
+                                         psyms, &text_shndx))
+           this->make_exidx_input_section(shndx, shdr, text_shndx);
+         else
+           gold_error(_("EXIDX section %u has no linked text section."),
+                      shndx);
+       }
+    }
 }
 
 // Process relocations for garbage collection.  The ARM target uses .ARM.exidx
@@ -4963,6 +6514,11 @@ Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
   // First, call base class method to process relocations in this object.
   Sized_relobj<32, big_endian>::do_gc_process_relocs(symtab, layout, rd);
 
+  // If --gc-sections is not specified, there is nothing more to do.
+  // This happens when --icf is used but --gc-sections is not.
+  if (!parameters->options().gc_sections())
+    return;
+  
   unsigned int shnum = this->shnum();
   const unsigned int shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
   const unsigned char* pshdrs = this->get_view(this->elf_file()->shoff(),
@@ -4985,6 +6541,99 @@ Arm_relobj<big_endian>::do_gc_process_relocs(Symbol_table* symtab,
     }
 }
 
+// Update output local symbol count.  Owing to EXIDX entry merging, some local
+// symbols  will be removed in output.  Adjust output local symbol count
+// accordingly.  We can only changed the static output local symbol count.  It
+// is too late to change the dynamic symbols.
+
+template<bool big_endian>
+void
+Arm_relobj<big_endian>::update_output_local_symbol_count()
+{
+  // Caller should check that this needs updating.  We want caller checking
+  // because output_local_symbol_count_needs_update() is most likely inlined.
+  gold_assert(this->output_local_symbol_count_needs_update_);
+
+  gold_assert(this->symtab_shndx() != -1U);
+  if (this->symtab_shndx() == 0)
+    {
+      // This object has no symbols.  Weird but legal.
+      return;
+    }
+
+  // Read the symbol table section header.
+  const unsigned int symtab_shndx = this->symtab_shndx();
+  elfcpp::Shdr<32, big_endian>
+    symtabshdr(this, this->elf_file()->section_header(symtab_shndx));
+  gold_assert(symtabshdr.get_sh_type() == elfcpp::SHT_SYMTAB);
+
+  // Read the local symbols.
+  const int sym_size = elfcpp::Elf_sizes<32>::sym_size;
+  const unsigned int loccount = this->local_symbol_count();
+  gold_assert(loccount == symtabshdr.get_sh_info());
+  off_t locsize = loccount * sym_size;
+  const unsigned char* psyms = this->get_view(symtabshdr.get_sh_offset(),
+                                             locsize, true, true);
+
+  // Loop over the local symbols.
+
+  typedef typename Sized_relobj<32, big_endian>::Output_sections
+     Output_sections;
+  const Output_sections& out_sections(this->output_sections());
+  unsigned int shnum = this->shnum();
+  unsigned int count = 0;
+  // Skip the first, dummy, symbol.
+  psyms += sym_size;
+  for (unsigned int i = 1; i < loccount; ++i, psyms += sym_size)
+    {
+      elfcpp::Sym<32, big_endian> sym(psyms);
+
+      Symbol_value<32>& lv((*this->local_values())[i]);
+
+      // This local symbol was already discarded by do_count_local_symbols.
+      if (lv.is_output_symtab_index_set() && !lv.has_output_symtab_entry())
+       continue;
+
+      bool is_ordinary;
+      unsigned int shndx = this->adjust_sym_shndx(i, sym.get_st_shndx(),
+                                                 &is_ordinary);
+
+      if (shndx < shnum)
+       {
+         Output_section* os = out_sections[shndx];
+
+         // This local symbol no longer has an output section.  Discard it.
+         if (os == NULL)
+           {
+             lv.set_no_output_symtab_entry();
+             continue;
+           }
+
+         // Currently we only discard parts of EXIDX input sections.
+         // We explicitly check for a merged EXIDX input section to avoid
+         // calling Output_section_data::output_offset unless necessary.
+         if ((this->get_output_section_offset(shndx) == invalid_address)
+             && (this->exidx_input_section_by_shndx(shndx) != NULL))
+           {
+             section_offset_type output_offset =
+               os->output_offset(this, shndx, lv.input_value());
+             if (output_offset == -1)
+               {
+                 // This symbol is defined in a part of an EXIDX input section
+                 // that is discarded due to entry merging.
+                 lv.set_no_output_symtab_entry();
+                 continue;
+               }       
+           }
+       }
+
+      ++count;
+    }
+
+  this->set_output_local_symbol_count(count);
+  this->output_local_symbol_count_needs_update_ = false;
+}
+
 // Arm_dynobj methods.
 
 // Read the symbol information.
@@ -5002,8 +6651,28 @@ Arm_dynobj<big_endian>::do_read_symbols(Read_symbols_data* sd)
                                              true, false);
   elfcpp::Ehdr<32, big_endian> ehdr(pehdr);
   this->processor_specific_flags_ = ehdr.get_e_flags();
-  this->attributes_section_data_ =
-    read_arm_attributes_section<big_endian>(this, sd); 
+
+  // Read the attributes section if there is one.
+  // We read from the end because gas seems to put it near the end of
+  // the section headers.
+  const size_t shdr_size = elfcpp::Elf_sizes<32>::shdr_size;
+  const unsigned char *ps =
+    sd->section_headers->data() + shdr_size * (this->shnum() - 1);
+  for (unsigned int i = this->shnum(); i > 0; --i, ps -= shdr_size)
+    {
+      elfcpp::Shdr<32, big_endian> shdr(ps);
+      if (shdr.get_sh_type() == elfcpp::SHT_ARM_ATTRIBUTES)
+       {
+         section_offset_type section_offset = shdr.get_sh_offset();
+         section_size_type section_size =
+           convert_to_section_size_type(shdr.get_sh_size());
+         File_view* view = this->get_lasting_view(section_offset,
+                                                  section_size, true, false);
+         this->attributes_section_data_ =
+           new Attributes_section_data(view->data(), section_size);
+         break;
+       }
+    }
 }
 
 // Stub_addend_reader methods.
@@ -5056,6 +6725,162 @@ Stub_addend_reader<elfcpp::SHT_REL, big_endian>::operator()(
     }
 }
 
+// Arm_output_data_got methods.
+
+// Add a GOT pair for R_ARM_TLS_GD32.  The creates a pair of GOT entries.
+// The first one is initialized to be 1, which is the module index for
+// the main executable and the second one 0.  A reloc of the type
+// R_ARM_TLS_DTPOFF32 will be created for the second GOT entry and will
+// be applied by gold.  GSYM is a global symbol.
+//
+template<bool big_endian>
+void
+Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
+    unsigned int got_type,
+    Symbol* gsym)
+{
+  if (gsym->has_got_offset(got_type))
+    return;
+
+  // We are doing a static link.  Just mark it as belong to module 1,
+  // the executable.
+  unsigned int got_offset = this->add_constant(1);
+  gsym->set_got_offset(got_type, got_offset); 
+  got_offset = this->add_constant(0);
+  this->static_relocs_.push_back(Static_reloc(got_offset,
+                                             elfcpp::R_ARM_TLS_DTPOFF32,
+                                             gsym));
+}
+
+// Same as the above but for a local symbol.
+
+template<bool big_endian>
+void
+Arm_output_data_got<big_endian>::add_tls_gd32_with_static_reloc(
+  unsigned int got_type,
+  Sized_relobj<32, big_endian>* object,
+  unsigned int index)
+{
+  if (object->local_has_got_offset(index, got_type))
+    return;
+
+  // We are doing a static link.  Just mark it as belong to module 1,
+  // the executable.
+  unsigned int got_offset = this->add_constant(1);
+  object->set_local_got_offset(index, got_type, got_offset);
+  got_offset = this->add_constant(0);
+  this->static_relocs_.push_back(Static_reloc(got_offset, 
+                                             elfcpp::R_ARM_TLS_DTPOFF32, 
+                                             object, index));
+}
+
+template<bool big_endian>
+void
+Arm_output_data_got<big_endian>::do_write(Output_file* of)
+{
+  // Call parent to write out GOT.
+  Output_data_got<32, big_endian>::do_write(of);
+
+  // We are done if there is no fix up.
+  if (this->static_relocs_.empty())
+    return;
+
+  gold_assert(parameters->doing_static_link());
+
+  const off_t offset = this->offset();
+  const section_size_type oview_size =
+    convert_to_section_size_type(this->data_size());
+  unsigned char* const oview = of->get_output_view(offset, oview_size);
+
+  Output_segment* tls_segment = this->layout_->tls_segment();
+  gold_assert(tls_segment != NULL);
+  
+  // The thread pointer $tp points to the TCB, which is followed by the
+  // TLS.  So we need to adjust $tp relative addressing by this amount.
+  Arm_address aligned_tcb_size =
+    align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
+
+  for (size_t i = 0; i < this->static_relocs_.size(); ++i)
+    {
+      Static_reloc& reloc(this->static_relocs_[i]);
+      
+      Arm_address value;
+      if (!reloc.symbol_is_global())
+       {
+         Sized_relobj<32, big_endian>* object = reloc.relobj();
+         const Symbol_value<32>* psymval =
+           reloc.relobj()->local_symbol(reloc.index());
+
+         // We are doing static linking.  Issue an error and skip this
+         // relocation if the symbol is undefined or in a discarded_section.
+         bool is_ordinary;
+         unsigned int shndx = psymval->input_shndx(&is_ordinary);
+         if ((shndx == elfcpp::SHN_UNDEF)
+             || (is_ordinary
+                 && shndx != elfcpp::SHN_UNDEF
+                 && !object->is_section_included(shndx)
+                 && !this->symbol_table_->is_section_folded(object, shndx)))
+           {
+             gold_error(_("undefined or discarded local symbol %u from "
+                          " object %s in GOT"),
+                        reloc.index(), reloc.relobj()->name().c_str());
+             continue;
+           }
+         
+         value = psymval->value(object, 0);
+       }
+      else
+       {
+         const Symbol* gsym = reloc.symbol();
+         gold_assert(gsym != NULL);
+         if (gsym->is_forwarder())
+           gsym = this->symbol_table_->resolve_forwards(gsym);
+
+         // We are doing static linking.  Issue an error and skip this
+         // relocation if the symbol is undefined or in a discarded_section
+         // unless it is a weakly_undefined symbol.
+         if ((gsym->is_defined_in_discarded_section()
+              || gsym->is_undefined())
+             && !gsym->is_weak_undefined())
+           {
+             gold_error(_("undefined or discarded symbol %s in GOT"),
+                        gsym->name());
+             continue;
+           }
+
+         if (!gsym->is_weak_undefined())
+           {
+             const Sized_symbol<32>* sym =
+               static_cast<const Sized_symbol<32>*>(gsym);
+             value = sym->value();
+           }
+         else
+             value = 0;
+       }
+
+      unsigned got_offset = reloc.got_offset();
+      gold_assert(got_offset < oview_size);
+
+      typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+      Valtype* wv = reinterpret_cast<Valtype*>(oview + got_offset);
+      Valtype x;
+      switch (reloc.r_type())
+       {
+       case elfcpp::R_ARM_TLS_DTPOFF32:
+         x = value;
+         break;
+       case elfcpp::R_ARM_TLS_TPOFF32:
+         x = value + aligned_tcb_size;
+         break;
+       default:
+         gold_unreachable();
+       }
+      elfcpp::Swap<32, big_endian>::writeval(wv, x);
+    }
+
+  of->write_output_view(offset, oview_size, oview);
+}
+
 // A class to handle the PLT data.
 
 template<bool big_endian>
@@ -5291,6 +7116,90 @@ Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
   this->plt_->add_entry(gsym);
 }
 
+// Get the section to use for TLS_DESC relocations.
+
+template<bool big_endian>
+typename Target_arm<big_endian>::Reloc_section*
+Target_arm<big_endian>::rel_tls_desc_section(Layout* layout) const
+{
+  return this->plt_section()->rel_tls_desc(layout);
+}
+
+// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
+
+template<bool big_endian>
+void
+Target_arm<big_endian>::define_tls_base_symbol(
+    Symbol_table* symtab,
+    Layout* layout)
+{
+  if (this->tls_base_symbol_defined_)
+    return;
+
+  Output_segment* tls_segment = layout->tls_segment();
+  if (tls_segment != NULL)
+    {
+      bool is_exec = parameters->options().output_is_executable();
+      symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
+                                      Symbol_table::PREDEFINED,
+                                      tls_segment, 0, 0,
+                                      elfcpp::STT_TLS,
+                                      elfcpp::STB_LOCAL,
+                                      elfcpp::STV_HIDDEN, 0,
+                                      (is_exec
+                                       ? Symbol::SEGMENT_END
+                                       : Symbol::SEGMENT_START),
+                                      true);
+    }
+  this->tls_base_symbol_defined_ = true;
+}
+
+// Create a GOT entry for the TLS module index.
+
+template<bool big_endian>
+unsigned int
+Target_arm<big_endian>::got_mod_index_entry(
+    Symbol_table* symtab,
+    Layout* layout,
+    Sized_relobj<32, big_endian>* object)
+{
+  if (this->got_mod_index_offset_ == -1U)
+    {
+      gold_assert(symtab != NULL && layout != NULL && object != NULL);
+      Arm_output_data_got<big_endian>* got = this->got_section(symtab, layout);
+      unsigned int got_offset;
+      if (!parameters->doing_static_link())
+       {
+         got_offset = got->add_constant(0);
+         Reloc_section* rel_dyn = this->rel_dyn_section(layout);
+         rel_dyn->add_local(object, 0, elfcpp::R_ARM_TLS_DTPMOD32, got,
+                            got_offset);
+       }
+      else
+       {
+         // We are doing a static link.  Just mark it as belong to module 1,
+         // the executable.
+         got_offset = got->add_constant(1);
+       }
+
+      got->add_constant(0);
+      this->got_mod_index_offset_ = got_offset;
+    }
+  return this->got_mod_index_offset_;
+}
+
+// Optimize the TLS relocation type based on what we know about the
+// symbol.  IS_FINAL is true if the final address of this symbol is
+// known at link time.
+
+template<bool big_endian>
+tls::Tls_optimization
+Target_arm<big_endian>::optimize_tls_reloc(bool, int)
+{
+  // FIXME: Currently we do not do any TLS optimization.
+  return tls::TLSOPT_NONE;
+}
+
 // Report an unsupported relocation against a local symbol.
 
 template<bool big_endian>
@@ -5334,15 +7243,21 @@ Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
       return;
 
     default:
-      // This prevents us from issuing more than one error per reloc
-      // section.  But we can still wind up issuing more than one
-      // error per object file.
-      if (this->issued_non_pic_error_)
+      {
+       // This prevents us from issuing more than one error per reloc
+       // section.  But we can still wind up issuing more than one
+       // error per object file.
+       if (this->issued_non_pic_error_)
+         return;
+       const Arm_reloc_property* reloc_property =
+         arm_reloc_property_table->get_reloc_property(r_type);
+       gold_assert(reloc_property != NULL);
+       object->error(_("requires unsupported dynamic reloc %s; "
+                     "recompile with -fPIC"),
+                     reloc_property->name().c_str());
+       this->issued_non_pic_error_ = true;
        return;
-      object->error(_("requires unsupported dynamic reloc; "
-                     "recompile with -fPIC"));
-      this->issued_non_pic_error_ = true;
-      return;
+      }
 
     case elfcpp::R_ARM_NONE:
       gold_unreachable();
@@ -5363,12 +7278,15 @@ Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
                                    Output_section* output_section,
                                    const elfcpp::Rel<32, big_endian>& reloc,
                                    unsigned int r_type,
-                                   const elfcpp::Sym<32, big_endian>&)
+                                   const elfcpp::Sym<32, big_endian>& lsym)
 {
   r_type = get_real_reloc_type(r_type);
   switch (r_type)
     {
     case elfcpp::R_ARM_NONE:
+    case elfcpp::R_ARM_V4BX:
+    case elfcpp::R_ARM_GNU_VTENTRY:
+    case elfcpp::R_ARM_GNU_VTINHERIT:
       break;
 
     case elfcpp::R_ARM_ABS32:
@@ -5391,47 +7309,121 @@ Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
        }
       break;
 
-    case elfcpp::R_ARM_REL32:
-    case elfcpp::R_ARM_THM_CALL:
-    case elfcpp::R_ARM_CALL:
-    case elfcpp::R_ARM_PREL31:
-    case elfcpp::R_ARM_JUMP24:
-    case elfcpp::R_ARM_THM_JUMP24:
-    case elfcpp::R_ARM_THM_JUMP19:
-    case elfcpp::R_ARM_PLT32:
+    case elfcpp::R_ARM_ABS16:
+    case elfcpp::R_ARM_ABS12:
     case elfcpp::R_ARM_THM_ABS5:
     case elfcpp::R_ARM_ABS8:
-    case elfcpp::R_ARM_ABS12:
-    case elfcpp::R_ARM_ABS16:
     case elfcpp::R_ARM_BASE_ABS:
     case elfcpp::R_ARM_MOVW_ABS_NC:
     case elfcpp::R_ARM_MOVT_ABS:
     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
     case elfcpp::R_ARM_THM_MOVT_ABS:
+      // If building a shared library (or a position-independent
+      // executable), we need to create a dynamic relocation for
+      // this location. Because the addend needs to remain in the
+      // data section, we need to be careful not to apply this
+      // relocation statically.
+      if (parameters->options().output_is_position_independent())
+        {
+         check_non_pic(object, r_type);
+          Reloc_section* rel_dyn = target->rel_dyn_section(layout);
+         unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
+          if (lsym.get_st_type() != elfcpp::STT_SECTION)
+           rel_dyn->add_local(object, r_sym, r_type, output_section,
+                              data_shndx, reloc.get_r_offset());
+          else
+            {
+              gold_assert(lsym.get_st_value() == 0);
+             unsigned int shndx = lsym.get_st_shndx();
+             bool is_ordinary;
+             shndx = object->adjust_sym_shndx(r_sym, shndx,
+                                              &is_ordinary);
+             if (!is_ordinary)
+               object->error(_("section symbol %u has bad shndx %u"),
+                             r_sym, shndx);
+             else
+               rel_dyn->add_local_section(object, shndx,
+                                          r_type, output_section,
+                                          data_shndx, reloc.get_r_offset());
+            }
+        }
+      break;
+
+    case elfcpp::R_ARM_PC24:
+    case elfcpp::R_ARM_REL32:
+    case elfcpp::R_ARM_LDR_PC_G0:
+    case elfcpp::R_ARM_SBREL32:
+    case elfcpp::R_ARM_THM_CALL:
+    case elfcpp::R_ARM_THM_PC8:
+    case elfcpp::R_ARM_BASE_PREL:
+    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_LDR_SBREL_11_0_NC:
+    case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
+    case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
+    case elfcpp::R_ARM_SBREL31:
+    case elfcpp::R_ARM_PREL31:
     case elfcpp::R_ARM_MOVW_PREL_NC:
     case elfcpp::R_ARM_MOVT_PREL:
     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
     case elfcpp::R_ARM_THM_MOVT_PREL:
+    case elfcpp::R_ARM_THM_JUMP19:
     case elfcpp::R_ARM_THM_JUMP6:
-    case elfcpp::R_ARM_THM_JUMP8:
+    case elfcpp::R_ARM_THM_ALU_PREL_11_0:
+    case elfcpp::R_ARM_THM_PC12:
+    case elfcpp::R_ARM_REL32_NOI:
+    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_LDR_PC_G1:
+    case elfcpp::R_ARM_LDR_PC_G2:
+    case elfcpp::R_ARM_LDRS_PC_G0:
+    case elfcpp::R_ARM_LDRS_PC_G1:
+    case elfcpp::R_ARM_LDRS_PC_G2:
+    case elfcpp::R_ARM_LDC_PC_G0:
+    case elfcpp::R_ARM_LDC_PC_G1:
+    case elfcpp::R_ARM_LDC_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_LDR_SB_G0:
+    case elfcpp::R_ARM_LDR_SB_G1:
+    case elfcpp::R_ARM_LDR_SB_G2:
+    case elfcpp::R_ARM_LDRS_SB_G0:
+    case elfcpp::R_ARM_LDRS_SB_G1:
+    case elfcpp::R_ARM_LDRS_SB_G2:
+    case elfcpp::R_ARM_LDC_SB_G0:
+    case elfcpp::R_ARM_LDC_SB_G1:
+    case elfcpp::R_ARM_LDC_SB_G2:
+    case elfcpp::R_ARM_MOVW_BREL_NC:
+    case elfcpp::R_ARM_MOVT_BREL:
+    case elfcpp::R_ARM_MOVW_BREL:
+    case elfcpp::R_ARM_THM_MOVW_BREL_NC:
+    case elfcpp::R_ARM_THM_MOVT_BREL:
+    case elfcpp::R_ARM_THM_MOVW_BREL:
     case elfcpp::R_ARM_THM_JUMP11:
-    case elfcpp::R_ARM_V4BX:
+    case elfcpp::R_ARM_THM_JUMP8:
+      // We don't need to do anything for a relative addressing relocation
+      // against a local symbol if it does not reference the GOT.
       break;
 
     case elfcpp::R_ARM_GOTOFF32:
+    case elfcpp::R_ARM_GOTOFF12:
       // We need a GOT section:
       target->got_section(symtab, layout);
       break;
 
-    case elfcpp::R_ARM_BASE_PREL:
-      // FIXME: What about this?
-      break;
-
     case elfcpp::R_ARM_GOT_BREL:
     case elfcpp::R_ARM_GOT_PREL:
       {
        // The symbol requires a GOT entry.
-       Output_data_got<32, big_endian>* got =
+       Arm_output_data_got<big_endian>* got =
          target->got_section(symtab, layout);
        unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
        if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
@@ -5451,6 +7443,7 @@ Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
       break;
 
     case elfcpp::R_ARM_TARGET1:
+    case elfcpp::R_ARM_TARGET2:
       // This should have been mapped to another type already.
       // Fall through.
     case elfcpp::R_ARM_COPY:
@@ -5463,6 +7456,116 @@ Target_arm<big_endian>::Scan::local(Symbol_table* symtab,
                 object->name().c_str(), r_type);
       break;
 
+
+      // These are initial TLS relocs, which are expected when
+      // linking.
+    case elfcpp::R_ARM_TLS_GD32:       // Global-dynamic
+    case elfcpp::R_ARM_TLS_LDM32:      // Local-dynamic
+    case elfcpp::R_ARM_TLS_LDO32:      // Alternate local-dynamic
+    case elfcpp::R_ARM_TLS_IE32:       // Initial-exec
+    case elfcpp::R_ARM_TLS_LE32:       // Local-exec
+      {
+       bool output_is_shared = parameters->options().shared();
+       const tls::Tls_optimization optimized_type
+            = Target_arm<big_endian>::optimize_tls_reloc(!output_is_shared,
+                                                        r_type);
+       switch (r_type)
+         {
+         case elfcpp::R_ARM_TLS_GD32:          // Global-dynamic
+           if (optimized_type == tls::TLSOPT_NONE)
+             {
+               // Create a pair of GOT entries for the module index and
+               // dtv-relative offset.
+                Arm_output_data_got<big_endian>* got
+                    = target->got_section(symtab, layout);
+                unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
+               unsigned int shndx = lsym.get_st_shndx();
+               bool is_ordinary;
+               shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
+               if (!is_ordinary)
+                 {
+                   object->error(_("local symbol %u has bad shndx %u"),
+                                 r_sym, shndx);
+                   break;
+                 }
+
+               if (!parameters->doing_static_link())
+                 got->add_local_pair_with_rel(object, r_sym, shndx,
+                                              GOT_TYPE_TLS_PAIR,
+                                              target->rel_dyn_section(layout),
+                                              elfcpp::R_ARM_TLS_DTPMOD32, 0);
+               else
+                 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR,
+                                                     object, r_sym);
+             }
+           else
+             // FIXME: TLS optimization not supported yet.
+             gold_unreachable();
+           break;
+
+         case elfcpp::R_ARM_TLS_LDM32:         // Local-dynamic
+           if (optimized_type == tls::TLSOPT_NONE)
+             {
+               // Create a GOT entry for the module index.
+               target->got_mod_index_entry(symtab, layout, object);
+             }
+           else
+             // FIXME: TLS optimization not supported yet.
+             gold_unreachable();
+           break;
+
+         case elfcpp::R_ARM_TLS_LDO32:         // Alternate local-dynamic
+           break;
+
+         case elfcpp::R_ARM_TLS_IE32:          // Initial-exec
+           layout->set_has_static_tls();
+           if (optimized_type == tls::TLSOPT_NONE)
+             {
+               // Create a GOT entry for the tp-relative offset.
+               Arm_output_data_got<big_endian>* got
+                 = target->got_section(symtab, layout);
+               unsigned int r_sym =
+                  elfcpp::elf_r_sym<32>(reloc.get_r_info());
+               if (!parameters->doing_static_link())
+                   got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
+                                           target->rel_dyn_section(layout),
+                                           elfcpp::R_ARM_TLS_TPOFF32);
+               else if (!object->local_has_got_offset(r_sym,
+                                                      GOT_TYPE_TLS_OFFSET))
+                 {
+                   got->add_local(object, r_sym, GOT_TYPE_TLS_OFFSET);
+                   unsigned int got_offset =
+                     object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET);
+                   got->add_static_reloc(got_offset,
+                                         elfcpp::R_ARM_TLS_TPOFF32, object,
+                                         r_sym);
+                 }
+             }
+           else
+             // FIXME: TLS optimization not supported yet.
+             gold_unreachable();
+           break;
+
+         case elfcpp::R_ARM_TLS_LE32:          // Local-exec
+           layout->set_has_static_tls();
+           if (output_is_shared)
+             {
+               // We need to create a dynamic relocation.
+                gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
+                unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
+               Reloc_section* rel_dyn = target->rel_dyn_section(layout);
+               rel_dyn->add_local(object, r_sym, elfcpp::R_ARM_TLS_TPOFF32,
+                                  output_section, data_shndx,
+                                  reloc.get_r_offset());
+             }
+           break;
+
+         default:
+           gold_unreachable();
+         }
+      }
+      break;
+
     default:
       unsupported_reloc_local(object, r_type);
       break;
@@ -5483,8 +7586,6 @@ Target_arm<big_endian>::Scan::unsupported_reloc_global(
 }
 
 // Scan a relocation for a global symbol.
-// FIXME: This only handles a subset of relocation types used by Android
-// on ARM v5te devices.
 
 template<bool big_endian>
 inline void
@@ -5498,77 +7599,129 @@ Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
                                     unsigned int r_type,
                                     Symbol* gsym)
 {
+  // A reference to _GLOBAL_OFFSET_TABLE_ implies that we need a got
+  // section.  We check here to avoid creating a dynamic reloc against
+  // _GLOBAL_OFFSET_TABLE_.
+  if (!target->has_got_section()
+      && strcmp(gsym->name(), "_GLOBAL_OFFSET_TABLE_") == 0)
+    target->got_section(symtab, layout);
+
   r_type = get_real_reloc_type(r_type);
   switch (r_type)
     {
     case elfcpp::R_ARM_NONE:
+    case elfcpp::R_ARM_V4BX:
+    case elfcpp::R_ARM_GNU_VTENTRY:
+    case elfcpp::R_ARM_GNU_VTINHERIT:
       break;
 
     case elfcpp::R_ARM_ABS32:
-    case elfcpp::R_ARM_ABS32_NOI:
-      {
-       // Make a dynamic relocation if necessary.
-       if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
-         {
-           if (target->may_need_copy_reloc(gsym))
-             {
-               target->copy_reloc(symtab, layout, object,
-                                  data_shndx, output_section, gsym, reloc);
-             }
-           else if (gsym->can_use_relative_reloc(false))
-             {
-               // If we are to add more other reloc types than R_ARM_ABS32,
-               // we need to add check_non_pic(object, r_type) here.
-               Reloc_section* rel_dyn = target->rel_dyn_section(layout);
-               rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
-                                            output_section, object,
-                                            data_shndx, reloc.get_r_offset());
-             }
-           else
-             {
-               // If we are to add more other reloc types than R_ARM_ABS32,
-               // we need to add check_non_pic(object, r_type) here.
-               Reloc_section* rel_dyn = target->rel_dyn_section(layout);
-               rel_dyn->add_global(gsym, r_type, output_section, object,
-                                   data_shndx, reloc.get_r_offset());
-             }
-         }
-      }
-      break;
-
+    case elfcpp::R_ARM_ABS16:
+    case elfcpp::R_ARM_ABS12:
+    case elfcpp::R_ARM_THM_ABS5:
+    case elfcpp::R_ARM_ABS8:
+    case elfcpp::R_ARM_BASE_ABS:
     case elfcpp::R_ARM_MOVW_ABS_NC:
     case elfcpp::R_ARM_MOVT_ABS:
     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
     case elfcpp::R_ARM_THM_MOVT_ABS:
-    case elfcpp::R_ARM_MOVW_PREL_NC:
-    case elfcpp::R_ARM_MOVT_PREL:
-    case elfcpp::R_ARM_THM_MOVW_PREL_NC:
-    case elfcpp::R_ARM_THM_MOVT_PREL:
-    case elfcpp::R_ARM_THM_JUMP6:
-    case elfcpp::R_ARM_THM_JUMP8:
-    case elfcpp::R_ARM_THM_JUMP11:
-    case elfcpp::R_ARM_V4BX:
-      break;
-
-    case elfcpp::R_ARM_THM_ABS5:
-    case elfcpp::R_ARM_ABS8:
-    case elfcpp::R_ARM_ABS12:
-    case elfcpp::R_ARM_ABS16:
-    case elfcpp::R_ARM_BASE_ABS:
+    case elfcpp::R_ARM_ABS32_NOI:
+      // Absolute addressing relocations.
       {
-       // No dynamic relocs of this kinds.
-       // Report the error in case of PIC.
-       int flags = Symbol::NON_PIC_REF;
-       if (gsym->type() == elfcpp::STT_FUNC
-           || gsym->type() == elfcpp::STT_ARM_TFUNC)
-         flags |= Symbol::FUNCTION_CALL;
-       if (gsym->needs_dynamic_reloc(flags))
-         check_non_pic(object, r_type);
+        // Make a PLT entry if necessary.
+        if (this->symbol_needs_plt_entry(gsym))
+          {
+            target->make_plt_entry(symtab, layout, gsym);
+            // Since this is not a PC-relative relocation, we may be
+            // taking the address of a function. In that case we need to
+            // set the entry in the dynamic symbol table to the address of
+            // the PLT entry.
+            if (gsym->is_from_dynobj() && !parameters->options().shared())
+              gsym->set_needs_dynsym_value();
+          }
+        // Make a dynamic relocation if necessary.
+        if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
+          {
+            if (gsym->may_need_copy_reloc())
+              {
+               target->copy_reloc(symtab, layout, object,
+                                  data_shndx, output_section, gsym, reloc);
+              }
+            else if ((r_type == elfcpp::R_ARM_ABS32
+                     || r_type == elfcpp::R_ARM_ABS32_NOI)
+                     && gsym->can_use_relative_reloc(false))
+              {
+                Reloc_section* rel_dyn = target->rel_dyn_section(layout);
+                rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
+                                             output_section, object,
+                                             data_shndx, reloc.get_r_offset());
+              }
+            else
+              {
+               check_non_pic(object, r_type);
+                Reloc_section* rel_dyn = target->rel_dyn_section(layout);
+                rel_dyn->add_global(gsym, r_type, output_section, object,
+                                    data_shndx, reloc.get_r_offset());
+              }
+          }
       }
       break;
 
+    case elfcpp::R_ARM_GOTOFF32:
+    case elfcpp::R_ARM_GOTOFF12:
+      // We need a GOT section.
+      target->got_section(symtab, layout);
+      break;
+      
     case elfcpp::R_ARM_REL32:
-    case elfcpp::R_ARM_PREL31:
+    case elfcpp::R_ARM_LDR_PC_G0:
+    case elfcpp::R_ARM_SBREL32:
+    case elfcpp::R_ARM_THM_PC8:
+    case elfcpp::R_ARM_BASE_PREL:
+    case elfcpp::R_ARM_LDR_SBREL_11_0_NC:
+    case elfcpp::R_ARM_ALU_SBREL_19_12_NC:
+    case elfcpp::R_ARM_ALU_SBREL_27_20_CK:
+    case elfcpp::R_ARM_MOVW_PREL_NC:
+    case elfcpp::R_ARM_MOVT_PREL:
+    case elfcpp::R_ARM_THM_MOVW_PREL_NC:
+    case elfcpp::R_ARM_THM_MOVT_PREL:
+    case elfcpp::R_ARM_THM_ALU_PREL_11_0:
+    case elfcpp::R_ARM_THM_PC12:
+    case elfcpp::R_ARM_REL32_NOI:
+    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_LDR_PC_G1:
+    case elfcpp::R_ARM_LDR_PC_G2:
+    case elfcpp::R_ARM_LDRS_PC_G0:
+    case elfcpp::R_ARM_LDRS_PC_G1:
+    case elfcpp::R_ARM_LDRS_PC_G2:
+    case elfcpp::R_ARM_LDC_PC_G0:
+    case elfcpp::R_ARM_LDC_PC_G1:
+    case elfcpp::R_ARM_LDC_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_LDR_SB_G0:
+    case elfcpp::R_ARM_LDR_SB_G1:
+    case elfcpp::R_ARM_LDR_SB_G2:
+    case elfcpp::R_ARM_LDRS_SB_G0:
+    case elfcpp::R_ARM_LDRS_SB_G1:
+    case elfcpp::R_ARM_LDRS_SB_G2:
+    case elfcpp::R_ARM_LDC_SB_G0:
+    case elfcpp::R_ARM_LDC_SB_G1:
+    case elfcpp::R_ARM_LDC_SB_G2:
+    case elfcpp::R_ARM_MOVW_BREL_NC:
+    case elfcpp::R_ARM_MOVT_BREL:
+    case elfcpp::R_ARM_MOVW_BREL:
+    case elfcpp::R_ARM_THM_MOVW_BREL_NC:
+    case elfcpp::R_ARM_THM_MOVT_BREL:
+    case elfcpp::R_ARM_THM_MOVW_BREL:
+      // Relative addressing relocations.
       {
        // Make a dynamic relocation if necessary.
        int flags = Symbol::NON_PIC_REF;
@@ -5590,31 +7743,27 @@ Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
       }
       break;
 
+    case elfcpp::R_ARM_PC24:
+    case elfcpp::R_ARM_THM_CALL:
+    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_THM_JUMP19:
-    case elfcpp::R_ARM_CALL:
-    case elfcpp::R_ARM_THM_CALL:
-
-      if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
-       target->make_plt_entry(symtab, layout, gsym);
-      else
-       {
-          // Check to see if this is a function that would need a PLT
-          // but does not get one because the function symbol is untyped.
-          // This happens in assembly code missing a proper .type directive.
-         if ((!gsym->is_undefined() || parameters->options().shared())
-             && !parameters->doing_static_link()
-             && gsym->type() == elfcpp::STT_NOTYPE
-             && (gsym->is_from_dynobj()
-                 || gsym->is_undefined()
-                 || gsym->is_preemptible()))
-           gold_error(_("%s is not a function."),
-                      gsym->demangled_name().c_str());
-       }
-      break;
+    case elfcpp::R_ARM_THM_JUMP6:
+    case elfcpp::R_ARM_THM_JUMP11:
+    case elfcpp::R_ARM_THM_JUMP8:
+      // All the relocation above are branches except for the PREL31 ones.
+      // A PREL31 relocation can point to a personality function in a shared
+      // library.  In that case we want to use a PLT because we want to
+      // call the personality routine and the dyanmic linkers we care about
+      // do not support dynamic PREL31 relocations. An REL31 relocation may
+      // point to a function whose unwinding behaviour is being described but
+      // we will not mistakenly generate a PLT for that because we should use
+      // a local section symbol.
 
-    case elfcpp::R_ARM_PLT32:
       // If the symbol is fully resolved, this is just a relative
       // local reloc.  Otherwise we need a PLT entry.
       if (gsym->final_value_is_known())
@@ -5629,20 +7778,12 @@ Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
       target->make_plt_entry(symtab, layout, gsym);
       break;
 
-    case elfcpp::R_ARM_GOTOFF32:
-      // We need a GOT section.
-      target->got_section(symtab, layout);
-      break;
-
-    case elfcpp::R_ARM_BASE_PREL:
-      // FIXME: What about this?
-      break;
-      
     case elfcpp::R_ARM_GOT_BREL:
+    case elfcpp::R_ARM_GOT_ABS:
     case elfcpp::R_ARM_GOT_PREL:
       {
        // The symbol requires a GOT entry.
-       Output_data_got<32, big_endian>* got =
+       Arm_output_data_got<big_endian>* got =
          target->got_section(symtab, layout);
        if (gsym->final_value_is_known())
          got->add_global(gsym, GOT_TYPE_STANDARD);
@@ -5668,7 +7809,8 @@ Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
       break;
 
     case elfcpp::R_ARM_TARGET1:
-      // This should have been mapped to another type already.
+    case elfcpp::R_ARM_TARGET2:
+      // These should have been mapped to other types already.
       // Fall through.
     case elfcpp::R_ARM_COPY:
     case elfcpp::R_ARM_GLOB_DAT:
@@ -5680,6 +7822,96 @@ Target_arm<big_endian>::Scan::global(Symbol_table* symtab,
                 object->name().c_str(), r_type);
       break;
 
+      // These are initial tls relocs, which are expected when
+      // linking.
+    case elfcpp::R_ARM_TLS_GD32:       // Global-dynamic
+    case elfcpp::R_ARM_TLS_LDM32:      // Local-dynamic
+    case elfcpp::R_ARM_TLS_LDO32:      // Alternate local-dynamic
+    case elfcpp::R_ARM_TLS_IE32:       // Initial-exec
+    case elfcpp::R_ARM_TLS_LE32:       // Local-exec
+      {
+       const bool is_final = gsym->final_value_is_known();
+       const tls::Tls_optimization optimized_type
+            = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
+       switch (r_type)
+         {
+         case elfcpp::R_ARM_TLS_GD32:          // Global-dynamic
+           if (optimized_type == tls::TLSOPT_NONE)
+             {
+               // Create a pair of GOT entries for the module index and
+               // dtv-relative offset.
+                Arm_output_data_got<big_endian>* got
+                    = target->got_section(symtab, layout);
+               if (!parameters->doing_static_link())
+                 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
+                                               target->rel_dyn_section(layout),
+                                               elfcpp::R_ARM_TLS_DTPMOD32,
+                                               elfcpp::R_ARM_TLS_DTPOFF32);
+               else
+                 got->add_tls_gd32_with_static_reloc(GOT_TYPE_TLS_PAIR, gsym);
+             }
+           else
+             // FIXME: TLS optimization not supported yet.
+             gold_unreachable();
+           break;
+
+         case elfcpp::R_ARM_TLS_LDM32:         // Local-dynamic
+           if (optimized_type == tls::TLSOPT_NONE)
+             {
+               // Create a GOT entry for the module index.
+               target->got_mod_index_entry(symtab, layout, object);
+             }
+           else
+             // FIXME: TLS optimization not supported yet.
+             gold_unreachable();
+           break;
+
+         case elfcpp::R_ARM_TLS_LDO32:         // Alternate local-dynamic
+           break;
+
+         case elfcpp::R_ARM_TLS_IE32:          // Initial-exec
+           layout->set_has_static_tls();
+           if (optimized_type == tls::TLSOPT_NONE)
+             {
+               // Create a GOT entry for the tp-relative offset.
+               Arm_output_data_got<big_endian>* got
+                 = target->got_section(symtab, layout);
+               if (!parameters->doing_static_link())
+                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
+                                          target->rel_dyn_section(layout),
+                                          elfcpp::R_ARM_TLS_TPOFF32);
+               else if (!gsym->has_got_offset(GOT_TYPE_TLS_OFFSET))
+                 {
+                   got->add_global(gsym, GOT_TYPE_TLS_OFFSET);
+                   unsigned int got_offset =
+                      gsym->got_offset(GOT_TYPE_TLS_OFFSET);
+                   got->add_static_reloc(got_offset,
+                                         elfcpp::R_ARM_TLS_TPOFF32, gsym);
+                 }
+             }
+           else
+             // FIXME: TLS optimization not supported yet.
+             gold_unreachable();
+           break;
+
+         case elfcpp::R_ARM_TLS_LE32:  // Local-exec
+           layout->set_has_static_tls();
+           if (parameters->options().shared())
+             {
+               // We need to create a dynamic relocation.
+                Reloc_section* rel_dyn = target->rel_dyn_section(layout);
+                rel_dyn->add_global(gsym, elfcpp::R_ARM_TLS_TPOFF32,
+                                   output_section, object,
+                                    data_shndx, reloc.get_r_offset());
+             }
+           break;
+
+         default:
+           gold_unreachable();
+         }
+      }
+      break;
+
     default:
       unsupported_reloc_global(object, r_type, gsym);
       break;
@@ -5773,12 +8005,14 @@ Target_arm<big_endian>::do_finalize_sections(
     {
       Arm_relobj<big_endian>* arm_relobj =
        Arm_relobj<big_endian>::as_arm_relobj(*p);
-      this->merge_processor_specific_flags(
-         arm_relobj->name(),
-         arm_relobj->processor_specific_flags());
-      this->merge_object_attributes(arm_relobj->name().c_str(),
-                                   arm_relobj->attributes_section_data());
-
+      if (arm_relobj->merge_flags_and_attributes())
+       {
+         this->merge_processor_specific_flags(
+             arm_relobj->name(),
+             arm_relobj->processor_specific_flags());
+         this->merge_object_attributes(arm_relobj->name().c_str(),
+                                       arm_relobj->attributes_section_data());
+       }
     } 
 
   for (Input_objects::Dynobj_iterator p = input_objects->dynobj_begin();
@@ -5794,6 +8028,12 @@ Target_arm<big_endian>::do_finalize_sections(
                                    arm_dynobj->attributes_section_data());
     }
 
+  // Create an empty uninitialized attribute section if we still don't have it
+  // at this moment.  This happens if there is no attributes sections in all
+  // inputs.
+  if (this->attributes_section_data_ == NULL)
+    this->attributes_section_data_ = new Attributes_section_data(NULL, 0);
+
   // Check BLX use.
   const Object_attribute* cpu_arch_attr =
     this->get_aeabi_object_attribute(elfcpp::Tag_CPU_arch);
@@ -5819,7 +8059,8 @@ Target_arm<big_endian>::do_finalize_sections(
   // Check if we can use V4BX interworking.
   // The V4BX interworking stub contains BX instruction,
   // which is not specified for some profiles.
-  if (this->fix_v4bx() == 2 && !this->may_use_blx())
+  if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING
+      && !this->may_use_blx())
     gold_error(_("unable to provide V4BX reloc interworking fix up; "
                 "the target profile does not support BX instruction"));
 
@@ -5828,7 +8069,7 @@ Target_arm<big_endian>::do_finalize_sections(
                                  ? NULL
                                  : this->plt_->rel_plt());
   layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
-                                 this->rel_dyn_, true);
+                                 this->rel_dyn_, true, false);
 
   // Emit any relocs we saved in an attempt to avoid generating COPY
   // relocs.
@@ -5866,13 +8107,17 @@ Target_arm<big_endian>::do_finalize_sections(
        }
     }
 
-  // Create an .ARM.attributes section if there is not one already.
-  Output_attributes_section_data* attributes_section =
-    new Output_attributes_section_data(*this->attributes_section_data_);
-  layout->add_output_section_data(".ARM.attributes",
-                                 elfcpp::SHT_ARM_ATTRIBUTES, 0,
-                                 attributes_section, false, false, false,
-                                 false);
+  // Create an .ARM.attributes section unless we have no regular input
+  // object.  In that case the output will be empty.
+  if (input_objects->number_of_relobjs() != 0)
+    {
+      Output_attributes_section_data* attributes_section =
+      new Output_attributes_section_data(*this->attributes_section_data_);
+      layout->add_output_section_data(".ARM.attributes",
+                                     elfcpp::SHT_ARM_ATTRIBUTES, 0,
+                                     attributes_section, false, false, false,
+                                     false);
+    }
 }
 
 // Return whether a direct absolute static relocation needs to be applied.
@@ -5930,11 +8175,22 @@ Target_arm<big_endian>::Relocate::relocate(
     const Symbol_value<32>* psymval,
     unsigned char* view,
     Arm_address address,
-    section_size_type /* view_size */ )
+    section_size_type view_size)
 {
   typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
 
   r_type = get_real_reloc_type(r_type);
+  const Arm_reloc_property* reloc_property =
+    arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
+  if (reloc_property == NULL)
+    {
+      std::string reloc_name =
+       arm_reloc_property_table->reloc_name_in_error_message(r_type);
+      gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
+                            _("cannot relocate %s in object file"),
+                            reloc_name.c_str());
+      return true;
+    }
 
   const Arm_relobj<big_endian>* object =
     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
@@ -5997,7 +8253,7 @@ Target_arm<big_endian>::Relocate::relocate(
 
   // Strip LSB if this points to a THUMB target.
   if (thumb_bit != 0
-      && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type
+      && reloc_property->uses_thumb_bit(
       && ((psymval->value(object, 0) & 1) != 0))
     {
       Arm_address stripped_value =
@@ -6040,8 +8296,57 @@ Target_arm<big_endian>::Relocate::relocate(
   // a local symbol.
   unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
 
+  // Get the addressing origin of the output segment defining the
+  // symbol gsym if needed (AAELF 4.6.1.2 Relocation types).
+  Arm_address sym_origin = 0;
+  if (reloc_property->uses_symbol_base())
+    {
+      if (r_type == elfcpp::R_ARM_BASE_ABS && gsym == NULL)
+       // R_ARM_BASE_ABS with the NULL symbol will give the
+       // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
+       // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
+       sym_origin = target->got_plt_section()->address();
+      else if (gsym == NULL)
+       sym_origin = 0;
+      else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
+       sym_origin = gsym->output_segment()->vaddr();
+      else if (gsym->source() == Symbol::IN_OUTPUT_DATA)
+       sym_origin = gsym->output_data()->address();
+
+      // TODO: Assumes the segment base to be zero for the global symbols
+      // till the proper support for the segment-base-relative addressing
+      // will be implemented.  This is consistent with GNU ld.
+    }
+
+  // For relative addressing relocation, find out the relative address base.
+  Arm_address relative_address_base = 0;
+  switch(reloc_property->relative_address_base())
+    {
+    case Arm_reloc_property::RAB_NONE:
+    // Relocations with relative address bases RAB_TLS and RAB_tp are
+    // handled by relocate_tls.  So we do not need to do anything here.
+    case Arm_reloc_property::RAB_TLS:
+    case Arm_reloc_property::RAB_tp:
+      break;
+    case Arm_reloc_property::RAB_B_S:
+      relative_address_base = sym_origin;
+      break;
+    case Arm_reloc_property::RAB_GOT_ORG:
+      relative_address_base = target->got_plt_section()->address();
+      break;
+    case Arm_reloc_property::RAB_P:
+      relative_address_base = address;
+      break;
+    case Arm_reloc_property::RAB_Pa:
+      relative_address_base = address & 0xfffffffcU;
+      break;
+    default:
+      gold_unreachable(); 
+    }
+    
   typename Arm_relocate_functions::Status reloc_status =
        Arm_relocate_functions::STATUS_OKAY;
+  bool check_overflow = reloc_property->checks_overflow();
   switch (r_type)
     {
     case elfcpp::R_ARM_NONE:
@@ -6081,66 +8386,63 @@ Target_arm<big_endian>::Relocate::relocate(
       break;
 
     case elfcpp::R_ARM_MOVW_ABS_NC:
-      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
+      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
                                    output_section))
-       reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
-                                                          psymval,
-                                                                  thumb_bit);
-      else
-       gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
-                    "a shared object; recompile with -fPIC"));
+       reloc_status = Arm_relocate_functions::movw(view, object, psymval,
+                                                   0, thumb_bit,
+                                                   check_overflow);
       break;
 
     case elfcpp::R_ARM_MOVT_ABS:
-      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
+      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
                                    output_section))
-       reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
-      else
-       gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
-                    "a shared object; recompile with -fPIC"));
+       reloc_status = Arm_relocate_functions::movt(view, object, psymval, 0);
       break;
 
     case elfcpp::R_ARM_THM_MOVW_ABS_NC:
-      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
+      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
                                    output_section))
-       reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
-                                                              psymval,
-                                                                      thumb_bit);
-      else
-       gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
-                    "making a shared object; recompile with -fPIC"));
+       reloc_status = Arm_relocate_functions::thm_movw(view, object, psymval,
+                                                               0, thumb_bit, false);
       break;
 
     case elfcpp::R_ARM_THM_MOVT_ABS:
-      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
+      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
                                    output_section))
-       reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
-                                                           psymval);
-      else
-       gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
-                    "making a shared object; recompile with -fPIC"));
+       reloc_status = Arm_relocate_functions::thm_movt(view, object,
+                                                       psymval, 0);
       break;
 
     case elfcpp::R_ARM_MOVW_PREL_NC:
-      reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
-                                                         psymval, address,
-                                                         thumb_bit);
+    case elfcpp::R_ARM_MOVW_BREL_NC:
+    case elfcpp::R_ARM_MOVW_BREL:
+      reloc_status =
+       Arm_relocate_functions::movw(view, object, psymval,
+                                    relative_address_base, thumb_bit,
+                                    check_overflow);
       break;
 
     case elfcpp::R_ARM_MOVT_PREL:
-      reloc_status = Arm_relocate_functions::movt_prel(view, object,
-                                                       psymval, address);
+    case elfcpp::R_ARM_MOVT_BREL:
+      reloc_status =
+       Arm_relocate_functions::movt(view, object, psymval,
+                                    relative_address_base);
       break;
 
     case elfcpp::R_ARM_THM_MOVW_PREL_NC:
-      reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
-                                                             psymval, address,
-                                                             thumb_bit);
+    case elfcpp::R_ARM_THM_MOVW_BREL_NC:
+    case elfcpp::R_ARM_THM_MOVW_BREL:
+      reloc_status =
+       Arm_relocate_functions::thm_movw(view, object, psymval,
+                                        relative_address_base,
+                                        thumb_bit, check_overflow);
       break;
 
     case elfcpp::R_ARM_THM_MOVT_PREL:
-      reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
-                                                          psymval, address);
+    case elfcpp::R_ARM_THM_MOVT_BREL:
+      reloc_status =
+       Arm_relocate_functions::thm_movt(view, object, psymval,
+                                        relative_address_base);
       break;
        
     case elfcpp::R_ARM_REL32:
@@ -6154,25 +8456,14 @@ Target_arm<big_endian>::Relocate::relocate(
        reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
       break;
 
+    // Thumb long branches.
     case elfcpp::R_ARM_THM_CALL:
-      reloc_status =
-       Arm_relocate_functions::thm_call(relinfo, view, gsym, object, r_sym,
-                                        psymval, address, thumb_bit,
-                                        is_weakly_undefined_without_plt);
-      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_THM_XPC22:
+    case elfcpp::R_ARM_THM_JUMP24:
       reloc_status =
-       Arm_relocate_functions::thm_xpc22(relinfo, view, gsym, object, r_sym,
-                                         psymval, address, thumb_bit,
-                                         is_weakly_undefined_without_plt);
+       Arm_relocate_functions::thumb_branch_common(
+           r_type, relinfo, view, gsym, object, r_sym, psymval, address,
+           thumb_bit, is_weakly_undefined_without_plt);
       break;
 
     case elfcpp::R_ARM_GOTOFF32:
@@ -6185,51 +8476,18 @@ Target_arm<big_endian>::Relocate::relocate(
       break;
 
     case elfcpp::R_ARM_BASE_PREL:
-      {
-       uint32_t origin;
-       // Get the addressing origin of the output segment defining the 
-       // symbol gsym (AAELF 4.6.1.2 Relocation types)
-       gold_assert(gsym != NULL); 
-       if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
-         origin = gsym->output_segment()->vaddr();
-       else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
-         origin = gsym->output_data()->address();
-       else
-         {
-            gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
-                                  _("cannot find origin of R_ARM_BASE_PREL"));
-           return true;
-         }
-       reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
-      }
+      gold_assert(gsym != NULL);
+      reloc_status =
+         Arm_relocate_functions::base_prel(view, sym_origin, address);
       break;
 
     case elfcpp::R_ARM_BASE_ABS:
       {
-       if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
+       if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
                                      output_section))
          break;
 
-       uint32_t origin;
-       // Get the addressing origin of the output segment defining
-       // the symbol gsym (AAELF 4.6.1.2 Relocation types).
-       if (gsym == NULL)
-         // R_ARM_BASE_ABS with the NULL symbol will give the
-         // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
-         // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
-         origin = target->got_plt_section()->address();
-       else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
-         origin = gsym->output_segment()->vaddr();
-       else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
-         origin = gsym->output_data()->address();
-       else
-         {
-            gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
-                                  _("cannot find origin of R_ARM_BASE_ABS"));
-           return true;
-         }
-
-       reloc_status = Arm_relocate_functions::base_abs(view, origin);
+       reloc_status = Arm_relocate_functions::base_abs(view, sym_origin);
       }
       break;
 
@@ -6251,6 +8509,9 @@ Target_arm<big_endian>::Relocate::relocate(
       break;
 
     case elfcpp::R_ARM_PLT32:
+    case elfcpp::R_ARM_CALL:
+    case elfcpp::R_ARM_JUMP24:
+    case elfcpp::R_ARM_XPC25:
       gold_assert(gsym == NULL
                  || gsym->has_plt_offset()
                  || gsym->final_value_is_known()
@@ -6258,30 +8519,9 @@ Target_arm<big_endian>::Relocate::relocate(
                      && !gsym->is_from_dynobj()
                      && !gsym->is_preemptible()));
       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(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(relinfo, view, gsym, object, r_sym,
-                                      psymval, address, thumb_bit,
-                                      is_weakly_undefined_without_plt);
-      break;
-
-    case elfcpp::R_ARM_THM_JUMP24:
-      reloc_status =
-       Arm_relocate_functions::thm_jump24(relinfo, view, gsym, object, r_sym,
-                                          psymval, address, thumb_bit,
-                                          is_weakly_undefined_without_plt);
+       Arm_relocate_functions::arm_branch_common(
+           r_type, relinfo, view, gsym, object, r_sym, psymval, address,
+           thumb_bit, is_weakly_undefined_without_plt);
       break;
 
     case elfcpp::R_ARM_THM_JUMP19:
@@ -6305,37 +8545,105 @@ Target_arm<big_endian>::Relocate::relocate(
        Arm_relocate_functions::thm_jump11(view, object, psymval, address);
       break;
 
-    case elfcpp::R_ARM_PREL31:
-      reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
-                                                   address, thumb_bit);
+    case elfcpp::R_ARM_PREL31:
+      reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
+                                                   address, thumb_bit);
+      break;
+
+    case elfcpp::R_ARM_V4BX:
+      if (target->fix_v4bx() > General_options::FIX_V4BX_NONE)
+       {
+         const bool is_v4bx_interworking =
+             (target->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING);
+         reloc_status =
+           Arm_relocate_functions::v4bx(relinfo, view, object, address,
+                                        is_v4bx_interworking);
+       }
+      break;
+
+    case elfcpp::R_ARM_THM_PC8:
+      reloc_status =
+       Arm_relocate_functions::thm_pc8(view, object, psymval, address);
+      break;
+
+    case elfcpp::R_ARM_THM_PC12:
+      reloc_status =
+       Arm_relocate_functions::thm_pc12(view, object, psymval, address);
+      break;
+
+    case elfcpp::R_ARM_THM_ALU_PREL_11_0:
+      reloc_status =
+       Arm_relocate_functions::thm_alu11(view, object, psymval, address,
+                                         thumb_bit);
+      break;
+
+    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:
+      reloc_status =
+       Arm_relocate_functions::arm_grp_alu(view, object, psymval,
+                                           reloc_property->group_index(),
+                                           relative_address_base,
+                                           thumb_bit, check_overflow);
+      break;
+
+    case elfcpp::R_ARM_LDR_PC_G0:
+    case elfcpp::R_ARM_LDR_PC_G1:
+    case elfcpp::R_ARM_LDR_PC_G2:
+    case elfcpp::R_ARM_LDR_SB_G0:
+    case elfcpp::R_ARM_LDR_SB_G1:
+    case elfcpp::R_ARM_LDR_SB_G2:
+      reloc_status =
+         Arm_relocate_functions::arm_grp_ldr(view, object, psymval,
+                                             reloc_property->group_index(),
+                                             relative_address_base);
+      break;
+
+    case elfcpp::R_ARM_LDRS_PC_G0:
+    case elfcpp::R_ARM_LDRS_PC_G1:
+    case elfcpp::R_ARM_LDRS_PC_G2:
+    case elfcpp::R_ARM_LDRS_SB_G0:
+    case elfcpp::R_ARM_LDRS_SB_G1:
+    case elfcpp::R_ARM_LDRS_SB_G2:
+      reloc_status =
+         Arm_relocate_functions::arm_grp_ldrs(view, object, psymval,
+                                              reloc_property->group_index(),
+                                              relative_address_base);
       break;
 
-    case elfcpp::R_ARM_V4BX:
-      if (target->fix_v4bx() > 0)
-       reloc_status =
-         Arm_relocate_functions::v4bx(relinfo, view, object, address,
-                                      (target->fix_v4bx() == 2));
+    case elfcpp::R_ARM_LDC_PC_G0:
+    case elfcpp::R_ARM_LDC_PC_G1:
+    case elfcpp::R_ARM_LDC_PC_G2:
+    case elfcpp::R_ARM_LDC_SB_G0:
+    case elfcpp::R_ARM_LDC_SB_G1:
+    case elfcpp::R_ARM_LDC_SB_G2:
+      reloc_status =
+         Arm_relocate_functions::arm_grp_ldc(view, object, psymval,
+                                             reloc_property->group_index(),
+                                             relative_address_base);
       break;
 
-    case elfcpp::R_ARM_TARGET1:
-      // This should have been mapped to another type already.
-      // Fall through.
-    case elfcpp::R_ARM_COPY:
-    case elfcpp::R_ARM_GLOB_DAT:
-    case elfcpp::R_ARM_JUMP_SLOT:
-    case elfcpp::R_ARM_RELATIVE:
-      // These are relocations which should only be seen by the
-      // dynamic linker, and should never be seen here.
-      gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
-                            _("unexpected reloc %u in object file"),
-                            r_type);
+      // These are initial tls relocs, which are expected when
+      // linking.
+    case elfcpp::R_ARM_TLS_GD32:       // Global-dynamic
+    case elfcpp::R_ARM_TLS_LDM32:      // Local-dynamic
+    case elfcpp::R_ARM_TLS_LDO32:      // Alternate local-dynamic
+    case elfcpp::R_ARM_TLS_IE32:       // Initial-exec
+    case elfcpp::R_ARM_TLS_LE32:       // Local-exec
+      reloc_status =
+       this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
+                          view, address, view_size);
       break;
 
     default:
-      gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
-                            _("unsupported reloc %u"),
-                            r_type);
-      break;
+      gold_unreachable();
     }
 
   // Report any errors.
@@ -6345,16 +8653,16 @@ Target_arm<big_endian>::Relocate::relocate(
       break;
     case Arm_relocate_functions::STATUS_OVERFLOW:
       gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
-                            _("relocation overflow in relocation %u"),
-                            r_type);
+                            _("relocation overflow in %s"),
+                            reloc_property->name().c_str());
       break;
     case Arm_relocate_functions::STATUS_BAD_RELOC:
       gold_error_at_location(
        relinfo,
        relnum,
        rel.get_r_offset(),
-       _("unexpected opcode while processing relocation %u"),
-       r_type);
+       _("unexpected opcode while processing relocation %s"),
+       reloc_property->name().c_str());
       break;
     default:
       gold_unreachable();
@@ -6363,6 +8671,145 @@ Target_arm<big_endian>::Relocate::relocate(
   return true;
 }
 
+// Perform a TLS relocation.
+
+template<bool big_endian>
+inline typename Arm_relocate_functions<big_endian>::Status
+Target_arm<big_endian>::Relocate::relocate_tls(
+    const Relocate_info<32, big_endian>* relinfo,
+    Target_arm<big_endian>* target,
+    size_t relnum,
+    const elfcpp::Rel<32, big_endian>& rel,
+    unsigned int r_type,
+    const Sized_symbol<32>* gsym,
+    const Symbol_value<32>* psymval,
+    unsigned char* view,
+    elfcpp::Elf_types<32>::Elf_Addr address,
+    section_size_type /*view_size*/ )
+{
+  typedef Arm_relocate_functions<big_endian> ArmRelocFuncs;
+  typedef Relocate_functions<32, big_endian> RelocFuncs;
+  Output_segment* tls_segment = relinfo->layout->tls_segment();
+
+  const Sized_relobj<32, big_endian>* object = relinfo->object;
+
+  elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
+
+  const bool is_final = (gsym == NULL
+                        ? !parameters->options().shared()
+                        : gsym->final_value_is_known());
+  const tls::Tls_optimization optimized_type
+      = Target_arm<big_endian>::optimize_tls_reloc(is_final, r_type);
+  switch (r_type)
+    {
+    case elfcpp::R_ARM_TLS_GD32:       // Global-dynamic
+        {
+          unsigned int got_type = GOT_TYPE_TLS_PAIR;
+          unsigned int got_offset;
+          if (gsym != NULL)
+            {
+              gold_assert(gsym->has_got_offset(got_type));
+              got_offset = gsym->got_offset(got_type) - target->got_size();
+            }
+          else
+            {
+              unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
+              gold_assert(object->local_has_got_offset(r_sym, got_type));
+              got_offset = (object->local_got_offset(r_sym, got_type)
+                           - target->got_size());
+            }
+          if (optimized_type == tls::TLSOPT_NONE)
+            {
+             Arm_address got_entry =
+               target->got_plt_section()->address() + got_offset;
+             
+              // Relocate the field with the PC relative offset of the pair of
+              // GOT entries.
+             RelocFuncs::pcrel32(view, got_entry, address);
+              return ArmRelocFuncs::STATUS_OKAY;
+            }
+        }
+      break;
+
+    case elfcpp::R_ARM_TLS_LDM32:      // Local-dynamic
+      if (optimized_type == tls::TLSOPT_NONE)
+        {
+          // Relocate the field with the offset of the GOT entry for
+          // the module index.
+          unsigned int got_offset;
+          got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
+                       - target->got_size());
+         Arm_address got_entry =
+           target->got_plt_section()->address() + got_offset;
+
+          // Relocate the field with the PC relative offset of the pair of
+          // GOT entries.
+          RelocFuncs::pcrel32(view, got_entry, address);
+         return ArmRelocFuncs::STATUS_OKAY;
+        }
+      break;
+
+    case elfcpp::R_ARM_TLS_LDO32:      // Alternate local-dynamic
+      RelocFuncs::rel32(view, value);
+      return ArmRelocFuncs::STATUS_OKAY;
+
+    case elfcpp::R_ARM_TLS_IE32:       // Initial-exec
+      if (optimized_type == tls::TLSOPT_NONE)
+        {
+          // Relocate the field with the offset of the GOT entry for
+          // the tp-relative offset of the symbol.
+         unsigned int got_type = GOT_TYPE_TLS_OFFSET;
+          unsigned int got_offset;
+          if (gsym != NULL)
+            {
+              gold_assert(gsym->has_got_offset(got_type));
+              got_offset = gsym->got_offset(got_type);
+            }
+          else
+            {
+              unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
+              gold_assert(object->local_has_got_offset(r_sym, got_type));
+              got_offset = object->local_got_offset(r_sym, got_type);
+            }
+
+          // All GOT offsets are relative to the end of the GOT.
+          got_offset -= target->got_size();
+
+         Arm_address got_entry =
+           target->got_plt_section()->address() + got_offset;
+
+          // Relocate the field with the PC relative offset of the GOT entry.
+         RelocFuncs::pcrel32(view, got_entry, address);
+         return ArmRelocFuncs::STATUS_OKAY;
+        }
+      break;
+
+    case elfcpp::R_ARM_TLS_LE32:       // Local-exec
+      // If we're creating a shared library, a dynamic relocation will
+      // have been created for this location, so do not apply it now.
+      if (!parameters->options().shared())
+        {
+          gold_assert(tls_segment != NULL);
+
+         // $tp points to the TCB, which is followed by the TLS, so we
+         // need to add TCB size to the offset.
+         Arm_address aligned_tcb_size =
+           align_address(ARM_TCB_SIZE, tls_segment->maximum_alignment());
+          RelocFuncs::rel32(view, value + aligned_tcb_size);
+
+        }
+      return ArmRelocFuncs::STATUS_OKAY;
+    
+    default:
+      gold_unreachable();
+    }
+
+  gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
+                        _("unsupported reloc %u"),
+                        r_type);
+  return ArmRelocFuncs::STATUS_BAD_RELOC;
+}
+
 // Relocate section data.
 
 template<bool big_endian>
@@ -6382,26 +8829,27 @@ 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)
+  // See if we are relocating a relaxed input section.  If so, the view
+  // covers the whole output section and we need to adjust accordingly.
+  if (needs_special_offset_handling)
     {
-      gold_assert(needs_special_offset_handling);
-      Arm_address section_address = arm_input_section->address();
-      section_size_type section_size = arm_input_section->data_size();
+      const Output_relaxed_input_section* poris =
+       output_section->find_relaxed_input_section(relinfo->object,
+                                                  relinfo->data_shndx);
+      if (poris != NULL)
+       {
+         Arm_address section_address = poris->address();
+         section_size_type section_size = poris->data_size();
 
-      gold_assert((arm_input_section->address() >= address)
-                 && ((arm_input_section->address()
-                      + arm_input_section->data_size())
-                     <= (address + view_size)));
+         gold_assert((section_address >= address)
+                     && ((section_address + section_size)
+                         <= (address + view_size)));
 
-      off_t offset = section_address - address;
-      view += offset;
-      address += offset;
-      view_size = section_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,
@@ -6428,61 +8876,16 @@ Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
     Relobj* object)
 {
   r_type = get_real_reloc_type(r_type);
-  switch (r_type)
+  const Arm_reloc_property* arp =
+      arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
+  if (arp != NULL)
+    return arp->size();
+  else
     {
-    case elfcpp::R_ARM_NONE:
-      return 0;
-
-    case elfcpp::R_ARM_ABS8:
-      return 1;
-
-    case elfcpp::R_ARM_ABS16:
-    case elfcpp::R_ARM_THM_ABS5:
-    case elfcpp::R_ARM_THM_JUMP6:
-    case elfcpp::R_ARM_THM_JUMP8:
-    case elfcpp::R_ARM_THM_JUMP11:
-      return 2;
-
-    case elfcpp::R_ARM_ABS32:
-    case elfcpp::R_ARM_ABS32_NOI:
-    case elfcpp::R_ARM_ABS12:
-    case elfcpp::R_ARM_BASE_ABS:
-    case elfcpp::R_ARM_REL32:
-    case elfcpp::R_ARM_THM_CALL:
-    case elfcpp::R_ARM_GOTOFF32:
-    case elfcpp::R_ARM_BASE_PREL:
-    case elfcpp::R_ARM_GOT_BREL:
-    case elfcpp::R_ARM_GOT_PREL:
-    case elfcpp::R_ARM_PLT32:
-    case elfcpp::R_ARM_CALL:
-    case elfcpp::R_ARM_JUMP24:
-    case elfcpp::R_ARM_PREL31:
-    case elfcpp::R_ARM_MOVW_ABS_NC:
-    case elfcpp::R_ARM_MOVT_ABS:
-    case elfcpp::R_ARM_THM_MOVW_ABS_NC:
-    case elfcpp::R_ARM_THM_MOVT_ABS:
-    case elfcpp::R_ARM_MOVW_PREL_NC:
-    case elfcpp::R_ARM_MOVT_PREL:
-    case elfcpp::R_ARM_THM_MOVW_PREL_NC:
-    case elfcpp::R_ARM_THM_MOVT_PREL:
-    case elfcpp::R_ARM_V4BX:
-      return 4;
-
-    case elfcpp::R_ARM_TARGET1:
-      // This should have been mapped to another type already.
-      // Fall through.
-    case elfcpp::R_ARM_COPY:
-    case elfcpp::R_ARM_GLOB_DAT:
-    case elfcpp::R_ARM_JUMP_SLOT:
-    case elfcpp::R_ARM_RELATIVE:
-      // These are relocations which should only be seen by the
-      // dynamic linker, and should never be seen here.
-      gold_error(_("%s: unexpected reloc %u in object file"),
-                object->name().c_str(), r_type);
-      return 0;
-
-    default:
-      object->error(_("unsupported reloc %u in object file"), r_type);
+      std::string reloc_name =
+       arm_reloc_property_table->reloc_name_in_error_message(r_type);
+      gold_error(_("%s: unexpected %s in object file"),
+                object->name().c_str(), reloc_name.c_str());
       return 0;
     }
 }
@@ -6632,7 +9035,8 @@ Target_arm<big_endian>::merge_processor_specific_flags(
       // Complain about various flag mismatches.
       elfcpp::Elf_Word version1 = elfcpp::arm_eabi_version(flags);
       elfcpp::Elf_Word version2 = elfcpp::arm_eabi_version(out_flags);
-      if (!this->are_eabi_versions_compatible(version1, version2))
+      if (!this->are_eabi_versions_compatible(version1, version2)
+         && parameters->options().warn_mismatch())
        gold_error(_("Source object %s has EABI version %d but output has "
                     "EABI version %d."),
                   name.c_str(),
@@ -7024,13 +9428,35 @@ Target_arm<big_endian>::merge_object_attributes(
     return;
 
   // If output has no object attributes, just copy.
+  const int vendor = Object_attribute::OBJ_ATTR_PROC;
   if (this->attributes_section_data_ == NULL)
     {
       this->attributes_section_data_ = new Attributes_section_data(*pasd);
+      Object_attribute* out_attr =
+       this->attributes_section_data_->known_attributes(vendor);
+
+      // We do not output objects with Tag_MPextension_use_legacy - we move
+      //  the attribute's value to Tag_MPextension_use.  */
+      if (out_attr[elfcpp::Tag_MPextension_use_legacy].int_value() != 0)
+       {
+         if (out_attr[elfcpp::Tag_MPextension_use].int_value() != 0
+             && out_attr[elfcpp::Tag_MPextension_use_legacy].int_value()
+               != out_attr[elfcpp::Tag_MPextension_use].int_value())
+           {
+             gold_error(_("%s has both the current and legacy "
+                          "Tag_MPextension_use attributes"),
+                        name);
+           }
+
+         out_attr[elfcpp::Tag_MPextension_use] =
+           out_attr[elfcpp::Tag_MPextension_use_legacy];
+         out_attr[elfcpp::Tag_MPextension_use_legacy].set_type(0);
+         out_attr[elfcpp::Tag_MPextension_use_legacy].set_int_value(0);
+       }
+
       return;
     }
 
-  const int vendor = Object_attribute::OBJ_ATTR_PROC;
   const Object_attribute* in_attr = pasd->known_attributes(vendor);
   Object_attribute* out_attr =
     this->attributes_section_data_->known_attributes(vendor);
@@ -7043,7 +9469,8 @@ Target_arm<big_endian>::merge_object_attributes(
       if (out_attr[elfcpp::Tag_ABI_FP_number_model].int_value() == 0)
        out_attr[elfcpp::Tag_ABI_VFP_args].set_int_value(
            in_attr[elfcpp::Tag_ABI_VFP_args].int_value());
-      else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0)
+      else if (in_attr[elfcpp::Tag_ABI_FP_number_model].int_value() != 0
+              && parameters->options().warn_mismatch())
         gold_error(_("%s uses VFP register arguments, output does not"),
                   name);
     }
@@ -7187,7 +9614,7 @@ Target_arm<big_endian>::merge_object_attributes(
                           && (out_attr[i].int_value() == 'A'
                               || out_attr[i].int_value() == 'R')))
                ; // Do nothing.
-             else
+             else if (parameters->options().warn_mismatch())
                {
                  gold_error
                    (_("conflicting architecture profiles %c/%c"),
@@ -7242,7 +9669,9 @@ Target_arm<big_endian>::merge_object_attributes(
        case elfcpp::Tag_PCS_config:
          if (out_attr[i].int_value() == 0)
            out_attr[i].set_int_value(in_attr[i].int_value());
-         else if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
+         else if (in_attr[i].int_value() != 0
+                  && out_attr[i].int_value() != 0
+                  && parameters->options().warn_mismatch())
            {
              // It's sometimes ok to mix different configs, so this is only
              // a warning.
@@ -7252,7 +9681,8 @@ Target_arm<big_endian>::merge_object_attributes(
        case elfcpp::Tag_ABI_PCS_R9_use:
          if (in_attr[i].int_value() != out_attr[i].int_value()
              && out_attr[i].int_value() != elfcpp::AEABI_R9_unused
-             && in_attr[i].int_value() != elfcpp::AEABI_R9_unused)
+             && in_attr[i].int_value() != elfcpp::AEABI_R9_unused
+             && parameters->options().warn_mismatch())
            {
              gold_error(_("%s: conflicting use of R9"), name);
            }
@@ -7264,11 +9694,12 @@ Target_arm<big_endian>::merge_object_attributes(
              && (in_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
                  != elfcpp::AEABI_R9_SB)
              && (out_attr[elfcpp::Tag_ABI_PCS_R9_use].int_value()
-                 != elfcpp::AEABI_R9_unused))
+                 != elfcpp::AEABI_R9_unused)
+             && parameters->options().warn_mismatch())
            {
              gold_error(_("%s: SB relative addressing conflicts with use "
                           "of R9"),
-                        name);
+                          name);
            }
          // Use the smallest value specified.
          if (in_attr[i].int_value() < out_attr[i].int_value())
@@ -7278,7 +9709,8 @@ Target_arm<big_endian>::merge_object_attributes(
          // FIXME: Make it possible to turn off this warning.
          if (out_attr[i].int_value()
              && in_attr[i].int_value()
-             && out_attr[i].int_value() != in_attr[i].int_value())
+             && out_attr[i].int_value() != in_attr[i].int_value()
+             && parameters->options().warn_mismatch())
            {
              gold_warning(_("%s uses %u-byte wchar_t yet the output is to "
                             "use %u-byte wchar_t; use of wchar_t values "
@@ -7301,7 +9733,8 @@ Target_arm<big_endian>::merge_object_attributes(
                }
              // FIXME: Make it possible to turn off this warning.
              else if (in_attr[i].int_value() != elfcpp::AEABI_enum_forced_wide
-                      && out_attr[i].int_value() != in_attr[i].int_value())
+                      && out_attr[i].int_value() != in_attr[i].int_value()
+                      && parameters->options().warn_mismatch())
                {
                  unsigned int in_value = in_attr[i].int_value();
                  unsigned int out_value = out_attr[i].int_value();
@@ -7318,7 +9751,8 @@ Target_arm<big_endian>::merge_object_attributes(
          // Aready done.
          break;
        case elfcpp::Tag_ABI_WMMX_args:
-         if (in_attr[i].int_value() != out_attr[i].int_value())
+         if (in_attr[i].int_value() != out_attr[i].int_value()
+             && parameters->options().warn_mismatch())
            {
              gold_error(_("%s uses iWMMXt register arguments, output does "
                           "not"),
@@ -7339,7 +9773,8 @@ Target_arm<big_endian>::merge_object_attributes(
        case elfcpp::Tag_ABI_FP_16bit_format:
          if (in_attr[i].int_value() != 0 && out_attr[i].int_value() != 0)
            {
-             if (in_attr[i].int_value() != out_attr[i].int_value())
+             if (in_attr[i].int_value() != out_attr[i].int_value()
+                 && parameters->options().warn_mismatch())
                gold_error(_("fp16 format mismatch between %s and output"),
                           name);
            }
@@ -7347,6 +9782,51 @@ Target_arm<big_endian>::merge_object_attributes(
            out_attr[i].set_int_value(in_attr[i].int_value());
          break;
 
+       case elfcpp::Tag_DIV_use:
+         // This tag is set to zero if we can use UDIV and SDIV in Thumb
+         // mode on a v7-M or v7-R CPU; to one if we can not use UDIV or
+         // SDIV at all; and to two if we can use UDIV or SDIV on a v7-A
+         // CPU.  We will merge as follows: If the input attribute's value
+         // is one then the output attribute's value remains unchanged.  If
+         // the input attribute's value is zero or two then if the output
+         // attribute's value is one the output value is set to the input
+         // value, otherwise the output value must be the same as the
+         // inputs.  */ 
+         if (in_attr[i].int_value() != 1 && out_attr[i].int_value() != 1) 
+           { 
+             if (in_attr[i].int_value() != out_attr[i].int_value())
+               {
+                 gold_error(_("DIV usage mismatch between %s and output"),
+                            name);
+               }
+           } 
+
+         if (in_attr[i].int_value() != 1)
+           out_attr[i].set_int_value(in_attr[i].int_value()); 
+         
+         break;
+
+       case elfcpp::Tag_MPextension_use_legacy:
+         // We don't output objects with Tag_MPextension_use_legacy - we
+         // move the value to Tag_MPextension_use.
+         if (in_attr[i].int_value() != 0
+             && in_attr[elfcpp::Tag_MPextension_use].int_value() != 0)
+           {
+             if (in_attr[elfcpp::Tag_MPextension_use].int_value()
+                 != in_attr[i].int_value())
+               {
+                 gold_error(_("%s has has both the current and legacy "
+                              "Tag_MPextension_use attributes"), 
+                            name);
+               }
+           }
+
+         if (in_attr[i].int_value()
+             > out_attr[elfcpp::Tag_MPextension_use].int_value())
+           out_attr[elfcpp::Tag_MPextension_use] = in_attr[i];
+
+         break;
+
        case elfcpp::Tag_nodefaults:
          // This tag is set if it exists, but the value is unused (and is
          // typically zero).  We don't actually need to do anything here -
@@ -7376,7 +9856,8 @@ Target_arm<big_endian>::merge_object_attributes(
                     || in_attr[i].string_value() != "")
              err_object = name;
 
-           if (err_object != NULL)
+           if (err_object != NULL
+               && parameters->options().warn_mismatch())
              {
                // Attribute numbers >=64 (mod 128) can be safely ignored.
                if ((i & 127) < 64)
@@ -7468,7 +9949,7 @@ Target_arm<big_endian>::merge_object_attributes(
            }
        }
 
-      if (err_object)
+      if (err_object && parameters->options().warn_mismatch())
        {
          // Attribute numbers >=64 (mod 128) can be safely ignored.  */
          if ((err_tag & 127) < 64)
@@ -7485,54 +9966,6 @@ Target_arm<big_endian>::merge_object_attributes(
     }
 }
 
-// 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.
@@ -7543,7 +9976,7 @@ Target_arm<big_endian>::new_arm_input_section(
     Relobj* relobj,
     unsigned int shndx)
 {
-  Input_section_specifier iss(relobj, shndx);
+  Section_id sid(relobj, shndx);
 
   Arm_input_section<big_endian>* arm_input_section =
     new Arm_input_section<big_endian>(relobj, shndx);
@@ -7551,7 +9984,7 @@ Target_arm<big_endian>::new_arm_input_section(
 
   // 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));
+    this->arm_input_section_map_.insert(std::make_pair(sid, arm_input_section));
 
   // Make sure that it we have not created another Arm_input_section
   // for this input section already.
@@ -7569,9 +10002,9 @@ Target_arm<big_endian>::find_arm_input_section(
     Relobj* relobj,
     unsigned int shndx) const
 {
-  Input_section_specifier iss(relobj, shndx);
+  Section_id sid(relobj, shndx);
   typename Arm_input_section_map::const_iterator p =
-    this->arm_input_section_map_.find(iss);
+    this->arm_input_section_map_.find(sid);
   return (p != this->arm_input_section_map_.end()) ? p->second : NULL;
 }
 
@@ -7610,29 +10043,6 @@ Target_arm<big_endian>::scan_reloc_for_stub(
   const Arm_relobj<big_endian>* arm_relobj =
     Arm_relobj<big_endian>::as_arm_relobj(relinfo->object);
 
-  if (r_type == elfcpp::R_ARM_V4BX)
-    {
-      const uint32_t reg = (addend & 0xf);
-      if (this->fix_v4bx() == 2 && reg < 0xf)
-       {
-         // 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);
-
-         if (stub_table->find_arm_v4bx_stub(reg) == NULL)
-           {
-             // create a new stub and add it to stub table.
-             Arm_v4bx_stub* stub =
-               this->stub_factory().make_arm_v4bx_stub(reg);
-             gold_assert(stub != NULL);
-             stub_table->add_arm_v4bx_stub(stub);
-           }
-       }
-
-      return;
-    }
-
   bool target_is_thumb;
   Symbol_value<32> symval;
   if (gsym != NULL)
@@ -7666,8 +10076,11 @@ Target_arm<big_endian>::scan_reloc_for_stub(
     }
 
   // Strip LSB if this points to a THUMB target.
+  const Arm_reloc_property* reloc_property =
+    arm_reloc_property_table->get_implemented_static_reloc_property(r_type);
+  gold_assert(reloc_property != NULL);
   if (target_is_thumb
-      && Target_arm<big_endian>::reloc_uses_thumb_bit(r_type)
+      && reloc_property->uses_thumb_bit()
       && ((psymval->value(arm_relobj, 0) & 1) != 0))
     {
       Arm_address stripped_value =
@@ -7820,15 +10233,36 @@ Target_arm<big_endian>::scan_reloc_section_for_stubs(
            continue;
        }
 
+      // Create a v4bx stub if --fix-v4bx-interworking is used.
       if (r_type == elfcpp::R_ARM_V4BX)
        {
-         // Get the BX instruction.
-         typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
-         const Valtype* wv = reinterpret_cast<const Valtype*>(view + offset);
-         elfcpp::Elf_types<32>::Elf_Swxword insn =
-             elfcpp::Swap<32, big_endian>::readval(wv);
-         this->scan_reloc_for_stub(relinfo, r_type, NULL, 0, NULL,
-                                   insn, NULL);
+         if (this->fix_v4bx() == General_options::FIX_V4BX_INTERWORKING)
+           {
+             // Get the BX instruction.
+             typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
+             const Valtype* wv =
+               reinterpret_cast<const Valtype*>(view + offset);
+             elfcpp::Elf_types<32>::Elf_Swxword insn =
+               elfcpp::Swap<32, big_endian>::readval(wv);
+             const uint32_t reg = (insn & 0xf);
+
+             if (reg < 0xf)
+               {
+                 // Try looking up an existing stub from a stub table.
+                 Stub_table<big_endian>* stub_table =
+                   arm_object->stub_table(relinfo->data_shndx);
+                 gold_assert(stub_table != NULL);
+
+                 if (stub_table->find_arm_v4bx_stub(reg) == NULL)
+                   {
+                     // create a new stub and add it to stub table.
+                     Arm_v4bx_stub* stub =
+                       this->stub_factory().make_arm_v4bx_stub(reg);
+                     gold_assert(stub != NULL);
+                     stub_table->add_arm_v4bx_stub(stub);
+                   }
+               }
+           }
          continue;
        }
 
@@ -8009,6 +10443,7 @@ Target_arm<big_endian>::do_relax(
 
   // If this is the first pass, we need to group input sections into
   // stub groups.
+  bool done_exidx_fixup = false;
   if (pass == 1)
     {
       // Determine the stub group size.  The group size is the absolute
@@ -8031,16 +10466,31 @@ Target_arm<big_endian>::do_relax(
          // 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).
+         // code, so the worst case has to be taken into account).  If we are
+         // fixing cortex-a8 errata, the branch range has to be even smaller,
+         // since wide conditional branch has a range of +-1MB only.
          //
          // 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;
+         if (this->fix_cortex_a8_)
+           stub_group_size = 1024276;
+         else
+           stub_group_size = 4170000;
        }
 
       group_sections(layout, stub_group_size, stubs_always_after_branch);
+     
+      // Also fix .ARM.exidx section coverage.
+      Output_section* os = layout->find_output_section(".ARM.exidx");
+      if (os != NULL && os->type() == elfcpp::SHT_ARM_EXIDX)
+       {
+         Arm_output_section<big_endian>* exidx_output_section =
+           Arm_output_section<big_endian>::as_arm_output_section(os);
+         this->fix_exidx_coverage(layout, exidx_output_section, symtab);
+         done_exidx_fixup = true;
+       }
     }
 
   // The Cortex-A8 stubs are sensitive to layout of code sections.  At the
@@ -8080,22 +10530,66 @@ Target_arm<big_endian>::do_relax(
   // or addresses alignments changed.  These are the only things that
   // matter.
   bool any_stub_table_changed = false;
+  Unordered_set<const Output_section*> sections_needing_adjustment;
   for (Stub_table_iterator sp = this->stub_tables_.begin();
        (sp != this->stub_tables_.end()) && !any_stub_table_changed;
        ++sp)
     {
       if ((*sp)->update_data_size_and_addralign())
-       any_stub_table_changed = true;
+       {
+         // Update data size of stub table owner.
+         Arm_input_section<big_endian>* owner = (*sp)->owner();
+         uint64_t address = owner->address();
+         off_t offset = owner->offset();
+         owner->reset_address_and_file_offset();
+         owner->set_address_and_file_offset(address, offset);
+
+         sections_needing_adjustment.insert(owner->output_section());
+         any_stub_table_changed = true;
+       }
+    }
+
+  // Output_section_data::output_section() returns a const pointer but we
+  // need to update output sections, so we record all output sections needing
+  // update above and scan the sections here to find out what sections need
+  // to be updated.
+  for(Layout::Section_list::const_iterator p = layout->section_list().begin();
+      p != layout->section_list().end();
+      ++p)
+    {
+      if (sections_needing_adjustment.find(*p)
+         != sections_needing_adjustment.end())
+       (*p)->set_section_offsets_need_adjustment();
     }
 
+  // Stop relaxation if no EXIDX fix-up and no stub table change.
+  bool continue_relaxation = done_exidx_fixup || any_stub_table_changed;
+
   // Finalize the stubs in the last relaxation pass.
-  if (!any_stub_table_changed)
-    for (Stub_table_iterator sp = this->stub_tables_.begin();
-        (sp != this->stub_tables_.end()) && !any_stub_table_changed;
-        ++sp)
-      (*sp)->finalize_stubs();
+  if (!continue_relaxation)
+    {
+      for (Stub_table_iterator sp = this->stub_tables_.begin();
+          (sp != this->stub_tables_.end()) && !any_stub_table_changed;
+           ++sp)
+       (*sp)->finalize_stubs();
+
+      // Update output local symbol counts of objects if necessary.
+      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);
 
-  return any_stub_table_changed;
+         // Update output local symbol counts.  We need to discard local
+         // symbols defined in parts of input sections that are discarded by
+         // relaxation.
+         if (arm_relobj->output_local_symbol_count_needs_update())
+           arm_relobj->update_output_local_symbol_count();
+       }
+    }
+
+  return continue_relaxation;
 }
 
 // Relocate a stub.
@@ -8427,6 +10921,53 @@ class Target_selector_arm : public Target_selector
   { return new Target_arm<big_endian>(); }
 };
 
+// Fix .ARM.exidx section coverage.
+
+template<bool big_endian>
+void
+Target_arm<big_endian>::fix_exidx_coverage(
+    Layout* layout,
+    Arm_output_section<big_endian>* exidx_section,
+    Symbol_table* symtab)
+{
+  // We need to look at all the input sections in output in ascending
+  // order of of output address.  We do that by building a sorted list
+  // of output sections by addresses.  Then we looks at the output sections
+  // in order.  The input sections in an output section are already sorted
+  // by addresses within the output section.
+
+  typedef std::set<Output_section*, output_section_address_less_than>
+      Sorted_output_section_list;
+  Sorted_output_section_list sorted_output_sections;
+  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)
+    {
+      // We only care about output sections that contain executable code.
+      if (((*p)->flags() & elfcpp::SHF_EXECINSTR) != 0)
+       sorted_output_sections.insert(*p);
+    }
+
+  // Go over the output sections in ascending order of output addresses.
+  typedef typename Arm_output_section<big_endian>::Text_section_list
+      Text_section_list;
+  Text_section_list sorted_text_sections;
+  for(typename Sorted_output_section_list::iterator p =
+       sorted_output_sections.begin();
+      p != sorted_output_sections.end();
+      ++p)
+    {
+      Arm_output_section<big_endian>* arm_output_section =
+       Arm_output_section<big_endian>::as_arm_output_section(*p);
+      arm_output_section->append_text_sections_to_list(&sorted_text_sections);
+    } 
+
+  exidx_section->fix_exidx_coverage(layout, sorted_text_sections, symtab,
+                                   merge_exidx_entries());
+}
+
 Target_selector_arm<false> target_selector_arm;
 Target_selector_arm<true> target_selector_armbe;