daily update
[binutils.git] / bfd / cpu-sh.c
blob6fe6c155274b08696fd8a7c3bb201a46da037a69
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. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
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. */
56 bfd_arch_sh,
57 bfd_mach_sh2,
58 "sh", /* Architecture name. */
59 "sh2", /* Machine name. */
61 FALSE, /* Not the default. */
62 bfd_default_compatible,
63 bfd_default_scan,
64 bfd_arch_default_fill,
65 SH2_NEXT
68 32, /* 32 bits in a word. */
69 32, /* 32 bits in an address. */
70 8, /* 8 bits in a byte. */
71 bfd_arch_sh,
72 bfd_mach_sh2e,
73 "sh", /* Architecture name. */
74 "sh2e", /* Machine name. */
76 FALSE, /* Not the default. */
77 bfd_default_compatible,
78 bfd_default_scan,
79 bfd_arch_default_fill,
80 SH2E_NEXT
83 32, /* 32 bits in a word. */
84 32, /* 32 bits in an address. */
85 8, /* 8 bits in a byte. */
86 bfd_arch_sh,
87 bfd_mach_sh_dsp,
88 "sh", /* Architecture name. */
89 "sh-dsp", /* Machine name. */
91 FALSE, /* Not the default. */
92 bfd_default_compatible,
93 bfd_default_scan,
94 bfd_arch_default_fill,
95 SH_DSP_NEXT
98 32, /* 32 bits in a word. */
99 32, /* 32 bits in an address. */
100 8, /* 8 bits in a byte. */
101 bfd_arch_sh,
102 bfd_mach_sh3,
103 "sh", /* Architecture name. */
104 "sh3", /* Machine name. */
106 FALSE, /* Not the default. */
107 bfd_default_compatible,
108 bfd_default_scan,
109 bfd_arch_default_fill,
110 SH3_NEXT
113 32, /* 32 bits in a word. */
114 32, /* 32 bits in an address. */
115 8, /* 8 bits in a byte. */
116 bfd_arch_sh,
117 bfd_mach_sh3_nommu,
118 "sh", /* Architecture name. */
119 "sh3-nommu", /* Machine name. */
121 FALSE, /* Not the default. */
122 bfd_default_compatible,
123 bfd_default_scan,
124 bfd_arch_default_fill,
125 SH3_NOMMU_NEXT
128 32, /* 32 bits in a word. */
129 32, /* 32 bits in an address. */
130 8, /* 8 bits in a byte. */
131 bfd_arch_sh,
132 bfd_mach_sh3_dsp,
133 "sh", /* Architecture name. */
134 "sh3-dsp", /* Machine name. */
136 FALSE, /* Not the default. */
137 bfd_default_compatible,
138 bfd_default_scan,
139 bfd_arch_default_fill,
140 SH3_DSP_NEXT
143 32, /* 32 bits in a word. */
144 32, /* 32 bits in an address. */
145 8, /* 8 bits in a byte. */
146 bfd_arch_sh,
147 bfd_mach_sh3e,
148 "sh", /* Architecture name. */
149 "sh3e", /* Machine name. */
151 FALSE, /* Not the default. */
152 bfd_default_compatible,
153 bfd_default_scan,
154 bfd_arch_default_fill,
155 SH3E_NEXT
158 32, /* 32 bits in a word. */
159 32, /* 32 bits in an address. */
160 8, /* 8 bits in a byte. */
161 bfd_arch_sh,
162 bfd_mach_sh4,
163 "sh", /* Architecture name. */
164 "sh4", /* Machine name. */
166 FALSE, /* Not the default. */
167 bfd_default_compatible,
168 bfd_default_scan,
169 bfd_arch_default_fill,
170 SH4_NEXT
173 32, /* 32 bits in a word. */
174 32, /* 32 bits in an address. */
175 8, /* 8 bits in a byte. */
176 bfd_arch_sh,
177 bfd_mach_sh4a,
178 "sh", /* Architecture name. */
179 "sh4a", /* Machine name. */
181 FALSE, /* Not the default. */
182 bfd_default_compatible,
183 bfd_default_scan,
184 bfd_arch_default_fill,
185 SH4A_NEXT
188 32, /* 32 bits in a word. */
189 32, /* 32 bits in an address. */
190 8, /* 8 bits in a byte. */
191 bfd_arch_sh,
192 bfd_mach_sh4al_dsp,
193 "sh", /* Architecture name. */
194 "sh4al-dsp", /* Machine name. */
196 FALSE, /* Not the default. */
197 bfd_default_compatible,
198 bfd_default_scan,
199 bfd_arch_default_fill,
200 SH4AL_DSP_NEXT
203 32, /* 32 bits in a word. */
204 32, /* 32 bits in an address. */
205 8, /* 8 bits in a byte. */
206 bfd_arch_sh,
207 bfd_mach_sh4_nofpu,
208 "sh", /* Architecture name. */
209 "sh4-nofpu", /* Machine name. */
211 FALSE, /* Not the default. */
212 bfd_default_compatible,
213 bfd_default_scan,
214 bfd_arch_default_fill,
215 SH4_NOFPU_NEXT
218 32, /* 32 bits in a word. */
219 32, /* 32 bits in an address. */
220 8, /* 8 bits in a byte. */
221 bfd_arch_sh,
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,
228 bfd_default_scan,
229 bfd_arch_default_fill,
230 SH4_NOMMU_NOFPU_NEXT
233 32, /* 32 bits in a word. */
234 32, /* 32 bits in an address. */
235 8, /* 8 bits in a byte. */
236 bfd_arch_sh,
237 bfd_mach_sh4a_nofpu,
238 "sh", /* Architecture name. */
239 "sh4a-nofpu", /* Machine name. */
241 FALSE, /* Not the default. */
242 bfd_default_compatible,
243 bfd_default_scan,
244 bfd_arch_default_fill,
245 SH4A_NOFPU_NEXT
248 32, /* 32 bits in a word. */
249 32, /* 32 bits in an address. */
250 8, /* 8 bits in a byte. */
251 bfd_arch_sh,
252 bfd_mach_sh2a,
253 "sh", /* Architecture name. */
254 "sh2a", /* Machine name. */
256 FALSE, /* Not the default. */
257 bfd_default_compatible,
258 bfd_default_scan,
259 bfd_arch_default_fill,
260 SH2A_NEXT
263 32, /* 32 bits in a word. */
264 32, /* 32 bits in an address. */
265 8, /* 8 bits in a byte. */
266 bfd_arch_sh,
267 bfd_mach_sh2a_nofpu,
268 "sh", /* Architecture name. */
269 "sh2a-nofpu", /* Machine name. */
271 FALSE, /* Not the default. */
272 bfd_default_compatible,
273 bfd_default_scan,
274 bfd_arch_default_fill,
275 SH2A_NOFPU_NEXT
278 32, /* 32 bits in a word. */
279 32, /* 32 bits in an address. */
280 8, /* 8 bits in a byte. */
281 bfd_arch_sh,
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,
288 bfd_default_scan,
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. */
296 bfd_arch_sh,
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,
303 bfd_default_scan,
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. */
311 bfd_arch_sh,
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,
318 bfd_default_scan,
319 bfd_arch_default_fill,
320 SH2A_OR_SH4_NEXT
323 32, /* 32 bits in a word. */
324 32, /* 32 bits in an address. */
325 8, /* 8 bits in a byte. */
326 bfd_arch_sh,
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,
333 bfd_default_scan,
334 bfd_arch_default_fill,
335 SH2A_OR_SH3E_NEXT
338 64, /* 64 bits in a word. */
339 64, /* 64 bits in an address. */
340 8, /* 8 bits in a byte. */
341 bfd_arch_sh,
342 bfd_mach_sh5,
343 "sh", /* Architecture name. */
344 "sh5", /* Machine name. */
346 FALSE, /* Not the default. */
347 bfd_default_compatible,
348 bfd_default_scan,
349 bfd_arch_default_fill,
350 SH64_NEXT
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. */
359 bfd_arch_sh,
360 bfd_mach_sh,
361 "sh", /* Architecture name. */
362 "sh", /* Machine name. */
364 TRUE, /* The default machine. */
365 bfd_default_compatible,
366 bfd_default_scan,
367 bfd_arch_default_fill,
368 SH_NEXT
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
376 sh-opc.h . */
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. */
409 unsigned int
410 sh_get_arch_from_bfd_mach (unsigned long mach)
412 int i = 0;
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;
417 else
418 i++;
420 /* Machine not found. */
421 BFD_FAIL();
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. */
431 unsigned int
432 sh_get_arch_up_from_bfd_mach (unsigned long mach)
434 int i = 0;
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;
439 else
440 i++;
442 /* Machine not found. */
443 BFD_FAIL();
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. */
454 unsigned long
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;
460 int i = 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;
488 best = try;
491 i++;
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);
498 return result;
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. */
509 bfd_boolean
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))
515 return FALSE;
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",
526 ibfd,
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);
530 return FALSE;
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);
539 return FALSE;
542 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
543 sh_get_bfd_mach_from_arch_set (merged_arch));
545 return TRUE;