xref: /netbsd-src/external/gpl3/binutils/dist/bfd/cpu-riscv.c (revision dd7241df2fae9da4ea2bd20a68f001fa86ecf909)
141b40da6Smatt /* BFD backend for RISC-V
2*dd7241dfSchristos    Copyright (C) 2011-2024 Free Software Foundation, Inc.
341b40da6Smatt 
4fc4f4269Schristos    Contributed by Andrew Waterman (andrew@sifive.com).
541b40da6Smatt    Based on MIPS target.
641b40da6Smatt 
741b40da6Smatt    This file is part of BFD, the Binary File Descriptor library.
841b40da6Smatt 
941b40da6Smatt    This program is free software; you can redistribute it and/or modify
1041b40da6Smatt    it under the terms of the GNU General Public License as published by
1141b40da6Smatt    the Free Software Foundation; either version 3 of the License, or
1241b40da6Smatt    (at your option) any later version.
1341b40da6Smatt 
1441b40da6Smatt    This program is distributed in the hope that it will be useful,
1541b40da6Smatt    but WITHOUT ANY WARRANTY; without even the implied warranty of
1641b40da6Smatt    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1741b40da6Smatt    GNU General Public License for more details.
1841b40da6Smatt 
1941b40da6Smatt    You should have received a copy of the GNU General Public License
20fc4f4269Schristos    along with this program; see the file COPYING3. If not,
21fc4f4269Schristos    see <http://www.gnu.org/licenses/>.  */
2241b40da6Smatt 
2341b40da6Smatt #include "sysdep.h"
2441b40da6Smatt #include "bfd.h"
2541b40da6Smatt #include "libbfd.h"
2603f5171aSchristos #include "cpu-riscv.h"
2741b40da6Smatt 
2841b40da6Smatt static const bfd_arch_info_type *
riscv_compatible(const bfd_arch_info_type * a,const bfd_arch_info_type * b)2941b40da6Smatt riscv_compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
3041b40da6Smatt {
3141b40da6Smatt   if (a->arch != b->arch)
3241b40da6Smatt     return NULL;
3341b40da6Smatt 
3441b40da6Smatt   /* Machine compatibility is checked in
3541b40da6Smatt      _bfd_riscv_elf_merge_private_bfd_data.  */
3641b40da6Smatt 
3741b40da6Smatt   return a;
3841b40da6Smatt }
3941b40da6Smatt 
4003f5171aSchristos /* Return TRUE if STRING matches the architecture described by INFO.  */
4103f5171aSchristos 
4203f5171aSchristos static bool
riscv_scan(const struct bfd_arch_info * info,const char * string)4303f5171aSchristos riscv_scan (const struct bfd_arch_info *info, const char *string)
4403f5171aSchristos {
4503f5171aSchristos   if (bfd_default_scan (info, string))
4603f5171aSchristos     return true;
4703f5171aSchristos 
4803f5171aSchristos   /* The incoming STRING might take the form of riscv:rvXXzzz, where XX is
4903f5171aSchristos      32 or 64, and zzz are one or more extension characters.  As we
5003f5171aSchristos      currently only have 3 architectures defined, 'riscv', 'riscv:rv32',
5103f5171aSchristos      and 'riscv:rv64', we would like to ignore the zzz for the purpose of
5203f5171aSchristos      matching here.
5303f5171aSchristos 
5403f5171aSchristos      However, we don't want the default 'riscv' to match over a more
5503f5171aSchristos      specific 'riscv:rv32' or 'riscv:rv64', so in the case of the default
5603f5171aSchristos      architecture (with the shorter 'riscv' name) we don't allow any
5703f5171aSchristos      special matching, but for the 'riscv:rvXX' cases, we allow a match
5803f5171aSchristos      with any additional trailing characters being ignored.  */
5903f5171aSchristos   if (!info->the_default
6003f5171aSchristos       && strncasecmp (string, info->printable_name,
6103f5171aSchristos                       strlen (info->printable_name)) == 0)
6203f5171aSchristos     return true;
6303f5171aSchristos 
6403f5171aSchristos   return false;
6503f5171aSchristos }
6603f5171aSchristos 
67106c59e5Schristos #define N(BITS, NUMBER, PRINT, DEFAULT, NEXT)			\
6841b40da6Smatt   {								\
69106c59e5Schristos     BITS,      /* Bits in a word.  */				\
70106c59e5Schristos     BITS,      /* Bits in an address.  */			\
71106c59e5Schristos     8,	       /* Bits in a byte.  */				\
7241b40da6Smatt     bfd_arch_riscv,						\
7341b40da6Smatt     NUMBER,							\
7441b40da6Smatt     "riscv",							\
7541b40da6Smatt     PRINT,							\
7641b40da6Smatt     3,								\
7741b40da6Smatt     DEFAULT,							\
7841b40da6Smatt     riscv_compatible,						\
7903f5171aSchristos     riscv_scan,							\
8041b40da6Smatt     bfd_arch_default_fill,					\
8141b40da6Smatt     NEXT,							\
82106c59e5Schristos     0 /* Maximum offset of a reloc from the start of an insn.  */\
8341b40da6Smatt   }
8441b40da6Smatt 
85fc4f4269Schristos /* This enum must be kept in the same order as arch_info_struct.  */
8641b40da6Smatt enum
8741b40da6Smatt {
8841b40da6Smatt   I_riscv64,
8941b40da6Smatt   I_riscv32
9041b40da6Smatt };
9141b40da6Smatt 
9241b40da6Smatt #define NN(index) (&arch_info_struct[(index) + 1])
9341b40da6Smatt 
94fc4f4269Schristos /* This array must be kept in the same order as the anonymous enum above,
95fc4f4269Schristos    and each entry except the last should end with NN (my enum value).  */
9641b40da6Smatt static const bfd_arch_info_type arch_info_struct[] =
9741b40da6Smatt {
9803f5171aSchristos   N (64, bfd_mach_riscv64, "riscv:rv64", false, NN (I_riscv64)),
9903f5171aSchristos   N (32, bfd_mach_riscv32, "riscv:rv32", false, NULL)
10041b40da6Smatt };
10141b40da6Smatt 
10241b40da6Smatt /* The default architecture is riscv:rv64.  */
10341b40da6Smatt const bfd_arch_info_type bfd_riscv_arch =
10403f5171aSchristos   N (64, 0, "riscv", true, &arch_info_struct[0]);
10503f5171aSchristos 
10603f5171aSchristos /* List for all supported ISA spec versions.  */
10703f5171aSchristos const struct riscv_spec riscv_isa_specs[] =
10803f5171aSchristos {
10903f5171aSchristos   {"2.2",      ISA_SPEC_CLASS_2P2},
11003f5171aSchristos   {"20190608", ISA_SPEC_CLASS_20190608},
11103f5171aSchristos   {"20191213", ISA_SPEC_CLASS_20191213},
11203f5171aSchristos };
11303f5171aSchristos 
11403f5171aSchristos /* List for all supported privileged spec versions.  */
11503f5171aSchristos const struct riscv_spec riscv_priv_specs[] =
11603f5171aSchristos {
11703f5171aSchristos   {"1.9.1", PRIV_SPEC_CLASS_1P9P1},
11803f5171aSchristos   {"1.10",  PRIV_SPEC_CLASS_1P10},
11903f5171aSchristos   {"1.11",  PRIV_SPEC_CLASS_1P11},
12003f5171aSchristos   {"1.12",  PRIV_SPEC_CLASS_1P12},
12103f5171aSchristos };
12203f5171aSchristos 
12303f5171aSchristos /* Get the corresponding CSR version class by giving privilege
12403f5171aSchristos    version numbers.  It is usually used to convert the priv
12503f5171aSchristos    attribute numbers into the corresponding class.  */
12603f5171aSchristos 
12703f5171aSchristos void
riscv_get_priv_spec_class_from_numbers(unsigned int major,unsigned int minor,unsigned int revision,enum riscv_spec_class * class)12803f5171aSchristos riscv_get_priv_spec_class_from_numbers (unsigned int major,
12903f5171aSchristos 					unsigned int minor,
13003f5171aSchristos 					unsigned int revision,
13103f5171aSchristos 					enum riscv_spec_class *class)
13203f5171aSchristos {
13303f5171aSchristos   enum riscv_spec_class class_t = *class;
13403f5171aSchristos   char buf[36];
13503f5171aSchristos 
13603f5171aSchristos   if (revision != 0)
13703f5171aSchristos     snprintf (buf, sizeof (buf), "%u.%u.%u", major, minor, revision);
13803f5171aSchristos   else
13903f5171aSchristos     snprintf (buf, sizeof (buf), "%u.%u", major, minor);
14003f5171aSchristos 
14103f5171aSchristos   RISCV_GET_PRIV_SPEC_CLASS (buf, class_t);
14203f5171aSchristos   *class = class_t;
14303f5171aSchristos }
14403f5171aSchristos 
14503f5171aSchristos /* Define mapping symbols for riscv.  */
14603f5171aSchristos 
14703f5171aSchristos bool
riscv_elf_is_mapping_symbols(const char * name)14803f5171aSchristos riscv_elf_is_mapping_symbols (const char *name)
14903f5171aSchristos {
150*dd7241dfSchristos   return (!strcmp (name, "$d")
151*dd7241dfSchristos 	  || !strcmp (name, "$x")
152*dd7241dfSchristos 	  || !strncmp (name, "$xrv", 4));
15303f5171aSchristos }
154