1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 1997
3 Free Software Foundation, Inc.
7 Center for Software Science
8 Department of Computer Science
11 This file is part of BFD, the Binary File Descriptor library.
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 #include "elf32-hppa.h"
38 /* We use three different hash tables to hold information for
39 linking PA ELF objects.
41 The first is the elf32_hppa_link_hash_table which is derived
42 from the standard ELF linker hash table. We use this as a place to
43 attach other hash tables and static information.
45 The second is the stub hash table which is derived from the
46 base BFD hash table. The stub hash table holds the information
47 necessary to build the linker stubs during a link. */
49 /* Hash table for linker stubs. */
51 struct elf32_hppa_stub_hash_entry
53 /* Base hash table entry structure, we can get the name of the stub
54 (and thus know exactly what actions it performs) from the base
56 struct bfd_hash_entry root
;
58 /* Offset of the beginning of this stub. */
61 /* Given the symbol's value and its section we can determine its final
62 value when building the stubs (so the stub knows where to jump. */
63 symvalue target_value
;
64 asection
*target_section
;
67 struct elf32_hppa_stub_hash_table
69 /* The hash table itself. */
70 struct bfd_hash_table root
;
75 /* Where to place the next stub. */
78 /* Current offset in the stub section. */
83 struct elf32_hppa_link_hash_entry
85 struct elf_link_hash_entry root
;
88 struct elf32_hppa_link_hash_table
90 /* The main hash table. */
91 struct elf_link_hash_table root
;
93 /* The stub hash table. */
94 struct elf32_hppa_stub_hash_table
*stub_hash_table
;
96 /* A count of the number of output symbols. */
97 unsigned int output_symbol_count
;
99 /* Stuff so we can handle DP relative relocations. */
101 int global_sym_defined
;
104 /* ELF32/HPPA relocation support
106 This file contains ELF32/HPPA relocation support as specified
107 in the Stratus FTX/Golf Object File Format (SED-1762) dated
110 #include "elf32-hppa.h"
111 #include "hppa_stubs.h"
113 static unsigned long hppa_elf_relocate_insn
114 PARAMS ((bfd
*, asection
*, unsigned long, unsigned long, long,
115 long, unsigned long, unsigned long, unsigned long));
117 static boolean hppa_elf_is_local_label_name
PARAMS ((bfd
*, const char *));
119 static boolean elf32_hppa_add_symbol_hook
120 PARAMS ((bfd
*, struct bfd_link_info
*, const Elf_Internal_Sym
*,
121 const char **, flagword
*, asection
**, bfd_vma
*));
123 static bfd_reloc_status_type elf32_hppa_bfd_final_link_relocate
124 PARAMS ((reloc_howto_type
*, bfd
*, bfd
*, asection
*,
125 bfd_byte
*, bfd_vma
, bfd_vma
, bfd_vma
, struct bfd_link_info
*,
126 asection
*, const char *, int));
128 static struct bfd_link_hash_table
*elf32_hppa_link_hash_table_create
131 static struct bfd_hash_entry
*
132 elf32_hppa_stub_hash_newfunc
133 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
136 elf32_hppa_relocate_section
137 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*,
138 bfd_byte
*, Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
141 elf32_hppa_stub_hash_table_init
142 PARAMS ((struct elf32_hppa_stub_hash_table
*, bfd
*,
143 struct bfd_hash_entry
*(*) PARAMS ((struct bfd_hash_entry
*,
144 struct bfd_hash_table
*,
148 elf32_hppa_build_one_stub
PARAMS ((struct bfd_hash_entry
*, PTR
));
150 static unsigned int elf32_hppa_size_of_stub
151 PARAMS ((bfd_vma
, bfd_vma
, const char *));
153 static void elf32_hppa_name_of_stub
154 PARAMS ((bfd_vma
, bfd_vma
, char *));
156 /* For linker stub hash tables. */
157 #define elf32_hppa_stub_hash_lookup(table, string, create, copy) \
158 ((struct elf32_hppa_stub_hash_entry *) \
159 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
161 #define elf32_hppa_stub_hash_traverse(table, func, info) \
164 (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
167 /* For HPPA linker hash table. */
169 #define elf32_hppa_link_hash_lookup(table, string, create, copy, follow)\
170 ((struct elf32_hppa_link_hash_entry *) \
171 elf_link_hash_lookup (&(table)->root, (string), (create), \
174 #define elf32_hppa_link_hash_traverse(table, func, info) \
175 (elf_link_hash_traverse \
177 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
180 /* Get the PA ELF linker hash table from a link_info structure. */
182 #define elf32_hppa_hash_table(p) \
183 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
186 /* Assorted hash table functions. */
188 /* Initialize an entry in the stub hash table. */
190 static struct bfd_hash_entry
*
191 elf32_hppa_stub_hash_newfunc (entry
, table
, string
)
192 struct bfd_hash_entry
*entry
;
193 struct bfd_hash_table
*table
;
196 struct elf32_hppa_stub_hash_entry
*ret
;
198 ret
= (struct elf32_hppa_stub_hash_entry
*) entry
;
200 /* Allocate the structure if it has not already been allocated by a
203 ret
= ((struct elf32_hppa_stub_hash_entry
*)
204 bfd_hash_allocate (table
,
205 sizeof (struct elf32_hppa_stub_hash_entry
)));
209 /* Call the allocation method of the superclass. */
210 ret
= ((struct elf32_hppa_stub_hash_entry
*)
211 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
215 /* Initialize the local fields. */
217 ret
->target_value
= 0;
218 ret
->target_section
= NULL
;
221 return (struct bfd_hash_entry
*) ret
;
224 /* Initialize a stub hash table. */
227 elf32_hppa_stub_hash_table_init (table
, stub_bfd
, newfunc
)
228 struct elf32_hppa_stub_hash_table
*table
;
230 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
231 struct bfd_hash_table
*,
236 table
->stub_bfd
= stub_bfd
;
237 return (bfd_hash_table_init (&table
->root
, newfunc
));
240 /* Create the derived linker hash table. The PA ELF port uses the derived
241 hash table to keep information specific to the PA ELF linker (without
242 using static variables). */
244 static struct bfd_link_hash_table
*
245 elf32_hppa_link_hash_table_create (abfd
)
248 struct elf32_hppa_link_hash_table
*ret
;
250 ret
= ((struct elf32_hppa_link_hash_table
*)
251 bfd_alloc (abfd
, sizeof (struct elf32_hppa_link_hash_table
)));
254 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
255 _bfd_elf_link_hash_newfunc
))
257 bfd_release (abfd
, ret
);
260 ret
->stub_hash_table
= NULL
;
261 ret
->output_symbol_count
= 0;
262 ret
->global_value
= 0;
263 ret
->global_sym_defined
= 0;
265 return &ret
->root
.root
;
268 /* Relocate the given INSN given the various input parameters.
270 FIXME: endianness and sizeof (long) issues abound here. */
273 hppa_elf_relocate_insn (abfd
, input_sect
, insn
, address
, sym_value
,
274 r_addend
, r_format
, r_field
, pcrel
)
276 asection
*input_sect
;
278 unsigned long address
;
281 unsigned long r_format
;
282 unsigned long r_field
;
285 unsigned char opcode
= get_opcode (insn
);
305 constant_value
= HPPA_R_CONSTANT (r_addend
);
308 sym_value
-= address
;
310 sym_value
= hppa_field_adjust (sym_value
, constant_value
, r_field
);
311 return hppa_rebuild_insn (abfd
, insn
, sym_value
, r_format
);
316 /* XXX computing constant_value is not needed??? */
317 constant_value
= assemble_17 ((insn
& 0x001f0000) >> 16,
318 (insn
& 0x00001ffc) >> 2,
321 constant_value
= (constant_value
<< 15) >> 15;
325 address
+ input_sect
->output_offset
326 + input_sect
->output_section
->vma
;
327 sym_value
= hppa_field_adjust (sym_value
, -8, r_field
);
330 sym_value
= hppa_field_adjust (sym_value
, constant_value
, r_field
);
332 return hppa_rebuild_insn (abfd
, insn
, sym_value
>> 2, r_format
);
337 constant_value
= HPPA_R_CONSTANT (r_addend
);
340 sym_value
-= address
;
342 return hppa_field_adjust (sym_value
, constant_value
, r_field
);
349 /* Relocate an HPPA ELF section. */
352 elf32_hppa_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
353 contents
, relocs
, local_syms
, local_sections
)
355 struct bfd_link_info
*info
;
357 asection
*input_section
;
359 Elf_Internal_Rela
*relocs
;
360 Elf_Internal_Sym
*local_syms
;
361 asection
**local_sections
;
363 Elf_Internal_Shdr
*symtab_hdr
;
364 Elf_Internal_Rela
*rel
;
365 Elf_Internal_Rela
*relend
;
367 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
370 relend
= relocs
+ input_section
->reloc_count
;
371 for (; rel
< relend
; rel
++)
374 reloc_howto_type
*howto
;
375 unsigned long r_symndx
;
376 struct elf_link_hash_entry
*h
;
377 Elf_Internal_Sym
*sym
;
380 bfd_reloc_status_type r
;
381 const char *sym_name
;
383 r_type
= ELF32_R_TYPE (rel
->r_info
);
384 if (r_type
< 0 || r_type
>= (int) R_PARISC_UNIMPLEMENTED
)
386 bfd_set_error (bfd_error_bad_value
);
389 howto
= elf_hppa_howto_table
+ r_type
;
391 r_symndx
= ELF32_R_SYM (rel
->r_info
);
393 if (info
->relocateable
)
395 /* This is a relocateable link. We don't have to change
396 anything, unless the reloc is against a section symbol,
397 in which case we have to adjust according to where the
398 section symbol winds up in the output section. */
399 if (r_symndx
< symtab_hdr
->sh_info
)
401 sym
= local_syms
+ r_symndx
;
402 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
404 sym_sec
= local_sections
[r_symndx
];
405 rel
->r_addend
+= sym_sec
->output_offset
;
412 /* This is a final link. */
416 if (r_symndx
< symtab_hdr
->sh_info
)
418 sym
= local_syms
+ r_symndx
;
419 sym_sec
= local_sections
[r_symndx
];
420 relocation
= ((ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
422 + sym_sec
->output_offset
423 + sym_sec
->output_section
->vma
);
429 indx
= r_symndx
- symtab_hdr
->sh_info
;
430 h
= elf_sym_hashes (input_bfd
)[indx
];
431 while (h
->root
.type
== bfd_link_hash_indirect
432 || h
->root
.type
== bfd_link_hash_warning
)
433 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
434 if (h
->root
.type
== bfd_link_hash_defined
435 || h
->root
.type
== bfd_link_hash_defweak
)
437 sym_sec
= h
->root
.u
.def
.section
;
438 relocation
= (h
->root
.u
.def
.value
439 + sym_sec
->output_offset
440 + sym_sec
->output_section
->vma
);
442 else if (h
->root
.type
== bfd_link_hash_undefweak
)
446 if (!((*info
->callbacks
->undefined_symbol
)
447 (info
, h
->root
.root
.string
, input_bfd
,
448 input_section
, rel
->r_offset
)))
455 sym_name
= h
->root
.root
.string
;
458 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
461 if (sym_name
== NULL
)
463 if (*sym_name
== '\0')
464 sym_name
= bfd_section_name (input_bfd
, sym_sec
);
467 r
= elf32_hppa_bfd_final_link_relocate (howto
, input_bfd
, output_bfd
,
468 input_section
, contents
,
469 rel
->r_offset
, relocation
,
470 rel
->r_addend
, info
, sym_sec
,
471 sym_name
, h
== NULL
);
473 if (r
!= bfd_reloc_ok
)
477 /* This can happen for DP relative relocs if $global$ is
478 undefined. This is a panic situation so we don't try
480 case bfd_reloc_undefined
:
481 case bfd_reloc_notsupported
:
482 if (!((*info
->callbacks
->undefined_symbol
)
483 (info
, "$global$", input_bfd
,
484 input_section
, rel
->r_offset
)))
487 case bfd_reloc_dangerous
:
489 /* We use this return value to indicate that we performed
490 a "dangerous" relocation. This doesn't mean we did
491 the wrong thing, it just means there may be some cleanup
492 that needs to be done here.
494 In particular we had to swap the last call insn and its
495 delay slot. If the delay slot insn needed a relocation,
496 then we'll need to adjust the next relocation entry's
497 offset to account for the fact that the insn moved.
499 This hair wouldn't be necessary if we inserted stubs
500 between procedures and used a "bl" to get to the stub. */
503 Elf_Internal_Rela
*next_rel
= rel
+ 1;
505 if (rel
->r_offset
+ 4 == next_rel
->r_offset
)
506 next_rel
->r_offset
-= 4;
511 case bfd_reloc_outofrange
:
512 case bfd_reloc_overflow
:
514 if (!((*info
->callbacks
->reloc_overflow
)
515 (info
, sym_name
, howto
->name
, (bfd_vma
) 0,
516 input_bfd
, input_section
, rel
->r_offset
)))
527 /* Actually perform a relocation as part of a final link. This can get
528 rather hairy when linker stubs are needed. */
530 static bfd_reloc_status_type
531 elf32_hppa_bfd_final_link_relocate (howto
, input_bfd
, output_bfd
,
532 input_section
, contents
, offset
, value
,
533 addend
, info
, sym_sec
, sym_name
, is_local
)
534 reloc_howto_type
*howto
;
537 asection
*input_section
;
542 struct bfd_link_info
*info
;
544 const char *sym_name
;
548 unsigned long r_type
= howto
->type
;
549 unsigned long r_format
= howto
->bitsize
;
550 unsigned long r_field
= e_fsel
;
551 bfd_byte
*hit_data
= contents
+ offset
;
552 boolean r_pcrel
= howto
->pc_relative
;
554 insn
= bfd_get_32 (input_bfd
, hit_data
);
556 /* Make sure we have a value for $global$. FIXME isn't this effectively
557 just like the gp pointer on MIPS? Can we use those routines for this
559 if (!elf32_hppa_hash_table (info
)->global_sym_defined
)
561 struct elf_link_hash_entry
*h
;
564 h
= elf_link_hash_lookup (elf_hash_table (info
), "$global$", false,
567 /* If there isn't a $global$, then we're in deep trouble. */
569 return bfd_reloc_notsupported
;
571 /* If $global$ isn't a defined symbol, then we're still in deep
573 if (h
->root
.type
!= bfd_link_hash_defined
)
574 return bfd_reloc_undefined
;
576 sec
= h
->root
.u
.def
.section
;
577 elf32_hppa_hash_table (info
)->global_value
= (h
->root
.u
.def
.value
578 + sec
->output_section
->vma
579 + sec
->output_offset
);
580 elf32_hppa_hash_table (info
)->global_sym_defined
= 1;
589 case R_PARISC_DIR17F
:
590 case R_PARISC_PCREL17C
:
592 goto do_basic_type_1
;
593 case R_PARISC_DIR21L
:
594 case R_PARISC_PCREL21L
:
596 goto do_basic_type_1
;
597 case R_PARISC_DIR17R
:
598 case R_PARISC_PCREL17R
:
599 case R_PARISC_DIR14R
:
600 case R_PARISC_PCREL14R
:
602 goto do_basic_type_1
;
604 /* For all the DP relative relocations, we need to examine the symbol's
605 section. If it's a code section, then "data pointer relative" makes
606 no sense. In that case we don't adjust the "value", and for 21 bit
607 addil instructions, we change the source addend register from %dp to
609 case R_PARISC_DPREL21L
:
611 if (sym_sec
->flags
& SEC_CODE
)
613 if ((insn
& 0xfc000000) >> 26 == 0xa
614 && (insn
& 0x03e00000) >> 21 == 0x1b)
618 value
-= elf32_hppa_hash_table (info
)->global_value
;
619 goto do_basic_type_1
;
620 case R_PARISC_DPREL14R
:
622 if ((sym_sec
->flags
& SEC_CODE
) == 0)
623 value
-= elf32_hppa_hash_table (info
)->global_value
;
624 goto do_basic_type_1
;
625 case R_PARISC_DPREL14F
:
627 if ((sym_sec
->flags
& SEC_CODE
) == 0)
628 value
-= elf32_hppa_hash_table (info
)->global_value
;
629 goto do_basic_type_1
;
631 /* These cases are separate as they may involve a lot more work
632 to deal with linker stubs. */
633 case R_PARISC_PLABEL32
:
634 case R_PARISC_PLABEL21L
:
635 case R_PARISC_PLABEL14R
:
636 case R_PARISC_PCREL17F
:
639 unsigned int len
, caller_args
, callee_args
;
640 char *new_name
, *stub_name
;
642 /* Get the field selector right. We'll need it in a minute. */
643 if (r_type
== R_PARISC_PCREL17F
644 || r_type
== R_PARISC_PLABEL32
)
646 else if (r_type
== R_PARISC_PLABEL21L
)
648 else if (r_type
== R_PARISC_PLABEL14R
)
651 /* Find out where we are and where we're going. */
653 input_section
->output_offset
+
654 input_section
->output_section
->vma
);
656 len
= strlen (sym_name
) + 1;
659 new_name
= bfd_malloc (len
);
661 return bfd_reloc_notsupported
;
662 strcpy (new_name
, sym_name
);
664 /* Local symbols have unique IDs. */
666 sprintf (new_name
+ len
- 10, "_%08x", (int)sym_sec
);
668 /* Any kind of linker stub needed? */
669 if (((int)(value
- location
) > 0x3ffff)
670 || ((int)(value
- location
) < (int)0xfffc0000))
672 struct elf32_hppa_stub_hash_table
*stub_hash_table
;
673 struct elf32_hppa_stub_hash_entry
*stub_hash
;
674 asection
*stub_section
;
676 /* Build a name for the stub. */
678 len
= strlen (new_name
);
680 stub_name
= bfd_malloc (len
);
682 return bfd_reloc_notsupported
;
683 elf32_hppa_name_of_stub (location
, value
, stub_name
);
684 strcat (stub_name
, new_name
);
687 stub_hash_table
= elf32_hppa_hash_table (info
)->stub_hash_table
;
690 = elf32_hppa_stub_hash_lookup (stub_hash_table
, stub_name
,
693 /* We're done with that name. */
696 /* The stub BFD only has one section. */
697 stub_section
= stub_hash_table
->stub_bfd
->sections
;
699 if (stub_hash
!= NULL
)
701 if (r_type
== R_PARISC_PCREL17F
)
703 unsigned long delay_insn
;
704 unsigned int opcode
, rtn_reg
, ldo_target_reg
, ldo_src_reg
;
706 /* We'll need to peek at the next insn. */
707 delay_insn
= bfd_get_32 (input_bfd
, hit_data
+ 4);
708 opcode
= get_opcode (delay_insn
);
710 /* We also need to know the return register for this
712 rtn_reg
= (insn
& 0x03e00000) >> 21;
714 ldo_src_reg
= (delay_insn
& 0x03e00000) >> 21;
715 ldo_target_reg
= (delay_insn
& 0x001f0000) >> 16;
717 /* Munge up the value and other parameters for
718 hppa_elf_relocate_insn. */
720 value
= (stub_hash
->offset
721 + stub_section
->output_offset
722 + stub_section
->output_section
->vma
);
729 /* We need to peek at the delay insn and determine if
730 we'll need to swap the branch and its delay insn. */
733 && ldo_target_reg
== rtn_reg
)
734 || (delay_insn
== 0x08000240))
736 /* No need to swap the branch and its delay slot, but
737 we do need to make sure to jump past the return
738 pointer update in the stub. */
741 /* If the delay insn does a return pointer adjustment,
742 then we have to make sure it stays valid. */
744 && ldo_target_reg
== rtn_reg
)
746 delay_insn
&= 0xfc00ffff;
747 delay_insn
|= ((31 << 21) | (31 << 16));
748 bfd_put_32 (input_bfd
, delay_insn
, hit_data
+ 4);
750 /* Use a BLE to reach the stub. */
755 /* Wonderful, we have to swap the call insn and its
757 bfd_put_32 (input_bfd
, delay_insn
, hit_data
);
758 /* Use a BLE,n to reach the stub. */
759 insn
= (BLE_SR4_R0
| 0x2);
760 bfd_put_32 (input_bfd
, insn
, hit_data
+ 4);
761 insn
= hppa_elf_relocate_insn (input_bfd
,
767 /* Update the instruction word. */
768 bfd_put_32 (input_bfd
, insn
, hit_data
+ 4);
769 return bfd_reloc_dangerous
;
773 return bfd_reloc_notsupported
;
776 goto do_basic_type_1
;
780 insn
= hppa_elf_relocate_insn (input_bfd
, input_section
, insn
,
781 offset
, value
, addend
, r_format
,
785 /* Something we don't know how to handle. */
787 return bfd_reloc_notsupported
;
790 /* Update the instruction word. */
791 bfd_put_32 (input_bfd
, insn
, hit_data
);
792 return (bfd_reloc_ok
);
795 /* Return true if SYM represents a local label symbol. */
798 hppa_elf_is_local_label_name (abfd
, name
)
802 return (name
[0] == 'L' && name
[1] == '$');
805 /* Undo the generic ELF code's subtraction of section->vma from the
806 value of each external symbol. */
809 elf32_hppa_add_symbol_hook (abfd
, info
, sym
, namep
, flagsp
, secp
, valp
)
811 struct bfd_link_info
*info
;
812 const Elf_Internal_Sym
*sym
;
818 *valp
+= (*secp
)->vma
;
822 /* Determine the name of the stub needed to perform a call assuming the
823 argument relocation bits for caller and callee are in CALLER and CALLEE
824 for a call from LOCATION to DESTINATION. Copy the name into STUB_NAME. */
827 elf32_hppa_name_of_stub (location
, destination
, stub_name
)
828 bfd_vma location
, destination
;
831 strcpy (stub_name
, "_____long_branch_stub_");
834 /* Compute the size of the stub needed to call from LOCATION to DESTINATION
835 (a function named SYM_NAME), with argument relocation bits CALLER and
836 CALLEE. Return zero if no stub is needed to perform such a call. */
839 elf32_hppa_size_of_stub (location
, destination
, sym_name
)
840 bfd_vma location
, destination
;
841 const char *sym_name
;
843 /* Determine if a long branch stub is needed. */
844 if (!(((int)(location
- destination
) > 0x3ffff)
845 || ((int)(location
- destination
) < (int)0xfffc0000)))
848 if (!strncmp ("$$", sym_name
, 2)
849 && strcmp ("$$dyncall", sym_name
))
855 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
856 IN_ARGS contains the stub BFD and link info pointers. */
859 elf32_hppa_build_one_stub (gen_entry
, in_args
)
860 struct bfd_hash_entry
*gen_entry
;
863 void **args
= (void **)in_args
;
864 bfd
*stub_bfd
= (bfd
*)args
[0];
865 struct bfd_link_info
*info
= (struct bfd_link_info
*)args
[1];
866 struct elf32_hppa_stub_hash_entry
*entry
;
867 struct elf32_hppa_stub_hash_table
*stub_hash_table
;
870 const char *sym_name
;
872 /* Initialize pointers to the stub hash table, the particular entry we
873 are building a stub for, and where (in memory) we should place the stub
875 entry
= (struct elf32_hppa_stub_hash_entry
*)gen_entry
;
876 stub_hash_table
= elf32_hppa_hash_table(info
)->stub_hash_table
;
877 loc
= stub_hash_table
->location
;
879 /* Make a note of the offset within the stubs for this entry. */
880 entry
->offset
= stub_hash_table
->offset
;
882 /* The symbol's name starts at offset 22. */
883 sym_name
= entry
->root
.string
+ 22;
885 sym_value
= (entry
->target_value
886 + entry
->target_section
->output_offset
887 + entry
->target_section
->output_section
->vma
);
891 /* Create one of two variant long branch stubs. One for $$dyncall and
892 normal calls, the other for calls to millicode. */
894 int millicode_call
= 0;
896 if (!strncmp ("$$", sym_name
, 2) && strcmp ("$$dyncall", sym_name
))
899 /* First the return pointer adjustment. Depending on exact calling
900 sequence this instruction may be skipped. */
901 bfd_put_32 (stub_bfd
, LDO_M4_R31_R31
, loc
);
903 /* The next two instructions are the long branch itself. A long branch
904 is formed with "ldil" loading the upper bits of the target address
905 into a register, then branching with "be" which adds in the lower bits.
906 Long branches to millicode nullify the delay slot of the "be". */
907 insn
= hppa_rebuild_insn (stub_bfd
, LDIL_R1
,
908 hppa_field_adjust (sym_value
, 0, e_lrsel
), 21);
909 bfd_put_32 (stub_bfd
, insn
, loc
+ 4);
910 insn
= hppa_rebuild_insn (stub_bfd
, BE_SR4_R1
| (millicode_call
? 2 : 0),
911 hppa_field_adjust (sym_value
, 0, e_rrsel
) >> 2,
913 bfd_put_32 (stub_bfd
, insn
, loc
+ 8);
917 /* The sequence to call this stub places the return pointer into %r31,
918 the final target expects the return pointer in %r2, so copy the
919 return pointer into the proper register. */
920 bfd_put_32 (stub_bfd
, COPY_R31_R2
, loc
+ 12);
922 /* Update the location and offsets. */
923 stub_hash_table
->location
+= 16;
924 stub_hash_table
->offset
+= 16;
928 /* Update the location and offsets. */
929 stub_hash_table
->location
+= 12;
930 stub_hash_table
->offset
+= 12;
937 /* External entry points for sizing and building linker stubs. */
939 /* Build all the stubs associated with the current output file. The
940 stubs are kept in a hash table attached to the main linker hash
941 table. This is called via hppaelf_finish in the linker. */
944 elf32_hppa_build_stubs (stub_bfd
, info
)
946 struct bfd_link_info
*info
;
948 /* The stub BFD only has one section. */
949 asection
*stub_sec
= stub_bfd
->sections
;
950 struct elf32_hppa_stub_hash_table
*table
;
954 /* So we can pass both the BFD for the stubs and the link info
955 structure to the routine which actually builds stubs. */
959 /* Allocate memory to hold the linker stubs. */
960 size
= bfd_section_size (stub_bfd
, stub_sec
);
961 stub_sec
->contents
= (unsigned char *) bfd_zalloc (stub_bfd
, size
);
962 if (stub_sec
->contents
== NULL
)
964 table
= elf32_hppa_hash_table(info
)->stub_hash_table
;
965 table
->location
= stub_sec
->contents
;
967 /* Build the stubs as directed by the stub hash table. */
968 elf32_hppa_stub_hash_traverse (table
, elf32_hppa_build_one_stub
, args
);
973 /* Determine and set the size of the stub section for a final link.
975 The basic idea here is to examine all the relocations looking for
976 PC-relative calls to a target that is unreachable with a "bl"
977 instruction or calls where the caller and callee disagree on the
978 location of their arguments or return value. */
981 elf32_hppa_size_stubs (stub_bfd
, output_bfd
, link_info
)
984 struct bfd_link_info
*link_info
;
987 asection
*section
, *stub_sec
= 0;
988 Elf_Internal_Shdr
*symtab_hdr
;
989 Elf_Internal_Sym
*local_syms
, *isym
, **all_local_syms
;
990 Elf32_External_Sym
*ext_syms
, *esym
;
991 unsigned int i
, index
, bfd_count
= 0;
992 struct elf32_hppa_stub_hash_table
*stub_hash_table
= 0;
994 /* Create and initialize the stub hash table. */
995 stub_hash_table
= ((struct elf32_hppa_stub_hash_table
*)
996 bfd_malloc (sizeof (struct elf32_hppa_stub_hash_table
)));
997 if (!stub_hash_table
)
1000 if (!elf32_hppa_stub_hash_table_init (stub_hash_table
, stub_bfd
,
1001 elf32_hppa_stub_hash_newfunc
))
1004 /* Attach the hash tables to the main hash table. */
1005 elf32_hppa_hash_table(link_info
)->stub_hash_table
= stub_hash_table
;
1007 /* Count the number of input BFDs. */
1008 for (input_bfd
= link_info
->input_bfds
;
1010 input_bfd
= input_bfd
->link_next
)
1013 /* Magic as we know the stub bfd only has one section. */
1014 stub_sec
= stub_bfd
->sections
;
1016 /* If generating a relocateable output file, then we don't
1017 have to examine the relocs. */
1018 if (link_info
->relocateable
)
1020 for (i
= 0; i
< bfd_count
; i
++)
1021 if (all_local_syms
[i
])
1022 free (all_local_syms
[i
]);
1023 free (all_local_syms
);
1027 /* Now that we have argument location information for all the global
1028 functions we can start looking for stubs. */
1029 for (input_bfd
= link_info
->input_bfds
, index
= 0;
1031 input_bfd
= input_bfd
->link_next
, index
++)
1033 /* We'll need the symbol table in a second. */
1034 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1035 if (symtab_hdr
->sh_info
== 0)
1038 local_syms
= all_local_syms
[index
];
1040 /* Walk over each section attached to the input bfd. */
1041 for (section
= input_bfd
->sections
;
1043 section
= section
->next
)
1045 Elf_Internal_Shdr
*input_rel_hdr
;
1046 Elf32_External_Rela
*external_relocs
, *erelaend
, *erela
;
1047 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
1049 /* If there aren't any relocs, then there's nothing to do. */
1050 if ((section
->flags
& SEC_RELOC
) == 0
1051 || section
->reloc_count
== 0)
1054 /* Allocate space for the external relocations. */
1056 = ((Elf32_External_Rela
*)
1057 bfd_malloc (section
->reloc_count
1058 * sizeof (Elf32_External_Rela
)));
1059 if (external_relocs
== NULL
)
1061 for (i
= 0; i
< bfd_count
; i
++)
1062 if (all_local_syms
[i
])
1063 free (all_local_syms
[i
]);
1064 free (all_local_syms
);
1068 /* Likewise for the internal relocations. */
1070 = ((Elf_Internal_Rela
*)
1071 bfd_malloc (section
->reloc_count
* sizeof (Elf_Internal_Rela
)));
1072 if (internal_relocs
== NULL
)
1074 free (external_relocs
);
1075 for (i
= 0; i
< bfd_count
; i
++)
1076 if (all_local_syms
[i
])
1077 free (all_local_syms
[i
]);
1078 free (all_local_syms
);
1082 /* Read in the external relocs. */
1083 input_rel_hdr
= &elf_section_data (section
)->rel_hdr
;
1084 if (bfd_seek (input_bfd
, input_rel_hdr
->sh_offset
, SEEK_SET
) != 0
1085 || bfd_read (external_relocs
, 1, input_rel_hdr
->sh_size
,
1086 input_bfd
) != input_rel_hdr
->sh_size
)
1088 free (external_relocs
);
1089 free (internal_relocs
);
1090 for (i
= 0; i
< bfd_count
; i
++)
1091 if (all_local_syms
[i
])
1092 free (all_local_syms
[i
]);
1093 free (all_local_syms
);
1097 /* Swap in the relocs. */
1098 erela
= external_relocs
;
1099 erelaend
= erela
+ section
->reloc_count
;
1100 irela
= internal_relocs
;
1101 for (; erela
< erelaend
; erela
++, irela
++)
1102 bfd_elf32_swap_reloca_in (input_bfd
, erela
, irela
);
1104 /* We're done with the external relocs, free them. */
1105 free (external_relocs
);
1107 /* Now examine each relocation. */
1108 irela
= internal_relocs
;
1109 irelaend
= irela
+ section
->reloc_count
;
1110 for (; irela
< irelaend
; irela
++)
1112 long r_type
, size_of_stub
;
1113 unsigned long r_index
;
1114 struct elf_link_hash_entry
*hash
;
1115 struct elf32_hppa_stub_hash_entry
*stub_hash
;
1116 Elf_Internal_Sym
*sym
;
1118 const char *sym_name
;
1120 bfd_vma location
, destination
;
1121 char *new_name
= NULL
;
1123 r_type
= ELF32_R_TYPE (irela
->r_info
);
1124 r_index
= ELF32_R_SYM (irela
->r_info
);
1126 if (r_type
< 0 || r_type
>= (int) R_PARISC_UNIMPLEMENTED
)
1128 bfd_set_error (bfd_error_bad_value
);
1129 free (internal_relocs
);
1130 for (i
= 0; i
< bfd_count
; i
++)
1131 if (all_local_syms
[i
])
1132 free (all_local_syms
[i
]);
1133 free (all_local_syms
);
1137 /* Only look for stubs on call instructions or plabel
1139 if (r_type
!= R_PARISC_PCREL17F
1140 && r_type
!= R_PARISC_PLABEL32
1141 && r_type
!= R_PARISC_PLABEL21L
1142 && r_type
!= R_PARISC_PLABEL14R
)
1145 /* Now determine the call target, its name, value, section
1146 and argument relocation bits. */
1150 if (r_index
< symtab_hdr
->sh_info
)
1152 /* It's a local symbol. */
1153 Elf_Internal_Shdr
*hdr
;
1155 sym
= local_syms
+ r_index
;
1156 hdr
= elf_elfsections (input_bfd
)[sym
->st_shndx
];
1157 sym_sec
= hdr
->bfd_section
;
1158 sym_name
= bfd_elf_string_from_elf_section (input_bfd
,
1159 symtab_hdr
->sh_link
,
1161 sym_value
= (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
1162 ? 0 : sym
->st_value
);
1163 destination
= (sym_value
1164 + sym_sec
->output_offset
1165 + sym_sec
->output_section
->vma
);
1167 /* Tack on an ID so we can uniquely identify this local
1168 symbol in the stub or arg info hash tables. */
1169 new_name
= bfd_malloc (strlen (sym_name
) + 10);
1172 free (internal_relocs
);
1173 for (i
= 0; i
< bfd_count
; i
++)
1174 if (all_local_syms
[i
])
1175 free (all_local_syms
[i
]);
1176 free (all_local_syms
);
1179 sprintf (new_name
, "%s_%08x", sym_name
, (int)sym_sec
);
1180 sym_name
= new_name
;
1184 /* It's an external symbol. */
1187 index
= r_index
- symtab_hdr
->sh_info
;
1188 hash
= elf_sym_hashes (input_bfd
)[index
];
1189 if (hash
->root
.type
== bfd_link_hash_defined
1190 || hash
->root
.type
== bfd_link_hash_defweak
)
1192 sym_sec
= hash
->root
.u
.def
.section
;
1193 sym_name
= hash
->root
.root
.string
;
1194 sym_value
= hash
->root
.u
.def
.value
;
1195 destination
= (sym_value
1196 + sym_sec
->output_offset
1197 + sym_sec
->output_section
->vma
);
1201 bfd_set_error (bfd_error_bad_value
);
1202 free (internal_relocs
);
1203 for (i
= 0; i
< bfd_count
; i
++)
1204 if (all_local_syms
[i
])
1205 free (all_local_syms
[i
]);
1206 free (all_local_syms
);
1211 /* Now determine where the call point is. */
1212 location
= (section
->output_offset
1213 + section
->output_section
->vma
1216 /* We only care about the destination for PCREL function
1217 calls (eg. we don't care for PLABELS). */
1218 if (r_type
!= R_PARISC_PCREL17F
)
1219 location
= destination
;
1221 /* Determine what (if any) linker stub is needed and its
1223 size_of_stub
= elf32_hppa_size_of_stub (location
,
1226 if (size_of_stub
!= 0)
1231 /* Get the name of this stub. */
1232 len
= strlen (sym_name
);
1235 stub_name
= bfd_malloc (len
);
1238 /* Because sym_name was mallocd above for local
1240 if (r_index
< symtab_hdr
->sh_info
)
1243 free (internal_relocs
);
1244 for (i
= 0; i
< bfd_count
; i
++)
1245 if (all_local_syms
[i
])
1246 free (all_local_syms
[i
]);
1247 free (all_local_syms
);
1250 elf32_hppa_name_of_stub (location
, destination
, stub_name
);
1251 strcat (stub_name
+ 22, sym_name
);
1253 /* Because sym_name was malloced above for local symbols. */
1254 if (r_index
< symtab_hdr
->sh_info
)
1258 = elf32_hppa_stub_hash_lookup (stub_hash_table
, stub_name
,
1260 if (stub_hash
!= NULL
)
1262 /* The proper stub has already been created, nothing
1268 bfd_set_section_size (stub_bfd
, stub_sec
,
1269 (bfd_section_size (stub_bfd
,
1273 /* Enter this entry into the linker stub hash table. */
1275 = elf32_hppa_stub_hash_lookup (stub_hash_table
,
1276 stub_name
, true, true);
1277 if (stub_hash
== NULL
)
1280 free (internal_relocs
);
1281 for (i
= 0; i
< bfd_count
; i
++)
1282 if (all_local_syms
[i
])
1283 free (all_local_syms
[i
]);
1284 free (all_local_syms
);
1288 /* We'll need these to determine the address that the
1289 stub will branch to. */
1290 stub_hash
->target_value
= sym_value
;
1291 stub_hash
->target_section
= sym_sec
;
1296 /* We're done with the internal relocs, free them. */
1297 free (internal_relocs
);
1300 /* We're done with the local symbols, free them. */
1301 for (i
= 0; i
< bfd_count
; i
++)
1302 if (all_local_syms
[i
])
1303 free (all_local_syms
[i
]);
1304 free (all_local_syms
);
1308 /* Return gracefully, avoiding dangling references to the hash tables. */
1309 if (stub_hash_table
)
1311 elf32_hppa_hash_table(link_info
)->stub_hash_table
= NULL
;
1312 free (stub_hash_table
);
1314 /* Set the size of the stub section to zero since we're never going
1315 to create them. Avoids losing when we try to get its contents
1317 bfd_set_section_size (stub_bfd
, stub_sec
, 0);
1321 /* Misc BFD support code. */
1322 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
1323 #define bfd_elf32_bfd_is_local_label_name hppa_elf_is_local_label_name
1324 #define elf_info_to_howto elf_hppa_info_to_howto
1325 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
1327 /* Stuff for the BFD linker. */
1328 #define elf_backend_relocate_section elf32_hppa_relocate_section
1329 #define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
1330 #define bfd_elf32_bfd_link_hash_table_create \
1331 elf32_hppa_link_hash_table_create
1333 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
1334 #define TARGET_BIG_NAME "elf32-hppa"
1335 #define ELF_ARCH bfd_arch_hppa
1336 #define ELF_MACHINE_CODE EM_PARISC
1337 #define ELF_MAXPAGESIZE 0x1000
1339 #include "elf32-target.h"