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