Document the GDB 10.1 release in gdb/ChangeLog
[binutils-gdb.git] / bfd / cpu-riscv.c
blob2e9e9eb9d2875d28cab4f56fe049baee2d19ace9
1 /* BFD backend for RISC-V
2 Copyright (C) 2011-2020 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on MIPS target.
7 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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/>. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "libbfd.h"
26 #include "elfxx-riscv.h"
28 /* Record the priv spec version string and the corresponding class. */
30 struct priv_spec_t
32 const char *name;
33 enum riscv_priv_spec_class class;
36 /* List for all supported privilege versions. */
38 static const struct priv_spec_t priv_specs[] =
40 {"1.9.1", PRIV_SPEC_CLASS_1P9P1},
41 {"1.10", PRIV_SPEC_CLASS_1P10},
42 {"1.11", PRIV_SPEC_CLASS_1P11},
44 /* Terminate the list. */
45 {NULL, 0}
48 /* Get the corresponding CSR version class by giving a privilege
49 version string. */
51 int
52 riscv_get_priv_spec_class (const char *s,
53 enum riscv_priv_spec_class *class)
55 const struct priv_spec_t *version;
57 if (s == NULL)
58 return 0;
60 for (version = &priv_specs[0]; version->name != NULL; ++version)
61 if (strcmp (version->name, s) == 0)
63 *class = version->class;
64 return 1;
67 /* Can not find the supported privilege version. */
68 return 0;
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. */
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)
81 char buf[36];
83 if (major == 0 && minor == 0 && revision == 0)
85 *class = PRIV_SPEC_CLASS_NONE;
86 return 1;
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);
94 return riscv_get_priv_spec_class (buf, class);
97 /* Get the corresponding privilege version string by giving a CSR
98 version class. */
100 const char *
101 riscv_get_priv_spec_name (enum riscv_priv_spec_class class)
103 /* The first enum is PRIV_SPEC_CLASS_NONE. */
104 return priv_specs[class - 1].name;
107 /* This routine is provided two arch_infos and returns an arch_info
108 that is compatible with both, or NULL if none exists. */
110 static const bfd_arch_info_type *
111 riscv_compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b)
113 if (a->arch != b->arch)
114 return NULL;
116 /* Machine compatibility is checked in
117 _bfd_riscv_elf_merge_private_bfd_data. */
119 return a;
122 /* Return TRUE if STRING matches the architecture described by INFO. */
124 static bfd_boolean
125 riscv_scan (const struct bfd_arch_info *info, const char *string)
127 if (bfd_default_scan (info, string))
128 return TRUE;
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.
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;
146 return FALSE;
149 #define N(BITS, NUMBER, PRINT, DEFAULT, NEXT) \
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. */\
167 /* This enum must be kept in the same order as arch_info_struct. */
168 enum
170 I_riscv64,
171 I_riscv32
174 #define NN(index) (&arch_info_struct[(index) + 1])
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[] =
180 N (64, bfd_mach_riscv64, "riscv:rv64", FALSE, NN (I_riscv64)),
181 N (32, bfd_mach_riscv32, "riscv:rv32", FALSE, NULL)
184 /* The default architecture is riscv:rv64. */
186 const bfd_arch_info_type bfd_riscv_arch =
187 N (64, 0, "riscv", TRUE, &arch_info_struct[0]);