* configure.host (ia64-*-hpux*): Set _LARGEFILE64_SOURCE.
[binutils.git] / bfd / cpu-sh.c
blob531431c1dcddf8b6cbe0e03567ac902c0c16f2af
1 /* BFD library support routines for the Renesas / SuperH SH architecture.
2 Copyright 1993, 1994, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005
3 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 2 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, MA 02110-1301, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
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. */
55 bfd_arch_sh,
56 bfd_mach_sh2,
57 "sh", /* Architecture name. */
58 "sh2", /* Machine name. */
60 FALSE, /* Not the default. */
61 bfd_default_compatible,
62 bfd_default_scan,
63 SH2_NEXT
66 32, /* 32 bits in a word. */
67 32, /* 32 bits in an address. */
68 8, /* 8 bits in a byte. */
69 bfd_arch_sh,
70 bfd_mach_sh2e,
71 "sh", /* Architecture name. */
72 "sh2e", /* Machine name. */
74 FALSE, /* Not the default. */
75 bfd_default_compatible,
76 bfd_default_scan,
77 SH2E_NEXT
80 32, /* 32 bits in a word. */
81 32, /* 32 bits in an address. */
82 8, /* 8 bits in a byte. */
83 bfd_arch_sh,
84 bfd_mach_sh_dsp,
85 "sh", /* Architecture name. */
86 "sh-dsp", /* Machine name. */
88 FALSE, /* Not the default. */
89 bfd_default_compatible,
90 bfd_default_scan,
91 SH_DSP_NEXT
94 32, /* 32 bits in a word. */
95 32, /* 32 bits in an address. */
96 8, /* 8 bits in a byte. */
97 bfd_arch_sh,
98 bfd_mach_sh3,
99 "sh", /* Architecture name. */
100 "sh3", /* Machine name. */
102 FALSE, /* Not the default. */
103 bfd_default_compatible,
104 bfd_default_scan,
105 SH3_NEXT
108 32, /* 32 bits in a word. */
109 32, /* 32 bits in an address. */
110 8, /* 8 bits in a byte. */
111 bfd_arch_sh,
112 bfd_mach_sh3_nommu,
113 "sh", /* Architecture name. */
114 "sh3-nommu", /* Machine name. */
116 FALSE, /* Not the default. */
117 bfd_default_compatible,
118 bfd_default_scan,
119 SH3_NOMMU_NEXT
122 32, /* 32 bits in a word. */
123 32, /* 32 bits in an address. */
124 8, /* 8 bits in a byte. */
125 bfd_arch_sh,
126 bfd_mach_sh3_dsp,
127 "sh", /* Architecture name. */
128 "sh3-dsp", /* Machine name. */
130 FALSE, /* Not the default. */
131 bfd_default_compatible,
132 bfd_default_scan,
133 SH3_DSP_NEXT
136 32, /* 32 bits in a word. */
137 32, /* 32 bits in an address. */
138 8, /* 8 bits in a byte. */
139 bfd_arch_sh,
140 bfd_mach_sh3e,
141 "sh", /* Architecture name. */
142 "sh3e", /* Machine name. */
144 FALSE, /* Not the default. */
145 bfd_default_compatible,
146 bfd_default_scan,
147 SH3E_NEXT
150 32, /* 32 bits in a word. */
151 32, /* 32 bits in an address. */
152 8, /* 8 bits in a byte. */
153 bfd_arch_sh,
154 bfd_mach_sh4,
155 "sh", /* Architecture name. */
156 "sh4", /* Machine name. */
158 FALSE, /* Not the default. */
159 bfd_default_compatible,
160 bfd_default_scan,
161 SH4_NEXT
164 32, /* 32 bits in a word. */
165 32, /* 32 bits in an address. */
166 8, /* 8 bits in a byte. */
167 bfd_arch_sh,
168 bfd_mach_sh4a,
169 "sh", /* Architecture name. */
170 "sh4a", /* Machine name. */
172 FALSE, /* Not the default. */
173 bfd_default_compatible,
174 bfd_default_scan,
175 SH4A_NEXT
178 32, /* 32 bits in a word. */
179 32, /* 32 bits in an address. */
180 8, /* 8 bits in a byte. */
181 bfd_arch_sh,
182 bfd_mach_sh4al_dsp,
183 "sh", /* Architecture name. */
184 "sh4al-dsp", /* Machine name. */
186 FALSE, /* Not the default. */
187 bfd_default_compatible,
188 bfd_default_scan,
189 SH4AL_DSP_NEXT
192 32, /* 32 bits in a word. */
193 32, /* 32 bits in an address. */
194 8, /* 8 bits in a byte. */
195 bfd_arch_sh,
196 bfd_mach_sh4_nofpu,
197 "sh", /* Architecture name. */
198 "sh4-nofpu", /* Machine name. */
200 FALSE, /* Not the default. */
201 bfd_default_compatible,
202 bfd_default_scan,
203 SH4_NOFPU_NEXT
206 32, /* 32 bits in a word. */
207 32, /* 32 bits in an address. */
208 8, /* 8 bits in a byte. */
209 bfd_arch_sh,
210 bfd_mach_sh4_nommu_nofpu,
211 "sh", /* Architecture name. */
212 "sh4-nommu-nofpu", /* Machine name. */
214 FALSE, /* Not the default. */
215 bfd_default_compatible,
216 bfd_default_scan,
217 SH4_NOMMU_NOFPU_NEXT
220 32, /* 32 bits in a word. */
221 32, /* 32 bits in an address. */
222 8, /* 8 bits in a byte. */
223 bfd_arch_sh,
224 bfd_mach_sh4a_nofpu,
225 "sh", /* Architecture name. */
226 "sh4a-nofpu", /* Machine name. */
228 FALSE, /* Not the default. */
229 bfd_default_compatible,
230 bfd_default_scan,
231 SH4A_NOFPU_NEXT
234 32, /* 32 bits in a word. */
235 32, /* 32 bits in an address. */
236 8, /* 8 bits in a byte. */
237 bfd_arch_sh,
238 bfd_mach_sh2a,
239 "sh", /* Architecture name. */
240 "sh2a", /* Machine name. */
242 FALSE, /* Not the default. */
243 bfd_default_compatible,
244 bfd_default_scan,
245 SH2A_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_nofpu,
253 "sh", /* Architecture name. */
254 "sh2a-nofpu", /* Machine name. */
256 FALSE, /* Not the default. */
257 bfd_default_compatible,
258 bfd_default_scan,
259 SH2A_NOFPU_NEXT
262 32, /* 32 bits in a word. */
263 32, /* 32 bits in an address. */
264 8, /* 8 bits in a byte. */
265 bfd_arch_sh,
266 bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu,
267 "sh", /* Architecture name. */
268 "sh2a-nofpu-or-sh4-nommu-nofpu", /* Machine name. */
270 FALSE, /* Not the default. */
271 bfd_default_compatible,
272 bfd_default_scan,
273 SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT
276 32, /* 32 bits in a word. */
277 32, /* 32 bits in an address. */
278 8, /* 8 bits in a byte. */
279 bfd_arch_sh,
280 bfd_mach_sh2a_nofpu_or_sh3_nommu,
281 "sh", /* Architecture name. . */
282 "sh2a-nofpu-or-sh3-nommu", /* Machine name. */
284 FALSE, /* Not the default. */
285 bfd_default_compatible,
286 bfd_default_scan,
287 SH2A_NOFPU_OR_SH3_NOMMU_NEXT
290 32, /* 32 bits in a word. */
291 32, /* 32 bits in an address. */
292 8, /* 8 bits in a byte. */
293 bfd_arch_sh,
294 bfd_mach_sh2a_or_sh4,
295 "sh", /* Architecture name. */
296 "sh2a-or-sh4", /* Machine name. */
298 FALSE, /* Not the default. */
299 bfd_default_compatible,
300 bfd_default_scan,
301 SH2A_OR_SH4_NEXT
304 32, /* 32 bits in a word. */
305 32, /* 32 bits in an address. */
306 8, /* 8 bits in a byte. */
307 bfd_arch_sh,
308 bfd_mach_sh2a_or_sh3e,
309 "sh", /* Architecture name. */
310 "sh2a-or-sh3e", /* Machine name. */
312 FALSE, /* Not the default. */
313 bfd_default_compatible,
314 bfd_default_scan,
315 SH2A_OR_SH3E_NEXT
318 64, /* 64 bits in a word. */
319 64, /* 64 bits in an address. */
320 8, /* 8 bits in a byte. */
321 bfd_arch_sh,
322 bfd_mach_sh5,
323 "sh", /* Architecture name. */
324 "sh5", /* Machine name. */
326 FALSE, /* Not the default. */
327 bfd_default_compatible,
328 bfd_default_scan,
329 SH64_NEXT
333 const bfd_arch_info_type bfd_sh_arch =
335 32, /* 32 bits in a word. */
336 32, /* 32 bits in an address. */
337 8, /* 8 bits in a byte. */
338 bfd_arch_sh,
339 bfd_mach_sh,
340 "sh", /* Architecture name. */
341 "sh", /* Machine name. */
343 TRUE, /* The default machine. */
344 bfd_default_compatible,
345 bfd_default_scan,
346 SH_NEXT
350 /* This table defines the mappings from the BFD internal numbering
351 system to the opcodes internal flags system.
352 It is used by the functions defined below.
353 The prototypes for these SH specific functions are found in
354 sh-opc.h . */
356 static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
358 { bfd_mach_sh, arch_sh1, arch_sh_up },
359 { bfd_mach_sh2, arch_sh2, arch_sh2_up },
360 { bfd_mach_sh2e, arch_sh2e, arch_sh2e_up },
361 { bfd_mach_sh_dsp, arch_sh_dsp, arch_sh_dsp_up },
362 { bfd_mach_sh2a, arch_sh2a, arch_sh2a_up },
363 { bfd_mach_sh2a_nofpu, arch_sh2a_nofpu, arch_sh2a_nofpu_up },
365 { bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu, arch_sh2a_nofpu_or_sh4_nommu_nofpu_up },
366 { bfd_mach_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu, arch_sh2a_nofpu_or_sh3_nommu_up },
367 { bfd_mach_sh2a_or_sh4, arch_sh2a_or_sh4, arch_sh2a_or_sh4_up },
368 { bfd_mach_sh2a_or_sh3e, arch_sh2a_or_sh3e, arch_sh2a_or_sh3e_up },
370 { bfd_mach_sh3, arch_sh3, arch_sh3_up },
371 { bfd_mach_sh3_nommu, arch_sh3_nommu, arch_sh3_nommu_up },
372 { bfd_mach_sh3_dsp, arch_sh3_dsp, arch_sh3_dsp_up },
373 { bfd_mach_sh3e, arch_sh3e, arch_sh3e_up },
374 { bfd_mach_sh4, arch_sh4, arch_sh4_up },
375 { bfd_mach_sh4a, arch_sh4a, arch_sh4a_up },
376 { bfd_mach_sh4al_dsp, arch_sh4al_dsp, arch_sh4al_dsp_up },
377 { bfd_mach_sh4_nofpu, arch_sh4_nofpu, arch_sh4_nofpu_up },
378 { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
379 { bfd_mach_sh4a_nofpu, arch_sh4a_nofpu, arch_sh4a_nofpu_up },
380 { 0, 0, 0 } /* Terminator. */
384 /* Convert a BFD mach number into the right opcodes arch flags
385 using the table above. */
387 unsigned int
388 sh_get_arch_from_bfd_mach (unsigned long mach)
390 int i = 0;
392 while (bfd_to_arch_table[i].bfd_mach != 0)
393 if (bfd_to_arch_table[i].bfd_mach == mach)
394 return bfd_to_arch_table[i].arch;
395 else
396 i++;
398 /* Machine not found. */
399 BFD_FAIL();
401 return SH_ARCH_UNKNOWN_ARCH;
405 /* Convert a BFD mach number into a set of opcodes arch flags
406 describing all the compatible architectures (i.e. arch_up)
407 using the table above. */
409 unsigned int
410 sh_get_arch_up_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_up;
417 else
418 i++;
420 /* Machine not found. */
421 BFD_FAIL();
423 return SH_ARCH_UNKNOWN_ARCH;
427 /* Convert an arbitary arch_set - not necessarily corresponding
428 directly to anything in the table above - to the most generic
429 architecture which supports all the required features, and
430 return the corresponding BFD mach. */
432 unsigned long
433 sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
435 unsigned long result = 0;
436 unsigned int best = ~arch_set;
437 unsigned int co_mask = ~0;
438 int i = 0;
440 /* If arch_set permits variants with no coprocessor then do not allow
441 the other irrelevant co-processor bits to influence the choice:
442 e.g. if dsp is disallowed by arch_set, then the algorithm would
443 prefer fpu variants over nofpu variants because they also disallow
444 dsp - even though the nofpu would be the most correct choice.
445 This assumes that EVERY fpu/dsp variant has a no-coprocessor
446 counter-part, or their non-fpu/dsp instructions do not have the
447 no co-processor bit set. */
448 if (arch_set & arch_sh_no_co)
449 co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
451 while (bfd_to_arch_table[i].bfd_mach != 0)
453 unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
455 /* Conceptually: Find the architecture with the least number
456 of extra features or, if they have the same number, then
457 the greatest number of required features. Disregard
458 architectures where the required features alone do
459 not describe a valid architecture. */
460 if (((try & ~arch_set) < (best & ~arch_set)
461 || ((try & ~arch_set) == (best & ~arch_set)
462 && (~try & arch_set) < (~best & arch_set)))
463 && SH_MERGE_ARCH_SET_VALID (try, arch_set))
465 result = bfd_to_arch_table[i].bfd_mach;
466 best = try;
469 i++;
472 /* This might happen if a new variant is added to sh-opc.h
473 but no corresponding entry is added to the table above. */
474 BFD_ASSERT (result != 0);
476 return result;
480 /* Merge the architecture type of two BFD files, such that the
481 resultant architecture supports all the features required
482 by the two input BFDs.
483 If the input BFDs are multually incompatible - i.e. one uses
484 DSP while the other uses FPU - or there is no known architecture
485 that fits the requirements then an error is emitted. */
487 bfd_boolean
488 sh_merge_bfd_arch (bfd *ibfd, bfd *obfd)
490 unsigned int old_arch, new_arch, merged_arch;
492 if (! _bfd_generic_verify_endian_match (ibfd, obfd))
493 return FALSE;
495 old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
496 new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
498 merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
500 if (!SH_VALID_CO_ARCH_SET (merged_arch))
502 (*_bfd_error_handler)
503 ("%B: uses %s instructions while previous modules use %s instructions",
504 ibfd,
505 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
506 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
507 bfd_set_error (bfd_error_bad_value);
508 return FALSE;
510 else if (!SH_VALID_ARCH_SET (merged_arch))
512 (*_bfd_error_handler)
513 ("internal error: merge of architecture '%s' with architecture '%s' produced unknown architecture\n",
514 bfd_printable_name (obfd),
515 bfd_printable_name (ibfd));
516 bfd_set_error (bfd_error_bad_value);
517 return FALSE;
520 bfd_default_set_arch_mach (obfd, bfd_arch_sh,
521 sh_get_bfd_mach_from_arch_set (merged_arch));
523 return TRUE;