hppa-linux TLS relocs
[deliverable/binutils-gdb.git] / bfd / elf32-ppc.c
index 35a505c4bc56cd9946c6d5f49fbd89189c44e602..bd62c42518df575b4f26c1ccdebe2ba532b3980c 100644 (file)
@@ -1,5 +1,5 @@
 /* PowerPC-specific support for 32-bit ELF
-   Copyright (C) 1994-2016 Free Software Foundation, Inc.
+   Copyright (C) 1994-2017 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -134,7 +134,7 @@ static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
 #define VXWORKS_PLT_NON_JMP_SLOT_RELOCS 3
 /* The number of relocations in the PLTResolve slot. */
 #define VXWORKS_PLTRESOLVE_RELOCS 2
-/* The number of relocations in the PLTResolve slot when when creating
+/* The number of relocations in the PLTResolve slot when creating
    a shared library. */
 #define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
 
@@ -1471,7 +1471,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         "R_PPC_VLE_LO16D",     /* name */
         FALSE,                 /* partial_inplace */
         0,                     /* src_mask */
-        0x1f007ff,             /* dst_mask */
+        0x3e007ff,             /* dst_mask */
         FALSE),                /* pcrel_offset */
 
   /* Bits 16-31 split16a format.  */
@@ -1501,7 +1501,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         "R_PPC_VLE_HI16D",     /* name */
         FALSE,                 /* partial_inplace */
         0,                     /* src_mask */
-        0x1f007ff,             /* dst_mask */
+        0x3e007ff,             /* dst_mask */
         FALSE),                /* pcrel_offset */
 
   /* Bits 16-31 (High Adjusted) in split16a format.  */
@@ -1531,7 +1531,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         "R_PPC_VLE_HA16D",     /* name */
         FALSE,                 /* partial_inplace */
         0,                     /* src_mask */
-        0x1f007ff,             /* dst_mask */
+        0x3e007ff,             /* dst_mask */
         FALSE),                /* pcrel_offset */
 
   /* This reloc is like R_PPC_EMB_SDA21 but only applies to e_add16i
@@ -1593,7 +1593,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         "R_PPC_VLE_SDAREL_LO16D", /* name */
         FALSE,                 /* partial_inplace */
         0,                     /* src_mask */
-        0x1f007ff,             /* dst_mask */
+        0x3e007ff,             /* dst_mask */
         FALSE),                /* pcrel_offset */
 
   /* Bits 16-31 relative to _SDA_BASE_ in split16a format.  */
@@ -1623,7 +1623,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         "R_PPC_VLE_SDAREL_HI16D", /* name */
         FALSE,                 /* partial_inplace */
         0,                     /* src_mask */
-        0x1f007ff,             /* dst_mask */
+        0x3e007ff,             /* dst_mask */
         FALSE),                /* pcrel_offset */
 
   /* Bits 16-31 (HA) relative to _SDA_BASE split16a format.  */
@@ -1653,7 +1653,22 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         "R_PPC_VLE_SDAREL_HA16D", /* name */
         FALSE,                 /* partial_inplace */
         0,                     /* src_mask */
-        0x1f007ff,             /* dst_mask */
+        0x3e007ff,             /* dst_mask */
+        FALSE),                /* pcrel_offset */
+
+  /* e_li split20 format.  */
+  HOWTO (R_PPC_VLE_ADDR20,     /* type */
+        16,                    /* rightshift */
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        20,                    /* bitsize */
+        FALSE,                 /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont, /* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_PPC_VLE_ADDR20",    /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0x1f07ff,              /* dst_mask */
         FALSE),                /* pcrel_offset */
 
   HOWTO (R_PPC_IRELATIVE,      /* type */
@@ -1746,6 +1761,21 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0x1fffc1,              /* dst_mask */
         TRUE),                 /* pcrel_offset */
 
+  /* A split-field reloc for addpcis, non-relative (gas internal use only).  */
+  HOWTO (R_PPC_16DX_HA,                /* type */
+        16,                    /* rightshift */
+        2,                     /* size (0 = byte, 1 = short, 2 = long) */
+        16,                    /* bitsize */
+        FALSE,                 /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_signed, /* complain_on_overflow */
+        ppc_elf_addr16_ha_reloc, /* special_function */
+        "R_PPC_16DX_HA",       /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0x1fffc1,              /* dst_mask */
+        FALSE),                /* pcrel_offset */
+
   /* GNU extension to record C++ vtable hierarchy.  */
   HOWTO (R_PPC_GNU_VTINHERIT,  /* type */
         0,                     /* rightshift */
@@ -1791,56 +1821,6 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0xffff,                /* dst_mask */
         FALSE),                /* pcrel_offset */
 };
-
-/* External 32-bit PPC structure for PRPSINFO.  This structure is
-   ABI-defined, thus we choose to use char arrays here in order to
-   avoid dealing with different types in different architectures.
-
-   The PPC 32-bit structure uses int for `pr_uid' and `pr_gid' while
-   most non-PPC architectures use `short int'.
-
-   This structure will ultimately be written in the corefile's note
-   section, as the PRPSINFO.  */
-
-struct elf_external_ppc_linux_prpsinfo32
-  {
-    char pr_state;                     /* Numeric process state.  */
-    char pr_sname;                     /* Char for pr_state.  */
-    char pr_zomb;                      /* Zombie.  */
-    char pr_nice;                      /* Nice val.  */
-    char pr_flag[4];                   /* Flags.  */
-    char pr_uid[4];
-    char pr_gid[4];
-    char pr_pid[4];
-    char pr_ppid[4];
-    char pr_pgrp[4];
-    char pr_sid[4];
-    char pr_fname[16];                 /* Filename of executable.  */
-    char pr_psargs[80];                        /* Initial part of arg list.  */
-  };
-
-/* Helper function to copy an elf_internal_linux_prpsinfo in host
-   endian to an elf_external_ppc_linux_prpsinfo32 in target endian.  */
-
-static inline void
-swap_ppc_linux_prpsinfo32_out (bfd *obfd,
-                              const struct elf_internal_linux_prpsinfo *from,
-                              struct elf_external_ppc_linux_prpsinfo32 *to)
-{
-  bfd_put_8 (obfd, from->pr_state, &to->pr_state);
-  bfd_put_8 (obfd, from->pr_sname, &to->pr_sname);
-  bfd_put_8 (obfd, from->pr_zomb, &to->pr_zomb);
-  bfd_put_8 (obfd, from->pr_nice, &to->pr_nice);
-  bfd_put_32 (obfd, from->pr_flag, to->pr_flag);
-  bfd_put_32 (obfd, from->pr_uid, to->pr_uid);
-  bfd_put_32 (obfd, from->pr_gid, to->pr_gid);
-  bfd_put_32 (obfd, from->pr_pid, to->pr_pid);
-  bfd_put_32 (obfd, from->pr_ppid, to->pr_ppid);
-  bfd_put_32 (obfd, from->pr_pgrp, to->pr_pgrp);
-  bfd_put_32 (obfd, from->pr_sid, to->pr_sid);
-  strncpy (to->pr_fname, from->pr_fname, sizeof (to->pr_fname));
-  strncpy (to->pr_psargs, from->pr_psargs, sizeof (to->pr_psargs));
-}
 \f
 /* Initialize the ppc_elf_howto_table, so that linear accesses can be done.  */
 
@@ -2002,6 +1982,7 @@ ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
     case BFD_RELOC_LO16_PCREL:         r = R_PPC_REL16_LO;             break;
     case BFD_RELOC_HI16_PCREL:         r = R_PPC_REL16_HI;             break;
     case BFD_RELOC_HI16_S_PCREL:       r = R_PPC_REL16_HA;             break;
+    case BFD_RELOC_PPC_16DX_HA:                r = R_PPC_16DX_HA;              break;
     case BFD_RELOC_PPC_REL16DX_HA:     r = R_PPC_REL16DX_HA;           break;
     case BFD_RELOC_VTABLE_INHERIT:     r = R_PPC_GNU_VTINHERIT;        break;
     case BFD_RELOC_VTABLE_ENTRY:       r = R_PPC_GNU_VTENTRY;          break;
@@ -2029,7 +2010,7 @@ ppc_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
 /* Set the howto pointer for a PowerPC ELF reloc.  */
 
 static void
-ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
+ppc_elf_info_to_howto (bfd *abfd,
                       arelent *cache_ptr,
                       Elf_Internal_Rela *dst)
 {
@@ -2066,10 +2047,10 @@ ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
 /* Handle the R_PPC_ADDR16_HA and R_PPC_REL16_HA relocs.  */
 
 static bfd_reloc_status_type
-ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
+ppc_elf_addr16_ha_reloc (bfd *abfd,
                         arelent *reloc_entry,
                         asymbol *symbol,
-                        void *data ATTRIBUTE_UNUSED,
+                        void *data,
                         asection *input_section,
                         bfd *output_bfd,
                         char **error_message ATTRIBUTE_UNUSED)
@@ -2377,20 +2358,6 @@ ppc_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
   return TRUE;
 }
 
-char *
-elfcore_write_ppc_linux_prpsinfo32
-  (bfd *abfd,
-   char *buf,
-   int *bufsiz,
-   const struct elf_internal_linux_prpsinfo *prpsinfo)
-{
-  struct elf_external_ppc_linux_prpsinfo32 data;
-
-  swap_ppc_linux_prpsinfo32_out (abfd, prpsinfo, &data);
-  return elfcore_write_note (abfd, buf, bufsiz,
-                            "CORE", NT_PRPSINFO, &data, sizeof (data));
-}
-
 static char *
 ppc_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type, ...)
 {
@@ -2593,7 +2560,7 @@ ppc_elf_modify_segment_map (bfd *abfd,
       amt += (m->count - j - 1) * sizeof (asection *);
       n = (struct elf_segment_map *) bfd_zalloc (abfd, amt);
       if (n == NULL)
-        return FALSE;
+       return FALSE;
 
       n->p_type = PT_LOAD;
       n->count = m->count - j;
@@ -2632,7 +2599,7 @@ static struct bfd_elf_special_section ppc_alt_plt =
   { STRING_COMMA_LEN (".plt"),             0, SHT_PROGBITS, SHF_ALLOC };
 
 static const struct bfd_elf_special_section *
-ppc_elf_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
+ppc_elf_get_sec_type_attr (bfd *abfd, asection *sec)
 {
   const struct bfd_elf_special_section *ssect;
 
@@ -2833,7 +2800,7 @@ ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
     free (buffer);
 
   if (error_message)
-    _bfd_error_handler (error_message, ibfd, APUINFO_SECTION_NAME);
+    _bfd_error_handler (error_message, APUINFO_SECTION_NAME, ibfd);
 }
 
 /* Prevent the output section from accumulating the input sections'
@@ -3176,9 +3143,9 @@ struct plt_entry
   bfd_vma glink_offset;
 };
 
-/* Of those relocs that might be copied as dynamic relocs, this function
-   selects those that must be copied when linking a shared library,
-   even when the symbol is local.  */
+/* Of those relocs that might be copied as dynamic relocs, this
+   function selects those that must be copied when linking a shared
+   library or PIE, even when the symbol is local.  */
 
 static int
 must_be_dyn_reloc (struct bfd_link_info *info,
@@ -3187,6 +3154,10 @@ must_be_dyn_reloc (struct bfd_link_info *info,
   switch (r_type)
     {
     default:
+      /* Only relative relocs can be resolved when the object load
+        address isn't fixed.  DTPREL32 is excluded because the
+        dynamic linker needs to differentiate global dynamic from
+        local dynamic __tls_index pairs when PPC_OPT_TLS is set.  */
       return 1;
 
     case R_PPC_REL24:
@@ -3201,7 +3172,9 @@ must_be_dyn_reloc (struct bfd_link_info *info,
     case R_PPC_TPREL16_LO:
     case R_PPC_TPREL16_HI:
     case R_PPC_TPREL16_HA:
-      return !bfd_link_executable (info);
+      /* These relocations are relative but in a shared library the
+        linker doesn't know the thread pointer base.  */
+      return bfd_link_dll (info);
     }
 }
 
@@ -3276,8 +3249,6 @@ struct ppc_elf_link_hash_table
 
   /* Short-cuts to get to dynamic linker sections.  */
   asection *glink;
-  asection *dynbss;
-  asection *relbss;
   asection *dynsbss;
   asection *relsbss;
   elf_linker_section_t sdata[2];
@@ -3313,6 +3284,14 @@ struct ppc_elf_link_hash_table
   /* True if the target system is VxWorks.  */
   unsigned int is_vxworks:1;
 
+  /* Whether there exist local gnu indirect function resolvers,
+     referenced by dynamic relocations.  */
+  unsigned int local_ifunc_resolver:1;
+  unsigned int maybe_local_ifunc_resolver:1;
+
+  /* Set if tls optimization is enabled.  */
+  unsigned int do_tls_opt:1;
+
   /* The size of PLT entries.  */
   int plt_entry_size;
   /* The distance between adjacent PLT slots.  */
@@ -3556,7 +3535,6 @@ ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
       && !ppc_elf_create_glink (abfd, info))
     return FALSE;
 
-  htab->dynbss = bfd_get_linker_section (abfd, ".dynbss");
   s = bfd_make_section_anyway_with_flags (abfd, ".dynsbss",
                                          SEC_ALLOC | SEC_LINKER_CREATED);
   htab->dynsbss = s;
@@ -3565,7 +3543,6 @@ ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
 
   if (! bfd_link_pic (info))
     {
-      htab->relbss = bfd_get_linker_section (abfd, ".rela.bss");
       flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
               | SEC_IN_MEMORY | SEC_LINKER_CREATED);
       s = bfd_make_section_anyway_with_flags (abfd, ".rela.sbss", flags);
@@ -3610,7 +3587,8 @@ ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
        && edir->elf.dynamic_adjusted))
     edir->elf.non_got_ref |= eind->elf.non_got_ref;
 
-  edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
+  if (edir->elf.versioned != versioned_hidden)
+    edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
   edir->elf.ref_regular |= eind->elf.ref_regular;
   edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
   edir->elf.needs_plt |= eind->elf.needs_plt;
@@ -4030,7 +4008,7 @@ ppc_elf_check_relocs (bfd *abfd,
 
          /* PR15323, ref flags aren't set for references in the same
             object.  */
-         h->root.non_ir_ref = 1;
+         h->root.non_ir_ref_regular = 1;
        }
 
       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
@@ -4126,7 +4104,7 @@ ppc_elf_check_relocs (bfd *abfd,
        case R_PPC_GOT_TPREL16_LO:
        case R_PPC_GOT_TPREL16_HI:
        case R_PPC_GOT_TPREL16_HA:
-         if (bfd_link_pic (info))
+         if (bfd_link_dll (info))
            info->flags |= DF_STATIC_TLS;
          tls_type = TLS_TLS | TLS_TPREL;
          goto dogottls;
@@ -4234,6 +4212,7 @@ ppc_elf_check_relocs (bfd *abfd,
        case R_PPC_VLE_HI16D:
        case R_PPC_VLE_HA16A:
        case R_PPC_VLE_HA16D:
+       case R_PPC_VLE_ADDR20:
          break;
 
        case R_PPC_EMB_SDA2REL:
@@ -4301,7 +4280,7 @@ ppc_elf_check_relocs (bfd *abfd,
                     table entry for a non-ifunc local symbol.  */
                  info->callbacks->einfo
                    /* xgettext:c-format */
-                   (_("%P: %H: %s reloc against local symbol\n"),
+                   (_("%H: %s reloc against local symbol\n"),
                     abfd, sec, rel->r_offset,
                     ppc_elf_howto_table[r_type]->name);
                  bfd_set_error (bfd_error_bad_value);
@@ -4354,6 +4333,7 @@ ppc_elf_check_relocs (bfd *abfd,
        case R_PPC_RELAX:
        case R_PPC_RELAX_PLT:
        case R_PPC_RELAX_PLTREL24:
+       case R_PPC_16DX_HA:
          break;
 
          /* These should only appear in dynamic objects.  */
@@ -4404,13 +4384,13 @@ ppc_elf_check_relocs (bfd *abfd,
            return FALSE;
          break;
 
-         /* We shouldn't really be seeing these.  */
+         /* We shouldn't really be seeing TPREL32.  */
        case R_PPC_TPREL32:
        case R_PPC_TPREL16:
        case R_PPC_TPREL16_LO:
        case R_PPC_TPREL16_HI:
        case R_PPC_TPREL16_HA:
-         if (bfd_link_pic (info))
+         if (bfd_link_dll (info))
            info->flags |= DF_STATIC_TLS;
          goto dodyn;
 
@@ -4880,9 +4860,9 @@ ppc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
          error = TRUE;
          _bfd_error_handler
            /* xgettext:c-format */
-           (_("%B: uses different e_flags (0x%lx) fields "
-              "than previous modules (0x%lx)"),
-            ibfd, (long) new_flags, (long) old_flags);
+           (_("%B: uses different e_flags (%#x) fields "
+              "than previous modules (%#x)"),
+            ibfd, new_flags, old_flags);
        }
 
       if (error)
@@ -4907,7 +4887,7 @@ ppc_elf_vle_split16 (bfd *input_bfd,
   unsigned int insn, opcode, top5;
 
   insn = bfd_get_32 (input_bfd, loc);
-  opcode = insn & 0xf300f800;
+  opcode = insn & 0xfc00f800;
   if (opcode == E_OR2I_INSN
       || opcode == E_AND2I_DOT_INSN
       || opcode == E_OR2IS_INSN
@@ -4945,12 +4925,29 @@ ppc_elf_vle_split16 (bfd *input_bfd,
        }
     }
   top5 = value & 0xf800;
-  top5 = top5 << (split16_format == split16a_type ? 5 : 9);
-  insn &= (split16_format == split16a_type ? ~0x1f07ff : ~0x1f007ff);
+  top5 = top5 << (split16_format == split16a_type ? 5 : 10);
+  insn &= (split16_format == split16a_type ? ~0x1f07ff : ~0x3e007ff);
   insn |= top5;
   insn |= value & 0x7ff;
   bfd_put_32 (input_bfd, insn, loc);
 }
+
+static void
+ppc_elf_vle_split20 (bfd *output_bfd, bfd_byte *loc, bfd_vma value)
+{
+  unsigned int insn;
+
+  insn = bfd_get_32 (output_bfd, loc);
+  /* We have an li20 field, bits 17..20, 11..15, 21..31.  */
+  /* Top 4 bits of value to 17..20.  */
+  insn |= (value & 0xf0000) >> 5;
+  /* Next 5 bits of the value to 11..15.  */
+  insn |= (value & 0xf800) << 5;
+  /* And the final 11 bits of the value to bits 21 to 31.  */
+  insn |= value & 0x7ff;
+  bfd_put_32 (output_bfd, insn, loc);
+}
+
 \f
 /* Choose which PLT scheme to use, and set .plt flags appropriately.
    Returns -1 on error, 0 for old PLT, 1 for new PLT.  */
@@ -4977,8 +4974,7 @@ ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
                   || h->needs_plt)
               && h->ref_regular
               && !(SYMBOL_CALLS_LOCAL (info, h)
-                   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
-                       && h->root.type == bfd_link_hash_undefweak)))
+                   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
        {
          /* Profiling of shared libs (and pies) is not supported with
             secure plt, because ppc32 does profiling before a
@@ -5068,182 +5064,6 @@ ppc_elf_gc_mark_hook (asection *sec,
 
   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
 }
-
-/* Update the got, plt and dynamic reloc reference counts for the
-   section being removed.  */
-
-static bfd_boolean
-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;
-  struct elf_link_hash_entry **sym_hashes;
-  bfd_signed_vma *local_got_refcounts;
-  const Elf_Internal_Rela *rel, *relend;
-  asection *got2;
-
-  if (bfd_link_relocatable (info))
-    return TRUE;
-
-  if ((sec->flags & SEC_ALLOC) == 0)
-    return TRUE;
-
-  elf_section_data (sec)->local_dynrel = NULL;
-
-  htab = ppc_elf_hash_table (info);
-  symtab_hdr = &elf_symtab_hdr (abfd);
-  sym_hashes = elf_sym_hashes (abfd);
-  local_got_refcounts = elf_local_got_refcounts (abfd);
-  got2 = bfd_get_section_by_name (abfd, ".got2");
-
-  relend = relocs + sec->reloc_count;
-  for (rel = relocs; rel < relend; rel++)
-    {
-      unsigned long r_symndx;
-      enum elf_ppc_reloc_type r_type;
-      struct elf_link_hash_entry *h = NULL;
-
-      r_symndx = ELF32_R_SYM (rel->r_info);
-      if (r_symndx >= symtab_hdr->sh_info)
-       {
-         struct elf_dyn_relocs **pp, *p;
-         struct ppc_elf_link_hash_entry *eh;
-
-         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-         while (h->root.type == bfd_link_hash_indirect
-                || h->root.type == bfd_link_hash_warning)
-           h = (struct elf_link_hash_entry *) h->root.u.i.link;
-         eh = (struct ppc_elf_link_hash_entry *) h;
-
-         for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
-           if (p->sec == sec)
-             {
-               /* Everything must go for SEC.  */
-               *pp = p->next;
-               break;
-             }
-       }
-
-      r_type = ELF32_R_TYPE (rel->r_info);
-      if (!htab->is_vxworks
-         && h == NULL
-         && local_got_refcounts != NULL
-         && (!bfd_link_pic (info)
-             || is_branch_reloc (r_type)))
-       {
-         struct plt_entry **local_plt = (struct plt_entry **)
-           (local_got_refcounts + symtab_hdr->sh_info);
-         char *local_got_tls_masks = (char *)
-           (local_plt + symtab_hdr->sh_info);
-         if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
-           {
-             struct plt_entry **ifunc = local_plt + r_symndx;
-             bfd_vma addend = 0;
-             struct plt_entry *ent;
-
-             if (r_type == R_PPC_PLTREL24 && bfd_link_pic (info))
-               addend = rel->r_addend;
-             ent = find_plt_ent (ifunc, got2, addend);
-             if (ent->plt.refcount > 0)
-               ent->plt.refcount -= 1;
-             continue;
-           }
-       }
-
-      switch (r_type)
-       {
-       case R_PPC_GOT_TLSLD16:
-       case R_PPC_GOT_TLSLD16_LO:
-       case R_PPC_GOT_TLSLD16_HI:
-       case R_PPC_GOT_TLSLD16_HA:
-       case R_PPC_GOT_TLSGD16:
-       case R_PPC_GOT_TLSGD16_LO:
-       case R_PPC_GOT_TLSGD16_HI:
-       case R_PPC_GOT_TLSGD16_HA:
-       case R_PPC_GOT_TPREL16:
-       case R_PPC_GOT_TPREL16_LO:
-       case R_PPC_GOT_TPREL16_HI:
-       case R_PPC_GOT_TPREL16_HA:
-       case R_PPC_GOT_DTPREL16:
-       case R_PPC_GOT_DTPREL16_LO:
-       case R_PPC_GOT_DTPREL16_HI:
-       case R_PPC_GOT_DTPREL16_HA:
-       case R_PPC_GOT16:
-       case R_PPC_GOT16_LO:
-       case R_PPC_GOT16_HI:
-       case R_PPC_GOT16_HA:
-         if (h != NULL)
-           {
-             if (h->got.refcount > 0)
-               h->got.refcount--;
-             if (!bfd_link_pic (info))
-               {
-                 struct plt_entry *ent;
-
-                 ent = find_plt_ent (&h->plt.plist, NULL, 0);
-                 if (ent != NULL && ent->plt.refcount > 0)
-                   ent->plt.refcount -= 1;
-               }
-           }
-         else if (local_got_refcounts != NULL)
-           {
-             if (local_got_refcounts[r_symndx] > 0)
-               local_got_refcounts[r_symndx]--;
-           }
-         break;
-
-       case R_PPC_REL24:
-       case R_PPC_REL14:
-       case R_PPC_REL14_BRTAKEN:
-       case R_PPC_REL14_BRNTAKEN:
-       case R_PPC_REL32:
-         if (h == NULL || h == htab->elf.hgot)
-           break;
-         /* Fall through.  */
-
-       case R_PPC_ADDR32:
-       case R_PPC_ADDR24:
-       case R_PPC_ADDR16:
-       case R_PPC_ADDR16_LO:
-       case R_PPC_ADDR16_HI:
-       case R_PPC_ADDR16_HA:
-       case R_PPC_ADDR14:
-       case R_PPC_ADDR14_BRTAKEN:
-       case R_PPC_ADDR14_BRNTAKEN:
-       case R_PPC_UADDR32:
-       case R_PPC_UADDR16:
-         if (bfd_link_pic (info))
-           break;
-         /* Fall through.  */
-
-       case R_PPC_PLT32:
-       case R_PPC_PLTREL24:
-       case R_PPC_PLTREL32:
-       case R_PPC_PLT16_LO:
-       case R_PPC_PLT16_HI:
-       case R_PPC_PLT16_HA:
-         if (h != NULL)
-           {
-             bfd_vma addend = 0;
-             struct plt_entry *ent;
-
-             if (r_type == R_PPC_PLTREL24 && bfd_link_pic (info))
-               addend = rel->r_addend;
-             ent = find_plt_ent (&h->plt.plist, got2, addend);
-             if (ent != NULL && ent->plt.refcount > 0)
-               ent->plt.refcount -= 1;
-           }
-         break;
-
-       default:
-         break;
-       }
-    }
-  return TRUE;
-}
 \f
 /* Set plt output section type, htab->tls_get_addr, and call the
    generic ELF tls_setup function.  */
@@ -5278,8 +5098,7 @@ ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
              && (tga->type == STT_FUNC
                  || tga->needs_plt)
              && !(SYMBOL_CALLS_LOCAL (info, tga)
-                  || (ELF_ST_VISIBILITY (tga->other) != STV_DEFAULT
-                      && tga->root.type == bfd_link_hash_undefweak)))
+                  || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga)))
            {
              struct plt_entry *ent;
              for (ent = tga->plt.plist; ent != NULL; ent = ent->next)
@@ -5290,7 +5109,7 @@ ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
                  tga->root.type = bfd_link_hash_indirect;
                  tga->root.u.i.link = &opt->root;
                  ppc_elf_copy_indirect_symbol (info, opt, tga);
-                 opt->forced_local = 0;
+                 opt->mark = 1;
                  if (opt->dynindx != -1)
                    {
                      /* Use __tls_get_addr_opt in dynamic relocations.  */
@@ -5601,13 +5420,15 @@ ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
              symtab_hdr->contents = (unsigned char *) locsyms;
          }
       }
+  htab->do_tls_opt = 1;
   return TRUE;
 }
 \f
 /* Return true if we have dynamic relocs that apply to read-only sections.  */
 
 static bfd_boolean
-readonly_dynrelocs (struct elf_link_hash_entry *h)
+readonly_dynrelocs (struct elf_link_hash_entry *h,
+                   struct bfd_link_info *info)
 {
   struct elf_dyn_relocs *p;
 
@@ -5618,7 +5439,13 @@ readonly_dynrelocs (struct elf_link_hash_entry *h)
       if (s != NULL
          && ((s->flags & (SEC_READONLY | SEC_ALLOC))
              == (SEC_READONLY | SEC_ALLOC)))
-       return TRUE;
+       {
+         if (info)
+           info->callbacks->minfo (_("%B: dynamic relocation in read-only section `%A'\n"),
+                                   p->sec->owner, p->sec);
+
+         return TRUE;
+       }
     }
   return FALSE;
 }
@@ -5665,8 +5492,7 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
       if (ent == NULL
          || (h->type != STT_GNU_IFUNC
              && (SYMBOL_CALLS_LOCAL (info, h)
-                 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
-                     && h->root.type == bfd_link_hash_undefweak))))
+                 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))))
        {
          /* A PLT entry is not required/allowed when:
 
@@ -5681,41 +5507,47 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
          h->plt.plist = NULL;
          h->needs_plt = 0;
          h->pointer_equality_needed = 0;
+         /* After adjust_dynamic_symbol, non_got_ref set in the
+            non-pic case means that dyn_relocs for this symbol should
+            be discarded.  We either want the symbol to remain
+            undefined, or we have a local definition of some sort.
+            The "local definition" for non-function symbols may be
+            due to creating a local definition in .dynbss, and for
+            function symbols, defining the symbol on the PLT call
+            stub code.  Set non_got_ref here to ensure undef weaks
+            stay undefined.  */
+         h->non_got_ref = 1;
        }
       else
        {
          /* Taking a function's address in a read/write section
             doesn't require us to define the function symbol in the
             executable on a plt call stub.  A dynamic reloc can
-            be used instead.  */
-         if (h->pointer_equality_needed
-             && h->type != STT_GNU_IFUNC
+            be used instead, giving better runtime performance.
+            (Calls via that function pointer don't need to bounce
+            through the plt call stub.)  Similarly, use a dynamic
+            reloc for a weak reference when possible, allowing the
+            resolution of the symbol to be set at load time rather
+            than link time.  */
+         if ((h->pointer_equality_needed
+              || (h->non_got_ref
+                  && !h->ref_regular_nonweak
+                  && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
              && !htab->is_vxworks
              && !ppc_elf_hash_entry (h)->has_sda_refs
-             && !readonly_dynrelocs (h))
+             && !readonly_dynrelocs (h, NULL))
            {
              h->pointer_equality_needed = 0;
+             /* Say that we do want dynamic relocs.  */
              h->non_got_ref = 0;
+             /* If we haven't seen a branch reloc then we don't need
+                a plt entry.  */
+             if (!h->needs_plt)
+               h->plt.plist = NULL;
            }
-
-         /* After adjust_dynamic_symbol, non_got_ref set in the
-            non-shared case means that we have allocated space in
-            .dynbss for the symbol and thus dyn_relocs for this
-            symbol should be discarded.
-            If we get here we know we are making a PLT entry for this
-            symbol, and in an executable we'd normally resolve
-            relocations against this symbol to the PLT entry.  Allow
-            dynamic relocs if the reference is weak, and the dynamic
-            relocs will not cause text relocation.  */
-         else if (!h->ref_regular_nonweak
-                  && h->non_got_ref
-                  && h->type != STT_GNU_IFUNC
-                  && !htab->is_vxworks
-                  && !ppc_elf_hash_entry (h)->has_sda_refs
-                  && !readonly_dynrelocs (h))
-           h->non_got_ref = 0;
        }
       h->protected_def = 0;
+      /* Function symbols can't have copy relocs.  */
       return TRUE;
     }
   else
@@ -5789,7 +5621,7 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
       && !ppc_elf_hash_entry (h)->has_sda_refs
       && !htab->is_vxworks
       && !h->def_regular
-      && !readonly_dynrelocs (h))
+      && !readonly_dynrelocs (h, NULL))
     {
       h->non_got_ref = 0;
       return TRUE;
@@ -5810,8 +5642,10 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
 
   if (ppc_elf_hash_entry (h)->has_sda_refs)
     s = htab->dynsbss;
+  else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
+    s = htab->elf.sdynrelro;
   else
-    s = htab->dynbss;
+    s = htab->elf.sdynbss;
   BFD_ASSERT (s != NULL);
 
   /* We must generate a R_PPC_COPY reloc to tell the dynamic linker to
@@ -5824,8 +5658,10 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
 
       if (ppc_elf_hash_entry (h)->has_sda_refs)
        srel = htab->relsbss;
+      else if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
+       srel = htab->elf.sreldynrelro;
       else
-       srel = htab->relbss;
+       srel = htab->elf.srelbss;
       BFD_ASSERT (srel != NULL);
       srel->size += sizeof (Elf32_External_Rela);
       h->needs_copy = 1;
@@ -5924,6 +5760,25 @@ allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
   return where;
 }
 
+/* If H is undefined, make it dynamic if that makes sense.  */
+
+static bfd_boolean
+ensure_undef_dynamic (struct bfd_link_info *info,
+                     struct elf_link_hash_entry *h)
+{
+  struct elf_link_hash_table *htab = elf_hash_table (info);
+
+  if (htab->dynamic_sections_created
+      && ((info->dynamic_undefined_weak != 0
+          && h->root.type == bfd_link_hash_undefweak)
+         || h->root.type == bfd_link_hash_undefined)
+      && h->dynindx == -1
+      && !h->forced_local
+      && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
+    return bfd_elf_link_record_dynamic_symbol (info, h);
+  return TRUE;
+}
+
 /* Allocate space in associated reloc sections for dynamic relocs.  */
 
 static bfd_boolean
@@ -5933,175 +5788,12 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   struct ppc_elf_link_hash_entry *eh;
   struct ppc_elf_link_hash_table *htab;
   struct elf_dyn_relocs *p;
+  bfd_boolean dyn;
 
   if (h->root.type == bfd_link_hash_indirect)
     return TRUE;
 
   htab = ppc_elf_hash_table (info);
-  if (htab->elf.dynamic_sections_created
-      || h->type == STT_GNU_IFUNC)
-    {
-      struct plt_entry *ent;
-      bfd_boolean doneone = FALSE;
-      bfd_vma plt_offset = 0, glink_offset = 0;
-      bfd_boolean dyn;
-
-      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
-       if (ent->plt.refcount > 0)
-         {
-           /* Make sure this symbol is output as a dynamic symbol.  */
-           if (h->dynindx == -1
-               && !h->forced_local
-               && !h->def_regular
-               && htab->elf.dynamic_sections_created)
-             {
-               if (! bfd_elf_link_record_dynamic_symbol (info, h))
-                 return FALSE;
-             }
-
-           dyn = htab->elf.dynamic_sections_created;
-           if (bfd_link_pic (info)
-               || h->type == STT_GNU_IFUNC
-               || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
-             {
-               asection *s = htab->elf.splt;
-               if (!dyn || h->dynindx == -1)
-                 s = htab->elf.iplt;
-
-               if (htab->plt_type == PLT_NEW || !dyn || h->dynindx == -1)
-                 {
-                   if (!doneone)
-                     {
-                       plt_offset = s->size;
-                       s->size += 4;
-                     }
-                   ent->plt.offset = plt_offset;
-
-                   s = htab->glink;
-                   if (!doneone || bfd_link_pic (info))
-                     {
-                       glink_offset = s->size;
-                       s->size += GLINK_ENTRY_SIZE;
-                       if (h == htab->tls_get_addr
-                           && !htab->params->no_tls_get_addr_opt)
-                         s->size += TLS_GET_ADDR_GLINK_SIZE - GLINK_ENTRY_SIZE;
-                     }
-                   if (!doneone
-                       && !bfd_link_pic (info)
-                       && h->def_dynamic
-                       && !h->def_regular)
-                     {
-                       h->root.u.def.section = s;
-                       h->root.u.def.value = glink_offset;
-                     }
-                   ent->glink_offset = glink_offset;
-
-                   if (htab->params->emit_stub_syms
-                       && !add_stub_sym (ent, h, info))
-                     return FALSE;
-                 }
-               else
-                 {
-                   if (!doneone)
-                     {
-                       /* If this is the first .plt entry, make room
-                          for the special first entry.  */
-                       if (s->size == 0)
-                         s->size += htab->plt_initial_entry_size;
-
-                       /* The PowerPC PLT is actually composed of two
-                          parts, the first part is 2 words (for a load
-                          and a jump), and then there is a remaining
-                          word available at the end.  */
-                       plt_offset = (htab->plt_initial_entry_size
-                                     + (htab->plt_slot_size
-                                        * ((s->size
-                                            - htab->plt_initial_entry_size)
-                                           / htab->plt_entry_size)));
-
-                       /* If this symbol is not defined in a regular
-                          file, and we are not generating a shared
-                          library, then set the symbol to this location
-                          in the .plt.  This is to avoid text
-                          relocations, and is required to make
-                          function pointers compare as equal between
-                          the normal executable and the shared library.  */
-                       if (! bfd_link_pic (info)
-                           && h->def_dynamic
-                           && !h->def_regular)
-                         {
-                           h->root.u.def.section = s;
-                           h->root.u.def.value = plt_offset;
-                         }
-
-                       /* Make room for this entry.  */
-                       s->size += htab->plt_entry_size;
-                       /* After the 8192nd entry, room for two entries
-                          is allocated.  */
-                       if (htab->plt_type == PLT_OLD
-                           && (s->size - htab->plt_initial_entry_size)
-                               / htab->plt_entry_size
-                              > PLT_NUM_SINGLE_ENTRIES)
-                         s->size += htab->plt_entry_size;
-                     }
-                   ent->plt.offset = plt_offset;
-                 }
-
-               /* We also need to make an entry in the .rela.plt section.  */
-               if (!doneone)
-                 {
-                   if (!htab->elf.dynamic_sections_created
-                       || h->dynindx == -1)
-                     htab->elf.irelplt->size += sizeof (Elf32_External_Rela);
-                   else
-                     {
-                       htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
-
-                       if (htab->plt_type == PLT_VXWORKS)
-                         {
-                           /* Allocate space for the unloaded relocations.  */
-                           if (!bfd_link_pic (info)
-                               && htab->elf.dynamic_sections_created)
-                             {
-                               if (ent->plt.offset
-                                   == (bfd_vma) htab->plt_initial_entry_size)
-                                 {
-                                   htab->srelplt2->size
-                                     += (sizeof (Elf32_External_Rela)
-                                         * VXWORKS_PLTRESOLVE_RELOCS);
-                                 }
-
-                               htab->srelplt2->size
-                                 += (sizeof (Elf32_External_Rela)
-                                     * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
-                             }
-
-                           /* Every PLT entry has an associated GOT entry in
-                              .got.plt.  */
-                           htab->elf.sgotplt->size += 4;
-                         }
-                     }
-                   doneone = TRUE;
-                 }
-             }
-           else
-             ent->plt.offset = (bfd_vma) -1;
-         }
-       else
-         ent->plt.offset = (bfd_vma) -1;
-
-      if (!doneone)
-       {
-         h->plt.plist = NULL;
-         h->needs_plt = 0;
-       }
-    }
-  else
-    {
-      h->plt.plist = NULL;
-      h->needs_plt = 0;
-    }
-
   eh = (struct ppc_elf_link_hash_entry *) h;
   if (eh->elf.got.refcount > 0
       || (ELIMINATE_COPY_RELOCS
@@ -6111,18 +5803,11 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
          && eh->has_addr16_lo
          && htab->params->pic_fixup > 0))
     {
-      bfd_boolean dyn;
       unsigned int need;
 
       /* Make sure this symbol is output as a dynamic symbol.  */
-      if (eh->elf.dynindx == -1
-         && !eh->elf.forced_local
-         && eh->elf.type != STT_GNU_IFUNC
-         && htab->elf.dynamic_sections_created)
-       {
-         if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
-           return FALSE;
-       }
+      if (!ensure_undef_dynamic (info, &eh->elf))
+       return FALSE;
 
       need = 0;
       if ((eh->tls_mask & TLS_TLS) != 0)
@@ -6151,11 +5836,11 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       else
        {
          eh->elf.got.offset = allocate_got (htab, need);
-         dyn = htab->elf.dynamic_sections_created;
          if ((bfd_link_pic (info)
-              || 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))
+              || (htab->elf.dynamic_sections_created
+                  && eh->elf.dynindx != -1
+                  && !SYMBOL_REFERENCES_LOCAL (info, &eh->elf)))
+             && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &eh->elf))
            {
              asection *rsec = htab->elf.srelgot;
 
@@ -6173,25 +5858,39 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   else
     eh->elf.got.offset = (bfd_vma) -1;
 
-  if (eh->dyn_relocs == NULL
-      || !htab->elf.dynamic_sections_created)
-    return TRUE;
+  /* If no dynamic sections we can't have dynamic relocs, except for
+     IFUNCs which are handled even in static executables.  */
+  if (!htab->elf.dynamic_sections_created
+      && h->type != STT_GNU_IFUNC)
+    eh->dyn_relocs = NULL;
+
+  if (eh->dyn_relocs == NULL)
+    ;
 
   /* 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 (bfd_link_pic (info))
+  else if (bfd_link_pic (info))
     {
+      /* Discard relocs on undefined symbols that must be local.  */
+      if (h->root.type == bfd_link_hash_undefined
+         && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
+       eh->dyn_relocs = NULL;
+
+      /* Also discard relocs on undefined weak syms with non-default
+        visibility, or when dynamic_undefined_weak says so.  */
+      else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
+       eh->dyn_relocs = NULL;
+
       /* 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))
+      else if (SYMBOL_CALLS_LOCAL (info, h))
        {
          struct elf_dyn_relocs **pp;
 
@@ -6219,38 +5918,18 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
            }
        }
 
-      /* Discard relocs on undefined symbols that must be local.  */
-      if (eh->dyn_relocs != NULL
-         && h->root.type == bfd_link_hash_undefined
-         && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
-             || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
-       eh->dyn_relocs = NULL;
-
-      /* Also discard relocs on undefined weak syms with non-default
-        visibility.  */
-      if (eh->dyn_relocs != NULL
-         && h->root.type == bfd_link_hash_undefweak)
+      if (eh->dyn_relocs != NULL)
        {
-         if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
-           eh->dyn_relocs = NULL;
-
-         /* Make sure undefined weak symbols are output as a dynamic
-            symbol in PIEs.  */
-         else if (h->dynindx == -1
-                  && !h->forced_local
-                  && !h->def_regular)
-           {
-             if (! bfd_elf_link_record_dynamic_symbol (info, h))
-               return FALSE;
-           }
+         /* Make sure this symbol is output as a dynamic symbol.  */
+         if (!ensure_undef_dynamic (info, h))
+           return FALSE;
        }
     }
   else if (ELIMINATE_COPY_RELOCS)
     {
-      /* For the non-shared case, discard space for relocs against
+      /* For the non-pic case, discard space for relocs against
         symbols which turn out to need copy relocs or are not
         dynamic.  */
-
       if (!h->non_got_ref
          && !h->def_regular
          && !(h->protected_def
@@ -6258,27 +5937,18 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
               && eh->has_addr16_lo
               && htab->params->pic_fixup > 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->forced_local)
-           {
-             if (! bfd_elf_link_record_dynamic_symbol (info, h))
-               return FALSE;
-           }
+         /* Make sure this symbol is output as a dynamic symbol.  */
+         if (!ensure_undef_dynamic (info, h))
+           return FALSE;
 
-         /* If that succeeded, we know we'll be keeping all the
-            relocs.  */
-         if (h->dynindx != -1)
-           goto keep;
+         if (h->dynindx == -1)
+           eh->dyn_relocs = NULL;
        }
-
-      eh->dyn_relocs = NULL;
-
-    keep: ;
+      else
+       eh->dyn_relocs = NULL;
     }
 
-  /* Finally, allocate space.  */
+  /* Allocate space.  */
   for (p = eh->dyn_relocs; p != NULL; p = p->next)
     {
       asection *sreloc = elf_section_data (p->sec)->sreloc;
@@ -6287,6 +5957,152 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       sreloc->size += p->count * sizeof (Elf32_External_Rela);
     }
 
+  /* Handle PLT relocs.  Done last, after dynindx has settled.  */
+  dyn = htab->elf.dynamic_sections_created && h->dynindx != -1;
+  if (dyn || h->type == STT_GNU_IFUNC)
+    {
+      struct plt_entry *ent;
+      bfd_boolean doneone = FALSE;
+      bfd_vma plt_offset = 0, glink_offset = 0;
+
+      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
+       if (ent->plt.refcount > 0)
+         {
+           asection *s = htab->elf.splt;
+
+           if (!dyn)
+             s = htab->elf.iplt;
+
+           if (htab->plt_type == PLT_NEW || !dyn)
+             {
+               if (!doneone)
+                 {
+                   plt_offset = s->size;
+                   s->size += 4;
+                 }
+               ent->plt.offset = plt_offset;
+
+               s = htab->glink;
+               if (!doneone || bfd_link_pic (info))
+                 {
+                   glink_offset = s->size;
+                   s->size += GLINK_ENTRY_SIZE;
+                   if (h == htab->tls_get_addr
+                       && !htab->params->no_tls_get_addr_opt)
+                     s->size += TLS_GET_ADDR_GLINK_SIZE - GLINK_ENTRY_SIZE;
+                 }
+               if (!doneone
+                   && !bfd_link_pic (info)
+                   && h->def_dynamic
+                   && !h->def_regular)
+                 {
+                   h->root.u.def.section = s;
+                   h->root.u.def.value = glink_offset;
+                 }
+               ent->glink_offset = glink_offset;
+
+               if (htab->params->emit_stub_syms
+                   && !add_stub_sym (ent, h, info))
+                 return FALSE;
+             }
+           else
+             {
+               if (!doneone)
+                 {
+                   /* If this is the first .plt entry, make room
+                      for the special first entry.  */
+                   if (s->size == 0)
+                     s->size += htab->plt_initial_entry_size;
+
+                   /* The PowerPC PLT is actually composed of two
+                      parts, the first part is 2 words (for a load
+                      and a jump), and then there is a remaining
+                      word available at the end.  */
+                   plt_offset = (htab->plt_initial_entry_size
+                                 + (htab->plt_slot_size
+                                    * ((s->size
+                                        - htab->plt_initial_entry_size)
+                                       / htab->plt_entry_size)));
+
+                   /* If this symbol is not defined in a regular
+                      file, and we are not generating a shared
+                      library, then set the symbol to this location
+                      in the .plt.  This is to avoid text
+                      relocations, and is required to make
+                      function pointers compare as equal between
+                      the normal executable and the shared library.  */
+                   if (! bfd_link_pic (info)
+                       && h->def_dynamic
+                       && !h->def_regular)
+                     {
+                       h->root.u.def.section = s;
+                       h->root.u.def.value = plt_offset;
+                     }
+
+                   /* Make room for this entry.  */
+                   s->size += htab->plt_entry_size;
+                   /* After the 8192nd entry, room for two entries
+                      is allocated.  */
+                   if (htab->plt_type == PLT_OLD
+                       && (s->size - htab->plt_initial_entry_size)
+                       / htab->plt_entry_size
+                       > PLT_NUM_SINGLE_ENTRIES)
+                     s->size += htab->plt_entry_size;
+                 }
+               ent->plt.offset = plt_offset;
+             }
+
+           /* We also need to make an entry in the .rela.plt section.  */
+           if (!doneone)
+             {
+               if (!dyn)
+                 htab->elf.irelplt->size += sizeof (Elf32_External_Rela);
+               else
+                 {
+                   htab->elf.srelplt->size += sizeof (Elf32_External_Rela);
+
+                   if (htab->plt_type == PLT_VXWORKS)
+                     {
+                       /* Allocate space for the unloaded relocations.  */
+                       if (!bfd_link_pic (info)
+                           && htab->elf.dynamic_sections_created)
+                         {
+                           if (ent->plt.offset
+                               == (bfd_vma) htab->plt_initial_entry_size)
+                             {
+                               htab->srelplt2->size
+                                 += (sizeof (Elf32_External_Rela)
+                                     * VXWORKS_PLTRESOLVE_RELOCS);
+                             }
+
+                           htab->srelplt2->size
+                             += (sizeof (Elf32_External_Rela)
+                                 * VXWORKS_PLT_NON_JMP_SLOT_RELOCS);
+                         }
+
+                       /* Every PLT entry has an associated GOT entry in
+                          .got.plt.  */
+                       htab->elf.sgotplt->size += 4;
+                     }
+                 }
+               doneone = TRUE;
+             }
+         }
+       else
+         ent->plt.offset = (bfd_vma) -1;
+
+      if (!doneone)
+       {
+         h->plt.plist = NULL;
+         h->needs_plt = 0;
+       }
+    }
+  else
+    {
+      h->plt.plist = NULL;
+      h->needs_plt = 0;
+    }
+
   return TRUE;
 }
 
@@ -6294,14 +6110,17 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
    read-only sections.  */
 
 static bfd_boolean
-maybe_set_textrel (struct elf_link_hash_entry *h, void *info)
+maybe_set_textrel (struct elf_link_hash_entry *h, void *info_p)
 {
+  struct bfd_link_info *info;
+
   if (h->root.type == bfd_link_hash_indirect)
     return TRUE;
 
-  if (readonly_dynrelocs (h))
+  info = (struct bfd_link_info *) info_p;
+  if (readonly_dynrelocs (h, info))
     {
-      ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
+      info->flags |= DF_TEXTREL;
 
       /* Not an error, just cut short the traversal.  */
       return FALSE;
@@ -6406,7 +6225,11 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd,
                  if ((p->sec->output_section->flags
                       & (SEC_READONLY | SEC_ALLOC))
                      == (SEC_READONLY | SEC_ALLOC))
-                   info->flags |= DF_TEXTREL;
+                   {
+                     info->flags |= DF_TEXTREL;
+                     info->callbacks->minfo (_("%B: dynamic relocation in read-only section `%A'\n"),
+                                             p->sec->owner, p->sec);
+                   }
                }
            }
        }
@@ -6639,7 +6462,8 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd,
               || s == htab->glink_eh_frame
               || s == htab->elf.sgotplt
               || s == htab->sbss
-              || s == htab->dynbss
+              || s == htab->elf.sdynbss
+              || s == htab->elf.sdynrelro
               || s == htab->dynsbss)
        {
          /* Strip these too.  */
@@ -7909,7 +7733,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
          wrel->r_addend = 0;
 
          /* For ld -r, remove relocations in debug sections against
-            sections defined in discarded sections.  Not done for
+            symbols defined in discarded sections.  Not done for
             non-debug to preserve relocs in .eh_frame which the
             eh_frame editing code expects to be present.  */
          if (bfd_link_relocatable (info)
@@ -8245,7 +8069,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  off = (p - contents) - (rel->r_offset - d_offset);
                  if (off > 0x1fffffc || (off & 3) != 0)
                    info->callbacks->einfo
-                     (_("%P: %H: fixup branch overflow\n"),
+                     (_("%H: fixup branch overflow\n"),
                       input_bfd, input_section, rel->r_offset);
 
                  bfd_put_32 (input_bfd, B | off,
@@ -8257,7 +8081,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  wrel->r_info = ELF32_R_INFO (0, R_PPC_ADDR16_HA);
                  wrel->r_addend = got_addr;
                  insn &= ~0xffff;
-                 insn |= ((unsigned int )(got_addr + 0x8000) >> 16) & 0xffff;
+                 insn |= ((unsigned int(got_addr + 0x8000) >> 16) & 0xffff;
                  bfd_put_32 (input_bfd, insn, p);
 
                  /* Convert lis to lwz, loading address from GOT.  */
@@ -8283,9 +8107,9 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  r_type = R_PPC_GOT16_LO;
                }
              else
-               info->callbacks->einfo
+               _bfd_error_handler
                  /* xgettext:c-format */
-                 (_("%P: %H: error: %s with unexpected instruction %x\n"),
+                 (_("%B(%A+%#Lx): error: %s with unexpected instruction %#x"),
                   input_bfd, input_section, rel->r_offset,
                   "R_PPC_ADDR16_HA", insn);
            }
@@ -8318,9 +8142,9 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  rel->r_info = ELF32_R_INFO (0, r_type);
                }
              else
-               info->callbacks->einfo
+               _bfd_error_handler
                  /* xgettext:c-format */
-                 (_("%P: %H: error: %s with unexpected instruction %x\n"),
+                 (_("%B(%A+%#Lx): error: %s with unexpected instruction %#x"),
                   input_bfd, input_section, rel->r_offset,
                   "R_PPC_ADDR16_LO", insn);
            }
@@ -8376,7 +8200,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                     into .got2).  */
                    info->callbacks->einfo
                      /* xgettext:c-format */
-                     (_("%X%P: %H: unsupported bss-plt -fPIC ifunc %s\n"),
+                     (_("%X%H: unsupported bss-plt -fPIC ifunc %s\n"),
                       input_bfd, input_section, rel->r_offset, sym_name);
                }
              if (h == NULL && (ent->plt.offset & 1) == 0)
@@ -8393,6 +8217,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  loc += (htab->elf.irelplt->reloc_count++
                          * sizeof (Elf32_External_Rela));
                  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
+                 htab->local_ifunc_resolver = 1;
 
                  ent->plt.offset |= 1;
                }
@@ -8420,10 +8245,44 @@ ppc_elf_relocate_section (bfd *output_bfd,
        }
 
       addend = rel->r_addend;
-      tls_type = 0;
       howto = NULL;
       if (r_type < R_PPC_max)
        howto = ppc_elf_howto_table[r_type];
+
+      switch (r_type)
+       {
+       default:
+         break;
+
+       case R_PPC_TPREL16_HA:
+         if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
+           {
+             bfd_byte *p = contents + (rel->r_offset & ~3);
+             unsigned int insn = bfd_get_32 (input_bfd, p);
+             if ((insn & ((0x3f << 26) | 0x1f << 16))
+                 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
+               /* xgettext:c-format */
+               info->callbacks->minfo
+                 (_("%H: warning: %s unexpected insn %#x.\n"),
+                  input_bfd, input_section, rel->r_offset, howto->name, insn);
+             else
+               bfd_put_32 (input_bfd, NOP, p);
+           }
+         break;
+
+       case R_PPC_TPREL16_LO:
+         if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
+           {
+             bfd_byte *p = contents + (rel->r_offset & ~3);
+             unsigned int insn = bfd_get_32 (input_bfd, p);
+             insn &= ~(0x1f << 16);
+             insn |= 2 << 16;
+             bfd_put_32 (input_bfd, insn, p);
+           }
+         break;
+       }
+
+      tls_type = 0;
       switch (r_type)
        {
        default:
@@ -8500,11 +8359,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
              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, bfd_link_pic (info), h)
-                   || (bfd_link_pic (info)
-                       && SYMBOL_REFERENCES_LOCAL (info, h)))
+               if (!htab->elf.dynamic_sections_created
+                   || h->dynindx == -1
+                   || SYMBOL_REFERENCES_LOCAL (info, h)
+                   || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
                  /* This is actually a static link, or it is a
                     -Bsymbolic link and the symbol is defined
                     locally, or the symbol was forced to be local
@@ -8512,7 +8370,6 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  ;
                else
                  {
-                   BFD_ASSERT (h->dynindx != -1);
                    indx = h->dynindx;
                    unresolved_reloc = FALSE;
                  }
@@ -8571,17 +8428,23 @@ ppc_elf_relocate_section (bfd *output_bfd,
                      }
 
                    /* Generate relocs for the dynamic linker.  */
-                   if ((bfd_link_pic (info) || indx != 0)
-                       && (offp == &htab->tlsld_got.offset
-                           || h == NULL
-                           || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
-                           || h->root.type != bfd_link_hash_undefweak))
+                   if (indx != 0
+                       || (bfd_link_pic (info)
+                           && (h == NULL
+                               || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)
+                               || offp == &htab->tlsld_got.offset)))
                      {
                        asection *rsec = htab->elf.srelgot;
                        bfd_byte * loc;
 
                        if (ifunc != NULL)
-                         rsec = htab->elf.irelplt;
+                         {
+                           rsec = htab->elf.irelplt;
+                           if (indx == 0)
+                             htab->local_ifunc_resolver = 1;
+                           else if (is_static_defined (h))
+                             htab->maybe_local_ifunc_resolver = 1;
+                         }
                        outrel.r_offset = (htab->elf.sgot->output_section->vma
                                           + htab->elf.sgot->output_offset
                                           + off);
@@ -8634,20 +8497,21 @@ ppc_elf_relocate_section (bfd *output_bfd,
                      {
                        bfd_vma value = relocation;
 
-                       if (tls_ty == (TLS_TLS | TLS_LD))
-                         value = 1;
-                       else if (tls_ty != 0)
+                       if (tls_ty != 0)
                          {
                            if (htab->elf.tls_sec == NULL)
                              value = 0;
                            else
                              {
-                               value -= htab->elf.tls_sec->vma + DTP_OFFSET;
-                               if (tls_ty == (TLS_TLS | TLS_TPREL))
+                               if (tls_ty & TLS_LD)
+                                 value = 0;
+                               else
+                                 value -= htab->elf.tls_sec->vma + DTP_OFFSET;
+                               if (tls_ty & TLS_TPREL)
                                  value += DTP_OFFSET - TP_OFFSET;
                              }
 
-                           if (tls_ty == (TLS_TLS | TLS_GD))
+                           if (tls_ty & (TLS_LD | TLS_GD))
                              {
                                bfd_put_32 (input_bfd, value,
                                            htab->elf.sgot->contents + off + 4);
@@ -8708,7 +8572,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
            if (addend != 0)
              info->callbacks->einfo
                /* xgettext:c-format */
-               (_("%P: %H: non-zero addend on %s reloc against `%s'\n"),
+               (_("%H: non-zero addend on %s reloc against `%s'\n"),
                 input_bfd, input_section, rel->r_offset,
                 howto->name,
                 sym_name);
@@ -8740,7 +8604,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                 -fPIC code, so the linker won't know whether r30 is
                 _GLOBAL_OFFSET_TABLE_ or pointing into a .got2 section.  */
              /* xgettext:c-format */
-             info->callbacks->einfo (_("%X%P: %H: @local call to ifunc %s\n"),
+             info->callbacks->einfo (_("%X%H: @local call to ifunc %s\n"),
                                      input_bfd, input_section, rel->r_offset,
                                      h->root.root.string);
            }
@@ -8778,8 +8642,8 @@ ppc_elf_relocate_section (bfd *output_bfd,
          if (htab->elf.tls_sec != NULL)
            addend -= htab->elf.tls_sec->vma + TP_OFFSET;
          /* The TPREL16 relocs shouldn't really be used in shared
-            libs as they will result in DT_TEXTREL being set, but
-            support them anyway.  */
+            libs or with non-local symbols as that will result in
+            DT_TEXTREL being set, but support them anyway.  */
          goto dodyn;
 
        case R_PPC_TPREL32:
@@ -8848,10 +8712,8 @@ ppc_elf_relocate_section (bfd *output_bfd,
          if ((bfd_link_pic (info)
               && !(h != NULL
                    && ((h->root.type == bfd_link_hash_undefined
-                        && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN
-                            || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL))
-                       || (h->root.type == bfd_link_hash_undefweak
-                           && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)))
+                        && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
+                       || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
               && (must_be_dyn_reloc (info, r_type)
                   || !SYMBOL_CALLS_LOCAL (info, h)))
              || (ELIMINATE_COPY_RELOCS
@@ -8868,6 +8730,8 @@ ppc_elf_relocate_section (bfd *output_bfd,
              int skip;
              bfd_byte *loc;
              asection *sreloc;
+             long indx = 0;
+
 #ifdef DEBUG
              fprintf (stderr, "ppc_elf_relocate_section needs to "
                       "create relocation for %s\n",
@@ -8878,12 +8742,6 @@ ppc_elf_relocate_section (bfd *output_bfd,
              /* When generating a shared object, these relocations
                 are copied into the output file to be resolved at run
                 time.  */
-             sreloc = elf_section_data (input_section)->sreloc;
-             if (ifunc)
-               sreloc = htab->elf.irelplt;
-             if (sreloc == NULL)
-               return FALSE;
-
              skip = 0;
              outrel.r_offset = _bfd_elf_section_offset (output_bfd, info,
                                                         input_section,
@@ -8896,14 +8754,12 @@ ppc_elf_relocate_section (bfd *output_bfd,
 
              if (skip)
                memset (&outrel, 0, sizeof outrel);
-             else if ((h != NULL
-                       && (h->root.type == bfd_link_hash_undefined
-                           || h->root.type == bfd_link_hash_undefweak))
-                      || !SYMBOL_REFERENCES_LOCAL (info, h))
+             else if (!SYMBOL_REFERENCES_LOCAL (info, h))
                {
-                 BFD_ASSERT (h->dynindx != -1);
+                 indx = h->dynindx;
+                 BFD_ASSERT (indx != -1);
                  unresolved_reloc = FALSE;
-                 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
+                 outrel.r_info = ELF32_R_INFO (indx, r_type);
                  outrel.r_addend = rel->r_addend;
                }
              else
@@ -8912,8 +8768,6 @@ ppc_elf_relocate_section (bfd *output_bfd,
 
                  if (r_type != R_PPC_ADDR32)
                    {
-                     long indx = 0;
-
                      if (ifunc != NULL)
                        {
                          /* If we get here when building a static
@@ -8930,7 +8784,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                             indirection function to resolve the reloc.  */
                          info->callbacks->einfo
                            /* xgettext:c-format */
-                           (_("%P: %H: relocation %s for indirect "
+                           (_("%H: relocation %s for indirect "
                               "function %s unsupported\n"),
                             input_bfd, input_section, rel->r_offset,
                             howto->name,
@@ -8978,6 +8832,18 @@ ppc_elf_relocate_section (bfd *output_bfd,
                    outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
                }
 
+             sreloc = elf_section_data (input_section)->sreloc;
+             if (ifunc)
+               {
+                 sreloc = htab->elf.irelplt;
+                 if (indx == 0)
+                   htab->local_ifunc_resolver = 1;
+                 else if (is_static_defined (h))
+                   htab->maybe_local_ifunc_resolver = 1;
+               }
+             if (sreloc == NULL)
+               return FALSE;
+
              loc = sreloc->contents;
              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
              bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
@@ -8985,11 +8851,9 @@ ppc_elf_relocate_section (bfd *output_bfd,
              if (skip == -1)
                goto copy_reloc;
 
-             /* This reloc will be computed at runtime.  We clear the memory
-                so that it contains predictable value.  */
-             if (! skip
-                 && ((input_section->flags & SEC_ALLOC) != 0
-                     || ELF32_R_TYPE (outrel.r_info) != R_PPC_RELATIVE))
+             /* This reloc will be computed at runtime.  Clear the memory
+                so that it contains a predictable value for prelink.  */
+             if (!skip)
                {
                  relocation = howto->pc_relative ? outrel.r_offset : 0;
                  addend = 0;
@@ -9380,7 +9244,6 @@ ppc_elf_relocate_section (bfd *output_bfd,
          {
            bfd_vma value;
            const char *name;
-           //int reg;
            struct elf_link_hash_entry *sda = NULL;
 
            if (sec == NULL || sec->output_section == NULL)
@@ -9392,16 +9255,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
            name = bfd_get_section_name (output_bfd, sec->output_section);
            if (strcmp (name, ".sdata") == 0
                || strcmp (name, ".sbss") == 0)
-             {
-               //reg = 13;
-               sda = htab->sdata[0].sym;
-             }
+             sda = htab->sdata[0].sym;
            else if (strcmp (name, ".sdata2") == 0
                     || strcmp (name, ".sbss2") == 0)
-             {
-               //reg = 2;
-               sda = htab->sdata[1].sym;
-             }
+             sda = htab->sdata[1].sym;
            else
              {
                _bfd_error_handler
@@ -9418,18 +9275,12 @@ ppc_elf_relocate_section (bfd *output_bfd,
                goto copy_reloc;
              }
 
-           if (sda != NULL)
+           if (sda == NULL || !is_static_defined (sda))
              {
-               if (!is_static_defined (sda))
-                 {
-                   unresolved_reloc = TRUE;
-                   break;
-                 }
+               unresolved_reloc = TRUE;
+               break;
              }
-
-           value = (sda->root.u.def.section->output_section->vma
-                    + sda->root.u.def.section->output_offset
-                    + addend);
+           value = relocation + addend - SYM_VAL (sda);
 
            if (r_type == R_PPC_VLE_SDAREL_LO16A)
              ppc_elf_vle_split16 (input_bfd, input_section, rel->r_offset,
@@ -9476,6 +9327,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
          }
          goto copy_reloc;
 
+       case R_PPC_VLE_ADDR20:
+         ppc_elf_vle_split20 (output_bfd, contents + rel->r_offset, relocation);
+         continue;
+
          /* Relocate against the beginning of the section.  */
        case R_PPC_SECTOFF:
        case R_PPC_SECTOFF_LO:
@@ -9601,7 +9456,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                relocation ^= lobit;
                info->callbacks->einfo
                  /* xgettext:c-format */
-                 (_("%P: %H: error: %s against `%s' not a multiple of %u\n"),
+                 (_("%H: error: %s against `%s' not a multiple of %u\n"),
                   input_bfd, input_section, rel->r_offset,
                   howto->name, sym_name, mask + 1);
                bfd_set_error (bfd_error_bad_value);
@@ -9630,7 +9485,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
        {
          info->callbacks->einfo
            /* xgettext:c-format */
-           (_("%P: %H: unresolvable %s relocation against symbol `%s'\n"),
+           (_("%H: unresolvable %s relocation against symbol `%s'\n"),
             input_bfd, input_section, rel->r_offset,
             howto->name,
             sym_name);
@@ -9711,7 +9566,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
            {
              info->callbacks->einfo
                /* xgettext:c-format */
-               (_("%P: %H: %s reloc against `%s': error %d\n"),
+               (_("%H: %s reloc against `%s': error %d\n"),
                 input_bfd, input_section, rel->r_offset,
                 howto->name, sym_name, (int) r);
              ret = FALSE;
@@ -10255,12 +10110,19 @@ ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
 
            if (!htab->elf.dynamic_sections_created
                || h->dynindx == -1)
-             loc = (htab->elf.irelplt->contents
-                    + (htab->elf.irelplt->reloc_count++
-                       * sizeof (Elf32_External_Rela)));
+             {
+               loc = (htab->elf.irelplt->contents
+                      + (htab->elf.irelplt->reloc_count++
+                         * sizeof (Elf32_External_Rela)));
+               htab->local_ifunc_resolver = 1;
+             }
            else
-             loc = (htab->elf.srelplt->contents
-                    + reloc_index * sizeof (Elf32_External_Rela));
+             {
+               loc = (htab->elf.srelplt->contents
+                      + reloc_index * sizeof (Elf32_External_Rela));
+               if (h->type == STT_GNU_IFUNC && is_static_defined (h))
+                 htab->maybe_local_ifunc_resolver = 1;
+             }
            bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
 
            if (!h->def_regular)
@@ -10360,8 +10222,10 @@ ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
 
       if (ppc_elf_hash_entry (h)->has_sda_refs)
        s = htab->relsbss;
+      else if (h->root.u.def.section == htab->elf.sdynrelro)
+       s = htab->elf.sreldynrelro;
       else
-       s = htab->relbss;
+       s = htab->elf.srelbss;
       BFD_ASSERT (s != NULL);
 
       rela.r_offset = SYM_VAL (h);
@@ -10408,7 +10272,6 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
                                 struct bfd_link_info *info)
 {
   asection *sdyn;
-  asection *splt;
   struct ppc_elf_link_hash_table *htab;
   bfd_vma got;
   bfd *dynobj;
@@ -10421,10 +10284,6 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
   htab = ppc_elf_hash_table (info);
   dynobj = htab->elf.dynobj;
   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
-  if (htab->is_vxworks)
-    splt = htab->elf.splt;
-  else
-    splt = NULL;
 
   got = 0;
   if (htab->elf.hgot != NULL)
@@ -10468,13 +10327,15 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
              dyn.d_un.d_ptr = got;
              break;
 
-           case DT_RELASZ:
-             if (htab->is_vxworks)
-               {
-                 if (htab->elf.srelplt)
-                   dyn.d_un.d_ptr -= htab->elf.srelplt->size;
-                 break;
-               }
+           case DT_TEXTREL:
+             if (htab->local_ifunc_resolver)
+               info->callbacks->einfo
+                 (_("%X%P: text relocations and GNU indirect "
+                    "functions will result in a segfault at runtime\n"));
+             else if (htab->maybe_local_ifunc_resolver)
+               info->callbacks->einfo
+                 (_("%P: warning: text relocations and GNU indirect "
+                    "functions may result in a segfault at runtime\n"));
              continue;
 
            default:
@@ -10488,7 +10349,8 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
        }
     }
 
-  if (htab->elf.sgot != NULL)
+  if (htab->elf.sgot != NULL
+      && htab->elf.sgot->output_section != bfd_abs_section_ptr)
     {
       if (htab->elf.hgot->root.u.def.section == htab->elf.sgot
          || htab->elf.hgot->root.u.def.section == htab->elf.sgotplt)
@@ -10530,8 +10392,12 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
     }
 
   /* Fill in the first entry in the VxWorks procedure linkage table.  */
-  if (splt && splt->size > 0)
+  if (htab->is_vxworks
+      && htab->elf.splt != NULL
+      && htab->elf.splt->size != 0
+      && htab->elf.splt->output_section != bfd_abs_section_ptr)
     {
+      asection *splt = htab->elf.splt;
       /* Use the right PLT. */
       const bfd_vma *plt_entry = (bfd_link_pic (info)
                                  ? ppc_elf_vxworks_pic_plt0_entry
@@ -10906,6 +10772,7 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 #endif
 
 #define elf_backend_plt_not_loaded     1
+#define elf_backend_want_dynrelro      1
 #define elf_backend_can_gc_sections    1
 #define elf_backend_can_refcount       1
 #define elf_backend_rela_normal                1
@@ -10922,11 +10789,11 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 
 #define elf_backend_object_p                   ppc_elf_object_p
 #define elf_backend_gc_mark_hook               ppc_elf_gc_mark_hook
-#define elf_backend_gc_sweep_hook              ppc_elf_gc_sweep_hook
 #define elf_backend_section_from_shdr          ppc_elf_section_from_shdr
 #define elf_backend_relocate_section           ppc_elf_relocate_section
 #define elf_backend_create_dynamic_sections    ppc_elf_create_dynamic_sections
 #define elf_backend_check_relocs               ppc_elf_check_relocs
+#define elf_backend_relocs_compatible          _bfd_elf_relocs_compatible
 #define elf_backend_copy_indirect_symbol       ppc_elf_copy_indirect_symbol
 #define elf_backend_adjust_dynamic_symbol      ppc_elf_adjust_dynamic_symbol
 #define elf_backend_add_symbol_hook            ppc_elf_add_symbol_hook
@@ -10936,7 +10803,7 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 #define elf_backend_finish_dynamic_sections    ppc_elf_finish_dynamic_sections
 #define elf_backend_fake_sections              ppc_elf_fake_sections
 #define elf_backend_additional_program_headers ppc_elf_additional_program_headers
-#define elf_backend_modify_segment_map         ppc_elf_modify_segment_map
+#define elf_backend_modify_segment_map         ppc_elf_modify_segment_map
 #define elf_backend_grok_prstatus              ppc_elf_grok_prstatus
 #define elf_backend_grok_psinfo                        ppc_elf_grok_psinfo
 #define elf_backend_write_core_note            ppc_elf_write_core_note
@@ -10984,7 +10851,7 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 
 /* VxWorks uses the elf default section flags for .plt.  */
 static const struct bfd_elf_special_section *
-ppc_elf_vxworks_get_sec_type_attr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
+ppc_elf_vxworks_get_sec_type_attr (bfd *abfd, asection *sec)
 {
   if (sec->name == NULL)
     return NULL;
@@ -11006,7 +10873,7 @@ ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
   if (ret)
     {
       struct ppc_elf_link_hash_table *htab
-        = (struct ppc_elf_link_hash_table *)ret;
+       = (struct ppc_elf_link_hash_table *)ret;
       htab->is_vxworks = 1;
       htab->plt_type = PLT_VXWORKS;
       htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
@@ -11021,23 +10888,23 @@ static bfd_boolean
 ppc_elf_vxworks_add_symbol_hook (bfd *abfd,
                                 struct bfd_link_info *info,
                                 Elf_Internal_Sym *sym,
-                                const char **namep ATTRIBUTE_UNUSED,
-                                flagword *flagsp ATTRIBUTE_UNUSED,
+                                const char **namep,
+                                flagword *flagsp,
                                 asection **secp,
                                 bfd_vma *valp)
 {
-  if (!elf_vxworks_add_symbol_hook(abfd, info, sym,namep, flagsp, secp,
-                                  valp))
+  if (!elf_vxworks_add_symbol_hook (abfd, info, sym, namep, flagsp, secp,
+                                   valp))
     return FALSE;
 
-  return ppc_elf_add_symbol_hook(abfd, info, sym,namep, flagsp, secp, valp);
+  return ppc_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp);
 }
 
 static void
 ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
 {
-  ppc_elf_final_write_processing(abfd, linker);
-  elf_vxworks_final_write_processing(abfd, linker);
+  ppc_elf_final_write_processing (abfd, linker);
+  elf_vxworks_final_write_processing (abfd, linker);
 }
 
 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
@@ -11054,6 +10921,8 @@ ppc_elf_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
 #define elf_backend_plt_readonly               1
 #undef elf_backend_got_header_size
 #define elf_backend_got_header_size            12
+#undef elf_backend_dtrel_excludes_plt
+#define elf_backend_dtrel_excludes_plt         1
 
 #undef bfd_elf32_get_synthetic_symtab
 
This page took 0.056203 seconds and 4 git commands to generate.