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