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