1 /* Freescale XGATE-specific support for 32-bit ELF
2 Copyright (C) 2010-2023 Free Software Foundation, Inc.
3 Contributed by Sean Keys(skeys@ipdatasys.com)
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
27 #include "elf/xgate.h"
28 #include "opcode/xgate.h"
29 #include "libiberty.h"
31 /* Forward declarations. */
32 static bfd_reloc_status_type xgate_elf_ignore_reloc
33 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
34 static bfd_reloc_status_type xgate_elf_special_reloc
35 (bfd
*, arelent
*, asymbol
*, void *, asection
*, bfd
*, char **);
37 /* Use REL instead of RELA to save space */
40 static reloc_howto_type elf_xgate_howto_table
[] =
42 /* This reloc does nothing. */
43 HOWTO (R_XGATE_NONE
, /* type */
47 false, /* pc_relative */
49 complain_overflow_dont
,/* complain_on_overflow */
50 bfd_elf_generic_reloc
, /* special_function */
51 "R_XGATE_NONE", /* name */
52 false, /* partial_inplace */
55 false), /* pcrel_offset */
57 /* A 8 bit absolute relocation. */
58 HOWTO (R_XGATE_8
, /* type */
62 false, /* pc_relative */
64 complain_overflow_bitfield
, /* complain_on_overflow */
65 bfd_elf_generic_reloc
, /* special_function */
66 "R_XGATE_8", /* name */
67 false, /* partial_inplace */
68 0x00ff, /* src_mask */
69 0x00ff, /* dst_mask */
70 false), /* pcrel_offset */
72 /* A 8 bit PC-rel relocation. */
73 HOWTO (R_XGATE_PCREL_8
, /* type */
77 true, /* pc_relative */
79 complain_overflow_bitfield
, /* complain_on_overflow */
80 bfd_elf_generic_reloc
, /* special_function */
81 "R_XGATE_PCREL_8", /* name */
82 false, /* partial_inplace */
83 0x00ff, /* src_mask */
84 0x00ff, /* dst_mask */
85 true), /* pcrel_offset */
87 /* A 16 bit absolute relocation. */
88 HOWTO (R_XGATE_16
, /* type */
92 false, /* pc_relative */
94 complain_overflow_dont
/*bitfield */, /* complain_on_overflow */
95 bfd_elf_generic_reloc
, /* special_function */
96 "R_XGATE_16", /* name */
97 false, /* partial_inplace */
98 0xffff, /* src_mask */
99 0xffff, /* dst_mask */
100 false), /* pcrel_offset */
102 /* A 32 bit absolute relocation. This one is never used for the
103 code relocation. It's used by gas for -gstabs generation. */
104 HOWTO (R_XGATE_32
, /* type */
108 false, /* pc_relative */
110 complain_overflow_bitfield
, /* complain_on_overflow */
111 bfd_elf_generic_reloc
, /* special_function */
112 "R_XGATE_32", /* name */
113 false, /* partial_inplace */
114 0xffffffff, /* src_mask */
115 0xffffffff, /* dst_mask */
116 false), /* pcrel_offset */
118 /* A 16 bit PC-rel relocation. */
119 HOWTO (R_XGATE_PCREL_16
, /* type */
123 true, /* pc_relative */
125 complain_overflow_dont
, /* complain_on_overflow */
126 bfd_elf_generic_reloc
, /* special_function */
127 "R_XGATE_PCREL_16", /* name */
128 false, /* partial_inplace */
129 0xffff, /* src_mask */
130 0xffff, /* dst_mask */
131 true), /* pcrel_offset */
133 /* GNU extension to record C++ vtable hierarchy. */
134 HOWTO (R_XGATE_GNU_VTINHERIT
, /* type */
138 false, /* pc_relative */
140 complain_overflow_dont
, /* complain_on_overflow */
141 NULL
, /* special_function */
142 "R_XGATE_GNU_VTINHERIT", /* name */
143 false, /* partial_inplace */
146 false), /* pcrel_offset */
148 /* GNU extension to record C++ vtable member usage. */
149 HOWTO (R_XGATE_GNU_VTENTRY
, /* type */
153 false, /* pc_relative */
155 complain_overflow_dont
, /* complain_on_overflow */
156 _bfd_elf_rel_vtable_reloc_fn
, /* special_function */
157 "R_XGATE_GNU_VTENTRY", /* name */
158 false, /* partial_inplace */
161 false), /* pcrel_offset */
163 /* A 24 bit relocation. */
164 HOWTO (R_XGATE_24
, /* type */
168 false, /* pc_relative */
170 complain_overflow_dont
, /* complain_on_overflow */
171 bfd_elf_generic_reloc
, /* special_function */
172 "R_XGATE_IMM8_LO", /* name */
173 false, /* partial_inplace */
174 0x00ff, /* src_mask */
175 0x00ff, /* dst_mask */
176 false), /* pcrel_offset */
178 /* A 16-bit low relocation. */
179 HOWTO (R_XGATE_LO16
, /* type */
183 false, /* pc_relative */
185 complain_overflow_dont
, /* complain_on_overflow */
186 bfd_elf_generic_reloc
, /* special_function */
187 "R_XGATE_IMM8_HI", /* name */
188 false, /* partial_inplace */
189 0x00ff, /* src_mask */
190 0x00ff, /* dst_mask */
191 false), /* pcrel_offset */
193 /* A page relocation. */
194 HOWTO (R_XGATE_GPAGE
, /* type */
198 false, /* pc_relative */
200 complain_overflow_dont
, /* complain_on_overflow */
201 xgate_elf_special_reloc
,/* special_function */
202 "R_XGATE_GPAGE", /* name */
203 false, /* partial_inplace */
204 0x00ff, /* src_mask */
205 0x00ff, /* dst_mask */
206 false), /* pcrel_offset */
208 /* A 9 bit absolute relocation. */
209 HOWTO (R_XGATE_PCREL_9
, /* type */
213 true, /* pc_relative */
215 complain_overflow_bitfield
, /* complain_on_overflow */
216 bfd_elf_generic_reloc
, /* special_function */
217 "R_XGATE_PCREL_9", /* name */
218 false, /* partial_inplace */
219 0xffff, /* src_mask */
220 0xffff, /* dst_mask */
221 true), /* pcrel_offset */
223 /* A 8 bit absolute relocation (upper address). */
224 HOWTO (R_XGATE_PCREL_10
, /* type */
228 true, /* pc_relative */
230 complain_overflow_dont
, /* complain_on_overflow */
231 bfd_elf_generic_reloc
, /* special_function */
232 "R_XGATE_PCREL_10", /* name */
233 false, /* partial_inplace */
234 0x00ff, /* src_mask */
235 0x00ff, /* dst_mask */
236 true), /* pcrel_offset */
238 /* A 8 bit absolute relocation. */
239 HOWTO (R_XGATE_IMM8_LO
, /* type */
243 false, /* pc_relative */
245 complain_overflow_dont
, /* complain_on_overflow */
246 bfd_elf_generic_reloc
, /* special_function */
247 "R_XGATE_IMM8_LO", /* name */
248 false, /* partial_inplace */
249 0xffff, /* src_mask */
250 0xffff, /* dst_mask */
251 false), /* pcrel_offset */
253 /* A 16 bit absolute relocation (upper address). */
254 HOWTO (R_XGATE_IMM8_HI
, /* type */
258 false, /* pc_relative */
260 complain_overflow_dont
, /* complain_on_overflow */
261 bfd_elf_generic_reloc
, /* special_function */
262 "R_XGATE_IMM8_HI", /* name */
263 false, /* partial_inplace */
264 0x00ff, /* src_mask */
265 0x00ff, /* dst_mask */
266 false), /* pcrel_offset */
268 /* A 3 bit absolute relocation. */
269 HOWTO (R_XGATE_IMM3
, /* type */
273 false, /* pc_relative */
275 complain_overflow_dont
, /* complain_on_overflow */
276 bfd_elf_generic_reloc
, /* special_function */
277 "R_XGATE_IMM3", /* name */
278 false, /* partial_inplace */
279 0x00ff, /* src_mask */
280 0x00ff, /* dst_mask */
281 false), /* pcrel_offset */
283 /* A 4 bit absolute relocation. */
284 HOWTO (R_XGATE_IMM4
, /* type */
288 false, /* pc_relative */
290 complain_overflow_dont
, /* complain_on_overflow */
291 bfd_elf_generic_reloc
, /* special_function */
292 "R_XGATE_IMM4", /* name */
293 false, /* partial_inplace */
294 0x00ff, /* src_mask */
295 0x00ff, /* dst_mask */
296 false), /* pcrel_offset */
298 /* A 5 bit absolute relocation. */
299 HOWTO (R_XGATE_IMM5
, /* type */
303 false, /* pc_relative */
305 complain_overflow_dont
, /* complain_on_overflow */
306 bfd_elf_generic_reloc
, /* special_function */
307 "R_XGATE_IMM5", /* name */
308 false, /* partial_inplace */
309 0x00ff, /* src_mask */
310 0x00ff, /* dst_mask */
311 false), /* pcrel_offset */
313 /* Mark beginning of a jump instruction (any form). */
314 HOWTO (R_XGATE_RL_JUMP
, /* type */
318 false, /* pc_relative */
320 complain_overflow_dont
, /* complain_on_overflow */
321 xgate_elf_ignore_reloc
, /* special_function */
322 "R_XGATE_RL_JUMP", /* name */
323 true, /* partial_inplace */
326 true), /* pcrel_offset */
328 /* Mark beginning of Gcc relaxation group instruction. */
329 HOWTO (R_XGATE_RL_GROUP
, /* type */
333 false, /* pc_relative */
335 complain_overflow_dont
, /* complain_on_overflow */
336 xgate_elf_ignore_reloc
, /* special_function */
337 "R_XGATE_RL_GROUP", /* name */
338 true, /* partial_inplace */
341 true), /* pcrel_offset */
344 /* Map BFD reloc types to XGATE ELF reloc types. */
346 struct xgate_reloc_map
348 bfd_reloc_code_real_type bfd_reloc_val
;
349 unsigned char elf_reloc_val
;
352 static const struct xgate_reloc_map xgate_reloc_map
[] =
354 {BFD_RELOC_NONE
, R_XGATE_NONE
},
355 {BFD_RELOC_8
, R_XGATE_8
},
356 {BFD_RELOC_8_PCREL
, R_XGATE_PCREL_8
},
357 {BFD_RELOC_16_PCREL
, R_XGATE_PCREL_16
},
358 {BFD_RELOC_16
, R_XGATE_16
},
359 {BFD_RELOC_32
, R_XGATE_32
},
361 {BFD_RELOC_VTABLE_INHERIT
, R_XGATE_GNU_VTINHERIT
},
362 {BFD_RELOC_VTABLE_ENTRY
, R_XGATE_GNU_VTENTRY
},
364 {BFD_RELOC_XGATE_LO16
, R_XGATE_LO16
},
365 {BFD_RELOC_XGATE_GPAGE
, R_XGATE_GPAGE
},
366 {BFD_RELOC_XGATE_24
, R_XGATE_24
},
367 {BFD_RELOC_XGATE_PCREL_9
, R_XGATE_PCREL_9
},
368 {BFD_RELOC_XGATE_PCREL_10
, R_XGATE_PCREL_10
},
369 {BFD_RELOC_XGATE_IMM8_LO
, R_XGATE_IMM8_LO
},
370 {BFD_RELOC_XGATE_IMM8_HI
, R_XGATE_IMM8_HI
},
371 {BFD_RELOC_XGATE_IMM3
, R_XGATE_IMM3
},
372 {BFD_RELOC_XGATE_IMM4
, R_XGATE_IMM4
},
373 {BFD_RELOC_XGATE_IMM5
, R_XGATE_IMM5
},
375 {BFD_RELOC_XGATE_RL_JUMP
, R_XGATE_RL_JUMP
},
376 {BFD_RELOC_XGATE_RL_GROUP
, R_XGATE_RL_GROUP
},
379 static reloc_howto_type
*
380 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
381 bfd_reloc_code_real_type code
)
385 for (i
= 0; i
< ARRAY_SIZE (xgate_reloc_map
); i
++)
386 if (xgate_reloc_map
[i
].bfd_reloc_val
== code
)
387 return &elf_xgate_howto_table
[xgate_reloc_map
[i
].elf_reloc_val
];
392 static reloc_howto_type
*
393 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
, const char *r_name
)
397 for (i
= 0; i
< ARRAY_SIZE (elf_xgate_howto_table
); i
++)
398 if (elf_xgate_howto_table
[i
].name
!= NULL
399 && strcasecmp (elf_xgate_howto_table
[i
].name
, r_name
) == 0)
400 return &elf_xgate_howto_table
[i
];
405 /* Set the howto pointer for an XGATE ELF reloc. */
408 xgate_info_to_howto_rel (bfd
*abfd
,
410 Elf_Internal_Rela
*dst
)
414 r_type
= ELF32_R_TYPE (dst
->r_info
);
415 if (r_type
>= (unsigned int) R_XGATE_max
)
417 /* xgettext:c-format */
418 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
420 bfd_set_error (bfd_error_bad_value
);
423 cache_ptr
->howto
= &elf_xgate_howto_table
[r_type
];
427 /* Specific sections:
428 - The .page0 is a data section that is mapped in [0x0000..0x00FF].
429 Page0 accesses are faster on the M68HC12.
430 - The .vectors is the section that represents the interrupt
432 - The .xgate section is starts in 0xE08800 or as xgate sees it 0x0800. */
433 static const struct bfd_elf_special_section elf32_xgate_special_sections
[] =
435 { STRING_COMMA_LEN (".eeprom"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
436 { STRING_COMMA_LEN (".page0"), 0, SHT_PROGBITS
, SHF_ALLOC
+ SHF_WRITE
},
437 { STRING_COMMA_LEN (".softregs"), 0, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
438 { STRING_COMMA_LEN (".vectors"), 0, SHT_PROGBITS
, SHF_ALLOC
},
439 /*{ STRING_COMMA_LEN (".xgate"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
440 TODO finish this implementation */
444 /* Hook called when reading symbols. */
447 elf32_xgate_backend_symbol_processing (bfd
*abfd ATTRIBUTE_UNUSED
,
450 /* Mark xgate symbols. */
451 ((elf_symbol_type
*) sym
)->internal_elf_sym
.st_target_internal
= 1;
454 /* This function is used for relocs which are only used for relaxing,
455 which the linker should otherwise ignore. */
457 static bfd_reloc_status_type
458 xgate_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
459 arelent
*reloc_entry
,
460 asymbol
*symbol ATTRIBUTE_UNUSED
,
461 void *data ATTRIBUTE_UNUSED
,
462 asection
*input_section
,
464 char **error_message ATTRIBUTE_UNUSED
)
466 if (output_bfd
!= NULL
)
467 reloc_entry
->address
+= input_section
->output_offset
;
471 static bfd_reloc_status_type
472 xgate_elf_special_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
473 arelent
*reloc_entry ATTRIBUTE_UNUSED
,
474 asymbol
*symbol ATTRIBUTE_UNUSED
,
475 void *data ATTRIBUTE_UNUSED
,
476 asection
*input_section ATTRIBUTE_UNUSED
,
477 bfd
*output_bfd ATTRIBUTE_UNUSED
,
478 char **error_message ATTRIBUTE_UNUSED
)
484 _bfd_xgate_elf_print_private_bfd_data (bfd
*abfd
, void *ptr
)
486 FILE *file
= (FILE *) ptr
;
488 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
490 /* Print normal ELF private data. */
491 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
493 /* xgettext:c-format */
494 fprintf (file
, _("private flags = %lx:"), elf_elfheader (abfd
)->e_flags
);
496 if (elf_elfheader (abfd
)->e_flags
& E_XGATE_I32
)
497 fprintf (file
, _("[abi=32-bit int, "));
499 fprintf (file
, _("[abi=16-bit int, "));
501 if (elf_elfheader (abfd
)->e_flags
& E_XGATE_F64
)
502 fprintf (file
, _("64-bit double, "));
504 fprintf (file
, _("32-bit double, "));
505 if (elf_elfheader (abfd
)->e_flags
& EF_XGATE_MACH
)
506 fprintf (file
, _("cpu=XGATE]"));
508 fprintf (file
, _("error reading cpu type from elf private data"));
514 #define ELF_ARCH bfd_arch_xgate
515 #define ELF_MACHINE_CODE EM_XGATE
517 #define ELF_MAXPAGESIZE 0x1000
519 #define TARGET_BIG_SYM xgate_elf32_vec
520 #define TARGET_BIG_NAME "elf32-xgate"
522 #define elf_info_to_howto_rel xgate_info_to_howto_rel
523 #define elf_backend_special_sections elf32_xgate_special_sections
524 #define elf_backend_symbol_processing elf32_xgate_backend_symbol_processing
525 #define bfd_elf32_bfd_print_private_bfd_data _bfd_xgate_elf_print_private_bfd_data
527 #include "elf32-target.h"