xref: /netbsd-src/external/gpl3/gdb.old/dist/bfd/elf32-s12z.c (revision ae082add65442546470c0ba499a860ee89eed305)
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 * 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 	 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   {BFD_RELOC_S12Z_OPR, R_S12Z_OPR}
241 };
242 
243 static reloc_howto_type *
244 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
245 				 bfd_reloc_code_real_type code)
246 {
247   unsigned int i;
248 
249   for (i = 0;
250        i < sizeof (s12z_reloc_map) / sizeof (struct s12z_reloc_map);
251        i++)
252     {
253       if (s12z_reloc_map[i].bfd_reloc_val == code)
254 	{
255 	  return &elf_s12z_howto_table[s12z_reloc_map[i].elf_reloc_val];
256 	}
257     }
258 
259   printf ("%s:%d Not found type %d\n", __FILE__, __LINE__, code);
260 
261   return NULL;
262 }
263 
264 static reloc_howto_type *
265 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
266 				 const char *r_name)
267 {
268   unsigned int i;
269 
270   for (i = 0;
271        i < (sizeof (elf_s12z_howto_table)
272 	    / sizeof (elf_s12z_howto_table[0]));
273        i++)
274     if (elf_s12z_howto_table[i].name != NULL
275 	&& strcasecmp (elf_s12z_howto_table[i].name, r_name) == 0)
276       return &elf_s12z_howto_table[i];
277 
278   return NULL;
279 }
280 
281 /* Set the howto pointer for an S12Z ELF reloc.  */
282 
283 static bfd_boolean
284 s12z_info_to_howto_rel (bfd *abfd,
285 			  arelent *cache_ptr, Elf_Internal_Rela *dst)
286 {
287   unsigned int  r_type = ELF32_R_TYPE (dst->r_info);
288 
289   if (r_type >= (unsigned int) R_S12Z_max)
290     {
291       /* xgettext:c-format */
292       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
293 			  abfd, r_type);
294       bfd_set_error (bfd_error_bad_value);
295       return FALSE;
296     }
297 
298   cache_ptr->howto = &elf_s12z_howto_table[r_type];
299   return TRUE;
300 }
301 
302 static bfd_boolean
303 s12z_elf_set_mach_from_flags (bfd *abfd)
304 {
305   bfd_default_set_arch_mach (abfd, bfd_arch_s12z, 0);
306 
307   return TRUE;
308 }
309 
310 #define ELF_ARCH		bfd_arch_s12z
311 #define ELF_MACHINE_CODE	EM_S12Z
312 #define ELF_MAXPAGESIZE		0x1000
313 
314 #define TARGET_BIG_SYM		s12z_elf32_vec
315 #define TARGET_BIG_NAME		"elf32-s12z"
316 
317 #define elf_info_to_howto			NULL
318 #define elf_info_to_howto_rel			s12z_info_to_howto_rel
319 #define elf_backend_object_p			s12z_elf_set_mach_from_flags
320 #define elf_backend_final_write_processing	NULL
321 #define elf_backend_can_gc_sections		1
322 
323 #include "elf32-target.h"
324