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