2004-05-14 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
index 1e6d22df14778f8429636b977cbf4d0adedc7118..a0d81d0550eeb2ae89d3f4aa1ee6c719b05bf43a 100644 (file)
@@ -1,7 +1,9 @@
 /* PowerPC64-specific support for 64-bit ELF.
-   Copyright 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
+   Copyright 1999, 2000, 2001, 2002, 2003, 2004
+   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.
 
@@ -28,7 +30,6 @@
 #include "bfdlink.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
-#include "elf/ppc.h"
 #include "elf/ppc64.h"
 #include "elf64-ppc.h"
 
@@ -65,7 +66,6 @@ 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
@@ -78,8 +78,11 @@ 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_add_symbol_hook          ppc64_elf_add_symbol_hook
 #define elf_backend_check_relocs             ppc64_elf_check_relocs
 #define elf_backend_gc_mark_hook             ppc64_elf_gc_mark_hook
 #define elf_backend_gc_sweep_hook            ppc64_elf_gc_sweep_hook
@@ -91,6 +94,7 @@ 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
@@ -2373,6 +2377,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.  */
 
@@ -2402,6 +2445,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;
@@ -2580,7 +2636,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
@@ -2698,6 +2765,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.
@@ -2773,9 +2843,6 @@ 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;
 
@@ -2910,6 +2977,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;
     }
 
@@ -3005,7 +3073,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);
@@ -3017,7 +3085,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,
@@ -3246,9 +3314,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;
@@ -3297,7 +3366,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.  */
@@ -3405,6 +3475,22 @@ ppc64_elf_mark_entry_syms (struct bfd_link_info *info)
   return TRUE;
 }
 
+/* Hack symbols defined in .opd sections to be function type.  */
+
+static bfd_boolean
+ppc64_elf_add_symbol_hook (bfd *ibfd ATTRIBUTE_UNUSED,
+                          struct bfd_link_info *info ATTRIBUTE_UNUSED,
+                          Elf_Internal_Sym *isym,
+                          const char **name ATTRIBUTE_UNUSED,
+                          flagword *flags ATTRIBUTE_UNUSED,
+                          asection **sec,
+                          bfd_vma *value ATTRIBUTE_UNUSED)
+{
+  if (strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
+    isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
+  return TRUE;
+}
+
 static bfd_boolean
 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
                       unsigned long r_symndx, bfd_vma r_addend, int tls_type)
@@ -3478,6 +3564,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.  */
@@ -3497,6 +3608,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;
 
@@ -3688,14 +3808,14 @@ ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
          /* This relocation describes the C++ object vtable hierarchy.
             Reconstruct it for later use during GC.  */
        case R_PPC64_GNU_VTINHERIT:
-         if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
+         if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
            return FALSE;
          break;
 
          /* This relocation describes which C++ vtable entries are actually
             used.  Record for later use during GC.  */
        case R_PPC64_GNU_VTENTRY:
-         if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
+         if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
            return FALSE;
          break;
 
@@ -3806,19 +3926,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
@@ -3865,10 +3974,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
@@ -4084,6 +4189,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);
@@ -4216,115 +4324,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
-             || (fdh->root.type == bfd_link_hash_undefweak
-                 && ELF_ST_VISIBILITY (fdh->other) == STV_DEFAULT)))
+  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_elf_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;
 }
 
@@ -4500,9 +4611,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
@@ -4537,6 +4645,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
@@ -4737,11 +4861,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;
@@ -4770,24 +4895,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)
     {
@@ -4838,7 +5003,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;
@@ -4849,26 +5014,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.
@@ -4882,10 +5030,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)
            {
@@ -4915,6 +5073,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)
@@ -4934,10 +5097,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;
@@ -4955,16 +5118,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);
@@ -4974,27 +5143,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;
                        }
@@ -5005,44 +5153,25 @@ 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;
-                       }
-                     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;
+                         fdh->adjust_done = 1;
                        }
 
+                     /* 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);
                      wptr += 24;
@@ -5051,11 +5180,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));
@@ -5070,6 +5229,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);
@@ -5084,25 +5244,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;
@@ -5114,7 +5269,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
@@ -5187,7 +5342,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;
@@ -5197,7 +5362,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);
                  }
@@ -5272,7 +5437,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;
@@ -5409,17 +5574,6 @@ ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
   return TRUE;
 }
 
-/* This is the condition under which ppc64_elf_finish_dynamic_symbol
-   will be called from elflink.h.  If elflink.h doesn't call our
-   finish_dynamic_symbol routine, we'll need to do something about
-   initializing any .plt and .got entries in ppc64_elf_relocate_section.  */
-#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
-  ((DYN)                                                               \
-   && ((SHARED)                                                                \
-       || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)     \
-   && ((H)->dynindx != -1                                              \
-       || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
-
 /* Allocate space in .plt, .got and associated reloc sections for
    dynamic relocs.  */
 
@@ -5529,7 +5683,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
        if (h->dynindx == -1
            && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
          {
-           if (! bfd_elf64_link_record_dynamic_symbol (info, h))
+           if (! bfd_elf_link_record_dynamic_symbol (info, h))
              return FALSE;
          }
 
@@ -5610,7 +5764,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
          if (h->dynindx == -1
              && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
            {
-             if (! bfd_elf64_link_record_dynamic_symbol (info, h))
+             if (! bfd_elf_link_record_dynamic_symbol (info, h))
                return FALSE;
            }
 
@@ -5862,7 +6016,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
@@ -5917,7 +6071,7 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
         the .dynamic section.  The DT_DEBUG entry is filled in by the
         dynamic linker and used by the debugger.  */
 #define add_dynamic_entry(TAG, VAL) \
-  bfd_elf64_add_dynamic_entry (info, (TAG), (VAL))
+  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
 
       if (info->executable)
        {
@@ -5995,8 +6149,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;
     }
 
@@ -6053,8 +6208,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;
@@ -6067,29 +6220,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)
@@ -6103,8 +6237,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;
@@ -6114,16 +6248,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;
@@ -6184,9 +6318,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
@@ -6195,21 +6329,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:
@@ -6256,7 +6390,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;
 
@@ -6265,7 +6399,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;
 }
 
@@ -6277,13 +6435,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)
     {
@@ -6350,6 +6511,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;
@@ -6482,6 +6646,9 @@ toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
   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;
@@ -6551,7 +6718,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)
@@ -6852,7 +7020,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)
@@ -6929,8 +7097,13 @@ ppc64_elf_size_stubs (bfd *output_bfd,
          }
       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) ();
@@ -7051,6 +7224,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;
@@ -7116,6 +7306,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);
@@ -7241,18 +7438,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;
@@ -7266,56 +7474,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 (info, input_bfd, input_section, rel,
+                                  r_symndx, symtab_hdr, sym_hashes,
+                                  h, sec, relocation,
+                                  unresolved_reloc, 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->executable
-                  && !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
@@ -7324,6 +7501,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)
@@ -7334,6 +7512,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.  */
@@ -7348,6 +7562,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:
@@ -7362,7 +7577,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;
 
@@ -7401,33 +7617,30 @@ 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 & ((0x3f << 26) | (31 << 11)))
                  == ((31 << 26) | (13 << 11)))
@@ -7459,11 +7672,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;
 
@@ -7559,9 +7781,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,
@@ -7578,10 +7802,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;
                    }
@@ -7715,6 +7939,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,
@@ -7956,7 +8187,7 @@ 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 = relgot->contents;
                    loc += (relgot->reloc_count++
@@ -7973,7 +8204,7 @@ 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;
 
@@ -8037,7 +8268,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;
@@ -8083,7 +8316,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
@@ -8101,7 +8334,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:
@@ -8110,11 +8343,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
@@ -8400,7 +8633,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)
            {
@@ -8519,6 +8752,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.  */
 
@@ -8762,37 +9021,4 @@ ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
   return TRUE;
 }
 
-/* Add extra PPC sections -- Note, for now, make .sbss2 and
-   .PPC.EMB.sbss0 a normal section, and not a bss section so
-   that the linker doesn't crater when trying to make more than
-   2 sections.  */
-
-static struct bfd_elf_special_section const ppc64_elf_special_sections[]=
-{
-  { ".tags",           0,      NULL,   0,
-    SHT_ORDERED,       SHF_ALLOC },
-  { ".sdata",          0,      NULL,   0,
-    SHT_PROGBITS,      SHF_ALLOC + SHF_WRITE },
-  { ".sbss",           0,      NULL,   0,
-    SHT_NOBITS,                SHF_ALLOC + SHF_WRITE },
-  { ".sdata2",         0,      NULL,   0,
-    SHT_PROGBITS,      SHF_ALLOC },
-  { ".sbss2",          0,      NULL,   0,
-    SHT_PROGBITS,      SHF_ALLOC },
-  { ".PPC.EMB.apuinfo",        0,      NULL,   0,
-    SHT_NOTE,          0 },
-  { ".PPC.EMB.sdata0", 0,      NULL,   0,
-    SHT_PROGBITS,      SHF_ALLOC },
-  { ".PPC.EMB.sbss0",  0,      NULL,   0,
-    SHT_PROGBITS,      SHF_ALLOC },
-  { ".plt",            0,      NULL,   0,
-    SHT_NOBITS,                0 },
-  { ".toc",            0,      NULL,   0,
-    SHT_PROGBITS,      SHF_ALLOC + SHF_WRITE },
-  { ".tocbss",         0,      NULL,   0,
-    SHT_NOBITS,                SHF_ALLOC + SHF_WRITE },
-  { NULL,              0,      NULL,   0,
-    0,                 0 }
-};
-
 #include "elf64-target.h"
This page took 0.051964 seconds and 4 git commands to generate.