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