[gdb/testsuite] Fix end_sequence addresses
[binutils-gdb.git] / bfd / elf64-hppa.c
blob041ab29c3b4ecf111522399f35330fcb46fac244
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 #define ARCH_SIZE 64
32 #define PLT_ENTRY_SIZE 0x10
33 #define DLT_ENTRY_SIZE 0x8
34 #define OPD_ENTRY_SIZE 0x20
36 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
38 /* The stub is supposed to load the target address and target's DP
39 value out of the PLT, then do an external branch to the target
40 address.
42 LDD PLTOFF(%r27),%r1
43 BVE (%r1)
44 LDD PLTOFF+8(%r27),%r27
46 Note that we must use the LDD with a 14 bit displacement, not the one
47 with a 5 bit displacement. */
48 static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
49 0x53, 0x7b, 0x00, 0x00 };
51 struct elf64_hppa_link_hash_entry
53 struct elf_link_hash_entry eh;
55 /* Offsets for this symbol in various linker sections. */
56 bfd_vma dlt_offset;
57 bfd_vma plt_offset;
58 bfd_vma opd_offset;
59 bfd_vma stub_offset;
61 /* The index of the (possibly local) symbol in the input bfd and its
62 associated BFD. Needed so that we can have relocs against local
63 symbols in shared libraries. */
64 long sym_indx;
65 bfd *owner;
67 /* Dynamic symbols may need to have two different values. One for
68 the dynamic symbol table, one for the normal symbol table.
70 In such cases we store the symbol's real value and section
71 index here so we can restore the real value before we write
72 the normal symbol table. */
73 bfd_vma st_value;
74 int st_shndx;
76 /* Used to count non-got, non-plt relocations for delayed sizing
77 of relocation sections. */
78 struct elf64_hppa_dyn_reloc_entry
80 /* Next relocation in the chain. */
81 struct elf64_hppa_dyn_reloc_entry *next;
83 /* The type of the relocation. */
84 int type;
86 /* The input section of the relocation. */
87 asection *sec;
89 /* Number of relocs copied in this section. */
90 bfd_size_type count;
92 /* The index of the section symbol for the input section of
93 the relocation. Only needed when building shared libraries. */
94 int sec_symndx;
96 /* The offset within the input section of the relocation. */
97 bfd_vma offset;
99 /* The addend for the relocation. */
100 bfd_vma addend;
102 } *reloc_entries;
104 /* Nonzero if this symbol needs an entry in one of the linker
105 sections. */
106 unsigned want_dlt;
107 unsigned want_plt;
108 unsigned want_opd;
109 unsigned want_stub;
112 struct elf64_hppa_link_hash_table
114 struct elf_link_hash_table root;
116 /* Shortcuts to get to the various linker defined sections. */
117 asection *dlt_sec;
118 asection *dlt_rel_sec;
119 asection *opd_sec;
120 asection *opd_rel_sec;
121 asection *other_rel_sec;
123 /* Offset of __gp within .plt section. When the PLT gets large we want
124 to slide __gp into the PLT section so that we can continue to use
125 single DP relative instructions to load values out of the PLT. */
126 bfd_vma gp_offset;
128 /* Note this is not strictly correct. We should create a stub section for
129 each input section with calls. The stub section should be placed before
130 the section with the call. */
131 asection *stub_sec;
133 bfd_vma text_segment_base;
134 bfd_vma data_segment_base;
136 /* We build tables to map from an input section back to its
137 symbol index. This is the BFD for which we currently have
138 a map. */
139 bfd *section_syms_bfd;
141 /* Array of symbol numbers for each input section attached to the
142 current BFD. */
143 int *section_syms;
146 #define hppa_link_hash_table(p) \
147 ((is_elf_hash_table ((p)->hash) \
148 && elf_hash_table_id (elf_hash_table (p)) == HPPA64_ELF_DATA) \
149 ? (struct elf64_hppa_link_hash_table *) (p)->hash : NULL)
151 #define hppa_elf_hash_entry(ent) \
152 ((struct elf64_hppa_link_hash_entry *)(ent))
154 #define eh_name(eh) \
155 (eh ? eh->root.root.string : "<undef>")
157 typedef struct bfd_hash_entry *(*new_hash_entry_func)
158 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
160 static struct bfd_link_hash_table *elf64_hppa_hash_table_create
161 (bfd *abfd);
163 /* This must follow the definitions of the various derived linker
164 hash tables and shared functions. */
165 #include "elf-hppa.h"
167 static bool elf64_hppa_object_p
168 (bfd *);
170 static bool elf64_hppa_create_dynamic_sections
171 (bfd *, struct bfd_link_info *);
173 static bool elf64_hppa_adjust_dynamic_symbol
174 (struct bfd_link_info *, struct elf_link_hash_entry *);
176 static bool elf64_hppa_mark_milli_and_exported_functions
177 (struct elf_link_hash_entry *, void *);
179 static int elf64_hppa_link_output_symbol_hook
180 (struct bfd_link_info *, const char *, Elf_Internal_Sym *,
181 asection *, struct elf_link_hash_entry *);
183 static bool elf64_hppa_finish_dynamic_symbol
184 (bfd *, struct bfd_link_info *,
185 struct elf_link_hash_entry *, Elf_Internal_Sym *);
187 static bool elf64_hppa_finish_dynamic_sections
188 (bfd *, struct bfd_link_info *);
190 static bool elf64_hppa_check_relocs
191 (bfd *, struct bfd_link_info *,
192 asection *, const Elf_Internal_Rela *);
194 static bool elf64_hppa_dynamic_symbol_p
195 (struct elf_link_hash_entry *, struct bfd_link_info *);
197 static bool elf64_hppa_mark_exported_functions
198 (struct elf_link_hash_entry *, void *);
200 static bool elf64_hppa_finalize_opd
201 (struct elf_link_hash_entry *, void *);
203 static bool elf64_hppa_finalize_dlt
204 (struct elf_link_hash_entry *, void *);
206 static bool allocate_global_data_dlt
207 (struct elf_link_hash_entry *, void *);
209 static bool allocate_global_data_plt
210 (struct elf_link_hash_entry *, void *);
212 static bool allocate_global_data_stub
213 (struct elf_link_hash_entry *, void *);
215 static bool allocate_global_data_opd
216 (struct elf_link_hash_entry *, void *);
218 static bool get_reloc_section
219 (bfd *, struct elf64_hppa_link_hash_table *, asection *);
221 static bool count_dyn_reloc
222 (bfd *, struct elf64_hppa_link_hash_entry *,
223 int, asection *, int, bfd_vma, bfd_vma);
225 static bool allocate_dynrel_entries
226 (struct elf_link_hash_entry *, void *);
228 static bool elf64_hppa_finalize_dynreloc
229 (struct elf_link_hash_entry *, void *);
231 static bool get_opd
232 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
234 static bool get_plt
235 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
237 static bool get_dlt
238 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
240 static bool get_stub
241 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
243 static int elf64_hppa_elf_get_symbol_type
244 (Elf_Internal_Sym *, int);
246 /* Initialize an entry in the link hash table. */
248 static struct bfd_hash_entry *
249 hppa64_link_hash_newfunc (struct bfd_hash_entry *entry,
250 struct bfd_hash_table *table,
251 const char *string)
253 /* Allocate the structure if it has not already been allocated by a
254 subclass. */
255 if (entry == NULL)
257 entry = bfd_hash_allocate (table,
258 sizeof (struct elf64_hppa_link_hash_entry));
259 if (entry == NULL)
260 return entry;
263 /* Call the allocation method of the superclass. */
264 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
265 if (entry != NULL)
267 struct elf64_hppa_link_hash_entry *hh;
269 /* Initialize our local data. All zeros. */
270 hh = hppa_elf_hash_entry (entry);
271 memset (&hh->dlt_offset, 0,
272 (sizeof (struct elf64_hppa_link_hash_entry)
273 - offsetof (struct elf64_hppa_link_hash_entry, dlt_offset)));
276 return entry;
279 /* Create the derived linker hash table. The PA64 ELF port uses this
280 derived hash table to keep information specific to the PA ElF
281 linker (without using static variables). */
283 static struct bfd_link_hash_table*
284 elf64_hppa_hash_table_create (bfd *abfd)
286 struct elf64_hppa_link_hash_table *htab;
287 size_t amt = sizeof (*htab);
289 htab = bfd_zmalloc (amt);
290 if (htab == NULL)
291 return NULL;
293 if (!_bfd_elf_link_hash_table_init (&htab->root, abfd,
294 hppa64_link_hash_newfunc,
295 sizeof (struct elf64_hppa_link_hash_entry),
296 HPPA64_ELF_DATA))
298 free (htab);
299 return NULL;
302 htab->root.dt_pltgot_required = true;
303 htab->text_segment_base = (bfd_vma) -1;
304 htab->data_segment_base = (bfd_vma) -1;
306 return &htab->root.root;
309 /* Return nonzero if ABFD represents a PA2.0 ELF64 file.
311 Additionally we set the default architecture and machine. */
312 static bool
313 elf64_hppa_object_p (bfd *abfd)
315 Elf_Internal_Ehdr * i_ehdrp;
316 unsigned int flags;
318 i_ehdrp = elf_elfheader (abfd);
319 if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
321 /* GCC on hppa-linux produces binaries with OSABI=GNU,
322 but the kernel produces corefiles with OSABI=SysV. */
323 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
324 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
325 return false;
327 else
329 /* HPUX produces binaries with OSABI=HPUX,
330 but the kernel produces corefiles with OSABI=SysV. */
331 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
332 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
333 return false;
336 flags = i_ehdrp->e_flags;
337 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
339 case EFA_PARISC_1_0:
340 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
341 case EFA_PARISC_1_1:
342 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
343 case EFA_PARISC_2_0:
344 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
345 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
346 else
347 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
348 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
349 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
351 /* Don't be fussy. */
352 return true;
355 /* Given section type (hdr->sh_type), return a boolean indicating
356 whether or not the section is an elf64-hppa specific section. */
357 static bool
358 elf64_hppa_section_from_shdr (bfd *abfd,
359 Elf_Internal_Shdr *hdr,
360 const char *name,
361 int shindex)
363 switch (hdr->sh_type)
365 case SHT_PARISC_EXT:
366 if (strcmp (name, ".PARISC.archext") != 0)
367 return false;
368 break;
369 case SHT_PARISC_UNWIND:
370 if (strcmp (name, ".PARISC.unwind") != 0)
371 return false;
372 break;
373 case SHT_PARISC_DOC:
374 case SHT_PARISC_ANNOT:
375 default:
376 return false;
379 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
380 return false;
382 return ((hdr->sh_flags & SHF_PARISC_SHORT) == 0
383 || bfd_set_section_flags (hdr->bfd_section,
384 hdr->bfd_section->flags | SEC_SMALL_DATA));
387 /* SEC is a section containing relocs for an input BFD when linking; return
388 a suitable section for holding relocs in the output BFD for a link. */
390 static bool
391 get_reloc_section (bfd *abfd,
392 struct elf64_hppa_link_hash_table *hppa_info,
393 asection *sec)
395 const char *srel_name;
396 asection *srel;
397 bfd *dynobj;
399 srel_name = (bfd_elf_string_from_elf_section
400 (abfd, elf_elfheader(abfd)->e_shstrndx,
401 _bfd_elf_single_rel_hdr(sec)->sh_name));
402 if (srel_name == NULL)
403 return false;
405 dynobj = hppa_info->root.dynobj;
406 if (!dynobj)
407 hppa_info->root.dynobj = dynobj = abfd;
409 srel = bfd_get_linker_section (dynobj, srel_name);
410 if (srel == NULL)
412 srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
413 (SEC_ALLOC
414 | SEC_LOAD
415 | SEC_HAS_CONTENTS
416 | SEC_IN_MEMORY
417 | SEC_LINKER_CREATED
418 | SEC_READONLY));
419 if (srel == NULL
420 || !bfd_set_section_alignment (srel, 3))
421 return false;
424 hppa_info->other_rel_sec = srel;
425 return true;
428 /* Add a new entry to the list of dynamic relocations against DYN_H.
430 We use this to keep a record of all the FPTR relocations against a
431 particular symbol so that we can create FPTR relocations in the
432 output file. */
434 static bool
435 count_dyn_reloc (bfd *abfd,
436 struct elf64_hppa_link_hash_entry *hh,
437 int type,
438 asection *sec,
439 int sec_symndx,
440 bfd_vma offset,
441 bfd_vma addend)
443 struct elf64_hppa_dyn_reloc_entry *rent;
445 rent = (struct elf64_hppa_dyn_reloc_entry *)
446 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
447 if (!rent)
448 return false;
450 rent->next = hh->reloc_entries;
451 rent->type = type;
452 rent->sec = sec;
453 rent->sec_symndx = sec_symndx;
454 rent->offset = offset;
455 rent->addend = addend;
456 hh->reloc_entries = rent;
458 return true;
461 /* Return a pointer to the local DLT, PLT and OPD reference counts
462 for ABFD. Returns NULL if the storage allocation fails. */
464 static bfd_signed_vma *
465 hppa64_elf_local_refcounts (bfd *abfd)
467 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
468 bfd_signed_vma *local_refcounts;
470 local_refcounts = elf_local_got_refcounts (abfd);
471 if (local_refcounts == NULL)
473 bfd_size_type size;
475 /* Allocate space for local DLT, PLT and OPD reference
476 counts. Done this way to save polluting elf_obj_tdata
477 with another target specific pointer. */
478 size = symtab_hdr->sh_info;
479 size *= 3 * sizeof (bfd_signed_vma);
480 local_refcounts = bfd_zalloc (abfd, size);
481 elf_local_got_refcounts (abfd) = local_refcounts;
483 return local_refcounts;
486 /* Scan the RELOCS and record the type of dynamic entries that each
487 referenced symbol needs. */
489 static bool
490 elf64_hppa_check_relocs (bfd *abfd,
491 struct bfd_link_info *info,
492 asection *sec,
493 const Elf_Internal_Rela *relocs)
495 struct elf64_hppa_link_hash_table *hppa_info;
496 const Elf_Internal_Rela *relend;
497 Elf_Internal_Shdr *symtab_hdr;
498 const Elf_Internal_Rela *rel;
499 unsigned int sec_symndx;
501 if (bfd_link_relocatable (info))
502 return true;
504 /* If this is the first dynamic object found in the link, create
505 the special sections required for dynamic linking. */
506 if (! elf_hash_table (info)->dynamic_sections_created)
508 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
509 return false;
512 hppa_info = hppa_link_hash_table (info);
513 if (hppa_info == NULL)
514 return false;
515 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
517 /* If necessary, build a new table holding section symbols indices
518 for this BFD. */
520 if (bfd_link_pic (info) && hppa_info->section_syms_bfd != abfd)
522 unsigned long i;
523 unsigned int highest_shndx;
524 Elf_Internal_Sym *local_syms = NULL;
525 Elf_Internal_Sym *isym, *isymend;
526 bfd_size_type amt;
528 /* We're done with the old cache of section index to section symbol
529 index information. Free it.
531 ?!? Note we leak the last section_syms array. Presumably we
532 could free it in one of the later routines in this file. */
533 free (hppa_info->section_syms);
535 /* Read this BFD's local symbols. */
536 if (symtab_hdr->sh_info != 0)
538 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
539 if (local_syms == NULL)
540 local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
541 symtab_hdr->sh_info, 0,
542 NULL, NULL, NULL);
543 if (local_syms == NULL)
544 return false;
547 /* Record the highest section index referenced by the local symbols. */
548 highest_shndx = 0;
549 isymend = local_syms + symtab_hdr->sh_info;
550 for (isym = local_syms; isym < isymend; isym++)
552 if (isym->st_shndx > highest_shndx
553 && isym->st_shndx < SHN_LORESERVE)
554 highest_shndx = isym->st_shndx;
557 /* Allocate an array to hold the section index to section symbol index
558 mapping. Bump by one since we start counting at zero. */
559 highest_shndx++;
560 amt = highest_shndx;
561 amt *= sizeof (int);
562 hppa_info->section_syms = (int *) bfd_malloc (amt);
564 /* Now walk the local symbols again. If we find a section symbol,
565 record the index of the symbol into the section_syms array. */
566 for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
568 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
569 hppa_info->section_syms[isym->st_shndx] = i;
572 /* We are finished with the local symbols. */
573 if (local_syms != NULL
574 && symtab_hdr->contents != (unsigned char *) local_syms)
576 if (! info->keep_memory)
577 free (local_syms);
578 else
580 /* Cache the symbols for elf_link_input_bfd. */
581 symtab_hdr->contents = (unsigned char *) local_syms;
585 /* Record which BFD we built the section_syms mapping for. */
586 hppa_info->section_syms_bfd = abfd;
589 /* Record the symbol index for this input section. We may need it for
590 relocations when building shared libraries. When not building shared
591 libraries this value is never really used, but assign it to zero to
592 prevent out of bounds memory accesses in other routines. */
593 if (bfd_link_pic (info))
595 sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
597 /* If we did not find a section symbol for this section, then
598 something went terribly wrong above. */
599 if (sec_symndx == SHN_BAD)
600 return false;
602 if (sec_symndx < SHN_LORESERVE)
603 sec_symndx = hppa_info->section_syms[sec_symndx];
604 else
605 sec_symndx = 0;
607 else
608 sec_symndx = 0;
610 relend = relocs + sec->reloc_count;
611 for (rel = relocs; rel < relend; ++rel)
613 enum
615 NEED_DLT = 1,
616 NEED_PLT = 2,
617 NEED_STUB = 4,
618 NEED_OPD = 8,
619 NEED_DYNREL = 16,
622 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
623 struct elf64_hppa_link_hash_entry *hh;
624 int need_entry;
625 bool maybe_dynamic;
626 int dynrel_type = R_PARISC_NONE;
627 static reloc_howto_type *howto;
629 if (r_symndx >= symtab_hdr->sh_info)
631 /* We're dealing with a global symbol -- find its hash entry
632 and mark it as being referenced. */
633 long indx = r_symndx - symtab_hdr->sh_info;
634 hh = hppa_elf_hash_entry (elf_sym_hashes (abfd)[indx]);
635 while (hh->eh.root.type == bfd_link_hash_indirect
636 || hh->eh.root.type == bfd_link_hash_warning)
637 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
639 /* PR15323, ref flags aren't set for references in the same
640 object. */
641 hh->eh.ref_regular = 1;
643 else
644 hh = NULL;
646 /* We can only get preliminary data on whether a symbol is
647 locally or externally defined, as not all of the input files
648 have yet been processed. Do something with what we know, as
649 this may help reduce memory usage and processing time later. */
650 maybe_dynamic = false;
651 if (hh && ((bfd_link_pic (info)
652 && (!info->symbolic
653 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
654 || !hh->eh.def_regular
655 || hh->eh.root.type == bfd_link_hash_defweak))
656 maybe_dynamic = true;
658 howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
659 need_entry = 0;
660 switch (howto->type)
662 /* These are simple indirect references to symbols through the
663 DLT. We need to create a DLT entry for any symbols which
664 appears in a DLTIND relocation. */
665 case R_PARISC_DLTIND21L:
666 case R_PARISC_DLTIND14R:
667 case R_PARISC_DLTIND14F:
668 case R_PARISC_DLTIND14WR:
669 case R_PARISC_DLTIND14DR:
670 need_entry = NEED_DLT;
671 break;
673 /* ?!? These need a DLT entry. But I have no idea what to do with
674 the "link time TP value. */
675 case R_PARISC_LTOFF_TP21L:
676 case R_PARISC_LTOFF_TP14R:
677 case R_PARISC_LTOFF_TP14F:
678 case R_PARISC_LTOFF_TP64:
679 case R_PARISC_LTOFF_TP14WR:
680 case R_PARISC_LTOFF_TP14DR:
681 case R_PARISC_LTOFF_TP16F:
682 case R_PARISC_LTOFF_TP16WF:
683 case R_PARISC_LTOFF_TP16DF:
684 need_entry = NEED_DLT;
685 break;
687 /* These are function calls. Depending on their precise target we
688 may need to make a stub for them. The stub uses the PLT, so we
689 need to create PLT entries for these symbols too. */
690 case R_PARISC_PCREL12F:
691 case R_PARISC_PCREL17F:
692 case R_PARISC_PCREL22F:
693 case R_PARISC_PCREL32:
694 case R_PARISC_PCREL64:
695 case R_PARISC_PCREL21L:
696 case R_PARISC_PCREL17R:
697 case R_PARISC_PCREL17C:
698 case R_PARISC_PCREL14R:
699 case R_PARISC_PCREL14F:
700 case R_PARISC_PCREL22C:
701 case R_PARISC_PCREL14WR:
702 case R_PARISC_PCREL14DR:
703 case R_PARISC_PCREL16F:
704 case R_PARISC_PCREL16WF:
705 case R_PARISC_PCREL16DF:
706 /* Function calls might need to go through the .plt, and
707 might need a long branch stub. */
708 if (hh != NULL && hh->eh.type != STT_PARISC_MILLI)
709 need_entry = (NEED_PLT | NEED_STUB);
710 else
711 need_entry = 0;
712 break;
714 case R_PARISC_PLTOFF21L:
715 case R_PARISC_PLTOFF14R:
716 case R_PARISC_PLTOFF14F:
717 case R_PARISC_PLTOFF14WR:
718 case R_PARISC_PLTOFF14DR:
719 case R_PARISC_PLTOFF16F:
720 case R_PARISC_PLTOFF16WF:
721 case R_PARISC_PLTOFF16DF:
722 need_entry = (NEED_PLT);
723 break;
725 case R_PARISC_DIR64:
726 if (bfd_link_pic (info) || maybe_dynamic)
727 need_entry = (NEED_DYNREL);
728 dynrel_type = R_PARISC_DIR64;
729 break;
731 /* This is an indirect reference through the DLT to get the address
732 of a OPD descriptor. Thus we need to make a DLT entry that points
733 to an OPD entry. */
734 case R_PARISC_LTOFF_FPTR21L:
735 case R_PARISC_LTOFF_FPTR14R:
736 case R_PARISC_LTOFF_FPTR14WR:
737 case R_PARISC_LTOFF_FPTR14DR:
738 case R_PARISC_LTOFF_FPTR32:
739 case R_PARISC_LTOFF_FPTR64:
740 case R_PARISC_LTOFF_FPTR16F:
741 case R_PARISC_LTOFF_FPTR16WF:
742 case R_PARISC_LTOFF_FPTR16DF:
743 if (bfd_link_pic (info) || maybe_dynamic)
744 need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
745 else
746 need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
747 dynrel_type = R_PARISC_FPTR64;
748 break;
750 /* This is a simple OPD entry. */
751 case R_PARISC_FPTR64:
752 if (bfd_link_pic (info) || maybe_dynamic)
753 need_entry = (NEED_OPD | NEED_PLT | NEED_DYNREL);
754 else
755 need_entry = (NEED_OPD | NEED_PLT);
756 dynrel_type = R_PARISC_FPTR64;
757 break;
759 /* Add more cases as needed. */
762 if (!need_entry)
763 continue;
765 if (hh)
767 /* Stash away enough information to be able to find this symbol
768 regardless of whether or not it is local or global. */
769 hh->owner = abfd;
770 hh->sym_indx = r_symndx;
773 /* Create what's needed. */
774 if (need_entry & NEED_DLT)
776 /* Allocate space for a DLT entry, as well as a dynamic
777 relocation for this entry. */
778 if (! hppa_info->dlt_sec
779 && ! get_dlt (abfd, info, hppa_info))
780 goto err_out;
782 if (hh != NULL)
784 hh->want_dlt = 1;
785 hh->eh.got.refcount += 1;
787 else
789 bfd_signed_vma *local_dlt_refcounts;
791 /* This is a DLT entry for a local symbol. */
792 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
793 if (local_dlt_refcounts == NULL)
794 return false;
795 local_dlt_refcounts[r_symndx] += 1;
799 if (need_entry & NEED_PLT)
801 if (! hppa_info->root.splt
802 && ! get_plt (abfd, info, hppa_info))
803 goto err_out;
805 if (hh != NULL)
807 hh->want_plt = 1;
808 hh->eh.needs_plt = 1;
809 hh->eh.plt.refcount += 1;
811 else
813 bfd_signed_vma *local_dlt_refcounts;
814 bfd_signed_vma *local_plt_refcounts;
816 /* This is a PLT entry for a local symbol. */
817 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
818 if (local_dlt_refcounts == NULL)
819 return false;
820 local_plt_refcounts = local_dlt_refcounts + symtab_hdr->sh_info;
821 local_plt_refcounts[r_symndx] += 1;
825 if (need_entry & NEED_STUB)
827 if (! hppa_info->stub_sec
828 && ! get_stub (abfd, info, hppa_info))
829 goto err_out;
830 if (hh)
831 hh->want_stub = 1;
834 if (need_entry & NEED_OPD)
836 if (! hppa_info->opd_sec
837 && ! get_opd (abfd, info, hppa_info))
838 goto err_out;
840 /* FPTRs are not allocated by the dynamic linker for PA64,
841 though it is possible that will change in the future. */
843 if (hh != NULL)
844 hh->want_opd = 1;
845 else
847 bfd_signed_vma *local_dlt_refcounts;
848 bfd_signed_vma *local_opd_refcounts;
850 /* This is a OPD for a local symbol. */
851 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
852 if (local_dlt_refcounts == NULL)
853 return false;
854 local_opd_refcounts = (local_dlt_refcounts
855 + 2 * symtab_hdr->sh_info);
856 local_opd_refcounts[r_symndx] += 1;
860 /* Add a new dynamic relocation to the chain of dynamic
861 relocations for this symbol. */
862 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
864 if (! hppa_info->other_rel_sec
865 && ! get_reloc_section (abfd, hppa_info, sec))
866 goto err_out;
868 /* Count dynamic relocations against global symbols. */
869 if (hh != NULL
870 && !count_dyn_reloc (abfd, hh, dynrel_type, sec,
871 sec_symndx, rel->r_offset, rel->r_addend))
872 goto err_out;
874 /* If we are building a shared library and we just recorded
875 a dynamic R_PARISC_FPTR64 relocation, then make sure the
876 section symbol for this section ends up in the dynamic
877 symbol table. */
878 if (bfd_link_pic (info) && dynrel_type == R_PARISC_FPTR64
879 && ! (bfd_elf_link_record_local_dynamic_symbol
880 (info, abfd, sec_symndx)))
881 return false;
885 return true;
887 err_out:
888 return false;
891 struct elf64_hppa_allocate_data
893 struct bfd_link_info *info;
894 bfd_size_type ofs;
897 /* Should we do dynamic things to this symbol? */
899 static bool
900 elf64_hppa_dynamic_symbol_p (struct elf_link_hash_entry *eh,
901 struct bfd_link_info *info)
903 /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
904 and relocations that retrieve a function descriptor? Assume the
905 worst for now. */
906 if (_bfd_elf_dynamic_symbol_p (eh, info, 1))
908 /* ??? Why is this here and not elsewhere is_local_label_name. */
909 if (eh->root.root.string[0] == '$' && eh->root.root.string[1] == '$')
910 return false;
912 return true;
914 else
915 return false;
918 /* Mark all functions exported by this file so that we can later allocate
919 entries in .opd for them. */
921 static bool
922 elf64_hppa_mark_exported_functions (struct elf_link_hash_entry *eh, void *data)
924 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
925 struct bfd_link_info *info = (struct bfd_link_info *)data;
926 struct elf64_hppa_link_hash_table *hppa_info;
928 hppa_info = hppa_link_hash_table (info);
929 if (hppa_info == NULL)
930 return false;
932 if (eh
933 && (eh->root.type == bfd_link_hash_defined
934 || eh->root.type == bfd_link_hash_defweak)
935 && eh->root.u.def.section->output_section != NULL
936 && eh->type == STT_FUNC)
938 if (! hppa_info->opd_sec
939 && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
940 return false;
942 hh->want_opd = 1;
944 /* Put a flag here for output_symbol_hook. */
945 hh->st_shndx = -1;
946 eh->needs_plt = 1;
949 return true;
952 /* Allocate space for a DLT entry. */
954 static bool
955 allocate_global_data_dlt (struct elf_link_hash_entry *eh, void *data)
957 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
958 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
960 if (hh->want_dlt)
962 if (bfd_link_pic (x->info))
964 /* Possibly add the symbol to the local dynamic symbol
965 table since we might need to create a dynamic relocation
966 against it. */
967 if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
969 bfd *owner = eh->root.u.def.section->owner;
971 if (! (bfd_elf_link_record_local_dynamic_symbol
972 (x->info, owner, hh->sym_indx)))
973 return false;
977 hh->dlt_offset = x->ofs;
978 x->ofs += DLT_ENTRY_SIZE;
980 return true;
983 /* Allocate space for a DLT.PLT entry. */
985 static bool
986 allocate_global_data_plt (struct elf_link_hash_entry *eh, void *data)
988 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
989 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *) data;
991 if (hh->want_plt
992 && elf64_hppa_dynamic_symbol_p (eh, x->info)
993 && !((eh->root.type == bfd_link_hash_defined
994 || eh->root.type == bfd_link_hash_defweak)
995 && eh->root.u.def.section->output_section != NULL))
997 hh->plt_offset = x->ofs;
998 x->ofs += PLT_ENTRY_SIZE;
999 if (hh->plt_offset < 0x2000)
1001 struct elf64_hppa_link_hash_table *hppa_info;
1003 hppa_info = hppa_link_hash_table (x->info);
1004 if (hppa_info == NULL)
1005 return false;
1007 hppa_info->gp_offset = hh->plt_offset;
1010 else
1011 hh->want_plt = 0;
1013 return true;
1016 /* Allocate space for a STUB entry. */
1018 static bool
1019 allocate_global_data_stub (struct elf_link_hash_entry *eh, void *data)
1021 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1022 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1024 if (hh->want_stub
1025 && elf64_hppa_dynamic_symbol_p (eh, x->info)
1026 && !((eh->root.type == bfd_link_hash_defined
1027 || eh->root.type == bfd_link_hash_defweak)
1028 && eh->root.u.def.section->output_section != NULL))
1030 hh->stub_offset = x->ofs;
1031 x->ofs += sizeof (plt_stub);
1033 else
1034 hh->want_stub = 0;
1035 return true;
1038 /* Allocate space for a FPTR entry. */
1040 static bool
1041 allocate_global_data_opd (struct elf_link_hash_entry *eh, void *data)
1043 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1044 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1046 if (hh && hh->want_opd)
1048 /* We never need an opd entry for a symbol which is not
1049 defined by this output file. */
1050 if (hh && (hh->eh.root.type == bfd_link_hash_undefined
1051 || hh->eh.root.type == bfd_link_hash_undefweak
1052 || hh->eh.root.u.def.section->output_section == NULL))
1053 hh->want_opd = 0;
1055 /* If we are creating a shared library, took the address of a local
1056 function or might export this function from this object file, then
1057 we have to create an opd descriptor. */
1058 else if (bfd_link_pic (x->info)
1059 || hh == NULL
1060 || (hh->eh.dynindx == -1 && hh->eh.type != STT_PARISC_MILLI)
1061 || (hh->eh.root.type == bfd_link_hash_defined
1062 || hh->eh.root.type == bfd_link_hash_defweak))
1064 /* If we are creating a shared library, then we will have to
1065 create a runtime relocation for the symbol to properly
1066 initialize the .opd entry. Make sure the symbol gets
1067 added to the dynamic symbol table. */
1068 if (bfd_link_pic (x->info)
1069 && (hh == NULL || (hh->eh.dynindx == -1)))
1071 bfd *owner;
1072 /* PR 6511: Default to using the dynamic symbol table. */
1073 owner = (hh->owner ? hh->owner: eh->root.u.def.section->owner);
1075 if (!bfd_elf_link_record_local_dynamic_symbol
1076 (x->info, owner, hh->sym_indx))
1077 return false;
1080 /* This may not be necessary or desirable anymore now that
1081 we have some support for dealing with section symbols
1082 in dynamic relocs. But name munging does make the result
1083 much easier to debug. ie, the EPLT reloc will reference
1084 a symbol like .foobar, instead of .text + offset. */
1085 if (bfd_link_pic (x->info) && eh)
1087 char *new_name;
1088 struct elf_link_hash_entry *nh;
1090 new_name = concat (".", eh->root.root.string, NULL);
1092 nh = elf_link_hash_lookup (elf_hash_table (x->info),
1093 new_name, true, true, true);
1095 free (new_name);
1096 nh->root.type = eh->root.type;
1097 nh->root.u.def.value = eh->root.u.def.value;
1098 nh->root.u.def.section = eh->root.u.def.section;
1100 if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
1101 return false;
1103 hh->opd_offset = x->ofs;
1104 x->ofs += OPD_ENTRY_SIZE;
1107 /* Otherwise we do not need an opd entry. */
1108 else
1109 hh->want_opd = 0;
1111 return true;
1114 /* HP requires the EI_OSABI field to be filled in. The assignment to
1115 EI_ABIVERSION may not be strictly necessary. */
1117 static bool
1118 elf64_hppa_init_file_header (bfd *abfd, struct bfd_link_info *info)
1120 Elf_Internal_Ehdr *i_ehdrp;
1122 if (!_bfd_elf_init_file_header (abfd, info))
1123 return false;
1125 i_ehdrp = elf_elfheader (abfd);
1126 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
1127 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
1128 return true;
1131 /* Create function descriptor section (.opd). This section is called .opd
1132 because it contains "official procedure descriptors". The "official"
1133 refers to the fact that these descriptors are used when taking the address
1134 of a procedure, thus ensuring a unique address for each procedure. */
1136 static bool
1137 get_opd (bfd *abfd,
1138 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1139 struct elf64_hppa_link_hash_table *hppa_info)
1141 asection *opd;
1142 bfd *dynobj;
1144 opd = hppa_info->opd_sec;
1145 if (!opd)
1147 dynobj = hppa_info->root.dynobj;
1148 if (!dynobj)
1149 hppa_info->root.dynobj = dynobj = abfd;
1151 opd = bfd_make_section_anyway_with_flags (dynobj, ".opd",
1152 (SEC_ALLOC
1153 | SEC_LOAD
1154 | SEC_HAS_CONTENTS
1155 | SEC_IN_MEMORY
1156 | SEC_LINKER_CREATED));
1157 if (!opd
1158 || !bfd_set_section_alignment (opd, 3))
1160 BFD_ASSERT (0);
1161 return false;
1164 hppa_info->opd_sec = opd;
1167 return true;
1170 /* Create the PLT section. */
1172 static bool
1173 get_plt (bfd *abfd,
1174 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1175 struct elf64_hppa_link_hash_table *hppa_info)
1177 asection *plt;
1178 bfd *dynobj;
1180 plt = hppa_info->root.splt;
1181 if (!plt)
1183 dynobj = hppa_info->root.dynobj;
1184 if (!dynobj)
1185 hppa_info->root.dynobj = dynobj = abfd;
1187 plt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
1188 (SEC_ALLOC
1189 | SEC_LOAD
1190 | SEC_HAS_CONTENTS
1191 | SEC_IN_MEMORY
1192 | SEC_LINKER_CREATED));
1193 if (!plt
1194 || !bfd_set_section_alignment (plt, 3))
1196 BFD_ASSERT (0);
1197 return false;
1200 hppa_info->root.splt = plt;
1203 return true;
1206 /* Create the DLT section. */
1208 static bool
1209 get_dlt (bfd *abfd,
1210 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1211 struct elf64_hppa_link_hash_table *hppa_info)
1213 asection *dlt;
1214 bfd *dynobj;
1216 dlt = hppa_info->dlt_sec;
1217 if (!dlt)
1219 dynobj = hppa_info->root.dynobj;
1220 if (!dynobj)
1221 hppa_info->root.dynobj = dynobj = abfd;
1223 dlt = bfd_make_section_anyway_with_flags (dynobj, ".dlt",
1224 (SEC_ALLOC
1225 | SEC_LOAD
1226 | SEC_HAS_CONTENTS
1227 | SEC_IN_MEMORY
1228 | SEC_LINKER_CREATED));
1229 if (!dlt
1230 || !bfd_set_section_alignment (dlt, 3))
1232 BFD_ASSERT (0);
1233 return false;
1236 hppa_info->dlt_sec = dlt;
1239 return true;
1242 /* Create the stubs section. */
1244 static bool
1245 get_stub (bfd *abfd,
1246 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1247 struct elf64_hppa_link_hash_table *hppa_info)
1249 asection *stub;
1250 bfd *dynobj;
1252 stub = hppa_info->stub_sec;
1253 if (!stub)
1255 dynobj = hppa_info->root.dynobj;
1256 if (!dynobj)
1257 hppa_info->root.dynobj = dynobj = abfd;
1259 stub = bfd_make_section_anyway_with_flags (dynobj, ".stub",
1260 (SEC_ALLOC | SEC_LOAD
1261 | SEC_HAS_CONTENTS
1262 | SEC_IN_MEMORY
1263 | SEC_READONLY
1264 | SEC_LINKER_CREATED));
1265 if (!stub
1266 || !bfd_set_section_alignment (stub, 3))
1268 BFD_ASSERT (0);
1269 return false;
1272 hppa_info->stub_sec = stub;
1275 return true;
1278 /* Create sections necessary for dynamic linking. This is only a rough
1279 cut and will likely change as we learn more about the somewhat
1280 unusual dynamic linking scheme HP uses.
1282 .stub:
1283 Contains code to implement cross-space calls. The first time one
1284 of the stubs is used it will call into the dynamic linker, later
1285 calls will go straight to the target.
1287 The only stub we support right now looks like
1289 ldd OFFSET(%dp),%r1
1290 bve %r0(%r1)
1291 ldd OFFSET+8(%dp),%dp
1293 Other stubs may be needed in the future. We may want the remove
1294 the break/nop instruction. It is only used right now to keep the
1295 offset of a .plt entry and a .stub entry in sync.
1297 .dlt:
1298 This is what most people call the .got. HP used a different name.
1299 Losers.
1301 .rela.dlt:
1302 Relocations for the DLT.
1304 .plt:
1305 Function pointers as address,gp pairs.
1307 .rela.plt:
1308 Should contain dynamic IPLT (and EPLT?) relocations.
1310 .opd:
1311 FPTRS
1313 .rela.opd:
1314 EPLT relocations for symbols exported from shared libraries. */
1316 static bool
1317 elf64_hppa_create_dynamic_sections (bfd *abfd,
1318 struct bfd_link_info *info)
1320 asection *s;
1321 struct elf64_hppa_link_hash_table *hppa_info;
1323 hppa_info = hppa_link_hash_table (info);
1324 if (hppa_info == NULL)
1325 return false;
1327 if (! get_stub (abfd, info, hppa_info))
1328 return false;
1330 if (! get_dlt (abfd, info, hppa_info))
1331 return false;
1333 if (! get_plt (abfd, info, hppa_info))
1334 return false;
1336 if (! get_opd (abfd, info, hppa_info))
1337 return false;
1339 s = bfd_make_section_anyway_with_flags (abfd, ".rela.dlt",
1340 (SEC_ALLOC | SEC_LOAD
1341 | SEC_HAS_CONTENTS
1342 | SEC_IN_MEMORY
1343 | SEC_READONLY
1344 | SEC_LINKER_CREATED));
1345 if (s == NULL
1346 || !bfd_set_section_alignment (s, 3))
1347 return false;
1348 hppa_info->dlt_rel_sec = s;
1350 s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt",
1351 (SEC_ALLOC | SEC_LOAD
1352 | SEC_HAS_CONTENTS
1353 | SEC_IN_MEMORY
1354 | SEC_READONLY
1355 | SEC_LINKER_CREATED));
1356 if (s == NULL
1357 || !bfd_set_section_alignment (s, 3))
1358 return false;
1359 hppa_info->root.srelplt = s;
1361 s = bfd_make_section_anyway_with_flags (abfd, ".rela.data",
1362 (SEC_ALLOC | SEC_LOAD
1363 | SEC_HAS_CONTENTS
1364 | SEC_IN_MEMORY
1365 | SEC_READONLY
1366 | SEC_LINKER_CREATED));
1367 if (s == NULL
1368 || !bfd_set_section_alignment (s, 3))
1369 return false;
1370 hppa_info->other_rel_sec = s;
1372 s = bfd_make_section_anyway_with_flags (abfd, ".rela.opd",
1373 (SEC_ALLOC | SEC_LOAD
1374 | SEC_HAS_CONTENTS
1375 | SEC_IN_MEMORY
1376 | SEC_READONLY
1377 | SEC_LINKER_CREATED));
1378 if (s == NULL
1379 || !bfd_set_section_alignment (s, 3))
1380 return false;
1381 hppa_info->opd_rel_sec = s;
1383 return true;
1386 /* Allocate dynamic relocations for those symbols that turned out
1387 to be dynamic. */
1389 static bool
1390 allocate_dynrel_entries (struct elf_link_hash_entry *eh, void *data)
1392 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1393 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1394 struct elf64_hppa_link_hash_table *hppa_info;
1395 struct elf64_hppa_dyn_reloc_entry *rent;
1396 bool dynamic_symbol, shared;
1398 hppa_info = hppa_link_hash_table (x->info);
1399 if (hppa_info == NULL)
1400 return false;
1402 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, x->info);
1403 shared = bfd_link_pic (x->info);
1405 /* We may need to allocate relocations for a non-dynamic symbol
1406 when creating a shared library. */
1407 if (!dynamic_symbol && !shared)
1408 return true;
1410 /* Take care of the normal data relocations. */
1412 for (rent = hh->reloc_entries; rent; rent = rent->next)
1414 /* Allocate one iff we are building a shared library, the relocation
1415 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
1416 if (!shared && rent->type == R_PARISC_FPTR64 && hh->want_opd)
1417 continue;
1419 hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
1421 /* Make sure this symbol gets into the dynamic symbol table if it is
1422 not already recorded. ?!? This should not be in the loop since
1423 the symbol need only be added once. */
1424 if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
1425 if (!bfd_elf_link_record_local_dynamic_symbol
1426 (x->info, rent->sec->owner, hh->sym_indx))
1427 return false;
1430 /* Take care of the GOT and PLT relocations. */
1432 if ((dynamic_symbol || shared) && hh->want_dlt)
1433 hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
1435 /* If we are building a shared library, then every symbol that has an
1436 opd entry will need an EPLT relocation to relocate the symbol's address
1437 and __gp value based on the runtime load address. */
1438 if (shared && hh->want_opd)
1439 hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
1441 if (hh->want_plt && dynamic_symbol)
1443 bfd_size_type t = 0;
1445 /* Dynamic symbols get one IPLT relocation. Local symbols in
1446 shared libraries get two REL relocations. Local symbols in
1447 main applications get nothing. */
1448 if (dynamic_symbol)
1449 t = sizeof (Elf64_External_Rela);
1450 else if (shared)
1451 t = 2 * sizeof (Elf64_External_Rela);
1453 hppa_info->root.srelplt->size += t;
1456 return true;
1459 /* Adjust a symbol defined by a dynamic object and referenced by a
1460 regular object. */
1462 static bool
1463 elf64_hppa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1464 struct elf_link_hash_entry *eh)
1466 /* ??? Undefined symbols with PLT entries should be re-defined
1467 to be the PLT entry. */
1469 /* If this is a weak symbol, and there is a real definition, the
1470 processor independent code will have arranged for us to see the
1471 real definition first, and we can just use the same value. */
1472 if (eh->is_weakalias)
1474 struct elf_link_hash_entry *def = weakdef (eh);
1475 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1476 eh->root.u.def.section = def->root.u.def.section;
1477 eh->root.u.def.value = def->root.u.def.value;
1478 return true;
1481 /* If this is a reference to a symbol defined by a dynamic object which
1482 is not a function, we might allocate the symbol in our .dynbss section
1483 and allocate a COPY dynamic relocation.
1485 But PA64 code is canonically PIC, so as a rule we can avoid this sort
1486 of hackery. */
1488 return true;
1491 /* This function is called via elf_link_hash_traverse to mark millicode
1492 symbols with a dynindx of -1 and to remove the string table reference
1493 from the dynamic symbol table. If the symbol is not a millicode symbol,
1494 elf64_hppa_mark_exported_functions is called. */
1496 static bool
1497 elf64_hppa_mark_milli_and_exported_functions (struct elf_link_hash_entry *eh,
1498 void *data)
1500 struct bfd_link_info *info = (struct bfd_link_info *) data;
1502 if (eh->type == STT_PARISC_MILLI)
1504 if (eh->dynindx != -1)
1506 eh->dynindx = -1;
1507 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1508 eh->dynstr_index);
1510 return true;
1513 return elf64_hppa_mark_exported_functions (eh, data);
1516 /* Set the final sizes of the dynamic sections and allocate memory for
1517 the contents of our special sections. */
1519 static bool
1520 elf64_hppa_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1522 struct elf64_hppa_link_hash_table *hppa_info;
1523 struct elf64_hppa_allocate_data data;
1524 bfd *dynobj;
1525 bfd *ibfd;
1526 asection *sec;
1527 bool relocs;
1529 hppa_info = hppa_link_hash_table (info);
1530 if (hppa_info == NULL)
1531 return false;
1533 dynobj = hppa_info->root.dynobj;
1534 if (dynobj == NULL)
1535 return true;
1537 /* Mark each function this program exports so that we will allocate
1538 space in the .opd section for each function's FPTR. If we are
1539 creating dynamic sections, change the dynamic index of millicode
1540 symbols to -1 and remove them from the string table for .dynstr.
1542 We have to traverse the main linker hash table since we have to
1543 find functions which may not have been mentioned in any relocs. */
1544 elf_link_hash_traverse (&hppa_info->root,
1545 (hppa_info->root.dynamic_sections_created
1546 ? elf64_hppa_mark_milli_and_exported_functions
1547 : elf64_hppa_mark_exported_functions),
1548 info);
1550 if (hppa_info->root.dynamic_sections_created)
1552 /* Set the contents of the .interp section to the interpreter. */
1553 if (bfd_link_executable (info) && !info->nointerp)
1555 sec = bfd_get_linker_section (dynobj, ".interp");
1556 BFD_ASSERT (sec != NULL);
1557 sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
1558 sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1561 else
1563 /* We may have created entries in the .rela.got section.
1564 However, if we are not creating the dynamic sections, we will
1565 not actually use these entries. Reset the size of .rela.dlt,
1566 which will cause it to get stripped from the output file
1567 below. */
1568 sec = hppa_info->dlt_rel_sec;
1569 if (sec != NULL)
1570 sec->size = 0;
1573 /* Set up DLT, PLT and OPD offsets for local syms, and space for local
1574 dynamic relocs. */
1575 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
1577 bfd_signed_vma *local_dlt;
1578 bfd_signed_vma *end_local_dlt;
1579 bfd_signed_vma *local_plt;
1580 bfd_signed_vma *end_local_plt;
1581 bfd_signed_vma *local_opd;
1582 bfd_signed_vma *end_local_opd;
1583 bfd_size_type locsymcount;
1584 Elf_Internal_Shdr *symtab_hdr;
1585 asection *srel;
1587 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1588 continue;
1590 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
1592 struct elf64_hppa_dyn_reloc_entry *hdh_p;
1594 for (hdh_p = ((struct elf64_hppa_dyn_reloc_entry *)
1595 elf_section_data (sec)->local_dynrel);
1596 hdh_p != NULL;
1597 hdh_p = hdh_p->next)
1599 if (!bfd_is_abs_section (hdh_p->sec)
1600 && bfd_is_abs_section (hdh_p->sec->output_section))
1602 /* Input section has been discarded, either because
1603 it is a copy of a linkonce section or due to
1604 linker script /DISCARD/, so we'll be discarding
1605 the relocs too. */
1607 else if (hdh_p->count != 0)
1609 srel = elf_section_data (hdh_p->sec)->sreloc;
1610 srel->size += hdh_p->count * sizeof (Elf64_External_Rela);
1611 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
1612 info->flags |= DF_TEXTREL;
1617 local_dlt = elf_local_got_refcounts (ibfd);
1618 if (!local_dlt)
1619 continue;
1621 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1622 locsymcount = symtab_hdr->sh_info;
1623 end_local_dlt = local_dlt + locsymcount;
1624 sec = hppa_info->dlt_sec;
1625 srel = hppa_info->dlt_rel_sec;
1626 for (; local_dlt < end_local_dlt; ++local_dlt)
1628 if (*local_dlt > 0)
1630 *local_dlt = sec->size;
1631 sec->size += DLT_ENTRY_SIZE;
1632 if (bfd_link_pic (info))
1634 srel->size += sizeof (Elf64_External_Rela);
1637 else
1638 *local_dlt = (bfd_vma) -1;
1641 local_plt = end_local_dlt;
1642 end_local_plt = local_plt + locsymcount;
1643 if (! hppa_info->root.dynamic_sections_created)
1645 /* Won't be used, but be safe. */
1646 for (; local_plt < end_local_plt; ++local_plt)
1647 *local_plt = (bfd_vma) -1;
1649 else
1651 sec = hppa_info->root.splt;
1652 srel = hppa_info->root.srelplt;
1653 for (; local_plt < end_local_plt; ++local_plt)
1655 if (*local_plt > 0)
1657 *local_plt = sec->size;
1658 sec->size += PLT_ENTRY_SIZE;
1659 if (bfd_link_pic (info))
1660 srel->size += sizeof (Elf64_External_Rela);
1662 else
1663 *local_plt = (bfd_vma) -1;
1667 local_opd = end_local_plt;
1668 end_local_opd = local_opd + locsymcount;
1669 if (! hppa_info->root.dynamic_sections_created)
1671 /* Won't be used, but be safe. */
1672 for (; local_opd < end_local_opd; ++local_opd)
1673 *local_opd = (bfd_vma) -1;
1675 else
1677 sec = hppa_info->opd_sec;
1678 srel = hppa_info->opd_rel_sec;
1679 for (; local_opd < end_local_opd; ++local_opd)
1681 if (*local_opd > 0)
1683 *local_opd = sec->size;
1684 sec->size += OPD_ENTRY_SIZE;
1685 if (bfd_link_pic (info))
1686 srel->size += sizeof (Elf64_External_Rela);
1688 else
1689 *local_opd = (bfd_vma) -1;
1694 /* Allocate the GOT entries. */
1696 data.info = info;
1697 if (hppa_info->dlt_sec)
1699 data.ofs = hppa_info->dlt_sec->size;
1700 elf_link_hash_traverse (&hppa_info->root,
1701 allocate_global_data_dlt, &data);
1702 hppa_info->dlt_sec->size = data.ofs;
1705 if (hppa_info->root.splt)
1707 data.ofs = hppa_info->root.splt->size;
1708 elf_link_hash_traverse (&hppa_info->root,
1709 allocate_global_data_plt, &data);
1710 hppa_info->root.splt->size = data.ofs;
1713 if (hppa_info->stub_sec)
1715 data.ofs = 0x0;
1716 elf_link_hash_traverse (&hppa_info->root,
1717 allocate_global_data_stub, &data);
1718 hppa_info->stub_sec->size = data.ofs;
1721 /* Allocate space for entries in the .opd section. */
1722 if (hppa_info->opd_sec)
1724 data.ofs = hppa_info->opd_sec->size;
1725 elf_link_hash_traverse (&hppa_info->root,
1726 allocate_global_data_opd, &data);
1727 hppa_info->opd_sec->size = data.ofs;
1730 /* Now allocate space for dynamic relocations, if necessary. */
1731 if (hppa_info->root.dynamic_sections_created)
1732 elf_link_hash_traverse (&hppa_info->root,
1733 allocate_dynrel_entries, &data);
1735 /* The sizes of all the sections are set. Allocate memory for them. */
1736 relocs = false;
1737 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
1739 const char *name;
1741 if ((sec->flags & SEC_LINKER_CREATED) == 0)
1742 continue;
1744 /* It's OK to base decisions on the section name, because none
1745 of the dynobj section names depend upon the input files. */
1746 name = bfd_section_name (sec);
1748 if (strcmp (name, ".plt") == 0)
1750 /* Remember whether there is a PLT. */
1753 else if (strcmp (name, ".opd") == 0
1754 || startswith (name, ".dlt")
1755 || strcmp (name, ".stub") == 0
1756 || strcmp (name, ".got") == 0)
1758 /* Strip this section if we don't need it; see the comment below. */
1760 else if (startswith (name, ".rela"))
1762 if (sec->size != 0)
1764 /* Remember whether there are any reloc sections other
1765 than .rela.plt. */
1766 if (strcmp (name, ".rela.plt") != 0)
1767 relocs = true;
1769 /* We use the reloc_count field as a counter if we need
1770 to copy relocs into the output file. */
1771 sec->reloc_count = 0;
1774 else
1776 /* It's not one of our sections, so don't allocate space. */
1777 continue;
1780 if (sec->size == 0)
1782 /* If we don't need this section, strip it from the
1783 output file. This is mostly to handle .rela.bss and
1784 .rela.plt. We must create both sections in
1785 create_dynamic_sections, because they must be created
1786 before the linker maps input sections to output
1787 sections. The linker does that before
1788 adjust_dynamic_symbol is called, and it is that
1789 function which decides whether anything needs to go
1790 into these sections. */
1791 sec->flags |= SEC_EXCLUDE;
1792 continue;
1795 if ((sec->flags & SEC_HAS_CONTENTS) == 0)
1796 continue;
1798 /* Allocate memory for the section contents if it has not
1799 been allocated already. We use bfd_zalloc here in case
1800 unused entries are not reclaimed before the section's
1801 contents are written out. This should not happen, but this
1802 way if it does, we get a R_PARISC_NONE reloc instead of
1803 garbage. */
1804 if (sec->contents == NULL)
1806 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
1807 if (sec->contents == NULL)
1808 return false;
1812 if (hppa_info->root.dynamic_sections_created)
1814 /* Always create a DT_PLTGOT. It actually has nothing to do with
1815 the PLT, it is how we communicate the __gp value of a load
1816 module to the dynamic linker. */
1817 #define add_dynamic_entry(TAG, VAL) \
1818 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1820 if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0))
1821 return false;
1823 /* Add some entries to the .dynamic section. We fill in the
1824 values later, in elf64_hppa_finish_dynamic_sections, but we
1825 must add the entries now so that we get the correct size for
1826 the .dynamic section. The DT_DEBUG entry is filled in by the
1827 dynamic linker and used by the debugger. */
1828 if (! bfd_link_pic (info))
1830 if (!add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1831 || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
1832 return false;
1835 /* Force DT_FLAGS to always be set.
1836 Required by HPUX 11.00 patch PHSS_26559. */
1837 if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1838 return false;
1840 #undef add_dynamic_entry
1842 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
1845 /* Called after we have output the symbol into the dynamic symbol
1846 table, but before we output the symbol into the normal symbol
1847 table.
1849 For some symbols we had to change their address when outputting
1850 the dynamic symbol table. We undo that change here so that
1851 the symbols have their expected value in the normal symbol
1852 table. Ick. */
1854 static int
1855 elf64_hppa_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1856 const char *name,
1857 Elf_Internal_Sym *sym,
1858 asection *input_sec ATTRIBUTE_UNUSED,
1859 struct elf_link_hash_entry *eh)
1861 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1863 /* We may be called with the file symbol or section symbols.
1864 They never need munging, so it is safe to ignore them. */
1865 if (!name || !eh)
1866 return 1;
1868 /* Function symbols for which we created .opd entries *may* have been
1869 munged by finish_dynamic_symbol and have to be un-munged here.
1871 Note that finish_dynamic_symbol sometimes turns dynamic symbols
1872 into non-dynamic ones, so we initialize st_shndx to -1 in
1873 mark_exported_functions and check to see if it was overwritten
1874 here instead of just checking eh->dynindx. */
1875 if (hh->want_opd && hh->st_shndx != -1)
1877 /* Restore the saved value and section index. */
1878 sym->st_value = hh->st_value;
1879 sym->st_shndx = hh->st_shndx;
1882 return 1;
1885 /* Finish up dynamic symbol handling. We set the contents of various
1886 dynamic sections here. */
1888 static bool
1889 elf64_hppa_finish_dynamic_symbol (bfd *output_bfd,
1890 struct bfd_link_info *info,
1891 struct elf_link_hash_entry *eh,
1892 Elf_Internal_Sym *sym)
1894 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
1895 asection *stub, *splt, *sopd, *spltrel;
1896 struct elf64_hppa_link_hash_table *hppa_info;
1898 hppa_info = hppa_link_hash_table (info);
1900 stub = hppa_info->stub_sec;
1901 splt = hppa_info->root.splt;
1902 sopd = hppa_info->opd_sec;
1903 spltrel = hppa_info->root.srelplt;
1905 /* Incredible. It is actually necessary to NOT use the symbol's real
1906 value when building the dynamic symbol table for a shared library.
1907 At least for symbols that refer to functions.
1909 We will store a new value and section index into the symbol long
1910 enough to output it into the dynamic symbol table, then we restore
1911 the original values (in elf64_hppa_link_output_symbol_hook). */
1912 if (hh->want_opd)
1914 BFD_ASSERT (sopd != NULL);
1916 /* Save away the original value and section index so that we
1917 can restore them later. */
1918 hh->st_value = sym->st_value;
1919 hh->st_shndx = sym->st_shndx;
1921 /* For the dynamic symbol table entry, we want the value to be
1922 address of this symbol's entry within the .opd section. */
1923 sym->st_value = (hh->opd_offset
1924 + sopd->output_offset
1925 + sopd->output_section->vma);
1926 sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1927 sopd->output_section);
1930 /* Initialize a .plt entry if requested. */
1931 if (hh->want_plt
1932 && elf64_hppa_dynamic_symbol_p (eh, info))
1934 bfd_vma value;
1935 Elf_Internal_Rela rel;
1936 bfd_byte *loc;
1938 BFD_ASSERT (splt != NULL && spltrel != NULL);
1940 /* We do not actually care about the value in the PLT entry
1941 if we are creating a shared library and the symbol is
1942 still undefined, we create a dynamic relocation to fill
1943 in the correct value. */
1944 if (bfd_link_pic (info) && eh->root.type == bfd_link_hash_undefined)
1945 value = 0;
1946 else
1947 value = (eh->root.u.def.value + eh->root.u.def.section->vma);
1949 /* Fill in the entry in the procedure linkage table.
1951 The format of a plt entry is
1952 <funcaddr> <__gp>.
1954 plt_offset is the offset within the PLT section at which to
1955 install the PLT entry.
1957 We are modifying the in-memory PLT contents here, so we do not add
1958 in the output_offset of the PLT section. */
1960 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset);
1961 value = _bfd_get_gp_value (info->output_bfd);
1962 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset + 0x8);
1964 /* Create a dynamic IPLT relocation for this entry.
1966 We are creating a relocation in the output file's PLT section,
1967 which is included within the DLT secton. So we do need to include
1968 the PLT's output_offset in the computation of the relocation's
1969 address. */
1970 rel.r_offset = (hh->plt_offset + splt->output_offset
1971 + splt->output_section->vma);
1972 rel.r_info = ELF64_R_INFO (hh->eh.dynindx, R_PARISC_IPLT);
1973 rel.r_addend = 0;
1975 loc = spltrel->contents;
1976 loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
1977 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
1980 /* Initialize an external call stub entry if requested. */
1981 if (hh->want_stub
1982 && elf64_hppa_dynamic_symbol_p (eh, info))
1984 bfd_vma value;
1985 int insn;
1986 unsigned int max_offset;
1988 BFD_ASSERT (stub != NULL);
1990 /* Install the generic stub template.
1992 We are modifying the contents of the stub section, so we do not
1993 need to include the stub section's output_offset here. */
1994 memcpy (stub->contents + hh->stub_offset, plt_stub, sizeof (plt_stub));
1996 /* Fix up the first ldd instruction.
1998 We are modifying the contents of the STUB section in memory,
1999 so we do not need to include its output offset in this computation.
2001 Note the plt_offset value is the value of the PLT entry relative to
2002 the start of the PLT section. These instructions will reference
2003 data relative to the value of __gp, which may not necessarily have
2004 the same address as the start of the PLT section.
2006 gp_offset contains the offset of __gp within the PLT section. */
2007 value = hh->plt_offset - hppa_info->gp_offset;
2009 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset);
2010 if (output_bfd->arch_info->mach >= 25)
2012 /* Wide mode allows 16 bit offsets. */
2013 max_offset = 32768;
2014 insn &= ~ 0xfff1;
2015 insn |= re_assemble_16 ((int) value);
2017 else
2019 max_offset = 8192;
2020 insn &= ~ 0x3ff1;
2021 insn |= re_assemble_14 ((int) value);
2024 if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2026 _bfd_error_handler
2027 /* xgettext:c-format */
2028 (_("stub entry for %s cannot load .plt, dp offset = %" PRId64),
2029 hh->eh.root.root.string, (int64_t) value);
2030 return false;
2033 bfd_put_32 (stub->owner, (bfd_vma) insn,
2034 stub->contents + hh->stub_offset);
2036 /* Fix up the second ldd instruction. */
2037 value += 8;
2038 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset + 8);
2039 if (output_bfd->arch_info->mach >= 25)
2041 insn &= ~ 0xfff1;
2042 insn |= re_assemble_16 ((int) value);
2044 else
2046 insn &= ~ 0x3ff1;
2047 insn |= re_assemble_14 ((int) value);
2049 bfd_put_32 (stub->owner, (bfd_vma) insn,
2050 stub->contents + hh->stub_offset + 8);
2053 return true;
2056 /* The .opd section contains FPTRs for each function this file
2057 exports. Initialize the FPTR entries. */
2059 static bool
2060 elf64_hppa_finalize_opd (struct elf_link_hash_entry *eh, void *data)
2062 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2063 struct bfd_link_info *info = (struct bfd_link_info *)data;
2064 struct elf64_hppa_link_hash_table *hppa_info;
2065 asection *sopd;
2066 asection *sopdrel;
2068 hppa_info = hppa_link_hash_table (info);
2069 if (hppa_info == NULL)
2070 return false;
2072 sopd = hppa_info->opd_sec;
2073 sopdrel = hppa_info->opd_rel_sec;
2075 if (hh->want_opd)
2077 bfd_vma value;
2079 /* The first two words of an .opd entry are zero.
2081 We are modifying the contents of the OPD section in memory, so we
2082 do not need to include its output offset in this computation. */
2083 memset (sopd->contents + hh->opd_offset, 0, 16);
2085 value = (eh->root.u.def.value
2086 + eh->root.u.def.section->output_section->vma
2087 + eh->root.u.def.section->output_offset);
2089 /* The next word is the address of the function. */
2090 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 16);
2092 /* The last word is our local __gp value. */
2093 value = _bfd_get_gp_value (info->output_bfd);
2094 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 24);
2097 /* If we are generating a shared library, we must generate EPLT relocations
2098 for each entry in the .opd, even for static functions (they may have
2099 had their address taken). */
2100 if (bfd_link_pic (info) && hh->want_opd)
2102 Elf_Internal_Rela rel;
2103 bfd_byte *loc;
2104 int dynindx;
2106 /* We may need to do a relocation against a local symbol, in
2107 which case we have to look up it's dynamic symbol index off
2108 the local symbol hash table. */
2109 if (eh->dynindx != -1)
2110 dynindx = eh->dynindx;
2111 else
2112 dynindx
2113 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2114 hh->sym_indx);
2116 /* The offset of this relocation is the absolute address of the
2117 .opd entry for this symbol. */
2118 rel.r_offset = (hh->opd_offset + sopd->output_offset
2119 + sopd->output_section->vma);
2121 /* If H is non-null, then we have an external symbol.
2123 It is imperative that we use a different dynamic symbol for the
2124 EPLT relocation if the symbol has global scope.
2126 In the dynamic symbol table, the function symbol will have a value
2127 which is address of the function's .opd entry.
2129 Thus, we can not use that dynamic symbol for the EPLT relocation
2130 (if we did, the data in the .opd would reference itself rather
2131 than the actual address of the function). Instead we have to use
2132 a new dynamic symbol which has the same value as the original global
2133 function symbol.
2135 We prefix the original symbol with a "." and use the new symbol in
2136 the EPLT relocation. This new symbol has already been recorded in
2137 the symbol table, we just have to look it up and use it.
2139 We do not have such problems with static functions because we do
2140 not make their addresses in the dynamic symbol table point to
2141 the .opd entry. Ultimately this should be safe since a static
2142 function can not be directly referenced outside of its shared
2143 library.
2145 We do have to play similar games for FPTR relocations in shared
2146 libraries, including those for static symbols. See the FPTR
2147 handling in elf64_hppa_finalize_dynreloc. */
2148 if (eh)
2150 char *new_name;
2151 struct elf_link_hash_entry *nh;
2153 new_name = concat (".", eh->root.root.string, NULL);
2155 nh = elf_link_hash_lookup (elf_hash_table (info),
2156 new_name, true, true, false);
2158 /* All we really want from the new symbol is its dynamic
2159 symbol index. */
2160 if (nh)
2161 dynindx = nh->dynindx;
2162 free (new_name);
2165 rel.r_addend = 0;
2166 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2168 loc = sopdrel->contents;
2169 loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2170 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2172 return true;
2175 /* The .dlt section contains addresses for items referenced through the
2176 dlt. Note that we can have a DLTIND relocation for a local symbol, thus
2177 we can not depend on finish_dynamic_symbol to initialize the .dlt. */
2179 static bool
2180 elf64_hppa_finalize_dlt (struct elf_link_hash_entry *eh, void *data)
2182 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2183 struct bfd_link_info *info = (struct bfd_link_info *)data;
2184 struct elf64_hppa_link_hash_table *hppa_info;
2185 asection *sdlt, *sdltrel;
2187 hppa_info = hppa_link_hash_table (info);
2188 if (hppa_info == NULL)
2189 return false;
2191 sdlt = hppa_info->dlt_sec;
2192 sdltrel = hppa_info->dlt_rel_sec;
2194 /* H/DYN_H may refer to a local variable and we know it's
2195 address, so there is no need to create a relocation. Just install
2196 the proper value into the DLT, note this shortcut can not be
2197 skipped when building a shared library. */
2198 if (! bfd_link_pic (info) && hh && hh->want_dlt)
2200 bfd_vma value;
2202 /* If we had an LTOFF_FPTR style relocation we want the DLT entry
2203 to point to the FPTR entry in the .opd section.
2205 We include the OPD's output offset in this computation as
2206 we are referring to an absolute address in the resulting
2207 object file. */
2208 if (hh->want_opd)
2210 value = (hh->opd_offset
2211 + hppa_info->opd_sec->output_offset
2212 + hppa_info->opd_sec->output_section->vma);
2214 else if ((eh->root.type == bfd_link_hash_defined
2215 || eh->root.type == bfd_link_hash_defweak)
2216 && eh->root.u.def.section)
2218 value = eh->root.u.def.value + eh->root.u.def.section->output_offset;
2219 if (eh->root.u.def.section->output_section)
2220 value += eh->root.u.def.section->output_section->vma;
2221 else
2222 value += eh->root.u.def.section->vma;
2224 else
2225 /* We have an undefined function reference. */
2226 value = 0;
2228 /* We do not need to include the output offset of the DLT section
2229 here because we are modifying the in-memory contents. */
2230 bfd_put_64 (sdlt->owner, value, sdlt->contents + hh->dlt_offset);
2233 /* Create a relocation for the DLT entry associated with this symbol.
2234 When building a shared library the symbol does not have to be dynamic. */
2235 if (hh->want_dlt
2236 && (elf64_hppa_dynamic_symbol_p (eh, info) || bfd_link_pic (info)))
2238 Elf_Internal_Rela rel;
2239 bfd_byte *loc;
2240 int dynindx;
2242 /* We may need to do a relocation against a local symbol, in
2243 which case we have to look up it's dynamic symbol index off
2244 the local symbol hash table. */
2245 if (eh && eh->dynindx != -1)
2246 dynindx = eh->dynindx;
2247 else
2248 dynindx
2249 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2250 hh->sym_indx);
2252 /* Create a dynamic relocation for this entry. Do include the output
2253 offset of the DLT entry since we need an absolute address in the
2254 resulting object file. */
2255 rel.r_offset = (hh->dlt_offset + sdlt->output_offset
2256 + sdlt->output_section->vma);
2257 if (eh && eh->type == STT_FUNC)
2258 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2259 else
2260 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2261 rel.r_addend = 0;
2263 loc = sdltrel->contents;
2264 loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2265 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2267 return true;
2270 /* Finalize the dynamic relocations. Specifically the FPTR relocations
2271 for dynamic functions used to initialize static data. */
2273 static bool
2274 elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
2275 void *data)
2277 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
2278 struct bfd_link_info *info = (struct bfd_link_info *)data;
2279 struct elf64_hppa_link_hash_table *hppa_info;
2280 int dynamic_symbol;
2282 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, info);
2284 if (!dynamic_symbol && !bfd_link_pic (info))
2285 return true;
2287 if (hh->reloc_entries)
2289 struct elf64_hppa_dyn_reloc_entry *rent;
2290 int dynindx;
2292 hppa_info = hppa_link_hash_table (info);
2293 if (hppa_info == NULL)
2294 return false;
2296 /* We may need to do a relocation against a local symbol, in
2297 which case we have to look up it's dynamic symbol index off
2298 the local symbol hash table. */
2299 if (eh->dynindx != -1)
2300 dynindx = eh->dynindx;
2301 else
2302 dynindx
2303 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2304 hh->sym_indx);
2306 for (rent = hh->reloc_entries; rent; rent = rent->next)
2308 Elf_Internal_Rela rel;
2309 bfd_byte *loc;
2311 /* Allocate one iff we are building a shared library, the relocation
2312 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
2313 if (!bfd_link_pic (info)
2314 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2315 continue;
2317 /* Create a dynamic relocation for this entry.
2319 We need the output offset for the reloc's section because
2320 we are creating an absolute address in the resulting object
2321 file. */
2322 rel.r_offset = (rent->offset + rent->sec->output_offset
2323 + rent->sec->output_section->vma);
2325 /* An FPTR64 relocation implies that we took the address of
2326 a function and that the function has an entry in the .opd
2327 section. We want the FPTR64 relocation to reference the
2328 entry in .opd.
2330 We could munge the symbol value in the dynamic symbol table
2331 (in fact we already do for functions with global scope) to point
2332 to the .opd entry. Then we could use that dynamic symbol in
2333 this relocation.
2335 Or we could do something sensible, not munge the symbol's
2336 address and instead just use a different symbol to reference
2337 the .opd entry. At least that seems sensible until you
2338 realize there's no local dynamic symbols we can use for that
2339 purpose. Thus the hair in the check_relocs routine.
2341 We use a section symbol recorded by check_relocs as the
2342 base symbol for the relocation. The addend is the difference
2343 between the section symbol and the address of the .opd entry. */
2344 if (bfd_link_pic (info)
2345 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
2347 bfd_vma value, value2;
2349 /* First compute the address of the opd entry for this symbol. */
2350 value = (hh->opd_offset
2351 + hppa_info->opd_sec->output_section->vma
2352 + hppa_info->opd_sec->output_offset);
2354 /* Compute the value of the start of the section with
2355 the relocation. */
2356 value2 = (rent->sec->output_section->vma
2357 + rent->sec->output_offset);
2359 /* Compute the difference between the start of the section
2360 with the relocation and the opd entry. */
2361 value -= value2;
2363 /* The result becomes the addend of the relocation. */
2364 rel.r_addend = value;
2366 /* The section symbol becomes the symbol for the dynamic
2367 relocation. */
2368 dynindx
2369 = _bfd_elf_link_lookup_local_dynindx (info,
2370 rent->sec->owner,
2371 rent->sec_symndx);
2373 else
2374 rel.r_addend = rent->addend;
2376 rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2378 loc = hppa_info->other_rel_sec->contents;
2379 loc += (hppa_info->other_rel_sec->reloc_count++
2380 * sizeof (Elf64_External_Rela));
2381 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
2385 return true;
2388 /* Used to decide how to sort relocs in an optimal manner for the
2389 dynamic linker, before writing them out. */
2391 static enum elf_reloc_type_class
2392 elf64_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2393 const asection *rel_sec ATTRIBUTE_UNUSED,
2394 const Elf_Internal_Rela *rela)
2396 if (ELF64_R_SYM (rela->r_info) == STN_UNDEF)
2397 return reloc_class_relative;
2399 switch ((int) ELF64_R_TYPE (rela->r_info))
2401 case R_PARISC_IPLT:
2402 return reloc_class_plt;
2403 case R_PARISC_COPY:
2404 return reloc_class_copy;
2405 default:
2406 return reloc_class_normal;
2410 /* Finish up the dynamic sections. */
2412 static bool
2413 elf64_hppa_finish_dynamic_sections (bfd *output_bfd,
2414 struct bfd_link_info *info)
2416 bfd *dynobj;
2417 asection *sdyn;
2418 struct elf64_hppa_link_hash_table *hppa_info;
2420 hppa_info = hppa_link_hash_table (info);
2421 if (hppa_info == NULL)
2422 return false;
2424 /* Finalize the contents of the .opd section. */
2425 elf_link_hash_traverse (elf_hash_table (info),
2426 elf64_hppa_finalize_opd,
2427 info);
2429 elf_link_hash_traverse (elf_hash_table (info),
2430 elf64_hppa_finalize_dynreloc,
2431 info);
2433 /* Finalize the contents of the .dlt section. */
2434 dynobj = elf_hash_table (info)->dynobj;
2435 /* Finalize the contents of the .dlt section. */
2436 elf_link_hash_traverse (elf_hash_table (info),
2437 elf64_hppa_finalize_dlt,
2438 info);
2440 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2442 if (elf_hash_table (info)->dynamic_sections_created)
2444 Elf64_External_Dyn *dyncon, *dynconend;
2446 BFD_ASSERT (sdyn != NULL);
2448 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2449 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2450 for (; dyncon < dynconend; dyncon++)
2452 Elf_Internal_Dyn dyn;
2453 asection *s;
2455 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2457 switch (dyn.d_tag)
2459 default:
2460 break;
2462 case DT_HP_LOAD_MAP:
2463 /* Compute the absolute address of 16byte scratchpad area
2464 for the dynamic linker.
2466 By convention the linker script will allocate the scratchpad
2467 area at the start of the .data section. So all we have to
2468 to is find the start of the .data section. */
2469 s = bfd_get_section_by_name (output_bfd, ".data");
2470 if (!s)
2471 return false;
2472 dyn.d_un.d_ptr = s->vma;
2473 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2474 break;
2476 case DT_PLTGOT:
2477 /* HP's use PLTGOT to set the GOT register. */
2478 dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2479 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2480 break;
2482 case DT_JMPREL:
2483 s = hppa_info->root.srelplt;
2484 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2485 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2486 break;
2488 case DT_PLTRELSZ:
2489 s = hppa_info->root.srelplt;
2490 dyn.d_un.d_val = s->size;
2491 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2492 break;
2494 case DT_RELA:
2495 s = hppa_info->other_rel_sec;
2496 if (! s || ! s->size)
2497 s = hppa_info->dlt_rel_sec;
2498 if (! s || ! s->size)
2499 s = hppa_info->opd_rel_sec;
2500 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2501 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2502 break;
2504 case DT_RELASZ:
2505 s = hppa_info->other_rel_sec;
2506 dyn.d_un.d_val = s->size;
2507 s = hppa_info->dlt_rel_sec;
2508 dyn.d_un.d_val += s->size;
2509 s = hppa_info->opd_rel_sec;
2510 dyn.d_un.d_val += s->size;
2511 /* There is some question about whether or not the size of
2512 the PLT relocs should be included here. HP's tools do
2513 it, so we'll emulate them. */
2514 s = hppa_info->root.srelplt;
2515 dyn.d_un.d_val += s->size;
2516 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2517 break;
2523 return true;
2526 /* Support for core dump NOTE sections. */
2528 static bool
2529 elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2531 int offset;
2532 size_t size;
2534 switch (note->descsz)
2536 default:
2537 return false;
2539 case 760: /* Linux/hppa */
2540 /* pr_cursig */
2541 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2543 /* pr_pid */
2544 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2546 /* pr_reg */
2547 offset = 112;
2548 size = 640;
2550 break;
2553 /* Make a ".reg/999" section. */
2554 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2555 size, note->descpos + offset);
2558 static bool
2559 elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2561 char * command;
2562 int n;
2564 switch (note->descsz)
2566 default:
2567 return false;
2569 case 136: /* Linux/hppa elf_prpsinfo. */
2570 elf_tdata (abfd)->core->program
2571 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2572 elf_tdata (abfd)->core->command
2573 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2576 /* Note that for some reason, a spurious space is tacked
2577 onto the end of the args in some (at least one anyway)
2578 implementations, so strip it off if it exists. */
2579 command = elf_tdata (abfd)->core->command;
2580 n = strlen (command);
2582 if (0 < n && command[n - 1] == ' ')
2583 command[n - 1] = '\0';
2585 return true;
2588 /* Return the number of additional phdrs we will need.
2590 The generic ELF code only creates PT_PHDRs for executables. The HP
2591 dynamic linker requires PT_PHDRs for dynamic libraries too.
2593 This routine indicates that the backend needs one additional program
2594 header for that case.
2596 Note we do not have access to the link info structure here, so we have
2597 to guess whether or not we are building a shared library based on the
2598 existence of a .interp section. */
2600 static int
2601 elf64_hppa_additional_program_headers (bfd *abfd,
2602 struct bfd_link_info *info ATTRIBUTE_UNUSED)
2604 asection *s;
2606 /* If we are creating a shared library, then we have to create a
2607 PT_PHDR segment. HP's dynamic linker chokes without it. */
2608 s = bfd_get_section_by_name (abfd, ".interp");
2609 if (! s)
2610 return 1;
2611 return 0;
2614 static bool
2615 elf64_hppa_allow_non_load_phdr (bfd *abfd ATTRIBUTE_UNUSED,
2616 const Elf_Internal_Phdr *phdr ATTRIBUTE_UNUSED,
2617 unsigned int count ATTRIBUTE_UNUSED)
2619 return true;
2622 /* Allocate and initialize any program headers required by this
2623 specific backend.
2625 The generic ELF code only creates PT_PHDRs for executables. The HP
2626 dynamic linker requires PT_PHDRs for dynamic libraries too.
2628 This allocates the PT_PHDR and initializes it in a manner suitable
2629 for the HP linker.
2631 Note we do not have access to the link info structure here, so we have
2632 to guess whether or not we are building a shared library based on the
2633 existence of a .interp section. */
2635 static bool
2636 elf64_hppa_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
2638 struct elf_segment_map *m;
2640 m = elf_seg_map (abfd);
2641 if (info != NULL && !info->user_phdrs && m != NULL && m->p_type != PT_PHDR)
2643 m = ((struct elf_segment_map *)
2644 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
2645 if (m == NULL)
2646 return false;
2648 m->p_type = PT_PHDR;
2649 m->p_flags = PF_R | PF_X;
2650 m->p_flags_valid = 1;
2651 m->p_paddr_valid = 1;
2652 m->includes_phdrs = 1;
2654 m->next = elf_seg_map (abfd);
2655 elf_seg_map (abfd) = m;
2658 for (m = elf_seg_map (abfd) ; m != NULL; m = m->next)
2659 if (m->p_type == PT_LOAD)
2661 unsigned int i;
2663 for (i = 0; i < m->count; i++)
2665 /* The code "hint" is not really a hint. It is a requirement
2666 for certain versions of the HP dynamic linker. Worse yet,
2667 it must be set even if the shared library does not have
2668 any code in its "text" segment (thus the check for .hash
2669 to catch this situation). */
2670 if (m->sections[i]->flags & SEC_CODE
2671 || (strcmp (m->sections[i]->name, ".hash") == 0))
2672 m->p_flags |= (PF_X | PF_HP_CODE);
2676 return true;
2679 /* Called when writing out an object file to decide the type of a
2680 symbol. */
2681 static int
2682 elf64_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym,
2683 int type)
2685 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2686 return STT_PARISC_MILLI;
2687 else
2688 return type;
2691 /* Support HP specific sections for core files. */
2693 static bool
2694 elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int sec_index,
2695 const char *typename)
2697 if (hdr->p_type == PT_HP_CORE_KERNEL)
2699 asection *sect;
2701 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2702 return false;
2704 sect = bfd_make_section_anyway (abfd, ".kernel");
2705 if (sect == NULL)
2706 return false;
2707 sect->size = hdr->p_filesz;
2708 sect->filepos = hdr->p_offset;
2709 sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
2710 return true;
2713 if (hdr->p_type == PT_HP_CORE_PROC)
2715 int sig;
2717 if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
2718 return false;
2719 if (bfd_read (&sig, 4, abfd) != 4)
2720 return false;
2722 elf_tdata (abfd)->core->signal = sig;
2724 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
2725 return false;
2727 /* GDB uses the ".reg" section to read register contents. */
2728 return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2729 hdr->p_offset);
2732 if (hdr->p_type == PT_HP_CORE_LOADABLE
2733 || hdr->p_type == PT_HP_CORE_STACK
2734 || hdr->p_type == PT_HP_CORE_MMF)
2735 hdr->p_type = PT_LOAD;
2737 return _bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename);
2740 /* Hook called by the linker routine which adds symbols from an object
2741 file. HP's libraries define symbols with HP specific section
2742 indices, which we have to handle. */
2744 static bool
2745 elf_hppa_add_symbol_hook (bfd *abfd,
2746 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2747 Elf_Internal_Sym *sym,
2748 const char **namep ATTRIBUTE_UNUSED,
2749 flagword *flagsp ATTRIBUTE_UNUSED,
2750 asection **secp,
2751 bfd_vma *valp)
2753 unsigned int sec_index = sym->st_shndx;
2755 switch (sec_index)
2757 case SHN_PARISC_ANSI_COMMON:
2758 *secp = bfd_make_section_old_way (abfd, ".PARISC.ansi.common");
2759 (*secp)->flags |= SEC_IS_COMMON;
2760 *valp = sym->st_size;
2761 break;
2763 case SHN_PARISC_HUGE_COMMON:
2764 *secp = bfd_make_section_old_way (abfd, ".PARISC.huge.common");
2765 (*secp)->flags |= SEC_IS_COMMON;
2766 *valp = sym->st_size;
2767 break;
2770 return true;
2773 static bool
2774 elf_hppa_unmark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2775 void *data)
2777 struct bfd_link_info *info = data;
2779 /* If we are not creating a shared library, and this symbol is
2780 referenced by a shared library but is not defined anywhere, then
2781 the generic code will warn that it is undefined.
2783 This behavior is undesirable on HPs since the standard shared
2784 libraries contain references to undefined symbols.
2786 So we twiddle the flags associated with such symbols so that they
2787 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2789 Ultimately we should have better controls over the generic ELF BFD
2790 linker code. */
2791 if (! bfd_link_relocatable (info)
2792 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2793 && h->root.type == bfd_link_hash_undefined
2794 && h->ref_dynamic
2795 && !h->ref_regular)
2797 h->ref_dynamic = 0;
2798 h->pointer_equality_needed = 1;
2801 return true;
2804 static bool
2805 elf_hppa_remark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2806 void *data)
2808 struct bfd_link_info *info = data;
2810 /* If we are not creating a shared library, and this symbol is
2811 referenced by a shared library but is not defined anywhere, then
2812 the generic code will warn that it is undefined.
2814 This behavior is undesirable on HPs since the standard shared
2815 libraries contain references to undefined symbols.
2817 So we twiddle the flags associated with such symbols so that they
2818 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2820 Ultimately we should have better controls over the generic ELF BFD
2821 linker code. */
2822 if (! bfd_link_relocatable (info)
2823 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2824 && h->root.type == bfd_link_hash_undefined
2825 && !h->ref_dynamic
2826 && !h->ref_regular
2827 && h->pointer_equality_needed)
2829 h->ref_dynamic = 1;
2830 h->pointer_equality_needed = 0;
2833 return true;
2836 static bool
2837 elf_hppa_is_dynamic_loader_symbol (const char *name)
2839 return (! strcmp (name, "__CPU_REVISION")
2840 || ! strcmp (name, "__CPU_KEYBITS_1")
2841 || ! strcmp (name, "__SYSTEM_ID_D")
2842 || ! strcmp (name, "__FPU_MODEL")
2843 || ! strcmp (name, "__FPU_REVISION")
2844 || ! strcmp (name, "__ARGC")
2845 || ! strcmp (name, "__ARGV")
2846 || ! strcmp (name, "__ENVP")
2847 || ! strcmp (name, "__TLS_SIZE_D")
2848 || ! strcmp (name, "__LOAD_INFO")
2849 || ! strcmp (name, "__systab"));
2852 /* Record the lowest address for the data and text segments. */
2853 static void
2854 elf_hppa_record_segment_addrs (bfd *abfd,
2855 asection *section,
2856 void *data)
2858 struct elf64_hppa_link_hash_table *hppa_info = data;
2860 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2862 bfd_vma value;
2863 Elf_Internal_Phdr *p;
2865 p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
2866 BFD_ASSERT (p != NULL);
2867 value = p->p_vaddr;
2869 if (section->flags & SEC_READONLY)
2871 if (value < hppa_info->text_segment_base)
2872 hppa_info->text_segment_base = value;
2874 else
2876 if (value < hppa_info->data_segment_base)
2877 hppa_info->data_segment_base = value;
2882 /* Called after we have seen all the input files/sections, but before
2883 final symbol resolution and section placement has been determined.
2885 We use this hook to (possibly) provide a value for __gp, then we
2886 fall back to the generic ELF final link routine. */
2888 static bool
2889 elf_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
2891 struct stat buf;
2892 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
2894 if (hppa_info == NULL)
2895 return false;
2897 if (! bfd_link_relocatable (info))
2899 struct elf_link_hash_entry *gp;
2900 bfd_vma gp_val;
2902 /* The linker script defines a value for __gp iff it was referenced
2903 by one of the objects being linked. First try to find the symbol
2904 in the hash table. If that fails, just compute the value __gp
2905 should have had. */
2906 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
2907 false, false);
2909 if (gp)
2912 /* Adjust the value of __gp as we may want to slide it into the
2913 .plt section so that the stubs can access PLT entries without
2914 using an addil sequence. */
2915 gp->root.u.def.value += hppa_info->gp_offset;
2917 gp_val = (gp->root.u.def.section->output_section->vma
2918 + gp->root.u.def.section->output_offset
2919 + gp->root.u.def.value);
2921 else
2923 asection *sec;
2925 /* First look for a .plt section. If found, then __gp is the
2926 address of the .plt + gp_offset.
2928 If no .plt is found, then look for .dlt, .opd and .data (in
2929 that order) and set __gp to the base address of whichever
2930 section is found first. */
2932 sec = hppa_info->root.splt;
2933 if (sec && ! (sec->flags & SEC_EXCLUDE))
2934 gp_val = (sec->output_offset
2935 + sec->output_section->vma
2936 + hppa_info->gp_offset);
2937 else
2939 sec = hppa_info->dlt_sec;
2940 if (!sec || (sec->flags & SEC_EXCLUDE))
2941 sec = hppa_info->opd_sec;
2942 if (!sec || (sec->flags & SEC_EXCLUDE))
2943 sec = bfd_get_section_by_name (abfd, ".data");
2944 if (!sec || (sec->flags & SEC_EXCLUDE))
2945 gp_val = 0;
2946 else
2947 gp_val = sec->output_offset + sec->output_section->vma;
2951 /* Install whatever value we found/computed for __gp. */
2952 _bfd_set_gp_value (abfd, gp_val);
2955 /* We need to know the base of the text and data segments so that we
2956 can perform SEGREL relocations. We will record the base addresses
2957 when we encounter the first SEGREL relocation. */
2958 hppa_info->text_segment_base = (bfd_vma)-1;
2959 hppa_info->data_segment_base = (bfd_vma)-1;
2961 /* HP's shared libraries have references to symbols that are not
2962 defined anywhere. The generic ELF BFD linker code will complain
2963 about such symbols.
2965 So we detect the losing case and arrange for the flags on the symbol
2966 to indicate that it was never referenced. This keeps the generic
2967 ELF BFD link code happy and appears to not create any secondary
2968 problems. Ultimately we need a way to control the behavior of the
2969 generic ELF BFD link code better. */
2970 elf_link_hash_traverse (elf_hash_table (info),
2971 elf_hppa_unmark_useless_dynamic_symbols,
2972 info);
2974 /* Invoke the regular ELF backend linker to do all the work. */
2975 if (!bfd_elf_final_link (abfd, info))
2976 return false;
2978 elf_link_hash_traverse (elf_hash_table (info),
2979 elf_hppa_remark_useless_dynamic_symbols,
2980 info);
2982 /* If we're producing a final executable, sort the contents of the
2983 unwind section. */
2984 if (bfd_link_relocatable (info))
2985 return true;
2987 /* Do not attempt to sort non-regular files. This is here
2988 especially for configure scripts and kernel builds which run
2989 tests with "ld [...] -o /dev/null". */
2990 if (stat (bfd_get_filename (abfd), &buf) != 0
2991 || !S_ISREG(buf.st_mode))
2992 return true;
2994 return elf_hppa_sort_unwind (abfd);
2997 /* Relocate the given INSN. VALUE should be the actual value we want
2998 to insert into the instruction, ie by this point we should not be
2999 concerned with computing an offset relative to the DLT, PC, etc.
3000 Instead this routine is meant to handle the bit manipulations needed
3001 to insert the relocation into the given instruction. */
3003 static int
3004 elf_hppa_relocate_insn (int insn, int sym_value, unsigned int r_type)
3006 switch (r_type)
3008 /* This is any 22 bit branch. In PA2.0 syntax it corresponds to
3009 the "B" instruction. */
3010 case R_PARISC_PCREL22F:
3011 case R_PARISC_PCREL22C:
3012 return (insn & ~0x3ff1ffd) | re_assemble_22 (sym_value);
3014 /* This is any 12 bit branch. */
3015 case R_PARISC_PCREL12F:
3016 return (insn & ~0x1ffd) | re_assemble_12 (sym_value);
3018 /* This is any 17 bit branch. In PA2.0 syntax it also corresponds
3019 to the "B" instruction as well as BE. */
3020 case R_PARISC_PCREL17F:
3021 case R_PARISC_DIR17F:
3022 case R_PARISC_DIR17R:
3023 case R_PARISC_PCREL17C:
3024 case R_PARISC_PCREL17R:
3025 return (insn & ~0x1f1ffd) | re_assemble_17 (sym_value);
3027 /* ADDIL or LDIL instructions. */
3028 case R_PARISC_DLTREL21L:
3029 case R_PARISC_DLTIND21L:
3030 case R_PARISC_LTOFF_FPTR21L:
3031 case R_PARISC_PCREL21L:
3032 case R_PARISC_LTOFF_TP21L:
3033 case R_PARISC_DPREL21L:
3034 case R_PARISC_PLTOFF21L:
3035 case R_PARISC_DIR21L:
3036 return (insn & ~0x1fffff) | re_assemble_21 (sym_value);
3038 /* LDO and integer loads/stores with 14 bit displacements. */
3039 case R_PARISC_DLTREL14R:
3040 case R_PARISC_DLTREL14F:
3041 case R_PARISC_DLTIND14R:
3042 case R_PARISC_DLTIND14F:
3043 case R_PARISC_LTOFF_FPTR14R:
3044 case R_PARISC_PCREL14R:
3045 case R_PARISC_PCREL14F:
3046 case R_PARISC_LTOFF_TP14R:
3047 case R_PARISC_LTOFF_TP14F:
3048 case R_PARISC_DPREL14R:
3049 case R_PARISC_DPREL14F:
3050 case R_PARISC_PLTOFF14R:
3051 case R_PARISC_PLTOFF14F:
3052 case R_PARISC_DIR14R:
3053 case R_PARISC_DIR14F:
3054 return (insn & ~0x3fff) | low_sign_unext (sym_value, 14);
3056 /* PA2.0W LDO and integer loads/stores with 16 bit displacements. */
3057 case R_PARISC_LTOFF_FPTR16F:
3058 case R_PARISC_PCREL16F:
3059 case R_PARISC_LTOFF_TP16F:
3060 case R_PARISC_GPREL16F:
3061 case R_PARISC_PLTOFF16F:
3062 case R_PARISC_DIR16F:
3063 case R_PARISC_LTOFF16F:
3064 return (insn & ~0xffff) | re_assemble_16 (sym_value);
3066 /* Doubleword loads and stores with a 14 bit displacement. */
3067 case R_PARISC_DLTREL14DR:
3068 case R_PARISC_DLTIND14DR:
3069 case R_PARISC_LTOFF_FPTR14DR:
3070 case R_PARISC_LTOFF_FPTR16DF:
3071 case R_PARISC_PCREL14DR:
3072 case R_PARISC_PCREL16DF:
3073 case R_PARISC_LTOFF_TP14DR:
3074 case R_PARISC_LTOFF_TP16DF:
3075 case R_PARISC_DPREL14DR:
3076 case R_PARISC_GPREL16DF:
3077 case R_PARISC_PLTOFF14DR:
3078 case R_PARISC_PLTOFF16DF:
3079 case R_PARISC_DIR14DR:
3080 case R_PARISC_DIR16DF:
3081 case R_PARISC_LTOFF16DF:
3082 return (insn & ~0x3ff1) | (((sym_value & 0x2000) >> 13)
3083 | ((sym_value & 0x1ff8) << 1));
3085 /* Floating point single word load/store instructions. */
3086 case R_PARISC_DLTREL14WR:
3087 case R_PARISC_DLTIND14WR:
3088 case R_PARISC_LTOFF_FPTR14WR:
3089 case R_PARISC_LTOFF_FPTR16WF:
3090 case R_PARISC_PCREL14WR:
3091 case R_PARISC_PCREL16WF:
3092 case R_PARISC_LTOFF_TP14WR:
3093 case R_PARISC_LTOFF_TP16WF:
3094 case R_PARISC_DPREL14WR:
3095 case R_PARISC_GPREL16WF:
3096 case R_PARISC_PLTOFF14WR:
3097 case R_PARISC_PLTOFF16WF:
3098 case R_PARISC_DIR16WF:
3099 case R_PARISC_DIR14WR:
3100 case R_PARISC_LTOFF16WF:
3101 return (insn & ~0x3ff9) | (((sym_value & 0x2000) >> 13)
3102 | ((sym_value & 0x1ffc) << 1));
3104 default:
3105 return insn;
3109 /* Compute the value for a relocation (REL) during a final link stage,
3110 then insert the value into the proper location in CONTENTS.
3112 VALUE is a tentative value for the relocation and may be overridden
3113 and modified here based on the specific relocation to be performed.
3115 For example we do conversions for PC-relative branches in this routine
3116 or redirection of calls to external routines to stubs.
3118 The work of actually applying the relocation is left to a helper
3119 routine in an attempt to reduce the complexity and size of this
3120 function. */
3122 static bfd_reloc_status_type
3123 elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
3124 bfd *input_bfd,
3125 bfd *output_bfd,
3126 asection *input_section,
3127 bfd_byte *contents,
3128 bfd_vma value,
3129 struct bfd_link_info *info,
3130 asection *sym_sec,
3131 struct elf_link_hash_entry *eh)
3133 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
3134 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
3135 bfd_vma *local_offsets;
3136 Elf_Internal_Shdr *symtab_hdr;
3137 int insn;
3138 bfd_vma max_branch_offset = 0;
3139 bfd_vma offset = rel->r_offset;
3140 bfd_signed_vma addend = rel->r_addend;
3141 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3142 unsigned int r_symndx = ELF_R_SYM (rel->r_info);
3143 unsigned int r_type = howto->type;
3144 bfd_byte *hit_data = contents + offset;
3146 if (hppa_info == NULL)
3147 return bfd_reloc_notsupported;
3149 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3150 local_offsets = elf_local_got_offsets (input_bfd);
3151 insn = bfd_get_32 (input_bfd, hit_data);
3153 switch (r_type)
3155 case R_PARISC_NONE:
3156 break;
3158 /* Basic function call support.
3160 Note for a call to a function defined in another dynamic library
3161 we want to redirect the call to a stub. */
3163 /* PC relative relocs without an implicit offset. */
3164 case R_PARISC_PCREL21L:
3165 case R_PARISC_PCREL14R:
3166 case R_PARISC_PCREL14F:
3167 case R_PARISC_PCREL14WR:
3168 case R_PARISC_PCREL14DR:
3169 case R_PARISC_PCREL16F:
3170 case R_PARISC_PCREL16WF:
3171 case R_PARISC_PCREL16DF:
3173 /* If this is a call to a function defined in another dynamic
3174 library, then redirect the call to the local stub for this
3175 function. */
3176 if (sym_sec == NULL || sym_sec->output_section == NULL)
3177 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3178 + hppa_info->stub_sec->output_section->vma);
3180 /* Turn VALUE into a proper PC relative address. */
3181 value -= (offset + input_section->output_offset
3182 + input_section->output_section->vma);
3184 /* Adjust for any field selectors. */
3185 if (r_type == R_PARISC_PCREL21L)
3186 value = hppa_field_adjust (value, -8 + addend, e_lsel);
3187 else if (r_type == R_PARISC_PCREL14F
3188 || r_type == R_PARISC_PCREL16F
3189 || r_type == R_PARISC_PCREL16WF
3190 || r_type == R_PARISC_PCREL16DF)
3191 value = hppa_field_adjust (value, -8 + addend, e_fsel);
3192 else
3193 value = hppa_field_adjust (value, -8 + addend, e_rsel);
3195 /* Apply the relocation to the given instruction. */
3196 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3197 break;
3200 case R_PARISC_PCREL12F:
3201 case R_PARISC_PCREL22F:
3202 case R_PARISC_PCREL17F:
3203 case R_PARISC_PCREL22C:
3204 case R_PARISC_PCREL17C:
3205 case R_PARISC_PCREL17R:
3207 /* If this is a call to a function defined in another dynamic
3208 library, then redirect the call to the local stub for this
3209 function. */
3210 if (sym_sec == NULL || sym_sec->output_section == NULL)
3211 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3212 + hppa_info->stub_sec->output_section->vma);
3214 /* Turn VALUE into a proper PC relative address. */
3215 value -= (offset + input_section->output_offset
3216 + input_section->output_section->vma);
3217 addend -= 8;
3219 if (r_type == (unsigned int) R_PARISC_PCREL22F)
3220 max_branch_offset = (1 << (22-1)) << 2;
3221 else if (r_type == (unsigned int) R_PARISC_PCREL17F)
3222 max_branch_offset = (1 << (17-1)) << 2;
3223 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3224 max_branch_offset = (1 << (12-1)) << 2;
3226 /* Make sure we can reach the branch target. */
3227 if (max_branch_offset != 0
3228 && value + addend + max_branch_offset >= 2*max_branch_offset)
3230 _bfd_error_handler
3231 /* xgettext:c-format */
3232 (_("%pB(%pA+%#" PRIx64 "): cannot reach %s"),
3233 input_bfd,
3234 input_section,
3235 (uint64_t) offset,
3236 eh ? eh->root.root.string : "unknown");
3237 bfd_set_error (bfd_error_bad_value);
3238 return bfd_reloc_overflow;
3241 /* Adjust for any field selectors. */
3242 if (r_type == R_PARISC_PCREL17R)
3243 value = hppa_field_adjust (value, addend, e_rsel);
3244 else
3245 value = hppa_field_adjust (value, addend, e_fsel);
3247 /* All branches are implicitly shifted by 2 places. */
3248 value >>= 2;
3250 /* Apply the relocation to the given instruction. */
3251 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3252 break;
3255 /* Indirect references to data through the DLT. */
3256 case R_PARISC_DLTIND14R:
3257 case R_PARISC_DLTIND14F:
3258 case R_PARISC_DLTIND14DR:
3259 case R_PARISC_DLTIND14WR:
3260 case R_PARISC_DLTIND21L:
3261 case R_PARISC_LTOFF_FPTR14R:
3262 case R_PARISC_LTOFF_FPTR14DR:
3263 case R_PARISC_LTOFF_FPTR14WR:
3264 case R_PARISC_LTOFF_FPTR21L:
3265 case R_PARISC_LTOFF_FPTR16F:
3266 case R_PARISC_LTOFF_FPTR16WF:
3267 case R_PARISC_LTOFF_FPTR16DF:
3268 case R_PARISC_LTOFF_TP21L:
3269 case R_PARISC_LTOFF_TP14R:
3270 case R_PARISC_LTOFF_TP14F:
3271 case R_PARISC_LTOFF_TP14WR:
3272 case R_PARISC_LTOFF_TP14DR:
3273 case R_PARISC_LTOFF_TP16F:
3274 case R_PARISC_LTOFF_TP16WF:
3275 case R_PARISC_LTOFF_TP16DF:
3276 case R_PARISC_LTOFF16F:
3277 case R_PARISC_LTOFF16WF:
3278 case R_PARISC_LTOFF16DF:
3280 bfd_vma off;
3282 /* If this relocation was against a local symbol, then we still
3283 have not set up the DLT entry (it's not convenient to do so
3284 in the "finalize_dlt" routine because it is difficult to get
3285 to the local symbol's value).
3287 So, if this is a local symbol (h == NULL), then we need to
3288 fill in its DLT entry.
3290 Similarly we may still need to set up an entry in .opd for
3291 a local function which had its address taken. */
3292 if (hh == NULL)
3294 bfd_vma *local_opd_offsets, *local_dlt_offsets;
3296 if (local_offsets == NULL)
3297 abort ();
3299 /* Now do .opd creation if needed. */
3300 if (r_type == R_PARISC_LTOFF_FPTR14R
3301 || r_type == R_PARISC_LTOFF_FPTR14DR
3302 || r_type == R_PARISC_LTOFF_FPTR14WR
3303 || r_type == R_PARISC_LTOFF_FPTR21L
3304 || r_type == R_PARISC_LTOFF_FPTR16F
3305 || r_type == R_PARISC_LTOFF_FPTR16WF
3306 || r_type == R_PARISC_LTOFF_FPTR16DF)
3308 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3309 off = local_opd_offsets[r_symndx];
3311 /* The last bit records whether we've already initialised
3312 this local .opd entry. */
3313 if ((off & 1) != 0)
3315 BFD_ASSERT (off != (bfd_vma) -1);
3316 off &= ~1;
3318 else
3320 local_opd_offsets[r_symndx] |= 1;
3322 /* The first two words of an .opd entry are zero. */
3323 memset (hppa_info->opd_sec->contents + off, 0, 16);
3325 /* The next word is the address of the function. */
3326 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3327 (hppa_info->opd_sec->contents + off + 16));
3329 /* The last word is our local __gp value. */
3330 value = _bfd_get_gp_value (info->output_bfd);
3331 bfd_put_64 (hppa_info->opd_sec->owner, value,
3332 (hppa_info->opd_sec->contents + off + 24));
3335 /* The DLT value is the address of the .opd entry. */
3336 value = (off
3337 + hppa_info->opd_sec->output_offset
3338 + hppa_info->opd_sec->output_section->vma);
3339 addend = 0;
3342 local_dlt_offsets = local_offsets;
3343 off = local_dlt_offsets[r_symndx];
3345 if ((off & 1) != 0)
3347 BFD_ASSERT (off != (bfd_vma) -1);
3348 off &= ~1;
3350 else
3352 local_dlt_offsets[r_symndx] |= 1;
3353 bfd_put_64 (hppa_info->dlt_sec->owner,
3354 value + addend,
3355 hppa_info->dlt_sec->contents + off);
3358 else
3359 off = hh->dlt_offset;
3361 /* We want the value of the DLT offset for this symbol, not
3362 the symbol's actual address. Note that __gp may not point
3363 to the start of the DLT, so we have to compute the absolute
3364 address, then subtract out the value of __gp. */
3365 value = (off
3366 + hppa_info->dlt_sec->output_offset
3367 + hppa_info->dlt_sec->output_section->vma);
3368 value -= _bfd_get_gp_value (output_bfd);
3370 /* All DLTIND relocations are basically the same at this point,
3371 except that we need different field selectors for the 21bit
3372 version vs the 14bit versions. */
3373 if (r_type == R_PARISC_DLTIND21L
3374 || r_type == R_PARISC_LTOFF_FPTR21L
3375 || r_type == R_PARISC_LTOFF_TP21L)
3376 value = hppa_field_adjust (value, 0, e_lsel);
3377 else if (r_type == R_PARISC_DLTIND14F
3378 || r_type == R_PARISC_LTOFF_FPTR16F
3379 || r_type == R_PARISC_LTOFF_FPTR16WF
3380 || r_type == R_PARISC_LTOFF_FPTR16DF
3381 || r_type == R_PARISC_LTOFF16F
3382 || r_type == R_PARISC_LTOFF16DF
3383 || r_type == R_PARISC_LTOFF16WF
3384 || r_type == R_PARISC_LTOFF_TP16F
3385 || r_type == R_PARISC_LTOFF_TP16WF
3386 || r_type == R_PARISC_LTOFF_TP16DF)
3387 value = hppa_field_adjust (value, 0, e_fsel);
3388 else
3389 value = hppa_field_adjust (value, 0, e_rsel);
3391 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3392 break;
3395 case R_PARISC_DLTREL14R:
3396 case R_PARISC_DLTREL14F:
3397 case R_PARISC_DLTREL14DR:
3398 case R_PARISC_DLTREL14WR:
3399 case R_PARISC_DLTREL21L:
3400 case R_PARISC_DPREL21L:
3401 case R_PARISC_DPREL14WR:
3402 case R_PARISC_DPREL14DR:
3403 case R_PARISC_DPREL14R:
3404 case R_PARISC_DPREL14F:
3405 case R_PARISC_GPREL16F:
3406 case R_PARISC_GPREL16WF:
3407 case R_PARISC_GPREL16DF:
3409 /* Subtract out the global pointer value to make value a DLT
3410 relative address. */
3411 value -= _bfd_get_gp_value (output_bfd);
3413 /* All DLTREL relocations are basically the same at this point,
3414 except that we need different field selectors for the 21bit
3415 version vs the 14bit versions. */
3416 if (r_type == R_PARISC_DLTREL21L
3417 || r_type == R_PARISC_DPREL21L)
3418 value = hppa_field_adjust (value, addend, e_lrsel);
3419 else if (r_type == R_PARISC_DLTREL14F
3420 || r_type == R_PARISC_DPREL14F
3421 || r_type == R_PARISC_GPREL16F
3422 || r_type == R_PARISC_GPREL16WF
3423 || r_type == R_PARISC_GPREL16DF)
3424 value = hppa_field_adjust (value, addend, e_fsel);
3425 else
3426 value = hppa_field_adjust (value, addend, e_rrsel);
3428 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3429 break;
3432 case R_PARISC_DIR21L:
3433 case R_PARISC_DIR17R:
3434 case R_PARISC_DIR17F:
3435 case R_PARISC_DIR14R:
3436 case R_PARISC_DIR14F:
3437 case R_PARISC_DIR14WR:
3438 case R_PARISC_DIR14DR:
3439 case R_PARISC_DIR16F:
3440 case R_PARISC_DIR16WF:
3441 case R_PARISC_DIR16DF:
3443 /* All DIR relocations are basically the same at this point,
3444 except that branch offsets need to be divided by four, and
3445 we need different field selectors. Note that we don't
3446 redirect absolute calls to local stubs. */
3448 if (r_type == R_PARISC_DIR21L)
3449 value = hppa_field_adjust (value, addend, e_lrsel);
3450 else if (r_type == R_PARISC_DIR17F
3451 || r_type == R_PARISC_DIR16F
3452 || r_type == R_PARISC_DIR16WF
3453 || r_type == R_PARISC_DIR16DF
3454 || r_type == R_PARISC_DIR14F)
3455 value = hppa_field_adjust (value, addend, e_fsel);
3456 else
3457 value = hppa_field_adjust (value, addend, e_rrsel);
3459 if (r_type == R_PARISC_DIR17R || r_type == R_PARISC_DIR17F)
3460 /* All branches are implicitly shifted by 2 places. */
3461 value >>= 2;
3463 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3464 break;
3467 case R_PARISC_PLTOFF21L:
3468 case R_PARISC_PLTOFF14R:
3469 case R_PARISC_PLTOFF14F:
3470 case R_PARISC_PLTOFF14WR:
3471 case R_PARISC_PLTOFF14DR:
3472 case R_PARISC_PLTOFF16F:
3473 case R_PARISC_PLTOFF16WF:
3474 case R_PARISC_PLTOFF16DF:
3476 /* We want the value of the PLT offset for this symbol, not
3477 the symbol's actual address. Note that __gp may not point
3478 to the start of the DLT, so we have to compute the absolute
3479 address, then subtract out the value of __gp. */
3480 value = (hh->plt_offset
3481 + hppa_info->root.splt->output_offset
3482 + hppa_info->root.splt->output_section->vma);
3483 value -= _bfd_get_gp_value (output_bfd);
3485 /* All PLTOFF relocations are basically the same at this point,
3486 except that we need different field selectors for the 21bit
3487 version vs the 14bit versions. */
3488 if (r_type == R_PARISC_PLTOFF21L)
3489 value = hppa_field_adjust (value, addend, e_lrsel);
3490 else if (r_type == R_PARISC_PLTOFF14F
3491 || r_type == R_PARISC_PLTOFF16F
3492 || r_type == R_PARISC_PLTOFF16WF
3493 || r_type == R_PARISC_PLTOFF16DF)
3494 value = hppa_field_adjust (value, addend, e_fsel);
3495 else
3496 value = hppa_field_adjust (value, addend, e_rrsel);
3498 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3499 break;
3502 case R_PARISC_LTOFF_FPTR32:
3504 /* FIXME: There used to be code here to create the FPTR itself if
3505 the relocation was against a local symbol. But the code could
3506 never have worked. If the assert below is ever triggered then
3507 the code will need to be reinstated and fixed so that it does
3508 what is needed. */
3509 BFD_ASSERT (hh != NULL);
3511 /* We want the value of the DLT offset for this symbol, not
3512 the symbol's actual address. Note that __gp may not point
3513 to the start of the DLT, so we have to compute the absolute
3514 address, then subtract out the value of __gp. */
3515 value = (hh->dlt_offset
3516 + hppa_info->dlt_sec->output_offset
3517 + hppa_info->dlt_sec->output_section->vma);
3518 value -= _bfd_get_gp_value (output_bfd);
3519 bfd_put_32 (input_bfd, value, hit_data);
3520 return bfd_reloc_ok;
3523 case R_PARISC_LTOFF_FPTR64:
3524 case R_PARISC_LTOFF_TP64:
3526 /* We may still need to create the FPTR itself if it was for
3527 a local symbol. */
3528 if (eh == NULL && r_type == R_PARISC_LTOFF_FPTR64)
3530 /* The first two words of an .opd entry are zero. */
3531 memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
3533 /* The next word is the address of the function. */
3534 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3535 (hppa_info->opd_sec->contents
3536 + hh->opd_offset + 16));
3538 /* The last word is our local __gp value. */
3539 value = _bfd_get_gp_value (info->output_bfd);
3540 bfd_put_64 (hppa_info->opd_sec->owner, value,
3541 hppa_info->opd_sec->contents + hh->opd_offset + 24);
3543 /* The DLT value is the address of the .opd entry. */
3544 value = (hh->opd_offset
3545 + hppa_info->opd_sec->output_offset
3546 + hppa_info->opd_sec->output_section->vma);
3548 bfd_put_64 (hppa_info->dlt_sec->owner,
3549 value,
3550 hppa_info->dlt_sec->contents + hh->dlt_offset);
3553 /* We want the value of the DLT offset for this symbol, not
3554 the symbol's actual address. Note that __gp may not point
3555 to the start of the DLT, so we have to compute the absolute
3556 address, then subtract out the value of __gp. */
3557 value = (hh->dlt_offset
3558 + hppa_info->dlt_sec->output_offset
3559 + hppa_info->dlt_sec->output_section->vma);
3560 value -= _bfd_get_gp_value (output_bfd);
3561 bfd_put_64 (input_bfd, value, hit_data);
3562 return bfd_reloc_ok;
3565 case R_PARISC_DIR32:
3566 bfd_put_32 (input_bfd, value + addend, hit_data);
3567 return bfd_reloc_ok;
3569 case R_PARISC_DIR64:
3570 bfd_put_64 (input_bfd, value + addend, hit_data);
3571 return bfd_reloc_ok;
3573 case R_PARISC_GPREL64:
3574 /* Subtract out the global pointer value to make value a DLT
3575 relative address. */
3576 value -= _bfd_get_gp_value (output_bfd);
3578 bfd_put_64 (input_bfd, value + addend, hit_data);
3579 return bfd_reloc_ok;
3581 case R_PARISC_LTOFF64:
3582 /* We want the value of the DLT offset for this symbol, not
3583 the symbol's actual address. Note that __gp may not point
3584 to the start of the DLT, so we have to compute the absolute
3585 address, then subtract out the value of __gp. */
3586 value = (hh->dlt_offset
3587 + hppa_info->dlt_sec->output_offset
3588 + hppa_info->dlt_sec->output_section->vma);
3589 value -= _bfd_get_gp_value (output_bfd);
3591 bfd_put_64 (input_bfd, value + addend, hit_data);
3592 return bfd_reloc_ok;
3594 case R_PARISC_PCREL32:
3596 /* If this is a call to a function defined in another dynamic
3597 library, then redirect the call to the local stub for this
3598 function. */
3599 if (sym_sec == NULL || sym_sec->output_section == NULL)
3600 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3601 + hppa_info->stub_sec->output_section->vma);
3603 /* Turn VALUE into a proper PC relative address. */
3604 value -= (offset + input_section->output_offset
3605 + input_section->output_section->vma);
3607 value += addend;
3608 value -= 8;
3609 bfd_put_32 (input_bfd, value, hit_data);
3610 return bfd_reloc_ok;
3613 case R_PARISC_PCREL64:
3615 /* If this is a call to a function defined in another dynamic
3616 library, then redirect the call to the local stub for this
3617 function. */
3618 if (sym_sec == NULL || sym_sec->output_section == NULL)
3619 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3620 + hppa_info->stub_sec->output_section->vma);
3622 /* Turn VALUE into a proper PC relative address. */
3623 value -= (offset + input_section->output_offset
3624 + input_section->output_section->vma);
3626 value += addend;
3627 value -= 8;
3628 bfd_put_64 (input_bfd, value, hit_data);
3629 return bfd_reloc_ok;
3632 case R_PARISC_FPTR64:
3634 bfd_vma off;
3636 /* We may still need to create the FPTR itself if it was for
3637 a local symbol. */
3638 if (hh == NULL)
3640 bfd_vma *local_opd_offsets;
3642 if (local_offsets == NULL)
3643 abort ();
3645 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3646 off = local_opd_offsets[r_symndx];
3648 /* The last bit records whether we've already initialised
3649 this local .opd entry. */
3650 if ((off & 1) != 0)
3652 BFD_ASSERT (off != (bfd_vma) -1);
3653 off &= ~1;
3655 else
3657 /* The first two words of an .opd entry are zero. */
3658 memset (hppa_info->opd_sec->contents + off, 0, 16);
3660 /* The next word is the address of the function. */
3661 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3662 (hppa_info->opd_sec->contents + off + 16));
3664 /* The last word is our local __gp value. */
3665 value = _bfd_get_gp_value (info->output_bfd);
3666 bfd_put_64 (hppa_info->opd_sec->owner, value,
3667 hppa_info->opd_sec->contents + off + 24);
3670 else
3671 off = hh->opd_offset;
3673 if (hh == NULL || hh->want_opd)
3674 /* We want the value of the OPD offset for this symbol. */
3675 value = (off
3676 + hppa_info->opd_sec->output_offset
3677 + hppa_info->opd_sec->output_section->vma);
3678 else
3679 /* We want the address of the symbol. */
3680 value += addend;
3682 bfd_put_64 (input_bfd, value, hit_data);
3683 return bfd_reloc_ok;
3686 case R_PARISC_SECREL32:
3687 if (sym_sec && sym_sec->output_section)
3688 value -= sym_sec->output_section->vma;
3689 bfd_put_32 (input_bfd, value + addend, hit_data);
3690 return bfd_reloc_ok;
3692 case R_PARISC_SEGREL32:
3693 case R_PARISC_SEGREL64:
3695 /* If this is the first SEGREL relocation, then initialize
3696 the segment base values. */
3697 if (hppa_info->text_segment_base == (bfd_vma) -1)
3698 bfd_map_over_sections (output_bfd, elf_hppa_record_segment_addrs,
3699 hppa_info);
3701 /* VALUE holds the absolute address. We want to include the
3702 addend, then turn it into a segment relative address.
3704 The segment is derived from SYM_SEC. We assume that there are
3705 only two segments of note in the resulting executable/shlib.
3706 A readonly segment (.text) and a readwrite segment (.data). */
3707 value += addend;
3709 if (sym_sec->flags & SEC_CODE)
3710 value -= hppa_info->text_segment_base;
3711 else
3712 value -= hppa_info->data_segment_base;
3714 if (r_type == R_PARISC_SEGREL32)
3715 bfd_put_32 (input_bfd, value, hit_data);
3716 else
3717 bfd_put_64 (input_bfd, value, hit_data);
3718 return bfd_reloc_ok;
3721 /* Something we don't know how to handle. */
3722 default:
3723 return bfd_reloc_notsupported;
3726 /* Update the instruction word. */
3727 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3728 return bfd_reloc_ok;
3731 /* Relocate an HPPA ELF section. */
3733 static int
3734 elf64_hppa_relocate_section (bfd *output_bfd,
3735 struct bfd_link_info *info,
3736 bfd *input_bfd,
3737 asection *input_section,
3738 bfd_byte *contents,
3739 Elf_Internal_Rela *relocs,
3740 Elf_Internal_Sym *local_syms,
3741 asection **local_sections)
3743 Elf_Internal_Shdr *symtab_hdr;
3744 Elf_Internal_Rela *rel;
3745 Elf_Internal_Rela *relend;
3746 struct elf64_hppa_link_hash_table *hppa_info;
3748 hppa_info = hppa_link_hash_table (info);
3749 if (hppa_info == NULL)
3750 return false;
3752 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3754 rel = relocs;
3755 relend = relocs + input_section->reloc_count;
3756 for (; rel < relend; rel++)
3758 int r_type;
3759 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3760 unsigned long r_symndx;
3761 struct elf_link_hash_entry *eh;
3762 Elf_Internal_Sym *sym;
3763 asection *sym_sec;
3764 bfd_vma relocation;
3765 bfd_reloc_status_type r;
3767 r_type = ELF_R_TYPE (rel->r_info);
3768 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
3770 bfd_set_error (bfd_error_bad_value);
3771 return false;
3773 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3774 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3775 continue;
3777 /* This is a final link. */
3778 r_symndx = ELF_R_SYM (rel->r_info);
3779 eh = NULL;
3780 sym = NULL;
3781 sym_sec = NULL;
3782 if (r_symndx < symtab_hdr->sh_info)
3784 /* This is a local symbol, hh defaults to NULL. */
3785 sym = local_syms + r_symndx;
3786 sym_sec = local_sections[r_symndx];
3787 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3789 else
3791 /* This is not a local symbol. */
3792 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3794 /* It seems this can happen with erroneous or unsupported
3795 input (mixing a.out and elf in an archive, for example.) */
3796 if (sym_hashes == NULL)
3797 return false;
3799 eh = sym_hashes[r_symndx - symtab_hdr->sh_info];
3801 if (info->wrap_hash != NULL
3802 && (input_section->flags & SEC_DEBUGGING) != 0)
3803 eh = ((struct elf_link_hash_entry *)
3804 unwrap_hash_lookup (info, input_bfd, &eh->root));
3806 while (eh->root.type == bfd_link_hash_indirect
3807 || eh->root.type == bfd_link_hash_warning)
3808 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3810 relocation = 0;
3811 if (eh->root.type == bfd_link_hash_defined
3812 || eh->root.type == bfd_link_hash_defweak)
3814 sym_sec = eh->root.u.def.section;
3815 if (sym_sec != NULL
3816 && sym_sec->output_section != NULL)
3817 relocation = (eh->root.u.def.value
3818 + sym_sec->output_section->vma
3819 + sym_sec->output_offset);
3821 else if (eh->root.type == bfd_link_hash_undefweak)
3823 else if (info->unresolved_syms_in_objects == RM_IGNORE
3824 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
3826 else if (!bfd_link_relocatable (info)
3827 && elf_hppa_is_dynamic_loader_symbol (eh->root.root.string))
3828 continue;
3829 else if (!bfd_link_relocatable (info))
3831 bool err;
3833 err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
3834 && !info->warn_unresolved_syms)
3835 || ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT;
3837 info->callbacks->undefined_symbol
3838 (info, eh->root.root.string, input_bfd,
3839 input_section, rel->r_offset, err);
3842 if (!bfd_link_relocatable (info)
3843 && relocation == 0
3844 && eh->root.type != bfd_link_hash_defined
3845 && eh->root.type != bfd_link_hash_defweak
3846 && eh->root.type != bfd_link_hash_undefweak)
3848 if (info->unresolved_syms_in_objects == RM_IGNORE
3849 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3850 && eh->type == STT_PARISC_MILLI)
3851 info->callbacks->undefined_symbol
3852 (info, eh_name (eh), input_bfd,
3853 input_section, rel->r_offset, false);
3857 if (sym_sec != NULL && discarded_section (sym_sec))
3858 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3859 rel, 1, relend, howto, 0, contents);
3861 if (bfd_link_relocatable (info))
3862 continue;
3864 r = elf_hppa_final_link_relocate (rel, input_bfd, output_bfd,
3865 input_section, contents,
3866 relocation, info, sym_sec,
3867 eh);
3869 if (r != bfd_reloc_ok)
3871 switch (r)
3873 default:
3874 abort ();
3875 case bfd_reloc_overflow:
3877 const char *sym_name;
3879 if (eh != NULL)
3880 sym_name = NULL;
3881 else
3883 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3884 symtab_hdr->sh_link,
3885 sym->st_name);
3886 if (sym_name == NULL)
3887 return false;
3888 if (*sym_name == '\0')
3889 sym_name = bfd_section_name (sym_sec);
3892 (*info->callbacks->reloc_overflow)
3893 (info, (eh ? &eh->root : NULL), sym_name, howto->name,
3894 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3896 break;
3900 return true;
3903 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
3905 { STRING_COMMA_LEN (".tbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
3906 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3907 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3908 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3909 { STRING_COMMA_LEN (".dlt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3910 { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3911 { STRING_COMMA_LEN (".sbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3912 { NULL, 0, 0, 0, 0 }
3915 /* The hash bucket size is the standard one, namely 4. */
3917 const struct elf_size_info hppa64_elf_size_info =
3919 sizeof (Elf64_External_Ehdr),
3920 sizeof (Elf64_External_Phdr),
3921 sizeof (Elf64_External_Shdr),
3922 sizeof (Elf64_External_Rel),
3923 sizeof (Elf64_External_Rela),
3924 sizeof (Elf64_External_Sym),
3925 sizeof (Elf64_External_Dyn),
3926 sizeof (Elf_External_Note),
3929 64, 3,
3930 ELFCLASS64, EV_CURRENT,
3931 bfd_elf64_write_out_phdrs,
3932 bfd_elf64_write_shdrs_and_ehdr,
3933 bfd_elf64_checksum_contents,
3934 bfd_elf64_write_relocs,
3935 bfd_elf64_swap_symbol_in,
3936 bfd_elf64_swap_symbol_out,
3937 bfd_elf64_slurp_reloc_table,
3938 bfd_elf64_slurp_symbol_table,
3939 bfd_elf64_swap_dyn_in,
3940 bfd_elf64_swap_dyn_out,
3941 bfd_elf64_swap_reloc_in,
3942 bfd_elf64_swap_reloc_out,
3943 bfd_elf64_swap_reloca_in,
3944 bfd_elf64_swap_reloca_out
3947 #define TARGET_BIG_SYM hppa_elf64_vec
3948 #define TARGET_BIG_NAME "elf64-hppa"
3949 #define ELF_ARCH bfd_arch_hppa
3950 #define ELF_TARGET_ID HPPA64_ELF_DATA
3951 #define ELF_MACHINE_CODE EM_PARISC
3952 /* This is not strictly correct. The maximum page size for PA2.0 is
3953 64M. But everything still uses 4k. */
3954 #define ELF_MAXPAGESIZE 0x1000
3955 #define ELF_OSABI ELFOSABI_HPUX
3957 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
3958 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
3959 #define bfd_elf64_bfd_is_local_label_name elf_hppa_is_local_label_name
3960 #define elf_info_to_howto elf_hppa_info_to_howto
3961 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
3963 #define elf_backend_section_from_shdr elf64_hppa_section_from_shdr
3964 #define elf_backend_object_p elf64_hppa_object_p
3965 #define elf_backend_final_write_processing \
3966 elf_hppa_final_write_processing
3967 #define elf_backend_fake_sections elf_hppa_fake_sections
3968 #define elf_backend_add_symbol_hook elf_hppa_add_symbol_hook
3970 #define elf_backend_relocate_section elf_hppa_relocate_section
3972 #define bfd_elf64_bfd_final_link elf_hppa_final_link
3974 #define elf_backend_create_dynamic_sections \
3975 elf64_hppa_create_dynamic_sections
3976 #define elf_backend_init_file_header elf64_hppa_init_file_header
3978 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
3980 #define elf_backend_adjust_dynamic_symbol \
3981 elf64_hppa_adjust_dynamic_symbol
3983 #define elf_backend_late_size_sections elf64_hppa_late_size_sections
3985 #define elf_backend_finish_dynamic_symbol \
3986 elf64_hppa_finish_dynamic_symbol
3987 #define elf_backend_finish_dynamic_sections \
3988 elf64_hppa_finish_dynamic_sections
3989 #define elf_backend_grok_prstatus elf64_hppa_grok_prstatus
3990 #define elf_backend_grok_psinfo elf64_hppa_grok_psinfo
3992 /* Stuff for the BFD linker: */
3993 #define bfd_elf64_bfd_link_hash_table_create \
3994 elf64_hppa_hash_table_create
3996 #define elf_backend_check_relocs \
3997 elf64_hppa_check_relocs
3999 #define elf_backend_size_info \
4000 hppa64_elf_size_info
4002 #define elf_backend_additional_program_headers \
4003 elf64_hppa_additional_program_headers
4005 #define elf_backend_modify_segment_map \
4006 elf64_hppa_modify_segment_map
4008 #define elf_backend_allow_non_load_phdr \
4009 elf64_hppa_allow_non_load_phdr
4011 #define elf_backend_link_output_symbol_hook \
4012 elf64_hppa_link_output_symbol_hook
4014 #define elf_backend_want_got_plt 0
4015 #define elf_backend_plt_readonly 0
4016 #define elf_backend_want_plt_sym 0
4017 #define elf_backend_got_header_size 0
4018 #define elf_backend_type_change_ok true
4019 #define elf_backend_get_symbol_type elf64_hppa_elf_get_symbol_type
4020 #define elf_backend_reloc_type_class elf64_hppa_reloc_type_class
4021 #define elf_backend_rela_normal 1
4022 #define elf_backend_special_sections elf64_hppa_special_sections
4023 #define elf_backend_action_discarded elf_hppa_action_discarded
4024 #define elf_backend_section_from_phdr elf64_hppa_section_from_phdr
4026 #define elf64_bed elf64_hppa_hpux_bed
4028 #include "elf64-target.h"
4030 #undef TARGET_BIG_SYM
4031 #define TARGET_BIG_SYM hppa_elf64_linux_vec
4032 #undef TARGET_BIG_NAME
4033 #define TARGET_BIG_NAME "elf64-hppa-linux"
4034 #undef ELF_OSABI
4035 #define ELF_OSABI ELFOSABI_GNU
4036 #undef elf64_bed
4037 #define elf64_bed elf64_hppa_linux_bed
4038 #undef elf_backend_special_sections
4039 #define elf_backend_special_sections (elf64_hppa_special_sections + 1)
4041 #include "elf64-target.h"