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