* dwarf2.c (read_attribute): Support DW_FORM_ref8.
[binutils.git] / bfd / elf32-hppa.c
blob1813b98bbc6f4b5b46755eea301e4629f36b9cea
1 /* BFD back-end for HP PA-RISC ELF files.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 Written by
7 Center for Software Science
8 Department of Computer Science
9 University of Utah
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. */
27 #include "bfd.h"
28 #include "sysdep.h"
29 #include "libbfd.h"
30 #include "elf-bfd.h"
31 #include "elf/hppa.h"
32 #include "libhppa.h"
33 #include "elf32-hppa.h"
34 #define ARCH_SIZE 32
35 #include "elf-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
55 hash table entry. */
56 struct bfd_hash_entry root;
58 /* Offset of the beginning of this stub. */
59 bfd_vma offset;
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;
72 /* The stub BFD. */
73 bfd *stub_bfd;
75 /* Where to place the next stub. */
76 bfd_byte *location;
78 /* Current offset in the stub section. */
79 unsigned int offset;
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. */
100 long global_value;
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
108 February 1994. */
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
129 PARAMS ((bfd *));
131 static struct bfd_hash_entry *
132 elf32_hppa_stub_hash_newfunc
133 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
135 static boolean
136 elf32_hppa_relocate_section
137 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *,
138 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
140 static boolean
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 *,
145 const char *))));
147 static boolean
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) \
162 (bfd_hash_traverse \
163 (&(table)->root, \
164 (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \
165 (info)))
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), \
172 (copy), (follow)))
174 #define elf32_hppa_link_hash_traverse(table, func, info) \
175 (elf_link_hash_traverse \
176 (&(table)->root, \
177 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
178 (info)))
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;
194 const char *string;
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
201 subclass. */
202 if (ret == NULL)
203 ret = ((struct elf32_hppa_stub_hash_entry *)
204 bfd_hash_allocate (table,
205 sizeof (struct elf32_hppa_stub_hash_entry)));
206 if (ret == NULL)
207 return NULL;
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));
213 if (ret)
215 /* Initialize the local fields. */
216 ret->offset = 0;
217 ret->target_value = 0;
218 ret->target_section = NULL;
221 return (struct bfd_hash_entry *) ret;
224 /* Initialize a stub hash table. */
226 static boolean
227 elf32_hppa_stub_hash_table_init (table, stub_bfd, newfunc)
228 struct elf32_hppa_stub_hash_table *table;
229 bfd *stub_bfd;
230 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
231 struct bfd_hash_table *,
232 const char *));
234 table->offset = 0;
235 table->location = 0;
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)
246 bfd *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)));
252 if (ret == NULL)
253 return NULL;
254 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
255 _bfd_elf_link_hash_newfunc))
257 bfd_release (abfd, ret);
258 return NULL;
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. */
272 static unsigned long
273 hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value,
274 r_addend, r_format, r_field, pcrel)
275 bfd *abfd;
276 asection *input_sect;
277 unsigned long insn;
278 unsigned long address;
279 long sym_value;
280 long r_addend;
281 unsigned long r_format;
282 unsigned long r_field;
283 unsigned long pcrel;
285 unsigned char opcode = get_opcode (insn);
286 long constant_value;
288 switch (opcode)
290 case LDO:
291 case LDB:
292 case LDH:
293 case LDW:
294 case LDWM:
295 case STB:
296 case STH:
297 case STW:
298 case STWM:
299 case COMICLR:
300 case SUBI:
301 case ADDIT:
302 case ADDI:
303 case LDIL:
304 case ADDIL:
305 constant_value = HPPA_R_CONSTANT (r_addend);
307 if (pcrel)
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);
313 case BL:
314 case BE:
315 case BLE:
316 /* XXX computing constant_value is not needed??? */
317 constant_value = assemble_17 ((insn & 0x001f0000) >> 16,
318 (insn & 0x00001ffc) >> 2,
319 insn & 1);
321 constant_value = (constant_value << 15) >> 15;
322 if (pcrel)
324 sym_value -=
325 address + input_sect->output_offset
326 + input_sect->output_section->vma;
327 sym_value = hppa_field_adjust (sym_value, -8, r_field);
329 else
330 sym_value = hppa_field_adjust (sym_value, constant_value, r_field);
332 return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format);
334 default:
335 if (opcode == 0)
337 constant_value = HPPA_R_CONSTANT (r_addend);
339 if (pcrel)
340 sym_value -= address;
342 return hppa_field_adjust (sym_value, constant_value, r_field);
344 else
345 abort ();
349 /* Relocate an HPPA ELF section. */
351 static boolean
352 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section,
353 contents, relocs, local_syms, local_sections)
354 bfd *output_bfd;
355 struct bfd_link_info *info;
356 bfd *input_bfd;
357 asection *input_section;
358 bfd_byte *contents;
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;
369 rel = relocs;
370 relend = relocs + input_section->reloc_count;
371 for (; rel < relend; rel++)
373 int r_type;
374 reloc_howto_type *howto;
375 unsigned long r_symndx;
376 struct elf_link_hash_entry *h;
377 Elf_Internal_Sym *sym;
378 asection *sym_sec;
379 bfd_vma relocation;
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);
387 return false;
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;
409 continue;
412 /* This is a final link. */
413 h = NULL;
414 sym = NULL;
415 sym_sec = NULL;
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
421 ? 0 : sym->st_value)
422 + sym_sec->output_offset
423 + sym_sec->output_section->vma);
425 else
427 long indx;
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)
443 relocation = 0;
444 else
446 if (!((*info->callbacks->undefined_symbol)
447 (info, h->root.root.string, input_bfd,
448 input_section, rel->r_offset)))
449 return false;
450 break;
454 if (h != NULL)
455 sym_name = h->root.root.string;
456 else
458 sym_name = bfd_elf_string_from_elf_section (input_bfd,
459 symtab_hdr->sh_link,
460 sym->st_name);
461 if (sym_name == NULL)
462 return false;
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)
475 switch (r)
477 /* This can happen for DP relative relocs if $global$ is
478 undefined. This is a panic situation so we don't try
479 to continue. */
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)))
485 return false;
486 return false;
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. */
501 if (rel != relend)
503 Elf_Internal_Rela *next_rel = rel + 1;
505 if (rel->r_offset + 4 == next_rel->r_offset)
506 next_rel->r_offset -= 4;
508 break;
510 default:
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)))
517 return false;
519 break;
524 return true;
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;
535 bfd *input_bfd;
536 bfd *output_bfd ATTRIBUTE_UNUSED;
537 asection *input_section;
538 bfd_byte *contents;
539 bfd_vma offset;
540 bfd_vma value;
541 bfd_vma addend;
542 struct bfd_link_info *info;
543 asection *sym_sec;
544 const char *sym_name;
545 int is_local;
547 unsigned long insn;
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
558 purpose? */
559 if (!elf32_hppa_hash_table (info)->global_sym_defined)
561 struct elf_link_hash_entry *h;
562 asection *sec;
564 h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false,
565 false, false);
567 /* If there isn't a $global$, then we're in deep trouble. */
568 if (h == NULL)
569 return bfd_reloc_notsupported;
571 /* If $global$ isn't a defined symbol, then we're still in deep
572 trouble. */
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;
583 switch (r_type)
585 case R_PARISC_NONE:
586 break;
588 case R_PARISC_DIR32:
589 case R_PARISC_DIR17F:
590 case R_PARISC_PCREL17C:
591 r_field = e_fsel;
592 goto do_basic_type_1;
593 case R_PARISC_DIR21L:
594 case R_PARISC_PCREL21L:
595 r_field = e_lrsel;
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:
601 r_field = e_rrsel;
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
608 %r0. */
609 case R_PARISC_DPREL21L:
610 r_field = e_lrsel;
611 if (sym_sec->flags & SEC_CODE)
613 if ((insn & 0xfc000000) >> 26 == 0xa
614 && (insn & 0x03e00000) >> 21 == 0x1b)
615 insn &= ~0x03e00000;
617 else
618 value -= elf32_hppa_hash_table (info)->global_value;
619 goto do_basic_type_1;
620 case R_PARISC_DPREL14R:
621 r_field = e_rrsel;
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:
626 r_field = e_fsel;
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:
638 bfd_vma location;
639 unsigned int len;
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)
645 r_field = e_fsel;
646 else if (r_type == R_PARISC_PLABEL21L)
647 r_field = e_lrsel;
648 else if (r_type == R_PARISC_PLABEL14R)
649 r_field = e_rrsel;
651 /* Find out where we are and where we're going. */
652 location = (offset +
653 input_section->output_offset +
654 input_section->output_section->vma);
656 len = strlen (sym_name) + 1;
657 if (is_local)
658 len += 9;
659 new_name = bfd_malloc (len);
660 if (!new_name)
661 return bfd_reloc_notsupported;
662 strcpy (new_name, sym_name);
664 /* Local symbols have unique IDs. */
665 if (is_local)
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);
679 len += 23;
680 stub_name = bfd_malloc (len);
681 if (!stub_name)
682 return bfd_reloc_notsupported;
683 elf32_hppa_name_of_stub (location, value, stub_name);
684 strcat (stub_name, new_name);
685 free (new_name);
687 stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table;
689 stub_hash
690 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
691 false, false);
693 /* We're done with that name. */
694 free (stub_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
711 call. */
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);
724 r_format = 17;
725 r_field = e_fsel;
726 r_pcrel = 0;
727 addend = 0;
729 /* We need to peek at the delay insn and determine if
730 we'll need to swap the branch and its delay insn. */
731 if ((insn & 2)
732 || (opcode == LDO
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. */
739 value += 4;
741 /* If the delay insn does a return pointer adjustment,
742 then we have to make sure it stays valid. */
743 if (opcode == LDO
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. */
751 insn = BLE_SR4_R0;
753 else
755 /* Wonderful, we have to swap the call insn and its
756 delay slot. */
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,
762 input_section,
763 insn, offset + 4,
764 value, addend,
765 r_format, r_field,
766 r_pcrel);
767 /* Update the instruction word. */
768 bfd_put_32 (input_bfd, insn, hit_data + 4);
769 return bfd_reloc_dangerous;
772 else
773 return bfd_reloc_notsupported;
776 goto do_basic_type_1;
779 do_basic_type_1:
780 insn = hppa_elf_relocate_insn (input_bfd, input_section, insn,
781 offset, value, addend, r_format,
782 r_field, r_pcrel);
783 break;
785 /* Something we don't know how to handle. */
786 default:
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. */
797 static boolean
798 hppa_elf_is_local_label_name (abfd, name)
799 bfd *abfd ATTRIBUTE_UNUSED;
800 const char *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. */
808 static boolean
809 elf32_hppa_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
810 bfd *abfd ATTRIBUTE_UNUSED;
811 struct bfd_link_info *info ATTRIBUTE_UNUSED;
812 const Elf_Internal_Sym *sym ATTRIBUTE_UNUSED;
813 const char **namep ATTRIBUTE_UNUSED;
814 flagword *flagsp ATTRIBUTE_UNUSED;
815 asection **secp;
816 bfd_vma *valp;
818 *valp += (*secp)->vma;
819 return true;
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. */
826 static void
827 elf32_hppa_name_of_stub (location, destination, stub_name)
828 bfd_vma location ATTRIBUTE_UNUSED;
829 bfd_vma destination ATTRIBUTE_UNUSED;
830 char *stub_name;
832 strcpy (stub_name, "_____long_branch_stub_");
835 /* Compute the size of the stub needed to call from LOCATION to DESTINATION
836 (a function named SYM_NAME), with argument relocation bits CALLER and
837 CALLEE. Return zero if no stub is needed to perform such a call. */
839 static unsigned int
840 elf32_hppa_size_of_stub (location, destination, sym_name)
841 bfd_vma location, destination;
842 const char *sym_name;
844 /* Determine if a long branch stub is needed. */
845 if (!(((int)(location - destination) > 0x3ffff)
846 || ((int)(location - destination) < (int)0xfffc0000)))
847 return 0;
849 if (!strncmp ("$$", sym_name, 2)
850 && strcmp ("$$dyncall", sym_name))
851 return 12;
852 else
853 return 16;
856 /* Build one linker stub as defined by the stub hash table entry GEN_ENTRY.
857 IN_ARGS contains the stub BFD and link info pointers. */
859 static boolean
860 elf32_hppa_build_one_stub (gen_entry, in_args)
861 struct bfd_hash_entry *gen_entry;
862 PTR in_args;
864 void **args = (void **)in_args;
865 bfd *stub_bfd = (bfd *)args[0];
866 struct bfd_link_info *info = (struct bfd_link_info *)args[1];
867 struct elf32_hppa_stub_hash_entry *entry;
868 struct elf32_hppa_stub_hash_table *stub_hash_table;
869 bfd_byte *loc;
870 symvalue sym_value;
871 const char *sym_name;
873 /* Initialize pointers to the stub hash table, the particular entry we
874 are building a stub for, and where (in memory) we should place the stub
875 instructions. */
876 entry = (struct elf32_hppa_stub_hash_entry *)gen_entry;
877 stub_hash_table = elf32_hppa_hash_table(info)->stub_hash_table;
878 loc = stub_hash_table->location;
880 /* Make a note of the offset within the stubs for this entry. */
881 entry->offset = stub_hash_table->offset;
883 /* The symbol's name starts at offset 22. */
884 sym_name = entry->root.string + 22;
886 sym_value = (entry->target_value
887 + entry->target_section->output_offset
888 + entry->target_section->output_section->vma);
890 if (1)
892 /* Create one of two variant long branch stubs. One for $$dyncall and
893 normal calls, the other for calls to millicode. */
894 unsigned long insn;
895 int millicode_call = 0;
897 if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name))
898 millicode_call = 1;
900 /* First the return pointer adjustment. Depending on exact calling
901 sequence this instruction may be skipped. */
902 bfd_put_32 (stub_bfd, LDO_M4_R31_R31, loc);
904 /* The next two instructions are the long branch itself. A long branch
905 is formed with "ldil" loading the upper bits of the target address
906 into a register, then branching with "be" which adds in the lower bits.
907 Long branches to millicode nullify the delay slot of the "be". */
908 insn = hppa_rebuild_insn (stub_bfd, LDIL_R1,
909 hppa_field_adjust (sym_value, 0, e_lrsel), 21);
910 bfd_put_32 (stub_bfd, insn, loc + 4);
911 insn = hppa_rebuild_insn (stub_bfd, BE_SR4_R1 | (millicode_call ? 2 : 0),
912 hppa_field_adjust (sym_value, 0, e_rrsel) >> 2,
913 17);
914 bfd_put_32 (stub_bfd, insn, loc + 8);
916 if (!millicode_call)
918 /* The sequence to call this stub places the return pointer into %r31,
919 the final target expects the return pointer in %r2, so copy the
920 return pointer into the proper register. */
921 bfd_put_32 (stub_bfd, COPY_R31_R2, loc + 12);
923 /* Update the location and offsets. */
924 stub_hash_table->location += 16;
925 stub_hash_table->offset += 16;
927 else
929 /* Update the location and offsets. */
930 stub_hash_table->location += 12;
931 stub_hash_table->offset += 12;
935 return true;
938 /* External entry points for sizing and building linker stubs. */
940 /* Build all the stubs associated with the current output file. The
941 stubs are kept in a hash table attached to the main linker hash
942 table. This is called via hppaelf_finish in the linker. */
944 boolean
945 elf32_hppa_build_stubs (stub_bfd, info)
946 bfd *stub_bfd;
947 struct bfd_link_info *info;
949 /* The stub BFD only has one section. */
950 asection *stub_sec = stub_bfd->sections;
951 struct elf32_hppa_stub_hash_table *table;
952 unsigned int size;
953 void *args[2];
955 /* So we can pass both the BFD for the stubs and the link info
956 structure to the routine which actually builds stubs. */
957 args[0] = stub_bfd;
958 args[1] = info;
960 /* Allocate memory to hold the linker stubs. */
961 size = bfd_section_size (stub_bfd, stub_sec);
962 stub_sec->contents = (unsigned char *) bfd_zalloc (stub_bfd, size);
963 if (stub_sec->contents == NULL)
964 return false;
965 table = elf32_hppa_hash_table(info)->stub_hash_table;
966 table->location = stub_sec->contents;
968 /* Build the stubs as directed by the stub hash table. */
969 elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args);
971 return true;
974 /* Determine and set the size of the stub section for a final link.
976 The basic idea here is to examine all the relocations looking for
977 PC-relative calls to a target that is unreachable with a "bl"
978 instruction or calls where the caller and callee disagree on the
979 location of their arguments or return value. */
981 boolean
982 elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info)
983 bfd *stub_bfd;
984 bfd *output_bfd ATTRIBUTE_UNUSED;
985 struct bfd_link_info *link_info;
987 bfd *input_bfd;
988 asection *section, *stub_sec = 0;
989 Elf_Internal_Shdr *symtab_hdr;
990 Elf_Internal_Sym *local_syms, **all_local_syms;
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)
998 goto error_return;
1000 if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd,
1001 elf32_hppa_stub_hash_newfunc))
1002 goto error_return;
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;
1009 input_bfd != NULL;
1010 input_bfd = input_bfd->link_next)
1011 bfd_count++;
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);
1024 return true;
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;
1030 input_bfd != NULL;
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)
1036 continue;
1038 local_syms = all_local_syms[index];
1040 /* Walk over each section attached to the input bfd. */
1041 for (section = input_bfd->sections;
1042 section != NULL;
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)
1052 continue;
1054 /* Allocate space for the external relocations. */
1055 external_relocs
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);
1065 goto error_return;
1068 /* Likewise for the internal relocations. */
1069 internal_relocs
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);
1079 goto error_return;
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);
1094 goto error_return;
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;
1117 asection *sym_sec;
1118 const char *sym_name;
1119 symvalue sym_value;
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);
1134 goto error_return;
1137 /* Only look for stubs on call instructions or plabel
1138 references. */
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)
1143 continue;
1145 /* Now determine the call target, its name, value, section
1146 and argument relocation bits. */
1147 hash = NULL;
1148 sym = NULL;
1149 sym_sec = NULL;
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,
1160 sym->st_name);
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);
1170 if (new_name == 0)
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);
1177 goto error_return;
1179 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1180 sym_name = new_name;
1182 else
1184 /* It's an external symbol. */
1185 long index;
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);
1199 else
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);
1207 goto error_return;
1211 /* Now determine where the call point is. */
1212 location = (section->output_offset
1213 + section->output_section->vma
1214 + irela->r_offset);
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
1222 size (in bytes). */
1223 size_of_stub = elf32_hppa_size_of_stub (location,
1224 destination,
1225 sym_name);
1226 if (size_of_stub != 0)
1228 char *stub_name;
1229 unsigned int len;
1231 /* Get the name of this stub. */
1232 len = strlen (sym_name);
1233 len += 23;
1235 stub_name = bfd_malloc (len);
1236 if (!stub_name)
1238 /* Because sym_name was mallocd above for local
1239 symbols. */
1240 if (r_index < symtab_hdr->sh_info)
1241 free (new_name);
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);
1248 goto error_return;
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)
1255 free (new_name);
1257 stub_hash
1258 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name,
1259 false, false);
1260 if (stub_hash != NULL)
1262 /* The proper stub has already been created, nothing
1263 else to do. */
1264 free (stub_name);
1266 else
1268 bfd_set_section_size (stub_bfd, stub_sec,
1269 (bfd_section_size (stub_bfd,
1270 stub_sec)
1271 + size_of_stub));
1273 /* Enter this entry into the linker stub hash table. */
1274 stub_hash
1275 = elf32_hppa_stub_hash_lookup (stub_hash_table,
1276 stub_name, true, true);
1277 if (stub_hash == NULL)
1279 free (stub_name);
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);
1285 goto error_return;
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;
1293 free (stub_name);
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);
1305 return true;
1307 error_return:
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
1316 too. */
1317 bfd_set_section_size (stub_bfd, stub_sec, 0);
1318 return false;
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"