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