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