PR 32603, ld -w misbehaviour
[binutils-gdb.git] / bfd / elf64-hppa.c
blob3f5a3fea4abf43ee93571ac54322cae0ec9aefb0
1 /* Support for HPPA 64-bit ELF
2 Copyright (C) 1999-2025 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 /* Target vectors for HPUX and non-HPUX versions of HPPA ELF binaries. */
31 extern const bfd_target hppa_elf64_vec;
32 extern const bfd_target hppa_elf64_linux_vec;
34 #define ARCH_SIZE 64
36 #define PLT_ENTRY_SIZE 0x10
37 #define DLT_ENTRY_SIZE 0x8
38 #define OPD_ENTRY_SIZE 0x20
40 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
42 /* The stub is supposed to load the target address and target's DP
43 value out of the PLT, then do an external branch to the target
44 address.
46 LDD PLTOFF(%r27),%r1
47 BVE (%r1)
48 LDD PLTOFF+8(%r27),%r27
50 Note that we must use the LDD with a 14 bit displacement, not the one
51 with a 5 bit displacement. */
52 static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
53 0x53, 0x7b, 0x00, 0x00 };
55 struct elf64_hppa_link_hash_entry
57 struct elf_link_hash_entry eh;
59 /* Offsets for this symbol in various linker sections. */
60 bfd_vma dlt_offset;
61 bfd_vma plt_offset;
62 bfd_vma opd_offset;
63 bfd_vma stub_offset;
65 /* The index of the (possibly local) symbol in the input bfd and its
66 associated BFD. Needed so that we can have relocs against local
67 symbols in shared libraries. */
68 long sym_indx;
69 bfd *owner;
71 /* Dynamic symbols may need to have two different values. One for
72 the dynamic symbol table, one for the normal symbol table.
74 In such cases we store the symbol's real value and section
75 index here so we can restore the real value before we write
76 the normal symbol table. */
77 bfd_vma st_value;
78 int st_shndx;
80 /* Used to count non-got, non-plt relocations for delayed sizing
81 of relocation sections. */
82 struct elf64_hppa_dyn_reloc_entry
84 /* Next relocation in the chain. */
85 struct elf64_hppa_dyn_reloc_entry *next;
87 /* The type of the relocation. */
88 int type;
90 /* The input section of the relocation. */
91 asection *sec;
93 /* Number of relocs copied in this section. */
94 bfd_size_type count;
96 /* The index of the section symbol for the input section of
97 the relocation. Only needed when building shared libraries. */
98 int sec_symndx;
100 /* The offset within the input section of the relocation. */
101 bfd_vma offset;
103 /* The addend for the relocation. */
104 bfd_vma addend;
106 } *reloc_entries;
108 /* Nonzero if this symbol needs an entry in one of the linker
109 sections. */
110 unsigned want_dlt;
111 unsigned want_plt;
112 unsigned want_opd;
113 unsigned want_stub;
116 struct elf64_hppa_link_hash_table
118 struct elf_link_hash_table root;
120 /* Shortcuts to get to the various linker defined sections. */
121 asection *dlt_sec;
122 asection *dlt_rel_sec;
123 asection *opd_sec;
124 asection *opd_rel_sec;
125 asection *other_rel_sec;
127 /* Offset of __gp within .plt section. When the PLT gets large we want
128 to slide __gp into the PLT section so that we can continue to use
129 single DP relative instructions to load values out of the PLT. */
130 bfd_vma gp_offset;
132 /* Note this is not strictly correct. We should create a stub section for
133 each input section with calls. The stub section should be placed before
134 the section with the call. */
135 asection *stub_sec;
137 bfd_vma text_segment_base;
138 bfd_vma data_segment_base;
140 /* We build tables to map from an input section back to its
141 symbol index. This is the BFD for which we currently have
142 a map. */
143 bfd *section_syms_bfd;
145 /* Array of symbol numbers for each input section attached to the
146 current BFD. */
147 int *section_syms;
150 #define hppa_link_hash_table(p) \
151 ((is_elf_hash_table ((p)->hash) \
152 && elf_hash_table_id (elf_hash_table (p)) == HPPA64_ELF_DATA) \
153 ? (struct elf64_hppa_link_hash_table *) (p)->hash : NULL)
155 #define hppa_elf_hash_entry(ent) \
156 ((struct elf64_hppa_link_hash_entry *)(ent))
158 #define eh_name(eh) \
159 (eh ? eh->root.root.string : "<undef>")
161 typedef struct bfd_hash_entry *(*new_hash_entry_func)
162 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
164 static struct bfd_link_hash_table *elf64_hppa_hash_table_create
165 (bfd *abfd);
167 /* This must follow the definitions of the various derived linker
168 hash tables and shared functions. */
169 #include "elf-hppa.h"
171 static bool elf64_hppa_object_p
172 (bfd *);
174 static bool elf64_hppa_create_dynamic_sections
175 (bfd *, struct bfd_link_info *);
177 static bool elf64_hppa_adjust_dynamic_symbol
178 (struct bfd_link_info *, struct elf_link_hash_entry *);
180 static bool elf64_hppa_mark_milli_and_exported_functions
181 (struct elf_link_hash_entry *, void *);
183 static int elf64_hppa_link_output_symbol_hook
184 (struct bfd_link_info *, const char *, Elf_Internal_Sym *,
185 asection *, struct elf_link_hash_entry *);
187 static bool elf64_hppa_finish_dynamic_symbol
188 (bfd *, struct bfd_link_info *,
189 struct elf_link_hash_entry *, Elf_Internal_Sym *);
191 static bool elf64_hppa_finish_dynamic_sections
192 (bfd *, struct bfd_link_info *);
194 static bool elf64_hppa_check_relocs
195 (bfd *, struct bfd_link_info *,
196 asection *, const Elf_Internal_Rela *);
198 static bool elf64_hppa_dynamic_symbol_p
199 (struct elf_link_hash_entry *, struct bfd_link_info *);
201 static bool elf64_hppa_mark_exported_functions
202 (struct elf_link_hash_entry *, void *);
204 static bool elf64_hppa_finalize_opd
205 (struct elf_link_hash_entry *, void *);
207 static bool elf64_hppa_finalize_dlt
208 (struct elf_link_hash_entry *, void *);
210 static bool allocate_global_data_dlt
211 (struct elf_link_hash_entry *, void *);
213 static bool allocate_global_data_plt
214 (struct elf_link_hash_entry *, void *);
216 static bool allocate_global_data_stub
217 (struct elf_link_hash_entry *, void *);
219 static bool allocate_global_data_opd
220 (struct elf_link_hash_entry *, void *);
222 static bool get_reloc_section
223 (bfd *, struct elf64_hppa_link_hash_table *, asection *);
225 static bool count_dyn_reloc
226 (bfd *, struct elf64_hppa_link_hash_entry *,
227 int, asection *, int, bfd_vma, bfd_vma);
229 static bool allocate_dynrel_entries
230 (struct elf_link_hash_entry *, void *);
232 static bool elf64_hppa_finalize_dynreloc
233 (struct elf_link_hash_entry *, void *);
235 static bool get_opd
236 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
238 static bool get_plt
239 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
241 static bool get_dlt
242 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
244 static bool get_stub
245 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
247 static int elf64_hppa_elf_get_symbol_type
248 (Elf_Internal_Sym *, int);
250 /* Initialize an entry in the link hash table. */
252 static struct bfd_hash_entry *
253 hppa64_link_hash_newfunc (struct bfd_hash_entry *entry,
254 struct bfd_hash_table *table,
255 const char *string)
257 /* Allocate the structure if it has not already been allocated by a
258 subclass. */
259 if (entry == NULL)
261 entry = bfd_hash_allocate (table,
262 sizeof (struct elf64_hppa_link_hash_entry));
263 if (entry == NULL)
264 return entry;
267 /* Call the allocation method of the superclass. */
268 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
269 if (entry != NULL)
271 struct elf64_hppa_link_hash_entry *hh;
273 /* Initialize our local data. All zeros. */
274 hh = hppa_elf_hash_entry (entry);
275 memset (&hh->dlt_offset, 0,
276 (sizeof (struct elf64_hppa_link_hash_entry)
277 - offsetof (struct elf64_hppa_link_hash_entry, dlt_offset)));
280 return entry;
283 /* Create the derived linker hash table. The PA64 ELF port uses this
284 derived hash table to keep information specific to the PA ElF
285 linker (without using static variables). */
287 static struct bfd_link_hash_table*
288 elf64_hppa_hash_table_create (bfd *abfd)
290 struct elf64_hppa_link_hash_table *htab;
291 size_t amt = sizeof (*htab);
293 htab = bfd_zmalloc (amt);
294 if (htab == NULL)
295 return NULL;
297 if (!_bfd_elf_link_hash_table_init (&htab->root, abfd,
298 hppa64_link_hash_newfunc,
299 sizeof (struct elf64_hppa_link_hash_entry)))
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 (abfd->xvec == & hppa_elf64_linux_vec)
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_late_size_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 if (dynobj == NULL)
1538 return true;
1540 /* Mark each function this program exports so that we will allocate
1541 space in the .opd section for each function's FPTR. If we are
1542 creating dynamic sections, change the dynamic index of millicode
1543 symbols to -1 and remove them from the string table for .dynstr.
1545 We have to traverse the main linker hash table since we have to
1546 find functions which may not have been mentioned in any relocs. */
1547 elf_link_hash_traverse (&hppa_info->root,
1548 (hppa_info->root.dynamic_sections_created
1549 ? elf64_hppa_mark_milli_and_exported_functions
1550 : elf64_hppa_mark_exported_functions),
1551 info);
1553 if (hppa_info->root.dynamic_sections_created)
1555 /* Set the contents of the .interp section to the interpreter. */
1556 if (bfd_link_executable (info) && !info->nointerp)
1558 sec = bfd_get_linker_section (dynobj, ".interp");
1559 BFD_ASSERT (sec != NULL);
1560 sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
1561 sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1562 sec->alloced = 1;
1565 else
1567 /* We may have created entries in the .rela.got section.
1568 However, if we are not creating the dynamic sections, we will
1569 not actually use these entries. Reset the size of .rela.dlt,
1570 which will cause it to get stripped from the output file
1571 below. */
1572 sec = hppa_info->dlt_rel_sec;
1573 if (sec != NULL)
1574 sec->size = 0;
1577 /* Set up DLT, PLT and OPD offsets for local syms, and space for local
1578 dynamic relocs. */
1579 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1581 bfd_signed_vma *local_dlt;
1582 bfd_signed_vma *end_local_dlt;
1583 bfd_signed_vma *local_plt;
1584 bfd_signed_vma *end_local_plt;
1585 bfd_signed_vma *local_opd;
1586 bfd_signed_vma *end_local_opd;
1587 bfd_size_type locsymcount;
1588 Elf_Internal_Shdr *symtab_hdr;
1589 asection *srel;
1591 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1592 continue;
1594 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
1596 struct elf64_hppa_dyn_reloc_entry *hdh_p;
1598 for (hdh_p = ((struct elf64_hppa_dyn_reloc_entry *)
1599 elf_section_data (sec)->local_dynrel);
1600 hdh_p != NULL;
1601 hdh_p = hdh_p->next)
1603 if (!bfd_is_abs_section (hdh_p->sec)
1604 && bfd_is_abs_section (hdh_p->sec->output_section))
1606 /* Input section has been discarded, either because
1607 it is a copy of a linkonce section or due to
1608 linker script /DISCARD/, so we'll be discarding
1609 the relocs too. */
1611 else if (hdh_p->count != 0)
1613 srel = elf_section_data (hdh_p->sec)->sreloc;
1614 srel->size += hdh_p->count * sizeof (Elf64_External_Rela);
1615 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
1616 info->flags |= DF_TEXTREL;
1621 local_dlt = elf_local_got_refcounts (ibfd);
1622 if (!local_dlt)
1623 continue;
1625 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1626 locsymcount = symtab_hdr->sh_info;
1627 end_local_dlt = local_dlt + locsymcount;
1628 sec = hppa_info->dlt_sec;
1629 srel = hppa_info->dlt_rel_sec;
1630 for (; local_dlt < end_local_dlt; ++local_dlt)
1632 if (*local_dlt > 0)
1634 *local_dlt = sec->size;
1635 sec->size += DLT_ENTRY_SIZE;
1636 if (bfd_link_pic (info))
1638 srel->size += sizeof (Elf64_External_Rela);
1641 else
1642 *local_dlt = (bfd_vma) -1;
1645 local_plt = end_local_dlt;
1646 end_local_plt = local_plt + locsymcount;
1647 if (! hppa_info->root.dynamic_sections_created)
1649 /* Won't be used, but be safe. */
1650 for (; local_plt < end_local_plt; ++local_plt)
1651 *local_plt = (bfd_vma) -1;
1653 else
1655 sec = hppa_info->root.splt;
1656 srel = hppa_info->root.srelplt;
1657 for (; local_plt < end_local_plt; ++local_plt)
1659 if (*local_plt > 0)
1661 *local_plt = sec->size;
1662 sec->size += PLT_ENTRY_SIZE;
1663 if (bfd_link_pic (info))
1664 srel->size += sizeof (Elf64_External_Rela);
1666 else
1667 *local_plt = (bfd_vma) -1;
1671 local_opd = end_local_plt;
1672 end_local_opd = local_opd + locsymcount;
1673 if (! hppa_info->root.dynamic_sections_created)
1675 /* Won't be used, but be safe. */
1676 for (; local_opd < end_local_opd; ++local_opd)
1677 *local_opd = (bfd_vma) -1;
1679 else
1681 sec = hppa_info->opd_sec;
1682 srel = hppa_info->opd_rel_sec;
1683 for (; local_opd < end_local_opd; ++local_opd)
1685 if (*local_opd > 0)
1687 *local_opd = sec->size;
1688 sec->size += OPD_ENTRY_SIZE;
1689 if (bfd_link_pic (info))
1690 srel->size += sizeof (Elf64_External_Rela);
1692 else
1693 *local_opd = (bfd_vma) -1;
1698 /* Allocate the GOT entries. */
1700 data.info = info;
1701 if (hppa_info->dlt_sec)
1703 data.ofs = hppa_info->dlt_sec->size;
1704 elf_link_hash_traverse (&hppa_info->root,
1705 allocate_global_data_dlt, &data);
1706 hppa_info->dlt_sec->size = data.ofs;
1709 if (hppa_info->root.splt)
1711 data.ofs = hppa_info->root.splt->size;
1712 elf_link_hash_traverse (&hppa_info->root,
1713 allocate_global_data_plt, &data);
1714 hppa_info->root.splt->size = data.ofs;
1717 if (hppa_info->stub_sec)
1719 data.ofs = 0x0;
1720 elf_link_hash_traverse (&hppa_info->root,
1721 allocate_global_data_stub, &data);
1722 hppa_info->stub_sec->size = data.ofs;
1725 /* Allocate space for entries in the .opd section. */
1726 if (hppa_info->opd_sec)
1728 data.ofs = hppa_info->opd_sec->size;
1729 elf_link_hash_traverse (&hppa_info->root,
1730 allocate_global_data_opd, &data);
1731 hppa_info->opd_sec->size = data.ofs;
1734 /* Now allocate space for dynamic relocations, if necessary. */
1735 if (hppa_info->root.dynamic_sections_created)
1736 elf_link_hash_traverse (&hppa_info->root,
1737 allocate_dynrel_entries, &data);
1739 /* The sizes of all the sections are set. Allocate memory for them. */
1740 relocs = false;
1741 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
1743 const char *name;
1745 if ((sec->flags & SEC_LINKER_CREATED) == 0)
1746 continue;
1748 /* It's OK to base decisions on the section name, because none
1749 of the dynobj section names depend upon the input files. */
1750 name = bfd_section_name (sec);
1752 if (strcmp (name, ".plt") == 0)
1754 /* Remember whether there is a PLT. */
1757 else if (strcmp (name, ".opd") == 0
1758 || startswith (name, ".dlt")
1759 || strcmp (name, ".stub") == 0
1760 || strcmp (name, ".got") == 0)
1762 /* Strip this section if we don't need it; see the comment below. */
1764 else if (startswith (name, ".rela"))
1766 if (sec->size != 0)
1768 /* Remember whether there are any reloc sections other
1769 than .rela.plt. */
1770 if (strcmp (name, ".rela.plt") != 0)
1771 relocs = true;
1773 /* We use the reloc_count field as a counter if we need
1774 to copy relocs into the output file. */
1775 sec->reloc_count = 0;
1778 else
1780 /* It's not one of our sections, so don't allocate space. */
1781 continue;
1784 if (sec->size == 0)
1786 /* If we don't need this section, strip it from the
1787 output file. This is mostly to handle .rela.bss and
1788 .rela.plt. We must create both sections in
1789 create_dynamic_sections, because they must be created
1790 before the linker maps input sections to output
1791 sections. The linker does that before
1792 adjust_dynamic_symbol is called, and it is that
1793 function which decides whether anything needs to go
1794 into these sections. */
1795 sec->flags |= SEC_EXCLUDE;
1796 continue;
1799 if ((sec->flags & SEC_HAS_CONTENTS) == 0)
1800 continue;
1802 /* Allocate memory for the section contents if it has not
1803 been allocated already. We use bfd_zalloc here in case
1804 unused entries are not reclaimed before the section's
1805 contents are written out. This should not happen, but this
1806 way if it does, we get a R_PARISC_NONE reloc instead of
1807 garbage. */
1808 if (sec->contents == NULL)
1810 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
1811 if (sec->contents == NULL)
1812 return false;
1813 sec->alloced = 1;
1817 if (hppa_info->root.dynamic_sections_created)
1819 /* Always create a DT_PLTGOT. It actually has nothing to do with
1820 the PLT, it is how we communicate the __gp value of a load
1821 module to the dynamic linker. */
1822 #define add_dynamic_entry(TAG, VAL) \
1823 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1825 if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0))
1826 return false;
1828 /* Add some entries to the .dynamic section. We fill in the
1829 values later, in elf64_hppa_finish_dynamic_sections, but we
1830 must add the entries now so that we get the correct size for
1831 the .dynamic section. The DT_DEBUG entry is filled in by the
1832 dynamic linker and used by the debugger. */
1833 if (! bfd_link_pic (info))
1835 if (!add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1836 || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
1837 return false;
1840 /* Force DT_FLAGS to always be set.
1841 Required by HPUX 11.00 patch PHSS_26559.
1842 PR 30743: But do not set them for non-HPUX targets. */
1843 if (output_bfd->xvec == & hppa_elf64_vec)
1845 if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1846 return false;
1849 #undef add_dynamic_entry
1851 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
1854 /* Called after we have output the symbol into the dynamic symbol
1855 table, but before we output the symbol into the normal symbol
1856 table.
1858 For some symbols we had to change their address when outputting
1859 the dynamic symbol table. We undo that change here so that
1860 the symbols have their expected value in the normal symbol
1861 table. Ick. */
1863 static int
1864 elf64_hppa_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1865 const char *name,
1866 Elf_Internal_Sym *sym,
1867 asection *input_sec ATTRIBUTE_UNUSED,
1868 struct elf_link_hash_entry *eh)
1870 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1872 /* We may be called with the file symbol or section symbols.
1873 They never need munging, so it is safe to ignore them. */
1874 if (!name || !eh)
1875 return 1;
1877 /* Function symbols for which we created .opd entries *may* have been
1878 munged by finish_dynamic_symbol and have to be un-munged here.
1880 Note that finish_dynamic_symbol sometimes turns dynamic symbols
1881 into non-dynamic ones, so we initialize st_shndx to -1 in
1882 mark_exported_functions and check to see if it was overwritten
1883 here instead of just checking eh->dynindx. */
1884 if (hh->want_opd && hh->st_shndx != -1)
1886 /* Restore the saved value and section index. */
1887 sym->st_value = hh->st_value;
1888 sym->st_shndx = hh->st_shndx;
1891 return 1;
1894 /* Finish up dynamic symbol handling. We set the contents of various
1895 dynamic sections here. */
1897 static bool
1898 elf64_hppa_finish_dynamic_symbol (bfd *output_bfd,
1899 struct bfd_link_info *info,
1900 struct elf_link_hash_entry *eh,
1901 Elf_Internal_Sym *sym)
1903 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1904 asection *stub, *splt, *sopd, *spltrel;
1905 struct elf64_hppa_link_hash_table *hppa_info;
1907 hppa_info = hppa_link_hash_table (info);
1909 stub = hppa_info->stub_sec;
1910 splt = hppa_info->root.splt;
1911 sopd = hppa_info->opd_sec;
1912 spltrel = hppa_info->root.srelplt;
1914 /* Incredible. It is actually necessary to NOT use the symbol's real
1915 value when building the dynamic symbol table for a shared library.
1916 At least for symbols that refer to functions.
1918 We will store a new value and section index into the symbol long
1919 enough to output it into the dynamic symbol table, then we restore
1920 the original values (in elf64_hppa_link_output_symbol_hook). */
1921 if (hh->want_opd)
1923 BFD_ASSERT (sopd != NULL);
1925 /* Save away the original value and section index so that we
1926 can restore them later. */
1927 hh->st_value = sym->st_value;
1928 hh->st_shndx = sym->st_shndx;
1930 /* For the dynamic symbol table entry, we want the value to be
1931 address of this symbol's entry within the .opd section. */
1932 sym->st_value = (hh->opd_offset
1933 + sopd->output_offset
1934 + sopd->output_section->vma);
1935 sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1936 sopd->output_section);
1939 /* Initialize a .plt entry if requested. */
1940 if (hh->want_plt
1941 && elf64_hppa_dynamic_symbol_p (eh, info))
1943 bfd_vma value;
1944 Elf_Internal_Rela rel;
1945 bfd_byte *loc;
1947 BFD_ASSERT (splt != NULL && spltrel != NULL);
1949 /* We do not actually care about the value in the PLT entry
1950 if we are creating a shared library and the symbol is
1951 still undefined, we create a dynamic relocation to fill
1952 in the correct value. */
1953 if (bfd_link_pic (info) && eh->root.type == bfd_link_hash_undefined)
1954 value = 0;
1955 else
1956 value = (eh->root.u.def.value + eh->root.u.def.section->vma);
1958 /* Fill in the entry in the procedure linkage table.
1960 The format of a plt entry is
1961 <funcaddr> <__gp>.
1963 plt_offset is the offset within the PLT section at which to
1964 install the PLT entry.
1966 We are modifying the in-memory PLT contents here, so we do not add
1967 in the output_offset of the PLT section. */
1969 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset);
1970 value = _bfd_get_gp_value (info->output_bfd);
1971 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset + 0x8);
1973 /* Create a dynamic IPLT relocation for this entry.
1975 We are creating a relocation in the output file's PLT section,
1976 which is included within the DLT secton. So we do need to include
1977 the PLT's output_offset in the computation of the relocation's
1978 address. */
1979 rel.r_offset = (hh->plt_offset + splt->output_offset
1980 + splt->output_section->vma);
1981 rel.r_info = ELF64_R_INFO (hh->eh.dynindx, R_PARISC_IPLT);
1982 rel.r_addend = 0;
1984 loc = spltrel->contents;
1985 loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
1986 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
1989 /* Initialize an external call stub entry if requested. */
1990 if (hh->want_stub
1991 && elf64_hppa_dynamic_symbol_p (eh, info))
1993 bfd_vma value;
1994 int insn;
1995 unsigned int max_offset;
1997 BFD_ASSERT (stub != NULL);
1999 /* Install the generic stub template.
2001 We are modifying the contents of the stub section, so we do not
2002 need to include the stub section's output_offset here. */
2003 memcpy (stub->contents + hh->stub_offset, plt_stub, sizeof (plt_stub));
2005 /* Fix up the first ldd instruction.
2007 We are modifying the contents of the STUB section in memory,
2008 so we do not need to include its output offset in this computation.
2010 Note the plt_offset value is the value of the PLT entry relative to
2011 the start of the PLT section. These instructions will reference
2012 data relative to the value of __gp, which may not necessarily have
2013 the same address as the start of the PLT section.
2015 gp_offset contains the offset of __gp within the PLT section. */
2016 value = hh->plt_offset - hppa_info->gp_offset;
2018 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset);
2019 if (output_bfd->arch_info->mach >= 25)
2021 /* Wide mode allows 16 bit offsets. */
2022 max_offset = 32768;
2023 insn &= ~ 0xfff1;
2024 insn |= re_assemble_16 ((int) value);
2026 else
2028 max_offset = 8192;
2029 insn &= ~ 0x3ff1;
2030 insn |= re_assemble_14 ((int) value);
2033 if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2035 _bfd_error_handler
2036 /* xgettext:c-format */
2037 (_("stub entry for %s cannot load .plt, dp offset = %" PRId64),
2038 hh->eh.root.root.string, (int64_t) value);
2039 return false;
2042 bfd_put_32 (stub->owner, (bfd_vma) insn,
2043 stub->contents + hh->stub_offset);
2045 /* Fix up the second ldd instruction. */
2046 value += 8;
2047 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset + 8);
2048 if (output_bfd->arch_info->mach >= 25)
2050 insn &= ~ 0xfff1;
2051 insn |= re_assemble_16 ((int) value);
2053 else
2055 insn &= ~ 0x3ff1;
2056 insn |= re_assemble_14 ((int) value);
2058 bfd_put_32 (stub->owner, (bfd_vma) insn,
2059 stub->contents + hh->stub_offset + 8);
2062 return true;
2065 /* The .opd section contains FPTRs for each function this file
2066 exports. Initialize the FPTR entries. */
2068 static bool
2069 elf64_hppa_finalize_opd (struct elf_link_hash_entry *eh, void *data)
2071 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2072 struct bfd_link_info *info = (struct bfd_link_info *)data;
2073 struct elf64_hppa_link_hash_table *hppa_info;
2074 asection *sopd;
2075 asection *sopdrel;
2077 hppa_info = hppa_link_hash_table (info);
2078 if (hppa_info == NULL)
2079 return false;
2081 sopd = hppa_info->opd_sec;
2082 sopdrel = hppa_info->opd_rel_sec;
2084 if (hh->want_opd)
2086 bfd_vma value;
2088 /* The first two words of an .opd entry are zero.
2090 We are modifying the contents of the OPD section in memory, so we
2091 do not need to include its output offset in this computation. */
2092 memset (sopd->contents + hh->opd_offset, 0, 16);
2094 value = (eh->root.u.def.value
2095 + eh->root.u.def.section->output_section->vma
2096 + eh->root.u.def.section->output_offset);
2098 /* The next word is the address of the function. */
2099 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 16);
2101 /* The last word is our local __gp value. */
2102 value = _bfd_get_gp_value (info->output_bfd);
2103 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 24);
2106 /* If we are generating a shared library, we must generate EPLT relocations
2107 for each entry in the .opd, even for static functions (they may have
2108 had their address taken). */
2109 if (bfd_link_pic (info) && hh->want_opd)
2111 Elf_Internal_Rela rel;
2112 bfd_byte *loc;
2113 int dynindx;
2115 /* We may need to do a relocation against a local symbol, in
2116 which case we have to look up it's dynamic symbol index off
2117 the local symbol hash table. */
2118 if (eh->dynindx != -1)
2119 dynindx = eh->dynindx;
2120 else
2121 dynindx
2122 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2123 hh->sym_indx);
2125 /* The offset of this relocation is the absolute address of the
2126 .opd entry for this symbol. */
2127 rel.r_offset = (hh->opd_offset + sopd->output_offset
2128 + sopd->output_section->vma);
2130 /* If H is non-null, then we have an external symbol.
2132 It is imperative that we use a different dynamic symbol for the
2133 EPLT relocation if the symbol has global scope.
2135 In the dynamic symbol table, the function symbol will have a value
2136 which is address of the function's .opd entry.
2138 Thus, we can not use that dynamic symbol for the EPLT relocation
2139 (if we did, the data in the .opd would reference itself rather
2140 than the actual address of the function). Instead we have to use
2141 a new dynamic symbol which has the same value as the original global
2142 function symbol.
2144 We prefix the original symbol with a "." and use the new symbol in
2145 the EPLT relocation. This new symbol has already been recorded in
2146 the symbol table, we just have to look it up and use it.
2148 We do not have such problems with static functions because we do
2149 not make their addresses in the dynamic symbol table point to
2150 the .opd entry. Ultimately this should be safe since a static
2151 function can not be directly referenced outside of its shared
2152 library.
2154 We do have to play similar games for FPTR relocations in shared
2155 libraries, including those for static symbols. See the FPTR
2156 handling in elf64_hppa_finalize_dynreloc. */
2157 if (eh)
2159 char *new_name;
2160 struct elf_link_hash_entry *nh;
2162 new_name = concat (".", eh->root.root.string, NULL);
2164 nh = elf_link_hash_lookup (elf_hash_table (info),
2165 new_name, true, true, false);
2167 /* All we really want from the new symbol is its dynamic
2168 symbol index. */
2169 if (nh)
2170 dynindx = nh->dynindx;
2171 free (new_name);
2174 rel.r_addend = 0;
2175 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2177 loc = sopdrel->contents;
2178 loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2179 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2181 return true;
2184 /* The .dlt section contains addresses for items referenced through the
2185 dlt. Note that we can have a DLTIND relocation for a local symbol, thus
2186 we can not depend on finish_dynamic_symbol to initialize the .dlt. */
2188 static bool
2189 elf64_hppa_finalize_dlt (struct elf_link_hash_entry *eh, void *data)
2191 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2192 struct bfd_link_info *info = (struct bfd_link_info *)data;
2193 struct elf64_hppa_link_hash_table *hppa_info;
2194 asection *sdlt, *sdltrel;
2196 hppa_info = hppa_link_hash_table (info);
2197 if (hppa_info == NULL)
2198 return false;
2200 sdlt = hppa_info->dlt_sec;
2201 sdltrel = hppa_info->dlt_rel_sec;
2203 /* H/DYN_H may refer to a local variable and we know it's
2204 address, so there is no need to create a relocation. Just install
2205 the proper value into the DLT, note this shortcut can not be
2206 skipped when building a shared library. */
2207 if (! bfd_link_pic (info) && hh && hh->want_dlt)
2209 bfd_vma value;
2211 /* If we had an LTOFF_FPTR style relocation we want the DLT entry
2212 to point to the FPTR entry in the .opd section.
2214 We include the OPD's output offset in this computation as
2215 we are referring to an absolute address in the resulting
2216 object file. */
2217 if (hh->want_opd)
2219 value = (hh->opd_offset
2220 + hppa_info->opd_sec->output_offset
2221 + hppa_info->opd_sec->output_section->vma);
2223 else if ((eh->root.type == bfd_link_hash_defined
2224 || eh->root.type == bfd_link_hash_defweak)
2225 && eh->root.u.def.section)
2227 value = eh->root.u.def.value + eh->root.u.def.section->output_offset;
2228 if (eh->root.u.def.section->output_section)
2229 value += eh->root.u.def.section->output_section->vma;
2230 else
2231 value += eh->root.u.def.section->vma;
2233 else
2234 /* We have an undefined function reference. */
2235 value = 0;
2237 /* We do not need to include the output offset of the DLT section
2238 here because we are modifying the in-memory contents. */
2239 bfd_put_64 (sdlt->owner, value, sdlt->contents + hh->dlt_offset);
2242 /* Create a relocation for the DLT entry associated with this symbol.
2243 When building a shared library the symbol does not have to be dynamic. */
2244 if (hh->want_dlt
2245 && (elf64_hppa_dynamic_symbol_p (eh, info) || bfd_link_pic (info)))
2247 Elf_Internal_Rela rel;
2248 bfd_byte *loc;
2249 int dynindx;
2251 /* We may need to do a relocation against a local symbol, in
2252 which case we have to look up it's dynamic symbol index off
2253 the local symbol hash table. */
2254 if (eh && eh->dynindx != -1)
2255 dynindx = eh->dynindx;
2256 else
2257 dynindx
2258 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2259 hh->sym_indx);
2261 /* Create a dynamic relocation for this entry. Do include the output
2262 offset of the DLT entry since we need an absolute address in the
2263 resulting object file. */
2264 rel.r_offset = (hh->dlt_offset + sdlt->output_offset
2265 + sdlt->output_section->vma);
2266 if (eh && eh->type == STT_FUNC)
2267 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2268 else
2269 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2270 rel.r_addend = 0;
2272 loc = sdltrel->contents;
2273 loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2274 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2276 return true;
2279 /* Finalize the dynamic relocations. Specifically the FPTR relocations
2280 for dynamic functions used to initialize static data. */
2282 static bool
2283 elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
2284 void *data)
2286 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2287 struct bfd_link_info *info = (struct bfd_link_info *)data;
2288 struct elf64_hppa_link_hash_table *hppa_info;
2289 int dynamic_symbol;
2291 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, info);
2293 if (!dynamic_symbol && !bfd_link_pic (info))
2294 return true;
2296 if (hh->reloc_entries)
2298 struct elf64_hppa_dyn_reloc_entry *rent;
2299 int dynindx;
2301 hppa_info = hppa_link_hash_table (info);
2302 if (hppa_info == NULL)
2303 return false;
2305 /* We may need to do a relocation against a local symbol, in
2306 which case we have to look up it's dynamic symbol index off
2307 the local symbol hash table. */
2308 if (eh->dynindx != -1)
2309 dynindx = eh->dynindx;
2310 else
2311 dynindx
2312 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2313 hh->sym_indx);
2315 for (rent = hh->reloc_entries; rent; rent = rent->next)
2317 Elf_Internal_Rela rel;
2318 bfd_byte *loc;
2320 /* Allocate one iff we are building a shared library, the relocation
2321 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
2322 if (!bfd_link_pic (info)
2323 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2324 continue;
2326 /* Create a dynamic relocation for this entry.
2328 We need the output offset for the reloc's section because
2329 we are creating an absolute address in the resulting object
2330 file. */
2331 rel.r_offset = (rent->offset + rent->sec->output_offset
2332 + rent->sec->output_section->vma);
2334 /* An FPTR64 relocation implies that we took the address of
2335 a function and that the function has an entry in the .opd
2336 section. We want the FPTR64 relocation to reference the
2337 entry in .opd.
2339 We could munge the symbol value in the dynamic symbol table
2340 (in fact we already do for functions with global scope) to point
2341 to the .opd entry. Then we could use that dynamic symbol in
2342 this relocation.
2344 Or we could do something sensible, not munge the symbol's
2345 address and instead just use a different symbol to reference
2346 the .opd entry. At least that seems sensible until you
2347 realize there's no local dynamic symbols we can use for that
2348 purpose. Thus the hair in the check_relocs routine.
2350 We use a section symbol recorded by check_relocs as the
2351 base symbol for the relocation. The addend is the difference
2352 between the section symbol and the address of the .opd entry. */
2353 if (bfd_link_pic (info)
2354 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2356 bfd_vma value, value2;
2358 /* First compute the address of the opd entry for this symbol. */
2359 value = (hh->opd_offset
2360 + hppa_info->opd_sec->output_section->vma
2361 + hppa_info->opd_sec->output_offset);
2363 /* Compute the value of the start of the section with
2364 the relocation. */
2365 value2 = (rent->sec->output_section->vma
2366 + rent->sec->output_offset);
2368 /* Compute the difference between the start of the section
2369 with the relocation and the opd entry. */
2370 value -= value2;
2372 /* The result becomes the addend of the relocation. */
2373 rel.r_addend = value;
2375 /* The section symbol becomes the symbol for the dynamic
2376 relocation. */
2377 dynindx
2378 = _bfd_elf_link_lookup_local_dynindx (info,
2379 rent->sec->owner,
2380 rent->sec_symndx);
2382 else
2383 rel.r_addend = rent->addend;
2385 rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2387 loc = hppa_info->other_rel_sec->contents;
2388 loc += (hppa_info->other_rel_sec->reloc_count++
2389 * sizeof (Elf64_External_Rela));
2390 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2394 return true;
2397 /* Used to decide how to sort relocs in an optimal manner for the
2398 dynamic linker, before writing them out. */
2400 static enum elf_reloc_type_class
2401 elf64_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2402 const asection *rel_sec ATTRIBUTE_UNUSED,
2403 const Elf_Internal_Rela *rela)
2405 if (ELF64_R_SYM (rela->r_info) == STN_UNDEF)
2406 return reloc_class_relative;
2408 switch ((int) ELF64_R_TYPE (rela->r_info))
2410 case R_PARISC_IPLT:
2411 return reloc_class_plt;
2412 case R_PARISC_COPY:
2413 return reloc_class_copy;
2414 default:
2415 return reloc_class_normal;
2419 /* Finish up the dynamic sections. */
2421 static bool
2422 elf64_hppa_finish_dynamic_sections (bfd *output_bfd,
2423 struct bfd_link_info *info)
2425 bfd *dynobj;
2426 asection *sdyn;
2427 struct elf64_hppa_link_hash_table *hppa_info;
2429 hppa_info = hppa_link_hash_table (info);
2430 if (hppa_info == NULL)
2431 return false;
2433 /* Finalize the contents of the .opd section. */
2434 elf_link_hash_traverse (elf_hash_table (info),
2435 elf64_hppa_finalize_opd,
2436 info);
2438 elf_link_hash_traverse (elf_hash_table (info),
2439 elf64_hppa_finalize_dynreloc,
2440 info);
2442 /* Finalize the contents of the .dlt section. */
2443 dynobj = elf_hash_table (info)->dynobj;
2444 /* Finalize the contents of the .dlt section. */
2445 elf_link_hash_traverse (elf_hash_table (info),
2446 elf64_hppa_finalize_dlt,
2447 info);
2449 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2451 if (elf_hash_table (info)->dynamic_sections_created)
2453 Elf64_External_Dyn *dyncon, *dynconend;
2455 BFD_ASSERT (sdyn != NULL);
2457 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2458 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2459 for (; dyncon < dynconend; dyncon++)
2461 Elf_Internal_Dyn dyn;
2462 asection *s;
2464 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2466 switch (dyn.d_tag)
2468 default:
2469 break;
2471 case DT_HP_LOAD_MAP:
2472 /* Compute the absolute address of 16byte scratchpad area
2473 for the dynamic linker.
2475 By convention the linker script will allocate the scratchpad
2476 area at the start of the .data section. So all we have to
2477 to is find the start of the .data section. */
2478 s = bfd_get_section_by_name (output_bfd, ".data");
2479 if (!s)
2480 return false;
2481 dyn.d_un.d_ptr = s->vma;
2482 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2483 break;
2485 case DT_PLTGOT:
2486 /* HP's use PLTGOT to set the GOT register. */
2487 dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2488 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2489 break;
2491 case DT_JMPREL:
2492 s = hppa_info->root.srelplt;
2493 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2494 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2495 break;
2497 case DT_PLTRELSZ:
2498 s = hppa_info->root.srelplt;
2499 dyn.d_un.d_val = s->size;
2500 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2501 break;
2503 case DT_RELA:
2504 s = hppa_info->other_rel_sec;
2505 if (! s || ! s->size)
2506 s = hppa_info->dlt_rel_sec;
2507 if (! s || ! s->size)
2508 s = hppa_info->opd_rel_sec;
2509 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2510 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2511 break;
2513 case DT_RELASZ:
2514 s = hppa_info->other_rel_sec;
2515 dyn.d_un.d_val = s->size;
2516 s = hppa_info->dlt_rel_sec;
2517 dyn.d_un.d_val += s->size;
2518 s = hppa_info->opd_rel_sec;
2519 dyn.d_un.d_val += s->size;
2520 /* There is some question about whether or not the size of
2521 the PLT relocs should be included here. HP's tools do
2522 it, so we'll emulate them. */
2523 s = hppa_info->root.srelplt;
2524 dyn.d_un.d_val += s->size;
2525 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2526 break;
2532 return true;
2535 /* Support for core dump NOTE sections. */
2537 static bool
2538 elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2540 int offset;
2541 size_t size;
2543 switch (note->descsz)
2545 default:
2546 return false;
2548 case 760: /* Linux/hppa */
2549 /* pr_cursig */
2550 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2552 /* pr_pid */
2553 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2555 /* pr_reg */
2556 offset = 112;
2557 size = 640;
2559 break;
2562 /* Make a ".reg/999" section. */
2563 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2564 size, note->descpos + offset);
2567 static bool
2568 elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2570 char * command;
2571 int n;
2573 switch (note->descsz)
2575 default:
2576 return false;
2578 case 136: /* Linux/hppa elf_prpsinfo. */
2579 elf_tdata (abfd)->core->program
2580 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2581 elf_tdata (abfd)->core->command
2582 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2585 /* Note that for some reason, a spurious space is tacked
2586 onto the end of the args in some (at least one anyway)
2587 implementations, so strip it off if it exists. */
2588 command = elf_tdata (abfd)->core->command;
2589 n = strlen (command);
2591 if (0 < n && command[n - 1] == ' ')
2592 command[n - 1] = '\0';
2594 return true;
2597 /* Return the number of additional phdrs we will need.
2599 The generic ELF code only creates PT_PHDRs for executables. The HP
2600 dynamic linker requires PT_PHDRs for dynamic libraries too.
2602 This routine indicates that the backend needs one additional program
2603 header for that case.
2605 Note we do not have access to the link info structure here, so we have
2606 to guess whether or not we are building a shared library based on the
2607 existence of a .interp section. */
2609 static int
2610 elf64_hppa_additional_program_headers (bfd *abfd,
2611 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2613 asection *s;
2615 /* If we are creating a shared library, then we have to create a
2616 PT_PHDR segment. HP's dynamic linker chokes without it. */
2617 s = bfd_get_section_by_name (abfd, ".interp");
2618 if (! s)
2619 return 1;
2620 return 0;
2623 static bool
2624 elf64_hppa_allow_non_load_phdr (bfd *abfd ATTRIBUTE_UNUSED,
2625 const Elf_Internal_Phdr *phdr ATTRIBUTE_UNUSED,
2626 unsigned int count ATTRIBUTE_UNUSED)
2628 return true;
2631 /* Allocate and initialize any program headers required by this
2632 specific backend.
2634 The generic ELF code only creates PT_PHDRs for executables. The HP
2635 dynamic linker requires PT_PHDRs for dynamic libraries too.
2637 This allocates the PT_PHDR and initializes it in a manner suitable
2638 for the HP linker.
2640 Note we do not have access to the link info structure here, so we have
2641 to guess whether or not we are building a shared library based on the
2642 existence of a .interp section. */
2644 static bool
2645 elf64_hppa_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
2647 struct elf_segment_map *m;
2649 m = elf_seg_map (abfd);
2650 if (info != NULL && !info->user_phdrs && m != NULL && m->p_type != PT_PHDR)
2652 m = ((struct elf_segment_map *)
2653 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
2654 if (m == NULL)
2655 return false;
2657 m->p_type = PT_PHDR;
2658 m->p_flags = PF_R | PF_X;
2659 m->p_flags_valid = 1;
2660 m->p_paddr_valid = 1;
2661 m->includes_phdrs = 1;
2663 m->next = elf_seg_map (abfd);
2664 elf_seg_map (abfd) = m;
2667 for (m = elf_seg_map (abfd) ; m != NULL; m = m->next)
2668 if (m->p_type == PT_LOAD)
2670 unsigned int i;
2672 for (i = 0; i < m->count; i++)
2674 /* The code "hint" is not really a hint. It is a requirement
2675 for certain versions of the HP dynamic linker. Worse yet,
2676 it must be set even if the shared library does not have
2677 any code in its "text" segment (thus the check for .hash
2678 to catch this situation). */
2679 if (m->sections[i]->flags & SEC_CODE
2680 || (strcmp (m->sections[i]->name, ".hash") == 0))
2681 m->p_flags |= (PF_X | PF_HP_CODE);
2685 return true;
2688 /* Called when writing out an object file to decide the type of a
2689 symbol. */
2690 static int
2691 elf64_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym,
2692 int type)
2694 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2695 return STT_PARISC_MILLI;
2696 else
2697 return type;
2700 /* Support HP specific sections for core files. */
2702 static bool
2703 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int sec_index,
2704 const char *typename)
2706 if (hdr->p_type == PT_HP_CORE_KERNEL)
2708 asection *sect;
2710 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2711 return false;
2713 sect = bfd_make_section_anyway (abfd, ".kernel");
2714 if (sect == NULL)
2715 return false;
2716 sect->size = hdr->p_filesz;
2717 sect->filepos = hdr->p_offset;
2718 sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2719 return true;
2722 if (hdr->p_type == PT_HP_CORE_PROC)
2724 int sig;
2726 if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2727 return false;
2728 if (bfd_read (&sig, 4, abfd) != 4)
2729 return false;
2731 elf_tdata (abfd)->core->signal = sig;
2733 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2734 return false;
2736 /* GDB uses the ".reg" section to read register contents. */
2737 return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2738 hdr->p_offset);
2741 if (hdr->p_type == PT_HP_CORE_LOADABLE
2742 || hdr->p_type == PT_HP_CORE_STACK
2743 || hdr->p_type == PT_HP_CORE_MMF)
2744 hdr->p_type = PT_LOAD;
2746 return _bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename);
2749 /* Hook called by the linker routine which adds symbols from an object
2750 file. HP's libraries define symbols with HP specific section
2751 indices, which we have to handle. */
2753 static bool
2754 elf_hppa_add_symbol_hook (bfd *abfd,
2755 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2756 Elf_Internal_Sym *sym,
2757 const char **namep ATTRIBUTE_UNUSED,
2758 flagword *flagsp ATTRIBUTE_UNUSED,
2759 asection **secp,
2760 bfd_vma *valp)
2762 unsigned int sec_index = sym->st_shndx;
2764 switch (sec_index)
2766 case SHN_PARISC_ANSI_COMMON:
2767 *secp = bfd_make_section_old_way (abfd, ".PARISC.ansi.common");
2768 (*secp)->flags |= SEC_IS_COMMON;
2769 *valp = sym->st_size;
2770 break;
2772 case SHN_PARISC_HUGE_COMMON:
2773 *secp = bfd_make_section_old_way (abfd, ".PARISC.huge.common");
2774 (*secp)->flags |= SEC_IS_COMMON;
2775 *valp = sym->st_size;
2776 break;
2779 return true;
2782 static bool
2783 elf_hppa_unmark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2784 void *data)
2786 struct bfd_link_info *info = data;
2788 /* If we are not creating a shared library, and this symbol is
2789 referenced by a shared library but is not defined anywhere, then
2790 the generic code will warn that it is undefined.
2792 This behavior is undesirable on HPs since the standard shared
2793 libraries contain references to undefined symbols.
2795 So we twiddle the flags associated with such symbols so that they
2796 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2798 Ultimately we should have better controls over the generic ELF BFD
2799 linker code. */
2800 if (! bfd_link_relocatable (info)
2801 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2802 && h->root.type == bfd_link_hash_undefined
2803 && h->ref_dynamic
2804 && !h->ref_regular)
2806 h->ref_dynamic = 0;
2807 h->pointer_equality_needed = 1;
2810 return true;
2813 static bool
2814 elf_hppa_remark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2815 void *data)
2817 struct bfd_link_info *info = data;
2819 /* If we are not creating a shared library, and this symbol is
2820 referenced by a shared library but is not defined anywhere, then
2821 the generic code will warn that it is undefined.
2823 This behavior is undesirable on HPs since the standard shared
2824 libraries contain references to undefined symbols.
2826 So we twiddle the flags associated with such symbols so that they
2827 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2829 Ultimately we should have better controls over the generic ELF BFD
2830 linker code. */
2831 if (! bfd_link_relocatable (info)
2832 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2833 && h->root.type == bfd_link_hash_undefined
2834 && !h->ref_dynamic
2835 && !h->ref_regular
2836 && h->pointer_equality_needed)
2838 h->ref_dynamic = 1;
2839 h->pointer_equality_needed = 0;
2842 return true;
2845 static bool
2846 elf_hppa_is_dynamic_loader_symbol (const char *name)
2848 return (! strcmp (name, "__CPU_REVISION")
2849 || ! strcmp (name, "__CPU_KEYBITS_1")
2850 || ! strcmp (name, "__SYSTEM_ID_D")
2851 || ! strcmp (name, "__FPU_MODEL")
2852 || ! strcmp (name, "__FPU_REVISION")
2853 || ! strcmp (name, "__ARGC")
2854 || ! strcmp (name, "__ARGV")
2855 || ! strcmp (name, "__ENVP")
2856 || ! strcmp (name, "__TLS_SIZE_D")
2857 || ! strcmp (name, "__LOAD_INFO")
2858 || ! strcmp (name, "__systab"));
2861 /* Record the lowest address for the data and text segments. */
2862 static void
2863 elf_hppa_record_segment_addrs (bfd *abfd,
2864 asection *section,
2865 void *data)
2867 struct elf64_hppa_link_hash_table *hppa_info = data;
2869 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2871 bfd_vma value;
2872 Elf_Internal_Phdr *p;
2874 p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
2875 BFD_ASSERT (p != NULL);
2876 value = p->p_vaddr;
2878 if (section->flags & SEC_READONLY)
2880 if (value < hppa_info->text_segment_base)
2881 hppa_info->text_segment_base = value;
2883 else
2885 if (value < hppa_info->data_segment_base)
2886 hppa_info->data_segment_base = value;
2891 /* Called after we have seen all the input files/sections, but before
2892 final symbol resolution and section placement has been determined.
2894 We use this hook to (possibly) provide a value for __gp, then we
2895 fall back to the generic ELF final link routine. */
2897 static bool
2898 elf_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
2900 struct stat buf;
2901 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
2903 if (hppa_info == NULL)
2904 return false;
2906 if (! bfd_link_relocatable (info))
2908 struct elf_link_hash_entry *gp;
2909 bfd_vma gp_val;
2911 /* The linker script defines a value for __gp iff it was referenced
2912 by one of the objects being linked. First try to find the symbol
2913 in the hash table. If that fails, just compute the value __gp
2914 should have had. */
2915 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
2916 false, false);
2918 if (gp)
2921 /* Adjust the value of __gp as we may want to slide it into the
2922 .plt section so that the stubs can access PLT entries without
2923 using an addil sequence. */
2924 gp->root.u.def.value += hppa_info->gp_offset;
2926 gp_val = (gp->root.u.def.section->output_section->vma
2927 + gp->root.u.def.section->output_offset
2928 + gp->root.u.def.value);
2930 else
2932 asection *sec;
2934 /* First look for a .plt section. If found, then __gp is the
2935 address of the .plt + gp_offset.
2937 If no .plt is found, then look for .dlt, .opd and .data (in
2938 that order) and set __gp to the base address of whichever
2939 section is found first. */
2941 sec = hppa_info->root.splt;
2942 if (sec && ! (sec->flags & SEC_EXCLUDE))
2943 gp_val = (sec->output_offset
2944 + sec->output_section->vma
2945 + hppa_info->gp_offset);
2946 else
2948 sec = hppa_info->dlt_sec;
2949 if (!sec || (sec->flags & SEC_EXCLUDE))
2950 sec = hppa_info->opd_sec;
2951 if (!sec || (sec->flags & SEC_EXCLUDE))
2952 sec = bfd_get_section_by_name (abfd, ".data");
2953 if (!sec || (sec->flags & SEC_EXCLUDE))
2954 gp_val = 0;
2955 else
2956 gp_val = sec->output_offset + sec->output_section->vma;
2960 /* Install whatever value we found/computed for __gp. */
2961 _bfd_set_gp_value (abfd, gp_val);
2964 /* We need to know the base of the text and data segments so that we
2965 can perform SEGREL relocations. We will record the base addresses
2966 when we encounter the first SEGREL relocation. */
2967 hppa_info->text_segment_base = (bfd_vma)-1;
2968 hppa_info->data_segment_base = (bfd_vma)-1;
2970 /* HP's shared libraries have references to symbols that are not
2971 defined anywhere. The generic ELF BFD linker code will complain
2972 about such symbols.
2974 So we detect the losing case and arrange for the flags on the symbol
2975 to indicate that it was never referenced. This keeps the generic
2976 ELF BFD link code happy and appears to not create any secondary
2977 problems. Ultimately we need a way to control the behavior of the
2978 generic ELF BFD link code better. */
2979 elf_link_hash_traverse (elf_hash_table (info),
2980 elf_hppa_unmark_useless_dynamic_symbols,
2981 info);
2983 /* Invoke the regular ELF backend linker to do all the work. */
2984 if (!bfd_elf_final_link (abfd, info))
2985 return false;
2987 elf_link_hash_traverse (elf_hash_table (info),
2988 elf_hppa_remark_useless_dynamic_symbols,
2989 info);
2991 /* If we're producing a final executable, sort the contents of the
2992 unwind section. */
2993 if (bfd_link_relocatable (info))
2994 return true;
2996 /* Do not attempt to sort non-regular files. This is here
2997 especially for configure scripts and kernel builds which run
2998 tests with "ld [...] -o /dev/null". */
2999 if (stat (bfd_get_filename (abfd), &buf) != 0
3000 || !S_ISREG(buf.st_mode))
3001 return true;
3003 return elf_hppa_sort_unwind (abfd);
3006 /* Relocate the given INSN. VALUE should be the actual value we want
3007 to insert into the instruction, ie by this point we should not be
3008 concerned with computing an offset relative to the DLT, PC, etc.
3009 Instead this routine is meant to handle the bit manipulations needed
3010 to insert the relocation into the given instruction. */
3012 static int
3013 elf_hppa_relocate_insn (int insn, int sym_value, unsigned int r_type)
3015 switch (r_type)
3017 /* This is any 22 bit branch. In PA2.0 syntax it corresponds to
3018 the "B" instruction. */
3019 case R_PARISC_PCREL22F:
3020 case R_PARISC_PCREL22C:
3021 return (insn & ~0x3ff1ffd) | re_assemble_22 (sym_value);
3023 /* This is any 12 bit branch. */
3024 case R_PARISC_PCREL12F:
3025 return (insn & ~0x1ffd) | re_assemble_12 (sym_value);
3027 /* This is any 17 bit branch. In PA2.0 syntax it also corresponds
3028 to the "B" instruction as well as BE. */
3029 case R_PARISC_PCREL17F:
3030 case R_PARISC_DIR17F:
3031 case R_PARISC_DIR17R:
3032 case R_PARISC_PCREL17C:
3033 case R_PARISC_PCREL17R:
3034 return (insn & ~0x1f1ffd) | re_assemble_17 (sym_value);
3036 /* ADDIL or LDIL instructions. */
3037 case R_PARISC_DLTREL21L:
3038 case R_PARISC_DLTIND21L:
3039 case R_PARISC_LTOFF_FPTR21L:
3040 case R_PARISC_PCREL21L:
3041 case R_PARISC_LTOFF_TP21L:
3042 case R_PARISC_DPREL21L:
3043 case R_PARISC_PLTOFF21L:
3044 case R_PARISC_DIR21L:
3045 return (insn & ~0x1fffff) | re_assemble_21 (sym_value);
3047 /* LDO and integer loads/stores with 14 bit displacements. */
3048 case R_PARISC_DLTREL14R:
3049 case R_PARISC_DLTREL14F:
3050 case R_PARISC_DLTIND14R:
3051 case R_PARISC_DLTIND14F:
3052 case R_PARISC_LTOFF_FPTR14R:
3053 case R_PARISC_PCREL14R:
3054 case R_PARISC_PCREL14F:
3055 case R_PARISC_LTOFF_TP14R:
3056 case R_PARISC_LTOFF_TP14F:
3057 case R_PARISC_DPREL14R:
3058 case R_PARISC_DPREL14F:
3059 case R_PARISC_PLTOFF14R:
3060 case R_PARISC_PLTOFF14F:
3061 case R_PARISC_DIR14R:
3062 case R_PARISC_DIR14F:
3063 return (insn & ~0x3fff) | low_sign_unext (sym_value, 14);
3065 /* PA2.0W LDO and integer loads/stores with 16 bit displacements. */
3066 case R_PARISC_LTOFF_FPTR16F:
3067 case R_PARISC_PCREL16F:
3068 case R_PARISC_LTOFF_TP16F:
3069 case R_PARISC_GPREL16F:
3070 case R_PARISC_PLTOFF16F:
3071 case R_PARISC_DIR16F:
3072 case R_PARISC_LTOFF16F:
3073 return (insn & ~0xffff) | re_assemble_16 (sym_value);
3075 /* Doubleword loads and stores with a 14 bit displacement. */
3076 case R_PARISC_DLTREL14DR:
3077 case R_PARISC_DLTIND14DR:
3078 case R_PARISC_LTOFF_FPTR14DR:
3079 case R_PARISC_LTOFF_FPTR16DF:
3080 case R_PARISC_PCREL14DR:
3081 case R_PARISC_PCREL16DF:
3082 case R_PARISC_LTOFF_TP14DR:
3083 case R_PARISC_LTOFF_TP16DF:
3084 case R_PARISC_DPREL14DR:
3085 case R_PARISC_GPREL16DF:
3086 case R_PARISC_PLTOFF14DR:
3087 case R_PARISC_PLTOFF16DF:
3088 case R_PARISC_DIR14DR:
3089 case R_PARISC_DIR16DF:
3090 case R_PARISC_LTOFF16DF:
3091 return (insn & ~0x3ff1) | (((sym_value & 0x2000) >> 13)
3092 | ((sym_value & 0x1ff8) << 1));
3094 /* Floating point single word load/store instructions. */
3095 case R_PARISC_DLTREL14WR:
3096 case R_PARISC_DLTIND14WR:
3097 case R_PARISC_LTOFF_FPTR14WR:
3098 case R_PARISC_LTOFF_FPTR16WF:
3099 case R_PARISC_PCREL14WR:
3100 case R_PARISC_PCREL16WF:
3101 case R_PARISC_LTOFF_TP14WR:
3102 case R_PARISC_LTOFF_TP16WF:
3103 case R_PARISC_DPREL14WR:
3104 case R_PARISC_GPREL16WF:
3105 case R_PARISC_PLTOFF14WR:
3106 case R_PARISC_PLTOFF16WF:
3107 case R_PARISC_DIR16WF:
3108 case R_PARISC_DIR14WR:
3109 case R_PARISC_LTOFF16WF:
3110 return (insn & ~0x3ff9) | (((sym_value & 0x2000) >> 13)
3111 | ((sym_value & 0x1ffc) << 1));
3113 default:
3114 return insn;
3118 /* Compute the value for a relocation (REL) during a final link stage,
3119 then insert the value into the proper location in CONTENTS.
3121 VALUE is a tentative value for the relocation and may be overridden
3122 and modified here based on the specific relocation to be performed.
3124 For example we do conversions for PC-relative branches in this routine
3125 or redirection of calls to external routines to stubs.
3127 The work of actually applying the relocation is left to a helper
3128 routine in an attempt to reduce the complexity and size of this
3129 function. */
3131 static bfd_reloc_status_type
3132 elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
3133 bfd *input_bfd,
3134 bfd *output_bfd,
3135 asection *input_section,
3136 bfd_byte *contents,
3137 bfd_vma value,
3138 struct bfd_link_info *info,
3139 asection *sym_sec,
3140 struct elf_link_hash_entry *eh)
3142 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
3143 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
3144 bfd_vma *local_offsets;
3145 Elf_Internal_Shdr *symtab_hdr;
3146 int insn;
3147 bfd_vma max_branch_offset = 0;
3148 bfd_vma offset = rel->r_offset;
3149 bfd_signed_vma addend = rel->r_addend;
3150 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3151 unsigned int r_symndx = ELF_R_SYM (rel->r_info);
3152 unsigned int r_type = howto->type;
3153 bfd_byte *hit_data = contents + offset;
3155 if (hppa_info == NULL)
3156 return bfd_reloc_notsupported;
3158 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3159 local_offsets = elf_local_got_offsets (input_bfd);
3160 insn = bfd_get_32 (input_bfd, hit_data);
3162 switch (r_type)
3164 case R_PARISC_NONE:
3165 break;
3167 /* Basic function call support.
3169 Note for a call to a function defined in another dynamic library
3170 we want to redirect the call to a stub. */
3172 /* PC relative relocs without an implicit offset. */
3173 case R_PARISC_PCREL21L:
3174 case R_PARISC_PCREL14R:
3175 case R_PARISC_PCREL14F:
3176 case R_PARISC_PCREL14WR:
3177 case R_PARISC_PCREL14DR:
3178 case R_PARISC_PCREL16F:
3179 case R_PARISC_PCREL16WF:
3180 case R_PARISC_PCREL16DF:
3182 /* If this is a call to a function defined in another dynamic
3183 library, then redirect the call to the local stub for this
3184 function. */
3185 if (sym_sec == NULL || sym_sec->output_section == NULL)
3186 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3187 + hppa_info->stub_sec->output_section->vma);
3189 /* Turn VALUE into a proper PC relative address. */
3190 value -= (offset + input_section->output_offset
3191 + input_section->output_section->vma);
3193 /* Adjust for any field selectors. */
3194 if (r_type == R_PARISC_PCREL21L)
3195 value = hppa_field_adjust (value, -8 + addend, e_lsel);
3196 else if (r_type == R_PARISC_PCREL14F
3197 || r_type == R_PARISC_PCREL16F
3198 || r_type == R_PARISC_PCREL16WF
3199 || r_type == R_PARISC_PCREL16DF)
3200 value = hppa_field_adjust (value, -8 + addend, e_fsel);
3201 else
3202 value = hppa_field_adjust (value, -8 + addend, e_rsel);
3204 /* Apply the relocation to the given instruction. */
3205 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3206 break;
3209 case R_PARISC_PCREL12F:
3210 case R_PARISC_PCREL22F:
3211 case R_PARISC_PCREL17F:
3212 case R_PARISC_PCREL22C:
3213 case R_PARISC_PCREL17C:
3214 case R_PARISC_PCREL17R:
3216 /* If this is a call to a function defined in another dynamic
3217 library, then redirect the call to the local stub for this
3218 function. */
3219 if (sym_sec == NULL || sym_sec->output_section == NULL)
3220 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3221 + hppa_info->stub_sec->output_section->vma);
3223 /* Turn VALUE into a proper PC relative address. */
3224 value -= (offset + input_section->output_offset
3225 + input_section->output_section->vma);
3226 addend -= 8;
3228 if (r_type == (unsigned int) R_PARISC_PCREL22F)
3229 max_branch_offset = (1 << (22-1)) << 2;
3230 else if (r_type == (unsigned int) R_PARISC_PCREL17F)
3231 max_branch_offset = (1 << (17-1)) << 2;
3232 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3233 max_branch_offset = (1 << (12-1)) << 2;
3235 /* Make sure we can reach the branch target. */
3236 if (max_branch_offset != 0
3237 && value + addend + max_branch_offset >= 2*max_branch_offset)
3239 _bfd_error_handler
3240 /* xgettext:c-format */
3241 (_("%pB(%pA+%#" PRIx64 "): cannot reach %s"),
3242 input_bfd,
3243 input_section,
3244 (uint64_t) offset,
3245 eh ? eh->root.root.string : "unknown");
3246 bfd_set_error (bfd_error_bad_value);
3247 return bfd_reloc_overflow;
3250 /* Adjust for any field selectors. */
3251 if (r_type == R_PARISC_PCREL17R)
3252 value = hppa_field_adjust (value, addend, e_rsel);
3253 else
3254 value = hppa_field_adjust (value, addend, e_fsel);
3256 /* All branches are implicitly shifted by 2 places. */
3257 value >>= 2;
3259 /* Apply the relocation to the given instruction. */
3260 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3261 break;
3264 /* Indirect references to data through the DLT. */
3265 case R_PARISC_DLTIND14R:
3266 case R_PARISC_DLTIND14F:
3267 case R_PARISC_DLTIND14DR:
3268 case R_PARISC_DLTIND14WR:
3269 case R_PARISC_DLTIND21L:
3270 case R_PARISC_LTOFF_FPTR14R:
3271 case R_PARISC_LTOFF_FPTR14DR:
3272 case R_PARISC_LTOFF_FPTR14WR:
3273 case R_PARISC_LTOFF_FPTR21L:
3274 case R_PARISC_LTOFF_FPTR16F:
3275 case R_PARISC_LTOFF_FPTR16WF:
3276 case R_PARISC_LTOFF_FPTR16DF:
3277 case R_PARISC_LTOFF_TP21L:
3278 case R_PARISC_LTOFF_TP14R:
3279 case R_PARISC_LTOFF_TP14F:
3280 case R_PARISC_LTOFF_TP14WR:
3281 case R_PARISC_LTOFF_TP14DR:
3282 case R_PARISC_LTOFF_TP16F:
3283 case R_PARISC_LTOFF_TP16WF:
3284 case R_PARISC_LTOFF_TP16DF:
3285 case R_PARISC_LTOFF16F:
3286 case R_PARISC_LTOFF16WF:
3287 case R_PARISC_LTOFF16DF:
3289 bfd_vma off;
3291 /* If this relocation was against a local symbol, then we still
3292 have not set up the DLT entry (it's not convenient to do so
3293 in the "finalize_dlt" routine because it is difficult to get
3294 to the local symbol's value).
3296 So, if this is a local symbol (h == NULL), then we need to
3297 fill in its DLT entry.
3299 Similarly we may still need to set up an entry in .opd for
3300 a local function which had its address taken. */
3301 if (hh == NULL)
3303 bfd_vma *local_opd_offsets, *local_dlt_offsets;
3305 if (local_offsets == NULL)
3306 abort ();
3308 /* Now do .opd creation if needed. */
3309 if (r_type == R_PARISC_LTOFF_FPTR14R
3310 || r_type == R_PARISC_LTOFF_FPTR14DR
3311 || r_type == R_PARISC_LTOFF_FPTR14WR
3312 || r_type == R_PARISC_LTOFF_FPTR21L
3313 || r_type == R_PARISC_LTOFF_FPTR16F
3314 || r_type == R_PARISC_LTOFF_FPTR16WF
3315 || r_type == R_PARISC_LTOFF_FPTR16DF)
3317 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3318 off = local_opd_offsets[r_symndx];
3320 /* The last bit records whether we've already initialised
3321 this local .opd entry. */
3322 if ((off & 1) != 0)
3324 BFD_ASSERT (off != (bfd_vma) -1);
3325 off &= ~1;
3327 else
3329 local_opd_offsets[r_symndx] |= 1;
3331 /* The first two words of an .opd entry are zero. */
3332 memset (hppa_info->opd_sec->contents + off, 0, 16);
3334 /* The next word is the address of the function. */
3335 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3336 (hppa_info->opd_sec->contents + off + 16));
3338 /* The last word is our local __gp value. */
3339 value = _bfd_get_gp_value (info->output_bfd);
3340 bfd_put_64 (hppa_info->opd_sec->owner, value,
3341 (hppa_info->opd_sec->contents + off + 24));
3344 /* The DLT value is the address of the .opd entry. */
3345 value = (off
3346 + hppa_info->opd_sec->output_offset
3347 + hppa_info->opd_sec->output_section->vma);
3348 addend = 0;
3351 local_dlt_offsets = local_offsets;
3352 off = local_dlt_offsets[r_symndx];
3354 if ((off & 1) != 0)
3356 BFD_ASSERT (off != (bfd_vma) -1);
3357 off &= ~1;
3359 else
3361 local_dlt_offsets[r_symndx] |= 1;
3362 bfd_put_64 (hppa_info->dlt_sec->owner,
3363 value + addend,
3364 hppa_info->dlt_sec->contents + off);
3367 else
3368 off = hh->dlt_offset;
3370 /* We want the value of the DLT offset for this symbol, not
3371 the symbol's actual address. Note that __gp may not point
3372 to the start of the DLT, so we have to compute the absolute
3373 address, then subtract out the value of __gp. */
3374 value = (off
3375 + hppa_info->dlt_sec->output_offset
3376 + hppa_info->dlt_sec->output_section->vma);
3377 value -= _bfd_get_gp_value (output_bfd);
3379 /* All DLTIND relocations are basically the same at this point,
3380 except that we need different field selectors for the 21bit
3381 version vs the 14bit versions. */
3382 if (r_type == R_PARISC_DLTIND21L
3383 || r_type == R_PARISC_LTOFF_FPTR21L
3384 || r_type == R_PARISC_LTOFF_TP21L)
3385 value = hppa_field_adjust (value, 0, e_lsel);
3386 else if (r_type == R_PARISC_DLTIND14F
3387 || r_type == R_PARISC_LTOFF_FPTR16F
3388 || r_type == R_PARISC_LTOFF_FPTR16WF
3389 || r_type == R_PARISC_LTOFF_FPTR16DF
3390 || r_type == R_PARISC_LTOFF16F
3391 || r_type == R_PARISC_LTOFF16DF
3392 || r_type == R_PARISC_LTOFF16WF
3393 || r_type == R_PARISC_LTOFF_TP16F
3394 || r_type == R_PARISC_LTOFF_TP16WF
3395 || r_type == R_PARISC_LTOFF_TP16DF)
3396 value = hppa_field_adjust (value, 0, e_fsel);
3397 else
3398 value = hppa_field_adjust (value, 0, e_rsel);
3400 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3401 break;
3404 case R_PARISC_DLTREL14R:
3405 case R_PARISC_DLTREL14F:
3406 case R_PARISC_DLTREL14DR:
3407 case R_PARISC_DLTREL14WR:
3408 case R_PARISC_DLTREL21L:
3409 case R_PARISC_DPREL21L:
3410 case R_PARISC_DPREL14WR:
3411 case R_PARISC_DPREL14DR:
3412 case R_PARISC_DPREL14R:
3413 case R_PARISC_DPREL14F:
3414 case R_PARISC_GPREL16F:
3415 case R_PARISC_GPREL16WF:
3416 case R_PARISC_GPREL16DF:
3418 /* Subtract out the global pointer value to make value a DLT
3419 relative address. */
3420 value -= _bfd_get_gp_value (output_bfd);
3422 /* All DLTREL relocations are basically the same at this point,
3423 except that we need different field selectors for the 21bit
3424 version vs the 14bit versions. */
3425 if (r_type == R_PARISC_DLTREL21L
3426 || r_type == R_PARISC_DPREL21L)
3427 value = hppa_field_adjust (value, addend, e_lrsel);
3428 else if (r_type == R_PARISC_DLTREL14F
3429 || r_type == R_PARISC_DPREL14F
3430 || r_type == R_PARISC_GPREL16F
3431 || r_type == R_PARISC_GPREL16WF
3432 || r_type == R_PARISC_GPREL16DF)
3433 value = hppa_field_adjust (value, addend, e_fsel);
3434 else
3435 value = hppa_field_adjust (value, addend, e_rrsel);
3437 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3438 break;
3441 case R_PARISC_DIR21L:
3442 case R_PARISC_DIR17R:
3443 case R_PARISC_DIR17F:
3444 case R_PARISC_DIR14R:
3445 case R_PARISC_DIR14F:
3446 case R_PARISC_DIR14WR:
3447 case R_PARISC_DIR14DR:
3448 case R_PARISC_DIR16F:
3449 case R_PARISC_DIR16WF:
3450 case R_PARISC_DIR16DF:
3452 /* All DIR relocations are basically the same at this point,
3453 except that branch offsets need to be divided by four, and
3454 we need different field selectors. Note that we don't
3455 redirect absolute calls to local stubs. */
3457 if (r_type == R_PARISC_DIR21L)
3458 value = hppa_field_adjust (value, addend, e_lrsel);
3459 else if (r_type == R_PARISC_DIR17F
3460 || r_type == R_PARISC_DIR16F
3461 || r_type == R_PARISC_DIR16WF
3462 || r_type == R_PARISC_DIR16DF
3463 || r_type == R_PARISC_DIR14F)
3464 value = hppa_field_adjust (value, addend, e_fsel);
3465 else
3466 value = hppa_field_adjust (value, addend, e_rrsel);
3468 if (r_type == R_PARISC_DIR17R || r_type == R_PARISC_DIR17F)
3469 /* All branches are implicitly shifted by 2 places. */
3470 value >>= 2;
3472 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3473 break;
3476 case R_PARISC_PLTOFF21L:
3477 case R_PARISC_PLTOFF14R:
3478 case R_PARISC_PLTOFF14F:
3479 case R_PARISC_PLTOFF14WR:
3480 case R_PARISC_PLTOFF14DR:
3481 case R_PARISC_PLTOFF16F:
3482 case R_PARISC_PLTOFF16WF:
3483 case R_PARISC_PLTOFF16DF:
3485 /* We want the value of the PLT offset for this symbol, not
3486 the symbol's actual address. Note that __gp may not point
3487 to the start of the DLT, so we have to compute the absolute
3488 address, then subtract out the value of __gp. */
3489 value = (hh->plt_offset
3490 + hppa_info->root.splt->output_offset
3491 + hppa_info->root.splt->output_section->vma);
3492 value -= _bfd_get_gp_value (output_bfd);
3494 /* All PLTOFF relocations are basically the same at this point,
3495 except that we need different field selectors for the 21bit
3496 version vs the 14bit versions. */
3497 if (r_type == R_PARISC_PLTOFF21L)
3498 value = hppa_field_adjust (value, addend, e_lrsel);
3499 else if (r_type == R_PARISC_PLTOFF14F
3500 || r_type == R_PARISC_PLTOFF16F
3501 || r_type == R_PARISC_PLTOFF16WF
3502 || r_type == R_PARISC_PLTOFF16DF)
3503 value = hppa_field_adjust (value, addend, e_fsel);
3504 else
3505 value = hppa_field_adjust (value, addend, e_rrsel);
3507 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3508 break;
3511 case R_PARISC_LTOFF_FPTR32:
3513 /* FIXME: There used to be code here to create the FPTR itself if
3514 the relocation was against a local symbol. But the code could
3515 never have worked. If the assert below is ever triggered then
3516 the code will need to be reinstated and fixed so that it does
3517 what is needed. */
3518 BFD_ASSERT (hh != NULL);
3520 /* We want the value of the DLT offset for this symbol, not
3521 the symbol's actual address. Note that __gp may not point
3522 to the start of the DLT, so we have to compute the absolute
3523 address, then subtract out the value of __gp. */
3524 value = (hh->dlt_offset
3525 + hppa_info->dlt_sec->output_offset
3526 + hppa_info->dlt_sec->output_section->vma);
3527 value -= _bfd_get_gp_value (output_bfd);
3528 bfd_put_32 (input_bfd, value, hit_data);
3529 return bfd_reloc_ok;
3532 case R_PARISC_LTOFF_FPTR64:
3533 case R_PARISC_LTOFF_TP64:
3535 /* We may still need to create the FPTR itself if it was for
3536 a local symbol. */
3537 if (eh == NULL && r_type == R_PARISC_LTOFF_FPTR64)
3539 /* The first two words of an .opd entry are zero. */
3540 memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
3542 /* The next word is the address of the function. */
3543 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3544 (hppa_info->opd_sec->contents
3545 + hh->opd_offset + 16));
3547 /* The last word is our local __gp value. */
3548 value = _bfd_get_gp_value (info->output_bfd);
3549 bfd_put_64 (hppa_info->opd_sec->owner, value,
3550 hppa_info->opd_sec->contents + hh->opd_offset + 24);
3552 /* The DLT value is the address of the .opd entry. */
3553 value = (hh->opd_offset
3554 + hppa_info->opd_sec->output_offset
3555 + hppa_info->opd_sec->output_section->vma);
3557 bfd_put_64 (hppa_info->dlt_sec->owner,
3558 value,
3559 hppa_info->dlt_sec->contents + hh->dlt_offset);
3562 /* We want the value of the DLT offset for this symbol, not
3563 the symbol's actual address. Note that __gp may not point
3564 to the start of the DLT, so we have to compute the absolute
3565 address, then subtract out the value of __gp. */
3566 value = (hh->dlt_offset
3567 + hppa_info->dlt_sec->output_offset
3568 + hppa_info->dlt_sec->output_section->vma);
3569 value -= _bfd_get_gp_value (output_bfd);
3570 bfd_put_64 (input_bfd, value, hit_data);
3571 return bfd_reloc_ok;
3574 case R_PARISC_DIR32:
3575 bfd_put_32 (input_bfd, value + addend, hit_data);
3576 return bfd_reloc_ok;
3578 case R_PARISC_DIR64:
3579 bfd_put_64 (input_bfd, value + addend, hit_data);
3580 return bfd_reloc_ok;
3582 case R_PARISC_GPREL64:
3583 /* Subtract out the global pointer value to make value a DLT
3584 relative address. */
3585 value -= _bfd_get_gp_value (output_bfd);
3587 bfd_put_64 (input_bfd, value + addend, hit_data);
3588 return bfd_reloc_ok;
3590 case R_PARISC_LTOFF64:
3591 /* We want the value of the DLT offset for this symbol, not
3592 the symbol's actual address. Note that __gp may not point
3593 to the start of the DLT, so we have to compute the absolute
3594 address, then subtract out the value of __gp. */
3595 value = (hh->dlt_offset
3596 + hppa_info->dlt_sec->output_offset
3597 + hppa_info->dlt_sec->output_section->vma);
3598 value -= _bfd_get_gp_value (output_bfd);
3600 bfd_put_64 (input_bfd, value + addend, hit_data);
3601 return bfd_reloc_ok;
3603 case R_PARISC_PCREL32:
3605 /* If this is a call to a function defined in another dynamic
3606 library, then redirect the call to the local stub for this
3607 function. */
3608 if (sym_sec == NULL || sym_sec->output_section == NULL)
3609 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3610 + hppa_info->stub_sec->output_section->vma);
3612 /* Turn VALUE into a proper PC relative address. */
3613 value -= (offset + input_section->output_offset
3614 + input_section->output_section->vma);
3616 value += addend;
3617 value -= 8;
3618 bfd_put_32 (input_bfd, value, hit_data);
3619 return bfd_reloc_ok;
3622 case R_PARISC_PCREL64:
3624 /* If this is a call to a function defined in another dynamic
3625 library, then redirect the call to the local stub for this
3626 function. */
3627 if (sym_sec == NULL || sym_sec->output_section == NULL)
3628 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3629 + hppa_info->stub_sec->output_section->vma);
3631 /* Turn VALUE into a proper PC relative address. */
3632 value -= (offset + input_section->output_offset
3633 + input_section->output_section->vma);
3635 value += addend;
3636 value -= 8;
3637 bfd_put_64 (input_bfd, value, hit_data);
3638 return bfd_reloc_ok;
3641 case R_PARISC_FPTR64:
3643 bfd_vma off;
3645 /* We may still need to create the FPTR itself if it was for
3646 a local symbol. */
3647 if (hh == NULL)
3649 bfd_vma *local_opd_offsets;
3651 if (local_offsets == NULL)
3652 abort ();
3654 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3655 off = local_opd_offsets[r_symndx];
3657 /* The last bit records whether we've already initialised
3658 this local .opd entry. */
3659 if ((off & 1) != 0)
3661 BFD_ASSERT (off != (bfd_vma) -1);
3662 off &= ~1;
3664 else
3666 /* The first two words of an .opd entry are zero. */
3667 memset (hppa_info->opd_sec->contents + off, 0, 16);
3669 /* The next word is the address of the function. */
3670 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3671 (hppa_info->opd_sec->contents + off + 16));
3673 /* The last word is our local __gp value. */
3674 value = _bfd_get_gp_value (info->output_bfd);
3675 bfd_put_64 (hppa_info->opd_sec->owner, value,
3676 hppa_info->opd_sec->contents + off + 24);
3679 else
3680 off = hh->opd_offset;
3682 if (hh == NULL || hh->want_opd)
3683 /* We want the value of the OPD offset for this symbol. */
3684 value = (off
3685 + hppa_info->opd_sec->output_offset
3686 + hppa_info->opd_sec->output_section->vma);
3687 else
3688 /* We want the address of the symbol. */
3689 value += addend;
3691 bfd_put_64 (input_bfd, value, hit_data);
3692 return bfd_reloc_ok;
3695 case R_PARISC_SECREL32:
3696 if (sym_sec && sym_sec->output_section)
3697 value -= sym_sec->output_section->vma;
3698 bfd_put_32 (input_bfd, value + addend, hit_data);
3699 return bfd_reloc_ok;
3701 case R_PARISC_SEGREL32:
3702 case R_PARISC_SEGREL64:
3704 /* If this is the first SEGREL relocation, then initialize
3705 the segment base values. */
3706 if (hppa_info->text_segment_base == (bfd_vma) -1)
3707 bfd_map_over_sections (output_bfd, elf_hppa_record_segment_addrs,
3708 hppa_info);
3710 /* VALUE holds the absolute address. We want to include the
3711 addend, then turn it into a segment relative address.
3713 The segment is derived from SYM_SEC. We assume that there are
3714 only two segments of note in the resulting executable/shlib.
3715 A readonly segment (.text) and a readwrite segment (.data). */
3716 value += addend;
3718 if (sym_sec->flags & SEC_CODE)
3719 value -= hppa_info->text_segment_base;
3720 else
3721 value -= hppa_info->data_segment_base;
3723 if (r_type == R_PARISC_SEGREL32)
3724 bfd_put_32 (input_bfd, value, hit_data);
3725 else
3726 bfd_put_64 (input_bfd, value, hit_data);
3727 return bfd_reloc_ok;
3730 /* Something we don't know how to handle. */
3731 default:
3732 return bfd_reloc_notsupported;
3735 /* Update the instruction word. */
3736 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3737 return bfd_reloc_ok;
3740 /* Relocate an HPPA ELF section. */
3742 static int
3743 elf64_hppa_relocate_section (bfd *output_bfd,
3744 struct bfd_link_info *info,
3745 bfd *input_bfd,
3746 asection *input_section,
3747 bfd_byte *contents,
3748 Elf_Internal_Rela *relocs,
3749 Elf_Internal_Sym *local_syms,
3750 asection **local_sections)
3752 Elf_Internal_Shdr *symtab_hdr;
3753 Elf_Internal_Rela *rel;
3754 Elf_Internal_Rela *relend;
3755 struct elf64_hppa_link_hash_table *hppa_info;
3757 hppa_info = hppa_link_hash_table (info);
3758 if (hppa_info == NULL)
3759 return false;
3761 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3763 rel = relocs;
3764 relend = relocs + input_section->reloc_count;
3765 for (; rel < relend; rel++)
3767 int r_type;
3768 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3769 unsigned long r_symndx;
3770 struct elf_link_hash_entry *eh;
3771 Elf_Internal_Sym *sym;
3772 asection *sym_sec;
3773 bfd_vma relocation;
3774 bfd_reloc_status_type r;
3776 r_type = ELF_R_TYPE (rel->r_info);
3777 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
3779 bfd_set_error (bfd_error_bad_value);
3780 return false;
3782 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3783 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3784 continue;
3786 /* This is a final link. */
3787 r_symndx = ELF_R_SYM (rel->r_info);
3788 eh = NULL;
3789 sym = NULL;
3790 sym_sec = NULL;
3791 if (r_symndx < symtab_hdr->sh_info)
3793 /* This is a local symbol, hh defaults to NULL. */
3794 sym = local_syms + r_symndx;
3795 sym_sec = local_sections[r_symndx];
3796 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3798 else
3800 /* This is not a local symbol. */
3801 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3803 /* It seems this can happen with erroneous or unsupported
3804 input (mixing a.out and elf in an archive, for example.) */
3805 if (sym_hashes == NULL)
3806 return false;
3808 eh = sym_hashes[r_symndx - symtab_hdr->sh_info];
3810 if (info->wrap_hash != NULL
3811 && (input_section->flags & SEC_DEBUGGING) != 0)
3812 eh = ((struct elf_link_hash_entry *)
3813 unwrap_hash_lookup (info, input_bfd, &eh->root));
3815 while (eh->root.type == bfd_link_hash_indirect
3816 || eh->root.type == bfd_link_hash_warning)
3817 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3819 relocation = 0;
3820 if (eh->root.type == bfd_link_hash_defined
3821 || eh->root.type == bfd_link_hash_defweak)
3823 sym_sec = eh->root.u.def.section;
3824 if (sym_sec != NULL
3825 && sym_sec->output_section != NULL)
3826 relocation = (eh->root.u.def.value
3827 + sym_sec->output_section->vma
3828 + sym_sec->output_offset);
3830 else if (eh->root.type == bfd_link_hash_undefweak)
3832 else if (info->unresolved_syms_in_objects == RM_IGNORE
3833 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
3835 else if (!bfd_link_relocatable (info)
3836 && elf_hppa_is_dynamic_loader_symbol (eh->root.root.string))
3837 continue;
3838 else if (!bfd_link_relocatable (info))
3840 bool err;
3842 err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
3843 && !info->warn_unresolved_syms)
3844 || ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT;
3846 info->callbacks->undefined_symbol
3847 (info, eh->root.root.string, input_bfd,
3848 input_section, rel->r_offset, err);
3851 if (!bfd_link_relocatable (info)
3852 && relocation == 0
3853 && eh->root.type != bfd_link_hash_defined
3854 && eh->root.type != bfd_link_hash_defweak
3855 && eh->root.type != bfd_link_hash_undefweak)
3857 if (info->unresolved_syms_in_objects == RM_IGNORE
3858 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3859 && eh->type == STT_PARISC_MILLI)
3860 info->callbacks->undefined_symbol
3861 (info, eh_name (eh), input_bfd,
3862 input_section, rel->r_offset, false);
3866 if (sym_sec != NULL && discarded_section (sym_sec))
3867 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3868 rel, 1, relend, howto, 0, contents);
3870 if (bfd_link_relocatable (info))
3871 continue;
3873 r = elf_hppa_final_link_relocate (rel, input_bfd, output_bfd,
3874 input_section, contents,
3875 relocation, info, sym_sec,
3876 eh);
3878 if (r != bfd_reloc_ok)
3880 switch (r)
3882 default:
3883 abort ();
3884 case bfd_reloc_overflow:
3886 const char *sym_name;
3888 if (eh != NULL)
3889 sym_name = NULL;
3890 else
3892 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3893 symtab_hdr->sh_link,
3894 sym->st_name);
3895 if (sym_name == NULL)
3896 return false;
3897 if (*sym_name == '\0')
3898 sym_name = bfd_section_name (sym_sec);
3901 (*info->callbacks->reloc_overflow)
3902 (info, (eh ? &eh->root : NULL), sym_name, howto->name,
3903 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3905 break;
3909 return true;
3912 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
3914 { STRING_COMMA_LEN (".tbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
3915 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3916 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3917 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3918 { STRING_COMMA_LEN (".dlt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3919 { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3920 { STRING_COMMA_LEN (".sbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3921 { NULL, 0, 0, 0, 0 }
3924 /* The hash bucket size is the standard one, namely 4. */
3926 const struct elf_size_info hppa64_elf_size_info =
3928 sizeof (Elf64_External_Ehdr),
3929 sizeof (Elf64_External_Phdr),
3930 sizeof (Elf64_External_Shdr),
3931 sizeof (Elf64_External_Rel),
3932 sizeof (Elf64_External_Rela),
3933 sizeof (Elf64_External_Sym),
3934 sizeof (Elf64_External_Dyn),
3935 sizeof (Elf_External_Note),
3938 64, 3,
3939 ELFCLASS64, EV_CURRENT,
3940 bfd_elf64_write_out_phdrs,
3941 bfd_elf64_write_shdrs_and_ehdr,
3942 bfd_elf64_checksum_contents,
3943 bfd_elf64_write_relocs,
3944 bfd_elf64_swap_symbol_in,
3945 bfd_elf64_swap_symbol_out,
3946 bfd_elf64_slurp_reloc_table,
3947 bfd_elf64_slurp_symbol_table,
3948 bfd_elf64_swap_dyn_in,
3949 bfd_elf64_swap_dyn_out,
3950 bfd_elf64_swap_reloc_in,
3951 bfd_elf64_swap_reloc_out,
3952 bfd_elf64_swap_reloca_in,
3953 bfd_elf64_swap_reloca_out
3956 #define TARGET_BIG_SYM hppa_elf64_vec
3957 #define TARGET_BIG_NAME "elf64-hppa"
3958 #define ELF_ARCH bfd_arch_hppa
3959 #define ELF_TARGET_ID HPPA64_ELF_DATA
3960 #define ELF_MACHINE_CODE EM_PARISC
3961 /* This is not strictly correct. The maximum page size for PA2.0 is
3962 64M. But everything still uses 4k. */
3963 #define ELF_MAXPAGESIZE 0x1000
3964 #define ELF_OSABI ELFOSABI_HPUX
3966 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
3967 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
3968 #define bfd_elf64_bfd_is_local_label_name elf_hppa_is_local_label_name
3969 #define elf_info_to_howto elf_hppa_info_to_howto
3970 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
3972 #define elf_backend_section_from_shdr elf64_hppa_section_from_shdr
3973 #define elf_backend_object_p elf64_hppa_object_p
3974 #define elf_backend_final_write_processing \
3975 elf_hppa_final_write_processing
3976 #define elf_backend_fake_sections elf_hppa_fake_sections
3977 #define elf_backend_add_symbol_hook elf_hppa_add_symbol_hook
3979 #define elf_backend_relocate_section elf_hppa_relocate_section
3981 #define bfd_elf64_bfd_final_link elf_hppa_final_link
3983 #define elf_backend_create_dynamic_sections \
3984 elf64_hppa_create_dynamic_sections
3985 #define elf_backend_init_file_header elf64_hppa_init_file_header
3987 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
3989 #define elf_backend_adjust_dynamic_symbol \
3990 elf64_hppa_adjust_dynamic_symbol
3992 #define elf_backend_late_size_sections elf64_hppa_late_size_sections
3994 #define elf_backend_finish_dynamic_symbol \
3995 elf64_hppa_finish_dynamic_symbol
3996 #define elf_backend_finish_dynamic_sections \
3997 elf64_hppa_finish_dynamic_sections
3998 #define elf_backend_grok_prstatus elf64_hppa_grok_prstatus
3999 #define elf_backend_grok_psinfo elf64_hppa_grok_psinfo
4001 /* Stuff for the BFD linker: */
4002 #define bfd_elf64_bfd_link_hash_table_create \
4003 elf64_hppa_hash_table_create
4005 #define elf_backend_check_relocs \
4006 elf64_hppa_check_relocs
4008 #define elf_backend_size_info \
4009 hppa64_elf_size_info
4011 #define elf_backend_additional_program_headers \
4012 elf64_hppa_additional_program_headers
4014 #define elf_backend_modify_segment_map \
4015 elf64_hppa_modify_segment_map
4017 #define elf_backend_allow_non_load_phdr \
4018 elf64_hppa_allow_non_load_phdr
4020 #define elf_backend_link_output_symbol_hook \
4021 elf64_hppa_link_output_symbol_hook
4023 #define elf_backend_want_got_plt 0
4024 #define elf_backend_plt_readonly 0
4025 #define elf_backend_want_plt_sym 0
4026 #define elf_backend_got_header_size 0
4027 #define elf_backend_type_change_ok true
4028 #define elf_backend_get_symbol_type elf64_hppa_elf_get_symbol_type
4029 #define elf_backend_reloc_type_class elf64_hppa_reloc_type_class
4030 #define elf_backend_rela_normal 1
4031 #define elf_backend_special_sections elf64_hppa_special_sections
4032 #define elf_backend_action_discarded elf_hppa_action_discarded
4033 #define elf_backend_section_from_phdr elf64_hppa_section_from_phdr
4035 #define elf64_bed elf64_hppa_hpux_bed
4037 #include "elf64-target.h"
4039 #undef TARGET_BIG_SYM
4040 #define TARGET_BIG_SYM hppa_elf64_linux_vec
4041 #undef TARGET_BIG_NAME
4042 #define TARGET_BIG_NAME "elf64-hppa-linux"
4043 #undef ELF_OSABI
4044 #define ELF_OSABI ELFOSABI_GNU
4045 #undef elf64_bed
4046 #define elf64_bed elf64_hppa_linux_bed
4047 #undef elf_backend_special_sections
4048 #define elf_backend_special_sections (elf64_hppa_special_sections + 1)
4050 #include "elf64-target.h"