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