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