xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/cpu-riscv.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
175fd0b74Schristos /* BFD backend for RISC-V
2*e992f068Schristos    Copyright (C) 2011-2022 Free Software Foundation, Inc.
375fd0b74Schristos 
4ede78133Schristos    Contributed by Andrew Waterman (andrew@sifive.com).
575fd0b74Schristos    Based on MIPS target.
675fd0b74Schristos 
775fd0b74Schristos    This file is part of BFD, the Binary File Descriptor library.
875fd0b74Schristos 
975fd0b74Schristos    This program is free software; you can redistribute it and/or modify
1075fd0b74Schristos    it under the terms of the GNU General Public License as published by
1175fd0b74Schristos    the Free Software Foundation; either version 3 of the License, or
1275fd0b74Schristos    (at your option) any later version.
1375fd0b74Schristos 
1475fd0b74Schristos    This program is distributed in the hope that it will be useful,
1575fd0b74Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
1675fd0b74Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1775fd0b74Schristos    GNU General Public License for more details.
1875fd0b74Schristos 
1975fd0b74Schristos    You should have received a copy of the GNU General Public License
20ede78133Schristos    along with this program; see the file COPYING3. If not,
21ede78133Schristos    see <http://www.gnu.org/licenses/>.  */
2275fd0b74Schristos 
2375fd0b74Schristos #include "sysdep.h"
2475fd0b74Schristos #include "bfd.h"
2575fd0b74Schristos #include "libbfd.h"
26*e992f068Schristos #include "cpu-riscv.h"
2775fd0b74Schristos 
2875fd0b74Schristos static const bfd_arch_info_type *
riscv_compatible(const bfd_arch_info_type * a,const bfd_arch_info_type * b)2975fd0b74Schristos riscv_compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
3075fd0b74Schristos {
3175fd0b74Schristos   if (a->arch != b->arch)
3275fd0b74Schristos     return NULL;
3375fd0b74Schristos 
3475fd0b74Schristos   /* Machine compatibility is checked in
3575fd0b74Schristos      _bfd_riscv_elf_merge_private_bfd_data.  */
3675fd0b74Schristos 
3775fd0b74Schristos   return a;
3875fd0b74Schristos }
3975fd0b74Schristos 
40*e992f068Schristos /* Return TRUE if STRING matches the architecture described by INFO.  */
41*e992f068Schristos 
42*e992f068Schristos static bool
riscv_scan(const struct bfd_arch_info * info,const char * string)43*e992f068Schristos riscv_scan (const struct bfd_arch_info *info, const char *string)
44*e992f068Schristos {
45*e992f068Schristos   if (bfd_default_scan (info, string))
46*e992f068Schristos     return true;
47*e992f068Schristos 
48*e992f068Schristos   /* The incoming STRING might take the form of riscv:rvXXzzz, where XX is
49*e992f068Schristos      32 or 64, and zzz are one or more extension characters.  As we
50*e992f068Schristos      currently only have 3 architectures defined, 'riscv', 'riscv:rv32',
51*e992f068Schristos      and 'riscv:rv64', we would like to ignore the zzz for the purpose of
52*e992f068Schristos      matching here.
53*e992f068Schristos 
54*e992f068Schristos      However, we don't want the default 'riscv' to match over a more
55*e992f068Schristos      specific 'riscv:rv32' or 'riscv:rv64', so in the case of the default
56*e992f068Schristos      architecture (with the shorter 'riscv' name) we don't allow any
57*e992f068Schristos      special matching, but for the 'riscv:rvXX' cases, we allow a match
58*e992f068Schristos      with any additional trailing characters being ignored.  */
59*e992f068Schristos   if (!info->the_default
60*e992f068Schristos       && strncasecmp (string, info->printable_name,
61*e992f068Schristos                       strlen (info->printable_name)) == 0)
62*e992f068Schristos     return true;
63*e992f068Schristos 
64*e992f068Schristos   return false;
65*e992f068Schristos }
66*e992f068Schristos 
67012573ebSchristos #define N(BITS, NUMBER, PRINT, DEFAULT, NEXT)			\
6875fd0b74Schristos   {								\
69012573ebSchristos     BITS,      /* Bits in a word.  */				\
70012573ebSchristos     BITS,      /* Bits in an address.  */			\
71012573ebSchristos     8,	       /* Bits in a byte.  */				\
7275fd0b74Schristos     bfd_arch_riscv,						\
7375fd0b74Schristos     NUMBER,							\
7475fd0b74Schristos     "riscv",							\
7575fd0b74Schristos     PRINT,							\
7675fd0b74Schristos     3,								\
7775fd0b74Schristos     DEFAULT,							\
7875fd0b74Schristos     riscv_compatible,						\
79*e992f068Schristos     riscv_scan,							\
8075fd0b74Schristos     bfd_arch_default_fill,					\
8175fd0b74Schristos     NEXT,							\
82012573ebSchristos     0 /* Maximum offset of a reloc from the start of an insn.  */\
8375fd0b74Schristos   }
8475fd0b74Schristos 
85ede78133Schristos /* This enum must be kept in the same order as arch_info_struct.  */
8675fd0b74Schristos enum
8775fd0b74Schristos {
8875fd0b74Schristos   I_riscv64,
8975fd0b74Schristos   I_riscv32
9075fd0b74Schristos };
9175fd0b74Schristos 
9275fd0b74Schristos #define NN(index) (&arch_info_struct[(index) + 1])
9375fd0b74Schristos 
94ede78133Schristos /* This array must be kept in the same order as the anonymous enum above,
95ede78133Schristos    and each entry except the last should end with NN (my enum value).  */
9675fd0b74Schristos static const bfd_arch_info_type arch_info_struct[] =
9775fd0b74Schristos {
98*e992f068Schristos   N (64, bfd_mach_riscv64, "riscv:rv64", false, NN (I_riscv64)),
99*e992f068Schristos   N (32, bfd_mach_riscv32, "riscv:rv32", false, NULL)
10075fd0b74Schristos };
10175fd0b74Schristos 
10275fd0b74Schristos /* The default architecture is riscv:rv64.  */
10375fd0b74Schristos const bfd_arch_info_type bfd_riscv_arch =
104*e992f068Schristos   N (64, 0, "riscv", true, &arch_info_struct[0]);
105*e992f068Schristos 
106*e992f068Schristos /* List for all supported ISA spec versions.  */
107*e992f068Schristos const struct riscv_spec riscv_isa_specs[] =
108*e992f068Schristos {
109*e992f068Schristos   {"2.2",      ISA_SPEC_CLASS_2P2},
110*e992f068Schristos   {"20190608", ISA_SPEC_CLASS_20190608},
111*e992f068Schristos   {"20191213", ISA_SPEC_CLASS_20191213},
112*e992f068Schristos };
113*e992f068Schristos 
114*e992f068Schristos /* List for all supported privileged spec versions.  */
115*e992f068Schristos const struct riscv_spec riscv_priv_specs[] =
116*e992f068Schristos {
117*e992f068Schristos   {"1.9.1", PRIV_SPEC_CLASS_1P9P1},
118*e992f068Schristos   {"1.10",  PRIV_SPEC_CLASS_1P10},
119*e992f068Schristos   {"1.11",  PRIV_SPEC_CLASS_1P11},
120*e992f068Schristos   {"1.12",  PRIV_SPEC_CLASS_1P12},
121*e992f068Schristos };
122*e992f068Schristos 
123*e992f068Schristos /* Get the corresponding CSR version class by giving privilege
124*e992f068Schristos    version numbers.  It is usually used to convert the priv
125*e992f068Schristos    attribute numbers into the corresponding class.  */
126*e992f068Schristos 
127*e992f068Schristos void
riscv_get_priv_spec_class_from_numbers(unsigned int major,unsigned int minor,unsigned int revision,enum riscv_spec_class * class)128*e992f068Schristos riscv_get_priv_spec_class_from_numbers (unsigned int major,
129*e992f068Schristos 					unsigned int minor,
130*e992f068Schristos 					unsigned int revision,
131*e992f068Schristos 					enum riscv_spec_class *class)
132*e992f068Schristos {
133*e992f068Schristos   enum riscv_spec_class class_t = *class;
134*e992f068Schristos   char buf[36];
135*e992f068Schristos 
136*e992f068Schristos   if (revision != 0)
137*e992f068Schristos     snprintf (buf, sizeof (buf), "%u.%u.%u", major, minor, revision);
138*e992f068Schristos   else
139*e992f068Schristos     snprintf (buf, sizeof (buf), "%u.%u", major, minor);
140*e992f068Schristos 
141*e992f068Schristos   RISCV_GET_PRIV_SPEC_CLASS (buf, class_t);
142*e992f068Schristos   *class = class_t;
143*e992f068Schristos }
144*e992f068Schristos 
145*e992f068Schristos /* Define mapping symbols for riscv.  */
146*e992f068Schristos 
147*e992f068Schristos bool
riscv_elf_is_mapping_symbols(const char * name)148*e992f068Schristos riscv_elf_is_mapping_symbols (const char *name)
149*e992f068Schristos {
150*e992f068Schristos   return (!strncmp (name, "$d", 2)
151*e992f068Schristos 	  || !strncmp (name, "$x", 2));
152*e992f068Schristos }
153