Commit | Line | Data |
---|---|---|
e23eba97 | 1 | /* BFD backend for RISC-V |
250d07de | 2 | Copyright (C) 2011-2021 Free Software Foundation, Inc. |
e23eba97 NC |
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" | |
3d73d29e | 26 | #include "cpu-riscv.h" |
e23eba97 NC |
27 | |
28 | static const bfd_arch_info_type * | |
29 | riscv_compatible (const bfd_arch_info_type *a, const bfd_arch_info_type *b) | |
30 | { | |
31 | if (a->arch != b->arch) | |
32 | return NULL; | |
33 | ||
34 | /* Machine compatibility is checked in | |
35 | _bfd_riscv_elf_merge_private_bfd_data. */ | |
36 | ||
37 | return a; | |
38 | } | |
39 | ||
c35d018b JW |
40 | /* Return TRUE if STRING matches the architecture described by INFO. */ |
41 | ||
0a1b45a2 | 42 | static bool |
c35d018b JW |
43 | riscv_scan (const struct bfd_arch_info *info, const char *string) |
44 | { | |
45 | if (bfd_default_scan (info, string)) | |
0a1b45a2 | 46 | return true; |
c35d018b | 47 | |
069057bf AB |
48 | /* The incoming STRING might take the form of riscv:rvXXzzz, where XX is |
49 | 32 or 64, and zzz are one or more extension characters. As we | |
50 | currently only have 3 architectures defined, 'riscv', 'riscv:rv32', | |
51 | and 'riscv:rv64', we would like to ignore the zzz for the purpose of | |
52 | matching here. | |
53 | ||
54 | However, we don't want the default 'riscv' to match over a more | |
55 | specific 'riscv:rv32' or 'riscv:rv64', so in the case of the default | |
56 | architecture (with the shorter 'riscv' name) we don't allow any | |
57 | special matching, but for the 'riscv:rvXX' cases, we allow a match | |
58 | with any additional trailing characters being ignored. */ | |
59 | if (!info->the_default | |
60 | && strncasecmp (string, info->printable_name, | |
61 | strlen (info->printable_name)) == 0) | |
0a1b45a2 | 62 | return true; |
c35d018b | 63 | |
0a1b45a2 | 64 | return false; |
c35d018b JW |
65 | } |
66 | ||
aebcfb76 | 67 | #define N(BITS, NUMBER, PRINT, DEFAULT, NEXT) \ |
e23eba97 | 68 | { \ |
aebcfb76 NC |
69 | BITS, /* Bits in a word. */ \ |
70 | BITS, /* Bits in an address. */ \ | |
71 | 8, /* Bits in a byte. */ \ | |
e23eba97 NC |
72 | bfd_arch_riscv, \ |
73 | NUMBER, \ | |
74 | "riscv", \ | |
75 | PRINT, \ | |
76 | 3, \ | |
77 | DEFAULT, \ | |
78 | riscv_compatible, \ | |
c35d018b | 79 | riscv_scan, \ |
e23eba97 NC |
80 | bfd_arch_default_fill, \ |
81 | NEXT, \ | |
aebcfb76 | 82 | 0 /* Maximum offset of a reloc from the start of an insn. */\ |
e23eba97 NC |
83 | } |
84 | ||
85 | /* This enum must be kept in the same order as arch_info_struct. */ | |
86 | enum | |
87 | { | |
88 | I_riscv64, | |
89 | I_riscv32 | |
90 | }; | |
91 | ||
92 | #define NN(index) (&arch_info_struct[(index) + 1]) | |
93 | ||
94 | /* This array must be kept in the same order as the anonymous enum above, | |
95 | and each entry except the last should end with NN (my enum value). */ | |
96 | static const bfd_arch_info_type arch_info_struct[] = | |
97 | { | |
0a1b45a2 AM |
98 | N (64, bfd_mach_riscv64, "riscv:rv64", false, NN (I_riscv64)), |
99 | N (32, bfd_mach_riscv32, "riscv:rv32", false, NULL) | |
e23eba97 NC |
100 | }; |
101 | ||
102 | /* The default architecture is riscv:rv64. */ | |
e23eba97 | 103 | const bfd_arch_info_type bfd_riscv_arch = |
0a1b45a2 | 104 | N (64, 0, "riscv", true, &arch_info_struct[0]); |
3d73d29e NC |
105 | |
106 | /* List for all supported ISA spec versions. */ | |
107 | const struct riscv_spec riscv_isa_specs[] = | |
108 | { | |
109 | {"2.2", ISA_SPEC_CLASS_2P2}, | |
110 | {"20190608", ISA_SPEC_CLASS_20190608}, | |
111 | {"20191213", ISA_SPEC_CLASS_20191213}, | |
112 | }; | |
113 | ||
114 | /* List for all supported privileged spec versions. */ | |
115 | const struct riscv_spec riscv_priv_specs[] = | |
116 | { | |
117 | {"1.9.1", PRIV_SPEC_CLASS_1P9P1}, | |
118 | {"1.10", PRIV_SPEC_CLASS_1P10}, | |
119 | {"1.11", PRIV_SPEC_CLASS_1P11}, | |
120 | }; | |
121 | ||
122 | /* Get the corresponding CSR version class by giving privilege | |
123 | version numbers. It is usually used to convert the priv | |
124 | attribute numbers into the corresponding class. */ | |
125 | ||
126 | void | |
127 | riscv_get_priv_spec_class_from_numbers (unsigned int major, | |
128 | unsigned int minor, | |
129 | unsigned int revision, | |
130 | enum riscv_spec_class *class) | |
131 | { | |
132 | enum riscv_spec_class class_t = *class; | |
133 | char buf[36]; | |
134 | ||
135 | if (revision != 0) | |
136 | snprintf (buf, sizeof (buf), "%u.%u.%u", major, minor, revision); | |
137 | else | |
138 | snprintf (buf, sizeof (buf), "%u.%u", major, minor); | |
139 | ||
140 | RISCV_GET_PRIV_SPEC_CLASS (buf, class_t); | |
141 | *class = class_t; | |
142 | } |