1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
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 elf32_hppa_add_symbol_hook
118 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
119 const char **, flagword *, asection **, bfd_vma *));
121 static bfd_reloc_status_type elf32_hppa_bfd_final_link_relocate
122 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *,
123 bfd_byte *, bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
124 asection *, const char *, int));
126 static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
129 static struct bfd_hash_entry *
130 elf32_hppa_stub_hash_newfunc
131 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
134 elf32_hppa_relocate_section
135 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
136 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
139 elf32_hppa_stub_hash_table_init
140 PARAMS ((struct elf32_hppa_stub_hash_table *, bfd *,
141 struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *,
142 struct bfd_hash_table *,
146 elf32_hppa_build_one_stub PARAMS ((struct bfd_hash_entry *, PTR));
148 static unsigned int elf32_hppa_size_of_stub
149 PARAMS ((bfd_vma, bfd_vma, const char *));
151 static void elf32_hppa_name_of_stub
152 PARAMS ((bfd_vma, bfd_vma, char *));
154 /* For linker stub hash tables. */
155 #define elf32_hppa_stub_hash_lookup(table, string, create, copy) \
156 ((struct elf32_hppa_stub_hash_entry *) \
157 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
159 #define elf32_hppa_stub_hash_traverse(table, func, info) \
162 (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
165 /* For HPPA linker hash table. */
167 #define elf32_hppa_link_hash_lookup(table, string, create, copy, follow)\
168 ((struct elf32_hppa_link_hash_entry *) \
169 elf_link_hash_lookup (&(table)->root, (string), (create), \
172 #define elf32_hppa_link_hash_traverse(table, func, info) \
173 (elf_link_hash_traverse \
175 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
178 /* Get the PA ELF linker hash table from a link_info structure. */
180 #define elf32_hppa_hash_table(p) \
181 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
184 /* Assorted hash table functions. */
186 /* Initialize an entry in the stub hash table. */
188 static struct bfd_hash_entry *
189 elf32_hppa_stub_hash_newfunc (entry, table, string)
190 struct bfd_hash_entry *entry;
191 struct bfd_hash_table *table;
194 struct elf32_hppa_stub_hash_entry *ret;
196 ret = (struct elf32_hppa_stub_hash_entry *) entry;
198 /* Allocate the structure if it has not already been allocated by a
201 ret = ((struct elf32_hppa_stub_hash_entry *)
202 bfd_hash_allocate (table,
203 sizeof (struct elf32_hppa_stub_hash_entry)));
207 /* Call the allocation method of the superclass. */
208 ret = ((struct elf32_hppa_stub_hash_entry *)
209 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
213 /* Initialize the local fields. */
215 ret->target_value = 0;
216 ret->target_section = NULL;
219 return (struct bfd_hash_entry *) ret;
222 /* Initialize a stub hash table. */
225 elf32_hppa_stub_hash_table_init (table, stub_bfd, newfunc)
226 struct elf32_hppa_stub_hash_table *table;
228 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
229 struct bfd_hash_table *,
234 table->stub_bfd = stub_bfd;
235 return (bfd_hash_table_init (&table->root, newfunc));
238 /* Create the derived linker hash table. The PA ELF port uses the derived
239 hash table to keep information specific to the PA ELF linker (without
240 using static variables). */
242 static struct bfd_link_hash_table *
243 elf32_hppa_link_hash_table_create (abfd)
246 struct elf32_hppa_link_hash_table *ret;
248 ret = ((struct elf32_hppa_link_hash_table *)
249 bfd_alloc (abfd, sizeof (struct elf32_hppa_link_hash_table)));
252 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
253 _bfd_elf_link_hash_newfunc))
255 bfd_release (abfd, ret);
258 ret->stub_hash_table = NULL;
259 ret->output_symbol_count = 0;
260 ret->global_value = 0;
261 ret->global_sym_defined = 0;
263 return &ret->root.root;
266 /* Relocate the given INSN given the various input parameters.
268 FIXME: endianness and sizeof (long) issues abound here. */
271 hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
272 r_addend, r_format, r_field, pcrel)
274 asection *input_sect;
276 unsigned long address;
279 unsigned long r_format;
280 unsigned long r_field;
283 unsigned char opcode = get_opcode (insn);
303 constant_value = HPPA_R_CONSTANT (r_addend);
306 sym_value -= address;
308 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
309 return hppa_rebuild_insn (abfd, insn, sym_value, r_format);
314 /* XXX computing constant_value is not needed??? */
315 constant_value = assemble_17 ((insn & 0x001f0000) >> 16,
316 (insn & 0x00001ffc) >> 2,
319 constant_value = (constant_value << 15) >> 15;
323 address + input_sect->output_offset
324 + input_sect->output_section->vma;
325 sym_value = hppa_field_adjust (sym_value, -8, r_field);
328 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
330 return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format);
335 constant_value = HPPA_R_CONSTANT (r_addend);
338 sym_value -= address;
340 return hppa_field_adjust (sym_value, constant_value, r_field);
347 /* Relocate an HPPA ELF section. */
350 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
351 contents, relocs, local_syms, local_sections)
353 struct bfd_link_info *info;
355 asection *input_section;
357 Elf_Internal_Rela *relocs;
358 Elf_Internal_Sym *local_syms;
359 asection **local_sections;
361 Elf_Internal_Shdr *symtab_hdr;
362 Elf_Internal_Rela *rel;
363 Elf_Internal_Rela *relend;
365 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
368 relend = relocs + input_section->reloc_count;
369 for (; rel < relend; rel++)
372 reloc_howto_type *howto;
373 unsigned long r_symndx;
374 struct elf_link_hash_entry *h;
375 Elf_Internal_Sym *sym;
378 bfd_reloc_status_type r;
379 const char *sym_name;
381 r_type = ELF32_R_TYPE (rel->r_info);
382 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
384 bfd_set_error (bfd_error_bad_value);
387 howto = elf_hppa_howto_table + r_type;
389 r_symndx = ELF32_R_SYM (rel->r_info);
391 if (info->relocateable)
393 /* This is a relocateable link. We don't have to change
394 anything, unless the reloc is against a section symbol,
395 in which case we have to adjust according to where the
396 section symbol winds up in the output section. */
397 if (r_symndx < symtab_hdr->sh_info)
399 sym = local_syms + r_symndx;
400 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
402 sym_sec = local_sections[r_symndx];
403 rel->r_addend += sym_sec->output_offset;
410 /* This is a final link. */
414 if (r_symndx < symtab_hdr->sh_info)
416 sym = local_syms + r_symndx;
417 sym_sec = local_sections[r_symndx];
418 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
420 + sym_sec->output_offset
421 + sym_sec->output_section->vma);
427 indx = r_symndx - symtab_hdr->sh_info;
428 h = elf_sym_hashes (input_bfd)[indx];
429 while (h->root.type == bfd_link_hash_indirect
430 || h->root.type == bfd_link_hash_warning)
431 h = (struct elf_link_hash_entry *) h->root.u.i.link;
432 if (h->root.type == bfd_link_hash_defined
433 || h->root.type == bfd_link_hash_defweak)
435 sym_sec = h->root.u.def.section;
436 relocation = (h->root.u.def.value
437 + sym_sec->output_offset
438 + sym_sec->output_section->vma);
440 else if (h->root.type == bfd_link_hash_undefweak)
444 if (!((*info->callbacks->undefined_symbol)
445 (info, h->root.root.string, input_bfd,
446 input_section, rel->r_offset, true)))
453 sym_name = h->root.root.string;
456 sym_name = bfd_elf_string_from_elf_section (input_bfd,
459 if (sym_name == NULL)
461 if (*sym_name == '\0')
462 sym_name = bfd_section_name (input_bfd, sym_sec);
465 r = elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
466 input_section, contents,
467 rel->r_offset, relocation,
468 rel->r_addend, info, sym_sec,
469 sym_name, h == NULL);
471 if (r != bfd_reloc_ok)
475 /* This can happen for DP relative relocs if $global$ is
476 undefined. This is a panic situation so we don't try
478 case bfd_reloc_undefined:
479 case bfd_reloc_notsupported:
480 if (!((*info->callbacks->undefined_symbol)
481 (info, "$global$", input_bfd,
482 input_section, rel->r_offset, true)))
485 case bfd_reloc_dangerous:
487 /* We use this return value to indicate that we performed
488 a "dangerous" relocation. This doesn't mean we did
489 the wrong thing, it just means there may be some cleanup
490 that needs to be done here.
492 In particular we had to swap the last call insn and its
493 delay slot. If the delay slot insn needed a relocation,
494 then we'll need to adjust the next relocation entry's
495 offset to account for the fact that the insn moved.
497 This hair wouldn't be necessary if we inserted stubs
498 between procedures and used a "bl" to get to the stub. */
501 Elf_Internal_Rela *next_rel = rel + 1;
503 if (rel->r_offset + 4 == next_rel->r_offset)
504 next_rel->r_offset -= 4;
509 case bfd_reloc_outofrange:
510 case bfd_reloc_overflow:
512 if (!((*info->callbacks->reloc_overflow)
513 (info, sym_name, howto->name, (bfd_vma) 0,
514 input_bfd, input_section, rel->r_offset)))
525 /* Actually perform a relocation as part of a final link. This can get
526 rather hairy when linker stubs are needed. */
528 static bfd_reloc_status_type
529 elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
530 input_section, contents, offset, value,
531 addend, info, sym_sec, sym_name, is_local)
532 reloc_howto_type *howto;
534 bfd *output_bfd ATTRIBUTE_UNUSED;
535 asection *input_section;
540 struct bfd_link_info *info;
542 const char *sym_name;
546 unsigned long r_type = howto->type;
547 unsigned long r_format = howto->bitsize;
548 unsigned long r_field = e_fsel;
549 bfd_byte *hit_data = contents + offset;
550 boolean r_pcrel = howto->pc_relative;
552 insn = bfd_get_32 (input_bfd, hit_data);
554 /* Make sure we have a value for $global$. FIXME isn't this effectively
555 just like the gp pointer on MIPS? Can we use those routines for this
557 if (!elf32_hppa_hash_table (info)->global_sym_defined)
559 struct elf_link_hash_entry *h;
562 h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false,
565 /* If there isn't a $global$, then we're in deep trouble. */
567 return bfd_reloc_notsupported;
569 /* If $global$ isn't a defined symbol, then we're still in deep
571 if (h->root.type != bfd_link_hash_defined)
572 return bfd_reloc_undefined;
574 sec = h->root.u.def.section;
575 elf32_hppa_hash_table (info)->global_value = (h->root.u.def.value
576 + sec->output_section->vma
577 + sec->output_offset);
578 elf32_hppa_hash_table (info)->global_sym_defined = 1;
587 case R_PARISC_DIR17F:
588 case R_PARISC_PCREL17C:
590 goto do_basic_type_1;
591 case R_PARISC_DIR21L:
592 case R_PARISC_PCREL21L:
594 goto do_basic_type_1;
595 case R_PARISC_DIR17R:
596 case R_PARISC_PCREL17R:
597 case R_PARISC_DIR14R:
598 case R_PARISC_PCREL14R:
600 goto do_basic_type_1;
602 /* For all the DP relative relocations, we need to examine the symbol's
603 section. If it's a code section, then "data pointer relative" makes
604 no sense. In that case we don't adjust the "value", and for 21 bit
605 addil instructions, we change the source addend register from %dp to
607 case R_PARISC_DPREL21L:
609 if (sym_sec->flags & SEC_CODE)
611 if ((insn & 0xfc000000) >> 26 == 0xa
612 && (insn & 0x03e00000) >> 21 == 0x1b)
616 value -= elf32_hppa_hash_table (info)->global_value;
617 goto do_basic_type_1;
618 case R_PARISC_DPREL14R:
620 if ((sym_sec->flags & SEC_CODE) == 0)
621 value -= elf32_hppa_hash_table (info)->global_value;
622 goto do_basic_type_1;
623 case R_PARISC_DPREL14F:
625 if ((sym_sec->flags & SEC_CODE) == 0)
626 value -= elf32_hppa_hash_table (info)->global_value;
627 goto do_basic_type_1;
629 /* These cases are separate as they may involve a lot more work
630 to deal with linker stubs. */
631 case R_PARISC_PLABEL32:
632 case R_PARISC_PLABEL21L:
633 case R_PARISC_PLABEL14R:
634 case R_PARISC_PCREL17F:
638 char *new_name, *stub_name;
640 /* Get the field selector right. We'll need it in a minute. */
641 if (r_type == R_PARISC_PCREL17F
642 || r_type == R_PARISC_PLABEL32)
644 else if (r_type == R_PARISC_PLABEL21L)
646 else if (r_type == R_PARISC_PLABEL14R)
649 /* Find out where we are and where we're going. */
651 input_section->output_offset +
652 input_section->output_section->vma);
654 len = strlen (sym_name) + 1;
657 new_name = bfd_malloc (len);
659 return bfd_reloc_notsupported;
660 strcpy (new_name, sym_name);
662 /* Local symbols have unique IDs. */
664 sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
666 /* Any kind of linker stub needed? */
667 if (((int)(value - location) > 0x3ffff)
668 || ((int)(value - location) < -0x40000))
670 struct elf32_hppa_stub_hash_table *stub_hash_table;
671 struct elf32_hppa_stub_hash_entry *stub_hash;
672 asection *stub_section;
674 /* Build a name for the stub. */
676 len = strlen (new_name);
678 stub_name = bfd_malloc (len);
680 return bfd_reloc_notsupported;
681 elf32_hppa_name_of_stub (location, value, stub_name);
682 strcat (stub_name, new_name);
685 stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table;
688 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
691 /* We're done with that name. */
694 /* The stub BFD only has one section. */
695 stub_section = stub_hash_table->stub_bfd->sections;
697 if (stub_hash != NULL)
699 if (r_type == R_PARISC_PCREL17F)
701 unsigned long delay_insn;
702 unsigned int opcode, rtn_reg, ldo_target_reg, ldo_src_reg;
704 /* We'll need to peek at the next insn. */
705 delay_insn = bfd_get_32 (input_bfd, hit_data + 4);
706 opcode = get_opcode (delay_insn);
708 /* We also need to know the return register for this
710 rtn_reg = (insn & 0x03e00000) >> 21;
712 ldo_src_reg = (delay_insn & 0x03e00000) >> 21;
713 ldo_target_reg = (delay_insn & 0x001f0000) >> 16;
715 /* Munge up the value and other parameters for
716 hppa_elf_relocate_insn. */
718 value = (stub_hash->offset
719 + stub_section->output_offset
720 + stub_section->output_section->vma);
727 /* We need to peek at the delay insn and determine if
728 we'll need to swap the branch and its delay insn. */
731 && ldo_target_reg == rtn_reg)
732 || (delay_insn == 0x08000240))
734 /* No need to swap the branch and its delay slot, but
735 we do need to make sure to jump past the return
736 pointer update in the stub. */
739 /* If the delay insn does a return pointer adjustment,
740 then we have to make sure it stays valid. */
742 && ldo_target_reg == rtn_reg)
744 delay_insn &= 0xfc00ffff;
745 delay_insn |= ((31 << 21) | (31 << 16));
746 bfd_put_32 (input_bfd, delay_insn, hit_data + 4);
748 /* Use a BLE to reach the stub. */
753 /* Wonderful, we have to swap the call insn and its
755 bfd_put_32 (input_bfd, delay_insn, hit_data);
756 /* Use a BLE,n to reach the stub. */
757 insn = (BLE_SR4_R0 | 0x2);
758 bfd_put_32 (input_bfd, insn, hit_data + 4);
759 insn = hppa_elf_relocate_insn (input_bfd,
765 /* Update the instruction word. */
766 bfd_put_32 (input_bfd, insn, hit_data + 4);
767 return bfd_reloc_dangerous;
771 return bfd_reloc_notsupported;
774 goto do_basic_type_1;
778 insn = hppa_elf_relocate_insn (input_bfd, input_section, insn,
779 offset, value, addend, r_format,
783 /* Something we don't know how to handle. */
785 return bfd_reloc_notsupported;
788 /* Update the instruction word. */
789 bfd_put_32 (input_bfd, insn, hit_data);
790 return (bfd_reloc_ok);
793 /* Undo the generic ELF code's subtraction of section->vma from the
794 value of each external symbol. */
797 elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
798 bfd *abfd ATTRIBUTE_UNUSED;
799 struct bfd_link_info *info ATTRIBUTE_UNUSED;
800 const Elf_Internal_Sym *sym ATTRIBUTE_UNUSED;
801 const char **namep ATTRIBUTE_UNUSED;
802 flagword *flagsp ATTRIBUTE_UNUSED;
806 *valp += (*secp)->vma;
810 /* Determine the name of the stub needed to perform a call assuming the
811 argument relocation bits for caller and callee are in CALLER and CALLEE
812 for a call from LOCATION to DESTINATION. Copy the name into STUB_NAME. */
815 elf32_hppa_name_of_stub (location, destination, stub_name)
816 bfd_vma location ATTRIBUTE_UNUSED;
817 bfd_vma destination ATTRIBUTE_UNUSED;
820 strcpy (stub_name, "_____long_branch_stub_");
823 /* Compute the size of the stub needed to call from LOCATION to DESTINATION
824 (a function named SYM_NAME), with argument relocation bits CALLER and
825 CALLEE. Return zero if no stub is needed to perform such a call. */
828 elf32_hppa_size_of_stub (location, destination, sym_name)
829 bfd_vma location, destination;
830 const char *sym_name;
832 /* Determine if a long branch stub is needed. */
833 if (!(((int)(location - destination) > 0x3ffff)
834 || ((int)(location - destination) < -0x40000)))
837 if (!strncmp ("$$", sym_name, 2)
838 && strcmp ("$$dyncall", sym_name))
844 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
845 IN_ARGS contains the stub BFD and link info pointers. */
848 elf32_hppa_build_one_stub (gen_entry, in_args)
849 struct bfd_hash_entry *gen_entry;
852 void **args = (void **)in_args;
853 bfd *stub_bfd = (bfd *)args[0];
854 struct bfd_link_info *info = (struct bfd_link_info *)args[1];
855 struct elf32_hppa_stub_hash_entry *entry;
856 struct elf32_hppa_stub_hash_table *stub_hash_table;
859 const char *sym_name;
861 /* Initialize pointers to the stub hash table, the particular entry we
862 are building a stub for, and where (in memory) we should place the stub
864 entry = (struct elf32_hppa_stub_hash_entry *)gen_entry;
865 stub_hash_table = elf32_hppa_hash_table(info)->stub_hash_table;
866 loc = stub_hash_table->location;
868 /* Make a note of the offset within the stubs for this entry. */
869 entry->offset = stub_hash_table->offset;
871 /* The symbol's name starts at offset 22. */
872 sym_name = entry->root.string + 22;
874 sym_value = (entry->target_value
875 + entry->target_section->output_offset
876 + entry->target_section->output_section->vma);
880 /* Create one of two variant long branch stubs. One for $$dyncall and
881 normal calls, the other for calls to millicode. */
883 int millicode_call = 0;
885 if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name))
888 /* First the return pointer adjustment. Depending on exact calling
889 sequence this instruction may be skipped. */
890 bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
892 /* The next two instructions are the long branch itself. A long branch
893 is formed with "ldil" loading the upper bits of the target address
894 into a register, then branching with "be" which adds in the lower bits.
895 Long branches to millicode nullify the delay slot of the "be". */
896 insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
897 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
898 bfd_put_32 (stub_bfd, insn, loc + 4);
899 insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1 | (millicode_call ? 2 : 0),
900 hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
902 bfd_put_32 (stub_bfd, insn, loc + 8);
906 /* The sequence to call this stub places the return pointer into %r31,
907 the final target expects the return pointer in %r2, so copy the
908 return pointer into the proper register. */
909 bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 12);
911 /* Update the location and offsets. */
912 stub_hash_table->location += 16;
913 stub_hash_table->offset += 16;
917 /* Update the location and offsets. */
918 stub_hash_table->location += 12;
919 stub_hash_table->offset += 12;
926 /* External entry points for sizing and building linker stubs. */
928 /* Build all the stubs associated with the current output file. The
929 stubs are kept in a hash table attached to the main linker hash
930 table. This is called via hppaelf_finish in the linker. */
933 elf32_hppa_build_stubs (stub_bfd, info)
935 struct bfd_link_info *info;
937 /* The stub BFD only has one section. */
938 asection *stub_sec = stub_bfd->sections;
939 struct elf32_hppa_stub_hash_table *table;
943 /* So we can pass both the BFD for the stubs and the link info
944 structure to the routine which actually builds stubs. */
948 /* Allocate memory to hold the linker stubs. */
949 size = bfd_section_size (stub_bfd, stub_sec);
950 stub_sec->contents = (unsigned char *) bfd_zalloc (stub_bfd, size);
951 if (stub_sec->contents == NULL)
953 table = elf32_hppa_hash_table(info)->stub_hash_table;
954 table->location = stub_sec->contents;
956 /* Build the stubs as directed by the stub hash table. */
957 elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args);
962 /* Determine and set the size of the stub section for a final link.
964 The basic idea here is to examine all the relocations looking for
965 PC-relative calls to a target that is unreachable with a "bl"
966 instruction or calls where the caller and callee disagree on the
967 location of their arguments or return value. */
970 elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info)
972 bfd *output_bfd ATTRIBUTE_UNUSED;
973 struct bfd_link_info *link_info;
976 asection *section, *stub_sec = 0;
977 Elf_Internal_Shdr *symtab_hdr;
978 Elf_Internal_Sym *local_syms, **all_local_syms;
979 unsigned int i, index, bfd_count = 0;
980 struct elf32_hppa_stub_hash_table *stub_hash_table = 0;
982 /* Create and initialize the stub hash table. */
983 stub_hash_table = ((struct elf32_hppa_stub_hash_table *)
984 bfd_malloc (sizeof (struct elf32_hppa_stub_hash_table)));
985 if (!stub_hash_table)
988 if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd,
989 elf32_hppa_stub_hash_newfunc))
992 /* Attach the hash tables to the main hash table. */
993 elf32_hppa_hash_table(link_info)->stub_hash_table = stub_hash_table;
995 /* Count the number of input BFDs. */
996 for (input_bfd = link_info->input_bfds;
998 input_bfd = input_bfd->link_next)
1001 /* Magic as we know the stub bfd only has one section. */
1002 stub_sec = stub_bfd->sections;
1004 /* If generating a relocateable output file, then we don't
1005 have to examine the relocs. */
1006 if (link_info->relocateable)
1008 for (i = 0; i < bfd_count; i++)
1009 if (all_local_syms[i])
1010 free (all_local_syms[i]);
1011 free (all_local_syms);
1015 /* Now that we have argument location information for all the global
1016 functions we can start looking for stubs. */
1017 for (input_bfd = link_info->input_bfds, index = 0;
1019 input_bfd = input_bfd->link_next, index++)
1021 /* We'll need the symbol table in a second. */
1022 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1023 if (symtab_hdr->sh_info == 0)
1026 local_syms = all_local_syms[index];
1028 /* Walk over each section attached to the input bfd. */
1029 for (section = input_bfd->sections;
1031 section = section->next)
1033 Elf_Internal_Shdr *input_rel_hdr;
1034 Elf32_External_Rela *external_relocs, *erelaend, *erela;
1035 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1037 /* If there aren't any relocs, then there's nothing to do. */
1038 if ((section->flags & SEC_RELOC) == 0
1039 || section->reloc_count == 0)
1042 /* Allocate space for the external relocations. */
1044 = ((Elf32_External_Rela *)
1045 bfd_malloc (section->reloc_count
1046 * sizeof (Elf32_External_Rela)));
1047 if (external_relocs == NULL)
1049 for (i = 0; i < bfd_count; i++)
1050 if (all_local_syms[i])
1051 free (all_local_syms[i]);
1052 free (all_local_syms);
1056 /* Likewise for the internal relocations. */
1058 = ((Elf_Internal_Rela *)
1059 bfd_malloc (section->reloc_count * sizeof (Elf_Internal_Rela)));
1060 if (internal_relocs == NULL)
1062 free (external_relocs);
1063 for (i = 0; i < bfd_count; i++)
1064 if (all_local_syms[i])
1065 free (all_local_syms[i]);
1066 free (all_local_syms);
1070 /* Read in the external relocs. */
1071 input_rel_hdr = &elf_section_data (section)->rel_hdr;
1072 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
1073 || bfd_read (external_relocs, 1, input_rel_hdr->sh_size,
1074 input_bfd) != input_rel_hdr->sh_size)
1076 free (external_relocs);
1077 free (internal_relocs);
1078 for (i = 0; i < bfd_count; i++)
1079 if (all_local_syms[i])
1080 free (all_local_syms[i]);
1081 free (all_local_syms);
1085 /* Swap in the relocs. */
1086 erela = external_relocs;
1087 erelaend = erela + section->reloc_count;
1088 irela = internal_relocs;
1089 for (; erela < erelaend; erela++, irela++)
1090 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
1092 /* We're done with the external relocs, free them. */
1093 free (external_relocs);
1095 /* Now examine each relocation. */
1096 irela = internal_relocs;
1097 irelaend = irela + section->reloc_count;
1098 for (; irela < irelaend; irela++)
1100 long r_type, size_of_stub;
1101 unsigned long r_index;
1102 struct elf_link_hash_entry *hash;
1103 struct elf32_hppa_stub_hash_entry *stub_hash;
1104 Elf_Internal_Sym *sym;
1106 const char *sym_name;
1108 bfd_vma location, destination;
1109 char *new_name = NULL;
1111 r_type = ELF32_R_TYPE (irela->r_info);
1112 r_index = ELF32_R_SYM (irela->r_info);
1114 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
1116 bfd_set_error (bfd_error_bad_value);
1117 free (internal_relocs);
1118 for (i = 0; i < bfd_count; i++)
1119 if (all_local_syms[i])
1120 free (all_local_syms[i]);
1121 free (all_local_syms);
1125 /* Only look for stubs on call instructions or plabel
1127 if (r_type != R_PARISC_PCREL17F
1128 && r_type != R_PARISC_PLABEL32
1129 && r_type != R_PARISC_PLABEL21L
1130 && r_type != R_PARISC_PLABEL14R)
1133 /* Now determine the call target, its name, value, section
1134 and argument relocation bits. */
1138 if (r_index < symtab_hdr->sh_info)
1140 /* It's a local symbol. */
1141 Elf_Internal_Shdr *hdr;
1143 sym = local_syms + r_index;
1144 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
1145 sym_sec = hdr->bfd_section;
1146 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1147 symtab_hdr->sh_link,
1149 sym_value = (ELF_ST_TYPE (sym->st_info) == STT_SECTION
1150 ? 0 : sym->st_value);
1151 destination = (sym_value
1152 + sym_sec->output_offset
1153 + sym_sec->output_section->vma);
1155 /* Tack on an ID so we can uniquely identify this local
1156 symbol in the stub or arg info hash tables. */
1157 new_name = bfd_malloc (strlen (sym_name) + 10);
1160 free (internal_relocs);
1161 for (i = 0; i < bfd_count; i++)
1162 if (all_local_syms[i])
1163 free (all_local_syms[i]);
1164 free (all_local_syms);
1167 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1168 sym_name = new_name;
1172 /* It's an external symbol. */
1175 index = r_index - symtab_hdr->sh_info;
1176 hash = elf_sym_hashes (input_bfd)[index];
1177 if (hash->root.type == bfd_link_hash_defined
1178 || hash->root.type == bfd_link_hash_defweak)
1180 sym_sec = hash->root.u.def.section;
1181 sym_name = hash->root.root.string;
1182 sym_value = hash->root.u.def.value;
1183 destination = (sym_value
1184 + sym_sec->output_offset
1185 + sym_sec->output_section->vma);
1189 bfd_set_error (bfd_error_bad_value);
1190 free (internal_relocs);
1191 for (i = 0; i < bfd_count; i++)
1192 if (all_local_syms[i])
1193 free (all_local_syms[i]);
1194 free (all_local_syms);
1199 /* Now determine where the call point is. */
1200 location = (section->output_offset
1201 + section->output_section->vma
1204 /* We only care about the destination for PCREL function
1205 calls (eg. we don't care for PLABELS). */
1206 if (r_type != R_PARISC_PCREL17F)
1207 location = destination;
1209 /* Determine what (if any) linker stub is needed and its
1211 size_of_stub = elf32_hppa_size_of_stub (location,
1214 if (size_of_stub != 0)
1219 /* Get the name of this stub. */
1220 len = strlen (sym_name);
1223 stub_name = bfd_malloc (len);
1226 /* Because sym_name was mallocd above for local
1228 if (r_index < symtab_hdr->sh_info)
1231 free (internal_relocs);
1232 for (i = 0; i < bfd_count; i++)
1233 if (all_local_syms[i])
1234 free (all_local_syms[i]);
1235 free (all_local_syms);
1238 elf32_hppa_name_of_stub (location, destination, stub_name);
1239 strcat (stub_name + 22, sym_name);
1241 /* Because sym_name was malloced above for local symbols. */
1242 if (r_index < symtab_hdr->sh_info)
1246 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
1248 if (stub_hash != NULL)
1250 /* The proper stub has already been created, nothing
1256 bfd_set_section_size (stub_bfd, stub_sec,
1257 (bfd_section_size (stub_bfd,
1261 /* Enter this entry into the linker stub hash table. */
1263 = elf32_hppa_stub_hash_lookup (stub_hash_table,
1264 stub_name, true, true);
1265 if (stub_hash == NULL)
1268 free (internal_relocs);
1269 for (i = 0; i < bfd_count; i++)
1270 if (all_local_syms[i])
1271 free (all_local_syms[i]);
1272 free (all_local_syms);
1276 /* We'll need these to determine the address that the
1277 stub will branch to. */
1278 stub_hash->target_value = sym_value;
1279 stub_hash->target_section = sym_sec;
1284 /* We're done with the internal relocs, free them. */
1285 free (internal_relocs);
1288 /* We're done with the local symbols, free them. */
1289 for (i = 0; i < bfd_count; i++)
1290 if (all_local_syms[i])
1291 free (all_local_syms[i]);
1292 free (all_local_syms);
1296 /* Return gracefully, avoiding dangling references to the hash tables. */
1297 if (stub_hash_table)
1299 elf32_hppa_hash_table(link_info)->stub_hash_table = NULL;
1300 free (stub_hash_table);
1302 /* Set the size of the stub section to zero since we're never going
1303 to create them. Avoids losing when we try to get its contents
1305 bfd_set_section_size (stub_bfd, stub_sec, 0);
1309 /* Misc BFD support code. */
1310 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
1311 #define bfd_elf32_bfd_is_local_label_name elf_hppa_is_local_label_name
1312 #define elf_info_to_howto elf_hppa_info_to_howto
1313 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
1315 /* Stuff for the BFD linker. */
1316 #define elf_backend_relocate_section elf32_hppa_relocate_section
1317 #define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
1318 #define bfd_elf32_bfd_link_hash_table_create \
1319 elf32_hppa_link_hash_table_create
1320 #define elf_backend_fake_sections elf_hppa_fake_sections
1323 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
1324 #define TARGET_BIG_NAME "elf32-hppa"
1325 #define ELF_ARCH bfd_arch_hppa
1326 #define ELF_MACHINE_CODE EM_PARISC
1327 #define ELF_MAXPAGESIZE 0x1000
1329 #include "elf32-target.h"