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