| 1 | /* Matsushita 10200 specific support for 32-bit ELF |
| 2 | Copyright (C) 1996-2014 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 | |
| 26 | static bfd_boolean |
| 27 | mn10200_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int); |
| 28 | static bfd_boolean |
| 29 | mn10200_elf_symbol_address_p (bfd *, asection *, Elf_Internal_Sym *, bfd_vma); |
| 30 | |
| 31 | enum reloc_type |
| 32 | { |
| 33 | R_MN10200_NONE = 0, |
| 34 | R_MN10200_32, |
| 35 | R_MN10200_16, |
| 36 | R_MN10200_8, |
| 37 | R_MN10200_24, |
| 38 | R_MN10200_PCREL8, |
| 39 | R_MN10200_PCREL16, |
| 40 | R_MN10200_PCREL24, |
| 41 | R_MN10200_MAX |
| 42 | }; |
| 43 | |
| 44 | static reloc_howto_type elf_mn10200_howto_table[] = |
| 45 | { |
| 46 | /* Dummy relocation. Does nothing. */ |
| 47 | HOWTO (R_MN10200_NONE, |
| 48 | 0, |
| 49 | 2, |
| 50 | 16, |
| 51 | FALSE, |
| 52 | 0, |
| 53 | complain_overflow_bitfield, |
| 54 | bfd_elf_generic_reloc, |
| 55 | "R_MN10200_NONE", |
| 56 | FALSE, |
| 57 | 0, |
| 58 | 0, |
| 59 | FALSE), |
| 60 | /* Standard 32 bit reloc. */ |
| 61 | HOWTO (R_MN10200_32, |
| 62 | 0, |
| 63 | 2, |
| 64 | 32, |
| 65 | FALSE, |
| 66 | 0, |
| 67 | complain_overflow_bitfield, |
| 68 | bfd_elf_generic_reloc, |
| 69 | "R_MN10200_32", |
| 70 | FALSE, |
| 71 | 0xffffffff, |
| 72 | 0xffffffff, |
| 73 | FALSE), |
| 74 | /* Standard 16 bit reloc. */ |
| 75 | HOWTO (R_MN10200_16, |
| 76 | 0, |
| 77 | 1, |
| 78 | 16, |
| 79 | FALSE, |
| 80 | 0, |
| 81 | complain_overflow_bitfield, |
| 82 | bfd_elf_generic_reloc, |
| 83 | "R_MN10200_16", |
| 84 | FALSE, |
| 85 | 0xffff, |
| 86 | 0xffff, |
| 87 | FALSE), |
| 88 | /* Standard 8 bit reloc. */ |
| 89 | HOWTO (R_MN10200_8, |
| 90 | 0, |
| 91 | 0, |
| 92 | 8, |
| 93 | FALSE, |
| 94 | 0, |
| 95 | complain_overflow_bitfield, |
| 96 | bfd_elf_generic_reloc, |
| 97 | "R_MN10200_8", |
| 98 | FALSE, |
| 99 | 0xff, |
| 100 | 0xff, |
| 101 | FALSE), |
| 102 | /* Standard 24 bit reloc. */ |
| 103 | HOWTO (R_MN10200_24, |
| 104 | 0, |
| 105 | 2, |
| 106 | 24, |
| 107 | FALSE, |
| 108 | 0, |
| 109 | complain_overflow_bitfield, |
| 110 | bfd_elf_generic_reloc, |
| 111 | "R_MN10200_24", |
| 112 | FALSE, |
| 113 | 0xffffff, |
| 114 | 0xffffff, |
| 115 | FALSE), |
| 116 | /* Simple 8 pc-relative reloc. */ |
| 117 | HOWTO (R_MN10200_PCREL8, |
| 118 | 0, |
| 119 | 0, |
| 120 | 8, |
| 121 | TRUE, |
| 122 | 0, |
| 123 | complain_overflow_bitfield, |
| 124 | bfd_elf_generic_reloc, |
| 125 | "R_MN10200_PCREL8", |
| 126 | FALSE, |
| 127 | 0xff, |
| 128 | 0xff, |
| 129 | TRUE), |
| 130 | /* Simple 16 pc-relative reloc. */ |
| 131 | HOWTO (R_MN10200_PCREL16, |
| 132 | 0, |
| 133 | 1, |
| 134 | 16, |
| 135 | TRUE, |
| 136 | 0, |
| 137 | complain_overflow_bitfield, |
| 138 | bfd_elf_generic_reloc, |
| 139 | "R_MN10200_PCREL16", |
| 140 | FALSE, |
| 141 | 0xffff, |
| 142 | 0xffff, |
| 143 | TRUE), |
| 144 | /* Simple 32bit pc-relative reloc with a 1 byte adjustment |
| 145 | to get the pc-relative offset correct. */ |
| 146 | HOWTO (R_MN10200_PCREL24, |
| 147 | 0, |
| 148 | 2, |
| 149 | 24, |
| 150 | TRUE, |
| 151 | 0, |
| 152 | complain_overflow_bitfield, |
| 153 | bfd_elf_generic_reloc, |
| 154 | "R_MN10200_PCREL24", |
| 155 | FALSE, |
| 156 | 0xffffff, |
| 157 | 0xffffff, |
| 158 | TRUE), |
| 159 | }; |
| 160 | |
| 161 | struct mn10200_reloc_map |
| 162 | { |
| 163 | bfd_reloc_code_real_type bfd_reloc_val; |
| 164 | unsigned char elf_reloc_val; |
| 165 | }; |
| 166 | |
| 167 | static const struct mn10200_reloc_map mn10200_reloc_map[] = |
| 168 | { |
| 169 | { BFD_RELOC_NONE , R_MN10200_NONE , }, |
| 170 | { BFD_RELOC_32 , R_MN10200_32 , }, |
| 171 | { BFD_RELOC_16 , R_MN10200_16 , }, |
| 172 | { BFD_RELOC_8 , R_MN10200_8 , }, |
| 173 | { BFD_RELOC_24 , R_MN10200_24 , }, |
| 174 | { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , }, |
| 175 | { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, }, |
| 176 | { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, }, |
| 177 | }; |
| 178 | |
| 179 | static reloc_howto_type * |
| 180 | bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
| 181 | bfd_reloc_code_real_type code) |
| 182 | { |
| 183 | unsigned int i; |
| 184 | |
| 185 | for (i = 0; |
| 186 | i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map); |
| 187 | i++) |
| 188 | { |
| 189 | if (mn10200_reloc_map[i].bfd_reloc_val == code) |
| 190 | return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val]; |
| 191 | } |
| 192 | |
| 193 | return NULL; |
| 194 | } |
| 195 | |
| 196 | static reloc_howto_type * |
| 197 | bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
| 198 | const char *r_name) |
| 199 | { |
| 200 | unsigned int i; |
| 201 | |
| 202 | for (i = 0; |
| 203 | i < (sizeof (elf_mn10200_howto_table) |
| 204 | / sizeof (elf_mn10200_howto_table[0])); |
| 205 | i++) |
| 206 | if (elf_mn10200_howto_table[i].name != NULL |
| 207 | && strcasecmp (elf_mn10200_howto_table[i].name, r_name) == 0) |
| 208 | return &elf_mn10200_howto_table[i]; |
| 209 | |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | /* Set the howto pointer for an MN10200 ELF reloc. */ |
| 214 | |
| 215 | static void |
| 216 | mn10200_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, |
| 217 | arelent *cache_ptr, |
| 218 | Elf_Internal_Rela *dst) |
| 219 | { |
| 220 | unsigned int r_type; |
| 221 | |
| 222 | r_type = ELF32_R_TYPE (dst->r_info); |
| 223 | BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX); |
| 224 | cache_ptr->howto = &elf_mn10200_howto_table[r_type]; |
| 225 | } |
| 226 | |
| 227 | /* Perform a relocation as part of a final link. */ |
| 228 | |
| 229 | static bfd_reloc_status_type |
| 230 | mn10200_elf_final_link_relocate (reloc_howto_type *howto, |
| 231 | bfd *input_bfd, |
| 232 | bfd *output_bfd ATTRIBUTE_UNUSED, |
| 233 | asection *input_section, |
| 234 | bfd_byte *contents, |
| 235 | bfd_vma offset, |
| 236 | bfd_vma value, |
| 237 | bfd_vma addend, |
| 238 | struct bfd_link_info *info ATTRIBUTE_UNUSED, |
| 239 | asection *sym_sec ATTRIBUTE_UNUSED, |
| 240 | int is_local ATTRIBUTE_UNUSED) |
| 241 | { |
| 242 | unsigned long r_type = howto->type; |
| 243 | bfd_byte *hit_data = contents + offset; |
| 244 | |
| 245 | switch (r_type) |
| 246 | { |
| 247 | |
| 248 | case R_MN10200_NONE: |
| 249 | return bfd_reloc_ok; |
| 250 | |
| 251 | case R_MN10200_32: |
| 252 | value += addend; |
| 253 | bfd_put_32 (input_bfd, value, hit_data); |
| 254 | return bfd_reloc_ok; |
| 255 | |
| 256 | case R_MN10200_16: |
| 257 | value += addend; |
| 258 | |
| 259 | if ((long) value > 0x7fff || (long) value < -0x8000) |
| 260 | return bfd_reloc_overflow; |
| 261 | |
| 262 | bfd_put_16 (input_bfd, value, hit_data); |
| 263 | return bfd_reloc_ok; |
| 264 | |
| 265 | case R_MN10200_8: |
| 266 | value += addend; |
| 267 | |
| 268 | if ((long) value > 0x7f || (long) value < -0x80) |
| 269 | return bfd_reloc_overflow; |
| 270 | |
| 271 | bfd_put_8 (input_bfd, value, hit_data); |
| 272 | return bfd_reloc_ok; |
| 273 | |
| 274 | case R_MN10200_24: |
| 275 | value += addend; |
| 276 | |
| 277 | if ((long) value > 0x7fffff || (long) value < -0x800000) |
| 278 | return bfd_reloc_overflow; |
| 279 | |
| 280 | value &= 0xffffff; |
| 281 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000); |
| 282 | bfd_put_32 (input_bfd, value, hit_data); |
| 283 | return bfd_reloc_ok; |
| 284 | |
| 285 | case R_MN10200_PCREL8: |
| 286 | value -= (input_section->output_section->vma |
| 287 | + input_section->output_offset); |
| 288 | value -= (offset + 1); |
| 289 | value += addend; |
| 290 | |
| 291 | if ((long) value > 0xff || (long) value < -0x100) |
| 292 | return bfd_reloc_overflow; |
| 293 | |
| 294 | bfd_put_8 (input_bfd, value, hit_data); |
| 295 | return bfd_reloc_ok; |
| 296 | |
| 297 | case R_MN10200_PCREL16: |
| 298 | value -= (input_section->output_section->vma |
| 299 | + input_section->output_offset); |
| 300 | value -= (offset + 2); |
| 301 | value += addend; |
| 302 | |
| 303 | if ((long) value > 0xffff || (long) value < -0x10000) |
| 304 | return bfd_reloc_overflow; |
| 305 | |
| 306 | bfd_put_16 (input_bfd, value, hit_data); |
| 307 | return bfd_reloc_ok; |
| 308 | |
| 309 | case R_MN10200_PCREL24: |
| 310 | value -= (input_section->output_section->vma |
| 311 | + input_section->output_offset); |
| 312 | value -= (offset + 3); |
| 313 | value += addend; |
| 314 | |
| 315 | if ((long) value > 0xffffff || (long) value < -0x1000000) |
| 316 | return bfd_reloc_overflow; |
| 317 | |
| 318 | value &= 0xffffff; |
| 319 | value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000); |
| 320 | bfd_put_32 (input_bfd, value, hit_data); |
| 321 | return bfd_reloc_ok; |
| 322 | |
| 323 | default: |
| 324 | return bfd_reloc_notsupported; |
| 325 | } |
| 326 | } |
| 327 | \f |
| 328 | /* Relocate an MN10200 ELF section. */ |
| 329 | static bfd_boolean |
| 330 | mn10200_elf_relocate_section (bfd *output_bfd, |
| 331 | struct bfd_link_info *info, |
| 332 | bfd *input_bfd, |
| 333 | asection *input_section, |
| 334 | bfd_byte *contents, |
| 335 | Elf_Internal_Rela *relocs, |
| 336 | Elf_Internal_Sym *local_syms, |
| 337 | asection **local_sections) |
| 338 | { |
| 339 | Elf_Internal_Shdr *symtab_hdr; |
| 340 | struct elf_link_hash_entry **sym_hashes; |
| 341 | Elf_Internal_Rela *rel, *relend; |
| 342 | |
| 343 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 344 | sym_hashes = elf_sym_hashes (input_bfd); |
| 345 | |
| 346 | rel = relocs; |
| 347 | relend = relocs + input_section->reloc_count; |
| 348 | for (; rel < relend; rel++) |
| 349 | { |
| 350 | int r_type; |
| 351 | reloc_howto_type *howto; |
| 352 | unsigned long r_symndx; |
| 353 | Elf_Internal_Sym *sym; |
| 354 | asection *sec; |
| 355 | struct elf_link_hash_entry *h; |
| 356 | bfd_vma relocation; |
| 357 | bfd_reloc_status_type r; |
| 358 | |
| 359 | r_symndx = ELF32_R_SYM (rel->r_info); |
| 360 | r_type = ELF32_R_TYPE (rel->r_info); |
| 361 | howto = elf_mn10200_howto_table + r_type; |
| 362 | |
| 363 | h = NULL; |
| 364 | sym = NULL; |
| 365 | sec = NULL; |
| 366 | if (r_symndx < symtab_hdr->sh_info) |
| 367 | { |
| 368 | sym = local_syms + r_symndx; |
| 369 | sec = local_sections[r_symndx]; |
| 370 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | bfd_boolean unresolved_reloc, warned, ignored; |
| 375 | |
| 376 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
| 377 | r_symndx, symtab_hdr, sym_hashes, |
| 378 | h, sec, relocation, |
| 379 | unresolved_reloc, warned, ignored); |
| 380 | } |
| 381 | |
| 382 | if (sec != NULL && discarded_section (sec)) |
| 383 | RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, |
| 384 | rel, 1, relend, howto, 0, contents); |
| 385 | |
| 386 | if (info->relocatable) |
| 387 | continue; |
| 388 | |
| 389 | r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd, |
| 390 | input_section, |
| 391 | contents, rel->r_offset, |
| 392 | relocation, rel->r_addend, |
| 393 | info, sec, h == NULL); |
| 394 | |
| 395 | if (r != bfd_reloc_ok) |
| 396 | { |
| 397 | const char *name; |
| 398 | const char *msg = (const char *) 0; |
| 399 | |
| 400 | if (h != NULL) |
| 401 | name = h->root.root.string; |
| 402 | else |
| 403 | { |
| 404 | name = (bfd_elf_string_from_elf_section |
| 405 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); |
| 406 | if (name == NULL || *name == '\0') |
| 407 | name = bfd_section_name (input_bfd, sec); |
| 408 | } |
| 409 | |
| 410 | switch (r) |
| 411 | { |
| 412 | case bfd_reloc_overflow: |
| 413 | if (! ((*info->callbacks->reloc_overflow) |
| 414 | (info, (h ? &h->root : NULL), name, howto->name, |
| 415 | (bfd_vma) 0, input_bfd, input_section, |
| 416 | rel->r_offset))) |
| 417 | return FALSE; |
| 418 | break; |
| 419 | |
| 420 | case bfd_reloc_undefined: |
| 421 | if (! ((*info->callbacks->undefined_symbol) |
| 422 | (info, name, input_bfd, input_section, |
| 423 | rel->r_offset, TRUE))) |
| 424 | return FALSE; |
| 425 | break; |
| 426 | |
| 427 | case bfd_reloc_outofrange: |
| 428 | msg = _("internal error: out of range error"); |
| 429 | goto common_error; |
| 430 | |
| 431 | case bfd_reloc_notsupported: |
| 432 | msg = _("internal error: unsupported relocation error"); |
| 433 | goto common_error; |
| 434 | |
| 435 | case bfd_reloc_dangerous: |
| 436 | msg = _("internal error: dangerous error"); |
| 437 | goto common_error; |
| 438 | |
| 439 | default: |
| 440 | msg = _("internal error: unknown error"); |
| 441 | /* fall through */ |
| 442 | |
| 443 | common_error: |
| 444 | if (!((*info->callbacks->warning) |
| 445 | (info, msg, name, input_bfd, input_section, |
| 446 | rel->r_offset))) |
| 447 | return FALSE; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | return TRUE; |
| 454 | } |
| 455 | |
| 456 | /* Delete some bytes from a section while relaxing. */ |
| 457 | |
| 458 | static bfd_boolean |
| 459 | mn10200_elf_relax_delete_bytes (bfd *abfd, asection *sec, |
| 460 | bfd_vma addr, int count) |
| 461 | { |
| 462 | Elf_Internal_Shdr *symtab_hdr; |
| 463 | unsigned int sec_shndx; |
| 464 | bfd_byte *contents; |
| 465 | Elf_Internal_Rela *irel, *irelend; |
| 466 | bfd_vma toaddr; |
| 467 | Elf_Internal_Sym *isym; |
| 468 | Elf_Internal_Sym *isymend; |
| 469 | struct elf_link_hash_entry **sym_hashes; |
| 470 | struct elf_link_hash_entry **end_hashes; |
| 471 | unsigned int symcount; |
| 472 | |
| 473 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
| 474 | |
| 475 | contents = elf_section_data (sec)->this_hdr.contents; |
| 476 | |
| 477 | toaddr = sec->size; |
| 478 | |
| 479 | irel = elf_section_data (sec)->relocs; |
| 480 | irelend = irel + sec->reloc_count; |
| 481 | |
| 482 | /* Actually delete the bytes. */ |
| 483 | memmove (contents + addr, contents + addr + count, |
| 484 | (size_t) (toaddr - addr - count)); |
| 485 | sec->size -= count; |
| 486 | |
| 487 | /* Adjust all the relocs. */ |
| 488 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) |
| 489 | { |
| 490 | /* Get the new reloc address. */ |
| 491 | if ((irel->r_offset > addr |
| 492 | && irel->r_offset < toaddr)) |
| 493 | irel->r_offset -= count; |
| 494 | } |
| 495 | |
| 496 | /* Adjust the local symbols defined in this section. */ |
| 497 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 498 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; |
| 499 | for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++) |
| 500 | { |
| 501 | if (isym->st_shndx == sec_shndx |
| 502 | && isym->st_value > addr |
| 503 | && isym->st_value < toaddr) |
| 504 | isym->st_value -= count; |
| 505 | } |
| 506 | |
| 507 | /* Now adjust the global symbols defined in this section. */ |
| 508 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
| 509 | - symtab_hdr->sh_info); |
| 510 | sym_hashes = elf_sym_hashes (abfd); |
| 511 | end_hashes = sym_hashes + symcount; |
| 512 | for (; sym_hashes < end_hashes; sym_hashes++) |
| 513 | { |
| 514 | struct elf_link_hash_entry *sym_hash = *sym_hashes; |
| 515 | if ((sym_hash->root.type == bfd_link_hash_defined |
| 516 | || sym_hash->root.type == bfd_link_hash_defweak) |
| 517 | && sym_hash->root.u.def.section == sec |
| 518 | && sym_hash->root.u.def.value > addr |
| 519 | && sym_hash->root.u.def.value < toaddr) |
| 520 | { |
| 521 | sym_hash->root.u.def.value -= count; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | return TRUE; |
| 526 | } |
| 527 | |
| 528 | /* This function handles relaxing for the mn10200. |
| 529 | |
| 530 | There are quite a few relaxing opportunities available on the mn10200: |
| 531 | |
| 532 | * jsr:24 -> jsr:16 2 bytes |
| 533 | |
| 534 | * jmp:24 -> jmp:16 2 bytes |
| 535 | * jmp:16 -> bra:8 1 byte |
| 536 | |
| 537 | * If the previous instruction is a conditional branch |
| 538 | around the jump/bra, we may be able to reverse its condition |
| 539 | and change its target to the jump's target. The jump/bra |
| 540 | can then be deleted. 2 bytes |
| 541 | |
| 542 | * mov abs24 -> mov abs16 2 byte savings |
| 543 | |
| 544 | * Most instructions which accept imm24 can relax to imm16 2 bytes |
| 545 | - Most instructions which accept imm16 can relax to imm8 1 byte |
| 546 | |
| 547 | * Most instructions which accept d24 can relax to d16 2 bytes |
| 548 | - Most instructions which accept d16 can relax to d8 1 byte |
| 549 | |
| 550 | abs24, imm24, d24 all look the same at the reloc level. It |
| 551 | might make the code simpler if we had different relocs for |
| 552 | the various relaxable operand types. |
| 553 | |
| 554 | We don't handle imm16->imm8 or d16->d8 as they're very rare |
| 555 | and somewhat more difficult to support. */ |
| 556 | |
| 557 | static bfd_boolean |
| 558 | mn10200_elf_relax_section (bfd *abfd, |
| 559 | asection *sec, |
| 560 | struct bfd_link_info *link_info, |
| 561 | bfd_boolean *again) |
| 562 | { |
| 563 | Elf_Internal_Shdr *symtab_hdr; |
| 564 | Elf_Internal_Rela *internal_relocs; |
| 565 | Elf_Internal_Rela *irel, *irelend; |
| 566 | bfd_byte *contents = NULL; |
| 567 | Elf_Internal_Sym *isymbuf = NULL; |
| 568 | |
| 569 | /* Assume nothing changes. */ |
| 570 | *again = FALSE; |
| 571 | |
| 572 | /* We don't have to do anything for a relocatable link, if |
| 573 | this section does not have relocs, or if this is not a |
| 574 | code section. */ |
| 575 | if (link_info->relocatable |
| 576 | || (sec->flags & SEC_RELOC) == 0 |
| 577 | || sec->reloc_count == 0 |
| 578 | || (sec->flags & SEC_CODE) == 0) |
| 579 | return TRUE; |
| 580 | |
| 581 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 582 | |
| 583 | /* Get a copy of the native relocations. */ |
| 584 | internal_relocs = (_bfd_elf_link_read_relocs |
| 585 | (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, |
| 586 | link_info->keep_memory)); |
| 587 | if (internal_relocs == NULL) |
| 588 | goto error_return; |
| 589 | |
| 590 | /* Walk through them looking for relaxing opportunities. */ |
| 591 | irelend = internal_relocs + sec->reloc_count; |
| 592 | for (irel = internal_relocs; irel < irelend; irel++) |
| 593 | { |
| 594 | bfd_vma symval; |
| 595 | |
| 596 | /* If this isn't something that can be relaxed, then ignore |
| 597 | this reloc. */ |
| 598 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE |
| 599 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8 |
| 600 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX) |
| 601 | continue; |
| 602 | |
| 603 | /* Get the section contents if we haven't done so already. */ |
| 604 | if (contents == NULL) |
| 605 | { |
| 606 | /* Get cached copy if it exists. */ |
| 607 | if (elf_section_data (sec)->this_hdr.contents != NULL) |
| 608 | contents = elf_section_data (sec)->this_hdr.contents; |
| 609 | else |
| 610 | { |
| 611 | /* Go get them off disk. */ |
| 612 | if (!bfd_malloc_and_get_section (abfd, sec, &contents)) |
| 613 | goto error_return; |
| 614 | } |
| 615 | } |
| 616 | |
| 617 | /* Read this BFD's local symbols if we haven't done so already. */ |
| 618 | if (isymbuf == NULL && symtab_hdr->sh_info != 0) |
| 619 | { |
| 620 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; |
| 621 | if (isymbuf == NULL) |
| 622 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, |
| 623 | symtab_hdr->sh_info, 0, |
| 624 | NULL, NULL, NULL); |
| 625 | if (isymbuf == NULL) |
| 626 | goto error_return; |
| 627 | } |
| 628 | |
| 629 | /* Get the value of the symbol referred to by the reloc. */ |
| 630 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) |
| 631 | { |
| 632 | /* A local symbol. */ |
| 633 | Elf_Internal_Sym *isym; |
| 634 | asection *sym_sec; |
| 635 | |
| 636 | isym = isymbuf + ELF32_R_SYM (irel->r_info); |
| 637 | if (isym->st_shndx == SHN_UNDEF) |
| 638 | sym_sec = bfd_und_section_ptr; |
| 639 | else if (isym->st_shndx == SHN_ABS) |
| 640 | sym_sec = bfd_abs_section_ptr; |
| 641 | else if (isym->st_shndx == SHN_COMMON) |
| 642 | sym_sec = bfd_com_section_ptr; |
| 643 | else |
| 644 | sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); |
| 645 | symval = (isym->st_value |
| 646 | + sym_sec->output_section->vma |
| 647 | + sym_sec->output_offset); |
| 648 | } |
| 649 | else |
| 650 | { |
| 651 | unsigned long indx; |
| 652 | struct elf_link_hash_entry *h; |
| 653 | |
| 654 | /* An external symbol. */ |
| 655 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; |
| 656 | h = elf_sym_hashes (abfd)[indx]; |
| 657 | BFD_ASSERT (h != NULL); |
| 658 | if (h->root.type != bfd_link_hash_defined |
| 659 | && h->root.type != bfd_link_hash_defweak) |
| 660 | { |
| 661 | /* This appears to be a reference to an undefined |
| 662 | symbol. Just ignore it--it will be caught by the |
| 663 | regular reloc processing. */ |
| 664 | continue; |
| 665 | } |
| 666 | |
| 667 | symval = (h->root.u.def.value |
| 668 | + h->root.u.def.section->output_section->vma |
| 669 | + h->root.u.def.section->output_offset); |
| 670 | } |
| 671 | |
| 672 | /* For simplicity of coding, we are going to modify the section |
| 673 | contents, the section relocs, and the BFD symbol table. We |
| 674 | must tell the rest of the code not to free up this |
| 675 | information. It would be possible to instead create a table |
| 676 | of changes which have to be made, as is done in coff-mips.c; |
| 677 | that would be more work, but would require less memory when |
| 678 | the linker is run. */ |
| 679 | |
| 680 | /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative |
| 681 | branch/call. */ |
| 682 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24) |
| 683 | { |
| 684 | bfd_vma value = symval; |
| 685 | |
| 686 | /* Deal with pc-relative gunk. */ |
| 687 | value -= (sec->output_section->vma + sec->output_offset); |
| 688 | value -= (irel->r_offset + 3); |
| 689 | value += irel->r_addend; |
| 690 | |
| 691 | /* See if the value will fit in 16 bits, note the high value is |
| 692 | 0x7fff + 2 as the target will be two bytes closer if we are |
| 693 | able to relax. */ |
| 694 | if ((long) value < 0x8001 && (long) value > -0x8000) |
| 695 | { |
| 696 | unsigned char code; |
| 697 | |
| 698 | /* Get the opcode. */ |
| 699 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 700 | |
| 701 | if (code != 0xe0 && code != 0xe1) |
| 702 | continue; |
| 703 | |
| 704 | /* Note that we've changed the relocs, section contents, etc. */ |
| 705 | elf_section_data (sec)->relocs = internal_relocs; |
| 706 | elf_section_data (sec)->this_hdr.contents = contents; |
| 707 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 708 | |
| 709 | /* Fix the opcode. */ |
| 710 | if (code == 0xe0) |
| 711 | bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2); |
| 712 | else if (code == 0xe1) |
| 713 | bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2); |
| 714 | |
| 715 | /* Fix the relocation's type. */ |
| 716 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 717 | R_MN10200_PCREL16); |
| 718 | |
| 719 | /* The opcode got shorter too, so we have to fix the offset. */ |
| 720 | irel->r_offset -= 1; |
| 721 | |
| 722 | /* Delete two bytes of data. */ |
| 723 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
| 724 | irel->r_offset + 1, 2)) |
| 725 | goto error_return; |
| 726 | |
| 727 | /* That will change things, so, we should relax again. |
| 728 | Note that this is not required, and it may be slow. */ |
| 729 | *again = TRUE; |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative |
| 734 | branch. */ |
| 735 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16) |
| 736 | { |
| 737 | bfd_vma value = symval; |
| 738 | |
| 739 | /* Deal with pc-relative gunk. */ |
| 740 | value -= (sec->output_section->vma + sec->output_offset); |
| 741 | value -= (irel->r_offset + 2); |
| 742 | value += irel->r_addend; |
| 743 | |
| 744 | /* See if the value will fit in 8 bits, note the high value is |
| 745 | 0x7f + 1 as the target will be one bytes closer if we are |
| 746 | able to relax. */ |
| 747 | if ((long) value < 0x80 && (long) value > -0x80) |
| 748 | { |
| 749 | unsigned char code; |
| 750 | |
| 751 | /* Get the opcode. */ |
| 752 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 753 | |
| 754 | if (code != 0xfc) |
| 755 | continue; |
| 756 | |
| 757 | /* Note that we've changed the relocs, section contents, etc. */ |
| 758 | elf_section_data (sec)->relocs = internal_relocs; |
| 759 | elf_section_data (sec)->this_hdr.contents = contents; |
| 760 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 761 | |
| 762 | /* Fix the opcode. */ |
| 763 | bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1); |
| 764 | |
| 765 | /* Fix the relocation's type. */ |
| 766 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 767 | R_MN10200_PCREL8); |
| 768 | |
| 769 | /* Delete one byte of data. */ |
| 770 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
| 771 | irel->r_offset + 1, 1)) |
| 772 | goto error_return; |
| 773 | |
| 774 | /* That will change things, so, we should relax again. |
| 775 | Note that this is not required, and it may be slow. */ |
| 776 | *again = TRUE; |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | /* Try to eliminate an unconditional 8 bit pc-relative branch |
| 781 | which immediately follows a conditional 8 bit pc-relative |
| 782 | branch around the unconditional branch. |
| 783 | |
| 784 | original: new: |
| 785 | bCC lab1 bCC' lab2 |
| 786 | bra lab2 |
| 787 | lab1: lab1: |
| 788 | |
| 789 | This happens when the bCC can't reach lab2 at assembly time, |
| 790 | but due to other relaxations it can reach at link time. */ |
| 791 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8) |
| 792 | { |
| 793 | Elf_Internal_Rela *nrel; |
| 794 | bfd_vma value = symval; |
| 795 | unsigned char code; |
| 796 | |
| 797 | /* Deal with pc-relative gunk. */ |
| 798 | value -= (sec->output_section->vma + sec->output_offset); |
| 799 | value -= (irel->r_offset + 1); |
| 800 | value += irel->r_addend; |
| 801 | |
| 802 | /* Do nothing if this reloc is the last byte in the section. */ |
| 803 | if (irel->r_offset == sec->size) |
| 804 | continue; |
| 805 | |
| 806 | /* See if the next instruction is an unconditional pc-relative |
| 807 | branch, more often than not this test will fail, so we |
| 808 | test it first to speed things up. */ |
| 809 | code = bfd_get_8 (abfd, contents + irel->r_offset + 1); |
| 810 | if (code != 0xea) |
| 811 | continue; |
| 812 | |
| 813 | /* Also make sure the next relocation applies to the next |
| 814 | instruction and that it's a pc-relative 8 bit branch. */ |
| 815 | nrel = irel + 1; |
| 816 | if (nrel == irelend |
| 817 | || irel->r_offset + 2 != nrel->r_offset |
| 818 | || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8) |
| 819 | continue; |
| 820 | |
| 821 | /* Make sure our destination immediately follows the |
| 822 | unconditional branch. */ |
| 823 | if (symval != (sec->output_section->vma + sec->output_offset |
| 824 | + irel->r_offset + 3)) |
| 825 | continue; |
| 826 | |
| 827 | /* Now make sure we are a conditional branch. This may not |
| 828 | be necessary, but why take the chance. |
| 829 | |
| 830 | Note these checks assume that R_MN10200_PCREL8 relocs |
| 831 | only occur on bCC and bCCx insns. If they occured |
| 832 | elsewhere, we'd need to know the start of this insn |
| 833 | for this check to be accurate. */ |
| 834 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 835 | if (code != 0xe0 && code != 0xe1 && code != 0xe2 |
| 836 | && code != 0xe3 && code != 0xe4 && code != 0xe5 |
| 837 | && code != 0xe6 && code != 0xe7 && code != 0xe8 |
| 838 | && code != 0xe9 && code != 0xec && code != 0xed |
| 839 | && code != 0xee && code != 0xef && code != 0xfc |
| 840 | && code != 0xfd && code != 0xfe && code != 0xff) |
| 841 | continue; |
| 842 | |
| 843 | /* We also have to be sure there is no symbol/label |
| 844 | at the unconditional branch. */ |
| 845 | if (mn10200_elf_symbol_address_p (abfd, sec, isymbuf, |
| 846 | irel->r_offset + 1)) |
| 847 | continue; |
| 848 | |
| 849 | /* Note that we've changed the relocs, section contents, etc. */ |
| 850 | elf_section_data (sec)->relocs = internal_relocs; |
| 851 | elf_section_data (sec)->this_hdr.contents = contents; |
| 852 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 853 | |
| 854 | /* Reverse the condition of the first branch. */ |
| 855 | switch (code) |
| 856 | { |
| 857 | case 0xfc: |
| 858 | code = 0xfd; |
| 859 | break; |
| 860 | case 0xfd: |
| 861 | code = 0xfc; |
| 862 | break; |
| 863 | case 0xfe: |
| 864 | code = 0xff; |
| 865 | break; |
| 866 | case 0xff: |
| 867 | code = 0xfe; |
| 868 | break; |
| 869 | case 0xe8: |
| 870 | code = 0xe9; |
| 871 | break; |
| 872 | case 0xe9: |
| 873 | code = 0xe8; |
| 874 | break; |
| 875 | case 0xe0: |
| 876 | code = 0xe2; |
| 877 | break; |
| 878 | case 0xe2: |
| 879 | code = 0xe0; |
| 880 | break; |
| 881 | case 0xe3: |
| 882 | code = 0xe1; |
| 883 | break; |
| 884 | case 0xe1: |
| 885 | code = 0xe3; |
| 886 | break; |
| 887 | case 0xe4: |
| 888 | code = 0xe6; |
| 889 | break; |
| 890 | case 0xe6: |
| 891 | code = 0xe4; |
| 892 | break; |
| 893 | case 0xe7: |
| 894 | code = 0xe5; |
| 895 | break; |
| 896 | case 0xe5: |
| 897 | code = 0xe7; |
| 898 | break; |
| 899 | case 0xec: |
| 900 | code = 0xed; |
| 901 | break; |
| 902 | case 0xed: |
| 903 | code = 0xec; |
| 904 | break; |
| 905 | case 0xee: |
| 906 | code = 0xef; |
| 907 | break; |
| 908 | case 0xef: |
| 909 | code = 0xee; |
| 910 | break; |
| 911 | } |
| 912 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
| 913 | |
| 914 | /* Set the reloc type and symbol for the first branch |
| 915 | from the second branch. */ |
| 916 | irel->r_info = nrel->r_info; |
| 917 | |
| 918 | /* Make the reloc for the second branch a null reloc. */ |
| 919 | nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info), |
| 920 | R_MN10200_NONE); |
| 921 | |
| 922 | /* Delete two bytes of data. */ |
| 923 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
| 924 | irel->r_offset + 1, 2)) |
| 925 | goto error_return; |
| 926 | |
| 927 | /* That will change things, so, we should relax again. |
| 928 | Note that this is not required, and it may be slow. */ |
| 929 | *again = TRUE; |
| 930 | } |
| 931 | |
| 932 | /* Try to turn a 24bit immediate, displacement or absolute address |
| 933 | into a 16bit immediate, displacement or absolute address. */ |
| 934 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24) |
| 935 | { |
| 936 | bfd_vma value = symval; |
| 937 | |
| 938 | /* See if the value will fit in 16 bits. |
| 939 | We allow any 16bit match here. We prune those we can't |
| 940 | handle below. */ |
| 941 | if ((long) value < 0x7fff && (long) value > -0x8000) |
| 942 | { |
| 943 | unsigned char code; |
| 944 | |
| 945 | /* All insns which have 24bit operands are 5 bytes long, |
| 946 | the first byte will always be 0xf4, but we double check |
| 947 | it just in case. */ |
| 948 | |
| 949 | /* Get the first opcode. */ |
| 950 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); |
| 951 | |
| 952 | if (code != 0xf4) |
| 953 | continue; |
| 954 | |
| 955 | /* Get the second opcode. */ |
| 956 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 957 | |
| 958 | switch (code & 0xfc) |
| 959 | { |
| 960 | /* mov imm24,dn -> mov imm16,dn */ |
| 961 | case 0x70: |
| 962 | /* Not safe if the high bit is on as relaxing may |
| 963 | move the value out of high mem and thus not fit |
| 964 | in a signed 16bit value. */ |
| 965 | if (value & 0x8000) |
| 966 | continue; |
| 967 | |
| 968 | /* Note that we've changed the relocation contents, etc. */ |
| 969 | elf_section_data (sec)->relocs = internal_relocs; |
| 970 | elf_section_data (sec)->this_hdr.contents = contents; |
| 971 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 972 | |
| 973 | /* Fix the opcode. */ |
| 974 | bfd_put_8 (abfd, 0xf8 + (code & 0x03), |
| 975 | contents + irel->r_offset - 2); |
| 976 | |
| 977 | /* Fix the relocation's type. */ |
| 978 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 979 | R_MN10200_16); |
| 980 | |
| 981 | /* The opcode got shorter too, so we have to fix the |
| 982 | offset. */ |
| 983 | irel->r_offset -= 1; |
| 984 | |
| 985 | /* Delete two bytes of data. */ |
| 986 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
| 987 | irel->r_offset + 1, 2)) |
| 988 | goto error_return; |
| 989 | |
| 990 | /* That will change things, so, we should relax again. |
| 991 | Note that this is not required, and it may be slow. */ |
| 992 | *again = TRUE; |
| 993 | break; |
| 994 | |
| 995 | /* mov imm24,an -> mov imm16,an |
| 996 | cmp imm24,an -> cmp imm16,an |
| 997 | mov (abs24),dn -> mov (abs16),dn |
| 998 | mov dn,(abs24) -> mov dn,(abs16) |
| 999 | movb dn,(abs24) -> movb dn,(abs16) |
| 1000 | movbu (abs24),dn -> movbu (abs16),dn */ |
| 1001 | case 0x74: |
| 1002 | case 0x7c: |
| 1003 | case 0xc0: |
| 1004 | case 0x40: |
| 1005 | case 0x44: |
| 1006 | case 0xc8: |
| 1007 | /* Note that we've changed the relocation contents, etc. */ |
| 1008 | elf_section_data (sec)->relocs = internal_relocs; |
| 1009 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1010 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 1011 | |
| 1012 | if ((code & 0xfc) == 0x74) |
| 1013 | code = 0xdc + (code & 0x03); |
| 1014 | else if ((code & 0xfc) == 0x7c) |
| 1015 | code = 0xec + (code & 0x03); |
| 1016 | else if ((code & 0xfc) == 0xc0) |
| 1017 | code = 0xc8 + (code & 0x03); |
| 1018 | else if ((code & 0xfc) == 0x40) |
| 1019 | code = 0xc0 + (code & 0x03); |
| 1020 | else if ((code & 0xfc) == 0x44) |
| 1021 | code = 0xc4 + (code & 0x03); |
| 1022 | else if ((code & 0xfc) == 0xc8) |
| 1023 | code = 0xcc + (code & 0x03); |
| 1024 | |
| 1025 | /* Fix the opcode. */ |
| 1026 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); |
| 1027 | |
| 1028 | /* Fix the relocation's type. */ |
| 1029 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 1030 | R_MN10200_16); |
| 1031 | |
| 1032 | /* The opcode got shorter too, so we have to fix the |
| 1033 | offset. */ |
| 1034 | irel->r_offset -= 1; |
| 1035 | |
| 1036 | /* Delete two bytes of data. */ |
| 1037 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
| 1038 | irel->r_offset + 1, 2)) |
| 1039 | goto error_return; |
| 1040 | |
| 1041 | /* That will change things, so, we should relax again. |
| 1042 | Note that this is not required, and it may be slow. */ |
| 1043 | *again = TRUE; |
| 1044 | break; |
| 1045 | |
| 1046 | /* cmp imm24,dn -> cmp imm16,dn |
| 1047 | mov (abs24),an -> mov (abs16),an |
| 1048 | mov an,(abs24) -> mov an,(abs16) |
| 1049 | add imm24,dn -> add imm16,dn |
| 1050 | add imm24,an -> add imm16,an |
| 1051 | sub imm24,dn -> sub imm16,dn |
| 1052 | sub imm24,an -> sub imm16,an |
| 1053 | And all d24->d16 in memory ops. */ |
| 1054 | case 0x78: |
| 1055 | case 0xd0: |
| 1056 | case 0x50: |
| 1057 | case 0x60: |
| 1058 | case 0x64: |
| 1059 | case 0x68: |
| 1060 | case 0x6c: |
| 1061 | case 0x80: |
| 1062 | case 0xf0: |
| 1063 | case 0x00: |
| 1064 | case 0x10: |
| 1065 | case 0xb0: |
| 1066 | case 0x30: |
| 1067 | case 0xa0: |
| 1068 | case 0x20: |
| 1069 | case 0x90: |
| 1070 | /* Not safe if the high bit is on as relaxing may |
| 1071 | move the value out of high mem and thus not fit |
| 1072 | in a signed 16bit value. */ |
| 1073 | if (((code & 0xfc) == 0x78 |
| 1074 | || (code & 0xfc) == 0x60 |
| 1075 | || (code & 0xfc) == 0x64 |
| 1076 | || (code & 0xfc) == 0x68 |
| 1077 | || (code & 0xfc) == 0x6c |
| 1078 | || (code & 0xfc) == 0x80 |
| 1079 | || (code & 0xfc) == 0xf0 |
| 1080 | || (code & 0xfc) == 0x00 |
| 1081 | || (code & 0xfc) == 0x10 |
| 1082 | || (code & 0xfc) == 0xb0 |
| 1083 | || (code & 0xfc) == 0x30 |
| 1084 | || (code & 0xfc) == 0xa0 |
| 1085 | || (code & 0xfc) == 0x20 |
| 1086 | || (code & 0xfc) == 0x90) |
| 1087 | && (value & 0x8000) != 0) |
| 1088 | continue; |
| 1089 | |
| 1090 | /* Note that we've changed the relocation contents, etc. */ |
| 1091 | elf_section_data (sec)->relocs = internal_relocs; |
| 1092 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1093 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 1094 | |
| 1095 | /* Fix the opcode. */ |
| 1096 | bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2); |
| 1097 | |
| 1098 | if ((code & 0xfc) == 0x78) |
| 1099 | code = 0x48 + (code & 0x03); |
| 1100 | else if ((code & 0xfc) == 0xd0) |
| 1101 | code = 0x30 + (code & 0x03); |
| 1102 | else if ((code & 0xfc) == 0x50) |
| 1103 | code = 0x20 + (code & 0x03); |
| 1104 | else if ((code & 0xfc) == 0x60) |
| 1105 | code = 0x18 + (code & 0x03); |
| 1106 | else if ((code & 0xfc) == 0x64) |
| 1107 | code = 0x08 + (code & 0x03); |
| 1108 | else if ((code & 0xfc) == 0x68) |
| 1109 | code = 0x1c + (code & 0x03); |
| 1110 | else if ((code & 0xfc) == 0x6c) |
| 1111 | code = 0x0c + (code & 0x03); |
| 1112 | else if ((code & 0xfc) == 0x80) |
| 1113 | code = 0xc0 + (code & 0x07); |
| 1114 | else if ((code & 0xfc) == 0xf0) |
| 1115 | code = 0xb0 + (code & 0x07); |
| 1116 | else if ((code & 0xfc) == 0x00) |
| 1117 | code = 0x80 + (code & 0x07); |
| 1118 | else if ((code & 0xfc) == 0x10) |
| 1119 | code = 0xa0 + (code & 0x07); |
| 1120 | else if ((code & 0xfc) == 0xb0) |
| 1121 | code = 0x70 + (code & 0x07); |
| 1122 | else if ((code & 0xfc) == 0x30) |
| 1123 | code = 0x60 + (code & 0x07); |
| 1124 | else if ((code & 0xfc) == 0xa0) |
| 1125 | code = 0xd0 + (code & 0x07); |
| 1126 | else if ((code & 0xfc) == 0x20) |
| 1127 | code = 0x90 + (code & 0x07); |
| 1128 | else if ((code & 0xfc) == 0x90) |
| 1129 | code = 0x50 + (code & 0x07); |
| 1130 | |
| 1131 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
| 1132 | |
| 1133 | /* Fix the relocation's type. */ |
| 1134 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 1135 | R_MN10200_16); |
| 1136 | |
| 1137 | /* Delete one bytes of data. */ |
| 1138 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
| 1139 | irel->r_offset + 2, 1)) |
| 1140 | goto error_return; |
| 1141 | |
| 1142 | /* That will change things, so, we should relax again. |
| 1143 | Note that this is not required, and it may be slow. */ |
| 1144 | *again = TRUE; |
| 1145 | break; |
| 1146 | |
| 1147 | /* movb (abs24),dn ->movbu (abs16),dn extxb bn */ |
| 1148 | case 0xc4: |
| 1149 | /* Note that we've changed the reldection contents, etc. */ |
| 1150 | elf_section_data (sec)->relocs = internal_relocs; |
| 1151 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1152 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 1153 | |
| 1154 | bfd_put_8 (abfd, 0xcc + (code & 0x03), |
| 1155 | contents + irel->r_offset - 2); |
| 1156 | |
| 1157 | bfd_put_8 (abfd, 0xb8 + (code & 0x03), |
| 1158 | contents + irel->r_offset - 1); |
| 1159 | |
| 1160 | /* Fix the relocation's type. */ |
| 1161 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 1162 | R_MN10200_16); |
| 1163 | |
| 1164 | /* The reloc will be applied one byte in front of its |
| 1165 | current location. */ |
| 1166 | irel->r_offset -= 1; |
| 1167 | |
| 1168 | /* Delete one bytes of data. */ |
| 1169 | if (!mn10200_elf_relax_delete_bytes (abfd, sec, |
| 1170 | irel->r_offset + 2, 1)) |
| 1171 | goto error_return; |
| 1172 | |
| 1173 | /* That will change things, so, we should relax again. |
| 1174 | Note that this is not required, and it may be slow. */ |
| 1175 | *again = TRUE; |
| 1176 | break; |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | if (isymbuf != NULL |
| 1183 | && symtab_hdr->contents != (unsigned char *) isymbuf) |
| 1184 | { |
| 1185 | if (! link_info->keep_memory) |
| 1186 | free (isymbuf); |
| 1187 | else |
| 1188 | { |
| 1189 | /* Cache the symbols for elf_link_input_bfd. */ |
| 1190 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | if (contents != NULL |
| 1195 | && elf_section_data (sec)->this_hdr.contents != contents) |
| 1196 | { |
| 1197 | if (! link_info->keep_memory) |
| 1198 | free (contents); |
| 1199 | else |
| 1200 | { |
| 1201 | /* Cache the section contents for elf_link_input_bfd. */ |
| 1202 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | if (internal_relocs != NULL |
| 1207 | && elf_section_data (sec)->relocs != internal_relocs) |
| 1208 | free (internal_relocs); |
| 1209 | |
| 1210 | return TRUE; |
| 1211 | |
| 1212 | error_return: |
| 1213 | if (isymbuf != NULL |
| 1214 | && symtab_hdr->contents != (unsigned char *) isymbuf) |
| 1215 | free (isymbuf); |
| 1216 | if (contents != NULL |
| 1217 | && elf_section_data (sec)->this_hdr.contents != contents) |
| 1218 | free (contents); |
| 1219 | if (internal_relocs != NULL |
| 1220 | && elf_section_data (sec)->relocs != internal_relocs) |
| 1221 | free (internal_relocs); |
| 1222 | |
| 1223 | return FALSE; |
| 1224 | } |
| 1225 | |
| 1226 | /* Return TRUE if a symbol exists at the given address, else return |
| 1227 | FALSE. */ |
| 1228 | static bfd_boolean |
| 1229 | mn10200_elf_symbol_address_p (bfd *abfd, |
| 1230 | asection *sec, |
| 1231 | Elf_Internal_Sym *isym, |
| 1232 | bfd_vma addr) |
| 1233 | { |
| 1234 | Elf_Internal_Shdr *symtab_hdr; |
| 1235 | unsigned int sec_shndx; |
| 1236 | Elf_Internal_Sym *isymend; |
| 1237 | struct elf_link_hash_entry **sym_hashes; |
| 1238 | struct elf_link_hash_entry **end_hashes; |
| 1239 | unsigned int symcount; |
| 1240 | |
| 1241 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
| 1242 | |
| 1243 | /* Examine all the local symbols. */ |
| 1244 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 1245 | for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++) |
| 1246 | { |
| 1247 | if (isym->st_shndx == sec_shndx |
| 1248 | && isym->st_value == addr) |
| 1249 | return TRUE; |
| 1250 | } |
| 1251 | |
| 1252 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
| 1253 | - symtab_hdr->sh_info); |
| 1254 | sym_hashes = elf_sym_hashes (abfd); |
| 1255 | end_hashes = sym_hashes + symcount; |
| 1256 | for (; sym_hashes < end_hashes; sym_hashes++) |
| 1257 | { |
| 1258 | struct elf_link_hash_entry *sym_hash = *sym_hashes; |
| 1259 | if ((sym_hash->root.type == bfd_link_hash_defined |
| 1260 | || sym_hash->root.type == bfd_link_hash_defweak) |
| 1261 | && sym_hash->root.u.def.section == sec |
| 1262 | && sym_hash->root.u.def.value == addr) |
| 1263 | return TRUE; |
| 1264 | } |
| 1265 | |
| 1266 | return FALSE; |
| 1267 | } |
| 1268 | |
| 1269 | /* This is a version of bfd_generic_get_relocated_section_contents |
| 1270 | which uses mn10200_elf_relocate_section. */ |
| 1271 | |
| 1272 | static bfd_byte * |
| 1273 | mn10200_elf_get_relocated_section_contents (bfd *output_bfd, |
| 1274 | struct bfd_link_info *link_info, |
| 1275 | struct bfd_link_order *link_order, |
| 1276 | bfd_byte *data, |
| 1277 | bfd_boolean relocatable, |
| 1278 | asymbol **symbols) |
| 1279 | { |
| 1280 | Elf_Internal_Shdr *symtab_hdr; |
| 1281 | asection *input_section = link_order->u.indirect.section; |
| 1282 | bfd *input_bfd = input_section->owner; |
| 1283 | asection **sections = NULL; |
| 1284 | Elf_Internal_Rela *internal_relocs = NULL; |
| 1285 | Elf_Internal_Sym *isymbuf = NULL; |
| 1286 | |
| 1287 | /* We only need to handle the case of relaxing, or of having a |
| 1288 | particular set of section contents, specially. */ |
| 1289 | if (relocatable |
| 1290 | || elf_section_data (input_section)->this_hdr.contents == NULL) |
| 1291 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, |
| 1292 | link_order, data, |
| 1293 | relocatable, |
| 1294 | symbols); |
| 1295 | |
| 1296 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 1297 | |
| 1298 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, |
| 1299 | (size_t) input_section->size); |
| 1300 | |
| 1301 | if ((input_section->flags & SEC_RELOC) != 0 |
| 1302 | && input_section->reloc_count > 0) |
| 1303 | { |
| 1304 | Elf_Internal_Sym *isym; |
| 1305 | Elf_Internal_Sym *isymend; |
| 1306 | asection **secpp; |
| 1307 | bfd_size_type amt; |
| 1308 | |
| 1309 | internal_relocs = (_bfd_elf_link_read_relocs |
| 1310 | (input_bfd, input_section, NULL, |
| 1311 | (Elf_Internal_Rela *) NULL, FALSE)); |
| 1312 | if (internal_relocs == NULL) |
| 1313 | goto error_return; |
| 1314 | |
| 1315 | if (symtab_hdr->sh_info != 0) |
| 1316 | { |
| 1317 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; |
| 1318 | if (isymbuf == NULL) |
| 1319 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, |
| 1320 | symtab_hdr->sh_info, 0, |
| 1321 | NULL, NULL, NULL); |
| 1322 | if (isymbuf == NULL) |
| 1323 | goto error_return; |
| 1324 | } |
| 1325 | |
| 1326 | amt = symtab_hdr->sh_info; |
| 1327 | amt *= sizeof (asection *); |
| 1328 | sections = (asection **) bfd_malloc (amt); |
| 1329 | if (sections == NULL && amt != 0) |
| 1330 | goto error_return; |
| 1331 | |
| 1332 | isymend = isymbuf + symtab_hdr->sh_info; |
| 1333 | for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) |
| 1334 | { |
| 1335 | asection *isec; |
| 1336 | |
| 1337 | if (isym->st_shndx == SHN_UNDEF) |
| 1338 | isec = bfd_und_section_ptr; |
| 1339 | else if (isym->st_shndx == SHN_ABS) |
| 1340 | isec = bfd_abs_section_ptr; |
| 1341 | else if (isym->st_shndx == SHN_COMMON) |
| 1342 | isec = bfd_com_section_ptr; |
| 1343 | else |
| 1344 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); |
| 1345 | |
| 1346 | *secpp = isec; |
| 1347 | } |
| 1348 | |
| 1349 | if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd, |
| 1350 | input_section, data, internal_relocs, |
| 1351 | isymbuf, sections)) |
| 1352 | goto error_return; |
| 1353 | |
| 1354 | if (sections != NULL) |
| 1355 | free (sections); |
| 1356 | if (isymbuf != NULL |
| 1357 | && symtab_hdr->contents != (unsigned char *) isymbuf) |
| 1358 | free (isymbuf); |
| 1359 | if (elf_section_data (input_section)->relocs != internal_relocs) |
| 1360 | free (internal_relocs); |
| 1361 | } |
| 1362 | |
| 1363 | return data; |
| 1364 | |
| 1365 | error_return: |
| 1366 | if (sections != NULL) |
| 1367 | free (sections); |
| 1368 | if (isymbuf != NULL |
| 1369 | && symtab_hdr->contents != (unsigned char *) isymbuf) |
| 1370 | free (isymbuf); |
| 1371 | if (internal_relocs != NULL |
| 1372 | && elf_section_data (input_section)->relocs != internal_relocs) |
| 1373 | free (internal_relocs); |
| 1374 | return NULL; |
| 1375 | } |
| 1376 | |
| 1377 | #define TARGET_LITTLE_SYM mn10200_elf32_vec |
| 1378 | #define TARGET_LITTLE_NAME "elf32-mn10200" |
| 1379 | #define ELF_ARCH bfd_arch_mn10200 |
| 1380 | #define ELF_MACHINE_CODE EM_MN10200 |
| 1381 | #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200 |
| 1382 | #define ELF_MAXPAGESIZE 0x1000 |
| 1383 | |
| 1384 | #define elf_backend_rela_normal 1 |
| 1385 | #define elf_info_to_howto mn10200_info_to_howto |
| 1386 | #define elf_info_to_howto_rel 0 |
| 1387 | #define elf_backend_relocate_section mn10200_elf_relocate_section |
| 1388 | #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section |
| 1389 | #define bfd_elf32_bfd_get_relocated_section_contents \ |
| 1390 | mn10200_elf_get_relocated_section_contents |
| 1391 | |
| 1392 | #define elf_symbol_leading_char '_' |
| 1393 | |
| 1394 | #include "elf32-target.h" |