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 bfd_reloc_status_type hppa_elf_reloc
114 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
116 static unsigned long hppa_elf_relocate_insn
117 PARAMS ((bfd *, asection *, unsigned long, unsigned long, long,
118 long, unsigned long, unsigned long, unsigned long));
120 static bfd_reloc_status_type hppa_elf_reloc
121 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd*, char **));
123 static boolean hppa_elf_is_local_label_name PARAMS ((bfd *, const char *));
125 static boolean elf32_hppa_add_symbol_hook
126 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
127 const char **, flagword *, asection **, bfd_vma *));
129 static bfd_reloc_status_type elf32_hppa_bfd_final_link_relocate
130 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *,
131 bfd_byte *, bfd_vma, bfd_vma, bfd_vma, struct bfd_link_info *,
132 asection *, const char *, int));
134 static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create
137 static struct bfd_hash_entry *
138 elf32_hppa_stub_hash_newfunc
139 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
142 elf32_hppa_relocate_section
143 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
144 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
147 elf32_hppa_stub_hash_table_init
148 PARAMS ((struct elf32_hppa_stub_hash_table *, bfd *,
149 struct bfd_hash_entry *(*) PARAMS ((struct bfd_hash_entry *,
150 struct bfd_hash_table *,
154 elf32_hppa_build_one_stub PARAMS ((struct bfd_hash_entry *, PTR));
156 static unsigned int elf32_hppa_size_of_stub
157 PARAMS ((bfd_vma, bfd_vma, const char *));
159 static void elf32_hppa_name_of_stub
160 PARAMS ((bfd_vma, bfd_vma, char *));
162 /* For linker stub hash tables. */
163 #define elf32_hppa_stub_hash_lookup(table, string, create, copy) \
164 ((struct elf32_hppa_stub_hash_entry *) \
165 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
167 #define elf32_hppa_stub_hash_traverse(table, func, info) \
170 (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
173 /* For HPPA linker hash table. */
175 #define elf32_hppa_link_hash_lookup(table, string, create, copy, follow)\
176 ((struct elf32_hppa_link_hash_entry *) \
177 elf_link_hash_lookup (&(table)->root, (string), (create), \
180 #define elf32_hppa_link_hash_traverse(table, func, info) \
181 (elf_link_hash_traverse \
183 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
186 /* Get the PA ELF linker hash table from a link_info structure. */
188 #define elf32_hppa_hash_table(p) \
189 ((struct elf32_hppa_link_hash_table *) ((p)->hash))
192 /* Assorted hash table functions. */
194 /* Initialize an entry in the stub hash table. */
196 static struct bfd_hash_entry *
197 elf32_hppa_stub_hash_newfunc (entry, table, string)
198 struct bfd_hash_entry *entry;
199 struct bfd_hash_table *table;
202 struct elf32_hppa_stub_hash_entry *ret;
204 ret = (struct elf32_hppa_stub_hash_entry *) entry;
206 /* Allocate the structure if it has not already been allocated by a
209 ret = ((struct elf32_hppa_stub_hash_entry *)
210 bfd_hash_allocate (table,
211 sizeof (struct elf32_hppa_stub_hash_entry)));
215 /* Call the allocation method of the superclass. */
216 ret = ((struct elf32_hppa_stub_hash_entry *)
217 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
221 /* Initialize the local fields. */
223 ret->target_value = 0;
224 ret->target_section = NULL;
227 return (struct bfd_hash_entry *) ret;
230 /* Initialize a stub hash table. */
233 elf32_hppa_stub_hash_table_init (table, stub_bfd, newfunc)
234 struct elf32_hppa_stub_hash_table *table;
236 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
237 struct bfd_hash_table *,
242 table->stub_bfd = stub_bfd;
243 return (bfd_hash_table_init (&table->root, newfunc));
246 /* Create the derived linker hash table. The PA ELF port uses the derived
247 hash table to keep information specific to the PA ELF linker (without
248 using static variables). */
250 static struct bfd_link_hash_table *
251 elf32_hppa_link_hash_table_create (abfd)
254 struct elf32_hppa_link_hash_table *ret;
256 ret = ((struct elf32_hppa_link_hash_table *)
257 bfd_alloc (abfd, sizeof (struct elf32_hppa_link_hash_table)));
260 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
261 _bfd_elf_link_hash_newfunc))
263 bfd_release (abfd, ret);
266 ret->stub_hash_table = NULL;
267 ret->output_symbol_count = 0;
268 ret->global_value = 0;
269 ret->global_sym_defined = 0;
271 return &ret->root.root;
274 /* Relocate the given INSN given the various input parameters.
276 FIXME: endianness and sizeof (long) issues abound here. */
279 hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
280 r_addend, r_format, r_field, pcrel)
282 asection *input_sect;
284 unsigned long address;
287 unsigned long r_format;
288 unsigned long r_field;
291 unsigned char opcode = get_opcode (insn);
311 constant_value = HPPA_R_CONSTANT (r_addend);
314 sym_value -= address;
316 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
317 return hppa_rebuild_insn (abfd, insn, sym_value, r_format);
322 /* XXX computing constant_value is not needed??? */
323 constant_value = assemble_17 ((insn & 0x001f0000) >> 16,
324 (insn & 0x00001ffc) >> 2,
327 constant_value = (constant_value << 15) >> 15;
331 address + input_sect->output_offset
332 + input_sect->output_section->vma;
333 sym_value = hppa_field_adjust (sym_value, -8, r_field);
336 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
338 return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format);
343 constant_value = HPPA_R_CONSTANT (r_addend);
346 sym_value -= address;
348 return hppa_field_adjust (sym_value, constant_value, r_field);
355 /* Relocate an HPPA ELF section. */
358 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
359 contents, relocs, local_syms, local_sections)
361 struct bfd_link_info *info;
363 asection *input_section;
365 Elf_Internal_Rela *relocs;
366 Elf_Internal_Sym *local_syms;
367 asection **local_sections;
369 Elf_Internal_Shdr *symtab_hdr;
370 Elf_Internal_Rela *rel;
371 Elf_Internal_Rela *relend;
373 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
376 relend = relocs + input_section->reloc_count;
377 for (; rel < relend; rel++)
380 reloc_howto_type *howto;
381 unsigned long r_symndx;
382 struct elf_link_hash_entry *h;
383 Elf_Internal_Sym *sym;
386 bfd_reloc_status_type r;
387 const char *sym_name;
389 r_type = ELF32_R_TYPE (rel->r_info);
390 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
392 bfd_set_error (bfd_error_bad_value);
395 howto = elf_hppa_howto_table + r_type;
397 r_symndx = ELF32_R_SYM (rel->r_info);
399 if (info->relocateable)
401 /* This is a relocateable link. We don't have to change
402 anything, unless the reloc is against a section symbol,
403 in which case we have to adjust according to where the
404 section symbol winds up in the output section. */
405 if (r_symndx < symtab_hdr->sh_info)
407 sym = local_syms + r_symndx;
408 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
410 sym_sec = local_sections[r_symndx];
411 rel->r_addend += sym_sec->output_offset;
418 /* This is a final link. */
422 if (r_symndx < symtab_hdr->sh_info)
424 sym = local_syms + r_symndx;
425 sym_sec = local_sections[r_symndx];
426 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION
428 + sym_sec->output_offset
429 + sym_sec->output_section->vma);
435 indx = r_symndx - symtab_hdr->sh_info;
436 h = elf_sym_hashes (input_bfd)[indx];
437 while (h->root.type == bfd_link_hash_indirect
438 || h->root.type == bfd_link_hash_warning)
439 h = (struct elf_link_hash_entry *) h->root.u.i.link;
440 if (h->root.type == bfd_link_hash_defined
441 || h->root.type == bfd_link_hash_defweak)
443 sym_sec = h->root.u.def.section;
444 relocation = (h->root.u.def.value
445 + sym_sec->output_offset
446 + sym_sec->output_section->vma);
448 else if (h->root.type == bfd_link_hash_undefweak)
452 if (!((*info->callbacks->undefined_symbol)
453 (info, h->root.root.string, input_bfd,
454 input_section, rel->r_offset)))
461 sym_name = h->root.root.string;
464 sym_name = bfd_elf_string_from_elf_section (input_bfd,
467 if (sym_name == NULL)
469 if (*sym_name == '\0')
470 sym_name = bfd_section_name (input_bfd, sym_sec);
473 r = elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
474 input_section, contents,
475 rel->r_offset, relocation,
476 rel->r_addend, info, sym_sec,
477 sym_name, h == NULL);
479 if (r != bfd_reloc_ok)
483 /* This can happen for DP relative relocs if $global$ is
484 undefined. This is a panic situation so we don't try
486 case bfd_reloc_undefined:
487 case bfd_reloc_notsupported:
488 if (!((*info->callbacks->undefined_symbol)
489 (info, "$global$", input_bfd,
490 input_section, rel->r_offset)))
493 case bfd_reloc_dangerous:
495 /* We use this return value to indicate that we performed
496 a "dangerous" relocation. This doesn't mean we did
497 the wrong thing, it just means there may be some cleanup
498 that needs to be done here.
500 In particular we had to swap the last call insn and its
501 delay slot. If the delay slot insn needed a relocation,
502 then we'll need to adjust the next relocation entry's
503 offset to account for the fact that the insn moved.
505 This hair wouldn't be necessary if we inserted stubs
506 between procedures and used a "bl" to get to the stub. */
509 Elf_Internal_Rela *next_rel = rel + 1;
511 if (rel->r_offset + 4 == next_rel->r_offset)
512 next_rel->r_offset -= 4;
517 case bfd_reloc_outofrange:
518 case bfd_reloc_overflow:
520 if (!((*info->callbacks->reloc_overflow)
521 (info, sym_name, howto->name, (bfd_vma) 0,
522 input_bfd, input_section, rel->r_offset)))
533 /* Actually perform a relocation. NOTE this is (mostly) superceeded
534 by elf32_hppa_bfd_final_link_relocate which is called by the new
537 static bfd_reloc_status_type
538 hppa_elf_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
541 arelent *reloc_entry;
544 asection *input_section;
546 char **error_message;
548 /* It is no longer valid to call hppa_elf_reloc when creating
549 a final executable. */
552 reloc_entry->address += input_section->output_offset;
554 /* Work around lossage in generic elf code to write relocations.
555 (maps different section symbols into the same symbol index). */
556 if ((symbol_in->flags & BSF_SECTION_SYM)
557 && symbol_in->section)
558 reloc_entry->addend += symbol_in->section->output_offset;
563 *error_message = (char *) _("Unsupported call to hppa_elf_reloc");
564 return bfd_reloc_notsupported;
568 /* Actually perform a relocation as part of a final link. This can get
569 rather hairy when linker stubs are needed. */
571 static bfd_reloc_status_type
572 elf32_hppa_bfd_final_link_relocate (howto, input_bfd, output_bfd,
573 input_section, contents, offset, value,
574 addend, info, sym_sec, sym_name, is_local)
575 reloc_howto_type *howto;
578 asection *input_section;
583 struct bfd_link_info *info;
585 const char *sym_name;
589 unsigned long r_type = howto->type;
590 unsigned long r_format = howto->bitsize;
591 unsigned long r_field = e_fsel;
592 bfd_byte *hit_data = contents + offset;
593 boolean r_pcrel = howto->pc_relative;
595 insn = bfd_get_32 (input_bfd, hit_data);
597 /* Make sure we have a value for $global$. FIXME isn't this effectively
598 just like the gp pointer on MIPS? Can we use those routines for this
600 if (!elf32_hppa_hash_table (info)->global_sym_defined)
602 struct elf_link_hash_entry *h;
605 h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false,
608 /* If there isn't a $global$, then we're in deep trouble. */
610 return bfd_reloc_notsupported;
612 /* If $global$ isn't a defined symbol, then we're still in deep
614 if (h->root.type != bfd_link_hash_defined)
615 return bfd_reloc_undefined;
617 sec = h->root.u.def.section;
618 elf32_hppa_hash_table (info)->global_value = (h->root.u.def.value
619 + sec->output_section->vma
620 + sec->output_offset);
621 elf32_hppa_hash_table (info)->global_sym_defined = 1;
630 case R_PARISC_DIR17F:
631 case R_PARISC_PCREL17C:
633 goto do_basic_type_1;
634 case R_PARISC_DIR21L:
635 case R_PARISC_PCREL21L:
637 goto do_basic_type_1;
638 case R_PARISC_DIR17R:
639 case R_PARISC_PCREL17R:
640 case R_PARISC_DIR14R:
641 case R_PARISC_PCREL14R:
643 goto do_basic_type_1;
645 /* For all the DP relative relocations, we need to examine the symbol's
646 section. If it's a code section, then "data pointer relative" makes
647 no sense. In that case we don't adjust the "value", and for 21 bit
648 addil instructions, we change the source addend register from %dp to
650 case R_PARISC_DPREL21L:
652 if (sym_sec->flags & SEC_CODE)
654 if ((insn & 0xfc000000) >> 26 == 0xa
655 && (insn & 0x03e00000) >> 21 == 0x1b)
659 value -= elf32_hppa_hash_table (info)->global_value;
660 goto do_basic_type_1;
661 case R_PARISC_DPREL14R:
663 if ((sym_sec->flags & SEC_CODE) == 0)
664 value -= elf32_hppa_hash_table (info)->global_value;
665 goto do_basic_type_1;
666 case R_PARISC_DPREL14F:
668 if ((sym_sec->flags & SEC_CODE) == 0)
669 value -= elf32_hppa_hash_table (info)->global_value;
670 goto do_basic_type_1;
672 /* These cases are separate as they may involve a lot more work
673 to deal with linker stubs. */
674 case R_PARISC_PLABEL32:
675 case R_PARISC_PLABEL21L:
676 case R_PARISC_PLABEL14R:
677 case R_PARISC_PCREL17F:
680 unsigned int len, caller_args, callee_args;
681 char *new_name, *stub_name;
683 /* Get the field selector right. We'll need it in a minute. */
684 if (r_type == R_PARISC_PCREL17F
685 || r_type == R_PARISC_PLABEL32)
687 else if (r_type == R_PARISC_PLABEL21L)
689 else if (r_type == R_PARISC_PLABEL14R)
692 /* Find out where we are and where we're going. */
694 input_section->output_offset +
695 input_section->output_section->vma);
697 len = strlen (sym_name) + 1;
700 new_name = bfd_malloc (len);
702 return bfd_reloc_notsupported;
703 strcpy (new_name, sym_name);
705 /* Local symbols have unique IDs. */
707 sprintf (new_name + len - 10, "_%08x", (int)sym_sec);
709 /* Any kind of linker stub needed? */
710 if (((int)(value - location) > 0x3ffff)
711 || ((int)(value - location) < (int)0xfffc0000))
713 struct elf32_hppa_stub_hash_table *stub_hash_table;
714 struct elf32_hppa_stub_hash_entry *stub_hash;
715 asection *stub_section;
717 /* Build a name for the stub. */
719 len = strlen (new_name);
721 stub_name = bfd_malloc (len);
723 return bfd_reloc_notsupported;
724 elf32_hppa_name_of_stub (location, value, stub_name);
725 strcat (stub_name, new_name);
728 stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table;
731 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
734 /* We're done with that name. */
737 /* The stub BFD only has one section. */
738 stub_section = stub_hash_table->stub_bfd->sections;
740 if (stub_hash != NULL)
742 if (r_type == R_PARISC_PCREL17F)
744 unsigned long delay_insn;
745 unsigned int opcode, rtn_reg, ldo_target_reg, ldo_src_reg;
747 /* We'll need to peek at the next insn. */
748 delay_insn = bfd_get_32 (input_bfd, hit_data + 4);
749 opcode = get_opcode (delay_insn);
751 /* We also need to know the return register for this
753 rtn_reg = (insn & 0x03e00000) >> 21;
755 ldo_src_reg = (delay_insn & 0x03e00000) >> 21;
756 ldo_target_reg = (delay_insn & 0x001f0000) >> 16;
758 /* Munge up the value and other parameters for
759 hppa_elf_relocate_insn. */
761 value = (stub_hash->offset
762 + stub_section->output_offset
763 + stub_section->output_section->vma);
770 /* We need to peek at the delay insn and determine if
771 we'll need to swap the branch and its delay insn. */
774 && ldo_target_reg == rtn_reg)
775 || (delay_insn == 0x08000240))
777 /* No need to swap the branch and its delay slot, but
778 we do need to make sure to jump past the return
779 pointer update in the stub. */
782 /* If the delay insn does a return pointer adjustment,
783 then we have to make sure it stays valid. */
785 && ldo_target_reg == rtn_reg)
787 delay_insn &= 0xfc00ffff;
788 delay_insn |= ((31 << 21) | (31 << 16));
789 bfd_put_32 (input_bfd, delay_insn, hit_data + 4);
791 /* Use a BLE to reach the stub. */
796 /* Wonderful, we have to swap the call insn and its
798 bfd_put_32 (input_bfd, delay_insn, hit_data);
799 /* Use a BLE,n to reach the stub. */
800 insn = (BLE_SR4_R0 | 0x2);
801 bfd_put_32 (input_bfd, insn, hit_data + 4);
802 insn = hppa_elf_relocate_insn (input_bfd,
808 /* Update the instruction word. */
809 bfd_put_32 (input_bfd, insn, hit_data + 4);
810 return bfd_reloc_dangerous;
814 return bfd_reloc_notsupported;
817 goto do_basic_type_1;
821 insn = hppa_elf_relocate_insn (input_bfd, input_section, insn,
822 offset, value, addend, r_format,
826 /* Something we don't know how to handle. */
828 return bfd_reloc_notsupported;
831 /* Update the instruction word. */
832 bfd_put_32 (input_bfd, insn, hit_data);
833 return (bfd_reloc_ok);
836 /* Return true if SYM represents a local label symbol. */
839 hppa_elf_is_local_label_name (abfd, name)
843 return (name[0] == 'L' && name[1] == '$');
846 /* Undo the generic ELF code's subtraction of section->vma from the
847 value of each external symbol. */
850 elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
852 struct bfd_link_info *info;
853 const Elf_Internal_Sym *sym;
859 *valp += (*secp)->vma;
863 /* Determine the name of the stub needed to perform a call assuming the
864 argument relocation bits for caller and callee are in CALLER and CALLEE
865 for a call from LOCATION to DESTINATION. Copy the name into STUB_NAME. */
868 elf32_hppa_name_of_stub (location, destination, stub_name)
869 bfd_vma location, destination;
872 strcpy (stub_name, "_____long_branch_stub_");
875 /* Compute the size of the stub needed to call from LOCATION to DESTINATION
876 (a function named SYM_NAME), with argument relocation bits CALLER and
877 CALLEE. Return zero if no stub is needed to perform such a call. */
880 elf32_hppa_size_of_stub (location, destination, sym_name)
881 bfd_vma location, destination;
882 const char *sym_name;
884 /* Determine if a long branch stub is needed. */
885 if (!(((int)(location - destination) > 0x3ffff)
886 || ((int)(location - destination) < (int)0xfffc0000)))
889 if (!strncmp ("$$", sym_name, 2)
890 && strcmp ("$$dyncall", sym_name))
896 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
897 IN_ARGS contains the stub BFD and link info pointers. */
900 elf32_hppa_build_one_stub (gen_entry, in_args)
901 struct bfd_hash_entry *gen_entry;
904 void **args = (void **)in_args;
905 bfd *stub_bfd = (bfd *)args[0];
906 struct bfd_link_info *info = (struct bfd_link_info *)args[1];
907 struct elf32_hppa_stub_hash_entry *entry;
908 struct elf32_hppa_stub_hash_table *stub_hash_table;
911 const char *sym_name;
913 /* Initialize pointers to the stub hash table, the particular entry we
914 are building a stub for, and where (in memory) we should place the stub
916 entry = (struct elf32_hppa_stub_hash_entry *)gen_entry;
917 stub_hash_table = elf32_hppa_hash_table(info)->stub_hash_table;
918 loc = stub_hash_table->location;
920 /* Make a note of the offset within the stubs for this entry. */
921 entry->offset = stub_hash_table->offset;
923 /* The symbol's name starts at offset 22. */
924 sym_name = entry->root.string + 22;
926 sym_value = (entry->target_value
927 + entry->target_section->output_offset
928 + entry->target_section->output_section->vma);
932 /* Create one of two variant long branch stubs. One for $$dyncall and
933 normal calls, the other for calls to millicode. */
935 int millicode_call = 0;
937 if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name))
940 /* First the return pointer adjustment. Depending on exact calling
941 sequence this instruction may be skipped. */
942 bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
944 /* The next two instructions are the long branch itself. A long branch
945 is formed with "ldil" loading the upper bits of the target address
946 into a register, then branching with "be" which adds in the lower bits.
947 Long branches to millicode nullify the delay slot of the "be". */
948 insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
949 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
950 bfd_put_32 (stub_bfd, insn, loc + 4);
951 insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1 | (millicode_call ? 2 : 0),
952 hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
954 bfd_put_32 (stub_bfd, insn, loc + 8);
958 /* The sequence to call this stub places the return pointer into %r31,
959 the final target expects the return pointer in %r2, so copy the
960 return pointer into the proper register. */
961 bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 12);
963 /* Update the location and offsets. */
964 stub_hash_table->location += 16;
965 stub_hash_table->offset += 16;
969 /* Update the location and offsets. */
970 stub_hash_table->location += 12;
971 stub_hash_table->offset += 12;
978 /* External entry points for sizing and building linker stubs. */
980 /* Build all the stubs associated with the current output file. The
981 stubs are kept in a hash table attached to the main linker hash
982 table. This is called via hppaelf_finish in the linker. */
985 elf32_hppa_build_stubs (stub_bfd, info)
987 struct bfd_link_info *info;
989 /* The stub BFD only has one section. */
990 asection *stub_sec = stub_bfd->sections;
991 struct elf32_hppa_stub_hash_table *table;
995 /* So we can pass both the BFD for the stubs and the link info
996 structure to the routine which actually builds stubs. */
1000 /* Allocate memory to hold the linker stubs. */
1001 size = bfd_section_size (stub_bfd, stub_sec);
1002 stub_sec->contents = (unsigned char *) bfd_zalloc (stub_bfd, size);
1003 if (stub_sec->contents == NULL)
1005 table = elf32_hppa_hash_table(info)->stub_hash_table;
1006 table->location = stub_sec->contents;
1008 /* Build the stubs as directed by the stub hash table. */
1009 elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args);
1014 /* Determine and set the size of the stub section for a final link.
1016 The basic idea here is to examine all the relocations looking for
1017 PC-relative calls to a target that is unreachable with a "bl"
1018 instruction or calls where the caller and callee disagree on the
1019 location of their arguments or return value. */
1022 elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info)
1025 struct bfd_link_info *link_info;
1028 asection *section, *stub_sec = 0;
1029 Elf_Internal_Shdr *symtab_hdr;
1030 Elf_Internal_Sym *local_syms, *isym, **all_local_syms;
1031 Elf32_External_Sym *ext_syms, *esym;
1032 unsigned int i, index, bfd_count = 0;
1033 struct elf32_hppa_stub_hash_table *stub_hash_table = 0;
1035 /* Create and initialize the stub hash table. */
1036 stub_hash_table = ((struct elf32_hppa_stub_hash_table *)
1037 bfd_malloc (sizeof (struct elf32_hppa_stub_hash_table)));
1038 if (!stub_hash_table)
1041 if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd,
1042 elf32_hppa_stub_hash_newfunc))
1045 /* Attach the hash tables to the main hash table. */
1046 elf32_hppa_hash_table(link_info)->stub_hash_table = stub_hash_table;
1048 /* Count the number of input BFDs. */
1049 for (input_bfd = link_info->input_bfds;
1051 input_bfd = input_bfd->link_next)
1054 /* Magic as we know the stub bfd only has one section. */
1055 stub_sec = stub_bfd->sections;
1057 /* If generating a relocateable output file, then we don't
1058 have to examine the relocs. */
1059 if (link_info->relocateable)
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 /* Now that we have argument location information for all the global
1069 functions we can start looking for stubs. */
1070 for (input_bfd = link_info->input_bfds, index = 0;
1072 input_bfd = input_bfd->link_next, index++)
1074 /* We'll need the symbol table in a second. */
1075 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1076 if (symtab_hdr->sh_info == 0)
1079 local_syms = all_local_syms[index];
1081 /* Walk over each section attached to the input bfd. */
1082 for (section = input_bfd->sections;
1084 section = section->next)
1086 Elf_Internal_Shdr *input_rel_hdr;
1087 Elf32_External_Rela *external_relocs, *erelaend, *erela;
1088 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1090 /* If there aren't any relocs, then there's nothing to do. */
1091 if ((section->flags & SEC_RELOC) == 0
1092 || section->reloc_count == 0)
1095 /* Allocate space for the external relocations. */
1097 = ((Elf32_External_Rela *)
1098 bfd_malloc (section->reloc_count
1099 * sizeof (Elf32_External_Rela)));
1100 if (external_relocs == NULL)
1102 for (i = 0; i < bfd_count; i++)
1103 if (all_local_syms[i])
1104 free (all_local_syms[i]);
1105 free (all_local_syms);
1109 /* Likewise for the internal relocations. */
1111 = ((Elf_Internal_Rela *)
1112 bfd_malloc (section->reloc_count * sizeof (Elf_Internal_Rela)));
1113 if (internal_relocs == NULL)
1115 free (external_relocs);
1116 for (i = 0; i < bfd_count; i++)
1117 if (all_local_syms[i])
1118 free (all_local_syms[i]);
1119 free (all_local_syms);
1123 /* Read in the external relocs. */
1124 input_rel_hdr = &elf_section_data (section)->rel_hdr;
1125 if (bfd_seek (input_bfd, input_rel_hdr->sh_offset, SEEK_SET) != 0
1126 || bfd_read (external_relocs, 1, input_rel_hdr->sh_size,
1127 input_bfd) != input_rel_hdr->sh_size)
1129 free (external_relocs);
1130 free (internal_relocs);
1131 for (i = 0; i < bfd_count; i++)
1132 if (all_local_syms[i])
1133 free (all_local_syms[i]);
1134 free (all_local_syms);
1138 /* Swap in the relocs. */
1139 erela = external_relocs;
1140 erelaend = erela + section->reloc_count;
1141 irela = internal_relocs;
1142 for (; erela < erelaend; erela++, irela++)
1143 bfd_elf32_swap_reloca_in (input_bfd, erela, irela);
1145 /* We're done with the external relocs, free them. */
1146 free (external_relocs);
1148 /* Now examine each relocation. */
1149 irela = internal_relocs;
1150 irelaend = irela + section->reloc_count;
1151 for (; irela < irelaend; irela++)
1153 long r_type, size_of_stub;
1154 unsigned long r_index;
1155 struct elf_link_hash_entry *hash;
1156 struct elf32_hppa_stub_hash_entry *stub_hash;
1157 Elf_Internal_Sym *sym;
1159 const char *sym_name;
1161 bfd_vma location, destination;
1162 char *new_name = NULL;
1164 r_type = ELF32_R_TYPE (irela->r_info);
1165 r_index = ELF32_R_SYM (irela->r_info);
1167 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED)
1169 bfd_set_error (bfd_error_bad_value);
1170 free (internal_relocs);
1171 for (i = 0; i < bfd_count; i++)
1172 if (all_local_syms[i])
1173 free (all_local_syms[i]);
1174 free (all_local_syms);
1178 /* Only look for stubs on call instructions or plabel
1180 if (r_type != R_PARISC_PCREL17F
1181 && r_type != R_PARISC_PLABEL32
1182 && r_type != R_PARISC_PLABEL21L
1183 && r_type != R_PARISC_PLABEL14R)
1186 /* Now determine the call target, its name, value, section
1187 and argument relocation bits. */
1191 if (r_index < symtab_hdr->sh_info)
1193 /* It's a local symbol. */
1194 Elf_Internal_Shdr *hdr;
1196 sym = local_syms + r_index;
1197 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
1198 sym_sec = hdr->bfd_section;
1199 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1200 symtab_hdr->sh_link,
1202 sym_value = (ELF_ST_TYPE (sym->st_info) == STT_SECTION
1203 ? 0 : sym->st_value);
1204 destination = (sym_value
1205 + sym_sec->output_offset
1206 + sym_sec->output_section->vma);
1208 /* Tack on an ID so we can uniquely identify this local
1209 symbol in the stub or arg info hash tables. */
1210 new_name = bfd_malloc (strlen (sym_name) + 10);
1213 free (internal_relocs);
1214 for (i = 0; i < bfd_count; i++)
1215 if (all_local_syms[i])
1216 free (all_local_syms[i]);
1217 free (all_local_syms);
1220 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1221 sym_name = new_name;
1225 /* It's an external symbol. */
1228 index = r_index - symtab_hdr->sh_info;
1229 hash = elf_sym_hashes (input_bfd)[index];
1230 if (hash->root.type == bfd_link_hash_defined
1231 || hash->root.type == bfd_link_hash_defweak)
1233 sym_sec = hash->root.u.def.section;
1234 sym_name = hash->root.root.string;
1235 sym_value = hash->root.u.def.value;
1236 destination = (sym_value
1237 + sym_sec->output_offset
1238 + sym_sec->output_section->vma);
1242 bfd_set_error (bfd_error_bad_value);
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);
1252 /* Now determine where the call point is. */
1253 location = (section->output_offset
1254 + section->output_section->vma
1257 /* We only care about the destination for PCREL function
1258 calls (eg. we don't care for PLABELS). */
1259 if (r_type != R_PARISC_PCREL17F)
1260 location = destination;
1262 /* Determine what (if any) linker stub is needed and its
1264 size_of_stub = elf32_hppa_size_of_stub (location,
1267 if (size_of_stub != 0)
1272 /* Get the name of this stub. */
1273 len = strlen (sym_name);
1276 stub_name = bfd_malloc (len);
1279 /* Because sym_name was mallocd above for local
1281 if (r_index < symtab_hdr->sh_info)
1284 free (internal_relocs);
1285 for (i = 0; i < bfd_count; i++)
1286 if (all_local_syms[i])
1287 free (all_local_syms[i]);
1288 free (all_local_syms);
1291 elf32_hppa_name_of_stub (location, destination, stub_name);
1292 strcat (stub_name + 22, sym_name);
1294 /* Because sym_name was malloced above for local symbols. */
1295 if (r_index < symtab_hdr->sh_info)
1299 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
1301 if (stub_hash != NULL)
1303 /* The proper stub has already been created, nothing
1309 bfd_set_section_size (stub_bfd, stub_sec,
1310 (bfd_section_size (stub_bfd,
1314 /* Enter this entry into the linker stub hash table. */
1316 = elf32_hppa_stub_hash_lookup (stub_hash_table,
1317 stub_name, true, true);
1318 if (stub_hash == NULL)
1321 free (internal_relocs);
1322 for (i = 0; i < bfd_count; i++)
1323 if (all_local_syms[i])
1324 free (all_local_syms[i]);
1325 free (all_local_syms);
1329 /* We'll need these to determine the address that the
1330 stub will branch to. */
1331 stub_hash->target_value = sym_value;
1332 stub_hash->target_section = sym_sec;
1337 /* We're done with the internal relocs, free them. */
1338 free (internal_relocs);
1341 /* We're done with the local symbols, free them. */
1342 for (i = 0; i < bfd_count; i++)
1343 if (all_local_syms[i])
1344 free (all_local_syms[i]);
1345 free (all_local_syms);
1349 /* Return gracefully, avoiding dangling references to the hash tables. */
1350 if (stub_hash_table)
1352 elf32_hppa_hash_table(link_info)->stub_hash_table = NULL;
1353 free (stub_hash_table);
1355 /* Set the size of the stub section to zero since we're never going
1356 to create them. Avoids losing when we try to get its contents
1358 bfd_set_section_size (stub_bfd, stub_sec, 0);
1362 /* Misc BFD support code. */
1363 #define bfd_elf32_bfd_reloc_type_lookup elf_hppa_reloc_type_lookup
1364 #define bfd_elf32_bfd_is_local_label_name hppa_elf_is_local_label_name
1365 #define elf_info_to_howto elf_hppa_info_to_howto
1366 #define elf_info_to_howto_rel elf_hppa_info_to_howto_rel
1368 /* Stuff for the BFD linker. */
1369 #define elf_backend_relocate_section elf32_hppa_relocate_section
1370 #define elf_backend_add_symbol_hook elf32_hppa_add_symbol_hook
1371 #define bfd_elf32_bfd_link_hash_table_create \
1372 elf32_hppa_link_hash_table_create
1374 #define TARGET_BIG_SYM bfd_elf32_hppa_vec
1375 #define TARGET_BIG_NAME "elf32-hppa"
1376 #define ELF_ARCH bfd_arch_hppa
1377 #define ELF_MACHINE_CODE EM_PARISC
1378 #define ELF_MAXPAGESIZE 0x1000
1380 #include "elf32-target.h"