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