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