8b3099c26ee3f98846404cc13de70fa5abe60160
[deliverable/binutils-gdb.git] / bfd / elf32-s12z.c
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))
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27
28 #include "elf/s12z.h"
29
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 *);
35
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)
40 {
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. */
45
46 bfd_size_type octets = (reloc_entry->address
47 * bfd_octets_per_byte (abfd, NULL));
48 bfd_vma result = bfd_get_24 (abfd, (unsigned char *) data + octets);
49 bfd_vma val = bfd_asymbol_value (symbol);
50
51 /* Keep the wanted bits and discard the rest. */
52 result &= 0xFA0000;
53
54 val += symbol->section->output_section->vma;
55 val += symbol->section->output_offset;
56
57 /* The lowest 17 bits are copied verbatim. */
58 result |= val & 0x1FFFF;
59
60 /* The 18th bit is put into the 19th position. */
61 result |= (val & 0x020000) << 1;
62
63 bfd_put_24 (abfd, result, (unsigned char *) data + octets);
64
65 return bfd_reloc_ok;
66 }
67
68
69 static bfd_reloc_status_type
70 shift_addend_reloc (bfd *abfd, arelent *reloc_entry, struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
71 void *data ATTRIBUTE_UNUSED, asection *input_section ATTRIBUTE_UNUSED,
72 bfd *output ATTRIBUTE_UNUSED, char **msg ATTRIBUTE_UNUSED)
73 {
74 /* This is a really peculiar reloc, which is done for compatibility
75 with the Freescale toolchain.
76
77 That toolchain appears to (ab)use the lowest 15 bits of the addend for
78 the purpose of holding flags. The purpose of these flags are unknown.
79 So in this function, when writing the bfd we left shift the addend by
80 15, and when reading we right shift it by 15 (discarding the lower bits).
81
82 This allows the linker to work with object files generated by Freescale,
83 as well as by Gas. */
84
85 if (abfd->is_linker_input)
86 reloc_entry->addend >>= 15;
87 else
88 reloc_entry->addend <<= 15;
89
90 return bfd_reloc_continue;
91 }
92
93 #define USE_REL 0
94
95 static reloc_howto_type elf_s12z_howto_table[] =
96 {
97 /* This reloc does nothing. */
98 HOWTO (R_S12Z_NONE, /* type */
99 0, /* rightshift */
100 3, /* size (0 = byte, 1 = short, 2 = long) */
101 0, /* bitsize */
102 FALSE, /* pc_relative */
103 0, /* bitpos */
104 complain_overflow_dont,/* complain_on_overflow */
105 bfd_elf_generic_reloc, /* special_function */
106 "R_S12Z_NONE", /* name */
107 FALSE, /* partial_inplace */
108 0, /* src_mask */
109 0, /* dst_mask */
110 FALSE), /* pcrel_offset */
111
112 /* A 24 bit absolute relocation emitted by the OPR mode operands */
113 HOWTO (R_S12Z_OPR, /* type */
114 0, /* rightshift */
115 5, /* size (0 = byte, 1 = short, 2 = long) */
116 24, /* bitsize */
117 FALSE, /* pc_relative */
118 0, /* bitpos */
119 complain_overflow_bitfield, /* complain_on_overflow */
120 shift_addend_reloc,
121 "R_S12Z_OPR", /* name */
122 FALSE, /* partial_inplace */
123 0x00ffffff, /* src_mask */
124 0x00ffffff, /* dst_mask */
125 FALSE), /* pcrel_offset */
126
127 /* The purpose of this reloc is not known */
128 HOWTO (R_S12Z_UKNWN_2, /* type */
129 0, /* rightshift */
130 3, /* size (0 = byte, 1 = short, 2 = long) */
131 0, /* bitsize */
132 FALSE, /* pc_relative */
133 0, /* bitpos */
134 complain_overflow_dont,/* complain_on_overflow */
135 bfd_elf_generic_reloc, /* special_function */
136 "R_S12Z_UKNWN_2", /* name */
137 FALSE, /* partial_inplace */
138 0, /* src_mask */
139 0, /* dst_mask */
140 FALSE), /* pcrel_offset */
141
142 /* A 15 bit PC-rel relocation */
143 HOWTO (R_S12Z_PCREL_7_15, /* type */
144 0, /* rightshift */
145 1, /* size (0 = byte, 1 = short, 2 = long) */
146 15, /* bitsize */
147 TRUE, /* pc_relative */
148 0, /* bitpos */
149 complain_overflow_bitfield, /* complain_on_overflow */
150 shift_addend_reloc,
151 "R_S12Z_PCREL_7_15", /* name */
152 FALSE, /* partial_inplace */
153 0x00, /* src_mask */
154 0x007fff, /* dst_mask */
155 TRUE), /* pcrel_offset */
156
157 /* A 24 bit absolute relocation emitted by EXT24 mode operands */
158 HOWTO (R_S12Z_EXT24, /* type */
159 0, /* rightshift */
160 5, /* size (0 = byte, 1 = short, 2 = long) */
161 24, /* bitsize */
162 FALSE, /* pc_relative */
163 0, /* bitpos */
164 complain_overflow_bitfield, /* complain_on_overflow */
165 bfd_elf_generic_reloc, /* special_function */
166 "R_S12Z_EXT24", /* name */
167 FALSE, /* partial_inplace */
168 0x00000000, /* src_mask */
169 0x00ffffff, /* dst_mask */
170 FALSE), /* pcrel_offset */
171
172 /* An 18 bit absolute relocation */
173 HOWTO (R_S12Z_EXT18, /* type */
174 0, /* rightshift */
175 5, /* size (0 = byte, 1 = short, 2 = long) */
176 18, /* bitsize */
177 FALSE, /* pc_relative */
178 0, /* bitpos */
179 complain_overflow_bitfield, /* complain_on_overflow */
180 opru18_reloc, /* special_function */
181 "R_S12Z_EXT18", /* name */
182 FALSE, /* partial_inplace */
183 0x00000000, /* src_mask */
184 0x0005ffff, /* dst_mask */
185 FALSE), /* pcrel_offset */
186
187 /* A 32 bit absolute relocation. This kind of relocation is
188 schizophrenic - Although they appear in sections named .rela.debug.*
189 in some sections they behave as RELA relocs, but in others they have
190 an added of zero and behave as REL.
191
192 It is not recommended that new code emits this reloc. It is here
193 only to support existing elf files generated by third party
194 applications. */
195
196 HOWTO (R_S12Z_CW32, /* type */
197 0, /* rightshift */
198 2, /* size (0 = byte, 1 = short, 2 = long) */
199 32, /* bitsize */
200 FALSE, /* pc_relative */
201 0, /* bitpos */
202 complain_overflow_bitfield, /* complain_on_overflow */
203 bfd_elf_generic_reloc, /* special_function */
204 "R_S12Z_CW32", /* name */
205 FALSE, /* partial_inplace */
206 0xffffffff, /* src_mask */
207 0xffffffff, /* dst_mask */
208 FALSE), /* pcrel_offset */
209
210 /* A 32 bit absolute relocation */
211 HOWTO (R_S12Z_EXT32, /* type */
212 0, /* rightshift */
213 2, /* size (0 = byte, 1 = short, 2 = long) */
214 32, /* bitsize */
215 FALSE, /* pc_relative */
216 0, /* bitpos */
217 complain_overflow_bitfield, /* complain_on_overflow */
218 bfd_elf_generic_reloc, /* special_function */
219 "R_S12Z_EXT32", /* name */
220 FALSE, /* partial_inplace */
221 0x00000000, /* src_mask */
222 0xffffffff, /* dst_mask */
223 FALSE), /* pcrel_offset */
224 };
225
226 /* Map BFD reloc types to S12Z ELF reloc types. */
227
228 struct s12z_reloc_map
229 {
230 bfd_reloc_code_real_type bfd_reloc_val;
231 unsigned char elf_reloc_val;
232 };
233
234 static const struct s12z_reloc_map s12z_reloc_map[] =
235 {
236 /* bfd reloc val */ /* elf reloc val */
237 {BFD_RELOC_NONE, R_S12Z_NONE},
238 {BFD_RELOC_32, R_S12Z_EXT32},
239 {BFD_RELOC_24, R_S12Z_EXT24},
240 {BFD_RELOC_16_PCREL, R_S12Z_PCREL_7_15},
241 {BFD_RELOC_S12Z_OPR, R_S12Z_OPR}
242 };
243
244 static reloc_howto_type *
245 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
246 bfd_reloc_code_real_type code)
247 {
248 unsigned int i;
249
250 for (i = 0;
251 i < sizeof (s12z_reloc_map) / sizeof (struct s12z_reloc_map);
252 i++)
253 {
254 if (s12z_reloc_map[i].bfd_reloc_val == code)
255 {
256 return &elf_s12z_howto_table[s12z_reloc_map[i].elf_reloc_val];
257 }
258 }
259
260 printf ("%s:%d Not found type %d\n", __FILE__, __LINE__, code);
261
262 return NULL;
263 }
264
265 static reloc_howto_type *
266 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
267 const char *r_name)
268 {
269 unsigned int i;
270
271 for (i = 0;
272 i < (sizeof (elf_s12z_howto_table)
273 / sizeof (elf_s12z_howto_table[0]));
274 i++)
275 if (elf_s12z_howto_table[i].name != NULL
276 && strcasecmp (elf_s12z_howto_table[i].name, r_name) == 0)
277 return &elf_s12z_howto_table[i];
278
279 return NULL;
280 }
281
282 /* Set the howto pointer for an S12Z ELF reloc. */
283
284 static bfd_boolean
285 s12z_info_to_howto_rel (bfd *abfd,
286 arelent *cache_ptr, Elf_Internal_Rela *dst)
287 {
288 unsigned int r_type = ELF32_R_TYPE (dst->r_info);
289
290 if (r_type >= (unsigned int) R_S12Z_max)
291 {
292 /* xgettext:c-format */
293 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
294 abfd, r_type);
295 bfd_set_error (bfd_error_bad_value);
296 return FALSE;
297 }
298
299 cache_ptr->howto = &elf_s12z_howto_table[r_type];
300 return TRUE;
301 }
302
303 static bfd_boolean
304 s12z_elf_set_mach_from_flags (bfd *abfd)
305 {
306 bfd_default_set_arch_mach (abfd, bfd_arch_s12z, 0);
307
308 return TRUE;
309 }
310
311 #define ELF_ARCH bfd_arch_s12z
312 #define ELF_MACHINE_CODE EM_S12Z
313 #define ELF_MAXPAGESIZE 0x1000
314
315 #define TARGET_BIG_SYM s12z_elf32_vec
316 #define TARGET_BIG_NAME "elf32-s12z"
317
318 #define elf_info_to_howto NULL
319 #define elf_info_to_howto_rel s12z_info_to_howto_rel
320 #define elf_backend_object_p s12z_elf_set_mach_from_flags
321
322 #include "elf32-target.h"
This page took 0.035307 seconds and 3 git commands to generate.