S12Z: Rename reloc R_S12Z_UKNWN_3 to R_S12Z_EXT18 and implement according to recently...
[external/binutils.git] / bfd / elf32-s12z.c
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))
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 * 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);
49
50   /* Keep the wanted bits and discard the rest.  */
51   result &= 0xFA0000;
52
53   val += symbol->section->output_section->vma;
54   val += symbol->section->output_offset;
55
56   /* The lowest 17 bits are copied verbatim.  */
57   result |= val & 0x1FFFF;
58
59   /* The 18th bit is put into the 19th position.  */
60   result |= (val & 0x020000) << 1;
61
62   bfd_put_24 (abfd, result, (unsigned char *) data + octets);
63
64   return bfd_reloc_ok;
65 }
66
67
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)
72 {
73   /* This is a really peculiar reloc, which is done for compatibility
74      with the Freescale toolchain.
75
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).
80
81      This allows the linker to work with object files generated by Freescale,
82      as well as by Gas.  */
83
84   if (abfd->is_linker_input)
85     reloc_entry->addend >>= 15;
86   else
87     reloc_entry->addend <<= 15;
88
89   return bfd_reloc_continue;
90 }
91
92 #define USE_REL 0
93
94 static reloc_howto_type elf_s12z_howto_table[] =
95 {
96   /* This reloc does nothing.  */
97   HOWTO (R_S12Z_NONE,   /* type */
98          0,                     /* rightshift */
99          3,                     /* size (0 = byte, 1 = short, 2 = long) */
100          0,                     /* bitsize */
101          FALSE,                 /* pc_relative */
102          0,                     /* bitpos */
103          complain_overflow_dont,/* complain_on_overflow */
104          bfd_elf_generic_reloc, /* special_function */
105          "R_S12Z_NONE", /* name */
106          FALSE,                 /* partial_inplace */
107          0,                     /* src_mask */
108          0,                     /* dst_mask */
109          FALSE),                /* pcrel_offset */
110
111   /* A 24 bit absolute relocation emitted by the OPR mode operands  */
112   HOWTO (R_S12Z_OPR,        /* type */
113          0,                     /* rightshift */
114          5,                     /* size (0 = byte, 1 = short, 2 = long) */
115          24,                    /* bitsize */
116          FALSE,                 /* pc_relative */
117          0,                     /* bitpos */
118          complain_overflow_bitfield,    /* complain_on_overflow */
119          shift_addend_reloc,
120          "R_S12Z_OPR",  /* name */
121          FALSE,                 /* partial_inplace */
122          0x00ffffff,            /* src_mask */
123          0x00ffffff,            /* dst_mask */
124          FALSE),                /* pcrel_offset */
125
126   /* The purpose of this reloc is not known */
127   HOWTO (R_S12Z_UKNWN_2,        /* type */
128          0,                     /* rightshift */
129          3,                     /* size (0 = byte, 1 = short, 2 = long) */
130          0,                     /* bitsize */
131          FALSE,                 /* pc_relative */
132          0,                     /* bitpos */
133          complain_overflow_dont,/* complain_on_overflow */
134          bfd_elf_generic_reloc, /* special_function */
135          "R_S12Z_UKNWN_2",      /* name */
136          FALSE,                 /* partial_inplace */
137          0,                     /* src_mask */
138          0,                     /* dst_mask */
139          FALSE),                /* pcrel_offset */
140
141   /* A 15 bit PC-rel relocation */
142   HOWTO (R_S12Z_PCREL_7_15,     /* type */
143          0,                     /* rightshift */
144          1,                     /* size (0 = byte, 1 = short, 2 = long) */
145          15,                    /* bitsize */
146          TRUE,                  /* pc_relative */
147          0,                     /* bitpos */
148          complain_overflow_bitfield,    /* complain_on_overflow */
149          shift_addend_reloc,
150          "R_S12Z_PCREL_7_15",   /* name */
151          FALSE,                 /* partial_inplace */
152          0x00,                  /* src_mask */
153          0x007fff,              /* dst_mask */
154          TRUE),         /* pcrel_offset */
155
156   /* A 24 bit absolute relocation emitted by EXT24 mode operands */
157   HOWTO (R_S12Z_EXT24,        /* type */
158          0,                     /* rightshift */
159          5,                     /* size (0 = byte, 1 = short, 2 = long) */
160          24,                    /* bitsize */
161          FALSE,                 /* pc_relative */
162          0,                     /* bitpos */
163          complain_overflow_bitfield,    /* complain_on_overflow */
164          bfd_elf_generic_reloc, /* special_function */
165          "R_S12Z_EXT24",        /* name */
166          FALSE,                 /* partial_inplace */
167          0x00ffffff,            /* src_mask */
168          0x00ffffff,            /* dst_mask */
169          FALSE),                /* pcrel_offset */
170
171   /* An 18 bit absolute relocation */
172   HOWTO (R_S12Z_EXT18,        /* type */
173          0,                     /* rightshift */
174          5,                     /* size (0 = byte, 1 = short, 2 = long) */
175          18,                    /* bitsize */
176          FALSE,                 /* pc_relative */
177          0,                     /* bitpos */
178          complain_overflow_bitfield,    /* complain_on_overflow */
179          opru18_reloc,          /* special_function */
180          "R_S12Z_EXT18",        /* name */
181          FALSE,                 /* partial_inplace */
182          0x0005ffff,            /* src_mask */
183          0x0005ffff,            /* dst_mask */
184          FALSE),                /* pcrel_offset */
185
186   /* A 32 bit absolute relocation  */
187   HOWTO (R_S12Z_EXT32,        /* type */
188          0,                     /* rightshift */
189          2,                     /* size (0 = byte, 1 = short, 2 = long) */
190          32,                    /* bitsize */
191          FALSE,                 /* pc_relative */
192          0,                     /* bitpos */
193          complain_overflow_bitfield,    /* complain_on_overflow */
194          bfd_elf_generic_reloc, /* special_function */
195          "R_S12Z_EXT32",        /* name */
196          FALSE,                 /* partial_inplace */
197          0xffffffff,            /* src_mask */
198          0xffffffff,            /* dst_mask */
199          FALSE),                /* pcrel_offset */
200 };
201
202 /* Map BFD reloc types to S12Z ELF reloc types.  */
203
204 struct s12z_reloc_map
205 {
206   bfd_reloc_code_real_type bfd_reloc_val;
207   unsigned char elf_reloc_val;
208 };
209
210 static const struct s12z_reloc_map s12z_reloc_map[] =
211 {
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}
217 };
218
219 static reloc_howto_type *
220 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
221                                  bfd_reloc_code_real_type code)
222 {
223   unsigned int i;
224
225   for (i = 0;
226        i < sizeof (s12z_reloc_map) / sizeof (struct s12z_reloc_map);
227        i++)
228     {
229       if (s12z_reloc_map[i].bfd_reloc_val == code)
230         {
231           return &elf_s12z_howto_table[s12z_reloc_map[i].elf_reloc_val];
232         }
233     }
234
235   printf ("%s:%d Not found type %d\n", __FILE__, __LINE__, code);
236
237   return NULL;
238 }
239
240 static reloc_howto_type *
241 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
242                                  const char *r_name)
243 {
244   unsigned int i;
245
246   printf ("%s:%d Looking up %s\n", __FILE__, __LINE__, r_name);
247
248   for (i = 0;
249        i < (sizeof (elf_s12z_howto_table)
250             / sizeof (elf_s12z_howto_table[0]));
251        i++)
252     if (elf_s12z_howto_table[i].name != NULL
253         && strcasecmp (elf_s12z_howto_table[i].name, r_name) == 0)
254       return &elf_s12z_howto_table[i];
255
256   return NULL;
257 }
258
259 /* Set the howto pointer for an S12Z ELF reloc.  */
260
261 static bfd_boolean
262 s12z_info_to_howto_rel (bfd *abfd,
263                           arelent *cache_ptr, Elf_Internal_Rela *dst)
264 {
265   unsigned int  r_type = ELF32_R_TYPE (dst->r_info);
266
267   if (r_type >= (unsigned int) R_S12Z_max)
268     {
269       /* xgettext:c-format */
270       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
271                           abfd, r_type);
272       bfd_set_error (bfd_error_bad_value);
273       return FALSE;
274     }
275
276   cache_ptr->howto = &elf_s12z_howto_table[r_type];
277   return TRUE;
278 }
279
280 static bfd_boolean
281 s12z_elf_set_mach_from_flags (bfd *abfd)
282 {
283   bfd_default_set_arch_mach (abfd, bfd_arch_s12z, 0);
284
285   return TRUE;
286 }
287
288 #define ELF_ARCH                bfd_arch_s12z
289 #define ELF_MACHINE_CODE        EM_S12Z
290 #define ELF_MAXPAGESIZE         0x1000
291
292 #define TARGET_BIG_SYM          s12z_elf32_vec
293 #define TARGET_BIG_NAME         "elf32-s12z"
294
295 #define elf_info_to_howto                       NULL
296 #define elf_info_to_howto_rel                   s12z_info_to_howto_rel
297 #define elf_backend_object_p                    s12z_elf_set_mach_from_flags
298 #define elf_backend_final_write_processing      NULL
299 #define elf_backend_can_gc_sections             1
300
301 #include "elf32-target.h"