Back to index

cell-binutils  2.17cvs20070401
elf64-hppa.c
Go to the documentation of this file.
00001 /* Support for HPPA 64-bit ELF
00002    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
00003    Free Software Foundation, Inc.
00004 
00005    This file is part of BFD, the Binary File Descriptor library.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00020 
00021 #include "alloca-conf.h"
00022 #include "bfd.h"
00023 #include "sysdep.h"
00024 #include "libbfd.h"
00025 #include "elf-bfd.h"
00026 #include "elf/hppa.h"
00027 #include "libhppa.h"
00028 #include "elf64-hppa.h"
00029 #define ARCH_SIZE           64
00030 
00031 #define PLT_ENTRY_SIZE 0x10
00032 #define DLT_ENTRY_SIZE 0x8
00033 #define OPD_ENTRY_SIZE 0x20
00034 
00035 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
00036 
00037 /* The stub is supposed to load the target address and target's DP
00038    value out of the PLT, then do an external branch to the target
00039    address.
00040 
00041    LDD PLTOFF(%r27),%r1
00042    BVE (%r1)
00043    LDD PLTOFF+8(%r27),%r27
00044 
00045    Note that we must use the LDD with a 14 bit displacement, not the one
00046    with a 5 bit displacement.  */
00047 static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
00048                        0x53, 0x7b, 0x00, 0x00 };
00049 
00050 struct elf64_hppa_dyn_hash_entry
00051 {
00052   struct bfd_hash_entry root;
00053 
00054   /* Offsets for this symbol in various linker sections.  */
00055   bfd_vma dlt_offset;
00056   bfd_vma plt_offset;
00057   bfd_vma opd_offset;
00058   bfd_vma stub_offset;
00059 
00060   /* The symbol table entry, if any, that this was derived from.  */
00061   struct elf_link_hash_entry *h;
00062 
00063   /* The index of the (possibly local) symbol in the input bfd and its
00064      associated BFD.  Needed so that we can have relocs against local
00065      symbols in shared libraries.  */
00066   long sym_indx;
00067   bfd *owner;
00068 
00069   /* Dynamic symbols may need to have two different values.  One for
00070      the dynamic symbol table, one for the normal symbol table.
00071 
00072      In such cases we store the symbol's real value and section
00073      index here so we can restore the real value before we write
00074      the normal symbol table.  */
00075   bfd_vma st_value;
00076   int st_shndx;
00077 
00078   /* Used to count non-got, non-plt relocations for delayed sizing
00079      of relocation sections.  */
00080   struct elf64_hppa_dyn_reloc_entry
00081   {
00082     /* Next relocation in the chain.  */
00083     struct elf64_hppa_dyn_reloc_entry *next;
00084 
00085     /* The type of the relocation.  */
00086     int type;
00087 
00088     /* The input section of the relocation.  */
00089     asection *sec;
00090 
00091     /* The index of the section symbol for the input section of
00092        the relocation.  Only needed when building shared libraries.  */
00093     int sec_symndx;
00094 
00095     /* The offset within the input section of the relocation.  */
00096     bfd_vma offset;
00097 
00098     /* The addend for the relocation.  */
00099     bfd_vma addend;
00100 
00101   } *reloc_entries;
00102 
00103   /* Nonzero if this symbol needs an entry in one of the linker
00104      sections.  */
00105   unsigned want_dlt;
00106   unsigned want_plt;
00107   unsigned want_opd;
00108   unsigned want_stub;
00109 };
00110 
00111 struct elf64_hppa_dyn_hash_table
00112 {
00113   struct bfd_hash_table root;
00114 };
00115 
00116 struct elf64_hppa_link_hash_table
00117 {
00118   struct elf_link_hash_table root;
00119 
00120   /* Shortcuts to get to the various linker defined sections.  */
00121   asection *dlt_sec;
00122   asection *dlt_rel_sec;
00123   asection *plt_sec;
00124   asection *plt_rel_sec;
00125   asection *opd_sec;
00126   asection *opd_rel_sec;
00127   asection *other_rel_sec;
00128 
00129   /* Offset of __gp within .plt section.  When the PLT gets large we want
00130      to slide __gp into the PLT section so that we can continue to use
00131      single DP relative instructions to load values out of the PLT.  */
00132   bfd_vma gp_offset;
00133 
00134   /* Note this is not strictly correct.  We should create a stub section for
00135      each input section with calls.  The stub section should be placed before
00136      the section with the call.  */
00137   asection *stub_sec;
00138 
00139   bfd_vma text_segment_base;
00140   bfd_vma data_segment_base;
00141 
00142   struct elf64_hppa_dyn_hash_table dyn_hash_table;
00143 
00144   /* We build tables to map from an input section back to its
00145      symbol index.  This is the BFD for which we currently have
00146      a map.  */
00147   bfd *section_syms_bfd;
00148 
00149   /* Array of symbol numbers for each input section attached to the
00150      current BFD.  */
00151   int *section_syms;
00152 };
00153 
00154 #define elf64_hppa_hash_table(p) \
00155   ((struct elf64_hppa_link_hash_table *) ((p)->hash))
00156 
00157 typedef struct bfd_hash_entry *(*new_hash_entry_func)
00158   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
00159 
00160 static struct bfd_hash_entry *elf64_hppa_new_dyn_hash_entry
00161   PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
00162           const char *string));
00163 static struct bfd_link_hash_table *elf64_hppa_hash_table_create
00164   PARAMS ((bfd *abfd));
00165 static struct elf64_hppa_dyn_hash_entry *elf64_hppa_dyn_hash_lookup
00166   PARAMS ((struct elf64_hppa_dyn_hash_table *table, const char *string,
00167           bfd_boolean create, bfd_boolean copy));
00168 static void elf64_hppa_dyn_hash_traverse
00169   PARAMS ((struct elf64_hppa_dyn_hash_table *table,
00170           bfd_boolean (*func) (struct elf64_hppa_dyn_hash_entry *, PTR),
00171           PTR info));
00172 
00173 static const char *get_dyn_name
00174   PARAMS ((bfd *, struct elf_link_hash_entry *,
00175           const Elf_Internal_Rela *, char **, size_t *));
00176 
00177 /* This must follow the definitions of the various derived linker
00178    hash tables and shared functions.  */
00179 #include "elf-hppa.h"
00180 
00181 static bfd_boolean elf64_hppa_object_p
00182   PARAMS ((bfd *));
00183 
00184 static void elf64_hppa_post_process_headers
00185   PARAMS ((bfd *, struct bfd_link_info *));
00186 
00187 static bfd_boolean elf64_hppa_create_dynamic_sections
00188   PARAMS ((bfd *, struct bfd_link_info *));
00189 
00190 static bfd_boolean elf64_hppa_adjust_dynamic_symbol
00191   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
00192 
00193 static bfd_boolean elf64_hppa_mark_milli_and_exported_functions
00194   PARAMS ((struct elf_link_hash_entry *, PTR));
00195 
00196 static bfd_boolean elf64_hppa_size_dynamic_sections
00197   PARAMS ((bfd *, struct bfd_link_info *));
00198 
00199 static bfd_boolean elf64_hppa_link_output_symbol_hook
00200   PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
00201           asection *, struct elf_link_hash_entry *));
00202 
00203 static bfd_boolean elf64_hppa_finish_dynamic_symbol
00204   PARAMS ((bfd *, struct bfd_link_info *,
00205           struct elf_link_hash_entry *, Elf_Internal_Sym *));
00206 
00207 static enum elf_reloc_type_class elf64_hppa_reloc_type_class
00208   PARAMS ((const Elf_Internal_Rela *));
00209 
00210 static bfd_boolean elf64_hppa_finish_dynamic_sections
00211   PARAMS ((bfd *, struct bfd_link_info *));
00212 
00213 static bfd_boolean elf64_hppa_check_relocs
00214   PARAMS ((bfd *, struct bfd_link_info *,
00215           asection *, const Elf_Internal_Rela *));
00216 
00217 static bfd_boolean elf64_hppa_dynamic_symbol_p
00218   PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
00219 
00220 static bfd_boolean elf64_hppa_mark_exported_functions
00221   PARAMS ((struct elf_link_hash_entry *, PTR));
00222 
00223 static bfd_boolean elf64_hppa_finalize_opd
00224   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00225 
00226 static bfd_boolean elf64_hppa_finalize_dlt
00227   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00228 
00229 static bfd_boolean allocate_global_data_dlt
00230   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00231 
00232 static bfd_boolean allocate_global_data_plt
00233   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00234 
00235 static bfd_boolean allocate_global_data_stub
00236   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00237 
00238 static bfd_boolean allocate_global_data_opd
00239   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00240 
00241 static bfd_boolean get_reloc_section
00242   PARAMS ((bfd *, struct elf64_hppa_link_hash_table *, asection *));
00243 
00244 static bfd_boolean count_dyn_reloc
00245   PARAMS ((bfd *, struct elf64_hppa_dyn_hash_entry *,
00246           int, asection *, int, bfd_vma, bfd_vma));
00247 
00248 static bfd_boolean allocate_dynrel_entries
00249   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00250 
00251 static bfd_boolean elf64_hppa_finalize_dynreloc
00252   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00253 
00254 static bfd_boolean get_opd
00255   PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
00256 
00257 static bfd_boolean get_plt
00258   PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
00259 
00260 static bfd_boolean get_dlt
00261   PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
00262 
00263 static bfd_boolean get_stub
00264   PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
00265 
00266 static int elf64_hppa_elf_get_symbol_type
00267   PARAMS ((Elf_Internal_Sym *, int));
00268 
00269 static bfd_boolean
00270 elf64_hppa_dyn_hash_table_init (struct elf64_hppa_dyn_hash_table *ht,
00271                             bfd *abfd ATTRIBUTE_UNUSED,
00272                             new_hash_entry_func new,
00273                             unsigned int entsize)
00274 {
00275   memset (ht, 0, sizeof (*ht));
00276   return bfd_hash_table_init (&ht->root, new, entsize);
00277 }
00278 
00279 static struct bfd_hash_entry*
00280 elf64_hppa_new_dyn_hash_entry (entry, table, string)
00281      struct bfd_hash_entry *entry;
00282      struct bfd_hash_table *table;
00283      const char *string;
00284 {
00285   struct elf64_hppa_dyn_hash_entry *ret;
00286   ret = (struct elf64_hppa_dyn_hash_entry *) entry;
00287 
00288   /* Allocate the structure if it has not already been allocated by a
00289      subclass.  */
00290   if (!ret)
00291     ret = bfd_hash_allocate (table, sizeof (*ret));
00292 
00293   if (!ret)
00294     return 0;
00295 
00296   /* Call the allocation method of the superclass.  */
00297   ret = ((struct elf64_hppa_dyn_hash_entry *)
00298         bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
00299 
00300   /* Initialize our local data.  All zeros.  */
00301   memset (&ret->dlt_offset, 0,
00302          (sizeof (struct elf64_hppa_dyn_hash_entry)
00303           - offsetof (struct elf64_hppa_dyn_hash_entry, dlt_offset)));
00304 
00305   return &ret->root;
00306 }
00307 
00308 /* Create the derived linker hash table.  The PA64 ELF port uses this
00309    derived hash table to keep information specific to the PA ElF
00310    linker (without using static variables).  */
00311 
00312 static struct bfd_link_hash_table*
00313 elf64_hppa_hash_table_create (abfd)
00314      bfd *abfd;
00315 {
00316   struct elf64_hppa_link_hash_table *ret;
00317 
00318   ret = bfd_zalloc (abfd, (bfd_size_type) sizeof (*ret));
00319   if (!ret)
00320     return 0;
00321   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
00322                                   _bfd_elf_link_hash_newfunc,
00323                                   sizeof (struct elf_link_hash_entry)))
00324     {
00325       bfd_release (abfd, ret);
00326       return 0;
00327     }
00328 
00329   if (!elf64_hppa_dyn_hash_table_init (&ret->dyn_hash_table, abfd,
00330                                    elf64_hppa_new_dyn_hash_entry,
00331                                    sizeof (struct elf64_hppa_dyn_hash_entry)))
00332     return 0;
00333   return &ret->root.root;
00334 }
00335 
00336 /* Look up an entry in a PA64 ELF linker hash table.  */
00337 
00338 static struct elf64_hppa_dyn_hash_entry *
00339 elf64_hppa_dyn_hash_lookup(table, string, create, copy)
00340      struct elf64_hppa_dyn_hash_table *table;
00341      const char *string;
00342      bfd_boolean create, copy;
00343 {
00344   return ((struct elf64_hppa_dyn_hash_entry *)
00345          bfd_hash_lookup (&table->root, string, create, copy));
00346 }
00347 
00348 /* Traverse a PA64 ELF linker hash table.  */
00349 
00350 static void
00351 elf64_hppa_dyn_hash_traverse (table, func, info)
00352      struct elf64_hppa_dyn_hash_table *table;
00353      bfd_boolean (*func) PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
00354      PTR info;
00355 {
00356   (bfd_hash_traverse
00357    (&table->root,
00358     (bfd_boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) func,
00359     info));
00360 }
00361 
00362 /* Return nonzero if ABFD represents a PA2.0 ELF64 file.
00363 
00364    Additionally we set the default architecture and machine.  */
00365 static bfd_boolean
00366 elf64_hppa_object_p (abfd)
00367      bfd *abfd;
00368 {
00369   Elf_Internal_Ehdr * i_ehdrp;
00370   unsigned int flags;
00371 
00372   i_ehdrp = elf_elfheader (abfd);
00373   if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
00374     {
00375       /* GCC on hppa-linux produces binaries with OSABI=Linux,
00376         but the kernel produces corefiles with OSABI=SysV.  */
00377       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX
00378          && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
00379        return FALSE;
00380     }
00381   else
00382     {
00383       /* HPUX produces binaries with OSABI=HPUX,
00384         but the kernel produces corefiles with OSABI=SysV.  */
00385       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
00386          && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
00387        return FALSE;
00388     }
00389 
00390   flags = i_ehdrp->e_flags;
00391   switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
00392     {
00393     case EFA_PARISC_1_0:
00394       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
00395     case EFA_PARISC_1_1:
00396       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
00397     case EFA_PARISC_2_0:
00398       if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
00399         return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
00400       else
00401         return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
00402     case EFA_PARISC_2_0 | EF_PARISC_WIDE:
00403       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
00404     }
00405   /* Don't be fussy.  */
00406   return TRUE;
00407 }
00408 
00409 /* Given section type (hdr->sh_type), return a boolean indicating
00410    whether or not the section is an elf64-hppa specific section.  */
00411 static bfd_boolean
00412 elf64_hppa_section_from_shdr (bfd *abfd,
00413                            Elf_Internal_Shdr *hdr,
00414                            const char *name,
00415                            int shindex)
00416 {
00417   asection *newsect;
00418 
00419   switch (hdr->sh_type)
00420     {
00421     case SHT_PARISC_EXT:
00422       if (strcmp (name, ".PARISC.archext") != 0)
00423        return FALSE;
00424       break;
00425     case SHT_PARISC_UNWIND:
00426       if (strcmp (name, ".PARISC.unwind") != 0)
00427        return FALSE;
00428       break;
00429     case SHT_PARISC_DOC:
00430     case SHT_PARISC_ANNOT:
00431     default:
00432       return FALSE;
00433     }
00434 
00435   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
00436     return FALSE;
00437   newsect = hdr->bfd_section;
00438 
00439   return TRUE;
00440 }
00441 
00442 /* Construct a string for use in the elf64_hppa_dyn_hash_table.  The
00443    name describes what was once potentially anonymous memory.  We
00444    allocate memory as necessary, possibly reusing PBUF/PLEN.  */
00445 
00446 static const char *
00447 get_dyn_name (abfd, h, rel, pbuf, plen)
00448      bfd *abfd;
00449      struct elf_link_hash_entry *h;
00450      const Elf_Internal_Rela *rel;
00451      char **pbuf;
00452      size_t *plen;
00453 {
00454   asection *sec = abfd->sections;
00455   size_t nlen, tlen;
00456   char *buf;
00457   size_t len;
00458 
00459   if (h && rel->r_addend == 0)
00460     return h->root.root.string;
00461 
00462   if (h)
00463     nlen = strlen (h->root.root.string);
00464   else
00465     nlen = 8 + 1 + sizeof (rel->r_info) * 2 - 8;
00466   tlen = nlen + 1 + sizeof (rel->r_addend) * 2 + 1;
00467 
00468   len = *plen;
00469   buf = *pbuf;
00470   if (len < tlen)
00471     {
00472       if (buf)
00473        free (buf);
00474       *pbuf = buf = malloc (tlen);
00475       *plen = len = tlen;
00476       if (!buf)
00477        return NULL;
00478     }
00479 
00480   if (h)
00481     {
00482       memcpy (buf, h->root.root.string, nlen);
00483       buf[nlen++] = '+';
00484       sprintf_vma (buf + nlen, rel->r_addend);
00485     }
00486   else
00487     {
00488       nlen = sprintf (buf, "%x:%lx",
00489                     sec->id & 0xffffffff,
00490                     (long) ELF64_R_SYM (rel->r_info));
00491       if (rel->r_addend)
00492        {
00493          buf[nlen++] = '+';
00494          sprintf_vma (buf + nlen, rel->r_addend);
00495        }
00496     }
00497 
00498   return buf;
00499 }
00500 
00501 /* SEC is a section containing relocs for an input BFD when linking; return
00502    a suitable section for holding relocs in the output BFD for a link.  */
00503 
00504 static bfd_boolean
00505 get_reloc_section (abfd, hppa_info, sec)
00506      bfd *abfd;
00507      struct elf64_hppa_link_hash_table *hppa_info;
00508      asection *sec;
00509 {
00510   const char *srel_name;
00511   asection *srel;
00512   bfd *dynobj;
00513 
00514   srel_name = (bfd_elf_string_from_elf_section
00515               (abfd, elf_elfheader(abfd)->e_shstrndx,
00516               elf_section_data(sec)->rel_hdr.sh_name));
00517   if (srel_name == NULL)
00518     return FALSE;
00519 
00520   BFD_ASSERT ((CONST_STRNEQ (srel_name, ".rela")
00521               && strcmp (bfd_get_section_name (abfd, sec),
00522                        srel_name + 5) == 0)
00523              || (CONST_STRNEQ (srel_name, ".rel")
00524                 && strcmp (bfd_get_section_name (abfd, sec),
00525                           srel_name + 4) == 0));
00526 
00527   dynobj = hppa_info->root.dynobj;
00528   if (!dynobj)
00529     hppa_info->root.dynobj = dynobj = abfd;
00530 
00531   srel = bfd_get_section_by_name (dynobj, srel_name);
00532   if (srel == NULL)
00533     {
00534       srel = bfd_make_section_with_flags (dynobj, srel_name,
00535                                      (SEC_ALLOC
00536                                       | SEC_LOAD
00537                                       | SEC_HAS_CONTENTS
00538                                       | SEC_IN_MEMORY
00539                                       | SEC_LINKER_CREATED
00540                                       | SEC_READONLY));
00541       if (srel == NULL
00542          || !bfd_set_section_alignment (dynobj, srel, 3))
00543        return FALSE;
00544     }
00545 
00546   hppa_info->other_rel_sec = srel;
00547   return TRUE;
00548 }
00549 
00550 /* Add a new entry to the list of dynamic relocations against DYN_H.
00551 
00552    We use this to keep a record of all the FPTR relocations against a
00553    particular symbol so that we can create FPTR relocations in the
00554    output file.  */
00555 
00556 static bfd_boolean
00557 count_dyn_reloc (abfd, dyn_h, type, sec, sec_symndx, offset, addend)
00558      bfd *abfd;
00559      struct elf64_hppa_dyn_hash_entry *dyn_h;
00560      int type;
00561      asection *sec;
00562      int sec_symndx;
00563      bfd_vma offset;
00564      bfd_vma addend;
00565 {
00566   struct elf64_hppa_dyn_reloc_entry *rent;
00567 
00568   rent = (struct elf64_hppa_dyn_reloc_entry *)
00569   bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
00570   if (!rent)
00571     return FALSE;
00572 
00573   rent->next = dyn_h->reloc_entries;
00574   rent->type = type;
00575   rent->sec = sec;
00576   rent->sec_symndx = sec_symndx;
00577   rent->offset = offset;
00578   rent->addend = addend;
00579   dyn_h->reloc_entries = rent;
00580 
00581   return TRUE;
00582 }
00583 
00584 /* Scan the RELOCS and record the type of dynamic entries that each
00585    referenced symbol needs.  */
00586 
00587 static bfd_boolean
00588 elf64_hppa_check_relocs (abfd, info, sec, relocs)
00589      bfd *abfd;
00590      struct bfd_link_info *info;
00591      asection *sec;
00592      const Elf_Internal_Rela *relocs;
00593 {
00594   struct elf64_hppa_link_hash_table *hppa_info;
00595   const Elf_Internal_Rela *relend;
00596   Elf_Internal_Shdr *symtab_hdr;
00597   const Elf_Internal_Rela *rel;
00598   asection *dlt, *plt, *stubs;
00599   char *buf;
00600   size_t buf_len;
00601   int sec_symndx;
00602 
00603   if (info->relocatable)
00604     return TRUE;
00605 
00606   /* If this is the first dynamic object found in the link, create
00607      the special sections required for dynamic linking.  */
00608   if (! elf_hash_table (info)->dynamic_sections_created)
00609     {
00610       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
00611        return FALSE;
00612     }
00613 
00614   hppa_info = elf64_hppa_hash_table (info);
00615   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00616 
00617   /* If necessary, build a new table holding section symbols indices
00618      for this BFD.  */
00619 
00620   if (info->shared && hppa_info->section_syms_bfd != abfd)
00621     {
00622       unsigned long i;
00623       unsigned int highest_shndx;
00624       Elf_Internal_Sym *local_syms = NULL;
00625       Elf_Internal_Sym *isym, *isymend;
00626       bfd_size_type amt;
00627 
00628       /* We're done with the old cache of section index to section symbol
00629         index information.  Free it.
00630 
00631         ?!? Note we leak the last section_syms array.  Presumably we
00632         could free it in one of the later routines in this file.  */
00633       if (hppa_info->section_syms)
00634        free (hppa_info->section_syms);
00635 
00636       /* Read this BFD's local symbols.  */
00637       if (symtab_hdr->sh_info != 0)
00638        {
00639          local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
00640          if (local_syms == NULL)
00641            local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
00642                                           symtab_hdr->sh_info, 0,
00643                                           NULL, NULL, NULL);
00644          if (local_syms == NULL)
00645            return FALSE;
00646        }
00647 
00648       /* Record the highest section index referenced by the local symbols.  */
00649       highest_shndx = 0;
00650       isymend = local_syms + symtab_hdr->sh_info;
00651       for (isym = local_syms; isym < isymend; isym++)
00652        {
00653          if (isym->st_shndx > highest_shndx)
00654            highest_shndx = isym->st_shndx;
00655        }
00656 
00657       /* Allocate an array to hold the section index to section symbol index
00658         mapping.  Bump by one since we start counting at zero.  */
00659       highest_shndx++;
00660       amt = highest_shndx;
00661       amt *= sizeof (int);
00662       hppa_info->section_syms = (int *) bfd_malloc (amt);
00663 
00664       /* Now walk the local symbols again.  If we find a section symbol,
00665         record the index of the symbol into the section_syms array.  */
00666       for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
00667        {
00668          if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
00669            hppa_info->section_syms[isym->st_shndx] = i;
00670        }
00671 
00672       /* We are finished with the local symbols.  */
00673       if (local_syms != NULL
00674          && symtab_hdr->contents != (unsigned char *) local_syms)
00675        {
00676          if (! info->keep_memory)
00677            free (local_syms);
00678          else
00679            {
00680              /* Cache the symbols for elf_link_input_bfd.  */
00681              symtab_hdr->contents = (unsigned char *) local_syms;
00682            }
00683        }
00684 
00685       /* Record which BFD we built the section_syms mapping for.  */
00686       hppa_info->section_syms_bfd = abfd;
00687     }
00688 
00689   /* Record the symbol index for this input section.  We may need it for
00690      relocations when building shared libraries.  When not building shared
00691      libraries this value is never really used, but assign it to zero to
00692      prevent out of bounds memory accesses in other routines.  */
00693   if (info->shared)
00694     {
00695       sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
00696 
00697       /* If we did not find a section symbol for this section, then
00698         something went terribly wrong above.  */
00699       if (sec_symndx == -1)
00700        return FALSE;
00701 
00702       sec_symndx = hppa_info->section_syms[sec_symndx];
00703     }
00704   else
00705     sec_symndx = 0;
00706 
00707   dlt = plt = stubs = NULL;
00708   buf = NULL;
00709   buf_len = 0;
00710 
00711   relend = relocs + sec->reloc_count;
00712   for (rel = relocs; rel < relend; ++rel)
00713     {
00714       enum
00715        {
00716          NEED_DLT = 1,
00717          NEED_PLT = 2,
00718          NEED_STUB = 4,
00719          NEED_OPD = 8,
00720          NEED_DYNREL = 16,
00721        };
00722 
00723       struct elf_link_hash_entry *h = NULL;
00724       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
00725       struct elf64_hppa_dyn_hash_entry *dyn_h;
00726       int need_entry;
00727       const char *addr_name;
00728       bfd_boolean maybe_dynamic;
00729       int dynrel_type = R_PARISC_NONE;
00730       static reloc_howto_type *howto;
00731 
00732       if (r_symndx >= symtab_hdr->sh_info)
00733        {
00734          /* We're dealing with a global symbol -- find its hash entry
00735             and mark it as being referenced.  */
00736          long indx = r_symndx - symtab_hdr->sh_info;
00737          h = elf_sym_hashes (abfd)[indx];
00738          while (h->root.type == bfd_link_hash_indirect
00739                || h->root.type == bfd_link_hash_warning)
00740            h = (struct elf_link_hash_entry *) h->root.u.i.link;
00741 
00742          h->ref_regular = 1;
00743        }
00744 
00745       /* We can only get preliminary data on whether a symbol is
00746         locally or externally defined, as not all of the input files
00747         have yet been processed.  Do something with what we know, as
00748         this may help reduce memory usage and processing time later.  */
00749       maybe_dynamic = FALSE;
00750       if (h && ((info->shared
00751                && (!info->symbolic
00752                    || info->unresolved_syms_in_shared_libs == RM_IGNORE))
00753               || !h->def_regular
00754               || h->root.type == bfd_link_hash_defweak))
00755        maybe_dynamic = TRUE;
00756 
00757       howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
00758       need_entry = 0;
00759       switch (howto->type)
00760        {
00761        /* These are simple indirect references to symbols through the
00762           DLT.  We need to create a DLT entry for any symbols which
00763           appears in a DLTIND relocation.  */
00764        case R_PARISC_DLTIND21L:
00765        case R_PARISC_DLTIND14R:
00766        case R_PARISC_DLTIND14F:
00767        case R_PARISC_DLTIND14WR:
00768        case R_PARISC_DLTIND14DR:
00769          need_entry = NEED_DLT;
00770          break;
00771 
00772        /* ?!?  These need a DLT entry.  But I have no idea what to do with
00773           the "link time TP value.  */
00774        case R_PARISC_LTOFF_TP21L:
00775        case R_PARISC_LTOFF_TP14R:
00776        case R_PARISC_LTOFF_TP14F:
00777        case R_PARISC_LTOFF_TP64:
00778        case R_PARISC_LTOFF_TP14WR:
00779        case R_PARISC_LTOFF_TP14DR:
00780        case R_PARISC_LTOFF_TP16F:
00781        case R_PARISC_LTOFF_TP16WF:
00782        case R_PARISC_LTOFF_TP16DF:
00783          need_entry = NEED_DLT;
00784          break;
00785 
00786        /* These are function calls.  Depending on their precise target we
00787           may need to make a stub for them.  The stub uses the PLT, so we
00788           need to create PLT entries for these symbols too.  */
00789        case R_PARISC_PCREL12F:
00790        case R_PARISC_PCREL17F:
00791        case R_PARISC_PCREL22F:
00792        case R_PARISC_PCREL32:
00793        case R_PARISC_PCREL64:
00794        case R_PARISC_PCREL21L:
00795        case R_PARISC_PCREL17R:
00796        case R_PARISC_PCREL17C:
00797        case R_PARISC_PCREL14R:
00798        case R_PARISC_PCREL14F:
00799        case R_PARISC_PCREL22C:
00800        case R_PARISC_PCREL14WR:
00801        case R_PARISC_PCREL14DR:
00802        case R_PARISC_PCREL16F:
00803        case R_PARISC_PCREL16WF:
00804        case R_PARISC_PCREL16DF:
00805          need_entry = (NEED_PLT | NEED_STUB);
00806          break;
00807 
00808        case R_PARISC_PLTOFF21L:
00809        case R_PARISC_PLTOFF14R:
00810        case R_PARISC_PLTOFF14F:
00811        case R_PARISC_PLTOFF14WR:
00812        case R_PARISC_PLTOFF14DR:
00813        case R_PARISC_PLTOFF16F:
00814        case R_PARISC_PLTOFF16WF:
00815        case R_PARISC_PLTOFF16DF:
00816          need_entry = (NEED_PLT);
00817          break;
00818 
00819        case R_PARISC_DIR64:
00820          if (info->shared || maybe_dynamic)
00821            need_entry = (NEED_DYNREL);
00822          dynrel_type = R_PARISC_DIR64;
00823          break;
00824 
00825        /* This is an indirect reference through the DLT to get the address
00826           of a OPD descriptor.  Thus we need to make a DLT entry that points
00827           to an OPD entry.  */
00828        case R_PARISC_LTOFF_FPTR21L:
00829        case R_PARISC_LTOFF_FPTR14R:
00830        case R_PARISC_LTOFF_FPTR14WR:
00831        case R_PARISC_LTOFF_FPTR14DR:
00832        case R_PARISC_LTOFF_FPTR32:
00833        case R_PARISC_LTOFF_FPTR64:
00834        case R_PARISC_LTOFF_FPTR16F:
00835        case R_PARISC_LTOFF_FPTR16WF:
00836        case R_PARISC_LTOFF_FPTR16DF:
00837          if (info->shared || maybe_dynamic)
00838            need_entry = (NEED_DLT | NEED_OPD);
00839          else
00840            need_entry = (NEED_DLT | NEED_OPD);
00841          dynrel_type = R_PARISC_FPTR64;
00842          break;
00843 
00844        /* This is a simple OPD entry.  */
00845        case R_PARISC_FPTR64:
00846          if (info->shared || maybe_dynamic)
00847            need_entry = (NEED_OPD | NEED_DYNREL);
00848          else
00849            need_entry = (NEED_OPD);
00850          dynrel_type = R_PARISC_FPTR64;
00851          break;
00852 
00853        /* Add more cases as needed.  */
00854        }
00855 
00856       if (!need_entry)
00857        continue;
00858 
00859       /* Collect a canonical name for this address.  */
00860       addr_name = get_dyn_name (abfd, h, rel, &buf, &buf_len);
00861 
00862       /* Collect the canonical entry data for this address.  */
00863       dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
00864                                      addr_name, TRUE, TRUE);
00865       BFD_ASSERT (dyn_h);
00866 
00867       /* Stash away enough information to be able to find this symbol
00868         regardless of whether or not it is local or global.  */
00869       dyn_h->h = h;
00870       dyn_h->owner = abfd;
00871       dyn_h->sym_indx = r_symndx;
00872 
00873       /* ?!? We may need to do some error checking in here.  */
00874       /* Create what's needed.  */
00875       if (need_entry & NEED_DLT)
00876        {
00877          if (! hppa_info->dlt_sec
00878              && ! get_dlt (abfd, info, hppa_info))
00879            goto err_out;
00880          dyn_h->want_dlt = 1;
00881        }
00882 
00883       if (need_entry & NEED_PLT)
00884        {
00885          if (! hppa_info->plt_sec
00886              && ! get_plt (abfd, info, hppa_info))
00887            goto err_out;
00888          dyn_h->want_plt = 1;
00889        }
00890 
00891       if (need_entry & NEED_STUB)
00892        {
00893          if (! hppa_info->stub_sec
00894              && ! get_stub (abfd, info, hppa_info))
00895            goto err_out;
00896          dyn_h->want_stub = 1;
00897        }
00898 
00899       if (need_entry & NEED_OPD)
00900        {
00901          if (! hppa_info->opd_sec
00902              && ! get_opd (abfd, info, hppa_info))
00903            goto err_out;
00904 
00905          dyn_h->want_opd = 1;
00906 
00907          /* FPTRs are not allocated by the dynamic linker for PA64, though
00908             it is possible that will change in the future.  */
00909 
00910          /* This could be a local function that had its address taken, in
00911             which case H will be NULL.  */
00912          if (h)
00913            h->needs_plt = 1;
00914        }
00915 
00916       /* Add a new dynamic relocation to the chain of dynamic
00917         relocations for this symbol.  */
00918       if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
00919        {
00920          if (! hppa_info->other_rel_sec
00921              && ! get_reloc_section (abfd, hppa_info, sec))
00922            goto err_out;
00923 
00924          if (!count_dyn_reloc (abfd, dyn_h, dynrel_type, sec,
00925                             sec_symndx, rel->r_offset, rel->r_addend))
00926            goto err_out;
00927 
00928          /* If we are building a shared library and we just recorded
00929             a dynamic R_PARISC_FPTR64 relocation, then make sure the
00930             section symbol for this section ends up in the dynamic
00931             symbol table.  */
00932          if (info->shared && dynrel_type == R_PARISC_FPTR64
00933              && ! (bfd_elf_link_record_local_dynamic_symbol
00934                   (info, abfd, sec_symndx)))
00935            return FALSE;
00936        }
00937     }
00938 
00939   if (buf)
00940     free (buf);
00941   return TRUE;
00942 
00943  err_out:
00944   if (buf)
00945     free (buf);
00946   return FALSE;
00947 }
00948 
00949 struct elf64_hppa_allocate_data
00950 {
00951   struct bfd_link_info *info;
00952   bfd_size_type ofs;
00953 };
00954 
00955 /* Should we do dynamic things to this symbol?  */
00956 
00957 static bfd_boolean
00958 elf64_hppa_dynamic_symbol_p (h, info)
00959      struct elf_link_hash_entry *h;
00960      struct bfd_link_info *info;
00961 {
00962   /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
00963      and relocations that retrieve a function descriptor?  Assume the
00964      worst for now.  */
00965   if (_bfd_elf_dynamic_symbol_p (h, info, 1))
00966     {
00967       /* ??? Why is this here and not elsewhere is_local_label_name.  */
00968       if (h->root.root.string[0] == '$' && h->root.root.string[1] == '$')
00969        return FALSE;
00970 
00971       return TRUE;
00972     }
00973   else
00974     return FALSE;
00975 }
00976 
00977 /* Mark all functions exported by this file so that we can later allocate
00978    entries in .opd for them.  */
00979 
00980 static bfd_boolean
00981 elf64_hppa_mark_exported_functions (h, data)
00982      struct elf_link_hash_entry *h;
00983      PTR data;
00984 {
00985   struct bfd_link_info *info = (struct bfd_link_info *)data;
00986   struct elf64_hppa_link_hash_table *hppa_info;
00987 
00988   hppa_info = elf64_hppa_hash_table (info);
00989 
00990   if (h->root.type == bfd_link_hash_warning)
00991     h = (struct elf_link_hash_entry *) h->root.u.i.link;
00992 
00993   if (h
00994       && (h->root.type == bfd_link_hash_defined
00995          || h->root.type == bfd_link_hash_defweak)
00996       && h->root.u.def.section->output_section != NULL
00997       && h->type == STT_FUNC)
00998     {
00999        struct elf64_hppa_dyn_hash_entry *dyn_h;
01000 
01001       /* Add this symbol to the PA64 linker hash table.  */
01002       dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
01003                                      h->root.root.string, TRUE, TRUE);
01004       BFD_ASSERT (dyn_h);
01005       dyn_h->h = h;
01006 
01007       if (! hppa_info->opd_sec
01008          && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
01009        return FALSE;
01010 
01011       dyn_h->want_opd = 1;
01012       /* Put a flag here for output_symbol_hook.  */
01013       dyn_h->st_shndx = -1;
01014       h->needs_plt = 1;
01015     }
01016 
01017   return TRUE;
01018 }
01019 
01020 /* Allocate space for a DLT entry.  */
01021 
01022 static bfd_boolean
01023 allocate_global_data_dlt (dyn_h, data)
01024      struct elf64_hppa_dyn_hash_entry *dyn_h;
01025      PTR data;
01026 {
01027   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
01028 
01029   if (dyn_h->want_dlt)
01030     {
01031       struct elf_link_hash_entry *h = dyn_h->h;
01032 
01033       if (x->info->shared)
01034        {
01035          /* Possibly add the symbol to the local dynamic symbol
01036             table since we might need to create a dynamic relocation
01037             against it.  */
01038          if (! h
01039              || (h->dynindx == -1 && h->type != STT_PARISC_MILLI))
01040            {
01041              bfd *owner;
01042              owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
01043 
01044              if (! (bfd_elf_link_record_local_dynamic_symbol
01045                    (x->info, owner, dyn_h->sym_indx)))
01046               return FALSE;
01047            }
01048        }
01049 
01050       dyn_h->dlt_offset = x->ofs;
01051       x->ofs += DLT_ENTRY_SIZE;
01052     }
01053   return TRUE;
01054 }
01055 
01056 /* Allocate space for a DLT.PLT entry.  */
01057 
01058 static bfd_boolean
01059 allocate_global_data_plt (dyn_h, data)
01060      struct elf64_hppa_dyn_hash_entry *dyn_h;
01061      PTR data;
01062 {
01063   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
01064 
01065   if (dyn_h->want_plt
01066       && elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info)
01067       && !((dyn_h->h->root.type == bfd_link_hash_defined
01068            || dyn_h->h->root.type == bfd_link_hash_defweak)
01069           && dyn_h->h->root.u.def.section->output_section != NULL))
01070     {
01071       dyn_h->plt_offset = x->ofs;
01072       x->ofs += PLT_ENTRY_SIZE;
01073       if (dyn_h->plt_offset < 0x2000)
01074        elf64_hppa_hash_table (x->info)->gp_offset = dyn_h->plt_offset;
01075     }
01076   else
01077     dyn_h->want_plt = 0;
01078 
01079   return TRUE;
01080 }
01081 
01082 /* Allocate space for a STUB entry.  */
01083 
01084 static bfd_boolean
01085 allocate_global_data_stub (dyn_h, data)
01086      struct elf64_hppa_dyn_hash_entry *dyn_h;
01087      PTR data;
01088 {
01089   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
01090 
01091   if (dyn_h->want_stub
01092       && elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info)
01093       && !((dyn_h->h->root.type == bfd_link_hash_defined
01094            || dyn_h->h->root.type == bfd_link_hash_defweak)
01095           && dyn_h->h->root.u.def.section->output_section != NULL))
01096     {
01097       dyn_h->stub_offset = x->ofs;
01098       x->ofs += sizeof (plt_stub);
01099     }
01100   else
01101     dyn_h->want_stub = 0;
01102   return TRUE;
01103 }
01104 
01105 /* Allocate space for a FPTR entry.  */
01106 
01107 static bfd_boolean
01108 allocate_global_data_opd (dyn_h, data)
01109      struct elf64_hppa_dyn_hash_entry *dyn_h;
01110      PTR data;
01111 {
01112   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
01113 
01114   if (dyn_h->want_opd)
01115     {
01116       struct elf_link_hash_entry *h = dyn_h->h;
01117 
01118       if (h)
01119        while (h->root.type == bfd_link_hash_indirect
01120               || h->root.type == bfd_link_hash_warning)
01121          h = (struct elf_link_hash_entry *) h->root.u.i.link;
01122 
01123       /* We never need an opd entry for a symbol which is not
01124         defined by this output file.  */
01125       if (h && (h->root.type == bfd_link_hash_undefined
01126               || h->root.type == bfd_link_hash_undefweak
01127               || h->root.u.def.section->output_section == NULL))
01128        dyn_h->want_opd = 0;
01129 
01130       /* If we are creating a shared library, took the address of a local
01131         function or might export this function from this object file, then
01132         we have to create an opd descriptor.  */
01133       else if (x->info->shared
01134               || h == NULL
01135               || (h->dynindx == -1 && h->type != STT_PARISC_MILLI)
01136               || (h->root.type == bfd_link_hash_defined
01137                  || h->root.type == bfd_link_hash_defweak))
01138        {
01139          /* If we are creating a shared library, then we will have to
01140             create a runtime relocation for the symbol to properly
01141             initialize the .opd entry.  Make sure the symbol gets
01142             added to the dynamic symbol table.  */
01143          if (x->info->shared
01144              && (h == NULL || (h->dynindx == -1)))
01145            {
01146              bfd *owner;
01147              owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
01148 
01149              if (!bfd_elf_link_record_local_dynamic_symbol
01150                   (x->info, owner, dyn_h->sym_indx))
01151               return FALSE;
01152            }
01153 
01154          /* This may not be necessary or desirable anymore now that
01155             we have some support for dealing with section symbols
01156             in dynamic relocs.  But name munging does make the result
01157             much easier to debug.  ie, the EPLT reloc will reference
01158             a symbol like .foobar, instead of .text + offset.  */
01159          if (x->info->shared && h)
01160            {
01161              char *new_name;
01162              struct elf_link_hash_entry *nh;
01163 
01164              new_name = alloca (strlen (h->root.root.string) + 2);
01165              new_name[0] = '.';
01166              strcpy (new_name + 1, h->root.root.string);
01167 
01168              nh = elf_link_hash_lookup (elf_hash_table (x->info),
01169                                     new_name, TRUE, TRUE, TRUE);
01170 
01171              nh->root.type = h->root.type;
01172              nh->root.u.def.value = h->root.u.def.value;
01173              nh->root.u.def.section = h->root.u.def.section;
01174 
01175              if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
01176               return FALSE;
01177 
01178             }
01179          dyn_h->opd_offset = x->ofs;
01180          x->ofs += OPD_ENTRY_SIZE;
01181        }
01182 
01183       /* Otherwise we do not need an opd entry.  */
01184       else
01185        dyn_h->want_opd = 0;
01186     }
01187   return TRUE;
01188 }
01189 
01190 /* HP requires the EI_OSABI field to be filled in.  The assignment to
01191    EI_ABIVERSION may not be strictly necessary.  */
01192 
01193 static void
01194 elf64_hppa_post_process_headers (abfd, link_info)
01195      bfd * abfd;
01196      struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
01197 {
01198   Elf_Internal_Ehdr * i_ehdrp;
01199 
01200   i_ehdrp = elf_elfheader (abfd);
01201   
01202   i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
01203   i_ehdrp->e_ident[EI_ABIVERSION] = 1;
01204 }
01205 
01206 /* Create function descriptor section (.opd).  This section is called .opd
01207    because it contains "official procedure descriptors".  The "official"
01208    refers to the fact that these descriptors are used when taking the address
01209    of a procedure, thus ensuring a unique address for each procedure.  */
01210 
01211 static bfd_boolean
01212 get_opd (abfd, info, hppa_info)
01213      bfd *abfd;
01214      struct bfd_link_info *info ATTRIBUTE_UNUSED;
01215      struct elf64_hppa_link_hash_table *hppa_info;
01216 {
01217   asection *opd;
01218   bfd *dynobj;
01219 
01220   opd = hppa_info->opd_sec;
01221   if (!opd)
01222     {
01223       dynobj = hppa_info->root.dynobj;
01224       if (!dynobj)
01225        hppa_info->root.dynobj = dynobj = abfd;
01226 
01227       opd = bfd_make_section_with_flags (dynobj, ".opd",
01228                                     (SEC_ALLOC
01229                                      | SEC_LOAD
01230                                      | SEC_HAS_CONTENTS
01231                                      | SEC_IN_MEMORY
01232                                      | SEC_LINKER_CREATED));
01233       if (!opd
01234          || !bfd_set_section_alignment (abfd, opd, 3))
01235        {
01236          BFD_ASSERT (0);
01237          return FALSE;
01238        }
01239 
01240       hppa_info->opd_sec = opd;
01241     }
01242 
01243   return TRUE;
01244 }
01245 
01246 /* Create the PLT section.  */
01247 
01248 static bfd_boolean
01249 get_plt (abfd, info, hppa_info)
01250      bfd *abfd;
01251      struct bfd_link_info *info ATTRIBUTE_UNUSED;
01252      struct elf64_hppa_link_hash_table *hppa_info;
01253 {
01254   asection *plt;
01255   bfd *dynobj;
01256 
01257   plt = hppa_info->plt_sec;
01258   if (!plt)
01259     {
01260       dynobj = hppa_info->root.dynobj;
01261       if (!dynobj)
01262        hppa_info->root.dynobj = dynobj = abfd;
01263 
01264       plt = bfd_make_section_with_flags (dynobj, ".plt",
01265                                     (SEC_ALLOC
01266                                      | SEC_LOAD
01267                                      | SEC_HAS_CONTENTS
01268                                      | SEC_IN_MEMORY
01269                                      | SEC_LINKER_CREATED));
01270       if (!plt
01271          || !bfd_set_section_alignment (abfd, plt, 3))
01272        {
01273          BFD_ASSERT (0);
01274          return FALSE;
01275        }
01276 
01277       hppa_info->plt_sec = plt;
01278     }
01279 
01280   return TRUE;
01281 }
01282 
01283 /* Create the DLT section.  */
01284 
01285 static bfd_boolean
01286 get_dlt (abfd, info, hppa_info)
01287      bfd *abfd;
01288      struct bfd_link_info *info ATTRIBUTE_UNUSED;
01289      struct elf64_hppa_link_hash_table *hppa_info;
01290 {
01291   asection *dlt;
01292   bfd *dynobj;
01293 
01294   dlt = hppa_info->dlt_sec;
01295   if (!dlt)
01296     {
01297       dynobj = hppa_info->root.dynobj;
01298       if (!dynobj)
01299        hppa_info->root.dynobj = dynobj = abfd;
01300 
01301       dlt = bfd_make_section_with_flags (dynobj, ".dlt",
01302                                     (SEC_ALLOC
01303                                      | SEC_LOAD
01304                                      | SEC_HAS_CONTENTS
01305                                      | SEC_IN_MEMORY
01306                                      | SEC_LINKER_CREATED));
01307       if (!dlt
01308          || !bfd_set_section_alignment (abfd, dlt, 3))
01309        {
01310          BFD_ASSERT (0);
01311          return FALSE;
01312        }
01313 
01314       hppa_info->dlt_sec = dlt;
01315     }
01316 
01317   return TRUE;
01318 }
01319 
01320 /* Create the stubs section.  */
01321 
01322 static bfd_boolean
01323 get_stub (abfd, info, hppa_info)
01324      bfd *abfd;
01325      struct bfd_link_info *info ATTRIBUTE_UNUSED;
01326      struct elf64_hppa_link_hash_table *hppa_info;
01327 {
01328   asection *stub;
01329   bfd *dynobj;
01330 
01331   stub = hppa_info->stub_sec;
01332   if (!stub)
01333     {
01334       dynobj = hppa_info->root.dynobj;
01335       if (!dynobj)
01336        hppa_info->root.dynobj = dynobj = abfd;
01337 
01338       stub = bfd_make_section_with_flags (dynobj, ".stub",
01339                                      (SEC_ALLOC | SEC_LOAD
01340                                       | SEC_HAS_CONTENTS
01341                                       | SEC_IN_MEMORY
01342                                       | SEC_READONLY
01343                                       | SEC_LINKER_CREATED));
01344       if (!stub
01345          || !bfd_set_section_alignment (abfd, stub, 3))
01346        {
01347          BFD_ASSERT (0);
01348          return FALSE;
01349        }
01350 
01351       hppa_info->stub_sec = stub;
01352     }
01353 
01354   return TRUE;
01355 }
01356 
01357 /* Create sections necessary for dynamic linking.  This is only a rough
01358    cut and will likely change as we learn more about the somewhat
01359    unusual dynamic linking scheme HP uses.
01360 
01361    .stub:
01362        Contains code to implement cross-space calls.  The first time one
01363        of the stubs is used it will call into the dynamic linker, later
01364        calls will go straight to the target.
01365 
01366        The only stub we support right now looks like
01367 
01368        ldd OFFSET(%dp),%r1
01369        bve %r0(%r1)
01370        ldd OFFSET+8(%dp),%dp
01371 
01372        Other stubs may be needed in the future.  We may want the remove
01373        the break/nop instruction.  It is only used right now to keep the
01374        offset of a .plt entry and a .stub entry in sync.
01375 
01376    .dlt:
01377        This is what most people call the .got.  HP used a different name.
01378        Losers.
01379 
01380    .rela.dlt:
01381        Relocations for the DLT.
01382 
01383    .plt:
01384        Function pointers as address,gp pairs.
01385 
01386    .rela.plt:
01387        Should contain dynamic IPLT (and EPLT?) relocations.
01388 
01389    .opd:
01390        FPTRS
01391 
01392    .rela.opd:
01393        EPLT relocations for symbols exported from shared libraries.  */
01394 
01395 static bfd_boolean
01396 elf64_hppa_create_dynamic_sections (abfd, info)
01397      bfd *abfd;
01398      struct bfd_link_info *info;
01399 {
01400   asection *s;
01401 
01402   if (! get_stub (abfd, info, elf64_hppa_hash_table (info)))
01403     return FALSE;
01404 
01405   if (! get_dlt (abfd, info, elf64_hppa_hash_table (info)))
01406     return FALSE;
01407 
01408   if (! get_plt (abfd, info, elf64_hppa_hash_table (info)))
01409     return FALSE;
01410 
01411   if (! get_opd (abfd, info, elf64_hppa_hash_table (info)))
01412     return FALSE;
01413 
01414   s = bfd_make_section_with_flags (abfd, ".rela.dlt",
01415                                (SEC_ALLOC | SEC_LOAD
01416                                 | SEC_HAS_CONTENTS
01417                                 | SEC_IN_MEMORY
01418                                 | SEC_READONLY
01419                                 | SEC_LINKER_CREATED));
01420   if (s == NULL
01421       || !bfd_set_section_alignment (abfd, s, 3))
01422     return FALSE;
01423   elf64_hppa_hash_table (info)->dlt_rel_sec = s;
01424 
01425   s = bfd_make_section_with_flags (abfd, ".rela.plt",
01426                                (SEC_ALLOC | SEC_LOAD
01427                                 | SEC_HAS_CONTENTS
01428                                 | SEC_IN_MEMORY
01429                                 | SEC_READONLY
01430                                 | SEC_LINKER_CREATED));
01431   if (s == NULL
01432       || !bfd_set_section_alignment (abfd, s, 3))
01433     return FALSE;
01434   elf64_hppa_hash_table (info)->plt_rel_sec = s;
01435 
01436   s = bfd_make_section_with_flags (abfd, ".rela.data",
01437                                (SEC_ALLOC | SEC_LOAD
01438                                 | SEC_HAS_CONTENTS
01439                                 | SEC_IN_MEMORY
01440                                 | SEC_READONLY
01441                                 | SEC_LINKER_CREATED));
01442   if (s == NULL
01443       || !bfd_set_section_alignment (abfd, s, 3))
01444     return FALSE;
01445   elf64_hppa_hash_table (info)->other_rel_sec = s;
01446 
01447   s = bfd_make_section_with_flags (abfd, ".rela.opd",
01448                                (SEC_ALLOC | SEC_LOAD
01449                                 | SEC_HAS_CONTENTS
01450                                 | SEC_IN_MEMORY
01451                                 | SEC_READONLY
01452                                 | SEC_LINKER_CREATED));
01453   if (s == NULL
01454       || !bfd_set_section_alignment (abfd, s, 3))
01455     return FALSE;
01456   elf64_hppa_hash_table (info)->opd_rel_sec = s;
01457 
01458   return TRUE;
01459 }
01460 
01461 /* Allocate dynamic relocations for those symbols that turned out
01462    to be dynamic.  */
01463 
01464 static bfd_boolean
01465 allocate_dynrel_entries (dyn_h, data)
01466      struct elf64_hppa_dyn_hash_entry *dyn_h;
01467      PTR data;
01468 {
01469   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
01470   struct elf64_hppa_link_hash_table *hppa_info;
01471   struct elf64_hppa_dyn_reloc_entry *rent;
01472   bfd_boolean dynamic_symbol, shared;
01473 
01474   hppa_info = elf64_hppa_hash_table (x->info);
01475   dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info);
01476   shared = x->info->shared;
01477 
01478   /* We may need to allocate relocations for a non-dynamic symbol
01479      when creating a shared library.  */
01480   if (!dynamic_symbol && !shared)
01481     return TRUE;
01482 
01483   /* Take care of the normal data relocations.  */
01484 
01485   for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
01486     {
01487       /* Allocate one iff we are building a shared library, the relocation
01488         isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
01489       if (!shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
01490        continue;
01491 
01492       hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
01493 
01494       /* Make sure this symbol gets into the dynamic symbol table if it is
01495         not already recorded.  ?!? This should not be in the loop since
01496         the symbol need only be added once.  */
01497       if (dyn_h->h == 0
01498          || (dyn_h->h->dynindx == -1 && dyn_h->h->type != STT_PARISC_MILLI))
01499        if (!bfd_elf_link_record_local_dynamic_symbol
01500            (x->info, rent->sec->owner, dyn_h->sym_indx))
01501          return FALSE;
01502     }
01503 
01504   /* Take care of the GOT and PLT relocations.  */
01505 
01506   if ((dynamic_symbol || shared) && dyn_h->want_dlt)
01507     hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
01508 
01509   /* If we are building a shared library, then every symbol that has an
01510      opd entry will need an EPLT relocation to relocate the symbol's address
01511      and __gp value based on the runtime load address.  */
01512   if (shared && dyn_h->want_opd)
01513     hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
01514 
01515   if (dyn_h->want_plt && dynamic_symbol)
01516     {
01517       bfd_size_type t = 0;
01518 
01519       /* Dynamic symbols get one IPLT relocation.  Local symbols in
01520         shared libraries get two REL relocations.  Local symbols in
01521         main applications get nothing.  */
01522       if (dynamic_symbol)
01523        t = sizeof (Elf64_External_Rela);
01524       else if (shared)
01525        t = 2 * sizeof (Elf64_External_Rela);
01526 
01527       hppa_info->plt_rel_sec->size += t;
01528     }
01529 
01530   return TRUE;
01531 }
01532 
01533 /* Adjust a symbol defined by a dynamic object and referenced by a
01534    regular object.  */
01535 
01536 static bfd_boolean
01537 elf64_hppa_adjust_dynamic_symbol (info, h)
01538      struct bfd_link_info *info ATTRIBUTE_UNUSED;
01539      struct elf_link_hash_entry *h;
01540 {
01541   /* ??? Undefined symbols with PLT entries should be re-defined
01542      to be the PLT entry.  */
01543 
01544   /* If this is a weak symbol, and there is a real definition, the
01545      processor independent code will have arranged for us to see the
01546      real definition first, and we can just use the same value.  */
01547   if (h->u.weakdef != NULL)
01548     {
01549       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
01550                 || h->u.weakdef->root.type == bfd_link_hash_defweak);
01551       h->root.u.def.section = h->u.weakdef->root.u.def.section;
01552       h->root.u.def.value = h->u.weakdef->root.u.def.value;
01553       return TRUE;
01554     }
01555 
01556   /* If this is a reference to a symbol defined by a dynamic object which
01557      is not a function, we might allocate the symbol in our .dynbss section
01558      and allocate a COPY dynamic relocation.
01559 
01560      But PA64 code is canonically PIC, so as a rule we can avoid this sort
01561      of hackery.  */
01562 
01563   return TRUE;
01564 }
01565 
01566 /* This function is called via elf_link_hash_traverse to mark millicode
01567    symbols with a dynindx of -1 and to remove the string table reference
01568    from the dynamic symbol table.  If the symbol is not a millicode symbol,
01569    elf64_hppa_mark_exported_functions is called.  */
01570 
01571 static bfd_boolean
01572 elf64_hppa_mark_milli_and_exported_functions (h, data)
01573      struct elf_link_hash_entry *h;
01574      PTR data;
01575 {
01576   struct bfd_link_info *info = (struct bfd_link_info *)data;
01577   struct elf_link_hash_entry *elf = h;
01578 
01579   if (elf->root.type == bfd_link_hash_warning)
01580     elf = (struct elf_link_hash_entry *) elf->root.u.i.link;
01581 
01582   if (elf->type == STT_PARISC_MILLI)
01583     {
01584       if (elf->dynindx != -1)
01585        {
01586          elf->dynindx = -1;
01587          _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
01588                               elf->dynstr_index);
01589        }
01590       return TRUE;
01591     }
01592 
01593   return elf64_hppa_mark_exported_functions (h, data);
01594 }
01595 
01596 /* Set the final sizes of the dynamic sections and allocate memory for
01597    the contents of our special sections.  */
01598 
01599 static bfd_boolean
01600 elf64_hppa_size_dynamic_sections (output_bfd, info)
01601      bfd *output_bfd;
01602      struct bfd_link_info *info;
01603 {
01604   bfd *dynobj;
01605   asection *s;
01606   bfd_boolean plt;
01607   bfd_boolean relocs;
01608   bfd_boolean reltext;
01609   struct elf64_hppa_allocate_data data;
01610   struct elf64_hppa_link_hash_table *hppa_info;
01611 
01612   hppa_info = elf64_hppa_hash_table (info);
01613 
01614   dynobj = elf_hash_table (info)->dynobj;
01615   BFD_ASSERT (dynobj != NULL);
01616 
01617   /* Mark each function this program exports so that we will allocate
01618      space in the .opd section for each function's FPTR.  If we are
01619      creating dynamic sections, change the dynamic index of millicode
01620      symbols to -1 and remove them from the string table for .dynstr.
01621 
01622      We have to traverse the main linker hash table since we have to
01623      find functions which may not have been mentioned in any relocs.  */
01624   elf_link_hash_traverse (elf_hash_table (info),
01625                        (elf_hash_table (info)->dynamic_sections_created
01626                         ? elf64_hppa_mark_milli_and_exported_functions
01627                         : elf64_hppa_mark_exported_functions),
01628                        info);
01629 
01630   if (elf_hash_table (info)->dynamic_sections_created)
01631     {
01632       /* Set the contents of the .interp section to the interpreter.  */
01633       if (info->executable)
01634        {
01635          s = bfd_get_section_by_name (dynobj, ".interp");
01636          BFD_ASSERT (s != NULL);
01637          s->size = sizeof ELF_DYNAMIC_INTERPRETER;
01638          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
01639        }
01640     }
01641   else
01642     {
01643       /* We may have created entries in the .rela.got section.
01644         However, if we are not creating the dynamic sections, we will
01645         not actually use these entries.  Reset the size of .rela.dlt,
01646         which will cause it to get stripped from the output file
01647         below.  */
01648       s = bfd_get_section_by_name (dynobj, ".rela.dlt");
01649       if (s != NULL)
01650        s->size = 0;
01651     }
01652 
01653   /* Allocate the GOT entries.  */
01654 
01655   data.info = info;
01656   if (elf64_hppa_hash_table (info)->dlt_sec)
01657     {
01658       data.ofs = 0x0;
01659       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
01660                                 allocate_global_data_dlt, &data);
01661       hppa_info->dlt_sec->size = data.ofs;
01662 
01663       data.ofs = 0x0;
01664       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
01665                                 allocate_global_data_plt, &data);
01666       hppa_info->plt_sec->size = data.ofs;
01667 
01668       data.ofs = 0x0;
01669       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
01670                                 allocate_global_data_stub, &data);
01671       hppa_info->stub_sec->size = data.ofs;
01672     }
01673 
01674   /* Allocate space for entries in the .opd section.  */
01675   if (elf64_hppa_hash_table (info)->opd_sec)
01676     {
01677       data.ofs = 0;
01678       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
01679                                 allocate_global_data_opd, &data);
01680       hppa_info->opd_sec->size = data.ofs;
01681     }
01682 
01683   /* Now allocate space for dynamic relocations, if necessary.  */
01684   if (hppa_info->root.dynamic_sections_created)
01685     elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
01686                               allocate_dynrel_entries, &data);
01687 
01688   /* The sizes of all the sections are set.  Allocate memory for them.  */
01689   plt = FALSE;
01690   relocs = FALSE;
01691   reltext = FALSE;
01692   for (s = dynobj->sections; s != NULL; s = s->next)
01693     {
01694       const char *name;
01695 
01696       if ((s->flags & SEC_LINKER_CREATED) == 0)
01697        continue;
01698 
01699       /* It's OK to base decisions on the section name, because none
01700         of the dynobj section names depend upon the input files.  */
01701       name = bfd_get_section_name (dynobj, s);
01702 
01703       if (strcmp (name, ".plt") == 0)
01704        {
01705          /* Remember whether there is a PLT.  */
01706          plt = s->size != 0;
01707        }
01708       else if (strcmp (name, ".opd") == 0
01709               || CONST_STRNEQ (name, ".dlt")
01710               || strcmp (name, ".stub") == 0
01711               || strcmp (name, ".got") == 0)
01712        {
01713          /* Strip this section if we don't need it; see the comment below.  */
01714        }
01715       else if (CONST_STRNEQ (name, ".rela"))
01716        {
01717          if (s->size != 0)
01718            {
01719              asection *target;
01720 
01721              /* Remember whether there are any reloc sections other
01722                than .rela.plt.  */
01723              if (strcmp (name, ".rela.plt") != 0)
01724               {
01725                 const char *outname;
01726 
01727                 relocs = TRUE;
01728 
01729                 /* If this relocation section applies to a read only
01730                    section, then we probably need a DT_TEXTREL
01731                    entry.  The entries in the .rela.plt section
01732                    really apply to the .got section, which we
01733                    created ourselves and so know is not readonly.  */
01734                 outname = bfd_get_section_name (output_bfd,
01735                                             s->output_section);
01736                 target = bfd_get_section_by_name (output_bfd, outname + 4);
01737                 if (target != NULL
01738                     && (target->flags & SEC_READONLY) != 0
01739                     && (target->flags & SEC_ALLOC) != 0)
01740                   reltext = TRUE;
01741               }
01742 
01743              /* We use the reloc_count field as a counter if we need
01744                to copy relocs into the output file.  */
01745              s->reloc_count = 0;
01746            }
01747        }
01748       else
01749        {
01750          /* It's not one of our sections, so don't allocate space.  */
01751          continue;
01752        }
01753 
01754       if (s->size == 0)
01755        {
01756          /* If we don't need this section, strip it from the
01757             output file.  This is mostly to handle .rela.bss and
01758             .rela.plt.  We must create both sections in
01759             create_dynamic_sections, because they must be created
01760             before the linker maps input sections to output
01761             sections.  The linker does that before
01762             adjust_dynamic_symbol is called, and it is that
01763             function which decides whether anything needs to go
01764             into these sections.  */
01765          s->flags |= SEC_EXCLUDE;
01766          continue;
01767        }
01768 
01769       if ((s->flags & SEC_HAS_CONTENTS) == 0)
01770        continue;
01771 
01772       /* Allocate memory for the section contents if it has not
01773         been allocated already.  We use bfd_zalloc here in case
01774         unused entries are not reclaimed before the section's
01775         contents are written out.  This should not happen, but this
01776         way if it does, we get a R_PARISC_NONE reloc instead of
01777         garbage.  */
01778       if (s->contents == NULL)
01779        {
01780          s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
01781          if (s->contents == NULL)
01782            return FALSE;
01783        }
01784     }
01785 
01786   if (elf_hash_table (info)->dynamic_sections_created)
01787     {
01788       /* Always create a DT_PLTGOT.  It actually has nothing to do with
01789         the PLT, it is how we communicate the __gp value of a load
01790         module to the dynamic linker.  */
01791 #define add_dynamic_entry(TAG, VAL) \
01792   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
01793 
01794       if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0)
01795          || !add_dynamic_entry (DT_PLTGOT, 0))
01796        return FALSE;
01797 
01798       /* Add some entries to the .dynamic section.  We fill in the
01799         values later, in elf64_hppa_finish_dynamic_sections, but we
01800         must add the entries now so that we get the correct size for
01801         the .dynamic section.  The DT_DEBUG entry is filled in by the
01802         dynamic linker and used by the debugger.  */
01803       if (! info->shared)
01804        {
01805          if (!add_dynamic_entry (DT_DEBUG, 0)
01806              || !add_dynamic_entry (DT_HP_DLD_HOOK, 0)
01807              || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
01808            return FALSE;
01809        }
01810 
01811       /* Force DT_FLAGS to always be set.
01812         Required by HPUX 11.00 patch PHSS_26559.  */
01813       if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
01814        return FALSE;
01815 
01816       if (plt)
01817        {
01818          if (!add_dynamic_entry (DT_PLTRELSZ, 0)
01819              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
01820              || !add_dynamic_entry (DT_JMPREL, 0))
01821            return FALSE;
01822        }
01823 
01824       if (relocs)
01825        {
01826          if (!add_dynamic_entry (DT_RELA, 0)
01827              || !add_dynamic_entry (DT_RELASZ, 0)
01828              || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
01829            return FALSE;
01830        }
01831 
01832       if (reltext)
01833        {
01834          if (!add_dynamic_entry (DT_TEXTREL, 0))
01835            return FALSE;
01836          info->flags |= DF_TEXTREL;
01837        }
01838     }
01839 #undef add_dynamic_entry
01840 
01841   return TRUE;
01842 }
01843 
01844 /* Called after we have output the symbol into the dynamic symbol
01845    table, but before we output the symbol into the normal symbol
01846    table.
01847 
01848    For some symbols we had to change their address when outputting
01849    the dynamic symbol table.  We undo that change here so that
01850    the symbols have their expected value in the normal symbol
01851    table.  Ick.  */
01852 
01853 static bfd_boolean
01854 elf64_hppa_link_output_symbol_hook (info, name, sym, input_sec, h)
01855      struct bfd_link_info *info;
01856      const char *name;
01857      Elf_Internal_Sym *sym;
01858      asection *input_sec ATTRIBUTE_UNUSED;
01859      struct elf_link_hash_entry *h;
01860 {
01861   struct elf64_hppa_link_hash_table *hppa_info;
01862   struct elf64_hppa_dyn_hash_entry *dyn_h;
01863 
01864   /* We may be called with the file symbol or section symbols.
01865      They never need munging, so it is safe to ignore them.  */
01866   if (!name)
01867     return TRUE;
01868 
01869   /* Get the PA dyn_symbol (if any) associated with NAME.  */
01870   hppa_info = elf64_hppa_hash_table (info);
01871   dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
01872                                   name, FALSE, FALSE);
01873   if (!dyn_h || dyn_h->h != h)
01874     return TRUE;
01875 
01876   /* Function symbols for which we created .opd entries *may* have been
01877      munged by finish_dynamic_symbol and have to be un-munged here.
01878 
01879      Note that finish_dynamic_symbol sometimes turns dynamic symbols
01880      into non-dynamic ones, so we initialize st_shndx to -1 in
01881      mark_exported_functions and check to see if it was overwritten
01882      here instead of just checking dyn_h->h->dynindx.  */
01883   if (dyn_h->want_opd && dyn_h->st_shndx != -1)
01884     {
01885       /* Restore the saved value and section index.  */
01886       sym->st_value = dyn_h->st_value;
01887       sym->st_shndx = dyn_h->st_shndx;
01888     }
01889 
01890   return TRUE;
01891 }
01892 
01893 /* Finish up dynamic symbol handling.  We set the contents of various
01894    dynamic sections here.  */
01895 
01896 static bfd_boolean
01897 elf64_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
01898      bfd *output_bfd;
01899      struct bfd_link_info *info;
01900      struct elf_link_hash_entry *h;
01901      Elf_Internal_Sym *sym;
01902 {
01903   asection *stub, *splt, *sdlt, *sopd, *spltrel, *sdltrel;
01904   struct elf64_hppa_link_hash_table *hppa_info;
01905   struct elf64_hppa_dyn_hash_entry *dyn_h;
01906 
01907   hppa_info = elf64_hppa_hash_table (info);
01908   dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
01909                                   h->root.root.string, FALSE, FALSE);
01910 
01911   stub = hppa_info->stub_sec;
01912   splt = hppa_info->plt_sec;
01913   sdlt = hppa_info->dlt_sec;
01914   sopd = hppa_info->opd_sec;
01915   spltrel = hppa_info->plt_rel_sec;
01916   sdltrel = hppa_info->dlt_rel_sec;
01917 
01918   /* Incredible.  It is actually necessary to NOT use the symbol's real
01919      value when building the dynamic symbol table for a shared library.
01920      At least for symbols that refer to functions.
01921 
01922      We will store a new value and section index into the symbol long
01923      enough to output it into the dynamic symbol table, then we restore
01924      the original values (in elf64_hppa_link_output_symbol_hook).  */
01925   if (dyn_h && dyn_h->want_opd)
01926     {
01927       BFD_ASSERT (sopd != NULL);
01928 
01929       /* Save away the original value and section index so that we
01930         can restore them later.  */
01931       dyn_h->st_value = sym->st_value;
01932       dyn_h->st_shndx = sym->st_shndx;
01933 
01934       /* For the dynamic symbol table entry, we want the value to be
01935         address of this symbol's entry within the .opd section.  */
01936       sym->st_value = (dyn_h->opd_offset
01937                      + sopd->output_offset
01938                      + sopd->output_section->vma);
01939       sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
01940                                                   sopd->output_section);
01941     }
01942 
01943   /* Initialize a .plt entry if requested.  */
01944   if (dyn_h && dyn_h->want_plt
01945       && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
01946     {
01947       bfd_vma value;
01948       Elf_Internal_Rela rel;
01949       bfd_byte *loc;
01950 
01951       BFD_ASSERT (splt != NULL && spltrel != NULL);
01952 
01953       /* We do not actually care about the value in the PLT entry
01954         if we are creating a shared library and the symbol is
01955         still undefined, we create a dynamic relocation to fill
01956         in the correct value.  */
01957       if (info->shared && h->root.type == bfd_link_hash_undefined)
01958        value = 0;
01959       else
01960        value = (h->root.u.def.value + h->root.u.def.section->vma);
01961 
01962       /* Fill in the entry in the procedure linkage table.
01963 
01964         The format of a plt entry is
01965         <funcaddr> <__gp>.
01966 
01967         plt_offset is the offset within the PLT section at which to
01968         install the PLT entry.
01969 
01970         We are modifying the in-memory PLT contents here, so we do not add
01971         in the output_offset of the PLT section.  */
01972 
01973       bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset);
01974       value = _bfd_get_gp_value (splt->output_section->owner);
01975       bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset + 0x8);
01976 
01977       /* Create a dynamic IPLT relocation for this entry.
01978 
01979         We are creating a relocation in the output file's PLT section,
01980         which is included within the DLT secton.  So we do need to include
01981         the PLT's output_offset in the computation of the relocation's
01982         address.  */
01983       rel.r_offset = (dyn_h->plt_offset + splt->output_offset
01984                     + splt->output_section->vma);
01985       rel.r_info = ELF64_R_INFO (h->dynindx, R_PARISC_IPLT);
01986       rel.r_addend = 0;
01987 
01988       loc = spltrel->contents;
01989       loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
01990       bfd_elf64_swap_reloca_out (splt->output_section->owner, &rel, loc);
01991     }
01992 
01993   /* Initialize an external call stub entry if requested.  */
01994   if (dyn_h && dyn_h->want_stub
01995       && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
01996     {
01997       bfd_vma value;
01998       int insn;
01999       unsigned int max_offset;
02000 
02001       BFD_ASSERT (stub != NULL);
02002 
02003       /* Install the generic stub template.
02004 
02005         We are modifying the contents of the stub section, so we do not
02006         need to include the stub section's output_offset here.  */
02007       memcpy (stub->contents + dyn_h->stub_offset, plt_stub, sizeof (plt_stub));
02008 
02009       /* Fix up the first ldd instruction.
02010 
02011         We are modifying the contents of the STUB section in memory,
02012         so we do not need to include its output offset in this computation.
02013 
02014         Note the plt_offset value is the value of the PLT entry relative to
02015         the start of the PLT section.  These instructions will reference
02016         data relative to the value of __gp, which may not necessarily have
02017         the same address as the start of the PLT section.
02018 
02019         gp_offset contains the offset of __gp within the PLT section.  */
02020       value = dyn_h->plt_offset - hppa_info->gp_offset;
02021 
02022       insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset);
02023       if (output_bfd->arch_info->mach >= 25)
02024        {
02025          /* Wide mode allows 16 bit offsets.  */
02026          max_offset = 32768;
02027          insn &= ~ 0xfff1;
02028          insn |= re_assemble_16 ((int) value);
02029        }
02030       else
02031        {
02032          max_offset = 8192;
02033          insn &= ~ 0x3ff1;
02034          insn |= re_assemble_14 ((int) value);
02035        }
02036 
02037       if ((value & 7) || value + max_offset >= 2*max_offset - 8)
02038        {
02039          (*_bfd_error_handler) (_("stub entry for %s cannot load .plt, dp offset = %ld"),
02040                              dyn_h->root.string,
02041                              (long) value);
02042          return FALSE;
02043        }
02044 
02045       bfd_put_32 (stub->owner, (bfd_vma) insn,
02046                 stub->contents + dyn_h->stub_offset);
02047 
02048       /* Fix up the second ldd instruction.  */
02049       value += 8;
02050       insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset + 8);
02051       if (output_bfd->arch_info->mach >= 25)
02052        {
02053          insn &= ~ 0xfff1;
02054          insn |= re_assemble_16 ((int) value);
02055        }
02056       else
02057        {
02058          insn &= ~ 0x3ff1;
02059          insn |= re_assemble_14 ((int) value);
02060        }
02061       bfd_put_32 (stub->owner, (bfd_vma) insn,
02062                 stub->contents + dyn_h->stub_offset + 8);
02063     }
02064 
02065   return TRUE;
02066 }
02067 
02068 /* The .opd section contains FPTRs for each function this file
02069    exports.  Initialize the FPTR entries.  */
02070 
02071 static bfd_boolean
02072 elf64_hppa_finalize_opd (dyn_h, data)
02073      struct elf64_hppa_dyn_hash_entry *dyn_h;
02074      PTR data;
02075 {
02076   struct bfd_link_info *info = (struct bfd_link_info *)data;
02077   struct elf64_hppa_link_hash_table *hppa_info;
02078   struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
02079   asection *sopd;
02080   asection *sopdrel;
02081 
02082   hppa_info = elf64_hppa_hash_table (info);
02083   sopd = hppa_info->opd_sec;
02084   sopdrel = hppa_info->opd_rel_sec;
02085 
02086   if (h && dyn_h->want_opd)
02087     {
02088       bfd_vma value;
02089 
02090       /* The first two words of an .opd entry are zero.
02091 
02092         We are modifying the contents of the OPD section in memory, so we
02093         do not need to include its output offset in this computation.  */
02094       memset (sopd->contents + dyn_h->opd_offset, 0, 16);
02095 
02096       value = (h->root.u.def.value
02097               + h->root.u.def.section->output_section->vma
02098               + h->root.u.def.section->output_offset);
02099 
02100       /* The next word is the address of the function.  */
02101       bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 16);
02102 
02103       /* The last word is our local __gp value.  */
02104       value = _bfd_get_gp_value (sopd->output_section->owner);
02105       bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 24);
02106     }
02107 
02108   /* If we are generating a shared library, we must generate EPLT relocations
02109      for each entry in the .opd, even for static functions (they may have
02110      had their address taken).  */
02111   if (info->shared && dyn_h && dyn_h->want_opd)
02112     {
02113       Elf_Internal_Rela rel;
02114       bfd_byte *loc;
02115       int dynindx;
02116 
02117       /* We may need to do a relocation against a local symbol, in
02118         which case we have to look up it's dynamic symbol index off
02119         the local symbol hash table.  */
02120       if (h && h->dynindx != -1)
02121        dynindx = h->dynindx;
02122       else
02123        dynindx
02124          = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
02125                                           dyn_h->sym_indx);
02126 
02127       /* The offset of this relocation is the absolute address of the
02128         .opd entry for this symbol.  */
02129       rel.r_offset = (dyn_h->opd_offset + sopd->output_offset
02130                     + sopd->output_section->vma);
02131 
02132       /* If H is non-null, then we have an external symbol.
02133 
02134         It is imperative that we use a different dynamic symbol for the
02135         EPLT relocation if the symbol has global scope.
02136 
02137         In the dynamic symbol table, the function symbol will have a value
02138         which is address of the function's .opd entry.
02139 
02140         Thus, we can not use that dynamic symbol for the EPLT relocation
02141         (if we did, the data in the .opd would reference itself rather
02142         than the actual address of the function).  Instead we have to use
02143         a new dynamic symbol which has the same value as the original global
02144         function symbol.
02145 
02146         We prefix the original symbol with a "." and use the new symbol in
02147         the EPLT relocation.  This new symbol has already been recorded in
02148         the symbol table, we just have to look it up and use it.
02149 
02150         We do not have such problems with static functions because we do
02151         not make their addresses in the dynamic symbol table point to
02152         the .opd entry.  Ultimately this should be safe since a static
02153         function can not be directly referenced outside of its shared
02154         library.
02155 
02156         We do have to play similar games for FPTR relocations in shared
02157         libraries, including those for static symbols.  See the FPTR
02158         handling in elf64_hppa_finalize_dynreloc.  */
02159       if (h)
02160        {
02161          char *new_name;
02162          struct elf_link_hash_entry *nh;
02163 
02164          new_name = alloca (strlen (h->root.root.string) + 2);
02165          new_name[0] = '.';
02166          strcpy (new_name + 1, h->root.root.string);
02167 
02168          nh = elf_link_hash_lookup (elf_hash_table (info),
02169                                  new_name, FALSE, FALSE, FALSE);
02170 
02171          /* All we really want from the new symbol is its dynamic
02172             symbol index.  */
02173          dynindx = nh->dynindx;
02174        }
02175 
02176       rel.r_addend = 0;
02177       rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
02178 
02179       loc = sopdrel->contents;
02180       loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
02181       bfd_elf64_swap_reloca_out (sopd->output_section->owner, &rel, loc);
02182     }
02183   return TRUE;
02184 }
02185 
02186 /* The .dlt section contains addresses for items referenced through the
02187    dlt.  Note that we can have a DLTIND relocation for a local symbol, thus
02188    we can not depend on finish_dynamic_symbol to initialize the .dlt.  */
02189 
02190 static bfd_boolean
02191 elf64_hppa_finalize_dlt (dyn_h, data)
02192      struct elf64_hppa_dyn_hash_entry *dyn_h;
02193      PTR data;
02194 {
02195   struct bfd_link_info *info = (struct bfd_link_info *)data;
02196   struct elf64_hppa_link_hash_table *hppa_info;
02197   asection *sdlt, *sdltrel;
02198   struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
02199 
02200   hppa_info = elf64_hppa_hash_table (info);
02201 
02202   sdlt = hppa_info->dlt_sec;
02203   sdltrel = hppa_info->dlt_rel_sec;
02204 
02205   /* H/DYN_H may refer to a local variable and we know it's
02206      address, so there is no need to create a relocation.  Just install
02207      the proper value into the DLT, note this shortcut can not be
02208      skipped when building a shared library.  */
02209   if (! info->shared && h && dyn_h->want_dlt)
02210     {
02211       bfd_vma value;
02212 
02213       /* If we had an LTOFF_FPTR style relocation we want the DLT entry
02214         to point to the FPTR entry in the .opd section.
02215 
02216         We include the OPD's output offset in this computation as
02217         we are referring to an absolute address in the resulting
02218         object file.  */
02219       if (dyn_h->want_opd)
02220        {
02221          value = (dyn_h->opd_offset
02222                  + hppa_info->opd_sec->output_offset
02223                  + hppa_info->opd_sec->output_section->vma);
02224        }
02225       else if ((h->root.type == bfd_link_hash_defined
02226               || h->root.type == bfd_link_hash_defweak)
02227               && h->root.u.def.section)
02228        {
02229          value = h->root.u.def.value + h->root.u.def.section->output_offset;
02230          if (h->root.u.def.section->output_section)
02231            value += h->root.u.def.section->output_section->vma;
02232          else
02233            value += h->root.u.def.section->vma;
02234        }
02235       else
02236        /* We have an undefined function reference.  */
02237        value = 0;
02238 
02239       /* We do not need to include the output offset of the DLT section
02240         here because we are modifying the in-memory contents.  */
02241       bfd_put_64 (sdlt->owner, value, sdlt->contents + dyn_h->dlt_offset);
02242     }
02243 
02244   /* Create a relocation for the DLT entry associated with this symbol.
02245      When building a shared library the symbol does not have to be dynamic.  */
02246   if (dyn_h->want_dlt
02247       && (elf64_hppa_dynamic_symbol_p (dyn_h->h, info) || info->shared))
02248     {
02249       Elf_Internal_Rela rel;
02250       bfd_byte *loc;
02251       int dynindx;
02252 
02253       /* We may need to do a relocation against a local symbol, in
02254         which case we have to look up it's dynamic symbol index off
02255         the local symbol hash table.  */
02256       if (h && h->dynindx != -1)
02257        dynindx = h->dynindx;
02258       else
02259        dynindx
02260          = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
02261                                           dyn_h->sym_indx);
02262 
02263       /* Create a dynamic relocation for this entry.  Do include the output
02264         offset of the DLT entry since we need an absolute address in the
02265         resulting object file.  */
02266       rel.r_offset = (dyn_h->dlt_offset + sdlt->output_offset
02267                     + sdlt->output_section->vma);
02268       if (h && h->type == STT_FUNC)
02269          rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
02270       else
02271          rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
02272       rel.r_addend = 0;
02273 
02274       loc = sdltrel->contents;
02275       loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
02276       bfd_elf64_swap_reloca_out (sdlt->output_section->owner, &rel, loc);
02277     }
02278   return TRUE;
02279 }
02280 
02281 /* Finalize the dynamic relocations.  Specifically the FPTR relocations
02282    for dynamic functions used to initialize static data.  */
02283 
02284 static bfd_boolean
02285 elf64_hppa_finalize_dynreloc (dyn_h, data)
02286      struct elf64_hppa_dyn_hash_entry *dyn_h;
02287      PTR data;
02288 {
02289   struct bfd_link_info *info = (struct bfd_link_info *)data;
02290   struct elf64_hppa_link_hash_table *hppa_info;
02291   struct elf_link_hash_entry *h;
02292   int dynamic_symbol;
02293 
02294   dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, info);
02295 
02296   if (!dynamic_symbol && !info->shared)
02297     return TRUE;
02298 
02299   if (dyn_h->reloc_entries)
02300     {
02301       struct elf64_hppa_dyn_reloc_entry *rent;
02302       int dynindx;
02303 
02304       hppa_info = elf64_hppa_hash_table (info);
02305       h = dyn_h->h;
02306 
02307       /* We may need to do a relocation against a local symbol, in
02308         which case we have to look up it's dynamic symbol index off
02309         the local symbol hash table.  */
02310       if (h && h->dynindx != -1)
02311        dynindx = h->dynindx;
02312       else
02313        dynindx
02314          = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
02315                                           dyn_h->sym_indx);
02316 
02317       for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
02318        {
02319          Elf_Internal_Rela rel;
02320          bfd_byte *loc;
02321 
02322          /* Allocate one iff we are building a shared library, the relocation
02323             isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
02324          if (!info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
02325            continue;
02326 
02327          /* Create a dynamic relocation for this entry.
02328 
02329             We need the output offset for the reloc's section because
02330             we are creating an absolute address in the resulting object
02331             file.  */
02332          rel.r_offset = (rent->offset + rent->sec->output_offset
02333                        + rent->sec->output_section->vma);
02334 
02335          /* An FPTR64 relocation implies that we took the address of
02336             a function and that the function has an entry in the .opd
02337             section.  We want the FPTR64 relocation to reference the
02338             entry in .opd.
02339 
02340             We could munge the symbol value in the dynamic symbol table
02341             (in fact we already do for functions with global scope) to point
02342             to the .opd entry.  Then we could use that dynamic symbol in
02343             this relocation.
02344 
02345             Or we could do something sensible, not munge the symbol's
02346             address and instead just use a different symbol to reference
02347             the .opd entry.  At least that seems sensible until you
02348             realize there's no local dynamic symbols we can use for that
02349             purpose.  Thus the hair in the check_relocs routine.
02350 
02351             We use a section symbol recorded by check_relocs as the
02352             base symbol for the relocation.  The addend is the difference
02353             between the section symbol and the address of the .opd entry.  */
02354          if (info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
02355            {
02356              bfd_vma value, value2;
02357 
02358              /* First compute the address of the opd entry for this symbol.  */
02359              value = (dyn_h->opd_offset
02360                      + hppa_info->opd_sec->output_section->vma
02361                      + hppa_info->opd_sec->output_offset);
02362 
02363              /* Compute the value of the start of the section with
02364                the relocation.  */
02365              value2 = (rent->sec->output_section->vma
02366                      + rent->sec->output_offset);
02367 
02368              /* Compute the difference between the start of the section
02369                with the relocation and the opd entry.  */
02370              value -= value2;
02371 
02372              /* The result becomes the addend of the relocation.  */
02373              rel.r_addend = value;
02374 
02375              /* The section symbol becomes the symbol for the dynamic
02376                relocation.  */
02377              dynindx
02378               = _bfd_elf_link_lookup_local_dynindx (info,
02379                                                 rent->sec->owner,
02380                                                 rent->sec_symndx);
02381            }
02382          else
02383            rel.r_addend = rent->addend;
02384 
02385          rel.r_info = ELF64_R_INFO (dynindx, rent->type);
02386 
02387          loc = hppa_info->other_rel_sec->contents;
02388          loc += (hppa_info->other_rel_sec->reloc_count++
02389                 * sizeof (Elf64_External_Rela));
02390          bfd_elf64_swap_reloca_out (hppa_info->other_rel_sec->output_section->owner,
02391                                  &rel, loc);
02392        }
02393     }
02394 
02395   return TRUE;
02396 }
02397 
02398 /* Used to decide how to sort relocs in an optimal manner for the
02399    dynamic linker, before writing them out.  */
02400 
02401 static enum elf_reloc_type_class
02402 elf64_hppa_reloc_type_class (rela)
02403      const Elf_Internal_Rela *rela;
02404 {
02405   if (ELF64_R_SYM (rela->r_info) == 0)
02406     return reloc_class_relative;
02407 
02408   switch ((int) ELF64_R_TYPE (rela->r_info))
02409     {
02410     case R_PARISC_IPLT:
02411       return reloc_class_plt;
02412     case R_PARISC_COPY:
02413       return reloc_class_copy;
02414     default:
02415       return reloc_class_normal;
02416     }
02417 }
02418 
02419 /* Finish up the dynamic sections.  */
02420 
02421 static bfd_boolean
02422 elf64_hppa_finish_dynamic_sections (output_bfd, info)
02423      bfd *output_bfd;
02424      struct bfd_link_info *info;
02425 {
02426   bfd *dynobj;
02427   asection *sdyn;
02428   struct elf64_hppa_link_hash_table *hppa_info;
02429 
02430   hppa_info = elf64_hppa_hash_table (info);
02431 
02432   /* Finalize the contents of the .opd section.  */
02433   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
02434                             elf64_hppa_finalize_opd,
02435                             info);
02436 
02437   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
02438                             elf64_hppa_finalize_dynreloc,
02439                             info);
02440 
02441   /* Finalize the contents of the .dlt section.  */
02442   dynobj = elf_hash_table (info)->dynobj;
02443   /* Finalize the contents of the .dlt section.  */
02444   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
02445                             elf64_hppa_finalize_dlt,
02446                             info);
02447 
02448   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
02449 
02450   if (elf_hash_table (info)->dynamic_sections_created)
02451     {
02452       Elf64_External_Dyn *dyncon, *dynconend;
02453 
02454       BFD_ASSERT (sdyn != NULL);
02455 
02456       dyncon = (Elf64_External_Dyn *) sdyn->contents;
02457       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
02458       for (; dyncon < dynconend; dyncon++)
02459        {
02460          Elf_Internal_Dyn dyn;
02461          asection *s;
02462 
02463          bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
02464 
02465          switch (dyn.d_tag)
02466            {
02467            default:
02468              break;
02469 
02470            case DT_HP_LOAD_MAP:
02471              /* Compute the absolute address of 16byte scratchpad area
02472                for the dynamic linker.
02473 
02474                By convention the linker script will allocate the scratchpad
02475                area at the start of the .data section.  So all we have to
02476                to is find the start of the .data section.  */
02477              s = bfd_get_section_by_name (output_bfd, ".data");
02478              dyn.d_un.d_ptr = s->vma;
02479              bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
02480              break;
02481 
02482            case DT_PLTGOT:
02483              /* HP's use PLTGOT to set the GOT register.  */
02484              dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
02485              bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
02486              break;
02487 
02488            case DT_JMPREL:
02489              s = hppa_info->plt_rel_sec;
02490              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
02491              bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
02492              break;
02493 
02494            case DT_PLTRELSZ:
02495              s = hppa_info->plt_rel_sec;
02496              dyn.d_un.d_val = s->size;
02497              bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
02498              break;
02499 
02500            case DT_RELA:
02501              s = hppa_info->other_rel_sec;
02502              if (! s || ! s->size)
02503               s = hppa_info->dlt_rel_sec;
02504              if (! s || ! s->size)
02505               s = hppa_info->opd_rel_sec;
02506              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
02507              bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
02508              break;
02509 
02510            case DT_RELASZ:
02511              s = hppa_info->other_rel_sec;
02512              dyn.d_un.d_val = s->size;
02513              s = hppa_info->dlt_rel_sec;
02514              dyn.d_un.d_val += s->size;
02515              s = hppa_info->opd_rel_sec;
02516              dyn.d_un.d_val += s->size;
02517              /* There is some question about whether or not the size of
02518                the PLT relocs should be included here.  HP's tools do
02519                it, so we'll emulate them.  */
02520              s = hppa_info->plt_rel_sec;
02521              dyn.d_un.d_val += s->size;
02522              bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
02523              break;
02524 
02525            }
02526        }
02527     }
02528 
02529   return TRUE;
02530 }
02531 
02532 /* Support for core dump NOTE sections.  */
02533 
02534 static bfd_boolean
02535 elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
02536 {
02537   int offset;
02538   size_t size;
02539 
02540   switch (note->descsz)
02541     {
02542       default:
02543        return FALSE;
02544 
02545       case 760:             /* Linux/hppa */
02546        /* pr_cursig */
02547        elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
02548 
02549        /* pr_pid */
02550        elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
02551 
02552        /* pr_reg */
02553        offset = 112;
02554        size = 640;
02555 
02556        break;
02557     }
02558 
02559   /* Make a ".reg/999" section.  */
02560   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
02561                                      size, note->descpos + offset);
02562 }
02563 
02564 static bfd_boolean
02565 elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
02566 {
02567   char * command;
02568   int n;
02569 
02570   switch (note->descsz)
02571     {
02572     default:
02573       return FALSE;
02574 
02575     case 136:        /* Linux/hppa elf_prpsinfo.  */
02576       elf_tdata (abfd)->core_program
02577        = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
02578       elf_tdata (abfd)->core_command
02579        = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
02580     }
02581 
02582   /* Note that for some reason, a spurious space is tacked
02583      onto the end of the args in some (at least one anyway)
02584      implementations, so strip it off if it exists.  */
02585   command = elf_tdata (abfd)->core_command;
02586   n = strlen (command);
02587 
02588   if (0 < n && command[n - 1] == ' ')
02589     command[n - 1] = '\0';
02590 
02591   return TRUE;
02592 }
02593 
02594 /* Return the number of additional phdrs we will need.
02595 
02596    The generic ELF code only creates PT_PHDRs for executables.  The HP
02597    dynamic linker requires PT_PHDRs for dynamic libraries too.
02598 
02599    This routine indicates that the backend needs one additional program
02600    header for that case.
02601 
02602    Note we do not have access to the link info structure here, so we have
02603    to guess whether or not we are building a shared library based on the
02604    existence of a .interp section.  */
02605 
02606 static int
02607 elf64_hppa_additional_program_headers (bfd *abfd,
02608                                    struct bfd_link_info *info ATTRIBUTE_UNUSED)
02609 {
02610   asection *s;
02611 
02612   /* If we are creating a shared library, then we have to create a
02613      PT_PHDR segment.  HP's dynamic linker chokes without it.  */
02614   s = bfd_get_section_by_name (abfd, ".interp");
02615   if (! s)
02616     return 1;
02617   return 0;
02618 }
02619 
02620 /* Allocate and initialize any program headers required by this
02621    specific backend.
02622 
02623    The generic ELF code only creates PT_PHDRs for executables.  The HP
02624    dynamic linker requires PT_PHDRs for dynamic libraries too.
02625 
02626    This allocates the PT_PHDR and initializes it in a manner suitable
02627    for the HP linker.
02628 
02629    Note we do not have access to the link info structure here, so we have
02630    to guess whether or not we are building a shared library based on the
02631    existence of a .interp section.  */
02632 
02633 static bfd_boolean
02634 elf64_hppa_modify_segment_map (bfd *abfd,
02635                             struct bfd_link_info *info ATTRIBUTE_UNUSED)
02636 {
02637   struct elf_segment_map *m;
02638   asection *s;
02639 
02640   s = bfd_get_section_by_name (abfd, ".interp");
02641   if (! s)
02642     {
02643       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
02644        if (m->p_type == PT_PHDR)
02645          break;
02646       if (m == NULL)
02647        {
02648          m = ((struct elf_segment_map *)
02649               bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
02650          if (m == NULL)
02651            return FALSE;
02652 
02653          m->p_type = PT_PHDR;
02654          m->p_flags = PF_R | PF_X;
02655          m->p_flags_valid = 1;
02656          m->p_paddr_valid = 1;
02657          m->includes_phdrs = 1;
02658 
02659          m->next = elf_tdata (abfd)->segment_map;
02660          elf_tdata (abfd)->segment_map = m;
02661        }
02662     }
02663 
02664   for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
02665     if (m->p_type == PT_LOAD)
02666       {
02667        unsigned int i;
02668 
02669        for (i = 0; i < m->count; i++)
02670          {
02671            /* The code "hint" is not really a hint.  It is a requirement
02672               for certain versions of the HP dynamic linker.  Worse yet,
02673               it must be set even if the shared library does not have
02674               any code in its "text" segment (thus the check for .hash
02675               to catch this situation).  */
02676            if (m->sections[i]->flags & SEC_CODE
02677               || (strcmp (m->sections[i]->name, ".hash") == 0))
02678              m->p_flags |= (PF_X | PF_HP_CODE);
02679          }
02680       }
02681 
02682   return TRUE;
02683 }
02684 
02685 /* Called when writing out an object file to decide the type of a
02686    symbol.  */
02687 static int
02688 elf64_hppa_elf_get_symbol_type (elf_sym, type)
02689      Elf_Internal_Sym *elf_sym;
02690      int type;
02691 {
02692   if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
02693     return STT_PARISC_MILLI;
02694   else
02695     return type;
02696 }
02697 
02698 /* Support HP specific sections for core files.  */
02699 static bfd_boolean
02700 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index,
02701                            const char *typename)
02702 {
02703   if (hdr->p_type == PT_HP_CORE_KERNEL)
02704     {
02705       asection *sect;
02706 
02707       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
02708        return FALSE;
02709 
02710       sect = bfd_make_section_anyway (abfd, ".kernel");
02711       if (sect == NULL)
02712        return FALSE;
02713       sect->size = hdr->p_filesz;
02714       sect->filepos = hdr->p_offset;
02715       sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
02716       return TRUE;
02717     }
02718 
02719   if (hdr->p_type == PT_HP_CORE_PROC)
02720     {
02721       int sig;
02722 
02723       if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
02724        return FALSE;
02725       if (bfd_bread (&sig, 4, abfd) != 4)
02726        return FALSE;
02727 
02728       elf_tdata (abfd)->core_signal = sig;
02729 
02730       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
02731        return FALSE;
02732 
02733       /* GDB uses the ".reg" section to read register contents.  */
02734       return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
02735                                          hdr->p_offset);
02736     }
02737 
02738   if (hdr->p_type == PT_HP_CORE_LOADABLE
02739       || hdr->p_type == PT_HP_CORE_STACK
02740       || hdr->p_type == PT_HP_CORE_MMF)
02741     hdr->p_type = PT_LOAD;
02742 
02743   return _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename);
02744 }
02745 
02746 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
02747 {
02748   { STRING_COMMA_LEN (".fini"),  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
02749   { STRING_COMMA_LEN (".init"),  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
02750   { STRING_COMMA_LEN (".plt"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
02751   { STRING_COMMA_LEN (".dlt"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
02752   { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
02753   { STRING_COMMA_LEN (".sbss"),  0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
02754   { STRING_COMMA_LEN (".tbss"),  0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
02755   { NULL,                    0,  0, 0,            0 }
02756 };
02757 
02758 /* The hash bucket size is the standard one, namely 4.  */
02759 
02760 const struct elf_size_info hppa64_elf_size_info =
02761 {
02762   sizeof (Elf64_External_Ehdr),
02763   sizeof (Elf64_External_Phdr),
02764   sizeof (Elf64_External_Shdr),
02765   sizeof (Elf64_External_Rel),
02766   sizeof (Elf64_External_Rela),
02767   sizeof (Elf64_External_Sym),
02768   sizeof (Elf64_External_Dyn),
02769   sizeof (Elf_External_Note),
02770   4,
02771   1,
02772   64, 3,
02773   ELFCLASS64, EV_CURRENT,
02774   bfd_elf64_write_out_phdrs,
02775   bfd_elf64_write_shdrs_and_ehdr,
02776   bfd_elf64_write_relocs,
02777   bfd_elf64_swap_symbol_in,
02778   bfd_elf64_swap_symbol_out,
02779   bfd_elf64_slurp_reloc_table,
02780   bfd_elf64_slurp_symbol_table,
02781   bfd_elf64_swap_dyn_in,
02782   bfd_elf64_swap_dyn_out,
02783   bfd_elf64_swap_reloc_in,
02784   bfd_elf64_swap_reloc_out,
02785   bfd_elf64_swap_reloca_in,
02786   bfd_elf64_swap_reloca_out
02787 };
02788 
02789 #define TARGET_BIG_SYM                    bfd_elf64_hppa_vec
02790 #define TARGET_BIG_NAME                   "elf64-hppa"
02791 #define ELF_ARCH                   bfd_arch_hppa
02792 #define ELF_MACHINE_CODE           EM_PARISC
02793 /* This is not strictly correct.  The maximum page size for PA2.0 is
02794    64M.  But everything still uses 4k.  */
02795 #define ELF_MAXPAGESIZE                   0x1000
02796 #define ELF_OSABI                  ELFOSABI_HPUX
02797 
02798 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
02799 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
02800 #define bfd_elf64_bfd_is_local_label_name       elf_hppa_is_local_label_name
02801 #define elf_info_to_howto          elf_hppa_info_to_howto
02802 #define elf_info_to_howto_rel             elf_hppa_info_to_howto_rel
02803 
02804 #define elf_backend_section_from_shdr     elf64_hppa_section_from_shdr
02805 #define elf_backend_object_p              elf64_hppa_object_p
02806 #define elf_backend_final_write_processing \
02807                                    elf_hppa_final_write_processing
02808 #define elf_backend_fake_sections  elf_hppa_fake_sections
02809 #define elf_backend_add_symbol_hook       elf_hppa_add_symbol_hook
02810 
02811 #define elf_backend_relocate_section      elf_hppa_relocate_section
02812 
02813 #define bfd_elf64_bfd_final_link   elf_hppa_final_link
02814 
02815 #define elf_backend_create_dynamic_sections \
02816                                    elf64_hppa_create_dynamic_sections
02817 #define elf_backend_post_process_headers  elf64_hppa_post_process_headers
02818 
02819 #define elf_backend_omit_section_dynsym \
02820   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
02821 #define elf_backend_adjust_dynamic_symbol \
02822                                    elf64_hppa_adjust_dynamic_symbol
02823 
02824 #define elf_backend_size_dynamic_sections \
02825                                    elf64_hppa_size_dynamic_sections
02826 
02827 #define elf_backend_finish_dynamic_symbol \
02828                                    elf64_hppa_finish_dynamic_symbol
02829 #define elf_backend_finish_dynamic_sections \
02830                                    elf64_hppa_finish_dynamic_sections
02831 #define elf_backend_grok_prstatus  elf64_hppa_grok_prstatus
02832 #define elf_backend_grok_psinfo           elf64_hppa_grok_psinfo
02833  
02834 /* Stuff for the BFD linker: */
02835 #define bfd_elf64_bfd_link_hash_table_create \
02836        elf64_hppa_hash_table_create
02837 
02838 #define elf_backend_check_relocs \
02839        elf64_hppa_check_relocs
02840 
02841 #define elf_backend_size_info \
02842   hppa64_elf_size_info
02843 
02844 #define elf_backend_additional_program_headers \
02845        elf64_hppa_additional_program_headers
02846 
02847 #define elf_backend_modify_segment_map \
02848        elf64_hppa_modify_segment_map
02849 
02850 #define elf_backend_link_output_symbol_hook \
02851        elf64_hppa_link_output_symbol_hook
02852 
02853 #define elf_backend_want_got_plt   0
02854 #define elf_backend_plt_readonly   0
02855 #define elf_backend_want_plt_sym   0
02856 #define elf_backend_got_header_size     0
02857 #define elf_backend_type_change_ok TRUE
02858 #define elf_backend_get_symbol_type       elf64_hppa_elf_get_symbol_type
02859 #define elf_backend_reloc_type_class      elf64_hppa_reloc_type_class
02860 #define elf_backend_rela_normal           1
02861 #define elf_backend_special_sections      elf64_hppa_special_sections
02862 #define elf_backend_action_discarded      elf_hppa_action_discarded
02863 #define elf_backend_section_from_phdr   elf64_hppa_section_from_phdr
02864 
02865 #define elf64_bed                  elf64_hppa_hpux_bed
02866 
02867 #include "elf64-target.h"
02868 
02869 #undef TARGET_BIG_SYM
02870 #define TARGET_BIG_SYM                    bfd_elf64_hppa_linux_vec
02871 #undef TARGET_BIG_NAME
02872 #define TARGET_BIG_NAME                   "elf64-hppa-linux"
02873 #undef ELF_OSABI
02874 #define ELF_OSABI                  ELFOSABI_LINUX
02875 #undef elf_backend_post_process_headers
02876 #define elf_backend_post_process_headers _bfd_elf_set_osabi
02877 #undef elf64_bed
02878 #define elf64_bed                  elf64_hppa_linux_bed
02879 
02880 #include "elf64-target.h"