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