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