1/* Support for HPPA 64-bit ELF
2   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
3   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 2 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, MA 02110-1301, USA.  */
20
21#include "alloca-conf.h"
22#include "bfd.h"
23#include "sysdep.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/hppa.h"
27#include "libhppa.h"
28#include "elf64-hppa.h"
29#define ARCH_SIZE	       64
30
31#define PLT_ENTRY_SIZE 0x10
32#define DLT_ENTRY_SIZE 0x8
33#define OPD_ENTRY_SIZE 0x20
34
35#define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
36
37/* The stub is supposed to load the target address and target's DP
38   value out of the PLT, then do an external branch to the target
39   address.
40
41   LDD PLTOFF(%r27),%r1
42   BVE (%r1)
43   LDD PLTOFF+8(%r27),%r27
44
45   Note that we must use the LDD with a 14 bit displacement, not the one
46   with a 5 bit displacement.  */
47static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
48			  0x53, 0x7b, 0x00, 0x00 };
49
50struct elf64_hppa_dyn_hash_entry
51{
52  struct bfd_hash_entry root;
53
54  /* Offsets for this symbol in various linker sections.  */
55  bfd_vma dlt_offset;
56  bfd_vma plt_offset;
57  bfd_vma opd_offset;
58  bfd_vma stub_offset;
59
60  /* The symbol table entry, if any, that this was derived from.  */
61  struct elf_link_hash_entry *h;
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    /* The index of the section symbol for the input section of
92       the relocation.  Only needed when building shared libraries.  */
93    int sec_symndx;
94
95    /* The offset within the input section of the relocation.  */
96    bfd_vma offset;
97
98    /* The addend for the relocation.  */
99    bfd_vma addend;
100
101  } *reloc_entries;
102
103  /* Nonzero if this symbol needs an entry in one of the linker
104     sections.  */
105  unsigned want_dlt;
106  unsigned want_plt;
107  unsigned want_opd;
108  unsigned want_stub;
109};
110
111struct elf64_hppa_dyn_hash_table
112{
113  struct bfd_hash_table root;
114};
115
116struct elf64_hppa_link_hash_table
117{
118  struct elf_link_hash_table root;
119
120  /* Shortcuts to get to the various linker defined sections.  */
121  asection *dlt_sec;
122  asection *dlt_rel_sec;
123  asection *plt_sec;
124  asection *plt_rel_sec;
125  asection *opd_sec;
126  asection *opd_rel_sec;
127  asection *other_rel_sec;
128
129  /* Offset of __gp within .plt section.  When the PLT gets large we want
130     to slide __gp into the PLT section so that we can continue to use
131     single DP relative instructions to load values out of the PLT.  */
132  bfd_vma gp_offset;
133
134  /* Note this is not strictly correct.  We should create a stub section for
135     each input section with calls.  The stub section should be placed before
136     the section with the call.  */
137  asection *stub_sec;
138
139  bfd_vma text_segment_base;
140  bfd_vma data_segment_base;
141
142  struct elf64_hppa_dyn_hash_table dyn_hash_table;
143
144  /* We build tables to map from an input section back to its
145     symbol index.  This is the BFD for which we currently have
146     a map.  */
147  bfd *section_syms_bfd;
148
149  /* Array of symbol numbers for each input section attached to the
150     current BFD.  */
151  int *section_syms;
152};
153
154#define elf64_hppa_hash_table(p) \
155  ((struct elf64_hppa_link_hash_table *) ((p)->hash))
156
157typedef struct bfd_hash_entry *(*new_hash_entry_func)
158  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
159
160static struct bfd_hash_entry *elf64_hppa_new_dyn_hash_entry
161  PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
162	   const char *string));
163static struct bfd_link_hash_table *elf64_hppa_hash_table_create
164  PARAMS ((bfd *abfd));
165static struct elf64_hppa_dyn_hash_entry *elf64_hppa_dyn_hash_lookup
166  PARAMS ((struct elf64_hppa_dyn_hash_table *table, const char *string,
167	   bfd_boolean create, bfd_boolean copy));
168static void elf64_hppa_dyn_hash_traverse
169  PARAMS ((struct elf64_hppa_dyn_hash_table *table,
170	   bfd_boolean (*func) (struct elf64_hppa_dyn_hash_entry *, PTR),
171	   PTR info));
172
173static const char *get_dyn_name
174  PARAMS ((bfd *, struct elf_link_hash_entry *,
175	   const Elf_Internal_Rela *, char **, size_t *));
176
177/* This must follow the definitions of the various derived linker
178   hash tables and shared functions.  */
179#include "elf-hppa.h"
180
181static bfd_boolean elf64_hppa_object_p
182  PARAMS ((bfd *));
183
184static void elf64_hppa_post_process_headers
185  PARAMS ((bfd *, struct bfd_link_info *));
186
187static bfd_boolean elf64_hppa_create_dynamic_sections
188  PARAMS ((bfd *, struct bfd_link_info *));
189
190static bfd_boolean elf64_hppa_adjust_dynamic_symbol
191  PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
192
193static bfd_boolean elf64_hppa_mark_milli_and_exported_functions
194  PARAMS ((struct elf_link_hash_entry *, PTR));
195
196static bfd_boolean elf64_hppa_size_dynamic_sections
197  PARAMS ((bfd *, struct bfd_link_info *));
198
199static bfd_boolean elf64_hppa_link_output_symbol_hook
200  PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
201	   asection *, struct elf_link_hash_entry *));
202
203static bfd_boolean elf64_hppa_finish_dynamic_symbol
204  PARAMS ((bfd *, struct bfd_link_info *,
205	   struct elf_link_hash_entry *, Elf_Internal_Sym *));
206
207static int elf64_hppa_additional_program_headers
208  PARAMS ((bfd *));
209
210static bfd_boolean elf64_hppa_modify_segment_map
211  PARAMS ((bfd *, struct bfd_link_info *));
212
213static enum elf_reloc_type_class elf64_hppa_reloc_type_class
214  PARAMS ((const Elf_Internal_Rela *));
215
216static bfd_boolean elf64_hppa_finish_dynamic_sections
217  PARAMS ((bfd *, struct bfd_link_info *));
218
219static bfd_boolean elf64_hppa_check_relocs
220  PARAMS ((bfd *, struct bfd_link_info *,
221	   asection *, const Elf_Internal_Rela *));
222
223static bfd_boolean elf64_hppa_dynamic_symbol_p
224  PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
225
226static bfd_boolean elf64_hppa_mark_exported_functions
227  PARAMS ((struct elf_link_hash_entry *, PTR));
228
229static bfd_boolean elf64_hppa_finalize_opd
230  PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
231
232static bfd_boolean elf64_hppa_finalize_dlt
233  PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
234
235static bfd_boolean allocate_global_data_dlt
236  PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
237
238static bfd_boolean allocate_global_data_plt
239  PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
240
241static bfd_boolean allocate_global_data_stub
242  PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
243
244static bfd_boolean allocate_global_data_opd
245  PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
246
247static bfd_boolean get_reloc_section
248  PARAMS ((bfd *, struct elf64_hppa_link_hash_table *, asection *));
249
250static bfd_boolean count_dyn_reloc
251  PARAMS ((bfd *, struct elf64_hppa_dyn_hash_entry *,
252	   int, asection *, int, bfd_vma, bfd_vma));
253
254static bfd_boolean allocate_dynrel_entries
255  PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
256
257static bfd_boolean elf64_hppa_finalize_dynreloc
258  PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
259
260static bfd_boolean get_opd
261  PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
262
263static bfd_boolean get_plt
264  PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
265
266static bfd_boolean get_dlt
267  PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
268
269static bfd_boolean get_stub
270  PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
271
272static int elf64_hppa_elf_get_symbol_type
273  PARAMS ((Elf_Internal_Sym *, int));
274
275static bfd_boolean
276elf64_hppa_dyn_hash_table_init (struct elf64_hppa_dyn_hash_table *ht,
277				bfd *abfd ATTRIBUTE_UNUSED,
278				new_hash_entry_func new,
279				unsigned int entsize)
280{
281  memset (ht, 0, sizeof (*ht));
282  return bfd_hash_table_init (&ht->root, new, entsize);
283}
284
285static struct bfd_hash_entry*
286elf64_hppa_new_dyn_hash_entry (entry, table, string)
287     struct bfd_hash_entry *entry;
288     struct bfd_hash_table *table;
289     const char *string;
290{
291  struct elf64_hppa_dyn_hash_entry *ret;
292  ret = (struct elf64_hppa_dyn_hash_entry *) entry;
293
294  /* Allocate the structure if it has not already been allocated by a
295     subclass.  */
296  if (!ret)
297    ret = bfd_hash_allocate (table, sizeof (*ret));
298
299  if (!ret)
300    return 0;
301
302  /* Call the allocation method of the superclass.  */
303  ret = ((struct elf64_hppa_dyn_hash_entry *)
304	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
305
306  /* Initialize our local data.  All zeros.  */
307  memset (&ret->dlt_offset, 0,
308	  (sizeof (struct elf64_hppa_dyn_hash_entry)
309	   - offsetof (struct elf64_hppa_dyn_hash_entry, dlt_offset)));
310
311  return &ret->root;
312}
313
314/* Create the derived linker hash table.  The PA64 ELF port uses this
315   derived hash table to keep information specific to the PA ElF
316   linker (without using static variables).  */
317
318static struct bfd_link_hash_table*
319elf64_hppa_hash_table_create (abfd)
320     bfd *abfd;
321{
322  struct elf64_hppa_link_hash_table *ret;
323
324  ret = bfd_zalloc (abfd, (bfd_size_type) sizeof (*ret));
325  if (!ret)
326    return 0;
327  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
328				      _bfd_elf_link_hash_newfunc,
329				      sizeof (struct elf_link_hash_entry)))
330    {
331      bfd_release (abfd, ret);
332      return 0;
333    }
334
335  if (!elf64_hppa_dyn_hash_table_init (&ret->dyn_hash_table, abfd,
336				       elf64_hppa_new_dyn_hash_entry,
337				       sizeof (struct elf64_hppa_dyn_hash_entry)))
338    return 0;
339  return &ret->root.root;
340}
341
342/* Look up an entry in a PA64 ELF linker hash table.  */
343
344static struct elf64_hppa_dyn_hash_entry *
345elf64_hppa_dyn_hash_lookup(table, string, create, copy)
346     struct elf64_hppa_dyn_hash_table *table;
347     const char *string;
348     bfd_boolean create, copy;
349{
350  return ((struct elf64_hppa_dyn_hash_entry *)
351	  bfd_hash_lookup (&table->root, string, create, copy));
352}
353
354/* Traverse a PA64 ELF linker hash table.  */
355
356static void
357elf64_hppa_dyn_hash_traverse (table, func, info)
358     struct elf64_hppa_dyn_hash_table *table;
359     bfd_boolean (*func) PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
360     PTR info;
361{
362  (bfd_hash_traverse
363   (&table->root,
364    (bfd_boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) func,
365    info));
366}
367
368/* Return nonzero if ABFD represents a PA2.0 ELF64 file.
369
370   Additionally we set the default architecture and machine.  */
371static bfd_boolean
372elf64_hppa_object_p (abfd)
373     bfd *abfd;
374{
375  Elf_Internal_Ehdr * i_ehdrp;
376  unsigned int flags;
377
378  i_ehdrp = elf_elfheader (abfd);
379  if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
380    {
381      /* GCC on hppa-linux produces binaries with OSABI=Linux,
382	 but the kernel produces corefiles with OSABI=SysV.  */
383      if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX
384	  && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
385	return FALSE;
386    }
387  else
388    {
389      /* HPUX produces binaries with OSABI=HPUX,
390	 but the kernel produces corefiles with OSABI=SysV.  */
391      if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
392	  && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
393	return FALSE;
394    }
395
396  flags = i_ehdrp->e_flags;
397  switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
398    {
399    case EFA_PARISC_1_0:
400      return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
401    case EFA_PARISC_1_1:
402      return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
403    case EFA_PARISC_2_0:
404      if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
405        return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
406      else
407        return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
408    case EFA_PARISC_2_0 | EF_PARISC_WIDE:
409      return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
410    }
411  /* Don't be fussy.  */
412  return TRUE;
413}
414
415/* Given section type (hdr->sh_type), return a boolean indicating
416   whether or not the section is an elf64-hppa specific section.  */
417static bfd_boolean
418elf64_hppa_section_from_shdr (bfd *abfd,
419			      Elf_Internal_Shdr *hdr,
420			      const char *name,
421			      int shindex)
422{
423  asection *newsect;
424
425  switch (hdr->sh_type)
426    {
427    case SHT_PARISC_EXT:
428      if (strcmp (name, ".PARISC.archext") != 0)
429	return FALSE;
430      break;
431    case SHT_PARISC_UNWIND:
432      if (strcmp (name, ".PARISC.unwind") != 0)
433	return FALSE;
434      break;
435    case SHT_PARISC_DOC:
436    case SHT_PARISC_ANNOT:
437    default:
438      return FALSE;
439    }
440
441  if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
442    return FALSE;
443  newsect = hdr->bfd_section;
444
445  return TRUE;
446}
447
448/* Construct a string for use in the elf64_hppa_dyn_hash_table.  The
449   name describes what was once potentially anonymous memory.  We
450   allocate memory as necessary, possibly reusing PBUF/PLEN.  */
451
452static const char *
453get_dyn_name (abfd, h, rel, pbuf, plen)
454     bfd *abfd;
455     struct elf_link_hash_entry *h;
456     const Elf_Internal_Rela *rel;
457     char **pbuf;
458     size_t *plen;
459{
460  asection *sec = abfd->sections;
461  size_t nlen, tlen;
462  char *buf;
463  size_t len;
464
465  if (h && rel->r_addend == 0)
466    return h->root.root.string;
467
468  if (h)
469    nlen = strlen (h->root.root.string);
470  else
471    nlen = 8 + 1 + sizeof (rel->r_info) * 2 - 8;
472  tlen = nlen + 1 + sizeof (rel->r_addend) * 2 + 1;
473
474  len = *plen;
475  buf = *pbuf;
476  if (len < tlen)
477    {
478      if (buf)
479	free (buf);
480      *pbuf = buf = malloc (tlen);
481      *plen = len = tlen;
482      if (!buf)
483	return NULL;
484    }
485
486  if (h)
487    {
488      memcpy (buf, h->root.root.string, nlen);
489      buf[nlen++] = '+';
490      sprintf_vma (buf + nlen, rel->r_addend);
491    }
492  else
493    {
494      nlen = sprintf (buf, "%x:%lx",
495		      sec->id & 0xffffffff,
496		      (long) ELF64_R_SYM (rel->r_info));
497      if (rel->r_addend)
498	{
499	  buf[nlen++] = '+';
500	  sprintf_vma (buf + nlen, rel->r_addend);
501	}
502    }
503
504  return buf;
505}
506
507/* SEC is a section containing relocs for an input BFD when linking; return
508   a suitable section for holding relocs in the output BFD for a link.  */
509
510static bfd_boolean
511get_reloc_section (abfd, hppa_info, sec)
512     bfd *abfd;
513     struct elf64_hppa_link_hash_table *hppa_info;
514     asection *sec;
515{
516  const char *srel_name;
517  asection *srel;
518  bfd *dynobj;
519
520  srel_name = (bfd_elf_string_from_elf_section
521	       (abfd, elf_elfheader(abfd)->e_shstrndx,
522		elf_section_data(sec)->rel_hdr.sh_name));
523  if (srel_name == NULL)
524    return FALSE;
525
526  BFD_ASSERT ((strncmp (srel_name, ".rela", 5) == 0
527	       && strcmp (bfd_get_section_name (abfd, sec),
528			  srel_name+5) == 0)
529	      || (strncmp (srel_name, ".rel", 4) == 0
530		  && strcmp (bfd_get_section_name (abfd, sec),
531			     srel_name+4) == 0));
532
533  dynobj = hppa_info->root.dynobj;
534  if (!dynobj)
535    hppa_info->root.dynobj = dynobj = abfd;
536
537  srel = bfd_get_section_by_name (dynobj, srel_name);
538  if (srel == NULL)
539    {
540      srel = bfd_make_section_with_flags (dynobj, srel_name,
541					  (SEC_ALLOC
542					   | SEC_LOAD
543					   | SEC_HAS_CONTENTS
544					   | SEC_IN_MEMORY
545					   | SEC_LINKER_CREATED
546					   | SEC_READONLY));
547      if (srel == NULL
548	  || !bfd_set_section_alignment (dynobj, srel, 3))
549	return FALSE;
550    }
551
552  hppa_info->other_rel_sec = srel;
553  return TRUE;
554}
555
556/* Add a new entry to the list of dynamic relocations against DYN_H.
557
558   We use this to keep a record of all the FPTR relocations against a
559   particular symbol so that we can create FPTR relocations in the
560   output file.  */
561
562static bfd_boolean
563count_dyn_reloc (abfd, dyn_h, type, sec, sec_symndx, offset, addend)
564     bfd *abfd;
565     struct elf64_hppa_dyn_hash_entry *dyn_h;
566     int type;
567     asection *sec;
568     int sec_symndx;
569     bfd_vma offset;
570     bfd_vma addend;
571{
572  struct elf64_hppa_dyn_reloc_entry *rent;
573
574  rent = (struct elf64_hppa_dyn_reloc_entry *)
575  bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
576  if (!rent)
577    return FALSE;
578
579  rent->next = dyn_h->reloc_entries;
580  rent->type = type;
581  rent->sec = sec;
582  rent->sec_symndx = sec_symndx;
583  rent->offset = offset;
584  rent->addend = addend;
585  dyn_h->reloc_entries = rent;
586
587  return TRUE;
588}
589
590/* Scan the RELOCS and record the type of dynamic entries that each
591   referenced symbol needs.  */
592
593static bfd_boolean
594elf64_hppa_check_relocs (abfd, info, sec, relocs)
595     bfd *abfd;
596     struct bfd_link_info *info;
597     asection *sec;
598     const Elf_Internal_Rela *relocs;
599{
600  struct elf64_hppa_link_hash_table *hppa_info;
601  const Elf_Internal_Rela *relend;
602  Elf_Internal_Shdr *symtab_hdr;
603  const Elf_Internal_Rela *rel;
604  asection *dlt, *plt, *stubs;
605  char *buf;
606  size_t buf_len;
607  int sec_symndx;
608
609  if (info->relocatable)
610    return TRUE;
611
612  /* If this is the first dynamic object found in the link, create
613     the special sections required for dynamic linking.  */
614  if (! elf_hash_table (info)->dynamic_sections_created)
615    {
616      if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
617	return FALSE;
618    }
619
620  hppa_info = elf64_hppa_hash_table (info);
621  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
622
623  /* If necessary, build a new table holding section symbols indices
624     for this BFD.  */
625
626  if (info->shared && hppa_info->section_syms_bfd != abfd)
627    {
628      unsigned long i;
629      unsigned int highest_shndx;
630      Elf_Internal_Sym *local_syms = NULL;
631      Elf_Internal_Sym *isym, *isymend;
632      bfd_size_type amt;
633
634      /* We're done with the old cache of section index to section symbol
635	 index information.  Free it.
636
637	 ?!? Note we leak the last section_syms array.  Presumably we
638	 could free it in one of the later routines in this file.  */
639      if (hppa_info->section_syms)
640	free (hppa_info->section_syms);
641
642      /* Read this BFD's local symbols.  */
643      if (symtab_hdr->sh_info != 0)
644	{
645	  local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
646	  if (local_syms == NULL)
647	    local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
648					       symtab_hdr->sh_info, 0,
649					       NULL, NULL, NULL);
650	  if (local_syms == NULL)
651	    return FALSE;
652	}
653
654      /* Record the highest section index referenced by the local symbols.  */
655      highest_shndx = 0;
656      isymend = local_syms + symtab_hdr->sh_info;
657      for (isym = local_syms; isym < isymend; isym++)
658	{
659	  if (isym->st_shndx > highest_shndx)
660	    highest_shndx = isym->st_shndx;
661	}
662
663      /* Allocate an array to hold the section index to section symbol index
664	 mapping.  Bump by one since we start counting at zero.  */
665      highest_shndx++;
666      amt = highest_shndx;
667      amt *= sizeof (int);
668      hppa_info->section_syms = (int *) bfd_malloc (amt);
669
670      /* Now walk the local symbols again.  If we find a section symbol,
671	 record the index of the symbol into the section_syms array.  */
672      for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
673	{
674	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
675	    hppa_info->section_syms[isym->st_shndx] = i;
676	}
677
678      /* We are finished with the local symbols.  */
679      if (local_syms != NULL
680	  && symtab_hdr->contents != (unsigned char *) local_syms)
681	{
682	  if (! info->keep_memory)
683	    free (local_syms);
684	  else
685	    {
686	      /* Cache the symbols for elf_link_input_bfd.  */
687	      symtab_hdr->contents = (unsigned char *) local_syms;
688	    }
689	}
690
691      /* Record which BFD we built the section_syms mapping for.  */
692      hppa_info->section_syms_bfd = abfd;
693    }
694
695  /* Record the symbol index for this input section.  We may need it for
696     relocations when building shared libraries.  When not building shared
697     libraries this value is never really used, but assign it to zero to
698     prevent out of bounds memory accesses in other routines.  */
699  if (info->shared)
700    {
701      sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
702
703      /* If we did not find a section symbol for this section, then
704	 something went terribly wrong above.  */
705      if (sec_symndx == -1)
706	return FALSE;
707
708      sec_symndx = hppa_info->section_syms[sec_symndx];
709    }
710  else
711    sec_symndx = 0;
712
713  dlt = plt = stubs = NULL;
714  buf = NULL;
715  buf_len = 0;
716
717  relend = relocs + sec->reloc_count;
718  for (rel = relocs; rel < relend; ++rel)
719    {
720      enum
721	{
722	  NEED_DLT = 1,
723	  NEED_PLT = 2,
724	  NEED_STUB = 4,
725	  NEED_OPD = 8,
726	  NEED_DYNREL = 16,
727	};
728
729      struct elf_link_hash_entry *h = NULL;
730      unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
731      struct elf64_hppa_dyn_hash_entry *dyn_h;
732      int need_entry;
733      const char *addr_name;
734      bfd_boolean maybe_dynamic;
735      int dynrel_type = R_PARISC_NONE;
736      static reloc_howto_type *howto;
737
738      if (r_symndx >= symtab_hdr->sh_info)
739	{
740	  /* We're dealing with a global symbol -- find its hash entry
741	     and mark it as being referenced.  */
742	  long indx = r_symndx - symtab_hdr->sh_info;
743	  h = elf_sym_hashes (abfd)[indx];
744	  while (h->root.type == bfd_link_hash_indirect
745		 || h->root.type == bfd_link_hash_warning)
746	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
747
748	  h->ref_regular = 1;
749	}
750
751      /* We can only get preliminary data on whether a symbol is
752	 locally or externally defined, as not all of the input files
753	 have yet been processed.  Do something with what we know, as
754	 this may help reduce memory usage and processing time later.  */
755      maybe_dynamic = FALSE;
756      if (h && ((info->shared
757		 && (!info->symbolic
758		     || info->unresolved_syms_in_shared_libs == RM_IGNORE))
759		|| !h->def_regular
760		|| h->root.type == bfd_link_hash_defweak))
761	maybe_dynamic = TRUE;
762
763      howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
764      need_entry = 0;
765      switch (howto->type)
766	{
767	/* These are simple indirect references to symbols through the
768	   DLT.  We need to create a DLT entry for any symbols which
769	   appears in a DLTIND relocation.  */
770	case R_PARISC_DLTIND21L:
771	case R_PARISC_DLTIND14R:
772	case R_PARISC_DLTIND14F:
773	case R_PARISC_DLTIND14WR:
774	case R_PARISC_DLTIND14DR:
775	  need_entry = NEED_DLT;
776	  break;
777
778	/* ?!?  These need a DLT entry.  But I have no idea what to do with
779	   the "link time TP value.  */
780	case R_PARISC_LTOFF_TP21L:
781	case R_PARISC_LTOFF_TP14R:
782	case R_PARISC_LTOFF_TP14F:
783	case R_PARISC_LTOFF_TP64:
784	case R_PARISC_LTOFF_TP14WR:
785	case R_PARISC_LTOFF_TP14DR:
786	case R_PARISC_LTOFF_TP16F:
787	case R_PARISC_LTOFF_TP16WF:
788	case R_PARISC_LTOFF_TP16DF:
789	  need_entry = NEED_DLT;
790	  break;
791
792	/* These are function calls.  Depending on their precise target we
793	   may need to make a stub for them.  The stub uses the PLT, so we
794	   need to create PLT entries for these symbols too.  */
795	case R_PARISC_PCREL12F:
796	case R_PARISC_PCREL17F:
797	case R_PARISC_PCREL22F:
798	case R_PARISC_PCREL32:
799	case R_PARISC_PCREL64:
800	case R_PARISC_PCREL21L:
801	case R_PARISC_PCREL17R:
802	case R_PARISC_PCREL17C:
803	case R_PARISC_PCREL14R:
804	case R_PARISC_PCREL14F:
805	case R_PARISC_PCREL22C:
806	case R_PARISC_PCREL14WR:
807	case R_PARISC_PCREL14DR:
808	case R_PARISC_PCREL16F:
809	case R_PARISC_PCREL16WF:
810	case R_PARISC_PCREL16DF:
811	  need_entry = (NEED_PLT | NEED_STUB);
812	  break;
813
814	case R_PARISC_PLTOFF21L:
815	case R_PARISC_PLTOFF14R:
816	case R_PARISC_PLTOFF14F:
817	case R_PARISC_PLTOFF14WR:
818	case R_PARISC_PLTOFF14DR:
819	case R_PARISC_PLTOFF16F:
820	case R_PARISC_PLTOFF16WF:
821	case R_PARISC_PLTOFF16DF:
822	  need_entry = (NEED_PLT);
823	  break;
824
825	case R_PARISC_DIR64:
826	  if (info->shared || maybe_dynamic)
827	    need_entry = (NEED_DYNREL);
828	  dynrel_type = R_PARISC_DIR64;
829	  break;
830
831	/* This is an indirect reference through the DLT to get the address
832	   of a OPD descriptor.  Thus we need to make a DLT entry that points
833	   to an OPD entry.  */
834	case R_PARISC_LTOFF_FPTR21L:
835	case R_PARISC_LTOFF_FPTR14R:
836	case R_PARISC_LTOFF_FPTR14WR:
837	case R_PARISC_LTOFF_FPTR14DR:
838	case R_PARISC_LTOFF_FPTR32:
839	case R_PARISC_LTOFF_FPTR64:
840	case R_PARISC_LTOFF_FPTR16F:
841	case R_PARISC_LTOFF_FPTR16WF:
842	case R_PARISC_LTOFF_FPTR16DF:
843	  if (info->shared || maybe_dynamic)
844	    need_entry = (NEED_DLT | NEED_OPD);
845	  else
846	    need_entry = (NEED_DLT | NEED_OPD);
847	  dynrel_type = R_PARISC_FPTR64;
848	  break;
849
850	/* This is a simple OPD entry.  */
851	case R_PARISC_FPTR64:
852	  if (info->shared || maybe_dynamic)
853	    need_entry = (NEED_OPD | NEED_DYNREL);
854	  else
855	    need_entry = (NEED_OPD);
856	  dynrel_type = R_PARISC_FPTR64;
857	  break;
858
859	/* Add more cases as needed.  */
860	}
861
862      if (!need_entry)
863	continue;
864
865      /* Collect a canonical name for this address.  */
866      addr_name = get_dyn_name (abfd, h, rel, &buf, &buf_len);
867
868      /* Collect the canonical entry data for this address.  */
869      dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
870					  addr_name, TRUE, TRUE);
871      BFD_ASSERT (dyn_h);
872
873      /* Stash away enough information to be able to find this symbol
874	 regardless of whether or not it is local or global.  */
875      dyn_h->h = h;
876      dyn_h->owner = abfd;
877      dyn_h->sym_indx = r_symndx;
878
879      /* ?!? We may need to do some error checking in here.  */
880      /* Create what's needed.  */
881      if (need_entry & NEED_DLT)
882	{
883	  if (! hppa_info->dlt_sec
884	      && ! get_dlt (abfd, info, hppa_info))
885	    goto err_out;
886	  dyn_h->want_dlt = 1;
887	}
888
889      if (need_entry & NEED_PLT)
890	{
891	  if (! hppa_info->plt_sec
892	      && ! get_plt (abfd, info, hppa_info))
893	    goto err_out;
894	  dyn_h->want_plt = 1;
895	}
896
897      if (need_entry & NEED_STUB)
898	{
899	  if (! hppa_info->stub_sec
900	      && ! get_stub (abfd, info, hppa_info))
901	    goto err_out;
902	  dyn_h->want_stub = 1;
903	}
904
905      if (need_entry & NEED_OPD)
906	{
907	  if (! hppa_info->opd_sec
908	      && ! get_opd (abfd, info, hppa_info))
909	    goto err_out;
910
911	  dyn_h->want_opd = 1;
912
913	  /* FPTRs are not allocated by the dynamic linker for PA64, though
914	     it is possible that will change in the future.  */
915
916	  /* This could be a local function that had its address taken, in
917	     which case H will be NULL.  */
918	  if (h)
919	    h->needs_plt = 1;
920	}
921
922      /* Add a new dynamic relocation to the chain of dynamic
923	 relocations for this symbol.  */
924      if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
925	{
926	  if (! hppa_info->other_rel_sec
927	      && ! get_reloc_section (abfd, hppa_info, sec))
928	    goto err_out;
929
930	  if (!count_dyn_reloc (abfd, dyn_h, dynrel_type, sec,
931				sec_symndx, rel->r_offset, rel->r_addend))
932	    goto err_out;
933
934	  /* If we are building a shared library and we just recorded
935	     a dynamic R_PARISC_FPTR64 relocation, then make sure the
936	     section symbol for this section ends up in the dynamic
937	     symbol table.  */
938	  if (info->shared && dynrel_type == R_PARISC_FPTR64
939	      && ! (bfd_elf_link_record_local_dynamic_symbol
940		    (info, abfd, sec_symndx)))
941	    return FALSE;
942	}
943    }
944
945  if (buf)
946    free (buf);
947  return TRUE;
948
949 err_out:
950  if (buf)
951    free (buf);
952  return FALSE;
953}
954
955struct elf64_hppa_allocate_data
956{
957  struct bfd_link_info *info;
958  bfd_size_type ofs;
959};
960
961/* Should we do dynamic things to this symbol?  */
962
963static bfd_boolean
964elf64_hppa_dynamic_symbol_p (h, info)
965     struct elf_link_hash_entry *h;
966     struct bfd_link_info *info;
967{
968  /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
969     and relocations that retrieve a function descriptor?  Assume the
970     worst for now.  */
971  if (_bfd_elf_dynamic_symbol_p (h, info, 1))
972    {
973      /* ??? Why is this here and not elsewhere is_local_label_name.  */
974      if (h->root.root.string[0] == '$' && h->root.root.string[1] == '$')
975	return FALSE;
976
977      return TRUE;
978    }
979  else
980    return FALSE;
981}
982
983/* Mark all functions exported by this file so that we can later allocate
984   entries in .opd for them.  */
985
986static bfd_boolean
987elf64_hppa_mark_exported_functions (h, data)
988     struct elf_link_hash_entry *h;
989     PTR data;
990{
991  struct bfd_link_info *info = (struct bfd_link_info *)data;
992  struct elf64_hppa_link_hash_table *hppa_info;
993
994  hppa_info = elf64_hppa_hash_table (info);
995
996  if (h->root.type == bfd_link_hash_warning)
997    h = (struct elf_link_hash_entry *) h->root.u.i.link;
998
999  if (h
1000      && (h->root.type == bfd_link_hash_defined
1001	  || h->root.type == bfd_link_hash_defweak)
1002      && h->root.u.def.section->output_section != NULL
1003      && h->type == STT_FUNC)
1004    {
1005       struct elf64_hppa_dyn_hash_entry *dyn_h;
1006
1007      /* Add this symbol to the PA64 linker hash table.  */
1008      dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
1009					  h->root.root.string, TRUE, TRUE);
1010      BFD_ASSERT (dyn_h);
1011      dyn_h->h = h;
1012
1013      if (! hppa_info->opd_sec
1014	  && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
1015	return FALSE;
1016
1017      dyn_h->want_opd = 1;
1018      /* Put a flag here for output_symbol_hook.  */
1019      dyn_h->st_shndx = -1;
1020      h->needs_plt = 1;
1021    }
1022
1023  return TRUE;
1024}
1025
1026/* Allocate space for a DLT entry.  */
1027
1028static bfd_boolean
1029allocate_global_data_dlt (dyn_h, data)
1030     struct elf64_hppa_dyn_hash_entry *dyn_h;
1031     PTR data;
1032{
1033  struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1034
1035  if (dyn_h->want_dlt)
1036    {
1037      struct elf_link_hash_entry *h = dyn_h->h;
1038
1039      if (x->info->shared)
1040	{
1041	  /* Possibly add the symbol to the local dynamic symbol
1042	     table since we might need to create a dynamic relocation
1043	     against it.  */
1044	  if (! h
1045	      || (h->dynindx == -1 && h->type != STT_PARISC_MILLI))
1046	    {
1047	      bfd *owner;
1048	      owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
1049
1050	      if (! (bfd_elf_link_record_local_dynamic_symbol
1051		     (x->info, owner, dyn_h->sym_indx)))
1052		return FALSE;
1053	    }
1054	}
1055
1056      dyn_h->dlt_offset = x->ofs;
1057      x->ofs += DLT_ENTRY_SIZE;
1058    }
1059  return TRUE;
1060}
1061
1062/* Allocate space for a DLT.PLT entry.  */
1063
1064static bfd_boolean
1065allocate_global_data_plt (dyn_h, data)
1066     struct elf64_hppa_dyn_hash_entry *dyn_h;
1067     PTR data;
1068{
1069  struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1070
1071  if (dyn_h->want_plt
1072      && elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info)
1073      && !((dyn_h->h->root.type == bfd_link_hash_defined
1074	    || dyn_h->h->root.type == bfd_link_hash_defweak)
1075	   && dyn_h->h->root.u.def.section->output_section != NULL))
1076    {
1077      dyn_h->plt_offset = x->ofs;
1078      x->ofs += PLT_ENTRY_SIZE;
1079      if (dyn_h->plt_offset < 0x2000)
1080	elf64_hppa_hash_table (x->info)->gp_offset = dyn_h->plt_offset;
1081    }
1082  else
1083    dyn_h->want_plt = 0;
1084
1085  return TRUE;
1086}
1087
1088/* Allocate space for a STUB entry.  */
1089
1090static bfd_boolean
1091allocate_global_data_stub (dyn_h, data)
1092     struct elf64_hppa_dyn_hash_entry *dyn_h;
1093     PTR data;
1094{
1095  struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1096
1097  if (dyn_h->want_stub
1098      && elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info)
1099      && !((dyn_h->h->root.type == bfd_link_hash_defined
1100	    || dyn_h->h->root.type == bfd_link_hash_defweak)
1101	   && dyn_h->h->root.u.def.section->output_section != NULL))
1102    {
1103      dyn_h->stub_offset = x->ofs;
1104      x->ofs += sizeof (plt_stub);
1105    }
1106  else
1107    dyn_h->want_stub = 0;
1108  return TRUE;
1109}
1110
1111/* Allocate space for a FPTR entry.  */
1112
1113static bfd_boolean
1114allocate_global_data_opd (dyn_h, data)
1115     struct elf64_hppa_dyn_hash_entry *dyn_h;
1116     PTR data;
1117{
1118  struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1119
1120  if (dyn_h->want_opd)
1121    {
1122      struct elf_link_hash_entry *h = dyn_h->h;
1123
1124      if (h)
1125	while (h->root.type == bfd_link_hash_indirect
1126	       || h->root.type == bfd_link_hash_warning)
1127	  h = (struct elf_link_hash_entry *) h->root.u.i.link;
1128
1129      /* We never need an opd entry for a symbol which is not
1130	 defined by this output file.  */
1131      if (h && (h->root.type == bfd_link_hash_undefined
1132		|| h->root.type == bfd_link_hash_undefweak
1133		|| h->root.u.def.section->output_section == NULL))
1134	dyn_h->want_opd = 0;
1135
1136      /* If we are creating a shared library, took the address of a local
1137	 function or might export this function from this object file, then
1138	 we have to create an opd descriptor.  */
1139      else if (x->info->shared
1140	       || h == NULL
1141	       || (h->dynindx == -1 && h->type != STT_PARISC_MILLI)
1142	       || (h->root.type == bfd_link_hash_defined
1143		   || h->root.type == bfd_link_hash_defweak))
1144	{
1145	  /* If we are creating a shared library, then we will have to
1146	     create a runtime relocation for the symbol to properly
1147	     initialize the .opd entry.  Make sure the symbol gets
1148	     added to the dynamic symbol table.  */
1149	  if (x->info->shared
1150	      && (h == NULL || (h->dynindx == -1)))
1151	    {
1152	      bfd *owner;
1153	      owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
1154
1155	      if (!bfd_elf_link_record_local_dynamic_symbol
1156		    (x->info, owner, dyn_h->sym_indx))
1157		return FALSE;
1158	    }
1159
1160	  /* This may not be necessary or desirable anymore now that
1161	     we have some support for dealing with section symbols
1162	     in dynamic relocs.  But name munging does make the result
1163	     much easier to debug.  ie, the EPLT reloc will reference
1164	     a symbol like .foobar, instead of .text + offset.  */
1165	  if (x->info->shared && h)
1166	    {
1167	      char *new_name;
1168	      struct elf_link_hash_entry *nh;
1169
1170	      new_name = alloca (strlen (h->root.root.string) + 2);
1171	      new_name[0] = '.';
1172	      strcpy (new_name + 1, h->root.root.string);
1173
1174	      nh = elf_link_hash_lookup (elf_hash_table (x->info),
1175					 new_name, TRUE, TRUE, TRUE);
1176
1177	      nh->root.type = h->root.type;
1178	      nh->root.u.def.value = h->root.u.def.value;
1179	      nh->root.u.def.section = h->root.u.def.section;
1180
1181	      if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
1182		return FALSE;
1183
1184	     }
1185	  dyn_h->opd_offset = x->ofs;
1186	  x->ofs += OPD_ENTRY_SIZE;
1187	}
1188
1189      /* Otherwise we do not need an opd entry.  */
1190      else
1191	dyn_h->want_opd = 0;
1192    }
1193  return TRUE;
1194}
1195
1196/* HP requires the EI_OSABI field to be filled in.  The assignment to
1197   EI_ABIVERSION may not be strictly necessary.  */
1198
1199static void
1200elf64_hppa_post_process_headers (abfd, link_info)
1201     bfd * abfd;
1202     struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
1203{
1204  Elf_Internal_Ehdr * i_ehdrp;
1205
1206  i_ehdrp = elf_elfheader (abfd);
1207
1208  if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
1209    {
1210      i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
1211    }
1212  else
1213    {
1214      i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
1215      i_ehdrp->e_ident[EI_ABIVERSION] = 1;
1216    }
1217}
1218
1219/* Create function descriptor section (.opd).  This section is called .opd
1220   because it contains "official procedure descriptors".  The "official"
1221   refers to the fact that these descriptors are used when taking the address
1222   of a procedure, thus ensuring a unique address for each procedure.  */
1223
1224static bfd_boolean
1225get_opd (abfd, info, hppa_info)
1226     bfd *abfd;
1227     struct bfd_link_info *info ATTRIBUTE_UNUSED;
1228     struct elf64_hppa_link_hash_table *hppa_info;
1229{
1230  asection *opd;
1231  bfd *dynobj;
1232
1233  opd = hppa_info->opd_sec;
1234  if (!opd)
1235    {
1236      dynobj = hppa_info->root.dynobj;
1237      if (!dynobj)
1238	hppa_info->root.dynobj = dynobj = abfd;
1239
1240      opd = bfd_make_section_with_flags (dynobj, ".opd",
1241					 (SEC_ALLOC
1242					  | SEC_LOAD
1243					  | SEC_HAS_CONTENTS
1244					  | SEC_IN_MEMORY
1245					  | SEC_LINKER_CREATED));
1246      if (!opd
1247	  || !bfd_set_section_alignment (abfd, opd, 3))
1248	{
1249	  BFD_ASSERT (0);
1250	  return FALSE;
1251	}
1252
1253      hppa_info->opd_sec = opd;
1254    }
1255
1256  return TRUE;
1257}
1258
1259/* Create the PLT section.  */
1260
1261static bfd_boolean
1262get_plt (abfd, info, hppa_info)
1263     bfd *abfd;
1264     struct bfd_link_info *info ATTRIBUTE_UNUSED;
1265     struct elf64_hppa_link_hash_table *hppa_info;
1266{
1267  asection *plt;
1268  bfd *dynobj;
1269
1270  plt = hppa_info->plt_sec;
1271  if (!plt)
1272    {
1273      dynobj = hppa_info->root.dynobj;
1274      if (!dynobj)
1275	hppa_info->root.dynobj = dynobj = abfd;
1276
1277      plt = bfd_make_section_with_flags (dynobj, ".plt",
1278					 (SEC_ALLOC
1279					  | SEC_LOAD
1280					  | SEC_HAS_CONTENTS
1281					  | SEC_IN_MEMORY
1282					  | SEC_LINKER_CREATED));
1283      if (!plt
1284	  || !bfd_set_section_alignment (abfd, plt, 3))
1285	{
1286	  BFD_ASSERT (0);
1287	  return FALSE;
1288	}
1289
1290      hppa_info->plt_sec = plt;
1291    }
1292
1293  return TRUE;
1294}
1295
1296/* Create the DLT section.  */
1297
1298static bfd_boolean
1299get_dlt (abfd, info, hppa_info)
1300     bfd *abfd;
1301     struct bfd_link_info *info ATTRIBUTE_UNUSED;
1302     struct elf64_hppa_link_hash_table *hppa_info;
1303{
1304  asection *dlt;
1305  bfd *dynobj;
1306
1307  dlt = hppa_info->dlt_sec;
1308  if (!dlt)
1309    {
1310      dynobj = hppa_info->root.dynobj;
1311      if (!dynobj)
1312	hppa_info->root.dynobj = dynobj = abfd;
1313
1314      dlt = bfd_make_section_with_flags (dynobj, ".dlt",
1315					 (SEC_ALLOC
1316					  | SEC_LOAD
1317					  | SEC_HAS_CONTENTS
1318					  | SEC_IN_MEMORY
1319					  | SEC_LINKER_CREATED));
1320      if (!dlt
1321	  || !bfd_set_section_alignment (abfd, dlt, 3))
1322	{
1323	  BFD_ASSERT (0);
1324	  return FALSE;
1325	}
1326
1327      hppa_info->dlt_sec = dlt;
1328    }
1329
1330  return TRUE;
1331}
1332
1333/* Create the stubs section.  */
1334
1335static bfd_boolean
1336get_stub (abfd, info, hppa_info)
1337     bfd *abfd;
1338     struct bfd_link_info *info ATTRIBUTE_UNUSED;
1339     struct elf64_hppa_link_hash_table *hppa_info;
1340{
1341  asection *stub;
1342  bfd *dynobj;
1343
1344  stub = hppa_info->stub_sec;
1345  if (!stub)
1346    {
1347      dynobj = hppa_info->root.dynobj;
1348      if (!dynobj)
1349	hppa_info->root.dynobj = dynobj = abfd;
1350
1351      stub = bfd_make_section_with_flags (dynobj, ".stub",
1352					  (SEC_ALLOC | SEC_LOAD
1353					   | SEC_HAS_CONTENTS
1354					   | SEC_IN_MEMORY
1355					   | SEC_READONLY
1356					   | SEC_LINKER_CREATED));
1357      if (!stub
1358	  || !bfd_set_section_alignment (abfd, stub, 3))
1359	{
1360	  BFD_ASSERT (0);
1361	  return FALSE;
1362	}
1363
1364      hppa_info->stub_sec = stub;
1365    }
1366
1367  return TRUE;
1368}
1369
1370/* Create sections necessary for dynamic linking.  This is only a rough
1371   cut and will likely change as we learn more about the somewhat
1372   unusual dynamic linking scheme HP uses.
1373
1374   .stub:
1375	Contains code to implement cross-space calls.  The first time one
1376	of the stubs is used it will call into the dynamic linker, later
1377	calls will go straight to the target.
1378
1379	The only stub we support right now looks like
1380
1381	ldd OFFSET(%dp),%r1
1382	bve %r0(%r1)
1383	ldd OFFSET+8(%dp),%dp
1384
1385	Other stubs may be needed in the future.  We may want the remove
1386	the break/nop instruction.  It is only used right now to keep the
1387	offset of a .plt entry and a .stub entry in sync.
1388
1389   .dlt:
1390	This is what most people call the .got.  HP used a different name.
1391	Losers.
1392
1393   .rela.dlt:
1394	Relocations for the DLT.
1395
1396   .plt:
1397	Function pointers as address,gp pairs.
1398
1399   .rela.plt:
1400	Should contain dynamic IPLT (and EPLT?) relocations.
1401
1402   .opd:
1403	FPTRS
1404
1405   .rela.opd:
1406	EPLT relocations for symbols exported from shared libraries.  */
1407
1408static bfd_boolean
1409elf64_hppa_create_dynamic_sections (abfd, info)
1410     bfd *abfd;
1411     struct bfd_link_info *info;
1412{
1413  asection *s;
1414
1415  if (! get_stub (abfd, info, elf64_hppa_hash_table (info)))
1416    return FALSE;
1417
1418  if (! get_dlt (abfd, info, elf64_hppa_hash_table (info)))
1419    return FALSE;
1420
1421  if (! get_plt (abfd, info, elf64_hppa_hash_table (info)))
1422    return FALSE;
1423
1424  if (! get_opd (abfd, info, elf64_hppa_hash_table (info)))
1425    return FALSE;
1426
1427  s = bfd_make_section_with_flags (abfd, ".rela.dlt",
1428				   (SEC_ALLOC | SEC_LOAD
1429				    | SEC_HAS_CONTENTS
1430				    | SEC_IN_MEMORY
1431				    | SEC_READONLY
1432				    | SEC_LINKER_CREATED));
1433  if (s == NULL
1434      || !bfd_set_section_alignment (abfd, s, 3))
1435    return FALSE;
1436  elf64_hppa_hash_table (info)->dlt_rel_sec = s;
1437
1438  s = bfd_make_section_with_flags (abfd, ".rela.plt",
1439				   (SEC_ALLOC | SEC_LOAD
1440				    | SEC_HAS_CONTENTS
1441				    | SEC_IN_MEMORY
1442				    | SEC_READONLY
1443				    | SEC_LINKER_CREATED));
1444  if (s == NULL
1445      || !bfd_set_section_alignment (abfd, s, 3))
1446    return FALSE;
1447  elf64_hppa_hash_table (info)->plt_rel_sec = s;
1448
1449  s = bfd_make_section_with_flags (abfd, ".rela.data",
1450				   (SEC_ALLOC | SEC_LOAD
1451				    | SEC_HAS_CONTENTS
1452				    | SEC_IN_MEMORY
1453				    | SEC_READONLY
1454				    | SEC_LINKER_CREATED));
1455  if (s == NULL
1456      || !bfd_set_section_alignment (abfd, s, 3))
1457    return FALSE;
1458  elf64_hppa_hash_table (info)->other_rel_sec = s;
1459
1460  s = bfd_make_section_with_flags (abfd, ".rela.opd",
1461				   (SEC_ALLOC | SEC_LOAD
1462				    | SEC_HAS_CONTENTS
1463				    | SEC_IN_MEMORY
1464				    | SEC_READONLY
1465				    | SEC_LINKER_CREATED));
1466  if (s == NULL
1467      || !bfd_set_section_alignment (abfd, s, 3))
1468    return FALSE;
1469  elf64_hppa_hash_table (info)->opd_rel_sec = s;
1470
1471  return TRUE;
1472}
1473
1474/* Allocate dynamic relocations for those symbols that turned out
1475   to be dynamic.  */
1476
1477static bfd_boolean
1478allocate_dynrel_entries (dyn_h, data)
1479     struct elf64_hppa_dyn_hash_entry *dyn_h;
1480     PTR data;
1481{
1482  struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1483  struct elf64_hppa_link_hash_table *hppa_info;
1484  struct elf64_hppa_dyn_reloc_entry *rent;
1485  bfd_boolean dynamic_symbol, shared;
1486
1487  hppa_info = elf64_hppa_hash_table (x->info);
1488  dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info);
1489  shared = x->info->shared;
1490
1491  /* We may need to allocate relocations for a non-dynamic symbol
1492     when creating a shared library.  */
1493  if (!dynamic_symbol && !shared)
1494    return TRUE;
1495
1496  /* Take care of the normal data relocations.  */
1497
1498  for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
1499    {
1500      /* Allocate one iff we are building a shared library, the relocation
1501	 isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
1502      if (!shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
1503	continue;
1504
1505      hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
1506
1507      /* Make sure this symbol gets into the dynamic symbol table if it is
1508	 not already recorded.  ?!? This should not be in the loop since
1509	 the symbol need only be added once.  */
1510      if (dyn_h->h == 0
1511	  || (dyn_h->h->dynindx == -1 && dyn_h->h->type != STT_PARISC_MILLI))
1512	if (!bfd_elf_link_record_local_dynamic_symbol
1513	    (x->info, rent->sec->owner, dyn_h->sym_indx))
1514	  return FALSE;
1515    }
1516
1517  /* Take care of the GOT and PLT relocations.  */
1518
1519  if ((dynamic_symbol || shared) && dyn_h->want_dlt)
1520    hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
1521
1522  /* If we are building a shared library, then every symbol that has an
1523     opd entry will need an EPLT relocation to relocate the symbol's address
1524     and __gp value based on the runtime load address.  */
1525  if (shared && dyn_h->want_opd)
1526    hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
1527
1528  if (dyn_h->want_plt && dynamic_symbol)
1529    {
1530      bfd_size_type t = 0;
1531
1532      /* Dynamic symbols get one IPLT relocation.  Local symbols in
1533	 shared libraries get two REL relocations.  Local symbols in
1534	 main applications get nothing.  */
1535      if (dynamic_symbol)
1536	t = sizeof (Elf64_External_Rela);
1537      else if (shared)
1538	t = 2 * sizeof (Elf64_External_Rela);
1539
1540      hppa_info->plt_rel_sec->size += t;
1541    }
1542
1543  return TRUE;
1544}
1545
1546/* Adjust a symbol defined by a dynamic object and referenced by a
1547   regular object.  */
1548
1549static bfd_boolean
1550elf64_hppa_adjust_dynamic_symbol (info, h)
1551     struct bfd_link_info *info ATTRIBUTE_UNUSED;
1552     struct elf_link_hash_entry *h;
1553{
1554  /* ??? Undefined symbols with PLT entries should be re-defined
1555     to be the PLT entry.  */
1556
1557  /* If this is a weak symbol, and there is a real definition, the
1558     processor independent code will have arranged for us to see the
1559     real definition first, and we can just use the same value.  */
1560  if (h->u.weakdef != NULL)
1561    {
1562      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1563		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
1564      h->root.u.def.section = h->u.weakdef->root.u.def.section;
1565      h->root.u.def.value = h->u.weakdef->root.u.def.value;
1566      return TRUE;
1567    }
1568
1569  /* If this is a reference to a symbol defined by a dynamic object which
1570     is not a function, we might allocate the symbol in our .dynbss section
1571     and allocate a COPY dynamic relocation.
1572
1573     But PA64 code is canonically PIC, so as a rule we can avoid this sort
1574     of hackery.  */
1575
1576  return TRUE;
1577}
1578
1579/* This function is called via elf_link_hash_traverse to mark millicode
1580   symbols with a dynindx of -1 and to remove the string table reference
1581   from the dynamic symbol table.  If the symbol is not a millicode symbol,
1582   elf64_hppa_mark_exported_functions is called.  */
1583
1584static bfd_boolean
1585elf64_hppa_mark_milli_and_exported_functions (h, data)
1586     struct elf_link_hash_entry *h;
1587     PTR data;
1588{
1589  struct bfd_link_info *info = (struct bfd_link_info *)data;
1590  struct elf_link_hash_entry *elf = h;
1591
1592  if (elf->root.type == bfd_link_hash_warning)
1593    elf = (struct elf_link_hash_entry *) elf->root.u.i.link;
1594
1595  if (elf->type == STT_PARISC_MILLI)
1596    {
1597      if (elf->dynindx != -1)
1598	{
1599	  elf->dynindx = -1;
1600	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1601				  elf->dynstr_index);
1602	}
1603      return TRUE;
1604    }
1605
1606  return elf64_hppa_mark_exported_functions (h, data);
1607}
1608
1609/* Set the final sizes of the dynamic sections and allocate memory for
1610   the contents of our special sections.  */
1611
1612static bfd_boolean
1613elf64_hppa_size_dynamic_sections (output_bfd, info)
1614     bfd *output_bfd;
1615     struct bfd_link_info *info;
1616{
1617  bfd *dynobj;
1618  asection *s;
1619  bfd_boolean plt;
1620  bfd_boolean relocs;
1621  bfd_boolean reltext;
1622  struct elf64_hppa_allocate_data data;
1623  struct elf64_hppa_link_hash_table *hppa_info;
1624
1625  hppa_info = elf64_hppa_hash_table (info);
1626
1627  dynobj = elf_hash_table (info)->dynobj;
1628  BFD_ASSERT (dynobj != NULL);
1629
1630  /* Mark each function this program exports so that we will allocate
1631     space in the .opd section for each function's FPTR.  If we are
1632     creating dynamic sections, change the dynamic index of millicode
1633     symbols to -1 and remove them from the string table for .dynstr.
1634
1635     We have to traverse the main linker hash table since we have to
1636     find functions which may not have been mentioned in any relocs.  */
1637  elf_link_hash_traverse (elf_hash_table (info),
1638			  (elf_hash_table (info)->dynamic_sections_created
1639			   ? elf64_hppa_mark_milli_and_exported_functions
1640			   : elf64_hppa_mark_exported_functions),
1641			  info);
1642
1643  if (elf_hash_table (info)->dynamic_sections_created)
1644    {
1645      /* Set the contents of the .interp section to the interpreter.  */
1646      if (info->executable)
1647	{
1648	  s = bfd_get_section_by_name (dynobj, ".interp");
1649	  BFD_ASSERT (s != NULL);
1650	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1651	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1652	}
1653    }
1654  else
1655    {
1656      /* We may have created entries in the .rela.got section.
1657	 However, if we are not creating the dynamic sections, we will
1658	 not actually use these entries.  Reset the size of .rela.dlt,
1659	 which will cause it to get stripped from the output file
1660	 below.  */
1661      s = bfd_get_section_by_name (dynobj, ".rela.dlt");
1662      if (s != NULL)
1663	s->size = 0;
1664    }
1665
1666  /* Allocate the GOT entries.  */
1667
1668  data.info = info;
1669  if (elf64_hppa_hash_table (info)->dlt_sec)
1670    {
1671      data.ofs = 0x0;
1672      elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1673				    allocate_global_data_dlt, &data);
1674      hppa_info->dlt_sec->size = data.ofs;
1675
1676      data.ofs = 0x0;
1677      elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1678				    allocate_global_data_plt, &data);
1679      hppa_info->plt_sec->size = data.ofs;
1680
1681      data.ofs = 0x0;
1682      elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1683				    allocate_global_data_stub, &data);
1684      hppa_info->stub_sec->size = data.ofs;
1685    }
1686
1687  /* Allocate space for entries in the .opd section.  */
1688  if (elf64_hppa_hash_table (info)->opd_sec)
1689    {
1690      data.ofs = 0;
1691      elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1692				    allocate_global_data_opd, &data);
1693      hppa_info->opd_sec->size = data.ofs;
1694    }
1695
1696  /* Now allocate space for dynamic relocations, if necessary.  */
1697  if (hppa_info->root.dynamic_sections_created)
1698    elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1699				  allocate_dynrel_entries, &data);
1700
1701  /* The sizes of all the sections are set.  Allocate memory for them.  */
1702  plt = FALSE;
1703  relocs = FALSE;
1704  reltext = FALSE;
1705  for (s = dynobj->sections; s != NULL; s = s->next)
1706    {
1707      const char *name;
1708
1709      if ((s->flags & SEC_LINKER_CREATED) == 0)
1710	continue;
1711
1712      /* It's OK to base decisions on the section name, because none
1713	 of the dynobj section names depend upon the input files.  */
1714      name = bfd_get_section_name (dynobj, s);
1715
1716      if (strcmp (name, ".plt") == 0)
1717	{
1718	  /* Remember whether there is a PLT.  */
1719	  plt = s->size != 0;
1720	}
1721      else if (strcmp (name, ".opd") == 0
1722	       || strncmp (name, ".dlt", 4) == 0
1723	       || strcmp (name, ".stub") == 0
1724	       || strcmp (name, ".got") == 0)
1725	{
1726	  /* Strip this section if we don't need it; see the comment below.  */
1727	}
1728      else if (strncmp (name, ".rela", 5) == 0)
1729	{
1730	  if (s->size != 0)
1731	    {
1732	      asection *target;
1733
1734	      /* Remember whether there are any reloc sections other
1735		 than .rela.plt.  */
1736	      if (strcmp (name, ".rela.plt") != 0)
1737		{
1738		  const char *outname;
1739
1740		  relocs = TRUE;
1741
1742		  /* If this relocation section applies to a read only
1743		     section, then we probably need a DT_TEXTREL
1744		     entry.  The entries in the .rela.plt section
1745		     really apply to the .got section, which we
1746		     created ourselves and so know is not readonly.  */
1747		  outname = bfd_get_section_name (output_bfd,
1748						  s->output_section);
1749		  target = bfd_get_section_by_name (output_bfd, outname + 4);
1750		  if (target != NULL
1751		      && (target->flags & SEC_READONLY) != 0
1752		      && (target->flags & SEC_ALLOC) != 0)
1753		    reltext = TRUE;
1754		}
1755
1756	      /* We use the reloc_count field as a counter if we need
1757		 to copy relocs into the output file.  */
1758	      s->reloc_count = 0;
1759	    }
1760	}
1761      else
1762	{
1763	  /* It's not one of our sections, so don't allocate space.  */
1764	  continue;
1765	}
1766
1767      if (s->size == 0)
1768	{
1769	  /* If we don't need this section, strip it from the
1770	     output file.  This is mostly to handle .rela.bss and
1771	     .rela.plt.  We must create both sections in
1772	     create_dynamic_sections, because they must be created
1773	     before the linker maps input sections to output
1774	     sections.  The linker does that before
1775	     adjust_dynamic_symbol is called, and it is that
1776	     function which decides whether anything needs to go
1777	     into these sections.  */
1778	  s->flags |= SEC_EXCLUDE;
1779	  continue;
1780	}
1781
1782      if ((s->flags & SEC_HAS_CONTENTS) == 0)
1783	continue;
1784
1785      /* Allocate memory for the section contents if it has not
1786	 been allocated already.  We use bfd_zalloc here in case
1787	 unused entries are not reclaimed before the section's
1788	 contents are written out.  This should not happen, but this
1789	 way if it does, we get a R_PARISC_NONE reloc instead of
1790	 garbage.  */
1791      if (s->contents == NULL)
1792	{
1793	  s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1794	  if (s->contents == NULL)
1795	    return FALSE;
1796	}
1797    }
1798
1799  if (elf_hash_table (info)->dynamic_sections_created)
1800    {
1801      /* Always create a DT_PLTGOT.  It actually has nothing to do with
1802	 the PLT, it is how we communicate the __gp value of a load
1803	 module to the dynamic linker.  */
1804#define add_dynamic_entry(TAG, VAL) \
1805  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1806
1807      if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0)
1808	  || !add_dynamic_entry (DT_PLTGOT, 0))
1809	return FALSE;
1810
1811      /* Add some entries to the .dynamic section.  We fill in the
1812	 values later, in elf64_hppa_finish_dynamic_sections, but we
1813	 must add the entries now so that we get the correct size for
1814	 the .dynamic section.  The DT_DEBUG entry is filled in by the
1815	 dynamic linker and used by the debugger.  */
1816      if (! info->shared)
1817	{
1818	  if (!add_dynamic_entry (DT_DEBUG, 0)
1819	      || !add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1820	      || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
1821	    return FALSE;
1822	}
1823
1824      /* Force DT_FLAGS to always be set.
1825	 Required by HPUX 11.00 patch PHSS_26559.  */
1826      if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1827	return FALSE;
1828
1829      if (plt)
1830	{
1831	  if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1832	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1833	      || !add_dynamic_entry (DT_JMPREL, 0))
1834	    return FALSE;
1835	}
1836
1837      if (relocs)
1838	{
1839	  if (!add_dynamic_entry (DT_RELA, 0)
1840	      || !add_dynamic_entry (DT_RELASZ, 0)
1841	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1842	    return FALSE;
1843	}
1844
1845      if (reltext)
1846	{
1847	  if (!add_dynamic_entry (DT_TEXTREL, 0))
1848	    return FALSE;
1849	  info->flags |= DF_TEXTREL;
1850	}
1851    }
1852#undef add_dynamic_entry
1853
1854  return TRUE;
1855}
1856
1857/* Called after we have output the symbol into the dynamic symbol
1858   table, but before we output the symbol into the normal symbol
1859   table.
1860
1861   For some symbols we had to change their address when outputting
1862   the dynamic symbol table.  We undo that change here so that
1863   the symbols have their expected value in the normal symbol
1864   table.  Ick.  */
1865
1866static bfd_boolean
1867elf64_hppa_link_output_symbol_hook (info, name, sym, input_sec, h)
1868     struct bfd_link_info *info;
1869     const char *name;
1870     Elf_Internal_Sym *sym;
1871     asection *input_sec ATTRIBUTE_UNUSED;
1872     struct elf_link_hash_entry *h;
1873{
1874  struct elf64_hppa_link_hash_table *hppa_info;
1875  struct elf64_hppa_dyn_hash_entry *dyn_h;
1876
1877  /* We may be called with the file symbol or section symbols.
1878     They never need munging, so it is safe to ignore them.  */
1879  if (!name)
1880    return TRUE;
1881
1882  /* Get the PA dyn_symbol (if any) associated with NAME.  */
1883  hppa_info = elf64_hppa_hash_table (info);
1884  dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
1885				      name, FALSE, FALSE);
1886  if (!dyn_h || dyn_h->h != h)
1887    return TRUE;
1888
1889  /* Function symbols for which we created .opd entries *may* have been
1890     munged by finish_dynamic_symbol and have to be un-munged here.
1891
1892     Note that finish_dynamic_symbol sometimes turns dynamic symbols
1893     into non-dynamic ones, so we initialize st_shndx to -1 in
1894     mark_exported_functions and check to see if it was overwritten
1895     here instead of just checking dyn_h->h->dynindx.  */
1896  if (dyn_h->want_opd && dyn_h->st_shndx != -1)
1897    {
1898      /* Restore the saved value and section index.  */
1899      sym->st_value = dyn_h->st_value;
1900      sym->st_shndx = dyn_h->st_shndx;
1901    }
1902
1903  return TRUE;
1904}
1905
1906/* Finish up dynamic symbol handling.  We set the contents of various
1907   dynamic sections here.  */
1908
1909static bfd_boolean
1910elf64_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
1911     bfd *output_bfd;
1912     struct bfd_link_info *info;
1913     struct elf_link_hash_entry *h;
1914     Elf_Internal_Sym *sym;
1915{
1916  asection *stub, *splt, *sdlt, *sopd, *spltrel, *sdltrel;
1917  struct elf64_hppa_link_hash_table *hppa_info;
1918  struct elf64_hppa_dyn_hash_entry *dyn_h;
1919
1920  hppa_info = elf64_hppa_hash_table (info);
1921  dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
1922				      h->root.root.string, FALSE, FALSE);
1923
1924  stub = hppa_info->stub_sec;
1925  splt = hppa_info->plt_sec;
1926  sdlt = hppa_info->dlt_sec;
1927  sopd = hppa_info->opd_sec;
1928  spltrel = hppa_info->plt_rel_sec;
1929  sdltrel = hppa_info->dlt_rel_sec;
1930
1931  /* Incredible.  It is actually necessary to NOT use the symbol's real
1932     value when building the dynamic symbol table for a shared library.
1933     At least for symbols that refer to functions.
1934
1935     We will store a new value and section index into the symbol long
1936     enough to output it into the dynamic symbol table, then we restore
1937     the original values (in elf64_hppa_link_output_symbol_hook).  */
1938  if (dyn_h && dyn_h->want_opd)
1939    {
1940      BFD_ASSERT (sopd != NULL);
1941
1942      /* Save away the original value and section index so that we
1943	 can restore them later.  */
1944      dyn_h->st_value = sym->st_value;
1945      dyn_h->st_shndx = sym->st_shndx;
1946
1947      /* For the dynamic symbol table entry, we want the value to be
1948	 address of this symbol's entry within the .opd section.  */
1949      sym->st_value = (dyn_h->opd_offset
1950		       + sopd->output_offset
1951		       + sopd->output_section->vma);
1952      sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1953							 sopd->output_section);
1954    }
1955
1956  /* Initialize a .plt entry if requested.  */
1957  if (dyn_h && dyn_h->want_plt
1958      && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
1959    {
1960      bfd_vma value;
1961      Elf_Internal_Rela rel;
1962      bfd_byte *loc;
1963
1964      BFD_ASSERT (splt != NULL && spltrel != NULL);
1965
1966      /* We do not actually care about the value in the PLT entry
1967	 if we are creating a shared library and the symbol is
1968	 still undefined, we create a dynamic relocation to fill
1969	 in the correct value.  */
1970      if (info->shared && h->root.type == bfd_link_hash_undefined)
1971	value = 0;
1972      else
1973	value = (h->root.u.def.value + h->root.u.def.section->vma);
1974
1975      /* Fill in the entry in the procedure linkage table.
1976
1977	 The format of a plt entry is
1978	 <funcaddr> <__gp>.
1979
1980	 plt_offset is the offset within the PLT section at which to
1981	 install the PLT entry.
1982
1983	 We are modifying the in-memory PLT contents here, so we do not add
1984	 in the output_offset of the PLT section.  */
1985
1986      bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset);
1987      value = _bfd_get_gp_value (splt->output_section->owner);
1988      bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset + 0x8);
1989
1990      /* Create a dynamic IPLT relocation for this entry.
1991
1992	 We are creating a relocation in the output file's PLT section,
1993	 which is included within the DLT secton.  So we do need to include
1994	 the PLT's output_offset in the computation of the relocation's
1995	 address.  */
1996      rel.r_offset = (dyn_h->plt_offset + splt->output_offset
1997		      + splt->output_section->vma);
1998      rel.r_info = ELF64_R_INFO (h->dynindx, R_PARISC_IPLT);
1999      rel.r_addend = 0;
2000
2001      loc = spltrel->contents;
2002      loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2003      bfd_elf64_swap_reloca_out (splt->output_section->owner, &rel, loc);
2004    }
2005
2006  /* Initialize an external call stub entry if requested.  */
2007  if (dyn_h && dyn_h->want_stub
2008      && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
2009    {
2010      bfd_vma value;
2011      int insn;
2012      unsigned int max_offset;
2013
2014      BFD_ASSERT (stub != NULL);
2015
2016      /* Install the generic stub template.
2017
2018	 We are modifying the contents of the stub section, so we do not
2019	 need to include the stub section's output_offset here.  */
2020      memcpy (stub->contents + dyn_h->stub_offset, plt_stub, sizeof (plt_stub));
2021
2022      /* Fix up the first ldd instruction.
2023
2024	 We are modifying the contents of the STUB section in memory,
2025	 so we do not need to include its output offset in this computation.
2026
2027	 Note the plt_offset value is the value of the PLT entry relative to
2028	 the start of the PLT section.  These instructions will reference
2029	 data relative to the value of __gp, which may not necessarily have
2030	 the same address as the start of the PLT section.
2031
2032	 gp_offset contains the offset of __gp within the PLT section.  */
2033      value = dyn_h->plt_offset - hppa_info->gp_offset;
2034
2035      insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset);
2036      if (output_bfd->arch_info->mach >= 25)
2037	{
2038	  /* Wide mode allows 16 bit offsets.  */
2039	  max_offset = 32768;
2040	  insn &= ~ 0xfff1;
2041	  insn |= re_assemble_16 ((int) value);
2042	}
2043      else
2044	{
2045	  max_offset = 8192;
2046	  insn &= ~ 0x3ff1;
2047	  insn |= re_assemble_14 ((int) value);
2048	}
2049
2050      if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2051	{
2052	  (*_bfd_error_handler) (_("stub entry for %s cannot load .plt, dp offset = %ld"),
2053				 dyn_h->root.string,
2054				 (long) value);
2055	  return FALSE;
2056	}
2057
2058      bfd_put_32 (stub->owner, (bfd_vma) insn,
2059		  stub->contents + dyn_h->stub_offset);
2060
2061      /* Fix up the second ldd instruction.  */
2062      value += 8;
2063      insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset + 8);
2064      if (output_bfd->arch_info->mach >= 25)
2065	{
2066	  insn &= ~ 0xfff1;
2067	  insn |= re_assemble_16 ((int) value);
2068	}
2069      else
2070	{
2071	  insn &= ~ 0x3ff1;
2072	  insn |= re_assemble_14 ((int) value);
2073	}
2074      bfd_put_32 (stub->owner, (bfd_vma) insn,
2075		  stub->contents + dyn_h->stub_offset + 8);
2076    }
2077
2078  return TRUE;
2079}
2080
2081/* The .opd section contains FPTRs for each function this file
2082   exports.  Initialize the FPTR entries.  */
2083
2084static bfd_boolean
2085elf64_hppa_finalize_opd (dyn_h, data)
2086     struct elf64_hppa_dyn_hash_entry *dyn_h;
2087     PTR data;
2088{
2089  struct bfd_link_info *info = (struct bfd_link_info *)data;
2090  struct elf64_hppa_link_hash_table *hppa_info;
2091  struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
2092  asection *sopd;
2093  asection *sopdrel;
2094
2095  hppa_info = elf64_hppa_hash_table (info);
2096  sopd = hppa_info->opd_sec;
2097  sopdrel = hppa_info->opd_rel_sec;
2098
2099  if (h && dyn_h->want_opd)
2100    {
2101      bfd_vma value;
2102
2103      /* The first two words of an .opd entry are zero.
2104
2105	 We are modifying the contents of the OPD section in memory, so we
2106	 do not need to include its output offset in this computation.  */
2107      memset (sopd->contents + dyn_h->opd_offset, 0, 16);
2108
2109      value = (h->root.u.def.value
2110	       + h->root.u.def.section->output_section->vma
2111	       + h->root.u.def.section->output_offset);
2112
2113      /* The next word is the address of the function.  */
2114      bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 16);
2115
2116      /* The last word is our local __gp value.  */
2117      value = _bfd_get_gp_value (sopd->output_section->owner);
2118      bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 24);
2119    }
2120
2121  /* If we are generating a shared library, we must generate EPLT relocations
2122     for each entry in the .opd, even for static functions (they may have
2123     had their address taken).  */
2124  if (info->shared && dyn_h && dyn_h->want_opd)
2125    {
2126      Elf_Internal_Rela rel;
2127      bfd_byte *loc;
2128      int dynindx;
2129
2130      /* We may need to do a relocation against a local symbol, in
2131	 which case we have to look up it's dynamic symbol index off
2132	 the local symbol hash table.  */
2133      if (h && h->dynindx != -1)
2134	dynindx = h->dynindx;
2135      else
2136	dynindx
2137	  = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2138						dyn_h->sym_indx);
2139
2140      /* The offset of this relocation is the absolute address of the
2141	 .opd entry for this symbol.  */
2142      rel.r_offset = (dyn_h->opd_offset + sopd->output_offset
2143		      + sopd->output_section->vma);
2144
2145      /* If H is non-null, then we have an external symbol.
2146
2147	 It is imperative that we use a different dynamic symbol for the
2148	 EPLT relocation if the symbol has global scope.
2149
2150	 In the dynamic symbol table, the function symbol will have a value
2151	 which is address of the function's .opd entry.
2152
2153	 Thus, we can not use that dynamic symbol for the EPLT relocation
2154	 (if we did, the data in the .opd would reference itself rather
2155	 than the actual address of the function).  Instead we have to use
2156	 a new dynamic symbol which has the same value as the original global
2157	 function symbol.
2158
2159	 We prefix the original symbol with a "." and use the new symbol in
2160	 the EPLT relocation.  This new symbol has already been recorded in
2161	 the symbol table, we just have to look it up and use it.
2162
2163	 We do not have such problems with static functions because we do
2164	 not make their addresses in the dynamic symbol table point to
2165	 the .opd entry.  Ultimately this should be safe since a static
2166	 function can not be directly referenced outside of its shared
2167	 library.
2168
2169	 We do have to play similar games for FPTR relocations in shared
2170	 libraries, including those for static symbols.  See the FPTR
2171	 handling in elf64_hppa_finalize_dynreloc.  */
2172      if (h)
2173	{
2174	  char *new_name;
2175	  struct elf_link_hash_entry *nh;
2176
2177	  new_name = alloca (strlen (h->root.root.string) + 2);
2178	  new_name[0] = '.';
2179	  strcpy (new_name + 1, h->root.root.string);
2180
2181	  nh = elf_link_hash_lookup (elf_hash_table (info),
2182				     new_name, FALSE, FALSE, FALSE);
2183
2184	  /* All we really want from the new symbol is its dynamic
2185	     symbol index.  */
2186	  dynindx = nh->dynindx;
2187	}
2188
2189      rel.r_addend = 0;
2190      rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2191
2192      loc = sopdrel->contents;
2193      loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2194      bfd_elf64_swap_reloca_out (sopd->output_section->owner, &rel, loc);
2195    }
2196  return TRUE;
2197}
2198
2199/* The .dlt section contains addresses for items referenced through the
2200   dlt.  Note that we can have a DLTIND relocation for a local symbol, thus
2201   we can not depend on finish_dynamic_symbol to initialize the .dlt.  */
2202
2203static bfd_boolean
2204elf64_hppa_finalize_dlt (dyn_h, data)
2205     struct elf64_hppa_dyn_hash_entry *dyn_h;
2206     PTR data;
2207{
2208  struct bfd_link_info *info = (struct bfd_link_info *)data;
2209  struct elf64_hppa_link_hash_table *hppa_info;
2210  asection *sdlt, *sdltrel;
2211  struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
2212
2213  hppa_info = elf64_hppa_hash_table (info);
2214
2215  sdlt = hppa_info->dlt_sec;
2216  sdltrel = hppa_info->dlt_rel_sec;
2217
2218  /* H/DYN_H may refer to a local variable and we know it's
2219     address, so there is no need to create a relocation.  Just install
2220     the proper value into the DLT, note this shortcut can not be
2221     skipped when building a shared library.  */
2222  if (! info->shared && h && dyn_h->want_dlt)
2223    {
2224      bfd_vma value;
2225
2226      /* If we had an LTOFF_FPTR style relocation we want the DLT entry
2227	 to point to the FPTR entry in the .opd section.
2228
2229	 We include the OPD's output offset in this computation as
2230	 we are referring to an absolute address in the resulting
2231	 object file.  */
2232      if (dyn_h->want_opd)
2233	{
2234	  value = (dyn_h->opd_offset
2235		   + hppa_info->opd_sec->output_offset
2236		   + hppa_info->opd_sec->output_section->vma);
2237	}
2238      else if ((h->root.type == bfd_link_hash_defined
2239		|| h->root.type == bfd_link_hash_defweak)
2240	       && h->root.u.def.section)
2241	{
2242	  value = h->root.u.def.value + h->root.u.def.section->output_offset;
2243	  if (h->root.u.def.section->output_section)
2244	    value += h->root.u.def.section->output_section->vma;
2245	  else
2246	    value += h->root.u.def.section->vma;
2247	}
2248      else
2249	/* We have an undefined function reference.  */
2250	value = 0;
2251
2252      /* We do not need to include the output offset of the DLT section
2253	 here because we are modifying the in-memory contents.  */
2254      bfd_put_64 (sdlt->owner, value, sdlt->contents + dyn_h->dlt_offset);
2255    }
2256
2257  /* Create a relocation for the DLT entry associated with this symbol.
2258     When building a shared library the symbol does not have to be dynamic.  */
2259  if (dyn_h->want_dlt
2260      && (elf64_hppa_dynamic_symbol_p (dyn_h->h, info) || info->shared))
2261    {
2262      Elf_Internal_Rela rel;
2263      bfd_byte *loc;
2264      int dynindx;
2265
2266      /* We may need to do a relocation against a local symbol, in
2267	 which case we have to look up it's dynamic symbol index off
2268	 the local symbol hash table.  */
2269      if (h && h->dynindx != -1)
2270	dynindx = h->dynindx;
2271      else
2272	dynindx
2273	  = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2274						dyn_h->sym_indx);
2275
2276      /* Create a dynamic relocation for this entry.  Do include the output
2277	 offset of the DLT entry since we need an absolute address in the
2278	 resulting object file.  */
2279      rel.r_offset = (dyn_h->dlt_offset + sdlt->output_offset
2280		      + sdlt->output_section->vma);
2281      if (h && h->type == STT_FUNC)
2282	  rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2283      else
2284	  rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2285      rel.r_addend = 0;
2286
2287      loc = sdltrel->contents;
2288      loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2289      bfd_elf64_swap_reloca_out (sdlt->output_section->owner, &rel, loc);
2290    }
2291  return TRUE;
2292}
2293
2294/* Finalize the dynamic relocations.  Specifically the FPTR relocations
2295   for dynamic functions used to initialize static data.  */
2296
2297static bfd_boolean
2298elf64_hppa_finalize_dynreloc (dyn_h, data)
2299     struct elf64_hppa_dyn_hash_entry *dyn_h;
2300     PTR data;
2301{
2302  struct bfd_link_info *info = (struct bfd_link_info *)data;
2303  struct elf64_hppa_link_hash_table *hppa_info;
2304  struct elf_link_hash_entry *h;
2305  int dynamic_symbol;
2306
2307  dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, info);
2308
2309  if (!dynamic_symbol && !info->shared)
2310    return TRUE;
2311
2312  if (dyn_h->reloc_entries)
2313    {
2314      struct elf64_hppa_dyn_reloc_entry *rent;
2315      int dynindx;
2316
2317      hppa_info = elf64_hppa_hash_table (info);
2318      h = dyn_h->h;
2319
2320      /* We may need to do a relocation against a local symbol, in
2321	 which case we have to look up it's dynamic symbol index off
2322	 the local symbol hash table.  */
2323      if (h && h->dynindx != -1)
2324	dynindx = h->dynindx;
2325      else
2326	dynindx
2327	  = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2328						dyn_h->sym_indx);
2329
2330      for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
2331	{
2332	  Elf_Internal_Rela rel;
2333	  bfd_byte *loc;
2334
2335	  /* Allocate one iff we are building a shared library, the relocation
2336	     isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
2337	  if (!info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
2338	    continue;
2339
2340	  /* Create a dynamic relocation for this entry.
2341
2342	     We need the output offset for the reloc's section because
2343	     we are creating an absolute address in the resulting object
2344	     file.  */
2345	  rel.r_offset = (rent->offset + rent->sec->output_offset
2346			  + rent->sec->output_section->vma);
2347
2348	  /* An FPTR64 relocation implies that we took the address of
2349	     a function and that the function has an entry in the .opd
2350	     section.  We want the FPTR64 relocation to reference the
2351	     entry in .opd.
2352
2353	     We could munge the symbol value in the dynamic symbol table
2354	     (in fact we already do for functions with global scope) to point
2355	     to the .opd entry.  Then we could use that dynamic symbol in
2356	     this relocation.
2357
2358	     Or we could do something sensible, not munge the symbol's
2359	     address and instead just use a different symbol to reference
2360	     the .opd entry.  At least that seems sensible until you
2361	     realize there's no local dynamic symbols we can use for that
2362	     purpose.  Thus the hair in the check_relocs routine.
2363
2364	     We use a section symbol recorded by check_relocs as the
2365	     base symbol for the relocation.  The addend is the difference
2366	     between the section symbol and the address of the .opd entry.  */
2367	  if (info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
2368	    {
2369	      bfd_vma value, value2;
2370
2371	      /* First compute the address of the opd entry for this symbol.  */
2372	      value = (dyn_h->opd_offset
2373		       + hppa_info->opd_sec->output_section->vma
2374		       + hppa_info->opd_sec->output_offset);
2375
2376	      /* Compute the value of the start of the section with
2377		 the relocation.  */
2378	      value2 = (rent->sec->output_section->vma
2379			+ rent->sec->output_offset);
2380
2381	      /* Compute the difference between the start of the section
2382		 with the relocation and the opd entry.  */
2383	      value -= value2;
2384
2385	      /* The result becomes the addend of the relocation.  */
2386	      rel.r_addend = value;
2387
2388	      /* The section symbol becomes the symbol for the dynamic
2389		 relocation.  */
2390	      dynindx
2391		= _bfd_elf_link_lookup_local_dynindx (info,
2392						      rent->sec->owner,
2393						      rent->sec_symndx);
2394	    }
2395	  else
2396	    rel.r_addend = rent->addend;
2397
2398	  rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2399
2400	  loc = hppa_info->other_rel_sec->contents;
2401	  loc += (hppa_info->other_rel_sec->reloc_count++
2402		  * sizeof (Elf64_External_Rela));
2403	  bfd_elf64_swap_reloca_out (hppa_info->other_rel_sec->output_section->owner,
2404				     &rel, loc);
2405	}
2406    }
2407
2408  return TRUE;
2409}
2410
2411/* Used to decide how to sort relocs in an optimal manner for the
2412   dynamic linker, before writing them out.  */
2413
2414static enum elf_reloc_type_class
2415elf64_hppa_reloc_type_class (rela)
2416     const Elf_Internal_Rela *rela;
2417{
2418  if (ELF64_R_SYM (rela->r_info) == 0)
2419    return reloc_class_relative;
2420
2421  switch ((int) ELF64_R_TYPE (rela->r_info))
2422    {
2423    case R_PARISC_IPLT:
2424      return reloc_class_plt;
2425    case R_PARISC_COPY:
2426      return reloc_class_copy;
2427    default:
2428      return reloc_class_normal;
2429    }
2430}
2431
2432/* Finish up the dynamic sections.  */
2433
2434static bfd_boolean
2435elf64_hppa_finish_dynamic_sections (output_bfd, info)
2436     bfd *output_bfd;
2437     struct bfd_link_info *info;
2438{
2439  bfd *dynobj;
2440  asection *sdyn;
2441  struct elf64_hppa_link_hash_table *hppa_info;
2442
2443  hppa_info = elf64_hppa_hash_table (info);
2444
2445  /* Finalize the contents of the .opd section.  */
2446  elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2447				elf64_hppa_finalize_opd,
2448				info);
2449
2450  elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2451				elf64_hppa_finalize_dynreloc,
2452				info);
2453
2454  /* Finalize the contents of the .dlt section.  */
2455  dynobj = elf_hash_table (info)->dynobj;
2456  /* Finalize the contents of the .dlt section.  */
2457  elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2458				elf64_hppa_finalize_dlt,
2459				info);
2460
2461  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2462
2463  if (elf_hash_table (info)->dynamic_sections_created)
2464    {
2465      Elf64_External_Dyn *dyncon, *dynconend;
2466
2467      BFD_ASSERT (sdyn != NULL);
2468
2469      dyncon = (Elf64_External_Dyn *) sdyn->contents;
2470      dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2471      for (; dyncon < dynconend; dyncon++)
2472	{
2473	  Elf_Internal_Dyn dyn;
2474	  asection *s;
2475
2476	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2477
2478	  switch (dyn.d_tag)
2479	    {
2480	    default:
2481	      break;
2482
2483	    case DT_HP_LOAD_MAP:
2484	      /* Compute the absolute address of 16byte scratchpad area
2485		 for the dynamic linker.
2486
2487		 By convention the linker script will allocate the scratchpad
2488		 area at the start of the .data section.  So all we have to
2489		 to is find the start of the .data section.  */
2490	      s = bfd_get_section_by_name (output_bfd, ".data");
2491	      dyn.d_un.d_ptr = s->vma;
2492	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2493	      break;
2494
2495	    case DT_PLTGOT:
2496	      /* HP's use PLTGOT to set the GOT register.  */
2497	      dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2498	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2499	      break;
2500
2501	    case DT_JMPREL:
2502	      s = hppa_info->plt_rel_sec;
2503	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2504	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2505	      break;
2506
2507	    case DT_PLTRELSZ:
2508	      s = hppa_info->plt_rel_sec;
2509	      dyn.d_un.d_val = s->size;
2510	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2511	      break;
2512
2513	    case DT_RELA:
2514	      s = hppa_info->other_rel_sec;
2515	      if (! s || ! s->size)
2516		s = hppa_info->dlt_rel_sec;
2517	      if (! s || ! s->size)
2518		s = hppa_info->opd_rel_sec;
2519	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2520	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2521	      break;
2522
2523	    case DT_RELASZ:
2524	      s = hppa_info->other_rel_sec;
2525	      dyn.d_un.d_val = s->size;
2526	      s = hppa_info->dlt_rel_sec;
2527	      dyn.d_un.d_val += s->size;
2528	      s = hppa_info->opd_rel_sec;
2529	      dyn.d_un.d_val += s->size;
2530	      /* There is some question about whether or not the size of
2531		 the PLT relocs should be included here.  HP's tools do
2532		 it, so we'll emulate them.  */
2533	      s = hppa_info->plt_rel_sec;
2534	      dyn.d_un.d_val += s->size;
2535	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2536	      break;
2537
2538	    }
2539	}
2540    }
2541
2542  return TRUE;
2543}
2544
2545/* Support for core dump NOTE sections.  */
2546
2547static bfd_boolean
2548elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2549{
2550  int offset;
2551  size_t size;
2552
2553  switch (note->descsz)
2554    {
2555      default:
2556	return FALSE;
2557
2558      case 760:		/* Linux/hppa */
2559	/* pr_cursig */
2560	elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2561
2562	/* pr_pid */
2563	elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2564
2565	/* pr_reg */
2566	offset = 112;
2567	size = 640;
2568
2569	break;
2570    }
2571
2572  /* Make a ".reg/999" section.  */
2573  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2574					  size, note->descpos + offset);
2575}
2576
2577static bfd_boolean
2578elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2579{
2580  char * command;
2581  int n;
2582
2583  switch (note->descsz)
2584    {
2585    default:
2586      return FALSE;
2587
2588    case 136:		/* Linux/hppa elf_prpsinfo.  */
2589      elf_tdata (abfd)->core_program
2590	= _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2591      elf_tdata (abfd)->core_command
2592	= _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2593    }
2594
2595  /* Note that for some reason, a spurious space is tacked
2596     onto the end of the args in some (at least one anyway)
2597     implementations, so strip it off if it exists.  */
2598  command = elf_tdata (abfd)->core_command;
2599  n = strlen (command);
2600
2601  if (0 < n && command[n - 1] == ' ')
2602    command[n - 1] = '\0';
2603
2604  return TRUE;
2605}
2606
2607/* Return the number of additional phdrs we will need.
2608
2609   The generic ELF code only creates PT_PHDRs for executables.  The HP
2610   dynamic linker requires PT_PHDRs for dynamic libraries too.
2611
2612   This routine indicates that the backend needs one additional program
2613   header for that case.
2614
2615   Note we do not have access to the link info structure here, so we have
2616   to guess whether or not we are building a shared library based on the
2617   existence of a .interp section.  */
2618
2619static int
2620elf64_hppa_additional_program_headers (abfd)
2621     bfd *abfd;
2622{
2623  asection *s;
2624
2625  /* If we are creating a shared library, then we have to create a
2626     PT_PHDR segment.  HP's dynamic linker chokes without it.  */
2627  s = bfd_get_section_by_name (abfd, ".interp");
2628  if (! s)
2629    return 1;
2630  return 0;
2631}
2632
2633/* Allocate and initialize any program headers required by this
2634   specific backend.
2635
2636   The generic ELF code only creates PT_PHDRs for executables.  The HP
2637   dynamic linker requires PT_PHDRs for dynamic libraries too.
2638
2639   This allocates the PT_PHDR and initializes it in a manner suitable
2640   for the HP linker.
2641
2642   Note we do not have access to the link info structure here, so we have
2643   to guess whether or not we are building a shared library based on the
2644   existence of a .interp section.  */
2645
2646static bfd_boolean
2647elf64_hppa_modify_segment_map (abfd, info)
2648     bfd *abfd;
2649     struct bfd_link_info *info ATTRIBUTE_UNUSED;
2650{
2651  struct elf_segment_map *m;
2652  asection *s;
2653
2654  s = bfd_get_section_by_name (abfd, ".interp");
2655  if (! s)
2656    {
2657      for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2658	if (m->p_type == PT_PHDR)
2659	  break;
2660      if (m == NULL)
2661	{
2662	  m = ((struct elf_segment_map *)
2663	       bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
2664	  if (m == NULL)
2665	    return FALSE;
2666
2667	  m->p_type = PT_PHDR;
2668	  m->p_flags = PF_R | PF_X;
2669	  m->p_flags_valid = 1;
2670	  m->p_paddr_valid = 1;
2671	  m->includes_phdrs = 1;
2672
2673	  m->next = elf_tdata (abfd)->segment_map;
2674	  elf_tdata (abfd)->segment_map = m;
2675	}
2676    }
2677
2678  for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2679    if (m->p_type == PT_LOAD)
2680      {
2681	unsigned int i;
2682
2683	for (i = 0; i < m->count; i++)
2684	  {
2685	    /* The code "hint" is not really a hint.  It is a requirement
2686	       for certain versions of the HP dynamic linker.  Worse yet,
2687	       it must be set even if the shared library does not have
2688	       any code in its "text" segment (thus the check for .hash
2689	       to catch this situation).  */
2690	    if (m->sections[i]->flags & SEC_CODE
2691		|| (strcmp (m->sections[i]->name, ".hash") == 0))
2692	      m->p_flags |= (PF_X | PF_HP_CODE);
2693	  }
2694      }
2695
2696  return TRUE;
2697}
2698
2699/* Called when writing out an object file to decide the type of a
2700   symbol.  */
2701static int
2702elf64_hppa_elf_get_symbol_type (elf_sym, type)
2703     Elf_Internal_Sym *elf_sym;
2704     int type;
2705{
2706  if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2707    return STT_PARISC_MILLI;
2708  else
2709    return type;
2710}
2711
2712/* Support HP specific sections for core files.  */
2713static bfd_boolean
2714elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index,
2715			      const char *typename)
2716{
2717  if (hdr->p_type == PT_HP_CORE_KERNEL)
2718    {
2719      asection *sect;
2720
2721      if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
2722	return FALSE;
2723
2724      sect = bfd_make_section_anyway (abfd, ".kernel");
2725      if (sect == NULL)
2726	return FALSE;
2727      sect->size = hdr->p_filesz;
2728      sect->filepos = hdr->p_offset;
2729      sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2730      return TRUE;
2731    }
2732
2733  if (hdr->p_type == PT_HP_CORE_PROC)
2734    {
2735      int sig;
2736
2737      if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2738	return FALSE;
2739      if (bfd_bread (&sig, 4, abfd) != 4)
2740	return FALSE;
2741
2742      elf_tdata (abfd)->core_signal = sig;
2743
2744      if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
2745	return FALSE;
2746
2747      /* GDB uses the ".reg" section to read register contents.  */
2748      return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2749					      hdr->p_offset);
2750    }
2751
2752  if (hdr->p_type == PT_HP_CORE_LOADABLE
2753      || hdr->p_type == PT_HP_CORE_STACK
2754      || hdr->p_type == PT_HP_CORE_MMF)
2755    hdr->p_type = PT_LOAD;
2756
2757  return _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename);
2758}
2759
2760static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
2761{
2762  { ".fini",   5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2763  { ".init",   5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764  { ".plt",    4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2765  { ".dlt",    4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2766  { ".sdata",  6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2767  { ".sbss",   5, 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2768  { ".tbss",   5, 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
2769  { NULL,      0, 0, 0,            0 }
2770};
2771
2772/* The hash bucket size is the standard one, namely 4.  */
2773
2774const struct elf_size_info hppa64_elf_size_info =
2775{
2776  sizeof (Elf64_External_Ehdr),
2777  sizeof (Elf64_External_Phdr),
2778  sizeof (Elf64_External_Shdr),
2779  sizeof (Elf64_External_Rel),
2780  sizeof (Elf64_External_Rela),
2781  sizeof (Elf64_External_Sym),
2782  sizeof (Elf64_External_Dyn),
2783  sizeof (Elf_External_Note),
2784  4,
2785  1,
2786  64, 3,
2787  ELFCLASS64, EV_CURRENT,
2788  bfd_elf64_write_out_phdrs,
2789  bfd_elf64_write_shdrs_and_ehdr,
2790  bfd_elf64_write_relocs,
2791  bfd_elf64_swap_symbol_in,
2792  bfd_elf64_swap_symbol_out,
2793  bfd_elf64_slurp_reloc_table,
2794  bfd_elf64_slurp_symbol_table,
2795  bfd_elf64_swap_dyn_in,
2796  bfd_elf64_swap_dyn_out,
2797  bfd_elf64_swap_reloc_in,
2798  bfd_elf64_swap_reloc_out,
2799  bfd_elf64_swap_reloca_in,
2800  bfd_elf64_swap_reloca_out
2801};
2802
2803#define TARGET_BIG_SYM			bfd_elf64_hppa_vec
2804#define TARGET_BIG_NAME			"elf64-hppa"
2805#define ELF_ARCH			bfd_arch_hppa
2806#define ELF_MACHINE_CODE		EM_PARISC
2807/* This is not strictly correct.  The maximum page size for PA2.0 is
2808   64M.  But everything still uses 4k.  */
2809#define ELF_MAXPAGESIZE			0x1000
2810#define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
2811#define bfd_elf64_bfd_is_local_label_name       elf_hppa_is_local_label_name
2812#define elf_info_to_howto		elf_hppa_info_to_howto
2813#define elf_info_to_howto_rel		elf_hppa_info_to_howto_rel
2814
2815#define elf_backend_section_from_shdr	elf64_hppa_section_from_shdr
2816#define elf_backend_object_p		elf64_hppa_object_p
2817#define elf_backend_final_write_processing \
2818					elf_hppa_final_write_processing
2819#define elf_backend_fake_sections	elf_hppa_fake_sections
2820#define elf_backend_add_symbol_hook	elf_hppa_add_symbol_hook
2821
2822#define elf_backend_relocate_section	elf_hppa_relocate_section
2823
2824#define bfd_elf64_bfd_final_link	elf_hppa_final_link
2825
2826#define elf_backend_create_dynamic_sections \
2827					elf64_hppa_create_dynamic_sections
2828#define elf_backend_post_process_headers	elf64_hppa_post_process_headers
2829
2830#define elf_backend_adjust_dynamic_symbol \
2831					elf64_hppa_adjust_dynamic_symbol
2832
2833#define elf_backend_size_dynamic_sections \
2834					elf64_hppa_size_dynamic_sections
2835
2836#define elf_backend_finish_dynamic_symbol \
2837					elf64_hppa_finish_dynamic_symbol
2838#define elf_backend_finish_dynamic_sections \
2839					elf64_hppa_finish_dynamic_sections
2840#define elf_backend_grok_prstatus	elf64_hppa_grok_prstatus
2841#define elf_backend_grok_psinfo		elf64_hppa_grok_psinfo
2842
2843/* Stuff for the BFD linker: */
2844#define bfd_elf64_bfd_link_hash_table_create \
2845	elf64_hppa_hash_table_create
2846
2847#define elf_backend_check_relocs \
2848	elf64_hppa_check_relocs
2849
2850#define elf_backend_size_info \
2851  hppa64_elf_size_info
2852
2853#define elf_backend_additional_program_headers \
2854	elf64_hppa_additional_program_headers
2855
2856#define elf_backend_modify_segment_map \
2857	elf64_hppa_modify_segment_map
2858
2859#define elf_backend_link_output_symbol_hook \
2860	elf64_hppa_link_output_symbol_hook
2861
2862#define elf_backend_want_got_plt	0
2863#define elf_backend_plt_readonly	0
2864#define elf_backend_want_plt_sym	0
2865#define elf_backend_got_header_size     0
2866#define elf_backend_type_change_ok	TRUE
2867#define elf_backend_get_symbol_type	elf64_hppa_elf_get_symbol_type
2868#define elf_backend_reloc_type_class	elf64_hppa_reloc_type_class
2869#define elf_backend_rela_normal		1
2870#define elf_backend_special_sections	elf64_hppa_special_sections
2871#define elf_backend_action_discarded	elf_hppa_action_discarded
2872#define elf_backend_section_from_phdr   elf64_hppa_section_from_phdr
2873
2874#include "elf64-target.h"
2875
2876#undef TARGET_BIG_SYM
2877#define TARGET_BIG_SYM			bfd_elf64_hppa_linux_vec
2878#undef TARGET_BIG_NAME
2879#define TARGET_BIG_NAME			"elf64-hppa-linux"
2880
2881#define INCLUDED_TARGET_FILE 1
2882#include "elf64-target.h"
2883