1 /* Support for HPPA 64-bit ELF
2    Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 2 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, MA 02110-1301, USA.  */
20 
21 #include "alloca-conf.h"
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/hppa.h"
27 #include "libhppa.h"
28 #include "elf64-hppa.h"
29 #define ARCH_SIZE	       64
30 
31 #define PLT_ENTRY_SIZE 0x10
32 #define DLT_ENTRY_SIZE 0x8
33 #define OPD_ENTRY_SIZE 0x20
34 
35 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
36 
37 /* The stub is supposed to load the target address and target's DP
38    value out of the PLT, then do an external branch to the target
39    address.
40 
41    LDD PLTOFF(%r27),%r1
42    BVE (%r1)
43    LDD PLTOFF+8(%r27),%r27
44 
45    Note that we must use the LDD with a 14 bit displacement, not the one
46    with a 5 bit displacement.  */
47 static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
48 			  0x53, 0x7b, 0x00, 0x00 };
49 
50 struct elf64_hppa_dyn_hash_entry
51 {
52   struct bfd_hash_entry root;
53 
54   /* Offsets for this symbol in various linker sections.  */
55   bfd_vma dlt_offset;
56   bfd_vma plt_offset;
57   bfd_vma opd_offset;
58   bfd_vma stub_offset;
59 
60   /* The symbol table entry, if any, that this was derived from.  */
61   struct elf_link_hash_entry *h;
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     /* The index of the section symbol for the input section of
92        the relocation.  Only needed when building shared libraries.  */
93     int sec_symndx;
94 
95     /* The offset within the input section of the relocation.  */
96     bfd_vma offset;
97 
98     /* The addend for the relocation.  */
99     bfd_vma addend;
100 
101   } *reloc_entries;
102 
103   /* Nonzero if this symbol needs an entry in one of the linker
104      sections.  */
105   unsigned want_dlt;
106   unsigned want_plt;
107   unsigned want_opd;
108   unsigned want_stub;
109 };
110 
111 struct elf64_hppa_dyn_hash_table
112 {
113   struct bfd_hash_table root;
114 };
115 
116 struct elf64_hppa_link_hash_table
117 {
118   struct elf_link_hash_table root;
119 
120   /* Shortcuts to get to the various linker defined sections.  */
121   asection *dlt_sec;
122   asection *dlt_rel_sec;
123   asection *plt_sec;
124   asection *plt_rel_sec;
125   asection *opd_sec;
126   asection *opd_rel_sec;
127   asection *other_rel_sec;
128 
129   /* Offset of __gp within .plt section.  When the PLT gets large we want
130      to slide __gp into the PLT section so that we can continue to use
131      single DP relative instructions to load values out of the PLT.  */
132   bfd_vma gp_offset;
133 
134   /* Note this is not strictly correct.  We should create a stub section for
135      each input section with calls.  The stub section should be placed before
136      the section with the call.  */
137   asection *stub_sec;
138 
139   bfd_vma text_segment_base;
140   bfd_vma data_segment_base;
141 
142   struct elf64_hppa_dyn_hash_table dyn_hash_table;
143 
144   /* We build tables to map from an input section back to its
145      symbol index.  This is the BFD for which we currently have
146      a map.  */
147   bfd *section_syms_bfd;
148 
149   /* Array of symbol numbers for each input section attached to the
150      current BFD.  */
151   int *section_syms;
152 };
153 
154 #define elf64_hppa_hash_table(p) \
155   ((struct elf64_hppa_link_hash_table *) ((p)->hash))
156 
157 typedef struct bfd_hash_entry *(*new_hash_entry_func)
158   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
159 
160 static struct bfd_hash_entry *elf64_hppa_new_dyn_hash_entry
161   PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
162 	   const char *string));
163 static struct bfd_link_hash_table *elf64_hppa_hash_table_create
164   PARAMS ((bfd *abfd));
165 static struct elf64_hppa_dyn_hash_entry *elf64_hppa_dyn_hash_lookup
166   PARAMS ((struct elf64_hppa_dyn_hash_table *table, const char *string,
167 	   bfd_boolean create, bfd_boolean copy));
168 static void elf64_hppa_dyn_hash_traverse
169   PARAMS ((struct elf64_hppa_dyn_hash_table *table,
170 	   bfd_boolean (*func) (struct elf64_hppa_dyn_hash_entry *, PTR),
171 	   PTR info));
172 
173 static const char *get_dyn_name
174   PARAMS ((bfd *, struct elf_link_hash_entry *,
175 	   const Elf_Internal_Rela *, char **, size_t *));
176 
177 /* This must follow the definitions of the various derived linker
178    hash tables and shared functions.  */
179 #include "elf-hppa.h"
180 
181 static bfd_boolean elf64_hppa_object_p
182   PARAMS ((bfd *));
183 
184 static void elf64_hppa_post_process_headers
185   PARAMS ((bfd *, struct bfd_link_info *));
186 
187 static bfd_boolean elf64_hppa_create_dynamic_sections
188   PARAMS ((bfd *, struct bfd_link_info *));
189 
190 static bfd_boolean elf64_hppa_adjust_dynamic_symbol
191   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
192 
193 static bfd_boolean elf64_hppa_mark_milli_and_exported_functions
194   PARAMS ((struct elf_link_hash_entry *, PTR));
195 
196 static bfd_boolean elf64_hppa_size_dynamic_sections
197   PARAMS ((bfd *, struct bfd_link_info *));
198 
199 static bfd_boolean elf64_hppa_link_output_symbol_hook
200   PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
201 	   asection *, struct elf_link_hash_entry *));
202 
203 static bfd_boolean elf64_hppa_finish_dynamic_symbol
204   PARAMS ((bfd *, struct bfd_link_info *,
205 	   struct elf_link_hash_entry *, Elf_Internal_Sym *));
206 
207 static int elf64_hppa_additional_program_headers
208   PARAMS ((bfd *));
209 
210 static bfd_boolean elf64_hppa_modify_segment_map
211   PARAMS ((bfd *, struct bfd_link_info *));
212 
213 static enum elf_reloc_type_class elf64_hppa_reloc_type_class
214   PARAMS ((const Elf_Internal_Rela *));
215 
216 static bfd_boolean elf64_hppa_finish_dynamic_sections
217   PARAMS ((bfd *, struct bfd_link_info *));
218 
219 static bfd_boolean elf64_hppa_check_relocs
220   PARAMS ((bfd *, struct bfd_link_info *,
221 	   asection *, const Elf_Internal_Rela *));
222 
223 static bfd_boolean elf64_hppa_dynamic_symbol_p
224   PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
225 
226 static bfd_boolean elf64_hppa_mark_exported_functions
227   PARAMS ((struct elf_link_hash_entry *, PTR));
228 
229 static bfd_boolean elf64_hppa_finalize_opd
230   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
231 
232 static bfd_boolean elf64_hppa_finalize_dlt
233   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
234 
235 static bfd_boolean allocate_global_data_dlt
236   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
237 
238 static bfd_boolean allocate_global_data_plt
239   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
240 
241 static bfd_boolean allocate_global_data_stub
242   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
243 
244 static bfd_boolean allocate_global_data_opd
245   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
246 
247 static bfd_boolean get_reloc_section
248   PARAMS ((bfd *, struct elf64_hppa_link_hash_table *, asection *));
249 
250 static bfd_boolean count_dyn_reloc
251   PARAMS ((bfd *, struct elf64_hppa_dyn_hash_entry *,
252 	   int, asection *, int, bfd_vma, bfd_vma));
253 
254 static bfd_boolean allocate_dynrel_entries
255   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
256 
257 static bfd_boolean elf64_hppa_finalize_dynreloc
258   PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
259 
260 static bfd_boolean get_opd
261   PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
262 
263 static bfd_boolean get_plt
264   PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
265 
266 static bfd_boolean get_dlt
267   PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
268 
269 static bfd_boolean get_stub
270   PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
271 
272 static int elf64_hppa_elf_get_symbol_type
273   PARAMS ((Elf_Internal_Sym *, int));
274 
275 static bfd_boolean
276 elf64_hppa_dyn_hash_table_init (struct elf64_hppa_dyn_hash_table *ht,
277 				bfd *abfd ATTRIBUTE_UNUSED,
278 				new_hash_entry_func new,
279 				unsigned int entsize)
280 {
281   memset (ht, 0, sizeof (*ht));
282   return bfd_hash_table_init (&ht->root, new, entsize);
283 }
284 
285 static struct bfd_hash_entry*
286 elf64_hppa_new_dyn_hash_entry (entry, table, string)
287      struct bfd_hash_entry *entry;
288      struct bfd_hash_table *table;
289      const char *string;
290 {
291   struct elf64_hppa_dyn_hash_entry *ret;
292   ret = (struct elf64_hppa_dyn_hash_entry *) entry;
293 
294   /* Allocate the structure if it has not already been allocated by a
295      subclass.  */
296   if (!ret)
297     ret = bfd_hash_allocate (table, sizeof (*ret));
298 
299   if (!ret)
300     return 0;
301 
302   /* Call the allocation method of the superclass.  */
303   ret = ((struct elf64_hppa_dyn_hash_entry *)
304 	 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
305 
306   /* Initialize our local data.  All zeros.  */
307   memset (&ret->dlt_offset, 0,
308 	  (sizeof (struct elf64_hppa_dyn_hash_entry)
309 	   - offsetof (struct elf64_hppa_dyn_hash_entry, dlt_offset)));
310 
311   return &ret->root;
312 }
313 
314 /* Create the derived linker hash table.  The PA64 ELF port uses this
315    derived hash table to keep information specific to the PA ElF
316    linker (without using static variables).  */
317 
318 static struct bfd_link_hash_table*
319 elf64_hppa_hash_table_create (abfd)
320      bfd *abfd;
321 {
322   struct elf64_hppa_link_hash_table *ret;
323 
324   ret = bfd_zalloc (abfd, (bfd_size_type) sizeof (*ret));
325   if (!ret)
326     return 0;
327   if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
328 				      _bfd_elf_link_hash_newfunc,
329 				      sizeof (struct elf_link_hash_entry)))
330     {
331       bfd_release (abfd, ret);
332       return 0;
333     }
334 
335   if (!elf64_hppa_dyn_hash_table_init (&ret->dyn_hash_table, abfd,
336 				       elf64_hppa_new_dyn_hash_entry,
337 				       sizeof (struct elf64_hppa_dyn_hash_entry)))
338     return 0;
339   return &ret->root.root;
340 }
341 
342 /* Look up an entry in a PA64 ELF linker hash table.  */
343 
344 static struct elf64_hppa_dyn_hash_entry *
345 elf64_hppa_dyn_hash_lookup(table, string, create, copy)
346      struct elf64_hppa_dyn_hash_table *table;
347      const char *string;
348      bfd_boolean create, copy;
349 {
350   return ((struct elf64_hppa_dyn_hash_entry *)
351 	  bfd_hash_lookup (&table->root, string, create, copy));
352 }
353 
354 /* Traverse a PA64 ELF linker hash table.  */
355 
356 static void
357 elf64_hppa_dyn_hash_traverse (table, func, info)
358      struct elf64_hppa_dyn_hash_table *table;
359      bfd_boolean (*func) PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
360      PTR info;
361 {
362   (bfd_hash_traverse
363    (&table->root,
364     (bfd_boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) func,
365     info));
366 }
367 
368 /* Return nonzero if ABFD represents a PA2.0 ELF64 file.
369 
370    Additionally we set the default architecture and machine.  */
371 static bfd_boolean
372 elf64_hppa_object_p (abfd)
373      bfd *abfd;
374 {
375   Elf_Internal_Ehdr * i_ehdrp;
376   unsigned int flags;
377 
378   i_ehdrp = elf_elfheader (abfd);
379   if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
380     {
381       /* GCC on hppa-linux produces binaries with OSABI=Linux,
382 	 but the kernel produces corefiles with OSABI=SysV.  */
383       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX
384 	  && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
385 	return FALSE;
386     }
387   else
388     {
389       /* HPUX produces binaries with OSABI=HPUX,
390 	 but the kernel produces corefiles with OSABI=SysV.  */
391       if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
392 	  && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
393 	return FALSE;
394     }
395 
396   flags = i_ehdrp->e_flags;
397   switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
398     {
399     case EFA_PARISC_1_0:
400       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
401     case EFA_PARISC_1_1:
402       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
403     case EFA_PARISC_2_0:
404       if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
405         return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
406       else
407         return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
408     case EFA_PARISC_2_0 | EF_PARISC_WIDE:
409       return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
410     }
411   /* Don't be fussy.  */
412   return TRUE;
413 }
414 
415 /* Given section type (hdr->sh_type), return a boolean indicating
416    whether or not the section is an elf64-hppa specific section.  */
417 static bfd_boolean
418 elf64_hppa_section_from_shdr (bfd *abfd,
419 			      Elf_Internal_Shdr *hdr,
420 			      const char *name,
421 			      int shindex)
422 {
423   asection *newsect;
424 
425   switch (hdr->sh_type)
426     {
427     case SHT_PARISC_EXT:
428       if (strcmp (name, ".PARISC.archext") != 0)
429 	return FALSE;
430       break;
431     case SHT_PARISC_UNWIND:
432       if (strcmp (name, ".PARISC.unwind") != 0)
433 	return FALSE;
434       break;
435     case SHT_PARISC_DOC:
436     case SHT_PARISC_ANNOT:
437     default:
438       return FALSE;
439     }
440 
441   if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
442     return FALSE;
443   newsect = hdr->bfd_section;
444 
445   return TRUE;
446 }
447 
448 /* Construct a string for use in the elf64_hppa_dyn_hash_table.  The
449    name describes what was once potentially anonymous memory.  We
450    allocate memory as necessary, possibly reusing PBUF/PLEN.  */
451 
452 static const char *
453 get_dyn_name (abfd, h, rel, pbuf, plen)
454      bfd *abfd;
455      struct elf_link_hash_entry *h;
456      const Elf_Internal_Rela *rel;
457      char **pbuf;
458      size_t *plen;
459 {
460   asection *sec = abfd->sections;
461   size_t nlen, tlen;
462   char *buf;
463   size_t len;
464 
465   if (h && rel->r_addend == 0)
466     return h->root.root.string;
467 
468   if (h)
469     nlen = strlen (h->root.root.string);
470   else
471     nlen = 8 + 1 + sizeof (rel->r_info) * 2 - 8;
472   tlen = nlen + 1 + sizeof (rel->r_addend) * 2 + 1;
473 
474   len = *plen;
475   buf = *pbuf;
476   if (len < tlen)
477     {
478       if (buf)
479 	free (buf);
480       *pbuf = buf = malloc (tlen);
481       *plen = len = tlen;
482       if (!buf)
483 	return NULL;
484     }
485 
486   if (h)
487     {
488       memcpy (buf, h->root.root.string, nlen);
489       buf[nlen++] = '+';
490       sprintf_vma (buf + nlen, rel->r_addend);
491     }
492   else
493     {
494       nlen = sprintf (buf, "%x:%lx",
495 		      sec->id & 0xffffffff,
496 		      (long) ELF64_R_SYM (rel->r_info));
497       if (rel->r_addend)
498 	{
499 	  buf[nlen++] = '+';
500 	  sprintf_vma (buf + nlen, rel->r_addend);
501 	}
502     }
503 
504   return buf;
505 }
506 
507 /* SEC is a section containing relocs for an input BFD when linking; return
508    a suitable section for holding relocs in the output BFD for a link.  */
509 
510 static bfd_boolean
511 get_reloc_section (abfd, hppa_info, sec)
512      bfd *abfd;
513      struct elf64_hppa_link_hash_table *hppa_info;
514      asection *sec;
515 {
516   const char *srel_name;
517   asection *srel;
518   bfd *dynobj;
519 
520   srel_name = (bfd_elf_string_from_elf_section
521 	       (abfd, elf_elfheader(abfd)->e_shstrndx,
522 		elf_section_data(sec)->rel_hdr.sh_name));
523   if (srel_name == NULL)
524     return FALSE;
525 
526   BFD_ASSERT ((strncmp (srel_name, ".rela", 5) == 0
527 	       && strcmp (bfd_get_section_name (abfd, sec),
528 			  srel_name+5) == 0)
529 	      || (strncmp (srel_name, ".rel", 4) == 0
530 		  && strcmp (bfd_get_section_name (abfd, sec),
531 			     srel_name+4) == 0));
532 
533   dynobj = hppa_info->root.dynobj;
534   if (!dynobj)
535     hppa_info->root.dynobj = dynobj = abfd;
536 
537   srel = bfd_get_section_by_name (dynobj, srel_name);
538   if (srel == NULL)
539     {
540       srel = bfd_make_section_with_flags (dynobj, srel_name,
541 					  (SEC_ALLOC
542 					   | SEC_LOAD
543 					   | SEC_HAS_CONTENTS
544 					   | SEC_IN_MEMORY
545 					   | SEC_LINKER_CREATED
546 					   | SEC_READONLY));
547       if (srel == NULL
548 	  || !bfd_set_section_alignment (dynobj, srel, 3))
549 	return FALSE;
550     }
551 
552   hppa_info->other_rel_sec = srel;
553   return TRUE;
554 }
555 
556 /* Add a new entry to the list of dynamic relocations against DYN_H.
557 
558    We use this to keep a record of all the FPTR relocations against a
559    particular symbol so that we can create FPTR relocations in the
560    output file.  */
561 
562 static bfd_boolean
563 count_dyn_reloc (abfd, dyn_h, type, sec, sec_symndx, offset, addend)
564      bfd *abfd;
565      struct elf64_hppa_dyn_hash_entry *dyn_h;
566      int type;
567      asection *sec;
568      int sec_symndx;
569      bfd_vma offset;
570      bfd_vma addend;
571 {
572   struct elf64_hppa_dyn_reloc_entry *rent;
573 
574   rent = (struct elf64_hppa_dyn_reloc_entry *)
575   bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
576   if (!rent)
577     return FALSE;
578 
579   rent->next = dyn_h->reloc_entries;
580   rent->type = type;
581   rent->sec = sec;
582   rent->sec_symndx = sec_symndx;
583   rent->offset = offset;
584   rent->addend = addend;
585   dyn_h->reloc_entries = rent;
586 
587   return TRUE;
588 }
589 
590 /* Scan the RELOCS and record the type of dynamic entries that each
591    referenced symbol needs.  */
592 
593 static bfd_boolean
594 elf64_hppa_check_relocs (abfd, info, sec, relocs)
595      bfd *abfd;
596      struct bfd_link_info *info;
597      asection *sec;
598      const Elf_Internal_Rela *relocs;
599 {
600   struct elf64_hppa_link_hash_table *hppa_info;
601   const Elf_Internal_Rela *relend;
602   Elf_Internal_Shdr *symtab_hdr;
603   const Elf_Internal_Rela *rel;
604   asection *dlt, *plt, *stubs;
605   char *buf;
606   size_t buf_len;
607   int sec_symndx;
608 
609   if (info->relocatable)
610     return TRUE;
611 
612 #if 0
613   /* If this is the first dynamic object found in the link, create
614      the special sections required for dynamic linking.  */
615   if (! elf_hash_table (info)->dynamic_sections_created)
616     {
617       if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
618 	return FALSE;
619     }
620 #endif
621 
622   hppa_info = elf64_hppa_hash_table (info);
623   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
624 
625   /* If necessary, build a new table holding section symbols indices
626      for this BFD.  */
627 
628   if (info->shared && hppa_info->section_syms_bfd != abfd)
629     {
630       unsigned long i;
631       unsigned int highest_shndx;
632       Elf_Internal_Sym *local_syms = NULL;
633       Elf_Internal_Sym *isym, *isymend;
634       bfd_size_type amt;
635 
636       /* We're done with the old cache of section index to section symbol
637 	 index information.  Free it.
638 
639 	 ?!? Note we leak the last section_syms array.  Presumably we
640 	 could free it in one of the later routines in this file.  */
641       if (hppa_info->section_syms)
642 	free (hppa_info->section_syms);
643 
644       /* Read this BFD's local symbols.  */
645       if (symtab_hdr->sh_info != 0)
646 	{
647 	  local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
648 	  if (local_syms == NULL)
649 	    local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
650 					       symtab_hdr->sh_info, 0,
651 					       NULL, NULL, NULL);
652 	  if (local_syms == NULL)
653 	    return FALSE;
654 	}
655 
656       /* Record the highest section index referenced by the local symbols.  */
657       highest_shndx = 0;
658       isymend = local_syms + symtab_hdr->sh_info;
659       for (isym = local_syms; isym < isymend; isym++)
660 	{
661 	  if (isym->st_shndx > highest_shndx)
662 	    highest_shndx = isym->st_shndx;
663 	}
664 
665       /* Allocate an array to hold the section index to section symbol index
666 	 mapping.  Bump by one since we start counting at zero.  */
667       highest_shndx++;
668       amt = highest_shndx;
669       amt *= sizeof (int);
670       hppa_info->section_syms = (int *) bfd_malloc (amt);
671 
672       /* Now walk the local symbols again.  If we find a section symbol,
673 	 record the index of the symbol into the section_syms array.  */
674       for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
675 	{
676 	  if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
677 	    hppa_info->section_syms[isym->st_shndx] = i;
678 	}
679 
680       /* We are finished with the local symbols.  */
681       if (local_syms != NULL
682 	  && symtab_hdr->contents != (unsigned char *) local_syms)
683 	{
684 	  if (! info->keep_memory)
685 	    free (local_syms);
686 	  else
687 	    {
688 	      /* Cache the symbols for elf_link_input_bfd.  */
689 	      symtab_hdr->contents = (unsigned char *) local_syms;
690 	    }
691 	}
692 
693       /* Record which BFD we built the section_syms mapping for.  */
694       hppa_info->section_syms_bfd = abfd;
695     }
696 
697   /* Record the symbol index for this input section.  We may need it for
698      relocations when building shared libraries.  When not building shared
699      libraries this value is never really used, but assign it to zero to
700      prevent out of bounds memory accesses in other routines.  */
701   if (info->shared)
702     {
703       sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
704 
705       /* If we did not find a section symbol for this section, then
706 	 something went terribly wrong above.  */
707       if (sec_symndx == -1)
708 	return FALSE;
709 
710       sec_symndx = hppa_info->section_syms[sec_symndx];
711     }
712   else
713     sec_symndx = 0;
714 
715   dlt = plt = stubs = NULL;
716   buf = NULL;
717   buf_len = 0;
718 
719   relend = relocs + sec->reloc_count;
720   for (rel = relocs; rel < relend; ++rel)
721     {
722       enum
723 	{
724 	  NEED_DLT = 1,
725 	  NEED_PLT = 2,
726 	  NEED_STUB = 4,
727 	  NEED_OPD = 8,
728 	  NEED_DYNREL = 16,
729 	};
730 
731       struct elf_link_hash_entry *h = NULL;
732       unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
733       struct elf64_hppa_dyn_hash_entry *dyn_h;
734       int need_entry;
735       const char *addr_name;
736       bfd_boolean maybe_dynamic;
737       int dynrel_type = R_PARISC_NONE;
738       static reloc_howto_type *howto;
739 
740       if (r_symndx >= symtab_hdr->sh_info)
741 	{
742 	  /* We're dealing with a global symbol -- find its hash entry
743 	     and mark it as being referenced.  */
744 	  long indx = r_symndx - symtab_hdr->sh_info;
745 	  h = elf_sym_hashes (abfd)[indx];
746 	  while (h->root.type == bfd_link_hash_indirect
747 		 || h->root.type == bfd_link_hash_warning)
748 	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
749 
750 	  h->ref_regular = 1;
751 	}
752 
753       /* We can only get preliminary data on whether a symbol is
754 	 locally or externally defined, as not all of the input files
755 	 have yet been processed.  Do something with what we know, as
756 	 this may help reduce memory usage and processing time later.  */
757       maybe_dynamic = FALSE;
758       if (h && ((info->shared
759 		 && (!info->symbolic
760 		     || info->unresolved_syms_in_shared_libs == RM_IGNORE))
761 		|| !h->def_regular
762 		|| h->root.type == bfd_link_hash_defweak))
763 	maybe_dynamic = TRUE;
764 
765       howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
766       need_entry = 0;
767       switch (howto->type)
768 	{
769 	/* These are simple indirect references to symbols through the
770 	   DLT.  We need to create a DLT entry for any symbols which
771 	   appears in a DLTIND relocation.  */
772 	case R_PARISC_DLTIND21L:
773 	case R_PARISC_DLTIND14R:
774 	case R_PARISC_DLTIND14F:
775 	case R_PARISC_DLTIND14WR:
776 	case R_PARISC_DLTIND14DR:
777 	  need_entry = NEED_DLT;
778 	  break;
779 
780 	/* ?!?  These need a DLT entry.  But I have no idea what to do with
781 	   the "link time TP value.  */
782 	case R_PARISC_LTOFF_TP21L:
783 	case R_PARISC_LTOFF_TP14R:
784 	case R_PARISC_LTOFF_TP14F:
785 	case R_PARISC_LTOFF_TP64:
786 	case R_PARISC_LTOFF_TP14WR:
787 	case R_PARISC_LTOFF_TP14DR:
788 	case R_PARISC_LTOFF_TP16F:
789 	case R_PARISC_LTOFF_TP16WF:
790 	case R_PARISC_LTOFF_TP16DF:
791 	  need_entry = NEED_DLT;
792 	  break;
793 
794 	/* These are function calls.  Depending on their precise target we
795 	   may need to make a stub for them.  The stub uses the PLT, so we
796 	   need to create PLT entries for these symbols too.  */
797 	case R_PARISC_PCREL12F:
798 	case R_PARISC_PCREL17F:
799 	case R_PARISC_PCREL22F:
800 	case R_PARISC_PCREL32:
801 	case R_PARISC_PCREL64:
802 	case R_PARISC_PCREL21L:
803 	case R_PARISC_PCREL17R:
804 	case R_PARISC_PCREL17C:
805 	case R_PARISC_PCREL14R:
806 	case R_PARISC_PCREL14F:
807 	case R_PARISC_PCREL22C:
808 	case R_PARISC_PCREL14WR:
809 	case R_PARISC_PCREL14DR:
810 	case R_PARISC_PCREL16F:
811 	case R_PARISC_PCREL16WF:
812 	case R_PARISC_PCREL16DF:
813 	  need_entry = (NEED_PLT | NEED_STUB);
814 	  break;
815 
816 	case R_PARISC_PLTOFF21L:
817 	case R_PARISC_PLTOFF14R:
818 	case R_PARISC_PLTOFF14F:
819 	case R_PARISC_PLTOFF14WR:
820 	case R_PARISC_PLTOFF14DR:
821 	case R_PARISC_PLTOFF16F:
822 	case R_PARISC_PLTOFF16WF:
823 	case R_PARISC_PLTOFF16DF:
824 	  need_entry = (NEED_PLT);
825 	  break;
826 
827 	case R_PARISC_DIR64:
828 	  if (info->shared || maybe_dynamic)
829 	    need_entry = (NEED_DYNREL);
830 	  dynrel_type = R_PARISC_DIR64;
831 	  break;
832 
833 	/* This is an indirect reference through the DLT to get the address
834 	   of a OPD descriptor.  Thus we need to make a DLT entry that points
835 	   to an OPD entry.  */
836 	case R_PARISC_LTOFF_FPTR21L:
837 	case R_PARISC_LTOFF_FPTR14R:
838 	case R_PARISC_LTOFF_FPTR14WR:
839 	case R_PARISC_LTOFF_FPTR14DR:
840 	case R_PARISC_LTOFF_FPTR32:
841 	case R_PARISC_LTOFF_FPTR64:
842 	case R_PARISC_LTOFF_FPTR16F:
843 	case R_PARISC_LTOFF_FPTR16WF:
844 	case R_PARISC_LTOFF_FPTR16DF:
845 	  if (info->shared || maybe_dynamic)
846 	    need_entry = (NEED_DLT | NEED_OPD);
847 	  else
848 	    need_entry = (NEED_DLT | NEED_OPD);
849 	  dynrel_type = R_PARISC_FPTR64;
850 	  break;
851 
852 	/* This is a simple OPD entry.  */
853 	case R_PARISC_FPTR64:
854 	  if (info->shared || maybe_dynamic)
855 	    need_entry = (NEED_OPD | NEED_DYNREL);
856 	  else
857 	    need_entry = (NEED_OPD);
858 	  dynrel_type = R_PARISC_FPTR64;
859 	  break;
860 
861 	/* Add more cases as needed.  */
862 	}
863 
864       if (!need_entry)
865 	continue;
866 
867       /* Collect a canonical name for this address.  */
868       addr_name = get_dyn_name (abfd, h, rel, &buf, &buf_len);
869 
870       /* Collect the canonical entry data for this address.  */
871       dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
872 					  addr_name, TRUE, TRUE);
873       BFD_ASSERT (dyn_h);
874 
875       /* Stash away enough information to be able to find this symbol
876 	 regardless of whether or not it is local or global.  */
877       dyn_h->h = h;
878       dyn_h->owner = abfd;
879       dyn_h->sym_indx = r_symndx;
880 
881       /* ?!? We may need to do some error checking in here.  */
882       /* Create what's needed.  */
883       if (need_entry & NEED_DLT)
884 	{
885 	  if (! hppa_info->dlt_sec
886 	      && ! get_dlt (abfd, info, hppa_info))
887 	    goto err_out;
888 	  dyn_h->want_dlt = 1;
889 	}
890 
891       if (need_entry & NEED_PLT)
892 	{
893 	  if (! hppa_info->plt_sec
894 	      && ! get_plt (abfd, info, hppa_info))
895 	    goto err_out;
896 	  dyn_h->want_plt = 1;
897 	}
898 
899       if (need_entry & NEED_STUB)
900 	{
901 	  if (! hppa_info->stub_sec
902 	      && ! get_stub (abfd, info, hppa_info))
903 	    goto err_out;
904 	  dyn_h->want_stub = 1;
905 	}
906 
907       if (need_entry & NEED_OPD)
908 	{
909 	  if (! hppa_info->opd_sec
910 	      && ! get_opd (abfd, info, hppa_info))
911 	    goto err_out;
912 
913 	  dyn_h->want_opd = 1;
914 
915 	  /* FPTRs are not allocated by the dynamic linker for PA64, though
916 	     it is possible that will change in the future.  */
917 
918 	  /* This could be a local function that had its address taken, in
919 	     which case H will be NULL.  */
920 	  if (h)
921 	    h->needs_plt = 1;
922 	}
923 
924       /* Add a new dynamic relocation to the chain of dynamic
925 	 relocations for this symbol.  */
926       if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
927 	{
928 	  if (! hppa_info->other_rel_sec
929 	      && ! get_reloc_section (abfd, hppa_info, sec))
930 	    goto err_out;
931 
932 	  if (!count_dyn_reloc (abfd, dyn_h, dynrel_type, sec,
933 				sec_symndx, rel->r_offset, rel->r_addend))
934 	    goto err_out;
935 
936 	  /* If we are building a shared library and we just recorded
937 	     a dynamic R_PARISC_FPTR64 relocation, then make sure the
938 	     section symbol for this section ends up in the dynamic
939 	     symbol table.  */
940 	  if (info->shared && dynrel_type == R_PARISC_FPTR64
941 	      && ! (bfd_elf_link_record_local_dynamic_symbol
942 		    (info, abfd, sec_symndx)))
943 	    return FALSE;
944 	}
945     }
946 
947   if (buf)
948     free (buf);
949   return TRUE;
950 
951  err_out:
952   if (buf)
953     free (buf);
954   return FALSE;
955 }
956 
957 struct elf64_hppa_allocate_data
958 {
959   struct bfd_link_info *info;
960   bfd_size_type ofs;
961 };
962 
963 /* Should we do dynamic things to this symbol?  */
964 
965 static bfd_boolean
966 elf64_hppa_dynamic_symbol_p (h, info)
967      struct elf_link_hash_entry *h;
968      struct bfd_link_info *info;
969 {
970   /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
971      and relocations that retrieve a function descriptor?  Assume the
972      worst for now.  */
973   if (_bfd_elf_dynamic_symbol_p (h, info, 1))
974     {
975       /* ??? Why is this here and not elsewhere is_local_label_name.  */
976       if (h->root.root.string[0] == '$' && h->root.root.string[1] == '$')
977 	return FALSE;
978 
979       return TRUE;
980     }
981   else
982     return FALSE;
983 }
984 
985 /* Mark all functions exported by this file so that we can later allocate
986    entries in .opd for them.  */
987 
988 static bfd_boolean
989 elf64_hppa_mark_exported_functions (h, data)
990      struct elf_link_hash_entry *h;
991      PTR data;
992 {
993   struct bfd_link_info *info = (struct bfd_link_info *)data;
994   struct elf64_hppa_link_hash_table *hppa_info;
995 
996   hppa_info = elf64_hppa_hash_table (info);
997 
998   if (h->root.type == bfd_link_hash_warning)
999     h = (struct elf_link_hash_entry *) h->root.u.i.link;
1000 
1001   if (h
1002       && (h->root.type == bfd_link_hash_defined
1003 	  || h->root.type == bfd_link_hash_defweak)
1004       && h->root.u.def.section->output_section != NULL
1005       && h->type == STT_FUNC)
1006     {
1007        struct elf64_hppa_dyn_hash_entry *dyn_h;
1008 
1009       /* Add this symbol to the PA64 linker hash table.  */
1010       dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
1011 					  h->root.root.string, TRUE, TRUE);
1012       BFD_ASSERT (dyn_h);
1013       dyn_h->h = h;
1014 
1015       if (! hppa_info->opd_sec
1016 	  && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
1017 	return FALSE;
1018 
1019       dyn_h->want_opd = 1;
1020       /* Put a flag here for output_symbol_hook.  */
1021       dyn_h->st_shndx = -1;
1022       h->needs_plt = 1;
1023     }
1024 
1025   return TRUE;
1026 }
1027 
1028 /* Allocate space for a DLT entry.  */
1029 
1030 static bfd_boolean
1031 allocate_global_data_dlt (dyn_h, data)
1032      struct elf64_hppa_dyn_hash_entry *dyn_h;
1033      PTR data;
1034 {
1035   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1036 
1037   if (dyn_h->want_dlt)
1038     {
1039       struct elf_link_hash_entry *h = dyn_h->h;
1040 
1041       if (x->info->shared)
1042 	{
1043 	  /* Possibly add the symbol to the local dynamic symbol
1044 	     table since we might need to create a dynamic relocation
1045 	     against it.  */
1046 	  if (! h
1047 	      || (h->dynindx == -1 && h->type != STT_PARISC_MILLI))
1048 	    {
1049 	      bfd *owner;
1050 	      owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
1051 
1052 	      if (! (bfd_elf_link_record_local_dynamic_symbol
1053 		     (x->info, owner, dyn_h->sym_indx)))
1054 		return FALSE;
1055 	    }
1056 	}
1057 
1058       dyn_h->dlt_offset = x->ofs;
1059       x->ofs += DLT_ENTRY_SIZE;
1060     }
1061   return TRUE;
1062 }
1063 
1064 /* Allocate space for a DLT.PLT entry.  */
1065 
1066 static bfd_boolean
1067 allocate_global_data_plt (dyn_h, data)
1068      struct elf64_hppa_dyn_hash_entry *dyn_h;
1069      PTR data;
1070 {
1071   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1072 
1073   if (dyn_h->want_plt
1074       && elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info)
1075       && !((dyn_h->h->root.type == bfd_link_hash_defined
1076 	    || dyn_h->h->root.type == bfd_link_hash_defweak)
1077 	   && dyn_h->h->root.u.def.section->output_section != NULL))
1078     {
1079       dyn_h->plt_offset = x->ofs;
1080       x->ofs += PLT_ENTRY_SIZE;
1081       if (dyn_h->plt_offset < 0x2000)
1082 	elf64_hppa_hash_table (x->info)->gp_offset = dyn_h->plt_offset;
1083     }
1084   else
1085     dyn_h->want_plt = 0;
1086 
1087   return TRUE;
1088 }
1089 
1090 /* Allocate space for a STUB entry.  */
1091 
1092 static bfd_boolean
1093 allocate_global_data_stub (dyn_h, data)
1094      struct elf64_hppa_dyn_hash_entry *dyn_h;
1095      PTR data;
1096 {
1097   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1098 
1099   if (dyn_h->want_stub
1100       && elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info)
1101       && !((dyn_h->h->root.type == bfd_link_hash_defined
1102 	    || dyn_h->h->root.type == bfd_link_hash_defweak)
1103 	   && dyn_h->h->root.u.def.section->output_section != NULL))
1104     {
1105       dyn_h->stub_offset = x->ofs;
1106       x->ofs += sizeof (plt_stub);
1107     }
1108   else
1109     dyn_h->want_stub = 0;
1110   return TRUE;
1111 }
1112 
1113 /* Allocate space for a FPTR entry.  */
1114 
1115 static bfd_boolean
1116 allocate_global_data_opd (dyn_h, data)
1117      struct elf64_hppa_dyn_hash_entry *dyn_h;
1118      PTR data;
1119 {
1120   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1121 
1122   if (dyn_h->want_opd)
1123     {
1124       struct elf_link_hash_entry *h = dyn_h->h;
1125 
1126       if (h)
1127 	while (h->root.type == bfd_link_hash_indirect
1128 	       || h->root.type == bfd_link_hash_warning)
1129 	  h = (struct elf_link_hash_entry *) h->root.u.i.link;
1130 
1131       /* We never need an opd entry for a symbol which is not
1132 	 defined by this output file.  */
1133       if (h && (h->root.type == bfd_link_hash_undefined
1134 		|| h->root.type == bfd_link_hash_undefweak
1135 		|| h->root.u.def.section->output_section == NULL))
1136 	dyn_h->want_opd = 0;
1137 
1138       /* If we are creating a shared library, took the address of a local
1139 	 function or might export this function from this object file, then
1140 	 we have to create an opd descriptor.  */
1141       else if (x->info->shared
1142 	       || h == NULL
1143 	       || (h->dynindx == -1 && h->type != STT_PARISC_MILLI)
1144 	       || (h->root.type == bfd_link_hash_defined
1145 		   || h->root.type == bfd_link_hash_defweak))
1146 	{
1147 	  /* If we are creating a shared library, then we will have to
1148 	     create a runtime relocation for the symbol to properly
1149 	     initialize the .opd entry.  Make sure the symbol gets
1150 	     added to the dynamic symbol table.  */
1151 	  if (x->info->shared
1152 	      && (h == NULL || (h->dynindx == -1)))
1153 	    {
1154 	      bfd *owner;
1155 	      owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
1156 
1157 	      if (!bfd_elf_link_record_local_dynamic_symbol
1158 		    (x->info, owner, dyn_h->sym_indx))
1159 		return FALSE;
1160 	    }
1161 
1162 	  /* This may not be necessary or desirable anymore now that
1163 	     we have some support for dealing with section symbols
1164 	     in dynamic relocs.  But name munging does make the result
1165 	     much easier to debug.  ie, the EPLT reloc will reference
1166 	     a symbol like .foobar, instead of .text + offset.  */
1167 	  if (x->info->shared && h)
1168 	    {
1169 	      char *new_name;
1170 	      struct elf_link_hash_entry *nh;
1171 
1172 	      new_name = alloca (strlen (h->root.root.string) + 2);
1173 	      new_name[0] = '.';
1174 	      strcpy (new_name + 1, h->root.root.string);
1175 
1176 	      nh = elf_link_hash_lookup (elf_hash_table (x->info),
1177 					 new_name, TRUE, TRUE, TRUE);
1178 
1179 	      nh->root.type = h->root.type;
1180 	      nh->root.u.def.value = h->root.u.def.value;
1181 	      nh->root.u.def.section = h->root.u.def.section;
1182 
1183 	      if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
1184 		return FALSE;
1185 
1186 	     }
1187 	  dyn_h->opd_offset = x->ofs;
1188 	  x->ofs += OPD_ENTRY_SIZE;
1189 	}
1190 
1191       /* Otherwise we do not need an opd entry.  */
1192       else
1193 	dyn_h->want_opd = 0;
1194     }
1195   return TRUE;
1196 }
1197 
1198 /* HP requires the EI_OSABI field to be filled in.  The assignment to
1199    EI_ABIVERSION may not be strictly necessary.  */
1200 
1201 static void
1202 elf64_hppa_post_process_headers (abfd, link_info)
1203      bfd * abfd;
1204      struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
1205 {
1206   Elf_Internal_Ehdr * i_ehdrp;
1207 
1208   i_ehdrp = elf_elfheader (abfd);
1209 
1210   if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
1211     {
1212       i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
1213     }
1214   else
1215     {
1216       i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
1217       i_ehdrp->e_ident[EI_ABIVERSION] = 1;
1218     }
1219 }
1220 
1221 /* Create function descriptor section (.opd).  This section is called .opd
1222    because it contains "official procedure descriptors".  The "official"
1223    refers to the fact that these descriptors are used when taking the address
1224    of a procedure, thus ensuring a unique address for each procedure.  */
1225 
1226 static bfd_boolean
1227 get_opd (abfd, info, hppa_info)
1228      bfd *abfd;
1229      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1230      struct elf64_hppa_link_hash_table *hppa_info;
1231 {
1232   asection *opd;
1233   bfd *dynobj;
1234 
1235   opd = hppa_info->opd_sec;
1236   if (!opd)
1237     {
1238       dynobj = hppa_info->root.dynobj;
1239       if (!dynobj)
1240 	hppa_info->root.dynobj = dynobj = abfd;
1241 
1242       opd = bfd_make_section_with_flags (dynobj, ".opd",
1243 					 (SEC_ALLOC
1244 					  | SEC_LOAD
1245 					  | SEC_HAS_CONTENTS
1246 					  | SEC_IN_MEMORY
1247 					  | SEC_LINKER_CREATED));
1248       if (!opd
1249 	  || !bfd_set_section_alignment (abfd, opd, 3))
1250 	{
1251 	  BFD_ASSERT (0);
1252 	  return FALSE;
1253 	}
1254 
1255       hppa_info->opd_sec = opd;
1256     }
1257 
1258   return TRUE;
1259 }
1260 
1261 /* Create the PLT section.  */
1262 
1263 static bfd_boolean
1264 get_plt (abfd, info, hppa_info)
1265      bfd *abfd;
1266      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1267      struct elf64_hppa_link_hash_table *hppa_info;
1268 {
1269   asection *plt;
1270   bfd *dynobj;
1271 
1272   plt = hppa_info->plt_sec;
1273   if (!plt)
1274     {
1275       dynobj = hppa_info->root.dynobj;
1276       if (!dynobj)
1277 	hppa_info->root.dynobj = dynobj = abfd;
1278 
1279       plt = bfd_make_section_with_flags (dynobj, ".plt",
1280 					 (SEC_ALLOC
1281 					  | SEC_LOAD
1282 					  | SEC_HAS_CONTENTS
1283 					  | SEC_IN_MEMORY
1284 					  | SEC_LINKER_CREATED));
1285       if (!plt
1286 	  || !bfd_set_section_alignment (abfd, plt, 3))
1287 	{
1288 	  BFD_ASSERT (0);
1289 	  return FALSE;
1290 	}
1291 
1292       hppa_info->plt_sec = plt;
1293     }
1294 
1295   return TRUE;
1296 }
1297 
1298 /* Create the DLT section.  */
1299 
1300 static bfd_boolean
1301 get_dlt (abfd, info, hppa_info)
1302      bfd *abfd;
1303      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1304      struct elf64_hppa_link_hash_table *hppa_info;
1305 {
1306   asection *dlt;
1307   bfd *dynobj;
1308 
1309   dlt = hppa_info->dlt_sec;
1310   if (!dlt)
1311     {
1312       dynobj = hppa_info->root.dynobj;
1313       if (!dynobj)
1314 	hppa_info->root.dynobj = dynobj = abfd;
1315 
1316       dlt = bfd_make_section_with_flags (dynobj, ".dlt",
1317 					 (SEC_ALLOC
1318 					  | SEC_LOAD
1319 					  | SEC_HAS_CONTENTS
1320 					  | SEC_IN_MEMORY
1321 					  | SEC_LINKER_CREATED));
1322       if (!dlt
1323 	  || !bfd_set_section_alignment (abfd, dlt, 3))
1324 	{
1325 	  BFD_ASSERT (0);
1326 	  return FALSE;
1327 	}
1328 
1329       hppa_info->dlt_sec = dlt;
1330     }
1331 
1332   return TRUE;
1333 }
1334 
1335 /* Create the stubs section.  */
1336 
1337 static bfd_boolean
1338 get_stub (abfd, info, hppa_info)
1339      bfd *abfd;
1340      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1341      struct elf64_hppa_link_hash_table *hppa_info;
1342 {
1343   asection *stub;
1344   bfd *dynobj;
1345 
1346   stub = hppa_info->stub_sec;
1347   if (!stub)
1348     {
1349       dynobj = hppa_info->root.dynobj;
1350       if (!dynobj)
1351 	hppa_info->root.dynobj = dynobj = abfd;
1352 
1353       stub = bfd_make_section_with_flags (dynobj, ".stub",
1354 					  (SEC_ALLOC | SEC_LOAD
1355 					   | SEC_HAS_CONTENTS
1356 					   | SEC_IN_MEMORY
1357 					   | SEC_READONLY
1358 					   | SEC_LINKER_CREATED));
1359       if (!stub
1360 	  || !bfd_set_section_alignment (abfd, stub, 3))
1361 	{
1362 	  BFD_ASSERT (0);
1363 	  return FALSE;
1364 	}
1365 
1366       hppa_info->stub_sec = stub;
1367     }
1368 
1369   return TRUE;
1370 }
1371 
1372 /* Create sections necessary for dynamic linking.  This is only a rough
1373    cut and will likely change as we learn more about the somewhat
1374    unusual dynamic linking scheme HP uses.
1375 
1376    .stub:
1377 	Contains code to implement cross-space calls.  The first time one
1378 	of the stubs is used it will call into the dynamic linker, later
1379 	calls will go straight to the target.
1380 
1381 	The only stub we support right now looks like
1382 
1383 	ldd OFFSET(%dp),%r1
1384 	bve %r0(%r1)
1385 	ldd OFFSET+8(%dp),%dp
1386 
1387 	Other stubs may be needed in the future.  We may want the remove
1388 	the break/nop instruction.  It is only used right now to keep the
1389 	offset of a .plt entry and a .stub entry in sync.
1390 
1391    .dlt:
1392 	This is what most people call the .got.  HP used a different name.
1393 	Losers.
1394 
1395    .rela.dlt:
1396 	Relocations for the DLT.
1397 
1398    .plt:
1399 	Function pointers as address,gp pairs.
1400 
1401    .rela.plt:
1402 	Should contain dynamic IPLT (and EPLT?) relocations.
1403 
1404    .opd:
1405 	FPTRS
1406 
1407    .rela.opd:
1408 	EPLT relocations for symbols exported from shared libraries.  */
1409 
1410 static bfd_boolean
1411 elf64_hppa_create_dynamic_sections (abfd, info)
1412      bfd *abfd;
1413      struct bfd_link_info *info;
1414 {
1415   asection *s;
1416 
1417   if (! get_stub (abfd, info, elf64_hppa_hash_table (info)))
1418     return FALSE;
1419 
1420   if (! get_dlt (abfd, info, elf64_hppa_hash_table (info)))
1421     return FALSE;
1422 
1423   if (! get_plt (abfd, info, elf64_hppa_hash_table (info)))
1424     return FALSE;
1425 
1426   if (! get_opd (abfd, info, elf64_hppa_hash_table (info)))
1427     return FALSE;
1428 
1429   s = bfd_make_section_with_flags (abfd, ".rela.dlt",
1430 				   (SEC_ALLOC | SEC_LOAD
1431 				    | SEC_HAS_CONTENTS
1432 				    | SEC_IN_MEMORY
1433 				    | SEC_READONLY
1434 				    | SEC_LINKER_CREATED));
1435   if (s == NULL
1436       || !bfd_set_section_alignment (abfd, s, 3))
1437     return FALSE;
1438   elf64_hppa_hash_table (info)->dlt_rel_sec = s;
1439 
1440   s = bfd_make_section_with_flags (abfd, ".rela.plt",
1441 				   (SEC_ALLOC | SEC_LOAD
1442 				    | SEC_HAS_CONTENTS
1443 				    | SEC_IN_MEMORY
1444 				    | SEC_READONLY
1445 				    | SEC_LINKER_CREATED));
1446   if (s == NULL
1447       || !bfd_set_section_alignment (abfd, s, 3))
1448     return FALSE;
1449   elf64_hppa_hash_table (info)->plt_rel_sec = s;
1450 
1451   s = bfd_make_section_with_flags (abfd, ".rela.data",
1452 				   (SEC_ALLOC | SEC_LOAD
1453 				    | SEC_HAS_CONTENTS
1454 				    | SEC_IN_MEMORY
1455 				    | SEC_READONLY
1456 				    | SEC_LINKER_CREATED));
1457   if (s == NULL
1458       || !bfd_set_section_alignment (abfd, s, 3))
1459     return FALSE;
1460   elf64_hppa_hash_table (info)->other_rel_sec = s;
1461 
1462   s = bfd_make_section_with_flags (abfd, ".rela.opd",
1463 				   (SEC_ALLOC | SEC_LOAD
1464 				    | SEC_HAS_CONTENTS
1465 				    | SEC_IN_MEMORY
1466 				    | SEC_READONLY
1467 				    | SEC_LINKER_CREATED));
1468   if (s == NULL
1469       || !bfd_set_section_alignment (abfd, s, 3))
1470     return FALSE;
1471   elf64_hppa_hash_table (info)->opd_rel_sec = s;
1472 
1473   return TRUE;
1474 }
1475 
1476 /* Allocate dynamic relocations for those symbols that turned out
1477    to be dynamic.  */
1478 
1479 static bfd_boolean
1480 allocate_dynrel_entries (dyn_h, data)
1481      struct elf64_hppa_dyn_hash_entry *dyn_h;
1482      PTR data;
1483 {
1484   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1485   struct elf64_hppa_link_hash_table *hppa_info;
1486   struct elf64_hppa_dyn_reloc_entry *rent;
1487   bfd_boolean dynamic_symbol, shared;
1488 
1489   hppa_info = elf64_hppa_hash_table (x->info);
1490   dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info);
1491   shared = x->info->shared;
1492 
1493   /* We may need to allocate relocations for a non-dynamic symbol
1494      when creating a shared library.  */
1495   if (!dynamic_symbol && !shared)
1496     return TRUE;
1497 
1498   /* Take care of the normal data relocations.  */
1499 
1500   for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
1501     {
1502       /* Allocate one iff we are building a shared library, the relocation
1503 	 isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
1504       if (!shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
1505 	continue;
1506 
1507       hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
1508 
1509       /* Make sure this symbol gets into the dynamic symbol table if it is
1510 	 not already recorded.  ?!? This should not be in the loop since
1511 	 the symbol need only be added once.  */
1512       if (dyn_h->h == 0
1513 	  || (dyn_h->h->dynindx == -1 && dyn_h->h->type != STT_PARISC_MILLI))
1514 	if (!bfd_elf_link_record_local_dynamic_symbol
1515 	    (x->info, rent->sec->owner, dyn_h->sym_indx))
1516 	  return FALSE;
1517     }
1518 
1519   /* Take care of the GOT and PLT relocations.  */
1520 
1521   if ((dynamic_symbol || shared) && dyn_h->want_dlt)
1522     hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
1523 
1524   /* If we are building a shared library, then every symbol that has an
1525      opd entry will need an EPLT relocation to relocate the symbol's address
1526      and __gp value based on the runtime load address.  */
1527   if (shared && dyn_h->want_opd)
1528     hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
1529 
1530   if (dyn_h->want_plt && dynamic_symbol)
1531     {
1532       bfd_size_type t = 0;
1533 
1534       /* Dynamic symbols get one IPLT relocation.  Local symbols in
1535 	 shared libraries get two REL relocations.  Local symbols in
1536 	 main applications get nothing.  */
1537       if (dynamic_symbol)
1538 	t = sizeof (Elf64_External_Rela);
1539       else if (shared)
1540 	t = 2 * sizeof (Elf64_External_Rela);
1541 
1542       hppa_info->plt_rel_sec->size += t;
1543     }
1544 
1545   return TRUE;
1546 }
1547 
1548 /* Adjust a symbol defined by a dynamic object and referenced by a
1549    regular object.  */
1550 
1551 static bfd_boolean
1552 elf64_hppa_adjust_dynamic_symbol (info, h)
1553      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1554      struct elf_link_hash_entry *h;
1555 {
1556   /* ??? Undefined symbols with PLT entries should be re-defined
1557      to be the PLT entry.  */
1558 
1559   /* If this is a weak symbol, and there is a real definition, the
1560      processor independent code will have arranged for us to see the
1561      real definition first, and we can just use the same value.  */
1562   if (h->u.weakdef != NULL)
1563     {
1564       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1565 		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
1566       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1567       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1568       return TRUE;
1569     }
1570 
1571   /* If this is a reference to a symbol defined by a dynamic object which
1572      is not a function, we might allocate the symbol in our .dynbss section
1573      and allocate a COPY dynamic relocation.
1574 
1575      But PA64 code is canonically PIC, so as a rule we can avoid this sort
1576      of hackery.  */
1577 
1578   return TRUE;
1579 }
1580 
1581 /* This function is called via elf_link_hash_traverse to mark millicode
1582    symbols with a dynindx of -1 and to remove the string table reference
1583    from the dynamic symbol table.  If the symbol is not a millicode symbol,
1584    elf64_hppa_mark_exported_functions is called.  */
1585 
1586 static bfd_boolean
1587 elf64_hppa_mark_milli_and_exported_functions (h, data)
1588      struct elf_link_hash_entry *h;
1589      PTR data;
1590 {
1591   struct bfd_link_info *info = (struct bfd_link_info *)data;
1592   struct elf_link_hash_entry *elf = h;
1593 
1594   if (elf->root.type == bfd_link_hash_warning)
1595     elf = (struct elf_link_hash_entry *) elf->root.u.i.link;
1596 
1597   if (elf->type == STT_PARISC_MILLI)
1598     {
1599       if (elf->dynindx != -1)
1600 	{
1601 	  elf->dynindx = -1;
1602 	  _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1603 				  elf->dynstr_index);
1604 	}
1605       return TRUE;
1606     }
1607 
1608   return elf64_hppa_mark_exported_functions (h, data);
1609 }
1610 
1611 /* Set the final sizes of the dynamic sections and allocate memory for
1612    the contents of our special sections.  */
1613 
1614 static bfd_boolean
1615 elf64_hppa_size_dynamic_sections (output_bfd, info)
1616      bfd *output_bfd;
1617      struct bfd_link_info *info;
1618 {
1619   bfd *dynobj;
1620   asection *s;
1621   bfd_boolean plt;
1622   bfd_boolean relocs;
1623   bfd_boolean reltext;
1624   struct elf64_hppa_allocate_data data;
1625   struct elf64_hppa_link_hash_table *hppa_info;
1626 
1627   hppa_info = elf64_hppa_hash_table (info);
1628 
1629   dynobj = elf_hash_table (info)->dynobj;
1630   BFD_ASSERT (dynobj != NULL);
1631 
1632   /* Mark each function this program exports so that we will allocate
1633      space in the .opd section for each function's FPTR.  If we are
1634      creating dynamic sections, change the dynamic index of millicode
1635      symbols to -1 and remove them from the string table for .dynstr.
1636 
1637      We have to traverse the main linker hash table since we have to
1638      find functions which may not have been mentioned in any relocs.  */
1639   elf_link_hash_traverse (elf_hash_table (info),
1640 			  (elf_hash_table (info)->dynamic_sections_created
1641 			   ? elf64_hppa_mark_milli_and_exported_functions
1642 			   : elf64_hppa_mark_exported_functions),
1643 			  info);
1644 
1645   if (elf_hash_table (info)->dynamic_sections_created)
1646     {
1647       /* Set the contents of the .interp section to the interpreter.  */
1648       if (info->executable && !info->static_link)
1649 	{
1650 	  s = bfd_get_section_by_name (dynobj, ".interp");
1651 	  BFD_ASSERT (s != NULL);
1652 	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1653 	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1654 	}
1655     }
1656   else
1657     {
1658       /* We may have created entries in the .rela.got section.
1659 	 However, if we are not creating the dynamic sections, we will
1660 	 not actually use these entries.  Reset the size of .rela.dlt,
1661 	 which will cause it to get stripped from the output file
1662 	 below.  */
1663       s = bfd_get_section_by_name (dynobj, ".rela.dlt");
1664       if (s != NULL)
1665 	s->size = 0;
1666     }
1667 
1668   /* Allocate the GOT entries.  */
1669 
1670   data.info = info;
1671   if (elf64_hppa_hash_table (info)->dlt_sec)
1672     {
1673       data.ofs = 0x0;
1674       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1675 				    allocate_global_data_dlt, &data);
1676       hppa_info->dlt_sec->size = data.ofs;
1677 
1678       data.ofs = 0x0;
1679       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1680 				    allocate_global_data_plt, &data);
1681       hppa_info->plt_sec->size = data.ofs;
1682 
1683       data.ofs = 0x0;
1684       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1685 				    allocate_global_data_stub, &data);
1686       hppa_info->stub_sec->size = data.ofs;
1687     }
1688 
1689   /* Allocate space for entries in the .opd section.  */
1690   if (elf64_hppa_hash_table (info)->opd_sec)
1691     {
1692       data.ofs = 0;
1693       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1694 				    allocate_global_data_opd, &data);
1695       hppa_info->opd_sec->size = data.ofs;
1696     }
1697 
1698   /* Now allocate space for dynamic relocations, if necessary.  */
1699   if (hppa_info->root.dynamic_sections_created)
1700     elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1701 				  allocate_dynrel_entries, &data);
1702 
1703   /* The sizes of all the sections are set.  Allocate memory for them.  */
1704   plt = FALSE;
1705   relocs = FALSE;
1706   reltext = FALSE;
1707   for (s = dynobj->sections; s != NULL; s = s->next)
1708     {
1709       const char *name;
1710 
1711       if ((s->flags & SEC_LINKER_CREATED) == 0)
1712 	continue;
1713 
1714       /* It's OK to base decisions on the section name, because none
1715 	 of the dynobj section names depend upon the input files.  */
1716       name = bfd_get_section_name (dynobj, s);
1717 
1718       if (strcmp (name, ".plt") == 0)
1719 	{
1720 	  /* Remember whether there is a PLT.  */
1721 	  plt = s->size != 0;
1722 	}
1723       else if (strcmp (name, ".opd") == 0
1724 	       || strncmp (name, ".dlt", 4) == 0
1725 	       || strcmp (name, ".stub") == 0
1726 	       || strcmp (name, ".got") == 0)
1727 	{
1728 	  /* Strip this section if we don't need it; see the comment below.  */
1729 	}
1730       else if (strncmp (name, ".rela", 5) == 0)
1731 	{
1732 	  if (s->size != 0)
1733 	    {
1734 	      asection *target;
1735 
1736 	      /* Remember whether there are any reloc sections other
1737 		 than .rela.plt.  */
1738 	      if (strcmp (name, ".rela.plt") != 0)
1739 		{
1740 		  const char *outname;
1741 
1742 		  relocs = TRUE;
1743 
1744 		  /* If this relocation section applies to a read only
1745 		     section, then we probably need a DT_TEXTREL
1746 		     entry.  The entries in the .rela.plt section
1747 		     really apply to the .got section, which we
1748 		     created ourselves and so know is not readonly.  */
1749 		  outname = bfd_get_section_name (output_bfd,
1750 						  s->output_section);
1751 		  target = bfd_get_section_by_name (output_bfd, outname + 4);
1752 		  if (target != NULL
1753 		      && (target->flags & SEC_READONLY) != 0
1754 		      && (target->flags & SEC_ALLOC) != 0)
1755 		    reltext = TRUE;
1756 		}
1757 
1758 	      /* We use the reloc_count field as a counter if we need
1759 		 to copy relocs into the output file.  */
1760 	      s->reloc_count = 0;
1761 	    }
1762 	}
1763       else
1764 	{
1765 	  /* It's not one of our sections, so don't allocate space.  */
1766 	  continue;
1767 	}
1768 
1769       if (s->size == 0)
1770 	{
1771 	  /* If we don't need this section, strip it from the
1772 	     output file.  This is mostly to handle .rela.bss and
1773 	     .rela.plt.  We must create both sections in
1774 	     create_dynamic_sections, because they must be created
1775 	     before the linker maps input sections to output
1776 	     sections.  The linker does that before
1777 	     adjust_dynamic_symbol is called, and it is that
1778 	     function which decides whether anything needs to go
1779 	     into these sections.  */
1780 	  s->flags |= SEC_EXCLUDE;
1781 	  continue;
1782 	}
1783 
1784       if ((s->flags & SEC_HAS_CONTENTS) == 0)
1785 	continue;
1786 
1787       /* Allocate memory for the section contents if it has not
1788 	 been allocated already.  We use bfd_zalloc here in case
1789 	 unused entries are not reclaimed before the section's
1790 	 contents are written out.  This should not happen, but this
1791 	 way if it does, we get a R_PARISC_NONE reloc instead of
1792 	 garbage.  */
1793       if (s->contents == NULL)
1794 	{
1795 	  s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1796 	  if (s->contents == NULL)
1797 	    return FALSE;
1798 	}
1799     }
1800 
1801   if (elf_hash_table (info)->dynamic_sections_created)
1802     {
1803       /* Always create a DT_PLTGOT.  It actually has nothing to do with
1804 	 the PLT, it is how we communicate the __gp value of a load
1805 	 module to the dynamic linker.  */
1806 #define add_dynamic_entry(TAG, VAL) \
1807   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1808 
1809       if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0)
1810 	  || !add_dynamic_entry (DT_PLTGOT, 0))
1811 	return FALSE;
1812 
1813       /* Add some entries to the .dynamic section.  We fill in the
1814 	 values later, in elf64_hppa_finish_dynamic_sections, but we
1815 	 must add the entries now so that we get the correct size for
1816 	 the .dynamic section.  The DT_DEBUG entry is filled in by the
1817 	 dynamic linker and used by the debugger.  */
1818       if (! info->shared)
1819 	{
1820 	  if (!add_dynamic_entry (DT_DEBUG, 0)
1821 	      || !add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1822 	      || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
1823 	    return FALSE;
1824 	}
1825 
1826       /* Force DT_FLAGS to always be set.
1827 	 Required by HPUX 11.00 patch PHSS_26559.  */
1828       if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1829 	return FALSE;
1830 
1831       if (plt)
1832 	{
1833 	  if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1834 	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1835 	      || !add_dynamic_entry (DT_JMPREL, 0))
1836 	    return FALSE;
1837 	}
1838 
1839       if (relocs)
1840 	{
1841 	  if (!add_dynamic_entry (DT_RELA, 0)
1842 	      || !add_dynamic_entry (DT_RELASZ, 0)
1843 	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1844 	    return FALSE;
1845 	}
1846 
1847       if (reltext)
1848 	{
1849 	  if (!add_dynamic_entry (DT_TEXTREL, 0))
1850 	    return FALSE;
1851 	  info->flags |= DF_TEXTREL;
1852 	}
1853     }
1854 #undef add_dynamic_entry
1855 
1856   return TRUE;
1857 }
1858 
1859 /* Called after we have output the symbol into the dynamic symbol
1860    table, but before we output the symbol into the normal symbol
1861    table.
1862 
1863    For some symbols we had to change their address when outputting
1864    the dynamic symbol table.  We undo that change here so that
1865    the symbols have their expected value in the normal symbol
1866    table.  Ick.  */
1867 
1868 static bfd_boolean
1869 elf64_hppa_link_output_symbol_hook (info, name, sym, input_sec, h)
1870      struct bfd_link_info *info;
1871      const char *name;
1872      Elf_Internal_Sym *sym;
1873      asection *input_sec ATTRIBUTE_UNUSED;
1874      struct elf_link_hash_entry *h;
1875 {
1876   struct elf64_hppa_link_hash_table *hppa_info;
1877   struct elf64_hppa_dyn_hash_entry *dyn_h;
1878 
1879   /* We may be called with the file symbol or section symbols.
1880      They never need munging, so it is safe to ignore them.  */
1881   if (!name)
1882     return TRUE;
1883 
1884   /* Get the PA dyn_symbol (if any) associated with NAME.  */
1885   hppa_info = elf64_hppa_hash_table (info);
1886   dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
1887 				      name, FALSE, FALSE);
1888   if (!dyn_h || dyn_h->h != h)
1889     return TRUE;
1890 
1891   /* Function symbols for which we created .opd entries *may* have been
1892      munged by finish_dynamic_symbol and have to be un-munged here.
1893 
1894      Note that finish_dynamic_symbol sometimes turns dynamic symbols
1895      into non-dynamic ones, so we initialize st_shndx to -1 in
1896      mark_exported_functions and check to see if it was overwritten
1897      here instead of just checking dyn_h->h->dynindx.  */
1898   if (dyn_h->want_opd && dyn_h->st_shndx != -1)
1899     {
1900       /* Restore the saved value and section index.  */
1901       sym->st_value = dyn_h->st_value;
1902       sym->st_shndx = dyn_h->st_shndx;
1903     }
1904 
1905   return TRUE;
1906 }
1907 
1908 /* Finish up dynamic symbol handling.  We set the contents of various
1909    dynamic sections here.  */
1910 
1911 static bfd_boolean
1912 elf64_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
1913      bfd *output_bfd;
1914      struct bfd_link_info *info;
1915      struct elf_link_hash_entry *h;
1916      Elf_Internal_Sym *sym;
1917 {
1918   asection *stub, *splt, *sdlt, *sopd, *spltrel, *sdltrel;
1919   struct elf64_hppa_link_hash_table *hppa_info;
1920   struct elf64_hppa_dyn_hash_entry *dyn_h;
1921 
1922   hppa_info = elf64_hppa_hash_table (info);
1923   dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
1924 				      h->root.root.string, FALSE, FALSE);
1925 
1926   stub = hppa_info->stub_sec;
1927   splt = hppa_info->plt_sec;
1928   sdlt = hppa_info->dlt_sec;
1929   sopd = hppa_info->opd_sec;
1930   spltrel = hppa_info->plt_rel_sec;
1931   sdltrel = hppa_info->dlt_rel_sec;
1932 
1933   /* Incredible.  It is actually necessary to NOT use the symbol's real
1934      value when building the dynamic symbol table for a shared library.
1935      At least for symbols that refer to functions.
1936 
1937      We will store a new value and section index into the symbol long
1938      enough to output it into the dynamic symbol table, then we restore
1939      the original values (in elf64_hppa_link_output_symbol_hook).  */
1940   if (dyn_h && dyn_h->want_opd)
1941     {
1942       BFD_ASSERT (sopd != NULL);
1943 
1944       /* Save away the original value and section index so that we
1945 	 can restore them later.  */
1946       dyn_h->st_value = sym->st_value;
1947       dyn_h->st_shndx = sym->st_shndx;
1948 
1949       /* For the dynamic symbol table entry, we want the value to be
1950 	 address of this symbol's entry within the .opd section.  */
1951       sym->st_value = (dyn_h->opd_offset
1952 		       + sopd->output_offset
1953 		       + sopd->output_section->vma);
1954       sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1955 							 sopd->output_section);
1956     }
1957 
1958   /* Initialize a .plt entry if requested.  */
1959   if (dyn_h && dyn_h->want_plt
1960       && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
1961     {
1962       bfd_vma value;
1963       Elf_Internal_Rela rel;
1964       bfd_byte *loc;
1965 
1966       BFD_ASSERT (splt != NULL && spltrel != NULL);
1967 
1968       /* We do not actually care about the value in the PLT entry
1969 	 if we are creating a shared library and the symbol is
1970 	 still undefined, we create a dynamic relocation to fill
1971 	 in the correct value.  */
1972       if (info->shared && h->root.type == bfd_link_hash_undefined)
1973 	value = 0;
1974       else
1975 	value = (h->root.u.def.value + h->root.u.def.section->vma);
1976 
1977       /* Fill in the entry in the procedure linkage table.
1978 
1979 	 The format of a plt entry is
1980 	 <funcaddr> <__gp>.
1981 
1982 	 plt_offset is the offset within the PLT section at which to
1983 	 install the PLT entry.
1984 
1985 	 We are modifying the in-memory PLT contents here, so we do not add
1986 	 in the output_offset of the PLT section.  */
1987 
1988       bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset);
1989       value = _bfd_get_gp_value (splt->output_section->owner);
1990       bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset + 0x8);
1991 
1992       /* Create a dynamic IPLT relocation for this entry.
1993 
1994 	 We are creating a relocation in the output file's PLT section,
1995 	 which is included within the DLT secton.  So we do need to include
1996 	 the PLT's output_offset in the computation of the relocation's
1997 	 address.  */
1998       rel.r_offset = (dyn_h->plt_offset + splt->output_offset
1999 		      + splt->output_section->vma);
2000       rel.r_info = ELF64_R_INFO (h->dynindx, R_PARISC_IPLT);
2001       rel.r_addend = 0;
2002 
2003       loc = spltrel->contents;
2004       loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2005       bfd_elf64_swap_reloca_out (splt->output_section->owner, &rel, loc);
2006     }
2007 
2008   /* Initialize an external call stub entry if requested.  */
2009   if (dyn_h && dyn_h->want_stub
2010       && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
2011     {
2012       bfd_vma value;
2013       int insn;
2014       unsigned int max_offset;
2015 
2016       BFD_ASSERT (stub != NULL);
2017 
2018       /* Install the generic stub template.
2019 
2020 	 We are modifying the contents of the stub section, so we do not
2021 	 need to include the stub section's output_offset here.  */
2022       memcpy (stub->contents + dyn_h->stub_offset, plt_stub, sizeof (plt_stub));
2023 
2024       /* Fix up the first ldd instruction.
2025 
2026 	 We are modifying the contents of the STUB section in memory,
2027 	 so we do not need to include its output offset in this computation.
2028 
2029 	 Note the plt_offset value is the value of the PLT entry relative to
2030 	 the start of the PLT section.  These instructions will reference
2031 	 data relative to the value of __gp, which may not necessarily have
2032 	 the same address as the start of the PLT section.
2033 
2034 	 gp_offset contains the offset of __gp within the PLT section.  */
2035       value = dyn_h->plt_offset - hppa_info->gp_offset;
2036 
2037       insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset);
2038       if (output_bfd->arch_info->mach >= 25)
2039 	{
2040 	  /* Wide mode allows 16 bit offsets.  */
2041 	  max_offset = 32768;
2042 	  insn &= ~ 0xfff1;
2043 	  insn |= re_assemble_16 ((int) value);
2044 	}
2045       else
2046 	{
2047 	  max_offset = 8192;
2048 	  insn &= ~ 0x3ff1;
2049 	  insn |= re_assemble_14 ((int) value);
2050 	}
2051 
2052       if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2053 	{
2054 	  (*_bfd_error_handler) (_("stub entry for %s cannot load .plt, dp offset = %ld"),
2055 				 dyn_h->root.string,
2056 				 (long) value);
2057 	  return FALSE;
2058 	}
2059 
2060       bfd_put_32 (stub->owner, (bfd_vma) insn,
2061 		  stub->contents + dyn_h->stub_offset);
2062 
2063       /* Fix up the second ldd instruction.  */
2064       value += 8;
2065       insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset + 8);
2066       if (output_bfd->arch_info->mach >= 25)
2067 	{
2068 	  insn &= ~ 0xfff1;
2069 	  insn |= re_assemble_16 ((int) value);
2070 	}
2071       else
2072 	{
2073 	  insn &= ~ 0x3ff1;
2074 	  insn |= re_assemble_14 ((int) value);
2075 	}
2076       bfd_put_32 (stub->owner, (bfd_vma) insn,
2077 		  stub->contents + dyn_h->stub_offset + 8);
2078     }
2079 
2080   return TRUE;
2081 }
2082 
2083 /* The .opd section contains FPTRs for each function this file
2084    exports.  Initialize the FPTR entries.  */
2085 
2086 static bfd_boolean
2087 elf64_hppa_finalize_opd (dyn_h, data)
2088      struct elf64_hppa_dyn_hash_entry *dyn_h;
2089      PTR data;
2090 {
2091   struct bfd_link_info *info = (struct bfd_link_info *)data;
2092   struct elf64_hppa_link_hash_table *hppa_info;
2093   struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
2094   asection *sopd;
2095   asection *sopdrel;
2096 
2097   hppa_info = elf64_hppa_hash_table (info);
2098   sopd = hppa_info->opd_sec;
2099   sopdrel = hppa_info->opd_rel_sec;
2100 
2101   if (h && dyn_h->want_opd)
2102     {
2103       bfd_vma value;
2104 
2105       /* The first two words of an .opd entry are zero.
2106 
2107 	 We are modifying the contents of the OPD section in memory, so we
2108 	 do not need to include its output offset in this computation.  */
2109       memset (sopd->contents + dyn_h->opd_offset, 0, 16);
2110 
2111       value = (h->root.u.def.value
2112 	       + h->root.u.def.section->output_section->vma
2113 	       + h->root.u.def.section->output_offset);
2114 
2115       /* The next word is the address of the function.  */
2116       bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 16);
2117 
2118       /* The last word is our local __gp value.  */
2119       value = _bfd_get_gp_value (sopd->output_section->owner);
2120       bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 24);
2121     }
2122 
2123   /* If we are generating a shared library, we must generate EPLT relocations
2124      for each entry in the .opd, even for static functions (they may have
2125      had their address taken).  */
2126   if (info->shared && dyn_h && dyn_h->want_opd)
2127     {
2128       Elf_Internal_Rela rel;
2129       bfd_byte *loc;
2130       int dynindx;
2131 
2132       /* We may need to do a relocation against a local symbol, in
2133 	 which case we have to look up it's dynamic symbol index off
2134 	 the local symbol hash table.  */
2135       if (h && h->dynindx != -1)
2136 	dynindx = h->dynindx;
2137       else
2138 	dynindx
2139 	  = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2140 						dyn_h->sym_indx);
2141 
2142       /* The offset of this relocation is the absolute address of the
2143 	 .opd entry for this symbol.  */
2144       rel.r_offset = (dyn_h->opd_offset + sopd->output_offset
2145 		      + sopd->output_section->vma);
2146 
2147       /* If H is non-null, then we have an external symbol.
2148 
2149 	 It is imperative that we use a different dynamic symbol for the
2150 	 EPLT relocation if the symbol has global scope.
2151 
2152 	 In the dynamic symbol table, the function symbol will have a value
2153 	 which is address of the function's .opd entry.
2154 
2155 	 Thus, we can not use that dynamic symbol for the EPLT relocation
2156 	 (if we did, the data in the .opd would reference itself rather
2157 	 than the actual address of the function).  Instead we have to use
2158 	 a new dynamic symbol which has the same value as the original global
2159 	 function symbol.
2160 
2161 	 We prefix the original symbol with a "." and use the new symbol in
2162 	 the EPLT relocation.  This new symbol has already been recorded in
2163 	 the symbol table, we just have to look it up and use it.
2164 
2165 	 We do not have such problems with static functions because we do
2166 	 not make their addresses in the dynamic symbol table point to
2167 	 the .opd entry.  Ultimately this should be safe since a static
2168 	 function can not be directly referenced outside of its shared
2169 	 library.
2170 
2171 	 We do have to play similar games for FPTR relocations in shared
2172 	 libraries, including those for static symbols.  See the FPTR
2173 	 handling in elf64_hppa_finalize_dynreloc.  */
2174       if (h)
2175 	{
2176 	  char *new_name;
2177 	  struct elf_link_hash_entry *nh;
2178 
2179 	  new_name = alloca (strlen (h->root.root.string) + 2);
2180 	  new_name[0] = '.';
2181 	  strcpy (new_name + 1, h->root.root.string);
2182 
2183 	  nh = elf_link_hash_lookup (elf_hash_table (info),
2184 				     new_name, FALSE, FALSE, FALSE);
2185 
2186 	  /* All we really want from the new symbol is its dynamic
2187 	     symbol index.  */
2188 	  dynindx = nh->dynindx;
2189 	}
2190 
2191       rel.r_addend = 0;
2192       rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2193 
2194       loc = sopdrel->contents;
2195       loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2196       bfd_elf64_swap_reloca_out (sopd->output_section->owner, &rel, loc);
2197     }
2198   return TRUE;
2199 }
2200 
2201 /* The .dlt section contains addresses for items referenced through the
2202    dlt.  Note that we can have a DLTIND relocation for a local symbol, thus
2203    we can not depend on finish_dynamic_symbol to initialize the .dlt.  */
2204 
2205 static bfd_boolean
2206 elf64_hppa_finalize_dlt (dyn_h, data)
2207      struct elf64_hppa_dyn_hash_entry *dyn_h;
2208      PTR data;
2209 {
2210   struct bfd_link_info *info = (struct bfd_link_info *)data;
2211   struct elf64_hppa_link_hash_table *hppa_info;
2212   asection *sdlt, *sdltrel;
2213   struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
2214 
2215   hppa_info = elf64_hppa_hash_table (info);
2216 
2217   sdlt = hppa_info->dlt_sec;
2218   sdltrel = hppa_info->dlt_rel_sec;
2219 
2220   /* H/DYN_H may refer to a local variable and we know it's
2221      address, so there is no need to create a relocation.  Just install
2222      the proper value into the DLT, note this shortcut can not be
2223      skipped when building a shared library.  */
2224   if (! info->shared && h && dyn_h->want_dlt)
2225     {
2226       bfd_vma value;
2227 
2228       /* If we had an LTOFF_FPTR style relocation we want the DLT entry
2229 	 to point to the FPTR entry in the .opd section.
2230 
2231 	 We include the OPD's output offset in this computation as
2232 	 we are referring to an absolute address in the resulting
2233 	 object file.  */
2234       if (dyn_h->want_opd)
2235 	{
2236 	  value = (dyn_h->opd_offset
2237 		   + hppa_info->opd_sec->output_offset
2238 		   + hppa_info->opd_sec->output_section->vma);
2239 	}
2240       else if ((h->root.type == bfd_link_hash_defined
2241 		|| h->root.type == bfd_link_hash_defweak)
2242 	       && h->root.u.def.section)
2243 	{
2244 	  value = h->root.u.def.value + h->root.u.def.section->output_offset;
2245 	  if (h->root.u.def.section->output_section)
2246 	    value += h->root.u.def.section->output_section->vma;
2247 	  else
2248 	    value += h->root.u.def.section->vma;
2249 	}
2250       else
2251 	/* We have an undefined function reference.  */
2252 	value = 0;
2253 
2254       /* We do not need to include the output offset of the DLT section
2255 	 here because we are modifying the in-memory contents.  */
2256       bfd_put_64 (sdlt->owner, value, sdlt->contents + dyn_h->dlt_offset);
2257     }
2258 
2259   /* Create a relocation for the DLT entry associated with this symbol.
2260      When building a shared library the symbol does not have to be dynamic.  */
2261   if (dyn_h->want_dlt
2262       && (elf64_hppa_dynamic_symbol_p (dyn_h->h, info) || info->shared))
2263     {
2264       Elf_Internal_Rela rel;
2265       bfd_byte *loc;
2266       int dynindx;
2267 
2268       /* We may need to do a relocation against a local symbol, in
2269 	 which case we have to look up it's dynamic symbol index off
2270 	 the local symbol hash table.  */
2271       if (h && h->dynindx != -1)
2272 	dynindx = h->dynindx;
2273       else
2274 	dynindx
2275 	  = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2276 						dyn_h->sym_indx);
2277 
2278       /* Create a dynamic relocation for this entry.  Do include the output
2279 	 offset of the DLT entry since we need an absolute address in the
2280 	 resulting object file.  */
2281       rel.r_offset = (dyn_h->dlt_offset + sdlt->output_offset
2282 		      + sdlt->output_section->vma);
2283       if (h && h->type == STT_FUNC)
2284 	  rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2285       else
2286 	  rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2287       rel.r_addend = 0;
2288 
2289       loc = sdltrel->contents;
2290       loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2291       bfd_elf64_swap_reloca_out (sdlt->output_section->owner, &rel, loc);
2292     }
2293   return TRUE;
2294 }
2295 
2296 /* Finalize the dynamic relocations.  Specifically the FPTR relocations
2297    for dynamic functions used to initialize static data.  */
2298 
2299 static bfd_boolean
2300 elf64_hppa_finalize_dynreloc (dyn_h, data)
2301      struct elf64_hppa_dyn_hash_entry *dyn_h;
2302      PTR data;
2303 {
2304   struct bfd_link_info *info = (struct bfd_link_info *)data;
2305   struct elf64_hppa_link_hash_table *hppa_info;
2306   struct elf_link_hash_entry *h;
2307   int dynamic_symbol;
2308 
2309   dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, info);
2310 
2311   if (!dynamic_symbol && !info->shared)
2312     return TRUE;
2313 
2314   if (dyn_h->reloc_entries)
2315     {
2316       struct elf64_hppa_dyn_reloc_entry *rent;
2317       int dynindx;
2318 
2319       hppa_info = elf64_hppa_hash_table (info);
2320       h = dyn_h->h;
2321 
2322       /* We may need to do a relocation against a local symbol, in
2323 	 which case we have to look up it's dynamic symbol index off
2324 	 the local symbol hash table.  */
2325       if (h && h->dynindx != -1)
2326 	dynindx = h->dynindx;
2327       else
2328 	dynindx
2329 	  = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2330 						dyn_h->sym_indx);
2331 
2332       for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
2333 	{
2334 	  Elf_Internal_Rela rel;
2335 	  bfd_byte *loc;
2336 
2337 	  /* Allocate one iff we are building a shared library, the relocation
2338 	     isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
2339 	  if (!info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
2340 	    continue;
2341 
2342 	  /* Create a dynamic relocation for this entry.
2343 
2344 	     We need the output offset for the reloc's section because
2345 	     we are creating an absolute address in the resulting object
2346 	     file.  */
2347 	  rel.r_offset = (rent->offset + rent->sec->output_offset
2348 			  + rent->sec->output_section->vma);
2349 
2350 	  /* An FPTR64 relocation implies that we took the address of
2351 	     a function and that the function has an entry in the .opd
2352 	     section.  We want the FPTR64 relocation to reference the
2353 	     entry in .opd.
2354 
2355 	     We could munge the symbol value in the dynamic symbol table
2356 	     (in fact we already do for functions with global scope) to point
2357 	     to the .opd entry.  Then we could use that dynamic symbol in
2358 	     this relocation.
2359 
2360 	     Or we could do something sensible, not munge the symbol's
2361 	     address and instead just use a different symbol to reference
2362 	     the .opd entry.  At least that seems sensible until you
2363 	     realize there's no local dynamic symbols we can use for that
2364 	     purpose.  Thus the hair in the check_relocs routine.
2365 
2366 	     We use a section symbol recorded by check_relocs as the
2367 	     base symbol for the relocation.  The addend is the difference
2368 	     between the section symbol and the address of the .opd entry.  */
2369 	  if (info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
2370 	    {
2371 	      bfd_vma value, value2;
2372 
2373 	      /* First compute the address of the opd entry for this symbol.  */
2374 	      value = (dyn_h->opd_offset
2375 		       + hppa_info->opd_sec->output_section->vma
2376 		       + hppa_info->opd_sec->output_offset);
2377 
2378 	      /* Compute the value of the start of the section with
2379 		 the relocation.  */
2380 	      value2 = (rent->sec->output_section->vma
2381 			+ rent->sec->output_offset);
2382 
2383 	      /* Compute the difference between the start of the section
2384 		 with the relocation and the opd entry.  */
2385 	      value -= value2;
2386 
2387 	      /* The result becomes the addend of the relocation.  */
2388 	      rel.r_addend = value;
2389 
2390 	      /* The section symbol becomes the symbol for the dynamic
2391 		 relocation.  */
2392 	      dynindx
2393 		= _bfd_elf_link_lookup_local_dynindx (info,
2394 						      rent->sec->owner,
2395 						      rent->sec_symndx);
2396 	    }
2397 	  else
2398 	    rel.r_addend = rent->addend;
2399 
2400 	  rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2401 
2402 	  loc = hppa_info->other_rel_sec->contents;
2403 	  loc += (hppa_info->other_rel_sec->reloc_count++
2404 		  * sizeof (Elf64_External_Rela));
2405 	  bfd_elf64_swap_reloca_out (hppa_info->other_rel_sec->output_section->owner,
2406 				     &rel, loc);
2407 	}
2408     }
2409 
2410   return TRUE;
2411 }
2412 
2413 /* Used to decide how to sort relocs in an optimal manner for the
2414    dynamic linker, before writing them out.  */
2415 
2416 static enum elf_reloc_type_class
2417 elf64_hppa_reloc_type_class (rela)
2418      const Elf_Internal_Rela *rela;
2419 {
2420   if (ELF64_R_SYM (rela->r_info) == 0)
2421     return reloc_class_relative;
2422 
2423   switch ((int) ELF64_R_TYPE (rela->r_info))
2424     {
2425     case R_PARISC_IPLT:
2426       return reloc_class_plt;
2427     case R_PARISC_COPY:
2428       return reloc_class_copy;
2429     default:
2430       return reloc_class_normal;
2431     }
2432 }
2433 
2434 /* Finish up the dynamic sections.  */
2435 
2436 static bfd_boolean
2437 elf64_hppa_finish_dynamic_sections (output_bfd, info)
2438      bfd *output_bfd;
2439      struct bfd_link_info *info;
2440 {
2441   bfd *dynobj;
2442   asection *sdyn;
2443   struct elf64_hppa_link_hash_table *hppa_info;
2444 
2445   hppa_info = elf64_hppa_hash_table (info);
2446 
2447   /* Finalize the contents of the .opd section.  */
2448   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2449 				elf64_hppa_finalize_opd,
2450 				info);
2451 
2452   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2453 				elf64_hppa_finalize_dynreloc,
2454 				info);
2455 
2456   /* Finalize the contents of the .dlt section.  */
2457   dynobj = elf_hash_table (info)->dynobj;
2458   /* Finalize the contents of the .dlt section.  */
2459   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2460 				elf64_hppa_finalize_dlt,
2461 				info);
2462 
2463   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2464 
2465   if (elf_hash_table (info)->dynamic_sections_created)
2466     {
2467       Elf64_External_Dyn *dyncon, *dynconend;
2468 
2469       BFD_ASSERT (sdyn != NULL);
2470 
2471       dyncon = (Elf64_External_Dyn *) sdyn->contents;
2472       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2473       for (; dyncon < dynconend; dyncon++)
2474 	{
2475 	  Elf_Internal_Dyn dyn;
2476 	  asection *s;
2477 
2478 	  bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2479 
2480 	  switch (dyn.d_tag)
2481 	    {
2482 	    default:
2483 	      break;
2484 
2485 	    case DT_HP_LOAD_MAP:
2486 	      /* Compute the absolute address of 16byte scratchpad area
2487 		 for the dynamic linker.
2488 
2489 		 By convention the linker script will allocate the scratchpad
2490 		 area at the start of the .data section.  So all we have to
2491 		 to is find the start of the .data section.  */
2492 	      s = bfd_get_section_by_name (output_bfd, ".data");
2493 	      dyn.d_un.d_ptr = s->vma;
2494 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2495 	      break;
2496 
2497 	    case DT_PLTGOT:
2498 	      /* HP's use PLTGOT to set the GOT register.  */
2499 	      dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2500 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2501 	      break;
2502 
2503 	    case DT_JMPREL:
2504 	      s = hppa_info->plt_rel_sec;
2505 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2506 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2507 	      break;
2508 
2509 	    case DT_PLTRELSZ:
2510 	      s = hppa_info->plt_rel_sec;
2511 	      dyn.d_un.d_val = s->size;
2512 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2513 	      break;
2514 
2515 	    case DT_RELA:
2516 	      s = hppa_info->other_rel_sec;
2517 	      if (! s || ! s->size)
2518 		s = hppa_info->dlt_rel_sec;
2519 	      if (! s || ! s->size)
2520 		s = hppa_info->opd_rel_sec;
2521 	      dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2522 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2523 	      break;
2524 
2525 	    case DT_RELASZ:
2526 	      s = hppa_info->other_rel_sec;
2527 	      dyn.d_un.d_val = s->size;
2528 	      s = hppa_info->dlt_rel_sec;
2529 	      dyn.d_un.d_val += s->size;
2530 	      s = hppa_info->opd_rel_sec;
2531 	      dyn.d_un.d_val += s->size;
2532 	      /* There is some question about whether or not the size of
2533 		 the PLT relocs should be included here.  HP's tools do
2534 		 it, so we'll emulate them.  */
2535 	      s = hppa_info->plt_rel_sec;
2536 	      dyn.d_un.d_val += s->size;
2537 	      bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2538 	      break;
2539 
2540 	    }
2541 	}
2542     }
2543 
2544   return TRUE;
2545 }
2546 
2547 /* Support for core dump NOTE sections.  */
2548 
2549 static bfd_boolean
2550 elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2551 {
2552   int offset;
2553   size_t size;
2554 
2555   switch (note->descsz)
2556     {
2557       default:
2558 	return FALSE;
2559 
2560       case 760:		/* Linux/hppa */
2561 	/* pr_cursig */
2562 	elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2563 
2564 	/* pr_pid */
2565 	elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2566 
2567 	/* pr_reg */
2568 	offset = 112;
2569 	size = 640;
2570 
2571 	break;
2572     }
2573 
2574   /* Make a ".reg/999" section.  */
2575   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2576 					  size, note->descpos + offset);
2577 }
2578 
2579 static bfd_boolean
2580 elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2581 {
2582   char * command;
2583   int n;
2584 
2585   switch (note->descsz)
2586     {
2587     default:
2588       return FALSE;
2589 
2590     case 136:		/* Linux/hppa elf_prpsinfo.  */
2591       elf_tdata (abfd)->core_program
2592 	= _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2593       elf_tdata (abfd)->core_command
2594 	= _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2595     }
2596 
2597   /* Note that for some reason, a spurious space is tacked
2598      onto the end of the args in some (at least one anyway)
2599      implementations, so strip it off if it exists.  */
2600   command = elf_tdata (abfd)->core_command;
2601   n = strlen (command);
2602 
2603   if (0 < n && command[n - 1] == ' ')
2604     command[n - 1] = '\0';
2605 
2606   return TRUE;
2607 }
2608 
2609 /* Return the number of additional phdrs we will need.
2610 
2611    The generic ELF code only creates PT_PHDRs for executables.  The HP
2612    dynamic linker requires PT_PHDRs for dynamic libraries too.
2613 
2614    This routine indicates that the backend needs one additional program
2615    header for that case.
2616 
2617    Note we do not have access to the link info structure here, so we have
2618    to guess whether or not we are building a shared library based on the
2619    existence of a .interp section.  */
2620 
2621 static int
2622 elf64_hppa_additional_program_headers (abfd)
2623      bfd *abfd;
2624 {
2625   asection *s;
2626 
2627   /* If we are creating a shared library, then we have to create a
2628      PT_PHDR segment.  HP's dynamic linker chokes without it.  */
2629   s = bfd_get_section_by_name (abfd, ".interp");
2630   if (! s)
2631     return 1;
2632   return 0;
2633 }
2634 
2635 /* Allocate and initialize any program headers required by this
2636    specific backend.
2637 
2638    The generic ELF code only creates PT_PHDRs for executables.  The HP
2639    dynamic linker requires PT_PHDRs for dynamic libraries too.
2640 
2641    This allocates the PT_PHDR and initializes it in a manner suitable
2642    for the HP linker.
2643 
2644    Note we do not have access to the link info structure here, so we have
2645    to guess whether or not we are building a shared library based on the
2646    existence of a .interp section.  */
2647 
2648 static bfd_boolean
2649 elf64_hppa_modify_segment_map (abfd, info)
2650      bfd *abfd;
2651      struct bfd_link_info *info ATTRIBUTE_UNUSED;
2652 {
2653   struct elf_segment_map *m;
2654   asection *s;
2655 
2656   s = bfd_get_section_by_name (abfd, ".interp");
2657   if (! s)
2658     {
2659       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2660 	if (m->p_type == PT_PHDR)
2661 	  break;
2662       if (m == NULL)
2663 	{
2664 	  m = ((struct elf_segment_map *)
2665 	       bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
2666 	  if (m == NULL)
2667 	    return FALSE;
2668 
2669 	  m->p_type = PT_PHDR;
2670 	  m->p_flags = PF_R | PF_X;
2671 	  m->p_flags_valid = 1;
2672 	  m->p_paddr_valid = 1;
2673 	  m->includes_phdrs = 1;
2674 
2675 	  m->next = elf_tdata (abfd)->segment_map;
2676 	  elf_tdata (abfd)->segment_map = m;
2677 	}
2678     }
2679 
2680   for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2681     if (m->p_type == PT_LOAD)
2682       {
2683 	unsigned int i;
2684 
2685 	for (i = 0; i < m->count; i++)
2686 	  {
2687 	    /* The code "hint" is not really a hint.  It is a requirement
2688 	       for certain versions of the HP dynamic linker.  Worse yet,
2689 	       it must be set even if the shared library does not have
2690 	       any code in its "text" segment (thus the check for .hash
2691 	       to catch this situation).  */
2692 	    if (m->sections[i]->flags & SEC_CODE
2693 		|| (strcmp (m->sections[i]->name, ".hash") == 0))
2694 	      m->p_flags |= (PF_X | PF_HP_CODE);
2695 	  }
2696       }
2697 
2698   return TRUE;
2699 }
2700 
2701 /* Called when writing out an object file to decide the type of a
2702    symbol.  */
2703 static int
2704 elf64_hppa_elf_get_symbol_type (elf_sym, type)
2705      Elf_Internal_Sym *elf_sym;
2706      int type;
2707 {
2708   if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2709     return STT_PARISC_MILLI;
2710   else
2711     return type;
2712 }
2713 
2714 /* Support HP specific sections for core files.  */
2715 static bfd_boolean
2716 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index,
2717 			      const char *typename)
2718 {
2719   if (hdr->p_type == PT_HP_CORE_KERNEL)
2720     {
2721       asection *sect;
2722 
2723       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
2724 	return FALSE;
2725 
2726       sect = bfd_make_section_anyway (abfd, ".kernel");
2727       if (sect == NULL)
2728 	return FALSE;
2729       sect->size = hdr->p_filesz;
2730       sect->filepos = hdr->p_offset;
2731       sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2732       return TRUE;
2733     }
2734 
2735   if (hdr->p_type == PT_HP_CORE_PROC)
2736     {
2737       int sig;
2738 
2739       if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2740 	return FALSE;
2741       if (bfd_bread (&sig, 4, abfd) != 4)
2742 	return FALSE;
2743 
2744       elf_tdata (abfd)->core_signal = sig;
2745 
2746       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
2747 	return FALSE;
2748 
2749       /* GDB uses the ".reg" section to read register contents.  */
2750       return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2751 					      hdr->p_offset);
2752     }
2753 
2754   if (hdr->p_type == PT_HP_CORE_LOADABLE
2755       || hdr->p_type == PT_HP_CORE_STACK
2756       || hdr->p_type == PT_HP_CORE_MMF)
2757     hdr->p_type = PT_LOAD;
2758 
2759   return _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename);
2760 }
2761 
2762 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
2763 {
2764   { ".fini",   5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2765   { ".init",   5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2766   { ".plt",    4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2767   { ".dlt",    4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2768   { ".sdata",  6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2769   { ".sbss",   5, 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2770   { ".tbss",   5, 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
2771   { NULL,      0, 0, 0,            0 }
2772 };
2773 
2774 /* The hash bucket size is the standard one, namely 4.  */
2775 
2776 const struct elf_size_info hppa64_elf_size_info =
2777 {
2778   sizeof (Elf64_External_Ehdr),
2779   sizeof (Elf64_External_Phdr),
2780   sizeof (Elf64_External_Shdr),
2781   sizeof (Elf64_External_Rel),
2782   sizeof (Elf64_External_Rela),
2783   sizeof (Elf64_External_Sym),
2784   sizeof (Elf64_External_Dyn),
2785   sizeof (Elf_External_Note),
2786   4,
2787   1,
2788   64, 3,
2789   ELFCLASS64, EV_CURRENT,
2790   bfd_elf64_write_out_phdrs,
2791   bfd_elf64_write_shdrs_and_ehdr,
2792   bfd_elf64_write_relocs,
2793   bfd_elf64_swap_symbol_in,
2794   bfd_elf64_swap_symbol_out,
2795   bfd_elf64_slurp_reloc_table,
2796   bfd_elf64_slurp_symbol_table,
2797   bfd_elf64_swap_dyn_in,
2798   bfd_elf64_swap_dyn_out,
2799   bfd_elf64_swap_reloc_in,
2800   bfd_elf64_swap_reloc_out,
2801   bfd_elf64_swap_reloca_in,
2802   bfd_elf64_swap_reloca_out
2803 };
2804 
2805 #define TARGET_BIG_SYM			bfd_elf64_hppa_vec
2806 #define TARGET_BIG_NAME			"elf64-hppa"
2807 #define ELF_ARCH			bfd_arch_hppa
2808 #define ELF_MACHINE_CODE		EM_PARISC
2809 /* This is not strictly correct.  The maximum page size for PA2.0 is
2810    64M.  But everything still uses 4k.  */
2811 #define ELF_MAXPAGESIZE			0x1000
2812 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
2813 #define bfd_elf64_bfd_is_local_label_name       elf_hppa_is_local_label_name
2814 #define elf_info_to_howto		elf_hppa_info_to_howto
2815 #define elf_info_to_howto_rel		elf_hppa_info_to_howto_rel
2816 
2817 #define elf_backend_section_from_shdr	elf64_hppa_section_from_shdr
2818 #define elf_backend_object_p		elf64_hppa_object_p
2819 #define elf_backend_final_write_processing \
2820 					elf_hppa_final_write_processing
2821 #define elf_backend_fake_sections	elf_hppa_fake_sections
2822 #define elf_backend_add_symbol_hook	elf_hppa_add_symbol_hook
2823 
2824 #define elf_backend_relocate_section	elf_hppa_relocate_section
2825 
2826 #define bfd_elf64_bfd_final_link	elf_hppa_final_link
2827 
2828 #define elf_backend_create_dynamic_sections \
2829 					elf64_hppa_create_dynamic_sections
2830 #define elf_backend_post_process_headers	elf64_hppa_post_process_headers
2831 
2832 #define elf_backend_adjust_dynamic_symbol \
2833 					elf64_hppa_adjust_dynamic_symbol
2834 
2835 #define elf_backend_size_dynamic_sections \
2836 					elf64_hppa_size_dynamic_sections
2837 
2838 #define elf_backend_finish_dynamic_symbol \
2839 					elf64_hppa_finish_dynamic_symbol
2840 #define elf_backend_finish_dynamic_sections \
2841 					elf64_hppa_finish_dynamic_sections
2842 #define elf_backend_grok_prstatus	elf64_hppa_grok_prstatus
2843 #define elf_backend_grok_psinfo		elf64_hppa_grok_psinfo
2844 
2845 /* Stuff for the BFD linker: */
2846 #define bfd_elf64_bfd_link_hash_table_create \
2847 	elf64_hppa_hash_table_create
2848 
2849 #define elf_backend_check_relocs \
2850 	elf64_hppa_check_relocs
2851 
2852 #define elf_backend_size_info \
2853   hppa64_elf_size_info
2854 
2855 #define elf_backend_additional_program_headers \
2856 	elf64_hppa_additional_program_headers
2857 
2858 #define elf_backend_modify_segment_map \
2859 	elf64_hppa_modify_segment_map
2860 
2861 #define elf_backend_link_output_symbol_hook \
2862 	elf64_hppa_link_output_symbol_hook
2863 
2864 #define elf_backend_want_got_plt	0
2865 #define elf_backend_plt_readonly	0
2866 #define elf_backend_want_plt_sym	0
2867 #define elf_backend_got_header_size     0
2868 #define elf_backend_type_change_ok	TRUE
2869 #define elf_backend_get_symbol_type	elf64_hppa_elf_get_symbol_type
2870 #define elf_backend_reloc_type_class	elf64_hppa_reloc_type_class
2871 #define elf_backend_rela_normal		1
2872 #define elf_backend_special_sections	elf64_hppa_special_sections
2873 #define elf_backend_action_discarded	elf_hppa_action_discarded
2874 #define elf_backend_section_from_phdr   elf64_hppa_section_from_phdr
2875 
2876 #include "elf64-target.h"
2877 
2878 #undef TARGET_BIG_SYM
2879 #define TARGET_BIG_SYM			bfd_elf64_hppa_linux_vec
2880 #undef TARGET_BIG_NAME
2881 #define TARGET_BIG_NAME			"elf64-hppa-linux"
2882 
2883 #define INCLUDED_TARGET_FILE 1
2884 #include "elf64-target.h"
2885