1 /* Freescale S12Z-specific support for 32-bit ELF
2 Copyright (C) 1999-2019 Free Software Foundation, Inc.
3 (Heavily copied from the D10V port by Martin Hunt (hunt@cygnus.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. */
30 /* Relocation functions. */
31 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
32 (bfd *, bfd_reloc_code_real_type);
33 static bfd_boolean s12z_info_to_howto_rel
34 (bfd *, arelent *, Elf_Internal_Rela *);
36 static bfd_reloc_status_type
37 opru18_reloc (bfd *abfd, arelent *reloc_entry, struct bfd_symbol *symbol,
38 void *data, asection *input_section ATTRIBUTE_UNUSED,
39 bfd *output ATTRIBUTE_UNUSED, char **msg ATTRIBUTE_UNUSED)
41 /* This reloc is used for 18 bit General Operand Addressing Postbyte in the
42 INST opru18 form. This is an 18 bit reloc, but the most significant bit
43 is shifted one place to the left of where it would normally be. See
44 Appendix A.4 of the S12Z reference manual. */
46 bfd_size_type octets = reloc_entry->address * bfd_octets_per_byte (abfd);
47 bfd_vma result = bfd_get_24 (abfd, (unsigned char *) data + octets);
48 bfd_vma val = bfd_asymbol_value (symbol);
50 /* Keep the wanted bits and discard the rest. */
53 val += symbol->section->output_section->vma;
54 val += symbol->section->output_offset;
56 /* The lowest 17 bits are copied verbatim. */
57 result |= val & 0x1FFFF;
59 /* The 18th bit is put into the 19th position. */
60 result |= (val & 0x020000) << 1;
62 bfd_put_24 (abfd, result, (unsigned char *) data + octets);
68 static bfd_reloc_status_type
69 shift_addend_reloc (bfd *abfd, arelent *reloc_entry, struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
70 void *data ATTRIBUTE_UNUSED, asection *input_section ATTRIBUTE_UNUSED,
71 bfd *output ATTRIBUTE_UNUSED, char **msg ATTRIBUTE_UNUSED)
73 /* This is a really peculiar reloc, which is done for compatibility
74 with the Freescale toolchain.
76 That toolchain appears to (ab)use the lowest 15 bits of the addend for
77 the purpose of holding flags. The purpose of these flags are unknown.
78 So in this function, when writing the bfd we left shift the addend by
79 15, and when reading we right shift it by 15 (discarding the lower bits).
81 This allows the linker to work with object files generated by Freescale,
84 if (abfd->is_linker_input)
85 reloc_entry->addend >>= 15;
87 reloc_entry->addend <<= 15;
89 return bfd_reloc_continue;
94 static reloc_howto_type elf_s12z_howto_table[] =
96 /* This reloc does nothing. */
97 HOWTO (R_S12Z_NONE, /* type */
99 3, /* size (0 = byte, 1 = short, 2 = long) */
101 FALSE, /* pc_relative */
103 complain_overflow_dont,/* complain_on_overflow */
104 bfd_elf_generic_reloc, /* special_function */
105 "R_S12Z_NONE", /* name */
106 FALSE, /* partial_inplace */
109 FALSE), /* pcrel_offset */
111 /* A 24 bit absolute relocation emitted by the OPR mode operands */
112 HOWTO (R_S12Z_OPR, /* type */
114 5, /* size (0 = byte, 1 = short, 2 = long) */
116 FALSE, /* pc_relative */
118 complain_overflow_bitfield, /* complain_on_overflow */
120 "R_S12Z_OPR", /* name */
121 FALSE, /* partial_inplace */
122 0x00ffffff, /* src_mask */
123 0x00ffffff, /* dst_mask */
124 FALSE), /* pcrel_offset */
126 /* The purpose of this reloc is not known */
127 HOWTO (R_S12Z_UKNWN_2, /* type */
129 3, /* size (0 = byte, 1 = short, 2 = long) */
131 FALSE, /* pc_relative */
133 complain_overflow_dont,/* complain_on_overflow */
134 bfd_elf_generic_reloc, /* special_function */
135 "R_S12Z_UKNWN_2", /* name */
136 FALSE, /* partial_inplace */
139 FALSE), /* pcrel_offset */
141 /* A 15 bit PC-rel relocation */
142 HOWTO (R_S12Z_PCREL_7_15, /* type */
144 1, /* size (0 = byte, 1 = short, 2 = long) */
146 TRUE, /* pc_relative */
148 complain_overflow_bitfield, /* complain_on_overflow */
150 "R_S12Z_PCREL_7_15", /* name */
151 FALSE, /* partial_inplace */
153 0x007fff, /* dst_mask */
154 TRUE), /* pcrel_offset */
156 /* A 24 bit absolute relocation emitted by EXT24 mode operands */
157 HOWTO (R_S12Z_EXT24, /* type */
159 5, /* size (0 = byte, 1 = short, 2 = long) */
161 FALSE, /* pc_relative */
163 complain_overflow_bitfield, /* complain_on_overflow */
164 bfd_elf_generic_reloc, /* special_function */
165 "R_S12Z_EXT24", /* name */
166 FALSE, /* partial_inplace */
167 0x00000000, /* src_mask */
168 0x00ffffff, /* dst_mask */
169 FALSE), /* pcrel_offset */
171 /* An 18 bit absolute relocation */
172 HOWTO (R_S12Z_EXT18, /* type */
174 5, /* size (0 = byte, 1 = short, 2 = long) */
176 FALSE, /* pc_relative */
178 complain_overflow_bitfield, /* complain_on_overflow */
179 opru18_reloc, /* special_function */
180 "R_S12Z_EXT18", /* name */
181 FALSE, /* partial_inplace */
182 0x00000000, /* src_mask */
183 0x0005ffff, /* dst_mask */
184 FALSE), /* pcrel_offset */
186 /* A 32 bit absolute relocation. This kind of relocation is
187 schizophrenic - Although they appear in sections named .rela.debug.*
188 in some sections they behave as RELA relocs, but in others they have
189 an added of zero and behave as REL.
191 It is not recommended that new code emits this reloc. It is here
192 only to support existing elf files generated by third party
195 HOWTO (R_S12Z_CW32, /* type */
197 2, /* size (0 = byte, 1 = short, 2 = long) */
199 FALSE, /* pc_relative */
201 complain_overflow_bitfield, /* complain_on_overflow */
202 bfd_elf_generic_reloc, /* special_function */
203 "R_S12Z_CW32", /* name */
204 FALSE, /* partial_inplace */
205 0xffffffff, /* src_mask */
206 0xffffffff, /* dst_mask */
207 FALSE), /* pcrel_offset */
209 /* A 32 bit absolute relocation */
210 HOWTO (R_S12Z_EXT32, /* type */
212 2, /* size (0 = byte, 1 = short, 2 = long) */
214 FALSE, /* pc_relative */
216 complain_overflow_bitfield, /* complain_on_overflow */
217 bfd_elf_generic_reloc, /* special_function */
218 "R_S12Z_EXT32", /* name */
219 FALSE, /* partial_inplace */
220 0x00000000, /* src_mask */
221 0xffffffff, /* dst_mask */
222 FALSE), /* pcrel_offset */
225 /* Map BFD reloc types to S12Z ELF reloc types. */
227 struct s12z_reloc_map
229 bfd_reloc_code_real_type bfd_reloc_val;
230 unsigned char elf_reloc_val;
233 static const struct s12z_reloc_map s12z_reloc_map[] =
235 /* bfd reloc val */ /* elf reloc val */
236 {BFD_RELOC_NONE, R_S12Z_NONE},
237 {BFD_RELOC_32, R_S12Z_EXT32},
238 {BFD_RELOC_24, R_S12Z_EXT24},
239 {BFD_RELOC_16_PCREL, R_S12Z_PCREL_7_15},
240 {BFD_RELOC_S12Z_OPR, R_S12Z_OPR}
243 static reloc_howto_type *
244 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
245 bfd_reloc_code_real_type code)
250 i < sizeof (s12z_reloc_map) / sizeof (struct s12z_reloc_map);
253 if (s12z_reloc_map[i].bfd_reloc_val == code)
255 return &elf_s12z_howto_table[s12z_reloc_map[i].elf_reloc_val];
259 printf ("%s:%d Not found type %d\n", __FILE__, __LINE__, code);
264 static reloc_howto_type *
265 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
271 i < (sizeof (elf_s12z_howto_table)
272 / sizeof (elf_s12z_howto_table[0]));
274 if (elf_s12z_howto_table[i].name != NULL
275 && strcasecmp (elf_s12z_howto_table[i].name, r_name) == 0)
276 return &elf_s12z_howto_table[i];
281 /* Set the howto pointer for an S12Z ELF reloc. */
284 s12z_info_to_howto_rel (bfd *abfd,
285 arelent *cache_ptr, Elf_Internal_Rela *dst)
287 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
289 if (r_type >= (unsigned int) R_S12Z_max)
291 /* xgettext:c-format */
292 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
294 bfd_set_error (bfd_error_bad_value);
298 cache_ptr->howto = &elf_s12z_howto_table[r_type];
303 s12z_elf_set_mach_from_flags (bfd *abfd)
305 bfd_default_set_arch_mach (abfd, bfd_arch_s12z, 0);
310 #define ELF_ARCH bfd_arch_s12z
311 #define ELF_MACHINE_CODE EM_S12Z
312 #define ELF_MAXPAGESIZE 0x1000
314 #define TARGET_BIG_SYM s12z_elf32_vec
315 #define TARGET_BIG_NAME "elf32-s12z"
317 #define elf_info_to_howto NULL
318 #define elf_info_to_howto_rel s12z_info_to_howto_rel
319 #define elf_backend_object_p s12z_elf_set_mach_from_flags
320 #define elf_backend_final_write_processing NULL
321 #define elf_backend_can_gc_sections 1
323 #include "elf32-target.h"