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