1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
7 Center for Software Science
8 Department of Computer Science
10 Largely rewritten by Alan Modra <alan@linuxcare.com.au>
11 Naming cleanup by Carlos O'Donell <carlos@systemhalted.org>
12 TLS support written by Randolph Chung <tausq@debian.org>
14 This file is part of BFD, the Binary File Descriptor library.
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
29 MA 02110-1301, USA. */
37 #include "elf32-hppa.h"
39 #include "elf32-hppa.h"
42 /* In order to gain some understanding of code in this file without
43 knowing all the intricate details of the linker, note the
46 Functions named elf32_hppa_* are called by external routines, other
47 functions are only called locally. elf32_hppa_* functions appear
48 in this file more or less in the order in which they are called
49 from external routines. eg. elf32_hppa_check_relocs is called
50 early in the link process, elf32_hppa_finish_dynamic_sections is
51 one of the last functions. */
53 /* We use two hash tables to hold information for linking PA ELF objects.
55 The first is the elf32_hppa_link_hash_table which is derived
56 from the standard ELF linker hash table. We use this as a place to
57 attach other hash tables and static information.
59 The second is the stub hash table which is derived from the
60 base BFD hash table. The stub hash table holds the information
61 necessary to build the linker stubs during a link.
63 There are a number of different stubs generated by the linker.
71 : addil LR'X - ($PIC_pcrel$0 - 4),%r1
72 : be,n RR'X - ($PIC_pcrel$0 - 8)(%sr4,%r1)
74 Import stub to call shared library routine from normal object file
75 (single sub-space version)
76 : addil LR'lt_ptr+ltoff,%dp ; get procedure entry point
77 : ldw RR'lt_ptr+ltoff(%r1),%r21
79 : ldw RR'lt_ptr+ltoff+4(%r1),%r19 ; get new dlt value.
81 Import stub to call shared library routine from shared library
82 (single sub-space version)
83 : addil LR'ltoff,%r19 ; get procedure entry point
84 : ldw RR'ltoff(%r1),%r21
86 : ldw RR'ltoff+4(%r1),%r19 ; get new dlt value.
88 Import stub to call shared library routine from normal object file
89 (multiple sub-space support)
90 : addil LR'lt_ptr+ltoff,%dp ; get procedure entry point
91 : ldw RR'lt_ptr+ltoff(%r1),%r21
92 : ldw RR'lt_ptr+ltoff+4(%r1),%r19 ; get new dlt value.
95 : be 0(%sr0,%r21) ; branch to target
96 : stw %rp,-24(%sp) ; save rp
98 Import stub to call shared library routine from shared library
99 (multiple sub-space support)
100 : addil LR'ltoff,%r19 ; get procedure entry point
101 : ldw RR'ltoff(%r1),%r21
102 : ldw RR'ltoff+4(%r1),%r19 ; get new dlt value.
105 : be 0(%sr0,%r21) ; branch to target
106 : stw %rp,-24(%sp) ; save rp
108 Export stub to return from shared lib routine (multiple sub-space support)
109 One of these is created for each exported procedure in a shared
110 library (and stored in the shared lib). Shared lib routines are
111 called via the first instruction in the export stub so that we can
112 do an inter-space return. Not required for single sub-space.
113 : bl,n X,%rp ; trap the return
115 : ldw -24(%sp),%rp ; restore the original rp
118 : be,n 0(%sr0,%rp) ; inter-space return. */
121 /* Variable names follow a coding style.
122 Please follow this (Apps Hungarian) style:
124 Structure/Variable Prefix
125 elf_link_hash_table "etab"
126 elf_link_hash_entry "eh"
128 elf32_hppa_link_hash_table "htab"
129 elf32_hppa_link_hash_entry "hh"
131 bfd_hash_table "btab"
134 bfd_hash_table containing stubs "bstab"
135 elf32_hppa_stub_hash_entry "hsh"
137 elf32_hppa_dyn_reloc_entry "hdh"
139 Always remember to use GNU Coding Style. */
141 #define PLT_ENTRY_SIZE 8
142 #define GOT_ENTRY_SIZE 4
143 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
145 static const bfd_byte plt_stub[] =
147 0x0e, 0x80, 0x10, 0x96, /* 1: ldw 0(%r20),%r22 */
148 0xea, 0xc0, 0xc0, 0x00, /* bv %r0(%r22) */
149 0x0e, 0x88, 0x10, 0x95, /* ldw 4(%r20),%r21 */
150 #define PLT_STUB_ENTRY (3*4)
151 0xea, 0x9f, 0x1f, 0xdd, /* b,l 1b,%r20 */
152 0xd6, 0x80, 0x1c, 0x1e, /* depi 0,31,2,%r20 */
153 0x00, 0xc0, 0xff, 0xee, /* 9: .word fixup_func */
154 0xde, 0xad, 0xbe, 0xef /* .word fixup_ltp */
157 /* Section name for stubs is the associated section name plus this
159 #define STUB_SUFFIX ".stub"
161 /* We don't need to copy certain PC- or GP-relative dynamic relocs
162 into a shared object's dynamic section. All the relocs of the
163 limited class we are interested in, are absolute. */
164 #ifndef RELATIVE_DYNRELOCS
165 #define RELATIVE_DYNRELOCS 0
166 #define IS_ABSOLUTE_RELOC(r_type) 1
169 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
170 copying dynamic variables from a shared lib into an app's dynbss
171 section, and instead use a dynamic relocation to point into the
173 #define ELIMINATE_COPY_RELOCS 1
175 enum elf32_hppa_stub_type
177 hppa_stub_long_branch,
178 hppa_stub_long_branch_shared,
180 hppa_stub_import_shared,
185 struct elf32_hppa_stub_hash_entry
187 /* Base hash table entry structure. */
188 struct bfd_hash_entry bh_root;
190 /* The stub section. */
193 /* Offset within stub_sec of the beginning of this stub. */
196 /* Given the symbol's value and its section we can determine its final
197 value when building the stubs (so the stub knows where to jump. */
198 bfd_vma target_value;
199 asection *target_section;
201 enum elf32_hppa_stub_type stub_type;
203 /* The symbol table entry, if any, that this was derived from. */
204 struct elf32_hppa_link_hash_entry *hh;
206 /* Where this stub is being called from, or, in the case of combined
207 stub sections, the first input section in the group. */
211 struct elf32_hppa_link_hash_entry
213 struct elf_link_hash_entry eh;
215 /* A pointer to the most recently used stub hash entry against this
217 struct elf32_hppa_stub_hash_entry *hsh_cache;
219 /* Used to count relocations for delayed sizing of relocation
221 struct elf32_hppa_dyn_reloc_entry
223 /* Next relocation in the chain. */
224 struct elf32_hppa_dyn_reloc_entry *hdh_next;
226 /* The input section of the reloc. */
229 /* Number of relocs copied in this section. */
232 #if RELATIVE_DYNRELOCS
233 /* Number of relative relocs copied for the input section. */
234 bfd_size_type relative_count;
240 GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_GD = 2, GOT_TLS_LDM = 4, GOT_TLS_IE = 8
243 /* Set if this symbol is used by a plabel reloc. */
244 unsigned int plabel:1;
247 struct elf32_hppa_link_hash_table
249 /* The main hash table. */
250 struct elf_link_hash_table etab;
252 /* The stub hash table. */
253 struct bfd_hash_table bstab;
255 /* Linker stub bfd. */
258 /* Linker call-backs. */
259 asection * (*add_stub_section) (const char *, asection *);
260 void (*layout_sections_again) (void);
262 /* Array to keep track of which stub sections have been created, and
263 information on stub grouping. */
266 /* This is the section to which stubs in the group will be
269 /* The stub section. */
273 /* Assorted information used by elf32_hppa_size_stubs. */
274 unsigned int bfd_count;
276 asection **input_list;
277 Elf_Internal_Sym **all_local_syms;
279 /* Short-cuts to get to dynamic linker sections. */
287 /* Used during a final link to store the base of the text and data
288 segments so that we can perform SEGREL relocations. */
289 bfd_vma text_segment_base;
290 bfd_vma data_segment_base;
292 /* Whether we support multiple sub-spaces for shared libs. */
293 unsigned int multi_subspace:1;
295 /* Flags set when various size branches are detected. Used to
296 select suitable defaults for the stub group size. */
297 unsigned int has_12bit_branch:1;
298 unsigned int has_17bit_branch:1;
299 unsigned int has_22bit_branch:1;
301 /* Set if we need a .plt stub to support lazy dynamic linking. */
302 unsigned int need_plt_stub:1;
304 /* Small local sym cache. */
305 struct sym_cache sym_cache;
307 /* Data for LDM relocations. */
310 bfd_signed_vma refcount;
315 /* Various hash macros and functions. */
316 #define hppa_link_hash_table(p) \
317 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
318 == HPPA32_ELF_DATA ? ((struct elf32_hppa_link_hash_table *) ((p)->hash)) : NULL)
320 #define hppa_elf_hash_entry(ent) \
321 ((struct elf32_hppa_link_hash_entry *)(ent))
323 #define hppa_stub_hash_entry(ent) \
324 ((struct elf32_hppa_stub_hash_entry *)(ent))
326 #define hppa_stub_hash_lookup(table, string, create, copy) \
327 ((struct elf32_hppa_stub_hash_entry *) \
328 bfd_hash_lookup ((table), (string), (create), (copy)))
330 #define hppa_elf_local_got_tls_type(abfd) \
331 ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info * 2)))
333 #define hh_name(hh) \
334 (hh ? hh->eh.root.root.string : "<undef>")
336 #define eh_name(eh) \
337 (eh ? eh->root.root.string : "<undef>")
339 /* Assorted hash table functions. */
341 /* Initialize an entry in the stub hash table. */
343 static struct bfd_hash_entry *
344 stub_hash_newfunc (struct bfd_hash_entry *entry,
345 struct bfd_hash_table *table,
348 /* Allocate the structure if it has not already been allocated by a
352 entry = bfd_hash_allocate (table,
353 sizeof (struct elf32_hppa_stub_hash_entry));
358 /* Call the allocation method of the superclass. */
359 entry = bfd_hash_newfunc (entry, table, string);
362 struct elf32_hppa_stub_hash_entry *hsh;
364 /* Initialize the local fields. */
365 hsh = hppa_stub_hash_entry (entry);
366 hsh->stub_sec = NULL;
367 hsh->stub_offset = 0;
368 hsh->target_value = 0;
369 hsh->target_section = NULL;
370 hsh->stub_type = hppa_stub_long_branch;
378 /* Initialize an entry in the link hash table. */
380 static struct bfd_hash_entry *
381 hppa_link_hash_newfunc (struct bfd_hash_entry *entry,
382 struct bfd_hash_table *table,
385 /* Allocate the structure if it has not already been allocated by a
389 entry = bfd_hash_allocate (table,
390 sizeof (struct elf32_hppa_link_hash_entry));
395 /* Call the allocation method of the superclass. */
396 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
399 struct elf32_hppa_link_hash_entry *hh;
401 /* Initialize the local fields. */
402 hh = hppa_elf_hash_entry (entry);
403 hh->hsh_cache = NULL;
404 hh->dyn_relocs = NULL;
406 hh->tls_type = GOT_UNKNOWN;
412 /* Create the derived linker hash table. The PA ELF port uses the derived
413 hash table to keep information specific to the PA ELF linker (without
414 using static variables). */
416 static struct bfd_link_hash_table *
417 elf32_hppa_link_hash_table_create (bfd *abfd)
419 struct elf32_hppa_link_hash_table *htab;
420 bfd_size_type amt = sizeof (*htab);
422 htab = bfd_zmalloc (amt);
426 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, hppa_link_hash_newfunc,
427 sizeof (struct elf32_hppa_link_hash_entry),
434 /* Init the stub hash table too. */
435 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
436 sizeof (struct elf32_hppa_stub_hash_entry)))
439 htab->text_segment_base = (bfd_vma) -1;
440 htab->data_segment_base = (bfd_vma) -1;
441 return &htab->etab.root;
444 /* Free the derived linker hash table. */
447 elf32_hppa_link_hash_table_free (struct bfd_link_hash_table *btab)
449 struct elf32_hppa_link_hash_table *htab
450 = (struct elf32_hppa_link_hash_table *) btab;
452 bfd_hash_table_free (&htab->bstab);
453 _bfd_elf_link_hash_table_free (btab);
456 /* Build a name for an entry in the stub hash table. */
459 hppa_stub_name (const asection *input_section,
460 const asection *sym_sec,
461 const struct elf32_hppa_link_hash_entry *hh,
462 const Elf_Internal_Rela *rela)
469 len = 8 + 1 + strlen (hh_name (hh)) + 1 + 8 + 1;
470 stub_name = bfd_malloc (len);
471 if (stub_name != NULL)
472 sprintf (stub_name, "%08x_%s+%x",
473 input_section->id & 0xffffffff,
475 (int) rela->r_addend & 0xffffffff);
479 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
480 stub_name = bfd_malloc (len);
481 if (stub_name != NULL)
482 sprintf (stub_name, "%08x_%x:%x+%x",
483 input_section->id & 0xffffffff,
484 sym_sec->id & 0xffffffff,
485 (int) ELF32_R_SYM (rela->r_info) & 0xffffffff,
486 (int) rela->r_addend & 0xffffffff);
491 /* Look up an entry in the stub hash. Stub entries are cached because
492 creating the stub name takes a bit of time. */
494 static struct elf32_hppa_stub_hash_entry *
495 hppa_get_stub_entry (const asection *input_section,
496 const asection *sym_sec,
497 struct elf32_hppa_link_hash_entry *hh,
498 const Elf_Internal_Rela *rela,
499 struct elf32_hppa_link_hash_table *htab)
501 struct elf32_hppa_stub_hash_entry *hsh_entry;
502 const asection *id_sec;
504 /* If this input section is part of a group of sections sharing one
505 stub section, then use the id of the first section in the group.
506 Stub names need to include a section id, as there may well be
507 more than one stub used to reach say, printf, and we need to
508 distinguish between them. */
509 id_sec = htab->stub_group[input_section->id].link_sec;
511 if (hh != NULL && hh->hsh_cache != NULL
512 && hh->hsh_cache->hh == hh
513 && hh->hsh_cache->id_sec == id_sec)
515 hsh_entry = hh->hsh_cache;
521 stub_name = hppa_stub_name (id_sec, sym_sec, hh, rela);
522 if (stub_name == NULL)
525 hsh_entry = hppa_stub_hash_lookup (&htab->bstab,
526 stub_name, FALSE, FALSE);
528 hh->hsh_cache = hsh_entry;
536 /* Add a new stub entry to the stub hash. Not all fields of the new
537 stub entry are initialised. */
539 static struct elf32_hppa_stub_hash_entry *
540 hppa_add_stub (const char *stub_name,
542 struct elf32_hppa_link_hash_table *htab)
546 struct elf32_hppa_stub_hash_entry *hsh;
548 link_sec = htab->stub_group[section->id].link_sec;
549 stub_sec = htab->stub_group[section->id].stub_sec;
550 if (stub_sec == NULL)
552 stub_sec = htab->stub_group[link_sec->id].stub_sec;
553 if (stub_sec == NULL)
559 namelen = strlen (link_sec->name);
560 len = namelen + sizeof (STUB_SUFFIX);
561 s_name = bfd_alloc (htab->stub_bfd, len);
565 memcpy (s_name, link_sec->name, namelen);
566 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
567 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
568 if (stub_sec == NULL)
570 htab->stub_group[link_sec->id].stub_sec = stub_sec;
572 htab->stub_group[section->id].stub_sec = stub_sec;
575 /* Enter this entry into the linker stub hash table. */
576 hsh = hppa_stub_hash_lookup (&htab->bstab, stub_name,
580 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
586 hsh->stub_sec = stub_sec;
587 hsh->stub_offset = 0;
588 hsh->id_sec = link_sec;
592 /* Determine the type of stub needed, if any, for a call. */
594 static enum elf32_hppa_stub_type
595 hppa_type_of_stub (asection *input_sec,
596 const Elf_Internal_Rela *rela,
597 struct elf32_hppa_link_hash_entry *hh,
599 struct bfd_link_info *info)
602 bfd_vma branch_offset;
603 bfd_vma max_branch_offset;
607 && hh->eh.plt.offset != (bfd_vma) -1
608 && hh->eh.dynindx != -1
611 || !hh->eh.def_regular
612 || hh->eh.root.type == bfd_link_hash_defweak))
614 /* We need an import stub. Decide between hppa_stub_import
615 and hppa_stub_import_shared later. */
616 return hppa_stub_import;
619 /* Determine where the call point is. */
620 location = (input_sec->output_offset
621 + input_sec->output_section->vma
624 branch_offset = destination - location - 8;
625 r_type = ELF32_R_TYPE (rela->r_info);
627 /* Determine if a long branch stub is needed. parisc branch offsets
628 are relative to the second instruction past the branch, ie. +8
629 bytes on from the branch instruction location. The offset is
630 signed and counts in units of 4 bytes. */
631 if (r_type == (unsigned int) R_PARISC_PCREL17F)
632 max_branch_offset = (1 << (17 - 1)) << 2;
634 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
635 max_branch_offset = (1 << (12 - 1)) << 2;
637 else /* R_PARISC_PCREL22F. */
638 max_branch_offset = (1 << (22 - 1)) << 2;
640 if (branch_offset + max_branch_offset >= 2*max_branch_offset)
641 return hppa_stub_long_branch;
643 return hppa_stub_none;
646 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
647 IN_ARG contains the link info pointer. */
649 #define LDIL_R1 0x20200000 /* ldil LR'XXX,%r1 */
650 #define BE_SR4_R1 0xe0202002 /* be,n RR'XXX(%sr4,%r1) */
652 #define BL_R1 0xe8200000 /* b,l .+8,%r1 */
653 #define ADDIL_R1 0x28200000 /* addil LR'XXX,%r1,%r1 */
654 #define DEPI_R1 0xd4201c1e /* depi 0,31,2,%r1 */
656 #define ADDIL_DP 0x2b600000 /* addil LR'XXX,%dp,%r1 */
657 #define LDW_R1_R21 0x48350000 /* ldw RR'XXX(%sr0,%r1),%r21 */
658 #define BV_R0_R21 0xeaa0c000 /* bv %r0(%r21) */
659 #define LDW_R1_R19 0x48330000 /* ldw RR'XXX(%sr0,%r1),%r19 */
661 #define ADDIL_R19 0x2a600000 /* addil LR'XXX,%r19,%r1 */
662 #define LDW_R1_DP 0x483b0000 /* ldw RR'XXX(%sr0,%r1),%dp */
664 #define LDSID_R21_R1 0x02a010a1 /* ldsid (%sr0,%r21),%r1 */
665 #define MTSP_R1 0x00011820 /* mtsp %r1,%sr0 */
666 #define BE_SR0_R21 0xe2a00000 /* be 0(%sr0,%r21) */
667 #define STW_RP 0x6bc23fd1 /* stw %rp,-24(%sr0,%sp) */
669 #define BL22_RP 0xe800a002 /* b,l,n XXX,%rp */
670 #define BL_RP 0xe8400002 /* b,l,n XXX,%rp */
671 #define NOP 0x08000240 /* nop */
672 #define LDW_RP 0x4bc23fd1 /* ldw -24(%sr0,%sp),%rp */
673 #define LDSID_RP_R1 0x004010a1 /* ldsid (%sr0,%rp),%r1 */
674 #define BE_SR0_RP 0xe0400002 /* be,n 0(%sr0,%rp) */
681 #define LDW_R1_DLT LDW_R1_R19
683 #define LDW_R1_DLT LDW_R1_DP
687 hppa_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
689 struct elf32_hppa_stub_hash_entry *hsh;
690 struct bfd_link_info *info;
691 struct elf32_hppa_link_hash_table *htab;
701 /* Massage our args to the form they really have. */
702 hsh = hppa_stub_hash_entry (bh);
703 info = (struct bfd_link_info *)in_arg;
705 htab = hppa_link_hash_table (info);
709 stub_sec = hsh->stub_sec;
711 /* Make a note of the offset within the stubs for this entry. */
712 hsh->stub_offset = stub_sec->size;
713 loc = stub_sec->contents + hsh->stub_offset;
715 stub_bfd = stub_sec->owner;
717 switch (hsh->stub_type)
719 case hppa_stub_long_branch:
720 /* Create the long branch. A long branch is formed with "ldil"
721 loading the upper bits of the target address into a register,
722 then branching with "be" which adds in the lower bits.
723 The "be" has its delay slot nullified. */
724 sym_value = (hsh->target_value
725 + hsh->target_section->output_offset
726 + hsh->target_section->output_section->vma);
728 val = hppa_field_adjust (sym_value, 0, e_lrsel);
729 insn = hppa_rebuild_insn ((int) LDIL_R1, val, 21);
730 bfd_put_32 (stub_bfd, insn, loc);
732 val = hppa_field_adjust (sym_value, 0, e_rrsel) >> 2;
733 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
734 bfd_put_32 (stub_bfd, insn, loc + 4);
739 case hppa_stub_long_branch_shared:
740 /* Branches are relative. This is where we are going to. */
741 sym_value = (hsh->target_value
742 + hsh->target_section->output_offset
743 + hsh->target_section->output_section->vma);
745 /* And this is where we are coming from, more or less. */
746 sym_value -= (hsh->stub_offset
747 + stub_sec->output_offset
748 + stub_sec->output_section->vma);
750 bfd_put_32 (stub_bfd, (bfd_vma) BL_R1, loc);
751 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_lrsel);
752 insn = hppa_rebuild_insn ((int) ADDIL_R1, val, 21);
753 bfd_put_32 (stub_bfd, insn, loc + 4);
755 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_rrsel) >> 2;
756 insn = hppa_rebuild_insn ((int) BE_SR4_R1, val, 17);
757 bfd_put_32 (stub_bfd, insn, loc + 8);
761 case hppa_stub_import:
762 case hppa_stub_import_shared:
763 off = hsh->hh->eh.plt.offset;
764 if (off >= (bfd_vma) -2)
767 off &= ~ (bfd_vma) 1;
769 + htab->splt->output_offset
770 + htab->splt->output_section->vma
771 - elf_gp (htab->splt->output_section->owner));
775 if (hsh->stub_type == hppa_stub_import_shared)
778 val = hppa_field_adjust (sym_value, 0, e_lrsel),
779 insn = hppa_rebuild_insn ((int) insn, val, 21);
780 bfd_put_32 (stub_bfd, insn, loc);
782 /* It is critical to use lrsel/rrsel here because we are using
783 two different offsets (+0 and +4) from sym_value. If we use
784 lsel/rsel then with unfortunate sym_values we will round
785 sym_value+4 up to the next 2k block leading to a mis-match
786 between the lsel and rsel value. */
787 val = hppa_field_adjust (sym_value, 0, e_rrsel);
788 insn = hppa_rebuild_insn ((int) LDW_R1_R21, val, 14);
789 bfd_put_32 (stub_bfd, insn, loc + 4);
791 if (htab->multi_subspace)
793 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
794 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
795 bfd_put_32 (stub_bfd, insn, loc + 8);
797 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_R21_R1, loc + 12);
798 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
799 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_R21, loc + 20);
800 bfd_put_32 (stub_bfd, (bfd_vma) STW_RP, loc + 24);
806 bfd_put_32 (stub_bfd, (bfd_vma) BV_R0_R21, loc + 8);
807 val = hppa_field_adjust (sym_value, (bfd_signed_vma) 4, e_rrsel);
808 insn = hppa_rebuild_insn ((int) LDW_R1_DLT, val, 14);
809 bfd_put_32 (stub_bfd, insn, loc + 12);
816 case hppa_stub_export:
817 /* Branches are relative. This is where we are going to. */
818 sym_value = (hsh->target_value
819 + hsh->target_section->output_offset
820 + hsh->target_section->output_section->vma);
822 /* And this is where we are coming from. */
823 sym_value -= (hsh->stub_offset
824 + stub_sec->output_offset
825 + stub_sec->output_section->vma);
827 if (sym_value - 8 + (1 << (17 + 1)) >= (1 << (17 + 2))
828 && (!htab->has_22bit_branch
829 || sym_value - 8 + (1 << (22 + 1)) >= (1 << (22 + 2))))
831 (*_bfd_error_handler)
832 (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
833 hsh->target_section->owner,
835 (long) hsh->stub_offset,
836 hsh->bh_root.string);
837 bfd_set_error (bfd_error_bad_value);
841 val = hppa_field_adjust (sym_value, (bfd_signed_vma) -8, e_fsel) >> 2;
842 if (!htab->has_22bit_branch)
843 insn = hppa_rebuild_insn ((int) BL_RP, val, 17);
845 insn = hppa_rebuild_insn ((int) BL22_RP, val, 22);
846 bfd_put_32 (stub_bfd, insn, loc);
848 bfd_put_32 (stub_bfd, (bfd_vma) NOP, loc + 4);
849 bfd_put_32 (stub_bfd, (bfd_vma) LDW_RP, loc + 8);
850 bfd_put_32 (stub_bfd, (bfd_vma) LDSID_RP_R1, loc + 12);
851 bfd_put_32 (stub_bfd, (bfd_vma) MTSP_R1, loc + 16);
852 bfd_put_32 (stub_bfd, (bfd_vma) BE_SR0_RP, loc + 20);
854 /* Point the function symbol at the stub. */
855 hsh->hh->eh.root.u.def.section = stub_sec;
856 hsh->hh->eh.root.u.def.value = stub_sec->size;
866 stub_sec->size += size;
891 /* As above, but don't actually build the stub. Just bump offset so
892 we know stub section sizes. */
895 hppa_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
897 struct elf32_hppa_stub_hash_entry *hsh;
898 struct elf32_hppa_link_hash_table *htab;
901 /* Massage our args to the form they really have. */
902 hsh = hppa_stub_hash_entry (bh);
905 if (hsh->stub_type == hppa_stub_long_branch)
907 else if (hsh->stub_type == hppa_stub_long_branch_shared)
909 else if (hsh->stub_type == hppa_stub_export)
911 else /* hppa_stub_import or hppa_stub_import_shared. */
913 if (htab->multi_subspace)
919 hsh->stub_sec->size += size;
923 /* Return nonzero if ABFD represents an HPPA ELF32 file.
924 Additionally we set the default architecture and machine. */
927 elf32_hppa_object_p (bfd *abfd)
929 Elf_Internal_Ehdr * i_ehdrp;
932 i_ehdrp = elf_elfheader (abfd);
933 if (strcmp (bfd_get_target (abfd), "elf32-hppa-linux") == 0)
935 /* GCC on hppa-linux produces binaries with OSABI=GNU,
936 but the kernel produces corefiles with OSABI=SysV. */
937 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU &&
938 i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
941 else if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0)
943 /* GCC on hppa-netbsd produces binaries with OSABI=NetBSD,
944 but the kernel produces corefiles with OSABI=SysV. */
945 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NETBSD &&
946 i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_NONE) /* aka SYSV */
951 if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_HPUX)
955 flags = i_ehdrp->e_flags;
956 switch (flags & (EF_PARISC_ARCH | EF_PARISC_WIDE))
959 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 10);
961 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 11);
963 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 20);
964 case EFA_PARISC_2_0 | EF_PARISC_WIDE:
965 return bfd_default_set_arch_mach (abfd, bfd_arch_hppa, 25);
970 /* Create the .plt and .got sections, and set up our hash table
971 short-cuts to various dynamic sections. */
974 elf32_hppa_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
976 struct elf32_hppa_link_hash_table *htab;
977 struct elf_link_hash_entry *eh;
979 /* Don't try to create the .plt and .got twice. */
980 htab = hppa_link_hash_table (info);
983 if (htab->splt != NULL)
986 /* Call the generic code to do most of the work. */
987 if (! _bfd_elf_create_dynamic_sections (abfd, info))
990 htab->splt = bfd_get_linker_section (abfd, ".plt");
991 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt");
993 htab->sgot = bfd_get_linker_section (abfd, ".got");
994 htab->srelgot = bfd_get_linker_section (abfd, ".rela.got");
996 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss");
997 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss");
999 /* hppa-linux needs _GLOBAL_OFFSET_TABLE_ to be visible from the main
1000 application, because __canonicalize_funcptr_for_compare needs it. */
1001 eh = elf_hash_table (info)->hgot;
1002 eh->forced_local = 0;
1003 eh->other = STV_DEFAULT;
1004 return bfd_elf_link_record_dynamic_symbol (info, eh);
1007 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1010 elf32_hppa_copy_indirect_symbol (struct bfd_link_info *info,
1011 struct elf_link_hash_entry *eh_dir,
1012 struct elf_link_hash_entry *eh_ind)
1014 struct elf32_hppa_link_hash_entry *hh_dir, *hh_ind;
1016 hh_dir = hppa_elf_hash_entry (eh_dir);
1017 hh_ind = hppa_elf_hash_entry (eh_ind);
1019 if (hh_ind->dyn_relocs != NULL)
1021 if (hh_dir->dyn_relocs != NULL)
1023 struct elf32_hppa_dyn_reloc_entry **hdh_pp;
1024 struct elf32_hppa_dyn_reloc_entry *hdh_p;
1026 /* Add reloc counts against the indirect sym to the direct sym
1027 list. Merge any entries against the same section. */
1028 for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
1030 struct elf32_hppa_dyn_reloc_entry *hdh_q;
1032 for (hdh_q = hh_dir->dyn_relocs;
1034 hdh_q = hdh_q->hdh_next)
1035 if (hdh_q->sec == hdh_p->sec)
1037 #if RELATIVE_DYNRELOCS
1038 hdh_q->relative_count += hdh_p->relative_count;
1040 hdh_q->count += hdh_p->count;
1041 *hdh_pp = hdh_p->hdh_next;
1045 hdh_pp = &hdh_p->hdh_next;
1047 *hdh_pp = hh_dir->dyn_relocs;
1050 hh_dir->dyn_relocs = hh_ind->dyn_relocs;
1051 hh_ind->dyn_relocs = NULL;
1054 if (ELIMINATE_COPY_RELOCS
1055 && eh_ind->root.type != bfd_link_hash_indirect
1056 && eh_dir->dynamic_adjusted)
1058 /* If called to transfer flags for a weakdef during processing
1059 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1060 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1061 eh_dir->ref_dynamic |= eh_ind->ref_dynamic;
1062 eh_dir->ref_regular |= eh_ind->ref_regular;
1063 eh_dir->ref_regular_nonweak |= eh_ind->ref_regular_nonweak;
1064 eh_dir->needs_plt |= eh_ind->needs_plt;
1068 if (eh_ind->root.type == bfd_link_hash_indirect
1069 && eh_dir->got.refcount <= 0)
1071 hh_dir->tls_type = hh_ind->tls_type;
1072 hh_ind->tls_type = GOT_UNKNOWN;
1075 _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind);
1080 elf32_hppa_optimized_tls_reloc (struct bfd_link_info *info ATTRIBUTE_UNUSED,
1081 int r_type, int is_local ATTRIBUTE_UNUSED)
1083 /* For now we don't support linker optimizations. */
1087 /* Return a pointer to the local GOT, PLT and TLS reference counts
1088 for ABFD. Returns NULL if the storage allocation fails. */
1090 static bfd_signed_vma *
1091 hppa32_elf_local_refcounts (bfd *abfd)
1093 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1094 bfd_signed_vma *local_refcounts;
1096 local_refcounts = elf_local_got_refcounts (abfd);
1097 if (local_refcounts == NULL)
1101 /* Allocate space for local GOT and PLT reference
1102 counts. Done this way to save polluting elf_obj_tdata
1103 with another target specific pointer. */
1104 size = symtab_hdr->sh_info;
1105 size *= 2 * sizeof (bfd_signed_vma);
1106 /* Add in space to store the local GOT TLS types. */
1107 size += symtab_hdr->sh_info;
1108 local_refcounts = bfd_zalloc (abfd, size);
1109 if (local_refcounts == NULL)
1111 elf_local_got_refcounts (abfd) = local_refcounts;
1112 memset (hppa_elf_local_got_tls_type (abfd), GOT_UNKNOWN,
1113 symtab_hdr->sh_info);
1115 return local_refcounts;
1119 /* Look through the relocs for a section during the first phase, and
1120 calculate needed space in the global offset table, procedure linkage
1121 table, and dynamic reloc sections. At this point we haven't
1122 necessarily read all the input files. */
1125 elf32_hppa_check_relocs (bfd *abfd,
1126 struct bfd_link_info *info,
1128 const Elf_Internal_Rela *relocs)
1130 Elf_Internal_Shdr *symtab_hdr;
1131 struct elf_link_hash_entry **eh_syms;
1132 const Elf_Internal_Rela *rela;
1133 const Elf_Internal_Rela *rela_end;
1134 struct elf32_hppa_link_hash_table *htab;
1136 int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN;
1138 if (info->relocatable)
1141 htab = hppa_link_hash_table (info);
1144 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1145 eh_syms = elf_sym_hashes (abfd);
1148 rela_end = relocs + sec->reloc_count;
1149 for (rela = relocs; rela < rela_end; rela++)
1158 unsigned int r_symndx, r_type;
1159 struct elf32_hppa_link_hash_entry *hh;
1162 r_symndx = ELF32_R_SYM (rela->r_info);
1164 if (r_symndx < symtab_hdr->sh_info)
1168 hh = hppa_elf_hash_entry (eh_syms[r_symndx - symtab_hdr->sh_info]);
1169 while (hh->eh.root.type == bfd_link_hash_indirect
1170 || hh->eh.root.type == bfd_link_hash_warning)
1171 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
1174 r_type = ELF32_R_TYPE (rela->r_info);
1175 r_type = elf32_hppa_optimized_tls_reloc (info, r_type, hh == NULL);
1179 case R_PARISC_DLTIND14F:
1180 case R_PARISC_DLTIND14R:
1181 case R_PARISC_DLTIND21L:
1182 /* This symbol requires a global offset table entry. */
1183 need_entry = NEED_GOT;
1186 case R_PARISC_PLABEL14R: /* "Official" procedure labels. */
1187 case R_PARISC_PLABEL21L:
1188 case R_PARISC_PLABEL32:
1189 /* If the addend is non-zero, we break badly. */
1190 if (rela->r_addend != 0)
1193 /* If we are creating a shared library, then we need to
1194 create a PLT entry for all PLABELs, because PLABELs with
1195 local symbols may be passed via a pointer to another
1196 object. Additionally, output a dynamic relocation
1197 pointing to the PLT entry.
1199 For executables, the original 32-bit ABI allowed two
1200 different styles of PLABELs (function pointers): For
1201 global functions, the PLABEL word points into the .plt
1202 two bytes past a (function address, gp) pair, and for
1203 local functions the PLABEL points directly at the
1204 function. The magic +2 for the first type allows us to
1205 differentiate between the two. As you can imagine, this
1206 is a real pain when it comes to generating code to call
1207 functions indirectly or to compare function pointers.
1208 We avoid the mess by always pointing a PLABEL into the
1209 .plt, even for local functions. */
1210 need_entry = PLT_PLABEL | NEED_PLT | NEED_DYNREL;
1213 case R_PARISC_PCREL12F:
1214 htab->has_12bit_branch = 1;
1217 case R_PARISC_PCREL17C:
1218 case R_PARISC_PCREL17F:
1219 htab->has_17bit_branch = 1;
1222 case R_PARISC_PCREL22F:
1223 htab->has_22bit_branch = 1;
1225 /* Function calls might need to go through the .plt, and
1226 might require long branch stubs. */
1229 /* We know local syms won't need a .plt entry, and if
1230 they need a long branch stub we can't guarantee that
1231 we can reach the stub. So just flag an error later
1232 if we're doing a shared link and find we need a long
1238 /* Global symbols will need a .plt entry if they remain
1239 global, and in most cases won't need a long branch
1240 stub. Unfortunately, we have to cater for the case
1241 where a symbol is forced local by versioning, or due
1242 to symbolic linking, and we lose the .plt entry. */
1243 need_entry = NEED_PLT;
1244 if (hh->eh.type == STT_PARISC_MILLI)
1249 case R_PARISC_SEGBASE: /* Used to set segment base. */
1250 case R_PARISC_SEGREL32: /* Relative reloc, used for unwind. */
1251 case R_PARISC_PCREL14F: /* PC relative load/store. */
1252 case R_PARISC_PCREL14R:
1253 case R_PARISC_PCREL17R: /* External branches. */
1254 case R_PARISC_PCREL21L: /* As above, and for load/store too. */
1255 case R_PARISC_PCREL32:
1256 /* We don't need to propagate the relocation if linking a
1257 shared object since these are section relative. */
1260 case R_PARISC_DPREL14F: /* Used for gp rel data load/store. */
1261 case R_PARISC_DPREL14R:
1262 case R_PARISC_DPREL21L:
1265 (*_bfd_error_handler)
1266 (_("%B: relocation %s can not be used when making a shared object; recompile with -fPIC"),
1268 elf_hppa_howto_table[r_type].name);
1269 bfd_set_error (bfd_error_bad_value);
1274 case R_PARISC_DIR17F: /* Used for external branches. */
1275 case R_PARISC_DIR17R:
1276 case R_PARISC_DIR14F: /* Used for load/store from absolute locn. */
1277 case R_PARISC_DIR14R:
1278 case R_PARISC_DIR21L: /* As above, and for ext branches too. */
1279 case R_PARISC_DIR32: /* .word relocs. */
1280 /* We may want to output a dynamic relocation later. */
1281 need_entry = NEED_DYNREL;
1284 /* This relocation describes the C++ object vtable hierarchy.
1285 Reconstruct it for later use during GC. */
1286 case R_PARISC_GNU_VTINHERIT:
1287 if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh, rela->r_offset))
1291 /* This relocation describes which C++ vtable entries are actually
1292 used. Record for later use during GC. */
1293 case R_PARISC_GNU_VTENTRY:
1294 BFD_ASSERT (hh != NULL);
1296 && !bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rela->r_addend))
1300 case R_PARISC_TLS_GD21L:
1301 case R_PARISC_TLS_GD14R:
1302 case R_PARISC_TLS_LDM21L:
1303 case R_PARISC_TLS_LDM14R:
1304 need_entry = NEED_GOT;
1307 case R_PARISC_TLS_IE21L:
1308 case R_PARISC_TLS_IE14R:
1310 info->flags |= DF_STATIC_TLS;
1311 need_entry = NEED_GOT;
1318 /* Now carry out our orders. */
1319 if (need_entry & NEED_GOT)
1324 tls_type = GOT_NORMAL;
1326 case R_PARISC_TLS_GD21L:
1327 case R_PARISC_TLS_GD14R:
1328 tls_type |= GOT_TLS_GD;
1330 case R_PARISC_TLS_LDM21L:
1331 case R_PARISC_TLS_LDM14R:
1332 tls_type |= GOT_TLS_LDM;
1334 case R_PARISC_TLS_IE21L:
1335 case R_PARISC_TLS_IE14R:
1336 tls_type |= GOT_TLS_IE;
1340 /* Allocate space for a GOT entry, as well as a dynamic
1341 relocation for this entry. */
1342 if (htab->sgot == NULL)
1344 if (htab->etab.dynobj == NULL)
1345 htab->etab.dynobj = abfd;
1346 if (!elf32_hppa_create_dynamic_sections (htab->etab.dynobj, info))
1350 if (r_type == R_PARISC_TLS_LDM21L
1351 || r_type == R_PARISC_TLS_LDM14R)
1352 htab->tls_ldm_got.refcount += 1;
1357 hh->eh.got.refcount += 1;
1358 old_tls_type = hh->tls_type;
1362 bfd_signed_vma *local_got_refcounts;
1364 /* This is a global offset table entry for a local symbol. */
1365 local_got_refcounts = hppa32_elf_local_refcounts (abfd);
1366 if (local_got_refcounts == NULL)
1368 local_got_refcounts[r_symndx] += 1;
1370 old_tls_type = hppa_elf_local_got_tls_type (abfd) [r_symndx];
1373 tls_type |= old_tls_type;
1375 if (old_tls_type != tls_type)
1378 hh->tls_type = tls_type;
1380 hppa_elf_local_got_tls_type (abfd) [r_symndx] = tls_type;
1386 if (need_entry & NEED_PLT)
1388 /* If we are creating a shared library, and this is a reloc
1389 against a weak symbol or a global symbol in a dynamic
1390 object, then we will be creating an import stub and a
1391 .plt entry for the symbol. Similarly, on a normal link
1392 to symbols defined in a dynamic object we'll need the
1393 import stub and a .plt entry. We don't know yet whether
1394 the symbol is defined or not, so make an entry anyway and
1395 clean up later in adjust_dynamic_symbol. */
1396 if ((sec->flags & SEC_ALLOC) != 0)
1400 hh->eh.needs_plt = 1;
1401 hh->eh.plt.refcount += 1;
1403 /* If this .plt entry is for a plabel, mark it so
1404 that adjust_dynamic_symbol will keep the entry
1405 even if it appears to be local. */
1406 if (need_entry & PLT_PLABEL)
1409 else if (need_entry & PLT_PLABEL)
1411 bfd_signed_vma *local_got_refcounts;
1412 bfd_signed_vma *local_plt_refcounts;
1414 local_got_refcounts = hppa32_elf_local_refcounts (abfd);
1415 if (local_got_refcounts == NULL)
1417 local_plt_refcounts = (local_got_refcounts
1418 + symtab_hdr->sh_info);
1419 local_plt_refcounts[r_symndx] += 1;
1424 if (need_entry & NEED_DYNREL)
1426 /* Flag this symbol as having a non-got, non-plt reference
1427 so that we generate copy relocs if it turns out to be
1429 if (hh != NULL && !info->shared)
1430 hh->eh.non_got_ref = 1;
1432 /* If we are creating a shared library then we need to copy
1433 the reloc into the shared library. However, if we are
1434 linking with -Bsymbolic, we need only copy absolute
1435 relocs or relocs against symbols that are not defined in
1436 an object we are including in the link. PC- or DP- or
1437 DLT-relative relocs against any local sym or global sym
1438 with DEF_REGULAR set, can be discarded. At this point we
1439 have not seen all the input files, so it is possible that
1440 DEF_REGULAR is not set now but will be set later (it is
1441 never cleared). We account for that possibility below by
1442 storing information in the dyn_relocs field of the
1445 A similar situation to the -Bsymbolic case occurs when
1446 creating shared libraries and symbol visibility changes
1447 render the symbol local.
1449 As it turns out, all the relocs we will be creating here
1450 are absolute, so we cannot remove them on -Bsymbolic
1451 links or visibility changes anyway. A STUB_REL reloc
1452 is absolute too, as in that case it is the reloc in the
1453 stub we will be creating, rather than copying the PCREL
1454 reloc in the branch.
1456 If on the other hand, we are creating an executable, we
1457 may need to keep relocations for symbols satisfied by a
1458 dynamic library if we manage to avoid copy relocs for the
1461 && (sec->flags & SEC_ALLOC) != 0
1462 && (IS_ABSOLUTE_RELOC (r_type)
1465 || hh->eh.root.type == bfd_link_hash_defweak
1466 || !hh->eh.def_regular))))
1467 || (ELIMINATE_COPY_RELOCS
1469 && (sec->flags & SEC_ALLOC) != 0
1471 && (hh->eh.root.type == bfd_link_hash_defweak
1472 || !hh->eh.def_regular)))
1474 struct elf32_hppa_dyn_reloc_entry *hdh_p;
1475 struct elf32_hppa_dyn_reloc_entry **hdh_head;
1477 /* Create a reloc section in dynobj and make room for
1481 if (htab->etab.dynobj == NULL)
1482 htab->etab.dynobj = abfd;
1484 sreloc = _bfd_elf_make_dynamic_reloc_section
1485 (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ TRUE);
1489 bfd_set_error (bfd_error_bad_value);
1494 /* If this is a global symbol, we count the number of
1495 relocations we need for this symbol. */
1498 hdh_head = &hh->dyn_relocs;
1502 /* Track dynamic relocs needed for local syms too.
1503 We really need local syms available to do this
1507 Elf_Internal_Sym *isym;
1509 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1514 sr = bfd_section_from_elf_index (abfd, isym->st_shndx);
1518 vpp = &elf_section_data (sr)->local_dynrel;
1519 hdh_head = (struct elf32_hppa_dyn_reloc_entry **) vpp;
1523 if (hdh_p == NULL || hdh_p->sec != sec)
1525 hdh_p = bfd_alloc (htab->etab.dynobj, sizeof *hdh_p);
1528 hdh_p->hdh_next = *hdh_head;
1532 #if RELATIVE_DYNRELOCS
1533 hdh_p->relative_count = 0;
1538 #if RELATIVE_DYNRELOCS
1539 if (!IS_ABSOLUTE_RELOC (rtype))
1540 hdh_p->relative_count += 1;
1549 /* Return the section that should be marked against garbage collection
1550 for a given relocation. */
1553 elf32_hppa_gc_mark_hook (asection *sec,
1554 struct bfd_link_info *info,
1555 Elf_Internal_Rela *rela,
1556 struct elf_link_hash_entry *hh,
1557 Elf_Internal_Sym *sym)
1560 switch ((unsigned int) ELF32_R_TYPE (rela->r_info))
1562 case R_PARISC_GNU_VTINHERIT:
1563 case R_PARISC_GNU_VTENTRY:
1567 return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym);
1570 /* Update the got and plt entry reference counts for the section being
1574 elf32_hppa_gc_sweep_hook (bfd *abfd,
1575 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1577 const Elf_Internal_Rela *relocs)
1579 Elf_Internal_Shdr *symtab_hdr;
1580 struct elf_link_hash_entry **eh_syms;
1581 bfd_signed_vma *local_got_refcounts;
1582 bfd_signed_vma *local_plt_refcounts;
1583 const Elf_Internal_Rela *rela, *relend;
1584 struct elf32_hppa_link_hash_table *htab;
1586 if (info->relocatable)
1589 htab = hppa_link_hash_table (info);
1593 elf_section_data (sec)->local_dynrel = NULL;
1595 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1596 eh_syms = elf_sym_hashes (abfd);
1597 local_got_refcounts = elf_local_got_refcounts (abfd);
1598 local_plt_refcounts = local_got_refcounts;
1599 if (local_plt_refcounts != NULL)
1600 local_plt_refcounts += symtab_hdr->sh_info;
1602 relend = relocs + sec->reloc_count;
1603 for (rela = relocs; rela < relend; rela++)
1605 unsigned long r_symndx;
1606 unsigned int r_type;
1607 struct elf_link_hash_entry *eh = NULL;
1609 r_symndx = ELF32_R_SYM (rela->r_info);
1610 if (r_symndx >= symtab_hdr->sh_info)
1612 struct elf32_hppa_link_hash_entry *hh;
1613 struct elf32_hppa_dyn_reloc_entry **hdh_pp;
1614 struct elf32_hppa_dyn_reloc_entry *hdh_p;
1616 eh = eh_syms[r_symndx - symtab_hdr->sh_info];
1617 while (eh->root.type == bfd_link_hash_indirect
1618 || eh->root.type == bfd_link_hash_warning)
1619 eh = (struct elf_link_hash_entry *) eh->root.u.i.link;
1620 hh = hppa_elf_hash_entry (eh);
1622 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; hdh_pp = &hdh_p->hdh_next)
1623 if (hdh_p->sec == sec)
1625 /* Everything must go for SEC. */
1626 *hdh_pp = hdh_p->hdh_next;
1631 r_type = ELF32_R_TYPE (rela->r_info);
1632 r_type = elf32_hppa_optimized_tls_reloc (info, r_type, eh != NULL);
1636 case R_PARISC_DLTIND14F:
1637 case R_PARISC_DLTIND14R:
1638 case R_PARISC_DLTIND21L:
1639 case R_PARISC_TLS_GD21L:
1640 case R_PARISC_TLS_GD14R:
1641 case R_PARISC_TLS_IE21L:
1642 case R_PARISC_TLS_IE14R:
1645 if (eh->got.refcount > 0)
1646 eh->got.refcount -= 1;
1648 else if (local_got_refcounts != NULL)
1650 if (local_got_refcounts[r_symndx] > 0)
1651 local_got_refcounts[r_symndx] -= 1;
1655 case R_PARISC_TLS_LDM21L:
1656 case R_PARISC_TLS_LDM14R:
1657 htab->tls_ldm_got.refcount -= 1;
1660 case R_PARISC_PCREL12F:
1661 case R_PARISC_PCREL17C:
1662 case R_PARISC_PCREL17F:
1663 case R_PARISC_PCREL22F:
1666 if (eh->plt.refcount > 0)
1667 eh->plt.refcount -= 1;
1671 case R_PARISC_PLABEL14R:
1672 case R_PARISC_PLABEL21L:
1673 case R_PARISC_PLABEL32:
1676 if (eh->plt.refcount > 0)
1677 eh->plt.refcount -= 1;
1679 else if (local_plt_refcounts != NULL)
1681 if (local_plt_refcounts[r_symndx] > 0)
1682 local_plt_refcounts[r_symndx] -= 1;
1694 /* Support for core dump NOTE sections. */
1697 elf32_hppa_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1702 switch (note->descsz)
1707 case 396: /* Linux/hppa */
1709 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1712 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
1721 /* Make a ".reg/999" section. */
1722 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1723 size, note->descpos + offset);
1727 elf32_hppa_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1729 switch (note->descsz)
1734 case 124: /* Linux/hppa elf_prpsinfo. */
1735 elf_tdata (abfd)->core->program
1736 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
1737 elf_tdata (abfd)->core->command
1738 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
1741 /* Note that for some reason, a spurious space is tacked
1742 onto the end of the args in some (at least one anyway)
1743 implementations, so strip it off if it exists. */
1745 char *command = elf_tdata (abfd)->core->command;
1746 int n = strlen (command);
1748 if (0 < n && command[n - 1] == ' ')
1749 command[n - 1] = '\0';
1755 /* Our own version of hide_symbol, so that we can keep plt entries for
1759 elf32_hppa_hide_symbol (struct bfd_link_info *info,
1760 struct elf_link_hash_entry *eh,
1761 bfd_boolean force_local)
1765 eh->forced_local = 1;
1766 if (eh->dynindx != -1)
1769 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
1774 /* STT_GNU_IFUNC symbol must go through PLT. */
1775 if (! hppa_elf_hash_entry (eh)->plabel
1776 && eh->type != STT_GNU_IFUNC)
1779 eh->plt = elf_hash_table (info)->init_plt_offset;
1783 /* Adjust a symbol defined by a dynamic object and referenced by a
1784 regular object. The current definition is in some section of the
1785 dynamic object, but we're not including those sections. We have to
1786 change the definition to something the rest of the link can
1790 elf32_hppa_adjust_dynamic_symbol (struct bfd_link_info *info,
1791 struct elf_link_hash_entry *eh)
1793 struct elf32_hppa_link_hash_table *htab;
1796 /* If this is a function, put it in the procedure linkage table. We
1797 will fill in the contents of the procedure linkage table later. */
1798 if (eh->type == STT_FUNC
1801 /* If the symbol is used by a plabel, we must allocate a PLT slot.
1802 The refcounts are not reliable when it has been hidden since
1803 hide_symbol can be called before the plabel flag is set. */
1804 if (hppa_elf_hash_entry (eh)->plabel
1805 && eh->plt.refcount <= 0)
1806 eh->plt.refcount = 1;
1808 if (eh->plt.refcount <= 0
1810 && eh->root.type != bfd_link_hash_defweak
1811 && ! hppa_elf_hash_entry (eh)->plabel
1812 && (!info->shared || info->symbolic)))
1814 /* The .plt entry is not needed when:
1815 a) Garbage collection has removed all references to the
1817 b) We know for certain the symbol is defined in this
1818 object, and it's not a weak definition, nor is the symbol
1819 used by a plabel relocation. Either this object is the
1820 application or we are doing a shared symbolic link. */
1822 eh->plt.offset = (bfd_vma) -1;
1829 eh->plt.offset = (bfd_vma) -1;
1831 /* If this is a weak symbol, and there is a real definition, the
1832 processor independent code will have arranged for us to see the
1833 real definition first, and we can just use the same value. */
1834 if (eh->u.weakdef != NULL)
1836 if (eh->u.weakdef->root.type != bfd_link_hash_defined
1837 && eh->u.weakdef->root.type != bfd_link_hash_defweak)
1839 eh->root.u.def.section = eh->u.weakdef->root.u.def.section;
1840 eh->root.u.def.value = eh->u.weakdef->root.u.def.value;
1841 if (ELIMINATE_COPY_RELOCS)
1842 eh->non_got_ref = eh->u.weakdef->non_got_ref;
1846 /* This is a reference to a symbol defined by a dynamic object which
1847 is not a function. */
1849 /* If we are creating a shared library, we must presume that the
1850 only references to the symbol are via the global offset table.
1851 For such cases we need not do anything here; the relocations will
1852 be handled correctly by relocate_section. */
1856 /* If there are no references to this symbol that do not use the
1857 GOT, we don't need to generate a copy reloc. */
1858 if (!eh->non_got_ref)
1861 if (ELIMINATE_COPY_RELOCS)
1863 struct elf32_hppa_link_hash_entry *hh;
1864 struct elf32_hppa_dyn_reloc_entry *hdh_p;
1866 hh = hppa_elf_hash_entry (eh);
1867 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
1869 sec = hdh_p->sec->output_section;
1870 if (sec != NULL && (sec->flags & SEC_READONLY) != 0)
1874 /* If we didn't find any dynamic relocs in read-only sections, then
1875 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1878 eh->non_got_ref = 0;
1883 /* We must allocate the symbol in our .dynbss section, which will
1884 become part of the .bss section of the executable. There will be
1885 an entry for this symbol in the .dynsym section. The dynamic
1886 object will contain position independent code, so all references
1887 from the dynamic object to this symbol will go through the global
1888 offset table. The dynamic linker will use the .dynsym entry to
1889 determine the address it must put in the global offset table, so
1890 both the dynamic object and the regular object will refer to the
1891 same memory location for the variable. */
1893 htab = hppa_link_hash_table (info);
1897 /* We must generate a COPY reloc to tell the dynamic linker to
1898 copy the initial value out of the dynamic object and into the
1899 runtime process image. */
1900 if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0)
1902 htab->srelbss->size += sizeof (Elf32_External_Rela);
1906 sec = htab->sdynbss;
1908 return _bfd_elf_adjust_dynamic_copy (eh, sec);
1911 /* Allocate space in the .plt for entries that won't have relocations.
1912 ie. plabel entries. */
1915 allocate_plt_static (struct elf_link_hash_entry *eh, void *inf)
1917 struct bfd_link_info *info;
1918 struct elf32_hppa_link_hash_table *htab;
1919 struct elf32_hppa_link_hash_entry *hh;
1922 if (eh->root.type == bfd_link_hash_indirect)
1925 info = (struct bfd_link_info *) inf;
1926 hh = hppa_elf_hash_entry (eh);
1927 htab = hppa_link_hash_table (info);
1931 if (htab->etab.dynamic_sections_created
1932 && eh->plt.refcount > 0)
1934 /* Make sure this symbol is output as a dynamic symbol.
1935 Undefined weak syms won't yet be marked as dynamic. */
1936 if (eh->dynindx == -1
1937 && !eh->forced_local
1938 && eh->type != STT_PARISC_MILLI)
1940 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
1944 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, eh))
1946 /* Allocate these later. From this point on, h->plabel
1947 means that the plt entry is only used by a plabel.
1948 We'll be using a normal plt entry for this symbol, so
1949 clear the plabel indicator. */
1953 else if (hh->plabel)
1955 /* Make an entry in the .plt section for plabel references
1956 that won't have a .plt entry for other reasons. */
1958 eh->plt.offset = sec->size;
1959 sec->size += PLT_ENTRY_SIZE;
1963 /* No .plt entry needed. */
1964 eh->plt.offset = (bfd_vma) -1;
1970 eh->plt.offset = (bfd_vma) -1;
1977 /* Allocate space in .plt, .got and associated reloc sections for
1981 allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
1983 struct bfd_link_info *info;
1984 struct elf32_hppa_link_hash_table *htab;
1986 struct elf32_hppa_link_hash_entry *hh;
1987 struct elf32_hppa_dyn_reloc_entry *hdh_p;
1989 if (eh->root.type == bfd_link_hash_indirect)
1993 htab = hppa_link_hash_table (info);
1997 hh = hppa_elf_hash_entry (eh);
1999 if (htab->etab.dynamic_sections_created
2000 && eh->plt.offset != (bfd_vma) -1
2002 && eh->plt.refcount > 0)
2004 /* Make an entry in the .plt section. */
2006 eh->plt.offset = sec->size;
2007 sec->size += PLT_ENTRY_SIZE;
2009 /* We also need to make an entry in the .rela.plt section. */
2010 htab->srelplt->size += sizeof (Elf32_External_Rela);
2011 htab->need_plt_stub = 1;
2014 if (eh->got.refcount > 0)
2016 /* Make sure this symbol is output as a dynamic symbol.
2017 Undefined weak syms won't yet be marked as dynamic. */
2018 if (eh->dynindx == -1
2019 && !eh->forced_local
2020 && eh->type != STT_PARISC_MILLI)
2022 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2027 eh->got.offset = sec->size;
2028 sec->size += GOT_ENTRY_SIZE;
2029 /* R_PARISC_TLS_GD* needs two GOT entries */
2030 if ((hh->tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
2031 sec->size += GOT_ENTRY_SIZE * 2;
2032 else if ((hh->tls_type & GOT_TLS_GD) == GOT_TLS_GD)
2033 sec->size += GOT_ENTRY_SIZE;
2034 if (htab->etab.dynamic_sections_created
2036 || (eh->dynindx != -1
2037 && !eh->forced_local)))
2039 htab->srelgot->size += sizeof (Elf32_External_Rela);
2040 if ((hh->tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
2041 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela);
2042 else if ((hh->tls_type & GOT_TLS_GD) == GOT_TLS_GD)
2043 htab->srelgot->size += sizeof (Elf32_External_Rela);
2047 eh->got.offset = (bfd_vma) -1;
2049 if (hh->dyn_relocs == NULL)
2052 /* If this is a -Bsymbolic shared link, then we need to discard all
2053 space allocated for dynamic pc-relative relocs against symbols
2054 defined in a regular object. For the normal shared case, discard
2055 space for relocs that have become local due to symbol visibility
2059 #if RELATIVE_DYNRELOCS
2060 if (SYMBOL_CALLS_LOCAL (info, eh))
2062 struct elf32_hppa_dyn_reloc_entry **hdh_pp;
2064 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; )
2066 hdh_p->count -= hdh_p->relative_count;
2067 hdh_p->relative_count = 0;
2068 if (hdh_p->count == 0)
2069 *hdh_pp = hdh_p->hdh_next;
2071 hdh_pp = &hdh_p->hdh_next;
2076 /* Also discard relocs on undefined weak syms with non-default
2078 if (hh->dyn_relocs != NULL
2079 && eh->root.type == bfd_link_hash_undefweak)
2081 if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT)
2082 hh->dyn_relocs = NULL;
2084 /* Make sure undefined weak symbols are output as a dynamic
2086 else if (eh->dynindx == -1
2087 && !eh->forced_local)
2089 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2096 /* For the non-shared case, discard space for relocs against
2097 symbols which turn out to need copy relocs or are not
2100 if (!eh->non_got_ref
2101 && ((ELIMINATE_COPY_RELOCS
2103 && !eh->def_regular)
2104 || (htab->etab.dynamic_sections_created
2105 && (eh->root.type == bfd_link_hash_undefweak
2106 || eh->root.type == bfd_link_hash_undefined))))
2108 /* Make sure this symbol is output as a dynamic symbol.
2109 Undefined weak syms won't yet be marked as dynamic. */
2110 if (eh->dynindx == -1
2111 && !eh->forced_local
2112 && eh->type != STT_PARISC_MILLI)
2114 if (! bfd_elf_link_record_dynamic_symbol (info, eh))
2118 /* If that succeeded, we know we'll be keeping all the
2120 if (eh->dynindx != -1)
2124 hh->dyn_relocs = NULL;
2130 /* Finally, allocate space. */
2131 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2133 asection *sreloc = elf_section_data (hdh_p->sec)->sreloc;
2134 sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela);
2140 /* This function is called via elf_link_hash_traverse to force
2141 millicode symbols local so they do not end up as globals in the
2142 dynamic symbol table. We ought to be able to do this in
2143 adjust_dynamic_symbol, but our adjust_dynamic_symbol is not called
2144 for all dynamic symbols. Arguably, this is a bug in
2145 elf_adjust_dynamic_symbol. */
2148 clobber_millicode_symbols (struct elf_link_hash_entry *eh,
2149 struct bfd_link_info *info)
2151 if (eh->type == STT_PARISC_MILLI
2152 && !eh->forced_local)
2154 elf32_hppa_hide_symbol (info, eh, TRUE);
2159 /* Find any dynamic relocs that apply to read-only sections. */
2162 readonly_dynrelocs (struct elf_link_hash_entry *eh, void *inf)
2164 struct elf32_hppa_link_hash_entry *hh;
2165 struct elf32_hppa_dyn_reloc_entry *hdh_p;
2167 hh = hppa_elf_hash_entry (eh);
2168 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next)
2170 asection *sec = hdh_p->sec->output_section;
2172 if (sec != NULL && (sec->flags & SEC_READONLY) != 0)
2174 struct bfd_link_info *info = inf;
2176 info->flags |= DF_TEXTREL;
2178 /* Not an error, just cut short the traversal. */
2185 /* Set the sizes of the dynamic sections. */
2188 elf32_hppa_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2189 struct bfd_link_info *info)
2191 struct elf32_hppa_link_hash_table *htab;
2197 htab = hppa_link_hash_table (info);
2201 dynobj = htab->etab.dynobj;
2205 if (htab->etab.dynamic_sections_created)
2207 /* Set the contents of the .interp section to the interpreter. */
2208 if (info->executable)
2210 sec = bfd_get_linker_section (dynobj, ".interp");
2213 sec->size = sizeof ELF_DYNAMIC_INTERPRETER;
2214 sec->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2217 /* Force millicode symbols local. */
2218 elf_link_hash_traverse (&htab->etab,
2219 clobber_millicode_symbols,
2223 /* Set up .got and .plt offsets for local syms, and space for local
2225 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2227 bfd_signed_vma *local_got;
2228 bfd_signed_vma *end_local_got;
2229 bfd_signed_vma *local_plt;
2230 bfd_signed_vma *end_local_plt;
2231 bfd_size_type locsymcount;
2232 Elf_Internal_Shdr *symtab_hdr;
2234 char *local_tls_type;
2236 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
2239 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2241 struct elf32_hppa_dyn_reloc_entry *hdh_p;
2243 for (hdh_p = ((struct elf32_hppa_dyn_reloc_entry *)
2244 elf_section_data (sec)->local_dynrel);
2246 hdh_p = hdh_p->hdh_next)
2248 if (!bfd_is_abs_section (hdh_p->sec)
2249 && bfd_is_abs_section (hdh_p->sec->output_section))
2251 /* Input section has been discarded, either because
2252 it is a copy of a linkonce section or due to
2253 linker script /DISCARD/, so we'll be discarding
2256 else if (hdh_p->count != 0)
2258 srel = elf_section_data (hdh_p->sec)->sreloc;
2259 srel->size += hdh_p->count * sizeof (Elf32_External_Rela);
2260 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0)
2261 info->flags |= DF_TEXTREL;
2266 local_got = elf_local_got_refcounts (ibfd);
2270 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
2271 locsymcount = symtab_hdr->sh_info;
2272 end_local_got = local_got + locsymcount;
2273 local_tls_type = hppa_elf_local_got_tls_type (ibfd);
2275 srel = htab->srelgot;
2276 for (; local_got < end_local_got; ++local_got)
2280 *local_got = sec->size;
2281 sec->size += GOT_ENTRY_SIZE;
2282 if ((*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
2283 sec->size += 2 * GOT_ENTRY_SIZE;
2284 else if ((*local_tls_type & GOT_TLS_GD) == GOT_TLS_GD)
2285 sec->size += GOT_ENTRY_SIZE;
2288 srel->size += sizeof (Elf32_External_Rela);
2289 if ((*local_tls_type & (GOT_TLS_GD | GOT_TLS_IE)) == (GOT_TLS_GD | GOT_TLS_IE))
2290 srel->size += 2 * sizeof (Elf32_External_Rela);
2291 else if ((*local_tls_type & GOT_TLS_GD) == GOT_TLS_GD)
2292 srel->size += sizeof (Elf32_External_Rela);
2296 *local_got = (bfd_vma) -1;
2301 local_plt = end_local_got;
2302 end_local_plt = local_plt + locsymcount;
2303 if (! htab->etab.dynamic_sections_created)
2305 /* Won't be used, but be safe. */
2306 for (; local_plt < end_local_plt; ++local_plt)
2307 *local_plt = (bfd_vma) -1;
2312 srel = htab->srelplt;
2313 for (; local_plt < end_local_plt; ++local_plt)
2317 *local_plt = sec->size;
2318 sec->size += PLT_ENTRY_SIZE;
2320 srel->size += sizeof (Elf32_External_Rela);
2323 *local_plt = (bfd_vma) -1;
2328 if (htab->tls_ldm_got.refcount > 0)
2330 /* Allocate 2 got entries and 1 dynamic reloc for
2331 R_PARISC_TLS_DTPMOD32 relocs. */
2332 htab->tls_ldm_got.offset = htab->sgot->size;
2333 htab->sgot->size += (GOT_ENTRY_SIZE * 2);
2334 htab->srelgot->size += sizeof (Elf32_External_Rela);
2337 htab->tls_ldm_got.offset = -1;
2339 /* Do all the .plt entries without relocs first. The dynamic linker
2340 uses the last .plt reloc to find the end of the .plt (and hence
2341 the start of the .got) for lazy linking. */
2342 elf_link_hash_traverse (&htab->etab, allocate_plt_static, info);
2344 /* Allocate global sym .plt and .got entries, and space for global
2345 sym dynamic relocs. */
2346 elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info);
2348 /* The check_relocs and adjust_dynamic_symbol entry points have
2349 determined the sizes of the various dynamic sections. Allocate
2352 for (sec = dynobj->sections; sec != NULL; sec = sec->next)
2354 if ((sec->flags & SEC_LINKER_CREATED) == 0)
2357 if (sec == htab->splt)
2359 if (htab->need_plt_stub)
2361 /* Make space for the plt stub at the end of the .plt
2362 section. We want this stub right at the end, up
2363 against the .got section. */
2364 int gotalign = bfd_section_alignment (dynobj, htab->sgot);
2365 int pltalign = bfd_section_alignment (dynobj, sec);
2368 if (gotalign > pltalign)
2369 bfd_set_section_alignment (dynobj, sec, gotalign);
2370 mask = ((bfd_size_type) 1 << gotalign) - 1;
2371 sec->size = (sec->size + sizeof (plt_stub) + mask) & ~mask;
2374 else if (sec == htab->sgot
2375 || sec == htab->sdynbss)
2377 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, sec), ".rela"))
2381 /* Remember whether there are any reloc sections other
2383 if (sec != htab->srelplt)
2386 /* We use the reloc_count field as a counter if we need
2387 to copy relocs into the output file. */
2388 sec->reloc_count = 0;
2393 /* It's not one of our sections, so don't allocate space. */
2399 /* If we don't need this section, strip it from the
2400 output file. This is mostly to handle .rela.bss and
2401 .rela.plt. We must create both sections in
2402 create_dynamic_sections, because they must be created
2403 before the linker maps input sections to output
2404 sections. The linker does that before
2405 adjust_dynamic_symbol is called, and it is that
2406 function which decides whether anything needs to go
2407 into these sections. */
2408 sec->flags |= SEC_EXCLUDE;
2412 if ((sec->flags & SEC_HAS_CONTENTS) == 0)
2415 /* Allocate memory for the section contents. Zero it, because
2416 we may not fill in all the reloc sections. */
2417 sec->contents = bfd_zalloc (dynobj, sec->size);
2418 if (sec->contents == NULL)
2422 if (htab->etab.dynamic_sections_created)
2424 /* Like IA-64 and HPPA64, always create a DT_PLTGOT. It
2425 actually has nothing to do with the PLT, it is how we
2426 communicate the LTP value of a load module to the dynamic
2428 #define add_dynamic_entry(TAG, VAL) \
2429 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2431 if (!add_dynamic_entry (DT_PLTGOT, 0))
2434 /* Add some entries to the .dynamic section. We fill in the
2435 values later, in elf32_hppa_finish_dynamic_sections, but we
2436 must add the entries now so that we get the correct size for
2437 the .dynamic section. The DT_DEBUG entry is filled in by the
2438 dynamic linker and used by the debugger. */
2439 if (info->executable)
2441 if (!add_dynamic_entry (DT_DEBUG, 0))
2445 if (htab->srelplt->size != 0)
2447 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
2448 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2449 || !add_dynamic_entry (DT_JMPREL, 0))
2455 if (!add_dynamic_entry (DT_RELA, 0)
2456 || !add_dynamic_entry (DT_RELASZ, 0)
2457 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
2460 /* If any dynamic relocs apply to a read-only section,
2461 then we need a DT_TEXTREL entry. */
2462 if ((info->flags & DF_TEXTREL) == 0)
2463 elf_link_hash_traverse (&htab->etab, readonly_dynrelocs, info);
2465 if ((info->flags & DF_TEXTREL) != 0)
2467 if (!add_dynamic_entry (DT_TEXTREL, 0))
2472 #undef add_dynamic_entry
2477 /* External entry points for sizing and building linker stubs. */
2479 /* Set up various things so that we can make a list of input sections
2480 for each output section included in the link. Returns -1 on error,
2481 0 when no stubs will be needed, and 1 on success. */
2484 elf32_hppa_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
2487 unsigned int bfd_count;
2488 int top_id, top_index;
2490 asection **input_list, **list;
2492 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2497 /* Count the number of input BFDs and find the top input section id. */
2498 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2500 input_bfd = input_bfd->link_next)
2503 for (section = input_bfd->sections;
2505 section = section->next)
2507 if (top_id < section->id)
2508 top_id = section->id;
2511 htab->bfd_count = bfd_count;
2513 amt = sizeof (struct map_stub) * (top_id + 1);
2514 htab->stub_group = bfd_zmalloc (amt);
2515 if (htab->stub_group == NULL)
2518 /* We can't use output_bfd->section_count here to find the top output
2519 section index as some sections may have been removed, and
2520 strip_excluded_output_sections doesn't renumber the indices. */
2521 for (section = output_bfd->sections, top_index = 0;
2523 section = section->next)
2525 if (top_index < section->index)
2526 top_index = section->index;
2529 htab->top_index = top_index;
2530 amt = sizeof (asection *) * (top_index + 1);
2531 input_list = bfd_malloc (amt);
2532 htab->input_list = input_list;
2533 if (input_list == NULL)
2536 /* For sections we aren't interested in, mark their entries with a
2537 value we can check later. */
2538 list = input_list + top_index;
2540 *list = bfd_abs_section_ptr;
2541 while (list-- != input_list);
2543 for (section = output_bfd->sections;
2545 section = section->next)
2547 if ((section->flags & SEC_CODE) != 0)
2548 input_list[section->index] = NULL;
2554 /* The linker repeatedly calls this function for each input section,
2555 in the order that input sections are linked into output sections.
2556 Build lists of input sections to determine groupings between which
2557 we may insert linker stubs. */
2560 elf32_hppa_next_input_section (struct bfd_link_info *info, asection *isec)
2562 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2567 if (isec->output_section->index <= htab->top_index)
2569 asection **list = htab->input_list + isec->output_section->index;
2570 if (*list != bfd_abs_section_ptr)
2572 /* Steal the link_sec pointer for our list. */
2573 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
2574 /* This happens to make the list in reverse order,
2575 which is what we want. */
2576 PREV_SEC (isec) = *list;
2582 /* See whether we can group stub sections together. Grouping stub
2583 sections may result in fewer stubs. More importantly, we need to
2584 put all .init* and .fini* stubs at the beginning of the .init or
2585 .fini output sections respectively, because glibc splits the
2586 _init and _fini functions into multiple parts. Putting a stub in
2587 the middle of a function is not a good idea. */
2590 group_sections (struct elf32_hppa_link_hash_table *htab,
2591 bfd_size_type stub_group_size,
2592 bfd_boolean stubs_always_before_branch)
2594 asection **list = htab->input_list + htab->top_index;
2597 asection *tail = *list;
2598 if (tail == bfd_abs_section_ptr)
2600 while (tail != NULL)
2604 bfd_size_type total;
2605 bfd_boolean big_sec;
2609 big_sec = total >= stub_group_size;
2611 while ((prev = PREV_SEC (curr)) != NULL
2612 && ((total += curr->output_offset - prev->output_offset)
2616 /* OK, the size from the start of CURR to the end is less
2617 than 240000 bytes and thus can be handled by one stub
2618 section. (or the tail section is itself larger than
2619 240000 bytes, in which case we may be toast.)
2620 We should really be keeping track of the total size of
2621 stubs added here, as stubs contribute to the final output
2622 section size. That's a little tricky, and this way will
2623 only break if stubs added total more than 22144 bytes, or
2624 2768 long branch stubs. It seems unlikely for more than
2625 2768 different functions to be called, especially from
2626 code only 240000 bytes long. This limit used to be
2627 250000, but c++ code tends to generate lots of little
2628 functions, and sometimes violated the assumption. */
2631 prev = PREV_SEC (tail);
2632 /* Set up this stub group. */
2633 htab->stub_group[tail->id].link_sec = curr;
2635 while (tail != curr && (tail = prev) != NULL);
2637 /* But wait, there's more! Input sections up to 240000
2638 bytes before the stub section can be handled by it too.
2639 Don't do this if we have a really large section after the
2640 stubs, as adding more stubs increases the chance that
2641 branches may not reach into the stub section. */
2642 if (!stubs_always_before_branch && !big_sec)
2646 && ((total += tail->output_offset - prev->output_offset)
2650 prev = PREV_SEC (tail);
2651 htab->stub_group[tail->id].link_sec = curr;
2657 while (list-- != htab->input_list);
2658 free (htab->input_list);
2662 /* Read in all local syms for all input bfds, and create hash entries
2663 for export stubs if we are building a multi-subspace shared lib.
2664 Returns -1 on error, 1 if export stubs created, 0 otherwise. */
2667 get_local_syms (bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *info)
2669 unsigned int bfd_indx;
2670 Elf_Internal_Sym *local_syms, **all_local_syms;
2671 int stub_changed = 0;
2672 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2677 /* We want to read in symbol extension records only once. To do this
2678 we need to read in the local symbols in parallel and save them for
2679 later use; so hold pointers to the local symbols in an array. */
2680 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2681 all_local_syms = bfd_zmalloc (amt);
2682 htab->all_local_syms = all_local_syms;
2683 if (all_local_syms == NULL)
2686 /* Walk over all the input BFDs, swapping in local symbols.
2687 If we are creating a shared library, create hash entries for the
2691 input_bfd = input_bfd->link_next, bfd_indx++)
2693 Elf_Internal_Shdr *symtab_hdr;
2695 /* We'll need the symbol table in a second. */
2696 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2697 if (symtab_hdr->sh_info == 0)
2700 /* We need an array of the local symbols attached to the input bfd. */
2701 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2702 if (local_syms == NULL)
2704 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2705 symtab_hdr->sh_info, 0,
2707 /* Cache them for elf_link_input_bfd. */
2708 symtab_hdr->contents = (unsigned char *) local_syms;
2710 if (local_syms == NULL)
2713 all_local_syms[bfd_indx] = local_syms;
2715 if (info->shared && htab->multi_subspace)
2717 struct elf_link_hash_entry **eh_syms;
2718 struct elf_link_hash_entry **eh_symend;
2719 unsigned int symcount;
2721 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2722 - symtab_hdr->sh_info);
2723 eh_syms = (struct elf_link_hash_entry **) elf_sym_hashes (input_bfd);
2724 eh_symend = (struct elf_link_hash_entry **) (eh_syms + symcount);
2726 /* Look through the global syms for functions; We need to
2727 build export stubs for all globally visible functions. */
2728 for (; eh_syms < eh_symend; eh_syms++)
2730 struct elf32_hppa_link_hash_entry *hh;
2732 hh = hppa_elf_hash_entry (*eh_syms);
2734 while (hh->eh.root.type == bfd_link_hash_indirect
2735 || hh->eh.root.type == bfd_link_hash_warning)
2736 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
2738 /* At this point in the link, undefined syms have been
2739 resolved, so we need to check that the symbol was
2740 defined in this BFD. */
2741 if ((hh->eh.root.type == bfd_link_hash_defined
2742 || hh->eh.root.type == bfd_link_hash_defweak)
2743 && hh->eh.type == STT_FUNC
2744 && hh->eh.root.u.def.section->output_section != NULL
2745 && (hh->eh.root.u.def.section->output_section->owner
2747 && hh->eh.root.u.def.section->owner == input_bfd
2748 && hh->eh.def_regular
2749 && !hh->eh.forced_local
2750 && ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT)
2753 const char *stub_name;
2754 struct elf32_hppa_stub_hash_entry *hsh;
2756 sec = hh->eh.root.u.def.section;
2757 stub_name = hh_name (hh);
2758 hsh = hppa_stub_hash_lookup (&htab->bstab,
2763 hsh = hppa_add_stub (stub_name, sec, htab);
2767 hsh->target_value = hh->eh.root.u.def.value;
2768 hsh->target_section = hh->eh.root.u.def.section;
2769 hsh->stub_type = hppa_stub_export;
2775 (*_bfd_error_handler) (_("%B: duplicate export stub %s"),
2784 return stub_changed;
2787 /* Determine and set the size of the stub section for a final link.
2789 The basic idea here is to examine all the relocations looking for
2790 PC-relative calls to a target that is unreachable with a "bl"
2794 elf32_hppa_size_stubs
2795 (bfd *output_bfd, bfd *stub_bfd, struct bfd_link_info *info,
2796 bfd_boolean multi_subspace, bfd_signed_vma group_size,
2797 asection * (*add_stub_section) (const char *, asection *),
2798 void (*layout_sections_again) (void))
2800 bfd_size_type stub_group_size;
2801 bfd_boolean stubs_always_before_branch;
2802 bfd_boolean stub_changed;
2803 struct elf32_hppa_link_hash_table *htab = hppa_link_hash_table (info);
2808 /* Stash our params away. */
2809 htab->stub_bfd = stub_bfd;
2810 htab->multi_subspace = multi_subspace;
2811 htab->add_stub_section = add_stub_section;
2812 htab->layout_sections_again = layout_sections_again;
2813 stubs_always_before_branch = group_size < 0;
2815 stub_group_size = -group_size;
2817 stub_group_size = group_size;
2818 if (stub_group_size == 1)
2820 /* Default values. */
2821 if (stubs_always_before_branch)
2823 stub_group_size = 7680000;
2824 if (htab->has_17bit_branch || htab->multi_subspace)
2825 stub_group_size = 240000;
2826 if (htab->has_12bit_branch)
2827 stub_group_size = 7500;
2831 stub_group_size = 6971392;
2832 if (htab->has_17bit_branch || htab->multi_subspace)
2833 stub_group_size = 217856;
2834 if (htab->has_12bit_branch)
2835 stub_group_size = 6808;
2839 group_sections (htab, stub_group_size, stubs_always_before_branch);
2841 switch (get_local_syms (output_bfd, info->input_bfds, info))
2844 if (htab->all_local_syms)
2845 goto error_ret_free_local;
2849 stub_changed = FALSE;
2853 stub_changed = TRUE;
2860 unsigned int bfd_indx;
2863 for (input_bfd = info->input_bfds, bfd_indx = 0;
2865 input_bfd = input_bfd->link_next, bfd_indx++)
2867 Elf_Internal_Shdr *symtab_hdr;
2869 Elf_Internal_Sym *local_syms;
2871 /* We'll need the symbol table in a second. */
2872 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2873 if (symtab_hdr->sh_info == 0)
2876 local_syms = htab->all_local_syms[bfd_indx];
2878 /* Walk over each section attached to the input bfd. */
2879 for (section = input_bfd->sections;
2881 section = section->next)
2883 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2885 /* If there aren't any relocs, then there's nothing more
2887 if ((section->flags & SEC_RELOC) == 0
2888 || section->reloc_count == 0)
2891 /* If this section is a link-once section that will be
2892 discarded, then don't create any stubs. */
2893 if (section->output_section == NULL
2894 || section->output_section->owner != output_bfd)
2897 /* Get the relocs. */
2899 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2901 if (internal_relocs == NULL)
2902 goto error_ret_free_local;
2904 /* Now examine each relocation. */
2905 irela = internal_relocs;
2906 irelaend = irela + section->reloc_count;
2907 for (; irela < irelaend; irela++)
2909 unsigned int r_type, r_indx;
2910 enum elf32_hppa_stub_type stub_type;
2911 struct elf32_hppa_stub_hash_entry *hsh;
2914 bfd_vma destination;
2915 struct elf32_hppa_link_hash_entry *hh;
2917 const asection *id_sec;
2919 r_type = ELF32_R_TYPE (irela->r_info);
2920 r_indx = ELF32_R_SYM (irela->r_info);
2922 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
2924 bfd_set_error (bfd_error_bad_value);
2925 error_ret_free_internal:
2926 if (elf_section_data (section)->relocs == NULL)
2927 free (internal_relocs);
2928 goto error_ret_free_local;
2931 /* Only look for stubs on call instructions. */
2932 if (r_type != (unsigned int) R_PARISC_PCREL12F
2933 && r_type != (unsigned int) R_PARISC_PCREL17F
2934 && r_type != (unsigned int) R_PARISC_PCREL22F)
2937 /* Now determine the call target, its name, value,
2943 if (r_indx < symtab_hdr->sh_info)
2945 /* It's a local symbol. */
2946 Elf_Internal_Sym *sym;
2947 Elf_Internal_Shdr *hdr;
2950 sym = local_syms + r_indx;
2951 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2952 sym_value = sym->st_value;
2953 shndx = sym->st_shndx;
2954 if (shndx < elf_numsections (input_bfd))
2956 hdr = elf_elfsections (input_bfd)[shndx];
2957 sym_sec = hdr->bfd_section;
2958 destination = (sym_value + irela->r_addend
2959 + sym_sec->output_offset
2960 + sym_sec->output_section->vma);
2965 /* It's an external symbol. */
2968 e_indx = r_indx - symtab_hdr->sh_info;
2969 hh = hppa_elf_hash_entry (elf_sym_hashes (input_bfd)[e_indx]);
2971 while (hh->eh.root.type == bfd_link_hash_indirect
2972 || hh->eh.root.type == bfd_link_hash_warning)
2973 hh = hppa_elf_hash_entry (hh->eh.root.u.i.link);
2975 if (hh->eh.root.type == bfd_link_hash_defined
2976 || hh->eh.root.type == bfd_link_hash_defweak)
2978 sym_sec = hh->eh.root.u.def.section;
2979 sym_value = hh->eh.root.u.def.value;
2980 if (sym_sec->output_section != NULL)
2981 destination = (sym_value + irela->r_addend
2982 + sym_sec->output_offset
2983 + sym_sec->output_section->vma);
2985 else if (hh->eh.root.type == bfd_link_hash_undefweak)
2990 else if (hh->eh.root.type == bfd_link_hash_undefined)
2992 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2993 && (ELF_ST_VISIBILITY (hh->eh.other)
2995 && hh->eh.type != STT_PARISC_MILLI))
3000 bfd_set_error (bfd_error_bad_value);
3001 goto error_ret_free_internal;
3005 /* Determine what (if any) linker stub is needed. */
3006 stub_type = hppa_type_of_stub (section, irela, hh,
3008 if (stub_type == hppa_stub_none)
3011 /* Support for grouping stub sections. */
3012 id_sec = htab->stub_group[section->id].link_sec;
3014 /* Get the name of this stub. */
3015 stub_name = hppa_stub_name (id_sec, sym_sec, hh, irela);
3017 goto error_ret_free_internal;
3019 hsh = hppa_stub_hash_lookup (&htab->bstab,
3024 /* The proper stub has already been created. */
3029 hsh = hppa_add_stub (stub_name, section, htab);
3033 goto error_ret_free_internal;
3036 hsh->target_value = sym_value;
3037 hsh->target_section = sym_sec;
3038 hsh->stub_type = stub_type;
3041 if (stub_type == hppa_stub_import)
3042 hsh->stub_type = hppa_stub_import_shared;
3043 else if (stub_type == hppa_stub_long_branch)
3044 hsh->stub_type = hppa_stub_long_branch_shared;
3047 stub_changed = TRUE;
3050 /* We're done with the internal relocs, free them. */
3051 if (elf_section_data (section)->relocs == NULL)
3052 free (internal_relocs);
3059 /* OK, we've added some stubs. Find out the new size of the
3061 for (stub_sec = htab->stub_bfd->sections;
3063 stub_sec = stub_sec->next)
3066 bfd_hash_traverse (&htab->bstab, hppa_size_one_stub, htab);
3068 /* Ask the linker to do its stuff. */
3069 (*htab->layout_sections_again) ();
3070 stub_changed = FALSE;
3073 free (htab->all_local_syms);
3076 error_ret_free_local:
3077 free (htab->all_local_syms);
3081 /* For a final link, this function is called after we have sized the
3082 stubs to provide a value for __gp. */
3085 elf32_hppa_set_gp (bfd *abfd, struct bfd_link_info *info)
3087 struct bfd_link_hash_entry *h;
3088 asection *sec = NULL;
3090 struct elf32_hppa_link_hash_table *htab;
3092 htab = hppa_link_hash_table (info);
3096 h = bfd_link_hash_lookup (&htab->etab.root, "$global$", FALSE, FALSE, FALSE);
3099 && (h->type == bfd_link_hash_defined
3100 || h->type == bfd_link_hash_defweak))
3102 gp_val = h->u.def.value;
3103 sec = h->u.def.section;
3107 asection *splt = bfd_get_section_by_name (abfd, ".plt");
3108 asection *sgot = bfd_get_section_by_name (abfd, ".got");
3110 /* Choose to point our LTP at, in this order, one of .plt, .got,
3111 or .data, if these sections exist. In the case of choosing
3112 .plt try to make the LTP ideal for addressing anywhere in the
3113 .plt or .got with a 14 bit signed offset. Typically, the end
3114 of the .plt is the start of the .got, so choose .plt + 0x2000
3115 if either the .plt or .got is larger than 0x2000. If both
3116 the .plt and .got are smaller than 0x2000, choose the end of
3117 the .plt section. */
3118 sec = strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") == 0
3123 if (gp_val > 0x2000 || (sgot && sgot->size > 0x2000))
3133 if (strcmp (bfd_get_target (abfd), "elf32-hppa-netbsd") != 0)
3135 /* We know we don't have a .plt. If .got is large,
3137 if (sec->size > 0x2000)
3143 /* No .plt or .got. Who cares what the LTP is? */
3144 sec = bfd_get_section_by_name (abfd, ".data");
3150 h->type = bfd_link_hash_defined;
3151 h->u.def.value = gp_val;
3153 h->u.def.section = sec;
3155 h->u.def.section = bfd_abs_section_ptr;
3159 if (sec != NULL && sec->output_section != NULL)
3160 gp_val += sec->output_section->vma + sec->output_offset;
3162 elf_gp (abfd) = gp_val;
3166 /* Build all the stubs associated with the current output file. The
3167 stubs are kept in a hash table attached to the main linker hash
3168 table. We also set up the .plt entries for statically linked PIC
3169 functions here. This function is called via hppaelf_finish in the
3173 elf32_hppa_build_stubs (struct bfd_link_info *info)
3176 struct bfd_hash_table *table;
3177 struct elf32_hppa_link_hash_table *htab;
3179 htab = hppa_link_hash_table (info);
3183 for (stub_sec = htab->stub_bfd->sections;
3185 stub_sec = stub_sec->next)
3189 /* Allocate memory to hold the linker stubs. */
3190 size = stub_sec->size;
3191 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3192 if (stub_sec->contents == NULL && size != 0)
3197 /* Build the stubs as directed by the stub hash table. */
3198 table = &htab->bstab;
3199 bfd_hash_traverse (table, hppa_build_one_stub, info);
3204 /* Return the base vma address which should be subtracted from the real
3205 address when resolving a dtpoff relocation.
3206 This is PT_TLS segment p_vaddr. */
3209 dtpoff_base (struct bfd_link_info *info)
3211 /* If tls_sec is NULL, we should have signalled an error already. */
3212 if (elf_hash_table (info)->tls_sec == NULL)
3214 return elf_hash_table (info)->tls_sec->vma;
3217 /* Return the relocation value for R_PARISC_TLS_TPOFF*.. */
3220 tpoff (struct bfd_link_info *info, bfd_vma address)
3222 struct elf_link_hash_table *htab = elf_hash_table (info);
3224 /* If tls_sec is NULL, we should have signalled an error already. */
3225 if (htab->tls_sec == NULL)
3227 /* hppa TLS ABI is variant I and static TLS block start just after
3228 tcbhead structure which has 2 pointer fields. */
3229 return (address - htab->tls_sec->vma
3230 + align_power ((bfd_vma) 8, htab->tls_sec->alignment_power));
3233 /* Perform a final link. */
3236 elf32_hppa_final_link (bfd *abfd, struct bfd_link_info *info)
3238 /* Invoke the regular ELF linker to do all the work. */
3239 if (!bfd_elf_final_link (abfd, info))
3242 /* If we're producing a final executable, sort the contents of the
3244 if (info->relocatable)
3247 return elf_hppa_sort_unwind (abfd);
3250 /* Record the lowest address for the data and text segments. */
3253 hppa_record_segment_addr (bfd *abfd, asection *section, void *data)
3255 struct elf32_hppa_link_hash_table *htab;
3257 htab = (struct elf32_hppa_link_hash_table*) data;
3261 if ((section->flags & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3264 Elf_Internal_Phdr *p;
3266 p = _bfd_elf_find_segment_containing_section (abfd, section->output_section);
3267 BFD_ASSERT (p != NULL);
3270 if ((section->flags & SEC_READONLY) != 0)
3272 if (value < htab->text_segment_base)
3273 htab->text_segment_base = value;
3277 if (value < htab->data_segment_base)
3278 htab->data_segment_base = value;
3283 /* Perform a relocation as part of a final link. */
3285 static bfd_reloc_status_type
3286 final_link_relocate (asection *input_section,
3288 const Elf_Internal_Rela *rela,
3290 struct elf32_hppa_link_hash_table *htab,
3292 struct elf32_hppa_link_hash_entry *hh,
3293 struct bfd_link_info *info)
3296 unsigned int r_type = ELF32_R_TYPE (rela->r_info);
3297 unsigned int orig_r_type = r_type;
3298 reloc_howto_type *howto = elf_hppa_howto_table + r_type;
3299 int r_format = howto->bitsize;
3300 enum hppa_reloc_field_selector_type_alt r_field;
3301 bfd *input_bfd = input_section->owner;
3302 bfd_vma offset = rela->r_offset;
3303 bfd_vma max_branch_offset = 0;
3304 bfd_byte *hit_data = contents + offset;
3305 bfd_signed_vma addend = rela->r_addend;
3307 struct elf32_hppa_stub_hash_entry *hsh = NULL;
3310 if (r_type == R_PARISC_NONE)
3311 return bfd_reloc_ok;
3313 insn = bfd_get_32 (input_bfd, hit_data);
3315 /* Find out where we are and where we're going. */
3316 location = (offset +
3317 input_section->output_offset +
3318 input_section->output_section->vma);
3320 /* If we are not building a shared library, convert DLTIND relocs to
3326 case R_PARISC_DLTIND21L:
3327 case R_PARISC_TLS_GD21L:
3328 case R_PARISC_TLS_LDM21L:
3329 case R_PARISC_TLS_IE21L:
3330 r_type = R_PARISC_DPREL21L;
3333 case R_PARISC_DLTIND14R:
3334 case R_PARISC_TLS_GD14R:
3335 case R_PARISC_TLS_LDM14R:
3336 case R_PARISC_TLS_IE14R:
3337 r_type = R_PARISC_DPREL14R;
3340 case R_PARISC_DLTIND14F:
3341 r_type = R_PARISC_DPREL14F;
3348 case R_PARISC_PCREL12F:
3349 case R_PARISC_PCREL17F:
3350 case R_PARISC_PCREL22F:
3351 /* If this call should go via the plt, find the import stub in
3354 || sym_sec->output_section == NULL
3356 && hh->eh.plt.offset != (bfd_vma) -1
3357 && hh->eh.dynindx != -1
3360 || !hh->eh.def_regular
3361 || hh->eh.root.type == bfd_link_hash_defweak)))
3363 hsh = hppa_get_stub_entry (input_section, sym_sec,
3367 value = (hsh->stub_offset
3368 + hsh->stub_sec->output_offset
3369 + hsh->stub_sec->output_section->vma);
3372 else if (sym_sec == NULL && hh != NULL
3373 && hh->eh.root.type == bfd_link_hash_undefweak)
3375 /* It's OK if undefined weak. Calls to undefined weak
3376 symbols behave as if the "called" function
3377 immediately returns. We can thus call to a weak
3378 function without first checking whether the function
3384 return bfd_reloc_undefined;
3388 case R_PARISC_PCREL21L:
3389 case R_PARISC_PCREL17C:
3390 case R_PARISC_PCREL17R:
3391 case R_PARISC_PCREL14R:
3392 case R_PARISC_PCREL14F:
3393 case R_PARISC_PCREL32:
3394 /* Make it a pc relative offset. */
3399 case R_PARISC_DPREL21L:
3400 case R_PARISC_DPREL14R:
3401 case R_PARISC_DPREL14F:
3402 /* Convert instructions that use the linkage table pointer (r19) to
3403 instructions that use the global data pointer (dp). This is the
3404 most efficient way of using PIC code in an incomplete executable,
3405 but the user must follow the standard runtime conventions for
3406 accessing data for this to work. */
3407 if (orig_r_type != r_type)
3409 if (r_type == R_PARISC_DPREL21L)
3411 /* GCC sometimes uses a register other than r19 for the
3412 operation, so we must convert any addil instruction
3413 that uses this relocation. */
3414 if ((insn & 0xfc000000) == ((int) OP_ADDIL << 26))
3417 /* We must have a ldil instruction. It's too hard to find
3418 and convert the associated add instruction, so issue an
3420 (*_bfd_error_handler)
3421 (_("%B(%A+0x%lx): %s fixup for insn 0x%x is not supported in a non-shared link"),
3428 else if (r_type == R_PARISC_DPREL14F)
3430 /* This must be a format 1 load/store. Change the base
3432 insn = (insn & 0xfc1ffff) | (27 << 21);
3436 /* For all the DP relative relocations, we need to examine the symbol's
3437 section. If it has no section or if it's a code section, then
3438 "data pointer relative" makes no sense. In that case we don't
3439 adjust the "value", and for 21 bit addil instructions, we change the
3440 source addend register from %dp to %r0. This situation commonly
3441 arises for undefined weak symbols and when a variable's "constness"
3442 is declared differently from the way the variable is defined. For
3443 instance: "extern int foo" with foo defined as "const int foo". */
3444 if (sym_sec == NULL || (sym_sec->flags & SEC_CODE) != 0)
3446 if ((insn & ((0x3f << 26) | (0x1f << 21)))
3447 == (((int) OP_ADDIL << 26) | (27 << 21)))
3449 insn &= ~ (0x1f << 21);
3451 /* Now try to make things easy for the dynamic linker. */
3457 case R_PARISC_DLTIND21L:
3458 case R_PARISC_DLTIND14R:
3459 case R_PARISC_DLTIND14F:
3460 case R_PARISC_TLS_GD21L:
3461 case R_PARISC_TLS_LDM21L:
3462 case R_PARISC_TLS_IE21L:
3463 case R_PARISC_TLS_GD14R:
3464 case R_PARISC_TLS_LDM14R:
3465 case R_PARISC_TLS_IE14R:
3466 value -= elf_gp (input_section->output_section->owner);
3469 case R_PARISC_SEGREL32:
3470 if ((sym_sec->flags & SEC_CODE) != 0)
3471 value -= htab->text_segment_base;
3473 value -= htab->data_segment_base;
3482 case R_PARISC_DIR32:
3483 case R_PARISC_DIR14F:
3484 case R_PARISC_DIR17F:
3485 case R_PARISC_PCREL17C:
3486 case R_PARISC_PCREL14F:
3487 case R_PARISC_PCREL32:
3488 case R_PARISC_DPREL14F:
3489 case R_PARISC_PLABEL32:
3490 case R_PARISC_DLTIND14F:
3491 case R_PARISC_SEGBASE:
3492 case R_PARISC_SEGREL32:
3493 case R_PARISC_TLS_DTPMOD32:
3494 case R_PARISC_TLS_DTPOFF32:
3495 case R_PARISC_TLS_TPREL32:
3499 case R_PARISC_DLTIND21L:
3500 case R_PARISC_PCREL21L:
3501 case R_PARISC_PLABEL21L:
3505 case R_PARISC_DIR21L:
3506 case R_PARISC_DPREL21L:
3507 case R_PARISC_TLS_GD21L:
3508 case R_PARISC_TLS_LDM21L:
3509 case R_PARISC_TLS_LDO21L:
3510 case R_PARISC_TLS_IE21L:
3511 case R_PARISC_TLS_LE21L:
3515 case R_PARISC_PCREL17R:
3516 case R_PARISC_PCREL14R:
3517 case R_PARISC_PLABEL14R:
3518 case R_PARISC_DLTIND14R:
3522 case R_PARISC_DIR17R:
3523 case R_PARISC_DIR14R:
3524 case R_PARISC_DPREL14R:
3525 case R_PARISC_TLS_GD14R:
3526 case R_PARISC_TLS_LDM14R:
3527 case R_PARISC_TLS_LDO14R:
3528 case R_PARISC_TLS_IE14R:
3529 case R_PARISC_TLS_LE14R:
3533 case R_PARISC_PCREL12F:
3534 case R_PARISC_PCREL17F:
3535 case R_PARISC_PCREL22F:
3538 if (r_type == (unsigned int) R_PARISC_PCREL17F)
3540 max_branch_offset = (1 << (17-1)) << 2;
3542 else if (r_type == (unsigned int) R_PARISC_PCREL12F)
3544 max_branch_offset = (1 << (12-1)) << 2;
3548 max_branch_offset = (1 << (22-1)) << 2;
3551 /* sym_sec is NULL on undefined weak syms or when shared on
3552 undefined syms. We've already checked for a stub for the
3553 shared undefined case. */
3554 if (sym_sec == NULL)
3557 /* If the branch is out of reach, then redirect the
3558 call to the local stub for this function. */
3559 if (value + addend + max_branch_offset >= 2*max_branch_offset)
3561 hsh = hppa_get_stub_entry (input_section, sym_sec,
3564 return bfd_reloc_undefined;
3566 /* Munge up the value and addend so that we call the stub
3567 rather than the procedure directly. */
3568 value = (hsh->stub_offset
3569 + hsh->stub_sec->output_offset
3570 + hsh->stub_sec->output_section->vma
3576 /* Something we don't know how to handle. */
3578 return bfd_reloc_notsupported;
3581 /* Make sure we can reach the stub. */
3582 if (max_branch_offset != 0
3583 && value + addend + max_branch_offset >= 2*max_branch_offset)
3585 (*_bfd_error_handler)
3586 (_("%B(%A+0x%lx): cannot reach %s, recompile with -ffunction-sections"),
3590 hsh->bh_root.string);
3591 bfd_set_error (bfd_error_bad_value);
3592 return bfd_reloc_notsupported;
3595 val = hppa_field_adjust (value, addend, r_field);
3599 case R_PARISC_PCREL12F:
3600 case R_PARISC_PCREL17C:
3601 case R_PARISC_PCREL17F:
3602 case R_PARISC_PCREL17R:
3603 case R_PARISC_PCREL22F:
3604 case R_PARISC_DIR17F:
3605 case R_PARISC_DIR17R:
3606 /* This is a branch. Divide the offset by four.
3607 Note that we need to decide whether it's a branch or
3608 otherwise by inspecting the reloc. Inspecting insn won't
3609 work as insn might be from a .word directive. */
3617 insn = hppa_rebuild_insn (insn, val, r_format);
3619 /* Update the instruction word. */
3620 bfd_put_32 (input_bfd, (bfd_vma) insn, hit_data);
3621 return bfd_reloc_ok;
3624 /* Relocate an HPPA ELF section. */
3627 elf32_hppa_relocate_section (bfd *output_bfd,
3628 struct bfd_link_info *info,
3630 asection *input_section,
3632 Elf_Internal_Rela *relocs,
3633 Elf_Internal_Sym *local_syms,
3634 asection **local_sections)
3636 bfd_vma *local_got_offsets;
3637 struct elf32_hppa_link_hash_table *htab;
3638 Elf_Internal_Shdr *symtab_hdr;
3639 Elf_Internal_Rela *rela;
3640 Elf_Internal_Rela *relend;
3642 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3644 htab = hppa_link_hash_table (info);
3648 local_got_offsets = elf_local_got_offsets (input_bfd);
3651 relend = relocs + input_section->reloc_count;
3652 for (; rela < relend; rela++)
3654 unsigned int r_type;
3655 reloc_howto_type *howto;
3656 unsigned int r_symndx;
3657 struct elf32_hppa_link_hash_entry *hh;
3658 Elf_Internal_Sym *sym;
3661 bfd_reloc_status_type rstatus;
3662 const char *sym_name;
3664 bfd_boolean warned_undef;
3666 r_type = ELF32_R_TYPE (rela->r_info);
3667 if (r_type >= (unsigned int) R_PARISC_UNIMPLEMENTED)
3669 bfd_set_error (bfd_error_bad_value);
3672 if (r_type == (unsigned int) R_PARISC_GNU_VTENTRY
3673 || r_type == (unsigned int) R_PARISC_GNU_VTINHERIT)
3676 r_symndx = ELF32_R_SYM (rela->r_info);
3680 warned_undef = FALSE;
3681 if (r_symndx < symtab_hdr->sh_info)
3683 /* This is a local symbol, h defaults to NULL. */
3684 sym = local_syms + r_symndx;
3685 sym_sec = local_sections[r_symndx];
3686 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sym_sec, rela);
3690 struct elf_link_hash_entry *eh;
3691 bfd_boolean unresolved_reloc;
3692 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd);
3694 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rela,
3695 r_symndx, symtab_hdr, sym_hashes,
3696 eh, sym_sec, relocation,
3697 unresolved_reloc, warned_undef);
3699 if (!info->relocatable
3701 && eh->root.type != bfd_link_hash_defined
3702 && eh->root.type != bfd_link_hash_defweak
3703 && eh->root.type != bfd_link_hash_undefweak)
3705 if (info->unresolved_syms_in_objects == RM_IGNORE
3706 && ELF_ST_VISIBILITY (eh->other) == STV_DEFAULT
3707 && eh->type == STT_PARISC_MILLI)
3709 if (! info->callbacks->undefined_symbol
3710 (info, eh_name (eh), input_bfd,
3711 input_section, rela->r_offset, FALSE))
3713 warned_undef = TRUE;
3716 hh = hppa_elf_hash_entry (eh);
3719 if (sym_sec != NULL && discarded_section (sym_sec))
3720 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3722 elf_hppa_howto_table + r_type, 0,
3725 if (info->relocatable)
3728 /* Do any required modifications to the relocation value, and
3729 determine what types of dynamic info we need to output, if
3734 case R_PARISC_DLTIND14F:
3735 case R_PARISC_DLTIND14R:
3736 case R_PARISC_DLTIND21L:
3739 bfd_boolean do_got = 0;
3741 /* Relocation is to the entry for this symbol in the
3742 global offset table. */
3747 off = hh->eh.got.offset;
3748 dyn = htab->etab.dynamic_sections_created;
3749 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
3752 /* If we aren't going to call finish_dynamic_symbol,
3753 then we need to handle initialisation of the .got
3754 entry and create needed relocs here. Since the
3755 offset must always be a multiple of 4, we use the
3756 least significant bit to record whether we have
3757 initialised it already. */
3762 hh->eh.got.offset |= 1;
3769 /* Local symbol case. */
3770 if (local_got_offsets == NULL)
3773 off = local_got_offsets[r_symndx];
3775 /* The offset must always be a multiple of 4. We use
3776 the least significant bit to record whether we have
3777 already generated the necessary reloc. */
3782 local_got_offsets[r_symndx] |= 1;
3791 /* Output a dynamic relocation for this GOT entry.
3792 In this case it is relative to the base of the
3793 object because the symbol index is zero. */
3794 Elf_Internal_Rela outrel;
3796 asection *sec = htab->srelgot;
3798 outrel.r_offset = (off
3799 + htab->sgot->output_offset
3800 + htab->sgot->output_section->vma);
3801 outrel.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
3802 outrel.r_addend = relocation;
3803 loc = sec->contents;
3804 loc += sec->reloc_count++ * sizeof (Elf32_External_Rela);
3805 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3808 bfd_put_32 (output_bfd, relocation,
3809 htab->sgot->contents + off);
3812 if (off >= (bfd_vma) -2)
3815 /* Add the base of the GOT to the relocation value. */
3817 + htab->sgot->output_offset
3818 + htab->sgot->output_section->vma);
3822 case R_PARISC_SEGREL32:
3823 /* If this is the first SEGREL relocation, then initialize
3824 the segment base values. */
3825 if (htab->text_segment_base == (bfd_vma) -1)
3826 bfd_map_over_sections (output_bfd, hppa_record_segment_addr, htab);
3829 case R_PARISC_PLABEL14R:
3830 case R_PARISC_PLABEL21L:
3831 case R_PARISC_PLABEL32:
3832 if (htab->etab.dynamic_sections_created)
3835 bfd_boolean do_plt = 0;
3836 /* If we have a global symbol with a PLT slot, then
3837 redirect this relocation to it. */
3840 off = hh->eh.plt.offset;
3841 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared,
3844 /* In a non-shared link, adjust_dynamic_symbols
3845 isn't called for symbols forced local. We
3846 need to write out the plt entry here. */
3851 hh->eh.plt.offset |= 1;
3858 bfd_vma *local_plt_offsets;
3860 if (local_got_offsets == NULL)
3863 local_plt_offsets = local_got_offsets + symtab_hdr->sh_info;
3864 off = local_plt_offsets[r_symndx];
3866 /* As for the local .got entry case, we use the last
3867 bit to record whether we've already initialised
3868 this local .plt entry. */
3873 local_plt_offsets[r_symndx] |= 1;
3882 /* Output a dynamic IPLT relocation for this
3884 Elf_Internal_Rela outrel;
3886 asection *s = htab->srelplt;
3888 outrel.r_offset = (off
3889 + htab->splt->output_offset
3890 + htab->splt->output_section->vma);
3891 outrel.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
3892 outrel.r_addend = relocation;
3894 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
3895 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3899 bfd_put_32 (output_bfd,
3901 htab->splt->contents + off);
3902 bfd_put_32 (output_bfd,
3903 elf_gp (htab->splt->output_section->owner),
3904 htab->splt->contents + off + 4);
3908 if (off >= (bfd_vma) -2)
3911 /* PLABELs contain function pointers. Relocation is to
3912 the entry for the function in the .plt. The magic +2
3913 offset signals to $$dyncall that the function pointer
3914 is in the .plt and thus has a gp pointer too.
3915 Exception: Undefined PLABELs should have a value of
3918 || (hh->eh.root.type != bfd_link_hash_undefweak
3919 && hh->eh.root.type != bfd_link_hash_undefined))
3922 + htab->splt->output_offset
3923 + htab->splt->output_section->vma
3928 /* Fall through and possibly emit a dynamic relocation. */
3930 case R_PARISC_DIR17F:
3931 case R_PARISC_DIR17R:
3932 case R_PARISC_DIR14F:
3933 case R_PARISC_DIR14R:
3934 case R_PARISC_DIR21L:
3935 case R_PARISC_DPREL14F:
3936 case R_PARISC_DPREL14R:
3937 case R_PARISC_DPREL21L:
3938 case R_PARISC_DIR32:
3939 if ((input_section->flags & SEC_ALLOC) == 0)
3942 /* The reloc types handled here and this conditional
3943 expression must match the code in ..check_relocs and
3944 allocate_dynrelocs. ie. We need exactly the same condition
3945 as in ..check_relocs, with some extra conditions (dynindx
3946 test in this case) to cater for relocs removed by
3947 allocate_dynrelocs. If you squint, the non-shared test
3948 here does indeed match the one in ..check_relocs, the
3949 difference being that here we test DEF_DYNAMIC as well as
3950 !DEF_REGULAR. All common syms end up with !DEF_REGULAR,
3951 which is why we can't use just that test here.
3952 Conversely, DEF_DYNAMIC can't be used in check_relocs as
3953 there all files have not been loaded. */
3956 || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
3957 || hh->eh.root.type != bfd_link_hash_undefweak)
3958 && (IS_ABSOLUTE_RELOC (r_type)
3959 || !SYMBOL_CALLS_LOCAL (info, &hh->eh)))
3962 && hh->eh.dynindx != -1
3963 && !hh->eh.non_got_ref
3964 && ((ELIMINATE_COPY_RELOCS
3965 && hh->eh.def_dynamic
3966 && !hh->eh.def_regular)
3967 || hh->eh.root.type == bfd_link_hash_undefweak
3968 || hh->eh.root.type == bfd_link_hash_undefined)))
3970 Elf_Internal_Rela outrel;
3975 /* When generating a shared object, these relocations
3976 are copied into the output file to be resolved at run
3979 outrel.r_addend = rela->r_addend;
3981 _bfd_elf_section_offset (output_bfd, info, input_section,
3983 skip = (outrel.r_offset == (bfd_vma) -1
3984 || outrel.r_offset == (bfd_vma) -2);
3985 outrel.r_offset += (input_section->output_offset
3986 + input_section->output_section->vma);
3990 memset (&outrel, 0, sizeof (outrel));
3993 && hh->eh.dynindx != -1
3995 || !IS_ABSOLUTE_RELOC (r_type)
3998 || !hh->eh.def_regular))
4000 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type);
4002 else /* It's a local symbol, or one marked to become local. */
4006 /* Add the absolute offset of the symbol. */
4007 outrel.r_addend += relocation;
4009 /* Global plabels need to be processed by the
4010 dynamic linker so that functions have at most one
4011 fptr. For this reason, we need to differentiate
4012 between global and local plabels, which we do by
4013 providing the function symbol for a global plabel
4014 reloc, and no symbol for local plabels. */
4017 && sym_sec->output_section != NULL
4018 && ! bfd_is_abs_section (sym_sec))
4022 osec = sym_sec->output_section;
4023 indx = elf_section_data (osec)->dynindx;
4026 osec = htab->etab.text_index_section;
4027 indx = elf_section_data (osec)->dynindx;
4029 BFD_ASSERT (indx != 0);
4031 /* We are turning this relocation into one
4032 against a section symbol, so subtract out the
4033 output section's address but not the offset
4034 of the input section in the output section. */
4035 outrel.r_addend -= osec->vma;
4038 outrel.r_info = ELF32_R_INFO (indx, r_type);
4040 sreloc = elf_section_data (input_section)->sreloc;
4044 loc = sreloc->contents;
4045 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4046 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4050 case R_PARISC_TLS_LDM21L:
4051 case R_PARISC_TLS_LDM14R:
4055 off = htab->tls_ldm_got.offset;
4060 Elf_Internal_Rela outrel;
4063 outrel.r_offset = (off
4064 + htab->sgot->output_section->vma
4065 + htab->sgot->output_offset);
4066 outrel.r_addend = 0;
4067 outrel.r_info = ELF32_R_INFO (0, R_PARISC_TLS_DTPMOD32);
4068 loc = htab->srelgot->contents;
4069 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4071 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4072 htab->tls_ldm_got.offset |= 1;
4075 /* Add the base of the GOT to the relocation value. */
4077 + htab->sgot->output_offset
4078 + htab->sgot->output_section->vma);
4083 case R_PARISC_TLS_LDO21L:
4084 case R_PARISC_TLS_LDO14R:
4085 relocation -= dtpoff_base (info);
4088 case R_PARISC_TLS_GD21L:
4089 case R_PARISC_TLS_GD14R:
4090 case R_PARISC_TLS_IE21L:
4091 case R_PARISC_TLS_IE14R:
4101 dyn = htab->etab.dynamic_sections_created;
4103 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &hh->eh)
4105 || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh)))
4107 indx = hh->eh.dynindx;
4109 off = hh->eh.got.offset;
4110 tls_type = hh->tls_type;
4114 off = local_got_offsets[r_symndx];
4115 tls_type = hppa_elf_local_got_tls_type (input_bfd)[r_symndx];
4118 if (tls_type == GOT_UNKNOWN)
4125 bfd_boolean need_relocs = FALSE;
4126 Elf_Internal_Rela outrel;
4127 bfd_byte *loc = NULL;
4130 /* The GOT entries have not been initialized yet. Do it
4131 now, and emit any relocations. If both an IE GOT and a
4132 GD GOT are necessary, we emit the GD first. */
4134 if ((info->shared || indx != 0)
4136 || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT
4137 || hh->eh.root.type != bfd_link_hash_undefweak))
4140 loc = htab->srelgot->contents;
4141 /* FIXME (CAO): Should this be reloc_count++ ? */
4142 loc += htab->srelgot->reloc_count * sizeof (Elf32_External_Rela);
4145 if (tls_type & GOT_TLS_GD)
4149 outrel.r_offset = (cur_off
4150 + htab->sgot->output_section->vma
4151 + htab->sgot->output_offset);
4152 outrel.r_info = ELF32_R_INFO (indx,R_PARISC_TLS_DTPMOD32);
4153 outrel.r_addend = 0;
4154 bfd_put_32 (output_bfd, 0, htab->sgot->contents + cur_off);
4155 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4156 htab->srelgot->reloc_count++;
4157 loc += sizeof (Elf32_External_Rela);
4160 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
4161 htab->sgot->contents + cur_off + 4);
4164 bfd_put_32 (output_bfd, 0,
4165 htab->sgot->contents + cur_off + 4);
4166 outrel.r_info = ELF32_R_INFO (indx, R_PARISC_TLS_DTPOFF32);
4167 outrel.r_offset += 4;
4168 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc);
4169 htab->srelgot->reloc_count++;
4170 loc += sizeof (Elf32_External_Rela);
4175 /* If we are not emitting relocations for a
4176 general dynamic reference, then we must be in a
4177 static link or an executable link with the
4178 symbol binding locally. Mark it as belonging
4179 to module 1, the executable. */
4180 bfd_put_32 (output_bfd, 1,
4181 htab->sgot->contents + cur_off);
4182 bfd_put_32 (output_bfd, relocation - dtpoff_base (info),
4183 htab->sgot->contents + cur_off + 4);
4190 if (tls_type & GOT_TLS_IE)
4194 outrel.r_offset = (cur_off
4195 + htab->sgot->output_section->vma
4196 + htab->sgot->output_offset);
4197 outrel.r_info = ELF32_R_INFO (indx, R_PARISC_TLS_TPREL32);
4200 outrel.r_addend = relocation - dtpoff_base (info);
4202 outrel.r_addend = 0;
4204 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4205 htab->srelgot->reloc_count++;
4206 loc += sizeof (Elf32_External_Rela);
4209 bfd_put_32 (output_bfd, tpoff (info, relocation),
4210 htab->sgot->contents + cur_off);
4216 hh->eh.got.offset |= 1;
4218 local_got_offsets[r_symndx] |= 1;
4221 if ((tls_type & GOT_TLS_GD)
4222 && r_type != R_PARISC_TLS_GD21L
4223 && r_type != R_PARISC_TLS_GD14R)
4224 off += 2 * GOT_ENTRY_SIZE;
4226 /* Add the base of the GOT to the relocation value. */
4228 + htab->sgot->output_offset
4229 + htab->sgot->output_section->vma);
4234 case R_PARISC_TLS_LE21L:
4235 case R_PARISC_TLS_LE14R:
4237 relocation = tpoff (info, relocation);
4246 rstatus = final_link_relocate (input_section, contents, rela, relocation,
4247 htab, sym_sec, hh, info);
4249 if (rstatus == bfd_reloc_ok)
4253 sym_name = hh_name (hh);
4256 sym_name = bfd_elf_string_from_elf_section (input_bfd,
4257 symtab_hdr->sh_link,
4259 if (sym_name == NULL)
4261 if (*sym_name == '\0')
4262 sym_name = bfd_section_name (input_bfd, sym_sec);
4265 howto = elf_hppa_howto_table + r_type;
4267 if (rstatus == bfd_reloc_undefined || rstatus == bfd_reloc_notsupported)
4269 if (rstatus == bfd_reloc_notsupported || !warned_undef)
4271 (*_bfd_error_handler)
4272 (_("%B(%A+0x%lx): cannot handle %s for %s"),
4275 (long) rela->r_offset,
4278 bfd_set_error (bfd_error_bad_value);
4284 if (!((*info->callbacks->reloc_overflow)
4285 (info, (hh ? &hh->eh.root : NULL), sym_name, howto->name,
4286 (bfd_vma) 0, input_bfd, input_section, rela->r_offset)))
4294 /* Finish up dynamic symbol handling. We set the contents of various
4295 dynamic sections here. */
4298 elf32_hppa_finish_dynamic_symbol (bfd *output_bfd,
4299 struct bfd_link_info *info,
4300 struct elf_link_hash_entry *eh,
4301 Elf_Internal_Sym *sym)
4303 struct elf32_hppa_link_hash_table *htab;
4304 Elf_Internal_Rela rela;
4307 htab = hppa_link_hash_table (info);
4311 if (eh->plt.offset != (bfd_vma) -1)
4315 if (eh->plt.offset & 1)
4318 /* This symbol has an entry in the procedure linkage table. Set
4321 The format of a plt entry is
4326 if (eh->root.type == bfd_link_hash_defined
4327 || eh->root.type == bfd_link_hash_defweak)
4329 value = eh->root.u.def.value;
4330 if (eh->root.u.def.section->output_section != NULL)
4331 value += (eh->root.u.def.section->output_offset
4332 + eh->root.u.def.section->output_section->vma);
4335 /* Create a dynamic IPLT relocation for this entry. */
4336 rela.r_offset = (eh->plt.offset
4337 + htab->splt->output_offset
4338 + htab->splt->output_section->vma);
4339 if (eh->dynindx != -1)
4341 rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_IPLT);
4346 /* This symbol has been marked to become local, and is
4347 used by a plabel so must be kept in the .plt. */
4348 rela.r_info = ELF32_R_INFO (0, R_PARISC_IPLT);
4349 rela.r_addend = value;
4352 loc = htab->srelplt->contents;
4353 loc += htab->srelplt->reloc_count++ * sizeof (Elf32_External_Rela);
4354 bfd_elf32_swap_reloca_out (htab->splt->output_section->owner, &rela, loc);
4356 if (!eh->def_regular)
4358 /* Mark the symbol as undefined, rather than as defined in
4359 the .plt section. Leave the value alone. */
4360 sym->st_shndx = SHN_UNDEF;
4364 if (eh->got.offset != (bfd_vma) -1
4365 && (hppa_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0
4366 && (hppa_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0)
4368 /* This symbol has an entry in the global offset table. Set it
4371 rela.r_offset = ((eh->got.offset &~ (bfd_vma) 1)
4372 + htab->sgot->output_offset
4373 + htab->sgot->output_section->vma);
4375 /* If this is a -Bsymbolic link and the symbol is defined
4376 locally or was forced to be local because of a version file,
4377 we just want to emit a RELATIVE reloc. The entry in the
4378 global offset table will already have been initialized in the
4379 relocate_section function. */
4381 && (info->symbolic || eh->dynindx == -1)
4384 rela.r_info = ELF32_R_INFO (0, R_PARISC_DIR32);
4385 rela.r_addend = (eh->root.u.def.value
4386 + eh->root.u.def.section->output_offset
4387 + eh->root.u.def.section->output_section->vma);
4391 if ((eh->got.offset & 1) != 0)
4394 bfd_put_32 (output_bfd, 0, htab->sgot->contents + (eh->got.offset & ~1));
4395 rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_DIR32);
4399 loc = htab->srelgot->contents;
4400 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4401 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4408 /* This symbol needs a copy reloc. Set it up. */
4410 if (! (eh->dynindx != -1
4411 && (eh->root.type == bfd_link_hash_defined
4412 || eh->root.type == bfd_link_hash_defweak)))
4415 sec = htab->srelbss;
4417 rela.r_offset = (eh->root.u.def.value
4418 + eh->root.u.def.section->output_offset
4419 + eh->root.u.def.section->output_section->vma);
4421 rela.r_info = ELF32_R_INFO (eh->dynindx, R_PARISC_COPY);
4422 loc = sec->contents + sec->reloc_count++ * sizeof (Elf32_External_Rela);
4423 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4426 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4427 if (eh == htab->etab.hdynamic || eh == htab->etab.hgot)
4429 sym->st_shndx = SHN_ABS;
4435 /* Used to decide how to sort relocs in an optimal manner for the
4436 dynamic linker, before writing them out. */
4438 static enum elf_reloc_type_class
4439 elf32_hppa_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4440 const asection *rel_sec ATTRIBUTE_UNUSED,
4441 const Elf_Internal_Rela *rela)
4443 /* Handle TLS relocs first; we don't want them to be marked
4444 relative by the "if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)"
4446 switch ((int) ELF32_R_TYPE (rela->r_info))
4448 case R_PARISC_TLS_DTPMOD32:
4449 case R_PARISC_TLS_DTPOFF32:
4450 case R_PARISC_TLS_TPREL32:
4451 return reloc_class_normal;
4454 if (ELF32_R_SYM (rela->r_info) == STN_UNDEF)
4455 return reloc_class_relative;
4457 switch ((int) ELF32_R_TYPE (rela->r_info))
4460 return reloc_class_plt;
4462 return reloc_class_copy;
4464 return reloc_class_normal;
4468 /* Finish up the dynamic sections. */
4471 elf32_hppa_finish_dynamic_sections (bfd *output_bfd,
4472 struct bfd_link_info *info)
4475 struct elf32_hppa_link_hash_table *htab;
4479 htab = hppa_link_hash_table (info);
4483 dynobj = htab->etab.dynobj;
4486 /* A broken linker script might have discarded the dynamic sections.
4487 Catch this here so that we do not seg-fault later on. */
4488 if (sgot != NULL && bfd_is_abs_section (sgot->output_section))
4491 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4493 if (htab->etab.dynamic_sections_created)
4495 Elf32_External_Dyn *dyncon, *dynconend;
4500 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4501 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4502 for (; dyncon < dynconend; dyncon++)
4504 Elf_Internal_Dyn dyn;
4507 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4515 /* Use PLTGOT to set the GOT register. */
4516 dyn.d_un.d_ptr = elf_gp (output_bfd);
4521 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4526 dyn.d_un.d_val = s->size;
4530 /* Don't count procedure linkage table relocs in the
4531 overall reloc count. */
4535 dyn.d_un.d_val -= s->size;
4539 /* We may not be using the standard ELF linker script.
4540 If .rela.plt is the first .rela section, we adjust
4541 DT_RELA to not include it. */
4545 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
4547 dyn.d_un.d_ptr += s->size;
4551 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4555 if (sgot != NULL && sgot->size != 0)
4557 /* Fill in the first entry in the global offset table.
4558 We use it to point to our dynamic section, if we have one. */
4559 bfd_put_32 (output_bfd,
4560 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0,
4563 /* The second entry is reserved for use by the dynamic linker. */
4564 memset (sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE);
4566 /* Set .got entry size. */
4567 elf_section_data (sgot->output_section)
4568 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4571 if (htab->splt != NULL && htab->splt->size != 0)
4573 /* Set plt entry size. */
4574 elf_section_data (htab->splt->output_section)
4575 ->this_hdr.sh_entsize = PLT_ENTRY_SIZE;
4577 if (htab->need_plt_stub)
4579 /* Set up the .plt stub. */
4580 memcpy (htab->splt->contents
4581 + htab->splt->size - sizeof (plt_stub),
4582 plt_stub, sizeof (plt_stub));
4584 if ((htab->splt->output_offset
4585 + htab->splt->output_section->vma
4587 != (sgot->output_offset
4588 + sgot->output_section->vma))
4590 (*_bfd_error_handler)
4591 (_(".got section not immediately after .plt section"));
4600 /* Called when writing out an object file to decide the type of a
4603 elf32_hppa_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
4605 if (ELF_ST_TYPE (elf_sym->st_info) == STT_PARISC_MILLI)
4606 return STT_PARISC_MILLI;
4611 /* Misc BFD support code. */
4612 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
4613 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
4614 #define bfd_elf32_bfd_reloc_name_lookup elf_hppa_reloc_name_lookup
4615 #define elf_info_to_howto elf_hppa_info_to_howto
4616 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
4618 /* Stuff for the BFD linker. */
4619 #define bfd_elf32_bfd_final_link elf32_hppa_final_link
4620 #define bfd_elf32_bfd_link_hash_table_create elf32_hppa_link_hash_table_create
4621 #define bfd_elf32_bfd_link_hash_table_free elf32_hppa_link_hash_table_free
4622 #define elf_backend_adjust_dynamic_symbol elf32_hppa_adjust_dynamic_symbol
4623 #define elf_backend_copy_indirect_symbol elf32_hppa_copy_indirect_symbol
4624 #define elf_backend_check_relocs elf32_hppa_check_relocs
4625 #define elf_backend_create_dynamic_sections elf32_hppa_create_dynamic_sections
4626 #define elf_backend_fake_sections elf_hppa_fake_sections
4627 #define elf_backend_relocate_section elf32_hppa_relocate_section
4628 #define elf_backend_hide_symbol elf32_hppa_hide_symbol
4629 #define elf_backend_finish_dynamic_symbol elf32_hppa_finish_dynamic_symbol
4630 #define elf_backend_finish_dynamic_sections elf32_hppa_finish_dynamic_sections
4631 #define elf_backend_size_dynamic_sections elf32_hppa_size_dynamic_sections
4632 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4633 #define elf_backend_gc_mark_hook elf32_hppa_gc_mark_hook
4634 #define elf_backend_gc_sweep_hook elf32_hppa_gc_sweep_hook
4635 #define elf_backend_grok_prstatus elf32_hppa_grok_prstatus
4636 #define elf_backend_grok_psinfo elf32_hppa_grok_psinfo
4637 #define elf_backend_object_p elf32_hppa_object_p
4638 #define elf_backend_final_write_processing elf_hppa_final_write_processing
4639 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4640 #define elf_backend_get_symbol_type elf32_hppa_elf_get_symbol_type
4641 #define elf_backend_reloc_type_class elf32_hppa_reloc_type_class
4642 #define elf_backend_action_discarded elf_hppa_action_discarded
4644 #define elf_backend_can_gc_sections 1
4645 #define elf_backend_can_refcount 1
4646 #define elf_backend_plt_alignment 2
4647 #define elf_backend_want_got_plt 0
4648 #define elf_backend_plt_readonly 0
4649 #define elf_backend_want_plt_sym 0
4650 #define elf_backend_got_header_size 8
4651 #define elf_backend_rela_normal 1
4653 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
4654 #define TARGET_BIG_NAME "elf32-hppa"
4655 #define ELF_ARCH bfd_arch_hppa
4656 #define ELF_TARGET_ID HPPA32_ELF_DATA
4657 #define ELF_MACHINE_CODE EM_PARISC
4658 #define ELF_MAXPAGESIZE 0x1000
4659 #define ELF_OSABI ELFOSABI_HPUX
4660 #define elf32_bed elf32_hppa_hpux_bed
4662 #include "elf32-target.h"
4664 #undef TARGET_BIG_SYM
4665 #define TARGET_BIG_SYM bfd_elf32_hppa_linux_vec
4666 #undef TARGET_BIG_NAME
4667 #define TARGET_BIG_NAME "elf32-hppa-linux"
4669 #define ELF_OSABI ELFOSABI_GNU
4671 #define elf32_bed elf32_hppa_linux_bed
4673 #include "elf32-target.h"
4675 #undef TARGET_BIG_SYM
4676 #define TARGET_BIG_SYM bfd_elf32_hppa_nbsd_vec
4677 #undef TARGET_BIG_NAME
4678 #define TARGET_BIG_NAME "elf32-hppa-netbsd"
4680 #define ELF_OSABI ELFOSABI_NETBSD
4682 #define elf32_bed elf32_hppa_netbsd_bed
4684 #include "elf32-target.h"