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