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