1 /* Motorola 68HC12-specific support for 32-bit ELF
2 Copyright 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007
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))
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
28 #include "elf32-m68hc1x.h"
29 #include "elf/m68hc11.h"
30 #include "opcode/m68hc11.h"
32 /* Relocation functions. */
33 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
34 (bfd *, bfd_reloc_code_real_type);
35 static void m68hc11_info_to_howto_rel
36 (bfd *, arelent *, Elf_Internal_Rela *);
38 /* Trampoline generation. */
39 static bfd_boolean m68hc12_elf_size_one_stub
40 (struct bfd_hash_entry *gen_entry, void *in_arg);
41 static bfd_boolean m68hc12_elf_build_one_stub
42 (struct bfd_hash_entry *gen_entry, void *in_arg);
43 static struct bfd_link_hash_table* m68hc12_elf_bfd_link_hash_table_create
46 static bfd_boolean m68hc12_elf_set_mach_from_flags PARAMS ((bfd *));
48 /* Use REL instead of RELA to save space */
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.
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.
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.
73 +---------------+ 0x1010000
78 +---------------+ 0x100C000
83 +---------------+ 0x1008000
85 | | call _foo | Page 1
87 | +---------------+ 0x1004000
89 Address Space | | | Page 0
91 +-----------+ 0x00FFFF | +---------------+ 0x1000000
95 +-----------+ 0x00BFFF -+---/
100 +-----------+ 0x008000 -+
109 The 'call _foo' must be relocated with page 3 and 16-bit address
112 The 3-bit and 16-bit PC rel relocation is only used by 68HC12. */
113 static reloc_howto_type elf_m68hc11_howto_table[] = {
114 /* This reloc does nothing. */
115 HOWTO (R_M68HC11_NONE, /* type */
117 2, /* size (0 = byte, 1 = short, 2 = long) */
119 FALSE, /* pc_relative */
121 complain_overflow_dont,/* complain_on_overflow */
122 bfd_elf_generic_reloc, /* special_function */
123 "R_M68HC12_NONE", /* name */
124 FALSE, /* partial_inplace */
127 FALSE), /* pcrel_offset */
129 /* A 8 bit absolute relocation */
130 HOWTO (R_M68HC11_8, /* type */
132 0, /* size (0 = byte, 1 = short, 2 = long) */
134 FALSE, /* pc_relative */
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 */
144 /* A 8 bit absolute relocation (upper address) */
145 HOWTO (R_M68HC11_HI8, /* type */
147 0, /* size (0 = byte, 1 = short, 2 = long) */
149 FALSE, /* pc_relative */
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 */
159 /* A 8 bit absolute relocation (upper address) */
160 HOWTO (R_M68HC11_LO8, /* type */
162 0, /* size (0 = byte, 1 = short, 2 = long) */
164 FALSE, /* pc_relative */
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 */
174 /* A 8 bit PC-rel relocation */
175 HOWTO (R_M68HC11_PCREL_8, /* type */
177 0, /* size (0 = byte, 1 = short, 2 = long) */
179 TRUE, /* pc_relative */
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 */
189 /* A 16 bit absolute relocation */
190 HOWTO (R_M68HC11_16, /* type */
192 1, /* size (0 = byte, 1 = short, 2 = long) */
194 FALSE, /* pc_relative */
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 */
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 */
208 2, /* size (0 = byte, 1 = short, 2 = long) */
210 FALSE, /* pc_relative */
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 */
220 /* A 3 bit absolute relocation */
221 HOWTO (R_M68HC11_3B, /* type */
223 0, /* size (0 = byte, 1 = short, 2 = long) */
225 FALSE, /* pc_relative */
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 */
235 /* A 16 bit PC-rel relocation */
236 HOWTO (R_M68HC11_PCREL_16, /* type */
238 1, /* size (0 = byte, 1 = short, 2 = long) */
240 TRUE, /* pc_relative */
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 */
250 /* GNU extension to record C++ vtable hierarchy */
251 HOWTO (R_M68HC11_GNU_VTINHERIT, /* type */
253 1, /* size (0 = byte, 1 = short, 2 = long) */
255 FALSE, /* pc_relative */
257 complain_overflow_dont, /* complain_on_overflow */
258 NULL, /* special_function */
259 "R_M68HC11_GNU_VTINHERIT", /* name */
260 FALSE, /* partial_inplace */
263 FALSE), /* pcrel_offset */
265 /* GNU extension to record C++ vtable member usage */
266 HOWTO (R_M68HC11_GNU_VTENTRY, /* type */
268 1, /* size (0 = byte, 1 = short, 2 = long) */
270 FALSE, /* pc_relative */
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 */
278 FALSE), /* pcrel_offset */
280 /* A 24 bit relocation */
281 HOWTO (R_M68HC11_24, /* type */
283 2, /* size (0 = byte, 1 = short, 2 = long) */
285 FALSE, /* pc_relative */
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 */
295 /* A 16-bit low relocation */
296 HOWTO (R_M68HC11_LO16, /* type */
298 1, /* size (0 = byte, 1 = short, 2 = long) */
300 FALSE, /* pc_relative */
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 */
310 /* A page relocation */
311 HOWTO (R_M68HC11_PAGE, /* type */
313 0, /* size (0 = byte, 1 = short, 2 = long) */
315 FALSE, /* pc_relative */
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 */
332 /* Mark beginning of a jump instruction (any form). */
333 HOWTO (R_M68HC11_RL_JUMP, /* type */
335 1, /* size (0 = byte, 1 = short, 2 = long) */
337 FALSE, /* pc_relative */
339 complain_overflow_dont, /* complain_on_overflow */
340 m68hc11_elf_ignore_reloc, /* special_function */
341 "R_M68HC12_RL_JUMP", /* name */
342 TRUE, /* partial_inplace */
345 TRUE), /* pcrel_offset */
347 /* Mark beginning of Gcc relaxation group instruction. */
348 HOWTO (R_M68HC11_RL_GROUP, /* type */
350 1, /* size (0 = byte, 1 = short, 2 = long) */
352 FALSE, /* pc_relative */
354 complain_overflow_dont, /* complain_on_overflow */
355 m68hc11_elf_ignore_reloc, /* special_function */
356 "R_M68HC12_RL_GROUP", /* name */
357 TRUE, /* partial_inplace */
360 TRUE), /* pcrel_offset */
363 /* Map BFD reloc types to M68HC11 ELF reloc types. */
365 struct m68hc11_reloc_map
367 bfd_reloc_code_real_type bfd_reloc_val;
368 unsigned char elf_reloc_val;
371 static 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},
382 {BFD_RELOC_VTABLE_INHERIT, R_M68HC11_GNU_VTINHERIT},
383 {BFD_RELOC_VTABLE_ENTRY, R_M68HC11_GNU_VTENTRY},
385 {BFD_RELOC_M68HC11_LO16, R_M68HC11_LO16},
386 {BFD_RELOC_M68HC11_PAGE, R_M68HC11_PAGE},
387 {BFD_RELOC_M68HC11_24, R_M68HC11_24},
389 {BFD_RELOC_M68HC11_RL_JUMP, R_M68HC11_RL_JUMP},
390 {BFD_RELOC_M68HC11_RL_GROUP, R_M68HC11_RL_GROUP},
393 static reloc_howto_type *
394 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
395 bfd_reloc_code_real_type code)
400 i < sizeof (m68hc11_reloc_map) / sizeof (struct m68hc11_reloc_map);
403 if (m68hc11_reloc_map[i].bfd_reloc_val == code)
404 return &elf_m68hc11_howto_table[m68hc11_reloc_map[i].elf_reloc_val];
410 static reloc_howto_type *
411 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
417 i < (sizeof (elf_m68hc11_howto_table)
418 / sizeof (elf_m68hc11_howto_table[0]));
420 if (elf_m68hc11_howto_table[i].name != NULL
421 && strcasecmp (elf_m68hc11_howto_table[i].name, r_name) == 0)
422 return &elf_m68hc11_howto_table[i];
427 /* Set the howto pointer for an M68HC11 ELF reloc. */
430 m68hc11_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
431 arelent *cache_ptr, Elf_Internal_Rela *dst)
435 r_type = ELF32_R_TYPE (dst->r_info);
436 BFD_ASSERT (r_type < (unsigned int) R_M68HC11_max);
437 cache_ptr->howto = &elf_m68hc11_howto_table[r_type];
441 /* Far trampoline generation. */
443 /* Build a 68HC12 trampoline stub. */
445 m68hc12_elf_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
447 struct elf32_m68hc11_stub_hash_entry *stub_entry;
448 struct bfd_link_info *info;
449 struct m68hc11_elf_link_hash_table *htab;
453 bfd_vma sym_value, phys_page, phys_addr;
455 /* Massage our args to the form they really have. */
456 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
457 info = (struct bfd_link_info *) in_arg;
459 htab = m68hc11_elf_hash_table (info);
461 stub_sec = stub_entry->stub_sec;
463 /* Make a note of the offset within the stubs for this entry. */
464 stub_entry->stub_offset = stub_sec->size;
466 loc = stub_sec->contents + stub_entry->stub_offset;
468 stub_bfd = stub_sec->owner;
470 /* Create the trampoline call stub:
473 call %page(symbol), __trampoline
476 sym_value = (stub_entry->target_value
477 + stub_entry->target_section->output_offset
478 + stub_entry->target_section->output_section->vma);
479 phys_addr = m68hc11_phys_addr (&htab->pinfo, sym_value);
480 phys_page = m68hc11_phys_page (&htab->pinfo, sym_value);
482 /* ldy #%page(sym) */
483 bfd_put_8 (stub_bfd, 0xCD, loc);
484 bfd_put_16 (stub_bfd, phys_addr, loc + 1);
487 /* call %page(sym), __trampoline */
488 bfd_put_8 (stub_bfd, 0x4a, loc);
489 bfd_put_16 (stub_bfd, htab->pinfo.trampoline_addr, loc + 1);
490 bfd_put_8 (stub_bfd, phys_page, loc + 3);
495 /* As above, but don't actually build the stub. Just bump offset so
496 we know stub section sizes. */
499 m68hc12_elf_size_one_stub (struct bfd_hash_entry *gen_entry,
500 void *in_arg ATTRIBUTE_UNUSED)
502 struct elf32_m68hc11_stub_hash_entry *stub_entry;
504 /* Massage our args to the form they really have. */
505 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
507 stub_entry->stub_sec->size += 7;
511 /* Create a 68HC12 ELF linker hash table. */
513 static struct bfd_link_hash_table *
514 m68hc12_elf_bfd_link_hash_table_create (bfd *abfd)
516 struct m68hc11_elf_link_hash_table *ret;
518 ret = m68hc11_elf_hash_table_create (abfd);
519 if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
522 ret->size_one_stub = m68hc12_elf_size_one_stub;
523 ret->build_one_stub = m68hc12_elf_build_one_stub;
525 return &ret->root.root;
529 m68hc12_elf_set_mach_from_flags (bfd *abfd)
531 flagword flags = elf_elfheader (abfd)->e_flags;
533 switch (flags & EF_M68HC11_MACH_MASK)
535 case EF_M68HC12_MACH:
536 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12, bfd_mach_m6812);
538 case EF_M68HCS12_MACH:
539 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12, bfd_mach_m6812s);
541 case EF_M68HC11_GENERIC:
542 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12,
543 bfd_mach_m6812_default);
551 /* Specific sections:
552 - The .page0 is a data section that is mapped in [0x0000..0x00FF].
553 Page0 accesses are faster on the M68HC12.
554 - The .vectors is the section that represents the interrupt
556 static const struct bfd_elf_special_section elf32_m68hc12_special_sections[] =
558 { STRING_COMMA_LEN (".eeprom"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
559 { STRING_COMMA_LEN (".page0"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
560 { STRING_COMMA_LEN (".softregs"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
561 { STRING_COMMA_LEN (".vectors"), 0, SHT_PROGBITS, SHF_ALLOC },
565 #define ELF_ARCH bfd_arch_m68hc12
566 #define ELF_MACHINE_CODE EM_68HC12
567 #define ELF_MAXPAGESIZE 0x1000
569 #define TARGET_BIG_SYM bfd_elf32_m68hc12_vec
570 #define TARGET_BIG_NAME "elf32-m68hc12"
572 #define elf_info_to_howto 0
573 #define elf_info_to_howto_rel m68hc11_info_to_howto_rel
574 #define elf_backend_check_relocs elf32_m68hc11_check_relocs
575 #define elf_backend_relocate_section elf32_m68hc11_relocate_section
576 #define elf_backend_object_p m68hc12_elf_set_mach_from_flags
577 #define elf_backend_final_write_processing 0
578 #define elf_backend_can_gc_sections 1
579 #define elf_backend_special_sections elf32_m68hc12_special_sections
580 #define elf_backend_post_process_headers elf32_m68hc11_post_process_headers
581 #define elf_backend_add_symbol_hook elf32_m68hc11_add_symbol_hook
583 #define bfd_elf32_bfd_link_hash_table_create \
584 m68hc12_elf_bfd_link_hash_table_create
585 #define bfd_elf32_bfd_link_hash_table_free \
586 m68hc11_elf_bfd_link_hash_table_free
587 #define bfd_elf32_bfd_merge_private_bfd_data \
588 _bfd_m68hc11_elf_merge_private_bfd_data
589 #define bfd_elf32_bfd_set_private_flags _bfd_m68hc11_elf_set_private_flags
590 #define bfd_elf32_bfd_print_private_bfd_data \
591 _bfd_m68hc11_elf_print_private_bfd_data
593 #include "elf32-target.h"