bfd/
[external/binutils.git] / bfd / elf32-hppa.c
1 /* BFD back-end for HP PA-RISC ELF files.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4
5    Original code by
6         Center for Software Science
7         Department of Computer Science
8         University of Utah
9    Largely rewritten by Alan Modra <alan@linuxcare.com.au>
10    Naming cleanup by Carlos O'Donell <carlos@systemhalted.org>
11    TLS support written by Randolph Chung <tausq@debian.org>
12  
13    This file is part of BFD, the Binary File Descriptor library.
14
15    This program is free software; you can redistribute it and/or modify
16    it under the terms of the GNU General Public License as published by
17    the Free Software Foundation; either version 3 of the License, or
18    (at your option) any later version.
19
20    This program is distributed in the hope that it will be useful,
21    but WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23    GNU General Public License for more details.
24
25    You should have received a copy of the GNU General Public License
26    along with this program; if not, write to the Free Software
27    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
28    MA 02110-1301, USA.  */
29
30 #include "sysdep.h"
31 #include "bfd.h"
32 #include "libbfd.h"
33 #include "elf-bfd.h"
34 #include "elf/hppa.h"
35 #include "libhppa.h"
36 #include "elf32-hppa.h"
37 #define ARCH_SIZE               32
38 #include "elf32-hppa.h"
39 #include "elf-hppa.h"
40
41 /* In order to gain some understanding of code in this file without
42    knowing all the intricate details of the linker, note the
43    following:
44
45    Functions named elf32_hppa_* are called by external routines, other
46    functions are only called locally.  elf32_hppa_* functions appear
47    in this file more or less in the order in which they are called
48    from external routines.  eg. elf32_hppa_check_relocs is called
49    early in the link process, elf32_hppa_finish_dynamic_sections is
50    one of the last functions.  */
51
52 /* We use two hash tables to hold information for linking PA ELF objects.
53
54    The first is the elf32_hppa_link_hash_table which is derived
55    from the standard ELF linker hash table.  We use this as a place to
56    attach other hash tables and static information.
57
58    The second is the stub hash table which is derived from the
59    base BFD hash table.  The stub hash table holds the information
60    necessary to build the linker stubs during a link.
61
62    There are a number of different stubs generated by the linker.
63
64    Long branch stub:
65    :            ldil LR'X,%r1
66    :            be,n RR'X(%sr4,%r1)
67
68    PIC long branch stub:
69    :            b,l .+8,%r1
70    :            addil LR'X - ($PIC_pcrel$0 - 4),%r1
71    :            be,n RR'X - ($PIC_pcrel$0 - 8)(%sr4,%r1)
72
73    Import stub to call shared library routine from normal object file
74    (single sub-space version)
75    :            addil LR'lt_ptr+ltoff,%dp       ; get procedure entry point
76    :            ldw RR'lt_ptr+ltoff(%r1),%r21
77    :            bv %r0(%r21)
78    :            ldw RR'lt_ptr+ltoff+4(%r1),%r19 ; get new dlt value.
79
80    Import stub to call shared library routine from shared library
81    (single sub-space version)
82    :            addil LR'ltoff,%r19             ; get procedure entry point
83    :            ldw RR'ltoff(%r1),%r21
84    :            bv %r0(%r21)
85    :            ldw RR'ltoff+4(%r1),%r19        ; get new dlt value.
86
87    Import stub to call shared library routine from normal object file
88    (multiple sub-space support)
89    :            addil LR'lt_ptr+ltoff,%dp       ; get procedure entry point
90    :            ldw RR'lt_ptr+ltoff(%r1),%r21
91    :            ldw RR'lt_ptr+ltoff+4(%r1),%r19 ; get new dlt value.
92    :            ldsid (%r21),%r1
93    :            mtsp %r1,%sr0
94    :            be 0(%sr0,%r21)                 ; branch to target
95    :            stw %rp,-24(%sp)                ; save rp
96
97    Import stub to call shared library routine from shared library
98    (multiple sub-space support)
99    :            addil LR'ltoff,%r19             ; get procedure entry point
100    :            ldw RR'ltoff(%r1),%r21
101    :            ldw RR'ltoff+4(%r1),%r19        ; get new dlt value.
102    :            ldsid (%r21),%r1
103    :            mtsp %r1,%sr0
104    :            be 0(%sr0,%r21)                 ; branch to target
105    :            stw %rp,-24(%sp)                ; save rp
106
107    Export stub to return from shared lib routine (multiple sub-space support)
108    One of these is created for each exported procedure in a shared
109    library (and stored in the shared lib).  Shared lib routines are
110    called via the first instruction in the export stub so that we can
111    do an inter-space return.  Not required for single sub-space.
112    :            bl,n X,%rp                      ; trap the return
113    :            nop
114    :            ldw -24(%sp),%rp                ; restore the original rp
115    :            ldsid (%rp),%r1
116    :            mtsp %r1,%sr0
117    :            be,n 0(%sr0,%rp)                ; inter-space return.  */
118
119
120 /* Variable names follow a coding style.
121    Please follow this (Apps Hungarian) style:
122
123    Structure/Variable                   Prefix
124    elf_link_hash_table                  "etab"
125    elf_link_hash_entry                  "eh"
126    
127    elf32_hppa_link_hash_table           "htab"
128    elf32_hppa_link_hash_entry           "hh"
129
130    bfd_hash_table                       "btab"
131    bfd_hash_entry                       "bh"
132    
133    bfd_hash_table containing stubs      "bstab"
134    elf32_hppa_stub_hash_entry           "hsh"
135
136    elf32_hppa_dyn_reloc_entry           "hdh"
137    
138    Always remember to use GNU Coding Style. */
139                                           
140 #define PLT_ENTRY_SIZE 8
141 #define GOT_ENTRY_SIZE 4
142 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
143
144 static const bfd_byte plt_stub[] =
145 {
146   0x0e, 0x80, 0x10, 0x96,  /* 1: ldw    0(%r20),%r22            */
147   0xea, 0xc0, 0xc0, 0x00,  /*    bv     %r0(%r22)               */
148   0x0e, 0x88, 0x10, 0x95,  /*    ldw    4(%r20),%r21            */
149 #define PLT_STUB_ENTRY (3*4)
150   0xea, 0x9f, 0x1f, 0xdd,  /*    b,l    1b,%r20                 */
151   0xd6, 0x80, 0x1c, 0x1e,  /*    depi   0,31,2,%r20             */
152   0x00, 0xc0, 0xff, 0xee,  /* 9: .word  fixup_func              */
153   0xde, 0xad, 0xbe, 0xef   /*    .word  fixup_ltp               */
154 };
155
156 /* Section name for stubs is the associated section name plus this
157    string.  */
158 #define STUB_SUFFIX ".stub"
159
160 /* We don't need to copy certain PC- or GP-relative dynamic relocs
161    into a shared object's dynamic section.  All the relocs of the
162    limited class we are interested in, are absolute.  */
163 #ifndef RELATIVE_DYNRELOCS
164 #define RELATIVE_DYNRELOCS 0
165 #define IS_ABSOLUTE_RELOC(r_type) 1
166 #endif
167
168 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
169    copying dynamic variables from a shared lib into an app's dynbss
170    section, and instead use a dynamic relocation to point into the
171    shared lib.  */
172 #define ELIMINATE_COPY_RELOCS 1
173
174 enum elf32_hppa_stub_type
175 {
176   hppa_stub_long_branch,
177   hppa_stub_long_branch_shared,
178   hppa_stub_import,
179   hppa_stub_import_shared,
180   hppa_stub_export,
181   hppa_stub_none
182 };
183
184 struct elf32_hppa_stub_hash_entry
185 {
186   /* Base hash table entry structure.  */
187   struct bfd_hash_entry bh_root;
188
189   /* The stub section.  */
190   asection *stub_sec;
191
192   /* Offset within stub_sec of the beginning of this stub.  */
193   bfd_vma stub_offset;
194
195   /* Given the symbol's value and its section we can determine its final
196      value when building the stubs (so the stub knows where to jump.  */
197   bfd_vma target_value;
198   asection *target_section;
199
200   enum elf32_hppa_stub_type stub_type;
201
202   /* The symbol table entry, if any, that this was derived from.  */
203   struct elf32_hppa_link_hash_entry *hh;
204
205   /* Where this stub is being called from, or, in the case of combined
206      stub sections, the first input section in the group.  */
207   asection *id_sec;
208 };
209
210 struct elf32_hppa_link_hash_entry
211 {
212   struct elf_link_hash_entry eh;
213
214   /* A pointer to the most recently used stub hash entry against this
215      symbol.  */
216   struct elf32_hppa_stub_hash_entry *hsh_cache;
217
218   /* Used to count relocations for delayed sizing of relocation
219      sections.  */
220   struct elf32_hppa_dyn_reloc_entry
221   {
222     /* Next relocation in the chain.  */
223     struct elf32_hppa_dyn_reloc_entry *hdh_next;
224
225     /* The input section of the reloc.  */
226     asection *sec;
227
228     /* Number of relocs copied in this section.  */
229     bfd_size_type count;
230
231 #if RELATIVE_DYNRELOCS
232   /* Number of relative relocs copied for the input section.  */
233     bfd_size_type relative_count;
234 #endif
235   } *dyn_relocs;
236
237   enum
238   {
239     GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_GD = 2, GOT_TLS_LDM = 4, GOT_TLS_IE = 8
240   } tls_type;
241
242   /* Set if this symbol is used by a plabel reloc.  */
243   unsigned int plabel:1;
244 };
245
246 struct elf32_hppa_link_hash_table
247 {
248   /* The main hash table.  */
249   struct elf_link_hash_table etab;
250
251   /* The stub hash table.  */
252   struct bfd_hash_table bstab;
253
254   /* Linker stub bfd.  */
255   bfd *stub_bfd;
256
257   /* Linker call-backs.  */
258   asection * (*add_stub_section) (const char *, asection *);
259   void (*layout_sections_again) (void);
260
261   /* Array to keep track of which stub sections have been created, and
262      information on stub grouping.  */
263   struct map_stub
264   {
265     /* This is the section to which stubs in the group will be
266        attached.  */
267     asection *link_sec;
268     /* The stub section.  */
269     asection *stub_sec;
270   } *stub_group;
271
272   /* Assorted information used by elf32_hppa_size_stubs.  */
273   unsigned int bfd_count;
274   int top_index;
275   asection **input_list;
276   Elf_Internal_Sym **all_local_syms;
277
278   /* Short-cuts to get to dynamic linker sections.  */
279   asection *sgot;
280   asection *srelgot;
281   asection *splt;
282   asection *srelplt;
283   asection *sdynbss;
284   asection *srelbss;
285
286   /* Used during a final link to store the base of the text and data
287      segments so that we can perform SEGREL relocations.  */
288   bfd_vma text_segment_base;
289   bfd_vma data_segment_base;
290
291   /* Whether we support multiple sub-spaces for shared libs.  */
292   unsigned int multi_subspace:1;
293
294   /* Flags set when various size branches are detected.  Used to
295      select suitable defaults for the stub group size.  */
296   unsigned int has_12bit_branch:1;
297   unsigned int has_17bit_branch:1;
298   unsigned int has_22bit_branch:1;
299
300   /* Set if we need a .plt stub to support lazy dynamic linking.  */
301   unsigned int need_plt_stub:1;
302
303   /* Small local sym to section mapping cache.  */
304   struct sym_sec_cache sym_sec;
305
306   /* Data for LDM relocations.  */
307   union
308   {
309     bfd_signed_vma refcount;
310     bfd_vma offset;
311   } tls_ldm_got;
312 };
313
314 /* Various hash macros and functions.  */
315 #define hppa_link_hash_table(p) \
316   ((struct elf32_hppa_link_hash_table *) ((p)->hash))
317
318 #define hppa_elf_hash_entry(ent) \
319   ((struct elf32_hppa_link_hash_entry *)(ent))
320
321 #define hppa_stub_hash_entry(ent) \
322   ((struct elf32_hppa_stub_hash_entry *)(ent))
323
324 #define hppa_stub_hash_lookup(table, string, create, copy) \
325   ((struct elf32_hppa_stub_hash_entry *) \
326    bfd_hash_lookup ((table), (string), (create), (copy)))
327
328 #define hppa_elf_local_got_tls_type(abfd) \
329   ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info * 2)))
330
331 #define hh_name(hh) \
332   (hh ? hh->eh.root.root.string : "<undef>")
333
334 #define eh_name(eh) \
335   (eh ? eh->root.root.string : "<undef>")
336
337 /* Assorted hash table functions.  */
338
339 /* Initialize an entry in the stub hash table.  */
340
341 static struct bfd_hash_entry *
342 stub_hash_newfunc (struct bfd_hash_entry *entry,
343                    struct bfd_hash_table *table,
344                    const char *string)
345 {
346   /* Allocate the structure if it has not already been allocated by a
347      subclass.  */
348   if (entry == NULL)
349     {
350       entry = bfd_hash_allocate (table,
351                                  sizeof (struct elf32_hppa_stub_hash_entry));
352       if (entry == NULL)
353         return entry;
354     }
355
356   /* Call the allocation method of the superclass.  */
357   entry = bfd_hash_newfunc (entry, table, string);
358   if (entry != NULL)
359     {
360       struct elf32_hppa_stub_hash_entry *hsh;
361
362       /* Initialize the local fields.  */
363       hsh = hppa_stub_hash_entry (entry);
364       hsh->stub_sec = NULL;
365       hsh->stub_offset = 0;
366       hsh->target_value = 0;
367       hsh->target_section = NULL;
368       hsh->stub_type = hppa_stub_long_branch;
369       hsh->hh = NULL;
370       hsh->id_sec = NULL;
371     }
372
373   return entry;
374 }
375
376 /* Initialize an entry in the link hash table.  */
377
378 static struct bfd_hash_entry *
379 hppa_link_hash_newfunc (struct bfd_hash_entry *entry,
380                         struct bfd_hash_table *table,
381                         const char *string)
382 {
383   /* Allocate the structure if it has not already been allocated by a
384      subclass.  */
385   if (entry == NULL)
386     {
387       entry = bfd_hash_allocate (table,
388                                  sizeof (struct elf32_hppa_link_hash_entry));
389       if (entry == NULL)
390         return entry;
391     }
392
393   /* Call the allocation method of the superclass.  */
394   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
395   if (entry != NULL)
396     {
397       struct elf32_hppa_link_hash_entry *hh;
398
399       /* Initialize the local fields.  */
400       hh = hppa_elf_hash_entry (entry);
401       hh->hsh_cache = NULL;
402       hh->dyn_relocs = NULL;
403       hh->plabel = 0;
404       hh->tls_type = GOT_UNKNOWN;
405     }
406
407   return entry;
408 }
409
410 /* Create the derived linker hash table.  The PA ELF port uses the derived
411    hash table to keep information specific to the PA ELF linker (without
412    using static variables).  */
413
414 static struct bfd_link_hash_table *
415 elf32_hppa_link_hash_table_create (bfd *abfd)
416 {
417   struct elf32_hppa_link_hash_table *htab;
418   bfd_size_type amt = sizeof (*htab);
419
420   htab = bfd_malloc (amt);
421   if (htab == NULL)
422     return NULL;
423
424   if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, hppa_link_hash_newfunc,
425                                       sizeof (struct elf32_hppa_link_hash_entry)))
426     {
427       free (htab);
428       return NULL;
429     }
430
431   /* Init the stub hash table too.  */
432   if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
433                             sizeof (struct elf32_hppa_stub_hash_entry)))
434     return NULL;
435
436   htab->stub_bfd = NULL;
437   htab->add_stub_section = NULL;
438   htab->layout_sections_again = NULL;
439   htab->stub_group = NULL;
440   htab->sgot = NULL;
441   htab->srelgot = NULL;
442   htab->splt = NULL;
443   htab->srelplt = NULL;
444   htab->sdynbss = NULL;
445   htab->srelbss = NULL;
446   htab->text_segment_base = (bfd_vma) -1;
447   htab->data_segment_base = (bfd_vma) -1;
448   htab->multi_subspace = 0;
449   htab->has_12bit_branch = 0;
450   htab->has_17bit_branch = 0;
451   htab->has_22bit_branch = 0;
452   htab->need_plt_stub = 0;
453   htab->sym_sec.abfd = NULL;
454   htab->tls_ldm_got.refcount = 0;
455
456   return &htab->etab.root;
457 }
458
459 /* Free the derived linker hash table.  */
460
461 static void
462 elf32_hppa_link_hash_table_free (struct bfd_link_hash_table *btab)
463 {
464   struct elf32_hppa_link_hash_table *htab
465     = (struct elf32_hppa_link_hash_table *) btab;
466
467   bfd_hash_table_free (&htab->bstab);
468   _bfd_generic_link_hash_table_free (btab);
469 }
470
471 /* Build a name for an entry in the stub hash table.  */
472
473 static char *
474 hppa_stub_name (const asection *input_section,
475                 const asection *sym_sec,
476                 const struct elf32_hppa_link_hash_entry *hh,
477                 const Elf_Internal_Rela *rela)
478 {
479   char *stub_name;
480   bfd_size_type len;
481
482   if (hh)
483     {
484       len = 8 + 1 + strlen (hh_name (hh)) + 1 + 8 + 1;
485       stub_name = bfd_malloc (len);
486       if (stub_name != NULL)
487         sprintf (stub_name, "%08x_%s+%x",
488                  input_section->id & 0xffffffff,
489                  hh_name (hh),
490                  (int) rela->r_addend & 0xffffffff);
491     }
492   else
493     {
494       len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
495       stub_name = bfd_malloc (len);
496       if (stub_name != NULL)
497         sprintf (stub_name, "%08x_%x:%x+%x",
498                  input_section->id & 0xffffffff,
499                  sym_sec->id & 0xffffffff,
500                  (int) ELF32_R_SYM (rela->r_info) & 0xffffffff,
501                  (int) rela->r_addend & 0xffffffff);
502     }
503   return stub_name;
504 }
505
506 /* Look up an entry in the stub hash.  Stub entries are cached because
507    creating the stub name takes a bit of time.  */
508
509 static struct elf32_hppa_stub_hash_entry *
510 hppa_get_stub_entry (const asection *input_section,
511                      const asection *sym_sec,
512                      struct elf32_hppa_link_hash_entry *hh,
513                      const Elf_Internal_Rela *rela,
514                      struct elf32_hppa_link_hash_table *htab)
515 {
516   struct elf32_hppa_stub_hash_entry *hsh_entry;
517   const asection *id_sec;
518
519   /* If this input section is part of a group of sections sharing one
520      stub section, then use the id of the first section in the group.
521      Stub names need to include a section id, as there may well be
522      more than one stub used to reach say, printf, and we need to
523      distinguish between them.  */
524   id_sec = htab->stub_group[input_section->id].link_sec;
525
526   if (hh != NULL && hh->hsh_cache != NULL
527       && hh->hsh_cache->hh == hh
528       && hh->hsh_cache->id_sec == id_sec)
529     {
530       hsh_entry = hh->hsh_cache;
531     }
532   else
533     {
534       char *stub_name;
535
536       stub_name = hppa_stub_name (id_sec, sym_sec, hh, rela);
537       if (stub_name == NULL)
538         return NULL;
539
540       hsh_entry = hppa_stub_hash_lookup (&htab->bstab,
541                                           stub_name, FALSE, FALSE);
542       if (hh != NULL)
543         hh->hsh_cache = hsh_entry;
544
545       free (stub_name);
546     }
547
548   return hsh_entry;
549 }
550
551 /* Add a new stub entry to the stub hash.  Not all fields of the new
552    stub entry are initialised.  */
553
554 static struct elf32_hppa_stub_hash_entry *
555 hppa_add_stub (const char *stub_name,
556                asection *section,
557                struct elf32_hppa_link_hash_table *htab)
558 {
559   asection *link_sec;
560   asection *stub_sec;
561   struct elf32_hppa_stub_hash_entry *hsh;
562
563   link_sec = htab->stub_group[section->id].link_sec;
564   stub_sec = htab->stub_group[section->id].stub_sec;
565   if (stub_sec == NULL)
566     {
567       stub_sec = htab->stub_group[link_sec->id].stub_sec;
568       if (stub_sec == NULL)
569         {
570           size_t namelen;
571           bfd_size_type len;
572           char *s_name;
573
574           namelen = strlen (link_sec->name);
575           len = namelen + sizeof (STUB_SUFFIX);
576           s_name = bfd_alloc (htab->stub_bfd, len);
577           if (s_name == NULL)
578             return NULL;
579
580           memcpy (s_name, link_sec->name, namelen);
581           memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
582           stub_sec = (*htab->add_stub_section) (s_name, link_sec);
583           if (stub_sec == NULL)
584             return NULL;
585           htab->stub_group[link_sec->id].stub_sec = stub_sec;
586         }
587       htab->stub_group[section->id].stub_sec = stub_sec;
588     }
589
590   /* Enter this entry into the linker stub hash table.  */
591   hsh = hppa_stub_hash_lookup (&htab->bstab, stub_name,
592                                       TRUE, FALSE);
593   if (hsh == NULL)
594     {
595       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
596                              section->owner,
597                              stub_name);
598       return NULL;
599     }
600
601   hsh->stub_sec = stub_sec;
602   hsh->stub_offset = 0;
603   hsh->id_sec = link_sec;
604   return hsh;
605 }
606
607 /* Determine the type of stub needed, if any, for a call.  */
608
609 static enum elf32_hppa_stub_type
610 hppa_type_of_stub (asection *input_sec,
611                    const Elf_Internal_Rela *rela,
612                    struct elf32_hppa_link_hash_entry *hh,
613                    bfd_vma destination,
614                    struct bfd_link_info *info)
615 {
616   bfd_vma location;
617   bfd_vma branch_offset;
618   bfd_vma max_branch_offset;
619   unsigned int r_type;
620
621   if (hh != NULL
622       && hh->eh.plt.offset != (bfd_vma) -1
623       && hh->eh.dynindx != -1
624       && !hh->plabel
625       && (info->shared
626           || !hh->eh.def_regular
627           || hh->eh.root.type == bfd_link_hash_defweak))
628     {
629       /* We need an import stub.  Decide between hppa_stub_import
630          and hppa_stub_import_shared later.  */
631       return hppa_stub_import;
632     }
633
634   /* Determine where the call point is.  */
635   location = (input_sec->output_offset
636               + input_sec->output_section->vma
637               + rela->r_offset);
638
639   branch_offset = destination - location - 8;
640   r_type = ELF32_R_TYPE (rela->r_info);
641
642   /* Determine if a long branch stub is needed.  parisc branch offsets
643      are relative to the second instruction past the branch, ie. +8
644      bytes on from the branch instruction location.  The offset is
645      signed and counts in units of 4 bytes.  */
646   if (r_type == (unsigned int) R_PARISC_PCREL17F)
647     max_branch_offset = (1 << (17 - 1)) << 2;
648
649   else if (r_type == (unsigned int) R_PARISC_PCREL12F)
650     max_branch_offset = (1 << (12 - 1)) << 2;
651
652   else /* R_PARISC_PCREL22F.  */
653     max_branch_offset = (1 << (22 - 1)) << 2;
654
655   if (branch_offset + max_branch_offset >= 2*max_branch_offset)
656     return hppa_stub_long_branch;
657
658   return hppa_stub_none;
659 }
660
661 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
662    IN_ARG contains the link info pointer.  */
663
664 #define LDIL_R1         0x20200000      /* ldil  LR'XXX,%r1             */
665 #define BE_SR4_R1       0xe0202002      /* be,n  RR'XXX(%sr4,%r1)       */
666
667 #define BL_R1           0xe8200000      /* b,l   .+8,%r1                */
668 #define ADDIL_R1        0x28200000      /* addil LR'XXX,%r1,%r1         */
669 #define DEPI_R1         0xd4201c1e      /* depi  0,31,2,%r1             */
670
671 #define ADDIL_DP        0x2b600000      /* addil LR'XXX,%dp,%r1         */
672 #define LDW_R1_R21      0x48350000      /* ldw   RR'XXX(%sr0,%r1),%r21  */
673 #define BV_R0_R21       0xeaa0c000      /* bv    %r0(%r21)              */
674 #define LDW_R1_R19      0x48330000      /* ldw   RR'XXX(%sr0,%r1),%r19  */
675
676 #define ADDIL_R19       0x2a600000      /* addil LR'XXX,%r19,%r1        */
677 #define LDW_R1_DP       0x483b0000      /* ldw   RR'XXX(%sr0,%r1),%dp   */
678
679 #define LDSID_R21_R1    0x02a010a1      /* ldsid (%sr0,%r21),%r1        */
680 #define MTSP_R1         0x00011820      /* mtsp  %r1,%sr0               */
681 #define BE_SR0_R21      0xe2a00000      /* be    0(%sr0,%r21)           */
682 #define STW_RP          0x6bc23fd1      /* stw   %rp,-24(%sr0,%sp)      */
683
684 #define BL22_RP         0xe800a002      /* b,l,n XXX,%rp                */
685 #define BL_RP           0xe8400002      /* b,l,n XXX,%rp                */
686 #define NOP             0x08000240      /* nop                          */
687 #define LDW_RP          0x4bc23fd1      /* ldw   -24(%sr0,%sp),%rp      */
688 #define LDSID_RP_R1     0x004010a1      /* ldsid (%sr0,%rp),%r1         */
689 #define BE_SR0_RP       0xe0400002      /* be,n  0(%sr0,%rp)            */
690
691 #ifndef R19_STUBS
692 #define R19_STUBS 1
693 #endif
694
695 #if R19_STUBS
696 #define LDW_R1_DLT      LDW_R1_R19
697 #else
698 #define LDW_R1_DLT      LDW_R1_DP
699 #endif
700
701 static bfd_boolean
702 hppa_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
703 {
704   struct elf32_hppa_stub_hash_entry *hsh;
705   struct bfd_link_info *info;
706   struct elf32_hppa_link_hash_table *htab;
707   asection *stub_sec;
708   bfd *stub_bfd;
709   bfd_byte *loc;
710   bfd_vma sym_value;
711   bfd_vma insn;
712   bfd_vma off;
713   int val;
714   int size;
715
716   /* Massage our args to the form they really have.  */
717   hsh = hppa_stub_hash_entry (bh);
718   info = (struct bfd_link_info *)in_arg;
719
720   htab = hppa_link_hash_table (info);
721   stub_sec = hsh->stub_sec;
722
723   /* Make a note of the offset within the stubs for this entry.  */
724   hsh->stub_offset = stub_sec->size;
725   loc = stub_sec->contents + hsh->stub_offset;
726
727   stub_bfd = stub_sec->owner;
728
729   switch (hsh->stub_type)
730     {
731     case hppa_stub_long_branch:
732       /* Create the long branch.  A long branch is formed with "ldil"
733          loading the upper bits of the target address into a register,
734          then branching with "be" which adds in the lower bits.
735          The "be" has its delay slot nullified.  */
736       sym_value = (hsh->target_value
737                    + hsh->target_section->output_offset
738                    + hsh->target_section->output_section->vma);
739
740       val = hppa_field_adjust (sym_value, 0, e_lrsel);
741       insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
742       bfd_put_32 (stub_bfd, insn, loc);
743
744       val = hppa_field_adjust (sym_value, 0, e_rrsel) >> 2;
745       insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
746       bfd_put_32 (stub_bfd, insn, loc + 4);
747
748       size = 8;
749       break;
750
751     case hppa_stub_long_branch_shared:
752       /* Branches are relative.  This is where we are going to.  */
753       sym_value = (hsh->target_value
754                    + hsh->target_section->output_offset
755                    + hsh->target_section->output_section->vma);
756
757       /* And this is where we are coming from, more or less.  */
758       sym_value -= (hsh->stub_offset
759                     + stub_sec->output_offset
760                     + stub_sec->output_section->vma);
761
762       bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
763       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
764       insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
765       bfd_put_32 (stub_bfd, insn, loc + 4);
766
767       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
768       insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
769       bfd_put_32 (stub_bfd, insn, loc + 8);
770       size = 12;
771       break;
772
773     case hppa_stub_import:
774     case hppa_stub_import_shared:
775       off = hsh->hh->eh.plt.offset;
776       if (off >= (bfd_vma) -2)
777         abort ();
778
779       off &= ~ (bfd_vma) 1;
780       sym_value = (off
781                    + htab->splt->output_offset
782                    + htab->splt->output_section->vma
783                    - elf_gp (htab->splt->output_section->owner));
784
785       insn = ADDIL_DP;
786 #if R19_STUBS
787       if (hsh->stub_type == hppa_stub_import_shared)
788         insn = ADDIL_R19;
789 #endif
790       val = hppa_field_adjust (sym_value, 0, e_lrsel),
791       insn = hppa_rebuild_insn ((int) insn, val, 21);
792       bfd_put_32 (stub_bfd, insn, loc);
793
794       /* It is critical to use lrsel/rrsel here because we are using
795          two different offsets (+0 and +4) from sym_value.  If we use
796          lsel/rsel then with unfortunate sym_values we will round
797          sym_value+4 up to the next 2k block leading to a mis-match
798          between the lsel and rsel value.  */
799       val = hppa_field_adjust (sym_value, 0, e_rrsel);
800       insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
801       bfd_put_32 (stub_bfd, insn, loc + 4);
802
803       if (htab->multi_subspace)
804         {
805           val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
806           insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
807           bfd_put_32 (stub_bfd, insn, loc + 8);
808
809           bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
810           bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,      loc + 16);
811           bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21,   loc + 20);
812           bfd_put_32 (stub_bfd, (bfd_vma) STW_RP,       loc + 24);
813
814           size = 28;
815         }
816       else
817         {
818           bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
819           val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
820           insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
821           bfd_put_32 (stub_bfd, insn, loc + 12);
822
823           size = 16;
824         }
825
826       break;
827
828     case hppa_stub_export:
829       /* Branches are relative.  This is where we are going to.  */
830       sym_value = (hsh->target_value
831                    + hsh->target_section->output_offset
832                    + hsh->target_section->output_section->vma);
833
834       /* And this is where we are coming from.  */
835       sym_value -= (hsh->stub_offset
836                     + stub_sec->output_offset
837                     + stub_sec->output_section->vma);
838
839       if (sym_value - 8 + (1 << (17 + 1)) >= (1 << (17 + 2))
840           && (!htab->has_22bit_branch
841               || sym_value - 8 + (1 << (22 + 1)) >= (1 << (22 + 2))))
842         {
843           (*_bfd_error_handler)
844             (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
845              hsh->target_section->owner,
846              stub_sec,
847              (long) hsh->stub_offset,
848              hsh->bh_root.string);
849           bfd_set_error (bfd_error_bad_value);
850           return FALSE;
851         }
852
853       val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
854       if (!htab->has_22bit_branch)
855         insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
856       else
857         insn = hppa_rebuild_insn ((int) BL22_RP, val, 22);
858       bfd_put_32 (stub_bfd, insn, loc);
859
860       bfd_put_32 (stub_bfd, (bfd_vma) NOP,         loc + 4);
861       bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP,      loc + 8);
862       bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
863       bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1,     loc + 16);
864       bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP,   loc + 20);
865
866       /* Point the function symbol at the stub.  */
867       hsh->hh->eh.root.u.def.section = stub_sec;
868       hsh->hh->eh.root.u.def.value = stub_sec->size;
869
870       size = 24;
871       break;
872
873     default:
874       BFD_FAIL ();
875       return FALSE;
876     }
877
878   stub_sec->size += size;
879   return TRUE;
880 }
881
882 #undef LDIL_R1
883 #undef BE_SR4_R1
884 #undef BL_R1
885 #undef ADDIL_R1
886 #undef DEPI_R1
887 #undef LDW_R1_R21
888 #undef LDW_R1_DLT
889 #undef LDW_R1_R19
890 #undef ADDIL_R19
891 #undef LDW_R1_DP
892 #undef LDSID_R21_R1
893 #undef MTSP_R1
894 #undef BE_SR0_R21
895 #undef STW_RP
896 #undef BV_R0_R21
897 #undef BL_RP
898 #undef NOP
899 #undef LDW_RP
900 #undef LDSID_RP_R1
901 #undef BE_SR0_RP
902
903 /* As above, but don't actually build the stub.  Just bump offset so
904    we know stub section sizes.  */
905
906 static bfd_boolean
907 hppa_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
908 {
909   struct elf32_hppa_stub_hash_entry *hsh;
910   struct elf32_hppa_link_hash_table *htab;
911   int size;
912
913   /* Massage our args to the form they really have.  */
914   hsh = hppa_stub_hash_entry (bh);
915   htab = in_arg;
916
917   if (hsh->stub_type == hppa_stub_long_branch)
918     size = 8;
919   else if (hsh->stub_type == hppa_stub_long_branch_shared)
920     size = 12;
921   else if (hsh->stub_type == hppa_stub_export)
922     size = 24;
923   else /* hppa_stub_import or hppa_stub_import_shared.  */
924     {
925       if (htab->multi_subspace)
926         size = 28;
927       else
928         size = 16;
929     }
930
931   hsh->stub_sec->size += size;
932   return TRUE;
933 }
934
935 /* Return nonzero if ABFD represents an HPPA ELF32 file.
936    Additionally we set the default architecture and machine.  */
937
938 static bfd_boolean
939 elf32_hppa_object_p (bfd *abfd)
940 {
941   Elf_Internal_Ehdr * i_ehdrp;
942   unsigned int flags;
943
944   i_ehdrp = elf_elfheader (abfd);
945   if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
946     {
947       /* GCC on hppa-linux produces binaries with OSABI=Linux,
948          but the kernel produces corefiles with OSABI=SysV.  */
949       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX &&
950           i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
951         return FALSE;
952     }
953   else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
954     {
955       /* GCC on hppa-netbsd produces binaries with OSABI=NetBSD,
956          but the kernel produces corefiles with OSABI=SysV.  */
957       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NETBSD &&
958           i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
959         return FALSE;
960     }
961   else
962     {
963       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
964         return FALSE;
965     }
966
967   flags = i_ehdrp->e_flags;
968   switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
969     {
970     case EFA_PARISC_1_0:
971       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
972     case EFA_PARISC_1_1:
973       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
974     case EFA_PARISC_2_0:
975       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
976     case EFA_PARISC_2_0 | EF_PARISC_WIDE:
977       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
978     }
979   return TRUE;
980 }
981
982 /* Create the .plt and .got sections, and set up our hash table
983    short-cuts to various dynamic sections.  */
984
985 static bfd_boolean
986 elf32_hppa_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
987 {
988   struct elf32_hppa_link_hash_table *htab;
989   struct elf_link_hash_entry *eh;
990
991   /* Don't try to create the .plt and .got twice.  */
992   htab = hppa_link_hash_table (info);
993   if (htab->splt != NULL)
994     return TRUE;
995
996   /* Call the generic code to do most of the work.  */
997   if (! _bfd_elf_create_dynamic_sections (abfd, info))
998     return FALSE;
999
1000   htab->splt = bfd_get_section_by_name (abfd, ".plt");
1001   htab->srelplt = bfd_get_section_by_name (abfd, ".rela.plt");
1002
1003   htab->sgot = bfd_get_section_by_name (abfd, ".got");
1004   htab->srelgot = bfd_make_section_with_flags (abfd, ".rela.got",
1005                                                (SEC_ALLOC
1006                                                 | SEC_LOAD
1007                                                 | SEC_HAS_CONTENTS
1008                                                 | SEC_IN_MEMORY
1009                                                 | SEC_LINKER_CREATED
1010                                                 | SEC_READONLY));
1011   if (htab->srelgot == NULL
1012       || ! bfd_set_section_alignment (abfd, htab->srelgot, 2))
1013     return FALSE;
1014
1015   htab->sdynbss = bfd_get_section_by_name (abfd, ".dynbss");
1016   htab->srelbss = bfd_get_section_by_name (abfd, ".rela.bss");
1017
1018   /* hppa-linux needs _GLOBAL_OFFSET_TABLE_ to be visible from the main
1019      application, because __canonicalize_funcptr_for_compare needs it.  */
1020   eh = elf_hash_table (info)->hgot;
1021   eh->forced_local = 0;
1022   eh->other = STV_DEFAULT;
1023   return bfd_elf_link_record_dynamic_symbol (info, eh);
1024 }
1025
1026 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
1027
1028 static void
1029 elf32_hppa_copy_indirect_symbol (struct bfd_link_info *info,
1030                                  struct elf_link_hash_entry *eh_dir,
1031                                  struct elf_link_hash_entry *eh_ind)
1032 {
1033   struct elf32_hppa_link_hash_entry *hh_dir, *hh_ind;
1034
1035   hh_dir = hppa_elf_hash_entry (eh_dir);
1036   hh_ind = hppa_elf_hash_entry (eh_ind);
1037
1038   if (hh_ind->dyn_relocs != NULL)
1039     {
1040       if (hh_dir->dyn_relocs != NULL)
1041         {
1042           struct elf32_hppa_dyn_reloc_entry **hdh_pp;
1043           struct elf32_hppa_dyn_reloc_entry *hdh_p;
1044
1045           /* Add reloc counts against the indirect sym to the direct sym
1046              list.  Merge any entries against the same section.  */
1047           for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
1048             {
1049               struct elf32_hppa_dyn_reloc_entry *hdh_q;
1050
1051               for (hdh_q = hh_dir->dyn_relocs;
1052                    hdh_q != NULL;
1053                    hdh_q = hdh_q->hdh_next)
1054                 if (hdh_q->sec == hdh_p->sec)
1055                   {
1056 #if RELATIVE_DYNRELOCS
1057                     hdh_q->relative_count += hdh_p->relative_count;
1058 #endif
1059                     hdh_q->count += hdh_p->count;
1060                     *hdh_pp = hdh_p->hdh_next;
1061                     break;
1062                   }
1063               if (hdh_q == NULL)
1064                 hdh_pp = &hdh_p->hdh_next;
1065             }
1066           *hdh_pp = hh_dir->dyn_relocs;
1067         }
1068
1069       hh_dir->dyn_relocs = hh_ind->dyn_relocs;
1070       hh_ind->dyn_relocs = NULL;
1071     }
1072
1073   if (ELIMINATE_COPY_RELOCS
1074       && eh_ind->root.type != bfd_link_hash_indirect
1075       && eh_dir->dynamic_adjusted)
1076     {
1077       /* If called to transfer flags for a weakdef during processing
1078          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1079          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
1080       eh_dir->ref_dynamic |= eh_ind->ref_dynamic;
1081       eh_dir->ref_regular |= eh_ind->ref_regular;
1082       eh_dir->ref_regular_nonweak |= eh_ind->ref_regular_nonweak;
1083       eh_dir->needs_plt |= eh_ind->needs_plt;
1084     }
1085   else
1086     {
1087       if (eh_ind->root.type == bfd_link_hash_indirect
1088           && eh_dir->got.refcount <= 0)
1089         {
1090           hh_dir->tls_type = hh_ind->tls_type;
1091           hh_ind->tls_type = GOT_UNKNOWN;
1092         }
1093
1094       _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
1095     }
1096 }
1097
1098 static int
1099 elf32_hppa_optimized_tls_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1100                                 int r_type, int is_local ATTRIBUTE_UNUSED)
1101 {
1102   /* For now we don't support linker optimizations.  */
1103   return r_type;
1104 }
1105
1106 /* Look through the relocs for a section during the first phase, and
1107    calculate needed space in the global offset table, procedure linkage
1108    table, and dynamic reloc sections.  At this point we haven't
1109    necessarily read all the input files.  */
1110
1111 static bfd_boolean
1112 elf32_hppa_check_relocs (bfd *abfd,
1113                          struct bfd_link_info *info,
1114                          asection *sec,
1115                          const Elf_Internal_Rela *relocs)
1116 {
1117   Elf_Internal_Shdr *symtab_hdr;
1118   struct elf_link_hash_entry **eh_syms;
1119   const Elf_Internal_Rela *rela;
1120   const Elf_Internal_Rela *rela_end;
1121   struct elf32_hppa_link_hash_table *htab;
1122   asection *sreloc;
1123   asection *stubreloc;
1124   int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN;
1125
1126   if (info->relocatable)
1127     return TRUE;
1128
1129   htab = hppa_link_hash_table (info);
1130   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1131   eh_syms = elf_sym_hashes (abfd);
1132   sreloc = NULL;
1133   stubreloc = NULL;
1134
1135   rela_end = relocs + sec->reloc_count;
1136   for (rela = relocs; rela < rela_end; rela++)
1137     {
1138       enum {
1139         NEED_GOT = 1,
1140         NEED_PLT = 2,
1141         NEED_DYNREL = 4,
1142         PLT_PLABEL = 8
1143       };
1144
1145       unsigned int r_symndx, r_type;
1146       struct elf32_hppa_link_hash_entry *hh;
1147       int need_entry = 0;
1148
1149       r_symndx = ELF32_R_SYM (rela->r_info);
1150
1151       if (r_symndx < symtab_hdr->sh_info)
1152         hh = NULL;
1153       else
1154         {
1155           hh =  hppa_elf_hash_entry (eh_syms[r_symndx - symtab_hdr->sh_info]);
1156           while (hh->eh.root.type == bfd_link_hash_indirect
1157                  || hh->eh.root.type == bfd_link_hash_warning)
1158             hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
1159         }
1160
1161       r_type = ELF32_R_TYPE (rela->r_info);
1162       r_type = elf32_hppa_optimized_tls_reloc (info, r_type, hh == NULL);
1163
1164       switch (r_type)
1165         {
1166         case R_PARISC_DLTIND14F:
1167         case R_PARISC_DLTIND14R:
1168         case R_PARISC_DLTIND21L:
1169           /* This symbol requires a global offset table entry.  */
1170           need_entry = NEED_GOT;
1171           break;
1172
1173         case R_PARISC_PLABEL14R: /* "Official" procedure labels.  */
1174         case R_PARISC_PLABEL21L:
1175         case R_PARISC_PLABEL32:
1176           /* If the addend is non-zero, we break badly.  */
1177           if (rela->r_addend != 0)
1178             abort ();
1179
1180           /* If we are creating a shared library, then we need to
1181              create a PLT entry for all PLABELs, because PLABELs with
1182              local symbols may be passed via a pointer to another
1183              object.  Additionally, output a dynamic relocation
1184              pointing to the PLT entry.
1185
1186              For executables, the original 32-bit ABI allowed two
1187              different styles of PLABELs (function pointers):  For
1188              global functions, the PLABEL word points into the .plt
1189              two bytes past a (function address, gp) pair, and for
1190              local functions the PLABEL points directly at the
1191              function.  The magic +2 for the first type allows us to
1192              differentiate between the two.  As you can imagine, this
1193              is a real pain when it comes to generating code to call
1194              functions indirectly or to compare function pointers.
1195              We avoid the mess by always pointing a PLABEL into the
1196              .plt, even for local functions.  */
1197           need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1198           break;
1199
1200         case R_PARISC_PCREL12F:
1201           htab->has_12bit_branch = 1;
1202           goto branch_common;
1203
1204         case R_PARISC_PCREL17C:
1205         case R_PARISC_PCREL17F:
1206           htab->has_17bit_branch = 1;
1207           goto branch_common;
1208
1209         case R_PARISC_PCREL22F:
1210           htab->has_22bit_branch = 1;
1211         branch_common:
1212           /* Function calls might need to go through the .plt, and
1213              might require long branch stubs.  */
1214           if (hh == NULL)
1215             {
1216               /* We know local syms won't need a .plt entry, and if
1217                  they need a long branch stub we can't guarantee that
1218                  we can reach the stub.  So just flag an error later
1219                  if we're doing a shared link and find we need a long
1220                  branch stub.  */
1221               continue;
1222             }
1223           else
1224             {
1225               /* Global symbols will need a .plt entry if they remain
1226                  global, and in most cases won't need a long branch
1227                  stub.  Unfortunately, we have to cater for the case
1228                  where a symbol is forced local by versioning, or due
1229                  to symbolic linking, and we lose the .plt entry.  */
1230               need_entry = NEED_PLT;
1231               if (hh->eh.type == STT_PARISC_MILLI)
1232                 need_entry = 0;
1233             }
1234           break;
1235
1236         case R_PARISC_SEGBASE:  /* Used to set segment base.  */
1237         case R_PARISC_SEGREL32: /* Relative reloc, used for unwind.  */
1238         case R_PARISC_PCREL14F: /* PC relative load/store.  */
1239         case R_PARISC_PCREL14R:
1240         case R_PARISC_PCREL17R: /* External branches.  */
1241         case R_PARISC_PCREL21L: /* As above, and for load/store too.  */
1242         case R_PARISC_PCREL32:
1243           /* We don't need to propagate the relocation if linking a
1244              shared object since these are section relative.  */
1245           continue;
1246
1247         case R_PARISC_DPREL14F: /* Used for gp rel data load/store.  */
1248         case R_PARISC_DPREL14R:
1249         case R_PARISC_DPREL21L:
1250           if (info->shared)
1251             {
1252               (*_bfd_error_handler)
1253                 (_("%B: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1254                  abfd,
1255                  elf_hppa_howto_table[r_type].name);
1256               bfd_set_error (bfd_error_bad_value);
1257               return FALSE;
1258             }
1259           /* Fall through.  */
1260
1261         case R_PARISC_DIR17F: /* Used for external branches.  */
1262         case R_PARISC_DIR17R:
1263         case R_PARISC_DIR14F: /* Used for load/store from absolute locn.  */
1264         case R_PARISC_DIR14R:
1265         case R_PARISC_DIR21L: /* As above, and for ext branches too.  */
1266         case R_PARISC_DIR32: /* .word relocs.  */
1267           /* We may want to output a dynamic relocation later.  */
1268           need_entry = NEED_DYNREL;
1269           break;
1270
1271           /* This relocation describes the C++ object vtable hierarchy.
1272              Reconstruct it for later use during GC.  */
1273         case R_PARISC_GNU_VTINHERIT:
1274           if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh, rela->r_offset))
1275             return FALSE;
1276           continue;
1277
1278           /* This relocation describes which C++ vtable entries are actually
1279              used.  Record for later use during GC.  */
1280         case R_PARISC_GNU_VTENTRY:
1281           BFD_ASSERT (hh != NULL);
1282           if (hh != NULL
1283               && !bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rela->r_addend))
1284             return FALSE;
1285           continue;
1286
1287         case R_PARISC_TLS_GD21L:
1288         case R_PARISC_TLS_GD14R:
1289         case R_PARISC_TLS_LDM21L:
1290         case R_PARISC_TLS_LDM14R:
1291           need_entry = NEED_GOT;
1292           break;
1293
1294         case R_PARISC_TLS_IE21L:
1295         case R_PARISC_TLS_IE14R:
1296           if (info->shared)
1297             info->flags |= DF_STATIC_TLS;
1298           need_entry = NEED_GOT;
1299           break;
1300
1301         default:
1302           continue;
1303         }
1304
1305       /* Now carry out our orders.  */
1306       if (need_entry & NEED_GOT)
1307         {
1308           switch (r_type)
1309             {
1310             default:
1311               tls_type = GOT_NORMAL;
1312               break;
1313             case R_PARISC_TLS_GD21L:
1314             case R_PARISC_TLS_GD14R:
1315               tls_type |= GOT_TLS_GD;
1316               break;
1317             case R_PARISC_TLS_LDM21L:
1318             case R_PARISC_TLS_LDM14R:
1319               tls_type |= GOT_TLS_LDM;
1320               break;
1321             case R_PARISC_TLS_IE21L:
1322             case R_PARISC_TLS_IE14R:
1323               tls_type |= GOT_TLS_IE;
1324               break;
1325             }
1326
1327           /* Allocate space for a GOT entry, as well as a dynamic
1328              relocation for this entry.  */
1329           if (htab->sgot == NULL)
1330             {
1331               if (htab->etab.dynobj == NULL)
1332                 htab->etab.dynobj = abfd;
1333               if (!elf32_hppa_create_dynamic_sections (htab->etab.dynobj, info))
1334                 return FALSE;
1335             }
1336
1337           if (r_type == R_PARISC_TLS_LDM21L
1338               || r_type == R_PARISC_TLS_LDM14R)
1339             hppa_link_hash_table (info)->tls_ldm_got.refcount += 1;
1340           else
1341             {
1342               if (hh != NULL)
1343                 {
1344                   hh->eh.got.refcount += 1;
1345                   old_tls_type = hh->tls_type;
1346                 }
1347               else
1348                 {
1349                   bfd_signed_vma *local_got_refcounts;
1350                   
1351                   /* This is a global offset table entry for a local symbol.  */
1352                   local_got_refcounts = elf_local_got_refcounts (abfd);
1353                   if (local_got_refcounts == NULL)
1354                     {
1355                       bfd_size_type size;
1356
1357                       /* Allocate space for local got offsets and local
1358                          plt offsets.  Done this way to save polluting
1359                          elf_obj_tdata with another target specific
1360                          pointer.  */
1361                       size = symtab_hdr->sh_info;
1362                       size *= 2 * sizeof (bfd_signed_vma);
1363                       /* Add in space to store the local GOT TLS types.  */
1364                       size += symtab_hdr->sh_info;
1365                       local_got_refcounts = bfd_zalloc (abfd, size);
1366                       if (local_got_refcounts == NULL)
1367                         return FALSE;
1368                       elf_local_got_refcounts (abfd) = local_got_refcounts;
1369                       memset (hppa_elf_local_got_tls_type (abfd),
1370                           GOT_UNKNOWN, symtab_hdr->sh_info);
1371                     }
1372                   local_got_refcounts[r_symndx] += 1;
1373
1374                   old_tls_type = hppa_elf_local_got_tls_type (abfd) [r_symndx];
1375                 }
1376
1377               tls_type |= old_tls_type;
1378
1379               if (old_tls_type != tls_type)
1380                 {
1381                   if (hh != NULL)
1382                     hh->tls_type = tls_type;
1383                   else
1384                     hppa_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
1385                 }
1386
1387             }
1388         }
1389
1390       if (need_entry & NEED_PLT)
1391         {
1392           /* If we are creating a shared library, and this is a reloc
1393              against a weak symbol or a global symbol in a dynamic
1394              object, then we will be creating an import stub and a
1395              .plt entry for the symbol.  Similarly, on a normal link
1396              to symbols defined in a dynamic object we'll need the
1397              import stub and a .plt entry.  We don't know yet whether
1398              the symbol is defined or not, so make an entry anyway and
1399              clean up later in adjust_dynamic_symbol.  */
1400           if ((sec->flags & SEC_ALLOC) != 0)
1401             {
1402               if (hh != NULL)
1403                 {
1404                   hh->eh.needs_plt = 1;
1405                   hh->eh.plt.refcount += 1;
1406
1407                   /* If this .plt entry is for a plabel, mark it so
1408                      that adjust_dynamic_symbol will keep the entry
1409                      even if it appears to be local.  */
1410                   if (need_entry & PLT_PLABEL)
1411                     hh->plabel = 1;
1412                 }
1413               else if (need_entry & PLT_PLABEL)
1414                 {
1415                   bfd_signed_vma *local_got_refcounts;
1416                   bfd_signed_vma *local_plt_refcounts;
1417
1418                   local_got_refcounts = elf_local_got_refcounts (abfd);
1419                   if (local_got_refcounts == NULL)
1420                     {
1421                       bfd_size_type size;
1422
1423                       /* Allocate space for local got offsets and local
1424                          plt offsets.  */
1425                       size = symtab_hdr->sh_info;
1426                       size *= 2 * sizeof (bfd_signed_vma);
1427                       /* Add in space to store the local GOT TLS types.  */
1428                       size += symtab_hdr->sh_info;
1429                       local_got_refcounts = bfd_zalloc (abfd, size);
1430                       if (local_got_refcounts == NULL)
1431                         return FALSE;
1432                       elf_local_got_refcounts (abfd) = local_got_refcounts;
1433                     }
1434                   local_plt_refcounts = (local_got_refcounts
1435                                          + symtab_hdr->sh_info);
1436                   local_plt_refcounts[r_symndx] += 1;
1437                 }
1438             }
1439         }
1440
1441       if (need_entry & NEED_DYNREL)
1442         {
1443           /* Flag this symbol as having a non-got, non-plt reference
1444              so that we generate copy relocs if it turns out to be
1445              dynamic.  */
1446           if (hh != NULL && !info->shared)
1447             hh->eh.non_got_ref = 1;
1448
1449           /* If we are creating a shared library then we need to copy
1450              the reloc into the shared library.  However, if we are
1451              linking with -Bsymbolic, we need only copy absolute
1452              relocs or relocs against symbols that are not defined in
1453              an object we are including in the link.  PC- or DP- or
1454              DLT-relative relocs against any local sym or global sym
1455              with DEF_REGULAR set, can be discarded.  At this point we
1456              have not seen all the input files, so it is possible that
1457              DEF_REGULAR is not set now but will be set later (it is
1458              never cleared).  We account for that possibility below by
1459              storing information in the dyn_relocs field of the
1460              hash table entry.
1461
1462              A similar situation to the -Bsymbolic case occurs when
1463              creating shared libraries and symbol visibility changes
1464              render the symbol local.
1465
1466              As it turns out, all the relocs we will be creating here
1467              are absolute, so we cannot remove them on -Bsymbolic
1468              links or visibility changes anyway.  A STUB_REL reloc
1469              is absolute too, as in that case it is the reloc in the
1470              stub we will be creating, rather than copying the PCREL
1471              reloc in the branch.
1472
1473              If on the other hand, we are creating an executable, we
1474              may need to keep relocations for symbols satisfied by a
1475              dynamic library if we manage to avoid copy relocs for the
1476              symbol.  */
1477           if ((info->shared
1478                && (sec->flags & SEC_ALLOC) != 0
1479                && (IS_ABSOLUTE_RELOC (r_type)
1480                    || (hh != NULL
1481                        && (!info->symbolic
1482                            || hh->eh.root.type == bfd_link_hash_defweak
1483                            || !hh->eh.def_regular))))
1484               || (ELIMINATE_COPY_RELOCS
1485                   && !info->shared
1486                   && (sec->flags & SEC_ALLOC) != 0
1487                   && hh != NULL
1488                   && (hh->eh.root.type == bfd_link_hash_defweak
1489                       || !hh->eh.def_regular)))
1490             {
1491               struct elf32_hppa_dyn_reloc_entry *hdh_p;
1492               struct elf32_hppa_dyn_reloc_entry **hdh_head;
1493
1494               /* Create a reloc section in dynobj and make room for
1495                  this reloc.  */
1496               if (sreloc == NULL)
1497                 {
1498                   char *name;
1499                   bfd *dynobj;
1500
1501                   name = (bfd_elf_string_from_elf_section
1502                           (abfd,
1503                            elf_elfheader (abfd)->e_shstrndx,
1504                            elf_section_data (sec)->rel_hdr.sh_name));
1505                   if (name == NULL)
1506                     {
1507                       (*_bfd_error_handler)
1508                         (_("Could not find relocation section for %s"),
1509                          sec->name);
1510                       bfd_set_error (bfd_error_bad_value);
1511                       return FALSE;
1512                     }
1513
1514                   if (htab->etab.dynobj == NULL)
1515                     htab->etab.dynobj = abfd;
1516
1517                   dynobj = htab->etab.dynobj;
1518                   sreloc = bfd_get_section_by_name (dynobj, name);
1519                   if (sreloc == NULL)
1520                     {
1521                       flagword flags;
1522
1523                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
1524                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1525                       if ((sec->flags & SEC_ALLOC) != 0)
1526                         flags |= SEC_ALLOC | SEC_LOAD;
1527                       sreloc = bfd_make_section_with_flags (dynobj,
1528                                                             name,
1529                                                             flags);
1530                       if (sreloc == NULL
1531                           || !bfd_set_section_alignment (dynobj, sreloc, 2))
1532                         return FALSE;
1533                     }
1534
1535                   elf_section_data (sec)->sreloc = sreloc;
1536                 }
1537
1538               /* If this is a global symbol, we count the number of
1539                  relocations we need for this symbol.  */
1540               if (hh != NULL)
1541                 {
1542                   hdh_head = &hh->dyn_relocs;
1543                 }
1544               else
1545                 {
1546                   /* Track dynamic relocs needed for local syms too.
1547                      We really need local syms available to do this
1548                      easily.  Oh well.  */
1549
1550                   asection *sr;
1551                   void *vpp;
1552
1553                   sr = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1554                                                        sec, r_symndx);
1555                   if (sr == NULL)
1556                     return FALSE;
1557
1558                   vpp = &elf_section_data (sr)->local_dynrel;
1559                   hdh_head = (struct elf32_hppa_dyn_reloc_entry **) vpp;
1560                 }
1561
1562               hdh_p = *hdh_head;
1563               if (hdh_p == NULL || hdh_p->sec != sec)
1564                 {
1565                   hdh_p = bfd_alloc (htab->etab.dynobj, sizeof *hdh_p);
1566                   if (hdh_p == NULL)
1567                     return FALSE;
1568                   hdh_p->hdh_next = *hdh_head;
1569                   *hdh_head = hdh_p;
1570                   hdh_p->sec = sec;
1571                   hdh_p->count = 0;
1572 #if RELATIVE_DYNRELOCS
1573                   hdh_p->relative_count = 0;
1574 #endif
1575                 }
1576
1577               hdh_p->count += 1;
1578 #if RELATIVE_DYNRELOCS
1579               if (!IS_ABSOLUTE_RELOC (rtype))
1580                 hdh_p->relative_count += 1;
1581 #endif
1582             }
1583         }
1584     }
1585
1586   return TRUE;
1587 }
1588
1589 /* Return the section that should be marked against garbage collection
1590    for a given relocation.  */
1591
1592 static asection *
1593 elf32_hppa_gc_mark_hook (asection *sec,
1594                          struct bfd_link_info *info,
1595                          Elf_Internal_Rela *rela,
1596                          struct elf_link_hash_entry *hh,
1597                          Elf_Internal_Sym *sym)
1598 {
1599   if (hh != NULL)
1600     switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
1601       {
1602       case R_PARISC_GNU_VTINHERIT:
1603       case R_PARISC_GNU_VTENTRY:
1604         return NULL;
1605       }
1606
1607   return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
1608 }
1609
1610 /* Update the got and plt entry reference counts for the section being
1611    removed.  */
1612
1613 static bfd_boolean
1614 elf32_hppa_gc_sweep_hook (bfd *abfd,
1615                           struct bfd_link_info *info ATTRIBUTE_UNUSED,
1616                           asection *sec,
1617                           const Elf_Internal_Rela *relocs)
1618 {
1619   Elf_Internal_Shdr *symtab_hdr;
1620   struct elf_link_hash_entry **eh_syms;
1621   bfd_signed_vma *local_got_refcounts;
1622   bfd_signed_vma *local_plt_refcounts;
1623   const Elf_Internal_Rela *rela, *relend;
1624
1625   elf_section_data (sec)->local_dynrel = NULL;
1626
1627   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1628   eh_syms = elf_sym_hashes (abfd);
1629   local_got_refcounts = elf_local_got_refcounts (abfd);
1630   local_plt_refcounts = local_got_refcounts;
1631   if (local_plt_refcounts != NULL)
1632     local_plt_refcounts += symtab_hdr->sh_info;
1633
1634   relend = relocs + sec->reloc_count;
1635   for (rela = relocs; rela < relend; rela++)
1636     {
1637       unsigned long r_symndx;
1638       unsigned int r_type;
1639       struct elf_link_hash_entry *eh = NULL;
1640
1641       r_symndx = ELF32_R_SYM (rela->r_info);
1642       if (r_symndx >= symtab_hdr->sh_info)
1643         {
1644           struct elf32_hppa_link_hash_entry *hh;
1645           struct elf32_hppa_dyn_reloc_entry **hdh_pp;
1646           struct elf32_hppa_dyn_reloc_entry *hdh_p;
1647
1648           eh = eh_syms[r_symndx - symtab_hdr->sh_info];
1649           while (eh->root.type == bfd_link_hash_indirect
1650                  || eh->root.type == bfd_link_hash_warning)
1651             eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
1652           hh = hppa_elf_hash_entry (eh);
1653
1654           for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; hdh_pp = &hdh_p->hdh_next)
1655             if (hdh_p->sec == sec)
1656               {
1657                 /* Everything must go for SEC.  */
1658                 *hdh_pp = hdh_p->hdh_next;
1659                 break;
1660               }
1661         }
1662
1663       r_type = ELF32_R_TYPE (rela->r_info);
1664       r_type = elf32_hppa_optimized_tls_reloc (info, r_type, eh != NULL);
1665
1666       switch (r_type)
1667         {
1668         case R_PARISC_DLTIND14F:
1669         case R_PARISC_DLTIND14R:
1670         case R_PARISC_DLTIND21L:
1671         case R_PARISC_TLS_GD21L:
1672         case R_PARISC_TLS_GD14R:
1673         case R_PARISC_TLS_IE21L:
1674         case R_PARISC_TLS_IE14R:
1675           if (eh != NULL)
1676             {
1677               if (eh->got.refcount > 0)
1678                 eh->got.refcount -= 1;
1679             }
1680           else if (local_got_refcounts != NULL)
1681             {
1682               if (local_got_refcounts[r_symndx] > 0)
1683                 local_got_refcounts[r_symndx] -= 1;
1684             }
1685           break;
1686
1687         case R_PARISC_TLS_LDM21L:
1688         case R_PARISC_TLS_LDM14R:
1689           hppa_link_hash_table (info)->tls_ldm_got.refcount -= 1;
1690           break;
1691
1692         case R_PARISC_PCREL12F:
1693         case R_PARISC_PCREL17C:
1694         case R_PARISC_PCREL17F:
1695         case R_PARISC_PCREL22F:
1696           if (eh != NULL)
1697             {
1698               if (eh->plt.refcount > 0)
1699                 eh->plt.refcount -= 1;
1700             }
1701           break;
1702
1703         case R_PARISC_PLABEL14R:
1704         case R_PARISC_PLABEL21L:
1705         case R_PARISC_PLABEL32:
1706           if (eh != NULL)
1707             {
1708               if (eh->plt.refcount > 0)
1709                 eh->plt.refcount -= 1;
1710             }
1711           else if (local_plt_refcounts != NULL)
1712             {
1713               if (local_plt_refcounts[r_symndx] > 0)
1714                 local_plt_refcounts[r_symndx] -= 1;
1715             }
1716           break;
1717
1718         default:
1719           break;
1720         }
1721     }
1722
1723   return TRUE;
1724 }
1725
1726 /* Support for core dump NOTE sections.  */
1727
1728 static bfd_boolean
1729 elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1730 {
1731   int offset;
1732   size_t size;
1733
1734   switch (note->descsz)
1735     {
1736       default:
1737         return FALSE;
1738
1739       case 396:         /* Linux/hppa */
1740         /* pr_cursig */
1741         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1742
1743         /* pr_pid */
1744         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1745
1746         /* pr_reg */
1747         offset = 72;
1748         size = 320;
1749
1750         break;
1751     }
1752
1753   /* Make a ".reg/999" section.  */
1754   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1755                                           size, note->descpos + offset);
1756 }
1757
1758 static bfd_boolean
1759 elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1760 {
1761   switch (note->descsz)
1762     {
1763       default:
1764         return FALSE;
1765
1766       case 124:         /* Linux/hppa elf_prpsinfo.  */
1767         elf_tdata (abfd)->core_program
1768           = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1769         elf_tdata (abfd)->core_command
1770           = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1771     }
1772
1773   /* Note that for some reason, a spurious space is tacked
1774      onto the end of the args in some (at least one anyway)
1775      implementations, so strip it off if it exists.  */
1776   {
1777     char *command = elf_tdata (abfd)->core_command;
1778     int n = strlen (command);
1779
1780     if (0 < n && command[n - 1] == ' ')
1781       command[n - 1] = '\0';
1782   }
1783
1784   return TRUE;
1785 }
1786
1787 /* Our own version of hide_symbol, so that we can keep plt entries for
1788    plabels.  */
1789
1790 static void
1791 elf32_hppa_hide_symbol (struct bfd_link_info *info,
1792                         struct elf_link_hash_entry *eh,
1793                         bfd_boolean force_local)
1794 {
1795   if (force_local)
1796     {
1797       eh->forced_local = 1;
1798       if (eh->dynindx != -1)
1799         {
1800           eh->dynindx = -1;
1801           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1802                                   eh->dynstr_index);
1803         }
1804     }
1805
1806   if (! hppa_elf_hash_entry (eh)->plabel)
1807     {
1808       eh->needs_plt = 0;
1809       eh->plt = elf_hash_table (info)->init_plt_refcount;
1810     }
1811 }
1812
1813 /* Adjust a symbol defined by a dynamic object and referenced by a
1814    regular object.  The current definition is in some section of the
1815    dynamic object, but we're not including those sections.  We have to
1816    change the definition to something the rest of the link can
1817    understand.  */
1818
1819 static bfd_boolean
1820 elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
1821                                   struct elf_link_hash_entry *eh)
1822 {
1823   struct elf32_hppa_link_hash_table *htab;
1824   asection *sec;
1825
1826   /* If this is a function, put it in the procedure linkage table.  We
1827      will fill in the contents of the procedure linkage table later.  */
1828   if (eh->type == STT_FUNC
1829       || eh->needs_plt)
1830     {
1831       if (eh->plt.refcount <= 0
1832           || (eh->def_regular
1833               && eh->root.type != bfd_link_hash_defweak
1834               && ! hppa_elf_hash_entry (eh)->plabel
1835               && (!info->shared || info->symbolic)))
1836         {
1837           /* The .plt entry is not needed when:
1838              a) Garbage collection has removed all references to the
1839              symbol, or
1840              b) We know for certain the symbol is defined in this
1841              object, and it's not a weak definition, nor is the symbol
1842              used by a plabel relocation.  Either this object is the
1843              application or we are doing a shared symbolic link.  */
1844
1845           eh->plt.offset = (bfd_vma) -1;
1846           eh->needs_plt = 0;
1847         }
1848
1849       return TRUE;
1850     }
1851   else
1852     eh->plt.offset = (bfd_vma) -1;
1853
1854   /* If this is a weak symbol, and there is a real definition, the
1855      processor independent code will have arranged for us to see the
1856      real definition first, and we can just use the same value.  */
1857   if (eh->u.weakdef != NULL)
1858     {
1859       if (eh->u.weakdef->root.type != bfd_link_hash_defined
1860           && eh->u.weakdef->root.type != bfd_link_hash_defweak)
1861         abort ();
1862       eh->root.u.def.section = eh->u.weakdef->root.u.def.section;
1863       eh->root.u.def.value = eh->u.weakdef->root.u.def.value;
1864       if (ELIMINATE_COPY_RELOCS)
1865         eh->non_got_ref = eh->u.weakdef->non_got_ref;
1866       return TRUE;
1867     }
1868
1869   /* This is a reference to a symbol defined by a dynamic object which
1870      is not a function.  */
1871
1872   /* If we are creating a shared library, we must presume that the
1873      only references to the symbol are via the global offset table.
1874      For such cases we need not do anything here; the relocations will
1875      be handled correctly by relocate_section.  */
1876   if (info->shared)
1877     return TRUE;
1878
1879   /* If there are no references to this symbol that do not use the
1880      GOT, we don't need to generate a copy reloc.  */
1881   if (!eh->non_got_ref)
1882     return TRUE;
1883
1884   if (ELIMINATE_COPY_RELOCS)
1885     {
1886       struct elf32_hppa_link_hash_entry *hh;
1887       struct elf32_hppa_dyn_reloc_entry *hdh_p;
1888
1889       hh = hppa_elf_hash_entry (eh);
1890       for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
1891         {
1892           sec = hdh_p->sec->output_section;
1893           if (sec != NULL && (sec->flags & SEC_READONLY) != 0)
1894             break;
1895         }
1896
1897       /* If we didn't find any dynamic relocs in read-only sections, then
1898          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1899       if (hdh_p == NULL)
1900         {
1901           eh->non_got_ref = 0;
1902           return TRUE;
1903         }
1904     }
1905
1906   if (eh->size == 0)
1907     {
1908       (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1909                              eh->root.root.string);
1910       return TRUE;
1911     }
1912
1913   /* We must allocate the symbol in our .dynbss section, which will
1914      become part of the .bss section of the executable.  There will be
1915      an entry for this symbol in the .dynsym section.  The dynamic
1916      object will contain position independent code, so all references
1917      from the dynamic object to this symbol will go through the global
1918      offset table.  The dynamic linker will use the .dynsym entry to
1919      determine the address it must put in the global offset table, so
1920      both the dynamic object and the regular object will refer to the
1921      same memory location for the variable.  */
1922
1923   htab = hppa_link_hash_table (info);
1924
1925   /* We must generate a COPY reloc to tell the dynamic linker to
1926      copy the initial value out of the dynamic object and into the
1927      runtime process image.  */
1928   if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0)
1929     {
1930       htab->srelbss->size += sizeof (Elf32_External_Rela);
1931       eh->needs_copy = 1;
1932     }
1933
1934   sec = htab->sdynbss;
1935
1936   return _bfd_elf_adjust_dynamic_copy (eh, sec);
1937 }
1938
1939 /* Allocate space in the .plt for entries that won't have relocations.
1940    ie. plabel entries.  */
1941
1942 static bfd_boolean
1943 allocate_plt_static (struct elf_link_hash_entry *eh, void *inf)
1944 {
1945   struct bfd_link_info *info;
1946   struct elf32_hppa_link_hash_table *htab;
1947   struct elf32_hppa_link_hash_entry *hh;
1948   asection *sec;
1949
1950   if (eh->root.type == bfd_link_hash_indirect)
1951     return TRUE;
1952
1953   if (eh->root.type == bfd_link_hash_warning)
1954     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
1955
1956   info = (struct bfd_link_info *) inf;
1957   hh = hppa_elf_hash_entry (eh);
1958   htab = hppa_link_hash_table (info);
1959   if (htab->etab.dynamic_sections_created
1960       && eh->plt.refcount > 0)
1961     {
1962       /* Make sure this symbol is output as a dynamic symbol.
1963          Undefined weak syms won't yet be marked as dynamic.  */
1964       if (eh->dynindx == -1
1965           && !eh->forced_local
1966           && eh->type != STT_PARISC_MILLI)
1967         {
1968           if (! bfd_elf_link_record_dynamic_symbol (info, eh))
1969             return FALSE;
1970         }
1971
1972       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, eh))
1973         {
1974           /* Allocate these later.  From this point on, h->plabel
1975              means that the plt entry is only used by a plabel.
1976              We'll be using a normal plt entry for this symbol, so
1977              clear the plabel indicator.  */
1978           
1979           hh->plabel = 0;
1980         }
1981       else if (hh->plabel)
1982         {
1983           /* Make an entry in the .plt section for plabel references
1984              that won't have a .plt entry for other reasons.  */
1985           sec = htab->splt;
1986           eh->plt.offset = sec->size;
1987           sec->size += PLT_ENTRY_SIZE;
1988         }
1989       else
1990         {
1991           /* No .plt entry needed.  */
1992           eh->plt.offset = (bfd_vma) -1;
1993           eh->needs_plt = 0;
1994         }
1995     }
1996   else
1997     {
1998       eh->plt.offset = (bfd_vma) -1;
1999       eh->needs_plt = 0;
2000     }
2001
2002   return TRUE;
2003 }
2004
2005 /* Allocate space in .plt, .got and associated reloc sections for
2006    global syms.  */
2007
2008 static bfd_boolean
2009 allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2010 {
2011   struct bfd_link_info *info;
2012   struct elf32_hppa_link_hash_table *htab;
2013   asection *sec;
2014   struct elf32_hppa_link_hash_entry *hh;
2015   struct elf32_hppa_dyn_reloc_entry *hdh_p;
2016
2017   if (eh->root.type == bfd_link_hash_indirect)
2018     return TRUE;
2019
2020   if (eh->root.type == bfd_link_hash_warning)
2021     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2022
2023   info = inf;
2024   htab = hppa_link_hash_table (info);
2025   hh = hppa_elf_hash_entry (eh);
2026   
2027   if (htab->etab.dynamic_sections_created
2028       && eh->plt.offset != (bfd_vma) -1
2029       && !hh->plabel
2030       && eh->plt.refcount > 0)
2031     {
2032       /* Make an entry in the .plt section.  */
2033       sec = htab->splt;
2034       eh->plt.offset = sec->size;
2035       sec->size += PLT_ENTRY_SIZE;
2036
2037       /* We also need to make an entry in the .rela.plt section.  */
2038       htab->srelplt->size += sizeof (Elf32_External_Rela);
2039       htab->need_plt_stub = 1;
2040     }
2041
2042   if (eh->got.refcount > 0)
2043     {
2044       /* Make sure this symbol is output as a dynamic symbol.
2045          Undefined weak syms won't yet be marked as dynamic.  */
2046       if (eh->dynindx == -1
2047           && !eh->forced_local
2048           && eh->type != STT_PARISC_MILLI)
2049         {
2050           if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2051             return FALSE;
2052         }
2053
2054       sec = htab->sgot;
2055       eh->got.offset = sec->size;
2056       sec->size += GOT_ENTRY_SIZE;
2057       /* R_PARISC_TLS_GD* needs two GOT entries */
2058       if ((hh->tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
2059         sec->size += GOT_ENTRY_SIZE * 2;
2060       else if ((hh->tls_type & GOT_TLS_GD) == GOT_TLS_GD)
2061         sec->size += GOT_ENTRY_SIZE;
2062       if (htab->etab.dynamic_sections_created
2063           && (info->shared
2064               || (eh->dynindx != -1
2065                   && !eh->forced_local)))
2066         {
2067           htab->srelgot->size += sizeof (Elf32_External_Rela);
2068           if ((hh->tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
2069             htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
2070           else if ((hh->tls_type & GOT_TLS_GD) == GOT_TLS_GD)
2071             htab->srelgot->size += sizeof (Elf32_External_Rela);
2072         }
2073     }
2074   else
2075     eh->got.offset = (bfd_vma) -1;
2076
2077   if (hh->dyn_relocs == NULL)
2078     return TRUE;
2079
2080   /* If this is a -Bsymbolic shared link, then we need to discard all
2081      space allocated for dynamic pc-relative relocs against symbols
2082      defined in a regular object.  For the normal shared case, discard
2083      space for relocs that have become local due to symbol visibility
2084      changes.  */
2085   if (info->shared)
2086     {
2087 #if RELATIVE_DYNRELOCS
2088       if (SYMBOL_CALLS_LOCAL (info, eh))
2089         {
2090           struct elf32_hppa_dyn_reloc_entry **hdh_pp;
2091
2092           for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2093             {
2094               hdh_p->count -= hdh_p->relative_count;
2095               hdh_p->relative_count = 0;
2096               if (hdh_p->count == 0)
2097                 *hdh_pp = hdh_p->hdh_next;
2098               else
2099                 hdh_pp = &hdh_p->hdh_next;
2100             }
2101         }
2102 #endif
2103
2104       /* Also discard relocs on undefined weak syms with non-default
2105          visibility.  */
2106       if (hh->dyn_relocs != NULL
2107           && eh->root.type == bfd_link_hash_undefweak)
2108         {
2109           if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
2110             hh->dyn_relocs = NULL;
2111
2112           /* Make sure undefined weak symbols are output as a dynamic
2113              symbol in PIEs.  */
2114           else if (eh->dynindx == -1
2115                    && !eh->forced_local)
2116             {
2117               if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2118                 return FALSE;
2119             }
2120         }
2121     }
2122   else
2123     {
2124       /* For the non-shared case, discard space for relocs against
2125          symbols which turn out to need copy relocs or are not
2126          dynamic.  */
2127       
2128       if (!eh->non_got_ref
2129           && ((ELIMINATE_COPY_RELOCS
2130                && eh->def_dynamic
2131                && !eh->def_regular)
2132                || (htab->etab.dynamic_sections_created
2133                    && (eh->root.type == bfd_link_hash_undefweak
2134                        || eh->root.type == bfd_link_hash_undefined))))
2135         {
2136           /* Make sure this symbol is output as a dynamic symbol.
2137              Undefined weak syms won't yet be marked as dynamic.  */
2138           if (eh->dynindx == -1
2139               && !eh->forced_local
2140               && eh->type != STT_PARISC_MILLI)
2141             {
2142               if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2143                 return FALSE;
2144             }
2145
2146           /* If that succeeded, we know we'll be keeping all the
2147              relocs.  */
2148           if (eh->dynindx != -1)
2149             goto keep;
2150         }
2151
2152       hh->dyn_relocs = NULL;
2153       return TRUE;
2154
2155     keep: ;
2156     }
2157
2158   /* Finally, allocate space.  */
2159   for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2160     {
2161       asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
2162       sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
2163     }
2164
2165   return TRUE;
2166 }
2167
2168 /* This function is called via elf_link_hash_traverse to force
2169    millicode symbols local so they do not end up as globals in the
2170    dynamic symbol table.  We ought to be able to do this in
2171    adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2172    for all dynamic symbols.  Arguably, this is a bug in
2173    elf_adjust_dynamic_symbol.  */
2174
2175 static bfd_boolean
2176 clobber_millicode_symbols (struct elf_link_hash_entry *eh,
2177                            struct bfd_link_info *info)
2178 {
2179   if (eh->root.type == bfd_link_hash_warning)
2180     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2181
2182   if (eh->type == STT_PARISC_MILLI
2183       && !eh->forced_local)
2184     {
2185       elf32_hppa_hide_symbol (info, eh, TRUE);
2186     }
2187   return TRUE;
2188 }
2189
2190 /* Find any dynamic relocs that apply to read-only sections.  */
2191
2192 static bfd_boolean
2193 readonly_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2194 {
2195   struct elf32_hppa_link_hash_entry *hh;
2196   struct elf32_hppa_dyn_reloc_entry *hdh_p;
2197
2198   if (eh->root.type == bfd_link_hash_warning)
2199     eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
2200
2201   hh = hppa_elf_hash_entry (eh);
2202   for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2203     {
2204       asection *sec = hdh_p->sec->output_section;
2205
2206       if (sec != NULL && (sec->flags & SEC_READONLY) != 0)
2207         {
2208           struct bfd_link_info *info = inf;
2209
2210           info->flags |= DF_TEXTREL;
2211
2212           /* Not an error, just cut short the traversal.  */
2213           return FALSE;
2214         }
2215     }
2216   return TRUE;
2217 }
2218
2219 /* Set the sizes of the dynamic sections.  */
2220
2221 static bfd_boolean
2222 elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2223                                   struct bfd_link_info *info)
2224 {
2225   struct elf32_hppa_link_hash_table *htab;
2226   bfd *dynobj;
2227   bfd *ibfd;
2228   asection *sec;
2229   bfd_boolean relocs;
2230
2231   htab = hppa_link_hash_table (info);
2232   dynobj = htab->etab.dynobj;
2233   if (dynobj == NULL)
2234     abort ();
2235
2236   if (htab->etab.dynamic_sections_created)
2237     {
2238       /* Set the contents of the .interp section to the interpreter.  */
2239       if (info->executable)
2240         {
2241           sec = bfd_get_section_by_name (dynobj, ".interp");
2242           if (sec == NULL)
2243             abort ();
2244           sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
2245           sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2246         }
2247
2248       /* Force millicode symbols local.  */
2249       elf_link_hash_traverse (&htab->etab,
2250                               clobber_millicode_symbols,
2251                               info);
2252     }
2253
2254   /* Set up .got and .plt offsets for local syms, and space for local
2255      dynamic relocs.  */
2256   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2257     {
2258       bfd_signed_vma *local_got;
2259       bfd_signed_vma *end_local_got;
2260       bfd_signed_vma *local_plt;
2261       bfd_signed_vma *end_local_plt;
2262       bfd_size_type locsymcount;
2263       Elf_Internal_Shdr *symtab_hdr;
2264       asection *srel;
2265       char *local_tls_type;
2266
2267       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2268         continue;
2269
2270       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2271         {
2272           struct elf32_hppa_dyn_reloc_entry *hdh_p;
2273
2274           for (hdh_p = ((struct elf32_hppa_dyn_reloc_entry *)
2275                     elf_section_data (sec)->local_dynrel);
2276                hdh_p != NULL;
2277                hdh_p = hdh_p->hdh_next)
2278             {
2279               if (!bfd_is_abs_section (hdh_p->sec)
2280                   && bfd_is_abs_section (hdh_p->sec->output_section))
2281                 {
2282                   /* Input section has been discarded, either because
2283                      it is a copy of a linkonce section or due to
2284                      linker script /DISCARD/, so we'll be discarding
2285                      the relocs too.  */
2286                 }
2287               else if (hdh_p->count != 0)
2288                 {
2289                   srel = elf_section_data (hdh_p->sec)->sreloc;
2290                   srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
2291                   if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
2292                     info->flags |= DF_TEXTREL;
2293                 }
2294             }
2295         }
2296
2297       local_got = elf_local_got_refcounts (ibfd);
2298       if (!local_got)
2299         continue;
2300
2301       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2302       locsymcount = symtab_hdr->sh_info;
2303       end_local_got = local_got + locsymcount;
2304       local_tls_type = hppa_elf_local_got_tls_type (ibfd);
2305       sec = htab->sgot;
2306       srel = htab->srelgot;
2307       for (; local_got < end_local_got; ++local_got)
2308         {
2309           if (*local_got > 0)
2310             {
2311               *local_got = sec->size;
2312               sec->size += GOT_ENTRY_SIZE;
2313               if ((*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
2314                 sec->size += 2 * GOT_ENTRY_SIZE;
2315               else if ((*local_tls_type & GOT_TLS_GD) == GOT_TLS_GD)
2316                 sec->size += GOT_ENTRY_SIZE;
2317               if (info->shared) 
2318                 {
2319                   srel->size += sizeof (Elf32_External_Rela);
2320                   if ((*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
2321                     srel->size += 2 * sizeof (Elf32_External_Rela);
2322                   else if ((*local_tls_type & GOT_TLS_GD) == GOT_TLS_GD)
2323                     srel->size += sizeof (Elf32_External_Rela);
2324                 }
2325             }
2326           else
2327             *local_got = (bfd_vma) -1;
2328
2329           ++local_tls_type;
2330         }
2331
2332       local_plt = end_local_got;
2333       end_local_plt = local_plt + locsymcount;
2334       if (! htab->etab.dynamic_sections_created)
2335         {
2336           /* Won't be used, but be safe.  */
2337           for (; local_plt < end_local_plt; ++local_plt)
2338             *local_plt = (bfd_vma) -1;
2339         }
2340       else
2341         {
2342           sec = htab->splt;
2343           srel = htab->srelplt;
2344           for (; local_plt < end_local_plt; ++local_plt)
2345             {
2346               if (*local_plt > 0)
2347                 {
2348                   *local_plt = sec->size;
2349                   sec->size += PLT_ENTRY_SIZE;
2350                   if (info->shared)
2351                     srel->size += sizeof (Elf32_External_Rela);
2352                 }
2353               else
2354                 *local_plt = (bfd_vma) -1;
2355             }
2356         }
2357     }
2358   
2359   if (htab->tls_ldm_got.refcount > 0)
2360     {
2361       /* Allocate 2 got entries and 1 dynamic reloc for 
2362          R_PARISC_TLS_DTPMOD32 relocs.  */
2363       htab->tls_ldm_got.offset = htab->sgot->size;
2364       htab->sgot->size += (GOT_ENTRY_SIZE * 2);
2365       htab->srelgot->size += sizeof (Elf32_External_Rela);
2366     }
2367   else
2368     htab->tls_ldm_got.offset = -1;
2369
2370   /* Do all the .plt entries without relocs first.  The dynamic linker
2371      uses the last .plt reloc to find the end of the .plt (and hence
2372      the start of the .got) for lazy linking.  */
2373   elf_link_hash_traverse (&htab->etab, allocate_plt_static, info);
2374
2375   /* Allocate global sym .plt and .got entries, and space for global
2376      sym dynamic relocs.  */
2377   elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
2378
2379   /* The check_relocs and adjust_dynamic_symbol entry points have
2380      determined the sizes of the various dynamic sections.  Allocate
2381      memory for them.  */
2382   relocs = FALSE;
2383   for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2384     {
2385       if ((sec->flags & SEC_LINKER_CREATED) == 0)
2386         continue;
2387
2388       if (sec == htab->splt)
2389         {
2390           if (htab->need_plt_stub)
2391             {
2392               /* Make space for the plt stub at the end of the .plt
2393                  section.  We want this stub right at the end, up
2394                  against the .got section.  */
2395               int gotalign = bfd_section_alignment (dynobj, htab->sgot);
2396               int pltalign = bfd_section_alignment (dynobj, sec);
2397               bfd_size_type mask;
2398
2399               if (gotalign > pltalign)
2400                 bfd_set_section_alignment (dynobj, sec, gotalign);
2401               mask = ((bfd_size_type) 1 << gotalign) - 1;
2402               sec->size = (sec->size + sizeof (plt_stub) + mask) & ~mask;
2403             }
2404         }
2405       else if (sec == htab->sgot
2406                || sec == htab->sdynbss)
2407         ;
2408       else if (CONST_STRNEQ (bfd_get_section_name (dynobj, sec), ".rela"))
2409         {
2410           if (sec->size != 0)
2411             {
2412               /* Remember whether there are any reloc sections other
2413                  than .rela.plt.  */
2414               if (sec != htab->srelplt)
2415                 relocs = TRUE;
2416
2417               /* We use the reloc_count field as a counter if we need
2418                  to copy relocs into the output file.  */
2419               sec->reloc_count = 0;
2420             }
2421         }
2422       else
2423         {
2424           /* It's not one of our sections, so don't allocate space.  */
2425           continue;
2426         }
2427
2428       if (sec->size == 0)
2429         {
2430           /* If we don't need this section, strip it from the
2431              output file.  This is mostly to handle .rela.bss and
2432              .rela.plt.  We must create both sections in
2433              create_dynamic_sections, because they must be created
2434              before the linker maps input sections to output
2435              sections.  The linker does that before
2436              adjust_dynamic_symbol is called, and it is that
2437              function which decides whether anything needs to go
2438              into these sections.  */
2439           sec->flags |= SEC_EXCLUDE;
2440           continue;
2441         }
2442
2443       if ((sec->flags & SEC_HAS_CONTENTS) == 0)
2444         continue;
2445
2446       /* Allocate memory for the section contents.  Zero it, because
2447          we may not fill in all the reloc sections.  */
2448       sec->contents = bfd_zalloc (dynobj, sec->size);
2449       if (sec->contents == NULL)
2450         return FALSE;
2451     }
2452
2453   if (htab->etab.dynamic_sections_created)
2454     {
2455       /* Like IA-64 and HPPA64, always create a DT_PLTGOT.  It
2456          actually has nothing to do with the PLT, it is how we
2457          communicate the LTP value of a load module to the dynamic
2458          linker.  */
2459 #define add_dynamic_entry(TAG, VAL) \
2460   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2461
2462       if (!add_dynamic_entry (DT_PLTGOT, 0))
2463         return FALSE;
2464
2465       /* Add some entries to the .dynamic section.  We fill in the
2466          values later, in elf32_hppa_finish_dynamic_sections, but we
2467          must add the entries now so that we get the correct size for
2468          the .dynamic section.  The DT_DEBUG entry is filled in by the
2469          dynamic linker and used by the debugger.  */
2470       if (info->executable)
2471         {
2472           if (!add_dynamic_entry (DT_DEBUG, 0))
2473             return FALSE;
2474         }
2475
2476       if (htab->srelplt->size != 0)
2477         {
2478           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2479               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2480               || !add_dynamic_entry (DT_JMPREL, 0))
2481             return FALSE;
2482         }
2483
2484       if (relocs)
2485         {
2486           if (!add_dynamic_entry (DT_RELA, 0)
2487               || !add_dynamic_entry (DT_RELASZ, 0)
2488               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2489             return FALSE;
2490
2491           /* If any dynamic relocs apply to a read-only section,
2492              then we need a DT_TEXTREL entry.  */
2493           if ((info->flags & DF_TEXTREL) == 0)
2494             elf_link_hash_traverse (&htab->etab, readonly_dynrelocs, info);
2495
2496           if ((info->flags & DF_TEXTREL) != 0)
2497             {
2498               if (!add_dynamic_entry (DT_TEXTREL, 0))
2499                 return FALSE;
2500             }
2501         }
2502     }
2503 #undef add_dynamic_entry
2504
2505   return TRUE;
2506 }
2507
2508 /* External entry points for sizing and building linker stubs.  */
2509
2510 /* Set up various things so that we can make a list of input sections
2511    for each output section included in the link.  Returns -1 on error,
2512    0 when no stubs will be needed, and 1 on success.  */
2513
2514 int
2515 elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2516 {
2517   bfd *input_bfd;
2518   unsigned int bfd_count;
2519   int top_id, top_index;
2520   asection *section;
2521   asection **input_list, **list;
2522   bfd_size_type amt;
2523   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2524
2525   /* Count the number of input BFDs and find the top input section id.  */
2526   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2527        input_bfd != NULL;
2528        input_bfd = input_bfd->link_next)
2529     {
2530       bfd_count += 1;
2531       for (section = input_bfd->sections;
2532            section != NULL;
2533            section = section->next)
2534         {
2535           if (top_id < section->id)
2536             top_id = section->id;
2537         }
2538     }
2539   htab->bfd_count = bfd_count;
2540
2541   amt = sizeof (struct map_stub) * (top_id + 1);
2542   htab->stub_group = bfd_zmalloc (amt);
2543   if (htab->stub_group == NULL)
2544     return -1;
2545
2546   /* We can't use output_bfd->section_count here to find the top output
2547      section index as some sections may have been removed, and
2548      strip_excluded_output_sections doesn't renumber the indices.  */
2549   for (section = output_bfd->sections, top_index = 0;
2550        section != NULL;
2551        section = section->next)
2552     {
2553       if (top_index < section->index)
2554         top_index = section->index;
2555     }
2556
2557   htab->top_index = top_index;
2558   amt = sizeof (asection *) * (top_index + 1);
2559   input_list = bfd_malloc (amt);
2560   htab->input_list = input_list;
2561   if (input_list == NULL)
2562     return -1;
2563
2564   /* For sections we aren't interested in, mark their entries with a
2565      value we can check later.  */
2566   list = input_list + top_index;
2567   do
2568     *list = bfd_abs_section_ptr;
2569   while (list-- != input_list);
2570
2571   for (section = output_bfd->sections;
2572        section != NULL;
2573        section = section->next)
2574     {
2575       if ((section->flags & SEC_CODE) != 0)
2576         input_list[section->index] = NULL;
2577     }
2578
2579   return 1;
2580 }
2581
2582 /* The linker repeatedly calls this function for each input section,
2583    in the order that input sections are linked into output sections.
2584    Build lists of input sections to determine groupings between which
2585    we may insert linker stubs.  */
2586
2587 void
2588 elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
2589 {
2590   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2591
2592   if (isec->output_section->index <= htab->top_index)
2593     {
2594       asection **list = htab->input_list + isec->output_section->index;
2595       if (*list != bfd_abs_section_ptr)
2596         {
2597           /* Steal the link_sec pointer for our list.  */
2598 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2599           /* This happens to make the list in reverse order,
2600              which is what we want.  */
2601           PREV_SEC (isec) = *list;
2602           *list = isec;
2603         }
2604     }
2605 }
2606
2607 /* See whether we can group stub sections together.  Grouping stub
2608    sections may result in fewer stubs.  More importantly, we need to
2609    put all .init* and .fini* stubs at the beginning of the .init or
2610    .fini output sections respectively, because glibc splits the
2611    _init and _fini functions into multiple parts.  Putting a stub in
2612    the middle of a function is not a good idea.  */
2613
2614 static void
2615 group_sections (struct elf32_hppa_link_hash_table *htab,
2616                 bfd_size_type stub_group_size,
2617                 bfd_boolean stubs_always_before_branch)
2618 {
2619   asection **list = htab->input_list + htab->top_index;
2620   do
2621     {
2622       asection *tail = *list;
2623       if (tail == bfd_abs_section_ptr)
2624         continue;
2625       while (tail != NULL)
2626         {
2627           asection *curr;
2628           asection *prev;
2629           bfd_size_type total;
2630           bfd_boolean big_sec;
2631
2632           curr = tail;
2633           total = tail->size;
2634           big_sec = total >= stub_group_size;
2635
2636           while ((prev = PREV_SEC (curr)) != NULL
2637                  && ((total += curr->output_offset - prev->output_offset)
2638                      < stub_group_size))
2639             curr = prev;
2640
2641           /* OK, the size from the start of CURR to the end is less
2642              than 240000 bytes and thus can be handled by one stub
2643              section.  (or the tail section is itself larger than
2644              240000 bytes, in which case we may be toast.)
2645              We should really be keeping track of the total size of
2646              stubs added here, as stubs contribute to the final output
2647              section size.  That's a little tricky, and this way will
2648              only break if stubs added total more than 22144 bytes, or
2649              2768 long branch stubs.  It seems unlikely for more than
2650              2768 different functions to be called, especially from
2651              code only 240000 bytes long.  This limit used to be
2652              250000, but c++ code tends to generate lots of little
2653              functions, and sometimes violated the assumption.  */
2654           do
2655             {
2656               prev = PREV_SEC (tail);
2657               /* Set up this stub group.  */
2658               htab->stub_group[tail->id].link_sec = curr;
2659             }
2660           while (tail != curr && (tail = prev) != NULL);
2661
2662           /* But wait, there's more!  Input sections up to 240000
2663              bytes before the stub section can be handled by it too.
2664              Don't do this if we have a really large section after the
2665              stubs, as adding more stubs increases the chance that
2666              branches may not reach into the stub section.  */
2667           if (!stubs_always_before_branch && !big_sec)
2668             {
2669               total = 0;
2670               while (prev != NULL
2671                      && ((total += tail->output_offset - prev->output_offset)
2672                          < stub_group_size))
2673                 {
2674                   tail = prev;
2675                   prev = PREV_SEC (tail);
2676                   htab->stub_group[tail->id].link_sec = curr;
2677                 }
2678             }
2679           tail = prev;
2680         }
2681     }
2682   while (list-- != htab->input_list);
2683   free (htab->input_list);
2684 #undef PREV_SEC
2685 }
2686
2687 /* Read in all local syms for all input bfds, and create hash entries
2688    for export stubs if we are building a multi-subspace shared lib.
2689    Returns -1 on error, 1 if export stubs created, 0 otherwise.  */
2690
2691 static int
2692 get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
2693 {
2694   unsigned int bfd_indx;
2695   Elf_Internal_Sym *local_syms, **all_local_syms;
2696   int stub_changed = 0;
2697   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2698
2699   /* We want to read in symbol extension records only once.  To do this
2700      we need to read in the local symbols in parallel and save them for
2701      later use; so hold pointers to the local symbols in an array.  */
2702   bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2703   all_local_syms = bfd_zmalloc (amt);
2704   htab->all_local_syms = all_local_syms;
2705   if (all_local_syms == NULL)
2706     return -1;
2707
2708   /* Walk over all the input BFDs, swapping in local symbols.
2709      If we are creating a shared library, create hash entries for the
2710      export stubs.  */
2711   for (bfd_indx = 0;
2712        input_bfd != NULL;
2713        input_bfd = input_bfd->link_next, bfd_indx++)
2714     {
2715       Elf_Internal_Shdr *symtab_hdr;
2716
2717       /* We'll need the symbol table in a second.  */
2718       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2719       if (symtab_hdr->sh_info == 0)
2720         continue;
2721
2722       /* We need an array of the local symbols attached to the input bfd.  */
2723       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2724       if (local_syms == NULL)
2725         {
2726           local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2727                                              symtab_hdr->sh_info, 0,
2728                                              NULL, NULL, NULL);
2729           /* Cache them for elf_link_input_bfd.  */
2730           symtab_hdr->contents = (unsigned char *) local_syms;
2731         }
2732       if (local_syms == NULL)
2733         return -1;
2734
2735       all_local_syms[bfd_indx] = local_syms;
2736
2737       if (info->shared && htab->multi_subspace)
2738         {
2739           struct elf_link_hash_entry **eh_syms;
2740           struct elf_link_hash_entry **eh_symend;
2741           unsigned int symcount;
2742
2743           symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2744                       - symtab_hdr->sh_info);
2745           eh_syms = (struct elf_link_hash_entry **) elf_sym_hashes (input_bfd);
2746           eh_symend = (struct elf_link_hash_entry **) (eh_syms + symcount);
2747
2748           /* Look through the global syms for functions;  We need to
2749              build export stubs for all globally visible functions.  */
2750           for (; eh_syms < eh_symend; eh_syms++)
2751             {
2752               struct elf32_hppa_link_hash_entry *hh;
2753
2754               hh = hppa_elf_hash_entry (*eh_syms);
2755
2756               while (hh->eh.root.type == bfd_link_hash_indirect
2757                      || hh->eh.root.type == bfd_link_hash_warning)
2758                    hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
2759
2760               /* At this point in the link, undefined syms have been
2761                  resolved, so we need to check that the symbol was
2762                  defined in this BFD.  */
2763               if ((hh->eh.root.type == bfd_link_hash_defined
2764                    || hh->eh.root.type == bfd_link_hash_defweak)
2765                   && hh->eh.type == STT_FUNC
2766                   && hh->eh.root.u.def.section->output_section != NULL
2767                   && (hh->eh.root.u.def.section->output_section->owner
2768                       == output_bfd)
2769                   && hh->eh.root.u.def.section->owner == input_bfd
2770                   && hh->eh.def_regular
2771                   && !hh->eh.forced_local
2772                   && ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT)
2773                 {
2774                   asection *sec;
2775                   const char *stub_name;
2776                   struct elf32_hppa_stub_hash_entry *hsh;
2777
2778                   sec = hh->eh.root.u.def.section;
2779                   stub_name = hh_name (hh);
2780                   hsh = hppa_stub_hash_lookup (&htab->bstab,
2781                                                       stub_name,
2782                                                       FALSE, FALSE);
2783                   if (hsh == NULL)
2784                     {
2785                       hsh = hppa_add_stub (stub_name, sec, htab);
2786                       if (!hsh)
2787                         return -1;
2788
2789                       hsh->target_value = hh->eh.root.u.def.value;
2790                       hsh->target_section = hh->eh.root.u.def.section;
2791                       hsh->stub_type = hppa_stub_export;
2792                       hsh->hh = hh;
2793                       stub_changed = 1;
2794                     }
2795                   else
2796                     {
2797                       (*_bfd_error_handler) (_("%B: duplicate export stub %s"),
2798                                              input_bfd,
2799                                              stub_name);
2800                     }
2801                 }
2802             }
2803         }
2804     }
2805
2806   return stub_changed;
2807 }
2808
2809 /* Determine and set the size of the stub section for a final link.
2810
2811    The basic idea here is to examine all the relocations looking for
2812    PC-relative calls to a target that is unreachable with a "bl"
2813    instruction.  */
2814
2815 bfd_boolean
2816 elf32_hppa_size_stubs
2817   (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2818    bfd_boolean multi_subspace, bfd_signed_vma group_size,
2819    asection * (*add_stub_section) (const char *, asection *),
2820    void (*layout_sections_again) (void))
2821 {
2822   bfd_size_type stub_group_size;
2823   bfd_boolean stubs_always_before_branch;
2824   bfd_boolean stub_changed;
2825   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2826
2827   /* Stash our params away.  */
2828   htab->stub_bfd = stub_bfd;
2829   htab->multi_subspace = multi_subspace;
2830   htab->add_stub_section = add_stub_section;
2831   htab->layout_sections_again = layout_sections_again;
2832   stubs_always_before_branch = group_size < 0;
2833   if (group_size < 0)
2834     stub_group_size = -group_size;
2835   else
2836     stub_group_size = group_size;
2837   if (stub_group_size == 1)
2838     {
2839       /* Default values.  */
2840       if (stubs_always_before_branch)
2841         {
2842           stub_group_size = 7680000;
2843           if (htab->has_17bit_branch || htab->multi_subspace)
2844             stub_group_size = 240000;
2845           if (htab->has_12bit_branch)
2846             stub_group_size = 7500;
2847         }
2848       else
2849         {
2850           stub_group_size = 6971392;
2851           if (htab->has_17bit_branch || htab->multi_subspace)
2852             stub_group_size = 217856;
2853           if (htab->has_12bit_branch)
2854             stub_group_size = 6808;
2855         }
2856     }
2857
2858   group_sections (htab, stub_group_size, stubs_always_before_branch);
2859
2860   switch (get_local_syms (output_bfd, info->input_bfds, info))
2861     {
2862     default:
2863       if (htab->all_local_syms)
2864         goto error_ret_free_local;
2865       return FALSE;
2866
2867     case 0:
2868       stub_changed = FALSE;
2869       break;
2870
2871     case 1:
2872       stub_changed = TRUE;
2873       break;
2874     }
2875
2876   while (1)
2877     {
2878       bfd *input_bfd;
2879       unsigned int bfd_indx;
2880       asection *stub_sec;
2881
2882       for (input_bfd = info->input_bfds, bfd_indx = 0;
2883            input_bfd != NULL;
2884            input_bfd = input_bfd->link_next, bfd_indx++)
2885         {
2886           Elf_Internal_Shdr *symtab_hdr;
2887           asection *section;
2888           Elf_Internal_Sym *local_syms;
2889
2890           /* We'll need the symbol table in a second.  */
2891           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2892           if (symtab_hdr->sh_info == 0)
2893             continue;
2894
2895           local_syms = htab->all_local_syms[bfd_indx];
2896
2897           /* Walk over each section attached to the input bfd.  */
2898           for (section = input_bfd->sections;
2899                section != NULL;
2900                section = section->next)
2901             {
2902               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2903
2904               /* If there aren't any relocs, then there's nothing more
2905                  to do.  */
2906               if ((section->flags & SEC_RELOC) == 0
2907                   || section->reloc_count == 0)
2908                 continue;
2909
2910               /* If this section is a link-once section that will be
2911                  discarded, then don't create any stubs.  */
2912               if (section->output_section == NULL
2913                   || section->output_section->owner != output_bfd)
2914                 continue;
2915
2916               /* Get the relocs.  */
2917               internal_relocs
2918                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2919                                              info->keep_memory);
2920               if (internal_relocs == NULL)
2921                 goto error_ret_free_local;
2922
2923               /* Now examine each relocation.  */
2924               irela = internal_relocs;
2925               irelaend = irela + section->reloc_count;
2926               for (; irela < irelaend; irela++)
2927                 {
2928                   unsigned int r_type, r_indx;
2929                   enum elf32_hppa_stub_type stub_type;
2930                   struct elf32_hppa_stub_hash_entry *hsh;
2931                   asection *sym_sec;
2932                   bfd_vma sym_value;
2933                   bfd_vma destination;
2934                   struct elf32_hppa_link_hash_entry *hh;
2935                   char *stub_name;
2936                   const asection *id_sec;
2937
2938                   r_type = ELF32_R_TYPE (irela->r_info);
2939                   r_indx = ELF32_R_SYM (irela->r_info);
2940
2941                   if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2942                     {
2943                       bfd_set_error (bfd_error_bad_value);
2944                     error_ret_free_internal:
2945                       if (elf_section_data (section)->relocs == NULL)
2946                         free (internal_relocs);
2947                       goto error_ret_free_local;
2948                     }
2949
2950                   /* Only look for stubs on call instructions.  */
2951                   if (r_type != (unsigned int) R_PARISC_PCREL12F
2952                       && r_type != (unsigned int) R_PARISC_PCREL17F
2953                       && r_type != (unsigned int) R_PARISC_PCREL22F)
2954                     continue;
2955
2956                   /* Now determine the call target, its name, value,
2957                      section.  */
2958                   sym_sec = NULL;
2959                   sym_value = 0;
2960                   destination = 0;
2961                   hh = NULL;
2962                   if (r_indx < symtab_hdr->sh_info)
2963                     {
2964                       /* It's a local symbol.  */
2965                       Elf_Internal_Sym *sym;
2966                       Elf_Internal_Shdr *hdr;
2967
2968                       sym = local_syms + r_indx;
2969                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2970                       sym_sec = hdr->bfd_section;
2971                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2972                         sym_value = sym->st_value;
2973                       destination = (sym_value + irela->r_addend
2974                                      + sym_sec->output_offset
2975                                      + sym_sec->output_section->vma);
2976                     }
2977                   else
2978                     {
2979                       /* It's an external symbol.  */
2980                       int e_indx;
2981
2982                       e_indx = r_indx - symtab_hdr->sh_info;
2983                       hh = hppa_elf_hash_entry (elf_sym_hashes (input_bfd)[e_indx]);
2984
2985                       while (hh->eh.root.type == bfd_link_hash_indirect
2986                              || hh->eh.root.type == bfd_link_hash_warning)
2987                         hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
2988
2989                       if (hh->eh.root.type == bfd_link_hash_defined
2990                           || hh->eh.root.type == bfd_link_hash_defweak)
2991                         {
2992                           sym_sec = hh->eh.root.u.def.section;
2993                           sym_value = hh->eh.root.u.def.value;
2994                           if (sym_sec->output_section != NULL)
2995                             destination = (sym_value + irela->r_addend
2996                                            + sym_sec->output_offset
2997                                            + sym_sec->output_section->vma);
2998                         }
2999                       else if (hh->eh.root.type == bfd_link_hash_undefweak)
3000                         {
3001                           if (! info->shared)
3002                             continue;
3003                         }
3004                       else if (hh->eh.root.type == bfd_link_hash_undefined)
3005                         {
3006                           if (! (info->unresolved_syms_in_objects == RM_IGNORE
3007                                  && (ELF_ST_VISIBILITY (hh->eh.other)
3008                                      == STV_DEFAULT)
3009                                  && hh->eh.type != STT_PARISC_MILLI))
3010                             continue;
3011                         }
3012                       else
3013                         {
3014                           bfd_set_error (bfd_error_bad_value);
3015                           goto error_ret_free_internal;
3016                         }
3017                     }
3018
3019                   /* Determine what (if any) linker stub is needed.  */
3020                   stub_type = hppa_type_of_stub (section, irela, hh,
3021                                                  destination, info);
3022                   if (stub_type == hppa_stub_none)
3023                     continue;
3024
3025                   /* Support for grouping stub sections.  */
3026                   id_sec = htab->stub_group[section->id].link_sec;
3027
3028                   /* Get the name of this stub.  */
3029                   stub_name = hppa_stub_name (id_sec, sym_sec, hh, irela);
3030                   if (!stub_name)
3031                     goto error_ret_free_internal;
3032
3033                   hsh = hppa_stub_hash_lookup (&htab->bstab,
3034                                                       stub_name,
3035                                                       FALSE, FALSE);
3036                   if (hsh != NULL)
3037                     {
3038                       /* The proper stub has already been created.  */
3039                       free (stub_name);
3040                       continue;
3041                     }
3042
3043                   hsh = hppa_add_stub (stub_name, section, htab);
3044                   if (hsh == NULL)
3045                     {
3046                       free (stub_name);
3047                       goto error_ret_free_internal;
3048                     }
3049
3050                   hsh->target_value = sym_value;
3051                   hsh->target_section = sym_sec;
3052                   hsh->stub_type = stub_type;
3053                   if (info->shared)
3054                     {
3055                       if (stub_type == hppa_stub_import)
3056                         hsh->stub_type = hppa_stub_import_shared;
3057                       else if (stub_type == hppa_stub_long_branch)
3058                         hsh->stub_type = hppa_stub_long_branch_shared;
3059                     }
3060                   hsh->hh = hh;
3061                   stub_changed = TRUE;
3062                 }
3063
3064               /* We're done with the internal relocs, free them.  */
3065               if (elf_section_data (section)->relocs == NULL)
3066                 free (internal_relocs);
3067             }
3068         }
3069
3070       if (!stub_changed)
3071         break;
3072
3073       /* OK, we've added some stubs.  Find out the new size of the
3074          stub sections.  */
3075       for (stub_sec = htab->stub_bfd->sections;
3076            stub_sec != NULL;
3077            stub_sec = stub_sec->next)
3078         stub_sec->size = 0;
3079
3080       bfd_hash_traverse (&htab->bstab, hppa_size_one_stub, htab);
3081
3082       /* Ask the linker to do its stuff.  */
3083       (*htab->layout_sections_again) ();
3084       stub_changed = FALSE;
3085     }
3086
3087   free (htab->all_local_syms);
3088   return TRUE;
3089
3090  error_ret_free_local:
3091   free (htab->all_local_syms);
3092   return FALSE;
3093 }
3094
3095 /* For a final link, this function is called after we have sized the
3096    stubs to provide a value for __gp.  */
3097
3098 bfd_boolean
3099 elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
3100 {
3101   struct bfd_link_hash_entry *h;
3102   asection *sec = NULL;
3103   bfd_vma gp_val = 0;
3104   struct elf32_hppa_link_hash_table *htab;
3105
3106   htab = hppa_link_hash_table (info);
3107   h = bfd_link_hash_lookup (&htab->etab.root, "$global$", FALSE, FALSE, FALSE);
3108
3109   if (h != NULL
3110       && (h->type == bfd_link_hash_defined
3111           || h->type == bfd_link_hash_defweak))
3112     {
3113       gp_val = h->u.def.value;
3114       sec = h->u.def.section;
3115     }
3116   else
3117     {
3118       asection *splt = bfd_get_section_by_name (abfd, ".plt");
3119       asection *sgot = bfd_get_section_by_name (abfd, ".got");
3120
3121       /* Choose to point our LTP at, in this order, one of .plt, .got,
3122          or .data, if these sections exist.  In the case of choosing
3123          .plt try to make the LTP ideal for addressing anywhere in the
3124          .plt or .got with a 14 bit signed offset.  Typically, the end
3125          of the .plt is the start of the .got, so choose .plt + 0x2000
3126          if either the .plt or .got is larger than 0x2000.  If both
3127          the .plt and .got are smaller than 0x2000, choose the end of
3128          the .plt section.  */
3129       sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
3130           ? NULL : splt;
3131       if (sec != NULL)
3132         {
3133           gp_val = sec->size;
3134           if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
3135             {
3136               gp_val = 0x2000;
3137             }
3138         }
3139       else
3140         {
3141           sec = sgot;
3142           if (sec != NULL)
3143             {
3144               if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
3145                 {
3146                   /* We know we don't have a .plt.  If .got is large,
3147                      offset our LTP.  */
3148                   if (sec->size > 0x2000)
3149                     gp_val = 0x2000;
3150                 }
3151             }
3152           else
3153             {
3154               /* No .plt or .got.  Who cares what the LTP is?  */
3155               sec = bfd_get_section_by_name (abfd, ".data");
3156             }
3157         }
3158
3159       if (h != NULL)
3160         {
3161           h->type = bfd_link_hash_defined;
3162           h->u.def.value = gp_val;
3163           if (sec != NULL)
3164             h->u.def.section = sec;
3165           else
3166             h->u.def.section = bfd_abs_section_ptr;
3167         }
3168     }
3169
3170   if (sec != NULL && sec->output_section != NULL)
3171     gp_val += sec->output_section->vma + sec->output_offset;
3172
3173   elf_gp (abfd) = gp_val;
3174   return TRUE;
3175 }
3176
3177 /* Build all the stubs associated with the current output file.  The
3178    stubs are kept in a hash table attached to the main linker hash
3179    table.  We also set up the .plt entries for statically linked PIC
3180    functions here.  This function is called via hppaelf_finish in the
3181    linker.  */
3182
3183 bfd_boolean
3184 elf32_hppa_build_stubs (struct bfd_link_info *info)
3185 {
3186   asection *stub_sec;
3187   struct bfd_hash_table *table;
3188   struct elf32_hppa_link_hash_table *htab;
3189
3190   htab = hppa_link_hash_table (info);
3191
3192   for (stub_sec = htab->stub_bfd->sections;
3193        stub_sec != NULL;
3194        stub_sec = stub_sec->next)
3195     {
3196       bfd_size_type size;
3197
3198       /* Allocate memory to hold the linker stubs.  */
3199       size = stub_sec->size;
3200       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3201       if (stub_sec->contents == NULL && size != 0)
3202         return FALSE;
3203       stub_sec->size = 0;
3204     }
3205
3206   /* Build the stubs as directed by the stub hash table.  */
3207   table = &htab->bstab;
3208   bfd_hash_traverse (table, hppa_build_one_stub, info);
3209
3210   return TRUE;
3211 }
3212
3213 /* Return the base vma address which should be subtracted from the real
3214    address when resolving a dtpoff relocation.  
3215    This is PT_TLS segment p_vaddr.  */
3216
3217 static bfd_vma
3218 dtpoff_base (struct bfd_link_info *info)
3219 {
3220   /* If tls_sec is NULL, we should have signalled an error already.  */
3221   if (elf_hash_table (info)->tls_sec == NULL)
3222     return 0;
3223   return elf_hash_table (info)->tls_sec->vma;
3224 }
3225
3226 /* Return the relocation value for R_PARISC_TLS_TPOFF*..  */
3227
3228 static bfd_vma
3229 tpoff (struct bfd_link_info *info, bfd_vma address)
3230 {
3231   struct elf_link_hash_table *htab = elf_hash_table (info);
3232
3233   /* If tls_sec is NULL, we should have signalled an error already.  */
3234   if (htab->tls_sec == NULL)
3235     return 0;
3236   /* hppa TLS ABI is variant I and static TLS block start just after 
3237      tcbhead structure which has 2 pointer fields.  */
3238   return (address - htab->tls_sec->vma 
3239           + align_power ((bfd_vma) 8, htab->tls_sec->alignment_power));
3240 }
3241
3242 /* Perform a final link.  */
3243
3244 static bfd_boolean
3245 elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
3246 {
3247   /* Invoke the regular ELF linker to do all the work.  */
3248   if (!bfd_elf_final_link (abfd, info))
3249     return FALSE;
3250
3251   /* If we're producing a final executable, sort the contents of the
3252      unwind section.  */
3253   return elf_hppa_sort_unwind (abfd);
3254 }
3255
3256 /* Record the lowest address for the data and text segments.  */
3257
3258 static void
3259 hppa_record_segment_addr (bfd *abfd ATTRIBUTE_UNUSED,
3260                           asection *section,
3261                           void *data)
3262 {
3263   struct elf32_hppa_link_hash_table *htab;
3264
3265   htab = (struct elf32_hppa_link_hash_table*) data;
3266
3267   if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3268     {
3269       bfd_vma value = section->vma - section->filepos;
3270
3271       if ((section->flags & SEC_READONLY) != 0)
3272         {
3273           if (value < htab->text_segment_base)
3274             htab->text_segment_base = value;
3275         }
3276       else
3277         {
3278           if (value < htab->data_segment_base)
3279             htab->data_segment_base = value;
3280         }
3281     }
3282 }
3283
3284 /* Perform a relocation as part of a final link.  */
3285
3286 static bfd_reloc_status_type
3287 final_link_relocate (asection *input_section,
3288                      bfd_byte *contents,
3289                      const Elf_Internal_Rela *rela,
3290                      bfd_vma value,
3291                      struct elf32_hppa_link_hash_table *htab,
3292                      asection *sym_sec,
3293                      struct elf32_hppa_link_hash_entry *hh,
3294                      struct bfd_link_info *info)
3295 {
3296   int insn;
3297   unsigned int r_type = ELF32_R_TYPE (rela->r_info);
3298   unsigned int orig_r_type = r_type;
3299   reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3300   int r_format = howto->bitsize;
3301   enum hppa_reloc_field_selector_type_alt r_field;
3302   bfd *input_bfd = input_section->owner;
3303   bfd_vma offset = rela->r_offset;
3304   bfd_vma max_branch_offset = 0;
3305   bfd_byte *hit_data = contents + offset;
3306   bfd_signed_vma addend = rela->r_addend;
3307   bfd_vma location;
3308   struct elf32_hppa_stub_hash_entry *hsh = NULL;
3309   int val;  
3310
3311   if (r_type == R_PARISC_NONE)
3312     return bfd_reloc_ok;
3313
3314   insn = bfd_get_32 (input_bfd, hit_data);
3315
3316   /* Find out where we are and where we're going.  */
3317   location = (offset +
3318               input_section->output_offset +
3319               input_section->output_section->vma);
3320
3321   /* If we are not building a shared library, convert DLTIND relocs to
3322      DPREL relocs.  */
3323   if (!info->shared)
3324     {
3325       switch (r_type)
3326         {
3327           case R_PARISC_DLTIND21L:
3328             r_type = R_PARISC_DPREL21L;
3329             break;
3330
3331           case R_PARISC_DLTIND14R:
3332             r_type = R_PARISC_DPREL14R;
3333             break;
3334
3335           case R_PARISC_DLTIND14F:
3336             r_type = R_PARISC_DPREL14F;
3337             break;
3338         }
3339     }
3340
3341   switch (r_type)
3342     {
3343     case R_PARISC_PCREL12F:
3344     case R_PARISC_PCREL17F:
3345     case R_PARISC_PCREL22F:
3346       /* If this call should go via the plt, find the import stub in
3347          the stub hash.  */
3348       if (sym_sec == NULL
3349           || sym_sec->output_section == NULL
3350           || (hh != NULL
3351               && hh->eh.plt.offset != (bfd_vma) -1
3352               && hh->eh.dynindx != -1
3353               && !hh->plabel
3354               && (info->shared
3355                   || !hh->eh.def_regular
3356                   || hh->eh.root.type == bfd_link_hash_defweak)))
3357         {
3358           hsh = hppa_get_stub_entry (input_section, sym_sec,
3359                                             hh, rela, htab);
3360           if (hsh != NULL)
3361             {
3362               value = (hsh->stub_offset
3363                        + hsh->stub_sec->output_offset
3364                        + hsh->stub_sec->output_section->vma);
3365               addend = 0;
3366             }
3367           else if (sym_sec == NULL && hh != NULL
3368                    && hh->eh.root.type == bfd_link_hash_undefweak)
3369             {
3370               /* It's OK if undefined weak.  Calls to undefined weak
3371                  symbols behave as if the "called" function
3372                  immediately returns.  We can thus call to a weak
3373                  function without first checking whether the function
3374                  is defined.  */
3375               value = location;
3376               addend = 8;
3377             }
3378           else
3379             return bfd_reloc_undefined;
3380         }
3381       /* Fall thru.  */
3382
3383     case R_PARISC_PCREL21L:
3384     case R_PARISC_PCREL17C:
3385     case R_PARISC_PCREL17R:
3386     case R_PARISC_PCREL14R:
3387     case R_PARISC_PCREL14F:
3388     case R_PARISC_PCREL32:
3389       /* Make it a pc relative offset.  */
3390       value -= location;
3391       addend -= 8;
3392       break;
3393
3394     case R_PARISC_DPREL21L:
3395     case R_PARISC_DPREL14R:
3396     case R_PARISC_DPREL14F:
3397       /* Convert instructions that use the linkage table pointer (r19) to
3398          instructions that use the global data pointer (dp).  This is the
3399          most efficient way of using PIC code in an incomplete executable,
3400          but the user must follow the standard runtime conventions for
3401          accessing data for this to work.  */
3402       if (orig_r_type == R_PARISC_DLTIND21L)
3403         {
3404           /* Convert addil instructions if the original reloc was a
3405              DLTIND21L.  GCC sometimes uses a register other than r19 for
3406              the operation, so we must convert any addil instruction
3407              that uses this relocation.  */
3408           if ((insn & 0xfc000000) == ((int) OP_ADDIL << 26))
3409             insn = ADDIL_DP;
3410           else
3411             /* We must have a ldil instruction.  It's too hard to find
3412                and convert the associated add instruction, so issue an
3413                error.  */
3414             (*_bfd_error_handler)
3415               (_("%B(%A+0x%lx): %s fixup for insn 0x%x is not supported in a non-shared link"),
3416                input_bfd,
3417                input_section,
3418                offset,
3419                howto->name,
3420                insn);
3421         }
3422       else if (orig_r_type == R_PARISC_DLTIND14F)
3423         {
3424           /* This must be a format 1 load/store.  Change the base
3425              register to dp.  */
3426           insn = (insn & 0xfc1ffff) | (27 << 21);
3427         }
3428
3429     /* For all the DP relative relocations, we need to examine the symbol's
3430        section.  If it has no section or if it's a code section, then
3431        "data pointer relative" makes no sense.  In that case we don't
3432        adjust the "value", and for 21 bit addil instructions, we change the
3433        source addend register from %dp to %r0.  This situation commonly
3434        arises for undefined weak symbols and when a variable's "constness"
3435        is declared differently from the way the variable is defined.  For
3436        instance: "extern int foo" with foo defined as "const int foo".  */
3437       if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
3438         {
3439           if ((insn & ((0x3f << 26) | (0x1f << 21)))
3440               == (((int) OP_ADDIL << 26) | (27 << 21)))
3441             {
3442               insn &= ~ (0x1f << 21);
3443             }
3444           /* Now try to make things easy for the dynamic linker.  */
3445
3446           break;
3447         }
3448       /* Fall thru.  */
3449
3450     case R_PARISC_DLTIND21L:
3451     case R_PARISC_DLTIND14R:
3452     case R_PARISC_DLTIND14F:
3453     case R_PARISC_TLS_GD21L:
3454     case R_PARISC_TLS_GD14R:
3455     case R_PARISC_TLS_LDM21L:
3456     case R_PARISC_TLS_LDM14R:
3457     case R_PARISC_TLS_IE21L:
3458     case R_PARISC_TLS_IE14R:
3459       value -= elf_gp (input_section->output_section->owner);
3460       break;
3461
3462     case R_PARISC_SEGREL32:
3463       if ((sym_sec->flags & SEC_CODE) != 0)
3464         value -= htab->text_segment_base;
3465       else
3466         value -= htab->data_segment_base;
3467       break;
3468
3469     default:
3470       break;
3471     }
3472
3473   switch (r_type)
3474     {
3475     case R_PARISC_DIR32:
3476     case R_PARISC_DIR14F:
3477     case R_PARISC_DIR17F:
3478     case R_PARISC_PCREL17C:
3479     case R_PARISC_PCREL14F:
3480     case R_PARISC_PCREL32:
3481     case R_PARISC_DPREL14F:
3482     case R_PARISC_PLABEL32:
3483     case R_PARISC_DLTIND14F:
3484     case R_PARISC_SEGBASE:
3485     case R_PARISC_SEGREL32:
3486     case R_PARISC_TLS_DTPMOD32:
3487     case R_PARISC_TLS_DTPOFF32:
3488     case R_PARISC_TLS_TPREL32:
3489       r_field = e_fsel;
3490       break;
3491
3492     case R_PARISC_DLTIND21L:
3493     case R_PARISC_PCREL21L:
3494     case R_PARISC_PLABEL21L:
3495       r_field = e_lsel;
3496       break;
3497
3498     case R_PARISC_DIR21L:
3499     case R_PARISC_DPREL21L:
3500     case R_PARISC_TLS_GD21L:
3501     case R_PARISC_TLS_LDM21L:
3502     case R_PARISC_TLS_LDO21L:
3503     case R_PARISC_TLS_IE21L:
3504     case R_PARISC_TLS_LE21L:
3505       r_field = e_lrsel;
3506       break;
3507
3508     case R_PARISC_PCREL17R:
3509     case R_PARISC_PCREL14R:
3510     case R_PARISC_PLABEL14R:
3511     case R_PARISC_DLTIND14R:
3512       r_field = e_rsel;
3513       break;
3514
3515     case R_PARISC_DIR17R:
3516     case R_PARISC_DIR14R:
3517     case R_PARISC_DPREL14R:
3518     case R_PARISC_TLS_GD14R:
3519     case R_PARISC_TLS_LDM14R:
3520     case R_PARISC_TLS_LDO14R:
3521     case R_PARISC_TLS_IE14R:
3522     case R_PARISC_TLS_LE14R:
3523       r_field = e_rrsel;
3524       break;
3525
3526     case R_PARISC_PCREL12F:
3527     case R_PARISC_PCREL17F:
3528     case R_PARISC_PCREL22F:
3529       r_field = e_fsel;
3530
3531       if (r_type == (unsigned int) R_PARISC_PCREL17F)
3532         {
3533           max_branch_offset = (1 << (17-1)) << 2;
3534         }
3535       else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3536         {
3537           max_branch_offset = (1 << (12-1)) << 2;
3538         }
3539       else
3540         {
3541           max_branch_offset = (1 << (22-1)) << 2;
3542         }
3543
3544       /* sym_sec is NULL on undefined weak syms or when shared on
3545          undefined syms.  We've already checked for a stub for the
3546          shared undefined case.  */
3547       if (sym_sec == NULL)
3548         break;
3549
3550       /* If the branch is out of reach, then redirect the
3551          call to the local stub for this function.  */
3552       if (value + addend + max_branch_offset >= 2*max_branch_offset)
3553         {
3554           hsh = hppa_get_stub_entry (input_section, sym_sec,
3555                                             hh, rela, htab);
3556           if (hsh == NULL)
3557             return bfd_reloc_undefined;
3558
3559           /* Munge up the value and addend so that we call the stub
3560              rather than the procedure directly.  */
3561           value = (hsh->stub_offset
3562                    + hsh->stub_sec->output_offset
3563                    + hsh->stub_sec->output_section->vma
3564                    - location);
3565           addend = -8;
3566         }
3567       break;
3568
3569     /* Something we don't know how to handle.  */
3570     default:
3571       return bfd_reloc_notsupported;
3572     }
3573
3574   /* Make sure we can reach the stub.  */
3575   if (max_branch_offset != 0
3576       && value + addend + max_branch_offset >= 2*max_branch_offset)
3577     {
3578       (*_bfd_error_handler)
3579         (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3580          input_bfd,
3581          input_section,
3582          offset,
3583          hsh->bh_root.string);
3584       bfd_set_error (bfd_error_bad_value);
3585       return bfd_reloc_notsupported;
3586     }
3587
3588   val = hppa_field_adjust (value, addend, r_field);
3589
3590   switch (r_type)
3591     {
3592     case R_PARISC_PCREL12F:
3593     case R_PARISC_PCREL17C:
3594     case R_PARISC_PCREL17F:
3595     case R_PARISC_PCREL17R:
3596     case R_PARISC_PCREL22F:
3597     case R_PARISC_DIR17F:
3598     case R_PARISC_DIR17R:
3599       /* This is a branch.  Divide the offset by four.
3600          Note that we need to decide whether it's a branch or
3601          otherwise by inspecting the reloc.  Inspecting insn won't
3602          work as insn might be from a .word directive.  */
3603       val >>= 2;
3604       break;
3605
3606     default:
3607       break;
3608     }
3609
3610   insn = hppa_rebuild_insn (insn, val, r_format);
3611
3612   /* Update the instruction word.  */
3613   bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3614   return bfd_reloc_ok;
3615 }
3616
3617 /* Relocate an HPPA ELF section.  */
3618
3619 static bfd_boolean
3620 elf32_hppa_relocate_section (bfd *output_bfd,
3621                              struct bfd_link_info *info,
3622                              bfd *input_bfd,
3623                              asection *input_section,
3624                              bfd_byte *contents,
3625                              Elf_Internal_Rela *relocs,
3626                              Elf_Internal_Sym *local_syms,
3627                              asection **local_sections)
3628 {
3629   bfd_vma *local_got_offsets;
3630   struct elf32_hppa_link_hash_table *htab;
3631   Elf_Internal_Shdr *symtab_hdr;
3632   Elf_Internal_Rela *rela;
3633   Elf_Internal_Rela *relend;
3634
3635   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3636
3637   htab = hppa_link_hash_table (info);
3638   local_got_offsets = elf_local_got_offsets (input_bfd);
3639
3640   rela = relocs;
3641   relend = relocs + input_section->reloc_count;
3642   for (; rela < relend; rela++)
3643     {
3644       unsigned int r_type;
3645       reloc_howto_type *howto;
3646       unsigned int r_symndx;
3647       struct elf32_hppa_link_hash_entry *hh;
3648       Elf_Internal_Sym *sym;
3649       asection *sym_sec;
3650       bfd_vma relocation;
3651       bfd_reloc_status_type rstatus;
3652       const char *sym_name;
3653       bfd_boolean plabel;
3654       bfd_boolean warned_undef;
3655
3656       r_type = ELF32_R_TYPE (rela->r_info);
3657       if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3658         {
3659           bfd_set_error (bfd_error_bad_value);
3660           return FALSE;
3661         }
3662       if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3663           || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3664         continue;
3665
3666       r_symndx = ELF32_R_SYM (rela->r_info);
3667       hh = NULL;
3668       sym = NULL;
3669       sym_sec = NULL;
3670       warned_undef = FALSE;
3671       if (r_symndx < symtab_hdr->sh_info)
3672         {
3673           /* This is a local symbol, h defaults to NULL.  */
3674           sym = local_syms + r_symndx;
3675           sym_sec = local_sections[r_symndx];
3676           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rela);
3677         }
3678       else
3679         {
3680           struct elf_link_hash_entry *eh;
3681           bfd_boolean unresolved_reloc;
3682           struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3683
3684           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rela,
3685                                    r_symndx, symtab_hdr, sym_hashes,
3686                                    eh, sym_sec, relocation,
3687                                    unresolved_reloc, warned_undef);
3688
3689           if (!info->relocatable
3690               && relocation == 0
3691               && eh->root.type != bfd_link_hash_defined
3692               && eh->root.type != bfd_link_hash_defweak
3693               && eh->root.type != bfd_link_hash_undefweak)
3694             {
3695               if (info->unresolved_syms_in_objects == RM_IGNORE
3696                   && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3697                   && eh->type == STT_PARISC_MILLI)
3698                 {
3699                   if (! info->callbacks->undefined_symbol
3700                       (info, eh_name (eh), input_bfd,
3701                        input_section, rela->r_offset, FALSE))
3702                     return FALSE;
3703                   warned_undef = TRUE;
3704                 }
3705             }
3706           hh = hppa_elf_hash_entry (eh);
3707         }
3708
3709       if (sym_sec != NULL && elf_discarded_section (sym_sec))
3710         {
3711           /* For relocs against symbols from removed linkonce
3712              sections, or sections discarded by a linker script,
3713              we just want the section contents zeroed.  Avoid any
3714              special processing.  */
3715           _bfd_clear_contents (elf_hppa_howto_table + r_type, input_bfd,
3716                                contents + rela->r_offset);
3717           rela->r_info = 0;
3718           rela->r_addend = 0;
3719           continue;
3720         }
3721
3722       if (info->relocatable)
3723         continue;
3724
3725       /* Do any required modifications to the relocation value, and
3726          determine what types of dynamic info we need to output, if
3727          any.  */
3728       plabel = 0;
3729       switch (r_type)
3730         {
3731         case R_PARISC_DLTIND14F:
3732         case R_PARISC_DLTIND14R:
3733         case R_PARISC_DLTIND21L:
3734           {
3735             bfd_vma off;
3736             bfd_boolean do_got = 0;
3737
3738             /* Relocation is to the entry for this symbol in the
3739                global offset table.  */
3740             if (hh != NULL)
3741               {
3742                 bfd_boolean dyn;
3743
3744                 off = hh->eh.got.offset;
3745                 dyn = htab->etab.dynamic_sections_created;
3746                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
3747                                                        &hh->eh))
3748                   {
3749                     /* If we aren't going to call finish_dynamic_symbol,
3750                        then we need to handle initialisation of the .got
3751                        entry and create needed relocs here.  Since the
3752                        offset must always be a multiple of 4, we use the
3753                        least significant bit to record whether we have
3754                        initialised it already.  */
3755                     if ((off & 1) != 0)
3756                       off &= ~1;
3757                     else
3758                       {
3759                         hh->eh.got.offset |= 1;
3760                         do_got = 1;
3761                       }
3762                   }
3763               }
3764             else
3765               {
3766                 /* Local symbol case.  */
3767                 if (local_got_offsets == NULL)
3768                   abort ();
3769
3770                 off = local_got_offsets[r_symndx];
3771
3772                 /* The offset must always be a multiple of 4.  We use
3773                    the least significant bit to record whether we have
3774                    already generated the necessary reloc.  */
3775                 if ((off & 1) != 0)
3776                   off &= ~1;
3777                 else
3778                   {
3779                     local_got_offsets[r_symndx] |= 1;
3780                     do_got = 1;
3781                   }
3782               }
3783
3784             if (do_got)
3785               {
3786                 if (info->shared)
3787                   {
3788                     /* Output a dynamic relocation for this GOT entry.
3789                        In this case it is relative to the base of the
3790                        object because the symbol index is zero.  */
3791                     Elf_Internal_Rela outrel;
3792                     bfd_byte *loc;
3793                     asection *sec = htab->srelgot;
3794
3795                     outrel.r_offset = (off
3796                                        + htab->sgot->output_offset
3797                                        + htab->sgot->output_section->vma);
3798                     outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3799                     outrel.r_addend = relocation;
3800                     loc = sec->contents;
3801                     loc += sec->reloc_count++ * sizeof (Elf32_External_Rela);
3802                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3803                   }
3804                 else
3805                   bfd_put_32 (output_bfd, relocation,
3806                               htab->sgot->contents + off);
3807               }
3808
3809             if (off >= (bfd_vma) -2)
3810               abort ();
3811
3812             /* Add the base of the GOT to the relocation value.  */
3813             relocation = (off
3814                           + htab->sgot->output_offset
3815                           + htab->sgot->output_section->vma);
3816           }
3817           break;
3818
3819         case R_PARISC_SEGREL32:
3820           /* If this is the first SEGREL relocation, then initialize
3821              the segment base values.  */
3822           if (htab->text_segment_base == (bfd_vma) -1)
3823             bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
3824           break;
3825
3826         case R_PARISC_PLABEL14R:
3827         case R_PARISC_PLABEL21L:
3828         case R_PARISC_PLABEL32:
3829           if (htab->etab.dynamic_sections_created)
3830             {
3831               bfd_vma off;
3832               bfd_boolean do_plt = 0;
3833               /* If we have a global symbol with a PLT slot, then
3834                  redirect this relocation to it.  */
3835               if (hh != NULL)
3836                 {
3837                   off = hh->eh.plt.offset;
3838                   if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared,
3839                                                          &hh->eh))
3840                     {
3841                       /* In a non-shared link, adjust_dynamic_symbols
3842                          isn't called for symbols forced local.  We
3843                          need to write out the plt entry here.  */
3844                       if ((off & 1) != 0)
3845                         off &= ~1;
3846                       else
3847                         {
3848                           hh->eh.plt.offset |= 1;
3849                           do_plt = 1;
3850                         }
3851                     }
3852                 }
3853               else
3854                 {
3855                   bfd_vma *local_plt_offsets;
3856
3857                   if (local_got_offsets == NULL)
3858                     abort ();
3859
3860                   local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3861                   off = local_plt_offsets[r_symndx];
3862
3863                   /* As for the local .got entry case, we use the last
3864                      bit to record whether we've already initialised
3865                      this local .plt entry.  */
3866                   if ((off & 1) != 0)
3867                     off &= ~1;
3868                   else
3869                     {
3870                       local_plt_offsets[r_symndx] |= 1;
3871                       do_plt = 1;
3872                     }
3873                 }
3874
3875               if (do_plt)
3876                 {
3877                   if (info->shared)
3878                     {
3879                       /* Output a dynamic IPLT relocation for this
3880                          PLT entry.  */
3881                       Elf_Internal_Rela outrel;
3882                       bfd_byte *loc;
3883                       asection *s = htab->srelplt;
3884
3885                       outrel.r_offset = (off
3886                                          + htab->splt->output_offset
3887                                          + htab->splt->output_section->vma);
3888                       outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3889                       outrel.r_addend = relocation;
3890                       loc = s->contents;
3891                       loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3892                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3893                     }
3894                   else
3895                     {
3896                       bfd_put_32 (output_bfd,
3897                                   relocation,
3898                                   htab->splt->contents + off);
3899                       bfd_put_32 (output_bfd,
3900                                   elf_gp (htab->splt->output_section->owner),
3901                                   htab->splt->contents + off + 4);
3902                     }
3903                 }
3904
3905               if (off >= (bfd_vma) -2)
3906                 abort ();
3907
3908               /* PLABELs contain function pointers.  Relocation is to
3909                  the entry for the function in the .plt.  The magic +2
3910                  offset signals to $$dyncall that the function pointer
3911                  is in the .plt and thus has a gp pointer too.
3912                  Exception:  Undefined PLABELs should have a value of
3913                  zero.  */
3914               if (hh == NULL
3915                   || (hh->eh.root.type != bfd_link_hash_undefweak
3916                       && hh->eh.root.type != bfd_link_hash_undefined))
3917                 {
3918                   relocation = (off
3919                                 + htab->splt->output_offset
3920                                 + htab->splt->output_section->vma
3921                                 + 2);
3922                 }
3923               plabel = 1;
3924             }
3925           /* Fall through and possibly emit a dynamic relocation.  */
3926
3927         case R_PARISC_DIR17F:
3928         case R_PARISC_DIR17R:
3929         case R_PARISC_DIR14F:
3930         case R_PARISC_DIR14R:
3931         case R_PARISC_DIR21L:
3932         case R_PARISC_DPREL14F:
3933         case R_PARISC_DPREL14R:
3934         case R_PARISC_DPREL21L:
3935         case R_PARISC_DIR32:
3936           if ((input_section->flags & SEC_ALLOC) == 0)
3937             break;
3938
3939           /* The reloc types handled here and this conditional
3940              expression must match the code in ..check_relocs and
3941              allocate_dynrelocs.  ie. We need exactly the same condition
3942              as in ..check_relocs, with some extra conditions (dynindx
3943              test in this case) to cater for relocs removed by
3944              allocate_dynrelocs.  If you squint, the non-shared test
3945              here does indeed match the one in ..check_relocs, the
3946              difference being that here we test DEF_DYNAMIC as well as
3947              !DEF_REGULAR.  All common syms end up with !DEF_REGULAR,
3948              which is why we can't use just that test here.
3949              Conversely, DEF_DYNAMIC can't be used in check_relocs as
3950              there all files have not been loaded.  */
3951           if ((info->shared
3952                && (hh == NULL
3953                    || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
3954                    || hh->eh.root.type != bfd_link_hash_undefweak)
3955                && (IS_ABSOLUTE_RELOC (r_type)
3956                    || !SYMBOL_CALLS_LOCAL (info, &hh->eh)))
3957               || (!info->shared
3958                   && hh != NULL
3959                   && hh->eh.dynindx != -1
3960                   && !hh->eh.non_got_ref
3961                   && ((ELIMINATE_COPY_RELOCS
3962                        && hh->eh.def_dynamic
3963                        && !hh->eh.def_regular)
3964                       || hh->eh.root.type == bfd_link_hash_undefweak
3965                       || hh->eh.root.type == bfd_link_hash_undefined)))
3966             {
3967               Elf_Internal_Rela outrel;
3968               bfd_boolean skip;
3969               asection *sreloc;
3970               bfd_byte *loc;
3971
3972               /* When generating a shared object, these relocations
3973                  are copied into the output file to be resolved at run
3974                  time.  */
3975
3976               outrel.r_addend = rela->r_addend;
3977               outrel.r_offset =
3978                 _bfd_elf_section_offset (output_bfd, info, input_section,
3979                                          rela->r_offset);
3980               skip = (outrel.r_offset == (bfd_vma) -1
3981                       || outrel.r_offset == (bfd_vma) -2);
3982               outrel.r_offset += (input_section->output_offset
3983                                   + input_section->output_section->vma);
3984                       
3985               if (skip)
3986                 {
3987                   memset (&outrel, 0, sizeof (outrel));
3988                 }
3989               else if (hh != NULL
3990                        && hh->eh.dynindx != -1
3991                        && (plabel
3992                            || !IS_ABSOLUTE_RELOC (r_type)
3993                            || !info->shared
3994                            || !info->symbolic
3995                            || !hh->eh.def_regular))
3996                 {
3997                   outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
3998                 }
3999               else /* It's a local symbol, or one marked to become local.  */
4000                 {
4001                   int indx = 0;
4002
4003                   /* Add the absolute offset of the symbol.  */
4004                   outrel.r_addend += relocation;
4005
4006                   /* Global plabels need to be processed by the
4007                      dynamic linker so that functions have at most one
4008                      fptr.  For this reason, we need to differentiate
4009                      between global and local plabels, which we do by
4010                      providing the function symbol for a global plabel
4011                      reloc, and no symbol for local plabels.  */
4012                   if (! plabel
4013                       && sym_sec != NULL
4014                       && sym_sec->output_section != NULL
4015                       && ! bfd_is_abs_section (sym_sec))
4016                     {
4017                       asection *osec;
4018
4019                       osec = sym_sec->output_section;
4020                       indx = elf_section_data (osec)->dynindx;
4021                       if (indx == 0)
4022                         {
4023                           osec = htab->etab.text_index_section;
4024                           indx = elf_section_data (osec)->dynindx;
4025                         }
4026                       BFD_ASSERT (indx != 0);
4027
4028                       /* We are turning this relocation into one
4029                          against a section symbol, so subtract out the
4030                          output section's address but not the offset
4031                          of the input section in the output section.  */
4032                       outrel.r_addend -= osec->vma;
4033                     }
4034
4035                   outrel.r_info = ELF32_R_INFO (indx, r_type);
4036                 }
4037               sreloc = elf_section_data (input_section)->sreloc;
4038               if (sreloc == NULL)
4039                 abort ();
4040
4041               loc = sreloc->contents;
4042               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4043               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4044             }
4045           break;
4046           
4047         case R_PARISC_TLS_LDM21L:
4048         case R_PARISC_TLS_LDM14R:
4049           {
4050             bfd_vma off;
4051         
4052             off = htab->tls_ldm_got.offset;
4053             if (off & 1)
4054               off &= ~1;
4055             else
4056               {
4057                 Elf_Internal_Rela outrel;
4058                 bfd_byte *loc;
4059
4060                 outrel.r_offset = (off 
4061                                    + htab->sgot->output_section->vma
4062                                    + htab->sgot->output_offset);
4063                 outrel.r_addend = 0;
4064                 outrel.r_info = ELF32_R_INFO (0, R_PARISC_TLS_DTPMOD32);
4065                 loc = htab->srelgot->contents; 
4066                 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4067
4068                 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4069                 htab->tls_ldm_got.offset |= 1;
4070               }
4071
4072             /* Add the base of the GOT to the relocation value.  */
4073             relocation = (off
4074                           + htab->sgot->output_offset
4075                           + htab->sgot->output_section->vma);
4076
4077             break;
4078           }
4079
4080         case R_PARISC_TLS_LDO21L:
4081         case R_PARISC_TLS_LDO14R:
4082           relocation -= dtpoff_base (info);
4083           break;
4084
4085         case R_PARISC_TLS_GD21L:
4086         case R_PARISC_TLS_GD14R:
4087         case R_PARISC_TLS_IE21L:
4088         case R_PARISC_TLS_IE14R:
4089           {
4090             bfd_vma off;
4091             int indx;
4092             char tls_type;
4093
4094             indx = 0;
4095             if (hh != NULL)
4096               {
4097                 bfd_boolean dyn;
4098                 dyn = htab->etab.dynamic_sections_created;
4099
4100                 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &hh->eh)
4101                     && (!info->shared
4102                         || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
4103                   {
4104                     indx = hh->eh.dynindx;
4105                   }
4106                 off = hh->eh.got.offset;
4107                 tls_type = hh->tls_type;
4108               }
4109             else
4110               {
4111                 off = local_got_offsets[r_symndx];
4112                 tls_type = hppa_elf_local_got_tls_type (input_bfd)[r_symndx];
4113               }
4114
4115             if (tls_type == GOT_UNKNOWN)
4116               abort ();
4117
4118             if ((off & 1) != 0)
4119               off &= ~1;
4120             else
4121               {
4122                 bfd_boolean need_relocs = FALSE;
4123                 Elf_Internal_Rela outrel;
4124                 bfd_byte *loc = NULL;
4125                 int cur_off = off;
4126
4127                 /* The GOT entries have not been initialized yet.  Do it
4128                    now, and emit any relocations.  If both an IE GOT and a
4129                    GD GOT are necessary, we emit the GD first.  */
4130
4131                 if ((info->shared || indx != 0)
4132                     && (hh == NULL
4133                         || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
4134                         || hh->eh.root.type != bfd_link_hash_undefweak))
4135                   {
4136                     need_relocs = TRUE;
4137                     loc = htab->srelgot->contents; 
4138                     /* FIXME (CAO): Should this be reloc_count++ ? */
4139                     loc += htab->srelgot->reloc_count * sizeof (Elf32_External_Rela);
4140                   }
4141
4142                 if (tls_type & GOT_TLS_GD)
4143                   {
4144                     if (need_relocs)
4145                       {
4146                         outrel.r_offset = (cur_off
4147                                            + htab->sgot->output_section->vma
4148                                            + htab->sgot->output_offset);
4149                         outrel.r_info = ELF32_R_INFO (indx,R_PARISC_TLS_DTPMOD32);
4150                         outrel.r_addend = 0;
4151                         bfd_put_32 (output_bfd, 0, htab->sgot->contents + cur_off);
4152                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4153                         htab->srelgot->reloc_count++;
4154                         loc += sizeof (Elf32_External_Rela);
4155
4156                         if (indx == 0)
4157                           bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
4158                                       htab->sgot->contents + cur_off + 4);
4159                         else
4160                           {
4161                             bfd_put_32 (output_bfd, 0,
4162                                         htab->sgot->contents + cur_off + 4);
4163                             outrel.r_info = ELF32_R_INFO (indx, R_PARISC_TLS_DTPOFF32);
4164                             outrel.r_offset += 4;
4165                             bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
4166                             htab->srelgot->reloc_count++;
4167                             loc += sizeof (Elf32_External_Rela);
4168                           }
4169                       }
4170                     else
4171                       {
4172                         /* If we are not emitting relocations for a
4173                            general dynamic reference, then we must be in a
4174                            static link or an executable link with the
4175                            symbol binding locally.  Mark it as belonging
4176                            to module 1, the executable.  */
4177                         bfd_put_32 (output_bfd, 1,
4178                                     htab->sgot->contents + cur_off);
4179                         bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
4180                                     htab->sgot->contents + cur_off + 4);
4181                       }
4182
4183
4184                     cur_off += 8;
4185                   }
4186
4187                 if (tls_type & GOT_TLS_IE)
4188                   {
4189                     if (need_relocs)
4190                       {
4191                         outrel.r_offset = (cur_off
4192                                            + htab->sgot->output_section->vma
4193                                            + htab->sgot->output_offset);
4194                         outrel.r_info = ELF32_R_INFO (indx, R_PARISC_TLS_TPREL32);
4195
4196                         if (indx == 0)
4197                           outrel.r_addend = relocation - dtpoff_base (info);
4198                         else
4199                           outrel.r_addend = 0;
4200
4201                         bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4202                         htab->srelgot->reloc_count++;
4203                         loc += sizeof (Elf32_External_Rela);
4204                       }
4205                     else
4206                       bfd_put_32 (output_bfd, tpoff (info, relocation),
4207                                   htab->sgot->contents + cur_off);
4208
4209                     cur_off += 4;
4210                   }
4211
4212                 if (hh != NULL)
4213                   hh->eh.got.offset |= 1;
4214                 else
4215                   local_got_offsets[r_symndx] |= 1;
4216               }
4217
4218             if ((tls_type & GOT_TLS_GD)
4219                 && r_type != R_PARISC_TLS_GD21L
4220                 && r_type != R_PARISC_TLS_GD14R)
4221               off += 2 * GOT_ENTRY_SIZE;
4222
4223             /* Add the base of the GOT to the relocation value.  */
4224             relocation = (off
4225                           + htab->sgot->output_offset
4226                           + htab->sgot->output_section->vma);
4227
4228             break;
4229           }
4230
4231         case R_PARISC_TLS_LE21L:
4232         case R_PARISC_TLS_LE14R:
4233           {
4234             relocation = tpoff (info, relocation);
4235             break;
4236           }
4237           break;
4238
4239         default:
4240           break;
4241         }
4242
4243       rstatus = final_link_relocate (input_section, contents, rela, relocation,
4244                                htab, sym_sec, hh, info);
4245
4246       if (rstatus == bfd_reloc_ok)
4247         continue;
4248
4249       if (hh != NULL)
4250         sym_name = hh_name (hh);
4251       else
4252         {
4253           sym_name = bfd_elf_string_from_elf_section (input_bfd,
4254                                                       symtab_hdr->sh_link,
4255                                                       sym->st_name);
4256           if (sym_name == NULL)
4257             return FALSE;
4258           if (*sym_name == '\0')
4259             sym_name = bfd_section_name (input_bfd, sym_sec);
4260         }
4261
4262       howto = elf_hppa_howto_table + r_type;
4263
4264       if (rstatus == bfd_reloc_undefined || rstatus == bfd_reloc_notsupported)
4265         {
4266           if (rstatus == bfd_reloc_notsupported || !warned_undef)
4267             {
4268               (*_bfd_error_handler)
4269                 (_("%B(%A+0x%lx): cannot handle %s for %s"),
4270                  input_bfd,
4271                  input_section,
4272                  (long) rela->r_offset,
4273                  howto->name,
4274                  sym_name);
4275               bfd_set_error (bfd_error_bad_value);
4276               return FALSE;
4277             }
4278         }
4279       else
4280         {
4281           if (!((*info->callbacks->reloc_overflow)
4282                 (info, (hh ? &hh->eh.root : NULL), sym_name, howto->name,
4283                  (bfd_vma) 0, input_bfd, input_section, rela->r_offset)))
4284             return FALSE;
4285         }
4286     }
4287
4288   return TRUE;
4289 }
4290
4291 /* Finish up dynamic symbol handling.  We set the contents of various
4292    dynamic sections here.  */
4293
4294 static bfd_boolean
4295 elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
4296                                   struct bfd_link_info *info,
4297                                   struct elf_link_hash_entry *eh,
4298                                   Elf_Internal_Sym *sym)
4299 {
4300   struct elf32_hppa_link_hash_table *htab;
4301   Elf_Internal_Rela rela;
4302   bfd_byte *loc;
4303
4304   htab = hppa_link_hash_table (info);
4305
4306   if (eh->plt.offset != (bfd_vma) -1)
4307     {
4308       bfd_vma value;
4309
4310       if (eh->plt.offset & 1)
4311         abort ();
4312
4313       /* This symbol has an entry in the procedure linkage table.  Set
4314          it up.
4315
4316          The format of a plt entry is
4317          <funcaddr>
4318          <__gp>
4319       */
4320       value = 0;
4321       if (eh->root.type == bfd_link_hash_defined
4322           || eh->root.type == bfd_link_hash_defweak)
4323         {
4324           value = eh->root.u.def.value;
4325           if (eh->root.u.def.section->output_section != NULL)
4326             value += (eh->root.u.def.section->output_offset
4327                       + eh->root.u.def.section->output_section->vma);
4328         }
4329
4330       /* Create a dynamic IPLT relocation for this entry.  */
4331       rela.r_offset = (eh->plt.offset
4332                       + htab->splt->output_offset
4333                       + htab->splt->output_section->vma);
4334       if (eh->dynindx != -1)
4335         {
4336           rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_IPLT);
4337           rela.r_addend = 0;
4338         }
4339       else
4340         {
4341           /* This symbol has been marked to become local, and is
4342              used by a plabel so must be kept in the .plt.  */
4343           rela.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
4344           rela.r_addend = value;
4345         }
4346
4347       loc = htab->srelplt->contents;
4348       loc += htab->srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
4349       bfd_elf32_swap_reloca_out (htab->splt->output_section->owner, &rela, loc);
4350
4351       if (!eh->def_regular)
4352         {
4353           /* Mark the symbol as undefined, rather than as defined in
4354              the .plt section.  Leave the value alone.  */
4355           sym->st_shndx = SHN_UNDEF;
4356         }
4357     }
4358
4359   if (eh->got.offset != (bfd_vma) -1
4360       && (hppa_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0
4361       && (hppa_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0)
4362     {
4363       /* This symbol has an entry in the global offset table.  Set it
4364          up.  */
4365
4366       rela.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
4367                       + htab->sgot->output_offset
4368                       + htab->sgot->output_section->vma);
4369
4370       /* If this is a -Bsymbolic link and the symbol is defined
4371          locally or was forced to be local because of a version file,
4372          we just want to emit a RELATIVE reloc.  The entry in the
4373          global offset table will already have been initialized in the
4374          relocate_section function.  */
4375       if (info->shared
4376           && (info->symbolic || eh->dynindx == -1)
4377           && eh->def_regular)
4378         {
4379           rela.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
4380           rela.r_addend = (eh->root.u.def.value
4381                           + eh->root.u.def.section->output_offset
4382                           + eh->root.u.def.section->output_section->vma);
4383         }
4384       else
4385         {
4386           if ((eh->got.offset & 1) != 0)
4387             abort ();
4388
4389           bfd_put_32 (output_bfd, 0, htab->sgot->contents + (eh->got.offset & ~1));
4390           rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_DIR32);
4391           rela.r_addend = 0;
4392         }
4393
4394       loc = htab->srelgot->contents;
4395       loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4396       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4397     }
4398
4399   if (eh->needs_copy)
4400     {
4401       asection *sec;
4402
4403       /* This symbol needs a copy reloc.  Set it up.  */
4404
4405       if (! (eh->dynindx != -1
4406              && (eh->root.type == bfd_link_hash_defined
4407                  || eh->root.type == bfd_link_hash_defweak)))
4408         abort ();
4409
4410       sec = htab->srelbss;
4411
4412       rela.r_offset = (eh->root.u.def.value
4413                       + eh->root.u.def.section->output_offset
4414                       + eh->root.u.def.section->output_section->vma);
4415       rela.r_addend = 0;
4416       rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_COPY);
4417       loc = sec->contents + sec->reloc_count++ * sizeof (Elf32_External_Rela);
4418       bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4419     }
4420
4421   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
4422   if (eh_name (eh)[0] == '_'
4423       && (strcmp (eh_name (eh), "_DYNAMIC") == 0
4424           || eh == htab->etab.hgot))
4425     {
4426       sym->st_shndx = SHN_ABS;
4427     }
4428
4429   return TRUE;
4430 }
4431
4432 /* Used to decide how to sort relocs in an optimal manner for the
4433    dynamic linker, before writing them out.  */
4434
4435 static enum elf_reloc_type_class
4436 elf32_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
4437 {
4438   /* Handle TLS relocs first; we don't want them to be marked
4439      relative by the "if (ELF32_R_SYM (rela->r_info) == 0)"
4440      check below.  */
4441   switch ((int) ELF32_R_TYPE (rela->r_info))
4442     {
4443       case R_PARISC_TLS_DTPMOD32:
4444       case R_PARISC_TLS_DTPOFF32:
4445       case R_PARISC_TLS_TPREL32:
4446         return reloc_class_normal;
4447     }
4448
4449   if (ELF32_R_SYM (rela->r_info) == 0)
4450     return reloc_class_relative;
4451
4452   switch ((int) ELF32_R_TYPE (rela->r_info))
4453     {
4454     case R_PARISC_IPLT:
4455       return reloc_class_plt;
4456     case R_PARISC_COPY:
4457       return reloc_class_copy;
4458     default:
4459       return reloc_class_normal;
4460     }
4461 }
4462
4463 /* Finish up the dynamic sections.  */
4464
4465 static bfd_boolean
4466 elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
4467                                     struct bfd_link_info *info)
4468 {
4469   bfd *dynobj;
4470   struct elf32_hppa_link_hash_table *htab;
4471   asection *sdyn;
4472
4473   htab = hppa_link_hash_table (info);
4474   dynobj = htab->etab.dynobj;
4475
4476   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4477
4478   if (htab->etab.dynamic_sections_created)
4479     {
4480       Elf32_External_Dyn *dyncon, *dynconend;
4481
4482       if (sdyn == NULL)
4483         abort ();
4484
4485       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4486       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4487       for (; dyncon < dynconend; dyncon++)
4488         {
4489           Elf_Internal_Dyn dyn;
4490           asection *s;
4491
4492           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4493
4494           switch (dyn.d_tag)
4495             {
4496             default:
4497               continue;
4498
4499             case DT_PLTGOT:
4500               /* Use PLTGOT to set the GOT register.  */
4501               dyn.d_un.d_ptr = elf_gp (output_bfd);
4502               break;
4503
4504             case DT_JMPREL:
4505               s = htab->srelplt;
4506               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4507               break;
4508
4509             case DT_PLTRELSZ:
4510               s = htab->srelplt;
4511               dyn.d_un.d_val = s->size;
4512               break;
4513
4514             case DT_RELASZ:
4515               /* Don't count procedure linkage table relocs in the
4516                  overall reloc count.  */
4517               s = htab->srelplt;
4518               if (s == NULL)
4519                 continue;
4520               dyn.d_un.d_val -= s->size;
4521               break;
4522
4523             case DT_RELA:
4524               /* We may not be using the standard ELF linker script.
4525                  If .rela.plt is the first .rela section, we adjust
4526                  DT_RELA to not include it.  */
4527               s = htab->srelplt;
4528               if (s == NULL)
4529                 continue;
4530               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4531                 continue;
4532               dyn.d_un.d_ptr += s->size;
4533               break;
4534             }
4535
4536           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4537         }
4538     }
4539
4540   if (htab->sgot != NULL && htab->sgot->size != 0)
4541     {
4542       /* Fill in the first entry in the global offset table.
4543          We use it to point to our dynamic section, if we have one.  */
4544       bfd_put_32 (output_bfd,
4545                   sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
4546                   htab->sgot->contents);
4547
4548       /* The second entry is reserved for use by the dynamic linker.  */
4549       memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4550
4551       /* Set .got entry size.  */
4552       elf_section_data (htab->sgot->output_section)
4553         ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4554     }
4555
4556   if (htab->splt != NULL && htab->splt->size != 0)
4557     {
4558       /* Set plt entry size.  */
4559       elf_section_data (htab->splt->output_section)
4560         ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4561
4562       if (htab->need_plt_stub)
4563         {
4564           /* Set up the .plt stub.  */
4565           memcpy (htab->splt->contents
4566                   + htab->splt->size - sizeof (plt_stub),
4567                   plt_stub, sizeof (plt_stub));
4568
4569           if ((htab->splt->output_offset
4570                + htab->splt->output_section->vma
4571                + htab->splt->size)
4572               != (htab->sgot->output_offset
4573                   + htab->sgot->output_section->vma))
4574             {
4575               (*_bfd_error_handler)
4576                 (_(".got section not immediately after .plt section"));
4577               return FALSE;
4578             }
4579         }
4580     }
4581
4582   return TRUE;
4583 }
4584
4585 /* Called when writing out an object file to decide the type of a
4586    symbol.  */
4587 static int
4588 elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
4589 {
4590   if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4591     return STT_PARISC_MILLI;
4592   else
4593     return type;
4594 }
4595
4596 /* Misc BFD support code.  */
4597 #define bfd_elf32_bfd_is_local_label_name    elf_hppa_is_local_label_name
4598 #define bfd_elf32_bfd_reloc_type_lookup      elf_hppa_reloc_type_lookup
4599 #define bfd_elf32_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
4600 #define elf_info_to_howto                    elf_hppa_info_to_howto
4601 #define elf_info_to_howto_rel                elf_hppa_info_to_howto_rel
4602
4603 /* Stuff for the BFD linker.  */
4604 #define bfd_elf32_bfd_final_link             elf32_hppa_final_link
4605 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4606 #define bfd_elf32_bfd_link_hash_table_free   elf32_hppa_link_hash_table_free
4607 #define elf_backend_adjust_dynamic_symbol    elf32_hppa_adjust_dynamic_symbol
4608 #define elf_backend_copy_indirect_symbol     elf32_hppa_copy_indirect_symbol
4609 #define elf_backend_check_relocs             elf32_hppa_check_relocs
4610 #define elf_backend_create_dynamic_sections  elf32_hppa_create_dynamic_sections
4611 #define elf_backend_fake_sections            elf_hppa_fake_sections
4612 #define elf_backend_relocate_section         elf32_hppa_relocate_section
4613 #define elf_backend_hide_symbol              elf32_hppa_hide_symbol
4614 #define elf_backend_finish_dynamic_symbol    elf32_hppa_finish_dynamic_symbol
4615 #define elf_backend_finish_dynamic_sections  elf32_hppa_finish_dynamic_sections
4616 #define elf_backend_size_dynamic_sections    elf32_hppa_size_dynamic_sections
4617 #define elf_backend_init_index_section       _bfd_elf_init_1_index_section
4618 #define elf_backend_gc_mark_hook             elf32_hppa_gc_mark_hook
4619 #define elf_backend_gc_sweep_hook            elf32_hppa_gc_sweep_hook
4620 #define elf_backend_grok_prstatus            elf32_hppa_grok_prstatus
4621 #define elf_backend_grok_psinfo              elf32_hppa_grok_psinfo
4622 #define elf_backend_object_p                 elf32_hppa_object_p
4623 #define elf_backend_final_write_processing   elf_hppa_final_write_processing
4624 #define elf_backend_post_process_headers     _bfd_elf_set_osabi
4625 #define elf_backend_get_symbol_type          elf32_hppa_elf_get_symbol_type
4626 #define elf_backend_reloc_type_class         elf32_hppa_reloc_type_class
4627 #define elf_backend_action_discarded         elf_hppa_action_discarded
4628
4629 #define elf_backend_can_gc_sections          1
4630 #define elf_backend_can_refcount             1
4631 #define elf_backend_plt_alignment            2
4632 #define elf_backend_want_got_plt             0
4633 #define elf_backend_plt_readonly             0
4634 #define elf_backend_want_plt_sym             0
4635 #define elf_backend_got_header_size          8
4636 #define elf_backend_rela_normal              1
4637
4638 #define TARGET_BIG_SYM          bfd_elf32_hppa_vec
4639 #define TARGET_BIG_NAME         "elf32-hppa"
4640 #define ELF_ARCH                bfd_arch_hppa
4641 #define ELF_MACHINE_CODE        EM_PARISC
4642 #define ELF_MAXPAGESIZE         0x1000
4643 #define ELF_OSABI               ELFOSABI_HPUX
4644 #define elf32_bed               elf32_hppa_hpux_bed
4645
4646 #include "elf32-target.h"
4647
4648 #undef TARGET_BIG_SYM
4649 #define TARGET_BIG_SYM          bfd_elf32_hppa_linux_vec
4650 #undef TARGET_BIG_NAME
4651 #define TARGET_BIG_NAME         "elf32-hppa-linux"
4652 #undef ELF_OSABI
4653 #define ELF_OSABI               ELFOSABI_LINUX
4654 #undef elf32_bed
4655 #define elf32_bed               elf32_hppa_linux_bed
4656
4657 #include "elf32-target.h"
4658
4659 #undef TARGET_BIG_SYM
4660 #define TARGET_BIG_SYM          bfd_elf32_hppa_nbsd_vec
4661 #undef TARGET_BIG_NAME
4662 #define TARGET_BIG_NAME         "elf32-hppa-netbsd"
4663 #undef ELF_OSABI
4664 #define ELF_OSABI               ELFOSABI_NETBSD
4665 #undef elf32_bed
4666 #define elf32_bed               elf32_hppa_netbsd_bed
4667
4668 #include "elf32-target.h"