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. 4 5 Written by 6 7 Center for Software Science 8 Department of Computer Science 9 University of Utah 10 11 This file is part of BFD, the Binary File Descriptor library. 12 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. 17 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. 22 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. */ 26 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" 36 37 38 /* We use three different hash tables to hold information for 39 linking PA ELF objects. 40 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. 44 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. */ 48 49 /* Hash table for linker stubs. */ 50 51 struct elf32_hppa_stub_hash_entry 52 { 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; 57 58 /* Offset of the beginning of this stub. */ 59 bfd_vma offset; 60 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; 65 }; 66 67 struct elf32_hppa_stub_hash_table 68 { 69 /* The hash table itself. */ 70 struct bfd_hash_table root; 71 72 /* The stub BFD. */ 73 bfd *stub_bfd; 74 75 /* Where to place the next stub. */ 76 bfd_byte *location; 77 78 /* Current offset in the stub section. */ 79 unsigned int offset; 80 81 }; 82 83 struct elf32_hppa_link_hash_entry 84 { 85 struct elf_link_hash_entry root; 86 }; 87 88 struct elf32_hppa_link_hash_table 89 { 90 /* The main hash table. */ 91 struct elf_link_hash_table root; 92 93 /* The stub hash table. */ 94 struct elf32_hppa_stub_hash_table *stub_hash_table; 95 96 /* A count of the number of output symbols. */ 97 unsigned int output_symbol_count; 98 99 /* Stuff so we can handle DP relative relocations. */ 100 long global_value; 101 int global_sym_defined; 102 }; 103 104 /* ELF32/HPPA relocation support 105 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. */ 109 110 #include "elf32-hppa.h" 111 #include "hppa_stubs.h" 112 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)); 116 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 *)); 120 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)); 125 126 static struct bfd_link_hash_table *elf32_hppa_link_hash_table_create 127 PARAMS ((bfd *)); 128 129 static struct bfd_hash_entry * 130 elf32_hppa_stub_hash_newfunc 131 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); 132 133 static boolean 134 elf32_hppa_relocate_section 135 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, 136 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); 137 138 static boolean 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 *, 143 const char *)))); 144 145 static boolean 146 elf32_hppa_build_one_stub PARAMS ((struct bfd_hash_entry *, PTR)); 147 148 static unsigned int elf32_hppa_size_of_stub 149 PARAMS ((bfd_vma, bfd_vma, const char *)); 150 151 static void elf32_hppa_name_of_stub 152 PARAMS ((bfd_vma, bfd_vma, char *)); 153 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))) 158 159 #define elf32_hppa_stub_hash_traverse(table, func, info) \ 160 (bfd_hash_traverse \ 161 (&(table)->root, \ 162 (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func), \ 163 (info))) 164 165 /* For HPPA linker hash table. */ 166 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), \ 170 (copy), (follow))) 171 172 #define elf32_hppa_link_hash_traverse(table, func, info) \ 173 (elf_link_hash_traverse \ 174 (&(table)->root, \ 175 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ 176 (info))) 177 178 /* Get the PA ELF linker hash table from a link_info structure. */ 179 180 #define elf32_hppa_hash_table(p) \ 181 ((struct elf32_hppa_link_hash_table *) ((p)->hash)) 182 183 184 /* Assorted hash table functions. */ 185 186 /* Initialize an entry in the stub hash table. */ 187 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; 192 const char *string; 193 { 194 struct elf32_hppa_stub_hash_entry *ret; 195 196 ret = (struct elf32_hppa_stub_hash_entry *) entry; 197 198 /* Allocate the structure if it has not already been allocated by a 199 subclass. */ 200 if (ret == NULL) 201 ret = ((struct elf32_hppa_stub_hash_entry *) 202 bfd_hash_allocate (table, 203 sizeof (struct elf32_hppa_stub_hash_entry))); 204 if (ret == NULL) 205 return NULL; 206 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)); 210 211 if (ret) 212 { 213 /* Initialize the local fields. */ 214 ret->offset = 0; 215 ret->target_value = 0; 216 ret->target_section = NULL; 217 } 218 219 return (struct bfd_hash_entry *) ret; 220 } 221 222 /* Initialize a stub hash table. */ 223 224 static boolean 225 elf32_hppa_stub_hash_table_init (table, stub_bfd, newfunc) 226 struct elf32_hppa_stub_hash_table *table; 227 bfd *stub_bfd; 228 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, 229 struct bfd_hash_table *, 230 const char *)); 231 { 232 table->offset = 0; 233 table->location = 0; 234 table->stub_bfd = stub_bfd; 235 return (bfd_hash_table_init (&table->root, newfunc)); 236 } 237 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). */ 241 242 static struct bfd_link_hash_table * 243 elf32_hppa_link_hash_table_create (abfd) 244 bfd *abfd; 245 { 246 struct elf32_hppa_link_hash_table *ret; 247 248 ret = ((struct elf32_hppa_link_hash_table *) 249 bfd_alloc (abfd, sizeof (struct elf32_hppa_link_hash_table))); 250 if (ret == NULL) 251 return NULL; 252 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, 253 _bfd_elf_link_hash_newfunc)) 254 { 255 bfd_release (abfd, ret); 256 return NULL; 257 } 258 ret->stub_hash_table = NULL; 259 ret->output_symbol_count = 0; 260 ret->global_value = 0; 261 ret->global_sym_defined = 0; 262 263 return &ret->root.root; 264 } 265 266 /* Relocate the given INSN given the various input parameters. 267 268 FIXME: endianness and sizeof (long) issues abound here. */ 269 270 static unsigned long 271 hppa_elf_relocate_insn (abfd, input_sect, insn, address, sym_value, 272 r_addend, r_format, r_field, pcrel) 273 bfd *abfd; 274 asection *input_sect; 275 unsigned long insn; 276 unsigned long address; 277 long sym_value; 278 long r_addend; 279 unsigned long r_format; 280 unsigned long r_field; 281 unsigned long pcrel; 282 { 283 unsigned char opcode = get_opcode (insn); 284 long constant_value; 285 286 switch (opcode) 287 { 288 case LDO: 289 case LDB: 290 case LDH: 291 case LDW: 292 case LDWM: 293 case STB: 294 case STH: 295 case STW: 296 case STWM: 297 case COMICLR: 298 case SUBI: 299 case ADDIT: 300 case ADDI: 301 case LDIL: 302 case ADDIL: 303 constant_value = HPPA_R_CONSTANT (r_addend); 304 305 if (pcrel) 306 sym_value -= address; 307 308 sym_value = hppa_field_adjust (sym_value, constant_value, r_field); 309 return hppa_rebuild_insn (abfd, insn, sym_value, r_format); 310 311 case BL: 312 case BE: 313 case BLE: 314 /* XXX computing constant_value is not needed??? */ 315 constant_value = assemble_17 ((insn & 0x001f0000) >> 16, 316 (insn & 0x00001ffc) >> 2, 317 insn & 1); 318 319 constant_value = (constant_value << 15) >> 15; 320 if (pcrel) 321 { 322 sym_value -= 323 address + input_sect->output_offset 324 + input_sect->output_section->vma; 325 sym_value = hppa_field_adjust (sym_value, -8, r_field); 326 } 327 else 328 sym_value = hppa_field_adjust (sym_value, constant_value, r_field); 329 330 return hppa_rebuild_insn (abfd, insn, sym_value >> 2, r_format); 331 332 default: 333 if (opcode == 0) 334 { 335 constant_value = HPPA_R_CONSTANT (r_addend); 336 337 if (pcrel) 338 sym_value -= address; 339 340 return hppa_field_adjust (sym_value, constant_value, r_field); 341 } 342 else 343 abort (); 344 } 345 } 346 347 /* Relocate an HPPA ELF section. */ 348 349 static boolean 350 elf32_hppa_relocate_section (output_bfd, info, input_bfd, input_section, 351 contents, relocs, local_syms, local_sections) 352 bfd *output_bfd; 353 struct bfd_link_info *info; 354 bfd *input_bfd; 355 asection *input_section; 356 bfd_byte *contents; 357 Elf_Internal_Rela *relocs; 358 Elf_Internal_Sym *local_syms; 359 asection **local_sections; 360 { 361 Elf_Internal_Shdr *symtab_hdr; 362 Elf_Internal_Rela *rel; 363 Elf_Internal_Rela *relend; 364 365 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 366 367 rel = relocs; 368 relend = relocs + input_section->reloc_count; 369 for (; rel < relend; rel++) 370 { 371 int r_type; 372 reloc_howto_type *howto; 373 unsigned long r_symndx; 374 struct elf_link_hash_entry *h; 375 Elf_Internal_Sym *sym; 376 asection *sym_sec; 377 bfd_vma relocation; 378 bfd_reloc_status_type r; 379 const char *sym_name; 380 381 r_type = ELF32_R_TYPE (rel->r_info); 382 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED) 383 { 384 bfd_set_error (bfd_error_bad_value); 385 return false; 386 } 387 howto = elf_hppa_howto_table + r_type; 388 389 r_symndx = ELF32_R_SYM (rel->r_info); 390 391 if (info->relocateable) 392 { 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) 398 { 399 sym = local_syms + r_symndx; 400 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) 401 { 402 sym_sec = local_sections[r_symndx]; 403 rel->r_addend += sym_sec->output_offset; 404 } 405 } 406 407 continue; 408 } 409 410 /* This is a final link. */ 411 h = NULL; 412 sym = NULL; 413 sym_sec = NULL; 414 if (r_symndx < symtab_hdr->sh_info) 415 { 416 sym = local_syms + r_symndx; 417 sym_sec = local_sections[r_symndx]; 418 relocation = ((ELF_ST_TYPE (sym->st_info) == STT_SECTION 419 ? 0 : sym->st_value) 420 + sym_sec->output_offset 421 + sym_sec->output_section->vma); 422 } 423 else 424 { 425 long indx; 426 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) 434 { 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); 439 } 440 else if (h->root.type == bfd_link_hash_undefweak) 441 relocation = 0; 442 else 443 { 444 if (!((*info->callbacks->undefined_symbol) 445 (info, h->root.root.string, input_bfd, 446 input_section, rel->r_offset, true))) 447 return false; 448 break; 449 } 450 } 451 452 if (h != NULL) 453 sym_name = h->root.root.string; 454 else 455 { 456 sym_name = bfd_elf_string_from_elf_section (input_bfd, 457 symtab_hdr->sh_link, 458 sym->st_name); 459 if (sym_name == NULL) 460 return false; 461 if (*sym_name == '\0') 462 sym_name = bfd_section_name (input_bfd, sym_sec); 463 } 464 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); 470 471 if (r != bfd_reloc_ok) 472 { 473 switch (r) 474 { 475 /* This can happen for DP relative relocs if $global$ is 476 undefined. This is a panic situation so we don't try 477 to continue. */ 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))) 483 return false; 484 return false; 485 case bfd_reloc_dangerous: 486 { 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. 491 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. 496 497 This hair wouldn't be necessary if we inserted stubs 498 between procedures and used a "bl" to get to the stub. */ 499 if (rel != relend) 500 { 501 Elf_Internal_Rela *next_rel = rel + 1; 502 503 if (rel->r_offset + 4 == next_rel->r_offset) 504 next_rel->r_offset -= 4; 505 } 506 break; 507 } 508 default: 509 case bfd_reloc_outofrange: 510 case bfd_reloc_overflow: 511 { 512 if (!((*info->callbacks->reloc_overflow) 513 (info, sym_name, howto->name, (bfd_vma) 0, 514 input_bfd, input_section, rel->r_offset))) 515 return false; 516 } 517 break; 518 } 519 } 520 } 521 522 return true; 523 } 524 525 /* Actually perform a relocation as part of a final link. This can get 526 rather hairy when linker stubs are needed. */ 527 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; 533 bfd *input_bfd; 534 bfd *output_bfd ATTRIBUTE_UNUSED; 535 asection *input_section; 536 bfd_byte *contents; 537 bfd_vma offset; 538 bfd_vma value; 539 bfd_vma addend; 540 struct bfd_link_info *info; 541 asection *sym_sec; 542 const char *sym_name; 543 int is_local; 544 { 545 unsigned long insn; 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; 551 552 insn = bfd_get_32 (input_bfd, hit_data); 553 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 556 purpose? */ 557 if (!elf32_hppa_hash_table (info)->global_sym_defined) 558 { 559 struct elf_link_hash_entry *h; 560 asection *sec; 561 562 h = elf_link_hash_lookup (elf_hash_table (info), "$global$", false, 563 false, false); 564 565 /* If there isn't a $global$, then we're in deep trouble. */ 566 if (h == NULL) 567 return bfd_reloc_notsupported; 568 569 /* If $global$ isn't a defined symbol, then we're still in deep 570 trouble. */ 571 if (h->root.type != bfd_link_hash_defined) 572 return bfd_reloc_undefined; 573 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; 579 } 580 581 switch (r_type) 582 { 583 case R_PARISC_NONE: 584 break; 585 586 case R_PARISC_DIR32: 587 case R_PARISC_DIR17F: 588 case R_PARISC_PCREL17C: 589 r_field = e_fsel; 590 goto do_basic_type_1; 591 case R_PARISC_DIR21L: 592 case R_PARISC_PCREL21L: 593 r_field = e_lrsel; 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: 599 r_field = e_rrsel; 600 goto do_basic_type_1; 601 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 606 %r0. */ 607 case R_PARISC_DPREL21L: 608 r_field = e_lrsel; 609 if (sym_sec->flags & SEC_CODE) 610 { 611 if ((insn & 0xfc000000) >> 26 == 0xa 612 && (insn & 0x03e00000) >> 21 == 0x1b) 613 insn &= ~0x03e00000; 614 } 615 else 616 value -= elf32_hppa_hash_table (info)->global_value; 617 goto do_basic_type_1; 618 case R_PARISC_DPREL14R: 619 r_field = e_rrsel; 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: 624 r_field = e_fsel; 625 if ((sym_sec->flags & SEC_CODE) == 0) 626 value -= elf32_hppa_hash_table (info)->global_value; 627 goto do_basic_type_1; 628 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: 635 { 636 bfd_vma location; 637 unsigned int len; 638 char *new_name, *stub_name; 639 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) 643 r_field = e_fsel; 644 else if (r_type == R_PARISC_PLABEL21L) 645 r_field = e_lrsel; 646 else if (r_type == R_PARISC_PLABEL14R) 647 r_field = e_rrsel; 648 649 /* Find out where we are and where we're going. */ 650 location = (offset + 651 input_section->output_offset + 652 input_section->output_section->vma); 653 654 len = strlen (sym_name) + 1; 655 if (is_local) 656 len += 9; 657 new_name = bfd_malloc (len); 658 if (!new_name) 659 return bfd_reloc_notsupported; 660 strcpy (new_name, sym_name); 661 662 /* Local symbols have unique IDs. */ 663 if (is_local) 664 sprintf (new_name + len - 10, "_%08x", (int)sym_sec); 665 666 /* Any kind of linker stub needed? */ 667 if (((int)(value - location) > 0x3ffff) 668 || ((int)(value - location) < (int)0xfffc0000)) 669 { 670 struct elf32_hppa_stub_hash_table *stub_hash_table; 671 struct elf32_hppa_stub_hash_entry *stub_hash; 672 asection *stub_section; 673 674 /* Build a name for the stub. */ 675 676 len = strlen (new_name); 677 len += 23; 678 stub_name = bfd_malloc (len); 679 if (!stub_name) 680 return bfd_reloc_notsupported; 681 elf32_hppa_name_of_stub (location, value, stub_name); 682 strcat (stub_name, new_name); 683 free (new_name); 684 685 stub_hash_table = elf32_hppa_hash_table (info)->stub_hash_table; 686 687 stub_hash 688 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name, 689 false, false); 690 691 /* We're done with that name. */ 692 free (stub_name); 693 694 /* The stub BFD only has one section. */ 695 stub_section = stub_hash_table->stub_bfd->sections; 696 697 if (stub_hash != NULL) 698 { 699 if (r_type == R_PARISC_PCREL17F) 700 { 701 unsigned long delay_insn; 702 unsigned int opcode, rtn_reg, ldo_target_reg, ldo_src_reg; 703 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); 707 708 /* We also need to know the return register for this 709 call. */ 710 rtn_reg = (insn & 0x03e00000) >> 21; 711 712 ldo_src_reg = (delay_insn & 0x03e00000) >> 21; 713 ldo_target_reg = (delay_insn & 0x001f0000) >> 16; 714 715 /* Munge up the value and other parameters for 716 hppa_elf_relocate_insn. */ 717 718 value = (stub_hash->offset 719 + stub_section->output_offset 720 + stub_section->output_section->vma); 721 722 r_format = 17; 723 r_field = e_fsel; 724 r_pcrel = 0; 725 addend = 0; 726 727 /* We need to peek at the delay insn and determine if 728 we'll need to swap the branch and its delay insn. */ 729 if ((insn & 2) 730 || (opcode == LDO 731 && ldo_target_reg == rtn_reg) 732 || (delay_insn == 0x08000240)) 733 { 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. */ 737 value += 4; 738 739 /* If the delay insn does a return pointer adjustment, 740 then we have to make sure it stays valid. */ 741 if (opcode == LDO 742 && ldo_target_reg == rtn_reg) 743 { 744 delay_insn &= 0xfc00ffff; 745 delay_insn |= ((31 << 21) | (31 << 16)); 746 bfd_put_32 (input_bfd, delay_insn, hit_data + 4); 747 } 748 /* Use a BLE to reach the stub. */ 749 insn = BLE_SR4_R0; 750 } 751 else 752 { 753 /* Wonderful, we have to swap the call insn and its 754 delay slot. */ 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, 760 input_section, 761 insn, offset + 4, 762 value, addend, 763 r_format, r_field, 764 r_pcrel); 765 /* Update the instruction word. */ 766 bfd_put_32 (input_bfd, insn, hit_data + 4); 767 return bfd_reloc_dangerous; 768 } 769 } 770 else 771 return bfd_reloc_notsupported; 772 } 773 } 774 goto do_basic_type_1; 775 } 776 777 do_basic_type_1: 778 insn = hppa_elf_relocate_insn (input_bfd, input_section, insn, 779 offset, value, addend, r_format, 780 r_field, r_pcrel); 781 break; 782 783 /* Something we don't know how to handle. */ 784 default: 785 return bfd_reloc_notsupported; 786 } 787 788 /* Update the instruction word. */ 789 bfd_put_32 (input_bfd, insn, hit_data); 790 return (bfd_reloc_ok); 791 } 792 793 /* Undo the generic ELF code's subtraction of section->vma from the 794 value of each external symbol. */ 795 796 static boolean 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; 803 asection **secp; 804 bfd_vma *valp; 805 { 806 *valp += (*secp)->vma; 807 return true; 808 } 809 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. */ 813 814 static void 815 elf32_hppa_name_of_stub (location, destination, stub_name) 816 bfd_vma location ATTRIBUTE_UNUSED; 817 bfd_vma destination ATTRIBUTE_UNUSED; 818 char *stub_name; 819 { 820 strcpy (stub_name, "_____long_branch_stub_"); 821 } 822 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. */ 826 827 static unsigned int 828 elf32_hppa_size_of_stub (location, destination, sym_name) 829 bfd_vma location, destination; 830 const char *sym_name; 831 { 832 /* Determine if a long branch stub is needed. */ 833 if (!(((int)(location - destination) > 0x3ffff) 834 || ((int)(location - destination) < (int)0xfffc0000))) 835 return 0; 836 837 if (!strncmp ("$$", sym_name, 2) 838 && strcmp ("$$dyncall", sym_name)) 839 return 12; 840 else 841 return 16; 842 } 843 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. */ 846 847 static boolean 848 elf32_hppa_build_one_stub (gen_entry, in_args) 849 struct bfd_hash_entry *gen_entry; 850 PTR in_args; 851 { 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; 857 bfd_byte *loc; 858 symvalue sym_value; 859 const char *sym_name; 860 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 863 instructions. */ 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; 867 868 /* Make a note of the offset within the stubs for this entry. */ 869 entry->offset = stub_hash_table->offset; 870 871 /* The symbol's name starts at offset 22. */ 872 sym_name = entry->root.string + 22; 873 874 sym_value = (entry->target_value 875 + entry->target_section->output_offset 876 + entry->target_section->output_section->vma); 877 878 if (1) 879 { 880 /* Create one of two variant long branch stubs. One for $$dyncall and 881 normal calls, the other for calls to millicode. */ 882 unsigned long insn; 883 int millicode_call = 0; 884 885 if (!strncmp ("$$", sym_name, 2) && strcmp ("$$dyncall", sym_name)) 886 millicode_call = 1; 887 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); 891 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, 901 17); 902 bfd_put_32 (stub_bfd, insn, loc + 8); 903 904 if (!millicode_call) 905 { 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); 910 911 /* Update the location and offsets. */ 912 stub_hash_table->location += 16; 913 stub_hash_table->offset += 16; 914 } 915 else 916 { 917 /* Update the location and offsets. */ 918 stub_hash_table->location += 12; 919 stub_hash_table->offset += 12; 920 } 921 922 } 923 return true; 924 } 925 926 /* External entry points for sizing and building linker stubs. */ 927 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. */ 931 932 boolean 933 elf32_hppa_build_stubs (stub_bfd, info) 934 bfd *stub_bfd; 935 struct bfd_link_info *info; 936 { 937 /* The stub BFD only has one section. */ 938 asection *stub_sec = stub_bfd->sections; 939 struct elf32_hppa_stub_hash_table *table; 940 unsigned int size; 941 void *args[2]; 942 943 /* So we can pass both the BFD for the stubs and the link info 944 structure to the routine which actually builds stubs. */ 945 args[0] = stub_bfd; 946 args[1] = info; 947 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) 952 return false; 953 table = elf32_hppa_hash_table(info)->stub_hash_table; 954 table->location = stub_sec->contents; 955 956 /* Build the stubs as directed by the stub hash table. */ 957 elf32_hppa_stub_hash_traverse (table, elf32_hppa_build_one_stub, args); 958 959 return true; 960 } 961 962 /* Determine and set the size of the stub section for a final link. 963 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. */ 968 969 boolean 970 elf32_hppa_size_stubs (stub_bfd, output_bfd, link_info) 971 bfd *stub_bfd; 972 bfd *output_bfd ATTRIBUTE_UNUSED; 973 struct bfd_link_info *link_info; 974 { 975 bfd *input_bfd; 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; 981 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) 986 goto error_return; 987 988 if (!elf32_hppa_stub_hash_table_init (stub_hash_table, stub_bfd, 989 elf32_hppa_stub_hash_newfunc)) 990 goto error_return; 991 992 /* Attach the hash tables to the main hash table. */ 993 elf32_hppa_hash_table(link_info)->stub_hash_table = stub_hash_table; 994 995 /* Count the number of input BFDs. */ 996 for (input_bfd = link_info->input_bfds; 997 input_bfd != NULL; 998 input_bfd = input_bfd->link_next) 999 bfd_count++; 1000 1001 /* Magic as we know the stub bfd only has one section. */ 1002 stub_sec = stub_bfd->sections; 1003 1004 /* If generating a relocateable output file, then we don't 1005 have to examine the relocs. */ 1006 if (link_info->relocateable) 1007 { 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); 1012 return true; 1013 } 1014 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; 1018 input_bfd != NULL; 1019 input_bfd = input_bfd->link_next, index++) 1020 { 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) 1024 continue; 1025 1026 local_syms = all_local_syms[index]; 1027 1028 /* Walk over each section attached to the input bfd. */ 1029 for (section = input_bfd->sections; 1030 section != NULL; 1031 section = section->next) 1032 { 1033 Elf_Internal_Shdr *input_rel_hdr; 1034 Elf32_External_Rela *external_relocs, *erelaend, *erela; 1035 Elf_Internal_Rela *internal_relocs, *irelaend, *irela; 1036 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) 1040 continue; 1041 1042 /* Allocate space for the external relocations. */ 1043 external_relocs 1044 = ((Elf32_External_Rela *) 1045 bfd_malloc (section->reloc_count 1046 * sizeof (Elf32_External_Rela))); 1047 if (external_relocs == NULL) 1048 { 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); 1053 goto error_return; 1054 } 1055 1056 /* Likewise for the internal relocations. */ 1057 internal_relocs 1058 = ((Elf_Internal_Rela *) 1059 bfd_malloc (section->reloc_count * sizeof (Elf_Internal_Rela))); 1060 if (internal_relocs == NULL) 1061 { 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); 1067 goto error_return; 1068 } 1069 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) 1075 { 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); 1082 goto error_return; 1083 } 1084 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); 1091 1092 /* We're done with the external relocs, free them. */ 1093 free (external_relocs); 1094 1095 /* Now examine each relocation. */ 1096 irela = internal_relocs; 1097 irelaend = irela + section->reloc_count; 1098 for (; irela < irelaend; irela++) 1099 { 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; 1105 asection *sym_sec; 1106 const char *sym_name; 1107 symvalue sym_value; 1108 bfd_vma location, destination; 1109 char *new_name = NULL; 1110 1111 r_type = ELF32_R_TYPE (irela->r_info); 1112 r_index = ELF32_R_SYM (irela->r_info); 1113 1114 if (r_type < 0 || r_type >= (int) R_PARISC_UNIMPLEMENTED) 1115 { 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); 1122 goto error_return; 1123 } 1124 1125 /* Only look for stubs on call instructions or plabel 1126 references. */ 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) 1131 continue; 1132 1133 /* Now determine the call target, its name, value, section 1134 and argument relocation bits. */ 1135 hash = NULL; 1136 sym = NULL; 1137 sym_sec = NULL; 1138 if (r_index < symtab_hdr->sh_info) 1139 { 1140 /* It's a local symbol. */ 1141 Elf_Internal_Shdr *hdr; 1142 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, 1148 sym->st_name); 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); 1154 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); 1158 if (new_name == 0) 1159 { 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); 1165 goto error_return; 1166 } 1167 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec); 1168 sym_name = new_name; 1169 } 1170 else 1171 { 1172 /* It's an external symbol. */ 1173 long index; 1174 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) 1179 { 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); 1186 } 1187 else 1188 { 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); 1195 goto error_return; 1196 } 1197 } 1198 1199 /* Now determine where the call point is. */ 1200 location = (section->output_offset 1201 + section->output_section->vma 1202 + irela->r_offset); 1203 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; 1208 1209 /* Determine what (if any) linker stub is needed and its 1210 size (in bytes). */ 1211 size_of_stub = elf32_hppa_size_of_stub (location, 1212 destination, 1213 sym_name); 1214 if (size_of_stub != 0) 1215 { 1216 char *stub_name; 1217 unsigned int len; 1218 1219 /* Get the name of this stub. */ 1220 len = strlen (sym_name); 1221 len += 23; 1222 1223 stub_name = bfd_malloc (len); 1224 if (!stub_name) 1225 { 1226 /* Because sym_name was mallocd above for local 1227 symbols. */ 1228 if (r_index < symtab_hdr->sh_info) 1229 free (new_name); 1230 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); 1236 goto error_return; 1237 } 1238 elf32_hppa_name_of_stub (location, destination, stub_name); 1239 strcat (stub_name + 22, sym_name); 1240 1241 /* Because sym_name was malloced above for local symbols. */ 1242 if (r_index < symtab_hdr->sh_info) 1243 free (new_name); 1244 1245 stub_hash 1246 = elf32_hppa_stub_hash_lookup (stub_hash_table, stub_name, 1247 false, false); 1248 if (stub_hash != NULL) 1249 { 1250 /* The proper stub has already been created, nothing 1251 else to do. */ 1252 free (stub_name); 1253 } 1254 else 1255 { 1256 bfd_set_section_size (stub_bfd, stub_sec, 1257 (bfd_section_size (stub_bfd, 1258 stub_sec) 1259 + size_of_stub)); 1260 1261 /* Enter this entry into the linker stub hash table. */ 1262 stub_hash 1263 = elf32_hppa_stub_hash_lookup (stub_hash_table, 1264 stub_name, true, true); 1265 if (stub_hash == NULL) 1266 { 1267 free (stub_name); 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); 1273 goto error_return; 1274 } 1275 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; 1280 } 1281 free (stub_name); 1282 } 1283 } 1284 /* We're done with the internal relocs, free them. */ 1285 free (internal_relocs); 1286 } 1287 } 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); 1293 return true; 1294 1295 error_return: 1296 /* Return gracefully, avoiding dangling references to the hash tables. */ 1297 if (stub_hash_table) 1298 { 1299 elf32_hppa_hash_table(link_info)->stub_hash_table = NULL; 1300 free (stub_hash_table); 1301 } 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 1304 too. */ 1305 bfd_set_section_size (stub_bfd, stub_sec, 0); 1306 return false; 1307 } 1308 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 1314 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 1321 1322 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 1328 1329 #include "elf32-target.h" 1330