bfd/
[binutils-gdb.git] / bfd / elf32-ppc.c
index 27efe2ee4622b8d7c8d5b2e25d8cc5dbb3a895da..31765b18e3524b179a5b6f523aa12b502e2f623a 100644 (file)
@@ -16,8 +16,9 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+   along with this program; if not, write to the
+   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.  */
 
 /* This file is based on a preliminary PowerPC ELF ABI.  The
    information may not match the final PowerPC ELF ABI.  It includes
 
 /* RELA relocations are used here.  */
 
-static struct bfd_hash_entry *ppc_elf_link_hash_newfunc
-  PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
-          const char *string));
-static struct bfd_link_hash_table *ppc_elf_link_hash_table_create
-  PARAMS ((bfd *abfd));
-static void ppc_elf_copy_indirect_symbol
-  PARAMS ((struct elf_backend_data *bed, struct elf_link_hash_entry *dir,
-          struct elf_link_hash_entry *ind));
-static reloc_howto_type *ppc_elf_reloc_type_lookup
-  PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
-static void ppc_elf_info_to_howto
-  PARAMS ((bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst));
-static void ppc_elf_howto_init
-  PARAMS ((void));
-static int ppc_elf_sort_rela
-  PARAMS ((const PTR, const PTR));
-static bfd_boolean ppc_elf_relax_section
-  PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *));
 static bfd_reloc_status_type ppc_elf_addr16_ha_reloc
-  PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
+  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
 static bfd_reloc_status_type ppc_elf_unhandled_reloc
-  PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
-static bfd_boolean ppc_elf_object_p
-  PARAMS ((bfd *));
-static bfd_boolean ppc_elf_set_private_flags
-  PARAMS ((bfd *, flagword));
-static bfd_boolean ppc_elf_merge_private_bfd_data
-  PARAMS ((bfd *, bfd *));
-static int ppc_elf_additional_program_headers
-  PARAMS ((bfd *));
-static bfd_boolean ppc_elf_modify_segment_map
-  PARAMS ((bfd *));
-static bfd_boolean ppc_elf_create_got
-  PARAMS ((bfd *, struct bfd_link_info *));
-static bfd_boolean ppc_elf_create_dynamic_sections
-  PARAMS ((bfd *, struct bfd_link_info *));
-static bfd_boolean ppc_elf_section_from_shdr
-  PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
-static bfd_boolean ppc_elf_fake_sections
-  PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
-static elf_linker_section_t *ppc_elf_create_linker_section
-  PARAMS ((bfd *abfd, struct bfd_link_info *info,
-          enum elf_linker_section_enum));
-static bfd_boolean update_local_sym_info
-  PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned long, int));
-static void bad_shared_reloc
-  PARAMS ((bfd *, enum elf_ppc_reloc_type));
-static bfd_boolean ppc_elf_check_relocs
-  PARAMS ((bfd *, struct bfd_link_info *, asection *,
-          const Elf_Internal_Rela *));
-static asection * ppc_elf_gc_mark_hook
-  PARAMS ((asection *sec, struct bfd_link_info *info, Elf_Internal_Rela *rel,
-          struct elf_link_hash_entry *h, Elf_Internal_Sym *sym));
-static bfd_boolean ppc_elf_gc_sweep_hook
-  PARAMS ((bfd *abfd, struct bfd_link_info *info, asection *sec,
-          const Elf_Internal_Rela *relocs));
-static bfd_boolean ppc_elf_adjust_dynamic_symbol
-  PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
-static bfd_boolean allocate_dynrelocs
-  PARAMS ((struct elf_link_hash_entry *, PTR));
-static bfd_boolean readonly_dynrelocs
-  PARAMS ((struct elf_link_hash_entry *, PTR));
-static bfd_boolean ppc_elf_size_dynamic_sections
-  PARAMS ((bfd *, struct bfd_link_info *));
-static bfd_boolean ppc_elf_relocate_section
-  PARAMS ((bfd *, struct bfd_link_info *info, bfd *, asection *, bfd_byte *,
-          Elf_Internal_Rela *relocs, Elf_Internal_Sym *local_syms,
-          asection **));
-static bfd_boolean ppc_elf_add_symbol_hook
-  PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
-          const char **, flagword *, asection **, bfd_vma *));
-static bfd_boolean ppc_elf_finish_dynamic_symbol
-  PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
-          Elf_Internal_Sym *));
-static bfd_boolean ppc_elf_finish_dynamic_sections
-  PARAMS ((bfd *, struct bfd_link_info *));
-static enum elf_reloc_type_class ppc_elf_reloc_type_class
-  PARAMS ((const Elf_Internal_Rela *));
-static bfd_boolean ppc_elf_grok_prstatus
-  PARAMS ((bfd *abfd, Elf_Internal_Note *note));
-static bfd_boolean ppc_elf_grok_psinfo
-  PARAMS ((bfd *abfd, Elf_Internal_Note *note));
-
-#define BRANCH_PREDICT_BIT 0x200000            /* Branch prediction bit for branch taken relocs.  */
-#define RA_REGISTER_MASK 0x001f0000            /* Mask to set RA in memory instructions.  */
-#define RA_REGISTER_SHIFT 16                   /* Value to shift register by to insert RA.  */
+  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
+
+/* Branch prediction bit for branch taken relocs.  */
+#define BRANCH_PREDICT_BIT 0x200000
+/* Mask to set RA in memory instructions.  */
+#define RA_REGISTER_MASK 0x001f0000
+/* Value to shift register by to insert RA.  */
+#define RA_REGISTER_SHIFT 16
 
 /* The name of the dynamic interpreter.  This is put in the .interp
    section.  */
-
 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
 
 /* The size in bytes of an entry in the procedure linkage table.  */
@@ -144,24 +69,73 @@ static bfd_boolean ppc_elf_grok_psinfo
 #define TP_OFFSET      0x7000
 #define DTP_OFFSET     0x8000
 
-/* Will references to this symbol always reference the symbol
-   in this object?  */
-#define SYMBOL_REFERENCES_LOCAL(INFO, H)                               \
-  ((! INFO->shared                                                     \
-    || INFO->symbolic                                                  \
-    || H->dynindx == -1                                                        \
-    || ELF_ST_VISIBILITY (H->other) == STV_INTERNAL                    \
-    || ELF_ST_VISIBILITY (H->other) == STV_HIDDEN)                     \
-   && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
-
-/* Will _calls_ to this symbol always call the version in this object?  */
-#define SYMBOL_CALLS_LOCAL(INFO, H)                            \
-  ((! INFO->shared                                                     \
-    || INFO->symbolic                                                  \
-    || H->dynindx == -1                                                        \
-    || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT)                    \
-   && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
 \f
+/* Enumeration to specify the special section.  */
+enum elf_linker_section_enum
+{
+  LINKER_SECTION_SDATA,
+  LINKER_SECTION_SDATA2
+};
+
+/* Sections created by the linker.  */
+
+typedef struct elf_linker_section
+{
+  /* pointer to the section */
+  asection *section;
+  /* pointer to the relocations needed for this section */
+  asection *rel_section;
+  /* pointer to the created symbol hash value */
+  struct elf_link_hash_entry *sym_hash;
+  /* offset of symbol from beginning of section */
+  bfd_vma sym_offset;
+} elf_linker_section_t;
+
+/* Linked list of allocated pointer entries.  This hangs off of the
+   symbol lists, and provides allows us to return different pointers,
+   based on different addend's.  */
+
+typedef struct elf_linker_section_pointers
+{
+  /* next allocated pointer for this symbol */
+  struct elf_linker_section_pointers *next;
+  /* offset of pointer from beginning of section */
+  bfd_vma offset;
+  /* addend used */
+  bfd_vma addend;
+  /* which linker section this is */
+  elf_linker_section_t *lsect;
+  /* whether address was written yet */
+  bfd_boolean written_address_p;
+} elf_linker_section_pointers_t;
+
+struct ppc_elf_obj_tdata
+{
+  struct elf_obj_tdata elf;
+
+  /* A mapping from local symbols to offsets into the various linker
+     sections added.  This is index by the symbol index.  */
+  elf_linker_section_pointers_t **linker_section_pointers;
+};
+
+#define ppc_elf_tdata(bfd) \
+  ((struct ppc_elf_obj_tdata *) (bfd)->tdata.any)
+
+#define elf_local_ptr_offsets(bfd) \
+  (ppc_elf_tdata (bfd)->linker_section_pointers)
+
+/* Override the generic function because we store some extras.  */
+
+static bfd_boolean
+ppc_elf_mkobject (bfd *abfd)
+{
+  bfd_size_type amt = sizeof (struct ppc_elf_obj_tdata);
+  abfd->tdata.any = bfd_zalloc (abfd, amt);
+  if (abfd->tdata.any == NULL)
+    return FALSE;
+  return TRUE;
+}
+
 /* The PPC linker needs to keep track of the number of relocs that it
    decides to copy as dynamic relocs in check_relocs for each symbol.
    This is so that it can later discard them if they are found to be
@@ -177,6 +151,9 @@ struct ppc_elf_dyn_relocs
 
   /* Total number of relocs copied for the input section.  */
   bfd_size_type count;
+
+  /* Number of pc-relative relocs copied for the input section.  */
+  bfd_size_type pc_count;
 };
 
 /* PPC ELF linker hash entry.  */
@@ -185,11 +162,16 @@ struct ppc_elf_link_hash_entry
 {
   struct elf_link_hash_entry elf;
 
+  /* If this symbol is used in the linker created sections, the processor
+     specific backend uses this field to map the field into the offset
+     from the beginning of the section.  */
+  elf_linker_section_pointers_t *linker_section_pointer;
+
   /* Track dynamic relocs copied for this symbol.  */
   struct ppc_elf_dyn_relocs *dyn_relocs;
 
   /* Contexts in which symbol is used in the GOT (or TOC).
-     TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
+     TLS_GD .. TLS_TLS bits are or'd into the mask as the
      corresponding relocs are encountered during check_relocs.
      tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
      indicate the corresponding GOT entry type is not needed.  */
@@ -221,6 +203,7 @@ struct ppc_elf_link_hash_table
   asection *relsbss;
   elf_linker_section_t *sdata;
   elf_linker_section_t *sdata2;
+  asection *sbss;
 
   /* Short-cut to first output tls section.  */
   asection *tls_sec;
@@ -246,10 +229,9 @@ struct ppc_elf_link_hash_table
 /* Create an entry in a PPC ELF linker hash table.  */
 
 static struct bfd_hash_entry *
-ppc_elf_link_hash_newfunc (entry, table, string)
-     struct bfd_hash_entry *entry;
-     struct bfd_hash_table *table;
-     const char *string;
+ppc_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
+                          struct bfd_hash_table *table,
+                          const char *string)
 {
   /* Allocate the structure if it has not already been allocated by a
      subclass.  */
@@ -265,6 +247,7 @@ ppc_elf_link_hash_newfunc (entry, table, string)
   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
   if (entry != NULL)
     {
+      ppc_elf_hash_entry (entry)->linker_section_pointer = NULL;
       ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
       ppc_elf_hash_entry (entry)->tls_mask = 0;
     }
@@ -275,13 +258,11 @@ ppc_elf_link_hash_newfunc (entry, table, string)
 /* Create a PPC ELF linker hash table.  */
 
 static struct bfd_link_hash_table *
-ppc_elf_link_hash_table_create (abfd)
-     bfd *abfd;
+ppc_elf_link_hash_table_create (bfd *abfd)
 {
   struct ppc_elf_link_hash_table *ret;
 
-  ret = ((struct ppc_elf_link_hash_table *)
-        bfd_malloc (sizeof (struct ppc_elf_link_hash_table)));
+  ret = bfd_zmalloc (sizeof (struct ppc_elf_link_hash_table));
   if (ret == NULL)
     return NULL;
 
@@ -292,30 +273,21 @@ ppc_elf_link_hash_table_create (abfd)
       return NULL;
     }
 
-  ret->got = NULL;
-  ret->relgot = NULL;
-  ret->plt = NULL;
-  ret->relplt = NULL;
-  ret->dynbss = NULL;
-  ret->relbss = NULL;
-  ret->dynsbss = NULL;
-  ret->relsbss = NULL;
-  ret->sdata = NULL;
-  ret->sdata2 = NULL;
-  ret->tls_sec = NULL;
-  ret->tls_get_addr = NULL;
-  ret->tlsld_got.refcount = 0;
-  ret->sym_sec.abfd = NULL;
-
   return &ret->elf.root;
 }
 
+/* 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.  */
+#define ELIMINATE_COPY_RELOCS 1
+
 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
 
 static void
-ppc_elf_copy_indirect_symbol (bed, dir, ind)
-     struct elf_backend_data *bed;
-     struct elf_link_hash_entry *dir, *ind;
+ppc_elf_copy_indirect_symbol (struct elf_backend_data *bed,
+                             struct elf_link_hash_entry *dir,
+                             struct elf_link_hash_entry *ind)
 {
   struct ppc_elf_link_hash_entry *edir, *eind;
 
@@ -341,6 +313,7 @@ ppc_elf_copy_indirect_symbol (bed, dir, ind)
              for (q = edir->dyn_relocs; q != NULL; q = q->next)
                if (q->sec == p->sec)
                  {
+                   q->pc_count += p->pc_count;
                    q->count += p->count;
                    *pp = p->next;
                    break;
@@ -357,10 +330,21 @@ ppc_elf_copy_indirect_symbol (bed, dir, ind)
 
   edir->tls_mask |= eind->tls_mask;
 
-  _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
+  if (ELIMINATE_COPY_RELOCS
+      && ind->root.type != bfd_link_hash_indirect
+      && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
+    /* 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.  */
+    dir->elf_link_hash_flags |=
+      (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
+                                  | ELF_LINK_HASH_REF_REGULAR
+                                  | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
+  else
+    _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
 }
 \f
-static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
+static reloc_howto_type *ppc_elf_howto_table[R_PPC_max];
 
 static reloc_howto_type ppc_elf_howto_raw[] = {
   /* This reloc does nothing.  */
@@ -1599,14 +1583,18 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
 
 static void
-ppc_elf_howto_init ()
+ppc_elf_howto_init (void)
 {
   unsigned int i, type;
 
-  for (i = 0; i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]); i++)
+  for (i = 0;
+       i < sizeof (ppc_elf_howto_raw) / sizeof (ppc_elf_howto_raw[0]);
+       i++)
     {
       type = ppc_elf_howto_raw[i].type;
-      BFD_ASSERT (type < sizeof (ppc_elf_howto_table) / sizeof (ppc_elf_howto_table[0]));
+      if (type >= (sizeof (ppc_elf_howto_table)
+                  / sizeof (ppc_elf_howto_table[0])))
+       abort ();
       ppc_elf_howto_table[type] = &ppc_elf_howto_raw[i];
     }
 }
@@ -1620,7 +1608,8 @@ ppc_elf_howto_init ()
    2/ The branch is predicted as not taken.
    3/ The branch is taken.
    4/ The branch is located in the last 5 words of a page.
-      (The EOP limit is 5 by default but may be specified as any value from 1-10.)
+      (The EOP limit is 5 by default but may be specified as any value
+      from 1-10.)
 
    Our software solution is to detect these problematic branches in a
    linker pass and modify them as follows:
@@ -1635,23 +1624,20 @@ ppc_elf_howto_init ()
 /* Sort sections by address.  */
 
 static int
-ppc_elf_sort_rela (arg1, arg2)
-     const PTR arg1;
-     const PTR arg2;
+ppc_elf_sort_rela (const void *arg1, const void *arg2)
 {
-  const Elf_Internal_Rela **rela1 = (const Elf_Internal_Rela**) arg1;
-  const Elf_Internal_Rela **rela2 = (const Elf_Internal_Rela**) arg2;
+  const Elf_Internal_Rela * const *rela1 = arg1;
+  const Elf_Internal_Rela * const *rela2 = arg2;
 
   /* Sort by offset.  */
   return ((*rela1)->r_offset - (*rela2)->r_offset);
 }
 
 static bfd_boolean
-ppc_elf_relax_section (abfd, isec, link_info, again)
-     bfd *abfd;
-     asection *isec;
-     struct bfd_link_info *link_info;
-     bfd_boolean *again;
+ppc_elf_relax_section (bfd *abfd,
+                      asection *isec,
+                      struct bfd_link_info *link_info,
+                      bfd_boolean *again)
 {
 #define PAGESIZE 0x1000
 
@@ -1667,7 +1653,7 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
 
   /* If needed, initialize this section's cooked size.  */
   if (isec->_cooked_size == 0)
-      isec->_cooked_size = isec->_raw_size;
+    isec->_cooked_size = isec->_raw_size;
 
   /* We're only interested in text sections which overlap the
      troublesome area at the end of a page.  */
@@ -1679,11 +1665,11 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
       /* Get the section contents.  */
       /* Get cached copy if it exists.  */
       if (elf_section_data (isec)->this_hdr.contents != NULL)
-         contents = elf_section_data (isec)->this_hdr.contents;
+       contents = elf_section_data (isec)->this_hdr.contents;
       else
        {
          /* Go get them off disk.  */
-         contents = (bfd_byte *) bfd_malloc (isec->_raw_size);
+         contents = bfd_malloc (isec->_raw_size);
          if (contents == NULL)
            goto error_return;
          free_contents = contents;
@@ -1697,55 +1683,57 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
       comb_count = 0;
       if (isec->reloc_count)
        {
-          unsigned n;
+         unsigned n;
          bfd_size_type amt;
 
-          /* Get a copy of the native relocations.  */
-          internal_relocs = _bfd_elf32_link_read_relocs (
-           abfd, isec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
-           link_info->keep_memory);
-          if (internal_relocs == NULL)
-             goto error_return;
-          if (! link_info->keep_memory)
-             free_relocs = internal_relocs;
+         /* Get a copy of the native relocations.  */
+         internal_relocs
+           = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
+                                        link_info->keep_memory);
+         if (internal_relocs == NULL)
+           goto error_return;
+         if (! link_info->keep_memory)
+           free_relocs = internal_relocs;
 
-          /* Setup a faster access method for the reloc info we need.  */
+         /* Setup a faster access method for the reloc info we need.  */
          amt = isec->reloc_count;
          amt *= sizeof (Elf_Internal_Rela*);
-          rela_comb = (Elf_Internal_Rela**) bfd_malloc (amt);
-          if (rela_comb == NULL)
-              goto error_return;
-          for (n = 0; n < isec->reloc_count; ++n)
-            {
-              long r_type;
-
-              r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
-              if (r_type < 0 || r_type >= (int) R_PPC_max)
-                  goto error_return;
-
-              /* Prologue constants are sometimes present in the ".text"
-              sections and they can be identified by their associated relocation.
-              We don't want to process those words and some others which
-              can also be identified by their relocations.  However, not all
-              conditional branches will have a relocation so we will
-              only ignore words that 1) have a reloc, and 2) the reloc
-              is not applicable to a conditional branch.
-              The array rela_comb is built here for use in the EOP scan loop.  */
-              switch (r_type)
-                {
-                case R_PPC_ADDR14_BRNTAKEN:     /* Absolute, predicted not taken */
-                case R_PPC_REL14:               /* Relative cond. br.  */
-                case R_PPC_REL14_BRNTAKEN:      /* Rel. cond. br., predicted not taken */
-                  /* We should check the instruction.  */
-                  break;
-                default:
-                  /* The word is not a conditional branch - ignore it.  */
-                  rela_comb[comb_count++] = &internal_relocs[n];
-                  break;
-                }
-            }
-          if (comb_count > 1)
-           qsort (rela_comb, (size_t) comb_count, sizeof (int), ppc_elf_sort_rela);
+         rela_comb = bfd_malloc (amt);
+         if (rela_comb == NULL)
+           goto error_return;
+         for (n = 0; n < isec->reloc_count; ++n)
+           {
+             enum elf_ppc_reloc_type r_type;
+
+             r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
+             if (r_type >= R_PPC_max)
+               goto error_return;
+
+             /* Prologue constants are sometimes present in the ".text"
+                sections and they can be identified by their associated
+                relocation.  We don't want to process those words and
+                some others which can also be identified by their
+                relocations.  However, not all conditional branches will
+                have a relocation so we will only ignore words that
+                1) have a reloc, and 2) the reloc is not applicable to a
+                conditional branch.  The array rela_comb is built here
+                for use in the EOP scan loop.  */
+             switch (r_type)
+               {
+               case R_PPC_ADDR14_BRNTAKEN:
+               case R_PPC_REL14:
+               case R_PPC_REL14_BRNTAKEN:
+                 /* We should check the instruction.  */
+                 break;
+               default:
+                 /* The word is not a conditional branch - ignore it.  */
+                 rela_comb[comb_count++] = &internal_relocs[n];
+                 break;
+               }
+           }
+         if (comb_count > 1)
+           qsort (rela_comb, (size_t) comb_count, sizeof (int),
+                  ppc_elf_sort_rela);
        }
 
       /* Enumerate each EOP region that overlaps this section.  */
@@ -1753,99 +1741,104 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
       dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
       dot -= link_info->mpc860c0;
       section_modified = FALSE;
-      if (dot < isec->vma)      /* Increment the start position if this section */
-          dot = isec->vma;      /* begins in the middle of its first EOP region.  */
+      /* Increment the start position if this section begins in the
+        middle of its first EOP region.  */
+      if (dot < isec->vma)
+       dot = isec->vma;
       for (;
-           dot < end_section;
-           dot += PAGESIZE, end_page += PAGESIZE)
-        {
-
-          /* Check each word in this EOP region.  */
-          for (; dot < end_page; dot += 4)
-            {
-              bfd_vma isec_offset;
-              unsigned long insn;
-              bfd_boolean skip, modified;
-
-              /* Don't process this word if there is a relocation for it and
-                the relocation indicates the word is not a conditional branch.  */
-              skip = FALSE;
-              isec_offset = dot - isec->vma;
-              for (; comb_curr<comb_count; ++comb_curr)
-                {
-                  bfd_vma r_offset;
-
-                  r_offset = rela_comb[comb_curr]->r_offset;
-                  if (r_offset >= isec_offset)
-                    {
-                      if (r_offset == isec_offset) skip = TRUE;
-                      break;
-                    }
-                }
-              if (skip) continue;
-
-              /* Check the current word for a problematic conditional branch.  */
+          dot < end_section;
+          dot += PAGESIZE, end_page += PAGESIZE)
+       {
+         /* Check each word in this EOP region.  */
+         for (; dot < end_page; dot += 4)
+           {
+             bfd_vma isec_offset;
+             unsigned long insn;
+             bfd_boolean skip, modified;
+
+             /* Don't process this word if there is a relocation for it
+                and the relocation indicates the word is not a
+                conditional branch.  */
+             skip = FALSE;
+             isec_offset = dot - isec->vma;
+             for (; comb_curr<comb_count; ++comb_curr)
+               {
+                 bfd_vma r_offset;
+
+                 r_offset = rela_comb[comb_curr]->r_offset;
+                 if (r_offset >= isec_offset)
+                   {
+                     if (r_offset == isec_offset) skip = TRUE;
+                     break;
+                   }
+               }
+             if (skip) continue;
+
+             /* Check the current word for a problematic conditional
+                branch.  */
 #define BO0(insn) ((insn) & 0x02000000)
 #define BO2(insn) ((insn) & 0x00800000)
 #define BO4(insn) ((insn) & 0x00200000)
-              insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
-              modified = FALSE;
-              if ((insn & 0xFc000000) == 0x40000000)
-                {
-                  /* Instruction is BCx */
-                  if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
-                    {
-                      bfd_vma target;
-                      /* This branch is predicted as "normal".
-                        If this is a forward branch, it is problematic.  */
+             insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
+             modified = FALSE;
+             if ((insn & 0xFc000000) == 0x40000000)
+               {
+                 /* Instruction is BCx */
+                 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
+                   {
+                     bfd_vma target;
 
-                      target = insn & 0x0000Fffc;               /*extract*/
-                      target = (target ^ 0x8000) - 0x8000;      /*sign extend*/
-                      if ((insn & 0x00000002) == 0)
-                          target += dot;                        /*convert to abs*/
-                      if (target > dot)
-                        {
-                          insn |= 0x00200000;   /* Set the prediction bit.  */
-                          modified = TRUE;
-                        }
-                    }
-                }
-              else if ((insn & 0xFc00Fffe) == 0x4c000420)
-                {
-                  /* Instruction is BCCTRx.  */
-                  if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
+                     /* This branch is predicted as "normal".
+                        If this is a forward branch, it is problematic.  */
+                     target = insn & 0x0000Fffc;
+                     target = (target ^ 0x8000) - 0x8000;
+                     if ((insn & 0x00000002) == 0)
+                       /* Convert to abs.  */
+                       target += dot;
+                     if (target > dot)
+                       {
+                         /* Set the prediction bit.  */
+                         insn |= 0x00200000;
+                         modified = TRUE;
+                       }
+                   }
+               }
+             else if ((insn & 0xFc00Fffe) == 0x4c000420)
+               {
+                 /* Instruction is BCCTRx.  */
+                 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
                    {
                      /* This branch is predicted as not-taken.
                         If this is a forward branch, it is problematic.
-                        Since we can't tell statically if it will branch forward,
-                        always set the prediction bit.  */
-                      insn |= 0x00200000;   /* Set the prediction bit.  */
-                      modified = TRUE;
+                        Since we can't tell statically if it will branch
+                        forward, always set the prediction bit.  */
+                     insn |= 0x00200000;
+                     modified = TRUE;
                    }
-                }
-              else if ((insn & 0xFc00Fffe) == 0x4c000020)
-                {
-                  /* Instruction is BCLRx */
-                  if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
+               }
+             else if ((insn & 0xFc00Fffe) == 0x4c000020)
+               {
+                 /* Instruction is BCLRx */
+                 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
                    {
                      /* This branch is predicted as not-taken.
                         If this is a forward branch, it is problematic.
-                        Since we can't tell statically if it will branch forward,
-                        always set the prediction bit.  */
-                      insn |= 0x00200000;   /* Set the prediction bit.  */
-                      modified = TRUE;
+                        Since we can't tell statically if it will branch
+                        forward, always set the prediction bit.  */
+                     insn |= 0x00200000;
+                     modified = TRUE;
                    }
-                }
+               }
 #undef BO0
 #undef BO2
 #undef BO4
-              if (modified)
-               {
-                  bfd_put_32 (abfd, (bfd_vma) insn, contents + isec_offset);
+             if (modified)
+               {
+                 bfd_put_32 (abfd, insn, contents + isec_offset);
                  section_modified = TRUE;
-               }
-            }
-        }
+               }
+           }
+       }
       if (section_modified)
        {
          elf_section_data (isec)->this_hdr.contents = contents;
@@ -1879,7 +1872,7 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
 
   return TRUE;
 
-error_return:
+ error_return:
   if (rela_comb != NULL)
     free (rela_comb);
   if (free_relocs != NULL)
@@ -1890,20 +1883,19 @@ error_return:
 }
 \f
 static reloc_howto_type *
-ppc_elf_reloc_type_lookup (abfd, code)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     bfd_reloc_code_real_type code;
+ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
+                          bfd_reloc_code_real_type code)
 {
   enum elf_ppc_reloc_type r;
 
+  /* Initialize howto table if not already done.  */
   if (!ppc_elf_howto_table[R_PPC_ADDR32])
-    /* Initialize howto table if needed.  */
     ppc_elf_howto_init ();
 
-  switch ((int) code)
+  switch (code)
     {
     default:
-      return (reloc_howto_type *) NULL;
+      return NULL;
 
     case BFD_RELOC_NONE:               r = R_PPC_NONE;                 break;
     case BFD_RELOC_32:                 r = R_PPC_ADDR32;               break;
@@ -1988,19 +1980,18 @@ ppc_elf_reloc_type_lookup (abfd, code)
     case BFD_RELOC_VTABLE_ENTRY:       r = R_PPC_GNU_VTENTRY;          break;
     }
 
-  return ppc_elf_howto_table[(int) r];
+  return ppc_elf_howto_table[r];
 };
 
 /* Set the howto pointer for a PowerPC ELF reloc.  */
 
 static void
-ppc_elf_info_to_howto (abfd, cache_ptr, dst)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     arelent *cache_ptr;
-     Elf_Internal_Rela *dst;
+ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
+                      arelent *cache_ptr,
+                      Elf_Internal_Rela *dst)
 {
+  /* Initialize howto table if not already done.  */
   if (!ppc_elf_howto_table[R_PPC_ADDR32])
-    /* Initialize howto table if needed.  */
     ppc_elf_howto_init ();
 
   BFD_ASSERT (ELF32_R_TYPE (dst->r_info) < (unsigned int) R_PPC_max);
@@ -2010,15 +2001,13 @@ ppc_elf_info_to_howto (abfd, cache_ptr, dst)
 /* Handle the R_PPC_ADDR16_HA reloc.  */
 
 static bfd_reloc_status_type
-ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
-                        output_bfd, error_message)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     arelent *reloc_entry;
-     asymbol *symbol;
-     PTR data ATTRIBUTE_UNUSED;
-     asection *input_section;
-     bfd *output_bfd;
-     char **error_message ATTRIBUTE_UNUSED;
+ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
+                        arelent *reloc_entry,
+                        asymbol *symbol,
+                        void *data ATTRIBUTE_UNUSED,
+                        asection *input_section,
+                        bfd *output_bfd,
+                        char **error_message ATTRIBUTE_UNUSED)
 {
   bfd_vma relocation;
 
@@ -2046,15 +2035,13 @@ ppc_elf_addr16_ha_reloc (abfd, reloc_entry, symbol, data, input_section,
 }
 
 static bfd_reloc_status_type
-ppc_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
-                        input_section, output_bfd, error_message)
-     bfd *abfd;
-     arelent *reloc_entry;
-     asymbol *symbol;
-     PTR data;
-     asection *input_section;
-     bfd *output_bfd;
-     char **error_message;
+ppc_elf_unhandled_reloc (bfd *abfd,
+                        arelent *reloc_entry,
+                        asymbol *symbol,
+                        void *data,
+                        asection *input_section,
+                        bfd *output_bfd,
+                        char **error_message)
 {
   /* If this is a relocatable link (output_bfd test tells us), just
      call the generic function.  Any adjustment will be done at final
@@ -2066,7 +2053,7 @@ ppc_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
   if (error_message != NULL)
     {
       static char buf[60];
-      sprintf (buf, "generic linker can't handle %s",
+      sprintf (buf, _("generic linker can't handle %s"),
               reloc_entry->howto->name);
       *error_message = buf;
     }
@@ -2077,8 +2064,7 @@ ppc_elf_unhandled_reloc (abfd, reloc_entry, symbol, data,
    default is 64 bit.  */
 
 static bfd_boolean
-ppc_elf_object_p (abfd)
-     bfd *abfd;
+ppc_elf_object_p (bfd *abfd)
 {
   if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 64)
     {
@@ -2097,9 +2083,7 @@ ppc_elf_object_p (abfd)
 /* Function to set whether a module needs the -mrelocatable bit set.  */
 
 static bfd_boolean
-ppc_elf_set_private_flags (abfd, flags)
-     bfd *abfd;
-     flagword flags;
+ppc_elf_set_private_flags (bfd *abfd, flagword flags)
 {
   BFD_ASSERT (!elf_flags_init (abfd)
              || elf_elfheader (abfd)->e_flags == flags);
@@ -2113,9 +2097,7 @@ ppc_elf_set_private_flags (abfd, flags)
    object file when linking.  */
 
 static bfd_boolean
-ppc_elf_merge_private_bfd_data (ibfd, obfd)
-     bfd *ibfd;
-     bfd *obfd;
+ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
 {
   flagword old_flags;
   flagword new_flags;
@@ -2131,26 +2113,30 @@ ppc_elf_merge_private_bfd_data (ibfd, obfd)
 
   new_flags = elf_elfheader (ibfd)->e_flags;
   old_flags = elf_elfheader (obfd)->e_flags;
-  if (!elf_flags_init (obfd))  /* First call, no flags set.  */
+  if (!elf_flags_init (obfd))
     {
+      /* First call, no flags set.  */
       elf_flags_init (obfd) = TRUE;
       elf_elfheader (obfd)->e_flags = new_flags;
     }
 
-  else if (new_flags == old_flags)     /* Compatible flags are ok.  */
+  /* Compatible flags are ok.  */
+  else if (new_flags == old_flags)
     ;
 
-  else                                 /* Incompatible flags.  */
+  /* Incompatible flags.  */
+  else
     {
-      /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib to be linked
-         with either.  */
+      /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib
+        to be linked with either.  */
       error = FALSE;
       if ((new_flags & EF_PPC_RELOCATABLE) != 0
          && (old_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0)
        {
          error = TRUE;
          (*_bfd_error_handler)
-           (_("%s: compiled with -mrelocatable and linked with modules compiled normally"),
+           (_("%s: compiled with -mrelocatable and linked with "
+              "modules compiled normally"),
             bfd_archive_filename (ibfd));
        }
       else if ((new_flags & (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB)) == 0
@@ -2158,7 +2144,8 @@ ppc_elf_merge_private_bfd_data (ibfd, obfd)
        {
          error = TRUE;
          (*_bfd_error_handler)
-           (_("%s: compiled normally and linked with modules compiled with -mrelocatable"),
+           (_("%s: compiled normally and linked with "
+              "modules compiled with -mrelocatable"),
             bfd_archive_filename (ibfd));
        }
 
@@ -2167,24 +2154,26 @@ ppc_elf_merge_private_bfd_data (ibfd, obfd)
        elf_elfheader (obfd)->e_flags &= ~EF_PPC_RELOCATABLE_LIB;
 
       /* The output is -mrelocatable iff it can't be -mrelocatable-lib,
-         but each input file is either -mrelocatable or -mrelocatable-lib.  */
+        but each input file is either -mrelocatable or -mrelocatable-lib.  */
       if (! (elf_elfheader (obfd)->e_flags & EF_PPC_RELOCATABLE_LIB)
          && (new_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE))
          && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
        elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
 
-      /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it.  */
+      /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if
+        any module uses it.  */
       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
 
-      new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
-      old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
+      new_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
+      old_flags &= ~(EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
 
       /* Warn about any other mismatches.  */
       if (new_flags != old_flags)
        {
          error = TRUE;
          (*_bfd_error_handler)
-           (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
+           (_("%s: uses different e_flags (0x%lx) fields "
+              "than previous modules (0x%lx)"),
             bfd_archive_filename (ibfd), (long) new_flags, (long) old_flags);
        }
 
@@ -2202,10 +2191,7 @@ ppc_elf_merge_private_bfd_data (ibfd, obfd)
    is called when elfcode.h finds a section with an unknown type.  */
 
 static bfd_boolean
-ppc_elf_section_from_shdr (abfd, hdr, name)
-     bfd *abfd;
-     Elf_Internal_Shdr *hdr;
-     const char *name;
+ppc_elf_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
 {
   asection *newsect;
   flagword flags;
@@ -2228,10 +2214,9 @@ ppc_elf_section_from_shdr (abfd, hdr, name)
 /* Set up any other section flags and such that may be necessary.  */
 
 static bfd_boolean
-ppc_elf_fake_sections (abfd, shdr, asect)
-     bfd *abfd ATTRIBUTE_UNUSED;
-     Elf_Internal_Shdr *shdr;
-     asection *asect;
+ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
+                      Elf_Internal_Shdr *shdr,
+                      asection *asect)
 {
   if ((asect->flags & SEC_EXCLUDE) != 0)
     shdr->sh_flags |= SHF_EXCLUDE;
@@ -2242,72 +2227,359 @@ ppc_elf_fake_sections (abfd, shdr, asect)
   return TRUE;
 }
 \f
+/* Find a linker generated pointer with a given addend and type.  */
+
+static elf_linker_section_pointers_t *
+elf_find_pointer_linker_section
+  (elf_linker_section_pointers_t *linker_pointers,
+   bfd_vma addend,
+   elf_linker_section_t *lsect)
+{
+  for ( ; linker_pointers != NULL; linker_pointers = linker_pointers->next)
+    if (lsect == linker_pointers->lsect && addend == linker_pointers->addend)
+      return linker_pointers;
+
+  return NULL;
+}
+\f
+/* Allocate a pointer to live in a linker created section.  */
+
+static bfd_boolean
+elf_create_pointer_linker_section (bfd *abfd,
+                                  struct bfd_link_info *info,
+                                  elf_linker_section_t *lsect,
+                                  struct elf_link_hash_entry *h,
+                                  const Elf_Internal_Rela *rel)
+{
+  elf_linker_section_pointers_t **ptr_linker_section_ptr = NULL;
+  elf_linker_section_pointers_t *linker_section_ptr;
+  unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
+  bfd_size_type amt;
+
+  BFD_ASSERT (lsect != NULL);
+
+  /* Is this a global symbol?  */
+  if (h != NULL)
+    {
+      struct ppc_elf_link_hash_entry *eh;
+
+      /* Has this symbol already been allocated?  If so, our work is done.  */
+      eh = (struct ppc_elf_link_hash_entry *) h;
+      if (elf_find_pointer_linker_section (eh->linker_section_pointer,
+                                          rel->r_addend,
+                                          lsect))
+       return TRUE;
+
+      ptr_linker_section_ptr = &eh->linker_section_pointer;
+      /* Make sure this symbol is output as a dynamic symbol.  */
+      if (h->dynindx == -1)
+       {
+         if (! _bfd_elf_link_record_dynamic_symbol (info, h))
+           return FALSE;
+       }
+
+      if (lsect->rel_section)
+       lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
+    }
+  else
+    {
+      /* Allocation of a pointer to a local symbol.  */
+      elf_linker_section_pointers_t **ptr = elf_local_ptr_offsets (abfd);
+
+      /* Allocate a table to hold the local symbols if first time.  */
+      if (!ptr)
+       {
+         unsigned int num_symbols = elf_tdata (abfd)->symtab_hdr.sh_info;
+
+         amt = num_symbols;
+         amt *= sizeof (elf_linker_section_pointers_t *);
+         ptr = bfd_zalloc (abfd, amt);
+
+         if (!ptr)
+           return FALSE;
+
+         elf_local_ptr_offsets (abfd) = ptr;
+       }
+
+      /* Has this symbol already been allocated?  If so, our work is done.  */
+      if (elf_find_pointer_linker_section (ptr[r_symndx],
+                                          rel->r_addend,
+                                          lsect))
+       return TRUE;
+
+      ptr_linker_section_ptr = &ptr[r_symndx];
+
+      if (info->shared)
+       {
+         /* If we are generating a shared object, we need to
+            output a R_<xxx>_RELATIVE reloc so that the
+            dynamic linker can adjust this GOT entry.  */
+         BFD_ASSERT (lsect->rel_section != NULL);
+         lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
+       }
+    }
+
+  /* Allocate space for a pointer in the linker section, and allocate
+     a new pointer record from internal memory.  */
+  BFD_ASSERT (ptr_linker_section_ptr != NULL);
+  amt = sizeof (elf_linker_section_pointers_t);
+  linker_section_ptr = bfd_alloc (abfd, amt);
+
+  if (!linker_section_ptr)
+    return FALSE;
+
+  linker_section_ptr->next = *ptr_linker_section_ptr;
+  linker_section_ptr->addend = rel->r_addend;
+  linker_section_ptr->lsect = lsect;
+  linker_section_ptr->written_address_p = FALSE;
+  *ptr_linker_section_ptr = linker_section_ptr;
+
+  linker_section_ptr->offset = lsect->section->_raw_size;
+  lsect->section->_raw_size += 4;
+
+#ifdef DEBUG
+  fprintf (stderr,
+          "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
+          lsect->name, (long) linker_section_ptr->offset,
+          (long) lsect->section->_raw_size);
+#endif
+
+  return TRUE;
+}
+\f
+#define bfd_put_ptr(BFD, VAL, ADDR) bfd_put_32 (BFD, VAL, ADDR)
+
+/* Fill in the address for a pointer generated in a linker section.  */
+
+static bfd_vma
+elf_finish_pointer_linker_section (bfd *output_bfd,
+                                  bfd *input_bfd,
+                                  struct bfd_link_info *info,
+                                  elf_linker_section_t *lsect,
+                                  struct elf_link_hash_entry *h,
+                                  bfd_vma relocation,
+                                  const Elf_Internal_Rela *rel,
+                                  int relative_reloc)
+{
+  elf_linker_section_pointers_t *linker_section_ptr;
+
+  BFD_ASSERT (lsect != NULL);
+
+  if (h != NULL)
+    {
+      /* Handle global symbol.  */
+      struct ppc_elf_link_hash_entry *eh;
+
+      eh = (struct ppc_elf_link_hash_entry *) h;
+      linker_section_ptr
+       = elf_find_pointer_linker_section (eh->linker_section_pointer,
+                                          rel->r_addend,
+                                          lsect);
+
+      BFD_ASSERT (linker_section_ptr != NULL);
+
+      if (! elf_hash_table (info)->dynamic_sections_created
+         || (info->shared
+             && info->symbolic
+             && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
+       {
+         /* This is actually a static link, or it is a
+            -Bsymbolic link and the symbol is defined
+            locally.  We must initialize this entry in the
+            global section.
+
+            When doing a dynamic link, we create a .rela.<xxx>
+            relocation entry to initialize the value.  This
+            is done in the finish_dynamic_symbol routine.  */
+         if (!linker_section_ptr->written_address_p)
+           {
+             linker_section_ptr->written_address_p = TRUE;
+             bfd_put_ptr (output_bfd,
+                          relocation + linker_section_ptr->addend,
+                          (lsect->section->contents
+                           + linker_section_ptr->offset));
+           }
+       }
+    }
+  else
+    {
+      /* Handle local symbol.  */
+      unsigned long r_symndx = ELF32_R_SYM (rel->r_info);
+      BFD_ASSERT (elf_local_ptr_offsets (input_bfd) != NULL);
+      BFD_ASSERT (elf_local_ptr_offsets (input_bfd)[r_symndx] != NULL);
+      linker_section_ptr = (elf_find_pointer_linker_section
+                           (elf_local_ptr_offsets (input_bfd)[r_symndx],
+                            rel->r_addend,
+                            lsect));
+
+      BFD_ASSERT (linker_section_ptr != NULL);
+
+      /* Write out pointer if it hasn't been rewritten out before.  */
+      if (!linker_section_ptr->written_address_p)
+       {
+         linker_section_ptr->written_address_p = TRUE;
+         bfd_put_ptr (output_bfd, relocation + linker_section_ptr->addend,
+                      lsect->section->contents + linker_section_ptr->offset);
+
+         if (info->shared)
+           {
+             /* We need to generate a relative reloc for the dynamic
+                linker.  */
+
+             asection *srel = lsect->rel_section;
+             Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
+             bfd_byte *erel;
+             struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
+             unsigned int i;
+
+             BFD_ASSERT (srel != NULL);
+
+             for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
+               {
+                 outrel[i].r_offset = (lsect->section->output_section->vma
+                                       + lsect->section->output_offset
+                                       + linker_section_ptr->offset);
+                 outrel[i].r_info = 0;
+                 outrel[i].r_addend = 0;
+               }
+             outrel[0].r_info = ELF32_R_INFO (0, relative_reloc);
+             erel = lsect->section->contents;
+             erel += (elf_section_data (lsect->section)->rel_count++
+                      * sizeof (Elf32_External_Rela));
+             bfd_elf32_swap_reloca_out (output_bfd, outrel, erel);
+           }
+       }
+    }
+
+  relocation = (lsect->section->output_offset
+               + linker_section_ptr->offset
+               - lsect->sym_offset);
+
+#ifdef DEBUG
+  fprintf (stderr,
+          "Finish pointer in linker section %s, offset = %ld (0x%lx)\n",
+          lsect->name, (long) relocation, (long) relocation);
+#endif
+
+  /* Subtract out the addend, because it will get added back in by the normal
+     processing.  */
+  return relocation - linker_section_ptr->addend;
+}
+\f
 /* Create a special linker section */
 static elf_linker_section_t *
-ppc_elf_create_linker_section (abfd, info, which)
-     bfd *abfd;
-     struct bfd_link_info *info;
-     enum elf_linker_section_enum which;
+ppc_elf_create_linker_section (bfd *abfd,
+                              struct bfd_link_info *info,
+                              enum elf_linker_section_enum which)
 {
-  bfd *dynobj = elf_hash_table (info)->dynobj;
   elf_linker_section_t *lsect;
+  struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
+  asection *s;
+  bfd_size_type amt;
+  flagword flags;
+  const char *name;
+  const char *rel_name;
+  const char *sym_name;
+  bfd_vma sym_offset;
+
+  /* Both of these sections are (technically) created by the user
+     putting data in them, so they shouldn't be marked
+     SEC_LINKER_CREATED.
+
+     The linker creates them so it has somewhere to attach their
+     respective symbols. In fact, if they were empty it would
+     be OK to leave the symbol set to 0 (or any random number), because
+     the appropriate register should never be used.  */
+  flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY;
+  sym_offset = 32768;
+
+  switch (which)
+    {
+    default:
+      abort ();
+      return NULL;
+
+    case LINKER_SECTION_SDATA: /* .sdata/.sbss section */
+      name     = ".sdata";
+      rel_name = ".rela.sdata";
+      sym_name = "_SDA_BASE_";
+      break;
+
+    case LINKER_SECTION_SDATA2:        /* .sdata2/.sbss2 section */
+      name     = ".sdata2";
+      rel_name = ".rela.sdata2";
+      sym_name = "_SDA2_BASE_";
+      flags    |= SEC_READONLY;
+      break;
+    }
+
+  /* Record the first bfd that needs the special sections.  */
+  if (!htab->elf.dynobj)
+    htab->elf.dynobj = abfd;
 
-  /* Record the first bfd section that needs the special section.  */
-  if (!dynobj)
-    dynobj = elf_hash_table (info)->dynobj = abfd;
+  amt = sizeof (elf_linker_section_t);
+  lsect = bfd_zalloc (htab->elf.dynobj, amt);
 
-  /* If this is the first time, create the section.  */
-  lsect = elf_linker_section (dynobj, which);
-  if (!lsect)
+  lsect->sym_offset = sym_offset;
+
+  /* See if the sections already exist.  */
+  s = bfd_get_section_by_name (htab->elf.dynobj, name);
+  if (s == NULL || (s->flags & flags) != flags)
     {
-      elf_linker_section_t defaults;
-      static elf_linker_section_t zero_section;
-
-      defaults = zero_section;
-      defaults.which = which;
-      defaults.hole_written_p = FALSE;
-      defaults.alignment = 2;
-
-      /* Both of these sections are (technically) created by the user
-        putting data in them, so they shouldn't be marked
-        SEC_LINKER_CREATED.
-
-        The linker creates them so it has somewhere to attach their
-        respective symbols. In fact, if they were empty it would
-        be OK to leave the symbol set to 0 (or any random number), because
-        the appropriate register should never be used.  */
-      defaults.flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
-                       | SEC_IN_MEMORY);
-
-      switch (which)
-       {
-       default:
-         (*_bfd_error_handler) (_("%s: Unknown special linker type %d"),
-                                bfd_get_filename (abfd),
-                                (int) which);
+      s = bfd_make_section_anyway (htab->elf.dynobj, name);
+      if (s == NULL
+         || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
+       return NULL;
+    }
+  lsect->section = s;
 
-         bfd_set_error (bfd_error_bad_value);
-         return (elf_linker_section_t *) 0;
-
-       case LINKER_SECTION_SDATA:      /* .sdata/.sbss section */
-         defaults.name           = ".sdata";
-         defaults.rel_name       = ".rela.sdata";
-         defaults.bss_name       = ".sbss";
-         defaults.sym_name       = "_SDA_BASE_";
-         defaults.sym_offset     = 32768;
-         break;
+  if (bfd_get_section_alignment (htab->elf.dynobj, s) < 2
+      && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
+    return NULL;
 
-       case LINKER_SECTION_SDATA2:     /* .sdata2/.sbss2 section */
-         defaults.name           = ".sdata2";
-         defaults.rel_name       = ".rela.sdata2";
-         defaults.bss_name       = ".sbss2";
-         defaults.sym_name       = "_SDA2_BASE_";
-         defaults.sym_offset     = 32768;
-         defaults.flags         |= SEC_READONLY;
-         break;
-       }
+  s->_raw_size = align_power (s->_raw_size, 2);
+
+#ifdef DEBUG
+  fprintf (stderr, "Creating section %s, current size = %ld\n",
+          name, (long) s->_raw_size);
+#endif
 
-      lsect = _bfd_elf_create_linker_section (abfd, info, which, &defaults);
+  if (sym_name)
+    {
+      struct elf_link_hash_entry *h;
+      struct bfd_link_hash_entry *bh;
+
+#ifdef DEBUG
+      fprintf (stderr, "Adding %s to section %s\n", sym_name, name);
+#endif
+      bh = bfd_link_hash_lookup (info->hash, sym_name,
+                                FALSE, FALSE, FALSE);
+
+      if ((bh == NULL || bh->type == bfd_link_hash_undefined)
+         && !(_bfd_generic_link_add_one_symbol
+              (info, abfd, sym_name, BSF_GLOBAL, s, sym_offset, NULL,
+               FALSE, get_elf_backend_data (abfd)->collect, &bh)))
+       return NULL;
+      h = (struct elf_link_hash_entry *) bh;
+
+      h->type = STT_OBJECT;
+      lsect->sym_hash = h;
+
+      if (info->shared
+         && ! _bfd_elf_link_record_dynamic_symbol (info, h))
+       return NULL;
+    }
+
+  if (info->shared)
+    {
+      s = bfd_make_section_anyway (htab->elf.dynobj, rel_name);
+      lsect->rel_section = s;
+      flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
+              | SEC_LINKER_CREATED | SEC_READONLY);
+      if (s == NULL
+         || ! bfd_set_section_flags (htab->elf.dynobj, s, flags)
+         || ! bfd_set_section_alignment (htab->elf.dynobj, s, 2))
+       return NULL;
     }
 
   return lsect;
@@ -2317,8 +2589,7 @@ ppc_elf_create_linker_section (abfd, info, which)
    need to bump up the number of section headers.  */
 
 static int
-ppc_elf_additional_program_headers (abfd)
-     bfd *abfd;
+ppc_elf_additional_program_headers (bfd *abfd)
 {
   asection *s;
   int ret;
@@ -2343,8 +2614,7 @@ ppc_elf_additional_program_headers (abfd)
 /* Modify the segment map if needed.  */
 
 static bfd_boolean
-ppc_elf_modify_segment_map (abfd)
-     bfd *abfd ATTRIBUTE_UNUSED;
+ppc_elf_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED)
 {
   return TRUE;
 }
@@ -2352,9 +2622,7 @@ ppc_elf_modify_segment_map (abfd)
 /* The powerpc .got has a blrl instruction in it.  Mark it executable.  */
 
 static bfd_boolean
-ppc_elf_create_got (abfd, info)
-     bfd *abfd;
-     struct bfd_link_info *info;
+ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
 {
   struct ppc_elf_link_hash_table *htab;
   asection *s;
@@ -2390,15 +2658,16 @@ ppc_elf_create_got (abfd, info)
    to create .dynbss and .rela.bss).  */
 
 static bfd_boolean
-ppc_elf_create_dynamic_sections (abfd, info)
-     bfd *abfd;
-     struct bfd_link_info *info;
+ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
 {
   struct ppc_elf_link_hash_table *htab;
   asection *s;
   flagword flags;
 
-  if (!ppc_elf_create_got (abfd, info))
+  htab = ppc_elf_hash_table (info);
+
+  if (htab->got == NULL
+      && !ppc_elf_create_got (abfd, info))
     return FALSE;
 
   if (!_bfd_elf_create_dynamic_sections (abfd, info))
@@ -2407,7 +2676,6 @@ ppc_elf_create_dynamic_sections (abfd, info)
   flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
           | SEC_LINKER_CREATED);
 
-  htab = ppc_elf_hash_table (info);
   htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
   htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
   if (s == NULL
@@ -2440,16 +2708,16 @@ ppc_elf_create_dynamic_sections (abfd, info)
    understand.  */
 
 static bfd_boolean
-ppc_elf_adjust_dynamic_symbol (info, h)
-     struct bfd_link_info *info;
-     struct elf_link_hash_entry *h;
+ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
+                              struct elf_link_hash_entry *h)
 {
   struct ppc_elf_link_hash_table *htab;
   asection *s;
   unsigned int power_of_two;
 
 #ifdef DEBUG
-  fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n", h->root.root.string);
+  fprintf (stderr, "ppc_elf_adjust_dynamic_symbol called for %s\n",
+          h->root.root.string);
 #endif
 
   /* Make sure we know what is going on here.  */
@@ -2470,19 +2738,21 @@ ppc_elf_adjust_dynamic_symbol (info, h)
     {
       /* Clear procedure linkage table information for any symbol that
         won't need a .plt entry.  */
-      if (! htab->elf.dynamic_sections_created
-         || SYMBOL_CALLS_LOCAL (info, h)
-         || h->plt.refcount <= 0)
+      if (h->plt.refcount <= 0
+         || SYMBOL_CALLS_LOCAL (info, h)
+         || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
+             && h->root.type == bfd_link_hash_undefweak))
        {
          /* A PLT entry is not required/allowed when:
 
             1. We are not using ld.so; because then the PLT entry
-            can't be set up, so we can't use one.
+            can't be set up, so we can't use one.  In this case,
+            ppc_elf_adjust_dynamic_symbol won't even be called.
 
-            2. We know for certain that a call to this symbol
-            will go to this object.
+            2. GC has rendered the entry unused.
 
-            3. GC has rendered the entry unused.  */
+            3. We know for certain that a call to this symbol
+            will go to this object, or will remain undefined.  */
          h->plt.offset = (bfd_vma) -1;
          h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
        }
@@ -2500,6 +2770,10 @@ ppc_elf_adjust_dynamic_symbol (info, h)
                  || h->weakdef->root.type == bfd_link_hash_defweak);
       h->root.u.def.section = h->weakdef->root.u.def.section;
       h->root.u.def.value = h->weakdef->root.u.def.value;
+      if (ELIMINATE_COPY_RELOCS)
+       h->elf_link_hash_flags
+         = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
+            | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
       return TRUE;
     }
 
@@ -2518,6 +2792,25 @@ ppc_elf_adjust_dynamic_symbol (info, h)
   if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
     return TRUE;
 
+  if (ELIMINATE_COPY_RELOCS)
+    {
+      struct ppc_elf_dyn_relocs *p;
+      for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
+       {
+         s = p->sec->output_section;
+         if (s != NULL && (s->flags & SEC_READONLY) != 0)
+           break;
+       }
+
+      /* If we didn't find any dynamic relocs in read-only sections, then
+        we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
+      if (p == NULL)
+       {
+         h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
+         return TRUE;
+       }
+    }
+
   /* 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
@@ -2584,21 +2877,30 @@ ppc_elf_adjust_dynamic_symbol (info, h)
    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 relocate_section.  */
-#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
+#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
   ((DYN)                                                               \
-   && ((INFO)->shared                                                  \
+   && ((SHARED)                                                                \
        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)     \
    && ((H)->dynindx != -1                                              \
        || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
 
+/* Of those relocs that might be copied as dynamic relocs, this macro
+   selects those that must be copied when linking a shared library,
+   even when the symbol is local.  */
+
+#define MUST_BE_DYN_RELOC(RTYPE)               \
+  ((RTYPE) != R_PPC_REL24                      \
+   && (RTYPE) != R_PPC_REL14                   \
+   && (RTYPE) != R_PPC_REL14_BRTAKEN           \
+   && (RTYPE) != R_PPC_REL14_BRNTAKEN          \
+   && (RTYPE) != R_PPC_REL32)
+
 /* Allocate space in associated reloc sections for dynamic relocs.  */
 
 static bfd_boolean
-allocate_dynrelocs (h, inf)
-     struct elf_link_hash_entry *h;
-     PTR inf;
+allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 {
-  struct bfd_link_info *info = (struct bfd_link_info *) inf;
+  struct bfd_link_info *info = inf;
   struct ppc_elf_link_hash_entry *eh;
   struct ppc_elf_link_hash_table *htab;
   struct ppc_elf_dyn_relocs *p;
@@ -2624,7 +2926,8 @@ allocate_dynrelocs (h, inf)
            return FALSE;
        }
 
-      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
+      if (info->shared
+         || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
        {
          asection *s = htab->plt;
 
@@ -2657,7 +2960,7 @@ allocate_dynrelocs (h, inf)
             for two entries is allocated.  */
          s->_raw_size += PLT_ENTRY_SIZE;
          if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
-             >= PLT_NUM_SINGLE_ENTRIES)
+             > PLT_NUM_SINGLE_ENTRIES)
            s->_raw_size += PLT_ENTRY_SIZE;
 
          /* We also need to make an entry in the .rela.plt section.  */
@@ -2696,7 +2999,9 @@ allocate_dynrelocs (h, inf)
          eh->elf.got.offset = htab->got->_raw_size;
          if ((eh->tls_mask & TLS_TLS) != 0)
            {
-             if ((eh->tls_mask & (TLS_GD | TLS_LD)) != 0)
+             if ((eh->tls_mask & TLS_LD) != 0)
+               htab->got->_raw_size += 8;
+             if ((eh->tls_mask & TLS_GD) != 0)
                htab->got->_raw_size += 8;
              if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
                htab->got->_raw_size += 4;
@@ -2706,7 +3011,10 @@ allocate_dynrelocs (h, inf)
          else
            htab->got->_raw_size += 4;
          dyn = htab->elf.dynamic_sections_created;
-         if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, &eh->elf))
+         if ((info->shared
+              || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
+             && (ELF_ST_VISIBILITY (eh->elf.other) == STV_DEFAULT
+                 || eh->elf.root.type != bfd_link_hash_undefweak))
            {
              /* All the entries we allocated need relocs.  */
              htab->relgot->_raw_size
@@ -2721,6 +3029,75 @@ allocate_dynrelocs (h, inf)
   else
     eh->elf.got.offset = (bfd_vma) -1;
 
+  if (eh->dyn_relocs == NULL)
+    return TRUE;
+
+  /* In the shared -Bsymbolic case, discard space allocated for
+     dynamic pc-relative relocs against symbols which turn out to be
+     defined in regular objects.  For the normal shared case, discard
+     space for relocs that have become local due to symbol visibility
+     changes.  */
+
+  if (info->shared)
+    {
+      /* Relocs that use pc_count are those that appear on a call insn,
+        or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
+        generated via assembly.  We want calls to protected symbols to
+        resolve directly to the function rather than going via the plt.
+        If people want function pointer comparisons to work as expected
+        then they should avoid writing weird assembly.  */
+      if (SYMBOL_CALLS_LOCAL (info, h))
+       {
+         struct ppc_elf_dyn_relocs **pp;
+
+         for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
+           {
+             p->count -= p->pc_count;
+             p->pc_count = 0;
+             if (p->count == 0)
+               *pp = p->next;
+             else
+               pp = &p->next;
+           }
+       }
+
+      /* Also discard relocs on undefined weak syms with non-default
+        visibility.  */
+      if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
+         && h->root.type == bfd_link_hash_undefweak)
+       eh->dyn_relocs = NULL;
+    }
+  else if (ELIMINATE_COPY_RELOCS)
+    {
+      /* For the non-shared case, discard space for relocs against
+        symbols which turn out to need copy relocs or are not
+        dynamic.  */
+
+      if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
+         && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
+         && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
+       {
+         /* Make sure this symbol is output as a dynamic symbol.
+            Undefined weak syms won't yet be marked as dynamic.  */
+         if (h->dynindx == -1
+             && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
+           {
+             if (! bfd_elf64_link_record_dynamic_symbol (info, h))
+               return FALSE;
+           }
+
+         /* If that succeeded, we know we'll be keeping all the
+            relocs.  */
+         if (h->dynindx != -1)
+           goto keep;
+       }
+
+      eh->dyn_relocs = NULL;
+
+    keep: ;
+    }
+
+  /* Finally, allocate space.  */
   for (p = eh->dyn_relocs; p != NULL; p = p->next)
     {
       asection *sreloc = elf_section_data (p->sec)->sreloc;
@@ -2733,9 +3110,7 @@ allocate_dynrelocs (h, inf)
 /* Find any dynamic relocs that apply to read-only sections.  */
 
 static bfd_boolean
-readonly_dynrelocs (h, info)
-     struct elf_link_hash_entry *h;
-     PTR info;
+readonly_dynrelocs (struct elf_link_hash_entry *h, void *info)
 {
   struct ppc_elf_dyn_relocs *p;
 
@@ -2765,9 +3140,8 @@ readonly_dynrelocs (h, info)
 /* Set the sizes of the dynamic sections.  */
 
 static bfd_boolean
-ppc_elf_size_dynamic_sections (output_bfd, info)
-     bfd *output_bfd ATTRIBUTE_UNUSED;
-     struct bfd_link_info *info;
+ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
+                              struct bfd_link_info *info)
 {
   struct ppc_elf_link_hash_table *htab;
   asection *s;
@@ -2784,7 +3158,7 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
   if (elf_hash_table (info)->dynamic_sections_created)
     {
       /* Set the contents of the .interp section to the interpreter.  */
-      if (! info->shared)
+      if (info->executable)
        {
          s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
          BFD_ASSERT (s != NULL);
@@ -2822,9 +3196,9 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
          struct ppc_elf_dyn_relocs *p;
 
          for (p = ((struct ppc_elf_dyn_relocs *)
-                  elf_section_data (s)->local_dynrel);
-             p != NULL;
-             p = p->next)
+                   elf_section_data (s)->local_dynrel);
+              p != NULL;
+              p = p->next)
            {
              if (!bfd_is_abs_section (p->sec)
                  && bfd_is_abs_section (p->sec->output_section))
@@ -2951,7 +3325,7 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
        }
 
       /* Allocate memory for the section contents.  */
-      s->contents = (bfd_byte *) bfd_zalloc (htab->elf.dynobj, s->_raw_size);
+      s->contents = bfd_zalloc (htab->elf.dynobj, s->_raw_size);
       if (s->contents == NULL)
        return FALSE;
     }
@@ -2964,9 +3338,9 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
         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_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
+  bfd_elf32_add_dynamic_entry (info, (TAG), (VAL))
 
-      if (!info->shared)
+      if (info->executable)
        {
          if (!add_dynamic_entry (DT_DEBUG, 0))
            return FALSE;
@@ -2993,7 +3367,7 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
         need a DT_TEXTREL entry.  */
       if ((info->flags & DF_TEXTREL) == 0)
        elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
-                               (PTR) info);
+                               info);
 
       if ((info->flags & DF_TEXTREL) != 0)
        {
@@ -3007,11 +3381,10 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
 }
 \f
 static bfd_boolean
-update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type)
-     bfd *abfd;
-     Elf_Internal_Shdr *symtab_hdr;
-     unsigned long r_symndx;
-     int tls_type;
+update_local_sym_info (bfd *abfd,
+                      Elf_Internal_Shdr *symtab_hdr,
+                      unsigned long r_symndx,
+                      int tls_type)
 {
   bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (abfd);
   char *local_got_tls_masks;
@@ -3021,7 +3394,7 @@ update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type)
       bfd_size_type size = symtab_hdr->sh_info;
 
       size *= sizeof (*local_got_refcounts) + sizeof (*local_got_tls_masks);
-      local_got_refcounts = (bfd_signed_vma *) bfd_zalloc (abfd, size);
+      local_got_refcounts = bfd_zalloc (abfd, size);
       if (local_got_refcounts == NULL)
        return FALSE;
       elf_local_got_refcounts (abfd) = local_got_refcounts;
@@ -3034,14 +3407,12 @@ update_local_sym_info (abfd, symtab_hdr, r_symndx, tls_type)
 }
 
 static void
-bad_shared_reloc (abfd, r_type)
-     bfd *abfd;
-     enum elf_ppc_reloc_type r_type;
+bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
 {
   (*_bfd_error_handler)
     (_("%s: relocation %s cannot be used when making a shared object"),
      bfd_archive_filename (abfd),
-     ppc_elf_howto_table[(int) r_type]->name);
+     ppc_elf_howto_table[r_type]->name);
   bfd_set_error (bfd_error_bad_value);
 }
 
@@ -3050,11 +3421,10 @@ bad_shared_reloc (abfd, r_type)
    table.  */
 
 static bfd_boolean
-ppc_elf_check_relocs (abfd, info, sec, relocs)
-     bfd *abfd;
-     struct bfd_link_info *info;
-     asection *sec;
-     const Elf_Internal_Rela *relocs;
+ppc_elf_check_relocs (bfd *abfd,
+                     struct bfd_link_info *info,
+                     asection *sec,
+                     const Elf_Internal_Rela *relocs)
 {
   struct ppc_elf_link_hash_table *htab;
   Elf_Internal_Shdr *symtab_hdr;
@@ -3063,7 +3433,7 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
   const Elf_Internal_Rela *rel_end;
   asection *sreloc;
 
-  if (info->relocateable)
+  if (info->relocatable)
     return TRUE;
 
 #ifdef DEBUG
@@ -3072,26 +3442,25 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
           bfd_archive_filename (abfd));
 #endif
 
+  /* Initialize howto table if not already done.  */
+  if (!ppc_elf_howto_table[R_PPC_ADDR32])
+    ppc_elf_howto_init ();
+
   /* Create the linker generated sections all the time so that the
      special symbols are created.  */
-
   htab = ppc_elf_hash_table (info);
   if (htab->sdata == NULL)
     {
-      htab->sdata = elf_linker_section (abfd, LINKER_SECTION_SDATA);
-      if (htab->sdata == NULL)
-       htab->sdata = ppc_elf_create_linker_section (abfd, info,
-                                                    LINKER_SECTION_SDATA);
+      htab->sdata = ppc_elf_create_linker_section (abfd, info,
+                                                  LINKER_SECTION_SDATA);
       if (htab->sdata == NULL)
        return FALSE;
     }
 
   if (htab->sdata2 == NULL)
     {
-      htab->sdata2 = elf_linker_section (abfd, LINKER_SECTION_SDATA2);
-      if (htab->sdata2 == NULL)
-       htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
-                                                     LINKER_SECTION_SDATA2);
+      htab->sdata2 = ppc_elf_create_linker_section (abfd, info,
+                                                   LINKER_SECTION_SDATA2);
       if (htab->sdata2 == NULL)
        return FALSE;
     }
@@ -3128,7 +3497,7 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
            }
        }
 
-      r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
+      r_type = ELF32_R_TYPE (rel->r_info);
       switch (r_type)
        {
        case R_PPC_GOT_TLSLD16:
@@ -3164,7 +3533,7 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
          sec->has_tls_reloc = 1;
          /* Fall thru */
 
-       /* GOT16 relocations */
+         /* GOT16 relocations */
        case R_PPC_GOT16:
        case R_PPC_GOT16_LO:
        case R_PPC_GOT16_HI:
@@ -3188,27 +3557,27 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
              return FALSE;
          break;
 
-       /* Indirect .sdata relocation.  */
+         /* Indirect .sdata relocation.  */
        case R_PPC_EMB_SDAI16:
          if (info->shared)
            {
              bad_shared_reloc (abfd, r_type);
              return FALSE;
            }
-         if (!bfd_elf32_create_pointer_linker_section (abfd, info,
-                                                       htab->sdata, h, rel))
+         if (!elf_create_pointer_linker_section (abfd, info,
+                                                 htab->sdata, h, rel))
            return FALSE;
          break;
 
-       /* Indirect .sdata2 relocation.  */
+         /* Indirect .sdata2 relocation.  */
        case R_PPC_EMB_SDA2I16:
          if (info->shared)
            {
              bad_shared_reloc (abfd, r_type);
              return FALSE;
            }
-         if (!bfd_elf32_create_pointer_linker_section (abfd, info,
-                                                       htab->sdata2, h, rel))
+         if (!elf_create_pointer_linker_section (abfd, info,
+                                                 htab->sdata2, h, rel))
            return FALSE;
          break;
 
@@ -3238,15 +3607,21 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
          fprintf (stderr, "Reloc requires a PLT entry\n");
 #endif
          /* This symbol requires a procedure linkage table entry.  We
-             actually build the entry in finish_dynamic_symbol,
-             because this might be a case of linking PIC code without
-             linking in any dynamic objects, in which case we don't
-             need to generate a procedure linkage table after all.  */
+            actually build the entry in finish_dynamic_symbol,
+            because this might be a case of linking PIC code without
+            linking in any dynamic objects, in which case we don't
+            need to generate a procedure linkage table after all.  */
 
          if (h == NULL)
            {
              /* It does not make sense to have a procedure linkage
-                 table entry for a local symbol.  */
+                table entry for a local symbol.  */
+             (*_bfd_error_handler) (_("%s(%s+0x%lx): %s reloc against "
+                                      "local symbol"),
+                                    bfd_archive_filename (abfd),
+                                    sec->name,
+                                    (long) rel->r_offset,
+                                    ppc_elf_howto_table[r_type]->name);
              bfd_set_error (bfd_error_bad_value);
              return FALSE;
            }
@@ -3338,8 +3713,7 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
        case R_PPC_REL14_BRNTAKEN:
        case R_PPC_REL32:
          if (h == NULL
-             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
-             || SYMBOL_REFERENCES_LOCAL (info, h))
+             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
            break;
          /* fall through */
 
@@ -3363,15 +3737,53 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
              /* We may need a copy reloc too.  */
              h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
            }
+
        dodyn:
-         if (info->shared)
+         /* 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
+            into the shared library.  However, if we are linking with
+            -Bsymbolic, we do not need to copy a reloc against a
+            global symbol which is defined in an object we are
+            including in the link (i.e., DEF_REGULAR is set).  At
+            this point we have not seen all the input files, so it is
+            possible that DEF_REGULAR is not set now but will be set
+            later (it is never cleared).  In case of a weak definition,
+            DEF_REGULAR may be cleared later by a strong definition in
+            a shared library.  We account for that possibility below by
+            storing information in the dyn_relocs field of the hash
+            table entry.  A similar situation occurs when creating
+            shared libraries and symbol visibility changes render the
+            symbol local.
+
+            If on the other hand, we are creating an executable, we
+            may need to keep relocations for symbols satisfied by a
+            dynamic library if we manage to avoid copy relocs for the
+            symbol.  */
+         if ((info->shared
+              && (MUST_BE_DYN_RELOC (r_type)
+                  || (h != NULL
+                      && (! info->symbolic
+                          || h->root.type == bfd_link_hash_defweak
+                          || (h->elf_link_hash_flags
+                              & ELF_LINK_HASH_DEF_REGULAR) == 0))))
+             || (ELIMINATE_COPY_RELOCS
+                 && !info->shared
+                 && (sec->flags & SEC_ALLOC) != 0
+                 && h != NULL
+                 && (h->root.type == bfd_link_hash_defweak
+                     || (h->elf_link_hash_flags
+                         & ELF_LINK_HASH_DEF_REGULAR) == 0)))
            {
              struct ppc_elf_dyn_relocs *p;
              struct ppc_elf_dyn_relocs **head;
 
 #ifdef DEBUG
-             fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
-                      (h && h->root.root.string) ? h->root.root.string : "<unknown>");
+             fprintf (stderr,
+                      "ppc_elf_check_relocs needs to "
+                      "create relocation for %s\n",
+                      (h && h->root.root.string
+                       ? h->root.root.string : "<unknown>"));
 #endif
              if (sreloc == NULL)
                {
@@ -3421,9 +3833,8 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
                     easily.  Oh well.  */
 
                  asection *s;
-                 s = (bfd_section_from_r_symndx
-                      (abfd, &ppc_elf_hash_table (info)->sym_sec,
-                       sec, r_symndx));
+                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
+                                                sec, r_symndx);
                  if (s == NULL)
                    return FALSE;
 
@@ -3434,17 +3845,19 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
              p = *head;
              if (p == NULL || p->sec != sec)
                {
-                 p = ((struct ppc_elf_dyn_relocs *)
-                      bfd_alloc (htab->elf.dynobj, sizeof *p));
+                 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
                  if (p == NULL)
                    return FALSE;
                  p->next = *head;
                  *head = p;
                  p->sec = sec;
                  p->count = 0;
+                 p->pc_count = 0;
                }
 
-             p->count++;
+             p->count += 1;
+             if (!MUST_BE_DYN_RELOC (r_type))
+               p->pc_count += 1;
            }
 
          break;
@@ -3458,12 +3871,11 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
    relocation.  */
 
 static asection *
-ppc_elf_gc_mark_hook (sec, info, rel, h, sym)
-     asection *sec;
-     struct bfd_link_info *info ATTRIBUTE_UNUSED;
-     Elf_Internal_Rela *rel;
-     struct elf_link_hash_entry *h;
-     Elf_Internal_Sym *sym;
+ppc_elf_gc_mark_hook (asection *sec,
+                     struct bfd_link_info *info ATTRIBUTE_UNUSED,
+                     Elf_Internal_Rela *rel,
+                     struct elf_link_hash_entry *h,
+                     Elf_Internal_Sym *sym)
 {
   if (h != NULL)
     {
@@ -3498,11 +3910,10 @@ ppc_elf_gc_mark_hook (sec, info, rel, h, sym)
    section being removed.  */
 
 static bfd_boolean
-ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
-     bfd *abfd;
-     struct bfd_link_info *info;
-     asection *sec;
-     const Elf_Internal_Rela *relocs;
+ppc_elf_gc_sweep_hook (bfd *abfd,
+                      struct bfd_link_info *info,
+                      asection *sec,
+                      const Elf_Internal_Rela *relocs)
 {
   struct ppc_elf_link_hash_table *htab;
   Elf_Internal_Shdr *symtab_hdr;
@@ -3542,7 +3953,7 @@ ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
              }
        }
 
-      r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
+      r_type = ELF32_R_TYPE (rel->r_info);
       switch (r_type)
        {
        case R_PPC_GOT_TLSLD16:
@@ -3586,8 +3997,7 @@ ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
        case R_PPC_REL14_BRNTAKEN:
        case R_PPC_REL32:
          if (h == NULL
-             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
-             || SYMBOL_REFERENCES_LOCAL (info, h))
+             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
            break;
          /* Fall thru */
 
@@ -3624,9 +4034,7 @@ ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
 /* Set htab->tls_sec and htab->tls_get_addr.  */
 
 bfd_boolean
-ppc_elf_tls_setup (obfd, info)
-     bfd *obfd;
-     struct bfd_link_info *info;
+ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
 {
   asection *tls;
   struct ppc_elf_link_hash_table *htab;
@@ -3648,15 +4056,14 @@ ppc_elf_tls_setup (obfd, info)
    opportunities.  */
 
 bfd_boolean
-ppc_elf_tls_optimize (obfd, info)
-     bfd *obfd ATTRIBUTE_UNUSED;
-     struct bfd_link_info *info;
+ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
+                     struct bfd_link_info *info)
 {
   bfd *ibfd;
   asection *sec;
   struct ppc_elf_link_hash_table *htab;
 
-  if (info->relocateable || info->shared)
+  if (info->relocatable || info->shared)
     return TRUE;
 
   htab = ppc_elf_hash_table (info);
@@ -3672,9 +4079,8 @@ ppc_elf_tls_optimize (obfd, info)
            int expecting_tls_get_addr;
 
            /* Read the relocations.  */
-           relstart = _bfd_elf32_link_read_relocs (ibfd, sec, (PTR) NULL,
-                                                   (Elf_Internal_Rela *) NULL,
-                                                   info->keep_memory);
+           relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
+                                                 info->keep_memory);
            if (relstart == NULL)
              return FALSE;
 
@@ -3706,7 +4112,7 @@ ppc_elf_tls_optimize (obfd, info)
                    || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC))
                  is_local = TRUE;
 
-               r_type = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel->r_info);
+               r_type = ELF32_R_TYPE (rel->r_info);
                switch (r_type)
                  {
                  case R_PPC_GOT_TLSLD16:
@@ -3843,55 +4249,36 @@ ppc_elf_tls_optimize (obfd, info)
    file.  We use it to put .comm items in .sbss, and not .bss.  */
 
 static bfd_boolean
-ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
-     bfd *abfd;
-     struct bfd_link_info *info;
-     const Elf_Internal_Sym *sym;
-     const char **namep ATTRIBUTE_UNUSED;
-     flagword *flagsp ATTRIBUTE_UNUSED;
-     asection **secp;
-     bfd_vma *valp;
+ppc_elf_add_symbol_hook (bfd *abfd,
+                        struct bfd_link_info *info,
+                        const Elf_Internal_Sym *sym,
+                        const char **namep ATTRIBUTE_UNUSED,
+                        flagword *flagsp ATTRIBUTE_UNUSED,
+                        asection **secp,
+                        bfd_vma *valp)
 {
   if (sym->st_shndx == SHN_COMMON
-      && !info->relocateable
+      && !info->relocatable
       && sym->st_size <= elf_gp_size (abfd)
-      && info->hash->creator->flavour == bfd_target_elf_flavour)
+      && (info->hash->creator == abfd->xvec
+         || info->hash->creator == abfd->xvec->alternative_target))
     {
       /* Common symbols less than or equal to -G nn bytes are automatically
-        put into .sdata.  */
-      elf_linker_section_t *sdata
-       = ppc_elf_create_linker_section (abfd, info, LINKER_SECTION_SDATA);
+        put into .sbss.  */
+      struct ppc_elf_link_hash_table *htab;
 
-      if (!sdata->bss_section)
+      htab = ppc_elf_hash_table (info);
+      if (htab->sbss == NULL)
        {
-         bfd_size_type amt;
+         flagword flags = SEC_IS_COMMON;
 
-         /* We don't go through bfd_make_section, because we don't
-             want to attach this common section to DYNOBJ.  The linker
-             will move the symbols to the appropriate output section
-             when it defines common symbols.  */
-         amt = sizeof (asection);
-         sdata->bss_section = (asection *) bfd_zalloc (abfd, amt);
-         if (sdata->bss_section == NULL)
+         htab->sbss = bfd_make_section_anyway (abfd, ".sbss");
+         if (htab->sbss == NULL
+             || ! bfd_set_section_flags (abfd, htab->sbss, flags))
            return FALSE;
-         sdata->bss_section->name = sdata->bss_name;
-         sdata->bss_section->flags = SEC_IS_COMMON;
-         sdata->bss_section->output_section = sdata->bss_section;
-         amt = sizeof (asymbol);
-         sdata->bss_section->symbol = (asymbol *) bfd_zalloc (abfd, amt);
-         amt = sizeof (asymbol *);
-         sdata->bss_section->symbol_ptr_ptr =
-           (asymbol **) bfd_zalloc (abfd, amt);
-         if (sdata->bss_section->symbol == NULL
-             || sdata->bss_section->symbol_ptr_ptr == NULL)
-           return FALSE;
-         sdata->bss_section->symbol->name = sdata->bss_name;
-         sdata->bss_section->symbol->flags = BSF_SECTION_SYM;
-         sdata->bss_section->symbol->section = sdata->bss_section;
-         *sdata->bss_section->symbol_ptr_ptr = sdata->bss_section->symbol;
        }
 
-      *secp = sdata->bss_section;
+      *secp = htab->sbss;
       *valp = sym->st_size;
     }
 
@@ -3902,11 +4289,10 @@ ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
    dynamic sections here.  */
 
 static bfd_boolean
-ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
-     bfd *output_bfd;
-     struct bfd_link_info *info;
-     struct elf_link_hash_entry *h;
-     Elf_Internal_Sym *sym;
+ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
+                              struct bfd_link_info *info,
+                              struct elf_link_hash_entry *h,
+                              Elf_Internal_Sym *sym)
 {
   struct ppc_elf_link_hash_table *htab;
 
@@ -3929,7 +4315,7 @@ ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
 #endif
 
       /* This symbol has an entry in the procedure linkage table.  Set
-         it up.  */
+        it up.  */
 
       BFD_ASSERT (h->dynindx != -1);
       BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
@@ -4011,9 +4397,8 @@ ppc_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
 /* Finish up the dynamic sections.  */
 
 static bfd_boolean
-ppc_elf_finish_dynamic_sections (output_bfd, info)
-     bfd *output_bfd;
-     struct bfd_link_info *info;
+ppc_elf_finish_dynamic_sections (bfd *output_bfd,
+                                struct bfd_link_info *info)
 {
   asection *sdyn;
   struct ppc_elf_link_hash_table *htab;
@@ -4069,10 +4454,10 @@ ppc_elf_finish_dynamic_sections (output_bfd, info)
   if (htab->got)
     {
       unsigned char *contents = htab->got->contents;
-      bfd_put_32 (output_bfd, (bfd_vma) 0x4e800021 /* blrl */, contents);
+      bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
 
       if (sdyn == NULL)
-       bfd_put_32 (output_bfd, (bfd_vma) 0, contents + 4);
+       bfd_put_32 (output_bfd, 0, contents + 4);
       else
        bfd_put_32 (output_bfd,
                    sdyn->output_section->vma + sdyn->output_offset,
@@ -4093,7 +4478,7 @@ ppc_elf_finish_dynamic_sections (output_bfd, info)
 
    This function is responsible for adjust the section contents as
    necessary, and (if using Rela relocs and generating a
-   relocateable output file) adjusting the reloc addend as
+   relocatable output file) adjusting the reloc addend as
    necessary.
 
    This function does not have to worry about setting the reloc
@@ -4107,23 +4492,21 @@ ppc_elf_finish_dynamic_sections (output_bfd, info)
    The global hash table entry for the global symbols can be found
    via elf_sym_hashes (input_bfd).
 
-   When generating relocateable output, this function must handle
+   When generating relocatable output, this function must handle
    STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
    going to be the section symbol corresponding to the output
    section, which means that the addend must be adjusted
    accordingly.  */
 
 static bfd_boolean
-ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
-                         contents, relocs, local_syms, local_sections)
-     bfd *output_bfd;
-     struct bfd_link_info *info;
-     bfd *input_bfd;
-     asection *input_section;
-     bfd_byte *contents;
-     Elf_Internal_Rela *relocs;
-     Elf_Internal_Sym *local_syms;
-     asection **local_sections;
+ppc_elf_relocate_section (bfd *output_bfd,
+                         struct bfd_link_info *info,
+                         bfd *input_bfd,
+                         asection *input_section,
+                         bfd_byte *contents,
+                         Elf_Internal_Rela *relocs,
+                         Elf_Internal_Sym *local_syms,
+                         asection **local_sections)
 {
   Elf_Internal_Shdr *symtab_hdr;
   struct elf_link_hash_entry **sym_hashes;
@@ -4137,18 +4520,19 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
   bfd_boolean ret = TRUE;
 
 #ifdef DEBUG
-  fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, %ld relocations%s\n",
+  fprintf (stderr, "ppc_elf_relocate_section called for %s section %s, "
+          "%ld relocations%s\n",
           bfd_archive_filename (input_bfd),
           bfd_section_name(input_bfd, input_section),
           (long) input_section->reloc_count,
-          (info->relocateable) ? " (relocatable)" : "");
+          (info->relocatable) ? " (relocatable)" : "");
 #endif
 
-  if (info->relocateable)
+  if (info->relocatable)
     return TRUE;
 
+  /* Initialize howto table if not already done.  */
   if (!ppc_elf_howto_table[R_PPC_ADDR32])
-    /* Initialize howto table if needed.  */
     ppc_elf_howto_init ();
 
   htab = ppc_elf_hash_table (info);
@@ -4170,15 +4554,14 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
       unsigned long r_symndx;
       bfd_vma relocation;
       bfd_vma branch_bit, insn, from;
-      bfd_boolean will_become_local;
       bfd_boolean unresolved_reloc;
       bfd_boolean warned;
       unsigned int tls_type, tls_mask, tls_gd;
 
-      r_type = (enum elf_ppc_reloc_type)ELF32_R_TYPE (rel->r_info);
-      sym = (Elf_Internal_Sym *) 0;
-      sec = (asection *) 0;
-      h = (struct elf_link_hash_entry *) 0;
+      r_type = ELF32_R_TYPE (rel->r_info);
+      sym = NULL;
+      sec = NULL;
+      h = NULL;
       unresolved_reloc = FALSE;
       warned = FALSE;
       r_symndx = ELF32_R_SYM (rel->r_info);
@@ -4189,8 +4572,6 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          sym_name = bfd_elf_local_sym_name (input_bfd, sym);
 
          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
-         /* Relocs to local symbols are always resolved.  */
-         will_become_local = TRUE;
        }
       else
        {
@@ -4200,17 +4581,15 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
          sym_name = h->root.root.string;
 
-         /* Can this relocation be resolved immediately?  */
-         will_become_local = SYMBOL_REFERENCES_LOCAL (info, h);
          relocation = 0;
          if (h->root.type == bfd_link_hash_defined
              || h->root.type == bfd_link_hash_defweak)
            {
              sec = h->root.u.def.section;
-               /* 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.  */
+             /* 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.  */
              if (sec->output_section == NULL)
                unresolved_reloc = TRUE;
              else
@@ -4220,7 +4599,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
            }
          else if (h->root.type == bfd_link_hash_undefweak)
            ;
-         else if (info->shared
+         else if (!info->executable
                   && !info->no_undefined
                   && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
            ;
@@ -4370,13 +4749,12 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                  enum elf_ppc_reloc_type r_type2;
                  unsigned long r_symndx2;
                  struct elf_link_hash_entry *h2;
-                 bfd_vma insn1, insn2, insn3;
+                 bfd_vma insn1, insn2;
                  bfd_vma offset;
 
                  /* The next instruction should be a call to
                     __tls_get_addr.  Peek at the reloc to be sure.  */
-                 r_type2
-                   = (enum elf_ppc_reloc_type) ELF32_R_TYPE (rel[1].r_info);
+                 r_type2 = ELF32_R_TYPE (rel[1].r_info);
                  r_symndx2 = ELF32_R_SYM (rel[1].r_info);
                  if (r_symndx2 < symtab_hdr->sh_info
                      || (r_type2 != R_PPC_REL14
@@ -4397,8 +4775,6 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                  offset = rel[1].r_offset;
                  insn1 = bfd_get_32 (output_bfd,
                                      contents + rel->r_offset - 2);
-                 insn3 = bfd_get_32 (output_bfd,
-                                     contents + offset + 4);
                  if ((tls_mask & tls_gd) != 0)
                    {
                      /* IE */
@@ -4428,20 +4804,12 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                                                    R_PPC_TPREL16_LO);
                      rel[1].r_offset += 2;
                    }
-                 if (insn3 == NOP
-                     || insn3 == CROR_151515 || insn3 == CROR_313131)
-                   {
-                     insn3 = insn2;
-                     insn2 = NOP;
-                     rel[1].r_offset += 4;
-                   }
                  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)
                    {
                      /* We changed the symbol on an LD reloc.  Start over
-                        in order to get h, sym, sec etc. right.  */ 
+                        in order to get h, sym, sec etc. right.  */
                      rel--;
                      continue;
                    }
@@ -4485,8 +4853,8 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
       addend = rel->r_addend;
       tls_type = 0;
       howto = NULL;
-      if ((unsigned) r_type < (unsigned) R_PPC_max)
-       howto = ppc_elf_howto_table[(int) r_type];
+      if (r_type < R_PPC_max)
+       howto = ppc_elf_howto_table[r_type];
       switch (r_type)
        {
        default:
@@ -4554,15 +4922,14 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
 
            indx = 0;
            if (tls_type == (TLS_TLS | TLS_LD)
-               && ((tls_mask & TLS_LD) == 0
-                   || h == NULL
+               && (h == NULL
                    || !(h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)))
              offp = &htab->tlsld_got.offset;
            else if (h != NULL)
              {
                bfd_boolean dyn;
                dyn = htab->elf.dynamic_sections_created;
-               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
+               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
                    || (info->shared
                        && SYMBOL_REFERENCES_LOCAL (info, h)))
                  /* This is actually a static link, or it is a
@@ -4592,76 +4959,149 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
              off &= ~1;
            else
              {
-               *offp = off | 1;
-
-               /* Generate relocs for the dynamic linker, except in
-                  the case of TLSLD where we'll use one entry per
-                  module.  */
-               if (info->shared || indx != 0)
+               unsigned int tls_m = (tls_mask
+                                     & (TLS_LD | TLS_GD | TLS_DTPREL
+                                        | TLS_TPREL | TLS_TPRELGD));
+
+               if (offp == &htab->tlsld_got.offset)
+                 tls_m = TLS_LD;
+               else if (h == NULL
+                        || !(h->elf_link_hash_flags
+                             & ELF_LINK_HASH_DEF_DYNAMIC))
+                 tls_m &= ~TLS_LD;
+
+               /* We might have multiple got entries for this sym.
+                  Initialize them all.  */
+               do
                  {
-                   outrel.r_offset = (htab->got->output_section->vma
-                                      + htab->got->output_offset
-                                      + off);
-                   if (tls_type & (TLS_LD | TLS_GD))
+                   int tls_ty = 0;
+
+                   if ((tls_m & TLS_LD) != 0)
+                     {
+                       tls_ty = TLS_TLS | TLS_LD;
+                       tls_m &= ~TLS_LD;
+                     }
+                   else if ((tls_m & TLS_GD) != 0)
+                     {
+                       tls_ty = TLS_TLS | TLS_GD;
+                       tls_m &= ~TLS_GD;
+                     }
+                   else if ((tls_m & TLS_DTPREL) != 0)
+                     {
+                       tls_ty = TLS_TLS | TLS_DTPREL;
+                       tls_m &= ~TLS_DTPREL;
+                     }
+                   else if ((tls_m & (TLS_TPREL | TLS_TPRELGD)) != 0)
                      {
-                       outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
+                       tls_ty = TLS_TLS | TLS_TPREL;
+                       tls_m = 0;
+                     }
+
+                   /* Generate relocs for the dynamic linker.  */
+                   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
+                                          + off);
                        outrel.r_addend = 0;
-                       if (tls_type == (TLS_TLS | TLS_GD))
+                       if (tls_ty & (TLS_LD | TLS_GD))
+                         {
+                           outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPMOD32);
+                           if (tls_ty == (TLS_TLS | TLS_GD))
+                             {
+                               loc = htab->relgot->contents;
+                               loc += (htab->relgot->reloc_count++
+                                       * sizeof (Elf32_External_Rela));
+                               bfd_elf32_swap_reloca_out (output_bfd,
+                                                          &outrel, loc);
+                               outrel.r_offset += 4;
+                               outrel.r_info
+                                 = ELF32_R_INFO (indx, R_PPC_DTPREL32);
+                             }
+                         }
+                       else if (tls_ty == (TLS_TLS | TLS_DTPREL))
+                         outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
+                       else if (tls_ty == (TLS_TLS | TLS_TPREL))
+                         outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
+                       else if (indx == 0)
+                         outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
+                       else
+                         outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
+                       if (indx == 0)
                          {
-                           loc = htab->relgot->contents;
-                           loc += (htab->relgot->reloc_count++
-                                   * sizeof (Elf32_External_Rela));
-                           bfd_elf32_swap_reloca_out (output_bfd,
-                                                      &outrel, loc);
-                           outrel.r_info
-                             = ELF32_R_INFO (indx, R_PPC_DTPREL32);
-                           outrel.r_offset += 4;
+                           outrel.r_addend += relocation;
+                           if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
+                             outrel.r_addend -= htab->tls_sec->vma;
                          }
+                       loc = htab->relgot->contents;
+                       loc += (htab->relgot->reloc_count++
+                               * sizeof (Elf32_External_Rela));
+                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
                      }
-                   else if (tls_type == (TLS_TLS | TLS_DTPREL))
-                     outrel.r_info = ELF32_R_INFO (indx, R_PPC_DTPREL32);
-                   else if (tls_type == (TLS_TLS | TLS_TPREL))
-                     outrel.r_info = ELF32_R_INFO (indx, R_PPC_TPREL32);
-                   else if (indx == 0)
-                     outrel.r_info = ELF32_R_INFO (indx, R_PPC_RELATIVE);
-                   else
-                     outrel.r_info = ELF32_R_INFO (indx, R_PPC_GLOB_DAT);
-                   outrel.r_addend = 0;
-                   if (indx == 0)
-                     outrel.r_addend += relocation;
-                   loc = htab->relgot->contents;
-                   loc += (htab->relgot->reloc_count++
-                           * sizeof (Elf32_External_Rela));
-                   bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
-                 }
 
-               /* Init the .got section contents if we're not
-                  emitting a reloc.  */
-               else
-                 {
-                   if (tls_type != 0)
+                   /* Init the .got section contents if we're not
+                      emitting a reloc.  */
+                   else
                      {
-                       relocation -= htab->tls_sec->vma + DTP_OFFSET;
-                       if ((tls_type & TLS_TPREL) != 0)
-                         relocation += DTP_OFFSET - TP_OFFSET;
-                     }
+                       bfd_vma value = relocation;
 
-                   if (tls_type == (TLS_TLS | TLS_GD))
-                     {
-                       bfd_put_32 (output_bfd, relocation,
-                                   htab->got->contents + off + 4);
-                       relocation = 1;
+                       if (tls_ty == (TLS_TLS | TLS_LD))
+                         value = 1;
+                       else if (tls_ty != 0)
+                         {
+                           value -= htab->tls_sec->vma + DTP_OFFSET;
+                           if (tls_ty == (TLS_TLS | TLS_TPREL))
+                             value += DTP_OFFSET - TP_OFFSET;
+
+                           if (tls_ty == (TLS_TLS | TLS_GD))
+                             {
+                               bfd_put_32 (output_bfd, value,
+                                           htab->got->contents + off + 4);
+                               value = 1;
+                             }
+                         }
+                       bfd_put_32 (output_bfd, value,
+                                   htab->got->contents + off);
                      }
-                   else if (tls_type == (TLS_TLS | TLS_LD))
-                     relocation = 1;
-                   bfd_put_32 (output_bfd, relocation,
-                               htab->got->contents + off);
+
+                   off += 4;
+                   if (tls_ty & (TLS_LD | TLS_GD))
+                     off += 4;
                  }
+               while (tls_m != 0);
+
+               off = *offp;
+               *offp = off | 1;
              }
 
            if (off >= (bfd_vma) -2)
              abort ();
 
+           if ((tls_type & TLS_TLS) != 0)
+             {
+               if (tls_type != (TLS_TLS | TLS_LD))
+                 {
+                   if ((tls_mask & TLS_LD) != 0
+                       && !(h == NULL
+                            || !(h->elf_link_hash_flags
+                                 & ELF_LINK_HASH_DEF_DYNAMIC)))
+                     off += 8;
+                   if (tls_type != (TLS_TLS | TLS_GD))
+                     {
+                       if ((tls_mask & TLS_GD) != 0)
+                         off += 8;
+                       if (tls_type != (TLS_TLS | TLS_DTPREL))
+                         {
+                           if ((tls_mask & TLS_DTPREL) != 0)
+                             off += 4;
+                         }
+                     }
+                 }
+             }
+
            relocation = htab->got->output_offset + off - 4;
 
            /* Addends on got relocations don't make much sense.
@@ -4670,22 +5110,20 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
               got at entry m+n bears little relation to the entry m.  */
            if (addend != 0)
              (*_bfd_error_handler)
-               (_("%s(%s+0x%lx): non-zero addend on got reloc against `%s'"),
+               (_("%s(%s+0x%lx): non-zero addend on %s reloc against `%s'"),
                 bfd_archive_filename (input_bfd),
                 bfd_get_section_name (input_bfd, input_section),
                 (long) rel->r_offset,
+                howto->name,
                 sym_name);
          }
-         break;
+       break;
 
        /* Relocations that need no special processing.  */
        case R_PPC_LOCAL24PC:
          /* It makes no sense to point a local relocation
             at a symbol not in this object.  */
-         if (h != NULL
-             && (h->root.type == bfd_link_hash_defined
-                 || h->root.type == bfd_link_hash_defweak)
-             && sec->output_section == NULL)
+         if (unresolved_reloc)
            {
              if (! (*info->callbacks->undefined_symbol) (info,
                                                          h->root.root.string,
@@ -4705,8 +5143,8 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          addend -= htab->tls_sec->vma + DTP_OFFSET;
          break;
 
-       /* Relocations that may need to be propagated if this is a shared
-           object.  */
+         /* Relocations that may need to be propagated if this is a shared
+            object.  */
        case R_PPC_TPREL16:
        case R_PPC_TPREL16_LO:
        case R_PPC_TPREL16_HI:
@@ -4725,6 +5163,11 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          addend -= htab->tls_sec->vma + DTP_OFFSET;
          goto dodyn;
 
+       case R_PPC_DTPMOD32:
+         relocation = 1;
+         addend = 0;
+         goto dodyn;
+
        case R_PPC_REL24:
        case R_PPC_REL32:
        case R_PPC_REL14:
@@ -4732,14 +5175,13 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
        case R_PPC_REL14_BRNTAKEN:
          /* If these relocations are not to a named symbol, they can be
             handled right here, no need to bother the dynamic linker.  */
-         if (h == NULL
-             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
-             || SYMBOL_REFERENCES_LOCAL (info, h))
+         if (SYMBOL_REFERENCES_LOCAL (info, h)
+             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
            break;
-       /* fall through */
+         /* fall through */
 
-       /* Relocations that always need to be propagated if this is a shared
-           object.  */
+         /* Relocations that always need to be propagated if this is a shared
+            object.  */
        case R_PPC_ADDR32:
        case R_PPC_ADDR24:
        case R_PPC_ADDR16:
@@ -4751,20 +5193,41 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
        case R_PPC_ADDR14_BRNTAKEN:
        case R_PPC_UADDR32:
        case R_PPC_UADDR16:
-       case R_PPC_DTPMOD32:
+         /* r_symndx will be zero only for relocs against symbols
+            from removed linkonce sections, or sections discarded by
+            a linker script.  */
        dodyn:
-         if (info->shared && r_symndx != 0)
+         if (r_symndx == 0)
+           break;
+         /* Fall thru.  */
+
+         if ((info->shared
+              && (h == NULL
+                  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
+                  || h->root.type != bfd_link_hash_undefweak)
+              && (MUST_BE_DYN_RELOC (r_type)
+                  || !SYMBOL_CALLS_LOCAL (info, h)))
+             || (ELIMINATE_COPY_RELOCS
+                 && !info->shared
+                 && (input_section->flags & SEC_ALLOC) != 0
+                 && h != NULL
+                 && h->dynindx != -1
+                 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
+                 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
+                 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
            {
              int skip;
 
 #ifdef DEBUG
-             fprintf (stderr, "ppc_elf_relocate_section need to create relocation for %s\n",
-                      (h && h->root.root.string) ? h->root.root.string : "<unknown>");
+             fprintf (stderr, "ppc_elf_relocate_section needs to "
+                      "create relocation for %s\n",
+                      (h && h->root.root.string
+                       ? h->root.root.string : "<unknown>"));
 #endif
 
              /* When generating a shared object, these relocations
-                 are copied into the output file to be resolved at run
-                 time.  */
+                are copied into the output file to be resolved at run
+                time.  */
              if (sreloc == NULL)
                {
                  const char *name;
@@ -4798,9 +5261,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
 
              if (skip)
                memset (&outrel, 0, sizeof outrel);
-             /* h->dynindx may be -1 if this symbol was marked to
-                 become local.  */
-             else if (! will_become_local)
+             else if (!SYMBOL_REFERENCES_LOCAL (info, h))
                {
                  unresolved_reloc = FALSE;
                  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
@@ -4816,16 +5277,6 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                    {
                      long indx;
 
-                     if (h == NULL)
-                       sec = local_sections[r_symndx];
-                     else
-                       {
-                         BFD_ASSERT (h->root.type == bfd_link_hash_defined
-                                     || (h->root.type
-                                         == bfd_link_hash_defweak));
-                         sec = h->root.u.def.section;
-                       }
-
                      if (bfd_is_abs_section (sec))
                        indx = 0;
                      else if (sec == NULL || sec->owner == NULL)
@@ -4847,11 +5298,9 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                          BFD_ASSERT (indx > 0);
 #ifdef DEBUG
                          if (indx <= 0)
-                           {
-                             printf ("indx=%d section=%s flags=%08x name=%s\n",
-                                    indx, osec->name, osec->flags,
-                                    h->root.root.string);
-                           }
+                           printf ("indx=%d section=%s flags=%08x name=%s\n",
+                                   indx, osec->name, osec->flags,
+                                   h->root.root.string);
 #endif
                        }
 
@@ -4879,50 +5328,48 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
            }
          break;
 
-       /* Indirect .sdata relocation.  */
+         /* Indirect .sdata relocation.  */
        case R_PPC_EMB_SDAI16:
          BFD_ASSERT (htab->sdata != NULL);
          relocation
-           = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
-                                                      info, htab->sdata, h,
-                                                      relocation, rel,
-                                                      R_PPC_RELATIVE);
+           = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
+                                                htab->sdata, h, relocation,
+                                                rel, R_PPC_RELATIVE);
          break;
 
-       /* Indirect .sdata2 relocation.  */
+         /* Indirect .sdata2 relocation.  */
        case R_PPC_EMB_SDA2I16:
          BFD_ASSERT (htab->sdata2 != NULL);
          relocation
-           = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd,
-                                                      info, htab->sdata2, h,
-                                                      relocation, rel,
-                                                      R_PPC_RELATIVE);
+           = elf_finish_pointer_linker_section (output_bfd, input_bfd, info,
+                                                htab->sdata2, h, relocation,
+                                                rel, R_PPC_RELATIVE);
          break;
 
-       /* Handle the TOC16 reloc.  We want to use the offset within the .got
-          section, not the actual VMA.  This is appropriate when generating
-          an embedded ELF object, for which the .got section acts like the
-          AIX .toc section.  */
+         /* Handle the TOC16 reloc.  We want to use the offset within the .got
+            section, not the actual VMA.  This is appropriate when generating
+            an embedded ELF object, for which the .got section acts like the
+            AIX .toc section.  */
        case R_PPC_TOC16:                       /* phony GOT16 relocations */
-         BFD_ASSERT (sec != (asection *) 0);
+         BFD_ASSERT (sec != NULL);
          BFD_ASSERT (bfd_is_und_section (sec)
                      || strcmp (bfd_get_section_name (abfd, sec), ".got") == 0
                      || strcmp (bfd_get_section_name (abfd, sec), ".cgot") == 0)
 
-         addend -= sec->output_section->vma + sec->output_offset + 0x8000;
+           addend -= sec->output_section->vma + sec->output_offset + 0x8000;
          break;
 
        case R_PPC_PLTREL24:
          /* Relocation is to the entry for this symbol in the
-             procedure linkage table.  */
+            procedure linkage table.  */
          BFD_ASSERT (h != NULL);
 
          if (h->plt.offset == (bfd_vma) -1
              || htab->plt == NULL)
            {
              /* We didn't make a PLT entry for this symbol.  This
-                 happens when statically linking PIC code, or when
-                 using -Bsymbolic.  */
+                happens when statically linking PIC code, or when
+                using -Bsymbolic.  */
              break;
            }
 
@@ -4932,24 +5379,26 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                        + h->plt.offset);
          break;
 
-       /* Relocate against _SDA_BASE_.  */
+         /* Relocate against _SDA_BASE_.  */
        case R_PPC_SDAREL16:
          {
            const char *name;
            const struct elf_link_hash_entry *sh;
 
-           BFD_ASSERT (sec != (asection *) 0);
+           BFD_ASSERT (sec != NULL);
            name = bfd_get_section_name (abfd, sec->output_section);
            if (! ((strncmp (name, ".sdata", 6) == 0
                    && (name[6] == 0 || name[6] == '.'))
                   || (strncmp (name, ".sbss", 5) == 0
                       && (name[5] == 0 || name[5] == '.'))))
              {
-               (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
-                                      bfd_archive_filename (input_bfd),
-                                      sym_name,
-                                      howto->name,
-                                      name);
+               (*_bfd_error_handler)
+                 (_("%s: the target (%s) of a %s relocation is "
+                    "in the wrong output section (%s)"),
+                  bfd_archive_filename (input_bfd),
+                  sym_name,
+                  howto->name,
+                  name);
              }
            sh = htab->sdata->sym_hash;
            addend -= (sh->root.u.def.value
@@ -4958,22 +5407,24 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          }
          break;
 
-       /* Relocate against _SDA2_BASE_.  */
+         /* Relocate against _SDA2_BASE_.  */
        case R_PPC_EMB_SDA2REL:
          {
            const char *name;
            const struct elf_link_hash_entry *sh;
 
-           BFD_ASSERT (sec != (asection *) 0);
+           BFD_ASSERT (sec != NULL);
            name = bfd_get_section_name (abfd, sec->output_section);
            if (! (strncmp (name, ".sdata2", 7) == 0
                   || strncmp (name, ".sbss2", 6) == 0))
              {
-               (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
-                                      bfd_archive_filename (input_bfd),
-                                      sym_name,
-                                      howto->name,
-                                      name);
+               (*_bfd_error_handler)
+                 (_("%s: the target (%s) of a %s relocation is "
+                    "in the wrong output section (%s)"),
+                  bfd_archive_filename (input_bfd),
+                  sym_name,
+                  howto->name,
+                  name);
 
                bfd_set_error (bfd_error_bad_value);
                ret = FALSE;
@@ -4986,7 +5437,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          }
          break;
 
-       /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
+         /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
        case R_PPC_EMB_SDA21:
        case R_PPC_EMB_RELSDA:
          {
@@ -4994,7 +5445,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
            const struct elf_link_hash_entry *sh;
            int reg;
 
-           BFD_ASSERT (sec != (asection *) 0);
+           BFD_ASSERT (sec != NULL);
            name = bfd_get_section_name (abfd, sec->output_section);
            if (((strncmp (name, ".sdata", 6) == 0
                  && (name[6] == 0 || name[6] == '.'))
@@ -5026,11 +5477,13 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
 
            else
              {
-               (*_bfd_error_handler) (_("%s: The target (%s) of a %s relocation is in the wrong output section (%s)"),
-                                      bfd_archive_filename (input_bfd),
-                                      sym_name,
-                                      howto->name,
-                                      name);
+               (*_bfd_error_handler)
+                 (_("%s: the target (%s) of a %s relocation is "
+                    "in the wrong output section (%s)"),
+                  bfd_archive_filename (input_bfd),
+                  sym_name,
+                  howto->name,
+                  name);
 
                bfd_set_error (bfd_error_bad_value);
                ret = FALSE;
@@ -5046,16 +5499,16 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          }
          break;
 
-       /* Relocate against the beginning of the section.  */
+         /* Relocate against the beginning of the section.  */
        case R_PPC_SECTOFF:
        case R_PPC_SECTOFF_LO:
        case R_PPC_SECTOFF_HI:
        case R_PPC_SECTOFF_HA:
-         BFD_ASSERT (sec != (asection *) 0);
+         BFD_ASSERT (sec != NULL);
          addend -= sec->output_section->vma;
          break;
 
-       /* Negative relocations.  */
+         /* Negative relocations.  */
        case R_PPC_EMB_NADDR32:
        case R_PPC_EMB_NADDR16:
        case R_PPC_EMB_NADDR16_LO:
@@ -5080,7 +5533,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
        case R_PPC_EMB_RELST_HA:
        case R_PPC_EMB_BIT_FLD:
          (*_bfd_error_handler)
-           (_("%s: Relocation %s is not yet supported for symbol %s."),
+           (_("%s: relocation %s is not yet supported for symbol %s."),
             bfd_archive_filename (input_bfd),
             howto->name,
             sym_name);
@@ -5109,17 +5562,19 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
        case R_PPC_EMB_NADDR16_HA:
        case R_PPC_EMB_RELST_HA:
          /* It's just possible that this symbol is a weak symbol
-            that's not actually defined anywhere. In that case,
+            that's not actually defined anywhere.  In that case,
             'sec' would be NULL, and we should leave the symbol
             alone (it will be set to zero elsewhere in the link).  */
          if (sec != NULL)
-           /* Add 0x10000 if sign bit in 0:15 is set.  */
-           addend += ((relocation + addend) & 0x8000) << 1;
+           /* Add 0x10000 if sign bit in 0:15 is set.
+              Bits 0:15 are not used.  */
+           addend += 0x8000;
          break;
        }
 
 #ifdef DEBUG
-      fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, offset = %ld, addend = %ld\n",
+      fprintf (stderr, "\ttype = %s (%d), name = %s, symbol index = %ld, "
+              "offset = %ld, addend = %ld\n",
               howto->name,
               (int) r_type,
               sym_name,
@@ -5133,10 +5588,11 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
               && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
        {
          (*_bfd_error_handler)
-           (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
+           (_("%s(%s+0x%lx): unresolvable %s relocation against symbol `%s'"),
             bfd_archive_filename (input_bfd),
             bfd_get_section_name (input_bfd, input_section),
             (long) rel->r_offset,
+            howto->name,
             sym_name);
          ret = FALSE;
        }
@@ -5182,10 +5638,10 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          else
            {
              (*_bfd_error_handler)
-               (_("%s(%s+0x%lx): reloc against `%s': error %d"),
+               (_("%s(%s+0x%lx): %s reloc against `%s': error %d"),
                 bfd_archive_filename (input_bfd),
                 bfd_get_section_name (input_bfd, input_section),
-                (long) rel->r_offset, sym_name, (int) r);
+                (long) rel->r_offset, howto->name, sym_name, (int) r);
              ret = FALSE;
            }
        }
@@ -5199,10 +5655,9 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
 }
 
 static enum elf_reloc_type_class
-ppc_elf_reloc_type_class (rela)
-     const Elf_Internal_Rela *rela;
+ppc_elf_reloc_type_class (const Elf_Internal_Rela *rela)
 {
-  switch ((int) ELF32_R_TYPE (rela->r_info))
+  switch (ELF32_R_TYPE (rela->r_info))
     {
     case R_PPC_RELATIVE:
       return reloc_class_relative;
@@ -5220,30 +5675,28 @@ ppc_elf_reloc_type_class (rela)
 /* Support for core dump NOTE sections.  */
 
 static bfd_boolean
-ppc_elf_grok_prstatus (abfd, note)
-     bfd *abfd;
-     Elf_Internal_Note *note;
+ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
 {
   int offset;
   unsigned int raw_size;
 
   switch (note->descsz)
     {
-      default:
-       return FALSE;
+    default:
+      return FALSE;
 
-      case 268:                /* Linux/PPC.  */
-       /* pr_cursig */
-       elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
+    case 268:          /* Linux/PPC.  */
+      /* 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 + 24);
+      /* pr_pid */
+      elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
 
-       /* pr_reg */
-       offset = 72;
-       raw_size = 192;
+      /* pr_reg */
+      offset = 72;
+      raw_size = 192;
 
-       break;
+      break;
     }
 
   /* Make a ".reg/999" section.  */
@@ -5252,20 +5705,18 @@ ppc_elf_grok_prstatus (abfd, note)
 }
 
 static bfd_boolean
-ppc_elf_grok_psinfo (abfd, note)
-     bfd *abfd;
-     Elf_Internal_Note *note;
+ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
 {
   switch (note->descsz)
     {
-      default:
-       return FALSE;
+    default:
+      return FALSE;
 
-      case 128:                /* Linux/PPC elf_prpsinfo.  */
-       elf_tdata (abfd)->core_program
-        = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
-       elf_tdata (abfd)->core_command
-        = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
+    case 128:          /* Linux/PPC elf_prpsinfo.  */
+      elf_tdata (abfd)->core_program
+       = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
+      elf_tdata (abfd)->core_command
+       = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
     }
 
   /* Note that for some reason, a spurious space is tacked
@@ -5286,39 +5737,24 @@ ppc_elf_grok_psinfo (abfd, note)
 /* Very simple linked list structure for recording apuinfo values.  */
 typedef struct apuinfo_list
 {
-  struct apuinfo_list *        next;
-  unsigned long         value;
+  struct apuinfo_list *next;
+  unsigned long value;
 }
 apuinfo_list;
 
-static apuinfo_list * head;
-
-static void          apuinfo_list_init    PARAMS ((void));
-static void          apuinfo_list_add     PARAMS ((unsigned long));
-static unsigned      apuinfo_list_length  PARAMS ((void));
-static unsigned long apuinfo_list_element PARAMS ((unsigned long));
-static void          apuinfo_list_finish  PARAMS ((void));
-
-extern void          ppc_elf_begin_write_processing
-  PARAMS ((bfd *, struct bfd_link_info *));
-extern void          ppc_elf_final_write_processing
-  PARAMS ((bfd *, bfd_boolean));
-extern bfd_boolean   ppc_elf_write_section
-  PARAMS ((bfd *, asection *, bfd_byte *));
-
+static apuinfo_list *head;
 
 
 static void
-apuinfo_list_init PARAMS ((void))
+apuinfo_list_init (void)
 {
   head = NULL;
 }
 
 static void
-apuinfo_list_add (value)
-     unsigned long value;
+apuinfo_list_add (unsigned long value)
 {
-  apuinfo_list * entry = head;
+  apuinfo_list *entry = head;
 
   while (entry != NULL)
     {
@@ -5337,9 +5773,9 @@ apuinfo_list_add (value)
 }
 
 static unsigned
-apuinfo_list_length PARAMS ((void))
+apuinfo_list_length (void)
 {
-  apuinfo_list * entry;
+  apuinfo_list *entry;
   unsigned long count;
 
   for (entry = head, count = 0;
@@ -5351,8 +5787,7 @@ apuinfo_list_length PARAMS ((void))
 }
 
 static inline unsigned long
-apuinfo_list_element (number)
-     unsigned long number;
+apuinfo_list_element (unsigned long number)
 {
   apuinfo_list * entry;
 
@@ -5365,13 +5800,13 @@ apuinfo_list_element (number)
 }
 
 static void
-apuinfo_list_finish PARAMS ((void))
+apuinfo_list_finish (void)
 {
-  apuinfo_list * entry;
+  apuinfo_list *entry;
 
   for (entry = head; entry;)
     {
-      apuinfo_list * next = entry->next;
+      apuinfo_list *next = entry->next;
       free (entry);
       entry = next;
     }
@@ -5379,27 +5814,25 @@ apuinfo_list_finish PARAMS ((void))
   head = NULL;
 }
 
-#define APUINFO_SECTION_NAME ".PPC.EMB.apuinfo"
-#define APUINFO_LABEL        "APUinfo"
+#define APUINFO_SECTION_NAME   ".PPC.EMB.apuinfo"
+#define APUINFO_LABEL          "APUinfo"
 
 /* Scan the input BFDs and create a linked list of
    the APUinfo values that will need to be emitted.  */
 
-void
-ppc_elf_begin_write_processing (abfd, link_info)
-     bfd *abfd;
-     struct bfd_link_info *link_info;
+static void
+ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
 {
-  bfd *         ibfd;
-  asection *    asec;
-  char *        buffer;
-  unsigned     num_input_sections;
+  bfd *ibfd;
+  asection *asec;
+  char *buffer;
+  unsigned num_input_sections;
   bfd_size_type        output_section_size;
-  unsigned      i;
-  unsigned      num_entries;
+  unsigned i;
+  unsigned num_entries;
   unsigned long        offset;
   unsigned long length;
-  const char *  error_message = NULL;
+  const char *error_message = NULL;
 
   if (link_info == NULL)
     return;
@@ -5439,9 +5872,8 @@ ppc_elf_begin_write_processing (abfd, link_info)
   /* Read in the input sections contents.  */
   for (ibfd = link_info->input_bfds; ibfd; ibfd = ibfd->link_next)
     {
-      unsigned long    datum;
-      char *           ptr;
-
+      unsigned long datum;
+      char *ptr;
 
       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
       if (asec == NULL)
@@ -5479,9 +5911,9 @@ ppc_elf_begin_write_processing (abfd, link_info)
       if (strcmp (ptr + 12, APUINFO_LABEL) != 0)
        goto fail;
 
-      /* Get the number of apuinfo entries.  */
+      /* Get the number of bytes used for apuinfo entries.  */
       datum = bfd_get_32 (ibfd, ptr + 4);
-      if ((datum * 4 + 20) != length)
+      if (datum + 20 != length)
        goto fail;
 
       /* Make sure that we do not run off the end of the section.  */
@@ -5489,8 +5921,8 @@ ppc_elf_begin_write_processing (abfd, link_info)
        goto fail;
 
       /* Scan the apuinfo section, building a list of apuinfo numbers.  */
-      for (i = 0; i < datum; i++)
-       apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + (i * 4)));
+      for (i = 0; i < datum; i += 4)
+       apuinfo_list_add (bfd_get_32 (ibfd, ptr + 20 + i));
 
       /* Update the offset.  */
       offset += length;
@@ -5504,7 +5936,7 @@ ppc_elf_begin_write_processing (abfd, link_info)
 
   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
 
-  if (! bfd_set_section_size  (abfd, asec, output_section_size))
+  if (! bfd_set_section_size (abfd, asec, output_section_size))
     ibfd = abfd,
       error_message = _("warning: unable to set size of %s section in %s");
 
@@ -5512,35 +5944,33 @@ ppc_elf_begin_write_processing (abfd, link_info)
   free (buffer);
 
   if (error_message)
-    _bfd_error_handler (error_message, APUINFO_SECTION_NAME,
-                       bfd_archive_filename (ibfd));
+    (*_bfd_error_handler) (error_message, APUINFO_SECTION_NAME,
+                          bfd_archive_filename (ibfd));
 }
 
 
 /* Prevent the output section from accumulating the input sections'
    contents.  We have already stored this in our linked list structure.  */
 
-bfd_boolean
-ppc_elf_write_section (abfd, asec, contents)
-     bfd * abfd ATTRIBUTE_UNUSED;
-     asection * asec;
-     bfd_byte * contents ATTRIBUTE_UNUSED;
+static bfd_boolean
+ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
+                      asection *asec,
+                      bfd_byte *contents ATTRIBUTE_UNUSED)
 {
-  return apuinfo_list_length () && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
+  return (apuinfo_list_length ()
+         && strcmp (asec->name, APUINFO_SECTION_NAME) == 0);
 }
 
 
 /* Finally we can generate the output section.  */
 
-void
-ppc_elf_final_write_processing (abfd, linker)
-     bfd * abfd;
-     bfd_boolean linker ATTRIBUTE_UNUSED;
+static void
+ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
 {
-  bfd_byte *    buffer;
-  asection *    asec;
-  unsigned      i;
-  unsigned      num_entries;
+  bfd_byte *buffer;
+  asection *asec;
+  unsigned i;
+  unsigned num_entries;
   bfd_size_type length;
 
   asec = bfd_get_section_by_name (abfd, APUINFO_SECTION_NAME);
@@ -5557,14 +5987,15 @@ ppc_elf_final_write_processing (abfd, linker)
   buffer = bfd_malloc (length);
   if (buffer == NULL)
     {
-      _bfd_error_handler (_("failed to allocate space for new APUinfo section."));
+      (*_bfd_error_handler)
+       (_("failed to allocate space for new APUinfo section."));
       return;
     }
 
   /* Create the apuinfo header.  */
   num_entries = apuinfo_list_length ();
   bfd_put_32 (abfd, sizeof APUINFO_LABEL, buffer);
-  bfd_put_32 (abfd, num_entries, buffer + 4);
+  bfd_put_32 (abfd, num_entries * 4, buffer + 4);
   bfd_put_32 (abfd, 0x2, buffer + 8);
   strcpy (buffer + 12, APUINFO_LABEL);
 
@@ -5576,15 +6007,44 @@ ppc_elf_final_write_processing (abfd, linker)
     }
 
   if (length != asec->_raw_size)
-    _bfd_error_handler (_("failed to compute new APUinfo section."));
+    (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
 
   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
-    _bfd_error_handler (_("failed to install new APUinfo section."));
+    (*_bfd_error_handler) (_("failed to install new APUinfo section."));
 
   free (buffer);
 
   apuinfo_list_finish ();
 }
+
+/* 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 ppc_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 },
+  { NULL,              0,      NULL,   0,
+    0,                 0 }
+};
 \f
 #define TARGET_LITTLE_SYM      bfd_elf32_powerpcle_vec
 #define TARGET_LITTLE_NAME     "elf32-powerpcle"
@@ -5592,7 +6052,11 @@ ppc_elf_final_write_processing (abfd, linker)
 #define TARGET_BIG_NAME                "elf32-powerpc"
 #define ELF_ARCH               bfd_arch_powerpc
 #define ELF_MACHINE_CODE       EM_PPC
+#ifdef __QNXTARGET__
+#define ELF_MAXPAGESIZE                0x1000
+#else
 #define ELF_MAXPAGESIZE                0x10000
+#endif
 #define elf_info_to_howto      ppc_elf_info_to_howto
 
 #ifdef  EM_CYGNUS_POWERPC
@@ -5611,11 +6075,12 @@ ppc_elf_final_write_processing (abfd, linker)
 #define elf_backend_plt_header_size    PLT_INITIAL_ENTRY_SIZE
 #define elf_backend_rela_normal                1
 
+#define bfd_elf32_mkobject                     ppc_elf_mkobject
 #define bfd_elf32_bfd_merge_private_bfd_data   ppc_elf_merge_private_bfd_data
-#define bfd_elf32_bfd_relax_section             ppc_elf_relax_section
+#define bfd_elf32_bfd_relax_section            ppc_elf_relax_section
 #define bfd_elf32_bfd_reloc_type_lookup                ppc_elf_reloc_type_lookup
 #define bfd_elf32_bfd_set_private_flags                ppc_elf_set_private_flags
-#define bfd_elf32_bfd_link_hash_table_create   ppc_elf_link_hash_table_create
+#define bfd_elf32_bfd_link_hash_table_create   ppc_elf_link_hash_table_create
 
 #define elf_backend_object_p                   ppc_elf_object_p
 #define elf_backend_gc_mark_hook               ppc_elf_gc_mark_hook
@@ -5636,8 +6101,9 @@ ppc_elf_final_write_processing (abfd, linker)
 #define elf_backend_grok_prstatus              ppc_elf_grok_prstatus
 #define elf_backend_grok_psinfo                        ppc_elf_grok_psinfo
 #define elf_backend_reloc_type_class           ppc_elf_reloc_type_class
-#define elf_backend_begin_write_processing      ppc_elf_begin_write_processing
-#define elf_backend_final_write_processing      ppc_elf_final_write_processing
-#define elf_backend_write_section               ppc_elf_write_section
+#define elf_backend_begin_write_processing     ppc_elf_begin_write_processing
+#define elf_backend_final_write_processing     ppc_elf_final_write_processing
+#define elf_backend_write_section              ppc_elf_write_section
+#define elf_backend_special_sections           ppc_elf_special_sections
 
 #include "elf32-target.h"