1 /* BFD library support routines for the Renesas / SuperH SH architecture.
2 Copyright 1993, 1994, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 Free Software Foundation, Inc.
4 Hacked by Steve Chamberlain of Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
26 #include "../opcodes/sh-opc.h"
28 #define SH_NEXT arch_info_struct + 0
29 #define SH2_NEXT arch_info_struct + 1
30 #define SH2E_NEXT arch_info_struct + 2
31 #define SH_DSP_NEXT arch_info_struct + 3
32 #define SH3_NEXT arch_info_struct + 4
33 #define SH3_NOMMU_NEXT arch_info_struct + 5
34 #define SH3_DSP_NEXT arch_info_struct + 6
35 #define SH3E_NEXT arch_info_struct + 7
36 #define SH4_NEXT arch_info_struct + 8
37 #define SH4A_NEXT arch_info_struct + 9
38 #define SH4AL_DSP_NEXT arch_info_struct + 10
39 #define SH4_NOFPU_NEXT arch_info_struct + 11
40 #define SH4_NOMMU_NOFPU_NEXT arch_info_struct + 12
41 #define SH4A_NOFPU_NEXT arch_info_struct + 13
42 #define SH2A_NEXT arch_info_struct + 14
43 #define SH2A_NOFPU_NEXT arch_info_struct + 15
44 #define SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT arch_info_struct + 16
45 #define SH2A_NOFPU_OR_SH3_NOMMU_NEXT arch_info_struct + 17
46 #define SH2A_OR_SH4_NEXT arch_info_struct + 18
47 #define SH2A_OR_SH3E_NEXT arch_info_struct + 19
48 #define SH64_NEXT NULL
50 static const bfd_arch_info_type arch_info_struct[] =
53 32, /* 32 bits in a word. */
54 32, /* 32 bits in an address. */
55 8, /* 8 bits in a byte. */
58 "sh", /* Architecture name. */
59 "sh2", /* Machine name. */
61 FALSE, /* Not the default. */
62 bfd_default_compatible,
64 bfd_arch_default_fill,
68 32, /* 32 bits in a word. */
69 32, /* 32 bits in an address. */
70 8, /* 8 bits in a byte. */
73 "sh", /* Architecture name. */
74 "sh2e", /* Machine name. */
76 FALSE, /* Not the default. */
77 bfd_default_compatible,
79 bfd_arch_default_fill,
83 32, /* 32 bits in a word. */
84 32, /* 32 bits in an address. */
85 8, /* 8 bits in a byte. */
88 "sh", /* Architecture name. */
89 "sh-dsp", /* Machine name. */
91 FALSE, /* Not the default. */
92 bfd_default_compatible,
94 bfd_arch_default_fill,
98 32, /* 32 bits in a word. */
99 32, /* 32 bits in an address. */
100 8, /* 8 bits in a byte. */
103 "sh", /* Architecture name. */
104 "sh3", /* Machine name. */
106 FALSE, /* Not the default. */
107 bfd_default_compatible,
109 bfd_arch_default_fill,
113 32, /* 32 bits in a word. */
114 32, /* 32 bits in an address. */
115 8, /* 8 bits in a byte. */
118 "sh", /* Architecture name. */
119 "sh3-nommu", /* Machine name. */
121 FALSE, /* Not the default. */
122 bfd_default_compatible,
124 bfd_arch_default_fill,
128 32, /* 32 bits in a word. */
129 32, /* 32 bits in an address. */
130 8, /* 8 bits in a byte. */
133 "sh", /* Architecture name. */
134 "sh3-dsp", /* Machine name. */
136 FALSE, /* Not the default. */
137 bfd_default_compatible,
139 bfd_arch_default_fill,
143 32, /* 32 bits in a word. */
144 32, /* 32 bits in an address. */
145 8, /* 8 bits in a byte. */
148 "sh", /* Architecture name. */
149 "sh3e", /* Machine name. */
151 FALSE, /* Not the default. */
152 bfd_default_compatible,
154 bfd_arch_default_fill,
158 32, /* 32 bits in a word. */
159 32, /* 32 bits in an address. */
160 8, /* 8 bits in a byte. */
163 "sh", /* Architecture name. */
164 "sh4", /* Machine name. */
166 FALSE, /* Not the default. */
167 bfd_default_compatible,
169 bfd_arch_default_fill,
173 32, /* 32 bits in a word. */
174 32, /* 32 bits in an address. */
175 8, /* 8 bits in a byte. */
178 "sh", /* Architecture name. */
179 "sh4a", /* Machine name. */
181 FALSE, /* Not the default. */
182 bfd_default_compatible,
184 bfd_arch_default_fill,
188 32, /* 32 bits in a word. */
189 32, /* 32 bits in an address. */
190 8, /* 8 bits in a byte. */
193 "sh", /* Architecture name. */
194 "sh4al-dsp", /* Machine name. */
196 FALSE, /* Not the default. */
197 bfd_default_compatible,
199 bfd_arch_default_fill,
203 32, /* 32 bits in a word. */
204 32, /* 32 bits in an address. */
205 8, /* 8 bits in a byte. */
208 "sh", /* Architecture name. */
209 "sh4-nofpu", /* Machine name. */
211 FALSE, /* Not the default. */
212 bfd_default_compatible,
214 bfd_arch_default_fill,
218 32, /* 32 bits in a word. */
219 32, /* 32 bits in an address. */
220 8, /* 8 bits in a byte. */
222 bfd_mach_sh4_nommu_nofpu,
223 "sh", /* Architecture name. */
224 "sh4-nommu-nofpu", /* Machine name. */
226 FALSE, /* Not the default. */
227 bfd_default_compatible,
229 bfd_arch_default_fill,
233 32, /* 32 bits in a word. */
234 32, /* 32 bits in an address. */
235 8, /* 8 bits in a byte. */
238 "sh", /* Architecture name. */
239 "sh4a-nofpu", /* Machine name. */
241 FALSE, /* Not the default. */
242 bfd_default_compatible,
244 bfd_arch_default_fill,
248 32, /* 32 bits in a word. */
249 32, /* 32 bits in an address. */
250 8, /* 8 bits in a byte. */
253 "sh", /* Architecture name. */
254 "sh2a", /* Machine name. */
256 FALSE, /* Not the default. */
257 bfd_default_compatible,
259 bfd_arch_default_fill,
263 32, /* 32 bits in a word. */
264 32, /* 32 bits in an address. */
265 8, /* 8 bits in a byte. */
268 "sh", /* Architecture name. */
269 "sh2a-nofpu", /* Machine name. */
271 FALSE, /* Not the default. */
272 bfd_default_compatible,
274 bfd_arch_default_fill,
278 32, /* 32 bits in a word. */
279 32, /* 32 bits in an address. */
280 8, /* 8 bits in a byte. */
282 bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu,
283 "sh", /* Architecture name. */
284 "sh2a-nofpu-or-sh4-nommu-nofpu", /* Machine name. */
286 FALSE, /* Not the default. */
287 bfd_default_compatible,
289 bfd_arch_default_fill,
290 SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT
293 32, /* 32 bits in a word. */
294 32, /* 32 bits in an address. */
295 8, /* 8 bits in a byte. */
297 bfd_mach_sh2a_nofpu_or_sh3_nommu,
298 "sh", /* Architecture name. . */
299 "sh2a-nofpu-or-sh3-nommu", /* Machine name. */
301 FALSE, /* Not the default. */
302 bfd_default_compatible,
304 bfd_arch_default_fill,
305 SH2A_NOFPU_OR_SH3_NOMMU_NEXT
308 32, /* 32 bits in a word. */
309 32, /* 32 bits in an address. */
310 8, /* 8 bits in a byte. */
312 bfd_mach_sh2a_or_sh4,
313 "sh", /* Architecture name. */
314 "sh2a-or-sh4", /* Machine name. */
316 FALSE, /* Not the default. */
317 bfd_default_compatible,
319 bfd_arch_default_fill,
323 32, /* 32 bits in a word. */
324 32, /* 32 bits in an address. */
325 8, /* 8 bits in a byte. */
327 bfd_mach_sh2a_or_sh3e,
328 "sh", /* Architecture name. */
329 "sh2a-or-sh3e", /* Machine name. */
331 FALSE, /* Not the default. */
332 bfd_default_compatible,
334 bfd_arch_default_fill,
338 64, /* 64 bits in a word. */
339 64, /* 64 bits in an address. */
340 8, /* 8 bits in a byte. */
343 "sh", /* Architecture name. */
344 "sh5", /* Machine name. */
346 FALSE, /* Not the default. */
347 bfd_default_compatible,
349 bfd_arch_default_fill,
354 const bfd_arch_info_type bfd_sh_arch =
356 32, /* 32 bits in a word. */
357 32, /* 32 bits in an address. */
358 8, /* 8 bits in a byte. */
361 "sh", /* Architecture name. */
362 "sh", /* Machine name. */
364 TRUE, /* The default machine. */
365 bfd_default_compatible,
367 bfd_arch_default_fill,
372 /* This table defines the mappings from the BFD internal numbering
373 system to the opcodes internal flags system.
374 It is used by the functions defined below.
375 The prototypes for these SH specific functions are found in
378 static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
380 { bfd_mach_sh, arch_sh1, arch_sh_up },
381 { bfd_mach_sh2, arch_sh2, arch_sh2_up },
382 { bfd_mach_sh2e, arch_sh2e, arch_sh2e_up },
383 { bfd_mach_sh_dsp, arch_sh_dsp, arch_sh_dsp_up },
384 { bfd_mach_sh2a, arch_sh2a, arch_sh2a_up },
385 { bfd_mach_sh2a_nofpu, arch_sh2a_nofpu, arch_sh2a_nofpu_up },
387 { bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu_up },
388 { bfd_mach_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu_up },
389 { bfd_mach_sh2a_or_sh4, arch_sh2a_or_sh4, arch_sh2a_or_sh4_up },
390 { bfd_mach_sh2a_or_sh3e, arch_sh2a_or_sh3e, arch_sh2a_or_sh3e_up },
392 { bfd_mach_sh3, arch_sh3, arch_sh3_up },
393 { bfd_mach_sh3_nommu, arch_sh3_nommu, arch_sh3_nommu_up },
394 { bfd_mach_sh3_dsp, arch_sh3_dsp, arch_sh3_dsp_up },
395 { bfd_mach_sh3e, arch_sh3e, arch_sh3e_up },
396 { bfd_mach_sh4, arch_sh4, arch_sh4_up },
397 { bfd_mach_sh4a, arch_sh4a, arch_sh4a_up },
398 { bfd_mach_sh4al_dsp, arch_sh4al_dsp, arch_sh4al_dsp_up },
399 { bfd_mach_sh4_nofpu, arch_sh4_nofpu, arch_sh4_nofpu_up },
400 { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
401 { bfd_mach_sh4a_nofpu, arch_sh4a_nofpu, arch_sh4a_nofpu_up },
402 { 0, 0, 0 } /* Terminator. */
406 /* Convert a BFD mach number into the right opcodes arch flags
407 using the table above. */
410 sh_get_arch_from_bfd_mach (unsigned long mach)
414 while (bfd_to_arch_table[i].bfd_mach != 0)
415 if (bfd_to_arch_table[i].bfd_mach == mach)
416 return bfd_to_arch_table[i].arch;
420 /* Machine not found. */
423 return SH_ARCH_UNKNOWN_ARCH;
427 /* Convert a BFD mach number into a set of opcodes arch flags
428 describing all the compatible architectures (i.e. arch_up)
429 using the table above. */
432 sh_get_arch_up_from_bfd_mach (unsigned long mach)
436 while (bfd_to_arch_table[i].bfd_mach != 0)
437 if (bfd_to_arch_table[i].bfd_mach == mach)
438 return bfd_to_arch_table[i].arch_up;
442 /* Machine not found. */
445 return SH_ARCH_UNKNOWN_ARCH;
449 /* Convert an arbitary arch_set - not necessarily corresponding
450 directly to anything in the table above - to the most generic
451 architecture which supports all the required features, and
452 return the corresponding BFD mach. */
455 sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
457 unsigned long result = 0;
458 unsigned int best = ~arch_set;
459 unsigned int co_mask = ~0;
462 /* If arch_set permits variants with no coprocessor then do not allow
463 the other irrelevant co-processor bits to influence the choice:
464 e.g. if dsp is disallowed by arch_set, then the algorithm would
465 prefer fpu variants over nofpu variants because they also disallow
466 dsp - even though the nofpu would be the most correct choice.
467 This assumes that EVERY fpu/dsp variant has a no-coprocessor
468 counter-part, or their non-fpu/dsp instructions do not have the
469 no co-processor bit set. */
470 if (arch_set & arch_sh_no_co)
471 co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
473 while (bfd_to_arch_table[i].bfd_mach != 0)
475 unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
477 /* Conceptually: Find the architecture with the least number
478 of extra features or, if they have the same number, then
479 the greatest number of required features. Disregard
480 architectures where the required features alone do
481 not describe a valid architecture. */
482 if (((try & ~arch_set) < (best & ~arch_set)
483 || ((try & ~arch_set) == (best & ~arch_set)
484 && (~try & arch_set) < (~best & arch_set)))
485 && SH_MERGE_ARCH_SET_VALID (try, arch_set))
487 result = bfd_to_arch_table[i].bfd_mach;
494 /* This might happen if a new variant is added to sh-opc.h
495 but no corresponding entry is added to the table above. */
496 BFD_ASSERT (result != 0);
502 /* Merge the architecture type of two BFD files, such that the
503 resultant architecture supports all the features required
504 by the two input BFDs.
505 If the input BFDs are multually incompatible - i.e. one uses
506 DSP while the other uses FPU - or there is no known architecture
507 that fits the requirements then an error is emitted. */
510 sh_merge_bfd_arch (bfd *ibfd, bfd *obfd)
512 unsigned int old_arch, new_arch, merged_arch;
514 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
517 old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
518 new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
520 merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
522 if (!SH_VALID_CO_ARCH_SET (merged_arch))
524 (*_bfd_error_handler)
525 ("%B: uses %s instructions while previous modules use %s instructions",
527 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
528 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
529 bfd_set_error (bfd_error_bad_value);
532 else if (!SH_VALID_ARCH_SET (merged_arch))
534 (*_bfd_error_handler)
535 ("internal error: merge of architecture '%s' with architecture '%s' produced unknown architecture\n",
536 bfd_printable_name (obfd),
537 bfd_printable_name (ibfd));
538 bfd_set_error (bfd_error_bad_value);
542 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
543 sh_get_bfd_mach_from_arch_set (merged_arch));