* elfcpp.h (NT_VERSION, NT_ARCH): Define as enum constants.
[binutils.git] / bfd / cpu-sh.c
blobaf97a47367f54c17b6f0f65118f01c70943c3939
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 SH2_NEXT
67 32, /* 32 bits in a word. */
68 32, /* 32 bits in an address. */
69 8, /* 8 bits in a byte. */
70 bfd_arch_sh,
71 bfd_mach_sh2e,
72 "sh", /* Architecture name. */
73 "sh2e", /* Machine name. */
75 FALSE, /* Not the default. */
76 bfd_default_compatible,
77 bfd_default_scan,
78 SH2E_NEXT
81 32, /* 32 bits in a word. */
82 32, /* 32 bits in an address. */
83 8, /* 8 bits in a byte. */
84 bfd_arch_sh,
85 bfd_mach_sh_dsp,
86 "sh", /* Architecture name. */
87 "sh-dsp", /* Machine name. */
89 FALSE, /* Not the default. */
90 bfd_default_compatible,
91 bfd_default_scan,
92 SH_DSP_NEXT
95 32, /* 32 bits in a word. */
96 32, /* 32 bits in an address. */
97 8, /* 8 bits in a byte. */
98 bfd_arch_sh,
99 bfd_mach_sh3,
100 "sh", /* Architecture name. */
101 "sh3", /* Machine name. */
103 FALSE, /* Not the default. */
104 bfd_default_compatible,
105 bfd_default_scan,
106 SH3_NEXT
109 32, /* 32 bits in a word. */
110 32, /* 32 bits in an address. */
111 8, /* 8 bits in a byte. */
112 bfd_arch_sh,
113 bfd_mach_sh3_nommu,
114 "sh", /* Architecture name. */
115 "sh3-nommu", /* Machine name. */
117 FALSE, /* Not the default. */
118 bfd_default_compatible,
119 bfd_default_scan,
120 SH3_NOMMU_NEXT
123 32, /* 32 bits in a word. */
124 32, /* 32 bits in an address. */
125 8, /* 8 bits in a byte. */
126 bfd_arch_sh,
127 bfd_mach_sh3_dsp,
128 "sh", /* Architecture name. */
129 "sh3-dsp", /* Machine name. */
131 FALSE, /* Not the default. */
132 bfd_default_compatible,
133 bfd_default_scan,
134 SH3_DSP_NEXT
137 32, /* 32 bits in a word. */
138 32, /* 32 bits in an address. */
139 8, /* 8 bits in a byte. */
140 bfd_arch_sh,
141 bfd_mach_sh3e,
142 "sh", /* Architecture name. */
143 "sh3e", /* Machine name. */
145 FALSE, /* Not the default. */
146 bfd_default_compatible,
147 bfd_default_scan,
148 SH3E_NEXT
151 32, /* 32 bits in a word. */
152 32, /* 32 bits in an address. */
153 8, /* 8 bits in a byte. */
154 bfd_arch_sh,
155 bfd_mach_sh4,
156 "sh", /* Architecture name. */
157 "sh4", /* Machine name. */
159 FALSE, /* Not the default. */
160 bfd_default_compatible,
161 bfd_default_scan,
162 SH4_NEXT
165 32, /* 32 bits in a word. */
166 32, /* 32 bits in an address. */
167 8, /* 8 bits in a byte. */
168 bfd_arch_sh,
169 bfd_mach_sh4a,
170 "sh", /* Architecture name. */
171 "sh4a", /* Machine name. */
173 FALSE, /* Not the default. */
174 bfd_default_compatible,
175 bfd_default_scan,
176 SH4A_NEXT
179 32, /* 32 bits in a word. */
180 32, /* 32 bits in an address. */
181 8, /* 8 bits in a byte. */
182 bfd_arch_sh,
183 bfd_mach_sh4al_dsp,
184 "sh", /* Architecture name. */
185 "sh4al-dsp", /* Machine name. */
187 FALSE, /* Not the default. */
188 bfd_default_compatible,
189 bfd_default_scan,
190 SH4AL_DSP_NEXT
193 32, /* 32 bits in a word. */
194 32, /* 32 bits in an address. */
195 8, /* 8 bits in a byte. */
196 bfd_arch_sh,
197 bfd_mach_sh4_nofpu,
198 "sh", /* Architecture name. */
199 "sh4-nofpu", /* Machine name. */
201 FALSE, /* Not the default. */
202 bfd_default_compatible,
203 bfd_default_scan,
204 SH4_NOFPU_NEXT
207 32, /* 32 bits in a word. */
208 32, /* 32 bits in an address. */
209 8, /* 8 bits in a byte. */
210 bfd_arch_sh,
211 bfd_mach_sh4_nommu_nofpu,
212 "sh", /* Architecture name. */
213 "sh4-nommu-nofpu", /* Machine name. */
215 FALSE, /* Not the default. */
216 bfd_default_compatible,
217 bfd_default_scan,
218 SH4_NOMMU_NOFPU_NEXT
221 32, /* 32 bits in a word. */
222 32, /* 32 bits in an address. */
223 8, /* 8 bits in a byte. */
224 bfd_arch_sh,
225 bfd_mach_sh4a_nofpu,
226 "sh", /* Architecture name. */
227 "sh4a-nofpu", /* Machine name. */
229 FALSE, /* Not the default. */
230 bfd_default_compatible,
231 bfd_default_scan,
232 SH4A_NOFPU_NEXT
235 32, /* 32 bits in a word. */
236 32, /* 32 bits in an address. */
237 8, /* 8 bits in a byte. */
238 bfd_arch_sh,
239 bfd_mach_sh2a,
240 "sh", /* Architecture name. */
241 "sh2a", /* Machine name. */
243 FALSE, /* Not the default. */
244 bfd_default_compatible,
245 bfd_default_scan,
246 SH2A_NEXT
249 32, /* 32 bits in a word. */
250 32, /* 32 bits in an address. */
251 8, /* 8 bits in a byte. */
252 bfd_arch_sh,
253 bfd_mach_sh2a_nofpu,
254 "sh", /* Architecture name. */
255 "sh2a-nofpu", /* Machine name. */
257 FALSE, /* Not the default. */
258 bfd_default_compatible,
259 bfd_default_scan,
260 SH2A_NOFPU_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_or_sh4_nommu_nofpu,
268 "sh", /* Architecture name. */
269 "sh2a-nofpu-or-sh4-nommu-nofpu", /* Machine name. */
271 FALSE, /* Not the default. */
272 bfd_default_compatible,
273 bfd_default_scan,
274 SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT
277 32, /* 32 bits in a word. */
278 32, /* 32 bits in an address. */
279 8, /* 8 bits in a byte. */
280 bfd_arch_sh,
281 bfd_mach_sh2a_nofpu_or_sh3_nommu,
282 "sh", /* Architecture name. . */
283 "sh2a-nofpu-or-sh3-nommu", /* Machine name. */
285 FALSE, /* Not the default. */
286 bfd_default_compatible,
287 bfd_default_scan,
288 SH2A_NOFPU_OR_SH3_NOMMU_NEXT
291 32, /* 32 bits in a word. */
292 32, /* 32 bits in an address. */
293 8, /* 8 bits in a byte. */
294 bfd_arch_sh,
295 bfd_mach_sh2a_or_sh4,
296 "sh", /* Architecture name. */
297 "sh2a-or-sh4", /* Machine name. */
299 FALSE, /* Not the default. */
300 bfd_default_compatible,
301 bfd_default_scan,
302 SH2A_OR_SH4_NEXT
305 32, /* 32 bits in a word. */
306 32, /* 32 bits in an address. */
307 8, /* 8 bits in a byte. */
308 bfd_arch_sh,
309 bfd_mach_sh2a_or_sh3e,
310 "sh", /* Architecture name. */
311 "sh2a-or-sh3e", /* Machine name. */
313 FALSE, /* Not the default. */
314 bfd_default_compatible,
315 bfd_default_scan,
316 SH2A_OR_SH3E_NEXT
319 64, /* 64 bits in a word. */
320 64, /* 64 bits in an address. */
321 8, /* 8 bits in a byte. */
322 bfd_arch_sh,
323 bfd_mach_sh5,
324 "sh", /* Architecture name. */
325 "sh5", /* Machine name. */
327 FALSE, /* Not the default. */
328 bfd_default_compatible,
329 bfd_default_scan,
330 SH64_NEXT
334 const bfd_arch_info_type bfd_sh_arch =
336 32, /* 32 bits in a word. */
337 32, /* 32 bits in an address. */
338 8, /* 8 bits in a byte. */
339 bfd_arch_sh,
340 bfd_mach_sh,
341 "sh", /* Architecture name. */
342 "sh", /* Machine name. */
344 TRUE, /* The default machine. */
345 bfd_default_compatible,
346 bfd_default_scan,
347 SH_NEXT
351 /* This table defines the mappings from the BFD internal numbering
352 system to the opcodes internal flags system.
353 It is used by the functions defined below.
354 The prototypes for these SH specific functions are found in
355 sh-opc.h . */
357 static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
359 { bfd_mach_sh, arch_sh1, arch_sh_up },
360 { bfd_mach_sh2, arch_sh2, arch_sh2_up },
361 { bfd_mach_sh2e, arch_sh2e, arch_sh2e_up },
362 { bfd_mach_sh_dsp, arch_sh_dsp, arch_sh_dsp_up },
363 { bfd_mach_sh2a, arch_sh2a, arch_sh2a_up },
364 { bfd_mach_sh2a_nofpu, arch_sh2a_nofpu, arch_sh2a_nofpu_up },
366 { bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu_up },
367 { bfd_mach_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu_up },
368 { bfd_mach_sh2a_or_sh4, arch_sh2a_or_sh4, arch_sh2a_or_sh4_up },
369 { bfd_mach_sh2a_or_sh3e, arch_sh2a_or_sh3e, arch_sh2a_or_sh3e_up },
371 { bfd_mach_sh3, arch_sh3, arch_sh3_up },
372 { bfd_mach_sh3_nommu, arch_sh3_nommu, arch_sh3_nommu_up },
373 { bfd_mach_sh3_dsp, arch_sh3_dsp, arch_sh3_dsp_up },
374 { bfd_mach_sh3e, arch_sh3e, arch_sh3e_up },
375 { bfd_mach_sh4, arch_sh4, arch_sh4_up },
376 { bfd_mach_sh4a, arch_sh4a, arch_sh4a_up },
377 { bfd_mach_sh4al_dsp, arch_sh4al_dsp, arch_sh4al_dsp_up },
378 { bfd_mach_sh4_nofpu, arch_sh4_nofpu, arch_sh4_nofpu_up },
379 { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
380 { bfd_mach_sh4a_nofpu, arch_sh4a_nofpu, arch_sh4a_nofpu_up },
381 { 0, 0, 0 } /* Terminator. */
385 /* Convert a BFD mach number into the right opcodes arch flags
386 using the table above. */
388 unsigned int
389 sh_get_arch_from_bfd_mach (unsigned long mach)
391 int i = 0;
393 while (bfd_to_arch_table[i].bfd_mach != 0)
394 if (bfd_to_arch_table[i].bfd_mach == mach)
395 return bfd_to_arch_table[i].arch;
396 else
397 i++;
399 /* Machine not found. */
400 BFD_FAIL();
402 return SH_ARCH_UNKNOWN_ARCH;
406 /* Convert a BFD mach number into a set of opcodes arch flags
407 describing all the compatible architectures (i.e. arch_up)
408 using the table above. */
410 unsigned int
411 sh_get_arch_up_from_bfd_mach (unsigned long mach)
413 int i = 0;
415 while (bfd_to_arch_table[i].bfd_mach != 0)
416 if (bfd_to_arch_table[i].bfd_mach == mach)
417 return bfd_to_arch_table[i].arch_up;
418 else
419 i++;
421 /* Machine not found. */
422 BFD_FAIL();
424 return SH_ARCH_UNKNOWN_ARCH;
428 /* Convert an arbitary arch_set - not necessarily corresponding
429 directly to anything in the table above - to the most generic
430 architecture which supports all the required features, and
431 return the corresponding BFD mach. */
433 unsigned long
434 sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
436 unsigned long result = 0;
437 unsigned int best = ~arch_set;
438 unsigned int co_mask = ~0;
439 int i = 0;
441 /* If arch_set permits variants with no coprocessor then do not allow
442 the other irrelevant co-processor bits to influence the choice:
443 e.g. if dsp is disallowed by arch_set, then the algorithm would
444 prefer fpu variants over nofpu variants because they also disallow
445 dsp - even though the nofpu would be the most correct choice.
446 This assumes that EVERY fpu/dsp variant has a no-coprocessor
447 counter-part, or their non-fpu/dsp instructions do not have the
448 no co-processor bit set. */
449 if (arch_set & arch_sh_no_co)
450 co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
452 while (bfd_to_arch_table[i].bfd_mach != 0)
454 unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
456 /* Conceptually: Find the architecture with the least number
457 of extra features or, if they have the same number, then
458 the greatest number of required features. Disregard
459 architectures where the required features alone do
460 not describe a valid architecture. */
461 if (((try & ~arch_set) < (best & ~arch_set)
462 || ((try & ~arch_set) == (best & ~arch_set)
463 && (~try & arch_set) < (~best & arch_set)))
464 && SH_MERGE_ARCH_SET_VALID (try, arch_set))
466 result = bfd_to_arch_table[i].bfd_mach;
467 best = try;
470 i++;
473 /* This might happen if a new variant is added to sh-opc.h
474 but no corresponding entry is added to the table above. */
475 BFD_ASSERT (result != 0);
477 return result;
481 /* Merge the architecture type of two BFD files, such that the
482 resultant architecture supports all the features required
483 by the two input BFDs.
484 If the input BFDs are multually incompatible - i.e. one uses
485 DSP while the other uses FPU - or there is no known architecture
486 that fits the requirements then an error is emitted. */
488 bfd_boolean
489 sh_merge_bfd_arch (bfd *ibfd, bfd *obfd)
491 unsigned int old_arch, new_arch, merged_arch;
493 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
494 return FALSE;
496 old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
497 new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
499 merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
501 if (!SH_VALID_CO_ARCH_SET (merged_arch))
503 (*_bfd_error_handler)
504 ("%B: uses %s instructions while previous modules use %s instructions",
505 ibfd,
506 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
507 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
508 bfd_set_error (bfd_error_bad_value);
509 return FALSE;
511 else if (!SH_VALID_ARCH_SET (merged_arch))
513 (*_bfd_error_handler)
514 ("internal error: merge of architecture '%s' with architecture '%s' produced unknown architecture\n",
515 bfd_printable_name (obfd),
516 bfd_printable_name (ibfd));
517 bfd_set_error (bfd_error_bad_value);
518 return FALSE;
521 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
522 sh_get_bfd_mach_from_arch_set (merged_arch));
524 return TRUE;