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