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