gdb/arm: Fix obvious typo in b0b23e06c3a
[binutils-gdb.git] / opcodes / riscv-dis.c
blob3a31647a2f80e3e97fdc727bdae9c702f7ddb799
1 /* RISC-V disassembler
2 Copyright (C) 2011-2022 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on MIPS target.
7 This file is part of the GNU opcodes library.
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.
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.
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 "disassemble.h"
25 #include "libiberty.h"
26 #include "opcode/riscv.h"
27 #include "opintl.h"
28 #include "elf-bfd.h"
29 #include "elf/riscv.h"
30 #include "elfxx-riscv.h"
32 #include <stdint.h>
33 #include <ctype.h>
35 /* Current XLEN for the disassembler. */
36 static unsigned xlen = 0;
38 /* Default ISA specification version (constant as of now). */
39 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
41 /* Default privileged specification
42 (as specified by the ELF attributes or the `priv-spec' option). */
43 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
45 static riscv_subset_list_t riscv_subsets;
46 static riscv_parse_subset_t riscv_rps_dis =
48 &riscv_subsets, /* subset_list. */
49 opcodes_error_handler,/* error_handler. */
50 &xlen, /* xlen. */
51 &default_isa_spec, /* isa_spec. */
52 false, /* check_unknown_prefixed_ext. */
55 struct riscv_private_data
57 bfd_vma gp;
58 bfd_vma print_addr;
59 bfd_vma hi_addr[OP_MASK_RD + 1];
60 bool to_print_addr;
61 bool has_gp;
64 /* Used for mapping symbols. */
65 static int last_map_symbol = -1;
66 static bfd_vma last_stop_offset = 0;
68 /* Register names as used by the disassembler. */
69 static const char * const *riscv_gpr_names;
70 static const char * const *riscv_fpr_names;
72 /* If set, disassemble as most general instruction. */
73 static bool no_aliases = false;
76 /* Set default RISC-V disassembler options. */
78 static void
79 set_default_riscv_dis_options (void)
81 riscv_gpr_names = riscv_gpr_names_abi;
82 riscv_fpr_names = riscv_fpr_names_abi;
83 no_aliases = false;
86 /* Parse RISC-V disassembler option (without arguments). */
88 static bool
89 parse_riscv_dis_option_without_args (const char *option)
91 if (strcmp (option, "no-aliases") == 0)
92 no_aliases = true;
93 else if (strcmp (option, "numeric") == 0)
95 riscv_gpr_names = riscv_gpr_names_numeric;
96 riscv_fpr_names = riscv_fpr_names_numeric;
98 else
99 return false;
100 return true;
103 /* Parse RISC-V disassembler option (possibly with arguments). */
105 static void
106 parse_riscv_dis_option (const char *option)
108 char *equal, *value;
110 if (parse_riscv_dis_option_without_args (option))
111 return;
113 equal = strchr (option, '=');
114 if (equal == NULL)
116 /* The option without '=' should be defined above. */
117 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
118 return;
120 if (equal == option
121 || *(equal + 1) == '\0')
123 /* Invalid options with '=', no option name before '=',
124 and no value after '='. */
125 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
126 option);
127 return;
130 *equal = '\0';
131 value = equal + 1;
132 if (strcmp (option, "priv-spec") == 0)
134 enum riscv_spec_class priv_spec = PRIV_SPEC_CLASS_NONE;
135 const char *name = NULL;
137 RISCV_GET_PRIV_SPEC_CLASS (value, priv_spec);
138 if (priv_spec == PRIV_SPEC_CLASS_NONE)
139 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
140 option, value);
141 else if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
142 default_priv_spec = priv_spec;
143 else if (default_priv_spec != priv_spec)
145 RISCV_GET_PRIV_SPEC_NAME (name, default_priv_spec);
146 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
147 "the elf privilege attribute is %s"),
148 option, value, name);
151 else
153 /* xgettext:c-format */
154 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
158 /* Parse RISC-V disassembler options. */
160 static void
161 parse_riscv_dis_options (const char *opts_in)
163 char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
165 set_default_riscv_dis_options ();
167 for ( ; opt_end != NULL; opt = opt_end + 1)
169 if ((opt_end = strchr (opt, ',')) != NULL)
170 *opt_end = 0;
171 parse_riscv_dis_option (opt);
174 free (opts);
177 /* Print one argument from an array. */
179 static void
180 arg_print (struct disassemble_info *info, unsigned long val,
181 const char* const* array, size_t size)
183 const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
184 (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s);
187 /* If we need to print an address, set its value and state. */
189 static void
190 maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset,
191 int wide)
193 if (pd->hi_addr[base_reg] != (bfd_vma)-1)
195 pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
196 pd->hi_addr[base_reg] = -1;
198 else if (base_reg == X_GP && pd->has_gp)
199 pd->print_addr = pd->gp + offset;
200 else if (base_reg == X_TP || base_reg == 0)
201 pd->print_addr = offset;
202 else
203 return; /* Don't print the address. */
204 pd->to_print_addr = true;
206 /* Sign-extend a 32-bit value to a 64-bit value. */
207 if (wide)
208 pd->print_addr = (bfd_vma)(int32_t) pd->print_addr;
210 /* Fit into a 32-bit value on RV32. */
211 if (xlen == 32)
212 pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr;
215 /* Print insn arguments for 32/64-bit code. */
217 static void
218 print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info)
220 struct riscv_private_data *pd = info->private_data;
221 int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
222 int rd = (l >> OP_SH_RD) & OP_MASK_RD;
223 fprintf_styled_ftype print = info->fprintf_styled_func;
224 const char *opargStart;
226 if (*oparg != '\0')
227 print (info->stream, dis_style_text, "\t");
229 for (; *oparg != '\0'; oparg++)
231 opargStart = oparg;
232 switch (*oparg)
234 case 'C': /* RVC */
235 switch (*++oparg)
237 case 's': /* RS1 x8-x15. */
238 case 'w': /* RS1 x8-x15. */
239 print (info->stream, dis_style_register, "%s",
240 riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
241 break;
242 case 't': /* RS2 x8-x15. */
243 case 'x': /* RS2 x8-x15. */
244 print (info->stream, dis_style_register, "%s",
245 riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
246 break;
247 case 'U': /* RS1, constrained to equal RD. */
248 print (info->stream, dis_style_register,
249 "%s", riscv_gpr_names[rd]);
250 break;
251 case 'c': /* RS1, constrained to equal sp. */
252 print (info->stream, dis_style_register, "%s",
253 riscv_gpr_names[X_SP]);
254 break;
255 case 'V': /* RS2 */
256 print (info->stream, dis_style_register, "%s",
257 riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
258 break;
259 case 'o':
260 case 'j':
261 if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0)
262 maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0);
263 if (info->mach == bfd_mach_riscv64
264 && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0)
265 maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1);
266 print (info->stream, dis_style_immediate, "%d",
267 (int)EXTRACT_CITYPE_IMM (l));
268 break;
269 case 'k':
270 print (info->stream, dis_style_address_offset, "%d",
271 (int)EXTRACT_CLTYPE_LW_IMM (l));
272 break;
273 case 'l':
274 print (info->stream, dis_style_address_offset, "%d",
275 (int)EXTRACT_CLTYPE_LD_IMM (l));
276 break;
277 case 'm':
278 print (info->stream, dis_style_address_offset, "%d",
279 (int)EXTRACT_CITYPE_LWSP_IMM (l));
280 break;
281 case 'n':
282 print (info->stream, dis_style_address_offset, "%d",
283 (int)EXTRACT_CITYPE_LDSP_IMM (l));
284 break;
285 case 'K':
286 print (info->stream, dis_style_immediate, "%d",
287 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
288 break;
289 case 'L':
290 print (info->stream, dis_style_immediate, "%d",
291 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
292 break;
293 case 'M':
294 print (info->stream, dis_style_address_offset, "%d",
295 (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
296 break;
297 case 'N':
298 print (info->stream, dis_style_address_offset, "%d",
299 (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
300 break;
301 case 'p':
302 info->target = EXTRACT_CBTYPE_IMM (l) + pc;
303 (*info->print_address_func) (info->target, info);
304 break;
305 case 'a':
306 info->target = EXTRACT_CJTYPE_IMM (l) + pc;
307 (*info->print_address_func) (info->target, info);
308 break;
309 case 'u':
310 print (info->stream, dis_style_immediate, "0x%x",
311 (unsigned)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
312 break;
313 case '>':
314 print (info->stream, dis_style_immediate, "0x%x",
315 (unsigned)EXTRACT_CITYPE_IMM (l) & 0x3f);
316 break;
317 case '<':
318 print (info->stream, dis_style_immediate, "0x%x",
319 (unsigned)EXTRACT_CITYPE_IMM (l) & 0x1f);
320 break;
321 case 'T': /* Floating-point RS2. */
322 print (info->stream, dis_style_register, "%s",
323 riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
324 break;
325 case 'D': /* Floating-point RS2 x8-x15. */
326 print (info->stream, dis_style_register, "%s",
327 riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
328 break;
330 break;
332 case 'V': /* RVV */
333 switch (*++oparg)
335 case 'd':
336 case 'f':
337 print (info->stream, dis_style_register, "%s",
338 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
339 break;
340 case 'e':
341 if (!EXTRACT_OPERAND (VWD, l))
342 print (info->stream, dis_style_register, "%s",
343 riscv_gpr_names[0]);
344 else
345 print (info->stream, dis_style_register, "%s",
346 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
347 break;
348 case 's':
349 print (info->stream, dis_style_register, "%s",
350 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS1, l)]);
351 break;
352 case 't':
353 case 'u': /* VS1 == VS2 already verified at this point. */
354 case 'v': /* VD == VS1 == VS2 already verified at this point. */
355 print (info->stream, dis_style_register, "%s",
356 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS2, l)]);
357 break;
358 case '0':
359 print (info->stream, dis_style_register, "%s",
360 riscv_vecr_names_numeric[0]);
361 break;
362 case 'b':
363 case 'c':
365 int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l)
366 : EXTRACT_RVV_VC_IMM (l);
367 unsigned int imm_vlmul = EXTRACT_OPERAND (VLMUL, imm);
368 unsigned int imm_vsew = EXTRACT_OPERAND (VSEW, imm);
369 unsigned int imm_vta = EXTRACT_OPERAND (VTA, imm);
370 unsigned int imm_vma = EXTRACT_OPERAND (VMA, imm);
371 unsigned int imm_vtype_res = (imm >> 8);
373 if (imm_vsew < ARRAY_SIZE (riscv_vsew)
374 && imm_vlmul < ARRAY_SIZE (riscv_vlmul)
375 && imm_vta < ARRAY_SIZE (riscv_vta)
376 && imm_vma < ARRAY_SIZE (riscv_vma)
377 && !imm_vtype_res
378 && riscv_vsew[imm_vsew] != NULL
379 && riscv_vlmul[imm_vlmul] != NULL)
380 print (info->stream, dis_style_text, "%s,%s,%s,%s",
381 riscv_vsew[imm_vsew],
382 riscv_vlmul[imm_vlmul], riscv_vta[imm_vta],
383 riscv_vma[imm_vma]);
384 else
385 print (info->stream, dis_style_immediate, "%d", imm);
387 break;
388 case 'i':
389 print (info->stream, dis_style_immediate, "%d",
390 (int)EXTRACT_RVV_VI_IMM (l));
391 break;
392 case 'j':
393 print (info->stream, dis_style_immediate, "%d",
394 (int)EXTRACT_RVV_VI_UIMM (l));
395 break;
396 case 'k':
397 print (info->stream, dis_style_immediate, "%d",
398 (int)EXTRACT_RVV_OFFSET (l));
399 break;
400 case 'm':
401 if (!EXTRACT_OPERAND (VMASK, l))
403 print (info->stream, dis_style_text, ",");
404 print (info->stream, dis_style_register, "%s",
405 riscv_vecm_names_numeric[0]);
407 break;
409 break;
411 case ',':
412 case '(':
413 case ')':
414 case '[':
415 case ']':
416 print (info->stream, dis_style_text, "%c", *oparg);
417 break;
419 case '0':
420 /* Only print constant 0 if it is the last argument. */
421 if (!oparg[1])
422 print (info->stream, dis_style_immediate, "0");
423 break;
425 case 's':
426 if ((l & MASK_JALR) == MATCH_JALR)
427 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
428 print (info->stream, dis_style_register, "%s", riscv_gpr_names[rs1]);
429 break;
431 case 't':
432 print (info->stream, dis_style_register, "%s",
433 riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
434 break;
436 case 'u':
437 print (info->stream, dis_style_immediate, "0x%x",
438 (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
439 break;
441 case 'm':
442 arg_print (info, EXTRACT_OPERAND (RM, l),
443 riscv_rm, ARRAY_SIZE (riscv_rm));
444 break;
446 case 'P':
447 arg_print (info, EXTRACT_OPERAND (PRED, l),
448 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
449 break;
451 case 'Q':
452 arg_print (info, EXTRACT_OPERAND (SUCC, l),
453 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
454 break;
456 case 'o':
457 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
458 /* Fall through. */
459 case 'j':
460 if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
461 || (l & MASK_JALR) == MATCH_JALR)
462 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
463 if (info->mach == bfd_mach_riscv64
464 && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0)
465 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1);
466 print (info->stream, dis_style_immediate, "%d",
467 (int)EXTRACT_ITYPE_IMM (l));
468 break;
470 case 'q':
471 maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0);
472 print (info->stream, dis_style_address_offset, "%d",
473 (int)EXTRACT_STYPE_IMM (l));
474 break;
476 case 'f':
477 print (info->stream, dis_style_address_offset, "%d",
478 (int)EXTRACT_STYPE_IMM (l));
479 break;
481 case 'a':
482 info->target = EXTRACT_JTYPE_IMM (l) + pc;
483 (*info->print_address_func) (info->target, info);
484 break;
486 case 'p':
487 info->target = EXTRACT_BTYPE_IMM (l) + pc;
488 (*info->print_address_func) (info->target, info);
489 break;
491 case 'd':
492 if ((l & MASK_AUIPC) == MATCH_AUIPC)
493 pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
494 else if ((l & MASK_LUI) == MATCH_LUI)
495 pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
496 else if ((l & MASK_C_LUI) == MATCH_C_LUI)
497 pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
498 print (info->stream, dis_style_register, "%s", riscv_gpr_names[rd]);
499 break;
501 case 'y':
502 print (info->stream, dis_style_immediate, "0x%x",
503 (unsigned)EXTRACT_OPERAND (BS, l));
504 break;
506 case 'z':
507 print (info->stream, dis_style_register, "%s", riscv_gpr_names[0]);
508 break;
510 case '>':
511 print (info->stream, dis_style_immediate, "0x%x",
512 (unsigned)EXTRACT_OPERAND (SHAMT, l));
513 break;
515 case '<':
516 print (info->stream, dis_style_immediate, "0x%x",
517 (unsigned)EXTRACT_OPERAND (SHAMTW, l));
518 break;
520 case 'S':
521 case 'U':
522 print (info->stream, dis_style_register, "%s", riscv_fpr_names[rs1]);
523 break;
525 case 'T':
526 print (info->stream, dis_style_register, "%s",
527 riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
528 break;
530 case 'D':
531 print (info->stream, dis_style_register, "%s", riscv_fpr_names[rd]);
532 break;
534 case 'R':
535 print (info->stream, dis_style_register, "%s",
536 riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
537 break;
539 case 'E':
541 static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */
542 static bool init_csr = false;
543 unsigned int csr = EXTRACT_OPERAND (CSR, l);
545 if (!init_csr)
547 unsigned int i;
548 for (i = 0; i < 4096; i++)
549 riscv_csr_hash[i] = NULL;
551 /* Set to the newest privileged version. */
552 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
553 default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1;
555 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
556 if (riscv_csr_hash[num] == NULL \
557 && ((define_version == PRIV_SPEC_CLASS_NONE \
558 && abort_version == PRIV_SPEC_CLASS_NONE) \
559 || (default_priv_spec >= define_version \
560 && default_priv_spec < abort_version))) \
561 riscv_csr_hash[num] = #name;
562 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
563 DECLARE_CSR (name, num, class, define_version, abort_version)
564 #include "opcode/riscv-opc.h"
565 #undef DECLARE_CSR
568 if (riscv_csr_hash[csr] != NULL)
569 print (info->stream, dis_style_register, "%s",
570 riscv_csr_hash[csr]);
571 else
572 print (info->stream, dis_style_immediate, "0x%x", csr);
573 break;
576 case 'Y':
577 print (info->stream, dis_style_immediate, "0x%x",
578 (unsigned) EXTRACT_OPERAND (RNUM, l));
579 break;
581 case 'Z':
582 print (info->stream, dis_style_immediate, "%d", rs1);
583 break;
585 case 'X': /* Integer immediate. */
587 size_t n;
588 size_t s;
589 bool sign;
591 switch (*++oparg)
593 case 'l': /* Literal. */
594 oparg++;
595 while (*oparg && *oparg != ',')
597 print (info->stream, dis_style_immediate, "%c", *oparg);
598 oparg++;
600 oparg--;
601 break;
602 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
603 sign = true;
604 goto print_imm;
605 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
606 sign = false;
607 goto print_imm;
608 print_imm:
609 n = strtol (oparg + 1, (char **)&oparg, 10);
610 if (*oparg != '@')
611 goto undefined_modifier;
612 s = strtol (oparg + 1, (char **)&oparg, 10);
613 oparg--;
615 if (!sign)
616 print (info->stream, dis_style_immediate, "%lu",
617 (unsigned long)EXTRACT_U_IMM (n, s, l));
618 else
619 print (info->stream, dis_style_immediate, "%li",
620 (signed long)EXTRACT_S_IMM (n, s, l));
621 break;
622 default:
623 goto undefined_modifier;
626 break;
627 default:
628 undefined_modifier:
629 /* xgettext:c-format */
630 print (info->stream, dis_style_text,
631 _("# internal error, undefined modifier (%c)"),
632 *opargStart);
633 return;
638 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
639 on using INFO. Returns length of the instruction, in bytes.
640 BIGENDIAN must be 1 if this is big-endian code, 0 if
641 this is little-endian code. */
643 static int
644 riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
646 const struct riscv_opcode *op;
647 static bool init = false;
648 static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
649 struct riscv_private_data *pd;
650 int insnlen;
652 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
654 /* Build a hash table to shorten the search time. */
655 if (! init)
657 for (op = riscv_opcodes; op->name; op++)
658 if (!riscv_hash[OP_HASH_IDX (op->match)])
659 riscv_hash[OP_HASH_IDX (op->match)] = op;
661 init = true;
664 if (info->private_data == NULL)
666 int i;
668 pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
669 pd->gp = 0;
670 pd->print_addr = 0;
671 for (i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++)
672 pd->hi_addr[i] = -1;
673 pd->to_print_addr = false;
674 pd->has_gp = false;
676 for (i = 0; i < info->symtab_size; i++)
677 if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0)
679 pd->gp = bfd_asymbol_value (info->symtab[i]);
680 pd->has_gp = true;
683 else
684 pd = info->private_data;
686 insnlen = riscv_insn_length (word);
688 /* RISC-V instructions are always little-endian. */
689 info->endian_code = BFD_ENDIAN_LITTLE;
691 info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
692 info->bytes_per_line = 8;
693 /* We don't support constant pools, so this must be code. */
694 info->display_endian = info->endian_code;
695 info->insn_info_valid = 1;
696 info->branch_delay_insns = 0;
697 info->data_size = 0;
698 info->insn_type = dis_nonbranch;
699 info->target = 0;
700 info->target2 = 0;
702 op = riscv_hash[OP_HASH_IDX (word)];
703 if (op != NULL)
705 /* If XLEN is not known, get its value from the ELF class. */
706 if (info->mach == bfd_mach_riscv64)
707 xlen = 64;
708 else if (info->mach == bfd_mach_riscv32)
709 xlen = 32;
710 else if (info->section != NULL)
712 Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
713 xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
716 /* If arch has the Zfinx extension, replace FPR with GPR. */
717 if (riscv_subset_supports (&riscv_rps_dis, "zfinx"))
718 riscv_fpr_names = riscv_gpr_names;
719 else
720 riscv_fpr_names = riscv_gpr_names == riscv_gpr_names_abi ?
721 riscv_fpr_names_abi : riscv_fpr_names_numeric;
723 for (; op->name; op++)
725 /* Does the opcode match? */
726 if (! (op->match_func) (op, word))
727 continue;
728 /* Is this a pseudo-instruction and may we print it as such? */
729 if (no_aliases && (op->pinfo & INSN_ALIAS))
730 continue;
731 /* Is this instruction restricted to a certain value of XLEN? */
732 if ((op->xlen_requirement != 0) && (op->xlen_requirement != xlen))
733 continue;
734 /* Is this instruction supported by the current architecture? */
735 if (!riscv_multi_subset_supports (&riscv_rps_dis, op->insn_class))
736 continue;
738 /* It's a match. */
739 (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
740 "%s", op->name);
741 print_insn_args (op->args, word, memaddr, info);
743 /* Try to disassemble multi-instruction addressing sequences. */
744 if (pd->to_print_addr)
746 info->target = pd->print_addr;
747 (*info->fprintf_styled_func)
748 (info->stream, dis_style_comment_start, " # ");
749 (*info->print_address_func) (info->target, info);
750 pd->to_print_addr = false;
753 /* Finish filling out insn_info fields. */
754 switch (op->pinfo & INSN_TYPE)
756 case INSN_BRANCH:
757 info->insn_type = dis_branch;
758 break;
759 case INSN_CONDBRANCH:
760 info->insn_type = dis_condbranch;
761 break;
762 case INSN_JSR:
763 info->insn_type = dis_jsr;
764 break;
765 case INSN_DREF:
766 info->insn_type = dis_dref;
767 break;
768 default:
769 break;
772 if (op->pinfo & INSN_DATA_SIZE)
774 int size = ((op->pinfo & INSN_DATA_SIZE)
775 >> INSN_DATA_SIZE_SHIFT);
776 info->data_size = 1 << (size - 1);
779 return insnlen;
783 /* We did not find a match, so just print the instruction bits. */
784 info->insn_type = dis_noninsn;
785 switch (insnlen)
787 case 2:
788 case 4:
789 case 8:
790 (*info->fprintf_styled_func)
791 (info->stream, dis_style_assembler_directive, ".%dbyte", insnlen);
792 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
793 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
794 "0x%llx", (unsigned long long) word);
795 break;
796 default:
798 int i;
799 (*info->fprintf_styled_func)
800 (info->stream, dis_style_assembler_directive, ".byte");
801 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
802 for (i = 0; i < insnlen; ++i)
804 if (i > 0)
805 (*info->fprintf_styled_func) (info->stream, dis_style_text,
806 ", ");
807 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
808 "0x%02x",
809 (unsigned int) (word & 0xff));
810 word >>= 8;
813 break;
815 return insnlen;
818 /* Return true if we find the suitable mapping symbol,
819 and also update the STATE. Otherwise, return false. */
821 static bool
822 riscv_get_map_state (int n,
823 enum riscv_seg_mstate *state,
824 struct disassemble_info *info)
826 const char *name;
828 /* If the symbol is in a different section, ignore it. */
829 if (info->section != NULL
830 && info->section != info->symtab[n]->section)
831 return false;
833 name = bfd_asymbol_name(info->symtab[n]);
834 if (strcmp (name, "$x") == 0)
835 *state = MAP_INSN;
836 else if (strcmp (name, "$d") == 0)
837 *state = MAP_DATA;
838 else if (strncmp (name, "$xrv", 4) == 0)
840 *state = MAP_INSN;
841 riscv_release_subset_list (&riscv_subsets);
842 riscv_parse_subset (&riscv_rps_dis, name + 2);
844 else
845 return false;
847 return true;
850 /* Check the sorted symbol table (sorted by the symbol value), find the
851 suitable mapping symbols. */
853 static enum riscv_seg_mstate
854 riscv_search_mapping_symbol (bfd_vma memaddr,
855 struct disassemble_info *info)
857 enum riscv_seg_mstate mstate;
858 bool from_last_map_symbol;
859 bool found = false;
860 int symbol = -1;
861 int n;
863 /* Decide whether to print the data or instruction by default, in case
864 we can not find the corresponding mapping symbols. */
865 mstate = MAP_DATA;
866 if ((info->section
867 && info->section->flags & SEC_CODE)
868 || !info->section)
869 mstate = MAP_INSN;
871 if (info->symtab_size == 0
872 || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
873 return mstate;
875 /* Reset the last_map_symbol if we start to dump a new section. */
876 if (memaddr <= 0)
877 last_map_symbol = -1;
879 /* If the last stop offset is different from the current one, then
880 don't use the last_map_symbol to search. We usually reset the
881 info->stop_offset when handling a new section. */
882 from_last_map_symbol = (last_map_symbol >= 0
883 && info->stop_offset == last_stop_offset);
885 /* Start scanning at the start of the function, or wherever
886 we finished last time. */
887 n = info->symtab_pos + 1;
888 if (from_last_map_symbol && n >= last_map_symbol)
889 n = last_map_symbol;
891 /* Find the suitable mapping symbol to dump. */
892 for (; n < info->symtab_size; n++)
894 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
895 /* We have searched all possible symbols in the range. */
896 if (addr > memaddr)
897 break;
898 if (riscv_get_map_state (n, &mstate, info))
900 symbol = n;
901 found = true;
902 /* Do not stop searching, in case there are some mapping
903 symbols have the same value, but have different names.
904 Use the last one. */
908 /* We can not find the suitable mapping symbol above. Therefore, we
909 look forwards and try to find it again, but don't go pass the start
910 of the section. Otherwise a data section without mapping symbols
911 can pick up a text mapping symbol of a preceeding section. */
912 if (!found)
914 n = info->symtab_pos;
915 if (from_last_map_symbol && n >= last_map_symbol)
916 n = last_map_symbol;
918 for (; n >= 0; n--)
920 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
921 /* We have searched all possible symbols in the range. */
922 if (addr < (info->section ? info->section->vma : 0))
923 break;
924 /* Stop searching once we find the closed mapping symbol. */
925 if (riscv_get_map_state (n, &mstate, info))
927 symbol = n;
928 found = true;
929 break;
934 /* Save the information for next use. */
935 last_map_symbol = symbol;
936 last_stop_offset = info->stop_offset;
938 return mstate;
941 /* Decide which data size we should print. */
943 static bfd_vma
944 riscv_data_length (bfd_vma memaddr,
945 disassemble_info *info)
947 bfd_vma length;
948 bool found = false;
950 length = 4;
951 if (info->symtab_size != 0
952 && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
953 && last_map_symbol >= 0)
955 int n;
956 enum riscv_seg_mstate m = MAP_NONE;
957 for (n = last_map_symbol + 1; n < info->symtab_size; n++)
959 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
960 if (addr > memaddr
961 && riscv_get_map_state (n, &m, info))
963 if (addr - memaddr < length)
964 length = addr - memaddr;
965 found = true;
966 break;
970 if (!found)
972 /* Do not set the length which exceeds the section size. */
973 bfd_vma offset = info->section->vma + info->section->size;
974 offset -= memaddr;
975 length = (offset < length) ? offset : length;
977 length = length == 3 ? 2 : length;
978 return length;
981 /* Dump the data contents. */
983 static int
984 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
985 insn_t data,
986 disassemble_info *info)
988 info->display_endian = info->endian;
990 switch (info->bytes_per_chunk)
992 case 1:
993 info->bytes_per_line = 6;
994 (*info->fprintf_styled_func)
995 (info->stream, dis_style_assembler_directive, ".byte");
996 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
997 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
998 "0x%02x", (unsigned)data);
999 break;
1000 case 2:
1001 info->bytes_per_line = 8;
1002 (*info->fprintf_styled_func)
1003 (info->stream, dis_style_assembler_directive, ".short");
1004 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1005 (*info->fprintf_styled_func)
1006 (info->stream, dis_style_immediate, "0x%04x", (unsigned) data);
1007 break;
1008 case 4:
1009 info->bytes_per_line = 8;
1010 (*info->fprintf_styled_func)
1011 (info->stream, dis_style_assembler_directive, ".word");
1012 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1013 (*info->fprintf_styled_func)
1014 (info->stream, dis_style_immediate, "0x%08lx",
1015 (unsigned long) data);
1016 break;
1017 case 8:
1018 info->bytes_per_line = 8;
1019 (*info->fprintf_styled_func)
1020 (info->stream, dis_style_assembler_directive, ".dword");
1021 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1022 (*info->fprintf_styled_func)
1023 (info->stream, dis_style_immediate, "0x%016llx",
1024 (unsigned long long) data);
1025 break;
1026 default:
1027 abort ();
1029 return info->bytes_per_chunk;
1033 print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
1035 bfd_byte packet[RISCV_MAX_INSN_LEN];
1036 insn_t insn = 0;
1037 bfd_vma dump_size;
1038 int status;
1039 enum riscv_seg_mstate mstate;
1040 int (*riscv_disassembler) (bfd_vma, insn_t, struct disassemble_info *);
1042 if (info->disassembler_options != NULL)
1044 parse_riscv_dis_options (info->disassembler_options);
1045 /* Avoid repeatedly parsing the options. */
1046 info->disassembler_options = NULL;
1048 else if (riscv_gpr_names == NULL)
1049 set_default_riscv_dis_options ();
1051 mstate = riscv_search_mapping_symbol (memaddr, info);
1053 /* Set the size to dump. */
1054 if (mstate == MAP_DATA
1055 && (info->flags & DISASSEMBLE_DATA) == 0)
1057 dump_size = riscv_data_length (memaddr, info);
1058 info->bytes_per_chunk = dump_size;
1059 riscv_disassembler = riscv_disassemble_data;
1061 else
1063 /* Get the first 2-bytes to check the lenghth of instruction. */
1064 status = (*info->read_memory_func) (memaddr, packet, 2, info);
1065 if (status != 0)
1067 (*info->memory_error_func) (status, memaddr, info);
1068 return status;
1070 insn = (insn_t) bfd_getl16 (packet);
1071 dump_size = riscv_insn_length (insn);
1072 riscv_disassembler = riscv_disassemble_insn;
1075 /* Fetch the instruction to dump. */
1076 status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
1077 if (status != 0)
1079 (*info->memory_error_func) (status, memaddr, info);
1080 return status;
1082 insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
1084 return (*riscv_disassembler) (memaddr, insn, info);
1087 disassembler_ftype
1088 riscv_get_disassembler (bfd *abfd)
1090 const char *default_arch = "rv64gc";
1092 if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1094 const char *sec_name = get_elf_backend_data (abfd)->obj_attrs_section;
1095 if (bfd_get_section_by_name (abfd, sec_name) != NULL)
1097 obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
1098 unsigned int Tag_a = Tag_RISCV_priv_spec;
1099 unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
1100 unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
1101 riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i,
1102 attr[Tag_b].i,
1103 attr[Tag_c].i,
1104 &default_priv_spec);
1105 default_arch = attr[Tag_RISCV_arch].s;
1109 riscv_release_subset_list (&riscv_subsets);
1110 riscv_parse_subset (&riscv_rps_dis, default_arch);
1111 return print_insn_riscv;
1114 /* Prevent use of the fake labels that are generated as part of the DWARF
1115 and for relaxable relocations in the assembler. */
1117 bool
1118 riscv_symbol_is_valid (asymbol * sym,
1119 struct disassemble_info * info ATTRIBUTE_UNUSED)
1121 const char * name;
1123 if (sym == NULL)
1124 return false;
1126 name = bfd_asymbol_name (sym);
1128 return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
1129 && !riscv_elf_is_mapping_symbols (name));
1133 /* Indices into option argument vector for options accepting an argument.
1134 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1136 typedef enum
1138 RISCV_OPTION_ARG_NONE = -1,
1139 RISCV_OPTION_ARG_PRIV_SPEC,
1141 RISCV_OPTION_ARG_COUNT
1142 } riscv_option_arg_t;
1144 /* Valid RISCV disassembler options. */
1146 static struct
1148 const char *name;
1149 const char *description;
1150 riscv_option_arg_t arg;
1151 } riscv_options[] =
1153 { "numeric",
1154 N_("Print numeric register names, rather than ABI names."),
1155 RISCV_OPTION_ARG_NONE },
1156 { "no-aliases",
1157 N_("Disassemble only into canonical instructions."),
1158 RISCV_OPTION_ARG_NONE },
1159 { "priv-spec=",
1160 N_("Print the CSR according to the chosen privilege spec."),
1161 RISCV_OPTION_ARG_PRIV_SPEC }
1164 /* Build the structure representing valid RISCV disassembler options.
1165 This is done dynamically for maintenance ease purpose; a static
1166 initializer would be unreadable. */
1168 const disasm_options_and_args_t *
1169 disassembler_options_riscv (void)
1171 static disasm_options_and_args_t *opts_and_args;
1173 if (opts_and_args == NULL)
1175 size_t num_options = ARRAY_SIZE (riscv_options);
1176 size_t num_args = RISCV_OPTION_ARG_COUNT;
1177 disasm_option_arg_t *args;
1178 disasm_options_t *opts;
1179 size_t i, priv_spec_count;
1181 args = XNEWVEC (disasm_option_arg_t, num_args + 1);
1183 args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC";
1184 priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_CLASS_NONE - 1;
1185 args[RISCV_OPTION_ARG_PRIV_SPEC].values
1186 = XNEWVEC (const char *, priv_spec_count + 1);
1187 for (i = 0; i < priv_spec_count; i++)
1188 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i]
1189 = riscv_priv_specs[i].name;
1190 /* The array we return must be NULL terminated. */
1191 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL;
1193 /* The array we return must be NULL terminated. */
1194 args[num_args].name = NULL;
1195 args[num_args].values = NULL;
1197 opts_and_args = XNEW (disasm_options_and_args_t);
1198 opts_and_args->args = args;
1200 opts = &opts_and_args->options;
1201 opts->name = XNEWVEC (const char *, num_options + 1);
1202 opts->description = XNEWVEC (const char *, num_options + 1);
1203 opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1);
1204 for (i = 0; i < num_options; i++)
1206 opts->name[i] = riscv_options[i].name;
1207 opts->description[i] = _(riscv_options[i].description);
1208 if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE)
1209 opts->arg[i] = &args[riscv_options[i].arg];
1210 else
1211 opts->arg[i] = NULL;
1213 /* The array we return must be NULL terminated. */
1214 opts->name[i] = NULL;
1215 opts->description[i] = NULL;
1216 opts->arg[i] = NULL;
1219 return opts_and_args;
1222 void
1223 print_riscv_disassembler_options (FILE *stream)
1225 const disasm_options_and_args_t *opts_and_args;
1226 const disasm_option_arg_t *args;
1227 const disasm_options_t *opts;
1228 size_t max_len = 0;
1229 size_t i;
1230 size_t j;
1232 opts_and_args = disassembler_options_riscv ();
1233 opts = &opts_and_args->options;
1234 args = opts_and_args->args;
1236 fprintf (stream, _("\n\
1237 The following RISC-V specific disassembler options are supported for use\n\
1238 with the -M switch (multiple options should be separated by commas):\n"));
1239 fprintf (stream, "\n");
1241 /* Compute the length of the longest option name. */
1242 for (i = 0; opts->name[i] != NULL; i++)
1244 size_t len = strlen (opts->name[i]);
1246 if (opts->arg[i] != NULL)
1247 len += strlen (opts->arg[i]->name);
1248 if (max_len < len)
1249 max_len = len;
1252 for (i = 0, max_len++; opts->name[i] != NULL; i++)
1254 fprintf (stream, " %s", opts->name[i]);
1255 if (opts->arg[i] != NULL)
1256 fprintf (stream, "%s", opts->arg[i]->name);
1257 if (opts->description[i] != NULL)
1259 size_t len = strlen (opts->name[i]);
1261 if (opts->arg != NULL && opts->arg[i] != NULL)
1262 len += strlen (opts->arg[i]->name);
1263 fprintf (stream, "%*c %s", (int) (max_len - len), ' ',
1264 opts->description[i]);
1266 fprintf (stream, "\n");
1269 for (i = 0; args[i].name != NULL; i++)
1271 if (args[i].values == NULL)
1272 continue;
1273 fprintf (stream, _("\n\
1274 For the options above, the following values are supported for \"%s\":\n "),
1275 args[i].name);
1276 for (j = 0; args[i].values[j] != NULL; j++)
1277 fprintf (stream, " %s", args[i].values[j]);
1278 fprintf (stream, _("\n"));
1281 fprintf (stream, _("\n"));