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