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