1/* BFD library support routines for the Renesas / SuperH SH architecture.
2   Copyright 1993, 1994, 1997, 1998, 2000, 2001, 2002, 2003, 2004
3   Free Software Foundation, Inc.
4   Hacked by Steve Chamberlain of Cygnus Support.
5
6   This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22#include "bfd.h"
23#include "sysdep.h"
24#include "libbfd.h"
25#include "../opcodes/sh-opc.h"
26
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 SH64_NEXT    NULL
44
45static const bfd_arch_info_type arch_info_struct[] =
46{
47  {
48    32,				/* 32 bits in a word */
49    32,				/* 32 bits in an address */
50    8,				/* 8 bits in a byte */
51    bfd_arch_sh,
52    bfd_mach_sh2,
53    "sh",			/* arch_name  */
54    "sh2",			/* printable name */
55    1,
56    FALSE,			/* not the default */
57    bfd_default_compatible,
58    bfd_default_scan,
59    SH2_NEXT
60  },
61  {
62    32,				/* 32 bits in a word */
63    32,				/* 32 bits in an address */
64    8,				/* 8 bits in a byte */
65    bfd_arch_sh,
66    bfd_mach_sh2e,
67    "sh",			/* arch_name  */
68    "sh2e",			/* printable name */
69    1,
70    FALSE,			/* not the default */
71    bfd_default_compatible,
72    bfd_default_scan,
73    SH2E_NEXT
74  },
75  {
76    32,				/* 32 bits in a word */
77    32,				/* 32 bits in an address */
78    8,				/* 8 bits in a byte */
79    bfd_arch_sh,
80    bfd_mach_sh_dsp,
81    "sh",			/* arch_name  */
82    "sh-dsp",			/* printable name */
83    1,
84    FALSE,			/* not the default */
85    bfd_default_compatible,
86    bfd_default_scan,
87    SH_DSP_NEXT
88  },
89  {
90    32,				/* 32 bits in a word */
91    32,				/* 32 bits in an address */
92    8,				/* 8 bits in a byte */
93    bfd_arch_sh,
94    bfd_mach_sh3,
95    "sh",			/* arch_name  */
96    "sh3",			/* printable name */
97    1,
98    FALSE,			/* not the default */
99    bfd_default_compatible,
100    bfd_default_scan,
101    SH3_NEXT
102  },
103  {
104    32,				/* 32 bits in a word */
105    32,				/* 32 bits in an address */
106    8,				/* 8 bits in a byte */
107    bfd_arch_sh,
108    bfd_mach_sh3_nommu,
109    "sh",			/* arch_name  */
110    "sh3-nommu",		/* printable name */
111    1,
112    FALSE,			/* not the default */
113    bfd_default_compatible,
114    bfd_default_scan,
115    SH3_NOMMU_NEXT
116  },
117  {
118    32,				/* 32 bits in a word */
119    32,				/* 32 bits in an address */
120    8,				/* 8 bits in a byte */
121    bfd_arch_sh,
122    bfd_mach_sh3_dsp,
123    "sh",			/* arch_name  */
124    "sh3-dsp",			/* printable name */
125    1,
126    FALSE,			/* not the default */
127    bfd_default_compatible,
128    bfd_default_scan,
129    SH3_DSP_NEXT
130  },
131  {
132    32,				/* 32 bits in a word */
133    32,				/* 32 bits in an address */
134    8,				/* 8 bits in a byte */
135    bfd_arch_sh,
136    bfd_mach_sh3e,
137    "sh",			/* arch_name  */
138    "sh3e",			/* printable name */
139    1,
140    FALSE,			/* not the default */
141    bfd_default_compatible,
142    bfd_default_scan,
143    SH3E_NEXT
144  },
145  {
146    32,				/* 32 bits in a word */
147    32,				/* 32 bits in an address */
148    8,				/* 8 bits in a byte */
149    bfd_arch_sh,
150    bfd_mach_sh4,
151    "sh",			/* arch_name  */
152    "sh4",			/* printable name */
153    1,
154    FALSE,			/* not the default */
155    bfd_default_compatible,
156    bfd_default_scan,
157    SH4_NEXT
158  },
159  {
160    32,				/* 32 bits in a word */
161    32,				/* 32 bits in an address */
162    8,				/* 8 bits in a byte */
163    bfd_arch_sh,
164    bfd_mach_sh4a,
165    "sh",			/* arch_name  */
166    "sh4a",			/* printable name */
167    1,
168    FALSE,			/* not the default */
169    bfd_default_compatible,
170    bfd_default_scan,
171    SH4A_NEXT
172  },
173  {
174    32,				/* 32 bits in a word */
175    32,				/* 32 bits in an address */
176    8,				/* 8 bits in a byte */
177    bfd_arch_sh,
178    bfd_mach_sh4al_dsp,
179    "sh",			/* arch_name  */
180    "sh4al-dsp",		/* printable name */
181    1,
182    FALSE,			/* not the default */
183    bfd_default_compatible,
184    bfd_default_scan,
185    SH4AL_DSP_NEXT
186  },
187  {
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_sh4_nofpu,
193    "sh",			/* arch_name  */
194    "sh4-nofpu",		/* printable name */
195    1,
196    FALSE,			/* not the default */
197    bfd_default_compatible,
198    bfd_default_scan,
199    SH4_NOFPU_NEXT
200  },
201  {
202    32,				/* 32 bits in a word */
203    32,				/* 32 bits in an address */
204    8,				/* 8 bits in a byte */
205    bfd_arch_sh,
206    bfd_mach_sh4_nommu_nofpu,
207    "sh",			/* arch_name  */
208    "sh4-nommu-nofpu",		/* printable name */
209    1,
210    FALSE,			/* not the default */
211    bfd_default_compatible,
212    bfd_default_scan,
213    SH4_NOMMU_NOFPU_NEXT
214  },
215  {
216    32,				/* 32 bits in a word */
217    32,				/* 32 bits in an address */
218    8,				/* 8 bits in a byte */
219    bfd_arch_sh,
220    bfd_mach_sh4a_nofpu,
221    "sh",			/* arch_name  */
222    "sh4a-nofpu",		/* printable name */
223    1,
224    FALSE,			/* not the default */
225    bfd_default_compatible,
226    bfd_default_scan,
227    SH4A_NOFPU_NEXT
228  },
229  {
230    32,				/* 32 bits in a word.  */
231    32,				/* 32 bits in an address.  */
232    8,				/* 8 bits in a byte.  */
233    bfd_arch_sh,
234    bfd_mach_sh2a,
235    "sh",			/* Arch_name.  */
236    "sh2a",			/* Printable name.  */
237    1,
238    FALSE,			/* Not the default.  */
239    bfd_default_compatible,
240    bfd_default_scan,
241    SH2A_NEXT
242  },
243  {
244    32,				/* 32 bits in a word.  */
245    32,				/* 32 bits in an address.  */
246    8,				/* 8 bits in a byte.  */
247    bfd_arch_sh,
248    bfd_mach_sh2a_nofpu,
249    "sh",			/* Arch_name.  */
250    "sh2a-nofpu",		/* Printable name.  */
251    1,
252    FALSE,			/* Not the default.  */
253    bfd_default_compatible,
254    bfd_default_scan,
255    SH2A_NOFPU_NEXT
256  },
257  {
258    64,				/* 64 bits in a word */
259    64,				/* 64 bits in an address */
260    8,				/* 8 bits in a byte */
261    bfd_arch_sh,
262    bfd_mach_sh5,
263    "sh",			/* arch_name  */
264    "sh5",			/* printable name */
265    1,
266    FALSE,			/* not the default */
267    bfd_default_compatible,
268    bfd_default_scan,
269    SH64_NEXT
270  },
271};
272
273const bfd_arch_info_type bfd_sh_arch =
274{
275  32,				/* 32 bits in a word */
276  32,				/* 32 bits in an address */
277  8,				/* 8 bits in a byte */
278  bfd_arch_sh,
279  bfd_mach_sh,
280  "sh",				/* arch_name  */
281  "sh",				/* printable name */
282  1,
283  TRUE,				/* the default machine */
284  bfd_default_compatible,
285  bfd_default_scan,
286  SH_NEXT
287};
288
289
290/* This table defines the mappings from the BFD internal numbering
291   system to the opcodes internal flags system.
292   It is used by the functions defined below.
293   The prototypes for these SH specific functions are found in
294   sh-opc.h .  */
295
296static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
297{
298  { bfd_mach_sh,              arch_sh1,             arch_sh1_up },
299  { bfd_mach_sh2,             arch_sh2,             arch_sh2_up },
300  { bfd_mach_sh2e,            arch_sh2e,            arch_sh2e_up },
301  { bfd_mach_sh_dsp,          arch_sh_dsp,          arch_sh_dsp_up },
302  { bfd_mach_sh2a,            arch_sh2a,            arch_sh2a_up },
303  { bfd_mach_sh2a_nofpu,      arch_sh2a_nofpu,      arch_sh2a_nofpu_up },
304  { bfd_mach_sh3,             arch_sh3,             arch_sh3_up },
305  { bfd_mach_sh3_nommu,       arch_sh3_nommu,       arch_sh3_nommu_up },
306  { bfd_mach_sh3_dsp,         arch_sh3_dsp,         arch_sh3_dsp_up },
307  { bfd_mach_sh3e,            arch_sh3e,            arch_sh3e_up },
308  { bfd_mach_sh4,             arch_sh4,             arch_sh4_up },
309  { bfd_mach_sh4a,            arch_sh4a,            arch_sh4a_up },
310  { bfd_mach_sh4al_dsp,       arch_sh4al_dsp,       arch_sh4al_dsp_up },
311  { bfd_mach_sh4_nofpu,       arch_sh4_nofpu,       arch_sh4_nofp_up },
312  { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
313  { bfd_mach_sh4a_nofpu,      arch_sh4a_nofpu,      arch_sh4a_nofp_up },
314  { 0, 0, 0 }   /* Terminator.  */
315};
316
317
318/* Convert a BFD mach number into the right opcodes arch flags
319   using the table above.  */
320
321unsigned int
322sh_get_arch_from_bfd_mach (unsigned long mach)
323{
324  int i = 0;
325
326  while (bfd_to_arch_table[i].bfd_mach != 0)
327    if (bfd_to_arch_table[i].bfd_mach == mach)
328      return bfd_to_arch_table[i].arch;
329    else
330      i++;
331
332  /* mach not found.  */
333  BFD_FAIL();
334
335  return SH_ARCH_UNKNOWN_ARCH;
336}
337
338
339/* Convert a BFD mach number into a set of opcodes arch flags
340   describing all the compatible architectures (i.e. arch_up)
341   using the table above.  */
342
343unsigned int
344sh_get_arch_up_from_bfd_mach (unsigned long mach)
345{
346  int i = 0;
347
348  while (bfd_to_arch_table[i].bfd_mach != 0)
349    if (bfd_to_arch_table[i].bfd_mach == mach)
350      return bfd_to_arch_table[i].arch_up;
351    else
352      i++;
353
354  /* mach not found.  */
355  BFD_FAIL();
356
357  return SH_ARCH_UNKNOWN_ARCH;
358}
359
360
361/* Convert an arbitary arch_set - not necessarily corresponding
362   directly to anything in the table above - to the most generic
363   architecture which supports all the required features, and
364   return the corresponding BFD mach.  */
365
366unsigned long
367sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
368{
369  unsigned long result = 0;
370  unsigned int best = ~arch_set;
371  unsigned int co_mask = ~0;
372  int i = 0;
373
374  /* If arch_set permits variants with no coprocessor then do not allow
375     the other irrelevant co-processor bits to influence the choice:
376       e.g. if dsp is disallowed by arch_set, then the algorithm would
377       prefer fpu variants over nofpu variants because they also disallow
378       dsp - even though the nofpu would be the most correct choice.
379     This assumes that EVERY fpu/dsp variant has a no-coprocessor
380     counter-part, or their non-fpu/dsp instructions do not have the
381     no co-processor bit set.  */
382  if (arch_set & arch_sh_no_co)
383    co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
384
385  while (bfd_to_arch_table[i].bfd_mach != 0)
386    {
387      unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
388
389      /* Conceptually: Find the architecture with the least number
390	 of extra features or, if they have the same number, then
391	 the greatest number of required features.  Disregard
392         architectures where the required features alone do
393	 not describe a valid architecture.  */
394      if (((try & ~arch_set) < (best & ~arch_set)
395	   || ((try & ~arch_set) == (best & ~arch_set)
396	       && (~try & arch_set) < (~best & arch_set)))
397	  && SH_MERGE_ARCH_SET_VALID (try, arch_set))
398	{
399	  result = bfd_to_arch_table[i].bfd_mach;
400	  best = try;
401	}
402
403      i++;
404    }
405
406  /* This might happen if a new variant is added to sh-opc.h
407     but no corresponding entry is added to the table above.  */
408  BFD_ASSERT (result != 0);
409
410  return result;
411}
412
413
414/* Merge the architecture type of two BFD files, such that the
415   resultant architecture supports all the features required
416   by the two input BFDs.
417   If the input BFDs are multually incompatible - i.e. one uses
418   DSP while the other uses FPU - or there is no known architecture
419   that fits the requirements then an error is emitted.  */
420
421bfd_boolean
422sh_merge_bfd_arch (bfd *ibfd, bfd *obfd)
423{
424  unsigned int old_arch, new_arch, merged_arch;
425
426  if (! _bfd_generic_verify_endian_match (ibfd, obfd))
427    return FALSE;
428
429  old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
430  new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
431
432  merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
433
434  if (!SH_VALID_CO_ARCH_SET (merged_arch))
435    {
436      (*_bfd_error_handler)
437	("%B: uses %s instructions while previous modules use %s instructions",
438	 ibfd,
439	 SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
440	 SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
441      bfd_set_error (bfd_error_bad_value);
442      return FALSE;
443    }
444  else if (!SH_VALID_ARCH_SET (merged_arch))
445    {
446      (*_bfd_error_handler)
447	("internal error: merge of architecture '%s' with architecture '%s' produced unknown architecture\n",
448	 bfd_printable_name (obfd),
449	 bfd_printable_name (ibfd));
450      bfd_set_error (bfd_error_bad_value);
451      return FALSE;
452    }
453
454  bfd_default_set_arch_mach (obfd, bfd_arch_sh,
455			     sh_get_bfd_mach_from_arch_set (merged_arch));
456
457  return TRUE;
458}
459