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