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