bfd/
[deliverable/binutils-gdb.git] / gold / powerpc.cc
index 3d54577c6288fdfa9e5d4d7c971cb3b10848418d..0653ea7167ab0a79a48baab2a471c885c156bf07 100644 (file)
@@ -1,6 +1,6 @@
 // powerpc.cc -- powerpc target support for gold.
 
-// Copyright 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
+// Copyright 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
 // Written by David S. Miller <davem@davemloft.net>
 //        and David Edelsohn <edelsohn@gnu.org>
 
 
 #include "gold.h"
 
+#include <set>
 #include <algorithm>
 #include "elfcpp.h"
+#include "dwarf.h"
 #include "parameters.h"
 #include "reloc.h"
 #include "powerpc.h"
@@ -60,6 +62,9 @@ class Output_data_glink;
 template<int size, bool big_endian>
 class Stub_table;
 
+inline bool
+is_branch_reloc(unsigned int r_type);
+
 template<int size, bool big_endian>
 class Powerpc_relobj : public Sized_relobj_file<size, big_endian>
 {
@@ -71,7 +76,8 @@ public:
   Powerpc_relobj(const std::string& name, Input_file* input_file, off_t offset,
                 const typename elfcpp::Ehdr<size, big_endian>& ehdr)
     : Sized_relobj_file<size, big_endian>(name, input_file, offset, ehdr),
-      special_(0), opd_valid_(false), opd_ent_(), access_from_map_()
+      special_(0), has_small_toc_reloc_(false), opd_valid_(false),
+      opd_ent_(), access_from_map_(), has14_(), stub_table_()
   { }
 
   ~Powerpc_relobj()
@@ -145,43 +151,6 @@ public:
     this->opd_ent_[ndx].discard = true;
   }
 
-  Access_from*
-  access_from_map()
-  { return &this->access_from_map_; }
-
-  // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
-  // section at DST_OFF.
-  void
-  add_reference(Object* src_obj,
-               unsigned int src_indx,
-               typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
-  {
-    Section_id src_id(src_obj, src_indx);
-    this->access_from_map_[dst_off].insert(src_id);
-  }
-
-  // Add a reference to the code section specified by the .opd entry
-  // at DST_OFF
-  void
-  add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
-  {
-    size_t ndx = this->opd_ent_ndx(dst_off);
-    if (ndx >= this->opd_ent_.size())
-      this->opd_ent_.resize(ndx + 1);
-    this->opd_ent_[ndx].gc_mark = true;
-  }
-
-  void
-  process_gc_mark(Symbol_table* symtab)
-  {
-    for (size_t i = 0; i < this->opd_ent_.size(); i++)
-      if (this->opd_ent_[i].gc_mark)
-       {
-         unsigned int shndx = this->opd_ent_[i].shndx;
-         symtab->gc()->worklist().push(Section_id(this, shndx));
-       }
-  }
-
   bool
   opd_valid() const
   { return this->opd_valid_; }
@@ -220,12 +189,57 @@ public:
     return true;
   }
 
+  Access_from*
+  access_from_map()
+  { return &this->access_from_map_; }
+
+  // Add a reference from SRC_OBJ, SRC_INDX to this object's .opd
+  // section at DST_OFF.
+  void
+  add_reference(Object* src_obj,
+               unsigned int src_indx,
+               typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
+  {
+    Section_id src_id(src_obj, src_indx);
+    this->access_from_map_[dst_off].insert(src_id);
+  }
+
+  // Add a reference to the code section specified by the .opd entry
+  // at DST_OFF
+  void
+  add_gc_mark(typename elfcpp::Elf_types<size>::Elf_Addr dst_off)
+  {
+    size_t ndx = this->opd_ent_ndx(dst_off);
+    if (ndx >= this->opd_ent_.size())
+      this->opd_ent_.resize(ndx + 1);
+    this->opd_ent_[ndx].gc_mark = true;
+  }
+
+  void
+  process_gc_mark(Symbol_table* symtab)
+  {
+    for (size_t i = 0; i < this->opd_ent_.size(); i++)
+      if (this->opd_ent_[i].gc_mark)
+       {
+         unsigned int shndx = this->opd_ent_[i].shndx;
+         symtab->gc()->worklist().push(Section_id(this, shndx));
+       }
+  }
+
   // Return offset in output GOT section that this object will use
   // as a TOC pointer.  Won't be just a constant with multi-toc support.
   Address
   toc_base_offset() const
   { return 0x8000; }
 
+  void
+  set_has_small_toc_reloc()
+  { has_small_toc_reloc_ = true; }
+
+  bool
+  has_small_toc_reloc() const
+  { return has_small_toc_reloc_; }
+
   void
   set_has_14bit_branch(unsigned int shndx)
   {
@@ -280,6 +294,10 @@ private:
   // For 32-bit the .got2 section shdnx, for 64-bit the .opd section shndx.
   unsigned int special_;
 
+  // For 64-bit, whether this object uses small model relocs to access
+  // the toc.
+  bool has_small_toc_reloc_;
+
   // Set at the start of gc_process_relocs, when we know opd_ent_
   // vector is valid.  The flag could be made atomic and set in
   // do_read_relocs with memory_order_release and then tested with
@@ -305,6 +323,109 @@ private:
   std::vector<Stub_table<size, big_endian>*> stub_table_;
 };
 
+template<int size, bool big_endian>
+class Powerpc_dynobj : public Sized_dynobj<size, big_endian>
+{
+public:
+  typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
+
+  Powerpc_dynobj(const std::string& name, Input_file* input_file, off_t offset,
+                const typename elfcpp::Ehdr<size, big_endian>& ehdr)
+    : Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr),
+      opd_shndx_(0), opd_ent_()
+  { }
+
+  ~Powerpc_dynobj()
+  { }
+
+  // Call Sized_dynobj::do_read_symbols to read the symbols then
+  // read .opd from a dynamic object, filling in opd_ent_ vector,
+  void
+  do_read_symbols(Read_symbols_data*);
+
+  // The .opd section shndx.
+  unsigned int
+  opd_shndx() const
+  {
+    return this->opd_shndx_;
+  }
+
+  // The .opd section address.
+  Address
+  opd_address() const
+  {
+    return this->opd_address_;
+  }
+
+  // Init OPD entry arrays.
+  void
+  init_opd(size_t opd_size)
+  {
+    size_t count = this->opd_ent_ndx(opd_size);
+    this->opd_ent_.resize(count);
+  }
+
+  // Return section and offset of function entry for .opd + R_OFF.
+  unsigned int
+  get_opd_ent(Address r_off, Address* value = NULL) const
+  {
+    size_t ndx = this->opd_ent_ndx(r_off);
+    gold_assert(ndx < this->opd_ent_.size());
+    gold_assert(this->opd_ent_[ndx].shndx != 0);
+    if (value != NULL)
+      *value = this->opd_ent_[ndx].off;
+    return this->opd_ent_[ndx].shndx;
+  }
+
+  // Set section and offset of function entry for .opd + R_OFF.
+  void
+  set_opd_ent(Address r_off, unsigned int shndx, Address value)
+  {
+    size_t ndx = this->opd_ent_ndx(r_off);
+    gold_assert(ndx < this->opd_ent_.size());
+    this->opd_ent_[ndx].shndx = shndx;
+    this->opd_ent_[ndx].off = value;
+  }
+
+private:
+  // Used to specify extent of executable sections.
+  struct Sec_info
+  {
+    Sec_info(Address start_, Address len_, unsigned int shndx_)
+      : start(start_), len(len_), shndx(shndx_)
+    { }
+
+    bool
+    operator<(const Sec_info& that) const
+    { return this->start < that.start; }
+
+    Address start;
+    Address len;
+    unsigned int shndx;
+  };
+
+  struct Opd_ent
+  {
+    unsigned int shndx;
+    Address off;
+  };
+
+  // Return index into opd_ent_ array for .opd entry at OFF.
+  size_t
+  opd_ent_ndx(size_t off) const
+  { return off >> 4;}
+
+  // For 64-bit the .opd section shndx and address.
+  unsigned int opd_shndx_;
+  Address opd_address_;
+
+  // The first 8-byte word of an OPD entry gives the address of the
+  // entry point of the function.  Records the section and offset
+  // corresponding to the address.  Note that in dynamic objects,
+  // offset is *not* relative to the section.
+  std::vector<Opd_ent> opd_ent_;
+};
+
 template<int size, bool big_endian>
 class Target_powerpc : public Sized_target<size, big_endian>
 {
@@ -381,6 +502,10 @@ class Target_powerpc : public Sized_target<size, big_endian>
   bool
   do_relax(int, const Input_objects*, Symbol_table*, Layout*, const Task*);
 
+  void
+  do_plt_fde_location(const Output_data*, unsigned char*,
+                     uint64_t*, off_t*) const;
+
   // Stash info about branches, for stub generation.
   void
   push_branch(Powerpc_relobj<size, big_endian>* ppc_object,
@@ -430,6 +555,13 @@ class Target_powerpc : public Sized_target<size, big_endian>
   int64_t
   do_tls_offset_for_global(Symbol* gsym, unsigned int got_indx) const;
 
+  void
+  do_function_location(Symbol_location*) const;
+
+  bool
+  do_can_check_for_function_pointers() const
+  { return true; }
+
   // Relocate a section.
   void
   relocate_section(const Relocate_info<size, big_endian>*,
@@ -513,6 +645,9 @@ class Target_powerpc : public Sized_target<size, big_endian>
     return this->glink_;
   }
 
+  bool has_glink() const
+  { return this->glink_ != NULL; }
+
   // Get the GOT section.
   const Output_data_got_powerpc<size, big_endian>*
   got_section() const
@@ -610,14 +745,102 @@ class Target_powerpc : public Sized_target<size, big_endian>
 
  private:
 
+  class Track_tls
+  {
+  public:
+    enum Tls_get_addr
+    {
+      NOT_EXPECTED = 0,
+      EXPECTED = 1,
+      SKIP = 2,
+      NORMAL = 3
+    };
+
+    Track_tls()
+      : tls_get_addr_(NOT_EXPECTED),
+       relinfo_(NULL), relnum_(0), r_offset_(0)
+    { }
+
+    ~Track_tls()
+    {
+      if (this->tls_get_addr_ != NOT_EXPECTED)
+       this->missing();
+    }
+
+    void
+    missing(void)
+    {
+      if (this->relinfo_ != NULL)
+       gold_error_at_location(this->relinfo_, this->relnum_, this->r_offset_,
+                              _("missing expected __tls_get_addr call"));
+    }
+
+    void
+    expect_tls_get_addr_call(
+       const Relocate_info<size, big_endian>* relinfo,
+       size_t relnum,
+       Address r_offset)
+    {
+      this->tls_get_addr_ = EXPECTED;
+      this->relinfo_ = relinfo;
+      this->relnum_ = relnum;
+      this->r_offset_ = r_offset;
+    }
+
+    void
+    expect_tls_get_addr_call()
+    { this->tls_get_addr_ = EXPECTED; }
+
+    void
+    skip_next_tls_get_addr_call()
+    {this->tls_get_addr_ = SKIP; }
+
+    Tls_get_addr
+    maybe_skip_tls_get_addr_call(unsigned int r_type, const Symbol* gsym)
+    {
+      bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
+                          || r_type == elfcpp::R_PPC_PLTREL24)
+                         && gsym != NULL
+                         && strcmp(gsym->name(), "__tls_get_addr") == 0);
+      Tls_get_addr last_tls = this->tls_get_addr_;
+      this->tls_get_addr_ = NOT_EXPECTED;
+      if (is_tls_call && last_tls != EXPECTED)
+       return last_tls;
+      else if (!is_tls_call && last_tls != NOT_EXPECTED)
+       {
+         this->missing();
+         return EXPECTED;
+       }
+      return NORMAL;
+    }
+
+  private:
+    // What we're up to regarding calls to __tls_get_addr.
+    // On powerpc, the branch and link insn making a call to
+    // __tls_get_addr is marked with a relocation, R_PPC64_TLSGD,
+    // R_PPC64_TLSLD, R_PPC_TLSGD or R_PPC_TLSLD, in addition to the
+    // usual R_POWERPC_REL24 or R_PPC_PLTREL25 relocation on a call.
+    // The marker relocation always comes first, and has the same
+    // symbol as the reloc on the insn setting up the __tls_get_addr
+    // argument.  This ties the arg setup insn with the call insn,
+    // allowing ld to safely optimize away the call.  We check that
+    // every call to __tls_get_addr has a marker relocation, and that
+    // every marker relocation is on a call to __tls_get_addr.
+    Tls_get_addr tls_get_addr_;
+    // Info about the last reloc for error message.
+    const Relocate_info<size, big_endian>* relinfo_;
+    size_t relnum_;
+    Address r_offset_;
+  };
+
   // The class which scans relocations.
-  class Scan
+  class Scan : protected Track_tls
   {
   public:
     typedef typename elfcpp::Elf_types<size>::Elf_Addr Address;
 
     Scan()
-      : issued_non_pic_error_(false)
+      : Track_tls(), issued_non_pic_error_(false)
     { }
 
     static inline int
@@ -647,9 +870,18 @@ class Target_powerpc : public Sized_target<size, big_endian>
                                        unsigned int ,
                                        Output_section* ,
                                        const elfcpp::Rela<size, big_endian>& ,
-                                       unsigned int ,
+                                       unsigned int r_type,
                                        const elfcpp::Sym<size, big_endian>&)
-    { return false; }
+    {
+      // PowerPC64 .opd is not folded, so any identical function text
+      // may be folded and we'll still keep function addresses distinct.
+      // That means no reloc is of concern here.
+      if (size == 64)
+       return false;
+      // For 32-bit, conservatively assume anything but calls to
+      // function code might be taking the address of the function.
+      return !is_branch_reloc(r_type);
+    }
 
     inline bool
     global_reloc_may_be_function_pointer(Symbol_table* , Layout* ,
@@ -657,10 +889,19 @@ class Target_powerpc : public Sized_target<size, big_endian>
                                         Sized_relobj_file<size, big_endian>* ,
                                         unsigned int ,
                                         Output_section* ,
-                                        const elfcpp::Rela<size,
-                                                           big_endian>& ,
-                                        unsigned int , Symbol*)
-    { return false; }
+                                        const elfcpp::Rela<size, big_endian>& ,
+                                        unsigned int r_type,
+                                        Symbol*)
+    {
+      // As above.
+      if (size == 64)
+       return false;
+      return !is_branch_reloc(r_type);
+    }
+
+    static bool
+    reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
+                             unsigned int r_type, bool report_err);
 
   private:
     static void
@@ -678,47 +919,28 @@ class Target_powerpc : public Sized_target<size, big_endian>
     void
     check_non_pic(Relobj*, unsigned int r_type);
 
-    bool
-    reloc_needs_plt_for_ifunc(Sized_relobj_file<size, big_endian>* object,
-                             unsigned int r_type);
-
     // Whether we have issued an error about a non-PIC compilation.
     bool issued_non_pic_error_;
   };
 
   Address
-  symval_for_branch(Address value, const Sized_symbol<size>* gsym,
+  symval_for_branch(const Symbol_table* symtab, Address value,
+                   const Sized_symbol<size>* gsym,
                    Powerpc_relobj<size, big_endian>* object,
                    unsigned int *dest_shndx);
 
   // The class which implements relocation.
-  class Relocate
+  class Relocate : protected Track_tls
   {
    public:
     // Use 'at' branch hints when true, 'y' when false.
     // FIXME maybe: set this with an option.
     static const bool is_isa_v2 = true;
 
-    enum skip_tls
-    {
-      CALL_NOT_EXPECTED = 0,
-      CALL_EXPECTED = 1,
-      CALL_SKIP = 2
-    };
-
     Relocate()
-      : call_tls_get_addr_(CALL_NOT_EXPECTED)
+      : Track_tls()
     { }
 
-    ~Relocate()
-    {
-      if (this->call_tls_get_addr_ != CALL_NOT_EXPECTED)
-       {
-         // FIXME: This needs to specify the location somehow.
-         gold_error(_("missing expected __tls_get_addr call"));
-       }
-    }
-
     // Do a relocation.  Return false if the caller should not issue
     // any warnings about this relocation.
     inline bool
@@ -730,10 +952,6 @@ class Target_powerpc : public Sized_target<size, big_endian>
             unsigned char*,
             typename elfcpp::Elf_types<size>::Elf_Addr,
             section_size_type);
-
-    // This is set if we should skip the next reloc, which should be a
-    // call to __tls_get_addr.
-    enum skip_tls call_tls_get_addr_;
   };
 
   class Relocate_comdat_behavior
@@ -850,6 +1068,10 @@ class Target_powerpc : public Sized_target<size, big_endian>
   Reloc_section*
   rela_dyn_section(Layout*);
 
+  // Similarly, but for ifunc symbols get the one for ifunc.
+  Reloc_section*
+  rela_dyn_section(Symbol_table*, Layout*, bool for_ifunc);
+
   // Copy a relocation against a global symbol.
   void
   copy_reloc(Symbol_table* symtab, Layout* layout,
@@ -863,7 +1085,7 @@ class Target_powerpc : public Sized_target<size, big_endian>
                                  reloc, this->rela_dyn_section(layout));
   }
 
-  // Look over all the input sections, deciding where to place stub.
+  // Look over all the input sections, deciding where to place stubs.
   void
   group_sections(Layout*, const Task*);
 
@@ -926,9 +1148,30 @@ class Target_powerpc : public Sized_target<size, big_endian>
 
   // The GOT section.
   Output_data_got_powerpc<size, big_endian>* got_;
-  // The PLT section.
+  // The PLT section.  This is a container for a table of addresses,
+  // and their relocations.  Each address in the PLT has a dynamic
+  // relocation (R_*_JMP_SLOT) and each address will have a
+  // corresponding entry in .glink for lazy resolution of the PLT.
+  // ppc32 initialises the PLT to point at the .glink entry, while
+  // ppc64 leaves this to ld.so.  To make a call via the PLT, the
+  // linker adds a stub that loads the PLT entry into ctr then
+  // branches to ctr.  There may be more than one stub for each PLT
+  // entry.  DT_JMPREL points at the first PLT dynamic relocation and
+  // DT_PLTRELSZ gives the total size of PLT dynamic relocations.
   Output_data_plt_powerpc<size, big_endian>* plt_;
-  // The IPLT section.
+  // The IPLT section.  Like plt_, this is a container for a table of
+  // addresses and their relocations, specifically for STT_GNU_IFUNC
+  // functions that resolve locally (STT_GNU_IFUNC functions that
+  // don't resolve locally go in PLT).  Unlike plt_, these have no
+  // entry in .glink for lazy resolution, and the relocation section
+  // does not have a 1-1 correspondence with IPLT addresses.  In fact,
+  // the relocation section may contain relocations against
+  // STT_GNU_IFUNC symbols at locations outside of IPLT.  The
+  // relocation section will appear at the end of other dynamic
+  // relocations, so that ld.so applies these relocations after other
+  // dynamic relocations.  In a static executable, the relocation
+  // section is emitted and marked with __rela_iplt_start and
+  // __rela_iplt_end symbols.
   Output_data_plt_powerpc<size, big_endian>* iplt_;
   // Section holding long branch destinations.
   Output_data_brlt_powerpc<size, big_endian>* brlt_section_;
@@ -1428,8 +1671,9 @@ Powerpc_relobj<size, big_endian>::do_find_special_sections(
   section_size_type names_size = sd->section_names_size;
   const unsigned char* s;
 
-  s = this->find_shdr(pshdrs, size == 32 ? ".got2" : ".opd",
-                     names, names_size, NULL);
+  s = this->template find_shdr<size, big_endian>(pshdrs,
+                                                size == 32 ? ".got2" : ".opd",
+                                                names, names_size, NULL);
   if (s != NULL)
     {
       unsigned int ndx = (s - pshdrs) / elfcpp::Elf_sizes<size>::shdr_size;
@@ -1544,6 +1788,105 @@ Powerpc_relobj<size, big_endian>::do_read_relocs(Read_relocs_data* rd)
     }
 }
 
+// Call Sized_dynobj::do_read_symbols to read the symbols then
+// read .opd from a dynamic object, filling in opd_ent_ vector,
+
+template<int size, bool big_endian>
+void
+Powerpc_dynobj<size, big_endian>::do_read_symbols(Read_symbols_data* sd)
+{
+  Sized_dynobj<size, big_endian>::do_read_symbols(sd);
+  if (size == 64)
+    {
+      const int shdr_size = elfcpp::Elf_sizes<size>::shdr_size;
+      const unsigned char* const pshdrs = sd->section_headers->data();
+      const unsigned char* namesu = sd->section_names->data();
+      const char* names = reinterpret_cast<const char*>(namesu);
+      const unsigned char* s = NULL;
+      const unsigned char* opd;
+      section_size_type opd_size;
+
+      // Find and read .opd section.
+      while (1)
+       {
+         s = this->template find_shdr<size, big_endian>(pshdrs, ".opd", names,
+                                                        sd->section_names_size,
+                                                        s);
+         if (s == NULL)
+           return;
+
+         typename elfcpp::Shdr<size, big_endian> shdr(s);
+         if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
+             && (shdr.get_sh_flags() & elfcpp::SHF_ALLOC) != 0)
+           {
+             this->opd_shndx_ = (s - pshdrs) / shdr_size;
+             this->opd_address_ = shdr.get_sh_addr();
+             opd_size = convert_to_section_size_type(shdr.get_sh_size());
+             opd = this->get_view(shdr.get_sh_offset(), opd_size,
+                                  true, false);
+             break;
+           }
+       }
+
+      // Build set of executable sections.
+      // Using a set is probably overkill.  There is likely to be only
+      // a few executable sections, typically .init, .text and .fini,
+      // and they are generally grouped together.
+      typedef std::set<Sec_info> Exec_sections;
+      Exec_sections exec_sections;
+      s = pshdrs;
+      for (unsigned int i = 1; i < this->shnum(); ++i, s += shdr_size)
+       {
+         typename elfcpp::Shdr<size, big_endian> shdr(s);
+         if (shdr.get_sh_type() == elfcpp::SHT_PROGBITS
+             && ((shdr.get_sh_flags()
+                  & (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
+                 == (elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR))
+             && shdr.get_sh_size() != 0)
+           {
+             exec_sections.insert(Sec_info(shdr.get_sh_addr(),
+                                           shdr.get_sh_size(), i));
+           }
+       }
+      if (exec_sections.empty())
+       return;
+
+      // Look over the OPD entries.  This is complicated by the fact
+      // that some binaries will use two-word entries while others
+      // will use the standard three-word entries.  In most cases
+      // the third word (the environment pointer for languages like
+      // Pascal) is unused and will be zero.  If the third word is
+      // used it should not be pointing into executable sections,
+      // I think.
+      this->init_opd(opd_size);
+      for (const unsigned char* p = opd; p < opd + opd_size; p += 8)
+       {
+         typedef typename elfcpp::Swap<64, big_endian>::Valtype Valtype;
+         const Valtype* valp = reinterpret_cast<const Valtype*>(p);
+         Valtype val = elfcpp::Swap<64, big_endian>::readval(valp);
+         if (val == 0)
+           // Chances are that this is the third word of an OPD entry.
+           continue;
+         typename Exec_sections::const_iterator e
+           = exec_sections.upper_bound(Sec_info(val, 0, 0));
+         if (e != exec_sections.begin())
+           {
+             --e;
+             if (e->start <= val && val < e->start + e->len)
+               {
+                 // We have an address in an executable section.
+                 // VAL ought to be the function entry, set it up.
+                 this->set_opd_ent(p - opd, e->shndx, val);
+                 // Skip second word of OPD entry, the TOC pointer.
+                 p += 8;
+               }
+           }
+         // If we didn't match any executable sections, we likely
+         // have a non-zero third word in the OPD entry.
+       }
+    }
+}
+
 // Set up some symbols.
 
 template<int size, bool big_endian>
@@ -1616,8 +1959,8 @@ Target_powerpc<size, big_endian>::do_make_elf_object(
     }
   else if (et == elfcpp::ET_DYN)
     {
-      Sized_dynobj<size, big_endian>* obj =
-       new Sized_dynobj<size, big_endian>(name, input_file, offset, ehdr);
+      Powerpc_dynobj<size, big_endian>* obj =
+       new Powerpc_dynobj<size, big_endian>(name, input_file, offset, ehdr);
       obj->setup();
       return obj;
     }
@@ -1804,6 +2147,22 @@ Target_powerpc<size, big_endian>::rela_dyn_section(Layout* layout)
   return this->rela_dyn_;
 }
 
+// Similarly, but for ifunc symbols get the one for ifunc.
+
+template<int size, bool big_endian>
+typename Target_powerpc<size, big_endian>::Reloc_section*
+Target_powerpc<size, big_endian>::rela_dyn_section(Symbol_table* symtab,
+                                                  Layout* layout,
+                                                  bool for_ifunc)
+{
+  if (!for_ifunc)
+    return this->rela_dyn_section(layout);
+
+  if (this->iplt_ == NULL)
+    this->make_iplt_section(symtab, layout);
+  return this->iplt_->rel_plt();
+}
+
 class Stub_control
 {
  public:
@@ -1867,7 +2226,7 @@ class Stub_control
   Output_section* output_section_;
 };
 
-// Return true iff input section can be handled by current stub/
+// Return true iff input section can be handled by current stub
 // group.
 
 bool
@@ -1899,7 +2258,9 @@ Stub_control::can_add_to_stub_group(Output_section* o,
                 i->relobj()->section_name(i->shndx()).c_str());
 
   if (this->state_ != HAS_STUB_SECTION
-      && (!whole_sec || this->output_section_ != o))
+      && (!whole_sec || this->output_section_ != o)
+      && (this->state_ == NO_GROUP
+         || this->group_end_addr_ - end_addr < group_size))
     {
       this->owner_ = i;
       this->output_section_ = o;
@@ -1972,7 +2333,25 @@ Target_powerpc<size, big_endian>::group_sections(Layout* layout,
        }
     }
   if (stub_table != NULL)
-    stub_table->init(stub_control.owner(), stub_control.output_section());
+    {
+      const Output_section::Input_section* i = stub_control.owner();
+      if (!i->is_input_section())
+       {
+         // Corner case.  A new stub group was made for the first
+         // section (last one looked at here) for some reason, but
+         // the first section is already being used as the owner for
+         // a stub table for following sections.  Force it into that
+         // stub group.
+         gold_assert(this->stub_tables_.size() >= 2);
+         this->stub_tables_.pop_back();
+         delete stub_table;
+         Powerpc_relobj<size, big_endian>* ppcobj = static_cast
+           <Powerpc_relobj<size, big_endian>*>(i->relobj());
+         ppcobj->set_stub_table(i->shndx(), this->stub_tables_.back());
+       }
+      else
+       stub_table->init(i, stub_control.output_section());
+    }
 }
 
 // If this branch needs a plt call stub, or a long branch stub, make one.
@@ -2067,18 +2446,29 @@ Target_powerpc<size, big_endian>::Branch_info::make_stub(
            return;
          to = symval.value(this->object_, 0);
        }
+      to += this->addend_;
       if (stub_table == NULL)
        stub_table = this->object_->stub_table(this->shndx_);
-      gold_assert(stub_table != NULL);
       if (size == 64 && is_branch_reloc(this->r_type_))
        {
          unsigned int dest_shndx;
-         to = stub_table->targ()->symval_for_branch(to, gsym, this->object_,
-                                                    &dest_shndx);
+         Target_powerpc<size, big_endian>* target =
+           static_cast<Target_powerpc<size, big_endian>*>(
+               parameters->sized_target<size, big_endian>());
+         to = target->symval_for_branch(symtab, to, gsym,
+                                        this->object_, &dest_shndx);
        }
       Address delta = to - from;
       if (delta + max_branch_offset >= 2 * max_branch_offset)
        {
+         if (stub_table == NULL)
+           {
+             gold_warning(_("%s:%s: branch in non-executable section,"
+                            " no long branch stub for you"),
+                          this->object_->name().c_str(),
+                          this->object_->section_name(this->shndx_).c_str());
+             return;
+           }
          stub_table->add_long_branch_entry(this->object_, to);
        }
     }
@@ -2100,7 +2490,7 @@ Target_powerpc<size, big_endian>::do_relax(int pass,
       bool thread_safe = parameters->options().plt_thread_safe();
       if (size == 64 && !parameters->options().user_set_plt_thread_safe())
        {
-         const char* const thread_starter[] =
+         static const char* const thread_starter[] =
            {
              "pthread_create",
              /* libstdc++ */
@@ -2119,14 +2509,21 @@ Target_powerpc<size, big_endian>::do_relax(int pass,
              "GOMP_parallel_sections_start", 
            };
 
-         for (unsigned int i = 0;
-              i < sizeof(thread_starter) / sizeof(thread_starter[0]);
-              i++)
+         if (parameters->options().shared())
+           thread_safe = true;
+         else
            {
-             Symbol* sym = symtab->lookup(thread_starter[i], NULL);
-             thread_safe = sym != NULL && sym->in_reg() && sym->in_real_elf();
-             if (thread_safe)
-               break;
+             for (unsigned int i = 0;
+                  i < sizeof(thread_starter) / sizeof(thread_starter[0]);
+                  i++)
+               {
+                 Symbol* sym = symtab->lookup(thread_starter[i], NULL);
+                 thread_safe = (sym != NULL
+                                && sym->in_reg()
+                                && sym->in_real_elf());
+                 if (thread_safe)
+                   break;
+               }
            }
        }
       this->plt_thread_safe_ = thread_safe;
@@ -2188,6 +2585,7 @@ Target_powerpc<size, big_endian>::do_relax(int pass,
       if ((*p)->size_update())
        {
          again = true;
+         (*p)->add_eh_frame(layout);
          os_need_update.insert((*p)->output_section());
        }
     }
@@ -2219,8 +2617,8 @@ Target_powerpc<size, big_endian>::do_relax(int pass,
          else
            off += i->data_size();
        }
-      // If .brlt is part of this output section, then we have just
-      // done the offset adjustment.
+      // If .branch_lt is part of this output section, then we have
+      // just done the offset adjustment.
       os->clear_section_offsets_need_adjustment();
     }
 
@@ -2230,7 +2628,7 @@ Target_powerpc<size, big_endian>::do_relax(int pass,
       && parameters->options().output_is_position_independent())
     {
       // Fill in the BRLT relocs.
-      this->brlt_section_->reset_data_size();
+      this->brlt_section_->reset_brlt_sizes();
       for (typename Branch_lookup_table::const_iterator p
             = this->branch_lookup_table_.begin();
           p != this->branch_lookup_table_.end();
@@ -2238,11 +2636,59 @@ Target_powerpc<size, big_endian>::do_relax(int pass,
        {
          this->brlt_section_->add_reloc(p->first, p->second);
        }
-      this->brlt_section_->finalize_data_size();
+      this->brlt_section_->finalize_brlt_sizes();
     }
   return again;
 }
 
+template<int size, bool big_endian>
+void
+Target_powerpc<size, big_endian>::do_plt_fde_location(const Output_data* plt,
+                                                     unsigned char* oview,
+                                                     uint64_t* paddress,
+                                                     off_t* plen) const
+{
+  uint64_t address = plt->address();
+  off_t len = plt->data_size();
+
+  if (plt == this->glink_)
+    {
+      // See Output_data_glink::do_write() for glink contents.
+      if (size == 64)
+       {
+         // There is one word before __glink_PLTresolve
+         address += 8;
+         len -= 8;
+       }
+      else if (parameters->options().output_is_position_independent())
+       {
+         // There are two FDEs for a position independent glink.
+         // The first covers the branch table, the second
+         // __glink_PLTresolve at the end of glink.
+         off_t resolve_size = this->glink_->pltresolve_size;
+         if (oview[9] == 0)
+           len -= resolve_size;
+         else
+           {
+             address += len - resolve_size;
+             len = resolve_size;
+           }
+       }
+    }
+  else
+    {
+      // Must be a stub table.
+      const Stub_table<size, big_endian>* stub_table
+       = static_cast<const Stub_table<size, big_endian>*>(plt);
+      uint64_t stub_address = stub_table->stub_address();
+      len -= stub_address - address;
+      address = stub_address;
+    }
+
+  *paddress = address;
+  *plen = len;
+}
+
 // A class to handle the PLT data.
 
 template<int size, bool big_endian>
@@ -2284,6 +2730,8 @@ class Output_data_plt_powerpc : public Output_section_data_build
   unsigned int
   entry_count() const
   {
+    if (this->current_data_size() == 0)
+      return 0;
     return ((this->current_data_size() - this->initial_plt_entry_size_)
            / plt_entry_size);
   }
@@ -2462,7 +2910,7 @@ template<int size, bool big_endian>
 void
 Output_data_plt_powerpc<size, big_endian>::do_write(Output_file* of)
 {
-  if (size == 32)
+  if (size == 32 && this->name_[3] != 'I')
     {
       const section_size_type offset = this->offset();
       const section_size_type oview_size
@@ -2504,7 +2952,7 @@ Target_powerpc<size, big_endian>::make_plt_section(Symbol_table* symtab,
 
       // Ensure that .rela.dyn always appears before .rela.plt  This is
       // necessary due to how, on PowerPC and some other targets, .rela.dyn
-      // needs to include .rela.plt in it's range.
+      // needs to include .rela.plt in its range.
       this->rela_dyn_section(layout);
 
       Reloc_section* plt_rel = new Reloc_section(false);
@@ -2565,6 +3013,20 @@ class Output_data_brlt_powerpc : public Output_section_data_build
       targ_(targ)
   { }
 
+  void
+  reset_brlt_sizes()
+  {
+    this->reset_data_size();
+    this->rel_->reset_data_size();
+  }
+
+  void
+  finalize_brlt_sizes()
+  {
+    this->finalize_data_size();
+    this->rel_->finalize_data_size();
+  }
+
   // Add a reloc for an entry in the BRLT.
   void
   add_reloc(Address to, unsigned int off)
@@ -2625,8 +3087,8 @@ Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
       bool is_pic = parameters->options().output_is_position_independent();
       if (is_pic)
        {
-         // When PIC we can't fill in .brlt (like .plt it can be a
-         // bss style section) but must initialise at runtime via
+         // When PIC we can't fill in .branch_lt (like .plt it can be
+         // bss style section) but must initialise at runtime via
          // dynamic relocats.
          this->rela_dyn_section(layout);
          brlt_rel = new Reloc_section(false);
@@ -2638,7 +3100,7 @@ Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
        this->plt_->output_section()
          ->add_output_section_data(this->brlt_section_);
       else
-       layout->add_output_section_data(".brlt",
+       layout->add_output_section_data(".branch_lt",
                                        (is_pic ? elfcpp::SHT_NOBITS
                                         : elfcpp::SHT_PROGBITS),
                                        elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
@@ -2649,7 +3111,7 @@ Target_powerpc<size, big_endian>::make_brlt_section(Layout* layout)
     }
 }
 
-// Write out .brlt when non-PIC.
+// Write out .branch_lt when non-PIC.
 
 template<int size, bool big_endian>
 void
@@ -2685,6 +3147,60 @@ ha(uint32_t a)
   return hi(a + 0x8000);
 }
 
+template<int size>
+struct Eh_cie
+{
+  static const unsigned char eh_frame_cie[12];
+};
+
+template<int size>
+const unsigned char Eh_cie<size>::eh_frame_cie[] =
+{
+  1,                                   // CIE version.
+  'z', 'R', 0,                         // Augmentation string.
+  4,                                   // Code alignment.
+  0x80 - size / 8 ,                    // Data alignment.
+  65,                                  // RA reg.
+  1,                                   // Augmentation size.
+  (elfcpp::DW_EH_PE_pcrel
+   | elfcpp::DW_EH_PE_sdata4),         // FDE encoding.
+  elfcpp::DW_CFA_def_cfa, 1, 0         // def_cfa: r1 offset 0.
+};
+
+// Describe __glink_PLTresolve use of LR, 64-bit version.
+static const unsigned char glink_eh_frame_fde_64[] =
+{
+  0, 0, 0, 0,                          // Replaced with offset to .glink.
+  0, 0, 0, 0,                          // Replaced with size of .glink.
+  0,                                   // Augmentation size.
+  elfcpp::DW_CFA_advance_loc + 1,
+  elfcpp::DW_CFA_register, 65, 12,
+  elfcpp::DW_CFA_advance_loc + 4,
+  elfcpp::DW_CFA_restore_extended, 65
+};
+
+// Describe __glink_PLTresolve use of LR, 32-bit version.
+static const unsigned char glink_eh_frame_fde_32[] =
+{
+  0, 0, 0, 0,                          // Replaced with offset to .glink.
+  0, 0, 0, 0,                          // Replaced with size of .glink.
+  0,                                   // Augmentation size.
+  elfcpp::DW_CFA_advance_loc + 2,
+  elfcpp::DW_CFA_register, 65, 0,
+  elfcpp::DW_CFA_advance_loc + 4,
+  elfcpp::DW_CFA_restore_extended, 65
+};
+
+static const unsigned char default_fde[] =
+{
+  0, 0, 0, 0,                          // Replaced with offset to stubs.
+  0, 0, 0, 0,                          // Replaced with size of stubs.
+  0,                                   // Augmentation size.
+  elfcpp::DW_CFA_nop,                  // Pad.
+  elfcpp::DW_CFA_nop,
+  elfcpp::DW_CFA_nop
+};
+
 template<bool big_endian>
 static inline void
 write_insn(unsigned char* p, uint32_t v)
@@ -2708,7 +3224,7 @@ class Stub_table : public Output_relaxed_input_section
     : Output_relaxed_input_section(NULL, 0, 0),
       targ_(targ), plt_call_stubs_(), long_branch_stubs_(),
       orig_data_size_(0), plt_size_(0), last_plt_size_(0),
-      branch_size_(0), last_branch_size_(0)
+      branch_size_(0), last_branch_size_(0), eh_frame_added_(false)
   { }
 
   // Delayed Output_relaxed_input_section init.
@@ -2753,7 +3269,8 @@ class Stub_table : public Output_relaxed_input_section
   add_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
 
   Address
-  find_long_branch_entry(const Powerpc_relobj<size, big_endian>*, Address);
+  find_long_branch_entry(const Powerpc_relobj<size, big_endian>*,
+                        Address) const;
 
   void
   clear_stubs()
@@ -2782,14 +3299,14 @@ class Stub_table : public Output_relaxed_input_section
   }
 
   Address
-  stub_address()
+  stub_address() const
   {
     return align_address(this->address() + this->orig_data_size_,
                         this->stub_align());
   }
 
   Address
-  stub_offset()
+  stub_offset() const
   {
     return align_address(this->offset() + this->orig_data_size_,
                         this->stub_align());
@@ -2830,6 +3347,35 @@ class Stub_table : public Output_relaxed_input_section
     return false;
   }
 
+  // Add .eh_frame info for this stub section.  Unlike other linker
+  // generated .eh_frame this is added late in the link, because we
+  // only want the .eh_frame info if this particular stub section is
+  // non-empty.
+  void
+  add_eh_frame(Layout* layout)
+  {
+    if (!this->eh_frame_added_)
+      {
+       if (!parameters->options().ld_generated_unwind_info())
+         return;
+
+       // Since we add stub .eh_frame info late, it must be placed
+       // after all other linker generated .eh_frame info so that
+       // merge mapping need not be updated for input sections.
+       // There is no provision to use a different CIE to that used
+       // by .glink.
+       if (!this->targ_->has_glink())
+         return;
+
+       layout->add_eh_frame_for_plt(this,
+                                    Eh_cie<size>::eh_frame_cie,
+                                    sizeof (Eh_cie<size>::eh_frame_cie),
+                                    default_fde,
+                                    sizeof (default_fde));
+       this->eh_frame_added_ = true;
+      }
+  }
+
   Target_powerpc<size, big_endian>*
   targ() const
   { return targ_; }
@@ -2851,6 +3397,26 @@ class Stub_table : public Output_relaxed_input_section
     return std::max(user_align, min_align);
   }
 
+  // Return the plt offset for the given call stub.
+  Address
+  plt_off(typename Plt_stub_entries::const_iterator p, bool* is_iplt) const
+  {
+    const Symbol* gsym = p->first.sym_;
+    if (gsym != NULL)
+      {
+       *is_iplt = (gsym->type() == elfcpp::STT_GNU_IFUNC
+                   && gsym->can_use_relative_reloc(false));
+       return gsym->plt_offset();
+      }
+    else
+      {
+       *is_iplt = true;
+       const Sized_relobj_file<size, big_endian>* relobj = p->first.object_;
+       unsigned int local_sym_index = p->first.locsym_;
+       return relobj->local_plt_offset(local_sym_index);
+      }
+  }
+
   // Size of a given plt call stub.
   unsigned int
   plt_call_size(typename Plt_stub_entries::const_iterator p) const
@@ -2858,18 +3424,17 @@ class Stub_table : public Output_relaxed_input_section
     if (size == 32)
       return 16;
 
-    Address pltaddr = p->second;
-    if (p->first.sym_ == NULL 
-       || (p->first.sym_->type() == elfcpp::STT_GNU_IFUNC
-           && p->first.sym_->can_use_relative_reloc(false)))
-      pltaddr += this->targ_->iplt_section()->address();
+    bool is_iplt;
+    Address plt_addr = this->plt_off(p, &is_iplt);
+    if (is_iplt)
+      plt_addr += this->targ_->iplt_section()->address();
     else
-      pltaddr += this->targ_->plt_section()->address();
-    Address tocbase = this->targ_->got_section()->output_section()->address();
+      plt_addr += this->targ_->plt_section()->address();
+    Address got_addr = this->targ_->got_section()->output_section()->address();
     const Powerpc_relobj<size, big_endian>* ppcobj = static_cast
       <const Powerpc_relobj<size, big_endian>*>(p->first.object_);
-    tocbase += ppcobj->toc_base_offset();
-    Address off = pltaddr - tocbase;
+    got_addr += ppcobj->toc_base_offset();
+    Address off = plt_addr - got_addr;
     bool static_chain = parameters->options().plt_static_chain();
     bool thread_safe = this->targ_->plt_thread_safe();
     unsigned int bytes = (4 * 5
@@ -3010,6 +3575,8 @@ class Stub_table : public Output_relaxed_input_section
   section_size_type orig_data_size_;
   // size of stubs
   section_size_type plt_size_, last_plt_size_, branch_size_, last_branch_size_;
+  // Whether .eh_frame info has been created for this stub section.
+  bool eh_frame_added_;
 };
 
 // Make a new stub table, and record.
@@ -3082,7 +3649,7 @@ Stub_table<size, big_endian>::add_plt_call_entry(
 // Find a plt call stub.
 
 template<int size, bool big_endian>
-typename elfcpp::Elf_types<size>::Elf_Addr
+typename Stub_table<size, big_endian>::Address
 Stub_table<size, big_endian>::find_plt_call_entry(
     const Sized_relobj_file<size, big_endian>* object,
     const Symbol* gsym,
@@ -3095,7 +3662,7 @@ Stub_table<size, big_endian>::find_plt_call_entry(
 }
 
 template<int size, bool big_endian>
-typename elfcpp::Elf_types<size>::Elf_Addr
+typename Stub_table<size, big_endian>::Address
 Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
 {
   Plt_stub_ent ent(gsym);
@@ -3104,7 +3671,7 @@ Stub_table<size, big_endian>::find_plt_call_entry(const Symbol* gsym) const
 }
 
 template<int size, bool big_endian>
-typename elfcpp::Elf_types<size>::Elf_Addr
+typename Stub_table<size, big_endian>::Address
 Stub_table<size, big_endian>::find_plt_call_entry(
     const Sized_relobj_file<size, big_endian>* object,
     unsigned int locsym_index,
@@ -3117,7 +3684,7 @@ Stub_table<size, big_endian>::find_plt_call_entry(
 }
 
 template<int size, bool big_endian>
-typename elfcpp::Elf_types<size>::Elf_Addr
+typename Stub_table<size, big_endian>::Address
 Stub_table<size, big_endian>::find_plt_call_entry(
     const Sized_relobj_file<size, big_endian>* object,
     unsigned int locsym_index) const
@@ -3150,10 +3717,10 @@ Stub_table<size, big_endian>::add_long_branch_entry(
 // Find long branch stub.
 
 template<int size, bool big_endian>
-typename elfcpp::Elf_types<size>::Elf_Addr
+typename Stub_table<size, big_endian>::Address
 Stub_table<size, big_endian>::find_long_branch_entry(
     const Powerpc_relobj<size, big_endian>* object,
-    Address to)
+    Address to) const
 {
   Branch_stub_ent ent(object, to);
   typename Branch_stub_entries::const_iterator p
@@ -3173,6 +3740,37 @@ class Output_data_glink : public Output_section_data
     : Output_section_data(16), targ_(targ)
   { }
 
+  void
+  add_eh_frame(Layout* layout)
+  {
+    if (!parameters->options().ld_generated_unwind_info())
+      return;
+
+    if (size == 64)
+      layout->add_eh_frame_for_plt(this,
+                                  Eh_cie<64>::eh_frame_cie,
+                                  sizeof (Eh_cie<64>::eh_frame_cie),
+                                  glink_eh_frame_fde_64,
+                                  sizeof (glink_eh_frame_fde_64));
+    else
+      {
+       // 32-bit .glink can use the default since the CIE return
+       // address reg, LR, is valid.
+       layout->add_eh_frame_for_plt(this,
+                                    Eh_cie<32>::eh_frame_cie,
+                                    sizeof (Eh_cie<32>::eh_frame_cie),
+                                    default_fde,
+                                    sizeof (default_fde));
+       // Except where LR is used in a PIC __glink_PLTresolve.
+       if (parameters->options().output_is_position_independent())
+         layout->add_eh_frame_for_plt(this,
+                                      Eh_cie<32>::eh_frame_cie,
+                                      sizeof (Eh_cie<32>::eh_frame_cie),
+                                      glink_eh_frame_fde_32,
+                                      sizeof (glink_eh_frame_fde_32));
+      }
+  }
+
  protected:
   // Write to a map file.
   void
@@ -3257,25 +3855,10 @@ Stub_table<size, big_endian>::do_write(Output_file* of)
               cs != this->plt_call_stubs_.end();
               ++cs)
            {
-             Address pltoff;
-             bool is_ifunc;
-             const Symbol* gsym = cs->first.sym_;
-             if (gsym != NULL)
-               {
-                 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
-                             && gsym->can_use_relative_reloc(false));
-                 pltoff = gsym->plt_offset();
-               }
-             else
-               {
-                 is_ifunc = true;
-                 const Sized_relobj_file<size, big_endian>* relobj
-                   = cs->first.object_;
-                 unsigned int local_sym_index = cs->first.locsym_;
-                 pltoff = relobj->local_plt_offset(local_sym_index);
-               }
+             bool is_iplt;
+             Address pltoff = this->plt_off(cs, &is_iplt);
              Address plt_addr = pltoff;
-             if (is_ifunc)
+             if (is_iplt)
                {
                  if (iplt_base == invalid_address)
                    iplt_base = this->targ_->iplt_section()->address();
@@ -3342,7 +3925,7 @@ Stub_table<size, big_endian>::do_write(Output_file* of)
              else
                {
                  write_insn<big_endian>(p, std_2_1 + 40),              p += 4;
-                 write_insn<big_endian>(p, ld_11_2 + l(off)),  p += 4;
+                 write_insn<big_endian>(p, ld_11_2 + l(off)),          p += 4;
                  if (ha(off + 8 + 8 * static_chain) != ha(off))
                    {
                      write_insn<big_endian>(p, addi_2_2 + l(off)),     p += 4;
@@ -3418,24 +4001,9 @@ Stub_table<size, big_endian>::do_write(Output_file* of)
               cs != this->plt_call_stubs_.end();
               ++cs)
            {
-             Address plt_addr;
-             bool is_ifunc;
-             const Symbol* gsym = cs->first.sym_;
-             if (gsym != NULL)
-               {
-                 is_ifunc = (gsym->type() == elfcpp::STT_GNU_IFUNC
-                             && gsym->can_use_relative_reloc(false));
-                 plt_addr = gsym->plt_offset();
-               }
-             else
-               {
-                 is_ifunc = true;
-                 const Sized_relobj_file<size, big_endian>* relobj
-                   = cs->first.object_;
-                 unsigned int local_sym_index = cs->first.locsym_;
-                 plt_addr = relobj->local_plt_offset(local_sym_index);
-               }
-             if (is_ifunc)
+             bool is_iplt;
+             Address plt_addr = this->plt_off(cs, &is_iplt);
+             if (is_iplt)
                {
                  if (iplt_base == invalid_address)
                    iplt_base = this->targ_->iplt_section()->address();
@@ -4008,6 +4576,7 @@ Target_powerpc<size, big_endian>::make_glink_section(Layout* layout)
   if (this->glink_ == NULL)
     {
       this->glink_ = new Output_data_glink<size, big_endian>(this);
+      this->glink_->add_eh_frame(layout);
       layout->add_output_section_data(".text", elfcpp::SHT_PROGBITS,
                                      elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
                                      this->glink_, ORDER_TEXT, false);
@@ -4060,10 +4629,7 @@ Target_powerpc<size, big_endian>::plt_entry_count() const
 {
   if (this->plt_ == NULL)
     return 0;
-  unsigned int count = this->plt_->entry_count();
-  if (this->iplt_ != NULL)
-    count += this->iplt_->entry_count();
-  return count;
+  return this->plt_->entry_count();
 }
 
 // Return the offset of the first non-reserved PLT entry.
@@ -4307,7 +4873,8 @@ template<int size, bool big_endian>
 bool
 Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
      Sized_relobj_file<size, big_endian>* object,
-     unsigned int r_type)
+     unsigned int r_type,
+     bool report_err)
 {
   // In non-pic code any reference will resolve to the plt call stub
   // for the ifunc symbol.
@@ -4316,29 +4883,29 @@ Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
 
   switch (r_type)
     {
-    // Word size refs from data sections are OK.
+    // Word size refs from data sections are OK, but don't need a PLT entry.
     case elfcpp::R_POWERPC_ADDR32:
     case elfcpp::R_POWERPC_UADDR32:
       if (size == 32)
-       return true;
+       return false;
       break;
 
     case elfcpp::R_PPC64_ADDR64:
     case elfcpp::R_PPC64_UADDR64:
       if (size == 64)
-       return true;
+       return false;
       break;
 
-    // GOT refs are good.
+    // GOT refs are good, but also don't need a PLT entry.
     case elfcpp::R_POWERPC_GOT16:
     case elfcpp::R_POWERPC_GOT16_LO:
     case elfcpp::R_POWERPC_GOT16_HI:
     case elfcpp::R_POWERPC_GOT16_HA:
     case elfcpp::R_PPC64_GOT16_DS:
     case elfcpp::R_PPC64_GOT16_LO_DS:
-      return true;
+      return false;
 
-    // So are function calls.
+    // Function calls are good, and these do need a PLT entry.
     case elfcpp::R_POWERPC_ADDR24:
     case elfcpp::R_POWERPC_ADDR14:
     case elfcpp::R_POWERPC_ADDR14_BRTAKEN:
@@ -4363,7 +4930,8 @@ Target_powerpc<size, big_endian>::Scan::reloc_needs_plt_for_ifunc(
   // writable and non-executable to apply text relocations.  So we'll
   // segfault when trying to run the indirection function to resolve
   // the reloc.
-  gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
+  if (report_err)
+    gold_error(_("%s: unsupported reloc %u for IFUNC symbol"),
               object->name().c_str(), r_type);
   return false;
 }
@@ -4384,6 +4952,25 @@ Target_powerpc<size, big_endian>::Scan::local(
     const elfcpp::Sym<size, big_endian>& lsym,
     bool is_discarded)
 {
+  this->maybe_skip_tls_get_addr_call(r_type, NULL);
+
+  if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
+      || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
+    {
+      this->expect_tls_get_addr_call();
+      const tls::Tls_optimization tls_type = target->optimize_tls_gd(true);
+      if (tls_type != tls::TLSOPT_NONE)
+       this->skip_next_tls_get_addr_call();
+    }
+  else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
+          || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
+    {
+      this->expect_tls_get_addr_call();
+      const tls::Tls_optimization tls_type = target->optimize_tls_ld();
+      if (tls_type != tls::TLSOPT_NONE)
+       this->skip_next_tls_get_addr_call();
+    }
+
   Powerpc_relobj<size, big_endian>* ppc_object
     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
 
@@ -4398,7 +4985,7 @@ Target_powerpc<size, big_endian>::Scan::local(
 
   // A local STT_GNU_IFUNC symbol may require a PLT entry.
   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
-  if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
+  if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
     {
       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
@@ -4464,18 +5051,14 @@ Target_powerpc<size, big_endian>::Scan::local(
       if (parameters->options().output_is_position_independent()
          || (size == 64 && is_ifunc))
        {
-         Reloc_section* rela_dyn = target->rela_dyn_section(layout);
-
+         Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
+                                                            is_ifunc);
          if ((size == 32 && r_type == elfcpp::R_POWERPC_ADDR32)
              || (size == 64 && r_type == elfcpp::R_PPC64_ADDR64))
            {
              unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
-             unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
-             if (is_ifunc)
-               {
-                 rela_dyn = target->iplt_section()->rel_plt();
-                 dynrel = elfcpp::R_POWERPC_IRELATIVE;
-               }
+             unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
+                                    : elfcpp::R_POWERPC_RELATIVE);
              rela_dyn->add_local_relative(object, r_sym, dynrel,
                                           output_section, data_shndx,
                                           reloc.get_r_offset(),
@@ -4495,17 +5078,13 @@ Target_powerpc<size, big_endian>::Scan::local(
     case elfcpp::R_POWERPC_REL24:
     case elfcpp::R_PPC_PLTREL24:
     case elfcpp::R_PPC_LOCAL24PC:
-      target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
-                         r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
-                         reloc.get_r_addend());
-      break;
-
     case elfcpp::R_POWERPC_REL14:
     case elfcpp::R_POWERPC_REL14_BRTAKEN:
     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
-      target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
-                         r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
-                         reloc.get_r_addend());
+      if (!is_ifunc)
+       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
+                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
+                           reloc.get_r_addend());
       break;
 
     case elfcpp::R_PPC64_REL64:
@@ -4571,13 +5150,10 @@ Target_powerpc<size, big_endian>::Scan::local(
            off = got->add_constant(0);
            object->set_local_got_offset(r_sym, GOT_TYPE_STANDARD, off);
 
-           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
-           unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
-           if (is_ifunc)
-             {
-               rela_dyn = target->iplt_section()->rel_plt();
-               dynrel = elfcpp::R_POWERPC_IRELATIVE;
-             }
+           Reloc_section* rela_dyn = target->rela_dyn_section(symtab, layout,
+                                                              is_ifunc);
+           unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
+                                  : elfcpp::R_POWERPC_RELATIVE);
            rela_dyn->add_local_relative(object, r_sym, dynrel,
                                         got, off, 0, false);
          }
@@ -4688,6 +5264,21 @@ Target_powerpc<size, big_endian>::Scan::local(
       unsupported_reloc_local(object, r_type);
       break;
     }
+
+  switch (r_type)
+    {
+    case elfcpp::R_POWERPC_GOT_TLSLD16:
+    case elfcpp::R_POWERPC_GOT_TLSGD16:
+    case elfcpp::R_POWERPC_GOT_TPREL16:
+    case elfcpp::R_POWERPC_GOT_DTPREL16:
+    case elfcpp::R_POWERPC_GOT16:
+    case elfcpp::R_PPC64_GOT16_DS:
+    case elfcpp::R_PPC64_TOC16:
+    case elfcpp::R_PPC64_TOC16_DS:
+      ppc_object->set_has_small_toc_reloc();
+    default:
+      break;
+    }
 }
 
 // Report an unsupported relocation against a global symbol.
@@ -4718,12 +5309,33 @@ Target_powerpc<size, big_endian>::Scan::global(
     unsigned int r_type,
     Symbol* gsym)
 {
+  if (this->maybe_skip_tls_get_addr_call(r_type, gsym) == Track_tls::SKIP)
+    return;
+
+  if ((size == 64 && r_type == elfcpp::R_PPC64_TLSGD)
+      || (size == 32 && r_type == elfcpp::R_PPC_TLSGD))
+    {
+      this->expect_tls_get_addr_call();
+      const bool final = gsym->final_value_is_known();
+      const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
+      if (tls_type != tls::TLSOPT_NONE)
+       this->skip_next_tls_get_addr_call();
+    }
+  else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
+          || (size == 32 && r_type == elfcpp::R_PPC_TLSLD))
+    {
+      this->expect_tls_get_addr_call();
+      const tls::Tls_optimization tls_type = target->optimize_tls_ld();
+      if (tls_type != tls::TLSOPT_NONE)
+       this->skip_next_tls_get_addr_call();
+    }
+
   Powerpc_relobj<size, big_endian>* ppc_object
     = static_cast<Powerpc_relobj<size, big_endian>*>(object);
 
   // A STT_GNU_IFUNC symbol may require a PLT entry.
-  if (gsym->type() == elfcpp::STT_GNU_IFUNC
-      && this->reloc_needs_plt_for_ifunc(object, r_type))
+  bool is_ifunc = gsym->type() == elfcpp::STT_GNU_IFUNC;
+  if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type, true))
     {
       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
                          r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
@@ -4799,11 +5411,14 @@ Target_powerpc<size, big_endian>::Scan::global(
        // Make a PLT entry if necessary.
        if (gsym->needs_plt_entry())
          {
-           target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
-                               r_type,
-                               elfcpp::elf_r_sym<size>(reloc.get_r_info()),
-                               reloc.get_r_addend());
-           target->make_plt_entry(symtab, layout, gsym);
+           if (!is_ifunc)
+             {
+               target->push_branch(ppc_object, data_shndx,
+                                   reloc.get_r_offset(), r_type,
+                                   elfcpp::elf_r_sym<size>(reloc.get_r_info()),
+                                   reloc.get_r_addend());
+               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
@@ -4815,7 +5430,7 @@ Target_powerpc<size, big_endian>::Scan::global(
          }
        // Make a dynamic relocation if necessary.
        if (needs_dynamic_reloc<size>(gsym, Scan::get_reference_flags(r_type))
-           || (size == 64 && gsym->type() == elfcpp::STT_GNU_IFUNC))
+           || (size == 64 && is_ifunc))
          {
            if (gsym->may_need_copy_reloc())
              {
@@ -4832,20 +5447,18 @@ Target_powerpc<size, big_endian>::Scan::global(
                         && (gsym->can_use_relative_reloc(false)
                             || data_shndx == ppc_object->opd_shndx())))
              {
-               Reloc_section* rela_dyn = target->rela_dyn_section(layout);
-               unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
-               if (gsym->type() == elfcpp::STT_GNU_IFUNC)
-                 {
-                   rela_dyn = target->iplt_section()->rel_plt();
-                   dynrel = elfcpp::R_POWERPC_IRELATIVE;
-                 }
+               Reloc_section* rela_dyn
+                 = target->rela_dyn_section(symtab, layout, is_ifunc);
+               unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
+                                      : elfcpp::R_POWERPC_RELATIVE);
                rela_dyn->add_symbolless_global_addend(
                    gsym, dynrel, output_section, object, data_shndx,
                    reloc.get_r_offset(), reloc.get_r_addend());
              }
            else
              {
-               Reloc_section* rela_dyn = target->rela_dyn_section(layout);
+               Reloc_section* rela_dyn
+                 = target->rela_dyn_section(symtab, layout, is_ifunc);
                check_non_pic(object, r_type);
                rela_dyn->add_global(gsym, r_type, output_section,
                                     object, data_shndx,
@@ -4858,15 +5471,19 @@ Target_powerpc<size, big_endian>::Scan::global(
 
     case elfcpp::R_PPC_PLTREL24:
     case elfcpp::R_POWERPC_REL24:
-      target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
-                         r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
-                         reloc.get_r_addend());
-      if (gsym->needs_plt_entry()
-         || (!gsym->final_value_is_known()
-             && (gsym->is_undefined()
-                 || gsym->is_from_dynobj()
-                 || gsym->is_preemptible())))
-       target->make_plt_entry(symtab, layout, gsym);
+      if (!is_ifunc)
+       {
+         target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
+                             r_type,
+                             elfcpp::elf_r_sym<size>(reloc.get_r_info()),
+                             reloc.get_r_addend());
+         if (gsym->needs_plt_entry()
+             || (!gsym->final_value_is_known()
+                 && (gsym->is_undefined()
+                     || gsym->is_from_dynobj()
+                     || gsym->is_preemptible())))
+           target->make_plt_entry(symtab, layout, gsym);
+       }
       // Fall thru
 
     case elfcpp::R_PPC64_REL64:
@@ -4882,7 +5499,8 @@ Target_powerpc<size, big_endian>::Scan::global(
            }
          else
            {
-             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
+             Reloc_section* rela_dyn
+               = target->rela_dyn_section(symtab, layout, is_ifunc);
              check_non_pic(object, r_type);
              rela_dyn->add_global(gsym, r_type, output_section, object,
                                   data_shndx, reloc.get_r_offset(),
@@ -4894,9 +5512,10 @@ Target_powerpc<size, big_endian>::Scan::global(
     case elfcpp::R_POWERPC_REL14:
     case elfcpp::R_POWERPC_REL14_BRTAKEN:
     case elfcpp::R_POWERPC_REL14_BRNTAKEN:
-      target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
-                         r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
-                         reloc.get_r_addend());
+      if (!is_ifunc)
+       target->push_branch(ppc_object, data_shndx, reloc.get_r_offset(),
+                           r_type, elfcpp::elf_r_sym<size>(reloc.get_r_info()),
+                           reloc.get_r_addend());
       break;
 
     case elfcpp::R_POWERPC_REL16:
@@ -4946,7 +5565,7 @@ Target_powerpc<size, big_endian>::Scan::global(
        got = target->got_section(symtab, layout);
        if (gsym->final_value_is_known())
          {
-           if (size == 32 && gsym->type() == elfcpp::STT_GNU_IFUNC)
+           if (size == 32 && is_ifunc)
              got->add_global_plt(gsym, GOT_TYPE_STANDARD);
            else
              got->add_global(gsym, GOT_TYPE_STANDARD);
@@ -4958,18 +5577,16 @@ Target_powerpc<size, big_endian>::Scan::global(
            unsigned int off = got->add_constant(0);
            gsym->set_got_offset(GOT_TYPE_STANDARD, off);
 
-           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
+           Reloc_section* rela_dyn
+             = target->rela_dyn_section(symtab, layout, is_ifunc);
+
            if (gsym->can_use_relative_reloc(false)
                && !(size == 32
                     && gsym->visibility() == elfcpp::STV_PROTECTED
                     && parameters->options().shared()))
              {
-               unsigned int dynrel = elfcpp::R_POWERPC_RELATIVE;
-               if (gsym->type() == elfcpp::STT_GNU_IFUNC)
-                 {
-                   rela_dyn = target->iplt_section()->rel_plt();
-                   dynrel = elfcpp::R_POWERPC_IRELATIVE;
-                 }
+               unsigned int dynrel = (is_ifunc ? elfcpp::R_POWERPC_IRELATIVE
+                                      : elfcpp::R_POWERPC_RELATIVE);
                rela_dyn->add_global_relative(gsym, dynrel, got, off, 0, false);
              }
            else
@@ -5002,8 +5619,8 @@ Target_powerpc<size, big_endian>::Scan::global(
          {
            Output_data_got_powerpc<size, big_endian>* got
              = target->got_section(symtab, layout);
-           got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD,
-                                         target->rela_dyn_section(layout),
+           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
+           got->add_global_pair_with_rel(gsym, GOT_TYPE_TLSGD, rela_dyn,
                                          elfcpp::R_POWERPC_DTPMOD,
                                          elfcpp::R_POWERPC_DTPREL);
          }
@@ -5124,6 +5741,21 @@ Target_powerpc<size, big_endian>::Scan::global(
       unsupported_reloc_global(object, r_type, gsym);
       break;
     }
+
+  switch (r_type)
+    {
+    case elfcpp::R_POWERPC_GOT_TLSLD16:
+    case elfcpp::R_POWERPC_GOT_TLSGD16:
+    case elfcpp::R_POWERPC_GOT_TPREL16:
+    case elfcpp::R_POWERPC_GOT_DTPREL16:
+    case elfcpp::R_POWERPC_GOT16:
+    case elfcpp::R_PPC64_GOT16_DS:
+    case elfcpp::R_PPC64_TOC16:
+    case elfcpp::R_PPC64_TOC16_DS:
+      ppc_object->set_has_small_toc_reloc();
+    default:
+      break;
+    }
 }
 
 // Process relocations for gc.
@@ -5204,11 +5836,12 @@ Target_powerpc<size, big_endian>::do_gc_add_reference(
     unsigned int dst_shndx,
     Address dst_off) const
 {
+  if (size != 64 || dst_obj->is_dynamic())
+    return;
+
   Powerpc_relobj<size, big_endian>* ppc_object
     = static_cast<Powerpc_relobj<size, big_endian>*>(dst_obj);
-  if (size == 64
-      && !ppc_object->is_dynamic()
-      && dst_shndx == ppc_object->opd_shndx())
+  if (dst_shndx != 0 && dst_shndx == ppc_object->opd_shndx())
     {
       if (ppc_object->opd_valid())
        {
@@ -5240,7 +5873,7 @@ Target_powerpc<size, big_endian>::do_gc_mark_symbol(
        = static_cast<Powerpc_relobj<size, big_endian>*>(sym->object());
       bool is_ordinary;
       unsigned int shndx = sym->shndx(&is_ordinary);
-      if (is_ordinary && shndx == ppc_object->opd_shndx())
+      if (is_ordinary && shndx != 0 && shndx == ppc_object->opd_shndx())
        {
          Sized_symbol<size>* gsym = symtab->get_sized_symbol<size>(sym);
          Address dst_off = gsym->value();
@@ -5255,6 +5888,42 @@ Target_powerpc<size, big_endian>::do_gc_mark_symbol(
     }
 }
 
+// For a symbol location in .opd, set LOC to the location of the
+// function entry.
+
+template<int size, bool big_endian>
+void
+Target_powerpc<size, big_endian>::do_function_location(
+    Symbol_location* loc) const
+{
+  if (size == 64 && loc->shndx != 0)
+    {
+      if (loc->object->is_dynamic())
+       {
+         Powerpc_dynobj<size, big_endian>* ppc_object
+           = static_cast<Powerpc_dynobj<size, big_endian>*>(loc->object);
+         if (loc->shndx == ppc_object->opd_shndx())
+           {
+             Address dest_off;
+             Address off = loc->offset - ppc_object->opd_address();
+             loc->shndx = ppc_object->get_opd_ent(off, &dest_off);
+             loc->offset = dest_off;
+           }
+       }
+      else
+       {
+         const Powerpc_relobj<size, big_endian>* ppc_object
+           = static_cast<const Powerpc_relobj<size, big_endian>*>(loc->object);
+         if (loc->shndx == ppc_object->opd_shndx())
+           {
+             Address dest_off;
+             loc->shndx = ppc_object->get_opd_ent(loc->offset, &dest_off);
+             loc->offset = dest_off;
+           }
+       }
+    }
+}
+
 // Scan relocations for a section.
 
 template<int size, bool big_endian>
@@ -5314,10 +5983,12 @@ class Global_symbol_visitor_opd
        || !sym->in_real_elf())
       return;
 
+    if (sym->object()->is_dynamic())
+      return;
+
     Powerpc_relobj<64, big_endian>* symobj
       = static_cast<Powerpc_relobj<64, big_endian>*>(sym->object());
-    if (symobj->is_dynamic()
-       || symobj->opd_shndx() == 0)
+    if (symobj->opd_shndx() == 0)
       return;
 
     bool is_ordinary;
@@ -5344,6 +6015,31 @@ Target_powerpc<size, big_endian>::define_save_restore_funcs(
     }
 }
 
+// Sort linker created .got section first (for the header), then input
+// sections belonging to files using small model code.
+
+template<bool big_endian>
+class Sort_toc_sections
+{
+ public:
+  bool
+  operator()(const Output_section::Input_section& is1,
+            const Output_section::Input_section& is2) const
+  {
+    if (!is1.is_input_section() && is2.is_input_section())
+      return true;
+    bool small1
+      = (is1.is_input_section()
+        && (static_cast<const Powerpc_relobj<64, big_endian>*>(is1.relobj())
+            ->has_small_toc_reloc()));
+    bool small2
+      = (is2.is_input_section()
+        && (static_cast<const Powerpc_relobj<64, big_endian>*>(is2.relobj())
+            ->has_small_toc_reloc()));
+    return small1 && !small2;
+  }
+};
+
 // Finalize the sections.
 
 template<int size, bool big_endian>
@@ -5397,6 +6093,15 @@ Target_powerpc<size, big_endian>::do_finalize_sections(
          // need to mess with the relaxation machinery checkpointing.
          this->got_section(symtab, layout);
          this->make_brlt_section(layout);
+
+         if (parameters->options().toc_sort())
+           {
+             Output_section* os = this->got_->output_section();
+             if (os != NULL && os->input_sections().size() > 1)
+               std::stable_sort(os->input_sections().begin(),
+                                os->input_sections().end(),
+                                Sort_toc_sections<big_endian>());
+           }
        }
     }
 
@@ -5468,8 +6173,9 @@ ok_lo_toc_insn(uint32_t insn)
 // Return the value to use for a branch relocation.
 
 template<int size, bool big_endian>
-typename elfcpp::Elf_types<size>::Elf_Addr
+typename Target_powerpc<size, big_endian>::Address
 Target_powerpc<size, big_endian>::symval_for_branch(
+    const Symbol_table* symtab,
     Address value,
     const Sized_symbol<size>* gsym,
     Powerpc_relobj<size, big_endian>* object,
@@ -5491,12 +6197,20 @@ Target_powerpc<size, big_endian>::symval_for_branch(
   if (shndx == 0)
     return value;
   Address opd_addr = symobj->get_output_section_offset(shndx);
-  gold_assert(opd_addr != invalid_address);
+  if (opd_addr == invalid_address)
+    return value;
   opd_addr += symobj->output_section(shndx)->address();
   if (value >= opd_addr && value < opd_addr + symobj->section_size(shndx))
     {
       Address sec_off;
       *dest_shndx = symobj->get_opd_ent(value - opd_addr, &sec_off);
+      if (symtab->is_section_folded(symobj, *dest_shndx))
+       {
+         Section_id folded
+           = symtab->icf()->get_folded_section(symobj, *dest_shndx);
+         symobj = static_cast<Powerpc_relobj<size, big_endian>*>(folded.first);
+         *dest_shndx = folded.second;
+       }
       Address sec_addr = symobj->get_output_section_offset(*dest_shndx);
       gold_assert(sec_addr != invalid_address);
       sec_addr += symobj->output_section(*dest_shndx)->address();
@@ -5522,23 +6236,23 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
     Address address,
     section_size_type view_size)
 {
-  bool is_tls_call = ((r_type == elfcpp::R_POWERPC_REL24
-                      || r_type == elfcpp::R_PPC_PLTREL24)
-                     && gsym != NULL
-                     && strcmp(gsym->name(), "__tls_get_addr") == 0);
-  enum skip_tls last_tls = this->call_tls_get_addr_;
-  this->call_tls_get_addr_ = CALL_NOT_EXPECTED;
-  if (is_tls_call)
+  if (view == NULL)
+    return true;
+
+  switch (this->maybe_skip_tls_get_addr_call(r_type, gsym))
     {
-      if (last_tls == CALL_NOT_EXPECTED)
-       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
-                              _("__tls_get_addr call lacks marker reloc"));
-      else if (last_tls == CALL_SKIP)
-       return false;
+    case Track_tls::NOT_EXPECTED:
+      gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
+                            _("__tls_get_addr call lacks marker reloc"));
+      break;
+    case Track_tls::EXPECTED:
+      // We have already complained.
+      break;
+    case Track_tls::SKIP:
+      return true;
+    case Track_tls::NORMAL:
+      break;
     }
-  else if (last_tls != CALL_NOT_EXPECTED)
-    gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
-                          _("missing expected __tls_get_addr call"));
 
   typedef Powerpc_relocate_functions<size, big_endian> Reloc;
   typedef typename elfcpp::Swap<32, big_endian>::Valtype Insn;
@@ -5547,9 +6261,11 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
   Address value = 0;
   bool has_plt_value = false;
   unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
-  if (gsym != NULL
-      ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
-      : object->local_has_plt_offset(r_sym))
+  if ((gsym != NULL
+       ? use_plt_offset<size>(gsym, Scan::get_reference_flags(r_type))
+       : object->local_has_plt_offset(r_sym))
+      && (!psymval->is_ifunc_symbol()
+         || Scan::reloc_needs_plt_for_ifunc(object, r_type, false)))
     {
       Stub_table<size, big_endian>* stub_table
        = object->stub_table(relinfo->data_shndx);
@@ -5630,10 +6346,13 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
            }
          if (!can_plt_call)
            {
-             // This is not an error in one special case: A self
-             // call.  It isn't possible to cheaply verify we have
-             // such a call so just check for a call to the same
-             // section.
+             // g++ as of 20130507 emits self-calls without a
+             // following nop.  This is arguably wrong since we have
+             // conflicting information.  On the one hand a global
+             // symbol and on the other a local call sequence, but
+             // don't error for this special case.
+             // It isn't possible to cheaply verify we have exactly
+             // such a call.  Allow all calls to the same section.
              bool ok = false;
              Address code = value;
              if (gsym->source() == Symbol::FROM_OBJECT
@@ -5642,8 +6361,8 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
                  Address addend = rela.get_r_addend();
                  unsigned int dest_shndx;
                  Address opdent = psymval->value(object, addend);
-                 code = target->symval_for_branch(opdent, gsym, object,
-                                                  &dest_shndx);
+                 code = target->symval_for_branch(relinfo->symtab, opdent,
+                                                  gsym, object, &dest_shndx);
                  bool is_ordinary;
                  if (dest_shndx == 0)
                    dest_shndx = gsym->shndx(&is_ordinary);
@@ -5835,7 +6554,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
     {
       // Second instruction of a global dynamic sequence,
       // the __tls_get_addr call
-      this->call_tls_get_addr_ = CALL_EXPECTED;
+      this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
       const bool final = gsym == NULL || gsym->final_value_is_known();
       const tls::Tls_optimization tls_type = target->optimize_tls_gd(final);
       if (tls_type != tls::TLSOPT_NONE)
@@ -5858,7 +6577,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
              view += 2 * big_endian;
              value = psymval->value(object, rela.get_r_addend());
            }
-         this->call_tls_get_addr_ = CALL_SKIP;
+         this->skip_next_tls_get_addr_call();
        }
     }
   else if ((size == 64 && r_type == elfcpp::R_PPC64_TLSLD)
@@ -5866,14 +6585,14 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
     {
       // Second instruction of a local dynamic sequence,
       // the __tls_get_addr call
-      this->call_tls_get_addr_ = CALL_EXPECTED;
+      this->expect_tls_get_addr_call(relinfo, relnum, rela.get_r_offset());
       const tls::Tls_optimization tls_type = target->optimize_tls_ld();
       if (tls_type == tls::TLSOPT_TO_LE)
        {
          Insn* iview = reinterpret_cast<Insn*>(view);
          Insn insn = addi_3_3;
          elfcpp::Swap<32, big_endian>::writeval(iview, insn);
-         this->call_tls_get_addr_ = CALL_SKIP;
+         this->skip_next_tls_get_addr_call();
          r_type = elfcpp::R_POWERPC_TPREL16_LO;
          view += 2 * big_endian;
          value = dtp_offset;
@@ -5905,7 +6624,8 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
        addend = rela.get_r_addend();
       value = psymval->value(object, addend);
       if (size == 64 && is_branch_reloc(r_type))
-       value = target->symval_for_branch(value, gsym, object, &dest_shndx);
+       value = target->symval_for_branch(relinfo->symtab, value,
+                                         gsym, object, &dest_shndx);
       unsigned int max_branch_offset = 0;
       if (r_type == elfcpp::R_POWERPC_REL24
          || r_type == elfcpp::R_PPC_PLTREL24
@@ -5920,10 +6640,13 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
        {
          Stub_table<size, big_endian>* stub_table
            = object->stub_table(relinfo->data_shndx);
-         gold_assert(stub_table != NULL);
-         Address off = stub_table->find_long_branch_entry(object, value);
-         if (off != invalid_address)
-           value = stub_table->stub_address() + stub_table->plt_size() + off;
+         if (stub_table != NULL)
+           {
+             Address off = stub_table->find_long_branch_entry(object, value);
+             if (off != invalid_address)
+               value = (stub_table->stub_address() + stub_table->plt_size()
+                        + off);
+           }
        }
     }
 
@@ -6061,7 +6784,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
        case elfcpp::R_POWERPC_GOT_DTPREL16_HA:
        case elfcpp::R_POWERPC_GOT16_HA:
        case elfcpp::R_PPC64_TOC16_HA:
-         if (!parameters->options().no_toc_optimize())
+         if (parameters->options().toc_optimize())
            {
              Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
              Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
@@ -6086,7 +6809,7 @@ Target_powerpc<size, big_endian>::Relocate::relocate(
        case elfcpp::R_PPC64_GOT16_LO_DS:
        case elfcpp::R_PPC64_TOC16_LO:
        case elfcpp::R_PPC64_TOC16_LO_DS:
-         if (!parameters->options().no_toc_optimize())
+         if (parameters->options().toc_optimize())
            {
              Insn* iview = reinterpret_cast<Insn*>(view - 2 * big_endian);
              Insn insn = elfcpp::Swap<32, big_endian>::readval(iview);
@@ -6940,7 +7663,8 @@ class Target_selector_powerpc : public Target_selector
 {
 public:
   Target_selector_powerpc()
-    : Target_selector(elfcpp::EM_NONE, size, big_endian,
+    : Target_selector(size == 64 ? elfcpp::EM_PPC64 : elfcpp::EM_PPC,
+                     size, big_endian,
                      (size == 64
                       ? (big_endian ? "elf64-powerpc" : "elf64-powerpcle")
                       : (big_endian ? "elf32-powerpc" : "elf32-powerpcle")),
@@ -6949,28 +7673,6 @@ public:
                       : (big_endian ? "elf32ppc" : "elf32lppc")))
   { }
 
-  virtual Target*
-  do_recognize(Input_file*, off_t, int machine, int, int)
-  {
-    switch (size)
-      {
-      case 64:
-       if (machine != elfcpp::EM_PPC64)
-         return NULL;
-       break;
-
-      case 32:
-       if (machine != elfcpp::EM_PPC)
-         return NULL;
-       break;
-
-      default:
-       return NULL;
-      }
-
-    return this->instantiate_target();
-  }
-
   virtual Target*
   do_instantiate_target()
   { return new Target_powerpc<size, big_endian>(); }
This page took 0.051327 seconds and 4 git commands to generate.