Fix calculation of space remaining in buffer when printing the contents of a DST__K_R...
[binutils-gdb.git] / opcodes / riscv-dis.c
blob9c5e6ce1e690296e0278b381ced2d884f22275d9
1 /* RISC-V disassembler
2 Copyright (C) 2011-2024 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 /* The RISC-V disassembler produces styled output using
36 disassemble_info::fprintf_styled_func. This define prevents use of
37 disassemble_info::fprintf_func which is for unstyled output. */
38 #define fprintf_func please_use_fprintf_styled_func_instead
40 /* Current XLEN for the disassembler. */
41 static unsigned xlen = 0;
43 /* Default ISA specification version (constant as of now). */
44 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
46 /* Default privileged specification
47 (as specified by the ELF attributes or the `priv-spec' option). */
48 static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
50 static riscv_subset_list_t riscv_subsets;
51 static riscv_parse_subset_t riscv_rps_dis =
53 &riscv_subsets, /* subset_list. */
54 opcodes_error_handler,/* error_handler. */
55 &xlen, /* xlen. */
56 &default_isa_spec, /* isa_spec. */
57 false, /* check_unknown_prefixed_ext. */
60 struct riscv_private_data
62 bfd_vma gp;
63 bfd_vma print_addr;
64 bfd_vma hi_addr[OP_MASK_RD + 1];
65 bool to_print_addr;
66 bool has_gp;
69 /* Used for mapping symbols. */
70 static int last_map_symbol = -1;
71 static bfd_vma last_stop_offset = 0;
72 static bfd_vma last_map_symbol_boundary = 0;
73 static enum riscv_seg_mstate last_map_state = MAP_NONE;
74 static asection *last_map_section = NULL;
76 /* Register names as used by the disassembler. */
77 static const char (*riscv_gpr_names)[NRC];
78 static const char (*riscv_fpr_names)[NRC];
80 /* If set, disassemble as most general instruction. */
81 static bool no_aliases = false;
84 /* Set default RISC-V disassembler options. */
86 static void
87 set_default_riscv_dis_options (void)
89 riscv_gpr_names = riscv_gpr_names_abi;
90 riscv_fpr_names = riscv_fpr_names_abi;
91 no_aliases = false;
94 /* Parse RISC-V disassembler option (without arguments). */
96 static bool
97 parse_riscv_dis_option_without_args (const char *option)
99 if (strcmp (option, "no-aliases") == 0)
100 no_aliases = true;
101 else if (strcmp (option, "numeric") == 0)
103 riscv_gpr_names = riscv_gpr_names_numeric;
104 riscv_fpr_names = riscv_fpr_names_numeric;
106 else
107 return false;
108 return true;
111 /* Parse RISC-V disassembler option (possibly with arguments). */
113 static void
114 parse_riscv_dis_option (const char *option)
116 char *equal, *value;
118 if (parse_riscv_dis_option_without_args (option))
119 return;
121 equal = strchr (option, '=');
122 if (equal == NULL)
124 /* The option without '=' should be defined above. */
125 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
126 return;
128 if (equal == option
129 || *(equal + 1) == '\0')
131 /* Invalid options with '=', no option name before '=',
132 and no value after '='. */
133 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
134 option);
135 return;
138 *equal = '\0';
139 value = equal + 1;
140 if (strcmp (option, "priv-spec") == 0)
142 enum riscv_spec_class priv_spec = PRIV_SPEC_CLASS_NONE;
143 const char *name = NULL;
145 RISCV_GET_PRIV_SPEC_CLASS (value, priv_spec);
146 if (priv_spec == PRIV_SPEC_CLASS_NONE)
147 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
148 option, value);
149 else if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
150 default_priv_spec = priv_spec;
151 else if (default_priv_spec != priv_spec)
153 RISCV_GET_PRIV_SPEC_NAME (name, default_priv_spec);
154 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
155 "the elf privilege attribute is %s"),
156 option, value, name);
159 else
161 /* xgettext:c-format */
162 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
166 /* Parse RISC-V disassembler options. */
168 static void
169 parse_riscv_dis_options (const char *opts_in)
171 char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
173 set_default_riscv_dis_options ();
175 for ( ; opt_end != NULL; opt = opt_end + 1)
177 if ((opt_end = strchr (opt, ',')) != NULL)
178 *opt_end = 0;
179 parse_riscv_dis_option (opt);
182 free (opts);
185 /* Print one argument from an array. */
187 static void
188 arg_print (struct disassemble_info *info, unsigned long val,
189 const char* const* array, size_t size)
191 const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
192 (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s);
195 /* If we need to print an address, set its value and state. */
197 static void
198 maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset,
199 int wide)
201 if (pd->hi_addr[base_reg] != (bfd_vma)-1)
203 pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
204 pd->hi_addr[base_reg] = -1;
206 else if (base_reg == X_GP && pd->has_gp)
207 pd->print_addr = pd->gp + offset;
208 else if (base_reg == X_TP || base_reg == 0)
209 pd->print_addr = offset;
210 else
211 return; /* Don't print the address. */
212 pd->to_print_addr = true;
214 /* Sign-extend a 32-bit value to a 64-bit value. */
215 if (wide)
216 pd->print_addr = (bfd_vma)(int32_t) pd->print_addr;
218 /* Fit into a 32-bit value on RV32. */
219 if (xlen == 32)
220 pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr;
223 /* Get Zcmp reg_list field. */
225 static void
226 print_reg_list (disassemble_info *info, insn_t l)
228 bool numeric = riscv_gpr_names == riscv_gpr_names_numeric;
229 unsigned reg_list = (int)EXTRACT_OPERAND (REG_LIST, l);
230 unsigned r_start = numeric ? X_S2 : X_S0;
231 info->fprintf_styled_func (info->stream, dis_style_register,
232 "%s", riscv_gpr_names[X_RA]);
234 if (reg_list == 5)
236 info->fprintf_styled_func (info->stream, dis_style_text, ",");
237 info->fprintf_styled_func (info->stream, dis_style_register,
238 "%s", riscv_gpr_names[X_S0]);
240 else if (reg_list == 6 || (numeric && reg_list > 6))
242 info->fprintf_styled_func (info->stream, dis_style_text, ",");
243 info->fprintf_styled_func (info->stream, dis_style_register,
244 "%s", riscv_gpr_names[X_S0]);
245 info->fprintf_styled_func (info->stream, dis_style_text, "-");
246 info->fprintf_styled_func (info->stream, dis_style_register,
247 "%s", riscv_gpr_names[X_S1]);
250 if (reg_list == 15)
252 info->fprintf_styled_func (info->stream, dis_style_text, ",");
253 info->fprintf_styled_func (info->stream, dis_style_register,
254 "%s", riscv_gpr_names[r_start]);
255 info->fprintf_styled_func (info->stream, dis_style_text, "-");
256 info->fprintf_styled_func (info->stream, dis_style_register,
257 "%s", riscv_gpr_names[X_S11]);
259 else if (reg_list == 7 && numeric)
261 info->fprintf_styled_func (info->stream, dis_style_text, ",");
262 info->fprintf_styled_func (info->stream, dis_style_register,
263 "%s", riscv_gpr_names[X_S2]);
265 else if (reg_list > 6)
267 info->fprintf_styled_func (info->stream, dis_style_text, ",");
268 info->fprintf_styled_func (info->stream, dis_style_register,
269 "%s", riscv_gpr_names[r_start]);
270 info->fprintf_styled_func (info->stream, dis_style_text, "-");
271 info->fprintf_styled_func (info->stream, dis_style_register,
272 "%s", riscv_gpr_names[reg_list + 11]);
276 /* Get Zcmp sp adjustment immediate. */
278 static int
279 riscv_get_spimm (insn_t l)
281 int spimm = riscv_get_sp_base(l, *riscv_rps_dis.xlen);
282 spimm += EXTRACT_ZCMP_SPIMM (l);
283 if (((l ^ MATCH_CM_PUSH) & MASK_CM_PUSH) == 0)
284 spimm *= -1;
285 return spimm;
288 /* Print insn arguments for 32/64-bit code. */
290 static void
291 print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info)
293 struct riscv_private_data *pd = info->private_data;
294 int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
295 int rd = (l >> OP_SH_RD) & OP_MASK_RD;
296 fprintf_styled_ftype print = info->fprintf_styled_func;
297 const char *opargStart;
299 if (*oparg != '\0')
300 print (info->stream, dis_style_text, "\t");
302 for (; *oparg != '\0'; oparg++)
304 opargStart = oparg;
305 switch (*oparg)
307 case 'C': /* RVC */
308 switch (*++oparg)
310 case 's': /* RS1 x8-x15. */
311 case 'w': /* RS1 x8-x15. */
312 print (info->stream, dis_style_register, "%s",
313 riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
314 break;
315 case 't': /* RS2 x8-x15. */
316 case 'x': /* RS2 x8-x15. */
317 print (info->stream, dis_style_register, "%s",
318 riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
319 break;
320 case 'U': /* RS1, constrained to equal RD. */
321 print (info->stream, dis_style_register,
322 "%s", riscv_gpr_names[rd]);
323 break;
324 case 'c': /* RS1, constrained to equal sp. */
325 print (info->stream, dis_style_register, "%s",
326 riscv_gpr_names[X_SP]);
327 break;
328 case 'V': /* RS2 */
329 print (info->stream, dis_style_register, "%s",
330 riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
331 break;
332 case 'o':
333 case 'j':
334 if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0)
335 maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0);
336 if (info->mach == bfd_mach_riscv64
337 && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0)
338 maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1);
339 print (info->stream, dis_style_immediate, "%d",
340 (int)EXTRACT_CITYPE_IMM (l));
341 break;
342 case 'k':
343 print (info->stream, dis_style_address_offset, "%d",
344 (int)EXTRACT_CLTYPE_LW_IMM (l));
345 break;
346 case 'l':
347 print (info->stream, dis_style_address_offset, "%d",
348 (int)EXTRACT_CLTYPE_LD_IMM (l));
349 break;
350 case 'm':
351 print (info->stream, dis_style_address_offset, "%d",
352 (int)EXTRACT_CITYPE_LWSP_IMM (l));
353 break;
354 case 'n':
355 print (info->stream, dis_style_address_offset, "%d",
356 (int)EXTRACT_CITYPE_LDSP_IMM (l));
357 break;
358 case 'K':
359 print (info->stream, dis_style_immediate, "%d",
360 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
361 break;
362 case 'L':
363 print (info->stream, dis_style_immediate, "%d",
364 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
365 break;
366 case 'M':
367 print (info->stream, dis_style_address_offset, "%d",
368 (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
369 break;
370 case 'N':
371 print (info->stream, dis_style_address_offset, "%d",
372 (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
373 break;
374 case 'p':
375 info->target = EXTRACT_CBTYPE_IMM (l) + pc;
376 (*info->print_address_func) (info->target, info);
377 break;
378 case 'a':
379 info->target = EXTRACT_CJTYPE_IMM (l) + pc;
380 (*info->print_address_func) (info->target, info);
381 break;
382 case 'u':
383 print (info->stream, dis_style_immediate, "0x%x",
384 (unsigned)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
385 break;
386 case '>':
387 print (info->stream, dis_style_immediate, "0x%x",
388 (unsigned)EXTRACT_CITYPE_IMM (l) & 0x3f);
389 break;
390 case '<':
391 print (info->stream, dis_style_immediate, "0x%x",
392 (unsigned)EXTRACT_CITYPE_IMM (l) & 0x1f);
393 break;
394 case 'T': /* Floating-point RS2. */
395 print (info->stream, dis_style_register, "%s",
396 riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
397 break;
398 case 'D': /* Floating-point RS2 x8-x15. */
399 print (info->stream, dis_style_register, "%s",
400 riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
401 break;
403 break;
405 case 'V': /* RVV */
406 switch (*++oparg)
408 case 'd':
409 case 'f':
410 print (info->stream, dis_style_register, "%s",
411 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
412 break;
413 case 'e':
414 if (!EXTRACT_OPERAND (VWD, l))
415 print (info->stream, dis_style_register, "%s",
416 riscv_gpr_names[0]);
417 else
418 print (info->stream, dis_style_register, "%s",
419 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
420 break;
421 case 's':
422 print (info->stream, dis_style_register, "%s",
423 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS1, l)]);
424 break;
425 case 't':
426 case 'u': /* VS1 == VS2 already verified at this point. */
427 case 'v': /* VD == VS1 == VS2 already verified at this point. */
428 print (info->stream, dis_style_register, "%s",
429 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS2, l)]);
430 break;
431 case '0':
432 print (info->stream, dis_style_register, "%s",
433 riscv_vecr_names_numeric[0]);
434 break;
435 case 'b':
436 case 'c':
438 int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l)
439 : EXTRACT_RVV_VC_IMM (l);
440 unsigned int imm_vlmul = EXTRACT_OPERAND (VLMUL, imm);
441 unsigned int imm_vsew = EXTRACT_OPERAND (VSEW, imm);
442 unsigned int imm_vta = EXTRACT_OPERAND (VTA, imm);
443 unsigned int imm_vma = EXTRACT_OPERAND (VMA, imm);
444 unsigned int imm_vtype_res = (imm >> 8);
446 if (imm_vsew < ARRAY_SIZE (riscv_vsew)
447 && imm_vlmul < ARRAY_SIZE (riscv_vlmul)
448 && imm_vta < ARRAY_SIZE (riscv_vta)
449 && imm_vma < ARRAY_SIZE (riscv_vma)
450 && !imm_vtype_res
451 && riscv_vsew[imm_vsew] != NULL
452 && riscv_vlmul[imm_vlmul] != NULL)
453 print (info->stream, dis_style_text, "%s,%s,%s,%s",
454 riscv_vsew[imm_vsew],
455 riscv_vlmul[imm_vlmul], riscv_vta[imm_vta],
456 riscv_vma[imm_vma]);
457 else
458 print (info->stream, dis_style_immediate, "%d", imm);
460 break;
461 case 'i':
462 print (info->stream, dis_style_immediate, "%d",
463 (int)EXTRACT_RVV_VI_IMM (l));
464 break;
465 case 'j':
466 print (info->stream, dis_style_immediate, "%d",
467 (int)EXTRACT_RVV_VI_UIMM (l));
468 break;
469 case 'k':
470 print (info->stream, dis_style_immediate, "%d",
471 (int)EXTRACT_RVV_OFFSET (l));
472 break;
473 case 'l':
474 print (info->stream, dis_style_immediate, "%d",
475 (int)EXTRACT_RVV_VI_UIMM6 (l));
476 break;
477 case 'm':
478 if (!EXTRACT_OPERAND (VMASK, l))
480 print (info->stream, dis_style_text, ",");
481 print (info->stream, dis_style_register, "%s",
482 riscv_vecm_names_numeric[0]);
484 break;
486 break;
488 case ',':
489 case '(':
490 case ')':
491 case '[':
492 case ']':
493 case '{':
494 case '}':
495 print (info->stream, dis_style_text, "%c", *oparg);
496 break;
498 case '0':
499 /* Only print constant 0 if it is the last argument. */
500 if (!oparg[1])
501 print (info->stream, dis_style_immediate, "0");
502 break;
504 case 's':
505 if ((l & MASK_JALR) == MATCH_JALR)
506 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
507 print (info->stream, dis_style_register, "%s", riscv_gpr_names[rs1]);
508 break;
510 case 't':
511 print (info->stream, dis_style_register, "%s",
512 riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
513 break;
515 case 'u':
516 print (info->stream, dis_style_immediate, "0x%x",
517 (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
518 break;
520 case 'm':
521 arg_print (info, EXTRACT_OPERAND (RM, l),
522 riscv_rm, ARRAY_SIZE (riscv_rm));
523 break;
525 case 'P':
526 arg_print (info, EXTRACT_OPERAND (PRED, l),
527 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
528 break;
530 case 'Q':
531 arg_print (info, EXTRACT_OPERAND (SUCC, l),
532 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
533 break;
535 case 'o':
536 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
537 /* Fall through. */
538 case 'j':
539 if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
540 || (l & MASK_JALR) == MATCH_JALR)
541 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
542 if (info->mach == bfd_mach_riscv64
543 && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0)
544 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1);
545 print (info->stream, dis_style_immediate, "%d",
546 (int)EXTRACT_ITYPE_IMM (l));
547 break;
549 case 'q':
550 maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0);
551 print (info->stream, dis_style_address_offset, "%d",
552 (int)EXTRACT_STYPE_IMM (l));
553 break;
555 case 'a':
556 info->target = EXTRACT_JTYPE_IMM (l) + pc;
557 (*info->print_address_func) (info->target, info);
558 break;
560 case 'p':
561 info->target = EXTRACT_BTYPE_IMM (l) + pc;
562 (*info->print_address_func) (info->target, info);
563 break;
565 case 'd':
566 if ((l & MASK_AUIPC) == MATCH_AUIPC)
567 pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
568 else if ((l & MASK_LUI) == MATCH_LUI)
569 pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
570 else if ((l & MASK_C_LUI) == MATCH_C_LUI)
571 pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
572 print (info->stream, dis_style_register, "%s", riscv_gpr_names[rd]);
573 break;
575 case 'y':
576 print (info->stream, dis_style_immediate, "0x%x",
577 EXTRACT_OPERAND (BS, l));
578 break;
580 case 'z':
581 print (info->stream, dis_style_register, "%s", riscv_gpr_names[0]);
582 break;
584 case '>':
585 print (info->stream, dis_style_immediate, "0x%x",
586 EXTRACT_OPERAND (SHAMT, l));
587 break;
589 case '<':
590 print (info->stream, dis_style_immediate, "0x%x",
591 EXTRACT_OPERAND (SHAMTW, l));
592 break;
594 case 'S':
595 case 'U':
596 print (info->stream, dis_style_register, "%s", riscv_fpr_names[rs1]);
597 break;
599 case 'T':
600 print (info->stream, dis_style_register, "%s",
601 riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
602 break;
604 case 'D':
605 print (info->stream, dis_style_register, "%s", riscv_fpr_names[rd]);
606 break;
608 case 'R':
609 print (info->stream, dis_style_register, "%s",
610 riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
611 break;
613 case 'E':
615 static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */
616 static bool init_csr = false;
617 unsigned int csr = EXTRACT_OPERAND (CSR, l);
619 if (!init_csr)
621 unsigned int i;
622 for (i = 0; i < 4096; i++)
623 riscv_csr_hash[i] = NULL;
625 /* Set to the newest privileged version. */
626 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
627 default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1;
629 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
630 if (riscv_csr_hash[num] == NULL \
631 && ((define_version == PRIV_SPEC_CLASS_NONE \
632 && abort_version == PRIV_SPEC_CLASS_NONE) \
633 || (default_priv_spec >= define_version \
634 && default_priv_spec < abort_version))) \
635 riscv_csr_hash[num] = #name;
636 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
637 DECLARE_CSR (name, num, class, define_version, abort_version)
638 #include "opcode/riscv-opc.h"
639 #undef DECLARE_CSR
642 if (riscv_csr_hash[csr] != NULL)
643 if (riscv_subset_supports (&riscv_rps_dis, "xtheadvector")
644 && (csr == CSR_VSTART
645 || csr == CSR_VXSAT
646 || csr == CSR_VXRM
647 || csr == CSR_VL
648 || csr == CSR_VTYPE
649 || csr == CSR_VLENB))
650 print (info->stream, dis_style_register, "%s",
651 concat ("th.", riscv_csr_hash[csr], NULL));
652 else
653 print (info->stream, dis_style_register, "%s",
654 riscv_csr_hash[csr]);
655 else
656 print (info->stream, dis_style_immediate, "0x%x", csr);
657 break;
660 case 'Y':
661 print (info->stream, dis_style_immediate, "0x%x",
662 EXTRACT_OPERAND (RNUM, l));
663 break;
665 case 'Z':
666 print (info->stream, dis_style_immediate, "%d", rs1);
667 break;
669 case 'W': /* Various operands for standard z extensions. */
670 switch (*++oparg)
672 case 'i':
673 switch (*++oparg)
675 case 'f':
676 print (info->stream, dis_style_address_offset, "%d",
677 (int) EXTRACT_STYPE_IMM (l));
678 break;
679 default:
680 goto undefined_modifier;
682 break;
683 case 'f':
684 switch (*++oparg)
686 case 'v':
687 if (riscv_fli_symval[rs1])
688 print (info->stream, dis_style_text, "%s",
689 riscv_fli_symval[rs1]);
690 else
691 print (info->stream, dis_style_immediate, "%a",
692 riscv_fli_numval[rs1]);
693 break;
694 default:
695 goto undefined_modifier;
697 break;
698 case 'c': /* Zcb extension 16 bits length instruction fields. */
699 switch (*++oparg)
701 case 'b':
702 print (info->stream, dis_style_immediate, "%d",
703 (int)EXTRACT_ZCB_BYTE_UIMM (l));
704 break;
705 case 'h':
706 print (info->stream, dis_style_immediate, "%d",
707 (int)EXTRACT_ZCB_HALFWORD_UIMM (l));
708 break;
709 case 'r':
710 print_reg_list (info, l);
711 break;
712 case 'p':
713 print (info->stream, dis_style_immediate, "%d",
714 riscv_get_spimm (l));
715 break;
716 default:
717 goto undefined_modifier;
719 break;
720 default:
721 goto undefined_modifier;
723 break;
725 case 'X': /* Vendor-specific operands. */
726 switch (*++oparg)
728 case 't': /* Vendor-specific (T-head) operands. */
730 size_t n;
731 size_t s;
732 bool sign;
733 switch (*++oparg)
735 case 'V':
736 ++oparg;
737 if (*oparg != 'c')
738 goto undefined_modifier;
740 int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l)
741 : EXTRACT_RVV_VC_IMM (l);
742 unsigned int imm_vediv = EXTRACT_OPERAND (XTHEADVEDIV, imm);
743 unsigned int imm_vlmul = EXTRACT_OPERAND (XTHEADVLMUL, imm);
744 unsigned int imm_vsew = EXTRACT_OPERAND (XTHEADVSEW, imm);
745 unsigned int imm_vtype_res
746 = EXTRACT_OPERAND (XTHEADVTYPE_RES, imm);
747 if (imm_vsew < ARRAY_SIZE (riscv_vsew)
748 && imm_vlmul < ARRAY_SIZE (riscv_th_vlen)
749 && imm_vediv < ARRAY_SIZE (riscv_th_vediv)
750 && ! imm_vtype_res)
751 print (info->stream, dis_style_text, "%s,%s,%s",
752 riscv_vsew[imm_vsew], riscv_th_vlen[imm_vlmul],
753 riscv_th_vediv[imm_vediv]);
754 else
755 print (info->stream, dis_style_immediate, "%d", imm);
756 break;
757 case 'l': /* Integer immediate, literal. */
758 oparg++;
759 while (*oparg && *oparg != ',')
761 print (info->stream, dis_style_immediate, "%c", *oparg);
762 oparg++;
764 oparg--;
765 break;
766 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
767 sign = true;
768 goto print_imm;
769 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
770 sign = false;
771 goto print_imm;
772 print_imm:
773 n = strtol (oparg + 1, (char **)&oparg, 10);
774 if (*oparg != '@')
775 goto undefined_modifier;
776 s = strtol (oparg + 1, (char **)&oparg, 10);
777 oparg--;
779 if (!sign)
780 print (info->stream, dis_style_immediate, "%lu",
781 (unsigned long)EXTRACT_U_IMM (n, s, l));
782 else
783 print (info->stream, dis_style_immediate, "%li",
784 (signed long)EXTRACT_S_IMM (n, s, l));
785 break;
786 default:
787 goto undefined_modifier;
790 break;
791 case 'c': /* Vendor-specific (CORE-V) operands. */
792 switch (*++oparg)
794 case '2':
795 print (info->stream, dis_style_immediate, "%d",
796 ((int) EXTRACT_CV_IS2_UIMM5 (l)));
797 break;
798 case '3':
799 print (info->stream, dis_style_immediate, "%d",
800 ((int) EXTRACT_CV_IS3_UIMM5 (l)));
801 break;
802 case '4':
803 print (info->stream, dis_style_immediate, "%d",
804 ((int) EXTRACT_CV_BI_IMM5 (l)));
805 break;
806 default:
807 goto undefined_modifier;
809 break;
810 case 's': /* Vendor-specific (SiFive) operands. */
811 switch (*++oparg)
813 /* SiFive vector coprocessor interface. */
814 case 'd':
815 print (info->stream, dis_style_register, "0x%x",
816 (unsigned) EXTRACT_OPERAND (RD, l));
817 break;
818 case 't':
819 print (info->stream, dis_style_register, "0x%x",
820 (unsigned) EXTRACT_OPERAND (RS2, l));
821 break;
822 case 'O':
823 switch (*++oparg)
825 case '2':
826 print (info->stream, dis_style_register, "0x%x",
827 (unsigned) EXTRACT_OPERAND (XSO2, l));
828 break;
829 case '1':
830 print (info->stream, dis_style_register, "0x%x",
831 (unsigned) EXTRACT_OPERAND (XSO1, l));
832 break;
834 break;
836 break;
837 default:
838 goto undefined_modifier;
840 break;
842 default:
843 undefined_modifier:
844 /* xgettext:c-format */
845 print (info->stream, dis_style_text,
846 _("# internal error, undefined modifier (%c)"),
847 *opargStart);
848 return;
853 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
854 on using INFO. Returns length of the instruction, in bytes.
855 BIGENDIAN must be 1 if this is big-endian code, 0 if
856 this is little-endian code. */
858 static int
859 riscv_disassemble_insn (bfd_vma memaddr,
860 insn_t word,
861 const bfd_byte *packet,
862 disassemble_info *info)
864 const struct riscv_opcode *op;
865 static bool init = false;
866 static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
867 struct riscv_private_data *pd = info->private_data;
868 int insnlen, i;
869 bool printed;
871 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
873 /* Build a hash table to shorten the search time. */
874 if (! init)
876 for (op = riscv_opcodes; op->name; op++)
877 if (!riscv_hash[OP_HASH_IDX (op->match)])
878 riscv_hash[OP_HASH_IDX (op->match)] = op;
880 init = true;
883 insnlen = riscv_insn_length (word);
885 /* RISC-V instructions are always little-endian. */
886 info->endian_code = BFD_ENDIAN_LITTLE;
888 info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
889 info->bytes_per_line = 8;
890 /* We don't support constant pools, so this must be code. */
891 info->display_endian = info->endian_code;
892 info->insn_info_valid = 1;
893 info->branch_delay_insns = 0;
894 info->data_size = 0;
895 info->insn_type = dis_nonbranch;
896 info->target = 0;
897 info->target2 = 0;
899 op = riscv_hash[OP_HASH_IDX (word)];
900 if (op != NULL)
902 /* If XLEN is not known, get its value from the ELF class. */
903 if (info->mach == bfd_mach_riscv64)
904 xlen = 64;
905 else if (info->mach == bfd_mach_riscv32)
906 xlen = 32;
907 else if (info->section != NULL)
909 Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
910 xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
913 /* If arch has the Zfinx extension, replace FPR with GPR. */
914 if (riscv_subset_supports (&riscv_rps_dis, "zfinx"))
915 riscv_fpr_names = riscv_gpr_names;
916 else
917 riscv_fpr_names = riscv_gpr_names == riscv_gpr_names_abi ?
918 riscv_fpr_names_abi : riscv_fpr_names_numeric;
920 for (; op->name; op++)
922 /* Ignore macro insns. */
923 if (op->pinfo == INSN_MACRO)
924 continue;
925 /* Does the opcode match? */
926 if (! (op->match_func) (op, word))
927 continue;
928 /* Is this a pseudo-instruction and may we print it as such? */
929 if (no_aliases && (op->pinfo & INSN_ALIAS))
930 continue;
931 /* Is this instruction restricted to a certain value of XLEN? */
932 if ((op->xlen_requirement != 0) && (op->xlen_requirement != xlen))
933 continue;
934 /* Is this instruction supported by the current architecture? */
935 if (!riscv_multi_subset_supports (&riscv_rps_dis, op->insn_class))
936 continue;
938 /* It's a match. */
939 (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
940 "%s", op->name);
941 print_insn_args (op->args, word, memaddr, info);
943 /* Try to disassemble multi-instruction addressing sequences. */
944 if (pd->to_print_addr)
946 info->target = pd->print_addr;
947 (*info->fprintf_styled_func)
948 (info->stream, dis_style_comment_start, " # ");
949 (*info->print_address_func) (info->target, info);
950 pd->to_print_addr = false;
953 /* Finish filling out insn_info fields. */
954 switch (op->pinfo & INSN_TYPE)
956 case INSN_BRANCH:
957 info->insn_type = dis_branch;
958 break;
959 case INSN_CONDBRANCH:
960 info->insn_type = dis_condbranch;
961 break;
962 case INSN_JSR:
963 info->insn_type = dis_jsr;
964 break;
965 case INSN_DREF:
966 info->insn_type = dis_dref;
967 break;
968 default:
969 break;
972 if (op->pinfo & INSN_DATA_SIZE)
974 int size = ((op->pinfo & INSN_DATA_SIZE)
975 >> INSN_DATA_SIZE_SHIFT);
976 info->data_size = 1 << (size - 1);
979 return insnlen;
983 /* We did not find a match, so just print the instruction bits in
984 the shape of an assembler .insn directive. */
985 info->insn_type = dis_noninsn;
986 (*info->fprintf_styled_func)
987 (info->stream, dis_style_assembler_directive, ".insn");
988 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
989 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
990 "%d", insnlen);
991 (*info->fprintf_styled_func) (info->stream, dis_style_text, ", ");
992 (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x");
993 for (i = insnlen, printed = false; i >= 2; )
995 i -= 2;
996 word = bfd_get_bits (packet + i, 16, false);
997 if (!word && !printed)
998 continue;
1000 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1001 "%04x", (unsigned int) word);
1002 printed = true;
1005 return insnlen;
1008 /* If we find the suitable mapping symbol update the STATE.
1009 Otherwise, do nothing. */
1011 static void
1012 riscv_update_map_state (int n,
1013 enum riscv_seg_mstate *state,
1014 struct disassemble_info *info)
1016 const char *name;
1018 /* If the symbol is in a different section, ignore it. */
1019 if (info->section != NULL
1020 && info->section != info->symtab[n]->section)
1021 return;
1023 name = bfd_asymbol_name(info->symtab[n]);
1024 if (strcmp (name, "$x") == 0)
1025 *state = MAP_INSN;
1026 else if (strcmp (name, "$d") == 0)
1027 *state = MAP_DATA;
1028 else if (strncmp (name, "$xrv", 4) == 0)
1030 *state = MAP_INSN;
1031 riscv_release_subset_list (&riscv_subsets);
1033 /* ISA mapping string may be numbered, suffixed with '.n'. Do not
1034 consider this as part of the ISA string. */
1035 char *suffix = strchr (name, '.');
1036 if (suffix)
1038 int suffix_index = (int)(suffix - name);
1039 char *name_substr = xmalloc (suffix_index + 1);
1040 strncpy (name_substr, name, suffix_index);
1041 name_substr[suffix_index] = '\0';
1042 riscv_parse_subset (&riscv_rps_dis, name_substr + 2);
1043 free (name_substr);
1045 else
1046 riscv_parse_subset (&riscv_rps_dis, name + 2);
1050 /* Return true if we find the suitable mapping symbol.
1051 Otherwise, return false. */
1053 static bool
1054 riscv_is_valid_mapping_symbol (int n,
1055 struct disassemble_info *info)
1057 const char *name;
1059 /* If the symbol is in a different section, ignore it. */
1060 if (info->section != NULL
1061 && info->section != info->symtab[n]->section)
1062 return false;
1064 name = bfd_asymbol_name(info->symtab[n]);
1065 return riscv_elf_is_mapping_symbols (name);
1068 /* Check the sorted symbol table (sorted by the symbol value), find the
1069 suitable mapping symbols. */
1071 static enum riscv_seg_mstate
1072 riscv_search_mapping_symbol (bfd_vma memaddr,
1073 struct disassemble_info *info)
1075 enum riscv_seg_mstate mstate;
1076 bool from_last_map_symbol;
1077 bool found = false;
1078 int symbol = -1;
1079 int n;
1081 /* Return the last map state if the address is still within the range of the
1082 last mapping symbol. */
1083 if (last_map_section == info->section
1084 && (memaddr < last_map_symbol_boundary))
1085 return last_map_state;
1087 last_map_section = info->section;
1089 /* Decide whether to print the data or instruction by default, in case
1090 we can not find the corresponding mapping symbols. */
1091 mstate = MAP_DATA;
1092 if ((info->section
1093 && info->section->flags & SEC_CODE)
1094 || !info->section)
1095 mstate = MAP_INSN;
1097 if (info->symtab_size == 0
1098 || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
1099 return mstate;
1101 /* Reset the last_map_symbol if we start to dump a new section. */
1102 if (memaddr <= 0)
1103 last_map_symbol = -1;
1105 /* If the last stop offset is different from the current one, then
1106 don't use the last_map_symbol to search. We usually reset the
1107 info->stop_offset when handling a new section. */
1108 from_last_map_symbol = (last_map_symbol >= 0
1109 && info->stop_offset == last_stop_offset);
1111 /* Start scanning from wherever we finished last time, or the start
1112 of the function. */
1113 n = from_last_map_symbol ? last_map_symbol : info->symtab_pos + 1;
1115 /* Find the suitable mapping symbol to dump. */
1116 for (; n < info->symtab_size; n++)
1118 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1119 /* We have searched all possible symbols in the range. */
1120 if (addr > memaddr)
1121 break;
1122 if (riscv_is_valid_mapping_symbol (n, info))
1124 symbol = n;
1125 found = true;
1126 /* Do not stop searching, in case there are some mapping
1127 symbols have the same value, but have different names.
1128 Use the last one. */
1132 /* We can not find the suitable mapping symbol above. Therefore, we
1133 look forwards and try to find it again, but don't go past the start
1134 of the section. Otherwise a data section without mapping symbols
1135 can pick up a text mapping symbol of a preceeding section. */
1136 if (!found)
1138 n = from_last_map_symbol ? last_map_symbol : info->symtab_pos;
1140 for (; n >= 0; n--)
1142 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1143 /* We have searched all possible symbols in the range. */
1144 if (addr < (info->section ? info->section->vma : 0))
1145 break;
1146 /* Stop searching once we find the closed mapping symbol. */
1147 if (riscv_is_valid_mapping_symbol (n, info))
1149 symbol = n;
1150 found = true;
1151 break;
1156 if (found)
1158 riscv_update_map_state (symbol, &mstate, info);
1160 /* Find the next mapping symbol to determine the boundary of this mapping
1161 symbol. */
1163 bool found_next = false;
1164 /* Try to found next mapping symbol. */
1165 for (n = symbol + 1; n < info->symtab_size; n++)
1167 if (info->symtab[symbol]->section != info->symtab[n]->section)
1168 continue;
1170 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1171 const char *sym_name = bfd_asymbol_name(info->symtab[n]);
1172 if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd'))
1174 /* The next mapping symbol has been found, and it represents the
1175 boundary of this mapping symbol. */
1176 found_next = true;
1177 last_map_symbol_boundary = addr;
1178 break;
1182 /* No further mapping symbol has been found, indicating that the boundary
1183 of the current mapping symbol is the end of this section. */
1184 if (!found_next)
1185 last_map_symbol_boundary = info->section->vma + info->section->size;
1188 /* Save the information for next use. */
1189 last_map_symbol = symbol;
1190 last_stop_offset = info->stop_offset;
1192 return mstate;
1195 /* Decide which data size we should print. */
1197 static bfd_vma
1198 riscv_data_length (bfd_vma memaddr,
1199 disassemble_info *info)
1201 bfd_vma length;
1202 bool found = false;
1204 length = 4;
1205 if (info->symtab_size != 0
1206 && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
1207 && last_map_symbol >= 0)
1209 int n;
1210 enum riscv_seg_mstate m = MAP_NONE;
1211 for (n = last_map_symbol + 1; n < info->symtab_size; n++)
1213 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1214 if (addr > memaddr
1215 && riscv_is_valid_mapping_symbol (n, info))
1217 if (addr - memaddr < length)
1218 length = addr - memaddr;
1219 found = true;
1220 riscv_update_map_state (n, &m, info);
1221 break;
1225 if (!found)
1227 /* Do not set the length which exceeds the section size. */
1228 bfd_vma offset = info->section->vma + info->section->size;
1229 offset -= memaddr;
1230 length = (offset < length) ? offset : length;
1232 length = length == 3 ? 2 : length;
1233 return length;
1236 /* Dump the data contents. */
1238 static int
1239 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
1240 insn_t data,
1241 const bfd_byte *packet ATTRIBUTE_UNUSED,
1242 disassemble_info *info)
1244 info->display_endian = info->endian;
1246 switch (info->bytes_per_chunk)
1248 case 1:
1249 info->bytes_per_line = 6;
1250 (*info->fprintf_styled_func)
1251 (info->stream, dis_style_assembler_directive, ".byte");
1252 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1253 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1254 "0x%02x", (unsigned)data);
1255 break;
1256 case 2:
1257 info->bytes_per_line = 8;
1258 (*info->fprintf_styled_func)
1259 (info->stream, dis_style_assembler_directive, ".short");
1260 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1261 (*info->fprintf_styled_func)
1262 (info->stream, dis_style_immediate, "0x%04x", (unsigned) data);
1263 break;
1264 case 4:
1265 info->bytes_per_line = 8;
1266 (*info->fprintf_styled_func)
1267 (info->stream, dis_style_assembler_directive, ".word");
1268 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1269 (*info->fprintf_styled_func)
1270 (info->stream, dis_style_immediate, "0x%08lx",
1271 (unsigned long) data);
1272 break;
1273 case 8:
1274 info->bytes_per_line = 8;
1275 (*info->fprintf_styled_func)
1276 (info->stream, dis_style_assembler_directive, ".dword");
1277 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1278 (*info->fprintf_styled_func)
1279 (info->stream, dis_style_immediate, "0x%016llx",
1280 (unsigned long long) data);
1281 break;
1282 default:
1283 abort ();
1285 return info->bytes_per_chunk;
1288 static bool
1289 riscv_init_disasm_info (struct disassemble_info *info)
1291 int i;
1293 struct riscv_private_data *pd =
1294 xcalloc (1, sizeof (struct riscv_private_data));
1295 pd->gp = 0;
1296 pd->print_addr = 0;
1297 for (i = 0; i < (int) ARRAY_SIZE (pd->hi_addr); i++)
1298 pd->hi_addr[i] = -1;
1299 pd->to_print_addr = false;
1300 pd->has_gp = false;
1302 for (i = 0; i < info->symtab_size; i++)
1304 asymbol *sym = info->symtab[i];
1305 if (strcmp (bfd_asymbol_name (sym), RISCV_GP_SYMBOL) == 0)
1307 pd->gp = bfd_asymbol_value (sym);
1308 pd->has_gp = true;
1312 info->private_data = pd;
1313 return true;
1317 print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
1319 bfd_byte packet[RISCV_MAX_INSN_LEN];
1320 insn_t insn = 0;
1321 bfd_vma dump_size;
1322 int status;
1323 enum riscv_seg_mstate mstate;
1324 int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *,
1325 struct disassemble_info *);
1327 if (info->disassembler_options != NULL)
1329 parse_riscv_dis_options (info->disassembler_options);
1330 /* Avoid repeatedly parsing the options. */
1331 info->disassembler_options = NULL;
1333 else if (riscv_gpr_names == NULL)
1334 set_default_riscv_dis_options ();
1336 if (info->private_data == NULL && !riscv_init_disasm_info (info))
1337 return -1;
1339 mstate = riscv_search_mapping_symbol (memaddr, info);
1340 /* Save the last mapping state. */
1341 last_map_state = mstate;
1343 /* Set the size to dump. */
1344 if (mstate == MAP_DATA
1345 && (info->flags & DISASSEMBLE_DATA) == 0)
1347 dump_size = riscv_data_length (memaddr, info);
1348 info->bytes_per_chunk = dump_size;
1349 riscv_disassembler = riscv_disassemble_data;
1351 else
1353 /* Get the first 2-bytes to check the lenghth of instruction. */
1354 status = (*info->read_memory_func) (memaddr, packet, 2, info);
1355 if (status != 0)
1357 (*info->memory_error_func) (status, memaddr, info);
1358 return -1;
1360 insn = (insn_t) bfd_getl16 (packet);
1361 dump_size = riscv_insn_length (insn);
1362 riscv_disassembler = riscv_disassemble_insn;
1365 /* Fetch the instruction to dump. */
1366 status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
1367 if (status != 0)
1369 (*info->memory_error_func) (status, memaddr, info);
1370 return -1;
1372 insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
1374 return (*riscv_disassembler) (memaddr, insn, packet, info);
1377 disassembler_ftype
1378 riscv_get_disassembler (bfd *abfd)
1380 const char *default_arch = "rv64gc";
1382 if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1384 const char *sec_name = get_elf_backend_data (abfd)->obj_attrs_section;
1385 if (bfd_get_section_by_name (abfd, sec_name) != NULL)
1387 obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
1388 unsigned int Tag_a = Tag_RISCV_priv_spec;
1389 unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
1390 unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
1391 riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i,
1392 attr[Tag_b].i,
1393 attr[Tag_c].i,
1394 &default_priv_spec);
1395 default_arch = attr[Tag_RISCV_arch].s;
1399 riscv_release_subset_list (&riscv_subsets);
1400 riscv_parse_subset (&riscv_rps_dis, default_arch);
1401 return print_insn_riscv;
1404 /* Prevent use of the fake labels that are generated as part of the DWARF
1405 and for relaxable relocations in the assembler. */
1407 bool
1408 riscv_symbol_is_valid (asymbol * sym,
1409 struct disassemble_info * info ATTRIBUTE_UNUSED)
1411 const char * name;
1413 if (sym == NULL)
1414 return false;
1416 name = bfd_asymbol_name (sym);
1418 return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
1419 && !riscv_elf_is_mapping_symbols (name));
1423 /* Indices into option argument vector for options accepting an argument.
1424 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1426 typedef enum
1428 RISCV_OPTION_ARG_NONE = -1,
1429 RISCV_OPTION_ARG_PRIV_SPEC,
1431 RISCV_OPTION_ARG_COUNT
1432 } riscv_option_arg_t;
1434 /* Valid RISCV disassembler options. */
1436 static struct
1438 const char *name;
1439 const char *description;
1440 riscv_option_arg_t arg;
1441 } riscv_options[] =
1443 { "numeric",
1444 N_("Print numeric register names, rather than ABI names."),
1445 RISCV_OPTION_ARG_NONE },
1446 { "no-aliases",
1447 N_("Disassemble only into canonical instructions."),
1448 RISCV_OPTION_ARG_NONE },
1449 { "priv-spec=",
1450 N_("Print the CSR according to the chosen privilege spec."),
1451 RISCV_OPTION_ARG_PRIV_SPEC }
1454 /* Build the structure representing valid RISCV disassembler options.
1455 This is done dynamically for maintenance ease purpose; a static
1456 initializer would be unreadable. */
1458 const disasm_options_and_args_t *
1459 disassembler_options_riscv (void)
1461 static disasm_options_and_args_t *opts_and_args;
1463 if (opts_and_args == NULL)
1465 size_t num_options = ARRAY_SIZE (riscv_options);
1466 size_t num_args = RISCV_OPTION_ARG_COUNT;
1467 disasm_option_arg_t *args;
1468 disasm_options_t *opts;
1469 size_t i, priv_spec_count;
1471 args = XNEWVEC (disasm_option_arg_t, num_args + 1);
1473 args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC";
1474 priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_CLASS_NONE - 1;
1475 args[RISCV_OPTION_ARG_PRIV_SPEC].values
1476 = XNEWVEC (const char *, priv_spec_count + 1);
1477 for (i = 0; i < priv_spec_count; i++)
1478 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i]
1479 = riscv_priv_specs[i].name;
1480 /* The array we return must be NULL terminated. */
1481 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL;
1483 /* The array we return must be NULL terminated. */
1484 args[num_args].name = NULL;
1485 args[num_args].values = NULL;
1487 opts_and_args = XNEW (disasm_options_and_args_t);
1488 opts_and_args->args = args;
1490 opts = &opts_and_args->options;
1491 opts->name = XNEWVEC (const char *, num_options + 1);
1492 opts->description = XNEWVEC (const char *, num_options + 1);
1493 opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1);
1494 for (i = 0; i < num_options; i++)
1496 opts->name[i] = riscv_options[i].name;
1497 opts->description[i] = _(riscv_options[i].description);
1498 if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE)
1499 opts->arg[i] = &args[riscv_options[i].arg];
1500 else
1501 opts->arg[i] = NULL;
1503 /* The array we return must be NULL terminated. */
1504 opts->name[i] = NULL;
1505 opts->description[i] = NULL;
1506 opts->arg[i] = NULL;
1509 return opts_and_args;
1512 void
1513 print_riscv_disassembler_options (FILE *stream)
1515 const disasm_options_and_args_t *opts_and_args;
1516 const disasm_option_arg_t *args;
1517 const disasm_options_t *opts;
1518 size_t max_len = 0;
1519 size_t i;
1520 size_t j;
1522 opts_and_args = disassembler_options_riscv ();
1523 opts = &opts_and_args->options;
1524 args = opts_and_args->args;
1526 fprintf (stream, _("\n\
1527 The following RISC-V specific disassembler options are supported for use\n\
1528 with the -M switch (multiple options should be separated by commas):\n"));
1529 fprintf (stream, "\n");
1531 /* Compute the length of the longest option name. */
1532 for (i = 0; opts->name[i] != NULL; i++)
1534 size_t len = strlen (opts->name[i]);
1536 if (opts->arg[i] != NULL)
1537 len += strlen (opts->arg[i]->name);
1538 if (max_len < len)
1539 max_len = len;
1542 for (i = 0, max_len++; opts->name[i] != NULL; i++)
1544 fprintf (stream, " %s", opts->name[i]);
1545 if (opts->arg[i] != NULL)
1546 fprintf (stream, "%s", opts->arg[i]->name);
1547 if (opts->description[i] != NULL)
1549 size_t len = strlen (opts->name[i]);
1551 if (opts->arg != NULL && opts->arg[i] != NULL)
1552 len += strlen (opts->arg[i]->name);
1553 fprintf (stream, "%*c %s", (int) (max_len - len), ' ',
1554 opts->description[i]);
1556 fprintf (stream, "\n");
1559 for (i = 0; args[i].name != NULL; i++)
1561 if (args[i].values == NULL)
1562 continue;
1563 fprintf (stream, _("\n\
1564 For the options above, the following values are supported for \"%s\":\n "),
1565 args[i].name);
1566 for (j = 0; args[i].values[j] != NULL; j++)
1567 fprintf (stream, " %s", args[i].values[j]);
1568 fprintf (stream, _("\n"));
1571 fprintf (stream, _("\n"));
1574 void disassemble_free_riscv (struct disassemble_info *info ATTRIBUTE_UNUSED)
1576 riscv_release_subset_list (&riscv_subsets);