1 /* BFD library support routines for the Renesas / SuperH SH architecture.
2 Copyright (C) 1993-2016 Free Software Foundation, Inc.
3 Hacked by Steve Chamberlain of Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
25 #include "../opcodes/sh-opc.h"
27 #define SH_NEXT arch_info_struct + 0
28 #define SH2_NEXT arch_info_struct + 1
29 #define SH2E_NEXT arch_info_struct + 2
30 #define SH_DSP_NEXT arch_info_struct + 3
31 #define SH3_NEXT arch_info_struct + 4
32 #define SH3_NOMMU_NEXT arch_info_struct + 5
33 #define SH3_DSP_NEXT arch_info_struct + 6
34 #define SH3E_NEXT arch_info_struct + 7
35 #define SH4_NEXT arch_info_struct + 8
36 #define SH4A_NEXT arch_info_struct + 9
37 #define SH4AL_DSP_NEXT arch_info_struct + 10
38 #define SH4_NOFPU_NEXT arch_info_struct + 11
39 #define SH4_NOMMU_NOFPU_NEXT arch_info_struct + 12
40 #define SH4A_NOFPU_NEXT arch_info_struct + 13
41 #define SH2A_NEXT arch_info_struct + 14
42 #define SH2A_NOFPU_NEXT arch_info_struct + 15
43 #define SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT arch_info_struct + 16
44 #define SH2A_NOFPU_OR_SH3_NOMMU_NEXT arch_info_struct + 17
45 #define SH2A_OR_SH4_NEXT arch_info_struct + 18
46 #define SH2A_OR_SH3E_NEXT arch_info_struct + 19
47 #define SH64_NEXT NULL
49 static const bfd_arch_info_type arch_info_struct[] =
52 32, /* 32 bits in a word. */
53 32, /* 32 bits in an address. */
54 8, /* 8 bits in a byte. */
57 "sh", /* Architecture name. */
58 "sh2", /* Machine name. */
60 FALSE, /* Not the default. */
61 bfd_default_compatible,
63 bfd_arch_default_fill,
67 32, /* 32 bits in a word. */
68 32, /* 32 bits in an address. */
69 8, /* 8 bits in a byte. */
72 "sh", /* Architecture name. */
73 "sh2e", /* Machine name. */
75 FALSE, /* Not the default. */
76 bfd_default_compatible,
78 bfd_arch_default_fill,
82 32, /* 32 bits in a word. */
83 32, /* 32 bits in an address. */
84 8, /* 8 bits in a byte. */
87 "sh", /* Architecture name. */
88 "sh-dsp", /* Machine name. */
90 FALSE, /* Not the default. */
91 bfd_default_compatible,
93 bfd_arch_default_fill,
97 32, /* 32 bits in a word. */
98 32, /* 32 bits in an address. */
99 8, /* 8 bits in a byte. */
102 "sh", /* Architecture name. */
103 "sh3", /* Machine name. */
105 FALSE, /* Not the default. */
106 bfd_default_compatible,
108 bfd_arch_default_fill,
112 32, /* 32 bits in a word. */
113 32, /* 32 bits in an address. */
114 8, /* 8 bits in a byte. */
117 "sh", /* Architecture name. */
118 "sh3-nommu", /* Machine name. */
120 FALSE, /* Not the default. */
121 bfd_default_compatible,
123 bfd_arch_default_fill,
127 32, /* 32 bits in a word. */
128 32, /* 32 bits in an address. */
129 8, /* 8 bits in a byte. */
132 "sh", /* Architecture name. */
133 "sh3-dsp", /* Machine name. */
135 FALSE, /* Not the default. */
136 bfd_default_compatible,
138 bfd_arch_default_fill,
142 32, /* 32 bits in a word. */
143 32, /* 32 bits in an address. */
144 8, /* 8 bits in a byte. */
147 "sh", /* Architecture name. */
148 "sh3e", /* Machine name. */
150 FALSE, /* Not the default. */
151 bfd_default_compatible,
153 bfd_arch_default_fill,
157 32, /* 32 bits in a word. */
158 32, /* 32 bits in an address. */
159 8, /* 8 bits in a byte. */
162 "sh", /* Architecture name. */
163 "sh4", /* Machine name. */
165 FALSE, /* Not the default. */
166 bfd_default_compatible,
168 bfd_arch_default_fill,
172 32, /* 32 bits in a word. */
173 32, /* 32 bits in an address. */
174 8, /* 8 bits in a byte. */
177 "sh", /* Architecture name. */
178 "sh4a", /* Machine name. */
180 FALSE, /* Not the default. */
181 bfd_default_compatible,
183 bfd_arch_default_fill,
187 32, /* 32 bits in a word. */
188 32, /* 32 bits in an address. */
189 8, /* 8 bits in a byte. */
192 "sh", /* Architecture name. */
193 "sh4al-dsp", /* Machine name. */
195 FALSE, /* Not the default. */
196 bfd_default_compatible,
198 bfd_arch_default_fill,
202 32, /* 32 bits in a word. */
203 32, /* 32 bits in an address. */
204 8, /* 8 bits in a byte. */
207 "sh", /* Architecture name. */
208 "sh4-nofpu", /* Machine name. */
210 FALSE, /* Not the default. */
211 bfd_default_compatible,
213 bfd_arch_default_fill,
217 32, /* 32 bits in a word. */
218 32, /* 32 bits in an address. */
219 8, /* 8 bits in a byte. */
221 bfd_mach_sh4_nommu_nofpu,
222 "sh", /* Architecture name. */
223 "sh4-nommu-nofpu", /* Machine name. */
225 FALSE, /* Not the default. */
226 bfd_default_compatible,
228 bfd_arch_default_fill,
232 32, /* 32 bits in a word. */
233 32, /* 32 bits in an address. */
234 8, /* 8 bits in a byte. */
237 "sh", /* Architecture name. */
238 "sh4a-nofpu", /* Machine name. */
240 FALSE, /* Not the default. */
241 bfd_default_compatible,
243 bfd_arch_default_fill,
247 32, /* 32 bits in a word. */
248 32, /* 32 bits in an address. */
249 8, /* 8 bits in a byte. */
252 "sh", /* Architecture name. */
253 "sh2a", /* Machine name. */
255 FALSE, /* Not the default. */
256 bfd_default_compatible,
258 bfd_arch_default_fill,
262 32, /* 32 bits in a word. */
263 32, /* 32 bits in an address. */
264 8, /* 8 bits in a byte. */
267 "sh", /* Architecture name. */
268 "sh2a-nofpu", /* Machine name. */
270 FALSE, /* Not the default. */
271 bfd_default_compatible,
273 bfd_arch_default_fill,
277 32, /* 32 bits in a word. */
278 32, /* 32 bits in an address. */
279 8, /* 8 bits in a byte. */
281 bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu,
282 "sh", /* Architecture name. */
283 "sh2a-nofpu-or-sh4-nommu-nofpu", /* Machine name. */
285 FALSE, /* Not the default. */
286 bfd_default_compatible,
288 bfd_arch_default_fill,
289 SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT
292 32, /* 32 bits in a word. */
293 32, /* 32 bits in an address. */
294 8, /* 8 bits in a byte. */
296 bfd_mach_sh2a_nofpu_or_sh3_nommu,
297 "sh", /* Architecture name. . */
298 "sh2a-nofpu-or-sh3-nommu", /* Machine name. */
300 FALSE, /* Not the default. */
301 bfd_default_compatible,
303 bfd_arch_default_fill,
304 SH2A_NOFPU_OR_SH3_NOMMU_NEXT
307 32, /* 32 bits in a word. */
308 32, /* 32 bits in an address. */
309 8, /* 8 bits in a byte. */
311 bfd_mach_sh2a_or_sh4,
312 "sh", /* Architecture name. */
313 "sh2a-or-sh4", /* Machine name. */
315 FALSE, /* Not the default. */
316 bfd_default_compatible,
318 bfd_arch_default_fill,
322 32, /* 32 bits in a word. */
323 32, /* 32 bits in an address. */
324 8, /* 8 bits in a byte. */
326 bfd_mach_sh2a_or_sh3e,
327 "sh", /* Architecture name. */
328 "sh2a-or-sh3e", /* Machine name. */
330 FALSE, /* Not the default. */
331 bfd_default_compatible,
333 bfd_arch_default_fill,
337 64, /* 64 bits in a word. */
338 64, /* 64 bits in an address. */
339 8, /* 8 bits in a byte. */
342 "sh", /* Architecture name. */
343 "sh5", /* Machine name. */
345 FALSE, /* Not the default. */
346 bfd_default_compatible,
348 bfd_arch_default_fill,
353 const bfd_arch_info_type bfd_sh_arch =
355 32, /* 32 bits in a word. */
356 32, /* 32 bits in an address. */
357 8, /* 8 bits in a byte. */
360 "sh", /* Architecture name. */
361 "sh", /* Machine name. */
363 TRUE, /* The default machine. */
364 bfd_default_compatible,
366 bfd_arch_default_fill,
371 /* This table defines the mappings from the BFD internal numbering
372 system to the opcodes internal flags system.
373 It is used by the functions defined below.
374 The prototypes for these SH specific functions are found in
377 static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
379 { bfd_mach_sh, arch_sh1, arch_sh_up },
380 { bfd_mach_sh2, arch_sh2, arch_sh2_up },
381 { bfd_mach_sh2e, arch_sh2e, arch_sh2e_up },
382 { bfd_mach_sh_dsp, arch_sh_dsp, arch_sh_dsp_up },
383 { bfd_mach_sh2a, arch_sh2a, arch_sh2a_up },
384 { bfd_mach_sh2a_nofpu, arch_sh2a_nofpu, arch_sh2a_nofpu_up },
386 { bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu_up },
387 { bfd_mach_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu_up },
388 { bfd_mach_sh2a_or_sh4, arch_sh2a_or_sh4, arch_sh2a_or_sh4_up },
389 { bfd_mach_sh2a_or_sh3e, arch_sh2a_or_sh3e, arch_sh2a_or_sh3e_up },
391 { bfd_mach_sh3, arch_sh3, arch_sh3_up },
392 { bfd_mach_sh3_nommu, arch_sh3_nommu, arch_sh3_nommu_up },
393 { bfd_mach_sh3_dsp, arch_sh3_dsp, arch_sh3_dsp_up },
394 { bfd_mach_sh3e, arch_sh3e, arch_sh3e_up },
395 { bfd_mach_sh4, arch_sh4, arch_sh4_up },
396 { bfd_mach_sh4a, arch_sh4a, arch_sh4a_up },
397 { bfd_mach_sh4al_dsp, arch_sh4al_dsp, arch_sh4al_dsp_up },
398 { bfd_mach_sh4_nofpu, arch_sh4_nofpu, arch_sh4_nofpu_up },
399 { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
400 { bfd_mach_sh4a_nofpu, arch_sh4a_nofpu, arch_sh4a_nofpu_up },
401 { 0, 0, 0 } /* Terminator. */
405 /* Convert a BFD mach number into the right opcodes arch flags
406 using the table above. */
409 sh_get_arch_from_bfd_mach (unsigned long mach)
413 while (bfd_to_arch_table[i].bfd_mach != 0)
414 if (bfd_to_arch_table[i].bfd_mach == mach)
415 return bfd_to_arch_table[i].arch;
419 /* Machine not found. */
422 return SH_ARCH_UNKNOWN_ARCH;
426 /* Convert a BFD mach number into a set of opcodes arch flags
427 describing all the compatible architectures (i.e. arch_up)
428 using the table above. */
431 sh_get_arch_up_from_bfd_mach (unsigned long mach)
435 while (bfd_to_arch_table[i].bfd_mach != 0)
436 if (bfd_to_arch_table[i].bfd_mach == mach)
437 return bfd_to_arch_table[i].arch_up;
441 /* Machine not found. */
444 return SH_ARCH_UNKNOWN_ARCH;
448 /* Convert an arbitary arch_set - not necessarily corresponding
449 directly to anything in the table above - to the most generic
450 architecture which supports all the required features, and
451 return the corresponding BFD mach. */
454 sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
456 unsigned long result = 0;
457 unsigned int best = ~arch_set;
458 unsigned int co_mask = ~0;
461 /* If arch_set permits variants with no coprocessor then do not allow
462 the other irrelevant co-processor bits to influence the choice:
463 e.g. if dsp is disallowed by arch_set, then the algorithm would
464 prefer fpu variants over nofpu variants because they also disallow
465 dsp - even though the nofpu would be the most correct choice.
466 This assumes that EVERY fpu/dsp variant has a no-coprocessor
467 counter-part, or their non-fpu/dsp instructions do not have the
468 no co-processor bit set. */
469 if (arch_set & arch_sh_no_co)
470 co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
472 while (bfd_to_arch_table[i].bfd_mach != 0)
474 unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
476 /* Conceptually: Find the architecture with the least number
477 of extra features or, if they have the same number, then
478 the greatest number of required features. Disregard
479 architectures where the required features alone do
480 not describe a valid architecture. */
481 if (((try & ~arch_set) < (best & ~arch_set)
482 || ((try & ~arch_set) == (best & ~arch_set)
483 && (~try & arch_set) < (~best & arch_set)))
484 && SH_MERGE_ARCH_SET_VALID (try, arch_set))
486 result = bfd_to_arch_table[i].bfd_mach;
493 /* This might happen if a new variant is added to sh-opc.h
494 but no corresponding entry is added to the table above. */
495 BFD_ASSERT (result != 0);
501 /* Merge the architecture type of two BFD files, such that the
502 resultant architecture supports all the features required
503 by the two input BFDs.
504 If the input BFDs are multually incompatible - i.e. one uses
505 DSP while the other uses FPU - or there is no known architecture
506 that fits the requirements then an error is emitted. */
509 sh_merge_bfd_arch (bfd *ibfd, bfd *obfd)
511 unsigned int old_arch, new_arch, merged_arch;
513 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
516 old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
517 new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
519 merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
521 if (!SH_VALID_CO_ARCH_SET (merged_arch))
523 (*_bfd_error_handler)
524 ("%B: uses %s instructions while previous modules use %s instructions",
526 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
527 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
528 bfd_set_error (bfd_error_bad_value);
531 else if (!SH_VALID_ARCH_SET (merged_arch))
533 (*_bfd_error_handler)
534 ("internal error: merge of architecture '%s' with architecture '%s' produced unknown architecture\n",
535 bfd_printable_name (obfd),
536 bfd_printable_name (ibfd));
537 bfd_set_error (bfd_error_bad_value);
541 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
542 sh_get_bfd_mach_from_arch_set (merged_arch));