OSDN Git Service

* bfdio.c (bfd_seek): Formatting. Ensure newly allocated memory
[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 static bfd_boolean
2744 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index,
2745                               const char *typename)
2746 {
2747   if (hdr->p_type == PT_HP_CORE_KERNEL)
2748     {
2749       asection *sect;
2750
2751       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
2752         return FALSE;
2753
2754       sect = bfd_make_section_anyway (abfd, ".kernel");
2755       if (sect == NULL)
2756         return FALSE;
2757       sect->size = hdr->p_filesz;
2758       sect->filepos = hdr->p_offset;
2759       sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2760       return TRUE;
2761     }
2762
2763   if (hdr->p_type == PT_HP_CORE_PROC)
2764     {
2765       int sig;
2766
2767       if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2768         return FALSE;
2769       if (bfd_bread (&sig, 4, abfd) != 4)
2770         return FALSE;
2771
2772       elf_tdata (abfd)->core_signal = sig;
2773
2774       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
2775         return FALSE;
2776
2777       /* GDB uses the ".reg" section to read register contents.  */
2778       return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2779                                               hdr->p_offset);
2780     }
2781
2782   if (hdr->p_type == PT_HP_CORE_LOADABLE
2783       || hdr->p_type == PT_HP_CORE_STACK
2784       || hdr->p_type == PT_HP_CORE_MMF)
2785     hdr->p_type = PT_LOAD;
2786
2787   return _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename);
2788 }
2789
2790 /* Hook called by the linker routine which adds symbols from an object
2791    file.  HP's libraries define symbols with HP specific section
2792    indices, which we have to handle.  */
2793
2794 static bfd_boolean
2795 elf_hppa_add_symbol_hook (bfd *abfd,
2796                           struct bfd_link_info *info ATTRIBUTE_UNUSED,
2797                           Elf_Internal_Sym *sym,
2798                           const char **namep ATTRIBUTE_UNUSED,
2799                           flagword *flagsp ATTRIBUTE_UNUSED,
2800                           asection **secp,
2801                           bfd_vma *valp)
2802 {
2803   unsigned int index = sym->st_shndx;
2804
2805   switch (index)
2806     {
2807     case SHN_PARISC_ANSI_COMMON:
2808       *secp = bfd_make_section_old_way (abfd, ".PARISC.ansi.common");
2809       (*secp)->flags |= SEC_IS_COMMON;
2810       *valp = sym->st_size;
2811       break;
2812
2813     case SHN_PARISC_HUGE_COMMON:
2814       *secp = bfd_make_section_old_way (abfd, ".PARISC.huge.common");
2815       (*secp)->flags |= SEC_IS_COMMON;
2816       *valp = sym->st_size;
2817       break;
2818     }
2819
2820   return TRUE;
2821 }
2822
2823 static bfd_boolean
2824 elf_hppa_unmark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2825                                          void *data)
2826 {
2827   struct bfd_link_info *info = data;
2828
2829   if (h->root.type == bfd_link_hash_warning)
2830     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2831
2832   /* If we are not creating a shared library, and this symbol is
2833      referenced by a shared library but is not defined anywhere, then
2834      the generic code will warn that it is undefined.
2835
2836      This behavior is undesirable on HPs since the standard shared
2837      libraries contain references to undefined symbols.
2838
2839      So we twiddle the flags associated with such symbols so that they
2840      will not trigger the warning.  ?!? FIXME.  This is horribly fragile.
2841
2842      Ultimately we should have better controls over the generic ELF BFD
2843      linker code.  */
2844   if (! info->relocatable
2845       && info->unresolved_syms_in_shared_libs != RM_IGNORE
2846       && h->root.type == bfd_link_hash_undefined
2847       && h->ref_dynamic
2848       && !h->ref_regular)
2849     {
2850       h->ref_dynamic = 0;
2851       h->pointer_equality_needed = 1;
2852     }
2853
2854   return TRUE;
2855 }
2856
2857 static bfd_boolean
2858 elf_hppa_remark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2859                                          void *data)
2860 {
2861   struct bfd_link_info *info = data;
2862
2863   if (h->root.type == bfd_link_hash_warning)
2864     h = (struct elf_link_hash_entry *) h->root.u.i.link;
2865
2866   /* If we are not creating a shared library, and this symbol is
2867      referenced by a shared library but is not defined anywhere, then
2868      the generic code will warn that it is undefined.
2869
2870      This behavior is undesirable on HPs since the standard shared
2871      libraries contain references to undefined symbols.
2872
2873      So we twiddle the flags associated with such symbols so that they
2874      will not trigger the warning.  ?!? FIXME.  This is horribly fragile.
2875
2876      Ultimately we should have better controls over the generic ELF BFD
2877      linker code.  */
2878   if (! info->relocatable
2879       && info->unresolved_syms_in_shared_libs != RM_IGNORE
2880       && h->root.type == bfd_link_hash_undefined
2881       && !h->ref_dynamic
2882       && !h->ref_regular
2883       && h->pointer_equality_needed)
2884     {
2885       h->ref_dynamic = 1;
2886       h->pointer_equality_needed = 0;
2887     }
2888
2889   return TRUE;
2890 }
2891
2892 static bfd_boolean
2893 elf_hppa_is_dynamic_loader_symbol (const char *name)
2894 {
2895   return (! strcmp (name, "__CPU_REVISION")
2896           || ! strcmp (name, "__CPU_KEYBITS_1")
2897           || ! strcmp (name, "__SYSTEM_ID_D")
2898           || ! strcmp (name, "__FPU_MODEL")
2899           || ! strcmp (name, "__FPU_REVISION")
2900           || ! strcmp (name, "__ARGC")
2901           || ! strcmp (name, "__ARGV")
2902           || ! strcmp (name, "__ENVP")
2903           || ! strcmp (name, "__TLS_SIZE_D")
2904           || ! strcmp (name, "__LOAD_INFO")
2905           || ! strcmp (name, "__systab"));
2906 }
2907
2908 /* Record the lowest address for the data and text segments.  */
2909 static void
2910 elf_hppa_record_segment_addrs (bfd *abfd,
2911                                asection *section,
2912                                void *data)
2913 {
2914   struct elf64_hppa_link_hash_table *hppa_info = data;
2915
2916   if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2917     {
2918       bfd_vma value;
2919       Elf_Internal_Phdr *p;
2920
2921       p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
2922       BFD_ASSERT (p != NULL);
2923       value = p->p_vaddr;
2924
2925       if (section->flags & SEC_READONLY)
2926         {
2927           if (value < hppa_info->text_segment_base)
2928             hppa_info->text_segment_base = value;
2929         }
2930       else
2931         {
2932           if (value < hppa_info->data_segment_base)
2933             hppa_info->data_segment_base = value;
2934         }
2935     }
2936 }
2937
2938 /* Called after we have seen all the input files/sections, but before
2939    final symbol resolution and section placement has been determined.
2940
2941    We use this hook to (possibly) provide a value for __gp, then we
2942    fall back to the generic ELF final link routine.  */
2943
2944 static bfd_boolean
2945 elf_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
2946 {
2947   bfd_boolean retval;
2948   struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
2949
2950   if (! info->relocatable)
2951     {
2952       struct elf_link_hash_entry *gp;
2953       bfd_vma gp_val;
2954
2955       /* The linker script defines a value for __gp iff it was referenced
2956          by one of the objects being linked.  First try to find the symbol
2957          in the hash table.  If that fails, just compute the value __gp
2958          should have had.  */
2959       gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", FALSE,
2960                                  FALSE, FALSE);
2961
2962       if (gp)
2963         {
2964
2965           /* Adjust the value of __gp as we may want to slide it into the
2966              .plt section so that the stubs can access PLT entries without
2967              using an addil sequence.  */
2968           gp->root.u.def.value += hppa_info->gp_offset;
2969
2970           gp_val = (gp->root.u.def.section->output_section->vma
2971                     + gp->root.u.def.section->output_offset
2972                     + gp->root.u.def.value);
2973         }
2974       else
2975         {
2976           asection *sec;
2977
2978           /* First look for a .plt section.  If found, then __gp is the
2979              address of the .plt + gp_offset.
2980
2981              If no .plt is found, then look for .dlt, .opd and .data (in
2982              that order) and set __gp to the base address of whichever
2983              section is found first.  */
2984
2985           sec = hppa_info->plt_sec;
2986           if (sec && ! (sec->flags & SEC_EXCLUDE))
2987             gp_val = (sec->output_offset
2988                       + sec->output_section->vma
2989                       + hppa_info->gp_offset);
2990           else
2991             {
2992               sec = hppa_info->dlt_sec;
2993               if (!sec || (sec->flags & SEC_EXCLUDE))
2994                 sec = hppa_info->opd_sec;
2995               if (!sec || (sec->flags & SEC_EXCLUDE))
2996                 sec = bfd_get_section_by_name (abfd, ".data");
2997               if (!sec || (sec->flags & SEC_EXCLUDE))
2998                 gp_val = 0;
2999               else
3000                 gp_val = sec->output_offset + sec->output_section->vma;
3001             }
3002         }
3003
3004       /* Install whatever value we found/computed for __gp.  */
3005       _bfd_set_gp_value (abfd, gp_val);
3006     }
3007
3008   /* We need to know the base of the text and data segments so that we
3009      can perform SEGREL relocations.  We will record the base addresses
3010      when we encounter the first SEGREL relocation.  */
3011   hppa_info->text_segment_base = (bfd_vma)-1;
3012   hppa_info->data_segment_base = (bfd_vma)-1;
3013
3014   /* HP's shared libraries have references to symbols that are not
3015      defined anywhere.  The generic ELF BFD linker code will complain
3016      about such symbols.
3017
3018      So we detect the losing case and arrange for the flags on the symbol
3019      to indicate that it was never referenced.  This keeps the generic
3020      ELF BFD link code happy and appears to not create any secondary
3021      problems.  Ultimately we need a way to control the behavior of the
3022      generic ELF BFD link code better.  */
3023   elf_link_hash_traverse (elf_hash_table (info),
3024                           elf_hppa_unmark_useless_dynamic_symbols,
3025                           info);
3026
3027   /* Invoke the regular ELF backend linker to do all the work.  */
3028   retval = bfd_elf_final_link (abfd, info);
3029
3030   elf_link_hash_traverse (elf_hash_table (info),
3031                           elf_hppa_remark_useless_dynamic_symbols,
3032                           info);
3033
3034   /* If we're producing a final executable, sort the contents of the
3035      unwind section. */
3036   if (retval)
3037     retval = elf_hppa_sort_unwind (abfd);
3038
3039   return retval;
3040 }
3041
3042 /* Relocate the given INSN.  VALUE should be the actual value we want
3043    to insert into the instruction, ie by this point we should not be
3044    concerned with computing an offset relative to the DLT, PC, etc.
3045    Instead this routine is meant to handle the bit manipulations needed
3046    to insert the relocation into the given instruction.  */
3047
3048 static int
3049 elf_hppa_relocate_insn (int insn, int sym_value, unsigned int r_type)
3050 {
3051   switch (r_type)
3052     {
3053     /* This is any 22 bit branch.  In PA2.0 syntax it corresponds to
3054        the "B" instruction.  */
3055     case R_PARISC_PCREL22F:
3056     case R_PARISC_PCREL22C:
3057       return (insn & ~0x3ff1ffd) | re_assemble_22 (sym_value);
3058
3059       /* This is any 12 bit branch.  */
3060     case R_PARISC_PCREL12F:
3061       return (insn & ~0x1ffd) | re_assemble_12 (sym_value);
3062
3063     /* This is any 17 bit branch.  In PA2.0 syntax it also corresponds
3064        to the "B" instruction as well as BE.  */
3065     case R_PARISC_PCREL17F:
3066     case R_PARISC_DIR17F:
3067     case R_PARISC_DIR17R:
3068     case R_PARISC_PCREL17C:
3069     case R_PARISC_PCREL17R:
3070       return (insn & ~0x1f1ffd) | re_assemble_17 (sym_value);
3071
3072     /* ADDIL or LDIL instructions.  */
3073     case R_PARISC_DLTREL21L:
3074     case R_PARISC_DLTIND21L:
3075     case R_PARISC_LTOFF_FPTR21L:
3076     case R_PARISC_PCREL21L:
3077     case R_PARISC_LTOFF_TP21L:
3078     case R_PARISC_DPREL21L:
3079     case R_PARISC_PLTOFF21L:
3080     case R_PARISC_DIR21L:
3081       return (insn & ~0x1fffff) | re_assemble_21 (sym_value);
3082
3083     /* LDO and integer loads/stores with 14 bit displacements.  */
3084     case R_PARISC_DLTREL14R:
3085     case R_PARISC_DLTREL14F:
3086     case R_PARISC_DLTIND14R:
3087     case R_PARISC_DLTIND14F:
3088     case R_PARISC_LTOFF_FPTR14R:
3089     case R_PARISC_PCREL14R:
3090     case R_PARISC_PCREL14F:
3091     case R_PARISC_LTOFF_TP14R:
3092     case R_PARISC_LTOFF_TP14F:
3093     case R_PARISC_DPREL14R:
3094     case R_PARISC_DPREL14F:
3095     case R_PARISC_PLTOFF14R:
3096     case R_PARISC_PLTOFF14F:
3097     case R_PARISC_DIR14R:
3098     case R_PARISC_DIR14F:
3099       return (insn & ~0x3fff) | low_sign_unext (sym_value, 14);
3100
3101     /* PA2.0W LDO and integer loads/stores with 16 bit displacements.  */
3102     case R_PARISC_LTOFF_FPTR16F:
3103     case R_PARISC_PCREL16F:
3104     case R_PARISC_LTOFF_TP16F:
3105     case R_PARISC_GPREL16F:
3106     case R_PARISC_PLTOFF16F:
3107     case R_PARISC_DIR16F:
3108     case R_PARISC_LTOFF16F:
3109       return (insn & ~0xffff) | re_assemble_16 (sym_value);
3110
3111     /* Doubleword loads and stores with a 14 bit displacement.  */
3112     case R_PARISC_DLTREL14DR:
3113     case R_PARISC_DLTIND14DR:
3114     case R_PARISC_LTOFF_FPTR14DR:
3115     case R_PARISC_LTOFF_FPTR16DF:
3116     case R_PARISC_PCREL14DR:
3117     case R_PARISC_PCREL16DF:
3118     case R_PARISC_LTOFF_TP14DR:
3119     case R_PARISC_LTOFF_TP16DF:
3120     case R_PARISC_DPREL14DR:
3121     case R_PARISC_GPREL16DF:
3122     case R_PARISC_PLTOFF14DR:
3123     case R_PARISC_PLTOFF16DF:
3124     case R_PARISC_DIR14DR:
3125     case R_PARISC_DIR16DF:
3126     case R_PARISC_LTOFF16DF:
3127       return (insn & ~0x3ff1) | (((sym_value & 0x2000) >> 13)
3128                                  | ((sym_value & 0x1ff8) << 1));
3129
3130     /* Floating point single word load/store instructions.  */
3131     case R_PARISC_DLTREL14WR:
3132     case R_PARISC_DLTIND14WR:
3133     case R_PARISC_LTOFF_FPTR14WR:
3134     case R_PARISC_LTOFF_FPTR16WF:
3135     case R_PARISC_PCREL14WR:
3136     case R_PARISC_PCREL16WF:
3137     case R_PARISC_LTOFF_TP14WR:
3138     case R_PARISC_LTOFF_TP16WF:
3139     case R_PARISC_DPREL14WR:
3140     case R_PARISC_GPREL16WF:
3141     case R_PARISC_PLTOFF14WR:
3142     case R_PARISC_PLTOFF16WF:
3143     case R_PARISC_DIR16WF:
3144     case R_PARISC_DIR14WR:
3145     case R_PARISC_LTOFF16WF:
3146       return (insn & ~0x3ff9) | (((sym_value & 0x2000) >> 13)
3147                                  | ((sym_value & 0x1ffc) << 1));
3148
3149     default:
3150       return insn;
3151     }
3152 }
3153
3154 /* Compute the value for a relocation (REL) during a final link stage,
3155    then insert the value into the proper location in CONTENTS.
3156
3157    VALUE is a tentative value for the relocation and may be overridden
3158    and modified here based on the specific relocation to be performed.
3159
3160    For example we do conversions for PC-relative branches in this routine
3161    or redirection of calls to external routines to stubs.
3162
3163    The work of actually applying the relocation is left to a helper
3164    routine in an attempt to reduce the complexity and size of this
3165    function.  */
3166
3167 static bfd_reloc_status_type
3168 elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
3169                               bfd *input_bfd,
3170                               bfd *output_bfd,
3171                               asection *input_section,
3172                               bfd_byte *contents,
3173                               bfd_vma value,
3174                               struct bfd_link_info *info,
3175                               asection *sym_sec,
3176                               struct elf_link_hash_entry *eh)
3177 {
3178   struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
3179   struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
3180   bfd_vma *local_offsets;
3181   Elf_Internal_Shdr *symtab_hdr;
3182   int insn;
3183   bfd_vma max_branch_offset = 0;
3184   bfd_vma offset = rel->r_offset;
3185   bfd_signed_vma addend = rel->r_addend;
3186   reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3187   unsigned int r_symndx = ELF_R_SYM (rel->r_info);
3188   unsigned int r_type = howto->type;
3189   bfd_byte *hit_data = contents + offset;
3190
3191   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3192   local_offsets = elf_local_got_offsets (input_bfd);
3193   insn = bfd_get_32 (input_bfd, hit_data);
3194
3195   switch (r_type)
3196     {
3197     case R_PARISC_NONE:
3198       break;
3199
3200     /* Basic function call support.
3201
3202        Note for a call to a function defined in another dynamic library
3203        we want to redirect the call to a stub.  */
3204
3205     /* PC relative relocs without an implicit offset.  */
3206     case R_PARISC_PCREL21L:
3207     case R_PARISC_PCREL14R:
3208     case R_PARISC_PCREL14F:
3209     case R_PARISC_PCREL14WR:
3210     case R_PARISC_PCREL14DR:
3211     case R_PARISC_PCREL16F:
3212     case R_PARISC_PCREL16WF:
3213     case R_PARISC_PCREL16DF:
3214       {
3215         /* If this is a call to a function defined in another dynamic
3216            library, then redirect the call to the local stub for this
3217            function.  */
3218         if (sym_sec == NULL || sym_sec->output_section == NULL)
3219           value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3220                    + hppa_info->stub_sec->output_section->vma);
3221
3222         /* Turn VALUE into a proper PC relative address.  */
3223         value -= (offset + input_section->output_offset
3224                   + input_section->output_section->vma);
3225
3226         /* Adjust for any field selectors.  */
3227         if (r_type == R_PARISC_PCREL21L)
3228           value = hppa_field_adjust (value, -8 + addend, e_lsel);
3229         else if (r_type == R_PARISC_PCREL14F
3230                  || r_type == R_PARISC_PCREL16F
3231                  || r_type == R_PARISC_PCREL16WF
3232                  || r_type == R_PARISC_PCREL16DF)
3233           value = hppa_field_adjust (value, -8 + addend, e_fsel);
3234         else
3235           value = hppa_field_adjust (value, -8 + addend, e_rsel);
3236
3237         /* Apply the relocation to the given instruction.  */
3238         insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3239         break;
3240       }
3241
3242     case R_PARISC_PCREL12F:
3243     case R_PARISC_PCREL22F:
3244     case R_PARISC_PCREL17F:
3245     case R_PARISC_PCREL22C:
3246     case R_PARISC_PCREL17C:
3247     case R_PARISC_PCREL17R:
3248       {
3249         /* If this is a call to a function defined in another dynamic
3250            library, then redirect the call to the local stub for this
3251            function.  */
3252         if (sym_sec == NULL || sym_sec->output_section == NULL)
3253           value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3254                    + hppa_info->stub_sec->output_section->vma);
3255
3256         /* Turn VALUE into a proper PC relative address.  */
3257         value -= (offset + input_section->output_offset
3258                   + input_section->output_section->vma);
3259         addend -= 8;
3260
3261         if (r_type == (unsigned int) R_PARISC_PCREL22F)
3262           max_branch_offset = (1 << (22-1)) << 2;
3263         else if (r_type == (unsigned int) R_PARISC_PCREL17F)
3264           max_branch_offset = (1 << (17-1)) << 2;
3265         else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3266           max_branch_offset = (1 << (12-1)) << 2;
3267
3268         /* Make sure we can reach the branch target.  */
3269         if (max_branch_offset != 0
3270             && value + addend + max_branch_offset >= 2*max_branch_offset)
3271           {
3272             (*_bfd_error_handler)
3273               (_("%B(%A+0x%lx): cannot reach %s"),
3274               input_bfd,
3275               input_section,
3276               offset,
3277               eh->root.root.string);
3278             bfd_set_error (bfd_error_bad_value);
3279             return bfd_reloc_notsupported;
3280           }
3281
3282         /* Adjust for any field selectors.  */
3283         if (r_type == R_PARISC_PCREL17R)
3284           value = hppa_field_adjust (value, addend, e_rsel);
3285         else
3286           value = hppa_field_adjust (value, addend, e_fsel);
3287
3288         /* All branches are implicitly shifted by 2 places.  */
3289         value >>= 2;
3290
3291         /* Apply the relocation to the given instruction.  */
3292         insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3293         break;
3294       }
3295
3296     /* Indirect references to data through the DLT.  */
3297     case R_PARISC_DLTIND14R:
3298     case R_PARISC_DLTIND14F:
3299     case R_PARISC_DLTIND14DR:
3300     case R_PARISC_DLTIND14WR:
3301     case R_PARISC_DLTIND21L:
3302     case R_PARISC_LTOFF_FPTR14R:
3303     case R_PARISC_LTOFF_FPTR14DR:
3304     case R_PARISC_LTOFF_FPTR14WR:
3305     case R_PARISC_LTOFF_FPTR21L:
3306     case R_PARISC_LTOFF_FPTR16F:
3307     case R_PARISC_LTOFF_FPTR16WF:
3308     case R_PARISC_LTOFF_FPTR16DF:
3309     case R_PARISC_LTOFF_TP21L:
3310     case R_PARISC_LTOFF_TP14R:
3311     case R_PARISC_LTOFF_TP14F:
3312     case R_PARISC_LTOFF_TP14WR:
3313     case R_PARISC_LTOFF_TP14DR:
3314     case R_PARISC_LTOFF_TP16F:
3315     case R_PARISC_LTOFF_TP16WF:
3316     case R_PARISC_LTOFF_TP16DF:
3317     case R_PARISC_LTOFF16F:
3318     case R_PARISC_LTOFF16WF:
3319     case R_PARISC_LTOFF16DF:
3320       {
3321         bfd_vma off;
3322
3323         /* If this relocation was against a local symbol, then we still
3324            have not set up the DLT entry (it's not convenient to do so
3325            in the "finalize_dlt" routine because it is difficult to get
3326            to the local symbol's value).
3327
3328            So, if this is a local symbol (h == NULL), then we need to
3329            fill in its DLT entry.
3330
3331            Similarly we may still need to set up an entry in .opd for
3332            a local function which had its address taken.  */
3333         if (hh == NULL)
3334           {
3335             bfd_vma *local_opd_offsets, *local_dlt_offsets;
3336
3337             if (local_offsets == NULL)
3338               abort ();
3339
3340             /* Now do .opd creation if needed.  */
3341             if (r_type == R_PARISC_LTOFF_FPTR14R
3342                 || r_type == R_PARISC_LTOFF_FPTR14DR
3343                 || r_type == R_PARISC_LTOFF_FPTR14WR
3344                 || r_type == R_PARISC_LTOFF_FPTR21L
3345                 || r_type == R_PARISC_LTOFF_FPTR16F
3346                 || r_type == R_PARISC_LTOFF_FPTR16WF
3347                 || r_type == R_PARISC_LTOFF_FPTR16DF)
3348               {
3349                 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3350                 off = local_opd_offsets[r_symndx];
3351
3352                 /* The last bit records whether we've already initialised
3353                    this local .opd entry.  */
3354                 if ((off & 1) != 0)
3355                   {
3356                     BFD_ASSERT (off != (bfd_vma) -1);
3357                     off &= ~1;
3358                   }
3359                 else
3360                   {
3361                     local_opd_offsets[r_symndx] |= 1;
3362
3363                     /* The first two words of an .opd entry are zero.  */
3364                     memset (hppa_info->opd_sec->contents + off, 0, 16);
3365
3366                     /* The next word is the address of the function.  */
3367                     bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3368                                 (hppa_info->opd_sec->contents + off + 16));
3369
3370                     /* The last word is our local __gp value.  */
3371                     value = _bfd_get_gp_value
3372                               (hppa_info->opd_sec->output_section->owner);
3373                     bfd_put_64 (hppa_info->opd_sec->owner, value,
3374                                 (hppa_info->opd_sec->contents + off + 24));
3375                   }
3376
3377                 /* The DLT value is the address of the .opd entry.  */
3378                 value = (off
3379                          + hppa_info->opd_sec->output_offset
3380                          + hppa_info->opd_sec->output_section->vma);
3381                 addend = 0;
3382               }
3383
3384             local_dlt_offsets = local_offsets;
3385             off = local_dlt_offsets[r_symndx];
3386
3387             if ((off & 1) != 0)
3388               {
3389                 BFD_ASSERT (off != (bfd_vma) -1);
3390                 off &= ~1;
3391               }
3392             else
3393               {
3394                 local_dlt_offsets[r_symndx] |= 1;
3395                 bfd_put_64 (hppa_info->dlt_sec->owner,
3396                             value + addend,
3397                             hppa_info->dlt_sec->contents + off);
3398               }
3399           }
3400         else
3401           off = hh->dlt_offset;
3402
3403         /* We want the value of the DLT offset for this symbol, not
3404            the symbol's actual address.  Note that __gp may not point
3405            to the start of the DLT, so we have to compute the absolute
3406            address, then subtract out the value of __gp.  */
3407         value = (off
3408                  + hppa_info->dlt_sec->output_offset
3409                  + hppa_info->dlt_sec->output_section->vma);
3410         value -= _bfd_get_gp_value (output_bfd);
3411
3412         /* All DLTIND relocations are basically the same at this point,
3413            except that we need different field selectors for the 21bit
3414            version vs the 14bit versions.  */
3415         if (r_type == R_PARISC_DLTIND21L
3416             || r_type == R_PARISC_LTOFF_FPTR21L
3417             || r_type == R_PARISC_LTOFF_TP21L)
3418           value = hppa_field_adjust (value, 0, e_lsel);
3419         else if (r_type == R_PARISC_DLTIND14F
3420                  || r_type == R_PARISC_LTOFF_FPTR16F
3421                  || r_type == R_PARISC_LTOFF_FPTR16WF
3422                  || r_type == R_PARISC_LTOFF_FPTR16DF
3423                  || r_type == R_PARISC_LTOFF16F
3424                  || r_type == R_PARISC_LTOFF16DF
3425                  || r_type == R_PARISC_LTOFF16WF
3426                  || r_type == R_PARISC_LTOFF_TP16F
3427                  || r_type == R_PARISC_LTOFF_TP16WF
3428                  || r_type == R_PARISC_LTOFF_TP16DF)
3429           value = hppa_field_adjust (value, 0, e_fsel);
3430         else
3431           value = hppa_field_adjust (value, 0, e_rsel);
3432
3433         insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3434         break;
3435       }
3436
3437     case R_PARISC_DLTREL14R:
3438     case R_PARISC_DLTREL14F:
3439     case R_PARISC_DLTREL14DR:
3440     case R_PARISC_DLTREL14WR:
3441     case R_PARISC_DLTREL21L:
3442     case R_PARISC_DPREL21L:
3443     case R_PARISC_DPREL14WR:
3444     case R_PARISC_DPREL14DR:
3445     case R_PARISC_DPREL14R:
3446     case R_PARISC_DPREL14F:
3447     case R_PARISC_GPREL16F:
3448     case R_PARISC_GPREL16WF:
3449     case R_PARISC_GPREL16DF:
3450       {
3451         /* Subtract out the global pointer value to make value a DLT
3452            relative address.  */
3453         value -= _bfd_get_gp_value (output_bfd);
3454
3455         /* All DLTREL relocations are basically the same at this point,
3456            except that we need different field selectors for the 21bit
3457            version vs the 14bit versions.  */
3458         if (r_type == R_PARISC_DLTREL21L
3459             || r_type == R_PARISC_DPREL21L)
3460           value = hppa_field_adjust (value, addend, e_lrsel);
3461         else if (r_type == R_PARISC_DLTREL14F
3462                  || r_type == R_PARISC_DPREL14F
3463                  || r_type == R_PARISC_GPREL16F
3464                  || r_type == R_PARISC_GPREL16WF
3465                  || r_type == R_PARISC_GPREL16DF)
3466           value = hppa_field_adjust (value, addend, e_fsel);
3467         else
3468           value = hppa_field_adjust (value, addend, e_rrsel);
3469
3470         insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3471         break;
3472       }
3473
3474     case R_PARISC_DIR21L:
3475     case R_PARISC_DIR17R:
3476     case R_PARISC_DIR17F:
3477     case R_PARISC_DIR14R:
3478     case R_PARISC_DIR14F:
3479     case R_PARISC_DIR14WR:
3480     case R_PARISC_DIR14DR:
3481     case R_PARISC_DIR16F:
3482     case R_PARISC_DIR16WF:
3483     case R_PARISC_DIR16DF:
3484       {
3485         /* All DIR relocations are basically the same at this point,
3486            except that branch offsets need to be divided by four, and
3487            we need different field selectors.  Note that we don't
3488            redirect absolute calls to local stubs.  */
3489
3490         if (r_type == R_PARISC_DIR21L)
3491           value = hppa_field_adjust (value, addend, e_lrsel);
3492         else if (r_type == R_PARISC_DIR17F
3493                  || r_type == R_PARISC_DIR16F
3494                  || r_type == R_PARISC_DIR16WF
3495                  || r_type == R_PARISC_DIR16DF
3496                  || r_type == R_PARISC_DIR14F)
3497           value = hppa_field_adjust (value, addend, e_fsel);
3498         else
3499           value = hppa_field_adjust (value, addend, e_rrsel);
3500
3501         if (r_type == R_PARISC_DIR17R || r_type == R_PARISC_DIR17F)
3502           /* All branches are implicitly shifted by 2 places.  */
3503           value >>= 2;
3504
3505         insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3506         break;
3507       }
3508
3509     case R_PARISC_PLTOFF21L:
3510     case R_PARISC_PLTOFF14R:
3511     case R_PARISC_PLTOFF14F:
3512     case R_PARISC_PLTOFF14WR:
3513     case R_PARISC_PLTOFF14DR:
3514     case R_PARISC_PLTOFF16F:
3515     case R_PARISC_PLTOFF16WF:
3516     case R_PARISC_PLTOFF16DF:
3517       {
3518         /* We want the value of the PLT offset for this symbol, not
3519            the symbol's actual address.  Note that __gp may not point
3520            to the start of the DLT, so we have to compute the absolute
3521            address, then subtract out the value of __gp.  */
3522         value = (hh->plt_offset
3523                  + hppa_info->plt_sec->output_offset
3524                  + hppa_info->plt_sec->output_section->vma);
3525         value -= _bfd_get_gp_value (output_bfd);
3526
3527         /* All PLTOFF relocations are basically the same at this point,
3528            except that we need different field selectors for the 21bit
3529            version vs the 14bit versions.  */
3530         if (r_type == R_PARISC_PLTOFF21L)
3531           value = hppa_field_adjust (value, addend, e_lrsel);
3532         else if (r_type == R_PARISC_PLTOFF14F
3533                  || r_type == R_PARISC_PLTOFF16F
3534                  || r_type == R_PARISC_PLTOFF16WF
3535                  || r_type == R_PARISC_PLTOFF16DF)
3536           value = hppa_field_adjust (value, addend, e_fsel);
3537         else
3538           value = hppa_field_adjust (value, addend, e_rrsel);
3539
3540         insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3541         break;
3542       }
3543
3544     case R_PARISC_LTOFF_FPTR32:
3545       {
3546         /* We may still need to create the FPTR itself if it was for
3547            a local symbol.  */
3548         if (hh == NULL)
3549           {
3550             /* The first two words of an .opd entry are zero.  */
3551             memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
3552
3553             /* The next word is the address of the function.  */
3554             bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3555                         (hppa_info->opd_sec->contents
3556                          + hh->opd_offset + 16));
3557
3558             /* The last word is our local __gp value.  */
3559             value = _bfd_get_gp_value
3560                       (hppa_info->opd_sec->output_section->owner);
3561             bfd_put_64 (hppa_info->opd_sec->owner, value,
3562                         hppa_info->opd_sec->contents + hh->opd_offset + 24);
3563
3564             /* The DLT value is the address of the .opd entry.  */
3565             value = (hh->opd_offset
3566                      + hppa_info->opd_sec->output_offset
3567                      + hppa_info->opd_sec->output_section->vma);
3568
3569             bfd_put_64 (hppa_info->dlt_sec->owner,
3570                         value,
3571                         hppa_info->dlt_sec->contents + hh->dlt_offset);
3572           }
3573
3574         /* We want the value of the DLT offset for this symbol, not
3575            the symbol's actual address.  Note that __gp may not point
3576            to the start of the DLT, so we have to compute the absolute
3577            address, then subtract out the value of __gp.  */
3578         value = (hh->dlt_offset
3579                  + hppa_info->dlt_sec->output_offset
3580                  + hppa_info->dlt_sec->output_section->vma);
3581         value -= _bfd_get_gp_value (output_bfd);
3582         bfd_put_32 (input_bfd, value, hit_data);
3583         return bfd_reloc_ok;
3584       }
3585
3586     case R_PARISC_LTOFF_FPTR64:
3587     case R_PARISC_LTOFF_TP64:
3588       {
3589         /* We may still need to create the FPTR itself if it was for
3590            a local symbol.  */
3591         if (eh == NULL && r_type == R_PARISC_LTOFF_FPTR64)
3592           {
3593             /* The first two words of an .opd entry are zero.  */
3594             memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
3595
3596             /* The next word is the address of the function.  */
3597             bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3598                         (hppa_info->opd_sec->contents
3599                          + hh->opd_offset + 16));
3600
3601             /* The last word is our local __gp value.  */
3602             value = _bfd_get_gp_value
3603                       (hppa_info->opd_sec->output_section->owner);
3604             bfd_put_64 (hppa_info->opd_sec->owner, value,
3605                         hppa_info->opd_sec->contents + hh->opd_offset + 24);
3606
3607             /* The DLT value is the address of the .opd entry.  */
3608             value = (hh->opd_offset
3609                      + hppa_info->opd_sec->output_offset
3610                      + hppa_info->opd_sec->output_section->vma);
3611
3612             bfd_put_64 (hppa_info->dlt_sec->owner,
3613                         value,
3614                         hppa_info->dlt_sec->contents + hh->dlt_offset);
3615           }
3616
3617         /* We want the value of the DLT offset for this symbol, not
3618            the symbol's actual address.  Note that __gp may not point
3619            to the start of the DLT, so we have to compute the absolute
3620            address, then subtract out the value of __gp.  */
3621         value = (hh->dlt_offset
3622                  + hppa_info->dlt_sec->output_offset
3623                  + hppa_info->dlt_sec->output_section->vma);
3624         value -= _bfd_get_gp_value (output_bfd);
3625         bfd_put_64 (input_bfd, value, hit_data);
3626         return bfd_reloc_ok;
3627       }
3628
3629     case R_PARISC_DIR32:
3630       bfd_put_32 (input_bfd, value + addend, hit_data);
3631       return bfd_reloc_ok;
3632
3633     case R_PARISC_DIR64:
3634       bfd_put_64 (input_bfd, value + addend, hit_data);
3635       return bfd_reloc_ok;
3636
3637     case R_PARISC_GPREL64:
3638       /* Subtract out the global pointer value to make value a DLT
3639          relative address.  */
3640       value -= _bfd_get_gp_value (output_bfd);
3641
3642       bfd_put_64 (input_bfd, value + addend, hit_data);
3643       return bfd_reloc_ok;
3644
3645     case R_PARISC_LTOFF64:
3646         /* We want the value of the DLT offset for this symbol, not
3647            the symbol's actual address.  Note that __gp may not point
3648            to the start of the DLT, so we have to compute the absolute
3649            address, then subtract out the value of __gp.  */
3650       value = (hh->dlt_offset
3651                + hppa_info->dlt_sec->output_offset
3652                + hppa_info->dlt_sec->output_section->vma);
3653       value -= _bfd_get_gp_value (output_bfd);
3654
3655       bfd_put_64 (input_bfd, value + addend, hit_data);
3656       return bfd_reloc_ok;
3657
3658     case R_PARISC_PCREL32:
3659       {
3660         /* If this is a call to a function defined in another dynamic
3661            library, then redirect the call to the local stub for this
3662            function.  */
3663         if (sym_sec == NULL || sym_sec->output_section == NULL)
3664           value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3665                    + hppa_info->stub_sec->output_section->vma);
3666
3667         /* Turn VALUE into a proper PC relative address.  */
3668         value -= (offset + input_section->output_offset
3669                   + input_section->output_section->vma);
3670
3671         value += addend;
3672         value -= 8;
3673         bfd_put_32 (input_bfd, value, hit_data);
3674         return bfd_reloc_ok;
3675       }
3676
3677     case R_PARISC_PCREL64:
3678       {
3679         /* If this is a call to a function defined in another dynamic
3680            library, then redirect the call to the local stub for this
3681            function.  */
3682         if (sym_sec == NULL || sym_sec->output_section == NULL)
3683           value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3684                    + hppa_info->stub_sec->output_section->vma);
3685
3686         /* Turn VALUE into a proper PC relative address.  */
3687         value -= (offset + input_section->output_offset
3688                   + input_section->output_section->vma);
3689
3690         value += addend;
3691         value -= 8;
3692         bfd_put_64 (input_bfd, value, hit_data);
3693         return bfd_reloc_ok;
3694       }
3695
3696     case R_PARISC_FPTR64:
3697       {
3698         bfd_vma off;
3699
3700         /* We may still need to create the FPTR itself if it was for
3701            a local symbol.  */
3702         if (hh == NULL)
3703           {
3704             bfd_vma *local_opd_offsets;
3705
3706             if (local_offsets == NULL)
3707               abort ();
3708
3709             local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3710             off = local_opd_offsets[r_symndx];
3711
3712             /* The last bit records whether we've already initialised
3713                this local .opd entry.  */
3714             if ((off & 1) != 0)
3715               {
3716                 BFD_ASSERT (off != (bfd_vma) -1);
3717                 off &= ~1;
3718               }
3719             else
3720               {
3721                 /* The first two words of an .opd entry are zero.  */
3722                 memset (hppa_info->opd_sec->contents + off, 0, 16);
3723
3724                 /* The next word is the address of the function.  */
3725                 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3726                             (hppa_info->opd_sec->contents + off + 16));
3727
3728                 /* The last word is our local __gp value.  */
3729                 value = _bfd_get_gp_value
3730                           (hppa_info->opd_sec->output_section->owner);
3731                 bfd_put_64 (hppa_info->opd_sec->owner, value,
3732                             hppa_info->opd_sec->contents + off + 24);
3733               }
3734           }
3735         else
3736           off = hh->opd_offset;
3737
3738         if (hh == NULL || hh->want_opd)
3739           /* We want the value of the OPD offset for this symbol.  */
3740           value = (off
3741                    + hppa_info->opd_sec->output_offset
3742                    + hppa_info->opd_sec->output_section->vma);
3743         else
3744           /* We want the address of the symbol.  */
3745           value += addend;
3746
3747         bfd_put_64 (input_bfd, value, hit_data);
3748         return bfd_reloc_ok;
3749       }
3750
3751     case R_PARISC_SECREL32:
3752       if (sym_sec)
3753         value -= sym_sec->output_section->vma;
3754       bfd_put_32 (input_bfd, value + addend, hit_data);
3755       return bfd_reloc_ok;
3756
3757     case R_PARISC_SEGREL32:
3758     case R_PARISC_SEGREL64:
3759       {
3760         /* If this is the first SEGREL relocation, then initialize
3761            the segment base values.  */
3762         if (hppa_info->text_segment_base == (bfd_vma) -1)
3763           bfd_map_over_sections (output_bfd, elf_hppa_record_segment_addrs,
3764                                  hppa_info);
3765
3766         /* VALUE holds the absolute address.  We want to include the
3767            addend, then turn it into a segment relative address.
3768
3769            The segment is derived from SYM_SEC.  We assume that there are
3770            only two segments of note in the resulting executable/shlib.
3771            A readonly segment (.text) and a readwrite segment (.data).  */
3772         value += addend;
3773
3774         if (sym_sec->flags & SEC_CODE)
3775           value -= hppa_info->text_segment_base;
3776         else
3777           value -= hppa_info->data_segment_base;
3778
3779         if (r_type == R_PARISC_SEGREL32)
3780           bfd_put_32 (input_bfd, value, hit_data);
3781         else
3782           bfd_put_64 (input_bfd, value, hit_data);
3783         return bfd_reloc_ok;
3784       }
3785
3786     /* Something we don't know how to handle.  */
3787     default:
3788       return bfd_reloc_notsupported;
3789     }
3790
3791   /* Update the instruction word.  */
3792   bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3793   return bfd_reloc_ok;
3794 }
3795
3796 /* Relocate an HPPA ELF section.  */
3797
3798 static bfd_boolean
3799 elf64_hppa_relocate_section (bfd *output_bfd,
3800                            struct bfd_link_info *info,
3801                            bfd *input_bfd,
3802                            asection *input_section,
3803                            bfd_byte *contents,
3804                            Elf_Internal_Rela *relocs,
3805                            Elf_Internal_Sym *local_syms,
3806                            asection **local_sections)
3807 {
3808   Elf_Internal_Shdr *symtab_hdr;
3809   Elf_Internal_Rela *rel;
3810   Elf_Internal_Rela *relend;
3811   struct elf64_hppa_link_hash_table *hppa_info;
3812
3813   hppa_info = hppa_link_hash_table (info);
3814   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3815
3816   rel = relocs;
3817   relend = relocs + input_section->reloc_count;
3818   for (; rel < relend; rel++)
3819     {
3820       int r_type;
3821       reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3822       unsigned long r_symndx;
3823       struct elf_link_hash_entry *eh;
3824       Elf_Internal_Sym *sym;
3825       asection *sym_sec;
3826       bfd_vma relocation;
3827       bfd_reloc_status_type r;
3828       bfd_boolean warned_undef;
3829
3830       r_type = ELF_R_TYPE (rel->r_info);
3831       if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
3832         {
3833           bfd_set_error (bfd_error_bad_value);
3834           return FALSE;
3835         }
3836       if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3837           || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3838         continue;
3839
3840       /* This is a final link.  */
3841       r_symndx = ELF_R_SYM (rel->r_info);
3842       eh = NULL;
3843       sym = NULL;
3844       sym_sec = NULL;
3845       warned_undef = FALSE;
3846       if (r_symndx < symtab_hdr->sh_info)
3847         {
3848           /* This is a local symbol, hh defaults to NULL.  */
3849           sym = local_syms + r_symndx;
3850           sym_sec = local_sections[r_symndx];
3851           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3852         }
3853       else
3854         {
3855           /* This is not a local symbol.  */
3856           bfd_boolean unresolved_reloc;
3857           struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3858
3859           /* It seems this can happen with erroneous or unsupported 
3860              input (mixing a.out and elf in an archive, for example.)  */
3861           if (sym_hashes == NULL)
3862             return FALSE;
3863
3864           eh = sym_hashes[r_symndx - symtab_hdr->sh_info];
3865
3866           while (eh->root.type == bfd_link_hash_indirect 
3867                  || eh->root.type == bfd_link_hash_warning)
3868             eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3869
3870           warned_undef = FALSE;
3871           unresolved_reloc = FALSE;
3872           relocation = 0;
3873           if (eh->root.type == bfd_link_hash_defined
3874               || eh->root.type == bfd_link_hash_defweak)
3875             {
3876               sym_sec = eh->root.u.def.section;
3877               if (sym_sec == NULL
3878                   || sym_sec->output_section == NULL)
3879                 /* Set a flag that will be cleared later if we find a
3880                    relocation value for this symbol.  output_section
3881                    is typically NULL for symbols satisfied by a shared
3882                    library.  */
3883                 unresolved_reloc = TRUE;
3884               else
3885                 relocation = (eh->root.u.def.value
3886                               + sym_sec->output_section->vma
3887                               + sym_sec->output_offset);
3888             }
3889           else if (eh->root.type == bfd_link_hash_undefweak)
3890             ;
3891           else if (info->unresolved_syms_in_objects == RM_IGNORE
3892                    && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
3893             ;
3894           else if (!info->relocatable
3895                    && elf_hppa_is_dynamic_loader_symbol (eh->root.root.string))
3896             continue;
3897           else if (!info->relocatable)
3898             {
3899               bfd_boolean err;
3900               err = (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
3901                      || ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT);
3902               if (!info->callbacks->undefined_symbol (info,
3903                                                       eh->root.root.string,
3904                                                       input_bfd,
3905                                                       input_section,
3906                                                       rel->r_offset, err))
3907                 return FALSE;
3908               warned_undef = TRUE;
3909             }
3910
3911           if (!info->relocatable
3912               && relocation == 0
3913               && eh->root.type != bfd_link_hash_defined
3914               && eh->root.type != bfd_link_hash_defweak
3915               && eh->root.type != bfd_link_hash_undefweak)
3916             {
3917               if (info->unresolved_syms_in_objects == RM_IGNORE
3918                   && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3919                   && eh->type == STT_PARISC_MILLI)
3920                 {
3921                   if (! info->callbacks->undefined_symbol
3922                       (info, eh_name (eh), input_bfd,
3923                        input_section, rel->r_offset, FALSE))
3924                     return FALSE;
3925                   warned_undef = TRUE;
3926                 }
3927             }
3928         }
3929
3930       if (sym_sec != NULL && elf_discarded_section (sym_sec))
3931         {
3932           /* For relocs against symbols from removed linkonce sections,
3933              or sections discarded by a linker script, we just want the
3934              section contents zeroed.  Avoid any special processing.  */
3935           _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
3936           rel->r_info = 0;
3937           rel->r_addend = 0;
3938           continue;
3939         }
3940
3941       if (info->relocatable)
3942         continue;
3943
3944       r = elf_hppa_final_link_relocate (rel, input_bfd, output_bfd,
3945                                         input_section, contents,
3946                                         relocation, info, sym_sec,
3947                                         eh);
3948
3949       if (r != bfd_reloc_ok)
3950         {
3951           switch (r)
3952             {
3953             default:
3954               abort ();
3955             case bfd_reloc_overflow:
3956               {
3957                 const char *sym_name;
3958
3959                 if (eh != NULL)
3960                   sym_name = NULL;
3961                 else
3962                   {
3963                     sym_name = bfd_elf_string_from_elf_section (input_bfd,
3964                                                                 symtab_hdr->sh_link,
3965                                                                 sym->st_name);
3966                     if (sym_name == NULL)
3967                       return FALSE;
3968                     if (*sym_name == '\0')
3969                       sym_name = bfd_section_name (input_bfd, sym_sec);
3970                   }
3971
3972                 if (!((*info->callbacks->reloc_overflow)
3973                       (info, (eh ? &eh->root : NULL), sym_name,
3974                        howto->name, (bfd_vma) 0, input_bfd,
3975                        input_section, rel->r_offset)))
3976                   return FALSE;
3977               }
3978               break;
3979             }
3980         }
3981     }
3982   return TRUE;
3983 }
3984
3985 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
3986 {
3987   { STRING_COMMA_LEN (".fini"),  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3988   { STRING_COMMA_LEN (".init"),  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3989   { STRING_COMMA_LEN (".plt"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3990   { STRING_COMMA_LEN (".dlt"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3991   { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3992   { STRING_COMMA_LEN (".sbss"),  0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3993   { STRING_COMMA_LEN (".tbss"),  0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
3994   { NULL,                    0,  0, 0,            0 }
3995 };
3996
3997 /* The hash bucket size is the standard one, namely 4.  */
3998
3999 const struct elf_size_info hppa64_elf_size_info =
4000 {
4001   sizeof (Elf64_External_Ehdr),
4002   sizeof (Elf64_External_Phdr),
4003   sizeof (Elf64_External_Shdr),
4004   sizeof (Elf64_External_Rel),
4005   sizeof (Elf64_External_Rela),
4006   sizeof (Elf64_External_Sym),
4007   sizeof (Elf64_External_Dyn),
4008   sizeof (Elf_External_Note),
4009   4,
4010   1,
4011   64, 3,
4012   ELFCLASS64, EV_CURRENT,
4013   bfd_elf64_write_out_phdrs,
4014   bfd_elf64_write_shdrs_and_ehdr,
4015   bfd_elf64_checksum_contents,
4016   bfd_elf64_write_relocs,
4017   bfd_elf64_swap_symbol_in,
4018   bfd_elf64_swap_symbol_out,
4019   bfd_elf64_slurp_reloc_table,
4020   bfd_elf64_slurp_symbol_table,
4021   bfd_elf64_swap_dyn_in,
4022   bfd_elf64_swap_dyn_out,
4023   bfd_elf64_swap_reloc_in,
4024   bfd_elf64_swap_reloc_out,
4025   bfd_elf64_swap_reloca_in,
4026   bfd_elf64_swap_reloca_out
4027 };
4028
4029 #define TARGET_BIG_SYM                  bfd_elf64_hppa_vec
4030 #define TARGET_BIG_NAME                 "elf64-hppa"
4031 #define ELF_ARCH                        bfd_arch_hppa
4032 #define ELF_MACHINE_CODE                EM_PARISC
4033 /* This is not strictly correct.  The maximum page size for PA2.0 is
4034    64M.  But everything still uses 4k.  */
4035 #define ELF_MAXPAGESIZE                 0x1000
4036 #define ELF_OSABI                       ELFOSABI_HPUX
4037
4038 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4039 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
4040 #define bfd_elf64_bfd_is_local_label_name       elf_hppa_is_local_label_name
4041 #define elf_info_to_howto               elf_hppa_info_to_howto
4042 #define elf_info_to_howto_rel           elf_hppa_info_to_howto_rel
4043
4044 #define elf_backend_section_from_shdr   elf64_hppa_section_from_shdr
4045 #define elf_backend_object_p            elf64_hppa_object_p
4046 #define elf_backend_final_write_processing \
4047                                         elf_hppa_final_write_processing
4048 #define elf_backend_fake_sections       elf_hppa_fake_sections
4049 #define elf_backend_add_symbol_hook     elf_hppa_add_symbol_hook
4050
4051 #define elf_backend_relocate_section    elf_hppa_relocate_section
4052
4053 #define bfd_elf64_bfd_final_link        elf_hppa_final_link
4054
4055 #define elf_backend_create_dynamic_sections \
4056                                         elf64_hppa_create_dynamic_sections
4057 #define elf_backend_post_process_headers        elf64_hppa_post_process_headers
4058
4059 #define elf_backend_omit_section_dynsym \
4060   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4061 #define elf_backend_adjust_dynamic_symbol \
4062                                         elf64_hppa_adjust_dynamic_symbol
4063
4064 #define elf_backend_size_dynamic_sections \
4065                                         elf64_hppa_size_dynamic_sections
4066
4067 #define elf_backend_finish_dynamic_symbol \
4068                                         elf64_hppa_finish_dynamic_symbol
4069 #define elf_backend_finish_dynamic_sections \
4070                                         elf64_hppa_finish_dynamic_sections
4071 #define elf_backend_grok_prstatus       elf64_hppa_grok_prstatus
4072 #define elf_backend_grok_psinfo         elf64_hppa_grok_psinfo
4073  
4074 /* Stuff for the BFD linker: */
4075 #define bfd_elf64_bfd_link_hash_table_create \
4076         elf64_hppa_hash_table_create
4077
4078 #define elf_backend_check_relocs \
4079         elf64_hppa_check_relocs
4080
4081 #define elf_backend_size_info \
4082   hppa64_elf_size_info
4083
4084 #define elf_backend_additional_program_headers \
4085         elf64_hppa_additional_program_headers
4086
4087 #define elf_backend_modify_segment_map \
4088         elf64_hppa_modify_segment_map
4089
4090 #define elf_backend_link_output_symbol_hook \
4091         elf64_hppa_link_output_symbol_hook
4092
4093 #define elf_backend_want_got_plt        0
4094 #define elf_backend_plt_readonly        0
4095 #define elf_backend_want_plt_sym        0
4096 #define elf_backend_got_header_size     0
4097 #define elf_backend_type_change_ok      TRUE
4098 #define elf_backend_get_symbol_type     elf64_hppa_elf_get_symbol_type
4099 #define elf_backend_reloc_type_class    elf64_hppa_reloc_type_class
4100 #define elf_backend_rela_normal         1
4101 #define elf_backend_special_sections    elf64_hppa_special_sections
4102 #define elf_backend_action_discarded    elf_hppa_action_discarded
4103 #define elf_backend_section_from_phdr   elf64_hppa_section_from_phdr
4104
4105 #define elf64_bed                       elf64_hppa_hpux_bed
4106
4107 #include "elf64-target.h"
4108
4109 #undef TARGET_BIG_SYM
4110 #define TARGET_BIG_SYM                  bfd_elf64_hppa_linux_vec
4111 #undef TARGET_BIG_NAME
4112 #define TARGET_BIG_NAME                 "elf64-hppa-linux"
4113 #undef ELF_OSABI
4114 #define ELF_OSABI                       ELFOSABI_LINUX
4115 #undef elf_backend_post_process_headers
4116 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4117 #undef elf64_bed
4118 #define elf64_bed                       elf64_hppa_linux_bed
4119
4120 #include "elf64-target.h"