1 /* Freescale S12Z-specific support for 32-bit ELF
2 Copyright (C) 1999-2018 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 0x00000000, /* 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 */
187 HOWTO (R_S12Z_EXT32, /* type */
189 2, /* size (0 = byte, 1 = short, 2 = long) */
191 FALSE, /* pc_relative */
193 complain_overflow_bitfield, /* complain_on_overflow */
194 bfd_elf_generic_reloc, /* special_function */
195 "R_S12Z_EXT32", /* name */
196 FALSE, /* partial_inplace */
197 0x00000000, /* src_mask */
198 0xffffffff, /* dst_mask */
199 FALSE), /* pcrel_offset */
202 /* Map BFD reloc types to S12Z ELF reloc types. */
204 struct s12z_reloc_map
206 bfd_reloc_code_real_type bfd_reloc_val;
207 unsigned char elf_reloc_val;
210 static const struct s12z_reloc_map s12z_reloc_map[] =
212 /* bfd reloc val */ /* elf reloc val */
213 {BFD_RELOC_NONE, R_S12Z_NONE},
214 {BFD_RELOC_32, R_S12Z_EXT32},
215 {BFD_RELOC_24, R_S12Z_EXT24},
216 {BFD_RELOC_16_PCREL, R_S12Z_PCREL_7_15}
219 static reloc_howto_type *
220 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
221 bfd_reloc_code_real_type code)
226 i < sizeof (s12z_reloc_map) / sizeof (struct s12z_reloc_map);
229 if (s12z_reloc_map[i].bfd_reloc_val == code)
231 return &elf_s12z_howto_table[s12z_reloc_map[i].elf_reloc_val];
235 printf ("%s:%d Not found type %d\n", __FILE__, __LINE__, code);
240 static reloc_howto_type *
241 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
247 i < (sizeof (elf_s12z_howto_table)
248 / sizeof (elf_s12z_howto_table[0]));
250 if (elf_s12z_howto_table[i].name != NULL
251 && strcasecmp (elf_s12z_howto_table[i].name, r_name) == 0)
252 return &elf_s12z_howto_table[i];
257 /* Set the howto pointer for an S12Z ELF reloc. */
260 s12z_info_to_howto_rel (bfd *abfd,
261 arelent *cache_ptr, Elf_Internal_Rela *dst)
263 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
265 if (r_type >= (unsigned int) R_S12Z_max)
267 /* xgettext:c-format */
268 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
270 bfd_set_error (bfd_error_bad_value);
274 cache_ptr->howto = &elf_s12z_howto_table[r_type];
279 s12z_elf_set_mach_from_flags (bfd *abfd)
281 bfd_default_set_arch_mach (abfd, bfd_arch_s12z, 0);
286 #define ELF_ARCH bfd_arch_s12z
287 #define ELF_MACHINE_CODE EM_S12Z
288 #define ELF_MAXPAGESIZE 0x1000
290 #define TARGET_BIG_SYM s12z_elf32_vec
291 #define TARGET_BIG_NAME "elf32-s12z"
293 #define elf_info_to_howto NULL
294 #define elf_info_to_howto_rel s12z_info_to_howto_rel
295 #define elf_backend_object_p s12z_elf_set_mach_from_flags
296 #define elf_backend_final_write_processing NULL
297 #define elf_backend_can_gc_sections 1
299 #include "elf32-target.h"