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