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