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