* elf32-ppc.c (allocate_dynrelocs): Create dynsym for undef weak
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
index bc331c8be66dbcdfdea2ed438fcb797c7270b456..304a635856d43f32f15be941e30666886eebee83 100644 (file)
@@ -2,6 +2,7 @@
    Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
    Written by Linus Nordberg, Swox AB <info@swox.com>,
    based on elf32-ppc.c by Ian Lance Taylor.
+   Largely rewritten by Alan Modra <amodra@bigpond.net.au>
 
    This file is part of BFD, the Binary File Descriptor library.
 
@@ -64,11 +65,11 @@ static bfd_reloc_status_type ppc64_elf_unhandled_reloc
 #define elf_backend_plt_not_loaded 1
 #define elf_backend_got_symbol_offset 0
 #define elf_backend_got_header_size 8
-#define elf_backend_plt_header_size PLT_INITIAL_ENTRY_SIZE
 #define elf_backend_can_gc_sections 1
 #define elf_backend_can_refcount 1
 #define elf_backend_rela_normal 1
 
+#define bfd_elf64_mkobject                   ppc64_elf_mkobject
 #define bfd_elf64_bfd_reloc_type_lookup              ppc64_elf_reloc_type_lookup
 #define bfd_elf64_bfd_merge_private_bfd_data  ppc64_elf_merge_private_bfd_data
 #define bfd_elf64_new_section_hook           ppc64_elf_new_section_hook
@@ -76,6 +77,8 @@ static bfd_reloc_status_type ppc64_elf_unhandled_reloc
 #define bfd_elf64_bfd_link_hash_table_free    ppc64_elf_link_hash_table_free
 
 #define elf_backend_object_p                 ppc64_elf_object_p
+#define elf_backend_grok_prstatus            ppc64_elf_grok_prstatus
+#define elf_backend_grok_psinfo                      ppc64_elf_grok_psinfo
 #define elf_backend_create_dynamic_sections   ppc64_elf_create_dynamic_sections
 #define elf_backend_copy_indirect_symbol      ppc64_elf_copy_indirect_symbol
 #define elf_backend_check_relocs             ppc64_elf_check_relocs
@@ -89,6 +92,8 @@ static bfd_reloc_status_type ppc64_elf_unhandled_reloc
 #define elf_backend_finish_dynamic_symbol     ppc64_elf_finish_dynamic_symbol
 #define elf_backend_reloc_type_class         ppc64_elf_reloc_type_class
 #define elf_backend_finish_dynamic_sections   ppc64_elf_finish_dynamic_sections
+#define elf_backend_link_output_symbol_hook   ppc64_elf_output_symbol_hook
+#define elf_backend_special_sections         ppc64_elf_special_sections
 
 /* The name of the dynamic interpreter.  This is put in the .interp
    section.  */
@@ -2316,6 +2321,40 @@ ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
   return bfd_reloc_dangerous;
 }
 
+struct ppc64_elf_obj_tdata
+{
+  struct elf_obj_tdata elf;
+
+  /* Shortcuts to dynamic linker sections.  */
+  asection *got;
+  asection *relgot;
+
+  /* TLS local dynamic got entry handling.  Suppose for multiple GOT
+     sections means we potentially need one of these for each input bfd.  */
+  union {
+    bfd_signed_vma refcount;
+    bfd_vma offset;
+  } tlsld_got;
+};
+
+#define ppc64_elf_tdata(bfd) \
+  ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
+
+#define ppc64_tlsld_got(bfd) \
+  (&ppc64_elf_tdata (bfd)->tlsld_got)
+
+/* Override the generic function because we store some extras.  */
+
+static bfd_boolean
+ppc64_elf_mkobject (bfd *abfd)
+{
+  bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
+  abfd->tdata.any = bfd_zalloc (abfd, amt);
+  if (abfd->tdata.any == NULL)
+    return FALSE;
+  return TRUE;
+}
+
 /* Fix bad default arch selected for a 64 bit input bfd when the
    default is 32 bit.  */
 
@@ -2336,6 +2375,45 @@ ppc64_elf_object_p (bfd *abfd)
   return TRUE;
 }
 
+/* Support for core dump NOTE sections.  */
+
+static bfd_boolean
+ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
+{
+  size_t offset, raw_size;
+
+  if (note->descsz != 504)
+    return FALSE;
+
+  /* pr_cursig */
+  elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
+
+  /* pr_pid */
+  elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
+
+  /* pr_reg */
+  offset = 112;
+  raw_size = 384;
+
+  /* Make a ".reg/999" section.  */
+  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
+                                         raw_size, note->descpos + offset);
+}
+
+static bfd_boolean
+ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
+{
+  if (note->descsz != 136)
+    return FALSE;
+
+  elf_tdata (abfd)->core_program
+    = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
+  elf_tdata (abfd)->core_command
+    = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
+
+  return TRUE;
+}
+
 /* Merge backend specific data from an object file to the output
    object file when linking.  */
 
@@ -2365,6 +2443,19 @@ ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   return TRUE;
 }
 
+/* Add extra PPC sections.  */
+
+static struct bfd_elf_special_section const ppc64_elf_special_sections[]=
+{
+  { ".sdata",   6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
+  { ".sbss",    5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
+  { ".plt",     4,  0, SHT_NOBITS,   0 },
+  { ".toc",     4,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
+  { ".toc1",    5,  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
+  { ".tocbss",  7,  0, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
+  { NULL,       0,  0, 0,            0 }
+};
+
 struct _ppc64_elf_section_data
 {
   struct bfd_elf_section_data elf;
@@ -2497,16 +2588,24 @@ struct got_entry
   /* The symbol addend that we'll be placing in the GOT.  */
   bfd_vma addend;
 
+  /* Unlike other ELF targets, we use separate GOT entries for the same
+     symbol referenced from different input files.  This is to support
+     automatic multiple TOC/GOT sections, where the TOC base can vary
+     from one input file to another.
+
+     Point to the BFD owning this GOT entry.  */
+  bfd *owner;
+
+  /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
+     TLS_TPREL or TLS_DTPREL for tls entries.  */
+  char tls_type;
+
   /* Reference count until size_dynamic_sections, GOT offset thereafter.  */
   union
     {
       bfd_signed_vma refcount;
       bfd_vma offset;
     } got;
-
-  /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
-     TLS_TPREL or TLS_DTPREL for tls entries.  */
-  char tls_type;
 };
 
 /* The same for PLT.  */
@@ -2535,7 +2634,18 @@ struct plt_entry
 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
    copying dynamic variables from a shared lib into an app's dynbss
    section, and instead use a dynamic relocation to point into the
-   shared lib.  */
+   shared lib.  With code that gcc generates, it's vital that this be
+   enabled;  In the PowerPC64 ABI, the address of a function is actually
+   the address of a function descriptor, which resides in the .opd
+   section.  gcc uses the descriptor directly rather than going via the
+   GOT as some other ABI's do, which means that initialized function
+   pointers must reference the descriptor.  Thus, a function pointer
+   initialized to the address of a function in a shared library will
+   either require a copy reloc, or a dynamic reloc.  Using a copy reloc
+   redefines the function descriptor symbol to point to the copy.  This
+   presents a problem as a plt entry for that function is also
+   initialized from the function descriptor symbol and the copy reloc
+   may not be initialized first.  */
 #define ELIMINATE_COPY_RELOCS 1
 
 /* Section name for stubs is the associated section name plus this
@@ -2653,6 +2763,9 @@ struct ppc_link_hash_entry
   unsigned int is_func_descriptor:1;
   unsigned int is_entry:1;
 
+  /* Whether global opd sym has been adjusted or not.  */
+  unsigned int adjust_done:1;
+
   /* Contexts in which symbol is used in the GOT (or TOC).
      TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
      corresponding relocs are encountered during check_relocs.
@@ -2719,7 +2832,6 @@ struct ppc_link_hash_table
 
   /* Short-cuts to get to dynamic linker sections.  */
   asection *got;
-  asection *relgot;
   asection *plt;
   asection *relplt;
   asection *dynbss;
@@ -2729,18 +2841,9 @@ struct ppc_link_hash_table
   asection *brlt;
   asection *relbrlt;
 
-  /* Short-cut to first output tls section.  */
-  asection *tls_sec;
-
   /* Shortcut to .__tls_get_addr.  */
   struct elf_link_hash_entry *tls_get_addr;
 
-  /* TLS local dynamic got entry handling.  */
-  union {
-    bfd_signed_vma refcount;
-    bfd_vma offset;
-  } tlsld_got;
-
   /* Statistics.  */
   unsigned long stub_count[ppc_stub_plt_call];
 
@@ -2872,6 +2975,7 @@ link_hash_newfunc (struct bfd_hash_entry *entry,
       eh->is_func = 0;
       eh->is_func_descriptor = 0;
       eh->is_entry = 0;
+      eh->adjust_done = 0;
       eh->tls_mask = 0;
     }
 
@@ -2928,6 +3032,23 @@ ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
   _bfd_generic_link_hash_table_free (hash);
 }
 
+/* Satisfy the ELF linker by filling in some fields in our fake bfd.  */
+
+void
+ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
+{
+  struct ppc_link_hash_table *htab;
+
+  elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
+
+/* Always hook our dynamic sections into the first bfd, which is the
+   linker created stub bfd.  This ensures that the GOT header is at
+   the start of the output TOC section.  */
+  htab = ppc_hash_table (info);
+  htab->stub_bfd = abfd;
+  htab->elf.dynobj = abfd;
+}
+
 /* Build a name for an entry in the stub hash table.  */
 
 static char *
@@ -2950,7 +3071,7 @@ ppc_stub_name (const asection *input_section,
       stub_name = bfd_malloc (len);
       if (stub_name != NULL)
        {
-         sprintf (stub_name, "%08x_%s+%x",
+         sprintf (stub_name, "%08x.%s+%x",
                   input_section->id & 0xffffffff,
                   h->elf.root.root.string,
                   (int) rel->r_addend & 0xffffffff);
@@ -2962,7 +3083,7 @@ ppc_stub_name (const asection *input_section,
       stub_name = bfd_malloc (len);
       if (stub_name != NULL)
        {
-         sprintf (stub_name, "%08x_%x:%x+%x",
+         sprintf (stub_name, "%08x.%x:%x+%x",
                   input_section->id & 0xffffffff,
                   sym_sec->id & 0xffffffff,
                   (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
@@ -3122,30 +3243,43 @@ create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
   return TRUE;
 }
 
-/* Create .got and .rela.got sections in DYNOBJ, and set up
-   shortcuts to them in our hash table.  */
+/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
+   not already done.  */
 
 static bfd_boolean
-create_got_section (bfd *dynobj, struct bfd_link_info *info)
+create_got_section (bfd *abfd, struct bfd_link_info *info)
 {
-  struct ppc_link_hash_table *htab;
-
-  if (! _bfd_elf_create_got_section (dynobj, info))
-    return FALSE;
+  asection *got, *relgot;
+  flagword flags;
+  struct ppc_link_hash_table *htab = ppc_hash_table (info);
 
-  htab = ppc_hash_table (info);
-  htab->got = bfd_get_section_by_name (dynobj, ".got");
   if (!htab->got)
-    abort ();
+    {
+      if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
+       return FALSE;
+
+      htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
+      if (!htab->got)
+       abort ();
+    }
+
+  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
+          | SEC_LINKER_CREATED);
 
-  htab->relgot = bfd_make_section (dynobj, ".rela.got");
-  if (!htab->relgot
-      || ! bfd_set_section_flags (dynobj, htab->relgot,
-                                 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
-                                  | SEC_IN_MEMORY | SEC_LINKER_CREATED
-                                  | SEC_READONLY))
-      || ! bfd_set_section_alignment (dynobj, htab->relgot, 3))
+  got = bfd_make_section (abfd, ".got");
+  if (!got
+      || !bfd_set_section_flags (abfd, got, flags)
+      || !bfd_set_section_alignment (abfd, got, 3))
     return FALSE;
+
+  relgot = bfd_make_section (abfd, ".rela.got");
+  if (!relgot
+      || ! bfd_set_section_flags (abfd, relgot, flags | SEC_READONLY)
+      || ! bfd_set_section_alignment (abfd, relgot, 3))
+    return FALSE;
+
+  ppc64_elf_tdata (abfd)->got = got;
+  ppc64_elf_tdata (abfd)->relgot = relgot;
   return TRUE;
 }
 
@@ -3156,20 +3290,19 @@ ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
 {
   struct ppc_link_hash_table *htab;
 
-  htab = ppc_hash_table (info);
-  if (!htab->got && !create_got_section (dynobj, info))
-    return FALSE;
-
   if (!_bfd_elf_create_dynamic_sections (dynobj, info))
     return FALSE;
 
+  htab = ppc_hash_table (info);
+  if (!htab->got)
+    htab->got = bfd_get_section_by_name (dynobj, ".got");
   htab->plt = bfd_get_section_by_name (dynobj, ".plt");
   htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
   htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
   if (!info->shared)
     htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
 
-  if (!htab->plt || !htab->relplt || !htab->dynbss
+  if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
       || (!info->shared && !htab->relbss))
     abort ();
 
@@ -3179,9 +3312,10 @@ ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
 
 static void
-ppc64_elf_copy_indirect_symbol (struct elf_backend_data *bed ATTRIBUTE_UNUSED,
-                               struct elf_link_hash_entry *dir,
-                               struct elf_link_hash_entry *ind)
+ppc64_elf_copy_indirect_symbol
+  (const struct elf_backend_data *bed ATTRIBUTE_UNUSED,
+   struct elf_link_hash_entry *dir,
+   struct elf_link_hash_entry *ind)
 {
   struct ppc_link_hash_entry *edir, *eind;
   flagword mask;
@@ -3230,7 +3364,8 @@ ppc64_elf_copy_indirect_symbol (struct elf_backend_data *bed ATTRIBUTE_UNUSED,
   edir->tls_mask |= eind->tls_mask;
 
   mask = (ELF_LINK_HASH_REF_DYNAMIC | ELF_LINK_HASH_REF_REGULAR
-         | ELF_LINK_HASH_REF_REGULAR_NONWEAK | ELF_LINK_NON_GOT_REF);
+         | ELF_LINK_HASH_REF_REGULAR_NONWEAK | ELF_LINK_NON_GOT_REF
+         | ELF_LINK_HASH_NEEDS_PLT);
   /* If called to transfer flags for a weakdef during processing
      of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
      We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
@@ -3260,6 +3395,7 @@ ppc64_elf_copy_indirect_symbol (struct elf_backend_data *bed ATTRIBUTE_UNUSED,
 
              for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
                if (dent->addend == ent->addend
+                   && dent->owner == ent->owner
                    && dent->tls_type == ent->tls_type)
                  {
                    dent->got.refcount += ent->got.refcount;
@@ -3360,7 +3496,9 @@ update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
       struct got_entry *ent;
 
       for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
-       if (ent->addend == r_addend && ent->tls_type == tls_type)
+       if (ent->addend == r_addend
+           && ent->owner == abfd
+           && ent->tls_type == tls_type)
          break;
       if (ent == NULL)
        {
@@ -3370,6 +3508,7 @@ update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
            return FALSE;
          ent->next = local_got_ents[r_symndx];
          ent->addend = r_addend;
+         ent->owner = abfd;
          ent->tls_type = tls_type;
          ent->got.refcount = 0;
          local_got_ents[r_symndx] = ent;
@@ -3407,6 +3546,31 @@ update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
   return TRUE;
 }
 
+/* Find the function descriptor hash entry from the given function code
+   hash entry FH.  Link the entries via their OH fields.  */
+static struct ppc_link_hash_entry *
+get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
+{
+  struct ppc_link_hash_entry *fdh = (struct ppc_link_hash_entry *) fh->oh;
+
+  if (fdh == NULL)
+    {
+      const char *fd_name = fh->elf.root.root.string + 1;
+
+      fdh = (struct ppc_link_hash_entry *)
+       elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
+      if (fdh != NULL)
+       {
+         fdh->is_func_descriptor = 1;
+         fdh->oh = &fh->elf;
+         fh->is_func = 1;
+         fh->oh = &fdh->elf;
+       }
+    }
+
+  return fdh;
+}
+
 /* Look through the relocs for a section during the first phase, and
    calculate needed space in the global offset table, procedure
    linkage table, and dynamic reloc sections.  */
@@ -3426,6 +3590,15 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
   if (info->relocatable)
     return TRUE;
 
+  /* Don't do anything special with non-loaded, non-alloced sections.
+     In particular, any relocs in such sections should not affect GOT
+     and PLT reference counting (ie. we don't allow them to create GOT
+     or PLT entries), there's no possibility or desire to optimize TLS
+     relocs, and there's not much point in propagating relocs to shared
+     libs that the dynamic linker won't relocate.  */
+  if ((sec->flags & SEC_ALLOC) == 0)
+    return TRUE;
+
   htab = ppc_hash_table (info);
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
 
@@ -3460,8 +3633,6 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
       ppc64_elf_section_data (sec)->opd.func_sec = opd_sym_map;
     }
 
-  if (htab->elf.dynobj == NULL)
-    htab->elf.dynobj = abfd;
   if (htab->sfpr == NULL
       && !create_linkage_sections (htab->elf.dynobj, info))
     return FALSE;
@@ -3487,7 +3658,7 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
        case R_PPC64_GOT_TLSLD16_LO:
        case R_PPC64_GOT_TLSLD16_HI:
        case R_PPC64_GOT_TLSLD16_HA:
-         htab->tlsld_got.refcount += 1;
+         ppc64_tlsld_got (abfd)->refcount += 1;
          tls_type = TLS_TLS | TLS_LD;
          goto dogottls;
 
@@ -3524,8 +3695,8 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
        case R_PPC64_GOT16_LO_DS:
          /* This symbol requires a global offset table entry.  */
          sec->has_gp_reloc = 1;
-         if (htab->got == NULL
-             && !create_got_section (htab->elf.dynobj, info))
+         if (ppc64_elf_tdata (abfd)->got == NULL
+             && !create_got_section (abfd, info))
            return FALSE;
 
          if (h != NULL)
@@ -3536,6 +3707,7 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
              eh = (struct ppc_link_hash_entry *) h;
              for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
                if (ent->addend == rel->r_addend
+                   && ent->owner == abfd
                    && ent->tls_type == tls_type)
                  break;
              if (ent == NULL)
@@ -3546,6 +3718,7 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
                    return FALSE;
                  ent->next = eh->elf.got.glist;
                  ent->addend = rel->r_addend;
+                 ent->owner = abfd;
                  ent->tls_type = tls_type;
                  ent->got.refcount = 0;
                  eh->elf.got.glist = ent;
@@ -3735,19 +3908,8 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
              && h != NULL
              && h->root.root.string[0] == '.'
              && h->root.root.string[1] != 0)
-           {
-             struct elf_link_hash_entry *fdh;
+           get_fdh ((struct ppc_link_hash_entry *) h, htab);
 
-             fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
-                                         FALSE, FALSE, FALSE);
-             if (fdh != NULL)
-               {
-                 ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
-                 ((struct ppc_link_hash_entry *) fdh)->oh = h;
-                 ((struct ppc_link_hash_entry *) h)->is_func = 1;
-                 ((struct ppc_link_hash_entry *) h)->oh = fdh;
-               }
-           }
          if (opd_sym_map != NULL
              && h == NULL
              && rel + 1 < rel_end
@@ -3794,10 +3956,6 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
          if (NO_OPD_RELOCS && opd_sym_map != NULL)
            break;
 
-         /* Don't propagate relocs that the dynamic linker won't relocate.  */
-         if ((sec->flags & SEC_ALLOC) == 0)
-           break;
-
          /* If we are creating a shared library, and this is a reloc
             against a global symbol, or a non PC relative reloc
             against a local symbol, then we need to copy the reloc
@@ -4013,6 +4171,9 @@ ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
   struct got_entry **local_got_ents;
   const Elf_Internal_Rela *rel, *relend;
 
+  if ((sec->flags & SEC_ALLOC) == 0)
+    return TRUE;
+
   elf_section_data (sec)->local_dynrel = NULL;
 
   htab = ppc_hash_table (info);
@@ -4054,7 +4215,7 @@ ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
        case R_PPC64_GOT_TLSLD16_LO:
        case R_PPC64_GOT_TLSLD16_HI:
        case R_PPC64_GOT_TLSLD16_HA:
-         htab->tlsld_got.refcount -= 1;
+         ppc64_tlsld_got (abfd)->refcount -= 1;
          tls_type = TLS_TLS | TLS_LD;
          goto dogot;
 
@@ -4096,6 +4257,7 @@ ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
 
            for (; ent != NULL; ent = ent->next)
              if (ent->addend == rel->r_addend
+                 && ent->owner == abfd
                  && ent->tls_type == tls_type)
                break;
            if (ent == NULL)
@@ -4144,113 +4306,118 @@ func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
   struct bfd_link_info *info;
   struct ppc_link_hash_table *htab;
   struct plt_entry *ent;
+  struct ppc_link_hash_entry *fh;
+  struct ppc_link_hash_entry *fdh;
+  bfd_boolean force_local;
 
-  if (h->root.type == bfd_link_hash_indirect)
+  fh = (struct ppc_link_hash_entry *) h;
+  if (fh->elf.root.type == bfd_link_hash_indirect)
     return TRUE;
 
-  if (h->root.type == bfd_link_hash_warning)
-    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+  if (fh->elf.root.type == bfd_link_hash_warning)
+    fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
 
   info = inf;
   htab = ppc_hash_table (info);
 
   /* If this is a function code symbol, transfer dynamic linking
      information to the function descriptor symbol.  */
-  if (!((struct ppc_link_hash_entry *) h)->is_func)
+  if (!fh->is_func)
     return TRUE;
 
-  if (h->root.type == bfd_link_hash_undefweak
-      && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
+  if (fh->elf.root.type == bfd_link_hash_undefweak
+      && (fh->elf.elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR))
     htab->have_undefweak = TRUE;
 
-  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
+  for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
     if (ent->plt.refcount > 0)
       break;
-  if (ent != NULL
-      && h->root.root.string[0] == '.'
-      && h->root.root.string[1] != '\0')
-    {
-      struct elf_link_hash_entry *fdh = ((struct ppc_link_hash_entry *) h)->oh;
-      bfd_boolean force_local;
+  if (ent == NULL
+      || fh->elf.root.root.string[0] != '.'
+      || fh->elf.root.root.string[1] == '\0')
+    return TRUE;
 
-      /* Find the corresponding function descriptor symbol.  Create it
-        as undefined if necessary.  */
+  /* Find the corresponding function descriptor symbol.  Create it
+     as undefined if necessary.  */
 
-      if (fdh == NULL)
-       fdh = elf_link_hash_lookup (&htab->elf, h->root.root.string + 1,
-                                   FALSE, FALSE, TRUE);
+  fdh = get_fdh (fh, htab);
+  if (fdh != NULL)
+    while (fdh->elf.root.type == bfd_link_hash_indirect
+          || fdh->elf.root.type == bfd_link_hash_warning)
+      fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
 
-      if (fdh == NULL
-         && info->shared
-         && (h->root.type == bfd_link_hash_undefined
-             || h->root.type == bfd_link_hash_undefweak))
+  if (fdh == NULL
+      && info->shared
+      && (fh->elf.root.type == bfd_link_hash_undefined
+         || fh->elf.root.type == bfd_link_hash_undefweak))
+    {
+      bfd *abfd;
+      asymbol *newsym;
+      struct bfd_link_hash_entry *bh;
+
+      abfd = fh->elf.root.u.undef.abfd;
+      newsym = bfd_make_empty_symbol (abfd);
+      newsym->name = fh->elf.root.root.string + 1;
+      newsym->section = bfd_und_section_ptr;
+      newsym->value = 0;
+      newsym->flags = BSF_OBJECT;
+      if (fh->elf.root.type == bfd_link_hash_undefweak)
+       newsym->flags |= BSF_WEAK;
+
+      bh = &fdh->elf.root;
+      if ( !(_bfd_generic_link_add_one_symbol
+            (info, abfd, newsym->name, newsym->flags,
+             newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
        {
-         bfd *abfd;
-         asymbol *newsym;
-         struct bfd_link_hash_entry *bh;
-
-         abfd = h->root.u.undef.abfd;
-         newsym = bfd_make_empty_symbol (abfd);
-         newsym->name = h->root.root.string + 1;
-         newsym->section = bfd_und_section_ptr;
-         newsym->value = 0;
-         newsym->flags = BSF_OBJECT;
-         if (h->root.type == bfd_link_hash_undefweak)
-           newsym->flags |= BSF_WEAK;
-
-         bh = &fdh->root;
-         if ( !(_bfd_generic_link_add_one_symbol
-                (info, abfd, newsym->name, newsym->flags,
-                 newsym->section, newsym->value, NULL, FALSE, FALSE, &bh)))
-           {
-             return FALSE;
-           }
-         fdh = (struct elf_link_hash_entry *) bh;
-         fdh->elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
+         return FALSE;
        }
+      fdh = (struct ppc_link_hash_entry *) bh;
+      fdh->elf.elf_link_hash_flags &= ~ELF_LINK_NON_ELF;
+      fdh->elf.size = 24;
+      fdh->elf.type = STT_OBJECT;
+    }
 
-      if (fdh != NULL
-         && (fdh->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
-         && (info->shared
-             || (fdh->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
-             || (fdh->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0))
+  if (fdh != NULL
+      && (fdh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0
+      && (info->shared
+         || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
+         || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) != 0
+         || (fdh->elf.root.type == bfd_link_hash_undefweak
+             && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
+    {
+      if (fdh->elf.dynindx == -1)
+       if (! bfd_elf64_link_record_dynamic_symbol (info, &fdh->elf))
+         return FALSE;
+      fdh->elf.elf_link_hash_flags
+       |= (fh->elf.elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
+                                     | ELF_LINK_HASH_REF_DYNAMIC
+                                     | ELF_LINK_HASH_REF_REGULAR_NONWEAK
+                                     | ELF_LINK_NON_GOT_REF));
+      if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
        {
-         if (fdh->dynindx == -1)
-           if (! bfd_elf64_link_record_dynamic_symbol (info, fdh))
-             return FALSE;
-         fdh->elf_link_hash_flags |= (h->elf_link_hash_flags
-                                      & (ELF_LINK_HASH_REF_REGULAR
-                                         | ELF_LINK_HASH_REF_DYNAMIC
-                                         | ELF_LINK_HASH_REF_REGULAR_NONWEAK
-                                         | ELF_LINK_NON_GOT_REF));
-         if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
-           {
-             fdh->plt.plist = h->plt.plist;
-             fdh->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
-           }
-         ((struct ppc_link_hash_entry *) fdh)->is_func_descriptor = 1;
-         ((struct ppc_link_hash_entry *) fdh)->oh = h;
-         ((struct ppc_link_hash_entry *) h)->oh = fdh;
+         fdh->elf.plt.plist = fh->elf.plt.plist;
+         fdh->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
        }
-
-      /* Now that the info is on the function descriptor, clear the
-        function code sym info.  Any function code syms for which we
-        don't have a definition in a regular file, we force local.
-        This prevents a shared library from exporting syms that have
-        been imported from another library.  Function code syms that
-        are really in the library we must leave global to prevent the
-        linker dragging in a definition from a static library.  */
-      force_local = (info->shared
-                    && ((h->elf_link_hash_flags
-                         & ELF_LINK_HASH_DEF_REGULAR) == 0
-                        || fdh == NULL
-                        || (fdh->elf_link_hash_flags
-                            & ELF_LINK_HASH_DEF_REGULAR) == 0
-                        || (fdh->elf_link_hash_flags
-                            & ELF_LINK_FORCED_LOCAL) != 0));
-      _bfd_elf_link_hash_hide_symbol (info, h, force_local);
+      fdh->is_func_descriptor = 1;
+      fdh->oh = &fh->elf;
+      fh->oh = &fdh->elf;
     }
 
+  /* Now that the info is on the function descriptor, clear the
+     function code sym info.  Any function code syms for which we
+     don't have a definition in a regular file, we force local.
+     This prevents a shared library from exporting syms that have
+     been imported from another library.  Function code syms that
+     are really in the library we must leave global to prevent the
+     linker dragging in a definition from a static library.  */
+  force_local
+    = (info->shared
+       && ((fh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
+          || fdh == NULL
+          || (fdh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0
+          || (fdh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0));
+  _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
+
   return TRUE;
 }
 
@@ -4287,7 +4454,7 @@ ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
       sym[8] = i % 10 + '0';
       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
       if (h != NULL
-         && h->root.type == bfd_link_hash_undefined)
+         && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
        {
          if (lowest_savef > i)
            lowest_savef = i;
@@ -4296,7 +4463,7 @@ ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
          h->root.u.def.value = (i - lowest_savef) * 4;
          h->type = STT_FUNC;
          h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
-         _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
+         _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
        }
     }
 
@@ -4307,7 +4474,7 @@ ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
       sym[8] = i % 10 + '0';
       h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
       if (h != NULL
-         && h->root.type == bfd_link_hash_undefined)
+         && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
        {
          if (lowest_restf > i)
            lowest_restf = i;
@@ -4317,7 +4484,7 @@ ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
                                 + (i - lowest_restf) * 4);
          h->type = STT_FUNC;
          h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
-         _bfd_elf_link_hash_hide_symbol (info, h, info->shared);
+         _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
        }
     }
 
@@ -4426,9 +4593,6 @@ ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
       return TRUE;
     }
 
-  /* This is a reference to a symbol defined by a dynamic object which
-     is not a function.  */
-
   /* If we are creating a shared library, we must presume that the
      only references to the symbol are via the global offset table.
      For such cases we need not do anything here; the relocations will
@@ -4463,6 +4627,22 @@ ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
        }
     }
 
+  if (h->plt.plist != NULL)
+    {
+      /* We should never get here, but unfortunately there are versions
+        of gcc out there that improperly (for this ABI) put initialized
+        function pointers, vtable refs and suchlike in read-only
+        sections.  Allow them to proceed, but warn that this might
+        break at runtime.  */
+      (*_bfd_error_handler)
+       (_("copy reloc against `%s' requires lazy plt linking; "
+          "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
+        h->root.root.string);
+    }
+
+  /* This is a reference to a symbol defined by a dynamic object which
+     is not a function.  */
+
   /* We must allocate the symbol in our .dynbss section, which will
      become part of the .bss section of the executable.  There will be
      an entry for this symbol in the .dynsym section.  The dynamic
@@ -4663,11 +4843,12 @@ get_sym_h (struct elf_link_hash_entry **hp, Elf_Internal_Sym **symp,
    type suitable for optimization, and 1 otherwise.  */
 
 static int
-get_tls_mask (char **tls_maskp, Elf_Internal_Sym **locsymsp,
+get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
+             Elf_Internal_Sym **locsymsp,
              const Elf_Internal_Rela *rel, bfd *ibfd)
 {
   unsigned long r_symndx;
-  unsigned int next_r;
+  int next_r;
   struct elf_link_hash_entry *h;
   Elf_Internal_Sym *sym;
   asection *sec;
@@ -4696,24 +4877,64 @@ get_tls_mask (char **tls_maskp, Elf_Internal_Sym **locsymsp,
   next_r = ppc64_elf_section_data (sec)->t_symndx[off / 8 + 1];
   if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
     return 0;
-  if (h == NULL
-      || h->root.type == bfd_link_hash_defined
-      || h->root.type == bfd_link_hash_defweak)
+  if (toc_symndx != NULL)
+    *toc_symndx = r_symndx;
+  if ((h == NULL
+       || ((h->root.type == bfd_link_hash_defined
+           || h->root.type == bfd_link_hash_defweak)
+          && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
+      && (next_r == -1 || next_r == -2))
+    return 1 - next_r;
+  return 1;
+}
+
+/* Adjust all global syms defined in opd sections.  In gcc generated
+   code these will already have been done, but I suppose we have to
+   cater for all sorts of hand written assembly.  */
+
+static bfd_boolean
+adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
+{
+  struct ppc_link_hash_entry *eh;
+  asection *sym_sec;
+  long *opd_adjust;
+
+  if (h->root.type == bfd_link_hash_indirect)
+    return TRUE;
+
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+  if (h->root.type != bfd_link_hash_defined
+      && h->root.type != bfd_link_hash_defweak)
+    return TRUE;
+
+  eh = (struct ppc_link_hash_entry *) h;
+  if (eh->adjust_done)
+    return TRUE;
+
+  sym_sec = eh->elf.root.u.def.section;
+  if (sym_sec != NULL
+      && elf_section_data (sym_sec) != NULL
+      && (opd_adjust = ppc64_elf_section_data (sym_sec)->opd.adjust) != NULL)
     {
-      if (next_r == (unsigned) -1)
-       return 2;
-      if (next_r == (unsigned) -2
-         && (h == NULL
-             || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
-       return 3;
+      eh->elf.root.u.def.value += opd_adjust[eh->elf.root.u.def.value / 24];
+      eh->adjust_done = 1;
     }
-  return 1;
+  return TRUE;
 }
 
+/* Remove unused Official Procedure Descriptor entries.  Currently we
+   only remove those associated with functions in discarded link-once
+   sections, or weakly defined functions that have been overridden.  It
+   would be possible to remove many more entries for statically linked
+   applications.  */
+
 bfd_boolean
 ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
 {
   bfd *ibfd;
+  bfd_boolean some_edited = FALSE;
 
   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
     {
@@ -4764,7 +4985,7 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
       need_edit = FALSE;
       offset = 0;
       relend = relstart + sec->reloc_count;
-      for (rel = relstart; rel < relend; rel++)
+      for (rel = relstart; rel < relend; )
        {
          enum elf_ppc64_reloc_type r_type;
          unsigned long r_symndx;
@@ -4775,26 +4996,9 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
          /* .opd contains a regular array of 24 byte entries.  We're
             only interested in the reloc pointing to a function entry
             point.  */
-         r_type = ELF64_R_TYPE (rel->r_info);
-         if (r_type == R_PPC64_TOC)
-           continue;
-
-         if (r_type != R_PPC64_ADDR64)
-           {
-             (*_bfd_error_handler)
-               (_("%s: unexpected reloc type %u in .opd section"),
-                bfd_archive_filename (ibfd), r_type);
-             need_edit = FALSE;
-             break;
-           }
-
-         if (rel + 1 >= relend)
-           continue;
-         r_type = ELF64_R_TYPE ((rel + 1)->r_info);
-         if (r_type != R_PPC64_TOC)
-           continue;
-
-         if (rel->r_offset != offset)
+         if (rel->r_offset != offset
+             || rel + 1 >= relend
+             || (rel + 1)->r_offset != offset + 8)
            {
              /* If someone messes with .opd alignment then after a
                 "ld -r" we might have padding in the middle of .opd.
@@ -4808,10 +5012,20 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
              break;
            }
 
+         if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
+             || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
+           {
+             (*_bfd_error_handler)
+               (_("%s: unexpected reloc type %u in .opd section"),
+                bfd_archive_filename (ibfd), r_type);
+             need_edit = FALSE;
+             break;
+           }
+
          r_symndx = ELF64_R_SYM (rel->r_info);
          if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
                          r_symndx, ibfd))
-           goto error_free_rel;
+           goto error_ret;
 
          if (sym_sec == NULL || sym_sec->owner == NULL)
            {
@@ -4841,6 +5055,11 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
            need_edit = TRUE;
 
          offset += 24;
+         rel += 2;
+         /* Allow for the possibility of a reloc on the third word.  */
+         if (rel < relend
+             && rel->r_offset == offset - 8)
+           rel += 1;
        }
 
       if (need_edit)
@@ -4860,10 +5079,10 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
                  || !bfd_get_section_contents (ibfd, sec, loc, 0,
                                                sec->_raw_size))
                {
+               error_ret:
                  if (local_syms != NULL
                      && symtab_hdr->contents != (unsigned char *) local_syms)
                    free (local_syms);
-               error_free_rel:
                  if (elf_section_data (sec)->relocs != relstart)
                    free (relstart);
                  return FALSE;
@@ -4881,16 +5100,22 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
          offset = 0;
          for (rel = relstart; rel < relend; rel++)
            {
+             unsigned long r_symndx;
+             asection *sym_sec;
+             struct elf_link_hash_entry *h;
+             Elf_Internal_Sym *sym;
+
+             r_symndx = ELF64_R_SYM (rel->r_info);
+             if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
+                             r_symndx, ibfd))
+               goto error_ret;
+
              if (rel->r_offset == offset)
                {
-                 unsigned long r_symndx;
-                 asection *sym_sec;
-                 struct elf_link_hash_entry *h;
-                 Elf_Internal_Sym *sym;
-
-                 r_symndx = ELF64_R_SYM (rel->r_info);
-                 get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
-                            r_symndx, ibfd);
+                 struct ppc_link_hash_entry *fdh = NULL;
+                 if (h != NULL)
+                   fdh = get_fdh ((struct ppc_link_hash_entry *) h,
+                                  ppc_hash_table (info));
 
                  skip = (sym_sec->owner != ibfd
                          || sym_sec->output_section == bfd_abs_section_ptr);
@@ -4900,27 +5125,6 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
                        {
                          /* Arrange for the function descriptor sym
                             to be dropped.  */
-                         struct ppc_link_hash_entry *fdh;
-                         struct ppc_link_hash_entry *fh;
-
-                         fh = (struct ppc_link_hash_entry *) h;
-                         fdh = (struct ppc_link_hash_entry *) fh->oh;
-                         if (fdh == NULL)
-                           {
-                             const char *fd_name;
-                             struct ppc_link_hash_table *htab;
-
-                             fd_name = h->root.root.string + 1;
-                             htab = ppc_hash_table (info);
-                             fdh = (struct ppc_link_hash_entry *)
-                               elf_link_hash_lookup (&htab->elf, fd_name,
-                                                     FALSE, FALSE, FALSE);
-                             fdh->is_func_descriptor = 1;
-                             fdh->oh = &fh->elf;
-                             fh->is_func = 1;
-                             fh->oh = &fdh->elf;
-                           }
-
                          fdh->elf.root.u.def.value = 0;
                          fdh->elf.root.u.def.section = sym_sec;
                        }
@@ -4931,43 +5135,24 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
 
                      if (h != NULL)
                        {
-                         /* Redefine the function descriptor symbol
-                            to this location in the opd section.
-                            We've checked above that opd relocs are
-                            ordered.  */
-                         struct ppc_link_hash_entry *fdh;
-                         struct ppc_link_hash_entry *fh;
-
-                         fh = (struct ppc_link_hash_entry *) h;
-                         fdh = (struct ppc_link_hash_entry *) fh->oh;
-                         if (fdh == NULL)
-                           {
-                             const char *fd_name;
-                             struct ppc_link_hash_table *htab;
-
-                             fd_name = h->root.root.string + 1;
-                             htab = ppc_hash_table (info);
-                             fdh = (struct ppc_link_hash_entry *)
-                               elf_link_hash_lookup (&htab->elf, fd_name,
-                                                     FALSE, FALSE, FALSE);
-                             fdh->is_func_descriptor = 1;
-                             fdh->oh = &fh->elf;
-                             fh->is_func = 1;
-                             fh->oh = &fdh->elf;
-                           }
-
+                         /* Redefine the function descriptor symbol to
+                            this location in the opd section.  It is
+                            necessary to update the value here rather
+                            than using an array of adjustments as we do
+                            for local symbols, because various places
+                            in the generic ELF code use the value
+                            stored in u.def.value.  */
                          fdh->elf.root.u.def.value = wptr - sec->contents;
+                         fdh->adjust_done = 1;
                        }
-                     else
-                       {
-                         /* Local syms are a bit tricky.  We could
-                            tweak them as they can be cached, but
-                            we'd need to look through the local syms
-                            for the function descriptor sym which we
-                            don't have at the moment.  So keep an
-                            array of adjustments.  */
-                         adjust[rel->r_offset / 24] = wptr - rptr;
-                       }
+
+                     /* Local syms are a bit tricky.  We could
+                        tweak them as they can be cached, but
+                        we'd need to look through the local syms
+                        for the function descriptor sym which we
+                        don't have at the moment.  So keep an
+                        array of adjustments.  */
+                     adjust[rel->r_offset / 24] = wptr - rptr;
 
                      if (wptr != rptr)
                        memcpy (wptr, rptr, 24);
@@ -4977,11 +5162,41 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
                  offset += 24;
                }
 
-             /* We need to adjust any reloc offsets to point to the
-                new opd entries.  While we're at it, we may as well
-                remove redundant relocs.  */
-             if (!skip)
+             if (skip)
+               {
+                 BFD_ASSERT (MUST_BE_DYN_RELOC (ELF64_R_TYPE (rel->r_info)));
+                 if (info->shared)
+                   {
+                     /* We won't be needing dynamic relocs here.  */
+                     struct ppc_dyn_relocs **pp;
+                     struct ppc_dyn_relocs *p;
+
+                     if (h != NULL)
+                       pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
+                     else if (sym_sec != NULL)
+                       pp = ((struct ppc_dyn_relocs **)
+                             &elf_section_data (sym_sec)->local_dynrel);
+                     else
+                       pp = ((struct ppc_dyn_relocs **)
+                             &elf_section_data (sec)->local_dynrel);
+                     while ((p = *pp) != NULL)
+                       {
+                         if (p->sec == sec)
+                           {
+                             p->count -= 1;
+                             if (p->count == 0)
+                               *pp = p->next;
+                             break;
+                           }
+                         pp = &p->next;
+                       }
+                   }
+               }
+             else
                {
+                 /* We need to adjust any reloc offsets to point to the
+                    new opd entries.  While we're at it, we may as well
+                    remove redundant relocs.  */
                  rel->r_offset += wptr - rptr;
                  if (write_rel != rel)
                    memcpy (write_rel, rel, sizeof (*rel));
@@ -4996,6 +5211,7 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
          elf_section_data (sec)->rel_hdr.sh_size
            = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
          BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
+         some_edited = TRUE;
        }
       else if (elf_section_data (sec)->relocs != relstart)
        free (relstart);
@@ -5010,25 +5226,20 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info)
        }
     }
 
+  if (some_edited)
+    elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
+
   return TRUE;
 }
 
-/* Set htab->tls_sec.  */
+/* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
 
-bfd_boolean
+asection *
 ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
 {
-  asection *tls;
   struct ppc_link_hash_table *htab;
 
-  for (tls = obfd->sections; tls != NULL; tls = tls->next)
-    if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
-       == (SEC_THREAD_LOCAL | SEC_LOAD))
-      break;
-
   htab = ppc_hash_table (info);
-  htab->tls_sec = tls;
-
   if (htab->tls_get_addr != NULL)
     {
       struct elf_link_hash_entry *h = htab->tls_get_addr;
@@ -5040,7 +5251,7 @@ ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
       htab->tls_get_addr = h;
     }
 
-  return tls != NULL;
+  return _bfd_elf_tls_setup (obfd, info);
 }
 
 /* Run through all the TLS relocs looking for optimization
@@ -5113,7 +5324,17 @@ ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
                    value = h->root.u.def.value;
                  }
                else
-                 value = sym->st_value;
+                 {
+                   value = sym->st_value;
+
+                   if (elf_section_data (sym_sec) != NULL)
+                     {
+                       long *adjust;
+                       adjust = ppc64_elf_section_data (sym_sec)->opd.adjust;
+                       if (adjust != NULL)
+                         value += adjust[value / 24];
+                     }
+                 }
 
                ok_tprel = FALSE;
                is_local = FALSE;
@@ -5123,7 +5344,7 @@ ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
                    is_local = TRUE;
                    value += sym_sec->output_offset;
                    value += sym_sec->output_section->vma;
-                   value -= htab->tls_sec->vma;
+                   value -= htab->elf.tls_sec->vma;
                    ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
                                < (bfd_vma) 1 << 32);
                  }
@@ -5138,7 +5359,7 @@ ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
                    /* These relocs should never be against a symbol
                       defined in a shared lib.  Leave them alone if
                       that turns out to be the case.  */
-                   htab->tlsld_got.refcount -= 1;
+                   ppc64_tlsld_got (ibfd)->refcount -= 1;
                    if (!is_local)
                      continue;
 
@@ -5198,7 +5419,7 @@ ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
                            char *toc_tls;
                            int retval;
 
-                           retval = get_tls_mask (&toc_tls, &locsyms,
+                           retval = get_tls_mask (&toc_tls, NULL, &locsyms,
                                                   rel - 1, ibfd);
                            if (retval == 0)
                              goto err_free_rel;
@@ -5276,6 +5497,7 @@ ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
 
                    for (; ent != NULL; ent = ent->next)
                      if (ent->addend == rel->r_addend
+                         && ent->owner == ibfd
                          && ent->tls_type == tls_type)
                        break;
                    if (ent == NULL)
@@ -5431,7 +5653,8 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
          for (ent = h->got.glist; ent != NULL; ent = ent->next)
            if (ent->got.refcount > 0
                && (ent->tls_type & TLS_TPREL) != 0
-               && ent->addend == gent->addend)
+               && ent->addend == gent->addend
+               && ent->owner == gent->owner)
              {
                gent->got.refcount = 0;
                break;
@@ -5460,11 +5683,11 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
        if ((gent->tls_type & TLS_LD) != 0
            && !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
          {
-           gent->got.offset = htab->tlsld_got.offset;
+           gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
            continue;
          }
 
-       s = htab->got;
+       s = ppc64_elf_tdata (gent->owner)->got;
        gent->got.offset = s->_raw_size;
        s->_raw_size
          += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
@@ -5473,7 +5696,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
             || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
            && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
                || h->root.type != bfd_link_hash_undefweak))
-         htab->relgot->_raw_size
+         ppc64_elf_tdata (gent->owner)->relgot->_raw_size
            += (gent->tls_type & eh->tls_mask & TLS_GD
                ? 2 * sizeof (Elf64_External_Rela)
                : sizeof (Elf64_External_Rela));
@@ -5618,16 +5841,6 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
        }
     }
 
-  if (htab->tlsld_got.refcount > 0)
-    {
-      htab->tlsld_got.offset = htab->got->_raw_size;
-      htab->got->_raw_size += 16;
-      if (info->shared)
-       htab->relgot->_raw_size += sizeof (Elf64_External_Rela);
-    }
-  else
-    htab->tlsld_got.offset = (bfd_vma) -1;
-
   /* Set up .got offsets for local syms, and space for local dynamic
      relocs.  */
   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
@@ -5642,6 +5855,20 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
        continue;
 
+      if (ppc64_tlsld_got (ibfd)->refcount > 0)
+       {
+         s = ppc64_elf_tdata (ibfd)->got;
+         ppc64_tlsld_got (ibfd)->offset = s->_raw_size;
+         s->_raw_size += 16;
+         if (info->shared)
+           {
+             srel = ppc64_elf_tdata (ibfd)->relgot;
+             srel->_raw_size += sizeof (Elf64_External_Rela);
+           }
+       }
+      else
+       ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
+
       for (s = ibfd->sections; s != NULL; s = s->next)
        {
          struct ppc_dyn_relocs *p;
@@ -5677,8 +5904,8 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       locsymcount = symtab_hdr->sh_info;
       end_lgot_ents = lgot_ents + locsymcount;
       lgot_masks = (char *) end_lgot_ents;
-      s = htab->got;
-      srel = htab->relgot;
+      s = ppc64_elf_tdata (ibfd)->got;
+      srel = ppc64_elf_tdata (ibfd)->relgot;
       for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
        {
          struct got_entry *ent;
@@ -5688,14 +5915,14 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
              {
                if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
                  {
-                   if (htab->tlsld_got.offset == (bfd_vma) -1)
+                   if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
                      {
-                       htab->tlsld_got.offset = s->_raw_size;
+                       ppc64_tlsld_got (ibfd)->offset = s->_raw_size;
                        s->_raw_size += 16;
                        if (info->shared)
                          srel->_raw_size += sizeof (Elf64_External_Rela);
                      }
-                   ent->got.offset = htab->tlsld_got.offset;
+                   ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
                  }
                else
                  {
@@ -5738,16 +5965,8 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       if (s == htab->brlt || s == htab->relbrlt)
        /* These haven't been allocated yet;  don't strip.  */
        continue;
-      else if (s == htab->got)
-       {
-         /* Automatic multiple tocs aren't possible if we are using the
-            GOT.  The GOT is accessed via r2, so we can't adjust r2.
-            FIXME: There's no reason why we couldn't lay out multiple
-            GOTs too.  */
-         if (s->_raw_size > elf_backend_got_header_size)
-           htab->no_multi_toc = 1;
-       }
-      else if (s == htab->plt
+      else if (s == htab->got
+              || s == htab->plt
               || s == htab->glink)
        {
          /* Strip this section if we don't need it; see the
@@ -5790,7 +6009,7 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
        }
 
       /* .plt is in the bss section.  We don't initialise it.  */
-      if ((s->flags & SEC_LOAD) == 0)
+      if (s == htab->plt)
        continue;
 
       /* Allocate memory for the section contents.  We use bfd_zalloc
@@ -5805,6 +6024,38 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
        return FALSE;
     }
 
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+    {
+      s = ppc64_elf_tdata (ibfd)->got;
+      if (s != NULL && s != htab->got)
+       {
+         s->_cooked_size = 0;
+         if (s->_raw_size == 0)
+           _bfd_strip_section_from_output (info, s);
+         else
+           {
+             s->contents = bfd_zalloc (ibfd, s->_raw_size);
+             if (s->contents == NULL)
+               return FALSE;
+           }
+       }
+      s = ppc64_elf_tdata (ibfd)->relgot;
+      if (s != NULL)
+       {
+         s->_cooked_size = 0;
+         if (s->_raw_size == 0)
+           _bfd_strip_section_from_output (info, s);
+         else
+           {
+             s->contents = bfd_zalloc (ibfd, s->_raw_size);
+             if (s->contents == NULL)
+               return FALSE;
+             relocs = TRUE;
+             s->reloc_count = 0;
+           }
+       }
+    }
+
   if (htab->elf.dynamic_sections_created)
     {
       /* Add some entries to the .dynamic section.  We fill in the
@@ -5891,8 +6142,9 @@ ppc_type_of_stub (asection *input_sec,
              }
        }
 
-      if (h->elf.root.type != bfd_link_hash_defined
-         && h->elf.root.type != bfd_link_hash_defweak)
+      if (!(h->elf.root.type == bfd_link_hash_defined
+           || h->elf.root.type == bfd_link_hash_defweak)
+         || h->elf.root.u.def.section->output_section == NULL)
        return ppc_stub_none;
     }
 
@@ -5949,8 +6201,6 @@ ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   struct ppc_branch_hash_entry *br_entry;
   struct bfd_link_info *info;
   struct ppc_link_hash_table *htab;
-  asection *stub_sec;
-  bfd *stub_bfd;
   bfd_byte *loc;
   bfd_byte *p;
   unsigned int indx;
@@ -5963,29 +6213,10 @@ ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
   info = in_arg;
 
   htab = ppc_hash_table (info);
-  stub_sec = stub_entry->stub_sec;
 
   /* Make a note of the offset within the stubs for this entry.  */
-  stub_entry->stub_offset = stub_sec->_cooked_size;
-  loc = stub_sec->contents + stub_entry->stub_offset;
-
-  if (htab->emit_stub_syms)
-    {
-      struct elf_link_hash_entry *h;
-      h = elf_link_hash_lookup (&htab->elf, stub_entry->root.string,
-                               TRUE, FALSE, FALSE);
-      if (h == NULL)
-       return FALSE;
-      h->root.type = bfd_link_hash_defined;
-      h->root.u.def.section = stub_entry->stub_sec;
-      h->root.u.def.value = stub_entry->stub_offset;
-      h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
-                               | ELF_LINK_HASH_DEF_REGULAR
-                               | ELF_LINK_HASH_REF_REGULAR_NONWEAK
-                               | ELF_LINK_FORCED_LOCAL);
-    }
-
-  stub_bfd = stub_sec->owner;
+  stub_entry->stub_offset = stub_entry->stub_sec->_cooked_size;
+  loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
 
   htab->stub_count[stub_entry->stub_type - 1] += 1;
   switch (stub_entry->stub_type)
@@ -5999,8 +6230,8 @@ ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
 
       /* And this is where we are coming from.  */
       off -= (stub_entry->stub_offset
-             + stub_sec->output_offset
-             + stub_sec->output_section->vma);
+             + stub_entry->stub_sec->output_offset
+             + stub_entry->stub_sec->output_section->vma);
 
       if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
        size = 4;
@@ -6010,16 +6241,16 @@ ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
 
          r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
                   - htab->stub_group[stub_entry->id_sec->id].toc_off);
-         bfd_put_32 (stub_bfd, STD_R2_40R1, loc);
+         bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
          loc += 4;
-         bfd_put_32 (stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
+         bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
          loc += 4;
-         bfd_put_32 (stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
+         bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
          loc += 4;
          off -= 12;
          size = 16;
        }
-      bfd_put_32 (stub_bfd, B_DOT | (off & 0x3fffffc), loc);
+      bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
 
       BFD_ASSERT (off + (1 << 25) < (bfd_vma) (1 << 26));
       break;
@@ -6080,9 +6311,9 @@ ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
       indx = off;
       if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
        {
-         bfd_put_32 (stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
+         bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
          loc += 4;
-         bfd_put_32 (stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
+         bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
          size = 16;
        }
       else
@@ -6091,21 +6322,21 @@ ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
 
          r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
                   - htab->stub_group[stub_entry->id_sec->id].toc_off);
-         bfd_put_32 (stub_bfd, STD_R2_40R1, loc);
+         bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
          loc += 4;
-         bfd_put_32 (stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
+         bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
          loc += 4;
-         bfd_put_32 (stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
+         bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
          loc += 4;
-         bfd_put_32 (stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
+         bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
          loc += 4;
-         bfd_put_32 (stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
+         bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
          size = 28;
        }
       loc += 4;
-      bfd_put_32 (stub_bfd, MTCTR_R11, loc);
+      bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
       loc += 4;
-      bfd_put_32 (stub_bfd, BCTR, loc);
+      bfd_put_32 (htab->stub_bfd, BCTR, loc);
       break;
 
     case ppc_stub_plt_call:
@@ -6152,7 +6383,7 @@ ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
          return FALSE;
        }
 
-      p = build_plt_stub (stub_bfd, loc, off);
+      p = build_plt_stub (htab->stub_bfd, loc, off);
       size = p - loc;
       break;
 
@@ -6161,7 +6392,31 @@ ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
       return FALSE;
     }
 
-  stub_sec->_cooked_size += size;
+  stub_entry->stub_sec->_cooked_size += size;
+
+  if (htab->emit_stub_syms
+      && !(stub_entry->stub_type == ppc_stub_plt_call
+          && stub_entry->h->oh->root.type == bfd_link_hash_defined
+          && stub_entry->h->oh->root.u.def.section == stub_entry->stub_sec
+          && stub_entry->h->oh->root.u.def.value == stub_entry->stub_offset))
+    {
+      struct elf_link_hash_entry *h;
+      h = elf_link_hash_lookup (&htab->elf, stub_entry->root.string,
+                               TRUE, FALSE, FALSE);
+      if (h == NULL)
+       return FALSE;
+      if (h->root.type == bfd_link_hash_new)
+       {
+         h->root.type = bfd_link_hash_defined;
+         h->root.u.def.section = stub_entry->stub_sec;
+         h->root.u.def.value = stub_entry->stub_offset;
+         h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
+                                   | ELF_LINK_HASH_DEF_REGULAR
+                                   | ELF_LINK_HASH_REF_REGULAR_NONWEAK
+                                   | ELF_LINK_FORCED_LOCAL);
+       }
+    }
+
   return TRUE;
 }
 
@@ -6173,13 +6428,16 @@ static bfd_boolean
 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
 {
   struct ppc_stub_hash_entry *stub_entry;
+  struct bfd_link_info *info;
   struct ppc_link_hash_table *htab;
   bfd_vma off;
   int size;
 
   /* Massage our args to the form they really have.  */
   stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
-  htab = in_arg;
+  info = in_arg;
+
+  htab = ppc_hash_table (info);
 
   if (stub_entry->stub_type == ppc_stub_plt_call)
     {
@@ -6246,6 +6504,9 @@ ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
              br_entry->iter = htab->stub_iteration;
              br_entry->offset = htab->brlt->_raw_size;
              htab->brlt->_raw_size += 8;
+
+             if (info->shared)
+               htab->relbrlt->_raw_size += sizeof (Elf64_External_Rela);
            }
 
          stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
@@ -6323,10 +6584,10 @@ ppc64_elf_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
   return 1;
 }
 
-/* The linker repeatedly calls this function for each toc input
-   section.  Group input bfds such that the toc within a group
-   is less than 64k in size.  Will break with cute linker scripts
-   that play games with dot in the output toc section.  */
+/* The linker repeatedly calls this function for each TOC input section
+   and linker generated GOT section.  Group input bfds such that the toc
+   within a group is less than 64k in size.  Will break with cute linker
+   scripts that play games with dot in the output toc section.  */
 
 void
 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
@@ -6374,6 +6635,13 @@ toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   int ret;
   int branch_ok;
 
+  /* We know none of our code bearing sections will need toc stubs.  */
+  if ((isec->flags & SEC_LINKER_CREATED) != 0)
+    return 0;
+
+  if (isec->_raw_size == 0)
+    return 0;
+
   /* Hack for linux kernel.  .fixup contains branches, but only back to
      the function that hit an exception.  */
   branch_ok = strcmp (isec->name, ".fixup") == 0;
@@ -6385,7 +6653,7 @@ toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
       if (contents == NULL)
        return -1;
       if (! bfd_get_section_contents (isec->owner, isec, contents,
-                                     (file_ptr) 0, isec->_raw_size))
+                                     0, isec->_raw_size))
        {
          free (contents);
          return -1;
@@ -6401,7 +6669,7 @@ toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
     {
       unsigned long insn = bfd_get_32 (isec->owner, contents + i);
       /* Is this a branch?  */
-      if ((insn & (0x1f << 26)) == (18 << 26)
+      if ((insn & (0x3f << 26)) == (18 << 26)
          /* If branch and link, it's a function call.  */
          && ((insn & 1) != 0
              /* Sibling calls use a plain branch.  I don't know a way
@@ -6443,7 +6711,8 @@ ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
 
   /* If a code section has a function that uses the TOC then we need
      to use the right TOC (obviously).  Also, make sure that .opd gets
-     the correct TOC value.  */
+     the correct TOC value for R_PPC64_TOC relocs that don't have or
+     can't find their function symbol (shouldn't ever happen now).  */
   if (isec->has_gp_reloc || (isec->flags & SEC_CODE) == 0)
     {
       if (elf_gp (isec->owner) != 0)
@@ -6551,7 +6820,6 @@ group_sections (struct ppc_link_hash_table *htab,
 
 bfd_boolean
 ppc64_elf_size_stubs (bfd *output_bfd,
-                     bfd *stub_bfd,
                      struct bfd_link_info *info,
                      bfd_signed_vma group_size,
                      asection *(*add_stub_section) (const char *, asection *),
@@ -6562,7 +6830,6 @@ ppc64_elf_size_stubs (bfd *output_bfd,
   struct ppc_link_hash_table *htab = ppc_hash_table (info);
 
   /* Stash our params away.  */
-  htab->stub_bfd = stub_bfd;
   htab->add_stub_section = add_stub_section;
   htab->layout_sections_again = layout_sections_again;
   stubs_always_before_branch = group_size < 0;
@@ -6746,7 +7013,7 @@ ppc64_elf_size_stubs (bfd *output_bfd,
                      /* Get tls info.  */
                      char *tls_mask;
 
-                     if (!get_tls_mask (&tls_mask, &local_syms,
+                     if (!get_tls_mask (&tls_mask, NULL, &local_syms,
                                         irela - 1, input_bfd))
                        goto error_ret_free_internal;
                      if (*tls_mask != 0)
@@ -6816,14 +7083,20 @@ ppc64_elf_size_stubs (bfd *output_bfd,
       for (stub_sec = htab->stub_bfd->sections;
           stub_sec != NULL;
           stub_sec = stub_sec->next)
-       {
-         stub_sec->_raw_size = 0;
-         stub_sec->_cooked_size = 0;
-       }
+       if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
+         {
+           stub_sec->_raw_size = 0;
+           stub_sec->_cooked_size = 0;
+         }
       htab->brlt->_raw_size = 0;
       htab->brlt->_cooked_size = 0;
+      if (info->shared)
+       {
+         htab->relbrlt->_raw_size = 0;
+         htab->relbrlt->_cooked_size = 0;
+       }
 
-      bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, htab);
+      bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
 
       /* Ask the linker to do its stuff.  */
       (*htab->layout_sections_again) ();
@@ -6905,24 +7178,26 @@ ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
   struct ppc_link_hash_table *htab = ppc_hash_table (info);
   asection *stub_sec;
   bfd_byte *p;
+  int stub_sec_count = 0;
 
   htab->emit_stub_syms = emit_stub_syms;
   for (stub_sec = htab->stub_bfd->sections;
        stub_sec != NULL;
        stub_sec = stub_sec->next)
-    {
-      bfd_size_type size;
+    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
+      {
+       bfd_size_type size;
 
-      /* Allocate memory to hold the linker stubs.  */
-      size = stub_sec->_raw_size;
-      if (size != 0)
-       {
-         stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
-         if (stub_sec->contents == NULL)
-           return FALSE;
-       }
-      stub_sec->_cooked_size = 0;
-    }
+       /* Allocate memory to hold the linker stubs.  */
+       size = stub_sec->_raw_size;
+       if (size != 0)
+         {
+           stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
+           if (stub_sec->contents == NULL)
+             return FALSE;
+         }
+       stub_sec->_cooked_size = 0;
+      }
 
   if (htab->plt != NULL)
     {
@@ -6942,6 +7217,23 @@ ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
          return FALSE;
        }
 
+      if (htab->emit_stub_syms)
+       {
+         struct elf_link_hash_entry *h;
+         h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
+         if (h == NULL)
+           return FALSE;
+         if (h->root.type == bfd_link_hash_new)
+           {
+             h->root.type = bfd_link_hash_defined;
+             h->root.u.def.section = htab->glink;
+             h->root.u.def.value = 0;
+             h->elf_link_hash_flags = (ELF_LINK_HASH_REF_REGULAR
+                                       | ELF_LINK_HASH_DEF_REGULAR
+                                       | ELF_LINK_HASH_REF_REGULAR_NONWEAK
+                                       | ELF_LINK_FORCED_LOCAL);
+           }
+       }
       p = htab->glink->contents;
       bfd_put_32 (htab->glink->owner, MFCTR_R12, p);
       p += 4;
@@ -7007,6 +7299,13 @@ ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
       if (htab->brlt->contents == NULL)
        return FALSE;
     }
+  if (info->shared && htab->relbrlt->_raw_size != 0)
+    {
+      htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
+                                           htab->relbrlt->_raw_size);
+      if (htab->relbrlt->contents == NULL)
+       return FALSE;
+    }
 
   /* Build the stubs as directed by the stub hash table.  */
   bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
@@ -7014,10 +7313,12 @@ ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
   for (stub_sec = htab->stub_bfd->sections;
        stub_sec != NULL;
        stub_sec = stub_sec->next)
-    {
-      if (stub_sec->_raw_size != stub_sec->_cooked_size)
-       break;
-    }
+    if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
+      {
+       stub_sec_count += 1;
+       if (stub_sec->_raw_size != stub_sec->_cooked_size)
+         break;
+      }
 
   if (stub_sec != NULL
       || htab->glink->_raw_size != htab->glink->_cooked_size)
@@ -7041,7 +7342,7 @@ ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
                         "  long branch  %lu\n"
                         "  long toc adj %lu\n"
                         "  plt call     %lu"),
-              htab->stub_bfd->section_count,
+              stub_sec_count,
               htab->stub_count[ppc_stub_long_branch - 1],
               htab->stub_count[ppc_stub_long_branch_r2off - 1],
               htab->stub_count[ppc_stub_plt_branch - 1],
@@ -7130,18 +7431,29 @@ ppc64_elf_relocate_section (bfd *output_bfd,
       struct elf_link_hash_entry *h;
       struct elf_link_hash_entry *fdh;
       const char *sym_name;
-      unsigned long r_symndx;
+      unsigned long r_symndx, toc_symndx;
       char tls_mask, tls_gd, tls_type;
+      char sym_type;
       bfd_vma relocation;
       bfd_boolean unresolved_reloc;
       bfd_boolean warned;
-      long insn, mask;
+      unsigned long insn, mask;
       struct ppc_stub_hash_entry *stub_entry;
       bfd_vma max_br_offset;
       bfd_vma from;
 
       r_type = ELF64_R_TYPE (rel->r_info);
       r_symndx = ELF64_R_SYM (rel->r_info);
+
+      /* For old style R_PPC64_TOC relocs with a zero symbol, use the
+        symbol of the previous ADDR64 reloc.  The symbol gives us the
+        proper TOC base to use.  */
+      if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
+         && rel != relocs
+         && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
+         && is_opd)
+       r_symndx = ELF64_R_SYM (rel[-1].r_info);
+
       sym = NULL;
       sec = NULL;
       h = NULL;
@@ -7155,56 +7467,25 @@ ppc64_elf_relocate_section (bfd *output_bfd,
          sym = local_syms + r_symndx;
          sec = local_sections[r_symndx];
          sym_name = bfd_elf_local_sym_name (input_bfd, sym);
-         relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
+         sym_type = ELF64_ST_TYPE (sym->st_info);
+         relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
          if (elf_section_data (sec) != NULL)
            {
              long *opd_sym_adjust;
 
              opd_sym_adjust = ppc64_elf_section_data (sec)->opd.adjust;
-             if (opd_sym_adjust != NULL && sym->st_value % 24 == 0)
+             if (opd_sym_adjust != NULL)
                relocation += opd_sym_adjust[sym->st_value / 24];
            }
        }
       else
        {
-         /* It's a global symbol.  */
-         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-         while (h->root.type == bfd_link_hash_indirect
-                || h->root.type == bfd_link_hash_warning)
-           h = (struct elf_link_hash_entry *) h->root.u.i.link;
+         RELOC_FOR_GLOBAL_SYMBOL (h, sym_hashes, r_symndx,
+                                  symtab_hdr, relocation, sec,
+                                  unresolved_reloc, info,
+                                  warned);
          sym_name = h->root.root.string;
-         relocation = 0;
-         if (h->root.type == bfd_link_hash_defined
-             || h->root.type == bfd_link_hash_defweak)
-           {
-             sec = h->root.u.def.section;
-             if (sec->output_section == NULL)
-               /* Set a flag that will be cleared later if we find a
-                  relocation value for this symbol.  output_section
-                  is typically NULL for symbols satisfied by a shared
-                  library.  */
-               unresolved_reloc = TRUE;
-             else
-               relocation = (h->root.u.def.value
-                             + sec->output_section->vma
-                             + sec->output_offset);
-           }
-         else if (h->root.type == bfd_link_hash_undefweak)
-           ;
-         else if (info->shared
-                  && !info->no_undefined
-                  && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
-           ;
-         else
-           {
-             if (! ((*info->callbacks->undefined_symbol)
-                    (info, h->root.root.string, input_bfd, input_section,
-                     rel->r_offset, (!info->shared
-                                     || info->no_undefined
-                                     || ELF_ST_VISIBILITY (h->other)))))
-               return FALSE;
-             warned = TRUE;
-           }
+         sym_type = h->type;
        }
 
       /* TLS optimizations.  Replace instruction sequences and relocs
@@ -7213,6 +7494,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
         for the final instruction stream.  */
       tls_mask = 0;
       tls_gd = 0;
+      toc_symndx = 0;
       if (IS_PPC64_TLS_RELOC (r_type))
        {
          if (h != NULL)
@@ -7223,6 +7505,42 @@ ppc64_elf_relocate_section (bfd *output_bfd,
              lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
              tls_mask = lgot_masks[r_symndx];
            }
+         if (tls_mask == 0 && r_type == R_PPC64_TLS)
+           {
+             /* Check for toc tls entries.  */
+             char *toc_tls;
+
+             if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
+                                rel, input_bfd))
+               return FALSE;
+
+             if (toc_tls)
+               tls_mask = *toc_tls;
+           }
+       }
+
+      /* Check that tls relocs are used with tls syms, and non-tls
+        relocs are used with non-tls syms.  */
+      if (r_symndx != 0
+         && r_type != R_PPC64_NONE
+         && (h == NULL
+             || h->root.type == bfd_link_hash_defined
+             || h->root.type == bfd_link_hash_defweak)
+         && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
+       {
+         if (r_type == R_PPC64_TLS && tls_mask != 0)
+           /* R_PPC64_TLS is OK against a symbol in the TOC.  */
+           ;
+         else
+           (*_bfd_error_handler)
+             (sym_type == STT_TLS
+              ? _("%s(%s+0x%lx): %s used with TLS symbol %s")
+              : _("%s(%s+0x%lx): %s used with non-TLS symbol %s"),
+              bfd_archive_filename (input_bfd),
+              input_section->name,
+              (long) rel->r_offset,
+              ppc64_elf_howto_table[r_type]->name,
+              sym_name);
        }
 
       /* Ensure reloc mapping code below stays sane.  */
@@ -7237,6 +7555,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
          || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
          || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
        abort ();
+
       switch (r_type)
        {
        default:
@@ -7251,7 +7570,8 @@ ppc64_elf_relocate_section (bfd *output_bfd,
            char *toc_tls;
            int retval;
 
-           retval = get_tls_mask (&toc_tls, &local_syms, rel, input_bfd);
+           retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
+                                  rel, input_bfd);
            if (retval == 0)
              return FALSE;
 
@@ -7290,38 +7610,35 @@ ppc64_elf_relocate_section (bfd *output_bfd,
          if (tls_mask != 0
              && (tls_mask & TLS_TPREL) == 0)
            {
-             bfd_vma insn;
            toctprel:
              insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
              insn &= 31 << 21;
              insn |= 0x3c0d0000;       /* addis 0,13,0 */
              bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
              r_type = R_PPC64_TPREL16_HA;
-             rel->r_info = ELF64_R_INFO (r_symndx, r_type);
+             if (toc_symndx != 0)
+               {
+                 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
+                 /* We changed the symbol.  Start over in order to
+                    get h, sym, sec etc. right.  */
+                 rel--;
+                 continue;
+               }
+             else
+               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
            }
          break;
 
        case R_PPC64_TLS:
-         if (tls_mask == 0)
-           {
-             /* Check for toc tls entries.  */
-             char *toc_tls;
-
-             if (!get_tls_mask (&toc_tls, &local_syms, rel, input_bfd))
-               return FALSE;
-
-             if (toc_tls)
-               tls_mask = *toc_tls;
-           }
          if (tls_mask != 0
              && (tls_mask & TLS_TPREL) == 0)
            {
-             bfd_vma insn, rtra;
+             bfd_vma rtra;
              insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
-             if ((insn & ((31 << 26) | (31 << 11)))
+             if ((insn & ((0x3f << 26) | (31 << 11)))
                  == ((31 << 26) | (13 << 11)))
                rtra = insn & ((1 << 26) - (1 << 16));
-             else if ((insn & ((31 << 26) | (31 << 16)))
+             else if ((insn & ((0x3f << 26) | (31 << 16)))
                       == ((31 << 26) | (13 << 16)))
                rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
              else
@@ -7348,11 +7665,20 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                abort ();
              insn |= rtra;
              bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
-             r_type = R_PPC64_TPREL16_LO;
-             rel->r_info = ELF64_R_INFO (r_symndx, r_type);
              /* Was PPC64_TLS which sits on insn boundary, now
                 PPC64_TPREL16_LO which is at insn+2.  */
              rel->r_offset += 2;
+             r_type = R_PPC64_TPREL16_LO;
+             if (toc_symndx != 0)
+               {
+                 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
+                 /* We changed the symbol.  Start over in order to
+                    get h, sym, sec etc. right.  */
+                 rel--;
+                 continue;
+               }
+             else
+               rel->r_info = ELF64_R_INFO (r_symndx, r_type);
            }
          break;
 
@@ -7448,9 +7774,11 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                        {
                          /* Was an LD reloc.  */
                          r_symndx = 0;
-                         rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
-                         rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
+                         rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
+                         rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
                        }
+                     else if (toc_symndx != 0)
+                       r_symndx = toc_symndx;
                      r_type = R_PPC64_TPREL16_HA;
                      rel->r_info = ELF64_R_INFO (r_symndx, r_type);
                      rel[1].r_info = ELF64_R_INFO (r_symndx,
@@ -7467,10 +7795,10 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                  bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
                  bfd_put_32 (output_bfd, insn2, contents + offset);
                  bfd_put_32 (output_bfd, insn3, contents + offset + 4);
-                 if (tls_gd == 0)
+                 if (tls_gd == 0 || toc_symndx != 0)
                    {
-                     /* We changed the symbol on an LD reloc.  Start over
-                        in order to get h, sym, sec etc. right.  */
+                     /* We changed the symbol.  Start over in order
+                        to get h, sym, sec etc. right.  */
                      rel--;
                      continue;
                    }
@@ -7604,6 +7932,13 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                      if ((insn & 1) == 0)
                        can_plt_call = 1;
                    }
+                 else if (h != NULL
+                          && strcmp (h->root.root.string,
+                                     ".__libc_start_main") == 0)
+                   {
+                     /* Allow crt1 branch to go via a toc adjusting stub.  */
+                     can_plt_call = 1;
+                   }
                  else
                    {
                      if (strcmp (input_section->output_section->name,
@@ -7732,17 +8067,15 @@ ppc64_elf_relocate_section (bfd *output_bfd,
          {
            /* Relocation is to the entry for this symbol in the global
               offset table.  */
+           asection *got;
            bfd_vma *offp;
            bfd_vma off;
            unsigned long indx = 0;
 
-           if (htab->got == NULL)
-             abort ();
-
            if (tls_type == (TLS_TLS | TLS_LD)
                && (h == NULL
                    || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
-             offp = &htab->tlsld_got.offset;
+             offp = &ppc64_tlsld_got (input_bfd)->offset;
            else
              {
                struct got_entry *ent;
@@ -7774,6 +8107,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
 
                for (; ent != NULL; ent = ent->next)
                  if (ent->addend == rel->r_addend
+                     && ent->owner == input_bfd
                      && ent->tls_type == tls_type)
                    break;
                if (ent == NULL)
@@ -7781,6 +8115,10 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                offp = &ent->got.offset;
              }
 
+           got = ppc64_elf_tdata (input_bfd)->got;
+           if (got == NULL)
+             abort ();
+
            /* The offset must always be a multiple of 8.  We use the
               least significant bit to record whether we have already
               processed this entry.  */
@@ -7792,14 +8130,16 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                /* Generate relocs for the dynamic linker, except in
                   the case of TLSLD where we'll use one entry per
                   module.  */
+               asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
+
                *offp = off | 1;
                if ((info->shared || indx != 0)
                    && (h == NULL
                        || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
                        || h->root.type != bfd_link_hash_undefweak))
                  {
-                   outrel.r_offset = (htab->got->output_section->vma
-                                      + htab->got->output_offset
+                   outrel.r_offset = (got->output_section->vma
+                                      + got->output_offset
                                       + off);
                    outrel.r_addend = rel->r_addend;
                    if (tls_type & (TLS_LD | TLS_GD))
@@ -7808,8 +8148,8 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                        outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
                        if (tls_type == (TLS_TLS | TLS_GD))
                          {
-                           loc = htab->relgot->contents;
-                           loc += (htab->relgot->reloc_count++
+                           loc = relgot->contents;
+                           loc += (relgot->reloc_count++
                                    * sizeof (Elf64_External_Rela));
                            bfd_elf64_swap_reloca_out (output_bfd,
                                                       &outrel, loc);
@@ -7829,7 +8169,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
 
                        /* Write the .got section contents for the sake
                           of prelink.  */
-                       loc = htab->got->contents + off;
+                       loc = got->contents + off;
                        bfd_put_64 (output_bfd, outrel.r_addend + relocation,
                                    loc);
                      }
@@ -7840,10 +8180,10 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                      {
                        outrel.r_addend += relocation;
                        if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
-                         outrel.r_addend -= htab->tls_sec->vma;
+                         outrel.r_addend -= htab->elf.tls_sec->vma;
                      }
-                   loc = htab->relgot->contents;
-                   loc += (htab->relgot->reloc_count++
+                   loc = relgot->contents;
+                   loc += (relgot->reloc_count++
                            * sizeof (Elf64_External_Rela));
                    bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
                  }
@@ -7857,30 +8197,30 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                      relocation = 1;
                    else if (tls_type != 0)
                      {
-                       relocation -= htab->tls_sec->vma + DTP_OFFSET;
+                       relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
                        if (tls_type == (TLS_TLS | TLS_TPREL))
                          relocation += DTP_OFFSET - TP_OFFSET;
 
                        if (tls_type == (TLS_TLS | TLS_GD))
                          {
                            bfd_put_64 (output_bfd, relocation,
-                                       htab->got->contents + off + 8);
+                                       got->contents + off + 8);
                            relocation = 1;
                          }
                      }
 
                    bfd_put_64 (output_bfd, relocation,
-                               htab->got->contents + off);
+                               got->contents + off);
                  }
              }
 
            if (off >= (bfd_vma) -2)
              abort ();
 
-           relocation = htab->got->output_offset + off;
+           relocation = got->output_offset + off;
 
            /* TOC base (r2) is TOC start plus 0x8000.  */
-           addend = - TOC_BASE_OFF;
+           addend = -TOC_BASE_OFF;
          }
          break;
 
@@ -7921,7 +8261,9 @@ ppc64_elf_relocate_section (bfd *output_bfd,
          relocation = TOCstart;
          if (r_symndx == 0)
            relocation += htab->stub_group[input_section->id].toc_off;
-         else if (sec != NULL && !unresolved_reloc)
+         else if (unresolved_reloc)
+           ;
+         else if (sec != NULL && sec->id <= htab->top_id)
            relocation += htab->stub_group[sec->id].toc_off;
          else
            unresolved_reloc = TRUE;
@@ -7967,7 +8309,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
        case R_PPC64_TPREL16_HIGHERA:
        case R_PPC64_TPREL16_HIGHEST:
        case R_PPC64_TPREL16_HIGHESTA:
-         addend -= htab->tls_sec->vma + TP_OFFSET;
+         addend -= htab->elf.tls_sec->vma + TP_OFFSET;
          if (info->shared)
            /* The TPREL16 relocs shouldn't really be used in shared
               libs as they will result in DT_TEXTREL being set, but
@@ -7985,7 +8327,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
        case R_PPC64_DTPREL16_HIGHERA:
        case R_PPC64_DTPREL16_HIGHEST:
        case R_PPC64_DTPREL16_HIGHESTA:
-         addend -= htab->tls_sec->vma + DTP_OFFSET;
+         addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
          break;
 
        case R_PPC64_DTPMOD64:
@@ -7994,11 +8336,11 @@ ppc64_elf_relocate_section (bfd *output_bfd,
          goto dodyn;
 
        case R_PPC64_TPREL64:
-         addend -= htab->tls_sec->vma + TP_OFFSET;
+         addend -= htab->elf.tls_sec->vma + TP_OFFSET;
          goto dodyn;
 
        case R_PPC64_DTPREL64:
-         addend -= htab->tls_sec->vma + DTP_OFFSET;
+         addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
          /* Fall thru */
 
          /* Relocations that may need to be propagated if this is a
@@ -8045,8 +8387,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
                   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
                   || h->root.type != bfd_link_hash_undefweak)
               && (MUST_BE_DYN_RELOC (r_type)
-                  || (h != NULL
-                      && !SYMBOL_CALLS_LOCAL (info, h))))
+                  || !SYMBOL_CALLS_LOCAL (info, h)))
              || (ELIMINATE_COPY_RELOCS
                  && !info->shared
                  && h != NULL
@@ -8092,8 +8433,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
 
              if (skip)
                memset (&outrel, 0, sizeof outrel);
-             else if (h != NULL
-                      && !SYMBOL_REFERENCES_LOCAL (info, h)
+             else if (!SYMBOL_REFERENCES_LOCAL (info, h)
                       && !is_opd
                       && r_type != R_PPC64_TOC)
                outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
@@ -8286,7 +8626,7 @@ ppc64_elf_relocate_section (bfd *output_bfd,
             _DS relocs bloats all reloc switches in this file.  It
             doesn't seem to make much sense to use any of these relocs
             in data, so testing the insn should be safe.  */
-         if ((insn & (0x3f << 26)) == (56 << 26))
+         if ((insn & (0x3f << 26)) == (56u << 26))
            mask = 15;
          if (((relocation + addend) & mask) != 0)
            {
@@ -8405,6 +8745,32 @@ ppc64_elf_relocate_section (bfd *output_bfd,
   return ret;
 }
 
+/* Adjust the value of any local symbols in opd sections.  */
+
+static bfd_boolean
+ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
+                             const char *name ATTRIBUTE_UNUSED,
+                             Elf_Internal_Sym *elfsym,
+                             asection *input_sec,
+                             struct elf_link_hash_entry *h)
+{
+  long *adjust;
+  bfd_vma value;
+
+  if (h != NULL
+      || input_sec == NULL
+      || ppc64_elf_section_data (input_sec) == NULL
+      || (adjust = ppc64_elf_section_data (input_sec)->opd.adjust) == NULL)
+    return TRUE;
+
+  value = elfsym->st_value - input_sec->output_offset;
+  if (!info->relocatable)
+    value -= input_sec->output_section->vma;
+
+  elfsym->st_value += adjust[value / 24];
+  return TRUE;
+}
+
 /* Finish up dynamic symbol handling.  We set the contents of various
    dynamic sections here.  */
 
@@ -8622,6 +8988,29 @@ ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
        = PLT_ENTRY_SIZE;
     }
 
+  /* We need to handle writing out multiple GOT sections ourselves,
+     since we didn't add them to DYNOBJ.  */
+  while ((dynobj = dynobj->link_next) != NULL)
+    {
+      asection *s;
+      s = ppc64_elf_tdata (dynobj)->got;
+      if (s != NULL
+         && s->_raw_size != 0
+         && s->output_section != bfd_abs_section_ptr
+         && !bfd_set_section_contents (output_bfd, s->output_section,
+                                       s->contents, s->output_offset,
+                                       s->_raw_size))
+       return FALSE;
+      s = ppc64_elf_tdata (dynobj)->relgot;
+      if (s != NULL
+         && s->_raw_size != 0
+         && s->output_section != bfd_abs_section_ptr
+         && !bfd_set_section_contents (output_bfd, s->output_section,
+                                       s->contents, s->output_offset,
+                                       s->_raw_size))
+       return FALSE;
+    }
+
   return TRUE;
 }
 
This page took 0.056728 seconds and 4 git commands to generate.