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