]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-hppa.c
PR31796, Internal error in write_function_pdata at obj-coff-seh
[thirdparty/binutils-gdb.git] / bfd / elf64-hppa.c
CommitLineData
b352eebf 1/* Support for HPPA 64-bit ELF
fd67aa11 2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
15bda425 3
ae9a127f 4 This file is part of BFD, the Binary File Descriptor library.
15bda425 5
ae9a127f
NC
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
cd123cb7 8 the Free Software Foundation; either version 3 of the License, or
ae9a127f 9 (at your option) any later version.
15bda425 10
ae9a127f
NC
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.
15bda425 15
ae9a127f
NC
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
cd123cb7
NC
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
15bda425 20
15bda425 21#include "sysdep.h"
3db64b00 22#include "bfd.h"
15bda425
JL
23#include "libbfd.h"
24#include "elf-bfd.h"
25#include "elf/hppa.h"
26#include "libhppa.h"
27#include "elf64-hppa.h"
e1fa0163 28#include "libiberty.h"
8bc9c892 29
31d5afc1
NC
30/* Target vectors for HPUX and non-HPUX versions of HPPA ELF binaries. */
31extern const bfd_target hppa_elf64_vec;
32extern const bfd_target hppa_elf64_linux_vec;
33
15bda425
JL
34#define ARCH_SIZE 64
35
36#define PLT_ENTRY_SIZE 0x10
37#define DLT_ENTRY_SIZE 0x8
38#define OPD_ENTRY_SIZE 0x20
fe8bc63d 39
15bda425
JL
40#define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
41
42/* The stub is supposed to load the target address and target's DP
43 value out of the PLT, then do an external branch to the target
44 address.
45
46 LDD PLTOFF(%r27),%r1
47 BVE (%r1)
48 LDD PLTOFF+8(%r27),%r27
49
50 Note that we must use the LDD with a 14 bit displacement, not the one
51 with a 5 bit displacement. */
52static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
53 0x53, 0x7b, 0x00, 0x00 };
54
a03bd320 55struct elf64_hppa_link_hash_entry
15bda425 56{
a03bd320 57 struct elf_link_hash_entry eh;
15bda425
JL
58
59 /* Offsets for this symbol in various linker sections. */
60 bfd_vma dlt_offset;
61 bfd_vma plt_offset;
62 bfd_vma opd_offset;
63 bfd_vma stub_offset;
64
15bda425
JL
65 /* The index of the (possibly local) symbol in the input bfd and its
66 associated BFD. Needed so that we can have relocs against local
67 symbols in shared libraries. */
dc810e39 68 long sym_indx;
15bda425
JL
69 bfd *owner;
70
71 /* Dynamic symbols may need to have two different values. One for
72 the dynamic symbol table, one for the normal symbol table.
73
74 In such cases we store the symbol's real value and section
75 index here so we can restore the real value before we write
76 the normal symbol table. */
77 bfd_vma st_value;
78 int st_shndx;
79
80 /* Used to count non-got, non-plt relocations for delayed sizing
81 of relocation sections. */
82 struct elf64_hppa_dyn_reloc_entry
83 {
84 /* Next relocation in the chain. */
85 struct elf64_hppa_dyn_reloc_entry *next;
86
87 /* The type of the relocation. */
88 int type;
89
90 /* The input section of the relocation. */
91 asection *sec;
92
a03bd320
DA
93 /* Number of relocs copied in this section. */
94 bfd_size_type count;
95
15bda425
JL
96 /* The index of the section symbol for the input section of
97 the relocation. Only needed when building shared libraries. */
98 int sec_symndx;
99
100 /* The offset within the input section of the relocation. */
101 bfd_vma offset;
102
103 /* The addend for the relocation. */
104 bfd_vma addend;
105
106 } *reloc_entries;
107
108 /* Nonzero if this symbol needs an entry in one of the linker
109 sections. */
110 unsigned want_dlt;
111 unsigned want_plt;
112 unsigned want_opd;
113 unsigned want_stub;
114};
115
15bda425
JL
116struct elf64_hppa_link_hash_table
117{
118 struct elf_link_hash_table root;
119
120 /* Shortcuts to get to the various linker defined sections. */
121 asection *dlt_sec;
122 asection *dlt_rel_sec;
15bda425
JL
123 asection *opd_sec;
124 asection *opd_rel_sec;
125 asection *other_rel_sec;
126
127 /* Offset of __gp within .plt section. When the PLT gets large we want
128 to slide __gp into the PLT section so that we can continue to use
129 single DP relative instructions to load values out of the PLT. */
130 bfd_vma gp_offset;
131
132 /* Note this is not strictly correct. We should create a stub section for
133 each input section with calls. The stub section should be placed before
134 the section with the call. */
135 asection *stub_sec;
136
137 bfd_vma text_segment_base;
138 bfd_vma data_segment_base;
139
15bda425
JL
140 /* We build tables to map from an input section back to its
141 symbol index. This is the BFD for which we currently have
142 a map. */
143 bfd *section_syms_bfd;
144
145 /* Array of symbol numbers for each input section attached to the
146 current BFD. */
147 int *section_syms;
148};
149
a03bd320 150#define hppa_link_hash_table(p) \
0f55320b
AM
151 ((is_elf_hash_table ((p)->hash) \
152 && elf_hash_table_id (elf_hash_table (p)) == HPPA64_ELF_DATA) \
153 ? (struct elf64_hppa_link_hash_table *) (p)->hash : NULL)
15bda425 154
a03bd320
DA
155#define hppa_elf_hash_entry(ent) \
156 ((struct elf64_hppa_link_hash_entry *)(ent))
157
158#define eh_name(eh) \
159 (eh ? eh->root.root.string : "<undef>")
160
15bda425 161typedef struct bfd_hash_entry *(*new_hash_entry_func)
813c8a3c 162 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
15bda425 163
15bda425 164static struct bfd_link_hash_table *elf64_hppa_hash_table_create
813c8a3c
DA
165 (bfd *abfd);
166
15bda425
JL
167/* This must follow the definitions of the various derived linker
168 hash tables and shared functions. */
169#include "elf-hppa.h"
170
0a1b45a2 171static bool elf64_hppa_object_p
813c8a3c 172 (bfd *);
15bda425 173
0a1b45a2 174static bool elf64_hppa_create_dynamic_sections
813c8a3c 175 (bfd *, struct bfd_link_info *);
15bda425 176
0a1b45a2 177static bool elf64_hppa_adjust_dynamic_symbol
813c8a3c 178 (struct bfd_link_info *, struct elf_link_hash_entry *);
15bda425 179
0a1b45a2 180static bool elf64_hppa_mark_milli_and_exported_functions
813c8a3c 181 (struct elf_link_hash_entry *, void *);
47b7c2db 182
6e0b88f1 183static int elf64_hppa_link_output_symbol_hook
813c8a3c
DA
184 (struct bfd_link_info *, const char *, Elf_Internal_Sym *,
185 asection *, struct elf_link_hash_entry *);
99c79b2e 186
0a1b45a2 187static bool elf64_hppa_finish_dynamic_symbol
813c8a3c
DA
188 (bfd *, struct bfd_link_info *,
189 struct elf_link_hash_entry *, Elf_Internal_Sym *);
fe8bc63d 190
0a1b45a2 191static bool elf64_hppa_finish_dynamic_sections
813c8a3c 192 (bfd *, struct bfd_link_info *);
15bda425 193
0a1b45a2 194static bool elf64_hppa_check_relocs
813c8a3c
DA
195 (bfd *, struct bfd_link_info *,
196 asection *, const Elf_Internal_Rela *);
15bda425 197
0a1b45a2 198static bool elf64_hppa_dynamic_symbol_p
813c8a3c 199 (struct elf_link_hash_entry *, struct bfd_link_info *);
15bda425 200
0a1b45a2 201static bool elf64_hppa_mark_exported_functions
813c8a3c 202 (struct elf_link_hash_entry *, void *);
15bda425 203
0a1b45a2 204static bool elf64_hppa_finalize_opd
a03bd320 205 (struct elf_link_hash_entry *, void *);
15bda425 206
0a1b45a2 207static bool elf64_hppa_finalize_dlt
a03bd320 208 (struct elf_link_hash_entry *, void *);
15bda425 209
0a1b45a2 210static bool allocate_global_data_dlt
a03bd320 211 (struct elf_link_hash_entry *, void *);
15bda425 212
0a1b45a2 213static bool allocate_global_data_plt
a03bd320 214 (struct elf_link_hash_entry *, void *);
15bda425 215
0a1b45a2 216static bool allocate_global_data_stub
a03bd320 217 (struct elf_link_hash_entry *, void *);
15bda425 218
0a1b45a2 219static bool allocate_global_data_opd
a03bd320 220 (struct elf_link_hash_entry *, void *);
15bda425 221
0a1b45a2 222static bool get_reloc_section
813c8a3c 223 (bfd *, struct elf64_hppa_link_hash_table *, asection *);
15bda425 224
0a1b45a2 225static bool count_dyn_reloc
a03bd320 226 (bfd *, struct elf64_hppa_link_hash_entry *,
813c8a3c 227 int, asection *, int, bfd_vma, bfd_vma);
15bda425 228
0a1b45a2 229static bool allocate_dynrel_entries
a03bd320 230 (struct elf_link_hash_entry *, void *);
15bda425 231
0a1b45a2 232static bool elf64_hppa_finalize_dynreloc
a03bd320 233 (struct elf_link_hash_entry *, void *);
15bda425 234
0a1b45a2 235static bool get_opd
813c8a3c 236 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
15bda425 237
0a1b45a2 238static bool get_plt
813c8a3c 239 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
15bda425 240
0a1b45a2 241static bool get_dlt
813c8a3c 242 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
15bda425 243
0a1b45a2 244static bool get_stub
813c8a3c 245 (bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *);
15bda425 246
3fab46d0 247static int elf64_hppa_elf_get_symbol_type
813c8a3c 248 (Elf_Internal_Sym *, int);
3fab46d0 249
a03bd320 250/* Initialize an entry in the link hash table. */
15bda425 251
a03bd320
DA
252static struct bfd_hash_entry *
253hppa64_link_hash_newfunc (struct bfd_hash_entry *entry,
254 struct bfd_hash_table *table,
255 const char *string)
15bda425 256{
15bda425
JL
257 /* Allocate the structure if it has not already been allocated by a
258 subclass. */
a03bd320
DA
259 if (entry == NULL)
260 {
261 entry = bfd_hash_allocate (table,
262 sizeof (struct elf64_hppa_link_hash_entry));
263 if (entry == NULL)
07d6d2b8 264 return entry;
a03bd320 265 }
15bda425 266
15bda425 267 /* Call the allocation method of the superclass. */
a03bd320
DA
268 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
269 if (entry != NULL)
270 {
271 struct elf64_hppa_link_hash_entry *hh;
15bda425 272
a03bd320
DA
273 /* Initialize our local data. All zeros. */
274 hh = hppa_elf_hash_entry (entry);
275 memset (&hh->dlt_offset, 0,
276 (sizeof (struct elf64_hppa_link_hash_entry)
277 - offsetof (struct elf64_hppa_link_hash_entry, dlt_offset)));
278 }
336549c1 279
a03bd320 280 return entry;
15bda425
JL
281}
282
283/* Create the derived linker hash table. The PA64 ELF port uses this
284 derived hash table to keep information specific to the PA ElF
285 linker (without using static variables). */
286
287static struct bfd_link_hash_table*
813c8a3c 288elf64_hppa_hash_table_create (bfd *abfd)
15bda425 289{
a03bd320 290 struct elf64_hppa_link_hash_table *htab;
986f0783 291 size_t amt = sizeof (*htab);
15bda425 292
22cdc249 293 htab = bfd_zmalloc (amt);
a03bd320
DA
294 if (htab == NULL)
295 return NULL;
15bda425 296
a03bd320
DA
297 if (!_bfd_elf_link_hash_table_init (&htab->root, abfd,
298 hppa64_link_hash_newfunc,
4dfe6ac6
NC
299 sizeof (struct elf64_hppa_link_hash_entry),
300 HPPA64_ELF_DATA))
a03bd320 301 {
22cdc249 302 free (htab);
a03bd320
DA
303 return NULL;
304 }
15bda425 305
0a1b45a2 306 htab->root.dt_pltgot_required = true;
a03bd320
DA
307 htab->text_segment_base = (bfd_vma) -1;
308 htab->data_segment_base = (bfd_vma) -1;
15bda425 309
a03bd320 310 return &htab->root.root;
15bda425
JL
311}
312\f
313/* Return nonzero if ABFD represents a PA2.0 ELF64 file.
314
315 Additionally we set the default architecture and machine. */
0a1b45a2 316static bool
813c8a3c 317elf64_hppa_object_p (bfd *abfd)
15bda425 318{
24a5e751
L
319 Elf_Internal_Ehdr * i_ehdrp;
320 unsigned int flags;
d9634ba1 321
24a5e751 322 i_ehdrp = elf_elfheader (abfd);
31d5afc1 323 if (abfd->xvec == & hppa_elf64_linux_vec)
24a5e751 324 {
9c55345c 325 /* GCC on hppa-linux produces binaries with OSABI=GNU,
6c21aa76 326 but the kernel produces corefiles with OSABI=SysV. */
9c55345c 327 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU
d97a8924 328 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
0a1b45a2 329 return false;
24a5e751
L
330 }
331 else
332 {
d97a8924
DA
333 /* HPUX produces binaries with OSABI=HPUX,
334 but the kernel produces corefiles with OSABI=SysV. */
335 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX
336 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
0a1b45a2 337 return false;
24a5e751
L
338 }
339
340 flags = i_ehdrp->e_flags;
d9634ba1
AM
341 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
342 {
343 case EFA_PARISC_1_0:
344 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
345 case EFA_PARISC_1_1:
346 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
347 case EFA_PARISC_2_0:
d97a8924 348 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64)
07d6d2b8 349 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
d97a8924 350 else
07d6d2b8 351 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
d9634ba1
AM
352 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
353 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
354 }
355 /* Don't be fussy. */
0a1b45a2 356 return true;
15bda425
JL
357}
358
359/* Given section type (hdr->sh_type), return a boolean indicating
360 whether or not the section is an elf64-hppa specific section. */
0a1b45a2 361static bool
6dc132d9
L
362elf64_hppa_section_from_shdr (bfd *abfd,
363 Elf_Internal_Shdr *hdr,
364 const char *name,
365 int shindex)
15bda425 366{
15bda425
JL
367 switch (hdr->sh_type)
368 {
369 case SHT_PARISC_EXT:
370 if (strcmp (name, ".PARISC.archext") != 0)
0a1b45a2 371 return false;
15bda425
JL
372 break;
373 case SHT_PARISC_UNWIND:
374 if (strcmp (name, ".PARISC.unwind") != 0)
0a1b45a2 375 return false;
15bda425
JL
376 break;
377 case SHT_PARISC_DOC:
378 case SHT_PARISC_ANNOT:
379 default:
0a1b45a2 380 return false;
15bda425
JL
381 }
382
6dc132d9 383 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
0a1b45a2 384 return false;
15bda425 385
bf577467
AM
386 return ((hdr->sh_flags & SHF_PARISC_SHORT) == 0
387 || bfd_set_section_flags (hdr->bfd_section,
388 hdr->bfd_section->flags | SEC_SMALL_DATA));
15bda425
JL
389}
390
15bda425
JL
391/* SEC is a section containing relocs for an input BFD when linking; return
392 a suitable section for holding relocs in the output BFD for a link. */
393
0a1b45a2 394static bool
813c8a3c
DA
395get_reloc_section (bfd *abfd,
396 struct elf64_hppa_link_hash_table *hppa_info,
397 asection *sec)
15bda425
JL
398{
399 const char *srel_name;
400 asection *srel;
401 bfd *dynobj;
402
403 srel_name = (bfd_elf_string_from_elf_section
404 (abfd, elf_elfheader(abfd)->e_shstrndx,
d4730f92 405 _bfd_elf_single_rel_hdr(sec)->sh_name));
15bda425 406 if (srel_name == NULL)
0a1b45a2 407 return false;
15bda425 408
15bda425
JL
409 dynobj = hppa_info->root.dynobj;
410 if (!dynobj)
411 hppa_info->root.dynobj = dynobj = abfd;
412
3d4d4302 413 srel = bfd_get_linker_section (dynobj, srel_name);
15bda425
JL
414 if (srel == NULL)
415 {
3d4d4302
AM
416 srel = bfd_make_section_anyway_with_flags (dynobj, srel_name,
417 (SEC_ALLOC
418 | SEC_LOAD
419 | SEC_HAS_CONTENTS
420 | SEC_IN_MEMORY
421 | SEC_LINKER_CREATED
422 | SEC_READONLY));
15bda425 423 if (srel == NULL
fd361982 424 || !bfd_set_section_alignment (srel, 3))
0a1b45a2 425 return false;
15bda425
JL
426 }
427
428 hppa_info->other_rel_sec = srel;
0a1b45a2 429 return true;
15bda425
JL
430}
431
fe8bc63d 432/* Add a new entry to the list of dynamic relocations against DYN_H.
15bda425
JL
433
434 We use this to keep a record of all the FPTR relocations against a
435 particular symbol so that we can create FPTR relocations in the
436 output file. */
437
0a1b45a2 438static bool
813c8a3c 439count_dyn_reloc (bfd *abfd,
a03bd320 440 struct elf64_hppa_link_hash_entry *hh,
813c8a3c
DA
441 int type,
442 asection *sec,
07d6d2b8
AM
443 int sec_symndx,
444 bfd_vma offset,
813c8a3c 445 bfd_vma addend)
15bda425
JL
446{
447 struct elf64_hppa_dyn_reloc_entry *rent;
448
449 rent = (struct elf64_hppa_dyn_reloc_entry *)
dc810e39 450 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
15bda425 451 if (!rent)
0a1b45a2 452 return false;
15bda425 453
a03bd320 454 rent->next = hh->reloc_entries;
15bda425
JL
455 rent->type = type;
456 rent->sec = sec;
457 rent->sec_symndx = sec_symndx;
458 rent->offset = offset;
459 rent->addend = addend;
a03bd320 460 hh->reloc_entries = rent;
15bda425 461
0a1b45a2 462 return true;
15bda425
JL
463}
464
a03bd320
DA
465/* Return a pointer to the local DLT, PLT and OPD reference counts
466 for ABFD. Returns NULL if the storage allocation fails. */
467
468static bfd_signed_vma *
469hppa64_elf_local_refcounts (bfd *abfd)
470{
471 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
472 bfd_signed_vma *local_refcounts;
68ffbac6 473
a03bd320
DA
474 local_refcounts = elf_local_got_refcounts (abfd);
475 if (local_refcounts == NULL)
476 {
477 bfd_size_type size;
478
479 /* Allocate space for local DLT, PLT and OPD reference
480 counts. Done this way to save polluting elf_obj_tdata
481 with another target specific pointer. */
482 size = symtab_hdr->sh_info;
483 size *= 3 * sizeof (bfd_signed_vma);
484 local_refcounts = bfd_zalloc (abfd, size);
485 elf_local_got_refcounts (abfd) = local_refcounts;
486 }
487 return local_refcounts;
488}
489
15bda425
JL
490/* Scan the RELOCS and record the type of dynamic entries that each
491 referenced symbol needs. */
492
0a1b45a2 493static bool
813c8a3c
DA
494elf64_hppa_check_relocs (bfd *abfd,
495 struct bfd_link_info *info,
496 asection *sec,
497 const Elf_Internal_Rela *relocs)
15bda425
JL
498{
499 struct elf64_hppa_link_hash_table *hppa_info;
500 const Elf_Internal_Rela *relend;
501 Elf_Internal_Shdr *symtab_hdr;
502 const Elf_Internal_Rela *rel;
4fbb74a6 503 unsigned int sec_symndx;
15bda425 504
0e1862bb 505 if (bfd_link_relocatable (info))
0a1b45a2 506 return true;
15bda425
JL
507
508 /* If this is the first dynamic object found in the link, create
509 the special sections required for dynamic linking. */
510 if (! elf_hash_table (info)->dynamic_sections_created)
511 {
45d6a902 512 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
0a1b45a2 513 return false;
15bda425
JL
514 }
515
a03bd320 516 hppa_info = hppa_link_hash_table (info);
4dfe6ac6 517 if (hppa_info == NULL)
0a1b45a2 518 return false;
15bda425
JL
519 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
520
521 /* If necessary, build a new table holding section symbols indices
6cdc0ccc 522 for this BFD. */
fe8bc63d 523
0e1862bb 524 if (bfd_link_pic (info) && hppa_info->section_syms_bfd != abfd)
15bda425 525 {
832d951b 526 unsigned long i;
9ad5cbcf 527 unsigned int highest_shndx;
6cdc0ccc
AM
528 Elf_Internal_Sym *local_syms = NULL;
529 Elf_Internal_Sym *isym, *isymend;
dc810e39 530 bfd_size_type amt;
15bda425
JL
531
532 /* We're done with the old cache of section index to section symbol
533 index information. Free it.
534
535 ?!? Note we leak the last section_syms array. Presumably we
536 could free it in one of the later routines in this file. */
c9594989 537 free (hppa_info->section_syms);
15bda425 538
6cdc0ccc
AM
539 /* Read this BFD's local symbols. */
540 if (symtab_hdr->sh_info != 0)
47b7c2db 541 {
6cdc0ccc
AM
542 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
543 if (local_syms == NULL)
544 local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
545 symtab_hdr->sh_info, 0,
546 NULL, NULL, NULL);
547 if (local_syms == NULL)
0a1b45a2 548 return false;
9ad5cbcf
AM
549 }
550
6cdc0ccc 551 /* Record the highest section index referenced by the local symbols. */
15bda425 552 highest_shndx = 0;
6cdc0ccc
AM
553 isymend = local_syms + symtab_hdr->sh_info;
554 for (isym = local_syms; isym < isymend; isym++)
15bda425 555 {
4fbb74a6
AM
556 if (isym->st_shndx > highest_shndx
557 && isym->st_shndx < SHN_LORESERVE)
15bda425
JL
558 highest_shndx = isym->st_shndx;
559 }
560
15bda425
JL
561 /* Allocate an array to hold the section index to section symbol index
562 mapping. Bump by one since we start counting at zero. */
563 highest_shndx++;
dc810e39
AM
564 amt = highest_shndx;
565 amt *= sizeof (int);
566 hppa_info->section_syms = (int *) bfd_malloc (amt);
15bda425
JL
567
568 /* Now walk the local symbols again. If we find a section symbol,
569 record the index of the symbol into the section_syms array. */
6cdc0ccc 570 for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
15bda425
JL
571 {
572 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
573 hppa_info->section_syms[isym->st_shndx] = i;
574 }
575
6cdc0ccc
AM
576 /* We are finished with the local symbols. */
577 if (local_syms != NULL
578 && symtab_hdr->contents != (unsigned char *) local_syms)
579 {
580 if (! info->keep_memory)
581 free (local_syms);
582 else
583 {
584 /* Cache the symbols for elf_link_input_bfd. */
585 symtab_hdr->contents = (unsigned char *) local_syms;
586 }
587 }
15bda425
JL
588
589 /* Record which BFD we built the section_syms mapping for. */
590 hppa_info->section_syms_bfd = abfd;
591 }
592
593 /* Record the symbol index for this input section. We may need it for
594 relocations when building shared libraries. When not building shared
595 libraries this value is never really used, but assign it to zero to
596 prevent out of bounds memory accesses in other routines. */
0e1862bb 597 if (bfd_link_pic (info))
15bda425
JL
598 {
599 sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
600
601 /* If we did not find a section symbol for this section, then
602 something went terribly wrong above. */
4fbb74a6 603 if (sec_symndx == SHN_BAD)
0a1b45a2 604 return false;
15bda425 605
4fbb74a6
AM
606 if (sec_symndx < SHN_LORESERVE)
607 sec_symndx = hppa_info->section_syms[sec_symndx];
608 else
609 sec_symndx = 0;
15bda425
JL
610 }
611 else
612 sec_symndx = 0;
fe8bc63d 613
15bda425
JL
614 relend = relocs + sec->reloc_count;
615 for (rel = relocs; rel < relend; ++rel)
616 {
560e09e9
NC
617 enum
618 {
619 NEED_DLT = 1,
620 NEED_PLT = 2,
621 NEED_STUB = 4,
622 NEED_OPD = 8,
623 NEED_DYNREL = 16,
624 };
15bda425 625
15bda425 626 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
a03bd320 627 struct elf64_hppa_link_hash_entry *hh;
15bda425 628 int need_entry;
0a1b45a2 629 bool maybe_dynamic;
15bda425
JL
630 int dynrel_type = R_PARISC_NONE;
631 static reloc_howto_type *howto;
632
633 if (r_symndx >= symtab_hdr->sh_info)
634 {
635 /* We're dealing with a global symbol -- find its hash entry
636 and mark it as being referenced. */
637 long indx = r_symndx - symtab_hdr->sh_info;
a03bd320
DA
638 hh = hppa_elf_hash_entry (elf_sym_hashes (abfd)[indx]);
639 while (hh->eh.root.type == bfd_link_hash_indirect
640 || hh->eh.root.type == bfd_link_hash_warning)
641 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
15bda425 642
81fbe831
AM
643 /* PR15323, ref flags aren't set for references in the same
644 object. */
a03bd320 645 hh->eh.ref_regular = 1;
15bda425 646 }
a03bd320
DA
647 else
648 hh = NULL;
15bda425
JL
649
650 /* We can only get preliminary data on whether a symbol is
651 locally or externally defined, as not all of the input files
652 have yet been processed. Do something with what we know, as
653 this may help reduce memory usage and processing time later. */
0a1b45a2 654 maybe_dynamic = false;
0e1862bb 655 if (hh && ((bfd_link_pic (info)
f5385ebf
AM
656 && (!info->symbolic
657 || info->unresolved_syms_in_shared_libs == RM_IGNORE))
a03bd320
DA
658 || !hh->eh.def_regular
659 || hh->eh.root.type == bfd_link_hash_defweak))
0a1b45a2 660 maybe_dynamic = true;
15bda425
JL
661
662 howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
663 need_entry = 0;
664 switch (howto->type)
665 {
666 /* These are simple indirect references to symbols through the
667 DLT. We need to create a DLT entry for any symbols which
668 appears in a DLTIND relocation. */
669 case R_PARISC_DLTIND21L:
670 case R_PARISC_DLTIND14R:
671 case R_PARISC_DLTIND14F:
672 case R_PARISC_DLTIND14WR:
673 case R_PARISC_DLTIND14DR:
674 need_entry = NEED_DLT;
675 break;
676
677 /* ?!? These need a DLT entry. But I have no idea what to do with
678 the "link time TP value. */
679 case R_PARISC_LTOFF_TP21L:
680 case R_PARISC_LTOFF_TP14R:
681 case R_PARISC_LTOFF_TP14F:
682 case R_PARISC_LTOFF_TP64:
683 case R_PARISC_LTOFF_TP14WR:
684 case R_PARISC_LTOFF_TP14DR:
685 case R_PARISC_LTOFF_TP16F:
686 case R_PARISC_LTOFF_TP16WF:
687 case R_PARISC_LTOFF_TP16DF:
688 need_entry = NEED_DLT;
689 break;
690
691 /* These are function calls. Depending on their precise target we
692 may need to make a stub for them. The stub uses the PLT, so we
693 need to create PLT entries for these symbols too. */
832d951b 694 case R_PARISC_PCREL12F:
15bda425
JL
695 case R_PARISC_PCREL17F:
696 case R_PARISC_PCREL22F:
697 case R_PARISC_PCREL32:
698 case R_PARISC_PCREL64:
699 case R_PARISC_PCREL21L:
700 case R_PARISC_PCREL17R:
701 case R_PARISC_PCREL17C:
702 case R_PARISC_PCREL14R:
703 case R_PARISC_PCREL14F:
704 case R_PARISC_PCREL22C:
705 case R_PARISC_PCREL14WR:
706 case R_PARISC_PCREL14DR:
707 case R_PARISC_PCREL16F:
708 case R_PARISC_PCREL16WF:
709 case R_PARISC_PCREL16DF:
a03bd320
DA
710 /* Function calls might need to go through the .plt, and
711 might need a long branch stub. */
712 if (hh != NULL && hh->eh.type != STT_PARISC_MILLI)
713 need_entry = (NEED_PLT | NEED_STUB);
714 else
715 need_entry = 0;
15bda425
JL
716 break;
717
718 case R_PARISC_PLTOFF21L:
719 case R_PARISC_PLTOFF14R:
720 case R_PARISC_PLTOFF14F:
721 case R_PARISC_PLTOFF14WR:
722 case R_PARISC_PLTOFF14DR:
723 case R_PARISC_PLTOFF16F:
724 case R_PARISC_PLTOFF16WF:
725 case R_PARISC_PLTOFF16DF:
726 need_entry = (NEED_PLT);
727 break;
728
729 case R_PARISC_DIR64:
0e1862bb 730 if (bfd_link_pic (info) || maybe_dynamic)
15bda425
JL
731 need_entry = (NEED_DYNREL);
732 dynrel_type = R_PARISC_DIR64;
733 break;
734
735 /* This is an indirect reference through the DLT to get the address
736 of a OPD descriptor. Thus we need to make a DLT entry that points
737 to an OPD entry. */
738 case R_PARISC_LTOFF_FPTR21L:
739 case R_PARISC_LTOFF_FPTR14R:
740 case R_PARISC_LTOFF_FPTR14WR:
741 case R_PARISC_LTOFF_FPTR14DR:
742 case R_PARISC_LTOFF_FPTR32:
743 case R_PARISC_LTOFF_FPTR64:
744 case R_PARISC_LTOFF_FPTR16F:
745 case R_PARISC_LTOFF_FPTR16WF:
746 case R_PARISC_LTOFF_FPTR16DF:
0e1862bb 747 if (bfd_link_pic (info) || maybe_dynamic)
a03bd320 748 need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
15bda425 749 else
a03bd320 750 need_entry = (NEED_DLT | NEED_OPD | NEED_PLT);
15bda425
JL
751 dynrel_type = R_PARISC_FPTR64;
752 break;
753
754 /* This is a simple OPD entry. */
755 case R_PARISC_FPTR64:
0e1862bb 756 if (bfd_link_pic (info) || maybe_dynamic)
a03bd320 757 need_entry = (NEED_OPD | NEED_PLT | NEED_DYNREL);
15bda425 758 else
a03bd320 759 need_entry = (NEED_OPD | NEED_PLT);
15bda425
JL
760 dynrel_type = R_PARISC_FPTR64;
761 break;
762
763 /* Add more cases as needed. */
764 }
765
766 if (!need_entry)
767 continue;
768
a03bd320
DA
769 if (hh)
770 {
771 /* Stash away enough information to be able to find this symbol
772 regardless of whether or not it is local or global. */
773 hh->owner = abfd;
774 hh->sym_indx = r_symndx;
775 }
15bda425 776
15bda425
JL
777 /* Create what's needed. */
778 if (need_entry & NEED_DLT)
779 {
a03bd320
DA
780 /* Allocate space for a DLT entry, as well as a dynamic
781 relocation for this entry. */
15bda425
JL
782 if (! hppa_info->dlt_sec
783 && ! get_dlt (abfd, info, hppa_info))
784 goto err_out;
a03bd320
DA
785
786 if (hh != NULL)
787 {
788 hh->want_dlt = 1;
789 hh->eh.got.refcount += 1;
790 }
791 else
792 {
793 bfd_signed_vma *local_dlt_refcounts;
68ffbac6 794
a03bd320
DA
795 /* This is a DLT entry for a local symbol. */
796 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
797 if (local_dlt_refcounts == NULL)
0a1b45a2 798 return false;
a03bd320
DA
799 local_dlt_refcounts[r_symndx] += 1;
800 }
15bda425
JL
801 }
802
803 if (need_entry & NEED_PLT)
804 {
9b8a8575 805 if (! hppa_info->root.splt
15bda425
JL
806 && ! get_plt (abfd, info, hppa_info))
807 goto err_out;
a03bd320
DA
808
809 if (hh != NULL)
810 {
811 hh->want_plt = 1;
812 hh->eh.needs_plt = 1;
813 hh->eh.plt.refcount += 1;
814 }
815 else
816 {
817 bfd_signed_vma *local_dlt_refcounts;
818 bfd_signed_vma *local_plt_refcounts;
68ffbac6 819
a03bd320
DA
820 /* This is a PLT entry for a local symbol. */
821 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
822 if (local_dlt_refcounts == NULL)
0a1b45a2 823 return false;
a03bd320
DA
824 local_plt_refcounts = local_dlt_refcounts + symtab_hdr->sh_info;
825 local_plt_refcounts[r_symndx] += 1;
826 }
15bda425
JL
827 }
828
829 if (need_entry & NEED_STUB)
830 {
831 if (! hppa_info->stub_sec
832 && ! get_stub (abfd, info, hppa_info))
833 goto err_out;
a03bd320
DA
834 if (hh)
835 hh->want_stub = 1;
15bda425
JL
836 }
837
838 if (need_entry & NEED_OPD)
839 {
840 if (! hppa_info->opd_sec
841 && ! get_opd (abfd, info, hppa_info))
842 goto err_out;
843
a03bd320
DA
844 /* FPTRs are not allocated by the dynamic linker for PA64,
845 though it is possible that will change in the future. */
fe8bc63d 846
a03bd320
DA
847 if (hh != NULL)
848 hh->want_opd = 1;
849 else
850 {
851 bfd_signed_vma *local_dlt_refcounts;
852 bfd_signed_vma *local_opd_refcounts;
68ffbac6 853
a03bd320
DA
854 /* This is a OPD for a local symbol. */
855 local_dlt_refcounts = hppa64_elf_local_refcounts (abfd);
856 if (local_dlt_refcounts == NULL)
0a1b45a2 857 return false;
a03bd320
DA
858 local_opd_refcounts = (local_dlt_refcounts
859 + 2 * symtab_hdr->sh_info);
860 local_opd_refcounts[r_symndx] += 1;
861 }
15bda425
JL
862 }
863
864 /* Add a new dynamic relocation to the chain of dynamic
865 relocations for this symbol. */
866 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
867 {
868 if (! hppa_info->other_rel_sec
869 && ! get_reloc_section (abfd, hppa_info, sec))
870 goto err_out;
871
a03bd320
DA
872 /* Count dynamic relocations against global symbols. */
873 if (hh != NULL
874 && !count_dyn_reloc (abfd, hh, dynrel_type, sec,
875 sec_symndx, rel->r_offset, rel->r_addend))
15bda425
JL
876 goto err_out;
877
878 /* If we are building a shared library and we just recorded
879 a dynamic R_PARISC_FPTR64 relocation, then make sure the
880 section symbol for this section ends up in the dynamic
881 symbol table. */
0e1862bb 882 if (bfd_link_pic (info) && dynrel_type == R_PARISC_FPTR64
c152c796 883 && ! (bfd_elf_link_record_local_dynamic_symbol
15bda425 884 (info, abfd, sec_symndx)))
0a1b45a2 885 return false;
15bda425
JL
886 }
887 }
888
0a1b45a2 889 return true;
15bda425
JL
890
891 err_out:
0a1b45a2 892 return false;
15bda425
JL
893}
894
895struct elf64_hppa_allocate_data
896{
897 struct bfd_link_info *info;
898 bfd_size_type ofs;
899};
900
901/* Should we do dynamic things to this symbol? */
902
0a1b45a2 903static bool
a03bd320 904elf64_hppa_dynamic_symbol_p (struct elf_link_hash_entry *eh,
813c8a3c 905 struct bfd_link_info *info)
15bda425 906{
986a241f
RH
907 /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
908 and relocations that retrieve a function descriptor? Assume the
909 worst for now. */
a03bd320 910 if (_bfd_elf_dynamic_symbol_p (eh, info, 1))
986a241f
RH
911 {
912 /* ??? Why is this here and not elsewhere is_local_label_name. */
a03bd320 913 if (eh->root.root.string[0] == '$' && eh->root.root.string[1] == '$')
0a1b45a2 914 return false;
15bda425 915
0a1b45a2 916 return true;
986a241f
RH
917 }
918 else
0a1b45a2 919 return false;
15bda425
JL
920}
921
4cc11e76 922/* Mark all functions exported by this file so that we can later allocate
15bda425
JL
923 entries in .opd for them. */
924
0a1b45a2 925static bool
a03bd320 926elf64_hppa_mark_exported_functions (struct elf_link_hash_entry *eh, void *data)
15bda425 927{
a03bd320 928 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
929 struct bfd_link_info *info = (struct bfd_link_info *)data;
930 struct elf64_hppa_link_hash_table *hppa_info;
931
a03bd320 932 hppa_info = hppa_link_hash_table (info);
4dfe6ac6 933 if (hppa_info == NULL)
0a1b45a2 934 return false;
15bda425 935
a03bd320
DA
936 if (eh
937 && (eh->root.type == bfd_link_hash_defined
938 || eh->root.type == bfd_link_hash_defweak)
939 && eh->root.u.def.section->output_section != NULL
940 && eh->type == STT_FUNC)
15bda425 941 {
15bda425
JL
942 if (! hppa_info->opd_sec
943 && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
0a1b45a2 944 return false;
15bda425 945
a03bd320
DA
946 hh->want_opd = 1;
947
832d951b 948 /* Put a flag here for output_symbol_hook. */
a03bd320
DA
949 hh->st_shndx = -1;
950 eh->needs_plt = 1;
15bda425
JL
951 }
952
0a1b45a2 953 return true;
15bda425
JL
954}
955
956/* Allocate space for a DLT entry. */
957
0a1b45a2 958static bool
a03bd320 959allocate_global_data_dlt (struct elf_link_hash_entry *eh, void *data)
15bda425 960{
a03bd320 961 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
962 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
963
a03bd320 964 if (hh->want_dlt)
15bda425 965 {
0e1862bb 966 if (bfd_link_pic (x->info))
15bda425
JL
967 {
968 /* Possibly add the symbol to the local dynamic symbol
969 table since we might need to create a dynamic relocation
970 against it. */
a03bd320 971 if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
15bda425 972 {
a03bd320 973 bfd *owner = eh->root.u.def.section->owner;
15bda425 974
c152c796 975 if (! (bfd_elf_link_record_local_dynamic_symbol
a03bd320 976 (x->info, owner, hh->sym_indx)))
0a1b45a2 977 return false;
15bda425
JL
978 }
979 }
980
a03bd320 981 hh->dlt_offset = x->ofs;
15bda425
JL
982 x->ofs += DLT_ENTRY_SIZE;
983 }
0a1b45a2 984 return true;
15bda425
JL
985}
986
987/* Allocate space for a DLT.PLT entry. */
988
0a1b45a2 989static bool
a03bd320 990allocate_global_data_plt (struct elf_link_hash_entry *eh, void *data)
15bda425 991{
a03bd320 992 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
4dfe6ac6 993 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *) data;
15bda425 994
a03bd320
DA
995 if (hh->want_plt
996 && elf64_hppa_dynamic_symbol_p (eh, x->info)
997 && !((eh->root.type == bfd_link_hash_defined
998 || eh->root.type == bfd_link_hash_defweak)
999 && eh->root.u.def.section->output_section != NULL))
15bda425 1000 {
a03bd320 1001 hh->plt_offset = x->ofs;
15bda425 1002 x->ofs += PLT_ENTRY_SIZE;
a03bd320 1003 if (hh->plt_offset < 0x2000)
4dfe6ac6
NC
1004 {
1005 struct elf64_hppa_link_hash_table *hppa_info;
1006
1007 hppa_info = hppa_link_hash_table (x->info);
1008 if (hppa_info == NULL)
0a1b45a2 1009 return false;
4dfe6ac6
NC
1010
1011 hppa_info->gp_offset = hh->plt_offset;
1012 }
15bda425
JL
1013 }
1014 else
a03bd320 1015 hh->want_plt = 0;
15bda425 1016
0a1b45a2 1017 return true;
15bda425
JL
1018}
1019
1020/* Allocate space for a STUB entry. */
1021
0a1b45a2 1022static bool
a03bd320 1023allocate_global_data_stub (struct elf_link_hash_entry *eh, void *data)
15bda425 1024{
a03bd320 1025 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
1026 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1027
a03bd320
DA
1028 if (hh->want_stub
1029 && elf64_hppa_dynamic_symbol_p (eh, x->info)
1030 && !((eh->root.type == bfd_link_hash_defined
1031 || eh->root.type == bfd_link_hash_defweak)
1032 && eh->root.u.def.section->output_section != NULL))
15bda425 1033 {
a03bd320 1034 hh->stub_offset = x->ofs;
15bda425
JL
1035 x->ofs += sizeof (plt_stub);
1036 }
1037 else
a03bd320 1038 hh->want_stub = 0;
0a1b45a2 1039 return true;
15bda425
JL
1040}
1041
1042/* Allocate space for a FPTR entry. */
1043
0a1b45a2 1044static bool
a03bd320 1045allocate_global_data_opd (struct elf_link_hash_entry *eh, void *data)
15bda425 1046{
a03bd320 1047 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
1048 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1049
a03bd320 1050 if (hh && hh->want_opd)
15bda425 1051 {
15bda425
JL
1052 /* We never need an opd entry for a symbol which is not
1053 defined by this output file. */
a03bd320
DA
1054 if (hh && (hh->eh.root.type == bfd_link_hash_undefined
1055 || hh->eh.root.type == bfd_link_hash_undefweak
1056 || hh->eh.root.u.def.section->output_section == NULL))
1057 hh->want_opd = 0;
15bda425
JL
1058
1059 /* If we are creating a shared library, took the address of a local
1060 function or might export this function from this object file, then
1061 we have to create an opd descriptor. */
0e1862bb 1062 else if (bfd_link_pic (x->info)
a03bd320
DA
1063 || hh == NULL
1064 || (hh->eh.dynindx == -1 && hh->eh.type != STT_PARISC_MILLI)
1065 || (hh->eh.root.type == bfd_link_hash_defined
1066 || hh->eh.root.type == bfd_link_hash_defweak))
15bda425
JL
1067 {
1068 /* If we are creating a shared library, then we will have to
1069 create a runtime relocation for the symbol to properly
1070 initialize the .opd entry. Make sure the symbol gets
1071 added to the dynamic symbol table. */
0e1862bb 1072 if (bfd_link_pic (x->info)
a03bd320 1073 && (hh == NULL || (hh->eh.dynindx == -1)))
15bda425
JL
1074 {
1075 bfd *owner;
adfef0bd 1076 /* PR 6511: Default to using the dynamic symbol table. */
a03bd320 1077 owner = (hh->owner ? hh->owner: eh->root.u.def.section->owner);
15bda425 1078
c152c796 1079 if (!bfd_elf_link_record_local_dynamic_symbol
a03bd320 1080 (x->info, owner, hh->sym_indx))
0a1b45a2 1081 return false;
15bda425
JL
1082 }
1083
1084 /* This may not be necessary or desirable anymore now that
1085 we have some support for dealing with section symbols
1086 in dynamic relocs. But name munging does make the result
1087 much easier to debug. ie, the EPLT reloc will reference
1088 a symbol like .foobar, instead of .text + offset. */
0e1862bb 1089 if (bfd_link_pic (x->info) && eh)
15bda425
JL
1090 {
1091 char *new_name;
1092 struct elf_link_hash_entry *nh;
1093
e1fa0163 1094 new_name = concat (".", eh->root.root.string, NULL);
15bda425
JL
1095
1096 nh = elf_link_hash_lookup (elf_hash_table (x->info),
0a1b45a2 1097 new_name, true, true, true);
15bda425 1098
e1fa0163 1099 free (new_name);
a03bd320
DA
1100 nh->root.type = eh->root.type;
1101 nh->root.u.def.value = eh->root.u.def.value;
1102 nh->root.u.def.section = eh->root.u.def.section;
15bda425 1103
c152c796 1104 if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
0a1b45a2 1105 return false;
15bda425 1106 }
a03bd320 1107 hh->opd_offset = x->ofs;
15bda425
JL
1108 x->ofs += OPD_ENTRY_SIZE;
1109 }
1110
1111 /* Otherwise we do not need an opd entry. */
1112 else
a03bd320 1113 hh->want_opd = 0;
15bda425 1114 }
0a1b45a2 1115 return true;
15bda425
JL
1116}
1117
1118/* HP requires the EI_OSABI field to be filled in. The assignment to
1119 EI_ABIVERSION may not be strictly necessary. */
1120
0a1b45a2 1121static bool
ed7e9d0b 1122elf64_hppa_init_file_header (bfd *abfd, struct bfd_link_info *info)
15bda425 1123{
ed7e9d0b 1124 Elf_Internal_Ehdr *i_ehdrp;
15bda425 1125
ed7e9d0b 1126 if (!_bfd_elf_init_file_header (abfd, info))
0a1b45a2 1127 return false;
68ffbac6 1128
ed7e9d0b 1129 i_ehdrp = elf_elfheader (abfd);
d1036acb
L
1130 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi;
1131 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
0a1b45a2 1132 return true;
15bda425
JL
1133}
1134
1135/* Create function descriptor section (.opd). This section is called .opd
4cc11e76 1136 because it contains "official procedure descriptors". The "official"
15bda425
JL
1137 refers to the fact that these descriptors are used when taking the address
1138 of a procedure, thus ensuring a unique address for each procedure. */
1139
0a1b45a2 1140static bool
813c8a3c
DA
1141get_opd (bfd *abfd,
1142 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1143 struct elf64_hppa_link_hash_table *hppa_info)
15bda425
JL
1144{
1145 asection *opd;
1146 bfd *dynobj;
1147
1148 opd = hppa_info->opd_sec;
1149 if (!opd)
1150 {
1151 dynobj = hppa_info->root.dynobj;
1152 if (!dynobj)
1153 hppa_info->root.dynobj = dynobj = abfd;
1154
3d4d4302
AM
1155 opd = bfd_make_section_anyway_with_flags (dynobj, ".opd",
1156 (SEC_ALLOC
1157 | SEC_LOAD
1158 | SEC_HAS_CONTENTS
1159 | SEC_IN_MEMORY
1160 | SEC_LINKER_CREATED));
15bda425 1161 if (!opd
fd361982 1162 || !bfd_set_section_alignment (opd, 3))
15bda425
JL
1163 {
1164 BFD_ASSERT (0);
0a1b45a2 1165 return false;
15bda425
JL
1166 }
1167
1168 hppa_info->opd_sec = opd;
1169 }
1170
0a1b45a2 1171 return true;
15bda425
JL
1172}
1173
1174/* Create the PLT section. */
1175
0a1b45a2 1176static bool
813c8a3c
DA
1177get_plt (bfd *abfd,
1178 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1179 struct elf64_hppa_link_hash_table *hppa_info)
15bda425
JL
1180{
1181 asection *plt;
1182 bfd *dynobj;
1183
9b8a8575 1184 plt = hppa_info->root.splt;
15bda425
JL
1185 if (!plt)
1186 {
1187 dynobj = hppa_info->root.dynobj;
1188 if (!dynobj)
1189 hppa_info->root.dynobj = dynobj = abfd;
1190
3d4d4302
AM
1191 plt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
1192 (SEC_ALLOC
1193 | SEC_LOAD
1194 | SEC_HAS_CONTENTS
1195 | SEC_IN_MEMORY
1196 | SEC_LINKER_CREATED));
15bda425 1197 if (!plt
fd361982 1198 || !bfd_set_section_alignment (plt, 3))
15bda425
JL
1199 {
1200 BFD_ASSERT (0);
0a1b45a2 1201 return false;
15bda425
JL
1202 }
1203
9b8a8575 1204 hppa_info->root.splt = plt;
15bda425
JL
1205 }
1206
0a1b45a2 1207 return true;
15bda425
JL
1208}
1209
1210/* Create the DLT section. */
1211
0a1b45a2 1212static bool
813c8a3c
DA
1213get_dlt (bfd *abfd,
1214 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1215 struct elf64_hppa_link_hash_table *hppa_info)
15bda425
JL
1216{
1217 asection *dlt;
1218 bfd *dynobj;
1219
1220 dlt = hppa_info->dlt_sec;
1221 if (!dlt)
1222 {
1223 dynobj = hppa_info->root.dynobj;
1224 if (!dynobj)
1225 hppa_info->root.dynobj = dynobj = abfd;
1226
3d4d4302
AM
1227 dlt = bfd_make_section_anyway_with_flags (dynobj, ".dlt",
1228 (SEC_ALLOC
1229 | SEC_LOAD
1230 | SEC_HAS_CONTENTS
1231 | SEC_IN_MEMORY
1232 | SEC_LINKER_CREATED));
15bda425 1233 if (!dlt
fd361982 1234 || !bfd_set_section_alignment (dlt, 3))
15bda425
JL
1235 {
1236 BFD_ASSERT (0);
0a1b45a2 1237 return false;
15bda425
JL
1238 }
1239
1240 hppa_info->dlt_sec = dlt;
1241 }
1242
0a1b45a2 1243 return true;
15bda425
JL
1244}
1245
1246/* Create the stubs section. */
1247
0a1b45a2 1248static bool
813c8a3c
DA
1249get_stub (bfd *abfd,
1250 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1251 struct elf64_hppa_link_hash_table *hppa_info)
15bda425
JL
1252{
1253 asection *stub;
1254 bfd *dynobj;
1255
1256 stub = hppa_info->stub_sec;
1257 if (!stub)
1258 {
1259 dynobj = hppa_info->root.dynobj;
1260 if (!dynobj)
1261 hppa_info->root.dynobj = dynobj = abfd;
1262
3d4d4302
AM
1263 stub = bfd_make_section_anyway_with_flags (dynobj, ".stub",
1264 (SEC_ALLOC | SEC_LOAD
1265 | SEC_HAS_CONTENTS
1266 | SEC_IN_MEMORY
1267 | SEC_READONLY
1268 | SEC_LINKER_CREATED));
15bda425 1269 if (!stub
fd361982 1270 || !bfd_set_section_alignment (stub, 3))
15bda425
JL
1271 {
1272 BFD_ASSERT (0);
0a1b45a2 1273 return false;
15bda425
JL
1274 }
1275
1276 hppa_info->stub_sec = stub;
1277 }
1278
0a1b45a2 1279 return true;
15bda425
JL
1280}
1281
1282/* Create sections necessary for dynamic linking. This is only a rough
1283 cut and will likely change as we learn more about the somewhat
1284 unusual dynamic linking scheme HP uses.
1285
1286 .stub:
1287 Contains code to implement cross-space calls. The first time one
1288 of the stubs is used it will call into the dynamic linker, later
1289 calls will go straight to the target.
1290
1291 The only stub we support right now looks like
1292
1293 ldd OFFSET(%dp),%r1
1294 bve %r0(%r1)
1295 ldd OFFSET+8(%dp),%dp
1296
1297 Other stubs may be needed in the future. We may want the remove
1298 the break/nop instruction. It is only used right now to keep the
1299 offset of a .plt entry and a .stub entry in sync.
1300
1301 .dlt:
1302 This is what most people call the .got. HP used a different name.
1303 Losers.
1304
1305 .rela.dlt:
1306 Relocations for the DLT.
1307
1308 .plt:
1309 Function pointers as address,gp pairs.
1310
1311 .rela.plt:
1312 Should contain dynamic IPLT (and EPLT?) relocations.
1313
1314 .opd:
fe8bc63d 1315 FPTRS
15bda425
JL
1316
1317 .rela.opd:
1318 EPLT relocations for symbols exported from shared libraries. */
1319
0a1b45a2 1320static bool
813c8a3c
DA
1321elf64_hppa_create_dynamic_sections (bfd *abfd,
1322 struct bfd_link_info *info)
15bda425
JL
1323{
1324 asection *s;
4dfe6ac6
NC
1325 struct elf64_hppa_link_hash_table *hppa_info;
1326
1327 hppa_info = hppa_link_hash_table (info);
1328 if (hppa_info == NULL)
0a1b45a2 1329 return false;
15bda425 1330
4dfe6ac6 1331 if (! get_stub (abfd, info, hppa_info))
0a1b45a2 1332 return false;
15bda425 1333
4dfe6ac6 1334 if (! get_dlt (abfd, info, hppa_info))
0a1b45a2 1335 return false;
15bda425 1336
4dfe6ac6 1337 if (! get_plt (abfd, info, hppa_info))
0a1b45a2 1338 return false;
15bda425 1339
4dfe6ac6 1340 if (! get_opd (abfd, info, hppa_info))
0a1b45a2 1341 return false;
15bda425 1342
3d4d4302
AM
1343 s = bfd_make_section_anyway_with_flags (abfd, ".rela.dlt",
1344 (SEC_ALLOC | SEC_LOAD
1345 | SEC_HAS_CONTENTS
1346 | SEC_IN_MEMORY
1347 | SEC_READONLY
1348 | SEC_LINKER_CREATED));
15bda425 1349 if (s == NULL
fd361982 1350 || !bfd_set_section_alignment (s, 3))
0a1b45a2 1351 return false;
4dfe6ac6 1352 hppa_info->dlt_rel_sec = s;
15bda425 1353
3d4d4302
AM
1354 s = bfd_make_section_anyway_with_flags (abfd, ".rela.plt",
1355 (SEC_ALLOC | SEC_LOAD
1356 | SEC_HAS_CONTENTS
1357 | SEC_IN_MEMORY
1358 | SEC_READONLY
1359 | SEC_LINKER_CREATED));
15bda425 1360 if (s == NULL
fd361982 1361 || !bfd_set_section_alignment (s, 3))
0a1b45a2 1362 return false;
9b8a8575 1363 hppa_info->root.srelplt = s;
15bda425 1364
3d4d4302
AM
1365 s = bfd_make_section_anyway_with_flags (abfd, ".rela.data",
1366 (SEC_ALLOC | SEC_LOAD
1367 | SEC_HAS_CONTENTS
1368 | SEC_IN_MEMORY
1369 | SEC_READONLY
1370 | SEC_LINKER_CREATED));
15bda425 1371 if (s == NULL
fd361982 1372 || !bfd_set_section_alignment (s, 3))
0a1b45a2 1373 return false;
4dfe6ac6 1374 hppa_info->other_rel_sec = s;
15bda425 1375
3d4d4302
AM
1376 s = bfd_make_section_anyway_with_flags (abfd, ".rela.opd",
1377 (SEC_ALLOC | SEC_LOAD
1378 | SEC_HAS_CONTENTS
1379 | SEC_IN_MEMORY
1380 | SEC_READONLY
1381 | SEC_LINKER_CREATED));
15bda425 1382 if (s == NULL
fd361982 1383 || !bfd_set_section_alignment (s, 3))
0a1b45a2 1384 return false;
4dfe6ac6 1385 hppa_info->opd_rel_sec = s;
15bda425 1386
0a1b45a2 1387 return true;
15bda425
JL
1388}
1389
1390/* Allocate dynamic relocations for those symbols that turned out
1391 to be dynamic. */
1392
0a1b45a2 1393static bool
a03bd320 1394allocate_dynrel_entries (struct elf_link_hash_entry *eh, void *data)
15bda425 1395{
a03bd320 1396 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
1397 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1398 struct elf64_hppa_link_hash_table *hppa_info;
1399 struct elf64_hppa_dyn_reloc_entry *rent;
0a1b45a2 1400 bool dynamic_symbol, shared;
15bda425 1401
a03bd320 1402 hppa_info = hppa_link_hash_table (x->info);
4dfe6ac6 1403 if (hppa_info == NULL)
0a1b45a2 1404 return false;
4dfe6ac6 1405
a03bd320 1406 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, x->info);
0e1862bb 1407 shared = bfd_link_pic (x->info);
15bda425
JL
1408
1409 /* We may need to allocate relocations for a non-dynamic symbol
1410 when creating a shared library. */
1411 if (!dynamic_symbol && !shared)
0a1b45a2 1412 return true;
15bda425
JL
1413
1414 /* Take care of the normal data relocations. */
1415
a03bd320 1416 for (rent = hh->reloc_entries; rent; rent = rent->next)
15bda425 1417 {
d663e1cd
JL
1418 /* Allocate one iff we are building a shared library, the relocation
1419 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
a03bd320 1420 if (!shared && rent->type == R_PARISC_FPTR64 && hh->want_opd)
d663e1cd
JL
1421 continue;
1422
eea6121a 1423 hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
15bda425
JL
1424
1425 /* Make sure this symbol gets into the dynamic symbol table if it is
1426 not already recorded. ?!? This should not be in the loop since
1427 the symbol need only be added once. */
a03bd320 1428 if (eh->dynindx == -1 && eh->type != STT_PARISC_MILLI)
c152c796 1429 if (!bfd_elf_link_record_local_dynamic_symbol
a03bd320 1430 (x->info, rent->sec->owner, hh->sym_indx))
0a1b45a2 1431 return false;
15bda425
JL
1432 }
1433
1434 /* Take care of the GOT and PLT relocations. */
1435
a03bd320 1436 if ((dynamic_symbol || shared) && hh->want_dlt)
eea6121a 1437 hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
15bda425
JL
1438
1439 /* If we are building a shared library, then every symbol that has an
1440 opd entry will need an EPLT relocation to relocate the symbol's address
1441 and __gp value based on the runtime load address. */
a03bd320 1442 if (shared && hh->want_opd)
eea6121a 1443 hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
15bda425 1444
a03bd320 1445 if (hh->want_plt && dynamic_symbol)
15bda425
JL
1446 {
1447 bfd_size_type t = 0;
1448
1449 /* Dynamic symbols get one IPLT relocation. Local symbols in
1450 shared libraries get two REL relocations. Local symbols in
1451 main applications get nothing. */
1452 if (dynamic_symbol)
1453 t = sizeof (Elf64_External_Rela);
1454 else if (shared)
1455 t = 2 * sizeof (Elf64_External_Rela);
1456
9b8a8575 1457 hppa_info->root.srelplt->size += t;
15bda425
JL
1458 }
1459
0a1b45a2 1460 return true;
15bda425
JL
1461}
1462
1463/* Adjust a symbol defined by a dynamic object and referenced by a
1464 regular object. */
1465
0a1b45a2 1466static bool
813c8a3c 1467elf64_hppa_adjust_dynamic_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
a03bd320 1468 struct elf_link_hash_entry *eh)
15bda425
JL
1469{
1470 /* ??? Undefined symbols with PLT entries should be re-defined
1471 to be the PLT entry. */
1472
1473 /* If this is a weak symbol, and there is a real definition, the
1474 processor independent code will have arranged for us to see the
1475 real definition first, and we can just use the same value. */
60d67dc8 1476 if (eh->is_weakalias)
15bda425 1477 {
60d67dc8
AM
1478 struct elf_link_hash_entry *def = weakdef (eh);
1479 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
1480 eh->root.u.def.section = def->root.u.def.section;
1481 eh->root.u.def.value = def->root.u.def.value;
0a1b45a2 1482 return true;
15bda425
JL
1483 }
1484
1485 /* If this is a reference to a symbol defined by a dynamic object which
1486 is not a function, we might allocate the symbol in our .dynbss section
1487 and allocate a COPY dynamic relocation.
1488
1489 But PA64 code is canonically PIC, so as a rule we can avoid this sort
1490 of hackery. */
1491
0a1b45a2 1492 return true;
15bda425
JL
1493}
1494
47b7c2db
AM
1495/* This function is called via elf_link_hash_traverse to mark millicode
1496 symbols with a dynindx of -1 and to remove the string table reference
1497 from the dynamic symbol table. If the symbol is not a millicode symbol,
1498 elf64_hppa_mark_exported_functions is called. */
1499
0a1b45a2 1500static bool
a03bd320 1501elf64_hppa_mark_milli_and_exported_functions (struct elf_link_hash_entry *eh,
813c8a3c 1502 void *data)
47b7c2db 1503{
7686d77d 1504 struct bfd_link_info *info = (struct bfd_link_info *) data;
47b7c2db 1505
7686d77d 1506 if (eh->type == STT_PARISC_MILLI)
47b7c2db 1507 {
7686d77d 1508 if (eh->dynindx != -1)
47b7c2db 1509 {
7686d77d 1510 eh->dynindx = -1;
47b7c2db 1511 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7686d77d 1512 eh->dynstr_index);
47b7c2db 1513 }
0a1b45a2 1514 return true;
47b7c2db
AM
1515 }
1516
a03bd320 1517 return elf64_hppa_mark_exported_functions (eh, data);
47b7c2db
AM
1518}
1519
15bda425
JL
1520/* Set the final sizes of the dynamic sections and allocate memory for
1521 the contents of our special sections. */
1522
0a1b45a2 1523static bool
af969b14 1524elf64_hppa_late_size_sections (bfd *output_bfd, struct bfd_link_info *info)
15bda425 1525{
a03bd320
DA
1526 struct elf64_hppa_link_hash_table *hppa_info;
1527 struct elf64_hppa_allocate_data data;
15bda425 1528 bfd *dynobj;
a03bd320
DA
1529 bfd *ibfd;
1530 asection *sec;
0a1b45a2 1531 bool relocs;
15bda425 1532
a03bd320 1533 hppa_info = hppa_link_hash_table (info);
4dfe6ac6 1534 if (hppa_info == NULL)
0a1b45a2 1535 return false;
15bda425 1536
ce558b89 1537 dynobj = hppa_info->root.dynobj;
af969b14
AM
1538 if (dynobj == NULL)
1539 return true;
15bda425 1540
47b7c2db
AM
1541 /* Mark each function this program exports so that we will allocate
1542 space in the .opd section for each function's FPTR. If we are
1543 creating dynamic sections, change the dynamic index of millicode
1544 symbols to -1 and remove them from the string table for .dynstr.
1545
1546 We have to traverse the main linker hash table since we have to
1547 find functions which may not have been mentioned in any relocs. */
ce558b89
AM
1548 elf_link_hash_traverse (&hppa_info->root,
1549 (hppa_info->root.dynamic_sections_created
47b7c2db
AM
1550 ? elf64_hppa_mark_milli_and_exported_functions
1551 : elf64_hppa_mark_exported_functions),
1552 info);
1553
ce558b89 1554 if (hppa_info->root.dynamic_sections_created)
15bda425
JL
1555 {
1556 /* Set the contents of the .interp section to the interpreter. */
9b8b325a 1557 if (bfd_link_executable (info) && !info->nointerp)
15bda425 1558 {
3d4d4302 1559 sec = bfd_get_linker_section (dynobj, ".interp");
a03bd320
DA
1560 BFD_ASSERT (sec != NULL);
1561 sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
1562 sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
15bda425
JL
1563 }
1564 }
1565 else
1566 {
1567 /* We may have created entries in the .rela.got section.
1568 However, if we are not creating the dynamic sections, we will
1569 not actually use these entries. Reset the size of .rela.dlt,
1570 which will cause it to get stripped from the output file
1571 below. */
ce558b89 1572 sec = hppa_info->dlt_rel_sec;
a03bd320
DA
1573 if (sec != NULL)
1574 sec->size = 0;
1575 }
1576
1577 /* Set up DLT, PLT and OPD offsets for local syms, and space for local
1578 dynamic relocs. */
c72f2fb2 1579 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
a03bd320
DA
1580 {
1581 bfd_signed_vma *local_dlt;
1582 bfd_signed_vma *end_local_dlt;
1583 bfd_signed_vma *local_plt;
1584 bfd_signed_vma *end_local_plt;
1585 bfd_signed_vma *local_opd;
1586 bfd_signed_vma *end_local_opd;
1587 bfd_size_type locsymcount;
1588 Elf_Internal_Shdr *symtab_hdr;
1589 asection *srel;
1590
1591 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1592 continue;
1593
1594 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
1595 {
1596 struct elf64_hppa_dyn_reloc_entry *hdh_p;
1597
1598 for (hdh_p = ((struct elf64_hppa_dyn_reloc_entry *)
1599 elf_section_data (sec)->local_dynrel);
1600 hdh_p != NULL;
1601 hdh_p = hdh_p->next)
1602 {
1603 if (!bfd_is_abs_section (hdh_p->sec)
1604 && bfd_is_abs_section (hdh_p->sec->output_section))
1605 {
1606 /* Input section has been discarded, either because
1607 it is a copy of a linkonce section or due to
1608 linker script /DISCARD/, so we'll be discarding
1609 the relocs too. */
1610 }
1611 else if (hdh_p->count != 0)
1612 {
1613 srel = elf_section_data (hdh_p->sec)->sreloc;
1614 srel->size += hdh_p->count * sizeof (Elf64_External_Rela);
1615 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
1616 info->flags |= DF_TEXTREL;
1617 }
1618 }
1619 }
1620
1621 local_dlt = elf_local_got_refcounts (ibfd);
1622 if (!local_dlt)
1623 continue;
1624
1625 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1626 locsymcount = symtab_hdr->sh_info;
1627 end_local_dlt = local_dlt + locsymcount;
1628 sec = hppa_info->dlt_sec;
1629 srel = hppa_info->dlt_rel_sec;
1630 for (; local_dlt < end_local_dlt; ++local_dlt)
1631 {
1632 if (*local_dlt > 0)
1633 {
1634 *local_dlt = sec->size;
1635 sec->size += DLT_ENTRY_SIZE;
0e1862bb 1636 if (bfd_link_pic (info))
07d6d2b8 1637 {
a03bd320 1638 srel->size += sizeof (Elf64_External_Rela);
07d6d2b8 1639 }
a03bd320
DA
1640 }
1641 else
1642 *local_dlt = (bfd_vma) -1;
1643 }
1644
1645 local_plt = end_local_dlt;
1646 end_local_plt = local_plt + locsymcount;
1647 if (! hppa_info->root.dynamic_sections_created)
1648 {
1649 /* Won't be used, but be safe. */
1650 for (; local_plt < end_local_plt; ++local_plt)
1651 *local_plt = (bfd_vma) -1;
1652 }
1653 else
1654 {
9b8a8575
L
1655 sec = hppa_info->root.splt;
1656 srel = hppa_info->root.srelplt;
a03bd320
DA
1657 for (; local_plt < end_local_plt; ++local_plt)
1658 {
1659 if (*local_plt > 0)
1660 {
1661 *local_plt = sec->size;
1662 sec->size += PLT_ENTRY_SIZE;
0e1862bb 1663 if (bfd_link_pic (info))
a03bd320
DA
1664 srel->size += sizeof (Elf64_External_Rela);
1665 }
1666 else
1667 *local_plt = (bfd_vma) -1;
1668 }
1669 }
1670
1671 local_opd = end_local_plt;
1672 end_local_opd = local_opd + locsymcount;
1673 if (! hppa_info->root.dynamic_sections_created)
1674 {
1675 /* Won't be used, but be safe. */
1676 for (; local_opd < end_local_opd; ++local_opd)
1677 *local_opd = (bfd_vma) -1;
1678 }
1679 else
1680 {
1681 sec = hppa_info->opd_sec;
1682 srel = hppa_info->opd_rel_sec;
1683 for (; local_opd < end_local_opd; ++local_opd)
1684 {
1685 if (*local_opd > 0)
1686 {
1687 *local_opd = sec->size;
1688 sec->size += OPD_ENTRY_SIZE;
0e1862bb 1689 if (bfd_link_pic (info))
a03bd320
DA
1690 srel->size += sizeof (Elf64_External_Rela);
1691 }
1692 else
1693 *local_opd = (bfd_vma) -1;
1694 }
1695 }
15bda425
JL
1696 }
1697
1698 /* Allocate the GOT entries. */
1699
1700 data.info = info;
a03bd320 1701 if (hppa_info->dlt_sec)
15bda425 1702 {
a03bd320 1703 data.ofs = hppa_info->dlt_sec->size;
ce558b89 1704 elf_link_hash_traverse (&hppa_info->root,
a03bd320 1705 allocate_global_data_dlt, &data);
eea6121a 1706 hppa_info->dlt_sec->size = data.ofs;
a03bd320 1707 }
15bda425 1708
9b8a8575 1709 if (hppa_info->root.splt)
a03bd320 1710 {
9b8a8575 1711 data.ofs = hppa_info->root.splt->size;
ce558b89 1712 elf_link_hash_traverse (&hppa_info->root,
07d6d2b8 1713 allocate_global_data_plt, &data);
9b8a8575 1714 hppa_info->root.splt->size = data.ofs;
a03bd320 1715 }
15bda425 1716
a03bd320
DA
1717 if (hppa_info->stub_sec)
1718 {
15bda425 1719 data.ofs = 0x0;
ce558b89 1720 elf_link_hash_traverse (&hppa_info->root,
a03bd320 1721 allocate_global_data_stub, &data);
eea6121a 1722 hppa_info->stub_sec->size = data.ofs;
15bda425
JL
1723 }
1724
15bda425 1725 /* Allocate space for entries in the .opd section. */
a03bd320 1726 if (hppa_info->opd_sec)
15bda425 1727 {
a03bd320 1728 data.ofs = hppa_info->opd_sec->size;
ce558b89 1729 elf_link_hash_traverse (&hppa_info->root,
a03bd320 1730 allocate_global_data_opd, &data);
eea6121a 1731 hppa_info->opd_sec->size = data.ofs;
15bda425
JL
1732 }
1733
1734 /* Now allocate space for dynamic relocations, if necessary. */
1735 if (hppa_info->root.dynamic_sections_created)
ce558b89 1736 elf_link_hash_traverse (&hppa_info->root,
a03bd320 1737 allocate_dynrel_entries, &data);
15bda425
JL
1738
1739 /* The sizes of all the sections are set. Allocate memory for them. */
0a1b45a2 1740 relocs = false;
a03bd320 1741 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
15bda425
JL
1742 {
1743 const char *name;
15bda425 1744
a03bd320 1745 if ((sec->flags & SEC_LINKER_CREATED) == 0)
15bda425
JL
1746 continue;
1747
1748 /* It's OK to base decisions on the section name, because none
1749 of the dynobj section names depend upon the input files. */
fd361982 1750 name = bfd_section_name (sec);
15bda425 1751
15bda425
JL
1752 if (strcmp (name, ".plt") == 0)
1753 {
c456f082 1754 /* Remember whether there is a PLT. */
3084d7a2 1755 ;
15bda425 1756 }
c456f082 1757 else if (strcmp (name, ".opd") == 0
08dedd66 1758 || startswith (name, ".dlt")
c456f082
AM
1759 || strcmp (name, ".stub") == 0
1760 || strcmp (name, ".got") == 0)
15bda425 1761 {
d663e1cd 1762 /* Strip this section if we don't need it; see the comment below. */
15bda425 1763 }
08dedd66 1764 else if (startswith (name, ".rela"))
15bda425 1765 {
a03bd320 1766 if (sec->size != 0)
15bda425 1767 {
15bda425
JL
1768 /* Remember whether there are any reloc sections other
1769 than .rela.plt. */
1770 if (strcmp (name, ".rela.plt") != 0)
0a1b45a2 1771 relocs = true;
15bda425
JL
1772
1773 /* We use the reloc_count field as a counter if we need
1774 to copy relocs into the output file. */
a03bd320 1775 sec->reloc_count = 0;
15bda425
JL
1776 }
1777 }
c456f082 1778 else
15bda425
JL
1779 {
1780 /* It's not one of our sections, so don't allocate space. */
1781 continue;
1782 }
1783
a03bd320 1784 if (sec->size == 0)
15bda425 1785 {
c456f082
AM
1786 /* If we don't need this section, strip it from the
1787 output file. This is mostly to handle .rela.bss and
1788 .rela.plt. We must create both sections in
1789 create_dynamic_sections, because they must be created
1790 before the linker maps input sections to output
1791 sections. The linker does that before
1792 adjust_dynamic_symbol is called, and it is that
1793 function which decides whether anything needs to go
1794 into these sections. */
a03bd320 1795 sec->flags |= SEC_EXCLUDE;
15bda425
JL
1796 continue;
1797 }
1798
a03bd320 1799 if ((sec->flags & SEC_HAS_CONTENTS) == 0)
c456f082
AM
1800 continue;
1801
15bda425 1802 /* Allocate memory for the section contents if it has not
832d951b
AM
1803 been allocated already. We use bfd_zalloc here in case
1804 unused entries are not reclaimed before the section's
1805 contents are written out. This should not happen, but this
1806 way if it does, we get a R_PARISC_NONE reloc instead of
1807 garbage. */
a03bd320 1808 if (sec->contents == NULL)
15bda425 1809 {
a03bd320
DA
1810 sec->contents = (bfd_byte *) bfd_zalloc (dynobj, sec->size);
1811 if (sec->contents == NULL)
0a1b45a2 1812 return false;
15bda425
JL
1813 }
1814 }
1815
ce558b89 1816 if (hppa_info->root.dynamic_sections_created)
15bda425
JL
1817 {
1818 /* Always create a DT_PLTGOT. It actually has nothing to do with
1819 the PLT, it is how we communicate the __gp value of a load
1820 module to the dynamic linker. */
dc810e39 1821#define add_dynamic_entry(TAG, VAL) \
5a580b3a 1822 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 1823
3084d7a2 1824 if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0))
0a1b45a2 1825 return false;
15bda425
JL
1826
1827 /* Add some entries to the .dynamic section. We fill in the
1828 values later, in elf64_hppa_finish_dynamic_sections, but we
1829 must add the entries now so that we get the correct size for
1830 the .dynamic section. The DT_DEBUG entry is filled in by the
1831 dynamic linker and used by the debugger. */
0e1862bb 1832 if (! bfd_link_pic (info))
15bda425 1833 {
3084d7a2 1834 if (!add_dynamic_entry (DT_HP_DLD_HOOK, 0)
dc810e39 1835 || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
0a1b45a2 1836 return false;
15bda425
JL
1837 }
1838
f2482cb2 1839 /* Force DT_FLAGS to always be set.
31d5afc1
NC
1840 Required by HPUX 11.00 patch PHSS_26559.
1841 PR 30743: But do not set them for non-HPUX targets. */
1842 if (output_bfd->xvec == & hppa_elf64_vec)
1843 {
1844 if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
1845 return false;
1846 }
15bda425 1847 }
dc810e39 1848#undef add_dynamic_entry
15bda425 1849
3084d7a2 1850 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
15bda425
JL
1851}
1852
1853/* Called after we have output the symbol into the dynamic symbol
1854 table, but before we output the symbol into the normal symbol
1855 table.
1856
1857 For some symbols we had to change their address when outputting
1858 the dynamic symbol table. We undo that change here so that
1859 the symbols have their expected value in the normal symbol
1860 table. Ick. */
1861
6e0b88f1 1862static int
a03bd320 1863elf64_hppa_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
813c8a3c
DA
1864 const char *name,
1865 Elf_Internal_Sym *sym,
1866 asection *input_sec ATTRIBUTE_UNUSED,
a03bd320 1867 struct elf_link_hash_entry *eh)
15bda425 1868{
a03bd320 1869 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
1870
1871 /* We may be called with the file symbol or section symbols.
1872 They never need munging, so it is safe to ignore them. */
a03bd320 1873 if (!name || !eh)
6e0b88f1 1874 return 1;
15bda425 1875
832d951b
AM
1876 /* Function symbols for which we created .opd entries *may* have been
1877 munged by finish_dynamic_symbol and have to be un-munged here.
1878
1879 Note that finish_dynamic_symbol sometimes turns dynamic symbols
1880 into non-dynamic ones, so we initialize st_shndx to -1 in
1881 mark_exported_functions and check to see if it was overwritten
a03bd320
DA
1882 here instead of just checking eh->dynindx. */
1883 if (hh->want_opd && hh->st_shndx != -1)
15bda425
JL
1884 {
1885 /* Restore the saved value and section index. */
a03bd320
DA
1886 sym->st_value = hh->st_value;
1887 sym->st_shndx = hh->st_shndx;
15bda425
JL
1888 }
1889
6e0b88f1 1890 return 1;
15bda425
JL
1891}
1892
1893/* Finish up dynamic symbol handling. We set the contents of various
1894 dynamic sections here. */
1895
0a1b45a2 1896static bool
813c8a3c
DA
1897elf64_hppa_finish_dynamic_symbol (bfd *output_bfd,
1898 struct bfd_link_info *info,
a03bd320 1899 struct elf_link_hash_entry *eh,
813c8a3c 1900 Elf_Internal_Sym *sym)
15bda425 1901{
a03bd320 1902 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
c7e2358a 1903 asection *stub, *splt, *sopd, *spltrel;
15bda425 1904 struct elf64_hppa_link_hash_table *hppa_info;
15bda425 1905
a03bd320 1906 hppa_info = hppa_link_hash_table (info);
15bda425
JL
1907
1908 stub = hppa_info->stub_sec;
9b8a8575 1909 splt = hppa_info->root.splt;
15bda425 1910 sopd = hppa_info->opd_sec;
9b8a8575 1911 spltrel = hppa_info->root.srelplt;
15bda425 1912
15bda425
JL
1913 /* Incredible. It is actually necessary to NOT use the symbol's real
1914 value when building the dynamic symbol table for a shared library.
1915 At least for symbols that refer to functions.
1916
1917 We will store a new value and section index into the symbol long
1918 enough to output it into the dynamic symbol table, then we restore
1919 the original values (in elf64_hppa_link_output_symbol_hook). */
a03bd320 1920 if (hh->want_opd)
15bda425 1921 {
f12123c0 1922 BFD_ASSERT (sopd != NULL);
d663e1cd 1923
15bda425
JL
1924 /* Save away the original value and section index so that we
1925 can restore them later. */
a03bd320
DA
1926 hh->st_value = sym->st_value;
1927 hh->st_shndx = sym->st_shndx;
15bda425
JL
1928
1929 /* For the dynamic symbol table entry, we want the value to be
1930 address of this symbol's entry within the .opd section. */
a03bd320 1931 sym->st_value = (hh->opd_offset
15bda425
JL
1932 + sopd->output_offset
1933 + sopd->output_section->vma);
1934 sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1935 sopd->output_section);
1936 }
1937
1938 /* Initialize a .plt entry if requested. */
a03bd320
DA
1939 if (hh->want_plt
1940 && elf64_hppa_dynamic_symbol_p (eh, info))
15bda425
JL
1941 {
1942 bfd_vma value;
1943 Elf_Internal_Rela rel;
947216bf 1944 bfd_byte *loc;
15bda425 1945
f12123c0 1946 BFD_ASSERT (splt != NULL && spltrel != NULL);
d663e1cd 1947
15bda425
JL
1948 /* We do not actually care about the value in the PLT entry
1949 if we are creating a shared library and the symbol is
1950 still undefined, we create a dynamic relocation to fill
1951 in the correct value. */
0e1862bb 1952 if (bfd_link_pic (info) && eh->root.type == bfd_link_hash_undefined)
15bda425
JL
1953 value = 0;
1954 else
a03bd320 1955 value = (eh->root.u.def.value + eh->root.u.def.section->vma);
15bda425 1956
fe8bc63d 1957 /* Fill in the entry in the procedure linkage table.
15bda425
JL
1958
1959 The format of a plt entry is
fe8bc63d 1960 <funcaddr> <__gp>.
15bda425
JL
1961
1962 plt_offset is the offset within the PLT section at which to
fe8bc63d 1963 install the PLT entry.
15bda425
JL
1964
1965 We are modifying the in-memory PLT contents here, so we do not add
1966 in the output_offset of the PLT section. */
1967
a03bd320 1968 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset);
586338b8 1969 value = _bfd_get_gp_value (info->output_bfd);
a03bd320 1970 bfd_put_64 (splt->owner, value, splt->contents + hh->plt_offset + 0x8);
15bda425
JL
1971
1972 /* Create a dynamic IPLT relocation for this entry.
1973
1974 We are creating a relocation in the output file's PLT section,
1975 which is included within the DLT secton. So we do need to include
1976 the PLT's output_offset in the computation of the relocation's
1977 address. */
a03bd320 1978 rel.r_offset = (hh->plt_offset + splt->output_offset
15bda425 1979 + splt->output_section->vma);
a03bd320 1980 rel.r_info = ELF64_R_INFO (hh->eh.dynindx, R_PARISC_IPLT);
15bda425
JL
1981 rel.r_addend = 0;
1982
947216bf
AM
1983 loc = spltrel->contents;
1984 loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
586338b8 1985 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
15bda425
JL
1986 }
1987
1988 /* Initialize an external call stub entry if requested. */
a03bd320
DA
1989 if (hh->want_stub
1990 && elf64_hppa_dynamic_symbol_p (eh, info))
15bda425
JL
1991 {
1992 bfd_vma value;
1993 int insn;
b352eebf 1994 unsigned int max_offset;
15bda425 1995
f12123c0 1996 BFD_ASSERT (stub != NULL);
d663e1cd 1997
15bda425
JL
1998 /* Install the generic stub template.
1999
2000 We are modifying the contents of the stub section, so we do not
2001 need to include the stub section's output_offset here. */
a03bd320 2002 memcpy (stub->contents + hh->stub_offset, plt_stub, sizeof (plt_stub));
15bda425
JL
2003
2004 /* Fix up the first ldd instruction.
2005
2006 We are modifying the contents of the STUB section in memory,
fe8bc63d 2007 so we do not need to include its output offset in this computation.
15bda425
JL
2008
2009 Note the plt_offset value is the value of the PLT entry relative to
2010 the start of the PLT section. These instructions will reference
2011 data relative to the value of __gp, which may not necessarily have
2012 the same address as the start of the PLT section.
2013
2014 gp_offset contains the offset of __gp within the PLT section. */
a03bd320 2015 value = hh->plt_offset - hppa_info->gp_offset;
fe8bc63d 2016
a03bd320 2017 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset);
b352eebf
AM
2018 if (output_bfd->arch_info->mach >= 25)
2019 {
2020 /* Wide mode allows 16 bit offsets. */
2021 max_offset = 32768;
2022 insn &= ~ 0xfff1;
dc810e39 2023 insn |= re_assemble_16 ((int) value);
b352eebf
AM
2024 }
2025 else
2026 {
2027 max_offset = 8192;
2028 insn &= ~ 0x3ff1;
dc810e39 2029 insn |= re_assemble_14 ((int) value);
b352eebf
AM
2030 }
2031
2032 if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2033 {
4eca0228 2034 _bfd_error_handler
695344c0 2035 /* xgettext:c-format */
2dcf00ce
AM
2036 (_("stub entry for %s cannot load .plt, dp offset = %" PRId64),
2037 hh->eh.root.root.string, (int64_t) value);
0a1b45a2 2038 return false;
b352eebf
AM
2039 }
2040
dc810e39 2041 bfd_put_32 (stub->owner, (bfd_vma) insn,
a03bd320 2042 stub->contents + hh->stub_offset);
15bda425
JL
2043
2044 /* Fix up the second ldd instruction. */
b352eebf 2045 value += 8;
a03bd320 2046 insn = bfd_get_32 (stub->owner, stub->contents + hh->stub_offset + 8);
b352eebf
AM
2047 if (output_bfd->arch_info->mach >= 25)
2048 {
2049 insn &= ~ 0xfff1;
dc810e39 2050 insn |= re_assemble_16 ((int) value);
b352eebf
AM
2051 }
2052 else
2053 {
2054 insn &= ~ 0x3ff1;
dc810e39 2055 insn |= re_assemble_14 ((int) value);
b352eebf 2056 }
dc810e39 2057 bfd_put_32 (stub->owner, (bfd_vma) insn,
a03bd320 2058 stub->contents + hh->stub_offset + 8);
15bda425
JL
2059 }
2060
0a1b45a2 2061 return true;
15bda425
JL
2062}
2063
2064/* The .opd section contains FPTRs for each function this file
2065 exports. Initialize the FPTR entries. */
2066
0a1b45a2 2067static bool
a03bd320 2068elf64_hppa_finalize_opd (struct elf_link_hash_entry *eh, void *data)
15bda425 2069{
a03bd320 2070 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
2071 struct bfd_link_info *info = (struct bfd_link_info *)data;
2072 struct elf64_hppa_link_hash_table *hppa_info;
15bda425
JL
2073 asection *sopd;
2074 asection *sopdrel;
2075
a03bd320 2076 hppa_info = hppa_link_hash_table (info);
4dfe6ac6 2077 if (hppa_info == NULL)
0a1b45a2 2078 return false;
4dfe6ac6 2079
15bda425
JL
2080 sopd = hppa_info->opd_sec;
2081 sopdrel = hppa_info->opd_rel_sec;
2082
a03bd320 2083 if (hh->want_opd)
15bda425
JL
2084 {
2085 bfd_vma value;
2086
fe8bc63d 2087 /* The first two words of an .opd entry are zero.
15bda425
JL
2088
2089 We are modifying the contents of the OPD section in memory, so we
2090 do not need to include its output offset in this computation. */
a03bd320 2091 memset (sopd->contents + hh->opd_offset, 0, 16);
15bda425 2092
a03bd320
DA
2093 value = (eh->root.u.def.value
2094 + eh->root.u.def.section->output_section->vma
2095 + eh->root.u.def.section->output_offset);
15bda425
JL
2096
2097 /* The next word is the address of the function. */
a03bd320 2098 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 16);
15bda425
JL
2099
2100 /* The last word is our local __gp value. */
586338b8 2101 value = _bfd_get_gp_value (info->output_bfd);
a03bd320 2102 bfd_put_64 (sopd->owner, value, sopd->contents + hh->opd_offset + 24);
15bda425
JL
2103 }
2104
2105 /* If we are generating a shared library, we must generate EPLT relocations
2106 for each entry in the .opd, even for static functions (they may have
2107 had their address taken). */
0e1862bb 2108 if (bfd_link_pic (info) && hh->want_opd)
15bda425 2109 {
947216bf
AM
2110 Elf_Internal_Rela rel;
2111 bfd_byte *loc;
15bda425
JL
2112 int dynindx;
2113
2114 /* We may need to do a relocation against a local symbol, in
2115 which case we have to look up it's dynamic symbol index off
2116 the local symbol hash table. */
a03bd320
DA
2117 if (eh->dynindx != -1)
2118 dynindx = eh->dynindx;
15bda425
JL
2119 else
2120 dynindx
a03bd320
DA
2121 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2122 hh->sym_indx);
15bda425
JL
2123
2124 /* The offset of this relocation is the absolute address of the
2125 .opd entry for this symbol. */
a03bd320 2126 rel.r_offset = (hh->opd_offset + sopd->output_offset
15bda425
JL
2127 + sopd->output_section->vma);
2128
2129 /* If H is non-null, then we have an external symbol.
2130
2131 It is imperative that we use a different dynamic symbol for the
2132 EPLT relocation if the symbol has global scope.
2133
2134 In the dynamic symbol table, the function symbol will have a value
2135 which is address of the function's .opd entry.
2136
2137 Thus, we can not use that dynamic symbol for the EPLT relocation
2138 (if we did, the data in the .opd would reference itself rather
2139 than the actual address of the function). Instead we have to use
2140 a new dynamic symbol which has the same value as the original global
fe8bc63d 2141 function symbol.
15bda425
JL
2142
2143 We prefix the original symbol with a "." and use the new symbol in
2144 the EPLT relocation. This new symbol has already been recorded in
2145 the symbol table, we just have to look it up and use it.
2146
2147 We do not have such problems with static functions because we do
2148 not make their addresses in the dynamic symbol table point to
2149 the .opd entry. Ultimately this should be safe since a static
2150 function can not be directly referenced outside of its shared
2151 library.
2152
2153 We do have to play similar games for FPTR relocations in shared
2154 libraries, including those for static symbols. See the FPTR
2155 handling in elf64_hppa_finalize_dynreloc. */
a03bd320 2156 if (eh)
15bda425
JL
2157 {
2158 char *new_name;
2159 struct elf_link_hash_entry *nh;
2160
e1fa0163 2161 new_name = concat (".", eh->root.root.string, NULL);
15bda425
JL
2162
2163 nh = elf_link_hash_lookup (elf_hash_table (info),
0a1b45a2 2164 new_name, true, true, false);
68ffbac6 2165
15bda425
JL
2166 /* All we really want from the new symbol is its dynamic
2167 symbol index. */
7fb9f789
NC
2168 if (nh)
2169 dynindx = nh->dynindx;
e1fa0163 2170 free (new_name);
15bda425
JL
2171 }
2172
2173 rel.r_addend = 0;
2174 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2175
947216bf
AM
2176 loc = sopdrel->contents;
2177 loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
586338b8 2178 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
15bda425 2179 }
0a1b45a2 2180 return true;
15bda425
JL
2181}
2182
2183/* The .dlt section contains addresses for items referenced through the
2184 dlt. Note that we can have a DLTIND relocation for a local symbol, thus
2185 we can not depend on finish_dynamic_symbol to initialize the .dlt. */
2186
0a1b45a2 2187static bool
a03bd320 2188elf64_hppa_finalize_dlt (struct elf_link_hash_entry *eh, void *data)
15bda425 2189{
a03bd320 2190 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
2191 struct bfd_link_info *info = (struct bfd_link_info *)data;
2192 struct elf64_hppa_link_hash_table *hppa_info;
2193 asection *sdlt, *sdltrel;
15bda425 2194
a03bd320 2195 hppa_info = hppa_link_hash_table (info);
4dfe6ac6 2196 if (hppa_info == NULL)
0a1b45a2 2197 return false;
15bda425
JL
2198
2199 sdlt = hppa_info->dlt_sec;
2200 sdltrel = hppa_info->dlt_rel_sec;
2201
2202 /* H/DYN_H may refer to a local variable and we know it's
2203 address, so there is no need to create a relocation. Just install
2204 the proper value into the DLT, note this shortcut can not be
2205 skipped when building a shared library. */
0e1862bb 2206 if (! bfd_link_pic (info) && hh && hh->want_dlt)
15bda425
JL
2207 {
2208 bfd_vma value;
2209
2210 /* If we had an LTOFF_FPTR style relocation we want the DLT entry
fe8bc63d 2211 to point to the FPTR entry in the .opd section.
15bda425
JL
2212
2213 We include the OPD's output offset in this computation as
2214 we are referring to an absolute address in the resulting
2215 object file. */
a03bd320 2216 if (hh->want_opd)
15bda425 2217 {
a03bd320 2218 value = (hh->opd_offset
15bda425
JL
2219 + hppa_info->opd_sec->output_offset
2220 + hppa_info->opd_sec->output_section->vma);
2221 }
a03bd320
DA
2222 else if ((eh->root.type == bfd_link_hash_defined
2223 || eh->root.type == bfd_link_hash_defweak)
2224 && eh->root.u.def.section)
15bda425 2225 {
a03bd320
DA
2226 value = eh->root.u.def.value + eh->root.u.def.section->output_offset;
2227 if (eh->root.u.def.section->output_section)
2228 value += eh->root.u.def.section->output_section->vma;
15bda425 2229 else
a03bd320 2230 value += eh->root.u.def.section->vma;
15bda425 2231 }
3db4b612
JL
2232 else
2233 /* We have an undefined function reference. */
2234 value = 0;
15bda425
JL
2235
2236 /* We do not need to include the output offset of the DLT section
2237 here because we are modifying the in-memory contents. */
a03bd320 2238 bfd_put_64 (sdlt->owner, value, sdlt->contents + hh->dlt_offset);
15bda425
JL
2239 }
2240
4cc11e76 2241 /* Create a relocation for the DLT entry associated with this symbol.
15bda425 2242 When building a shared library the symbol does not have to be dynamic. */
a03bd320 2243 if (hh->want_dlt
0e1862bb 2244 && (elf64_hppa_dynamic_symbol_p (eh, info) || bfd_link_pic (info)))
15bda425 2245 {
947216bf
AM
2246 Elf_Internal_Rela rel;
2247 bfd_byte *loc;
15bda425
JL
2248 int dynindx;
2249
2250 /* We may need to do a relocation against a local symbol, in
2251 which case we have to look up it's dynamic symbol index off
2252 the local symbol hash table. */
a03bd320
DA
2253 if (eh && eh->dynindx != -1)
2254 dynindx = eh->dynindx;
15bda425
JL
2255 else
2256 dynindx
a03bd320
DA
2257 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2258 hh->sym_indx);
15bda425 2259
15bda425
JL
2260 /* Create a dynamic relocation for this entry. Do include the output
2261 offset of the DLT entry since we need an absolute address in the
2262 resulting object file. */
a03bd320 2263 rel.r_offset = (hh->dlt_offset + sdlt->output_offset
15bda425 2264 + sdlt->output_section->vma);
a03bd320 2265 if (eh && eh->type == STT_FUNC)
15bda425
JL
2266 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2267 else
2268 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2269 rel.r_addend = 0;
2270
947216bf
AM
2271 loc = sdltrel->contents;
2272 loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
586338b8 2273 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
15bda425 2274 }
0a1b45a2 2275 return true;
15bda425
JL
2276}
2277
2278/* Finalize the dynamic relocations. Specifically the FPTR relocations
2279 for dynamic functions used to initialize static data. */
2280
0a1b45a2 2281static bool
a03bd320 2282elf64_hppa_finalize_dynreloc (struct elf_link_hash_entry *eh,
813c8a3c 2283 void *data)
15bda425 2284{
a03bd320 2285 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
15bda425
JL
2286 struct bfd_link_info *info = (struct bfd_link_info *)data;
2287 struct elf64_hppa_link_hash_table *hppa_info;
15bda425
JL
2288 int dynamic_symbol;
2289
a03bd320 2290 dynamic_symbol = elf64_hppa_dynamic_symbol_p (eh, info);
15bda425 2291
0e1862bb 2292 if (!dynamic_symbol && !bfd_link_pic (info))
0a1b45a2 2293 return true;
15bda425 2294
a03bd320 2295 if (hh->reloc_entries)
15bda425
JL
2296 {
2297 struct elf64_hppa_dyn_reloc_entry *rent;
2298 int dynindx;
2299
a03bd320 2300 hppa_info = hppa_link_hash_table (info);
4dfe6ac6 2301 if (hppa_info == NULL)
0a1b45a2 2302 return false;
15bda425
JL
2303
2304 /* We may need to do a relocation against a local symbol, in
2305 which case we have to look up it's dynamic symbol index off
2306 the local symbol hash table. */
a03bd320
DA
2307 if (eh->dynindx != -1)
2308 dynindx = eh->dynindx;
15bda425
JL
2309 else
2310 dynindx
a03bd320
DA
2311 = _bfd_elf_link_lookup_local_dynindx (info, hh->owner,
2312 hh->sym_indx);
15bda425 2313
a03bd320 2314 for (rent = hh->reloc_entries; rent; rent = rent->next)
15bda425 2315 {
947216bf
AM
2316 Elf_Internal_Rela rel;
2317 bfd_byte *loc;
15bda425 2318
d663e1cd
JL
2319 /* Allocate one iff we are building a shared library, the relocation
2320 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
0e1862bb
L
2321 if (!bfd_link_pic (info)
2322 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
d663e1cd 2323 continue;
15bda425 2324
fe8bc63d 2325 /* Create a dynamic relocation for this entry.
15bda425
JL
2326
2327 We need the output offset for the reloc's section because
2328 we are creating an absolute address in the resulting object
2329 file. */
2330 rel.r_offset = (rent->offset + rent->sec->output_offset
2331 + rent->sec->output_section->vma);
2332
2333 /* An FPTR64 relocation implies that we took the address of
2334 a function and that the function has an entry in the .opd
2335 section. We want the FPTR64 relocation to reference the
2336 entry in .opd.
2337
2338 We could munge the symbol value in the dynamic symbol table
2339 (in fact we already do for functions with global scope) to point
2340 to the .opd entry. Then we could use that dynamic symbol in
2341 this relocation.
2342
2343 Or we could do something sensible, not munge the symbol's
2344 address and instead just use a different symbol to reference
2345 the .opd entry. At least that seems sensible until you
2346 realize there's no local dynamic symbols we can use for that
2347 purpose. Thus the hair in the check_relocs routine.
fe8bc63d 2348
15bda425
JL
2349 We use a section symbol recorded by check_relocs as the
2350 base symbol for the relocation. The addend is the difference
2351 between the section symbol and the address of the .opd entry. */
0e1862bb
L
2352 if (bfd_link_pic (info)
2353 && rent->type == R_PARISC_FPTR64 && hh->want_opd)
15bda425
JL
2354 {
2355 bfd_vma value, value2;
15bda425
JL
2356
2357 /* First compute the address of the opd entry for this symbol. */
a03bd320 2358 value = (hh->opd_offset
15bda425
JL
2359 + hppa_info->opd_sec->output_section->vma
2360 + hppa_info->opd_sec->output_offset);
2361
2362 /* Compute the value of the start of the section with
2363 the relocation. */
2364 value2 = (rent->sec->output_section->vma
2365 + rent->sec->output_offset);
2366
2367 /* Compute the difference between the start of the section
2368 with the relocation and the opd entry. */
2369 value -= value2;
fe8bc63d 2370
15bda425
JL
2371 /* The result becomes the addend of the relocation. */
2372 rel.r_addend = value;
2373
2374 /* The section symbol becomes the symbol for the dynamic
2375 relocation. */
2376 dynindx
2377 = _bfd_elf_link_lookup_local_dynindx (info,
2378 rent->sec->owner,
2379 rent->sec_symndx);
2380 }
2381 else
2382 rel.r_addend = rent->addend;
2383
2384 rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2385
947216bf
AM
2386 loc = hppa_info->other_rel_sec->contents;
2387 loc += (hppa_info->other_rel_sec->reloc_count++
2388 * sizeof (Elf64_External_Rela));
586338b8 2389 bfd_elf64_swap_reloca_out (info->output_bfd, &rel, loc);
15bda425
JL
2390 }
2391 }
2392
0a1b45a2 2393 return true;
15bda425
JL
2394}
2395
5ac81c74
JL
2396/* Used to decide how to sort relocs in an optimal manner for the
2397 dynamic linker, before writing them out. */
2398
2399static enum elf_reloc_type_class
7e612e98
AM
2400elf64_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2401 const asection *rel_sec ATTRIBUTE_UNUSED,
2402 const Elf_Internal_Rela *rela)
5ac81c74 2403{
cf35638d 2404 if (ELF64_R_SYM (rela->r_info) == STN_UNDEF)
5ac81c74
JL
2405 return reloc_class_relative;
2406
2407 switch ((int) ELF64_R_TYPE (rela->r_info))
2408 {
2409 case R_PARISC_IPLT:
2410 return reloc_class_plt;
2411 case R_PARISC_COPY:
2412 return reloc_class_copy;
2413 default:
2414 return reloc_class_normal;
2415 }
2416}
2417
15bda425
JL
2418/* Finish up the dynamic sections. */
2419
0a1b45a2 2420static bool
813c8a3c
DA
2421elf64_hppa_finish_dynamic_sections (bfd *output_bfd,
2422 struct bfd_link_info *info)
15bda425
JL
2423{
2424 bfd *dynobj;
2425 asection *sdyn;
2426 struct elf64_hppa_link_hash_table *hppa_info;
2427
a03bd320 2428 hppa_info = hppa_link_hash_table (info);
4dfe6ac6 2429 if (hppa_info == NULL)
0a1b45a2 2430 return false;
15bda425
JL
2431
2432 /* Finalize the contents of the .opd section. */
a03bd320
DA
2433 elf_link_hash_traverse (elf_hash_table (info),
2434 elf64_hppa_finalize_opd,
2435 info);
15bda425 2436
a03bd320
DA
2437 elf_link_hash_traverse (elf_hash_table (info),
2438 elf64_hppa_finalize_dynreloc,
2439 info);
15bda425
JL
2440
2441 /* Finalize the contents of the .dlt section. */
2442 dynobj = elf_hash_table (info)->dynobj;
2443 /* Finalize the contents of the .dlt section. */
a03bd320
DA
2444 elf_link_hash_traverse (elf_hash_table (info),
2445 elf64_hppa_finalize_dlt,
2446 info);
15bda425 2447
3d4d4302 2448 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
15bda425
JL
2449
2450 if (elf_hash_table (info)->dynamic_sections_created)
2451 {
2452 Elf64_External_Dyn *dyncon, *dynconend;
15bda425
JL
2453
2454 BFD_ASSERT (sdyn != NULL);
2455
2456 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 2457 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15bda425
JL
2458 for (; dyncon < dynconend; dyncon++)
2459 {
2460 Elf_Internal_Dyn dyn;
2461 asection *s;
2462
2463 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2464
2465 switch (dyn.d_tag)
2466 {
2467 default:
2468 break;
2469
2470 case DT_HP_LOAD_MAP:
2471 /* Compute the absolute address of 16byte scratchpad area
2472 for the dynamic linker.
2473
2474 By convention the linker script will allocate the scratchpad
2475 area at the start of the .data section. So all we have to
2476 to is find the start of the .data section. */
2477 s = bfd_get_section_by_name (output_bfd, ".data");
a505d7ac 2478 if (!s)
0a1b45a2 2479 return false;
15bda425
JL
2480 dyn.d_un.d_ptr = s->vma;
2481 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2482 break;
2483
2484 case DT_PLTGOT:
2485 /* HP's use PLTGOT to set the GOT register. */
2486 dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2487 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2488 break;
2489
2490 case DT_JMPREL:
9b8a8575 2491 s = hppa_info->root.srelplt;
15bda425
JL
2492 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2493 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2494 break;
2495
2496 case DT_PLTRELSZ:
9b8a8575 2497 s = hppa_info->root.srelplt;
eea6121a 2498 dyn.d_un.d_val = s->size;
15bda425
JL
2499 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2500 break;
2501
2502 case DT_RELA:
2503 s = hppa_info->other_rel_sec;
eea6121a 2504 if (! s || ! s->size)
15bda425 2505 s = hppa_info->dlt_rel_sec;
eea6121a 2506 if (! s || ! s->size)
5ac81c74 2507 s = hppa_info->opd_rel_sec;
15bda425
JL
2508 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2509 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2510 break;
2511
2512 case DT_RELASZ:
2513 s = hppa_info->other_rel_sec;
eea6121a 2514 dyn.d_un.d_val = s->size;
15bda425 2515 s = hppa_info->dlt_rel_sec;
eea6121a 2516 dyn.d_un.d_val += s->size;
15bda425 2517 s = hppa_info->opd_rel_sec;
eea6121a 2518 dyn.d_un.d_val += s->size;
15bda425
JL
2519 /* There is some question about whether or not the size of
2520 the PLT relocs should be included here. HP's tools do
2521 it, so we'll emulate them. */
9b8a8575 2522 s = hppa_info->root.srelplt;
eea6121a 2523 dyn.d_un.d_val += s->size;
15bda425
JL
2524 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2525 break;
2526
2527 }
2528 }
2529 }
2530
0a1b45a2 2531 return true;
15bda425
JL
2532}
2533
235ecfbc
NC
2534/* Support for core dump NOTE sections. */
2535
0a1b45a2 2536static bool
235ecfbc
NC
2537elf64_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2538{
2539 int offset;
2540 size_t size;
2541
2542 switch (note->descsz)
2543 {
2544 default:
0a1b45a2 2545 return false;
235ecfbc
NC
2546
2547 case 760: /* Linux/hppa */
2548 /* pr_cursig */
228e534f 2549 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
235ecfbc
NC
2550
2551 /* pr_pid */
228e534f 2552 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
235ecfbc
NC
2553
2554 /* pr_reg */
2555 offset = 112;
2556 size = 640;
2557
2558 break;
2559 }
2560
2561 /* Make a ".reg/999" section. */
2562 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2563 size, note->descpos + offset);
2564}
2565
0a1b45a2 2566static bool
235ecfbc
NC
2567elf64_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2568{
2569 char * command;
2570 int n;
2571
2572 switch (note->descsz)
2573 {
2574 default:
0a1b45a2 2575 return false;
235ecfbc
NC
2576
2577 case 136: /* Linux/hppa elf_prpsinfo. */
228e534f 2578 elf_tdata (abfd)->core->program
235ecfbc 2579 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2580 elf_tdata (abfd)->core->command
235ecfbc
NC
2581 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2582 }
2583
2584 /* Note that for some reason, a spurious space is tacked
2585 onto the end of the args in some (at least one anyway)
2586 implementations, so strip it off if it exists. */
228e534f 2587 command = elf_tdata (abfd)->core->command;
235ecfbc
NC
2588 n = strlen (command);
2589
2590 if (0 < n && command[n - 1] == ' ')
2591 command[n - 1] = '\0';
2592
0a1b45a2 2593 return true;
235ecfbc
NC
2594}
2595
15bda425
JL
2596/* Return the number of additional phdrs we will need.
2597
2598 The generic ELF code only creates PT_PHDRs for executables. The HP
fe8bc63d 2599 dynamic linker requires PT_PHDRs for dynamic libraries too.
15bda425
JL
2600
2601 This routine indicates that the backend needs one additional program
2602 header for that case.
2603
2604 Note we do not have access to the link info structure here, so we have
2605 to guess whether or not we are building a shared library based on the
2606 existence of a .interp section. */
2607
2608static int
a6b96beb 2609elf64_hppa_additional_program_headers (bfd *abfd,
813c8a3c 2610 struct bfd_link_info *info ATTRIBUTE_UNUSED)
15bda425
JL
2611{
2612 asection *s;
2613
2614 /* If we are creating a shared library, then we have to create a
2615 PT_PHDR segment. HP's dynamic linker chokes without it. */
2616 s = bfd_get_section_by_name (abfd, ".interp");
2617 if (! s)
2618 return 1;
2619 return 0;
2620}
2621
0a1b45a2 2622static bool
1a9ccd70
NC
2623elf64_hppa_allow_non_load_phdr (bfd *abfd ATTRIBUTE_UNUSED,
2624 const Elf_Internal_Phdr *phdr ATTRIBUTE_UNUSED,
2625 unsigned int count ATTRIBUTE_UNUSED)
2626{
0a1b45a2 2627 return true;
1a9ccd70
NC
2628}
2629
15bda425
JL
2630/* Allocate and initialize any program headers required by this
2631 specific backend.
2632
2633 The generic ELF code only creates PT_PHDRs for executables. The HP
fe8bc63d 2634 dynamic linker requires PT_PHDRs for dynamic libraries too.
15bda425
JL
2635
2636 This allocates the PT_PHDR and initializes it in a manner suitable
fe8bc63d 2637 for the HP linker.
15bda425
JL
2638
2639 Note we do not have access to the link info structure here, so we have
2640 to guess whether or not we are building a shared library based on the
2641 existence of a .interp section. */
2642
0a1b45a2 2643static bool
22b05d33 2644elf64_hppa_modify_segment_map (bfd *abfd, struct bfd_link_info *info)
15bda425 2645{
edd21aca 2646 struct elf_segment_map *m;
15bda425 2647
22b05d33
AM
2648 m = elf_seg_map (abfd);
2649 if (info != NULL && !info->user_phdrs && m != NULL && m->p_type != PT_PHDR)
15bda425 2650 {
22b05d33
AM
2651 m = ((struct elf_segment_map *)
2652 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
15bda425 2653 if (m == NULL)
0a1b45a2 2654 return false;
15bda425 2655
22b05d33
AM
2656 m->p_type = PT_PHDR;
2657 m->p_flags = PF_R | PF_X;
2658 m->p_flags_valid = 1;
2659 m->p_paddr_valid = 1;
2660 m->includes_phdrs = 1;
15bda425 2661
22b05d33
AM
2662 m->next = elf_seg_map (abfd);
2663 elf_seg_map (abfd) = m;
15bda425
JL
2664 }
2665
22b05d33 2666 for (m = elf_seg_map (abfd) ; m != NULL; m = m->next)
15bda425
JL
2667 if (m->p_type == PT_LOAD)
2668 {
0ba2a60e 2669 unsigned int i;
15bda425
JL
2670
2671 for (i = 0; i < m->count; i++)
2672 {
2673 /* The code "hint" is not really a hint. It is a requirement
2674 for certain versions of the HP dynamic linker. Worse yet,
2675 it must be set even if the shared library does not have
2676 any code in its "text" segment (thus the check for .hash
2677 to catch this situation). */
2678 if (m->sections[i]->flags & SEC_CODE
2679 || (strcmp (m->sections[i]->name, ".hash") == 0))
2680 m->p_flags |= (PF_X | PF_HP_CODE);
2681 }
2682 }
2683
0a1b45a2 2684 return true;
15bda425
JL
2685}
2686
3fab46d0
AM
2687/* Called when writing out an object file to decide the type of a
2688 symbol. */
2689static int
813c8a3c
DA
2690elf64_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym,
2691 int type)
3fab46d0
AM
2692{
2693 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2694 return STT_PARISC_MILLI;
2695 else
2696 return type;
2697}
2698
d97a8924 2699/* Support HP specific sections for core files. */
91d6fa6a 2700
0a1b45a2 2701static bool
91d6fa6a 2702elf64_hppa_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int sec_index,
d97a8924
DA
2703 const char *typename)
2704{
927e625f
MK
2705 if (hdr->p_type == PT_HP_CORE_KERNEL)
2706 {
2707 asection *sect;
2708
91d6fa6a 2709 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
0a1b45a2 2710 return false;
927e625f
MK
2711
2712 sect = bfd_make_section_anyway (abfd, ".kernel");
2713 if (sect == NULL)
0a1b45a2 2714 return false;
927e625f
MK
2715 sect->size = hdr->p_filesz;
2716 sect->filepos = hdr->p_offset;
2717 sect->flags = SEC_HAS_CONTENTS | SEC_READONLY;
0a1b45a2 2718 return true;
927e625f
MK
2719 }
2720
d97a8924
DA
2721 if (hdr->p_type == PT_HP_CORE_PROC)
2722 {
2723 int sig;
2724
2725 if (bfd_seek (abfd, hdr->p_offset, SEEK_SET) != 0)
0a1b45a2 2726 return false;
226f9f4f 2727 if (bfd_read (&sig, 4, abfd) != 4)
0a1b45a2 2728 return false;
d97a8924 2729
228e534f 2730 elf_tdata (abfd)->core->signal = sig;
d97a8924 2731
91d6fa6a 2732 if (!_bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename))
0a1b45a2 2733 return false;
927e625f
MK
2734
2735 /* GDB uses the ".reg" section to read register contents. */
2736 return _bfd_elfcore_make_pseudosection (abfd, ".reg", hdr->p_filesz,
2737 hdr->p_offset);
d97a8924
DA
2738 }
2739
2740 if (hdr->p_type == PT_HP_CORE_LOADABLE
2741 || hdr->p_type == PT_HP_CORE_STACK
2742 || hdr->p_type == PT_HP_CORE_MMF)
2743 hdr->p_type = PT_LOAD;
2744
91d6fa6a 2745 return _bfd_elf_make_section_from_phdr (abfd, hdr, sec_index, typename);
d97a8924
DA
2746}
2747
5887528b
DA
2748/* Hook called by the linker routine which adds symbols from an object
2749 file. HP's libraries define symbols with HP specific section
2750 indices, which we have to handle. */
2751
0a1b45a2 2752static bool
5887528b
DA
2753elf_hppa_add_symbol_hook (bfd *abfd,
2754 struct bfd_link_info *info ATTRIBUTE_UNUSED,
2755 Elf_Internal_Sym *sym,
2756 const char **namep ATTRIBUTE_UNUSED,
2757 flagword *flagsp ATTRIBUTE_UNUSED,
2758 asection **secp,
2759 bfd_vma *valp)
2760{
91d6fa6a 2761 unsigned int sec_index = sym->st_shndx;
5887528b 2762
91d6fa6a 2763 switch (sec_index)
5887528b
DA
2764 {
2765 case SHN_PARISC_ANSI_COMMON:
2766 *secp = bfd_make_section_old_way (abfd, ".PARISC.ansi.common");
2767 (*secp)->flags |= SEC_IS_COMMON;
2768 *valp = sym->st_size;
2769 break;
2770
2771 case SHN_PARISC_HUGE_COMMON:
2772 *secp = bfd_make_section_old_way (abfd, ".PARISC.huge.common");
2773 (*secp)->flags |= SEC_IS_COMMON;
2774 *valp = sym->st_size;
2775 break;
2776 }
2777
0a1b45a2 2778 return true;
5887528b
DA
2779}
2780
0a1b45a2 2781static bool
5887528b
DA
2782elf_hppa_unmark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2783 void *data)
2784{
2785 struct bfd_link_info *info = data;
2786
5887528b
DA
2787 /* If we are not creating a shared library, and this symbol is
2788 referenced by a shared library but is not defined anywhere, then
2789 the generic code will warn that it is undefined.
2790
2791 This behavior is undesirable on HPs since the standard shared
2792 libraries contain references to undefined symbols.
2793
2794 So we twiddle the flags associated with such symbols so that they
2795 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2796
2797 Ultimately we should have better controls over the generic ELF BFD
2798 linker code. */
0e1862bb 2799 if (! bfd_link_relocatable (info)
5887528b
DA
2800 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2801 && h->root.type == bfd_link_hash_undefined
2802 && h->ref_dynamic
2803 && !h->ref_regular)
2804 {
2805 h->ref_dynamic = 0;
2806 h->pointer_equality_needed = 1;
2807 }
2808
0a1b45a2 2809 return true;
5887528b
DA
2810}
2811
0a1b45a2 2812static bool
5887528b
DA
2813elf_hppa_remark_useless_dynamic_symbols (struct elf_link_hash_entry *h,
2814 void *data)
2815{
2816 struct bfd_link_info *info = data;
2817
5887528b
DA
2818 /* If we are not creating a shared library, and this symbol is
2819 referenced by a shared library but is not defined anywhere, then
2820 the generic code will warn that it is undefined.
2821
2822 This behavior is undesirable on HPs since the standard shared
2823 libraries contain references to undefined symbols.
2824
2825 So we twiddle the flags associated with such symbols so that they
2826 will not trigger the warning. ?!? FIXME. This is horribly fragile.
2827
2828 Ultimately we should have better controls over the generic ELF BFD
2829 linker code. */
0e1862bb 2830 if (! bfd_link_relocatable (info)
5887528b
DA
2831 && info->unresolved_syms_in_shared_libs != RM_IGNORE
2832 && h->root.type == bfd_link_hash_undefined
2833 && !h->ref_dynamic
2834 && !h->ref_regular
2835 && h->pointer_equality_needed)
2836 {
2837 h->ref_dynamic = 1;
2838 h->pointer_equality_needed = 0;
2839 }
2840
0a1b45a2 2841 return true;
5887528b
DA
2842}
2843
0a1b45a2 2844static bool
5887528b
DA
2845elf_hppa_is_dynamic_loader_symbol (const char *name)
2846{
2847 return (! strcmp (name, "__CPU_REVISION")
2848 || ! strcmp (name, "__CPU_KEYBITS_1")
2849 || ! strcmp (name, "__SYSTEM_ID_D")
2850 || ! strcmp (name, "__FPU_MODEL")
2851 || ! strcmp (name, "__FPU_REVISION")
2852 || ! strcmp (name, "__ARGC")
2853 || ! strcmp (name, "__ARGV")
2854 || ! strcmp (name, "__ENVP")
2855 || ! strcmp (name, "__TLS_SIZE_D")
2856 || ! strcmp (name, "__LOAD_INFO")
2857 || ! strcmp (name, "__systab"));
2858}
2859
2860/* Record the lowest address for the data and text segments. */
2861static void
2862elf_hppa_record_segment_addrs (bfd *abfd,
2863 asection *section,
2864 void *data)
2865{
2866 struct elf64_hppa_link_hash_table *hppa_info = data;
2867
2868 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2869 {
2870 bfd_vma value;
2871 Elf_Internal_Phdr *p;
2872
2873 p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
2874 BFD_ASSERT (p != NULL);
2875 value = p->p_vaddr;
2876
2877 if (section->flags & SEC_READONLY)
2878 {
2879 if (value < hppa_info->text_segment_base)
2880 hppa_info->text_segment_base = value;
2881 }
2882 else
2883 {
2884 if (value < hppa_info->data_segment_base)
2885 hppa_info->data_segment_base = value;
2886 }
2887 }
2888}
2889
2890/* Called after we have seen all the input files/sections, but before
2891 final symbol resolution and section placement has been determined.
2892
2893 We use this hook to (possibly) provide a value for __gp, then we
2894 fall back to the generic ELF final link routine. */
2895
0a1b45a2 2896static bool
5887528b
DA
2897elf_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
2898{
6d4b2867 2899 struct stat buf;
5887528b
DA
2900 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
2901
4dfe6ac6 2902 if (hppa_info == NULL)
0a1b45a2 2903 return false;
4dfe6ac6 2904
0e1862bb 2905 if (! bfd_link_relocatable (info))
5887528b
DA
2906 {
2907 struct elf_link_hash_entry *gp;
2908 bfd_vma gp_val;
2909
2910 /* The linker script defines a value for __gp iff it was referenced
2911 by one of the objects being linked. First try to find the symbol
2912 in the hash table. If that fails, just compute the value __gp
2913 should have had. */
0a1b45a2
AM
2914 gp = elf_link_hash_lookup (elf_hash_table (info), "__gp", false,
2915 false, false);
5887528b
DA
2916
2917 if (gp)
2918 {
2919
2920 /* Adjust the value of __gp as we may want to slide it into the
2921 .plt section so that the stubs can access PLT entries without
2922 using an addil sequence. */
2923 gp->root.u.def.value += hppa_info->gp_offset;
2924
2925 gp_val = (gp->root.u.def.section->output_section->vma
2926 + gp->root.u.def.section->output_offset
2927 + gp->root.u.def.value);
2928 }
2929 else
2930 {
2931 asection *sec;
2932
2933 /* First look for a .plt section. If found, then __gp is the
2934 address of the .plt + gp_offset.
2935
2936 If no .plt is found, then look for .dlt, .opd and .data (in
2937 that order) and set __gp to the base address of whichever
2938 section is found first. */
2939
9b8a8575 2940 sec = hppa_info->root.splt;
5887528b
DA
2941 if (sec && ! (sec->flags & SEC_EXCLUDE))
2942 gp_val = (sec->output_offset
2943 + sec->output_section->vma
2944 + hppa_info->gp_offset);
2945 else
2946 {
2947 sec = hppa_info->dlt_sec;
2948 if (!sec || (sec->flags & SEC_EXCLUDE))
2949 sec = hppa_info->opd_sec;
2950 if (!sec || (sec->flags & SEC_EXCLUDE))
2951 sec = bfd_get_section_by_name (abfd, ".data");
2952 if (!sec || (sec->flags & SEC_EXCLUDE))
2953 gp_val = 0;
2954 else
2955 gp_val = sec->output_offset + sec->output_section->vma;
2956 }
2957 }
2958
2959 /* Install whatever value we found/computed for __gp. */
2960 _bfd_set_gp_value (abfd, gp_val);
2961 }
2962
2963 /* We need to know the base of the text and data segments so that we
2964 can perform SEGREL relocations. We will record the base addresses
2965 when we encounter the first SEGREL relocation. */
2966 hppa_info->text_segment_base = (bfd_vma)-1;
2967 hppa_info->data_segment_base = (bfd_vma)-1;
2968
2969 /* HP's shared libraries have references to symbols that are not
2970 defined anywhere. The generic ELF BFD linker code will complain
2971 about such symbols.
2972
2973 So we detect the losing case and arrange for the flags on the symbol
2974 to indicate that it was never referenced. This keeps the generic
2975 ELF BFD link code happy and appears to not create any secondary
2976 problems. Ultimately we need a way to control the behavior of the
2977 generic ELF BFD link code better. */
2978 elf_link_hash_traverse (elf_hash_table (info),
2979 elf_hppa_unmark_useless_dynamic_symbols,
2980 info);
2981
2982 /* Invoke the regular ELF backend linker to do all the work. */
6d4b2867 2983 if (!bfd_elf_final_link (abfd, info))
0a1b45a2 2984 return false;
5887528b
DA
2985
2986 elf_link_hash_traverse (elf_hash_table (info),
2987 elf_hppa_remark_useless_dynamic_symbols,
2988 info);
2989
2990 /* If we're producing a final executable, sort the contents of the
2991 unwind section. */
6d4b2867 2992 if (bfd_link_relocatable (info))
0a1b45a2 2993 return true;
6d4b2867
JDA
2994
2995 /* Do not attempt to sort non-regular files. This is here
2996 especially for configure scripts and kernel builds which run
2997 tests with "ld [...] -o /dev/null". */
765cf5f6 2998 if (stat (bfd_get_filename (abfd), &buf) != 0
6d4b2867 2999 || !S_ISREG(buf.st_mode))
0a1b45a2 3000 return true;
5887528b 3001
6d4b2867 3002 return elf_hppa_sort_unwind (abfd);
5887528b
DA
3003}
3004
3005/* Relocate the given INSN. VALUE should be the actual value we want
3006 to insert into the instruction, ie by this point we should not be
3007 concerned with computing an offset relative to the DLT, PC, etc.
3008 Instead this routine is meant to handle the bit manipulations needed
3009 to insert the relocation into the given instruction. */
3010
3011static int
3012elf_hppa_relocate_insn (int insn, int sym_value, unsigned int r_type)
3013{
3014 switch (r_type)
3015 {
3016 /* This is any 22 bit branch. In PA2.0 syntax it corresponds to
3017 the "B" instruction. */
3018 case R_PARISC_PCREL22F:
3019 case R_PARISC_PCREL22C:
3020 return (insn & ~0x3ff1ffd) | re_assemble_22 (sym_value);
3021
3022 /* This is any 12 bit branch. */
3023 case R_PARISC_PCREL12F:
3024 return (insn & ~0x1ffd) | re_assemble_12 (sym_value);
3025
3026 /* This is any 17 bit branch. In PA2.0 syntax it also corresponds
3027 to the "B" instruction as well as BE. */
3028 case R_PARISC_PCREL17F:
3029 case R_PARISC_DIR17F:
3030 case R_PARISC_DIR17R:
3031 case R_PARISC_PCREL17C:
3032 case R_PARISC_PCREL17R:
3033 return (insn & ~0x1f1ffd) | re_assemble_17 (sym_value);
3034
3035 /* ADDIL or LDIL instructions. */
3036 case R_PARISC_DLTREL21L:
3037 case R_PARISC_DLTIND21L:
3038 case R_PARISC_LTOFF_FPTR21L:
3039 case R_PARISC_PCREL21L:
3040 case R_PARISC_LTOFF_TP21L:
3041 case R_PARISC_DPREL21L:
3042 case R_PARISC_PLTOFF21L:
3043 case R_PARISC_DIR21L:
3044 return (insn & ~0x1fffff) | re_assemble_21 (sym_value);
3045
3046 /* LDO and integer loads/stores with 14 bit displacements. */
3047 case R_PARISC_DLTREL14R:
3048 case R_PARISC_DLTREL14F:
3049 case R_PARISC_DLTIND14R:
3050 case R_PARISC_DLTIND14F:
3051 case R_PARISC_LTOFF_FPTR14R:
3052 case R_PARISC_PCREL14R:
3053 case R_PARISC_PCREL14F:
3054 case R_PARISC_LTOFF_TP14R:
3055 case R_PARISC_LTOFF_TP14F:
3056 case R_PARISC_DPREL14R:
3057 case R_PARISC_DPREL14F:
3058 case R_PARISC_PLTOFF14R:
3059 case R_PARISC_PLTOFF14F:
3060 case R_PARISC_DIR14R:
3061 case R_PARISC_DIR14F:
3062 return (insn & ~0x3fff) | low_sign_unext (sym_value, 14);
3063
3064 /* PA2.0W LDO and integer loads/stores with 16 bit displacements. */
3065 case R_PARISC_LTOFF_FPTR16F:
3066 case R_PARISC_PCREL16F:
3067 case R_PARISC_LTOFF_TP16F:
3068 case R_PARISC_GPREL16F:
3069 case R_PARISC_PLTOFF16F:
3070 case R_PARISC_DIR16F:
3071 case R_PARISC_LTOFF16F:
3072 return (insn & ~0xffff) | re_assemble_16 (sym_value);
3073
3074 /* Doubleword loads and stores with a 14 bit displacement. */
3075 case R_PARISC_DLTREL14DR:
3076 case R_PARISC_DLTIND14DR:
3077 case R_PARISC_LTOFF_FPTR14DR:
3078 case R_PARISC_LTOFF_FPTR16DF:
3079 case R_PARISC_PCREL14DR:
3080 case R_PARISC_PCREL16DF:
3081 case R_PARISC_LTOFF_TP14DR:
3082 case R_PARISC_LTOFF_TP16DF:
3083 case R_PARISC_DPREL14DR:
3084 case R_PARISC_GPREL16DF:
3085 case R_PARISC_PLTOFF14DR:
3086 case R_PARISC_PLTOFF16DF:
3087 case R_PARISC_DIR14DR:
3088 case R_PARISC_DIR16DF:
3089 case R_PARISC_LTOFF16DF:
3090 return (insn & ~0x3ff1) | (((sym_value & 0x2000) >> 13)
3091 | ((sym_value & 0x1ff8) << 1));
3092
3093 /* Floating point single word load/store instructions. */
3094 case R_PARISC_DLTREL14WR:
3095 case R_PARISC_DLTIND14WR:
3096 case R_PARISC_LTOFF_FPTR14WR:
3097 case R_PARISC_LTOFF_FPTR16WF:
3098 case R_PARISC_PCREL14WR:
3099 case R_PARISC_PCREL16WF:
3100 case R_PARISC_LTOFF_TP14WR:
3101 case R_PARISC_LTOFF_TP16WF:
3102 case R_PARISC_DPREL14WR:
3103 case R_PARISC_GPREL16WF:
3104 case R_PARISC_PLTOFF14WR:
3105 case R_PARISC_PLTOFF16WF:
3106 case R_PARISC_DIR16WF:
3107 case R_PARISC_DIR14WR:
3108 case R_PARISC_LTOFF16WF:
3109 return (insn & ~0x3ff9) | (((sym_value & 0x2000) >> 13)
3110 | ((sym_value & 0x1ffc) << 1));
3111
3112 default:
3113 return insn;
3114 }
3115}
3116
3117/* Compute the value for a relocation (REL) during a final link stage,
3118 then insert the value into the proper location in CONTENTS.
3119
3120 VALUE is a tentative value for the relocation and may be overridden
3121 and modified here based on the specific relocation to be performed.
3122
3123 For example we do conversions for PC-relative branches in this routine
3124 or redirection of calls to external routines to stubs.
3125
3126 The work of actually applying the relocation is left to a helper
3127 routine in an attempt to reduce the complexity and size of this
3128 function. */
3129
3130static bfd_reloc_status_type
3131elf_hppa_final_link_relocate (Elf_Internal_Rela *rel,
3132 bfd *input_bfd,
3133 bfd *output_bfd,
3134 asection *input_section,
3135 bfd_byte *contents,
3136 bfd_vma value,
3137 struct bfd_link_info *info,
3138 asection *sym_sec,
3139 struct elf_link_hash_entry *eh)
3140{
3141 struct elf64_hppa_link_hash_table *hppa_info = hppa_link_hash_table (info);
3142 struct elf64_hppa_link_hash_entry *hh = hppa_elf_hash_entry (eh);
3143 bfd_vma *local_offsets;
3144 Elf_Internal_Shdr *symtab_hdr;
3145 int insn;
3146 bfd_vma max_branch_offset = 0;
3147 bfd_vma offset = rel->r_offset;
3148 bfd_signed_vma addend = rel->r_addend;
3149 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3150 unsigned int r_symndx = ELF_R_SYM (rel->r_info);
3151 unsigned int r_type = howto->type;
3152 bfd_byte *hit_data = contents + offset;
3153
4dfe6ac6
NC
3154 if (hppa_info == NULL)
3155 return bfd_reloc_notsupported;
68ffbac6 3156
5887528b
DA
3157 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3158 local_offsets = elf_local_got_offsets (input_bfd);
3159 insn = bfd_get_32 (input_bfd, hit_data);
3160
3161 switch (r_type)
3162 {
3163 case R_PARISC_NONE:
3164 break;
3165
3166 /* Basic function call support.
3167
3168 Note for a call to a function defined in another dynamic library
3169 we want to redirect the call to a stub. */
3170
3171 /* PC relative relocs without an implicit offset. */
3172 case R_PARISC_PCREL21L:
3173 case R_PARISC_PCREL14R:
3174 case R_PARISC_PCREL14F:
3175 case R_PARISC_PCREL14WR:
3176 case R_PARISC_PCREL14DR:
3177 case R_PARISC_PCREL16F:
3178 case R_PARISC_PCREL16WF:
3179 case R_PARISC_PCREL16DF:
3180 {
3181 /* If this is a call to a function defined in another dynamic
3182 library, then redirect the call to the local stub for this
3183 function. */
3184 if (sym_sec == NULL || sym_sec->output_section == NULL)
3185 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3186 + hppa_info->stub_sec->output_section->vma);
3187
3188 /* Turn VALUE into a proper PC relative address. */
3189 value -= (offset + input_section->output_offset
3190 + input_section->output_section->vma);
3191
3192 /* Adjust for any field selectors. */
3193 if (r_type == R_PARISC_PCREL21L)
3194 value = hppa_field_adjust (value, -8 + addend, e_lsel);
3195 else if (r_type == R_PARISC_PCREL14F
3196 || r_type == R_PARISC_PCREL16F
3197 || r_type == R_PARISC_PCREL16WF
3198 || r_type == R_PARISC_PCREL16DF)
3199 value = hppa_field_adjust (value, -8 + addend, e_fsel);
3200 else
3201 value = hppa_field_adjust (value, -8 + addend, e_rsel);
3202
3203 /* Apply the relocation to the given instruction. */
3204 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3205 break;
3206 }
3207
3208 case R_PARISC_PCREL12F:
3209 case R_PARISC_PCREL22F:
3210 case R_PARISC_PCREL17F:
3211 case R_PARISC_PCREL22C:
3212 case R_PARISC_PCREL17C:
3213 case R_PARISC_PCREL17R:
3214 {
3215 /* If this is a call to a function defined in another dynamic
3216 library, then redirect the call to the local stub for this
3217 function. */
3218 if (sym_sec == NULL || sym_sec->output_section == NULL)
3219 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3220 + hppa_info->stub_sec->output_section->vma);
3221
3222 /* Turn VALUE into a proper PC relative address. */
3223 value -= (offset + input_section->output_offset
3224 + input_section->output_section->vma);
3225 addend -= 8;
3226
3227 if (r_type == (unsigned int) R_PARISC_PCREL22F)
3228 max_branch_offset = (1 << (22-1)) << 2;
3229 else if (r_type == (unsigned int) R_PARISC_PCREL17F)
3230 max_branch_offset = (1 << (17-1)) << 2;
3231 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3232 max_branch_offset = (1 << (12-1)) << 2;
3233
3234 /* Make sure we can reach the branch target. */
3235 if (max_branch_offset != 0
3236 && value + addend + max_branch_offset >= 2*max_branch_offset)
3237 {
4eca0228 3238 _bfd_error_handler
695344c0 3239 /* xgettext:c-format */
2dcf00ce 3240 (_("%pB(%pA+%#" PRIx64 "): cannot reach %s"),
5887528b
DA
3241 input_bfd,
3242 input_section,
2dcf00ce 3243 (uint64_t) offset,
d91a6875 3244 eh ? eh->root.root.string : "unknown");
5887528b 3245 bfd_set_error (bfd_error_bad_value);
d91a6875 3246 return bfd_reloc_overflow;
5887528b
DA
3247 }
3248
3249 /* Adjust for any field selectors. */
3250 if (r_type == R_PARISC_PCREL17R)
3251 value = hppa_field_adjust (value, addend, e_rsel);
3252 else
3253 value = hppa_field_adjust (value, addend, e_fsel);
3254
3255 /* All branches are implicitly shifted by 2 places. */
3256 value >>= 2;
3257
3258 /* Apply the relocation to the given instruction. */
3259 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3260 break;
3261 }
3262
3263 /* Indirect references to data through the DLT. */
3264 case R_PARISC_DLTIND14R:
3265 case R_PARISC_DLTIND14F:
3266 case R_PARISC_DLTIND14DR:
3267 case R_PARISC_DLTIND14WR:
3268 case R_PARISC_DLTIND21L:
3269 case R_PARISC_LTOFF_FPTR14R:
3270 case R_PARISC_LTOFF_FPTR14DR:
3271 case R_PARISC_LTOFF_FPTR14WR:
3272 case R_PARISC_LTOFF_FPTR21L:
3273 case R_PARISC_LTOFF_FPTR16F:
3274 case R_PARISC_LTOFF_FPTR16WF:
3275 case R_PARISC_LTOFF_FPTR16DF:
3276 case R_PARISC_LTOFF_TP21L:
3277 case R_PARISC_LTOFF_TP14R:
3278 case R_PARISC_LTOFF_TP14F:
3279 case R_PARISC_LTOFF_TP14WR:
3280 case R_PARISC_LTOFF_TP14DR:
3281 case R_PARISC_LTOFF_TP16F:
3282 case R_PARISC_LTOFF_TP16WF:
3283 case R_PARISC_LTOFF_TP16DF:
3284 case R_PARISC_LTOFF16F:
3285 case R_PARISC_LTOFF16WF:
3286 case R_PARISC_LTOFF16DF:
3287 {
3288 bfd_vma off;
3289
3290 /* If this relocation was against a local symbol, then we still
3291 have not set up the DLT entry (it's not convenient to do so
3292 in the "finalize_dlt" routine because it is difficult to get
3293 to the local symbol's value).
3294
3295 So, if this is a local symbol (h == NULL), then we need to
3296 fill in its DLT entry.
3297
3298 Similarly we may still need to set up an entry in .opd for
3299 a local function which had its address taken. */
3300 if (hh == NULL)
3301 {
3302 bfd_vma *local_opd_offsets, *local_dlt_offsets;
3303
07d6d2b8
AM
3304 if (local_offsets == NULL)
3305 abort ();
5887528b
DA
3306
3307 /* Now do .opd creation if needed. */
3308 if (r_type == R_PARISC_LTOFF_FPTR14R
3309 || r_type == R_PARISC_LTOFF_FPTR14DR
3310 || r_type == R_PARISC_LTOFF_FPTR14WR
3311 || r_type == R_PARISC_LTOFF_FPTR21L
3312 || r_type == R_PARISC_LTOFF_FPTR16F
3313 || r_type == R_PARISC_LTOFF_FPTR16WF
3314 || r_type == R_PARISC_LTOFF_FPTR16DF)
3315 {
3316 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3317 off = local_opd_offsets[r_symndx];
3318
3319 /* The last bit records whether we've already initialised
3320 this local .opd entry. */
3321 if ((off & 1) != 0)
3322 {
3323 BFD_ASSERT (off != (bfd_vma) -1);
3324 off &= ~1;
3325 }
3326 else
3327 {
3328 local_opd_offsets[r_symndx] |= 1;
3329
3330 /* The first two words of an .opd entry are zero. */
3331 memset (hppa_info->opd_sec->contents + off, 0, 16);
3332
3333 /* The next word is the address of the function. */
3334 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3335 (hppa_info->opd_sec->contents + off + 16));
3336
3337 /* The last word is our local __gp value. */
586338b8 3338 value = _bfd_get_gp_value (info->output_bfd);
5887528b
DA
3339 bfd_put_64 (hppa_info->opd_sec->owner, value,
3340 (hppa_info->opd_sec->contents + off + 24));
3341 }
3342
3343 /* The DLT value is the address of the .opd entry. */
3344 value = (off
3345 + hppa_info->opd_sec->output_offset
3346 + hppa_info->opd_sec->output_section->vma);
3347 addend = 0;
3348 }
3349
3350 local_dlt_offsets = local_offsets;
3351 off = local_dlt_offsets[r_symndx];
3352
3353 if ((off & 1) != 0)
3354 {
3355 BFD_ASSERT (off != (bfd_vma) -1);
3356 off &= ~1;
3357 }
3358 else
3359 {
3360 local_dlt_offsets[r_symndx] |= 1;
3361 bfd_put_64 (hppa_info->dlt_sec->owner,
3362 value + addend,
3363 hppa_info->dlt_sec->contents + off);
3364 }
3365 }
3366 else
3367 off = hh->dlt_offset;
3368
3369 /* We want the value of the DLT offset for this symbol, not
3370 the symbol's actual address. Note that __gp may not point
3371 to the start of the DLT, so we have to compute the absolute
3372 address, then subtract out the value of __gp. */
3373 value = (off
3374 + hppa_info->dlt_sec->output_offset
3375 + hppa_info->dlt_sec->output_section->vma);
3376 value -= _bfd_get_gp_value (output_bfd);
3377
3378 /* All DLTIND relocations are basically the same at this point,
3379 except that we need different field selectors for the 21bit
3380 version vs the 14bit versions. */
3381 if (r_type == R_PARISC_DLTIND21L
3382 || r_type == R_PARISC_LTOFF_FPTR21L
3383 || r_type == R_PARISC_LTOFF_TP21L)
3384 value = hppa_field_adjust (value, 0, e_lsel);
3385 else if (r_type == R_PARISC_DLTIND14F
3386 || r_type == R_PARISC_LTOFF_FPTR16F
3387 || r_type == R_PARISC_LTOFF_FPTR16WF
3388 || r_type == R_PARISC_LTOFF_FPTR16DF
3389 || r_type == R_PARISC_LTOFF16F
3390 || r_type == R_PARISC_LTOFF16DF
3391 || r_type == R_PARISC_LTOFF16WF
3392 || r_type == R_PARISC_LTOFF_TP16F
3393 || r_type == R_PARISC_LTOFF_TP16WF
3394 || r_type == R_PARISC_LTOFF_TP16DF)
3395 value = hppa_field_adjust (value, 0, e_fsel);
3396 else
3397 value = hppa_field_adjust (value, 0, e_rsel);
3398
3399 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3400 break;
3401 }
3402
3403 case R_PARISC_DLTREL14R:
3404 case R_PARISC_DLTREL14F:
3405 case R_PARISC_DLTREL14DR:
3406 case R_PARISC_DLTREL14WR:
3407 case R_PARISC_DLTREL21L:
3408 case R_PARISC_DPREL21L:
3409 case R_PARISC_DPREL14WR:
3410 case R_PARISC_DPREL14DR:
3411 case R_PARISC_DPREL14R:
3412 case R_PARISC_DPREL14F:
3413 case R_PARISC_GPREL16F:
3414 case R_PARISC_GPREL16WF:
3415 case R_PARISC_GPREL16DF:
3416 {
3417 /* Subtract out the global pointer value to make value a DLT
3418 relative address. */
3419 value -= _bfd_get_gp_value (output_bfd);
3420
3421 /* All DLTREL relocations are basically the same at this point,
3422 except that we need different field selectors for the 21bit
3423 version vs the 14bit versions. */
3424 if (r_type == R_PARISC_DLTREL21L
3425 || r_type == R_PARISC_DPREL21L)
3426 value = hppa_field_adjust (value, addend, e_lrsel);
3427 else if (r_type == R_PARISC_DLTREL14F
3428 || r_type == R_PARISC_DPREL14F
3429 || r_type == R_PARISC_GPREL16F
3430 || r_type == R_PARISC_GPREL16WF
3431 || r_type == R_PARISC_GPREL16DF)
3432 value = hppa_field_adjust (value, addend, e_fsel);
3433 else
3434 value = hppa_field_adjust (value, addend, e_rrsel);
3435
3436 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3437 break;
3438 }
3439
3440 case R_PARISC_DIR21L:
3441 case R_PARISC_DIR17R:
3442 case R_PARISC_DIR17F:
3443 case R_PARISC_DIR14R:
3444 case R_PARISC_DIR14F:
3445 case R_PARISC_DIR14WR:
3446 case R_PARISC_DIR14DR:
3447 case R_PARISC_DIR16F:
3448 case R_PARISC_DIR16WF:
3449 case R_PARISC_DIR16DF:
3450 {
3451 /* All DIR relocations are basically the same at this point,
3452 except that branch offsets need to be divided by four, and
3453 we need different field selectors. Note that we don't
3454 redirect absolute calls to local stubs. */
3455
3456 if (r_type == R_PARISC_DIR21L)
3457 value = hppa_field_adjust (value, addend, e_lrsel);
3458 else if (r_type == R_PARISC_DIR17F
3459 || r_type == R_PARISC_DIR16F
3460 || r_type == R_PARISC_DIR16WF
3461 || r_type == R_PARISC_DIR16DF
3462 || r_type == R_PARISC_DIR14F)
3463 value = hppa_field_adjust (value, addend, e_fsel);
3464 else
3465 value = hppa_field_adjust (value, addend, e_rrsel);
3466
3467 if (r_type == R_PARISC_DIR17R || r_type == R_PARISC_DIR17F)
3468 /* All branches are implicitly shifted by 2 places. */
3469 value >>= 2;
3470
3471 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3472 break;
3473 }
3474
3475 case R_PARISC_PLTOFF21L:
3476 case R_PARISC_PLTOFF14R:
3477 case R_PARISC_PLTOFF14F:
3478 case R_PARISC_PLTOFF14WR:
3479 case R_PARISC_PLTOFF14DR:
3480 case R_PARISC_PLTOFF16F:
3481 case R_PARISC_PLTOFF16WF:
3482 case R_PARISC_PLTOFF16DF:
3483 {
3484 /* We want the value of the PLT offset for this symbol, not
3485 the symbol's actual address. Note that __gp may not point
3486 to the start of the DLT, so we have to compute the absolute
3487 address, then subtract out the value of __gp. */
3488 value = (hh->plt_offset
9b8a8575
L
3489 + hppa_info->root.splt->output_offset
3490 + hppa_info->root.splt->output_section->vma);
5887528b
DA
3491 value -= _bfd_get_gp_value (output_bfd);
3492
3493 /* All PLTOFF relocations are basically the same at this point,
3494 except that we need different field selectors for the 21bit
3495 version vs the 14bit versions. */
3496 if (r_type == R_PARISC_PLTOFF21L)
3497 value = hppa_field_adjust (value, addend, e_lrsel);
3498 else if (r_type == R_PARISC_PLTOFF14F
3499 || r_type == R_PARISC_PLTOFF16F
3500 || r_type == R_PARISC_PLTOFF16WF
3501 || r_type == R_PARISC_PLTOFF16DF)
3502 value = hppa_field_adjust (value, addend, e_fsel);
3503 else
3504 value = hppa_field_adjust (value, addend, e_rrsel);
3505
3506 insn = elf_hppa_relocate_insn (insn, (int) value, r_type);
3507 break;
3508 }
3509
3510 case R_PARISC_LTOFF_FPTR32:
3511 {
38cf70ca
NC
3512 /* FIXME: There used to be code here to create the FPTR itself if
3513 the relocation was against a local symbol. But the code could
3514 never have worked. If the assert below is ever triggered then
3515 the code will need to be reinstated and fixed so that it does
3516 what is needed. */
3517 BFD_ASSERT (hh != NULL);
5887528b
DA
3518
3519 /* We want the value of the DLT offset for this symbol, not
3520 the symbol's actual address. Note that __gp may not point
3521 to the start of the DLT, so we have to compute the absolute
3522 address, then subtract out the value of __gp. */
3523 value = (hh->dlt_offset
3524 + hppa_info->dlt_sec->output_offset
3525 + hppa_info->dlt_sec->output_section->vma);
3526 value -= _bfd_get_gp_value (output_bfd);
3527 bfd_put_32 (input_bfd, value, hit_data);
3528 return bfd_reloc_ok;
3529 }
3530
3531 case R_PARISC_LTOFF_FPTR64:
3532 case R_PARISC_LTOFF_TP64:
3533 {
3534 /* We may still need to create the FPTR itself if it was for
3535 a local symbol. */
3536 if (eh == NULL && r_type == R_PARISC_LTOFF_FPTR64)
3537 {
3538 /* The first two words of an .opd entry are zero. */
3539 memset (hppa_info->opd_sec->contents + hh->opd_offset, 0, 16);
3540
3541 /* The next word is the address of the function. */
3542 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3543 (hppa_info->opd_sec->contents
3544 + hh->opd_offset + 16));
3545
3546 /* The last word is our local __gp value. */
586338b8 3547 value = _bfd_get_gp_value (info->output_bfd);
5887528b
DA
3548 bfd_put_64 (hppa_info->opd_sec->owner, value,
3549 hppa_info->opd_sec->contents + hh->opd_offset + 24);
3550
3551 /* The DLT value is the address of the .opd entry. */
3552 value = (hh->opd_offset
3553 + hppa_info->opd_sec->output_offset
3554 + hppa_info->opd_sec->output_section->vma);
3555
3556 bfd_put_64 (hppa_info->dlt_sec->owner,
3557 value,
3558 hppa_info->dlt_sec->contents + hh->dlt_offset);
3559 }
3560
3561 /* We want the value of the DLT offset for this symbol, not
3562 the symbol's actual address. Note that __gp may not point
3563 to the start of the DLT, so we have to compute the absolute
3564 address, then subtract out the value of __gp. */
3565 value = (hh->dlt_offset
3566 + hppa_info->dlt_sec->output_offset
3567 + hppa_info->dlt_sec->output_section->vma);
3568 value -= _bfd_get_gp_value (output_bfd);
3569 bfd_put_64 (input_bfd, value, hit_data);
3570 return bfd_reloc_ok;
3571 }
3572
3573 case R_PARISC_DIR32:
3574 bfd_put_32 (input_bfd, value + addend, hit_data);
3575 return bfd_reloc_ok;
3576
3577 case R_PARISC_DIR64:
3578 bfd_put_64 (input_bfd, value + addend, hit_data);
3579 return bfd_reloc_ok;
3580
3581 case R_PARISC_GPREL64:
3582 /* Subtract out the global pointer value to make value a DLT
3583 relative address. */
3584 value -= _bfd_get_gp_value (output_bfd);
3585
3586 bfd_put_64 (input_bfd, value + addend, hit_data);
3587 return bfd_reloc_ok;
3588
3589 case R_PARISC_LTOFF64:
3590 /* We want the value of the DLT offset for this symbol, not
3591 the symbol's actual address. Note that __gp may not point
3592 to the start of the DLT, so we have to compute the absolute
3593 address, then subtract out the value of __gp. */
3594 value = (hh->dlt_offset
3595 + hppa_info->dlt_sec->output_offset
3596 + hppa_info->dlt_sec->output_section->vma);
3597 value -= _bfd_get_gp_value (output_bfd);
3598
3599 bfd_put_64 (input_bfd, value + addend, hit_data);
3600 return bfd_reloc_ok;
3601
3602 case R_PARISC_PCREL32:
3603 {
3604 /* If this is a call to a function defined in another dynamic
3605 library, then redirect the call to the local stub for this
3606 function. */
3607 if (sym_sec == NULL || sym_sec->output_section == NULL)
3608 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3609 + hppa_info->stub_sec->output_section->vma);
3610
3611 /* Turn VALUE into a proper PC relative address. */
3612 value -= (offset + input_section->output_offset
3613 + input_section->output_section->vma);
3614
3615 value += addend;
3616 value -= 8;
3617 bfd_put_32 (input_bfd, value, hit_data);
3618 return bfd_reloc_ok;
3619 }
3620
3621 case R_PARISC_PCREL64:
3622 {
3623 /* If this is a call to a function defined in another dynamic
3624 library, then redirect the call to the local stub for this
3625 function. */
3626 if (sym_sec == NULL || sym_sec->output_section == NULL)
3627 value = (hh->stub_offset + hppa_info->stub_sec->output_offset
3628 + hppa_info->stub_sec->output_section->vma);
3629
3630 /* Turn VALUE into a proper PC relative address. */
3631 value -= (offset + input_section->output_offset
3632 + input_section->output_section->vma);
3633
3634 value += addend;
3635 value -= 8;
3636 bfd_put_64 (input_bfd, value, hit_data);
3637 return bfd_reloc_ok;
3638 }
3639
3640 case R_PARISC_FPTR64:
3641 {
3642 bfd_vma off;
3643
3644 /* We may still need to create the FPTR itself if it was for
3645 a local symbol. */
3646 if (hh == NULL)
3647 {
3648 bfd_vma *local_opd_offsets;
3649
07d6d2b8
AM
3650 if (local_offsets == NULL)
3651 abort ();
5887528b
DA
3652
3653 local_opd_offsets = local_offsets + 2 * symtab_hdr->sh_info;
3654 off = local_opd_offsets[r_symndx];
3655
3656 /* The last bit records whether we've already initialised
3657 this local .opd entry. */
3658 if ((off & 1) != 0)
3659 {
3660 BFD_ASSERT (off != (bfd_vma) -1);
07d6d2b8 3661 off &= ~1;
5887528b
DA
3662 }
3663 else
3664 {
3665 /* The first two words of an .opd entry are zero. */
3666 memset (hppa_info->opd_sec->contents + off, 0, 16);
3667
3668 /* The next word is the address of the function. */
3669 bfd_put_64 (hppa_info->opd_sec->owner, value + addend,
3670 (hppa_info->opd_sec->contents + off + 16));
3671
3672 /* The last word is our local __gp value. */
586338b8 3673 value = _bfd_get_gp_value (info->output_bfd);
5887528b
DA
3674 bfd_put_64 (hppa_info->opd_sec->owner, value,
3675 hppa_info->opd_sec->contents + off + 24);
3676 }
3677 }
3678 else
3679 off = hh->opd_offset;
3680
3681 if (hh == NULL || hh->want_opd)
3682 /* We want the value of the OPD offset for this symbol. */
3683 value = (off
3684 + hppa_info->opd_sec->output_offset
3685 + hppa_info->opd_sec->output_section->vma);
3686 else
3687 /* We want the address of the symbol. */
3688 value += addend;
3689
3690 bfd_put_64 (input_bfd, value, hit_data);
3691 return bfd_reloc_ok;
3692 }
3693
3694 case R_PARISC_SECREL32:
27e5e547 3695 if (sym_sec && sym_sec->output_section)
5887528b
DA
3696 value -= sym_sec->output_section->vma;
3697 bfd_put_32 (input_bfd, value + addend, hit_data);
3698 return bfd_reloc_ok;
3699
3700 case R_PARISC_SEGREL32:
3701 case R_PARISC_SEGREL64:
3702 {
3703 /* If this is the first SEGREL relocation, then initialize
3704 the segment base values. */
3705 if (hppa_info->text_segment_base == (bfd_vma) -1)
3706 bfd_map_over_sections (output_bfd, elf_hppa_record_segment_addrs,
3707 hppa_info);
3708
3709 /* VALUE holds the absolute address. We want to include the
3710 addend, then turn it into a segment relative address.
3711
3712 The segment is derived from SYM_SEC. We assume that there are
3713 only two segments of note in the resulting executable/shlib.
3714 A readonly segment (.text) and a readwrite segment (.data). */
3715 value += addend;
3716
3717 if (sym_sec->flags & SEC_CODE)
3718 value -= hppa_info->text_segment_base;
3719 else
3720 value -= hppa_info->data_segment_base;
3721
3722 if (r_type == R_PARISC_SEGREL32)
3723 bfd_put_32 (input_bfd, value, hit_data);
3724 else
3725 bfd_put_64 (input_bfd, value, hit_data);
3726 return bfd_reloc_ok;
3727 }
3728
3729 /* Something we don't know how to handle. */
3730 default:
3731 return bfd_reloc_notsupported;
3732 }
3733
3734 /* Update the instruction word. */
3735 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3736 return bfd_reloc_ok;
3737}
3738
3739/* Relocate an HPPA ELF section. */
3740
0f684201 3741static int
5887528b
DA
3742elf64_hppa_relocate_section (bfd *output_bfd,
3743 struct bfd_link_info *info,
3744 bfd *input_bfd,
3745 asection *input_section,
3746 bfd_byte *contents,
3747 Elf_Internal_Rela *relocs,
3748 Elf_Internal_Sym *local_syms,
3749 asection **local_sections)
3750{
3751 Elf_Internal_Shdr *symtab_hdr;
3752 Elf_Internal_Rela *rel;
3753 Elf_Internal_Rela *relend;
3754 struct elf64_hppa_link_hash_table *hppa_info;
3755
3756 hppa_info = hppa_link_hash_table (info);
4dfe6ac6 3757 if (hppa_info == NULL)
0a1b45a2 3758 return false;
4dfe6ac6 3759
5887528b
DA
3760 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3761
3762 rel = relocs;
3763 relend = relocs + input_section->reloc_count;
3764 for (; rel < relend; rel++)
3765 {
3766 int r_type;
3767 reloc_howto_type *howto = elf_hppa_howto_table + ELF_R_TYPE (rel->r_info);
3768 unsigned long r_symndx;
3769 struct elf_link_hash_entry *eh;
3770 Elf_Internal_Sym *sym;
3771 asection *sym_sec;
3772 bfd_vma relocation;
3773 bfd_reloc_status_type r;
5887528b
DA
3774
3775 r_type = ELF_R_TYPE (rel->r_info);
3776 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
3777 {
3778 bfd_set_error (bfd_error_bad_value);
0a1b45a2 3779 return false;
5887528b
DA
3780 }
3781 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3782 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3783 continue;
3784
3785 /* This is a final link. */
3786 r_symndx = ELF_R_SYM (rel->r_info);
3787 eh = NULL;
3788 sym = NULL;
3789 sym_sec = NULL;
5887528b
DA
3790 if (r_symndx < symtab_hdr->sh_info)
3791 {
3792 /* This is a local symbol, hh defaults to NULL. */
3793 sym = local_syms + r_symndx;
3794 sym_sec = local_sections[r_symndx];
3795 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rel);
3796 }
3797 else
3798 {
3799 /* This is not a local symbol. */
5887528b
DA
3800 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3801
68ffbac6 3802 /* It seems this can happen with erroneous or unsupported
5887528b
DA
3803 input (mixing a.out and elf in an archive, for example.) */
3804 if (sym_hashes == NULL)
0a1b45a2 3805 return false;
5887528b
DA
3806
3807 eh = sym_hashes[r_symndx - symtab_hdr->sh_info];
3808
8a5da09b
AM
3809 if (info->wrap_hash != NULL
3810 && (input_section->flags & SEC_DEBUGGING) != 0)
3811 eh = ((struct elf_link_hash_entry *)
3812 unwrap_hash_lookup (info, input_bfd, &eh->root));
3813
68ffbac6 3814 while (eh->root.type == bfd_link_hash_indirect
5887528b
DA
3815 || eh->root.type == bfd_link_hash_warning)
3816 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
3817
5887528b
DA
3818 relocation = 0;
3819 if (eh->root.type == bfd_link_hash_defined
3820 || eh->root.type == bfd_link_hash_defweak)
3821 {
3822 sym_sec = eh->root.u.def.section;
c7e2358a
AM
3823 if (sym_sec != NULL
3824 && sym_sec->output_section != NULL)
5887528b
DA
3825 relocation = (eh->root.u.def.value
3826 + sym_sec->output_section->vma
3827 + sym_sec->output_offset);
3828 }
3829 else if (eh->root.type == bfd_link_hash_undefweak)
3830 ;
3831 else if (info->unresolved_syms_in_objects == RM_IGNORE
3832 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT)
3833 ;
0e1862bb 3834 else if (!bfd_link_relocatable (info)
5887528b
DA
3835 && elf_hppa_is_dynamic_loader_symbol (eh->root.root.string))
3836 continue;
0e1862bb 3837 else if (!bfd_link_relocatable (info))
5887528b 3838 {
0a1b45a2 3839 bool err;
95a51568
FS
3840
3841 err = (info->unresolved_syms_in_objects == RM_DIAGNOSE
3842 && !info->warn_unresolved_syms)
3843 || ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT;
3844
3845 info->callbacks->undefined_symbol
3846 (info, eh->root.root.string, input_bfd,
3847 input_section, rel->r_offset, err);
5887528b
DA
3848 }
3849
07d6d2b8
AM
3850 if (!bfd_link_relocatable (info)
3851 && relocation == 0
3852 && eh->root.type != bfd_link_hash_defined
3853 && eh->root.type != bfd_link_hash_defweak
3854 && eh->root.type != bfd_link_hash_undefweak)
3855 {
3856 if (info->unresolved_syms_in_objects == RM_IGNORE
3857 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3858 && eh->type == STT_PARISC_MILLI)
95a51568 3859 info->callbacks->undefined_symbol
1a72702b 3860 (info, eh_name (eh), input_bfd,
0a1b45a2 3861 input_section, rel->r_offset, false);
07d6d2b8 3862 }
5887528b
DA
3863 }
3864
dbaa2011 3865 if (sym_sec != NULL && discarded_section (sym_sec))
e4067dbb 3866 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 3867 rel, 1, relend, howto, 0, contents);
5887528b 3868
0e1862bb 3869 if (bfd_link_relocatable (info))
5887528b
DA
3870 continue;
3871
3872 r = elf_hppa_final_link_relocate (rel, input_bfd, output_bfd,
3873 input_section, contents,
3874 relocation, info, sym_sec,
3875 eh);
3876
3877 if (r != bfd_reloc_ok)
3878 {
3879 switch (r)
3880 {
3881 default:
3882 abort ();
3883 case bfd_reloc_overflow:
3884 {
3885 const char *sym_name;
3886
3887 if (eh != NULL)
3888 sym_name = NULL;
3889 else
3890 {
3891 sym_name = bfd_elf_string_from_elf_section (input_bfd,
3892 symtab_hdr->sh_link,
3893 sym->st_name);
3894 if (sym_name == NULL)
0a1b45a2 3895 return false;
5887528b 3896 if (*sym_name == '\0')
fd361982 3897 sym_name = bfd_section_name (sym_sec);
5887528b
DA
3898 }
3899
1a72702b
AM
3900 (*info->callbacks->reloc_overflow)
3901 (info, (eh ? &eh->root : NULL), sym_name, howto->name,
3902 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5887528b
DA
3903 }
3904 break;
3905 }
3906 }
3907 }
0a1b45a2 3908 return true;
5887528b
DA
3909}
3910
b35d266b 3911static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
2f89ff8d 3912{
df3a023b 3913 { STRING_COMMA_LEN (".tbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
07d6d2b8
AM
3914 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3915 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3916 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
3917 { STRING_COMMA_LEN (".dlt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
0112cd26 3918 { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
07d6d2b8 3919 { STRING_COMMA_LEN (".sbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
07d6d2b8 3920 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
3921};
3922
15bda425
JL
3923/* The hash bucket size is the standard one, namely 4. */
3924
3925const struct elf_size_info hppa64_elf_size_info =
3926{
3927 sizeof (Elf64_External_Ehdr),
3928 sizeof (Elf64_External_Phdr),
3929 sizeof (Elf64_External_Shdr),
3930 sizeof (Elf64_External_Rel),
3931 sizeof (Elf64_External_Rela),
3932 sizeof (Elf64_External_Sym),
3933 sizeof (Elf64_External_Dyn),
3934 sizeof (Elf_External_Note),
3935 4,
3936 1,
45d6a902 3937 64, 3,
15bda425
JL
3938 ELFCLASS64, EV_CURRENT,
3939 bfd_elf64_write_out_phdrs,
3940 bfd_elf64_write_shdrs_and_ehdr,
1489a3a0 3941 bfd_elf64_checksum_contents,
15bda425 3942 bfd_elf64_write_relocs,
73ff0d56 3943 bfd_elf64_swap_symbol_in,
15bda425
JL
3944 bfd_elf64_swap_symbol_out,
3945 bfd_elf64_slurp_reloc_table,
3946 bfd_elf64_slurp_symbol_table,
3947 bfd_elf64_swap_dyn_in,
3948 bfd_elf64_swap_dyn_out,
947216bf
AM
3949 bfd_elf64_swap_reloc_in,
3950 bfd_elf64_swap_reloc_out,
3951 bfd_elf64_swap_reloca_in,
3952 bfd_elf64_swap_reloca_out
15bda425
JL
3953};
3954
6d00b590 3955#define TARGET_BIG_SYM hppa_elf64_vec
15bda425
JL
3956#define TARGET_BIG_NAME "elf64-hppa"
3957#define ELF_ARCH bfd_arch_hppa
ae95ffa6 3958#define ELF_TARGET_ID HPPA64_ELF_DATA
15bda425
JL
3959#define ELF_MACHINE_CODE EM_PARISC
3960/* This is not strictly correct. The maximum page size for PA2.0 is
3961 64M. But everything still uses 4k. */
3962#define ELF_MAXPAGESIZE 0x1000
d1036acb
L
3963#define ELF_OSABI ELFOSABI_HPUX
3964
15bda425 3965#define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
157090f7 3966#define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
15bda425
JL
3967#define bfd_elf64_bfd_is_local_label_name elf_hppa_is_local_label_name
3968#define elf_info_to_howto elf_hppa_info_to_howto
3969#define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
3970
3971#define elf_backend_section_from_shdr elf64_hppa_section_from_shdr
3972#define elf_backend_object_p elf64_hppa_object_p
3973#define elf_backend_final_write_processing \
3974 elf_hppa_final_write_processing
99c79b2e 3975#define elf_backend_fake_sections elf_hppa_fake_sections
15bda425
JL
3976#define elf_backend_add_symbol_hook elf_hppa_add_symbol_hook
3977
f0fe0e16 3978#define elf_backend_relocate_section elf_hppa_relocate_section
15bda425
JL
3979
3980#define bfd_elf64_bfd_final_link elf_hppa_final_link
3981
3982#define elf_backend_create_dynamic_sections \
3983 elf64_hppa_create_dynamic_sections
ed7e9d0b 3984#define elf_backend_init_file_header elf64_hppa_init_file_header
15bda425 3985
d00dd7dc
AM
3986#define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
3987
15bda425
JL
3988#define elf_backend_adjust_dynamic_symbol \
3989 elf64_hppa_adjust_dynamic_symbol
3990
af969b14 3991#define elf_backend_late_size_sections elf64_hppa_late_size_sections
15bda425
JL
3992
3993#define elf_backend_finish_dynamic_symbol \
3994 elf64_hppa_finish_dynamic_symbol
3995#define elf_backend_finish_dynamic_sections \
3996 elf64_hppa_finish_dynamic_sections
235ecfbc
NC
3997#define elf_backend_grok_prstatus elf64_hppa_grok_prstatus
3998#define elf_backend_grok_psinfo elf64_hppa_grok_psinfo
68ffbac6 3999
15bda425
JL
4000/* Stuff for the BFD linker: */
4001#define bfd_elf64_bfd_link_hash_table_create \
4002 elf64_hppa_hash_table_create
4003
4004#define elf_backend_check_relocs \
4005 elf64_hppa_check_relocs
4006
4007#define elf_backend_size_info \
4008 hppa64_elf_size_info
4009
4010#define elf_backend_additional_program_headers \
4011 elf64_hppa_additional_program_headers
4012
4013#define elf_backend_modify_segment_map \
4014 elf64_hppa_modify_segment_map
4015
1a9ccd70
NC
4016#define elf_backend_allow_non_load_phdr \
4017 elf64_hppa_allow_non_load_phdr
4018
15bda425
JL
4019#define elf_backend_link_output_symbol_hook \
4020 elf64_hppa_link_output_symbol_hook
4021
15bda425
JL
4022#define elf_backend_want_got_plt 0
4023#define elf_backend_plt_readonly 0
4024#define elf_backend_want_plt_sym 0
4025#define elf_backend_got_header_size 0
0a1b45a2 4026#define elf_backend_type_change_ok true
b34976b6
AM
4027#define elf_backend_get_symbol_type elf64_hppa_elf_get_symbol_type
4028#define elf_backend_reloc_type_class elf64_hppa_reloc_type_class
4029#define elf_backend_rela_normal 1
29ef7005 4030#define elf_backend_special_sections elf64_hppa_special_sections
8a696751 4031#define elf_backend_action_discarded elf_hppa_action_discarded
d97a8924 4032#define elf_backend_section_from_phdr elf64_hppa_section_from_phdr
15bda425 4033
83d1651b
L
4034#define elf64_bed elf64_hppa_hpux_bed
4035
15bda425 4036#include "elf64-target.h"
d952f17a
AM
4037
4038#undef TARGET_BIG_SYM
6d00b590 4039#define TARGET_BIG_SYM hppa_elf64_linux_vec
d952f17a
AM
4040#undef TARGET_BIG_NAME
4041#define TARGET_BIG_NAME "elf64-hppa-linux"
d1036acb 4042#undef ELF_OSABI
9c55345c 4043#define ELF_OSABI ELFOSABI_GNU
83d1651b
L
4044#undef elf64_bed
4045#define elf64_bed elf64_hppa_linux_bed
df3a023b
AM
4046#undef elf_backend_special_sections
4047#define elf_backend_special_sections (elf64_hppa_special_sections + 1)
d952f17a 4048
d952f17a 4049#include "elf64-target.h"