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