]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-hppa.c
bfd/
[thirdparty/binutils-gdb.git] / bfd / elf64-hppa.c
CommitLineData
b352eebf 1/* Support for HPPA 64-bit ELF
5a580b3a
AM
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
15bda425 4
ae9a127f 5 This file is part of BFD, the Binary File Descriptor library.
15bda425 6
ae9a127f
NC
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
15bda425 11
ae9a127f
NC
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
15bda425 16
ae9a127f
NC
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
15bda425 20
3ef20aaa 21#include "alloca-conf.h"
15bda425
JL
22#include "bfd.h"
23#include "sysdep.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/hppa.h"
27#include "libhppa.h"
28#include "elf64-hppa.h"
29#define ARCH_SIZE 64
30
31#define PLT_ENTRY_SIZE 0x10
32#define DLT_ENTRY_SIZE 0x8
33#define OPD_ENTRY_SIZE 0x20
fe8bc63d 34
15bda425
JL
35#define ELF_DYNAMIC_INTERPRETER "/usr/lib/pa20_64/dld.sl"
36
37/* The stub is supposed to load the target address and target's DP
38 value out of the PLT, then do an external branch to the target
39 address.
40
41 LDD PLTOFF(%r27),%r1
42 BVE (%r1)
43 LDD PLTOFF+8(%r27),%r27
44
45 Note that we must use the LDD with a 14 bit displacement, not the one
46 with a 5 bit displacement. */
47static char plt_stub[] = {0x53, 0x61, 0x00, 0x00, 0xe8, 0x20, 0xd0, 0x00,
48 0x53, 0x7b, 0x00, 0x00 };
49
50struct elf64_hppa_dyn_hash_entry
51{
52 struct bfd_hash_entry root;
53
54 /* Offsets for this symbol in various linker sections. */
55 bfd_vma dlt_offset;
56 bfd_vma plt_offset;
57 bfd_vma opd_offset;
58 bfd_vma stub_offset;
59
edd21aca 60 /* The symbol table entry, if any, that this was derived from. */
15bda425
JL
61 struct elf_link_hash_entry *h;
62
63 /* The index of the (possibly local) symbol in the input bfd and its
64 associated BFD. Needed so that we can have relocs against local
65 symbols in shared libraries. */
dc810e39 66 long sym_indx;
15bda425
JL
67 bfd *owner;
68
69 /* Dynamic symbols may need to have two different values. One for
70 the dynamic symbol table, one for the normal symbol table.
71
72 In such cases we store the symbol's real value and section
73 index here so we can restore the real value before we write
74 the normal symbol table. */
75 bfd_vma st_value;
76 int st_shndx;
77
78 /* Used to count non-got, non-plt relocations for delayed sizing
79 of relocation sections. */
80 struct elf64_hppa_dyn_reloc_entry
81 {
82 /* Next relocation in the chain. */
83 struct elf64_hppa_dyn_reloc_entry *next;
84
85 /* The type of the relocation. */
86 int type;
87
88 /* The input section of the relocation. */
89 asection *sec;
90
91 /* The index of the section symbol for the input section of
92 the relocation. Only needed when building shared libraries. */
93 int sec_symndx;
94
95 /* The offset within the input section of the relocation. */
96 bfd_vma offset;
97
98 /* The addend for the relocation. */
99 bfd_vma addend;
100
101 } *reloc_entries;
102
103 /* Nonzero if this symbol needs an entry in one of the linker
104 sections. */
105 unsigned want_dlt;
106 unsigned want_plt;
107 unsigned want_opd;
108 unsigned want_stub;
109};
110
111struct elf64_hppa_dyn_hash_table
112{
113 struct bfd_hash_table root;
114};
115
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;
123 asection *plt_sec;
124 asection *plt_rel_sec;
125 asection *opd_sec;
126 asection *opd_rel_sec;
127 asection *other_rel_sec;
128
129 /* Offset of __gp within .plt section. When the PLT gets large we want
130 to slide __gp into the PLT section so that we can continue to use
131 single DP relative instructions to load values out of the PLT. */
132 bfd_vma gp_offset;
133
134 /* Note this is not strictly correct. We should create a stub section for
135 each input section with calls. The stub section should be placed before
136 the section with the call. */
137 asection *stub_sec;
138
139 bfd_vma text_segment_base;
140 bfd_vma data_segment_base;
141
142 struct elf64_hppa_dyn_hash_table dyn_hash_table;
143
144 /* We build tables to map from an input section back to its
145 symbol index. This is the BFD for which we currently have
146 a map. */
147 bfd *section_syms_bfd;
148
149 /* Array of symbol numbers for each input section attached to the
150 current BFD. */
151 int *section_syms;
152};
153
154#define elf64_hppa_hash_table(p) \
155 ((struct elf64_hppa_link_hash_table *) ((p)->hash))
156
157typedef struct bfd_hash_entry *(*new_hash_entry_func)
158 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
159
b34976b6 160static bfd_boolean elf64_hppa_dyn_hash_table_init
15bda425
JL
161 PARAMS ((struct elf64_hppa_dyn_hash_table *ht, bfd *abfd,
162 new_hash_entry_func new));
163static struct bfd_hash_entry *elf64_hppa_new_dyn_hash_entry
164 PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
165 const char *string));
166static struct bfd_link_hash_table *elf64_hppa_hash_table_create
167 PARAMS ((bfd *abfd));
168static struct elf64_hppa_dyn_hash_entry *elf64_hppa_dyn_hash_lookup
169 PARAMS ((struct elf64_hppa_dyn_hash_table *table, const char *string,
b34976b6 170 bfd_boolean create, bfd_boolean copy));
15bda425
JL
171static void elf64_hppa_dyn_hash_traverse
172 PARAMS ((struct elf64_hppa_dyn_hash_table *table,
b34976b6 173 bfd_boolean (*func) (struct elf64_hppa_dyn_hash_entry *, PTR),
15bda425
JL
174 PTR info));
175
176static const char *get_dyn_name
d63b5ed9 177 PARAMS ((bfd *, struct elf_link_hash_entry *,
0ba2a60e 178 const Elf_Internal_Rela *, char **, size_t *));
15bda425 179
15bda425
JL
180/* This must follow the definitions of the various derived linker
181 hash tables and shared functions. */
182#include "elf-hppa.h"
183
b34976b6 184static bfd_boolean elf64_hppa_object_p
15bda425
JL
185 PARAMS ((bfd *));
186
b34976b6 187static bfd_boolean elf64_hppa_section_from_shdr
947216bf 188 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
15bda425
JL
189
190static void elf64_hppa_post_process_headers
191 PARAMS ((bfd *, struct bfd_link_info *));
192
b34976b6 193static bfd_boolean elf64_hppa_create_dynamic_sections
15bda425
JL
194 PARAMS ((bfd *, struct bfd_link_info *));
195
b34976b6 196static bfd_boolean elf64_hppa_adjust_dynamic_symbol
15bda425
JL
197 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
198
b34976b6 199static bfd_boolean elf64_hppa_mark_milli_and_exported_functions
47b7c2db
AM
200 PARAMS ((struct elf_link_hash_entry *, PTR));
201
b34976b6 202static bfd_boolean elf64_hppa_size_dynamic_sections
15bda425
JL
203 PARAMS ((bfd *, struct bfd_link_info *));
204
b34976b6 205static bfd_boolean elf64_hppa_link_output_symbol_hook
754021d0
AM
206 PARAMS ((struct bfd_link_info *, const char *, Elf_Internal_Sym *,
207 asection *, struct elf_link_hash_entry *));
99c79b2e 208
b34976b6 209static bfd_boolean elf64_hppa_finish_dynamic_symbol
15bda425
JL
210 PARAMS ((bfd *, struct bfd_link_info *,
211 struct elf_link_hash_entry *, Elf_Internal_Sym *));
fe8bc63d 212
b34976b6
AM
213static int elf64_hppa_additional_program_headers
214 PARAMS ((bfd *));
99c79b2e 215
b34976b6 216static bfd_boolean elf64_hppa_modify_segment_map
c84fca4d 217 PARAMS ((bfd *, struct bfd_link_info *));
99c79b2e 218
5ac81c74
JL
219static enum elf_reloc_type_class elf64_hppa_reloc_type_class
220 PARAMS ((const Elf_Internal_Rela *));
221
b34976b6 222static bfd_boolean elf64_hppa_finish_dynamic_sections
15bda425
JL
223 PARAMS ((bfd *, struct bfd_link_info *));
224
b34976b6 225static bfd_boolean elf64_hppa_check_relocs
15bda425
JL
226 PARAMS ((bfd *, struct bfd_link_info *,
227 asection *, const Elf_Internal_Rela *));
228
b34976b6 229static bfd_boolean elf64_hppa_dynamic_symbol_p
15bda425
JL
230 PARAMS ((struct elf_link_hash_entry *, struct bfd_link_info *));
231
b34976b6 232static bfd_boolean elf64_hppa_mark_exported_functions
15bda425
JL
233 PARAMS ((struct elf_link_hash_entry *, PTR));
234
b34976b6 235static bfd_boolean elf64_hppa_finalize_opd
15bda425
JL
236 PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
237
b34976b6 238static bfd_boolean elf64_hppa_finalize_dlt
15bda425
JL
239 PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
240
b34976b6 241static bfd_boolean allocate_global_data_dlt
15bda425
JL
242 PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
243
b34976b6 244static bfd_boolean allocate_global_data_plt
15bda425
JL
245 PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
246
b34976b6 247static bfd_boolean allocate_global_data_stub
15bda425
JL
248 PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
249
b34976b6 250static bfd_boolean allocate_global_data_opd
15bda425
JL
251 PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
252
b34976b6 253static bfd_boolean get_reloc_section
15bda425
JL
254 PARAMS ((bfd *, struct elf64_hppa_link_hash_table *, asection *));
255
b34976b6 256static bfd_boolean count_dyn_reloc
15bda425
JL
257 PARAMS ((bfd *, struct elf64_hppa_dyn_hash_entry *,
258 int, asection *, int, bfd_vma, bfd_vma));
259
b34976b6 260static bfd_boolean allocate_dynrel_entries
15bda425
JL
261 PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
262
b34976b6 263static bfd_boolean elf64_hppa_finalize_dynreloc
15bda425
JL
264 PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
265
b34976b6 266static bfd_boolean get_opd
15bda425
JL
267 PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
268
b34976b6 269static bfd_boolean get_plt
15bda425
JL
270 PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
271
b34976b6 272static bfd_boolean get_dlt
15bda425
JL
273 PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
274
b34976b6 275static bfd_boolean get_stub
15bda425
JL
276 PARAMS ((bfd *, struct bfd_link_info *, struct elf64_hppa_link_hash_table *));
277
3fab46d0
AM
278static int elf64_hppa_elf_get_symbol_type
279 PARAMS ((Elf_Internal_Sym *, int));
280
b34976b6 281static bfd_boolean
15bda425
JL
282elf64_hppa_dyn_hash_table_init (ht, abfd, new)
283 struct elf64_hppa_dyn_hash_table *ht;
edd21aca 284 bfd *abfd ATTRIBUTE_UNUSED;
15bda425
JL
285 new_hash_entry_func new;
286{
fe8bc63d 287 memset (ht, 0, sizeof (*ht));
15bda425
JL
288 return bfd_hash_table_init (&ht->root, new);
289}
290
291static struct bfd_hash_entry*
292elf64_hppa_new_dyn_hash_entry (entry, table, string)
293 struct bfd_hash_entry *entry;
294 struct bfd_hash_table *table;
295 const char *string;
296{
297 struct elf64_hppa_dyn_hash_entry *ret;
298 ret = (struct elf64_hppa_dyn_hash_entry *) entry;
299
300 /* Allocate the structure if it has not already been allocated by a
301 subclass. */
302 if (!ret)
303 ret = bfd_hash_allocate (table, sizeof (*ret));
304
305 if (!ret)
306 return 0;
307
308 /* Initialize our local data. All zeros, and definitely easier
309 than setting 8 bit fields. */
fe8bc63d 310 memset (ret, 0, sizeof (*ret));
15bda425
JL
311
312 /* Call the allocation method of the superclass. */
313 ret = ((struct elf64_hppa_dyn_hash_entry *)
314 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
315
316 return &ret->root;
317}
318
319/* Create the derived linker hash table. The PA64 ELF port uses this
320 derived hash table to keep information specific to the PA ElF
321 linker (without using static variables). */
322
323static struct bfd_link_hash_table*
324elf64_hppa_hash_table_create (abfd)
325 bfd *abfd;
326{
327 struct elf64_hppa_link_hash_table *ret;
328
dc810e39 329 ret = bfd_zalloc (abfd, (bfd_size_type) sizeof (*ret));
15bda425
JL
330 if (!ret)
331 return 0;
332 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
333 _bfd_elf_link_hash_newfunc))
334 {
335 bfd_release (abfd, ret);
336 return 0;
337 }
338
339 if (!elf64_hppa_dyn_hash_table_init (&ret->dyn_hash_table, abfd,
340 elf64_hppa_new_dyn_hash_entry))
341 return 0;
342 return &ret->root.root;
343}
344
345/* Look up an entry in a PA64 ELF linker hash table. */
346
347static struct elf64_hppa_dyn_hash_entry *
348elf64_hppa_dyn_hash_lookup(table, string, create, copy)
349 struct elf64_hppa_dyn_hash_table *table;
350 const char *string;
b34976b6 351 bfd_boolean create, copy;
15bda425
JL
352{
353 return ((struct elf64_hppa_dyn_hash_entry *)
354 bfd_hash_lookup (&table->root, string, create, copy));
355}
356
357/* Traverse a PA64 ELF linker hash table. */
358
359static void
360elf64_hppa_dyn_hash_traverse (table, func, info)
361 struct elf64_hppa_dyn_hash_table *table;
b34976b6 362 bfd_boolean (*func) PARAMS ((struct elf64_hppa_dyn_hash_entry *, PTR));
15bda425
JL
363 PTR info;
364{
365 (bfd_hash_traverse
366 (&table->root,
b34976b6 367 (bfd_boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) func,
15bda425
JL
368 info));
369}
370\f
371/* Return nonzero if ABFD represents a PA2.0 ELF64 file.
372
373 Additionally we set the default architecture and machine. */
b34976b6 374static bfd_boolean
15bda425
JL
375elf64_hppa_object_p (abfd)
376 bfd *abfd;
377{
24a5e751
L
378 Elf_Internal_Ehdr * i_ehdrp;
379 unsigned int flags;
d9634ba1 380
24a5e751
L
381 i_ehdrp = elf_elfheader (abfd);
382 if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
383 {
6c21aa76
NC
384 /* GCC on hppa-linux produces binaries with OSABI=Linux,
385 but the kernel produces corefiles with OSABI=SysV. */
386 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_LINUX &&
387 i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
b34976b6 388 return FALSE;
24a5e751
L
389 }
390 else
391 {
392 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
b34976b6 393 return FALSE;
24a5e751
L
394 }
395
396 flags = i_ehdrp->e_flags;
d9634ba1
AM
397 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
398 {
399 case EFA_PARISC_1_0:
400 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
401 case EFA_PARISC_1_1:
402 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
403 case EFA_PARISC_2_0:
404 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
405 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
406 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
407 }
408 /* Don't be fussy. */
b34976b6 409 return TRUE;
15bda425
JL
410}
411
412/* Given section type (hdr->sh_type), return a boolean indicating
413 whether or not the section is an elf64-hppa specific section. */
b34976b6 414static bfd_boolean
15bda425
JL
415elf64_hppa_section_from_shdr (abfd, hdr, name)
416 bfd *abfd;
947216bf 417 Elf_Internal_Shdr *hdr;
90937f86 418 const char *name;
15bda425
JL
419{
420 asection *newsect;
421
422 switch (hdr->sh_type)
423 {
424 case SHT_PARISC_EXT:
425 if (strcmp (name, ".PARISC.archext") != 0)
b34976b6 426 return FALSE;
15bda425
JL
427 break;
428 case SHT_PARISC_UNWIND:
429 if (strcmp (name, ".PARISC.unwind") != 0)
b34976b6 430 return FALSE;
15bda425
JL
431 break;
432 case SHT_PARISC_DOC:
433 case SHT_PARISC_ANNOT:
434 default:
b34976b6 435 return FALSE;
15bda425
JL
436 }
437
438 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
b34976b6 439 return FALSE;
15bda425
JL
440 newsect = hdr->bfd_section;
441
b34976b6 442 return TRUE;
15bda425
JL
443}
444
15bda425 445/* Construct a string for use in the elf64_hppa_dyn_hash_table. The
fe8bc63d 446 name describes what was once potentially anonymous memory. We
15bda425
JL
447 allocate memory as necessary, possibly reusing PBUF/PLEN. */
448
449static const char *
d63b5ed9
DA
450get_dyn_name (abfd, h, rel, pbuf, plen)
451 bfd *abfd;
15bda425
JL
452 struct elf_link_hash_entry *h;
453 const Elf_Internal_Rela *rel;
454 char **pbuf;
455 size_t *plen;
456{
d63b5ed9 457 asection *sec = abfd->sections;
15bda425
JL
458 size_t nlen, tlen;
459 char *buf;
460 size_t len;
461
462 if (h && rel->r_addend == 0)
463 return h->root.root.string;
464
465 if (h)
466 nlen = strlen (h->root.root.string);
467 else
0ba2a60e
AM
468 nlen = 8 + 1 + sizeof (rel->r_info) * 2 - 8;
469 tlen = nlen + 1 + sizeof (rel->r_addend) * 2 + 1;
15bda425
JL
470
471 len = *plen;
472 buf = *pbuf;
473 if (len < tlen)
474 {
475 if (buf)
476 free (buf);
477 *pbuf = buf = malloc (tlen);
478 *plen = len = tlen;
479 if (!buf)
480 return NULL;
481 }
482
483 if (h)
484 {
485 memcpy (buf, h->root.root.string, nlen);
0ba2a60e 486 buf[nlen++] = '+';
15bda425
JL
487 sprintf_vma (buf + nlen, rel->r_addend);
488 }
489 else
490 {
0ba2a60e
AM
491 nlen = sprintf (buf, "%x:%lx",
492 sec->id & 0xffffffff,
493 (long) ELF64_R_SYM (rel->r_info));
15bda425
JL
494 if (rel->r_addend)
495 {
496 buf[nlen++] = '+';
497 sprintf_vma (buf + nlen, rel->r_addend);
498 }
499 }
500
501 return buf;
502}
503
504/* SEC is a section containing relocs for an input BFD when linking; return
505 a suitable section for holding relocs in the output BFD for a link. */
506
b34976b6 507static bfd_boolean
15bda425
JL
508get_reloc_section (abfd, hppa_info, sec)
509 bfd *abfd;
510 struct elf64_hppa_link_hash_table *hppa_info;
511 asection *sec;
512{
513 const char *srel_name;
514 asection *srel;
515 bfd *dynobj;
516
517 srel_name = (bfd_elf_string_from_elf_section
518 (abfd, elf_elfheader(abfd)->e_shstrndx,
519 elf_section_data(sec)->rel_hdr.sh_name));
520 if (srel_name == NULL)
b34976b6 521 return FALSE;
15bda425
JL
522
523 BFD_ASSERT ((strncmp (srel_name, ".rela", 5) == 0
524 && strcmp (bfd_get_section_name (abfd, sec),
525 srel_name+5) == 0)
526 || (strncmp (srel_name, ".rel", 4) == 0
527 && strcmp (bfd_get_section_name (abfd, sec),
528 srel_name+4) == 0));
529
530 dynobj = hppa_info->root.dynobj;
531 if (!dynobj)
532 hppa_info->root.dynobj = dynobj = abfd;
533
534 srel = bfd_get_section_by_name (dynobj, srel_name);
535 if (srel == NULL)
536 {
537 srel = bfd_make_section (dynobj, srel_name);
538 if (srel == NULL
539 || !bfd_set_section_flags (dynobj, srel,
540 (SEC_ALLOC
541 | SEC_LOAD
542 | SEC_HAS_CONTENTS
543 | SEC_IN_MEMORY
544 | SEC_LINKER_CREATED
545 | SEC_READONLY))
546 || !bfd_set_section_alignment (dynobj, srel, 3))
b34976b6 547 return FALSE;
15bda425
JL
548 }
549
550 hppa_info->other_rel_sec = srel;
b34976b6 551 return TRUE;
15bda425
JL
552}
553
fe8bc63d 554/* Add a new entry to the list of dynamic relocations against DYN_H.
15bda425
JL
555
556 We use this to keep a record of all the FPTR relocations against a
557 particular symbol so that we can create FPTR relocations in the
558 output file. */
559
b34976b6 560static bfd_boolean
15bda425
JL
561count_dyn_reloc (abfd, dyn_h, type, sec, sec_symndx, offset, addend)
562 bfd *abfd;
563 struct elf64_hppa_dyn_hash_entry *dyn_h;
564 int type;
565 asection *sec;
566 int sec_symndx;
567 bfd_vma offset;
568 bfd_vma addend;
569{
570 struct elf64_hppa_dyn_reloc_entry *rent;
571
572 rent = (struct elf64_hppa_dyn_reloc_entry *)
dc810e39 573 bfd_alloc (abfd, (bfd_size_type) sizeof (*rent));
15bda425 574 if (!rent)
b34976b6 575 return FALSE;
15bda425
JL
576
577 rent->next = dyn_h->reloc_entries;
578 rent->type = type;
579 rent->sec = sec;
580 rent->sec_symndx = sec_symndx;
581 rent->offset = offset;
582 rent->addend = addend;
583 dyn_h->reloc_entries = rent;
584
b34976b6 585 return TRUE;
15bda425
JL
586}
587
588/* Scan the RELOCS and record the type of dynamic entries that each
589 referenced symbol needs. */
590
b34976b6 591static bfd_boolean
15bda425
JL
592elf64_hppa_check_relocs (abfd, info, sec, relocs)
593 bfd *abfd;
594 struct bfd_link_info *info;
595 asection *sec;
596 const Elf_Internal_Rela *relocs;
597{
598 struct elf64_hppa_link_hash_table *hppa_info;
599 const Elf_Internal_Rela *relend;
600 Elf_Internal_Shdr *symtab_hdr;
601 const Elf_Internal_Rela *rel;
602 asection *dlt, *plt, *stubs;
603 char *buf;
604 size_t buf_len;
605 int sec_symndx;
606
1049f94e 607 if (info->relocatable)
b34976b6 608 return TRUE;
15bda425
JL
609
610 /* If this is the first dynamic object found in the link, create
611 the special sections required for dynamic linking. */
612 if (! elf_hash_table (info)->dynamic_sections_created)
613 {
45d6a902 614 if (! _bfd_elf_link_create_dynamic_sections (abfd, info))
b34976b6 615 return FALSE;
15bda425
JL
616 }
617
618 hppa_info = elf64_hppa_hash_table (info);
619 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
620
621 /* If necessary, build a new table holding section symbols indices
6cdc0ccc 622 for this BFD. */
fe8bc63d 623
15bda425
JL
624 if (info->shared && hppa_info->section_syms_bfd != abfd)
625 {
832d951b 626 unsigned long i;
9ad5cbcf 627 unsigned int highest_shndx;
6cdc0ccc
AM
628 Elf_Internal_Sym *local_syms = NULL;
629 Elf_Internal_Sym *isym, *isymend;
dc810e39 630 bfd_size_type amt;
15bda425
JL
631
632 /* We're done with the old cache of section index to section symbol
633 index information. Free it.
634
635 ?!? Note we leak the last section_syms array. Presumably we
636 could free it in one of the later routines in this file. */
637 if (hppa_info->section_syms)
638 free (hppa_info->section_syms);
639
6cdc0ccc
AM
640 /* Read this BFD's local symbols. */
641 if (symtab_hdr->sh_info != 0)
47b7c2db 642 {
6cdc0ccc
AM
643 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
644 if (local_syms == NULL)
645 local_syms = bfd_elf_get_elf_syms (abfd, symtab_hdr,
646 symtab_hdr->sh_info, 0,
647 NULL, NULL, NULL);
648 if (local_syms == NULL)
b34976b6 649 return FALSE;
9ad5cbcf
AM
650 }
651
6cdc0ccc 652 /* Record the highest section index referenced by the local symbols. */
15bda425 653 highest_shndx = 0;
6cdc0ccc
AM
654 isymend = local_syms + symtab_hdr->sh_info;
655 for (isym = local_syms; isym < isymend; isym++)
15bda425 656 {
15bda425
JL
657 if (isym->st_shndx > highest_shndx)
658 highest_shndx = isym->st_shndx;
659 }
660
15bda425
JL
661 /* Allocate an array to hold the section index to section symbol index
662 mapping. Bump by one since we start counting at zero. */
663 highest_shndx++;
dc810e39
AM
664 amt = highest_shndx;
665 amt *= sizeof (int);
666 hppa_info->section_syms = (int *) bfd_malloc (amt);
15bda425
JL
667
668 /* Now walk the local symbols again. If we find a section symbol,
669 record the index of the symbol into the section_syms array. */
6cdc0ccc 670 for (i = 0, isym = local_syms; isym < isymend; i++, isym++)
15bda425
JL
671 {
672 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
673 hppa_info->section_syms[isym->st_shndx] = i;
674 }
675
6cdc0ccc
AM
676 /* We are finished with the local symbols. */
677 if (local_syms != NULL
678 && symtab_hdr->contents != (unsigned char *) local_syms)
679 {
680 if (! info->keep_memory)
681 free (local_syms);
682 else
683 {
684 /* Cache the symbols for elf_link_input_bfd. */
685 symtab_hdr->contents = (unsigned char *) local_syms;
686 }
687 }
15bda425
JL
688
689 /* Record which BFD we built the section_syms mapping for. */
690 hppa_info->section_syms_bfd = abfd;
691 }
692
693 /* Record the symbol index for this input section. We may need it for
694 relocations when building shared libraries. When not building shared
695 libraries this value is never really used, but assign it to zero to
696 prevent out of bounds memory accesses in other routines. */
697 if (info->shared)
698 {
699 sec_symndx = _bfd_elf_section_from_bfd_section (abfd, sec);
700
701 /* If we did not find a section symbol for this section, then
702 something went terribly wrong above. */
703 if (sec_symndx == -1)
b34976b6 704 return FALSE;
15bda425
JL
705
706 sec_symndx = hppa_info->section_syms[sec_symndx];
707 }
708 else
709 sec_symndx = 0;
fe8bc63d 710
15bda425
JL
711 dlt = plt = stubs = NULL;
712 buf = NULL;
713 buf_len = 0;
714
715 relend = relocs + sec->reloc_count;
716 for (rel = relocs; rel < relend; ++rel)
717 {
560e09e9
NC
718 enum
719 {
720 NEED_DLT = 1,
721 NEED_PLT = 2,
722 NEED_STUB = 4,
723 NEED_OPD = 8,
724 NEED_DYNREL = 16,
725 };
15bda425
JL
726
727 struct elf_link_hash_entry *h = NULL;
728 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
729 struct elf64_hppa_dyn_hash_entry *dyn_h;
730 int need_entry;
731 const char *addr_name;
b34976b6 732 bfd_boolean maybe_dynamic;
15bda425
JL
733 int dynrel_type = R_PARISC_NONE;
734 static reloc_howto_type *howto;
735
736 if (r_symndx >= symtab_hdr->sh_info)
737 {
738 /* We're dealing with a global symbol -- find its hash entry
739 and mark it as being referenced. */
740 long indx = r_symndx - symtab_hdr->sh_info;
741 h = elf_sym_hashes (abfd)[indx];
742 while (h->root.type == bfd_link_hash_indirect
743 || h->root.type == bfd_link_hash_warning)
744 h = (struct elf_link_hash_entry *) h->root.u.i.link;
745
746 h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
747 }
748
749 /* We can only get preliminary data on whether a symbol is
750 locally or externally defined, as not all of the input files
751 have yet been processed. Do something with what we know, as
752 this may help reduce memory usage and processing time later. */
b34976b6 753 maybe_dynamic = FALSE;
671bae9c 754 if (h && ((info->shared
560e09e9 755 && (!info->symbolic || info->unresolved_syms_in_shared_libs == RM_IGNORE))
15bda425
JL
756 || ! (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)
757 || h->root.type == bfd_link_hash_defweak))
b34976b6 758 maybe_dynamic = TRUE;
15bda425
JL
759
760 howto = elf_hppa_howto_table + ELF64_R_TYPE (rel->r_info);
761 need_entry = 0;
762 switch (howto->type)
763 {
764 /* These are simple indirect references to symbols through the
765 DLT. We need to create a DLT entry for any symbols which
766 appears in a DLTIND relocation. */
767 case R_PARISC_DLTIND21L:
768 case R_PARISC_DLTIND14R:
769 case R_PARISC_DLTIND14F:
770 case R_PARISC_DLTIND14WR:
771 case R_PARISC_DLTIND14DR:
772 need_entry = NEED_DLT;
773 break;
774
775 /* ?!? These need a DLT entry. But I have no idea what to do with
776 the "link time TP value. */
777 case R_PARISC_LTOFF_TP21L:
778 case R_PARISC_LTOFF_TP14R:
779 case R_PARISC_LTOFF_TP14F:
780 case R_PARISC_LTOFF_TP64:
781 case R_PARISC_LTOFF_TP14WR:
782 case R_PARISC_LTOFF_TP14DR:
783 case R_PARISC_LTOFF_TP16F:
784 case R_PARISC_LTOFF_TP16WF:
785 case R_PARISC_LTOFF_TP16DF:
786 need_entry = NEED_DLT;
787 break;
788
789 /* These are function calls. Depending on their precise target we
790 may need to make a stub for them. The stub uses the PLT, so we
791 need to create PLT entries for these symbols too. */
832d951b 792 case R_PARISC_PCREL12F:
15bda425
JL
793 case R_PARISC_PCREL17F:
794 case R_PARISC_PCREL22F:
795 case R_PARISC_PCREL32:
796 case R_PARISC_PCREL64:
797 case R_PARISC_PCREL21L:
798 case R_PARISC_PCREL17R:
799 case R_PARISC_PCREL17C:
800 case R_PARISC_PCREL14R:
801 case R_PARISC_PCREL14F:
802 case R_PARISC_PCREL22C:
803 case R_PARISC_PCREL14WR:
804 case R_PARISC_PCREL14DR:
805 case R_PARISC_PCREL16F:
806 case R_PARISC_PCREL16WF:
807 case R_PARISC_PCREL16DF:
808 need_entry = (NEED_PLT | NEED_STUB);
809 break;
810
811 case R_PARISC_PLTOFF21L:
812 case R_PARISC_PLTOFF14R:
813 case R_PARISC_PLTOFF14F:
814 case R_PARISC_PLTOFF14WR:
815 case R_PARISC_PLTOFF14DR:
816 case R_PARISC_PLTOFF16F:
817 case R_PARISC_PLTOFF16WF:
818 case R_PARISC_PLTOFF16DF:
819 need_entry = (NEED_PLT);
820 break;
821
822 case R_PARISC_DIR64:
823 if (info->shared || maybe_dynamic)
824 need_entry = (NEED_DYNREL);
825 dynrel_type = R_PARISC_DIR64;
826 break;
827
828 /* This is an indirect reference through the DLT to get the address
829 of a OPD descriptor. Thus we need to make a DLT entry that points
830 to an OPD entry. */
831 case R_PARISC_LTOFF_FPTR21L:
832 case R_PARISC_LTOFF_FPTR14R:
833 case R_PARISC_LTOFF_FPTR14WR:
834 case R_PARISC_LTOFF_FPTR14DR:
835 case R_PARISC_LTOFF_FPTR32:
836 case R_PARISC_LTOFF_FPTR64:
837 case R_PARISC_LTOFF_FPTR16F:
838 case R_PARISC_LTOFF_FPTR16WF:
839 case R_PARISC_LTOFF_FPTR16DF:
840 if (info->shared || maybe_dynamic)
841 need_entry = (NEED_DLT | NEED_OPD);
842 else
843 need_entry = (NEED_DLT | NEED_OPD);
844 dynrel_type = R_PARISC_FPTR64;
845 break;
846
847 /* This is a simple OPD entry. */
848 case R_PARISC_FPTR64:
849 if (info->shared || maybe_dynamic)
850 need_entry = (NEED_OPD | NEED_DYNREL);
851 else
852 need_entry = (NEED_OPD);
853 dynrel_type = R_PARISC_FPTR64;
854 break;
855
856 /* Add more cases as needed. */
857 }
858
859 if (!need_entry)
860 continue;
861
862 /* Collect a canonical name for this address. */
d63b5ed9 863 addr_name = get_dyn_name (abfd, h, rel, &buf, &buf_len);
15bda425
JL
864
865 /* Collect the canonical entry data for this address. */
866 dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
b34976b6 867 addr_name, TRUE, TRUE);
15bda425
JL
868 BFD_ASSERT (dyn_h);
869
870 /* Stash away enough information to be able to find this symbol
871 regardless of whether or not it is local or global. */
872 dyn_h->h = h;
873 dyn_h->owner = abfd;
874 dyn_h->sym_indx = r_symndx;
875
876 /* ?!? We may need to do some error checking in here. */
877 /* Create what's needed. */
878 if (need_entry & NEED_DLT)
879 {
880 if (! hppa_info->dlt_sec
881 && ! get_dlt (abfd, info, hppa_info))
882 goto err_out;
883 dyn_h->want_dlt = 1;
884 }
885
886 if (need_entry & NEED_PLT)
887 {
888 if (! hppa_info->plt_sec
889 && ! get_plt (abfd, info, hppa_info))
890 goto err_out;
891 dyn_h->want_plt = 1;
892 }
893
894 if (need_entry & NEED_STUB)
895 {
896 if (! hppa_info->stub_sec
897 && ! get_stub (abfd, info, hppa_info))
898 goto err_out;
899 dyn_h->want_stub = 1;
900 }
901
902 if (need_entry & NEED_OPD)
903 {
904 if (! hppa_info->opd_sec
905 && ! get_opd (abfd, info, hppa_info))
906 goto err_out;
907
908 dyn_h->want_opd = 1;
909
910 /* FPTRs are not allocated by the dynamic linker for PA64, though
911 it is possible that will change in the future. */
fe8bc63d 912
15bda425
JL
913 /* This could be a local function that had its address taken, in
914 which case H will be NULL. */
915 if (h)
916 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
917 }
918
919 /* Add a new dynamic relocation to the chain of dynamic
920 relocations for this symbol. */
921 if ((need_entry & NEED_DYNREL) && (sec->flags & SEC_ALLOC))
922 {
923 if (! hppa_info->other_rel_sec
924 && ! get_reloc_section (abfd, hppa_info, sec))
925 goto err_out;
926
927 if (!count_dyn_reloc (abfd, dyn_h, dynrel_type, sec,
928 sec_symndx, rel->r_offset, rel->r_addend))
929 goto err_out;
930
931 /* If we are building a shared library and we just recorded
932 a dynamic R_PARISC_FPTR64 relocation, then make sure the
933 section symbol for this section ends up in the dynamic
934 symbol table. */
935 if (info->shared && dynrel_type == R_PARISC_FPTR64
c152c796 936 && ! (bfd_elf_link_record_local_dynamic_symbol
15bda425 937 (info, abfd, sec_symndx)))
b34976b6 938 return FALSE;
15bda425
JL
939 }
940 }
941
942 if (buf)
943 free (buf);
b34976b6 944 return TRUE;
15bda425
JL
945
946 err_out:
947 if (buf)
948 free (buf);
b34976b6 949 return FALSE;
15bda425
JL
950}
951
952struct elf64_hppa_allocate_data
953{
954 struct bfd_link_info *info;
955 bfd_size_type ofs;
956};
957
958/* Should we do dynamic things to this symbol? */
959
b34976b6 960static bfd_boolean
15bda425
JL
961elf64_hppa_dynamic_symbol_p (h, info)
962 struct elf_link_hash_entry *h;
963 struct bfd_link_info *info;
964{
986a241f
RH
965 /* ??? What, if anything, needs to happen wrt STV_PROTECTED symbols
966 and relocations that retrieve a function descriptor? Assume the
967 worst for now. */
968 if (_bfd_elf_dynamic_symbol_p (h, info, 1))
969 {
970 /* ??? Why is this here and not elsewhere is_local_label_name. */
971 if (h->root.root.string[0] == '$' && h->root.root.string[1] == '$')
972 return FALSE;
15bda425 973
986a241f
RH
974 return TRUE;
975 }
976 else
b34976b6 977 return FALSE;
15bda425
JL
978}
979
4cc11e76 980/* Mark all functions exported by this file so that we can later allocate
15bda425
JL
981 entries in .opd for them. */
982
b34976b6 983static bfd_boolean
15bda425
JL
984elf64_hppa_mark_exported_functions (h, data)
985 struct elf_link_hash_entry *h;
986 PTR data;
987{
988 struct bfd_link_info *info = (struct bfd_link_info *)data;
989 struct elf64_hppa_link_hash_table *hppa_info;
990
991 hppa_info = elf64_hppa_hash_table (info);
992
e92d460e
AM
993 if (h->root.type == bfd_link_hash_warning)
994 h = (struct elf_link_hash_entry *) h->root.u.i.link;
995
15bda425
JL
996 if (h
997 && (h->root.type == bfd_link_hash_defined
998 || h->root.type == bfd_link_hash_defweak)
999 && h->root.u.def.section->output_section != NULL
1000 && h->type == STT_FUNC)
1001 {
1002 struct elf64_hppa_dyn_hash_entry *dyn_h;
1003
1004 /* Add this symbol to the PA64 linker hash table. */
1005 dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
b34976b6 1006 h->root.root.string, TRUE, TRUE);
15bda425
JL
1007 BFD_ASSERT (dyn_h);
1008 dyn_h->h = h;
1009
1010 if (! hppa_info->opd_sec
1011 && ! get_opd (hppa_info->root.dynobj, info, hppa_info))
b34976b6 1012 return FALSE;
15bda425
JL
1013
1014 dyn_h->want_opd = 1;
832d951b
AM
1015 /* Put a flag here for output_symbol_hook. */
1016 dyn_h->st_shndx = -1;
15bda425
JL
1017 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
1018 }
1019
b34976b6 1020 return TRUE;
15bda425
JL
1021}
1022
1023/* Allocate space for a DLT entry. */
1024
b34976b6 1025static bfd_boolean
15bda425
JL
1026allocate_global_data_dlt (dyn_h, data)
1027 struct elf64_hppa_dyn_hash_entry *dyn_h;
1028 PTR data;
1029{
1030 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1031
1032 if (dyn_h->want_dlt)
1033 {
1034 struct elf_link_hash_entry *h = dyn_h->h;
1035
1036 if (x->info->shared)
1037 {
1038 /* Possibly add the symbol to the local dynamic symbol
1039 table since we might need to create a dynamic relocation
1040 against it. */
1041 if (! h
47b7c2db 1042 || (h->dynindx == -1 && h->type != STT_PARISC_MILLI))
15bda425
JL
1043 {
1044 bfd *owner;
1045 owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
1046
c152c796 1047 if (! (bfd_elf_link_record_local_dynamic_symbol
dc810e39 1048 (x->info, owner, dyn_h->sym_indx)))
b34976b6 1049 return FALSE;
15bda425
JL
1050 }
1051 }
1052
1053 dyn_h->dlt_offset = x->ofs;
1054 x->ofs += DLT_ENTRY_SIZE;
1055 }
b34976b6 1056 return TRUE;
15bda425
JL
1057}
1058
1059/* Allocate space for a DLT.PLT entry. */
1060
b34976b6 1061static bfd_boolean
15bda425
JL
1062allocate_global_data_plt (dyn_h, data)
1063 struct elf64_hppa_dyn_hash_entry *dyn_h;
1064 PTR data;
1065{
1066 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1067
1068 if (dyn_h->want_plt
1069 && elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info)
1070 && !((dyn_h->h->root.type == bfd_link_hash_defined
1071 || dyn_h->h->root.type == bfd_link_hash_defweak)
1072 && dyn_h->h->root.u.def.section->output_section != NULL))
1073 {
1074 dyn_h->plt_offset = x->ofs;
1075 x->ofs += PLT_ENTRY_SIZE;
1076 if (dyn_h->plt_offset < 0x2000)
1077 elf64_hppa_hash_table (x->info)->gp_offset = dyn_h->plt_offset;
1078 }
1079 else
1080 dyn_h->want_plt = 0;
1081
b34976b6 1082 return TRUE;
15bda425
JL
1083}
1084
1085/* Allocate space for a STUB entry. */
1086
b34976b6 1087static bfd_boolean
15bda425
JL
1088allocate_global_data_stub (dyn_h, data)
1089 struct elf64_hppa_dyn_hash_entry *dyn_h;
1090 PTR data;
1091{
1092 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1093
1094 if (dyn_h->want_stub
1095 && elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info)
1096 && !((dyn_h->h->root.type == bfd_link_hash_defined
1097 || dyn_h->h->root.type == bfd_link_hash_defweak)
1098 && dyn_h->h->root.u.def.section->output_section != NULL))
1099 {
1100 dyn_h->stub_offset = x->ofs;
1101 x->ofs += sizeof (plt_stub);
1102 }
1103 else
1104 dyn_h->want_stub = 0;
b34976b6 1105 return TRUE;
15bda425
JL
1106}
1107
1108/* Allocate space for a FPTR entry. */
1109
b34976b6 1110static bfd_boolean
15bda425
JL
1111allocate_global_data_opd (dyn_h, data)
1112 struct elf64_hppa_dyn_hash_entry *dyn_h;
1113 PTR data;
1114{
1115 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1116
1117 if (dyn_h->want_opd)
1118 {
1119 struct elf_link_hash_entry *h = dyn_h->h;
fe8bc63d 1120
15bda425
JL
1121 if (h)
1122 while (h->root.type == bfd_link_hash_indirect
1123 || h->root.type == bfd_link_hash_warning)
1124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1125
1126 /* We never need an opd entry for a symbol which is not
1127 defined by this output file. */
3db4b612
JL
1128 if (h && (h->root.type == bfd_link_hash_undefined
1129 || h->root.u.def.section->output_section == NULL))
15bda425
JL
1130 dyn_h->want_opd = 0;
1131
1132 /* If we are creating a shared library, took the address of a local
1133 function or might export this function from this object file, then
1134 we have to create an opd descriptor. */
1135 else if (x->info->shared
1136 || h == NULL
47b7c2db 1137 || (h->dynindx == -1 && h->type != STT_PARISC_MILLI)
3db4b612
JL
1138 || (h->root.type == bfd_link_hash_defined
1139 || h->root.type == bfd_link_hash_defweak))
15bda425
JL
1140 {
1141 /* If we are creating a shared library, then we will have to
1142 create a runtime relocation for the symbol to properly
1143 initialize the .opd entry. Make sure the symbol gets
1144 added to the dynamic symbol table. */
1145 if (x->info->shared
1146 && (h == NULL || (h->dynindx == -1)))
1147 {
1148 bfd *owner;
1149 owner = (h ? h->root.u.def.section->owner : dyn_h->owner);
1150
c152c796 1151 if (!bfd_elf_link_record_local_dynamic_symbol
15bda425 1152 (x->info, owner, dyn_h->sym_indx))
b34976b6 1153 return FALSE;
15bda425
JL
1154 }
1155
1156 /* This may not be necessary or desirable anymore now that
1157 we have some support for dealing with section symbols
1158 in dynamic relocs. But name munging does make the result
1159 much easier to debug. ie, the EPLT reloc will reference
1160 a symbol like .foobar, instead of .text + offset. */
1161 if (x->info->shared && h)
1162 {
1163 char *new_name;
1164 struct elf_link_hash_entry *nh;
1165
1166 new_name = alloca (strlen (h->root.root.string) + 2);
1167 new_name[0] = '.';
1168 strcpy (new_name + 1, h->root.root.string);
1169
1170 nh = elf_link_hash_lookup (elf_hash_table (x->info),
b34976b6 1171 new_name, TRUE, TRUE, TRUE);
15bda425
JL
1172
1173 nh->root.type = h->root.type;
1174 nh->root.u.def.value = h->root.u.def.value;
1175 nh->root.u.def.section = h->root.u.def.section;
1176
c152c796 1177 if (! bfd_elf_link_record_dynamic_symbol (x->info, nh))
b34976b6 1178 return FALSE;
15bda425
JL
1179
1180 }
1181 dyn_h->opd_offset = x->ofs;
1182 x->ofs += OPD_ENTRY_SIZE;
1183 }
1184
1185 /* Otherwise we do not need an opd entry. */
1186 else
1187 dyn_h->want_opd = 0;
1188 }
b34976b6 1189 return TRUE;
15bda425
JL
1190}
1191
1192/* HP requires the EI_OSABI field to be filled in. The assignment to
1193 EI_ABIVERSION may not be strictly necessary. */
1194
1195static void
1196elf64_hppa_post_process_headers (abfd, link_info)
1197 bfd * abfd;
1198 struct bfd_link_info * link_info ATTRIBUTE_UNUSED;
1199{
1200 Elf_Internal_Ehdr * i_ehdrp;
1201
1202 i_ehdrp = elf_elfheader (abfd);
1203
d952f17a
AM
1204 if (strcmp (bfd_get_target (abfd), "elf64-hppa-linux") == 0)
1205 {
1206 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_LINUX;
1207 }
1208 else
1209 {
1210 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_HPUX;
1211 i_ehdrp->e_ident[EI_ABIVERSION] = 1;
1212 }
15bda425
JL
1213}
1214
1215/* Create function descriptor section (.opd). This section is called .opd
4cc11e76 1216 because it contains "official procedure descriptors". The "official"
15bda425
JL
1217 refers to the fact that these descriptors are used when taking the address
1218 of a procedure, thus ensuring a unique address for each procedure. */
1219
b34976b6 1220static bfd_boolean
15bda425
JL
1221get_opd (abfd, info, hppa_info)
1222 bfd *abfd;
edd21aca 1223 struct bfd_link_info *info ATTRIBUTE_UNUSED;
15bda425
JL
1224 struct elf64_hppa_link_hash_table *hppa_info;
1225{
1226 asection *opd;
1227 bfd *dynobj;
1228
1229 opd = hppa_info->opd_sec;
1230 if (!opd)
1231 {
1232 dynobj = hppa_info->root.dynobj;
1233 if (!dynobj)
1234 hppa_info->root.dynobj = dynobj = abfd;
1235
1236 opd = bfd_make_section (dynobj, ".opd");
1237 if (!opd
1238 || !bfd_set_section_flags (dynobj, opd,
1239 (SEC_ALLOC
1240 | SEC_LOAD
1241 | SEC_HAS_CONTENTS
1242 | SEC_IN_MEMORY
1243 | SEC_LINKER_CREATED))
1244 || !bfd_set_section_alignment (abfd, opd, 3))
1245 {
1246 BFD_ASSERT (0);
b34976b6 1247 return FALSE;
15bda425
JL
1248 }
1249
1250 hppa_info->opd_sec = opd;
1251 }
1252
b34976b6 1253 return TRUE;
15bda425
JL
1254}
1255
1256/* Create the PLT section. */
1257
b34976b6 1258static bfd_boolean
15bda425
JL
1259get_plt (abfd, info, hppa_info)
1260 bfd *abfd;
edd21aca 1261 struct bfd_link_info *info ATTRIBUTE_UNUSED;
15bda425
JL
1262 struct elf64_hppa_link_hash_table *hppa_info;
1263{
1264 asection *plt;
1265 bfd *dynobj;
1266
1267 plt = hppa_info->plt_sec;
1268 if (!plt)
1269 {
1270 dynobj = hppa_info->root.dynobj;
1271 if (!dynobj)
1272 hppa_info->root.dynobj = dynobj = abfd;
1273
1274 plt = bfd_make_section (dynobj, ".plt");
1275 if (!plt
1276 || !bfd_set_section_flags (dynobj, plt,
1277 (SEC_ALLOC
1278 | SEC_LOAD
1279 | SEC_HAS_CONTENTS
1280 | SEC_IN_MEMORY
1281 | SEC_LINKER_CREATED))
1282 || !bfd_set_section_alignment (abfd, plt, 3))
1283 {
1284 BFD_ASSERT (0);
b34976b6 1285 return FALSE;
15bda425
JL
1286 }
1287
1288 hppa_info->plt_sec = plt;
1289 }
1290
b34976b6 1291 return TRUE;
15bda425
JL
1292}
1293
1294/* Create the DLT section. */
1295
b34976b6 1296static bfd_boolean
15bda425
JL
1297get_dlt (abfd, info, hppa_info)
1298 bfd *abfd;
edd21aca 1299 struct bfd_link_info *info ATTRIBUTE_UNUSED;
15bda425
JL
1300 struct elf64_hppa_link_hash_table *hppa_info;
1301{
1302 asection *dlt;
1303 bfd *dynobj;
1304
1305 dlt = hppa_info->dlt_sec;
1306 if (!dlt)
1307 {
1308 dynobj = hppa_info->root.dynobj;
1309 if (!dynobj)
1310 hppa_info->root.dynobj = dynobj = abfd;
1311
1312 dlt = bfd_make_section (dynobj, ".dlt");
1313 if (!dlt
1314 || !bfd_set_section_flags (dynobj, dlt,
1315 (SEC_ALLOC
1316 | SEC_LOAD
1317 | SEC_HAS_CONTENTS
1318 | SEC_IN_MEMORY
1319 | SEC_LINKER_CREATED))
1320 || !bfd_set_section_alignment (abfd, dlt, 3))
1321 {
1322 BFD_ASSERT (0);
b34976b6 1323 return FALSE;
15bda425
JL
1324 }
1325
1326 hppa_info->dlt_sec = dlt;
1327 }
1328
b34976b6 1329 return TRUE;
15bda425
JL
1330}
1331
1332/* Create the stubs section. */
1333
b34976b6 1334static bfd_boolean
15bda425
JL
1335get_stub (abfd, info, hppa_info)
1336 bfd *abfd;
edd21aca 1337 struct bfd_link_info *info ATTRIBUTE_UNUSED;
15bda425
JL
1338 struct elf64_hppa_link_hash_table *hppa_info;
1339{
1340 asection *stub;
1341 bfd *dynobj;
1342
1343 stub = hppa_info->stub_sec;
1344 if (!stub)
1345 {
1346 dynobj = hppa_info->root.dynobj;
1347 if (!dynobj)
1348 hppa_info->root.dynobj = dynobj = abfd;
1349
1350 stub = bfd_make_section (dynobj, ".stub");
1351 if (!stub
1352 || !bfd_set_section_flags (dynobj, stub,
1353 (SEC_ALLOC
1354 | SEC_LOAD
1355 | SEC_HAS_CONTENTS
1356 | SEC_IN_MEMORY
1357 | SEC_READONLY
1358 | SEC_LINKER_CREATED))
1359 || !bfd_set_section_alignment (abfd, stub, 3))
1360 {
1361 BFD_ASSERT (0);
b34976b6 1362 return FALSE;
15bda425
JL
1363 }
1364
1365 hppa_info->stub_sec = stub;
1366 }
1367
b34976b6 1368 return TRUE;
15bda425
JL
1369}
1370
1371/* Create sections necessary for dynamic linking. This is only a rough
1372 cut and will likely change as we learn more about the somewhat
1373 unusual dynamic linking scheme HP uses.
1374
1375 .stub:
1376 Contains code to implement cross-space calls. The first time one
1377 of the stubs is used it will call into the dynamic linker, later
1378 calls will go straight to the target.
1379
1380 The only stub we support right now looks like
1381
1382 ldd OFFSET(%dp),%r1
1383 bve %r0(%r1)
1384 ldd OFFSET+8(%dp),%dp
1385
1386 Other stubs may be needed in the future. We may want the remove
1387 the break/nop instruction. It is only used right now to keep the
1388 offset of a .plt entry and a .stub entry in sync.
1389
1390 .dlt:
1391 This is what most people call the .got. HP used a different name.
1392 Losers.
1393
1394 .rela.dlt:
1395 Relocations for the DLT.
1396
1397 .plt:
1398 Function pointers as address,gp pairs.
1399
1400 .rela.plt:
1401 Should contain dynamic IPLT (and EPLT?) relocations.
1402
1403 .opd:
fe8bc63d 1404 FPTRS
15bda425
JL
1405
1406 .rela.opd:
1407 EPLT relocations for symbols exported from shared libraries. */
1408
b34976b6 1409static bfd_boolean
15bda425
JL
1410elf64_hppa_create_dynamic_sections (abfd, info)
1411 bfd *abfd;
1412 struct bfd_link_info *info;
1413{
1414 asection *s;
1415
1416 if (! get_stub (abfd, info, elf64_hppa_hash_table (info)))
b34976b6 1417 return FALSE;
15bda425
JL
1418
1419 if (! get_dlt (abfd, info, elf64_hppa_hash_table (info)))
b34976b6 1420 return FALSE;
15bda425
JL
1421
1422 if (! get_plt (abfd, info, elf64_hppa_hash_table (info)))
b34976b6 1423 return FALSE;
15bda425
JL
1424
1425 if (! get_opd (abfd, info, elf64_hppa_hash_table (info)))
b34976b6 1426 return FALSE;
15bda425
JL
1427
1428 s = bfd_make_section(abfd, ".rela.dlt");
1429 if (s == NULL
1430 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1431 | SEC_HAS_CONTENTS
1432 | SEC_IN_MEMORY
1433 | SEC_READONLY
1434 | SEC_LINKER_CREATED))
1435 || !bfd_set_section_alignment (abfd, s, 3))
b34976b6 1436 return FALSE;
15bda425
JL
1437 elf64_hppa_hash_table (info)->dlt_rel_sec = s;
1438
1439 s = bfd_make_section(abfd, ".rela.plt");
1440 if (s == NULL
1441 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1442 | SEC_HAS_CONTENTS
1443 | SEC_IN_MEMORY
1444 | SEC_READONLY
1445 | SEC_LINKER_CREATED))
1446 || !bfd_set_section_alignment (abfd, s, 3))
b34976b6 1447 return FALSE;
15bda425
JL
1448 elf64_hppa_hash_table (info)->plt_rel_sec = s;
1449
1450 s = bfd_make_section(abfd, ".rela.data");
1451 if (s == NULL
1452 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1453 | SEC_HAS_CONTENTS
1454 | SEC_IN_MEMORY
1455 | SEC_READONLY
1456 | SEC_LINKER_CREATED))
1457 || !bfd_set_section_alignment (abfd, s, 3))
b34976b6 1458 return FALSE;
15bda425
JL
1459 elf64_hppa_hash_table (info)->other_rel_sec = s;
1460
1461 s = bfd_make_section(abfd, ".rela.opd");
1462 if (s == NULL
1463 || !bfd_set_section_flags (abfd, s, (SEC_ALLOC | SEC_LOAD
1464 | SEC_HAS_CONTENTS
1465 | SEC_IN_MEMORY
1466 | SEC_READONLY
1467 | SEC_LINKER_CREATED))
1468 || !bfd_set_section_alignment (abfd, s, 3))
b34976b6 1469 return FALSE;
15bda425
JL
1470 elf64_hppa_hash_table (info)->opd_rel_sec = s;
1471
b34976b6 1472 return TRUE;
15bda425
JL
1473}
1474
1475/* Allocate dynamic relocations for those symbols that turned out
1476 to be dynamic. */
1477
b34976b6 1478static bfd_boolean
15bda425
JL
1479allocate_dynrel_entries (dyn_h, data)
1480 struct elf64_hppa_dyn_hash_entry *dyn_h;
1481 PTR data;
1482{
1483 struct elf64_hppa_allocate_data *x = (struct elf64_hppa_allocate_data *)data;
1484 struct elf64_hppa_link_hash_table *hppa_info;
1485 struct elf64_hppa_dyn_reloc_entry *rent;
b34976b6 1486 bfd_boolean dynamic_symbol, shared;
15bda425
JL
1487
1488 hppa_info = elf64_hppa_hash_table (x->info);
1489 dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, x->info);
1490 shared = x->info->shared;
1491
1492 /* We may need to allocate relocations for a non-dynamic symbol
1493 when creating a shared library. */
1494 if (!dynamic_symbol && !shared)
b34976b6 1495 return TRUE;
15bda425
JL
1496
1497 /* Take care of the normal data relocations. */
1498
1499 for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
1500 {
d663e1cd
JL
1501 /* Allocate one iff we are building a shared library, the relocation
1502 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
1503 if (!shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
1504 continue;
1505
eea6121a 1506 hppa_info->other_rel_sec->size += sizeof (Elf64_External_Rela);
15bda425
JL
1507
1508 /* Make sure this symbol gets into the dynamic symbol table if it is
1509 not already recorded. ?!? This should not be in the loop since
1510 the symbol need only be added once. */
47b7c2db
AM
1511 if (dyn_h->h == 0
1512 || (dyn_h->h->dynindx == -1 && dyn_h->h->type != STT_PARISC_MILLI))
c152c796 1513 if (!bfd_elf_link_record_local_dynamic_symbol
15bda425 1514 (x->info, rent->sec->owner, dyn_h->sym_indx))
b34976b6 1515 return FALSE;
15bda425
JL
1516 }
1517
1518 /* Take care of the GOT and PLT relocations. */
1519
1520 if ((dynamic_symbol || shared) && dyn_h->want_dlt)
eea6121a 1521 hppa_info->dlt_rel_sec->size += sizeof (Elf64_External_Rela);
15bda425
JL
1522
1523 /* If we are building a shared library, then every symbol that has an
1524 opd entry will need an EPLT relocation to relocate the symbol's address
1525 and __gp value based on the runtime load address. */
1526 if (shared && dyn_h->want_opd)
eea6121a 1527 hppa_info->opd_rel_sec->size += sizeof (Elf64_External_Rela);
15bda425
JL
1528
1529 if (dyn_h->want_plt && dynamic_symbol)
1530 {
1531 bfd_size_type t = 0;
1532
1533 /* Dynamic symbols get one IPLT relocation. Local symbols in
1534 shared libraries get two REL relocations. Local symbols in
1535 main applications get nothing. */
1536 if (dynamic_symbol)
1537 t = sizeof (Elf64_External_Rela);
1538 else if (shared)
1539 t = 2 * sizeof (Elf64_External_Rela);
1540
eea6121a 1541 hppa_info->plt_rel_sec->size += t;
15bda425
JL
1542 }
1543
b34976b6 1544 return TRUE;
15bda425
JL
1545}
1546
1547/* Adjust a symbol defined by a dynamic object and referenced by a
1548 regular object. */
1549
b34976b6 1550static bfd_boolean
15bda425 1551elf64_hppa_adjust_dynamic_symbol (info, h)
edd21aca 1552 struct bfd_link_info *info ATTRIBUTE_UNUSED;
15bda425
JL
1553 struct elf_link_hash_entry *h;
1554{
1555 /* ??? Undefined symbols with PLT entries should be re-defined
1556 to be the PLT entry. */
1557
1558 /* If this is a weak symbol, and there is a real definition, the
1559 processor independent code will have arranged for us to see the
1560 real definition first, and we can just use the same value. */
1561 if (h->weakdef != NULL)
1562 {
1563 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1564 || h->weakdef->root.type == bfd_link_hash_defweak);
1565 h->root.u.def.section = h->weakdef->root.u.def.section;
1566 h->root.u.def.value = h->weakdef->root.u.def.value;
b34976b6 1567 return TRUE;
15bda425
JL
1568 }
1569
1570 /* If this is a reference to a symbol defined by a dynamic object which
1571 is not a function, we might allocate the symbol in our .dynbss section
1572 and allocate a COPY dynamic relocation.
1573
1574 But PA64 code is canonically PIC, so as a rule we can avoid this sort
1575 of hackery. */
1576
b34976b6 1577 return TRUE;
15bda425
JL
1578}
1579
47b7c2db
AM
1580/* This function is called via elf_link_hash_traverse to mark millicode
1581 symbols with a dynindx of -1 and to remove the string table reference
1582 from the dynamic symbol table. If the symbol is not a millicode symbol,
1583 elf64_hppa_mark_exported_functions is called. */
1584
b34976b6 1585static bfd_boolean
47b7c2db
AM
1586elf64_hppa_mark_milli_and_exported_functions (h, data)
1587 struct elf_link_hash_entry *h;
1588 PTR data;
1589{
1590 struct bfd_link_info *info = (struct bfd_link_info *)data;
1591 struct elf_link_hash_entry *elf = h;
1592
1593 if (elf->root.type == bfd_link_hash_warning)
1594 elf = (struct elf_link_hash_entry *) elf->root.u.i.link;
1595
1596 if (elf->type == STT_PARISC_MILLI)
1597 {
1598 if (elf->dynindx != -1)
1599 {
1600 elf->dynindx = -1;
1601 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1602 elf->dynstr_index);
1603 }
b34976b6 1604 return TRUE;
47b7c2db
AM
1605 }
1606
1607 return elf64_hppa_mark_exported_functions (h, data);
1608}
1609
15bda425
JL
1610/* Set the final sizes of the dynamic sections and allocate memory for
1611 the contents of our special sections. */
1612
b34976b6 1613static bfd_boolean
15bda425
JL
1614elf64_hppa_size_dynamic_sections (output_bfd, info)
1615 bfd *output_bfd;
1616 struct bfd_link_info *info;
1617{
1618 bfd *dynobj;
1619 asection *s;
b34976b6
AM
1620 bfd_boolean plt;
1621 bfd_boolean relocs;
1622 bfd_boolean reltext;
15bda425
JL
1623 struct elf64_hppa_allocate_data data;
1624 struct elf64_hppa_link_hash_table *hppa_info;
1625
1626 hppa_info = elf64_hppa_hash_table (info);
1627
1628 dynobj = elf_hash_table (info)->dynobj;
1629 BFD_ASSERT (dynobj != NULL);
1630
47b7c2db
AM
1631 /* Mark each function this program exports so that we will allocate
1632 space in the .opd section for each function's FPTR. If we are
1633 creating dynamic sections, change the dynamic index of millicode
1634 symbols to -1 and remove them from the string table for .dynstr.
1635
1636 We have to traverse the main linker hash table since we have to
1637 find functions which may not have been mentioned in any relocs. */
1638 elf_link_hash_traverse (elf_hash_table (info),
1639 (elf_hash_table (info)->dynamic_sections_created
1640 ? elf64_hppa_mark_milli_and_exported_functions
1641 : elf64_hppa_mark_exported_functions),
1642 info);
1643
15bda425
JL
1644 if (elf_hash_table (info)->dynamic_sections_created)
1645 {
1646 /* Set the contents of the .interp section to the interpreter. */
893c4fe2 1647 if (info->executable)
15bda425
JL
1648 {
1649 s = bfd_get_section_by_name (dynobj, ".interp");
1650 BFD_ASSERT (s != NULL);
eea6121a 1651 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
15bda425
JL
1652 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1653 }
1654 }
1655 else
1656 {
1657 /* We may have created entries in the .rela.got section.
1658 However, if we are not creating the dynamic sections, we will
1659 not actually use these entries. Reset the size of .rela.dlt,
1660 which will cause it to get stripped from the output file
1661 below. */
1662 s = bfd_get_section_by_name (dynobj, ".rela.dlt");
1663 if (s != NULL)
eea6121a 1664 s->size = 0;
15bda425
JL
1665 }
1666
1667 /* Allocate the GOT entries. */
1668
1669 data.info = info;
1670 if (elf64_hppa_hash_table (info)->dlt_sec)
1671 {
1672 data.ofs = 0x0;
1673 elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1674 allocate_global_data_dlt, &data);
eea6121a 1675 hppa_info->dlt_sec->size = data.ofs;
15bda425
JL
1676
1677 data.ofs = 0x0;
1678 elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1679 allocate_global_data_plt, &data);
eea6121a 1680 hppa_info->plt_sec->size = data.ofs;
15bda425
JL
1681
1682 data.ofs = 0x0;
1683 elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1684 allocate_global_data_stub, &data);
eea6121a 1685 hppa_info->stub_sec->size = data.ofs;
15bda425
JL
1686 }
1687
15bda425
JL
1688 /* Allocate space for entries in the .opd section. */
1689 if (elf64_hppa_hash_table (info)->opd_sec)
1690 {
1691 data.ofs = 0;
1692 elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1693 allocate_global_data_opd, &data);
eea6121a 1694 hppa_info->opd_sec->size = data.ofs;
15bda425
JL
1695 }
1696
1697 /* Now allocate space for dynamic relocations, if necessary. */
1698 if (hppa_info->root.dynamic_sections_created)
1699 elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
1700 allocate_dynrel_entries, &data);
1701
1702 /* The sizes of all the sections are set. Allocate memory for them. */
b34976b6
AM
1703 plt = FALSE;
1704 relocs = FALSE;
1705 reltext = FALSE;
15bda425
JL
1706 for (s = dynobj->sections; s != NULL; s = s->next)
1707 {
1708 const char *name;
b34976b6 1709 bfd_boolean strip;
15bda425
JL
1710
1711 if ((s->flags & SEC_LINKER_CREATED) == 0)
1712 continue;
1713
1714 /* It's OK to base decisions on the section name, because none
1715 of the dynobj section names depend upon the input files. */
1716 name = bfd_get_section_name (dynobj, s);
1717
1718 strip = 0;
1719
1720 if (strcmp (name, ".plt") == 0)
1721 {
d663e1cd 1722 /* Strip this section if we don't need it; see the comment below. */
eea6121a 1723 if (s->size == 0)
15bda425 1724 {
b34976b6 1725 strip = TRUE;
15bda425
JL
1726 }
1727 else
1728 {
1729 /* Remember whether there is a PLT. */
b34976b6 1730 plt = TRUE;
15bda425
JL
1731 }
1732 }
1733 else if (strcmp (name, ".dlt") == 0)
1734 {
d663e1cd 1735 /* Strip this section if we don't need it; see the comment below. */
eea6121a 1736 if (s->size == 0)
15bda425 1737 {
b34976b6 1738 strip = TRUE;
15bda425
JL
1739 }
1740 }
1741 else if (strcmp (name, ".opd") == 0)
1742 {
d663e1cd 1743 /* Strip this section if we don't need it; see the comment below. */
eea6121a 1744 if (s->size == 0)
15bda425 1745 {
b34976b6 1746 strip = TRUE;
15bda425
JL
1747 }
1748 }
d663e1cd 1749 else if (strncmp (name, ".rela", 5) == 0)
15bda425 1750 {
d663e1cd
JL
1751 /* If we don't need this section, strip it from the output file.
1752 This is mostly to handle .rela.bss and .rela.plt. We must
1753 create both sections in create_dynamic_sections, because they
1754 must be created before the linker maps input sections to output
1755 sections. The linker does that before adjust_dynamic_symbol
1756 is called, and it is that function which decides whether
1757 anything needs to go into these sections. */
eea6121a 1758 if (s->size == 0)
15bda425
JL
1759 {
1760 /* If we don't need this section, strip it from the
1761 output file. This is mostly to handle .rela.bss and
1762 .rela.plt. We must create both sections in
1763 create_dynamic_sections, because they must be created
1764 before the linker maps input sections to output
1765 sections. The linker does that before
1766 adjust_dynamic_symbol is called, and it is that
1767 function which decides whether anything needs to go
1768 into these sections. */
b34976b6 1769 strip = TRUE;
15bda425
JL
1770 }
1771 else
1772 {
1773 asection *target;
1774
1775 /* Remember whether there are any reloc sections other
1776 than .rela.plt. */
1777 if (strcmp (name, ".rela.plt") != 0)
1778 {
1779 const char *outname;
1780
b34976b6 1781 relocs = TRUE;
15bda425
JL
1782
1783 /* If this relocation section applies to a read only
1784 section, then we probably need a DT_TEXTREL
1785 entry. The entries in the .rela.plt section
1786 really apply to the .got section, which we
1787 created ourselves and so know is not readonly. */
1788 outname = bfd_get_section_name (output_bfd,
1789 s->output_section);
1790 target = bfd_get_section_by_name (output_bfd, outname + 4);
1791 if (target != NULL
1792 && (target->flags & SEC_READONLY) != 0
1793 && (target->flags & SEC_ALLOC) != 0)
b34976b6 1794 reltext = TRUE;
15bda425
JL
1795 }
1796
1797 /* We use the reloc_count field as a counter if we need
1798 to copy relocs into the output file. */
1799 s->reloc_count = 0;
1800 }
1801 }
1802 else if (strncmp (name, ".dlt", 4) != 0
1803 && strcmp (name, ".stub") != 0
1804 && strcmp (name, ".got") != 0)
1805 {
1806 /* It's not one of our sections, so don't allocate space. */
1807 continue;
1808 }
1809
1810 if (strip)
1811 {
1812 _bfd_strip_section_from_output (info, s);
1813 continue;
1814 }
1815
1816 /* Allocate memory for the section contents if it has not
832d951b
AM
1817 been allocated already. We use bfd_zalloc here in case
1818 unused entries are not reclaimed before the section's
1819 contents are written out. This should not happen, but this
1820 way if it does, we get a R_PARISC_NONE reloc instead of
1821 garbage. */
15bda425
JL
1822 if (s->contents == NULL)
1823 {
eea6121a
AM
1824 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1825 if (s->contents == NULL && s->size != 0)
b34976b6 1826 return FALSE;
15bda425
JL
1827 }
1828 }
1829
1830 if (elf_hash_table (info)->dynamic_sections_created)
1831 {
1832 /* Always create a DT_PLTGOT. It actually has nothing to do with
1833 the PLT, it is how we communicate the __gp value of a load
1834 module to the dynamic linker. */
dc810e39 1835#define add_dynamic_entry(TAG, VAL) \
5a580b3a 1836 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39
AM
1837
1838 if (!add_dynamic_entry (DT_HP_DLD_FLAGS, 0)
1839 || !add_dynamic_entry (DT_PLTGOT, 0))
b34976b6 1840 return FALSE;
15bda425
JL
1841
1842 /* Add some entries to the .dynamic section. We fill in the
1843 values later, in elf64_hppa_finish_dynamic_sections, but we
1844 must add the entries now so that we get the correct size for
1845 the .dynamic section. The DT_DEBUG entry is filled in by the
1846 dynamic linker and used by the debugger. */
1847 if (! info->shared)
1848 {
dc810e39
AM
1849 if (!add_dynamic_entry (DT_DEBUG, 0)
1850 || !add_dynamic_entry (DT_HP_DLD_HOOK, 0)
1851 || !add_dynamic_entry (DT_HP_LOAD_MAP, 0))
b34976b6 1852 return FALSE;
15bda425
JL
1853 }
1854
f2482cb2
NC
1855 /* Force DT_FLAGS to always be set.
1856 Required by HPUX 11.00 patch PHSS_26559. */
1857 if (!add_dynamic_entry (DT_FLAGS, (info)->flags))
b34976b6 1858 return FALSE;
f2482cb2 1859
15bda425
JL
1860 if (plt)
1861 {
dc810e39
AM
1862 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
1863 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1864 || !add_dynamic_entry (DT_JMPREL, 0))
b34976b6 1865 return FALSE;
15bda425
JL
1866 }
1867
1868 if (relocs)
1869 {
dc810e39
AM
1870 if (!add_dynamic_entry (DT_RELA, 0)
1871 || !add_dynamic_entry (DT_RELASZ, 0)
1872 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 1873 return FALSE;
15bda425
JL
1874 }
1875
1876 if (reltext)
1877 {
dc810e39 1878 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 1879 return FALSE;
d6cf2879 1880 info->flags |= DF_TEXTREL;
15bda425
JL
1881 }
1882 }
dc810e39 1883#undef add_dynamic_entry
15bda425 1884
b34976b6 1885 return TRUE;
15bda425
JL
1886}
1887
1888/* Called after we have output the symbol into the dynamic symbol
1889 table, but before we output the symbol into the normal symbol
1890 table.
1891
1892 For some symbols we had to change their address when outputting
1893 the dynamic symbol table. We undo that change here so that
1894 the symbols have their expected value in the normal symbol
1895 table. Ick. */
1896
b34976b6 1897static bfd_boolean
754021d0 1898elf64_hppa_link_output_symbol_hook (info, name, sym, input_sec, h)
15bda425
JL
1899 struct bfd_link_info *info;
1900 const char *name;
1901 Elf_Internal_Sym *sym;
edd21aca 1902 asection *input_sec ATTRIBUTE_UNUSED;
754021d0 1903 struct elf_link_hash_entry *h;
15bda425
JL
1904{
1905 struct elf64_hppa_link_hash_table *hppa_info;
1906 struct elf64_hppa_dyn_hash_entry *dyn_h;
1907
1908 /* We may be called with the file symbol or section symbols.
1909 They never need munging, so it is safe to ignore them. */
1910 if (!name)
b34976b6 1911 return TRUE;
15bda425
JL
1912
1913 /* Get the PA dyn_symbol (if any) associated with NAME. */
1914 hppa_info = elf64_hppa_hash_table (info);
1915 dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
b34976b6 1916 name, FALSE, FALSE);
ac7bbf74 1917 if (!dyn_h || dyn_h->h != h)
754021d0 1918 return TRUE;
15bda425 1919
832d951b
AM
1920 /* Function symbols for which we created .opd entries *may* have been
1921 munged by finish_dynamic_symbol and have to be un-munged here.
1922
1923 Note that finish_dynamic_symbol sometimes turns dynamic symbols
1924 into non-dynamic ones, so we initialize st_shndx to -1 in
1925 mark_exported_functions and check to see if it was overwritten
1926 here instead of just checking dyn_h->h->dynindx. */
ac7bbf74 1927 if (dyn_h->want_opd && dyn_h->st_shndx != -1)
15bda425
JL
1928 {
1929 /* Restore the saved value and section index. */
1930 sym->st_value = dyn_h->st_value;
fe8bc63d 1931 sym->st_shndx = dyn_h->st_shndx;
15bda425
JL
1932 }
1933
b34976b6 1934 return TRUE;
15bda425
JL
1935}
1936
1937/* Finish up dynamic symbol handling. We set the contents of various
1938 dynamic sections here. */
1939
b34976b6 1940static bfd_boolean
15bda425
JL
1941elf64_hppa_finish_dynamic_symbol (output_bfd, info, h, sym)
1942 bfd *output_bfd;
1943 struct bfd_link_info *info;
1944 struct elf_link_hash_entry *h;
1945 Elf_Internal_Sym *sym;
1946{
1947 asection *stub, *splt, *sdlt, *sopd, *spltrel, *sdltrel;
1948 struct elf64_hppa_link_hash_table *hppa_info;
1949 struct elf64_hppa_dyn_hash_entry *dyn_h;
1950
1951 hppa_info = elf64_hppa_hash_table (info);
1952 dyn_h = elf64_hppa_dyn_hash_lookup (&hppa_info->dyn_hash_table,
b34976b6 1953 h->root.root.string, FALSE, FALSE);
15bda425
JL
1954
1955 stub = hppa_info->stub_sec;
1956 splt = hppa_info->plt_sec;
1957 sdlt = hppa_info->dlt_sec;
1958 sopd = hppa_info->opd_sec;
1959 spltrel = hppa_info->plt_rel_sec;
1960 sdltrel = hppa_info->dlt_rel_sec;
1961
15bda425
JL
1962 /* Incredible. It is actually necessary to NOT use the symbol's real
1963 value when building the dynamic symbol table for a shared library.
1964 At least for symbols that refer to functions.
1965
1966 We will store a new value and section index into the symbol long
1967 enough to output it into the dynamic symbol table, then we restore
1968 the original values (in elf64_hppa_link_output_symbol_hook). */
1969 if (dyn_h && dyn_h->want_opd)
1970 {
d663e1cd
JL
1971 BFD_ASSERT (sopd != NULL)
1972
15bda425
JL
1973 /* Save away the original value and section index so that we
1974 can restore them later. */
1975 dyn_h->st_value = sym->st_value;
1976 dyn_h->st_shndx = sym->st_shndx;
1977
1978 /* For the dynamic symbol table entry, we want the value to be
1979 address of this symbol's entry within the .opd section. */
1980 sym->st_value = (dyn_h->opd_offset
1981 + sopd->output_offset
1982 + sopd->output_section->vma);
1983 sym->st_shndx = _bfd_elf_section_from_bfd_section (output_bfd,
1984 sopd->output_section);
1985 }
1986
1987 /* Initialize a .plt entry if requested. */
1988 if (dyn_h && dyn_h->want_plt
1989 && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
1990 {
1991 bfd_vma value;
1992 Elf_Internal_Rela rel;
947216bf 1993 bfd_byte *loc;
15bda425 1994
d663e1cd
JL
1995 BFD_ASSERT (splt != NULL && spltrel != NULL)
1996
15bda425
JL
1997 /* We do not actually care about the value in the PLT entry
1998 if we are creating a shared library and the symbol is
1999 still undefined, we create a dynamic relocation to fill
2000 in the correct value. */
2001 if (info->shared && h->root.type == bfd_link_hash_undefined)
2002 value = 0;
2003 else
2004 value = (h->root.u.def.value + h->root.u.def.section->vma);
2005
fe8bc63d 2006 /* Fill in the entry in the procedure linkage table.
15bda425
JL
2007
2008 The format of a plt entry is
fe8bc63d 2009 <funcaddr> <__gp>.
15bda425
JL
2010
2011 plt_offset is the offset within the PLT section at which to
fe8bc63d 2012 install the PLT entry.
15bda425
JL
2013
2014 We are modifying the in-memory PLT contents here, so we do not add
2015 in the output_offset of the PLT section. */
2016
2017 bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset);
2018 value = _bfd_get_gp_value (splt->output_section->owner);
2019 bfd_put_64 (splt->owner, value, splt->contents + dyn_h->plt_offset + 0x8);
2020
2021 /* Create a dynamic IPLT relocation for this entry.
2022
2023 We are creating a relocation in the output file's PLT section,
2024 which is included within the DLT secton. So we do need to include
2025 the PLT's output_offset in the computation of the relocation's
2026 address. */
2027 rel.r_offset = (dyn_h->plt_offset + splt->output_offset
2028 + splt->output_section->vma);
2029 rel.r_info = ELF64_R_INFO (h->dynindx, R_PARISC_IPLT);
2030 rel.r_addend = 0;
2031
947216bf
AM
2032 loc = spltrel->contents;
2033 loc += spltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2034 bfd_elf64_swap_reloca_out (splt->output_section->owner, &rel, loc);
15bda425
JL
2035 }
2036
2037 /* Initialize an external call stub entry if requested. */
2038 if (dyn_h && dyn_h->want_stub
2039 && elf64_hppa_dynamic_symbol_p (dyn_h->h, info))
2040 {
2041 bfd_vma value;
2042 int insn;
b352eebf 2043 unsigned int max_offset;
15bda425 2044
d663e1cd
JL
2045 BFD_ASSERT (stub != NULL)
2046
15bda425
JL
2047 /* Install the generic stub template.
2048
2049 We are modifying the contents of the stub section, so we do not
2050 need to include the stub section's output_offset here. */
2051 memcpy (stub->contents + dyn_h->stub_offset, plt_stub, sizeof (plt_stub));
2052
2053 /* Fix up the first ldd instruction.
2054
2055 We are modifying the contents of the STUB section in memory,
fe8bc63d 2056 so we do not need to include its output offset in this computation.
15bda425
JL
2057
2058 Note the plt_offset value is the value of the PLT entry relative to
2059 the start of the PLT section. These instructions will reference
2060 data relative to the value of __gp, which may not necessarily have
2061 the same address as the start of the PLT section.
2062
2063 gp_offset contains the offset of __gp within the PLT section. */
2064 value = dyn_h->plt_offset - hppa_info->gp_offset;
fe8bc63d 2065
15bda425 2066 insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset);
b352eebf
AM
2067 if (output_bfd->arch_info->mach >= 25)
2068 {
2069 /* Wide mode allows 16 bit offsets. */
2070 max_offset = 32768;
2071 insn &= ~ 0xfff1;
dc810e39 2072 insn |= re_assemble_16 ((int) value);
b352eebf
AM
2073 }
2074 else
2075 {
2076 max_offset = 8192;
2077 insn &= ~ 0x3ff1;
dc810e39 2078 insn |= re_assemble_14 ((int) value);
b352eebf
AM
2079 }
2080
2081 if ((value & 7) || value + max_offset >= 2*max_offset - 8)
2082 {
2083 (*_bfd_error_handler) (_("stub entry for %s cannot load .plt, dp offset = %ld"),
2084 dyn_h->root.string,
2085 (long) value);
b34976b6 2086 return FALSE;
b352eebf
AM
2087 }
2088
dc810e39 2089 bfd_put_32 (stub->owner, (bfd_vma) insn,
15bda425
JL
2090 stub->contents + dyn_h->stub_offset);
2091
2092 /* Fix up the second ldd instruction. */
b352eebf 2093 value += 8;
15bda425 2094 insn = bfd_get_32 (stub->owner, stub->contents + dyn_h->stub_offset + 8);
b352eebf
AM
2095 if (output_bfd->arch_info->mach >= 25)
2096 {
2097 insn &= ~ 0xfff1;
dc810e39 2098 insn |= re_assemble_16 ((int) value);
b352eebf
AM
2099 }
2100 else
2101 {
2102 insn &= ~ 0x3ff1;
dc810e39 2103 insn |= re_assemble_14 ((int) value);
b352eebf 2104 }
dc810e39 2105 bfd_put_32 (stub->owner, (bfd_vma) insn,
15bda425
JL
2106 stub->contents + dyn_h->stub_offset + 8);
2107 }
2108
b34976b6 2109 return TRUE;
15bda425
JL
2110}
2111
2112/* The .opd section contains FPTRs for each function this file
2113 exports. Initialize the FPTR entries. */
2114
b34976b6 2115static bfd_boolean
15bda425
JL
2116elf64_hppa_finalize_opd (dyn_h, data)
2117 struct elf64_hppa_dyn_hash_entry *dyn_h;
2118 PTR data;
2119{
2120 struct bfd_link_info *info = (struct bfd_link_info *)data;
2121 struct elf64_hppa_link_hash_table *hppa_info;
3db4b612 2122 struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
15bda425
JL
2123 asection *sopd;
2124 asection *sopdrel;
2125
2126 hppa_info = elf64_hppa_hash_table (info);
2127 sopd = hppa_info->opd_sec;
2128 sopdrel = hppa_info->opd_rel_sec;
2129
3db4b612 2130 if (h && dyn_h->want_opd)
15bda425
JL
2131 {
2132 bfd_vma value;
2133
fe8bc63d 2134 /* The first two words of an .opd entry are zero.
15bda425
JL
2135
2136 We are modifying the contents of the OPD section in memory, so we
2137 do not need to include its output offset in this computation. */
2138 memset (sopd->contents + dyn_h->opd_offset, 0, 16);
2139
2140 value = (h->root.u.def.value
2141 + h->root.u.def.section->output_section->vma
2142 + h->root.u.def.section->output_offset);
2143
2144 /* The next word is the address of the function. */
2145 bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 16);
2146
2147 /* The last word is our local __gp value. */
2148 value = _bfd_get_gp_value (sopd->output_section->owner);
2149 bfd_put_64 (sopd->owner, value, sopd->contents + dyn_h->opd_offset + 24);
2150 }
2151
2152 /* If we are generating a shared library, we must generate EPLT relocations
2153 for each entry in the .opd, even for static functions (they may have
2154 had their address taken). */
2155 if (info->shared && dyn_h && dyn_h->want_opd)
2156 {
947216bf
AM
2157 Elf_Internal_Rela rel;
2158 bfd_byte *loc;
15bda425
JL
2159 int dynindx;
2160
2161 /* We may need to do a relocation against a local symbol, in
2162 which case we have to look up it's dynamic symbol index off
2163 the local symbol hash table. */
2164 if (h && h->dynindx != -1)
2165 dynindx = h->dynindx;
2166 else
2167 dynindx
2168 = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2169 dyn_h->sym_indx);
2170
2171 /* The offset of this relocation is the absolute address of the
2172 .opd entry for this symbol. */
2173 rel.r_offset = (dyn_h->opd_offset + sopd->output_offset
2174 + sopd->output_section->vma);
2175
2176 /* If H is non-null, then we have an external symbol.
2177
2178 It is imperative that we use a different dynamic symbol for the
2179 EPLT relocation if the symbol has global scope.
2180
2181 In the dynamic symbol table, the function symbol will have a value
2182 which is address of the function's .opd entry.
2183
2184 Thus, we can not use that dynamic symbol for the EPLT relocation
2185 (if we did, the data in the .opd would reference itself rather
2186 than the actual address of the function). Instead we have to use
2187 a new dynamic symbol which has the same value as the original global
fe8bc63d 2188 function symbol.
15bda425
JL
2189
2190 We prefix the original symbol with a "." and use the new symbol in
2191 the EPLT relocation. This new symbol has already been recorded in
2192 the symbol table, we just have to look it up and use it.
2193
2194 We do not have such problems with static functions because we do
2195 not make their addresses in the dynamic symbol table point to
2196 the .opd entry. Ultimately this should be safe since a static
2197 function can not be directly referenced outside of its shared
2198 library.
2199
2200 We do have to play similar games for FPTR relocations in shared
2201 libraries, including those for static symbols. See the FPTR
2202 handling in elf64_hppa_finalize_dynreloc. */
2203 if (h)
2204 {
2205 char *new_name;
2206 struct elf_link_hash_entry *nh;
2207
2208 new_name = alloca (strlen (h->root.root.string) + 2);
2209 new_name[0] = '.';
2210 strcpy (new_name + 1, h->root.root.string);
2211
2212 nh = elf_link_hash_lookup (elf_hash_table (info),
b34976b6 2213 new_name, FALSE, FALSE, FALSE);
15bda425
JL
2214
2215 /* All we really want from the new symbol is its dynamic
2216 symbol index. */
2217 dynindx = nh->dynindx;
2218 }
2219
2220 rel.r_addend = 0;
2221 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_EPLT);
2222
947216bf
AM
2223 loc = sopdrel->contents;
2224 loc += sopdrel->reloc_count++ * sizeof (Elf64_External_Rela);
2225 bfd_elf64_swap_reloca_out (sopd->output_section->owner, &rel, loc);
15bda425 2226 }
b34976b6 2227 return TRUE;
15bda425
JL
2228}
2229
2230/* The .dlt section contains addresses for items referenced through the
2231 dlt. Note that we can have a DLTIND relocation for a local symbol, thus
2232 we can not depend on finish_dynamic_symbol to initialize the .dlt. */
2233
b34976b6 2234static bfd_boolean
15bda425
JL
2235elf64_hppa_finalize_dlt (dyn_h, data)
2236 struct elf64_hppa_dyn_hash_entry *dyn_h;
2237 PTR data;
2238{
2239 struct bfd_link_info *info = (struct bfd_link_info *)data;
2240 struct elf64_hppa_link_hash_table *hppa_info;
2241 asection *sdlt, *sdltrel;
3db4b612 2242 struct elf_link_hash_entry *h = dyn_h ? dyn_h->h : NULL;
15bda425
JL
2243
2244 hppa_info = elf64_hppa_hash_table (info);
2245
2246 sdlt = hppa_info->dlt_sec;
2247 sdltrel = hppa_info->dlt_rel_sec;
2248
2249 /* H/DYN_H may refer to a local variable and we know it's
2250 address, so there is no need to create a relocation. Just install
2251 the proper value into the DLT, note this shortcut can not be
2252 skipped when building a shared library. */
3db4b612 2253 if (! info->shared && h && dyn_h->want_dlt)
15bda425
JL
2254 {
2255 bfd_vma value;
2256
2257 /* If we had an LTOFF_FPTR style relocation we want the DLT entry
fe8bc63d 2258 to point to the FPTR entry in the .opd section.
15bda425
JL
2259
2260 We include the OPD's output offset in this computation as
2261 we are referring to an absolute address in the resulting
2262 object file. */
2263 if (dyn_h->want_opd)
2264 {
2265 value = (dyn_h->opd_offset
2266 + hppa_info->opd_sec->output_offset
2267 + hppa_info->opd_sec->output_section->vma);
2268 }
37f4508b
AM
2269 else if ((h->root.type == bfd_link_hash_defined
2270 || h->root.type == bfd_link_hash_defweak)
2271 && h->root.u.def.section)
15bda425 2272 {
3db4b612 2273 value = h->root.u.def.value + h->root.u.def.section->output_offset;
15bda425
JL
2274 if (h->root.u.def.section->output_section)
2275 value += h->root.u.def.section->output_section->vma;
2276 else
2277 value += h->root.u.def.section->vma;
2278 }
3db4b612
JL
2279 else
2280 /* We have an undefined function reference. */
2281 value = 0;
15bda425
JL
2282
2283 /* We do not need to include the output offset of the DLT section
2284 here because we are modifying the in-memory contents. */
2285 bfd_put_64 (sdlt->owner, value, sdlt->contents + dyn_h->dlt_offset);
2286 }
2287
4cc11e76 2288 /* Create a relocation for the DLT entry associated with this symbol.
15bda425
JL
2289 When building a shared library the symbol does not have to be dynamic. */
2290 if (dyn_h->want_dlt
2291 && (elf64_hppa_dynamic_symbol_p (dyn_h->h, info) || info->shared))
2292 {
947216bf
AM
2293 Elf_Internal_Rela rel;
2294 bfd_byte *loc;
15bda425
JL
2295 int dynindx;
2296
2297 /* We may need to do a relocation against a local symbol, in
2298 which case we have to look up it's dynamic symbol index off
2299 the local symbol hash table. */
2300 if (h && h->dynindx != -1)
2301 dynindx = h->dynindx;
2302 else
2303 dynindx
2304 = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2305 dyn_h->sym_indx);
2306
15bda425
JL
2307 /* Create a dynamic relocation for this entry. Do include the output
2308 offset of the DLT entry since we need an absolute address in the
2309 resulting object file. */
2310 rel.r_offset = (dyn_h->dlt_offset + sdlt->output_offset
2311 + sdlt->output_section->vma);
2312 if (h && h->type == STT_FUNC)
2313 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_FPTR64);
2314 else
2315 rel.r_info = ELF64_R_INFO (dynindx, R_PARISC_DIR64);
2316 rel.r_addend = 0;
2317
947216bf
AM
2318 loc = sdltrel->contents;
2319 loc += sdltrel->reloc_count++ * sizeof (Elf64_External_Rela);
2320 bfd_elf64_swap_reloca_out (sdlt->output_section->owner, &rel, loc);
15bda425 2321 }
b34976b6 2322 return TRUE;
15bda425
JL
2323}
2324
2325/* Finalize the dynamic relocations. Specifically the FPTR relocations
2326 for dynamic functions used to initialize static data. */
2327
b34976b6 2328static bfd_boolean
15bda425
JL
2329elf64_hppa_finalize_dynreloc (dyn_h, data)
2330 struct elf64_hppa_dyn_hash_entry *dyn_h;
2331 PTR data;
2332{
2333 struct bfd_link_info *info = (struct bfd_link_info *)data;
2334 struct elf64_hppa_link_hash_table *hppa_info;
2335 struct elf_link_hash_entry *h;
2336 int dynamic_symbol;
2337
2338 dynamic_symbol = elf64_hppa_dynamic_symbol_p (dyn_h->h, info);
2339
2340 if (!dynamic_symbol && !info->shared)
b34976b6 2341 return TRUE;
15bda425
JL
2342
2343 if (dyn_h->reloc_entries)
2344 {
2345 struct elf64_hppa_dyn_reloc_entry *rent;
2346 int dynindx;
2347
2348 hppa_info = elf64_hppa_hash_table (info);
2349 h = dyn_h->h;
2350
2351 /* We may need to do a relocation against a local symbol, in
2352 which case we have to look up it's dynamic symbol index off
2353 the local symbol hash table. */
2354 if (h && h->dynindx != -1)
2355 dynindx = h->dynindx;
2356 else
2357 dynindx
2358 = _bfd_elf_link_lookup_local_dynindx (info, dyn_h->owner,
2359 dyn_h->sym_indx);
2360
2361 for (rent = dyn_h->reloc_entries; rent; rent = rent->next)
2362 {
947216bf
AM
2363 Elf_Internal_Rela rel;
2364 bfd_byte *loc;
15bda425 2365
d663e1cd
JL
2366 /* Allocate one iff we are building a shared library, the relocation
2367 isn't a R_PARISC_FPTR64, or we don't want an opd entry. */
2368 if (!info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
2369 continue;
15bda425 2370
fe8bc63d 2371 /* Create a dynamic relocation for this entry.
15bda425
JL
2372
2373 We need the output offset for the reloc's section because
2374 we are creating an absolute address in the resulting object
2375 file. */
2376 rel.r_offset = (rent->offset + rent->sec->output_offset
2377 + rent->sec->output_section->vma);
2378
2379 /* An FPTR64 relocation implies that we took the address of
2380 a function and that the function has an entry in the .opd
2381 section. We want the FPTR64 relocation to reference the
2382 entry in .opd.
2383
2384 We could munge the symbol value in the dynamic symbol table
2385 (in fact we already do for functions with global scope) to point
2386 to the .opd entry. Then we could use that dynamic symbol in
2387 this relocation.
2388
2389 Or we could do something sensible, not munge the symbol's
2390 address and instead just use a different symbol to reference
2391 the .opd entry. At least that seems sensible until you
2392 realize there's no local dynamic symbols we can use for that
2393 purpose. Thus the hair in the check_relocs routine.
fe8bc63d 2394
15bda425
JL
2395 We use a section symbol recorded by check_relocs as the
2396 base symbol for the relocation. The addend is the difference
2397 between the section symbol and the address of the .opd entry. */
3db4b612 2398 if (info->shared && rent->type == R_PARISC_FPTR64 && dyn_h->want_opd)
15bda425
JL
2399 {
2400 bfd_vma value, value2;
15bda425
JL
2401
2402 /* First compute the address of the opd entry for this symbol. */
2403 value = (dyn_h->opd_offset
2404 + hppa_info->opd_sec->output_section->vma
2405 + hppa_info->opd_sec->output_offset);
2406
2407 /* Compute the value of the start of the section with
2408 the relocation. */
2409 value2 = (rent->sec->output_section->vma
2410 + rent->sec->output_offset);
2411
2412 /* Compute the difference between the start of the section
2413 with the relocation and the opd entry. */
2414 value -= value2;
fe8bc63d 2415
15bda425
JL
2416 /* The result becomes the addend of the relocation. */
2417 rel.r_addend = value;
2418
2419 /* The section symbol becomes the symbol for the dynamic
2420 relocation. */
2421 dynindx
2422 = _bfd_elf_link_lookup_local_dynindx (info,
2423 rent->sec->owner,
2424 rent->sec_symndx);
2425 }
2426 else
2427 rel.r_addend = rent->addend;
2428
2429 rel.r_info = ELF64_R_INFO (dynindx, rent->type);
2430
947216bf
AM
2431 loc = hppa_info->other_rel_sec->contents;
2432 loc += (hppa_info->other_rel_sec->reloc_count++
2433 * sizeof (Elf64_External_Rela));
15bda425 2434 bfd_elf64_swap_reloca_out (hppa_info->other_rel_sec->output_section->owner,
947216bf 2435 &rel, loc);
15bda425
JL
2436 }
2437 }
2438
b34976b6 2439 return TRUE;
15bda425
JL
2440}
2441
5ac81c74
JL
2442/* Used to decide how to sort relocs in an optimal manner for the
2443 dynamic linker, before writing them out. */
2444
2445static enum elf_reloc_type_class
2446elf64_hppa_reloc_type_class (rela)
2447 const Elf_Internal_Rela *rela;
2448{
2449 if (ELF64_R_SYM (rela->r_info) == 0)
2450 return reloc_class_relative;
2451
2452 switch ((int) ELF64_R_TYPE (rela->r_info))
2453 {
2454 case R_PARISC_IPLT:
2455 return reloc_class_plt;
2456 case R_PARISC_COPY:
2457 return reloc_class_copy;
2458 default:
2459 return reloc_class_normal;
2460 }
2461}
2462
15bda425
JL
2463/* Finish up the dynamic sections. */
2464
b34976b6 2465static bfd_boolean
15bda425
JL
2466elf64_hppa_finish_dynamic_sections (output_bfd, info)
2467 bfd *output_bfd;
2468 struct bfd_link_info *info;
2469{
2470 bfd *dynobj;
2471 asection *sdyn;
2472 struct elf64_hppa_link_hash_table *hppa_info;
2473
2474 hppa_info = elf64_hppa_hash_table (info);
2475
2476 /* Finalize the contents of the .opd section. */
2477 elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2478 elf64_hppa_finalize_opd,
2479 info);
2480
2481 elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2482 elf64_hppa_finalize_dynreloc,
2483 info);
2484
2485 /* Finalize the contents of the .dlt section. */
2486 dynobj = elf_hash_table (info)->dynobj;
2487 /* Finalize the contents of the .dlt section. */
2488 elf64_hppa_dyn_hash_traverse (&hppa_info->dyn_hash_table,
2489 elf64_hppa_finalize_dlt,
2490 info);
2491
15bda425
JL
2492 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2493
2494 if (elf_hash_table (info)->dynamic_sections_created)
2495 {
2496 Elf64_External_Dyn *dyncon, *dynconend;
15bda425
JL
2497
2498 BFD_ASSERT (sdyn != NULL);
2499
2500 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 2501 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
15bda425
JL
2502 for (; dyncon < dynconend; dyncon++)
2503 {
2504 Elf_Internal_Dyn dyn;
2505 asection *s;
2506
2507 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2508
2509 switch (dyn.d_tag)
2510 {
2511 default:
2512 break;
2513
2514 case DT_HP_LOAD_MAP:
2515 /* Compute the absolute address of 16byte scratchpad area
2516 for the dynamic linker.
2517
2518 By convention the linker script will allocate the scratchpad
2519 area at the start of the .data section. So all we have to
2520 to is find the start of the .data section. */
2521 s = bfd_get_section_by_name (output_bfd, ".data");
2522 dyn.d_un.d_ptr = s->vma;
2523 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2524 break;
2525
2526 case DT_PLTGOT:
2527 /* HP's use PLTGOT to set the GOT register. */
2528 dyn.d_un.d_ptr = _bfd_get_gp_value (output_bfd);
2529 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2530 break;
2531
2532 case DT_JMPREL:
2533 s = hppa_info->plt_rel_sec;
2534 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2535 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2536 break;
2537
2538 case DT_PLTRELSZ:
2539 s = hppa_info->plt_rel_sec;
eea6121a 2540 dyn.d_un.d_val = s->size;
15bda425
JL
2541 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2542 break;
2543
2544 case DT_RELA:
2545 s = hppa_info->other_rel_sec;
eea6121a 2546 if (! s || ! s->size)
15bda425 2547 s = hppa_info->dlt_rel_sec;
eea6121a 2548 if (! s || ! s->size)
5ac81c74 2549 s = hppa_info->opd_rel_sec;
15bda425
JL
2550 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2551 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2552 break;
2553
2554 case DT_RELASZ:
2555 s = hppa_info->other_rel_sec;
eea6121a 2556 dyn.d_un.d_val = s->size;
15bda425 2557 s = hppa_info->dlt_rel_sec;
eea6121a 2558 dyn.d_un.d_val += s->size;
15bda425 2559 s = hppa_info->opd_rel_sec;
eea6121a 2560 dyn.d_un.d_val += s->size;
15bda425
JL
2561 /* There is some question about whether or not the size of
2562 the PLT relocs should be included here. HP's tools do
2563 it, so we'll emulate them. */
2564 s = hppa_info->plt_rel_sec;
eea6121a 2565 dyn.d_un.d_val += s->size;
15bda425
JL
2566 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2567 break;
2568
2569 }
2570 }
2571 }
2572
b34976b6 2573 return TRUE;
15bda425
JL
2574}
2575
15bda425
JL
2576/* Return the number of additional phdrs we will need.
2577
2578 The generic ELF code only creates PT_PHDRs for executables. The HP
fe8bc63d 2579 dynamic linker requires PT_PHDRs for dynamic libraries too.
15bda425
JL
2580
2581 This routine indicates that the backend needs one additional program
2582 header for that case.
2583
2584 Note we do not have access to the link info structure here, so we have
2585 to guess whether or not we are building a shared library based on the
2586 existence of a .interp section. */
2587
2588static int
2589elf64_hppa_additional_program_headers (abfd)
2590 bfd *abfd;
2591{
2592 asection *s;
2593
2594 /* If we are creating a shared library, then we have to create a
2595 PT_PHDR segment. HP's dynamic linker chokes without it. */
2596 s = bfd_get_section_by_name (abfd, ".interp");
2597 if (! s)
2598 return 1;
2599 return 0;
2600}
2601
2602/* Allocate and initialize any program headers required by this
2603 specific backend.
2604
2605 The generic ELF code only creates PT_PHDRs for executables. The HP
fe8bc63d 2606 dynamic linker requires PT_PHDRs for dynamic libraries too.
15bda425
JL
2607
2608 This allocates the PT_PHDR and initializes it in a manner suitable
fe8bc63d 2609 for the HP linker.
15bda425
JL
2610
2611 Note we do not have access to the link info structure here, so we have
2612 to guess whether or not we are building a shared library based on the
2613 existence of a .interp section. */
2614
b34976b6 2615static bfd_boolean
c84fca4d 2616elf64_hppa_modify_segment_map (abfd, info)
15bda425 2617 bfd *abfd;
c84fca4d 2618 struct bfd_link_info *info ATTRIBUTE_UNUSED;
15bda425 2619{
edd21aca 2620 struct elf_segment_map *m;
15bda425
JL
2621 asection *s;
2622
2623 s = bfd_get_section_by_name (abfd, ".interp");
2624 if (! s)
2625 {
2626 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2627 if (m->p_type == PT_PHDR)
2628 break;
2629 if (m == NULL)
2630 {
dc810e39
AM
2631 m = ((struct elf_segment_map *)
2632 bfd_zalloc (abfd, (bfd_size_type) sizeof *m));
15bda425 2633 if (m == NULL)
b34976b6 2634 return FALSE;
15bda425
JL
2635
2636 m->p_type = PT_PHDR;
2637 m->p_flags = PF_R | PF_X;
2638 m->p_flags_valid = 1;
2639 m->p_paddr_valid = 1;
2640 m->includes_phdrs = 1;
2641
2642 m->next = elf_tdata (abfd)->segment_map;
2643 elf_tdata (abfd)->segment_map = m;
2644 }
2645 }
2646
2647 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next)
2648 if (m->p_type == PT_LOAD)
2649 {
0ba2a60e 2650 unsigned int i;
15bda425
JL
2651
2652 for (i = 0; i < m->count; i++)
2653 {
2654 /* The code "hint" is not really a hint. It is a requirement
2655 for certain versions of the HP dynamic linker. Worse yet,
2656 it must be set even if the shared library does not have
2657 any code in its "text" segment (thus the check for .hash
2658 to catch this situation). */
2659 if (m->sections[i]->flags & SEC_CODE
2660 || (strcmp (m->sections[i]->name, ".hash") == 0))
2661 m->p_flags |= (PF_X | PF_HP_CODE);
2662 }
2663 }
2664
b34976b6 2665 return TRUE;
15bda425
JL
2666}
2667
3fab46d0
AM
2668/* Called when writing out an object file to decide the type of a
2669 symbol. */
2670static int
2671elf64_hppa_elf_get_symbol_type (elf_sym, type)
2672 Elf_Internal_Sym *elf_sym;
2673 int type;
2674{
2675 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
2676 return STT_PARISC_MILLI;
2677 else
2678 return type;
2679}
2680
2f89ff8d
L
2681static struct bfd_elf_special_section const elf64_hppa_special_sections[]=
2682{
7dcb9820
AM
2683 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2684 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2685 { NULL, 0, 0, 0, 0 }
2f89ff8d
L
2686};
2687
15bda425
JL
2688/* The hash bucket size is the standard one, namely 4. */
2689
2690const struct elf_size_info hppa64_elf_size_info =
2691{
2692 sizeof (Elf64_External_Ehdr),
2693 sizeof (Elf64_External_Phdr),
2694 sizeof (Elf64_External_Shdr),
2695 sizeof (Elf64_External_Rel),
2696 sizeof (Elf64_External_Rela),
2697 sizeof (Elf64_External_Sym),
2698 sizeof (Elf64_External_Dyn),
2699 sizeof (Elf_External_Note),
2700 4,
2701 1,
45d6a902 2702 64, 3,
15bda425
JL
2703 ELFCLASS64, EV_CURRENT,
2704 bfd_elf64_write_out_phdrs,
2705 bfd_elf64_write_shdrs_and_ehdr,
2706 bfd_elf64_write_relocs,
73ff0d56 2707 bfd_elf64_swap_symbol_in,
15bda425
JL
2708 bfd_elf64_swap_symbol_out,
2709 bfd_elf64_slurp_reloc_table,
2710 bfd_elf64_slurp_symbol_table,
2711 bfd_elf64_swap_dyn_in,
2712 bfd_elf64_swap_dyn_out,
947216bf
AM
2713 bfd_elf64_swap_reloc_in,
2714 bfd_elf64_swap_reloc_out,
2715 bfd_elf64_swap_reloca_in,
2716 bfd_elf64_swap_reloca_out
15bda425
JL
2717};
2718
2719#define TARGET_BIG_SYM bfd_elf64_hppa_vec
2720#define TARGET_BIG_NAME "elf64-hppa"
2721#define ELF_ARCH bfd_arch_hppa
2722#define ELF_MACHINE_CODE EM_PARISC
2723/* This is not strictly correct. The maximum page size for PA2.0 is
2724 64M. But everything still uses 4k. */
2725#define ELF_MAXPAGESIZE 0x1000
2726#define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
2727#define bfd_elf64_bfd_is_local_label_name elf_hppa_is_local_label_name
2728#define elf_info_to_howto elf_hppa_info_to_howto
2729#define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
2730
2731#define elf_backend_section_from_shdr elf64_hppa_section_from_shdr
2732#define elf_backend_object_p elf64_hppa_object_p
2733#define elf_backend_final_write_processing \
2734 elf_hppa_final_write_processing
99c79b2e 2735#define elf_backend_fake_sections elf_hppa_fake_sections
15bda425
JL
2736#define elf_backend_add_symbol_hook elf_hppa_add_symbol_hook
2737
f0fe0e16 2738#define elf_backend_relocate_section elf_hppa_relocate_section
15bda425
JL
2739
2740#define bfd_elf64_bfd_final_link elf_hppa_final_link
2741
2742#define elf_backend_create_dynamic_sections \
2743 elf64_hppa_create_dynamic_sections
2744#define elf_backend_post_process_headers elf64_hppa_post_process_headers
2745
2746#define elf_backend_adjust_dynamic_symbol \
2747 elf64_hppa_adjust_dynamic_symbol
2748
2749#define elf_backend_size_dynamic_sections \
2750 elf64_hppa_size_dynamic_sections
2751
2752#define elf_backend_finish_dynamic_symbol \
2753 elf64_hppa_finish_dynamic_symbol
2754#define elf_backend_finish_dynamic_sections \
2755 elf64_hppa_finish_dynamic_sections
2756
2757/* Stuff for the BFD linker: */
2758#define bfd_elf64_bfd_link_hash_table_create \
2759 elf64_hppa_hash_table_create
2760
2761#define elf_backend_check_relocs \
2762 elf64_hppa_check_relocs
2763
2764#define elf_backend_size_info \
2765 hppa64_elf_size_info
2766
2767#define elf_backend_additional_program_headers \
2768 elf64_hppa_additional_program_headers
2769
2770#define elf_backend_modify_segment_map \
2771 elf64_hppa_modify_segment_map
2772
2773#define elf_backend_link_output_symbol_hook \
2774 elf64_hppa_link_output_symbol_hook
2775
15bda425
JL
2776#define elf_backend_want_got_plt 0
2777#define elf_backend_plt_readonly 0
2778#define elf_backend_want_plt_sym 0
2779#define elf_backend_got_header_size 0
b34976b6
AM
2780#define elf_backend_type_change_ok TRUE
2781#define elf_backend_get_symbol_type elf64_hppa_elf_get_symbol_type
2782#define elf_backend_reloc_type_class elf64_hppa_reloc_type_class
2783#define elf_backend_rela_normal 1
2f89ff8d 2784#define elf_backend_special_sections elf64_hppa_special_sections
15bda425
JL
2785
2786#include "elf64-target.h"
d952f17a
AM
2787
2788#undef TARGET_BIG_SYM
2789#define TARGET_BIG_SYM bfd_elf64_hppa_linux_vec
2790#undef TARGET_BIG_NAME
2791#define TARGET_BIG_NAME "elf64-hppa-linux"
2792
2f89ff8d
L
2793#undef elf_backend_special_sections
2794
d952f17a
AM
2795#define INCLUDED_TARGET_FILE 1
2796#include "elf64-target.h"