Automatic date update in version.in
[binutils-gdb.git] / bfd / elf64-hppa.c
blob030e6e3a189aa711c6eeba8d1459b2ab00bdab16
1 /* Support for HPPA 64-bit ELF
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/hppa.h"
26 #include "libhppa.h"
27 #include "elf64-hppa.h"
28 #include "libiberty.h"
30 #define ARCH_SIZE 64
32 #define PLT_ENTRY_SIZE 0x10
33 #define DLT_ENTRY_SIZE 0x8
34 #define OPD_ENTRY_SIZE 0x20
36 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
38 /* The stub is supposed to load the target address and target's DP
39 value out of the PLT, then do an external branch to the target
40 address.
42 LDD PLTOFF(%r27),%r1
43 BVE (%r1)
44 LDD PLTOFF+8(%r27),%r27
46 Note that we must use the LDD with a 14 bit displacement, not the one
47 with a 5 bit displacement. */
48 static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
49 0x53, 0x7b, 0x00, 0x00 };
51 struct elf64_hppa_link_hash_entry
53 struct elf_link_hash_entry eh;
55 /* Offsets for this symbol in various linker sections. */
56 bfd_vma dlt_offset;
57 bfd_vma plt_offset;
58 bfd_vma opd_offset;
59 bfd_vma stub_offset;
61 /* The index of the (possibly local) symbol in the input bfd and its
62 associated BFD. Needed so that we can have relocs against local
63 symbols in shared libraries. */
64 long sym_indx;
65 bfd *owner;
67 /* Dynamic symbols may need to have two different values. One for
68 the dynamic symbol table, one for the normal symbol table.
70 In such cases we store the symbol's real value and section
71 index here so we can restore the real value before we write
72 the normal symbol table. */
73 bfd_vma st_value;
74 int st_shndx;
76 /* Used to count non-got, non-plt relocations for delayed sizing
77 of relocation sections. */
78 struct elf64_hppa_dyn_reloc_entry
80 /* Next relocation in the chain. */
81 struct elf64_hppa_dyn_reloc_entry *next;
83 /* The type of the relocation. */
84 int type;
86 /* The input section of the relocation. */
87 asection *sec;
89 /* Number of relocs copied in this section. */
90 bfd_size_type count;
92 /* The index of the section symbol for the input section of
93 the relocation. Only needed when building shared libraries. */
94 int sec_symndx;
96 /* The offset within the input section of the relocation. */
97 bfd_vma offset;
99 /* The addend for the relocation. */
100 bfd_vma addend;
102 } *reloc_entries;
104 /* Nonzero if this symbol needs an entry in one of the linker
105 sections. */
106 unsigned want_dlt;
107 unsigned want_plt;
108 unsigned want_opd;
109 unsigned want_stub;
112 struct elf64_hppa_link_hash_table
114 struct elf_link_hash_table root;
116 /* Shortcuts to get to the various linker defined sections. */
117 asection *dlt_sec;
118 asection *dlt_rel_sec;
119 asection *opd_sec;
120 asection *opd_rel_sec;
121 asection *other_rel_sec;
123 /* Offset of __gp within .plt section. When the PLT gets large we want
124 to slide __gp into the PLT section so that we can continue to use
125 single DP relative instructions to load values out of the PLT. */
126 bfd_vma gp_offset;
128 /* Note this is not strictly correct. We should create a stub section for
129 each input section with calls. The stub section should be placed before
130 the section with the call. */
131 asection *stub_sec;
133 bfd_vma text_segment_base;
134 bfd_vma data_segment_base;
136 /* We build tables to map from an input section back to its
137 symbol index. This is the BFD for which we currently have
138 a map. */
139 bfd *section_syms_bfd;
141 /* Array of symbol numbers for each input section attached to the
142 current BFD. */
143 int *section_syms;
146 #define hppa_link_hash_table(p) \
147 ((is_elf_hash_table ((p)->hash) \
148 && elf_hash_table_id (elf_hash_table (p)) == HPPA64_ELF_DATA) \
149 ? (struct elf64_hppa_link_hash_table *) (p)->hash : NULL)
151 #define hppa_elf_hash_entry(ent) \
152 ((struct elf64_hppa_link_hash_entry *)(ent))
154 #define eh_name(eh) \
155 (eh ? eh->root.root.string : "<undef>")
157 typedef struct bfd_hash_entry *(*new_hash_entry_func)
158 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
160 static struct bfd_link_hash_table *elf64_hppa_hash_table_create
161 (bfd *abfd);
163 /* This must follow the definitions of the various derived linker
164 hash tables and shared functions. */
165 #include "elf-hppa.h"
167 static bool elf64_hppa_object_p
168 (bfd *);
170 static bool elf64_hppa_create_dynamic_sections
171 (bfd *, struct bfd_link_info *);
173 static bool elf64_hppa_adjust_dynamic_symbol
174 (struct bfd_link_info *, struct elf_link_hash_entry *);
176 static bool elf64_hppa_mark_milli_and_exported_functions
177 (struct elf_link_hash_entry *, void *);
179 static bool elf64_hppa_size_dynamic_sections
180 (bfd *, struct bfd_link_info *);
182 static int elf64_hppa_link_output_symbol_hook
183 (struct bfd_link_info *, const char *, Elf_Internal_Sym *,
184 asection *, struct elf_link_hash_entry *);
186 static bool elf64_hppa_finish_dynamic_symbol
187 (bfd *, struct bfd_link_info *,
188 struct elf_link_hash_entry *, Elf_Internal_Sym *);
190 static bool elf64_hppa_finish_dynamic_sections
191 (bfd *, struct bfd_link_info *);
193 static bool elf64_hppa_check_relocs
194 (bfd *, struct bfd_link_info *,
195 asection *, const Elf_Internal_Rela *);
197 static bool elf64_hppa_dynamic_symbol_p
198 (struct elf_link_hash_entry *, struct bfd_link_info *);
200 static bool elf64_hppa_mark_exported_functions
201 (struct elf_link_hash_entry *, void *);
203 static bool elf64_hppa_finalize_opd
204 (struct elf_link_hash_entry *, void *);
206 static bool elf64_hppa_finalize_dlt
207 (struct elf_link_hash_entry *, void *);
209 static bool allocate_global_data_dlt
210 (struct elf_link_hash_entry *, void *);
212 static bool allocate_global_data_plt
213 (struct elf_link_hash_entry *, void *);
215 static bool allocate_global_data_stub
216 (struct elf_link_hash_entry *, void *);
218 static bool allocate_global_data_opd
219 (struct elf_link_hash_entry *, void *);
221 static bool get_reloc_section
222 (bfd *, struct elf64_hppa_link_hash_table *, asection *);
224 static bool count_dyn_reloc
225 (bfd *, struct elf64_hppa_link_hash_entry *,
226 int, asection *, int, bfd_vma, bfd_vma);
228 static bool allocate_dynrel_entries
229 (struct elf_link_hash_entry *, void *);
231 static bool elf64_hppa_finalize_dynreloc
232 (struct elf_link_hash_entry *, void *);
234 static bool get_opd
235 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
237 static bool get_plt
238 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
240 static bool get_dlt
241 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
243 static bool get_stub
244 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
246 static int elf64_hppa_elf_get_symbol_type
247 (Elf_Internal_Sym *, int);
249 /* Initialize an entry in the link hash table. */
251 static struct bfd_hash_entry *
252 hppa64_link_hash_newfunc (struct bfd_hash_entry *entry,
253 struct bfd_hash_table *table,
254 const char *string)
256 /* Allocate the structure if it has not already been allocated by a
257 subclass. */
258 if (entry == NULL)
260 entry = bfd_hash_allocate (table,
261 sizeof (struct elf64_hppa_link_hash_entry));
262 if (entry == NULL)
263 return entry;
266 /* Call the allocation method of the superclass. */
267 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
268 if (entry != NULL)
270 struct elf64_hppa_link_hash_entry *hh;
272 /* Initialize our local data. All zeros. */
273 hh = hppa_elf_hash_entry (entry);
274 memset (&hh->dlt_offset, 0,
275 (sizeof (struct elf64_hppa_link_hash_entry)
276 - offsetof (struct elf64_hppa_link_hash_entry, dlt_offset)));
279 return entry;
282 /* Create the derived linker hash table. The PA64 ELF port uses this
283 derived hash table to keep information specific to the PA ElF
284 linker (without using static variables). */
286 static struct bfd_link_hash_table*
287 elf64_hppa_hash_table_create (bfd *abfd)
289 struct elf64_hppa_link_hash_table *htab;
290 size_t amt = sizeof (*htab);
292 htab = bfd_zmalloc (amt);
293 if (htab == NULL)
294 return NULL;
296 if (!_bfd_elf_link_hash_table_init (&htab->root, abfd,
297 hppa64_link_hash_newfunc,
298 sizeof (struct elf64_hppa_link_hash_entry),
299 HPPA64_ELF_DATA))
301 free (htab);
302 return NULL;
305 htab->root.dt_pltgot_required = true;
306 htab->text_segment_base = (bfd_vma) -1;
307 htab->data_segment_base = (bfd_vma) -1;
309 return &htab->root.root;
312 /* Return nonzero if ABFD represents a PA2.0 ELF64 file.
314 Additionally we set the default architecture and machine. */
315 static bool
316 elf64_hppa_object_p (bfd *abfd)
318 Elf_Internal_Ehdr * i_ehdrp;
319 unsigned int flags;
321 i_ehdrp = elf_elfheader (abfd);
322 if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
324 /* GCC on hppa-linux produces binaries with OSABI=GNU,
325 but the kernel produces corefiles with OSABI=SysV. */
326 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
327 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
328 return false;
330 else
332 /* HPUX produces binaries with OSABI=HPUX,
333 but the kernel produces corefiles with OSABI=SysV. */
334 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
335 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
336 return false;
339 flags = i_ehdrp->e_flags;
340 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
342 case EFA_PARISC_1_0:
343 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
344 case EFA_PARISC_1_1:
345 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
346 case EFA_PARISC_2_0:
347 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
348 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
349 else
350 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
351 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
352 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
354 /* Don't be fussy. */
355 return true;
358 /* Given section type (hdr->sh_type), return a boolean indicating
359 whether or not the section is an elf64-hppa specific section. */
360 static bool
361 elf64_hppa_section_from_shdr (bfd *abfd,
362 Elf_Internal_Shdr *hdr,
363 const char *name,
364 int shindex)
366 switch (hdr->sh_type)
368 case SHT_PARISC_EXT:
369 if (strcmp (name, ".PARISC.archext") != 0)
370 return false;
371 break;
372 case SHT_PARISC_UNWIND:
373 if (strcmp (name, ".PARISC.unwind") != 0)
374 return false;
375 break;
376 case SHT_PARISC_DOC:
377 case SHT_PARISC_ANNOT:
378 default:
379 return false;
382 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
383 return false;
385 return ((hdr->sh_flags & SHF_PARISC_SHORT) == 0
386 || bfd_set_section_flags (hdr->bfd_section,
387 hdr->bfd_section->flags | SEC_SMALL_DATA));
390 /* SEC is a section containing relocs for an input BFD when linking; return
391 a suitable section for holding relocs in the output BFD for a link. */
393 static bool
394 get_reloc_section (bfd *abfd,
395 struct elf64_hppa_link_hash_table *hppa_info,
396 asection *sec)
398 const char *srel_name;
399 asection *srel;
400 bfd *dynobj;
402 srel_name = (bfd_elf_string_from_elf_section
403 (abfd, elf_elfheader(abfd)->e_shstrndx,
404 _bfd_elf_single_rel_hdr(sec)->sh_name));
405 if (srel_name == NULL)
406 return false;
408 dynobj = hppa_info->root.dynobj;
409 if (!dynobj)
410 hppa_info->root.dynobj = dynobj = abfd;
412 srel = bfd_get_linker_section (dynobj, srel_name);
413 if (srel == NULL)
415 srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
416 (SEC_ALLOC
417 | SEC_LOAD
418 | SEC_HAS_CONTENTS
419 | SEC_IN_MEMORY
420 | SEC_LINKER_CREATED
421 | SEC_READONLY));
422 if (srel == NULL
423 || !bfd_set_section_alignment (srel, 3))
424 return false;
427 hppa_info->other_rel_sec = srel;
428 return true;
431 /* Add a new entry to the list of dynamic relocations against DYN_H.
433 We use this to keep a record of all the FPTR relocations against a
434 particular symbol so that we can create FPTR relocations in the
435 output file. */
437 static bool
438 count_dyn_reloc (bfd *abfd,
439 struct elf64_hppa_link_hash_entry *hh,
440 int type,
441 asection *sec,
442 int sec_symndx,
443 bfd_vma offset,
444 bfd_vma addend)
446 struct elf64_hppa_dyn_reloc_entry *rent;
448 rent = (struct elf64_hppa_dyn_reloc_entry *)
449 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
450 if (!rent)
451 return false;
453 rent->next = hh->reloc_entries;
454 rent->type = type;
455 rent->sec = sec;
456 rent->sec_symndx = sec_symndx;
457 rent->offset = offset;
458 rent->addend = addend;
459 hh->reloc_entries = rent;
461 return true;
464 /* Return a pointer to the local DLT, PLT and OPD reference counts
465 for ABFD. Returns NULL if the storage allocation fails. */
467 static bfd_signed_vma *
468 hppa64_elf_local_refcounts (bfd *abfd)
470 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
471 bfd_signed_vma *local_refcounts;
473 local_refcounts = elf_local_got_refcounts (abfd);
474 if (local_refcounts == NULL)
476 bfd_size_type size;
478 /* Allocate space for local DLT, PLT and OPD reference
479 counts. Done this way to save polluting elf_obj_tdata
480 with another target specific pointer. */
481 size = symtab_hdr->sh_info;
482 size *= 3 * sizeof (bfd_signed_vma);
483 local_refcounts = bfd_zalloc (abfd, size);
484 elf_local_got_refcounts (abfd) = local_refcounts;
486 return local_refcounts;
489 /* Scan the RELOCS and record the type of dynamic entries that each
490 referenced symbol needs. */
492 static bool
493 elf64_hppa_check_relocs (bfd *abfd,
494 struct bfd_link_info *info,
495 asection *sec,
496 const Elf_Internal_Rela *relocs)
498 struct elf64_hppa_link_hash_table *hppa_info;
499 const Elf_Internal_Rela *relend;
500 Elf_Internal_Shdr *symtab_hdr;
501 const Elf_Internal_Rela *rel;
502 unsigned int sec_symndx;
504 if (bfd_link_relocatable (info))
505 return true;
507 /* If this is the first dynamic object found in the link, create
508 the special sections required for dynamic linking. */
509 if (! elf_hash_table (info)->dynamic_sections_created)
511 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
512 return false;
515 hppa_info = hppa_link_hash_table (info);
516 if (hppa_info == NULL)
517 return false;
518 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
520 /* If necessary, build a new table holding section symbols indices
521 for this BFD. */
523 if (bfd_link_pic (info) && hppa_info->section_syms_bfd != abfd)
525 unsigned long i;
526 unsigned int highest_shndx;
527 Elf_Internal_Sym *local_syms = NULL;
528 Elf_Internal_Sym *isym, *isymend;
529 bfd_size_type amt;
531 /* We're done with the old cache of section index to section symbol
532 index information. Free it.
534 ?!? Note we leak the last section_syms array. Presumably we
535 could free it in one of the later routines in this file. */
536 free (hppa_info->section_syms);
538 /* Read this BFD's local symbols. */
539 if (symtab_hdr->sh_info != 0)
541 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
542 if (local_syms == NULL)
543 local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
544 symtab_hdr->sh_info, 0,
545 NULL, NULL, NULL);
546 if (local_syms == NULL)
547 return false;
550 /* Record the highest section index referenced by the local symbols. */
551 highest_shndx = 0;
552 isymend = local_syms + symtab_hdr->sh_info;
553 for (isym = local_syms; isym < isymend; isym++)
555 if (isym->st_shndx > highest_shndx
556 && isym->st_shndx < SHN_LORESERVE)
557 highest_shndx = isym->st_shndx;
560 /* Allocate an array to hold the section index to section symbol index
561 mapping. Bump by one since we start counting at zero. */
562 highest_shndx++;
563 amt = highest_shndx;
564 amt *= sizeof (int);
565 hppa_info->section_syms = (int *) bfd_malloc (amt);
567 /* Now walk the local symbols again. If we find a section symbol,
568 record the index of the symbol into the section_syms array. */
569 for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
571 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
572 hppa_info->section_syms[isym->st_shndx] = i;
575 /* We are finished with the local symbols. */
576 if (local_syms != NULL
577 && symtab_hdr->contents != (unsigned char *) local_syms)
579 if (! info->keep_memory)
580 free (local_syms);
581 else
583 /* Cache the symbols for elf_link_input_bfd. */
584 symtab_hdr->contents = (unsigned char *) local_syms;
588 /* Record which BFD we built the section_syms mapping for. */
589 hppa_info->section_syms_bfd = abfd;
592 /* Record the symbol index for this input section. We may need it for
593 relocations when building shared libraries. When not building shared
594 libraries this value is never really used, but assign it to zero to
595 prevent out of bounds memory accesses in other routines. */
596 if (bfd_link_pic (info))
598 sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
600 /* If we did not find a section symbol for this section, then
601 something went terribly wrong above. */
602 if (sec_symndx == SHN_BAD)
603 return false;
605 if (sec_symndx < SHN_LORESERVE)
606 sec_symndx = hppa_info->section_syms[sec_symndx];
607 else
608 sec_symndx = 0;
610 else
611 sec_symndx = 0;
613 relend = relocs + sec->reloc_count;
614 for (rel = relocs; rel < relend; ++rel)
616 enum
618 NEED_DLT = 1,
619 NEED_PLT = 2,
620 NEED_STUB = 4,
621 NEED_OPD = 8,
622 NEED_DYNREL = 16,
625 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
626 struct elf64_hppa_link_hash_entry *hh;
627 int need_entry;
628 bool maybe_dynamic;
629 int dynrel_type = R_PARISC_NONE;
630 static reloc_howto_type *howto;
632 if (r_symndx >= symtab_hdr->sh_info)
634 /* We're dealing with a global symbol -- find its hash entry
635 and mark it as being referenced. */
636 long indx = r_symndx - symtab_hdr->sh_info;
637 hh = hppa_elf_hash_entry (elf_sym_hashes (abfd)[indx]);
638 while (hh->eh.root.type == bfd_link_hash_indirect
639 || hh->eh.root.type == bfd_link_hash_warning)
640 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
642 /* PR15323, ref flags aren't set for references in the same
643 object. */
644 hh->eh.ref_regular = 1;
646 else
647 hh = NULL;
649 /* We can only get preliminary data on whether a symbol is
650 locally or externally defined, as not all of the input files
651 have yet been processed. Do something with what we know, as
652 this may help reduce memory usage and processing time later. */
653 maybe_dynamic = false;
654 if (hh && ((bfd_link_pic (info)
655 && (!info->symbolic
656 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
657 || !hh->eh.def_regular
658 || hh->eh.root.type == bfd_link_hash_defweak))
659 maybe_dynamic = true;
661 howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
662 need_entry = 0;
663 switch (howto->type)
665 /* These are simple indirect references to symbols through the
666 DLT. We need to create a DLT entry for any symbols which
667 appears in a DLTIND relocation. */
668 case R_PARISC_DLTIND21L:
669 case R_PARISC_DLTIND14R:
670 case R_PARISC_DLTIND14F:
671 case R_PARISC_DLTIND14WR:
672 case R_PARISC_DLTIND14DR:
673 need_entry = NEED_DLT;
674 break;
676 /* ?!? These need a DLT entry. But I have no idea what to do with
677 the "link time TP value. */
678 case R_PARISC_LTOFF_TP21L:
679 case R_PARISC_LTOFF_TP14R:
680 case R_PARISC_LTOFF_TP14F:
681 case R_PARISC_LTOFF_TP64:
682 case R_PARISC_LTOFF_TP14WR:
683 case R_PARISC_LTOFF_TP14DR:
684 case R_PARISC_LTOFF_TP16F:
685 case R_PARISC_LTOFF_TP16WF:
686 case R_PARISC_LTOFF_TP16DF:
687 need_entry = NEED_DLT;
688 break;
690 /* These are function calls. Depending on their precise target we
691 may need to make a stub for them. The stub uses the PLT, so we
692 need to create PLT entries for these symbols too. */
693 case R_PARISC_PCREL12F:
694 case R_PARISC_PCREL17F:
695 case R_PARISC_PCREL22F:
696 case R_PARISC_PCREL32:
697 case R_PARISC_PCREL64:
698 case R_PARISC_PCREL21L:
699 case R_PARISC_PCREL17R:
700 case R_PARISC_PCREL17C:
701 case R_PARISC_PCREL14R:
702 case R_PARISC_PCREL14F:
703 case R_PARISC_PCREL22C:
704 case R_PARISC_PCREL14WR:
705 case R_PARISC_PCREL14DR:
706 case R_PARISC_PCREL16F:
707 case R_PARISC_PCREL16WF:
708 case R_PARISC_PCREL16DF:
709 /* Function calls might need to go through the .plt, and
710 might need a long branch stub. */
711 if (hh != NULL && hh->eh.type != STT_PARISC_MILLI)
712 need_entry = (NEED_PLT | NEED_STUB);
713 else
714 need_entry = 0;
715 break;
717 case R_PARISC_PLTOFF21L:
718 case R_PARISC_PLTOFF14R:
719 case R_PARISC_PLTOFF14F:
720 case R_PARISC_PLTOFF14WR:
721 case R_PARISC_PLTOFF14DR:
722 case R_PARISC_PLTOFF16F:
723 case R_PARISC_PLTOFF16WF:
724 case R_PARISC_PLTOFF16DF:
725 need_entry = (NEED_PLT);
726 break;
728 case R_PARISC_DIR64:
729 if (bfd_link_pic (info) || maybe_dynamic)
730 need_entry = (NEED_DYNREL);
731 dynrel_type = R_PARISC_DIR64;
732 break;
734 /* This is an indirect reference through the DLT to get the address
735 of a OPD descriptor. Thus we need to make a DLT entry that points
736 to an OPD entry. */
737 case R_PARISC_LTOFF_FPTR21L:
738 case R_PARISC_LTOFF_FPTR14R:
739 case R_PARISC_LTOFF_FPTR14WR:
740 case R_PARISC_LTOFF_FPTR14DR:
741 case R_PARISC_LTOFF_FPTR32:
742 case R_PARISC_LTOFF_FPTR64:
743 case R_PARISC_LTOFF_FPTR16F:
744 case R_PARISC_LTOFF_FPTR16WF:
745 case R_PARISC_LTOFF_FPTR16DF:
746 if (bfd_link_pic (info) || maybe_dynamic)
747 need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
748 else
749 need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
750 dynrel_type = R_PARISC_FPTR64;
751 break;
753 /* This is a simple OPD entry. */
754 case R_PARISC_FPTR64:
755 if (bfd_link_pic (info) || maybe_dynamic)
756 need_entry = (NEED_OPD | NEED_PLT | NEED_DYNREL);
757 else
758 need_entry = (NEED_OPD | NEED_PLT);
759 dynrel_type = R_PARISC_FPTR64;
760 break;
762 /* Add more cases as needed. */
765 if (!need_entry)
766 continue;
768 if (hh)
770 /* Stash away enough information to be able to find this symbol
771 regardless of whether or not it is local or global. */
772 hh->owner = abfd;
773 hh->sym_indx = r_symndx;
776 /* Create what's needed. */
777 if (need_entry & NEED_DLT)
779 /* Allocate space for a DLT entry, as well as a dynamic
780 relocation for this entry. */
781 if (! hppa_info->dlt_sec
782 && ! get_dlt (abfd, info, hppa_info))
783 goto err_out;
785 if (hh != NULL)
787 hh->want_dlt = 1;
788 hh->eh.got.refcount += 1;
790 else
792 bfd_signed_vma *local_dlt_refcounts;
794 /* This is a DLT entry for a local symbol. */
795 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
796 if (local_dlt_refcounts == NULL)
797 return false;
798 local_dlt_refcounts[r_symndx] += 1;
802 if (need_entry & NEED_PLT)
804 if (! hppa_info->root.splt
805 && ! get_plt (abfd, info, hppa_info))
806 goto err_out;
808 if (hh != NULL)
810 hh->want_plt = 1;
811 hh->eh.needs_plt = 1;
812 hh->eh.plt.refcount += 1;
814 else
816 bfd_signed_vma *local_dlt_refcounts;
817 bfd_signed_vma *local_plt_refcounts;
819 /* This is a PLT entry for a local symbol. */
820 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
821 if (local_dlt_refcounts == NULL)
822 return false;
823 local_plt_refcounts = local_dlt_refcounts + symtab_hdr->sh_info;
824 local_plt_refcounts[r_symndx] += 1;
828 if (need_entry & NEED_STUB)
830 if (! hppa_info->stub_sec
831 && ! get_stub (abfd, info, hppa_info))
832 goto err_out;
833 if (hh)
834 hh->want_stub = 1;
837 if (need_entry & NEED_OPD)
839 if (! hppa_info->opd_sec
840 && ! get_opd (abfd, info, hppa_info))
841 goto err_out;
843 /* FPTRs are not allocated by the dynamic linker for PA64,
844 though it is possible that will change in the future. */
846 if (hh != NULL)
847 hh->want_opd = 1;
848 else
850 bfd_signed_vma *local_dlt_refcounts;
851 bfd_signed_vma *local_opd_refcounts;
853 /* This is a OPD for a local symbol. */
854 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
855 if (local_dlt_refcounts == NULL)
856 return false;
857 local_opd_refcounts = (local_dlt_refcounts
858 + 2 * symtab_hdr->sh_info);
859 local_opd_refcounts[r_symndx] += 1;
863 /* Add a new dynamic relocation to the chain of dynamic
864 relocations for this symbol. */
865 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
867 if (! hppa_info->other_rel_sec
868 && ! get_reloc_section (abfd, hppa_info, sec))
869 goto err_out;
871 /* Count dynamic relocations against global symbols. */
872 if (hh != NULL
873 && !count_dyn_reloc (abfd, hh, dynrel_type, sec,
874 sec_symndx, rel->r_offset, rel->r_addend))
875 goto err_out;
877 /* If we are building a shared library and we just recorded
878 a dynamic R_PARISC_FPTR64 relocation, then make sure the
879 section symbol for this section ends up in the dynamic
880 symbol table. */
881 if (bfd_link_pic (info) && dynrel_type == R_PARISC_FPTR64
882 && ! (bfd_elf_link_record_local_dynamic_symbol
883 (info, abfd, sec_symndx)))
884 return false;
888 return true;
890 err_out:
891 return false;
894 struct elf64_hppa_allocate_data
896 struct bfd_link_info *info;
897 bfd_size_type ofs;
900 /* Should we do dynamic things to this symbol? */
902 static bool
903 elf64_hppa_dynamic_symbol_p (struct elf_link_hash_entry *eh,
904 struct bfd_link_info *info)
906 /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
907 and relocations that retrieve a function descriptor? Assume the
908 worst for now. */
909 if (_bfd_elf_dynamic_symbol_p (eh, info, 1))
911 /* ??? Why is this here and not elsewhere is_local_label_name. */
912 if (eh->root.root.string[0] == '$' && eh->root.root.string[1] == '$')
913 return false;
915 return true;
917 else
918 return false;
921 /* Mark all functions exported by this file so that we can later allocate
922 entries in .opd for them. */
924 static bool
925 elf64_hppa_mark_exported_functions (struct elf_link_hash_entry *eh, void *data)
927 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
928 struct bfd_link_info *info = (struct bfd_link_info *)data;
929 struct elf64_hppa_link_hash_table *hppa_info;
931 hppa_info = hppa_link_hash_table (info);
932 if (hppa_info == NULL)
933 return false;
935 if (eh
936 && (eh->root.type == bfd_link_hash_defined
937 || eh->root.type == bfd_link_hash_defweak)
938 && eh->root.u.def.section->output_section != NULL
939 && eh->type == STT_FUNC)
941 if (! hppa_info->opd_sec
942 && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
943 return false;
945 hh->want_opd = 1;
947 /* Put a flag here for output_symbol_hook. */
948 hh->st_shndx = -1;
949 eh->needs_plt = 1;
952 return true;
955 /* Allocate space for a DLT entry. */
957 static bool
958 allocate_global_data_dlt (struct elf_link_hash_entry *eh, void *data)
960 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
961 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
963 if (hh->want_dlt)
965 if (bfd_link_pic (x->info))
967 /* Possibly add the symbol to the local dynamic symbol
968 table since we might need to create a dynamic relocation
969 against it. */
970 if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
972 bfd *owner = eh->root.u.def.section->owner;
974 if (! (bfd_elf_link_record_local_dynamic_symbol
975 (x->info, owner, hh->sym_indx)))
976 return false;
980 hh->dlt_offset = x->ofs;
981 x->ofs += DLT_ENTRY_SIZE;
983 return true;
986 /* Allocate space for a DLT.PLT entry. */
988 static bool
989 allocate_global_data_plt (struct elf_link_hash_entry *eh, void *data)
991 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
992 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *) data;
994 if (hh->want_plt
995 && elf64_hppa_dynamic_symbol_p (eh, x->info)
996 && !((eh->root.type == bfd_link_hash_defined
997 || eh->root.type == bfd_link_hash_defweak)
998 && eh->root.u.def.section->output_section != NULL))
1000 hh->plt_offset = x->ofs;
1001 x->ofs += PLT_ENTRY_SIZE;
1002 if (hh->plt_offset < 0x2000)
1004 struct elf64_hppa_link_hash_table *hppa_info;
1006 hppa_info = hppa_link_hash_table (x->info);
1007 if (hppa_info == NULL)
1008 return false;
1010 hppa_info->gp_offset = hh->plt_offset;
1013 else
1014 hh->want_plt = 0;
1016 return true;
1019 /* Allocate space for a STUB entry. */
1021 static bool
1022 allocate_global_data_stub (struct elf_link_hash_entry *eh, void *data)
1024 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1025 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1027 if (hh->want_stub
1028 && elf64_hppa_dynamic_symbol_p (eh, x->info)
1029 && !((eh->root.type == bfd_link_hash_defined
1030 || eh->root.type == bfd_link_hash_defweak)
1031 && eh->root.u.def.section->output_section != NULL))
1033 hh->stub_offset = x->ofs;
1034 x->ofs += sizeof (plt_stub);
1036 else
1037 hh->want_stub = 0;
1038 return true;
1041 /* Allocate space for a FPTR entry. */
1043 static bool
1044 allocate_global_data_opd (struct elf_link_hash_entry *eh, void *data)
1046 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1047 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1049 if (hh && hh->want_opd)
1051 /* We never need an opd entry for a symbol which is not
1052 defined by this output file. */
1053 if (hh && (hh->eh.root.type == bfd_link_hash_undefined
1054 || hh->eh.root.type == bfd_link_hash_undefweak
1055 || hh->eh.root.u.def.section->output_section == NULL))
1056 hh->want_opd = 0;
1058 /* If we are creating a shared library, took the address of a local
1059 function or might export this function from this object file, then
1060 we have to create an opd descriptor. */
1061 else if (bfd_link_pic (x->info)
1062 || hh == NULL
1063 || (hh->eh.dynindx == -1 && hh->eh.type != STT_PARISC_MILLI)
1064 || (hh->eh.root.type == bfd_link_hash_defined
1065 || hh->eh.root.type == bfd_link_hash_defweak))
1067 /* If we are creating a shared library, then we will have to
1068 create a runtime relocation for the symbol to properly
1069 initialize the .opd entry. Make sure the symbol gets
1070 added to the dynamic symbol table. */
1071 if (bfd_link_pic (x->info)
1072 && (hh == NULL || (hh->eh.dynindx == -1)))
1074 bfd *owner;
1075 /* PR 6511: Default to using the dynamic symbol table. */
1076 owner = (hh->owner ? hh->owner: eh->root.u.def.section->owner);
1078 if (!bfd_elf_link_record_local_dynamic_symbol
1079 (x->info, owner, hh->sym_indx))
1080 return false;
1083 /* This may not be necessary or desirable anymore now that
1084 we have some support for dealing with section symbols
1085 in dynamic relocs. But name munging does make the result
1086 much easier to debug. ie, the EPLT reloc will reference
1087 a symbol like .foobar, instead of .text + offset. */
1088 if (bfd_link_pic (x->info) && eh)
1090 char *new_name;
1091 struct elf_link_hash_entry *nh;
1093 new_name = concat (".", eh->root.root.string, NULL);
1095 nh = elf_link_hash_lookup (elf_hash_table (x->info),
1096 new_name, true, true, true);
1098 free (new_name);
1099 nh->root.type = eh->root.type;
1100 nh->root.u.def.value = eh->root.u.def.value;
1101 nh->root.u.def.section = eh->root.u.def.section;
1103 if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
1104 return false;
1106 hh->opd_offset = x->ofs;
1107 x->ofs += OPD_ENTRY_SIZE;
1110 /* Otherwise we do not need an opd entry. */
1111 else
1112 hh->want_opd = 0;
1114 return true;
1117 /* HP requires the EI_OSABI field to be filled in. The assignment to
1118 EI_ABIVERSION may not be strictly necessary. */
1120 static bool
1121 elf64_hppa_init_file_header (bfd *abfd, struct bfd_link_info *info)
1123 Elf_Internal_Ehdr *i_ehdrp;
1125 if (!_bfd_elf_init_file_header (abfd, info))
1126 return false;
1128 i_ehdrp = elf_elfheader (abfd);
1129 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
1130 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
1131 return true;
1134 /* Create function descriptor section (.opd). This section is called .opd
1135 because it contains "official procedure descriptors". The "official"
1136 refers to the fact that these descriptors are used when taking the address
1137 of a procedure, thus ensuring a unique address for each procedure. */
1139 static bool
1140 get_opd (bfd *abfd,
1141 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1142 struct elf64_hppa_link_hash_table *hppa_info)
1144 asection *opd;
1145 bfd *dynobj;
1147 opd = hppa_info->opd_sec;
1148 if (!opd)
1150 dynobj = hppa_info->root.dynobj;
1151 if (!dynobj)
1152 hppa_info->root.dynobj = dynobj = abfd;
1154 opd = bfd_make_section_anyway_with_flags (dynobj, ".opd",
1155 (SEC_ALLOC
1156 | SEC_LOAD
1157 | SEC_HAS_CONTENTS
1158 | SEC_IN_MEMORY
1159 | SEC_LINKER_CREATED));
1160 if (!opd
1161 || !bfd_set_section_alignment (opd, 3))
1163 BFD_ASSERT (0);
1164 return false;
1167 hppa_info->opd_sec = opd;
1170 return true;
1173 /* Create the PLT section. */
1175 static bool
1176 get_plt (bfd *abfd,
1177 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1178 struct elf64_hppa_link_hash_table *hppa_info)
1180 asection *plt;
1181 bfd *dynobj;
1183 plt = hppa_info->root.splt;
1184 if (!plt)
1186 dynobj = hppa_info->root.dynobj;
1187 if (!dynobj)
1188 hppa_info->root.dynobj = dynobj = abfd;
1190 plt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
1191 (SEC_ALLOC
1192 | SEC_LOAD
1193 | SEC_HAS_CONTENTS
1194 | SEC_IN_MEMORY
1195 | SEC_LINKER_CREATED));
1196 if (!plt
1197 || !bfd_set_section_alignment (plt, 3))
1199 BFD_ASSERT (0);
1200 return false;
1203 hppa_info->root.splt = plt;
1206 return true;
1209 /* Create the DLT section. */
1211 static bool
1212 get_dlt (bfd *abfd,
1213 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1214 struct elf64_hppa_link_hash_table *hppa_info)
1216 asection *dlt;
1217 bfd *dynobj;
1219 dlt = hppa_info->dlt_sec;
1220 if (!dlt)
1222 dynobj = hppa_info->root.dynobj;
1223 if (!dynobj)
1224 hppa_info->root.dynobj = dynobj = abfd;
1226 dlt = bfd_make_section_anyway_with_flags (dynobj, ".dlt",
1227 (SEC_ALLOC
1228 | SEC_LOAD
1229 | SEC_HAS_CONTENTS
1230 | SEC_IN_MEMORY
1231 | SEC_LINKER_CREATED));
1232 if (!dlt
1233 || !bfd_set_section_alignment (dlt, 3))
1235 BFD_ASSERT (0);
1236 return false;
1239 hppa_info->dlt_sec = dlt;
1242 return true;
1245 /* Create the stubs section. */
1247 static bool
1248 get_stub (bfd *abfd,
1249 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1250 struct elf64_hppa_link_hash_table *hppa_info)
1252 asection *stub;
1253 bfd *dynobj;
1255 stub = hppa_info->stub_sec;
1256 if (!stub)
1258 dynobj = hppa_info->root.dynobj;
1259 if (!dynobj)
1260 hppa_info->root.dynobj = dynobj = abfd;
1262 stub = bfd_make_section_anyway_with_flags (dynobj, ".stub",
1263 (SEC_ALLOC | SEC_LOAD
1264 | SEC_HAS_CONTENTS
1265 | SEC_IN_MEMORY
1266 | SEC_READONLY
1267 | SEC_LINKER_CREATED));
1268 if (!stub
1269 || !bfd_set_section_alignment (stub, 3))
1271 BFD_ASSERT (0);
1272 return false;
1275 hppa_info->stub_sec = stub;
1278 return true;
1281 /* Create sections necessary for dynamic linking. This is only a rough
1282 cut and will likely change as we learn more about the somewhat
1283 unusual dynamic linking scheme HP uses.
1285 .stub:
1286 Contains code to implement cross-space calls. The first time one
1287 of the stubs is used it will call into the dynamic linker, later
1288 calls will go straight to the target.
1290 The only stub we support right now looks like
1292 ldd OFFSET(%dp),%r1
1293 bve %r0(%r1)
1294 ldd OFFSET+8(%dp),%dp
1296 Other stubs may be needed in the future. We may want the remove
1297 the break/nop instruction. It is only used right now to keep the
1298 offset of a .plt entry and a .stub entry in sync.
1300 .dlt:
1301 This is what most people call the .got. HP used a different name.
1302 Losers.
1304 .rela.dlt:
1305 Relocations for the DLT.
1307 .plt:
1308 Function pointers as address,gp pairs.
1310 .rela.plt:
1311 Should contain dynamic IPLT (and EPLT?) relocations.
1313 .opd:
1314 FPTRS
1316 .rela.opd:
1317 EPLT relocations for symbols exported from shared libraries. */
1319 static bool
1320 elf64_hppa_create_dynamic_sections (bfd *abfd,
1321 struct bfd_link_info *info)
1323 asection *s;
1324 struct elf64_hppa_link_hash_table *hppa_info;
1326 hppa_info = hppa_link_hash_table (info);
1327 if (hppa_info == NULL)
1328 return false;
1330 if (! get_stub (abfd, info, hppa_info))
1331 return false;
1333 if (! get_dlt (abfd, info, hppa_info))
1334 return false;
1336 if (! get_plt (abfd, info, hppa_info))
1337 return false;
1339 if (! get_opd (abfd, info, hppa_info))
1340 return false;
1342 s = bfd_make_section_anyway_with_flags (abfd, ".rela.dlt",
1343 (SEC_ALLOC | SEC_LOAD
1344 | SEC_HAS_CONTENTS
1345 | SEC_IN_MEMORY
1346 | SEC_READONLY
1347 | SEC_LINKER_CREATED));
1348 if (s == NULL
1349 || !bfd_set_section_alignment (s, 3))
1350 return false;
1351 hppa_info->dlt_rel_sec = s;
1353 s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt",
1354 (SEC_ALLOC | SEC_LOAD
1355 | SEC_HAS_CONTENTS
1356 | SEC_IN_MEMORY
1357 | SEC_READONLY
1358 | SEC_LINKER_CREATED));
1359 if (s == NULL
1360 || !bfd_set_section_alignment (s, 3))
1361 return false;
1362 hppa_info->root.srelplt = s;
1364 s = bfd_make_section_anyway_with_flags (abfd, ".rela.data",
1365 (SEC_ALLOC | SEC_LOAD
1366 | SEC_HAS_CONTENTS
1367 | SEC_IN_MEMORY
1368 | SEC_READONLY
1369 | SEC_LINKER_CREATED));
1370 if (s == NULL
1371 || !bfd_set_section_alignment (s, 3))
1372 return false;
1373 hppa_info->other_rel_sec = s;
1375 s = bfd_make_section_anyway_with_flags (abfd, ".rela.opd",
1376 (SEC_ALLOC | SEC_LOAD
1377 | SEC_HAS_CONTENTS
1378 | SEC_IN_MEMORY
1379 | SEC_READONLY
1380 | SEC_LINKER_CREATED));
1381 if (s == NULL
1382 || !bfd_set_section_alignment (s, 3))
1383 return false;
1384 hppa_info->opd_rel_sec = s;
1386 return true;
1389 /* Allocate dynamic relocations for those symbols that turned out
1390 to be dynamic. */
1392 static bool
1393 allocate_dynrel_entries (struct elf_link_hash_entry *eh, void *data)
1395 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1396 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1397 struct elf64_hppa_link_hash_table *hppa_info;
1398 struct elf64_hppa_dyn_reloc_entry *rent;
1399 bool dynamic_symbol, shared;
1401 hppa_info = hppa_link_hash_table (x->info);
1402 if (hppa_info == NULL)
1403 return false;
1405 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, x->info);
1406 shared = bfd_link_pic (x->info);
1408 /* We may need to allocate relocations for a non-dynamic symbol
1409 when creating a shared library. */
1410 if (!dynamic_symbol && !shared)
1411 return true;
1413 /* Take care of the normal data relocations. */
1415 for (rent = hh->reloc_entries; rent; rent = rent->next)
1417 /* Allocate one iff we are building a shared library, the relocation
1418 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
1419 if (!shared && rent->type == R_PARISC_FPTR64 && hh->want_opd)
1420 continue;
1422 hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
1424 /* Make sure this symbol gets into the dynamic symbol table if it is
1425 not already recorded. ?!? This should not be in the loop since
1426 the symbol need only be added once. */
1427 if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
1428 if (!bfd_elf_link_record_local_dynamic_symbol
1429 (x->info, rent->sec->owner, hh->sym_indx))
1430 return false;
1433 /* Take care of the GOT and PLT relocations. */
1435 if ((dynamic_symbol || shared) && hh->want_dlt)
1436 hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
1438 /* If we are building a shared library, then every symbol that has an
1439 opd entry will need an EPLT relocation to relocate the symbol's address
1440 and __gp value based on the runtime load address. */
1441 if (shared && hh->want_opd)
1442 hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
1444 if (hh->want_plt && dynamic_symbol)
1446 bfd_size_type t = 0;
1448 /* Dynamic symbols get one IPLT relocation. Local symbols in
1449 shared libraries get two REL relocations. Local symbols in
1450 main applications get nothing. */
1451 if (dynamic_symbol)
1452 t = sizeof (Elf64_External_Rela);
1453 else if (shared)
1454 t = 2 * sizeof (Elf64_External_Rela);
1456 hppa_info->root.srelplt->size += t;
1459 return true;
1462 /* Adjust a symbol defined by a dynamic object and referenced by a
1463 regular object. */
1465 static bool
1466 elf64_hppa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1467 struct elf_link_hash_entry *eh)
1469 /* ??? Undefined symbols with PLT entries should be re-defined
1470 to be the PLT entry. */
1472 /* If this is a weak symbol, and there is a real definition, the
1473 processor independent code will have arranged for us to see the
1474 real definition first, and we can just use the same value. */
1475 if (eh->is_weakalias)
1477 struct elf_link_hash_entry *def = weakdef (eh);
1478 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1479 eh->root.u.def.section = def->root.u.def.section;
1480 eh->root.u.def.value = def->root.u.def.value;
1481 return true;
1484 /* If this is a reference to a symbol defined by a dynamic object which
1485 is not a function, we might allocate the symbol in our .dynbss section
1486 and allocate a COPY dynamic relocation.
1488 But PA64 code is canonically PIC, so as a rule we can avoid this sort
1489 of hackery. */
1491 return true;
1494 /* This function is called via elf_link_hash_traverse to mark millicode
1495 symbols with a dynindx of -1 and to remove the string table reference
1496 from the dynamic symbol table. If the symbol is not a millicode symbol,
1497 elf64_hppa_mark_exported_functions is called. */
1499 static bool
1500 elf64_hppa_mark_milli_and_exported_functions (struct elf_link_hash_entry *eh,
1501 void *data)
1503 struct bfd_link_info *info = (struct bfd_link_info *) data;
1505 if (eh->type == STT_PARISC_MILLI)
1507 if (eh->dynindx != -1)
1509 eh->dynindx = -1;
1510 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1511 eh->dynstr_index);
1513 return true;
1516 return elf64_hppa_mark_exported_functions (eh, data);
1519 /* Set the final sizes of the dynamic sections and allocate memory for
1520 the contents of our special sections. */
1522 static bool
1523 elf64_hppa_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
1525 struct elf64_hppa_link_hash_table *hppa_info;
1526 struct elf64_hppa_allocate_data data;
1527 bfd *dynobj;
1528 bfd *ibfd;
1529 asection *sec;
1530 bool relocs;
1532 hppa_info = hppa_link_hash_table (info);
1533 if (hppa_info == NULL)
1534 return false;
1536 dynobj = hppa_info->root.dynobj;
1537 BFD_ASSERT (dynobj != NULL);
1539 /* Mark each function this program exports so that we will allocate
1540 space in the .opd section for each function's FPTR. If we are
1541 creating dynamic sections, change the dynamic index of millicode
1542 symbols to -1 and remove them from the string table for .dynstr.
1544 We have to traverse the main linker hash table since we have to
1545 find functions which may not have been mentioned in any relocs. */
1546 elf_link_hash_traverse (&hppa_info->root,
1547 (hppa_info->root.dynamic_sections_created
1548 ? elf64_hppa_mark_milli_and_exported_functions
1549 : elf64_hppa_mark_exported_functions),
1550 info);
1552 if (hppa_info->root.dynamic_sections_created)
1554 /* Set the contents of the .interp section to the interpreter. */
1555 if (bfd_link_executable (info) && !info->nointerp)
1557 sec = bfd_get_linker_section (dynobj, ".interp");
1558 BFD_ASSERT (sec != NULL);
1559 sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
1560 sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1563 else
1565 /* We may have created entries in the .rela.got section.
1566 However, if we are not creating the dynamic sections, we will
1567 not actually use these entries. Reset the size of .rela.dlt,
1568 which will cause it to get stripped from the output file
1569 below. */
1570 sec = hppa_info->dlt_rel_sec;
1571 if (sec != NULL)
1572 sec->size = 0;
1575 /* Set up DLT, PLT and OPD offsets for local syms, and space for local
1576 dynamic relocs. */
1577 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1579 bfd_signed_vma *local_dlt;
1580 bfd_signed_vma *end_local_dlt;
1581 bfd_signed_vma *local_plt;
1582 bfd_signed_vma *end_local_plt;
1583 bfd_signed_vma *local_opd;
1584 bfd_signed_vma *end_local_opd;
1585 bfd_size_type locsymcount;
1586 Elf_Internal_Shdr *symtab_hdr;
1587 asection *srel;
1589 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1590 continue;
1592 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
1594 struct elf64_hppa_dyn_reloc_entry *hdh_p;
1596 for (hdh_p = ((struct elf64_hppa_dyn_reloc_entry *)
1597 elf_section_data (sec)->local_dynrel);
1598 hdh_p != NULL;
1599 hdh_p = hdh_p->next)
1601 if (!bfd_is_abs_section (hdh_p->sec)
1602 && bfd_is_abs_section (hdh_p->sec->output_section))
1604 /* Input section has been discarded, either because
1605 it is a copy of a linkonce section or due to
1606 linker script /DISCARD/, so we'll be discarding
1607 the relocs too. */
1609 else if (hdh_p->count != 0)
1611 srel = elf_section_data (hdh_p->sec)->sreloc;
1612 srel->size += hdh_p->count * sizeof (Elf64_External_Rela);
1613 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
1614 info->flags |= DF_TEXTREL;
1619 local_dlt = elf_local_got_refcounts (ibfd);
1620 if (!local_dlt)
1621 continue;
1623 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1624 locsymcount = symtab_hdr->sh_info;
1625 end_local_dlt = local_dlt + locsymcount;
1626 sec = hppa_info->dlt_sec;
1627 srel = hppa_info->dlt_rel_sec;
1628 for (; local_dlt < end_local_dlt; ++local_dlt)
1630 if (*local_dlt > 0)
1632 *local_dlt = sec->size;
1633 sec->size += DLT_ENTRY_SIZE;
1634 if (bfd_link_pic (info))
1636 srel->size += sizeof (Elf64_External_Rela);
1639 else
1640 *local_dlt = (bfd_vma) -1;
1643 local_plt = end_local_dlt;
1644 end_local_plt = local_plt + locsymcount;
1645 if (! hppa_info->root.dynamic_sections_created)
1647 /* Won't be used, but be safe. */
1648 for (; local_plt < end_local_plt; ++local_plt)
1649 *local_plt = (bfd_vma) -1;
1651 else
1653 sec = hppa_info->root.splt;
1654 srel = hppa_info->root.srelplt;
1655 for (; local_plt < end_local_plt; ++local_plt)
1657 if (*local_plt > 0)
1659 *local_plt = sec->size;
1660 sec->size += PLT_ENTRY_SIZE;
1661 if (bfd_link_pic (info))
1662 srel->size += sizeof (Elf64_External_Rela);
1664 else
1665 *local_plt = (bfd_vma) -1;
1669 local_opd = end_local_plt;
1670 end_local_opd = local_opd + locsymcount;
1671 if (! hppa_info->root.dynamic_sections_created)
1673 /* Won't be used, but be safe. */
1674 for (; local_opd < end_local_opd; ++local_opd)
1675 *local_opd = (bfd_vma) -1;
1677 else
1679 sec = hppa_info->opd_sec;
1680 srel = hppa_info->opd_rel_sec;
1681 for (; local_opd < end_local_opd; ++local_opd)
1683 if (*local_opd > 0)
1685 *local_opd = sec->size;
1686 sec->size += OPD_ENTRY_SIZE;
1687 if (bfd_link_pic (info))
1688 srel->size += sizeof (Elf64_External_Rela);
1690 else
1691 *local_opd = (bfd_vma) -1;
1696 /* Allocate the GOT entries. */
1698 data.info = info;
1699 if (hppa_info->dlt_sec)
1701 data.ofs = hppa_info->dlt_sec->size;
1702 elf_link_hash_traverse (&hppa_info->root,
1703 allocate_global_data_dlt, &data);
1704 hppa_info->dlt_sec->size = data.ofs;
1707 if (hppa_info->root.splt)
1709 data.ofs = hppa_info->root.splt->size;
1710 elf_link_hash_traverse (&hppa_info->root,
1711 allocate_global_data_plt, &data);
1712 hppa_info->root.splt->size = data.ofs;
1715 if (hppa_info->stub_sec)
1717 data.ofs = 0x0;
1718 elf_link_hash_traverse (&hppa_info->root,
1719 allocate_global_data_stub, &data);
1720 hppa_info->stub_sec->size = data.ofs;
1723 /* Allocate space for entries in the .opd section. */
1724 if (hppa_info->opd_sec)
1726 data.ofs = hppa_info->opd_sec->size;
1727 elf_link_hash_traverse (&hppa_info->root,
1728 allocate_global_data_opd, &data);
1729 hppa_info->opd_sec->size = data.ofs;
1732 /* Now allocate space for dynamic relocations, if necessary. */
1733 if (hppa_info->root.dynamic_sections_created)
1734 elf_link_hash_traverse (&hppa_info->root,
1735 allocate_dynrel_entries, &data);
1737 /* The sizes of all the sections are set. Allocate memory for them. */
1738 relocs = false;
1739 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
1741 const char *name;
1743 if ((sec->flags & SEC_LINKER_CREATED) == 0)
1744 continue;
1746 /* It's OK to base decisions on the section name, because none
1747 of the dynobj section names depend upon the input files. */
1748 name = bfd_section_name (sec);
1750 if (strcmp (name, ".plt") == 0)
1752 /* Remember whether there is a PLT. */
1755 else if (strcmp (name, ".opd") == 0
1756 || startswith (name, ".dlt")
1757 || strcmp (name, ".stub") == 0
1758 || strcmp (name, ".got") == 0)
1760 /* Strip this section if we don't need it; see the comment below. */
1762 else if (startswith (name, ".rela"))
1764 if (sec->size != 0)
1766 /* Remember whether there are any reloc sections other
1767 than .rela.plt. */
1768 if (strcmp (name, ".rela.plt") != 0)
1769 relocs = true;
1771 /* We use the reloc_count field as a counter if we need
1772 to copy relocs into the output file. */
1773 sec->reloc_count = 0;
1776 else
1778 /* It's not one of our sections, so don't allocate space. */
1779 continue;
1782 if (sec->size == 0)
1784 /* If we don't need this section, strip it from the
1785 output file. This is mostly to handle .rela.bss and
1786 .rela.plt. We must create both sections in
1787 create_dynamic_sections, because they must be created
1788 before the linker maps input sections to output
1789 sections. The linker does that before
1790 adjust_dynamic_symbol is called, and it is that
1791 function which decides whether anything needs to go
1792 into these sections. */
1793 sec->flags |= SEC_EXCLUDE;
1794 continue;
1797 if ((sec->flags & SEC_HAS_CONTENTS) == 0)
1798 continue;
1800 /* Allocate memory for the section contents if it has not
1801 been allocated already. We use bfd_zalloc here in case
1802 unused entries are not reclaimed before the section's
1803 contents are written out. This should not happen, but this
1804 way if it does, we get a R_PARISC_NONE reloc instead of
1805 garbage. */
1806 if (sec->contents == NULL)
1808 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
1809 if (sec->contents == NULL)
1810 return false;
1814 if (hppa_info->root.dynamic_sections_created)
1816 /* Always create a DT_PLTGOT. It actually has nothing to do with
1817 the PLT, it is how we communicate the __gp value of a load
1818 module to the dynamic linker. */
1819 #define add_dynamic_entry(TAG, VAL) \
1820 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1822 if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0))
1823 return false;
1825 /* Add some entries to the .dynamic section. We fill in the
1826 values later, in elf64_hppa_finish_dynamic_sections, but we
1827 must add the entries now so that we get the correct size for
1828 the .dynamic section. The DT_DEBUG entry is filled in by the
1829 dynamic linker and used by the debugger. */
1830 if (! bfd_link_pic (info))
1832 if (!add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1833 || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
1834 return false;
1837 /* Force DT_FLAGS to always be set.
1838 Required by HPUX 11.00 patch PHSS_26559. */
1839 if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1840 return false;
1842 #undef add_dynamic_entry
1844 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
1847 /* Called after we have output the symbol into the dynamic symbol
1848 table, but before we output the symbol into the normal symbol
1849 table.
1851 For some symbols we had to change their address when outputting
1852 the dynamic symbol table. We undo that change here so that
1853 the symbols have their expected value in the normal symbol
1854 table. Ick. */
1856 static int
1857 elf64_hppa_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1858 const char *name,
1859 Elf_Internal_Sym *sym,
1860 asection *input_sec ATTRIBUTE_UNUSED,
1861 struct elf_link_hash_entry *eh)
1863 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1865 /* We may be called with the file symbol or section symbols.
1866 They never need munging, so it is safe to ignore them. */
1867 if (!name || !eh)
1868 return 1;
1870 /* Function symbols for which we created .opd entries *may* have been
1871 munged by finish_dynamic_symbol and have to be un-munged here.
1873 Note that finish_dynamic_symbol sometimes turns dynamic symbols
1874 into non-dynamic ones, so we initialize st_shndx to -1 in
1875 mark_exported_functions and check to see if it was overwritten
1876 here instead of just checking eh->dynindx. */
1877 if (hh->want_opd && hh->st_shndx != -1)
1879 /* Restore the saved value and section index. */
1880 sym->st_value = hh->st_value;
1881 sym->st_shndx = hh->st_shndx;
1884 return 1;
1887 /* Finish up dynamic symbol handling. We set the contents of various
1888 dynamic sections here. */
1890 static bool
1891 elf64_hppa_finish_dynamic_symbol (bfd *output_bfd,
1892 struct bfd_link_info *info,
1893 struct elf_link_hash_entry *eh,
1894 Elf_Internal_Sym *sym)
1896 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1897 asection *stub, *splt, *sopd, *spltrel;
1898 struct elf64_hppa_link_hash_table *hppa_info;
1900 hppa_info = hppa_link_hash_table (info);
1901 if (hppa_info == NULL)
1902 return false;
1904 stub = hppa_info->stub_sec;
1905 splt = hppa_info->root.splt;
1906 sopd = hppa_info->opd_sec;
1907 spltrel = hppa_info->root.srelplt;
1909 /* Incredible. It is actually necessary to NOT use the symbol's real
1910 value when building the dynamic symbol table for a shared library.
1911 At least for symbols that refer to functions.
1913 We will store a new value and section index into the symbol long
1914 enough to output it into the dynamic symbol table, then we restore
1915 the original values (in elf64_hppa_link_output_symbol_hook). */
1916 if (hh->want_opd)
1918 BFD_ASSERT (sopd != NULL);
1920 /* Save away the original value and section index so that we
1921 can restore them later. */
1922 hh->st_value = sym->st_value;
1923 hh->st_shndx = sym->st_shndx;
1925 /* For the dynamic symbol table entry, we want the value to be
1926 address of this symbol's entry within the .opd section. */
1927 sym->st_value = (hh->opd_offset
1928 + sopd->output_offset
1929 + sopd->output_section->vma);
1930 sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1931 sopd->output_section);
1934 /* Initialize a .plt entry if requested. */
1935 if (hh->want_plt
1936 && elf64_hppa_dynamic_symbol_p (eh, info))
1938 bfd_vma value;
1939 Elf_Internal_Rela rel;
1940 bfd_byte *loc;
1942 BFD_ASSERT (splt != NULL && spltrel != NULL);
1944 /* We do not actually care about the value in the PLT entry
1945 if we are creating a shared library and the symbol is
1946 still undefined, we create a dynamic relocation to fill
1947 in the correct value. */
1948 if (bfd_link_pic (info) && eh->root.type == bfd_link_hash_undefined)
1949 value = 0;
1950 else
1951 value = (eh->root.u.def.value + eh->root.u.def.section->vma);
1953 /* Fill in the entry in the procedure linkage table.
1955 The format of a plt entry is
1956 <funcaddr> <__gp>.
1958 plt_offset is the offset within the PLT section at which to
1959 install the PLT entry.
1961 We are modifying the in-memory PLT contents here, so we do not add
1962 in the output_offset of the PLT section. */
1964 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset);
1965 value = _bfd_get_gp_value (info->output_bfd);
1966 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset + 0x8);
1968 /* Create a dynamic IPLT relocation for this entry.
1970 We are creating a relocation in the output file's PLT section,
1971 which is included within the DLT secton. So we do need to include
1972 the PLT's output_offset in the computation of the relocation's
1973 address. */
1974 rel.r_offset = (hh->plt_offset + splt->output_offset
1975 + splt->output_section->vma);
1976 rel.r_info = ELF64_R_INFO (hh->eh.dynindx, R_PARISC_IPLT);
1977 rel.r_addend = 0;
1979 loc = spltrel->contents;
1980 loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
1981 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
1984 /* Initialize an external call stub entry if requested. */
1985 if (hh->want_stub
1986 && elf64_hppa_dynamic_symbol_p (eh, info))
1988 bfd_vma value;
1989 int insn;
1990 unsigned int max_offset;
1992 BFD_ASSERT (stub != NULL);
1994 /* Install the generic stub template.
1996 We are modifying the contents of the stub section, so we do not
1997 need to include the stub section's output_offset here. */
1998 memcpy (stub->contents + hh->stub_offset, plt_stub, sizeof (plt_stub));
2000 /* Fix up the first ldd instruction.
2002 We are modifying the contents of the STUB section in memory,
2003 so we do not need to include its output offset in this computation.
2005 Note the plt_offset value is the value of the PLT entry relative to
2006 the start of the PLT section. These instructions will reference
2007 data relative to the value of __gp, which may not necessarily have
2008 the same address as the start of the PLT section.
2010 gp_offset contains the offset of __gp within the PLT section. */
2011 value = hh->plt_offset - hppa_info->gp_offset;
2013 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset);
2014 if (output_bfd->arch_info->mach >= 25)
2016 /* Wide mode allows 16 bit offsets. */
2017 max_offset = 32768;
2018 insn &= ~ 0xfff1;
2019 insn |= re_assemble_16 ((int) value);
2021 else
2023 max_offset = 8192;
2024 insn &= ~ 0x3ff1;
2025 insn |= re_assemble_14 ((int) value);
2028 if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2030 _bfd_error_handler
2031 /* xgettext:c-format */
2032 (_("stub entry for %s cannot load .plt, dp offset = %" PRId64),
2033 hh->eh.root.root.string, (int64_t) value);
2034 return false;
2037 bfd_put_32 (stub->owner, (bfd_vma) insn,
2038 stub->contents + hh->stub_offset);
2040 /* Fix up the second ldd instruction. */
2041 value += 8;
2042 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset + 8);
2043 if (output_bfd->arch_info->mach >= 25)
2045 insn &= ~ 0xfff1;
2046 insn |= re_assemble_16 ((int) value);
2048 else
2050 insn &= ~ 0x3ff1;
2051 insn |= re_assemble_14 ((int) value);
2053 bfd_put_32 (stub->owner, (bfd_vma) insn,
2054 stub->contents + hh->stub_offset + 8);
2057 return true;
2060 /* The .opd section contains FPTRs for each function this file
2061 exports. Initialize the FPTR entries. */
2063 static bool
2064 elf64_hppa_finalize_opd (struct elf_link_hash_entry *eh, void *data)
2066 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2067 struct bfd_link_info *info = (struct bfd_link_info *)data;
2068 struct elf64_hppa_link_hash_table *hppa_info;
2069 asection *sopd;
2070 asection *sopdrel;
2072 hppa_info = hppa_link_hash_table (info);
2073 if (hppa_info == NULL)
2074 return false;
2076 sopd = hppa_info->opd_sec;
2077 sopdrel = hppa_info->opd_rel_sec;
2079 if (hh->want_opd)
2081 bfd_vma value;
2083 /* The first two words of an .opd entry are zero.
2085 We are modifying the contents of the OPD section in memory, so we
2086 do not need to include its output offset in this computation. */
2087 memset (sopd->contents + hh->opd_offset, 0, 16);
2089 value = (eh->root.u.def.value
2090 + eh->root.u.def.section->output_section->vma
2091 + eh->root.u.def.section->output_offset);
2093 /* The next word is the address of the function. */
2094 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 16);
2096 /* The last word is our local __gp value. */
2097 value = _bfd_get_gp_value (info->output_bfd);
2098 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 24);
2101 /* If we are generating a shared library, we must generate EPLT relocations
2102 for each entry in the .opd, even for static functions (they may have
2103 had their address taken). */
2104 if (bfd_link_pic (info) && hh->want_opd)
2106 Elf_Internal_Rela rel;
2107 bfd_byte *loc;
2108 int dynindx;
2110 /* We may need to do a relocation against a local symbol, in
2111 which case we have to look up it's dynamic symbol index off
2112 the local symbol hash table. */
2113 if (eh->dynindx != -1)
2114 dynindx = eh->dynindx;
2115 else
2116 dynindx
2117 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2118 hh->sym_indx);
2120 /* The offset of this relocation is the absolute address of the
2121 .opd entry for this symbol. */
2122 rel.r_offset = (hh->opd_offset + sopd->output_offset
2123 + sopd->output_section->vma);
2125 /* If H is non-null, then we have an external symbol.
2127 It is imperative that we use a different dynamic symbol for the
2128 EPLT relocation if the symbol has global scope.
2130 In the dynamic symbol table, the function symbol will have a value
2131 which is address of the function's .opd entry.
2133 Thus, we can not use that dynamic symbol for the EPLT relocation
2134 (if we did, the data in the .opd would reference itself rather
2135 than the actual address of the function). Instead we have to use
2136 a new dynamic symbol which has the same value as the original global
2137 function symbol.
2139 We prefix the original symbol with a "." and use the new symbol in
2140 the EPLT relocation. This new symbol has already been recorded in
2141 the symbol table, we just have to look it up and use it.
2143 We do not have such problems with static functions because we do
2144 not make their addresses in the dynamic symbol table point to
2145 the .opd entry. Ultimately this should be safe since a static
2146 function can not be directly referenced outside of its shared
2147 library.
2149 We do have to play similar games for FPTR relocations in shared
2150 libraries, including those for static symbols. See the FPTR
2151 handling in elf64_hppa_finalize_dynreloc. */
2152 if (eh)
2154 char *new_name;
2155 struct elf_link_hash_entry *nh;
2157 new_name = concat (".", eh->root.root.string, NULL);
2159 nh = elf_link_hash_lookup (elf_hash_table (info),
2160 new_name, true, true, false);
2162 /* All we really want from the new symbol is its dynamic
2163 symbol index. */
2164 if (nh)
2165 dynindx = nh->dynindx;
2166 free (new_name);
2169 rel.r_addend = 0;
2170 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2172 loc = sopdrel->contents;
2173 loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2174 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2176 return true;
2179 /* The .dlt section contains addresses for items referenced through the
2180 dlt. Note that we can have a DLTIND relocation for a local symbol, thus
2181 we can not depend on finish_dynamic_symbol to initialize the .dlt. */
2183 static bool
2184 elf64_hppa_finalize_dlt (struct elf_link_hash_entry *eh, void *data)
2186 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2187 struct bfd_link_info *info = (struct bfd_link_info *)data;
2188 struct elf64_hppa_link_hash_table *hppa_info;
2189 asection *sdlt, *sdltrel;
2191 hppa_info = hppa_link_hash_table (info);
2192 if (hppa_info == NULL)
2193 return false;
2195 sdlt = hppa_info->dlt_sec;
2196 sdltrel = hppa_info->dlt_rel_sec;
2198 /* H/DYN_H may refer to a local variable and we know it's
2199 address, so there is no need to create a relocation. Just install
2200 the proper value into the DLT, note this shortcut can not be
2201 skipped when building a shared library. */
2202 if (! bfd_link_pic (info) && hh && hh->want_dlt)
2204 bfd_vma value;
2206 /* If we had an LTOFF_FPTR style relocation we want the DLT entry
2207 to point to the FPTR entry in the .opd section.
2209 We include the OPD's output offset in this computation as
2210 we are referring to an absolute address in the resulting
2211 object file. */
2212 if (hh->want_opd)
2214 value = (hh->opd_offset
2215 + hppa_info->opd_sec->output_offset
2216 + hppa_info->opd_sec->output_section->vma);
2218 else if ((eh->root.type == bfd_link_hash_defined
2219 || eh->root.type == bfd_link_hash_defweak)
2220 && eh->root.u.def.section)
2222 value = eh->root.u.def.value + eh->root.u.def.section->output_offset;
2223 if (eh->root.u.def.section->output_section)
2224 value += eh->root.u.def.section->output_section->vma;
2225 else
2226 value += eh->root.u.def.section->vma;
2228 else
2229 /* We have an undefined function reference. */
2230 value = 0;
2232 /* We do not need to include the output offset of the DLT section
2233 here because we are modifying the in-memory contents. */
2234 bfd_put_64 (sdlt->owner, value, sdlt->contents + hh->dlt_offset);
2237 /* Create a relocation for the DLT entry associated with this symbol.
2238 When building a shared library the symbol does not have to be dynamic. */
2239 if (hh->want_dlt
2240 && (elf64_hppa_dynamic_symbol_p (eh, info) || bfd_link_pic (info)))
2242 Elf_Internal_Rela rel;
2243 bfd_byte *loc;
2244 int dynindx;
2246 /* We may need to do a relocation against a local symbol, in
2247 which case we have to look up it's dynamic symbol index off
2248 the local symbol hash table. */
2249 if (eh && eh->dynindx != -1)
2250 dynindx = eh->dynindx;
2251 else
2252 dynindx
2253 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2254 hh->sym_indx);
2256 /* Create a dynamic relocation for this entry. Do include the output
2257 offset of the DLT entry since we need an absolute address in the
2258 resulting object file. */
2259 rel.r_offset = (hh->dlt_offset + sdlt->output_offset
2260 + sdlt->output_section->vma);
2261 if (eh && eh->type == STT_FUNC)
2262 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2263 else
2264 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2265 rel.r_addend = 0;
2267 loc = sdltrel->contents;
2268 loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2269 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2271 return true;
2274 /* Finalize the dynamic relocations. Specifically the FPTR relocations
2275 for dynamic functions used to initialize static data. */
2277 static bool
2278 elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
2279 void *data)
2281 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2282 struct bfd_link_info *info = (struct bfd_link_info *)data;
2283 struct elf64_hppa_link_hash_table *hppa_info;
2284 int dynamic_symbol;
2286 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, info);
2288 if (!dynamic_symbol && !bfd_link_pic (info))
2289 return true;
2291 if (hh->reloc_entries)
2293 struct elf64_hppa_dyn_reloc_entry *rent;
2294 int dynindx;
2296 hppa_info = hppa_link_hash_table (info);
2297 if (hppa_info == NULL)
2298 return false;
2300 /* We may need to do a relocation against a local symbol, in
2301 which case we have to look up it's dynamic symbol index off
2302 the local symbol hash table. */
2303 if (eh->dynindx != -1)
2304 dynindx = eh->dynindx;
2305 else
2306 dynindx
2307 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2308 hh->sym_indx);
2310 for (rent = hh->reloc_entries; rent; rent = rent->next)
2312 Elf_Internal_Rela rel;
2313 bfd_byte *loc;
2315 /* Allocate one iff we are building a shared library, the relocation
2316 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
2317 if (!bfd_link_pic (info)
2318 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2319 continue;
2321 /* Create a dynamic relocation for this entry.
2323 We need the output offset for the reloc's section because
2324 we are creating an absolute address in the resulting object
2325 file. */
2326 rel.r_offset = (rent->offset + rent->sec->output_offset
2327 + rent->sec->output_section->vma);
2329 /* An FPTR64 relocation implies that we took the address of
2330 a function and that the function has an entry in the .opd
2331 section. We want the FPTR64 relocation to reference the
2332 entry in .opd.
2334 We could munge the symbol value in the dynamic symbol table
2335 (in fact we already do for functions with global scope) to point
2336 to the .opd entry. Then we could use that dynamic symbol in
2337 this relocation.
2339 Or we could do something sensible, not munge the symbol's
2340 address and instead just use a different symbol to reference
2341 the .opd entry. At least that seems sensible until you
2342 realize there's no local dynamic symbols we can use for that
2343 purpose. Thus the hair in the check_relocs routine.
2345 We use a section symbol recorded by check_relocs as the
2346 base symbol for the relocation. The addend is the difference
2347 between the section symbol and the address of the .opd entry. */
2348 if (bfd_link_pic (info)
2349 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2351 bfd_vma value, value2;
2353 /* First compute the address of the opd entry for this symbol. */
2354 value = (hh->opd_offset
2355 + hppa_info->opd_sec->output_section->vma
2356 + hppa_info->opd_sec->output_offset);
2358 /* Compute the value of the start of the section with
2359 the relocation. */
2360 value2 = (rent->sec->output_section->vma
2361 + rent->sec->output_offset);
2363 /* Compute the difference between the start of the section
2364 with the relocation and the opd entry. */
2365 value -= value2;
2367 /* The result becomes the addend of the relocation. */
2368 rel.r_addend = value;
2370 /* The section symbol becomes the symbol for the dynamic
2371 relocation. */
2372 dynindx
2373 = _bfd_elf_link_lookup_local_dynindx (info,
2374 rent->sec->owner,
2375 rent->sec_symndx);
2377 else
2378 rel.r_addend = rent->addend;
2380 rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2382 loc = hppa_info->other_rel_sec->contents;
2383 loc += (hppa_info->other_rel_sec->reloc_count++
2384 * sizeof (Elf64_External_Rela));
2385 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2389 return true;
2392 /* Used to decide how to sort relocs in an optimal manner for the
2393 dynamic linker, before writing them out. */
2395 static enum elf_reloc_type_class
2396 elf64_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2397 const asection *rel_sec ATTRIBUTE_UNUSED,
2398 const Elf_Internal_Rela *rela)
2400 if (ELF64_R_SYM (rela->r_info) == STN_UNDEF)
2401 return reloc_class_relative;
2403 switch ((int) ELF64_R_TYPE (rela->r_info))
2405 case R_PARISC_IPLT:
2406 return reloc_class_plt;
2407 case R_PARISC_COPY:
2408 return reloc_class_copy;
2409 default:
2410 return reloc_class_normal;
2414 /* Finish up the dynamic sections. */
2416 static bool
2417 elf64_hppa_finish_dynamic_sections (bfd *output_bfd,
2418 struct bfd_link_info *info)
2420 bfd *dynobj;
2421 asection *sdyn;
2422 struct elf64_hppa_link_hash_table *hppa_info;
2424 hppa_info = hppa_link_hash_table (info);
2425 if (hppa_info == NULL)
2426 return false;
2428 /* Finalize the contents of the .opd section. */
2429 elf_link_hash_traverse (elf_hash_table (info),
2430 elf64_hppa_finalize_opd,
2431 info);
2433 elf_link_hash_traverse (elf_hash_table (info),
2434 elf64_hppa_finalize_dynreloc,
2435 info);
2437 /* Finalize the contents of the .dlt section. */
2438 dynobj = elf_hash_table (info)->dynobj;
2439 /* Finalize the contents of the .dlt section. */
2440 elf_link_hash_traverse (elf_hash_table (info),
2441 elf64_hppa_finalize_dlt,
2442 info);
2444 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2446 if (elf_hash_table (info)->dynamic_sections_created)
2448 Elf64_External_Dyn *dyncon, *dynconend;
2450 BFD_ASSERT (sdyn != NULL);
2452 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2453 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2454 for (; dyncon < dynconend; dyncon++)
2456 Elf_Internal_Dyn dyn;
2457 asection *s;
2459 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2461 switch (dyn.d_tag)
2463 default:
2464 break;
2466 case DT_HP_LOAD_MAP:
2467 /* Compute the absolute address of 16byte scratchpad area
2468 for the dynamic linker.
2470 By convention the linker script will allocate the scratchpad
2471 area at the start of the .data section. So all we have to
2472 to is find the start of the .data section. */
2473 s = bfd_get_section_by_name (output_bfd, ".data");
2474 if (!s)
2475 return false;
2476 dyn.d_un.d_ptr = s->vma;
2477 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2478 break;
2480 case DT_PLTGOT:
2481 /* HP's use PLTGOT to set the GOT register. */
2482 dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2483 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2484 break;
2486 case DT_JMPREL:
2487 s = hppa_info->root.srelplt;
2488 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2489 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2490 break;
2492 case DT_PLTRELSZ:
2493 s = hppa_info->root.srelplt;
2494 dyn.d_un.d_val = s->size;
2495 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2496 break;
2498 case DT_RELA:
2499 s = hppa_info->other_rel_sec;
2500 if (! s || ! s->size)
2501 s = hppa_info->dlt_rel_sec;
2502 if (! s || ! s->size)
2503 s = hppa_info->opd_rel_sec;
2504 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2505 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2506 break;
2508 case DT_RELASZ:
2509 s = hppa_info->other_rel_sec;
2510 dyn.d_un.d_val = s->size;
2511 s = hppa_info->dlt_rel_sec;
2512 dyn.d_un.d_val += s->size;
2513 s = hppa_info->opd_rel_sec;
2514 dyn.d_un.d_val += s->size;
2515 /* There is some question about whether or not the size of
2516 the PLT relocs should be included here. HP's tools do
2517 it, so we'll emulate them. */
2518 s = hppa_info->root.srelplt;
2519 dyn.d_un.d_val += s->size;
2520 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2521 break;
2527 return true;
2530 /* Support for core dump NOTE sections. */
2532 static bool
2533 elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2535 int offset;
2536 size_t size;
2538 switch (note->descsz)
2540 default:
2541 return false;
2543 case 760: /* Linux/hppa */
2544 /* pr_cursig */
2545 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2547 /* pr_pid */
2548 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2550 /* pr_reg */
2551 offset = 112;
2552 size = 640;
2554 break;
2557 /* Make a ".reg/999" section. */
2558 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2559 size, note->descpos + offset);
2562 static bool
2563 elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2565 char * command;
2566 int n;
2568 switch (note->descsz)
2570 default:
2571 return false;
2573 case 136: /* Linux/hppa elf_prpsinfo. */
2574 elf_tdata (abfd)->core->program
2575 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2576 elf_tdata (abfd)->core->command
2577 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2580 /* Note that for some reason, a spurious space is tacked
2581 onto the end of the args in some (at least one anyway)
2582 implementations, so strip it off if it exists. */
2583 command = elf_tdata (abfd)->core->command;
2584 n = strlen (command);
2586 if (0 < n && command[n - 1] == ' ')
2587 command[n - 1] = '\0';
2589 return true;
2592 /* Return the number of additional phdrs we will need.
2594 The generic ELF code only creates PT_PHDRs for executables. The HP
2595 dynamic linker requires PT_PHDRs for dynamic libraries too.
2597 This routine indicates that the backend needs one additional program
2598 header for that case.
2600 Note we do not have access to the link info structure here, so we have
2601 to guess whether or not we are building a shared library based on the
2602 existence of a .interp section. */
2604 static int
2605 elf64_hppa_additional_program_headers (bfd *abfd,
2606 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2608 asection *s;
2610 /* If we are creating a shared library, then we have to create a
2611 PT_PHDR segment. HP's dynamic linker chokes without it. */
2612 s = bfd_get_section_by_name (abfd, ".interp");
2613 if (! s)
2614 return 1;
2615 return 0;
2618 static bool
2619 elf64_hppa_allow_non_load_phdr (bfd *abfd ATTRIBUTE_UNUSED,
2620 const Elf_Internal_Phdr *phdr ATTRIBUTE_UNUSED,
2621 unsigned int count ATTRIBUTE_UNUSED)
2623 return true;
2626 /* Allocate and initialize any program headers required by this
2627 specific backend.
2629 The generic ELF code only creates PT_PHDRs for executables. The HP
2630 dynamic linker requires PT_PHDRs for dynamic libraries too.
2632 This allocates the PT_PHDR and initializes it in a manner suitable
2633 for the HP linker.
2635 Note we do not have access to the link info structure here, so we have
2636 to guess whether or not we are building a shared library based on the
2637 existence of a .interp section. */
2639 static bool
2640 elf64_hppa_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
2642 struct elf_segment_map *m;
2644 m = elf_seg_map (abfd);
2645 if (info != NULL && !info->user_phdrs && m != NULL && m->p_type != PT_PHDR)
2647 m = ((struct elf_segment_map *)
2648 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
2649 if (m == NULL)
2650 return false;
2652 m->p_type = PT_PHDR;
2653 m->p_flags = PF_R | PF_X;
2654 m->p_flags_valid = 1;
2655 m->p_paddr_valid = 1;
2656 m->includes_phdrs = 1;
2658 m->next = elf_seg_map (abfd);
2659 elf_seg_map (abfd) = m;
2662 for (m = elf_seg_map (abfd) ; m != NULL; m = m->next)
2663 if (m->p_type == PT_LOAD)
2665 unsigned int i;
2667 for (i = 0; i < m->count; i++)
2669 /* The code "hint" is not really a hint. It is a requirement
2670 for certain versions of the HP dynamic linker. Worse yet,
2671 it must be set even if the shared library does not have
2672 any code in its "text" segment (thus the check for .hash
2673 to catch this situation). */
2674 if (m->sections[i]->flags & SEC_CODE
2675 || (strcmp (m->sections[i]->name, ".hash") == 0))
2676 m->p_flags |= (PF_X | PF_HP_CODE);
2680 return true;
2683 /* Called when writing out an object file to decide the type of a
2684 symbol. */
2685 static int
2686 elf64_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym,
2687 int type)
2689 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2690 return STT_PARISC_MILLI;
2691 else
2692 return type;
2695 /* Support HP specific sections for core files. */
2697 static bool
2698 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int sec_index,
2699 const char *typename)
2701 if (hdr->p_type == PT_HP_CORE_KERNEL)
2703 asection *sect;
2705 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2706 return false;
2708 sect = bfd_make_section_anyway (abfd, ".kernel");
2709 if (sect == NULL)
2710 return false;
2711 sect->size = hdr->p_filesz;
2712 sect->filepos = hdr->p_offset;
2713 sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2714 return true;
2717 if (hdr->p_type == PT_HP_CORE_PROC)
2719 int sig;
2721 if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2722 return false;
2723 if (bfd_bread (&sig, 4, abfd) != 4)
2724 return false;
2726 elf_tdata (abfd)->core->signal = sig;
2728 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2729 return false;
2731 /* GDB uses the ".reg" section to read register contents. */
2732 return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2733 hdr->p_offset);
2736 if (hdr->p_type == PT_HP_CORE_LOADABLE
2737 || hdr->p_type == PT_HP_CORE_STACK
2738 || hdr->p_type == PT_HP_CORE_MMF)
2739 hdr->p_type = PT_LOAD;
2741 return _bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename);
2744 /* Hook called by the linker routine which adds symbols from an object
2745 file. HP's libraries define symbols with HP specific section
2746 indices, which we have to handle. */
2748 static bool
2749 elf_hppa_add_symbol_hook (bfd *abfd,
2750 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2751 Elf_Internal_Sym *sym,
2752 const char **namep ATTRIBUTE_UNUSED,
2753 flagword *flagsp ATTRIBUTE_UNUSED,
2754 asection **secp,
2755 bfd_vma *valp)
2757 unsigned int sec_index = sym->st_shndx;
2759 switch (sec_index)
2761 case SHN_PARISC_ANSI_COMMON:
2762 *secp = bfd_make_section_old_way (abfd, ".PARISC.ansi.common");
2763 (*secp)->flags |= SEC_IS_COMMON;
2764 *valp = sym->st_size;
2765 break;
2767 case SHN_PARISC_HUGE_COMMON:
2768 *secp = bfd_make_section_old_way (abfd, ".PARISC.huge.common");
2769 (*secp)->flags |= SEC_IS_COMMON;
2770 *valp = sym->st_size;
2771 break;
2774 return true;
2777 static bool
2778 elf_hppa_unmark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2779 void *data)
2781 struct bfd_link_info *info = data;
2783 /* If we are not creating a shared library, and this symbol is
2784 referenced by a shared library but is not defined anywhere, then
2785 the generic code will warn that it is undefined.
2787 This behavior is undesirable on HPs since the standard shared
2788 libraries contain references to undefined symbols.
2790 So we twiddle the flags associated with such symbols so that they
2791 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2793 Ultimately we should have better controls over the generic ELF BFD
2794 linker code. */
2795 if (! bfd_link_relocatable (info)
2796 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2797 && h->root.type == bfd_link_hash_undefined
2798 && h->ref_dynamic
2799 && !h->ref_regular)
2801 h->ref_dynamic = 0;
2802 h->pointer_equality_needed = 1;
2805 return true;
2808 static bool
2809 elf_hppa_remark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2810 void *data)
2812 struct bfd_link_info *info = data;
2814 /* If we are not creating a shared library, and this symbol is
2815 referenced by a shared library but is not defined anywhere, then
2816 the generic code will warn that it is undefined.
2818 This behavior is undesirable on HPs since the standard shared
2819 libraries contain references to undefined symbols.
2821 So we twiddle the flags associated with such symbols so that they
2822 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2824 Ultimately we should have better controls over the generic ELF BFD
2825 linker code. */
2826 if (! bfd_link_relocatable (info)
2827 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2828 && h->root.type == bfd_link_hash_undefined
2829 && !h->ref_dynamic
2830 && !h->ref_regular
2831 && h->pointer_equality_needed)
2833 h->ref_dynamic = 1;
2834 h->pointer_equality_needed = 0;
2837 return true;
2840 static bool
2841 elf_hppa_is_dynamic_loader_symbol (const char *name)
2843 return (! strcmp (name, "__CPU_REVISION")
2844 || ! strcmp (name, "__CPU_KEYBITS_1")
2845 || ! strcmp (name, "__SYSTEM_ID_D")
2846 || ! strcmp (name, "__FPU_MODEL")
2847 || ! strcmp (name, "__FPU_REVISION")
2848 || ! strcmp (name, "__ARGC")
2849 || ! strcmp (name, "__ARGV")
2850 || ! strcmp (name, "__ENVP")
2851 || ! strcmp (name, "__TLS_SIZE_D")
2852 || ! strcmp (name, "__LOAD_INFO")
2853 || ! strcmp (name, "__systab"));
2856 /* Record the lowest address for the data and text segments. */
2857 static void
2858 elf_hppa_record_segment_addrs (bfd *abfd,
2859 asection *section,
2860 void *data)
2862 struct elf64_hppa_link_hash_table *hppa_info = data;
2864 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2866 bfd_vma value;
2867 Elf_Internal_Phdr *p;
2869 p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
2870 BFD_ASSERT (p != NULL);
2871 value = p->p_vaddr;
2873 if (section->flags & SEC_READONLY)
2875 if (value < hppa_info->text_segment_base)
2876 hppa_info->text_segment_base = value;
2878 else
2880 if (value < hppa_info->data_segment_base)
2881 hppa_info->data_segment_base = value;
2886 /* Called after we have seen all the input files/sections, but before
2887 final symbol resolution and section placement has been determined.
2889 We use this hook to (possibly) provide a value for __gp, then we
2890 fall back to the generic ELF final link routine. */
2892 static bool
2893 elf_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
2895 struct stat buf;
2896 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
2898 if (hppa_info == NULL)
2899 return false;
2901 if (! bfd_link_relocatable (info))
2903 struct elf_link_hash_entry *gp;
2904 bfd_vma gp_val;
2906 /* The linker script defines a value for __gp iff it was referenced
2907 by one of the objects being linked. First try to find the symbol
2908 in the hash table. If that fails, just compute the value __gp
2909 should have had. */
2910 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
2911 false, false);
2913 if (gp)
2916 /* Adjust the value of __gp as we may want to slide it into the
2917 .plt section so that the stubs can access PLT entries without
2918 using an addil sequence. */
2919 gp->root.u.def.value += hppa_info->gp_offset;
2921 gp_val = (gp->root.u.def.section->output_section->vma
2922 + gp->root.u.def.section->output_offset
2923 + gp->root.u.def.value);
2925 else
2927 asection *sec;
2929 /* First look for a .plt section. If found, then __gp is the
2930 address of the .plt + gp_offset.
2932 If no .plt is found, then look for .dlt, .opd and .data (in
2933 that order) and set __gp to the base address of whichever
2934 section is found first. */
2936 sec = hppa_info->root.splt;
2937 if (sec && ! (sec->flags & SEC_EXCLUDE))
2938 gp_val = (sec->output_offset
2939 + sec->output_section->vma
2940 + hppa_info->gp_offset);
2941 else
2943 sec = hppa_info->dlt_sec;
2944 if (!sec || (sec->flags & SEC_EXCLUDE))
2945 sec = hppa_info->opd_sec;
2946 if (!sec || (sec->flags & SEC_EXCLUDE))
2947 sec = bfd_get_section_by_name (abfd, ".data");
2948 if (!sec || (sec->flags & SEC_EXCLUDE))
2949 gp_val = 0;
2950 else
2951 gp_val = sec->output_offset + sec->output_section->vma;
2955 /* Install whatever value we found/computed for __gp. */
2956 _bfd_set_gp_value (abfd, gp_val);
2959 /* We need to know the base of the text and data segments so that we
2960 can perform SEGREL relocations. We will record the base addresses
2961 when we encounter the first SEGREL relocation. */
2962 hppa_info->text_segment_base = (bfd_vma)-1;
2963 hppa_info->data_segment_base = (bfd_vma)-1;
2965 /* HP's shared libraries have references to symbols that are not
2966 defined anywhere. The generic ELF BFD linker code will complain
2967 about such symbols.
2969 So we detect the losing case and arrange for the flags on the symbol
2970 to indicate that it was never referenced. This keeps the generic
2971 ELF BFD link code happy and appears to not create any secondary
2972 problems. Ultimately we need a way to control the behavior of the
2973 generic ELF BFD link code better. */
2974 elf_link_hash_traverse (elf_hash_table (info),
2975 elf_hppa_unmark_useless_dynamic_symbols,
2976 info);
2978 /* Invoke the regular ELF backend linker to do all the work. */
2979 if (!bfd_elf_final_link (abfd, info))
2980 return false;
2982 elf_link_hash_traverse (elf_hash_table (info),
2983 elf_hppa_remark_useless_dynamic_symbols,
2984 info);
2986 /* If we're producing a final executable, sort the contents of the
2987 unwind section. */
2988 if (bfd_link_relocatable (info))
2989 return true;
2991 /* Do not attempt to sort non-regular files. This is here
2992 especially for configure scripts and kernel builds which run
2993 tests with "ld [...] -o /dev/null". */
2994 if (stat (bfd_get_filename (abfd), &buf) != 0
2995 || !S_ISREG(buf.st_mode))
2996 return true;
2998 return elf_hppa_sort_unwind (abfd);
3001 /* Relocate the given INSN. VALUE should be the actual value we want
3002 to insert into the instruction, ie by this point we should not be
3003 concerned with computing an offset relative to the DLT, PC, etc.
3004 Instead this routine is meant to handle the bit manipulations needed
3005 to insert the relocation into the given instruction. */
3007 static int
3008 elf_hppa_relocate_insn (int insn, int sym_value, unsigned int r_type)
3010 switch (r_type)
3012 /* This is any 22 bit branch. In PA2.0 syntax it corresponds to
3013 the "B" instruction. */
3014 case R_PARISC_PCREL22F:
3015 case R_PARISC_PCREL22C:
3016 return (insn & ~0x3ff1ffd) | re_assemble_22 (sym_value);
3018 /* This is any 12 bit branch. */
3019 case R_PARISC_PCREL12F:
3020 return (insn & ~0x1ffd) | re_assemble_12 (sym_value);
3022 /* This is any 17 bit branch. In PA2.0 syntax it also corresponds
3023 to the "B" instruction as well as BE. */
3024 case R_PARISC_PCREL17F:
3025 case R_PARISC_DIR17F:
3026 case R_PARISC_DIR17R:
3027 case R_PARISC_PCREL17C:
3028 case R_PARISC_PCREL17R:
3029 return (insn & ~0x1f1ffd) | re_assemble_17 (sym_value);
3031 /* ADDIL or LDIL instructions. */
3032 case R_PARISC_DLTREL21L:
3033 case R_PARISC_DLTIND21L:
3034 case R_PARISC_LTOFF_FPTR21L:
3035 case R_PARISC_PCREL21L:
3036 case R_PARISC_LTOFF_TP21L:
3037 case R_PARISC_DPREL21L:
3038 case R_PARISC_PLTOFF21L:
3039 case R_PARISC_DIR21L:
3040 return (insn & ~0x1fffff) | re_assemble_21 (sym_value);
3042 /* LDO and integer loads/stores with 14 bit displacements. */
3043 case R_PARISC_DLTREL14R:
3044 case R_PARISC_DLTREL14F:
3045 case R_PARISC_DLTIND14R:
3046 case R_PARISC_DLTIND14F:
3047 case R_PARISC_LTOFF_FPTR14R:
3048 case R_PARISC_PCREL14R:
3049 case R_PARISC_PCREL14F:
3050 case R_PARISC_LTOFF_TP14R:
3051 case R_PARISC_LTOFF_TP14F:
3052 case R_PARISC_DPREL14R:
3053 case R_PARISC_DPREL14F:
3054 case R_PARISC_PLTOFF14R:
3055 case R_PARISC_PLTOFF14F:
3056 case R_PARISC_DIR14R:
3057 case R_PARISC_DIR14F:
3058 return (insn & ~0x3fff) | low_sign_unext (sym_value, 14);
3060 /* PA2.0W LDO and integer loads/stores with 16 bit displacements. */
3061 case R_PARISC_LTOFF_FPTR16F:
3062 case R_PARISC_PCREL16F:
3063 case R_PARISC_LTOFF_TP16F:
3064 case R_PARISC_GPREL16F:
3065 case R_PARISC_PLTOFF16F:
3066 case R_PARISC_DIR16F:
3067 case R_PARISC_LTOFF16F:
3068 return (insn & ~0xffff) | re_assemble_16 (sym_value);
3070 /* Doubleword loads and stores with a 14 bit displacement. */
3071 case R_PARISC_DLTREL14DR:
3072 case R_PARISC_DLTIND14DR:
3073 case R_PARISC_LTOFF_FPTR14DR:
3074 case R_PARISC_LTOFF_FPTR16DF:
3075 case R_PARISC_PCREL14DR:
3076 case R_PARISC_PCREL16DF:
3077 case R_PARISC_LTOFF_TP14DR:
3078 case R_PARISC_LTOFF_TP16DF:
3079 case R_PARISC_DPREL14DR:
3080 case R_PARISC_GPREL16DF:
3081 case R_PARISC_PLTOFF14DR:
3082 case R_PARISC_PLTOFF16DF:
3083 case R_PARISC_DIR14DR:
3084 case R_PARISC_DIR16DF:
3085 case R_PARISC_LTOFF16DF:
3086 return (insn & ~0x3ff1) | (((sym_value & 0x2000) >> 13)
3087 | ((sym_value & 0x1ff8) << 1));
3089 /* Floating point single word load/store instructions. */
3090 case R_PARISC_DLTREL14WR:
3091 case R_PARISC_DLTIND14WR:
3092 case R_PARISC_LTOFF_FPTR14WR:
3093 case R_PARISC_LTOFF_FPTR16WF:
3094 case R_PARISC_PCREL14WR:
3095 case R_PARISC_PCREL16WF:
3096 case R_PARISC_LTOFF_TP14WR:
3097 case R_PARISC_LTOFF_TP16WF:
3098 case R_PARISC_DPREL14WR:
3099 case R_PARISC_GPREL16WF:
3100 case R_PARISC_PLTOFF14WR:
3101 case R_PARISC_PLTOFF16WF:
3102 case R_PARISC_DIR16WF:
3103 case R_PARISC_DIR14WR:
3104 case R_PARISC_LTOFF16WF:
3105 return (insn & ~0x3ff9) | (((sym_value & 0x2000) >> 13)
3106 | ((sym_value & 0x1ffc) << 1));
3108 default:
3109 return insn;
3113 /* Compute the value for a relocation (REL) during a final link stage,
3114 then insert the value into the proper location in CONTENTS.
3116 VALUE is a tentative value for the relocation and may be overridden
3117 and modified here based on the specific relocation to be performed.
3119 For example we do conversions for PC-relative branches in this routine
3120 or redirection of calls to external routines to stubs.
3122 The work of actually applying the relocation is left to a helper
3123 routine in an attempt to reduce the complexity and size of this
3124 function. */
3126 static bfd_reloc_status_type
3127 elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
3128 bfd *input_bfd,
3129 bfd *output_bfd,
3130 asection *input_section,
3131 bfd_byte *contents,
3132 bfd_vma value,
3133 struct bfd_link_info *info,
3134 asection *sym_sec,
3135 struct elf_link_hash_entry *eh)
3137 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
3138 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
3139 bfd_vma *local_offsets;
3140 Elf_Internal_Shdr *symtab_hdr;
3141 int insn;
3142 bfd_vma max_branch_offset = 0;
3143 bfd_vma offset = rel->r_offset;
3144 bfd_signed_vma addend = rel->r_addend;
3145 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3146 unsigned int r_symndx = ELF_R_SYM (rel->r_info);
3147 unsigned int r_type = howto->type;
3148 bfd_byte *hit_data = contents + offset;
3150 if (hppa_info == NULL)
3151 return bfd_reloc_notsupported;
3153 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3154 local_offsets = elf_local_got_offsets (input_bfd);
3155 insn = bfd_get_32 (input_bfd, hit_data);
3157 switch (r_type)
3159 case R_PARISC_NONE:
3160 break;
3162 /* Basic function call support.
3164 Note for a call to a function defined in another dynamic library
3165 we want to redirect the call to a stub. */
3167 /* PC relative relocs without an implicit offset. */
3168 case R_PARISC_PCREL21L:
3169 case R_PARISC_PCREL14R:
3170 case R_PARISC_PCREL14F:
3171 case R_PARISC_PCREL14WR:
3172 case R_PARISC_PCREL14DR:
3173 case R_PARISC_PCREL16F:
3174 case R_PARISC_PCREL16WF:
3175 case R_PARISC_PCREL16DF:
3177 /* If this is a call to a function defined in another dynamic
3178 library, then redirect the call to the local stub for this
3179 function. */
3180 if (sym_sec == NULL || sym_sec->output_section == NULL)
3181 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3182 + hppa_info->stub_sec->output_section->vma);
3184 /* Turn VALUE into a proper PC relative address. */
3185 value -= (offset + input_section->output_offset
3186 + input_section->output_section->vma);
3188 /* Adjust for any field selectors. */
3189 if (r_type == R_PARISC_PCREL21L)
3190 value = hppa_field_adjust (value, -8 + addend, e_lsel);
3191 else if (r_type == R_PARISC_PCREL14F
3192 || r_type == R_PARISC_PCREL16F
3193 || r_type == R_PARISC_PCREL16WF
3194 || r_type == R_PARISC_PCREL16DF)
3195 value = hppa_field_adjust (value, -8 + addend, e_fsel);
3196 else
3197 value = hppa_field_adjust (value, -8 + addend, e_rsel);
3199 /* Apply the relocation to the given instruction. */
3200 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3201 break;
3204 case R_PARISC_PCREL12F:
3205 case R_PARISC_PCREL22F:
3206 case R_PARISC_PCREL17F:
3207 case R_PARISC_PCREL22C:
3208 case R_PARISC_PCREL17C:
3209 case R_PARISC_PCREL17R:
3211 /* If this is a call to a function defined in another dynamic
3212 library, then redirect the call to the local stub for this
3213 function. */
3214 if (sym_sec == NULL || sym_sec->output_section == NULL)
3215 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3216 + hppa_info->stub_sec->output_section->vma);
3218 /* Turn VALUE into a proper PC relative address. */
3219 value -= (offset + input_section->output_offset
3220 + input_section->output_section->vma);
3221 addend -= 8;
3223 if (r_type == (unsigned int) R_PARISC_PCREL22F)
3224 max_branch_offset = (1 << (22-1)) << 2;
3225 else if (r_type == (unsigned int) R_PARISC_PCREL17F)
3226 max_branch_offset = (1 << (17-1)) << 2;
3227 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3228 max_branch_offset = (1 << (12-1)) << 2;
3230 /* Make sure we can reach the branch target. */
3231 if (max_branch_offset != 0
3232 && value + addend + max_branch_offset >= 2*max_branch_offset)
3234 _bfd_error_handler
3235 /* xgettext:c-format */
3236 (_("%pB(%pA+%#" PRIx64 "): cannot reach %s"),
3237 input_bfd,
3238 input_section,
3239 (uint64_t) offset,
3240 eh ? eh->root.root.string : "unknown");
3241 bfd_set_error (bfd_error_bad_value);
3242 return bfd_reloc_overflow;
3245 /* Adjust for any field selectors. */
3246 if (r_type == R_PARISC_PCREL17R)
3247 value = hppa_field_adjust (value, addend, e_rsel);
3248 else
3249 value = hppa_field_adjust (value, addend, e_fsel);
3251 /* All branches are implicitly shifted by 2 places. */
3252 value >>= 2;
3254 /* Apply the relocation to the given instruction. */
3255 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3256 break;
3259 /* Indirect references to data through the DLT. */
3260 case R_PARISC_DLTIND14R:
3261 case R_PARISC_DLTIND14F:
3262 case R_PARISC_DLTIND14DR:
3263 case R_PARISC_DLTIND14WR:
3264 case R_PARISC_DLTIND21L:
3265 case R_PARISC_LTOFF_FPTR14R:
3266 case R_PARISC_LTOFF_FPTR14DR:
3267 case R_PARISC_LTOFF_FPTR14WR:
3268 case R_PARISC_LTOFF_FPTR21L:
3269 case R_PARISC_LTOFF_FPTR16F:
3270 case R_PARISC_LTOFF_FPTR16WF:
3271 case R_PARISC_LTOFF_FPTR16DF:
3272 case R_PARISC_LTOFF_TP21L:
3273 case R_PARISC_LTOFF_TP14R:
3274 case R_PARISC_LTOFF_TP14F:
3275 case R_PARISC_LTOFF_TP14WR:
3276 case R_PARISC_LTOFF_TP14DR:
3277 case R_PARISC_LTOFF_TP16F:
3278 case R_PARISC_LTOFF_TP16WF:
3279 case R_PARISC_LTOFF_TP16DF:
3280 case R_PARISC_LTOFF16F:
3281 case R_PARISC_LTOFF16WF:
3282 case R_PARISC_LTOFF16DF:
3284 bfd_vma off;
3286 /* If this relocation was against a local symbol, then we still
3287 have not set up the DLT entry (it's not convenient to do so
3288 in the "finalize_dlt" routine because it is difficult to get
3289 to the local symbol's value).
3291 So, if this is a local symbol (h == NULL), then we need to
3292 fill in its DLT entry.
3294 Similarly we may still need to set up an entry in .opd for
3295 a local function which had its address taken. */
3296 if (hh == NULL)
3298 bfd_vma *local_opd_offsets, *local_dlt_offsets;
3300 if (local_offsets == NULL)
3301 abort ();
3303 /* Now do .opd creation if needed. */
3304 if (r_type == R_PARISC_LTOFF_FPTR14R
3305 || r_type == R_PARISC_LTOFF_FPTR14DR
3306 || r_type == R_PARISC_LTOFF_FPTR14WR
3307 || r_type == R_PARISC_LTOFF_FPTR21L
3308 || r_type == R_PARISC_LTOFF_FPTR16F
3309 || r_type == R_PARISC_LTOFF_FPTR16WF
3310 || r_type == R_PARISC_LTOFF_FPTR16DF)
3312 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3313 off = local_opd_offsets[r_symndx];
3315 /* The last bit records whether we've already initialised
3316 this local .opd entry. */
3317 if ((off & 1) != 0)
3319 BFD_ASSERT (off != (bfd_vma) -1);
3320 off &= ~1;
3322 else
3324 local_opd_offsets[r_symndx] |= 1;
3326 /* The first two words of an .opd entry are zero. */
3327 memset (hppa_info->opd_sec->contents + off, 0, 16);
3329 /* The next word is the address of the function. */
3330 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3331 (hppa_info->opd_sec->contents + off + 16));
3333 /* The last word is our local __gp value. */
3334 value = _bfd_get_gp_value (info->output_bfd);
3335 bfd_put_64 (hppa_info->opd_sec->owner, value,
3336 (hppa_info->opd_sec->contents + off + 24));
3339 /* The DLT value is the address of the .opd entry. */
3340 value = (off
3341 + hppa_info->opd_sec->output_offset
3342 + hppa_info->opd_sec->output_section->vma);
3343 addend = 0;
3346 local_dlt_offsets = local_offsets;
3347 off = local_dlt_offsets[r_symndx];
3349 if ((off & 1) != 0)
3351 BFD_ASSERT (off != (bfd_vma) -1);
3352 off &= ~1;
3354 else
3356 local_dlt_offsets[r_symndx] |= 1;
3357 bfd_put_64 (hppa_info->dlt_sec->owner,
3358 value + addend,
3359 hppa_info->dlt_sec->contents + off);
3362 else
3363 off = hh->dlt_offset;
3365 /* We want the value of the DLT offset for this symbol, not
3366 the symbol's actual address. Note that __gp may not point
3367 to the start of the DLT, so we have to compute the absolute
3368 address, then subtract out the value of __gp. */
3369 value = (off
3370 + hppa_info->dlt_sec->output_offset
3371 + hppa_info->dlt_sec->output_section->vma);
3372 value -= _bfd_get_gp_value (output_bfd);
3374 /* All DLTIND relocations are basically the same at this point,
3375 except that we need different field selectors for the 21bit
3376 version vs the 14bit versions. */
3377 if (r_type == R_PARISC_DLTIND21L
3378 || r_type == R_PARISC_LTOFF_FPTR21L
3379 || r_type == R_PARISC_LTOFF_TP21L)
3380 value = hppa_field_adjust (value, 0, e_lsel);
3381 else if (r_type == R_PARISC_DLTIND14F
3382 || r_type == R_PARISC_LTOFF_FPTR16F
3383 || r_type == R_PARISC_LTOFF_FPTR16WF
3384 || r_type == R_PARISC_LTOFF_FPTR16DF
3385 || r_type == R_PARISC_LTOFF16F
3386 || r_type == R_PARISC_LTOFF16DF
3387 || r_type == R_PARISC_LTOFF16WF
3388 || r_type == R_PARISC_LTOFF_TP16F
3389 || r_type == R_PARISC_LTOFF_TP16WF
3390 || r_type == R_PARISC_LTOFF_TP16DF)
3391 value = hppa_field_adjust (value, 0, e_fsel);
3392 else
3393 value = hppa_field_adjust (value, 0, e_rsel);
3395 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3396 break;
3399 case R_PARISC_DLTREL14R:
3400 case R_PARISC_DLTREL14F:
3401 case R_PARISC_DLTREL14DR:
3402 case R_PARISC_DLTREL14WR:
3403 case R_PARISC_DLTREL21L:
3404 case R_PARISC_DPREL21L:
3405 case R_PARISC_DPREL14WR:
3406 case R_PARISC_DPREL14DR:
3407 case R_PARISC_DPREL14R:
3408 case R_PARISC_DPREL14F:
3409 case R_PARISC_GPREL16F:
3410 case R_PARISC_GPREL16WF:
3411 case R_PARISC_GPREL16DF:
3413 /* Subtract out the global pointer value to make value a DLT
3414 relative address. */
3415 value -= _bfd_get_gp_value (output_bfd);
3417 /* All DLTREL relocations are basically the same at this point,
3418 except that we need different field selectors for the 21bit
3419 version vs the 14bit versions. */
3420 if (r_type == R_PARISC_DLTREL21L
3421 || r_type == R_PARISC_DPREL21L)
3422 value = hppa_field_adjust (value, addend, e_lrsel);
3423 else if (r_type == R_PARISC_DLTREL14F
3424 || r_type == R_PARISC_DPREL14F
3425 || r_type == R_PARISC_GPREL16F
3426 || r_type == R_PARISC_GPREL16WF
3427 || r_type == R_PARISC_GPREL16DF)
3428 value = hppa_field_adjust (value, addend, e_fsel);
3429 else
3430 value = hppa_field_adjust (value, addend, e_rrsel);
3432 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3433 break;
3436 case R_PARISC_DIR21L:
3437 case R_PARISC_DIR17R:
3438 case R_PARISC_DIR17F:
3439 case R_PARISC_DIR14R:
3440 case R_PARISC_DIR14F:
3441 case R_PARISC_DIR14WR:
3442 case R_PARISC_DIR14DR:
3443 case R_PARISC_DIR16F:
3444 case R_PARISC_DIR16WF:
3445 case R_PARISC_DIR16DF:
3447 /* All DIR relocations are basically the same at this point,
3448 except that branch offsets need to be divided by four, and
3449 we need different field selectors. Note that we don't
3450 redirect absolute calls to local stubs. */
3452 if (r_type == R_PARISC_DIR21L)
3453 value = hppa_field_adjust (value, addend, e_lrsel);
3454 else if (r_type == R_PARISC_DIR17F
3455 || r_type == R_PARISC_DIR16F
3456 || r_type == R_PARISC_DIR16WF
3457 || r_type == R_PARISC_DIR16DF
3458 || r_type == R_PARISC_DIR14F)
3459 value = hppa_field_adjust (value, addend, e_fsel);
3460 else
3461 value = hppa_field_adjust (value, addend, e_rrsel);
3463 if (r_type == R_PARISC_DIR17R || r_type == R_PARISC_DIR17F)
3464 /* All branches are implicitly shifted by 2 places. */
3465 value >>= 2;
3467 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3468 break;
3471 case R_PARISC_PLTOFF21L:
3472 case R_PARISC_PLTOFF14R:
3473 case R_PARISC_PLTOFF14F:
3474 case R_PARISC_PLTOFF14WR:
3475 case R_PARISC_PLTOFF14DR:
3476 case R_PARISC_PLTOFF16F:
3477 case R_PARISC_PLTOFF16WF:
3478 case R_PARISC_PLTOFF16DF:
3480 /* We want the value of the PLT offset for this symbol, not
3481 the symbol's actual address. Note that __gp may not point
3482 to the start of the DLT, so we have to compute the absolute
3483 address, then subtract out the value of __gp. */
3484 value = (hh->plt_offset
3485 + hppa_info->root.splt->output_offset
3486 + hppa_info->root.splt->output_section->vma);
3487 value -= _bfd_get_gp_value (output_bfd);
3489 /* All PLTOFF relocations are basically the same at this point,
3490 except that we need different field selectors for the 21bit
3491 version vs the 14bit versions. */
3492 if (r_type == R_PARISC_PLTOFF21L)
3493 value = hppa_field_adjust (value, addend, e_lrsel);
3494 else if (r_type == R_PARISC_PLTOFF14F
3495 || r_type == R_PARISC_PLTOFF16F
3496 || r_type == R_PARISC_PLTOFF16WF
3497 || r_type == R_PARISC_PLTOFF16DF)
3498 value = hppa_field_adjust (value, addend, e_fsel);
3499 else
3500 value = hppa_field_adjust (value, addend, e_rrsel);
3502 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3503 break;
3506 case R_PARISC_LTOFF_FPTR32:
3508 /* FIXME: There used to be code here to create the FPTR itself if
3509 the relocation was against a local symbol. But the code could
3510 never have worked. If the assert below is ever triggered then
3511 the code will need to be reinstated and fixed so that it does
3512 what is needed. */
3513 BFD_ASSERT (hh != NULL);
3515 /* We want the value of the DLT offset for this symbol, not
3516 the symbol's actual address. Note that __gp may not point
3517 to the start of the DLT, so we have to compute the absolute
3518 address, then subtract out the value of __gp. */
3519 value = (hh->dlt_offset
3520 + hppa_info->dlt_sec->output_offset
3521 + hppa_info->dlt_sec->output_section->vma);
3522 value -= _bfd_get_gp_value (output_bfd);
3523 bfd_put_32 (input_bfd, value, hit_data);
3524 return bfd_reloc_ok;
3527 case R_PARISC_LTOFF_FPTR64:
3528 case R_PARISC_LTOFF_TP64:
3530 /* We may still need to create the FPTR itself if it was for
3531 a local symbol. */
3532 if (eh == NULL && r_type == R_PARISC_LTOFF_FPTR64)
3534 /* The first two words of an .opd entry are zero. */
3535 memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
3537 /* The next word is the address of the function. */
3538 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3539 (hppa_info->opd_sec->contents
3540 + hh->opd_offset + 16));
3542 /* The last word is our local __gp value. */
3543 value = _bfd_get_gp_value (info->output_bfd);
3544 bfd_put_64 (hppa_info->opd_sec->owner, value,
3545 hppa_info->opd_sec->contents + hh->opd_offset + 24);
3547 /* The DLT value is the address of the .opd entry. */
3548 value = (hh->opd_offset
3549 + hppa_info->opd_sec->output_offset
3550 + hppa_info->opd_sec->output_section->vma);
3552 bfd_put_64 (hppa_info->dlt_sec->owner,
3553 value,
3554 hppa_info->dlt_sec->contents + hh->dlt_offset);
3557 /* We want the value of the DLT offset for this symbol, not
3558 the symbol's actual address. Note that __gp may not point
3559 to the start of the DLT, so we have to compute the absolute
3560 address, then subtract out the value of __gp. */
3561 value = (hh->dlt_offset
3562 + hppa_info->dlt_sec->output_offset
3563 + hppa_info->dlt_sec->output_section->vma);
3564 value -= _bfd_get_gp_value (output_bfd);
3565 bfd_put_64 (input_bfd, value, hit_data);
3566 return bfd_reloc_ok;
3569 case R_PARISC_DIR32:
3570 bfd_put_32 (input_bfd, value + addend, hit_data);
3571 return bfd_reloc_ok;
3573 case R_PARISC_DIR64:
3574 bfd_put_64 (input_bfd, value + addend, hit_data);
3575 return bfd_reloc_ok;
3577 case R_PARISC_GPREL64:
3578 /* Subtract out the global pointer value to make value a DLT
3579 relative address. */
3580 value -= _bfd_get_gp_value (output_bfd);
3582 bfd_put_64 (input_bfd, value + addend, hit_data);
3583 return bfd_reloc_ok;
3585 case R_PARISC_LTOFF64:
3586 /* We want the value of the DLT offset for this symbol, not
3587 the symbol's actual address. Note that __gp may not point
3588 to the start of the DLT, so we have to compute the absolute
3589 address, then subtract out the value of __gp. */
3590 value = (hh->dlt_offset
3591 + hppa_info->dlt_sec->output_offset
3592 + hppa_info->dlt_sec->output_section->vma);
3593 value -= _bfd_get_gp_value (output_bfd);
3595 bfd_put_64 (input_bfd, value + addend, hit_data);
3596 return bfd_reloc_ok;
3598 case R_PARISC_PCREL32:
3600 /* If this is a call to a function defined in another dynamic
3601 library, then redirect the call to the local stub for this
3602 function. */
3603 if (sym_sec == NULL || sym_sec->output_section == NULL)
3604 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3605 + hppa_info->stub_sec->output_section->vma);
3607 /* Turn VALUE into a proper PC relative address. */
3608 value -= (offset + input_section->output_offset
3609 + input_section->output_section->vma);
3611 value += addend;
3612 value -= 8;
3613 bfd_put_32 (input_bfd, value, hit_data);
3614 return bfd_reloc_ok;
3617 case R_PARISC_PCREL64:
3619 /* If this is a call to a function defined in another dynamic
3620 library, then redirect the call to the local stub for this
3621 function. */
3622 if (sym_sec == NULL || sym_sec->output_section == NULL)
3623 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3624 + hppa_info->stub_sec->output_section->vma);
3626 /* Turn VALUE into a proper PC relative address. */
3627 value -= (offset + input_section->output_offset
3628 + input_section->output_section->vma);
3630 value += addend;
3631 value -= 8;
3632 bfd_put_64 (input_bfd, value, hit_data);
3633 return bfd_reloc_ok;
3636 case R_PARISC_FPTR64:
3638 bfd_vma off;
3640 /* We may still need to create the FPTR itself if it was for
3641 a local symbol. */
3642 if (hh == NULL)
3644 bfd_vma *local_opd_offsets;
3646 if (local_offsets == NULL)
3647 abort ();
3649 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3650 off = local_opd_offsets[r_symndx];
3652 /* The last bit records whether we've already initialised
3653 this local .opd entry. */
3654 if ((off & 1) != 0)
3656 BFD_ASSERT (off != (bfd_vma) -1);
3657 off &= ~1;
3659 else
3661 /* The first two words of an .opd entry are zero. */
3662 memset (hppa_info->opd_sec->contents + off, 0, 16);
3664 /* The next word is the address of the function. */
3665 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3666 (hppa_info->opd_sec->contents + off + 16));
3668 /* The last word is our local __gp value. */
3669 value = _bfd_get_gp_value (info->output_bfd);
3670 bfd_put_64 (hppa_info->opd_sec->owner, value,
3671 hppa_info->opd_sec->contents + off + 24);
3674 else
3675 off = hh->opd_offset;
3677 if (hh == NULL || hh->want_opd)
3678 /* We want the value of the OPD offset for this symbol. */
3679 value = (off
3680 + hppa_info->opd_sec->output_offset
3681 + hppa_info->opd_sec->output_section->vma);
3682 else
3683 /* We want the address of the symbol. */
3684 value += addend;
3686 bfd_put_64 (input_bfd, value, hit_data);
3687 return bfd_reloc_ok;
3690 case R_PARISC_SECREL32:
3691 if (sym_sec && sym_sec->output_section)
3692 value -= sym_sec->output_section->vma;
3693 bfd_put_32 (input_bfd, value + addend, hit_data);
3694 return bfd_reloc_ok;
3696 case R_PARISC_SEGREL32:
3697 case R_PARISC_SEGREL64:
3699 /* If this is the first SEGREL relocation, then initialize
3700 the segment base values. */
3701 if (hppa_info->text_segment_base == (bfd_vma) -1)
3702 bfd_map_over_sections (output_bfd, elf_hppa_record_segment_addrs,
3703 hppa_info);
3705 /* VALUE holds the absolute address. We want to include the
3706 addend, then turn it into a segment relative address.
3708 The segment is derived from SYM_SEC. We assume that there are
3709 only two segments of note in the resulting executable/shlib.
3710 A readonly segment (.text) and a readwrite segment (.data). */
3711 value += addend;
3713 if (sym_sec->flags & SEC_CODE)
3714 value -= hppa_info->text_segment_base;
3715 else
3716 value -= hppa_info->data_segment_base;
3718 if (r_type == R_PARISC_SEGREL32)
3719 bfd_put_32 (input_bfd, value, hit_data);
3720 else
3721 bfd_put_64 (input_bfd, value, hit_data);
3722 return bfd_reloc_ok;
3725 /* Something we don't know how to handle. */
3726 default:
3727 return bfd_reloc_notsupported;
3730 /* Update the instruction word. */
3731 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3732 return bfd_reloc_ok;
3735 /* Relocate an HPPA ELF section. */
3737 static int
3738 elf64_hppa_relocate_section (bfd *output_bfd,
3739 struct bfd_link_info *info,
3740 bfd *input_bfd,
3741 asection *input_section,
3742 bfd_byte *contents,
3743 Elf_Internal_Rela *relocs,
3744 Elf_Internal_Sym *local_syms,
3745 asection **local_sections)
3747 Elf_Internal_Shdr *symtab_hdr;
3748 Elf_Internal_Rela *rel;
3749 Elf_Internal_Rela *relend;
3750 struct elf64_hppa_link_hash_table *hppa_info;
3752 hppa_info = hppa_link_hash_table (info);
3753 if (hppa_info == NULL)
3754 return false;
3756 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3758 rel = relocs;
3759 relend = relocs + input_section->reloc_count;
3760 for (; rel < relend; rel++)
3762 int r_type;
3763 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3764 unsigned long r_symndx;
3765 struct elf_link_hash_entry *eh;
3766 Elf_Internal_Sym *sym;
3767 asection *sym_sec;
3768 bfd_vma relocation;
3769 bfd_reloc_status_type r;
3771 r_type = ELF_R_TYPE (rel->r_info);
3772 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
3774 bfd_set_error (bfd_error_bad_value);
3775 return false;
3777 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3778 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3779 continue;
3781 /* This is a final link. */
3782 r_symndx = ELF_R_SYM (rel->r_info);
3783 eh = NULL;
3784 sym = NULL;
3785 sym_sec = NULL;
3786 if (r_symndx < symtab_hdr->sh_info)
3788 /* This is a local symbol, hh defaults to NULL. */
3789 sym = local_syms + r_symndx;
3790 sym_sec = local_sections[r_symndx];
3791 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3793 else
3795 /* This is not a local symbol. */
3796 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3798 /* It seems this can happen with erroneous or unsupported
3799 input (mixing a.out and elf in an archive, for example.) */
3800 if (sym_hashes == NULL)
3801 return false;
3803 eh = sym_hashes[r_symndx - symtab_hdr->sh_info];
3805 if (info->wrap_hash != NULL
3806 && (input_section->flags & SEC_DEBUGGING) != 0)
3807 eh = ((struct elf_link_hash_entry *)
3808 unwrap_hash_lookup (info, input_bfd, &eh->root));
3810 while (eh->root.type == bfd_link_hash_indirect
3811 || eh->root.type == bfd_link_hash_warning)
3812 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3814 relocation = 0;
3815 if (eh->root.type == bfd_link_hash_defined
3816 || eh->root.type == bfd_link_hash_defweak)
3818 sym_sec = eh->root.u.def.section;
3819 if (sym_sec != NULL
3820 && sym_sec->output_section != NULL)
3821 relocation = (eh->root.u.def.value
3822 + sym_sec->output_section->vma
3823 + sym_sec->output_offset);
3825 else if (eh->root.type == bfd_link_hash_undefweak)
3827 else if (info->unresolved_syms_in_objects == RM_IGNORE
3828 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
3830 else if (!bfd_link_relocatable (info)
3831 && elf_hppa_is_dynamic_loader_symbol (eh->root.root.string))
3832 continue;
3833 else if (!bfd_link_relocatable (info))
3835 bool err;
3837 err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
3838 && !info->warn_unresolved_syms)
3839 || ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT;
3841 info->callbacks->undefined_symbol
3842 (info, eh->root.root.string, input_bfd,
3843 input_section, rel->r_offset, err);
3846 if (!bfd_link_relocatable (info)
3847 && relocation == 0
3848 && eh->root.type != bfd_link_hash_defined
3849 && eh->root.type != bfd_link_hash_defweak
3850 && eh->root.type != bfd_link_hash_undefweak)
3852 if (info->unresolved_syms_in_objects == RM_IGNORE
3853 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3854 && eh->type == STT_PARISC_MILLI)
3855 info->callbacks->undefined_symbol
3856 (info, eh_name (eh), input_bfd,
3857 input_section, rel->r_offset, false);
3861 if (sym_sec != NULL && discarded_section (sym_sec))
3862 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3863 rel, 1, relend, howto, 0, contents);
3865 if (bfd_link_relocatable (info))
3866 continue;
3868 r = elf_hppa_final_link_relocate (rel, input_bfd, output_bfd,
3869 input_section, contents,
3870 relocation, info, sym_sec,
3871 eh);
3873 if (r != bfd_reloc_ok)
3875 switch (r)
3877 default:
3878 abort ();
3879 case bfd_reloc_overflow:
3881 const char *sym_name;
3883 if (eh != NULL)
3884 sym_name = NULL;
3885 else
3887 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3888 symtab_hdr->sh_link,
3889 sym->st_name);
3890 if (sym_name == NULL)
3891 return false;
3892 if (*sym_name == '\0')
3893 sym_name = bfd_section_name (sym_sec);
3896 (*info->callbacks->reloc_overflow)
3897 (info, (eh ? &eh->root : NULL), sym_name, howto->name,
3898 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3900 break;
3904 return true;
3907 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
3909 { STRING_COMMA_LEN (".tbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
3910 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3911 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3912 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3913 { STRING_COMMA_LEN (".dlt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3914 { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3915 { STRING_COMMA_LEN (".sbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3916 { NULL, 0, 0, 0, 0 }
3919 /* The hash bucket size is the standard one, namely 4. */
3921 const struct elf_size_info hppa64_elf_size_info =
3923 sizeof (Elf64_External_Ehdr),
3924 sizeof (Elf64_External_Phdr),
3925 sizeof (Elf64_External_Shdr),
3926 sizeof (Elf64_External_Rel),
3927 sizeof (Elf64_External_Rela),
3928 sizeof (Elf64_External_Sym),
3929 sizeof (Elf64_External_Dyn),
3930 sizeof (Elf_External_Note),
3933 64, 3,
3934 ELFCLASS64, EV_CURRENT,
3935 bfd_elf64_write_out_phdrs,
3936 bfd_elf64_write_shdrs_and_ehdr,
3937 bfd_elf64_checksum_contents,
3938 bfd_elf64_write_relocs,
3939 bfd_elf64_swap_symbol_in,
3940 bfd_elf64_swap_symbol_out,
3941 bfd_elf64_slurp_reloc_table,
3942 bfd_elf64_slurp_symbol_table,
3943 bfd_elf64_swap_dyn_in,
3944 bfd_elf64_swap_dyn_out,
3945 bfd_elf64_swap_reloc_in,
3946 bfd_elf64_swap_reloc_out,
3947 bfd_elf64_swap_reloca_in,
3948 bfd_elf64_swap_reloca_out
3951 #define TARGET_BIG_SYM hppa_elf64_vec
3952 #define TARGET_BIG_NAME "elf64-hppa"
3953 #define ELF_ARCH bfd_arch_hppa
3954 #define ELF_TARGET_ID HPPA64_ELF_DATA
3955 #define ELF_MACHINE_CODE EM_PARISC
3956 /* This is not strictly correct. The maximum page size for PA2.0 is
3957 64M. But everything still uses 4k. */
3958 #define ELF_MAXPAGESIZE 0x1000
3959 #define ELF_OSABI ELFOSABI_HPUX
3961 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
3962 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
3963 #define bfd_elf64_bfd_is_local_label_name elf_hppa_is_local_label_name
3964 #define elf_info_to_howto elf_hppa_info_to_howto
3965 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
3967 #define elf_backend_section_from_shdr elf64_hppa_section_from_shdr
3968 #define elf_backend_object_p elf64_hppa_object_p
3969 #define elf_backend_final_write_processing \
3970 elf_hppa_final_write_processing
3971 #define elf_backend_fake_sections elf_hppa_fake_sections
3972 #define elf_backend_add_symbol_hook elf_hppa_add_symbol_hook
3974 #define elf_backend_relocate_section elf_hppa_relocate_section
3976 #define bfd_elf64_bfd_final_link elf_hppa_final_link
3978 #define elf_backend_create_dynamic_sections \
3979 elf64_hppa_create_dynamic_sections
3980 #define elf_backend_init_file_header elf64_hppa_init_file_header
3982 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
3984 #define elf_backend_adjust_dynamic_symbol \
3985 elf64_hppa_adjust_dynamic_symbol
3987 #define elf_backend_size_dynamic_sections \
3988 elf64_hppa_size_dynamic_sections
3990 #define elf_backend_finish_dynamic_symbol \
3991 elf64_hppa_finish_dynamic_symbol
3992 #define elf_backend_finish_dynamic_sections \
3993 elf64_hppa_finish_dynamic_sections
3994 #define elf_backend_grok_prstatus elf64_hppa_grok_prstatus
3995 #define elf_backend_grok_psinfo elf64_hppa_grok_psinfo
3997 /* Stuff for the BFD linker: */
3998 #define bfd_elf64_bfd_link_hash_table_create \
3999 elf64_hppa_hash_table_create
4001 #define elf_backend_check_relocs \
4002 elf64_hppa_check_relocs
4004 #define elf_backend_size_info \
4005 hppa64_elf_size_info
4007 #define elf_backend_additional_program_headers \
4008 elf64_hppa_additional_program_headers
4010 #define elf_backend_modify_segment_map \
4011 elf64_hppa_modify_segment_map
4013 #define elf_backend_allow_non_load_phdr \
4014 elf64_hppa_allow_non_load_phdr
4016 #define elf_backend_link_output_symbol_hook \
4017 elf64_hppa_link_output_symbol_hook
4019 #define elf_backend_want_got_plt 0
4020 #define elf_backend_plt_readonly 0
4021 #define elf_backend_want_plt_sym 0
4022 #define elf_backend_got_header_size 0
4023 #define elf_backend_type_change_ok true
4024 #define elf_backend_get_symbol_type elf64_hppa_elf_get_symbol_type
4025 #define elf_backend_reloc_type_class elf64_hppa_reloc_type_class
4026 #define elf_backend_rela_normal 1
4027 #define elf_backend_special_sections elf64_hppa_special_sections
4028 #define elf_backend_action_discarded elf_hppa_action_discarded
4029 #define elf_backend_section_from_phdr elf64_hppa_section_from_phdr
4031 #define elf64_bed elf64_hppa_hpux_bed
4033 #include "elf64-target.h"
4035 #undef TARGET_BIG_SYM
4036 #define TARGET_BIG_SYM hppa_elf64_linux_vec
4037 #undef TARGET_BIG_NAME
4038 #define TARGET_BIG_NAME "elf64-hppa-linux"
4039 #undef ELF_OSABI
4040 #define ELF_OSABI ELFOSABI_GNU
4041 #undef elf64_bed
4042 #define elf64_bed elf64_hppa_linux_bed
4043 #undef elf_backend_special_sections
4044 #define elf_backend_special_sections (elf64_hppa_special_sections + 1)
4046 #include "elf64-target.h"