OSDN Git Service

daily update
[pf3gnuchains/pf3gnuchains4x.git] / bfd / elf64-hppa.c
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 \f
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                       (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               owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
1179
1180               if (!bfd_elf_link_record_local_dynamic_symbol
1181                     (x->info, owner, dyn_h->sym_indx))
1182                 return FALSE;
1183             }
1184
1185           /* This may not be necessary or desirable anymore now that
1186              we have some support for dealing with section symbols
1187              in dynamic relocs.  But name munging does make the result
1188              much easier to debug.  ie, the EPLT reloc will reference
1189              a symbol like .foobar, instead of .text + offset.  */
1190           if (x->info->shared && h)
1191             {
1192               char *new_name;
1193               struct elf_link_hash_entry *nh;
1194
1195               new_name = alloca (strlen (h->root.root.string) + 2);
1196               new_name[0] = '.';
1197               strcpy (new_name + 1, h->root.root.string);
1198
1199               nh = elf_link_hash_lookup (elf_hash_table (x->info),
1200                                          new_name, TRUE, TRUE, TRUE);
1201
1202               nh->root.type = h->root.type;
1203               nh->root.u.def.value = h->root.u.def.value;
1204               nh->root.u.def.section = h->root.u.def.section;
1205
1206               if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
1207                 return FALSE;
1208
1209              }
1210           dyn_h->opd_offset = x->ofs;
1211           x->ofs += OPD_ENTRY_SIZE;
1212         }
1213
1214       /* Otherwise we do not need an opd entry.  */
1215       else
1216         dyn_h->want_opd = 0;
1217     }
1218   return TRUE;
1219 }
1220
1221 /* HP requires the EI_OSABI field to be filled in.  The assignment to
1222    EI_ABIVERSION may not be strictly necessary.  */
1223
1224 static void
1225 elf64_hppa_post_process_headers (abfd, link_info)
1226      bfd * abfd;
1227      struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
1228 {
1229   Elf_Internal_Ehdr * i_ehdrp;
1230
1231   i_ehdrp = elf_elfheader (abfd);
1232   
1233   i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
1234   i_ehdrp->e_ident[EI_ABIVERSION] = 1;
1235 }
1236
1237 /* Create function descriptor section (.opd).  This section is called .opd
1238    because it contains "official procedure descriptors".  The "official"
1239    refers to the fact that these descriptors are used when taking the address
1240    of a procedure, thus ensuring a unique address for each procedure.  */
1241
1242 static bfd_boolean
1243 get_opd (abfd, info, hppa_info)
1244      bfd *abfd;
1245      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1246      struct elf64_hppa_link_hash_table *hppa_info;
1247 {
1248   asection *opd;
1249   bfd *dynobj;
1250
1251   opd = hppa_info->opd_sec;
1252   if (!opd)
1253     {
1254       dynobj = hppa_info->root.dynobj;
1255       if (!dynobj)
1256         hppa_info->root.dynobj = dynobj = abfd;
1257
1258       opd = bfd_make_section_with_flags (dynobj, ".opd",
1259                                          (SEC_ALLOC
1260                                           | SEC_LOAD
1261                                           | SEC_HAS_CONTENTS
1262                                           | SEC_IN_MEMORY
1263                                           | SEC_LINKER_CREATED));
1264       if (!opd
1265           || !bfd_set_section_alignment (abfd, opd, 3))
1266         {
1267           BFD_ASSERT (0);
1268           return FALSE;
1269         }
1270
1271       hppa_info->opd_sec = opd;
1272     }
1273
1274   return TRUE;
1275 }
1276
1277 /* Create the PLT section.  */
1278
1279 static bfd_boolean
1280 get_plt (abfd, info, hppa_info)
1281      bfd *abfd;
1282      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1283      struct elf64_hppa_link_hash_table *hppa_info;
1284 {
1285   asection *plt;
1286   bfd *dynobj;
1287
1288   plt = hppa_info->plt_sec;
1289   if (!plt)
1290     {
1291       dynobj = hppa_info->root.dynobj;
1292       if (!dynobj)
1293         hppa_info->root.dynobj = dynobj = abfd;
1294
1295       plt = bfd_make_section_with_flags (dynobj, ".plt",
1296                                          (SEC_ALLOC
1297                                           | SEC_LOAD
1298                                           | SEC_HAS_CONTENTS
1299                                           | SEC_IN_MEMORY
1300                                           | SEC_LINKER_CREATED));
1301       if (!plt
1302           || !bfd_set_section_alignment (abfd, plt, 3))
1303         {
1304           BFD_ASSERT (0);
1305           return FALSE;
1306         }
1307
1308       hppa_info->plt_sec = plt;
1309     }
1310
1311   return TRUE;
1312 }
1313
1314 /* Create the DLT section.  */
1315
1316 static bfd_boolean
1317 get_dlt (abfd, info, hppa_info)
1318      bfd *abfd;
1319      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1320      struct elf64_hppa_link_hash_table *hppa_info;
1321 {
1322   asection *dlt;
1323   bfd *dynobj;
1324
1325   dlt = hppa_info->dlt_sec;
1326   if (!dlt)
1327     {
1328       dynobj = hppa_info->root.dynobj;
1329       if (!dynobj)
1330         hppa_info->root.dynobj = dynobj = abfd;
1331
1332       dlt = bfd_make_section_with_flags (dynobj, ".dlt",
1333                                          (SEC_ALLOC
1334                                           | SEC_LOAD
1335                                           | SEC_HAS_CONTENTS
1336                                           | SEC_IN_MEMORY
1337                                           | SEC_LINKER_CREATED));
1338       if (!dlt
1339           || !bfd_set_section_alignment (abfd, dlt, 3))
1340         {
1341           BFD_ASSERT (0);
1342           return FALSE;
1343         }
1344
1345       hppa_info->dlt_sec = dlt;
1346     }
1347
1348   return TRUE;
1349 }
1350
1351 /* Create the stubs section.  */
1352
1353 static bfd_boolean
1354 get_stub (abfd, info, hppa_info)
1355      bfd *abfd;
1356      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1357      struct elf64_hppa_link_hash_table *hppa_info;
1358 {
1359   asection *stub;
1360   bfd *dynobj;
1361
1362   stub = hppa_info->stub_sec;
1363   if (!stub)
1364     {
1365       dynobj = hppa_info->root.dynobj;
1366       if (!dynobj)
1367         hppa_info->root.dynobj = dynobj = abfd;
1368
1369       stub = bfd_make_section_with_flags (dynobj, ".stub",
1370                                           (SEC_ALLOC | SEC_LOAD
1371                                            | SEC_HAS_CONTENTS
1372                                            | SEC_IN_MEMORY
1373                                            | SEC_READONLY
1374                                            | SEC_LINKER_CREATED));
1375       if (!stub
1376           || !bfd_set_section_alignment (abfd, stub, 3))
1377         {
1378           BFD_ASSERT (0);
1379           return FALSE;
1380         }
1381
1382       hppa_info->stub_sec = stub;
1383     }
1384
1385   return TRUE;
1386 }
1387
1388 /* Create sections necessary for dynamic linking.  This is only a rough
1389    cut and will likely change as we learn more about the somewhat
1390    unusual dynamic linking scheme HP uses.
1391
1392    .stub:
1393         Contains code to implement cross-space calls.  The first time one
1394         of the stubs is used it will call into the dynamic linker, later
1395         calls will go straight to the target.
1396
1397         The only stub we support right now looks like
1398
1399         ldd OFFSET(%dp),%r1
1400         bve %r0(%r1)
1401         ldd OFFSET+8(%dp),%dp
1402
1403         Other stubs may be needed in the future.  We may want the remove
1404         the break/nop instruction.  It is only used right now to keep the
1405         offset of a .plt entry and a .stub entry in sync.
1406
1407    .dlt:
1408         This is what most people call the .got.  HP used a different name.
1409         Losers.
1410
1411    .rela.dlt:
1412         Relocations for the DLT.
1413
1414    .plt:
1415         Function pointers as address,gp pairs.
1416
1417    .rela.plt:
1418         Should contain dynamic IPLT (and EPLT?) relocations.
1419
1420    .opd:
1421         FPTRS
1422
1423    .rela.opd:
1424         EPLT relocations for symbols exported from shared libraries.  */
1425
1426 static bfd_boolean
1427 elf64_hppa_create_dynamic_sections (abfd, info)
1428      bfd *abfd;
1429      struct bfd_link_info *info;
1430 {
1431   asection *s;
1432
1433   if (! get_stub (abfd, info, elf64_hppa_hash_table (info)))
1434     return FALSE;
1435
1436   if (! get_dlt (abfd, info, elf64_hppa_hash_table (info)))
1437     return FALSE;
1438
1439   if (! get_plt (abfd, info, elf64_hppa_hash_table (info)))
1440     return FALSE;
1441
1442   if (! get_opd (abfd, info, elf64_hppa_hash_table (info)))
1443     return FALSE;
1444
1445   s = bfd_make_section_with_flags (abfd, ".rela.dlt",
1446                                    (SEC_ALLOC | SEC_LOAD
1447                                     | SEC_HAS_CONTENTS
1448                                     | SEC_IN_MEMORY
1449                                     | SEC_READONLY
1450                                     | SEC_LINKER_CREATED));
1451   if (s == NULL
1452       || !bfd_set_section_alignment (abfd, s, 3))
1453     return FALSE;
1454   elf64_hppa_hash_table (info)->dlt_rel_sec = s;
1455
1456   s = bfd_make_section_with_flags (abfd, ".rela.plt",
1457                                    (SEC_ALLOC | SEC_LOAD
1458                                     | SEC_HAS_CONTENTS
1459                                     | SEC_IN_MEMORY
1460                                     | SEC_READONLY
1461                                     | SEC_LINKER_CREATED));
1462   if (s == NULL
1463       || !bfd_set_section_alignment (abfd, s, 3))
1464     return FALSE;
1465   elf64_hppa_hash_table (info)->plt_rel_sec = s;
1466
1467   s = bfd_make_section_with_flags (abfd, ".rela.data",
1468                                    (SEC_ALLOC | SEC_LOAD
1469                                     | SEC_HAS_CONTENTS
1470                                     | SEC_IN_MEMORY
1471                                     | SEC_READONLY
1472                                     | SEC_LINKER_CREATED));
1473   if (s == NULL
1474       || !bfd_set_section_alignment (abfd, s, 3))
1475     return FALSE;
1476   elf64_hppa_hash_table (info)->other_rel_sec = s;
1477
1478   s = bfd_make_section_with_flags (abfd, ".rela.opd",
1479                                    (SEC_ALLOC | SEC_LOAD
1480                                     | SEC_HAS_CONTENTS
1481                                     | SEC_IN_MEMORY
1482                                     | SEC_READONLY
1483                                     | SEC_LINKER_CREATED));
1484   if (s == NULL
1485       || !bfd_set_section_alignment (abfd, s, 3))
1486     return FALSE;
1487   elf64_hppa_hash_table (info)->opd_rel_sec = s;
1488
1489   return TRUE;
1490 }
1491
1492 /* Allocate dynamic relocations for those symbols that turned out
1493    to be dynamic.  */
1494
1495 static bfd_boolean
1496 allocate_dynrel_entries (dyn_h, data)
1497      struct elf64_hppa_dyn_hash_entry *dyn_h;
1498      PTR data;
1499 {
1500   struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1501   struct elf64_hppa_link_hash_table *hppa_info;
1502   struct elf64_hppa_dyn_reloc_entry *rent;
1503   bfd_boolean dynamic_symbol, shared;
1504
1505   hppa_info = elf64_hppa_hash_table (x->info);
1506   dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info);
1507   shared = x->info->shared;
1508
1509   /* We may need to allocate relocations for a non-dynamic symbol
1510      when creating a shared library.  */
1511   if (!dynamic_symbol && !shared)
1512     return TRUE;
1513
1514   /* Take care of the normal data relocations.  */
1515
1516   for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
1517     {
1518       /* Allocate one iff we are building a shared library, the relocation
1519          isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
1520       if (!shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
1521         continue;
1522
1523       hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
1524
1525       /* Make sure this symbol gets into the dynamic symbol table if it is
1526          not already recorded.  ?!? This should not be in the loop since
1527          the symbol need only be added once.  */
1528       if (dyn_h->h == 0
1529           || (dyn_h->h->dynindx == -1 && dyn_h->h->type != STT_PARISC_MILLI))
1530         if (!bfd_elf_link_record_local_dynamic_symbol
1531             (x->info, rent->sec->owner, dyn_h->sym_indx))
1532           return FALSE;
1533     }
1534
1535   /* Take care of the GOT and PLT relocations.  */
1536
1537   if ((dynamic_symbol || shared) && dyn_h->want_dlt)
1538     hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
1539
1540   /* If we are building a shared library, then every symbol that has an
1541      opd entry will need an EPLT relocation to relocate the symbol's address
1542      and __gp value based on the runtime load address.  */
1543   if (shared && dyn_h->want_opd)
1544     hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
1545
1546   if (dyn_h->want_plt && dynamic_symbol)
1547     {
1548       bfd_size_type t = 0;
1549
1550       /* Dynamic symbols get one IPLT relocation.  Local symbols in
1551          shared libraries get two REL relocations.  Local symbols in
1552          main applications get nothing.  */
1553       if (dynamic_symbol)
1554         t = sizeof (Elf64_External_Rela);
1555       else if (shared)
1556         t = 2 * sizeof (Elf64_External_Rela);
1557
1558       hppa_info->plt_rel_sec->size += t;
1559     }
1560
1561   return TRUE;
1562 }
1563
1564 /* Adjust a symbol defined by a dynamic object and referenced by a
1565    regular object.  */
1566
1567 static bfd_boolean
1568 elf64_hppa_adjust_dynamic_symbol (info, h)
1569      struct bfd_link_info *info ATTRIBUTE_UNUSED;
1570      struct elf_link_hash_entry *h;
1571 {
1572   /* ??? Undefined symbols with PLT entries should be re-defined
1573      to be the PLT entry.  */
1574
1575   /* If this is a weak symbol, and there is a real definition, the
1576      processor independent code will have arranged for us to see the
1577      real definition first, and we can just use the same value.  */
1578   if (h->u.weakdef != NULL)
1579     {
1580       BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1581                   || h->u.weakdef->root.type == bfd_link_hash_defweak);
1582       h->root.u.def.section = h->u.weakdef->root.u.def.section;
1583       h->root.u.def.value = h->u.weakdef->root.u.def.value;
1584       return TRUE;
1585     }
1586
1587   /* If this is a reference to a symbol defined by a dynamic object which
1588      is not a function, we might allocate the symbol in our .dynbss section
1589      and allocate a COPY dynamic relocation.
1590
1591      But PA64 code is canonically PIC, so as a rule we can avoid this sort
1592      of hackery.  */
1593
1594   return TRUE;
1595 }
1596
1597 /* This function is called via elf_link_hash_traverse to mark millicode
1598    symbols with a dynindx of -1 and to remove the string table reference
1599    from the dynamic symbol table.  If the symbol is not a millicode symbol,
1600    elf64_hppa_mark_exported_functions is called.  */
1601
1602 static bfd_boolean
1603 elf64_hppa_mark_milli_and_exported_functions (h, data)
1604      struct elf_link_hash_entry *h;
1605      PTR data;
1606 {
1607   struct bfd_link_info *info = (struct bfd_link_info *)data;
1608   struct elf_link_hash_entry *elf = h;
1609
1610   if (elf->root.type == bfd_link_hash_warning)
1611     elf = (struct elf_link_hash_entry *) elf->root.u.i.link;
1612
1613   if (elf->type == STT_PARISC_MILLI)
1614     {
1615       if (elf->dynindx != -1)
1616         {
1617           elf->dynindx = -1;
1618           _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1619                                   elf->dynstr_index);
1620         }
1621       return TRUE;
1622     }
1623
1624   return elf64_hppa_mark_exported_functions (h, data);
1625 }
1626
1627 /* Set the final sizes of the dynamic sections and allocate memory for
1628    the contents of our special sections.  */
1629
1630 static bfd_boolean
1631 elf64_hppa_size_dynamic_sections (output_bfd, info)
1632      bfd *output_bfd;
1633      struct bfd_link_info *info;
1634 {
1635   bfd *dynobj;
1636   asection *s;
1637   bfd_boolean plt;
1638   bfd_boolean relocs;
1639   bfd_boolean reltext;
1640   struct elf64_hppa_allocate_data data;
1641   struct elf64_hppa_link_hash_table *hppa_info;
1642
1643   hppa_info = elf64_hppa_hash_table (info);
1644
1645   dynobj = elf_hash_table (info)->dynobj;
1646   BFD_ASSERT (dynobj != NULL);
1647
1648   /* Mark each function this program exports so that we will allocate
1649      space in the .opd section for each function's FPTR.  If we are
1650      creating dynamic sections, change the dynamic index of millicode
1651      symbols to -1 and remove them from the string table for .dynstr.
1652
1653      We have to traverse the main linker hash table since we have to
1654      find functions which may not have been mentioned in any relocs.  */
1655   elf_link_hash_traverse (elf_hash_table (info),
1656                           (elf_hash_table (info)->dynamic_sections_created
1657                            ? elf64_hppa_mark_milli_and_exported_functions
1658                            : elf64_hppa_mark_exported_functions),
1659                           info);
1660
1661   if (elf_hash_table (info)->dynamic_sections_created)
1662     {
1663       /* Set the contents of the .interp section to the interpreter.  */
1664       if (info->executable)
1665         {
1666           s = bfd_get_section_by_name (dynobj, ".interp");
1667           BFD_ASSERT (s != NULL);
1668           s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1669           s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1670         }
1671     }
1672   else
1673     {
1674       /* We may have created entries in the .rela.got section.
1675          However, if we are not creating the dynamic sections, we will
1676          not actually use these entries.  Reset the size of .rela.dlt,
1677          which will cause it to get stripped from the output file
1678          below.  */
1679       s = bfd_get_section_by_name (dynobj, ".rela.dlt");
1680       if (s != NULL)
1681         s->size = 0;
1682     }
1683
1684   /* Allocate the GOT entries.  */
1685
1686   data.info = info;
1687   if (elf64_hppa_hash_table (info)->dlt_sec)
1688     {
1689       data.ofs = 0x0;
1690       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1691                                     allocate_global_data_dlt, &data);
1692       hppa_info->dlt_sec->size = data.ofs;
1693
1694       data.ofs = 0x0;
1695       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1696                                     allocate_global_data_plt, &data);
1697       hppa_info->plt_sec->size = data.ofs;
1698
1699       data.ofs = 0x0;
1700       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1701                                     allocate_global_data_stub, &data);
1702       hppa_info->stub_sec->size = data.ofs;
1703     }
1704
1705   /* Allocate space for entries in the .opd section.  */
1706   if (elf64_hppa_hash_table (info)->opd_sec)
1707     {
1708       data.ofs = 0;
1709       elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1710                                     allocate_global_data_opd, &data);
1711       hppa_info->opd_sec->size = data.ofs;
1712     }
1713
1714   /* Now allocate space for dynamic relocations, if necessary.  */
1715   if (hppa_info->root.dynamic_sections_created)
1716     elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1717                                   allocate_dynrel_entries, &data);
1718
1719   /* The sizes of all the sections are set.  Allocate memory for them.  */
1720   plt = FALSE;
1721   relocs = FALSE;
1722   reltext = FALSE;
1723   for (s = dynobj->sections; s != NULL; s = s->next)
1724     {
1725       const char *name;
1726
1727       if ((s->flags & SEC_LINKER_CREATED) == 0)
1728         continue;
1729
1730       /* It's OK to base decisions on the section name, because none
1731          of the dynobj section names depend upon the input files.  */
1732       name = bfd_get_section_name (dynobj, s);
1733
1734       if (strcmp (name, ".plt") == 0)
1735         {
1736           /* Remember whether there is a PLT.  */
1737           plt = s->size != 0;
1738         }
1739       else if (strcmp (name, ".opd") == 0
1740                || CONST_STRNEQ (name, ".dlt")
1741                || strcmp (name, ".stub") == 0
1742                || strcmp (name, ".got") == 0)
1743         {
1744           /* Strip this section if we don't need it; see the comment below.  */
1745         }
1746       else if (CONST_STRNEQ (name, ".rela"))
1747         {
1748           if (s->size != 0)
1749             {
1750               asection *target;
1751
1752               /* Remember whether there are any reloc sections other
1753                  than .rela.plt.  */
1754               if (strcmp (name, ".rela.plt") != 0)
1755                 {
1756                   const char *outname;
1757
1758                   relocs = TRUE;
1759
1760                   /* If this relocation section applies to a read only
1761                      section, then we probably need a DT_TEXTREL
1762                      entry.  The entries in the .rela.plt section
1763                      really apply to the .got section, which we
1764                      created ourselves and so know is not readonly.  */
1765                   outname = bfd_get_section_name (output_bfd,
1766                                                   s->output_section);
1767                   target = bfd_get_section_by_name (output_bfd, outname + 4);
1768                   if (target != NULL
1769                       && (target->flags & SEC_READONLY) != 0
1770                       && (target->flags & SEC_ALLOC) != 0)
1771                     reltext = TRUE;
1772                 }
1773
1774               /* We use the reloc_count field as a counter if we need
1775                  to copy relocs into the output file.  */
1776               s->reloc_count = 0;
1777             }
1778         }
1779       else
1780         {
1781           /* It's not one of our sections, so don't allocate space.  */
1782           continue;
1783         }
1784
1785       if (s->size == 0)
1786         {
1787           /* If we don't need this section, strip it from the
1788              output file.  This is mostly to handle .rela.bss and
1789              .rela.plt.  We must create both sections in
1790              create_dynamic_sections, because they must be created
1791              before the linker maps input sections to output
1792              sections.  The linker does that before
1793              adjust_dynamic_symbol is called, and it is that
1794              function which decides whether anything needs to go
1795              into these sections.  */
1796           s->flags |= SEC_EXCLUDE;
1797           continue;
1798         }
1799
1800       if ((s->flags & SEC_HAS_CONTENTS) == 0)
1801         continue;
1802
1803       /* Allocate memory for the section contents if it has not
1804          been allocated already.  We use bfd_zalloc here in case
1805          unused entries are not reclaimed before the section's
1806          contents are written out.  This should not happen, but this
1807          way if it does, we get a R_PARISC_NONE reloc instead of
1808          garbage.  */
1809       if (s->contents == NULL)
1810         {
1811           s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1812           if (s->contents == NULL)
1813             return FALSE;
1814         }
1815     }
1816
1817   if (elf_hash_table (info)->dynamic_sections_created)
1818     {
1819       /* Always create a DT_PLTGOT.  It actually has nothing to do with
1820          the PLT, it is how we communicate the __gp value of a load
1821          module to the dynamic linker.  */
1822 #define add_dynamic_entry(TAG, VAL) \
1823   _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1824
1825       if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0)
1826           || !add_dynamic_entry (DT_PLTGOT, 0))
1827         return FALSE;
1828
1829       /* Add some entries to the .dynamic section.  We fill in the
1830          values later, in elf64_hppa_finish_dynamic_sections, but we
1831          must add the entries now so that we get the correct size for
1832          the .dynamic section.  The DT_DEBUG entry is filled in by the
1833          dynamic linker and used by the debugger.  */
1834       if (! info->shared)
1835         {
1836           if (!add_dynamic_entry (DT_DEBUG, 0)
1837               || !add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1838               || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
1839             return FALSE;
1840         }
1841
1842       /* Force DT_FLAGS to always be set.
1843          Required by HPUX 11.00 patch PHSS_26559.  */
1844       if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1845         return FALSE;
1846
1847       if (plt)
1848         {
1849           if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1850               || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1851               || !add_dynamic_entry (DT_JMPREL, 0))
1852             return FALSE;
1853         }
1854
1855       if (relocs)
1856         {
1857           if (!add_dynamic_entry (DT_RELA, 0)
1858               || !add_dynamic_entry (DT_RELASZ, 0)
1859               || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1860             return FALSE;
1861         }
1862
1863       if (reltext)
1864         {
1865           if (!add_dynamic_entry (DT_TEXTREL, 0))
1866             return FALSE;
1867           info->flags |= DF_TEXTREL;
1868         }
1869     }
1870 #undef add_dynamic_entry
1871
1872   return TRUE;
1873 }
1874
1875 /* Called after we have output the symbol into the dynamic symbol
1876    table, but before we output the symbol into the normal symbol
1877    table.
1878
1879    For some symbols we had to change their address when outputting
1880    the dynamic symbol table.  We undo that change here so that
1881    the symbols have their expected value in the normal symbol
1882    table.  Ick.  */
1883
1884 static bfd_boolean
1885 elf64_hppa_link_output_symbol_hook (info, name, sym, input_sec, h)
1886      struct bfd_link_info *info;
1887      const char *name;
1888      Elf_Internal_Sym *sym;
1889      asection *input_sec ATTRIBUTE_UNUSED;
1890      struct elf_link_hash_entry *h;
1891 {
1892   struct elf64_hppa_link_hash_table *hppa_info;
1893   struct elf64_hppa_dyn_hash_entry *dyn_h;
1894
1895   /* We may be called with the file symbol or section symbols.
1896      They never need munging, so it is safe to ignore them.  */
1897   if (!name)
1898     return TRUE;
1899
1900   /* Get the PA dyn_symbol (if any) associated with NAME.  */
1901   hppa_info = elf64_hppa_hash_table (info);
1902   dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
1903                                       name, FALSE, FALSE);
1904   if (!dyn_h || dyn_h->h != h)
1905     return TRUE;
1906
1907   /* Function symbols for which we created .opd entries *may* have been
1908      munged by finish_dynamic_symbol and have to be un-munged here.
1909
1910      Note that finish_dynamic_symbol sometimes turns dynamic symbols
1911      into non-dynamic ones, so we initialize st_shndx to -1 in
1912      mark_exported_functions and check to see if it was overwritten
1913      here instead of just checking dyn_h->h->dynindx.  */
1914   if (dyn_h->want_opd && dyn_h->st_shndx != -1)
1915     {
1916       /* Restore the saved value and section index.  */
1917       sym->st_value = dyn_h->st_value;
1918       sym->st_shndx = dyn_h->st_shndx;
1919     }
1920
1921   return TRUE;
1922 }
1923
1924 /* Finish up dynamic symbol handling.  We set the contents of various
1925    dynamic sections here.  */
1926
1927 static bfd_boolean
1928 elf64_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
1929      bfd *output_bfd;
1930      struct bfd_link_info *info;
1931      struct elf_link_hash_entry *h;
1932      Elf_Internal_Sym *sym;
1933 {
1934   asection *stub, *splt, *sdlt, *sopd, *spltrel, *sdltrel;
1935   struct elf64_hppa_link_hash_table *hppa_info;
1936   struct elf64_hppa_dyn_hash_entry *dyn_h;
1937
1938   hppa_info = elf64_hppa_hash_table (info);
1939   dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
1940                                       h->root.root.string, FALSE, FALSE);
1941
1942   stub = hppa_info->stub_sec;
1943   splt = hppa_info->plt_sec;
1944   sdlt = hppa_info->dlt_sec;
1945   sopd = hppa_info->opd_sec;
1946   spltrel = hppa_info->plt_rel_sec;
1947   sdltrel = hppa_info->dlt_rel_sec;
1948
1949   /* Incredible.  It is actually necessary to NOT use the symbol's real
1950      value when building the dynamic symbol table for a shared library.
1951      At least for symbols that refer to functions.
1952
1953      We will store a new value and section index into the symbol long
1954      enough to output it into the dynamic symbol table, then we restore
1955      the original values (in elf64_hppa_link_output_symbol_hook).  */
1956   if (dyn_h && dyn_h->want_opd)
1957     {
1958       BFD_ASSERT (sopd != NULL);
1959
1960       /* Save away the original value and section index so that we
1961          can restore them later.  */
1962       dyn_h->st_value = sym->st_value;
1963       dyn_h->st_shndx = sym->st_shndx;
1964
1965       /* For the dynamic symbol table entry, we want the value to be
1966          address of this symbol's entry within the .opd section.  */
1967       sym->st_value = (dyn_h->opd_offset
1968                        + sopd->output_offset
1969                        + sopd->output_section->vma);
1970       sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1971                                                          sopd->output_section);
1972     }
1973
1974   /* Initialize a .plt entry if requested.  */
1975   if (dyn_h && dyn_h->want_plt
1976       && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
1977     {
1978       bfd_vma value;
1979       Elf_Internal_Rela rel;
1980       bfd_byte *loc;
1981
1982       BFD_ASSERT (splt != NULL && spltrel != NULL);
1983
1984       /* We do not actually care about the value in the PLT entry
1985          if we are creating a shared library and the symbol is
1986          still undefined, we create a dynamic relocation to fill
1987          in the correct value.  */
1988       if (info->shared && h->root.type == bfd_link_hash_undefined)
1989         value = 0;
1990       else
1991         value = (h->root.u.def.value + h->root.u.def.section->vma);
1992
1993       /* Fill in the entry in the procedure linkage table.
1994
1995          The format of a plt entry is
1996          <funcaddr> <__gp>.
1997
1998          plt_offset is the offset within the PLT section at which to
1999          install the PLT entry.
2000
2001          We are modifying the in-memory PLT contents here, so we do not add
2002          in the output_offset of the PLT section.  */
2003
2004       bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset);
2005       value = _bfd_get_gp_value (splt->output_section->owner);
2006       bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset + 0x8);
2007
2008       /* Create a dynamic IPLT relocation for this entry.
2009
2010          We are creating a relocation in the output file's PLT section,
2011          which is included within the DLT secton.  So we do need to include
2012          the PLT's output_offset in the computation of the relocation's
2013          address.  */
2014       rel.r_offset = (dyn_h->plt_offset + splt->output_offset
2015                       + splt->output_section->vma);
2016       rel.r_info = ELF64_R_INFO (h->dynindx, R_PARISC_IPLT);
2017       rel.r_addend = 0;
2018
2019       loc = spltrel->contents;
2020       loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2021       bfd_elf64_swap_reloca_out (splt->output_section->owner, &rel, loc);
2022     }
2023
2024   /* Initialize an external call stub entry if requested.  */
2025   if (dyn_h && dyn_h->want_stub
2026       && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
2027     {
2028       bfd_vma value;
2029       int insn;
2030       unsigned int max_offset;
2031
2032       BFD_ASSERT (stub != NULL);
2033
2034       /* Install the generic stub template.
2035
2036          We are modifying the contents of the stub section, so we do not
2037          need to include the stub section's output_offset here.  */
2038       memcpy (stub->contents + dyn_h->stub_offset, plt_stub, sizeof (plt_stub));
2039
2040       /* Fix up the first ldd instruction.
2041
2042          We are modifying the contents of the STUB section in memory,
2043          so we do not need to include its output offset in this computation.
2044
2045          Note the plt_offset value is the value of the PLT entry relative to
2046          the start of the PLT section.  These instructions will reference
2047          data relative to the value of __gp, which may not necessarily have
2048          the same address as the start of the PLT section.
2049
2050          gp_offset contains the offset of __gp within the PLT section.  */
2051       value = dyn_h->plt_offset - hppa_info->gp_offset;
2052
2053       insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset);
2054       if (output_bfd->arch_info->mach >= 25)
2055         {
2056           /* Wide mode allows 16 bit offsets.  */
2057           max_offset = 32768;
2058           insn &= ~ 0xfff1;
2059           insn |= re_assemble_16 ((int) value);
2060         }
2061       else
2062         {
2063           max_offset = 8192;
2064           insn &= ~ 0x3ff1;
2065           insn |= re_assemble_14 ((int) value);
2066         }
2067
2068       if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2069         {
2070           (*_bfd_error_handler) (_("stub entry for %s cannot load .plt, dp offset = %ld"),
2071                                  dyn_h->root.string,
2072                                  (long) value);
2073           return FALSE;
2074         }
2075
2076       bfd_put_32 (stub->owner, (bfd_vma) insn,
2077                   stub->contents + dyn_h->stub_offset);
2078
2079       /* Fix up the second ldd instruction.  */
2080       value += 8;
2081       insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset + 8);
2082       if (output_bfd->arch_info->mach >= 25)
2083         {
2084           insn &= ~ 0xfff1;
2085           insn |= re_assemble_16 ((int) value);
2086         }
2087       else
2088         {
2089           insn &= ~ 0x3ff1;
2090           insn |= re_assemble_14 ((int) value);
2091         }
2092       bfd_put_32 (stub->owner, (bfd_vma) insn,
2093                   stub->contents + dyn_h->stub_offset + 8);
2094     }
2095
2096   return TRUE;
2097 }
2098
2099 /* The .opd section contains FPTRs for each function this file
2100    exports.  Initialize the FPTR entries.  */
2101
2102 static bfd_boolean
2103 elf64_hppa_finalize_opd (dyn_h, data)
2104      struct elf64_hppa_dyn_hash_entry *dyn_h;
2105      PTR data;
2106 {
2107   struct bfd_link_info *info = (struct bfd_link_info *)data;
2108   struct elf64_hppa_link_hash_table *hppa_info;
2109   struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
2110   asection *sopd;
2111   asection *sopdrel;
2112
2113   hppa_info = elf64_hppa_hash_table (info);
2114   sopd = hppa_info->opd_sec;
2115   sopdrel = hppa_info->opd_rel_sec;
2116
2117   if (h && dyn_h->want_opd)
2118     {
2119       bfd_vma value;
2120
2121       /* The first two words of an .opd entry are zero.
2122
2123          We are modifying the contents of the OPD section in memory, so we
2124          do not need to include its output offset in this computation.  */
2125       memset (sopd->contents + dyn_h->opd_offset, 0, 16);
2126
2127       value = (h->root.u.def.value
2128                + h->root.u.def.section->output_section->vma
2129                + h->root.u.def.section->output_offset);
2130
2131       /* The next word is the address of the function.  */
2132       bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 16);
2133
2134       /* The last word is our local __gp value.  */
2135       value = _bfd_get_gp_value (sopd->output_section->owner);
2136       bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 24);
2137     }
2138
2139   /* If we are generating a shared library, we must generate EPLT relocations
2140      for each entry in the .opd, even for static functions (they may have
2141      had their address taken).  */
2142   if (info->shared && dyn_h && dyn_h->want_opd)
2143     {
2144       Elf_Internal_Rela rel;
2145       bfd_byte *loc;
2146       int dynindx;
2147
2148       /* We may need to do a relocation against a local symbol, in
2149          which case we have to look up it's dynamic symbol index off
2150          the local symbol hash table.  */
2151       if (h && h->dynindx != -1)
2152         dynindx = h->dynindx;
2153       else
2154         dynindx
2155           = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2156                                                 dyn_h->sym_indx);
2157
2158       /* The offset of this relocation is the absolute address of the
2159          .opd entry for this symbol.  */
2160       rel.r_offset = (dyn_h->opd_offset + sopd->output_offset
2161                       + sopd->output_section->vma);
2162
2163       /* If H is non-null, then we have an external symbol.
2164
2165          It is imperative that we use a different dynamic symbol for the
2166          EPLT relocation if the symbol has global scope.
2167
2168          In the dynamic symbol table, the function symbol will have a value
2169          which is address of the function's .opd entry.
2170
2171          Thus, we can not use that dynamic symbol for the EPLT relocation
2172          (if we did, the data in the .opd would reference itself rather
2173          than the actual address of the function).  Instead we have to use
2174          a new dynamic symbol which has the same value as the original global
2175          function symbol.
2176
2177          We prefix the original symbol with a "." and use the new symbol in
2178          the EPLT relocation.  This new symbol has already been recorded in
2179          the symbol table, we just have to look it up and use it.
2180
2181          We do not have such problems with static functions because we do
2182          not make their addresses in the dynamic symbol table point to
2183          the .opd entry.  Ultimately this should be safe since a static
2184          function can not be directly referenced outside of its shared
2185          library.
2186
2187          We do have to play similar games for FPTR relocations in shared
2188          libraries, including those for static symbols.  See the FPTR
2189          handling in elf64_hppa_finalize_dynreloc.  */
2190       if (h)
2191         {
2192           char *new_name;
2193           struct elf_link_hash_entry *nh;
2194
2195           new_name = alloca (strlen (h->root.root.string) + 2);
2196           new_name[0] = '.';
2197           strcpy (new_name + 1, h->root.root.string);
2198
2199           nh = elf_link_hash_lookup (elf_hash_table (info),
2200                                      new_name, FALSE, FALSE, FALSE);
2201
2202           /* All we really want from the new symbol is its dynamic
2203              symbol index.  */
2204           dynindx = nh->dynindx;
2205         }
2206
2207       rel.r_addend = 0;
2208       rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2209
2210       loc = sopdrel->contents;
2211       loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2212       bfd_elf64_swap_reloca_out (sopd->output_section->owner, &rel, loc);
2213     }
2214   return TRUE;
2215 }
2216
2217 /* The .dlt section contains addresses for items referenced through the
2218    dlt.  Note that we can have a DLTIND relocation for a local symbol, thus
2219    we can not depend on finish_dynamic_symbol to initialize the .dlt.  */
2220
2221 static bfd_boolean
2222 elf64_hppa_finalize_dlt (dyn_h, data)
2223      struct elf64_hppa_dyn_hash_entry *dyn_h;
2224      PTR data;
2225 {
2226   struct bfd_link_info *info = (struct bfd_link_info *)data;
2227   struct elf64_hppa_link_hash_table *hppa_info;
2228   asection *sdlt, *sdltrel;
2229   struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
2230
2231   hppa_info = elf64_hppa_hash_table (info);
2232
2233   sdlt = hppa_info->dlt_sec;
2234   sdltrel = hppa_info->dlt_rel_sec;
2235
2236   /* H/DYN_H may refer to a local variable and we know it's
2237      address, so there is no need to create a relocation.  Just install
2238      the proper value into the DLT, note this shortcut can not be
2239      skipped when building a shared library.  */
2240   if (! info->shared && h && dyn_h->want_dlt)
2241     {
2242       bfd_vma value;
2243
2244       /* If we had an LTOFF_FPTR style relocation we want the DLT entry
2245          to point to the FPTR entry in the .opd section.
2246
2247          We include the OPD's output offset in this computation as
2248          we are referring to an absolute address in the resulting
2249          object file.  */
2250       if (dyn_h->want_opd)
2251         {
2252           value = (dyn_h->opd_offset
2253                    + hppa_info->opd_sec->output_offset
2254                    + hppa_info->opd_sec->output_section->vma);
2255         }
2256       else if ((h->root.type == bfd_link_hash_defined
2257                 || h->root.type == bfd_link_hash_defweak)
2258                && h->root.u.def.section)
2259         {
2260           value = h->root.u.def.value + h->root.u.def.section->output_offset;
2261           if (h->root.u.def.section->output_section)
2262             value += h->root.u.def.section->output_section->vma;
2263           else
2264             value += h->root.u.def.section->vma;
2265         }
2266       else
2267         /* We have an undefined function reference.  */
2268         value = 0;
2269
2270       /* We do not need to include the output offset of the DLT section
2271          here because we are modifying the in-memory contents.  */
2272       bfd_put_64 (sdlt->owner, value, sdlt->contents + dyn_h->dlt_offset);
2273     }
2274
2275   /* Create a relocation for the DLT entry associated with this symbol.
2276      When building a shared library the symbol does not have to be dynamic.  */
2277   if (dyn_h->want_dlt
2278       && (elf64_hppa_dynamic_symbol_p (dyn_h->h, info) || info->shared))
2279     {
2280       Elf_Internal_Rela rel;
2281       bfd_byte *loc;
2282       int dynindx;
2283
2284       /* We may need to do a relocation against a local symbol, in
2285          which case we have to look up it's dynamic symbol index off
2286          the local symbol hash table.  */
2287       if (h && h->dynindx != -1)
2288         dynindx = h->dynindx;
2289       else
2290         dynindx
2291           = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2292                                                 dyn_h->sym_indx);
2293
2294       /* Create a dynamic relocation for this entry.  Do include the output
2295          offset of the DLT entry since we need an absolute address in the
2296          resulting object file.  */
2297       rel.r_offset = (dyn_h->dlt_offset + sdlt->output_offset
2298                       + sdlt->output_section->vma);
2299       if (h && h->type == STT_FUNC)
2300           rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2301       else
2302           rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2303       rel.r_addend = 0;
2304
2305       loc = sdltrel->contents;
2306       loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2307       bfd_elf64_swap_reloca_out (sdlt->output_section->owner, &rel, loc);
2308     }
2309   return TRUE;
2310 }
2311
2312 /* Finalize the dynamic relocations.  Specifically the FPTR relocations
2313    for dynamic functions used to initialize static data.  */
2314
2315 static bfd_boolean
2316 elf64_hppa_finalize_dynreloc (dyn_h, data)
2317      struct elf64_hppa_dyn_hash_entry *dyn_h;
2318      PTR data;
2319 {
2320   struct bfd_link_info *info = (struct bfd_link_info *)data;
2321   struct elf64_hppa_link_hash_table *hppa_info;
2322   struct elf_link_hash_entry *h;
2323   int dynamic_symbol;
2324
2325   dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, info);
2326
2327   if (!dynamic_symbol && !info->shared)
2328     return TRUE;
2329
2330   if (dyn_h->reloc_entries)
2331     {
2332       struct elf64_hppa_dyn_reloc_entry *rent;
2333       int dynindx;
2334
2335       hppa_info = elf64_hppa_hash_table (info);
2336       h = dyn_h->h;
2337
2338       /* We may need to do a relocation against a local symbol, in
2339          which case we have to look up it's dynamic symbol index off
2340          the local symbol hash table.  */
2341       if (h && h->dynindx != -1)
2342         dynindx = h->dynindx;
2343       else
2344         dynindx
2345           = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2346                                                 dyn_h->sym_indx);
2347
2348       for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
2349         {
2350           Elf_Internal_Rela rel;
2351           bfd_byte *loc;
2352
2353           /* Allocate one iff we are building a shared library, the relocation
2354              isn't a R_PARISC_FPTR64, or we don't want an opd entry.  */
2355           if (!info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
2356             continue;
2357
2358           /* Create a dynamic relocation for this entry.
2359
2360              We need the output offset for the reloc's section because
2361              we are creating an absolute address in the resulting object
2362              file.  */
2363           rel.r_offset = (rent->offset + rent->sec->output_offset
2364                           + rent->sec->output_section->vma);
2365
2366           /* An FPTR64 relocation implies that we took the address of
2367              a function and that the function has an entry in the .opd
2368              section.  We want the FPTR64 relocation to reference the
2369              entry in .opd.
2370
2371              We could munge the symbol value in the dynamic symbol table
2372              (in fact we already do for functions with global scope) to point
2373              to the .opd entry.  Then we could use that dynamic symbol in
2374              this relocation.
2375
2376              Or we could do something sensible, not munge the symbol's
2377              address and instead just use a different symbol to reference
2378              the .opd entry.  At least that seems sensible until you
2379              realize there's no local dynamic symbols we can use for that
2380              purpose.  Thus the hair in the check_relocs routine.
2381
2382              We use a section symbol recorded by check_relocs as the
2383              base symbol for the relocation.  The addend is the difference
2384              between the section symbol and the address of the .opd entry.  */
2385           if (info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
2386             {
2387               bfd_vma value, value2;
2388
2389               /* First compute the address of the opd entry for this symbol.  */
2390               value = (dyn_h->opd_offset
2391                        + hppa_info->opd_sec->output_section->vma
2392                        + hppa_info->opd_sec->output_offset);
2393
2394               /* Compute the value of the start of the section with
2395                  the relocation.  */
2396               value2 = (rent->sec->output_section->vma
2397                         + rent->sec->output_offset);
2398
2399               /* Compute the difference between the start of the section
2400                  with the relocation and the opd entry.  */
2401               value -= value2;
2402
2403               /* The result becomes the addend of the relocation.  */
2404               rel.r_addend = value;
2405
2406               /* The section symbol becomes the symbol for the dynamic
2407                  relocation.  */
2408               dynindx
2409                 = _bfd_elf_link_lookup_local_dynindx (info,
2410                                                       rent->sec->owner,
2411                                                       rent->sec_symndx);
2412             }
2413           else
2414             rel.r_addend = rent->addend;
2415
2416           rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2417
2418           loc = hppa_info->other_rel_sec->contents;
2419           loc += (hppa_info->other_rel_sec->reloc_count++
2420                   * sizeof (Elf64_External_Rela));
2421           bfd_elf64_swap_reloca_out (hppa_info->other_rel_sec->output_section->owner,
2422                                      &rel, loc);
2423         }
2424     }
2425
2426   return TRUE;
2427 }
2428
2429 /* Used to decide how to sort relocs in an optimal manner for the
2430    dynamic linker, before writing them out.  */
2431
2432 static enum elf_reloc_type_class
2433 elf64_hppa_reloc_type_class (rela)
2434      const Elf_Internal_Rela *rela;
2435 {
2436   if (ELF64_R_SYM (rela->r_info) == 0)
2437     return reloc_class_relative;
2438
2439   switch ((int) ELF64_R_TYPE (rela->r_info))
2440     {
2441     case R_PARISC_IPLT:
2442       return reloc_class_plt;
2443     case R_PARISC_COPY:
2444       return reloc_class_copy;
2445     default:
2446       return reloc_class_normal;
2447     }
2448 }
2449
2450 /* Finish up the dynamic sections.  */
2451
2452 static bfd_boolean
2453 elf64_hppa_finish_dynamic_sections (output_bfd, info)
2454      bfd *output_bfd;
2455      struct bfd_link_info *info;
2456 {
2457   bfd *dynobj;
2458   asection *sdyn;
2459   struct elf64_hppa_link_hash_table *hppa_info;
2460
2461   hppa_info = elf64_hppa_hash_table (info);
2462
2463   /* Finalize the contents of the .opd section.  */
2464   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2465                                 elf64_hppa_finalize_opd,
2466                                 info);
2467
2468   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2469                                 elf64_hppa_finalize_dynreloc,
2470                                 info);
2471
2472   /* Finalize the contents of the .dlt section.  */
2473   dynobj = elf_hash_table (info)->dynobj;
2474   /* Finalize the contents of the .dlt section.  */
2475   elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2476                                 elf64_hppa_finalize_dlt,
2477                                 info);
2478
2479   sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2480
2481   if (elf_hash_table (info)->dynamic_sections_created)
2482     {
2483       Elf64_External_Dyn *dyncon, *dynconend;
2484
2485       BFD_ASSERT (sdyn != NULL);
2486
2487       dyncon = (Elf64_External_Dyn *) sdyn->contents;
2488       dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2489       for (; dyncon < dynconend; dyncon++)
2490         {
2491           Elf_Internal_Dyn dyn;
2492           asection *s;
2493
2494           bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2495
2496           switch (dyn.d_tag)
2497             {
2498             default:
2499               break;
2500
2501             case DT_HP_LOAD_MAP:
2502               /* Compute the absolute address of 16byte scratchpad area
2503                  for the dynamic linker.
2504
2505                  By convention the linker script will allocate the scratchpad
2506                  area at the start of the .data section.  So all we have to
2507                  to is find the start of the .data section.  */
2508               s = bfd_get_section_by_name (output_bfd, ".data");
2509               dyn.d_un.d_ptr = s->vma;
2510               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2511               break;
2512
2513             case DT_PLTGOT:
2514               /* HP's use PLTGOT to set the GOT register.  */
2515               dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2516               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2517               break;
2518
2519             case DT_JMPREL:
2520               s = hppa_info->plt_rel_sec;
2521               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2522               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2523               break;
2524
2525             case DT_PLTRELSZ:
2526               s = hppa_info->plt_rel_sec;
2527               dyn.d_un.d_val = s->size;
2528               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2529               break;
2530
2531             case DT_RELA:
2532               s = hppa_info->other_rel_sec;
2533               if (! s || ! s->size)
2534                 s = hppa_info->dlt_rel_sec;
2535               if (! s || ! s->size)
2536                 s = hppa_info->opd_rel_sec;
2537               dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2538               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2539               break;
2540
2541             case DT_RELASZ:
2542               s = hppa_info->other_rel_sec;
2543               dyn.d_un.d_val = s->size;
2544               s = hppa_info->dlt_rel_sec;
2545               dyn.d_un.d_val += s->size;
2546               s = hppa_info->opd_rel_sec;
2547               dyn.d_un.d_val += s->size;
2548               /* There is some question about whether or not the size of
2549                  the PLT relocs should be included here.  HP's tools do
2550                  it, so we'll emulate them.  */
2551               s = hppa_info->plt_rel_sec;
2552               dyn.d_un.d_val += s->size;
2553               bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2554               break;
2555
2556             }
2557         }
2558     }
2559
2560   return TRUE;
2561 }
2562
2563 /* Support for core dump NOTE sections.  */
2564
2565 static bfd_boolean
2566 elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2567 {
2568   int offset;
2569   size_t size;
2570
2571   switch (note->descsz)
2572     {
2573       default:
2574         return FALSE;
2575
2576       case 760:         /* Linux/hppa */
2577         /* pr_cursig */
2578         elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2579
2580         /* pr_pid */
2581         elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2582
2583         /* pr_reg */
2584         offset = 112;
2585         size = 640;
2586
2587         break;
2588     }
2589
2590   /* Make a ".reg/999" section.  */
2591   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2592                                           size, note->descpos + offset);
2593 }
2594
2595 static bfd_boolean
2596 elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2597 {
2598   char * command;
2599   int n;
2600
2601   switch (note->descsz)
2602     {
2603     default:
2604       return FALSE;
2605
2606     case 136:           /* Linux/hppa elf_prpsinfo.  */
2607       elf_tdata (abfd)->core_program
2608         = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2609       elf_tdata (abfd)->core_command
2610         = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2611     }
2612
2613   /* Note that for some reason, a spurious space is tacked
2614      onto the end of the args in some (at least one anyway)
2615      implementations, so strip it off if it exists.  */
2616   command = elf_tdata (abfd)->core_command;
2617   n = strlen (command);
2618
2619   if (0 < n && command[n - 1] == ' ')
2620     command[n - 1] = '\0';
2621
2622   return TRUE;
2623 }
2624
2625 /* Return the number of additional phdrs we will need.
2626
2627    The generic ELF code only creates PT_PHDRs for executables.  The HP
2628    dynamic linker requires PT_PHDRs for dynamic libraries too.
2629
2630    This routine indicates that the backend needs one additional program
2631    header for that case.
2632
2633    Note we do not have access to the link info structure here, so we have
2634    to guess whether or not we are building a shared library based on the
2635    existence of a .interp section.  */
2636
2637 static int
2638 elf64_hppa_additional_program_headers (bfd *abfd,
2639                                        struct bfd_link_info *info ATTRIBUTE_UNUSED)
2640 {
2641   asection *s;
2642
2643   /* If we are creating a shared library, then we have to create a
2644      PT_PHDR segment.  HP's dynamic linker chokes without it.  */
2645   s = bfd_get_section_by_name (abfd, ".interp");
2646   if (! s)
2647     return 1;
2648   return 0;
2649 }
2650
2651 /* Allocate and initialize any program headers required by this
2652    specific backend.
2653
2654    The generic ELF code only creates PT_PHDRs for executables.  The HP
2655    dynamic linker requires PT_PHDRs for dynamic libraries too.
2656
2657    This allocates the PT_PHDR and initializes it in a manner suitable
2658    for the HP linker.
2659
2660    Note we do not have access to the link info structure here, so we have
2661    to guess whether or not we are building a shared library based on the
2662    existence of a .interp section.  */
2663
2664 static bfd_boolean
2665 elf64_hppa_modify_segment_map (bfd *abfd,
2666                                struct bfd_link_info *info ATTRIBUTE_UNUSED)
2667 {
2668   struct elf_segment_map *m;
2669   asection *s;
2670
2671   s = bfd_get_section_by_name (abfd, ".interp");
2672   if (! s)
2673     {
2674       for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2675         if (m->p_type == PT_PHDR)
2676           break;
2677       if (m == NULL)
2678         {
2679           m = ((struct elf_segment_map *)
2680                bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
2681           if (m == NULL)
2682             return FALSE;
2683
2684           m->p_type = PT_PHDR;
2685           m->p_flags = PF_R | PF_X;
2686           m->p_flags_valid = 1;
2687           m->p_paddr_valid = 1;
2688           m->includes_phdrs = 1;
2689
2690           m->next = elf_tdata (abfd)->segment_map;
2691           elf_tdata (abfd)->segment_map = m;
2692         }
2693     }
2694
2695   for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2696     if (m->p_type == PT_LOAD)
2697       {
2698         unsigned int i;
2699
2700         for (i = 0; i < m->count; i++)
2701           {
2702             /* The code "hint" is not really a hint.  It is a requirement
2703                for certain versions of the HP dynamic linker.  Worse yet,
2704                it must be set even if the shared library does not have
2705                any code in its "text" segment (thus the check for .hash
2706                to catch this situation).  */
2707             if (m->sections[i]->flags & SEC_CODE
2708                 || (strcmp (m->sections[i]->name, ".hash") == 0))
2709               m->p_flags |= (PF_X | PF_HP_CODE);
2710           }
2711       }
2712
2713   return TRUE;
2714 }
2715
2716 /* Called when writing out an object file to decide the type of a
2717    symbol.  */
2718 static int
2719 elf64_hppa_elf_get_symbol_type (elf_sym, type)
2720      Elf_Internal_Sym *elf_sym;
2721      int type;
2722 {
2723   if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2724     return STT_PARISC_MILLI;
2725   else
2726     return type;
2727 }
2728
2729 /* Support HP specific sections for core files.  */
2730 static bfd_boolean
2731 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index,
2732                               const char *typename)
2733 {
2734   if (hdr->p_type == PT_HP_CORE_KERNEL)
2735     {
2736       asection *sect;
2737
2738       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
2739         return FALSE;
2740
2741       sect = bfd_make_section_anyway (abfd, ".kernel");
2742       if (sect == NULL)
2743         return FALSE;
2744       sect->size = hdr->p_filesz;
2745       sect->filepos = hdr->p_offset;
2746       sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2747       return TRUE;
2748     }
2749
2750   if (hdr->p_type == PT_HP_CORE_PROC)
2751     {
2752       int sig;
2753
2754       if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2755         return FALSE;
2756       if (bfd_bread (&sig, 4, abfd) != 4)
2757         return FALSE;
2758
2759       elf_tdata (abfd)->core_signal = sig;
2760
2761       if (!_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename))
2762         return FALSE;
2763
2764       /* GDB uses the ".reg" section to read register contents.  */
2765       return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2766                                               hdr->p_offset);
2767     }
2768
2769   if (hdr->p_type == PT_HP_CORE_LOADABLE
2770       || hdr->p_type == PT_HP_CORE_STACK
2771       || hdr->p_type == PT_HP_CORE_MMF)
2772     hdr->p_type = PT_LOAD;
2773
2774   return _bfd_elf_make_section_from_phdr (abfd, hdr, index, typename);
2775 }
2776
2777 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
2778 {
2779   { STRING_COMMA_LEN (".fini"),  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2780   { STRING_COMMA_LEN (".init"),  0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2781   { STRING_COMMA_LEN (".plt"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2782   { STRING_COMMA_LEN (".dlt"),   0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2783   { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2784   { STRING_COMMA_LEN (".sbss"),  0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2785   { STRING_COMMA_LEN (".tbss"),  0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
2786   { NULL,                    0,  0, 0,            0 }
2787 };
2788
2789 /* The hash bucket size is the standard one, namely 4.  */
2790
2791 const struct elf_size_info hppa64_elf_size_info =
2792 {
2793   sizeof (Elf64_External_Ehdr),
2794   sizeof (Elf64_External_Phdr),
2795   sizeof (Elf64_External_Shdr),
2796   sizeof (Elf64_External_Rel),
2797   sizeof (Elf64_External_Rela),
2798   sizeof (Elf64_External_Sym),
2799   sizeof (Elf64_External_Dyn),
2800   sizeof (Elf_External_Note),
2801   4,
2802   1,
2803   64, 3,
2804   ELFCLASS64, EV_CURRENT,
2805   bfd_elf64_write_out_phdrs,
2806   bfd_elf64_write_shdrs_and_ehdr,
2807   bfd_elf64_checksum_contents,
2808   bfd_elf64_write_relocs,
2809   bfd_elf64_swap_symbol_in,
2810   bfd_elf64_swap_symbol_out,
2811   bfd_elf64_slurp_reloc_table,
2812   bfd_elf64_slurp_symbol_table,
2813   bfd_elf64_swap_dyn_in,
2814   bfd_elf64_swap_dyn_out,
2815   bfd_elf64_swap_reloc_in,
2816   bfd_elf64_swap_reloc_out,
2817   bfd_elf64_swap_reloca_in,
2818   bfd_elf64_swap_reloca_out
2819 };
2820
2821 #define TARGET_BIG_SYM                  bfd_elf64_hppa_vec
2822 #define TARGET_BIG_NAME                 "elf64-hppa"
2823 #define ELF_ARCH                        bfd_arch_hppa
2824 #define ELF_MACHINE_CODE                EM_PARISC
2825 /* This is not strictly correct.  The maximum page size for PA2.0 is
2826    64M.  But everything still uses 4k.  */
2827 #define ELF_MAXPAGESIZE                 0x1000
2828 #define ELF_OSABI                       ELFOSABI_HPUX
2829
2830 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
2831 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
2832 #define bfd_elf64_bfd_is_local_label_name       elf_hppa_is_local_label_name
2833 #define elf_info_to_howto               elf_hppa_info_to_howto
2834 #define elf_info_to_howto_rel           elf_hppa_info_to_howto_rel
2835
2836 #define elf_backend_section_from_shdr   elf64_hppa_section_from_shdr
2837 #define elf_backend_object_p            elf64_hppa_object_p
2838 #define elf_backend_final_write_processing \
2839                                         elf_hppa_final_write_processing
2840 #define elf_backend_fake_sections       elf_hppa_fake_sections
2841 #define elf_backend_add_symbol_hook     elf_hppa_add_symbol_hook
2842
2843 #define elf_backend_relocate_section    elf_hppa_relocate_section
2844
2845 #define bfd_elf64_bfd_final_link        elf_hppa_final_link
2846
2847 #define elf_backend_create_dynamic_sections \
2848                                         elf64_hppa_create_dynamic_sections
2849 #define elf_backend_post_process_headers        elf64_hppa_post_process_headers
2850
2851 #define elf_backend_omit_section_dynsym \
2852   ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
2853 #define elf_backend_adjust_dynamic_symbol \
2854                                         elf64_hppa_adjust_dynamic_symbol
2855
2856 #define elf_backend_size_dynamic_sections \
2857                                         elf64_hppa_size_dynamic_sections
2858
2859 #define elf_backend_finish_dynamic_symbol \
2860                                         elf64_hppa_finish_dynamic_symbol
2861 #define elf_backend_finish_dynamic_sections \
2862                                         elf64_hppa_finish_dynamic_sections
2863 #define elf_backend_grok_prstatus       elf64_hppa_grok_prstatus
2864 #define elf_backend_grok_psinfo         elf64_hppa_grok_psinfo
2865  
2866 /* Stuff for the BFD linker: */
2867 #define bfd_elf64_bfd_link_hash_table_create \
2868         elf64_hppa_hash_table_create
2869
2870 #define elf_backend_check_relocs \
2871         elf64_hppa_check_relocs
2872
2873 #define elf_backend_size_info \
2874   hppa64_elf_size_info
2875
2876 #define elf_backend_additional_program_headers \
2877         elf64_hppa_additional_program_headers
2878
2879 #define elf_backend_modify_segment_map \
2880         elf64_hppa_modify_segment_map
2881
2882 #define elf_backend_link_output_symbol_hook \
2883         elf64_hppa_link_output_symbol_hook
2884
2885 #define elf_backend_want_got_plt        0
2886 #define elf_backend_plt_readonly        0
2887 #define elf_backend_want_plt_sym        0
2888 #define elf_backend_got_header_size     0
2889 #define elf_backend_type_change_ok      TRUE
2890 #define elf_backend_get_symbol_type     elf64_hppa_elf_get_symbol_type
2891 #define elf_backend_reloc_type_class    elf64_hppa_reloc_type_class
2892 #define elf_backend_rela_normal         1
2893 #define elf_backend_special_sections    elf64_hppa_special_sections
2894 #define elf_backend_action_discarded    elf_hppa_action_discarded
2895 #define elf_backend_section_from_phdr   elf64_hppa_section_from_phdr
2896
2897 #define elf64_bed                       elf64_hppa_hpux_bed
2898
2899 #include "elf64-target.h"
2900
2901 #undef TARGET_BIG_SYM
2902 #define TARGET_BIG_SYM                  bfd_elf64_hppa_linux_vec
2903 #undef TARGET_BIG_NAME
2904 #define TARGET_BIG_NAME                 "elf64-hppa-linux"
2905 #undef ELF_OSABI
2906 #define ELF_OSABI                       ELFOSABI_LINUX
2907 #undef elf_backend_post_process_headers
2908 #define elf_backend_post_process_headers _bfd_elf_set_osabi
2909 #undef elf64_bed
2910 #define elf64_bed                       elf64_hppa_linux_bed
2911
2912 #include "elf64-target.h"