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