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