]>
Commit | Line | Data |
---|---|---|
e23eba97 | 1 | /* RISC-V disassembler |
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 the GNU opcodes library. | |
8 | ||
9 | This library 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, or (at your option) | |
12 | any later version. | |
13 | ||
14 | It is distributed in the hope that it will be useful, but WITHOUT | |
15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
16 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public | |
17 | 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" | |
88c1242d | 24 | #include "disassemble.h" |
e23eba97 NC |
25 | #include "libiberty.h" |
26 | #include "opcode/riscv.h" | |
27 | #include "opintl.h" | |
28 | #include "elf-bfd.h" | |
29 | #include "elf/riscv.h" | |
f786c359 | 30 | #include "elfxx-riscv.h" |
e23eba97 | 31 | |
3dfb1b6d | 32 | #include <stdint.h> |
e23eba97 NC |
33 | #include <ctype.h> |
34 | ||
f786c359 | 35 | static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1; |
3d73d29e | 36 | static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE; |
8f595e9b | 37 | |
f786c359 NC |
38 | unsigned xlen = 0; |
39 | ||
40 | static riscv_subset_list_t riscv_subsets; | |
41 | static riscv_parse_subset_t riscv_rps_dis = | |
42 | { | |
43 | &riscv_subsets, /* subset_list. */ | |
44 | opcodes_error_handler,/* error_handler. */ | |
45 | &xlen, /* xlen. */ | |
46 | &default_isa_spec, /* isa_spec. */ | |
47 | false, /* check_unknown_prefixed_ext. */ | |
48 | }; | |
49 | ||
e23eba97 NC |
50 | struct riscv_private_data |
51 | { | |
52 | bfd_vma gp; | |
53 | bfd_vma print_addr; | |
54 | bfd_vma hi_addr[OP_MASK_RD + 1]; | |
55 | }; | |
56 | ||
9b9b1092 NC |
57 | /* Used for mapping symbols. */ |
58 | static int last_map_symbol = -1; | |
59 | static bfd_vma last_stop_offset = 0; | |
60 | enum riscv_seg_mstate last_map_state; | |
61 | ||
e23eba97 NC |
62 | static const char * const *riscv_gpr_names; |
63 | static const char * const *riscv_fpr_names; | |
64 | ||
dcd709e0 NC |
65 | /* If set, disassemble as most general instruction. */ |
66 | static int no_aliases; | |
e23eba97 NC |
67 | |
68 | static void | |
69 | set_default_riscv_dis_options (void) | |
70 | { | |
71 | riscv_gpr_names = riscv_gpr_names_abi; | |
72 | riscv_fpr_names = riscv_fpr_names_abi; | |
73 | no_aliases = 0; | |
74 | } | |
75 | ||
78933a4a | 76 | static bool |
8f595e9b | 77 | parse_riscv_dis_option_without_args (const char *option) |
e23eba97 NC |
78 | { |
79 | if (strcmp (option, "no-aliases") == 0) | |
80 | no_aliases = 1; | |
81 | else if (strcmp (option, "numeric") == 0) | |
82 | { | |
83 | riscv_gpr_names = riscv_gpr_names_numeric; | |
84 | riscv_fpr_names = riscv_fpr_names_numeric; | |
85 | } | |
8f595e9b | 86 | else |
78933a4a AM |
87 | return false; |
88 | return true; | |
8f595e9b NC |
89 | } |
90 | ||
91 | static void | |
92 | parse_riscv_dis_option (const char *option) | |
93 | { | |
94 | char *equal, *value; | |
95 | ||
96 | if (parse_riscv_dis_option_without_args (option)) | |
97 | return; | |
98 | ||
99 | equal = strchr (option, '='); | |
100 | if (equal == NULL) | |
101 | { | |
102 | /* The option without '=' should be defined above. */ | |
103 | opcodes_error_handler (_("unrecognized disassembler option: %s"), option); | |
104 | return; | |
105 | } | |
106 | if (equal == option | |
107 | || *(equal + 1) == '\0') | |
108 | { | |
109 | /* Invalid options with '=', no option name before '=', | |
110 | and no value after '='. */ | |
111 | opcodes_error_handler (_("unrecognized disassembler option with '=': %s"), | |
112 | option); | |
113 | return; | |
114 | } | |
115 | ||
116 | *equal = '\0'; | |
117 | value = equal + 1; | |
118 | if (strcmp (option, "priv-spec") == 0) | |
119 | { | |
3d73d29e NC |
120 | enum riscv_spec_class priv_spec = PRIV_SPEC_CLASS_NONE; |
121 | const char *name = NULL; | |
122 | ||
123 | RISCV_GET_PRIV_SPEC_CLASS (value, priv_spec); | |
124 | if (priv_spec == PRIV_SPEC_CLASS_NONE) | |
b800637e | 125 | opcodes_error_handler (_("unknown privileged spec set by %s=%s"), |
8152e040 NC |
126 | option, value); |
127 | else if (default_priv_spec == PRIV_SPEC_CLASS_NONE) | |
128 | default_priv_spec = priv_spec; | |
129 | else if (default_priv_spec != priv_spec) | |
3d73d29e NC |
130 | { |
131 | RISCV_GET_PRIV_SPEC_NAME (name, default_priv_spec); | |
132 | opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, " | |
133 | "the elf privilege attribute is %s"), | |
134 | option, value, name); | |
135 | } | |
8f595e9b | 136 | } |
e23eba97 NC |
137 | else |
138 | { | |
a6743a54 AM |
139 | /* xgettext:c-format */ |
140 | opcodes_error_handler (_("unrecognized disassembler option: %s"), option); | |
e23eba97 NC |
141 | } |
142 | } | |
143 | ||
144 | static void | |
145 | parse_riscv_dis_options (const char *opts_in) | |
146 | { | |
147 | char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts; | |
148 | ||
149 | set_default_riscv_dis_options (); | |
150 | ||
151 | for ( ; opt_end != NULL; opt = opt_end + 1) | |
152 | { | |
153 | if ((opt_end = strchr (opt, ',')) != NULL) | |
154 | *opt_end = 0; | |
155 | parse_riscv_dis_option (opt); | |
156 | } | |
157 | ||
158 | free (opts); | |
159 | } | |
160 | ||
161 | /* Print one argument from an array. */ | |
162 | ||
163 | static void | |
164 | arg_print (struct disassemble_info *info, unsigned long val, | |
165 | const char* const* array, size_t size) | |
166 | { | |
167 | const char *s = val >= size || array[val] == NULL ? "unknown" : array[val]; | |
168 | (*info->fprintf_func) (info->stream, "%s", s); | |
169 | } | |
170 | ||
171 | static void | |
c7dee848 JW |
172 | maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset, |
173 | int wide) | |
e23eba97 NC |
174 | { |
175 | if (pd->hi_addr[base_reg] != (bfd_vma)-1) | |
176 | { | |
35fd2b2b | 177 | pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset; |
e23eba97 NC |
178 | pd->hi_addr[base_reg] = -1; |
179 | } | |
180 | else if (base_reg == X_GP && pd->gp != (bfd_vma)-1) | |
181 | pd->print_addr = pd->gp + offset; | |
182 | else if (base_reg == X_TP || base_reg == 0) | |
183 | pd->print_addr = offset; | |
c7dee848 JW |
184 | |
185 | /* Sign-extend a 32-bit value to a 64-bit value. */ | |
186 | if (wide) | |
187 | pd->print_addr = (bfd_vma)(int32_t) pd->print_addr; | |
e23eba97 NC |
188 | } |
189 | ||
190 | /* Print insn arguments for 32/64-bit code. */ | |
191 | ||
192 | static void | |
437e2ff1 | 193 | print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info) |
e23eba97 NC |
194 | { |
195 | struct riscv_private_data *pd = info->private_data; | |
196 | int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1; | |
197 | int rd = (l >> OP_SH_RD) & OP_MASK_RD; | |
198 | fprintf_ftype print = info->fprintf_func; | |
437e2ff1 | 199 | const char *opargStart; |
e23eba97 | 200 | |
437e2ff1 | 201 | if (*oparg != '\0') |
e23eba97 NC |
202 | print (info->stream, "\t"); |
203 | ||
437e2ff1 | 204 | for (; *oparg != '\0'; oparg++) |
e23eba97 | 205 | { |
437e2ff1 NC |
206 | opargStart = oparg; |
207 | switch (*oparg) | |
e23eba97 NC |
208 | { |
209 | case 'C': /* RVC */ | |
437e2ff1 | 210 | switch (*++oparg) |
e23eba97 | 211 | { |
dcd709e0 NC |
212 | case 's': /* RS1 x8-x15. */ |
213 | case 'w': /* RS1 x8-x15. */ | |
e23eba97 NC |
214 | print (info->stream, "%s", |
215 | riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]); | |
216 | break; | |
dcd709e0 NC |
217 | case 't': /* RS2 x8-x15. */ |
218 | case 'x': /* RS2 x8-x15. */ | |
e23eba97 NC |
219 | print (info->stream, "%s", |
220 | riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]); | |
221 | break; | |
dcd709e0 | 222 | case 'U': /* RS1, constrained to equal RD. */ |
e23eba97 NC |
223 | print (info->stream, "%s", riscv_gpr_names[rd]); |
224 | break; | |
dcd709e0 | 225 | case 'c': /* RS1, constrained to equal sp. */ |
e23eba97 NC |
226 | print (info->stream, "%s", riscv_gpr_names[X_SP]); |
227 | break; | |
228 | case 'V': /* RS2 */ | |
229 | print (info->stream, "%s", | |
230 | riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]); | |
231 | break; | |
f91d48de | 232 | case 'o': |
e23eba97 | 233 | case 'j': |
c7dee848 JW |
234 | if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0) |
235 | maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0); | |
236 | if (info->mach == bfd_mach_riscv64 | |
237 | && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0) | |
238 | maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1); | |
5a9f5403 | 239 | print (info->stream, "%d", (int)EXTRACT_CITYPE_IMM (l)); |
e23eba97 NC |
240 | break; |
241 | case 'k': | |
5a9f5403 | 242 | print (info->stream, "%d", (int)EXTRACT_CLTYPE_LW_IMM (l)); |
e23eba97 NC |
243 | break; |
244 | case 'l': | |
5a9f5403 | 245 | print (info->stream, "%d", (int)EXTRACT_CLTYPE_LD_IMM (l)); |
e23eba97 NC |
246 | break; |
247 | case 'm': | |
5a9f5403 | 248 | print (info->stream, "%d", (int)EXTRACT_CITYPE_LWSP_IMM (l)); |
e23eba97 NC |
249 | break; |
250 | case 'n': | |
5a9f5403 | 251 | print (info->stream, "%d", (int)EXTRACT_CITYPE_LDSP_IMM (l)); |
e23eba97 NC |
252 | break; |
253 | case 'K': | |
5a9f5403 | 254 | print (info->stream, "%d", (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l)); |
e23eba97 NC |
255 | break; |
256 | case 'L': | |
5a9f5403 | 257 | print (info->stream, "%d", (int)EXTRACT_CITYPE_ADDI16SP_IMM (l)); |
e23eba97 NC |
258 | break; |
259 | case 'M': | |
5a9f5403 | 260 | print (info->stream, "%d", (int)EXTRACT_CSSTYPE_SWSP_IMM (l)); |
e23eba97 NC |
261 | break; |
262 | case 'N': | |
5a9f5403 | 263 | print (info->stream, "%d", (int)EXTRACT_CSSTYPE_SDSP_IMM (l)); |
e23eba97 NC |
264 | break; |
265 | case 'p': | |
5a9f5403 | 266 | info->target = EXTRACT_CBTYPE_IMM (l) + pc; |
e23eba97 NC |
267 | (*info->print_address_func) (info->target, info); |
268 | break; | |
269 | case 'a': | |
5a9f5403 | 270 | info->target = EXTRACT_CJTYPE_IMM (l) + pc; |
e23eba97 NC |
271 | (*info->print_address_func) (info->target, info); |
272 | break; | |
273 | case 'u': | |
274 | print (info->stream, "0x%x", | |
5a9f5403 | 275 | (int)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1))); |
e23eba97 NC |
276 | break; |
277 | case '>': | |
5a9f5403 | 278 | print (info->stream, "0x%x", (int)EXTRACT_CITYPE_IMM (l) & 0x3f); |
e23eba97 NC |
279 | break; |
280 | case '<': | |
5a9f5403 | 281 | print (info->stream, "0x%x", (int)EXTRACT_CITYPE_IMM (l) & 0x1f); |
e23eba97 | 282 | break; |
dcd709e0 | 283 | case 'T': /* Floating-point RS2. */ |
e23eba97 NC |
284 | print (info->stream, "%s", |
285 | riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]); | |
286 | break; | |
dcd709e0 | 287 | case 'D': /* Floating-point RS2 x8-x15. */ |
e23eba97 NC |
288 | print (info->stream, "%s", |
289 | riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]); | |
290 | break; | |
291 | } | |
292 | break; | |
293 | ||
294 | case ',': | |
295 | case '(': | |
296 | case ')': | |
297 | case '[': | |
298 | case ']': | |
437e2ff1 | 299 | print (info->stream, "%c", *oparg); |
e23eba97 NC |
300 | break; |
301 | ||
302 | case '0': | |
dcd709e0 | 303 | /* Only print constant 0 if it is the last argument. */ |
437e2ff1 | 304 | if (!oparg[1]) |
e23eba97 NC |
305 | print (info->stream, "0"); |
306 | break; | |
307 | ||
308 | case 'b': | |
309 | case 's': | |
35eeb78f | 310 | if ((l & MASK_JALR) == MATCH_JALR) |
c7dee848 | 311 | maybe_print_address (pd, rs1, 0, 0); |
e23eba97 NC |
312 | print (info->stream, "%s", riscv_gpr_names[rs1]); |
313 | break; | |
314 | ||
315 | case 't': | |
316 | print (info->stream, "%s", | |
317 | riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]); | |
318 | break; | |
319 | ||
320 | case 'u': | |
321 | print (info->stream, "0x%x", | |
322 | (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS); | |
323 | break; | |
324 | ||
325 | case 'm': | |
326 | arg_print (info, EXTRACT_OPERAND (RM, l), | |
327 | riscv_rm, ARRAY_SIZE (riscv_rm)); | |
328 | break; | |
329 | ||
330 | case 'P': | |
331 | arg_print (info, EXTRACT_OPERAND (PRED, l), | |
332 | riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ)); | |
333 | break; | |
334 | ||
335 | case 'Q': | |
336 | arg_print (info, EXTRACT_OPERAND (SUCC, l), | |
337 | riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ)); | |
338 | break; | |
339 | ||
340 | case 'o': | |
c7dee848 | 341 | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0); |
b52d3cfc | 342 | /* Fall through. */ |
e23eba97 NC |
343 | case 'j': |
344 | if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0) | |
345 | || (l & MASK_JALR) == MATCH_JALR) | |
c7dee848 JW |
346 | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0); |
347 | if (info->mach == bfd_mach_riscv64 | |
348 | && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0) | |
349 | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1); | |
e23eba97 NC |
350 | print (info->stream, "%d", (int)EXTRACT_ITYPE_IMM (l)); |
351 | break; | |
352 | ||
353 | case 'q': | |
c7dee848 | 354 | maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0); |
e23eba97 NC |
355 | print (info->stream, "%d", (int)EXTRACT_STYPE_IMM (l)); |
356 | break; | |
357 | ||
358 | case 'a': | |
5a9f5403 | 359 | info->target = EXTRACT_JTYPE_IMM (l) + pc; |
e23eba97 NC |
360 | (*info->print_address_func) (info->target, info); |
361 | break; | |
362 | ||
363 | case 'p': | |
5a9f5403 | 364 | info->target = EXTRACT_BTYPE_IMM (l) + pc; |
e23eba97 NC |
365 | (*info->print_address_func) (info->target, info); |
366 | break; | |
367 | ||
368 | case 'd': | |
369 | if ((l & MASK_AUIPC) == MATCH_AUIPC) | |
370 | pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l); | |
371 | else if ((l & MASK_LUI) == MATCH_LUI) | |
372 | pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l); | |
373 | else if ((l & MASK_C_LUI) == MATCH_C_LUI) | |
5a9f5403 | 374 | pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l); |
e23eba97 NC |
375 | print (info->stream, "%s", riscv_gpr_names[rd]); |
376 | break; | |
377 | ||
3d1cafa0 | 378 | case 'y': |
379 | print (info->stream, "0x%x", (int)EXTRACT_OPERAND (BS, l)); | |
380 | break; | |
381 | ||
e23eba97 NC |
382 | case 'z': |
383 | print (info->stream, "%s", riscv_gpr_names[0]); | |
384 | break; | |
385 | ||
386 | case '>': | |
387 | print (info->stream, "0x%x", (int)EXTRACT_OPERAND (SHAMT, l)); | |
388 | break; | |
389 | ||
390 | case '<': | |
391 | print (info->stream, "0x%x", (int)EXTRACT_OPERAND (SHAMTW, l)); | |
392 | break; | |
393 | ||
394 | case 'S': | |
395 | case 'U': | |
396 | print (info->stream, "%s", riscv_fpr_names[rs1]); | |
397 | break; | |
398 | ||
399 | case 'T': | |
400 | print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]); | |
401 | break; | |
402 | ||
403 | case 'D': | |
404 | print (info->stream, "%s", riscv_fpr_names[rd]); | |
405 | break; | |
406 | ||
407 | case 'R': | |
408 | print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]); | |
409 | break; | |
410 | ||
411 | case 'E': | |
412 | { | |
dcd709e0 | 413 | static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */ |
78933a4a | 414 | static bool init_csr = false; |
e23eba97 | 415 | unsigned int csr = EXTRACT_OPERAND (CSR, l); |
8f595e9b NC |
416 | |
417 | if (!init_csr) | |
e23eba97 | 418 | { |
8f595e9b NC |
419 | unsigned int i; |
420 | for (i = 0; i < 4096; i++) | |
421 | riscv_csr_hash[i] = NULL; | |
422 | ||
dcd709e0 | 423 | /* Set to the newest privileged version. */ |
8f595e9b NC |
424 | if (default_priv_spec == PRIV_SPEC_CLASS_NONE) |
425 | default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1; | |
426 | ||
08ccfccf NC |
427 | #define DECLARE_CSR(name, num, class, define_version, abort_version) \ |
428 | if (riscv_csr_hash[num] == NULL \ | |
429 | && ((define_version == PRIV_SPEC_CLASS_NONE \ | |
430 | && abort_version == PRIV_SPEC_CLASS_NONE) \ | |
431 | || (default_priv_spec >= define_version \ | |
432 | && default_priv_spec < abort_version))) \ | |
8f595e9b NC |
433 | riscv_csr_hash[num] = #name; |
434 | #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \ | |
435 | DECLARE_CSR (name, num, class, define_version, abort_version) | |
e23eba97 NC |
436 | #include "opcode/riscv-opc.h" |
437 | #undef DECLARE_CSR | |
438 | } | |
8f595e9b NC |
439 | |
440 | if (riscv_csr_hash[csr] != NULL) | |
441 | print (info->stream, "%s", riscv_csr_hash[csr]); | |
e23eba97 NC |
442 | else |
443 | print (info->stream, "0x%x", csr); | |
444 | break; | |
445 | } | |
446 | ||
3d1cafa0 | 447 | case 'Y': |
448 | print (info->stream, "0x%x", (int)EXTRACT_OPERAND (RNUM, l)); | |
449 | break; | |
450 | ||
e23eba97 NC |
451 | case 'Z': |
452 | print (info->stream, "%d", rs1); | |
453 | break; | |
454 | ||
455 | default: | |
456 | /* xgettext:c-format */ | |
457 | print (info->stream, _("# internal error, undefined modifier (%c)"), | |
437e2ff1 | 458 | *opargStart); |
e23eba97 NC |
459 | return; |
460 | } | |
461 | } | |
462 | } | |
463 | ||
464 | /* Print the RISC-V instruction at address MEMADDR in debugged memory, | |
465 | on using INFO. Returns length of the instruction, in bytes. | |
466 | BIGENDIAN must be 1 if this is big-endian code, 0 if | |
467 | this is little-endian code. */ | |
468 | ||
469 | static int | |
470 | riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info) | |
471 | { | |
472 | const struct riscv_opcode *op; | |
78933a4a | 473 | static bool init = 0; |
e23eba97 NC |
474 | static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1]; |
475 | struct riscv_private_data *pd; | |
476 | int insnlen; | |
477 | ||
478 | #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP)) | |
479 | ||
480 | /* Build a hash table to shorten the search time. */ | |
481 | if (! init) | |
482 | { | |
483 | for (op = riscv_opcodes; op->name; op++) | |
484 | if (!riscv_hash[OP_HASH_IDX (op->match)]) | |
485 | riscv_hash[OP_HASH_IDX (op->match)] = op; | |
486 | ||
487 | init = 1; | |
488 | } | |
489 | ||
490 | if (info->private_data == NULL) | |
491 | { | |
492 | int i; | |
493 | ||
494 | pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data)); | |
495 | pd->gp = -1; | |
496 | pd->print_addr = -1; | |
497 | for (i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++) | |
498 | pd->hi_addr[i] = -1; | |
499 | ||
500 | for (i = 0; i < info->symtab_size; i++) | |
b5292032 | 501 | if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0) |
e23eba97 NC |
502 | pd->gp = bfd_asymbol_value (info->symtab[i]); |
503 | } | |
504 | else | |
505 | pd = info->private_data; | |
506 | ||
507 | insnlen = riscv_insn_length (word); | |
508 | ||
d7560e2d JW |
509 | /* RISC-V instructions are always little-endian. */ |
510 | info->endian_code = BFD_ENDIAN_LITTLE; | |
511 | ||
e23eba97 NC |
512 | info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2; |
513 | info->bytes_per_line = 8; | |
d7560e2d JW |
514 | /* We don't support constant pools, so this must be code. */ |
515 | info->display_endian = info->endian_code; | |
e23eba97 NC |
516 | info->insn_info_valid = 1; |
517 | info->branch_delay_insns = 0; | |
518 | info->data_size = 0; | |
519 | info->insn_type = dis_nonbranch; | |
520 | info->target = 0; | |
521 | info->target2 = 0; | |
522 | ||
523 | op = riscv_hash[OP_HASH_IDX (word)]; | |
524 | if (op != NULL) | |
525 | { | |
2922d21d AW |
526 | /* If XLEN is not known, get its value from the ELF class. */ |
527 | if (info->mach == bfd_mach_riscv64) | |
528 | xlen = 64; | |
529 | else if (info->mach == bfd_mach_riscv32) | |
530 | xlen = 32; | |
531 | else if (info->section != NULL) | |
e23eba97 NC |
532 | { |
533 | Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner); | |
534 | xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32; | |
535 | } | |
536 | ||
537 | for (; op->name; op++) | |
538 | { | |
539 | /* Does the opcode match? */ | |
540 | if (! (op->match_func) (op, word)) | |
541 | continue; | |
542 | /* Is this a pseudo-instruction and may we print it as such? */ | |
543 | if (no_aliases && (op->pinfo & INSN_ALIAS)) | |
544 | continue; | |
545 | /* Is this instruction restricted to a certain value of XLEN? */ | |
43135d3b | 546 | if ((op->xlen_requirement != 0) && (op->xlen_requirement != xlen)) |
e23eba97 NC |
547 | continue; |
548 | ||
f786c359 NC |
549 | if (!riscv_multi_subset_supports (&riscv_rps_dis, op->insn_class)) |
550 | continue; | |
551 | ||
e23eba97 NC |
552 | /* It's a match. */ |
553 | (*info->fprintf_func) (info->stream, "%s", op->name); | |
554 | print_insn_args (op->args, word, memaddr, info); | |
555 | ||
556 | /* Try to disassemble multi-instruction addressing sequences. */ | |
557 | if (pd->print_addr != (bfd_vma)-1) | |
558 | { | |
559 | info->target = pd->print_addr; | |
560 | (*info->fprintf_func) (info->stream, " # "); | |
561 | (*info->print_address_func) (info->target, info); | |
562 | pd->print_addr = -1; | |
563 | } | |
564 | ||
eb41b248 JW |
565 | /* Finish filling out insn_info fields. */ |
566 | switch (op->pinfo & INSN_TYPE) | |
567 | { | |
568 | case INSN_BRANCH: | |
569 | info->insn_type = dis_branch; | |
570 | break; | |
571 | case INSN_CONDBRANCH: | |
572 | info->insn_type = dis_condbranch; | |
573 | break; | |
574 | case INSN_JSR: | |
575 | info->insn_type = dis_jsr; | |
576 | break; | |
577 | case INSN_DREF: | |
578 | info->insn_type = dis_dref; | |
579 | break; | |
580 | default: | |
581 | break; | |
582 | } | |
583 | ||
584 | if (op->pinfo & INSN_DATA_SIZE) | |
585 | { | |
586 | int size = ((op->pinfo & INSN_DATA_SIZE) | |
587 | >> INSN_DATA_SIZE_SHIFT); | |
588 | info->data_size = 1 << (size - 1); | |
589 | } | |
590 | ||
e23eba97 NC |
591 | return insnlen; |
592 | } | |
593 | } | |
594 | ||
595 | /* We did not find a match, so just print the instruction bits. */ | |
596 | info->insn_type = dis_noninsn; | |
6a7f5766 AB |
597 | switch (insnlen) |
598 | { | |
599 | case 2: | |
600 | case 4: | |
601 | case 8: | |
602 | (*info->fprintf_func) (info->stream, ".%dbyte\t0x%llx", | |
603 | insnlen, (unsigned long long) word); | |
604 | break; | |
605 | default: | |
606 | { | |
607 | int i; | |
608 | (*info->fprintf_func) (info->stream, ".byte\t"); | |
609 | for (i = 0; i < insnlen; ++i) | |
610 | { | |
611 | if (i > 0) | |
612 | (*info->fprintf_func) (info->stream, ", "); | |
613 | (*info->fprintf_func) (info->stream, "0x%02x", | |
614 | (unsigned int) (word & 0xff)); | |
615 | word >>= 8; | |
616 | } | |
617 | } | |
618 | break; | |
619 | } | |
e23eba97 NC |
620 | return insnlen; |
621 | } | |
622 | ||
9b9b1092 NC |
623 | /* Return true if we find the suitable mapping symbol, |
624 | and also update the STATE. Otherwise, return false. */ | |
625 | ||
626 | static bool | |
627 | riscv_get_map_state (int n, | |
628 | enum riscv_seg_mstate *state, | |
629 | struct disassemble_info *info) | |
630 | { | |
631 | const char *name; | |
632 | ||
633 | /* If the symbol is in a different section, ignore it. */ | |
634 | if (info->section != NULL | |
635 | && info->section != info->symtab[n]->section) | |
636 | return false; | |
637 | ||
638 | name = bfd_asymbol_name(info->symtab[n]); | |
639 | if (strcmp (name, "$x") == 0) | |
640 | *state = MAP_INSN; | |
641 | else if (strcmp (name, "$d") == 0) | |
642 | *state = MAP_DATA; | |
643 | else | |
644 | return false; | |
645 | ||
646 | return true; | |
647 | } | |
648 | ||
649 | /* Check the sorted symbol table (sorted by the symbol value), find the | |
650 | suitable mapping symbols. */ | |
651 | ||
652 | static enum riscv_seg_mstate | |
653 | riscv_search_mapping_symbol (bfd_vma memaddr, | |
654 | struct disassemble_info *info) | |
655 | { | |
656 | enum riscv_seg_mstate mstate; | |
657 | bool from_last_map_symbol; | |
658 | bool found = false; | |
659 | int symbol = -1; | |
660 | int n; | |
661 | ||
662 | /* Decide whether to print the data or instruction by default, in case | |
663 | we can not find the corresponding mapping symbols. */ | |
664 | mstate = MAP_DATA; | |
665 | if ((info->section | |
666 | && info->section->flags & SEC_CODE) | |
667 | || !info->section) | |
668 | mstate = MAP_INSN; | |
669 | ||
670 | if (info->symtab_size == 0 | |
671 | || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour) | |
672 | return mstate; | |
673 | ||
674 | /* Reset the last_map_symbol if we start to dump a new section. */ | |
675 | if (memaddr <= 0) | |
676 | last_map_symbol = -1; | |
677 | ||
678 | /* If the last stop offset is different from the current one, then | |
679 | don't use the last_map_symbol to search. We usually reset the | |
680 | info->stop_offset when handling a new section. */ | |
681 | from_last_map_symbol = (last_map_symbol >= 0 | |
682 | && info->stop_offset == last_stop_offset); | |
683 | ||
684 | /* Start scanning at the start of the function, or wherever | |
685 | we finished last time. */ | |
686 | n = info->symtab_pos + 1; | |
687 | if (from_last_map_symbol && n >= last_map_symbol) | |
688 | n = last_map_symbol; | |
689 | ||
690 | /* Find the suitable mapping symbol to dump. */ | |
691 | for (; n < info->symtab_size; n++) | |
692 | { | |
693 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); | |
694 | /* We have searched all possible symbols in the range. */ | |
695 | if (addr > memaddr) | |
696 | break; | |
697 | if (riscv_get_map_state (n, &mstate, info)) | |
698 | { | |
699 | symbol = n; | |
700 | found = true; | |
701 | /* Do not stop searching, in case there are some mapping | |
702 | symbols have the same value, but have different names. | |
703 | Use the last one. */ | |
704 | } | |
705 | } | |
706 | ||
707 | /* We can not find the suitable mapping symbol above. Therefore, we | |
708 | look forwards and try to find it again, but don't go pass the start | |
709 | of the section. Otherwise a data section without mapping symbols | |
710 | can pick up a text mapping symbol of a preceeding section. */ | |
711 | if (!found) | |
712 | { | |
713 | n = info->symtab_pos; | |
714 | if (from_last_map_symbol && n >= last_map_symbol) | |
715 | n = last_map_symbol; | |
716 | ||
717 | for (; n >= 0; n--) | |
718 | { | |
719 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); | |
720 | /* We have searched all possible symbols in the range. */ | |
721 | if (addr < (info->section ? info->section->vma : 0)) | |
722 | break; | |
723 | /* Stop searching once we find the closed mapping symbol. */ | |
724 | if (riscv_get_map_state (n, &mstate, info)) | |
725 | { | |
726 | symbol = n; | |
727 | found = true; | |
728 | break; | |
729 | } | |
730 | } | |
731 | } | |
732 | ||
733 | /* Save the information for next use. */ | |
734 | last_map_symbol = symbol; | |
735 | last_stop_offset = info->stop_offset; | |
736 | ||
737 | return mstate; | |
738 | } | |
739 | ||
740 | /* Decide which data size we should print. */ | |
741 | ||
742 | static bfd_vma | |
743 | riscv_data_length (bfd_vma memaddr, | |
744 | disassemble_info *info) | |
745 | { | |
746 | bfd_vma length; | |
747 | bool found = false; | |
748 | ||
749 | length = 4; | |
750 | if (info->symtab_size != 0 | |
751 | && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour | |
752 | && last_map_symbol >= 0) | |
753 | { | |
754 | int n; | |
755 | enum riscv_seg_mstate m = MAP_NONE; | |
756 | for (n = last_map_symbol + 1; n < info->symtab_size; n++) | |
757 | { | |
758 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); | |
759 | if (addr > memaddr | |
760 | && riscv_get_map_state (n, &m, info)) | |
761 | { | |
762 | if (addr - memaddr < length) | |
763 | length = addr - memaddr; | |
764 | found = true; | |
765 | break; | |
766 | } | |
767 | } | |
768 | } | |
769 | if (!found) | |
770 | { | |
771 | /* Do not set the length which exceeds the section size. */ | |
772 | bfd_vma offset = info->section->vma + info->section->size; | |
773 | offset -= memaddr; | |
774 | length = (offset < length) ? offset : length; | |
775 | } | |
776 | length = length == 3 ? 2 : length; | |
777 | return length; | |
778 | } | |
779 | ||
780 | /* Dump the data contents. */ | |
781 | ||
782 | static int | |
783 | riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED, | |
784 | insn_t data, | |
785 | disassemble_info *info) | |
786 | { | |
787 | info->display_endian = info->endian; | |
788 | ||
789 | switch (info->bytes_per_chunk) | |
790 | { | |
791 | case 1: | |
792 | info->bytes_per_line = 6; | |
793 | (*info->fprintf_func) (info->stream, ".byte\t0x%02llx", | |
794 | (unsigned long long) data); | |
795 | break; | |
796 | case 2: | |
797 | info->bytes_per_line = 8; | |
798 | (*info->fprintf_func) (info->stream, ".short\t0x%04llx", | |
799 | (unsigned long long) data); | |
800 | break; | |
801 | case 4: | |
802 | info->bytes_per_line = 8; | |
803 | (*info->fprintf_func) (info->stream, ".word\t0x%08llx", | |
804 | (unsigned long long) data); | |
805 | break; | |
806 | case 8: | |
807 | info->bytes_per_line = 8; | |
808 | (*info->fprintf_func) (info->stream, ".dword\t0x%016llx", | |
809 | (unsigned long long) data); | |
810 | break; | |
811 | default: | |
812 | abort (); | |
813 | } | |
814 | return info->bytes_per_chunk; | |
815 | } | |
816 | ||
e23eba97 NC |
817 | int |
818 | print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info) | |
819 | { | |
9b9b1092 | 820 | bfd_byte packet[8]; |
e23eba97 | 821 | insn_t insn = 0; |
9b9b1092 | 822 | bfd_vma dump_size; |
e23eba97 | 823 | int status; |
9b9b1092 NC |
824 | enum riscv_seg_mstate mstate; |
825 | int (*riscv_disassembler) (bfd_vma, insn_t, struct disassemble_info *); | |
e23eba97 NC |
826 | |
827 | if (info->disassembler_options != NULL) | |
828 | { | |
829 | parse_riscv_dis_options (info->disassembler_options); | |
830 | /* Avoid repeatedly parsing the options. */ | |
831 | info->disassembler_options = NULL; | |
832 | } | |
833 | else if (riscv_gpr_names == NULL) | |
834 | set_default_riscv_dis_options (); | |
835 | ||
9b9b1092 NC |
836 | mstate = riscv_search_mapping_symbol (memaddr, info); |
837 | /* Save the last mapping state. */ | |
838 | last_map_state = mstate; | |
839 | ||
840 | /* Set the size to dump. */ | |
841 | if (mstate == MAP_DATA | |
842 | && (info->flags & DISASSEMBLE_DATA) == 0) | |
843 | { | |
844 | dump_size = riscv_data_length (memaddr, info); | |
845 | info->bytes_per_chunk = dump_size; | |
846 | riscv_disassembler = riscv_disassemble_data; | |
847 | } | |
848 | else | |
e23eba97 | 849 | { |
9b9b1092 NC |
850 | /* Get the first 2-bytes to check the lenghth of instruction. */ |
851 | status = (*info->read_memory_func) (memaddr, packet, 2, info); | |
e23eba97 NC |
852 | if (status != 0) |
853 | { | |
e23eba97 | 854 | (*info->memory_error_func) (status, memaddr, info); |
685bb4e8 | 855 | return status; |
e23eba97 | 856 | } |
9b9b1092 NC |
857 | insn = (insn_t) bfd_getl16 (packet); |
858 | dump_size = riscv_insn_length (insn); | |
859 | riscv_disassembler = riscv_disassemble_insn; | |
860 | } | |
e23eba97 | 861 | |
9b9b1092 NC |
862 | /* Fetch the instruction to dump. */ |
863 | status = (*info->read_memory_func) (memaddr, packet, dump_size, info); | |
864 | if (status != 0) | |
865 | { | |
866 | (*info->memory_error_func) (status, memaddr, info); | |
685bb4e8 | 867 | return status; |
e23eba97 | 868 | } |
9b9b1092 | 869 | insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false); |
e23eba97 | 870 | |
9b9b1092 | 871 | return (*riscv_disassembler) (memaddr, insn, info); |
e23eba97 NC |
872 | } |
873 | ||
8152e040 NC |
874 | disassembler_ftype |
875 | riscv_get_disassembler (bfd *abfd) | |
876 | { | |
f786c359 NC |
877 | const char *default_arch = "rv64gc"; |
878 | ||
8152e040 NC |
879 | if (abfd) |
880 | { | |
113bb761 JN |
881 | const struct elf_backend_data *ebd = get_elf_backend_data (abfd); |
882 | if (ebd) | |
f786c359 | 883 | { |
113bb761 JN |
884 | const char *sec_name = ebd->obj_attrs_section; |
885 | if (bfd_get_section_by_name (abfd, sec_name) != NULL) | |
886 | { | |
887 | obj_attribute *attr = elf_known_obj_attributes_proc (abfd); | |
888 | unsigned int Tag_a = Tag_RISCV_priv_spec; | |
889 | unsigned int Tag_b = Tag_RISCV_priv_spec_minor; | |
890 | unsigned int Tag_c = Tag_RISCV_priv_spec_revision; | |
891 | riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i, | |
892 | attr[Tag_b].i, | |
893 | attr[Tag_c].i, | |
894 | &default_priv_spec); | |
f786c359 | 895 | default_arch = attr[Tag_RISCV_arch].s; |
113bb761 | 896 | } |
f786c359 | 897 | } |
8152e040 | 898 | } |
f786c359 NC |
899 | |
900 | riscv_release_subset_list (&riscv_subsets); | |
901 | riscv_parse_subset (&riscv_rps_dis, default_arch); | |
902 | return print_insn_riscv; | |
8152e040 NC |
903 | } |
904 | ||
884b49e3 AB |
905 | /* Prevent use of the fake labels that are generated as part of the DWARF |
906 | and for relaxable relocations in the assembler. */ | |
907 | ||
78933a4a | 908 | bool |
884b49e3 AB |
909 | riscv_symbol_is_valid (asymbol * sym, |
910 | struct disassemble_info * info ATTRIBUTE_UNUSED) | |
911 | { | |
912 | const char * name; | |
913 | ||
914 | if (sym == NULL) | |
78933a4a | 915 | return false; |
884b49e3 AB |
916 | |
917 | name = bfd_asymbol_name (sym); | |
918 | ||
9b9b1092 NC |
919 | return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0 |
920 | && !riscv_elf_is_mapping_symbols (name)); | |
884b49e3 AB |
921 | } |
922 | ||
e23eba97 NC |
923 | void |
924 | print_riscv_disassembler_options (FILE *stream) | |
925 | { | |
926 | fprintf (stream, _("\n\ | |
927 | The following RISC-V-specific disassembler options are supported for use\n\ | |
928 | with the -M switch (multiple options should be separated by commas):\n")); | |
929 | ||
930 | fprintf (stream, _("\n\ | |
8f595e9b NC |
931 | numeric Print numeric register names, rather than ABI names.\n")); |
932 | ||
933 | fprintf (stream, _("\n\ | |
934 | no-aliases Disassemble only into canonical instructions, rather\n\ | |
935 | than into pseudoinstructions.\n")); | |
e23eba97 NC |
936 | |
937 | fprintf (stream, _("\n\ | |
8f595e9b NC |
938 | priv-spec=PRIV Print the CSR according to the chosen privilege spec\n\ |
939 | (1.9, 1.9.1, 1.10, 1.11).\n")); | |
e23eba97 NC |
940 | |
941 | fprintf (stream, _("\n")); | |
942 | } |