Automatic date update in version.in
[binutils-gdb.git] / bfd / elf64-hppa.c
blob2d08bf3715574dfdccc68b66e4134bc45c65bf7e
1 /* Support for HPPA 64-bit ELF
2 Copyright (C) 1999-2024 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),
300 HPPA64_ELF_DATA))
302 free (htab);
303 return NULL;
306 htab->root.dt_pltgot_required = true;
307 htab->text_segment_base = (bfd_vma) -1;
308 htab->data_segment_base = (bfd_vma) -1;
310 return &htab->root.root;
313 /* Return nonzero if ABFD represents a PA2.0 ELF64 file.
315 Additionally we set the default architecture and machine. */
316 static bool
317 elf64_hppa_object_p (bfd *abfd)
319 Elf_Internal_Ehdr * i_ehdrp;
320 unsigned int flags;
322 i_ehdrp = elf_elfheader (abfd);
323 if (abfd->xvec == & hppa_elf64_linux_vec)
325 /* GCC on hppa-linux produces binaries with OSABI=GNU,
326 but the kernel produces corefiles with OSABI=SysV. */
327 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
328 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
329 return false;
331 else
333 /* HPUX produces binaries with OSABI=HPUX,
334 but the kernel produces corefiles with OSABI=SysV. */
335 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
336 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
337 return false;
340 flags = i_ehdrp->e_flags;
341 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
343 case EFA_PARISC_1_0:
344 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
345 case EFA_PARISC_1_1:
346 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
347 case EFA_PARISC_2_0:
348 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
349 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
350 else
351 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
352 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
353 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
355 /* Don't be fussy. */
356 return true;
359 /* Given section type (hdr->sh_type), return a boolean indicating
360 whether or not the section is an elf64-hppa specific section. */
361 static bool
362 elf64_hppa_section_from_shdr (bfd *abfd,
363 Elf_Internal_Shdr *hdr,
364 const char *name,
365 int shindex)
367 switch (hdr->sh_type)
369 case SHT_PARISC_EXT:
370 if (strcmp (name, ".PARISC.archext") != 0)
371 return false;
372 break;
373 case SHT_PARISC_UNWIND:
374 if (strcmp (name, ".PARISC.unwind") != 0)
375 return false;
376 break;
377 case SHT_PARISC_DOC:
378 case SHT_PARISC_ANNOT:
379 default:
380 return false;
383 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
384 return false;
386 return ((hdr->sh_flags & SHF_PARISC_SHORT) == 0
387 || bfd_set_section_flags (hdr->bfd_section,
388 hdr->bfd_section->flags | SEC_SMALL_DATA));
391 /* SEC is a section containing relocs for an input BFD when linking; return
392 a suitable section for holding relocs in the output BFD for a link. */
394 static bool
395 get_reloc_section (bfd *abfd,
396 struct elf64_hppa_link_hash_table *hppa_info,
397 asection *sec)
399 const char *srel_name;
400 asection *srel;
401 bfd *dynobj;
403 srel_name = (bfd_elf_string_from_elf_section
404 (abfd, elf_elfheader(abfd)->e_shstrndx,
405 _bfd_elf_single_rel_hdr(sec)->sh_name));
406 if (srel_name == NULL)
407 return false;
409 dynobj = hppa_info->root.dynobj;
410 if (!dynobj)
411 hppa_info->root.dynobj = dynobj = abfd;
413 srel = bfd_get_linker_section (dynobj, srel_name);
414 if (srel == NULL)
416 srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
417 (SEC_ALLOC
418 | SEC_LOAD
419 | SEC_HAS_CONTENTS
420 | SEC_IN_MEMORY
421 | SEC_LINKER_CREATED
422 | SEC_READONLY));
423 if (srel == NULL
424 || !bfd_set_section_alignment (srel, 3))
425 return false;
428 hppa_info->other_rel_sec = srel;
429 return true;
432 /* Add a new entry to the list of dynamic relocations against DYN_H.
434 We use this to keep a record of all the FPTR relocations against a
435 particular symbol so that we can create FPTR relocations in the
436 output file. */
438 static bool
439 count_dyn_reloc (bfd *abfd,
440 struct elf64_hppa_link_hash_entry *hh,
441 int type,
442 asection *sec,
443 int sec_symndx,
444 bfd_vma offset,
445 bfd_vma addend)
447 struct elf64_hppa_dyn_reloc_entry *rent;
449 rent = (struct elf64_hppa_dyn_reloc_entry *)
450 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
451 if (!rent)
452 return false;
454 rent->next = hh->reloc_entries;
455 rent->type = type;
456 rent->sec = sec;
457 rent->sec_symndx = sec_symndx;
458 rent->offset = offset;
459 rent->addend = addend;
460 hh->reloc_entries = rent;
462 return true;
465 /* Return a pointer to the local DLT, PLT and OPD reference counts
466 for ABFD. Returns NULL if the storage allocation fails. */
468 static bfd_signed_vma *
469 hppa64_elf_local_refcounts (bfd *abfd)
471 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
472 bfd_signed_vma *local_refcounts;
474 local_refcounts = elf_local_got_refcounts (abfd);
475 if (local_refcounts == NULL)
477 bfd_size_type size;
479 /* Allocate space for local DLT, PLT and OPD reference
480 counts. Done this way to save polluting elf_obj_tdata
481 with another target specific pointer. */
482 size = symtab_hdr->sh_info;
483 size *= 3 * sizeof (bfd_signed_vma);
484 local_refcounts = bfd_zalloc (abfd, size);
485 elf_local_got_refcounts (abfd) = local_refcounts;
487 return local_refcounts;
490 /* Scan the RELOCS and record the type of dynamic entries that each
491 referenced symbol needs. */
493 static bool
494 elf64_hppa_check_relocs (bfd *abfd,
495 struct bfd_link_info *info,
496 asection *sec,
497 const Elf_Internal_Rela *relocs)
499 struct elf64_hppa_link_hash_table *hppa_info;
500 const Elf_Internal_Rela *relend;
501 Elf_Internal_Shdr *symtab_hdr;
502 const Elf_Internal_Rela *rel;
503 unsigned int sec_symndx;
505 if (bfd_link_relocatable (info))
506 return true;
508 /* If this is the first dynamic object found in the link, create
509 the special sections required for dynamic linking. */
510 if (! elf_hash_table (info)->dynamic_sections_created)
512 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
513 return false;
516 hppa_info = hppa_link_hash_table (info);
517 if (hppa_info == NULL)
518 return false;
519 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
521 /* If necessary, build a new table holding section symbols indices
522 for this BFD. */
524 if (bfd_link_pic (info) && hppa_info->section_syms_bfd != abfd)
526 unsigned long i;
527 unsigned int highest_shndx;
528 Elf_Internal_Sym *local_syms = NULL;
529 Elf_Internal_Sym *isym, *isymend;
530 bfd_size_type amt;
532 /* We're done with the old cache of section index to section symbol
533 index information. Free it.
535 ?!? Note we leak the last section_syms array. Presumably we
536 could free it in one of the later routines in this file. */
537 free (hppa_info->section_syms);
539 /* Read this BFD's local symbols. */
540 if (symtab_hdr->sh_info != 0)
542 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
543 if (local_syms == NULL)
544 local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
545 symtab_hdr->sh_info, 0,
546 NULL, NULL, NULL);
547 if (local_syms == NULL)
548 return false;
551 /* Record the highest section index referenced by the local symbols. */
552 highest_shndx = 0;
553 isymend = local_syms + symtab_hdr->sh_info;
554 for (isym = local_syms; isym < isymend; isym++)
556 if (isym->st_shndx > highest_shndx
557 && isym->st_shndx < SHN_LORESERVE)
558 highest_shndx = isym->st_shndx;
561 /* Allocate an array to hold the section index to section symbol index
562 mapping. Bump by one since we start counting at zero. */
563 highest_shndx++;
564 amt = highest_shndx;
565 amt *= sizeof (int);
566 hppa_info->section_syms = (int *) bfd_malloc (amt);
568 /* Now walk the local symbols again. If we find a section symbol,
569 record the index of the symbol into the section_syms array. */
570 for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
572 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
573 hppa_info->section_syms[isym->st_shndx] = i;
576 /* We are finished with the local symbols. */
577 if (local_syms != NULL
578 && symtab_hdr->contents != (unsigned char *) local_syms)
580 if (! info->keep_memory)
581 free (local_syms);
582 else
584 /* Cache the symbols for elf_link_input_bfd. */
585 symtab_hdr->contents = (unsigned char *) local_syms;
589 /* Record which BFD we built the section_syms mapping for. */
590 hppa_info->section_syms_bfd = abfd;
593 /* Record the symbol index for this input section. We may need it for
594 relocations when building shared libraries. When not building shared
595 libraries this value is never really used, but assign it to zero to
596 prevent out of bounds memory accesses in other routines. */
597 if (bfd_link_pic (info))
599 sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
601 /* If we did not find a section symbol for this section, then
602 something went terribly wrong above. */
603 if (sec_symndx == SHN_BAD)
604 return false;
606 if (sec_symndx < SHN_LORESERVE)
607 sec_symndx = hppa_info->section_syms[sec_symndx];
608 else
609 sec_symndx = 0;
611 else
612 sec_symndx = 0;
614 relend = relocs + sec->reloc_count;
615 for (rel = relocs; rel < relend; ++rel)
617 enum
619 NEED_DLT = 1,
620 NEED_PLT = 2,
621 NEED_STUB = 4,
622 NEED_OPD = 8,
623 NEED_DYNREL = 16,
626 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
627 struct elf64_hppa_link_hash_entry *hh;
628 int need_entry;
629 bool maybe_dynamic;
630 int dynrel_type = R_PARISC_NONE;
631 static reloc_howto_type *howto;
633 if (r_symndx >= symtab_hdr->sh_info)
635 /* We're dealing with a global symbol -- find its hash entry
636 and mark it as being referenced. */
637 long indx = r_symndx - symtab_hdr->sh_info;
638 hh = hppa_elf_hash_entry (elf_sym_hashes (abfd)[indx]);
639 while (hh->eh.root.type == bfd_link_hash_indirect
640 || hh->eh.root.type == bfd_link_hash_warning)
641 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
643 /* PR15323, ref flags aren't set for references in the same
644 object. */
645 hh->eh.ref_regular = 1;
647 else
648 hh = NULL;
650 /* We can only get preliminary data on whether a symbol is
651 locally or externally defined, as not all of the input files
652 have yet been processed. Do something with what we know, as
653 this may help reduce memory usage and processing time later. */
654 maybe_dynamic = false;
655 if (hh && ((bfd_link_pic (info)
656 && (!info->symbolic
657 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
658 || !hh->eh.def_regular
659 || hh->eh.root.type == bfd_link_hash_defweak))
660 maybe_dynamic = true;
662 howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
663 need_entry = 0;
664 switch (howto->type)
666 /* These are simple indirect references to symbols through the
667 DLT. We need to create a DLT entry for any symbols which
668 appears in a DLTIND relocation. */
669 case R_PARISC_DLTIND21L:
670 case R_PARISC_DLTIND14R:
671 case R_PARISC_DLTIND14F:
672 case R_PARISC_DLTIND14WR:
673 case R_PARISC_DLTIND14DR:
674 need_entry = NEED_DLT;
675 break;
677 /* ?!? These need a DLT entry. But I have no idea what to do with
678 the "link time TP value. */
679 case R_PARISC_LTOFF_TP21L:
680 case R_PARISC_LTOFF_TP14R:
681 case R_PARISC_LTOFF_TP14F:
682 case R_PARISC_LTOFF_TP64:
683 case R_PARISC_LTOFF_TP14WR:
684 case R_PARISC_LTOFF_TP14DR:
685 case R_PARISC_LTOFF_TP16F:
686 case R_PARISC_LTOFF_TP16WF:
687 case R_PARISC_LTOFF_TP16DF:
688 need_entry = NEED_DLT;
689 break;
691 /* These are function calls. Depending on their precise target we
692 may need to make a stub for them. The stub uses the PLT, so we
693 need to create PLT entries for these symbols too. */
694 case R_PARISC_PCREL12F:
695 case R_PARISC_PCREL17F:
696 case R_PARISC_PCREL22F:
697 case R_PARISC_PCREL32:
698 case R_PARISC_PCREL64:
699 case R_PARISC_PCREL21L:
700 case R_PARISC_PCREL17R:
701 case R_PARISC_PCREL17C:
702 case R_PARISC_PCREL14R:
703 case R_PARISC_PCREL14F:
704 case R_PARISC_PCREL22C:
705 case R_PARISC_PCREL14WR:
706 case R_PARISC_PCREL14DR:
707 case R_PARISC_PCREL16F:
708 case R_PARISC_PCREL16WF:
709 case R_PARISC_PCREL16DF:
710 /* Function calls might need to go through the .plt, and
711 might need a long branch stub. */
712 if (hh != NULL && hh->eh.type != STT_PARISC_MILLI)
713 need_entry = (NEED_PLT | NEED_STUB);
714 else
715 need_entry = 0;
716 break;
718 case R_PARISC_PLTOFF21L:
719 case R_PARISC_PLTOFF14R:
720 case R_PARISC_PLTOFF14F:
721 case R_PARISC_PLTOFF14WR:
722 case R_PARISC_PLTOFF14DR:
723 case R_PARISC_PLTOFF16F:
724 case R_PARISC_PLTOFF16WF:
725 case R_PARISC_PLTOFF16DF:
726 need_entry = (NEED_PLT);
727 break;
729 case R_PARISC_DIR64:
730 if (bfd_link_pic (info) || maybe_dynamic)
731 need_entry = (NEED_DYNREL);
732 dynrel_type = R_PARISC_DIR64;
733 break;
735 /* This is an indirect reference through the DLT to get the address
736 of a OPD descriptor. Thus we need to make a DLT entry that points
737 to an OPD entry. */
738 case R_PARISC_LTOFF_FPTR21L:
739 case R_PARISC_LTOFF_FPTR14R:
740 case R_PARISC_LTOFF_FPTR14WR:
741 case R_PARISC_LTOFF_FPTR14DR:
742 case R_PARISC_LTOFF_FPTR32:
743 case R_PARISC_LTOFF_FPTR64:
744 case R_PARISC_LTOFF_FPTR16F:
745 case R_PARISC_LTOFF_FPTR16WF:
746 case R_PARISC_LTOFF_FPTR16DF:
747 if (bfd_link_pic (info) || maybe_dynamic)
748 need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
749 else
750 need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
751 dynrel_type = R_PARISC_FPTR64;
752 break;
754 /* This is a simple OPD entry. */
755 case R_PARISC_FPTR64:
756 if (bfd_link_pic (info) || maybe_dynamic)
757 need_entry = (NEED_OPD | NEED_PLT | NEED_DYNREL);
758 else
759 need_entry = (NEED_OPD | NEED_PLT);
760 dynrel_type = R_PARISC_FPTR64;
761 break;
763 /* Add more cases as needed. */
766 if (!need_entry)
767 continue;
769 if (hh)
771 /* Stash away enough information to be able to find this symbol
772 regardless of whether or not it is local or global. */
773 hh->owner = abfd;
774 hh->sym_indx = r_symndx;
777 /* Create what's needed. */
778 if (need_entry & NEED_DLT)
780 /* Allocate space for a DLT entry, as well as a dynamic
781 relocation for this entry. */
782 if (! hppa_info->dlt_sec
783 && ! get_dlt (abfd, info, hppa_info))
784 goto err_out;
786 if (hh != NULL)
788 hh->want_dlt = 1;
789 hh->eh.got.refcount += 1;
791 else
793 bfd_signed_vma *local_dlt_refcounts;
795 /* This is a DLT entry for a local symbol. */
796 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
797 if (local_dlt_refcounts == NULL)
798 return false;
799 local_dlt_refcounts[r_symndx] += 1;
803 if (need_entry & NEED_PLT)
805 if (! hppa_info->root.splt
806 && ! get_plt (abfd, info, hppa_info))
807 goto err_out;
809 if (hh != NULL)
811 hh->want_plt = 1;
812 hh->eh.needs_plt = 1;
813 hh->eh.plt.refcount += 1;
815 else
817 bfd_signed_vma *local_dlt_refcounts;
818 bfd_signed_vma *local_plt_refcounts;
820 /* This is a PLT entry for a local symbol. */
821 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
822 if (local_dlt_refcounts == NULL)
823 return false;
824 local_plt_refcounts = local_dlt_refcounts + symtab_hdr->sh_info;
825 local_plt_refcounts[r_symndx] += 1;
829 if (need_entry & NEED_STUB)
831 if (! hppa_info->stub_sec
832 && ! get_stub (abfd, info, hppa_info))
833 goto err_out;
834 if (hh)
835 hh->want_stub = 1;
838 if (need_entry & NEED_OPD)
840 if (! hppa_info->opd_sec
841 && ! get_opd (abfd, info, hppa_info))
842 goto err_out;
844 /* FPTRs are not allocated by the dynamic linker for PA64,
845 though it is possible that will change in the future. */
847 if (hh != NULL)
848 hh->want_opd = 1;
849 else
851 bfd_signed_vma *local_dlt_refcounts;
852 bfd_signed_vma *local_opd_refcounts;
854 /* This is a OPD for a local symbol. */
855 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
856 if (local_dlt_refcounts == NULL)
857 return false;
858 local_opd_refcounts = (local_dlt_refcounts
859 + 2 * symtab_hdr->sh_info);
860 local_opd_refcounts[r_symndx] += 1;
864 /* Add a new dynamic relocation to the chain of dynamic
865 relocations for this symbol. */
866 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
868 if (! hppa_info->other_rel_sec
869 && ! get_reloc_section (abfd, hppa_info, sec))
870 goto err_out;
872 /* Count dynamic relocations against global symbols. */
873 if (hh != NULL
874 && !count_dyn_reloc (abfd, hh, dynrel_type, sec,
875 sec_symndx, rel->r_offset, rel->r_addend))
876 goto err_out;
878 /* If we are building a shared library and we just recorded
879 a dynamic R_PARISC_FPTR64 relocation, then make sure the
880 section symbol for this section ends up in the dynamic
881 symbol table. */
882 if (bfd_link_pic (info) && dynrel_type == R_PARISC_FPTR64
883 && ! (bfd_elf_link_record_local_dynamic_symbol
884 (info, abfd, sec_symndx)))
885 return false;
889 return true;
891 err_out:
892 return false;
895 struct elf64_hppa_allocate_data
897 struct bfd_link_info *info;
898 bfd_size_type ofs;
901 /* Should we do dynamic things to this symbol? */
903 static bool
904 elf64_hppa_dynamic_symbol_p (struct elf_link_hash_entry *eh,
905 struct bfd_link_info *info)
907 /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
908 and relocations that retrieve a function descriptor? Assume the
909 worst for now. */
910 if (_bfd_elf_dynamic_symbol_p (eh, info, 1))
912 /* ??? Why is this here and not elsewhere is_local_label_name. */
913 if (eh->root.root.string[0] == '$' && eh->root.root.string[1] == '$')
914 return false;
916 return true;
918 else
919 return false;
922 /* Mark all functions exported by this file so that we can later allocate
923 entries in .opd for them. */
925 static bool
926 elf64_hppa_mark_exported_functions (struct elf_link_hash_entry *eh, void *data)
928 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
929 struct bfd_link_info *info = (struct bfd_link_info *)data;
930 struct elf64_hppa_link_hash_table *hppa_info;
932 hppa_info = hppa_link_hash_table (info);
933 if (hppa_info == NULL)
934 return false;
936 if (eh
937 && (eh->root.type == bfd_link_hash_defined
938 || eh->root.type == bfd_link_hash_defweak)
939 && eh->root.u.def.section->output_section != NULL
940 && eh->type == STT_FUNC)
942 if (! hppa_info->opd_sec
943 && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
944 return false;
946 hh->want_opd = 1;
948 /* Put a flag here for output_symbol_hook. */
949 hh->st_shndx = -1;
950 eh->needs_plt = 1;
953 return true;
956 /* Allocate space for a DLT entry. */
958 static bool
959 allocate_global_data_dlt (struct elf_link_hash_entry *eh, void *data)
961 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
962 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
964 if (hh->want_dlt)
966 if (bfd_link_pic (x->info))
968 /* Possibly add the symbol to the local dynamic symbol
969 table since we might need to create a dynamic relocation
970 against it. */
971 if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
973 bfd *owner = eh->root.u.def.section->owner;
975 if (! (bfd_elf_link_record_local_dynamic_symbol
976 (x->info, owner, hh->sym_indx)))
977 return false;
981 hh->dlt_offset = x->ofs;
982 x->ofs += DLT_ENTRY_SIZE;
984 return true;
987 /* Allocate space for a DLT.PLT entry. */
989 static bool
990 allocate_global_data_plt (struct elf_link_hash_entry *eh, void *data)
992 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
993 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *) data;
995 if (hh->want_plt
996 && elf64_hppa_dynamic_symbol_p (eh, x->info)
997 && !((eh->root.type == bfd_link_hash_defined
998 || eh->root.type == bfd_link_hash_defweak)
999 && eh->root.u.def.section->output_section != NULL))
1001 hh->plt_offset = x->ofs;
1002 x->ofs += PLT_ENTRY_SIZE;
1003 if (hh->plt_offset < 0x2000)
1005 struct elf64_hppa_link_hash_table *hppa_info;
1007 hppa_info = hppa_link_hash_table (x->info);
1008 if (hppa_info == NULL)
1009 return false;
1011 hppa_info->gp_offset = hh->plt_offset;
1014 else
1015 hh->want_plt = 0;
1017 return true;
1020 /* Allocate space for a STUB entry. */
1022 static bool
1023 allocate_global_data_stub (struct elf_link_hash_entry *eh, void *data)
1025 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1026 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1028 if (hh->want_stub
1029 && elf64_hppa_dynamic_symbol_p (eh, x->info)
1030 && !((eh->root.type == bfd_link_hash_defined
1031 || eh->root.type == bfd_link_hash_defweak)
1032 && eh->root.u.def.section->output_section != NULL))
1034 hh->stub_offset = x->ofs;
1035 x->ofs += sizeof (plt_stub);
1037 else
1038 hh->want_stub = 0;
1039 return true;
1042 /* Allocate space for a FPTR entry. */
1044 static bool
1045 allocate_global_data_opd (struct elf_link_hash_entry *eh, void *data)
1047 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1048 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1050 if (hh && hh->want_opd)
1052 /* We never need an opd entry for a symbol which is not
1053 defined by this output file. */
1054 if (hh && (hh->eh.root.type == bfd_link_hash_undefined
1055 || hh->eh.root.type == bfd_link_hash_undefweak
1056 || hh->eh.root.u.def.section->output_section == NULL))
1057 hh->want_opd = 0;
1059 /* If we are creating a shared library, took the address of a local
1060 function or might export this function from this object file, then
1061 we have to create an opd descriptor. */
1062 else if (bfd_link_pic (x->info)
1063 || hh == NULL
1064 || (hh->eh.dynindx == -1 && hh->eh.type != STT_PARISC_MILLI)
1065 || (hh->eh.root.type == bfd_link_hash_defined
1066 || hh->eh.root.type == bfd_link_hash_defweak))
1068 /* If we are creating a shared library, then we will have to
1069 create a runtime relocation for the symbol to properly
1070 initialize the .opd entry. Make sure the symbol gets
1071 added to the dynamic symbol table. */
1072 if (bfd_link_pic (x->info)
1073 && (hh == NULL || (hh->eh.dynindx == -1)))
1075 bfd *owner;
1076 /* PR 6511: Default to using the dynamic symbol table. */
1077 owner = (hh->owner ? hh->owner: eh->root.u.def.section->owner);
1079 if (!bfd_elf_link_record_local_dynamic_symbol
1080 (x->info, owner, hh->sym_indx))
1081 return false;
1084 /* This may not be necessary or desirable anymore now that
1085 we have some support for dealing with section symbols
1086 in dynamic relocs. But name munging does make the result
1087 much easier to debug. ie, the EPLT reloc will reference
1088 a symbol like .foobar, instead of .text + offset. */
1089 if (bfd_link_pic (x->info) && eh)
1091 char *new_name;
1092 struct elf_link_hash_entry *nh;
1094 new_name = concat (".", eh->root.root.string, NULL);
1096 nh = elf_link_hash_lookup (elf_hash_table (x->info),
1097 new_name, true, true, true);
1099 free (new_name);
1100 nh->root.type = eh->root.type;
1101 nh->root.u.def.value = eh->root.u.def.value;
1102 nh->root.u.def.section = eh->root.u.def.section;
1104 if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
1105 return false;
1107 hh->opd_offset = x->ofs;
1108 x->ofs += OPD_ENTRY_SIZE;
1111 /* Otherwise we do not need an opd entry. */
1112 else
1113 hh->want_opd = 0;
1115 return true;
1118 /* HP requires the EI_OSABI field to be filled in. The assignment to
1119 EI_ABIVERSION may not be strictly necessary. */
1121 static bool
1122 elf64_hppa_init_file_header (bfd *abfd, struct bfd_link_info *info)
1124 Elf_Internal_Ehdr *i_ehdrp;
1126 if (!_bfd_elf_init_file_header (abfd, info))
1127 return false;
1129 i_ehdrp = elf_elfheader (abfd);
1130 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
1131 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
1132 return true;
1135 /* Create function descriptor section (.opd). This section is called .opd
1136 because it contains "official procedure descriptors". The "official"
1137 refers to the fact that these descriptors are used when taking the address
1138 of a procedure, thus ensuring a unique address for each procedure. */
1140 static bool
1141 get_opd (bfd *abfd,
1142 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1143 struct elf64_hppa_link_hash_table *hppa_info)
1145 asection *opd;
1146 bfd *dynobj;
1148 opd = hppa_info->opd_sec;
1149 if (!opd)
1151 dynobj = hppa_info->root.dynobj;
1152 if (!dynobj)
1153 hppa_info->root.dynobj = dynobj = abfd;
1155 opd = bfd_make_section_anyway_with_flags (dynobj, ".opd",
1156 (SEC_ALLOC
1157 | SEC_LOAD
1158 | SEC_HAS_CONTENTS
1159 | SEC_IN_MEMORY
1160 | SEC_LINKER_CREATED));
1161 if (!opd
1162 || !bfd_set_section_alignment (opd, 3))
1164 BFD_ASSERT (0);
1165 return false;
1168 hppa_info->opd_sec = opd;
1171 return true;
1174 /* Create the PLT section. */
1176 static bool
1177 get_plt (bfd *abfd,
1178 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1179 struct elf64_hppa_link_hash_table *hppa_info)
1181 asection *plt;
1182 bfd *dynobj;
1184 plt = hppa_info->root.splt;
1185 if (!plt)
1187 dynobj = hppa_info->root.dynobj;
1188 if (!dynobj)
1189 hppa_info->root.dynobj = dynobj = abfd;
1191 plt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
1192 (SEC_ALLOC
1193 | SEC_LOAD
1194 | SEC_HAS_CONTENTS
1195 | SEC_IN_MEMORY
1196 | SEC_LINKER_CREATED));
1197 if (!plt
1198 || !bfd_set_section_alignment (plt, 3))
1200 BFD_ASSERT (0);
1201 return false;
1204 hppa_info->root.splt = plt;
1207 return true;
1210 /* Create the DLT section. */
1212 static bool
1213 get_dlt (bfd *abfd,
1214 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1215 struct elf64_hppa_link_hash_table *hppa_info)
1217 asection *dlt;
1218 bfd *dynobj;
1220 dlt = hppa_info->dlt_sec;
1221 if (!dlt)
1223 dynobj = hppa_info->root.dynobj;
1224 if (!dynobj)
1225 hppa_info->root.dynobj = dynobj = abfd;
1227 dlt = bfd_make_section_anyway_with_flags (dynobj, ".dlt",
1228 (SEC_ALLOC
1229 | SEC_LOAD
1230 | SEC_HAS_CONTENTS
1231 | SEC_IN_MEMORY
1232 | SEC_LINKER_CREATED));
1233 if (!dlt
1234 || !bfd_set_section_alignment (dlt, 3))
1236 BFD_ASSERT (0);
1237 return false;
1240 hppa_info->dlt_sec = dlt;
1243 return true;
1246 /* Create the stubs section. */
1248 static bool
1249 get_stub (bfd *abfd,
1250 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1251 struct elf64_hppa_link_hash_table *hppa_info)
1253 asection *stub;
1254 bfd *dynobj;
1256 stub = hppa_info->stub_sec;
1257 if (!stub)
1259 dynobj = hppa_info->root.dynobj;
1260 if (!dynobj)
1261 hppa_info->root.dynobj = dynobj = abfd;
1263 stub = bfd_make_section_anyway_with_flags (dynobj, ".stub",
1264 (SEC_ALLOC | SEC_LOAD
1265 | SEC_HAS_CONTENTS
1266 | SEC_IN_MEMORY
1267 | SEC_READONLY
1268 | SEC_LINKER_CREATED));
1269 if (!stub
1270 || !bfd_set_section_alignment (stub, 3))
1272 BFD_ASSERT (0);
1273 return false;
1276 hppa_info->stub_sec = stub;
1279 return true;
1282 /* Create sections necessary for dynamic linking. This is only a rough
1283 cut and will likely change as we learn more about the somewhat
1284 unusual dynamic linking scheme HP uses.
1286 .stub:
1287 Contains code to implement cross-space calls. The first time one
1288 of the stubs is used it will call into the dynamic linker, later
1289 calls will go straight to the target.
1291 The only stub we support right now looks like
1293 ldd OFFSET(%dp),%r1
1294 bve %r0(%r1)
1295 ldd OFFSET+8(%dp),%dp
1297 Other stubs may be needed in the future. We may want the remove
1298 the break/nop instruction. It is only used right now to keep the
1299 offset of a .plt entry and a .stub entry in sync.
1301 .dlt:
1302 This is what most people call the .got. HP used a different name.
1303 Losers.
1305 .rela.dlt:
1306 Relocations for the DLT.
1308 .plt:
1309 Function pointers as address,gp pairs.
1311 .rela.plt:
1312 Should contain dynamic IPLT (and EPLT?) relocations.
1314 .opd:
1315 FPTRS
1317 .rela.opd:
1318 EPLT relocations for symbols exported from shared libraries. */
1320 static bool
1321 elf64_hppa_create_dynamic_sections (bfd *abfd,
1322 struct bfd_link_info *info)
1324 asection *s;
1325 struct elf64_hppa_link_hash_table *hppa_info;
1327 hppa_info = hppa_link_hash_table (info);
1328 if (hppa_info == NULL)
1329 return false;
1331 if (! get_stub (abfd, info, hppa_info))
1332 return false;
1334 if (! get_dlt (abfd, info, hppa_info))
1335 return false;
1337 if (! get_plt (abfd, info, hppa_info))
1338 return false;
1340 if (! get_opd (abfd, info, hppa_info))
1341 return false;
1343 s = bfd_make_section_anyway_with_flags (abfd, ".rela.dlt",
1344 (SEC_ALLOC | SEC_LOAD
1345 | SEC_HAS_CONTENTS
1346 | SEC_IN_MEMORY
1347 | SEC_READONLY
1348 | SEC_LINKER_CREATED));
1349 if (s == NULL
1350 || !bfd_set_section_alignment (s, 3))
1351 return false;
1352 hppa_info->dlt_rel_sec = s;
1354 s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt",
1355 (SEC_ALLOC | SEC_LOAD
1356 | SEC_HAS_CONTENTS
1357 | SEC_IN_MEMORY
1358 | SEC_READONLY
1359 | SEC_LINKER_CREATED));
1360 if (s == NULL
1361 || !bfd_set_section_alignment (s, 3))
1362 return false;
1363 hppa_info->root.srelplt = s;
1365 s = bfd_make_section_anyway_with_flags (abfd, ".rela.data",
1366 (SEC_ALLOC | SEC_LOAD
1367 | SEC_HAS_CONTENTS
1368 | SEC_IN_MEMORY
1369 | SEC_READONLY
1370 | SEC_LINKER_CREATED));
1371 if (s == NULL
1372 || !bfd_set_section_alignment (s, 3))
1373 return false;
1374 hppa_info->other_rel_sec = s;
1376 s = bfd_make_section_anyway_with_flags (abfd, ".rela.opd",
1377 (SEC_ALLOC | SEC_LOAD
1378 | SEC_HAS_CONTENTS
1379 | SEC_IN_MEMORY
1380 | SEC_READONLY
1381 | SEC_LINKER_CREATED));
1382 if (s == NULL
1383 || !bfd_set_section_alignment (s, 3))
1384 return false;
1385 hppa_info->opd_rel_sec = s;
1387 return true;
1390 /* Allocate dynamic relocations for those symbols that turned out
1391 to be dynamic. */
1393 static bool
1394 allocate_dynrel_entries (struct elf_link_hash_entry *eh, void *data)
1396 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1397 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1398 struct elf64_hppa_link_hash_table *hppa_info;
1399 struct elf64_hppa_dyn_reloc_entry *rent;
1400 bool dynamic_symbol, shared;
1402 hppa_info = hppa_link_hash_table (x->info);
1403 if (hppa_info == NULL)
1404 return false;
1406 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, x->info);
1407 shared = bfd_link_pic (x->info);
1409 /* We may need to allocate relocations for a non-dynamic symbol
1410 when creating a shared library. */
1411 if (!dynamic_symbol && !shared)
1412 return true;
1414 /* Take care of the normal data relocations. */
1416 for (rent = hh->reloc_entries; rent; rent = rent->next)
1418 /* Allocate one iff we are building a shared library, the relocation
1419 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
1420 if (!shared && rent->type == R_PARISC_FPTR64 && hh->want_opd)
1421 continue;
1423 hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
1425 /* Make sure this symbol gets into the dynamic symbol table if it is
1426 not already recorded. ?!? This should not be in the loop since
1427 the symbol need only be added once. */
1428 if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
1429 if (!bfd_elf_link_record_local_dynamic_symbol
1430 (x->info, rent->sec->owner, hh->sym_indx))
1431 return false;
1434 /* Take care of the GOT and PLT relocations. */
1436 if ((dynamic_symbol || shared) && hh->want_dlt)
1437 hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
1439 /* If we are building a shared library, then every symbol that has an
1440 opd entry will need an EPLT relocation to relocate the symbol's address
1441 and __gp value based on the runtime load address. */
1442 if (shared && hh->want_opd)
1443 hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
1445 if (hh->want_plt && dynamic_symbol)
1447 bfd_size_type t = 0;
1449 /* Dynamic symbols get one IPLT relocation. Local symbols in
1450 shared libraries get two REL relocations. Local symbols in
1451 main applications get nothing. */
1452 if (dynamic_symbol)
1453 t = sizeof (Elf64_External_Rela);
1454 else if (shared)
1455 t = 2 * sizeof (Elf64_External_Rela);
1457 hppa_info->root.srelplt->size += t;
1460 return true;
1463 /* Adjust a symbol defined by a dynamic object and referenced by a
1464 regular object. */
1466 static bool
1467 elf64_hppa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1468 struct elf_link_hash_entry *eh)
1470 /* ??? Undefined symbols with PLT entries should be re-defined
1471 to be the PLT entry. */
1473 /* If this is a weak symbol, and there is a real definition, the
1474 processor independent code will have arranged for us to see the
1475 real definition first, and we can just use the same value. */
1476 if (eh->is_weakalias)
1478 struct elf_link_hash_entry *def = weakdef (eh);
1479 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1480 eh->root.u.def.section = def->root.u.def.section;
1481 eh->root.u.def.value = def->root.u.def.value;
1482 return true;
1485 /* If this is a reference to a symbol defined by a dynamic object which
1486 is not a function, we might allocate the symbol in our .dynbss section
1487 and allocate a COPY dynamic relocation.
1489 But PA64 code is canonically PIC, so as a rule we can avoid this sort
1490 of hackery. */
1492 return true;
1495 /* This function is called via elf_link_hash_traverse to mark millicode
1496 symbols with a dynindx of -1 and to remove the string table reference
1497 from the dynamic symbol table. If the symbol is not a millicode symbol,
1498 elf64_hppa_mark_exported_functions is called. */
1500 static bool
1501 elf64_hppa_mark_milli_and_exported_functions (struct elf_link_hash_entry *eh,
1502 void *data)
1504 struct bfd_link_info *info = (struct bfd_link_info *) data;
1506 if (eh->type == STT_PARISC_MILLI)
1508 if (eh->dynindx != -1)
1510 eh->dynindx = -1;
1511 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1512 eh->dynstr_index);
1514 return true;
1517 return elf64_hppa_mark_exported_functions (eh, data);
1520 /* Set the final sizes of the dynamic sections and allocate memory for
1521 the contents of our special sections. */
1523 static bool
1524 elf64_hppa_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1526 struct elf64_hppa_link_hash_table *hppa_info;
1527 struct elf64_hppa_allocate_data data;
1528 bfd *dynobj;
1529 bfd *ibfd;
1530 asection *sec;
1531 bool relocs;
1533 hppa_info = hppa_link_hash_table (info);
1534 if (hppa_info == NULL)
1535 return false;
1537 dynobj = hppa_info->root.dynobj;
1538 if (dynobj == NULL)
1539 return true;
1541 /* Mark each function this program exports so that we will allocate
1542 space in the .opd section for each function's FPTR. If we are
1543 creating dynamic sections, change the dynamic index of millicode
1544 symbols to -1 and remove them from the string table for .dynstr.
1546 We have to traverse the main linker hash table since we have to
1547 find functions which may not have been mentioned in any relocs. */
1548 elf_link_hash_traverse (&hppa_info->root,
1549 (hppa_info->root.dynamic_sections_created
1550 ? elf64_hppa_mark_milli_and_exported_functions
1551 : elf64_hppa_mark_exported_functions),
1552 info);
1554 if (hppa_info->root.dynamic_sections_created)
1556 /* Set the contents of the .interp section to the interpreter. */
1557 if (bfd_link_executable (info) && !info->nointerp)
1559 sec = bfd_get_linker_section (dynobj, ".interp");
1560 BFD_ASSERT (sec != NULL);
1561 sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
1562 sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
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;
1816 if (hppa_info->root.dynamic_sections_created)
1818 /* Always create a DT_PLTGOT. It actually has nothing to do with
1819 the PLT, it is how we communicate the __gp value of a load
1820 module to the dynamic linker. */
1821 #define add_dynamic_entry(TAG, VAL) \
1822 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1824 if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0))
1825 return false;
1827 /* Add some entries to the .dynamic section. We fill in the
1828 values later, in elf64_hppa_finish_dynamic_sections, but we
1829 must add the entries now so that we get the correct size for
1830 the .dynamic section. The DT_DEBUG entry is filled in by the
1831 dynamic linker and used by the debugger. */
1832 if (! bfd_link_pic (info))
1834 if (!add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1835 || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
1836 return false;
1839 /* Force DT_FLAGS to always be set.
1840 Required by HPUX 11.00 patch PHSS_26559.
1841 PR 30743: But do not set them for non-HPUX targets. */
1842 if (output_bfd->xvec == & hppa_elf64_vec)
1844 if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1845 return false;
1848 #undef add_dynamic_entry
1850 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
1853 /* Called after we have output the symbol into the dynamic symbol
1854 table, but before we output the symbol into the normal symbol
1855 table.
1857 For some symbols we had to change their address when outputting
1858 the dynamic symbol table. We undo that change here so that
1859 the symbols have their expected value in the normal symbol
1860 table. Ick. */
1862 static int
1863 elf64_hppa_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1864 const char *name,
1865 Elf_Internal_Sym *sym,
1866 asection *input_sec ATTRIBUTE_UNUSED,
1867 struct elf_link_hash_entry *eh)
1869 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1871 /* We may be called with the file symbol or section symbols.
1872 They never need munging, so it is safe to ignore them. */
1873 if (!name || !eh)
1874 return 1;
1876 /* Function symbols for which we created .opd entries *may* have been
1877 munged by finish_dynamic_symbol and have to be un-munged here.
1879 Note that finish_dynamic_symbol sometimes turns dynamic symbols
1880 into non-dynamic ones, so we initialize st_shndx to -1 in
1881 mark_exported_functions and check to see if it was overwritten
1882 here instead of just checking eh->dynindx. */
1883 if (hh->want_opd && hh->st_shndx != -1)
1885 /* Restore the saved value and section index. */
1886 sym->st_value = hh->st_value;
1887 sym->st_shndx = hh->st_shndx;
1890 return 1;
1893 /* Finish up dynamic symbol handling. We set the contents of various
1894 dynamic sections here. */
1896 static bool
1897 elf64_hppa_finish_dynamic_symbol (bfd *output_bfd,
1898 struct bfd_link_info *info,
1899 struct elf_link_hash_entry *eh,
1900 Elf_Internal_Sym *sym)
1902 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1903 asection *stub, *splt, *sopd, *spltrel;
1904 struct elf64_hppa_link_hash_table *hppa_info;
1906 hppa_info = hppa_link_hash_table (info);
1908 stub = hppa_info->stub_sec;
1909 splt = hppa_info->root.splt;
1910 sopd = hppa_info->opd_sec;
1911 spltrel = hppa_info->root.srelplt;
1913 /* Incredible. It is actually necessary to NOT use the symbol's real
1914 value when building the dynamic symbol table for a shared library.
1915 At least for symbols that refer to functions.
1917 We will store a new value and section index into the symbol long
1918 enough to output it into the dynamic symbol table, then we restore
1919 the original values (in elf64_hppa_link_output_symbol_hook). */
1920 if (hh->want_opd)
1922 BFD_ASSERT (sopd != NULL);
1924 /* Save away the original value and section index so that we
1925 can restore them later. */
1926 hh->st_value = sym->st_value;
1927 hh->st_shndx = sym->st_shndx;
1929 /* For the dynamic symbol table entry, we want the value to be
1930 address of this symbol's entry within the .opd section. */
1931 sym->st_value = (hh->opd_offset
1932 + sopd->output_offset
1933 + sopd->output_section->vma);
1934 sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1935 sopd->output_section);
1938 /* Initialize a .plt entry if requested. */
1939 if (hh->want_plt
1940 && elf64_hppa_dynamic_symbol_p (eh, info))
1942 bfd_vma value;
1943 Elf_Internal_Rela rel;
1944 bfd_byte *loc;
1946 BFD_ASSERT (splt != NULL && spltrel != NULL);
1948 /* We do not actually care about the value in the PLT entry
1949 if we are creating a shared library and the symbol is
1950 still undefined, we create a dynamic relocation to fill
1951 in the correct value. */
1952 if (bfd_link_pic (info) && eh->root.type == bfd_link_hash_undefined)
1953 value = 0;
1954 else
1955 value = (eh->root.u.def.value + eh->root.u.def.section->vma);
1957 /* Fill in the entry in the procedure linkage table.
1959 The format of a plt entry is
1960 <funcaddr> <__gp>.
1962 plt_offset is the offset within the PLT section at which to
1963 install the PLT entry.
1965 We are modifying the in-memory PLT contents here, so we do not add
1966 in the output_offset of the PLT section. */
1968 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset);
1969 value = _bfd_get_gp_value (info->output_bfd);
1970 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset + 0x8);
1972 /* Create a dynamic IPLT relocation for this entry.
1974 We are creating a relocation in the output file's PLT section,
1975 which is included within the DLT secton. So we do need to include
1976 the PLT's output_offset in the computation of the relocation's
1977 address. */
1978 rel.r_offset = (hh->plt_offset + splt->output_offset
1979 + splt->output_section->vma);
1980 rel.r_info = ELF64_R_INFO (hh->eh.dynindx, R_PARISC_IPLT);
1981 rel.r_addend = 0;
1983 loc = spltrel->contents;
1984 loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
1985 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
1988 /* Initialize an external call stub entry if requested. */
1989 if (hh->want_stub
1990 && elf64_hppa_dynamic_symbol_p (eh, info))
1992 bfd_vma value;
1993 int insn;
1994 unsigned int max_offset;
1996 BFD_ASSERT (stub != NULL);
1998 /* Install the generic stub template.
2000 We are modifying the contents of the stub section, so we do not
2001 need to include the stub section's output_offset here. */
2002 memcpy (stub->contents + hh->stub_offset, plt_stub, sizeof (plt_stub));
2004 /* Fix up the first ldd instruction.
2006 We are modifying the contents of the STUB section in memory,
2007 so we do not need to include its output offset in this computation.
2009 Note the plt_offset value is the value of the PLT entry relative to
2010 the start of the PLT section. These instructions will reference
2011 data relative to the value of __gp, which may not necessarily have
2012 the same address as the start of the PLT section.
2014 gp_offset contains the offset of __gp within the PLT section. */
2015 value = hh->plt_offset - hppa_info->gp_offset;
2017 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset);
2018 if (output_bfd->arch_info->mach >= 25)
2020 /* Wide mode allows 16 bit offsets. */
2021 max_offset = 32768;
2022 insn &= ~ 0xfff1;
2023 insn |= re_assemble_16 ((int) value);
2025 else
2027 max_offset = 8192;
2028 insn &= ~ 0x3ff1;
2029 insn |= re_assemble_14 ((int) value);
2032 if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2034 _bfd_error_handler
2035 /* xgettext:c-format */
2036 (_("stub entry for %s cannot load .plt, dp offset = %" PRId64),
2037 hh->eh.root.root.string, (int64_t) value);
2038 return false;
2041 bfd_put_32 (stub->owner, (bfd_vma) insn,
2042 stub->contents + hh->stub_offset);
2044 /* Fix up the second ldd instruction. */
2045 value += 8;
2046 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset + 8);
2047 if (output_bfd->arch_info->mach >= 25)
2049 insn &= ~ 0xfff1;
2050 insn |= re_assemble_16 ((int) value);
2052 else
2054 insn &= ~ 0x3ff1;
2055 insn |= re_assemble_14 ((int) value);
2057 bfd_put_32 (stub->owner, (bfd_vma) insn,
2058 stub->contents + hh->stub_offset + 8);
2061 return true;
2064 /* The .opd section contains FPTRs for each function this file
2065 exports. Initialize the FPTR entries. */
2067 static bool
2068 elf64_hppa_finalize_opd (struct elf_link_hash_entry *eh, void *data)
2070 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2071 struct bfd_link_info *info = (struct bfd_link_info *)data;
2072 struct elf64_hppa_link_hash_table *hppa_info;
2073 asection *sopd;
2074 asection *sopdrel;
2076 hppa_info = hppa_link_hash_table (info);
2077 if (hppa_info == NULL)
2078 return false;
2080 sopd = hppa_info->opd_sec;
2081 sopdrel = hppa_info->opd_rel_sec;
2083 if (hh->want_opd)
2085 bfd_vma value;
2087 /* The first two words of an .opd entry are zero.
2089 We are modifying the contents of the OPD section in memory, so we
2090 do not need to include its output offset in this computation. */
2091 memset (sopd->contents + hh->opd_offset, 0, 16);
2093 value = (eh->root.u.def.value
2094 + eh->root.u.def.section->output_section->vma
2095 + eh->root.u.def.section->output_offset);
2097 /* The next word is the address of the function. */
2098 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 16);
2100 /* The last word is our local __gp value. */
2101 value = _bfd_get_gp_value (info->output_bfd);
2102 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 24);
2105 /* If we are generating a shared library, we must generate EPLT relocations
2106 for each entry in the .opd, even for static functions (they may have
2107 had their address taken). */
2108 if (bfd_link_pic (info) && hh->want_opd)
2110 Elf_Internal_Rela rel;
2111 bfd_byte *loc;
2112 int dynindx;
2114 /* We may need to do a relocation against a local symbol, in
2115 which case we have to look up it's dynamic symbol index off
2116 the local symbol hash table. */
2117 if (eh->dynindx != -1)
2118 dynindx = eh->dynindx;
2119 else
2120 dynindx
2121 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2122 hh->sym_indx);
2124 /* The offset of this relocation is the absolute address of the
2125 .opd entry for this symbol. */
2126 rel.r_offset = (hh->opd_offset + sopd->output_offset
2127 + sopd->output_section->vma);
2129 /* If H is non-null, then we have an external symbol.
2131 It is imperative that we use a different dynamic symbol for the
2132 EPLT relocation if the symbol has global scope.
2134 In the dynamic symbol table, the function symbol will have a value
2135 which is address of the function's .opd entry.
2137 Thus, we can not use that dynamic symbol for the EPLT relocation
2138 (if we did, the data in the .opd would reference itself rather
2139 than the actual address of the function). Instead we have to use
2140 a new dynamic symbol which has the same value as the original global
2141 function symbol.
2143 We prefix the original symbol with a "." and use the new symbol in
2144 the EPLT relocation. This new symbol has already been recorded in
2145 the symbol table, we just have to look it up and use it.
2147 We do not have such problems with static functions because we do
2148 not make their addresses in the dynamic symbol table point to
2149 the .opd entry. Ultimately this should be safe since a static
2150 function can not be directly referenced outside of its shared
2151 library.
2153 We do have to play similar games for FPTR relocations in shared
2154 libraries, including those for static symbols. See the FPTR
2155 handling in elf64_hppa_finalize_dynreloc. */
2156 if (eh)
2158 char *new_name;
2159 struct elf_link_hash_entry *nh;
2161 new_name = concat (".", eh->root.root.string, NULL);
2163 nh = elf_link_hash_lookup (elf_hash_table (info),
2164 new_name, true, true, false);
2166 /* All we really want from the new symbol is its dynamic
2167 symbol index. */
2168 if (nh)
2169 dynindx = nh->dynindx;
2170 free (new_name);
2173 rel.r_addend = 0;
2174 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2176 loc = sopdrel->contents;
2177 loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2178 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2180 return true;
2183 /* The .dlt section contains addresses for items referenced through the
2184 dlt. Note that we can have a DLTIND relocation for a local symbol, thus
2185 we can not depend on finish_dynamic_symbol to initialize the .dlt. */
2187 static bool
2188 elf64_hppa_finalize_dlt (struct elf_link_hash_entry *eh, void *data)
2190 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2191 struct bfd_link_info *info = (struct bfd_link_info *)data;
2192 struct elf64_hppa_link_hash_table *hppa_info;
2193 asection *sdlt, *sdltrel;
2195 hppa_info = hppa_link_hash_table (info);
2196 if (hppa_info == NULL)
2197 return false;
2199 sdlt = hppa_info->dlt_sec;
2200 sdltrel = hppa_info->dlt_rel_sec;
2202 /* H/DYN_H may refer to a local variable and we know it's
2203 address, so there is no need to create a relocation. Just install
2204 the proper value into the DLT, note this shortcut can not be
2205 skipped when building a shared library. */
2206 if (! bfd_link_pic (info) && hh && hh->want_dlt)
2208 bfd_vma value;
2210 /* If we had an LTOFF_FPTR style relocation we want the DLT entry
2211 to point to the FPTR entry in the .opd section.
2213 We include the OPD's output offset in this computation as
2214 we are referring to an absolute address in the resulting
2215 object file. */
2216 if (hh->want_opd)
2218 value = (hh->opd_offset
2219 + hppa_info->opd_sec->output_offset
2220 + hppa_info->opd_sec->output_section->vma);
2222 else if ((eh->root.type == bfd_link_hash_defined
2223 || eh->root.type == bfd_link_hash_defweak)
2224 && eh->root.u.def.section)
2226 value = eh->root.u.def.value + eh->root.u.def.section->output_offset;
2227 if (eh->root.u.def.section->output_section)
2228 value += eh->root.u.def.section->output_section->vma;
2229 else
2230 value += eh->root.u.def.section->vma;
2232 else
2233 /* We have an undefined function reference. */
2234 value = 0;
2236 /* We do not need to include the output offset of the DLT section
2237 here because we are modifying the in-memory contents. */
2238 bfd_put_64 (sdlt->owner, value, sdlt->contents + hh->dlt_offset);
2241 /* Create a relocation for the DLT entry associated with this symbol.
2242 When building a shared library the symbol does not have to be dynamic. */
2243 if (hh->want_dlt
2244 && (elf64_hppa_dynamic_symbol_p (eh, info) || bfd_link_pic (info)))
2246 Elf_Internal_Rela rel;
2247 bfd_byte *loc;
2248 int dynindx;
2250 /* We may need to do a relocation against a local symbol, in
2251 which case we have to look up it's dynamic symbol index off
2252 the local symbol hash table. */
2253 if (eh && eh->dynindx != -1)
2254 dynindx = eh->dynindx;
2255 else
2256 dynindx
2257 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2258 hh->sym_indx);
2260 /* Create a dynamic relocation for this entry. Do include the output
2261 offset of the DLT entry since we need an absolute address in the
2262 resulting object file. */
2263 rel.r_offset = (hh->dlt_offset + sdlt->output_offset
2264 + sdlt->output_section->vma);
2265 if (eh && eh->type == STT_FUNC)
2266 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2267 else
2268 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2269 rel.r_addend = 0;
2271 loc = sdltrel->contents;
2272 loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2273 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2275 return true;
2278 /* Finalize the dynamic relocations. Specifically the FPTR relocations
2279 for dynamic functions used to initialize static data. */
2281 static bool
2282 elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
2283 void *data)
2285 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2286 struct bfd_link_info *info = (struct bfd_link_info *)data;
2287 struct elf64_hppa_link_hash_table *hppa_info;
2288 int dynamic_symbol;
2290 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, info);
2292 if (!dynamic_symbol && !bfd_link_pic (info))
2293 return true;
2295 if (hh->reloc_entries)
2297 struct elf64_hppa_dyn_reloc_entry *rent;
2298 int dynindx;
2300 hppa_info = hppa_link_hash_table (info);
2301 if (hppa_info == NULL)
2302 return false;
2304 /* We may need to do a relocation against a local symbol, in
2305 which case we have to look up it's dynamic symbol index off
2306 the local symbol hash table. */
2307 if (eh->dynindx != -1)
2308 dynindx = eh->dynindx;
2309 else
2310 dynindx
2311 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2312 hh->sym_indx);
2314 for (rent = hh->reloc_entries; rent; rent = rent->next)
2316 Elf_Internal_Rela rel;
2317 bfd_byte *loc;
2319 /* Allocate one iff we are building a shared library, the relocation
2320 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
2321 if (!bfd_link_pic (info)
2322 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2323 continue;
2325 /* Create a dynamic relocation for this entry.
2327 We need the output offset for the reloc's section because
2328 we are creating an absolute address in the resulting object
2329 file. */
2330 rel.r_offset = (rent->offset + rent->sec->output_offset
2331 + rent->sec->output_section->vma);
2333 /* An FPTR64 relocation implies that we took the address of
2334 a function and that the function has an entry in the .opd
2335 section. We want the FPTR64 relocation to reference the
2336 entry in .opd.
2338 We could munge the symbol value in the dynamic symbol table
2339 (in fact we already do for functions with global scope) to point
2340 to the .opd entry. Then we could use that dynamic symbol in
2341 this relocation.
2343 Or we could do something sensible, not munge the symbol's
2344 address and instead just use a different symbol to reference
2345 the .opd entry. At least that seems sensible until you
2346 realize there's no local dynamic symbols we can use for that
2347 purpose. Thus the hair in the check_relocs routine.
2349 We use a section symbol recorded by check_relocs as the
2350 base symbol for the relocation. The addend is the difference
2351 between the section symbol and the address of the .opd entry. */
2352 if (bfd_link_pic (info)
2353 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2355 bfd_vma value, value2;
2357 /* First compute the address of the opd entry for this symbol. */
2358 value = (hh->opd_offset
2359 + hppa_info->opd_sec->output_section->vma
2360 + hppa_info->opd_sec->output_offset);
2362 /* Compute the value of the start of the section with
2363 the relocation. */
2364 value2 = (rent->sec->output_section->vma
2365 + rent->sec->output_offset);
2367 /* Compute the difference between the start of the section
2368 with the relocation and the opd entry. */
2369 value -= value2;
2371 /* The result becomes the addend of the relocation. */
2372 rel.r_addend = value;
2374 /* The section symbol becomes the symbol for the dynamic
2375 relocation. */
2376 dynindx
2377 = _bfd_elf_link_lookup_local_dynindx (info,
2378 rent->sec->owner,
2379 rent->sec_symndx);
2381 else
2382 rel.r_addend = rent->addend;
2384 rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2386 loc = hppa_info->other_rel_sec->contents;
2387 loc += (hppa_info->other_rel_sec->reloc_count++
2388 * sizeof (Elf64_External_Rela));
2389 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2393 return true;
2396 /* Used to decide how to sort relocs in an optimal manner for the
2397 dynamic linker, before writing them out. */
2399 static enum elf_reloc_type_class
2400 elf64_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2401 const asection *rel_sec ATTRIBUTE_UNUSED,
2402 const Elf_Internal_Rela *rela)
2404 if (ELF64_R_SYM (rela->r_info) == STN_UNDEF)
2405 return reloc_class_relative;
2407 switch ((int) ELF64_R_TYPE (rela->r_info))
2409 case R_PARISC_IPLT:
2410 return reloc_class_plt;
2411 case R_PARISC_COPY:
2412 return reloc_class_copy;
2413 default:
2414 return reloc_class_normal;
2418 /* Finish up the dynamic sections. */
2420 static bool
2421 elf64_hppa_finish_dynamic_sections (bfd *output_bfd,
2422 struct bfd_link_info *info)
2424 bfd *dynobj;
2425 asection *sdyn;
2426 struct elf64_hppa_link_hash_table *hppa_info;
2428 hppa_info = hppa_link_hash_table (info);
2429 if (hppa_info == NULL)
2430 return false;
2432 /* Finalize the contents of the .opd section. */
2433 elf_link_hash_traverse (elf_hash_table (info),
2434 elf64_hppa_finalize_opd,
2435 info);
2437 elf_link_hash_traverse (elf_hash_table (info),
2438 elf64_hppa_finalize_dynreloc,
2439 info);
2441 /* Finalize the contents of the .dlt section. */
2442 dynobj = elf_hash_table (info)->dynobj;
2443 /* Finalize the contents of the .dlt section. */
2444 elf_link_hash_traverse (elf_hash_table (info),
2445 elf64_hppa_finalize_dlt,
2446 info);
2448 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2450 if (elf_hash_table (info)->dynamic_sections_created)
2452 Elf64_External_Dyn *dyncon, *dynconend;
2454 BFD_ASSERT (sdyn != NULL);
2456 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2457 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2458 for (; dyncon < dynconend; dyncon++)
2460 Elf_Internal_Dyn dyn;
2461 asection *s;
2463 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2465 switch (dyn.d_tag)
2467 default:
2468 break;
2470 case DT_HP_LOAD_MAP:
2471 /* Compute the absolute address of 16byte scratchpad area
2472 for the dynamic linker.
2474 By convention the linker script will allocate the scratchpad
2475 area at the start of the .data section. So all we have to
2476 to is find the start of the .data section. */
2477 s = bfd_get_section_by_name (output_bfd, ".data");
2478 if (!s)
2479 return false;
2480 dyn.d_un.d_ptr = s->vma;
2481 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2482 break;
2484 case DT_PLTGOT:
2485 /* HP's use PLTGOT to set the GOT register. */
2486 dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2487 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2488 break;
2490 case DT_JMPREL:
2491 s = hppa_info->root.srelplt;
2492 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2493 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2494 break;
2496 case DT_PLTRELSZ:
2497 s = hppa_info->root.srelplt;
2498 dyn.d_un.d_val = s->size;
2499 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2500 break;
2502 case DT_RELA:
2503 s = hppa_info->other_rel_sec;
2504 if (! s || ! s->size)
2505 s = hppa_info->dlt_rel_sec;
2506 if (! s || ! s->size)
2507 s = hppa_info->opd_rel_sec;
2508 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2509 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2510 break;
2512 case DT_RELASZ:
2513 s = hppa_info->other_rel_sec;
2514 dyn.d_un.d_val = s->size;
2515 s = hppa_info->dlt_rel_sec;
2516 dyn.d_un.d_val += s->size;
2517 s = hppa_info->opd_rel_sec;
2518 dyn.d_un.d_val += s->size;
2519 /* There is some question about whether or not the size of
2520 the PLT relocs should be included here. HP's tools do
2521 it, so we'll emulate them. */
2522 s = hppa_info->root.srelplt;
2523 dyn.d_un.d_val += s->size;
2524 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2525 break;
2531 return true;
2534 /* Support for core dump NOTE sections. */
2536 static bool
2537 elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2539 int offset;
2540 size_t size;
2542 switch (note->descsz)
2544 default:
2545 return false;
2547 case 760: /* Linux/hppa */
2548 /* pr_cursig */
2549 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2551 /* pr_pid */
2552 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2554 /* pr_reg */
2555 offset = 112;
2556 size = 640;
2558 break;
2561 /* Make a ".reg/999" section. */
2562 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2563 size, note->descpos + offset);
2566 static bool
2567 elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2569 char * command;
2570 int n;
2572 switch (note->descsz)
2574 default:
2575 return false;
2577 case 136: /* Linux/hppa elf_prpsinfo. */
2578 elf_tdata (abfd)->core->program
2579 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2580 elf_tdata (abfd)->core->command
2581 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2584 /* Note that for some reason, a spurious space is tacked
2585 onto the end of the args in some (at least one anyway)
2586 implementations, so strip it off if it exists. */
2587 command = elf_tdata (abfd)->core->command;
2588 n = strlen (command);
2590 if (0 < n && command[n - 1] == ' ')
2591 command[n - 1] = '\0';
2593 return true;
2596 /* Return the number of additional phdrs we will need.
2598 The generic ELF code only creates PT_PHDRs for executables. The HP
2599 dynamic linker requires PT_PHDRs for dynamic libraries too.
2601 This routine indicates that the backend needs one additional program
2602 header for that case.
2604 Note we do not have access to the link info structure here, so we have
2605 to guess whether or not we are building a shared library based on the
2606 existence of a .interp section. */
2608 static int
2609 elf64_hppa_additional_program_headers (bfd *abfd,
2610 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2612 asection *s;
2614 /* If we are creating a shared library, then we have to create a
2615 PT_PHDR segment. HP's dynamic linker chokes without it. */
2616 s = bfd_get_section_by_name (abfd, ".interp");
2617 if (! s)
2618 return 1;
2619 return 0;
2622 static bool
2623 elf64_hppa_allow_non_load_phdr (bfd *abfd ATTRIBUTE_UNUSED,
2624 const Elf_Internal_Phdr *phdr ATTRIBUTE_UNUSED,
2625 unsigned int count ATTRIBUTE_UNUSED)
2627 return true;
2630 /* Allocate and initialize any program headers required by this
2631 specific backend.
2633 The generic ELF code only creates PT_PHDRs for executables. The HP
2634 dynamic linker requires PT_PHDRs for dynamic libraries too.
2636 This allocates the PT_PHDR and initializes it in a manner suitable
2637 for the HP linker.
2639 Note we do not have access to the link info structure here, so we have
2640 to guess whether or not we are building a shared library based on the
2641 existence of a .interp section. */
2643 static bool
2644 elf64_hppa_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
2646 struct elf_segment_map *m;
2648 m = elf_seg_map (abfd);
2649 if (info != NULL && !info->user_phdrs && m != NULL && m->p_type != PT_PHDR)
2651 m = ((struct elf_segment_map *)
2652 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
2653 if (m == NULL)
2654 return false;
2656 m->p_type = PT_PHDR;
2657 m->p_flags = PF_R | PF_X;
2658 m->p_flags_valid = 1;
2659 m->p_paddr_valid = 1;
2660 m->includes_phdrs = 1;
2662 m->next = elf_seg_map (abfd);
2663 elf_seg_map (abfd) = m;
2666 for (m = elf_seg_map (abfd) ; m != NULL; m = m->next)
2667 if (m->p_type == PT_LOAD)
2669 unsigned int i;
2671 for (i = 0; i < m->count; i++)
2673 /* The code "hint" is not really a hint. It is a requirement
2674 for certain versions of the HP dynamic linker. Worse yet,
2675 it must be set even if the shared library does not have
2676 any code in its "text" segment (thus the check for .hash
2677 to catch this situation). */
2678 if (m->sections[i]->flags & SEC_CODE
2679 || (strcmp (m->sections[i]->name, ".hash") == 0))
2680 m->p_flags |= (PF_X | PF_HP_CODE);
2684 return true;
2687 /* Called when writing out an object file to decide the type of a
2688 symbol. */
2689 static int
2690 elf64_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym,
2691 int type)
2693 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2694 return STT_PARISC_MILLI;
2695 else
2696 return type;
2699 /* Support HP specific sections for core files. */
2701 static bool
2702 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int sec_index,
2703 const char *typename)
2705 if (hdr->p_type == PT_HP_CORE_KERNEL)
2707 asection *sect;
2709 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2710 return false;
2712 sect = bfd_make_section_anyway (abfd, ".kernel");
2713 if (sect == NULL)
2714 return false;
2715 sect->size = hdr->p_filesz;
2716 sect->filepos = hdr->p_offset;
2717 sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2718 return true;
2721 if (hdr->p_type == PT_HP_CORE_PROC)
2723 int sig;
2725 if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2726 return false;
2727 if (bfd_read (&sig, 4, abfd) != 4)
2728 return false;
2730 elf_tdata (abfd)->core->signal = sig;
2732 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2733 return false;
2735 /* GDB uses the ".reg" section to read register contents. */
2736 return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2737 hdr->p_offset);
2740 if (hdr->p_type == PT_HP_CORE_LOADABLE
2741 || hdr->p_type == PT_HP_CORE_STACK
2742 || hdr->p_type == PT_HP_CORE_MMF)
2743 hdr->p_type = PT_LOAD;
2745 return _bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename);
2748 /* Hook called by the linker routine which adds symbols from an object
2749 file. HP's libraries define symbols with HP specific section
2750 indices, which we have to handle. */
2752 static bool
2753 elf_hppa_add_symbol_hook (bfd *abfd,
2754 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2755 Elf_Internal_Sym *sym,
2756 const char **namep ATTRIBUTE_UNUSED,
2757 flagword *flagsp ATTRIBUTE_UNUSED,
2758 asection **secp,
2759 bfd_vma *valp)
2761 unsigned int sec_index = sym->st_shndx;
2763 switch (sec_index)
2765 case SHN_PARISC_ANSI_COMMON:
2766 *secp = bfd_make_section_old_way (abfd, ".PARISC.ansi.common");
2767 (*secp)->flags |= SEC_IS_COMMON;
2768 *valp = sym->st_size;
2769 break;
2771 case SHN_PARISC_HUGE_COMMON:
2772 *secp = bfd_make_section_old_way (abfd, ".PARISC.huge.common");
2773 (*secp)->flags |= SEC_IS_COMMON;
2774 *valp = sym->st_size;
2775 break;
2778 return true;
2781 static bool
2782 elf_hppa_unmark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2783 void *data)
2785 struct bfd_link_info *info = data;
2787 /* If we are not creating a shared library, and this symbol is
2788 referenced by a shared library but is not defined anywhere, then
2789 the generic code will warn that it is undefined.
2791 This behavior is undesirable on HPs since the standard shared
2792 libraries contain references to undefined symbols.
2794 So we twiddle the flags associated with such symbols so that they
2795 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2797 Ultimately we should have better controls over the generic ELF BFD
2798 linker code. */
2799 if (! bfd_link_relocatable (info)
2800 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2801 && h->root.type == bfd_link_hash_undefined
2802 && h->ref_dynamic
2803 && !h->ref_regular)
2805 h->ref_dynamic = 0;
2806 h->pointer_equality_needed = 1;
2809 return true;
2812 static bool
2813 elf_hppa_remark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2814 void *data)
2816 struct bfd_link_info *info = data;
2818 /* If we are not creating a shared library, and this symbol is
2819 referenced by a shared library but is not defined anywhere, then
2820 the generic code will warn that it is undefined.
2822 This behavior is undesirable on HPs since the standard shared
2823 libraries contain references to undefined symbols.
2825 So we twiddle the flags associated with such symbols so that they
2826 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2828 Ultimately we should have better controls over the generic ELF BFD
2829 linker code. */
2830 if (! bfd_link_relocatable (info)
2831 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2832 && h->root.type == bfd_link_hash_undefined
2833 && !h->ref_dynamic
2834 && !h->ref_regular
2835 && h->pointer_equality_needed)
2837 h->ref_dynamic = 1;
2838 h->pointer_equality_needed = 0;
2841 return true;
2844 static bool
2845 elf_hppa_is_dynamic_loader_symbol (const char *name)
2847 return (! strcmp (name, "__CPU_REVISION")
2848 || ! strcmp (name, "__CPU_KEYBITS_1")
2849 || ! strcmp (name, "__SYSTEM_ID_D")
2850 || ! strcmp (name, "__FPU_MODEL")
2851 || ! strcmp (name, "__FPU_REVISION")
2852 || ! strcmp (name, "__ARGC")
2853 || ! strcmp (name, "__ARGV")
2854 || ! strcmp (name, "__ENVP")
2855 || ! strcmp (name, "__TLS_SIZE_D")
2856 || ! strcmp (name, "__LOAD_INFO")
2857 || ! strcmp (name, "__systab"));
2860 /* Record the lowest address for the data and text segments. */
2861 static void
2862 elf_hppa_record_segment_addrs (bfd *abfd,
2863 asection *section,
2864 void *data)
2866 struct elf64_hppa_link_hash_table *hppa_info = data;
2868 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2870 bfd_vma value;
2871 Elf_Internal_Phdr *p;
2873 p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
2874 BFD_ASSERT (p != NULL);
2875 value = p->p_vaddr;
2877 if (section->flags & SEC_READONLY)
2879 if (value < hppa_info->text_segment_base)
2880 hppa_info->text_segment_base = value;
2882 else
2884 if (value < hppa_info->data_segment_base)
2885 hppa_info->data_segment_base = value;
2890 /* Called after we have seen all the input files/sections, but before
2891 final symbol resolution and section placement has been determined.
2893 We use this hook to (possibly) provide a value for __gp, then we
2894 fall back to the generic ELF final link routine. */
2896 static bool
2897 elf_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
2899 struct stat buf;
2900 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
2902 if (hppa_info == NULL)
2903 return false;
2905 if (! bfd_link_relocatable (info))
2907 struct elf_link_hash_entry *gp;
2908 bfd_vma gp_val;
2910 /* The linker script defines a value for __gp iff it was referenced
2911 by one of the objects being linked. First try to find the symbol
2912 in the hash table. If that fails, just compute the value __gp
2913 should have had. */
2914 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
2915 false, false);
2917 if (gp)
2920 /* Adjust the value of __gp as we may want to slide it into the
2921 .plt section so that the stubs can access PLT entries without
2922 using an addil sequence. */
2923 gp->root.u.def.value += hppa_info->gp_offset;
2925 gp_val = (gp->root.u.def.section->output_section->vma
2926 + gp->root.u.def.section->output_offset
2927 + gp->root.u.def.value);
2929 else
2931 asection *sec;
2933 /* First look for a .plt section. If found, then __gp is the
2934 address of the .plt + gp_offset.
2936 If no .plt is found, then look for .dlt, .opd and .data (in
2937 that order) and set __gp to the base address of whichever
2938 section is found first. */
2940 sec = hppa_info->root.splt;
2941 if (sec && ! (sec->flags & SEC_EXCLUDE))
2942 gp_val = (sec->output_offset
2943 + sec->output_section->vma
2944 + hppa_info->gp_offset);
2945 else
2947 sec = hppa_info->dlt_sec;
2948 if (!sec || (sec->flags & SEC_EXCLUDE))
2949 sec = hppa_info->opd_sec;
2950 if (!sec || (sec->flags & SEC_EXCLUDE))
2951 sec = bfd_get_section_by_name (abfd, ".data");
2952 if (!sec || (sec->flags & SEC_EXCLUDE))
2953 gp_val = 0;
2954 else
2955 gp_val = sec->output_offset + sec->output_section->vma;
2959 /* Install whatever value we found/computed for __gp. */
2960 _bfd_set_gp_value (abfd, gp_val);
2963 /* We need to know the base of the text and data segments so that we
2964 can perform SEGREL relocations. We will record the base addresses
2965 when we encounter the first SEGREL relocation. */
2966 hppa_info->text_segment_base = (bfd_vma)-1;
2967 hppa_info->data_segment_base = (bfd_vma)-1;
2969 /* HP's shared libraries have references to symbols that are not
2970 defined anywhere. The generic ELF BFD linker code will complain
2971 about such symbols.
2973 So we detect the losing case and arrange for the flags on the symbol
2974 to indicate that it was never referenced. This keeps the generic
2975 ELF BFD link code happy and appears to not create any secondary
2976 problems. Ultimately we need a way to control the behavior of the
2977 generic ELF BFD link code better. */
2978 elf_link_hash_traverse (elf_hash_table (info),
2979 elf_hppa_unmark_useless_dynamic_symbols,
2980 info);
2982 /* Invoke the regular ELF backend linker to do all the work. */
2983 if (!bfd_elf_final_link (abfd, info))
2984 return false;
2986 elf_link_hash_traverse (elf_hash_table (info),
2987 elf_hppa_remark_useless_dynamic_symbols,
2988 info);
2990 /* If we're producing a final executable, sort the contents of the
2991 unwind section. */
2992 if (bfd_link_relocatable (info))
2993 return true;
2995 /* Do not attempt to sort non-regular files. This is here
2996 especially for configure scripts and kernel builds which run
2997 tests with "ld [...] -o /dev/null". */
2998 if (stat (bfd_get_filename (abfd), &buf) != 0
2999 || !S_ISREG(buf.st_mode))
3000 return true;
3002 return elf_hppa_sort_unwind (abfd);
3005 /* Relocate the given INSN. VALUE should be the actual value we want
3006 to insert into the instruction, ie by this point we should not be
3007 concerned with computing an offset relative to the DLT, PC, etc.
3008 Instead this routine is meant to handle the bit manipulations needed
3009 to insert the relocation into the given instruction. */
3011 static int
3012 elf_hppa_relocate_insn (int insn, int sym_value, unsigned int r_type)
3014 switch (r_type)
3016 /* This is any 22 bit branch. In PA2.0 syntax it corresponds to
3017 the "B" instruction. */
3018 case R_PARISC_PCREL22F:
3019 case R_PARISC_PCREL22C:
3020 return (insn & ~0x3ff1ffd) | re_assemble_22 (sym_value);
3022 /* This is any 12 bit branch. */
3023 case R_PARISC_PCREL12F:
3024 return (insn & ~0x1ffd) | re_assemble_12 (sym_value);
3026 /* This is any 17 bit branch. In PA2.0 syntax it also corresponds
3027 to the "B" instruction as well as BE. */
3028 case R_PARISC_PCREL17F:
3029 case R_PARISC_DIR17F:
3030 case R_PARISC_DIR17R:
3031 case R_PARISC_PCREL17C:
3032 case R_PARISC_PCREL17R:
3033 return (insn & ~0x1f1ffd) | re_assemble_17 (sym_value);
3035 /* ADDIL or LDIL instructions. */
3036 case R_PARISC_DLTREL21L:
3037 case R_PARISC_DLTIND21L:
3038 case R_PARISC_LTOFF_FPTR21L:
3039 case R_PARISC_PCREL21L:
3040 case R_PARISC_LTOFF_TP21L:
3041 case R_PARISC_DPREL21L:
3042 case R_PARISC_PLTOFF21L:
3043 case R_PARISC_DIR21L:
3044 return (insn & ~0x1fffff) | re_assemble_21 (sym_value);
3046 /* LDO and integer loads/stores with 14 bit displacements. */
3047 case R_PARISC_DLTREL14R:
3048 case R_PARISC_DLTREL14F:
3049 case R_PARISC_DLTIND14R:
3050 case R_PARISC_DLTIND14F:
3051 case R_PARISC_LTOFF_FPTR14R:
3052 case R_PARISC_PCREL14R:
3053 case R_PARISC_PCREL14F:
3054 case R_PARISC_LTOFF_TP14R:
3055 case R_PARISC_LTOFF_TP14F:
3056 case R_PARISC_DPREL14R:
3057 case R_PARISC_DPREL14F:
3058 case R_PARISC_PLTOFF14R:
3059 case R_PARISC_PLTOFF14F:
3060 case R_PARISC_DIR14R:
3061 case R_PARISC_DIR14F:
3062 return (insn & ~0x3fff) | low_sign_unext (sym_value, 14);
3064 /* PA2.0W LDO and integer loads/stores with 16 bit displacements. */
3065 case R_PARISC_LTOFF_FPTR16F:
3066 case R_PARISC_PCREL16F:
3067 case R_PARISC_LTOFF_TP16F:
3068 case R_PARISC_GPREL16F:
3069 case R_PARISC_PLTOFF16F:
3070 case R_PARISC_DIR16F:
3071 case R_PARISC_LTOFF16F:
3072 return (insn & ~0xffff) | re_assemble_16 (sym_value);
3074 /* Doubleword loads and stores with a 14 bit displacement. */
3075 case R_PARISC_DLTREL14DR:
3076 case R_PARISC_DLTIND14DR:
3077 case R_PARISC_LTOFF_FPTR14DR:
3078 case R_PARISC_LTOFF_FPTR16DF:
3079 case R_PARISC_PCREL14DR:
3080 case R_PARISC_PCREL16DF:
3081 case R_PARISC_LTOFF_TP14DR:
3082 case R_PARISC_LTOFF_TP16DF:
3083 case R_PARISC_DPREL14DR:
3084 case R_PARISC_GPREL16DF:
3085 case R_PARISC_PLTOFF14DR:
3086 case R_PARISC_PLTOFF16DF:
3087 case R_PARISC_DIR14DR:
3088 case R_PARISC_DIR16DF:
3089 case R_PARISC_LTOFF16DF:
3090 return (insn & ~0x3ff1) | (((sym_value & 0x2000) >> 13)
3091 | ((sym_value & 0x1ff8) << 1));
3093 /* Floating point single word load/store instructions. */
3094 case R_PARISC_DLTREL14WR:
3095 case R_PARISC_DLTIND14WR:
3096 case R_PARISC_LTOFF_FPTR14WR:
3097 case R_PARISC_LTOFF_FPTR16WF:
3098 case R_PARISC_PCREL14WR:
3099 case R_PARISC_PCREL16WF:
3100 case R_PARISC_LTOFF_TP14WR:
3101 case R_PARISC_LTOFF_TP16WF:
3102 case R_PARISC_DPREL14WR:
3103 case R_PARISC_GPREL16WF:
3104 case R_PARISC_PLTOFF14WR:
3105 case R_PARISC_PLTOFF16WF:
3106 case R_PARISC_DIR16WF:
3107 case R_PARISC_DIR14WR:
3108 case R_PARISC_LTOFF16WF:
3109 return (insn & ~0x3ff9) | (((sym_value & 0x2000) >> 13)
3110 | ((sym_value & 0x1ffc) << 1));
3112 default:
3113 return insn;
3117 /* Compute the value for a relocation (REL) during a final link stage,
3118 then insert the value into the proper location in CONTENTS.
3120 VALUE is a tentative value for the relocation and may be overridden
3121 and modified here based on the specific relocation to be performed.
3123 For example we do conversions for PC-relative branches in this routine
3124 or redirection of calls to external routines to stubs.
3126 The work of actually applying the relocation is left to a helper
3127 routine in an attempt to reduce the complexity and size of this
3128 function. */
3130 static bfd_reloc_status_type
3131 elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
3132 bfd *input_bfd,
3133 bfd *output_bfd,
3134 asection *input_section,
3135 bfd_byte *contents,
3136 bfd_vma value,
3137 struct bfd_link_info *info,
3138 asection *sym_sec,
3139 struct elf_link_hash_entry *eh)
3141 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
3142 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
3143 bfd_vma *local_offsets;
3144 Elf_Internal_Shdr *symtab_hdr;
3145 int insn;
3146 bfd_vma max_branch_offset = 0;
3147 bfd_vma offset = rel->r_offset;
3148 bfd_signed_vma addend = rel->r_addend;
3149 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3150 unsigned int r_symndx = ELF_R_SYM (rel->r_info);
3151 unsigned int r_type = howto->type;
3152 bfd_byte *hit_data = contents + offset;
3154 if (hppa_info == NULL)
3155 return bfd_reloc_notsupported;
3157 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3158 local_offsets = elf_local_got_offsets (input_bfd);
3159 insn = bfd_get_32 (input_bfd, hit_data);
3161 switch (r_type)
3163 case R_PARISC_NONE:
3164 break;
3166 /* Basic function call support.
3168 Note for a call to a function defined in another dynamic library
3169 we want to redirect the call to a stub. */
3171 /* PC relative relocs without an implicit offset. */
3172 case R_PARISC_PCREL21L:
3173 case R_PARISC_PCREL14R:
3174 case R_PARISC_PCREL14F:
3175 case R_PARISC_PCREL14WR:
3176 case R_PARISC_PCREL14DR:
3177 case R_PARISC_PCREL16F:
3178 case R_PARISC_PCREL16WF:
3179 case R_PARISC_PCREL16DF:
3181 /* If this is a call to a function defined in another dynamic
3182 library, then redirect the call to the local stub for this
3183 function. */
3184 if (sym_sec == NULL || sym_sec->output_section == NULL)
3185 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3186 + hppa_info->stub_sec->output_section->vma);
3188 /* Turn VALUE into a proper PC relative address. */
3189 value -= (offset + input_section->output_offset
3190 + input_section->output_section->vma);
3192 /* Adjust for any field selectors. */
3193 if (r_type == R_PARISC_PCREL21L)
3194 value = hppa_field_adjust (value, -8 + addend, e_lsel);
3195 else if (r_type == R_PARISC_PCREL14F
3196 || r_type == R_PARISC_PCREL16F
3197 || r_type == R_PARISC_PCREL16WF
3198 || r_type == R_PARISC_PCREL16DF)
3199 value = hppa_field_adjust (value, -8 + addend, e_fsel);
3200 else
3201 value = hppa_field_adjust (value, -8 + addend, e_rsel);
3203 /* Apply the relocation to the given instruction. */
3204 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3205 break;
3208 case R_PARISC_PCREL12F:
3209 case R_PARISC_PCREL22F:
3210 case R_PARISC_PCREL17F:
3211 case R_PARISC_PCREL22C:
3212 case R_PARISC_PCREL17C:
3213 case R_PARISC_PCREL17R:
3215 /* If this is a call to a function defined in another dynamic
3216 library, then redirect the call to the local stub for this
3217 function. */
3218 if (sym_sec == NULL || sym_sec->output_section == NULL)
3219 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3220 + hppa_info->stub_sec->output_section->vma);
3222 /* Turn VALUE into a proper PC relative address. */
3223 value -= (offset + input_section->output_offset
3224 + input_section->output_section->vma);
3225 addend -= 8;
3227 if (r_type == (unsigned int) R_PARISC_PCREL22F)
3228 max_branch_offset = (1 << (22-1)) << 2;
3229 else if (r_type == (unsigned int) R_PARISC_PCREL17F)
3230 max_branch_offset = (1 << (17-1)) << 2;
3231 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3232 max_branch_offset = (1 << (12-1)) << 2;
3234 /* Make sure we can reach the branch target. */
3235 if (max_branch_offset != 0
3236 && value + addend + max_branch_offset >= 2*max_branch_offset)
3238 _bfd_error_handler
3239 /* xgettext:c-format */
3240 (_("%pB(%pA+%#" PRIx64 "): cannot reach %s"),
3241 input_bfd,
3242 input_section,
3243 (uint64_t) offset,
3244 eh ? eh->root.root.string : "unknown");
3245 bfd_set_error (bfd_error_bad_value);
3246 return bfd_reloc_overflow;
3249 /* Adjust for any field selectors. */
3250 if (r_type == R_PARISC_PCREL17R)
3251 value = hppa_field_adjust (value, addend, e_rsel);
3252 else
3253 value = hppa_field_adjust (value, addend, e_fsel);
3255 /* All branches are implicitly shifted by 2 places. */
3256 value >>= 2;
3258 /* Apply the relocation to the given instruction. */
3259 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3260 break;
3263 /* Indirect references to data through the DLT. */
3264 case R_PARISC_DLTIND14R:
3265 case R_PARISC_DLTIND14F:
3266 case R_PARISC_DLTIND14DR:
3267 case R_PARISC_DLTIND14WR:
3268 case R_PARISC_DLTIND21L:
3269 case R_PARISC_LTOFF_FPTR14R:
3270 case R_PARISC_LTOFF_FPTR14DR:
3271 case R_PARISC_LTOFF_FPTR14WR:
3272 case R_PARISC_LTOFF_FPTR21L:
3273 case R_PARISC_LTOFF_FPTR16F:
3274 case R_PARISC_LTOFF_FPTR16WF:
3275 case R_PARISC_LTOFF_FPTR16DF:
3276 case R_PARISC_LTOFF_TP21L:
3277 case R_PARISC_LTOFF_TP14R:
3278 case R_PARISC_LTOFF_TP14F:
3279 case R_PARISC_LTOFF_TP14WR:
3280 case R_PARISC_LTOFF_TP14DR:
3281 case R_PARISC_LTOFF_TP16F:
3282 case R_PARISC_LTOFF_TP16WF:
3283 case R_PARISC_LTOFF_TP16DF:
3284 case R_PARISC_LTOFF16F:
3285 case R_PARISC_LTOFF16WF:
3286 case R_PARISC_LTOFF16DF:
3288 bfd_vma off;
3290 /* If this relocation was against a local symbol, then we still
3291 have not set up the DLT entry (it's not convenient to do so
3292 in the "finalize_dlt" routine because it is difficult to get
3293 to the local symbol's value).
3295 So, if this is a local symbol (h == NULL), then we need to
3296 fill in its DLT entry.
3298 Similarly we may still need to set up an entry in .opd for
3299 a local function which had its address taken. */
3300 if (hh == NULL)
3302 bfd_vma *local_opd_offsets, *local_dlt_offsets;
3304 if (local_offsets == NULL)
3305 abort ();
3307 /* Now do .opd creation if needed. */
3308 if (r_type == R_PARISC_LTOFF_FPTR14R
3309 || r_type == R_PARISC_LTOFF_FPTR14DR
3310 || r_type == R_PARISC_LTOFF_FPTR14WR
3311 || r_type == R_PARISC_LTOFF_FPTR21L
3312 || r_type == R_PARISC_LTOFF_FPTR16F
3313 || r_type == R_PARISC_LTOFF_FPTR16WF
3314 || r_type == R_PARISC_LTOFF_FPTR16DF)
3316 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3317 off = local_opd_offsets[r_symndx];
3319 /* The last bit records whether we've already initialised
3320 this local .opd entry. */
3321 if ((off & 1) != 0)
3323 BFD_ASSERT (off != (bfd_vma) -1);
3324 off &= ~1;
3326 else
3328 local_opd_offsets[r_symndx] |= 1;
3330 /* The first two words of an .opd entry are zero. */
3331 memset (hppa_info->opd_sec->contents + off, 0, 16);
3333 /* The next word is the address of the function. */
3334 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3335 (hppa_info->opd_sec->contents + off + 16));
3337 /* The last word is our local __gp value. */
3338 value = _bfd_get_gp_value (info->output_bfd);
3339 bfd_put_64 (hppa_info->opd_sec->owner, value,
3340 (hppa_info->opd_sec->contents + off + 24));
3343 /* The DLT value is the address of the .opd entry. */
3344 value = (off
3345 + hppa_info->opd_sec->output_offset
3346 + hppa_info->opd_sec->output_section->vma);
3347 addend = 0;
3350 local_dlt_offsets = local_offsets;
3351 off = local_dlt_offsets[r_symndx];
3353 if ((off & 1) != 0)
3355 BFD_ASSERT (off != (bfd_vma) -1);
3356 off &= ~1;
3358 else
3360 local_dlt_offsets[r_symndx] |= 1;
3361 bfd_put_64 (hppa_info->dlt_sec->owner,
3362 value + addend,
3363 hppa_info->dlt_sec->contents + off);
3366 else
3367 off = hh->dlt_offset;
3369 /* We want the value of the DLT offset for this symbol, not
3370 the symbol's actual address. Note that __gp may not point
3371 to the start of the DLT, so we have to compute the absolute
3372 address, then subtract out the value of __gp. */
3373 value = (off
3374 + hppa_info->dlt_sec->output_offset
3375 + hppa_info->dlt_sec->output_section->vma);
3376 value -= _bfd_get_gp_value (output_bfd);
3378 /* All DLTIND relocations are basically the same at this point,
3379 except that we need different field selectors for the 21bit
3380 version vs the 14bit versions. */
3381 if (r_type == R_PARISC_DLTIND21L
3382 || r_type == R_PARISC_LTOFF_FPTR21L
3383 || r_type == R_PARISC_LTOFF_TP21L)
3384 value = hppa_field_adjust (value, 0, e_lsel);
3385 else if (r_type == R_PARISC_DLTIND14F
3386 || r_type == R_PARISC_LTOFF_FPTR16F
3387 || r_type == R_PARISC_LTOFF_FPTR16WF
3388 || r_type == R_PARISC_LTOFF_FPTR16DF
3389 || r_type == R_PARISC_LTOFF16F
3390 || r_type == R_PARISC_LTOFF16DF
3391 || r_type == R_PARISC_LTOFF16WF
3392 || r_type == R_PARISC_LTOFF_TP16F
3393 || r_type == R_PARISC_LTOFF_TP16WF
3394 || r_type == R_PARISC_LTOFF_TP16DF)
3395 value = hppa_field_adjust (value, 0, e_fsel);
3396 else
3397 value = hppa_field_adjust (value, 0, e_rsel);
3399 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3400 break;
3403 case R_PARISC_DLTREL14R:
3404 case R_PARISC_DLTREL14F:
3405 case R_PARISC_DLTREL14DR:
3406 case R_PARISC_DLTREL14WR:
3407 case R_PARISC_DLTREL21L:
3408 case R_PARISC_DPREL21L:
3409 case R_PARISC_DPREL14WR:
3410 case R_PARISC_DPREL14DR:
3411 case R_PARISC_DPREL14R:
3412 case R_PARISC_DPREL14F:
3413 case R_PARISC_GPREL16F:
3414 case R_PARISC_GPREL16WF:
3415 case R_PARISC_GPREL16DF:
3417 /* Subtract out the global pointer value to make value a DLT
3418 relative address. */
3419 value -= _bfd_get_gp_value (output_bfd);
3421 /* All DLTREL relocations are basically the same at this point,
3422 except that we need different field selectors for the 21bit
3423 version vs the 14bit versions. */
3424 if (r_type == R_PARISC_DLTREL21L
3425 || r_type == R_PARISC_DPREL21L)
3426 value = hppa_field_adjust (value, addend, e_lrsel);
3427 else if (r_type == R_PARISC_DLTREL14F
3428 || r_type == R_PARISC_DPREL14F
3429 || r_type == R_PARISC_GPREL16F
3430 || r_type == R_PARISC_GPREL16WF
3431 || r_type == R_PARISC_GPREL16DF)
3432 value = hppa_field_adjust (value, addend, e_fsel);
3433 else
3434 value = hppa_field_adjust (value, addend, e_rrsel);
3436 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3437 break;
3440 case R_PARISC_DIR21L:
3441 case R_PARISC_DIR17R:
3442 case R_PARISC_DIR17F:
3443 case R_PARISC_DIR14R:
3444 case R_PARISC_DIR14F:
3445 case R_PARISC_DIR14WR:
3446 case R_PARISC_DIR14DR:
3447 case R_PARISC_DIR16F:
3448 case R_PARISC_DIR16WF:
3449 case R_PARISC_DIR16DF:
3451 /* All DIR relocations are basically the same at this point,
3452 except that branch offsets need to be divided by four, and
3453 we need different field selectors. Note that we don't
3454 redirect absolute calls to local stubs. */
3456 if (r_type == R_PARISC_DIR21L)
3457 value = hppa_field_adjust (value, addend, e_lrsel);
3458 else if (r_type == R_PARISC_DIR17F
3459 || r_type == R_PARISC_DIR16F
3460 || r_type == R_PARISC_DIR16WF
3461 || r_type == R_PARISC_DIR16DF
3462 || r_type == R_PARISC_DIR14F)
3463 value = hppa_field_adjust (value, addend, e_fsel);
3464 else
3465 value = hppa_field_adjust (value, addend, e_rrsel);
3467 if (r_type == R_PARISC_DIR17R || r_type == R_PARISC_DIR17F)
3468 /* All branches are implicitly shifted by 2 places. */
3469 value >>= 2;
3471 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3472 break;
3475 case R_PARISC_PLTOFF21L:
3476 case R_PARISC_PLTOFF14R:
3477 case R_PARISC_PLTOFF14F:
3478 case R_PARISC_PLTOFF14WR:
3479 case R_PARISC_PLTOFF14DR:
3480 case R_PARISC_PLTOFF16F:
3481 case R_PARISC_PLTOFF16WF:
3482 case R_PARISC_PLTOFF16DF:
3484 /* We want the value of the PLT offset for this symbol, not
3485 the symbol's actual address. Note that __gp may not point
3486 to the start of the DLT, so we have to compute the absolute
3487 address, then subtract out the value of __gp. */
3488 value = (hh->plt_offset
3489 + hppa_info->root.splt->output_offset
3490 + hppa_info->root.splt->output_section->vma);
3491 value -= _bfd_get_gp_value (output_bfd);
3493 /* All PLTOFF relocations are basically the same at this point,
3494 except that we need different field selectors for the 21bit
3495 version vs the 14bit versions. */
3496 if (r_type == R_PARISC_PLTOFF21L)
3497 value = hppa_field_adjust (value, addend, e_lrsel);
3498 else if (r_type == R_PARISC_PLTOFF14F
3499 || r_type == R_PARISC_PLTOFF16F
3500 || r_type == R_PARISC_PLTOFF16WF
3501 || r_type == R_PARISC_PLTOFF16DF)
3502 value = hppa_field_adjust (value, addend, e_fsel);
3503 else
3504 value = hppa_field_adjust (value, addend, e_rrsel);
3506 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3507 break;
3510 case R_PARISC_LTOFF_FPTR32:
3512 /* FIXME: There used to be code here to create the FPTR itself if
3513 the relocation was against a local symbol. But the code could
3514 never have worked. If the assert below is ever triggered then
3515 the code will need to be reinstated and fixed so that it does
3516 what is needed. */
3517 BFD_ASSERT (hh != NULL);
3519 /* We want the value of the DLT offset for this symbol, not
3520 the symbol's actual address. Note that __gp may not point
3521 to the start of the DLT, so we have to compute the absolute
3522 address, then subtract out the value of __gp. */
3523 value = (hh->dlt_offset
3524 + hppa_info->dlt_sec->output_offset
3525 + hppa_info->dlt_sec->output_section->vma);
3526 value -= _bfd_get_gp_value (output_bfd);
3527 bfd_put_32 (input_bfd, value, hit_data);
3528 return bfd_reloc_ok;
3531 case R_PARISC_LTOFF_FPTR64:
3532 case R_PARISC_LTOFF_TP64:
3534 /* We may still need to create the FPTR itself if it was for
3535 a local symbol. */
3536 if (eh == NULL && r_type == R_PARISC_LTOFF_FPTR64)
3538 /* The first two words of an .opd entry are zero. */
3539 memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
3541 /* The next word is the address of the function. */
3542 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3543 (hppa_info->opd_sec->contents
3544 + hh->opd_offset + 16));
3546 /* The last word is our local __gp value. */
3547 value = _bfd_get_gp_value (info->output_bfd);
3548 bfd_put_64 (hppa_info->opd_sec->owner, value,
3549 hppa_info->opd_sec->contents + hh->opd_offset + 24);
3551 /* The DLT value is the address of the .opd entry. */
3552 value = (hh->opd_offset
3553 + hppa_info->opd_sec->output_offset
3554 + hppa_info->opd_sec->output_section->vma);
3556 bfd_put_64 (hppa_info->dlt_sec->owner,
3557 value,
3558 hppa_info->dlt_sec->contents + hh->dlt_offset);
3561 /* We want the value of the DLT offset for this symbol, not
3562 the symbol's actual address. Note that __gp may not point
3563 to the start of the DLT, so we have to compute the absolute
3564 address, then subtract out the value of __gp. */
3565 value = (hh->dlt_offset
3566 + hppa_info->dlt_sec->output_offset
3567 + hppa_info->dlt_sec->output_section->vma);
3568 value -= _bfd_get_gp_value (output_bfd);
3569 bfd_put_64 (input_bfd, value, hit_data);
3570 return bfd_reloc_ok;
3573 case R_PARISC_DIR32:
3574 bfd_put_32 (input_bfd, value + addend, hit_data);
3575 return bfd_reloc_ok;
3577 case R_PARISC_DIR64:
3578 bfd_put_64 (input_bfd, value + addend, hit_data);
3579 return bfd_reloc_ok;
3581 case R_PARISC_GPREL64:
3582 /* Subtract out the global pointer value to make value a DLT
3583 relative address. */
3584 value -= _bfd_get_gp_value (output_bfd);
3586 bfd_put_64 (input_bfd, value + addend, hit_data);
3587 return bfd_reloc_ok;
3589 case R_PARISC_LTOFF64:
3590 /* We want the value of the DLT offset for this symbol, not
3591 the symbol's actual address. Note that __gp may not point
3592 to the start of the DLT, so we have to compute the absolute
3593 address, then subtract out the value of __gp. */
3594 value = (hh->dlt_offset
3595 + hppa_info->dlt_sec->output_offset
3596 + hppa_info->dlt_sec->output_section->vma);
3597 value -= _bfd_get_gp_value (output_bfd);
3599 bfd_put_64 (input_bfd, value + addend, hit_data);
3600 return bfd_reloc_ok;
3602 case R_PARISC_PCREL32:
3604 /* If this is a call to a function defined in another dynamic
3605 library, then redirect the call to the local stub for this
3606 function. */
3607 if (sym_sec == NULL || sym_sec->output_section == NULL)
3608 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3609 + hppa_info->stub_sec->output_section->vma);
3611 /* Turn VALUE into a proper PC relative address. */
3612 value -= (offset + input_section->output_offset
3613 + input_section->output_section->vma);
3615 value += addend;
3616 value -= 8;
3617 bfd_put_32 (input_bfd, value, hit_data);
3618 return bfd_reloc_ok;
3621 case R_PARISC_PCREL64:
3623 /* If this is a call to a function defined in another dynamic
3624 library, then redirect the call to the local stub for this
3625 function. */
3626 if (sym_sec == NULL || sym_sec->output_section == NULL)
3627 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3628 + hppa_info->stub_sec->output_section->vma);
3630 /* Turn VALUE into a proper PC relative address. */
3631 value -= (offset + input_section->output_offset
3632 + input_section->output_section->vma);
3634 value += addend;
3635 value -= 8;
3636 bfd_put_64 (input_bfd, value, hit_data);
3637 return bfd_reloc_ok;
3640 case R_PARISC_FPTR64:
3642 bfd_vma off;
3644 /* We may still need to create the FPTR itself if it was for
3645 a local symbol. */
3646 if (hh == NULL)
3648 bfd_vma *local_opd_offsets;
3650 if (local_offsets == NULL)
3651 abort ();
3653 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3654 off = local_opd_offsets[r_symndx];
3656 /* The last bit records whether we've already initialised
3657 this local .opd entry. */
3658 if ((off & 1) != 0)
3660 BFD_ASSERT (off != (bfd_vma) -1);
3661 off &= ~1;
3663 else
3665 /* The first two words of an .opd entry are zero. */
3666 memset (hppa_info->opd_sec->contents + off, 0, 16);
3668 /* The next word is the address of the function. */
3669 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3670 (hppa_info->opd_sec->contents + off + 16));
3672 /* The last word is our local __gp value. */
3673 value = _bfd_get_gp_value (info->output_bfd);
3674 bfd_put_64 (hppa_info->opd_sec->owner, value,
3675 hppa_info->opd_sec->contents + off + 24);
3678 else
3679 off = hh->opd_offset;
3681 if (hh == NULL || hh->want_opd)
3682 /* We want the value of the OPD offset for this symbol. */
3683 value = (off
3684 + hppa_info->opd_sec->output_offset
3685 + hppa_info->opd_sec->output_section->vma);
3686 else
3687 /* We want the address of the symbol. */
3688 value += addend;
3690 bfd_put_64 (input_bfd, value, hit_data);
3691 return bfd_reloc_ok;
3694 case R_PARISC_SECREL32:
3695 if (sym_sec && sym_sec->output_section)
3696 value -= sym_sec->output_section->vma;
3697 bfd_put_32 (input_bfd, value + addend, hit_data);
3698 return bfd_reloc_ok;
3700 case R_PARISC_SEGREL32:
3701 case R_PARISC_SEGREL64:
3703 /* If this is the first SEGREL relocation, then initialize
3704 the segment base values. */
3705 if (hppa_info->text_segment_base == (bfd_vma) -1)
3706 bfd_map_over_sections (output_bfd, elf_hppa_record_segment_addrs,
3707 hppa_info);
3709 /* VALUE holds the absolute address. We want to include the
3710 addend, then turn it into a segment relative address.
3712 The segment is derived from SYM_SEC. We assume that there are
3713 only two segments of note in the resulting executable/shlib.
3714 A readonly segment (.text) and a readwrite segment (.data). */
3715 value += addend;
3717 if (sym_sec->flags & SEC_CODE)
3718 value -= hppa_info->text_segment_base;
3719 else
3720 value -= hppa_info->data_segment_base;
3722 if (r_type == R_PARISC_SEGREL32)
3723 bfd_put_32 (input_bfd, value, hit_data);
3724 else
3725 bfd_put_64 (input_bfd, value, hit_data);
3726 return bfd_reloc_ok;
3729 /* Something we don't know how to handle. */
3730 default:
3731 return bfd_reloc_notsupported;
3734 /* Update the instruction word. */
3735 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3736 return bfd_reloc_ok;
3739 /* Relocate an HPPA ELF section. */
3741 static int
3742 elf64_hppa_relocate_section (bfd *output_bfd,
3743 struct bfd_link_info *info,
3744 bfd *input_bfd,
3745 asection *input_section,
3746 bfd_byte *contents,
3747 Elf_Internal_Rela *relocs,
3748 Elf_Internal_Sym *local_syms,
3749 asection **local_sections)
3751 Elf_Internal_Shdr *symtab_hdr;
3752 Elf_Internal_Rela *rel;
3753 Elf_Internal_Rela *relend;
3754 struct elf64_hppa_link_hash_table *hppa_info;
3756 hppa_info = hppa_link_hash_table (info);
3757 if (hppa_info == NULL)
3758 return false;
3760 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3762 rel = relocs;
3763 relend = relocs + input_section->reloc_count;
3764 for (; rel < relend; rel++)
3766 int r_type;
3767 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3768 unsigned long r_symndx;
3769 struct elf_link_hash_entry *eh;
3770 Elf_Internal_Sym *sym;
3771 asection *sym_sec;
3772 bfd_vma relocation;
3773 bfd_reloc_status_type r;
3775 r_type = ELF_R_TYPE (rel->r_info);
3776 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
3778 bfd_set_error (bfd_error_bad_value);
3779 return false;
3781 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3782 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3783 continue;
3785 /* This is a final link. */
3786 r_symndx = ELF_R_SYM (rel->r_info);
3787 eh = NULL;
3788 sym = NULL;
3789 sym_sec = NULL;
3790 if (r_symndx < symtab_hdr->sh_info)
3792 /* This is a local symbol, hh defaults to NULL. */
3793 sym = local_syms + r_symndx;
3794 sym_sec = local_sections[r_symndx];
3795 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3797 else
3799 /* This is not a local symbol. */
3800 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3802 /* It seems this can happen with erroneous or unsupported
3803 input (mixing a.out and elf in an archive, for example.) */
3804 if (sym_hashes == NULL)
3805 return false;
3807 eh = sym_hashes[r_symndx - symtab_hdr->sh_info];
3809 if (info->wrap_hash != NULL
3810 && (input_section->flags & SEC_DEBUGGING) != 0)
3811 eh = ((struct elf_link_hash_entry *)
3812 unwrap_hash_lookup (info, input_bfd, &eh->root));
3814 while (eh->root.type == bfd_link_hash_indirect
3815 || eh->root.type == bfd_link_hash_warning)
3816 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3818 relocation = 0;
3819 if (eh->root.type == bfd_link_hash_defined
3820 || eh->root.type == bfd_link_hash_defweak)
3822 sym_sec = eh->root.u.def.section;
3823 if (sym_sec != NULL
3824 && sym_sec->output_section != NULL)
3825 relocation = (eh->root.u.def.value
3826 + sym_sec->output_section->vma
3827 + sym_sec->output_offset);
3829 else if (eh->root.type == bfd_link_hash_undefweak)
3831 else if (info->unresolved_syms_in_objects == RM_IGNORE
3832 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
3834 else if (!bfd_link_relocatable (info)
3835 && elf_hppa_is_dynamic_loader_symbol (eh->root.root.string))
3836 continue;
3837 else if (!bfd_link_relocatable (info))
3839 bool err;
3841 err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
3842 && !info->warn_unresolved_syms)
3843 || ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT;
3845 info->callbacks->undefined_symbol
3846 (info, eh->root.root.string, input_bfd,
3847 input_section, rel->r_offset, err);
3850 if (!bfd_link_relocatable (info)
3851 && relocation == 0
3852 && eh->root.type != bfd_link_hash_defined
3853 && eh->root.type != bfd_link_hash_defweak
3854 && eh->root.type != bfd_link_hash_undefweak)
3856 if (info->unresolved_syms_in_objects == RM_IGNORE
3857 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3858 && eh->type == STT_PARISC_MILLI)
3859 info->callbacks->undefined_symbol
3860 (info, eh_name (eh), input_bfd,
3861 input_section, rel->r_offset, false);
3865 if (sym_sec != NULL && discarded_section (sym_sec))
3866 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3867 rel, 1, relend, howto, 0, contents);
3869 if (bfd_link_relocatable (info))
3870 continue;
3872 r = elf_hppa_final_link_relocate (rel, input_bfd, output_bfd,
3873 input_section, contents,
3874 relocation, info, sym_sec,
3875 eh);
3877 if (r != bfd_reloc_ok)
3879 switch (r)
3881 default:
3882 abort ();
3883 case bfd_reloc_overflow:
3885 const char *sym_name;
3887 if (eh != NULL)
3888 sym_name = NULL;
3889 else
3891 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3892 symtab_hdr->sh_link,
3893 sym->st_name);
3894 if (sym_name == NULL)
3895 return false;
3896 if (*sym_name == '\0')
3897 sym_name = bfd_section_name (sym_sec);
3900 (*info->callbacks->reloc_overflow)
3901 (info, (eh ? &eh->root : NULL), sym_name, howto->name,
3902 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3904 break;
3908 return true;
3911 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
3913 { STRING_COMMA_LEN (".tbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
3914 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3915 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3916 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3917 { STRING_COMMA_LEN (".dlt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3918 { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3919 { STRING_COMMA_LEN (".sbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3920 { NULL, 0, 0, 0, 0 }
3923 /* The hash bucket size is the standard one, namely 4. */
3925 const struct elf_size_info hppa64_elf_size_info =
3927 sizeof (Elf64_External_Ehdr),
3928 sizeof (Elf64_External_Phdr),
3929 sizeof (Elf64_External_Shdr),
3930 sizeof (Elf64_External_Rel),
3931 sizeof (Elf64_External_Rela),
3932 sizeof (Elf64_External_Sym),
3933 sizeof (Elf64_External_Dyn),
3934 sizeof (Elf_External_Note),
3937 64, 3,
3938 ELFCLASS64, EV_CURRENT,
3939 bfd_elf64_write_out_phdrs,
3940 bfd_elf64_write_shdrs_and_ehdr,
3941 bfd_elf64_checksum_contents,
3942 bfd_elf64_write_relocs,
3943 bfd_elf64_swap_symbol_in,
3944 bfd_elf64_swap_symbol_out,
3945 bfd_elf64_slurp_reloc_table,
3946 bfd_elf64_slurp_symbol_table,
3947 bfd_elf64_swap_dyn_in,
3948 bfd_elf64_swap_dyn_out,
3949 bfd_elf64_swap_reloc_in,
3950 bfd_elf64_swap_reloc_out,
3951 bfd_elf64_swap_reloca_in,
3952 bfd_elf64_swap_reloca_out
3955 #define TARGET_BIG_SYM hppa_elf64_vec
3956 #define TARGET_BIG_NAME "elf64-hppa"
3957 #define ELF_ARCH bfd_arch_hppa
3958 #define ELF_TARGET_ID HPPA64_ELF_DATA
3959 #define ELF_MACHINE_CODE EM_PARISC
3960 /* This is not strictly correct. The maximum page size for PA2.0 is
3961 64M. But everything still uses 4k. */
3962 #define ELF_MAXPAGESIZE 0x1000
3963 #define ELF_OSABI ELFOSABI_HPUX
3965 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
3966 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
3967 #define bfd_elf64_bfd_is_local_label_name elf_hppa_is_local_label_name
3968 #define elf_info_to_howto elf_hppa_info_to_howto
3969 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
3971 #define elf_backend_section_from_shdr elf64_hppa_section_from_shdr
3972 #define elf_backend_object_p elf64_hppa_object_p
3973 #define elf_backend_final_write_processing \
3974 elf_hppa_final_write_processing
3975 #define elf_backend_fake_sections elf_hppa_fake_sections
3976 #define elf_backend_add_symbol_hook elf_hppa_add_symbol_hook
3978 #define elf_backend_relocate_section elf_hppa_relocate_section
3980 #define bfd_elf64_bfd_final_link elf_hppa_final_link
3982 #define elf_backend_create_dynamic_sections \
3983 elf64_hppa_create_dynamic_sections
3984 #define elf_backend_init_file_header elf64_hppa_init_file_header
3986 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
3988 #define elf_backend_adjust_dynamic_symbol \
3989 elf64_hppa_adjust_dynamic_symbol
3991 #define elf_backend_late_size_sections elf64_hppa_late_size_sections
3993 #define elf_backend_finish_dynamic_symbol \
3994 elf64_hppa_finish_dynamic_symbol
3995 #define elf_backend_finish_dynamic_sections \
3996 elf64_hppa_finish_dynamic_sections
3997 #define elf_backend_grok_prstatus elf64_hppa_grok_prstatus
3998 #define elf_backend_grok_psinfo elf64_hppa_grok_psinfo
4000 /* Stuff for the BFD linker: */
4001 #define bfd_elf64_bfd_link_hash_table_create \
4002 elf64_hppa_hash_table_create
4004 #define elf_backend_check_relocs \
4005 elf64_hppa_check_relocs
4007 #define elf_backend_size_info \
4008 hppa64_elf_size_info
4010 #define elf_backend_additional_program_headers \
4011 elf64_hppa_additional_program_headers
4013 #define elf_backend_modify_segment_map \
4014 elf64_hppa_modify_segment_map
4016 #define elf_backend_allow_non_load_phdr \
4017 elf64_hppa_allow_non_load_phdr
4019 #define elf_backend_link_output_symbol_hook \
4020 elf64_hppa_link_output_symbol_hook
4022 #define elf_backend_want_got_plt 0
4023 #define elf_backend_plt_readonly 0
4024 #define elf_backend_want_plt_sym 0
4025 #define elf_backend_got_header_size 0
4026 #define elf_backend_type_change_ok true
4027 #define elf_backend_get_symbol_type elf64_hppa_elf_get_symbol_type
4028 #define elf_backend_reloc_type_class elf64_hppa_reloc_type_class
4029 #define elf_backend_rela_normal 1
4030 #define elf_backend_special_sections elf64_hppa_special_sections
4031 #define elf_backend_action_discarded elf_hppa_action_discarded
4032 #define elf_backend_section_from_phdr elf64_hppa_section_from_phdr
4034 #define elf64_bed elf64_hppa_hpux_bed
4036 #include "elf64-target.h"
4038 #undef TARGET_BIG_SYM
4039 #define TARGET_BIG_SYM hppa_elf64_linux_vec
4040 #undef TARGET_BIG_NAME
4041 #define TARGET_BIG_NAME "elf64-hppa-linux"
4042 #undef ELF_OSABI
4043 #define ELF_OSABI ELFOSABI_GNU
4044 #undef elf64_bed
4045 #define elf64_bed elf64_hppa_linux_bed
4046 #undef elf_backend_special_sections
4047 #define elf_backend_special_sections (elf64_hppa_special_sections + 1)
4049 #include "elf64-target.h"