* bfd-in2.h: Regenerate.
[deliverable/binutils-gdb.git] / bfd / elf32-ppc.c
index 82de7df90799c7a68badf9e33618221cc38f9e31..c81ea1e7707f5dd40c6b18d7aa2de87fdf6219d6 100644 (file)
@@ -1,6 +1,6 @@
 /* PowerPC-specific support for 32-bit ELF
    Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
-   2004, 2005 Free Software Foundation, Inc.
+   2004, 2005, 2006 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -17,8 +17,8 @@
 
    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.  */
+   Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+   Boston, MA 02110-1301, USA.  */
 
 /* This file is based on a preliminary PowerPC ELF ABI.  The
    information may not match the final PowerPC ELF ABI.  It includes
@@ -32,6 +32,7 @@
 #include "elf-bfd.h"
 #include "elf/ppc.h"
 #include "elf32-ppc.h"
+#include "elf-vxworks.h"
 
 /* RELA relocations are used here.  */
 
@@ -51,19 +52,105 @@ static bfd_reloc_status_type ppc_elf_unhandled_reloc
    section.  */
 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
 
-/* The size in bytes of an entry in the procedure linkage table.  */
-#define PLT_ENTRY_SIZE 12
-/* The initial size of the plt reserved for the dynamic linker.  */
-#define PLT_INITIAL_ENTRY_SIZE 72
-/* The size of the gap between entries in the PLT.  */
-#define PLT_SLOT_SIZE 8
+/* For old-style PLT.  */
 /* The number of single-slot PLT entries (the rest use two slots).  */
 #define PLT_NUM_SINGLE_ENTRIES 8192
 
-/* Some nop instructions.  */
+/* For new-style .glink and .plt.  */
+#define GLINK_PLTRESOLVE 16*4
+#define GLINK_ENTRY_SIZE 4*4
+
+/* VxWorks uses its own plt layout, filled in by the static linker.  */
+
+/* The standard VxWorks PLT entry.  */
+#define VXWORKS_PLT_ENTRY_SIZE 32
+static const bfd_vma ppc_elf_vxworks_plt_entry
+    [VXWORKS_PLT_ENTRY_SIZE / 4] =
+  {
+    0x3d800000, /* lis     r12,0                 */
+    0x818c0000, /* lwz     r12,0(r12)            */
+    0x7d8903a6, /* mtctr   r12                   */
+    0x4e800420, /* bctr                          */
+    0x39600000, /* li      r11,0                 */
+    0x48000000, /* b       14 <.PLT0resolve+0x4> */
+    0x60000000, /* nop                           */
+    0x60000000, /* nop                           */
+  };
+static const bfd_vma ppc_elf_vxworks_pic_plt_entry
+    [VXWORKS_PLT_ENTRY_SIZE / 4] =
+  {
+    0x3d9e0000, /* addis r12,r30,0 */
+    0x818c0000, /* lwz  r12,0(r12) */
+    0x7d8903a6, /* mtctr r12 */
+    0x4e800420, /* bctr */
+    0x39600000, /* li   r11,0 */
+    0x48000000, /* b    14 <.PLT0resolve+0x4> 14: R_PPC_REL24 .PLTresolve */
+    0x60000000, /* nop */
+    0x60000000, /* nop */
+  };
+
+/* The initial VxWorks PLT entry.  */
+#define VXWORKS_PLT_INITIAL_ENTRY_SIZE 32
+static const bfd_vma ppc_elf_vxworks_plt0_entry
+    [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
+  {
+    0x3d800000, /* lis     r12,0        */
+    0x398c0000, /* addi    r12,r12,0    */
+    0x800c0008, /* lwz     r0,8(r12)    */
+    0x7c0903a6, /* mtctr   r0           */
+    0x818c0004, /* lwz     r12,4(r12)   */
+    0x4e800420, /* bctr                 */
+    0x60000000, /* nop                  */
+    0x60000000, /* nop                  */
+  };
+static const bfd_vma ppc_elf_vxworks_pic_plt0_entry
+    [VXWORKS_PLT_INITIAL_ENTRY_SIZE / 4] =
+  {
+    0x819e0008, /* lwz  r12,8(r30) */
+    0x7d8903a6, /* mtctr r12        */
+    0x819e0004, /* lwz  r12,4(r30) */
+    0x4e800420, /* bctr             */
+    0x60000000, /* nop              */
+    0x60000000, /* nop              */
+    0x60000000, /* nop              */
+    0x60000000, /* nop              */
+  };
+
+/* For executables, we have some additional relocations in
+   .rela.plt.unloaded, for the kernel loader.  */
+
+/* The number of non-JMP_SLOT relocations per PLT0 slot. */
+#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
+   a shared library. */
+#define VXWORKS_PLTRESOLVE_RELOCS_SHLIB 0
+
+/* Some instructions.  */
+#define ADDIS_11_11    0x3d6b0000
+#define ADDIS_11_30    0x3d7e0000
+#define ADDIS_12_12    0x3d8c0000
+#define ADDI_11_11     0x396b0000
+#define ADD_0_11_11    0x7c0b5a14
+#define ADD_11_0_11    0x7d605a14
+#define B              0x48000000
+#define BCL_20_31      0x429f0005
+#define BCTR           0x4e800420
+#define LIS_11         0x3d600000
+#define LIS_12         0x3d800000
+#define LWZU_0_12      0x840c0000
+#define LWZ_0_12       0x800c0000
+#define LWZ_11_11      0x816b0000
+#define LWZ_11_30      0x817e0000
+#define LWZ_12_12      0x818c0000
+#define MFLR_0         0x7c0802a6
+#define MFLR_12                0x7d8802a6
+#define MTCTR_0                0x7c0903a6
+#define MTCTR_11       0x7d6903a6
+#define MTLR_0         0x7c0803a6
 #define NOP            0x60000000
-#define CROR_151515    0x4def7b82
-#define CROR_313131    0x4ffffb82
+#define SUB_11_11_12   0x7d6c5850
 
 /* Offset of tp and dtp pointers from start of TLS block.  */
 #define TP_OFFSET      0x7000
@@ -1259,6 +1346,67 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0xffff,                /* dst_mask */
         FALSE),                /* pcrel_offset */
 
+  /* A 16 bit relative relocation.  */
+  HOWTO (R_PPC_REL16,          /* type */
+        0,                     /* rightshift */
+        1,                     /* size (0 = byte, 1 = short, 2 = long) */
+        16,                    /* bitsize */
+        TRUE,                  /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_bitfield, /* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_PPC_REL16",         /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0xffff,                /* dst_mask */
+        TRUE),                 /* pcrel_offset */
+
+  /* A 16 bit relative relocation without overflow.  */
+  HOWTO (R_PPC_REL16_LO,       /* type */
+        0,                     /* rightshift */
+        1,                     /* size (0 = byte, 1 = short, 2 = long) */
+        16,                    /* bitsize */
+        TRUE,                  /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont,/* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_PPC_REL16_LO",      /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0xffff,                /* dst_mask */
+        TRUE),                 /* pcrel_offset */
+
+  /* The high order 16 bits of a relative address.  */
+  HOWTO (R_PPC_REL16_HI,       /* type */
+        16,                    /* rightshift */
+        1,                     /* size (0 = byte, 1 = short, 2 = long) */
+        16,                    /* bitsize */
+        TRUE,                  /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont, /* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_PPC_REL16_HI",      /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0xffff,                /* dst_mask */
+        TRUE),                 /* pcrel_offset */
+
+  /* The high order 16 bits of a relative address, plus 1 if the contents of
+     the low 16 bits, treated as a signed number, is negative.  */
+  HOWTO (R_PPC_REL16_HA,       /* type */
+        16,                    /* rightshift */
+        1,                     /* size (0 = byte, 1 = short, 2 = long) */
+        16,                    /* bitsize */
+        TRUE,                  /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont, /* complain_on_overflow */
+        ppc_elf_addr16_ha_reloc, /* special_function */
+        "R_PPC_REL16_HA",      /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0xffff,                /* dst_mask */
+        TRUE),                 /* pcrel_offset */
+
   /* GNU extension to record C++ vtable hierarchy.  */
   HOWTO (R_PPC_GNU_VTINHERIT,  /* type */
         0,                     /* rightshift */
@@ -1418,6 +1566,10 @@ ppc_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
     case BFD_RELOC_PPC_EMB_RELST_HA:   r = R_PPC_EMB_RELST_HA;         break;
     case BFD_RELOC_PPC_EMB_BIT_FLD:    r = R_PPC_EMB_BIT_FLD;          break;
     case BFD_RELOC_PPC_EMB_RELSDA:     r = R_PPC_EMB_RELSDA;           break;
+    case BFD_RELOC_16_PCREL:           r = R_PPC_REL16;                break;
+    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_VTABLE_INHERIT:     r = R_PPC_GNU_VTINHERIT;        break;
     case BFD_RELOC_VTABLE_ENTRY:       r = R_PPC_GNU_VTENTRY;          break;
     }
@@ -1440,7 +1592,7 @@ ppc_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
   cache_ptr->howto = ppc_elf_howto_table[ELF32_R_TYPE (dst->r_info)];
 }
 
-/* Handle the R_PPC_ADDR16_HA reloc.  */
+/* 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,
@@ -1470,6 +1622,8 @@ ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
   relocation += symbol->section->output_section->vma;
   relocation += symbol->section->output_offset;
   relocation += reloc_entry->addend;
+  if (reloc_entry->howto->pc_relative)
+    relocation -= reloc_entry->address;
 
   reloc_entry->addend += (relocation & 0x8000) << 1;
 
@@ -1514,8 +1668,8 @@ typedef struct elf_linker_section
   const char *bss_name;
   /* Associated symbol name.  */
   const char *sym_name;
-  /* Value of symbol.  */
-  bfd_vma sym_val;
+  /* Associated symbol.  */
+  struct elf_link_hash_entry *sym;
 } elf_linker_section_t;
 
 /* Linked list of allocated pointer entries.  This hangs off of the
@@ -1554,11 +1708,14 @@ struct ppc_elf_obj_tdata
 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;
+    {
+      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 bfd_elf_mkobject (abfd);
 }
 
 /* Fix bad default arch selected for a 32 bit input bfd when the
@@ -1702,7 +1859,7 @@ ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
                       Elf_Internal_Shdr *shdr,
                       asection *asect)
 {
-  if ((asect->flags & SEC_EXCLUDE) != 0)
+  if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE)
     shdr->sh_flags |= SHF_EXCLUDE;
 
   if ((asect->flags & SEC_SORT_ENTRIES) != 0)
@@ -1715,7 +1872,8 @@ ppc_elf_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
    need to bump up the number of section headers.  */
 
 static int
-ppc_elf_additional_program_headers (bfd *abfd)
+ppc_elf_additional_program_headers (bfd *abfd,
+                                   struct bfd_link_info *info ATTRIBUTE_UNUSED)
 {
   asection *s;
   int ret = 0;
@@ -1736,19 +1894,44 @@ ppc_elf_additional_program_headers (bfd *abfd)
    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[]=
+static const struct bfd_elf_special_section ppc_elf_special_sections[] =
 {
-  { ".tags",             5,  0, SHT_ORDERED,  SHF_ALLOC },
-  { ".sdata",            6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
-  { ".sbss",             5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
-  { ".sdata2",           7, -2, SHT_PROGBITS, SHF_ALLOC },
-  { ".sbss2",            6, -2, SHT_PROGBITS, SHF_ALLOC },
-  { ".PPC.EMB.apuinfo", 16,  0, SHT_NOTE,     0 },
-  { ".PPC.EMB.sdata0",  15,  0, SHT_PROGBITS, SHF_ALLOC },
-  { ".PPC.EMB.sbss0",   14,  0, SHT_PROGBITS, SHF_ALLOC },
-  { ".plt",              4,  0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
-  { NULL,                0,  0, 0,            0 }
+  { STRING_COMMA_LEN (".plt"),             0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
+  { STRING_COMMA_LEN (".sbss"),           -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
+  { STRING_COMMA_LEN (".sbss2"),          -2, SHT_PROGBITS, SHF_ALLOC },
+  { STRING_COMMA_LEN (".sdata"),          -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
+  { STRING_COMMA_LEN (".sdata2"),         -2, SHT_PROGBITS, SHF_ALLOC },
+  { STRING_COMMA_LEN (".tags"),            0, SHT_ORDERED,  SHF_ALLOC },
+  { STRING_COMMA_LEN (".PPC.EMB.apuinfo"), 0, SHT_NOTE,     0 },
+  { STRING_COMMA_LEN (".PPC.EMB.sbss0"),   0, SHT_PROGBITS, SHF_ALLOC },
+  { STRING_COMMA_LEN (".PPC.EMB.sdata0"),  0, SHT_PROGBITS, SHF_ALLOC },
+  { NULL,                              0,  0, 0,            0 }
 };
+
+/* This is what we want for new plt/got.  */
+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)
+{
+  const struct bfd_elf_special_section *ssect;
+
+  /* See if this is one of the special sections.  */
+  if (sec->name == NULL)
+    return NULL;
+
+  ssect = _bfd_elf_get_special_section (sec->name, ppc_elf_special_sections,
+                                       sec->use_rela_p);
+  if (ssect != NULL)
+    {
+      if (ssect == ppc_elf_special_sections && (sec->flags & SEC_LOAD) != 0)
+       ssect = &ppc_alt_plt;
+      return ssect;
+    }
+
+  return _bfd_elf_get_sec_type_attr (abfd, sec);
+}
 \f
 /* Very simple linked list structure for recording apuinfo values.  */
 typedef struct apuinfo_list
@@ -1968,6 +2151,7 @@ ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
 
 static bfd_boolean
 ppc_elf_write_section (bfd *abfd ATTRIBUTE_UNUSED,
+                      struct bfd_link_info *link_info ATTRIBUTE_UNUSED,
                       asection *asec,
                       bfd_byte *contents ATTRIBUTE_UNUSED)
 {
@@ -2057,6 +2241,32 @@ struct ppc_elf_dyn_relocs
   bfd_size_type pc_count;
 };
 
+/* Track PLT entries needed for a given symbol.  We might need more
+   than one glink entry per symbol.  */
+struct plt_entry
+{
+  struct plt_entry *next;
+
+  /* -fPIC uses multiple GOT sections, one per file, called ".got2".
+     This field stores the offset into .got2 used to initialise the
+     GOT pointer reg.  It will always be at least 32768 (and for
+     current gcc this is the only offset used).  */
+  bfd_vma addend;
+
+  /* The .got2 section.  */
+  asection *sec;
+
+  /* PLT refcount or offset.  */
+  union
+    {
+      bfd_signed_vma refcount;
+      bfd_vma offset;
+    } plt;
+
+  /* .glink stub offset.  */
+  bfd_vma glink_offset;
+};
+
 /* 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.  */
@@ -2100,10 +2310,21 @@ struct ppc_elf_link_hash_entry
 #define TLS_TLS                16      /* Any TLS reloc.  */
 #define TLS_TPRELGD    32      /* TPREL reloc resulting from GD->IE. */
   char tls_mask;
+
+  /* Nonzero if we have seen a small data relocation referring to this
+     symbol.  */
+  unsigned char has_sda_refs;
 };
 
 #define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
 
+enum ppc_elf_plt_type {
+  PLT_UNSET,
+  PLT_OLD,
+  PLT_NEW,
+  PLT_VXWORKS
+};
+
 /* PPC ELF linker hash table.  */
 
 struct ppc_elf_link_hash_table
@@ -2113,6 +2334,7 @@ struct ppc_elf_link_hash_table
   /* Short-cuts to get to dynamic linker sections.  */
   asection *got;
   asection *relgot;
+  asection *glink;
   asection *plt;
   asection *relplt;
   asection *dynbss;
@@ -2131,8 +2353,41 @@ struct ppc_elf_link_hash_table
     bfd_vma offset;
   } tlsld_got;
 
+  /* Offset of PltResolve function in glink.  */
+  bfd_vma glink_pltresolve;
+
+  /* Size of reserved GOT entries.  */
+  unsigned int got_header_size;
+  /* Non-zero if allocating the header left a gap.  */
+  unsigned int got_gap;
+
+  /* The type of PLT we have chosen to use.  */
+  enum ppc_elf_plt_type plt_type;
+
+  /* Whether we can use the new PLT layout.  */
+  unsigned int can_use_new_plt:1;
+
+  /* Set if we should emit symbols for stubs.  */
+  unsigned int emit_stub_syms:1;
+
   /* Small local sym to section mapping cache.  */
   struct sym_sec_cache sym_sec;
+
+  /* The (unloaded but important) .rela.plt.unloaded on VxWorks.  */
+  asection *srelplt2;
+
+  /* The .got.plt section (VxWorks only)*/
+  asection *sgotplt;
+
+  /* True if the target system is VxWorks.  */
+  int is_vxworks;
+
+  /* The size of PLT entries.  */
+  int plt_entry_size;
+  /* The distance between adjacent PLT slots.  */
+  int plt_slot_size;
+  /* The size of the first PLT entry.  */
+  int plt_initial_entry_size;
 };
 
 /* Get the PPC ELF linker hash table from a link_info structure.  */
@@ -2180,13 +2435,19 @@ ppc_elf_link_hash_table_create (bfd *abfd)
   if (ret == NULL)
     return NULL;
 
-  if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd,
-                                      ppc_elf_link_hash_newfunc))
+  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
+                                     ppc_elf_link_hash_newfunc,
+                                     sizeof (struct ppc_elf_link_hash_entry)))
     {
       free (ret);
       return NULL;
     }
 
+  ret->elf.init_plt_refcount.refcount = 0;
+  ret->elf.init_plt_refcount.glist = NULL;
+  ret->elf.init_plt_offset.offset = 0;
+  ret->elf.init_plt_offset.glist = NULL;
+
   ret->sdata[0].name = ".sdata";
   ret->sdata[0].sym_name = "_SDA_BASE_";
   ret->sdata[0].bss_name = ".sbss";
@@ -2195,10 +2456,16 @@ ppc_elf_link_hash_table_create (bfd *abfd)
   ret->sdata[1].sym_name = "_SDA2_BASE_";
   ret->sdata[1].bss_name = ".sbss2";
 
+  ret->plt_entry_size = 12;
+  ret->plt_slot_size = 8;
+  ret->plt_initial_entry_size = 72;
+  
+  ret->is_vxworks = 0;
+
   return &ret->elf.root;
 }
 
-/* The powerpc .got has a blrl instruction in it.  Mark it executable.  */
+/* Create .got and the related sections.  */
 
 static bfd_boolean
 ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
@@ -2215,17 +2482,26 @@ ppc_elf_create_got (bfd *abfd, struct bfd_link_info *info)
   if (s == NULL)
     abort ();
 
-  flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS | SEC_IN_MEMORY
-          | SEC_LINKER_CREATED);
-  if (!bfd_set_section_flags (abfd, s, flags))
-    return FALSE;
+  if (htab->is_vxworks)
+    {
+      htab->sgotplt = bfd_get_section_by_name (abfd, ".got.plt");
+      if (!htab->sgotplt)
+       abort ();
+    }
+  else
+    {
+      /* The powerpc .got has a blrl instruction in it.  Mark it
+        executable.  */
+      flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS
+              | SEC_IN_MEMORY | SEC_LINKER_CREATED);
+      if (!bfd_set_section_flags (abfd, s, flags))
+       return FALSE;
+    }
 
-  htab->relgot = bfd_make_section (abfd, ".rela.got");
+  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
+          | SEC_LINKER_CREATED | SEC_READONLY);
+  htab->relgot = bfd_make_section_with_flags (abfd, ".rela.got", flags);
   if (!htab->relgot
-      || ! bfd_set_section_flags (abfd, htab->relgot,
-                                 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
-                                  | SEC_IN_MEMORY | SEC_LINKER_CREATED
-                                  | SEC_READONLY))
       || ! bfd_set_section_alignment (abfd, htab->relgot, 2))
     return FALSE;
 
@@ -2252,38 +2528,52 @@ ppc_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
   if (!_bfd_elf_create_dynamic_sections (abfd, info))
     return FALSE;
 
-  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
-          | SEC_LINKER_CREATED);
+  flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
+          | SEC_IN_MEMORY | SEC_LINKER_CREATED);
 
-  htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
-  htab->dynsbss = s = bfd_make_section (abfd, ".dynsbss");
+  s = bfd_make_section_anyway_with_flags (abfd, ".glink", flags | SEC_CODE);
+  htab->glink = s;
   if (s == NULL
-      || ! bfd_set_section_flags (abfd, s, SEC_ALLOC | SEC_LINKER_CREATED))
+      || !bfd_set_section_alignment (abfd, s, 4))
+    return FALSE;
+
+  htab->dynbss = bfd_get_section_by_name (abfd, ".dynbss");
+  s = bfd_make_section_with_flags (abfd, ".dynsbss",
+                                  SEC_ALLOC | SEC_LINKER_CREATED);
+  htab->dynsbss = s;
+  if (s == NULL)
     return FALSE;
 
   if (! info->shared)
     {
       htab->relbss = bfd_get_section_by_name (abfd, ".rela.bss");
-      htab->relsbss = s = bfd_make_section (abfd, ".rela.sbss");
+      s = bfd_make_section_with_flags (abfd, ".rela.sbss", flags);
+      htab->relsbss = s;
       if (s == NULL
-         || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
          || ! bfd_set_section_alignment (abfd, s, 2))
        return FALSE;
     }
 
+  if (htab->is_vxworks
+      && !elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
+    return FALSE;
+
   htab->relplt = bfd_get_section_by_name (abfd, ".rela.plt");
   htab->plt = s = bfd_get_section_by_name (abfd, ".plt");
   if (s == NULL)
     abort ();
 
-  flags = SEC_ALLOC | SEC_CODE | SEC_IN_MEMORY | SEC_LINKER_CREATED;
+  flags = SEC_ALLOC | SEC_CODE | SEC_LINKER_CREATED;
+  if (htab->plt_type == PLT_VXWORKS)
+    /* The VxWorks PLT is a loaded section with contents.  */
+    flags |= SEC_HAS_CONTENTS | SEC_LOAD | SEC_READONLY;
   return bfd_set_section_flags (abfd, s, flags);
 }
 
 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
 
 static void
-ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
+ppc_elf_copy_indirect_symbol (struct bfd_link_info *info,
                              struct elf_link_hash_entry *dir,
                              struct elf_link_hash_entry *ind)
 {
@@ -2299,10 +2589,7 @@ ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
          struct ppc_elf_dyn_relocs **pp;
          struct ppc_elf_dyn_relocs *p;
 
-         if (ind->root.type == bfd_link_hash_indirect)
-           abort ();
-
-         /* Add reloc counts against the weak sym to the strong sym
+         /* Add reloc counts against the indirect sym to the direct sym
             list.  Merge any entries against the same section.  */
          for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
            {
@@ -2327,21 +2614,69 @@ ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
     }
 
   edir->tls_mask |= eind->tls_mask;
+  edir->has_sda_refs |= eind->has_sda_refs;
+
+  /* If called to transfer flags for a weakdef during processing
+     of elf_adjust_dynamic_symbol, don't copy non_got_ref.
+     We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
+  if (!(ELIMINATE_COPY_RELOCS
+       && eind->elf.root.type != bfd_link_hash_indirect
+       && edir->elf.dynamic_adjusted))
+    edir->elf.non_got_ref |= eind->elf.non_got_ref;
+
+  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;
+
+  /* If we were called to copy over info for a weak sym, that's all.  */
+  if (eind->elf.root.type != bfd_link_hash_indirect)
+    return;
 
-  if (ELIMINATE_COPY_RELOCS
-      && ind->root.type != bfd_link_hash_indirect
-      && dir->dynamic_adjusted)
+  /* Copy over the GOT refcount entries that we may have already seen to
+     the symbol which just became indirect.  */
+  edir->elf.got.refcount += eind->elf.got.refcount;
+  eind->elf.got.refcount = 0;
+
+  /* And plt entries.  */
+  if (eind->elf.plt.plist != NULL)
+    {
+      if (edir->elf.plt.plist != NULL)
+       {
+         struct plt_entry **entp;
+         struct plt_entry *ent;
+
+         for (entp = &eind->elf.plt.plist; (ent = *entp) != NULL; )
+           {
+             struct plt_entry *dent;
+
+             for (dent = edir->elf.plt.plist; dent != NULL; dent = dent->next)
+               if (dent->sec == ent->sec && dent->addend == ent->addend)
+                 {
+                   dent->plt.refcount += ent->plt.refcount;
+                   *entp = ent->next;
+                   break;
+                 }
+             if (dent == NULL)
+               entp = &ent->next;
+           }
+         *entp = edir->elf.plt.plist;
+       }
+
+      edir->elf.plt.plist = eind->elf.plt.plist;
+      eind->elf.plt.plist = NULL;
+    }
+
+  if (eind->elf.dynindx != -1)
     {
-      /* If called to transfer flags for a weakdef during processing
-        of elf_adjust_dynamic_symbol, don't copy non_got_ref.
-        We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
-      dir->ref_dynamic |= ind->ref_dynamic;
-      dir->ref_regular |= ind->ref_regular;
-      dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
-      dir->needs_plt |= ind->needs_plt;
+      if (edir->elf.dynindx != -1)
+       _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
+                               edir->elf.dynstr_index);
+      edir->elf.dynindx = eind->elf.dynindx;
+      edir->elf.dynstr_index = eind->elf.dynstr_index;
+      eind->elf.dynindx = -1;
+      eind->elf.dynstr_index = 0;
     }
-  else
-    _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
 }
 
 /* Return 1 if target is one of ours.  */
@@ -2350,9 +2685,12 @@ static bfd_boolean
 is_ppc_elf_target (const struct bfd_target *targ)
 {
   extern const bfd_target bfd_elf32_powerpc_vec;
+  extern const bfd_target bfd_elf32_powerpc_vxworks_vec;
   extern const bfd_target bfd_elf32_powerpcle_vec;
 
-  return targ == &bfd_elf32_powerpc_vec || targ == &bfd_elf32_powerpcle_vec;
+  return (targ == &bfd_elf32_powerpc_vec
+         || targ == &bfd_elf32_powerpc_vxworks_vec
+         || targ == &bfd_elf32_powerpcle_vec);
 }
 
 /* Hook called by the linker routine which adds symbols from an object
@@ -2384,9 +2722,10 @@ ppc_elf_add_symbol_hook (bfd *abfd,
          if (!htab->elf.dynobj)
            htab->elf.dynobj = abfd;
 
-         htab->sbss = bfd_make_section_anyway (htab->elf.dynobj, ".sbss");
-         if (htab->sbss == NULL
-             || ! bfd_set_section_flags (htab->elf.dynobj, htab->sbss, flags))
+         htab->sbss = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
+                                                          ".sbss",
+                                                          flags);
+         if (htab->sbss == NULL)
            return FALSE;
        }
 
@@ -2397,6 +2736,20 @@ ppc_elf_add_symbol_hook (bfd *abfd,
   return TRUE;
 }
 \f
+static bfd_boolean
+create_sdata_sym (struct ppc_elf_link_hash_table *htab,
+                 elf_linker_section_t *lsect)
+{
+  lsect->sym = elf_link_hash_lookup (&htab->elf, lsect->sym_name,
+                                    TRUE, FALSE, TRUE);
+  if (lsect->sym == NULL)
+    return FALSE;
+  if (lsect->sym->root.type == bfd_link_hash_new)
+    lsect->sym->non_elf = 0;
+  lsect->sym->ref_regular = 1;
+  return TRUE;
+}
+
 /* Create a special linker section.  */
 
 static bfd_boolean
@@ -2415,14 +2768,15 @@ ppc_elf_create_linker_section (bfd *abfd,
   if (!htab->elf.dynobj)
     htab->elf.dynobj = abfd;
 
-  s = bfd_make_section_anyway (htab->elf.dynobj, lsect->name);
+  s = bfd_make_section_anyway_with_flags (htab->elf.dynobj,
+                                         lsect->name,
+                                         flags);
   if (s == NULL
-      || !bfd_set_section_flags (htab->elf.dynobj, s, flags)
       || !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
     return FALSE;
   lsect->section = s;
 
-  return TRUE;
+  return create_sdata_sym (htab, lsect);
 }
 
 /* Find a linker generated pointer with a given addend and type.  */
@@ -2551,6 +2905,46 @@ update_local_sym_info (bfd *abfd,
   return TRUE;
 }
 
+static bfd_boolean
+update_plt_info (bfd *abfd, struct elf_link_hash_entry *h,
+                asection *sec, bfd_vma addend)
+{
+  struct plt_entry *ent;
+
+  if (addend < 32768)
+    sec = NULL;
+  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
+    if (ent->sec == sec && ent->addend == addend)
+      break;
+  if (ent == NULL)
+    {
+      bfd_size_type amt = sizeof (*ent);
+      ent = bfd_alloc (abfd, amt);
+      if (ent == NULL)
+       return FALSE;
+      ent->next = h->plt.plist;
+      ent->sec = sec;
+      ent->addend = addend;
+      ent->plt.refcount = 0;
+      h->plt.plist = ent;
+    }
+  ent->plt.refcount += 1;
+  return TRUE;
+}
+
+static struct plt_entry *
+find_plt_ent (struct elf_link_hash_entry *h, asection *sec, bfd_vma addend)
+{
+  struct plt_entry *ent;
+
+  if (addend < 32768)
+    sec = NULL;
+  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
+    if (ent->sec == sec && ent->addend == addend)
+      break;
+  return ent;
+}
+
 static void
 bad_shared_reloc (bfd *abfd, enum elf_ppc_reloc_type r_type)
 {
@@ -2576,11 +2970,20 @@ ppc_elf_check_relocs (bfd *abfd,
   struct elf_link_hash_entry **sym_hashes;
   const Elf_Internal_Rela *rel;
   const Elf_Internal_Rela *rel_end;
-  asection *sreloc;
+  asection *got2, *sreloc;
 
   if (info->relocatable)
     return TRUE;
 
+  /* Don't do anything special with non-loaded, non-alloced sections.
+     In particular, any relocs in such sections should not affect GOT
+     and PLT reference counting (ie. we don't allow them to create GOT
+     or PLT entries), there's no possibility or desire to optimize TLS
+     relocs, and there's not much point in propagating relocs to shared
+     libs that the dynamic linker won't relocate.  */
+  if ((sec->flags & SEC_ALLOC) == 0)
+    return TRUE;
+
 #ifdef DEBUG
   _bfd_error_handler ("ppc_elf_check_relocs called for section %A in %B",
                      sec, abfd);
@@ -2593,6 +2996,7 @@ ppc_elf_check_relocs (bfd *abfd,
   htab = ppc_elf_hash_table (info);
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
+  got2 = bfd_get_section_by_name (abfd, ".got2");
   sreloc = NULL;
 
   rel_end = relocs + sec->reloc_count;
@@ -2607,20 +3011,25 @@ ppc_elf_check_relocs (bfd *abfd,
       if (r_symndx < symtab_hdr->sh_info)
        h = NULL;
       else
-       h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+       {
+         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;
+       }
 
       /* If a relocation refers to _GLOBAL_OFFSET_TABLE_, create the .got.
         This shows up in particular in an R_PPC_ADDR32 in the eabi
         startup code.  */
-      if (h && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
+      if (h != NULL
+         && htab->got == NULL
+         && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
        {
-         if (htab->got == NULL)
-           {
-             if (htab->elf.dynobj == NULL)
-               htab->elf.dynobj = abfd;
-             if (!ppc_elf_create_got (htab->elf.dynobj, info))
-               return FALSE;
-           }
+         if (htab->elf.dynobj == NULL)
+           htab->elf.dynobj = abfd;
+         if (!ppc_elf_create_got (htab->elf.dynobj, info))
+           return FALSE;
+         BFD_ASSERT (h == htab->elf.hgot);
        }
 
       r_type = ELF32_R_TYPE (rel->r_info);
@@ -2697,6 +3106,11 @@ ppc_elf_check_relocs (bfd *abfd,
          if (!elf_create_pointer_linker_section (abfd, &htab->sdata[0],
                                                  h, rel))
            return FALSE;
+         if (h != NULL)
+           {
+             ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
+             h->non_got_ref = TRUE;
+           }
          break;
 
          /* Indirect .sdata2 relocation.  */
@@ -2713,12 +3127,65 @@ ppc_elf_check_relocs (bfd *abfd,
          if (!elf_create_pointer_linker_section (abfd, &htab->sdata[1],
                                                  h, rel))
            return FALSE;
+         if (h != NULL)
+           {
+             ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
+             h->non_got_ref = TRUE;
+           }
          break;
 
        case R_PPC_SDAREL16:
+         if (info->shared)
+           {
+             bad_shared_reloc (abfd, r_type);
+             return FALSE;
+           }
+         if (htab->sdata[0].sym == NULL
+             && !create_sdata_sym (htab, &htab->sdata[0]))
+           return FALSE;
+         if (h != NULL)
+           {
+             ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
+             h->non_got_ref = TRUE;
+           }
+         break;
+
        case R_PPC_EMB_SDA2REL:
+         if (info->shared)
+           {
+             bad_shared_reloc (abfd, r_type);
+             return FALSE;
+           }
+         if (htab->sdata[1].sym == NULL
+             && !create_sdata_sym (htab, &htab->sdata[1]))
+           return FALSE;
+         if (h != NULL)
+           {
+             ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
+             h->non_got_ref = TRUE;
+           }
+         break;
+
        case R_PPC_EMB_SDA21:
        case R_PPC_EMB_RELSDA:
+         if (info->shared)
+           {
+             bad_shared_reloc (abfd, r_type);
+             return FALSE;
+           }
+         if (htab->sdata[0].sym == NULL
+             && !create_sdata_sym (htab, &htab->sdata[0]))
+           return FALSE;
+         if (htab->sdata[1].sym == NULL
+             && !create_sdata_sym (htab, &htab->sdata[1]))
+           return FALSE;
+         if (h != NULL)
+           {
+             ppc_elf_hash_entry (h)->has_sda_refs = TRUE;
+             h->non_got_ref = TRUE;
+           }
+         break;
+
        case R_PPC_EMB_NADDR32:
        case R_PPC_EMB_NADDR16:
        case R_PPC_EMB_NADDR16_LO:
@@ -2729,6 +3196,8 @@ ppc_elf_check_relocs (bfd *abfd,
              bad_shared_reloc (abfd, r_type);
              return FALSE;
            }
+         if (h != NULL)
+           h->non_got_ref = TRUE;
          break;
 
        case R_PPC_PLT32:
@@ -2759,9 +3228,14 @@ ppc_elf_check_relocs (bfd *abfd,
              bfd_set_error (bfd_error_bad_value);
              return FALSE;
            }
+         else
+           {
+             bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
 
-         h->needs_plt = 1;
-         h->plt.refcount++;
+             h->needs_plt = 1;
+             if (!update_plt_info (abfd, h, got2, addend))
+               return FALSE;
+           }
          break;
 
          /* The following relocations don't need to propagate the
@@ -2778,7 +3252,14 @@ ppc_elf_check_relocs (bfd *abfd,
        case R_PPC_TOC16:
          break;
 
-         /* This are just markers.  */
+       case R_PPC_REL16:
+       case R_PPC_REL16_LO:
+       case R_PPC_REL16_HI:
+       case R_PPC_REL16_HA:
+         htab->can_use_new_plt = 1;
+         break;
+
+         /* These are just markers.  */
        case R_PPC_TLS:
        case R_PPC_EMB_MRKREF:
        case R_PPC_NONE:
@@ -2803,6 +3284,8 @@ ppc_elf_check_relocs (bfd *abfd,
 
          /* This refers only to functions defined in the shared library.  */
        case R_PPC_LOCAL24PC:
+         if (h && h == htab->elf.hgot && htab->plt_type == PLT_UNSET)
+           htab->plt_type = PLT_OLD;
          break;
 
          /* This relocation describes the C++ object vtable hierarchy.
@@ -2838,17 +3321,42 @@ ppc_elf_check_relocs (bfd *abfd,
            info->flags |= DF_STATIC_TLS;
          goto dodyn;
 
-         /* When creating a shared object, we must copy these
-            relocs into the output file.  We create a reloc
-            section in dynobj and make room for the reloc.  */
+       case R_PPC_REL32:
+         if (h == NULL
+             && got2 != NULL
+             && (sec->flags & SEC_CODE) != 0
+             && (info->shared || info->pie)
+             && htab->plt_type == PLT_UNSET)
+           {
+             /* Old -fPIC gcc code has .long LCTOC1-LCFx just before
+                the start of a function, which assembles to a REL32
+                reference to .got2.  If we detect one of these, then
+                force the old PLT layout because the linker cannot
+                reliably deduce the GOT pointer value needed for
+                PLT call stubs.  */
+             asection *s;
+
+             s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
+                                            r_symndx);
+             if (s == got2)
+               htab->plt_type = PLT_OLD;
+           }
+         if (h == NULL || h == htab->elf.hgot)
+           break;
+         goto dodyn1;
+
        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
-             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
+         if (h == NULL)
            break;
+         if (h == htab->elf.hgot)
+           {
+             if (htab->plt_type == PLT_UNSET)
+               htab->plt_type = PLT_OLD;
+             break;
+           }
          /* fall through */
 
        case R_PPC_ADDR32:
@@ -2862,11 +3370,13 @@ ppc_elf_check_relocs (bfd *abfd,
        case R_PPC_ADDR14_BRNTAKEN:
        case R_PPC_UADDR32:
        case R_PPC_UADDR16:
+       dodyn1:
          if (h != NULL && !info->shared)
            {
              /* We may need a plt entry if the symbol turns out to be
                 a function defined in a dynamic object.  */
-             h->plt.refcount++;
+             if (!update_plt_info (abfd, h, NULL, 0))
+               return FALSE;
 
              /* We may need a copy reloc too.  */
              h->non_got_ref = 1;
@@ -2902,7 +3412,6 @@ ppc_elf_check_relocs (bfd *abfd,
                           || !h->def_regular))))
              || (ELIMINATE_COPY_RELOCS
                  && !info->shared
-                 && (sec->flags & SEC_ALLOC) != 0
                  && h != NULL
                  && (h->root.type == bfd_link_hash_defweak
                      || !h->def_regular)))
@@ -2928,7 +3437,7 @@ ppc_elf_check_relocs (bfd *abfd,
                  if (name == NULL)
                    return FALSE;
 
-                 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
+                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
                              && strcmp (bfd_get_section_name (abfd, sec),
                                         name + 5) == 0);
 
@@ -2939,14 +3448,13 @@ ppc_elf_check_relocs (bfd *abfd,
                    {
                      flagword flags;
 
-                     sreloc = bfd_make_section (htab->elf.dynobj, name);
                      flags = (SEC_HAS_CONTENTS | SEC_READONLY
-                              | SEC_IN_MEMORY | SEC_LINKER_CREATED);
-                     if ((sec->flags & SEC_ALLOC) != 0)
-                       flags |= SEC_ALLOC | SEC_LOAD;
+                              | SEC_IN_MEMORY | SEC_LINKER_CREATED
+                              | SEC_ALLOC | SEC_LOAD);
+                     sreloc = bfd_make_section_with_flags (htab->elf.dynobj,
+                                                           name,
+                                                           flags);
                      if (sreloc == NULL
-                         || ! bfd_set_section_flags (htab->elf.dynobj,
-                                                     sreloc, flags)
                          || ! bfd_set_section_alignment (htab->elf.dynobj,
                                                          sreloc, 2))
                        return FALSE;
@@ -2967,13 +3475,15 @@ ppc_elf_check_relocs (bfd *abfd,
                     easily.  Oh well.  */
 
                  asection *s;
+                 void *vpp;
+
                  s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
                                                 sec, r_symndx);
                  if (s == NULL)
                    return FALSE;
 
-                 head = ((struct ppc_elf_dyn_relocs **)
-                         &elf_section_data (s)->local_dynrel);
+                 vpp = &elf_section_data (s)->local_dynrel;
+                 head = (struct ppc_elf_dyn_relocs **) vpp;
                }
 
              p = *head;
@@ -3093,43 +3603,71 @@ ppc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
   return TRUE;
 }
 \f
+/* Choose which PLT scheme to use, and set .plt flags appropriately.
+   Returns -1 on error, 0 for old PLT, 1 for new PLT.  */
+int
+ppc_elf_select_plt_layout (bfd *output_bfd ATTRIBUTE_UNUSED,
+                          struct bfd_link_info *info,
+                          int force_old_plt,
+                          int emit_stub_syms)
+{
+  struct ppc_elf_link_hash_table *htab;
+  flagword flags;
+
+  htab = ppc_elf_hash_table (info);
+
+  if (htab->plt_type == PLT_UNSET)
+    htab->plt_type = (force_old_plt || !htab->can_use_new_plt
+                     ? PLT_OLD : PLT_NEW);
+
+  htab->emit_stub_syms = emit_stub_syms;
+
+  BFD_ASSERT (htab->plt_type != PLT_VXWORKS);
+
+  if (htab->plt_type == PLT_NEW)
+    {
+      flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
+              | SEC_IN_MEMORY | SEC_LINKER_CREATED);
+
+      /* The new PLT is a loaded section.  */
+      if (htab->plt != NULL
+         && !bfd_set_section_flags (htab->elf.dynobj, htab->plt, flags))
+       return -1;
+
+      /* The new GOT is not executable.  */
+      if (htab->got != NULL
+         && !bfd_set_section_flags (htab->elf.dynobj, htab->got, flags))
+       return -1;
+    }
+  else
+    {
+      /* Stop an unused .glink section from affecting .text alignment.  */
+      if (htab->glink != NULL
+         && !bfd_set_section_alignment (htab->elf.dynobj, htab->glink, 0))
+       return -1;
+    }
+  return htab->plt_type == PLT_NEW;
+}
+\f
 /* Return the section that should be marked against GC for a given
    relocation.  */
 
 static asection *
 ppc_elf_gc_mark_hook (asection *sec,
-                     struct bfd_link_info *info ATTRIBUTE_UNUSED,
+                     struct bfd_link_info *info,
                      Elf_Internal_Rela *rel,
                      struct elf_link_hash_entry *h,
                      Elf_Internal_Sym *sym)
 {
   if (h != NULL)
-    {
-      switch (ELF32_R_TYPE (rel->r_info))
-       {
-       case R_PPC_GNU_VTINHERIT:
-       case R_PPC_GNU_VTENTRY:
-         break;
-
-       default:
-         switch (h->root.type)
-           {
-           case bfd_link_hash_defined:
-           case bfd_link_hash_defweak:
-             return h->root.u.def.section;
-
-           case bfd_link_hash_common:
-             return h->root.u.c.p->section;
-
-           default:
-             break;
-           }
-       }
-    }
-  else
-    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
-
-  return NULL;
+    switch (ELF32_R_TYPE (rel->r_info))
+      {
+      case R_PPC_GNU_VTINHERIT:
+      case R_PPC_GNU_VTENTRY:
+       return NULL;
+      }
+
+  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
 }
 
 /* Update the got, plt and dynamic reloc reference counts for the
@@ -3146,6 +3684,10 @@ ppc_elf_gc_sweep_hook (bfd *abfd,
   struct elf_link_hash_entry **sym_hashes;
   bfd_signed_vma *local_got_refcounts;
   const Elf_Internal_Rela *rel, *relend;
+  asection *got2;
+
+  if ((sec->flags & SEC_ALLOC) == 0)
+    return TRUE;
 
   elf_section_data (sec)->local_dynrel = NULL;
 
@@ -3153,6 +3695,7 @@ ppc_elf_gc_sweep_hook (bfd *abfd,
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   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++)
@@ -3225,8 +3768,7 @@ ppc_elf_gc_sweep_hook (bfd *abfd,
        case R_PPC_REL14_BRTAKEN:
        case R_PPC_REL14_BRNTAKEN:
        case R_PPC_REL32:
-         if (h == NULL
-             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
+         if (h == NULL || h == htab->elf.hgot)
            break;
          /* Fall thru */
 
@@ -3241,15 +3783,21 @@ ppc_elf_gc_sweep_hook (bfd *abfd,
        case R_PPC_ADDR14_BRNTAKEN:
        case R_PPC_UADDR32:
        case R_PPC_UADDR16:
+         if (info->shared)
+           break;
+
        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)
            {
-             if (h->plt.refcount > 0)
-               h->plt.refcount--;
+             bfd_vma addend = r_type == R_PPC_PLTREL24 ? rel->r_addend : 0;
+             struct plt_entry *ent = find_plt_ent (h, got2, addend);
+             if (ent->plt.refcount > 0)
+               ent->plt.refcount -= 1;
            }
          break;
 
@@ -3268,9 +3816,16 @@ ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
   struct ppc_elf_link_hash_table *htab;
 
   htab = ppc_elf_hash_table (info);
+  if (htab->plt_type == PLT_NEW
+      && htab->plt != NULL
+      && htab->plt->output_section != NULL)
+    {
+      elf_section_type (htab->plt->output_section) = SHT_PROGBITS;
+      elf_section_flags (htab->plt->output_section) = SHF_ALLOC + SHF_WRITE;
+    }
+
   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
                                             FALSE, FALSE, TRUE);
-
   return _bfd_elf_tls_setup (obfd, info);
 }
 
@@ -3392,8 +3947,9 @@ ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
                        && h != NULL
                        && h == htab->tls_get_addr)
                      {
-                       if (h->plt.refcount > 0)
-                         h->plt.refcount -= 1;
+                       struct plt_entry *ent = find_plt_ent (h, NULL, 0);
+                       if (ent != NULL && ent->plt.refcount > 0)
+                         ent->plt.refcount -= 1;
                      }
                    expecting_tls_get_addr = 0;
                    continue;
@@ -3501,7 +4057,11 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
     {
       /* Clear procedure linkage table information for any symbol that
         won't need a .plt entry.  */
-      if (h->plt.refcount <= 0
+      struct plt_entry *ent;
+      for (ent = h->plt.plist; ent != NULL; ent = ent->next)
+       if (ent->plt.refcount > 0)
+         break;
+      if (ent == NULL
          || SYMBOL_CALLS_LOCAL (info, h)
          || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
              && h->root.type == bfd_link_hash_undefweak))
@@ -3516,13 +4076,13 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
 
             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->plt.plist = NULL;
          h->needs_plt = 0;
        }
       return TRUE;
     }
   else
-    h->plt.offset = (bfd_vma) -1;
+    h->plt.plist = NULL;
 
   /* If this is a weak symbol, and there is a real definition, the
      processor independent code will have arranged for us to see the
@@ -3553,7 +4113,11 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   if (!h->non_got_ref)
     return TRUE;
 
-  if (ELIMINATE_COPY_RELOCS)
+   /* 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.  We can't
+      do this if there are any small data relocations.  */
+  if (ELIMINATE_COPY_RELOCS
+      && !ppc_elf_hash_entry (h)->has_sda_refs)
     {
       struct ppc_elf_dyn_relocs *p;
       for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
@@ -3563,8 +4127,6 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
            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->non_got_ref = 0;
@@ -3572,6 +4134,13 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
        }
     }
 
+  if (h->size == 0)
+    {
+      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
+                            h->root.root.string);
+      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
@@ -3582,11 +4151,10 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
      both the dynamic object and the regular object will refer to the
      same memory location for the variable.
 
-     Of course, if the symbol is sufficiently small, we must instead
-     allocate it in .sbss.  FIXME: It would be better to do this if and
-     only if there were actually SDAREL relocs for that symbol.  */
+     Of course, if the symbol is referenced using SDAREL relocs, we
+     must instead allocate it in .sbss.  */
 
-  if (h->size <= elf_gp_size (htab->elf.dynobj))
+  if (ppc_elf_hash_entry (h)->has_sda_refs)
     s = htab->dynsbss;
   else
     s = htab->dynbss;
@@ -3600,7 +4168,7 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
     {
       asection *srel;
 
-      if (h->size <= elf_gp_size (htab->elf.dynobj))
+      if (ppc_elf_hash_entry (h)->has_sda_refs)
        srel = htab->relsbss;
       else
        srel = htab->relbss;
@@ -3633,6 +4201,95 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
   return TRUE;
 }
 \f
+/* Generate a symbol to mark plt call stubs.  For non-PIC code the sym is
+   xxxxxxxx.plt_call32.<callee> where xxxxxxxx is a hex number, usually 0,
+   specifying the addend on the plt relocation.  For -fpic code, the sym
+   is xxxxxxxx.plt_pic32.<callee>, and for -fPIC
+   xxxxxxxx.got2.plt_pic32.<callee>.  */
+
+static bfd_boolean
+add_stub_sym (struct plt_entry *ent,
+             struct elf_link_hash_entry *h,
+             struct bfd_link_info *info)
+{
+  struct elf_link_hash_entry *sh;
+  size_t len1, len2, len3;
+  char *name;
+  const char *stub;
+  struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
+
+  if (info->shared || info->pie)
+    stub = ".plt_pic32.";
+  else
+    stub = ".plt_call32.";
+
+  len1 = strlen (h->root.root.string);
+  len2 = strlen (stub);
+  len3 = 0;
+  if (ent->sec)
+    len3 = strlen (ent->sec->name);
+  name = bfd_malloc (len1 + len2 + len3 + 9);
+  if (name == NULL)
+    return FALSE;
+  sprintf (name, "%08x", (unsigned) ent->addend & 0xffffffff);
+  if (ent->sec)
+    memcpy (name + 8, ent->sec->name, len3);
+  memcpy (name + 8 + len3, stub, len2);
+  memcpy (name + 8 + len3 + len2, h->root.root.string, len1 + 1);
+  sh = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
+  if (sh == NULL)
+    return FALSE;
+  if (sh->root.type == bfd_link_hash_new)
+    {
+      sh->root.type = bfd_link_hash_defined;
+      sh->root.u.def.section = htab->glink;
+      sh->root.u.def.value = ent->glink_offset;
+      sh->ref_regular = 1;
+      sh->def_regular = 1;
+      sh->ref_regular_nonweak = 1;
+      sh->forced_local = 1;
+      sh->non_elf = 0;
+    }
+  return TRUE;
+}
+
+/* Allocate NEED contiguous space in .got, and return the offset.
+   Handles allocation of the got header when crossing 32k.  */
+
+static bfd_vma
+allocate_got (struct ppc_elf_link_hash_table *htab, unsigned int need)
+{
+  bfd_vma where;
+  unsigned int max_before_header;
+
+  if (htab->plt_type == PLT_VXWORKS)
+    {
+      where = htab->got->size;
+      htab->got->size += need;
+    }
+  else
+    {
+      max_before_header = htab->plt_type == PLT_NEW ? 32768 : 32764;
+      if (need <= htab->got_gap)
+       {
+         where = max_before_header - htab->got_gap;
+         htab->got_gap -= need;
+       }
+      else
+       {
+         if (htab->got->size + need > max_before_header
+             && htab->got->size <= max_before_header)
+           {
+             htab->got_gap = max_before_header - htab->got->size;
+             htab->got->size = max_before_header + htab->got_header_size;
+           }
+         where = htab->got->size;
+         htab->got->size += need;
+       }
+    }
+  return where;
+}
+
 /* Allocate space in associated reloc sections for dynamic relocs.  */
 
 static bfd_boolean
@@ -3653,66 +4310,144 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
     h = (struct elf_link_hash_entry *) h->root.u.i.link;
 
   htab = ppc_elf_hash_table (info);
-  if (htab->elf.dynamic_sections_created
-      && h->plt.refcount > 0)
+  if (htab->elf.dynamic_sections_created)
     {
-      /* Make sure this symbol is output as a dynamic symbol.  */
-      if (h->dynindx == -1
-         && !h->forced_local)
-       {
-         if (! bfd_elf_link_record_dynamic_symbol (info, h))
-           return FALSE;
-       }
+      struct plt_entry *ent;
+      bfd_boolean doneone = FALSE;
+      bfd_vma plt_offset = 0, glink_offset = 0;
 
-      if (info->shared
-         || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
-       {
-         asection *s = htab->plt;
-
-         /* If this is the first .plt entry, make room for the special
-            first entry.  */
-         if (s->size == 0)
-           s->size += 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.  */
-         h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
-                          + (PLT_SLOT_SIZE
-                             * ((s->size - PLT_INITIAL_ENTRY_SIZE)
-                                / 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 required to make
-            function pointers compare as equal between the normal
-            executable and the shared library.  */
-         if (! info->shared
-             && !h->def_regular)
-           {
-             h->root.u.def.section = s;
-             h->root.u.def.value = h->plt.offset;
-           }
+      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)
+             {
+               if (! bfd_elf_link_record_dynamic_symbol (info, h))
+                 return FALSE;
+             }
 
-         /* Make room for this entry.  After the 8192nd entry, room
-            for two entries is allocated.  */
-         s->size += PLT_ENTRY_SIZE;
-         if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
-             > PLT_NUM_SINGLE_ENTRIES)
-           s->size += PLT_ENTRY_SIZE;
+           if (info->shared
+               || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
+             {
+               asection *s = htab->plt;
 
-         /* We also need to make an entry in the .rela.plt section.  */
-         htab->relplt->size += sizeof (Elf32_External_Rela);
-       }
-      else
-       {
-         h->plt.offset = (bfd_vma) -1;
-         h->needs_plt = 0;
-       }
+               if (htab->plt_type == PLT_NEW)
+                 {
+                   if (!doneone)
+                     {
+                       plt_offset = s->size;
+                       s->size += 4;
+                     }
+                   ent->plt.offset = plt_offset;
+
+                   s = htab->glink;
+                   if (!doneone || info->shared || info->pie)
+                     {
+                       glink_offset = s->size;
+                       s->size += GLINK_ENTRY_SIZE;
+                     }
+                   if (!doneone
+                       && !info->shared
+                       && !h->def_regular)
+                     {
+                       h->root.u.def.section = s;
+                       h->root.u.def.value = glink_offset;
+                     }
+                   ent->glink_offset = glink_offset;
+
+                   if (htab->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 required to make
+                          function pointers compare as equal between
+                          the normal executable and the shared library.  */
+                       if (! info->shared
+                           && !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)
+                 {
+                   htab->relplt->size += sizeof (Elf32_External_Rela);
+
+                   if (htab->plt_type == PLT_VXWORKS)
+                     {
+                       /* Allocate space for the unloaded relocations.  */
+                       if (!info->shared)
+                         {
+                           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->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.offset = (bfd_vma) -1;
+      h->plt.plist = NULL;
       h->needs_plt = 0;
     }
 
@@ -3734,33 +4469,32 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       else
        {
          bfd_boolean dyn;
-         eh->elf.got.offset = htab->got->size;
+         unsigned int need = 0;
          if ((eh->tls_mask & TLS_TLS) != 0)
            {
              if ((eh->tls_mask & TLS_LD) != 0)
-               htab->got->size += 8;
+               need += 8;
              if ((eh->tls_mask & TLS_GD) != 0)
-               htab->got->size += 8;
+               need += 8;
              if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
-               htab->got->size += 4;
+               need += 4;
              if ((eh->tls_mask & TLS_DTPREL) != 0)
-               htab->got->size += 4;
+               need += 4;
            }
          else
-           htab->got->size += 4;
+           need += 4;
+         eh->elf.got.offset = allocate_got (htab, need);
          dyn = htab->elf.dynamic_sections_created;
          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->size
-               += ((htab->got->size - eh->elf.got.offset) / 4
-                   * sizeof (Elf32_External_Rela));
-             /* Except LD only needs one.  */
+             /* All the entries we allocated need relocs.
+                Except LD only needs one.  */
              if ((eh->tls_mask & TLS_LD) != 0)
-               htab->relgot->size -= sizeof (Elf32_External_Rela);
+               need -= 4;
+             htab->relgot->size += need * (sizeof (Elf32_External_Rela) / 4);
            }
        }
     }
@@ -3801,20 +4535,20 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 
       /* Also discard relocs on undefined weak syms with non-default
         visibility.  */
-      if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
+      if (eh->dyn_relocs != NULL
          && h->root.type == bfd_link_hash_undefweak)
-       eh->dyn_relocs = NULL;
-
-      /* Make sure undefined weak symbols are output as a dynamic symbol
-        in PIEs.  */
-      if (info->pie
-         && eh->dyn_relocs != NULL
-         && h->dynindx == -1
-         && h->root.type == bfd_link_hash_undefweak
-         && !h->forced_local)
        {
-         if (! bfd_elf_link_record_dynamic_symbol (info, h))
-           return FALSE;
+         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)
+           {
+             if (! bfd_elf_link_record_dynamic_symbol (info, h))
+               return FALSE;
+           }
        }
     }
   else if (ELIMINATE_COPY_RELOCS)
@@ -3917,15 +4651,10 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
        }
     }
 
-  if (htab->tlsld_got.refcount > 0)
-    {
-      htab->tlsld_got.offset = htab->got->size;
-      htab->got->size += 8;
-      if (info->shared)
-       htab->relgot->size += sizeof (Elf32_External_Rela);
-    }
-  else
-    htab->tlsld_got.offset = (bfd_vma) -1;
+  if (htab->plt_type == PLT_OLD)
+    htab->got_header_size = 16;
+  else if (htab->plt_type == PLT_NEW)
+    htab->got_header_size = 12;
 
   /* Set up .got offsets for local syms, and space for local dynamic
      relocs.  */
@@ -3936,7 +4665,6 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       char *lgot_masks;
       bfd_size_type locsymcount;
       Elf_Internal_Shdr *symtab_hdr;
-      asection *srel;
 
       if (!is_ppc_elf_target (ibfd->xvec))
        continue;
@@ -3978,8 +4706,6 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       locsymcount = symtab_hdr->sh_info;
       end_local_got = local_got + locsymcount;
       lgot_masks = (char *) end_local_got;
-      s = htab->got;
-      srel = htab->relgot;
       for (; local_got < end_local_got; ++local_got, ++lgot_masks)
        if (*local_got > 0)
          {
@@ -3987,53 +4713,132 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
              {
                /* If just an LD reloc, we'll just use
                   htab->tlsld_got.offset.  */
-               if (htab->tlsld_got.offset == (bfd_vma) -1)
-                 {
-                   htab->tlsld_got.offset = s->size;
-                   s->size += 8;
-                   if (info->shared)
-                     srel->size += sizeof (Elf32_External_Rela);
-                 }
+               htab->tlsld_got.refcount += 1;
                *local_got = (bfd_vma) -1;
              }
            else
              {
-               *local_got = s->size;
+               unsigned int need = 0;
                if ((*lgot_masks & TLS_TLS) != 0)
                  {
                    if ((*lgot_masks & TLS_GD) != 0)
-                     s->size += 8;
+                     need += 8;
                    if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
-                     s->size += 4;
+                     need += 4;
                    if ((*lgot_masks & TLS_DTPREL) != 0)
-                     s->size += 4;
+                     need += 4;
                  }
                else
-                 s->size += 4;
+                 need += 4;
+               *local_got = allocate_got (htab, need);
                if (info->shared)
-                 srel->size += ((s->size - *local_got) / 4
-                                * sizeof (Elf32_External_Rela));
+                 htab->relgot->size += (need
+                                        * (sizeof (Elf32_External_Rela) / 4));
              }
          }
        else
          *local_got = (bfd_vma) -1;
     }
 
+  if (htab->tlsld_got.refcount > 0)
+    {
+      htab->tlsld_got.offset = allocate_got (htab, 8);
+      if (info->shared)
+       htab->relgot->size += sizeof (Elf32_External_Rela);
+    }
+  else
+    htab->tlsld_got.offset = (bfd_vma) -1;
+
   /* Allocate space for global sym dynamic relocs.  */
   elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, info);
 
+  if (htab->got != NULL && htab->plt_type != PLT_VXWORKS)
+    {
+      unsigned int g_o_t = 32768;
+
+      /* If we haven't allocated the header, do so now.  When we get here,
+        for old plt/got the got size will be 0 to 32764 (not allocated),
+        or 32780 to 65536 (header allocated).  For new plt/got, the
+        corresponding ranges are 0 to 32768 and 32780 to 65536.  */
+      if (htab->got->size <= 32768)
+       {
+         g_o_t = htab->got->size;
+         if (htab->plt_type == PLT_OLD)
+           g_o_t += 4;
+         htab->got->size += htab->got_header_size;
+       }
+
+      htab->elf.hgot->root.u.def.value = g_o_t;
+    }
+
+  if (htab->glink != NULL && htab->glink->size != 0)
+    {
+      htab->glink_pltresolve = htab->glink->size;
+      /* Space for the branch table.  */
+      htab->glink->size += htab->glink->size / (GLINK_ENTRY_SIZE / 4) - 4;
+      /* Pad out to align the start of PLTresolve.  */
+      htab->glink->size += -htab->glink->size & 15;
+      htab->glink->size += GLINK_PLTRESOLVE;
+
+      if (htab->emit_stub_syms)
+       {
+         struct elf_link_hash_entry *sh;
+         sh = elf_link_hash_lookup (&htab->elf, "__glink",
+                                    TRUE, FALSE, FALSE);
+         if (sh == NULL)
+           return FALSE;
+         if (sh->root.type == bfd_link_hash_new)
+           {
+             sh->root.type = bfd_link_hash_defined;
+             sh->root.u.def.section = htab->glink;
+             sh->root.u.def.value = htab->glink_pltresolve;
+             sh->ref_regular = 1;
+             sh->def_regular = 1;
+             sh->ref_regular_nonweak = 1;
+             sh->forced_local = 1;
+             sh->non_elf = 0;
+           }
+         sh = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
+                                    TRUE, FALSE, FALSE);
+         if (sh == NULL)
+           return FALSE;
+         if (sh->root.type == bfd_link_hash_new)
+           {
+             sh->root.type = bfd_link_hash_defined;
+             sh->root.u.def.section = htab->glink;
+             sh->root.u.def.value = htab->glink->size - GLINK_PLTRESOLVE;
+             sh->ref_regular = 1;
+             sh->def_regular = 1;
+             sh->ref_regular_nonweak = 1;
+             sh->forced_local = 1;
+             sh->non_elf = 0;
+           }
+       }
+    }
+
   /* We've now determined the sizes of the various dynamic sections.
      Allocate memory for them.  */
   relocs = FALSE;
   for (s = htab->elf.dynobj->sections; s != NULL; s = s->next)
     {
+      bfd_boolean strip_section = TRUE;
+
       if ((s->flags & SEC_LINKER_CREATED) == 0)
        continue;
 
       if (s == htab->plt
+         || s == htab->glink
          || s == htab->got
-         || s == htab->sbss)
+         || s == htab->sgotplt
+         || s == htab->sbss
+         || s == htab->dynbss
+         || s == htab->dynsbss)
        {
+         /* We'd like to strip these sections if they aren't needed, but if
+            we've exported dynamic symbols from them we must leave them.
+            It's too late to tell BFD to get rid of the symbols.  */
+         if ((s == htab->plt || s == htab->got) && htab->elf.hplt != NULL)
+           strip_section = FALSE;
          /* Strip this section if we don't need it; see the
             comment below.  */
        }
@@ -4042,21 +4847,9 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
        {
          /* Strip these too.  */
        }
-      else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
+      else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
        {
-         if (s->size == 0)
-           {
-             /* If we don't need this section, strip it from the
-                output file.  This is mostly to handle .rela.bss and
-                .rela.plt.  We must create both sections in
-                create_dynamic_sections, because they must be created
-                before the linker maps input sections to output
-                sections.  The linker does that before
-                adjust_dynamic_symbol is called, and it is that
-                function which decides whether anything needs to go
-                into these sections.  */
-           }
-         else
+         if (s->size != 0)
            {
              /* Remember whether there are any relocation sections.  */
              relocs = TRUE;
@@ -4072,13 +4865,22 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
          continue;
        }
 
-      if (s->size == 0)
+      if (s->size == 0 && strip_section)
        {
-         _bfd_strip_section_from_output (info, s);
+         /* If we don't need this section, strip it from the
+            output file.  This is mostly to handle .rela.bss and
+            .rela.plt.  We must create both sections in
+            create_dynamic_sections, because they must be created
+            before the linker maps input sections to output
+            sections.  The linker does that before
+            adjust_dynamic_symbol is called, and it is that
+            function which decides whether anything needs to go
+            into these sections.  */
+         s->flags |= SEC_EXCLUDE;
          continue;
        }
 
-      if (s == htab->sbss)
+      if ((s->flags & SEC_HAS_CONTENTS) == 0)
        continue;
 
       /* Allocate memory for the section contents.  */
@@ -4112,6 +4914,12 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
            return FALSE;
        }
 
+      if (htab->glink != NULL && htab->glink->size != 0)
+       {
+         if (!add_dynamic_entry (DT_PPC_GOT, 0))
+           return FALSE;
+       }
+
       if (relocs)
        {
          if (!add_dynamic_entry (DT_RELA, 0)
@@ -4180,13 +4988,17 @@ ppc_elf_relax_section (bfd *abfd,
   Elf_Internal_Rela *irel, *irelend;
   struct one_fixup *fixups = NULL;
   bfd_boolean changed;
-  struct ppc_elf_link_hash_table *ppc_info;
+  struct ppc_elf_link_hash_table *htab;
   bfd_size_type trampoff;
+  asection *got2;
 
   *again = FALSE;
 
-  /* Nothing to do if there are no relocations.  */
-  if ((isec->flags & SEC_RELOC) == 0 || isec->reloc_count == 0)
+  /* Nothing to do if there are no relocations, and no need to do
+     anything with non-alloc sections.  */
+  if ((isec->flags & SEC_ALLOC) == 0
+      || (isec->flags & SEC_RELOC) == 0
+      || isec->reloc_count == 0)
     return TRUE;
 
   trampoff = (isec->size + 3) & (bfd_vma) -4;
@@ -4201,9 +5013,10 @@ ppc_elf_relax_section (bfd *abfd,
   if (internal_relocs == NULL)
     goto error_return;
 
-  ppc_info = ppc_elf_hash_table (link_info);
-  irelend = internal_relocs + isec->reloc_count;
+  htab = ppc_elf_hash_table (link_info);
+  got2 = bfd_get_section_by_name (abfd, ".got2");
 
+  irelend = internal_relocs + isec->reloc_count;
   for (irel = internal_relocs; irel < irelend; irel++)
     {
       unsigned long r_type = ELF32_R_TYPE (irel->r_info);
@@ -4277,13 +5090,29 @@ ppc_elf_relax_section (bfd *abfd,
                 || h->root.type == bfd_link_hash_warning)
            h = (struct elf_link_hash_entry *) h->root.u.i.link;
 
+         tsec = NULL;
+         toff = 0;
          if (r_type == R_PPC_PLTREL24
-             && ppc_info->plt != NULL
-             && h->plt.offset != (bfd_vma) -1)
+             && htab->plt != NULL)
            {
-             tsec = ppc_info->plt;
-             toff = h->plt.offset;
+             struct plt_entry *ent = find_plt_ent (h, got2, irel->r_addend);
+
+             if (ent != NULL)
+               {
+                 if (htab->plt_type == PLT_NEW)
+                   {
+                     tsec = htab->glink;
+                     toff = ent->glink_offset;
+                   }
+                 else
+                   {
+                     tsec = htab->plt;
+                     toff = ent->plt.offset;
+                   }
+               }
            }
+         if (tsec != NULL)
+           ;
          else if (h->root.type == bfd_link_hash_defined
                   || h->root.type == bfd_link_hash_defweak)
            {
@@ -4335,9 +5164,14 @@ ppc_elf_relax_section (bfd *abfd,
          if (sym_type != STT_SECTION)
            toff += irel->r_addend;
        }
-      else
+      /* PLTREL24 addends are special.  */
+      else if (r_type != R_PPC_PLTREL24)
        toff += irel->r_addend;
 
+      /* Attempted -shared link of non-pic code loses.  */
+      if (tsec->output_section == NULL)
+       continue;
+
       symaddr = tsec->output_section->vma + tsec->output_offset + toff;
 
       roff = irel->r_offset;
@@ -4379,7 +5213,8 @@ ppc_elf_relax_section (bfd *abfd,
          if (R_PPC_RELAX32_PLT - R_PPC_RELAX32
              != R_PPC_RELAX32PC_PLT - R_PPC_RELAX32PC)
            abort ();
-         if (tsec == ppc_info->plt)
+         if (tsec == htab->plt
+             || tsec == htab->glink)
            stub_rtype += R_PPC_RELAX32_PLT - R_PPC_RELAX32;
 
          /* Hijack the old relocation.  Since we need two
@@ -4545,74 +5380,19 @@ ppc_elf_relax_section (bfd *abfd,
   return FALSE;
 }
 \f
-/* Set SYM_NAME to VAL if the symbol exists and is undefined.  */
+/* What to do when ld finds relocations against symbols defined in
+   discarded sections.  */
 
-static void
-set_linker_sym (struct ppc_elf_link_hash_table *htab,
-               const char *sym_name,
-               bfd_vma val)
+static unsigned int
+ppc_elf_action_discarded (asection *sec)
 {
-  struct elf_link_hash_entry *h;
-  h = elf_link_hash_lookup (&htab->elf, sym_name, FALSE, FALSE, FALSE);
-  if (h != NULL && h->root.type == bfd_link_hash_undefined)
-    {
-      h->root.type = bfd_link_hash_defined;
-      h->root.u.def.section = bfd_abs_section_ptr;
-      h->root.u.def.value = val;
-      h->def_regular = 1;
-      h->type = STT_OBJECT;
-      h->other = STV_HIDDEN;
-    }
-}
+  if (strcmp (".fixup", sec->name) == 0)
+    return 0;
 
-/* Set _SDA_BASE_, _SDA2_BASE, and sbss start and end syms.  They are
-   set here rather than via PROVIDE in the default linker script,
-   because using PROVIDE inside an output section statement results in
-   unnecessary output sections.  Using PROVIDE outside an output section
-   statement runs the risk of section alignment affecting where the
-   section starts.  */
-
-bfd_boolean
-ppc_elf_set_sdata_syms (bfd *obfd, struct bfd_link_info *info)
-{
-  struct ppc_elf_link_hash_table *htab;
-  unsigned i;
-  asection *s;
-  bfd_vma val;
-
-  htab = ppc_elf_hash_table (info);
-
-  for (i = 0; i < 2; i++)
-    {
-      elf_linker_section_t *lsect = &htab->sdata[i];
-
-      s = lsect->section;
-      if (s != NULL)
-       s = s->output_section;
-      if (s == NULL)
-       s = bfd_get_section_by_name (obfd, lsect->name);
-      if (s == NULL)
-       s = bfd_get_section_by_name (obfd, lsect->bss_name);
-
-      val = 0;
-      if (s != NULL)
-       val = s->vma + 32768;
-      lsect->sym_val = val;
-
-      set_linker_sym (htab, lsect->sym_name, val);
-    }
+  if (strcmp (".got2", sec->name) == 0)
+    return 0;
 
-  s = bfd_get_section_by_name (obfd, ".sbss");
-  val = 0;
-  if (s != NULL)
-    val = s->vma;
-  set_linker_sym (htab, "__sbss_start", val);
-  set_linker_sym (htab, "___sbss_start", val);
-  if (s != NULL)
-    val += s->size;
-  set_linker_sym (htab, "__sbss_end", val);
-  set_linker_sym (htab, "___sbss_end", val);
-  return TRUE;
+  return _bfd_elf_default_action_discarded (sec);
 }
 \f
 /* Fill in the address for a pointer generated in a linker section.  */
@@ -4722,9 +5502,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
   Elf_Internal_Rela *relend;
   Elf_Internal_Rela outrel;
   bfd_byte *loc;
-  asection *sreloc = NULL;
+  asection *got2, *sreloc = NULL;
   bfd_vma *local_got_offsets;
   bfd_boolean ret = TRUE;
+  bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
 
 #ifdef DEBUG
   _bfd_error_handler ("ppc_elf_relocate_section called for %B section %A, "
@@ -4734,8 +5515,30 @@ ppc_elf_relocate_section (bfd *output_bfd,
                      (info->relocatable) ? " (relocatable)" : "");
 #endif
 
+  got2 = bfd_get_section_by_name (input_bfd, ".got2");
+
   if (info->relocatable)
-    return TRUE;
+    {
+      if (got2 == NULL)
+       return TRUE;
+
+      rel = relocs;
+      relend = relocs + input_section->reloc_count;
+      for (; rel < relend; rel++)
+       {
+         enum elf_ppc_reloc_type r_type;
+
+         r_type = ELF32_R_TYPE (rel->r_info);
+         if (r_type == R_PPC_PLTREL24
+             && rel->r_addend >= 32768)
+           {
+             /* R_PPC_PLTREL24 is rather special.  If non-zero, the
+                addend specifies the GOT pointer offset within .got2.  */
+             rel->r_addend += got2->output_offset;
+           }
+       }
+      return TRUE;
+    }
 
   /* Initialize howto table if not already done.  */
   if (!ppc_elf_howto_table[R_PPC_ADDR32])
@@ -4776,7 +5579,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
        {
          sym = local_syms + r_symndx;
          sec = local_sections[r_symndx];
-         sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym);
+         sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
 
          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
        }
@@ -4829,10 +5632,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
              && (tls_mask & TLS_TPREL) == 0)
            {
              bfd_vma insn;
-             insn = bfd_get_32 (output_bfd, contents + rel->r_offset - 2);
+             insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
              insn &= 31 << 21;
              insn |= 0x3c020000;       /* addis 0,2,0 */
-             bfd_put_32 (output_bfd, insn, contents + rel->r_offset - 2);
+             bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
              r_type = R_PPC_TPREL16_HA;
              rel->r_info = ELF32_R_INFO (r_symndx, r_type);
            }
@@ -4876,9 +5679,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
              bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
              r_type = R_PPC_TPREL16_LO;
              rel->r_info = ELF32_R_INFO (r_symndx, r_type);
+
              /* Was PPC_TLS which sits on insn boundary, now
-                PPC_TPREL16_LO which is at insn+2.  */
-             rel->r_offset += 2;
+                PPC_TPREL16_LO which is at low-order half-word.  */
+             rel->r_offset += d_offset;
            }
          break;
 
@@ -4900,7 +5704,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
              else
                {
                  bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
-                 rel->r_offset -= 2;
+                 rel->r_offset -= d_offset;
                  r_type = R_PPC_NONE;
                }
              rel->r_info = ELF32_R_INFO (r_symndx, r_type);
@@ -4949,7 +5753,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  /* OK, it checks out.  Replace the call.  */
                  offset = rel[1].r_offset;
                  insn1 = bfd_get_32 (output_bfd,
-                                     contents + rel->r_offset - 2);
+                                     contents + rel->r_offset - d_offset);
                  if ((tls_mask & tls_gd) != 0)
                    {
                      /* IE */
@@ -4957,6 +5761,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                      insn1 |= 32 << 26;        /* lwz */
                      insn2 = 0x7c631214;       /* add 3,3,2 */
                      rel[1].r_info = ELF32_R_INFO (r_symndx2, R_PPC_NONE);
+                     rel[1].r_addend = 0;
                      r_type = (((r_type - (R_PPC_GOT_TLSGD16 & 3)) & 3)
                                + R_PPC_GOT_TPREL16);
                      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
@@ -4971,15 +5776,15 @@ ppc_elf_relocate_section (bfd *output_bfd,
                          /* Was an LD reloc.  */
                          r_symndx = 0;
                          rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
-                         rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
                        }
                      r_type = R_PPC_TPREL16_HA;
                      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
                      rel[1].r_info = ELF32_R_INFO (r_symndx,
                                                    R_PPC_TPREL16_LO);
-                     rel[1].r_offset += 2;
+                     rel[1].r_offset += d_offset;
+                     rel[1].r_addend = rel->r_addend;
                    }
-                 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - 2);
+                 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - d_offset);
                  bfd_put_32 (output_bfd, insn2, contents + offset);
                  if (tls_gd == 0)
                    {
@@ -5275,7 +6080,8 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  }
              }
 
-           relocation = htab->got->output_offset + off - 4;
+           relocation = htab->got->output_offset + off;
+           relocation -= htab->elf.hgot->root.u.def.value;
 
            /* Addends on got relocations don't make much sense.
               x+off@got is actually x@got+off, and since the got is
@@ -5341,6 +6147,12 @@ ppc_elf_relocate_section (bfd *output_bfd,
          addend = 0;
          goto dodyn;
 
+       case R_PPC_REL16:
+       case R_PPC_REL16_LO:
+       case R_PPC_REL16_HI:
+       case R_PPC_REL16_HA:
+         break;
+
        case R_PPC_REL24:
        case R_PPC_REL32:
        case R_PPC_REL14:
@@ -5349,7 +6161,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
          /* If these relocations are not to a named symbol, they can be
             handled right here, no need to bother the dynamic linker.  */
          if (SYMBOL_REFERENCES_LOCAL (info, h)
-             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
+             || h == htab->elf.hgot)
            break;
          /* fall through */
 
@@ -5371,6 +6183,13 @@ ppc_elf_relocate_section (bfd *output_bfd,
             a linker script.  */
        dodyn:
          if (r_symndx == 0)
+           {
+             _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
+             break;
+           }
+         /* Fall thru.  */
+
+         if ((input_section->flags & SEC_ALLOC) == 0)
            break;
          /* Fall thru.  */
 
@@ -5382,7 +6201,6 @@ ppc_elf_relocate_section (bfd *output_bfd,
                   || !SYMBOL_CALLS_LOCAL (info, h)))
              || (ELIMINATE_COPY_RELOCS
                  && !info->shared
-                 && (input_section->flags & SEC_ALLOC) != 0
                  && h != NULL
                  && h->dynindx != -1
                  && !h->non_got_ref
@@ -5412,7 +6230,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                  if (name == NULL)
                    return FALSE;
 
-                 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
+                 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
                              && strcmp (bfd_get_section_name (input_bfd,
                                                               input_section),
                                         name + 5) == 0);
@@ -5468,10 +6286,15 @@ ppc_elf_relocate_section (bfd *output_bfd,
                             but ld.so expects buggy relocs.  */
                          osec = sec->output_section;
                          indx = elf_section_data (osec)->dynindx;
-                         BFD_ASSERT (indx > 0);
+                         if (indx == 0)
+                           {
+                             osec = htab->elf.text_index_section;
+                             indx = elf_section_data (osec)->dynindx;
+                           }
+                         BFD_ASSERT (indx != 0);
 #ifdef DEBUG
-                         if (indx <= 0)
-                           printf ("indx=%d section=%s flags=%08x name=%s\n",
+                         if (indx == 0)
+                           printf ("indx=%ld section=%s flags=%08x name=%s\n",
                                    indx, osec->name, osec->flags,
                                    h->root.root.string);
 #endif
@@ -5503,13 +6326,19 @@ ppc_elf_relocate_section (bfd *output_bfd,
 
        case R_PPC_RELAX32PC_PLT:
        case R_PPC_RELAX32_PLT:
-         BFD_ASSERT (h != NULL
-                     && h->plt.offset != (bfd_vma) -1
-                     && htab->plt != NULL);
+         {
+           struct plt_entry *ent = find_plt_ent (h, got2, addend);
 
-         relocation = (htab->plt->output_section->vma
-                       + htab->plt->output_offset
-                       + h->plt.offset);
+           if (htab->plt_type == PLT_NEW)
+             relocation = (htab->glink->output_section->vma
+                           + htab->glink->output_offset
+                           + ent->glink_offset);
+           else
+             relocation = (htab->plt->output_section->vma
+                           + htab->plt->output_offset
+                           + ent->plt.offset);
+           addend = 0;
+         }
          if (r_type == R_PPC_RELAX32_PLT)
            goto relax32;
          /* Fall thru */
@@ -5576,33 +6405,42 @@ ppc_elf_relocate_section (bfd *output_bfd,
        case R_PPC_PLTREL24:
          /* Relocation is to the entry for this symbol in the
             procedure linkage table.  */
-         BFD_ASSERT (h != NULL);
+         {
+           struct plt_entry *ent = find_plt_ent (h, got2, addend);
 
-         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.  */
-             break;
-           }
+           addend = 0;
+           if (ent == NULL
+               || htab->plt == NULL)
+             {
+               /* We didn't make a PLT entry for this symbol.  This
+                  happens when statically linking PIC code, or when
+                  using -Bsymbolic.  */
+               break;
+             }
 
-         unresolved_reloc = FALSE;
-         relocation = (htab->plt->output_section->vma
-                       + htab->plt->output_offset
-                       + h->plt.offset);
+           unresolved_reloc = FALSE;
+           if (htab->plt_type == PLT_NEW)
+             relocation = (htab->glink->output_section->vma
+                           + htab->glink->output_offset
+                           + ent->glink_offset);
+           else
+             relocation = (htab->plt->output_section->vma
+                           + htab->plt->output_offset
+                           + ent->plt.offset);
+         }
          break;
 
          /* Relocate against _SDA_BASE_.  */
        case R_PPC_SDAREL16:
          {
            const char *name;
+           struct elf_link_hash_entry *sh;
 
            BFD_ASSERT (sec != NULL);
            name = bfd_get_section_name (abfd, sec->output_section);
-           if (! ((strncmp (name, ".sdata", 6) == 0
+           if (! ((CONST_STRNEQ (name, ".sdata")
                    && (name[6] == 0 || name[6] == '.'))
-                  || (strncmp (name, ".sbss", 5) == 0
+                  || (CONST_STRNEQ (name, ".sbss")
                       && (name[5] == 0 || name[5] == '.'))))
              {
                (*_bfd_error_handler)
@@ -5613,7 +6451,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
                   howto->name,
                   name);
              }
-           addend -= htab->sdata[0].sym_val;
+           sh = htab->sdata[0].sym;
+           addend -= (sh->root.u.def.value
+                      + sh->root.u.def.section->output_offset
+                      + sh->root.u.def.section->output_section->vma);
          }
          break;
 
@@ -5621,11 +6462,12 @@ ppc_elf_relocate_section (bfd *output_bfd,
        case R_PPC_EMB_SDA2REL:
          {
            const char *name;
+           struct elf_link_hash_entry *sh;
 
            BFD_ASSERT (sec != NULL);
            name = bfd_get_section_name (abfd, sec->output_section);
-           if (! (strncmp (name, ".sdata2", 7) == 0
-                  || strncmp (name, ".sbss2", 6) == 0))
+           if (! (CONST_STRNEQ (name, ".sdata2")
+                  || CONST_STRNEQ (name, ".sbss2")))
              {
                (*_bfd_error_handler)
                  (_("%B: the target (%s) of a %s relocation is "
@@ -5639,7 +6481,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
                ret = FALSE;
                continue;
              }
-           addend -= htab->sdata[1].sym_val;
+           sh = htab->sdata[1].sym;
+           addend -= (sh->root.u.def.value
+                      + sh->root.u.def.section->output_offset
+                      + sh->root.u.def.section->output_section->vma);
          }
          break;
 
@@ -5649,23 +6494,30 @@ ppc_elf_relocate_section (bfd *output_bfd,
          {
            const char *name;
            int reg;
+           struct elf_link_hash_entry *sh;
 
            BFD_ASSERT (sec != NULL);
            name = bfd_get_section_name (abfd, sec->output_section);
-           if (((strncmp (name, ".sdata", 6) == 0
+           if (((CONST_STRNEQ (name, ".sdata")
                  && (name[6] == 0 || name[6] == '.'))
-                || (strncmp (name, ".sbss", 5) == 0
+                || (CONST_STRNEQ (name, ".sbss")
                     && (name[5] == 0 || name[5] == '.'))))
              {
                reg = 13;
-               addend -= htab->sdata[0].sym_val;
+               sh = htab->sdata[0].sym;
+               addend -= (sh->root.u.def.value
+                          + sh->root.u.def.section->output_offset
+                          + sh->root.u.def.section->output_section->vma);
              }
 
-           else if (strncmp (name, ".sdata2", 7) == 0
-                    || strncmp (name, ".sbss2", 6) == 0)
+           else if (CONST_STRNEQ (name, ".sdata2")
+                    || CONST_STRNEQ (name, ".sbss2"))
              {
                reg = 2;
-               addend -= htab->sdata[1].sym_val;
+               sh = htab->sdata[1].sym;
+               addend -= (sh->root.u.def.value
+                          + sh->root.u.def.section->output_offset
+                          + sh->root.u.def.section->output_section->vma);
              }
 
            else if (strcmp (name, ".PPC.EMB.sdata0") == 0
@@ -5749,25 +6601,29 @@ ppc_elf_relocate_section (bfd *output_bfd,
          break;
 
        case R_PPC_ADDR16_HA:
-       case R_PPC_GOT16_HA:
-       case R_PPC_PLT16_HA:
+       case R_PPC_REL16_HA:
        case R_PPC_SECTOFF_HA:
        case R_PPC_TPREL16_HA:
        case R_PPC_DTPREL16_HA:
-       case R_PPC_GOT_TLSGD16_HA:
-       case R_PPC_GOT_TLSLD16_HA:
-       case R_PPC_GOT_TPREL16_HA:
-       case R_PPC_GOT_DTPREL16_HA:
        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,
             '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.
-              Bits 0:15 are not used.  */
-           addend += 0x8000;
+         if (sec == NULL)
+           break;
+         /* Fall thru */
+
+       case R_PPC_PLT16_HA:
+       case R_PPC_GOT16_HA:
+       case R_PPC_GOT_TLSGD16_HA:
+       case R_PPC_GOT_TLSLD16_HA:
+       case R_PPC_GOT_TPREL16_HA:
+       case R_PPC_GOT_DTPREL16_HA:
+         /* Add 0x10000 if sign bit in 0:15 is set.
+            Bits 0:15 are not used.  */
+         addend += 0x8000;
          break;
        }
 
@@ -5806,8 +6662,6 @@ ppc_elf_relocate_section (bfd *output_bfd,
 
       if (r != bfd_reloc_ok)
        {
-         if (sym_name == NULL)
-           sym_name = "(null)";
          if (r == bfd_reloc_overflow)
            {
              if (warned)
@@ -5853,6 +6707,10 @@ ppc_elf_relocate_section (bfd *output_bfd,
   return ret;
 }
 \f
+#define PPC_LO(v) ((v) & 0xffff)
+#define PPC_HI(v) (((v) >> 16) & 0xffff)
+#define PPC_HA(v) PPC_HI ((v) + 0x8000)
+
 /* Finish up dynamic symbol handling.  We set the contents of various
    dynamic sections here.  */
 
@@ -5863,6 +6721,8 @@ ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
                               Elf_Internal_Sym *sym)
 {
   struct ppc_elf_link_hash_table *htab;
+  struct plt_entry *ent;
+  bfd_boolean doneone;
 
 #ifdef DEBUG
   fprintf (stderr, "ppc_elf_finish_dynamic_symbol called for %s",
@@ -5872,52 +6732,267 @@ ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
   htab = ppc_elf_hash_table (info);
   BFD_ASSERT (htab->elf.dynobj != NULL);
 
-  if (h->plt.offset != (bfd_vma) -1)
-    {
-      Elf_Internal_Rela rela;
-      bfd_byte *loc;
-      bfd_vma reloc_index;
+  doneone = FALSE;
+  for (ent = h->plt.plist; ent != NULL; ent = ent->next)
+    if (ent->plt.offset != (bfd_vma) -1)
+      {
+       if (!doneone)
+         {
+           Elf_Internal_Rela rela;
+           bfd_byte *loc;
+           bfd_vma reloc_index;
 
-#ifdef DEBUG
-      fprintf (stderr, ", plt_offset = %d", h->plt.offset);
-#endif
+           if (htab->plt_type == PLT_NEW)
+             reloc_index = ent->plt.offset / 4;
+           else
+             {
+               reloc_index = ((ent->plt.offset - htab->plt_initial_entry_size)
+                              / htab->plt_slot_size);
+               if (reloc_index > PLT_NUM_SINGLE_ENTRIES
+                   && htab->plt_type == PLT_OLD)
+                 reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
+             }
 
-      /* This symbol has an entry in the procedure linkage table.  Set
-        it up.  */
+           /* This symbol has an entry in the procedure linkage table.
+              Set it up.  */
+           if (htab->plt_type == PLT_VXWORKS)
+             {
+               bfd_vma got_offset;
+               const bfd_vma *plt_entry;
+               
+               /* The first three entries in .got.plt are reserved.  */
+               got_offset = (reloc_index + 3) * 4;
 
-      BFD_ASSERT (h->dynindx != -1);
-      BFD_ASSERT (htab->plt != NULL && htab->relplt != NULL);
+               /* Use the right PLT. */
+               plt_entry = info->shared ? ppc_elf_vxworks_pic_plt_entry
+                           : ppc_elf_vxworks_plt_entry;
 
-      /* We don't need to fill in the .plt.  The ppc dynamic linker
-        will fill it in.  */
+               /* Fill in the .plt on VxWorks.  */
+               if (info->shared)
+                 {
+                   bfd_vma got_offset_hi = (got_offset >> 16)
+                                           + ((got_offset & 0x8000) >> 15);
+
+                   bfd_put_32 (output_bfd,
+                               plt_entry[0] | (got_offset_hi & 0xffff),
+                               htab->plt->contents + ent->plt.offset + 0);
+                   bfd_put_32 (output_bfd,
+                               plt_entry[1] | (got_offset & 0xffff),
+                               htab->plt->contents + ent->plt.offset + 4);
+                 }
+               else
+                 {
+                   bfd_vma got_loc
+                     = (got_offset
+                        + htab->elf.hgot->root.u.def.value
+                        + htab->elf.hgot->root.u.def.section->output_offset
+                        + htab->elf.hgot->root.u.def.section->output_section->vma);
+                   bfd_vma got_loc_hi = (got_loc >> 16)
+                                        + ((got_loc & 0x8000) >> 15);
+
+                   bfd_put_32 (output_bfd,
+                               plt_entry[0] | (got_loc_hi & 0xffff),
+                               htab->plt->contents + ent->plt.offset + 0);
+                   bfd_put_32 (output_bfd,
+                               plt_entry[1] | (got_loc & 0xffff),
+                               htab->plt->contents + ent->plt.offset + 4);
+                 }
 
-      /* Fill in the entry in the .rela.plt section.  */
-      rela.r_offset = (htab->plt->output_section->vma
-                      + htab->plt->output_offset
-                      + h->plt.offset);
-      rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
-      rela.r_addend = 0;
+               bfd_put_32 (output_bfd, plt_entry[2],
+                           htab->plt->contents + ent->plt.offset + 8);
+               bfd_put_32 (output_bfd, plt_entry[3],
+                           htab->plt->contents + ent->plt.offset + 12);
+
+               /* This instruction is an immediate load.  The value loaded is
+                  the byte offset of the R_PPC_JMP_SLOT relocation from the
+                  start of the .rela.plt section.  The value is stored in the
+                  low-order 16 bits of the load instruction.  */
+               /* NOTE: It appears that this is now an index rather than a
+                  prescaled offset.  */
+               bfd_put_32 (output_bfd, 
+                           plt_entry[4] | reloc_index,
+                           htab->plt->contents + ent->plt.offset + 16);
+               /* This instruction is a PC-relative branch whose target is
+                  the start of the PLT section.  The address of this branch
+                  instruction is 20 bytes beyond the start of this PLT entry.
+                  The address is encoded in bits 6-29, inclusive.  The value
+                  stored is right-shifted by two bits, permitting a 26-bit
+                  offset.  */
+               bfd_put_32 (output_bfd, 
+                           (plt_entry[5] 
+                            | (-(ent->plt.offset + 20) & 0x03fffffc)),
+                           htab->plt->contents + ent->plt.offset + 20);
+               bfd_put_32 (output_bfd, plt_entry[6],
+                           htab->plt->contents + ent->plt.offset + 24);
+               bfd_put_32 (output_bfd, plt_entry[7],
+                           htab->plt->contents + ent->plt.offset + 28);
+
+               /* Fill in the GOT entry corresponding to this PLT slot with
+                  the address immediately after the the "bctr" instruction
+                  in this PLT entry.  */
+               bfd_put_32 (output_bfd, (htab->plt->output_section->vma
+                                        + htab->plt->output_offset
+                                        + ent->plt.offset + 16),
+                           htab->sgotplt->contents + got_offset);
+
+               if (!info->shared)
+                 {
+                   /* Fill in a couple of entries in .rela.plt.unloaded.  */
+                   loc = htab->srelplt2->contents
+                     + ((VXWORKS_PLTRESOLVE_RELOCS + reloc_index
+                         * VXWORKS_PLT_NON_JMP_SLOT_RELOCS)
+                        * sizeof (Elf32_External_Rela));
+
+                   /* Provide the @ha relocation for the first instruction.  */
+                   rela.r_offset = (htab->plt->output_section->vma
+                                    + htab->plt->output_offset
+                                    + ent->plt.offset + 2);
+                   rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
+                                               R_PPC_ADDR16_HA);
+                   rela.r_addend = got_offset;
+                   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
+                   loc += sizeof (Elf32_External_Rela);
+
+                   /* Provide the @l relocation for the second instruction.  */
+                   rela.r_offset = (htab->plt->output_section->vma
+                                    + htab->plt->output_offset
+                                    + ent->plt.offset + 6);
+                   rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx,
+                                               R_PPC_ADDR16_LO);
+                   rela.r_addend = got_offset;
+                   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
+                   loc += sizeof (Elf32_External_Rela);
+
+                   /* Provide a relocation for the GOT entry corresponding to this
+                      PLT slot.  Point it at the middle of the .plt entry.  */
+                   rela.r_offset = (htab->sgotplt->output_section->vma
+                                    + htab->sgotplt->output_offset
+                                    + got_offset);
+                   rela.r_info = ELF32_R_INFO (htab->elf.hplt->indx,
+                                               R_PPC_ADDR32);
+                   rela.r_addend = ent->plt.offset + 16;
+                   bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
+                 }
 
-      reloc_index = (h->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_SLOT_SIZE;
-      if (reloc_index > PLT_NUM_SINGLE_ENTRIES)
-       reloc_index -= (reloc_index - PLT_NUM_SINGLE_ENTRIES) / 2;
-      loc = (htab->relplt->contents
-            + reloc_index * sizeof (Elf32_External_Rela));
-      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
+               /* VxWorks uses non-standard semantics for R_PPC_JMP_SLOT.
+                  In particular, the offset for the relocation is not the
+                  address of the PLT entry for this function, as specified
+                  by the ABI.  Instead, the offset is set to the address of
+                  the GOT slot for this function.  See EABI 4.4.4.1.  */
+               rela.r_offset = (htab->sgotplt->output_section->vma
+                                + htab->sgotplt->output_offset
+                                + got_offset);
 
-      if (!h->def_regular)
-       {
-         /* Mark the symbol as undefined, rather than as defined in
-            the .plt section.  Leave the value alone.  */
-         sym->st_shndx = SHN_UNDEF;
-         /* If the symbol is weak, we do need to clear the value.
-            Otherwise, the PLT entry would provide a definition for
-            the symbol even if the symbol wasn't defined anywhere,
-            and so the symbol would never be NULL.  */
-         if (!h->ref_regular_nonweak)
-           sym->st_value = 0;
-       }
-    }
+             }
+           else
+             {
+               rela.r_offset = (htab->plt->output_section->vma
+                                + htab->plt->output_offset
+                                + ent->plt.offset);
+               if (htab->plt_type == PLT_OLD)
+                 {
+                   /* We don't need to fill in the .plt.  The ppc dynamic
+                      linker will fill it in.  */
+                 }
+               else
+                 {
+                   bfd_vma val = (htab->glink_pltresolve + ent->plt.offset
+                                  + htab->glink->output_section->vma
+                                  + htab->glink->output_offset);
+                   bfd_put_32 (output_bfd, val,
+                               htab->plt->contents + ent->plt.offset);
+                 }
+             }
+
+           /* Fill in the entry in the .rela.plt section.  */
+           rela.r_info = ELF32_R_INFO (h->dynindx, R_PPC_JMP_SLOT);
+           rela.r_addend = 0;
+
+           loc = (htab->relplt->contents
+                  + reloc_index * sizeof (Elf32_External_Rela));
+           bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
+
+           if (!h->def_regular)
+             {
+               /* Mark the symbol as undefined, rather than as defined in
+                  the .plt section.  Leave the value alone.  */
+               sym->st_shndx = SHN_UNDEF;
+               /* If the symbol is weak, we do need to clear the value.
+                  Otherwise, the PLT entry would provide a definition for
+                  the symbol even if the symbol wasn't defined anywhere,
+                  and so the symbol would never be NULL.  */
+               if (!h->ref_regular_nonweak)
+                 sym->st_value = 0;
+             }
+           doneone = TRUE;
+         }
+
+       if (htab->plt_type == PLT_NEW)
+         {
+           bfd_vma plt;
+           unsigned char *p;
+
+           plt = (ent->plt.offset
+                  + htab->plt->output_section->vma
+                  + htab->plt->output_offset);
+           p = (unsigned char *) htab->glink->contents + ent->glink_offset;
+
+           if (info->shared || info->pie)
+             {
+               bfd_vma got = 0;
+
+               if (ent->addend >= 32768)
+                 got = (ent->addend
+                        + ent->sec->output_section->vma
+                        + ent->sec->output_offset);
+               else if (htab->elf.hgot != NULL)
+                 got = (htab->elf.hgot->root.u.def.value
+                        + htab->elf.hgot->root.u.def.section->output_section->vma
+                        + htab->elf.hgot->root.u.def.section->output_offset);
+
+               plt -= got;
+
+               if (plt + 0x8000 < 0x10000)
+                 {
+                   bfd_put_32 (output_bfd, LWZ_11_30 + PPC_LO (plt), p);
+                   p += 4;
+                   bfd_put_32 (output_bfd, MTCTR_11, p);
+                   p += 4;
+                   bfd_put_32 (output_bfd, BCTR, p);
+                   p += 4;
+                   bfd_put_32 (output_bfd, NOP, p);
+                   p += 4;
+                 }
+               else
+                 {
+                   bfd_put_32 (output_bfd, ADDIS_11_30 + PPC_HA (plt), p);
+                   p += 4;
+                   bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
+                   p += 4;
+                   bfd_put_32 (output_bfd, MTCTR_11, p);
+                   p += 4;
+                   bfd_put_32 (output_bfd, BCTR, p);
+                   p += 4;
+                 }
+             }
+           else
+             {
+               bfd_put_32 (output_bfd, LIS_11 + PPC_HA (plt), p);
+               p += 4;
+               bfd_put_32 (output_bfd, LWZ_11_11 + PPC_LO (plt), p);
+               p += 4;
+               bfd_put_32 (output_bfd, MTCTR_11, p);
+               p += 4;
+               bfd_put_32 (output_bfd, BCTR, p);
+               p += 4;
+
+               /* We only need one non-PIC glink stub.  */
+               break;
+             }
+         }
+       else
+         break;
+      }
 
   if (h->needs_copy)
     {
@@ -5933,7 +7008,7 @@ ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
 
       BFD_ASSERT (h->dynindx != -1);
 
-      if (h->size <= elf_gp_size (htab->elf.dynobj))
+      if (ppc_elf_hash_entry (h)->has_sda_refs)
        s = htab->relsbss;
       else
        s = htab->relbss;
@@ -5954,8 +7029,10 @@ ppc_elf_finish_dynamic_symbol (bfd *output_bfd,
 
   /* Mark some specially defined symbols as absolute.  */
   if (strcmp (h->root.root.string, "_DYNAMIC") == 0
-      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
-      || strcmp (h->root.root.string, "_PROCEDURE_LINKAGE_TABLE_") == 0)
+      || (!htab->is_vxworks
+         && (h == htab->elf.hgot
+             || strcmp (h->root.root.string,
+                        "_PROCEDURE_LINKAGE_TABLE_") == 0)))
     sym->st_shndx = SHN_ABS;
 
   return TRUE;
@@ -5986,14 +7063,28 @@ 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;
 
 #ifdef DEBUG
   fprintf (stderr, "ppc_elf_finish_dynamic_sections called\n");
 #endif
 
   htab = ppc_elf_hash_table (info);
-  sdyn = bfd_get_section_by_name (htab->elf.dynobj, ".dynamic");
+  dynobj = elf_hash_table (info)->dynobj;
+  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
+  if (htab->is_vxworks)
+    splt = bfd_get_section_by_name (dynobj, ".plt");  
+  else
+    splt = NULL;
+
+  got = 0;
+  if (htab->elf.hgot != NULL)
+    got = (htab->elf.hgot->root.u.def.value
+          + htab->elf.hgot->root.u.def.section->output_section->vma
+          + htab->elf.hgot->root.u.def.section->output_offset);
 
   if (htab->elf.dynamic_sections_created)
     {
@@ -6008,12 +7099,15 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
          Elf_Internal_Dyn dyn;
          asection *s;
 
-         bfd_elf32_swap_dyn_in (htab->elf.dynobj, dyncon, &dyn);
+         bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
 
          switch (dyn.d_tag)
            {
            case DT_PLTGOT:
-             s = htab->plt;
+             if (htab->is_vxworks)
+               s = htab->sgotplt;
+             else
+               s = htab->plt;
              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
              break;
 
@@ -6026,6 +7120,19 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
              break;
 
+           case DT_PPC_GOT:
+             dyn.d_un.d_ptr = got;
+             break;
+
+           case DT_RELASZ:
+             if (htab->is_vxworks)
+               {
+                 if (htab->relplt)
+                   dyn.d_un.d_ptr -= htab->relplt->size;
+                 break;
+               }
+             continue;
+
            default:
              continue;
            }
@@ -6036,21 +7143,284 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 
   /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can
      easily find the address of the _GLOBAL_OFFSET_TABLE_.  */
-  if (htab->got)
+  if (htab->got != NULL)
     {
-      unsigned char *contents = htab->got->contents;
-      bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents);
+      unsigned char *p = htab->got->contents;
+      bfd_vma val;
 
-      if (sdyn == NULL)
-       bfd_put_32 (output_bfd, 0, contents + 4);
-      else
-       bfd_put_32 (output_bfd,
-                   sdyn->output_section->vma + sdyn->output_offset,
-                   contents + 4);
+      p += htab->elf.hgot->root.u.def.value;
+      if (htab->plt_type == PLT_OLD)
+       bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, p - 4);
+
+      val = 0;
+      if (sdyn != NULL)
+       val = sdyn->output_section->vma + sdyn->output_offset;
+      bfd_put_32 (output_bfd, val, p);
 
       elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 4;
     }
 
+  /* Fill in the first entry in the VxWorks procedure linkage table.  */
+  if (splt && splt->size > 0)
+    {
+      /* Use the right PLT. */
+      static const bfd_vma *plt_entry = NULL;
+      plt_entry = info->shared ? 
+       ppc_elf_vxworks_pic_plt0_entry : ppc_elf_vxworks_plt0_entry;
+
+      if (!info->shared)
+       {
+         bfd_vma got_value =
+           (htab->elf.hgot->root.u.def.section->output_section->vma
+            + htab->elf.hgot->root.u.def.section->output_offset
+            + htab->elf.hgot->root.u.def.value);
+         bfd_vma got_hi = (got_value >> 16) + ((got_value & 0x8000) >> 15);
+
+         bfd_put_32 (output_bfd, plt_entry[0] | (got_hi & 0xffff),
+                     splt->contents +  0);
+         bfd_put_32 (output_bfd, plt_entry[1] | (got_value & 0xffff),
+                     splt->contents +  4);
+       }
+      else
+       {
+         bfd_put_32 (output_bfd, plt_entry[0], splt->contents +  0);
+         bfd_put_32 (output_bfd, plt_entry[1], splt->contents +  4);
+       }
+      bfd_put_32 (output_bfd, plt_entry[2], splt->contents +  8);
+      bfd_put_32 (output_bfd, plt_entry[3], splt->contents + 12);
+      bfd_put_32 (output_bfd, plt_entry[4], splt->contents + 16);
+      bfd_put_32 (output_bfd, plt_entry[5], splt->contents + 20);
+      bfd_put_32 (output_bfd, plt_entry[6], splt->contents + 24);
+      bfd_put_32 (output_bfd, plt_entry[7], splt->contents + 28);
+
+      if (! info->shared)
+       {
+         Elf_Internal_Rela rela;
+         bfd_byte *loc;
+
+         loc = htab->srelplt2->contents;
+
+         /* Output the @ha relocation for the first instruction.  */
+         rela.r_offset = (htab->plt->output_section->vma
+                          + htab->plt->output_offset
+                          + 2);
+         rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
+         rela.r_addend = 0;
+         bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
+         loc += sizeof (Elf32_External_Rela);
+         
+         /* Output the @l relocation for the second instruction.  */
+         rela.r_offset = (htab->plt->output_section->vma
+                          + htab->plt->output_offset
+                          + 6);
+         rela.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
+         rela.r_addend = 0;
+         bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
+         loc += sizeof (Elf32_External_Rela);
+
+         /* Fix up the remaining relocations.  They may have the wrong
+            symbol index for _G_O_T_ or _P_L_T_ depending on the order
+            in which symbols were output.  */
+         while (loc < htab->srelplt2->contents + htab->srelplt2->size)
+           {
+             Elf_Internal_Rela rel;
+
+             bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
+             rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_HA);
+             bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
+             loc += sizeof (Elf32_External_Rela);
+
+             bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
+             rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_PPC_ADDR16_LO);
+             bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
+             loc += sizeof (Elf32_External_Rela);
+
+             bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
+             rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_PPC_ADDR32);
+             bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
+             loc += sizeof (Elf32_External_Rela);
+           }
+       }
+    }
+
+  if (htab->glink != NULL && htab->glink->contents != NULL)
+    {
+      unsigned char *p;
+      unsigned char *endp;
+      bfd_vma res0;
+      unsigned int i;
+
+      /*
+       * PIC glink code is the following:
+       *
+       * # ith PLT code stub.
+       *   addis 11,30,(plt+(i-1)*4-got)@ha
+       *   lwz 11,(plt+(i-1)*4-got)@l(11)
+       *   mtctr 11
+       *   bctr
+       *
+       * # A table of branches, one for each plt entry.
+       * # The idea is that the plt call stub loads ctr (and r11) with these
+       * # addresses, so (r11 - res_0) gives the plt index * 4.
+       * res_0:        b PLTresolve
+       * res_1:        b PLTresolve
+       * .
+       * # Some number of entries towards the end can be nops
+       * res_n_m3: nop
+       * res_n_m2: nop
+       * res_n_m1:
+       *
+       * PLTresolve:
+       *    addis 11,11,(1f-res_0)@ha
+       *    mflr 0
+       *    bcl 20,31,1f
+       * 1: addi 11,11,(1b-res_0)@l
+       *    mflr 12
+       *    mtlr 0
+       *    sub 11,11,12                # r11 = index * 4
+       *    addis 12,12,(got+4-1b)@ha
+       *    lwz 0,(got+4-1b)@l(12)      # got[1] address of dl_runtime_resolve
+       *    lwz 12,(got+8-1b)@l(12)     # got[2] contains the map address
+       *    mtctr 0
+       *    add 0,11,11
+       *    add 11,0,11                 # r11 = index * 12 = reloc offset.
+       *    bctr
+       */
+      static const unsigned int pic_plt_resolve[] =
+       {
+         ADDIS_11_11,
+         MFLR_0,
+         BCL_20_31,
+         ADDI_11_11,
+         MFLR_12,
+         MTLR_0,
+         SUB_11_11_12,
+         ADDIS_12_12,
+         LWZ_0_12,
+         LWZ_12_12,
+         MTCTR_0,
+         ADD_0_11_11,
+         ADD_11_0_11,
+         BCTR,
+         NOP,
+         NOP
+       };
+
+      static const unsigned int plt_resolve[] =
+       {
+         LIS_12,
+         ADDIS_11_11,
+         LWZ_0_12,
+         ADDI_11_11,
+         MTCTR_0,
+         ADD_0_11_11,
+         LWZ_12_12,
+         ADD_11_0_11,
+         BCTR,
+         NOP,
+         NOP,
+         NOP,
+         NOP,
+         NOP,
+         NOP,
+         NOP
+       };
+
+      if (ARRAY_SIZE (pic_plt_resolve) != GLINK_PLTRESOLVE / 4)
+       abort ();
+      if (ARRAY_SIZE (plt_resolve) != GLINK_PLTRESOLVE / 4)
+       abort ();
+
+      /* Build the branch table, one for each plt entry (less one),
+        and perhaps some padding.  */
+      p = htab->glink->contents;
+      p += htab->glink_pltresolve;
+      endp = htab->glink->contents;
+      endp += htab->glink->size - GLINK_PLTRESOLVE;
+      while (p < endp - 8 * 4)
+       {
+         bfd_put_32 (output_bfd, B + endp - p, p);
+         p += 4;
+       }
+      while (p < endp)
+       {
+         bfd_put_32 (output_bfd, NOP, p);
+         p += 4;
+       }
+
+      res0 = (htab->glink_pltresolve
+             + htab->glink->output_section->vma
+             + htab->glink->output_offset);
+
+      /* Last comes the PLTresolve stub.  */
+      if (info->shared || info->pie)
+       {
+         bfd_vma bcl;
+
+         for (i = 0; i < ARRAY_SIZE (pic_plt_resolve); i++)
+           {
+             bfd_put_32 (output_bfd, pic_plt_resolve[i], p);
+             p += 4;
+           }
+         p -= 4 * ARRAY_SIZE (pic_plt_resolve);
+
+         bcl = (htab->glink->size - GLINK_PLTRESOLVE + 3*4
+                + htab->glink->output_section->vma
+                + htab->glink->output_offset);
+
+         bfd_put_32 (output_bfd,
+                     ADDIS_11_11 + PPC_HA (bcl - res0), p + 0*4);
+         bfd_put_32 (output_bfd,
+                     ADDI_11_11 + PPC_LO (bcl - res0), p + 3*4);
+         bfd_put_32 (output_bfd,
+                     ADDIS_12_12 + PPC_HA (got + 4 - bcl), p + 7*4);
+         if (PPC_HA (got + 4 - bcl) == PPC_HA (got + 8 - bcl))
+           {
+             bfd_put_32 (output_bfd,
+                         LWZ_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
+             bfd_put_32 (output_bfd,
+                         LWZ_12_12 + PPC_LO (got + 8 - bcl), p + 9*4);
+           }
+         else
+           {
+             bfd_put_32 (output_bfd,
+                         LWZU_0_12 + PPC_LO (got + 4 - bcl), p + 8*4);
+             bfd_put_32 (output_bfd,
+                         LWZ_12_12 + 4, p + 9*4);
+           }
+       }
+      else
+       {
+         for (i = 0; i < ARRAY_SIZE (plt_resolve); i++)
+           {
+             bfd_put_32 (output_bfd, plt_resolve[i], p);
+             p += 4;
+           }
+         p -= 4 * ARRAY_SIZE (plt_resolve);
+
+         bfd_put_32 (output_bfd,
+                     LIS_12 + PPC_HA (got + 4), p + 0*4);
+         bfd_put_32 (output_bfd,
+                     ADDIS_11_11 + PPC_HA (-res0), p + 1*4);
+         bfd_put_32 (output_bfd,
+                     ADDI_11_11 + PPC_LO (-res0), p + 3*4);
+         if (PPC_HA (got + 4) == PPC_HA (got + 8))
+           {
+             bfd_put_32 (output_bfd,
+                         LWZ_0_12 + PPC_LO (got + 4), p + 2*4);
+             bfd_put_32 (output_bfd,
+                         LWZ_12_12 + PPC_LO (got + 8), p + 6*4);
+           }
+         else
+           {
+             bfd_put_32 (output_bfd,
+                         LWZU_0_12 + PPC_LO (got + 4), p + 2*4);
+             bfd_put_32 (output_bfd,
+                         LWZ_12_12 + 4, p + 6*4);
+           }
+       }
+    }
+
   return TRUE;
 }
 \f
@@ -6066,6 +7436,7 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 #define ELF_MAXPAGESIZE                0x10000
 #endif
 #define ELF_MINPAGESIZE                0x1000
+#define ELF_COMMONPAGESIZE     0x1000
 #define elf_info_to_howto      ppc_elf_info_to_howto
 
 #ifdef  EM_CYGNUS_POWERPC
@@ -6077,10 +7448,8 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 #endif
 
 #define elf_backend_plt_not_loaded     1
-#define elf_backend_got_symbol_offset  4
 #define elf_backend_can_gc_sections    1
 #define elf_backend_can_refcount       1
-#define elf_backend_got_header_size    12
 #define elf_backend_rela_normal                1
 
 #define bfd_elf32_mkobject                     ppc_elf_mkobject
@@ -6111,7 +7480,116 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
 #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
+#define elf_backend_get_sec_type_attr          ppc_elf_get_sec_type_attr
 #define elf_backend_plt_sym_val                        ppc_elf_plt_sym_val
+#define elf_backend_action_discarded           ppc_elf_action_discarded
+#define elf_backend_init_index_section         _bfd_elf_init_1_index_section
+
+#include "elf32-target.h"
+
+/* VxWorks Target */
+
+#undef TARGET_LITTLE_SYM
+#undef TARGET_LITTLE_NAME
+
+#undef TARGET_BIG_SYM
+#define TARGET_BIG_SYM         bfd_elf32_powerpc_vxworks_vec
+#undef TARGET_BIG_NAME
+#define TARGET_BIG_NAME                "elf32-powerpc-vxworks"
+
+/* 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)
+{
+  if (sec->name == NULL)
+    return NULL;
+
+  if (strcmp (sec->name, ".plt") == 0)
+    return _bfd_elf_get_sec_type_attr (abfd, sec);
+
+  return ppc_elf_get_sec_type_attr (abfd, sec);
+}
+
+/* Like ppc_elf_link_hash_table_create, but overrides
+   appropriately for VxWorks.  */
+static struct bfd_link_hash_table *
+ppc_elf_vxworks_link_hash_table_create (bfd *abfd)
+{
+  struct bfd_link_hash_table *ret;
+
+  ret = ppc_elf_link_hash_table_create (abfd);
+  if (ret)
+    {
+      struct ppc_elf_link_hash_table *htab
+        = (struct ppc_elf_link_hash_table *)ret;
+      htab->is_vxworks = 1;
+      htab->plt_type = PLT_VXWORKS;
+      htab->plt_entry_size = VXWORKS_PLT_ENTRY_SIZE;
+      htab->plt_slot_size = VXWORKS_PLT_ENTRY_SIZE;
+      htab->plt_initial_entry_size = VXWORKS_PLT_INITIAL_ENTRY_SIZE;
+    }
+  return ret;
+}
+
+/* Tweak magic VxWorks symbols as they are loaded.  */
+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,
+                                asection **secp,
+                                bfd_vma *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);
+}
+
+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);
+}
+
+/* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so
+   define it.  */
+#undef elf_backend_want_plt_sym
+#define elf_backend_want_plt_sym               1
+#undef elf_backend_want_got_plt
+#define elf_backend_want_got_plt               1
+#undef elf_backend_got_symbol_offset
+#define elf_backend_got_symbol_offset          0
+#undef elf_backend_plt_not_loaded
+#define elf_backend_plt_not_loaded             0
+#undef elf_backend_plt_readonly
+#define elf_backend_plt_readonly               1
+#undef elf_backend_got_header_size
+#define elf_backend_got_header_size            12
+
+#undef bfd_elf32_bfd_link_hash_table_create
+#define bfd_elf32_bfd_link_hash_table_create \
+  ppc_elf_vxworks_link_hash_table_create
+#undef elf_backend_add_symbol_hook
+#define elf_backend_add_symbol_hook \
+  ppc_elf_vxworks_add_symbol_hook
+#undef elf_backend_link_output_symbol_hook
+#define elf_backend_link_output_symbol_hook \
+  elf_vxworks_link_output_symbol_hook
+#undef elf_backend_final_write_processing
+#define elf_backend_final_write_processing \
+  ppc_elf_vxworks_final_write_processing
+#undef elf_backend_get_sec_type_attr
+#define elf_backend_get_sec_type_attr \
+  ppc_elf_vxworks_get_sec_type_attr
+#undef elf_backend_emit_relocs
+#define elf_backend_emit_relocs \
+  elf_vxworks_emit_relocs
+
+#undef elf32_bed
+#define elf32_bed                              ppc_elf_vxworks_bed
 
 #include "elf32-target.h"
This page took 0.065744 seconds and 4 git commands to generate.