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