Back to index

cell-binutils  2.17cvs20070401
cpu-sh.c
Go to the documentation of this file.
00001 /* BFD library support routines for the Renesas / SuperH SH architecture.
00002    Copyright 1993, 1994, 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005
00003    Free Software Foundation, Inc.
00004    Hacked by Steve Chamberlain of Cygnus Support.
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
00021 
00022 #include "bfd.h"
00023 #include "sysdep.h"
00024 #include "libbfd.h"
00025 #include "../opcodes/sh-opc.h"
00026 
00027 #define SH_NEXT                            arch_info_struct + 0
00028 #define SH2_NEXT                           arch_info_struct + 1
00029 #define SH2E_NEXT                          arch_info_struct + 2
00030 #define SH_DSP_NEXT                        arch_info_struct + 3
00031 #define SH3_NEXT                           arch_info_struct + 4
00032 #define SH3_NOMMU_NEXT                     arch_info_struct + 5
00033 #define SH3_DSP_NEXT                       arch_info_struct + 6
00034 #define SH3E_NEXT                          arch_info_struct + 7
00035 #define SH4_NEXT                           arch_info_struct + 8
00036 #define SH4A_NEXT                          arch_info_struct + 9
00037 #define SH4AL_DSP_NEXT                     arch_info_struct + 10
00038 #define SH4_NOFPU_NEXT                     arch_info_struct + 11
00039 #define SH4_NOMMU_NOFPU_NEXT               arch_info_struct + 12
00040 #define SH4A_NOFPU_NEXT                    arch_info_struct + 13
00041 #define SH2A_NEXT                          arch_info_struct + 14
00042 #define SH2A_NOFPU_NEXT                    arch_info_struct + 15
00043 #define SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT arch_info_struct + 16
00044 #define SH2A_NOFPU_OR_SH3_NOMMU_NEXT       arch_info_struct + 17
00045 #define SH2A_OR_SH4_NEXT                   arch_info_struct + 18
00046 #define SH2A_OR_SH3E_NEXT                  arch_info_struct + 19
00047 #define SH64_NEXT                          NULL
00048 
00049 static const bfd_arch_info_type arch_info_struct[] =
00050 {
00051   {
00052     32,                            /* 32 bits in a word.  */
00053     32,                            /* 32 bits in an address.  */
00054     8,                      /* 8 bits in a byte.  */
00055     bfd_arch_sh,
00056     bfd_mach_sh2,
00057     "sh",                   /* Architecture name.  */
00058     "sh2",                  /* Machine name.  */
00059     1,
00060     FALSE,                  /* Not the default.  */
00061     bfd_default_compatible,
00062     bfd_default_scan,
00063     SH2_NEXT
00064   },
00065   {
00066     32,                            /* 32 bits in a word.  */
00067     32,                            /* 32 bits in an address.  */
00068     8,                      /* 8 bits in a byte.  */
00069     bfd_arch_sh,
00070     bfd_mach_sh2e,
00071     "sh",                   /* Architecture name.  */
00072     "sh2e",                 /* Machine name.  */
00073     1,
00074     FALSE,                  /* Not the default.  */
00075     bfd_default_compatible,
00076     bfd_default_scan,
00077     SH2E_NEXT
00078   },
00079   {
00080     32,                            /* 32 bits in a word.  */
00081     32,                            /* 32 bits in an address.  */
00082     8,                      /* 8 bits in a byte.  */
00083     bfd_arch_sh,
00084     bfd_mach_sh_dsp,
00085     "sh",                   /* Architecture name.   */
00086     "sh-dsp",               /* Machine name.  */
00087     1,
00088     FALSE,                  /* Not the default.  */
00089     bfd_default_compatible,
00090     bfd_default_scan,
00091     SH_DSP_NEXT
00092   },
00093   {
00094     32,                            /* 32 bits in a word.  */
00095     32,                            /* 32 bits in an address.  */
00096     8,                      /* 8 bits in a byte.  */
00097     bfd_arch_sh,
00098     bfd_mach_sh3,
00099     "sh",                   /* Architecture name.   */
00100     "sh3",                  /* Machine name.  */
00101     1,
00102     FALSE,                  /* Not the default.  */
00103     bfd_default_compatible,
00104     bfd_default_scan,
00105     SH3_NEXT
00106   },
00107   {
00108     32,                            /* 32 bits in a word.  */
00109     32,                            /* 32 bits in an address.  */
00110     8,                      /* 8 bits in a byte.  */
00111     bfd_arch_sh,
00112     bfd_mach_sh3_nommu,
00113     "sh",                   /* Architecture name.   */
00114     "sh3-nommu",            /* Machine name.  */
00115     1,
00116     FALSE,                  /* Not the default.  */
00117     bfd_default_compatible,
00118     bfd_default_scan,
00119     SH3_NOMMU_NEXT
00120   },
00121   {
00122     32,                            /* 32 bits in a word.  */
00123     32,                            /* 32 bits in an address.  */
00124     8,                      /* 8 bits in a byte.  */
00125     bfd_arch_sh,
00126     bfd_mach_sh3_dsp,
00127     "sh",                   /* Architecture name.   */
00128     "sh3-dsp",                     /* Machine name.  */
00129     1,
00130     FALSE,                  /* Not the default.  */
00131     bfd_default_compatible,
00132     bfd_default_scan,
00133     SH3_DSP_NEXT
00134   },
00135   {
00136     32,                            /* 32 bits in a word.  */
00137     32,                            /* 32 bits in an address.  */
00138     8,                      /* 8 bits in a byte.  */
00139     bfd_arch_sh,
00140     bfd_mach_sh3e,
00141     "sh",                   /* Architecture name.   */
00142     "sh3e",                 /* Machine name.  */
00143     1,
00144     FALSE,                  /* Not the default.  */
00145     bfd_default_compatible,
00146     bfd_default_scan,
00147     SH3E_NEXT
00148   },
00149   {
00150     32,                            /* 32 bits in a word.  */
00151     32,                            /* 32 bits in an address.  */
00152     8,                      /* 8 bits in a byte.  */
00153     bfd_arch_sh,
00154     bfd_mach_sh4,
00155     "sh",                   /* Architecture name.   */
00156     "sh4",                  /* Machine name.  */
00157     1,
00158     FALSE,                  /* Not the default.  */
00159     bfd_default_compatible,
00160     bfd_default_scan,
00161     SH4_NEXT
00162   },
00163   {
00164     32,                            /* 32 bits in a word.  */
00165     32,                            /* 32 bits in an address.  */
00166     8,                      /* 8 bits in a byte.  */
00167     bfd_arch_sh,
00168     bfd_mach_sh4a,
00169     "sh",                   /* Architecture name.   */
00170     "sh4a",                 /* Machine name.  */
00171     1,
00172     FALSE,                  /* Not the default.  */
00173     bfd_default_compatible,
00174     bfd_default_scan,
00175     SH4A_NEXT
00176   },
00177   {
00178     32,                            /* 32 bits in a word.  */
00179     32,                            /* 32 bits in an address.  */
00180     8,                      /* 8 bits in a byte.  */
00181     bfd_arch_sh,
00182     bfd_mach_sh4al_dsp,
00183     "sh",                   /* Architecture name.   */
00184     "sh4al-dsp",            /* Machine name.  */
00185     1,
00186     FALSE,                  /* Not the default.  */
00187     bfd_default_compatible,
00188     bfd_default_scan,
00189     SH4AL_DSP_NEXT
00190   },
00191   {
00192     32,                            /* 32 bits in a word.  */
00193     32,                            /* 32 bits in an address.  */
00194     8,                      /* 8 bits in a byte.  */
00195     bfd_arch_sh,
00196     bfd_mach_sh4_nofpu,
00197     "sh",                   /* Architecture name.   */
00198     "sh4-nofpu",            /* Machine name.  */
00199     1,
00200     FALSE,                  /* Not the default.  */
00201     bfd_default_compatible,
00202     bfd_default_scan,
00203     SH4_NOFPU_NEXT
00204   },
00205   {
00206     32,                            /* 32 bits in a word.  */
00207     32,                            /* 32 bits in an address.  */
00208     8,                      /* 8 bits in a byte.  */
00209     bfd_arch_sh,
00210     bfd_mach_sh4_nommu_nofpu,
00211     "sh",                   /* Architecture name.   */
00212     "sh4-nommu-nofpu",             /* Machine name.  */
00213     1,
00214     FALSE,                  /* Not the default.  */
00215     bfd_default_compatible,
00216     bfd_default_scan,
00217     SH4_NOMMU_NOFPU_NEXT
00218   },
00219   {
00220     32,                            /* 32 bits in a word.  */
00221     32,                            /* 32 bits in an address.  */
00222     8,                      /* 8 bits in a byte.  */
00223     bfd_arch_sh,
00224     bfd_mach_sh4a_nofpu,
00225     "sh",                   /* Architecture name.   */
00226     "sh4a-nofpu",           /* Machine name.  */
00227     1,
00228     FALSE,                  /* Not the default.  */
00229     bfd_default_compatible,
00230     bfd_default_scan,
00231     SH4A_NOFPU_NEXT
00232   },
00233   {
00234     32,                            /* 32 bits in a word.  */
00235     32,                            /* 32 bits in an address.  */
00236     8,                      /* 8 bits in a byte.  */
00237     bfd_arch_sh,
00238     bfd_mach_sh2a,
00239     "sh",                   /* Architecture name.  */
00240     "sh2a",                 /* Machine name.  */
00241     1,
00242     FALSE,                  /* Not the default.  */
00243     bfd_default_compatible,
00244     bfd_default_scan,
00245     SH2A_NEXT
00246   },
00247   {
00248     32,                            /* 32 bits in a word.  */
00249     32,                            /* 32 bits in an address.  */
00250     8,                      /* 8 bits in a byte.  */
00251     bfd_arch_sh,
00252     bfd_mach_sh2a_nofpu,
00253     "sh",                   /* Architecture name.  */
00254     "sh2a-nofpu",           /* Machine name.  */
00255     1,
00256     FALSE,                  /* Not the default.  */
00257     bfd_default_compatible,
00258     bfd_default_scan,
00259     SH2A_NOFPU_NEXT
00260   },
00261   {
00262     32,                            /* 32 bits in a word.  */
00263     32,                            /* 32 bits in an address.  */
00264     8,                      /* 8 bits in a byte.  */
00265     bfd_arch_sh,
00266     bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu,
00267     "sh",                   /* Architecture name.  */
00268     "sh2a-nofpu-or-sh4-nommu-nofpu",             /* Machine name.  */
00269     1,
00270     FALSE,                  /* Not the default.  */
00271     bfd_default_compatible,
00272     bfd_default_scan,
00273     SH2A_NOFPU_OR_SH4_NOMMU_NOFPU_NEXT
00274   },
00275   {
00276     32,                            /* 32 bits in a word.  */
00277     32,                            /* 32 bits in an address.  */
00278     8,                      /* 8 bits in a byte.  */
00279     bfd_arch_sh,
00280     bfd_mach_sh2a_nofpu_or_sh3_nommu,
00281     "sh",                   /* Architecture name. .  */
00282     "sh2a-nofpu-or-sh3-nommu",     /* Machine name.  */
00283     1,
00284     FALSE,                  /* Not the default.  */
00285     bfd_default_compatible,
00286     bfd_default_scan,
00287     SH2A_NOFPU_OR_SH3_NOMMU_NEXT
00288   },
00289   {
00290     32,                            /* 32 bits in a word.  */
00291     32,                            /* 32 bits in an address.  */
00292     8,                      /* 8 bits in a byte.  */
00293     bfd_arch_sh,
00294     bfd_mach_sh2a_or_sh4,
00295     "sh",                   /* Architecture name.  */
00296     "sh2a-or-sh4",          /* Machine name.  */
00297     1,
00298     FALSE,                  /* Not the default.  */
00299     bfd_default_compatible,
00300     bfd_default_scan,
00301     SH2A_OR_SH4_NEXT
00302   },
00303   {
00304     32,                            /* 32 bits in a word.  */
00305     32,                            /* 32 bits in an address.  */
00306     8,                      /* 8 bits in a byte.  */
00307     bfd_arch_sh,
00308     bfd_mach_sh2a_or_sh3e,
00309     "sh",                   /* Architecture name.  */
00310     "sh2a-or-sh3e",         /* Machine name.  */
00311     1,
00312     FALSE,                  /* Not the default.  */
00313     bfd_default_compatible,
00314     bfd_default_scan,
00315     SH2A_OR_SH3E_NEXT
00316   },
00317   {
00318     64,                            /* 64 bits in a word.  */
00319     64,                            /* 64 bits in an address.  */
00320     8,                      /* 8 bits in a byte.  */
00321     bfd_arch_sh,
00322     bfd_mach_sh5,
00323     "sh",                   /* Architecture name.   */
00324     "sh5",                  /* Machine name.  */
00325     1,
00326     FALSE,                  /* Not the default.  */
00327     bfd_default_compatible,
00328     bfd_default_scan,
00329     SH64_NEXT
00330   },
00331 };
00332 
00333 const bfd_arch_info_type bfd_sh_arch =
00334 {
00335   32,                       /* 32 bits in a word.  */
00336   32,                       /* 32 bits in an address.  */
00337   8,                        /* 8 bits in a byte.  */
00338   bfd_arch_sh,
00339   bfd_mach_sh,
00340   "sh",                            /* Architecture name.   */
00341   "sh",                            /* Machine name.  */
00342   1,
00343   TRUE,                            /* The default machine.  */
00344   bfd_default_compatible,
00345   bfd_default_scan,
00346   SH_NEXT
00347 };
00348 
00349 
00350 /* This table defines the mappings from the BFD internal numbering
00351    system to the opcodes internal flags system.
00352    It is used by the functions defined below.
00353    The prototypes for these SH specific functions are found in
00354    sh-opc.h .  */
00355 
00356 static struct { unsigned long bfd_mach, arch, arch_up; } bfd_to_arch_table[] =
00357 {
00358   { bfd_mach_sh,              arch_sh1,             arch_sh_up },
00359   { bfd_mach_sh2,             arch_sh2,             arch_sh2_up },
00360   { bfd_mach_sh2e,            arch_sh2e,            arch_sh2e_up },
00361   { bfd_mach_sh_dsp,          arch_sh_dsp,          arch_sh_dsp_up },
00362   { bfd_mach_sh2a,            arch_sh2a,            arch_sh2a_up },
00363   { bfd_mach_sh2a_nofpu,      arch_sh2a_nofpu,      arch_sh2a_nofpu_up },
00364 
00365   { bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu,         arch_sh2a_nofpu_or_sh4_nommu_nofpu,   arch_sh2a_nofpu_or_sh4_nommu_nofpu_up },
00366   { bfd_mach_sh2a_nofpu_or_sh3_nommu,               arch_sh2a_nofpu_or_sh3_nommu,         arch_sh2a_nofpu_or_sh3_nommu_up },
00367   { bfd_mach_sh2a_or_sh4,     arch_sh2a_or_sh4,     arch_sh2a_or_sh4_up },
00368   { bfd_mach_sh2a_or_sh3e,    arch_sh2a_or_sh3e,    arch_sh2a_or_sh3e_up },
00369   
00370   { bfd_mach_sh3,             arch_sh3,             arch_sh3_up },
00371   { bfd_mach_sh3_nommu,       arch_sh3_nommu,       arch_sh3_nommu_up },
00372   { bfd_mach_sh3_dsp,         arch_sh3_dsp,         arch_sh3_dsp_up },
00373   { bfd_mach_sh3e,            arch_sh3e,            arch_sh3e_up },
00374   { bfd_mach_sh4,             arch_sh4,             arch_sh4_up },
00375   { bfd_mach_sh4a,            arch_sh4a,            arch_sh4a_up },
00376   { bfd_mach_sh4al_dsp,       arch_sh4al_dsp,       arch_sh4al_dsp_up },
00377   { bfd_mach_sh4_nofpu,       arch_sh4_nofpu,       arch_sh4_nofpu_up },
00378   { bfd_mach_sh4_nommu_nofpu, arch_sh4_nommu_nofpu, arch_sh4_nommu_nofpu_up },
00379   { bfd_mach_sh4a_nofpu,      arch_sh4a_nofpu,      arch_sh4a_nofpu_up },
00380   { 0, 0, 0 }   /* Terminator.  */
00381 };
00382 
00383 
00384 /* Convert a BFD mach number into the right opcodes arch flags
00385    using the table above.  */
00386 
00387 unsigned int
00388 sh_get_arch_from_bfd_mach (unsigned long mach)
00389 {
00390   int i = 0;
00391 
00392   while (bfd_to_arch_table[i].bfd_mach != 0)
00393     if (bfd_to_arch_table[i].bfd_mach == mach)
00394       return bfd_to_arch_table[i].arch;
00395     else
00396       i++;
00397 
00398   /* Machine not found.   */
00399   BFD_FAIL();
00400 
00401   return SH_ARCH_UNKNOWN_ARCH;
00402 }
00403 
00404 
00405 /* Convert a BFD mach number into a set of opcodes arch flags
00406    describing all the compatible architectures (i.e. arch_up)
00407    using the table above.  */
00408 
00409 unsigned int
00410 sh_get_arch_up_from_bfd_mach (unsigned long mach)
00411 {
00412   int i = 0;
00413 
00414   while (bfd_to_arch_table[i].bfd_mach != 0)
00415     if (bfd_to_arch_table[i].bfd_mach == mach)
00416       return bfd_to_arch_table[i].arch_up;
00417     else
00418       i++;
00419 
00420   /* Machine not found.  */
00421   BFD_FAIL();
00422 
00423   return SH_ARCH_UNKNOWN_ARCH;
00424 }
00425 
00426 
00427 /* Convert an arbitary arch_set - not necessarily corresponding
00428    directly to anything in the table above - to the most generic
00429    architecture which supports all the required features, and
00430    return the corresponding BFD mach.  */
00431 
00432 unsigned long
00433 sh_get_bfd_mach_from_arch_set (unsigned int arch_set)
00434 {
00435   unsigned long result = 0;
00436   unsigned int best = ~arch_set;
00437   unsigned int co_mask = ~0;
00438   int i = 0;
00439 
00440   /* If arch_set permits variants with no coprocessor then do not allow
00441      the other irrelevant co-processor bits to influence the choice:
00442        e.g. if dsp is disallowed by arch_set, then the algorithm would
00443        prefer fpu variants over nofpu variants because they also disallow
00444        dsp - even though the nofpu would be the most correct choice.
00445      This assumes that EVERY fpu/dsp variant has a no-coprocessor
00446      counter-part, or their non-fpu/dsp instructions do not have the
00447      no co-processor bit set.  */
00448   if (arch_set & arch_sh_no_co)
00449     co_mask = ~(arch_sh_sp_fpu | arch_sh_dp_fpu | arch_sh_has_dsp);
00450 
00451   while (bfd_to_arch_table[i].bfd_mach != 0)
00452     {
00453       unsigned int try = bfd_to_arch_table[i].arch_up & co_mask;
00454 
00455       /* Conceptually: Find the architecture with the least number
00456         of extra features or, if they have the same number, then
00457         the greatest number of required features.  Disregard
00458          architectures where the required features alone do
00459         not describe a valid architecture.  */
00460       if (((try & ~arch_set) < (best & ~arch_set)
00461           || ((try & ~arch_set) == (best & ~arch_set)
00462               && (~try & arch_set) < (~best & arch_set)))
00463          && SH_MERGE_ARCH_SET_VALID (try, arch_set))
00464        {
00465          result = bfd_to_arch_table[i].bfd_mach;
00466          best = try;
00467        }
00468 
00469       i++;
00470     }
00471 
00472   /* This might happen if a new variant is added to sh-opc.h
00473      but no corresponding entry is added to the table above.  */
00474   BFD_ASSERT (result != 0);
00475 
00476   return result;
00477 }
00478 
00479 
00480 /* Merge the architecture type of two BFD files, such that the
00481    resultant architecture supports all the features required
00482    by the two input BFDs.
00483    If the input BFDs are multually incompatible - i.e. one uses
00484    DSP while the other uses FPU - or there is no known architecture
00485    that fits the requirements then an error is emitted.  */
00486 
00487 bfd_boolean
00488 sh_merge_bfd_arch (bfd *ibfd, bfd *obfd)
00489 {
00490   unsigned int old_arch, new_arch, merged_arch;
00491 
00492   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
00493     return FALSE;
00494 
00495   old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
00496   new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
00497 
00498   merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
00499 
00500   if (!SH_VALID_CO_ARCH_SET (merged_arch))
00501     {
00502       (*_bfd_error_handler)
00503        ("%B: uses %s instructions while previous modules use %s instructions",
00504         ibfd,
00505         SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
00506         SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
00507       bfd_set_error (bfd_error_bad_value);
00508       return FALSE;
00509     }
00510   else if (!SH_VALID_ARCH_SET (merged_arch))
00511     {
00512       (*_bfd_error_handler)
00513        ("internal error: merge of architecture '%s' with architecture '%s' produced unknown architecture\n",
00514         bfd_printable_name (obfd),
00515         bfd_printable_name (ibfd));
00516       bfd_set_error (bfd_error_bad_value);
00517       return FALSE;
00518     }
00519 
00520   bfd_default_set_arch_mach (obfd, bfd_arch_sh,
00521                           sh_get_bfd_mach_from_arch_set (merged_arch));
00522   
00523   return TRUE;
00524 }