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