* elf-hppa.h (elf_hppa_final_link): Use elf_hppa_final_link.
[deliverable/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 static const struct bfd_elf_special_section elf64_hppa_special_sections[] =
2815 {
2816 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2817 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2818 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2819 { STRING_COMMA_LEN (".dlt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2820 { STRING_COMMA_LEN (".sdata"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2821 { STRING_COMMA_LEN (".sbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_PARISC_SHORT },
2822 { STRING_COMMA_LEN (".tbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_HP_TLS },
2823 { NULL, 0, 0, 0, 0 }
2824 };
2825
2826 /* The hash bucket size is the standard one, namely 4. */
2827
2828 const struct elf_size_info hppa64_elf_size_info =
2829 {
2830 sizeof (Elf64_External_Ehdr),
2831 sizeof (Elf64_External_Phdr),
2832 sizeof (Elf64_External_Shdr),
2833 sizeof (Elf64_External_Rel),
2834 sizeof (Elf64_External_Rela),
2835 sizeof (Elf64_External_Sym),
2836 sizeof (Elf64_External_Dyn),
2837 sizeof (Elf_External_Note),
2838 4,
2839 1,
2840 64, 3,
2841 ELFCLASS64, EV_CURRENT,
2842 bfd_elf64_write_out_phdrs,
2843 bfd_elf64_write_shdrs_and_ehdr,
2844 bfd_elf64_checksum_contents,
2845 bfd_elf64_write_relocs,
2846 bfd_elf64_swap_symbol_in,
2847 bfd_elf64_swap_symbol_out,
2848 bfd_elf64_slurp_reloc_table,
2849 bfd_elf64_slurp_symbol_table,
2850 bfd_elf64_swap_dyn_in,
2851 bfd_elf64_swap_dyn_out,
2852 bfd_elf64_swap_reloc_in,
2853 bfd_elf64_swap_reloc_out,
2854 bfd_elf64_swap_reloca_in,
2855 bfd_elf64_swap_reloca_out
2856 };
2857
2858 #define TARGET_BIG_SYM bfd_elf64_hppa_vec
2859 #define TARGET_BIG_NAME "elf64-hppa"
2860 #define ELF_ARCH bfd_arch_hppa
2861 #define ELF_MACHINE_CODE EM_PARISC
2862 /* This is not strictly correct. The maximum page size for PA2.0 is
2863 64M. But everything still uses 4k. */
2864 #define ELF_MAXPAGESIZE 0x1000
2865 #define ELF_OSABI ELFOSABI_HPUX
2866
2867 #define bfd_elf64_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
2868 #define bfd_elf64_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
2869 #define bfd_elf64_bfd_is_local_label_name elf_hppa_is_local_label_name
2870 #define elf_info_to_howto elf_hppa_info_to_howto
2871 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
2872
2873 #define elf_backend_section_from_shdr elf64_hppa_section_from_shdr
2874 #define elf_backend_object_p elf64_hppa_object_p
2875 #define elf_backend_final_write_processing \
2876 elf_hppa_final_write_processing
2877 #define elf_backend_fake_sections elf_hppa_fake_sections
2878 #define elf_backend_add_symbol_hook elf_hppa_add_symbol_hook
2879
2880 #define elf_backend_relocate_section elf_hppa_relocate_section
2881
2882 #define bfd_elf64_bfd_final_link elf_hppa_final_link
2883
2884 #define elf_backend_create_dynamic_sections \
2885 elf64_hppa_create_dynamic_sections
2886 #define elf_backend_post_process_headers elf64_hppa_post_process_headers
2887
2888 #define elf_backend_omit_section_dynsym \
2889 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
2890 #define elf_backend_adjust_dynamic_symbol \
2891 elf64_hppa_adjust_dynamic_symbol
2892
2893 #define elf_backend_size_dynamic_sections \
2894 elf64_hppa_size_dynamic_sections
2895
2896 #define elf_backend_finish_dynamic_symbol \
2897 elf64_hppa_finish_dynamic_symbol
2898 #define elf_backend_finish_dynamic_sections \
2899 elf64_hppa_finish_dynamic_sections
2900 #define elf_backend_grok_prstatus elf64_hppa_grok_prstatus
2901 #define elf_backend_grok_psinfo elf64_hppa_grok_psinfo
2902
2903 /* Stuff for the BFD linker: */
2904 #define bfd_elf64_bfd_link_hash_table_create \
2905 elf64_hppa_hash_table_create
2906
2907 #define elf_backend_check_relocs \
2908 elf64_hppa_check_relocs
2909
2910 #define elf_backend_size_info \
2911 hppa64_elf_size_info
2912
2913 #define elf_backend_additional_program_headers \
2914 elf64_hppa_additional_program_headers
2915
2916 #define elf_backend_modify_segment_map \
2917 elf64_hppa_modify_segment_map
2918
2919 #define elf_backend_link_output_symbol_hook \
2920 elf64_hppa_link_output_symbol_hook
2921
2922 #define elf_backend_want_got_plt 0
2923 #define elf_backend_plt_readonly 0
2924 #define elf_backend_want_plt_sym 0
2925 #define elf_backend_got_header_size 0
2926 #define elf_backend_type_change_ok TRUE
2927 #define elf_backend_get_symbol_type elf64_hppa_elf_get_symbol_type
2928 #define elf_backend_reloc_type_class elf64_hppa_reloc_type_class
2929 #define elf_backend_rela_normal 1
2930 #define elf_backend_special_sections elf64_hppa_special_sections
2931 #define elf_backend_action_discarded elf_hppa_action_discarded
2932 #define elf_backend_section_from_phdr elf64_hppa_section_from_phdr
2933
2934 #define elf64_bed elf64_hppa_hpux_bed
2935
2936 #include "elf64-target.h"
2937
2938 #undef TARGET_BIG_SYM
2939 #define TARGET_BIG_SYM bfd_elf64_hppa_linux_vec
2940 #undef TARGET_BIG_NAME
2941 #define TARGET_BIG_NAME "elf64-hppa-linux"
2942 #undef ELF_OSABI
2943 #define ELF_OSABI ELFOSABI_LINUX
2944 #undef elf_backend_post_process_headers
2945 #define elf_backend_post_process_headers _bfd_elf_set_osabi
2946 #undef elf64_bed
2947 #define elf64_bed elf64_hppa_linux_bed
2948
2949 #include "elf64-target.h"
This page took 0.085975 seconds and 5 git commands to generate.