xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elf32-s12z.c (revision e6c7e151de239c49d2e38720a061ed9d1fa99309)
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 shift_addend_reloc (bfd *abfd, arelent *reloc_entry, struct bfd_symbol *symbol ATTRIBUTE_UNUSED,
38 		    void *data ATTRIBUTE_UNUSED, asection *input_section ATTRIBUTE_UNUSED,
39 		    bfd *output ATTRIBUTE_UNUSED, char **msg ATTRIBUTE_UNUSED)
40 {
41   /* This is a really peculiar reloc, which is done for compatibility
42      with the Freescale toolchain.
43 
44      That toolchain appears to (ab)use the lowest 15 bits of the addend for
45      the purpose of holding flags.  The purpose of these flags are unknown.
46      So in this function, when writing the bfd we left shift the addend by
47      15, and when reading we right shift it by 15 (discarding the lower bits).
48 
49      This allows the linker to work with object files generated by Freescale,
50      as well as by Gas.  */
51 
52   if (abfd->is_linker_input)
53     reloc_entry->addend >>= 15;
54   else
55     reloc_entry->addend <<= 15;
56 
57   return bfd_reloc_continue;
58 }
59 
60 #define USE_REL	0
61 
62 static reloc_howto_type elf_s12z_howto_table[] =
63 {
64   /* This reloc does nothing.  */
65   HOWTO (R_S12Z_NONE,	/* type */
66 	 0,			/* rightshift */
67 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
68 	 0,			/* bitsize */
69 	 FALSE,			/* pc_relative */
70 	 0,			/* bitpos */
71 	 complain_overflow_dont,/* complain_on_overflow */
72 	 bfd_elf_generic_reloc,	/* special_function */
73 	 "R_S12Z_NONE",	/* name */
74 	 FALSE,			/* partial_inplace */
75 	 0,			/* src_mask */
76 	 0,			/* dst_mask */
77 	 FALSE),		/* pcrel_offset */
78 
79   /* A 24 bit absolute relocation emitted by the OPR mode operands  */
80   HOWTO (R_S12Z_OPR,        /* type */
81 	 0,			/* rightshift */
82 	 5,			/* size (0 = byte, 1 = short, 2 = long) */
83 	 24,			/* bitsize */
84 	 FALSE,			/* pc_relative */
85 	 0,			/* bitpos */
86 	 complain_overflow_bitfield,	/* complain_on_overflow */
87 	 shift_addend_reloc,
88 	 "R_S12Z_OPR",	/* name */
89 	 FALSE,			/* partial_inplace */
90 	 0x00ffffff,            /* src_mask */
91 	 0x00ffffff,		/* dst_mask */
92 	 FALSE),		/* pcrel_offset */
93 
94   /* The purpose of this reloc is not known */
95   HOWTO (R_S12Z_UKNWN_2,	/* type */
96 	 0,			/* rightshift */
97 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
98 	 0,			/* bitsize */
99 	 FALSE,			/* pc_relative */
100 	 0,			/* bitpos */
101 	 complain_overflow_dont,/* complain_on_overflow */
102 	 bfd_elf_generic_reloc,	/* special_function */
103 	 "R_S12Z_UKNWN_2",	/* name */
104 	 FALSE,			/* partial_inplace */
105 	 0,			/* src_mask */
106 	 0,			/* dst_mask */
107 	 FALSE),		/* pcrel_offset */
108 
109   /* A 15 bit PC-rel relocation */
110   HOWTO (R_S12Z_PCREL_7_15,	/* type */
111 	 0,			/* rightshift */
112 	 1,			/* size (0 = byte, 1 = short, 2 = long) */
113 	 15,			/* bitsize */
114 	 TRUE,			/* pc_relative */
115 	 0,			/* bitpos */
116 	 complain_overflow_bitfield,	/* complain_on_overflow */
117 	 shift_addend_reloc,
118 	 "R_S12Z_PCREL_7_15",	/* name */
119 	 FALSE,			/* partial_inplace */
120 	 0x00,                  /* src_mask */
121 	 0x007fff,		/* dst_mask */
122 	 TRUE),		/* pcrel_offset */
123 
124   /* A 24 bit absolute relocation emitted by EXT24 mode operands */
125   HOWTO (R_S12Z_EXT24,        /* type */
126 	 0,			/* rightshift */
127 	 5,			/* size (0 = byte, 1 = short, 2 = long) */
128 	 24,			/* bitsize */
129 	 FALSE,			/* pc_relative */
130 	 0,			/* bitpos */
131 	 complain_overflow_bitfield,	/* complain_on_overflow */
132 	 bfd_elf_generic_reloc,	/* special_function */
133 	 "R_S12Z_EXT24",	/* name */
134 	 FALSE,			/* partial_inplace */
135 	 0x00ffffff,            /* src_mask */
136 	 0x00ffffff,		/* dst_mask */
137 	 FALSE),		/* pcrel_offset */
138 
139   /* The purpose of this reloc is not known */
140   HOWTO (R_S12Z_UKNWN_3,	/* type */
141 	 0,			/* rightshift */
142 	 3,			/* size (0 = byte, 1 = short, 2 = long) */
143 	 0,			/* bitsize */
144 	 FALSE,			/* pc_relative */
145 	 0,			/* bitpos */
146 	 complain_overflow_dont,/* complain_on_overflow */
147 	 bfd_elf_generic_reloc,	/* special_function */
148 	 "R_S12Z_UKNWN_3",	/* name */
149 	 FALSE,			/* partial_inplace */
150 	 0,			/* src_mask */
151 	 0,			/* dst_mask */
152 	 FALSE),		/* pcrel_offset */
153 
154   /* A 32 bit absolute relocation  */
155   HOWTO (R_S12Z_EXT32,        /* type */
156 	 0,			/* rightshift */
157 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
158 	 32,			/* bitsize */
159 	 FALSE,			/* pc_relative */
160 	 0,			/* bitpos */
161 	 complain_overflow_bitfield,	/* complain_on_overflow */
162 	 bfd_elf_generic_reloc,	/* special_function */
163 	 "R_S12Z_EXT32",	/* name */
164 	 FALSE,			/* partial_inplace */
165 	 0xffffffff,            /* src_mask */
166 	 0xffffffff,		/* dst_mask */
167 	 FALSE),		/* pcrel_offset */
168 };
169 
170 /* Map BFD reloc types to S12Z ELF reloc types.  */
171 
172 struct s12z_reloc_map
173 {
174   bfd_reloc_code_real_type bfd_reloc_val;
175   unsigned char elf_reloc_val;
176 };
177 
178 static const struct s12z_reloc_map s12z_reloc_map[] =
179 {
180   /* bfd reloc val */ /* elf reloc val */
181   {BFD_RELOC_NONE, R_S12Z_NONE},
182   {BFD_RELOC_32, R_S12Z_EXT32},
183   {BFD_RELOC_24, R_S12Z_EXT24},
184   {BFD_RELOC_16_PCREL, R_S12Z_PCREL_7_15}
185 };
186 
187 static reloc_howto_type *
188 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
189 				 bfd_reloc_code_real_type code)
190 {
191   unsigned int i;
192 
193   for (i = 0;
194        i < sizeof (s12z_reloc_map) / sizeof (struct s12z_reloc_map);
195        i++)
196     {
197       if (s12z_reloc_map[i].bfd_reloc_val == code)
198 	{
199 	  return &elf_s12z_howto_table[s12z_reloc_map[i].elf_reloc_val];
200 	}
201     }
202 
203   printf ("%s:%d Not found type %d\n", __FILE__, __LINE__, code);
204 
205   return NULL;
206 }
207 
208 static reloc_howto_type *
209 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
210 				 const char *r_name)
211 {
212   unsigned int i;
213 
214   printf ("%s:%d Looking up %s\n", __FILE__, __LINE__, r_name);
215 
216   for (i = 0;
217        i < (sizeof (elf_s12z_howto_table)
218 	    / sizeof (elf_s12z_howto_table[0]));
219        i++)
220     if (elf_s12z_howto_table[i].name != NULL
221 	&& strcasecmp (elf_s12z_howto_table[i].name, r_name) == 0)
222       return &elf_s12z_howto_table[i];
223 
224   return NULL;
225 }
226 
227 /* Set the howto pointer for an S12Z ELF reloc.  */
228 
229 static bfd_boolean
230 s12z_info_to_howto_rel (bfd *abfd,
231 			  arelent *cache_ptr, Elf_Internal_Rela *dst)
232 {
233   unsigned int  r_type = ELF32_R_TYPE (dst->r_info);
234 
235   if (r_type >= (unsigned int) R_S12Z_max)
236     {
237       /* xgettext:c-format */
238       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
239 			  abfd, r_type);
240       bfd_set_error (bfd_error_bad_value);
241       return FALSE;
242     }
243 
244   cache_ptr->howto = &elf_s12z_howto_table[r_type];
245   return TRUE;
246 }
247 
248 static bfd_boolean
249 s12z_elf_set_mach_from_flags (bfd *abfd)
250 {
251   bfd_default_set_arch_mach (abfd, bfd_arch_s12z, 0); // bfd_mach_s12z);
252 
253   return TRUE;
254 }
255 
256 #define ELF_ARCH		bfd_arch_s12z
257 #define ELF_TARGET_ID		0
258 #define ELF_MACHINE_CODE	EM_S12Z
259 #define ELF_MAXPAGESIZE		0x1000
260 
261 #define TARGET_BIG_SYM		s12z_elf32_vec
262 #define TARGET_BIG_NAME		"elf32-s12z"
263 
264 #define elf_info_to_howto			NULL
265 #define elf_info_to_howto_rel			s12z_info_to_howto_rel
266 #define elf_backend_object_p			s12z_elf_set_mach_from_flags
267 #define elf_backend_final_write_processing	NULL
268 #define elf_backend_can_gc_sections		1
269 
270 #include "elf32-target.h"
271