]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-m68hc12.c
[gdb/symtab] Work around PR gas/29517, dwarf2 case
[thirdparty/binutils-gdb.git] / bfd / elf32-m68hc12.c
CommitLineData
60bcf0fa 1/* Motorola 68HC12-specific support for 32-bit ELF
fd67aa11 2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
dae78fb0 3 Contributed by Stephane Carrez (stcarrez@nerim.fr)
60bcf0fa
NC
4 (Heavily copied from the D10V port by Martin Hunt (hunt@cygnus.com))
5
cd123cb7 6 This file is part of BFD, the Binary File Descriptor library.
60bcf0fa 7
cd123cb7
NC
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 3 of the License, or
11 (at your option) any later version.
60bcf0fa 12
cd123cb7
NC
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.
60bcf0fa 17
cd123cb7
NC
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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
60bcf0fa 22
60bcf0fa 23#include "sysdep.h"
3db64b00 24#include "bfd.h"
3a65329d 25#include "bfdlink.h"
60bcf0fa
NC
26#include "libbfd.h"
27#include "elf-bfd.h"
3a65329d 28#include "elf32-m68hc1x.h"
60bcf0fa 29#include "elf/m68hc11.h"
dae78fb0 30#include "opcode/m68hc11.h"
60bcf0fa 31
3a65329d 32/* Relocation functions. */
60bcf0fa 33static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
0a6a3ebe 34 (bfd *, bfd_reloc_code_real_type);
0a1b45a2 35static bool m68hc11_info_to_howto_rel
0a6a3ebe 36 (bfd *, arelent *, Elf_Internal_Rela *);
60bcf0fa 37
3a65329d 38/* Trampoline generation. */
2c3fc389 39
cf3d882d 40
60bcf0fa 41/* Use REL instead of RELA to save space */
acf8aed4 42#define USE_REL 1
60bcf0fa 43
3a65329d 44/* The 68HC12 microcontroler has a memory bank switching system
dae78fb0
SC
45 with a 16Kb window in the 64Kb address space. The extended memory
46 is mapped in the 16Kb window (at 0x8000). The page register controls
47 which 16Kb bank is mapped. The call/rtc instructions take care of
48 bank switching in function calls/returns.
49
50 For GNU Binutils to work, we consider there is a physical memory
51 at 0..0x0ffff and a kind of virtual memory above that. Symbols
52 in virtual memory have their addresses treated in a special way
53 when disassembling and when linking.
54
55 For the linker to work properly, we must always relocate the virtual
56 memory as if it is mapped at 0x8000. When a 16-bit relocation is
57 made in the virtual memory, we check that it does not cross the
58 memory bank where it is used. This would involve a page change
59 which would be wrong. The 24-bit relocation is for that and it
60 treats the address as a physical address + page number.
61
62
63 Banked
64 Address Space
07d6d2b8 65 | | Page n
dae78fb0 66 +---------------+ 0x1010000
07d6d2b8
AM
67 | |
68 | jsr _foo |
69 | .. | Page 3
70 | _foo: |
dae78fb0 71 +---------------+ 0x100C000
07d6d2b8
AM
72 | |
73 | call _bar |
74 | .. | Page 2
75 | _bar: |
dae78fb0 76 +---------------+ 0x1008000
07d6d2b8
AM
77 /------>| |
78 | | call _foo | Page 1
79 | | |
dae78fb0 80 | +---------------+ 0x1004000
07d6d2b8
AM
81 Physical | | |
82 Address Space | | | Page 0
83 | | |
dae78fb0
SC
84 +-----------+ 0x00FFFF | +---------------+ 0x1000000
85 | | |
86 | call _foo | |
87 | | |
88 +-----------+ 0x00BFFF -+---/
07d6d2b8 89 | | |
dae78fb0
SC
90 | | |
91 | | 16K |
92 | | |
93 +-----------+ 0x008000 -+
94 | |
95 | |
96 = =
97 | |
98 | |
99 +-----------+ 0000
100
101
102 The 'call _foo' must be relocated with page 3 and 16-bit address
b34976b6 103 mapped at 0x8000.
dae78fb0 104
60bcf0fa
NC
105 The 3-bit and 16-bit PC rel relocation is only used by 68HC12. */
106static reloc_howto_type elf_m68hc11_howto_table[] = {
107 /* This reloc does nothing. */
108 HOWTO (R_M68HC11_NONE, /* type */
109 0, /* rightshift */
c94cb026 110 0, /* size */
6346d5ca 111 0, /* bitsize */
0a1b45a2 112 false, /* pc_relative */
60bcf0fa 113 0, /* bitpos */
dae78fb0 114 complain_overflow_dont,/* complain_on_overflow */
60bcf0fa 115 bfd_elf_generic_reloc, /* special_function */
dae78fb0 116 "R_M68HC12_NONE", /* name */
0a1b45a2 117 false, /* partial_inplace */
60bcf0fa
NC
118 0, /* src_mask */
119 0, /* dst_mask */
0a1b45a2 120 false), /* pcrel_offset */
60bcf0fa
NC
121
122 /* A 8 bit absolute relocation */
123 HOWTO (R_M68HC11_8, /* type */
124 0, /* rightshift */
c94cb026 125 1, /* size */
60bcf0fa 126 8, /* bitsize */
0a1b45a2 127 false, /* pc_relative */
60bcf0fa
NC
128 0, /* bitpos */
129 complain_overflow_bitfield, /* complain_on_overflow */
130 bfd_elf_generic_reloc, /* special_function */
dae78fb0 131 "R_M68HC12_8", /* name */
0a1b45a2 132 false, /* partial_inplace */
60bcf0fa
NC
133 0x00ff, /* src_mask */
134 0x00ff, /* dst_mask */
0a1b45a2 135 false), /* pcrel_offset */
60bcf0fa
NC
136
137 /* A 8 bit absolute relocation (upper address) */
138 HOWTO (R_M68HC11_HI8, /* type */
139 8, /* rightshift */
c94cb026 140 1, /* size */
60bcf0fa 141 8, /* bitsize */
0a1b45a2 142 false, /* pc_relative */
60bcf0fa
NC
143 0, /* bitpos */
144 complain_overflow_bitfield, /* complain_on_overflow */
145 bfd_elf_generic_reloc, /* special_function */
dae78fb0 146 "R_M68HC12_HI8", /* name */
0a1b45a2 147 false, /* partial_inplace */
60bcf0fa
NC
148 0x00ff, /* src_mask */
149 0x00ff, /* dst_mask */
0a1b45a2 150 false), /* pcrel_offset */
60bcf0fa
NC
151
152 /* A 8 bit absolute relocation (upper address) */
153 HOWTO (R_M68HC11_LO8, /* type */
154 0, /* rightshift */
c94cb026 155 1, /* size */
60bcf0fa 156 8, /* bitsize */
0a1b45a2 157 false, /* pc_relative */
60bcf0fa
NC
158 0, /* bitpos */
159 complain_overflow_dont, /* complain_on_overflow */
160 bfd_elf_generic_reloc, /* special_function */
dae78fb0 161 "R_M68HC12_LO8", /* name */
0a1b45a2 162 false, /* partial_inplace */
60bcf0fa
NC
163 0x00ff, /* src_mask */
164 0x00ff, /* dst_mask */
0a1b45a2 165 false), /* pcrel_offset */
60bcf0fa
NC
166
167 /* A 8 bit PC-rel relocation */
168 HOWTO (R_M68HC11_PCREL_8, /* type */
169 0, /* rightshift */
c94cb026 170 1, /* size */
60bcf0fa 171 8, /* bitsize */
0a1b45a2 172 true, /* pc_relative */
60bcf0fa
NC
173 0, /* bitpos */
174 complain_overflow_bitfield, /* complain_on_overflow */
175 bfd_elf_generic_reloc, /* special_function */
dae78fb0 176 "R_M68HC12_PCREL_8", /* name */
0a1b45a2 177 false, /* partial_inplace */
dae78fb0 178 0x00ff, /* src_mask */
60bcf0fa 179 0x00ff, /* dst_mask */
0a1b45a2 180 true), /* pcrel_offset */
60bcf0fa
NC
181
182 /* A 16 bit absolute relocation */
183 HOWTO (R_M68HC11_16, /* type */
184 0, /* rightshift */
c94cb026 185 2, /* size */
60bcf0fa 186 16, /* bitsize */
0a1b45a2 187 false, /* pc_relative */
60bcf0fa
NC
188 0, /* bitpos */
189 complain_overflow_dont /*bitfield */ , /* complain_on_overflow */
3a65329d 190 bfd_elf_generic_reloc, /* special_function */
dae78fb0 191 "R_M68HC12_16", /* name */
0a1b45a2 192 false, /* partial_inplace */
60bcf0fa
NC
193 0xffff, /* src_mask */
194 0xffff, /* dst_mask */
0a1b45a2 195 false), /* pcrel_offset */
60bcf0fa
NC
196
197 /* A 32 bit absolute relocation. This one is never used for the
198 code relocation. It's used by gas for -gstabs generation. */
199 HOWTO (R_M68HC11_32, /* type */
200 0, /* rightshift */
c94cb026 201 4, /* size */
60bcf0fa 202 32, /* bitsize */
0a1b45a2 203 false, /* pc_relative */
60bcf0fa
NC
204 0, /* bitpos */
205 complain_overflow_bitfield, /* complain_on_overflow */
206 bfd_elf_generic_reloc, /* special_function */
dae78fb0 207 "R_M68HC12_32", /* name */
0a1b45a2 208 false, /* partial_inplace */
60bcf0fa
NC
209 0xffffffff, /* src_mask */
210 0xffffffff, /* dst_mask */
0a1b45a2 211 false), /* pcrel_offset */
60bcf0fa
NC
212
213 /* A 3 bit absolute relocation */
214 HOWTO (R_M68HC11_3B, /* type */
215 0, /* rightshift */
c94cb026 216 1, /* size */
60bcf0fa 217 3, /* bitsize */
0a1b45a2 218 false, /* pc_relative */
60bcf0fa
NC
219 0, /* bitpos */
220 complain_overflow_bitfield, /* complain_on_overflow */
221 bfd_elf_generic_reloc, /* special_function */
dae78fb0 222 "R_M68HC12_4B", /* name */
0a1b45a2 223 false, /* partial_inplace */
60bcf0fa
NC
224 0x003, /* src_mask */
225 0x003, /* dst_mask */
0a1b45a2 226 false), /* pcrel_offset */
60bcf0fa
NC
227
228 /* A 16 bit PC-rel relocation */
229 HOWTO (R_M68HC11_PCREL_16, /* type */
230 0, /* rightshift */
c94cb026 231 2, /* size */
60bcf0fa 232 16, /* bitsize */
0a1b45a2 233 true, /* pc_relative */
60bcf0fa
NC
234 0, /* bitpos */
235 complain_overflow_dont, /* complain_on_overflow */
236 bfd_elf_generic_reloc, /* special_function */
dae78fb0 237 "R_M68HC12_PCREL_16", /* name */
0a1b45a2 238 false, /* partial_inplace */
dae78fb0 239 0xffff, /* src_mask */
60bcf0fa 240 0xffff, /* dst_mask */
0a1b45a2 241 true), /* pcrel_offset */
60bcf0fa
NC
242
243 /* GNU extension to record C++ vtable hierarchy */
244 HOWTO (R_M68HC11_GNU_VTINHERIT, /* type */
245 0, /* rightshift */
c94cb026 246 2, /* size */
60bcf0fa 247 0, /* bitsize */
0a1b45a2 248 false, /* pc_relative */
60bcf0fa
NC
249 0, /* bitpos */
250 complain_overflow_dont, /* complain_on_overflow */
251 NULL, /* special_function */
252 "R_M68HC11_GNU_VTINHERIT", /* name */
0a1b45a2 253 false, /* partial_inplace */
60bcf0fa
NC
254 0, /* src_mask */
255 0, /* dst_mask */
0a1b45a2 256 false), /* pcrel_offset */
60bcf0fa
NC
257
258 /* GNU extension to record C++ vtable member usage */
259 HOWTO (R_M68HC11_GNU_VTENTRY, /* type */
260 0, /* rightshift */
c94cb026 261 2, /* size */
60bcf0fa 262 0, /* bitsize */
0a1b45a2 263 false, /* pc_relative */
60bcf0fa
NC
264 0, /* bitpos */
265 complain_overflow_dont, /* complain_on_overflow */
266 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
267 "R_M68HC11_GNU_VTENTRY", /* name */
0a1b45a2 268 false, /* partial_inplace */
60bcf0fa
NC
269 0, /* src_mask */
270 0, /* dst_mask */
0a1b45a2 271 false), /* pcrel_offset */
dae78fb0
SC
272
273 /* A 24 bit relocation */
07d6d2b8 274 HOWTO (R_M68HC11_24, /* type */
dae78fb0 275 0, /* rightshift */
c94cb026 276 4, /* size */
dae78fb0 277 24, /* bitsize */
0a1b45a2 278 false, /* pc_relative */
dae78fb0
SC
279 0, /* bitpos */
280 complain_overflow_dont, /* complain_on_overflow */
3a65329d 281 m68hc11_elf_special_reloc, /* special_function */
dae78fb0 282 "R_M68HC12_24", /* name */
0a1b45a2 283 false, /* partial_inplace */
3a65329d
SC
284 0xffffff, /* src_mask */
285 0xffffff, /* dst_mask */
0a1b45a2 286 false), /* pcrel_offset */
b34976b6 287
dae78fb0 288 /* A 16-bit low relocation */
07d6d2b8 289 HOWTO (R_M68HC11_LO16, /* type */
dae78fb0 290 0, /* rightshift */
c94cb026 291 2, /* size */
dae78fb0 292 16, /* bitsize */
0a1b45a2 293 false, /* pc_relative */
dae78fb0
SC
294 0, /* bitpos */
295 complain_overflow_dont, /* complain_on_overflow */
3a65329d 296 m68hc11_elf_special_reloc,/* special_function */
dae78fb0 297 "R_M68HC12_LO16", /* name */
0a1b45a2 298 false, /* partial_inplace */
dae78fb0
SC
299 0xffff, /* src_mask */
300 0xffff, /* dst_mask */
0a1b45a2 301 false), /* pcrel_offset */
dae78fb0
SC
302
303 /* A page relocation */
07d6d2b8 304 HOWTO (R_M68HC11_PAGE, /* type */
dae78fb0 305 0, /* rightshift */
c94cb026 306 1, /* size */
dae78fb0 307 8, /* bitsize */
0a1b45a2 308 false, /* pc_relative */
dae78fb0
SC
309 0, /* bitpos */
310 complain_overflow_dont, /* complain_on_overflow */
3a65329d 311 m68hc11_elf_special_reloc,/* special_function */
dae78fb0 312 "R_M68HC12_PAGE", /* name */
0a1b45a2 313 false, /* partial_inplace */
dae78fb0
SC
314 0x00ff, /* src_mask */
315 0x00ff, /* dst_mask */
0a1b45a2 316 false), /* pcrel_offset */
dae78fb0
SC
317
318 EMPTY_HOWTO (14),
6927f982
NC
319
320 /* A 16 bit absolute relocation. */
321 HOWTO (R_M68HC12_16B, /* type */
322 0, /* rightshift */
c94cb026 323 2, /* size */
6927f982 324 16, /* bitsize */
0a1b45a2 325 false, /* pc_relative */
6927f982
NC
326 0, /* bitpos */
327 complain_overflow_bitfield, /* complain_on_overflow */
328 bfd_elf_generic_reloc, /* special_function */
329 "R_M68HC12_16B", /* name */
0a1b45a2 330 false, /* partial_inplace */
07d6d2b8
AM
331 0xffff, /* src_mask */
332 0xffff, /* dst_mask */
0a1b45a2 333 false), /* pcrel_offset */
6927f982
NC
334
335 /* A 9 bit PC-rel relocation. */
336 HOWTO (R_M68HC12_PCREL_9, /* type */
337 1, /* rightshift */
c94cb026 338 2, /* size */
6927f982 339 10, /* bitsize (result is >>1) */
0a1b45a2 340 true, /* pc_relative */
6927f982
NC
341 0, /* bitpos */
342 complain_overflow_dont, /* complain_on_overflow */
343 bfd_elf_generic_reloc, /* special_function */
344 "R_M68HC12_PCREL_9", /* name */
0a1b45a2 345 true, /* partial_inplace */
6927f982
NC
346 0xfe00, /* src_mask */
347 0x01ff, /* dst_mask */
0a1b45a2 348 true), /* pcrel_offset */
6927f982
NC
349
350 /* A 10 bit PC-rel relocation. */
351 HOWTO (R_M68HC12_PCREL_10, /* type */
352 1, /* rightshift */
c94cb026 353 2, /* size */
6927f982 354 11, /* bitsize (result is >>1) */
0a1b45a2 355 true, /* pc_relative */
6927f982
NC
356 0, /* bitpos */
357 complain_overflow_dont, /* complain_on_overflow */
358 bfd_elf_generic_reloc, /* special_function */
359 "R_M68HC12_PCREL_10", /* name */
0a1b45a2 360 true, /* partial_inplace */
6927f982
NC
361 0xfc00, /* src_mask */
362 0x03ff, /* dst_mask */
0a1b45a2 363 true), /* pcrel_offset */
6927f982
NC
364
365 /* A 8 bit absolute relocation (upper address). */
366 HOWTO (R_M68HC12_HI8XG, /* type */
367 8, /* rightshift */
c94cb026 368 1, /* size */
6927f982 369 8, /* bitsize */
0a1b45a2 370 false, /* pc_relative */
6927f982
NC
371 0, /* bitpos */
372 complain_overflow_bitfield, /* complain_on_overflow */
373 bfd_elf_generic_reloc, /* special_function */
374 "R_M68HC12_HI8XG", /* name */
0a1b45a2 375 false, /* partial_inplace */
6927f982
NC
376 0x00ff, /* src_mask */
377 0x00ff, /* dst_mask */
0a1b45a2 378 false), /* pcrel_offset */
6927f982
NC
379
380 /* A 8 bit absolute relocation (lower address). */
381 HOWTO (R_M68HC12_LO8XG, /* type */
382 8, /* rightshift */
c94cb026 383 1, /* size */
6927f982 384 8, /* bitsize */
0a1b45a2 385 false, /* pc_relative */
6927f982
NC
386 0, /* bitpos */
387 complain_overflow_bitfield, /* complain_on_overflow */
388 bfd_elf_generic_reloc, /* special_function */
389 "R_M68HC12_LO8XG", /* name */
0a1b45a2 390 false, /* partial_inplace */
6927f982
NC
391 0x00ff, /* src_mask */
392 0x00ff, /* dst_mask */
0a1b45a2 393 false), /* pcrel_offset */
b34976b6 394
dae78fb0
SC
395 /* Mark beginning of a jump instruction (any form). */
396 HOWTO (R_M68HC11_RL_JUMP, /* type */
397 0, /* rightshift */
c94cb026 398 2, /* size */
dae78fb0 399 0, /* bitsize */
0a1b45a2 400 false, /* pc_relative */
dae78fb0
SC
401 0, /* bitpos */
402 complain_overflow_dont, /* complain_on_overflow */
403 m68hc11_elf_ignore_reloc, /* special_function */
404 "R_M68HC12_RL_JUMP", /* name */
0a1b45a2 405 true, /* partial_inplace */
dae78fb0
SC
406 0, /* src_mask */
407 0, /* dst_mask */
0a1b45a2 408 true), /* pcrel_offset */
dae78fb0
SC
409
410 /* Mark beginning of Gcc relaxation group instruction. */
411 HOWTO (R_M68HC11_RL_GROUP, /* type */
412 0, /* rightshift */
c94cb026 413 2, /* size */
dae78fb0 414 0, /* bitsize */
0a1b45a2 415 false, /* pc_relative */
dae78fb0
SC
416 0, /* bitpos */
417 complain_overflow_dont, /* complain_on_overflow */
418 m68hc11_elf_ignore_reloc, /* special_function */
419 "R_M68HC12_RL_GROUP", /* name */
0a1b45a2 420 true, /* partial_inplace */
dae78fb0
SC
421 0, /* src_mask */
422 0, /* dst_mask */
0a1b45a2 423 true), /* pcrel_offset */
60bcf0fa
NC
424};
425
426/* Map BFD reloc types to M68HC11 ELF reloc types. */
427
428struct m68hc11_reloc_map
429{
430 bfd_reloc_code_real_type bfd_reloc_val;
431 unsigned char elf_reloc_val;
432};
433
6927f982
NC
434static const struct m68hc11_reloc_map m68hc11_reloc_map[] =
435{
60bcf0fa
NC
436 {BFD_RELOC_NONE, R_M68HC11_NONE,},
437 {BFD_RELOC_8, R_M68HC11_8},
438 {BFD_RELOC_M68HC11_HI8, R_M68HC11_HI8},
439 {BFD_RELOC_M68HC11_LO8, R_M68HC11_LO8},
440 {BFD_RELOC_8_PCREL, R_M68HC11_PCREL_8},
441 {BFD_RELOC_16_PCREL, R_M68HC11_PCREL_16},
442 {BFD_RELOC_16, R_M68HC11_16},
443 {BFD_RELOC_32, R_M68HC11_32},
444 {BFD_RELOC_M68HC11_3B, R_M68HC11_3B},
445
60bcf0fa
NC
446 {BFD_RELOC_VTABLE_INHERIT, R_M68HC11_GNU_VTINHERIT},
447 {BFD_RELOC_VTABLE_ENTRY, R_M68HC11_GNU_VTENTRY},
dae78fb0
SC
448
449 {BFD_RELOC_M68HC11_LO16, R_M68HC11_LO16},
450 {BFD_RELOC_M68HC11_PAGE, R_M68HC11_PAGE},
451 {BFD_RELOC_M68HC11_24, R_M68HC11_24},
452
453 {BFD_RELOC_M68HC11_RL_JUMP, R_M68HC11_RL_JUMP},
454 {BFD_RELOC_M68HC11_RL_GROUP, R_M68HC11_RL_GROUP},
6927f982
NC
455
456 {BFD_RELOC_M68HC12_16B, R_M68HC12_16B},
457
458 {BFD_RELOC_M68HC12_9_PCREL, R_M68HC12_PCREL_9},
459 {BFD_RELOC_M68HC12_10_PCREL, R_M68HC12_PCREL_10},
460 {BFD_RELOC_M68HC12_HI8XG, R_M68HC12_HI8XG},
461 {BFD_RELOC_M68HC12_LO8XG, R_M68HC12_LO8XG},
60bcf0fa
NC
462};
463
464static reloc_howto_type *
0a6a3ebe 465bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
07d6d2b8 466 bfd_reloc_code_real_type code)
60bcf0fa
NC
467{
468 unsigned int i;
469
470 for (i = 0;
471 i < sizeof (m68hc11_reloc_map) / sizeof (struct m68hc11_reloc_map);
472 i++)
473 {
474 if (m68hc11_reloc_map[i].bfd_reloc_val == code)
475 return &elf_m68hc11_howto_table[m68hc11_reloc_map[i].elf_reloc_val];
476 }
477
478 return NULL;
479}
480
157090f7
AM
481static reloc_howto_type *
482bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
483 const char *r_name)
484{
485 unsigned int i;
486
487 for (i = 0;
488 i < (sizeof (elf_m68hc11_howto_table)
489 / sizeof (elf_m68hc11_howto_table[0]));
490 i++)
491 if (elf_m68hc11_howto_table[i].name != NULL
492 && strcasecmp (elf_m68hc11_howto_table[i].name, r_name) == 0)
493 return &elf_m68hc11_howto_table[i];
494
495 return NULL;
496}
497
3a65329d 498/* Set the howto pointer for an M68HC11 ELF reloc. */
dae78fb0 499
0a1b45a2 500static bool
0aa13fee 501m68hc11_info_to_howto_rel (bfd *abfd,
07d6d2b8 502 arelent *cache_ptr, Elf_Internal_Rela *dst)
dae78fb0 503{
3a65329d 504 unsigned int r_type;
dae78fb0 505
3a65329d 506 r_type = ELF32_R_TYPE (dst->r_info);
5860e3f8
NC
507 if (r_type >= (unsigned int) R_M68HC11_max)
508 {
695344c0 509 /* xgettext:c-format */
0aa13fee
AM
510 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
511 abfd, r_type);
f3185997 512 bfd_set_error (bfd_error_bad_value);
0a1b45a2 513 return false;
5860e3f8 514 }
3a65329d 515 cache_ptr->howto = &elf_m68hc11_howto_table[r_type];
0a1b45a2 516 return true;
dae78fb0
SC
517}
518
3a65329d
SC
519\f
520/* Far trampoline generation. */
dae78fb0 521
3a65329d 522/* Build a 68HC12 trampoline stub. */
0a1b45a2 523static bool
0a6a3ebe 524m68hc12_elf_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
dae78fb0 525{
3a65329d
SC
526 struct elf32_m68hc11_stub_hash_entry *stub_entry;
527 struct bfd_link_info *info;
528 struct m68hc11_elf_link_hash_table *htab;
529 asection *stub_sec;
530 bfd *stub_bfd;
531 bfd_byte *loc;
532 bfd_vma sym_value, phys_page, phys_addr;
dae78fb0 533
3a65329d
SC
534 /* Massage our args to the form they really have. */
535 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
536 info = (struct bfd_link_info *) in_arg;
dae78fb0 537
abf874aa
CL
538 /* Fail if the target section could not be assigned to an output
539 section. The user should fix his linker script. */
540 if (stub_entry->target_section->output_section == NULL
541 && info->non_contiguous_regions)
c63d4862 542 info->callbacks->einfo (_("%F%P: Could not assign `%pA' to an output section. "
53215f21
CL
543 "Retry without --enable-non-contiguous-regions.\n"),
544 stub_entry->target_section);
abf874aa 545
3a65329d 546 htab = m68hc11_elf_hash_table (info);
dae78fb0 547
3a65329d 548 stub_sec = stub_entry->stub_sec;
dae78fb0 549
3a65329d 550 /* Make a note of the offset within the stubs for this entry. */
eea6121a
AM
551 stub_entry->stub_offset = stub_sec->size;
552 stub_sec->size += 7;
3a65329d 553 loc = stub_sec->contents + stub_entry->stub_offset;
dae78fb0 554
3a65329d 555 stub_bfd = stub_sec->owner;
dae78fb0 556
3a65329d 557 /* Create the trampoline call stub:
dae78fb0 558
3a65329d
SC
559 ldy #%addr(symbol)
560 call %page(symbol), __trampoline
dae78fb0 561
3a65329d
SC
562 */
563 sym_value = (stub_entry->target_value
07d6d2b8
AM
564 + stub_entry->target_section->output_offset
565 + stub_entry->target_section->output_section->vma);
3a65329d
SC
566 phys_addr = m68hc11_phys_addr (&htab->pinfo, sym_value);
567 phys_page = m68hc11_phys_page (&htab->pinfo, sym_value);
dae78fb0 568
3a65329d
SC
569 /* ldy #%page(sym) */
570 bfd_put_8 (stub_bfd, 0xCD, loc);
571 bfd_put_16 (stub_bfd, phys_addr, loc + 1);
572 loc += 3;
dae78fb0 573
3a65329d
SC
574 /* call %page(sym), __trampoline */
575 bfd_put_8 (stub_bfd, 0x4a, loc);
576 bfd_put_16 (stub_bfd, htab->pinfo.trampoline_addr, loc + 1);
577 bfd_put_8 (stub_bfd, phys_page, loc + 3);
b34976b6 578
0a1b45a2 579 return true;
dae78fb0
SC
580}
581
3a65329d
SC
582/* As above, but don't actually build the stub. Just bump offset so
583 we know stub section sizes. */
60bcf0fa 584
0a1b45a2 585static bool
0a6a3ebe 586m68hc12_elf_size_one_stub (struct bfd_hash_entry *gen_entry,
07d6d2b8 587 void *in_arg ATTRIBUTE_UNUSED)
60bcf0fa 588{
3a65329d 589 struct elf32_m68hc11_stub_hash_entry *stub_entry;
60bcf0fa 590
3a65329d
SC
591 /* Massage our args to the form they really have. */
592 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
593
eea6121a 594 stub_entry->stub_sec->size += 7;
0a1b45a2 595 return true;
60bcf0fa
NC
596}
597
3a65329d
SC
598/* Create a 68HC12 ELF linker hash table. */
599
600static struct bfd_link_hash_table *
0a6a3ebe 601m68hc12_elf_bfd_link_hash_table_create (bfd *abfd)
9b701e44 602{
3a65329d 603 struct m68hc11_elf_link_hash_table *ret;
56780f18 604
3a65329d
SC
605 ret = m68hc11_elf_hash_table_create (abfd);
606 if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
607 return NULL;
9b701e44 608
3a65329d
SC
609 ret->size_one_stub = m68hc12_elf_size_one_stub;
610 ret->build_one_stub = m68hc12_elf_build_one_stub;
9b701e44 611
3a65329d
SC
612 return &ret->root.root;
613}
96405e3c 614\f
0a1b45a2 615static bool
0a6a3ebe 616m68hc12_elf_set_mach_from_flags (bfd *abfd)
bc7c6a90
SC
617{
618 flagword flags = elf_elfheader (abfd)->e_flags;
619
620 switch (flags & EF_M68HC11_MACH_MASK)
621 {
622 case EF_M68HC12_MACH:
623 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12, bfd_mach_m6812);
624 break;
625 case EF_M68HCS12_MACH:
626 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12, bfd_mach_m6812s);
627 break;
628 case EF_M68HC11_GENERIC:
629 bfd_default_set_arch_mach (abfd, bfd_arch_m68hc12,
07d6d2b8 630 bfd_mach_m6812_default);
bc7c6a90
SC
631 break;
632 default:
0a1b45a2 633 return false;
bc7c6a90 634 }
0a1b45a2 635 return true;
bc7c6a90
SC
636}
637
3f533aa9
SC
638/* Specific sections:
639 - The .page0 is a data section that is mapped in [0x0000..0x00FF].
640 Page0 accesses are faster on the M68HC12.
641 - The .vectors is the section that represents the interrupt
642 vectors. */
b35d266b 643static const struct bfd_elf_special_section elf32_m68hc12_special_sections[] =
3f533aa9 644{
0112cd26
NC
645 { STRING_COMMA_LEN (".eeprom"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
646 { STRING_COMMA_LEN (".page0"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
647 { STRING_COMMA_LEN (".softregs"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
648 { STRING_COMMA_LEN (".vectors"), 0, SHT_PROGBITS, SHF_ALLOC },
07d6d2b8 649 { NULL, 0, 0, 0, 0 }
7f4d3958 650};
3f533aa9 651\f
60bcf0fa 652#define ELF_ARCH bfd_arch_m68hc12
ae95ffa6 653#define ELF_TARGET_ID M68HC11_ELF_DATA
60bcf0fa
NC
654#define ELF_MACHINE_CODE EM_68HC12
655#define ELF_MAXPAGESIZE 0x1000
656
07d6d2b8 657#define TARGET_BIG_SYM m68hc12_elf32_vec
60bcf0fa
NC
658#define TARGET_BIG_NAME "elf32-m68hc12"
659
f3185997 660#define elf_info_to_howto NULL
60bcf0fa 661#define elf_info_to_howto_rel m68hc11_info_to_howto_rel
3a65329d
SC
662#define elf_backend_check_relocs elf32_m68hc11_check_relocs
663#define elf_backend_relocate_section elf32_m68hc11_relocate_section
bc7c6a90 664#define elf_backend_object_p m68hc12_elf_set_mach_from_flags
3a65329d 665#define elf_backend_can_gc_sections 1
29ef7005 666#define elf_backend_special_sections elf32_m68hc12_special_sections
ed7e9d0b 667#define elf_backend_init_file_header elf32_m68hc11_init_file_header
3a65329d 668#define elf_backend_add_symbol_hook elf32_m68hc11_add_symbol_hook
5efbbc43 669#define elf_backend_merge_symbol_attribute elf32_m68hc11_merge_symbol_attribute
3a65329d
SC
670
671#define bfd_elf32_bfd_link_hash_table_create \
07d6d2b8 672 m68hc12_elf_bfd_link_hash_table_create
96405e3c 673#define bfd_elf32_bfd_merge_private_bfd_data \
3a65329d
SC
674 _bfd_m68hc11_elf_merge_private_bfd_data
675#define bfd_elf32_bfd_set_private_flags _bfd_m68hc11_elf_set_private_flags
96405e3c 676#define bfd_elf32_bfd_print_private_bfd_data \
3a65329d 677 _bfd_m68hc11_elf_print_private_bfd_data
96405e3c 678
60bcf0fa 679#include "elf32-target.h"