OSDN Git Service

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