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