2000-12-14 Kazu Hirata <kazu@hxi.com>
[deliverable/binutils-gdb.git] / bfd / elf32-hppa.c
index b645b5fdb2de3027d74f23f07d7189e275bdc595..3d0c9eec9ce3e7b4b26ecca1f13e368f05255a06 100644 (file)
@@ -35,6 +35,16 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #include "elf-hppa.h"
 #include "elf32-hppa.h"
 
+/* In order to gain some understanding of code in this file without
+   knowing all the intricate details of the linker, note the
+   following:
+
+   Functions named elf32_hppa_* are called by external routines, other
+   functions are only called locally.  elf32_hppa_* functions appear
+   in this file more or less in the order in which they are called
+   from external routines.  eg. elf32_hppa_check_relocs is called
+   early in the link process, elf32_hppa_finish_dynamic_sections is
+   one of the last functions.  */
 
 /* We use two hash tables to hold information for linking PA ELF objects.
 
@@ -101,13 +111,25 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
    :           ldw -24(%sp),%rp                ; restore the original rp
    :           ldsid (%rp),%r1
    :           mtsp %r1,%sr0
-   :           be,n 0(%sr0,%rp)                ; inter-space return
-*/
+   :           be,n 0(%sr0,%rp)                ; inter-space return  */
 
 #define PLT_ENTRY_SIZE 8
+#define PLABEL_PLT_ENTRY_SIZE PLT_ENTRY_SIZE
 #define GOT_ENTRY_SIZE 4
 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
 
+static const bfd_byte plt_stub[] =
+{
+  0x0e, 0x80, 0x10, 0x96,  /* 1: ldw   0(%r20),%r22            */
+  0xea, 0xc0, 0xc0, 0x00,  /*    bv    %r0(%r22)               */
+  0x0e, 0x88, 0x10, 0x95,  /*    ldw   4(%r20),%r21            */
+#define PLT_STUB_ENTRY (3*4)
+  0xea, 0x9f, 0x1f, 0xdd,  /*    b,l   1b,%r20                 */
+  0xd6, 0x80, 0x1c, 0x1e,  /*    depi  0,31,2,%r20             */
+  0x00, 0xc0, 0xff, 0xee,  /* 9: .word fixup_func              */
+  0xde, 0xad, 0xbe, 0xef   /*    .word fixup_ltp               */
+};
+
 /* Section name for stubs is the associated section name plus this
    string.  */
 #define STUB_SUFFIX ".stub"
@@ -119,13 +141,19 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #define LONG_BRANCH_PIC_IN_SHLIB 1
 #endif
 
+/* Set this non-zero to use import stubs instead of long branch stubs
+   where a .plt entry exists for the symbol.  This is a fairly useless
+   option as import stubs are bigger than PIC long branch stubs.  */
+#ifndef LONG_BRANCH_VIA_PLT
+#define LONG_BRANCH_VIA_PLT 0
+#endif
+
 /* We don't need to copy any PC- or GP-relative dynamic relocs into a
    shared object's dynamic section.  */
 #ifndef RELATIVE_DYNAMIC_RELOCS
 #define RELATIVE_DYNAMIC_RELOCS 0
 #endif
 
-
 enum elf32_hppa_stub_type {
   hppa_stub_long_branch,
   hppa_stub_long_branch_shared,
@@ -135,7 +163,6 @@ enum elf32_hppa_stub_type {
   hppa_stub_none
 };
 
-
 struct elf32_hppa_stub_hash_entry {
 
   /* Base hash table entry structure.  */
@@ -162,11 +189,11 @@ struct elf32_hppa_stub_hash_entry {
   /* The symbol table entry, if any, that this was derived from.  */
   struct elf32_hppa_link_hash_entry *h;
 
-  /* Where this stub is being called from.  */
-  asection *input_section;
+  /* Where this stub is being called from, or, in the case of combined
+     stub sections, the first input section in the group.  */
+  asection *id_sec;
 };
 
-
 struct elf32_hppa_link_hash_entry {
 
   struct elf_link_hash_entry elf;
@@ -198,9 +225,15 @@ struct elf32_hppa_link_hash_entry {
 #endif
 
   /* Set during a static link if we detect a function is PIC.  */
-  boolean pic_call;
-};
+  unsigned int pic_call:1;
 
+  /* Set if this symbol is used by a plabel reloc.  */
+  unsigned int plabel:1;
+
+  /* Set if this symbol is an init or fini function and thus should
+     use an absolute reloc.  */
+  unsigned int plt_abs:1;
+};
 
 struct elf32_hppa_link_hash_table {
 
@@ -213,23 +246,23 @@ struct elf32_hppa_link_hash_table {
   /* Linker stub bfd.  */
   bfd *stub_bfd;
 
-  /* Whether we support multiple sub-spaces for shared libs.  */
-  boolean multi_subspace;
-
   /* Linker call-backs.  */
   asection * (*add_stub_section) PARAMS ((const char *, asection *));
   void (*layout_sections_again) PARAMS ((void));
 
-  /* Arrays to keep track of which stub sections have been created.  */
-  asection **stub_section_created;
+  /* Array to keep track of which stub sections have been created, and
+     information on stub grouping.  */
+  struct map_stub {
+    /* This is the section to which stubs in the group will be
+       attached.  */
+    asection *link_sec;
+    /* The stub section.  */
+    asection *stub_sec;
 #if ! LONG_BRANCH_PIC_IN_SHLIB
-  asection **reloc_section_created;
+    /* The stub section's reloc section.  */
+    asection *reloc_sec;
 #endif
-  int first_init_sec;
-  int first_fini_sec;
-
-  /* Current offsets in the stub sections.  */
-  bfd_vma *offset;
+  } *stub_group;
 
   /* Short-cuts to get to dynamic linker sections.  */
   asection *sgot;
@@ -238,16 +271,18 @@ struct elf32_hppa_link_hash_table {
   asection *srelplt;
   asection *sdynbss;
   asection *srelbss;
-};
 
+  /* Whether we support multiple sub-spaces for shared libs.  */
+  unsigned int multi_subspace:1;
 
-/* Functions named elf32_hppa_* are called by external routines, other
-   functions are only called locally.  elf32_hppa_* functions appear
-   in this file more or less in the order in which they are called
-   from external routines.  eg. elf32_hppa_check_relocs is called
-   early in the link process, elf32_hppa_finish_dynamic_sections is
-   one of the last functions.  */
+  /* Flags set when PCREL12F and PCREL17F branches detected.  Used to
+     select suitable defaults for the stub group size.  */
+  unsigned int has_12bit_branch:1;
+  unsigned int has_17bit_branch:1;
 
+  /* Set if we need a .plt stub to support lazy dynamic linking.  */
+  unsigned int need_plt_stub:1;
+};
 
 /* Various hash macros and functions.  */
 #define hppa_link_hash_table(p) \
@@ -266,7 +301,6 @@ static struct bfd_hash_entry *hppa_link_hash_newfunc
 static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
   PARAMS ((bfd *));
 
-
 /* Stub handling functions.  */
 static char *hppa_stub_name
   PARAMS ((const asection *, const asection *,
@@ -276,11 +310,11 @@ static char *hppa_stub_name
 static struct elf32_hppa_stub_hash_entry *hppa_get_stub_entry
   PARAMS ((const asection *, const asection *,
           struct elf32_hppa_link_hash_entry *,
-          const Elf_Internal_Rela *, struct bfd_link_info *));
+          const Elf_Internal_Rela *,
+          struct elf32_hppa_link_hash_table *));
 
 static struct elf32_hppa_stub_hash_entry *hppa_add_stub
-  PARAMS ((const char *, asection *, unsigned int,
-          struct bfd_link_info *));
+  PARAMS ((const char *, asection *, struct elf32_hppa_link_hash_table *));
 
 static enum elf32_hppa_stub_type hppa_type_of_stub
   PARAMS ((asection *, const Elf_Internal_Rela *,
@@ -292,7 +326,6 @@ static boolean hppa_build_one_stub
 static boolean hppa_size_one_stub
   PARAMS ((struct bfd_hash_entry *, PTR));
 
-
 /* BFD and elf backend functions.  */
 static boolean elf32_hppa_object_p PARAMS ((bfd *));
 
@@ -315,23 +348,30 @@ static boolean elf32_hppa_gc_sweep_hook
   PARAMS ((bfd *, struct bfd_link_info *,
           asection *, const Elf_Internal_Rela *));
 
+static void elf32_hppa_hide_symbol
+  PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
+
 static boolean elf32_hppa_adjust_dynamic_symbol
   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
 
 static boolean hppa_handle_PIC_calls
   PARAMS ((struct elf_link_hash_entry *, PTR));
 
-#if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
+#if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
+     || RELATIVE_DYNAMIC_RELOCS)
 static boolean hppa_discard_copies
   PARAMS ((struct elf_link_hash_entry *, PTR));
 #endif
 
+static boolean clobber_millicode_symbols
+  PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
+
 static boolean elf32_hppa_size_dynamic_sections
   PARAMS ((bfd *, struct bfd_link_info *));
 
 static bfd_reloc_status_type final_link_relocate
   PARAMS ((asection *, bfd_byte *, const Elf_Internal_Rela *,
-          bfd_vma, struct bfd_link_info *, asection *,
+          bfd_vma, struct elf32_hppa_link_hash_table *, asection *,
           struct elf32_hppa_link_hash_entry *));
 
 static boolean elf32_hppa_relocate_section
@@ -348,7 +388,6 @@ static boolean elf32_hppa_finish_dynamic_sections
 static int elf32_hppa_elf_get_symbol_type
   PARAMS ((Elf_Internal_Sym *, int));
 
-
 /* Assorted hash table functions.  */
 
 /* Initialize an entry in the stub hash table.  */
@@ -390,13 +429,12 @@ stub_hash_newfunc (entry, table, string)
       ret->target_section = NULL;
       ret->stub_type = hppa_stub_long_branch;
       ret->h = NULL;
-      ret->input_section = NULL;
+      ret->id_sec = NULL;
     }
 
   return (struct bfd_hash_entry *) ret;
 }
 
-
 /* Initialize an entry in the link hash table.  */
 
 static struct bfd_hash_entry *
@@ -436,12 +474,13 @@ hppa_link_hash_newfunc (entry, table, string)
       ret->reloc_entries = NULL;
 #endif
       ret->pic_call = 0;
+      ret->plabel = 0;
+      ret->plt_abs = 0;
     }
 
   return (struct bfd_hash_entry *) ret;
 }
 
-
 /* Create the derived linker hash table.  The PA ELF port uses the derived
    hash table to keep information specific to the PA ELF linker (without
    using static variables).  */
@@ -467,27 +506,23 @@ elf32_hppa_link_hash_table_create (abfd)
     return NULL;
 
   ret->stub_bfd = NULL;
-  ret->multi_subspace = 0;
   ret->add_stub_section = NULL;
   ret->layout_sections_again = NULL;
-  ret->stub_section_created = NULL;
-#if ! LONG_BRANCH_PIC_IN_SHLIB
-  ret->reloc_section_created = NULL;
-#endif
-  ret->first_init_sec = 0;
-  ret->first_fini_sec = 0;
-  ret->offset = NULL;
+  ret->stub_group = NULL;
   ret->sgot = NULL;
   ret->srelgot = NULL;
   ret->splt = NULL;
   ret->srelplt = NULL;
   ret->sdynbss = NULL;
   ret->srelbss = NULL;
+  ret->multi_subspace = 0;
+  ret->has_12bit_branch = 0;
+  ret->has_17bit_branch = 0;
+  ret->need_plt_stub = 0;
 
   return &ret->root.root;
 }
 
-
 /* Build a name for an entry in the stub hash table.  */
 
 static char *
@@ -498,7 +533,7 @@ hppa_stub_name (input_section, sym_sec, hash, rel)
      const Elf_Internal_Rela *rel;
 {
   char *stub_name;
-  unsigned int len;
+  size_t len;
 
   if (hash)
     {
@@ -528,39 +563,43 @@ hppa_stub_name (input_section, sym_sec, hash, rel)
   return stub_name;
 }
 
-
 /* Look up an entry in the stub hash.  Stub entries are cached because
    creating the stub name takes a bit of time.  */
 
 static struct elf32_hppa_stub_hash_entry *
-hppa_get_stub_entry (input_section, sym_sec, hash, rel, info)
+hppa_get_stub_entry (input_section, sym_sec, hash, rel, hplink)
      const asection *input_section;
      const asection *sym_sec;
      struct elf32_hppa_link_hash_entry *hash;
      const Elf_Internal_Rela *rel;
-     struct bfd_link_info *info;
+     struct elf32_hppa_link_hash_table *hplink;
 {
   struct elf32_hppa_stub_hash_entry *stub_entry;
+  const asection *id_sec;
+
+  /* If this input section is part of a group of sections sharing one
+     stub section, then use the id of the first section in the group.
+     Stub names need to include a section id, as there may well be
+     more than one stub used to reach say, printf, and we need to
+     distinguish between them.  */
+  id_sec = hplink->stub_group[input_section->id].link_sec;
 
   if (hash != NULL && hash->stub_cache != NULL
       && hash->stub_cache->h == hash
-      && hash->stub_cache->input_section == input_section)
+      && hash->stub_cache->id_sec == id_sec)
     {
       stub_entry = hash->stub_cache;
     }
   else
     {
-      struct bfd_hash_table *stub_hash_table;
       char *stub_name;
 
-      stub_name = hppa_stub_name (input_section, sym_sec, hash, rel);
+      stub_name = hppa_stub_name (id_sec, sym_sec, hash, rel);
       if (stub_name == NULL)
        return NULL;
 
-      stub_hash_table = &hppa_link_hash_table (info)->stub_hash_table;
-
-      stub_entry = hppa_stub_hash_lookup (stub_hash_table, stub_name,
-                                         false, false);
+      stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table,
+                                         stub_name, false, false);
       if (stub_entry == NULL)
        {
          if (hash == NULL || hash->elf.root.type != bfd_link_hash_undefweak)
@@ -582,91 +621,43 @@ hppa_get_stub_entry (input_section, sym_sec, hash, rel, info)
   return stub_entry;
 }
 
-
 /* Add a new stub entry to the stub hash.  Not all fields of the new
    stub entry are initialised.  */
 
 static struct elf32_hppa_stub_hash_entry *
-hppa_add_stub (stub_name, section, sec_count, info)
+hppa_add_stub (stub_name, section, hplink)
      const char *stub_name;
      asection *section;
-     unsigned int sec_count;
-     struct bfd_link_info *info;
+     struct elf32_hppa_link_hash_table *hplink;
 {
+  asection *link_sec;
   asection *stub_sec;
-#if ! LONG_BRANCH_PIC_IN_SHLIB
-  asection *reloc_sec;
-#endif
   struct elf32_hppa_stub_hash_entry *stub_entry;
-  struct elf32_hppa_link_hash_table *hplink;
 
-  hplink = hppa_link_hash_table (info);
-  stub_sec = hplink->stub_section_created[sec_count];
+  link_sec = hplink->stub_group[section->id].link_sec;
+  stub_sec = hplink->stub_group[section->id].stub_sec;
   if (stub_sec == NULL)
     {
-      int special_sec = 0;
-
-      /* We only want one stub for .init and .fini because glibc
-        splits the _init and _fini functions into two parts.  We
-        don't want to put a stub in the middle of a function.
-        It would be better to merge all the stub sections for an
-        output section if the output section + stubs is small enough.
-        This would fix the .init and .fini case and also allow stubs
-        to be merged.  It's more linker work though.  */
-      if (strncmp (section->name, ".init", 5) == 0)
-       {
-         stub_sec = hplink->stub_section_created[hplink->first_init_sec];
-         special_sec = 1;
-       }
-      else if (strncmp (section->name, ".fini", 5) == 0)
-       {
-         stub_sec = hplink->stub_section_created[hplink->first_fini_sec];
-         special_sec = 2;
-       }
+      stub_sec = hplink->stub_group[link_sec->id].stub_sec;
       if (stub_sec == NULL)
        {
-         int len;
+         size_t len;
          char *s_name;
 
-         len = strlen (section->name) + sizeof (STUB_SUFFIX);
+         len = strlen (link_sec->name) + sizeof (STUB_SUFFIX);
          s_name = bfd_alloc (hplink->stub_bfd, len);
          if (s_name == NULL)
            return NULL;
 
-         strcpy (s_name, section->name);
+         strcpy (s_name, link_sec->name);
          strcpy (s_name + len - sizeof (STUB_SUFFIX), STUB_SUFFIX);
-         stub_sec = (*hplink->add_stub_section) (s_name, section);
+         stub_sec = (*hplink->add_stub_section) (s_name, link_sec);
          if (stub_sec == NULL)
            return NULL;
-
-         if (special_sec != 0)
-           {
-             if (special_sec == 1)
-               hplink->first_init_sec = sec_count;
-             else
-               hplink->first_fini_sec = sec_count;
-           }
+         hplink->stub_group[link_sec->id].stub_sec = stub_sec;
        }
-      hplink->stub_section_created[sec_count] = stub_sec;
-    }
-
-#if ! LONG_BRANCH_PIC_IN_SHLIB
-  reloc_sec = hplink->reloc_section_created[sec_count];
-  if (reloc_sec == NULL && info->shared)
-    {
-      char *name;
-
-      name = bfd_malloc (sizeof ".rela" + strlen (stub_sec->name));
-      if (name == NULL)
-       return NULL;
-      strcpy (name, ".rela");
-      strcpy (name + sizeof ".rela" - 1, stub_sec->name);
-      reloc_sec = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
-                                          name);
-      hplink->reloc_section_created[sec_count] = reloc_sec;
-      free (name);
+      hplink->stub_group[section->id].stub_sec = stub_sec;
     }
-#endif
 
   /* Enter this entry into the linker stub hash table.  */
   stub_entry = hppa_stub_hash_lookup (&hplink->stub_hash_table, stub_name,
@@ -681,14 +672,13 @@ hppa_add_stub (stub_name, section, sec_count, info)
 
   stub_entry->stub_sec = stub_sec;
 #if ! LONG_BRANCH_PIC_IN_SHLIB
-  stub_entry->reloc_sec = reloc_sec;
+  stub_entry->reloc_sec = hplink->stub_group[section->id].reloc_sec;
 #endif
   stub_entry->stub_offset = 0;
-  stub_entry->input_section = section;
+  stub_entry->id_sec = link_sec;
   return stub_entry;
 }
 
-
 /* Determine the type of stub needed, if any, for a call.  */
 
 static enum elf32_hppa_stub_type
@@ -705,17 +695,20 @@ hppa_type_of_stub (input_sec, rel, hash, destination)
 
   if (hash != NULL
       && (((hash->elf.root.type == bfd_link_hash_defined
-              || hash->elf.root.type == bfd_link_hash_defweak)
-             && hash->elf.root.u.def.section->output_section == NULL)
+           || hash->elf.root.type == bfd_link_hash_defweak)
+          && hash->elf.root.u.def.section->output_section == NULL)
+         || (hash->elf.root.type == bfd_link_hash_defweak
+             && hash->elf.dynindx != -1
+             && hash->elf.plt.offset != (bfd_vma) -1)
          || hash->elf.root.type == bfd_link_hash_undefweak
          || hash->elf.root.type == bfd_link_hash_undefined
          || hash->pic_call))
     {
       /* If output_section is NULL, then it's a symbol defined in a
         shared library.  We will need an import stub.  Decide between
-        hppa_stub_import and hppa_stub_import_shared later.
-        For shared links we need stubs for undefined syms too;  They
-        will presumably be resolved by the dynamic linker.  */
+        hppa_stub_import and hppa_stub_import_shared later.  For
+        shared links we need stubs for undefined or weak syms too;
+        They will presumably be resolved by the dynamic linker.  */
       return hppa_stub_import;
     }
 
@@ -739,14 +732,14 @@ hppa_type_of_stub (input_sec, rel, hash, destination)
     {
       max_branch_offset = (1 << (12-1)) << 2;
     }
-  else /* R_PARISC_PCREL22F */
+  else /* R_PARISC_PCREL22F */
     {
       max_branch_offset = (1 << (22-1)) << 2;
     }
 
   if (branch_offset + max_branch_offset >= 2*max_branch_offset)
     {
-#if 0
+#if LONG_BRANCH_VIA_PLT
       if (hash != NULL
          && hash->elf.dynindx != -1
          && hash->elf.plt.offset != (bfd_vma) -1)
@@ -762,7 +755,6 @@ hppa_type_of_stub (input_sec, rel, hash, destination)
   return hppa_stub_none;
 }
 
-
 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
    IN_ARG contains the link info pointer.  */
 
@@ -814,7 +806,8 @@ hppa_build_one_stub (gen_entry, in_arg)
   bfd *stub_bfd;
   bfd_byte *loc;
   bfd_vma sym_value;
-  unsigned int insn;
+  bfd_vma insn;
+  int val;
   int size;
 
   /* Massage our args to the form they really have.  */
@@ -825,7 +818,7 @@ hppa_build_one_stub (gen_entry, in_arg)
   stub_sec = stub_entry->stub_sec;
 
   /* Make a note of the offset within the stubs for this entry.  */
-  stub_entry->stub_offset = hplink->offset[stub_sec->index];
+  stub_entry->stub_offset = stub_sec->_raw_size;
   loc = stub_sec->contents + stub_entry->stub_offset;
 
   stub_bfd = stub_sec->owner;
@@ -841,14 +834,12 @@ hppa_build_one_stub (gen_entry, in_arg)
                   + stub_entry->target_section->output_offset
                   + stub_entry->target_section->output_section->vma);
 
-      insn = hppa_rebuild_insn ((int) LDIL_R1,
-                               hppa_field_adjust (sym_value, 0, e_lrsel),
-                               21);
+      val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_lrsel);
+      insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
       bfd_put_32 (stub_bfd, insn, loc);
 
-      insn = hppa_rebuild_insn ((int) BE_SR4_R1,
-                               hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
-                               17);
+      val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_rrsel) >> 2;
+      insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
       bfd_put_32 (stub_bfd, insn, loc + 4);
 
 #if ! LONG_BRANCH_PIC_IN_SHLIB
@@ -910,15 +901,13 @@ hppa_build_one_stub (gen_entry, in_arg)
                    + stub_sec->output_offset
                    + stub_sec->output_section->vma);
 
-      bfd_put_32 (stub_bfd, (unsigned int) BL_R1, loc);
-      insn = hppa_rebuild_insn ((int) ADDIL_R1,
-                               hppa_field_adjust (sym_value, -8, e_lsel),
-                               21);
+      bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
+      val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
+      insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
       bfd_put_32 (stub_bfd, insn, loc + 4);
 
-      insn = hppa_rebuild_insn ((int) BE_SR4_R1,
-                               hppa_field_adjust (sym_value, -8, e_rsel) >> 2,
-                               17);
+      val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
+      insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
       bfd_put_32 (stub_bfd, insn, loc + 8);
       size = 12;
       break;
@@ -935,36 +924,37 @@ hppa_build_one_stub (gen_entry, in_arg)
       if (stub_entry->stub_type == hppa_stub_import_shared)
        insn = ADDIL_R19;
 #endif
-      insn = hppa_rebuild_insn ((int) insn,
-                               hppa_field_adjust (sym_value, 0, e_lsel),
-                               21);
+      val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_lrsel),
+      insn = hppa_rebuild_insn ((int) insn, val, 21);
       bfd_put_32 (stub_bfd, insn, loc);
 
-      insn = hppa_rebuild_insn ((int) LDW_R1_R21,
-                               hppa_field_adjust (sym_value, 0, e_rsel),
-                               14);
+      /* It is critical to use lrsel/rrsel here because we are using
+        two different offsets (+0 and +4) from sym_value.  If we use
+        lsel/rsel then with unfortunate sym_values we will round
+        sym_value+4 up to the next 2k block leading to a mis-match
+        between the lsel and rsel value.  */
+      val = hppa_field_adjust (sym_value, (bfd_signed_vma) 0, e_rrsel);
+      insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
       bfd_put_32 (stub_bfd, insn, loc + 4);
 
       if (hplink->multi_subspace)
        {
-         insn = hppa_rebuild_insn ((int) LDW_R1_DLT,
-                                   hppa_field_adjust (sym_value, 4, e_rsel),
-                                   14);
+         val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
+         insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
          bfd_put_32 (stub_bfd, insn, loc + 8);
 
-         bfd_put_32 (stub_bfd, (unsigned int) LDSID_R21_R1, loc + 12);
-         bfd_put_32 (stub_bfd, (unsigned int) MTSP_R1,      loc + 16);
-         bfd_put_32 (stub_bfd, (unsigned int) BE_SR0_R21,   loc + 20);
-         bfd_put_32 (stub_bfd, (unsigned int) STW_RP,       loc + 24);
+         bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
+         bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,      loc + 16);
+         bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21,   loc + 20);
+         bfd_put_32 (stub_bfd, (bfd_vma) STW_RP,       loc + 24);
 
          size = 28;
        }
       else
        {
-         bfd_put_32 (stub_bfd, (unsigned int) BV_R0_R21, loc + 8);
-         insn = hppa_rebuild_insn ((int) LDW_R1_DLT,
-                                   hppa_field_adjust (sym_value, 4, e_rsel),
-                                   14);
+         bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
+         val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
+         insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
          bfd_put_32 (stub_bfd, insn, loc + 12);
 
          size = 16;
@@ -980,7 +970,7 @@ hppa_build_one_stub (gen_entry, in_arg)
          struct elf32_hppa_link_hash_entry *eh;
          bfd_vma value;
 
-         dynobj = elf_hash_table (info)->dynobj;
+         dynobj = hplink->root.dynobj;
          eh = (struct elf32_hppa_link_hash_entry *) stub_entry->h;
 
          BFD_ASSERT (eh->elf.root.type == bfd_link_hash_defined
@@ -993,7 +983,8 @@ hppa_build_one_stub (gen_entry, in_arg)
          /* Fill in the entry in the procedure linkage table.
 
             The format of a plt entry is
-            <funcaddr> <__gp>.  */
+            <funcaddr>
+            <__gp>.  */
 
          bfd_put_32 (hplink->splt->owner, value,
                      hplink->splt->contents + eh->elf.plt.offset);
@@ -1026,20 +1017,19 @@ hppa_build_one_stub (gen_entry, in_arg)
          return false;
        }
 
-      insn = hppa_rebuild_insn ((int) BL_RP,
-                               hppa_field_adjust (sym_value, -8, e_fsel) >> 2,
-                               17);
+      val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
+      insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
       bfd_put_32 (stub_bfd, insn, loc);
 
-      bfd_put_32 (stub_bfd, (unsigned int) NOP,         loc + 4);
-      bfd_put_32 (stub_bfd, (unsigned int) LDW_RP,      loc + 8);
-      bfd_put_32 (stub_bfd, (unsigned int) LDSID_RP_R1, loc + 12);
-      bfd_put_32 (stub_bfd, (unsigned int) MTSP_R1,     loc + 16);
-      bfd_put_32 (stub_bfd, (unsigned int) BE_SR0_RP,   loc + 20);
+      bfd_put_32 (stub_bfd, (bfd_vma) NOP,         loc + 4);
+      bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP,      loc + 8);
+      bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
+      bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,     loc + 16);
+      bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP,   loc + 20);
 
       /* Point the function symbol at the stub.  */
       stub_entry->h->elf.root.u.def.section = stub_sec;
-      stub_entry->h->elf.root.u.def.value = hplink->offset[stub_sec->index];
+      stub_entry->h->elf.root.u.def.value = stub_sec->_raw_size;
 
       size = 24;
       break;
@@ -1049,7 +1039,7 @@ hppa_build_one_stub (gen_entry, in_arg)
       return false;
     }
 
-  hplink->offset[stub_sec->index] += size;
+  stub_sec->_raw_size += size;
   return true;
 }
 
@@ -1075,7 +1065,6 @@ hppa_build_one_stub (gen_entry, in_arg)
 #undef LDSID_RP_R1
 #undef BE_SR0_RP
 
-
 /* As above, but don't actually build the stub.  Just bump offset so
    we know stub section sizes.  */
 
@@ -1093,12 +1082,18 @@ hppa_size_one_stub (gen_entry, in_arg)
   hplink = (struct elf32_hppa_link_hash_table *) in_arg;
 
   if (stub_entry->stub_type == hppa_stub_long_branch)
-    size = 8;
+    {
+#if ! LONG_BRANCH_PIC_IN_SHLIB
+      if (stub_entry->reloc_sec != NULL)
+       stub_entry->reloc_sec->_raw_size += sizeof (Elf32_External_Rela);
+#endif
+      size = 8;
+    }
   else if (stub_entry->stub_type == hppa_stub_long_branch_shared)
     size = 12;
   else if (stub_entry->stub_type == hppa_stub_export)
     size = 24;
-  else /* hppa_stub_import or hppa_stub_import_shared */
+  else /* hppa_stub_import or hppa_stub_import_shared */
     {
       if (hplink->multi_subspace)
        size = 28;
@@ -1106,11 +1101,10 @@ hppa_size_one_stub (gen_entry, in_arg)
        size = 16;
     }
 
-  hplink->offset[stub_entry->stub_sec->index] += size;
+  stub_entry->stub_sec->_raw_size += size;
   return true;
 }
 
-
 /* Return nonzero if ABFD represents an HPPA ELF32 file.
    Additionally we set the default architecture and machine.  */
 
@@ -1134,7 +1128,6 @@ elf32_hppa_object_p (abfd)
   return true;
 }
 
-
 /* Undo the generic ELF code's subtraction of section->vma from the
    value of each external symbol.  */
 
@@ -1152,7 +1145,6 @@ elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
   return true;
 }
 
-
 /* Create the .plt and .got sections, and set up our hash table
    short-cuts to various dynamic sections.  */
 
@@ -1161,8 +1153,6 @@ elf32_hppa_create_dynamic_sections (abfd, info)
      bfd *abfd;
      struct bfd_link_info *info;
 {
-  flagword flags;
-  asection *s;
   struct elf32_hppa_link_hash_table *hplink;
 
   /* Don't try to create the .plt and .got twice.  */
@@ -1174,14 +1164,7 @@ elf32_hppa_create_dynamic_sections (abfd, info)
   if (! _bfd_elf_create_dynamic_sections (abfd, info))
     return false;
 
-  /* Our .plt just contains pointers.  I suppose we should be using
-     .plt.got but .plt.got doesn't make too much sense without a .plt
-     section.  Set the flags to say the .plt isn't executable.  */
-  s = bfd_get_section_by_name (abfd, ".plt");
-  flags = bfd_get_section_flags (abfd, s);
-  if (! bfd_set_section_flags (abfd, s, flags & ~SEC_CODE))
-    return false;
-  hplink->splt = s;
+  hplink->splt = bfd_get_section_by_name (abfd, ".plt");
   hplink->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
 
   hplink->sgot = bfd_get_section_by_name (abfd, ".got");
@@ -1203,7 +1186,6 @@ elf32_hppa_create_dynamic_sections (abfd, info)
   return true;
 }
 
-
 /* Look through the relocs for a section during the first phase, and
    allocate space in the global offset table or procedure linkage
    table.  At this point we haven't necessarily read all the input
@@ -1230,7 +1212,7 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
     return true;
 
   hplink = hppa_link_hash_table (info);
-  dynobj = elf_hash_table (info)->dynobj;
+  dynobj = hplink->root.dynobj;
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
   local_got_refcounts = elf_local_got_refcounts (abfd);
@@ -1245,10 +1227,11 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
        NEED_PLT = 2,
        NEED_DYNREL = 4,
 #if LONG_BRANCH_PIC_IN_SHLIB
-       NEED_STUBREL = 0  /* We won't be needing them in this case.  */
+       NEED_STUBREL = 0,  /* We won't be needing them in this case.  */
 #else
-       NEED_STUBREL = 8
+       NEED_STUBREL = 8,
 #endif
+       PLT_PLABEL = 16
       };
 
       unsigned int r_symndx, r_type;
@@ -1280,29 +1263,27 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
        case R_PARISC_PLABEL14R: /* "Official" procedure labels.  */
        case R_PARISC_PLABEL21L:
        case R_PARISC_PLABEL32:
-         if (h == NULL)
-           {
-             /* If this is a local symbol we do not need to create a
-                PLT entry, but if we are creating a shared object we
-                have to output a relocation for the PLABEL itself.  */
-             need_entry = NEED_DYNREL;
-           }
-         else
-           {
-             /* If it is a global symbol, then we do need to create a
-                PLT entry, and additionally, if we are creating a
-                shared object, we need to output a dynamic relocation
-                pointing to that PLT entry.  */
-             need_entry = NEED_PLT | NEED_DYNREL;
-           }
+         /* If the addend is non-zero, we break badly.  */
+         BFD_ASSERT (rel->r_addend == 0);
+
+         /* If we are creating a shared library, then we need to
+            create a PLT entry for all PLABELs, because PLABELs with
+            local symbols may be passed via a pointer to another
+            object.  Additionally, output a dynamic relocation
+            pointing to the PLT entry.  */
+         need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
          break;
 
        case R_PARISC_PCREL12F:
+         hplink->has_12bit_branch = 1;
+         /* Fall thru.  */
        case R_PARISC_PCREL17C:
        case R_PARISC_PCREL17F:
+         hplink->has_17bit_branch = 1;
+         /* Fall thru.  */
        case R_PARISC_PCREL22F:
-         /* Handle calls, and function pointers as they might need to
-            go through the .plt, and might require long branch stubs.  */
+         /* Function calls might need to go through the .plt, and
+            might require long branch stubs.  */
          if (h == NULL)
            {
              /* We know local syms won't need a .plt entry, and if
@@ -1349,7 +1330,8 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
 
        case R_PARISC_DIR17F: /* Used for external branches.  */
        case R_PARISC_DIR17R:
-       case R_PARISC_DIR14R: /* Used for load/store from absolute locn.  */
+       case R_PARISC_DIR14F: /* Used for load/store from absolute locn.  */
+       case R_PARISC_DIR14R:
        case R_PARISC_DIR21L: /* As above, and for ext branches too.  */
 #if 1
          /* Help debug shared library creation.  Any of the above
@@ -1382,7 +1364,7 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
             used.  Record for later use during GC.  */
        case R_PARISC_GNU_VTENTRY:
          if (!_bfd_elf32_gc_record_vtentry (abfd, sec,
-                                            &h->elf, rel->r_offset))
+                                            &h->elf, rel->r_addend))
            return false;
          continue;
 
@@ -1394,9 +1376,9 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
       if (need_entry & NEED_GOT)
        {
          /* Allocate space for a GOT entry, as well as a dynamic
-             relocation for this entry.  */
+            relocation for this entry.  */
          if (dynobj == NULL)
-           elf_hash_table (info)->dynobj = dynobj = abfd;
+           hplink->root.dynobj = dynobj = abfd;
 
          if (hplink->sgot == NULL)
            {
@@ -1431,7 +1413,11 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
                {
                  size_t size;
 
-                 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
+                 /* Allocate space for local got offsets and local
+                    plt offsets.  Done this way to save polluting
+                    elf_obj_tdata with another target specific
+                    pointer.  */
+                 size = symtab_hdr->sh_info * 2 * sizeof (bfd_signed_vma);
                  local_got_refcounts = ((bfd_signed_vma *)
                                         bfd_alloc (abfd, size));
                  if (local_got_refcounts == NULL)
@@ -1471,13 +1457,46 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
             clean up later in adjust_dynamic_symbol.  */
          if ((sec->flags & SEC_ALLOC) != 0)
            {
-             if (h->elf.plt.refcount == -1)
+             if (h != NULL)
                {
-                 h->elf.plt.refcount = 1;
-                 h->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
+                 if (h->elf.plt.refcount == -1)
+                   {
+                     h->elf.plt.refcount = 1;
+                     h->elf.elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
+                   }
+                 else
+                   h->elf.plt.refcount += 1;
+
+                 /* If this .plt entry is for a plabel, mark it so
+                    that adjust_dynamic_symbol will keep the entry
+                    even if it appears to be local.  */
+                 if (need_entry & PLT_PLABEL)
+                   h->plabel = 1;
+               }
+             else if (need_entry & PLT_PLABEL)
+               {
+                 int indx;
+
+                 if (local_got_refcounts == NULL)
+                   {
+                     size_t size;
+
+                     /* Allocate space for local got offsets and local
+                        plt offsets.  */
+                     size = symtab_hdr->sh_info * 2 * sizeof (bfd_signed_vma);
+                     local_got_refcounts = ((bfd_signed_vma *)
+                                            bfd_alloc (abfd, size));
+                     if (local_got_refcounts == NULL)
+                       return false;
+                     elf_local_got_refcounts (abfd) = local_got_refcounts;
+                     memset (local_got_refcounts, -1, size);
+                   }
+                 indx = r_symndx + symtab_hdr->sh_info;
+                 if (local_got_refcounts[indx] == -1)
+                   local_got_refcounts[indx] = 1;
+                 else
+                   local_got_refcounts[indx] += 1;
                }
-             else
-               h->elf.plt.refcount += 1;
            }
        }
 
@@ -1519,8 +1538,7 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
                  || is_absolute_reloc (r_type)
                  || (h != NULL
                      && ((h->elf.elf_link_hash_flags
-                          & ELF_LINK_HASH_DEF_REGULAR) == 0
-                         || h->elf.root.type == bfd_link_hash_defweak)))
+                          & ELF_LINK_HASH_DEF_REGULAR) == 0)))
 #endif
              )
            {
@@ -1538,7 +1556,7 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
                  char *name;
 
                  if (dynobj == NULL)
-                   elf_hash_table (info)->dynobj = dynobj = abfd;
+                   hplink->root.dynobj = dynobj = abfd;
 
                  name = bfd_elf_string_from_elf_section
                    (abfd,
@@ -1555,7 +1573,7 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
 
                  if ((need_entry & NEED_STUBREL))
                    {
-                     int len = strlen (name) + sizeof (STUB_SUFFIX);
+                     size_t len = strlen (name) + sizeof (STUB_SUFFIX);
                      char *newname = bfd_malloc (len);
 
                      if (newname == NULL)
@@ -1656,7 +1674,6 @@ elf32_hppa_check_relocs (abfd, info, sec, relocs)
   return true;
 }
 
-
 /* Return the section that should be marked against garbage collection
    for a given relocation.  */
 
@@ -1705,7 +1722,6 @@ elf32_hppa_gc_mark_hook (abfd, info, rel, h, sym)
   return NULL;
 }
 
-
 /* Update the got and plt entry reference counts for the section being
    removed.  */
 
@@ -1719,9 +1735,11 @@ elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
   Elf_Internal_Shdr *symtab_hdr;
   struct elf_link_hash_entry **sym_hashes;
   bfd_signed_vma *local_got_refcounts;
+  bfd_signed_vma *local_plt_refcounts;
   const Elf_Internal_Rela *rel, *relend;
   unsigned long r_symndx;
   struct elf_link_hash_entry *h;
+  struct elf32_hppa_link_hash_table *hplink;
   bfd *dynobj;
   asection *sgot;
   asection *srelgot;
@@ -1729,13 +1747,16 @@ elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
   local_got_refcounts = elf_local_got_refcounts (abfd);
-
-  dynobj = elf_hash_table (info)->dynobj;
+  local_plt_refcounts = local_got_refcounts;
+  if (local_plt_refcounts != NULL)
+    local_plt_refcounts += symtab_hdr->sh_info;
+  hplink = hppa_link_hash_table (info);
+  dynobj = hplink->root.dynobj;
   if (dynobj == NULL)
     return true;
 
-  sgot = hppa_link_hash_table (info)->sgot;
-  srelgot = hppa_link_hash_table (info)->srelgot;
+  sgot = hplink->sgot;
+  srelgot = hplink->srelgot;
 
   relend = relocs + sec->reloc_count;
   for (rel = relocs; rel < relend; rel++)
@@ -1753,7 +1774,7 @@ elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
                h->got.refcount -= 1;
                if (h->got.refcount == 0)
                  {
-                   sgot->_raw_size -= 4;
+                   sgot->_raw_size -= GOT_ENTRY_SIZE;
                    srelgot->_raw_size -= sizeof (Elf32_External_Rela);
                  }
              }
@@ -1765,7 +1786,7 @@ elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
                local_got_refcounts[r_symndx] -= 1;
                if (local_got_refcounts[r_symndx] == 0)
                  {
-                   sgot->_raw_size -= 4;
+                   sgot->_raw_size -= GOT_ENTRY_SIZE;
                    if (info->shared)
                      srelgot->_raw_size -= sizeof (Elf32_External_Rela);
                  }
@@ -1773,9 +1794,6 @@ elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
          }
        break;
 
-      case R_PARISC_PLABEL14R:
-      case R_PARISC_PLABEL21L:
-      case R_PARISC_PLABEL32:
       case R_PARISC_PCREL12F:
       case R_PARISC_PCREL17C:
       case R_PARISC_PCREL17F:
@@ -1789,6 +1807,23 @@ elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
          }
        break;
 
+      case R_PARISC_PLABEL14R:
+      case R_PARISC_PLABEL21L:
+      case R_PARISC_PLABEL32:
+       r_symndx = ELF32_R_SYM (rel->r_info);
+       if (r_symndx >= symtab_hdr->sh_info)
+         {
+           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+           if (h->plt.refcount > 0)
+             h->plt.refcount -= 1;
+         }
+       else if (local_plt_refcounts != NULL)
+         {
+           if (local_plt_refcounts[r_symndx] > 0)
+             local_plt_refcounts[r_symndx] -= 1;
+         }
+       break;
+
       default:
        break;
       }
@@ -1796,6 +1831,21 @@ elf32_hppa_gc_sweep_hook (abfd, info, sec, relocs)
   return true;
 }
 
+/* Our own version of hide_symbol, so that we can keep plt entries for
+   plabels.  */
+
+static void
+elf32_hppa_hide_symbol (info, h)
+     struct bfd_link_info *info ATTRIBUTE_UNUSED;
+     struct elf_link_hash_entry *h;
+{
+  h->dynindx = -1;
+  if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
+    {
+      h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
+      h->plt.offset = (bfd_vma) -1;
+    }
+}
 
 /* Adjust a symbol defined by a dynamic object and referenced by a
    regular object.  The current definition is in some section of the
@@ -1812,8 +1862,8 @@ elf32_hppa_adjust_dynamic_symbol (info, h)
   struct elf32_hppa_link_hash_table *hplink;
   asection *s;
 
-  dynobj = elf_hash_table (info)->dynobj;
   hplink = hppa_link_hash_table (info);
+  dynobj = hplink->root.dynobj;
 
   /* If this is a function, put it in the procedure linkage table.  We
      will fill in the contents of the procedure linkage table later,
@@ -1824,17 +1874,19 @@ elf32_hppa_adjust_dynamic_symbol (info, h)
       if (h->plt.refcount <= 0
          || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
              && h->root.type != bfd_link_hash_defweak
+             && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
              && (!info->shared || info->symbolic)))
        {
          /* The .plt entry is not needed when:
             a) Garbage collection has removed all references to the
             symbol, or
             b) We know for certain the symbol is defined in this
-            object, and it's not a weak definition.  Either this
-            object is the application or we are doing a shared
-            symbolic link.  As a special sop to the hppa ABI, we
-            keep a .plt entry for functions in sections containing
-            PIC code.  */
+            object, and it's not a weak definition, nor is the symbol
+            used by a plabel relocation.  Either this object is the
+            application or we are doing a shared symbolic link.  */
+
+         /* As a special sop to the hppa ABI, we keep a .plt entry
+            for functions in sections containing PIC code.  */
          if (!info->shared
              && h->plt.refcount > 0
              && (h->root.type == bfd_link_hash_defined
@@ -1854,12 +1906,21 @@ elf32_hppa_adjust_dynamic_symbol (info, h)
       /* Make an entry in the .plt section.  */
       s = hplink->splt;
       h->plt.offset = s->_raw_size;
-      s->_raw_size += PLT_ENTRY_SIZE;
+      if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
+         && ((struct elf32_hppa_link_hash_entry *) h)->plabel
+         && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
+       {
+         /* Add some extra space for the dynamic linker to use.  */
+         s->_raw_size += PLABEL_PLT_ENTRY_SIZE;
+       }
+      else
+       s->_raw_size += PLT_ENTRY_SIZE;
 
       if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
        {
          /* Make sure this symbol is output as a dynamic symbol.  */
-         if (h->dynindx == -1)
+         if (h->dynindx == -1
+             && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
            {
              if (! bfd_elf32_link_record_dynamic_symbol (info, h))
                return false;
@@ -1868,6 +1929,8 @@ elf32_hppa_adjust_dynamic_symbol (info, h)
          /* We also need to make an entry in the .rela.plt section.  */
          s = hplink->srelplt;
          s->_raw_size += sizeof (Elf32_External_Rela);
+
+         hplink->need_plt_stub = 1;
        }
       return true;
     }
@@ -1952,7 +2015,6 @@ elf32_hppa_adjust_dynamic_symbol (info, h)
   return true;
 }
 
-
 /* Called via elf_link_hash_traverse to create .plt entries for an
    application that uses statically linked PIC functions.  Similar to
    the first part of elf32_hppa_adjust_dynamic_symbol.  */
@@ -1977,11 +2039,12 @@ hppa_handle_PIC_calls (h, inf)
       return true;
     }
 
+  h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
   ((struct elf32_hppa_link_hash_entry *) h)->pic_call = 1;
 
   info = (struct bfd_link_info *) inf;
-  dynobj = elf_hash_table (info)->dynobj;
   hplink = hppa_link_hash_table (info);
+  dynobj = hplink->root.dynobj;
 
   /* Make an entry in the .plt section.  */
   s = hplink->splt;
@@ -1991,8 +2054,8 @@ hppa_handle_PIC_calls (h, inf)
   return true;
 }
 
-
-#if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
+#if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
+     || RELATIVE_DYNAMIC_RELOCS)
 /* This function is called via elf_link_hash_traverse to discard space
    we allocated for relocs that it turned out we didn't need.  */
 
@@ -2008,7 +2071,7 @@ hppa_discard_copies (h, inf)
   eh = (struct elf32_hppa_link_hash_entry *) h;
   info = (struct bfd_link_info *) inf;
 
-#if ! LONG_BRANCH_PIC_IN_SHLIB
+#if ! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT
   /* Handle the stub reloc case.  If we have a plt entry for the
      function, we won't be needing long branch stubs.  s->count will
      only be zero for stub relocs, which provides a handy way of
@@ -2023,13 +2086,13 @@ hppa_discard_copies (h, inf)
     }
 #endif
 
+#if RELATIVE_DYNAMIC_RELOCS
   /* If a symbol has been forced local or we have found a regular
      definition for the symbolic link case, then we won't be needing
      any relocs.  */
-#if RELATIVE_DYNAMIC_RELOCS
   if (eh->elf.dynindx == -1
       || ((eh->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
-         && eh->elf.root.type != bfd_link_hash_defweak
+         && !is_absolute_reloc (r_type)
          && info->symbolic))
     {
       for (s = eh->reloc_entries; s != NULL; s = s->next)
@@ -2041,6 +2104,24 @@ hppa_discard_copies (h, inf)
 }
 #endif
 
+/* This function is called via elf_link_hash_traverse to force
+   millicode symbols local so they do not end up as globals in the
+   dynamic symbol table.  We ought to be able to do this in
+   adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
+   for all dynamic symbols.  Arguably, this is a bug in
+   elf_adjust_dynamic_symbol.  */
+
+static boolean
+clobber_millicode_symbols (h, info)
+     struct elf_link_hash_entry *h;
+     struct bfd_link_info *info;
+{
+  /* Note!  We only want to remove these from the dynamic symbol
+     table.  Therefore we do not set ELF_LINK_FORCED_LOCAL.  */
+  if (h->type == STT_PARISC_MILLI)
+    elf32_hppa_hide_symbol(info, h);
+  return true;
+}
 
 /* Set the sizes of the dynamic sections.  */
 
@@ -2056,11 +2137,14 @@ elf32_hppa_size_dynamic_sections (output_bfd, info)
   boolean reltext;
 
   hplink = hppa_link_hash_table (info);
-  dynobj = elf_hash_table (info)->dynobj;
+  dynobj = hplink->root.dynobj;
   BFD_ASSERT (dynobj != NULL);
 
-  if (elf_hash_table (info)->dynamic_sections_created)
+  if (hplink->root.dynamic_sections_created)
     {
+      const char *funcname;
+      bfd *i;
+
       /* Set the contents of the .interp section to the interpreter.  */
       if (! info->shared)
        {
@@ -2069,6 +2153,91 @@ elf32_hppa_size_dynamic_sections (output_bfd, info)
          s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
        }
+
+      /* Force millicode symbols local.  */
+      elf_link_hash_traverse (&hplink->root,
+                             clobber_millicode_symbols,
+                             info);
+
+      /* DT_INIT and DT_FINI need a .plt entry.  Make sure they have
+        one.  */
+      funcname = info->init_function;
+      while (1)
+       {
+         if (funcname != NULL)
+           {
+             struct elf_link_hash_entry *h;
+
+             h = elf_link_hash_lookup (&hplink->root,
+                                       funcname,
+                                       false, false, false);
+             if (h != NULL
+                 && (h->elf_link_hash_flags & (ELF_LINK_HASH_REF_REGULAR
+                                               | ELF_LINK_HASH_DEF_REGULAR)))
+               {
+                 if (h->plt.refcount <= 0)
+                   {
+                     h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
+
+                     /* Make an entry in the .plt section.  We know
+                        the function doesn't have a plabel by the
+                        refcount.  */
+                     s = hplink->splt;
+                     h->plt.offset = s->_raw_size;
+                     s->_raw_size += PLT_ENTRY_SIZE;
+
+                     /* Make sure this symbol is output as a dynamic
+                        symbol.  */
+                     if (h->dynindx == -1)
+                       {
+                         if (! bfd_elf32_link_record_dynamic_symbol (info, h))
+                           return false;
+                       }
+
+                     /* Make an entry for the reloc too.  */
+                     s = hplink->srelplt;
+                     s->_raw_size += sizeof (Elf32_External_Rela);
+                   }
+
+                 ((struct elf32_hppa_link_hash_entry *) h)->plt_abs = 1;
+               }
+           }
+         if (funcname == info->fini_function)
+           break;
+         funcname = info->fini_function;
+       }
+
+      /* Set up .plt offsets for local plabels.  */
+      for (i = info->input_bfds; i; i = i->link_next)
+       {
+         bfd_signed_vma *local_plt;
+         bfd_signed_vma *end_local_plt;
+         bfd_size_type locsymcount;
+         Elf_Internal_Shdr *symtab_hdr;
+
+         local_plt = elf_local_got_refcounts (i);
+         if (!local_plt)
+           continue;
+
+         symtab_hdr = &elf_tdata (i)->symtab_hdr;
+         locsymcount = symtab_hdr->sh_info;
+         local_plt += locsymcount;
+         end_local_plt = local_plt + locsymcount;
+
+         for (; local_plt < end_local_plt; ++local_plt)
+           {
+             if (*local_plt > 0)
+               {
+                 s = hplink->splt;
+                 *local_plt = s->_raw_size;
+                 s->_raw_size += PLT_ENTRY_SIZE;
+                 if (info->shared)
+                   hplink->srelplt->_raw_size += sizeof (Elf32_External_Rela);
+               }
+             else
+               *local_plt = (bfd_vma) -1;
+           }
+       }
     }
   else
     {
@@ -2088,7 +2257,8 @@ elf32_hppa_size_dynamic_sections (output_bfd, info)
       hplink->srelgot->_raw_size = 0;
     }
 
-#if ! LONG_BRANCH_PIC_IN_SHLIB || RELATIVE_DYNAMIC_RELOCS
+#if ((! LONG_BRANCH_PIC_IN_SHLIB && LONG_BRANCH_VIA_PLT) \
+     || RELATIVE_DYNAMIC_RELOCS)
   /* If this is a -Bsymbolic shared link, then we need to discard all
      relocs against symbols defined in a regular object.  We also need
      to lose relocs we've allocated for long branch stubs if we know
@@ -2120,28 +2290,22 @@ elf32_hppa_size_dynamic_sections (output_bfd, info)
          if (s->_raw_size != 0)
            {
              asection *target;
+             const char *outname;
 
              /* Remember whether there are any reloc sections other
                 than .rela.plt.  */
              if (strcmp (name+5, ".plt") != 0)
-               {
-                 const char *outname;
-
-                 relocs = true;
-
-                 /* If this relocation section applies to a read only
-                    section, then we probably need a DT_TEXTREL
-                    entry.  The entries in the .rela.plt section
-                    really apply to the .got section, which we
-                    created ourselves and so know is not readonly.  */
-                 outname = bfd_get_section_name (output_bfd,
-                                                 s->output_section);
-                 target = bfd_get_section_by_name (output_bfd, outname + 5);
-                 if (target != NULL
-                     && (target->flags & SEC_READONLY) != 0
-                     && (target->flags & SEC_ALLOC) != 0)
-                   reltext = true;
-               }
+               relocs = true;
+
+             /* If this relocation section applies to a read only
+                section, then we probably need a DT_TEXTREL entry.  */
+             outname = bfd_get_section_name (output_bfd,
+                                             s->output_section);
+             target = bfd_get_section_by_name (output_bfd, outname + 5);
+             if (target != NULL
+                 && (target->flags & SEC_READONLY) != 0
+                 && (target->flags & SEC_ALLOC) != 0)
+               reltext = true;
 
              /* We use the reloc_count field as a counter if we need
                 to copy relocs into the output file.  */
@@ -2149,7 +2313,22 @@ elf32_hppa_size_dynamic_sections (output_bfd, info)
            }
        }
       else if (strcmp (name, ".plt") == 0)
-       ;
+       {
+         if (hplink->need_plt_stub)
+           {
+             /* Make space for the plt stub at the end of the .plt
+                section.  We want this stub right at the end, up
+                against the .got section.  */
+             int gotalign = bfd_section_alignment (dynobj, hplink->sgot);
+             int pltalign = bfd_section_alignment (dynobj, s);
+             bfd_size_type mask;
+
+             if (gotalign > pltalign)
+               bfd_set_section_alignment (dynobj, s, gotalign);
+             mask = ((bfd_size_type) 1 << gotalign) - 1;
+             s->_raw_size = (s->_raw_size + sizeof (plt_stub) + mask) & ~mask;
+           }
+       }
       else if (strcmp (name, ".got") == 0)
        ;
       else
@@ -2180,7 +2359,7 @@ elf32_hppa_size_dynamic_sections (output_bfd, info)
        return false;
     }
 
-  if (elf_hash_table (info)->dynamic_sections_created)
+  if (hplink->root.dynamic_sections_created)
     {
       /* Like IA-64 and HPPA64, always create a DT_PLTGOT.  It
         actually has nothing to do with the PLT, it is how we
@@ -2228,7 +2407,6 @@ elf32_hppa_size_dynamic_sections (output_bfd, info)
   return true;
 }
 
-
 /* External entry points for sizing and building linker stubs.  */
 
 /* Determine and set the size of the stub section for a final link.
@@ -2238,20 +2416,27 @@ elf32_hppa_size_dynamic_sections (output_bfd, info)
    instruction.  */
 
 boolean
-elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
+elf32_hppa_size_stubs (output_bfd, stub_bfd, info, multi_subspace, group_size,
                       add_stub_section, layout_sections_again)
+     bfd *output_bfd;
      bfd *stub_bfd;
-     boolean multi_subspace;
      struct bfd_link_info *info;
+     boolean multi_subspace;
+     bfd_signed_vma group_size;
      asection * (*add_stub_section) PARAMS ((const char *, asection *));
      void (*layout_sections_again) PARAMS ((void));
 {
   bfd *input_bfd;
   asection *section;
+  asection **input_list, **list;
   Elf_Internal_Sym *local_syms, **all_local_syms;
-  unsigned int i, indx, bfd_count, sec_count;
+  unsigned int bfd_indx, bfd_count;
+  int top_id, top_index;
   struct elf32_hppa_link_hash_table *hplink;
+  bfd_size_type stub_group_size;
+  boolean stubs_always_before_branch;
   boolean stub_changed = 0;
+  boolean ret = 0;
 
   hplink = hppa_link_hash_table (info);
 
@@ -2260,27 +2445,169 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
   hplink->multi_subspace = multi_subspace;
   hplink->add_stub_section = add_stub_section;
   hplink->layout_sections_again = layout_sections_again;
+  stubs_always_before_branch = group_size < 0;
+  if (group_size < 0)
+    stub_group_size = -group_size;
+  else
+    stub_group_size = group_size;
+  if (stub_group_size == 1)
+    {
+      /* Default values.  */
+      stub_group_size = 8000000;
+      if (hplink->has_17bit_branch || hplink->multi_subspace)
+       stub_group_size = 250000;
+      if (hplink->has_12bit_branch)
+       stub_group_size = 7812;
+    }
 
-  /* Count the number of input BFDs and the total number of input sections.  */
-  for (input_bfd = info->input_bfds, bfd_count = 0, sec_count = 0;
+  /* Count the number of input BFDs and find the top input section id.  */
+  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
        input_bfd != NULL;
        input_bfd = input_bfd->link_next)
     {
       bfd_count += 1;
-      sec_count += input_bfd->section_count;
+      for (section = input_bfd->sections;
+          section != NULL;
+          section = section->next)
+       {
+         if (top_id < section->id)
+           top_id = section->id;
+       }
     }
 
-  hplink->stub_section_created
-    = (asection **) bfd_zmalloc (sizeof (asection *) * sec_count);
-  if (hplink->stub_section_created == NULL)
+  hplink->stub_group
+    = (struct map_stub *) bfd_zmalloc (sizeof (struct map_stub) * (top_id + 1));
+  if (hplink->stub_group == NULL)
     return false;
 
-#if ! LONG_BRANCH_PIC_IN_SHLIB
-  hplink->reloc_section_created
-    = (asection **) bfd_zmalloc (sizeof (asection *) * sec_count);
-  if (hplink->reloc_section_created == NULL)
-    goto error_ret_free_stub;
-#endif
+  /* Make a list of input sections for each output section included in
+     the link.
+
+     We can't use output_bfd->section_count here to find the top output
+     section index as some sections may have been removed, and
+     _bfd_strip_section_from_output doesn't renumber the indices.  */
+  for (section = output_bfd->sections, top_index = 0;
+       section != NULL;
+       section = section->next)
+    {
+      if (top_index < section->index)
+       top_index = section->index;
+    }
+
+  input_list
+    = (asection **) bfd_malloc (sizeof (asection *) * (top_index + 1));
+  if (input_list == NULL)
+    return false;
+
+  /* For sections we aren't interested in, mark their entries with a
+     value we can check later.  */
+  list = input_list + top_index;
+  do
+    *list = bfd_abs_section_ptr;
+  while (list-- != input_list);
+
+  for (section = output_bfd->sections;
+       section != NULL;
+       section = section->next)
+    {
+      if ((section->flags & SEC_CODE) != 0)
+       input_list[section->index] = NULL;
+    }
+
+  /* Now actually build the lists.  */
+  for (input_bfd = info->input_bfds;
+       input_bfd != NULL;
+       input_bfd = input_bfd->link_next)
+    {
+      for (section = input_bfd->sections;
+          section != NULL;
+          section = section->next)
+       {
+         if (section->output_section != NULL
+             && section->output_section->owner == output_bfd
+             && section->output_section->index <= top_index)
+           {
+             list = input_list + section->output_section->index;
+             if (*list != bfd_abs_section_ptr)
+               {
+                 /* Steal the link_sec pointer for our list.  */
+#define PREV_SEC(sec) (hplink->stub_group[(sec)->id].link_sec)
+                 /* This happens to make the list in reverse order,
+                    which is what we want.  */
+                 PREV_SEC (section) = *list;
+                 *list = section;
+               }
+           }
+       }
+    }
+
+  /* See whether we can group stub sections together.  Grouping stub
+     sections may result in fewer stubs.  More importantly, we need to
+     put all .init* and .fini* stubs at the beginning of the .init or
+     .fini output sections respectively, because glibc splits the
+     _init and _fini functions into multiple parts.  Putting a stub in
+     the middle of a function is not a good idea.  */
+  list = input_list + top_index;
+  do
+    {
+      asection *tail = *list;
+      if (tail == bfd_abs_section_ptr)
+       continue;
+      while (tail != NULL)
+       {
+         asection *curr;
+         asection *prev;
+         bfd_size_type total;
+
+         curr = tail;
+         if (tail->_cooked_size)
+           total = tail->_cooked_size;
+         else
+           total = tail->_raw_size;
+         while ((prev = PREV_SEC (curr)) != NULL
+                && ((total += curr->output_offset - prev->output_offset)
+                    < stub_group_size))
+           curr = prev;
+
+         /* OK, the size from the start of CURR to the end is less
+            than 250000 bytes and thus can be handled by one stub
+            section.  (or the tail section is itself larger than
+            250000 bytes, in which case we may be toast.)
+            We should really be keeping track of the total size of
+            stubs added here, as stubs contribute to the final output
+            section size.  That's a little tricky, and this way will
+            only break if stubs added total more than 12144 bytes, or
+            1518 long branch stubs.  It seems unlikely for more than
+            1518 different functions to be called, especially from
+            code only 250000 bytes long.  */
+         do
+           {
+             prev = PREV_SEC (tail);
+             /* Set up this stub group.  */
+             hplink->stub_group[tail->id].link_sec = curr;
+           }
+         while (tail != curr && (tail = prev) != NULL);
+
+         /* But wait, there's more!  Input sections up to 250000
+            bytes before the stub section can be handled by it too.  */
+         if (!stubs_always_before_branch)
+           {
+             total = 0;
+             while (prev != NULL
+                    && ((total += tail->output_offset - prev->output_offset)
+                        < stub_group_size))
+               {
+                 tail = prev;
+                 prev = PREV_SEC (tail);
+                 hplink->stub_group[tail->id].link_sec = curr;
+               }
+           }
+         tail = prev;
+       }
+    }
+  while (list-- != input_list);
+  free (input_list);
+#undef PREV_SEC
 
   /* We want to read in symbol extension records only once.  To do this
      we need to read in the local symbols in parallel and save them for
@@ -2289,18 +2616,18 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
     = (Elf_Internal_Sym **) bfd_zmalloc (sizeof (Elf_Internal_Sym *)
                                         * bfd_count);
   if (all_local_syms == NULL)
-    goto error_ret_free_reloc;
+    return false;
 
   /* Walk over all the input BFDs, swapping in local symbols.
      If we are creating a shared library, create hash entries for the
      export stubs.  */
-  for (input_bfd = info->input_bfds, indx = 0, sec_count = 0;
+  for (input_bfd = info->input_bfds, bfd_indx = 0;
        input_bfd != NULL;
-       input_bfd = input_bfd->link_next, indx++)
+       input_bfd = input_bfd->link_next, bfd_indx++)
     {
       Elf_Internal_Shdr *symtab_hdr;
       Elf_Internal_Sym *isym;
-      Elf32_External_Sym *ext_syms, *esym;
+      Elf32_External_Sym *ext_syms, *esym, *end_sy;
 
       /* We'll need the symbol table in a second.  */
       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
@@ -2315,7 +2642,7 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
        {
          goto error_ret_free_local;
        }
-      all_local_syms[indx] = local_syms;
+      all_local_syms[bfd_indx] = local_syms;
       ext_syms = (Elf32_External_Sym *)
        bfd_malloc (symtab_hdr->sh_info * sizeof (Elf32_External_Sym));
       if (ext_syms == NULL)
@@ -2336,28 +2663,52 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
       /* Swap the local symbols in.  */
       isym = local_syms;
       esym = ext_syms;
-      for (i = 0; i < symtab_hdr->sh_info; i++, esym++, isym++)
+      for (end_sy = esym + symtab_hdr->sh_info; esym < end_sy; esym++, isym++)
        bfd_elf32_swap_symbol_in (input_bfd, esym, isym);
 
       /* Now we can free the external symbols.  */
       free (ext_syms);
 
+#if ! LONG_BRANCH_PIC_IN_SHLIB
+      /* If this is a shared link, find all the stub reloc sections.  */
+      if (info->shared)
+       for (section = input_bfd->sections;
+            section != NULL;
+            section = section->next)
+         {
+           char *name;
+           asection *reloc_sec;
+
+           name = bfd_malloc (strlen (section->name)
+                              + sizeof STUB_SUFFIX
+                              + 5);
+           if (name == NULL)
+             return false;
+           sprintf (name, ".rela%s%s", section->name, STUB_SUFFIX);
+           reloc_sec = bfd_get_section_by_name (hplink->root.dynobj, name);
+           hplink->stub_group[section->id].reloc_sec = reloc_sec;
+           free (name);
+         }
+#endif
+
       if (info->shared && hplink->multi_subspace)
        {
-         unsigned int symndx;
+         struct elf_link_hash_entry **sym_hashes;
+         struct elf_link_hash_entry **end_hashes;
          unsigned int symcount;
 
          symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
                      - symtab_hdr->sh_info);
+         sym_hashes = elf_sym_hashes (input_bfd);
+         end_hashes = sym_hashes + symcount;
 
          /* Look through the global syms for functions;  We need to
             build export stubs for all globally visible functions.  */
-         for (symndx = 0; symndx < symcount; symndx++)
+         for (; sym_hashes < end_hashes; sym_hashes++)
            {
              struct elf32_hppa_link_hash_entry *hash;
 
-             hash = ((struct elf32_hppa_link_hash_entry *)
-                     elf_sym_hashes (input_bfd)[symndx]);
+             hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
 
              while (hash->elf.root.type == bfd_link_hash_indirect
                     || hash->elf.root.type == bfd_link_hash_warning)
@@ -2371,6 +2722,8 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
                   || hash->elf.root.type == bfd_link_hash_defweak)
                  && hash->elf.type == STT_FUNC
                  && hash->elf.root.u.def.section->output_section != NULL
+                 && (hash->elf.root.u.def.section->output_section->owner
+                     == output_bfd)
                  && hash->elf.root.u.def.section->owner == input_bfd
                  && (hash->elf.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
                  && !(hash->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL)
@@ -2387,10 +2740,7 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
                                                      false, false);
                  if (stub_entry == NULL)
                    {
-                     stub_entry = hppa_add_stub (stub_name,
-                                                 sec,
-                                                 sec_count + sec->index,
-                                                 info);
+                     stub_entry = hppa_add_stub (stub_name, sec, hplink);
                      if (!stub_entry)
                        goto error_ret_free_local;
 
@@ -2408,7 +2758,6 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
                    }
                }
            }
-         sec_count += input_bfd->section_count;
        }
     }
 
@@ -2416,9 +2765,9 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
     {
       asection *stub_sec;
 
-      for (input_bfd = info->input_bfds, indx = 0, sec_count = 0;
+      for (input_bfd = info->input_bfds, bfd_indx = 0;
           input_bfd != NULL;
-          input_bfd = input_bfd->link_next, indx++)
+          input_bfd = input_bfd->link_next, bfd_indx++)
        {
          Elf_Internal_Shdr *symtab_hdr;
 
@@ -2427,12 +2776,12 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
          if (symtab_hdr->sh_info == 0)
            continue;
 
-         local_syms = all_local_syms[indx];
+         local_syms = all_local_syms[bfd_indx];
 
          /* Walk over each section attached to the input bfd.  */
          for (section = input_bfd->sections;
               section != NULL;
-              section = section->next, sec_count++)
+              section = section->next)
            {
              Elf_Internal_Shdr *input_rel_hdr;
              Elf32_External_Rela *external_relocs, *erelaend, *erela;
@@ -2444,6 +2793,12 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
                  || section->reloc_count == 0)
                continue;
 
+             /* If this section is a link-once section that will be
+                discarded, then don't create any stubs.  */
+             if (section->output_section == NULL
+                 || section->output_section->owner != output_bfd)
+               continue;
+
              /* Allocate space for the external relocations.  */
              external_relocs
                = ((Elf32_External_Rela *)
@@ -2500,6 +2855,7 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
                  bfd_vma destination;
                  struct elf32_hppa_link_hash_entry *hash;
                  char *stub_name;
+                 const asection *id_sec;
 
                  r_type = ELF32_R_TYPE (irela->r_info);
                  r_indx = ELF32_R_SYM (irela->r_info);
@@ -2587,8 +2943,11 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
                  if (stub_type == hppa_stub_none)
                    continue;
 
+                 /* Support for grouping stub sections.  */
+                 id_sec = hplink->stub_group[section->id].link_sec;
+
                  /* Get the name of this stub.  */
-                 stub_name = hppa_stub_name (section, sym_sec, hash, irela);
+                 stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
                  if (!stub_name)
                    goto error_ret_free_internal;
 
@@ -2602,8 +2961,7 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
                      continue;
                    }
 
-                 stub_entry = hppa_add_stub (stub_name, section,
-                                             sec_count, info);
+                 stub_entry = hppa_add_stub (stub_name, section, hplink);
                  if (stub_entry == NULL)
                    {
                      free (stub_name);
@@ -2635,58 +2993,50 @@ elf32_hppa_size_stubs (stub_bfd, multi_subspace, info,
 
       /* OK, we've added some stubs.  Find out the new size of the
         stub sections.  */
-      hplink->offset = (bfd_vma *)
-       bfd_realloc (hplink->offset,
-                    hplink->stub_bfd->section_count * sizeof (bfd_vma));
-      if (hplink->offset == NULL)
-       goto error_ret_free_local;
+      for (stub_sec = hplink->stub_bfd->sections;
+          stub_sec != NULL;
+          stub_sec = stub_sec->next)
+       {
+         stub_sec->_raw_size = 0;
+         stub_sec->_cooked_size = 0;
+       }
+#if ! LONG_BRANCH_PIC_IN_SHLIB
+      {
+       int i;
 
-      memset (hplink->offset, 0,
-             hplink->stub_bfd->section_count * sizeof (bfd_vma));
+       for (i = top_id; i >= 0; --i)
+         {
+           /* This will probably hit the same section many times..  */
+           stub_sec = hplink->stub_group[i].reloc_sec;
+           if (stub_sec != NULL)
+             {
+               stub_sec->_raw_size = 0;
+               stub_sec->_cooked_size = 0;
+             }
+         }
+      }
+#endif
 
       bfd_hash_traverse (&hplink->stub_hash_table,
                         hppa_size_one_stub,
                         hplink);
 
-      for (stub_sec = hplink->stub_bfd->sections;
-          stub_sec != NULL;
-          stub_sec = stub_sec->next)
-       {
-         bfd_set_section_size (hplink->stub_bfd, stub_sec,
-                               hplink->offset[stub_sec->index]);
-       }
       /* Ask the linker to do its stuff.  */
       (*hplink->layout_sections_again) ();
       stub_changed = 0;
     }
 
-  /* We're done with the local symbols, free them.  */
-  for (i = 0; i < bfd_count; i++)
-    if (all_local_syms[i])
-      free (all_local_syms[i]);
-  free (all_local_syms);
-#if ! LONG_BRANCH_PIC_IN_SHLIB
-  free (hplink->reloc_section_created);
-#endif
-  free (hplink->stub_section_created);
-  return true;
+  ret = 1;
 
  error_ret_free_local:
-  for (i = 0; i < bfd_count; i++)
-    if (all_local_syms[i])
-      free (all_local_syms[i]);
+  while (bfd_count-- > 0)
+    if (all_local_syms[bfd_count])
+      free (all_local_syms[bfd_count]);
   free (all_local_syms);
 
- error_ret_free_reloc:
-#if ! LONG_BRANCH_PIC_IN_SHLIB
-  free (hplink->reloc_section_created);
- error_ret_free_stub:
-#endif
-  free (hplink->stub_section_created);
-  return false;
+  return ret;
 }
 
-
 /* For a final link, this function is called after we have sized the
    stubs to provide a value for __gp.  */
 
@@ -2695,11 +3045,13 @@ elf32_hppa_set_gp (abfd, info)
      bfd *abfd;
      struct bfd_link_info *info;
 {
+  struct elf32_hppa_link_hash_table *hplink;
   struct elf_link_hash_entry *h;
   asection *sec;
   bfd_vma gp_val;
 
-  h = elf_link_hash_lookup (elf_hash_table (info), "$global$",
+  hplink = hppa_link_hash_table (info);
+  h = elf_link_hash_lookup (&hplink->root, "$global$",
                            false, false, false);
 
   if (h != NULL && h->root.type == bfd_link_hash_defined)
@@ -2709,32 +3061,51 @@ elf32_hppa_set_gp (abfd, info)
     }
   else
     {
-      /* If $global$ isn't defined, we make one up ourselves
-        from the start of .data, .plt, or .got.  */
-      struct elf32_hppa_link_hash_table *hplink;
-
-      hplink = hppa_link_hash_table (info);
-      gp_val = 0;
-      sec = bfd_get_section_by_name (abfd, ".data");
-      if (sec == NULL)
-       sec = hplink->splt;
-      if (sec == NULL)
-       sec = hplink->sgot;
-      if (sec == NULL)
-       {
-         (*info->callbacks->undefined_symbol)
-           (info, "$global$", abfd, NULL, 0, true);
-         return false;
+      /* Choose to point our LTP at, in this order, one of .plt, .got,
+        or .data, if these sections exist.  In the case of choosing
+        .plt try to make the LTP ideal for addressing anywhere in the
+        .plt or .got with a 14 bit signed offset.  Typically, the end
+        of the .plt is the start of the .got, so choose .plt + 0x2000
+        if either the .plt or .got is larger than 0x2000.  If both
+        the .plt and .got are smaller than 0x2000, choose the end of
+        the .plt section.  */
+
+      sec = hplink->splt;
+      if (sec != NULL)
+       {
+         gp_val = sec->_raw_size;
+         if (gp_val > 0x2000
+             || (hplink->sgot && hplink->sgot->_raw_size > 0x2000))
+           {
+             gp_val = 0x2000;
+           }
+       }
+      else
+       {
+         gp_val = 0;
+         sec = hplink->sgot;
+         if (sec != NULL)
+           {
+             /* We know we don't have a .plt.  If .got is large,
+                offset our LTP.  */
+             if (sec->_raw_size > 0x2000)
+               gp_val = 0x2000;
+           }
+         else
+           {
+             /* No .plt or .got.  Who cares what the LTP is?  */
+             sec = bfd_get_section_by_name (abfd, ".data");
+           }
        }
     }
 
-  elf_gp (abfd) = (gp_val
-                  + sec->output_section->vma
-                  + sec->output_offset);
+  if (sec != NULL)
+    gp_val += sec->output_section->vma + sec->output_offset;
+
+  elf_gp (abfd) = gp_val;
   return true;
 }
 
-
 /* Build all the stubs associated with the current output file.  The
    stubs are kept in a hash table attached to the main linker hash
    table.  We also set up the .plt entries for statically linked PIC
@@ -2755,36 +3126,33 @@ elf32_hppa_build_stubs (info)
        stub_sec != NULL;
        stub_sec = stub_sec->next)
     {
-      unsigned int size;
+      size_t size;
 
       /* Allocate memory to hold the linker stubs.  */
-      size = bfd_section_size (hplink->stub_bfd, stub_sec);
+      size = stub_sec->_raw_size;
       stub_sec->contents = (unsigned char *) bfd_zalloc (hplink->stub_bfd,
                                                         size);
       if (stub_sec->contents == NULL && size != 0)
        return false;
+      stub_sec->_raw_size = 0;
     }
 
   /* Build the stubs as directed by the stub hash table.  */
-  memset (hplink->offset, 0,
-         hplink->stub_bfd->section_count * sizeof (bfd_vma));
-
   table = &hplink->stub_hash_table;
   bfd_hash_traverse (table, hppa_build_one_stub, info);
 
   return true;
 }
 
-
 /* Perform a relocation as part of a final link.  */
 
 static bfd_reloc_status_type
-final_link_relocate (input_section, contents, rel, value, info, sym_sec, h)
+final_link_relocate (input_section, contents, rel, value, hplink, sym_sec, h)
      asection *input_section;
      bfd_byte *contents;
      const Elf_Internal_Rela *rel;
      bfd_vma value;
-     struct bfd_link_info *info;
+     struct elf32_hppa_link_hash_table *hplink;
      asection *sym_sec;
      struct elf32_hppa_link_hash_entry *h;
 {
@@ -2819,13 +3187,19 @@ final_link_relocate (input_section, contents, rel, value, info, sym_sec, h)
     case R_PARISC_PCREL22F:
       /* If this is a call to a function defined in another dynamic
         library, or if it is a call to a PIC function in the same
-        object, then find the import stub in the stub hash.  */
+        object, or if this is a shared link and it is a call to a
+        weak symbol which may or may not be in the same object, then
+        find the import stub in the stub hash.  */
       if (sym_sec == NULL
          || sym_sec->output_section == NULL
-         || (h != NULL && h->pic_call))
+         || (h != NULL &&
+             (h->pic_call
+              || (h->elf.root.type == bfd_link_hash_defweak
+                  && h->elf.dynindx != -1
+                  && h->elf.plt.offset != (bfd_vma) -1))))
        {
          stub_entry = hppa_get_stub_entry (input_section, sym_sec,
-                                           h, rel, info);
+                                           h, rel, hplink);
          if (stub_entry != NULL)
            {
              value = (stub_entry->stub_offset
@@ -2874,7 +3248,7 @@ final_link_relocate (input_section, contents, rel, value, info, sym_sec, h)
              == (((int) OP_ADDIL << 26) | (27 << 21)))
            {
              insn &= ~ (0x1f << 21);
-#if 1 /* debug them */
+#if 1 /* debug them */
              (*_bfd_error_handler)
                (_("%s(%s+0x%lx): fixing %s"),
                 bfd_get_filename (input_bfd),
@@ -2887,7 +3261,7 @@ final_link_relocate (input_section, contents, rel, value, info, sym_sec, h)
 
          break;
        }
-      /* Fall thru */
+      /* Fall thru */
 
     case R_PARISC_DLTIND21L:
     case R_PARISC_DLTIND14R:
@@ -2902,6 +3276,7 @@ final_link_relocate (input_section, contents, rel, value, info, sym_sec, h)
   switch (r_type)
     {
     case R_PARISC_DIR32:
+    case R_PARISC_DIR14F:
     case R_PARISC_DIR17F:
     case R_PARISC_PCREL17C:
     case R_PARISC_PCREL14F:
@@ -2960,7 +3335,7 @@ final_link_relocate (input_section, contents, rel, value, info, sym_sec, h)
       if (value + addend + max_branch_offset >= 2*max_branch_offset)
        {
          stub_entry = hppa_get_stub_entry (input_section, sym_sec,
-                                           h, rel, info);
+                                           h, rel, hplink);
          if (stub_entry == NULL)
            return bfd_reloc_notsupported;
 
@@ -3017,11 +3392,10 @@ final_link_relocate (input_section, contents, rel, value, info, sym_sec, h)
   insn = hppa_rebuild_insn (insn, val, r_format);
 
   /* Update the instruction word.  */
-  bfd_put_32 (input_bfd, (unsigned int) insn, hit_data);
+  bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
   return bfd_reloc_ok;
 }
 
-
 /* Relocate an HPPA ELF section.  */
 
 static boolean
@@ -3046,9 +3420,9 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
 
   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
 
-  dynobj = elf_hash_table (info)->dynobj;
-  local_got_offsets = elf_local_got_offsets (input_bfd);
   hplink = hppa_link_hash_table (info);
+  dynobj = hplink->root.dynobj;
+  local_got_offsets = elf_local_got_offsets (input_bfd);
   sreloc = NULL;
 
   rel = relocs;
@@ -3064,7 +3438,7 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
       bfd_vma relocation;
       bfd_reloc_status_type r;
       const char *sym_name;
-      boolean pltrel;
+      boolean plabel;
 
       r_type = ELF32_R_TYPE (rel->r_info);
       if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
@@ -3155,9 +3529,9 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
        }
 
       /* Do any required modifications to the relocation value, and
-         determine what types of dynamic info we need to output, if
-         any.  */
-      pltrel = 0;
+        determine what types of dynamic info we need to output, if
+        any.  */
+      plabel = 0;
       switch (r_type)
        {
        case R_PARISC_DLTIND14F:
@@ -3172,7 +3546,7 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
              off = h->elf.got.offset;
              BFD_ASSERT (off != (bfd_vma) -1);
 
-             if (! elf_hash_table (info)->dynamic_sections_created
+             if (! hplink->root.dynamic_sections_created
                  || (info->shared
                      && (info->symbolic || h->elf.dynindx == -1)
                      && (h->elf.elf_link_hash_flags
@@ -3224,7 +3598,7 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
 
                  if (info->shared)
                    {
-                     /* Output a dynamic SEGREL32 relocation for this
+                     /* Output a dynamic *ABS* relocation for this
                         GOT entry.  In this case it is relative to
                         the base of the object because the symbol
                         index is zero.  */
@@ -3234,7 +3608,7 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
                      outrel.r_offset = (off
                                         + hplink->sgot->output_offset
                                         + hplink->sgot->output_section->vma);
-                     outrel.r_info = ELF32_R_INFO (0, R_PARISC_SEGREL32);
+                     outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
                      outrel.r_addend = relocation;
                      bfd_elf32_swap_reloca_out (output_bfd, &outrel,
                                                 ((Elf32_External_Rela *)
@@ -3257,27 +3631,84 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
        case R_PARISC_PLABEL14R:
        case R_PARISC_PLABEL21L:
        case R_PARISC_PLABEL32:
-         /* If we have a global symbol with a PLT slot, then redirect
-             this relocation to it.  */
-         if (h != NULL
-             && h->elf.dynindx != -1
-             && h->elf.plt.offset != (bfd_vma) -1)
+         if (hplink->root.dynamic_sections_created)
            {
-             /* PLABELs contain function pointers.  Relocation is
-                to the entry for the function in the .plt.  The
-                magic +2 offset signals to $$dyncall that the
-                function pointer is in the .plt and thus has a gp
-                pointer too.  */
-             relocation = (h->elf.plt.offset
-                           + hplink->splt->output_offset
-                           + hplink->splt->output_section->vma
-                           + 2);
-             pltrel = 1;
+             bfd_vma off;
+
+             /* If we have a global symbol with a PLT slot, then
+                redirect this relocation to it.  */
+             if (h != NULL)
+               {
+                 off = h->elf.plt.offset;
+               }
+             else
+               {
+                 int indx;
+
+                 indx = r_symndx + symtab_hdr->sh_info;
+                 off = local_got_offsets[indx];
+
+                 /* As for the local .got entry case, we use the last
+                    bit to record whether we've already initialised
+                    this local .plt entry.  */
+                 if ((off & 1) != 0)
+                   off &= ~1;
+                 else
+                   {
+                     bfd_put_32 (output_bfd,
+                                 relocation,
+                                 hplink->splt->contents + off);
+                     bfd_put_32 (output_bfd,
+                                 elf_gp (hplink->splt->output_section->owner),
+                                 hplink->splt->contents + off + 4);
+
+                     if (info->shared)
+                       {
+                         /* Output a dynamic IPLT relocation for this
+                            PLT entry.  */
+                         Elf_Internal_Rela outrel;
+                         asection *srelplt = hplink->srelplt;
+
+                         outrel.r_offset = (off
+                                            + hplink->splt->output_offset
+                                            + hplink->splt->output_section->vma);
+                         outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
+                         outrel.r_addend = relocation;
+                         bfd_elf32_swap_reloca_out (output_bfd, &outrel,
+                                                    ((Elf32_External_Rela *)
+                                                     srelplt->contents
+                                                     + srelplt->reloc_count));
+                         ++srelplt->reloc_count;
+                       }
+
+                     local_got_offsets[indx] |= 1;
+                   }
+               }
+
+             BFD_ASSERT (off < (bfd_vma) -2);
+
+             /* PLABELs contain function pointers.  Relocation is to
+                the entry for the function in the .plt.  The magic +2
+                offset signals to $$dyncall that the function pointer
+                is in the .plt and thus has a gp pointer too.
+                Exception:  Undefined PLABELs should have a value of
+                zero.  */
+             if (h == NULL
+                 || (h->elf.root.type != bfd_link_hash_undefweak
+                     && h->elf.root.type != bfd_link_hash_undefined))
+               {
+                 relocation = (off
+                               + hplink->splt->output_offset
+                               + hplink->splt->output_section->vma
+                               + 2);
+               }
+             plabel = 1;
            }
          /* Fall through and possibly emit a dynamic relocation.  */
 
        case R_PARISC_DIR17F:
        case R_PARISC_DIR17R:
+       case R_PARISC_DIR14F:
        case R_PARISC_DIR14R:
        case R_PARISC_DIR21L:
        case R_PARISC_DPREL14F:
@@ -3296,7 +3727,7 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
              && (is_absolute_reloc (r_type)
                  || ((!info->symbolic
                       || (h != NULL
-                          && ((h->elf.elf_link_hash_flags 
+                          && ((h->elf.elf_link_hash_flags
                                & ELF_LINK_HASH_DEF_REGULAR) == 0
                               || h->elf.root.type == bfd_link_hash_defweak)))
                      && (h == NULL || h->elf.dynindx != -1)))
@@ -3332,7 +3763,7 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
                  bfd_vma off;
 
                  off = (_bfd_stab_section_offset
-                        (output_bfd, &elf_hash_table (info)->stab_info,
+                        (output_bfd, &hplink->root.stab_info,
                          input_section,
                          &elf_section_data (input_section)->stab_info,
                          rel->r_offset));
@@ -3348,9 +3779,10 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
                {
                  memset (&outrel, 0, sizeof (outrel));
                }
-             else if (!pltrel
-                      && h != NULL
-                      && ((!info->symbolic && h->elf.dynindx != -1)
+             else if (h != NULL
+                      && h->elf.dynindx != -1
+                      && (plabel
+                          || !info->symbolic
                           || (h->elf.elf_link_hash_flags
                               & ELF_LINK_HASH_DEF_REGULAR) == 0))
                {
@@ -3363,7 +3795,14 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
                  /* Add the absolute offset of the symbol.  */
                  outrel.r_addend += relocation;
 
-                 if (sym_sec != NULL
+                 /* Global plabels need to be processed by the
+                    dynamic linker so that functions have at most one
+                    fptr.  For this reason, we need to differentiate
+                    between global and local plabels, which we do by
+                    providing the function symbol for a global plabel
+                    reloc, and no symbol for local plabels.  */
+                 if (! plabel
+                     && sym_sec != NULL
                      && sym_sec->output_section != NULL
                      && ! bfd_is_abs_section (sym_sec))
                    {
@@ -3391,7 +3830,7 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
        }
 
       r = final_link_relocate (input_section, contents, rel, relocation,
-                              info, sym_sec, h);
+                              hplink, sym_sec, h);
 
       if (r == bfd_reloc_ok)
        continue;
@@ -3433,7 +3872,6 @@ elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
   return true;
 }
 
-
 /* Finish up dynamic symbol handling.  We set the contents of various
    dynamic sections here.  */
 
@@ -3448,28 +3886,19 @@ elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
   bfd *dynobj;
 
   hplink = hppa_link_hash_table (info);
-  dynobj = elf_hash_table (info)->dynobj;
-
-  /* Millicode symbols should not be put in the dynamic
-     symbol table under any circumstances.  */
-  if (ELF_ST_TYPE (sym->st_info) == STT_PARISC_MILLI)
-    h->dynindx = -1;
+  dynobj = hplink->root.dynobj;
 
   if (h->plt.offset != (bfd_vma) -1)
     {
       bfd_vma value;
-      Elf_Internal_Rela rel;
 
       /* This symbol has an entry in the procedure linkage table.  Set
         it up.
 
         The format of a plt entry is
-        <funcaddr> <__gp>.  */
-
-      /* We do not actually care about the value in the PLT entry if
-        we are creating a shared library and the symbol is still
-        undefined;  We create a dynamic relocation to fill in the
-        correct value.  */
+        <funcaddr>
+        <__gp>
+      */
       value = 0;
       if (h->root.type == bfd_link_hash_defined
          || h->root.type == bfd_link_hash_defweak)
@@ -3480,21 +3909,36 @@ elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
                      + h->root.u.def.section->output_section->vma);
        }
 
-      bfd_put_32 (hplink->splt->owner, value,
-                 hplink->splt->contents + h->plt.offset);
-      value = elf_gp (hplink->splt->output_section->owner);
-      bfd_put_32 (hplink->splt->owner, value,
-                 hplink->splt->contents + h->plt.offset + 4);
-
-      if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call
-         && h->dynindx != -1)
+      if (! ((struct elf32_hppa_link_hash_entry *) h)->pic_call)
        {
+         Elf_Internal_Rela rel;
+
          /* Create a dynamic IPLT relocation for this entry.  */
          rel.r_offset = (h->plt.offset
                          + hplink->splt->output_offset
                          + hplink->splt->output_section->vma);
-         rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
-         rel.r_addend = 0;
+         if (! ((struct elf32_hppa_link_hash_entry *) h)->plt_abs
+             && h->dynindx != -1)
+           {
+             /* To support lazy linking, the function pointer is
+                initialised to point to a special stub stored at the
+                end of the .plt.  This is only done for plt entries
+                with a non-*ABS* dynamic relocation.  */
+             value = (hplink->splt->output_offset
+                      + hplink->splt->output_section->vma
+                      + hplink->splt->_raw_size
+                      - sizeof (plt_stub)
+                      + PLT_STUB_ENTRY);
+             rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
+             rel.r_addend = 0;
+           }
+         else
+           {
+             /* This symbol has been marked to become local, and is
+                used by a plabel so must be kept in the .plt.  */
+             rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
+             rel.r_addend = value;
+           }
 
          bfd_elf32_swap_reloca_out (hplink->splt->output_section->owner,
                                     &rel,
@@ -3504,6 +3948,20 @@ elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
          hplink->srelplt->reloc_count++;
        }
 
+      bfd_put_32 (hplink->splt->owner,
+                 value,
+                 hplink->splt->contents + h->plt.offset);
+      bfd_put_32 (hplink->splt->owner,
+                 elf_gp (hplink->splt->output_section->owner),
+                 hplink->splt->contents + h->plt.offset + 4);
+      if (PLABEL_PLT_ENTRY_SIZE != PLT_ENTRY_SIZE
+         && ((struct elf32_hppa_link_hash_entry *) h)->plabel
+         && h->dynindx != -1)
+       {
+         memset (hplink->splt->contents + h->plt.offset + 8,
+                 0, PLABEL_PLT_ENTRY_SIZE - PLT_ENTRY_SIZE);
+       }
+
       if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
        {
          /* Mark the symbol as undefined, rather than as defined in
@@ -3528,12 +3986,12 @@ elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
         of a version file, we just want to emit a RELATIVE reloc.
         The entry in the global offset table will already have been
         initialized in the relocate_section function.  */
-      if (! elf_hash_table (info)->dynamic_sections_created
+      if (! hplink->root.dynamic_sections_created
          || (info->shared
              && (info->symbolic || h->dynindx == -1)
              && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
        {
-         rel.r_info = ELF32_R_INFO (0, R_PARISC_SEGREL32);
+         rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
          rel.r_addend = (h->root.u.def.value
                          + h->root.u.def.section->output_offset
                          + h->root.u.def.section->output_section->vma);
@@ -3589,7 +4047,6 @@ elf32_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
   return true;
 }
 
-
 /* Finish up the dynamic sections.  */
 
 static boolean
@@ -3601,12 +4058,12 @@ elf32_hppa_finish_dynamic_sections (output_bfd, info)
   struct elf32_hppa_link_hash_table *hplink;
   asection *sdyn;
 
-  dynobj = elf_hash_table (info)->dynobj;
   hplink = hppa_link_hash_table (info);
+  dynobj = hplink->root.dynobj;
 
   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
 
-  if (elf_hash_table (info)->dynamic_sections_created)
+  if (hplink->root.dynamic_sections_created)
     {
       Elf32_External_Dyn *dyncon, *dynconend;
 
@@ -3646,32 +4103,82 @@ elf32_hppa_finish_dynamic_sections (output_bfd, info)
                dyn.d_un.d_val = s->_raw_size;
              bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
              break;
+
+           case DT_INIT:
+           case DT_FINI:
+             {
+               struct elf_link_hash_entry *h;
+               const char *funcname;
+
+               if (dyn.d_tag == DT_INIT)
+                 funcname = info->init_function;
+               else
+                 funcname = info->fini_function;
+
+               h = elf_link_hash_lookup (&hplink->root, funcname,
+                                         false, false, false);
+
+               /* This is a function pointer.  The magic +2 offset
+                  signals to $$dyncall that the function pointer
+                  is in the .plt and thus has a gp pointer too.  */
+               dyn.d_un.d_ptr = (h->plt.offset
+                                 + hplink->splt->output_offset
+                                 + hplink->splt->output_section->vma
+                                 + 2);
+               bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
+               break;
+             }
            }
        }
     }
 
-  /* Fill in the first entry in the global offset table.
-     We use it to point to our dynamic section, if we have one.  */
   if (hplink->sgot->_raw_size != 0)
     {
+      /* Fill in the first entry in the global offset table.
+        We use it to point to our dynamic section, if we have one.  */
       bfd_put_32 (output_bfd,
                  (sdyn != NULL
                   ? sdyn->output_section->vma + sdyn->output_offset
                   : (bfd_vma) 0),
                  hplink->sgot->contents);
 
+      /* The second entry is reserved for use by the dynamic linker.  */
+      memset (hplink->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
+
       /* Set .got entry size.  */
-      elf_section_data (hplink->sgot->output_section)->this_hdr.sh_entsize = 4;
+      elf_section_data (hplink->sgot->output_section)
+       ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
     }
 
-  /* Set plt entry size.  */
   if (hplink->splt->_raw_size != 0)
-    elf_section_data (hplink->splt->output_section)->this_hdr.sh_entsize = 8;
+    {
+      /* Set plt entry size.  */
+      elf_section_data (hplink->splt->output_section)
+       ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
+
+      if (hplink->need_plt_stub)
+       {
+         /* Set up the .plt stub.  */
+         memcpy (hplink->splt->contents
+                 + hplink->splt->_raw_size - sizeof (plt_stub),
+                 plt_stub, sizeof (plt_stub));
+
+         if ((hplink->splt->output_offset
+              + hplink->splt->output_section->vma
+              + hplink->splt->_raw_size)
+             != (hplink->sgot->output_offset
+                 + hplink->sgot->output_section->vma))
+           {
+             (*_bfd_error_handler)
+               (_(".got section not immediately after .plt section"));
+             return false;
+           }
+       }
+    }
 
   return true;
 }
 
-
 /* Called when writing out an object file to decide the type of a
    symbol.  */
 static int
@@ -3685,7 +4192,6 @@ elf32_hppa_elf_get_symbol_type (elf_sym, type)
     return type;
 }
 
-
 /* Misc BFD support code.  */
 #define bfd_elf32_bfd_is_local_label_name    elf_hppa_is_local_label_name
 #define bfd_elf32_bfd_reloc_type_lookup             elf_hppa_reloc_type_lookup
@@ -3701,6 +4207,7 @@ elf32_hppa_elf_get_symbol_type (elf_sym, type)
 #define elf_backend_create_dynamic_sections  elf32_hppa_create_dynamic_sections
 #define elf_backend_fake_sections           elf_hppa_fake_sections
 #define elf_backend_relocate_section        elf32_hppa_relocate_section
+#define elf_backend_hide_symbol                     elf32_hppa_hide_symbol
 #define elf_backend_finish_dynamic_symbol    elf32_hppa_finish_dynamic_symbol
 #define elf_backend_finish_dynamic_sections  elf32_hppa_finish_dynamic_sections
 #define elf_backend_size_dynamic_sections    elf32_hppa_size_dynamic_sections
@@ -3715,7 +4222,7 @@ elf32_hppa_elf_get_symbol_type (elf_sym, type)
 #define elf_backend_want_got_plt            0
 #define elf_backend_plt_readonly            0
 #define elf_backend_want_plt_sym            0
-#define elf_backend_got_header_size         4
+#define elf_backend_got_header_size         8
 
 #define TARGET_BIG_SYM         bfd_elf32_hppa_vec
 #define TARGET_BIG_NAME                "elf32-hppa"
This page took 0.056428 seconds and 4 git commands to generate.