| 1 | /* Matsushita 10300 specific support for 32-bit ELF |
| 2 | Copyright 1996, 1997, 1998, 1999, 2000, 2001 |
| 3 | Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of BFD, the Binary File Descriptor library. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 20 | |
| 21 | #include "bfd.h" |
| 22 | #include "sysdep.h" |
| 23 | #include "libbfd.h" |
| 24 | #include "elf-bfd.h" |
| 25 | #include "elf/mn10300.h" |
| 26 | |
| 27 | struct elf32_mn10300_link_hash_entry { |
| 28 | /* The basic elf link hash table entry. */ |
| 29 | struct elf_link_hash_entry root; |
| 30 | |
| 31 | /* For function symbols, the number of times this function is |
| 32 | called directly (ie by name). */ |
| 33 | unsigned int direct_calls; |
| 34 | |
| 35 | /* For function symbols, the size of this function's stack |
| 36 | (if <= 255 bytes). We stuff this into "call" instructions |
| 37 | to this target when it's valid and profitable to do so. |
| 38 | |
| 39 | This does not include stack allocated by movm! */ |
| 40 | unsigned char stack_size; |
| 41 | |
| 42 | /* For function symbols, arguments (if any) for movm instruction |
| 43 | in the prologue. We stuff this value into "call" instructions |
| 44 | to the target when it's valid and profitable to do so. */ |
| 45 | unsigned char movm_args; |
| 46 | |
| 47 | /* For funtion symbols, the amount of stack space that would be allocated |
| 48 | by the movm instruction. This is redundant with movm_args, but we |
| 49 | add it to the hash table to avoid computing it over and over. */ |
| 50 | unsigned char movm_stack_size; |
| 51 | |
| 52 | /* When set, convert all "call" instructions to this target into "calls" |
| 53 | instructions. */ |
| 54 | #define MN10300_CONVERT_CALL_TO_CALLS 0x1 |
| 55 | |
| 56 | /* Used to mark functions which have had redundant parts of their |
| 57 | prologue deleted. */ |
| 58 | #define MN10300_DELETED_PROLOGUE_BYTES 0x2 |
| 59 | unsigned char flags; |
| 60 | }; |
| 61 | |
| 62 | /* We derive a hash table from the main elf linker hash table so |
| 63 | we can store state variables and a secondary hash table without |
| 64 | resorting to global variables. */ |
| 65 | struct elf32_mn10300_link_hash_table { |
| 66 | /* The main hash table. */ |
| 67 | struct elf_link_hash_table root; |
| 68 | |
| 69 | /* A hash table for static functions. We could derive a new hash table |
| 70 | instead of using the full elf32_mn10300_link_hash_table if we wanted |
| 71 | to save some memory. */ |
| 72 | struct elf32_mn10300_link_hash_table *static_hash_table; |
| 73 | |
| 74 | /* Random linker state flags. */ |
| 75 | #define MN10300_HASH_ENTRIES_INITIALIZED 0x1 |
| 76 | char flags; |
| 77 | }; |
| 78 | |
| 79 | /* For MN10300 linker hash table. */ |
| 80 | |
| 81 | /* Get the MN10300 ELF linker hash table from a link_info structure. */ |
| 82 | |
| 83 | #define elf32_mn10300_hash_table(p) \ |
| 84 | ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) |
| 85 | |
| 86 | #define elf32_mn10300_link_hash_traverse(table, func, info) \ |
| 87 | (elf_link_hash_traverse \ |
| 88 | (&(table)->root, \ |
| 89 | (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ |
| 90 | (info))) |
| 91 | |
| 92 | static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc |
| 93 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); |
| 94 | static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create |
| 95 | PARAMS ((bfd *)); |
| 96 | |
| 97 | static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup |
| 98 | PARAMS ((bfd *abfd, bfd_reloc_code_real_type code)); |
| 99 | static void mn10300_info_to_howto |
| 100 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *)); |
| 101 | static boolean mn10300_elf_check_relocs |
| 102 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
| 103 | const Elf_Internal_Rela *)); |
| 104 | static asection *mn10300_elf_gc_mark_hook |
| 105 | PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *, |
| 106 | struct elf_link_hash_entry *, Elf_Internal_Sym *)); |
| 107 | static boolean mn10300_elf_relax_delete_bytes |
| 108 | PARAMS ((bfd *, asection *, bfd_vma, int)); |
| 109 | static boolean mn10300_elf_symbol_address_p |
| 110 | PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma)); |
| 111 | static boolean elf32_mn10300_finish_hash_table_entry |
| 112 | PARAMS ((struct bfd_hash_entry *, PTR)); |
| 113 | static void compute_function_info |
| 114 | PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *, |
| 115 | bfd_vma, unsigned char *)); |
| 116 | |
| 117 | /* We have to use RELA instructions since md_apply_fix3 in the assembler |
| 118 | does absolutely nothing. */ |
| 119 | #define USE_RELA |
| 120 | |
| 121 | static reloc_howto_type elf_mn10300_howto_table[] = { |
| 122 | /* Dummy relocation. Does nothing. */ |
| 123 | HOWTO (R_MN10300_NONE, |
| 124 | 0, |
| 125 | 2, |
| 126 | 16, |
| 127 | false, |
| 128 | 0, |
| 129 | complain_overflow_bitfield, |
| 130 | bfd_elf_generic_reloc, |
| 131 | "R_MN10300_NONE", |
| 132 | false, |
| 133 | 0, |
| 134 | 0, |
| 135 | false), |
| 136 | /* Standard 32 bit reloc. */ |
| 137 | HOWTO (R_MN10300_32, |
| 138 | 0, |
| 139 | 2, |
| 140 | 32, |
| 141 | false, |
| 142 | 0, |
| 143 | complain_overflow_bitfield, |
| 144 | bfd_elf_generic_reloc, |
| 145 | "R_MN10300_32", |
| 146 | false, |
| 147 | 0xffffffff, |
| 148 | 0xffffffff, |
| 149 | false), |
| 150 | /* Standard 16 bit reloc. */ |
| 151 | HOWTO (R_MN10300_16, |
| 152 | 0, |
| 153 | 1, |
| 154 | 16, |
| 155 | false, |
| 156 | 0, |
| 157 | complain_overflow_bitfield, |
| 158 | bfd_elf_generic_reloc, |
| 159 | "R_MN10300_16", |
| 160 | false, |
| 161 | 0xffff, |
| 162 | 0xffff, |
| 163 | false), |
| 164 | /* Standard 8 bit reloc. */ |
| 165 | HOWTO (R_MN10300_8, |
| 166 | 0, |
| 167 | 0, |
| 168 | 8, |
| 169 | false, |
| 170 | 0, |
| 171 | complain_overflow_bitfield, |
| 172 | bfd_elf_generic_reloc, |
| 173 | "R_MN10300_8", |
| 174 | false, |
| 175 | 0xff, |
| 176 | 0xff, |
| 177 | false), |
| 178 | /* Standard 32bit pc-relative reloc. */ |
| 179 | HOWTO (R_MN10300_PCREL32, |
| 180 | 0, |
| 181 | 2, |
| 182 | 32, |
| 183 | true, |
| 184 | 0, |
| 185 | complain_overflow_bitfield, |
| 186 | bfd_elf_generic_reloc, |
| 187 | "R_MN10300_PCREL32", |
| 188 | false, |
| 189 | 0xffffffff, |
| 190 | 0xffffffff, |
| 191 | true), |
| 192 | /* Standard 16bit pc-relative reloc. */ |
| 193 | HOWTO (R_MN10300_PCREL16, |
| 194 | 0, |
| 195 | 1, |
| 196 | 16, |
| 197 | true, |
| 198 | 0, |
| 199 | complain_overflow_bitfield, |
| 200 | bfd_elf_generic_reloc, |
| 201 | "R_MN10300_PCREL16", |
| 202 | false, |
| 203 | 0xffff, |
| 204 | 0xffff, |
| 205 | true), |
| 206 | /* Standard 8 pc-relative reloc. */ |
| 207 | HOWTO (R_MN10300_PCREL8, |
| 208 | 0, |
| 209 | 0, |
| 210 | 8, |
| 211 | true, |
| 212 | 0, |
| 213 | complain_overflow_bitfield, |
| 214 | bfd_elf_generic_reloc, |
| 215 | "R_MN10300_PCREL8", |
| 216 | false, |
| 217 | 0xff, |
| 218 | 0xff, |
| 219 | true), |
| 220 | |
| 221 | /* GNU extension to record C++ vtable hierarchy */ |
| 222 | HOWTO (R_MN10300_GNU_VTINHERIT, /* type */ |
| 223 | 0, /* rightshift */ |
| 224 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 225 | 0, /* bitsize */ |
| 226 | false, /* pc_relative */ |
| 227 | 0, /* bitpos */ |
| 228 | complain_overflow_dont, /* complain_on_overflow */ |
| 229 | NULL, /* special_function */ |
| 230 | "R_MN10300_GNU_VTINHERIT", /* name */ |
| 231 | false, /* partial_inplace */ |
| 232 | 0, /* src_mask */ |
| 233 | 0, /* dst_mask */ |
| 234 | false), /* pcrel_offset */ |
| 235 | |
| 236 | /* GNU extension to record C++ vtable member usage */ |
| 237 | HOWTO (R_MN10300_GNU_VTENTRY, /* type */ |
| 238 | 0, /* rightshift */ |
| 239 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 240 | 0, /* bitsize */ |
| 241 | false, /* pc_relative */ |
| 242 | 0, /* bitpos */ |
| 243 | complain_overflow_dont, /* complain_on_overflow */ |
| 244 | NULL, /* special_function */ |
| 245 | "R_MN10300_GNU_VTENTRY", /* name */ |
| 246 | false, /* partial_inplace */ |
| 247 | 0, /* src_mask */ |
| 248 | 0, /* dst_mask */ |
| 249 | false), /* pcrel_offset */ |
| 250 | |
| 251 | /* Standard 24 bit reloc. */ |
| 252 | HOWTO (R_MN10300_24, |
| 253 | 0, |
| 254 | 2, |
| 255 | 24, |
| 256 | false, |
| 257 | 0, |
| 258 | complain_overflow_bitfield, |
| 259 | bfd_elf_generic_reloc, |
| 260 | "R_MN10300_24", |
| 261 | false, |
| 262 | 0xffffff, |
| 263 | 0xffffff, |
| 264 | false), |
| 265 | }; |
| 266 | |
| 267 | struct mn10300_reloc_map { |
| 268 | bfd_reloc_code_real_type bfd_reloc_val; |
| 269 | unsigned char elf_reloc_val; |
| 270 | }; |
| 271 | |
| 272 | static const struct mn10300_reloc_map mn10300_reloc_map[] = { |
| 273 | { BFD_RELOC_NONE, R_MN10300_NONE, }, |
| 274 | { BFD_RELOC_32, R_MN10300_32, }, |
| 275 | { BFD_RELOC_16, R_MN10300_16, }, |
| 276 | { BFD_RELOC_8, R_MN10300_8, }, |
| 277 | { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, }, |
| 278 | { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, }, |
| 279 | { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, }, |
| 280 | { BFD_RELOC_24, R_MN10300_24, }, |
| 281 | { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT }, |
| 282 | { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY }, |
| 283 | }; |
| 284 | |
| 285 | static reloc_howto_type * |
| 286 | bfd_elf32_bfd_reloc_type_lookup (abfd, code) |
| 287 | bfd *abfd ATTRIBUTE_UNUSED; |
| 288 | bfd_reloc_code_real_type code; |
| 289 | { |
| 290 | unsigned int i; |
| 291 | |
| 292 | for (i = 0; |
| 293 | i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map); |
| 294 | i++) |
| 295 | { |
| 296 | if (mn10300_reloc_map[i].bfd_reloc_val == code) |
| 297 | return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val]; |
| 298 | } |
| 299 | |
| 300 | return NULL; |
| 301 | } |
| 302 | |
| 303 | /* Set the howto pointer for an MN10300 ELF reloc. */ |
| 304 | |
| 305 | static void |
| 306 | mn10300_info_to_howto (abfd, cache_ptr, dst) |
| 307 | bfd *abfd ATTRIBUTE_UNUSED; |
| 308 | arelent *cache_ptr; |
| 309 | Elf32_Internal_Rela *dst; |
| 310 | { |
| 311 | unsigned int r_type; |
| 312 | |
| 313 | r_type = ELF32_R_TYPE (dst->r_info); |
| 314 | BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX); |
| 315 | cache_ptr->howto = &elf_mn10300_howto_table[r_type]; |
| 316 | } |
| 317 | |
| 318 | /* Look through the relocs for a section during the first phase. |
| 319 | Since we don't do .gots or .plts, we just need to consider the |
| 320 | virtual table relocs for gc. */ |
| 321 | |
| 322 | static boolean |
| 323 | mn10300_elf_check_relocs (abfd, info, sec, relocs) |
| 324 | bfd *abfd; |
| 325 | struct bfd_link_info *info; |
| 326 | asection *sec; |
| 327 | const Elf_Internal_Rela *relocs; |
| 328 | { |
| 329 | Elf_Internal_Shdr *symtab_hdr; |
| 330 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; |
| 331 | const Elf_Internal_Rela *rel; |
| 332 | const Elf_Internal_Rela *rel_end; |
| 333 | |
| 334 | if (info->relocateable) |
| 335 | return true; |
| 336 | |
| 337 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 338 | sym_hashes = elf_sym_hashes (abfd); |
| 339 | sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym); |
| 340 | if (!elf_bad_symtab (abfd)) |
| 341 | sym_hashes_end -= symtab_hdr->sh_info; |
| 342 | |
| 343 | rel_end = relocs + sec->reloc_count; |
| 344 | for (rel = relocs; rel < rel_end; rel++) |
| 345 | { |
| 346 | struct elf_link_hash_entry *h; |
| 347 | unsigned long r_symndx; |
| 348 | |
| 349 | r_symndx = ELF32_R_SYM (rel->r_info); |
| 350 | if (r_symndx < symtab_hdr->sh_info) |
| 351 | h = NULL; |
| 352 | else |
| 353 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
| 354 | |
| 355 | switch (ELF32_R_TYPE (rel->r_info)) |
| 356 | { |
| 357 | /* This relocation describes the C++ object vtable hierarchy. |
| 358 | Reconstruct it for later use during GC. */ |
| 359 | case R_MN10300_GNU_VTINHERIT: |
| 360 | if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) |
| 361 | return false; |
| 362 | break; |
| 363 | |
| 364 | /* This relocation describes which C++ vtable entries are actually |
| 365 | used. Record for later use during GC. */ |
| 366 | case R_MN10300_GNU_VTENTRY: |
| 367 | if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend)) |
| 368 | return false; |
| 369 | break; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return true; |
| 374 | } |
| 375 | |
| 376 | /* Return the section that should be marked against GC for a given |
| 377 | relocation. */ |
| 378 | |
| 379 | static asection * |
| 380 | mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym) |
| 381 | bfd *abfd; |
| 382 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
| 383 | Elf_Internal_Rela *rel; |
| 384 | struct elf_link_hash_entry *h; |
| 385 | Elf_Internal_Sym *sym; |
| 386 | { |
| 387 | if (h != NULL) |
| 388 | { |
| 389 | switch (ELF32_R_TYPE (rel->r_info)) |
| 390 | { |
| 391 | case R_MN10300_GNU_VTINHERIT: |
| 392 | case R_MN10300_GNU_VTENTRY: |
| 393 | break; |
| 394 | |
| 395 | default: |
| 396 | switch (h->root.type) |
| 397 | { |
| 398 | case bfd_link_hash_defined: |
| 399 | case bfd_link_hash_defweak: |
| 400 | return h->root.u.def.section; |
| 401 | |
| 402 | case bfd_link_hash_common: |
| 403 | return h->root.u.c.p->section; |
| 404 | |
| 405 | default: |
| 406 | break; |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | else |
| 411 | { |
| 412 | if (!(elf_bad_symtab (abfd) |
| 413 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) |
| 414 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) |
| 415 | && sym->st_shndx != SHN_COMMON)) |
| 416 | { |
| 417 | return bfd_section_from_elf_index (abfd, sym->st_shndx); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | return NULL; |
| 422 | } |
| 423 | |
| 424 | /* Perform a relocation as part of a final link. */ |
| 425 | static bfd_reloc_status_type |
| 426 | mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd, |
| 427 | input_section, contents, offset, value, |
| 428 | addend, info, sym_sec, is_local) |
| 429 | reloc_howto_type *howto; |
| 430 | bfd *input_bfd; |
| 431 | bfd *output_bfd ATTRIBUTE_UNUSED; |
| 432 | asection *input_section; |
| 433 | bfd_byte *contents; |
| 434 | bfd_vma offset; |
| 435 | bfd_vma value; |
| 436 | bfd_vma addend; |
| 437 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
| 438 | asection *sym_sec ATTRIBUTE_UNUSED; |
| 439 | int is_local ATTRIBUTE_UNUSED; |
| 440 | { |
| 441 | unsigned long r_type = howto->type; |
| 442 | bfd_byte *hit_data = contents + offset; |
| 443 | |
| 444 | switch (r_type) |
| 445 | { |
| 446 | case R_MN10300_NONE: |
| 447 | return bfd_reloc_ok; |
| 448 | |
| 449 | case R_MN10300_32: |
| 450 | value += addend; |
| 451 | bfd_put_32 (input_bfd, value, hit_data); |
| 452 | return bfd_reloc_ok; |
| 453 | |
| 454 | case R_MN10300_24: |
| 455 | value += addend; |
| 456 | |
| 457 | if ((long) value > 0x7fffff || (long) value < -0x800000) |
| 458 | return bfd_reloc_overflow; |
| 459 | |
| 460 | bfd_put_8 (input_bfd, value & 0xff, hit_data); |
| 461 | bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1); |
| 462 | bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2); |
| 463 | return bfd_reloc_ok; |
| 464 | |
| 465 | case R_MN10300_16: |
| 466 | value += addend; |
| 467 | |
| 468 | if ((long) value > 0x7fff || (long) value < -0x8000) |
| 469 | return bfd_reloc_overflow; |
| 470 | |
| 471 | bfd_put_16 (input_bfd, value, hit_data); |
| 472 | return bfd_reloc_ok; |
| 473 | |
| 474 | case R_MN10300_8: |
| 475 | value += addend; |
| 476 | |
| 477 | if ((long) value > 0x7f || (long) value < -0x80) |
| 478 | return bfd_reloc_overflow; |
| 479 | |
| 480 | bfd_put_8 (input_bfd, value, hit_data); |
| 481 | return bfd_reloc_ok; |
| 482 | |
| 483 | case R_MN10300_PCREL8: |
| 484 | value -= (input_section->output_section->vma |
| 485 | + input_section->output_offset); |
| 486 | value -= offset; |
| 487 | value += addend; |
| 488 | |
| 489 | if ((long) value > 0xff || (long) value < -0x100) |
| 490 | return bfd_reloc_overflow; |
| 491 | |
| 492 | bfd_put_8 (input_bfd, value, hit_data); |
| 493 | return bfd_reloc_ok; |
| 494 | |
| 495 | case R_MN10300_PCREL16: |
| 496 | value -= (input_section->output_section->vma |
| 497 | + input_section->output_offset); |
| 498 | value -= offset; |
| 499 | value += addend; |
| 500 | |
| 501 | if ((long) value > 0xffff || (long) value < -0x10000) |
| 502 | return bfd_reloc_overflow; |
| 503 | |
| 504 | bfd_put_16 (input_bfd, value, hit_data); |
| 505 | return bfd_reloc_ok; |
| 506 | |
| 507 | case R_MN10300_PCREL32: |
| 508 | value -= (input_section->output_section->vma |
| 509 | + input_section->output_offset); |
| 510 | value -= offset; |
| 511 | value += addend; |
| 512 | |
| 513 | bfd_put_32 (input_bfd, value, hit_data); |
| 514 | return bfd_reloc_ok; |
| 515 | |
| 516 | case R_MN10300_GNU_VTINHERIT: |
| 517 | case R_MN10300_GNU_VTENTRY: |
| 518 | return bfd_reloc_ok; |
| 519 | |
| 520 | default: |
| 521 | return bfd_reloc_notsupported; |
| 522 | } |
| 523 | } |
| 524 | \f |
| 525 | /* Relocate an MN10300 ELF section. */ |
| 526 | static boolean |
| 527 | mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section, |
| 528 | contents, relocs, local_syms, local_sections) |
| 529 | bfd *output_bfd; |
| 530 | struct bfd_link_info *info; |
| 531 | bfd *input_bfd; |
| 532 | asection *input_section; |
| 533 | bfd_byte *contents; |
| 534 | Elf_Internal_Rela *relocs; |
| 535 | Elf_Internal_Sym *local_syms; |
| 536 | asection **local_sections; |
| 537 | { |
| 538 | Elf_Internal_Shdr *symtab_hdr; |
| 539 | struct elf32_mn10300_link_hash_entry **sym_hashes; |
| 540 | Elf_Internal_Rela *rel, *relend; |
| 541 | |
| 542 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 543 | sym_hashes = (struct elf32_mn10300_link_hash_entry **) |
| 544 | (elf_sym_hashes (input_bfd)); |
| 545 | |
| 546 | rel = relocs; |
| 547 | relend = relocs + input_section->reloc_count; |
| 548 | for (; rel < relend; rel++) |
| 549 | { |
| 550 | int r_type; |
| 551 | reloc_howto_type *howto; |
| 552 | unsigned long r_symndx; |
| 553 | Elf_Internal_Sym *sym; |
| 554 | asection *sec; |
| 555 | struct elf32_mn10300_link_hash_entry *h; |
| 556 | bfd_vma relocation; |
| 557 | bfd_reloc_status_type r; |
| 558 | |
| 559 | r_symndx = ELF32_R_SYM (rel->r_info); |
| 560 | r_type = ELF32_R_TYPE (rel->r_info); |
| 561 | howto = elf_mn10300_howto_table + r_type; |
| 562 | |
| 563 | /* Just skip the vtable gc relocs. */ |
| 564 | if (r_type == R_MN10300_GNU_VTINHERIT |
| 565 | || r_type == R_MN10300_GNU_VTENTRY) |
| 566 | continue; |
| 567 | |
| 568 | if (info->relocateable) |
| 569 | { |
| 570 | /* This is a relocateable link. We don't have to change |
| 571 | anything, unless the reloc is against a section symbol, |
| 572 | in which case we have to adjust according to where the |
| 573 | section symbol winds up in the output section. */ |
| 574 | if (r_symndx < symtab_hdr->sh_info) |
| 575 | { |
| 576 | sym = local_syms + r_symndx; |
| 577 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) |
| 578 | { |
| 579 | sec = local_sections[r_symndx]; |
| 580 | rel->r_addend += sec->output_offset + sym->st_value; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | continue; |
| 585 | } |
| 586 | |
| 587 | /* This is a final link. */ |
| 588 | h = NULL; |
| 589 | sym = NULL; |
| 590 | sec = NULL; |
| 591 | if (r_symndx < symtab_hdr->sh_info) |
| 592 | { |
| 593 | sym = local_syms + r_symndx; |
| 594 | sec = local_sections[r_symndx]; |
| 595 | relocation = (sec->output_section->vma |
| 596 | + sec->output_offset |
| 597 | + sym->st_value); |
| 598 | } |
| 599 | else |
| 600 | { |
| 601 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
| 602 | while (h->root.type == bfd_link_hash_indirect |
| 603 | || h->root.type == bfd_link_hash_warning) |
| 604 | h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link; |
| 605 | if (h->root.root.type == bfd_link_hash_defined |
| 606 | || h->root.root.type == bfd_link_hash_defweak) |
| 607 | { |
| 608 | sec = h->root.root.u.def.section; |
| 609 | relocation = (h->root.root.u.def.value |
| 610 | + sec->output_section->vma |
| 611 | + sec->output_offset); |
| 612 | } |
| 613 | else if (h->root.root.type == bfd_link_hash_undefweak) |
| 614 | relocation = 0; |
| 615 | else |
| 616 | { |
| 617 | if (! ((*info->callbacks->undefined_symbol) |
| 618 | (info, h->root.root.root.string, input_bfd, |
| 619 | input_section, rel->r_offset, true))) |
| 620 | return false; |
| 621 | relocation = 0; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd, |
| 626 | input_section, |
| 627 | contents, rel->r_offset, |
| 628 | relocation, rel->r_addend, |
| 629 | info, sec, h == NULL); |
| 630 | |
| 631 | if (r != bfd_reloc_ok) |
| 632 | { |
| 633 | const char *name; |
| 634 | const char *msg = (const char *) 0; |
| 635 | |
| 636 | if (h != NULL) |
| 637 | name = h->root.root.root.string; |
| 638 | else |
| 639 | { |
| 640 | name = (bfd_elf_string_from_elf_section |
| 641 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); |
| 642 | if (name == NULL || *name == '\0') |
| 643 | name = bfd_section_name (input_bfd, sec); |
| 644 | } |
| 645 | |
| 646 | switch (r) |
| 647 | { |
| 648 | case bfd_reloc_overflow: |
| 649 | if (! ((*info->callbacks->reloc_overflow) |
| 650 | (info, name, howto->name, (bfd_vma) 0, |
| 651 | input_bfd, input_section, rel->r_offset))) |
| 652 | return false; |
| 653 | break; |
| 654 | |
| 655 | case bfd_reloc_undefined: |
| 656 | if (! ((*info->callbacks->undefined_symbol) |
| 657 | (info, name, input_bfd, input_section, |
| 658 | rel->r_offset, true))) |
| 659 | return false; |
| 660 | break; |
| 661 | |
| 662 | case bfd_reloc_outofrange: |
| 663 | msg = _("internal error: out of range error"); |
| 664 | goto common_error; |
| 665 | |
| 666 | case bfd_reloc_notsupported: |
| 667 | msg = _("internal error: unsupported relocation error"); |
| 668 | goto common_error; |
| 669 | |
| 670 | case bfd_reloc_dangerous: |
| 671 | msg = _("internal error: dangerous error"); |
| 672 | goto common_error; |
| 673 | |
| 674 | default: |
| 675 | msg = _("internal error: unknown error"); |
| 676 | /* fall through */ |
| 677 | |
| 678 | common_error: |
| 679 | if (!((*info->callbacks->warning) |
| 680 | (info, msg, name, input_bfd, input_section, |
| 681 | rel->r_offset))) |
| 682 | return false; |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | return true; |
| 689 | } |
| 690 | |
| 691 | /* Finish initializing one hash table entry. */ |
| 692 | static boolean |
| 693 | elf32_mn10300_finish_hash_table_entry (gen_entry, in_args) |
| 694 | struct bfd_hash_entry *gen_entry; |
| 695 | PTR in_args ATTRIBUTE_UNUSED; |
| 696 | { |
| 697 | struct elf32_mn10300_link_hash_entry *entry; |
| 698 | unsigned int byte_count = 0; |
| 699 | |
| 700 | entry = (struct elf32_mn10300_link_hash_entry *) gen_entry; |
| 701 | |
| 702 | /* If we already know we want to convert "call" to "calls" for calls |
| 703 | to this symbol, then return now. */ |
| 704 | if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS) |
| 705 | return true; |
| 706 | |
| 707 | /* If there are no named calls to this symbol, or there's nothing we |
| 708 | can move from the function itself into the "call" instruction, then |
| 709 | note that all "call" instructions should be converted into "calls" |
| 710 | instructions and return. */ |
| 711 | if (entry->direct_calls == 0 |
| 712 | || (entry->stack_size == 0 && entry->movm_args == 0)) |
| 713 | { |
| 714 | /* Make a note that we should convert "call" instructions to "calls" |
| 715 | instructions for calls to this symbol. */ |
| 716 | entry->flags |= MN10300_CONVERT_CALL_TO_CALLS; |
| 717 | return true; |
| 718 | } |
| 719 | |
| 720 | /* We may be able to move some instructions from the function itself into |
| 721 | the "call" instruction. Count how many bytes we might be able to |
| 722 | eliminate in the function itself. */ |
| 723 | |
| 724 | /* A movm instruction is two bytes. */ |
| 725 | if (entry->movm_args) |
| 726 | byte_count += 2; |
| 727 | |
| 728 | /* Count the insn to allocate stack space too. */ |
| 729 | if (entry->stack_size > 0 && entry->stack_size <= 128) |
| 730 | byte_count += 3; |
| 731 | else if (entry->stack_size > 0 && entry->stack_size < 256) |
| 732 | byte_count += 4; |
| 733 | |
| 734 | /* If using "call" will result in larger code, then turn all |
| 735 | the associated "call" instructions into "calls" instrutions. */ |
| 736 | if (byte_count < entry->direct_calls) |
| 737 | entry->flags |= MN10300_CONVERT_CALL_TO_CALLS; |
| 738 | |
| 739 | /* This routine never fails. */ |
| 740 | return true; |
| 741 | } |
| 742 | |
| 743 | /* This function handles relaxing for the mn10300. |
| 744 | |
| 745 | There's quite a few relaxing opportunites available on the mn10300: |
| 746 | |
| 747 | * calls:32 -> calls:16 2 bytes |
| 748 | * call:32 -> call:16 2 bytes |
| 749 | |
| 750 | * call:32 -> calls:32 1 byte |
| 751 | * call:16 -> calls:16 1 byte |
| 752 | * These are done anytime using "calls" would result |
| 753 | in smaller code, or when necessary to preserve the |
| 754 | meaning of the program. |
| 755 | |
| 756 | * call:32 varies |
| 757 | * call:16 |
| 758 | * In some circumstances we can move instructions |
| 759 | from a function prologue into a "call" instruction. |
| 760 | This is only done if the resulting code is no larger |
| 761 | than the original code. |
| 762 | |
| 763 | * jmp:32 -> jmp:16 2 bytes |
| 764 | * jmp:16 -> bra:8 1 byte |
| 765 | |
| 766 | * If the previous instruction is a conditional branch |
| 767 | around the jump/bra, we may be able to reverse its condition |
| 768 | and change its target to the jump's target. The jump/bra |
| 769 | can then be deleted. 2 bytes |
| 770 | |
| 771 | * mov abs32 -> mov abs16 1 or 2 bytes |
| 772 | |
| 773 | * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes |
| 774 | - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes |
| 775 | |
| 776 | * Most instructions which accept d32 can relax to d16 1 or 2 bytes |
| 777 | - Most instructions which accept d16 can relax to d8 1 or 2 bytes |
| 778 | |
| 779 | We don't handle imm16->imm8 or d16->d8 as they're very rare |
| 780 | and somewhat more difficult to support. */ |
| 781 | |
| 782 | static boolean |
| 783 | mn10300_elf_relax_section (abfd, sec, link_info, again) |
| 784 | bfd *abfd; |
| 785 | asection *sec; |
| 786 | struct bfd_link_info *link_info; |
| 787 | boolean *again; |
| 788 | { |
| 789 | Elf_Internal_Shdr *symtab_hdr; |
| 790 | Elf_Internal_Rela *internal_relocs = NULL; |
| 791 | Elf_Internal_Rela *free_relocs = NULL; |
| 792 | Elf_Internal_Rela *irel, *irelend; |
| 793 | bfd_byte *contents = NULL; |
| 794 | bfd_byte *free_contents = NULL; |
| 795 | Elf32_External_Sym *extsyms = NULL; |
| 796 | Elf32_External_Sym *free_extsyms = NULL; |
| 797 | struct elf32_mn10300_link_hash_table *hash_table; |
| 798 | |
| 799 | /* Assume nothing changes. */ |
| 800 | *again = false; |
| 801 | |
| 802 | /* We need a pointer to the mn10300 specific hash table. */ |
| 803 | hash_table = elf32_mn10300_hash_table (link_info); |
| 804 | |
| 805 | /* Initialize fields in each hash table entry the first time through. */ |
| 806 | if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0) |
| 807 | { |
| 808 | bfd *input_bfd; |
| 809 | |
| 810 | /* Iterate over all the input bfds. */ |
| 811 | for (input_bfd = link_info->input_bfds; |
| 812 | input_bfd != NULL; |
| 813 | input_bfd = input_bfd->link_next) |
| 814 | { |
| 815 | asection *section; |
| 816 | |
| 817 | /* We're going to need all the symbols for each bfd. */ |
| 818 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 819 | |
| 820 | /* Get cached copy if it exists. */ |
| 821 | if (symtab_hdr->contents != NULL) |
| 822 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; |
| 823 | else |
| 824 | { |
| 825 | /* Go get them off disk. */ |
| 826 | extsyms = ((Elf32_External_Sym *) |
| 827 | bfd_malloc (symtab_hdr->sh_size)); |
| 828 | if (extsyms == NULL) |
| 829 | goto error_return; |
| 830 | free_extsyms = extsyms; |
| 831 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 |
| 832 | || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd) |
| 833 | != symtab_hdr->sh_size)) |
| 834 | goto error_return; |
| 835 | } |
| 836 | |
| 837 | /* Iterate over each section in this bfd. */ |
| 838 | for (section = input_bfd->sections; |
| 839 | section != NULL; |
| 840 | section = section->next) |
| 841 | { |
| 842 | struct elf32_mn10300_link_hash_entry *hash; |
| 843 | Elf_Internal_Sym *sym; |
| 844 | asection *sym_sec = NULL; |
| 845 | const char *sym_name; |
| 846 | char *new_name; |
| 847 | |
| 848 | /* Get cached copy of section contents if it exists. */ |
| 849 | if (elf_section_data (section)->this_hdr.contents != NULL) |
| 850 | contents = elf_section_data (section)->this_hdr.contents; |
| 851 | else if (section->_raw_size != 0) |
| 852 | { |
| 853 | /* Go get them off disk. */ |
| 854 | contents = (bfd_byte *) bfd_malloc (section->_raw_size); |
| 855 | if (contents == NULL) |
| 856 | goto error_return; |
| 857 | free_contents = contents; |
| 858 | |
| 859 | if (!bfd_get_section_contents (input_bfd, section, |
| 860 | contents, (file_ptr) 0, |
| 861 | section->_raw_size)) |
| 862 | goto error_return; |
| 863 | } |
| 864 | else |
| 865 | { |
| 866 | contents = NULL; |
| 867 | free_contents = NULL; |
| 868 | } |
| 869 | |
| 870 | /* If there aren't any relocs, then there's nothing to do. */ |
| 871 | if ((section->flags & SEC_RELOC) != 0 |
| 872 | && section->reloc_count != 0) |
| 873 | { |
| 874 | |
| 875 | /* Get a copy of the native relocations. */ |
| 876 | internal_relocs = (_bfd_elf32_link_read_relocs |
| 877 | (input_bfd, section, (PTR) NULL, |
| 878 | (Elf_Internal_Rela *) NULL, |
| 879 | link_info->keep_memory)); |
| 880 | if (internal_relocs == NULL) |
| 881 | goto error_return; |
| 882 | if (! link_info->keep_memory) |
| 883 | free_relocs = internal_relocs; |
| 884 | |
| 885 | /* Now examine each relocation. */ |
| 886 | irel = internal_relocs; |
| 887 | irelend = irel + section->reloc_count; |
| 888 | for (; irel < irelend; irel++) |
| 889 | { |
| 890 | long r_type; |
| 891 | unsigned long r_index; |
| 892 | unsigned char code; |
| 893 | |
| 894 | r_type = ELF32_R_TYPE (irel->r_info); |
| 895 | r_index = ELF32_R_SYM (irel->r_info); |
| 896 | |
| 897 | if (r_type < 0 || r_type >= (int) R_MN10300_MAX) |
| 898 | goto error_return; |
| 899 | |
| 900 | /* We need the name and hash table entry of the target |
| 901 | symbol! */ |
| 902 | hash = NULL; |
| 903 | sym = NULL; |
| 904 | sym_sec = NULL; |
| 905 | |
| 906 | if (r_index < symtab_hdr->sh_info) |
| 907 | { |
| 908 | /* A local symbol. */ |
| 909 | Elf_Internal_Sym isym; |
| 910 | |
| 911 | bfd_elf32_swap_symbol_in (input_bfd, |
| 912 | extsyms + r_index, &isym); |
| 913 | |
| 914 | if (isym.st_shndx == SHN_UNDEF) |
| 915 | sym_sec = bfd_und_section_ptr; |
| 916 | else if (isym.st_shndx > 0 |
| 917 | && isym.st_shndx < SHN_LORESERVE) |
| 918 | sym_sec |
| 919 | = bfd_section_from_elf_index (input_bfd, |
| 920 | isym.st_shndx); |
| 921 | else if (isym.st_shndx == SHN_ABS) |
| 922 | sym_sec = bfd_abs_section_ptr; |
| 923 | else if (isym.st_shndx == SHN_COMMON) |
| 924 | sym_sec = bfd_com_section_ptr; |
| 925 | |
| 926 | sym_name = bfd_elf_string_from_elf_section (input_bfd, |
| 927 | symtab_hdr->sh_link, |
| 928 | isym.st_name); |
| 929 | |
| 930 | /* If it isn't a function, then we don't care |
| 931 | about it. */ |
| 932 | if (r_index < symtab_hdr->sh_info |
| 933 | && ELF_ST_TYPE (isym.st_info) != STT_FUNC) |
| 934 | continue; |
| 935 | |
| 936 | /* Tack on an ID so we can uniquely identify this |
| 937 | local symbol in the global hash table. */ |
| 938 | new_name = bfd_malloc (strlen (sym_name) + 10); |
| 939 | if (new_name == 0) |
| 940 | goto error_return; |
| 941 | |
| 942 | sprintf (new_name, "%s_%08x", |
| 943 | sym_name, (int) sym_sec); |
| 944 | sym_name = new_name; |
| 945 | |
| 946 | hash = (struct elf32_mn10300_link_hash_entry *) |
| 947 | elf_link_hash_lookup (&hash_table->static_hash_table->root, |
| 948 | sym_name, true, |
| 949 | true, false); |
| 950 | free (new_name); |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | r_index -= symtab_hdr->sh_info; |
| 955 | hash = (struct elf32_mn10300_link_hash_entry *) |
| 956 | elf_sym_hashes (input_bfd)[r_index]; |
| 957 | } |
| 958 | |
| 959 | /* If this is not a "call" instruction, then we |
| 960 | should convert "call" instructions to "calls" |
| 961 | instructions. */ |
| 962 | code = bfd_get_8 (input_bfd, |
| 963 | contents + irel->r_offset - 1); |
| 964 | if (code != 0xdd && code != 0xcd) |
| 965 | hash->flags |= MN10300_CONVERT_CALL_TO_CALLS; |
| 966 | |
| 967 | /* If this is a jump/call, then bump the direct_calls |
| 968 | counter. Else force "call" to "calls" conversions. */ |
| 969 | if (r_type == R_MN10300_PCREL32 |
| 970 | || r_type == R_MN10300_PCREL16) |
| 971 | hash->direct_calls++; |
| 972 | else |
| 973 | hash->flags |= MN10300_CONVERT_CALL_TO_CALLS; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | /* Now look at the actual contents to get the stack size, |
| 978 | and a list of what registers were saved in the prologue |
| 979 | (ie movm_args). */ |
| 980 | if ((section->flags & SEC_CODE) != 0) |
| 981 | { |
| 982 | |
| 983 | Elf32_External_Sym *esym, *esymend; |
| 984 | int idx, shndx; |
| 985 | |
| 986 | shndx = _bfd_elf_section_from_bfd_section (input_bfd, |
| 987 | section); |
| 988 | |
| 989 | /* Look at each function defined in this section and |
| 990 | update info for that function. */ |
| 991 | esym = extsyms; |
| 992 | esymend = esym + symtab_hdr->sh_info; |
| 993 | for (; esym < esymend; esym++) |
| 994 | { |
| 995 | Elf_Internal_Sym isym; |
| 996 | |
| 997 | bfd_elf32_swap_symbol_in (input_bfd, esym, &isym); |
| 998 | if (isym.st_shndx == shndx |
| 999 | && ELF_ST_TYPE (isym.st_info) == STT_FUNC) |
| 1000 | { |
| 1001 | if (isym.st_shndx == SHN_UNDEF) |
| 1002 | sym_sec = bfd_und_section_ptr; |
| 1003 | else if (isym.st_shndx > 0 |
| 1004 | && isym.st_shndx < SHN_LORESERVE) |
| 1005 | sym_sec |
| 1006 | = bfd_section_from_elf_index (input_bfd, |
| 1007 | isym.st_shndx); |
| 1008 | else if (isym.st_shndx == SHN_ABS) |
| 1009 | sym_sec = bfd_abs_section_ptr; |
| 1010 | else if (isym.st_shndx == SHN_COMMON) |
| 1011 | sym_sec = bfd_com_section_ptr; |
| 1012 | |
| 1013 | sym_name = bfd_elf_string_from_elf_section (input_bfd, |
| 1014 | symtab_hdr->sh_link, |
| 1015 | isym.st_name); |
| 1016 | |
| 1017 | /* Tack on an ID so we can uniquely identify this |
| 1018 | local symbol in the global hash table. */ |
| 1019 | new_name = bfd_malloc (strlen (sym_name) + 10); |
| 1020 | if (new_name == 0) |
| 1021 | goto error_return; |
| 1022 | |
| 1023 | sprintf (new_name, "%s_%08x", |
| 1024 | sym_name, (int) sym_sec); |
| 1025 | sym_name = new_name; |
| 1026 | |
| 1027 | hash = (struct elf32_mn10300_link_hash_entry *) |
| 1028 | elf_link_hash_lookup (&hash_table->static_hash_table->root, |
| 1029 | sym_name, true, |
| 1030 | true, false); |
| 1031 | free (new_name); |
| 1032 | compute_function_info (input_bfd, hash, |
| 1033 | isym.st_value, contents); |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | esym = extsyms + symtab_hdr->sh_info; |
| 1038 | esymend = extsyms + (symtab_hdr->sh_size |
| 1039 | / sizeof (Elf32_External_Sym)); |
| 1040 | for (idx = 0; esym < esymend; esym++, idx++) |
| 1041 | { |
| 1042 | Elf_Internal_Sym isym; |
| 1043 | |
| 1044 | bfd_elf32_swap_symbol_in (input_bfd, esym, &isym); |
| 1045 | hash = (struct elf32_mn10300_link_hash_entry *) |
| 1046 | elf_sym_hashes (input_bfd)[idx]; |
| 1047 | if (isym.st_shndx == shndx |
| 1048 | && ELF_ST_TYPE (isym.st_info) == STT_FUNC |
| 1049 | && (hash)->root.root.u.def.section == section |
| 1050 | && ((hash)->root.root.type == bfd_link_hash_defined |
| 1051 | || (hash)->root.root.type == bfd_link_hash_defweak)) |
| 1052 | compute_function_info (input_bfd, hash, |
| 1053 | (hash)->root.root.u.def.value, |
| 1054 | contents); |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | /* Cache or free any memory we allocated for the relocs. */ |
| 1059 | if (free_relocs != NULL) |
| 1060 | { |
| 1061 | free (free_relocs); |
| 1062 | free_relocs = NULL; |
| 1063 | } |
| 1064 | |
| 1065 | /* Cache or free any memory we allocated for the contents. */ |
| 1066 | if (free_contents != NULL) |
| 1067 | { |
| 1068 | if (! link_info->keep_memory) |
| 1069 | free (free_contents); |
| 1070 | else |
| 1071 | { |
| 1072 | /* Cache the section contents for elf_link_input_bfd. */ |
| 1073 | elf_section_data (section)->this_hdr.contents = contents; |
| 1074 | } |
| 1075 | free_contents = NULL; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /* Cache or free any memory we allocated for the symbols. */ |
| 1080 | if (free_extsyms != NULL) |
| 1081 | { |
| 1082 | if (! link_info->keep_memory) |
| 1083 | free (free_extsyms); |
| 1084 | else |
| 1085 | { |
| 1086 | /* Cache the symbols for elf_link_input_bfd. */ |
| 1087 | symtab_hdr->contents = extsyms; |
| 1088 | } |
| 1089 | free_extsyms = NULL; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /* Now iterate on each symbol in the hash table and perform |
| 1094 | the final initialization steps on each. */ |
| 1095 | elf32_mn10300_link_hash_traverse (hash_table, |
| 1096 | elf32_mn10300_finish_hash_table_entry, |
| 1097 | NULL); |
| 1098 | elf32_mn10300_link_hash_traverse (hash_table->static_hash_table, |
| 1099 | elf32_mn10300_finish_hash_table_entry, |
| 1100 | NULL); |
| 1101 | |
| 1102 | /* All entries in the hash table are fully initialized. */ |
| 1103 | hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED; |
| 1104 | |
| 1105 | /* Now that everything has been initialized, go through each |
| 1106 | code section and delete any prologue insns which will be |
| 1107 | redundant because their operations will be performed by |
| 1108 | a "call" instruction. */ |
| 1109 | for (input_bfd = link_info->input_bfds; |
| 1110 | input_bfd != NULL; |
| 1111 | input_bfd = input_bfd->link_next) |
| 1112 | { |
| 1113 | asection *section; |
| 1114 | |
| 1115 | /* We're going to need all the symbols for each bfd. */ |
| 1116 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 1117 | |
| 1118 | /* Get cached copy if it exists. */ |
| 1119 | if (symtab_hdr->contents != NULL) |
| 1120 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; |
| 1121 | else |
| 1122 | { |
| 1123 | /* Go get them off disk. */ |
| 1124 | extsyms = ((Elf32_External_Sym *) |
| 1125 | bfd_malloc (symtab_hdr->sh_size)); |
| 1126 | if (extsyms == NULL) |
| 1127 | goto error_return; |
| 1128 | free_extsyms = extsyms; |
| 1129 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 |
| 1130 | || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd) |
| 1131 | != symtab_hdr->sh_size)) |
| 1132 | goto error_return; |
| 1133 | } |
| 1134 | |
| 1135 | /* Walk over each section in this bfd. */ |
| 1136 | for (section = input_bfd->sections; |
| 1137 | section != NULL; |
| 1138 | section = section->next) |
| 1139 | { |
| 1140 | int shndx; |
| 1141 | Elf32_External_Sym *esym, *esymend; |
| 1142 | int idx; |
| 1143 | |
| 1144 | /* Skip non-code sections and empty sections. */ |
| 1145 | if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0) |
| 1146 | continue; |
| 1147 | |
| 1148 | if (section->reloc_count != 0) |
| 1149 | { |
| 1150 | /* Get a copy of the native relocations. */ |
| 1151 | internal_relocs = (_bfd_elf32_link_read_relocs |
| 1152 | (input_bfd, section, (PTR) NULL, |
| 1153 | (Elf_Internal_Rela *) NULL, |
| 1154 | link_info->keep_memory)); |
| 1155 | if (internal_relocs == NULL) |
| 1156 | goto error_return; |
| 1157 | if (! link_info->keep_memory) |
| 1158 | free_relocs = internal_relocs; |
| 1159 | } |
| 1160 | |
| 1161 | /* Get cached copy of section contents if it exists. */ |
| 1162 | if (elf_section_data (section)->this_hdr.contents != NULL) |
| 1163 | contents = elf_section_data (section)->this_hdr.contents; |
| 1164 | else |
| 1165 | { |
| 1166 | /* Go get them off disk. */ |
| 1167 | contents = (bfd_byte *) bfd_malloc (section->_raw_size); |
| 1168 | if (contents == NULL) |
| 1169 | goto error_return; |
| 1170 | free_contents = contents; |
| 1171 | |
| 1172 | if (!bfd_get_section_contents (input_bfd, section, |
| 1173 | contents, (file_ptr) 0, |
| 1174 | section->_raw_size)) |
| 1175 | goto error_return; |
| 1176 | } |
| 1177 | |
| 1178 | shndx = _bfd_elf_section_from_bfd_section (input_bfd, section); |
| 1179 | |
| 1180 | /* Now look for any function in this section which needs |
| 1181 | insns deleted from its prologue. */ |
| 1182 | esym = extsyms; |
| 1183 | esymend = esym + symtab_hdr->sh_info; |
| 1184 | for (; esym < esymend; esym++) |
| 1185 | { |
| 1186 | Elf_Internal_Sym isym; |
| 1187 | struct elf32_mn10300_link_hash_entry *sym_hash; |
| 1188 | asection *sym_sec = NULL; |
| 1189 | const char *sym_name; |
| 1190 | char *new_name; |
| 1191 | |
| 1192 | bfd_elf32_swap_symbol_in (input_bfd, esym, &isym); |
| 1193 | |
| 1194 | if (isym.st_shndx != shndx) |
| 1195 | continue; |
| 1196 | |
| 1197 | if (isym.st_shndx == SHN_UNDEF) |
| 1198 | sym_sec = bfd_und_section_ptr; |
| 1199 | else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE) |
| 1200 | sym_sec |
| 1201 | = bfd_section_from_elf_index (input_bfd, isym.st_shndx); |
| 1202 | else if (isym.st_shndx == SHN_ABS) |
| 1203 | sym_sec = bfd_abs_section_ptr; |
| 1204 | else if (isym.st_shndx == SHN_COMMON) |
| 1205 | sym_sec = bfd_com_section_ptr; |
| 1206 | else |
| 1207 | abort (); |
| 1208 | |
| 1209 | sym_name = bfd_elf_string_from_elf_section (input_bfd, |
| 1210 | symtab_hdr->sh_link, |
| 1211 | isym.st_name); |
| 1212 | |
| 1213 | /* Tack on an ID so we can uniquely identify this |
| 1214 | local symbol in the global hash table. */ |
| 1215 | new_name = bfd_malloc (strlen (sym_name) + 10); |
| 1216 | if (new_name == 0) |
| 1217 | goto error_return; |
| 1218 | sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec); |
| 1219 | sym_name = new_name; |
| 1220 | |
| 1221 | sym_hash = (struct elf32_mn10300_link_hash_entry *) |
| 1222 | elf_link_hash_lookup (&hash_table->static_hash_table->root, |
| 1223 | sym_name, false, |
| 1224 | false, false); |
| 1225 | |
| 1226 | free (new_name); |
| 1227 | if (sym_hash == NULL) |
| 1228 | continue; |
| 1229 | |
| 1230 | if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS) |
| 1231 | && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES)) |
| 1232 | { |
| 1233 | int bytes = 0; |
| 1234 | |
| 1235 | /* Note that we've changed things. */ |
| 1236 | elf_section_data (section)->relocs = internal_relocs; |
| 1237 | free_relocs = NULL; |
| 1238 | |
| 1239 | elf_section_data (section)->this_hdr.contents = contents; |
| 1240 | free_contents = NULL; |
| 1241 | |
| 1242 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1243 | free_extsyms = NULL; |
| 1244 | |
| 1245 | /* Count how many bytes we're going to delete. */ |
| 1246 | if (sym_hash->movm_args) |
| 1247 | bytes += 2; |
| 1248 | |
| 1249 | if (sym_hash->stack_size && sym_hash->stack_size <= 128) |
| 1250 | bytes += 3; |
| 1251 | else if (sym_hash->stack_size |
| 1252 | && sym_hash->stack_size < 256) |
| 1253 | bytes += 4; |
| 1254 | |
| 1255 | /* Note that we've deleted prologue bytes for this |
| 1256 | function. */ |
| 1257 | sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES; |
| 1258 | |
| 1259 | /* Actually delete the bytes. */ |
| 1260 | if (!mn10300_elf_relax_delete_bytes (input_bfd, |
| 1261 | section, |
| 1262 | isym.st_value, |
| 1263 | bytes)) |
| 1264 | goto error_return; |
| 1265 | |
| 1266 | /* Something changed. Not strictly necessary, but |
| 1267 | may lead to more relaxing opportunities. */ |
| 1268 | *again = true; |
| 1269 | } |
| 1270 | } |
| 1271 | |
| 1272 | /* Look for any global functions in this section which |
| 1273 | need insns deleted from their prologues. */ |
| 1274 | esym = extsyms + symtab_hdr->sh_info; |
| 1275 | esymend = extsyms + (symtab_hdr->sh_size |
| 1276 | / sizeof (Elf32_External_Sym)); |
| 1277 | for (idx = 0; esym < esymend; esym++, idx++) |
| 1278 | { |
| 1279 | Elf_Internal_Sym isym; |
| 1280 | struct elf32_mn10300_link_hash_entry *sym_hash; |
| 1281 | |
| 1282 | bfd_elf32_swap_symbol_in (input_bfd, esym, &isym); |
| 1283 | sym_hash = (struct elf32_mn10300_link_hash_entry *) |
| 1284 | (elf_sym_hashes (input_bfd)[idx]); |
| 1285 | if (isym.st_shndx == shndx |
| 1286 | && (sym_hash)->root.root.u.def.section == section |
| 1287 | && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS) |
| 1288 | && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES)) |
| 1289 | { |
| 1290 | int bytes = 0; |
| 1291 | |
| 1292 | /* Note that we've changed things. */ |
| 1293 | elf_section_data (section)->relocs = internal_relocs; |
| 1294 | free_relocs = NULL; |
| 1295 | |
| 1296 | elf_section_data (section)->this_hdr.contents = contents; |
| 1297 | free_contents = NULL; |
| 1298 | |
| 1299 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1300 | free_extsyms = NULL; |
| 1301 | |
| 1302 | /* Count how many bytes we're going to delete. */ |
| 1303 | if (sym_hash->movm_args) |
| 1304 | bytes += 2; |
| 1305 | |
| 1306 | if (sym_hash->stack_size && sym_hash->stack_size <= 128) |
| 1307 | bytes += 3; |
| 1308 | else if (sym_hash->stack_size |
| 1309 | && sym_hash->stack_size < 256) |
| 1310 | bytes += 4; |
| 1311 | |
| 1312 | /* Note that we've deleted prologue bytes for this |
| 1313 | function. */ |
| 1314 | sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES; |
| 1315 | |
| 1316 | /* Actually delete the bytes. */ |
| 1317 | if (!mn10300_elf_relax_delete_bytes (input_bfd, |
| 1318 | section, |
| 1319 | (sym_hash)->root.root.u.def.value, |
| 1320 | bytes)) |
| 1321 | goto error_return; |
| 1322 | |
| 1323 | /* Something changed. Not strictly necessary, but |
| 1324 | may lead to more relaxing opportunities. */ |
| 1325 | *again = true; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | /* Cache or free any memory we allocated for the relocs. */ |
| 1330 | if (free_relocs != NULL) |
| 1331 | { |
| 1332 | free (free_relocs); |
| 1333 | free_relocs = NULL; |
| 1334 | } |
| 1335 | |
| 1336 | /* Cache or free any memory we allocated for the contents. */ |
| 1337 | if (free_contents != NULL) |
| 1338 | { |
| 1339 | if (! link_info->keep_memory) |
| 1340 | free (free_contents); |
| 1341 | else |
| 1342 | { |
| 1343 | /* Cache the section contents for elf_link_input_bfd. */ |
| 1344 | elf_section_data (section)->this_hdr.contents = contents; |
| 1345 | } |
| 1346 | free_contents = NULL; |
| 1347 | } |
| 1348 | } |
| 1349 | |
| 1350 | /* Cache or free any memory we allocated for the symbols. */ |
| 1351 | if (free_extsyms != NULL) |
| 1352 | { |
| 1353 | if (! link_info->keep_memory) |
| 1354 | free (free_extsyms); |
| 1355 | else |
| 1356 | { |
| 1357 | /* Cache the symbols for elf_link_input_bfd. */ |
| 1358 | symtab_hdr->contents = extsyms; |
| 1359 | } |
| 1360 | free_extsyms = NULL; |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | /* (Re)initialize for the basic instruction shortening/relaxing pass. */ |
| 1366 | contents = NULL; |
| 1367 | extsyms = NULL; |
| 1368 | internal_relocs = NULL; |
| 1369 | free_relocs = NULL; |
| 1370 | free_contents = NULL; |
| 1371 | free_extsyms = NULL; |
| 1372 | |
| 1373 | /* We don't have to do anything for a relocateable link, if |
| 1374 | this section does not have relocs, or if this is not a |
| 1375 | code section. */ |
| 1376 | if (link_info->relocateable |
| 1377 | || (sec->flags & SEC_RELOC) == 0 |
| 1378 | || sec->reloc_count == 0 |
| 1379 | || (sec->flags & SEC_CODE) == 0) |
| 1380 | return true; |
| 1381 | |
| 1382 | /* If this is the first time we have been called for this section, |
| 1383 | initialize the cooked size. */ |
| 1384 | if (sec->_cooked_size == 0) |
| 1385 | sec->_cooked_size = sec->_raw_size; |
| 1386 | |
| 1387 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 1388 | |
| 1389 | /* Get a copy of the native relocations. */ |
| 1390 | internal_relocs = (_bfd_elf32_link_read_relocs |
| 1391 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, |
| 1392 | link_info->keep_memory)); |
| 1393 | if (internal_relocs == NULL) |
| 1394 | goto error_return; |
| 1395 | if (! link_info->keep_memory) |
| 1396 | free_relocs = internal_relocs; |
| 1397 | |
| 1398 | /* Walk through them looking for relaxing opportunities. */ |
| 1399 | irelend = internal_relocs + sec->reloc_count; |
| 1400 | for (irel = internal_relocs; irel < irelend; irel++) |
| 1401 | { |
| 1402 | bfd_vma symval; |
| 1403 | struct elf32_mn10300_link_hash_entry *h = NULL; |
| 1404 | |
| 1405 | /* If this isn't something that can be relaxed, then ignore |
| 1406 | this reloc. */ |
| 1407 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE |
| 1408 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8 |
| 1409 | || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX) |
| 1410 | continue; |
| 1411 | |
| 1412 | /* Get the section contents if we haven't done so already. */ |
| 1413 | if (contents == NULL) |
| 1414 | { |
| 1415 | /* Get cached copy if it exists. */ |
| 1416 | if (elf_section_data (sec)->this_hdr.contents != NULL) |
| 1417 | contents = elf_section_data (sec)->this_hdr.contents; |
| 1418 | else |
| 1419 | { |
| 1420 | /* Go get them off disk. */ |
| 1421 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); |
| 1422 | if (contents == NULL) |
| 1423 | goto error_return; |
| 1424 | free_contents = contents; |
| 1425 | |
| 1426 | if (! bfd_get_section_contents (abfd, sec, contents, |
| 1427 | (file_ptr) 0, sec->_raw_size)) |
| 1428 | goto error_return; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | /* Read this BFD's symbols if we haven't done so already. */ |
| 1433 | if (extsyms == NULL) |
| 1434 | { |
| 1435 | /* Get cached copy if it exists. */ |
| 1436 | if (symtab_hdr->contents != NULL) |
| 1437 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; |
| 1438 | else |
| 1439 | { |
| 1440 | /* Go get them off disk. */ |
| 1441 | extsyms = ((Elf32_External_Sym *) |
| 1442 | bfd_malloc (symtab_hdr->sh_size)); |
| 1443 | if (extsyms == NULL) |
| 1444 | goto error_return; |
| 1445 | free_extsyms = extsyms; |
| 1446 | if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0 |
| 1447 | || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd) |
| 1448 | != symtab_hdr->sh_size)) |
| 1449 | goto error_return; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | /* Get the value of the symbol referred to by the reloc. */ |
| 1454 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) |
| 1455 | { |
| 1456 | Elf_Internal_Sym isym; |
| 1457 | asection *sym_sec = NULL; |
| 1458 | const char *sym_name; |
| 1459 | char *new_name; |
| 1460 | |
| 1461 | /* A local symbol. */ |
| 1462 | bfd_elf32_swap_symbol_in (abfd, |
| 1463 | extsyms + ELF32_R_SYM (irel->r_info), |
| 1464 | &isym); |
| 1465 | |
| 1466 | if (isym.st_shndx == SHN_UNDEF) |
| 1467 | sym_sec = bfd_und_section_ptr; |
| 1468 | else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE) |
| 1469 | sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx); |
| 1470 | else if (isym.st_shndx == SHN_ABS) |
| 1471 | sym_sec = bfd_abs_section_ptr; |
| 1472 | else if (isym.st_shndx == SHN_COMMON) |
| 1473 | sym_sec = bfd_com_section_ptr; |
| 1474 | else |
| 1475 | abort (); |
| 1476 | |
| 1477 | symval = (isym.st_value |
| 1478 | + sym_sec->output_section->vma |
| 1479 | + sym_sec->output_offset); |
| 1480 | sym_name = bfd_elf_string_from_elf_section (abfd, |
| 1481 | symtab_hdr->sh_link, |
| 1482 | isym.st_name); |
| 1483 | |
| 1484 | /* Tack on an ID so we can uniquely identify this |
| 1485 | local symbol in the global hash table. */ |
| 1486 | new_name = bfd_malloc (strlen (sym_name) + 10); |
| 1487 | if (new_name == 0) |
| 1488 | goto error_return; |
| 1489 | sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec); |
| 1490 | sym_name = new_name; |
| 1491 | |
| 1492 | h = (struct elf32_mn10300_link_hash_entry *) |
| 1493 | elf_link_hash_lookup (&hash_table->static_hash_table->root, |
| 1494 | sym_name, false, false, false); |
| 1495 | free (new_name); |
| 1496 | } |
| 1497 | else |
| 1498 | { |
| 1499 | unsigned long indx; |
| 1500 | |
| 1501 | /* An external symbol. */ |
| 1502 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; |
| 1503 | h = (struct elf32_mn10300_link_hash_entry *) |
| 1504 | (elf_sym_hashes (abfd)[indx]); |
| 1505 | BFD_ASSERT (h != NULL); |
| 1506 | if (h->root.root.type != bfd_link_hash_defined |
| 1507 | && h->root.root.type != bfd_link_hash_defweak) |
| 1508 | { |
| 1509 | /* This appears to be a reference to an undefined |
| 1510 | symbol. Just ignore it--it will be caught by the |
| 1511 | regular reloc processing. */ |
| 1512 | continue; |
| 1513 | } |
| 1514 | |
| 1515 | symval = (h->root.root.u.def.value |
| 1516 | + h->root.root.u.def.section->output_section->vma |
| 1517 | + h->root.root.u.def.section->output_offset); |
| 1518 | } |
| 1519 | |
| 1520 | /* For simplicity of coding, we are going to modify the section |
| 1521 | contents, the section relocs, and the BFD symbol table. We |
| 1522 | must tell the rest of the code not to free up this |
| 1523 | information. It would be possible to instead create a table |
| 1524 | of changes which have to be made, as is done in coff-mips.c; |
| 1525 | that would be more work, but would require less memory when |
| 1526 | the linker is run. */ |
| 1527 | |
| 1528 | /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative |
| 1529 | branch/call, also deal with "call" -> "calls" conversions and |
| 1530 | insertion of prologue data into "call" instructions. */ |
| 1531 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32) |
| 1532 | { |
| 1533 | bfd_vma value = symval; |
| 1534 | |
| 1535 | /* If we've got a "call" instruction that needs to be turned |
| 1536 | into a "calls" instruction, do so now. It saves a byte. */ |
| 1537 | if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS)) |
| 1538 | { |
| 1539 | unsigned char code; |
| 1540 | |
| 1541 | /* Get the opcode. */ |
| 1542 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 1543 | |
| 1544 | /* Make sure we're working with a "call" instruction! */ |
| 1545 | if (code == 0xdd) |
| 1546 | { |
| 1547 | /* Note that we've changed the relocs, section contents, |
| 1548 | etc. */ |
| 1549 | elf_section_data (sec)->relocs = internal_relocs; |
| 1550 | free_relocs = NULL; |
| 1551 | |
| 1552 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1553 | free_contents = NULL; |
| 1554 | |
| 1555 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1556 | free_extsyms = NULL; |
| 1557 | |
| 1558 | /* Fix the opcode. */ |
| 1559 | bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1); |
| 1560 | bfd_put_8 (abfd, 0xff, contents + irel->r_offset); |
| 1561 | |
| 1562 | /* Fix irel->r_offset and irel->r_addend. */ |
| 1563 | irel->r_offset += 1; |
| 1564 | irel->r_addend += 1; |
| 1565 | |
| 1566 | /* Delete one byte of data. */ |
| 1567 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 1568 | irel->r_offset + 3, 1)) |
| 1569 | goto error_return; |
| 1570 | |
| 1571 | /* That will change things, so, we should relax again. |
| 1572 | Note that this is not required, and it may be slow. */ |
| 1573 | *again = true; |
| 1574 | } |
| 1575 | } |
| 1576 | else if (h) |
| 1577 | { |
| 1578 | /* We've got a "call" instruction which needs some data |
| 1579 | from target function filled in. */ |
| 1580 | unsigned char code; |
| 1581 | |
| 1582 | /* Get the opcode. */ |
| 1583 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 1584 | |
| 1585 | /* Insert data from the target function into the "call" |
| 1586 | instruction if needed. */ |
| 1587 | if (code == 0xdd) |
| 1588 | { |
| 1589 | bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4); |
| 1590 | bfd_put_8 (abfd, h->stack_size + h->movm_stack_size, |
| 1591 | contents + irel->r_offset + 5); |
| 1592 | } |
| 1593 | } |
| 1594 | |
| 1595 | /* Deal with pc-relative gunk. */ |
| 1596 | value -= (sec->output_section->vma + sec->output_offset); |
| 1597 | value -= irel->r_offset; |
| 1598 | value += irel->r_addend; |
| 1599 | |
| 1600 | /* See if the value will fit in 16 bits, note the high value is |
| 1601 | 0x7fff + 2 as the target will be two bytes closer if we are |
| 1602 | able to relax. */ |
| 1603 | if ((long) value < 0x8001 && (long) value > -0x8000) |
| 1604 | { |
| 1605 | unsigned char code; |
| 1606 | |
| 1607 | /* Get the opcode. */ |
| 1608 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 1609 | |
| 1610 | if (code != 0xdc && code != 0xdd && code != 0xff) |
| 1611 | continue; |
| 1612 | |
| 1613 | /* Note that we've changed the relocs, section contents, etc. */ |
| 1614 | elf_section_data (sec)->relocs = internal_relocs; |
| 1615 | free_relocs = NULL; |
| 1616 | |
| 1617 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1618 | free_contents = NULL; |
| 1619 | |
| 1620 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1621 | free_extsyms = NULL; |
| 1622 | |
| 1623 | /* Fix the opcode. */ |
| 1624 | if (code == 0xdc) |
| 1625 | bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1); |
| 1626 | else if (code == 0xdd) |
| 1627 | bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1); |
| 1628 | else if (code == 0xff) |
| 1629 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); |
| 1630 | |
| 1631 | /* Fix the relocation's type. */ |
| 1632 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 1633 | R_MN10300_PCREL16); |
| 1634 | |
| 1635 | /* Delete two bytes of data. */ |
| 1636 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 1637 | irel->r_offset + 1, 2)) |
| 1638 | goto error_return; |
| 1639 | |
| 1640 | /* That will change things, so, we should relax again. |
| 1641 | Note that this is not required, and it may be slow. */ |
| 1642 | *again = true; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative |
| 1647 | branch. */ |
| 1648 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16) |
| 1649 | { |
| 1650 | bfd_vma value = symval; |
| 1651 | |
| 1652 | /* If we've got a "call" instruction that needs to be turned |
| 1653 | into a "calls" instruction, do so now. It saves a byte. */ |
| 1654 | if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS)) |
| 1655 | { |
| 1656 | unsigned char code; |
| 1657 | |
| 1658 | /* Get the opcode. */ |
| 1659 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 1660 | |
| 1661 | /* Make sure we're working with a "call" instruction! */ |
| 1662 | if (code == 0xcd) |
| 1663 | { |
| 1664 | /* Note that we've changed the relocs, section contents, |
| 1665 | etc. */ |
| 1666 | elf_section_data (sec)->relocs = internal_relocs; |
| 1667 | free_relocs = NULL; |
| 1668 | |
| 1669 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1670 | free_contents = NULL; |
| 1671 | |
| 1672 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1673 | free_extsyms = NULL; |
| 1674 | |
| 1675 | /* Fix the opcode. */ |
| 1676 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1); |
| 1677 | bfd_put_8 (abfd, 0xff, contents + irel->r_offset); |
| 1678 | |
| 1679 | /* Fix irel->r_offset and irel->r_addend. */ |
| 1680 | irel->r_offset += 1; |
| 1681 | irel->r_addend += 1; |
| 1682 | |
| 1683 | /* Delete one byte of data. */ |
| 1684 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 1685 | irel->r_offset + 1, 1)) |
| 1686 | goto error_return; |
| 1687 | |
| 1688 | /* That will change things, so, we should relax again. |
| 1689 | Note that this is not required, and it may be slow. */ |
| 1690 | *again = true; |
| 1691 | } |
| 1692 | } |
| 1693 | else if (h) |
| 1694 | { |
| 1695 | unsigned char code; |
| 1696 | |
| 1697 | /* Get the opcode. */ |
| 1698 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 1699 | |
| 1700 | /* Insert data from the target function into the "call" |
| 1701 | instruction if needed. */ |
| 1702 | if (code == 0xcd) |
| 1703 | { |
| 1704 | bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2); |
| 1705 | bfd_put_8 (abfd, h->stack_size + h->movm_stack_size, |
| 1706 | contents + irel->r_offset + 3); |
| 1707 | } |
| 1708 | } |
| 1709 | |
| 1710 | /* Deal with pc-relative gunk. */ |
| 1711 | value -= (sec->output_section->vma + sec->output_offset); |
| 1712 | value -= irel->r_offset; |
| 1713 | value += irel->r_addend; |
| 1714 | |
| 1715 | /* See if the value will fit in 8 bits, note the high value is |
| 1716 | 0x7f + 1 as the target will be one bytes closer if we are |
| 1717 | able to relax. */ |
| 1718 | if ((long) value < 0x80 && (long) value > -0x80) |
| 1719 | { |
| 1720 | unsigned char code; |
| 1721 | |
| 1722 | /* Get the opcode. */ |
| 1723 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 1724 | |
| 1725 | if (code != 0xcc) |
| 1726 | continue; |
| 1727 | |
| 1728 | /* Note that we've changed the relocs, section contents, etc. */ |
| 1729 | elf_section_data (sec)->relocs = internal_relocs; |
| 1730 | free_relocs = NULL; |
| 1731 | |
| 1732 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1733 | free_contents = NULL; |
| 1734 | |
| 1735 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1736 | free_extsyms = NULL; |
| 1737 | |
| 1738 | /* Fix the opcode. */ |
| 1739 | bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1); |
| 1740 | |
| 1741 | /* Fix the relocation's type. */ |
| 1742 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 1743 | R_MN10300_PCREL8); |
| 1744 | |
| 1745 | /* Delete one byte of data. */ |
| 1746 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 1747 | irel->r_offset + 1, 1)) |
| 1748 | goto error_return; |
| 1749 | |
| 1750 | /* That will change things, so, we should relax again. |
| 1751 | Note that this is not required, and it may be slow. */ |
| 1752 | *again = true; |
| 1753 | } |
| 1754 | } |
| 1755 | |
| 1756 | /* Try to eliminate an unconditional 8 bit pc-relative branch |
| 1757 | which immediately follows a conditional 8 bit pc-relative |
| 1758 | branch around the unconditional branch. |
| 1759 | |
| 1760 | original: new: |
| 1761 | bCC lab1 bCC' lab2 |
| 1762 | bra lab2 |
| 1763 | lab1: lab1: |
| 1764 | |
| 1765 | This happens when the bCC can't reach lab2 at assembly time, |
| 1766 | but due to other relaxations it can reach at link time. */ |
| 1767 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8) |
| 1768 | { |
| 1769 | Elf_Internal_Rela *nrel; |
| 1770 | bfd_vma value = symval; |
| 1771 | unsigned char code; |
| 1772 | |
| 1773 | /* Deal with pc-relative gunk. */ |
| 1774 | value -= (sec->output_section->vma + sec->output_offset); |
| 1775 | value -= irel->r_offset; |
| 1776 | value += irel->r_addend; |
| 1777 | |
| 1778 | /* Do nothing if this reloc is the last byte in the section. */ |
| 1779 | if (irel->r_offset == sec->_cooked_size) |
| 1780 | continue; |
| 1781 | |
| 1782 | /* See if the next instruction is an unconditional pc-relative |
| 1783 | branch, more often than not this test will fail, so we |
| 1784 | test it first to speed things up. */ |
| 1785 | code = bfd_get_8 (abfd, contents + irel->r_offset + 1); |
| 1786 | if (code != 0xca) |
| 1787 | continue; |
| 1788 | |
| 1789 | /* Also make sure the next relocation applies to the next |
| 1790 | instruction and that it's a pc-relative 8 bit branch. */ |
| 1791 | nrel = irel + 1; |
| 1792 | if (nrel == irelend |
| 1793 | || irel->r_offset + 2 != nrel->r_offset |
| 1794 | || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8) |
| 1795 | continue; |
| 1796 | |
| 1797 | /* Make sure our destination immediately follows the |
| 1798 | unconditional branch. */ |
| 1799 | if (symval != (sec->output_section->vma + sec->output_offset |
| 1800 | + irel->r_offset + 3)) |
| 1801 | continue; |
| 1802 | |
| 1803 | /* Now make sure we are a conditional branch. This may not |
| 1804 | be necessary, but why take the chance. |
| 1805 | |
| 1806 | Note these checks assume that R_MN10300_PCREL8 relocs |
| 1807 | only occur on bCC and bCCx insns. If they occured |
| 1808 | elsewhere, we'd need to know the start of this insn |
| 1809 | for this check to be accurate. */ |
| 1810 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 1811 | if (code != 0xc0 && code != 0xc1 && code != 0xc2 |
| 1812 | && code != 0xc3 && code != 0xc4 && code != 0xc5 |
| 1813 | && code != 0xc6 && code != 0xc7 && code != 0xc8 |
| 1814 | && code != 0xc9 && code != 0xe8 && code != 0xe9 |
| 1815 | && code != 0xea && code != 0xeb) |
| 1816 | continue; |
| 1817 | |
| 1818 | /* We also have to be sure there is no symbol/label |
| 1819 | at the unconditional branch. */ |
| 1820 | if (mn10300_elf_symbol_address_p (abfd, sec, extsyms, |
| 1821 | irel->r_offset + 1)) |
| 1822 | continue; |
| 1823 | |
| 1824 | /* Note that we've changed the relocs, section contents, etc. */ |
| 1825 | elf_section_data (sec)->relocs = internal_relocs; |
| 1826 | free_relocs = NULL; |
| 1827 | |
| 1828 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1829 | free_contents = NULL; |
| 1830 | |
| 1831 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1832 | free_extsyms = NULL; |
| 1833 | |
| 1834 | /* Reverse the condition of the first branch. */ |
| 1835 | switch (code) |
| 1836 | { |
| 1837 | case 0xc8: |
| 1838 | code = 0xc9; |
| 1839 | break; |
| 1840 | case 0xc9: |
| 1841 | code = 0xc8; |
| 1842 | break; |
| 1843 | case 0xc0: |
| 1844 | code = 0xc2; |
| 1845 | break; |
| 1846 | case 0xc2: |
| 1847 | code = 0xc0; |
| 1848 | break; |
| 1849 | case 0xc3: |
| 1850 | code = 0xc1; |
| 1851 | break; |
| 1852 | case 0xc1: |
| 1853 | code = 0xc3; |
| 1854 | break; |
| 1855 | case 0xc4: |
| 1856 | code = 0xc6; |
| 1857 | break; |
| 1858 | case 0xc6: |
| 1859 | code = 0xc4; |
| 1860 | break; |
| 1861 | case 0xc7: |
| 1862 | code = 0xc5; |
| 1863 | break; |
| 1864 | case 0xc5: |
| 1865 | code = 0xc7; |
| 1866 | break; |
| 1867 | case 0xe8: |
| 1868 | code = 0xe9; |
| 1869 | break; |
| 1870 | case 0x9d: |
| 1871 | code = 0xe8; |
| 1872 | break; |
| 1873 | case 0xea: |
| 1874 | code = 0xeb; |
| 1875 | break; |
| 1876 | case 0xeb: |
| 1877 | code = 0xea; |
| 1878 | break; |
| 1879 | } |
| 1880 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
| 1881 | |
| 1882 | /* Set the reloc type and symbol for the first branch |
| 1883 | from the second branch. */ |
| 1884 | irel->r_info = nrel->r_info; |
| 1885 | |
| 1886 | /* Make the reloc for the second branch a null reloc. */ |
| 1887 | nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info), |
| 1888 | R_MN10300_NONE); |
| 1889 | |
| 1890 | /* Delete two bytes of data. */ |
| 1891 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 1892 | irel->r_offset + 1, 2)) |
| 1893 | goto error_return; |
| 1894 | |
| 1895 | /* That will change things, so, we should relax again. |
| 1896 | Note that this is not required, and it may be slow. */ |
| 1897 | *again = true; |
| 1898 | } |
| 1899 | |
| 1900 | /* Try to turn a 24 immediate, displacement or absolute address |
| 1901 | into a 8 immediate, displacement or absolute address. */ |
| 1902 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24) |
| 1903 | { |
| 1904 | bfd_vma value = symval; |
| 1905 | value += irel->r_addend; |
| 1906 | |
| 1907 | /* See if the value will fit in 8 bits. */ |
| 1908 | if ((long) value < 0x7f && (long) value > -0x80) |
| 1909 | { |
| 1910 | unsigned char code; |
| 1911 | |
| 1912 | /* AM33 insns which have 24 operands are 6 bytes long and |
| 1913 | will have 0xfd as the first byte. */ |
| 1914 | |
| 1915 | /* Get the first opcode. */ |
| 1916 | code = bfd_get_8 (abfd, contents + irel->r_offset - 3); |
| 1917 | |
| 1918 | if (code == 0xfd) |
| 1919 | { |
| 1920 | /* Get the second opcode. */ |
| 1921 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); |
| 1922 | |
| 1923 | /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit |
| 1924 | equivalent instructions exists. */ |
| 1925 | if (code != 0x6b && code != 0x7b |
| 1926 | && code != 0x8b && code != 0x9b |
| 1927 | && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08 |
| 1928 | || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b |
| 1929 | || (code & 0x0f) == 0x0e)) |
| 1930 | { |
| 1931 | /* Not safe if the high bit is on as relaxing may |
| 1932 | move the value out of high mem and thus not fit |
| 1933 | in a signed 8bit value. This is currently over |
| 1934 | conservative. */ |
| 1935 | if ((value & 0x80) == 0) |
| 1936 | { |
| 1937 | /* Note that we've changed the relocation contents, |
| 1938 | etc. */ |
| 1939 | elf_section_data (sec)->relocs = internal_relocs; |
| 1940 | free_relocs = NULL; |
| 1941 | |
| 1942 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1943 | free_contents = NULL; |
| 1944 | |
| 1945 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 1946 | free_extsyms = NULL; |
| 1947 | |
| 1948 | /* Fix the opcode. */ |
| 1949 | bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3); |
| 1950 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); |
| 1951 | |
| 1952 | /* Fix the relocation's type. */ |
| 1953 | irel->r_info = |
| 1954 | ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 1955 | R_MN10300_8); |
| 1956 | |
| 1957 | /* Delete two bytes of data. */ |
| 1958 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 1959 | irel->r_offset + 1, 2)) |
| 1960 | goto error_return; |
| 1961 | |
| 1962 | /* That will change things, so, we should relax |
| 1963 | again. Note that this is not required, and it |
| 1964 | may be slow. */ |
| 1965 | *again = true; |
| 1966 | break; |
| 1967 | } |
| 1968 | } |
| 1969 | } |
| 1970 | } |
| 1971 | } |
| 1972 | |
| 1973 | /* Try to turn a 32bit immediate, displacement or absolute address |
| 1974 | into a 16bit immediate, displacement or absolute address. */ |
| 1975 | if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32) |
| 1976 | { |
| 1977 | bfd_vma value = symval; |
| 1978 | value += irel->r_addend; |
| 1979 | |
| 1980 | /* See if the value will fit in 24 bits. |
| 1981 | We allow any 16bit match here. We prune those we can't |
| 1982 | handle below. */ |
| 1983 | if ((long) value < 0x7fffff && (long) value > -0x800000) |
| 1984 | { |
| 1985 | unsigned char code; |
| 1986 | |
| 1987 | /* AM33 insns which have 32bit operands are 7 bytes long and |
| 1988 | will have 0xfe as the first byte. */ |
| 1989 | |
| 1990 | /* Get the first opcode. */ |
| 1991 | code = bfd_get_8 (abfd, contents + irel->r_offset - 3); |
| 1992 | |
| 1993 | if (code == 0xfe) |
| 1994 | { |
| 1995 | /* Get the second opcode. */ |
| 1996 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); |
| 1997 | |
| 1998 | /* All the am33 32 -> 24 relaxing possibilities. */ |
| 1999 | /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit |
| 2000 | equivalent instructions exists. */ |
| 2001 | if (code != 0x6b && code != 0x7b |
| 2002 | && code != 0x8b && code != 0x9b |
| 2003 | && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08 |
| 2004 | || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b |
| 2005 | || (code & 0x0f) == 0x0e)) |
| 2006 | { |
| 2007 | /* Not safe if the high bit is on as relaxing may |
| 2008 | move the value out of high mem and thus not fit |
| 2009 | in a signed 16bit value. This is currently over |
| 2010 | conservative. */ |
| 2011 | if ((value & 0x8000) == 0) |
| 2012 | { |
| 2013 | /* Note that we've changed the relocation contents, |
| 2014 | etc. */ |
| 2015 | elf_section_data (sec)->relocs = internal_relocs; |
| 2016 | free_relocs = NULL; |
| 2017 | |
| 2018 | elf_section_data (sec)->this_hdr.contents = contents; |
| 2019 | free_contents = NULL; |
| 2020 | |
| 2021 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 2022 | free_extsyms = NULL; |
| 2023 | |
| 2024 | /* Fix the opcode. */ |
| 2025 | bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3); |
| 2026 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); |
| 2027 | |
| 2028 | /* Fix the relocation's type. */ |
| 2029 | irel->r_info = |
| 2030 | ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 2031 | R_MN10300_24); |
| 2032 | |
| 2033 | /* Delete one byte of data. */ |
| 2034 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 2035 | irel->r_offset + 3, 1)) |
| 2036 | goto error_return; |
| 2037 | |
| 2038 | /* That will change things, so, we should relax |
| 2039 | again. Note that this is not required, and it |
| 2040 | may be slow. */ |
| 2041 | *again = true; |
| 2042 | break; |
| 2043 | } |
| 2044 | } |
| 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | /* See if the value will fit in 16 bits. |
| 2049 | We allow any 16bit match here. We prune those we can't |
| 2050 | handle below. */ |
| 2051 | if ((long) value < 0x7fff && (long) value > -0x8000) |
| 2052 | { |
| 2053 | unsigned char code; |
| 2054 | |
| 2055 | /* Most insns which have 32bit operands are 6 bytes long; |
| 2056 | exceptions are pcrel insns and bit insns. |
| 2057 | |
| 2058 | We handle pcrel insns above. We don't bother trying |
| 2059 | to handle the bit insns here. |
| 2060 | |
| 2061 | The first byte of the remaining insns will be 0xfc. */ |
| 2062 | |
| 2063 | /* Get the first opcode. */ |
| 2064 | code = bfd_get_8 (abfd, contents + irel->r_offset - 2); |
| 2065 | |
| 2066 | if (code != 0xfc) |
| 2067 | continue; |
| 2068 | |
| 2069 | /* Get the second opcode. */ |
| 2070 | code = bfd_get_8 (abfd, contents + irel->r_offset - 1); |
| 2071 | |
| 2072 | if ((code & 0xf0) < 0x80) |
| 2073 | switch (code & 0xf0) |
| 2074 | { |
| 2075 | /* mov (d32,am),dn -> mov (d32,am),dn |
| 2076 | mov dm,(d32,am) -> mov dn,(d32,am) |
| 2077 | mov (d32,am),an -> mov (d32,am),an |
| 2078 | mov dm,(d32,am) -> mov dn,(d32,am) |
| 2079 | movbu (d32,am),dn -> movbu (d32,am),dn |
| 2080 | movbu dm,(d32,am) -> movbu dn,(d32,am) |
| 2081 | movhu (d32,am),dn -> movhu (d32,am),dn |
| 2082 | movhu dm,(d32,am) -> movhu dn,(d32,am) */ |
| 2083 | case 0x00: |
| 2084 | case 0x10: |
| 2085 | case 0x20: |
| 2086 | case 0x30: |
| 2087 | case 0x40: |
| 2088 | case 0x50: |
| 2089 | case 0x60: |
| 2090 | case 0x70: |
| 2091 | /* Not safe if the high bit is on as relaxing may |
| 2092 | move the value out of high mem and thus not fit |
| 2093 | in a signed 16bit value. */ |
| 2094 | if (code == 0xcc |
| 2095 | && (value & 0x8000)) |
| 2096 | continue; |
| 2097 | |
| 2098 | /* Note that we've changed the relocation contents, etc. */ |
| 2099 | elf_section_data (sec)->relocs = internal_relocs; |
| 2100 | free_relocs = NULL; |
| 2101 | |
| 2102 | elf_section_data (sec)->this_hdr.contents = contents; |
| 2103 | free_contents = NULL; |
| 2104 | |
| 2105 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 2106 | free_extsyms = NULL; |
| 2107 | |
| 2108 | /* Fix the opcode. */ |
| 2109 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); |
| 2110 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
| 2111 | |
| 2112 | /* Fix the relocation's type. */ |
| 2113 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 2114 | R_MN10300_16); |
| 2115 | |
| 2116 | /* Delete two bytes of data. */ |
| 2117 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 2118 | irel->r_offset + 2, 2)) |
| 2119 | goto error_return; |
| 2120 | |
| 2121 | /* That will change things, so, we should relax again. |
| 2122 | Note that this is not required, and it may be slow. */ |
| 2123 | *again = true; |
| 2124 | break; |
| 2125 | } |
| 2126 | else if ((code & 0xf0) == 0x80 |
| 2127 | || (code & 0xf0) == 0x90) |
| 2128 | switch (code & 0xf3) |
| 2129 | { |
| 2130 | /* mov dn,(abs32) -> mov dn,(abs16) |
| 2131 | movbu dn,(abs32) -> movbu dn,(abs16) |
| 2132 | movhu dn,(abs32) -> movhu dn,(abs16) */ |
| 2133 | case 0x81: |
| 2134 | case 0x82: |
| 2135 | case 0x83: |
| 2136 | /* Note that we've changed the relocation contents, etc. */ |
| 2137 | elf_section_data (sec)->relocs = internal_relocs; |
| 2138 | free_relocs = NULL; |
| 2139 | |
| 2140 | elf_section_data (sec)->this_hdr.contents = contents; |
| 2141 | free_contents = NULL; |
| 2142 | |
| 2143 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 2144 | free_extsyms = NULL; |
| 2145 | |
| 2146 | if ((code & 0xf3) == 0x81) |
| 2147 | code = 0x01 + (code & 0x0c); |
| 2148 | else if ((code & 0xf3) == 0x82) |
| 2149 | code = 0x02 + (code & 0x0c); |
| 2150 | else if ((code & 0xf3) == 0x83) |
| 2151 | code = 0x03 + (code & 0x0c); |
| 2152 | else |
| 2153 | abort (); |
| 2154 | |
| 2155 | /* Fix the opcode. */ |
| 2156 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); |
| 2157 | |
| 2158 | /* Fix the relocation's type. */ |
| 2159 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 2160 | R_MN10300_16); |
| 2161 | |
| 2162 | /* The opcode got shorter too, so we have to fix the |
| 2163 | addend and offset too! */ |
| 2164 | irel->r_offset -= 1; |
| 2165 | |
| 2166 | /* Delete three bytes of data. */ |
| 2167 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 2168 | irel->r_offset + 1, 3)) |
| 2169 | goto error_return; |
| 2170 | |
| 2171 | /* That will change things, so, we should relax again. |
| 2172 | Note that this is not required, and it may be slow. */ |
| 2173 | *again = true; |
| 2174 | break; |
| 2175 | |
| 2176 | /* mov am,(abs32) -> mov am,(abs16) |
| 2177 | mov am,(d32,sp) -> mov am,(d16,sp) |
| 2178 | mov dm,(d32,sp) -> mov dm,(d32,sp) |
| 2179 | movbu dm,(d32,sp) -> movbu dm,(d32,sp) |
| 2180 | movhu dm,(d32,sp) -> movhu dm,(d32,sp) */ |
| 2181 | case 0x80: |
| 2182 | case 0x90: |
| 2183 | case 0x91: |
| 2184 | case 0x92: |
| 2185 | case 0x93: |
| 2186 | /* sp-based offsets are zero-extended. */ |
| 2187 | if (code >= 0x90 && code <= 0x93 |
| 2188 | && (long)value < 0) |
| 2189 | continue; |
| 2190 | |
| 2191 | /* Note that we've changed the relocation contents, etc. */ |
| 2192 | elf_section_data (sec)->relocs = internal_relocs; |
| 2193 | free_relocs = NULL; |
| 2194 | |
| 2195 | elf_section_data (sec)->this_hdr.contents = contents; |
| 2196 | free_contents = NULL; |
| 2197 | |
| 2198 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 2199 | free_extsyms = NULL; |
| 2200 | |
| 2201 | /* Fix the opcode. */ |
| 2202 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); |
| 2203 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
| 2204 | |
| 2205 | /* Fix the relocation's type. */ |
| 2206 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 2207 | R_MN10300_16); |
| 2208 | |
| 2209 | /* Delete two bytes of data. */ |
| 2210 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 2211 | irel->r_offset + 2, 2)) |
| 2212 | goto error_return; |
| 2213 | |
| 2214 | /* That will change things, so, we should relax again. |
| 2215 | Note that this is not required, and it may be slow. */ |
| 2216 | *again = true; |
| 2217 | break; |
| 2218 | } |
| 2219 | else if ((code & 0xf0) < 0xf0) |
| 2220 | switch (code & 0xfc) |
| 2221 | { |
| 2222 | /* mov imm32,dn -> mov imm16,dn |
| 2223 | mov imm32,an -> mov imm16,an |
| 2224 | mov (abs32),dn -> mov (abs16),dn |
| 2225 | movbu (abs32),dn -> movbu (abs16),dn |
| 2226 | movhu (abs32),dn -> movhu (abs16),dn */ |
| 2227 | case 0xcc: |
| 2228 | case 0xdc: |
| 2229 | case 0xa4: |
| 2230 | case 0xa8: |
| 2231 | case 0xac: |
| 2232 | /* Not safe if the high bit is on as relaxing may |
| 2233 | move the value out of high mem and thus not fit |
| 2234 | in a signed 16bit value. */ |
| 2235 | if (code == 0xcc |
| 2236 | && (value & 0x8000)) |
| 2237 | continue; |
| 2238 | |
| 2239 | /* mov imm16, an zero-extends the immediate. */ |
| 2240 | if (code == 0xdc |
| 2241 | && (long)value < 0) |
| 2242 | continue; |
| 2243 | |
| 2244 | /* Note that we've changed the relocation contents, etc. */ |
| 2245 | elf_section_data (sec)->relocs = internal_relocs; |
| 2246 | free_relocs = NULL; |
| 2247 | |
| 2248 | elf_section_data (sec)->this_hdr.contents = contents; |
| 2249 | free_contents = NULL; |
| 2250 | |
| 2251 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 2252 | free_extsyms = NULL; |
| 2253 | |
| 2254 | if ((code & 0xfc) == 0xcc) |
| 2255 | code = 0x2c + (code & 0x03); |
| 2256 | else if ((code & 0xfc) == 0xdc) |
| 2257 | code = 0x24 + (code & 0x03); |
| 2258 | else if ((code & 0xfc) == 0xa4) |
| 2259 | code = 0x30 + (code & 0x03); |
| 2260 | else if ((code & 0xfc) == 0xa8) |
| 2261 | code = 0x34 + (code & 0x03); |
| 2262 | else if ((code & 0xfc) == 0xac) |
| 2263 | code = 0x38 + (code & 0x03); |
| 2264 | else |
| 2265 | abort (); |
| 2266 | |
| 2267 | /* Fix the opcode. */ |
| 2268 | bfd_put_8 (abfd, code, contents + irel->r_offset - 2); |
| 2269 | |
| 2270 | /* Fix the relocation's type. */ |
| 2271 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 2272 | R_MN10300_16); |
| 2273 | |
| 2274 | /* The opcode got shorter too, so we have to fix the |
| 2275 | addend and offset too! */ |
| 2276 | irel->r_offset -= 1; |
| 2277 | |
| 2278 | /* Delete three bytes of data. */ |
| 2279 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 2280 | irel->r_offset + 1, 3)) |
| 2281 | goto error_return; |
| 2282 | |
| 2283 | /* That will change things, so, we should relax again. |
| 2284 | Note that this is not required, and it may be slow. */ |
| 2285 | *again = true; |
| 2286 | break; |
| 2287 | |
| 2288 | /* mov (abs32),an -> mov (abs16),an |
| 2289 | mov (d32,sp),an -> mov (d16,sp),an |
| 2290 | mov (d32,sp),dn -> mov (d16,sp),dn |
| 2291 | movbu (d32,sp),dn -> movbu (d16,sp),dn |
| 2292 | movhu (d32,sp),dn -> movhu (d16,sp),dn |
| 2293 | add imm32,dn -> add imm16,dn |
| 2294 | cmp imm32,dn -> cmp imm16,dn |
| 2295 | add imm32,an -> add imm16,an |
| 2296 | cmp imm32,an -> cmp imm16,an |
| 2297 | and imm32,dn -> and imm16,dn |
| 2298 | or imm32,dn -> or imm16,dn |
| 2299 | xor imm32,dn -> xor imm16,dn |
| 2300 | btst imm32,dn -> btst imm16,dn */ |
| 2301 | |
| 2302 | case 0xa0: |
| 2303 | case 0xb0: |
| 2304 | case 0xb1: |
| 2305 | case 0xb2: |
| 2306 | case 0xb3: |
| 2307 | case 0xc0: |
| 2308 | case 0xc8: |
| 2309 | |
| 2310 | case 0xd0: |
| 2311 | case 0xd8: |
| 2312 | case 0xe0: |
| 2313 | case 0xe1: |
| 2314 | case 0xe2: |
| 2315 | case 0xe3: |
| 2316 | /* cmp imm16, an zero-extends the immediate. */ |
| 2317 | if (code == 0xdc |
| 2318 | && (long)value < 0) |
| 2319 | continue; |
| 2320 | |
| 2321 | /* So do sp-based offsets. */ |
| 2322 | if (code >= 0xb0 && code <= 0xb3 |
| 2323 | && (long)value < 0) |
| 2324 | continue; |
| 2325 | |
| 2326 | /* Note that we've changed the relocation contents, etc. */ |
| 2327 | elf_section_data (sec)->relocs = internal_relocs; |
| 2328 | free_relocs = NULL; |
| 2329 | |
| 2330 | elf_section_data (sec)->this_hdr.contents = contents; |
| 2331 | free_contents = NULL; |
| 2332 | |
| 2333 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 2334 | free_extsyms = NULL; |
| 2335 | |
| 2336 | /* Fix the opcode. */ |
| 2337 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); |
| 2338 | bfd_put_8 (abfd, code, contents + irel->r_offset - 1); |
| 2339 | |
| 2340 | /* Fix the relocation's type. */ |
| 2341 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 2342 | R_MN10300_16); |
| 2343 | |
| 2344 | /* Delete two bytes of data. */ |
| 2345 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 2346 | irel->r_offset + 2, 2)) |
| 2347 | goto error_return; |
| 2348 | |
| 2349 | /* That will change things, so, we should relax again. |
| 2350 | Note that this is not required, and it may be slow. */ |
| 2351 | *again = true; |
| 2352 | break; |
| 2353 | } |
| 2354 | else if (code == 0xfe) |
| 2355 | { |
| 2356 | /* add imm32,sp -> add imm16,sp */ |
| 2357 | |
| 2358 | /* Note that we've changed the relocation contents, etc. */ |
| 2359 | elf_section_data (sec)->relocs = internal_relocs; |
| 2360 | free_relocs = NULL; |
| 2361 | |
| 2362 | elf_section_data (sec)->this_hdr.contents = contents; |
| 2363 | free_contents = NULL; |
| 2364 | |
| 2365 | symtab_hdr->contents = (bfd_byte *) extsyms; |
| 2366 | free_extsyms = NULL; |
| 2367 | |
| 2368 | /* Fix the opcode. */ |
| 2369 | bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); |
| 2370 | bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1); |
| 2371 | |
| 2372 | /* Fix the relocation's type. */ |
| 2373 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), |
| 2374 | R_MN10300_16); |
| 2375 | |
| 2376 | /* Delete two bytes of data. */ |
| 2377 | if (!mn10300_elf_relax_delete_bytes (abfd, sec, |
| 2378 | irel->r_offset + 2, 2)) |
| 2379 | goto error_return; |
| 2380 | |
| 2381 | /* That will change things, so, we should relax again. |
| 2382 | Note that this is not required, and it may be slow. */ |
| 2383 | *again = true; |
| 2384 | break; |
| 2385 | } |
| 2386 | } |
| 2387 | } |
| 2388 | } |
| 2389 | |
| 2390 | if (free_relocs != NULL) |
| 2391 | { |
| 2392 | free (free_relocs); |
| 2393 | free_relocs = NULL; |
| 2394 | } |
| 2395 | |
| 2396 | if (free_contents != NULL) |
| 2397 | { |
| 2398 | if (! link_info->keep_memory) |
| 2399 | free (free_contents); |
| 2400 | else |
| 2401 | { |
| 2402 | /* Cache the section contents for elf_link_input_bfd. */ |
| 2403 | elf_section_data (sec)->this_hdr.contents = contents; |
| 2404 | } |
| 2405 | free_contents = NULL; |
| 2406 | } |
| 2407 | |
| 2408 | if (free_extsyms != NULL) |
| 2409 | { |
| 2410 | if (! link_info->keep_memory) |
| 2411 | free (free_extsyms); |
| 2412 | else |
| 2413 | { |
| 2414 | /* Cache the symbols for elf_link_input_bfd. */ |
| 2415 | symtab_hdr->contents = extsyms; |
| 2416 | } |
| 2417 | free_extsyms = NULL; |
| 2418 | } |
| 2419 | |
| 2420 | return true; |
| 2421 | |
| 2422 | error_return: |
| 2423 | if (free_relocs != NULL) |
| 2424 | free (free_relocs); |
| 2425 | if (free_contents != NULL) |
| 2426 | free (free_contents); |
| 2427 | if (free_extsyms != NULL) |
| 2428 | free (free_extsyms); |
| 2429 | return false; |
| 2430 | } |
| 2431 | |
| 2432 | /* Compute the stack size and movm arguments for the function |
| 2433 | referred to by HASH at address ADDR in section with |
| 2434 | contents CONTENTS, store the information in the hash table. */ |
| 2435 | static void |
| 2436 | compute_function_info (abfd, hash, addr, contents) |
| 2437 | bfd *abfd; |
| 2438 | struct elf32_mn10300_link_hash_entry *hash; |
| 2439 | bfd_vma addr; |
| 2440 | unsigned char *contents; |
| 2441 | { |
| 2442 | unsigned char byte1, byte2; |
| 2443 | /* We only care about a very small subset of the possible prologue |
| 2444 | sequences here. Basically we look for: |
| 2445 | |
| 2446 | movm [d2,d3,a2,a3],sp (optional) |
| 2447 | add <size>,sp (optional, and only for sizes which fit in an unsigned |
| 2448 | 8 bit number) |
| 2449 | |
| 2450 | If we find anything else, we quit. */ |
| 2451 | |
| 2452 | /* Look for movm [regs],sp */ |
| 2453 | byte1 = bfd_get_8 (abfd, contents + addr); |
| 2454 | byte2 = bfd_get_8 (abfd, contents + addr + 1); |
| 2455 | |
| 2456 | if (byte1 == 0xcf) |
| 2457 | { |
| 2458 | hash->movm_args = byte2; |
| 2459 | addr += 2; |
| 2460 | byte1 = bfd_get_8 (abfd, contents + addr); |
| 2461 | byte2 = bfd_get_8 (abfd, contents + addr + 1); |
| 2462 | } |
| 2463 | |
| 2464 | /* Now figure out how much stack space will be allocated by the movm |
| 2465 | instruction. We need this kept separate from the funtion's normal |
| 2466 | stack space. */ |
| 2467 | if (hash->movm_args) |
| 2468 | { |
| 2469 | /* Space for d2. */ |
| 2470 | if (hash->movm_args & 0x80) |
| 2471 | hash->movm_stack_size += 4; |
| 2472 | |
| 2473 | /* Space for d3. */ |
| 2474 | if (hash->movm_args & 0x40) |
| 2475 | hash->movm_stack_size += 4; |
| 2476 | |
| 2477 | /* Space for a2. */ |
| 2478 | if (hash->movm_args & 0x20) |
| 2479 | hash->movm_stack_size += 4; |
| 2480 | |
| 2481 | /* Space for a3. */ |
| 2482 | if (hash->movm_args & 0x10) |
| 2483 | hash->movm_stack_size += 4; |
| 2484 | |
| 2485 | /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */ |
| 2486 | if (hash->movm_args & 0x08) |
| 2487 | hash->movm_stack_size += 8 * 4; |
| 2488 | |
| 2489 | if (bfd_get_mach (abfd) == bfd_mach_am33) |
| 2490 | { |
| 2491 | /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */ |
| 2492 | if (hash->movm_args & 0x1) |
| 2493 | hash->movm_stack_size += 6 * 4; |
| 2494 | |
| 2495 | /* exreg1 space. e4, e5, e6, e7 */ |
| 2496 | if (hash->movm_args & 0x2) |
| 2497 | hash->movm_stack_size += 4 * 4; |
| 2498 | |
| 2499 | /* exreg0 space. e2, e3 */ |
| 2500 | if (hash->movm_args & 0x4) |
| 2501 | hash->movm_stack_size += 2 * 4; |
| 2502 | } |
| 2503 | } |
| 2504 | |
| 2505 | /* Now look for the two stack adjustment variants. */ |
| 2506 | if (byte1 == 0xf8 && byte2 == 0xfe) |
| 2507 | { |
| 2508 | int temp = bfd_get_8 (abfd, contents + addr + 2); |
| 2509 | temp = ((temp & 0xff) ^ (~0x7f)) + 0x80; |
| 2510 | |
| 2511 | hash->stack_size = -temp; |
| 2512 | } |
| 2513 | else if (byte1 == 0xfa && byte2 == 0xfe) |
| 2514 | { |
| 2515 | int temp = bfd_get_16 (abfd, contents + addr + 2); |
| 2516 | temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000; |
| 2517 | temp = -temp; |
| 2518 | |
| 2519 | if (temp < 255) |
| 2520 | hash->stack_size = temp; |
| 2521 | } |
| 2522 | |
| 2523 | /* If the total stack to be allocated by the call instruction is more |
| 2524 | than 255 bytes, then we can't remove the stack adjustment by using |
| 2525 | "call" (we might still be able to remove the "movm" instruction. */ |
| 2526 | if (hash->stack_size + hash->movm_stack_size > 255) |
| 2527 | hash->stack_size = 0; |
| 2528 | |
| 2529 | return; |
| 2530 | } |
| 2531 | |
| 2532 | /* Delete some bytes from a section while relaxing. */ |
| 2533 | |
| 2534 | static boolean |
| 2535 | mn10300_elf_relax_delete_bytes (abfd, sec, addr, count) |
| 2536 | bfd *abfd; |
| 2537 | asection *sec; |
| 2538 | bfd_vma addr; |
| 2539 | int count; |
| 2540 | { |
| 2541 | Elf_Internal_Shdr *symtab_hdr; |
| 2542 | Elf32_External_Sym *extsyms; |
| 2543 | int shndx, index; |
| 2544 | bfd_byte *contents; |
| 2545 | Elf_Internal_Rela *irel, *irelend; |
| 2546 | Elf_Internal_Rela *irelalign; |
| 2547 | bfd_vma toaddr; |
| 2548 | Elf32_External_Sym *esym, *esymend; |
| 2549 | struct elf32_mn10300_link_hash_entry *sym_hash; |
| 2550 | |
| 2551 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 2552 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; |
| 2553 | |
| 2554 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
| 2555 | |
| 2556 | contents = elf_section_data (sec)->this_hdr.contents; |
| 2557 | |
| 2558 | /* The deletion must stop at the next ALIGN reloc for an aligment |
| 2559 | power larger than the number of bytes we are deleting. */ |
| 2560 | |
| 2561 | irelalign = NULL; |
| 2562 | toaddr = sec->_cooked_size; |
| 2563 | |
| 2564 | irel = elf_section_data (sec)->relocs; |
| 2565 | irelend = irel + sec->reloc_count; |
| 2566 | |
| 2567 | /* Actually delete the bytes. */ |
| 2568 | memmove (contents + addr, contents + addr + count, toaddr - addr - count); |
| 2569 | sec->_cooked_size -= count; |
| 2570 | |
| 2571 | /* Adjust all the relocs. */ |
| 2572 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) |
| 2573 | { |
| 2574 | /* Get the new reloc address. */ |
| 2575 | if ((irel->r_offset > addr |
| 2576 | && irel->r_offset < toaddr)) |
| 2577 | irel->r_offset -= count; |
| 2578 | } |
| 2579 | |
| 2580 | /* Adjust the local symbols defined in this section. */ |
| 2581 | esym = extsyms; |
| 2582 | esymend = esym + symtab_hdr->sh_info; |
| 2583 | for (; esym < esymend; esym++) |
| 2584 | { |
| 2585 | Elf_Internal_Sym isym; |
| 2586 | |
| 2587 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); |
| 2588 | |
| 2589 | if (isym.st_shndx == shndx |
| 2590 | && isym.st_value > addr |
| 2591 | && isym.st_value < toaddr) |
| 2592 | { |
| 2593 | isym.st_value -= count; |
| 2594 | bfd_elf32_swap_symbol_out (abfd, &isym, esym); |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | /* Now adjust the global symbols defined in this section. */ |
| 2599 | esym = extsyms + symtab_hdr->sh_info; |
| 2600 | esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)); |
| 2601 | for (index = 0; esym < esymend; esym++, index++) |
| 2602 | { |
| 2603 | Elf_Internal_Sym isym; |
| 2604 | |
| 2605 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); |
| 2606 | sym_hash = (struct elf32_mn10300_link_hash_entry *) |
| 2607 | (elf_sym_hashes (abfd)[index]); |
| 2608 | if (isym.st_shndx == shndx |
| 2609 | && ((sym_hash)->root.root.type == bfd_link_hash_defined |
| 2610 | || (sym_hash)->root.root.type == bfd_link_hash_defweak) |
| 2611 | && (sym_hash)->root.root.u.def.section == sec |
| 2612 | && (sym_hash)->root.root.u.def.value > addr |
| 2613 | && (sym_hash)->root.root.u.def.value < toaddr) |
| 2614 | { |
| 2615 | (sym_hash)->root.root.u.def.value -= count; |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | return true; |
| 2620 | } |
| 2621 | |
| 2622 | /* Return true if a symbol exists at the given address, else return |
| 2623 | false. */ |
| 2624 | static boolean |
| 2625 | mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr) |
| 2626 | bfd *abfd; |
| 2627 | asection *sec; |
| 2628 | Elf32_External_Sym *extsyms; |
| 2629 | bfd_vma addr; |
| 2630 | { |
| 2631 | Elf_Internal_Shdr *symtab_hdr; |
| 2632 | int shndx; |
| 2633 | Elf32_External_Sym *esym, *esymend; |
| 2634 | struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end; |
| 2635 | |
| 2636 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 2637 | shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
| 2638 | |
| 2639 | /* Examine all the symbols. */ |
| 2640 | esym = extsyms; |
| 2641 | esymend = esym + symtab_hdr->sh_info; |
| 2642 | for (; esym < esymend; esym++) |
| 2643 | { |
| 2644 | Elf_Internal_Sym isym; |
| 2645 | |
| 2646 | bfd_elf32_swap_symbol_in (abfd, esym, &isym); |
| 2647 | |
| 2648 | if (isym.st_shndx == shndx |
| 2649 | && isym.st_value == addr) |
| 2650 | return true; |
| 2651 | } |
| 2652 | |
| 2653 | sym_hash = (struct elf32_mn10300_link_hash_entry **) (elf_sym_hashes (abfd)); |
| 2654 | sym_hash_end = (sym_hash |
| 2655 | + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
| 2656 | - symtab_hdr->sh_info)); |
| 2657 | for (; sym_hash < sym_hash_end; sym_hash++) |
| 2658 | { |
| 2659 | if (((*sym_hash)->root.root.type == bfd_link_hash_defined |
| 2660 | || (*sym_hash)->root.root.type == bfd_link_hash_defweak) |
| 2661 | && (*sym_hash)->root.root.u.def.section == sec |
| 2662 | && (*sym_hash)->root.root.u.def.value == addr) |
| 2663 | return true; |
| 2664 | } |
| 2665 | return false; |
| 2666 | } |
| 2667 | |
| 2668 | /* This is a version of bfd_generic_get_relocated_section_contents |
| 2669 | which uses mn10300_elf_relocate_section. */ |
| 2670 | |
| 2671 | static bfd_byte * |
| 2672 | mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order, |
| 2673 | data, relocateable, symbols) |
| 2674 | bfd *output_bfd; |
| 2675 | struct bfd_link_info *link_info; |
| 2676 | struct bfd_link_order *link_order; |
| 2677 | bfd_byte *data; |
| 2678 | boolean relocateable; |
| 2679 | asymbol **symbols; |
| 2680 | { |
| 2681 | Elf_Internal_Shdr *symtab_hdr; |
| 2682 | asection *input_section = link_order->u.indirect.section; |
| 2683 | bfd *input_bfd = input_section->owner; |
| 2684 | asection **sections = NULL; |
| 2685 | Elf_Internal_Rela *internal_relocs = NULL; |
| 2686 | Elf32_External_Sym *external_syms = NULL; |
| 2687 | Elf_Internal_Sym *internal_syms = NULL; |
| 2688 | |
| 2689 | /* We only need to handle the case of relaxing, or of having a |
| 2690 | particular set of section contents, specially. */ |
| 2691 | if (relocateable |
| 2692 | || elf_section_data (input_section)->this_hdr.contents == NULL) |
| 2693 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, |
| 2694 | link_order, data, |
| 2695 | relocateable, |
| 2696 | symbols); |
| 2697 | |
| 2698 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
| 2699 | |
| 2700 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, |
| 2701 | input_section->_raw_size); |
| 2702 | |
| 2703 | if ((input_section->flags & SEC_RELOC) != 0 |
| 2704 | && input_section->reloc_count > 0) |
| 2705 | { |
| 2706 | Elf_Internal_Sym *isymp; |
| 2707 | asection **secpp; |
| 2708 | Elf32_External_Sym *esym, *esymend; |
| 2709 | |
| 2710 | if (symtab_hdr->contents != NULL) |
| 2711 | external_syms = (Elf32_External_Sym *) symtab_hdr->contents; |
| 2712 | else |
| 2713 | { |
| 2714 | external_syms = ((Elf32_External_Sym *) |
| 2715 | bfd_malloc (symtab_hdr->sh_info |
| 2716 | * sizeof (Elf32_External_Sym))); |
| 2717 | if (external_syms == NULL && symtab_hdr->sh_info > 0) |
| 2718 | goto error_return; |
| 2719 | if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0 |
| 2720 | || (bfd_read (external_syms, sizeof (Elf32_External_Sym), |
| 2721 | symtab_hdr->sh_info, input_bfd) |
| 2722 | != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym)))) |
| 2723 | goto error_return; |
| 2724 | } |
| 2725 | |
| 2726 | internal_relocs = (_bfd_elf32_link_read_relocs |
| 2727 | (input_bfd, input_section, (PTR) NULL, |
| 2728 | (Elf_Internal_Rela *) NULL, false)); |
| 2729 | if (internal_relocs == NULL) |
| 2730 | goto error_return; |
| 2731 | |
| 2732 | internal_syms = ((Elf_Internal_Sym *) |
| 2733 | bfd_malloc (symtab_hdr->sh_info |
| 2734 | * sizeof (Elf_Internal_Sym))); |
| 2735 | if (internal_syms == NULL && symtab_hdr->sh_info > 0) |
| 2736 | goto error_return; |
| 2737 | |
| 2738 | sections = (asection **) bfd_malloc (symtab_hdr->sh_info |
| 2739 | * sizeof (asection *)); |
| 2740 | if (sections == NULL && symtab_hdr->sh_info > 0) |
| 2741 | goto error_return; |
| 2742 | |
| 2743 | isymp = internal_syms; |
| 2744 | secpp = sections; |
| 2745 | esym = external_syms; |
| 2746 | esymend = esym + symtab_hdr->sh_info; |
| 2747 | for (; esym < esymend; ++esym, ++isymp, ++secpp) |
| 2748 | { |
| 2749 | asection *isec; |
| 2750 | |
| 2751 | bfd_elf32_swap_symbol_in (input_bfd, esym, isymp); |
| 2752 | |
| 2753 | if (isymp->st_shndx == SHN_UNDEF) |
| 2754 | isec = bfd_und_section_ptr; |
| 2755 | else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE) |
| 2756 | isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx); |
| 2757 | else if (isymp->st_shndx == SHN_ABS) |
| 2758 | isec = bfd_abs_section_ptr; |
| 2759 | else if (isymp->st_shndx == SHN_COMMON) |
| 2760 | isec = bfd_com_section_ptr; |
| 2761 | else |
| 2762 | { |
| 2763 | /* Who knows? */ |
| 2764 | isec = NULL; |
| 2765 | } |
| 2766 | |
| 2767 | *secpp = isec; |
| 2768 | } |
| 2769 | |
| 2770 | if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd, |
| 2771 | input_section, data, internal_relocs, |
| 2772 | internal_syms, sections)) |
| 2773 | goto error_return; |
| 2774 | |
| 2775 | if (sections != NULL) |
| 2776 | free (sections); |
| 2777 | sections = NULL; |
| 2778 | if (internal_syms != NULL) |
| 2779 | free (internal_syms); |
| 2780 | internal_syms = NULL; |
| 2781 | if (external_syms != NULL && symtab_hdr->contents == NULL) |
| 2782 | free (external_syms); |
| 2783 | external_syms = NULL; |
| 2784 | if (internal_relocs != elf_section_data (input_section)->relocs) |
| 2785 | free (internal_relocs); |
| 2786 | internal_relocs = NULL; |
| 2787 | } |
| 2788 | |
| 2789 | return data; |
| 2790 | |
| 2791 | error_return: |
| 2792 | if (internal_relocs != NULL |
| 2793 | && internal_relocs != elf_section_data (input_section)->relocs) |
| 2794 | free (internal_relocs); |
| 2795 | if (external_syms != NULL && symtab_hdr->contents == NULL) |
| 2796 | free (external_syms); |
| 2797 | if (internal_syms != NULL) |
| 2798 | free (internal_syms); |
| 2799 | if (sections != NULL) |
| 2800 | free (sections); |
| 2801 | return NULL; |
| 2802 | } |
| 2803 | |
| 2804 | /* Assorted hash table functions. */ |
| 2805 | |
| 2806 | /* Initialize an entry in the link hash table. */ |
| 2807 | |
| 2808 | /* Create an entry in an MN10300 ELF linker hash table. */ |
| 2809 | |
| 2810 | static struct bfd_hash_entry * |
| 2811 | elf32_mn10300_link_hash_newfunc (entry, table, string) |
| 2812 | struct bfd_hash_entry *entry; |
| 2813 | struct bfd_hash_table *table; |
| 2814 | const char *string; |
| 2815 | { |
| 2816 | struct elf32_mn10300_link_hash_entry *ret = |
| 2817 | (struct elf32_mn10300_link_hash_entry *) entry; |
| 2818 | |
| 2819 | /* Allocate the structure if it has not already been allocated by a |
| 2820 | subclass. */ |
| 2821 | if (ret == (struct elf32_mn10300_link_hash_entry *) NULL) |
| 2822 | ret = ((struct elf32_mn10300_link_hash_entry *) |
| 2823 | bfd_hash_allocate (table, |
| 2824 | sizeof (struct elf32_mn10300_link_hash_entry))); |
| 2825 | if (ret == (struct elf32_mn10300_link_hash_entry *) NULL) |
| 2826 | return (struct bfd_hash_entry *) ret; |
| 2827 | |
| 2828 | /* Call the allocation method of the superclass. */ |
| 2829 | ret = ((struct elf32_mn10300_link_hash_entry *) |
| 2830 | _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, |
| 2831 | table, string)); |
| 2832 | if (ret != (struct elf32_mn10300_link_hash_entry *) NULL) |
| 2833 | { |
| 2834 | ret->direct_calls = 0; |
| 2835 | ret->stack_size = 0; |
| 2836 | ret->movm_stack_size = 0; |
| 2837 | ret->flags = 0; |
| 2838 | ret->movm_args = 0; |
| 2839 | } |
| 2840 | |
| 2841 | return (struct bfd_hash_entry *) ret; |
| 2842 | } |
| 2843 | |
| 2844 | /* Create an mn10300 ELF linker hash table. */ |
| 2845 | |
| 2846 | static struct bfd_link_hash_table * |
| 2847 | elf32_mn10300_link_hash_table_create (abfd) |
| 2848 | bfd *abfd; |
| 2849 | { |
| 2850 | struct elf32_mn10300_link_hash_table *ret; |
| 2851 | |
| 2852 | ret = ((struct elf32_mn10300_link_hash_table *) |
| 2853 | bfd_alloc (abfd, sizeof (struct elf32_mn10300_link_hash_table))); |
| 2854 | if (ret == (struct elf32_mn10300_link_hash_table *) NULL) |
| 2855 | return NULL; |
| 2856 | |
| 2857 | if (! _bfd_elf_link_hash_table_init (&ret->root, abfd, |
| 2858 | elf32_mn10300_link_hash_newfunc)) |
| 2859 | { |
| 2860 | bfd_release (abfd, ret); |
| 2861 | return NULL; |
| 2862 | } |
| 2863 | |
| 2864 | ret->flags = 0; |
| 2865 | ret->static_hash_table |
| 2866 | = ((struct elf32_mn10300_link_hash_table *) |
| 2867 | bfd_alloc (abfd, sizeof (struct elf_link_hash_table))); |
| 2868 | if (ret->static_hash_table == NULL) |
| 2869 | { |
| 2870 | bfd_release (abfd, ret); |
| 2871 | return NULL; |
| 2872 | } |
| 2873 | |
| 2874 | if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd, |
| 2875 | elf32_mn10300_link_hash_newfunc)) |
| 2876 | { |
| 2877 | bfd_release (abfd, ret->static_hash_table); |
| 2878 | bfd_release (abfd, ret); |
| 2879 | return NULL; |
| 2880 | } |
| 2881 | return &ret->root.root; |
| 2882 | } |
| 2883 | |
| 2884 | static int |
| 2885 | elf_mn10300_mach (flags) |
| 2886 | flagword flags; |
| 2887 | { |
| 2888 | switch (flags & EF_MN10300_MACH) |
| 2889 | { |
| 2890 | case E_MN10300_MACH_MN10300: |
| 2891 | default: |
| 2892 | return bfd_mach_mn10300; |
| 2893 | |
| 2894 | case E_MN10300_MACH_AM33: |
| 2895 | return bfd_mach_am33; |
| 2896 | } |
| 2897 | } |
| 2898 | |
| 2899 | /* The final processing done just before writing out a MN10300 ELF object |
| 2900 | file. This gets the MN10300 architecture right based on the machine |
| 2901 | number. */ |
| 2902 | |
| 2903 | void |
| 2904 | _bfd_mn10300_elf_final_write_processing (abfd, linker) |
| 2905 | bfd *abfd; |
| 2906 | boolean linker ATTRIBUTE_UNUSED; |
| 2907 | { |
| 2908 | unsigned long val; |
| 2909 | |
| 2910 | switch (bfd_get_mach (abfd)) |
| 2911 | { |
| 2912 | default: |
| 2913 | case bfd_mach_mn10300: |
| 2914 | val = E_MN10300_MACH_MN10300; |
| 2915 | break; |
| 2916 | |
| 2917 | case bfd_mach_am33: |
| 2918 | val = E_MN10300_MACH_AM33; |
| 2919 | break; |
| 2920 | } |
| 2921 | |
| 2922 | elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH); |
| 2923 | elf_elfheader (abfd)->e_flags |= val; |
| 2924 | } |
| 2925 | |
| 2926 | boolean |
| 2927 | _bfd_mn10300_elf_object_p (abfd) |
| 2928 | bfd *abfd; |
| 2929 | { |
| 2930 | bfd_default_set_arch_mach (abfd, bfd_arch_mn10300, |
| 2931 | elf_mn10300_mach (elf_elfheader (abfd)->e_flags)); |
| 2932 | return true; |
| 2933 | } |
| 2934 | |
| 2935 | /* Merge backend specific data from an object file to the output |
| 2936 | object file when linking. */ |
| 2937 | |
| 2938 | boolean |
| 2939 | _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd) |
| 2940 | bfd *ibfd; |
| 2941 | bfd *obfd; |
| 2942 | { |
| 2943 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour |
| 2944 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) |
| 2945 | return true; |
| 2946 | |
| 2947 | if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) |
| 2948 | && bfd_get_mach (obfd) < bfd_get_mach (ibfd)) |
| 2949 | { |
| 2950 | if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), |
| 2951 | bfd_get_mach (ibfd))) |
| 2952 | return false; |
| 2953 | } |
| 2954 | |
| 2955 | return true; |
| 2956 | } |
| 2957 | |
| 2958 | #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec |
| 2959 | #define TARGET_LITTLE_NAME "elf32-mn10300" |
| 2960 | #define ELF_ARCH bfd_arch_mn10300 |
| 2961 | #define ELF_MACHINE_CODE EM_CYGNUS_MN10300 |
| 2962 | #define ELF_MAXPAGESIZE 0x1000 |
| 2963 | |
| 2964 | #define elf_info_to_howto mn10300_info_to_howto |
| 2965 | #define elf_info_to_howto_rel 0 |
| 2966 | #define elf_backend_can_gc_sections 1 |
| 2967 | #define elf_backend_check_relocs mn10300_elf_check_relocs |
| 2968 | #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook |
| 2969 | #define elf_backend_relocate_section mn10300_elf_relocate_section |
| 2970 | #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section |
| 2971 | #define bfd_elf32_bfd_get_relocated_section_contents \ |
| 2972 | mn10300_elf_get_relocated_section_contents |
| 2973 | #define bfd_elf32_bfd_link_hash_table_create \ |
| 2974 | elf32_mn10300_link_hash_table_create |
| 2975 | |
| 2976 | #define elf_symbol_leading_char '_' |
| 2977 | |
| 2978 | /* So we can set bits in e_flags. */ |
| 2979 | #define elf_backend_final_write_processing \ |
| 2980 | _bfd_mn10300_elf_final_write_processing |
| 2981 | #define elf_backend_object_p _bfd_mn10300_elf_object_p |
| 2982 | |
| 2983 | #define bfd_elf32_bfd_merge_private_bfd_data \ |
| 2984 | _bfd_mn10300_elf_merge_private_bfd_data |
| 2985 | |
| 2986 | #include "elf32-target.h" |