* aout-arm.c, aout-target.h, aoutx.h, archive.c, armnetbsd.c,
[external/binutils.git] / bfd / elf32-hppa.c
1 /* BFD back-end for HP PA-RISC ELF files.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
3    2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5    Original code by
6         Center for Software Science
7         Department of Computer Science
8         University of Utah
9    Largely rewritten by Alan Modra <alan@linuxcare.com.au>
10
11    This file is part of BFD, the Binary File Descriptor library.
12
13    This program is free software; you can redistribute it and/or modify
14    it under the terms of the GNU General Public License as published by
15    the Free Software Foundation; either version 2 of the License, or
16    (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22
23    You should have received a copy of the GNU General Public License
24    along with this program; if not, write to the Free Software
25    Foundation, Inc., 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.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->dynamic_adjusted)
1023     {
1024       /* If called to transfer flags for a weakdef during processing
1025          of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1026          We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
1027       dir->ref_dynamic |= ind->ref_dynamic;
1028       dir->ref_regular |= ind->ref_regular;
1029       dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1030       dir->needs_plt |= ind->needs_plt;
1031     }
1032   else
1033     _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
1034 }
1035
1036 /* Look through the relocs for a section during the first phase, and
1037    calculate needed space in the global offset table, procedure linkage
1038    table, and dynamic reloc sections.  At this point we haven't
1039    necessarily read all the input files.  */
1040
1041 static bfd_boolean
1042 elf32_hppa_check_relocs (bfd *abfd,
1043                          struct bfd_link_info *info,
1044                          asection *sec,
1045                          const Elf_Internal_Rela *relocs)
1046 {
1047   Elf_Internal_Shdr *symtab_hdr;
1048   struct elf_link_hash_entry **sym_hashes;
1049   const Elf_Internal_Rela *rel;
1050   const Elf_Internal_Rela *rel_end;
1051   struct elf32_hppa_link_hash_table *htab;
1052   asection *sreloc;
1053   asection *stubreloc;
1054
1055   if (info->relocatable)
1056     return TRUE;
1057
1058   htab = hppa_link_hash_table (info);
1059   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1060   sym_hashes = elf_sym_hashes (abfd);
1061   sreloc = NULL;
1062   stubreloc = NULL;
1063
1064   rel_end = relocs + sec->reloc_count;
1065   for (rel = relocs; rel < rel_end; rel++)
1066     {
1067       enum {
1068         NEED_GOT = 1,
1069         NEED_PLT = 2,
1070         NEED_DYNREL = 4,
1071         PLT_PLABEL = 8
1072       };
1073
1074       unsigned int r_symndx, r_type;
1075       struct elf32_hppa_link_hash_entry *h;
1076       int need_entry;
1077
1078       r_symndx = ELF32_R_SYM (rel->r_info);
1079
1080       if (r_symndx < symtab_hdr->sh_info)
1081         h = NULL;
1082       else
1083         h = ((struct elf32_hppa_link_hash_entry *)
1084              sym_hashes[r_symndx - symtab_hdr->sh_info]);
1085
1086       r_type = ELF32_R_TYPE (rel->r_info);
1087
1088       switch (r_type)
1089         {
1090         case R_PARISC_DLTIND14F:
1091         case R_PARISC_DLTIND14R:
1092         case R_PARISC_DLTIND21L:
1093           /* This symbol requires a global offset table entry.  */
1094           need_entry = NEED_GOT;
1095           break;
1096
1097         case R_PARISC_PLABEL14R: /* "Official" procedure labels.  */
1098         case R_PARISC_PLABEL21L:
1099         case R_PARISC_PLABEL32:
1100           /* If the addend is non-zero, we break badly.  */
1101           if (rel->r_addend != 0)
1102             abort ();
1103
1104           /* If we are creating a shared library, then we need to
1105              create a PLT entry for all PLABELs, because PLABELs with
1106              local symbols may be passed via a pointer to another
1107              object.  Additionally, output a dynamic relocation
1108              pointing to the PLT entry.
1109              For executables, the original 32-bit ABI allowed two
1110              different styles of PLABELs (function pointers):  For
1111              global functions, the PLABEL word points into the .plt
1112              two bytes past a (function address, gp) pair, and for
1113              local functions the PLABEL points directly at the
1114              function.  The magic +2 for the first type allows us to
1115              differentiate between the two.  As you can imagine, this
1116              is a real pain when it comes to generating code to call
1117              functions indirectly or to compare function pointers.
1118              We avoid the mess by always pointing a PLABEL into the
1119              .plt, even for local functions.  */
1120           need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1121           break;
1122
1123         case R_PARISC_PCREL12F:
1124           htab->has_12bit_branch = 1;
1125           goto branch_common;
1126
1127         case R_PARISC_PCREL17C:
1128         case R_PARISC_PCREL17F:
1129           htab->has_17bit_branch = 1;
1130           goto branch_common;
1131
1132         case R_PARISC_PCREL22F:
1133           htab->has_22bit_branch = 1;
1134         branch_common:
1135           /* Function calls might need to go through the .plt, and
1136              might require long branch stubs.  */
1137           if (h == NULL)
1138             {
1139               /* We know local syms won't need a .plt entry, and if
1140                  they need a long branch stub we can't guarantee that
1141                  we can reach the stub.  So just flag an error later
1142                  if we're doing a shared link and find we need a long
1143                  branch stub.  */
1144               continue;
1145             }
1146           else
1147             {
1148               /* Global symbols will need a .plt entry if they remain
1149                  global, and in most cases won't need a long branch
1150                  stub.  Unfortunately, we have to cater for the case
1151                  where a symbol is forced local by versioning, or due
1152                  to symbolic linking, and we lose the .plt entry.  */
1153               need_entry = NEED_PLT;
1154               if (h->elf.type == STT_PARISC_MILLI)
1155                 need_entry = 0;
1156             }
1157           break;
1158
1159         case R_PARISC_SEGBASE:  /* Used to set segment base.  */
1160         case R_PARISC_SEGREL32: /* Relative reloc, used for unwind.  */
1161         case R_PARISC_PCREL14F: /* PC relative load/store.  */
1162         case R_PARISC_PCREL14R:
1163         case R_PARISC_PCREL17R: /* External branches.  */
1164         case R_PARISC_PCREL21L: /* As above, and for load/store too.  */
1165         case R_PARISC_PCREL32:
1166           /* We don't need to propagate the relocation if linking a
1167              shared object since these are section relative.  */
1168           continue;
1169
1170         case R_PARISC_DPREL14F: /* Used for gp rel data load/store.  */
1171         case R_PARISC_DPREL14R:
1172         case R_PARISC_DPREL21L:
1173           if (info->shared)
1174             {
1175               (*_bfd_error_handler)
1176                 (_("%B: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1177                  abfd,
1178                  elf_hppa_howto_table[r_type].name);
1179               bfd_set_error (bfd_error_bad_value);
1180               return FALSE;
1181             }
1182           /* Fall through.  */
1183
1184         case R_PARISC_DIR17F: /* Used for external branches.  */
1185         case R_PARISC_DIR17R:
1186         case R_PARISC_DIR14F: /* Used for load/store from absolute locn.  */
1187         case R_PARISC_DIR14R:
1188         case R_PARISC_DIR21L: /* As above, and for ext branches too.  */
1189         case R_PARISC_DIR32: /* .word relocs.  */
1190           /* We may want to output a dynamic relocation later.  */
1191           need_entry = NEED_DYNREL;
1192           break;
1193
1194           /* This relocation describes the C++ object vtable hierarchy.
1195              Reconstruct it for later use during GC.  */
1196         case R_PARISC_GNU_VTINHERIT:
1197           if (!bfd_elf_gc_record_vtinherit (abfd, sec, &h->elf, rel->r_offset))
1198             return FALSE;
1199           continue;
1200
1201           /* This relocation describes which C++ vtable entries are actually
1202              used.  Record for later use during GC.  */
1203         case R_PARISC_GNU_VTENTRY:
1204           if (!bfd_elf_gc_record_vtentry (abfd, sec, &h->elf, rel->r_addend))
1205             return FALSE;
1206           continue;
1207
1208         default:
1209           continue;
1210         }
1211
1212       /* Now carry out our orders.  */
1213       if (need_entry & NEED_GOT)
1214         {
1215           /* Allocate space for a GOT entry, as well as a dynamic
1216              relocation for this entry.  */
1217           if (htab->sgot == NULL)
1218             {
1219               if (htab->elf.dynobj == NULL)
1220                 htab->elf.dynobj = abfd;
1221               if (!elf32_hppa_create_dynamic_sections (htab->elf.dynobj, info))
1222                 return FALSE;
1223             }
1224
1225           if (h != NULL)
1226             {
1227               h->elf.got.refcount += 1;
1228             }
1229           else
1230             {
1231               bfd_signed_vma *local_got_refcounts;
1232
1233               /* This is a global offset table entry for a local symbol.  */
1234               local_got_refcounts = elf_local_got_refcounts (abfd);
1235               if (local_got_refcounts == NULL)
1236                 {
1237                   bfd_size_type size;
1238
1239                   /* Allocate space for local got offsets and local
1240                      plt offsets.  Done this way to save polluting
1241                      elf_obj_tdata with another target specific
1242                      pointer.  */
1243                   size = symtab_hdr->sh_info;
1244                   size *= 2 * sizeof (bfd_signed_vma);
1245                   local_got_refcounts = bfd_zalloc (abfd, size);
1246                   if (local_got_refcounts == NULL)
1247                     return FALSE;
1248                   elf_local_got_refcounts (abfd) = local_got_refcounts;
1249                 }
1250               local_got_refcounts[r_symndx] += 1;
1251             }
1252         }
1253
1254       if (need_entry & NEED_PLT)
1255         {
1256           /* If we are creating a shared library, and this is a reloc
1257              against a weak symbol or a global symbol in a dynamic
1258              object, then we will be creating an import stub and a
1259              .plt entry for the symbol.  Similarly, on a normal link
1260              to symbols defined in a dynamic object we'll need the
1261              import stub and a .plt entry.  We don't know yet whether
1262              the symbol is defined or not, so make an entry anyway and
1263              clean up later in adjust_dynamic_symbol.  */
1264           if ((sec->flags & SEC_ALLOC) != 0)
1265             {
1266               if (h != NULL)
1267                 {
1268                   h->elf.needs_plt = 1;
1269                   h->elf.plt.refcount += 1;
1270
1271                   /* If this .plt entry is for a plabel, mark it so
1272                      that adjust_dynamic_symbol will keep the entry
1273                      even if it appears to be local.  */
1274                   if (need_entry & PLT_PLABEL)
1275                     h->plabel = 1;
1276                 }
1277               else if (need_entry & PLT_PLABEL)
1278                 {
1279                   bfd_signed_vma *local_got_refcounts;
1280                   bfd_signed_vma *local_plt_refcounts;
1281
1282                   local_got_refcounts = elf_local_got_refcounts (abfd);
1283                   if (local_got_refcounts == NULL)
1284                     {
1285                       bfd_size_type size;
1286
1287                       /* Allocate space for local got offsets and local
1288                          plt offsets.  */
1289                       size = symtab_hdr->sh_info;
1290                       size *= 2 * sizeof (bfd_signed_vma);
1291                       local_got_refcounts = bfd_zalloc (abfd, size);
1292                       if (local_got_refcounts == NULL)
1293                         return FALSE;
1294                       elf_local_got_refcounts (abfd) = local_got_refcounts;
1295                     }
1296                   local_plt_refcounts = (local_got_refcounts
1297                                          + symtab_hdr->sh_info);
1298                   local_plt_refcounts[r_symndx] += 1;
1299                 }
1300             }
1301         }
1302
1303       if (need_entry & NEED_DYNREL)
1304         {
1305           /* Flag this symbol as having a non-got, non-plt reference
1306              so that we generate copy relocs if it turns out to be
1307              dynamic.  */
1308           if (h != NULL && !info->shared)
1309             h->elf.non_got_ref = 1;
1310
1311           /* If we are creating a shared library then we need to copy
1312              the reloc into the shared library.  However, if we are
1313              linking with -Bsymbolic, we need only copy absolute
1314              relocs or relocs against symbols that are not defined in
1315              an object we are including in the link.  PC- or DP- or
1316              DLT-relative relocs against any local sym or global sym
1317              with DEF_REGULAR set, can be discarded.  At this point we
1318              have not seen all the input files, so it is possible that
1319              DEF_REGULAR is not set now but will be set later (it is
1320              never cleared).  We account for that possibility below by
1321              storing information in the dyn_relocs field of the
1322              hash table entry.
1323
1324              A similar situation to the -Bsymbolic case occurs when
1325              creating shared libraries and symbol visibility changes
1326              render the symbol local.
1327
1328              As it turns out, all the relocs we will be creating here
1329              are absolute, so we cannot remove them on -Bsymbolic
1330              links or visibility changes anyway.  A STUB_REL reloc
1331              is absolute too, as in that case it is the reloc in the
1332              stub we will be creating, rather than copying the PCREL
1333              reloc in the branch.
1334
1335              If on the other hand, we are creating an executable, we
1336              may need to keep relocations for symbols satisfied by a
1337              dynamic library if we manage to avoid copy relocs for the
1338              symbol.  */
1339           if ((info->shared
1340                && (sec->flags & SEC_ALLOC) != 0
1341                && (IS_ABSOLUTE_RELOC (r_type)
1342                    || (h != NULL
1343                        && (!info->symbolic
1344                            || h->elf.root.type == bfd_link_hash_defweak
1345                            || !h->elf.def_regular))))
1346               || (ELIMINATE_COPY_RELOCS
1347                   && !info->shared
1348                   && (sec->flags & SEC_ALLOC) != 0
1349                   && h != NULL
1350                   && (h->elf.root.type == bfd_link_hash_defweak
1351                       || !h->elf.def_regular)))
1352             {
1353               struct elf32_hppa_dyn_reloc_entry *p;
1354               struct elf32_hppa_dyn_reloc_entry **head;
1355
1356               /* Create a reloc section in dynobj and make room for
1357                  this reloc.  */
1358               if (sreloc == NULL)
1359                 {
1360                   char *name;
1361                   bfd *dynobj;
1362
1363                   name = (bfd_elf_string_from_elf_section
1364                           (abfd,
1365                            elf_elfheader (abfd)->e_shstrndx,
1366                            elf_section_data (sec)->rel_hdr.sh_name));
1367                   if (name == NULL)
1368                     {
1369                       (*_bfd_error_handler)
1370                         (_("Could not find relocation section for %s"),
1371                          sec->name);
1372                       bfd_set_error (bfd_error_bad_value);
1373                       return FALSE;
1374                     }
1375
1376                   if (htab->elf.dynobj == NULL)
1377                     htab->elf.dynobj = abfd;
1378
1379                   dynobj = htab->elf.dynobj;
1380                   sreloc = bfd_get_section_by_name (dynobj, name);
1381                   if (sreloc == NULL)
1382                     {
1383                       flagword flags;
1384
1385                       sreloc = bfd_make_section (dynobj, name);
1386                       flags = (SEC_HAS_CONTENTS | SEC_READONLY
1387                                | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1388                       if ((sec->flags & SEC_ALLOC) != 0)
1389                         flags |= SEC_ALLOC | SEC_LOAD;
1390                       if (sreloc == NULL
1391                           || !bfd_set_section_flags (dynobj, sreloc, flags)
1392                           || !bfd_set_section_alignment (dynobj, sreloc, 2))
1393                         return FALSE;
1394                     }
1395
1396                   elf_section_data (sec)->sreloc = sreloc;
1397                 }
1398
1399               /* If this is a global symbol, we count the number of
1400                  relocations we need for this symbol.  */
1401               if (h != NULL)
1402                 {
1403                   head = &h->dyn_relocs;
1404                 }
1405               else
1406                 {
1407                   /* Track dynamic relocs needed for local syms too.
1408                      We really need local syms available to do this
1409                      easily.  Oh well.  */
1410
1411                   asection *s;
1412                   s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1413                                                  sec, r_symndx);
1414                   if (s == NULL)
1415                     return FALSE;
1416
1417                   head = ((struct elf32_hppa_dyn_reloc_entry **)
1418                           &elf_section_data (s)->local_dynrel);
1419                 }
1420
1421               p = *head;
1422               if (p == NULL || p->sec != sec)
1423                 {
1424                   p = bfd_alloc (htab->elf.dynobj, sizeof *p);
1425                   if (p == NULL)
1426                     return FALSE;
1427                   p->next = *head;
1428                   *head = p;
1429                   p->sec = sec;
1430                   p->count = 0;
1431 #if RELATIVE_DYNRELOCS
1432                   p->relative_count = 0;
1433 #endif
1434                 }
1435
1436               p->count += 1;
1437 #if RELATIVE_DYNRELOCS
1438               if (!IS_ABSOLUTE_RELOC (rtype))
1439                 p->relative_count += 1;
1440 #endif
1441             }
1442         }
1443     }
1444
1445   return TRUE;
1446 }
1447
1448 /* Return the section that should be marked against garbage collection
1449    for a given relocation.  */
1450
1451 static asection *
1452 elf32_hppa_gc_mark_hook (asection *sec,
1453                          struct bfd_link_info *info ATTRIBUTE_UNUSED,
1454                          Elf_Internal_Rela *rel,
1455                          struct elf_link_hash_entry *h,
1456                          Elf_Internal_Sym *sym)
1457 {
1458   if (h != NULL)
1459     {
1460       switch ((unsigned int) ELF32_R_TYPE (rel->r_info))
1461         {
1462         case R_PARISC_GNU_VTINHERIT:
1463         case R_PARISC_GNU_VTENTRY:
1464           break;
1465
1466         default:
1467           switch (h->root.type)
1468             {
1469             case bfd_link_hash_defined:
1470             case bfd_link_hash_defweak:
1471               return h->root.u.def.section;
1472
1473             case bfd_link_hash_common:
1474               return h->root.u.c.p->section;
1475
1476             default:
1477               break;
1478             }
1479         }
1480     }
1481   else
1482     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1483
1484   return NULL;
1485 }
1486
1487 /* Update the got and plt entry reference counts for the section being
1488    removed.  */
1489
1490 static bfd_boolean
1491 elf32_hppa_gc_sweep_hook (bfd *abfd,
1492                           struct bfd_link_info *info ATTRIBUTE_UNUSED,
1493                           asection *sec,
1494                           const Elf_Internal_Rela *relocs)
1495 {
1496   Elf_Internal_Shdr *symtab_hdr;
1497   struct elf_link_hash_entry **sym_hashes;
1498   bfd_signed_vma *local_got_refcounts;
1499   bfd_signed_vma *local_plt_refcounts;
1500   const Elf_Internal_Rela *rel, *relend;
1501
1502   elf_section_data (sec)->local_dynrel = NULL;
1503
1504   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1505   sym_hashes = elf_sym_hashes (abfd);
1506   local_got_refcounts = elf_local_got_refcounts (abfd);
1507   local_plt_refcounts = local_got_refcounts;
1508   if (local_plt_refcounts != NULL)
1509     local_plt_refcounts += symtab_hdr->sh_info;
1510
1511   relend = relocs + sec->reloc_count;
1512   for (rel = relocs; rel < relend; rel++)
1513     {
1514       unsigned long r_symndx;
1515       unsigned int r_type;
1516       struct elf_link_hash_entry *h = NULL;
1517
1518       r_symndx = ELF32_R_SYM (rel->r_info);
1519       if (r_symndx >= symtab_hdr->sh_info)
1520         {
1521           struct elf32_hppa_link_hash_entry *eh;
1522           struct elf32_hppa_dyn_reloc_entry **pp;
1523           struct elf32_hppa_dyn_reloc_entry *p;
1524
1525           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1526           while (h->root.type == bfd_link_hash_indirect
1527                  || h->root.type == bfd_link_hash_warning)
1528             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1529           eh = (struct elf32_hppa_link_hash_entry *) h;
1530
1531           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1532             if (p->sec == sec)
1533               {
1534                 /* Everything must go for SEC.  */
1535                 *pp = p->next;
1536                 break;
1537               }
1538         }
1539
1540       r_type = ELF32_R_TYPE (rel->r_info);
1541       switch (r_type)
1542         {
1543         case R_PARISC_DLTIND14F:
1544         case R_PARISC_DLTIND14R:
1545         case R_PARISC_DLTIND21L:
1546           if (h != NULL)
1547             {
1548               if (h->got.refcount > 0)
1549                 h->got.refcount -= 1;
1550             }
1551           else if (local_got_refcounts != NULL)
1552             {
1553               if (local_got_refcounts[r_symndx] > 0)
1554                 local_got_refcounts[r_symndx] -= 1;
1555             }
1556           break;
1557
1558         case R_PARISC_PCREL12F:
1559         case R_PARISC_PCREL17C:
1560         case R_PARISC_PCREL17F:
1561         case R_PARISC_PCREL22F:
1562           if (h != NULL)
1563             {
1564               if (h->plt.refcount > 0)
1565                 h->plt.refcount -= 1;
1566             }
1567           break;
1568
1569         case R_PARISC_PLABEL14R:
1570         case R_PARISC_PLABEL21L:
1571         case R_PARISC_PLABEL32:
1572           if (h != NULL)
1573             {
1574               if (h->plt.refcount > 0)
1575                 h->plt.refcount -= 1;
1576             }
1577           else if (local_plt_refcounts != NULL)
1578             {
1579               if (local_plt_refcounts[r_symndx] > 0)
1580                 local_plt_refcounts[r_symndx] -= 1;
1581             }
1582           break;
1583
1584         default:
1585           break;
1586         }
1587     }
1588
1589   return TRUE;
1590 }
1591
1592 /* Support for core dump NOTE sections.  */
1593
1594 static bfd_boolean
1595 elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1596 {
1597   int offset;
1598   size_t size;
1599
1600   switch (note->descsz)
1601     {
1602       default:
1603         return FALSE;
1604
1605       case 396:         /* Linux/hppa */
1606         /* pr_cursig */
1607         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
1608
1609         /* pr_pid */
1610         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 24);
1611
1612         /* pr_reg */
1613         offset = 72;
1614         size = 320;
1615
1616         break;
1617     }
1618
1619   /* Make a ".reg/999" section.  */
1620   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1621                                           size, note->descpos + offset);
1622 }
1623
1624 static bfd_boolean
1625 elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1626 {
1627   switch (note->descsz)
1628     {
1629       default:
1630         return FALSE;
1631
1632       case 124:         /* Linux/hppa elf_prpsinfo.  */
1633         elf_tdata (abfd)->core_program
1634           = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1635         elf_tdata (abfd)->core_command
1636           = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1637     }
1638
1639   /* Note that for some reason, a spurious space is tacked
1640      onto the end of the args in some (at least one anyway)
1641      implementations, so strip it off if it exists.  */
1642   {
1643     char *command = elf_tdata (abfd)->core_command;
1644     int n = strlen (command);
1645
1646     if (0 < n && command[n - 1] == ' ')
1647       command[n - 1] = '\0';
1648   }
1649
1650   return TRUE;
1651 }
1652
1653 /* Our own version of hide_symbol, so that we can keep plt entries for
1654    plabels.  */
1655
1656 static void
1657 elf32_hppa_hide_symbol (struct bfd_link_info *info,
1658                         struct elf_link_hash_entry *h,
1659                         bfd_boolean force_local)
1660 {
1661   if (force_local)
1662     {
1663       h->forced_local = 1;
1664       if (h->dynindx != -1)
1665         {
1666           h->dynindx = -1;
1667           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1668                                   h->dynstr_index);
1669         }
1670     }
1671
1672   if (! ((struct elf32_hppa_link_hash_entry *) h)->plabel)
1673     {
1674       h->needs_plt = 0;
1675       h->plt = elf_hash_table (info)->init_refcount;
1676     }
1677 }
1678
1679 /* Adjust a symbol defined by a dynamic object and referenced by a
1680    regular object.  The current definition is in some section of the
1681    dynamic object, but we're not including those sections.  We have to
1682    change the definition to something the rest of the link can
1683    understand.  */
1684
1685 static bfd_boolean
1686 elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
1687                                   struct elf_link_hash_entry *h)
1688 {
1689   struct elf32_hppa_link_hash_table *htab;
1690   asection *s;
1691   unsigned int power_of_two;
1692
1693   /* If this is a function, put it in the procedure linkage table.  We
1694      will fill in the contents of the procedure linkage table later.  */
1695   if (h->type == STT_FUNC
1696       || h->needs_plt)
1697     {
1698       if (h->plt.refcount <= 0
1699           || (h->def_regular
1700               && h->root.type != bfd_link_hash_defweak
1701               && ! ((struct elf32_hppa_link_hash_entry *) h)->plabel
1702               && (!info->shared || info->symbolic)))
1703         {
1704           /* The .plt entry is not needed when:
1705              a) Garbage collection has removed all references to the
1706              symbol, or
1707              b) We know for certain the symbol is defined in this
1708              object, and it's not a weak definition, nor is the symbol
1709              used by a plabel relocation.  Either this object is the
1710              application or we are doing a shared symbolic link.  */
1711
1712           h->plt.offset = (bfd_vma) -1;
1713           h->needs_plt = 0;
1714         }
1715
1716       return TRUE;
1717     }
1718   else
1719     h->plt.offset = (bfd_vma) -1;
1720
1721   /* If this is a weak symbol, and there is a real definition, the
1722      processor independent code will have arranged for us to see the
1723      real definition first, and we can just use the same value.  */
1724   if (h->u.weakdef != NULL)
1725     {
1726       if (h->u.weakdef->root.type != bfd_link_hash_defined
1727           && h->u.weakdef->root.type != bfd_link_hash_defweak)
1728         abort ();
1729       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1730       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1731       if (ELIMINATE_COPY_RELOCS)
1732         h->non_got_ref = h->u.weakdef->non_got_ref;
1733       return TRUE;
1734     }
1735
1736   /* This is a reference to a symbol defined by a dynamic object which
1737      is not a function.  */
1738
1739   /* If we are creating a shared library, we must presume that the
1740      only references to the symbol are via the global offset table.
1741      For such cases we need not do anything here; the relocations will
1742      be handled correctly by relocate_section.  */
1743   if (info->shared)
1744     return TRUE;
1745
1746   /* If there are no references to this symbol that do not use the
1747      GOT, we don't need to generate a copy reloc.  */
1748   if (!h->non_got_ref)
1749     return TRUE;
1750
1751   if (ELIMINATE_COPY_RELOCS)
1752     {
1753       struct elf32_hppa_link_hash_entry *eh;
1754       struct elf32_hppa_dyn_reloc_entry *p;
1755
1756       eh = (struct elf32_hppa_link_hash_entry *) h;
1757       for (p = eh->dyn_relocs; p != NULL; p = p->next)
1758         {
1759           s = p->sec->output_section;
1760           if (s != NULL && (s->flags & SEC_READONLY) != 0)
1761             break;
1762         }
1763
1764       /* If we didn't find any dynamic relocs in read-only sections, then
1765          we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
1766       if (p == NULL)
1767         {
1768           h->non_got_ref = 0;
1769           return TRUE;
1770         }
1771     }
1772
1773   /* We must allocate the symbol in our .dynbss section, which will
1774      become part of the .bss section of the executable.  There will be
1775      an entry for this symbol in the .dynsym section.  The dynamic
1776      object will contain position independent code, so all references
1777      from the dynamic object to this symbol will go through the global
1778      offset table.  The dynamic linker will use the .dynsym entry to
1779      determine the address it must put in the global offset table, so
1780      both the dynamic object and the regular object will refer to the
1781      same memory location for the variable.  */
1782
1783   htab = hppa_link_hash_table (info);
1784
1785   /* We must generate a COPY reloc to tell the dynamic linker to
1786      copy the initial value out of the dynamic object and into the
1787      runtime process image.  */
1788   if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1789     {
1790       htab->srelbss->size += sizeof (Elf32_External_Rela);
1791       h->needs_copy = 1;
1792     }
1793
1794   /* We need to figure out the alignment required for this symbol.  I
1795      have no idea how other ELF linkers handle this.  */
1796
1797   power_of_two = bfd_log2 (h->size);
1798   if (power_of_two > 3)
1799     power_of_two = 3;
1800
1801   /* Apply the required alignment.  */
1802   s = htab->sdynbss;
1803   s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1804   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1805     {
1806       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1807         return FALSE;
1808     }
1809
1810   /* Define the symbol as being at this point in the section.  */
1811   h->root.u.def.section = s;
1812   h->root.u.def.value = s->size;
1813
1814   /* Increment the section size to make room for the symbol.  */
1815   s->size += h->size;
1816
1817   return TRUE;
1818 }
1819
1820 /* Allocate space in the .plt for entries that won't have relocations.
1821    ie. plabel entries.  */
1822
1823 static bfd_boolean
1824 allocate_plt_static (struct elf_link_hash_entry *h, void *inf)
1825 {
1826   struct bfd_link_info *info;
1827   struct elf32_hppa_link_hash_table *htab;
1828   asection *s;
1829
1830   if (h->root.type == bfd_link_hash_indirect)
1831     return TRUE;
1832
1833   if (h->root.type == bfd_link_hash_warning)
1834     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1835
1836   info = inf;
1837   htab = hppa_link_hash_table (info);
1838   if (htab->elf.dynamic_sections_created
1839            && h->plt.refcount > 0)
1840     {
1841       /* Make sure this symbol is output as a dynamic symbol.
1842          Undefined weak syms won't yet be marked as dynamic.  */
1843       if (h->dynindx == -1
1844           && !h->forced_local
1845           && h->type != STT_PARISC_MILLI)
1846         {
1847           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1848             return FALSE;
1849         }
1850
1851       if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
1852         {
1853           /* Allocate these later.  From this point on, h->plabel
1854              means that the plt entry is only used by a plabel.
1855              We'll be using a normal plt entry for this symbol, so
1856              clear the plabel indicator.  */
1857           ((struct elf32_hppa_link_hash_entry *) h)->plabel = 0;
1858         }
1859       else if (((struct elf32_hppa_link_hash_entry *) h)->plabel)
1860         {
1861           /* Make an entry in the .plt section for plabel references
1862              that won't have a .plt entry for other reasons.  */
1863           s = htab->splt;
1864           h->plt.offset = s->size;
1865           s->size += PLT_ENTRY_SIZE;
1866         }
1867       else
1868         {
1869           /* No .plt entry needed.  */
1870           h->plt.offset = (bfd_vma) -1;
1871           h->needs_plt = 0;
1872         }
1873     }
1874   else
1875     {
1876       h->plt.offset = (bfd_vma) -1;
1877       h->needs_plt = 0;
1878     }
1879
1880   return TRUE;
1881 }
1882
1883 /* Allocate space in .plt, .got and associated reloc sections for
1884    global syms.  */
1885
1886 static bfd_boolean
1887 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
1888 {
1889   struct bfd_link_info *info;
1890   struct elf32_hppa_link_hash_table *htab;
1891   asection *s;
1892   struct elf32_hppa_link_hash_entry *eh;
1893   struct elf32_hppa_dyn_reloc_entry *p;
1894
1895   if (h->root.type == bfd_link_hash_indirect)
1896     return TRUE;
1897
1898   if (h->root.type == bfd_link_hash_warning)
1899     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1900
1901   info = inf;
1902   htab = hppa_link_hash_table (info);
1903   if (htab->elf.dynamic_sections_created
1904       && h->plt.offset != (bfd_vma) -1
1905       && !((struct elf32_hppa_link_hash_entry *) h)->plabel)
1906     {
1907       /* Make an entry in the .plt section.  */
1908       s = htab->splt;
1909       h->plt.offset = s->size;
1910       s->size += PLT_ENTRY_SIZE;
1911
1912       /* We also need to make an entry in the .rela.plt section.  */
1913       htab->srelplt->size += sizeof (Elf32_External_Rela);
1914       htab->need_plt_stub = 1;
1915     }
1916
1917   if (h->got.refcount > 0)
1918     {
1919       /* Make sure this symbol is output as a dynamic symbol.
1920          Undefined weak syms won't yet be marked as dynamic.  */
1921       if (h->dynindx == -1
1922           && !h->forced_local
1923           && h->type != STT_PARISC_MILLI)
1924         {
1925           if (! bfd_elf_link_record_dynamic_symbol (info, h))
1926             return FALSE;
1927         }
1928
1929       s = htab->sgot;
1930       h->got.offset = s->size;
1931       s->size += GOT_ENTRY_SIZE;
1932       if (htab->elf.dynamic_sections_created
1933           && (info->shared
1934               || (h->dynindx != -1
1935                   && !h->forced_local)))
1936         {
1937           htab->srelgot->size += sizeof (Elf32_External_Rela);
1938         }
1939     }
1940   else
1941     h->got.offset = (bfd_vma) -1;
1942
1943   eh = (struct elf32_hppa_link_hash_entry *) h;
1944   if (eh->dyn_relocs == NULL)
1945     return TRUE;
1946
1947   /* If this is a -Bsymbolic shared link, then we need to discard all
1948      space allocated for dynamic pc-relative relocs against symbols
1949      defined in a regular object.  For the normal shared case, discard
1950      space for relocs that have become local due to symbol visibility
1951      changes.  */
1952   if (info->shared)
1953     {
1954 #if RELATIVE_DYNRELOCS
1955       if (SYMBOL_CALLS_LOCAL (info, h))
1956         {
1957           struct elf32_hppa_dyn_reloc_entry **pp;
1958
1959           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1960             {
1961               p->count -= p->relative_count;
1962               p->relative_count = 0;
1963               if (p->count == 0)
1964                 *pp = p->next;
1965               else
1966                 pp = &p->next;
1967             }
1968         }
1969 #endif
1970
1971       /* Also discard relocs on undefined weak syms with non-default
1972          visibility.  */
1973       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1974           && h->root.type == bfd_link_hash_undefweak)
1975         eh->dyn_relocs = NULL;
1976     }
1977   else
1978     {
1979       /* For the non-shared case, discard space for relocs against
1980          symbols which turn out to need copy relocs or are not
1981          dynamic.  */
1982       if (!h->non_got_ref
1983           && ((ELIMINATE_COPY_RELOCS
1984                && h->def_dynamic
1985                && !h->def_regular)
1986               || (htab->elf.dynamic_sections_created
1987                   && (h->root.type == bfd_link_hash_undefweak
1988                       || h->root.type == bfd_link_hash_undefined))))
1989         {
1990           /* Make sure this symbol is output as a dynamic symbol.
1991              Undefined weak syms won't yet be marked as dynamic.  */
1992           if (h->dynindx == -1
1993               && !h->forced_local
1994               && h->type != STT_PARISC_MILLI)
1995             {
1996               if (! bfd_elf_link_record_dynamic_symbol (info, h))
1997                 return FALSE;
1998             }
1999
2000           /* If that succeeded, we know we'll be keeping all the
2001              relocs.  */
2002           if (h->dynindx != -1)
2003             goto keep;
2004         }
2005
2006       eh->dyn_relocs = NULL;
2007       return TRUE;
2008
2009     keep: ;
2010     }
2011
2012   /* Finally, allocate space.  */
2013   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2014     {
2015       asection *sreloc = elf_section_data (p->sec)->sreloc;
2016       sreloc->size += p->count * sizeof (Elf32_External_Rela);
2017     }
2018
2019   return TRUE;
2020 }
2021
2022 /* This function is called via elf_link_hash_traverse to force
2023    millicode symbols local so they do not end up as globals in the
2024    dynamic symbol table.  We ought to be able to do this in
2025    adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2026    for all dynamic symbols.  Arguably, this is a bug in
2027    elf_adjust_dynamic_symbol.  */
2028
2029 static bfd_boolean
2030 clobber_millicode_symbols (struct elf_link_hash_entry *h,
2031                            struct bfd_link_info *info)
2032 {
2033   if (h->root.type == bfd_link_hash_warning)
2034     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2035
2036   if (h->type == STT_PARISC_MILLI
2037       && !h->forced_local)
2038     {
2039       elf32_hppa_hide_symbol (info, h, TRUE);
2040     }
2041   return TRUE;
2042 }
2043
2044 /* Find any dynamic relocs that apply to read-only sections.  */
2045
2046 static bfd_boolean
2047 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2048 {
2049   struct elf32_hppa_link_hash_entry *eh;
2050   struct elf32_hppa_dyn_reloc_entry *p;
2051
2052   if (h->root.type == bfd_link_hash_warning)
2053     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2054
2055   eh = (struct elf32_hppa_link_hash_entry *) h;
2056   for (p = eh->dyn_relocs; p != NULL; p = p->next)
2057     {
2058       asection *s = p->sec->output_section;
2059
2060       if (s != NULL && (s->flags & SEC_READONLY) != 0)
2061         {
2062           struct bfd_link_info *info = inf;
2063
2064           info->flags |= DF_TEXTREL;
2065
2066           /* Not an error, just cut short the traversal.  */
2067           return FALSE;
2068         }
2069     }
2070   return TRUE;
2071 }
2072
2073 /* Set the sizes of the dynamic sections.  */
2074
2075 static bfd_boolean
2076 elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2077                                   struct bfd_link_info *info)
2078 {
2079   struct elf32_hppa_link_hash_table *htab;
2080   bfd *dynobj;
2081   bfd *ibfd;
2082   asection *s;
2083   bfd_boolean relocs;
2084
2085   htab = hppa_link_hash_table (info);
2086   dynobj = htab->elf.dynobj;
2087   if (dynobj == NULL)
2088     abort ();
2089
2090   if (htab->elf.dynamic_sections_created)
2091     {
2092       /* Set the contents of the .interp section to the interpreter.  */
2093       if (info->executable)
2094         {
2095           s = bfd_get_section_by_name (dynobj, ".interp");
2096           if (s == NULL)
2097             abort ();
2098           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2099           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2100         }
2101
2102       /* Force millicode symbols local.  */
2103       elf_link_hash_traverse (&htab->elf,
2104                               clobber_millicode_symbols,
2105                               info);
2106     }
2107
2108   /* Set up .got and .plt offsets for local syms, and space for local
2109      dynamic relocs.  */
2110   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2111     {
2112       bfd_signed_vma *local_got;
2113       bfd_signed_vma *end_local_got;
2114       bfd_signed_vma *local_plt;
2115       bfd_signed_vma *end_local_plt;
2116       bfd_size_type locsymcount;
2117       Elf_Internal_Shdr *symtab_hdr;
2118       asection *srel;
2119
2120       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2121         continue;
2122
2123       for (s = ibfd->sections; s != NULL; s = s->next)
2124         {
2125           struct elf32_hppa_dyn_reloc_entry *p;
2126
2127           for (p = ((struct elf32_hppa_dyn_reloc_entry *)
2128                     elf_section_data (s)->local_dynrel);
2129                p != NULL;
2130                p = p->next)
2131             {
2132               if (!bfd_is_abs_section (p->sec)
2133                   && bfd_is_abs_section (p->sec->output_section))
2134                 {
2135                   /* Input section has been discarded, either because
2136                      it is a copy of a linkonce section or due to
2137                      linker script /DISCARD/, so we'll be discarding
2138                      the relocs too.  */
2139                 }
2140               else if (p->count != 0)
2141                 {
2142                   srel = elf_section_data (p->sec)->sreloc;
2143                   srel->size += p->count * sizeof (Elf32_External_Rela);
2144                   if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2145                     info->flags |= DF_TEXTREL;
2146                 }
2147             }
2148         }
2149
2150       local_got = elf_local_got_refcounts (ibfd);
2151       if (!local_got)
2152         continue;
2153
2154       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2155       locsymcount = symtab_hdr->sh_info;
2156       end_local_got = local_got + locsymcount;
2157       s = htab->sgot;
2158       srel = htab->srelgot;
2159       for (; local_got < end_local_got; ++local_got)
2160         {
2161           if (*local_got > 0)
2162             {
2163               *local_got = s->size;
2164               s->size += GOT_ENTRY_SIZE;
2165               if (info->shared)
2166                 srel->size += sizeof (Elf32_External_Rela);
2167             }
2168           else
2169             *local_got = (bfd_vma) -1;
2170         }
2171
2172       local_plt = end_local_got;
2173       end_local_plt = local_plt + locsymcount;
2174       if (! htab->elf.dynamic_sections_created)
2175         {
2176           /* Won't be used, but be safe.  */
2177           for (; local_plt < end_local_plt; ++local_plt)
2178             *local_plt = (bfd_vma) -1;
2179         }
2180       else
2181         {
2182           s = htab->splt;
2183           srel = htab->srelplt;
2184           for (; local_plt < end_local_plt; ++local_plt)
2185             {
2186               if (*local_plt > 0)
2187                 {
2188                   *local_plt = s->size;
2189                   s->size += PLT_ENTRY_SIZE;
2190                   if (info->shared)
2191                     srel->size += sizeof (Elf32_External_Rela);
2192                 }
2193               else
2194                 *local_plt = (bfd_vma) -1;
2195             }
2196         }
2197     }
2198
2199   /* Do all the .plt entries without relocs first.  The dynamic linker
2200      uses the last .plt reloc to find the end of the .plt (and hence
2201      the start of the .got) for lazy linking.  */
2202   elf_link_hash_traverse (&htab->elf, allocate_plt_static, info);
2203
2204   /* Allocate global sym .plt and .got entries, and space for global
2205      sym dynamic relocs.  */
2206   elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
2207
2208   /* The check_relocs and adjust_dynamic_symbol entry points have
2209      determined the sizes of the various dynamic sections.  Allocate
2210      memory for them.  */
2211   relocs = FALSE;
2212   for (s = dynobj->sections; s != NULL; s = s->next)
2213     {
2214       if ((s->flags & SEC_LINKER_CREATED) == 0)
2215         continue;
2216
2217       if (s == htab->splt)
2218         {
2219           if (htab->need_plt_stub)
2220             {
2221               /* Make space for the plt stub at the end of the .plt
2222                  section.  We want this stub right at the end, up
2223                  against the .got section.  */
2224               int gotalign = bfd_section_alignment (dynobj, htab->sgot);
2225               int pltalign = bfd_section_alignment (dynobj, s);
2226               bfd_size_type mask;
2227
2228               if (gotalign > pltalign)
2229                 bfd_set_section_alignment (dynobj, s, gotalign);
2230               mask = ((bfd_size_type) 1 << gotalign) - 1;
2231               s->size = (s->size + sizeof (plt_stub) + mask) & ~mask;
2232             }
2233         }
2234       else if (s == htab->sgot)
2235         ;
2236       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
2237         {
2238           if (s->size != 0)
2239             {
2240               /* Remember whether there are any reloc sections other
2241                  than .rela.plt.  */
2242               if (s != htab->srelplt)
2243                 relocs = TRUE;
2244
2245               /* We use the reloc_count field as a counter if we need
2246                  to copy relocs into the output file.  */
2247               s->reloc_count = 0;
2248             }
2249         }
2250       else
2251         {
2252           /* It's not one of our sections, so don't allocate space.  */
2253           continue;
2254         }
2255
2256       if (s->size == 0)
2257         {
2258           /* If we don't need this section, strip it from the
2259              output file.  This is mostly to handle .rela.bss and
2260              .rela.plt.  We must create both sections in
2261              create_dynamic_sections, because they must be created
2262              before the linker maps input sections to output
2263              sections.  The linker does that before
2264              adjust_dynamic_symbol is called, and it is that
2265              function which decides whether anything needs to go
2266              into these sections.  */
2267           _bfd_strip_section_from_output (info, s);
2268           continue;
2269         }
2270
2271       /* Allocate memory for the section contents.  Zero it, because
2272          we may not fill in all the reloc sections.  */
2273       s->contents = bfd_zalloc (dynobj, s->size);
2274       if (s->contents == NULL && s->size != 0)
2275         return FALSE;
2276     }
2277
2278   if (htab->elf.dynamic_sections_created)
2279     {
2280       /* Like IA-64 and HPPA64, always create a DT_PLTGOT.  It
2281          actually has nothing to do with the PLT, it is how we
2282          communicate the LTP value of a load module to the dynamic
2283          linker.  */
2284 #define add_dynamic_entry(TAG, VAL) \
2285   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2286
2287       if (!add_dynamic_entry (DT_PLTGOT, 0))
2288         return FALSE;
2289
2290       /* Add some entries to the .dynamic section.  We fill in the
2291          values later, in elf32_hppa_finish_dynamic_sections, but we
2292          must add the entries now so that we get the correct size for
2293          the .dynamic section.  The DT_DEBUG entry is filled in by the
2294          dynamic linker and used by the debugger.  */
2295       if (!info->shared)
2296         {
2297           if (!add_dynamic_entry (DT_DEBUG, 0))
2298             return FALSE;
2299         }
2300
2301       if (htab->srelplt->size != 0)
2302         {
2303           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2304               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2305               || !add_dynamic_entry (DT_JMPREL, 0))
2306             return FALSE;
2307         }
2308
2309       if (relocs)
2310         {
2311           if (!add_dynamic_entry (DT_RELA, 0)
2312               || !add_dynamic_entry (DT_RELASZ, 0)
2313               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2314             return FALSE;
2315
2316           /* If any dynamic relocs apply to a read-only section,
2317              then we need a DT_TEXTREL entry.  */
2318           if ((info->flags & DF_TEXTREL) == 0)
2319             elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
2320
2321           if ((info->flags & DF_TEXTREL) != 0)
2322             {
2323               if (!add_dynamic_entry (DT_TEXTREL, 0))
2324                 return FALSE;
2325             }
2326         }
2327     }
2328 #undef add_dynamic_entry
2329
2330   return TRUE;
2331 }
2332
2333 /* External entry points for sizing and building linker stubs.  */
2334
2335 /* Set up various things so that we can make a list of input sections
2336    for each output section included in the link.  Returns -1 on error,
2337    0 when no stubs will be needed, and 1 on success.  */
2338
2339 int
2340 elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2341 {
2342   bfd *input_bfd;
2343   unsigned int bfd_count;
2344   int top_id, top_index;
2345   asection *section;
2346   asection **input_list, **list;
2347   bfd_size_type amt;
2348   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2349
2350   /* Count the number of input BFDs and find the top input section id.  */
2351   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2352        input_bfd != NULL;
2353        input_bfd = input_bfd->link_next)
2354     {
2355       bfd_count += 1;
2356       for (section = input_bfd->sections;
2357            section != NULL;
2358            section = section->next)
2359         {
2360           if (top_id < section->id)
2361             top_id = section->id;
2362         }
2363     }
2364   htab->bfd_count = bfd_count;
2365
2366   amt = sizeof (struct map_stub) * (top_id + 1);
2367   htab->stub_group = bfd_zmalloc (amt);
2368   if (htab->stub_group == NULL)
2369     return -1;
2370
2371   /* We can't use output_bfd->section_count here to find the top output
2372      section index as some sections may have been removed, and
2373      _bfd_strip_section_from_output doesn't renumber the indices.  */
2374   for (section = output_bfd->sections, top_index = 0;
2375        section != NULL;
2376        section = section->next)
2377     {
2378       if (top_index < section->index)
2379         top_index = section->index;
2380     }
2381
2382   htab->top_index = top_index;
2383   amt = sizeof (asection *) * (top_index + 1);
2384   input_list = bfd_malloc (amt);
2385   htab->input_list = input_list;
2386   if (input_list == NULL)
2387     return -1;
2388
2389   /* For sections we aren't interested in, mark their entries with a
2390      value we can check later.  */
2391   list = input_list + top_index;
2392   do
2393     *list = bfd_abs_section_ptr;
2394   while (list-- != input_list);
2395
2396   for (section = output_bfd->sections;
2397        section != NULL;
2398        section = section->next)
2399     {
2400       if ((section->flags & SEC_CODE) != 0)
2401         input_list[section->index] = NULL;
2402     }
2403
2404   return 1;
2405 }
2406
2407 /* The linker repeatedly calls this function for each input section,
2408    in the order that input sections are linked into output sections.
2409    Build lists of input sections to determine groupings between which
2410    we may insert linker stubs.  */
2411
2412 void
2413 elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
2414 {
2415   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2416
2417   if (isec->output_section->index <= htab->top_index)
2418     {
2419       asection **list = htab->input_list + isec->output_section->index;
2420       if (*list != bfd_abs_section_ptr)
2421         {
2422           /* Steal the link_sec pointer for our list.  */
2423 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2424           /* This happens to make the list in reverse order,
2425              which is what we want.  */
2426           PREV_SEC (isec) = *list;
2427           *list = isec;
2428         }
2429     }
2430 }
2431
2432 /* See whether we can group stub sections together.  Grouping stub
2433    sections may result in fewer stubs.  More importantly, we need to
2434    put all .init* and .fini* stubs at the beginning of the .init or
2435    .fini output sections respectively, because glibc splits the
2436    _init and _fini functions into multiple parts.  Putting a stub in
2437    the middle of a function is not a good idea.  */
2438
2439 static void
2440 group_sections (struct elf32_hppa_link_hash_table *htab,
2441                 bfd_size_type stub_group_size,
2442                 bfd_boolean stubs_always_before_branch)
2443 {
2444   asection **list = htab->input_list + htab->top_index;
2445   do
2446     {
2447       asection *tail = *list;
2448       if (tail == bfd_abs_section_ptr)
2449         continue;
2450       while (tail != NULL)
2451         {
2452           asection *curr;
2453           asection *prev;
2454           bfd_size_type total;
2455           bfd_boolean big_sec;
2456
2457           curr = tail;
2458           total = tail->size;
2459           big_sec = total >= stub_group_size;
2460
2461           while ((prev = PREV_SEC (curr)) != NULL
2462                  && ((total += curr->output_offset - prev->output_offset)
2463                      < stub_group_size))
2464             curr = prev;
2465
2466           /* OK, the size from the start of CURR to the end is less
2467              than 240000 bytes and thus can be handled by one stub
2468              section.  (or the tail section is itself larger than
2469              240000 bytes, in which case we may be toast.)
2470              We should really be keeping track of the total size of
2471              stubs added here, as stubs contribute to the final output
2472              section size.  That's a little tricky, and this way will
2473              only break if stubs added total more than 22144 bytes, or
2474              2768 long branch stubs.  It seems unlikely for more than
2475              2768 different functions to be called, especially from
2476              code only 240000 bytes long.  This limit used to be
2477              250000, but c++ code tends to generate lots of little
2478              functions, and sometimes violated the assumption.  */
2479           do
2480             {
2481               prev = PREV_SEC (tail);
2482               /* Set up this stub group.  */
2483               htab->stub_group[tail->id].link_sec = curr;
2484             }
2485           while (tail != curr && (tail = prev) != NULL);
2486
2487           /* But wait, there's more!  Input sections up to 240000
2488              bytes before the stub section can be handled by it too.
2489              Don't do this if we have a really large section after the
2490              stubs, as adding more stubs increases the chance that
2491              branches may not reach into the stub section.  */
2492           if (!stubs_always_before_branch && !big_sec)
2493             {
2494               total = 0;
2495               while (prev != NULL
2496                      && ((total += tail->output_offset - prev->output_offset)
2497                          < stub_group_size))
2498                 {
2499                   tail = prev;
2500                   prev = PREV_SEC (tail);
2501                   htab->stub_group[tail->id].link_sec = curr;
2502                 }
2503             }
2504           tail = prev;
2505         }
2506     }
2507   while (list-- != htab->input_list);
2508   free (htab->input_list);
2509 #undef PREV_SEC
2510 }
2511
2512 /* Read in all local syms for all input bfds, and create hash entries
2513    for export stubs if we are building a multi-subspace shared lib.
2514    Returns -1 on error, 1 if export stubs created, 0 otherwise.  */
2515
2516 static int
2517 get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
2518 {
2519   unsigned int bfd_indx;
2520   Elf_Internal_Sym *local_syms, **all_local_syms;
2521   int stub_changed = 0;
2522   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2523
2524   /* We want to read in symbol extension records only once.  To do this
2525      we need to read in the local symbols in parallel and save them for
2526      later use; so hold pointers to the local symbols in an array.  */
2527   bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2528   all_local_syms = bfd_zmalloc (amt);
2529   htab->all_local_syms = all_local_syms;
2530   if (all_local_syms == NULL)
2531     return -1;
2532
2533   /* Walk over all the input BFDs, swapping in local symbols.
2534      If we are creating a shared library, create hash entries for the
2535      export stubs.  */
2536   for (bfd_indx = 0;
2537        input_bfd != NULL;
2538        input_bfd = input_bfd->link_next, bfd_indx++)
2539     {
2540       Elf_Internal_Shdr *symtab_hdr;
2541
2542       /* We'll need the symbol table in a second.  */
2543       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2544       if (symtab_hdr->sh_info == 0)
2545         continue;
2546
2547       /* We need an array of the local symbols attached to the input bfd.  */
2548       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2549       if (local_syms == NULL)
2550         {
2551           local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2552                                              symtab_hdr->sh_info, 0,
2553                                              NULL, NULL, NULL);
2554           /* Cache them for elf_link_input_bfd.  */
2555           symtab_hdr->contents = (unsigned char *) local_syms;
2556         }
2557       if (local_syms == NULL)
2558         return -1;
2559
2560       all_local_syms[bfd_indx] = local_syms;
2561
2562       if (info->shared && htab->multi_subspace)
2563         {
2564           struct elf_link_hash_entry **sym_hashes;
2565           struct elf_link_hash_entry **end_hashes;
2566           unsigned int symcount;
2567
2568           symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2569                       - symtab_hdr->sh_info);
2570           sym_hashes = elf_sym_hashes (input_bfd);
2571           end_hashes = sym_hashes + symcount;
2572
2573           /* Look through the global syms for functions;  We need to
2574              build export stubs for all globally visible functions.  */
2575           for (; sym_hashes < end_hashes; sym_hashes++)
2576             {
2577               struct elf32_hppa_link_hash_entry *hash;
2578
2579               hash = (struct elf32_hppa_link_hash_entry *) *sym_hashes;
2580
2581               while (hash->elf.root.type == bfd_link_hash_indirect
2582                      || hash->elf.root.type == bfd_link_hash_warning)
2583                 hash = ((struct elf32_hppa_link_hash_entry *)
2584                         hash->elf.root.u.i.link);
2585
2586               /* At this point in the link, undefined syms have been
2587                  resolved, so we need to check that the symbol was
2588                  defined in this BFD.  */
2589               if ((hash->elf.root.type == bfd_link_hash_defined
2590                    || hash->elf.root.type == bfd_link_hash_defweak)
2591                   && hash->elf.type == STT_FUNC
2592                   && hash->elf.root.u.def.section->output_section != NULL
2593                   && (hash->elf.root.u.def.section->output_section->owner
2594                       == output_bfd)
2595                   && hash->elf.root.u.def.section->owner == input_bfd
2596                   && hash->elf.def_regular
2597                   && !hash->elf.forced_local
2598                   && ELF_ST_VISIBILITY (hash->elf.other) == STV_DEFAULT)
2599                 {
2600                   asection *sec;
2601                   const char *stub_name;
2602                   struct elf32_hppa_stub_hash_entry *stub_entry;
2603
2604                   sec = hash->elf.root.u.def.section;
2605                   stub_name = hash->elf.root.root.string;
2606                   stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
2607                                                       stub_name,
2608                                                       FALSE, FALSE);
2609                   if (stub_entry == NULL)
2610                     {
2611                       stub_entry = hppa_add_stub (stub_name, sec, htab);
2612                       if (!stub_entry)
2613                         return -1;
2614
2615                       stub_entry->target_value = hash->elf.root.u.def.value;
2616                       stub_entry->target_section = hash->elf.root.u.def.section;
2617                       stub_entry->stub_type = hppa_stub_export;
2618                       stub_entry->h = hash;
2619                       stub_changed = 1;
2620                     }
2621                   else
2622                     {
2623                       (*_bfd_error_handler) (_("%B: duplicate export stub %s"),
2624                                              input_bfd,
2625                                              stub_name);
2626                     }
2627                 }
2628             }
2629         }
2630     }
2631
2632   return stub_changed;
2633 }
2634
2635 /* Determine and set the size of the stub section for a final link.
2636
2637    The basic idea here is to examine all the relocations looking for
2638    PC-relative calls to a target that is unreachable with a "bl"
2639    instruction.  */
2640
2641 bfd_boolean
2642 elf32_hppa_size_stubs
2643   (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2644    bfd_boolean multi_subspace, bfd_signed_vma group_size,
2645    asection * (*add_stub_section) (const char *, asection *),
2646    void (*layout_sections_again) (void))
2647 {
2648   bfd_size_type stub_group_size;
2649   bfd_boolean stubs_always_before_branch;
2650   bfd_boolean stub_changed;
2651   struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2652
2653   /* Stash our params away.  */
2654   htab->stub_bfd = stub_bfd;
2655   htab->multi_subspace = multi_subspace;
2656   htab->add_stub_section = add_stub_section;
2657   htab->layout_sections_again = layout_sections_again;
2658   stubs_always_before_branch = group_size < 0;
2659   if (group_size < 0)
2660     stub_group_size = -group_size;
2661   else
2662     stub_group_size = group_size;
2663   if (stub_group_size == 1)
2664     {
2665       /* Default values.  */
2666       if (stubs_always_before_branch)
2667         {
2668           stub_group_size = 7680000;
2669           if (htab->has_17bit_branch || htab->multi_subspace)
2670             stub_group_size = 240000;
2671           if (htab->has_12bit_branch)
2672             stub_group_size = 7500;
2673         }
2674       else
2675         {
2676           stub_group_size = 6971392;
2677           if (htab->has_17bit_branch || htab->multi_subspace)
2678             stub_group_size = 217856;
2679           if (htab->has_12bit_branch)
2680             stub_group_size = 6808;
2681         }
2682     }
2683
2684   group_sections (htab, stub_group_size, stubs_always_before_branch);
2685
2686   switch (get_local_syms (output_bfd, info->input_bfds, info))
2687     {
2688     default:
2689       if (htab->all_local_syms)
2690         goto error_ret_free_local;
2691       return FALSE;
2692
2693     case 0:
2694       stub_changed = FALSE;
2695       break;
2696
2697     case 1:
2698       stub_changed = TRUE;
2699       break;
2700     }
2701
2702   while (1)
2703     {
2704       bfd *input_bfd;
2705       unsigned int bfd_indx;
2706       asection *stub_sec;
2707
2708       for (input_bfd = info->input_bfds, bfd_indx = 0;
2709            input_bfd != NULL;
2710            input_bfd = input_bfd->link_next, bfd_indx++)
2711         {
2712           Elf_Internal_Shdr *symtab_hdr;
2713           asection *section;
2714           Elf_Internal_Sym *local_syms;
2715
2716           /* We'll need the symbol table in a second.  */
2717           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2718           if (symtab_hdr->sh_info == 0)
2719             continue;
2720
2721           local_syms = htab->all_local_syms[bfd_indx];
2722
2723           /* Walk over each section attached to the input bfd.  */
2724           for (section = input_bfd->sections;
2725                section != NULL;
2726                section = section->next)
2727             {
2728               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2729
2730               /* If there aren't any relocs, then there's nothing more
2731                  to do.  */
2732               if ((section->flags & SEC_RELOC) == 0
2733                   || section->reloc_count == 0)
2734                 continue;
2735
2736               /* If this section is a link-once section that will be
2737                  discarded, then don't create any stubs.  */
2738               if (section->output_section == NULL
2739                   || section->output_section->owner != output_bfd)
2740                 continue;
2741
2742               /* Get the relocs.  */
2743               internal_relocs
2744                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2745                                              info->keep_memory);
2746               if (internal_relocs == NULL)
2747                 goto error_ret_free_local;
2748
2749               /* Now examine each relocation.  */
2750               irela = internal_relocs;
2751               irelaend = irela + section->reloc_count;
2752               for (; irela < irelaend; irela++)
2753                 {
2754                   unsigned int r_type, r_indx;
2755                   enum elf32_hppa_stub_type stub_type;
2756                   struct elf32_hppa_stub_hash_entry *stub_entry;
2757                   asection *sym_sec;
2758                   bfd_vma sym_value;
2759                   bfd_vma destination;
2760                   struct elf32_hppa_link_hash_entry *hash;
2761                   char *stub_name;
2762                   const asection *id_sec;
2763
2764                   r_type = ELF32_R_TYPE (irela->r_info);
2765                   r_indx = ELF32_R_SYM (irela->r_info);
2766
2767                   if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2768                     {
2769                       bfd_set_error (bfd_error_bad_value);
2770                     error_ret_free_internal:
2771                       if (elf_section_data (section)->relocs == NULL)
2772                         free (internal_relocs);
2773                       goto error_ret_free_local;
2774                     }
2775
2776                   /* Only look for stubs on call instructions.  */
2777                   if (r_type != (unsigned int) R_PARISC_PCREL12F
2778                       && r_type != (unsigned int) R_PARISC_PCREL17F
2779                       && r_type != (unsigned int) R_PARISC_PCREL22F)
2780                     continue;
2781
2782                   /* Now determine the call target, its name, value,
2783                      section.  */
2784                   sym_sec = NULL;
2785                   sym_value = 0;
2786                   destination = 0;
2787                   hash = NULL;
2788                   if (r_indx < symtab_hdr->sh_info)
2789                     {
2790                       /* It's a local symbol.  */
2791                       Elf_Internal_Sym *sym;
2792                       Elf_Internal_Shdr *hdr;
2793
2794                       sym = local_syms + r_indx;
2795                       hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2796                       sym_sec = hdr->bfd_section;
2797                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2798                         sym_value = sym->st_value;
2799                       destination = (sym_value + irela->r_addend
2800                                      + sym_sec->output_offset
2801                                      + sym_sec->output_section->vma);
2802                     }
2803                   else
2804                     {
2805                       /* It's an external symbol.  */
2806                       int e_indx;
2807
2808                       e_indx = r_indx - symtab_hdr->sh_info;
2809                       hash = ((struct elf32_hppa_link_hash_entry *)
2810                               elf_sym_hashes (input_bfd)[e_indx]);
2811
2812                       while (hash->elf.root.type == bfd_link_hash_indirect
2813                              || hash->elf.root.type == bfd_link_hash_warning)
2814                         hash = ((struct elf32_hppa_link_hash_entry *)
2815                                 hash->elf.root.u.i.link);
2816
2817                       if (hash->elf.root.type == bfd_link_hash_defined
2818                           || hash->elf.root.type == bfd_link_hash_defweak)
2819                         {
2820                           sym_sec = hash->elf.root.u.def.section;
2821                           sym_value = hash->elf.root.u.def.value;
2822                           if (sym_sec->output_section != NULL)
2823                             destination = (sym_value + irela->r_addend
2824                                            + sym_sec->output_offset
2825                                            + sym_sec->output_section->vma);
2826                         }
2827                       else if (hash->elf.root.type == bfd_link_hash_undefweak)
2828                         {
2829                           if (! info->shared)
2830                             continue;
2831                         }
2832                       else if (hash->elf.root.type == bfd_link_hash_undefined)
2833                         {
2834                           if (! (info->unresolved_syms_in_objects == RM_IGNORE
2835                                  && (ELF_ST_VISIBILITY (hash->elf.other)
2836                                      == STV_DEFAULT)
2837                                  && hash->elf.type != STT_PARISC_MILLI))
2838                             continue;
2839                         }
2840                       else
2841                         {
2842                           bfd_set_error (bfd_error_bad_value);
2843                           goto error_ret_free_internal;
2844                         }
2845                     }
2846
2847                   /* Determine what (if any) linker stub is needed.  */
2848                   stub_type = hppa_type_of_stub (section, irela, hash,
2849                                                  destination, info);
2850                   if (stub_type == hppa_stub_none)
2851                     continue;
2852
2853                   /* Support for grouping stub sections.  */
2854                   id_sec = htab->stub_group[section->id].link_sec;
2855
2856                   /* Get the name of this stub.  */
2857                   stub_name = hppa_stub_name (id_sec, sym_sec, hash, irela);
2858                   if (!stub_name)
2859                     goto error_ret_free_internal;
2860
2861                   stub_entry = hppa_stub_hash_lookup (&htab->stub_hash_table,
2862                                                       stub_name,
2863                                                       FALSE, FALSE);
2864                   if (stub_entry != NULL)
2865                     {
2866                       /* The proper stub has already been created.  */
2867                       free (stub_name);
2868                       continue;
2869                     }
2870
2871                   stub_entry = hppa_add_stub (stub_name, section, htab);
2872                   if (stub_entry == NULL)
2873                     {
2874                       free (stub_name);
2875                       goto error_ret_free_internal;
2876                     }
2877
2878                   stub_entry->target_value = sym_value;
2879                   stub_entry->target_section = sym_sec;
2880                   stub_entry->stub_type = stub_type;
2881                   if (info->shared)
2882                     {
2883                       if (stub_type == hppa_stub_import)
2884                         stub_entry->stub_type = hppa_stub_import_shared;
2885                       else if (stub_type == hppa_stub_long_branch)
2886                         stub_entry->stub_type = hppa_stub_long_branch_shared;
2887                     }
2888                   stub_entry->h = hash;
2889                   stub_changed = TRUE;
2890                 }
2891
2892               /* We're done with the internal relocs, free them.  */
2893               if (elf_section_data (section)->relocs == NULL)
2894                 free (internal_relocs);
2895             }
2896         }
2897
2898       if (!stub_changed)
2899         break;
2900
2901       /* OK, we've added some stubs.  Find out the new size of the
2902          stub sections.  */
2903       for (stub_sec = htab->stub_bfd->sections;
2904            stub_sec != NULL;
2905            stub_sec = stub_sec->next)
2906         stub_sec->size = 0;
2907
2908       bfd_hash_traverse (&htab->stub_hash_table, hppa_size_one_stub, htab);
2909
2910       /* Ask the linker to do its stuff.  */
2911       (*htab->layout_sections_again) ();
2912       stub_changed = FALSE;
2913     }
2914
2915   free (htab->all_local_syms);
2916   return TRUE;
2917
2918  error_ret_free_local:
2919   free (htab->all_local_syms);
2920   return FALSE;
2921 }
2922
2923 /* For a final link, this function is called after we have sized the
2924    stubs to provide a value for __gp.  */
2925
2926 bfd_boolean
2927 elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
2928 {
2929   struct bfd_link_hash_entry *h;
2930   asection *sec = NULL;
2931   bfd_vma gp_val = 0;
2932   struct elf32_hppa_link_hash_table *htab;
2933
2934   htab = hppa_link_hash_table (info);
2935   h = bfd_link_hash_lookup (&htab->elf.root, "$global$", FALSE, FALSE, FALSE);
2936
2937   if (h != NULL
2938       && (h->type == bfd_link_hash_defined
2939           || h->type == bfd_link_hash_defweak))
2940     {
2941       gp_val = h->u.def.value;
2942       sec = h->u.def.section;
2943     }
2944   else
2945     {
2946       asection *splt = bfd_get_section_by_name (abfd, ".plt");
2947       asection *sgot = bfd_get_section_by_name (abfd, ".got");
2948
2949       /* Choose to point our LTP at, in this order, one of .plt, .got,
2950          or .data, if these sections exist.  In the case of choosing
2951          .plt try to make the LTP ideal for addressing anywhere in the
2952          .plt or .got with a 14 bit signed offset.  Typically, the end
2953          of the .plt is the start of the .got, so choose .plt + 0x2000
2954          if either the .plt or .got is larger than 0x2000.  If both
2955          the .plt and .got are smaller than 0x2000, choose the end of
2956          the .plt section.  */
2957       sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
2958           ? NULL : splt;
2959       if (sec != NULL)
2960         {
2961           gp_val = sec->size;
2962           if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
2963             {
2964               gp_val = 0x2000;
2965             }
2966         }
2967       else
2968         {
2969           sec = sgot;
2970           if (sec != NULL)
2971             {
2972               if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
2973                 {
2974                   /* We know we don't have a .plt.  If .got is large,
2975                      offset our LTP.  */
2976                   if (sec->size > 0x2000)
2977                     gp_val = 0x2000;
2978                 }
2979             }
2980           else
2981             {
2982               /* No .plt or .got.  Who cares what the LTP is?  */
2983               sec = bfd_get_section_by_name (abfd, ".data");
2984             }
2985         }
2986
2987       if (h != NULL)
2988         {
2989           h->type = bfd_link_hash_defined;
2990           h->u.def.value = gp_val;
2991           if (sec != NULL)
2992             h->u.def.section = sec;
2993           else
2994             h->u.def.section = bfd_abs_section_ptr;
2995         }
2996     }
2997
2998   if (sec != NULL && sec->output_section != NULL)
2999     gp_val += sec->output_section->vma + sec->output_offset;
3000
3001   elf_gp (abfd) = gp_val;
3002   return TRUE;
3003 }
3004
3005 /* Build all the stubs associated with the current output file.  The
3006    stubs are kept in a hash table attached to the main linker hash
3007    table.  We also set up the .plt entries for statically linked PIC
3008    functions here.  This function is called via hppaelf_finish in the
3009    linker.  */
3010
3011 bfd_boolean
3012 elf32_hppa_build_stubs (struct bfd_link_info *info)
3013 {
3014   asection *stub_sec;
3015   struct bfd_hash_table *table;
3016   struct elf32_hppa_link_hash_table *htab;
3017
3018   htab = hppa_link_hash_table (info);
3019
3020   for (stub_sec = htab->stub_bfd->sections;
3021        stub_sec != NULL;
3022        stub_sec = stub_sec->next)
3023     {
3024       bfd_size_type size;
3025
3026       /* Allocate memory to hold the linker stubs.  */
3027       size = stub_sec->size;
3028       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3029       if (stub_sec->contents == NULL && size != 0)
3030         return FALSE;
3031       stub_sec->size = 0;
3032     }
3033
3034   /* Build the stubs as directed by the stub hash table.  */
3035   table = &htab->stub_hash_table;
3036   bfd_hash_traverse (table, hppa_build_one_stub, info);
3037
3038   return TRUE;
3039 }
3040
3041 /* Perform a final link.  */
3042
3043 static bfd_boolean
3044 elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
3045 {
3046   /* Invoke the regular ELF linker to do all the work.  */
3047   if (!bfd_elf_final_link (abfd, info))
3048     return FALSE;
3049
3050   /* If we're producing a final executable, sort the contents of the
3051      unwind section.  */
3052   return elf_hppa_sort_unwind (abfd);
3053 }
3054
3055 /* Record the lowest address for the data and text segments.  */
3056
3057 static void
3058 hppa_record_segment_addr (bfd *abfd ATTRIBUTE_UNUSED,
3059                           asection *section,
3060                           void *data)
3061 {
3062   struct elf32_hppa_link_hash_table *htab;
3063
3064   htab = (struct elf32_hppa_link_hash_table *) data;
3065
3066   if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3067     {
3068       bfd_vma value = section->vma - section->filepos;
3069
3070       if ((section->flags & SEC_READONLY) != 0)
3071         {
3072           if (value < htab->text_segment_base)
3073             htab->text_segment_base = value;
3074         }
3075       else
3076         {
3077           if (value < htab->data_segment_base)
3078             htab->data_segment_base = value;
3079         }
3080     }
3081 }
3082
3083 /* Perform a relocation as part of a final link.  */
3084
3085 static bfd_reloc_status_type
3086 final_link_relocate (asection *input_section,
3087                      bfd_byte *contents,
3088                      const Elf_Internal_Rela *rel,
3089                      bfd_vma value,
3090                      struct elf32_hppa_link_hash_table *htab,
3091                      asection *sym_sec,
3092                      struct elf32_hppa_link_hash_entry *h,
3093                      struct bfd_link_info *info)
3094 {
3095   int insn;
3096   unsigned int r_type = ELF32_R_TYPE (rel->r_info);
3097   unsigned int orig_r_type = r_type;
3098   reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3099   int r_format = howto->bitsize;
3100   enum hppa_reloc_field_selector_type_alt r_field;
3101   bfd *input_bfd = input_section->owner;
3102   bfd_vma offset = rel->r_offset;
3103   bfd_vma max_branch_offset = 0;
3104   bfd_byte *hit_data = contents + offset;
3105   bfd_signed_vma addend = rel->r_addend;
3106   bfd_vma location;
3107   struct elf32_hppa_stub_hash_entry *stub_entry = NULL;
3108   int val;
3109
3110   if (r_type == R_PARISC_NONE)
3111     return bfd_reloc_ok;
3112
3113   insn = bfd_get_32 (input_bfd, hit_data);
3114
3115   /* Find out where we are and where we're going.  */
3116   location = (offset +
3117               input_section->output_offset +
3118               input_section->output_section->vma);
3119
3120   /* If we are not building a shared library, convert DLTIND relocs to
3121      DPREL relocs.  */
3122   if (!info->shared)
3123     {
3124       switch (r_type)
3125         {
3126           case R_PARISC_DLTIND21L:
3127             r_type = R_PARISC_DPREL21L;
3128             break;
3129
3130           case R_PARISC_DLTIND14R:
3131             r_type = R_PARISC_DPREL14R;
3132             break;
3133
3134           case R_PARISC_DLTIND14F:
3135             r_type = R_PARISC_DPREL14F;
3136             break;
3137         }
3138     }
3139
3140   switch (r_type)
3141     {
3142     case R_PARISC_PCREL12F:
3143     case R_PARISC_PCREL17F:
3144     case R_PARISC_PCREL22F:
3145       /* If this call should go via the plt, find the import stub in
3146          the stub hash.  */
3147       if (sym_sec == NULL
3148           || sym_sec->output_section == NULL
3149           || (h != NULL
3150               && h->elf.plt.offset != (bfd_vma) -1
3151               && h->elf.dynindx != -1
3152               && !h->plabel
3153               && (info->shared
3154                   || !h->elf.def_regular
3155                   || h->elf.root.type == bfd_link_hash_defweak)))
3156         {
3157           stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3158                                             h, rel, htab);
3159           if (stub_entry != NULL)
3160             {
3161               value = (stub_entry->stub_offset
3162                        + stub_entry->stub_sec->output_offset
3163                        + stub_entry->stub_sec->output_section->vma);
3164               addend = 0;
3165             }
3166           else if (sym_sec == NULL && h != NULL
3167                    && h->elf.root.type == bfd_link_hash_undefweak)
3168             {
3169               /* It's OK if undefined weak.  Calls to undefined weak
3170                  symbols behave as if the "called" function
3171                  immediately returns.  We can thus call to a weak
3172                  function without first checking whether the function
3173                  is defined.  */
3174               value = location;
3175               addend = 8;
3176             }
3177           else
3178             return bfd_reloc_undefined;
3179         }
3180       /* Fall thru.  */
3181
3182     case R_PARISC_PCREL21L:
3183     case R_PARISC_PCREL17C:
3184     case R_PARISC_PCREL17R:
3185     case R_PARISC_PCREL14R:
3186     case R_PARISC_PCREL14F:
3187     case R_PARISC_PCREL32:
3188       /* Make it a pc relative offset.  */
3189       value -= location;
3190       addend -= 8;
3191       break;
3192
3193     case R_PARISC_DPREL21L:
3194     case R_PARISC_DPREL14R:
3195     case R_PARISC_DPREL14F:
3196       /* Convert instructions that use the linkage table pointer (r19) to
3197          instructions that use the global data pointer (dp).  This is the
3198          most efficient way of using PIC code in an incomplete executable,
3199          but the user must follow the standard runtime conventions for
3200          accessing data for this to work.  */
3201       if (orig_r_type == R_PARISC_DLTIND21L)
3202         {
3203           /* Convert addil instructions if the original reloc was a
3204              DLTIND21L.  GCC sometimes uses a register other than r19 for
3205              the operation, so we must convert any addil instruction
3206              that uses this relocation.  */
3207           if ((insn & 0xfc000000) == ((int) OP_ADDIL << 26))
3208             insn = ADDIL_DP;
3209           else
3210             /* We must have a ldil instruction.  It's too hard to find
3211                and convert the associated add instruction, so issue an
3212                error.  */
3213             (*_bfd_error_handler)
3214               (_("%B(%A+0x%lx): %s fixup for insn 0x%x is not supported in a non-shared link"),
3215                input_bfd,
3216                input_section,
3217                (long) rel->r_offset,
3218                howto->name,
3219                insn);
3220         }
3221       else if (orig_r_type == R_PARISC_DLTIND14F)
3222         {
3223           /* This must be a format 1 load/store.  Change the base
3224              register to dp.  */
3225           insn = (insn & 0xfc1ffff) | (27 << 21);
3226         }
3227
3228     /* For all the DP relative relocations, we need to examine the symbol's
3229        section.  If it has no section or if it's a code section, then
3230        "data pointer relative" makes no sense.  In that case we don't
3231        adjust the "value", and for 21 bit addil instructions, we change the
3232        source addend register from %dp to %r0.  This situation commonly
3233        arises for undefined weak symbols and when a variable's "constness"
3234        is declared differently from the way the variable is defined.  For
3235        instance: "extern int foo" with foo defined as "const int foo".  */
3236       if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
3237         {
3238           if ((insn & ((0x3f << 26) | (0x1f << 21)))
3239               == (((int) OP_ADDIL << 26) | (27 << 21)))
3240             {
3241               insn &= ~ (0x1f << 21);
3242             }
3243           /* Now try to make things easy for the dynamic linker.  */
3244
3245           break;
3246         }
3247       /* Fall thru.  */
3248
3249     case R_PARISC_DLTIND21L:
3250     case R_PARISC_DLTIND14R:
3251     case R_PARISC_DLTIND14F:
3252       value -= elf_gp (input_section->output_section->owner);
3253       break;
3254
3255     case R_PARISC_SEGREL32:
3256       if ((sym_sec->flags & SEC_CODE) != 0)
3257         value -= htab->text_segment_base;
3258       else
3259         value -= htab->data_segment_base;
3260       break;
3261
3262     default:
3263       break;
3264     }
3265
3266   switch (r_type)
3267     {
3268     case R_PARISC_DIR32:
3269     case R_PARISC_DIR14F:
3270     case R_PARISC_DIR17F:
3271     case R_PARISC_PCREL17C:
3272     case R_PARISC_PCREL14F:
3273     case R_PARISC_PCREL32:
3274     case R_PARISC_DPREL14F:
3275     case R_PARISC_PLABEL32:
3276     case R_PARISC_DLTIND14F:
3277     case R_PARISC_SEGBASE:
3278     case R_PARISC_SEGREL32:
3279       r_field = e_fsel;
3280       break;
3281
3282     case R_PARISC_DLTIND21L:
3283     case R_PARISC_PCREL21L:
3284     case R_PARISC_PLABEL21L:
3285       r_field = e_lsel;
3286       break;
3287
3288     case R_PARISC_DIR21L:
3289     case R_PARISC_DPREL21L:
3290       r_field = e_lrsel;
3291       break;
3292
3293     case R_PARISC_PCREL17R:
3294     case R_PARISC_PCREL14R:
3295     case R_PARISC_PLABEL14R:
3296     case R_PARISC_DLTIND14R:
3297       r_field = e_rsel;
3298       break;
3299
3300     case R_PARISC_DIR17R:
3301     case R_PARISC_DIR14R:
3302     case R_PARISC_DPREL14R:
3303       r_field = e_rrsel;
3304       break;
3305
3306     case R_PARISC_PCREL12F:
3307     case R_PARISC_PCREL17F:
3308     case R_PARISC_PCREL22F:
3309       r_field = e_fsel;
3310
3311       if (r_type == (unsigned int) R_PARISC_PCREL17F)
3312         {
3313           max_branch_offset = (1 << (17-1)) << 2;
3314         }
3315       else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3316         {
3317           max_branch_offset = (1 << (12-1)) << 2;
3318         }
3319       else
3320         {
3321           max_branch_offset = (1 << (22-1)) << 2;
3322         }
3323
3324       /* sym_sec is NULL on undefined weak syms or when shared on
3325          undefined syms.  We've already checked for a stub for the
3326          shared undefined case.  */
3327       if (sym_sec == NULL)
3328         break;
3329
3330       /* If the branch is out of reach, then redirect the
3331          call to the local stub for this function.  */
3332       if (value + addend + max_branch_offset >= 2*max_branch_offset)
3333         {
3334           stub_entry = hppa_get_stub_entry (input_section, sym_sec,
3335                                             h, rel, htab);
3336           if (stub_entry == NULL)
3337             return bfd_reloc_undefined;
3338
3339           /* Munge up the value and addend so that we call the stub
3340              rather than the procedure directly.  */
3341           value = (stub_entry->stub_offset
3342                    + stub_entry->stub_sec->output_offset
3343                    + stub_entry->stub_sec->output_section->vma
3344                    - location);
3345           addend = -8;
3346         }
3347       break;
3348
3349     /* Something we don't know how to handle.  */
3350     default:
3351       return bfd_reloc_notsupported;
3352     }
3353
3354   /* Make sure we can reach the stub.  */
3355   if (max_branch_offset != 0
3356       && value + addend + max_branch_offset >= 2*max_branch_offset)
3357     {
3358       (*_bfd_error_handler)
3359         (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3360          input_bfd,
3361          input_section,
3362          (long) rel->r_offset,
3363          stub_entry->root.string);
3364       bfd_set_error (bfd_error_bad_value);
3365       return bfd_reloc_notsupported;
3366     }
3367
3368   val = hppa_field_adjust (value, addend, r_field);
3369
3370   switch (r_type)
3371     {
3372     case R_PARISC_PCREL12F:
3373     case R_PARISC_PCREL17C:
3374     case R_PARISC_PCREL17F:
3375     case R_PARISC_PCREL17R:
3376     case R_PARISC_PCREL22F:
3377     case R_PARISC_DIR17F:
3378     case R_PARISC_DIR17R:
3379       /* This is a branch.  Divide the offset by four.
3380          Note that we need to decide whether it's a branch or
3381          otherwise by inspecting the reloc.  Inspecting insn won't
3382          work as insn might be from a .word directive.  */
3383       val >>= 2;
3384       break;
3385
3386     default:
3387       break;
3388     }
3389
3390   insn = hppa_rebuild_insn (insn, val, r_format);
3391
3392   /* Update the instruction word.  */
3393   bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3394   return bfd_reloc_ok;
3395 }
3396
3397 /* Relocate an HPPA ELF section.  */
3398
3399 static bfd_boolean
3400 elf32_hppa_relocate_section (bfd *output_bfd,
3401                              struct bfd_link_info *info,
3402                              bfd *input_bfd,
3403                              asection *input_section,
3404                              bfd_byte *contents,
3405                              Elf_Internal_Rela *relocs,
3406                              Elf_Internal_Sym *local_syms,
3407                              asection **local_sections)
3408 {
3409   bfd_vma *local_got_offsets;
3410   struct elf32_hppa_link_hash_table *htab;
3411   Elf_Internal_Shdr *symtab_hdr;
3412   Elf_Internal_Rela *rel;
3413   Elf_Internal_Rela *relend;
3414
3415   if (info->relocatable)
3416     return TRUE;
3417
3418   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3419
3420   htab = hppa_link_hash_table (info);
3421   local_got_offsets = elf_local_got_offsets (input_bfd);
3422
3423   rel = relocs;
3424   relend = relocs + input_section->reloc_count;
3425   for (; rel < relend; rel++)
3426     {
3427       unsigned int r_type;
3428       reloc_howto_type *howto;
3429       unsigned int r_symndx;
3430       struct elf32_hppa_link_hash_entry *h;
3431       Elf_Internal_Sym *sym;
3432       asection *sym_sec;
3433       bfd_vma relocation;
3434       bfd_reloc_status_type r;
3435       const char *sym_name;
3436       bfd_boolean plabel;
3437       bfd_boolean warned_undef;
3438
3439       r_type = ELF32_R_TYPE (rel->r_info);
3440       if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3441         {
3442           bfd_set_error (bfd_error_bad_value);
3443           return FALSE;
3444         }
3445       if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3446           || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3447         continue;
3448
3449       /* This is a final link.  */
3450       r_symndx = ELF32_R_SYM (rel->r_info);
3451       h = NULL;
3452       sym = NULL;
3453       sym_sec = NULL;
3454       warned_undef = FALSE;
3455       if (r_symndx < symtab_hdr->sh_info)
3456         {
3457           /* This is a local symbol, h defaults to NULL.  */
3458           sym = local_syms + r_symndx;
3459           sym_sec = local_sections[r_symndx];
3460           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3461         }
3462       else
3463         {
3464           struct elf_link_hash_entry *hh;
3465           bfd_boolean unresolved_reloc;
3466           struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3467
3468           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3469                                    r_symndx, symtab_hdr, sym_hashes,
3470                                    hh, sym_sec, relocation,
3471                                    unresolved_reloc, warned_undef);
3472
3473           if (relocation == 0
3474               && hh->root.type != bfd_link_hash_defined
3475               && hh->root.type != bfd_link_hash_defweak
3476               && hh->root.type != bfd_link_hash_undefweak)
3477             {
3478               if (info->unresolved_syms_in_objects == RM_IGNORE
3479                   && ELF_ST_VISIBILITY (hh->other) == STV_DEFAULT
3480                   && hh->type == STT_PARISC_MILLI)
3481                 {
3482                   if (! info->callbacks->undefined_symbol
3483                       (info, hh->root.root.string, input_bfd,
3484                        input_section, rel->r_offset, FALSE))
3485                     return FALSE;
3486                   warned_undef = TRUE;
3487                 }
3488             }
3489           h = (struct elf32_hppa_link_hash_entry *) hh;
3490         }
3491
3492       /* Do any required modifications to the relocation value, and
3493          determine what types of dynamic info we need to output, if
3494          any.  */
3495       plabel = 0;
3496       switch (r_type)
3497         {
3498         case R_PARISC_DLTIND14F:
3499         case R_PARISC_DLTIND14R:
3500         case R_PARISC_DLTIND21L:
3501           {
3502             bfd_vma off;
3503             bfd_boolean do_got = 0;
3504
3505             /* Relocation is to the entry for this symbol in the
3506                global offset table.  */
3507             if (h != NULL)
3508               {
3509                 bfd_boolean dyn;
3510
3511                 off = h->elf.got.offset;
3512                 dyn = htab->elf.dynamic_sections_created;
3513                 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
3514                                                        &h->elf))
3515                   {
3516                     /* If we aren't going to call finish_dynamic_symbol,
3517                        then we need to handle initialisation of the .got
3518                        entry and create needed relocs here.  Since the
3519                        offset must always be a multiple of 4, we use the
3520                        least significant bit to record whether we have
3521                        initialised it already.  */
3522                     if ((off & 1) != 0)
3523                       off &= ~1;
3524                     else
3525                       {
3526                         h->elf.got.offset |= 1;
3527                         do_got = 1;
3528                       }
3529                   }
3530               }
3531             else
3532               {
3533                 /* Local symbol case.  */
3534                 if (local_got_offsets == NULL)
3535                   abort ();
3536
3537                 off = local_got_offsets[r_symndx];
3538
3539                 /* The offset must always be a multiple of 4.  We use
3540                    the least significant bit to record whether we have
3541                    already generated the necessary reloc.  */
3542                 if ((off & 1) != 0)
3543                   off &= ~1;
3544                 else
3545                   {
3546                     local_got_offsets[r_symndx] |= 1;
3547                     do_got = 1;
3548                   }
3549               }
3550
3551             if (do_got)
3552               {
3553                 if (info->shared)
3554                   {
3555                     /* Output a dynamic relocation for this GOT entry.
3556                        In this case it is relative to the base of the
3557                        object because the symbol index is zero.  */
3558                     Elf_Internal_Rela outrel;
3559                     bfd_byte *loc;
3560                     asection *s = htab->srelgot;
3561
3562                     outrel.r_offset = (off
3563                                        + htab->sgot->output_offset
3564                                        + htab->sgot->output_section->vma);
3565                     outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3566                     outrel.r_addend = relocation;
3567                     loc = s->contents;
3568                     loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3569                     bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3570                   }
3571                 else
3572                   bfd_put_32 (output_bfd, relocation,
3573                               htab->sgot->contents + off);
3574               }
3575
3576             if (off >= (bfd_vma) -2)
3577               abort ();
3578
3579             /* Add the base of the GOT to the relocation value.  */
3580             relocation = (off
3581                           + htab->sgot->output_offset
3582                           + htab->sgot->output_section->vma);
3583           }
3584           break;
3585
3586         case R_PARISC_SEGREL32:
3587           /* If this is the first SEGREL relocation, then initialize
3588              the segment base values.  */
3589           if (htab->text_segment_base == (bfd_vma) -1)
3590             bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
3591           break;
3592
3593         case R_PARISC_PLABEL14R:
3594         case R_PARISC_PLABEL21L:
3595         case R_PARISC_PLABEL32:
3596           if (htab->elf.dynamic_sections_created)
3597             {
3598               bfd_vma off;
3599               bfd_boolean do_plt = 0;
3600
3601               /* If we have a global symbol with a PLT slot, then
3602                  redirect this relocation to it.  */
3603               if (h != NULL)
3604                 {
3605                   off = h->elf.plt.offset;
3606                   if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared,
3607                                                          &h->elf))
3608                     {
3609                       /* In a non-shared link, adjust_dynamic_symbols
3610                          isn't called for symbols forced local.  We
3611                          need to write out the plt entry here.  */
3612                       if ((off & 1) != 0)
3613                         off &= ~1;
3614                       else
3615                         {
3616                           h->elf.plt.offset |= 1;
3617                           do_plt = 1;
3618                         }
3619                     }
3620                 }
3621               else
3622                 {
3623                   bfd_vma *local_plt_offsets;
3624
3625                   if (local_got_offsets == NULL)
3626                     abort ();
3627
3628                   local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3629                   off = local_plt_offsets[r_symndx];
3630
3631                   /* As for the local .got entry case, we use the last
3632                      bit to record whether we've already initialised
3633                      this local .plt entry.  */
3634                   if ((off & 1) != 0)
3635                     off &= ~1;
3636                   else
3637                     {
3638                       local_plt_offsets[r_symndx] |= 1;
3639                       do_plt = 1;
3640                     }
3641                 }
3642
3643               if (do_plt)
3644                 {
3645                   if (info->shared)
3646                     {
3647                       /* Output a dynamic IPLT relocation for this
3648                          PLT entry.  */
3649                       Elf_Internal_Rela outrel;
3650                       bfd_byte *loc;
3651                       asection *s = htab->srelplt;
3652
3653                       outrel.r_offset = (off
3654                                          + htab->splt->output_offset
3655                                          + htab->splt->output_section->vma);
3656                       outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3657                       outrel.r_addend = relocation;
3658                       loc = s->contents;
3659                       loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3660                       bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3661                     }
3662                   else
3663                     {
3664                       bfd_put_32 (output_bfd,
3665                                   relocation,
3666                                   htab->splt->contents + off);
3667                       bfd_put_32 (output_bfd,
3668                                   elf_gp (htab->splt->output_section->owner),
3669                                   htab->splt->contents + off + 4);
3670                     }
3671                 }
3672
3673               if (off >= (bfd_vma) -2)
3674                 abort ();
3675
3676               /* PLABELs contain function pointers.  Relocation is to
3677                  the entry for the function in the .plt.  The magic +2
3678                  offset signals to $$dyncall that the function pointer
3679                  is in the .plt and thus has a gp pointer too.
3680                  Exception:  Undefined PLABELs should have a value of
3681                  zero.  */
3682               if (h == NULL
3683                   || (h->elf.root.type != bfd_link_hash_undefweak
3684                       && h->elf.root.type != bfd_link_hash_undefined))
3685                 {
3686                   relocation = (off
3687                                 + htab->splt->output_offset
3688                                 + htab->splt->output_section->vma
3689                                 + 2);
3690                 }
3691               plabel = 1;
3692             }
3693           /* Fall through and possibly emit a dynamic relocation.  */
3694
3695         case R_PARISC_DIR17F:
3696         case R_PARISC_DIR17R:
3697         case R_PARISC_DIR14F:
3698         case R_PARISC_DIR14R:
3699         case R_PARISC_DIR21L:
3700         case R_PARISC_DPREL14F:
3701         case R_PARISC_DPREL14R:
3702         case R_PARISC_DPREL21L:
3703         case R_PARISC_DIR32:
3704           /* r_symndx will be zero only for relocs against symbols
3705              from removed linkonce sections, or sections discarded by
3706              a linker script.  */
3707           if (r_symndx == 0
3708               || (input_section->flags & SEC_ALLOC) == 0)
3709             break;
3710
3711           /* The reloc types handled here and this conditional
3712              expression must match the code in ..check_relocs and
3713              allocate_dynrelocs.  ie. We need exactly the same condition
3714              as in ..check_relocs, with some extra conditions (dynindx
3715              test in this case) to cater for relocs removed by
3716              allocate_dynrelocs.  If you squint, the non-shared test
3717              here does indeed match the one in ..check_relocs, the
3718              difference being that here we test DEF_DYNAMIC as well as
3719              !DEF_REGULAR.  All common syms end up with !DEF_REGULAR,
3720              which is why we can't use just that test here.
3721              Conversely, DEF_DYNAMIC can't be used in check_relocs as
3722              there all files have not been loaded.  */
3723           if ((info->shared
3724                && (h == NULL
3725                    || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
3726                    || h->elf.root.type != bfd_link_hash_undefweak)
3727                && (IS_ABSOLUTE_RELOC (r_type)
3728                    || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
3729               || (!info->shared
3730                   && h != NULL
3731                   && h->elf.dynindx != -1
3732                   && !h->elf.non_got_ref
3733                   && ((ELIMINATE_COPY_RELOCS
3734                        && h->elf.def_dynamic
3735                        && !h->elf.def_regular)
3736                       || h->elf.root.type == bfd_link_hash_undefweak
3737                       || h->elf.root.type == bfd_link_hash_undefined)))
3738             {
3739               Elf_Internal_Rela outrel;
3740               bfd_boolean skip;
3741               asection *sreloc;
3742               bfd_byte *loc;
3743
3744               /* When generating a shared object, these relocations
3745                  are copied into the output file to be resolved at run
3746                  time.  */
3747
3748               outrel.r_addend = rel->r_addend;
3749               outrel.r_offset =
3750                 _bfd_elf_section_offset (output_bfd, info, input_section,
3751                                          rel->r_offset);
3752               skip = (outrel.r_offset == (bfd_vma) -1
3753                       || outrel.r_offset == (bfd_vma) -2);
3754               outrel.r_offset += (input_section->output_offset
3755                                   + input_section->output_section->vma);
3756
3757               if (skip)
3758                 {
3759                   memset (&outrel, 0, sizeof (outrel));
3760                 }
3761               else if (h != NULL
3762                        && h->elf.dynindx != -1
3763                        && (plabel
3764                            || !IS_ABSOLUTE_RELOC (r_type)
3765                            || !info->shared
3766                            || !info->symbolic
3767                            || !h->elf.def_regular))
3768                 {
3769                   outrel.r_info = ELF32_R_INFO (h->elf.dynindx, r_type);
3770                 }
3771               else /* It's a local symbol, or one marked to become local.  */
3772                 {
3773                   int indx = 0;
3774
3775                   /* Add the absolute offset of the symbol.  */
3776                   outrel.r_addend += relocation;
3777
3778                   /* Global plabels need to be processed by the
3779                      dynamic linker so that functions have at most one
3780                      fptr.  For this reason, we need to differentiate
3781                      between global and local plabels, which we do by
3782                      providing the function symbol for a global plabel
3783                      reloc, and no symbol for local plabels.  */
3784                   if (! plabel
3785                       && sym_sec != NULL
3786                       && sym_sec->output_section != NULL
3787                       && ! bfd_is_abs_section (sym_sec))
3788                     {
3789                       /* Skip this relocation if the output section has
3790                          been discarded.  */
3791                       if (bfd_is_abs_section (sym_sec->output_section))
3792                         break;
3793
3794                       indx = elf_section_data (sym_sec->output_section)->dynindx;
3795                       /* We are turning this relocation into one
3796                          against a section symbol, so subtract out the
3797                          output section's address but not the offset
3798                          of the input section in the output section.  */
3799                       outrel.r_addend -= sym_sec->output_section->vma;
3800                     }
3801
3802                   outrel.r_info = ELF32_R_INFO (indx, r_type);
3803                 }
3804               sreloc = elf_section_data (input_section)->sreloc;
3805               if (sreloc == NULL)
3806                 abort ();
3807
3808               loc = sreloc->contents;
3809               loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3810               bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3811             }
3812           break;
3813
3814         default:
3815           break;
3816         }
3817
3818       r = final_link_relocate (input_section, contents, rel, relocation,
3819                                htab, sym_sec, h, info);
3820
3821       if (r == bfd_reloc_ok)
3822         continue;
3823
3824       if (h != NULL)
3825         sym_name = h->elf.root.root.string;
3826       else
3827         {
3828           sym_name = bfd_elf_string_from_elf_section (input_bfd,
3829                                                       symtab_hdr->sh_link,
3830                                                       sym->st_name);
3831           if (sym_name == NULL)
3832             return FALSE;
3833           if (*sym_name == '\0')
3834             sym_name = bfd_section_name (input_bfd, sym_sec);
3835         }
3836
3837       howto = elf_hppa_howto_table + r_type;
3838
3839       if (r == bfd_reloc_undefined || r == bfd_reloc_notsupported)
3840         {
3841           if (r == bfd_reloc_notsupported || !warned_undef)
3842             {
3843               (*_bfd_error_handler)
3844                 (_("%B(%A+0x%lx): cannot handle %s for %s"),
3845                  input_bfd,
3846                  input_section,
3847                  (long) rel->r_offset,
3848                  howto->name,
3849                  sym_name);
3850               bfd_set_error (bfd_error_bad_value);
3851               return FALSE;
3852             }
3853         }
3854       else
3855         {
3856           if (!((*info->callbacks->reloc_overflow)
3857                 (info, (h ? &h->elf.root : NULL), sym_name, howto->name,
3858                  (bfd_vma) 0, input_bfd, input_section, rel->r_offset)))
3859             return FALSE;
3860         }
3861     }
3862
3863   return TRUE;
3864 }
3865
3866 /* Finish up dynamic symbol handling.  We set the contents of various
3867    dynamic sections here.  */
3868
3869 static bfd_boolean
3870 elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
3871                                   struct bfd_link_info *info,
3872                                   struct elf_link_hash_entry *h,
3873                                   Elf_Internal_Sym *sym)
3874 {
3875   struct elf32_hppa_link_hash_table *htab;
3876   Elf_Internal_Rela rel;
3877   bfd_byte *loc;
3878
3879   htab = hppa_link_hash_table (info);
3880
3881   if (h->plt.offset != (bfd_vma) -1)
3882     {
3883       bfd_vma value;
3884
3885       if (h->plt.offset & 1)
3886         abort ();
3887
3888       /* This symbol has an entry in the procedure linkage table.  Set
3889          it up.
3890
3891          The format of a plt entry is
3892          <funcaddr>
3893          <__gp>
3894       */
3895       value = 0;
3896       if (h->root.type == bfd_link_hash_defined
3897           || h->root.type == bfd_link_hash_defweak)
3898         {
3899           value = h->root.u.def.value;
3900           if (h->root.u.def.section->output_section != NULL)
3901             value += (h->root.u.def.section->output_offset
3902                       + h->root.u.def.section->output_section->vma);
3903         }
3904
3905       /* Create a dynamic IPLT relocation for this entry.  */
3906       rel.r_offset = (h->plt.offset
3907                       + htab->splt->output_offset
3908                       + htab->splt->output_section->vma);
3909       if (h->dynindx != -1)
3910         {
3911           rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_IPLT);
3912           rel.r_addend = 0;
3913         }
3914       else
3915         {
3916           /* This symbol has been marked to become local, and is
3917              used by a plabel so must be kept in the .plt.  */
3918           rel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3919           rel.r_addend = value;
3920         }
3921
3922       loc = htab->srelplt->contents;
3923       loc += htab->srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
3924       bfd_elf32_swap_reloca_out (htab->splt->output_section->owner, &rel, loc);
3925
3926       if (!h->def_regular)
3927         {
3928           /* Mark the symbol as undefined, rather than as defined in
3929              the .plt section.  Leave the value alone.  */
3930           sym->st_shndx = SHN_UNDEF;
3931         }
3932     }
3933
3934   if (h->got.offset != (bfd_vma) -1)
3935     {
3936       /* This symbol has an entry in the global offset table.  Set it
3937          up.  */
3938
3939       rel.r_offset = ((h->got.offset &~ (bfd_vma) 1)
3940                       + htab->sgot->output_offset
3941                       + htab->sgot->output_section->vma);
3942
3943       /* If this is a -Bsymbolic link and the symbol is defined
3944          locally or was forced to be local because of a version file,
3945          we just want to emit a RELATIVE reloc.  The entry in the
3946          global offset table will already have been initialized in the
3947          relocate_section function.  */
3948       if (info->shared
3949           && (info->symbolic || h->dynindx == -1)
3950           && h->def_regular)
3951         {
3952           rel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3953           rel.r_addend = (h->root.u.def.value
3954                           + h->root.u.def.section->output_offset
3955                           + h->root.u.def.section->output_section->vma);
3956         }
3957       else
3958         {
3959           if ((h->got.offset & 1) != 0)
3960             abort ();
3961           bfd_put_32 (output_bfd, 0, htab->sgot->contents + h->got.offset);
3962           rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_DIR32);
3963           rel.r_addend = 0;
3964         }
3965
3966       loc = htab->srelgot->contents;
3967       loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
3968       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3969     }
3970
3971   if (h->needs_copy)
3972     {
3973       asection *s;
3974
3975       /* This symbol needs a copy reloc.  Set it up.  */
3976
3977       if (! (h->dynindx != -1
3978              && (h->root.type == bfd_link_hash_defined
3979                  || h->root.type == bfd_link_hash_defweak)))
3980         abort ();
3981
3982       s = htab->srelbss;
3983
3984       rel.r_offset = (h->root.u.def.value
3985                       + h->root.u.def.section->output_offset
3986                       + h->root.u.def.section->output_section->vma);
3987       rel.r_addend = 0;
3988       rel.r_info = ELF32_R_INFO (h->dynindx, R_PARISC_COPY);
3989       loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
3990       bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
3991     }
3992
3993   /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
3994   if (h->root.root.string[0] == '_'
3995       && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3996           || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0))
3997     {
3998       sym->st_shndx = SHN_ABS;
3999     }
4000
4001   return TRUE;
4002 }
4003
4004 /* Used to decide how to sort relocs in an optimal manner for the
4005    dynamic linker, before writing them out.  */
4006
4007 static enum elf_reloc_type_class
4008 elf32_hppa_reloc_type_class (const Elf_Internal_Rela *rela)
4009 {
4010   if (ELF32_R_SYM (rela->r_info) == 0)
4011     return reloc_class_relative;
4012
4013   switch ((int) ELF32_R_TYPE (rela->r_info))
4014     {
4015     case R_PARISC_IPLT:
4016       return reloc_class_plt;
4017     case R_PARISC_COPY:
4018       return reloc_class_copy;
4019     default:
4020       return reloc_class_normal;
4021     }
4022 }
4023
4024 /* Finish up the dynamic sections.  */
4025
4026 static bfd_boolean
4027 elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
4028                                     struct bfd_link_info *info)
4029 {
4030   bfd *dynobj;
4031   struct elf32_hppa_link_hash_table *htab;
4032   asection *sdyn;
4033
4034   htab = hppa_link_hash_table (info);
4035   dynobj = htab->elf.dynobj;
4036
4037   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4038
4039   if (htab->elf.dynamic_sections_created)
4040     {
4041       Elf32_External_Dyn *dyncon, *dynconend;
4042
4043       if (sdyn == NULL)
4044         abort ();
4045
4046       dyncon = (Elf32_External_Dyn *) sdyn->contents;
4047       dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4048       for (; dyncon < dynconend; dyncon++)
4049         {
4050           Elf_Internal_Dyn dyn;
4051           asection *s;
4052
4053           bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4054
4055           switch (dyn.d_tag)
4056             {
4057             default:
4058               continue;
4059
4060             case DT_PLTGOT:
4061               /* Use PLTGOT to set the GOT register.  */
4062               dyn.d_un.d_ptr = elf_gp (output_bfd);
4063               break;
4064
4065             case DT_JMPREL:
4066               s = htab->srelplt;
4067               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4068               break;
4069
4070             case DT_PLTRELSZ:
4071               s = htab->srelplt;
4072               dyn.d_un.d_val = s->size;
4073               break;
4074
4075             case DT_RELASZ:
4076               /* Don't count procedure linkage table relocs in the
4077                  overall reloc count.  */
4078               s = htab->srelplt;
4079               if (s == NULL)
4080                 continue;
4081               dyn.d_un.d_val -= s->size;
4082               break;
4083
4084             case DT_RELA:
4085               /* We may not be using the standard ELF linker script.
4086                  If .rela.plt is the first .rela section, we adjust
4087                  DT_RELA to not include it.  */
4088               s = htab->srelplt;
4089               if (s == NULL)
4090                 continue;
4091               if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4092                 continue;
4093               dyn.d_un.d_ptr += s->size;
4094               break;
4095             }
4096
4097           bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4098         }
4099     }
4100
4101   if (htab->sgot != NULL && htab->sgot->size != 0)
4102     {
4103       /* Fill in the first entry in the global offset table.
4104          We use it to point to our dynamic section, if we have one.  */
4105       bfd_put_32 (output_bfd,
4106                   sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
4107                   htab->sgot->contents);
4108
4109       /* The second entry is reserved for use by the dynamic linker.  */
4110       memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4111
4112       /* Set .got entry size.  */
4113       elf_section_data (htab->sgot->output_section)
4114         ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4115     }
4116
4117   if (htab->splt != NULL && htab->splt->size != 0)
4118     {
4119       /* Set plt entry size.  */
4120       elf_section_data (htab->splt->output_section)
4121         ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4122
4123       if (htab->need_plt_stub)
4124         {
4125           /* Set up the .plt stub.  */
4126           memcpy (htab->splt->contents
4127                   + htab->splt->size - sizeof (plt_stub),
4128                   plt_stub, sizeof (plt_stub));
4129
4130           if ((htab->splt->output_offset
4131                + htab->splt->output_section->vma
4132                + htab->splt->size)
4133               != (htab->sgot->output_offset
4134                   + htab->sgot->output_section->vma))
4135             {
4136               (*_bfd_error_handler)
4137                 (_(".got section not immediately after .plt section"));
4138               return FALSE;
4139             }
4140         }
4141     }
4142
4143   return TRUE;
4144 }
4145
4146 /* Tweak the OSABI field of the elf header.  */
4147
4148 static void
4149 elf32_hppa_post_process_headers (bfd *abfd,
4150                                  struct bfd_link_info *info ATTRIBUTE_UNUSED)
4151 {
4152   Elf_Internal_Ehdr * i_ehdrp;
4153
4154   i_ehdrp = elf_elfheader (abfd);
4155
4156   if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
4157     {
4158       i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
4159     }
4160   else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
4161     {
4162       i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NETBSD;
4163     }
4164   else
4165     {
4166       i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
4167     }
4168 }
4169
4170 /* Called when writing out an object file to decide the type of a
4171    symbol.  */
4172 static int
4173 elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
4174 {
4175   if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4176     return STT_PARISC_MILLI;
4177   else
4178     return type;
4179 }
4180
4181 /* Misc BFD support code.  */
4182 #define bfd_elf32_bfd_is_local_label_name    elf_hppa_is_local_label_name
4183 #define bfd_elf32_bfd_reloc_type_lookup      elf_hppa_reloc_type_lookup
4184 #define elf_info_to_howto                    elf_hppa_info_to_howto
4185 #define elf_info_to_howto_rel                elf_hppa_info_to_howto_rel
4186
4187 /* Stuff for the BFD linker.  */
4188 #define bfd_elf32_bfd_final_link             elf32_hppa_final_link
4189 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4190 #define bfd_elf32_bfd_link_hash_table_free   elf32_hppa_link_hash_table_free
4191 #define elf_backend_adjust_dynamic_symbol    elf32_hppa_adjust_dynamic_symbol
4192 #define elf_backend_copy_indirect_symbol     elf32_hppa_copy_indirect_symbol
4193 #define elf_backend_check_relocs             elf32_hppa_check_relocs
4194 #define elf_backend_create_dynamic_sections  elf32_hppa_create_dynamic_sections
4195 #define elf_backend_fake_sections            elf_hppa_fake_sections
4196 #define elf_backend_relocate_section         elf32_hppa_relocate_section
4197 #define elf_backend_hide_symbol              elf32_hppa_hide_symbol
4198 #define elf_backend_finish_dynamic_symbol    elf32_hppa_finish_dynamic_symbol
4199 #define elf_backend_finish_dynamic_sections  elf32_hppa_finish_dynamic_sections
4200 #define elf_backend_size_dynamic_sections    elf32_hppa_size_dynamic_sections
4201 #define elf_backend_gc_mark_hook             elf32_hppa_gc_mark_hook
4202 #define elf_backend_gc_sweep_hook            elf32_hppa_gc_sweep_hook
4203 #define elf_backend_grok_prstatus            elf32_hppa_grok_prstatus
4204 #define elf_backend_grok_psinfo              elf32_hppa_grok_psinfo
4205 #define elf_backend_object_p                 elf32_hppa_object_p
4206 #define elf_backend_final_write_processing   elf_hppa_final_write_processing
4207 #define elf_backend_post_process_headers     elf32_hppa_post_process_headers
4208 #define elf_backend_get_symbol_type          elf32_hppa_elf_get_symbol_type
4209 #define elf_backend_reloc_type_class         elf32_hppa_reloc_type_class
4210
4211 #define elf_backend_can_gc_sections          1
4212 #define elf_backend_can_refcount             1
4213 #define elf_backend_plt_alignment            2
4214 #define elf_backend_want_got_plt             0
4215 #define elf_backend_plt_readonly             0
4216 #define elf_backend_want_plt_sym             0
4217 #define elf_backend_got_header_size          8
4218 #define elf_backend_rela_normal              1
4219
4220 #define TARGET_BIG_SYM          bfd_elf32_hppa_vec
4221 #define TARGET_BIG_NAME         "elf32-hppa"
4222 #define ELF_ARCH                bfd_arch_hppa
4223 #define ELF_MACHINE_CODE        EM_PARISC
4224 #define ELF_MAXPAGESIZE         0x1000
4225
4226 #include "elf32-target.h"
4227
4228 #undef TARGET_BIG_SYM
4229 #define TARGET_BIG_SYM                  bfd_elf32_hppa_linux_vec
4230 #undef TARGET_BIG_NAME
4231 #define TARGET_BIG_NAME                 "elf32-hppa-linux"
4232
4233 #define INCLUDED_TARGET_FILE 1
4234 #include "elf32-target.h"
4235
4236 #undef TARGET_BIG_SYM
4237 #define TARGET_BIG_SYM                  bfd_elf32_hppa_nbsd_vec
4238 #undef TARGET_BIG_NAME
4239 #define TARGET_BIG_NAME                 "elf32-hppa-netbsd"
4240
4241 #include "elf32-target.h"