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