| 1 | /* M16C/M32C specific support for 32-bit ELF. |
| 2 | Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 |
| 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 3 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 "sysdep.h" |
| 22 | #include "bfd.h" |
| 23 | #include "libbfd.h" |
| 24 | #include "elf-bfd.h" |
| 25 | #include "elf/m32c.h" |
| 26 | #include "libiberty.h" |
| 27 | |
| 28 | /* Forward declarations. */ |
| 29 | static reloc_howto_type * m32c_reloc_type_lookup |
| 30 | (bfd *, bfd_reloc_code_real_type); |
| 31 | static void m32c_info_to_howto_rela |
| 32 | (bfd *, arelent *, Elf_Internal_Rela *); |
| 33 | static bfd_boolean m32c_elf_relocate_section |
| 34 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); |
| 35 | static bfd_boolean m32c_elf_check_relocs |
| 36 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); |
| 37 | static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int); |
| 38 | #ifdef DEBUG |
| 39 | char * m32c_get_reloc (long reloc); |
| 40 | void dump_symtab (bfd *, void *, void *); |
| 41 | #endif |
| 42 | static bfd_boolean m32c_elf_relax_section |
| 43 | (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again); |
| 44 | |
| 45 | |
| 46 | static reloc_howto_type m32c_elf_howto_table [] = |
| 47 | { |
| 48 | /* This reloc does nothing. */ |
| 49 | HOWTO (R_M32C_NONE, /* type */ |
| 50 | 0, /* rightshift */ |
| 51 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 52 | 32, /* bitsize */ |
| 53 | FALSE, /* pc_relative */ |
| 54 | 0, /* bitpos */ |
| 55 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 56 | bfd_elf_generic_reloc, /* special_function */ |
| 57 | "R_M32C_NONE", /* name */ |
| 58 | FALSE, /* partial_inplace */ |
| 59 | 0, /* src_mask */ |
| 60 | 0, /* dst_mask */ |
| 61 | FALSE), /* pcrel_offset */ |
| 62 | |
| 63 | /* GCC intentionally overflows these next two in order to work |
| 64 | around limitations in the addressing modes, so don't complain |
| 65 | about overflow. */ |
| 66 | HOWTO (R_M32C_16, /* type */ |
| 67 | 0, /* rightshift */ |
| 68 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
| 69 | 16, /* bitsize */ |
| 70 | FALSE, /* pc_relative */ |
| 71 | 0, /* bitpos */ |
| 72 | complain_overflow_dont, /* complain_on_overflow */ |
| 73 | bfd_elf_generic_reloc, /* special_function */ |
| 74 | "R_M32C_16", /* name */ |
| 75 | FALSE, /* partial_inplace */ |
| 76 | 0, /* src_mask */ |
| 77 | 0xffff, /* dst_mask */ |
| 78 | FALSE), /* pcrel_offset */ |
| 79 | |
| 80 | HOWTO (R_M32C_24, /* type */ |
| 81 | 0, /* rightshift */ |
| 82 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 83 | 24, /* bitsize */ |
| 84 | FALSE, /* pc_relative */ |
| 85 | 0, /* bitpos */ |
| 86 | complain_overflow_dont, /* complain_on_overflow */ |
| 87 | bfd_elf_generic_reloc, /* special_function */ |
| 88 | "R_M32C_24", /* name */ |
| 89 | FALSE, /* partial_inplace */ |
| 90 | 0, /* src_mask */ |
| 91 | 0xffffff, /* dst_mask */ |
| 92 | FALSE), /* pcrel_offset */ |
| 93 | |
| 94 | HOWTO (R_M32C_32, /* type */ |
| 95 | 0, /* rightshift */ |
| 96 | 2, /* size (0 = byte, 1 = short, 2 = long) */ |
| 97 | 32, /* bitsize */ |
| 98 | FALSE, /* pc_relative */ |
| 99 | 0, /* bitpos */ |
| 100 | complain_overflow_bitfield, /* complain_on_overflow */ |
| 101 | bfd_elf_generic_reloc, /* special_function */ |
| 102 | "R_M32C_32", /* name */ |
| 103 | FALSE, /* partial_inplace */ |
| 104 | 0, /* src_mask */ |
| 105 | 0xffffffff, /* dst_mask */ |
| 106 | FALSE), /* pcrel_offset */ |
| 107 | |
| 108 | HOWTO (R_M32C_8_PCREL, /* type */ |
| 109 | 0, /* rightshift */ |
| 110 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 111 | 8, /* bitsize */ |
| 112 | TRUE, /* pc_relative */ |
| 113 | 0, /* bitpos */ |
| 114 | complain_overflow_signed, /* complain_on_overflow */ |
| 115 | bfd_elf_generic_reloc, /* special_function */ |
| 116 | "R_M32C_8_PCREL", /* name */ |
| 117 | FALSE, /* partial_inplace */ |
| 118 | 0, /* src_mask */ |
| 119 | 0xff, /* dst_mask */ |
| 120 | TRUE), /* pcrel_offset */ |
| 121 | |
| 122 | HOWTO (R_M32C_16_PCREL, /* type */ |
| 123 | 0, /* rightshift */ |
| 124 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
| 125 | 16, /* bitsize */ |
| 126 | TRUE, /* pc_relative */ |
| 127 | 0, /* bitpos */ |
| 128 | complain_overflow_signed, /* complain_on_overflow */ |
| 129 | bfd_elf_generic_reloc, /* special_function */ |
| 130 | "R_M32C_16_PCREL", /* name */ |
| 131 | FALSE, /* partial_inplace */ |
| 132 | 0, /* src_mask */ |
| 133 | 0xffff, /* dst_mask */ |
| 134 | TRUE), /* pcrel_offset */ |
| 135 | |
| 136 | HOWTO (R_M32C_8, /* type */ |
| 137 | 0, /* rightshift */ |
| 138 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 139 | 8, /* bitsize */ |
| 140 | FALSE, /* pc_relative */ |
| 141 | 0, /* bitpos */ |
| 142 | complain_overflow_unsigned, /* complain_on_overflow */ |
| 143 | bfd_elf_generic_reloc, /* special_function */ |
| 144 | "R_M32C_8", /* name */ |
| 145 | FALSE, /* partial_inplace */ |
| 146 | 0, /* src_mask */ |
| 147 | 0xff, /* dst_mask */ |
| 148 | FALSE), /* pcrel_offset */ |
| 149 | |
| 150 | HOWTO (R_M32C_LO16, /* type */ |
| 151 | 0, /* rightshift */ |
| 152 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
| 153 | 16, /* bitsize */ |
| 154 | FALSE, /* pc_relative */ |
| 155 | 0, /* bitpos */ |
| 156 | complain_overflow_dont, /* complain_on_overflow */ |
| 157 | bfd_elf_generic_reloc, /* special_function */ |
| 158 | "R_M32C_LO16", /* name */ |
| 159 | FALSE, /* partial_inplace */ |
| 160 | 0, /* src_mask */ |
| 161 | 0xffff, /* dst_mask */ |
| 162 | FALSE), /* pcrel_offset */ |
| 163 | |
| 164 | HOWTO (R_M32C_HI8, /* type */ |
| 165 | 0, /* rightshift */ |
| 166 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 167 | 8, /* bitsize */ |
| 168 | FALSE, /* pc_relative */ |
| 169 | 0, /* bitpos */ |
| 170 | complain_overflow_dont, /* complain_on_overflow */ |
| 171 | bfd_elf_generic_reloc, /* special_function */ |
| 172 | "R_M32C_HI8", /* name */ |
| 173 | FALSE, /* partial_inplace */ |
| 174 | 0, /* src_mask */ |
| 175 | 0xff, /* dst_mask */ |
| 176 | FALSE), /* pcrel_offset */ |
| 177 | |
| 178 | HOWTO (R_M32C_HI16, /* type */ |
| 179 | 0, /* rightshift */ |
| 180 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
| 181 | 16, /* bitsize */ |
| 182 | FALSE, /* pc_relative */ |
| 183 | 0, /* bitpos */ |
| 184 | complain_overflow_dont, /* complain_on_overflow */ |
| 185 | bfd_elf_generic_reloc, /* special_function */ |
| 186 | "R_M32C_HI16", /* name */ |
| 187 | FALSE, /* partial_inplace */ |
| 188 | 0, /* src_mask */ |
| 189 | 0xffff, /* dst_mask */ |
| 190 | FALSE), /* pcrel_offset */ |
| 191 | |
| 192 | HOWTO (R_M32C_RL_JUMP, /* type */ |
| 193 | 0, /* rightshift */ |
| 194 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 195 | 0, /* bitsize */ |
| 196 | FALSE, /* pc_relative */ |
| 197 | 0, /* bitpos */ |
| 198 | complain_overflow_signed, /* complain_on_overflow */ |
| 199 | bfd_elf_generic_reloc, /* special_function */ |
| 200 | "R_M32C_RL_JUMP", /* name */ |
| 201 | FALSE, /* partial_inplace */ |
| 202 | 0, /* src_mask */ |
| 203 | 0, /* dst_mask */ |
| 204 | FALSE), /* pcrel_offset */ |
| 205 | |
| 206 | HOWTO (R_M32C_RL_1ADDR, /* type */ |
| 207 | 0, /* rightshift */ |
| 208 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 209 | 0, /* bitsize */ |
| 210 | FALSE, /* pc_relative */ |
| 211 | 0, /* bitpos */ |
| 212 | complain_overflow_signed, /* complain_on_overflow */ |
| 213 | bfd_elf_generic_reloc, /* special_function */ |
| 214 | "R_M32C_RL_1ADDR", /* name */ |
| 215 | FALSE, /* partial_inplace */ |
| 216 | 0, /* src_mask */ |
| 217 | 0, /* dst_mask */ |
| 218 | FALSE), /* pcrel_offset */ |
| 219 | |
| 220 | HOWTO (R_M32C_RL_2ADDR, /* type */ |
| 221 | 0, /* rightshift */ |
| 222 | 0, /* size (0 = byte, 1 = short, 2 = long) */ |
| 223 | 0, /* bitsize */ |
| 224 | FALSE, /* pc_relative */ |
| 225 | 0, /* bitpos */ |
| 226 | complain_overflow_signed, /* complain_on_overflow */ |
| 227 | bfd_elf_generic_reloc, /* special_function */ |
| 228 | "R_M32C_RL_2ADDR", /* name */ |
| 229 | FALSE, /* partial_inplace */ |
| 230 | 0, /* src_mask */ |
| 231 | 0, /* dst_mask */ |
| 232 | FALSE), /* pcrel_offset */ |
| 233 | |
| 234 | }; |
| 235 | \f |
| 236 | /* Map BFD reloc types to M32C ELF reloc types. */ |
| 237 | |
| 238 | struct m32c_reloc_map |
| 239 | { |
| 240 | bfd_reloc_code_real_type bfd_reloc_val; |
| 241 | unsigned int m32c_reloc_val; |
| 242 | }; |
| 243 | |
| 244 | static const struct m32c_reloc_map m32c_reloc_map [] = |
| 245 | { |
| 246 | { BFD_RELOC_NONE, R_M32C_NONE }, |
| 247 | { BFD_RELOC_16, R_M32C_16 }, |
| 248 | { BFD_RELOC_24, R_M32C_24 }, |
| 249 | { BFD_RELOC_32, R_M32C_32 }, |
| 250 | { BFD_RELOC_8_PCREL, R_M32C_8_PCREL }, |
| 251 | { BFD_RELOC_16_PCREL, R_M32C_16_PCREL }, |
| 252 | { BFD_RELOC_8, R_M32C_8 }, |
| 253 | { BFD_RELOC_LO16, R_M32C_LO16 }, |
| 254 | { BFD_RELOC_HI16, R_M32C_HI16 }, |
| 255 | { BFD_RELOC_M32C_HI8, R_M32C_HI8 }, |
| 256 | { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP }, |
| 257 | { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR }, |
| 258 | { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR } |
| 259 | }; |
| 260 | |
| 261 | static reloc_howto_type * |
| 262 | m32c_reloc_type_lookup |
| 263 | (bfd * abfd ATTRIBUTE_UNUSED, |
| 264 | bfd_reloc_code_real_type code) |
| 265 | { |
| 266 | unsigned int i; |
| 267 | |
| 268 | for (i = ARRAY_SIZE (m32c_reloc_map); --i;) |
| 269 | if (m32c_reloc_map [i].bfd_reloc_val == code) |
| 270 | return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val]; |
| 271 | |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | static reloc_howto_type * |
| 276 | m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name) |
| 277 | { |
| 278 | unsigned int i; |
| 279 | |
| 280 | for (i = 0; |
| 281 | i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]); |
| 282 | i++) |
| 283 | if (m32c_elf_howto_table[i].name != NULL |
| 284 | && strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0) |
| 285 | return &m32c_elf_howto_table[i]; |
| 286 | |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | /* Set the howto pointer for an M32C ELF reloc. */ |
| 291 | |
| 292 | static void |
| 293 | m32c_info_to_howto_rela |
| 294 | (bfd * abfd ATTRIBUTE_UNUSED, |
| 295 | arelent * cache_ptr, |
| 296 | Elf_Internal_Rela * dst) |
| 297 | { |
| 298 | unsigned int r_type; |
| 299 | |
| 300 | r_type = ELF32_R_TYPE (dst->r_info); |
| 301 | BFD_ASSERT (r_type < (unsigned int) R_M32C_max); |
| 302 | cache_ptr->howto = & m32c_elf_howto_table [r_type]; |
| 303 | } |
| 304 | |
| 305 | \f |
| 306 | |
| 307 | /* Relocate an M32C ELF section. |
| 308 | There is some attempt to make this function usable for many architectures, |
| 309 | both USE_REL and USE_RELA ['twould be nice if such a critter existed], |
| 310 | if only to serve as a learning tool. |
| 311 | |
| 312 | The RELOCATE_SECTION function is called by the new ELF backend linker |
| 313 | to handle the relocations for a section. |
| 314 | |
| 315 | The relocs are always passed as Rela structures; if the section |
| 316 | actually uses Rel structures, the r_addend field will always be |
| 317 | zero. |
| 318 | |
| 319 | This function is responsible for adjusting the section contents as |
| 320 | necessary, and (if using Rela relocs and generating a relocatable |
| 321 | output file) adjusting the reloc addend as necessary. |
| 322 | |
| 323 | This function does not have to worry about setting the reloc |
| 324 | address or the reloc symbol index. |
| 325 | |
| 326 | LOCAL_SYMS is a pointer to the swapped in local symbols. |
| 327 | |
| 328 | LOCAL_SECTIONS is an array giving the section in the input file |
| 329 | corresponding to the st_shndx field of each local symbol. |
| 330 | |
| 331 | The global hash table entry for the global symbols can be found |
| 332 | via elf_sym_hashes (input_bfd). |
| 333 | |
| 334 | When generating relocatable output, this function must handle |
| 335 | STB_LOCAL/STT_SECTION symbols specially. The output symbol is |
| 336 | going to be the section symbol corresponding to the output |
| 337 | section, which means that the addend must be adjusted |
| 338 | accordingly. */ |
| 339 | |
| 340 | static bfd_boolean |
| 341 | m32c_elf_relocate_section |
| 342 | (bfd * output_bfd ATTRIBUTE_UNUSED, |
| 343 | struct bfd_link_info * info, |
| 344 | bfd * input_bfd, |
| 345 | asection * input_section, |
| 346 | bfd_byte * contents, |
| 347 | Elf_Internal_Rela * relocs, |
| 348 | Elf_Internal_Sym * local_syms, |
| 349 | asection ** local_sections) |
| 350 | { |
| 351 | Elf_Internal_Shdr * symtab_hdr; |
| 352 | struct elf_link_hash_entry ** sym_hashes; |
| 353 | Elf_Internal_Rela * rel; |
| 354 | Elf_Internal_Rela * relend; |
| 355 | bfd *dynobj; |
| 356 | asection *splt; |
| 357 | |
| 358 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
| 359 | sym_hashes = elf_sym_hashes (input_bfd); |
| 360 | relend = relocs + input_section->reloc_count; |
| 361 | |
| 362 | dynobj = elf_hash_table (info)->dynobj; |
| 363 | splt = NULL; |
| 364 | if (dynobj != NULL) |
| 365 | splt = bfd_get_linker_section (dynobj, ".plt"); |
| 366 | |
| 367 | for (rel = relocs; rel < relend; rel ++) |
| 368 | { |
| 369 | reloc_howto_type * howto; |
| 370 | unsigned long r_symndx; |
| 371 | Elf_Internal_Sym * sym; |
| 372 | asection * sec; |
| 373 | struct elf_link_hash_entry * h; |
| 374 | bfd_vma relocation; |
| 375 | bfd_reloc_status_type r; |
| 376 | const char * name = NULL; |
| 377 | int r_type; |
| 378 | |
| 379 | r_type = ELF32_R_TYPE (rel->r_info); |
| 380 | |
| 381 | /* These are only used for relaxing; we don't actually relocate |
| 382 | anything with them, so skip them. */ |
| 383 | if (r_type == R_M32C_RL_JUMP |
| 384 | || r_type == R_M32C_RL_1ADDR |
| 385 | || r_type == R_M32C_RL_2ADDR) |
| 386 | continue; |
| 387 | |
| 388 | r_symndx = ELF32_R_SYM (rel->r_info); |
| 389 | |
| 390 | howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info); |
| 391 | h = NULL; |
| 392 | sym = NULL; |
| 393 | sec = NULL; |
| 394 | relocation = 0; |
| 395 | |
| 396 | if (r_symndx < symtab_hdr->sh_info) |
| 397 | { |
| 398 | sym = local_syms + r_symndx; |
| 399 | sec = local_sections [r_symndx]; |
| 400 | relocation = (sec->output_section->vma |
| 401 | + sec->output_offset |
| 402 | + sym->st_value); |
| 403 | |
| 404 | name = bfd_elf_string_from_elf_section |
| 405 | (input_bfd, symtab_hdr->sh_link, sym->st_name); |
| 406 | name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name; |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | h = sym_hashes [r_symndx - symtab_hdr->sh_info]; |
| 411 | |
| 412 | while (h->root.type == bfd_link_hash_indirect |
| 413 | || h->root.type == bfd_link_hash_warning) |
| 414 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 415 | |
| 416 | name = h->root.root.string; |
| 417 | |
| 418 | if (h->root.type == bfd_link_hash_defined |
| 419 | || h->root.type == bfd_link_hash_defweak) |
| 420 | { |
| 421 | sec = h->root.u.def.section; |
| 422 | relocation = (h->root.u.def.value |
| 423 | + sec->output_section->vma |
| 424 | + sec->output_offset); |
| 425 | } |
| 426 | else if (h->root.type == bfd_link_hash_undefweak) |
| 427 | ; |
| 428 | else if (!info->relocatable) |
| 429 | { |
| 430 | if (! ((*info->callbacks->undefined_symbol) |
| 431 | (info, h->root.root.string, input_bfd, |
| 432 | input_section, rel->r_offset, TRUE))) |
| 433 | return FALSE; |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | if (sec != NULL && discarded_section (sec)) |
| 438 | RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, |
| 439 | rel, 1, relend, howto, 0, contents); |
| 440 | |
| 441 | if (info->relocatable) |
| 442 | { |
| 443 | /* This is a relocatable link. We don't have to change |
| 444 | anything, unless the reloc is against a section symbol, |
| 445 | in which case we have to adjust according to where the |
| 446 | section symbol winds up in the output section. */ |
| 447 | if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION) |
| 448 | rel->r_addend += sec->output_offset; |
| 449 | continue; |
| 450 | } |
| 451 | |
| 452 | switch (ELF32_R_TYPE (rel->r_info)) |
| 453 | { |
| 454 | case R_M32C_16: |
| 455 | { |
| 456 | bfd_vma *plt_offset; |
| 457 | |
| 458 | if (h != NULL) |
| 459 | plt_offset = &h->plt.offset; |
| 460 | else |
| 461 | plt_offset = elf_local_got_offsets (input_bfd) + r_symndx; |
| 462 | |
| 463 | /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)", |
| 464 | relocation, *plt_offset);*/ |
| 465 | if (relocation <= 0xffff) |
| 466 | { |
| 467 | /* If the symbol is in range for a 16-bit address, we should |
| 468 | have deallocated the plt entry in relax_section. */ |
| 469 | BFD_ASSERT (*plt_offset == (bfd_vma) -1); |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | /* If the symbol is out of range for a 16-bit address, |
| 474 | we must have allocated a plt entry. */ |
| 475 | BFD_ASSERT (*plt_offset != (bfd_vma) -1); |
| 476 | |
| 477 | /* If this is the first time we've processed this symbol, |
| 478 | fill in the plt entry with the correct symbol address. */ |
| 479 | if ((*plt_offset & 1) == 0) |
| 480 | { |
| 481 | unsigned int x; |
| 482 | |
| 483 | x = 0x000000fc; /* jmpf */ |
| 484 | x |= (relocation << 8) & 0xffffff00; |
| 485 | bfd_put_32 (input_bfd, x, splt->contents + *plt_offset); |
| 486 | *plt_offset |= 1; |
| 487 | } |
| 488 | |
| 489 | relocation = (splt->output_section->vma |
| 490 | + splt->output_offset |
| 491 | + (*plt_offset & -2)); |
| 492 | if (name) |
| 493 | { |
| 494 | char *newname = bfd_malloc (strlen(name)+5); |
| 495 | strcpy (newname, name); |
| 496 | strcat(newname, ".plt"); |
| 497 | _bfd_generic_link_add_one_symbol (info, |
| 498 | input_bfd, |
| 499 | newname, |
| 500 | BSF_FUNCTION | BSF_WEAK, |
| 501 | splt, |
| 502 | (*plt_offset & -2), |
| 503 | 0, |
| 504 | 1, |
| 505 | 0, |
| 506 | 0); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | break; |
| 511 | |
| 512 | case R_M32C_HI8: |
| 513 | case R_M32C_HI16: |
| 514 | relocation >>= 16; |
| 515 | break; |
| 516 | } |
| 517 | |
| 518 | #if 0 |
| 519 | printf ("relocate %s at %06lx relocation %06lx addend %ld ", |
| 520 | m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name, |
| 521 | rel->r_offset + input_section->output_section->vma + input_section->output_offset, |
| 522 | relocation, rel->r_addend); |
| 523 | { |
| 524 | int i; |
| 525 | for (i=0; i<4; i++) |
| 526 | printf (" %02x", contents[rel->r_offset+i]); |
| 527 | printf ("\n"); |
| 528 | } |
| 529 | #endif |
| 530 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
| 531 | contents, rel->r_offset, relocation, |
| 532 | rel->r_addend); |
| 533 | |
| 534 | if (r != bfd_reloc_ok) |
| 535 | { |
| 536 | const char * msg = (const char *) NULL; |
| 537 | |
| 538 | switch (r) |
| 539 | { |
| 540 | case bfd_reloc_overflow: |
| 541 | r = info->callbacks->reloc_overflow |
| 542 | (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0, |
| 543 | input_bfd, input_section, rel->r_offset); |
| 544 | break; |
| 545 | |
| 546 | case bfd_reloc_undefined: |
| 547 | r = info->callbacks->undefined_symbol |
| 548 | (info, name, input_bfd, input_section, rel->r_offset, |
| 549 | TRUE); |
| 550 | break; |
| 551 | |
| 552 | case bfd_reloc_outofrange: |
| 553 | msg = _("internal error: out of range error"); |
| 554 | break; |
| 555 | |
| 556 | case bfd_reloc_notsupported: |
| 557 | msg = _("internal error: unsupported relocation error"); |
| 558 | break; |
| 559 | |
| 560 | case bfd_reloc_dangerous: |
| 561 | msg = _("internal error: dangerous relocation"); |
| 562 | break; |
| 563 | |
| 564 | default: |
| 565 | msg = _("internal error: unknown error"); |
| 566 | break; |
| 567 | } |
| 568 | |
| 569 | if (msg) |
| 570 | r = info->callbacks->warning |
| 571 | (info, msg, name, input_bfd, input_section, rel->r_offset); |
| 572 | |
| 573 | if (! r) |
| 574 | return FALSE; |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | return TRUE; |
| 579 | } |
| 580 | \f |
| 581 | /* We support 16-bit pointers to code above 64k by generating a thunk |
| 582 | below 64k containing a JMP instruction to the final address. */ |
| 583 | |
| 584 | static bfd_boolean |
| 585 | m32c_elf_check_relocs |
| 586 | (bfd * abfd, |
| 587 | struct bfd_link_info * info, |
| 588 | asection * sec, |
| 589 | const Elf_Internal_Rela * relocs) |
| 590 | { |
| 591 | Elf_Internal_Shdr * symtab_hdr; |
| 592 | struct elf_link_hash_entry ** sym_hashes; |
| 593 | const Elf_Internal_Rela * rel; |
| 594 | const Elf_Internal_Rela * rel_end; |
| 595 | bfd_vma *local_plt_offsets; |
| 596 | asection *splt; |
| 597 | bfd *dynobj; |
| 598 | |
| 599 | if (info->relocatable) |
| 600 | return TRUE; |
| 601 | |
| 602 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 603 | sym_hashes = elf_sym_hashes (abfd); |
| 604 | local_plt_offsets = elf_local_got_offsets (abfd); |
| 605 | splt = NULL; |
| 606 | dynobj = elf_hash_table(info)->dynobj; |
| 607 | |
| 608 | rel_end = relocs + sec->reloc_count; |
| 609 | for (rel = relocs; rel < rel_end; rel++) |
| 610 | { |
| 611 | struct elf_link_hash_entry *h; |
| 612 | unsigned long r_symndx; |
| 613 | bfd_vma *offset; |
| 614 | |
| 615 | r_symndx = ELF32_R_SYM (rel->r_info); |
| 616 | if (r_symndx < symtab_hdr->sh_info) |
| 617 | h = NULL; |
| 618 | else |
| 619 | { |
| 620 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
| 621 | while (h->root.type == bfd_link_hash_indirect |
| 622 | || h->root.type == bfd_link_hash_warning) |
| 623 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
| 624 | |
| 625 | /* PR15323, ref flags aren't set for references in the same |
| 626 | object. */ |
| 627 | h->root.non_ir_ref = 1; |
| 628 | } |
| 629 | |
| 630 | switch (ELF32_R_TYPE (rel->r_info)) |
| 631 | { |
| 632 | /* This relocation describes a 16-bit pointer to a function. |
| 633 | We may need to allocate a thunk in low memory; reserve memory |
| 634 | for it now. */ |
| 635 | case R_M32C_16: |
| 636 | if (dynobj == NULL) |
| 637 | elf_hash_table (info)->dynobj = dynobj = abfd; |
| 638 | if (splt == NULL) |
| 639 | { |
| 640 | splt = bfd_get_linker_section (dynobj, ".plt"); |
| 641 | if (splt == NULL) |
| 642 | { |
| 643 | flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS |
| 644 | | SEC_IN_MEMORY | SEC_LINKER_CREATED |
| 645 | | SEC_READONLY | SEC_CODE); |
| 646 | splt = bfd_make_section_anyway_with_flags (dynobj, ".plt", |
| 647 | flags); |
| 648 | if (splt == NULL |
| 649 | || ! bfd_set_section_alignment (dynobj, splt, 1)) |
| 650 | return FALSE; |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (h != NULL) |
| 655 | offset = &h->plt.offset; |
| 656 | else |
| 657 | { |
| 658 | if (local_plt_offsets == NULL) |
| 659 | { |
| 660 | size_t size; |
| 661 | unsigned int i; |
| 662 | |
| 663 | size = symtab_hdr->sh_info * sizeof (bfd_vma); |
| 664 | local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size); |
| 665 | if (local_plt_offsets == NULL) |
| 666 | return FALSE; |
| 667 | elf_local_got_offsets (abfd) = local_plt_offsets; |
| 668 | |
| 669 | for (i = 0; i < symtab_hdr->sh_info; i++) |
| 670 | local_plt_offsets[i] = (bfd_vma) -1; |
| 671 | } |
| 672 | offset = &local_plt_offsets[r_symndx]; |
| 673 | } |
| 674 | |
| 675 | if (*offset == (bfd_vma) -1) |
| 676 | { |
| 677 | *offset = splt->size; |
| 678 | splt->size += 4; |
| 679 | } |
| 680 | break; |
| 681 | } |
| 682 | } |
| 683 | |
| 684 | return TRUE; |
| 685 | } |
| 686 | |
| 687 | /* This must exist if dynobj is ever set. */ |
| 688 | |
| 689 | static bfd_boolean |
| 690 | m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED, |
| 691 | struct bfd_link_info *info) |
| 692 | { |
| 693 | bfd *dynobj; |
| 694 | asection *splt; |
| 695 | |
| 696 | /* As an extra sanity check, verify that all plt entries have |
| 697 | been filled in. */ |
| 698 | |
| 699 | if ((dynobj = elf_hash_table (info)->dynobj) != NULL |
| 700 | && (splt = bfd_get_linker_section (dynobj, ".plt")) != NULL) |
| 701 | { |
| 702 | bfd_byte *contents = splt->contents; |
| 703 | unsigned int i, size = splt->size; |
| 704 | for (i = 0; i < size; i += 4) |
| 705 | { |
| 706 | unsigned int x = bfd_get_32 (dynobj, contents + i); |
| 707 | BFD_ASSERT (x != 0); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | return TRUE; |
| 712 | } |
| 713 | |
| 714 | static bfd_boolean |
| 715 | m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED, |
| 716 | struct bfd_link_info *info) |
| 717 | { |
| 718 | bfd *dynobj; |
| 719 | asection *splt; |
| 720 | |
| 721 | if (info->relocatable) |
| 722 | return TRUE; |
| 723 | |
| 724 | dynobj = elf_hash_table (info)->dynobj; |
| 725 | if (dynobj == NULL) |
| 726 | return TRUE; |
| 727 | |
| 728 | splt = bfd_get_linker_section (dynobj, ".plt"); |
| 729 | BFD_ASSERT (splt != NULL); |
| 730 | |
| 731 | splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size); |
| 732 | if (splt->contents == NULL) |
| 733 | return FALSE; |
| 734 | |
| 735 | return TRUE; |
| 736 | } |
| 737 | \f |
| 738 | /* Function to set the ELF flag bits. */ |
| 739 | |
| 740 | static bfd_boolean |
| 741 | m32c_elf_set_private_flags (bfd *abfd, flagword flags) |
| 742 | { |
| 743 | elf_elfheader (abfd)->e_flags = flags; |
| 744 | elf_flags_init (abfd) = TRUE; |
| 745 | return TRUE; |
| 746 | } |
| 747 | |
| 748 | /* Merge backend specific data from an object file to the output |
| 749 | object file when linking. */ |
| 750 | |
| 751 | static bfd_boolean |
| 752 | m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd) |
| 753 | { |
| 754 | flagword old_flags, old_partial; |
| 755 | flagword new_flags, new_partial; |
| 756 | bfd_boolean error = FALSE; |
| 757 | char new_opt[80]; |
| 758 | char old_opt[80]; |
| 759 | |
| 760 | new_opt[0] = old_opt[0] = '\0'; |
| 761 | new_flags = elf_elfheader (ibfd)->e_flags; |
| 762 | old_flags = elf_elfheader (obfd)->e_flags; |
| 763 | |
| 764 | #ifdef DEBUG |
| 765 | (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s", |
| 766 | old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no", |
| 767 | bfd_get_filename (ibfd)); |
| 768 | #endif |
| 769 | |
| 770 | if (!elf_flags_init (obfd)) |
| 771 | { |
| 772 | /* First call, no flags set. */ |
| 773 | elf_flags_init (obfd) = TRUE; |
| 774 | elf_elfheader (obfd)->e_flags = new_flags; |
| 775 | } |
| 776 | |
| 777 | else if (new_flags == old_flags) |
| 778 | /* Compatible flags are ok. */ |
| 779 | ; |
| 780 | |
| 781 | else /* Possibly incompatible flags. */ |
| 782 | { |
| 783 | /* Warn if different cpu is used (allow a specific cpu to override |
| 784 | the generic cpu). */ |
| 785 | new_partial = (new_flags & EF_M32C_CPU_MASK); |
| 786 | old_partial = (old_flags & EF_M32C_CPU_MASK); |
| 787 | if (new_partial == old_partial) |
| 788 | ; |
| 789 | |
| 790 | else |
| 791 | { |
| 792 | switch (new_partial) |
| 793 | { |
| 794 | default: strcat (new_opt, " -m16c"); break; |
| 795 | case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break; |
| 796 | case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break; |
| 797 | } |
| 798 | |
| 799 | switch (old_partial) |
| 800 | { |
| 801 | default: strcat (old_opt, " -m16c"); break; |
| 802 | case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break; |
| 803 | case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break; |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /* Print out any mismatches from above. */ |
| 808 | if (new_opt[0]) |
| 809 | { |
| 810 | error = TRUE; |
| 811 | (*_bfd_error_handler) |
| 812 | (_("%s: compiled with %s and linked with modules compiled with %s"), |
| 813 | bfd_get_filename (ibfd), new_opt, old_opt); |
| 814 | } |
| 815 | |
| 816 | new_flags &= ~ EF_M32C_ALL_FLAGS; |
| 817 | old_flags &= ~ EF_M32C_ALL_FLAGS; |
| 818 | |
| 819 | /* Warn about any other mismatches. */ |
| 820 | if (new_flags != old_flags) |
| 821 | { |
| 822 | error = TRUE; |
| 823 | (*_bfd_error_handler) |
| 824 | (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"), |
| 825 | bfd_get_filename (ibfd), (long)new_flags, (long)old_flags); |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | if (error) |
| 830 | bfd_set_error (bfd_error_bad_value); |
| 831 | |
| 832 | return !error; |
| 833 | } |
| 834 | |
| 835 | \f |
| 836 | static bfd_boolean |
| 837 | m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr) |
| 838 | { |
| 839 | FILE *file = (FILE *) ptr; |
| 840 | flagword flags; |
| 841 | |
| 842 | BFD_ASSERT (abfd != NULL && ptr != NULL); |
| 843 | |
| 844 | /* Print normal ELF private data. */ |
| 845 | _bfd_elf_print_private_bfd_data (abfd, ptr); |
| 846 | |
| 847 | flags = elf_elfheader (abfd)->e_flags; |
| 848 | fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags); |
| 849 | |
| 850 | switch (flags & EF_M32C_CPU_MASK) |
| 851 | { |
| 852 | default: break; |
| 853 | case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break; |
| 854 | case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break; |
| 855 | } |
| 856 | |
| 857 | fputc ('\n', file); |
| 858 | return TRUE; |
| 859 | } |
| 860 | |
| 861 | /* Return the MACH for an e_flags value. */ |
| 862 | |
| 863 | static int |
| 864 | elf32_m32c_machine (bfd *abfd) |
| 865 | { |
| 866 | switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) |
| 867 | { |
| 868 | case EF_M32C_CPU_M16C: return bfd_mach_m16c; |
| 869 | case EF_M32C_CPU_M32C: return bfd_mach_m32c; |
| 870 | } |
| 871 | |
| 872 | return bfd_mach_m16c; |
| 873 | } |
| 874 | |
| 875 | static bfd_boolean |
| 876 | m32c_elf_object_p (bfd *abfd) |
| 877 | { |
| 878 | bfd_default_set_arch_mach (abfd, bfd_arch_m32c, |
| 879 | elf32_m32c_machine (abfd)); |
| 880 | return TRUE; |
| 881 | } |
| 882 | \f |
| 883 | |
| 884 | #ifdef DEBUG |
| 885 | void |
| 886 | dump_symtab (bfd * abfd, void *internal_syms, void *external_syms) |
| 887 | { |
| 888 | size_t locsymcount; |
| 889 | Elf_Internal_Sym *isymbuf; |
| 890 | Elf_Internal_Sym *isymend; |
| 891 | Elf_Internal_Sym *isym; |
| 892 | Elf_Internal_Shdr *symtab_hdr; |
| 893 | bfd_boolean free_internal = 0, free_external = 0; |
| 894 | char * st_info_str; |
| 895 | char * st_info_stb_str; |
| 896 | char * st_other_str; |
| 897 | char * st_shndx_str; |
| 898 | |
| 899 | if (! internal_syms) |
| 900 | { |
| 901 | internal_syms = bfd_malloc (1000); |
| 902 | free_internal = 1; |
| 903 | } |
| 904 | if (! external_syms) |
| 905 | { |
| 906 | external_syms = bfd_malloc (1000); |
| 907 | free_external = 1; |
| 908 | } |
| 909 | |
| 910 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 911 | locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym; |
| 912 | if (free_internal) |
| 913 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, |
| 914 | symtab_hdr->sh_info, 0, |
| 915 | internal_syms, external_syms, NULL); |
| 916 | else |
| 917 | isymbuf = internal_syms; |
| 918 | isymend = isymbuf + locsymcount; |
| 919 | |
| 920 | for (isym = isymbuf ; isym < isymend ; isym++) |
| 921 | { |
| 922 | switch (ELF_ST_TYPE (isym->st_info)) |
| 923 | { |
| 924 | case STT_FUNC: |
| 925 | st_info_str = "STT_FUNC"; |
| 926 | break; |
| 927 | |
| 928 | case STT_SECTION: |
| 929 | st_info_str = "STT_SECTION"; |
| 930 | break; |
| 931 | |
| 932 | case STT_FILE: |
| 933 | st_info_str = "STT_FILE"; |
| 934 | break; |
| 935 | |
| 936 | case STT_OBJECT: |
| 937 | st_info_str = "STT_OBJECT"; |
| 938 | break; |
| 939 | |
| 940 | case STT_TLS: |
| 941 | st_info_str = "STT_TLS"; |
| 942 | break; |
| 943 | |
| 944 | default: |
| 945 | st_info_str = ""; |
| 946 | } |
| 947 | |
| 948 | switch (ELF_ST_BIND (isym->st_info)) |
| 949 | { |
| 950 | case STB_LOCAL: |
| 951 | st_info_stb_str = "STB_LOCAL"; |
| 952 | break; |
| 953 | |
| 954 | case STB_GLOBAL: |
| 955 | st_info_stb_str = "STB_GLOBAL"; |
| 956 | break; |
| 957 | |
| 958 | default: |
| 959 | st_info_stb_str = ""; |
| 960 | } |
| 961 | |
| 962 | switch (ELF_ST_VISIBILITY (isym->st_other)) |
| 963 | { |
| 964 | case STV_DEFAULT: |
| 965 | st_other_str = "STV_DEFAULT"; |
| 966 | break; |
| 967 | |
| 968 | case STV_INTERNAL: |
| 969 | st_other_str = "STV_INTERNAL"; |
| 970 | break; |
| 971 | |
| 972 | case STV_PROTECTED: |
| 973 | st_other_str = "STV_PROTECTED"; |
| 974 | break; |
| 975 | |
| 976 | default: |
| 977 | st_other_str = ""; |
| 978 | } |
| 979 | |
| 980 | switch (isym->st_shndx) |
| 981 | { |
| 982 | case SHN_ABS: |
| 983 | st_shndx_str = "SHN_ABS"; |
| 984 | break; |
| 985 | |
| 986 | case SHN_COMMON: |
| 987 | st_shndx_str = "SHN_COMMON"; |
| 988 | break; |
| 989 | |
| 990 | case SHN_UNDEF: |
| 991 | st_shndx_str = "SHN_UNDEF"; |
| 992 | break; |
| 993 | |
| 994 | default: |
| 995 | st_shndx_str = ""; |
| 996 | } |
| 997 | |
| 998 | printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s " |
| 999 | "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n", |
| 1000 | isym, |
| 1001 | (unsigned long) isym->st_value, |
| 1002 | (unsigned long) isym->st_size, |
| 1003 | isym->st_name, |
| 1004 | bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link, |
| 1005 | isym->st_name), |
| 1006 | isym->st_info, st_info_str, st_info_stb_str, |
| 1007 | isym->st_other, st_other_str, |
| 1008 | isym->st_shndx, st_shndx_str); |
| 1009 | } |
| 1010 | if (free_internal) |
| 1011 | free (internal_syms); |
| 1012 | if (free_external) |
| 1013 | free (external_syms); |
| 1014 | } |
| 1015 | |
| 1016 | char * |
| 1017 | m32c_get_reloc (long reloc) |
| 1018 | { |
| 1019 | if (0 <= reloc && reloc < R_M32C_max) |
| 1020 | return m32c_elf_howto_table[reloc].name; |
| 1021 | else |
| 1022 | return ""; |
| 1023 | } |
| 1024 | #endif /* DEBUG */ |
| 1025 | |
| 1026 | /* Handle relaxing. */ |
| 1027 | |
| 1028 | /* A subroutine of m32c_elf_relax_section. If the global symbol H |
| 1029 | is within the low 64k, remove any entry for it in the plt. */ |
| 1030 | |
| 1031 | struct relax_plt_data |
| 1032 | { |
| 1033 | asection *splt; |
| 1034 | bfd_boolean *again; |
| 1035 | }; |
| 1036 | |
| 1037 | static bfd_boolean |
| 1038 | m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata) |
| 1039 | { |
| 1040 | struct relax_plt_data *data = (struct relax_plt_data *) xdata; |
| 1041 | |
| 1042 | if (h->plt.offset != (bfd_vma) -1) |
| 1043 | { |
| 1044 | bfd_vma address; |
| 1045 | |
| 1046 | if (h->root.type == bfd_link_hash_undefined |
| 1047 | || h->root.type == bfd_link_hash_undefweak) |
| 1048 | address = 0; |
| 1049 | else |
| 1050 | address = (h->root.u.def.section->output_section->vma |
| 1051 | + h->root.u.def.section->output_offset |
| 1052 | + h->root.u.def.value); |
| 1053 | |
| 1054 | if (address <= 0xffff) |
| 1055 | { |
| 1056 | h->plt.offset = -1; |
| 1057 | data->splt->size -= 4; |
| 1058 | *data->again = TRUE; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | return TRUE; |
| 1063 | } |
| 1064 | |
| 1065 | /* A subroutine of m32c_elf_relax_section. If the global symbol H |
| 1066 | previously had a plt entry, give it a new entry offset. */ |
| 1067 | |
| 1068 | static bfd_boolean |
| 1069 | m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata) |
| 1070 | { |
| 1071 | bfd_vma *entry = (bfd_vma *) xdata; |
| 1072 | |
| 1073 | if (h->plt.offset != (bfd_vma) -1) |
| 1074 | { |
| 1075 | h->plt.offset = *entry; |
| 1076 | *entry += 4; |
| 1077 | } |
| 1078 | |
| 1079 | return TRUE; |
| 1080 | } |
| 1081 | |
| 1082 | static bfd_boolean |
| 1083 | m32c_elf_relax_plt_section (asection *splt, |
| 1084 | struct bfd_link_info *info, |
| 1085 | bfd_boolean *again) |
| 1086 | { |
| 1087 | struct relax_plt_data relax_plt_data; |
| 1088 | bfd *ibfd; |
| 1089 | |
| 1090 | /* Assume nothing changes. */ |
| 1091 | *again = FALSE; |
| 1092 | |
| 1093 | if (info->relocatable) |
| 1094 | return TRUE; |
| 1095 | |
| 1096 | /* Quick check for an empty plt. */ |
| 1097 | if (splt->size == 0) |
| 1098 | return TRUE; |
| 1099 | |
| 1100 | /* Map across all global symbols; see which ones happen to |
| 1101 | fall in the low 64k. */ |
| 1102 | relax_plt_data.splt = splt; |
| 1103 | relax_plt_data.again = again; |
| 1104 | elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check, |
| 1105 | &relax_plt_data); |
| 1106 | |
| 1107 | /* Likewise for local symbols, though that's somewhat less convenient |
| 1108 | as we have to walk the list of input bfds and swap in symbol data. */ |
| 1109 | for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next) |
| 1110 | { |
| 1111 | bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd); |
| 1112 | Elf_Internal_Shdr *symtab_hdr; |
| 1113 | Elf_Internal_Sym *isymbuf = NULL; |
| 1114 | unsigned int idx; |
| 1115 | |
| 1116 | if (! local_plt_offsets) |
| 1117 | continue; |
| 1118 | |
| 1119 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; |
| 1120 | if (symtab_hdr->sh_info != 0) |
| 1121 | { |
| 1122 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; |
| 1123 | if (isymbuf == NULL) |
| 1124 | isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr, |
| 1125 | symtab_hdr->sh_info, 0, |
| 1126 | NULL, NULL, NULL); |
| 1127 | if (isymbuf == NULL) |
| 1128 | return FALSE; |
| 1129 | } |
| 1130 | |
| 1131 | for (idx = 0; idx < symtab_hdr->sh_info; ++idx) |
| 1132 | { |
| 1133 | Elf_Internal_Sym *isym; |
| 1134 | asection *tsec; |
| 1135 | bfd_vma address; |
| 1136 | |
| 1137 | if (local_plt_offsets[idx] == (bfd_vma) -1) |
| 1138 | continue; |
| 1139 | |
| 1140 | isym = &isymbuf[idx]; |
| 1141 | if (isym->st_shndx == SHN_UNDEF) |
| 1142 | continue; |
| 1143 | else if (isym->st_shndx == SHN_ABS) |
| 1144 | tsec = bfd_abs_section_ptr; |
| 1145 | else if (isym->st_shndx == SHN_COMMON) |
| 1146 | tsec = bfd_com_section_ptr; |
| 1147 | else |
| 1148 | tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx); |
| 1149 | |
| 1150 | address = (tsec->output_section->vma |
| 1151 | + tsec->output_offset |
| 1152 | + isym->st_value); |
| 1153 | if (address <= 0xffff) |
| 1154 | { |
| 1155 | local_plt_offsets[idx] = -1; |
| 1156 | splt->size -= 4; |
| 1157 | *again = TRUE; |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | if (isymbuf != NULL |
| 1162 | && symtab_hdr->contents != (unsigned char *) isymbuf) |
| 1163 | { |
| 1164 | if (! info->keep_memory) |
| 1165 | free (isymbuf); |
| 1166 | else |
| 1167 | { |
| 1168 | /* Cache the symbols for elf_link_input_bfd. */ |
| 1169 | symtab_hdr->contents = (unsigned char *) isymbuf; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | /* If we changed anything, walk the symbols again to reallocate |
| 1175 | .plt entry addresses. */ |
| 1176 | if (*again && splt->size > 0) |
| 1177 | { |
| 1178 | bfd_vma entry = 0; |
| 1179 | |
| 1180 | elf_link_hash_traverse (elf_hash_table (info), |
| 1181 | m32c_relax_plt_realloc, &entry); |
| 1182 | |
| 1183 | for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next) |
| 1184 | { |
| 1185 | bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd); |
| 1186 | unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info; |
| 1187 | unsigned int idx; |
| 1188 | |
| 1189 | if (! local_plt_offsets) |
| 1190 | continue; |
| 1191 | |
| 1192 | for (idx = 0; idx < nlocals; ++idx) |
| 1193 | if (local_plt_offsets[idx] != (bfd_vma) -1) |
| 1194 | { |
| 1195 | local_plt_offsets[idx] = entry; |
| 1196 | entry += 4; |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | return TRUE; |
| 1202 | } |
| 1203 | |
| 1204 | static int |
| 1205 | compare_reloc (const void *e1, const void *e2) |
| 1206 | { |
| 1207 | const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1; |
| 1208 | const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2; |
| 1209 | |
| 1210 | if (i1->r_offset == i2->r_offset) |
| 1211 | return 0; |
| 1212 | else |
| 1213 | return i1->r_offset < i2->r_offset ? -1 : 1; |
| 1214 | } |
| 1215 | |
| 1216 | #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms) |
| 1217 | static bfd_vma |
| 1218 | m32c_offset_for_reloc (bfd *abfd, |
| 1219 | Elf_Internal_Rela *rel, |
| 1220 | Elf_Internal_Shdr *symtab_hdr, |
| 1221 | Elf_External_Sym_Shndx *shndx_buf ATTRIBUTE_UNUSED, |
| 1222 | Elf_Internal_Sym *intsyms) |
| 1223 | { |
| 1224 | bfd_vma symval; |
| 1225 | |
| 1226 | /* Get the value of the symbol referred to by the reloc. */ |
| 1227 | if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info) |
| 1228 | { |
| 1229 | /* A local symbol. */ |
| 1230 | Elf_Internal_Sym *isym; |
| 1231 | asection *ssec; |
| 1232 | |
| 1233 | isym = intsyms + ELF32_R_SYM (rel->r_info); |
| 1234 | ssec = bfd_section_from_elf_index (abfd, isym->st_shndx); |
| 1235 | symval = isym->st_value; |
| 1236 | if (ssec) |
| 1237 | symval += ssec->output_section->vma |
| 1238 | + ssec->output_offset; |
| 1239 | } |
| 1240 | else |
| 1241 | { |
| 1242 | unsigned long indx; |
| 1243 | struct elf_link_hash_entry *h; |
| 1244 | |
| 1245 | /* An external symbol. */ |
| 1246 | indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info; |
| 1247 | h = elf_sym_hashes (abfd)[indx]; |
| 1248 | BFD_ASSERT (h != NULL); |
| 1249 | |
| 1250 | if (h->root.type != bfd_link_hash_defined |
| 1251 | && h->root.type != bfd_link_hash_defweak) |
| 1252 | /* This appears to be a reference to an undefined |
| 1253 | symbol. Just ignore it--it will be caught by the |
| 1254 | regular reloc processing. */ |
| 1255 | return 0; |
| 1256 | |
| 1257 | symval = (h->root.u.def.value |
| 1258 | + h->root.u.def.section->output_section->vma |
| 1259 | + h->root.u.def.section->output_offset); |
| 1260 | } |
| 1261 | return symval; |
| 1262 | } |
| 1263 | |
| 1264 | static int bytes_saved = 0; |
| 1265 | |
| 1266 | static int bytes_to_reloc[] = { |
| 1267 | R_M32C_NONE, |
| 1268 | R_M32C_8, |
| 1269 | R_M32C_16, |
| 1270 | R_M32C_24, |
| 1271 | R_M32C_32 |
| 1272 | }; |
| 1273 | |
| 1274 | /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */ |
| 1275 | |
| 1276 | /* Mask for the number of relocs associated with this insn. */ |
| 1277 | #define RLA_RELOCS 0x0000000f |
| 1278 | /* Number of bytes gas emitted (before gas's relaxing) */ |
| 1279 | #define RLA_NBYTES 0x00000ff0 |
| 1280 | |
| 1281 | /* If the displacement is within the given range and the new encoding |
| 1282 | differs from the old encoding (the index), then the insn can be |
| 1283 | relaxed to the new encoding. */ |
| 1284 | typedef struct { |
| 1285 | int bytes; |
| 1286 | unsigned int max_disp; |
| 1287 | unsigned char new_encoding; |
| 1288 | } EncodingTable; |
| 1289 | |
| 1290 | static EncodingTable m16c_addr_encodings[] = { |
| 1291 | { 0, 0, 0 }, /* R0 */ |
| 1292 | { 0, 0, 1 }, /* R1 */ |
| 1293 | { 0, 0, 2 }, /* R2 */ |
| 1294 | { 0, 0, 3 }, /* R3 */ |
| 1295 | { 0, 0, 4 }, /* A0 */ |
| 1296 | { 0, 0, 5 }, /* A1 */ |
| 1297 | { 0, 0, 6 }, /* [A0] */ |
| 1298 | { 0, 0, 7 }, /* [A1] */ |
| 1299 | { 1, 0, 6 }, /* udsp:8[A0] */ |
| 1300 | { 1, 0, 7 }, /* udsp:8[A1] */ |
| 1301 | { 1, 0, 10 }, /* udsp:8[SB] */ |
| 1302 | { 1, 0, 11 }, /* sdsp:8[FB] */ |
| 1303 | { 2, 255, 8 }, /* udsp:16[A0] */ |
| 1304 | { 2, 255, 9 }, /* udsp:16[A1] */ |
| 1305 | { 2, 255, 10 }, /* udsp:16[SB] */ |
| 1306 | { 2, 0, 15 }, /* abs:16 */ |
| 1307 | }; |
| 1308 | |
| 1309 | static EncodingTable m16c_jmpaddr_encodings[] = { |
| 1310 | { 0, 0, 0 }, /* R0 */ |
| 1311 | { 0, 0, 1 }, /* R1 */ |
| 1312 | { 0, 0, 2 }, /* R2 */ |
| 1313 | { 0, 0, 3 }, /* R3 */ |
| 1314 | { 0, 0, 4 }, /* A0 */ |
| 1315 | { 0, 0, 5 }, /* A1 */ |
| 1316 | { 0, 0, 6 }, /* [A0] */ |
| 1317 | { 0, 0, 7 }, /* [A1] */ |
| 1318 | { 1, 0, 6 }, /* udsp:8[A0] */ |
| 1319 | { 1, 0, 7 }, /* udsp:8[A1] */ |
| 1320 | { 1, 0, 10 }, /* udsp:8[SB] */ |
| 1321 | { 1, 0, 11 }, /* sdsp:8[FB] */ |
| 1322 | { 3, 255, 8 }, /* udsp:20[A0] */ |
| 1323 | { 3, 255, 9 }, /* udsp:20[A1] */ |
| 1324 | { 2, 255, 10 }, /* udsp:16[SB] */ |
| 1325 | { 2, 0, 15 }, /* abs:16 */ |
| 1326 | }; |
| 1327 | |
| 1328 | static EncodingTable m32c_addr_encodings[] = { |
| 1329 | { 0, 0, 0 }, /* [A0] */ |
| 1330 | { 0, 0, 1 }, /* [A1] */ |
| 1331 | { 0, 0, 2 }, /* A0 */ |
| 1332 | { 0, 0, 3 }, /* A1 */ |
| 1333 | { 1, 0, 0 }, /* udsp:8[A0] */ |
| 1334 | { 1, 0, 1 }, /* udsp:8[A1] */ |
| 1335 | { 1, 0, 6 }, /* udsp:8[SB] */ |
| 1336 | { 1, 0, 7 }, /* sdsp:8[FB] */ |
| 1337 | { 2, 255, 4 }, /* udsp:16[A0] */ |
| 1338 | { 2, 255, 5 }, /* udsp:16[A1] */ |
| 1339 | { 2, 255, 6 }, /* udsp:16[SB] */ |
| 1340 | { 2, 127, 7 }, /* sdsp:16[FB] */ |
| 1341 | { 3, 65535, 8 }, /* udsp:24[A0] */ |
| 1342 | { 3, 65535, 9 }, /* udsp:24[A1] */ |
| 1343 | { 3, 65535, 15 }, /* abs24 */ |
| 1344 | { 2, 0, 15 }, /* abs16 */ |
| 1345 | { 0, 0, 16 }, /* R2 */ |
| 1346 | { 0, 0, 17 }, /* R3 */ |
| 1347 | { 0, 0, 18 }, /* R0 */ |
| 1348 | { 0, 0, 19 }, /* R1 */ |
| 1349 | { 0, 0, 20 }, /* */ |
| 1350 | { 0, 0, 21 }, /* */ |
| 1351 | { 0, 0, 22 }, /* */ |
| 1352 | { 0, 0, 23 }, /* */ |
| 1353 | { 0, 0, 24 }, /* */ |
| 1354 | { 0, 0, 25 }, /* */ |
| 1355 | { 0, 0, 26 }, /* */ |
| 1356 | { 0, 0, 27 }, /* */ |
| 1357 | { 0, 0, 28 }, /* */ |
| 1358 | { 0, 0, 29 }, /* */ |
| 1359 | { 0, 0, 30 }, /* */ |
| 1360 | { 0, 0, 31 }, /* */ |
| 1361 | }; |
| 1362 | |
| 1363 | static bfd_boolean |
| 1364 | m32c_elf_relax_section |
| 1365 | (bfd * abfd, |
| 1366 | asection * sec, |
| 1367 | struct bfd_link_info * link_info, |
| 1368 | bfd_boolean * again) |
| 1369 | { |
| 1370 | Elf_Internal_Shdr *symtab_hdr; |
| 1371 | Elf_Internal_Shdr *shndx_hdr; |
| 1372 | Elf_Internal_Rela *internal_relocs; |
| 1373 | Elf_Internal_Rela *free_relocs = NULL; |
| 1374 | Elf_Internal_Rela *irel, *irelend, *srel; |
| 1375 | bfd_byte * contents = NULL; |
| 1376 | bfd_byte * free_contents = NULL; |
| 1377 | Elf_Internal_Sym *intsyms = NULL; |
| 1378 | Elf_Internal_Sym *free_intsyms = NULL; |
| 1379 | Elf_External_Sym_Shndx *shndx_buf = NULL; |
| 1380 | int machine; |
| 1381 | |
| 1382 | if (abfd == elf_hash_table (link_info)->dynobj |
| 1383 | && (sec->flags & SEC_LINKER_CREATED) != 0 |
| 1384 | && strcmp (sec->name, ".plt") == 0) |
| 1385 | return m32c_elf_relax_plt_section (sec, link_info, again); |
| 1386 | |
| 1387 | /* Assume nothing changes. */ |
| 1388 | *again = FALSE; |
| 1389 | |
| 1390 | machine = elf32_m32c_machine (abfd); |
| 1391 | |
| 1392 | /* We don't have to do anything for a relocatable link, if |
| 1393 | this section does not have relocs, or if this is not a |
| 1394 | code section. */ |
| 1395 | if (link_info->relocatable |
| 1396 | || (sec->flags & SEC_RELOC) == 0 |
| 1397 | || sec->reloc_count == 0 |
| 1398 | || (sec->flags & SEC_CODE) == 0) |
| 1399 | return TRUE; |
| 1400 | |
| 1401 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
| 1402 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; |
| 1403 | |
| 1404 | /* Get the section contents. */ |
| 1405 | if (elf_section_data (sec)->this_hdr.contents != NULL) |
| 1406 | contents = elf_section_data (sec)->this_hdr.contents; |
| 1407 | /* Go get them off disk. */ |
| 1408 | else if (!bfd_malloc_and_get_section (abfd, sec, &contents)) |
| 1409 | goto error_return; |
| 1410 | |
| 1411 | /* Read this BFD's symbols. */ |
| 1412 | /* Get cached copy if it exists. */ |
| 1413 | if (symtab_hdr->contents != NULL) |
| 1414 | { |
| 1415 | intsyms = (Elf_Internal_Sym *) symtab_hdr->contents; |
| 1416 | } |
| 1417 | else |
| 1418 | { |
| 1419 | intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL); |
| 1420 | symtab_hdr->contents = (bfd_byte *) intsyms; |
| 1421 | } |
| 1422 | |
| 1423 | if (shndx_hdr->sh_size != 0) |
| 1424 | { |
| 1425 | bfd_size_type amt; |
| 1426 | |
| 1427 | amt = symtab_hdr->sh_info; |
| 1428 | amt *= sizeof (Elf_External_Sym_Shndx); |
| 1429 | shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt); |
| 1430 | if (shndx_buf == NULL) |
| 1431 | goto error_return; |
| 1432 | if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 |
| 1433 | || bfd_bread (shndx_buf, amt, abfd) != amt) |
| 1434 | goto error_return; |
| 1435 | shndx_hdr->contents = (bfd_byte *) shndx_buf; |
| 1436 | } |
| 1437 | |
| 1438 | /* Get a copy of the native relocations. */ |
| 1439 | internal_relocs = (_bfd_elf_link_read_relocs |
| 1440 | (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, |
| 1441 | link_info->keep_memory)); |
| 1442 | if (internal_relocs == NULL) |
| 1443 | goto error_return; |
| 1444 | if (! link_info->keep_memory) |
| 1445 | free_relocs = internal_relocs; |
| 1446 | |
| 1447 | /* The RL_ relocs must be just before the operand relocs they go |
| 1448 | with, so we must sort them to guarantee this. */ |
| 1449 | qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela), |
| 1450 | compare_reloc); |
| 1451 | |
| 1452 | /* Walk through them looking for relaxing opportunities. */ |
| 1453 | irelend = internal_relocs + sec->reloc_count; |
| 1454 | |
| 1455 | for (irel = internal_relocs; irel < irelend; irel++) |
| 1456 | { |
| 1457 | bfd_vma symval; |
| 1458 | unsigned char *insn, *gap, *einsn; |
| 1459 | bfd_vma pc; |
| 1460 | bfd_signed_vma pcrel; |
| 1461 | int relax_relocs; |
| 1462 | int gap_size; |
| 1463 | int new_type; |
| 1464 | int posn; |
| 1465 | int enc; |
| 1466 | EncodingTable *enctbl; |
| 1467 | EncodingTable *e; |
| 1468 | |
| 1469 | if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP |
| 1470 | && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR |
| 1471 | && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR) |
| 1472 | continue; |
| 1473 | |
| 1474 | srel = irel; |
| 1475 | |
| 1476 | /* There will always be room for the relaxed insn, since it is smaller |
| 1477 | than the one it would replace. */ |
| 1478 | BFD_ASSERT (irel->r_offset < sec->size); |
| 1479 | |
| 1480 | insn = contents + irel->r_offset; |
| 1481 | relax_relocs = irel->r_addend % 16; |
| 1482 | |
| 1483 | /* Ok, we only have three relocs we care about, and they're all |
| 1484 | fake. The lower four bits of the addend is always the number |
| 1485 | of following relocs (hence the qsort above) that are assigned |
| 1486 | to this opcode. The next 8 bits of the addend indicates the |
| 1487 | number of bytes in the insn. We use the rest of them |
| 1488 | ourselves as flags for the more expensive operations (defines |
| 1489 | above). The three relocs are: |
| 1490 | |
| 1491 | RL_JUMP: This marks all direct jump insns. We check the |
| 1492 | displacement and replace them with shorter jumps if |
| 1493 | they're in range. We also use this to find JMP.S |
| 1494 | insns and manually shorten them when we delete bytes. |
| 1495 | We have to decode these insns to figure out what to |
| 1496 | do. |
| 1497 | |
| 1498 | RL_1ADDR: This is a :G or :Q insn, which has a single |
| 1499 | "standard" operand. We have to extract the type |
| 1500 | field, see if it's a wide displacement, then figure |
| 1501 | out if we can replace it with a narrow displacement. |
| 1502 | We don't have to decode these insns. |
| 1503 | |
| 1504 | RL_2ADDR: Similarly, but two "standard" operands. Note that |
| 1505 | r_addend may still be 1, as standard operands don't |
| 1506 | always have displacements. Gas shouldn't give us one |
| 1507 | with zero operands, but since we don't know which one |
| 1508 | has the displacement, we check them both anyway. |
| 1509 | |
| 1510 | These all point to the beginning of the insn itself, not the |
| 1511 | operands. |
| 1512 | |
| 1513 | Note that we only relax one step at a time, relying on the |
| 1514 | linker to call us repeatedly. Thus, there is no code for |
| 1515 | JMP.A->JMP.B although that will happen in two steps. |
| 1516 | Likewise, for 2ADDR relaxes, we do one operand per cycle. |
| 1517 | */ |
| 1518 | |
| 1519 | /* Get the value of the symbol referred to by the reloc. Just |
| 1520 | in case this is the last reloc in the list, use the RL's |
| 1521 | addend to choose between this reloc (no addend) or the next |
| 1522 | (yes addend, which means at least one following reloc). */ |
| 1523 | srel = irel + (relax_relocs ? 1 : 0); |
| 1524 | symval = OFFSET_FOR_RELOC (srel); |
| 1525 | |
| 1526 | /* Setting gap_size nonzero is the flag which means "something |
| 1527 | shrunk". */ |
| 1528 | gap_size = 0; |
| 1529 | gap = NULL; |
| 1530 | new_type = ELF32_R_TYPE(srel->r_info); |
| 1531 | |
| 1532 | pc = sec->output_section->vma + sec->output_offset |
| 1533 | + srel->r_offset; |
| 1534 | pcrel = symval - pc + srel->r_addend; |
| 1535 | |
| 1536 | if (machine == bfd_mach_m16c) |
| 1537 | { |
| 1538 | /* R8C / M16C */ |
| 1539 | |
| 1540 | switch (ELF32_R_TYPE(irel->r_info)) |
| 1541 | { |
| 1542 | |
| 1543 | case R_M32C_RL_JUMP: |
| 1544 | switch (insn[0]) |
| 1545 | { |
| 1546 | case 0xfe: /* jmp.b */ |
| 1547 | if (pcrel >= 2 && pcrel <= 9) |
| 1548 | { |
| 1549 | /* Relax JMP.B -> JMP.S. We need to get rid of |
| 1550 | the following reloc though. */ |
| 1551 | insn[0] = 0x60 | (pcrel - 2); |
| 1552 | new_type = R_M32C_NONE; |
| 1553 | irel->r_addend = 0x10; |
| 1554 | gap_size = 1; |
| 1555 | gap = insn + 1; |
| 1556 | } |
| 1557 | break; |
| 1558 | |
| 1559 | case 0xf4: /* jmp.w */ |
| 1560 | /* 128 is allowed because it will be one byte closer |
| 1561 | after relaxing. Likewise for all other pc-rel |
| 1562 | jumps. */ |
| 1563 | if (pcrel <= 128 && pcrel >= -128) |
| 1564 | { |
| 1565 | /* Relax JMP.W -> JMP.B */ |
| 1566 | insn[0] = 0xfe; |
| 1567 | insn[1] = 0; |
| 1568 | new_type = R_M32C_8_PCREL; |
| 1569 | gap_size = 1; |
| 1570 | gap = insn + 2; |
| 1571 | } |
| 1572 | break; |
| 1573 | |
| 1574 | case 0xfc: /* jmp.a */ |
| 1575 | if (pcrel <= 32768 && pcrel >= -32768) |
| 1576 | { |
| 1577 | /* Relax JMP.A -> JMP.W */ |
| 1578 | insn[0] = 0xf4; |
| 1579 | insn[1] = 0; |
| 1580 | insn[2] = 0; |
| 1581 | new_type = R_M32C_16_PCREL; |
| 1582 | gap_size = 1; |
| 1583 | gap = insn + 3; |
| 1584 | } |
| 1585 | break; |
| 1586 | |
| 1587 | case 0xfd: /* jsr.a */ |
| 1588 | if (pcrel <= 32768 && pcrel >= -32768) |
| 1589 | { |
| 1590 | /* Relax JSR.A -> JSR.W */ |
| 1591 | insn[0] = 0xf5; |
| 1592 | insn[1] = 0; |
| 1593 | insn[2] = 0; |
| 1594 | new_type = R_M32C_16_PCREL; |
| 1595 | gap_size = 1; |
| 1596 | gap = insn + 3; |
| 1597 | } |
| 1598 | break; |
| 1599 | } |
| 1600 | break; |
| 1601 | |
| 1602 | case R_M32C_RL_2ADDR: |
| 1603 | /* xxxx xxxx srce dest [src-disp] [dest-disp]*/ |
| 1604 | |
| 1605 | enctbl = m16c_addr_encodings; |
| 1606 | posn = 2; |
| 1607 | enc = (insn[1] >> 4) & 0x0f; |
| 1608 | e = & enctbl[enc]; |
| 1609 | |
| 1610 | if (srel->r_offset == irel->r_offset + posn |
| 1611 | && e->new_encoding != enc |
| 1612 | && symval <= e->max_disp) |
| 1613 | { |
| 1614 | insn[1] &= 0x0f; |
| 1615 | insn[1] |= e->new_encoding << 4; |
| 1616 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; |
| 1617 | gap = insn + posn + enctbl[e->new_encoding].bytes; |
| 1618 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; |
| 1619 | break; |
| 1620 | } |
| 1621 | if (relax_relocs == 2) |
| 1622 | srel ++; |
| 1623 | posn += e->bytes; |
| 1624 | |
| 1625 | goto try_1addr_16; |
| 1626 | |
| 1627 | case R_M32C_RL_1ADDR: |
| 1628 | /* xxxx xxxx xxxx dest [disp] */ |
| 1629 | |
| 1630 | enctbl = m16c_addr_encodings; |
| 1631 | posn = 2; |
| 1632 | |
| 1633 | /* Check the opcode for jumps. We know it's safe to |
| 1634 | do this because all 2ADDR insns are at least two |
| 1635 | bytes long. */ |
| 1636 | enc = insn[0] * 256 + insn[1]; |
| 1637 | enc &= 0xfff0; |
| 1638 | if (enc == 0x7d20 |
| 1639 | || enc == 0x7d00 |
| 1640 | || enc == 0x7d30 |
| 1641 | || enc == 0x7d10) |
| 1642 | { |
| 1643 | enctbl = m16c_jmpaddr_encodings; |
| 1644 | } |
| 1645 | |
| 1646 | try_1addr_16: |
| 1647 | /* srel, posn, and enc must be set here. */ |
| 1648 | |
| 1649 | symval = OFFSET_FOR_RELOC (srel); |
| 1650 | enc = insn[1] & 0x0f; |
| 1651 | e = & enctbl[enc]; |
| 1652 | |
| 1653 | if (srel->r_offset == irel->r_offset + posn |
| 1654 | && e->new_encoding != enc |
| 1655 | && symval <= e->max_disp) |
| 1656 | { |
| 1657 | insn[1] &= 0xf0; |
| 1658 | insn[1] |= e->new_encoding; |
| 1659 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; |
| 1660 | gap = insn + posn + enctbl[e->new_encoding].bytes; |
| 1661 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; |
| 1662 | break; |
| 1663 | } |
| 1664 | |
| 1665 | break; |
| 1666 | |
| 1667 | } /* Ends switch (reloc type) for m16c. */ |
| 1668 | } |
| 1669 | else /* machine == bfd_mach_m32c */ |
| 1670 | { |
| 1671 | /* M32CM / M32C */ |
| 1672 | |
| 1673 | switch (ELF32_R_TYPE(irel->r_info)) |
| 1674 | { |
| 1675 | |
| 1676 | case R_M32C_RL_JUMP: |
| 1677 | switch (insn[0]) |
| 1678 | { |
| 1679 | case 0xbb: /* jmp.b */ |
| 1680 | if (pcrel >= 2 && pcrel <= 9) |
| 1681 | { |
| 1682 | int p = pcrel - 2; |
| 1683 | /* Relax JMP.B -> JMP.S. We need to get rid of |
| 1684 | the following reloc though. */ |
| 1685 | insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1); |
| 1686 | new_type = R_M32C_NONE; |
| 1687 | irel->r_addend = 0x10; |
| 1688 | gap_size = 1; |
| 1689 | gap = insn + 1; |
| 1690 | } |
| 1691 | break; |
| 1692 | |
| 1693 | case 0xce: /* jmp.w */ |
| 1694 | if (pcrel <= 128 && pcrel >= -128) |
| 1695 | { |
| 1696 | /* Relax JMP.W -> JMP.B */ |
| 1697 | insn[0] = 0xbb; |
| 1698 | insn[1] = 0; |
| 1699 | new_type = R_M32C_8_PCREL; |
| 1700 | gap_size = 1; |
| 1701 | gap = insn + 2; |
| 1702 | } |
| 1703 | break; |
| 1704 | |
| 1705 | case 0xcc: /* jmp.a */ |
| 1706 | if (pcrel <= 32768 && pcrel >= -32768) |
| 1707 | { |
| 1708 | /* Relax JMP.A -> JMP.W */ |
| 1709 | insn[0] = 0xce; |
| 1710 | insn[1] = 0; |
| 1711 | insn[2] = 0; |
| 1712 | new_type = R_M32C_16_PCREL; |
| 1713 | gap_size = 1; |
| 1714 | gap = insn + 3; |
| 1715 | } |
| 1716 | break; |
| 1717 | |
| 1718 | case 0xcd: /* jsr.a */ |
| 1719 | if (pcrel <= 32768 && pcrel >= -32768) |
| 1720 | { |
| 1721 | /* Relax JSR.A -> JSR.W */ |
| 1722 | insn[0] = 0xcf; |
| 1723 | insn[1] = 0; |
| 1724 | insn[2] = 0; |
| 1725 | new_type = R_M32C_16_PCREL; |
| 1726 | gap_size = 1; |
| 1727 | gap = insn + 3; |
| 1728 | } |
| 1729 | break; |
| 1730 | } |
| 1731 | break; |
| 1732 | |
| 1733 | case R_M32C_RL_2ADDR: |
| 1734 | /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/ |
| 1735 | |
| 1736 | einsn = insn; |
| 1737 | posn = 2; |
| 1738 | if (einsn[0] == 1) |
| 1739 | { |
| 1740 | /* prefix; remove it as far as the RL reloc is concerned. */ |
| 1741 | einsn ++; |
| 1742 | posn ++; |
| 1743 | } |
| 1744 | |
| 1745 | enctbl = m32c_addr_encodings; |
| 1746 | enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4); |
| 1747 | e = & enctbl[enc]; |
| 1748 | |
| 1749 | if (srel->r_offset == irel->r_offset + posn |
| 1750 | && e->new_encoding != enc |
| 1751 | && symval <= e->max_disp) |
| 1752 | { |
| 1753 | einsn[0] &= 0x8f; |
| 1754 | einsn[0] |= (e->new_encoding & 0x1c) << 2; |
| 1755 | einsn[1] &= 0xcf; |
| 1756 | einsn[1] |= (e->new_encoding & 0x03) << 4; |
| 1757 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; |
| 1758 | gap = insn + posn + enctbl[e->new_encoding].bytes; |
| 1759 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; |
| 1760 | break; |
| 1761 | } |
| 1762 | if (relax_relocs == 2) |
| 1763 | srel ++; |
| 1764 | posn += e->bytes; |
| 1765 | |
| 1766 | goto try_1addr_32; |
| 1767 | |
| 1768 | case R_M32C_RL_1ADDR: |
| 1769 | /* xxxx DDDx DDxx xxxx [disp] */ |
| 1770 | |
| 1771 | einsn = insn; |
| 1772 | posn = 2; |
| 1773 | if (einsn[0] == 1) |
| 1774 | { |
| 1775 | /* prefix; remove it as far as the RL reloc is concerned. */ |
| 1776 | einsn ++; |
| 1777 | posn ++; |
| 1778 | } |
| 1779 | |
| 1780 | enctbl = m32c_addr_encodings; |
| 1781 | |
| 1782 | try_1addr_32: |
| 1783 | /* srel, posn, and enc must be set here. */ |
| 1784 | |
| 1785 | symval = OFFSET_FOR_RELOC (srel); |
| 1786 | enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6); |
| 1787 | e = & enctbl[enc]; |
| 1788 | |
| 1789 | if (srel->r_offset == irel->r_offset + posn |
| 1790 | && e->new_encoding != enc |
| 1791 | && symval <= e->max_disp) |
| 1792 | { |
| 1793 | einsn[0] &= 0xf1; |
| 1794 | einsn[0] |= (e->new_encoding & 0x1c) >> 1; |
| 1795 | einsn[1] &= 0x3f; |
| 1796 | einsn[1] |= (e->new_encoding & 0x03) << 6; |
| 1797 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; |
| 1798 | gap = insn + posn + enctbl[e->new_encoding].bytes; |
| 1799 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; |
| 1800 | break; |
| 1801 | } |
| 1802 | |
| 1803 | break; |
| 1804 | |
| 1805 | } /* Ends switch (reloc type) for m32c. */ |
| 1806 | } |
| 1807 | |
| 1808 | if (gap_size == 0) |
| 1809 | continue; |
| 1810 | |
| 1811 | *again = TRUE; |
| 1812 | |
| 1813 | srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type); |
| 1814 | |
| 1815 | /* Note that we've changed the relocs, section contents, etc. */ |
| 1816 | elf_section_data (sec)->relocs = internal_relocs; |
| 1817 | free_relocs = NULL; |
| 1818 | |
| 1819 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1820 | free_contents = NULL; |
| 1821 | |
| 1822 | symtab_hdr->contents = (bfd_byte *) intsyms; |
| 1823 | free_intsyms = NULL; |
| 1824 | |
| 1825 | bytes_saved += gap_size; |
| 1826 | |
| 1827 | if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size)) |
| 1828 | goto error_return; |
| 1829 | |
| 1830 | } /* next relocation */ |
| 1831 | |
| 1832 | if (free_relocs != NULL) |
| 1833 | { |
| 1834 | free (free_relocs); |
| 1835 | free_relocs = NULL; |
| 1836 | } |
| 1837 | |
| 1838 | if (free_contents != NULL) |
| 1839 | { |
| 1840 | if (! link_info->keep_memory) |
| 1841 | free (free_contents); |
| 1842 | /* Cache the section contents for elf_link_input_bfd. */ |
| 1843 | else |
| 1844 | elf_section_data (sec)->this_hdr.contents = contents; |
| 1845 | |
| 1846 | free_contents = NULL; |
| 1847 | } |
| 1848 | |
| 1849 | if (shndx_buf != NULL) |
| 1850 | { |
| 1851 | shndx_hdr->contents = NULL; |
| 1852 | free (shndx_buf); |
| 1853 | } |
| 1854 | |
| 1855 | if (free_intsyms != NULL) |
| 1856 | { |
| 1857 | if (! link_info->keep_memory) |
| 1858 | free (free_intsyms); |
| 1859 | /* Cache the symbols for elf_link_input_bfd. */ |
| 1860 | else |
| 1861 | { |
| 1862 | symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/; |
| 1863 | } |
| 1864 | |
| 1865 | free_intsyms = NULL; |
| 1866 | } |
| 1867 | |
| 1868 | return TRUE; |
| 1869 | |
| 1870 | error_return: |
| 1871 | if (free_relocs != NULL) |
| 1872 | free (free_relocs); |
| 1873 | if (free_contents != NULL) |
| 1874 | free (free_contents); |
| 1875 | if (shndx_buf != NULL) |
| 1876 | { |
| 1877 | shndx_hdr->contents = NULL; |
| 1878 | free (shndx_buf); |
| 1879 | } |
| 1880 | if (free_intsyms != NULL) |
| 1881 | free (free_intsyms); |
| 1882 | return FALSE; |
| 1883 | } |
| 1884 | |
| 1885 | /* Delete some bytes from a section while relaxing. */ |
| 1886 | |
| 1887 | static bfd_boolean |
| 1888 | m32c_elf_relax_delete_bytes |
| 1889 | (bfd * abfd, |
| 1890 | asection * sec, |
| 1891 | bfd_vma addr, |
| 1892 | int count) |
| 1893 | { |
| 1894 | Elf_Internal_Shdr *symtab_hdr; |
| 1895 | Elf_Internal_Shdr *shndx_hdr; |
| 1896 | int sec_shndx; |
| 1897 | bfd_byte *contents; |
| 1898 | Elf_Internal_Rela *irel; |
| 1899 | Elf_Internal_Rela *irelend; |
| 1900 | bfd_vma toaddr; |
| 1901 | Elf_Internal_Sym *isym; |
| 1902 | Elf_Internal_Sym *isymend; |
| 1903 | Elf_Internal_Sym *intsyms; |
| 1904 | Elf_External_Sym_Shndx *shndx_buf; |
| 1905 | Elf_External_Sym_Shndx *shndx; |
| 1906 | struct elf_link_hash_entry ** sym_hashes; |
| 1907 | struct elf_link_hash_entry ** end_hashes; |
| 1908 | unsigned int symcount; |
| 1909 | |
| 1910 | contents = elf_section_data (sec)->this_hdr.contents; |
| 1911 | |
| 1912 | toaddr = sec->size; |
| 1913 | |
| 1914 | irel = elf_section_data (sec)->relocs; |
| 1915 | irelend = irel + sec->reloc_count; |
| 1916 | |
| 1917 | /* Actually delete the bytes. */ |
| 1918 | memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count)); |
| 1919 | sec->size -= count; |
| 1920 | |
| 1921 | /* Adjust all the relocs. */ |
| 1922 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++) |
| 1923 | { |
| 1924 | /* Get the new reloc address. */ |
| 1925 | if (irel->r_offset > addr && irel->r_offset < toaddr) |
| 1926 | irel->r_offset -= count; |
| 1927 | |
| 1928 | if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP |
| 1929 | && irel->r_addend == 0x10 /* one byte insn, no relocs */ |
| 1930 | && irel->r_offset + 1 < addr |
| 1931 | && irel->r_offset + 7 > addr) |
| 1932 | { |
| 1933 | bfd_vma disp; |
| 1934 | unsigned char *insn = &contents[irel->r_offset]; |
| 1935 | disp = *insn; |
| 1936 | /* This is a JMP.S, which we have to manually update. */ |
| 1937 | if (elf32_m32c_machine (abfd) == bfd_mach_m16c) |
| 1938 | { |
| 1939 | if ((*insn & 0xf8) != 0x60) |
| 1940 | continue; |
| 1941 | disp = (disp & 7); |
| 1942 | } |
| 1943 | else |
| 1944 | { |
| 1945 | if ((*insn & 0xce) != 0x4a) |
| 1946 | continue; |
| 1947 | disp = ((disp & 0x30) >> 3) | (disp & 1); |
| 1948 | } |
| 1949 | if (irel->r_offset + disp + 2 >= addr+count) |
| 1950 | { |
| 1951 | disp -= count; |
| 1952 | if (elf32_m32c_machine (abfd) == bfd_mach_m16c) |
| 1953 | { |
| 1954 | *insn = (*insn & 0xf8) | disp; |
| 1955 | } |
| 1956 | else |
| 1957 | { |
| 1958 | *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1); |
| 1959 | } |
| 1960 | } |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | /* Adjust the local symbols defined in this section. */ |
| 1965 | symtab_hdr = & elf_tdata (abfd)->symtab_hdr; |
| 1966 | intsyms = (Elf_Internal_Sym *) symtab_hdr->contents; |
| 1967 | isym = intsyms; |
| 1968 | isymend = isym + symtab_hdr->sh_info; |
| 1969 | |
| 1970 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); |
| 1971 | shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr; |
| 1972 | shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents; |
| 1973 | shndx = shndx_buf; |
| 1974 | |
| 1975 | for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL)) |
| 1976 | { |
| 1977 | /* If the symbol is in the range of memory we just moved, we |
| 1978 | have to adjust its value. */ |
| 1979 | if ((int) isym->st_shndx == sec_shndx |
| 1980 | && isym->st_value > addr |
| 1981 | && isym->st_value < toaddr) |
| 1982 | { |
| 1983 | isym->st_value -= count; |
| 1984 | } |
| 1985 | /* If the symbol *spans* the bytes we just deleted (i.e. it's |
| 1986 | *end* is in the moved bytes but it's *start* isn't), then we |
| 1987 | must adjust its size. */ |
| 1988 | if ((int) isym->st_shndx == sec_shndx |
| 1989 | && isym->st_value < addr |
| 1990 | && isym->st_value + isym->st_size > addr |
| 1991 | && isym->st_value + isym->st_size < toaddr) |
| 1992 | { |
| 1993 | isym->st_size -= count; |
| 1994 | } |
| 1995 | } |
| 1996 | |
| 1997 | /* Now adjust the global symbols defined in this section. */ |
| 1998 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) |
| 1999 | - symtab_hdr->sh_info); |
| 2000 | sym_hashes = elf_sym_hashes (abfd); |
| 2001 | // sym_hashes += symtab_hdr->sh_info; |
| 2002 | end_hashes = sym_hashes + symcount; |
| 2003 | |
| 2004 | for (; sym_hashes < end_hashes; sym_hashes ++) |
| 2005 | { |
| 2006 | struct elf_link_hash_entry * sym_hash = * sym_hashes; |
| 2007 | |
| 2008 | if (sym_hash && |
| 2009 | (sym_hash->root.type == bfd_link_hash_defined |
| 2010 | || sym_hash->root.type == bfd_link_hash_defweak) |
| 2011 | && sym_hash->root.u.def.section == sec) |
| 2012 | { |
| 2013 | if (sym_hash->root.u.def.value > addr |
| 2014 | && sym_hash->root.u.def.value < toaddr) |
| 2015 | { |
| 2016 | sym_hash->root.u.def.value -= count; |
| 2017 | } |
| 2018 | if (sym_hash->root.u.def.value < addr |
| 2019 | && sym_hash->root.u.def.value + sym_hash->size > addr |
| 2020 | && sym_hash->root.u.def.value + sym_hash->size < toaddr) |
| 2021 | { |
| 2022 | sym_hash->size -= count; |
| 2023 | } |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | return TRUE; |
| 2028 | } |
| 2029 | \f |
| 2030 | /* This is for versions of gcc prior to 4.3. */ |
| 2031 | static unsigned int |
| 2032 | _bfd_m32c_elf_eh_frame_address_size (bfd *abfd, asection *sec ATTRIBUTE_UNUSED) |
| 2033 | { |
| 2034 | if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C) |
| 2035 | return 2; |
| 2036 | return 4; |
| 2037 | } |
| 2038 | |
| 2039 | \f |
| 2040 | |
| 2041 | #define ELF_ARCH bfd_arch_m32c |
| 2042 | #define ELF_MACHINE_CODE EM_M32C |
| 2043 | #define ELF_MACHINE_ALT1 EM_M32C_OLD |
| 2044 | #define ELF_MAXPAGESIZE 0x100 |
| 2045 | |
| 2046 | #if 0 |
| 2047 | #define TARGET_BIG_SYM bfd_elf32_m32c_vec |
| 2048 | #define TARGET_BIG_NAME "elf32-m32c" |
| 2049 | #else |
| 2050 | #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec |
| 2051 | #define TARGET_LITTLE_NAME "elf32-m32c" |
| 2052 | #endif |
| 2053 | |
| 2054 | #define elf_info_to_howto_rel NULL |
| 2055 | #define elf_info_to_howto m32c_info_to_howto_rela |
| 2056 | #define elf_backend_object_p m32c_elf_object_p |
| 2057 | #define elf_backend_relocate_section m32c_elf_relocate_section |
| 2058 | #define elf_backend_check_relocs m32c_elf_check_relocs |
| 2059 | #define elf_backend_object_p m32c_elf_object_p |
| 2060 | #define elf_symbol_leading_char ('_') |
| 2061 | #define elf_backend_always_size_sections \ |
| 2062 | m32c_elf_always_size_sections |
| 2063 | #define elf_backend_finish_dynamic_sections \ |
| 2064 | m32c_elf_finish_dynamic_sections |
| 2065 | |
| 2066 | #define elf_backend_can_gc_sections 1 |
| 2067 | #define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size |
| 2068 | |
| 2069 | #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup |
| 2070 | #define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup |
| 2071 | #define bfd_elf32_bfd_relax_section m32c_elf_relax_section |
| 2072 | #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags |
| 2073 | #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data |
| 2074 | #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data |
| 2075 | |
| 2076 | #include "elf32-target.h" |