1/* Motorola 68HC12-specific support for 32-bit ELF 2 Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2006 3 Free Software Foundation, Inc. 4 Contributed by Stephane Carrez (stcarrez@nerim.fr) 5 (Heavily copied from the D10V port by Martin Hunt (hunt@cygnus.com)) 6 7This file is part of BFD, the Binary File Descriptor library. 8 9This program is free software; you can redistribute it and/or modify 10it under the terms of the GNU General Public License as published by 11the Free Software Foundation; either version 2 of the License, or 12(at your option) any later version. 13 14This program is distributed in the hope that it will be useful, 15but WITHOUT ANY WARRANTY; without even the implied warranty of 16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17GNU General Public License for more details. 18 19You should have received a copy of the GNU General Public License 20along with this program; if not, write to the Free Software 21Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 22 23#include "bfd.h" 24#include "sysdep.h" 25#include "bfdlink.h" 26#include "libbfd.h" 27#include "elf-bfd.h" 28#include "elf32-m68hc1x.h" 29#include "elf/m68hc11.h" 30#include "opcode/m68hc11.h" 31 32/* Relocation functions. */ 33static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup 34 (bfd *, bfd_reloc_code_real_type); 35static void m68hc11_info_to_howto_rel 36 (bfd *, arelent *, Elf_Internal_Rela *); 37 38/* Trampoline generation. */ 39static bfd_boolean m68hc12_elf_size_one_stub 40 (struct bfd_hash_entry *gen_entry, void *in_arg); 41static bfd_boolean m68hc12_elf_build_one_stub 42 (struct bfd_hash_entry *gen_entry, void *in_arg); 43static struct bfd_link_hash_table* m68hc12_elf_bfd_link_hash_table_create 44 (bfd*); 45 46static bfd_boolean m68hc12_elf_set_mach_from_flags PARAMS ((bfd *)); 47 48/* Use REL instead of RELA to save space */ 49#define USE_REL 1 50 51/* The 68HC12 microcontroler has a memory bank switching system 52 with a 16Kb window in the 64Kb address space. The extended memory 53 is mapped in the 16Kb window (at 0x8000). The page register controls 54 which 16Kb bank is mapped. The call/rtc instructions take care of 55 bank switching in function calls/returns. 56 57 For GNU Binutils to work, we consider there is a physical memory 58 at 0..0x0ffff and a kind of virtual memory above that. Symbols 59 in virtual memory have their addresses treated in a special way 60 when disassembling and when linking. 61 62 For the linker to work properly, we must always relocate the virtual 63 memory as if it is mapped at 0x8000. When a 16-bit relocation is 64 made in the virtual memory, we check that it does not cross the 65 memory bank where it is used. This would involve a page change 66 which would be wrong. The 24-bit relocation is for that and it 67 treats the address as a physical address + page number. 68 69 70 Banked 71 Address Space 72 | | Page n 73 +---------------+ 0x1010000 74 | | 75 | jsr _foo | 76 | .. | Page 3 77 | _foo: | 78 +---------------+ 0x100C000 79 | | 80 | call _bar | 81 | .. | Page 2 82 | _bar: | 83 +---------------+ 0x1008000 84 /------>| | 85 | | call _foo | Page 1 86 | | | 87 | +---------------+ 0x1004000 88 Physical | | | 89 Address Space | | | Page 0 90 | | | 91 +-----------+ 0x00FFFF | +---------------+ 0x1000000 92 | | | 93 | call _foo | | 94 | | | 95 +-----------+ 0x00BFFF -+---/ 96 | | | 97 | | | 98 | | 16K | 99 | | | 100 +-----------+ 0x008000 -+ 101 | | 102 | | 103 = = 104 | | 105 | | 106 +-----------+ 0000 107 108 109 The 'call _foo' must be relocated with page 3 and 16-bit address 110 mapped at 0x8000. 111 112 The 3-bit and 16-bit PC rel relocation is only used by 68HC12. */ 113static reloc_howto_type elf_m68hc11_howto_table[] = { 114 /* This reloc does nothing. */ 115 HOWTO (R_M68HC11_NONE, /* type */ 116 0, /* rightshift */ 117 2, /* size (0 = byte, 1 = short, 2 = long) */ 118 32, /* bitsize */ 119 FALSE, /* pc_relative */ 120 0, /* bitpos */ 121 complain_overflow_dont,/* complain_on_overflow */ 122 bfd_elf_generic_reloc, /* special_function */ 123 "R_M68HC12_NONE", /* name */ 124 FALSE, /* partial_inplace */ 125 0, /* src_mask */ 126 0, /* dst_mask */ 127 FALSE), /* pcrel_offset */ 128 129 /* A 8 bit absolute relocation */ 130 HOWTO (R_M68HC11_8, /* type */ 131 0, /* rightshift */ 132 0, /* size (0 = byte, 1 = short, 2 = long) */ 133 8, /* bitsize */ 134 FALSE, /* pc_relative */ 135 0, /* bitpos */ 136 complain_overflow_bitfield, /* complain_on_overflow */ 137 bfd_elf_generic_reloc, /* special_function */ 138 "R_M68HC12_8", /* name */ 139 FALSE, /* partial_inplace */ 140 0x00ff, /* src_mask */ 141 0x00ff, /* dst_mask */ 142 FALSE), /* pcrel_offset */ 143 144 /* A 8 bit absolute relocation (upper address) */ 145 HOWTO (R_M68HC11_HI8, /* type */ 146 8, /* rightshift */ 147 0, /* size (0 = byte, 1 = short, 2 = long) */ 148 8, /* bitsize */ 149 FALSE, /* pc_relative */ 150 0, /* bitpos */ 151 complain_overflow_bitfield, /* complain_on_overflow */ 152 bfd_elf_generic_reloc, /* special_function */ 153 "R_M68HC12_HI8", /* name */ 154 FALSE, /* partial_inplace */ 155 0x00ff, /* src_mask */ 156 0x00ff, /* dst_mask */ 157 FALSE), /* pcrel_offset */ 158 159 /* A 8 bit absolute relocation (upper address) */ 160 HOWTO (R_M68HC11_LO8, /* type */ 161 0, /* rightshift */ 162 0, /* size (0 = byte, 1 = short, 2 = long) */ 163 8, /* bitsize */ 164 FALSE, /* pc_relative */ 165 0, /* bitpos */ 166 complain_overflow_dont, /* complain_on_overflow */ 167 bfd_elf_generic_reloc, /* special_function */ 168 "R_M68HC12_LO8", /* name */ 169 FALSE, /* partial_inplace */ 170 0x00ff, /* src_mask */ 171 0x00ff, /* dst_mask */ 172 FALSE), /* pcrel_offset */ 173 174 /* A 8 bit PC-rel relocation */ 175 HOWTO (R_M68HC11_PCREL_8, /* type */ 176 0, /* rightshift */ 177 0, /* size (0 = byte, 1 = short, 2 = long) */ 178 8, /* bitsize */ 179 TRUE, /* pc_relative */ 180 0, /* bitpos */ 181 complain_overflow_bitfield, /* complain_on_overflow */ 182 bfd_elf_generic_reloc, /* special_function */ 183 "R_M68HC12_PCREL_8", /* name */ 184 FALSE, /* partial_inplace */ 185 0x00ff, /* src_mask */ 186 0x00ff, /* dst_mask */ 187 TRUE), /* pcrel_offset */ 188 189 /* A 16 bit absolute relocation */ 190 HOWTO (R_M68HC11_16, /* type */ 191 0, /* rightshift */ 192 1, /* size (0 = byte, 1 = short, 2 = long) */ 193 16, /* bitsize */ 194 FALSE, /* pc_relative */ 195 0, /* bitpos */ 196 complain_overflow_dont /*bitfield */ , /* complain_on_overflow */ 197 bfd_elf_generic_reloc, /* special_function */ 198 "R_M68HC12_16", /* name */ 199 FALSE, /* partial_inplace */ 200 0xffff, /* src_mask */ 201 0xffff, /* dst_mask */ 202 FALSE), /* pcrel_offset */ 203 204 /* A 32 bit absolute relocation. This one is never used for the 205 code relocation. It's used by gas for -gstabs generation. */ 206 HOWTO (R_M68HC11_32, /* type */ 207 0, /* rightshift */ 208 2, /* size (0 = byte, 1 = short, 2 = long) */ 209 32, /* bitsize */ 210 FALSE, /* pc_relative */ 211 0, /* bitpos */ 212 complain_overflow_bitfield, /* complain_on_overflow */ 213 bfd_elf_generic_reloc, /* special_function */ 214 "R_M68HC12_32", /* name */ 215 FALSE, /* partial_inplace */ 216 0xffffffff, /* src_mask */ 217 0xffffffff, /* dst_mask */ 218 FALSE), /* pcrel_offset */ 219 220 /* A 3 bit absolute relocation */ 221 HOWTO (R_M68HC11_3B, /* type */ 222 0, /* rightshift */ 223 0, /* size (0 = byte, 1 = short, 2 = long) */ 224 3, /* bitsize */ 225 FALSE, /* pc_relative */ 226 0, /* bitpos */ 227 complain_overflow_bitfield, /* complain_on_overflow */ 228 bfd_elf_generic_reloc, /* special_function */ 229 "R_M68HC12_4B", /* name */ 230 FALSE, /* partial_inplace */ 231 0x003, /* src_mask */ 232 0x003, /* dst_mask */ 233 FALSE), /* pcrel_offset */ 234 235 /* A 16 bit PC-rel relocation */ 236 HOWTO (R_M68HC11_PCREL_16, /* type */ 237 0, /* rightshift */ 238 1, /* size (0 = byte, 1 = short, 2 = long) */ 239 16, /* bitsize */ 240 TRUE, /* pc_relative */ 241 0, /* bitpos */ 242 complain_overflow_dont, /* complain_on_overflow */ 243 bfd_elf_generic_reloc, /* special_function */ 244 "R_M68HC12_PCREL_16", /* name */ 245 FALSE, /* partial_inplace */ 246 0xffff, /* src_mask */ 247 0xffff, /* dst_mask */ 248 TRUE), /* pcrel_offset */ 249 250 /* GNU extension to record C++ vtable hierarchy */ 251 HOWTO (R_M68HC11_GNU_VTINHERIT, /* type */ 252 0, /* rightshift */ 253 1, /* size (0 = byte, 1 = short, 2 = long) */ 254 0, /* bitsize */ 255 FALSE, /* pc_relative */ 256 0, /* bitpos */ 257 complain_overflow_dont, /* complain_on_overflow */ 258 NULL, /* special_function */ 259 "R_M68HC11_GNU_VTINHERIT", /* name */ 260 FALSE, /* partial_inplace */ 261 0, /* src_mask */ 262 0, /* dst_mask */ 263 FALSE), /* pcrel_offset */ 264 265 /* GNU extension to record C++ vtable member usage */ 266 HOWTO (R_M68HC11_GNU_VTENTRY, /* type */ 267 0, /* rightshift */ 268 1, /* size (0 = byte, 1 = short, 2 = long) */ 269 0, /* bitsize */ 270 FALSE, /* pc_relative */ 271 0, /* bitpos */ 272 complain_overflow_dont, /* complain_on_overflow */ 273 _bfd_elf_rel_vtable_reloc_fn, /* special_function */ 274 "R_M68HC11_GNU_VTENTRY", /* name */ 275 FALSE, /* partial_inplace */ 276 0, /* src_mask */ 277 0, /* dst_mask */ 278 FALSE), /* pcrel_offset */ 279 280 /* A 24 bit relocation */ 281 HOWTO (R_M68HC11_24, /* type */ 282 0, /* rightshift */ 283 2, /* size (0 = byte, 1 = short, 2 = long) */ 284 24, /* bitsize */ 285 FALSE, /* pc_relative */ 286 0, /* bitpos */ 287 complain_overflow_dont, /* complain_on_overflow */ 288 m68hc11_elf_special_reloc, /* special_function */ 289 "R_M68HC12_24", /* name */ 290 FALSE, /* partial_inplace */ 291 0xffffff, /* src_mask */ 292 0xffffff, /* dst_mask */ 293 FALSE), /* pcrel_offset */ 294 295 /* A 16-bit low relocation */ 296 HOWTO (R_M68HC11_LO16, /* type */ 297 0, /* rightshift */ 298 1, /* size (0 = byte, 1 = short, 2 = long) */ 299 16, /* bitsize */ 300 FALSE, /* pc_relative */ 301 0, /* bitpos */ 302 complain_overflow_dont, /* complain_on_overflow */ 303 m68hc11_elf_special_reloc,/* special_function */ 304 "R_M68HC12_LO16", /* name */ 305 FALSE, /* partial_inplace */ 306 0xffff, /* src_mask */ 307 0xffff, /* dst_mask */ 308 FALSE), /* pcrel_offset */ 309 310 /* A page relocation */ 311 HOWTO (R_M68HC11_PAGE, /* type */ 312 0, /* rightshift */ 313 0, /* size (0 = byte, 1 = short, 2 = long) */ 314 8, /* bitsize */ 315 FALSE, /* pc_relative */ 316 0, /* bitpos */ 317 complain_overflow_dont, /* complain_on_overflow */ 318 m68hc11_elf_special_reloc,/* special_function */ 319 "R_M68HC12_PAGE", /* name */ 320 FALSE, /* partial_inplace */ 321 0x00ff, /* src_mask */ 322 0x00ff, /* dst_mask */ 323 FALSE), /* pcrel_offset */ 324 325 EMPTY_HOWTO (14), 326 EMPTY_HOWTO (15), 327 EMPTY_HOWTO (16), 328 EMPTY_HOWTO (17), 329 EMPTY_HOWTO (18), 330 EMPTY_HOWTO (19), 331 332 /* Mark beginning of a jump instruction (any form). */ 333 HOWTO (R_M68HC11_RL_JUMP, /* type */ 334 0, /* rightshift */ 335 1, /* size (0 = byte, 1 = short, 2 = long) */ 336 0, /* bitsize */ 337 FALSE, /* pc_relative */ 338 0, /* bitpos */ 339 complain_overflow_dont, /* complain_on_overflow */ 340 m68hc11_elf_ignore_reloc, /* special_function */ 341 "R_M68HC12_RL_JUMP", /* name */ 342 TRUE, /* partial_inplace */ 343 0, /* src_mask */ 344 0, /* dst_mask */ 345 TRUE), /* pcrel_offset */ 346 347 /* Mark beginning of Gcc relaxation group instruction. */ 348 HOWTO (R_M68HC11_RL_GROUP, /* type */ 349 0, /* rightshift */ 350 1, /* size (0 = byte, 1 = short, 2 = long) */ 351 0, /* bitsize */ 352 FALSE, /* pc_relative */ 353 0, /* bitpos */ 354 complain_overflow_dont, /* complain_on_overflow */ 355 m68hc11_elf_ignore_reloc, /* special_function */ 356 "R_M68HC12_RL_GROUP", /* name */ 357 TRUE, /* partial_inplace */ 358 0, /* src_mask */ 359 0, /* dst_mask */ 360 TRUE), /* pcrel_offset */ 361}; 362 363/* Map BFD reloc types to M68HC11 ELF reloc types. */ 364 365struct m68hc11_reloc_map 366{ 367 bfd_reloc_code_real_type bfd_reloc_val; 368 unsigned char elf_reloc_val; 369}; 370 371static const struct m68hc11_reloc_map m68hc11_reloc_map[] = { 372 {BFD_RELOC_NONE, R_M68HC11_NONE,}, 373 {BFD_RELOC_8, R_M68HC11_8}, 374 {BFD_RELOC_M68HC11_HI8, R_M68HC11_HI8}, 375 {BFD_RELOC_M68HC11_LO8, R_M68HC11_LO8}, 376 {BFD_RELOC_8_PCREL, R_M68HC11_PCREL_8}, 377 {BFD_RELOC_16_PCREL, R_M68HC11_PCREL_16}, 378 {BFD_RELOC_16, R_M68HC11_16}, 379 {BFD_RELOC_32, R_M68HC11_32}, 380 {BFD_RELOC_M68HC11_3B, R_M68HC11_3B}, 381 382 {BFD_RELOC_VTABLE_INHERIT, R_M68HC11_GNU_VTINHERIT}, 383 {BFD_RELOC_VTABLE_ENTRY, R_M68HC11_GNU_VTENTRY}, 384 385 {BFD_RELOC_M68HC11_LO16, R_M68HC11_LO16}, 386 {BFD_RELOC_M68HC11_PAGE, R_M68HC11_PAGE}, 387 {BFD_RELOC_M68HC11_24, R_M68HC11_24}, 388 389 {BFD_RELOC_M68HC11_RL_JUMP, R_M68HC11_RL_JUMP}, 390 {BFD_RELOC_M68HC11_RL_GROUP, R_M68HC11_RL_GROUP}, 391}; 392 393static reloc_howto_type * 394bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, 395 bfd_reloc_code_real_type code) 396{ 397 unsigned int i; 398 399 for (i = 0; 400 i < sizeof (m68hc11_reloc_map) / sizeof (struct m68hc11_reloc_map); 401 i++) 402 { 403 if (m68hc11_reloc_map[i].bfd_reloc_val == code) 404 return &elf_m68hc11_howto_table[m68hc11_reloc_map[i].elf_reloc_val]; 405 } 406 407 return NULL; 408} 409 410/* Set the howto pointer for an M68HC11 ELF reloc. */ 411 412static void 413m68hc11_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, 414 arelent *cache_ptr, Elf_Internal_Rela *dst) 415{ 416 unsigned int r_type; 417 418 r_type = ELF32_R_TYPE (dst->r_info); 419 BFD_ASSERT (r_type < (unsigned int) R_M68HC11_max); 420 cache_ptr->howto = &elf_m68hc11_howto_table[r_type]; 421} 422 423 424/* Far trampoline generation. */ 425 426/* Build a 68HC12 trampoline stub. */ 427static bfd_boolean 428m68hc12_elf_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg) 429{ 430 struct elf32_m68hc11_stub_hash_entry *stub_entry; 431 struct bfd_link_info *info; 432 struct m68hc11_elf_link_hash_table *htab; 433 asection *stub_sec; 434 bfd *stub_bfd; 435 bfd_byte *loc; 436 bfd_vma sym_value, phys_page, phys_addr; 437 438 /* Massage our args to the form they really have. */ 439 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry; 440 info = (struct bfd_link_info *) in_arg; 441 442 htab = m68hc11_elf_hash_table (info); 443 444 stub_sec = stub_entry->stub_sec; 445 446 /* Make a note of the offset within the stubs for this entry. */ 447 stub_entry->stub_offset = stub_sec->size; 448 stub_sec->size += 7; 449 loc = stub_sec->contents + stub_entry->stub_offset; 450 451 stub_bfd = stub_sec->owner; 452 453 /* Create the trampoline call stub: 454 455 ldy #%addr(symbol) 456 call %page(symbol), __trampoline 457 458 */ 459 sym_value = (stub_entry->target_value 460 + stub_entry->target_section->output_offset 461 + stub_entry->target_section->output_section->vma); 462 phys_addr = m68hc11_phys_addr (&htab->pinfo, sym_value); 463 phys_page = m68hc11_phys_page (&htab->pinfo, sym_value); 464 465 /* ldy #%page(sym) */ 466 bfd_put_8 (stub_bfd, 0xCD, loc); 467 bfd_put_16 (stub_bfd, phys_addr, loc + 1); 468 loc += 3; 469 470 /* call %page(sym), __trampoline */ 471 bfd_put_8 (stub_bfd, 0x4a, loc); 472 bfd_put_16 (stub_bfd, htab->pinfo.trampoline_addr, loc + 1); 473 bfd_put_8 (stub_bfd, phys_page, loc + 3); 474 475 return TRUE; 476} 477 478/* As above, but don't actually build the stub. Just bump offset so 479 we know stub section sizes. */ 480 481static bfd_boolean 482m68hc12_elf_size_one_stub (struct bfd_hash_entry *gen_entry, 483 void *in_arg ATTRIBUTE_UNUSED) 484{ 485 struct elf32_m68hc11_stub_hash_entry *stub_entry; 486 487 /* Massage our args to the form they really have. */ 488 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry; 489 490 stub_entry->stub_sec->size += 7; 491 return TRUE; 492} 493 494/* Create a 68HC12 ELF linker hash table. */ 495 496static struct bfd_link_hash_table * 497m68hc12_elf_bfd_link_hash_table_create (bfd *abfd) 498{ 499 struct m68hc11_elf_link_hash_table *ret; 500 501 ret = m68hc11_elf_hash_table_create (abfd); 502 if (ret == (struct m68hc11_elf_link_hash_table *) NULL) 503 return NULL; 504 505 ret->size_one_stub = m68hc12_elf_size_one_stub; 506 ret->build_one_stub = m68hc12_elf_build_one_stub; 507 508 return &ret->root.root; 509} 510 511static bfd_boolean 512m68hc12_elf_set_mach_from_flags (bfd *abfd) 513{ 514 flagword flags = elf_elfheader (abfd)->e_flags; 515 516 switch (flags & EF_M68HC11_MACH_MASK) 517 { 518 case EF_M68HC12_MACH: 519 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12, bfd_mach_m6812); 520 break; 521 case EF_M68HCS12_MACH: 522 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12, bfd_mach_m6812s); 523 break; 524 case EF_M68HC11_GENERIC: 525 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12, 526 bfd_mach_m6812_default); 527 break; 528 default: 529 return FALSE; 530 } 531 return TRUE; 532} 533 534/* Specific sections: 535 - The .page0 is a data section that is mapped in [0x0000..0x00FF]. 536 Page0 accesses are faster on the M68HC12. 537 - The .vectors is the section that represents the interrupt 538 vectors. */ 539static const struct bfd_elf_special_section elf32_m68hc12_special_sections[] = 540{ 541 { STRING_COMMA_LEN (".eeprom"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 542 { STRING_COMMA_LEN (".page0"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 543 { STRING_COMMA_LEN (".softregs"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 544 { STRING_COMMA_LEN (".vectors"), 0, SHT_PROGBITS, SHF_ALLOC }, 545 { NULL, 0, 0, 0, 0 } 546}; 547 548#define ELF_ARCH bfd_arch_m68hc12 549#define ELF_MACHINE_CODE EM_68HC12 550#define ELF_MAXPAGESIZE 0x1000 551 552#define TARGET_BIG_SYM bfd_elf32_m68hc12_vec 553#define TARGET_BIG_NAME "elf32-m68hc12" 554 555#define elf_info_to_howto 0 556#define elf_info_to_howto_rel m68hc11_info_to_howto_rel 557#define elf_backend_check_relocs elf32_m68hc11_check_relocs 558#define elf_backend_relocate_section elf32_m68hc11_relocate_section 559#define elf_backend_object_p m68hc12_elf_set_mach_from_flags 560#define elf_backend_final_write_processing 0 561#define elf_backend_can_gc_sections 1 562#define elf_backend_special_sections elf32_m68hc12_special_sections 563#define elf_backend_post_process_headers elf32_m68hc11_post_process_headers 564#define elf_backend_add_symbol_hook elf32_m68hc11_add_symbol_hook 565 566#define bfd_elf32_bfd_link_hash_table_create \ 567 m68hc12_elf_bfd_link_hash_table_create 568#define bfd_elf32_bfd_link_hash_table_free \ 569 m68hc11_elf_bfd_link_hash_table_free 570#define bfd_elf32_bfd_merge_private_bfd_data \ 571 _bfd_m68hc11_elf_merge_private_bfd_data 572#define bfd_elf32_bfd_set_private_flags _bfd_m68hc11_elf_set_private_flags 573#define bfd_elf32_bfd_print_private_bfd_data \ 574 _bfd_m68hc11_elf_print_private_bfd_data 575 576#include "elf32-target.h" 577