xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elf64-hppa.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /* Support for HPPA 64-bit ELF
2    Copyright (C) 1999-2020 Free Software Foundation, Inc.
3 
4    This file is part of BFD, the Binary File Descriptor library.
5 
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10 
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19    MA 02110-1301, USA.  */
20 
21 #include "sysdep.h"
22 #include "alloca-conf.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/hppa.h"
27 #include "libhppa.h"
28 #include "elf64-hppa.h"
29 #include "libiberty.h"
30 
31 #define ARCH_SIZE	       64
32 
33 #define PLT_ENTRY_SIZE 0x10
34 #define DLT_ENTRY_SIZE 0x8
35 #define OPD_ENTRY_SIZE 0x20
36 
37 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
38 
39 /* The stub is supposed to load the target address and target's DP
40    value out of the PLT, then do an external branch to the target
41    address.
42 
43    LDD PLTOFF(%r27),%r1
44    BVE (%r1)
45    LDD PLTOFF+8(%r27),%r27
46 
47    Note that we must use the LDD with a 14 bit displacement, not the one
48    with a 5 bit displacement.  */
49 static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
50 			  0x53, 0x7b, 0x00, 0x00 };
51 
52 struct elf64_hppa_link_hash_entry
53 {
54   struct elf_link_hash_entry eh;
55 
56   /* Offsets for this symbol in various linker sections.  */
57   bfd_vma dlt_offset;
58   bfd_vma plt_offset;
59   bfd_vma opd_offset;
60   bfd_vma stub_offset;
61 
62   /* The index of the (possibly local) symbol in the input bfd and its
63      associated BFD.  Needed so that we can have relocs against local
64      symbols in shared libraries.  */
65   long sym_indx;
66   bfd *owner;
67 
68   /* Dynamic symbols may need to have two different values.  One for
69      the dynamic symbol table, one for the normal symbol table.
70 
71      In such cases we store the symbol's real value and section
72      index here so we can restore the real value before we write
73      the normal symbol table.  */
74   bfd_vma st_value;
75   int st_shndx;
76 
77   /* Used to count non-got, non-plt relocations for delayed sizing
78      of relocation sections.  */
79   struct elf64_hppa_dyn_reloc_entry
80   {
81     /* Next relocation in the chain.  */
82     struct elf64_hppa_dyn_reloc_entry *next;
83 
84     /* The type of the relocation.  */
85     int type;
86 
87     /* The input section of the relocation.  */
88     asection *sec;
89 
90     /* Number of relocs copied in this section.  */
91     bfd_size_type count;
92 
93     /* The index of the section symbol for the input section of
94        the relocation.  Only needed when building shared libraries.  */
95     int sec_symndx;
96 
97     /* The offset within the input section of the relocation.  */
98     bfd_vma offset;
99 
100     /* The addend for the relocation.  */
101     bfd_vma addend;
102 
103   } *reloc_entries;
104 
105   /* Nonzero if this symbol needs an entry in one of the linker
106      sections.  */
107   unsigned want_dlt;
108   unsigned want_plt;
109   unsigned want_opd;
110   unsigned want_stub;
111 };
112 
113 struct elf64_hppa_link_hash_table
114 {
115   struct elf_link_hash_table root;
116 
117   /* Shortcuts to get to the various linker defined sections.  */
118   asection *dlt_sec;
119   asection *dlt_rel_sec;
120   asection *plt_sec;
121   asection *plt_rel_sec;
122   asection *opd_sec;
123   asection *opd_rel_sec;
124   asection *other_rel_sec;
125 
126   /* Offset of __gp within .plt section.  When the PLT gets large we want
127      to slide __gp into the PLT section so that we can continue to use
128      single DP relative instructions to load values out of the PLT.  */
129   bfd_vma gp_offset;
130 
131   /* Note this is not strictly correct.  We should create a stub section for
132      each input section with calls.  The stub section should be placed before
133      the section with the call.  */
134   asection *stub_sec;
135 
136   bfd_vma text_segment_base;
137   bfd_vma data_segment_base;
138 
139   /* We build tables to map from an input section back to its
140      symbol index.  This is the BFD for which we currently have
141      a map.  */
142   bfd *section_syms_bfd;
143 
144   /* Array of symbol numbers for each input section attached to the
145      current BFD.  */
146   int *section_syms;
147 };
148 
149 #define hppa_link_hash_table(p) \
150   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
151   == HPPA64_ELF_DATA ? ((struct elf64_hppa_link_hash_table *) ((p)->hash)) : NULL)
152 
153 #define hppa_elf_hash_entry(ent) \
154   ((struct elf64_hppa_link_hash_entry *)(ent))
155 
156 #define eh_name(eh) \
157   (eh ? eh->root.root.string : "<undef>")
158 
159 typedef struct bfd_hash_entry *(*new_hash_entry_func)
160   (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
161 
162 static struct bfd_link_hash_table *elf64_hppa_hash_table_create
163   (bfd *abfd);
164 
165 /* This must follow the definitions of the various derived linker
166    hash tables and shared functions.  */
167 #include "elf-hppa.h"
168 
169 static bfd_boolean elf64_hppa_object_p
170   (bfd *);
171 
172 static bfd_boolean elf64_hppa_create_dynamic_sections
173   (bfd *, struct bfd_link_info *);
174 
175 static bfd_boolean elf64_hppa_adjust_dynamic_symbol
176   (struct bfd_link_info *, struct elf_link_hash_entry *);
177 
178 static bfd_boolean elf64_hppa_mark_milli_and_exported_functions
179   (struct elf_link_hash_entry *, void *);
180 
181 static bfd_boolean elf64_hppa_size_dynamic_sections
182   (bfd *, struct bfd_link_info *);
183 
184 static int elf64_hppa_link_output_symbol_hook
185   (struct bfd_link_info *, const char *, Elf_Internal_Sym *,
186    asection *, struct elf_link_hash_entry *);
187 
188 static bfd_boolean elf64_hppa_finish_dynamic_symbol
189   (bfd *, struct bfd_link_info *,
190    struct elf_link_hash_entry *, Elf_Internal_Sym *);
191 
192 static bfd_boolean elf64_hppa_finish_dynamic_sections
193   (bfd *, struct bfd_link_info *);
194 
195 static bfd_boolean elf64_hppa_check_relocs
196   (bfd *, struct bfd_link_info *,
197    asection *, const Elf_Internal_Rela *);
198 
199 static bfd_boolean elf64_hppa_dynamic_symbol_p
200   (struct elf_link_hash_entry *, struct bfd_link_info *);
201 
202 static bfd_boolean elf64_hppa_mark_exported_functions
203   (struct elf_link_hash_entry *, void *);
204 
205 static bfd_boolean elf64_hppa_finalize_opd
206   (struct elf_link_hash_entry *, void *);
207 
208 static bfd_boolean elf64_hppa_finalize_dlt
209   (struct elf_link_hash_entry *, void *);
210 
211 static bfd_boolean allocate_global_data_dlt
212   (struct elf_link_hash_entry *, void *);
213 
214 static bfd_boolean allocate_global_data_plt
215   (struct elf_link_hash_entry *, void *);
216 
217 static bfd_boolean allocate_global_data_stub
218   (struct elf_link_hash_entry *, void *);
219 
220 static bfd_boolean allocate_global_data_opd
221   (struct elf_link_hash_entry *, void *);
222 
223 static bfd_boolean get_reloc_section
224   (bfd *, struct elf64_hppa_link_hash_table *, asection *);
225 
226 static bfd_boolean count_dyn_reloc
227   (bfd *, struct elf64_hppa_link_hash_entry *,
228    int, asection *, int, bfd_vma, bfd_vma);
229 
230 static bfd_boolean allocate_dynrel_entries
231   (struct elf_link_hash_entry *, void *);
232 
233 static bfd_boolean elf64_hppa_finalize_dynreloc
234   (struct elf_link_hash_entry *, void *);
235 
236 static bfd_boolean get_opd
237   (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
238 
239 static bfd_boolean get_plt
240   (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
241 
242 static bfd_boolean get_dlt
243   (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
244 
245 static bfd_boolean get_stub
246   (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
247 
248 static int elf64_hppa_elf_get_symbol_type
249   (Elf_Internal_Sym *, int);
250 
251 /* Initialize an entry in the link hash table.  */
252 
253 static struct bfd_hash_entry *
254 hppa64_link_hash_newfunc (struct bfd_hash_entry *entry,
255 			  struct bfd_hash_table *table,
256 			  const char *string)
257 {
258   /* Allocate the structure if it has not already been allocated by a
259      subclass.  */
260   if (entry == NULL)
261     {
262       entry = bfd_hash_allocate (table,
263 				 sizeof (struct elf64_hppa_link_hash_entry));
264       if (entry == NULL)
265 	return entry;
266     }
267 
268   /* Call the allocation method of the superclass.  */
269   entry = _bfd_elf_link_hash_newfunc (entry, table, string);
270   if (entry != NULL)
271     {
272       struct elf64_hppa_link_hash_entry *hh;
273 
274       /* Initialize our local data.  All zeros.  */
275       hh = hppa_elf_hash_entry (entry);
276       memset (&hh->dlt_offset, 0,
277 	      (sizeof (struct elf64_hppa_link_hash_entry)
278 	       - offsetof (struct elf64_hppa_link_hash_entry, dlt_offset)));
279     }
280 
281   return entry;
282 }
283 
284 /* Create the derived linker hash table.  The PA64 ELF port uses this
285    derived hash table to keep information specific to the PA ElF
286    linker (without using static variables).  */
287 
288 static struct bfd_link_hash_table*
289 elf64_hppa_hash_table_create (bfd *abfd)
290 {
291   struct elf64_hppa_link_hash_table *htab;
292   bfd_size_type amt = sizeof (*htab);
293 
294   htab = bfd_zmalloc (amt);
295   if (htab == NULL)
296     return NULL;
297 
298   if (!_bfd_elf_link_hash_table_init (&htab->root, abfd,
299 				      hppa64_link_hash_newfunc,
300 				      sizeof (struct elf64_hppa_link_hash_entry),
301 				      HPPA64_ELF_DATA))
302     {
303       free (htab);
304       return NULL;
305     }
306 
307   htab->text_segment_base = (bfd_vma) -1;
308   htab->data_segment_base = (bfd_vma) -1;
309 
310   return &htab->root.root;
311 }
312 
313 /* Return nonzero if ABFD represents a PA2.0 ELF64 file.
314 
315    Additionally we set the default architecture and machine.  */
316 static bfd_boolean
317 elf64_hppa_object_p (bfd *abfd)
318 {
319   Elf_Internal_Ehdr * i_ehdrp;
320   unsigned int flags;
321 
322   i_ehdrp = elf_elfheader (abfd);
323   if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
324     {
325       /* GCC on hppa-linux produces binaries with OSABI=GNU,
326 	 but the kernel produces corefiles with OSABI=SysV.  */
327       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
328 	  && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
329 	return FALSE;
330     }
331   else
332     {
333       /* HPUX produces binaries with OSABI=HPUX,
334 	 but the kernel produces corefiles with OSABI=SysV.  */
335       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
336 	  && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
337 	return FALSE;
338     }
339 
340   flags = i_ehdrp->e_flags;
341   switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
342     {
343     case EFA_PARISC_1_0:
344       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
345     case EFA_PARISC_1_1:
346       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
347     case EFA_PARISC_2_0:
348       if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
349 	return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
350       else
351 	return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
352     case EFA_PARISC_2_0 | EF_PARISC_WIDE:
353       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
354     }
355   /* Don't be fussy.  */
356   return TRUE;
357 }
358 
359 /* Given section type (hdr->sh_type), return a boolean indicating
360    whether or not the section is an elf64-hppa specific section.  */
361 static bfd_boolean
362 elf64_hppa_section_from_shdr (bfd *abfd,
363 			      Elf_Internal_Shdr *hdr,
364 			      const char *name,
365 			      int shindex)
366 {
367   switch (hdr->sh_type)
368     {
369     case SHT_PARISC_EXT:
370       if (strcmp (name, ".PARISC.archext") != 0)
371 	return FALSE;
372       break;
373     case SHT_PARISC_UNWIND:
374       if (strcmp (name, ".PARISC.unwind") != 0)
375 	return FALSE;
376       break;
377     case SHT_PARISC_DOC:
378     case SHT_PARISC_ANNOT:
379     default:
380       return FALSE;
381     }
382 
383   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
384     return FALSE;
385 
386   return TRUE;
387 }
388 
389 /* SEC is a section containing relocs for an input BFD when linking; return
390    a suitable section for holding relocs in the output BFD for a link.  */
391 
392 static bfd_boolean
393 get_reloc_section (bfd *abfd,
394 		   struct elf64_hppa_link_hash_table *hppa_info,
395 		   asection *sec)
396 {
397   const char *srel_name;
398   asection *srel;
399   bfd *dynobj;
400 
401   srel_name = (bfd_elf_string_from_elf_section
402 	       (abfd, elf_elfheader(abfd)->e_shstrndx,
403 		_bfd_elf_single_rel_hdr(sec)->sh_name));
404   if (srel_name == NULL)
405     return FALSE;
406 
407   dynobj = hppa_info->root.dynobj;
408   if (!dynobj)
409     hppa_info->root.dynobj = dynobj = abfd;
410 
411   srel = bfd_get_linker_section (dynobj, srel_name);
412   if (srel == NULL)
413     {
414       srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
415 						 (SEC_ALLOC
416 						  | SEC_LOAD
417 						  | SEC_HAS_CONTENTS
418 						  | SEC_IN_MEMORY
419 						  | SEC_LINKER_CREATED
420 						  | SEC_READONLY));
421       if (srel == NULL
422 	  || !bfd_set_section_alignment (srel, 3))
423 	return FALSE;
424     }
425 
426   hppa_info->other_rel_sec = srel;
427   return TRUE;
428 }
429 
430 /* Add a new entry to the list of dynamic relocations against DYN_H.
431 
432    We use this to keep a record of all the FPTR relocations against a
433    particular symbol so that we can create FPTR relocations in the
434    output file.  */
435 
436 static bfd_boolean
437 count_dyn_reloc (bfd *abfd,
438 		 struct elf64_hppa_link_hash_entry *hh,
439 		 int type,
440 		 asection *sec,
441 		 int sec_symndx,
442 		 bfd_vma offset,
443 		 bfd_vma addend)
444 {
445   struct elf64_hppa_dyn_reloc_entry *rent;
446 
447   rent = (struct elf64_hppa_dyn_reloc_entry *)
448   bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
449   if (!rent)
450     return FALSE;
451 
452   rent->next = hh->reloc_entries;
453   rent->type = type;
454   rent->sec = sec;
455   rent->sec_symndx = sec_symndx;
456   rent->offset = offset;
457   rent->addend = addend;
458   hh->reloc_entries = rent;
459 
460   return TRUE;
461 }
462 
463 /* Return a pointer to the local DLT, PLT and OPD reference counts
464    for ABFD.  Returns NULL if the storage allocation fails.  */
465 
466 static bfd_signed_vma *
467 hppa64_elf_local_refcounts (bfd *abfd)
468 {
469   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
470   bfd_signed_vma *local_refcounts;
471 
472   local_refcounts = elf_local_got_refcounts (abfd);
473   if (local_refcounts == NULL)
474     {
475       bfd_size_type size;
476 
477       /* Allocate space for local DLT, PLT and OPD reference
478 	 counts.  Done this way to save polluting elf_obj_tdata
479 	 with another target specific pointer.  */
480       size = symtab_hdr->sh_info;
481       size *= 3 * sizeof (bfd_signed_vma);
482       local_refcounts = bfd_zalloc (abfd, size);
483       elf_local_got_refcounts (abfd) = local_refcounts;
484     }
485   return local_refcounts;
486 }
487 
488 /* Scan the RELOCS and record the type of dynamic entries that each
489    referenced symbol needs.  */
490 
491 static bfd_boolean
492 elf64_hppa_check_relocs (bfd *abfd,
493 			 struct bfd_link_info *info,
494 			 asection *sec,
495 			 const Elf_Internal_Rela *relocs)
496 {
497   struct elf64_hppa_link_hash_table *hppa_info;
498   const Elf_Internal_Rela *relend;
499   Elf_Internal_Shdr *symtab_hdr;
500   const Elf_Internal_Rela *rel;
501   unsigned int sec_symndx;
502 
503   if (bfd_link_relocatable (info))
504     return TRUE;
505 
506   /* If this is the first dynamic object found in the link, create
507      the special sections required for dynamic linking.  */
508   if (! elf_hash_table (info)->dynamic_sections_created)
509     {
510       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
511 	return FALSE;
512     }
513 
514   hppa_info = hppa_link_hash_table (info);
515   if (hppa_info == NULL)
516     return FALSE;
517   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
518 
519   /* If necessary, build a new table holding section symbols indices
520      for this BFD.  */
521 
522   if (bfd_link_pic (info) && hppa_info->section_syms_bfd != abfd)
523     {
524       unsigned long i;
525       unsigned int highest_shndx;
526       Elf_Internal_Sym *local_syms = NULL;
527       Elf_Internal_Sym *isym, *isymend;
528       bfd_size_type amt;
529 
530       /* We're done with the old cache of section index to section symbol
531 	 index information.  Free it.
532 
533 	 ?!? Note we leak the last section_syms array.  Presumably we
534 	 could free it in one of the later routines in this file.  */
535       if (hppa_info->section_syms)
536 	free (hppa_info->section_syms);
537 
538       /* Read this BFD's local symbols.  */
539       if (symtab_hdr->sh_info != 0)
540 	{
541 	  local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
542 	  if (local_syms == NULL)
543 	    local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
544 					       symtab_hdr->sh_info, 0,
545 					       NULL, NULL, NULL);
546 	  if (local_syms == NULL)
547 	    return FALSE;
548 	}
549 
550       /* Record the highest section index referenced by the local symbols.  */
551       highest_shndx = 0;
552       isymend = local_syms + symtab_hdr->sh_info;
553       for (isym = local_syms; isym < isymend; isym++)
554 	{
555 	  if (isym->st_shndx > highest_shndx
556 	      && isym->st_shndx < SHN_LORESERVE)
557 	    highest_shndx = isym->st_shndx;
558 	}
559 
560       /* Allocate an array to hold the section index to section symbol index
561 	 mapping.  Bump by one since we start counting at zero.  */
562       highest_shndx++;
563       amt = highest_shndx;
564       amt *= sizeof (int);
565       hppa_info->section_syms = (int *) bfd_malloc (amt);
566 
567       /* Now walk the local symbols again.  If we find a section symbol,
568 	 record the index of the symbol into the section_syms array.  */
569       for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
570 	{
571 	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
572 	    hppa_info->section_syms[isym->st_shndx] = i;
573 	}
574 
575       /* We are finished with the local symbols.  */
576       if (local_syms != NULL
577 	  && symtab_hdr->contents != (unsigned char *) local_syms)
578 	{
579 	  if (! info->keep_memory)
580 	    free (local_syms);
581 	  else
582 	    {
583 	      /* Cache the symbols for elf_link_input_bfd.  */
584 	      symtab_hdr->contents = (unsigned char *) local_syms;
585 	    }
586 	}
587 
588       /* Record which BFD we built the section_syms mapping for.  */
589       hppa_info->section_syms_bfd = abfd;
590     }
591 
592   /* Record the symbol index for this input section.  We may need it for
593      relocations when building shared libraries.  When not building shared
594      libraries this value is never really used, but assign it to zero to
595      prevent out of bounds memory accesses in other routines.  */
596   if (bfd_link_pic (info))
597     {
598       sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
599 
600       /* If we did not find a section symbol for this section, then
601 	 something went terribly wrong above.  */
602       if (sec_symndx == SHN_BAD)
603 	return FALSE;
604 
605       if (sec_symndx < SHN_LORESERVE)
606 	sec_symndx = hppa_info->section_syms[sec_symndx];
607       else
608 	sec_symndx = 0;
609     }
610   else
611     sec_symndx = 0;
612 
613   relend = relocs + sec->reloc_count;
614   for (rel = relocs; rel < relend; ++rel)
615     {
616       enum
617 	{
618 	  NEED_DLT = 1,
619 	  NEED_PLT = 2,
620 	  NEED_STUB = 4,
621 	  NEED_OPD = 8,
622 	  NEED_DYNREL = 16,
623 	};
624 
625       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
626       struct elf64_hppa_link_hash_entry *hh;
627       int need_entry;
628       bfd_boolean maybe_dynamic;
629       int dynrel_type = R_PARISC_NONE;
630       static reloc_howto_type *howto;
631 
632       if (r_symndx >= symtab_hdr->sh_info)
633 	{
634 	  /* We're dealing with a global symbol -- find its hash entry
635 	     and mark it as being referenced.  */
636 	  long indx = r_symndx - symtab_hdr->sh_info;
637 	  hh = hppa_elf_hash_entry (elf_sym_hashes (abfd)[indx]);
638 	  while (hh->eh.root.type == bfd_link_hash_indirect
639 		 || hh->eh.root.type == bfd_link_hash_warning)
640 	    hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
641 
642 	  /* PR15323, ref flags aren't set for references in the same
643 	     object.  */
644 	  hh->eh.ref_regular = 1;
645 	}
646       else
647 	hh = NULL;
648 
649       /* We can only get preliminary data on whether a symbol is
650 	 locally or externally defined, as not all of the input files
651 	 have yet been processed.  Do something with what we know, as
652 	 this may help reduce memory usage and processing time later.  */
653       maybe_dynamic = FALSE;
654       if (hh && ((bfd_link_pic (info)
655 		 && (!info->symbolic
656 		     || info->unresolved_syms_in_shared_libs == RM_IGNORE))
657 		|| !hh->eh.def_regular
658 		|| hh->eh.root.type == bfd_link_hash_defweak))
659 	maybe_dynamic = TRUE;
660 
661       howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
662       need_entry = 0;
663       switch (howto->type)
664 	{
665 	/* These are simple indirect references to symbols through the
666 	   DLT.  We need to create a DLT entry for any symbols which
667 	   appears in a DLTIND relocation.  */
668 	case R_PARISC_DLTIND21L:
669 	case R_PARISC_DLTIND14R:
670 	case R_PARISC_DLTIND14F:
671 	case R_PARISC_DLTIND14WR:
672 	case R_PARISC_DLTIND14DR:
673 	  need_entry = NEED_DLT;
674 	  break;
675 
676 	/* ?!?  These need a DLT entry.  But I have no idea what to do with
677 	   the "link time TP value.  */
678 	case R_PARISC_LTOFF_TP21L:
679 	case R_PARISC_LTOFF_TP14R:
680 	case R_PARISC_LTOFF_TP14F:
681 	case R_PARISC_LTOFF_TP64:
682 	case R_PARISC_LTOFF_TP14WR:
683 	case R_PARISC_LTOFF_TP14DR:
684 	case R_PARISC_LTOFF_TP16F:
685 	case R_PARISC_LTOFF_TP16WF:
686 	case R_PARISC_LTOFF_TP16DF:
687 	  need_entry = NEED_DLT;
688 	  break;
689 
690 	/* These are function calls.  Depending on their precise target we
691 	   may need to make a stub for them.  The stub uses the PLT, so we
692 	   need to create PLT entries for these symbols too.  */
693 	case R_PARISC_PCREL12F:
694 	case R_PARISC_PCREL17F:
695 	case R_PARISC_PCREL22F:
696 	case R_PARISC_PCREL32:
697 	case R_PARISC_PCREL64:
698 	case R_PARISC_PCREL21L:
699 	case R_PARISC_PCREL17R:
700 	case R_PARISC_PCREL17C:
701 	case R_PARISC_PCREL14R:
702 	case R_PARISC_PCREL14F:
703 	case R_PARISC_PCREL22C:
704 	case R_PARISC_PCREL14WR:
705 	case R_PARISC_PCREL14DR:
706 	case R_PARISC_PCREL16F:
707 	case R_PARISC_PCREL16WF:
708 	case R_PARISC_PCREL16DF:
709 	  /* Function calls might need to go through the .plt, and
710 	     might need a long branch stub.  */
711 	  if (hh != NULL && hh->eh.type != STT_PARISC_MILLI)
712 	    need_entry = (NEED_PLT | NEED_STUB);
713 	  else
714 	    need_entry = 0;
715 	  break;
716 
717 	case R_PARISC_PLTOFF21L:
718 	case R_PARISC_PLTOFF14R:
719 	case R_PARISC_PLTOFF14F:
720 	case R_PARISC_PLTOFF14WR:
721 	case R_PARISC_PLTOFF14DR:
722 	case R_PARISC_PLTOFF16F:
723 	case R_PARISC_PLTOFF16WF:
724 	case R_PARISC_PLTOFF16DF:
725 	  need_entry = (NEED_PLT);
726 	  break;
727 
728 	case R_PARISC_DIR64:
729 	  if (bfd_link_pic (info) || maybe_dynamic)
730 	    need_entry = (NEED_DYNREL);
731 	  dynrel_type = R_PARISC_DIR64;
732 	  break;
733 
734 	/* This is an indirect reference through the DLT to get the address
735 	   of a OPD descriptor.  Thus we need to make a DLT entry that points
736 	   to an OPD entry.  */
737 	case R_PARISC_LTOFF_FPTR21L:
738 	case R_PARISC_LTOFF_FPTR14R:
739 	case R_PARISC_LTOFF_FPTR14WR:
740 	case R_PARISC_LTOFF_FPTR14DR:
741 	case R_PARISC_LTOFF_FPTR32:
742 	case R_PARISC_LTOFF_FPTR64:
743 	case R_PARISC_LTOFF_FPTR16F:
744 	case R_PARISC_LTOFF_FPTR16WF:
745 	case R_PARISC_LTOFF_FPTR16DF:
746 	  if (bfd_link_pic (info) || maybe_dynamic)
747 	    need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
748 	  else
749 	    need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
750 	  dynrel_type = R_PARISC_FPTR64;
751 	  break;
752 
753 	/* This is a simple OPD entry.  */
754 	case R_PARISC_FPTR64:
755 	  if (bfd_link_pic (info) || maybe_dynamic)
756 	    need_entry = (NEED_OPD | NEED_PLT | NEED_DYNREL);
757 	  else
758 	    need_entry = (NEED_OPD | NEED_PLT);
759 	  dynrel_type = R_PARISC_FPTR64;
760 	  break;
761 
762 	/* Add more cases as needed.  */
763 	}
764 
765       if (!need_entry)
766 	continue;
767 
768       if (hh)
769 	{
770 	  /* Stash away enough information to be able to find this symbol
771 	     regardless of whether or not it is local or global.  */
772 	  hh->owner = abfd;
773 	  hh->sym_indx = r_symndx;
774 	}
775 
776       /* Create what's needed.  */
777       if (need_entry & NEED_DLT)
778 	{
779 	  /* Allocate space for a DLT entry, as well as a dynamic
780 	     relocation for this entry.  */
781 	  if (! hppa_info->dlt_sec
782 	      && ! get_dlt (abfd, info, hppa_info))
783 	    goto err_out;
784 
785 	  if (hh != NULL)
786 	    {
787 	      hh->want_dlt = 1;
788 	      hh->eh.got.refcount += 1;
789 	    }
790 	  else
791 	    {
792 	      bfd_signed_vma *local_dlt_refcounts;
793 
794 	      /* This is a DLT entry for a local symbol.  */
795 	      local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
796 	      if (local_dlt_refcounts == NULL)
797 		return FALSE;
798 	      local_dlt_refcounts[r_symndx] += 1;
799 	    }
800 	}
801 
802       if (need_entry & NEED_PLT)
803 	{
804 	  if (! hppa_info->plt_sec
805 	      && ! get_plt (abfd, info, hppa_info))
806 	    goto err_out;
807 
808 	  if (hh != NULL)
809 	    {
810 	      hh->want_plt = 1;
811 	      hh->eh.needs_plt = 1;
812 	      hh->eh.plt.refcount += 1;
813 	    }
814 	  else
815 	    {
816 	      bfd_signed_vma *local_dlt_refcounts;
817 	      bfd_signed_vma *local_plt_refcounts;
818 
819 	      /* This is a PLT entry for a local symbol.  */
820 	      local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
821 	      if (local_dlt_refcounts == NULL)
822 		return FALSE;
823 	      local_plt_refcounts = local_dlt_refcounts + symtab_hdr->sh_info;
824 	      local_plt_refcounts[r_symndx] += 1;
825 	    }
826 	}
827 
828       if (need_entry & NEED_STUB)
829 	{
830 	  if (! hppa_info->stub_sec
831 	      && ! get_stub (abfd, info, hppa_info))
832 	    goto err_out;
833 	  if (hh)
834 	    hh->want_stub = 1;
835 	}
836 
837       if (need_entry & NEED_OPD)
838 	{
839 	  if (! hppa_info->opd_sec
840 	      && ! get_opd (abfd, info, hppa_info))
841 	    goto err_out;
842 
843 	  /* FPTRs are not allocated by the dynamic linker for PA64,
844 	     though it is possible that will change in the future.  */
845 
846 	  if (hh != NULL)
847 	    hh->want_opd = 1;
848 	  else
849 	    {
850 	      bfd_signed_vma *local_dlt_refcounts;
851 	      bfd_signed_vma *local_opd_refcounts;
852 
853 	      /* This is a OPD for a local symbol.  */
854 	      local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
855 	      if (local_dlt_refcounts == NULL)
856 		return FALSE;
857 	      local_opd_refcounts = (local_dlt_refcounts
858 				     + 2 * symtab_hdr->sh_info);
859 	      local_opd_refcounts[r_symndx] += 1;
860 	    }
861 	}
862 
863       /* Add a new dynamic relocation to the chain of dynamic
864 	 relocations for this symbol.  */
865       if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
866 	{
867 	  if (! hppa_info->other_rel_sec
868 	      && ! get_reloc_section (abfd, hppa_info, sec))
869 	    goto err_out;
870 
871 	  /* Count dynamic relocations against global symbols.  */
872 	  if (hh != NULL
873 	      && !count_dyn_reloc (abfd, hh, dynrel_type, sec,
874 				   sec_symndx, rel->r_offset, rel->r_addend))
875 	    goto err_out;
876 
877 	  /* If we are building a shared library and we just recorded
878 	     a dynamic R_PARISC_FPTR64 relocation, then make sure the
879 	     section symbol for this section ends up in the dynamic
880 	     symbol table.  */
881 	  if (bfd_link_pic (info) && dynrel_type == R_PARISC_FPTR64
882 	      && ! (bfd_elf_link_record_local_dynamic_symbol
883 		    (info, abfd, sec_symndx)))
884 	    return FALSE;
885 	}
886     }
887 
888   return TRUE;
889 
890  err_out:
891   return FALSE;
892 }
893 
894 struct elf64_hppa_allocate_data
895 {
896   struct bfd_link_info *info;
897   bfd_size_type ofs;
898 };
899 
900 /* Should we do dynamic things to this symbol?  */
901 
902 static bfd_boolean
903 elf64_hppa_dynamic_symbol_p (struct elf_link_hash_entry *eh,
904 			     struct bfd_link_info *info)
905 {
906   /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
907      and relocations that retrieve a function descriptor?  Assume the
908      worst for now.  */
909   if (_bfd_elf_dynamic_symbol_p (eh, info, 1))
910     {
911       /* ??? Why is this here and not elsewhere is_local_label_name.  */
912       if (eh->root.root.string[0] == '$' && eh->root.root.string[1] == '$')
913 	return FALSE;
914 
915       return TRUE;
916     }
917   else
918     return FALSE;
919 }
920 
921 /* Mark all functions exported by this file so that we can later allocate
922    entries in .opd for them.  */
923 
924 static bfd_boolean
925 elf64_hppa_mark_exported_functions (struct elf_link_hash_entry *eh, void *data)
926 {
927   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
928   struct bfd_link_info *info = (struct bfd_link_info *)data;
929   struct elf64_hppa_link_hash_table *hppa_info;
930 
931   hppa_info = hppa_link_hash_table (info);
932   if (hppa_info == NULL)
933     return FALSE;
934 
935   if (eh
936       && (eh->root.type == bfd_link_hash_defined
937 	  || eh->root.type == bfd_link_hash_defweak)
938       && eh->root.u.def.section->output_section != NULL
939       && eh->type == STT_FUNC)
940     {
941       if (! hppa_info->opd_sec
942 	  && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
943 	return FALSE;
944 
945       hh->want_opd = 1;
946 
947       /* Put a flag here for output_symbol_hook.  */
948       hh->st_shndx = -1;
949       eh->needs_plt = 1;
950     }
951 
952   return TRUE;
953 }
954 
955 /* Allocate space for a DLT entry.  */
956 
957 static bfd_boolean
958 allocate_global_data_dlt (struct elf_link_hash_entry *eh, void *data)
959 {
960   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
961   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
962 
963   if (hh->want_dlt)
964     {
965       if (bfd_link_pic (x->info))
966 	{
967 	  /* Possibly add the symbol to the local dynamic symbol
968 	     table since we might need to create a dynamic relocation
969 	     against it.  */
970 	  if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
971 	    {
972 	      bfd *owner = eh->root.u.def.section->owner;
973 
974 	      if (! (bfd_elf_link_record_local_dynamic_symbol
975 		     (x->info, owner, hh->sym_indx)))
976 		return FALSE;
977 	    }
978 	}
979 
980       hh->dlt_offset = x->ofs;
981       x->ofs += DLT_ENTRY_SIZE;
982     }
983   return TRUE;
984 }
985 
986 /* Allocate space for a DLT.PLT entry.  */
987 
988 static bfd_boolean
989 allocate_global_data_plt (struct elf_link_hash_entry *eh, void *data)
990 {
991   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
992   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *) data;
993 
994   if (hh->want_plt
995       && elf64_hppa_dynamic_symbol_p (eh, x->info)
996       && !((eh->root.type == bfd_link_hash_defined
997 	    || eh->root.type == bfd_link_hash_defweak)
998 	   && eh->root.u.def.section->output_section != NULL))
999     {
1000       hh->plt_offset = x->ofs;
1001       x->ofs += PLT_ENTRY_SIZE;
1002       if (hh->plt_offset < 0x2000)
1003 	{
1004 	  struct elf64_hppa_link_hash_table *hppa_info;
1005 
1006 	  hppa_info = hppa_link_hash_table (x->info);
1007 	  if (hppa_info == NULL)
1008 	    return FALSE;
1009 
1010 	  hppa_info->gp_offset = hh->plt_offset;
1011 	}
1012     }
1013   else
1014     hh->want_plt = 0;
1015 
1016   return TRUE;
1017 }
1018 
1019 /* Allocate space for a STUB entry.  */
1020 
1021 static bfd_boolean
1022 allocate_global_data_stub (struct elf_link_hash_entry *eh, void *data)
1023 {
1024   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1025   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1026 
1027   if (hh->want_stub
1028       && elf64_hppa_dynamic_symbol_p (eh, x->info)
1029       && !((eh->root.type == bfd_link_hash_defined
1030 	    || eh->root.type == bfd_link_hash_defweak)
1031 	   && eh->root.u.def.section->output_section != NULL))
1032     {
1033       hh->stub_offset = x->ofs;
1034       x->ofs += sizeof (plt_stub);
1035     }
1036   else
1037     hh->want_stub = 0;
1038   return TRUE;
1039 }
1040 
1041 /* Allocate space for a FPTR entry.  */
1042 
1043 static bfd_boolean
1044 allocate_global_data_opd (struct elf_link_hash_entry *eh, void *data)
1045 {
1046   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1047   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1048 
1049   if (hh && hh->want_opd)
1050     {
1051       /* We never need an opd entry for a symbol which is not
1052 	 defined by this output file.  */
1053       if (hh && (hh->eh.root.type == bfd_link_hash_undefined
1054 		 || hh->eh.root.type == bfd_link_hash_undefweak
1055 		 || hh->eh.root.u.def.section->output_section == NULL))
1056 	hh->want_opd = 0;
1057 
1058       /* If we are creating a shared library, took the address of a local
1059 	 function or might export this function from this object file, then
1060 	 we have to create an opd descriptor.  */
1061       else if (bfd_link_pic (x->info)
1062 	       || hh == NULL
1063 	       || (hh->eh.dynindx == -1 && hh->eh.type != STT_PARISC_MILLI)
1064 	       || (hh->eh.root.type == bfd_link_hash_defined
1065 		   || hh->eh.root.type == bfd_link_hash_defweak))
1066 	{
1067 	  /* If we are creating a shared library, then we will have to
1068 	     create a runtime relocation for the symbol to properly
1069 	     initialize the .opd entry.  Make sure the symbol gets
1070 	     added to the dynamic symbol table.  */
1071 	  if (bfd_link_pic (x->info)
1072 	      && (hh == NULL || (hh->eh.dynindx == -1)))
1073 	    {
1074 	      bfd *owner;
1075 	      /* PR 6511: Default to using the dynamic symbol table.  */
1076 	      owner = (hh->owner ? hh->owner: eh->root.u.def.section->owner);
1077 
1078 	      if (!bfd_elf_link_record_local_dynamic_symbol
1079 		    (x->info, owner, hh->sym_indx))
1080 		return FALSE;
1081 	    }
1082 
1083 	  /* This may not be necessary or desirable anymore now that
1084 	     we have some support for dealing with section symbols
1085 	     in dynamic relocs.  But name munging does make the result
1086 	     much easier to debug.  ie, the EPLT reloc will reference
1087 	     a symbol like .foobar, instead of .text + offset.  */
1088 	  if (bfd_link_pic (x->info) && eh)
1089 	    {
1090 	      char *new_name;
1091 	      struct elf_link_hash_entry *nh;
1092 
1093 	      new_name = concat (".", eh->root.root.string, NULL);
1094 
1095 	      nh = elf_link_hash_lookup (elf_hash_table (x->info),
1096 					 new_name, TRUE, TRUE, TRUE);
1097 
1098 	      free (new_name);
1099 	      nh->root.type = eh->root.type;
1100 	      nh->root.u.def.value = eh->root.u.def.value;
1101 	      nh->root.u.def.section = eh->root.u.def.section;
1102 
1103 	      if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
1104 		return FALSE;
1105 	     }
1106 	  hh->opd_offset = x->ofs;
1107 	  x->ofs += OPD_ENTRY_SIZE;
1108 	}
1109 
1110       /* Otherwise we do not need an opd entry.  */
1111       else
1112 	hh->want_opd = 0;
1113     }
1114   return TRUE;
1115 }
1116 
1117 /* HP requires the EI_OSABI field to be filled in.  The assignment to
1118    EI_ABIVERSION may not be strictly necessary.  */
1119 
1120 static bfd_boolean
1121 elf64_hppa_init_file_header (bfd *abfd, struct bfd_link_info *info)
1122 {
1123   Elf_Internal_Ehdr *i_ehdrp;
1124 
1125   if (!_bfd_elf_init_file_header (abfd, info))
1126     return FALSE;
1127 
1128   i_ehdrp = elf_elfheader (abfd);
1129   i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
1130   i_ehdrp->e_ident[EI_ABIVERSION] = 1;
1131   return TRUE;
1132 }
1133 
1134 /* Create function descriptor section (.opd).  This section is called .opd
1135    because it contains "official procedure descriptors".  The "official"
1136    refers to the fact that these descriptors are used when taking the address
1137    of a procedure, thus ensuring a unique address for each procedure.  */
1138 
1139 static bfd_boolean
1140 get_opd (bfd *abfd,
1141 	 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1142 	 struct elf64_hppa_link_hash_table *hppa_info)
1143 {
1144   asection *opd;
1145   bfd *dynobj;
1146 
1147   opd = hppa_info->opd_sec;
1148   if (!opd)
1149     {
1150       dynobj = hppa_info->root.dynobj;
1151       if (!dynobj)
1152 	hppa_info->root.dynobj = dynobj = abfd;
1153 
1154       opd = bfd_make_section_anyway_with_flags (dynobj, ".opd",
1155 						(SEC_ALLOC
1156 						 | SEC_LOAD
1157 						 | SEC_HAS_CONTENTS
1158 						 | SEC_IN_MEMORY
1159 						 | SEC_LINKER_CREATED));
1160       if (!opd
1161 	  || !bfd_set_section_alignment (opd, 3))
1162 	{
1163 	  BFD_ASSERT (0);
1164 	  return FALSE;
1165 	}
1166 
1167       hppa_info->opd_sec = opd;
1168     }
1169 
1170   return TRUE;
1171 }
1172 
1173 /* Create the PLT section.  */
1174 
1175 static bfd_boolean
1176 get_plt (bfd *abfd,
1177 	 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1178 	 struct elf64_hppa_link_hash_table *hppa_info)
1179 {
1180   asection *plt;
1181   bfd *dynobj;
1182 
1183   plt = hppa_info->plt_sec;
1184   if (!plt)
1185     {
1186       dynobj = hppa_info->root.dynobj;
1187       if (!dynobj)
1188 	hppa_info->root.dynobj = dynobj = abfd;
1189 
1190       plt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
1191 						(SEC_ALLOC
1192 						 | SEC_LOAD
1193 						 | SEC_HAS_CONTENTS
1194 						 | SEC_IN_MEMORY
1195 						 | SEC_LINKER_CREATED));
1196       if (!plt
1197 	  || !bfd_set_section_alignment (plt, 3))
1198 	{
1199 	  BFD_ASSERT (0);
1200 	  return FALSE;
1201 	}
1202 
1203       hppa_info->plt_sec = plt;
1204     }
1205 
1206   return TRUE;
1207 }
1208 
1209 /* Create the DLT section.  */
1210 
1211 static bfd_boolean
1212 get_dlt (bfd *abfd,
1213 	 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1214 	 struct elf64_hppa_link_hash_table *hppa_info)
1215 {
1216   asection *dlt;
1217   bfd *dynobj;
1218 
1219   dlt = hppa_info->dlt_sec;
1220   if (!dlt)
1221     {
1222       dynobj = hppa_info->root.dynobj;
1223       if (!dynobj)
1224 	hppa_info->root.dynobj = dynobj = abfd;
1225 
1226       dlt = bfd_make_section_anyway_with_flags (dynobj, ".dlt",
1227 						(SEC_ALLOC
1228 						 | SEC_LOAD
1229 						 | SEC_HAS_CONTENTS
1230 						 | SEC_IN_MEMORY
1231 						 | SEC_LINKER_CREATED));
1232       if (!dlt
1233 	  || !bfd_set_section_alignment (dlt, 3))
1234 	{
1235 	  BFD_ASSERT (0);
1236 	  return FALSE;
1237 	}
1238 
1239       hppa_info->dlt_sec = dlt;
1240     }
1241 
1242   return TRUE;
1243 }
1244 
1245 /* Create the stubs section.  */
1246 
1247 static bfd_boolean
1248 get_stub (bfd *abfd,
1249 	  struct bfd_link_info *info ATTRIBUTE_UNUSED,
1250 	  struct elf64_hppa_link_hash_table *hppa_info)
1251 {
1252   asection *stub;
1253   bfd *dynobj;
1254 
1255   stub = hppa_info->stub_sec;
1256   if (!stub)
1257     {
1258       dynobj = hppa_info->root.dynobj;
1259       if (!dynobj)
1260 	hppa_info->root.dynobj = dynobj = abfd;
1261 
1262       stub = bfd_make_section_anyway_with_flags (dynobj, ".stub",
1263 						 (SEC_ALLOC | SEC_LOAD
1264 						  | SEC_HAS_CONTENTS
1265 						  | SEC_IN_MEMORY
1266 						  | SEC_READONLY
1267 						  | SEC_LINKER_CREATED));
1268       if (!stub
1269 	  || !bfd_set_section_alignment (stub, 3))
1270 	{
1271 	  BFD_ASSERT (0);
1272 	  return FALSE;
1273 	}
1274 
1275       hppa_info->stub_sec = stub;
1276     }
1277 
1278   return TRUE;
1279 }
1280 
1281 /* Create sections necessary for dynamic linking.  This is only a rough
1282    cut and will likely change as we learn more about the somewhat
1283    unusual dynamic linking scheme HP uses.
1284 
1285    .stub:
1286 	Contains code to implement cross-space calls.  The first time one
1287 	of the stubs is used it will call into the dynamic linker, later
1288 	calls will go straight to the target.
1289 
1290 	The only stub we support right now looks like
1291 
1292 	ldd OFFSET(%dp),%r1
1293 	bve %r0(%r1)
1294 	ldd OFFSET+8(%dp),%dp
1295 
1296 	Other stubs may be needed in the future.  We may want the remove
1297 	the break/nop instruction.  It is only used right now to keep the
1298 	offset of a .plt entry and a .stub entry in sync.
1299 
1300    .dlt:
1301 	This is what most people call the .got.  HP used a different name.
1302 	Losers.
1303 
1304    .rela.dlt:
1305 	Relocations for the DLT.
1306 
1307    .plt:
1308 	Function pointers as address,gp pairs.
1309 
1310    .rela.plt:
1311 	Should contain dynamic IPLT (and EPLT?) relocations.
1312 
1313    .opd:
1314 	FPTRS
1315 
1316    .rela.opd:
1317 	EPLT relocations for symbols exported from shared libraries.  */
1318 
1319 static bfd_boolean
1320 elf64_hppa_create_dynamic_sections (bfd *abfd,
1321 				    struct bfd_link_info *info)
1322 {
1323   asection *s;
1324   struct elf64_hppa_link_hash_table *hppa_info;
1325 
1326   hppa_info = hppa_link_hash_table (info);
1327   if (hppa_info == NULL)
1328     return FALSE;
1329 
1330   if (! get_stub (abfd, info, hppa_info))
1331     return FALSE;
1332 
1333   if (! get_dlt (abfd, info, hppa_info))
1334     return FALSE;
1335 
1336   if (! get_plt (abfd, info, hppa_info))
1337     return FALSE;
1338 
1339   if (! get_opd (abfd, info, hppa_info))
1340     return FALSE;
1341 
1342   s = bfd_make_section_anyway_with_flags (abfd, ".rela.dlt",
1343 					  (SEC_ALLOC | SEC_LOAD
1344 					   | SEC_HAS_CONTENTS
1345 					   | SEC_IN_MEMORY
1346 					   | SEC_READONLY
1347 					   | SEC_LINKER_CREATED));
1348   if (s == NULL
1349       || !bfd_set_section_alignment (s, 3))
1350     return FALSE;
1351   hppa_info->dlt_rel_sec = s;
1352 
1353   s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt",
1354 					  (SEC_ALLOC | SEC_LOAD
1355 					   | SEC_HAS_CONTENTS
1356 					   | SEC_IN_MEMORY
1357 					   | SEC_READONLY
1358 					   | SEC_LINKER_CREATED));
1359   if (s == NULL
1360       || !bfd_set_section_alignment (s, 3))
1361     return FALSE;
1362   hppa_info->plt_rel_sec = s;
1363 
1364   s = bfd_make_section_anyway_with_flags (abfd, ".rela.data",
1365 					  (SEC_ALLOC | SEC_LOAD
1366 					   | SEC_HAS_CONTENTS
1367 					   | SEC_IN_MEMORY
1368 					   | SEC_READONLY
1369 					   | SEC_LINKER_CREATED));
1370   if (s == NULL
1371       || !bfd_set_section_alignment (s, 3))
1372     return FALSE;
1373   hppa_info->other_rel_sec = s;
1374 
1375   s = bfd_make_section_anyway_with_flags (abfd, ".rela.opd",
1376 					  (SEC_ALLOC | SEC_LOAD
1377 					   | SEC_HAS_CONTENTS
1378 					   | SEC_IN_MEMORY
1379 					   | SEC_READONLY
1380 					   | SEC_LINKER_CREATED));
1381   if (s == NULL
1382       || !bfd_set_section_alignment (s, 3))
1383     return FALSE;
1384   hppa_info->opd_rel_sec = s;
1385 
1386   return TRUE;
1387 }
1388 
1389 /* Allocate dynamic relocations for those symbols that turned out
1390    to be dynamic.  */
1391 
1392 static bfd_boolean
1393 allocate_dynrel_entries (struct elf_link_hash_entry *eh, void *data)
1394 {
1395   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1396   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1397   struct elf64_hppa_link_hash_table *hppa_info;
1398   struct elf64_hppa_dyn_reloc_entry *rent;
1399   bfd_boolean dynamic_symbol, shared;
1400 
1401   hppa_info = hppa_link_hash_table (x->info);
1402   if (hppa_info == NULL)
1403     return FALSE;
1404 
1405   dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, x->info);
1406   shared = bfd_link_pic (x->info);
1407 
1408   /* We may need to allocate relocations for a non-dynamic symbol
1409      when creating a shared library.  */
1410   if (!dynamic_symbol && !shared)
1411     return TRUE;
1412 
1413   /* Take care of the normal data relocations.  */
1414 
1415   for (rent = hh->reloc_entries; rent; rent = rent->next)
1416     {
1417       /* Allocate one iff we are building a shared library, the relocation
1418 	 isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
1419       if (!shared && rent->type == R_PARISC_FPTR64 && hh->want_opd)
1420 	continue;
1421 
1422       hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
1423 
1424       /* Make sure this symbol gets into the dynamic symbol table if it is
1425 	 not already recorded.  ?!? This should not be in the loop since
1426 	 the symbol need only be added once.  */
1427       if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
1428 	if (!bfd_elf_link_record_local_dynamic_symbol
1429 	    (x->info, rent->sec->owner, hh->sym_indx))
1430 	  return FALSE;
1431     }
1432 
1433   /* Take care of the GOT and PLT relocations.  */
1434 
1435   if ((dynamic_symbol || shared) && hh->want_dlt)
1436     hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
1437 
1438   /* If we are building a shared library, then every symbol that has an
1439      opd entry will need an EPLT relocation to relocate the symbol's address
1440      and __gp value based on the runtime load address.  */
1441   if (shared && hh->want_opd)
1442     hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
1443 
1444   if (hh->want_plt && dynamic_symbol)
1445     {
1446       bfd_size_type t = 0;
1447 
1448       /* Dynamic symbols get one IPLT relocation.  Local symbols in
1449 	 shared libraries get two REL relocations.  Local symbols in
1450 	 main applications get nothing.  */
1451       if (dynamic_symbol)
1452 	t = sizeof (Elf64_External_Rela);
1453       else if (shared)
1454 	t = 2 * sizeof (Elf64_External_Rela);
1455 
1456       hppa_info->plt_rel_sec->size += t;
1457     }
1458 
1459   return TRUE;
1460 }
1461 
1462 /* Adjust a symbol defined by a dynamic object and referenced by a
1463    regular object.  */
1464 
1465 static bfd_boolean
1466 elf64_hppa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1467 				  struct elf_link_hash_entry *eh)
1468 {
1469   /* ??? Undefined symbols with PLT entries should be re-defined
1470      to be the PLT entry.  */
1471 
1472   /* If this is a weak symbol, and there is a real definition, the
1473      processor independent code will have arranged for us to see the
1474      real definition first, and we can just use the same value.  */
1475   if (eh->is_weakalias)
1476     {
1477       struct elf_link_hash_entry *def = weakdef (eh);
1478       BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1479       eh->root.u.def.section = def->root.u.def.section;
1480       eh->root.u.def.value = def->root.u.def.value;
1481       return TRUE;
1482     }
1483 
1484   /* If this is a reference to a symbol defined by a dynamic object which
1485      is not a function, we might allocate the symbol in our .dynbss section
1486      and allocate a COPY dynamic relocation.
1487 
1488      But PA64 code is canonically PIC, so as a rule we can avoid this sort
1489      of hackery.  */
1490 
1491   return TRUE;
1492 }
1493 
1494 /* This function is called via elf_link_hash_traverse to mark millicode
1495    symbols with a dynindx of -1 and to remove the string table reference
1496    from the dynamic symbol table.  If the symbol is not a millicode symbol,
1497    elf64_hppa_mark_exported_functions is called.  */
1498 
1499 static bfd_boolean
1500 elf64_hppa_mark_milli_and_exported_functions (struct elf_link_hash_entry *eh,
1501 					      void *data)
1502 {
1503   struct bfd_link_info *info = (struct bfd_link_info *) data;
1504 
1505   if (eh->type == STT_PARISC_MILLI)
1506     {
1507       if (eh->dynindx != -1)
1508 	{
1509 	  eh->dynindx = -1;
1510 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1511 				  eh->dynstr_index);
1512 	}
1513       return TRUE;
1514     }
1515 
1516   return elf64_hppa_mark_exported_functions (eh, data);
1517 }
1518 
1519 /* Set the final sizes of the dynamic sections and allocate memory for
1520    the contents of our special sections.  */
1521 
1522 static bfd_boolean
1523 elf64_hppa_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
1524 {
1525   struct elf64_hppa_link_hash_table *hppa_info;
1526   struct elf64_hppa_allocate_data data;
1527   bfd *dynobj;
1528   bfd *ibfd;
1529   asection *sec;
1530   bfd_boolean plt;
1531   bfd_boolean relocs;
1532   bfd_boolean reltext;
1533 
1534   hppa_info = hppa_link_hash_table (info);
1535   if (hppa_info == NULL)
1536     return FALSE;
1537 
1538   dynobj = hppa_info->root.dynobj;
1539   BFD_ASSERT (dynobj != NULL);
1540 
1541   /* Mark each function this program exports so that we will allocate
1542      space in the .opd section for each function's FPTR.  If we are
1543      creating dynamic sections, change the dynamic index of millicode
1544      symbols to -1 and remove them from the string table for .dynstr.
1545 
1546      We have to traverse the main linker hash table since we have to
1547      find functions which may not have been mentioned in any relocs.  */
1548   elf_link_hash_traverse (&hppa_info->root,
1549 			  (hppa_info->root.dynamic_sections_created
1550 			   ? elf64_hppa_mark_milli_and_exported_functions
1551 			   : elf64_hppa_mark_exported_functions),
1552 			  info);
1553 
1554   if (hppa_info->root.dynamic_sections_created)
1555     {
1556       /* Set the contents of the .interp section to the interpreter.  */
1557       if (bfd_link_executable (info) && !info->nointerp)
1558 	{
1559 	  sec = bfd_get_linker_section (dynobj, ".interp");
1560 	  BFD_ASSERT (sec != NULL);
1561 	  sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
1562 	  sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1563 	}
1564     }
1565   else
1566     {
1567       /* We may have created entries in the .rela.got section.
1568 	 However, if we are not creating the dynamic sections, we will
1569 	 not actually use these entries.  Reset the size of .rela.dlt,
1570 	 which will cause it to get stripped from the output file
1571 	 below.  */
1572       sec = hppa_info->dlt_rel_sec;
1573       if (sec != NULL)
1574 	sec->size = 0;
1575     }
1576 
1577   /* Set up DLT, PLT and OPD offsets for local syms, and space for local
1578      dynamic relocs.  */
1579   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1580     {
1581       bfd_signed_vma *local_dlt;
1582       bfd_signed_vma *end_local_dlt;
1583       bfd_signed_vma *local_plt;
1584       bfd_signed_vma *end_local_plt;
1585       bfd_signed_vma *local_opd;
1586       bfd_signed_vma *end_local_opd;
1587       bfd_size_type locsymcount;
1588       Elf_Internal_Shdr *symtab_hdr;
1589       asection *srel;
1590 
1591       if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1592 	continue;
1593 
1594       for (sec = ibfd->sections; sec != NULL; sec = sec->next)
1595 	{
1596 	  struct elf64_hppa_dyn_reloc_entry *hdh_p;
1597 
1598 	  for (hdh_p = ((struct elf64_hppa_dyn_reloc_entry *)
1599 		    elf_section_data (sec)->local_dynrel);
1600 	       hdh_p != NULL;
1601 	       hdh_p = hdh_p->next)
1602 	    {
1603 	      if (!bfd_is_abs_section (hdh_p->sec)
1604 		  && bfd_is_abs_section (hdh_p->sec->output_section))
1605 		{
1606 		  /* Input section has been discarded, either because
1607 		     it is a copy of a linkonce section or due to
1608 		     linker script /DISCARD/, so we'll be discarding
1609 		     the relocs too.  */
1610 		}
1611 	      else if (hdh_p->count != 0)
1612 		{
1613 		  srel = elf_section_data (hdh_p->sec)->sreloc;
1614 		  srel->size += hdh_p->count * sizeof (Elf64_External_Rela);
1615 		  if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
1616 		    info->flags |= DF_TEXTREL;
1617 		}
1618 	    }
1619 	}
1620 
1621       local_dlt = elf_local_got_refcounts (ibfd);
1622       if (!local_dlt)
1623 	continue;
1624 
1625       symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1626       locsymcount = symtab_hdr->sh_info;
1627       end_local_dlt = local_dlt + locsymcount;
1628       sec = hppa_info->dlt_sec;
1629       srel = hppa_info->dlt_rel_sec;
1630       for (; local_dlt < end_local_dlt; ++local_dlt)
1631 	{
1632 	  if (*local_dlt > 0)
1633 	    {
1634 	      *local_dlt = sec->size;
1635 	      sec->size += DLT_ENTRY_SIZE;
1636 	      if (bfd_link_pic (info))
1637 		{
1638 		  srel->size += sizeof (Elf64_External_Rela);
1639 		}
1640 	    }
1641 	  else
1642 	    *local_dlt = (bfd_vma) -1;
1643 	}
1644 
1645       local_plt = end_local_dlt;
1646       end_local_plt = local_plt + locsymcount;
1647       if (! hppa_info->root.dynamic_sections_created)
1648 	{
1649 	  /* Won't be used, but be safe.  */
1650 	  for (; local_plt < end_local_plt; ++local_plt)
1651 	    *local_plt = (bfd_vma) -1;
1652 	}
1653       else
1654 	{
1655 	  sec = hppa_info->plt_sec;
1656 	  srel = hppa_info->plt_rel_sec;
1657 	  for (; local_plt < end_local_plt; ++local_plt)
1658 	    {
1659 	      if (*local_plt > 0)
1660 		{
1661 		  *local_plt = sec->size;
1662 		  sec->size += PLT_ENTRY_SIZE;
1663 		  if (bfd_link_pic (info))
1664 		    srel->size += sizeof (Elf64_External_Rela);
1665 		}
1666 	      else
1667 		*local_plt = (bfd_vma) -1;
1668 	    }
1669 	}
1670 
1671       local_opd = end_local_plt;
1672       end_local_opd = local_opd + locsymcount;
1673       if (! hppa_info->root.dynamic_sections_created)
1674 	{
1675 	  /* Won't be used, but be safe.  */
1676 	  for (; local_opd < end_local_opd; ++local_opd)
1677 	    *local_opd = (bfd_vma) -1;
1678 	}
1679       else
1680 	{
1681 	  sec = hppa_info->opd_sec;
1682 	  srel = hppa_info->opd_rel_sec;
1683 	  for (; local_opd < end_local_opd; ++local_opd)
1684 	    {
1685 	      if (*local_opd > 0)
1686 		{
1687 		  *local_opd = sec->size;
1688 		  sec->size += OPD_ENTRY_SIZE;
1689 		  if (bfd_link_pic (info))
1690 		    srel->size += sizeof (Elf64_External_Rela);
1691 		}
1692 	      else
1693 		*local_opd = (bfd_vma) -1;
1694 	    }
1695 	}
1696     }
1697 
1698   /* Allocate the GOT entries.  */
1699 
1700   data.info = info;
1701   if (hppa_info->dlt_sec)
1702     {
1703       data.ofs = hppa_info->dlt_sec->size;
1704       elf_link_hash_traverse (&hppa_info->root,
1705 			      allocate_global_data_dlt, &data);
1706       hppa_info->dlt_sec->size = data.ofs;
1707     }
1708 
1709   if (hppa_info->plt_sec)
1710     {
1711       data.ofs = hppa_info->plt_sec->size;
1712       elf_link_hash_traverse (&hppa_info->root,
1713 			      allocate_global_data_plt, &data);
1714       hppa_info->plt_sec->size = data.ofs;
1715     }
1716 
1717   if (hppa_info->stub_sec)
1718     {
1719       data.ofs = 0x0;
1720       elf_link_hash_traverse (&hppa_info->root,
1721 			      allocate_global_data_stub, &data);
1722       hppa_info->stub_sec->size = data.ofs;
1723     }
1724 
1725   /* Allocate space for entries in the .opd section.  */
1726   if (hppa_info->opd_sec)
1727     {
1728       data.ofs = hppa_info->opd_sec->size;
1729       elf_link_hash_traverse (&hppa_info->root,
1730 			      allocate_global_data_opd, &data);
1731       hppa_info->opd_sec->size = data.ofs;
1732     }
1733 
1734   /* Now allocate space for dynamic relocations, if necessary.  */
1735   if (hppa_info->root.dynamic_sections_created)
1736     elf_link_hash_traverse (&hppa_info->root,
1737 			    allocate_dynrel_entries, &data);
1738 
1739   /* The sizes of all the sections are set.  Allocate memory for them.  */
1740   plt = FALSE;
1741   relocs = FALSE;
1742   reltext = FALSE;
1743   for (sec = dynobj->sections; sec != NULL; sec = sec->next)
1744     {
1745       const char *name;
1746 
1747       if ((sec->flags & SEC_LINKER_CREATED) == 0)
1748 	continue;
1749 
1750       /* It's OK to base decisions on the section name, because none
1751 	 of the dynobj section names depend upon the input files.  */
1752       name = bfd_section_name (sec);
1753 
1754       if (strcmp (name, ".plt") == 0)
1755 	{
1756 	  /* Remember whether there is a PLT.  */
1757 	  plt = sec->size != 0;
1758 	}
1759       else if (strcmp (name, ".opd") == 0
1760 	       || CONST_STRNEQ (name, ".dlt")
1761 	       || strcmp (name, ".stub") == 0
1762 	       || strcmp (name, ".got") == 0)
1763 	{
1764 	  /* Strip this section if we don't need it; see the comment below.  */
1765 	}
1766       else if (CONST_STRNEQ (name, ".rela"))
1767 	{
1768 	  if (sec->size != 0)
1769 	    {
1770 	      asection *target;
1771 
1772 	      /* Remember whether there are any reloc sections other
1773 		 than .rela.plt.  */
1774 	      if (strcmp (name, ".rela.plt") != 0)
1775 		{
1776 		  const char *outname;
1777 
1778 		  relocs = TRUE;
1779 
1780 		  /* If this relocation section applies to a read only
1781 		     section, then we probably need a DT_TEXTREL
1782 		     entry.  The entries in the .rela.plt section
1783 		     really apply to the .got section, which we
1784 		     created ourselves and so know is not readonly.  */
1785 		  outname = bfd_section_name (sec->output_section);
1786 		  target = bfd_get_section_by_name (output_bfd, outname + 4);
1787 		  if (target != NULL
1788 		      && (target->flags & SEC_READONLY) != 0
1789 		      && (target->flags & SEC_ALLOC) != 0)
1790 		    reltext = TRUE;
1791 		}
1792 
1793 	      /* We use the reloc_count field as a counter if we need
1794 		 to copy relocs into the output file.  */
1795 	      sec->reloc_count = 0;
1796 	    }
1797 	}
1798       else
1799 	{
1800 	  /* It's not one of our sections, so don't allocate space.  */
1801 	  continue;
1802 	}
1803 
1804       if (sec->size == 0)
1805 	{
1806 	  /* If we don't need this section, strip it from the
1807 	     output file.  This is mostly to handle .rela.bss and
1808 	     .rela.plt.  We must create both sections in
1809 	     create_dynamic_sections, because they must be created
1810 	     before the linker maps input sections to output
1811 	     sections.  The linker does that before
1812 	     adjust_dynamic_symbol is called, and it is that
1813 	     function which decides whether anything needs to go
1814 	     into these sections.  */
1815 	  sec->flags |= SEC_EXCLUDE;
1816 	  continue;
1817 	}
1818 
1819       if ((sec->flags & SEC_HAS_CONTENTS) == 0)
1820 	continue;
1821 
1822       /* Allocate memory for the section contents if it has not
1823 	 been allocated already.  We use bfd_zalloc here in case
1824 	 unused entries are not reclaimed before the section's
1825 	 contents are written out.  This should not happen, but this
1826 	 way if it does, we get a R_PARISC_NONE reloc instead of
1827 	 garbage.  */
1828       if (sec->contents == NULL)
1829 	{
1830 	  sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
1831 	  if (sec->contents == NULL)
1832 	    return FALSE;
1833 	}
1834     }
1835 
1836   if (hppa_info->root.dynamic_sections_created)
1837     {
1838       /* Always create a DT_PLTGOT.  It actually has nothing to do with
1839 	 the PLT, it is how we communicate the __gp value of a load
1840 	 module to the dynamic linker.  */
1841 #define add_dynamic_entry(TAG, VAL) \
1842   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1843 
1844       if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0)
1845 	  || !add_dynamic_entry (DT_PLTGOT, 0))
1846 	return FALSE;
1847 
1848       /* Add some entries to the .dynamic section.  We fill in the
1849 	 values later, in elf64_hppa_finish_dynamic_sections, but we
1850 	 must add the entries now so that we get the correct size for
1851 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
1852 	 dynamic linker and used by the debugger.  */
1853       if (! bfd_link_pic (info))
1854 	{
1855 	  if (!add_dynamic_entry (DT_DEBUG, 0)
1856 	      || !add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1857 	      || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
1858 	    return FALSE;
1859 	}
1860 
1861       /* Force DT_FLAGS to always be set.
1862 	 Required by HPUX 11.00 patch PHSS_26559.  */
1863       if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1864 	return FALSE;
1865 
1866       if (plt)
1867 	{
1868 	  if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1869 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1870 	      || !add_dynamic_entry (DT_JMPREL, 0))
1871 	    return FALSE;
1872 	}
1873 
1874       if (relocs)
1875 	{
1876 	  if (!add_dynamic_entry (DT_RELA, 0)
1877 	      || !add_dynamic_entry (DT_RELASZ, 0)
1878 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1879 	    return FALSE;
1880 	}
1881 
1882       if (reltext)
1883 	{
1884 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
1885 	    return FALSE;
1886 	  info->flags |= DF_TEXTREL;
1887 	}
1888     }
1889 #undef add_dynamic_entry
1890 
1891   return TRUE;
1892 }
1893 
1894 /* Called after we have output the symbol into the dynamic symbol
1895    table, but before we output the symbol into the normal symbol
1896    table.
1897 
1898    For some symbols we had to change their address when outputting
1899    the dynamic symbol table.  We undo that change here so that
1900    the symbols have their expected value in the normal symbol
1901    table.  Ick.  */
1902 
1903 static int
1904 elf64_hppa_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1905 				    const char *name,
1906 				    Elf_Internal_Sym *sym,
1907 				    asection *input_sec ATTRIBUTE_UNUSED,
1908 				    struct elf_link_hash_entry *eh)
1909 {
1910   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1911 
1912   /* We may be called with the file symbol or section symbols.
1913      They never need munging, so it is safe to ignore them.  */
1914   if (!name || !eh)
1915     return 1;
1916 
1917   /* Function symbols for which we created .opd entries *may* have been
1918      munged by finish_dynamic_symbol and have to be un-munged here.
1919 
1920      Note that finish_dynamic_symbol sometimes turns dynamic symbols
1921      into non-dynamic ones, so we initialize st_shndx to -1 in
1922      mark_exported_functions and check to see if it was overwritten
1923      here instead of just checking eh->dynindx.  */
1924   if (hh->want_opd && hh->st_shndx != -1)
1925     {
1926       /* Restore the saved value and section index.  */
1927       sym->st_value = hh->st_value;
1928       sym->st_shndx = hh->st_shndx;
1929     }
1930 
1931   return 1;
1932 }
1933 
1934 /* Finish up dynamic symbol handling.  We set the contents of various
1935    dynamic sections here.  */
1936 
1937 static bfd_boolean
1938 elf64_hppa_finish_dynamic_symbol (bfd *output_bfd,
1939 				  struct bfd_link_info *info,
1940 				  struct elf_link_hash_entry *eh,
1941 				  Elf_Internal_Sym *sym)
1942 {
1943   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1944   asection *stub, *splt, *sopd, *spltrel;
1945   struct elf64_hppa_link_hash_table *hppa_info;
1946 
1947   hppa_info = hppa_link_hash_table (info);
1948   if (hppa_info == NULL)
1949     return FALSE;
1950 
1951   stub = hppa_info->stub_sec;
1952   splt = hppa_info->plt_sec;
1953   sopd = hppa_info->opd_sec;
1954   spltrel = hppa_info->plt_rel_sec;
1955 
1956   /* Incredible.  It is actually necessary to NOT use the symbol's real
1957      value when building the dynamic symbol table for a shared library.
1958      At least for symbols that refer to functions.
1959 
1960      We will store a new value and section index into the symbol long
1961      enough to output it into the dynamic symbol table, then we restore
1962      the original values (in elf64_hppa_link_output_symbol_hook).  */
1963   if (hh->want_opd)
1964     {
1965       BFD_ASSERT (sopd != NULL);
1966 
1967       /* Save away the original value and section index so that we
1968 	 can restore them later.  */
1969       hh->st_value = sym->st_value;
1970       hh->st_shndx = sym->st_shndx;
1971 
1972       /* For the dynamic symbol table entry, we want the value to be
1973 	 address of this symbol's entry within the .opd section.  */
1974       sym->st_value = (hh->opd_offset
1975 		       + sopd->output_offset
1976 		       + sopd->output_section->vma);
1977       sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1978 							 sopd->output_section);
1979     }
1980 
1981   /* Initialize a .plt entry if requested.  */
1982   if (hh->want_plt
1983       && elf64_hppa_dynamic_symbol_p (eh, info))
1984     {
1985       bfd_vma value;
1986       Elf_Internal_Rela rel;
1987       bfd_byte *loc;
1988 
1989       BFD_ASSERT (splt != NULL && spltrel != NULL);
1990 
1991       /* We do not actually care about the value in the PLT entry
1992 	 if we are creating a shared library and the symbol is
1993 	 still undefined, we create a dynamic relocation to fill
1994 	 in the correct value.  */
1995       if (bfd_link_pic (info) && eh->root.type == bfd_link_hash_undefined)
1996 	value = 0;
1997       else
1998 	value = (eh->root.u.def.value + eh->root.u.def.section->vma);
1999 
2000       /* Fill in the entry in the procedure linkage table.
2001 
2002 	 The format of a plt entry is
2003 	 <funcaddr> <__gp>.
2004 
2005 	 plt_offset is the offset within the PLT section at which to
2006 	 install the PLT entry.
2007 
2008 	 We are modifying the in-memory PLT contents here, so we do not add
2009 	 in the output_offset of the PLT section.  */
2010 
2011       bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset);
2012       value = _bfd_get_gp_value (info->output_bfd);
2013       bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset + 0x8);
2014 
2015       /* Create a dynamic IPLT relocation for this entry.
2016 
2017 	 We are creating a relocation in the output file's PLT section,
2018 	 which is included within the DLT secton.  So we do need to include
2019 	 the PLT's output_offset in the computation of the relocation's
2020 	 address.  */
2021       rel.r_offset = (hh->plt_offset + splt->output_offset
2022 		      + splt->output_section->vma);
2023       rel.r_info = ELF64_R_INFO (hh->eh.dynindx, R_PARISC_IPLT);
2024       rel.r_addend = 0;
2025 
2026       loc = spltrel->contents;
2027       loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2028       bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2029     }
2030 
2031   /* Initialize an external call stub entry if requested.  */
2032   if (hh->want_stub
2033       && elf64_hppa_dynamic_symbol_p (eh, info))
2034     {
2035       bfd_vma value;
2036       int insn;
2037       unsigned int max_offset;
2038 
2039       BFD_ASSERT (stub != NULL);
2040 
2041       /* Install the generic stub template.
2042 
2043 	 We are modifying the contents of the stub section, so we do not
2044 	 need to include the stub section's output_offset here.  */
2045       memcpy (stub->contents + hh->stub_offset, plt_stub, sizeof (plt_stub));
2046 
2047       /* Fix up the first ldd instruction.
2048 
2049 	 We are modifying the contents of the STUB section in memory,
2050 	 so we do not need to include its output offset in this computation.
2051 
2052 	 Note the plt_offset value is the value of the PLT entry relative to
2053 	 the start of the PLT section.  These instructions will reference
2054 	 data relative to the value of __gp, which may not necessarily have
2055 	 the same address as the start of the PLT section.
2056 
2057 	 gp_offset contains the offset of __gp within the PLT section.  */
2058       value = hh->plt_offset - hppa_info->gp_offset;
2059 
2060       insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset);
2061       if (output_bfd->arch_info->mach >= 25)
2062 	{
2063 	  /* Wide mode allows 16 bit offsets.  */
2064 	  max_offset = 32768;
2065 	  insn &= ~ 0xfff1;
2066 	  insn |= re_assemble_16 ((int) value);
2067 	}
2068       else
2069 	{
2070 	  max_offset = 8192;
2071 	  insn &= ~ 0x3ff1;
2072 	  insn |= re_assemble_14 ((int) value);
2073 	}
2074 
2075       if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2076 	{
2077 	  _bfd_error_handler
2078 	    /* xgettext:c-format */
2079 	    (_("stub entry for %s cannot load .plt, dp offset = %" PRId64),
2080 	     hh->eh.root.root.string, (int64_t) value);
2081 	  return FALSE;
2082 	}
2083 
2084       bfd_put_32 (stub->owner, (bfd_vma) insn,
2085 		  stub->contents + hh->stub_offset);
2086 
2087       /* Fix up the second ldd instruction.  */
2088       value += 8;
2089       insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset + 8);
2090       if (output_bfd->arch_info->mach >= 25)
2091 	{
2092 	  insn &= ~ 0xfff1;
2093 	  insn |= re_assemble_16 ((int) value);
2094 	}
2095       else
2096 	{
2097 	  insn &= ~ 0x3ff1;
2098 	  insn |= re_assemble_14 ((int) value);
2099 	}
2100       bfd_put_32 (stub->owner, (bfd_vma) insn,
2101 		  stub->contents + hh->stub_offset + 8);
2102     }
2103 
2104   return TRUE;
2105 }
2106 
2107 /* The .opd section contains FPTRs for each function this file
2108    exports.  Initialize the FPTR entries.  */
2109 
2110 static bfd_boolean
2111 elf64_hppa_finalize_opd (struct elf_link_hash_entry *eh, void *data)
2112 {
2113   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2114   struct bfd_link_info *info = (struct bfd_link_info *)data;
2115   struct elf64_hppa_link_hash_table *hppa_info;
2116   asection *sopd;
2117   asection *sopdrel;
2118 
2119   hppa_info = hppa_link_hash_table (info);
2120   if (hppa_info == NULL)
2121     return FALSE;
2122 
2123   sopd = hppa_info->opd_sec;
2124   sopdrel = hppa_info->opd_rel_sec;
2125 
2126   if (hh->want_opd)
2127     {
2128       bfd_vma value;
2129 
2130       /* The first two words of an .opd entry are zero.
2131 
2132 	 We are modifying the contents of the OPD section in memory, so we
2133 	 do not need to include its output offset in this computation.  */
2134       memset (sopd->contents + hh->opd_offset, 0, 16);
2135 
2136       value = (eh->root.u.def.value
2137 	       + eh->root.u.def.section->output_section->vma
2138 	       + eh->root.u.def.section->output_offset);
2139 
2140       /* The next word is the address of the function.  */
2141       bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 16);
2142 
2143       /* The last word is our local __gp value.  */
2144       value = _bfd_get_gp_value (info->output_bfd);
2145       bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 24);
2146     }
2147 
2148   /* If we are generating a shared library, we must generate EPLT relocations
2149      for each entry in the .opd, even for static functions (they may have
2150      had their address taken).  */
2151   if (bfd_link_pic (info) && hh->want_opd)
2152     {
2153       Elf_Internal_Rela rel;
2154       bfd_byte *loc;
2155       int dynindx;
2156 
2157       /* We may need to do a relocation against a local symbol, in
2158 	 which case we have to look up it's dynamic symbol index off
2159 	 the local symbol hash table.  */
2160       if (eh->dynindx != -1)
2161 	dynindx = eh->dynindx;
2162       else
2163 	dynindx
2164 	  = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2165 						hh->sym_indx);
2166 
2167       /* The offset of this relocation is the absolute address of the
2168 	 .opd entry for this symbol.  */
2169       rel.r_offset = (hh->opd_offset + sopd->output_offset
2170 		      + sopd->output_section->vma);
2171 
2172       /* If H is non-null, then we have an external symbol.
2173 
2174 	 It is imperative that we use a different dynamic symbol for the
2175 	 EPLT relocation if the symbol has global scope.
2176 
2177 	 In the dynamic symbol table, the function symbol will have a value
2178 	 which is address of the function's .opd entry.
2179 
2180 	 Thus, we can not use that dynamic symbol for the EPLT relocation
2181 	 (if we did, the data in the .opd would reference itself rather
2182 	 than the actual address of the function).  Instead we have to use
2183 	 a new dynamic symbol which has the same value as the original global
2184 	 function symbol.
2185 
2186 	 We prefix the original symbol with a "." and use the new symbol in
2187 	 the EPLT relocation.  This new symbol has already been recorded in
2188 	 the symbol table, we just have to look it up and use it.
2189 
2190 	 We do not have such problems with static functions because we do
2191 	 not make their addresses in the dynamic symbol table point to
2192 	 the .opd entry.  Ultimately this should be safe since a static
2193 	 function can not be directly referenced outside of its shared
2194 	 library.
2195 
2196 	 We do have to play similar games for FPTR relocations in shared
2197 	 libraries, including those for static symbols.  See the FPTR
2198 	 handling in elf64_hppa_finalize_dynreloc.  */
2199       if (eh)
2200 	{
2201 	  char *new_name;
2202 	  struct elf_link_hash_entry *nh;
2203 
2204 	  new_name = concat (".", eh->root.root.string, NULL);
2205 
2206 	  nh = elf_link_hash_lookup (elf_hash_table (info),
2207 				     new_name, TRUE, TRUE, FALSE);
2208 
2209 	  /* All we really want from the new symbol is its dynamic
2210 	     symbol index.  */
2211 	  if (nh)
2212 	    dynindx = nh->dynindx;
2213 	  free (new_name);
2214 	}
2215 
2216       rel.r_addend = 0;
2217       rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2218 
2219       loc = sopdrel->contents;
2220       loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2221       bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2222     }
2223   return TRUE;
2224 }
2225 
2226 /* The .dlt section contains addresses for items referenced through the
2227    dlt.  Note that we can have a DLTIND relocation for a local symbol, thus
2228    we can not depend on finish_dynamic_symbol to initialize the .dlt.  */
2229 
2230 static bfd_boolean
2231 elf64_hppa_finalize_dlt (struct elf_link_hash_entry *eh, void *data)
2232 {
2233   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2234   struct bfd_link_info *info = (struct bfd_link_info *)data;
2235   struct elf64_hppa_link_hash_table *hppa_info;
2236   asection *sdlt, *sdltrel;
2237 
2238   hppa_info = hppa_link_hash_table (info);
2239   if (hppa_info == NULL)
2240     return FALSE;
2241 
2242   sdlt = hppa_info->dlt_sec;
2243   sdltrel = hppa_info->dlt_rel_sec;
2244 
2245   /* H/DYN_H may refer to a local variable and we know it's
2246      address, so there is no need to create a relocation.  Just install
2247      the proper value into the DLT, note this shortcut can not be
2248      skipped when building a shared library.  */
2249   if (! bfd_link_pic (info) && hh && hh->want_dlt)
2250     {
2251       bfd_vma value;
2252 
2253       /* If we had an LTOFF_FPTR style relocation we want the DLT entry
2254 	 to point to the FPTR entry in the .opd section.
2255 
2256 	 We include the OPD's output offset in this computation as
2257 	 we are referring to an absolute address in the resulting
2258 	 object file.  */
2259       if (hh->want_opd)
2260 	{
2261 	  value = (hh->opd_offset
2262 		   + hppa_info->opd_sec->output_offset
2263 		   + hppa_info->opd_sec->output_section->vma);
2264 	}
2265       else if ((eh->root.type == bfd_link_hash_defined
2266 		|| eh->root.type == bfd_link_hash_defweak)
2267 	       && eh->root.u.def.section)
2268 	{
2269 	  value = eh->root.u.def.value + eh->root.u.def.section->output_offset;
2270 	  if (eh->root.u.def.section->output_section)
2271 	    value += eh->root.u.def.section->output_section->vma;
2272 	  else
2273 	    value += eh->root.u.def.section->vma;
2274 	}
2275       else
2276 	/* We have an undefined function reference.  */
2277 	value = 0;
2278 
2279       /* We do not need to include the output offset of the DLT section
2280 	 here because we are modifying the in-memory contents.  */
2281       bfd_put_64 (sdlt->owner, value, sdlt->contents + hh->dlt_offset);
2282     }
2283 
2284   /* Create a relocation for the DLT entry associated with this symbol.
2285      When building a shared library the symbol does not have to be dynamic.  */
2286   if (hh->want_dlt
2287       && (elf64_hppa_dynamic_symbol_p (eh, info) || bfd_link_pic (info)))
2288     {
2289       Elf_Internal_Rela rel;
2290       bfd_byte *loc;
2291       int dynindx;
2292 
2293       /* We may need to do a relocation against a local symbol, in
2294 	 which case we have to look up it's dynamic symbol index off
2295 	 the local symbol hash table.  */
2296       if (eh && eh->dynindx != -1)
2297 	dynindx = eh->dynindx;
2298       else
2299 	dynindx
2300 	  = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2301 						hh->sym_indx);
2302 
2303       /* Create a dynamic relocation for this entry.  Do include the output
2304 	 offset of the DLT entry since we need an absolute address in the
2305 	 resulting object file.  */
2306       rel.r_offset = (hh->dlt_offset + sdlt->output_offset
2307 		      + sdlt->output_section->vma);
2308       if (eh && eh->type == STT_FUNC)
2309 	  rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2310       else
2311 	  rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2312       rel.r_addend = 0;
2313 
2314       loc = sdltrel->contents;
2315       loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2316       bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2317     }
2318   return TRUE;
2319 }
2320 
2321 /* Finalize the dynamic relocations.  Specifically the FPTR relocations
2322    for dynamic functions used to initialize static data.  */
2323 
2324 static bfd_boolean
2325 elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
2326 			      void *data)
2327 {
2328   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2329   struct bfd_link_info *info = (struct bfd_link_info *)data;
2330   struct elf64_hppa_link_hash_table *hppa_info;
2331   int dynamic_symbol;
2332 
2333   dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, info);
2334 
2335   if (!dynamic_symbol && !bfd_link_pic (info))
2336     return TRUE;
2337 
2338   if (hh->reloc_entries)
2339     {
2340       struct elf64_hppa_dyn_reloc_entry *rent;
2341       int dynindx;
2342 
2343       hppa_info = hppa_link_hash_table (info);
2344       if (hppa_info == NULL)
2345 	return FALSE;
2346 
2347       /* We may need to do a relocation against a local symbol, in
2348 	 which case we have to look up it's dynamic symbol index off
2349 	 the local symbol hash table.  */
2350       if (eh->dynindx != -1)
2351 	dynindx = eh->dynindx;
2352       else
2353 	dynindx
2354 	  = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2355 						hh->sym_indx);
2356 
2357       for (rent = hh->reloc_entries; rent; rent = rent->next)
2358 	{
2359 	  Elf_Internal_Rela rel;
2360 	  bfd_byte *loc;
2361 
2362 	  /* Allocate one iff we are building a shared library, the relocation
2363 	     isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
2364 	  if (!bfd_link_pic (info)
2365 	      && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2366 	    continue;
2367 
2368 	  /* Create a dynamic relocation for this entry.
2369 
2370 	     We need the output offset for the reloc's section because
2371 	     we are creating an absolute address in the resulting object
2372 	     file.  */
2373 	  rel.r_offset = (rent->offset + rent->sec->output_offset
2374 			  + rent->sec->output_section->vma);
2375 
2376 	  /* An FPTR64 relocation implies that we took the address of
2377 	     a function and that the function has an entry in the .opd
2378 	     section.  We want the FPTR64 relocation to reference the
2379 	     entry in .opd.
2380 
2381 	     We could munge the symbol value in the dynamic symbol table
2382 	     (in fact we already do for functions with global scope) to point
2383 	     to the .opd entry.  Then we could use that dynamic symbol in
2384 	     this relocation.
2385 
2386 	     Or we could do something sensible, not munge the symbol's
2387 	     address and instead just use a different symbol to reference
2388 	     the .opd entry.  At least that seems sensible until you
2389 	     realize there's no local dynamic symbols we can use for that
2390 	     purpose.  Thus the hair in the check_relocs routine.
2391 
2392 	     We use a section symbol recorded by check_relocs as the
2393 	     base symbol for the relocation.  The addend is the difference
2394 	     between the section symbol and the address of the .opd entry.  */
2395 	  if (bfd_link_pic (info)
2396 	      && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2397 	    {
2398 	      bfd_vma value, value2;
2399 
2400 	      /* First compute the address of the opd entry for this symbol.  */
2401 	      value = (hh->opd_offset
2402 		       + hppa_info->opd_sec->output_section->vma
2403 		       + hppa_info->opd_sec->output_offset);
2404 
2405 	      /* Compute the value of the start of the section with
2406 		 the relocation.  */
2407 	      value2 = (rent->sec->output_section->vma
2408 			+ rent->sec->output_offset);
2409 
2410 	      /* Compute the difference between the start of the section
2411 		 with the relocation and the opd entry.  */
2412 	      value -= value2;
2413 
2414 	      /* The result becomes the addend of the relocation.  */
2415 	      rel.r_addend = value;
2416 
2417 	      /* The section symbol becomes the symbol for the dynamic
2418 		 relocation.  */
2419 	      dynindx
2420 		= _bfd_elf_link_lookup_local_dynindx (info,
2421 						      rent->sec->owner,
2422 						      rent->sec_symndx);
2423 	    }
2424 	  else
2425 	    rel.r_addend = rent->addend;
2426 
2427 	  rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2428 
2429 	  loc = hppa_info->other_rel_sec->contents;
2430 	  loc += (hppa_info->other_rel_sec->reloc_count++
2431 		  * sizeof (Elf64_External_Rela));
2432 	  bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2433 	}
2434     }
2435 
2436   return TRUE;
2437 }
2438 
2439 /* Used to decide how to sort relocs in an optimal manner for the
2440    dynamic linker, before writing them out.  */
2441 
2442 static enum elf_reloc_type_class
2443 elf64_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2444 			     const asection *rel_sec ATTRIBUTE_UNUSED,
2445 			     const Elf_Internal_Rela *rela)
2446 {
2447   if (ELF64_R_SYM (rela->r_info) == STN_UNDEF)
2448     return reloc_class_relative;
2449 
2450   switch ((int) ELF64_R_TYPE (rela->r_info))
2451     {
2452     case R_PARISC_IPLT:
2453       return reloc_class_plt;
2454     case R_PARISC_COPY:
2455       return reloc_class_copy;
2456     default:
2457       return reloc_class_normal;
2458     }
2459 }
2460 
2461 /* Finish up the dynamic sections.  */
2462 
2463 static bfd_boolean
2464 elf64_hppa_finish_dynamic_sections (bfd *output_bfd,
2465 				    struct bfd_link_info *info)
2466 {
2467   bfd *dynobj;
2468   asection *sdyn;
2469   struct elf64_hppa_link_hash_table *hppa_info;
2470 
2471   hppa_info = hppa_link_hash_table (info);
2472   if (hppa_info == NULL)
2473     return FALSE;
2474 
2475   /* Finalize the contents of the .opd section.  */
2476   elf_link_hash_traverse (elf_hash_table (info),
2477 			  elf64_hppa_finalize_opd,
2478 			  info);
2479 
2480   elf_link_hash_traverse (elf_hash_table (info),
2481 			  elf64_hppa_finalize_dynreloc,
2482 			  info);
2483 
2484   /* Finalize the contents of the .dlt section.  */
2485   dynobj = elf_hash_table (info)->dynobj;
2486   /* Finalize the contents of the .dlt section.  */
2487   elf_link_hash_traverse (elf_hash_table (info),
2488 			  elf64_hppa_finalize_dlt,
2489 			  info);
2490 
2491   sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2492 
2493   if (elf_hash_table (info)->dynamic_sections_created)
2494     {
2495       Elf64_External_Dyn *dyncon, *dynconend;
2496 
2497       BFD_ASSERT (sdyn != NULL);
2498 
2499       dyncon = (Elf64_External_Dyn *) sdyn->contents;
2500       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2501       for (; dyncon < dynconend; dyncon++)
2502 	{
2503 	  Elf_Internal_Dyn dyn;
2504 	  asection *s;
2505 
2506 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2507 
2508 	  switch (dyn.d_tag)
2509 	    {
2510 	    default:
2511 	      break;
2512 
2513 	    case DT_HP_LOAD_MAP:
2514 	      /* Compute the absolute address of 16byte scratchpad area
2515 		 for the dynamic linker.
2516 
2517 		 By convention the linker script will allocate the scratchpad
2518 		 area at the start of the .data section.  So all we have to
2519 		 to is find the start of the .data section.  */
2520 	      s = bfd_get_section_by_name (output_bfd, ".data");
2521 	      if (!s)
2522 		return FALSE;
2523 	      dyn.d_un.d_ptr = s->vma;
2524 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2525 	      break;
2526 
2527 	    case DT_PLTGOT:
2528 	      /* HP's use PLTGOT to set the GOT register.  */
2529 	      dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2530 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2531 	      break;
2532 
2533 	    case DT_JMPREL:
2534 	      s = hppa_info->plt_rel_sec;
2535 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2536 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2537 	      break;
2538 
2539 	    case DT_PLTRELSZ:
2540 	      s = hppa_info->plt_rel_sec;
2541 	      dyn.d_un.d_val = s->size;
2542 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2543 	      break;
2544 
2545 	    case DT_RELA:
2546 	      s = hppa_info->other_rel_sec;
2547 	      if (! s || ! s->size)
2548 		s = hppa_info->dlt_rel_sec;
2549 	      if (! s || ! s->size)
2550 		s = hppa_info->opd_rel_sec;
2551 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2552 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2553 	      break;
2554 
2555 	    case DT_RELASZ:
2556 	      s = hppa_info->other_rel_sec;
2557 	      dyn.d_un.d_val = s->size;
2558 	      s = hppa_info->dlt_rel_sec;
2559 	      dyn.d_un.d_val += s->size;
2560 	      s = hppa_info->opd_rel_sec;
2561 	      dyn.d_un.d_val += s->size;
2562 	      /* There is some question about whether or not the size of
2563 		 the PLT relocs should be included here.  HP's tools do
2564 		 it, so we'll emulate them.  */
2565 	      s = hppa_info->plt_rel_sec;
2566 	      dyn.d_un.d_val += s->size;
2567 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2568 	      break;
2569 
2570 	    }
2571 	}
2572     }
2573 
2574   return TRUE;
2575 }
2576 
2577 /* Support for core dump NOTE sections.  */
2578 
2579 static bfd_boolean
2580 elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2581 {
2582   int offset;
2583   size_t size;
2584 
2585   switch (note->descsz)
2586     {
2587       default:
2588 	return FALSE;
2589 
2590       case 760:		/* Linux/hppa */
2591 	/* pr_cursig */
2592 	elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2593 
2594 	/* pr_pid */
2595 	elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2596 
2597 	/* pr_reg */
2598 	offset = 112;
2599 	size = 640;
2600 
2601 	break;
2602     }
2603 
2604   /* Make a ".reg/999" section.  */
2605   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2606 					  size, note->descpos + offset);
2607 }
2608 
2609 static bfd_boolean
2610 elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2611 {
2612   char * command;
2613   int n;
2614 
2615   switch (note->descsz)
2616     {
2617     default:
2618       return FALSE;
2619 
2620     case 136:		/* Linux/hppa elf_prpsinfo.  */
2621       elf_tdata (abfd)->core->program
2622 	= _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2623       elf_tdata (abfd)->core->command
2624 	= _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2625     }
2626 
2627   /* Note that for some reason, a spurious space is tacked
2628      onto the end of the args in some (at least one anyway)
2629      implementations, so strip it off if it exists.  */
2630   command = elf_tdata (abfd)->core->command;
2631   n = strlen (command);
2632 
2633   if (0 < n && command[n - 1] == ' ')
2634     command[n - 1] = '\0';
2635 
2636   return TRUE;
2637 }
2638 
2639 /* Return the number of additional phdrs we will need.
2640 
2641    The generic ELF code only creates PT_PHDRs for executables.  The HP
2642    dynamic linker requires PT_PHDRs for dynamic libraries too.
2643 
2644    This routine indicates that the backend needs one additional program
2645    header for that case.
2646 
2647    Note we do not have access to the link info structure here, so we have
2648    to guess whether or not we are building a shared library based on the
2649    existence of a .interp section.  */
2650 
2651 static int
2652 elf64_hppa_additional_program_headers (bfd *abfd,
2653 				struct bfd_link_info *info ATTRIBUTE_UNUSED)
2654 {
2655   asection *s;
2656 
2657   /* If we are creating a shared library, then we have to create a
2658      PT_PHDR segment.  HP's dynamic linker chokes without it.  */
2659   s = bfd_get_section_by_name (abfd, ".interp");
2660   if (! s)
2661     return 1;
2662   return 0;
2663 }
2664 
2665 static bfd_boolean
2666 elf64_hppa_allow_non_load_phdr (bfd *abfd ATTRIBUTE_UNUSED,
2667 				const Elf_Internal_Phdr *phdr ATTRIBUTE_UNUSED,
2668 				unsigned int count ATTRIBUTE_UNUSED)
2669 {
2670   return TRUE;
2671 }
2672 
2673 /* Allocate and initialize any program headers required by this
2674    specific backend.
2675 
2676    The generic ELF code only creates PT_PHDRs for executables.  The HP
2677    dynamic linker requires PT_PHDRs for dynamic libraries too.
2678 
2679    This allocates the PT_PHDR and initializes it in a manner suitable
2680    for the HP linker.
2681 
2682    Note we do not have access to the link info structure here, so we have
2683    to guess whether or not we are building a shared library based on the
2684    existence of a .interp section.  */
2685 
2686 static bfd_boolean
2687 elf64_hppa_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
2688 {
2689   struct elf_segment_map *m;
2690 
2691   m = elf_seg_map (abfd);
2692   if (info != NULL && !info->user_phdrs && m != NULL && m->p_type != PT_PHDR)
2693     {
2694       m = ((struct elf_segment_map *)
2695 	   bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
2696       if (m == NULL)
2697 	return FALSE;
2698 
2699       m->p_type = PT_PHDR;
2700       m->p_flags = PF_R | PF_X;
2701       m->p_flags_valid = 1;
2702       m->p_paddr_valid = 1;
2703       m->includes_phdrs = 1;
2704 
2705       m->next = elf_seg_map (abfd);
2706       elf_seg_map (abfd) = m;
2707     }
2708 
2709   for (m = elf_seg_map (abfd) ; m != NULL; m = m->next)
2710     if (m->p_type == PT_LOAD)
2711       {
2712 	unsigned int i;
2713 
2714 	for (i = 0; i < m->count; i++)
2715 	  {
2716 	    /* The code "hint" is not really a hint.  It is a requirement
2717 	       for certain versions of the HP dynamic linker.  Worse yet,
2718 	       it must be set even if the shared library does not have
2719 	       any code in its "text" segment (thus the check for .hash
2720 	       to catch this situation).  */
2721 	    if (m->sections[i]->flags & SEC_CODE
2722 		|| (strcmp (m->sections[i]->name, ".hash") == 0))
2723 	      m->p_flags |= (PF_X | PF_HP_CODE);
2724 	  }
2725       }
2726 
2727   return TRUE;
2728 }
2729 
2730 /* Called when writing out an object file to decide the type of a
2731    symbol.  */
2732 static int
2733 elf64_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym,
2734 				int type)
2735 {
2736   if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2737     return STT_PARISC_MILLI;
2738   else
2739     return type;
2740 }
2741 
2742 /* Support HP specific sections for core files.  */
2743 
2744 static bfd_boolean
2745 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int sec_index,
2746 			      const char *typename)
2747 {
2748   if (hdr->p_type == PT_HP_CORE_KERNEL)
2749     {
2750       asection *sect;
2751 
2752       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2753 	return FALSE;
2754 
2755       sect = bfd_make_section_anyway (abfd, ".kernel");
2756       if (sect == NULL)
2757 	return FALSE;
2758       sect->size = hdr->p_filesz;
2759       sect->filepos = hdr->p_offset;
2760       sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2761       return TRUE;
2762     }
2763 
2764   if (hdr->p_type == PT_HP_CORE_PROC)
2765     {
2766       int sig;
2767 
2768       if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2769 	return FALSE;
2770       if (bfd_bread (&sig, 4, abfd) != 4)
2771 	return FALSE;
2772 
2773       elf_tdata (abfd)->core->signal = sig;
2774 
2775       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2776 	return FALSE;
2777 
2778       /* GDB uses the ".reg" section to read register contents.  */
2779       return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2780 					      hdr->p_offset);
2781     }
2782 
2783   if (hdr->p_type == PT_HP_CORE_LOADABLE
2784       || hdr->p_type == PT_HP_CORE_STACK
2785       || hdr->p_type == PT_HP_CORE_MMF)
2786     hdr->p_type = PT_LOAD;
2787 
2788   return _bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename);
2789 }
2790 
2791 /* Hook called by the linker routine which adds symbols from an object
2792    file.  HP's libraries define symbols with HP specific section
2793    indices, which we have to handle.  */
2794 
2795 static bfd_boolean
2796 elf_hppa_add_symbol_hook (bfd *abfd,
2797 			  struct bfd_link_info *info ATTRIBUTE_UNUSED,
2798 			  Elf_Internal_Sym *sym,
2799 			  const char **namep ATTRIBUTE_UNUSED,
2800 			  flagword *flagsp ATTRIBUTE_UNUSED,
2801 			  asection **secp,
2802 			  bfd_vma *valp)
2803 {
2804   unsigned int sec_index = sym->st_shndx;
2805 
2806   switch (sec_index)
2807     {
2808     case SHN_PARISC_ANSI_COMMON:
2809       *secp = bfd_make_section_old_way (abfd, ".PARISC.ansi.common");
2810       (*secp)->flags |= SEC_IS_COMMON;
2811       *valp = sym->st_size;
2812       break;
2813 
2814     case SHN_PARISC_HUGE_COMMON:
2815       *secp = bfd_make_section_old_way (abfd, ".PARISC.huge.common");
2816       (*secp)->flags |= SEC_IS_COMMON;
2817       *valp = sym->st_size;
2818       break;
2819     }
2820 
2821   return TRUE;
2822 }
2823 
2824 static bfd_boolean
2825 elf_hppa_unmark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2826 					 void *data)
2827 {
2828   struct bfd_link_info *info = data;
2829 
2830   /* If we are not creating a shared library, and this symbol is
2831      referenced by a shared library but is not defined anywhere, then
2832      the generic code will warn that it is undefined.
2833 
2834      This behavior is undesirable on HPs since the standard shared
2835      libraries contain references to undefined symbols.
2836 
2837      So we twiddle the flags associated with such symbols so that they
2838      will not trigger the warning.  ?!? FIXME.  This is horribly fragile.
2839 
2840      Ultimately we should have better controls over the generic ELF BFD
2841      linker code.  */
2842   if (! bfd_link_relocatable (info)
2843       && info->unresolved_syms_in_shared_libs != RM_IGNORE
2844       && h->root.type == bfd_link_hash_undefined
2845       && h->ref_dynamic
2846       && !h->ref_regular)
2847     {
2848       h->ref_dynamic = 0;
2849       h->pointer_equality_needed = 1;
2850     }
2851 
2852   return TRUE;
2853 }
2854 
2855 static bfd_boolean
2856 elf_hppa_remark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2857 					 void *data)
2858 {
2859   struct bfd_link_info *info = data;
2860 
2861   /* If we are not creating a shared library, and this symbol is
2862      referenced by a shared library but is not defined anywhere, then
2863      the generic code will warn that it is undefined.
2864 
2865      This behavior is undesirable on HPs since the standard shared
2866      libraries contain references to undefined symbols.
2867 
2868      So we twiddle the flags associated with such symbols so that they
2869      will not trigger the warning.  ?!? FIXME.  This is horribly fragile.
2870 
2871      Ultimately we should have better controls over the generic ELF BFD
2872      linker code.  */
2873   if (! bfd_link_relocatable (info)
2874       && info->unresolved_syms_in_shared_libs != RM_IGNORE
2875       && h->root.type == bfd_link_hash_undefined
2876       && !h->ref_dynamic
2877       && !h->ref_regular
2878       && h->pointer_equality_needed)
2879     {
2880       h->ref_dynamic = 1;
2881       h->pointer_equality_needed = 0;
2882     }
2883 
2884   return TRUE;
2885 }
2886 
2887 static bfd_boolean
2888 elf_hppa_is_dynamic_loader_symbol (const char *name)
2889 {
2890   return (! strcmp (name, "__CPU_REVISION")
2891 	  || ! strcmp (name, "__CPU_KEYBITS_1")
2892 	  || ! strcmp (name, "__SYSTEM_ID_D")
2893 	  || ! strcmp (name, "__FPU_MODEL")
2894 	  || ! strcmp (name, "__FPU_REVISION")
2895 	  || ! strcmp (name, "__ARGC")
2896 	  || ! strcmp (name, "__ARGV")
2897 	  || ! strcmp (name, "__ENVP")
2898 	  || ! strcmp (name, "__TLS_SIZE_D")
2899 	  || ! strcmp (name, "__LOAD_INFO")
2900 	  || ! strcmp (name, "__systab"));
2901 }
2902 
2903 /* Record the lowest address for the data and text segments.  */
2904 static void
2905 elf_hppa_record_segment_addrs (bfd *abfd,
2906 			       asection *section,
2907 			       void *data)
2908 {
2909   struct elf64_hppa_link_hash_table *hppa_info = data;
2910 
2911   if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2912     {
2913       bfd_vma value;
2914       Elf_Internal_Phdr *p;
2915 
2916       p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
2917       BFD_ASSERT (p != NULL);
2918       value = p->p_vaddr;
2919 
2920       if (section->flags & SEC_READONLY)
2921 	{
2922 	  if (value < hppa_info->text_segment_base)
2923 	    hppa_info->text_segment_base = value;
2924 	}
2925       else
2926 	{
2927 	  if (value < hppa_info->data_segment_base)
2928 	    hppa_info->data_segment_base = value;
2929 	}
2930     }
2931 }
2932 
2933 /* Called after we have seen all the input files/sections, but before
2934    final symbol resolution and section placement has been determined.
2935 
2936    We use this hook to (possibly) provide a value for __gp, then we
2937    fall back to the generic ELF final link routine.  */
2938 
2939 static bfd_boolean
2940 elf_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
2941 {
2942   struct stat buf;
2943   struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
2944 
2945   if (hppa_info == NULL)
2946     return FALSE;
2947 
2948   if (! bfd_link_relocatable (info))
2949     {
2950       struct elf_link_hash_entry *gp;
2951       bfd_vma gp_val;
2952 
2953       /* The linker script defines a value for __gp iff it was referenced
2954 	 by one of the objects being linked.  First try to find the symbol
2955 	 in the hash table.  If that fails, just compute the value __gp
2956 	 should have had.  */
2957       gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
2958 				 FALSE, FALSE);
2959 
2960       if (gp)
2961 	{
2962 
2963 	  /* Adjust the value of __gp as we may want to slide it into the
2964 	     .plt section so that the stubs can access PLT entries without
2965 	     using an addil sequence.  */
2966 	  gp->root.u.def.value += hppa_info->gp_offset;
2967 
2968 	  gp_val = (gp->root.u.def.section->output_section->vma
2969 		    + gp->root.u.def.section->output_offset
2970 		    + gp->root.u.def.value);
2971 	}
2972       else
2973 	{
2974 	  asection *sec;
2975 
2976 	  /* First look for a .plt section.  If found, then __gp is the
2977 	     address of the .plt + gp_offset.
2978 
2979 	     If no .plt is found, then look for .dlt, .opd and .data (in
2980 	     that order) and set __gp to the base address of whichever
2981 	     section is found first.  */
2982 
2983 	  sec = hppa_info->plt_sec;
2984 	  if (sec && ! (sec->flags & SEC_EXCLUDE))
2985 	    gp_val = (sec->output_offset
2986 		      + sec->output_section->vma
2987 		      + hppa_info->gp_offset);
2988 	  else
2989 	    {
2990 	      sec = hppa_info->dlt_sec;
2991 	      if (!sec || (sec->flags & SEC_EXCLUDE))
2992 		sec = hppa_info->opd_sec;
2993 	      if (!sec || (sec->flags & SEC_EXCLUDE))
2994 		sec = bfd_get_section_by_name (abfd, ".data");
2995 	      if (!sec || (sec->flags & SEC_EXCLUDE))
2996 		gp_val = 0;
2997 	      else
2998 		gp_val = sec->output_offset + sec->output_section->vma;
2999 	    }
3000 	}
3001 
3002       /* Install whatever value we found/computed for __gp.  */
3003       _bfd_set_gp_value (abfd, gp_val);
3004     }
3005 
3006   /* We need to know the base of the text and data segments so that we
3007      can perform SEGREL relocations.  We will record the base addresses
3008      when we encounter the first SEGREL relocation.  */
3009   hppa_info->text_segment_base = (bfd_vma)-1;
3010   hppa_info->data_segment_base = (bfd_vma)-1;
3011 
3012   /* HP's shared libraries have references to symbols that are not
3013      defined anywhere.  The generic ELF BFD linker code will complain
3014      about such symbols.
3015 
3016      So we detect the losing case and arrange for the flags on the symbol
3017      to indicate that it was never referenced.  This keeps the generic
3018      ELF BFD link code happy and appears to not create any secondary
3019      problems.  Ultimately we need a way to control the behavior of the
3020      generic ELF BFD link code better.  */
3021   elf_link_hash_traverse (elf_hash_table (info),
3022 			  elf_hppa_unmark_useless_dynamic_symbols,
3023 			  info);
3024 
3025   /* Invoke the regular ELF backend linker to do all the work.  */
3026   if (!bfd_elf_final_link (abfd, info))
3027     return FALSE;
3028 
3029   elf_link_hash_traverse (elf_hash_table (info),
3030 			  elf_hppa_remark_useless_dynamic_symbols,
3031 			  info);
3032 
3033   /* If we're producing a final executable, sort the contents of the
3034      unwind section. */
3035   if (bfd_link_relocatable (info))
3036     return TRUE;
3037 
3038   /* Do not attempt to sort non-regular files.  This is here
3039      especially for configure scripts and kernel builds which run
3040      tests with "ld [...] -o /dev/null".  */
3041   if (stat (abfd->filename, &buf) != 0
3042       || !S_ISREG(buf.st_mode))
3043     return TRUE;
3044 
3045   return elf_hppa_sort_unwind (abfd);
3046 }
3047 
3048 /* Relocate the given INSN.  VALUE should be the actual value we want
3049    to insert into the instruction, ie by this point we should not be
3050    concerned with computing an offset relative to the DLT, PC, etc.
3051    Instead this routine is meant to handle the bit manipulations needed
3052    to insert the relocation into the given instruction.  */
3053 
3054 static int
3055 elf_hppa_relocate_insn (int insn, int sym_value, unsigned int r_type)
3056 {
3057   switch (r_type)
3058     {
3059     /* This is any 22 bit branch.  In PA2.0 syntax it corresponds to
3060        the "B" instruction.  */
3061     case R_PARISC_PCREL22F:
3062     case R_PARISC_PCREL22C:
3063       return (insn & ~0x3ff1ffd) | re_assemble_22 (sym_value);
3064 
3065       /* This is any 12 bit branch.  */
3066     case R_PARISC_PCREL12F:
3067       return (insn & ~0x1ffd) | re_assemble_12 (sym_value);
3068 
3069     /* This is any 17 bit branch.  In PA2.0 syntax it also corresponds
3070        to the "B" instruction as well as BE.  */
3071     case R_PARISC_PCREL17F:
3072     case R_PARISC_DIR17F:
3073     case R_PARISC_DIR17R:
3074     case R_PARISC_PCREL17C:
3075     case R_PARISC_PCREL17R:
3076       return (insn & ~0x1f1ffd) | re_assemble_17 (sym_value);
3077 
3078     /* ADDIL or LDIL instructions.  */
3079     case R_PARISC_DLTREL21L:
3080     case R_PARISC_DLTIND21L:
3081     case R_PARISC_LTOFF_FPTR21L:
3082     case R_PARISC_PCREL21L:
3083     case R_PARISC_LTOFF_TP21L:
3084     case R_PARISC_DPREL21L:
3085     case R_PARISC_PLTOFF21L:
3086     case R_PARISC_DIR21L:
3087       return (insn & ~0x1fffff) | re_assemble_21 (sym_value);
3088 
3089     /* LDO and integer loads/stores with 14 bit displacements.  */
3090     case R_PARISC_DLTREL14R:
3091     case R_PARISC_DLTREL14F:
3092     case R_PARISC_DLTIND14R:
3093     case R_PARISC_DLTIND14F:
3094     case R_PARISC_LTOFF_FPTR14R:
3095     case R_PARISC_PCREL14R:
3096     case R_PARISC_PCREL14F:
3097     case R_PARISC_LTOFF_TP14R:
3098     case R_PARISC_LTOFF_TP14F:
3099     case R_PARISC_DPREL14R:
3100     case R_PARISC_DPREL14F:
3101     case R_PARISC_PLTOFF14R:
3102     case R_PARISC_PLTOFF14F:
3103     case R_PARISC_DIR14R:
3104     case R_PARISC_DIR14F:
3105       return (insn & ~0x3fff) | low_sign_unext (sym_value, 14);
3106 
3107     /* PA2.0W LDO and integer loads/stores with 16 bit displacements.  */
3108     case R_PARISC_LTOFF_FPTR16F:
3109     case R_PARISC_PCREL16F:
3110     case R_PARISC_LTOFF_TP16F:
3111     case R_PARISC_GPREL16F:
3112     case R_PARISC_PLTOFF16F:
3113     case R_PARISC_DIR16F:
3114     case R_PARISC_LTOFF16F:
3115       return (insn & ~0xffff) | re_assemble_16 (sym_value);
3116 
3117     /* Doubleword loads and stores with a 14 bit displacement.  */
3118     case R_PARISC_DLTREL14DR:
3119     case R_PARISC_DLTIND14DR:
3120     case R_PARISC_LTOFF_FPTR14DR:
3121     case R_PARISC_LTOFF_FPTR16DF:
3122     case R_PARISC_PCREL14DR:
3123     case R_PARISC_PCREL16DF:
3124     case R_PARISC_LTOFF_TP14DR:
3125     case R_PARISC_LTOFF_TP16DF:
3126     case R_PARISC_DPREL14DR:
3127     case R_PARISC_GPREL16DF:
3128     case R_PARISC_PLTOFF14DR:
3129     case R_PARISC_PLTOFF16DF:
3130     case R_PARISC_DIR14DR:
3131     case R_PARISC_DIR16DF:
3132     case R_PARISC_LTOFF16DF:
3133       return (insn & ~0x3ff1) | (((sym_value & 0x2000) >> 13)
3134 				 | ((sym_value & 0x1ff8) << 1));
3135 
3136     /* Floating point single word load/store instructions.  */
3137     case R_PARISC_DLTREL14WR:
3138     case R_PARISC_DLTIND14WR:
3139     case R_PARISC_LTOFF_FPTR14WR:
3140     case R_PARISC_LTOFF_FPTR16WF:
3141     case R_PARISC_PCREL14WR:
3142     case R_PARISC_PCREL16WF:
3143     case R_PARISC_LTOFF_TP14WR:
3144     case R_PARISC_LTOFF_TP16WF:
3145     case R_PARISC_DPREL14WR:
3146     case R_PARISC_GPREL16WF:
3147     case R_PARISC_PLTOFF14WR:
3148     case R_PARISC_PLTOFF16WF:
3149     case R_PARISC_DIR16WF:
3150     case R_PARISC_DIR14WR:
3151     case R_PARISC_LTOFF16WF:
3152       return (insn & ~0x3ff9) | (((sym_value & 0x2000) >> 13)
3153 				 | ((sym_value & 0x1ffc) << 1));
3154 
3155     default:
3156       return insn;
3157     }
3158 }
3159 
3160 /* Compute the value for a relocation (REL) during a final link stage,
3161    then insert the value into the proper location in CONTENTS.
3162 
3163    VALUE is a tentative value for the relocation and may be overridden
3164    and modified here based on the specific relocation to be performed.
3165 
3166    For example we do conversions for PC-relative branches in this routine
3167    or redirection of calls to external routines to stubs.
3168 
3169    The work of actually applying the relocation is left to a helper
3170    routine in an attempt to reduce the complexity and size of this
3171    function.  */
3172 
3173 static bfd_reloc_status_type
3174 elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
3175 			      bfd *input_bfd,
3176 			      bfd *output_bfd,
3177 			      asection *input_section,
3178 			      bfd_byte *contents,
3179 			      bfd_vma value,
3180 			      struct bfd_link_info *info,
3181 			      asection *sym_sec,
3182 			      struct elf_link_hash_entry *eh)
3183 {
3184   struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
3185   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
3186   bfd_vma *local_offsets;
3187   Elf_Internal_Shdr *symtab_hdr;
3188   int insn;
3189   bfd_vma max_branch_offset = 0;
3190   bfd_vma offset = rel->r_offset;
3191   bfd_signed_vma addend = rel->r_addend;
3192   reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3193   unsigned int r_symndx = ELF_R_SYM (rel->r_info);
3194   unsigned int r_type = howto->type;
3195   bfd_byte *hit_data = contents + offset;
3196 
3197   if (hppa_info == NULL)
3198     return bfd_reloc_notsupported;
3199 
3200   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3201   local_offsets = elf_local_got_offsets (input_bfd);
3202   insn = bfd_get_32 (input_bfd, hit_data);
3203 
3204   switch (r_type)
3205     {
3206     case R_PARISC_NONE:
3207       break;
3208 
3209     /* Basic function call support.
3210 
3211        Note for a call to a function defined in another dynamic library
3212        we want to redirect the call to a stub.  */
3213 
3214     /* PC relative relocs without an implicit offset.  */
3215     case R_PARISC_PCREL21L:
3216     case R_PARISC_PCREL14R:
3217     case R_PARISC_PCREL14F:
3218     case R_PARISC_PCREL14WR:
3219     case R_PARISC_PCREL14DR:
3220     case R_PARISC_PCREL16F:
3221     case R_PARISC_PCREL16WF:
3222     case R_PARISC_PCREL16DF:
3223       {
3224 	/* If this is a call to a function defined in another dynamic
3225 	   library, then redirect the call to the local stub for this
3226 	   function.  */
3227 	if (sym_sec == NULL || sym_sec->output_section == NULL)
3228 	  value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3229 		   + hppa_info->stub_sec->output_section->vma);
3230 
3231 	/* Turn VALUE into a proper PC relative address.  */
3232 	value -= (offset + input_section->output_offset
3233 		  + input_section->output_section->vma);
3234 
3235 	/* Adjust for any field selectors.  */
3236 	if (r_type == R_PARISC_PCREL21L)
3237 	  value = hppa_field_adjust (value, -8 + addend, e_lsel);
3238 	else if (r_type == R_PARISC_PCREL14F
3239 		 || r_type == R_PARISC_PCREL16F
3240 		 || r_type == R_PARISC_PCREL16WF
3241 		 || r_type == R_PARISC_PCREL16DF)
3242 	  value = hppa_field_adjust (value, -8 + addend, e_fsel);
3243 	else
3244 	  value = hppa_field_adjust (value, -8 + addend, e_rsel);
3245 
3246 	/* Apply the relocation to the given instruction.  */
3247 	insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3248 	break;
3249       }
3250 
3251     case R_PARISC_PCREL12F:
3252     case R_PARISC_PCREL22F:
3253     case R_PARISC_PCREL17F:
3254     case R_PARISC_PCREL22C:
3255     case R_PARISC_PCREL17C:
3256     case R_PARISC_PCREL17R:
3257       {
3258 	/* If this is a call to a function defined in another dynamic
3259 	   library, then redirect the call to the local stub for this
3260 	   function.  */
3261 	if (sym_sec == NULL || sym_sec->output_section == NULL)
3262 	  value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3263 		   + hppa_info->stub_sec->output_section->vma);
3264 
3265 	/* Turn VALUE into a proper PC relative address.  */
3266 	value -= (offset + input_section->output_offset
3267 		  + input_section->output_section->vma);
3268 	addend -= 8;
3269 
3270 	if (r_type == (unsigned int) R_PARISC_PCREL22F)
3271 	  max_branch_offset = (1 << (22-1)) << 2;
3272 	else if (r_type == (unsigned int) R_PARISC_PCREL17F)
3273 	  max_branch_offset = (1 << (17-1)) << 2;
3274 	else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3275 	  max_branch_offset = (1 << (12-1)) << 2;
3276 
3277 	/* Make sure we can reach the branch target.  */
3278 	if (max_branch_offset != 0
3279 	    && value + addend + max_branch_offset >= 2*max_branch_offset)
3280 	  {
3281 	    _bfd_error_handler
3282 	      /* xgettext:c-format */
3283 	      (_("%pB(%pA+%#" PRIx64 "): cannot reach %s"),
3284 	      input_bfd,
3285 	      input_section,
3286 	      (uint64_t) offset,
3287 	      eh ? eh->root.root.string : "unknown");
3288 	    bfd_set_error (bfd_error_bad_value);
3289 	    return bfd_reloc_overflow;
3290 	  }
3291 
3292 	/* Adjust for any field selectors.  */
3293 	if (r_type == R_PARISC_PCREL17R)
3294 	  value = hppa_field_adjust (value, addend, e_rsel);
3295 	else
3296 	  value = hppa_field_adjust (value, addend, e_fsel);
3297 
3298 	/* All branches are implicitly shifted by 2 places.  */
3299 	value >>= 2;
3300 
3301 	/* Apply the relocation to the given instruction.  */
3302 	insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3303 	break;
3304       }
3305 
3306     /* Indirect references to data through the DLT.  */
3307     case R_PARISC_DLTIND14R:
3308     case R_PARISC_DLTIND14F:
3309     case R_PARISC_DLTIND14DR:
3310     case R_PARISC_DLTIND14WR:
3311     case R_PARISC_DLTIND21L:
3312     case R_PARISC_LTOFF_FPTR14R:
3313     case R_PARISC_LTOFF_FPTR14DR:
3314     case R_PARISC_LTOFF_FPTR14WR:
3315     case R_PARISC_LTOFF_FPTR21L:
3316     case R_PARISC_LTOFF_FPTR16F:
3317     case R_PARISC_LTOFF_FPTR16WF:
3318     case R_PARISC_LTOFF_FPTR16DF:
3319     case R_PARISC_LTOFF_TP21L:
3320     case R_PARISC_LTOFF_TP14R:
3321     case R_PARISC_LTOFF_TP14F:
3322     case R_PARISC_LTOFF_TP14WR:
3323     case R_PARISC_LTOFF_TP14DR:
3324     case R_PARISC_LTOFF_TP16F:
3325     case R_PARISC_LTOFF_TP16WF:
3326     case R_PARISC_LTOFF_TP16DF:
3327     case R_PARISC_LTOFF16F:
3328     case R_PARISC_LTOFF16WF:
3329     case R_PARISC_LTOFF16DF:
3330       {
3331 	bfd_vma off;
3332 
3333 	/* If this relocation was against a local symbol, then we still
3334 	   have not set up the DLT entry (it's not convenient to do so
3335 	   in the "finalize_dlt" routine because it is difficult to get
3336 	   to the local symbol's value).
3337 
3338 	   So, if this is a local symbol (h == NULL), then we need to
3339 	   fill in its DLT entry.
3340 
3341 	   Similarly we may still need to set up an entry in .opd for
3342 	   a local function which had its address taken.  */
3343 	if (hh == NULL)
3344 	  {
3345 	    bfd_vma *local_opd_offsets, *local_dlt_offsets;
3346 
3347 	    if (local_offsets == NULL)
3348 	      abort ();
3349 
3350 	    /* Now do .opd creation if needed.  */
3351 	    if (r_type == R_PARISC_LTOFF_FPTR14R
3352 		|| r_type == R_PARISC_LTOFF_FPTR14DR
3353 		|| r_type == R_PARISC_LTOFF_FPTR14WR
3354 		|| r_type == R_PARISC_LTOFF_FPTR21L
3355 		|| r_type == R_PARISC_LTOFF_FPTR16F
3356 		|| r_type == R_PARISC_LTOFF_FPTR16WF
3357 		|| r_type == R_PARISC_LTOFF_FPTR16DF)
3358 	      {
3359 		local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3360 		off = local_opd_offsets[r_symndx];
3361 
3362 		/* The last bit records whether we've already initialised
3363 		   this local .opd entry.  */
3364 		if ((off & 1) != 0)
3365 		  {
3366 		    BFD_ASSERT (off != (bfd_vma) -1);
3367 		    off &= ~1;
3368 		  }
3369 		else
3370 		  {
3371 		    local_opd_offsets[r_symndx] |= 1;
3372 
3373 		    /* The first two words of an .opd entry are zero.  */
3374 		    memset (hppa_info->opd_sec->contents + off, 0, 16);
3375 
3376 		    /* The next word is the address of the function.  */
3377 		    bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3378 				(hppa_info->opd_sec->contents + off + 16));
3379 
3380 		    /* The last word is our local __gp value.  */
3381 		    value = _bfd_get_gp_value (info->output_bfd);
3382 		    bfd_put_64 (hppa_info->opd_sec->owner, value,
3383 				(hppa_info->opd_sec->contents + off + 24));
3384 		  }
3385 
3386 		/* The DLT value is the address of the .opd entry.  */
3387 		value = (off
3388 			 + hppa_info->opd_sec->output_offset
3389 			 + hppa_info->opd_sec->output_section->vma);
3390 		addend = 0;
3391 	      }
3392 
3393 	    local_dlt_offsets = local_offsets;
3394 	    off = local_dlt_offsets[r_symndx];
3395 
3396 	    if ((off & 1) != 0)
3397 	      {
3398 		BFD_ASSERT (off != (bfd_vma) -1);
3399 		off &= ~1;
3400 	      }
3401 	    else
3402 	      {
3403 		local_dlt_offsets[r_symndx] |= 1;
3404 		bfd_put_64 (hppa_info->dlt_sec->owner,
3405 			    value + addend,
3406 			    hppa_info->dlt_sec->contents + off);
3407 	      }
3408 	  }
3409 	else
3410 	  off = hh->dlt_offset;
3411 
3412 	/* We want the value of the DLT offset for this symbol, not
3413 	   the symbol's actual address.  Note that __gp may not point
3414 	   to the start of the DLT, so we have to compute the absolute
3415 	   address, then subtract out the value of __gp.  */
3416 	value = (off
3417 		 + hppa_info->dlt_sec->output_offset
3418 		 + hppa_info->dlt_sec->output_section->vma);
3419 	value -= _bfd_get_gp_value (output_bfd);
3420 
3421 	/* All DLTIND relocations are basically the same at this point,
3422 	   except that we need different field selectors for the 21bit
3423 	   version vs the 14bit versions.  */
3424 	if (r_type == R_PARISC_DLTIND21L
3425 	    || r_type == R_PARISC_LTOFF_FPTR21L
3426 	    || r_type == R_PARISC_LTOFF_TP21L)
3427 	  value = hppa_field_adjust (value, 0, e_lsel);
3428 	else if (r_type == R_PARISC_DLTIND14F
3429 		 || r_type == R_PARISC_LTOFF_FPTR16F
3430 		 || r_type == R_PARISC_LTOFF_FPTR16WF
3431 		 || r_type == R_PARISC_LTOFF_FPTR16DF
3432 		 || r_type == R_PARISC_LTOFF16F
3433 		 || r_type == R_PARISC_LTOFF16DF
3434 		 || r_type == R_PARISC_LTOFF16WF
3435 		 || r_type == R_PARISC_LTOFF_TP16F
3436 		 || r_type == R_PARISC_LTOFF_TP16WF
3437 		 || r_type == R_PARISC_LTOFF_TP16DF)
3438 	  value = hppa_field_adjust (value, 0, e_fsel);
3439 	else
3440 	  value = hppa_field_adjust (value, 0, e_rsel);
3441 
3442 	insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3443 	break;
3444       }
3445 
3446     case R_PARISC_DLTREL14R:
3447     case R_PARISC_DLTREL14F:
3448     case R_PARISC_DLTREL14DR:
3449     case R_PARISC_DLTREL14WR:
3450     case R_PARISC_DLTREL21L:
3451     case R_PARISC_DPREL21L:
3452     case R_PARISC_DPREL14WR:
3453     case R_PARISC_DPREL14DR:
3454     case R_PARISC_DPREL14R:
3455     case R_PARISC_DPREL14F:
3456     case R_PARISC_GPREL16F:
3457     case R_PARISC_GPREL16WF:
3458     case R_PARISC_GPREL16DF:
3459       {
3460 	/* Subtract out the global pointer value to make value a DLT
3461 	   relative address.  */
3462 	value -= _bfd_get_gp_value (output_bfd);
3463 
3464 	/* All DLTREL relocations are basically the same at this point,
3465 	   except that we need different field selectors for the 21bit
3466 	   version vs the 14bit versions.  */
3467 	if (r_type == R_PARISC_DLTREL21L
3468 	    || r_type == R_PARISC_DPREL21L)
3469 	  value = hppa_field_adjust (value, addend, e_lrsel);
3470 	else if (r_type == R_PARISC_DLTREL14F
3471 		 || r_type == R_PARISC_DPREL14F
3472 		 || r_type == R_PARISC_GPREL16F
3473 		 || r_type == R_PARISC_GPREL16WF
3474 		 || r_type == R_PARISC_GPREL16DF)
3475 	  value = hppa_field_adjust (value, addend, e_fsel);
3476 	else
3477 	  value = hppa_field_adjust (value, addend, e_rrsel);
3478 
3479 	insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3480 	break;
3481       }
3482 
3483     case R_PARISC_DIR21L:
3484     case R_PARISC_DIR17R:
3485     case R_PARISC_DIR17F:
3486     case R_PARISC_DIR14R:
3487     case R_PARISC_DIR14F:
3488     case R_PARISC_DIR14WR:
3489     case R_PARISC_DIR14DR:
3490     case R_PARISC_DIR16F:
3491     case R_PARISC_DIR16WF:
3492     case R_PARISC_DIR16DF:
3493       {
3494 	/* All DIR relocations are basically the same at this point,
3495 	   except that branch offsets need to be divided by four, and
3496 	   we need different field selectors.  Note that we don't
3497 	   redirect absolute calls to local stubs.  */
3498 
3499 	if (r_type == R_PARISC_DIR21L)
3500 	  value = hppa_field_adjust (value, addend, e_lrsel);
3501 	else if (r_type == R_PARISC_DIR17F
3502 		 || r_type == R_PARISC_DIR16F
3503 		 || r_type == R_PARISC_DIR16WF
3504 		 || r_type == R_PARISC_DIR16DF
3505 		 || r_type == R_PARISC_DIR14F)
3506 	  value = hppa_field_adjust (value, addend, e_fsel);
3507 	else
3508 	  value = hppa_field_adjust (value, addend, e_rrsel);
3509 
3510 	if (r_type == R_PARISC_DIR17R || r_type == R_PARISC_DIR17F)
3511 	  /* All branches are implicitly shifted by 2 places.  */
3512 	  value >>= 2;
3513 
3514 	insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3515 	break;
3516       }
3517 
3518     case R_PARISC_PLTOFF21L:
3519     case R_PARISC_PLTOFF14R:
3520     case R_PARISC_PLTOFF14F:
3521     case R_PARISC_PLTOFF14WR:
3522     case R_PARISC_PLTOFF14DR:
3523     case R_PARISC_PLTOFF16F:
3524     case R_PARISC_PLTOFF16WF:
3525     case R_PARISC_PLTOFF16DF:
3526       {
3527 	/* We want the value of the PLT offset for this symbol, not
3528 	   the symbol's actual address.  Note that __gp may not point
3529 	   to the start of the DLT, so we have to compute the absolute
3530 	   address, then subtract out the value of __gp.  */
3531 	value = (hh->plt_offset
3532 		 + hppa_info->plt_sec->output_offset
3533 		 + hppa_info->plt_sec->output_section->vma);
3534 	value -= _bfd_get_gp_value (output_bfd);
3535 
3536 	/* All PLTOFF relocations are basically the same at this point,
3537 	   except that we need different field selectors for the 21bit
3538 	   version vs the 14bit versions.  */
3539 	if (r_type == R_PARISC_PLTOFF21L)
3540 	  value = hppa_field_adjust (value, addend, e_lrsel);
3541 	else if (r_type == R_PARISC_PLTOFF14F
3542 		 || r_type == R_PARISC_PLTOFF16F
3543 		 || r_type == R_PARISC_PLTOFF16WF
3544 		 || r_type == R_PARISC_PLTOFF16DF)
3545 	  value = hppa_field_adjust (value, addend, e_fsel);
3546 	else
3547 	  value = hppa_field_adjust (value, addend, e_rrsel);
3548 
3549 	insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3550 	break;
3551       }
3552 
3553     case R_PARISC_LTOFF_FPTR32:
3554       {
3555 	/* FIXME: There used to be code here to create the FPTR itself if
3556 	   the relocation was against a local symbol.  But the code could
3557 	   never have worked.  If the assert below is ever triggered then
3558 	   the code will need to be reinstated and fixed so that it does
3559 	   what is needed.  */
3560 	BFD_ASSERT (hh != NULL);
3561 
3562 	/* We want the value of the DLT offset for this symbol, not
3563 	   the symbol's actual address.  Note that __gp may not point
3564 	   to the start of the DLT, so we have to compute the absolute
3565 	   address, then subtract out the value of __gp.  */
3566 	value = (hh->dlt_offset
3567 		 + hppa_info->dlt_sec->output_offset
3568 		 + hppa_info->dlt_sec->output_section->vma);
3569 	value -= _bfd_get_gp_value (output_bfd);
3570 	bfd_put_32 (input_bfd, value, hit_data);
3571 	return bfd_reloc_ok;
3572       }
3573 
3574     case R_PARISC_LTOFF_FPTR64:
3575     case R_PARISC_LTOFF_TP64:
3576       {
3577 	/* We may still need to create the FPTR itself if it was for
3578 	   a local symbol.  */
3579 	if (eh == NULL && r_type == R_PARISC_LTOFF_FPTR64)
3580 	  {
3581 	    /* The first two words of an .opd entry are zero.  */
3582 	    memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
3583 
3584 	    /* The next word is the address of the function.  */
3585 	    bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3586 			(hppa_info->opd_sec->contents
3587 			 + hh->opd_offset + 16));
3588 
3589 	    /* The last word is our local __gp value.  */
3590 	    value = _bfd_get_gp_value (info->output_bfd);
3591 	    bfd_put_64 (hppa_info->opd_sec->owner, value,
3592 			hppa_info->opd_sec->contents + hh->opd_offset + 24);
3593 
3594 	    /* The DLT value is the address of the .opd entry.  */
3595 	    value = (hh->opd_offset
3596 		     + hppa_info->opd_sec->output_offset
3597 		     + hppa_info->opd_sec->output_section->vma);
3598 
3599 	    bfd_put_64 (hppa_info->dlt_sec->owner,
3600 			value,
3601 			hppa_info->dlt_sec->contents + hh->dlt_offset);
3602 	  }
3603 
3604 	/* We want the value of the DLT offset for this symbol, not
3605 	   the symbol's actual address.  Note that __gp may not point
3606 	   to the start of the DLT, so we have to compute the absolute
3607 	   address, then subtract out the value of __gp.  */
3608 	value = (hh->dlt_offset
3609 		 + hppa_info->dlt_sec->output_offset
3610 		 + hppa_info->dlt_sec->output_section->vma);
3611 	value -= _bfd_get_gp_value (output_bfd);
3612 	bfd_put_64 (input_bfd, value, hit_data);
3613 	return bfd_reloc_ok;
3614       }
3615 
3616     case R_PARISC_DIR32:
3617       bfd_put_32 (input_bfd, value + addend, hit_data);
3618       return bfd_reloc_ok;
3619 
3620     case R_PARISC_DIR64:
3621       bfd_put_64 (input_bfd, value + addend, hit_data);
3622       return bfd_reloc_ok;
3623 
3624     case R_PARISC_GPREL64:
3625       /* Subtract out the global pointer value to make value a DLT
3626 	 relative address.  */
3627       value -= _bfd_get_gp_value (output_bfd);
3628 
3629       bfd_put_64 (input_bfd, value + addend, hit_data);
3630       return bfd_reloc_ok;
3631 
3632     case R_PARISC_LTOFF64:
3633 	/* We want the value of the DLT offset for this symbol, not
3634 	   the symbol's actual address.  Note that __gp may not point
3635 	   to the start of the DLT, so we have to compute the absolute
3636 	   address, then subtract out the value of __gp.  */
3637       value = (hh->dlt_offset
3638 	       + hppa_info->dlt_sec->output_offset
3639 	       + hppa_info->dlt_sec->output_section->vma);
3640       value -= _bfd_get_gp_value (output_bfd);
3641 
3642       bfd_put_64 (input_bfd, value + addend, hit_data);
3643       return bfd_reloc_ok;
3644 
3645     case R_PARISC_PCREL32:
3646       {
3647 	/* If this is a call to a function defined in another dynamic
3648 	   library, then redirect the call to the local stub for this
3649 	   function.  */
3650 	if (sym_sec == NULL || sym_sec->output_section == NULL)
3651 	  value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3652 		   + hppa_info->stub_sec->output_section->vma);
3653 
3654 	/* Turn VALUE into a proper PC relative address.  */
3655 	value -= (offset + input_section->output_offset
3656 		  + input_section->output_section->vma);
3657 
3658 	value += addend;
3659 	value -= 8;
3660 	bfd_put_32 (input_bfd, value, hit_data);
3661 	return bfd_reloc_ok;
3662       }
3663 
3664     case R_PARISC_PCREL64:
3665       {
3666 	/* If this is a call to a function defined in another dynamic
3667 	   library, then redirect the call to the local stub for this
3668 	   function.  */
3669 	if (sym_sec == NULL || sym_sec->output_section == NULL)
3670 	  value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3671 		   + hppa_info->stub_sec->output_section->vma);
3672 
3673 	/* Turn VALUE into a proper PC relative address.  */
3674 	value -= (offset + input_section->output_offset
3675 		  + input_section->output_section->vma);
3676 
3677 	value += addend;
3678 	value -= 8;
3679 	bfd_put_64 (input_bfd, value, hit_data);
3680 	return bfd_reloc_ok;
3681       }
3682 
3683     case R_PARISC_FPTR64:
3684       {
3685 	bfd_vma off;
3686 
3687 	/* We may still need to create the FPTR itself if it was for
3688 	   a local symbol.  */
3689 	if (hh == NULL)
3690 	  {
3691 	    bfd_vma *local_opd_offsets;
3692 
3693 	    if (local_offsets == NULL)
3694 	      abort ();
3695 
3696 	    local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3697 	    off = local_opd_offsets[r_symndx];
3698 
3699 	    /* The last bit records whether we've already initialised
3700 	       this local .opd entry.  */
3701 	    if ((off & 1) != 0)
3702 	      {
3703 		BFD_ASSERT (off != (bfd_vma) -1);
3704 		off &= ~1;
3705 	      }
3706 	    else
3707 	      {
3708 		/* The first two words of an .opd entry are zero.  */
3709 		memset (hppa_info->opd_sec->contents + off, 0, 16);
3710 
3711 		/* The next word is the address of the function.  */
3712 		bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3713 			    (hppa_info->opd_sec->contents + off + 16));
3714 
3715 		/* The last word is our local __gp value.  */
3716 		value = _bfd_get_gp_value (info->output_bfd);
3717 		bfd_put_64 (hppa_info->opd_sec->owner, value,
3718 			    hppa_info->opd_sec->contents + off + 24);
3719 	      }
3720 	  }
3721 	else
3722 	  off = hh->opd_offset;
3723 
3724 	if (hh == NULL || hh->want_opd)
3725 	  /* We want the value of the OPD offset for this symbol.  */
3726 	  value = (off
3727 		   + hppa_info->opd_sec->output_offset
3728 		   + hppa_info->opd_sec->output_section->vma);
3729 	else
3730 	  /* We want the address of the symbol.  */
3731 	  value += addend;
3732 
3733 	bfd_put_64 (input_bfd, value, hit_data);
3734 	return bfd_reloc_ok;
3735       }
3736 
3737     case R_PARISC_SECREL32:
3738       if (sym_sec)
3739 	value -= sym_sec->output_section->vma;
3740       bfd_put_32 (input_bfd, value + addend, hit_data);
3741       return bfd_reloc_ok;
3742 
3743     case R_PARISC_SEGREL32:
3744     case R_PARISC_SEGREL64:
3745       {
3746 	/* If this is the first SEGREL relocation, then initialize
3747 	   the segment base values.  */
3748 	if (hppa_info->text_segment_base == (bfd_vma) -1)
3749 	  bfd_map_over_sections (output_bfd, elf_hppa_record_segment_addrs,
3750 				 hppa_info);
3751 
3752 	/* VALUE holds the absolute address.  We want to include the
3753 	   addend, then turn it into a segment relative address.
3754 
3755 	   The segment is derived from SYM_SEC.  We assume that there are
3756 	   only two segments of note in the resulting executable/shlib.
3757 	   A readonly segment (.text) and a readwrite segment (.data).  */
3758 	value += addend;
3759 
3760 	if (sym_sec->flags & SEC_CODE)
3761 	  value -= hppa_info->text_segment_base;
3762 	else
3763 	  value -= hppa_info->data_segment_base;
3764 
3765 	if (r_type == R_PARISC_SEGREL32)
3766 	  bfd_put_32 (input_bfd, value, hit_data);
3767 	else
3768 	  bfd_put_64 (input_bfd, value, hit_data);
3769 	return bfd_reloc_ok;
3770       }
3771 
3772     /* Something we don't know how to handle.  */
3773     default:
3774       return bfd_reloc_notsupported;
3775     }
3776 
3777   /* Update the instruction word.  */
3778   bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3779   return bfd_reloc_ok;
3780 }
3781 
3782 /* Relocate an HPPA ELF section.  */
3783 
3784 static bfd_boolean
3785 elf64_hppa_relocate_section (bfd *output_bfd,
3786 			   struct bfd_link_info *info,
3787 			   bfd *input_bfd,
3788 			   asection *input_section,
3789 			   bfd_byte *contents,
3790 			   Elf_Internal_Rela *relocs,
3791 			   Elf_Internal_Sym *local_syms,
3792 			   asection **local_sections)
3793 {
3794   Elf_Internal_Shdr *symtab_hdr;
3795   Elf_Internal_Rela *rel;
3796   Elf_Internal_Rela *relend;
3797   struct elf64_hppa_link_hash_table *hppa_info;
3798 
3799   hppa_info = hppa_link_hash_table (info);
3800   if (hppa_info == NULL)
3801     return FALSE;
3802 
3803   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3804 
3805   rel = relocs;
3806   relend = relocs + input_section->reloc_count;
3807   for (; rel < relend; rel++)
3808     {
3809       int r_type;
3810       reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3811       unsigned long r_symndx;
3812       struct elf_link_hash_entry *eh;
3813       Elf_Internal_Sym *sym;
3814       asection *sym_sec;
3815       bfd_vma relocation;
3816       bfd_reloc_status_type r;
3817 
3818       r_type = ELF_R_TYPE (rel->r_info);
3819       if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
3820 	{
3821 	  bfd_set_error (bfd_error_bad_value);
3822 	  return FALSE;
3823 	}
3824       if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3825 	  || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3826 	continue;
3827 
3828       /* This is a final link.  */
3829       r_symndx = ELF_R_SYM (rel->r_info);
3830       eh = NULL;
3831       sym = NULL;
3832       sym_sec = NULL;
3833       if (r_symndx < symtab_hdr->sh_info)
3834 	{
3835 	  /* This is a local symbol, hh defaults to NULL.  */
3836 	  sym = local_syms + r_symndx;
3837 	  sym_sec = local_sections[r_symndx];
3838 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3839 	}
3840       else
3841 	{
3842 	  /* This is not a local symbol.  */
3843 	  struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3844 
3845 	  /* It seems this can happen with erroneous or unsupported
3846 	     input (mixing a.out and elf in an archive, for example.)  */
3847 	  if (sym_hashes == NULL)
3848 	    return FALSE;
3849 
3850 	  eh = sym_hashes[r_symndx - symtab_hdr->sh_info];
3851 
3852 	  if (info->wrap_hash != NULL
3853 	      && (input_section->flags & SEC_DEBUGGING) != 0)
3854 	    eh = ((struct elf_link_hash_entry *)
3855 		  unwrap_hash_lookup (info, input_bfd, &eh->root));
3856 
3857 	  while (eh->root.type == bfd_link_hash_indirect
3858 		 || eh->root.type == bfd_link_hash_warning)
3859 	    eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3860 
3861 	  relocation = 0;
3862 	  if (eh->root.type == bfd_link_hash_defined
3863 	      || eh->root.type == bfd_link_hash_defweak)
3864 	    {
3865 	      sym_sec = eh->root.u.def.section;
3866 	      if (sym_sec != NULL
3867 		  && sym_sec->output_section != NULL)
3868 		relocation = (eh->root.u.def.value
3869 			      + sym_sec->output_section->vma
3870 			      + sym_sec->output_offset);
3871 	    }
3872 	  else if (eh->root.type == bfd_link_hash_undefweak)
3873 	    ;
3874 	  else if (info->unresolved_syms_in_objects == RM_IGNORE
3875 		   && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
3876 	    ;
3877 	  else if (!bfd_link_relocatable (info)
3878 		   && elf_hppa_is_dynamic_loader_symbol (eh->root.root.string))
3879 	    continue;
3880 	  else if (!bfd_link_relocatable (info))
3881 	    {
3882 	      bfd_boolean err;
3883 	      err = (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
3884 		     || ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT);
3885 	      (*info->callbacks->undefined_symbol) (info,
3886 						    eh->root.root.string,
3887 						    input_bfd,
3888 						    input_section,
3889 						    rel->r_offset, err);
3890 	    }
3891 
3892 	  if (!bfd_link_relocatable (info)
3893 	      && relocation == 0
3894 	      && eh->root.type != bfd_link_hash_defined
3895 	      && eh->root.type != bfd_link_hash_defweak
3896 	      && eh->root.type != bfd_link_hash_undefweak)
3897 	    {
3898 	      if (info->unresolved_syms_in_objects == RM_IGNORE
3899 		  && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3900 		  && eh->type == STT_PARISC_MILLI)
3901 		(*info->callbacks->undefined_symbol)
3902 		  (info, eh_name (eh), input_bfd,
3903 		   input_section, rel->r_offset, FALSE);
3904 	    }
3905 	}
3906 
3907       if (sym_sec != NULL && discarded_section (sym_sec))
3908 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3909 					 rel, 1, relend, howto, 0, contents);
3910 
3911       if (bfd_link_relocatable (info))
3912 	continue;
3913 
3914       r = elf_hppa_final_link_relocate (rel, input_bfd, output_bfd,
3915 					input_section, contents,
3916 					relocation, info, sym_sec,
3917 					eh);
3918 
3919       if (r != bfd_reloc_ok)
3920 	{
3921 	  switch (r)
3922 	    {
3923 	    default:
3924 	      abort ();
3925 	    case bfd_reloc_overflow:
3926 	      {
3927 		const char *sym_name;
3928 
3929 		if (eh != NULL)
3930 		  sym_name = NULL;
3931 		else
3932 		  {
3933 		    sym_name = bfd_elf_string_from_elf_section (input_bfd,
3934 								symtab_hdr->sh_link,
3935 								sym->st_name);
3936 		    if (sym_name == NULL)
3937 		      return FALSE;
3938 		    if (*sym_name == '\0')
3939 		      sym_name = bfd_section_name (sym_sec);
3940 		  }
3941 
3942 		(*info->callbacks->reloc_overflow)
3943 		  (info, (eh ? &eh->root : NULL), sym_name, howto->name,
3944 		   (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3945 	      }
3946 	      break;
3947 	    }
3948 	}
3949     }
3950   return TRUE;
3951 }
3952 
3953 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
3954 {
3955   { STRING_COMMA_LEN (".tbss"),	 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
3956   { STRING_COMMA_LEN (".fini"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3957   { STRING_COMMA_LEN (".init"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3958   { STRING_COMMA_LEN (".plt"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3959   { STRING_COMMA_LEN (".dlt"),	 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3960   { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3961   { STRING_COMMA_LEN (".sbss"),	 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3962   { NULL,		     0,	 0, 0,		  0 }
3963 };
3964 
3965 /* The hash bucket size is the standard one, namely 4.  */
3966 
3967 const struct elf_size_info hppa64_elf_size_info =
3968 {
3969   sizeof (Elf64_External_Ehdr),
3970   sizeof (Elf64_External_Phdr),
3971   sizeof (Elf64_External_Shdr),
3972   sizeof (Elf64_External_Rel),
3973   sizeof (Elf64_External_Rela),
3974   sizeof (Elf64_External_Sym),
3975   sizeof (Elf64_External_Dyn),
3976   sizeof (Elf_External_Note),
3977   4,
3978   1,
3979   64, 3,
3980   ELFCLASS64, EV_CURRENT,
3981   bfd_elf64_write_out_phdrs,
3982   bfd_elf64_write_shdrs_and_ehdr,
3983   bfd_elf64_checksum_contents,
3984   bfd_elf64_write_relocs,
3985   bfd_elf64_swap_symbol_in,
3986   bfd_elf64_swap_symbol_out,
3987   bfd_elf64_slurp_reloc_table,
3988   bfd_elf64_slurp_symbol_table,
3989   bfd_elf64_swap_dyn_in,
3990   bfd_elf64_swap_dyn_out,
3991   bfd_elf64_swap_reloc_in,
3992   bfd_elf64_swap_reloc_out,
3993   bfd_elf64_swap_reloca_in,
3994   bfd_elf64_swap_reloca_out
3995 };
3996 
3997 #define TARGET_BIG_SYM			hppa_elf64_vec
3998 #define TARGET_BIG_NAME			"elf64-hppa"
3999 #define ELF_ARCH			bfd_arch_hppa
4000 #define ELF_TARGET_ID			HPPA64_ELF_DATA
4001 #define ELF_MACHINE_CODE		EM_PARISC
4002 /* This is not strictly correct.  The maximum page size for PA2.0 is
4003    64M.  But everything still uses 4k.  */
4004 #define ELF_MAXPAGESIZE			0x1000
4005 #define ELF_OSABI			ELFOSABI_HPUX
4006 
4007 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4008 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
4009 #define bfd_elf64_bfd_is_local_label_name       elf_hppa_is_local_label_name
4010 #define elf_info_to_howto		elf_hppa_info_to_howto
4011 #define elf_info_to_howto_rel		elf_hppa_info_to_howto_rel
4012 
4013 #define elf_backend_section_from_shdr	elf64_hppa_section_from_shdr
4014 #define elf_backend_object_p		elf64_hppa_object_p
4015 #define elf_backend_final_write_processing \
4016 					elf_hppa_final_write_processing
4017 #define elf_backend_fake_sections	elf_hppa_fake_sections
4018 #define elf_backend_add_symbol_hook	elf_hppa_add_symbol_hook
4019 
4020 #define elf_backend_relocate_section	elf_hppa_relocate_section
4021 
4022 #define bfd_elf64_bfd_final_link	elf_hppa_final_link
4023 
4024 #define elf_backend_create_dynamic_sections \
4025 					elf64_hppa_create_dynamic_sections
4026 #define elf_backend_init_file_header	elf64_hppa_init_file_header
4027 
4028 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
4029 
4030 #define elf_backend_adjust_dynamic_symbol \
4031 					elf64_hppa_adjust_dynamic_symbol
4032 
4033 #define elf_backend_size_dynamic_sections \
4034 					elf64_hppa_size_dynamic_sections
4035 
4036 #define elf_backend_finish_dynamic_symbol \
4037 					elf64_hppa_finish_dynamic_symbol
4038 #define elf_backend_finish_dynamic_sections \
4039 					elf64_hppa_finish_dynamic_sections
4040 #define elf_backend_grok_prstatus	elf64_hppa_grok_prstatus
4041 #define elf_backend_grok_psinfo		elf64_hppa_grok_psinfo
4042 
4043 /* Stuff for the BFD linker: */
4044 #define bfd_elf64_bfd_link_hash_table_create \
4045 	elf64_hppa_hash_table_create
4046 
4047 #define elf_backend_check_relocs \
4048 	elf64_hppa_check_relocs
4049 
4050 #define elf_backend_size_info \
4051   hppa64_elf_size_info
4052 
4053 #define elf_backend_additional_program_headers \
4054 	elf64_hppa_additional_program_headers
4055 
4056 #define elf_backend_modify_segment_map \
4057 	elf64_hppa_modify_segment_map
4058 
4059 #define elf_backend_allow_non_load_phdr \
4060 	elf64_hppa_allow_non_load_phdr
4061 
4062 #define elf_backend_link_output_symbol_hook \
4063 	elf64_hppa_link_output_symbol_hook
4064 
4065 #define elf_backend_want_got_plt	0
4066 #define elf_backend_plt_readonly	0
4067 #define elf_backend_want_plt_sym	0
4068 #define elf_backend_got_header_size     0
4069 #define elf_backend_type_change_ok	TRUE
4070 #define elf_backend_get_symbol_type	elf64_hppa_elf_get_symbol_type
4071 #define elf_backend_reloc_type_class	elf64_hppa_reloc_type_class
4072 #define elf_backend_rela_normal		1
4073 #define elf_backend_special_sections	elf64_hppa_special_sections
4074 #define elf_backend_action_discarded	elf_hppa_action_discarded
4075 #define elf_backend_section_from_phdr   elf64_hppa_section_from_phdr
4076 
4077 #define elf64_bed			elf64_hppa_hpux_bed
4078 
4079 #include "elf64-target.h"
4080 
4081 #undef TARGET_BIG_SYM
4082 #define TARGET_BIG_SYM			hppa_elf64_linux_vec
4083 #undef TARGET_BIG_NAME
4084 #define TARGET_BIG_NAME			"elf64-hppa-linux"
4085 #undef ELF_OSABI
4086 #define ELF_OSABI			ELFOSABI_GNU
4087 #undef elf64_bed
4088 #define elf64_bed			elf64_hppa_linux_bed
4089 #undef elf_backend_special_sections
4090 #define elf_backend_special_sections	(elf64_hppa_special_sections + 1)
4091 
4092 #include "elf64-target.h"
4093