Remove path name from test case
[binutils-gdb.git] / opcodes / riscv-dis.c
1 /* RISC-V disassembler
2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
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"
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"
31
32 #include <stdint.h>
33 #include <ctype.h>
34
35 /* Current XLEN for the disassembler. */
36 static unsigned xlen = 0;
37
38 /* Default ISA specification version (constant as of now). */
39 static enum riscv_spec_class default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
40
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;
44
45 static riscv_subset_list_t riscv_subsets;
46 static riscv_parse_subset_t riscv_rps_dis =
47 {
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. */
53 };
54
55 struct riscv_private_data
56 {
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;
62 };
63
64 /* Used for mapping symbols. */
65 static int last_map_symbol = -1;
66 static bfd_vma last_stop_offset = 0;
67 static bfd_vma last_map_symbol_boundary = 0;
68 static enum riscv_seg_mstate last_map_state = MAP_NONE;
69 static asection *last_map_section = NULL;
70
71 /* Register names as used by the disassembler. */
72 static const char (*riscv_gpr_names)[NRC];
73 static const char (*riscv_fpr_names)[NRC];
74
75 /* If set, disassemble as most general instruction. */
76 static bool no_aliases = false;
77
78
79 /* Set default RISC-V disassembler options. */
80
81 static void
82 set_default_riscv_dis_options (void)
83 {
84 riscv_gpr_names = riscv_gpr_names_abi;
85 riscv_fpr_names = riscv_fpr_names_abi;
86 no_aliases = false;
87 }
88
89 /* Parse RISC-V disassembler option (without arguments). */
90
91 static bool
92 parse_riscv_dis_option_without_args (const char *option)
93 {
94 if (strcmp (option, "no-aliases") == 0)
95 no_aliases = true;
96 else if (strcmp (option, "numeric") == 0)
97 {
98 riscv_gpr_names = riscv_gpr_names_numeric;
99 riscv_fpr_names = riscv_fpr_names_numeric;
100 }
101 else
102 return false;
103 return true;
104 }
105
106 /* Parse RISC-V disassembler option (possibly with arguments). */
107
108 static void
109 parse_riscv_dis_option (const char *option)
110 {
111 char *equal, *value;
112
113 if (parse_riscv_dis_option_without_args (option))
114 return;
115
116 equal = strchr (option, '=');
117 if (equal == NULL)
118 {
119 /* The option without '=' should be defined above. */
120 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
121 return;
122 }
123 if (equal == option
124 || *(equal + 1) == '\0')
125 {
126 /* Invalid options with '=', no option name before '=',
127 and no value after '='. */
128 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
129 option);
130 return;
131 }
132
133 *equal = '\0';
134 value = equal + 1;
135 if (strcmp (option, "priv-spec") == 0)
136 {
137 enum riscv_spec_class priv_spec = PRIV_SPEC_CLASS_NONE;
138 const char *name = NULL;
139
140 RISCV_GET_PRIV_SPEC_CLASS (value, priv_spec);
141 if (priv_spec == PRIV_SPEC_CLASS_NONE)
142 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
143 option, value);
144 else if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
145 default_priv_spec = priv_spec;
146 else if (default_priv_spec != priv_spec)
147 {
148 RISCV_GET_PRIV_SPEC_NAME (name, default_priv_spec);
149 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
150 "the elf privilege attribute is %s"),
151 option, value, name);
152 }
153 }
154 else
155 {
156 /* xgettext:c-format */
157 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
158 }
159 }
160
161 /* Parse RISC-V disassembler options. */
162
163 static void
164 parse_riscv_dis_options (const char *opts_in)
165 {
166 char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
167
168 set_default_riscv_dis_options ();
169
170 for ( ; opt_end != NULL; opt = opt_end + 1)
171 {
172 if ((opt_end = strchr (opt, ',')) != NULL)
173 *opt_end = 0;
174 parse_riscv_dis_option (opt);
175 }
176
177 free (opts);
178 }
179
180 /* Print one argument from an array. */
181
182 static void
183 arg_print (struct disassemble_info *info, unsigned long val,
184 const char* const* array, size_t size)
185 {
186 const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
187 (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s);
188 }
189
190 /* If we need to print an address, set its value and state. */
191
192 static void
193 maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset,
194 int wide)
195 {
196 if (pd->hi_addr[base_reg] != (bfd_vma)-1)
197 {
198 pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
199 pd->hi_addr[base_reg] = -1;
200 }
201 else if (base_reg == X_GP && pd->has_gp)
202 pd->print_addr = pd->gp + offset;
203 else if (base_reg == X_TP || base_reg == 0)
204 pd->print_addr = offset;
205 else
206 return; /* Don't print the address. */
207 pd->to_print_addr = true;
208
209 /* Sign-extend a 32-bit value to a 64-bit value. */
210 if (wide)
211 pd->print_addr = (bfd_vma)(int32_t) pd->print_addr;
212
213 /* Fit into a 32-bit value on RV32. */
214 if (xlen == 32)
215 pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr;
216 }
217
218 /* Print insn arguments for 32/64-bit code. */
219
220 static void
221 print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info)
222 {
223 struct riscv_private_data *pd = info->private_data;
224 int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
225 int rd = (l >> OP_SH_RD) & OP_MASK_RD;
226 fprintf_styled_ftype print = info->fprintf_styled_func;
227 const char *opargStart;
228
229 if (*oparg != '\0')
230 print (info->stream, dis_style_text, "\t");
231
232 for (; *oparg != '\0'; oparg++)
233 {
234 opargStart = oparg;
235 switch (*oparg)
236 {
237 case 'C': /* RVC */
238 switch (*++oparg)
239 {
240 case 's': /* RS1 x8-x15. */
241 case 'w': /* RS1 x8-x15. */
242 print (info->stream, dis_style_register, "%s",
243 riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
244 break;
245 case 't': /* RS2 x8-x15. */
246 case 'x': /* RS2 x8-x15. */
247 print (info->stream, dis_style_register, "%s",
248 riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
249 break;
250 case 'U': /* RS1, constrained to equal RD. */
251 print (info->stream, dis_style_register,
252 "%s", riscv_gpr_names[rd]);
253 break;
254 case 'c': /* RS1, constrained to equal sp. */
255 print (info->stream, dis_style_register, "%s",
256 riscv_gpr_names[X_SP]);
257 break;
258 case 'V': /* RS2 */
259 print (info->stream, dis_style_register, "%s",
260 riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
261 break;
262 case 'o':
263 case 'j':
264 if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0)
265 maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0);
266 if (info->mach == bfd_mach_riscv64
267 && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0)
268 maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1);
269 print (info->stream, dis_style_immediate, "%d",
270 (int)EXTRACT_CITYPE_IMM (l));
271 break;
272 case 'k':
273 print (info->stream, dis_style_address_offset, "%d",
274 (int)EXTRACT_CLTYPE_LW_IMM (l));
275 break;
276 case 'l':
277 print (info->stream, dis_style_address_offset, "%d",
278 (int)EXTRACT_CLTYPE_LD_IMM (l));
279 break;
280 case 'm':
281 print (info->stream, dis_style_address_offset, "%d",
282 (int)EXTRACT_CITYPE_LWSP_IMM (l));
283 break;
284 case 'n':
285 print (info->stream, dis_style_address_offset, "%d",
286 (int)EXTRACT_CITYPE_LDSP_IMM (l));
287 break;
288 case 'K':
289 print (info->stream, dis_style_immediate, "%d",
290 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
291 break;
292 case 'L':
293 print (info->stream, dis_style_immediate, "%d",
294 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
295 break;
296 case 'M':
297 print (info->stream, dis_style_address_offset, "%d",
298 (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
299 break;
300 case 'N':
301 print (info->stream, dis_style_address_offset, "%d",
302 (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
303 break;
304 case 'p':
305 info->target = EXTRACT_CBTYPE_IMM (l) + pc;
306 (*info->print_address_func) (info->target, info);
307 break;
308 case 'a':
309 info->target = EXTRACT_CJTYPE_IMM (l) + pc;
310 (*info->print_address_func) (info->target, info);
311 break;
312 case 'u':
313 print (info->stream, dis_style_immediate, "0x%x",
314 (unsigned)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
315 break;
316 case '>':
317 print (info->stream, dis_style_immediate, "0x%x",
318 (unsigned)EXTRACT_CITYPE_IMM (l) & 0x3f);
319 break;
320 case '<':
321 print (info->stream, dis_style_immediate, "0x%x",
322 (unsigned)EXTRACT_CITYPE_IMM (l) & 0x1f);
323 break;
324 case 'T': /* Floating-point RS2. */
325 print (info->stream, dis_style_register, "%s",
326 riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
327 break;
328 case 'D': /* Floating-point RS2 x8-x15. */
329 print (info->stream, dis_style_register, "%s",
330 riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
331 break;
332 }
333 break;
334
335 case 'V': /* RVV */
336 switch (*++oparg)
337 {
338 case 'd':
339 case 'f':
340 print (info->stream, dis_style_register, "%s",
341 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
342 break;
343 case 'e':
344 if (!EXTRACT_OPERAND (VWD, l))
345 print (info->stream, dis_style_register, "%s",
346 riscv_gpr_names[0]);
347 else
348 print (info->stream, dis_style_register, "%s",
349 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
350 break;
351 case 's':
352 print (info->stream, dis_style_register, "%s",
353 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS1, l)]);
354 break;
355 case 't':
356 case 'u': /* VS1 == VS2 already verified at this point. */
357 case 'v': /* VD == VS1 == VS2 already verified at this point. */
358 print (info->stream, dis_style_register, "%s",
359 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS2, l)]);
360 break;
361 case '0':
362 print (info->stream, dis_style_register, "%s",
363 riscv_vecr_names_numeric[0]);
364 break;
365 case 'b':
366 case 'c':
367 {
368 int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l)
369 : EXTRACT_RVV_VC_IMM (l);
370 unsigned int imm_vlmul = EXTRACT_OPERAND (VLMUL, imm);
371 unsigned int imm_vsew = EXTRACT_OPERAND (VSEW, imm);
372 unsigned int imm_vta = EXTRACT_OPERAND (VTA, imm);
373 unsigned int imm_vma = EXTRACT_OPERAND (VMA, imm);
374 unsigned int imm_vtype_res = (imm >> 8);
375
376 if (imm_vsew < ARRAY_SIZE (riscv_vsew)
377 && imm_vlmul < ARRAY_SIZE (riscv_vlmul)
378 && imm_vta < ARRAY_SIZE (riscv_vta)
379 && imm_vma < ARRAY_SIZE (riscv_vma)
380 && !imm_vtype_res
381 && riscv_vsew[imm_vsew] != NULL
382 && riscv_vlmul[imm_vlmul] != NULL)
383 print (info->stream, dis_style_text, "%s,%s,%s,%s",
384 riscv_vsew[imm_vsew],
385 riscv_vlmul[imm_vlmul], riscv_vta[imm_vta],
386 riscv_vma[imm_vma]);
387 else
388 print (info->stream, dis_style_immediate, "%d", imm);
389 }
390 break;
391 case 'i':
392 print (info->stream, dis_style_immediate, "%d",
393 (int)EXTRACT_RVV_VI_IMM (l));
394 break;
395 case 'j':
396 print (info->stream, dis_style_immediate, "%d",
397 (int)EXTRACT_RVV_VI_UIMM (l));
398 break;
399 case 'k':
400 print (info->stream, dis_style_immediate, "%d",
401 (int)EXTRACT_RVV_OFFSET (l));
402 break;
403 case 'l':
404 print (info->stream, dis_style_immediate, "%d",
405 (int)EXTRACT_RVV_VI_UIMM6 (l));
406 break;
407 case 'm':
408 if (!EXTRACT_OPERAND (VMASK, l))
409 {
410 print (info->stream, dis_style_text, ",");
411 print (info->stream, dis_style_register, "%s",
412 riscv_vecm_names_numeric[0]);
413 }
414 break;
415 }
416 break;
417
418 case ',':
419 case '(':
420 case ')':
421 case '[':
422 case ']':
423 print (info->stream, dis_style_text, "%c", *oparg);
424 break;
425
426 case '0':
427 /* Only print constant 0 if it is the last argument. */
428 if (!oparg[1])
429 print (info->stream, dis_style_immediate, "0");
430 break;
431
432 case 's':
433 if ((l & MASK_JALR) == MATCH_JALR)
434 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
435 print (info->stream, dis_style_register, "%s", riscv_gpr_names[rs1]);
436 break;
437
438 case 't':
439 print (info->stream, dis_style_register, "%s",
440 riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
441 break;
442
443 case 'u':
444 print (info->stream, dis_style_immediate, "0x%x",
445 (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
446 break;
447
448 case 'm':
449 arg_print (info, EXTRACT_OPERAND (RM, l),
450 riscv_rm, ARRAY_SIZE (riscv_rm));
451 break;
452
453 case 'P':
454 arg_print (info, EXTRACT_OPERAND (PRED, l),
455 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
456 break;
457
458 case 'Q':
459 arg_print (info, EXTRACT_OPERAND (SUCC, l),
460 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
461 break;
462
463 case 'o':
464 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
465 /* Fall through. */
466 case 'j':
467 if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
468 || (l & MASK_JALR) == MATCH_JALR)
469 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
470 if (info->mach == bfd_mach_riscv64
471 && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0)
472 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1);
473 print (info->stream, dis_style_immediate, "%d",
474 (int)EXTRACT_ITYPE_IMM (l));
475 break;
476
477 case 'q':
478 maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0);
479 print (info->stream, dis_style_address_offset, "%d",
480 (int)EXTRACT_STYPE_IMM (l));
481 break;
482
483 case 'a':
484 info->target = EXTRACT_JTYPE_IMM (l) + pc;
485 (*info->print_address_func) (info->target, info);
486 break;
487
488 case 'p':
489 info->target = EXTRACT_BTYPE_IMM (l) + pc;
490 (*info->print_address_func) (info->target, info);
491 break;
492
493 case 'd':
494 if ((l & MASK_AUIPC) == MATCH_AUIPC)
495 pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
496 else if ((l & MASK_LUI) == MATCH_LUI)
497 pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
498 else if ((l & MASK_C_LUI) == MATCH_C_LUI)
499 pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
500 print (info->stream, dis_style_register, "%s", riscv_gpr_names[rd]);
501 break;
502
503 case 'y':
504 print (info->stream, dis_style_immediate, "0x%x",
505 EXTRACT_OPERAND (BS, l));
506 break;
507
508 case 'z':
509 print (info->stream, dis_style_register, "%s", riscv_gpr_names[0]);
510 break;
511
512 case '>':
513 print (info->stream, dis_style_immediate, "0x%x",
514 EXTRACT_OPERAND (SHAMT, l));
515 break;
516
517 case '<':
518 print (info->stream, dis_style_immediate, "0x%x",
519 EXTRACT_OPERAND (SHAMTW, l));
520 break;
521
522 case 'S':
523 case 'U':
524 print (info->stream, dis_style_register, "%s", riscv_fpr_names[rs1]);
525 break;
526
527 case 'T':
528 print (info->stream, dis_style_register, "%s",
529 riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
530 break;
531
532 case 'D':
533 print (info->stream, dis_style_register, "%s", riscv_fpr_names[rd]);
534 break;
535
536 case 'R':
537 print (info->stream, dis_style_register, "%s",
538 riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
539 break;
540
541 case 'E':
542 {
543 static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */
544 static bool init_csr = false;
545 unsigned int csr = EXTRACT_OPERAND (CSR, l);
546
547 if (!init_csr)
548 {
549 unsigned int i;
550 for (i = 0; i < 4096; i++)
551 riscv_csr_hash[i] = NULL;
552
553 /* Set to the newest privileged version. */
554 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
555 default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1;
556
557 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
558 if (riscv_csr_hash[num] == NULL \
559 && ((define_version == PRIV_SPEC_CLASS_NONE \
560 && abort_version == PRIV_SPEC_CLASS_NONE) \
561 || (default_priv_spec >= define_version \
562 && default_priv_spec < abort_version))) \
563 riscv_csr_hash[num] = #name;
564 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
565 DECLARE_CSR (name, num, class, define_version, abort_version)
566 #include "opcode/riscv-opc.h"
567 #undef DECLARE_CSR
568 }
569
570 if (riscv_csr_hash[csr] != NULL)
571 print (info->stream, dis_style_register, "%s",
572 riscv_csr_hash[csr]);
573 else
574 print (info->stream, dis_style_immediate, "0x%x", csr);
575 break;
576 }
577
578 case 'Y':
579 print (info->stream, dis_style_immediate, "0x%x",
580 EXTRACT_OPERAND (RNUM, l));
581 break;
582
583 case 'Z':
584 print (info->stream, dis_style_immediate, "%d", rs1);
585 break;
586
587 case 'W': /* Various operands for standard z extensions. */
588 switch (*++oparg)
589 {
590 case 'i':
591 switch (*++oparg)
592 {
593 case 'f':
594 print (info->stream, dis_style_address_offset, "%d",
595 (int) EXTRACT_STYPE_IMM (l));
596 break;
597 default:
598 goto undefined_modifier;
599 }
600 break;
601 case 'f':
602 switch (*++oparg)
603 {
604 case 'v':
605 if (riscv_fli_symval[rs1])
606 print (info->stream, dis_style_text, "%s",
607 riscv_fli_symval[rs1]);
608 else
609 print (info->stream, dis_style_immediate, "%a",
610 riscv_fli_numval[rs1]);
611 break;
612 default:
613 goto undefined_modifier;
614 }
615 break;
616 case 'c': /* Zcb extension 16 bits length instruction fields. */
617 switch (*++oparg)
618 {
619 case 'b':
620 print (info->stream, dis_style_immediate, "%d",
621 (int)EXTRACT_ZCB_BYTE_UIMM (l));
622 break;
623 case 'h':
624 print (info->stream, dis_style_immediate, "%d",
625 (int)EXTRACT_ZCB_HALFWORD_UIMM (l));
626 break;
627 default:
628 goto undefined_modifier;
629 }
630 break;
631 default:
632 goto undefined_modifier;
633 }
634 break;
635
636 case 'X': /* Vendor-specific operands. */
637 switch (*++oparg)
638 {
639 case 't': /* Vendor-specific (T-head) operands. */
640 {
641 size_t n;
642 size_t s;
643 bool sign;
644 switch (*++oparg)
645 {
646 case 'l': /* Integer immediate, literal. */
647 oparg++;
648 while (*oparg && *oparg != ',')
649 {
650 print (info->stream, dis_style_immediate, "%c", *oparg);
651 oparg++;
652 }
653 oparg--;
654 break;
655 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
656 sign = true;
657 goto print_imm;
658 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
659 sign = false;
660 goto print_imm;
661 print_imm:
662 n = strtol (oparg + 1, (char **)&oparg, 10);
663 if (*oparg != '@')
664 goto undefined_modifier;
665 s = strtol (oparg + 1, (char **)&oparg, 10);
666 oparg--;
667
668 if (!sign)
669 print (info->stream, dis_style_immediate, "%lu",
670 (unsigned long)EXTRACT_U_IMM (n, s, l));
671 else
672 print (info->stream, dis_style_immediate, "%li",
673 (signed long)EXTRACT_S_IMM (n, s, l));
674 break;
675 default:
676 goto undefined_modifier;
677 }
678 }
679 break;
680 case 'c': /* Vendor-specific (CORE-V) operands. */
681 switch (*++oparg)
682 {
683 case '2':
684 print (info->stream, dis_style_immediate, "%d",
685 ((int) EXTRACT_CV_IS2_UIMM5 (l)));
686 break;
687 case '3':
688 print (info->stream, dis_style_immediate, "%d",
689 ((int) EXTRACT_CV_IS3_UIMM5 (l)));
690 break;
691 default:
692 goto undefined_modifier;
693 }
694 break;
695 default:
696 goto undefined_modifier;
697 }
698 break;
699
700 default:
701 undefined_modifier:
702 /* xgettext:c-format */
703 print (info->stream, dis_style_text,
704 _("# internal error, undefined modifier (%c)"),
705 *opargStart);
706 return;
707 }
708 }
709 }
710
711 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
712 on using INFO. Returns length of the instruction, in bytes.
713 BIGENDIAN must be 1 if this is big-endian code, 0 if
714 this is little-endian code. */
715
716 static int
717 riscv_disassemble_insn (bfd_vma memaddr,
718 insn_t word,
719 const bfd_byte *packet,
720 disassemble_info *info)
721 {
722 const struct riscv_opcode *op;
723 static bool init = false;
724 static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
725 struct riscv_private_data *pd = info->private_data;
726 int insnlen, i;
727 bool printed;
728
729 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
730
731 /* Build a hash table to shorten the search time. */
732 if (! init)
733 {
734 for (op = riscv_opcodes; op->name; op++)
735 if (!riscv_hash[OP_HASH_IDX (op->match)])
736 riscv_hash[OP_HASH_IDX (op->match)] = op;
737
738 init = true;
739 }
740
741 insnlen = riscv_insn_length (word);
742
743 /* RISC-V instructions are always little-endian. */
744 info->endian_code = BFD_ENDIAN_LITTLE;
745
746 info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
747 info->bytes_per_line = 8;
748 /* We don't support constant pools, so this must be code. */
749 info->display_endian = info->endian_code;
750 info->insn_info_valid = 1;
751 info->branch_delay_insns = 0;
752 info->data_size = 0;
753 info->insn_type = dis_nonbranch;
754 info->target = 0;
755 info->target2 = 0;
756
757 op = riscv_hash[OP_HASH_IDX (word)];
758 if (op != NULL)
759 {
760 /* If XLEN is not known, get its value from the ELF class. */
761 if (info->mach == bfd_mach_riscv64)
762 xlen = 64;
763 else if (info->mach == bfd_mach_riscv32)
764 xlen = 32;
765 else if (info->section != NULL)
766 {
767 Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
768 xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
769 }
770
771 /* If arch has the Zfinx extension, replace FPR with GPR. */
772 if (riscv_subset_supports (&riscv_rps_dis, "zfinx"))
773 riscv_fpr_names = riscv_gpr_names;
774 else
775 riscv_fpr_names = riscv_gpr_names == riscv_gpr_names_abi ?
776 riscv_fpr_names_abi : riscv_fpr_names_numeric;
777
778 for (; op->name; op++)
779 {
780 /* Does the opcode match? */
781 if (! (op->match_func) (op, word))
782 continue;
783 /* Is this a pseudo-instruction and may we print it as such? */
784 if (no_aliases && (op->pinfo & INSN_ALIAS))
785 continue;
786 /* Is this instruction restricted to a certain value of XLEN? */
787 if ((op->xlen_requirement != 0) && (op->xlen_requirement != xlen))
788 continue;
789 /* Is this instruction supported by the current architecture? */
790 if (!riscv_multi_subset_supports (&riscv_rps_dis, op->insn_class))
791 continue;
792
793 /* It's a match. */
794 (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
795 "%s", op->name);
796 print_insn_args (op->args, word, memaddr, info);
797
798 /* Try to disassemble multi-instruction addressing sequences. */
799 if (pd->to_print_addr)
800 {
801 info->target = pd->print_addr;
802 (*info->fprintf_styled_func)
803 (info->stream, dis_style_comment_start, " # ");
804 (*info->print_address_func) (info->target, info);
805 pd->to_print_addr = false;
806 }
807
808 /* Finish filling out insn_info fields. */
809 switch (op->pinfo & INSN_TYPE)
810 {
811 case INSN_BRANCH:
812 info->insn_type = dis_branch;
813 break;
814 case INSN_CONDBRANCH:
815 info->insn_type = dis_condbranch;
816 break;
817 case INSN_JSR:
818 info->insn_type = dis_jsr;
819 break;
820 case INSN_DREF:
821 info->insn_type = dis_dref;
822 break;
823 default:
824 break;
825 }
826
827 if (op->pinfo & INSN_DATA_SIZE)
828 {
829 int size = ((op->pinfo & INSN_DATA_SIZE)
830 >> INSN_DATA_SIZE_SHIFT);
831 info->data_size = 1 << (size - 1);
832 }
833
834 return insnlen;
835 }
836 }
837
838 /* We did not find a match, so just print the instruction bits in
839 the shape of an assembler .insn directive. */
840 info->insn_type = dis_noninsn;
841 (*info->fprintf_styled_func)
842 (info->stream, dis_style_assembler_directive, ".insn");
843 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
844 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
845 "%d", insnlen);
846 (*info->fprintf_styled_func) (info->stream, dis_style_text, ", ");
847 (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x");
848 for (i = insnlen, printed = false; i >= 2; )
849 {
850 i -= 2;
851 word = bfd_get_bits (packet + i, 16, false);
852 if (!word && !printed)
853 continue;
854
855 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
856 "%04x", (unsigned int) word);
857 printed = true;
858 }
859
860 return insnlen;
861 }
862
863 /* Return true if we find the suitable mapping symbol,
864 and also update the STATE. Otherwise, return false. */
865
866 static bool
867 riscv_get_map_state (int n,
868 enum riscv_seg_mstate *state,
869 struct disassemble_info *info)
870 {
871 const char *name;
872
873 /* If the symbol is in a different section, ignore it. */
874 if (info->section != NULL
875 && info->section != info->symtab[n]->section)
876 return false;
877
878 name = bfd_asymbol_name(info->symtab[n]);
879 if (strcmp (name, "$x") == 0)
880 *state = MAP_INSN;
881 else if (strcmp (name, "$d") == 0)
882 *state = MAP_DATA;
883 else if (strncmp (name, "$xrv", 4) == 0)
884 {
885 *state = MAP_INSN;
886 riscv_release_subset_list (&riscv_subsets);
887
888 /* ISA mapping string may be numbered, suffixed with '.n'. Do not
889 consider this as part of the ISA string. */
890 char *suffix = strchr (name, '.');
891 if (suffix)
892 {
893 int suffix_index = (int)(suffix - name);
894 char *name_substr = xmalloc (suffix_index + 1);
895 strncpy (name_substr, name, suffix_index);
896 name_substr[suffix_index] = '\0';
897 riscv_parse_subset (&riscv_rps_dis, name_substr + 2);
898 free (name_substr);
899 }
900 else
901 riscv_parse_subset (&riscv_rps_dis, name + 2);
902 }
903 else
904 return false;
905
906 return true;
907 }
908
909 /* Check the sorted symbol table (sorted by the symbol value), find the
910 suitable mapping symbols. */
911
912 static enum riscv_seg_mstate
913 riscv_search_mapping_symbol (bfd_vma memaddr,
914 struct disassemble_info *info)
915 {
916 enum riscv_seg_mstate mstate;
917 bool from_last_map_symbol;
918 bool found = false;
919 int symbol = -1;
920 int n;
921
922 /* Return the last map state if the address is still within the range of the
923 last mapping symbol. */
924 if (last_map_section == info->section
925 && (memaddr < last_map_symbol_boundary))
926 return last_map_state;
927
928 last_map_section = info->section;
929
930 /* Decide whether to print the data or instruction by default, in case
931 we can not find the corresponding mapping symbols. */
932 mstate = MAP_DATA;
933 if ((info->section
934 && info->section->flags & SEC_CODE)
935 || !info->section)
936 mstate = MAP_INSN;
937
938 if (info->symtab_size == 0
939 || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
940 return mstate;
941
942 /* Reset the last_map_symbol if we start to dump a new section. */
943 if (memaddr <= 0)
944 last_map_symbol = -1;
945
946 /* If the last stop offset is different from the current one, then
947 don't use the last_map_symbol to search. We usually reset the
948 info->stop_offset when handling a new section. */
949 from_last_map_symbol = (last_map_symbol >= 0
950 && info->stop_offset == last_stop_offset);
951
952 /* Start scanning at the start of the function, or wherever
953 we finished last time. */
954 n = info->symtab_pos + 1;
955 if (from_last_map_symbol && n >= last_map_symbol)
956 n = last_map_symbol;
957
958 /* Find the suitable mapping symbol to dump. */
959 for (; n < info->symtab_size; n++)
960 {
961 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
962 /* We have searched all possible symbols in the range. */
963 if (addr > memaddr)
964 break;
965 if (riscv_get_map_state (n, &mstate, info))
966 {
967 symbol = n;
968 found = true;
969 /* Do not stop searching, in case there are some mapping
970 symbols have the same value, but have different names.
971 Use the last one. */
972 }
973 }
974
975 /* We can not find the suitable mapping symbol above. Therefore, we
976 look forwards and try to find it again, but don't go past the start
977 of the section. Otherwise a data section without mapping symbols
978 can pick up a text mapping symbol of a preceeding section. */
979 if (!found)
980 {
981 n = info->symtab_pos;
982 if (from_last_map_symbol && n >= last_map_symbol)
983 n = last_map_symbol;
984
985 for (; n >= 0; n--)
986 {
987 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
988 /* We have searched all possible symbols in the range. */
989 if (addr < (info->section ? info->section->vma : 0))
990 break;
991 /* Stop searching once we find the closed mapping symbol. */
992 if (riscv_get_map_state (n, &mstate, info))
993 {
994 symbol = n;
995 found = true;
996 break;
997 }
998 }
999 }
1000
1001 if (found)
1002 {
1003 /* Find the next mapping symbol to determine the boundary of this mapping
1004 symbol. */
1005
1006 bool found_next = false;
1007 /* Try to found next mapping symbol. */
1008 for (n = symbol + 1; n < info->symtab_size; n++)
1009 {
1010 if (info->symtab[symbol]->section != info->symtab[n]->section)
1011 continue;
1012
1013 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1014 const char *sym_name = bfd_asymbol_name(info->symtab[n]);
1015 if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd'))
1016 {
1017 /* The next mapping symbol has been found, and it represents the
1018 boundary of this mapping symbol. */
1019 found_next = true;
1020 last_map_symbol_boundary = addr;
1021 break;
1022 }
1023 }
1024
1025 /* No further mapping symbol has been found, indicating that the boundary
1026 of the current mapping symbol is the end of this section. */
1027 if (!found_next)
1028 last_map_symbol_boundary = info->section->vma + info->section->size;
1029 }
1030
1031 /* Save the information for next use. */
1032 last_map_symbol = symbol;
1033 last_stop_offset = info->stop_offset;
1034
1035 return mstate;
1036 }
1037
1038 /* Decide which data size we should print. */
1039
1040 static bfd_vma
1041 riscv_data_length (bfd_vma memaddr,
1042 disassemble_info *info)
1043 {
1044 bfd_vma length;
1045 bool found = false;
1046
1047 length = 4;
1048 if (info->symtab_size != 0
1049 && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
1050 && last_map_symbol >= 0)
1051 {
1052 int n;
1053 enum riscv_seg_mstate m = MAP_NONE;
1054 for (n = last_map_symbol + 1; n < info->symtab_size; n++)
1055 {
1056 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1057 if (addr > memaddr
1058 && riscv_get_map_state (n, &m, info))
1059 {
1060 if (addr - memaddr < length)
1061 length = addr - memaddr;
1062 found = true;
1063 break;
1064 }
1065 }
1066 }
1067 if (!found)
1068 {
1069 /* Do not set the length which exceeds the section size. */
1070 bfd_vma offset = info->section->vma + info->section->size;
1071 offset -= memaddr;
1072 length = (offset < length) ? offset : length;
1073 }
1074 length = length == 3 ? 2 : length;
1075 return length;
1076 }
1077
1078 /* Dump the data contents. */
1079
1080 static int
1081 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
1082 insn_t data,
1083 const bfd_byte *packet ATTRIBUTE_UNUSED,
1084 disassemble_info *info)
1085 {
1086 info->display_endian = info->endian;
1087
1088 switch (info->bytes_per_chunk)
1089 {
1090 case 1:
1091 info->bytes_per_line = 6;
1092 (*info->fprintf_styled_func)
1093 (info->stream, dis_style_assembler_directive, ".byte");
1094 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1095 (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1096 "0x%02x", (unsigned)data);
1097 break;
1098 case 2:
1099 info->bytes_per_line = 8;
1100 (*info->fprintf_styled_func)
1101 (info->stream, dis_style_assembler_directive, ".short");
1102 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1103 (*info->fprintf_styled_func)
1104 (info->stream, dis_style_immediate, "0x%04x", (unsigned) data);
1105 break;
1106 case 4:
1107 info->bytes_per_line = 8;
1108 (*info->fprintf_styled_func)
1109 (info->stream, dis_style_assembler_directive, ".word");
1110 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1111 (*info->fprintf_styled_func)
1112 (info->stream, dis_style_immediate, "0x%08lx",
1113 (unsigned long) data);
1114 break;
1115 case 8:
1116 info->bytes_per_line = 8;
1117 (*info->fprintf_styled_func)
1118 (info->stream, dis_style_assembler_directive, ".dword");
1119 (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1120 (*info->fprintf_styled_func)
1121 (info->stream, dis_style_immediate, "0x%016llx",
1122 (unsigned long long) data);
1123 break;
1124 default:
1125 abort ();
1126 }
1127 return info->bytes_per_chunk;
1128 }
1129
1130 static bool
1131 riscv_init_disasm_info (struct disassemble_info *info)
1132 {
1133 int i;
1134
1135 struct riscv_private_data *pd =
1136 xcalloc (1, sizeof (struct riscv_private_data));
1137 pd->gp = 0;
1138 pd->print_addr = 0;
1139 for (i = 0; i < (int) ARRAY_SIZE (pd->hi_addr); i++)
1140 pd->hi_addr[i] = -1;
1141 pd->to_print_addr = false;
1142 pd->has_gp = false;
1143
1144 for (i = 0; i < info->symtab_size; i++)
1145 {
1146 asymbol *sym = info->symtab[i];
1147 if (strcmp (bfd_asymbol_name (sym), RISCV_GP_SYMBOL) == 0)
1148 {
1149 pd->gp = bfd_asymbol_value (sym);
1150 pd->has_gp = true;
1151 }
1152 }
1153
1154 info->private_data = pd;
1155 return true;
1156 }
1157
1158 int
1159 print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
1160 {
1161 bfd_byte packet[RISCV_MAX_INSN_LEN];
1162 insn_t insn = 0;
1163 bfd_vma dump_size;
1164 int status;
1165 enum riscv_seg_mstate mstate;
1166 int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *,
1167 struct disassemble_info *);
1168
1169 if (info->disassembler_options != NULL)
1170 {
1171 parse_riscv_dis_options (info->disassembler_options);
1172 /* Avoid repeatedly parsing the options. */
1173 info->disassembler_options = NULL;
1174 }
1175 else if (riscv_gpr_names == NULL)
1176 set_default_riscv_dis_options ();
1177
1178 if (info->private_data == NULL && !riscv_init_disasm_info (info))
1179 return -1;
1180
1181 mstate = riscv_search_mapping_symbol (memaddr, info);
1182 /* Save the last mapping state. */
1183 last_map_state = mstate;
1184
1185 /* Set the size to dump. */
1186 if (mstate == MAP_DATA
1187 && (info->flags & DISASSEMBLE_DATA) == 0)
1188 {
1189 dump_size = riscv_data_length (memaddr, info);
1190 info->bytes_per_chunk = dump_size;
1191 riscv_disassembler = riscv_disassemble_data;
1192 }
1193 else
1194 {
1195 /* Get the first 2-bytes to check the lenghth of instruction. */
1196 status = (*info->read_memory_func) (memaddr, packet, 2, info);
1197 if (status != 0)
1198 {
1199 (*info->memory_error_func) (status, memaddr, info);
1200 return -1;
1201 }
1202 insn = (insn_t) bfd_getl16 (packet);
1203 dump_size = riscv_insn_length (insn);
1204 riscv_disassembler = riscv_disassemble_insn;
1205 }
1206
1207 /* Fetch the instruction to dump. */
1208 status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
1209 if (status != 0)
1210 {
1211 (*info->memory_error_func) (status, memaddr, info);
1212 return -1;
1213 }
1214 insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
1215
1216 return (*riscv_disassembler) (memaddr, insn, packet, info);
1217 }
1218
1219 disassembler_ftype
1220 riscv_get_disassembler (bfd *abfd)
1221 {
1222 const char *default_arch = "rv64gc";
1223
1224 if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1225 {
1226 const char *sec_name = get_elf_backend_data (abfd)->obj_attrs_section;
1227 if (bfd_get_section_by_name (abfd, sec_name) != NULL)
1228 {
1229 obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
1230 unsigned int Tag_a = Tag_RISCV_priv_spec;
1231 unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
1232 unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
1233 riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i,
1234 attr[Tag_b].i,
1235 attr[Tag_c].i,
1236 &default_priv_spec);
1237 default_arch = attr[Tag_RISCV_arch].s;
1238 }
1239 }
1240
1241 riscv_release_subset_list (&riscv_subsets);
1242 riscv_parse_subset (&riscv_rps_dis, default_arch);
1243 return print_insn_riscv;
1244 }
1245
1246 /* Prevent use of the fake labels that are generated as part of the DWARF
1247 and for relaxable relocations in the assembler. */
1248
1249 bool
1250 riscv_symbol_is_valid (asymbol * sym,
1251 struct disassemble_info * info ATTRIBUTE_UNUSED)
1252 {
1253 const char * name;
1254
1255 if (sym == NULL)
1256 return false;
1257
1258 name = bfd_asymbol_name (sym);
1259
1260 return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
1261 && !riscv_elf_is_mapping_symbols (name));
1262 }
1263 \f
1264
1265 /* Indices into option argument vector for options accepting an argument.
1266 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1267
1268 typedef enum
1269 {
1270 RISCV_OPTION_ARG_NONE = -1,
1271 RISCV_OPTION_ARG_PRIV_SPEC,
1272
1273 RISCV_OPTION_ARG_COUNT
1274 } riscv_option_arg_t;
1275
1276 /* Valid RISCV disassembler options. */
1277
1278 static struct
1279 {
1280 const char *name;
1281 const char *description;
1282 riscv_option_arg_t arg;
1283 } riscv_options[] =
1284 {
1285 { "numeric",
1286 N_("Print numeric register names, rather than ABI names."),
1287 RISCV_OPTION_ARG_NONE },
1288 { "no-aliases",
1289 N_("Disassemble only into canonical instructions."),
1290 RISCV_OPTION_ARG_NONE },
1291 { "priv-spec=",
1292 N_("Print the CSR according to the chosen privilege spec."),
1293 RISCV_OPTION_ARG_PRIV_SPEC }
1294 };
1295
1296 /* Build the structure representing valid RISCV disassembler options.
1297 This is done dynamically for maintenance ease purpose; a static
1298 initializer would be unreadable. */
1299
1300 const disasm_options_and_args_t *
1301 disassembler_options_riscv (void)
1302 {
1303 static disasm_options_and_args_t *opts_and_args;
1304
1305 if (opts_and_args == NULL)
1306 {
1307 size_t num_options = ARRAY_SIZE (riscv_options);
1308 size_t num_args = RISCV_OPTION_ARG_COUNT;
1309 disasm_option_arg_t *args;
1310 disasm_options_t *opts;
1311 size_t i, priv_spec_count;
1312
1313 args = XNEWVEC (disasm_option_arg_t, num_args + 1);
1314
1315 args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC";
1316 priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_CLASS_NONE - 1;
1317 args[RISCV_OPTION_ARG_PRIV_SPEC].values
1318 = XNEWVEC (const char *, priv_spec_count + 1);
1319 for (i = 0; i < priv_spec_count; i++)
1320 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i]
1321 = riscv_priv_specs[i].name;
1322 /* The array we return must be NULL terminated. */
1323 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL;
1324
1325 /* The array we return must be NULL terminated. */
1326 args[num_args].name = NULL;
1327 args[num_args].values = NULL;
1328
1329 opts_and_args = XNEW (disasm_options_and_args_t);
1330 opts_and_args->args = args;
1331
1332 opts = &opts_and_args->options;
1333 opts->name = XNEWVEC (const char *, num_options + 1);
1334 opts->description = XNEWVEC (const char *, num_options + 1);
1335 opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1);
1336 for (i = 0; i < num_options; i++)
1337 {
1338 opts->name[i] = riscv_options[i].name;
1339 opts->description[i] = _(riscv_options[i].description);
1340 if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE)
1341 opts->arg[i] = &args[riscv_options[i].arg];
1342 else
1343 opts->arg[i] = NULL;
1344 }
1345 /* The array we return must be NULL terminated. */
1346 opts->name[i] = NULL;
1347 opts->description[i] = NULL;
1348 opts->arg[i] = NULL;
1349 }
1350
1351 return opts_and_args;
1352 }
1353
1354 void
1355 print_riscv_disassembler_options (FILE *stream)
1356 {
1357 const disasm_options_and_args_t *opts_and_args;
1358 const disasm_option_arg_t *args;
1359 const disasm_options_t *opts;
1360 size_t max_len = 0;
1361 size_t i;
1362 size_t j;
1363
1364 opts_and_args = disassembler_options_riscv ();
1365 opts = &opts_and_args->options;
1366 args = opts_and_args->args;
1367
1368 fprintf (stream, _("\n\
1369 The following RISC-V specific disassembler options are supported for use\n\
1370 with the -M switch (multiple options should be separated by commas):\n"));
1371 fprintf (stream, "\n");
1372
1373 /* Compute the length of the longest option name. */
1374 for (i = 0; opts->name[i] != NULL; i++)
1375 {
1376 size_t len = strlen (opts->name[i]);
1377
1378 if (opts->arg[i] != NULL)
1379 len += strlen (opts->arg[i]->name);
1380 if (max_len < len)
1381 max_len = len;
1382 }
1383
1384 for (i = 0, max_len++; opts->name[i] != NULL; i++)
1385 {
1386 fprintf (stream, " %s", opts->name[i]);
1387 if (opts->arg[i] != NULL)
1388 fprintf (stream, "%s", opts->arg[i]->name);
1389 if (opts->description[i] != NULL)
1390 {
1391 size_t len = strlen (opts->name[i]);
1392
1393 if (opts->arg != NULL && opts->arg[i] != NULL)
1394 len += strlen (opts->arg[i]->name);
1395 fprintf (stream, "%*c %s", (int) (max_len - len), ' ',
1396 opts->description[i]);
1397 }
1398 fprintf (stream, "\n");
1399 }
1400
1401 for (i = 0; args[i].name != NULL; i++)
1402 {
1403 if (args[i].values == NULL)
1404 continue;
1405 fprintf (stream, _("\n\
1406 For the options above, the following values are supported for \"%s\":\n "),
1407 args[i].name);
1408 for (j = 0; args[i].values[j] != NULL; j++)
1409 fprintf (stream, " %s", args[i].values[j]);
1410 fprintf (stream, _("\n"));
1411 }
1412
1413 fprintf (stream, _("\n"));
1414 }
1415
1416 void disassemble_free_riscv (struct disassemble_info *info ATTRIBUTE_UNUSED)
1417 {
1418 riscv_release_subset_list (&riscv_subsets);
1419 }