1 /* BFD back-end for Renesas H8/300 ELF binaries. 2 Copyright (C) 1993-2016 Free Software Foundation, Inc. 3 4 This file is part of BFD, the Binary File Descriptor library. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 MA 02110-1301, USA. */ 20 21 #include "sysdep.h" 22 #include "bfd.h" 23 #include "libbfd.h" 24 #include "elf-bfd.h" 25 #include "elf/h8.h" 26 27 static reloc_howto_type *elf32_h8_reloc_type_lookup 28 (bfd *abfd, bfd_reloc_code_real_type code); 29 static void elf32_h8_info_to_howto 30 (bfd *, arelent *, Elf_Internal_Rela *); 31 static void elf32_h8_info_to_howto_rel 32 (bfd *, arelent *, Elf_Internal_Rela *); 33 static unsigned long elf32_h8_mach (flagword); 34 static void elf32_h8_final_write_processing (bfd *, bfd_boolean); 35 static bfd_boolean elf32_h8_object_p (bfd *); 36 static bfd_boolean elf32_h8_merge_private_bfd_data (bfd *, bfd *); 37 static bfd_boolean elf32_h8_relax_section 38 (bfd *, asection *, struct bfd_link_info *, bfd_boolean *); 39 static bfd_boolean elf32_h8_relax_delete_bytes 40 (bfd *, asection *, bfd_vma, int); 41 static bfd_boolean elf32_h8_symbol_address_p (bfd *, asection *, bfd_vma); 42 static bfd_byte *elf32_h8_get_relocated_section_contents 43 (bfd *, struct bfd_link_info *, struct bfd_link_order *, 44 bfd_byte *, bfd_boolean, asymbol **); 45 static bfd_reloc_status_type elf32_h8_final_link_relocate 46 (unsigned long, bfd *, bfd *, asection *, 47 bfd_byte *, bfd_vma, bfd_vma, bfd_vma, 48 struct bfd_link_info *, asection *, int); 49 static bfd_boolean elf32_h8_relocate_section 50 (bfd *, struct bfd_link_info *, bfd *, asection *, 51 bfd_byte *, Elf_Internal_Rela *, 52 Elf_Internal_Sym *, asection **); 53 static bfd_reloc_status_type special 54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); 55 56 /* This does not include any relocation information, but should be 57 good enough for GDB or objdump to read the file. */ 58 59 static reloc_howto_type h8_elf_howto_table[] = 60 { 61 #define R_H8_NONE_X 0 62 HOWTO (R_H8_NONE, /* type */ 63 0, /* rightshift */ 64 3, /* size (0 = byte, 1 = short, 2 = long) */ 65 0, /* bitsize */ 66 FALSE, /* pc_relative */ 67 0, /* bitpos */ 68 complain_overflow_dont,/* complain_on_overflow */ 69 special, /* special_function */ 70 "R_H8_NONE", /* name */ 71 FALSE, /* partial_inplace */ 72 0, /* src_mask */ 73 0, /* dst_mask */ 74 FALSE), /* pcrel_offset */ 75 #define R_H8_DIR32_X (R_H8_NONE_X + 1) 76 HOWTO (R_H8_DIR32, /* type */ 77 0, /* rightshift */ 78 2, /* size (0 = byte, 1 = short, 2 = long) */ 79 32, /* bitsize */ 80 FALSE, /* pc_relative */ 81 0, /* bitpos */ 82 complain_overflow_dont,/* complain_on_overflow */ 83 special, /* special_function */ 84 "R_H8_DIR32", /* name */ 85 FALSE, /* partial_inplace */ 86 0, /* src_mask */ 87 0xffffffff, /* dst_mask */ 88 FALSE), /* pcrel_offset */ 89 #define R_H8_DIR16_X (R_H8_DIR32_X + 1) 90 HOWTO (R_H8_DIR16, /* type */ 91 0, /* rightshift */ 92 1, /* size (0 = byte, 1 = short, 2 = long) */ 93 16, /* bitsize */ 94 FALSE, /* pc_relative */ 95 0, /* bitpos */ 96 complain_overflow_dont,/* complain_on_overflow */ 97 special, /* special_function */ 98 "R_H8_DIR16", /* name */ 99 FALSE, /* partial_inplace */ 100 0, /* src_mask */ 101 0x0000ffff, /* dst_mask */ 102 FALSE), /* pcrel_offset */ 103 #define R_H8_DIR8_X (R_H8_DIR16_X + 1) 104 HOWTO (R_H8_DIR8, /* type */ 105 0, /* rightshift */ 106 0, /* size (0 = byte, 1 = short, 2 = long) */ 107 8, /* bitsize */ 108 FALSE, /* pc_relative */ 109 0, /* bitpos */ 110 complain_overflow_dont,/* complain_on_overflow */ 111 special, /* special_function */ 112 "R_H8_DIR8", /* name */ 113 FALSE, /* partial_inplace */ 114 0, /* src_mask */ 115 0x000000ff, /* dst_mask */ 116 FALSE), /* pcrel_offset */ 117 #define R_H8_DIR16A8_X (R_H8_DIR8_X + 1) 118 HOWTO (R_H8_DIR16A8, /* type */ 119 0, /* rightshift */ 120 1, /* size (0 = byte, 1 = short, 2 = long) */ 121 16, /* bitsize */ 122 FALSE, /* pc_relative */ 123 0, /* bitpos */ 124 complain_overflow_bitfield, /* complain_on_overflow */ 125 special, /* special_function */ 126 "R_H8_DIR16A8", /* name */ 127 FALSE, /* partial_inplace */ 128 0, /* src_mask */ 129 0x0000ffff, /* dst_mask */ 130 FALSE), /* pcrel_offset */ 131 #define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1) 132 HOWTO (R_H8_DIR16R8, /* type */ 133 0, /* rightshift */ 134 1, /* size (0 = byte, 1 = short, 2 = long) */ 135 16, /* bitsize */ 136 FALSE, /* pc_relative */ 137 0, /* bitpos */ 138 complain_overflow_bitfield, /* complain_on_overflow */ 139 special, /* special_function */ 140 "R_H8_DIR16R8", /* name */ 141 FALSE, /* partial_inplace */ 142 0, /* src_mask */ 143 0x0000ffff, /* dst_mask */ 144 FALSE), /* pcrel_offset */ 145 #define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1) 146 HOWTO (R_H8_DIR24A8, /* type */ 147 0, /* rightshift */ 148 2, /* size (0 = byte, 1 = short, 2 = long) */ 149 24, /* bitsize */ 150 FALSE, /* pc_relative */ 151 0, /* bitpos */ 152 complain_overflow_bitfield, /* complain_on_overflow */ 153 special, /* special_function */ 154 "R_H8_DIR24A8", /* name */ 155 TRUE, /* partial_inplace */ 156 0xff000000, /* src_mask */ 157 0x00ffffff, /* dst_mask */ 158 FALSE), /* pcrel_offset */ 159 #define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1) 160 HOWTO (R_H8_DIR24R8, /* type */ 161 0, /* rightshift */ 162 2, /* size (0 = byte, 1 = short, 2 = long) */ 163 24, /* bitsize */ 164 FALSE, /* pc_relative */ 165 0, /* bitpos */ 166 complain_overflow_bitfield, /* complain_on_overflow */ 167 special, /* special_function */ 168 "R_H8_DIR24R8", /* name */ 169 TRUE, /* partial_inplace */ 170 0xff000000, /* src_mask */ 171 0x00ffffff, /* dst_mask */ 172 FALSE), /* pcrel_offset */ 173 #define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1) 174 HOWTO (R_H8_DIR32A16, /* type */ 175 0, /* rightshift */ 176 2, /* size (0 = byte, 1 = short, 2 = long) */ 177 32, /* bitsize */ 178 FALSE, /* pc_relative */ 179 0, /* bitpos */ 180 complain_overflow_dont,/* complain_on_overflow */ 181 special, /* special_function */ 182 "R_H8_DIR32A16", /* name */ 183 FALSE, /* partial_inplace */ 184 0, /* src_mask */ 185 0xffffffff, /* dst_mask */ 186 FALSE), /* pcrel_offset */ 187 #define R_H8_DISP32A16_X (R_H8_DIR32A16_X + 1) 188 HOWTO (R_H8_DISP32A16, /* type */ 189 0, /* rightshift */ 190 2, /* size (0 = byte, 1 = short, 2 = long) */ 191 32, /* bitsize */ 192 FALSE, /* pc_relative */ 193 0, /* bitpos */ 194 complain_overflow_dont,/* complain_on_overflow */ 195 special, /* special_function */ 196 "R_H8_DISP32A16", /* name */ 197 FALSE, /* partial_inplace */ 198 0, /* src_mask */ 199 0xffffffff, /* dst_mask */ 200 FALSE), /* pcrel_offset */ 201 #define R_H8_PCREL16_X (R_H8_DISP32A16_X + 1) 202 HOWTO (R_H8_PCREL16, /* type */ 203 0, /* rightshift */ 204 1, /* size (0 = byte, 1 = short, 2 = long) */ 205 16, /* bitsize */ 206 TRUE, /* pc_relative */ 207 0, /* bitpos */ 208 complain_overflow_signed,/* complain_on_overflow */ 209 special, /* special_function */ 210 "R_H8_PCREL16", /* name */ 211 FALSE, /* partial_inplace */ 212 0xffff, /* src_mask */ 213 0xffff, /* dst_mask */ 214 TRUE), /* pcrel_offset */ 215 #define R_H8_PCREL8_X (R_H8_PCREL16_X + 1) 216 HOWTO (R_H8_PCREL8, /* type */ 217 0, /* rightshift */ 218 0, /* size (0 = byte, 1 = short, 2 = long) */ 219 8, /* bitsize */ 220 TRUE, /* pc_relative */ 221 0, /* bitpos */ 222 complain_overflow_signed,/* complain_on_overflow */ 223 special, /* special_function */ 224 "R_H8_PCREL8", /* name */ 225 FALSE, /* partial_inplace */ 226 0xff, /* src_mask */ 227 0xff, /* dst_mask */ 228 TRUE), /* pcrel_offset */ 229 }; 230 231 /* This structure is used to map BFD reloc codes to H8 ELF relocs. */ 232 233 struct elf_reloc_map { 234 bfd_reloc_code_real_type bfd_reloc_val; 235 unsigned char howto_index; 236 }; 237 238 /* An array mapping BFD reloc codes to H8 ELF relocs. */ 239 240 static const struct elf_reloc_map h8_reloc_map[] = { 241 { BFD_RELOC_NONE, R_H8_NONE_X }, 242 { BFD_RELOC_32, R_H8_DIR32_X }, 243 { BFD_RELOC_16, R_H8_DIR16_X }, 244 { BFD_RELOC_8, R_H8_DIR8_X }, 245 { BFD_RELOC_H8_DIR16A8, R_H8_DIR16A8_X }, 246 { BFD_RELOC_H8_DIR16R8, R_H8_DIR16R8_X }, 247 { BFD_RELOC_H8_DIR24A8, R_H8_DIR24A8_X }, 248 { BFD_RELOC_H8_DIR24R8, R_H8_DIR24R8_X }, 249 { BFD_RELOC_H8_DIR32A16, R_H8_DIR32A16_X }, 250 { BFD_RELOC_H8_DISP32A16, R_H8_DISP32A16_X }, 251 { BFD_RELOC_16_PCREL, R_H8_PCREL16_X }, 252 { BFD_RELOC_8_PCREL, R_H8_PCREL8_X }, 253 }; 254 255 256 static reloc_howto_type * 257 elf32_h8_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, 258 bfd_reloc_code_real_type code) 259 { 260 unsigned int i; 261 262 for (i = 0; i < sizeof (h8_reloc_map) / sizeof (struct elf_reloc_map); i++) 263 { 264 if (h8_reloc_map[i].bfd_reloc_val == code) 265 return &h8_elf_howto_table[(int) h8_reloc_map[i].howto_index]; 266 } 267 return NULL; 268 } 269 270 static reloc_howto_type * 271 elf32_h8_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 272 const char *r_name) 273 { 274 unsigned int i; 275 276 for (i = 0; 277 i < sizeof (h8_elf_howto_table) / sizeof (h8_elf_howto_table[0]); 278 i++) 279 if (h8_elf_howto_table[i].name != NULL 280 && strcasecmp (h8_elf_howto_table[i].name, r_name) == 0) 281 return &h8_elf_howto_table[i]; 282 283 return NULL; 284 } 285 286 static void 287 elf32_h8_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc, 288 Elf_Internal_Rela *elf_reloc) 289 { 290 unsigned int r; 291 unsigned int i; 292 293 r = ELF32_R_TYPE (elf_reloc->r_info); 294 for (i = 0; i < sizeof (h8_elf_howto_table) / sizeof (reloc_howto_type); i++) 295 if (h8_elf_howto_table[i].type == r) 296 { 297 bfd_reloc->howto = &h8_elf_howto_table[i]; 298 return; 299 } 300 abort (); 301 } 302 303 static void 304 elf32_h8_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc, 305 Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED) 306 { 307 unsigned int r; 308 309 abort (); 310 r = ELF32_R_TYPE (elf_reloc->r_info); 311 bfd_reloc->howto = &h8_elf_howto_table[r]; 312 } 313 314 /* Special handling for H8/300 relocs. 315 We only come here for pcrel stuff and return normally if not an -r link. 316 When doing -r, we can't do any arithmetic for the pcrel stuff, because 317 we support relaxing on the H8/300 series chips. */ 318 static bfd_reloc_status_type 319 special (bfd *abfd ATTRIBUTE_UNUSED, 320 arelent *reloc_entry ATTRIBUTE_UNUSED, 321 asymbol *symbol ATTRIBUTE_UNUSED, 322 void * data ATTRIBUTE_UNUSED, 323 asection *input_section ATTRIBUTE_UNUSED, 324 bfd *output_bfd, 325 char **error_message ATTRIBUTE_UNUSED) 326 { 327 if (output_bfd == (bfd *) NULL) 328 return bfd_reloc_continue; 329 330 /* Adjust the reloc address to that in the output section. */ 331 reloc_entry->address += input_section->output_offset; 332 return bfd_reloc_ok; 333 } 334 335 /* Perform a relocation as part of a final link. */ 336 static bfd_reloc_status_type 337 elf32_h8_final_link_relocate (unsigned long r_type, bfd *input_bfd, 338 bfd *output_bfd ATTRIBUTE_UNUSED, 339 asection *input_section ATTRIBUTE_UNUSED, 340 bfd_byte *contents, bfd_vma offset, 341 bfd_vma value, bfd_vma addend, 342 struct bfd_link_info *info ATTRIBUTE_UNUSED, 343 asection *sym_sec ATTRIBUTE_UNUSED, 344 int is_local ATTRIBUTE_UNUSED) 345 { 346 bfd_byte *hit_data = contents + offset; 347 348 switch (r_type) 349 { 350 case R_H8_NONE: 351 return bfd_reloc_ok; 352 353 case R_H8_DIR32: 354 case R_H8_DIR32A16: 355 case R_H8_DISP32A16: 356 case R_H8_DIR24A8: 357 value += addend; 358 bfd_put_32 (input_bfd, value, hit_data); 359 return bfd_reloc_ok; 360 361 case R_H8_DIR16: 362 case R_H8_DIR16A8: 363 case R_H8_DIR16R8: 364 value += addend; 365 bfd_put_16 (input_bfd, value, hit_data); 366 return bfd_reloc_ok; 367 368 /* AKA R_RELBYTE */ 369 case R_H8_DIR8: 370 value += addend; 371 372 bfd_put_8 (input_bfd, value, hit_data); 373 return bfd_reloc_ok; 374 375 case R_H8_DIR24R8: 376 value += addend; 377 378 /* HIT_DATA is the address for the first byte for the relocated 379 value. Subtract 1 so that we can manipulate the data in 32-bit 380 hunks. */ 381 hit_data--; 382 383 /* Clear out the top byte in value. */ 384 value &= 0xffffff; 385 386 /* Retrieve the type byte for value from the section contents. */ 387 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000); 388 389 /* Now scribble it out in one 32-bit hunk. */ 390 bfd_put_32 (input_bfd, value, hit_data); 391 return bfd_reloc_ok; 392 393 case R_H8_PCREL16: 394 value -= (input_section->output_section->vma 395 + input_section->output_offset); 396 value -= offset; 397 value += addend; 398 399 /* The value is relative to the start of the instruction, 400 not the relocation offset. Subtract 2 to account for 401 this minor issue. */ 402 value -= 2; 403 404 bfd_put_16 (input_bfd, value, hit_data); 405 return bfd_reloc_ok; 406 407 case R_H8_PCREL8: 408 value -= (input_section->output_section->vma 409 + input_section->output_offset); 410 value -= offset; 411 value += addend; 412 413 /* The value is relative to the start of the instruction, 414 not the relocation offset. Subtract 1 to account for 415 this minor issue. */ 416 value -= 1; 417 418 bfd_put_8 (input_bfd, value, hit_data); 419 return bfd_reloc_ok; 420 421 default: 422 return bfd_reloc_notsupported; 423 } 424 } 425 426 /* Relocate an H8 ELF section. */ 428 static bfd_boolean 429 elf32_h8_relocate_section (bfd *output_bfd, struct bfd_link_info *info, 430 bfd *input_bfd, asection *input_section, 431 bfd_byte *contents, Elf_Internal_Rela *relocs, 432 Elf_Internal_Sym *local_syms, 433 asection **local_sections) 434 { 435 Elf_Internal_Shdr *symtab_hdr; 436 struct elf_link_hash_entry **sym_hashes; 437 Elf_Internal_Rela *rel, *relend; 438 439 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 440 sym_hashes = elf_sym_hashes (input_bfd); 441 442 rel = relocs; 443 relend = relocs + input_section->reloc_count; 444 for (; rel < relend; rel++) 445 { 446 unsigned int r_type; 447 unsigned long r_symndx; 448 Elf_Internal_Sym *sym; 449 asection *sec; 450 struct elf_link_hash_entry *h; 451 bfd_vma relocation; 452 bfd_reloc_status_type r; 453 arelent bfd_reloc; 454 reloc_howto_type *howto; 455 456 elf32_h8_info_to_howto (input_bfd, &bfd_reloc, rel); 457 howto = bfd_reloc.howto; 458 459 r_symndx = ELF32_R_SYM (rel->r_info); 460 r_type = ELF32_R_TYPE (rel->r_info); 461 h = NULL; 462 sym = NULL; 463 sec = NULL; 464 if (r_symndx < symtab_hdr->sh_info) 465 { 466 sym = local_syms + r_symndx; 467 sec = local_sections[r_symndx]; 468 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 469 } 470 else 471 { 472 bfd_boolean unresolved_reloc, warned, ignored; 473 474 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 475 r_symndx, symtab_hdr, sym_hashes, 476 h, sec, relocation, 477 unresolved_reloc, warned, ignored); 478 } 479 480 if (sec != NULL && discarded_section (sec)) 481 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, 482 rel, 1, relend, howto, 0, contents); 483 484 if (bfd_link_relocatable (info)) 485 continue; 486 487 r = elf32_h8_final_link_relocate (r_type, input_bfd, output_bfd, 488 input_section, 489 contents, rel->r_offset, 490 relocation, rel->r_addend, 491 info, sec, h == NULL); 492 493 if (r != bfd_reloc_ok) 494 { 495 const char *name; 496 const char *msg = (const char *) 0; 497 498 if (h != NULL) 499 name = h->root.root.string; 500 else 501 { 502 name = (bfd_elf_string_from_elf_section 503 (input_bfd, symtab_hdr->sh_link, sym->st_name)); 504 if (name == NULL || *name == '\0') 505 name = bfd_section_name (input_bfd, sec); 506 } 507 508 switch (r) 509 { 510 case bfd_reloc_overflow: 511 (*info->callbacks->reloc_overflow) 512 (info, (h ? &h->root : NULL), name, howto->name, 513 (bfd_vma) 0, input_bfd, input_section, rel->r_offset); 514 break; 515 516 case bfd_reloc_undefined: 517 (*info->callbacks->undefined_symbol) 518 (info, name, input_bfd, input_section, rel->r_offset, TRUE); 519 break; 520 521 case bfd_reloc_outofrange: 522 msg = _("internal error: out of range error"); 523 goto common_error; 524 525 case bfd_reloc_notsupported: 526 msg = _("internal error: unsupported relocation error"); 527 goto common_error; 528 529 case bfd_reloc_dangerous: 530 msg = _("internal error: dangerous error"); 531 goto common_error; 532 533 default: 534 msg = _("internal error: unknown error"); 535 /* fall through */ 536 537 common_error: 538 (*info->callbacks->warning) (info, msg, name, input_bfd, 539 input_section, rel->r_offset); 540 break; 541 } 542 } 543 } 544 545 return TRUE; 546 } 547 548 /* Object files encode the specific H8 model they were compiled 549 for in the ELF flags field. 550 551 Examine that field and return the proper BFD machine type for 552 the object file. */ 553 static unsigned long 554 elf32_h8_mach (flagword flags) 555 { 556 switch (flags & EF_H8_MACH) 557 { 558 case E_H8_MACH_H8300: 559 default: 560 return bfd_mach_h8300; 561 562 case E_H8_MACH_H8300H: 563 return bfd_mach_h8300h; 564 565 case E_H8_MACH_H8300S: 566 return bfd_mach_h8300s; 567 568 case E_H8_MACH_H8300HN: 569 return bfd_mach_h8300hn; 570 571 case E_H8_MACH_H8300SN: 572 return bfd_mach_h8300sn; 573 574 case E_H8_MACH_H8300SX: 575 return bfd_mach_h8300sx; 576 577 case E_H8_MACH_H8300SXN: 578 return bfd_mach_h8300sxn; 579 } 580 } 581 582 /* The final processing done just before writing out a H8 ELF object 583 file. We use this opportunity to encode the BFD machine type 584 into the flags field in the object file. */ 585 586 static void 587 elf32_h8_final_write_processing (bfd *abfd, 588 bfd_boolean linker ATTRIBUTE_UNUSED) 589 { 590 unsigned long val; 591 592 switch (bfd_get_mach (abfd)) 593 { 594 default: 595 case bfd_mach_h8300: 596 val = E_H8_MACH_H8300; 597 break; 598 599 case bfd_mach_h8300h: 600 val = E_H8_MACH_H8300H; 601 break; 602 603 case bfd_mach_h8300s: 604 val = E_H8_MACH_H8300S; 605 break; 606 607 case bfd_mach_h8300hn: 608 val = E_H8_MACH_H8300HN; 609 break; 610 611 case bfd_mach_h8300sn: 612 val = E_H8_MACH_H8300SN; 613 break; 614 615 case bfd_mach_h8300sx: 616 val = E_H8_MACH_H8300SX; 617 break; 618 619 case bfd_mach_h8300sxn: 620 val = E_H8_MACH_H8300SXN; 621 break; 622 } 623 624 elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH); 625 elf_elfheader (abfd)->e_flags |= val; 626 } 627 628 /* Return nonzero if ABFD represents a valid H8 ELF object file; also 629 record the encoded machine type found in the ELF flags. */ 630 631 static bfd_boolean 632 elf32_h8_object_p (bfd *abfd) 633 { 634 bfd_default_set_arch_mach (abfd, bfd_arch_h8300, 635 elf32_h8_mach (elf_elfheader (abfd)->e_flags)); 636 return TRUE; 637 } 638 639 /* Merge backend specific data from an object file to the output 640 object file when linking. The only data we need to copy at this 641 time is the architecture/machine information. */ 642 643 static bfd_boolean 644 elf32_h8_merge_private_bfd_data (bfd *ibfd, bfd *obfd) 645 { 646 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 647 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 648 return TRUE; 649 650 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) 651 && bfd_get_mach (obfd) < bfd_get_mach (ibfd)) 652 { 653 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), 654 bfd_get_mach (ibfd))) 655 return FALSE; 656 } 657 658 return TRUE; 659 } 660 661 /* This function handles relaxing for the H8.. 662 663 There are a few relaxing opportunities available on the H8: 664 665 jmp/jsr:24 -> bra/bsr:8 2 bytes 666 The jmp may be completely eliminated if the previous insn is a 667 conditional branch to the insn after the jump. In that case 668 we invert the branch and delete the jump and save 4 bytes. 669 670 bCC:16 -> bCC:8 2 bytes 671 bsr:16 -> bsr:8 2 bytes 672 673 bset:16 -> bset:8 2 bytes 674 bset:24/32 -> bset:8 4 bytes 675 (also applicable to other bit manipulation instructions) 676 677 mov.b:16 -> mov.b:8 2 bytes 678 mov.b:24/32 -> mov.b:8 4 bytes 679 680 bset:24/32 -> bset:16 2 bytes 681 (also applicable to other bit manipulation instructions) 682 683 mov.[bwl]:24/32 -> mov.[bwl]:16 2 bytes 684 685 mov.[bwl] @(displ:24/32+ERx) -> mov.[bwl] @(displ:16+ERx) 4 bytes. */ 686 687 static bfd_boolean 688 elf32_h8_relax_section (bfd *abfd, asection *sec, 689 struct bfd_link_info *link_info, bfd_boolean *again) 690 { 691 Elf_Internal_Shdr *symtab_hdr; 692 Elf_Internal_Rela *internal_relocs; 693 Elf_Internal_Rela *irel, *irelend; 694 bfd_byte *contents = NULL; 695 Elf_Internal_Sym *isymbuf = NULL; 696 static asection *last_input_section = NULL; 697 static Elf_Internal_Rela *last_reloc = NULL; 698 699 /* Assume nothing changes. */ 700 *again = FALSE; 701 702 /* We don't have to do anything for a relocatable link, if 703 this section does not have relocs, or if this is not a 704 code section. */ 705 if (bfd_link_relocatable (link_info) 706 || (sec->flags & SEC_RELOC) == 0 707 || sec->reloc_count == 0 708 || (sec->flags & SEC_CODE) == 0) 709 return TRUE; 710 711 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 712 713 /* Get a copy of the native relocations. */ 714 internal_relocs = (_bfd_elf_link_read_relocs 715 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, 716 link_info->keep_memory)); 717 if (internal_relocs == NULL) 718 goto error_return; 719 720 if (sec != last_input_section) 721 last_reloc = NULL; 722 723 last_input_section = sec; 724 725 /* Walk through the relocs looking for relaxing opportunities. */ 726 irelend = internal_relocs + sec->reloc_count; 727 for (irel = internal_relocs; irel < irelend; irel++) 728 { 729 bfd_vma symval; 730 731 { 732 arelent bfd_reloc; 733 734 elf32_h8_info_to_howto (abfd, &bfd_reloc, irel); 735 } 736 /* Keep track of the previous reloc so that we can delete 737 some long jumps created by the compiler. */ 738 if (irel != internal_relocs) 739 last_reloc = irel - 1; 740 741 switch(ELF32_R_TYPE (irel->r_info)) 742 { 743 case R_H8_DIR24R8: 744 case R_H8_PCREL16: 745 case R_H8_DIR16A8: 746 case R_H8_DIR24A8: 747 case R_H8_DIR32A16: 748 case R_H8_DISP32A16: 749 break; 750 default: 751 continue; 752 } 753 754 /* Get the section contents if we haven't done so already. */ 755 if (contents == NULL) 756 { 757 /* Get cached copy if it exists. */ 758 if (elf_section_data (sec)->this_hdr.contents != NULL) 759 contents = elf_section_data (sec)->this_hdr.contents; 760 else 761 { 762 /* Go get them off disk. */ 763 if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 764 goto error_return; 765 } 766 } 767 768 /* Read this BFD's local symbols if we haven't done so already. */ 769 if (isymbuf == NULL && symtab_hdr->sh_info != 0) 770 { 771 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 772 if (isymbuf == NULL) 773 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 774 symtab_hdr->sh_info, 0, 775 NULL, NULL, NULL); 776 if (isymbuf == NULL) 777 goto error_return; 778 } 779 780 /* Get the value of the symbol referred to by the reloc. */ 781 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) 782 { 783 /* A local symbol. */ 784 Elf_Internal_Sym *isym; 785 asection *sym_sec; 786 787 isym = isymbuf + ELF32_R_SYM (irel->r_info); 788 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 789 symval = isym->st_value; 790 /* If the reloc is absolute, it will not have 791 a symbol or section associated with it. */ 792 if (sym_sec) 793 symval += sym_sec->output_section->vma 794 + sym_sec->output_offset; 795 } 796 else 797 { 798 unsigned long indx; 799 struct elf_link_hash_entry *h; 800 801 /* An external symbol. */ 802 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; 803 h = elf_sym_hashes (abfd)[indx]; 804 BFD_ASSERT (h != NULL); 805 if (h->root.type != bfd_link_hash_defined 806 && h->root.type != bfd_link_hash_defweak) 807 { 808 /* This appears to be a reference to an undefined 809 symbol. Just ignore it--it will be caught by the 810 regular reloc processing. */ 811 continue; 812 } 813 814 symval = (h->root.u.def.value 815 + h->root.u.def.section->output_section->vma 816 + h->root.u.def.section->output_offset); 817 } 818 819 /* For simplicity of coding, we are going to modify the section 820 contents, the section relocs, and the BFD symbol table. We 821 must tell the rest of the code not to free up this 822 information. It would be possible to instead create a table 823 of changes which have to be made, as is done in coff-mips.c; 824 that would be more work, but would require less memory when 825 the linker is run. */ 826 switch (ELF32_R_TYPE (irel->r_info)) 827 { 828 /* Try to turn a 24-bit absolute branch/call into an 8-bit 829 pc-relative branch/call. */ 830 case R_H8_DIR24R8: 831 { 832 bfd_vma value = symval + irel->r_addend; 833 bfd_vma dot, gap; 834 835 /* Get the address of this instruction. */ 836 dot = (sec->output_section->vma 837 + sec->output_offset + irel->r_offset - 1); 838 839 /* Compute the distance from this insn to the branch target. */ 840 gap = value - dot; 841 842 /* If the distance is within -126..+130 inclusive, then we can 843 relax this jump. +130 is valid since the target will move 844 two bytes closer if we do relax this branch. */ 845 if ((int) gap >= -126 && (int) gap <= 130) 846 { 847 unsigned char code; 848 849 /* Note that we've changed the relocs, section contents, 850 etc. */ 851 elf_section_data (sec)->relocs = internal_relocs; 852 elf_section_data (sec)->this_hdr.contents = contents; 853 symtab_hdr->contents = (unsigned char *) isymbuf; 854 855 /* Get the instruction code being relaxed. */ 856 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 857 858 /* If the previous instruction conditionally jumped around 859 this instruction, we may be able to reverse the condition 860 and redirect the previous instruction to the target of 861 this instruction. 862 863 Such sequences are used by the compiler to deal with 864 long conditional branches. 865 866 Only perform this optimisation for jumps (code 0x5a) not 867 subroutine calls, as otherwise it could transform: 868 869 mov.w r0,r0 870 beq .L1 871 jsr @_bar 872 .L1: rts 873 _bar: rts 874 into: 875 mov.w r0,r0 876 bne _bar 877 rts 878 _bar: rts 879 880 which changes the call (jsr) into a branch (bne). */ 881 if (code == 0x5a /* jmp24. */ 882 && (int) gap <= 130 883 && (int) gap >= -128 884 && last_reloc 885 && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8 886 && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info) 887 { 888 bfd_vma last_value; 889 asection *last_sym_sec; 890 Elf_Internal_Sym *last_sym; 891 892 /* We will need to examine the symbol used by the 893 previous relocation. */ 894 895 last_sym = isymbuf + ELF32_R_SYM (last_reloc->r_info); 896 last_sym_sec 897 = bfd_section_from_elf_index (abfd, last_sym->st_shndx); 898 last_value = (last_sym->st_value 899 + last_sym_sec->output_section->vma 900 + last_sym_sec->output_offset); 901 902 /* Verify that the previous relocation was for a 903 branch around this instruction and that no symbol 904 exists at the current location. */ 905 if (last_value == dot + 4 906 && last_reloc->r_offset + 2 == irel->r_offset 907 && ! elf32_h8_symbol_address_p (abfd, sec, dot)) 908 { 909 /* We can eliminate this jump. Twiddle the 910 previous relocation as necessary. */ 911 irel->r_info 912 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 913 ELF32_R_TYPE (R_H8_NONE)); 914 915 last_reloc->r_info 916 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 917 ELF32_R_TYPE (R_H8_PCREL8)); 918 last_reloc->r_addend = irel->r_addend; 919 920 code = bfd_get_8 (abfd, 921 contents + last_reloc->r_offset - 1); 922 code ^= 1; 923 bfd_put_8 (abfd, 924 code, 925 contents + last_reloc->r_offset - 1); 926 927 /* Delete four bytes of data. */ 928 if (!elf32_h8_relax_delete_bytes (abfd, sec, 929 irel->r_offset - 1, 930 4)) 931 goto error_return; 932 933 *again = TRUE; 934 break; 935 } 936 } 937 938 if (code == 0x5e) 939 /* This is jsr24 */ 940 bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1); /* bsr8. */ 941 else if (code == 0x5a) 942 /* This is jmp24 */ 943 bfd_put_8 (abfd, 0x40, contents + irel->r_offset - 1); /* bra8. */ 944 else 945 abort (); 946 947 /* Fix the relocation's type. */ 948 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 949 R_H8_PCREL8); 950 951 /* Delete two bytes of data. */ 952 if (!elf32_h8_relax_delete_bytes (abfd, sec, 953 irel->r_offset + 1, 2)) 954 goto error_return; 955 956 /* That will change things, so, we should relax again. 957 Note that this is not required, and it may be slow. */ 958 *again = TRUE; 959 } 960 break; 961 } 962 963 /* Try to turn a 16-bit pc-relative branch into a 8-bit pc-relative 964 branch. */ 965 case R_H8_PCREL16: 966 { 967 bfd_vma value = symval + irel->r_addend; 968 bfd_vma dot; 969 bfd_vma gap; 970 971 /* Get the address of this instruction. */ 972 dot = (sec->output_section->vma 973 + sec->output_offset 974 + irel->r_offset - 2); 975 976 gap = value - dot; 977 978 /* If the distance is within -126..+130 inclusive, then we can 979 relax this jump. +130 is valid since the target will move 980 two bytes closer if we do relax this branch. */ 981 if ((int) gap >= -126 && (int) gap <= 130) 982 { 983 unsigned char code; 984 985 /* Note that we've changed the relocs, section contents, 986 etc. */ 987 elf_section_data (sec)->relocs = internal_relocs; 988 elf_section_data (sec)->this_hdr.contents = contents; 989 symtab_hdr->contents = (unsigned char *) isymbuf; 990 991 /* Get the opcode. */ 992 code = bfd_get_8 (abfd, contents + irel->r_offset - 2); 993 994 if (code == 0x58) 995 { 996 /* bCC:16 -> bCC:8 */ 997 /* Get the second byte of the original insn, which 998 contains the condition code. */ 999 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 1000 1001 /* Compute the first byte of the relaxed 1002 instruction. The original sequence 0x58 0xX0 1003 is relaxed to 0x4X, where X represents the 1004 condition code. */ 1005 code &= 0xf0; 1006 code >>= 4; 1007 code |= 0x40; 1008 bfd_put_8 (abfd, code, contents + irel->r_offset - 2); /* bCC:8. */ 1009 } 1010 else if (code == 0x5c) /* bsr16. */ 1011 /* This is bsr. */ 1012 bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2); /* bsr8. */ 1013 else 1014 /* Might be MOVSD. */ 1015 break; 1016 1017 /* Fix the relocation's type. */ 1018 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 1019 R_H8_PCREL8); 1020 irel->r_offset--; 1021 1022 /* Delete two bytes of data. */ 1023 if (!elf32_h8_relax_delete_bytes (abfd, sec, 1024 irel->r_offset + 1, 2)) 1025 goto error_return; 1026 1027 /* That will change things, so, we should relax again. 1028 Note that this is not required, and it may be slow. */ 1029 *again = TRUE; 1030 } 1031 break; 1032 } 1033 1034 /* This is a 16-bit absolute address in one of the following 1035 instructions: 1036 1037 "band", "bclr", "biand", "bild", "bior", "bist", "bixor", 1038 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and 1039 "mov.b" 1040 1041 We may relax this into an 8-bit absolute address if it's in 1042 the right range. */ 1043 case R_H8_DIR16A8: 1044 { 1045 bfd_vma value; 1046 1047 value = bfd_h8300_pad_address (abfd, symval + irel->r_addend); 1048 if (value >= 0xffffff00u) 1049 { 1050 unsigned char code; 1051 unsigned char temp_code; 1052 1053 /* Note that we've changed the relocs, section contents, 1054 etc. */ 1055 elf_section_data (sec)->relocs = internal_relocs; 1056 elf_section_data (sec)->this_hdr.contents = contents; 1057 symtab_hdr->contents = (unsigned char *) isymbuf; 1058 1059 /* Get the opcode. */ 1060 code = bfd_get_8 (abfd, contents + irel->r_offset - 2); 1061 1062 /* All instructions with R_H8_DIR16A8 start with 1063 0x6a. */ 1064 if (code != 0x6a) 1065 abort (); 1066 1067 temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 1068 /* If this is a mov.b instruction, clear the lower 1069 nibble, which contains the source/destination 1070 register number. */ 1071 if ((temp_code & 0x10) != 0x10) 1072 temp_code &= 0xf0; 1073 1074 switch (temp_code) 1075 { 1076 case 0x00: 1077 /* This is mov.b @aa:16,Rd. */ 1078 bfd_put_8 (abfd, (code & 0xf) | 0x20, 1079 contents + irel->r_offset - 2); 1080 break; 1081 case 0x80: 1082 /* This is mov.b Rs,@aa:16. */ 1083 bfd_put_8 (abfd, (code & 0xf) | 0x30, 1084 contents + irel->r_offset - 2); 1085 break; 1086 case 0x18: 1087 /* This is a bit-maniputation instruction that 1088 stores one bit into memory, one of "bclr", 1089 "bist", "bnot", "bset", and "bst". */ 1090 bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2); 1091 break; 1092 case 0x10: 1093 /* This is a bit-maniputation instruction that 1094 loads one bit from memory, one of "band", 1095 "biand", "bild", "bior", "bixor", "bld", "bor", 1096 "btst", and "bxor". */ 1097 bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2); 1098 break; 1099 default: 1100 abort (); 1101 } 1102 1103 /* Fix the relocation's type. */ 1104 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 1105 R_H8_DIR8); 1106 1107 /* Move the relocation. */ 1108 irel->r_offset--; 1109 1110 /* Delete two bytes of data. */ 1111 if (!elf32_h8_relax_delete_bytes (abfd, sec, 1112 irel->r_offset + 1, 2)) 1113 goto error_return; 1114 1115 /* That will change things, so, we should relax again. 1116 Note that this is not required, and it may be slow. */ 1117 *again = TRUE; 1118 } 1119 break; 1120 } 1121 1122 /* This is a 24-bit absolute address in one of the following 1123 instructions: 1124 1125 "band", "bclr", "biand", "bild", "bior", "bist", "bixor", 1126 "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and 1127 "mov.b" 1128 1129 We may relax this into an 8-bit absolute address if it's in 1130 the right range. */ 1131 case R_H8_DIR24A8: 1132 { 1133 bfd_vma value; 1134 1135 value = bfd_h8300_pad_address (abfd, symval + irel->r_addend); 1136 if (value >= 0xffffff00u) 1137 { 1138 unsigned char code; 1139 unsigned char temp_code; 1140 1141 /* Note that we've changed the relocs, section contents, 1142 etc. */ 1143 elf_section_data (sec)->relocs = internal_relocs; 1144 elf_section_data (sec)->this_hdr.contents = contents; 1145 symtab_hdr->contents = (unsigned char *) isymbuf; 1146 1147 /* Get the opcode. */ 1148 code = bfd_get_8 (abfd, contents + irel->r_offset - 2); 1149 1150 /* All instructions with R_H8_DIR24A8 start with 1151 0x6a. */ 1152 if (code != 0x6a) 1153 abort (); 1154 1155 temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 1156 1157 /* If this is a mov.b instruction, clear the lower 1158 nibble, which contains the source/destination 1159 register number. */ 1160 if ((temp_code & 0x30) != 0x30) 1161 temp_code &= 0xf0; 1162 1163 switch (temp_code) 1164 { 1165 case 0x20: 1166 /* This is mov.b @aa:24/32,Rd. */ 1167 bfd_put_8 (abfd, (code & 0xf) | 0x20, 1168 contents + irel->r_offset - 2); 1169 break; 1170 case 0xa0: 1171 /* This is mov.b Rs,@aa:24/32. */ 1172 bfd_put_8 (abfd, (code & 0xf) | 0x30, 1173 contents + irel->r_offset - 2); 1174 break; 1175 case 0x38: 1176 /* This is a bit-maniputation instruction that 1177 stores one bit into memory, one of "bclr", 1178 "bist", "bnot", "bset", and "bst". */ 1179 bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2); 1180 break; 1181 case 0x30: 1182 /* This is a bit-maniputation instruction that 1183 loads one bit from memory, one of "band", 1184 "biand", "bild", "bior", "bixor", "bld", "bor", 1185 "btst", and "bxor". */ 1186 bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2); 1187 break; 1188 default: 1189 abort(); 1190 } 1191 1192 /* Fix the relocation's type. */ 1193 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 1194 R_H8_DIR8); 1195 irel->r_offset--; 1196 1197 /* Delete four bytes of data. */ 1198 if (!elf32_h8_relax_delete_bytes (abfd, sec, 1199 irel->r_offset + 1, 4)) 1200 goto error_return; 1201 1202 /* That will change things, so, we should relax again. 1203 Note that this is not required, and it may be slow. */ 1204 *again = TRUE; 1205 break; 1206 } 1207 } 1208 1209 /* Fall through. */ 1210 1211 /* This is a 24-/32-bit absolute address in one of the 1212 following instructions: 1213 1214 "band", "bclr", "biand", "bild", "bior", "bist", 1215 "bixor", "bld", "bnot", "bor", "bset", "bst", "btst", 1216 "bxor", "ldc.w", "stc.w" and "mov.[bwl]" 1217 1218 We may relax this into an 16-bit absolute address if it's 1219 in the right range. */ 1220 case R_H8_DIR32A16: 1221 { 1222 bfd_vma value; 1223 1224 value = bfd_h8300_pad_address (abfd, symval + irel->r_addend); 1225 if (value <= 0x7fff || value >= 0xffff8000u) 1226 { 1227 unsigned char code; 1228 unsigned char op0, op1, op2, op3; 1229 unsigned char *op_ptr; 1230 1231 /* Note that we've changed the relocs, section contents, 1232 etc. */ 1233 elf_section_data (sec)->relocs = internal_relocs; 1234 elf_section_data (sec)->this_hdr.contents = contents; 1235 symtab_hdr->contents = (unsigned char *) isymbuf; 1236 1237 if (irel->r_offset >= 4) 1238 { 1239 /* Check for 4-byte MOVA relaxation (SH-specific). */ 1240 int second_reloc = 0; 1241 1242 op_ptr = contents + irel->r_offset - 4; 1243 1244 if (last_reloc) 1245 { 1246 arelent bfd_reloc; 1247 reloc_howto_type *h; 1248 bfd_vma last_reloc_size; 1249 1250 elf32_h8_info_to_howto (abfd, &bfd_reloc, last_reloc); 1251 h = bfd_reloc.howto; 1252 last_reloc_size = 1 << h->size; 1253 if (last_reloc->r_offset + last_reloc_size 1254 == irel->r_offset) 1255 { 1256 op_ptr -= last_reloc_size; 1257 second_reloc = 1; 1258 } 1259 } 1260 1261 if (irel + 1 < irelend) 1262 { 1263 Elf_Internal_Rela *next_reloc = irel + 1; 1264 arelent bfd_reloc; 1265 reloc_howto_type *h; 1266 bfd_vma next_reloc_size; 1267 1268 elf32_h8_info_to_howto (abfd, &bfd_reloc, next_reloc); 1269 h = bfd_reloc.howto; 1270 next_reloc_size = 1 << h->size; 1271 if (next_reloc->r_offset + next_reloc_size 1272 == irel->r_offset) 1273 { 1274 op_ptr -= next_reloc_size; 1275 second_reloc = 1; 1276 } 1277 } 1278 1279 op0 = bfd_get_8 (abfd, op_ptr + 0); 1280 op1 = bfd_get_8 (abfd, op_ptr + 1); 1281 op2 = bfd_get_8 (abfd, op_ptr + 2); 1282 op3 = bfd_get_8 (abfd, op_ptr + 3); 1283 1284 if (op0 == 0x01 1285 && (op1 & 0xdf) == 0x5f 1286 && (op2 & 0x40) == 0x40 1287 && (op3 & 0x80) == 0x80) 1288 { 1289 if ((op2 & 0x08) == 0) 1290 second_reloc = 1; 1291 1292 if (second_reloc) 1293 { 1294 op3 &= ~0x08; 1295 bfd_put_8 (abfd, op3, op_ptr + 3); 1296 } 1297 else 1298 { 1299 op2 &= ~0x08; 1300 bfd_put_8 (abfd, op2, op_ptr + 2); 1301 } 1302 goto r_h8_dir32a16_common; 1303 } 1304 } 1305 1306 /* Now check for short version of MOVA. (SH-specific) */ 1307 op_ptr = contents + irel->r_offset - 2; 1308 op0 = bfd_get_8 (abfd, op_ptr + 0); 1309 op1 = bfd_get_8 (abfd, op_ptr + 1); 1310 1311 if (op0 == 0x7a 1312 && (op1 & 0x88) == 0x80) 1313 { 1314 op1 |= 0x08; 1315 bfd_put_8 (abfd, op1, op_ptr + 1); 1316 goto r_h8_dir32a16_common; 1317 } 1318 1319 /* Get the opcode. */ 1320 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 1321 1322 /* Fix the opcode. For all the instructions that 1323 belong to this relaxation, we simply need to turn 1324 off bit 0x20 in the previous byte. */ 1325 code &= ~0x20; 1326 1327 bfd_put_8 (abfd, code, contents + irel->r_offset - 1); 1328 1329 r_h8_dir32a16_common: 1330 /* Fix the relocation's type. */ 1331 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 1332 R_H8_DIR16); 1333 1334 /* Delete two bytes of data. */ 1335 if (!elf32_h8_relax_delete_bytes (abfd, sec, 1336 irel->r_offset + 1, 2)) 1337 goto error_return; 1338 1339 /* That will change things, so, we should relax again. 1340 Note that this is not required, and it may be slow. */ 1341 *again = TRUE; 1342 } 1343 break; /* case R_H8_DIR32A16 */ 1344 } 1345 1346 case R_H8_DISP32A16: 1347 /* mov.[bwl] @(displ:24/32+ERx) -> mov.[bwl] @(displ:16+ERx) 4 bytes 1348 It is assured that instruction uses at least 4 bytes opcode before 1349 reloc entry addressing mode "register indirect with displacement" 1350 relaxing options (all saving 4 bytes): 1351 0x78 0sss0000 0x6A 0010dddd disp:32 mov.b @(d:32,ERs),Rd -> 1352 0x6E 0sssdddd disp:16 mov.b @(d:16,ERs),Rd 1353 0x78 0sss0000 0x6B 0010dddd disp:32 mov.w @(d:32,ERs),Rd -> 1354 0x6F 0sssdddd disp:16 mov.w @(d:16,ERs),Rd 1355 0x01 0x00 0x78 0sss0000 0x6B 00100ddd disp:32 mov.l @(d:32,ERs),ERd -> 1356 0x01 0x00 0x6F 0sss0ddd disp:16 mov.l @(d:16,ERs),ERd 1357 1358 0x78 0ddd0000 0x6A 1010ssss disp:32 mov.b Rs,@(d:32,ERd) -> 1359 0x6E 1dddssss disp:16 mov.b Rs,@(d:16,ERd) 1360 0x78 0ddd0000 0x6B 1010ssss disp:32 mov.w Rs,@(d:32,ERd) -> 1361 0x6F 1dddssss disp:16 mov.w Rs,@(d:16,ERd) 1362 0x01 0x00 0x78 xddd0000 0x6B 10100sss disp:32 mov.l ERs,@(d:32,ERd) -> 1363 0x01 0x00 0x6F 1ddd0sss disp:16 mov.l ERs,@(d:16,ERd) 1364 mov.l prefix 0x01 0x00 can be left as is and mov.l handled same 1365 as mov.w/ */ 1366 { 1367 bfd_vma value; 1368 1369 value = bfd_h8300_pad_address (abfd, symval + irel->r_addend); 1370 if (value <= 0x7fff || value >= 0xffff8000u) 1371 { 1372 unsigned char op0, op1, op2, op3, op0n, op1n; 1373 int relax = 0; 1374 1375 /* Note that we've changed the relocs, section contents, 1376 etc. */ 1377 elf_section_data (sec)->relocs = internal_relocs; 1378 elf_section_data (sec)->this_hdr.contents = contents; 1379 symtab_hdr->contents = (unsigned char *) isymbuf; 1380 1381 if (irel->r_offset >= 4) 1382 { 1383 op0 = bfd_get_8 (abfd, contents + irel->r_offset - 4); 1384 op1 = bfd_get_8 (abfd, contents + irel->r_offset - 3); 1385 op2 = bfd_get_8 (abfd, contents + irel->r_offset - 2); 1386 op3 = bfd_get_8 (abfd, contents + irel->r_offset - 1); 1387 1388 if (op0 == 0x78) 1389 { 1390 switch(op2) 1391 { 1392 case 0x6A: 1393 if ((op1 & 0x8F) == 0x00 && (op3 & 0x70) == 0x20) 1394 { 1395 /* mov.b. */ 1396 op0n = 0x6E; 1397 relax = 1; 1398 } 1399 break; 1400 case 0x6B: 1401 if ((op1 & 0x0F) == 0x00 && (op3 & 0x70) == 0x20) 1402 { 1403 /* mov.w/l. */ 1404 op0n = 0x6F; 1405 relax = 1; 1406 } 1407 break; 1408 default: 1409 break; 1410 } 1411 } 1412 } 1413 1414 if (relax) 1415 { 1416 op1n = (op3 & 0x8F) | (op1 & 0x70); 1417 bfd_put_8 (abfd, op0n, contents + irel->r_offset - 4); 1418 bfd_put_8 (abfd, op1n, contents + irel->r_offset - 3); 1419 1420 /* Fix the relocation's type. */ 1421 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_H8_DIR16); 1422 irel->r_offset -= 2; 1423 1424 /* Delete four bytes of data. */ 1425 if (!elf32_h8_relax_delete_bytes (abfd, sec, irel->r_offset + 2, 4)) 1426 goto error_return; 1427 1428 /* That will change things, so, we should relax again. 1429 Note that this is not required, and it may be slow. */ 1430 *again = TRUE; 1431 } 1432 } 1433 } 1434 break; 1435 1436 default: 1437 break; 1438 } 1439 } 1440 1441 if (isymbuf != NULL 1442 && symtab_hdr->contents != (unsigned char *) isymbuf) 1443 { 1444 if (! link_info->keep_memory) 1445 free (isymbuf); 1446 else 1447 symtab_hdr->contents = (unsigned char *) isymbuf; 1448 } 1449 1450 if (contents != NULL 1451 && elf_section_data (sec)->this_hdr.contents != contents) 1452 { 1453 if (! link_info->keep_memory) 1454 free (contents); 1455 else 1456 { 1457 /* Cache the section contents for elf_link_input_bfd. */ 1458 elf_section_data (sec)->this_hdr.contents = contents; 1459 } 1460 } 1461 1462 if (internal_relocs != NULL 1463 && elf_section_data (sec)->relocs != internal_relocs) 1464 free (internal_relocs); 1465 1466 return TRUE; 1467 1468 error_return: 1469 if (isymbuf != NULL 1470 && symtab_hdr->contents != (unsigned char *) isymbuf) 1471 free (isymbuf); 1472 if (contents != NULL 1473 && elf_section_data (sec)->this_hdr.contents != contents) 1474 free (contents); 1475 if (internal_relocs != NULL 1476 && elf_section_data (sec)->relocs != internal_relocs) 1477 free (internal_relocs); 1478 return FALSE; 1479 } 1480 1481 /* Delete some bytes from a section while relaxing. */ 1482 1483 static bfd_boolean 1484 elf32_h8_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count) 1485 { 1486 Elf_Internal_Shdr *symtab_hdr; 1487 unsigned int sec_shndx; 1488 bfd_byte *contents; 1489 Elf_Internal_Rela *irel, *irelend; 1490 Elf_Internal_Sym *isym; 1491 Elf_Internal_Sym *isymend; 1492 bfd_vma toaddr; 1493 struct elf_link_hash_entry **sym_hashes; 1494 struct elf_link_hash_entry **end_hashes; 1495 unsigned int symcount; 1496 1497 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 1498 1499 contents = elf_section_data (sec)->this_hdr.contents; 1500 1501 toaddr = sec->size; 1502 1503 irel = elf_section_data (sec)->relocs; 1504 irelend = irel + sec->reloc_count; 1505 1506 /* Actually delete the bytes. */ 1507 memmove (contents + addr, contents + addr + count, 1508 (size_t) (toaddr - addr - count)); 1509 sec->size -= count; 1510 1511 /* Adjust all the relocs. */ 1512 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) 1513 { 1514 /* Get the new reloc address. */ 1515 if ((irel->r_offset > addr 1516 && irel->r_offset <= toaddr)) 1517 irel->r_offset -= count; 1518 } 1519 1520 /* Adjust the local symbols defined in this section. */ 1521 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 1522 isym = (Elf_Internal_Sym *) symtab_hdr->contents; 1523 isymend = isym + symtab_hdr->sh_info; 1524 for (; isym < isymend; isym++) 1525 { 1526 if (isym->st_shndx == sec_shndx 1527 && isym->st_value > addr 1528 && isym->st_value <= toaddr) 1529 isym->st_value -= count; 1530 } 1531 1532 /* Now adjust the global symbols defined in this section. */ 1533 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 1534 - symtab_hdr->sh_info); 1535 sym_hashes = elf_sym_hashes (abfd); 1536 end_hashes = sym_hashes + symcount; 1537 for (; sym_hashes < end_hashes; sym_hashes++) 1538 { 1539 struct elf_link_hash_entry *sym_hash = *sym_hashes; 1540 1541 if ((sym_hash->root.type == bfd_link_hash_defined 1542 || sym_hash->root.type == bfd_link_hash_defweak) 1543 && sym_hash->root.u.def.section == sec 1544 && sym_hash->root.u.def.value > addr 1545 && sym_hash->root.u.def.value <= toaddr) 1546 sym_hash->root.u.def.value -= count; 1547 } 1548 1549 return TRUE; 1550 } 1551 1552 /* Return TRUE if a symbol exists at the given address, else return 1553 FALSE. */ 1554 static bfd_boolean 1555 elf32_h8_symbol_address_p (bfd *abfd, asection *sec, bfd_vma addr) 1556 { 1557 Elf_Internal_Shdr *symtab_hdr; 1558 unsigned int sec_shndx; 1559 Elf_Internal_Sym *isym; 1560 Elf_Internal_Sym *isymend; 1561 struct elf_link_hash_entry **sym_hashes; 1562 struct elf_link_hash_entry **end_hashes; 1563 unsigned int symcount; 1564 1565 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 1566 1567 /* Examine all the symbols. */ 1568 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 1569 isym = (Elf_Internal_Sym *) symtab_hdr->contents; 1570 isymend = isym + symtab_hdr->sh_info; 1571 for (; isym < isymend; isym++) 1572 { 1573 if (isym->st_shndx == sec_shndx 1574 && isym->st_value == addr) 1575 return TRUE; 1576 } 1577 1578 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 1579 - symtab_hdr->sh_info); 1580 sym_hashes = elf_sym_hashes (abfd); 1581 end_hashes = sym_hashes + symcount; 1582 for (; sym_hashes < end_hashes; sym_hashes++) 1583 { 1584 struct elf_link_hash_entry *sym_hash = *sym_hashes; 1585 if ((sym_hash->root.type == bfd_link_hash_defined 1586 || sym_hash->root.type == bfd_link_hash_defweak) 1587 && sym_hash->root.u.def.section == sec 1588 && sym_hash->root.u.def.value == addr) 1589 return TRUE; 1590 } 1591 1592 return FALSE; 1593 } 1594 1595 /* This is a version of bfd_generic_get_relocated_section_contents 1596 which uses elf32_h8_relocate_section. */ 1597 1598 static bfd_byte * 1599 elf32_h8_get_relocated_section_contents (bfd *output_bfd, 1600 struct bfd_link_info *link_info, 1601 struct bfd_link_order *link_order, 1602 bfd_byte *data, 1603 bfd_boolean relocatable, 1604 asymbol **symbols) 1605 { 1606 Elf_Internal_Shdr *symtab_hdr; 1607 asection *input_section = link_order->u.indirect.section; 1608 bfd *input_bfd = input_section->owner; 1609 asection **sections = NULL; 1610 Elf_Internal_Rela *internal_relocs = NULL; 1611 Elf_Internal_Sym *isymbuf = NULL; 1612 1613 /* We only need to handle the case of relaxing, or of having a 1614 particular set of section contents, specially. */ 1615 if (relocatable 1616 || elf_section_data (input_section)->this_hdr.contents == NULL) 1617 return bfd_generic_get_relocated_section_contents (output_bfd, link_info, 1618 link_order, data, 1619 relocatable, 1620 symbols); 1621 1622 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 1623 1624 memcpy (data, elf_section_data (input_section)->this_hdr.contents, 1625 (size_t) input_section->size); 1626 1627 if ((input_section->flags & SEC_RELOC) != 0 1628 && input_section->reloc_count > 0) 1629 { 1630 asection **secpp; 1631 Elf_Internal_Sym *isym, *isymend; 1632 bfd_size_type amt; 1633 1634 internal_relocs = (_bfd_elf_link_read_relocs 1635 (input_bfd, input_section, NULL, 1636 (Elf_Internal_Rela *) NULL, FALSE)); 1637 if (internal_relocs == NULL) 1638 goto error_return; 1639 1640 if (symtab_hdr->sh_info != 0) 1641 { 1642 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 1643 if (isymbuf == NULL) 1644 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 1645 symtab_hdr->sh_info, 0, 1646 NULL, NULL, NULL); 1647 if (isymbuf == NULL) 1648 goto error_return; 1649 } 1650 1651 amt = symtab_hdr->sh_info; 1652 amt *= sizeof (asection *); 1653 sections = (asection **) bfd_malloc (amt); 1654 if (sections == NULL && amt != 0) 1655 goto error_return; 1656 1657 isymend = isymbuf + symtab_hdr->sh_info; 1658 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) 1659 { 1660 asection *isec; 1661 1662 if (isym->st_shndx == SHN_UNDEF) 1663 isec = bfd_und_section_ptr; 1664 else if (isym->st_shndx == SHN_ABS) 1665 isec = bfd_abs_section_ptr; 1666 else if (isym->st_shndx == SHN_COMMON) 1667 isec = bfd_com_section_ptr; 1668 else 1669 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 1670 1671 *secpp = isec; 1672 } 1673 1674 if (! elf32_h8_relocate_section (output_bfd, link_info, input_bfd, 1675 input_section, data, internal_relocs, 1676 isymbuf, sections)) 1677 goto error_return; 1678 1679 if (sections != NULL) 1680 free (sections); 1681 if (isymbuf != NULL 1682 && symtab_hdr->contents != (unsigned char *) isymbuf) 1683 free (isymbuf); 1684 if (elf_section_data (input_section)->relocs != internal_relocs) 1685 free (internal_relocs); 1686 } 1687 1688 return data; 1689 1690 error_return: 1691 if (sections != NULL) 1692 free (sections); 1693 if (isymbuf != NULL 1694 && symtab_hdr->contents != (unsigned char *) isymbuf) 1695 free (isymbuf); 1696 if (internal_relocs != NULL 1697 && elf_section_data (input_section)->relocs != internal_relocs) 1698 free (internal_relocs); 1699 return NULL; 1700 } 1701 1702 1703 #define TARGET_BIG_SYM h8300_elf32_vec 1704 #define TARGET_BIG_NAME "elf32-h8300" 1705 #define ELF_ARCH bfd_arch_h8300 1706 #define ELF_MACHINE_CODE EM_H8_300 1707 #define ELF_MAXPAGESIZE 0x1 1708 #define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup 1709 #define bfd_elf32_bfd_reloc_name_lookup elf32_h8_reloc_name_lookup 1710 #define elf_info_to_howto elf32_h8_info_to_howto 1711 #define elf_info_to_howto_rel elf32_h8_info_to_howto_rel 1712 1713 /* So we can set/examine bits in e_flags to get the specific 1714 H8 architecture in use. */ 1715 #define elf_backend_final_write_processing \ 1716 elf32_h8_final_write_processing 1717 #define elf_backend_object_p \ 1718 elf32_h8_object_p 1719 #define bfd_elf32_bfd_merge_private_bfd_data \ 1720 elf32_h8_merge_private_bfd_data 1721 1722 /* ??? when elf_backend_relocate_section is not defined, elf32-target.h 1723 defaults to using _bfd_generic_link_hash_table_create, but 1724 bfd_elf_size_dynamic_sections uses 1725 dynobj = elf_hash_table (info)->dynobj; 1726 and thus requires an elf hash table. */ 1727 #define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create 1728 1729 /* Use an H8 specific linker, not the ELF generic linker. */ 1730 #define elf_backend_relocate_section elf32_h8_relocate_section 1731 #define elf_backend_rela_normal 1 1732 #define elf_backend_can_gc_sections 1 1733 1734 /* And relaxing stuff. */ 1735 #define bfd_elf32_bfd_relax_section elf32_h8_relax_section 1736 #define bfd_elf32_bfd_get_relocated_section_contents \ 1737 elf32_h8_get_relocated_section_contents 1738 1739 #define elf_symbol_leading_char '_' 1740 1741 #include "elf32-target.h" 1742 1743 #undef TARGET_BIG_SYM 1744 #define TARGET_BIG_SYM h8300_elf32_linux_vec 1745 #undef TARGET_BIG_NAME 1746 #define TARGET_BIG_NAME "elf32-h8300-linux" 1747 #undef elf_symbol_leading_char 1748 #define elf32_bed elf32_h8300_linux_bed 1749 1750 #include "elf32-target.h" 1751