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