xref: /netbsd-src/external/gpl3/gdb/dist/bfd/cpu-riscv.c (revision 9fd8799cb5ceb66c69f2eb1a6d26a1d587ba1f1e)
1 /* BFD backend for RISC-V
2    Copyright (C) 2011-2020 Free Software Foundation, Inc.
3 
4    Contributed by Andrew Waterman (andrew@sifive.com).
5    Based on MIPS target.
6 
7    This file is part of BFD, the Binary File Descriptor library.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; see the file COPYING3. If not,
21    see <http://www.gnu.org/licenses/>.  */
22 
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "elfxx-riscv.h"
27 
28 /* Record the priv spec version string and the corresponding class.  */
29 
30 struct priv_spec_t
31 {
32   const char *name;
33   enum riscv_priv_spec_class class;
34 };
35 
36 /* List for all supported privilege versions.  */
37 
38 static const struct priv_spec_t priv_specs[] =
39 {
40   {"1.9.1", PRIV_SPEC_CLASS_1P9P1},
41   {"1.10",  PRIV_SPEC_CLASS_1P10},
42   {"1.11",  PRIV_SPEC_CLASS_1P11},
43 
44 /* Terminate the list.  */
45   {NULL, 0}
46 };
47 
48 /* Get the corresponding CSR version class by giving a privilege
49    version string.  */
50 
51 int
52 riscv_get_priv_spec_class (const char *s,
53 			   enum riscv_priv_spec_class *class)
54 {
55   const struct priv_spec_t *version;
56 
57   if (s == NULL)
58     return 0;
59 
60   for (version = &priv_specs[0]; version->name != NULL; ++version)
61     if (strcmp (version->name, s) == 0)
62       {
63 	*class = version->class;
64 	return 1;
65       }
66 
67   /* Can not find the supported privilege version.  */
68   return 0;
69 }
70 
71 /* Get the corresponding CSR version class by giving privilege
72    version numbers.  It is usually used to convert the priv
73    attribute numbers into the corresponding class.  */
74 
75 int
76 riscv_get_priv_spec_class_from_numbers (unsigned int major,
77 					unsigned int minor,
78 					unsigned int revision,
79 					enum riscv_priv_spec_class *class)
80 {
81   char buf[36];
82 
83   if (major == 0 && minor == 0 && revision == 0)
84     {
85       *class = PRIV_SPEC_CLASS_NONE;
86       return 1;
87     }
88 
89   if (revision != 0)
90     snprintf (buf, sizeof (buf), "%u.%u.%u", major, minor, revision);
91   else
92     snprintf (buf, sizeof (buf), "%u.%u", major, minor);
93 
94   return riscv_get_priv_spec_class (buf, class);
95 }
96 
97 /* Get the corresponding privilege version string by giving a CSR
98    version class.  */
99 
100 const char *
101 riscv_get_priv_spec_name (enum riscv_priv_spec_class class)
102 {
103   /* The first enum is PRIV_SPEC_CLASS_NONE.  */
104   return priv_specs[class - 1].name;
105 }
106 
107 /* This routine is provided two arch_infos and returns an arch_info
108    that is compatible with both, or NULL if none exists.  */
109 
110 static const bfd_arch_info_type *
111 riscv_compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
112 {
113   if (a->arch != b->arch)
114     return NULL;
115 
116   /* Machine compatibility is checked in
117      _bfd_riscv_elf_merge_private_bfd_data.  */
118 
119   return a;
120 }
121 
122 /* Return TRUE if STRING matches the architecture described by INFO.  */
123 
124 static bfd_boolean
125 riscv_scan (const struct bfd_arch_info *info, const char *string)
126 {
127   if (bfd_default_scan (info, string))
128     return TRUE;
129 
130   /* The incoming STRING might take the form of riscv:rvXXzzz, where XX is
131      32 or 64, and zzz are one or more extension characters.  As we
132      currently only have 3 architectures defined, 'riscv', 'riscv:rv32',
133      and 'riscv:rv64', we would like to ignore the zzz for the purpose of
134      matching here.
135 
136      However, we don't want the default 'riscv' to match over a more
137      specific 'riscv:rv32' or 'riscv:rv64', so in the case of the default
138      architecture (with the shorter 'riscv' name) we don't allow any
139      special matching, but for the 'riscv:rvXX' cases, we allow a match
140      with any additional trailing characters being ignored.  */
141   if (!info->the_default
142       && strncasecmp (string, info->printable_name,
143                       strlen (info->printable_name)) == 0)
144     return TRUE;
145 
146   return FALSE;
147 }
148 
149 #define N(BITS, NUMBER, PRINT, DEFAULT, NEXT)			\
150   {								\
151     BITS,      /* Bits in a word.  */				\
152     BITS,      /* Bits in an address.  */			\
153     8,	       /* Bits in a byte.  */				\
154     bfd_arch_riscv,						\
155     NUMBER,							\
156     "riscv",							\
157     PRINT,							\
158     3,								\
159     DEFAULT,							\
160     riscv_compatible,						\
161     riscv_scan,							\
162     bfd_arch_default_fill,					\
163     NEXT,							\
164     0 /* Maximum offset of a reloc from the start of an insn.  */\
165   }
166 
167 /* This enum must be kept in the same order as arch_info_struct.  */
168 enum
169 {
170   I_riscv64,
171   I_riscv32
172 };
173 
174 #define NN(index) (&arch_info_struct[(index) + 1])
175 
176 /* This array must be kept in the same order as the anonymous enum above,
177    and each entry except the last should end with NN (my enum value).  */
178 static const bfd_arch_info_type arch_info_struct[] =
179 {
180   N (64, bfd_mach_riscv64, "riscv:rv64", FALSE, NN (I_riscv64)),
181   N (32, bfd_mach_riscv32, "riscv:rv32", FALSE, NULL)
182 };
183 
184 /* The default architecture is riscv:rv64.  */
185 
186 const bfd_arch_info_type bfd_riscv_arch =
187   N (64, 0, "riscv", TRUE, &arch_info_struct[0]);
188