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