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