gdb: Update test pattern to deal with native-extended-gdbserver
[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          0x00000000,            /* 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          0x00000000,            /* 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          0x00000000,            /* src_mask */
183          0x0005ffff,            /* dst_mask */
184          FALSE),                /* pcrel_offset */
185
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.
190
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
193      applications.  */
194
195   HOWTO (R_S12Z_CW32,        /* type */
196          0,                     /* rightshift */
197          2,                     /* size (0 = byte, 1 = short, 2 = long) */
198          32,                    /* bitsize */
199          FALSE,                 /* pc_relative */
200          0,                     /* bitpos */
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 */
208
209   /* A 32 bit absolute relocation  */
210   HOWTO (R_S12Z_EXT32,        /* type */
211          0,                     /* rightshift */
212          2,                     /* size (0 = byte, 1 = short, 2 = long) */
213          32,                    /* bitsize */
214          FALSE,                 /* pc_relative */
215          0,                     /* bitpos */
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 */
223 };
224
225 /* Map BFD reloc types to S12Z ELF reloc types.  */
226
227 struct s12z_reloc_map
228 {
229   bfd_reloc_code_real_type bfd_reloc_val;
230   unsigned char elf_reloc_val;
231 };
232
233 static const struct s12z_reloc_map s12z_reloc_map[] =
234 {
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 };
241
242 static reloc_howto_type *
243 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
244                                  bfd_reloc_code_real_type code)
245 {
246   unsigned int i;
247
248   for (i = 0;
249        i < sizeof (s12z_reloc_map) / sizeof (struct s12z_reloc_map);
250        i++)
251     {
252       if (s12z_reloc_map[i].bfd_reloc_val == code)
253         {
254           return &elf_s12z_howto_table[s12z_reloc_map[i].elf_reloc_val];
255         }
256     }
257
258   printf ("%s:%d Not found type %d\n", __FILE__, __LINE__, code);
259
260   return NULL;
261 }
262
263 static reloc_howto_type *
264 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
265                                  const char *r_name)
266 {
267   unsigned int i;
268
269   for (i = 0;
270        i < (sizeof (elf_s12z_howto_table)
271             / sizeof (elf_s12z_howto_table[0]));
272        i++)
273     if (elf_s12z_howto_table[i].name != NULL
274         && strcasecmp (elf_s12z_howto_table[i].name, r_name) == 0)
275       return &elf_s12z_howto_table[i];
276
277   return NULL;
278 }
279
280 /* Set the howto pointer for an S12Z ELF reloc.  */
281
282 static bfd_boolean
283 s12z_info_to_howto_rel (bfd *abfd,
284                           arelent *cache_ptr, Elf_Internal_Rela *dst)
285 {
286   unsigned int  r_type = ELF32_R_TYPE (dst->r_info);
287
288   if (r_type >= (unsigned int) R_S12Z_max)
289     {
290       /* xgettext:c-format */
291       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
292                           abfd, r_type);
293       bfd_set_error (bfd_error_bad_value);
294       return FALSE;
295     }
296
297   cache_ptr->howto = &elf_s12z_howto_table[r_type];
298   return TRUE;
299 }
300
301 static bfd_boolean
302 s12z_elf_set_mach_from_flags (bfd *abfd)
303 {
304   bfd_default_set_arch_mach (abfd, bfd_arch_s12z, 0);
305
306   return TRUE;
307 }
308
309 #define ELF_ARCH                bfd_arch_s12z
310 #define ELF_MACHINE_CODE        EM_S12Z
311 #define ELF_MAXPAGESIZE         0x1000
312
313 #define TARGET_BIG_SYM          s12z_elf32_vec
314 #define TARGET_BIG_NAME         "elf32-s12z"
315
316 #define elf_info_to_howto                       NULL
317 #define elf_info_to_howto_rel                   s12z_info_to_howto_rel
318 #define elf_backend_object_p                    s12z_elf_set_mach_from_flags
319 #define elf_backend_final_write_processing      NULL
320 #define elf_backend_can_gc_sections             1
321
322 #include "elf32-target.h"