Back to index

cell-binutils  2.17cvs20070401
elf32-hppa.c
Go to the documentation of this file.
00001 /* BFD back-end for HP PA-RISC ELF files.
00002    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
00003    2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
00004 
00005    Original code by
00006        Center for Software Science
00007        Department of Computer Science
00008        University of Utah
00009    Largely rewritten by Alan Modra <alan@linuxcare.com.au>
00010    Naming cleanup by Carlos O'Donell <carlos@systemhalted.org>
00011    TLS support written by Randolph Chung <tausq@debian.org>
00012  
00013    This file is part of BFD, the Binary File Descriptor library.
00014 
00015    This program is free software; you can redistribute it and/or modify
00016    it under the terms of the GNU General Public License as published by
00017    the Free Software Foundation; either version 2 of the License, or
00018    (at your option) any later version.
00019 
00020    This program is distributed in the hope that it will be useful,
00021    but WITHOUT ANY WARRANTY; without even the implied warranty of
00022    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023    GNU General Public License for more details.
00024 
00025    You should have received a copy of the GNU General Public License
00026    along with this program; if not, write to the Free Software
00027    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00028 
00029 #include "bfd.h"
00030 #include "sysdep.h"
00031 #include "libbfd.h"
00032 #include "elf-bfd.h"
00033 #include "elf/hppa.h"
00034 #include "libhppa.h"
00035 #include "elf32-hppa.h"
00036 #define ARCH_SIZE           32
00037 #include "elf32-hppa.h"
00038 #include "elf-hppa.h"
00039 
00040 /* In order to gain some understanding of code in this file without
00041    knowing all the intricate details of the linker, note the
00042    following:
00043 
00044    Functions named elf32_hppa_* are called by external routines, other
00045    functions are only called locally.  elf32_hppa_* functions appear
00046    in this file more or less in the order in which they are called
00047    from external routines.  eg. elf32_hppa_check_relocs is called
00048    early in the link process, elf32_hppa_finish_dynamic_sections is
00049    one of the last functions.  */
00050 
00051 /* We use two hash tables to hold information for linking PA ELF objects.
00052 
00053    The first is the elf32_hppa_link_hash_table which is derived
00054    from the standard ELF linker hash table.  We use this as a place to
00055    attach other hash tables and static information.
00056 
00057    The second is the stub hash table which is derived from the
00058    base BFD hash table.  The stub hash table holds the information
00059    necessary to build the linker stubs during a link.
00060 
00061    There are a number of different stubs generated by the linker.
00062 
00063    Long branch stub:
00064    :          ldil LR'X,%r1
00065    :          be,n RR'X(%sr4,%r1)
00066 
00067    PIC long branch stub:
00068    :          b,l .+8,%r1
00069    :          addil LR'X - ($PIC_pcrel$0 - 4),%r1
00070    :          be,n RR'X - ($PIC_pcrel$0 - 8)(%sr4,%r1)
00071 
00072    Import stub to call shared library routine from normal object file
00073    (single sub-space version)
00074    :          addil LR'lt_ptr+ltoff,%dp   ; get procedure entry point
00075    :          ldw RR'lt_ptr+ltoff(%r1),%r21
00076    :          bv %r0(%r21)
00077    :          ldw RR'lt_ptr+ltoff+4(%r1),%r19    ; get new dlt value.
00078 
00079    Import stub to call shared library routine from shared library
00080    (single sub-space version)
00081    :          addil LR'ltoff,%r19         ; get procedure entry point
00082    :          ldw RR'ltoff(%r1),%r21
00083    :          bv %r0(%r21)
00084    :          ldw RR'ltoff+4(%r1),%r19    ; get new dlt value.
00085 
00086    Import stub to call shared library routine from normal object file
00087    (multiple sub-space support)
00088    :          addil LR'lt_ptr+ltoff,%dp   ; get procedure entry point
00089    :          ldw RR'lt_ptr+ltoff(%r1),%r21
00090    :          ldw RR'lt_ptr+ltoff+4(%r1),%r19    ; get new dlt value.
00091    :          ldsid (%r21),%r1
00092    :          mtsp %r1,%sr0
00093    :          be 0(%sr0,%r21)                    ; branch to target
00094    :          stw %rp,-24(%sp)            ; save rp
00095 
00096    Import stub to call shared library routine from shared library
00097    (multiple sub-space support)
00098    :          addil LR'ltoff,%r19         ; get procedure entry point
00099    :          ldw RR'ltoff(%r1),%r21
00100    :          ldw RR'ltoff+4(%r1),%r19    ; get new dlt value.
00101    :          ldsid (%r21),%r1
00102    :          mtsp %r1,%sr0
00103    :          be 0(%sr0,%r21)                    ; branch to target
00104    :          stw %rp,-24(%sp)            ; save rp
00105 
00106    Export stub to return from shared lib routine (multiple sub-space support)
00107    One of these is created for each exported procedure in a shared
00108    library (and stored in the shared lib).  Shared lib routines are
00109    called via the first instruction in the export stub so that we can
00110    do an inter-space return.  Not required for single sub-space.
00111    :          bl,n X,%rp                  ; trap the return
00112    :          nop
00113    :          ldw -24(%sp),%rp            ; restore the original rp
00114    :          ldsid (%rp),%r1
00115    :          mtsp %r1,%sr0
00116    :          be,n 0(%sr0,%rp)            ; inter-space return.  */
00117 
00118 
00119 /* Variable names follow a coding style.
00120    Please follow this (Apps Hungarian) style:
00121 
00122    Structure/Variable                     Prefix
00123    elf_link_hash_table                    "etab"
00124    elf_link_hash_entry                    "eh"
00125    
00126    elf32_hppa_link_hash_table             "htab"
00127    elf32_hppa_link_hash_entry             "hh"
00128 
00129    bfd_hash_table                  "btab"
00130    bfd_hash_entry                  "bh"
00131    
00132    bfd_hash_table containing stubs "bstab"
00133    elf32_hppa_stub_hash_entry             "hsh"
00134 
00135    elf32_hppa_dyn_reloc_entry             "hdh"
00136    
00137    Always remember to use GNU Coding Style. */
00138                                      
00139 #define PLT_ENTRY_SIZE 8
00140 #define GOT_ENTRY_SIZE 4
00141 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
00142 
00143 static const bfd_byte plt_stub[] =
00144 {
00145   0x0e, 0x80, 0x10, 0x96,  /* 1: ldw      0(%r20),%r22         */
00146   0xea, 0xc0, 0xc0, 0x00,  /*    bv       %r0(%r22)            */
00147   0x0e, 0x88, 0x10, 0x95,  /*    ldw      4(%r20),%r21         */
00148 #define PLT_STUB_ENTRY (3*4)
00149   0xea, 0x9f, 0x1f, 0xdd,  /*    b,l      1b,%r20                     */
00150   0xd6, 0x80, 0x1c, 0x1e,  /*    depi     0,31,2,%r20          */
00151   0x00, 0xc0, 0xff, 0xee,  /* 9: .word    fixup_func           */
00152   0xde, 0xad, 0xbe, 0xef   /*    .word    fixup_ltp            */
00153 };
00154 
00155 /* Section name for stubs is the associated section name plus this
00156    string.  */
00157 #define STUB_SUFFIX ".stub"
00158 
00159 /* We don't need to copy certain PC- or GP-relative dynamic relocs
00160    into a shared object's dynamic section.  All the relocs of the
00161    limited class we are interested in, are absolute.  */
00162 #ifndef RELATIVE_DYNRELOCS
00163 #define RELATIVE_DYNRELOCS 0
00164 #define IS_ABSOLUTE_RELOC(r_type) 1
00165 #endif
00166 
00167 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
00168    copying dynamic variables from a shared lib into an app's dynbss
00169    section, and instead use a dynamic relocation to point into the
00170    shared lib.  */
00171 #define ELIMINATE_COPY_RELOCS 1
00172 
00173 enum elf32_hppa_stub_type
00174 {
00175   hppa_stub_long_branch,
00176   hppa_stub_long_branch_shared,
00177   hppa_stub_import,
00178   hppa_stub_import_shared,
00179   hppa_stub_export,
00180   hppa_stub_none
00181 };
00182 
00183 struct elf32_hppa_stub_hash_entry
00184 {
00185   /* Base hash table entry structure.  */
00186   struct bfd_hash_entry bh_root;
00187 
00188   /* The stub section.  */
00189   asection *stub_sec;
00190 
00191   /* Offset within stub_sec of the beginning of this stub.  */
00192   bfd_vma stub_offset;
00193 
00194   /* Given the symbol's value and its section we can determine its final
00195      value when building the stubs (so the stub knows where to jump.  */
00196   bfd_vma target_value;
00197   asection *target_section;
00198 
00199   enum elf32_hppa_stub_type stub_type;
00200 
00201   /* The symbol table entry, if any, that this was derived from.  */
00202   struct elf32_hppa_link_hash_entry *hh;
00203 
00204   /* Where this stub is being called from, or, in the case of combined
00205      stub sections, the first input section in the group.  */
00206   asection *id_sec;
00207 };
00208 
00209 struct elf32_hppa_link_hash_entry
00210 {
00211   struct elf_link_hash_entry eh;
00212 
00213   /* A pointer to the most recently used stub hash entry against this
00214      symbol.  */
00215   struct elf32_hppa_stub_hash_entry *hsh_cache;
00216 
00217   /* Used to count relocations for delayed sizing of relocation
00218      sections.  */
00219   struct elf32_hppa_dyn_reloc_entry
00220   {
00221     /* Next relocation in the chain.  */
00222     struct elf32_hppa_dyn_reloc_entry *hdh_next;
00223 
00224     /* The input section of the reloc.  */
00225     asection *sec;
00226 
00227     /* Number of relocs copied in this section.  */
00228     bfd_size_type count;
00229 
00230 #if RELATIVE_DYNRELOCS
00231   /* Number of relative relocs copied for the input section.  */
00232     bfd_size_type relative_count;
00233 #endif
00234   } *dyn_relocs;
00235 
00236   enum
00237   {
00238     GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_GD = 2, GOT_TLS_LDM = 4, GOT_TLS_IE = 8
00239   } tls_type;
00240 
00241   /* Set if this symbol is used by a plabel reloc.  */
00242   unsigned int plabel:1;
00243 };
00244 
00245 struct elf32_hppa_link_hash_table
00246 {
00247   /* The main hash table.  */
00248   struct elf_link_hash_table etab;
00249 
00250   /* The stub hash table.  */
00251   struct bfd_hash_table bstab;
00252 
00253   /* Linker stub bfd.  */
00254   bfd *stub_bfd;
00255 
00256   /* Linker call-backs.  */
00257   asection * (*add_stub_section) (const char *, asection *);
00258   void (*layout_sections_again) (void);
00259 
00260   /* Array to keep track of which stub sections have been created, and
00261      information on stub grouping.  */
00262   struct map_stub
00263   {
00264     /* This is the section to which stubs in the group will be
00265        attached.  */
00266     asection *link_sec;
00267     /* The stub section.  */
00268     asection *stub_sec;
00269   } *stub_group;
00270 
00271   /* Assorted information used by elf32_hppa_size_stubs.  */
00272   unsigned int bfd_count;
00273   int top_index;
00274   asection **input_list;
00275   Elf_Internal_Sym **all_local_syms;
00276 
00277   /* Short-cuts to get to dynamic linker sections.  */
00278   asection *sgot;
00279   asection *srelgot;
00280   asection *splt;
00281   asection *srelplt;
00282   asection *sdynbss;
00283   asection *srelbss;
00284 
00285   /* Used during a final link to store the base of the text and data
00286      segments so that we can perform SEGREL relocations.  */
00287   bfd_vma text_segment_base;
00288   bfd_vma data_segment_base;
00289 
00290   /* Whether we support multiple sub-spaces for shared libs.  */
00291   unsigned int multi_subspace:1;
00292 
00293   /* Flags set when various size branches are detected.  Used to
00294      select suitable defaults for the stub group size.  */
00295   unsigned int has_12bit_branch:1;
00296   unsigned int has_17bit_branch:1;
00297   unsigned int has_22bit_branch:1;
00298 
00299   /* Set if we need a .plt stub to support lazy dynamic linking.  */
00300   unsigned int need_plt_stub:1;
00301 
00302   /* Small local sym to section mapping cache.  */
00303   struct sym_sec_cache sym_sec;
00304 
00305   /* Data for LDM relocations.  */
00306   union
00307   {
00308     bfd_signed_vma refcount;
00309     bfd_vma offset;
00310   } tls_ldm_got;
00311 };
00312 
00313 /* Various hash macros and functions.  */
00314 #define hppa_link_hash_table(p) \
00315   ((struct elf32_hppa_link_hash_table *) ((p)->hash))
00316 
00317 #define hppa_elf_hash_entry(ent) \
00318   ((struct elf32_hppa_link_hash_entry *)(ent))
00319 
00320 #define hppa_stub_hash_entry(ent) \
00321   ((struct elf32_hppa_stub_hash_entry *)(ent))
00322 
00323 #define hppa_stub_hash_lookup(table, string, create, copy) \
00324   ((struct elf32_hppa_stub_hash_entry *) \
00325    bfd_hash_lookup ((table), (string), (create), (copy)))
00326 
00327 #define hppa_elf_local_got_tls_type(abfd) \
00328   ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info * 2)))
00329 
00330 #define hh_name(hh) \
00331   (hh ? hh->eh.root.root.string : "<undef>")
00332 
00333 #define eh_name(eh) \
00334   (eh ? eh->root.root.string : "<undef>")
00335 
00336 /* Assorted hash table functions.  */
00337 
00338 /* Initialize an entry in the stub hash table.  */
00339 
00340 static struct bfd_hash_entry *
00341 stub_hash_newfunc (struct bfd_hash_entry *entry,
00342                  struct bfd_hash_table *table,
00343                  const char *string)
00344 {
00345   /* Allocate the structure if it has not already been allocated by a
00346      subclass.  */
00347   if (entry == NULL)
00348     {
00349       entry = bfd_hash_allocate (table,
00350                              sizeof (struct elf32_hppa_stub_hash_entry));
00351       if (entry == NULL)
00352        return entry;
00353     }
00354 
00355   /* Call the allocation method of the superclass.  */
00356   entry = bfd_hash_newfunc (entry, table, string);
00357   if (entry != NULL)
00358     {
00359       struct elf32_hppa_stub_hash_entry *hsh;
00360 
00361       /* Initialize the local fields.  */
00362       hsh = hppa_stub_hash_entry (entry);
00363       hsh->stub_sec = NULL;
00364       hsh->stub_offset = 0;
00365       hsh->target_value = 0;
00366       hsh->target_section = NULL;
00367       hsh->stub_type = hppa_stub_long_branch;
00368       hsh->hh = NULL;
00369       hsh->id_sec = NULL;
00370     }
00371 
00372   return entry;
00373 }
00374 
00375 /* Initialize an entry in the link hash table.  */
00376 
00377 static struct bfd_hash_entry *
00378 hppa_link_hash_newfunc (struct bfd_hash_entry *entry,
00379                      struct bfd_hash_table *table,
00380                      const char *string)
00381 {
00382   /* Allocate the structure if it has not already been allocated by a
00383      subclass.  */
00384   if (entry == NULL)
00385     {
00386       entry = bfd_hash_allocate (table,
00387                              sizeof (struct elf32_hppa_link_hash_entry));
00388       if (entry == NULL)
00389        return entry;
00390     }
00391 
00392   /* Call the allocation method of the superclass.  */
00393   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
00394   if (entry != NULL)
00395     {
00396       struct elf32_hppa_link_hash_entry *hh;
00397 
00398       /* Initialize the local fields.  */
00399       hh = hppa_elf_hash_entry (entry);
00400       hh->hsh_cache = NULL;
00401       hh->dyn_relocs = NULL;
00402       hh->plabel = 0;
00403       hh->tls_type = GOT_UNKNOWN;
00404     }
00405 
00406   return entry;
00407 }
00408 
00409 /* Create the derived linker hash table.  The PA ELF port uses the derived
00410    hash table to keep information specific to the PA ELF linker (without
00411    using static variables).  */
00412 
00413 static struct bfd_link_hash_table *
00414 elf32_hppa_link_hash_table_create (bfd *abfd)
00415 {
00416   struct elf32_hppa_link_hash_table *htab;
00417   bfd_size_type amt = sizeof (*htab);
00418 
00419   htab = bfd_malloc (amt);
00420   if (htab == NULL)
00421     return NULL;
00422 
00423   if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, hppa_link_hash_newfunc,
00424                                   sizeof (struct elf32_hppa_link_hash_entry)))
00425     {
00426       free (htab);
00427       return NULL;
00428     }
00429 
00430   /* Init the stub hash table too.  */
00431   if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
00432                          sizeof (struct elf32_hppa_stub_hash_entry)))
00433     return NULL;
00434 
00435   htab->stub_bfd = NULL;
00436   htab->add_stub_section = NULL;
00437   htab->layout_sections_again = NULL;
00438   htab->stub_group = NULL;
00439   htab->sgot = NULL;
00440   htab->srelgot = NULL;
00441   htab->splt = NULL;
00442   htab->srelplt = NULL;
00443   htab->sdynbss = NULL;
00444   htab->srelbss = NULL;
00445   htab->text_segment_base = (bfd_vma) -1;
00446   htab->data_segment_base = (bfd_vma) -1;
00447   htab->multi_subspace = 0;
00448   htab->has_12bit_branch = 0;
00449   htab->has_17bit_branch = 0;
00450   htab->has_22bit_branch = 0;
00451   htab->need_plt_stub = 0;
00452   htab->sym_sec.abfd = NULL;
00453   htab->tls_ldm_got.refcount = 0;
00454 
00455   return &htab->etab.root;
00456 }
00457 
00458 /* Free the derived linker hash table.  */
00459 
00460 static void
00461 elf32_hppa_link_hash_table_free (struct bfd_link_hash_table *btab)
00462 {
00463   struct elf32_hppa_link_hash_table *htab
00464     = (struct elf32_hppa_link_hash_table *) btab;
00465 
00466   bfd_hash_table_free (&htab->bstab);
00467   _bfd_generic_link_hash_table_free (btab);
00468 }
00469 
00470 /* Build a name for an entry in the stub hash table.  */
00471 
00472 static char *
00473 hppa_stub_name (const asection *input_section,
00474               const asection *sym_sec,
00475               const struct elf32_hppa_link_hash_entry *hh,
00476               const Elf_Internal_Rela *rela)
00477 {
00478   char *stub_name;
00479   bfd_size_type len;
00480 
00481   if (hh)
00482     {
00483       len = 8 + 1 + strlen (hh_name (hh)) + 1 + 8 + 1;
00484       stub_name = bfd_malloc (len);
00485       if (stub_name != NULL)
00486        sprintf (stub_name, "%08x_%s+%x",
00487                input_section->id & 0xffffffff,
00488                hh_name (hh),
00489                (int) rela->r_addend & 0xffffffff);
00490     }
00491   else
00492     {
00493       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
00494       stub_name = bfd_malloc (len);
00495       if (stub_name != NULL)
00496        sprintf (stub_name, "%08x_%x:%x+%x",
00497                input_section->id & 0xffffffff,
00498                sym_sec->id & 0xffffffff,
00499                (int) ELF32_R_SYM (rela->r_info) & 0xffffffff,
00500                (int) rela->r_addend & 0xffffffff);
00501     }
00502   return stub_name;
00503 }
00504 
00505 /* Look up an entry in the stub hash.  Stub entries are cached because
00506    creating the stub name takes a bit of time.  */
00507 
00508 static struct elf32_hppa_stub_hash_entry *
00509 hppa_get_stub_entry (const asection *input_section,
00510                    const asection *sym_sec,
00511                    struct elf32_hppa_link_hash_entry *hh,
00512                    const Elf_Internal_Rela *rela,
00513                    struct elf32_hppa_link_hash_table *htab)
00514 {
00515   struct elf32_hppa_stub_hash_entry *hsh_entry;
00516   const asection *id_sec;
00517 
00518   /* If this input section is part of a group of sections sharing one
00519      stub section, then use the id of the first section in the group.
00520      Stub names need to include a section id, as there may well be
00521      more than one stub used to reach say, printf, and we need to
00522      distinguish between them.  */
00523   id_sec = htab->stub_group[input_section->id].link_sec;
00524 
00525   if (hh != NULL && hh->hsh_cache != NULL
00526       && hh->hsh_cache->hh == hh
00527       && hh->hsh_cache->id_sec == id_sec)
00528     {
00529       hsh_entry = hh->hsh_cache;
00530     }
00531   else
00532     {
00533       char *stub_name;
00534 
00535       stub_name = hppa_stub_name (id_sec, sym_sec, hh, rela);
00536       if (stub_name == NULL)
00537        return NULL;
00538 
00539       hsh_entry = hppa_stub_hash_lookup (&htab->bstab,
00540                                      stub_name, FALSE, FALSE);
00541       if (hh != NULL)
00542        hh->hsh_cache = hsh_entry;
00543 
00544       free (stub_name);
00545     }
00546 
00547   return hsh_entry;
00548 }
00549 
00550 /* Add a new stub entry to the stub hash.  Not all fields of the new
00551    stub entry are initialised.  */
00552 
00553 static struct elf32_hppa_stub_hash_entry *
00554 hppa_add_stub (const char *stub_name,
00555               asection *section,
00556               struct elf32_hppa_link_hash_table *htab)
00557 {
00558   asection *link_sec;
00559   asection *stub_sec;
00560   struct elf32_hppa_stub_hash_entry *hsh;
00561 
00562   link_sec = htab->stub_group[section->id].link_sec;
00563   stub_sec = htab->stub_group[section->id].stub_sec;
00564   if (stub_sec == NULL)
00565     {
00566       stub_sec = htab->stub_group[link_sec->id].stub_sec;
00567       if (stub_sec == NULL)
00568        {
00569          size_t namelen;
00570          bfd_size_type len;
00571          char *s_name;
00572 
00573          namelen = strlen (link_sec->name);
00574          len = namelen + sizeof (STUB_SUFFIX);
00575          s_name = bfd_alloc (htab->stub_bfd, len);
00576          if (s_name == NULL)
00577            return NULL;
00578 
00579          memcpy (s_name, link_sec->name, namelen);
00580          memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
00581          stub_sec = (*htab->add_stub_section) (s_name, link_sec);
00582          if (stub_sec == NULL)
00583            return NULL;
00584          htab->stub_group[link_sec->id].stub_sec = stub_sec;
00585        }
00586       htab->stub_group[section->id].stub_sec = stub_sec;
00587     }
00588 
00589   /* Enter this entry into the linker stub hash table.  */
00590   hsh = hppa_stub_hash_lookup (&htab->bstab, stub_name,
00591                                   TRUE, FALSE);
00592   if (hsh == NULL)
00593     {
00594       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
00595                           section->owner,
00596                           stub_name);
00597       return NULL;
00598     }
00599 
00600   hsh->stub_sec = stub_sec;
00601   hsh->stub_offset = 0;
00602   hsh->id_sec = link_sec;
00603   return hsh;
00604 }
00605 
00606 /* Determine the type of stub needed, if any, for a call.  */
00607 
00608 static enum elf32_hppa_stub_type
00609 hppa_type_of_stub (asection *input_sec,
00610                  const Elf_Internal_Rela *rela,
00611                  struct elf32_hppa_link_hash_entry *hh,
00612                  bfd_vma destination,
00613                  struct bfd_link_info *info)
00614 {
00615   bfd_vma location;
00616   bfd_vma branch_offset;
00617   bfd_vma max_branch_offset;
00618   unsigned int r_type;
00619 
00620   if (hh != NULL
00621       && hh->eh.plt.offset != (bfd_vma) -1
00622       && hh->eh.dynindx != -1
00623       && !hh->plabel
00624       && (info->shared
00625          || !hh->eh.def_regular
00626          || hh->eh.root.type == bfd_link_hash_defweak))
00627     {
00628       /* We need an import stub.  Decide between hppa_stub_import
00629         and hppa_stub_import_shared later.  */
00630       return hppa_stub_import;
00631     }
00632 
00633   /* Determine where the call point is.  */
00634   location = (input_sec->output_offset
00635              + input_sec->output_section->vma
00636              + rela->r_offset);
00637 
00638   branch_offset = destination - location - 8;
00639   r_type = ELF32_R_TYPE (rela->r_info);
00640 
00641   /* Determine if a long branch stub is needed.  parisc branch offsets
00642      are relative to the second instruction past the branch, ie. +8
00643      bytes on from the branch instruction location.  The offset is
00644      signed and counts in units of 4 bytes.  */
00645   if (r_type == (unsigned int) R_PARISC_PCREL17F)
00646     max_branch_offset = (1 << (17 - 1)) << 2;
00647 
00648   else if (r_type == (unsigned int) R_PARISC_PCREL12F)
00649     max_branch_offset = (1 << (12 - 1)) << 2;
00650 
00651   else /* R_PARISC_PCREL22F.  */
00652     max_branch_offset = (1 << (22 - 1)) << 2;
00653 
00654   if (branch_offset + max_branch_offset >= 2*max_branch_offset)
00655     return hppa_stub_long_branch;
00656 
00657   return hppa_stub_none;
00658 }
00659 
00660 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
00661    IN_ARG contains the link info pointer.  */
00662 
00663 #define LDIL_R1             0x20200000    /* ldil  LR'XXX,%r1         */
00664 #define BE_SR4_R1    0xe0202002    /* be,n  RR'XXX(%sr4,%r1)   */
00665 
00666 #define BL_R1        0xe8200000    /* b,l   .+8,%r1            */
00667 #define ADDIL_R1     0x28200000    /* addil LR'XXX,%r1,%r1            */
00668 #define DEPI_R1             0xd4201c1e    /* depi  0,31,2,%r1         */
00669 
00670 #define ADDIL_DP     0x2b600000    /* addil LR'XXX,%dp,%r1            */
00671 #define LDW_R1_R21   0x48350000    /* ldw   RR'XXX(%sr0,%r1),%r21     */
00672 #define BV_R0_R21    0xeaa0c000    /* bv    %r0(%r21)          */
00673 #define LDW_R1_R19   0x48330000    /* ldw   RR'XXX(%sr0,%r1),%r19     */
00674 
00675 #define ADDIL_R19    0x2a600000    /* addil LR'XXX,%r19,%r1    */
00676 #define LDW_R1_DP    0x483b0000    /* ldw   RR'XXX(%sr0,%r1),%dp      */
00677 
00678 #define LDSID_R21_R1 0x02a010a1    /* ldsid (%sr0,%r21),%r1    */
00679 #define MTSP_R1             0x00011820    /* mtsp  %r1,%sr0           */
00680 #define BE_SR0_R21   0xe2a00000    /* be    0(%sr0,%r21)              */
00681 #define STW_RP              0x6bc23fd1    /* stw   %rp,-24(%sr0,%sp)  */
00682 
00683 #define BL22_RP             0xe800a002    /* b,l,n XXX,%rp            */
00684 #define BL_RP        0xe8400002    /* b,l,n XXX,%rp            */
00685 #define NOP          0x08000240    /* nop                      */
00686 #define LDW_RP              0x4bc23fd1    /* ldw   -24(%sr0,%sp),%rp  */
00687 #define LDSID_RP_R1  0x004010a1    /* ldsid (%sr0,%rp),%r1            */
00688 #define BE_SR0_RP    0xe0400002    /* be,n  0(%sr0,%rp)        */
00689 
00690 #ifndef R19_STUBS
00691 #define R19_STUBS 1
00692 #endif
00693 
00694 #if R19_STUBS
00695 #define LDW_R1_DLT   LDW_R1_R19
00696 #else
00697 #define LDW_R1_DLT   LDW_R1_DP
00698 #endif
00699 
00700 static bfd_boolean
00701 hppa_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
00702 {
00703   struct elf32_hppa_stub_hash_entry *hsh;
00704   struct bfd_link_info *info;
00705   struct elf32_hppa_link_hash_table *htab;
00706   asection *stub_sec;
00707   bfd *stub_bfd;
00708   bfd_byte *loc;
00709   bfd_vma sym_value;
00710   bfd_vma insn;
00711   bfd_vma off;
00712   int val;
00713   int size;
00714 
00715   /* Massage our args to the form they really have.  */
00716   hsh = hppa_stub_hash_entry (bh);
00717   info = (struct bfd_link_info *)in_arg;
00718 
00719   htab = hppa_link_hash_table (info);
00720   stub_sec = hsh->stub_sec;
00721 
00722   /* Make a note of the offset within the stubs for this entry.  */
00723   hsh->stub_offset = stub_sec->size;
00724   loc = stub_sec->contents + hsh->stub_offset;
00725 
00726   stub_bfd = stub_sec->owner;
00727 
00728   switch (hsh->stub_type)
00729     {
00730     case hppa_stub_long_branch:
00731       /* Create the long branch.  A long branch is formed with "ldil"
00732         loading the upper bits of the target address into a register,
00733         then branching with "be" which adds in the lower bits.
00734         The "be" has its delay slot nullified.  */
00735       sym_value = (hsh->target_value
00736                  + hsh->target_section->output_offset
00737                  + hsh->target_section->output_section->vma);
00738 
00739       val = hppa_field_adjust (sym_value, 0, e_lrsel);
00740       insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
00741       bfd_put_32 (stub_bfd, insn, loc);
00742 
00743       val = hppa_field_adjust (sym_value, 0, e_rrsel) >> 2;
00744       insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
00745       bfd_put_32 (stub_bfd, insn, loc + 4);
00746 
00747       size = 8;
00748       break;
00749 
00750     case hppa_stub_long_branch_shared:
00751       /* Branches are relative.  This is where we are going to.  */
00752       sym_value = (hsh->target_value
00753                  + hsh->target_section->output_offset
00754                  + hsh->target_section->output_section->vma);
00755 
00756       /* And this is where we are coming from, more or less.  */
00757       sym_value -= (hsh->stub_offset
00758                   + stub_sec->output_offset
00759                   + stub_sec->output_section->vma);
00760 
00761       bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
00762       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
00763       insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
00764       bfd_put_32 (stub_bfd, insn, loc + 4);
00765 
00766       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
00767       insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
00768       bfd_put_32 (stub_bfd, insn, loc + 8);
00769       size = 12;
00770       break;
00771 
00772     case hppa_stub_import:
00773     case hppa_stub_import_shared:
00774       off = hsh->hh->eh.plt.offset;
00775       if (off >= (bfd_vma) -2)
00776        abort ();
00777 
00778       off &= ~ (bfd_vma) 1;
00779       sym_value = (off
00780                  + htab->splt->output_offset
00781                  + htab->splt->output_section->vma
00782                  - elf_gp (htab->splt->output_section->owner));
00783 
00784       insn = ADDIL_DP;
00785 #if R19_STUBS
00786       if (hsh->stub_type == hppa_stub_import_shared)
00787        insn = ADDIL_R19;
00788 #endif
00789       val = hppa_field_adjust (sym_value, 0, e_lrsel),
00790       insn = hppa_rebuild_insn ((int) insn, val, 21);
00791       bfd_put_32 (stub_bfd, insn, loc);
00792 
00793       /* It is critical to use lrsel/rrsel here because we are using
00794         two different offsets (+0 and +4) from sym_value.  If we use
00795         lsel/rsel then with unfortunate sym_values we will round
00796         sym_value+4 up to the next 2k block leading to a mis-match
00797         between the lsel and rsel value.  */
00798       val = hppa_field_adjust (sym_value, 0, e_rrsel);
00799       insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
00800       bfd_put_32 (stub_bfd, insn, loc + 4);
00801 
00802       if (htab->multi_subspace)
00803        {
00804          val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
00805          insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
00806          bfd_put_32 (stub_bfd, insn, loc + 8);
00807 
00808          bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
00809          bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,      loc + 16);
00810          bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21,   loc + 20);
00811          bfd_put_32 (stub_bfd, (bfd_vma) STW_RP,       loc + 24);
00812 
00813          size = 28;
00814        }
00815       else
00816        {
00817          bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
00818          val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
00819          insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
00820          bfd_put_32 (stub_bfd, insn, loc + 12);
00821 
00822          size = 16;
00823        }
00824 
00825       break;
00826 
00827     case hppa_stub_export:
00828       /* Branches are relative.  This is where we are going to.  */
00829       sym_value = (hsh->target_value
00830                  + hsh->target_section->output_offset
00831                  + hsh->target_section->output_section->vma);
00832 
00833       /* And this is where we are coming from.  */
00834       sym_value -= (hsh->stub_offset
00835                   + stub_sec->output_offset
00836                   + stub_sec->output_section->vma);
00837 
00838       if (sym_value - 8 + (1 << (17 + 1)) >= (1 << (17 + 2))
00839          && (!htab->has_22bit_branch
00840              || sym_value - 8 + (1 << (22 + 1)) >= (1 << (22 + 2))))
00841        {
00842          (*_bfd_error_handler)
00843            (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
00844             hsh->target_section->owner,
00845             stub_sec,
00846             (long) hsh->stub_offset,
00847             hsh->bh_root.string);
00848          bfd_set_error (bfd_error_bad_value);
00849          return FALSE;
00850        }
00851 
00852       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
00853       if (!htab->has_22bit_branch)
00854        insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
00855       else
00856        insn = hppa_rebuild_insn ((int) BL22_RP, val, 22);
00857       bfd_put_32 (stub_bfd, insn, loc);
00858 
00859       bfd_put_32 (stub_bfd, (bfd_vma) NOP,         loc + 4);
00860       bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP,      loc + 8);
00861       bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
00862       bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,     loc + 16);
00863       bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP,   loc + 20);
00864 
00865       /* Point the function symbol at the stub.  */
00866       hsh->hh->eh.root.u.def.section = stub_sec;
00867       hsh->hh->eh.root.u.def.value = stub_sec->size;
00868 
00869       size = 24;
00870       break;
00871 
00872     default:
00873       BFD_FAIL ();
00874       return FALSE;
00875     }
00876 
00877   stub_sec->size += size;
00878   return TRUE;
00879 }
00880 
00881 #undef LDIL_R1
00882 #undef BE_SR4_R1
00883 #undef BL_R1
00884 #undef ADDIL_R1
00885 #undef DEPI_R1
00886 #undef LDW_R1_R21
00887 #undef LDW_R1_DLT
00888 #undef LDW_R1_R19
00889 #undef ADDIL_R19
00890 #undef LDW_R1_DP
00891 #undef LDSID_R21_R1
00892 #undef MTSP_R1
00893 #undef BE_SR0_R21
00894 #undef STW_RP
00895 #undef BV_R0_R21
00896 #undef BL_RP
00897 #undef NOP
00898 #undef LDW_RP
00899 #undef LDSID_RP_R1
00900 #undef BE_SR0_RP
00901 
00902 /* As above, but don't actually build the stub.  Just bump offset so
00903    we know stub section sizes.  */
00904 
00905 static bfd_boolean
00906 hppa_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
00907 {
00908   struct elf32_hppa_stub_hash_entry *hsh;
00909   struct elf32_hppa_link_hash_table *htab;
00910   int size;
00911 
00912   /* Massage our args to the form they really have.  */
00913   hsh = hppa_stub_hash_entry (bh);
00914   htab = in_arg;
00915 
00916   if (hsh->stub_type == hppa_stub_long_branch)
00917     size = 8;
00918   else if (hsh->stub_type == hppa_stub_long_branch_shared)
00919     size = 12;
00920   else if (hsh->stub_type == hppa_stub_export)
00921     size = 24;
00922   else /* hppa_stub_import or hppa_stub_import_shared.  */
00923     {
00924       if (htab->multi_subspace)
00925        size = 28;
00926       else
00927        size = 16;
00928     }
00929 
00930   hsh->stub_sec->size += size;
00931   return TRUE;
00932 }
00933 
00934 /* Return nonzero if ABFD represents an HPPA ELF32 file.
00935    Additionally we set the default architecture and machine.  */
00936 
00937 static bfd_boolean
00938 elf32_hppa_object_p (bfd *abfd)
00939 {
00940   Elf_Internal_Ehdr * i_ehdrp;
00941   unsigned int flags;
00942 
00943   i_ehdrp = elf_elfheader (abfd);
00944   if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
00945     {
00946       /* GCC on hppa-linux produces binaries with OSABI=Linux,
00947         but the kernel produces corefiles with OSABI=SysV.  */
00948       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX &&
00949          i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
00950        return FALSE;
00951     }
00952   else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
00953     {
00954       /* GCC on hppa-netbsd produces binaries with OSABI=NetBSD,
00955         but the kernel produces corefiles with OSABI=SysV.  */
00956       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NETBSD &&
00957          i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
00958        return FALSE;
00959     }
00960   else
00961     {
00962       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
00963        return FALSE;
00964     }
00965 
00966   flags = i_ehdrp->e_flags;
00967   switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
00968     {
00969     case EFA_PARISC_1_0:
00970       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
00971     case EFA_PARISC_1_1:
00972       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
00973     case EFA_PARISC_2_0:
00974       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
00975     case EFA_PARISC_2_0 | EF_PARISC_WIDE:
00976       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
00977     }
00978   return TRUE;
00979 }
00980 
00981 /* Create the .plt and .got sections, and set up our hash table
00982    short-cuts to various dynamic sections.  */
00983 
00984 static bfd_boolean
00985 elf32_hppa_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
00986 {
00987   struct elf32_hppa_link_hash_table *htab;
00988   struct elf_link_hash_entry *eh;
00989 
00990   /* Don't try to create the .plt and .got twice.  */
00991   htab = hppa_link_hash_table (info);
00992   if (htab->splt != NULL)
00993     return TRUE;
00994 
00995   /* Call the generic code to do most of the work.  */
00996   if (! _bfd_elf_create_dynamic_sections (abfd, info))
00997     return FALSE;
00998 
00999   htab->splt = bfd_get_section_by_name (abfd, ".plt");
01000   htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
01001 
01002   htab->sgot = bfd_get_section_by_name (abfd, ".got");
01003   htab->srelgot = bfd_make_section_with_flags (abfd, ".rela.got",
01004                                           (SEC_ALLOC
01005                                           | SEC_LOAD
01006                                           | SEC_HAS_CONTENTS
01007                                           | SEC_IN_MEMORY
01008                                           | SEC_LINKER_CREATED
01009                                           | SEC_READONLY));
01010   if (htab->srelgot == NULL
01011       || ! bfd_set_section_alignment (abfd, htab->srelgot, 2))
01012     return FALSE;
01013 
01014   htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
01015   htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
01016 
01017   /* hppa-linux needs _GLOBAL_OFFSET_TABLE_ to be visible from the main
01018      application, because __canonicalize_funcptr_for_compare needs it.  */
01019   eh = elf_hash_table (info)->hgot;
01020   eh->forced_local = 0;
01021   eh->other = STV_DEFAULT;
01022   return bfd_elf_link_record_dynamic_symbol (info, eh);
01023 }
01024 
01025 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
01026 
01027 static void
01028 elf32_hppa_copy_indirect_symbol (struct bfd_link_info *info,
01029                              struct elf_link_hash_entry *eh_dir,
01030                              struct elf_link_hash_entry *eh_ind)
01031 {
01032   struct elf32_hppa_link_hash_entry *hh_dir, *hh_ind;
01033 
01034   hh_dir = hppa_elf_hash_entry (eh_dir);
01035   hh_ind = hppa_elf_hash_entry (eh_ind);
01036 
01037   if (hh_ind->dyn_relocs != NULL)
01038     {
01039       if (hh_dir->dyn_relocs != NULL)
01040        {
01041          struct elf32_hppa_dyn_reloc_entry **hdh_pp;
01042          struct elf32_hppa_dyn_reloc_entry *hdh_p;
01043 
01044          /* Add reloc counts against the indirect sym to the direct sym
01045             list.  Merge any entries against the same section.  */
01046          for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
01047            {
01048              struct elf32_hppa_dyn_reloc_entry *hdh_q;
01049 
01050              for (hdh_q = hh_dir->dyn_relocs;
01051                  hdh_q != NULL;
01052                  hdh_q = hdh_q->hdh_next)
01053               if (hdh_q->sec == hdh_p->sec)
01054                 {
01055 #if RELATIVE_DYNRELOCS
01056                   hdh_q->relative_count += hdh_p->relative_count;
01057 #endif
01058                   hdh_q->count += hdh_p->count;
01059                   *hdh_pp = hdh_p->hdh_next;
01060                   break;
01061                 }
01062              if (hdh_q == NULL)
01063               hdh_pp = &hdh_p->hdh_next;
01064            }
01065          *hdh_pp = hh_dir->dyn_relocs;
01066        }
01067 
01068       hh_dir->dyn_relocs = hh_ind->dyn_relocs;
01069       hh_ind->dyn_relocs = NULL;
01070     }
01071 
01072   if (ELIMINATE_COPY_RELOCS
01073       && eh_ind->root.type != bfd_link_hash_indirect
01074       && eh_dir->dynamic_adjusted)
01075     {
01076       /* If called to transfer flags for a weakdef during processing
01077         of elf_adjust_dynamic_symbol, don't copy non_got_ref.
01078         We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
01079       eh_dir->ref_dynamic |= eh_ind->ref_dynamic;
01080       eh_dir->ref_regular |= eh_ind->ref_regular;
01081       eh_dir->ref_regular_nonweak |= eh_ind->ref_regular_nonweak;
01082       eh_dir->needs_plt |= eh_ind->needs_plt;
01083     }
01084   else
01085     {
01086       if (eh_ind->root.type == bfd_link_hash_indirect
01087           && eh_dir->got.refcount <= 0)
01088         {
01089           hh_dir->tls_type = hh_ind->tls_type;
01090           hh_ind->tls_type = GOT_UNKNOWN;
01091         }
01092 
01093       _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
01094     }
01095 }
01096 
01097 static int
01098 elf32_hppa_optimized_tls_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
01099                             int r_type, int is_local ATTRIBUTE_UNUSED)
01100 {
01101   /* For now we don't support linker optimizations.  */
01102   return r_type;
01103 }
01104 
01105 /* Look through the relocs for a section during the first phase, and
01106    calculate needed space in the global offset table, procedure linkage
01107    table, and dynamic reloc sections.  At this point we haven't
01108    necessarily read all the input files.  */
01109 
01110 static bfd_boolean
01111 elf32_hppa_check_relocs (bfd *abfd,
01112                       struct bfd_link_info *info,
01113                       asection *sec,
01114                       const Elf_Internal_Rela *relocs)
01115 {
01116   Elf_Internal_Shdr *symtab_hdr;
01117   struct elf_link_hash_entry **eh_syms;
01118   const Elf_Internal_Rela *rela;
01119   const Elf_Internal_Rela *rela_end;
01120   struct elf32_hppa_link_hash_table *htab;
01121   asection *sreloc;
01122   asection *stubreloc;
01123   int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN;
01124 
01125   if (info->relocatable)
01126     return TRUE;
01127 
01128   htab = hppa_link_hash_table (info);
01129   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01130   eh_syms = elf_sym_hashes (abfd);
01131   sreloc = NULL;
01132   stubreloc = NULL;
01133 
01134   rela_end = relocs + sec->reloc_count;
01135   for (rela = relocs; rela < rela_end; rela++)
01136     {
01137       enum {
01138        NEED_GOT = 1,
01139        NEED_PLT = 2,
01140        NEED_DYNREL = 4,
01141        PLT_PLABEL = 8
01142       };
01143 
01144       unsigned int r_symndx, r_type;
01145       struct elf32_hppa_link_hash_entry *hh;
01146       int need_entry = 0;
01147 
01148       r_symndx = ELF32_R_SYM (rela->r_info);
01149 
01150       if (r_symndx < symtab_hdr->sh_info)
01151        hh = NULL;
01152       else
01153        {
01154          hh =  hppa_elf_hash_entry (eh_syms[r_symndx - symtab_hdr->sh_info]);
01155          while (hh->eh.root.type == bfd_link_hash_indirect
01156                || hh->eh.root.type == bfd_link_hash_warning)
01157            hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
01158        }
01159 
01160       r_type = ELF32_R_TYPE (rela->r_info);
01161       r_type = elf32_hppa_optimized_tls_reloc (info, r_type, hh == NULL);
01162 
01163       switch (r_type)
01164        {
01165        case R_PARISC_DLTIND14F:
01166        case R_PARISC_DLTIND14R:
01167        case R_PARISC_DLTIND21L:
01168          /* This symbol requires a global offset table entry.  */
01169          need_entry = NEED_GOT;
01170          break;
01171 
01172        case R_PARISC_PLABEL14R: /* "Official" procedure labels.  */
01173        case R_PARISC_PLABEL21L:
01174        case R_PARISC_PLABEL32:
01175          /* If the addend is non-zero, we break badly.  */
01176          if (rela->r_addend != 0)
01177            abort ();
01178 
01179          /* If we are creating a shared library, then we need to
01180             create a PLT entry for all PLABELs, because PLABELs with
01181             local symbols may be passed via a pointer to another
01182             object.  Additionally, output a dynamic relocation
01183             pointing to the PLT entry.
01184 
01185             For executables, the original 32-bit ABI allowed two
01186             different styles of PLABELs (function pointers):  For
01187             global functions, the PLABEL word points into the .plt
01188             two bytes past a (function address, gp) pair, and for
01189             local functions the PLABEL points directly at the
01190             function.  The magic +2 for the first type allows us to
01191             differentiate between the two.  As you can imagine, this
01192             is a real pain when it comes to generating code to call
01193             functions indirectly or to compare function pointers.
01194             We avoid the mess by always pointing a PLABEL into the
01195             .plt, even for local functions.  */
01196          need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
01197          break;
01198 
01199        case R_PARISC_PCREL12F:
01200          htab->has_12bit_branch = 1;
01201          goto branch_common;
01202 
01203        case R_PARISC_PCREL17C:
01204        case R_PARISC_PCREL17F:
01205          htab->has_17bit_branch = 1;
01206          goto branch_common;
01207 
01208        case R_PARISC_PCREL22F:
01209          htab->has_22bit_branch = 1;
01210        branch_common:
01211          /* Function calls might need to go through the .plt, and
01212             might require long branch stubs.  */
01213          if (hh == NULL)
01214            {
01215              /* We know local syms won't need a .plt entry, and if
01216                they need a long branch stub we can't guarantee that
01217                we can reach the stub.  So just flag an error later
01218                if we're doing a shared link and find we need a long
01219                branch stub.  */
01220              continue;
01221            }
01222          else
01223            {
01224              /* Global symbols will need a .plt entry if they remain
01225                global, and in most cases won't need a long branch
01226                stub.  Unfortunately, we have to cater for the case
01227                where a symbol is forced local by versioning, or due
01228                to symbolic linking, and we lose the .plt entry.  */
01229              need_entry = NEED_PLT;
01230              if (hh->eh.type == STT_PARISC_MILLI)
01231               need_entry = 0;
01232            }
01233          break;
01234 
01235        case R_PARISC_SEGBASE:  /* Used to set segment base.  */
01236        case R_PARISC_SEGREL32: /* Relative reloc, used for unwind.  */
01237        case R_PARISC_PCREL14F: /* PC relative load/store.  */
01238        case R_PARISC_PCREL14R:
01239        case R_PARISC_PCREL17R: /* External branches.  */
01240        case R_PARISC_PCREL21L: /* As above, and for load/store too.  */
01241        case R_PARISC_PCREL32:
01242          /* We don't need to propagate the relocation if linking a
01243             shared object since these are section relative.  */
01244          continue;
01245 
01246        case R_PARISC_DPREL14F: /* Used for gp rel data load/store.  */
01247        case R_PARISC_DPREL14R:
01248        case R_PARISC_DPREL21L:
01249          if (info->shared)
01250            {
01251              (*_bfd_error_handler)
01252               (_("%B: relocation %s can not be used when making a shared object; recompile with -fPIC"),
01253                abfd,
01254                elf_hppa_howto_table[r_type].name);
01255              bfd_set_error (bfd_error_bad_value);
01256              return FALSE;
01257            }
01258          /* Fall through.  */
01259 
01260        case R_PARISC_DIR17F: /* Used for external branches.  */
01261        case R_PARISC_DIR17R:
01262        case R_PARISC_DIR14F: /* Used for load/store from absolute locn.  */
01263        case R_PARISC_DIR14R:
01264        case R_PARISC_DIR21L: /* As above, and for ext branches too.  */
01265        case R_PARISC_DIR32: /* .word relocs.  */
01266          /* We may want to output a dynamic relocation later.  */
01267          need_entry = NEED_DYNREL;
01268          break;
01269 
01270          /* This relocation describes the C++ object vtable hierarchy.
01271             Reconstruct it for later use during GC.  */
01272        case R_PARISC_GNU_VTINHERIT:
01273          if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh, rela->r_offset))
01274            return FALSE;
01275          continue;
01276 
01277          /* This relocation describes which C++ vtable entries are actually
01278             used.  Record for later use during GC.  */
01279        case R_PARISC_GNU_VTENTRY:
01280          if (!bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rela->r_addend))
01281            return FALSE;
01282          continue;
01283 
01284        case R_PARISC_TLS_GD21L:
01285        case R_PARISC_TLS_GD14R:
01286        case R_PARISC_TLS_LDM21L:
01287        case R_PARISC_TLS_LDM14R:
01288          need_entry = NEED_GOT;
01289          break;
01290 
01291        case R_PARISC_TLS_IE21L:
01292        case R_PARISC_TLS_IE14R:
01293          if (info->shared)
01294             info->flags |= DF_STATIC_TLS;
01295          need_entry = NEED_GOT;
01296          break;
01297 
01298        default:
01299          continue;
01300        }
01301 
01302       /* Now carry out our orders.  */
01303       if (need_entry & NEED_GOT)
01304        {
01305          switch (r_type)
01306            {
01307            default:
01308              tls_type = GOT_NORMAL;
01309              break;
01310            case R_PARISC_TLS_GD21L:
01311            case R_PARISC_TLS_GD14R:
01312              tls_type |= GOT_TLS_GD;
01313              break;
01314            case R_PARISC_TLS_LDM21L:
01315            case R_PARISC_TLS_LDM14R:
01316              tls_type |= GOT_TLS_LDM;
01317              break;
01318            case R_PARISC_TLS_IE21L:
01319            case R_PARISC_TLS_IE14R:
01320              tls_type |= GOT_TLS_IE;
01321              break;
01322            }
01323 
01324          /* Allocate space for a GOT entry, as well as a dynamic
01325             relocation for this entry.  */
01326          if (htab->sgot == NULL)
01327            {
01328              if (htab->etab.dynobj == NULL)
01329               htab->etab.dynobj = abfd;
01330              if (!elf32_hppa_create_dynamic_sections (htab->etab.dynobj, info))
01331               return FALSE;
01332            }
01333 
01334          if (r_type == R_PARISC_TLS_LDM21L
01335              || r_type == R_PARISC_TLS_LDM14R)
01336            hppa_link_hash_table (info)->tls_ldm_got.refcount += 1;
01337          else
01338            {
01339              if (hh != NULL)
01340                {
01341                  hh->eh.got.refcount += 1;
01342                  old_tls_type = hh->tls_type;
01343                }
01344              else
01345                {
01346                  bfd_signed_vma *local_got_refcounts;
01347                 
01348                  /* This is a global offset table entry for a local symbol.  */
01349                  local_got_refcounts = elf_local_got_refcounts (abfd);
01350                  if (local_got_refcounts == NULL)
01351                   {
01352                     bfd_size_type size;
01353 
01354                     /* Allocate space for local got offsets and local
01355                        plt offsets.  Done this way to save polluting
01356                        elf_obj_tdata with another target specific
01357                        pointer.  */
01358                     size = symtab_hdr->sh_info;
01359                     size *= 2 * sizeof (bfd_signed_vma);
01360                     /* Add in space to store the local GOT TLS types.  */
01361                     size += symtab_hdr->sh_info;
01362                     local_got_refcounts = bfd_zalloc (abfd, size);
01363                     if (local_got_refcounts == NULL)
01364                       return FALSE;
01365                     elf_local_got_refcounts (abfd) = local_got_refcounts;
01366                     memset (hppa_elf_local_got_tls_type (abfd),
01367                        GOT_UNKNOWN, symtab_hdr->sh_info);
01368                   }
01369                  local_got_refcounts[r_symndx] += 1;
01370 
01371                  old_tls_type = hppa_elf_local_got_tls_type (abfd) [r_symndx];
01372                }
01373 
01374              tls_type |= old_tls_type;
01375 
01376              if (old_tls_type != tls_type)
01377                {
01378                  if (hh != NULL)
01379                   hh->tls_type = tls_type;
01380                  else
01381                   hppa_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
01382                }
01383 
01384            }
01385        }
01386 
01387       if (need_entry & NEED_PLT)
01388        {
01389          /* If we are creating a shared library, and this is a reloc
01390             against a weak symbol or a global symbol in a dynamic
01391             object, then we will be creating an import stub and a
01392             .plt entry for the symbol.  Similarly, on a normal link
01393             to symbols defined in a dynamic object we'll need the
01394             import stub and a .plt entry.  We don't know yet whether
01395             the symbol is defined or not, so make an entry anyway and
01396             clean up later in adjust_dynamic_symbol.  */
01397          if ((sec->flags & SEC_ALLOC) != 0)
01398            {
01399              if (hh != NULL)
01400               {
01401                 hh->eh.needs_plt = 1;
01402                 hh->eh.plt.refcount += 1;
01403 
01404                 /* If this .plt entry is for a plabel, mark it so
01405                    that adjust_dynamic_symbol will keep the entry
01406                    even if it appears to be local.  */
01407                 if (need_entry & PLT_PLABEL)
01408                   hh->plabel = 1;
01409               }
01410              else if (need_entry & PLT_PLABEL)
01411               {
01412                 bfd_signed_vma *local_got_refcounts;
01413                 bfd_signed_vma *local_plt_refcounts;
01414 
01415                 local_got_refcounts = elf_local_got_refcounts (abfd);
01416                 if (local_got_refcounts == NULL)
01417                   {
01418                     bfd_size_type size;
01419 
01420                     /* Allocate space for local got offsets and local
01421                       plt offsets.  */
01422                     size = symtab_hdr->sh_info;
01423                     size *= 2 * sizeof (bfd_signed_vma);
01424                     /* Add in space to store the local GOT TLS types.  */
01425                     size += symtab_hdr->sh_info;
01426                     local_got_refcounts = bfd_zalloc (abfd, size);
01427                     if (local_got_refcounts == NULL)
01428                      return FALSE;
01429                     elf_local_got_refcounts (abfd) = local_got_refcounts;
01430                   }
01431                 local_plt_refcounts = (local_got_refcounts
01432                                     + symtab_hdr->sh_info);
01433                 local_plt_refcounts[r_symndx] += 1;
01434               }
01435            }
01436        }
01437 
01438       if (need_entry & NEED_DYNREL)
01439        {
01440          /* Flag this symbol as having a non-got, non-plt reference
01441             so that we generate copy relocs if it turns out to be
01442             dynamic.  */
01443          if (hh != NULL && !info->shared)
01444            hh->eh.non_got_ref = 1;
01445 
01446          /* If we are creating a shared library then we need to copy
01447             the reloc into the shared library.  However, if we are
01448             linking with -Bsymbolic, we need only copy absolute
01449             relocs or relocs against symbols that are not defined in
01450             an object we are including in the link.  PC- or DP- or
01451             DLT-relative relocs against any local sym or global sym
01452             with DEF_REGULAR set, can be discarded.  At this point we
01453             have not seen all the input files, so it is possible that
01454             DEF_REGULAR is not set now but will be set later (it is
01455             never cleared).  We account for that possibility below by
01456             storing information in the dyn_relocs field of the
01457             hash table entry.
01458 
01459             A similar situation to the -Bsymbolic case occurs when
01460             creating shared libraries and symbol visibility changes
01461             render the symbol local.
01462 
01463             As it turns out, all the relocs we will be creating here
01464             are absolute, so we cannot remove them on -Bsymbolic
01465             links or visibility changes anyway.  A STUB_REL reloc
01466             is absolute too, as in that case it is the reloc in the
01467             stub we will be creating, rather than copying the PCREL
01468             reloc in the branch.
01469 
01470             If on the other hand, we are creating an executable, we
01471             may need to keep relocations for symbols satisfied by a
01472             dynamic library if we manage to avoid copy relocs for the
01473             symbol.  */
01474          if ((info->shared
01475               && (sec->flags & SEC_ALLOC) != 0
01476               && (IS_ABSOLUTE_RELOC (r_type)
01477                  || (hh != NULL
01478                      && (!info->symbolic
01479                         || hh->eh.root.type == bfd_link_hash_defweak
01480                         || !hh->eh.def_regular))))
01481              || (ELIMINATE_COPY_RELOCS
01482                 && !info->shared
01483                 && (sec->flags & SEC_ALLOC) != 0
01484                 && hh != NULL
01485                 && (hh->eh.root.type == bfd_link_hash_defweak
01486                     || !hh->eh.def_regular)))
01487            {
01488              struct elf32_hppa_dyn_reloc_entry *hdh_p;
01489              struct elf32_hppa_dyn_reloc_entry **hdh_head;
01490 
01491              /* Create a reloc section in dynobj and make room for
01492                this reloc.  */
01493              if (sreloc == NULL)
01494               {
01495                 char *name;
01496                 bfd *dynobj;
01497 
01498                 name = (bfd_elf_string_from_elf_section
01499                        (abfd,
01500                         elf_elfheader (abfd)->e_shstrndx,
01501                         elf_section_data (sec)->rel_hdr.sh_name));
01502                 if (name == NULL)
01503                   {
01504                     (*_bfd_error_handler)
01505                      (_("Could not find relocation section for %s"),
01506                       sec->name);
01507                     bfd_set_error (bfd_error_bad_value);
01508                     return FALSE;
01509                   }
01510 
01511                 if (htab->etab.dynobj == NULL)
01512                   htab->etab.dynobj = abfd;
01513 
01514                 dynobj = htab->etab.dynobj;
01515                 sreloc = bfd_get_section_by_name (dynobj, name);
01516                 if (sreloc == NULL)
01517                   {
01518                     flagword flags;
01519 
01520                     flags = (SEC_HAS_CONTENTS | SEC_READONLY
01521                             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
01522                     if ((sec->flags & SEC_ALLOC) != 0)
01523                      flags |= SEC_ALLOC | SEC_LOAD;
01524                     sreloc = bfd_make_section_with_flags (dynobj,
01525                                                      name,
01526                                                      flags);
01527                     if (sreloc == NULL
01528                        || !bfd_set_section_alignment (dynobj, sreloc, 2))
01529                      return FALSE;
01530                   }
01531 
01532                 elf_section_data (sec)->sreloc = sreloc;
01533               }
01534 
01535              /* If this is a global symbol, we count the number of
01536                relocations we need for this symbol.  */
01537              if (hh != NULL)
01538               {
01539                 hdh_head = &hh->dyn_relocs;
01540               }
01541              else
01542               {
01543                 /* Track dynamic relocs needed for local syms too.
01544                    We really need local syms available to do this
01545                    easily.  Oh well.  */
01546 
01547                 asection *sr;
01548                 void *vpp;
01549 
01550                 sr = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
01551                                                  sec, r_symndx);
01552                 if (sr == NULL)
01553                   return FALSE;
01554 
01555                 vpp = &elf_section_data (sr)->local_dynrel;
01556                 hdh_head = (struct elf32_hppa_dyn_reloc_entry **) vpp;
01557               }
01558 
01559              hdh_p = *hdh_head;
01560              if (hdh_p == NULL || hdh_p->sec != sec)
01561               {
01562                 hdh_p = bfd_alloc (htab->etab.dynobj, sizeof *hdh_p);
01563                 if (hdh_p == NULL)
01564                   return FALSE;
01565                 hdh_p->hdh_next = *hdh_head;
01566                 *hdh_head = hdh_p;
01567                 hdh_p->sec = sec;
01568                 hdh_p->count = 0;
01569 #if RELATIVE_DYNRELOCS
01570                 hdh_p->relative_count = 0;
01571 #endif
01572               }
01573 
01574              hdh_p->count += 1;
01575 #if RELATIVE_DYNRELOCS
01576              if (!IS_ABSOLUTE_RELOC (rtype))
01577               hdh_p->relative_count += 1;
01578 #endif
01579            }
01580        }
01581     }
01582 
01583   return TRUE;
01584 }
01585 
01586 /* Return the section that should be marked against garbage collection
01587    for a given relocation.  */
01588 
01589 static asection *
01590 elf32_hppa_gc_mark_hook (asection *sec,
01591                       struct bfd_link_info *info,
01592                       Elf_Internal_Rela *rela,
01593                       struct elf_link_hash_entry *hh,
01594                       Elf_Internal_Sym *sym)
01595 {
01596   if (hh != NULL)
01597     switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
01598       {
01599       case R_PARISC_GNU_VTINHERIT:
01600       case R_PARISC_GNU_VTENTRY:
01601        return NULL;
01602       }
01603 
01604   return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
01605 }
01606 
01607 /* Update the got and plt entry reference counts for the section being
01608    removed.  */
01609 
01610 static bfd_boolean
01611 elf32_hppa_gc_sweep_hook (bfd *abfd,
01612                        struct bfd_link_info *info ATTRIBUTE_UNUSED,
01613                        asection *sec,
01614                        const Elf_Internal_Rela *relocs)
01615 {
01616   Elf_Internal_Shdr *symtab_hdr;
01617   struct elf_link_hash_entry **eh_syms;
01618   bfd_signed_vma *local_got_refcounts;
01619   bfd_signed_vma *local_plt_refcounts;
01620   const Elf_Internal_Rela *rela, *relend;
01621 
01622   elf_section_data (sec)->local_dynrel = NULL;
01623 
01624   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01625   eh_syms = elf_sym_hashes (abfd);
01626   local_got_refcounts = elf_local_got_refcounts (abfd);
01627   local_plt_refcounts = local_got_refcounts;
01628   if (local_plt_refcounts != NULL)
01629     local_plt_refcounts += symtab_hdr->sh_info;
01630 
01631   relend = relocs + sec->reloc_count;
01632   for (rela = relocs; rela < relend; rela++)
01633     {
01634       unsigned long r_symndx;
01635       unsigned int r_type;
01636       struct elf_link_hash_entry *eh = NULL;
01637 
01638       r_symndx = ELF32_R_SYM (rela->r_info);
01639       if (r_symndx >= symtab_hdr->sh_info)
01640        {
01641          struct elf32_hppa_link_hash_entry *hh;
01642          struct elf32_hppa_dyn_reloc_entry **hdh_pp;
01643          struct elf32_hppa_dyn_reloc_entry *hdh_p;
01644 
01645          eh = eh_syms[r_symndx - symtab_hdr->sh_info];
01646          while (eh->root.type == bfd_link_hash_indirect
01647                || eh->root.type == bfd_link_hash_warning)
01648            eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
01649          hh = hppa_elf_hash_entry (eh);
01650 
01651          for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; hdh_pp = &hdh_p->hdh_next)
01652            if (hdh_p->sec == sec)
01653              {
01654               /* Everything must go for SEC.  */
01655               *hdh_pp = hdh_p->hdh_next;
01656               break;
01657              }
01658        }
01659 
01660       r_type = ELF32_R_TYPE (rela->r_info);
01661       r_type = elf32_hppa_optimized_tls_reloc (info, r_type, eh != NULL);
01662 
01663       switch (r_type)
01664        {
01665        case R_PARISC_DLTIND14F:
01666        case R_PARISC_DLTIND14R:
01667        case R_PARISC_DLTIND21L:
01668        case R_PARISC_TLS_GD21L:
01669        case R_PARISC_TLS_GD14R:
01670        case R_PARISC_TLS_IE21L:
01671        case R_PARISC_TLS_IE14R:
01672          if (eh != NULL)
01673            {
01674              if (eh->got.refcount > 0)
01675               eh->got.refcount -= 1;
01676            }
01677          else if (local_got_refcounts != NULL)
01678            {
01679              if (local_got_refcounts[r_symndx] > 0)
01680               local_got_refcounts[r_symndx] -= 1;
01681            }
01682          break;
01683 
01684        case R_PARISC_TLS_LDM21L:
01685        case R_PARISC_TLS_LDM14R:
01686          hppa_link_hash_table (info)->tls_ldm_got.refcount -= 1;
01687          break;
01688 
01689        case R_PARISC_PCREL12F:
01690        case R_PARISC_PCREL17C:
01691        case R_PARISC_PCREL17F:
01692        case R_PARISC_PCREL22F:
01693          if (eh != NULL)
01694            {
01695              if (eh->plt.refcount > 0)
01696               eh->plt.refcount -= 1;
01697            }
01698          break;
01699 
01700        case R_PARISC_PLABEL14R:
01701        case R_PARISC_PLABEL21L:
01702        case R_PARISC_PLABEL32:
01703          if (eh != NULL)
01704            {
01705              if (eh->plt.refcount > 0)
01706               eh->plt.refcount -= 1;
01707            }
01708          else if (local_plt_refcounts != NULL)
01709            {
01710              if (local_plt_refcounts[r_symndx] > 0)
01711               local_plt_refcounts[r_symndx] -= 1;
01712            }
01713          break;
01714 
01715        default:
01716          break;
01717        }
01718     }
01719 
01720   return TRUE;
01721 }
01722 
01723 /* Support for core dump NOTE sections.  */
01724 
01725 static bfd_boolean
01726 elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
01727 {
01728   int offset;
01729   size_t size;
01730 
01731   switch (note->descsz)
01732     {
01733       default:
01734        return FALSE;
01735 
01736       case 396:             /* Linux/hppa */
01737        /* pr_cursig */
01738        elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
01739 
01740        /* pr_pid */
01741        elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
01742 
01743        /* pr_reg */
01744        offset = 72;
01745        size = 320;
01746 
01747        break;
01748     }
01749 
01750   /* Make a ".reg/999" section.  */
01751   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
01752                                      size, note->descpos + offset);
01753 }
01754 
01755 static bfd_boolean
01756 elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
01757 {
01758   switch (note->descsz)
01759     {
01760       default:
01761        return FALSE;
01762 
01763       case 124:             /* Linux/hppa elf_prpsinfo.  */
01764        elf_tdata (abfd)->core_program
01765          = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
01766        elf_tdata (abfd)->core_command
01767          = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
01768     }
01769 
01770   /* Note that for some reason, a spurious space is tacked
01771      onto the end of the args in some (at least one anyway)
01772      implementations, so strip it off if it exists.  */
01773   {
01774     char *command = elf_tdata (abfd)->core_command;
01775     int n = strlen (command);
01776 
01777     if (0 < n && command[n - 1] == ' ')
01778       command[n - 1] = '\0';
01779   }
01780 
01781   return TRUE;
01782 }
01783 
01784 /* Our own version of hide_symbol, so that we can keep plt entries for
01785    plabels.  */
01786 
01787 static void
01788 elf32_hppa_hide_symbol (struct bfd_link_info *info,
01789                      struct elf_link_hash_entry *eh,
01790                      bfd_boolean force_local)
01791 {
01792   if (force_local)
01793     {
01794       eh->forced_local = 1;
01795       if (eh->dynindx != -1)
01796        {
01797          eh->dynindx = -1;
01798          _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
01799                               eh->dynstr_index);
01800        }
01801     }
01802 
01803   if (! hppa_elf_hash_entry (eh)->plabel)
01804     {
01805       eh->needs_plt = 0;
01806       eh->plt = elf_hash_table (info)->init_plt_refcount;
01807     }
01808 }
01809 
01810 /* Adjust a symbol defined by a dynamic object and referenced by a
01811    regular object.  The current definition is in some section of the
01812    dynamic object, but we're not including those sections.  We have to
01813    change the definition to something the rest of the link can
01814    understand.  */
01815 
01816 static bfd_boolean
01817 elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
01818                               struct elf_link_hash_entry *eh)
01819 {
01820   struct elf32_hppa_link_hash_table *htab;
01821   asection *sec;
01822   unsigned int power_of_two;
01823 
01824   /* If this is a function, put it in the procedure linkage table.  We
01825      will fill in the contents of the procedure linkage table later.  */
01826   if (eh->type == STT_FUNC
01827       || eh->needs_plt)
01828     {
01829       if (eh->plt.refcount <= 0
01830          || (eh->def_regular
01831              && eh->root.type != bfd_link_hash_defweak
01832              && ! hppa_elf_hash_entry (eh)->plabel
01833              && (!info->shared || info->symbolic)))
01834        {
01835          /* The .plt entry is not needed when:
01836             a) Garbage collection has removed all references to the
01837             symbol, or
01838             b) We know for certain the symbol is defined in this
01839             object, and it's not a weak definition, nor is the symbol
01840             used by a plabel relocation.  Either this object is the
01841             application or we are doing a shared symbolic link.  */
01842 
01843          eh->plt.offset = (bfd_vma) -1;
01844          eh->needs_plt = 0;
01845        }
01846 
01847       return TRUE;
01848     }
01849   else
01850     eh->plt.offset = (bfd_vma) -1;
01851 
01852   /* If this is a weak symbol, and there is a real definition, the
01853      processor independent code will have arranged for us to see the
01854      real definition first, and we can just use the same value.  */
01855   if (eh->u.weakdef != NULL)
01856     {
01857       if (eh->u.weakdef->root.type != bfd_link_hash_defined
01858          && eh->u.weakdef->root.type != bfd_link_hash_defweak)
01859        abort ();
01860       eh->root.u.def.section = eh->u.weakdef->root.u.def.section;
01861       eh->root.u.def.value = eh->u.weakdef->root.u.def.value;
01862       if (ELIMINATE_COPY_RELOCS)
01863        eh->non_got_ref = eh->u.weakdef->non_got_ref;
01864       return TRUE;
01865     }
01866 
01867   /* This is a reference to a symbol defined by a dynamic object which
01868      is not a function.  */
01869 
01870   /* If we are creating a shared library, we must presume that the
01871      only references to the symbol are via the global offset table.
01872      For such cases we need not do anything here; the relocations will
01873      be handled correctly by relocate_section.  */
01874   if (info->shared)
01875     return TRUE;
01876 
01877   /* If there are no references to this symbol that do not use the
01878      GOT, we don't need to generate a copy reloc.  */
01879   if (!eh->non_got_ref)
01880     return TRUE;
01881 
01882   if (ELIMINATE_COPY_RELOCS)
01883     {
01884       struct elf32_hppa_link_hash_entry *hh;
01885       struct elf32_hppa_dyn_reloc_entry *hdh_p;
01886 
01887       hh = hppa_elf_hash_entry (eh);
01888       for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
01889        {
01890          sec = hdh_p->sec->output_section;
01891          if (sec != NULL && (sec->flags & SEC_READONLY) != 0)
01892            break;
01893        }
01894 
01895       /* If we didn't find any dynamic relocs in read-only sections, then
01896         we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
01897       if (hdh_p == NULL)
01898        {
01899          eh->non_got_ref = 0;
01900          return TRUE;
01901        }
01902     }
01903 
01904   if (eh->size == 0)
01905     {
01906       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
01907                           eh->root.root.string);
01908       return TRUE;
01909     }
01910 
01911   /* We must allocate the symbol in our .dynbss section, which will
01912      become part of the .bss section of the executable.  There will be
01913      an entry for this symbol in the .dynsym section.  The dynamic
01914      object will contain position independent code, so all references
01915      from the dynamic object to this symbol will go through the global
01916      offset table.  The dynamic linker will use the .dynsym entry to
01917      determine the address it must put in the global offset table, so
01918      both the dynamic object and the regular object will refer to the
01919      same memory location for the variable.  */
01920 
01921   htab = hppa_link_hash_table (info);
01922 
01923   /* We must generate a COPY reloc to tell the dynamic linker to
01924      copy the initial value out of the dynamic object and into the
01925      runtime process image.  */
01926   if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0)
01927     {
01928       htab->srelbss->size += sizeof (Elf32_External_Rela);
01929       eh->needs_copy = 1;
01930     }
01931 
01932   /* We need to figure out the alignment required for this symbol.  I
01933      have no idea how other ELF linkers handle this.  */
01934 
01935   power_of_two = bfd_log2 (eh->size);
01936   if (power_of_two > 3)
01937     power_of_two = 3;
01938 
01939   /* Apply the required alignment.  */
01940   sec = htab->sdynbss;
01941   sec->size = BFD_ALIGN (sec->size, (bfd_size_type) (1 << power_of_two));
01942   if (power_of_two > bfd_get_section_alignment (htab->etab.dynobj, sec))
01943     {
01944       if (! bfd_set_section_alignment (htab->etab.dynobj, sec, power_of_two))
01945        return FALSE;
01946     }
01947 
01948   /* Define the symbol as being at this point in the section.  */
01949   eh->root.u.def.section = sec;
01950   eh->root.u.def.value = sec->size;
01951 
01952   /* Increment the section size to make room for the symbol.  */
01953   sec->size += eh->size;
01954 
01955   return TRUE;
01956 }
01957 
01958 /* Allocate space in the .plt for entries that won't have relocations.
01959    ie. plabel entries.  */
01960 
01961 static bfd_boolean
01962 allocate_plt_static (struct elf_link_hash_entry *eh, void *inf)
01963 {
01964   struct bfd_link_info *info;
01965   struct elf32_hppa_link_hash_table *htab;
01966   struct elf32_hppa_link_hash_entry *hh;
01967   asection *sec;
01968 
01969   if (eh->root.type == bfd_link_hash_indirect)
01970     return TRUE;
01971 
01972   if (eh->root.type == bfd_link_hash_warning)
01973     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
01974 
01975   info = (struct bfd_link_info *) inf;
01976   hh = hppa_elf_hash_entry (eh);
01977   htab = hppa_link_hash_table (info);
01978   if (htab->etab.dynamic_sections_created
01979       && eh->plt.refcount > 0)
01980     {
01981       /* Make sure this symbol is output as a dynamic symbol.
01982         Undefined weak syms won't yet be marked as dynamic.  */
01983       if (eh->dynindx == -1
01984          && !eh->forced_local
01985          && eh->type != STT_PARISC_MILLI)
01986        {
01987          if (! bfd_elf_link_record_dynamic_symbol (info, eh))
01988            return FALSE;
01989        }
01990 
01991       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, eh))
01992        {
01993          /* Allocate these later.  From this point on, h->plabel
01994             means that the plt entry is only used by a plabel.
01995             We'll be using a normal plt entry for this symbol, so
01996             clear the plabel indicator.  */
01997          
01998          hh->plabel = 0;
01999        }
02000       else if (hh->plabel)
02001        {
02002          /* Make an entry in the .plt section for plabel references
02003             that won't have a .plt entry for other reasons.  */
02004          sec = htab->splt;
02005          eh->plt.offset = sec->size;
02006          sec->size += PLT_ENTRY_SIZE;
02007        }
02008       else
02009        {
02010          /* No .plt entry needed.  */
02011          eh->plt.offset = (bfd_vma) -1;
02012          eh->needs_plt = 0;
02013        }
02014     }
02015   else
02016     {
02017       eh->plt.offset = (bfd_vma) -1;
02018       eh->needs_plt = 0;
02019     }
02020 
02021   return TRUE;
02022 }
02023 
02024 /* Allocate space in .plt, .got and associated reloc sections for
02025    global syms.  */
02026 
02027 static bfd_boolean
02028 allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
02029 {
02030   struct bfd_link_info *info;
02031   struct elf32_hppa_link_hash_table *htab;
02032   asection *sec;
02033   struct elf32_hppa_link_hash_entry *hh;
02034   struct elf32_hppa_dyn_reloc_entry *hdh_p;
02035 
02036   if (eh->root.type == bfd_link_hash_indirect)
02037     return TRUE;
02038 
02039   if (eh->root.type == bfd_link_hash_warning)
02040     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
02041 
02042   info = inf;
02043   htab = hppa_link_hash_table (info);
02044   hh = hppa_elf_hash_entry (eh);
02045   
02046   if (htab->etab.dynamic_sections_created
02047       && eh->plt.offset != (bfd_vma) -1
02048       && !hh->plabel
02049       && eh->plt.refcount > 0)
02050     {
02051       /* Make an entry in the .plt section.  */
02052       sec = htab->splt;
02053       eh->plt.offset = sec->size;
02054       sec->size += PLT_ENTRY_SIZE;
02055 
02056       /* We also need to make an entry in the .rela.plt section.  */
02057       htab->srelplt->size += sizeof (Elf32_External_Rela);
02058       htab->need_plt_stub = 1;
02059     }
02060 
02061   if (eh->got.refcount > 0)
02062     {
02063       /* Make sure this symbol is output as a dynamic symbol.
02064         Undefined weak syms won't yet be marked as dynamic.  */
02065       if (eh->dynindx == -1
02066          && !eh->forced_local
02067          && eh->type != STT_PARISC_MILLI)
02068        {
02069          if (! bfd_elf_link_record_dynamic_symbol (info, eh))
02070            return FALSE;
02071        }
02072 
02073       sec = htab->sgot;
02074       eh->got.offset = sec->size;
02075       sec->size += GOT_ENTRY_SIZE;
02076       /* R_PARISC_TLS_GD* needs two GOT entries */
02077       if ((hh->tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
02078        sec->size += GOT_ENTRY_SIZE * 2;
02079       else if ((hh->tls_type & GOT_TLS_GD) == GOT_TLS_GD)
02080        sec->size += GOT_ENTRY_SIZE;
02081       if (htab->etab.dynamic_sections_created
02082          && (info->shared
02083              || (eh->dynindx != -1
02084                 && !eh->forced_local)))
02085        {
02086          htab->srelgot->size += sizeof (Elf32_External_Rela);
02087          if ((hh->tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
02088            htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
02089          else if ((hh->tls_type & GOT_TLS_GD) == GOT_TLS_GD)
02090            htab->srelgot->size += sizeof (Elf32_External_Rela);
02091        }
02092     }
02093   else
02094     eh->got.offset = (bfd_vma) -1;
02095 
02096   if (hh->dyn_relocs == NULL)
02097     return TRUE;
02098 
02099   /* If this is a -Bsymbolic shared link, then we need to discard all
02100      space allocated for dynamic pc-relative relocs against symbols
02101      defined in a regular object.  For the normal shared case, discard
02102      space for relocs that have become local due to symbol visibility
02103      changes.  */
02104   if (info->shared)
02105     {
02106 #if RELATIVE_DYNRELOCS
02107       if (SYMBOL_CALLS_LOCAL (info, eh))
02108        {
02109          struct elf32_hppa_dyn_reloc_entry **hdh_pp;
02110 
02111          for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
02112            {
02113              hdh_p->count -= hdh_p->relative_count;
02114              hdh_p->relative_count = 0;
02115              if (hdh_p->count == 0)
02116               *hdh_pp = hdh_p->hdh_next;
02117              else
02118               hdh_pp = &hdh_p->hdh_next;
02119            }
02120        }
02121 #endif
02122 
02123       /* Also discard relocs on undefined weak syms with non-default
02124         visibility.  */
02125       if (hh->dyn_relocs != NULL
02126          && eh->root.type == bfd_link_hash_undefweak)
02127        {
02128          if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
02129            hh->dyn_relocs = NULL;
02130 
02131          /* Make sure undefined weak symbols are output as a dynamic
02132             symbol in PIEs.  */
02133          else if (eh->dynindx == -1
02134                  && !eh->forced_local)
02135            {
02136              if (! bfd_elf_link_record_dynamic_symbol (info, eh))
02137               return FALSE;
02138            }
02139        }
02140     }
02141   else
02142     {
02143       /* For the non-shared case, discard space for relocs against
02144         symbols which turn out to need copy relocs or are not
02145         dynamic.  */
02146       
02147       if (!eh->non_got_ref
02148          && ((ELIMINATE_COPY_RELOCS
02149               && eh->def_dynamic
02150               && !eh->def_regular)
02151               || (htab->etab.dynamic_sections_created
02152                  && (eh->root.type == bfd_link_hash_undefweak
02153                      || eh->root.type == bfd_link_hash_undefined))))
02154        {
02155          /* Make sure this symbol is output as a dynamic symbol.
02156             Undefined weak syms won't yet be marked as dynamic.  */
02157          if (eh->dynindx == -1
02158              && !eh->forced_local
02159              && eh->type != STT_PARISC_MILLI)
02160            {
02161              if (! bfd_elf_link_record_dynamic_symbol (info, eh))
02162               return FALSE;
02163            }
02164 
02165          /* If that succeeded, we know we'll be keeping all the
02166             relocs.  */
02167          if (eh->dynindx != -1)
02168            goto keep;
02169        }
02170 
02171       hh->dyn_relocs = NULL;
02172       return TRUE;
02173 
02174     keep: ;
02175     }
02176 
02177   /* Finally, allocate space.  */
02178   for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
02179     {
02180       asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
02181       sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
02182     }
02183 
02184   return TRUE;
02185 }
02186 
02187 /* This function is called via elf_link_hash_traverse to force
02188    millicode symbols local so they do not end up as globals in the
02189    dynamic symbol table.  We ought to be able to do this in
02190    adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
02191    for all dynamic symbols.  Arguably, this is a bug in
02192    elf_adjust_dynamic_symbol.  */
02193 
02194 static bfd_boolean
02195 clobber_millicode_symbols (struct elf_link_hash_entry *eh,
02196                         struct bfd_link_info *info)
02197 {
02198   if (eh->root.type == bfd_link_hash_warning)
02199     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
02200 
02201   if (eh->type == STT_PARISC_MILLI
02202       && !eh->forced_local)
02203     {
02204       elf32_hppa_hide_symbol (info, eh, TRUE);
02205     }
02206   return TRUE;
02207 }
02208 
02209 /* Find any dynamic relocs that apply to read-only sections.  */
02210 
02211 static bfd_boolean
02212 readonly_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
02213 {
02214   struct elf32_hppa_link_hash_entry *hh;
02215   struct elf32_hppa_dyn_reloc_entry *hdh_p;
02216 
02217   if (eh->root.type == bfd_link_hash_warning)
02218     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
02219 
02220   hh = hppa_elf_hash_entry (eh);
02221   for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
02222     {
02223       asection *sec = hdh_p->sec->output_section;
02224 
02225       if (sec != NULL && (sec->flags & SEC_READONLY) != 0)
02226        {
02227          struct bfd_link_info *info = inf;
02228 
02229          info->flags |= DF_TEXTREL;
02230 
02231          /* Not an error, just cut short the traversal.  */
02232          return FALSE;
02233        }
02234     }
02235   return TRUE;
02236 }
02237 
02238 /* Set the sizes of the dynamic sections.  */
02239 
02240 static bfd_boolean
02241 elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
02242                               struct bfd_link_info *info)
02243 {
02244   struct elf32_hppa_link_hash_table *htab;
02245   bfd *dynobj;
02246   bfd *ibfd;
02247   asection *sec;
02248   bfd_boolean relocs;
02249 
02250   htab = hppa_link_hash_table (info);
02251   dynobj = htab->etab.dynobj;
02252   if (dynobj == NULL)
02253     abort ();
02254 
02255   if (htab->etab.dynamic_sections_created)
02256     {
02257       /* Set the contents of the .interp section to the interpreter.  */
02258       if (info->executable)
02259        {
02260          sec = bfd_get_section_by_name (dynobj, ".interp");
02261          if (sec == NULL)
02262            abort ();
02263          sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
02264          sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
02265        }
02266 
02267       /* Force millicode symbols local.  */
02268       elf_link_hash_traverse (&htab->etab,
02269                            clobber_millicode_symbols,
02270                            info);
02271     }
02272 
02273   /* Set up .got and .plt offsets for local syms, and space for local
02274      dynamic relocs.  */
02275   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
02276     {
02277       bfd_signed_vma *local_got;
02278       bfd_signed_vma *end_local_got;
02279       bfd_signed_vma *local_plt;
02280       bfd_signed_vma *end_local_plt;
02281       bfd_size_type locsymcount;
02282       Elf_Internal_Shdr *symtab_hdr;
02283       asection *srel;
02284       char *local_tls_type;
02285 
02286       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
02287        continue;
02288 
02289       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
02290        {
02291          struct elf32_hppa_dyn_reloc_entry *hdh_p;
02292 
02293          for (hdh_p = ((struct elf32_hppa_dyn_reloc_entry *)
02294                   elf_section_data (sec)->local_dynrel);
02295               hdh_p != NULL;
02296               hdh_p = hdh_p->hdh_next)
02297            {
02298              if (!bfd_is_abs_section (hdh_p->sec)
02299                 && bfd_is_abs_section (hdh_p->sec->output_section))
02300               {
02301                 /* Input section has been discarded, either because
02302                    it is a copy of a linkonce section or due to
02303                    linker script /DISCARD/, so we'll be discarding
02304                    the relocs too.  */
02305               }
02306              else if (hdh_p->count != 0)
02307               {
02308                 srel = elf_section_data (hdh_p->sec)->sreloc;
02309                 srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
02310                 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
02311                   info->flags |= DF_TEXTREL;
02312               }
02313            }
02314        }
02315 
02316       local_got = elf_local_got_refcounts (ibfd);
02317       if (!local_got)
02318        continue;
02319 
02320       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
02321       locsymcount = symtab_hdr->sh_info;
02322       end_local_got = local_got + locsymcount;
02323       local_tls_type = hppa_elf_local_got_tls_type (ibfd);
02324       sec = htab->sgot;
02325       srel = htab->srelgot;
02326       for (; local_got < end_local_got; ++local_got)
02327        {
02328          if (*local_got > 0)
02329            {
02330              *local_got = sec->size;
02331              sec->size += GOT_ENTRY_SIZE;
02332              if ((*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
02333               sec->size += 2 * GOT_ENTRY_SIZE;
02334              else if ((*local_tls_type & GOT_TLS_GD) == GOT_TLS_GD)
02335               sec->size += GOT_ENTRY_SIZE;
02336              if (info->shared) 
02337                {
02338                 srel->size += sizeof (Elf32_External_Rela);
02339                 if ((*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
02340                   srel->size += 2 * sizeof (Elf32_External_Rela);
02341                 else if ((*local_tls_type & GOT_TLS_GD) == GOT_TLS_GD)
02342                   srel->size += sizeof (Elf32_External_Rela);
02343                }
02344            }
02345          else
02346            *local_got = (bfd_vma) -1;
02347 
02348          ++local_tls_type;
02349        }
02350 
02351       local_plt = end_local_got;
02352       end_local_plt = local_plt + locsymcount;
02353       if (! htab->etab.dynamic_sections_created)
02354        {
02355          /* Won't be used, but be safe.  */
02356          for (; local_plt < end_local_plt; ++local_plt)
02357            *local_plt = (bfd_vma) -1;
02358        }
02359       else
02360        {
02361          sec = htab->splt;
02362          srel = htab->srelplt;
02363          for (; local_plt < end_local_plt; ++local_plt)
02364            {
02365              if (*local_plt > 0)
02366               {
02367                 *local_plt = sec->size;
02368                 sec->size += PLT_ENTRY_SIZE;
02369                 if (info->shared)
02370                   srel->size += sizeof (Elf32_External_Rela);
02371               }
02372              else
02373               *local_plt = (bfd_vma) -1;
02374            }
02375        }
02376     }
02377   
02378   if (htab->tls_ldm_got.refcount > 0)
02379     {
02380       /* Allocate 2 got entries and 1 dynamic reloc for 
02381          R_PARISC_TLS_DTPMOD32 relocs.  */
02382       htab->tls_ldm_got.offset = htab->sgot->size;
02383       htab->sgot->size += (GOT_ENTRY_SIZE * 2);
02384       htab->srelgot->size += sizeof (Elf32_External_Rela);
02385     }
02386   else
02387     htab->tls_ldm_got.offset = -1;
02388 
02389   /* Do all the .plt entries without relocs first.  The dynamic linker
02390      uses the last .plt reloc to find the end of the .plt (and hence
02391      the start of the .got) for lazy linking.  */
02392   elf_link_hash_traverse (&htab->etab, allocate_plt_static, info);
02393 
02394   /* Allocate global sym .plt and .got entries, and space for global
02395      sym dynamic relocs.  */
02396   elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
02397 
02398   /* The check_relocs and adjust_dynamic_symbol entry points have
02399      determined the sizes of the various dynamic sections.  Allocate
02400      memory for them.  */
02401   relocs = FALSE;
02402   for (sec = dynobj->sections; sec != NULL; sec = sec->next)
02403     {
02404       if ((sec->flags & SEC_LINKER_CREATED) == 0)
02405        continue;
02406 
02407       if (sec == htab->splt)
02408        {
02409          if (htab->need_plt_stub)
02410            {
02411              /* Make space for the plt stub at the end of the .plt
02412                section.  We want this stub right at the end, up
02413                against the .got section.  */
02414              int gotalign = bfd_section_alignment (dynobj, htab->sgot);
02415              int pltalign = bfd_section_alignment (dynobj, sec);
02416              bfd_size_type mask;
02417 
02418              if (gotalign > pltalign)
02419               bfd_set_section_alignment (dynobj, sec, gotalign);
02420              mask = ((bfd_size_type) 1 << gotalign) - 1;
02421              sec->size = (sec->size + sizeof (plt_stub) + mask) & ~mask;
02422            }
02423        }
02424       else if (sec == htab->sgot
02425               || sec == htab->sdynbss)
02426        ;
02427       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, sec), ".rela"))
02428        {
02429          if (sec->size != 0)
02430            {
02431              /* Remember whether there are any reloc sections other
02432                than .rela.plt.  */
02433              if (sec != htab->srelplt)
02434               relocs = TRUE;
02435 
02436              /* We use the reloc_count field as a counter if we need
02437                to copy relocs into the output file.  */
02438              sec->reloc_count = 0;
02439            }
02440        }
02441       else
02442        {
02443          /* It's not one of our sections, so don't allocate space.  */
02444          continue;
02445        }
02446 
02447       if (sec->size == 0)
02448        {
02449          /* If we don't need this section, strip it from the
02450             output file.  This is mostly to handle .rela.bss and
02451             .rela.plt.  We must create both sections in
02452             create_dynamic_sections, because they must be created
02453             before the linker maps input sections to output
02454             sections.  The linker does that before
02455             adjust_dynamic_symbol is called, and it is that
02456             function which decides whether anything needs to go
02457             into these sections.  */
02458          sec->flags |= SEC_EXCLUDE;
02459          continue;
02460        }
02461 
02462       if ((sec->flags & SEC_HAS_CONTENTS) == 0)
02463        continue;
02464 
02465       /* Allocate memory for the section contents.  Zero it, because
02466         we may not fill in all the reloc sections.  */
02467       sec->contents = bfd_zalloc (dynobj, sec->size);
02468       if (sec->contents == NULL)
02469        return FALSE;
02470     }
02471 
02472   if (htab->etab.dynamic_sections_created)
02473     {
02474       /* Like IA-64 and HPPA64, always create a DT_PLTGOT.  It
02475         actually has nothing to do with the PLT, it is how we
02476         communicate the LTP value of a load module to the dynamic
02477         linker.  */
02478 #define add_dynamic_entry(TAG, VAL) \
02479   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
02480 
02481       if (!add_dynamic_entry (DT_PLTGOT, 0))
02482        return FALSE;
02483 
02484       /* Add some entries to the .dynamic section.  We fill in the
02485         values later, in elf32_hppa_finish_dynamic_sections, but we
02486         must add the entries now so that we get the correct size for
02487         the .dynamic section.  The DT_DEBUG entry is filled in by the
02488         dynamic linker and used by the debugger.  */
02489       if (info->executable)
02490        {
02491          if (!add_dynamic_entry (DT_DEBUG, 0))
02492            return FALSE;
02493        }
02494 
02495       if (htab->srelplt->size != 0)
02496        {
02497          if (!add_dynamic_entry (DT_PLTRELSZ, 0)
02498              || !add_dynamic_entry (DT_PLTREL, DT_RELA)
02499              || !add_dynamic_entry (DT_JMPREL, 0))
02500            return FALSE;
02501        }
02502 
02503       if (relocs)
02504        {
02505          if (!add_dynamic_entry (DT_RELA, 0)
02506              || !add_dynamic_entry (DT_RELASZ, 0)
02507              || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
02508            return FALSE;
02509 
02510          /* If any dynamic relocs apply to a read-only section,
02511             then we need a DT_TEXTREL entry.  */
02512          if ((info->flags & DF_TEXTREL) == 0)
02513            elf_link_hash_traverse (&htab->etab, readonly_dynrelocs, info);
02514 
02515          if ((info->flags & DF_TEXTREL) != 0)
02516            {
02517              if (!add_dynamic_entry (DT_TEXTREL, 0))
02518               return FALSE;
02519            }
02520        }
02521     }
02522 #undef add_dynamic_entry
02523 
02524   return TRUE;
02525 }
02526 
02527 /* External entry points for sizing and building linker stubs.  */
02528 
02529 /* Set up various things so that we can make a list of input sections
02530    for each output section included in the link.  Returns -1 on error,
02531    0 when no stubs will be needed, and 1 on success.  */
02532 
02533 int
02534 elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
02535 {
02536   bfd *input_bfd;
02537   unsigned int bfd_count;
02538   int top_id, top_index;
02539   asection *section;
02540   asection **input_list, **list;
02541   bfd_size_type amt;
02542   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
02543 
02544   /* Count the number of input BFDs and find the top input section id.  */
02545   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
02546        input_bfd != NULL;
02547        input_bfd = input_bfd->link_next)
02548     {
02549       bfd_count += 1;
02550       for (section = input_bfd->sections;
02551           section != NULL;
02552           section = section->next)
02553        {
02554          if (top_id < section->id)
02555            top_id = section->id;
02556        }
02557     }
02558   htab->bfd_count = bfd_count;
02559 
02560   amt = sizeof (struct map_stub) * (top_id + 1);
02561   htab->stub_group = bfd_zmalloc (amt);
02562   if (htab->stub_group == NULL)
02563     return -1;
02564 
02565   /* We can't use output_bfd->section_count here to find the top output
02566      section index as some sections may have been removed, and
02567      strip_excluded_output_sections doesn't renumber the indices.  */
02568   for (section = output_bfd->sections, top_index = 0;
02569        section != NULL;
02570        section = section->next)
02571     {
02572       if (top_index < section->index)
02573        top_index = section->index;
02574     }
02575 
02576   htab->top_index = top_index;
02577   amt = sizeof (asection *) * (top_index + 1);
02578   input_list = bfd_malloc (amt);
02579   htab->input_list = input_list;
02580   if (input_list == NULL)
02581     return -1;
02582 
02583   /* For sections we aren't interested in, mark their entries with a
02584      value we can check later.  */
02585   list = input_list + top_index;
02586   do
02587     *list = bfd_abs_section_ptr;
02588   while (list-- != input_list);
02589 
02590   for (section = output_bfd->sections;
02591        section != NULL;
02592        section = section->next)
02593     {
02594       if ((section->flags & SEC_CODE) != 0)
02595        input_list[section->index] = NULL;
02596     }
02597 
02598   return 1;
02599 }
02600 
02601 /* The linker repeatedly calls this function for each input section,
02602    in the order that input sections are linked into output sections.
02603    Build lists of input sections to determine groupings between which
02604    we may insert linker stubs.  */
02605 
02606 void
02607 elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
02608 {
02609   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
02610 
02611   if (isec->output_section->index <= htab->top_index)
02612     {
02613       asection **list = htab->input_list + isec->output_section->index;
02614       if (*list != bfd_abs_section_ptr)
02615        {
02616          /* Steal the link_sec pointer for our list.  */
02617 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
02618          /* This happens to make the list in reverse order,
02619             which is what we want.  */
02620          PREV_SEC (isec) = *list;
02621          *list = isec;
02622        }
02623     }
02624 }
02625 
02626 /* See whether we can group stub sections together.  Grouping stub
02627    sections may result in fewer stubs.  More importantly, we need to
02628    put all .init* and .fini* stubs at the beginning of the .init or
02629    .fini output sections respectively, because glibc splits the
02630    _init and _fini functions into multiple parts.  Putting a stub in
02631    the middle of a function is not a good idea.  */
02632 
02633 static void
02634 group_sections (struct elf32_hppa_link_hash_table *htab,
02635               bfd_size_type stub_group_size,
02636               bfd_boolean stubs_always_before_branch)
02637 {
02638   asection **list = htab->input_list + htab->top_index;
02639   do
02640     {
02641       asection *tail = *list;
02642       if (tail == bfd_abs_section_ptr)
02643        continue;
02644       while (tail != NULL)
02645        {
02646          asection *curr;
02647          asection *prev;
02648          bfd_size_type total;
02649          bfd_boolean big_sec;
02650 
02651          curr = tail;
02652          total = tail->size;
02653          big_sec = total >= stub_group_size;
02654 
02655          while ((prev = PREV_SEC (curr)) != NULL
02656                && ((total += curr->output_offset - prev->output_offset)
02657                    < stub_group_size))
02658            curr = prev;
02659 
02660          /* OK, the size from the start of CURR to the end is less
02661             than 240000 bytes and thus can be handled by one stub
02662             section.  (or the tail section is itself larger than
02663             240000 bytes, in which case we may be toast.)
02664             We should really be keeping track of the total size of
02665             stubs added here, as stubs contribute to the final output
02666             section size.  That's a little tricky, and this way will
02667             only break if stubs added total more than 22144 bytes, or
02668             2768 long branch stubs.  It seems unlikely for more than
02669             2768 different functions to be called, especially from
02670             code only 240000 bytes long.  This limit used to be
02671             250000, but c++ code tends to generate lots of little
02672             functions, and sometimes violated the assumption.  */
02673          do
02674            {
02675              prev = PREV_SEC (tail);
02676              /* Set up this stub group.  */
02677              htab->stub_group[tail->id].link_sec = curr;
02678            }
02679          while (tail != curr && (tail = prev) != NULL);
02680 
02681          /* But wait, there's more!  Input sections up to 240000
02682             bytes before the stub section can be handled by it too.
02683             Don't do this if we have a really large section after the
02684             stubs, as adding more stubs increases the chance that
02685             branches may not reach into the stub section.  */
02686          if (!stubs_always_before_branch && !big_sec)
02687            {
02688              total = 0;
02689              while (prev != NULL
02690                    && ((total += tail->output_offset - prev->output_offset)
02691                       < stub_group_size))
02692               {
02693                 tail = prev;
02694                 prev = PREV_SEC (tail);
02695                 htab->stub_group[tail->id].link_sec = curr;
02696               }
02697            }
02698          tail = prev;
02699        }
02700     }
02701   while (list-- != htab->input_list);
02702   free (htab->input_list);
02703 #undef PREV_SEC
02704 }
02705 
02706 /* Read in all local syms for all input bfds, and create hash entries
02707    for export stubs if we are building a multi-subspace shared lib.
02708    Returns -1 on error, 1 if export stubs created, 0 otherwise.  */
02709 
02710 static int
02711 get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
02712 {
02713   unsigned int bfd_indx;
02714   Elf_Internal_Sym *local_syms, **all_local_syms;
02715   int stub_changed = 0;
02716   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
02717 
02718   /* We want to read in symbol extension records only once.  To do this
02719      we need to read in the local symbols in parallel and save them for
02720      later use; so hold pointers to the local symbols in an array.  */
02721   bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
02722   all_local_syms = bfd_zmalloc (amt);
02723   htab->all_local_syms = all_local_syms;
02724   if (all_local_syms == NULL)
02725     return -1;
02726 
02727   /* Walk over all the input BFDs, swapping in local symbols.
02728      If we are creating a shared library, create hash entries for the
02729      export stubs.  */
02730   for (bfd_indx = 0;
02731        input_bfd != NULL;
02732        input_bfd = input_bfd->link_next, bfd_indx++)
02733     {
02734       Elf_Internal_Shdr *symtab_hdr;
02735 
02736       /* We'll need the symbol table in a second.  */
02737       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02738       if (symtab_hdr->sh_info == 0)
02739        continue;
02740 
02741       /* We need an array of the local symbols attached to the input bfd.  */
02742       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
02743       if (local_syms == NULL)
02744        {
02745          local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
02746                                         symtab_hdr->sh_info, 0,
02747                                         NULL, NULL, NULL);
02748          /* Cache them for elf_link_input_bfd.  */
02749          symtab_hdr->contents = (unsigned char *) local_syms;
02750        }
02751       if (local_syms == NULL)
02752        return -1;
02753 
02754       all_local_syms[bfd_indx] = local_syms;
02755 
02756       if (info->shared && htab->multi_subspace)
02757        {
02758          struct elf_link_hash_entry **eh_syms;
02759          struct elf_link_hash_entry **eh_symend;
02760          unsigned int symcount;
02761 
02762          symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
02763                     - symtab_hdr->sh_info);
02764          eh_syms = (struct elf_link_hash_entry **) elf_sym_hashes (input_bfd);
02765          eh_symend = (struct elf_link_hash_entry **) (eh_syms + symcount);
02766 
02767          /* Look through the global syms for functions;  We need to
02768             build export stubs for all globally visible functions.  */
02769          for (; eh_syms < eh_symend; eh_syms++)
02770            {
02771              struct elf32_hppa_link_hash_entry *hh;
02772 
02773              hh = hppa_elf_hash_entry (*eh_syms);
02774 
02775              while (hh->eh.root.type == bfd_link_hash_indirect
02776                    || hh->eh.root.type == bfd_link_hash_warning)
02777                  hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
02778 
02779              /* At this point in the link, undefined syms have been
02780                resolved, so we need to check that the symbol was
02781                defined in this BFD.  */
02782              if ((hh->eh.root.type == bfd_link_hash_defined
02783                  || hh->eh.root.type == bfd_link_hash_defweak)
02784                 && hh->eh.type == STT_FUNC
02785                 && hh->eh.root.u.def.section->output_section != NULL
02786                 && (hh->eh.root.u.def.section->output_section->owner
02787                     == output_bfd)
02788                 && hh->eh.root.u.def.section->owner == input_bfd
02789                 && hh->eh.def_regular
02790                 && !hh->eh.forced_local
02791                 && ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT)
02792               {
02793                 asection *sec;
02794                 const char *stub_name;
02795                 struct elf32_hppa_stub_hash_entry *hsh;
02796 
02797                 sec = hh->eh.root.u.def.section;
02798                 stub_name = hh_name (hh);
02799                 hsh = hppa_stub_hash_lookup (&htab->bstab,
02800                                                 stub_name,
02801                                                 FALSE, FALSE);
02802                 if (hsh == NULL)
02803                   {
02804                     hsh = hppa_add_stub (stub_name, sec, htab);
02805                     if (!hsh)
02806                      return -1;
02807 
02808                     hsh->target_value = hh->eh.root.u.def.value;
02809                     hsh->target_section = hh->eh.root.u.def.section;
02810                     hsh->stub_type = hppa_stub_export;
02811                     hsh->hh = hh;
02812                     stub_changed = 1;
02813                   }
02814                 else
02815                   {
02816                     (*_bfd_error_handler) (_("%B: duplicate export stub %s"),
02817                                         input_bfd,
02818                                         stub_name);
02819                   }
02820               }
02821            }
02822        }
02823     }
02824 
02825   return stub_changed;
02826 }
02827 
02828 /* Determine and set the size of the stub section for a final link.
02829 
02830    The basic idea here is to examine all the relocations looking for
02831    PC-relative calls to a target that is unreachable with a "bl"
02832    instruction.  */
02833 
02834 bfd_boolean
02835 elf32_hppa_size_stubs
02836   (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
02837    bfd_boolean multi_subspace, bfd_signed_vma group_size,
02838    asection * (*add_stub_section) (const char *, asection *),
02839    void (*layout_sections_again) (void))
02840 {
02841   bfd_size_type stub_group_size;
02842   bfd_boolean stubs_always_before_branch;
02843   bfd_boolean stub_changed;
02844   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
02845 
02846   /* Stash our params away.  */
02847   htab->stub_bfd = stub_bfd;
02848   htab->multi_subspace = multi_subspace;
02849   htab->add_stub_section = add_stub_section;
02850   htab->layout_sections_again = layout_sections_again;
02851   stubs_always_before_branch = group_size < 0;
02852   if (group_size < 0)
02853     stub_group_size = -group_size;
02854   else
02855     stub_group_size = group_size;
02856   if (stub_group_size == 1)
02857     {
02858       /* Default values.  */
02859       if (stubs_always_before_branch)
02860        {
02861          stub_group_size = 7680000;
02862          if (htab->has_17bit_branch || htab->multi_subspace)
02863            stub_group_size = 240000;
02864          if (htab->has_12bit_branch)
02865            stub_group_size = 7500;
02866        }
02867       else
02868        {
02869          stub_group_size = 6971392;
02870          if (htab->has_17bit_branch || htab->multi_subspace)
02871            stub_group_size = 217856;
02872          if (htab->has_12bit_branch)
02873            stub_group_size = 6808;
02874        }
02875     }
02876 
02877   group_sections (htab, stub_group_size, stubs_always_before_branch);
02878 
02879   switch (get_local_syms (output_bfd, info->input_bfds, info))
02880     {
02881     default:
02882       if (htab->all_local_syms)
02883        goto error_ret_free_local;
02884       return FALSE;
02885 
02886     case 0:
02887       stub_changed = FALSE;
02888       break;
02889 
02890     case 1:
02891       stub_changed = TRUE;
02892       break;
02893     }
02894 
02895   while (1)
02896     {
02897       bfd *input_bfd;
02898       unsigned int bfd_indx;
02899       asection *stub_sec;
02900 
02901       for (input_bfd = info->input_bfds, bfd_indx = 0;
02902           input_bfd != NULL;
02903           input_bfd = input_bfd->link_next, bfd_indx++)
02904        {
02905          Elf_Internal_Shdr *symtab_hdr;
02906          asection *section;
02907          Elf_Internal_Sym *local_syms;
02908 
02909          /* We'll need the symbol table in a second.  */
02910          symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
02911          if (symtab_hdr->sh_info == 0)
02912            continue;
02913 
02914          local_syms = htab->all_local_syms[bfd_indx];
02915 
02916          /* Walk over each section attached to the input bfd.  */
02917          for (section = input_bfd->sections;
02918               section != NULL;
02919               section = section->next)
02920            {
02921              Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
02922 
02923              /* If there aren't any relocs, then there's nothing more
02924                to do.  */
02925              if ((section->flags & SEC_RELOC) == 0
02926                 || section->reloc_count == 0)
02927               continue;
02928 
02929              /* If this section is a link-once section that will be
02930                discarded, then don't create any stubs.  */
02931              if (section->output_section == NULL
02932                 || section->output_section->owner != output_bfd)
02933               continue;
02934 
02935              /* Get the relocs.  */
02936              internal_relocs
02937               = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
02938                                         info->keep_memory);
02939              if (internal_relocs == NULL)
02940               goto error_ret_free_local;
02941 
02942              /* Now examine each relocation.  */
02943              irela = internal_relocs;
02944              irelaend = irela + section->reloc_count;
02945              for (; irela < irelaend; irela++)
02946               {
02947                 unsigned int r_type, r_indx;
02948                 enum elf32_hppa_stub_type stub_type;
02949                 struct elf32_hppa_stub_hash_entry *hsh;
02950                 asection *sym_sec;
02951                 bfd_vma sym_value;
02952                 bfd_vma destination;
02953                 struct elf32_hppa_link_hash_entry *hh;
02954                 char *stub_name;
02955                 const asection *id_sec;
02956 
02957                 r_type = ELF32_R_TYPE (irela->r_info);
02958                 r_indx = ELF32_R_SYM (irela->r_info);
02959 
02960                 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
02961                   {
02962                     bfd_set_error (bfd_error_bad_value);
02963                   error_ret_free_internal:
02964                     if (elf_section_data (section)->relocs == NULL)
02965                      free (internal_relocs);
02966                     goto error_ret_free_local;
02967                   }
02968 
02969                 /* Only look for stubs on call instructions.  */
02970                 if (r_type != (unsigned int) R_PARISC_PCREL12F
02971                     && r_type != (unsigned int) R_PARISC_PCREL17F
02972                     && r_type != (unsigned int) R_PARISC_PCREL22F)
02973                   continue;
02974 
02975                 /* Now determine the call target, its name, value,
02976                    section.  */
02977                 sym_sec = NULL;
02978                 sym_value = 0;
02979                 destination = 0;
02980                 hh = NULL;
02981                 if (r_indx < symtab_hdr->sh_info)
02982                   {
02983                     /* It's a local symbol.  */
02984                     Elf_Internal_Sym *sym;
02985                     Elf_Internal_Shdr *hdr;
02986 
02987                     sym = local_syms + r_indx;
02988                     hdr = elf_elfsections (input_bfd)[sym->st_shndx];
02989                     sym_sec = hdr->bfd_section;
02990                     if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
02991                      sym_value = sym->st_value;
02992                     destination = (sym_value + irela->r_addend
02993                                  + sym_sec->output_offset
02994                                  + sym_sec->output_section->vma);
02995                   }
02996                 else
02997                   {
02998                     /* It's an external symbol.  */
02999                     int e_indx;
03000 
03001                     e_indx = r_indx - symtab_hdr->sh_info;
03002                     hh = hppa_elf_hash_entry (elf_sym_hashes (input_bfd)[e_indx]);
03003 
03004                     while (hh->eh.root.type == bfd_link_hash_indirect
03005                           || hh->eh.root.type == bfd_link_hash_warning)
03006                      hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
03007 
03008                     if (hh->eh.root.type == bfd_link_hash_defined
03009                        || hh->eh.root.type == bfd_link_hash_defweak)
03010                      {
03011                        sym_sec = hh->eh.root.u.def.section;
03012                        sym_value = hh->eh.root.u.def.value;
03013                        if (sym_sec->output_section != NULL)
03014                          destination = (sym_value + irela->r_addend
03015                                       + sym_sec->output_offset
03016                                       + sym_sec->output_section->vma);
03017                      }
03018                     else if (hh->eh.root.type == bfd_link_hash_undefweak)
03019                      {
03020                        if (! info->shared)
03021                          continue;
03022                      }
03023                     else if (hh->eh.root.type == bfd_link_hash_undefined)
03024                      {
03025                        if (! (info->unresolved_syms_in_objects == RM_IGNORE
03026                              && (ELF_ST_VISIBILITY (hh->eh.other)
03027                                  == STV_DEFAULT)
03028                              && hh->eh.type != STT_PARISC_MILLI))
03029                          continue;
03030                      }
03031                     else
03032                      {
03033                        bfd_set_error (bfd_error_bad_value);
03034                        goto error_ret_free_internal;
03035                      }
03036                   }
03037 
03038                 /* Determine what (if any) linker stub is needed.  */
03039                 stub_type = hppa_type_of_stub (section, irela, hh,
03040                                            destination, info);
03041                 if (stub_type == hppa_stub_none)
03042                   continue;
03043 
03044                 /* Support for grouping stub sections.  */
03045                 id_sec = htab->stub_group[section->id].link_sec;
03046 
03047                 /* Get the name of this stub.  */
03048                 stub_name = hppa_stub_name (id_sec, sym_sec, hh, irela);
03049                 if (!stub_name)
03050                   goto error_ret_free_internal;
03051 
03052                 hsh = hppa_stub_hash_lookup (&htab->bstab,
03053                                                 stub_name,
03054                                                 FALSE, FALSE);
03055                 if (hsh != NULL)
03056                   {
03057                     /* The proper stub has already been created.  */
03058                     free (stub_name);
03059                     continue;
03060                   }
03061 
03062                 hsh = hppa_add_stub (stub_name, section, htab);
03063                 if (hsh == NULL)
03064                   {
03065                     free (stub_name);
03066                     goto error_ret_free_internal;
03067                   }
03068 
03069                 hsh->target_value = sym_value;
03070                 hsh->target_section = sym_sec;
03071                 hsh->stub_type = stub_type;
03072                 if (info->shared)
03073                   {
03074                     if (stub_type == hppa_stub_import)
03075                      hsh->stub_type = hppa_stub_import_shared;
03076                     else if (stub_type == hppa_stub_long_branch)
03077                      hsh->stub_type = hppa_stub_long_branch_shared;
03078                   }
03079                 hsh->hh = hh;
03080                 stub_changed = TRUE;
03081               }
03082 
03083              /* We're done with the internal relocs, free them.  */
03084              if (elf_section_data (section)->relocs == NULL)
03085               free (internal_relocs);
03086            }
03087        }
03088 
03089       if (!stub_changed)
03090        break;
03091 
03092       /* OK, we've added some stubs.  Find out the new size of the
03093         stub sections.  */
03094       for (stub_sec = htab->stub_bfd->sections;
03095           stub_sec != NULL;
03096           stub_sec = stub_sec->next)
03097        stub_sec->size = 0;
03098 
03099       bfd_hash_traverse (&htab->bstab, hppa_size_one_stub, htab);
03100 
03101       /* Ask the linker to do its stuff.  */
03102       (*htab->layout_sections_again) ();
03103       stub_changed = FALSE;
03104     }
03105 
03106   free (htab->all_local_syms);
03107   return TRUE;
03108 
03109  error_ret_free_local:
03110   free (htab->all_local_syms);
03111   return FALSE;
03112 }
03113 
03114 /* For a final link, this function is called after we have sized the
03115    stubs to provide a value for __gp.  */
03116 
03117 bfd_boolean
03118 elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
03119 {
03120   struct bfd_link_hash_entry *h;
03121   asection *sec = NULL;
03122   bfd_vma gp_val = 0;
03123   struct elf32_hppa_link_hash_table *htab;
03124 
03125   htab = hppa_link_hash_table (info);
03126   h = bfd_link_hash_lookup (&htab->etab.root, "$global$", FALSE, FALSE, FALSE);
03127 
03128   if (h != NULL
03129       && (h->type == bfd_link_hash_defined
03130          || h->type == bfd_link_hash_defweak))
03131     {
03132       gp_val = h->u.def.value;
03133       sec = h->u.def.section;
03134     }
03135   else
03136     {
03137       asection *splt = bfd_get_section_by_name (abfd, ".plt");
03138       asection *sgot = bfd_get_section_by_name (abfd, ".got");
03139 
03140       /* Choose to point our LTP at, in this order, one of .plt, .got,
03141         or .data, if these sections exist.  In the case of choosing
03142         .plt try to make the LTP ideal for addressing anywhere in the
03143         .plt or .got with a 14 bit signed offset.  Typically, the end
03144         of the .plt is the start of the .got, so choose .plt + 0x2000
03145         if either the .plt or .got is larger than 0x2000.  If both
03146         the .plt and .got are smaller than 0x2000, choose the end of
03147         the .plt section.  */
03148       sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
03149          ? NULL : splt;
03150       if (sec != NULL)
03151        {
03152          gp_val = sec->size;
03153          if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
03154            {
03155              gp_val = 0x2000;
03156            }
03157        }
03158       else
03159        {
03160          sec = sgot;
03161          if (sec != NULL)
03162            {
03163              if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
03164               {
03165                  /* We know we don't have a .plt.  If .got is large,
03166                    offset our LTP.  */
03167                  if (sec->size > 0x2000)
03168                   gp_val = 0x2000;
03169               }
03170            }
03171          else
03172            {
03173              /* No .plt or .got.  Who cares what the LTP is?  */
03174              sec = bfd_get_section_by_name (abfd, ".data");
03175            }
03176        }
03177 
03178       if (h != NULL)
03179        {
03180          h->type = bfd_link_hash_defined;
03181          h->u.def.value = gp_val;
03182          if (sec != NULL)
03183            h->u.def.section = sec;
03184          else
03185            h->u.def.section = bfd_abs_section_ptr;
03186        }
03187     }
03188 
03189   if (sec != NULL && sec->output_section != NULL)
03190     gp_val += sec->output_section->vma + sec->output_offset;
03191 
03192   elf_gp (abfd) = gp_val;
03193   return TRUE;
03194 }
03195 
03196 /* Build all the stubs associated with the current output file.  The
03197    stubs are kept in a hash table attached to the main linker hash
03198    table.  We also set up the .plt entries for statically linked PIC
03199    functions here.  This function is called via hppaelf_finish in the
03200    linker.  */
03201 
03202 bfd_boolean
03203 elf32_hppa_build_stubs (struct bfd_link_info *info)
03204 {
03205   asection *stub_sec;
03206   struct bfd_hash_table *table;
03207   struct elf32_hppa_link_hash_table *htab;
03208 
03209   htab = hppa_link_hash_table (info);
03210 
03211   for (stub_sec = htab->stub_bfd->sections;
03212        stub_sec != NULL;
03213        stub_sec = stub_sec->next)
03214     {
03215       bfd_size_type size;
03216 
03217       /* Allocate memory to hold the linker stubs.  */
03218       size = stub_sec->size;
03219       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
03220       if (stub_sec->contents == NULL && size != 0)
03221        return FALSE;
03222       stub_sec->size = 0;
03223     }
03224 
03225   /* Build the stubs as directed by the stub hash table.  */
03226   table = &htab->bstab;
03227   bfd_hash_traverse (table, hppa_build_one_stub, info);
03228 
03229   return TRUE;
03230 }
03231 
03232 /* Return the base vma address which should be subtracted from the real
03233    address when resolving a dtpoff relocation.  
03234    This is PT_TLS segment p_vaddr.  */
03235 
03236 static bfd_vma
03237 dtpoff_base (struct bfd_link_info *info)
03238 {
03239   /* If tls_sec is NULL, we should have signalled an error already.  */
03240   if (elf_hash_table (info)->tls_sec == NULL)
03241     return 0;
03242   return elf_hash_table (info)->tls_sec->vma;
03243 }
03244 
03245 /* Return the relocation value for R_PARISC_TLS_TPOFF*..  */
03246 
03247 static bfd_vma
03248 tpoff (struct bfd_link_info *info, bfd_vma address)
03249 {
03250   struct elf_link_hash_table *htab = elf_hash_table (info);
03251 
03252   /* If tls_sec is NULL, we should have signalled an error already.  */
03253   if (htab->tls_sec == NULL)
03254     return 0;
03255   /* hppa TLS ABI is variant I and static TLS block start just after 
03256      tcbhead structure which has 2 pointer fields.  */
03257   return (address - htab->tls_sec->vma 
03258          + align_power ((bfd_vma) 8, htab->tls_sec->alignment_power));
03259 }
03260 
03261 /* Perform a final link.  */
03262 
03263 static bfd_boolean
03264 elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
03265 {
03266   /* Invoke the regular ELF linker to do all the work.  */
03267   if (!bfd_elf_final_link (abfd, info))
03268     return FALSE;
03269 
03270   /* If we're producing a final executable, sort the contents of the
03271      unwind section.  */
03272   return elf_hppa_sort_unwind (abfd);
03273 }
03274 
03275 /* Record the lowest address for the data and text segments.  */
03276 
03277 static void
03278 hppa_record_segment_addr (bfd *abfd ATTRIBUTE_UNUSED,
03279                        asection *section,
03280                        void *data)
03281 {
03282   struct elf32_hppa_link_hash_table *htab;
03283 
03284   htab = (struct elf32_hppa_link_hash_table*) data;
03285 
03286   if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
03287     {
03288       bfd_vma value = section->vma - section->filepos;
03289 
03290       if ((section->flags & SEC_READONLY) != 0)
03291        {
03292          if (value < htab->text_segment_base)
03293            htab->text_segment_base = value;
03294        }
03295       else
03296        {
03297          if (value < htab->data_segment_base)
03298            htab->data_segment_base = value;
03299        }
03300     }
03301 }
03302 
03303 /* Perform a relocation as part of a final link.  */
03304 
03305 static bfd_reloc_status_type
03306 final_link_relocate (asection *input_section,
03307                    bfd_byte *contents,
03308                    const Elf_Internal_Rela *rela,
03309                    bfd_vma value,
03310                    struct elf32_hppa_link_hash_table *htab,
03311                    asection *sym_sec,
03312                    struct elf32_hppa_link_hash_entry *hh,
03313                    struct bfd_link_info *info)
03314 {
03315   int insn;
03316   unsigned int r_type = ELF32_R_TYPE (rela->r_info);
03317   unsigned int orig_r_type = r_type;
03318   reloc_howto_type *howto = elf_hppa_howto_table + r_type;
03319   int r_format = howto->bitsize;
03320   enum hppa_reloc_field_selector_type_alt r_field;
03321   bfd *input_bfd = input_section->owner;
03322   bfd_vma offset = rela->r_offset;
03323   bfd_vma max_branch_offset = 0;
03324   bfd_byte *hit_data = contents + offset;
03325   bfd_signed_vma addend = rela->r_addend;
03326   bfd_vma location;
03327   struct elf32_hppa_stub_hash_entry *hsh = NULL;
03328   int val;  
03329 
03330   if (r_type == R_PARISC_NONE)
03331     return bfd_reloc_ok;
03332 
03333   insn = bfd_get_32 (input_bfd, hit_data);
03334 
03335   /* Find out where we are and where we're going.  */
03336   location = (offset +
03337              input_section->output_offset +
03338              input_section->output_section->vma);
03339 
03340   /* If we are not building a shared library, convert DLTIND relocs to
03341      DPREL relocs.  */
03342   if (!info->shared)
03343     {
03344       switch (r_type)
03345        {
03346          case R_PARISC_DLTIND21L:
03347            r_type = R_PARISC_DPREL21L;
03348            break;
03349 
03350          case R_PARISC_DLTIND14R:
03351            r_type = R_PARISC_DPREL14R;
03352            break;
03353 
03354          case R_PARISC_DLTIND14F:
03355            r_type = R_PARISC_DPREL14F;
03356            break;
03357        }
03358     }
03359 
03360   switch (r_type)
03361     {
03362     case R_PARISC_PCREL12F:
03363     case R_PARISC_PCREL17F:
03364     case R_PARISC_PCREL22F:
03365       /* If this call should go via the plt, find the import stub in
03366         the stub hash.  */
03367       if (sym_sec == NULL
03368          || sym_sec->output_section == NULL
03369          || (hh != NULL
03370              && hh->eh.plt.offset != (bfd_vma) -1
03371              && hh->eh.dynindx != -1
03372              && !hh->plabel
03373              && (info->shared
03374                 || !hh->eh.def_regular
03375                 || hh->eh.root.type == bfd_link_hash_defweak)))
03376        {
03377          hsh = hppa_get_stub_entry (input_section, sym_sec,
03378                                        hh, rela, htab);
03379          if (hsh != NULL)
03380            {
03381              value = (hsh->stub_offset
03382                      + hsh->stub_sec->output_offset
03383                      + hsh->stub_sec->output_section->vma);
03384              addend = 0;
03385            }
03386          else if (sym_sec == NULL && hh != NULL
03387                  && hh->eh.root.type == bfd_link_hash_undefweak)
03388            {
03389              /* It's OK if undefined weak.  Calls to undefined weak
03390                symbols behave as if the "called" function
03391                immediately returns.  We can thus call to a weak
03392                function without first checking whether the function
03393                is defined.  */
03394              value = location;
03395              addend = 8;
03396            }
03397          else
03398            return bfd_reloc_undefined;
03399        }
03400       /* Fall thru.  */
03401 
03402     case R_PARISC_PCREL21L:
03403     case R_PARISC_PCREL17C:
03404     case R_PARISC_PCREL17R:
03405     case R_PARISC_PCREL14R:
03406     case R_PARISC_PCREL14F:
03407     case R_PARISC_PCREL32:
03408       /* Make it a pc relative offset.  */
03409       value -= location;
03410       addend -= 8;
03411       break;
03412 
03413     case R_PARISC_DPREL21L:
03414     case R_PARISC_DPREL14R:
03415     case R_PARISC_DPREL14F:
03416       /* Convert instructions that use the linkage table pointer (r19) to
03417         instructions that use the global data pointer (dp).  This is the
03418         most efficient way of using PIC code in an incomplete executable,
03419         but the user must follow the standard runtime conventions for
03420         accessing data for this to work.  */
03421       if (orig_r_type == R_PARISC_DLTIND21L)
03422        {
03423          /* Convert addil instructions if the original reloc was a
03424             DLTIND21L.  GCC sometimes uses a register other than r19 for
03425             the operation, so we must convert any addil instruction
03426             that uses this relocation.  */
03427          if ((insn & 0xfc000000) == ((int) OP_ADDIL << 26))
03428            insn = ADDIL_DP;
03429          else
03430            /* We must have a ldil instruction.  It's too hard to find
03431               and convert the associated add instruction, so issue an
03432               error.  */
03433            (*_bfd_error_handler)
03434              (_("%B(%A+0x%lx): %s fixup for insn 0x%x is not supported in a non-shared link"),
03435               input_bfd,
03436               input_section,
03437               offset,
03438               howto->name,
03439               insn);
03440        }
03441       else if (orig_r_type == R_PARISC_DLTIND14F)
03442        {
03443          /* This must be a format 1 load/store.  Change the base
03444             register to dp.  */
03445          insn = (insn & 0xfc1ffff) | (27 << 21);
03446        }
03447 
03448     /* For all the DP relative relocations, we need to examine the symbol's
03449        section.  If it has no section or if it's a code section, then
03450        "data pointer relative" makes no sense.  In that case we don't
03451        adjust the "value", and for 21 bit addil instructions, we change the
03452        source addend register from %dp to %r0.  This situation commonly
03453        arises for undefined weak symbols and when a variable's "constness"
03454        is declared differently from the way the variable is defined.  For
03455        instance: "extern int foo" with foo defined as "const int foo".  */
03456       if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
03457        {
03458          if ((insn & ((0x3f << 26) | (0x1f << 21)))
03459              == (((int) OP_ADDIL << 26) | (27 << 21)))
03460            {
03461              insn &= ~ (0x1f << 21);
03462            }
03463          /* Now try to make things easy for the dynamic linker.  */
03464 
03465          break;
03466        }
03467       /* Fall thru.  */
03468 
03469     case R_PARISC_DLTIND21L:
03470     case R_PARISC_DLTIND14R:
03471     case R_PARISC_DLTIND14F:
03472     case R_PARISC_TLS_GD21L:
03473     case R_PARISC_TLS_GD14R:
03474     case R_PARISC_TLS_LDM21L:
03475     case R_PARISC_TLS_LDM14R:
03476     case R_PARISC_TLS_IE21L:
03477     case R_PARISC_TLS_IE14R:
03478       value -= elf_gp (input_section->output_section->owner);
03479       break;
03480 
03481     case R_PARISC_SEGREL32:
03482       if ((sym_sec->flags & SEC_CODE) != 0)
03483        value -= htab->text_segment_base;
03484       else
03485        value -= htab->data_segment_base;
03486       break;
03487 
03488     default:
03489       break;
03490     }
03491 
03492   switch (r_type)
03493     {
03494     case R_PARISC_DIR32:
03495     case R_PARISC_DIR14F:
03496     case R_PARISC_DIR17F:
03497     case R_PARISC_PCREL17C:
03498     case R_PARISC_PCREL14F:
03499     case R_PARISC_PCREL32:
03500     case R_PARISC_DPREL14F:
03501     case R_PARISC_PLABEL32:
03502     case R_PARISC_DLTIND14F:
03503     case R_PARISC_SEGBASE:
03504     case R_PARISC_SEGREL32:
03505     case R_PARISC_TLS_DTPMOD32:
03506     case R_PARISC_TLS_DTPOFF32:
03507     case R_PARISC_TLS_TPREL32:
03508       r_field = e_fsel;
03509       break;
03510 
03511     case R_PARISC_DLTIND21L:
03512     case R_PARISC_PCREL21L:
03513     case R_PARISC_PLABEL21L:
03514       r_field = e_lsel;
03515       break;
03516 
03517     case R_PARISC_DIR21L:
03518     case R_PARISC_DPREL21L:
03519     case R_PARISC_TLS_GD21L:
03520     case R_PARISC_TLS_LDM21L:
03521     case R_PARISC_TLS_LDO21L:
03522     case R_PARISC_TLS_IE21L:
03523     case R_PARISC_TLS_LE21L:
03524       r_field = e_lrsel;
03525       break;
03526 
03527     case R_PARISC_PCREL17R:
03528     case R_PARISC_PCREL14R:
03529     case R_PARISC_PLABEL14R:
03530     case R_PARISC_DLTIND14R:
03531       r_field = e_rsel;
03532       break;
03533 
03534     case R_PARISC_DIR17R:
03535     case R_PARISC_DIR14R:
03536     case R_PARISC_DPREL14R:
03537     case R_PARISC_TLS_GD14R:
03538     case R_PARISC_TLS_LDM14R:
03539     case R_PARISC_TLS_LDO14R:
03540     case R_PARISC_TLS_IE14R:
03541     case R_PARISC_TLS_LE14R:
03542       r_field = e_rrsel;
03543       break;
03544 
03545     case R_PARISC_PCREL12F:
03546     case R_PARISC_PCREL17F:
03547     case R_PARISC_PCREL22F:
03548       r_field = e_fsel;
03549 
03550       if (r_type == (unsigned int) R_PARISC_PCREL17F)
03551        {
03552          max_branch_offset = (1 << (17-1)) << 2;
03553        }
03554       else if (r_type == (unsigned int) R_PARISC_PCREL12F)
03555        {
03556          max_branch_offset = (1 << (12-1)) << 2;
03557        }
03558       else
03559        {
03560          max_branch_offset = (1 << (22-1)) << 2;
03561        }
03562 
03563       /* sym_sec is NULL on undefined weak syms or when shared on
03564         undefined syms.  We've already checked for a stub for the
03565         shared undefined case.  */
03566       if (sym_sec == NULL)
03567        break;
03568 
03569       /* If the branch is out of reach, then redirect the
03570         call to the local stub for this function.  */
03571       if (value + addend + max_branch_offset >= 2*max_branch_offset)
03572        {
03573          hsh = hppa_get_stub_entry (input_section, sym_sec,
03574                                        hh, rela, htab);
03575          if (hsh == NULL)
03576            return bfd_reloc_undefined;
03577 
03578          /* Munge up the value and addend so that we call the stub
03579             rather than the procedure directly.  */
03580          value = (hsh->stub_offset
03581                  + hsh->stub_sec->output_offset
03582                  + hsh->stub_sec->output_section->vma
03583                  - location);
03584          addend = -8;
03585        }
03586       break;
03587 
03588     /* Something we don't know how to handle.  */
03589     default:
03590       return bfd_reloc_notsupported;
03591     }
03592 
03593   /* Make sure we can reach the stub.  */
03594   if (max_branch_offset != 0
03595       && value + addend + max_branch_offset >= 2*max_branch_offset)
03596     {
03597       (*_bfd_error_handler)
03598        (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
03599         input_bfd,
03600         input_section,
03601         offset,
03602         hsh->bh_root.string);
03603       bfd_set_error (bfd_error_bad_value);
03604       return bfd_reloc_notsupported;
03605     }
03606 
03607   val = hppa_field_adjust (value, addend, r_field);
03608 
03609   switch (r_type)
03610     {
03611     case R_PARISC_PCREL12F:
03612     case R_PARISC_PCREL17C:
03613     case R_PARISC_PCREL17F:
03614     case R_PARISC_PCREL17R:
03615     case R_PARISC_PCREL22F:
03616     case R_PARISC_DIR17F:
03617     case R_PARISC_DIR17R:
03618       /* This is a branch.  Divide the offset by four.
03619         Note that we need to decide whether it's a branch or
03620         otherwise by inspecting the reloc.  Inspecting insn won't
03621         work as insn might be from a .word directive.  */
03622       val >>= 2;
03623       break;
03624 
03625     default:
03626       break;
03627     }
03628 
03629   insn = hppa_rebuild_insn (insn, val, r_format);
03630 
03631   /* Update the instruction word.  */
03632   bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
03633   return bfd_reloc_ok;
03634 }
03635 
03636 /* Relocate an HPPA ELF section.  */
03637 
03638 static bfd_boolean
03639 elf32_hppa_relocate_section (bfd *output_bfd,
03640                           struct bfd_link_info *info,
03641                           bfd *input_bfd,
03642                           asection *input_section,
03643                           bfd_byte *contents,
03644                           Elf_Internal_Rela *relocs,
03645                           Elf_Internal_Sym *local_syms,
03646                           asection **local_sections)
03647 {
03648   bfd_vma *local_got_offsets;
03649   struct elf32_hppa_link_hash_table *htab;
03650   Elf_Internal_Shdr *symtab_hdr;
03651   Elf_Internal_Rela *rela;
03652   Elf_Internal_Rela *relend;
03653 
03654   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
03655 
03656   htab = hppa_link_hash_table (info);
03657   local_got_offsets = elf_local_got_offsets (input_bfd);
03658 
03659   rela = relocs;
03660   relend = relocs + input_section->reloc_count;
03661   for (; rela < relend; rela++)
03662     {
03663       unsigned int r_type;
03664       reloc_howto_type *howto;
03665       unsigned int r_symndx;
03666       struct elf32_hppa_link_hash_entry *hh;
03667       Elf_Internal_Sym *sym;
03668       asection *sym_sec;
03669       bfd_vma relocation;
03670       bfd_reloc_status_type rstatus;
03671       const char *sym_name;
03672       bfd_boolean plabel;
03673       bfd_boolean warned_undef;
03674 
03675       r_type = ELF32_R_TYPE (rela->r_info);
03676       if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
03677        {
03678          bfd_set_error (bfd_error_bad_value);
03679          return FALSE;
03680        }
03681       if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
03682          || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
03683        continue;
03684 
03685       r_symndx = ELF32_R_SYM (rela->r_info);
03686       hh = NULL;
03687       sym = NULL;
03688       sym_sec = NULL;
03689       warned_undef = FALSE;
03690       if (r_symndx < symtab_hdr->sh_info)
03691        {
03692          /* This is a local symbol, h defaults to NULL.  */
03693          sym = local_syms + r_symndx;
03694          sym_sec = local_sections[r_symndx];
03695          relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rela);
03696        }
03697       else
03698        {
03699          struct elf_link_hash_entry *eh;
03700          bfd_boolean unresolved_reloc;
03701          struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
03702 
03703          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rela,
03704                                r_symndx, symtab_hdr, sym_hashes,
03705                                eh, sym_sec, relocation,
03706                                unresolved_reloc, warned_undef);
03707 
03708          if (!info->relocatable
03709              && relocation == 0
03710              && eh->root.type != bfd_link_hash_defined
03711              && eh->root.type != bfd_link_hash_defweak
03712              && eh->root.type != bfd_link_hash_undefweak)
03713            {
03714              if (info->unresolved_syms_in_objects == RM_IGNORE
03715                 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
03716                 && eh->type == STT_PARISC_MILLI)
03717               {
03718                 if (! info->callbacks->undefined_symbol
03719                     (info, eh_name (eh), input_bfd,
03720                      input_section, rela->r_offset, FALSE))
03721                   return FALSE;
03722                 warned_undef = TRUE;
03723               }
03724            }
03725          hh = hppa_elf_hash_entry (eh);
03726        }
03727 
03728       if (sym_sec != NULL && elf_discarded_section (sym_sec))
03729        {
03730          /* For relocs against symbols from removed linkonce
03731             sections, or sections discarded by a linker script,
03732             we just want the section contents zeroed.  Avoid any
03733             special processing.  */
03734          _bfd_clear_contents (elf_hppa_howto_table + r_type, input_bfd,
03735                             contents + rela->r_offset);
03736          rela->r_info = 0;
03737          rela->r_addend = 0;
03738          continue;
03739        }
03740 
03741       if (info->relocatable)
03742        continue;
03743 
03744       /* Do any required modifications to the relocation value, and
03745         determine what types of dynamic info we need to output, if
03746         any.  */
03747       plabel = 0;
03748       switch (r_type)
03749        {
03750        case R_PARISC_DLTIND14F:
03751        case R_PARISC_DLTIND14R:
03752        case R_PARISC_DLTIND21L:
03753          {
03754            bfd_vma off;
03755            bfd_boolean do_got = 0;
03756 
03757            /* Relocation is to the entry for this symbol in the
03758               global offset table.  */
03759            if (hh != NULL)
03760              {
03761               bfd_boolean dyn;
03762 
03763               off = hh->eh.got.offset;
03764               dyn = htab->etab.dynamic_sections_created;
03765               if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
03766                                                  &hh->eh))
03767                 {
03768                   /* If we aren't going to call finish_dynamic_symbol,
03769                      then we need to handle initialisation of the .got
03770                      entry and create needed relocs here.  Since the
03771                      offset must always be a multiple of 4, we use the
03772                      least significant bit to record whether we have
03773                      initialised it already.  */
03774                   if ((off & 1) != 0)
03775                     off &= ~1;
03776                   else
03777                     {
03778                      hh->eh.got.offset |= 1;
03779                      do_got = 1;
03780                     }
03781                 }
03782              }
03783            else
03784              {
03785               /* Local symbol case.  */
03786               if (local_got_offsets == NULL)
03787                 abort ();
03788 
03789               off = local_got_offsets[r_symndx];
03790 
03791               /* The offset must always be a multiple of 4.  We use
03792                  the least significant bit to record whether we have
03793                  already generated the necessary reloc.  */
03794               if ((off & 1) != 0)
03795                 off &= ~1;
03796               else
03797                 {
03798                   local_got_offsets[r_symndx] |= 1;
03799                   do_got = 1;
03800                 }
03801              }
03802 
03803            if (do_got)
03804              {
03805               if (info->shared)
03806                 {
03807                   /* Output a dynamic relocation for this GOT entry.
03808                      In this case it is relative to the base of the
03809                      object because the symbol index is zero.  */
03810                   Elf_Internal_Rela outrel;
03811                   bfd_byte *loc;
03812                   asection *sec = htab->srelgot;
03813 
03814                   outrel.r_offset = (off
03815                                    + htab->sgot->output_offset
03816                                    + htab->sgot->output_section->vma);
03817                   outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
03818                   outrel.r_addend = relocation;
03819                   loc = sec->contents;
03820                   loc += sec->reloc_count++ * sizeof (Elf32_External_Rela);
03821                   bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
03822                 }
03823               else
03824                 bfd_put_32 (output_bfd, relocation,
03825                            htab->sgot->contents + off);
03826              }
03827 
03828            if (off >= (bfd_vma) -2)
03829              abort ();
03830 
03831            /* Add the base of the GOT to the relocation value.  */
03832            relocation = (off
03833                        + htab->sgot->output_offset
03834                        + htab->sgot->output_section->vma);
03835          }
03836          break;
03837 
03838        case R_PARISC_SEGREL32:
03839          /* If this is the first SEGREL relocation, then initialize
03840             the segment base values.  */
03841          if (htab->text_segment_base == (bfd_vma) -1)
03842            bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
03843          break;
03844 
03845        case R_PARISC_PLABEL14R:
03846        case R_PARISC_PLABEL21L:
03847        case R_PARISC_PLABEL32:
03848          if (htab->etab.dynamic_sections_created)
03849            {
03850              bfd_vma off;
03851              bfd_boolean do_plt = 0;
03852              /* If we have a global symbol with a PLT slot, then
03853                redirect this relocation to it.  */
03854              if (hh != NULL)
03855               {
03856                 off = hh->eh.plt.offset;
03857                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared,
03858                                                   &hh->eh))
03859                   {
03860                     /* In a non-shared link, adjust_dynamic_symbols
03861                       isn't called for symbols forced local.  We
03862                       need to write out the plt entry here.  */
03863                     if ((off & 1) != 0)
03864                      off &= ~1;
03865                     else
03866                      {
03867                        hh->eh.plt.offset |= 1;
03868                        do_plt = 1;
03869                      }
03870                   }
03871               }
03872              else
03873               {
03874                 bfd_vma *local_plt_offsets;
03875 
03876                 if (local_got_offsets == NULL)
03877                   abort ();
03878 
03879                 local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
03880                 off = local_plt_offsets[r_symndx];
03881 
03882                 /* As for the local .got entry case, we use the last
03883                    bit to record whether we've already initialised
03884                    this local .plt entry.  */
03885                 if ((off & 1) != 0)
03886                   off &= ~1;
03887                 else
03888                   {
03889                     local_plt_offsets[r_symndx] |= 1;
03890                     do_plt = 1;
03891                   }
03892               }
03893 
03894              if (do_plt)
03895               {
03896                 if (info->shared)
03897                   {
03898                     /* Output a dynamic IPLT relocation for this
03899                       PLT entry.  */
03900                     Elf_Internal_Rela outrel;
03901                     bfd_byte *loc;
03902                     asection *s = htab->srelplt;
03903 
03904                     outrel.r_offset = (off
03905                                     + htab->splt->output_offset
03906                                     + htab->splt->output_section->vma);
03907                     outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
03908                     outrel.r_addend = relocation;
03909                     loc = s->contents;
03910                     loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
03911                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
03912                   }
03913                 else
03914                   {
03915                     bfd_put_32 (output_bfd,
03916                               relocation,
03917                               htab->splt->contents + off);
03918                     bfd_put_32 (output_bfd,
03919                               elf_gp (htab->splt->output_section->owner),
03920                               htab->splt->contents + off + 4);
03921                   }
03922               }
03923 
03924              if (off >= (bfd_vma) -2)
03925               abort ();
03926 
03927              /* PLABELs contain function pointers.  Relocation is to
03928                the entry for the function in the .plt.  The magic +2
03929                offset signals to $$dyncall that the function pointer
03930                is in the .plt and thus has a gp pointer too.
03931                Exception:  Undefined PLABELs should have a value of
03932                zero.  */
03933              if (hh == NULL
03934                 || (hh->eh.root.type != bfd_link_hash_undefweak
03935                     && hh->eh.root.type != bfd_link_hash_undefined))
03936               {
03937                 relocation = (off
03938                             + htab->splt->output_offset
03939                             + htab->splt->output_section->vma
03940                             + 2);
03941               }
03942              plabel = 1;
03943            }
03944          /* Fall through and possibly emit a dynamic relocation.  */
03945 
03946        case R_PARISC_DIR17F:
03947        case R_PARISC_DIR17R:
03948        case R_PARISC_DIR14F:
03949        case R_PARISC_DIR14R:
03950        case R_PARISC_DIR21L:
03951        case R_PARISC_DPREL14F:
03952        case R_PARISC_DPREL14R:
03953        case R_PARISC_DPREL21L:
03954        case R_PARISC_DIR32:
03955          if ((input_section->flags & SEC_ALLOC) == 0)
03956            break;
03957 
03958          /* The reloc types handled here and this conditional
03959             expression must match the code in ..check_relocs and
03960             allocate_dynrelocs.  ie. We need exactly the same condition
03961             as in ..check_relocs, with some extra conditions (dynindx
03962             test in this case) to cater for relocs removed by
03963             allocate_dynrelocs.  If you squint, the non-shared test
03964             here does indeed match the one in ..check_relocs, the
03965             difference being that here we test DEF_DYNAMIC as well as
03966             !DEF_REGULAR.  All common syms end up with !DEF_REGULAR,
03967             which is why we can't use just that test here.
03968             Conversely, DEF_DYNAMIC can't be used in check_relocs as
03969             there all files have not been loaded.  */
03970          if ((info->shared
03971               && (hh == NULL
03972                  || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
03973                  || hh->eh.root.type != bfd_link_hash_undefweak)
03974               && (IS_ABSOLUTE_RELOC (r_type)
03975                  || !SYMBOL_CALLS_LOCAL (info, &hh->eh)))
03976              || (!info->shared
03977                 && hh != NULL
03978                 && hh->eh.dynindx != -1
03979                 && !hh->eh.non_got_ref
03980                 && ((ELIMINATE_COPY_RELOCS
03981                      && hh->eh.def_dynamic
03982                      && !hh->eh.def_regular)
03983                     || hh->eh.root.type == bfd_link_hash_undefweak
03984                     || hh->eh.root.type == bfd_link_hash_undefined)))
03985            {
03986              Elf_Internal_Rela outrel;
03987              bfd_boolean skip;
03988              asection *sreloc;
03989              bfd_byte *loc;
03990 
03991              /* When generating a shared object, these relocations
03992                are copied into the output file to be resolved at run
03993                time.  */
03994 
03995              outrel.r_addend = rela->r_addend;
03996              outrel.r_offset =
03997               _bfd_elf_section_offset (output_bfd, info, input_section,
03998                                     rela->r_offset);
03999              skip = (outrel.r_offset == (bfd_vma) -1
04000                     || outrel.r_offset == (bfd_vma) -2);
04001              outrel.r_offset += (input_section->output_offset
04002                               + input_section->output_section->vma);
04003                     
04004              if (skip)
04005               {
04006                 memset (&outrel, 0, sizeof (outrel));
04007               }
04008              else if (hh != NULL
04009                      && hh->eh.dynindx != -1
04010                      && (plabel
04011                         || !IS_ABSOLUTE_RELOC (r_type)
04012                         || !info->shared
04013                         || !info->symbolic
04014                         || !hh->eh.def_regular))
04015               {
04016                 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
04017               }
04018              else /* It's a local symbol, or one marked to become local.  */
04019               {
04020                 int indx = 0;
04021 
04022                 /* Add the absolute offset of the symbol.  */
04023                 outrel.r_addend += relocation;
04024 
04025                 /* Global plabels need to be processed by the
04026                    dynamic linker so that functions have at most one
04027                    fptr.  For this reason, we need to differentiate
04028                    between global and local plabels, which we do by
04029                    providing the function symbol for a global plabel
04030                    reloc, and no symbol for local plabels.  */
04031                 if (! plabel
04032                     && sym_sec != NULL
04033                     && sym_sec->output_section != NULL
04034                     && ! bfd_is_abs_section (sym_sec))
04035                   {
04036                     asection *osec;
04037 
04038                     osec = sym_sec->output_section;
04039                     indx = elf_section_data (osec)->dynindx;
04040                     if (indx == 0)
04041                      {
04042                        osec = htab->etab.text_index_section;
04043                        indx = elf_section_data (osec)->dynindx;
04044                      }
04045                     BFD_ASSERT (indx != 0);
04046 
04047                     /* We are turning this relocation into one
04048                       against a section symbol, so subtract out the
04049                       output section's address but not the offset
04050                       of the input section in the output section.  */
04051                     outrel.r_addend -= osec->vma;
04052                   }
04053 
04054                 outrel.r_info = ELF32_R_INFO (indx, r_type);
04055               }
04056              sreloc = elf_section_data (input_section)->sreloc;
04057              if (sreloc == NULL)
04058               abort ();
04059 
04060              loc = sreloc->contents;
04061              loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
04062              bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
04063            }
04064          break;
04065          
04066        case R_PARISC_TLS_LDM21L:
04067        case R_PARISC_TLS_LDM14R:
04068          {
04069            bfd_vma off;
04070        
04071            off = htab->tls_ldm_got.offset;
04072            if (off & 1)
04073              off &= ~1;
04074            else
04075              {
04076               Elf_Internal_Rela outrel;
04077               bfd_byte *loc;
04078 
04079               outrel.r_offset = (off 
04080                                + htab->sgot->output_section->vma
04081                                + htab->sgot->output_offset);
04082               outrel.r_addend = 0;
04083               outrel.r_info = ELF32_R_INFO (0, R_PARISC_TLS_DTPMOD32);
04084               loc = htab->srelgot->contents; 
04085               loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
04086 
04087               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
04088               htab->tls_ldm_got.offset |= 1;
04089              }
04090 
04091            /* Add the base of the GOT to the relocation value.  */
04092            relocation = (off
04093                        + htab->sgot->output_offset
04094                        + htab->sgot->output_section->vma);
04095 
04096            break;
04097          }
04098 
04099        case R_PARISC_TLS_LDO21L:
04100        case R_PARISC_TLS_LDO14R:
04101          relocation -= dtpoff_base (info);
04102          break;
04103 
04104        case R_PARISC_TLS_GD21L:
04105        case R_PARISC_TLS_GD14R:
04106        case R_PARISC_TLS_IE21L:
04107        case R_PARISC_TLS_IE14R:
04108          {
04109            bfd_vma off;
04110            int indx;
04111            char tls_type;
04112 
04113            indx = 0;
04114            if (hh != NULL)
04115              {
04116                bfd_boolean dyn;
04117                dyn = htab->etab.dynamic_sections_created;
04118 
04119               if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &hh->eh)
04120                   && (!info->shared
04121                      || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
04122                 {
04123                   indx = hh->eh.dynindx;
04124                 }
04125               off = hh->eh.got.offset;
04126               tls_type = hh->tls_type;
04127              }
04128            else
04129              {
04130               off = local_got_offsets[r_symndx];
04131               tls_type = hppa_elf_local_got_tls_type (input_bfd)[r_symndx];
04132              }
04133 
04134            if (tls_type == GOT_UNKNOWN)
04135              abort ();
04136 
04137            if ((off & 1) != 0)
04138              off &= ~1;
04139            else
04140              {
04141               bfd_boolean need_relocs = FALSE;
04142               Elf_Internal_Rela outrel;
04143               bfd_byte *loc = NULL;
04144               int cur_off = off;
04145 
04146                /* The GOT entries have not been initialized yet.  Do it
04147                   now, and emit any relocations.  If both an IE GOT and a
04148                   GD GOT are necessary, we emit the GD first.  */
04149 
04150               if ((info->shared || indx != 0)
04151                   && (hh == NULL
04152                      || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
04153                      || hh->eh.root.type != bfd_link_hash_undefweak))
04154                 {
04155                   need_relocs = TRUE;
04156                   loc = htab->srelgot->contents; 
04157                   /* FIXME (CAO): Should this be reloc_count++ ? */
04158                   loc += htab->srelgot->reloc_count * sizeof (Elf32_External_Rela);
04159                 }
04160 
04161               if (tls_type & GOT_TLS_GD)
04162                 {
04163                   if (need_relocs)
04164                     {
04165                      outrel.r_offset = (cur_off
04166                                       + htab->sgot->output_section->vma
04167                                       + htab->sgot->output_offset);
04168                      outrel.r_info = ELF32_R_INFO (indx,R_PARISC_TLS_DTPMOD32);
04169                      outrel.r_addend = 0;
04170                      bfd_put_32 (output_bfd, 0, htab->sgot->contents + cur_off);
04171                      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
04172                      htab->srelgot->reloc_count++;
04173                      loc += sizeof (Elf32_External_Rela);
04174 
04175                      if (indx == 0)
04176                        bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
04177                                   htab->sgot->contents + cur_off + 4);
04178                      else
04179                        {
04180                          bfd_put_32 (output_bfd, 0,
04181                                    htab->sgot->contents + cur_off + 4);
04182                          outrel.r_info = ELF32_R_INFO (indx, R_PARISC_TLS_DTPOFF32);
04183                          outrel.r_offset += 4;
04184                          bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
04185                          htab->srelgot->reloc_count++;
04186                          loc += sizeof (Elf32_External_Rela);
04187                        }
04188                     }
04189                   else
04190                     {
04191                       /* If we are not emitting relocations for a
04192                          general dynamic reference, then we must be in a
04193                          static link or an executable link with the
04194                          symbol binding locally.  Mark it as belonging
04195                          to module 1, the executable.  */
04196                       bfd_put_32 (output_bfd, 1,
04197                                 htab->sgot->contents + cur_off);
04198                       bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
04199                                 htab->sgot->contents + cur_off + 4);
04200                     }
04201 
04202 
04203                   cur_off += 8;
04204                 }
04205 
04206               if (tls_type & GOT_TLS_IE)
04207                 {
04208                   if (need_relocs)
04209                     {
04210                      outrel.r_offset = (cur_off
04211                                       + htab->sgot->output_section->vma
04212                                       + htab->sgot->output_offset);
04213                      outrel.r_info = ELF32_R_INFO (indx, R_PARISC_TLS_TPREL32);
04214 
04215                      if (indx == 0)
04216                        outrel.r_addend = relocation - dtpoff_base (info);
04217                      else
04218                        outrel.r_addend = 0;
04219 
04220                      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
04221                      htab->srelgot->reloc_count++;
04222                      loc += sizeof (Elf32_External_Rela);
04223                     }
04224                   else
04225                     bfd_put_32 (output_bfd, tpoff (info, relocation),
04226                               htab->sgot->contents + cur_off);
04227 
04228                   cur_off += 4;
04229                 }
04230 
04231               if (hh != NULL)
04232                 hh->eh.got.offset |= 1;
04233               else
04234                 local_got_offsets[r_symndx] |= 1;
04235              }
04236 
04237            if ((tls_type & GOT_TLS_GD)
04238               && r_type != R_PARISC_TLS_GD21L
04239               && r_type != R_PARISC_TLS_GD14R)
04240              off += 2 * GOT_ENTRY_SIZE;
04241 
04242            /* Add the base of the GOT to the relocation value.  */
04243            relocation = (off
04244                        + htab->sgot->output_offset
04245                        + htab->sgot->output_section->vma);
04246 
04247            break;
04248          }
04249 
04250        case R_PARISC_TLS_LE21L:
04251        case R_PARISC_TLS_LE14R:
04252          {
04253            relocation = tpoff (info, relocation);
04254            break;
04255          }
04256          break;
04257 
04258        default:
04259          break;
04260        }
04261 
04262       rstatus = final_link_relocate (input_section, contents, rela, relocation,
04263                             htab, sym_sec, hh, info);
04264 
04265       if (rstatus == bfd_reloc_ok)
04266        continue;
04267 
04268       if (hh != NULL)
04269        sym_name = hh_name (hh);
04270       else
04271        {
04272          sym_name = bfd_elf_string_from_elf_section (input_bfd,
04273                                                 symtab_hdr->sh_link,
04274                                                 sym->st_name);
04275          if (sym_name == NULL)
04276            return FALSE;
04277          if (*sym_name == '\0')
04278            sym_name = bfd_section_name (input_bfd, sym_sec);
04279        }
04280 
04281       howto = elf_hppa_howto_table + r_type;
04282 
04283       if (rstatus == bfd_reloc_undefined || rstatus == bfd_reloc_notsupported)
04284        {
04285          if (rstatus == bfd_reloc_notsupported || !warned_undef)
04286            {
04287              (*_bfd_error_handler)
04288               (_("%B(%A+0x%lx): cannot handle %s for %s"),
04289                input_bfd,
04290                input_section,
04291                (long) rela->r_offset,
04292                howto->name,
04293                sym_name);
04294              bfd_set_error (bfd_error_bad_value);
04295              return FALSE;
04296            }
04297        }
04298       else
04299        {
04300          if (!((*info->callbacks->reloc_overflow)
04301               (info, (hh ? &hh->eh.root : NULL), sym_name, howto->name,
04302                (bfd_vma) 0, input_bfd, input_section, rela->r_offset)))
04303            return FALSE;
04304        }
04305     }
04306 
04307   return TRUE;
04308 }
04309 
04310 /* Finish up dynamic symbol handling.  We set the contents of various
04311    dynamic sections here.  */
04312 
04313 static bfd_boolean
04314 elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
04315                               struct bfd_link_info *info,
04316                               struct elf_link_hash_entry *eh,
04317                               Elf_Internal_Sym *sym)
04318 {
04319   struct elf32_hppa_link_hash_table *htab;
04320   Elf_Internal_Rela rela;
04321   bfd_byte *loc;
04322 
04323   htab = hppa_link_hash_table (info);
04324 
04325   if (eh->plt.offset != (bfd_vma) -1)
04326     {
04327       bfd_vma value;
04328 
04329       if (eh->plt.offset & 1)
04330        abort ();
04331 
04332       /* This symbol has an entry in the procedure linkage table.  Set
04333         it up.
04334 
04335         The format of a plt entry is
04336         <funcaddr>
04337         <__gp>
04338       */
04339       value = 0;
04340       if (eh->root.type == bfd_link_hash_defined
04341          || eh->root.type == bfd_link_hash_defweak)
04342        {
04343          value = eh->root.u.def.value;
04344          if (eh->root.u.def.section->output_section != NULL)
04345            value += (eh->root.u.def.section->output_offset
04346                     + eh->root.u.def.section->output_section->vma);
04347        }
04348 
04349       /* Create a dynamic IPLT relocation for this entry.  */
04350       rela.r_offset = (eh->plt.offset
04351                     + htab->splt->output_offset
04352                     + htab->splt->output_section->vma);
04353       if (eh->dynindx != -1)
04354        {
04355          rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_IPLT);
04356          rela.r_addend = 0;
04357        }
04358       else
04359        {
04360          /* This symbol has been marked to become local, and is
04361             used by a plabel so must be kept in the .plt.  */
04362          rela.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
04363          rela.r_addend = value;
04364        }
04365 
04366       loc = htab->srelplt->contents;
04367       loc += htab->srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
04368       bfd_elf32_swap_reloca_out (htab->splt->output_section->owner, &rela, loc);
04369 
04370       if (!eh->def_regular)
04371        {
04372          /* Mark the symbol as undefined, rather than as defined in
04373             the .plt section.  Leave the value alone.  */
04374          sym->st_shndx = SHN_UNDEF;
04375        }
04376     }
04377 
04378   if (eh->got.offset != (bfd_vma) -1
04379       && (hppa_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0
04380       && (hppa_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0)
04381     {
04382       /* This symbol has an entry in the global offset table.  Set it
04383         up.  */
04384 
04385       rela.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
04386                     + htab->sgot->output_offset
04387                     + htab->sgot->output_section->vma);
04388 
04389       /* If this is a -Bsymbolic link and the symbol is defined
04390         locally or was forced to be local because of a version file,
04391         we just want to emit a RELATIVE reloc.  The entry in the
04392         global offset table will already have been initialized in the
04393         relocate_section function.  */
04394       if (info->shared
04395          && (info->symbolic || eh->dynindx == -1)
04396          && eh->def_regular)
04397        {
04398          rela.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
04399          rela.r_addend = (eh->root.u.def.value
04400                        + eh->root.u.def.section->output_offset
04401                        + eh->root.u.def.section->output_section->vma);
04402        }
04403       else
04404        {
04405          if ((eh->got.offset & 1) != 0)
04406            abort ();
04407 
04408          bfd_put_32 (output_bfd, 0, htab->sgot->contents + (eh->got.offset & ~1));
04409          rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_DIR32);
04410          rela.r_addend = 0;
04411        }
04412 
04413       loc = htab->srelgot->contents;
04414       loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
04415       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
04416     }
04417 
04418   if (eh->needs_copy)
04419     {
04420       asection *sec;
04421 
04422       /* This symbol needs a copy reloc.  Set it up.  */
04423 
04424       if (! (eh->dynindx != -1
04425             && (eh->root.type == bfd_link_hash_defined
04426                || eh->root.type == bfd_link_hash_defweak)))
04427        abort ();
04428 
04429       sec = htab->srelbss;
04430 
04431       rela.r_offset = (eh->root.u.def.value
04432                     + eh->root.u.def.section->output_offset
04433                     + eh->root.u.def.section->output_section->vma);
04434       rela.r_addend = 0;
04435       rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_COPY);
04436       loc = sec->contents + sec->reloc_count++ * sizeof (Elf32_External_Rela);
04437       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
04438     }
04439 
04440   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
04441   if (eh_name (eh)[0] == '_'
04442       && (strcmp (eh_name (eh), "_DYNAMIC") == 0
04443          || eh == htab->etab.hgot))
04444     {
04445       sym->st_shndx = SHN_ABS;
04446     }
04447 
04448   return TRUE;
04449 }
04450 
04451 /* Used to decide how to sort relocs in an optimal manner for the
04452    dynamic linker, before writing them out.  */
04453 
04454 static enum elf_reloc_type_class
04455 elf32_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
04456 {
04457   /* Handle TLS relocs first; we don't want them to be marked
04458      relative by the "if (ELF32_R_SYM (rela->r_info) == 0)"
04459      check below.  */
04460   switch ((int) ELF32_R_TYPE (rela->r_info))
04461     {
04462       case R_PARISC_TLS_DTPMOD32:
04463       case R_PARISC_TLS_DTPOFF32:
04464       case R_PARISC_TLS_TPREL32:
04465         return reloc_class_normal;
04466     }
04467 
04468   if (ELF32_R_SYM (rela->r_info) == 0)
04469     return reloc_class_relative;
04470 
04471   switch ((int) ELF32_R_TYPE (rela->r_info))
04472     {
04473     case R_PARISC_IPLT:
04474       return reloc_class_plt;
04475     case R_PARISC_COPY:
04476       return reloc_class_copy;
04477     default:
04478       return reloc_class_normal;
04479     }
04480 }
04481 
04482 /* Finish up the dynamic sections.  */
04483 
04484 static bfd_boolean
04485 elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
04486                                 struct bfd_link_info *info)
04487 {
04488   bfd *dynobj;
04489   struct elf32_hppa_link_hash_table *htab;
04490   asection *sdyn;
04491 
04492   htab = hppa_link_hash_table (info);
04493   dynobj = htab->etab.dynobj;
04494 
04495   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
04496 
04497   if (htab->etab.dynamic_sections_created)
04498     {
04499       Elf32_External_Dyn *dyncon, *dynconend;
04500 
04501       if (sdyn == NULL)
04502        abort ();
04503 
04504       dyncon = (Elf32_External_Dyn *) sdyn->contents;
04505       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
04506       for (; dyncon < dynconend; dyncon++)
04507        {
04508          Elf_Internal_Dyn dyn;
04509          asection *s;
04510 
04511          bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
04512 
04513          switch (dyn.d_tag)
04514            {
04515            default:
04516              continue;
04517 
04518            case DT_PLTGOT:
04519              /* Use PLTGOT to set the GOT register.  */
04520              dyn.d_un.d_ptr = elf_gp (output_bfd);
04521              break;
04522 
04523            case DT_JMPREL:
04524              s = htab->srelplt;
04525              dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
04526              break;
04527 
04528            case DT_PLTRELSZ:
04529              s = htab->srelplt;
04530              dyn.d_un.d_val = s->size;
04531              break;
04532 
04533            case DT_RELASZ:
04534              /* Don't count procedure linkage table relocs in the
04535                overall reloc count.  */
04536              s = htab->srelplt;
04537              if (s == NULL)
04538               continue;
04539              dyn.d_un.d_val -= s->size;
04540              break;
04541 
04542            case DT_RELA:
04543              /* We may not be using the standard ELF linker script.
04544                If .rela.plt is the first .rela section, we adjust
04545                DT_RELA to not include it.  */
04546              s = htab->srelplt;
04547              if (s == NULL)
04548               continue;
04549              if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
04550               continue;
04551              dyn.d_un.d_ptr += s->size;
04552              break;
04553            }
04554 
04555          bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
04556        }
04557     }
04558 
04559   if (htab->sgot != NULL && htab->sgot->size != 0)
04560     {
04561       /* Fill in the first entry in the global offset table.
04562         We use it to point to our dynamic section, if we have one.  */
04563       bfd_put_32 (output_bfd,
04564                 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
04565                 htab->sgot->contents);
04566 
04567       /* The second entry is reserved for use by the dynamic linker.  */
04568       memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
04569 
04570       /* Set .got entry size.  */
04571       elf_section_data (htab->sgot->output_section)
04572        ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
04573     }
04574 
04575   if (htab->splt != NULL && htab->splt->size != 0)
04576     {
04577       /* Set plt entry size.  */
04578       elf_section_data (htab->splt->output_section)
04579        ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
04580 
04581       if (htab->need_plt_stub)
04582        {
04583          /* Set up the .plt stub.  */
04584          memcpy (htab->splt->contents
04585                 + htab->splt->size - sizeof (plt_stub),
04586                 plt_stub, sizeof (plt_stub));
04587 
04588          if ((htab->splt->output_offset
04589               + htab->splt->output_section->vma
04590               + htab->splt->size)
04591              != (htab->sgot->output_offset
04592                 + htab->sgot->output_section->vma))
04593            {
04594              (*_bfd_error_handler)
04595               (_(".got section not immediately after .plt section"));
04596              return FALSE;
04597            }
04598        }
04599     }
04600 
04601   return TRUE;
04602 }
04603 
04604 /* Called when writing out an object file to decide the type of a
04605    symbol.  */
04606 static int
04607 elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
04608 {
04609   if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
04610     return STT_PARISC_MILLI;
04611   else
04612     return type;
04613 }
04614 
04615 /* Misc BFD support code.  */
04616 #define bfd_elf32_bfd_is_local_label_name    elf_hppa_is_local_label_name
04617 #define bfd_elf32_bfd_reloc_type_lookup        elf_hppa_reloc_type_lookup
04618 #define bfd_elf32_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
04619 #define elf_info_to_howto               elf_hppa_info_to_howto
04620 #define elf_info_to_howto_rel                  elf_hppa_info_to_howto_rel
04621 
04622 /* Stuff for the BFD linker.  */
04623 #define bfd_elf32_bfd_final_link        elf32_hppa_final_link
04624 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
04625 #define bfd_elf32_bfd_link_hash_table_free   elf32_hppa_link_hash_table_free
04626 #define elf_backend_adjust_dynamic_symbol    elf32_hppa_adjust_dynamic_symbol
04627 #define elf_backend_copy_indirect_symbol     elf32_hppa_copy_indirect_symbol
04628 #define elf_backend_check_relocs        elf32_hppa_check_relocs
04629 #define elf_backend_create_dynamic_sections  elf32_hppa_create_dynamic_sections
04630 #define elf_backend_fake_sections       elf_hppa_fake_sections
04631 #define elf_backend_relocate_section           elf32_hppa_relocate_section
04632 #define elf_backend_hide_symbol                elf32_hppa_hide_symbol
04633 #define elf_backend_finish_dynamic_symbol    elf32_hppa_finish_dynamic_symbol
04634 #define elf_backend_finish_dynamic_sections  elf32_hppa_finish_dynamic_sections
04635 #define elf_backend_size_dynamic_sections    elf32_hppa_size_dynamic_sections
04636 #define elf_backend_init_index_section         _bfd_elf_init_1_index_section
04637 #define elf_backend_gc_mark_hook        elf32_hppa_gc_mark_hook
04638 #define elf_backend_gc_sweep_hook       elf32_hppa_gc_sweep_hook
04639 #define elf_backend_grok_prstatus       elf32_hppa_grok_prstatus
04640 #define elf_backend_grok_psinfo                elf32_hppa_grok_psinfo
04641 #define elf_backend_object_p                   elf32_hppa_object_p
04642 #define elf_backend_final_write_processing   elf_hppa_final_write_processing
04643 #define elf_backend_post_process_headers     _bfd_elf_set_osabi
04644 #define elf_backend_get_symbol_type            elf32_hppa_elf_get_symbol_type
04645 #define elf_backend_reloc_type_class           elf32_hppa_reloc_type_class
04646 #define elf_backend_action_discarded           elf_hppa_action_discarded
04647 
04648 #define elf_backend_can_gc_sections            1
04649 #define elf_backend_can_refcount        1
04650 #define elf_backend_plt_alignment       2
04651 #define elf_backend_want_got_plt        0
04652 #define elf_backend_plt_readonly        0
04653 #define elf_backend_want_plt_sym        0
04654 #define elf_backend_got_header_size            8
04655 #define elf_backend_rela_normal                1
04656 
04657 #define TARGET_BIG_SYM             bfd_elf32_hppa_vec
04658 #define TARGET_BIG_NAME            "elf32-hppa"
04659 #define ELF_ARCH            bfd_arch_hppa
04660 #define ELF_MACHINE_CODE    EM_PARISC
04661 #define ELF_MAXPAGESIZE            0x1000
04662 #define ELF_OSABI           ELFOSABI_HPUX
04663 #define elf32_bed           elf32_hppa_hpux_bed
04664 
04665 #include "elf32-target.h"
04666 
04667 #undef TARGET_BIG_SYM
04668 #define TARGET_BIG_SYM             bfd_elf32_hppa_linux_vec
04669 #undef TARGET_BIG_NAME
04670 #define TARGET_BIG_NAME            "elf32-hppa-linux"
04671 #undef ELF_OSABI
04672 #define ELF_OSABI           ELFOSABI_LINUX
04673 #undef elf32_bed
04674 #define elf32_bed           elf32_hppa_linux_bed
04675 
04676 #include "elf32-target.h"
04677 
04678 #undef TARGET_BIG_SYM
04679 #define TARGET_BIG_SYM             bfd_elf32_hppa_nbsd_vec
04680 #undef TARGET_BIG_NAME
04681 #define TARGET_BIG_NAME            "elf32-hppa-netbsd"
04682 #undef ELF_OSABI
04683 #define ELF_OSABI           ELFOSABI_NETBSD
04684 #undef elf32_bed
04685 #define elf32_bed           elf32_hppa_netbsd_bed
04686 
04687 #include "elf32-target.h"