2 Copyright (C) 2011-2023 Free Software Foundation, Inc.
4 Contributed by Andrew Waterman (andrew@sifive.com).
7 This file is part of the GNU opcodes library.
9 This library is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 It is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
17 License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; see the file COPYING3. If not,
21 see <http://www.gnu.org/licenses/>. */
24 #include "disassemble.h"
25 #include "libiberty.h"
26 #include "opcode/riscv.h"
29 #include "elf/riscv.h"
30 #include "elfxx-riscv.h"
35 /* Current XLEN for the disassembler. */
36 static unsigned xlen
= 0;
38 /* Default ISA specification version (constant as of now). */
39 static enum riscv_spec_class default_isa_spec
= ISA_SPEC_CLASS_DRAFT
- 1;
41 /* Default privileged specification
42 (as specified by the ELF attributes or the `priv-spec' option). */
43 static enum riscv_spec_class default_priv_spec
= PRIV_SPEC_CLASS_NONE
;
45 static riscv_subset_list_t riscv_subsets
;
46 static riscv_parse_subset_t riscv_rps_dis
=
48 &riscv_subsets
, /* subset_list. */
49 opcodes_error_handler
,/* error_handler. */
51 &default_isa_spec
, /* isa_spec. */
52 false, /* check_unknown_prefixed_ext. */
55 struct riscv_private_data
59 bfd_vma hi_addr
[OP_MASK_RD
+ 1];
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
;
71 /* Register names as used by the disassembler. */
72 static const char (*riscv_gpr_names
)[NRC
];
73 static const char (*riscv_fpr_names
)[NRC
];
75 /* If set, disassemble as most general instruction. */
76 static bool no_aliases
= false;
79 /* Set default RISC-V disassembler options. */
82 set_default_riscv_dis_options (void)
84 riscv_gpr_names
= riscv_gpr_names_abi
;
85 riscv_fpr_names
= riscv_fpr_names_abi
;
89 /* Parse RISC-V disassembler option (without arguments). */
92 parse_riscv_dis_option_without_args (const char *option
)
94 if (strcmp (option
, "no-aliases") == 0)
96 else if (strcmp (option
, "numeric") == 0)
98 riscv_gpr_names
= riscv_gpr_names_numeric
;
99 riscv_fpr_names
= riscv_fpr_names_numeric
;
106 /* Parse RISC-V disassembler option (possibly with arguments). */
109 parse_riscv_dis_option (const char *option
)
113 if (parse_riscv_dis_option_without_args (option
))
116 equal
= strchr (option
, '=');
119 /* The option without '=' should be defined above. */
120 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
124 || *(equal
+ 1) == '\0')
126 /* Invalid options with '=', no option name before '=',
127 and no value after '='. */
128 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
135 if (strcmp (option
, "priv-spec") == 0)
137 enum riscv_spec_class priv_spec
= PRIV_SPEC_CLASS_NONE
;
138 const char *name
= NULL
;
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"),
144 else if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
145 default_priv_spec
= priv_spec
;
146 else if (default_priv_spec
!= priv_spec
)
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
);
156 /* xgettext:c-format */
157 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
161 /* Parse RISC-V disassembler options. */
164 parse_riscv_dis_options (const char *opts_in
)
166 char *opts
= xstrdup (opts_in
), *opt
= opts
, *opt_end
= opts
;
168 set_default_riscv_dis_options ();
170 for ( ; opt_end
!= NULL
; opt
= opt_end
+ 1)
172 if ((opt_end
= strchr (opt
, ',')) != NULL
)
174 parse_riscv_dis_option (opt
);
180 /* Print one argument from an array. */
183 arg_print (struct disassemble_info
*info
, unsigned long val
,
184 const char* const* array
, size_t size
)
186 const char *s
= val
>= size
|| array
[val
] == NULL
? "unknown" : array
[val
];
187 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "%s", s
);
190 /* If we need to print an address, set its value and state. */
193 maybe_print_address (struct riscv_private_data
*pd
, int base_reg
, int offset
,
196 if (pd
->hi_addr
[base_reg
] != (bfd_vma
)-1)
198 pd
->print_addr
= (base_reg
!= 0 ? pd
->hi_addr
[base_reg
] : 0) + offset
;
199 pd
->hi_addr
[base_reg
] = -1;
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
;
206 return; /* Don't print the address. */
207 pd
->to_print_addr
= true;
209 /* Sign-extend a 32-bit value to a 64-bit value. */
211 pd
->print_addr
= (bfd_vma
)(int32_t) pd
->print_addr
;
213 /* Fit into a 32-bit value on RV32. */
215 pd
->print_addr
= (bfd_vma
)(uint32_t)pd
->print_addr
;
218 /* Print insn arguments for 32/64-bit code. */
221 print_insn_args (const char *oparg
, insn_t l
, bfd_vma pc
, disassemble_info
*info
)
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
;
230 print (info
->stream
, dis_style_text
, "\t");
232 for (; *oparg
!= '\0'; oparg
++)
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]);
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]);
250 case 'U': /* RS1, constrained to equal RD. */
251 print (info
->stream
, dis_style_register
,
252 "%s", riscv_gpr_names
[rd
]);
254 case 'c': /* RS1, constrained to equal sp. */
255 print (info
->stream
, dis_style_register
, "%s",
256 riscv_gpr_names
[X_SP
]);
259 print (info
->stream
, dis_style_register
, "%s",
260 riscv_gpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
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
));
273 print (info
->stream
, dis_style_address_offset
, "%d",
274 (int)EXTRACT_CLTYPE_LW_IMM (l
));
277 print (info
->stream
, dis_style_address_offset
, "%d",
278 (int)EXTRACT_CLTYPE_LD_IMM (l
));
281 print (info
->stream
, dis_style_address_offset
, "%d",
282 (int)EXTRACT_CITYPE_LWSP_IMM (l
));
285 print (info
->stream
, dis_style_address_offset
, "%d",
286 (int)EXTRACT_CITYPE_LDSP_IMM (l
));
289 print (info
->stream
, dis_style_immediate
, "%d",
290 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l
));
293 print (info
->stream
, dis_style_immediate
, "%d",
294 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l
));
297 print (info
->stream
, dis_style_address_offset
, "%d",
298 (int)EXTRACT_CSSTYPE_SWSP_IMM (l
));
301 print (info
->stream
, dis_style_address_offset
, "%d",
302 (int)EXTRACT_CSSTYPE_SDSP_IMM (l
));
305 info
->target
= EXTRACT_CBTYPE_IMM (l
) + pc
;
306 (*info
->print_address_func
) (info
->target
, info
);
309 info
->target
= EXTRACT_CJTYPE_IMM (l
) + pc
;
310 (*info
->print_address_func
) (info
->target
, info
);
313 print (info
->stream
, dis_style_immediate
, "0x%x",
314 (unsigned)(EXTRACT_CITYPE_IMM (l
) & (RISCV_BIGIMM_REACH
-1)));
317 print (info
->stream
, dis_style_immediate
, "0x%x",
318 (unsigned)EXTRACT_CITYPE_IMM (l
) & 0x3f);
321 print (info
->stream
, dis_style_immediate
, "0x%x",
322 (unsigned)EXTRACT_CITYPE_IMM (l
) & 0x1f);
324 case 'T': /* Floating-point RS2. */
325 print (info
->stream
, dis_style_register
, "%s",
326 riscv_fpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
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]);
340 print (info
->stream
, dis_style_register
, "%s",
341 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
344 if (!EXTRACT_OPERAND (VWD
, l
))
345 print (info
->stream
, dis_style_register
, "%s",
348 print (info
->stream
, dis_style_register
, "%s",
349 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
352 print (info
->stream
, dis_style_register
, "%s",
353 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS1
, l
)]);
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
)]);
362 print (info
->stream
, dis_style_register
, "%s",
363 riscv_vecr_names_numeric
[0]);
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);
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
)
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
],
388 print (info
->stream
, dis_style_immediate
, "%d", imm
);
392 print (info
->stream
, dis_style_immediate
, "%d",
393 (int)EXTRACT_RVV_VI_IMM (l
));
396 print (info
->stream
, dis_style_immediate
, "%d",
397 (int)EXTRACT_RVV_VI_UIMM (l
));
400 print (info
->stream
, dis_style_immediate
, "%d",
401 (int)EXTRACT_RVV_OFFSET (l
));
404 print (info
->stream
, dis_style_immediate
, "%d",
405 (int)EXTRACT_RVV_VI_UIMM6 (l
));
408 if (!EXTRACT_OPERAND (VMASK
, l
))
410 print (info
->stream
, dis_style_text
, ",");
411 print (info
->stream
, dis_style_register
, "%s",
412 riscv_vecm_names_numeric
[0]);
423 print (info
->stream
, dis_style_text
, "%c", *oparg
);
427 /* Only print constant 0 if it is the last argument. */
429 print (info
->stream
, dis_style_immediate
, "0");
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
]);
439 print (info
->stream
, dis_style_register
, "%s",
440 riscv_gpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
444 print (info
->stream
, dis_style_immediate
, "0x%x",
445 (unsigned)EXTRACT_UTYPE_IMM (l
) >> RISCV_IMM_BITS
);
449 arg_print (info
, EXTRACT_OPERAND (RM
, l
),
450 riscv_rm
, ARRAY_SIZE (riscv_rm
));
454 arg_print (info
, EXTRACT_OPERAND (PRED
, l
),
455 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
459 arg_print (info
, EXTRACT_OPERAND (SUCC
, l
),
460 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
464 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
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
));
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
));
484 info
->target
= EXTRACT_JTYPE_IMM (l
) + pc
;
485 (*info
->print_address_func
) (info
->target
, info
);
489 info
->target
= EXTRACT_BTYPE_IMM (l
) + pc
;
490 (*info
->print_address_func
) (info
->target
, info
);
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
]);
504 print (info
->stream
, dis_style_immediate
, "0x%x",
505 EXTRACT_OPERAND (BS
, l
));
509 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[0]);
513 print (info
->stream
, dis_style_immediate
, "0x%x",
514 EXTRACT_OPERAND (SHAMT
, l
));
518 print (info
->stream
, dis_style_immediate
, "0x%x",
519 EXTRACT_OPERAND (SHAMTW
, l
));
524 print (info
->stream
, dis_style_register
, "%s", riscv_fpr_names
[rs1
]);
528 print (info
->stream
, dis_style_register
, "%s",
529 riscv_fpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
533 print (info
->stream
, dis_style_register
, "%s", riscv_fpr_names
[rd
]);
537 print (info
->stream
, dis_style_register
, "%s",
538 riscv_fpr_names
[EXTRACT_OPERAND (RS3
, l
)]);
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
);
550 for (i
= 0; i
< 4096; i
++)
551 riscv_csr_hash
[i
] = NULL
;
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;
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"
570 if (riscv_csr_hash
[csr
] != NULL
)
571 if (riscv_subset_supports (&riscv_rps_dis
, "xtheadvector")
572 && (csr
== CSR_VSTART
577 || csr
== CSR_VLENB
))
578 print (info
->stream
, dis_style_register
, "%s",
579 concat ("th.", riscv_csr_hash
[csr
], NULL
));
581 print (info
->stream
, dis_style_register
, "%s",
582 riscv_csr_hash
[csr
]);
584 print (info
->stream
, dis_style_immediate
, "0x%x", csr
);
589 print (info
->stream
, dis_style_immediate
, "0x%x",
590 EXTRACT_OPERAND (RNUM
, l
));
594 print (info
->stream
, dis_style_immediate
, "%d", rs1
);
597 case 'W': /* Various operands for standard z extensions. */
604 print (info
->stream
, dis_style_address_offset
, "%d",
605 (int) EXTRACT_STYPE_IMM (l
));
608 goto undefined_modifier
;
615 if (riscv_fli_symval
[rs1
])
616 print (info
->stream
, dis_style_text
, "%s",
617 riscv_fli_symval
[rs1
]);
619 print (info
->stream
, dis_style_immediate
, "%a",
620 riscv_fli_numval
[rs1
]);
623 goto undefined_modifier
;
626 case 'c': /* Zcb extension 16 bits length instruction fields. */
630 print (info
->stream
, dis_style_immediate
, "%d",
631 (int)EXTRACT_ZCB_BYTE_UIMM (l
));
634 print (info
->stream
, dis_style_immediate
, "%d",
635 (int)EXTRACT_ZCB_HALFWORD_UIMM (l
));
638 goto undefined_modifier
;
642 goto undefined_modifier
;
646 case 'X': /* Vendor-specific operands. */
649 case 't': /* Vendor-specific (T-head) operands. */
656 case 'l': /* Integer immediate, literal. */
658 while (*oparg
&& *oparg
!= ',')
660 print (info
->stream
, dis_style_immediate
, "%c", *oparg
);
665 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
668 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
672 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
674 goto undefined_modifier
;
675 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
679 print (info
->stream
, dis_style_immediate
, "%lu",
680 (unsigned long)EXTRACT_U_IMM (n
, s
, l
));
682 print (info
->stream
, dis_style_immediate
, "%li",
683 (signed long)EXTRACT_S_IMM (n
, s
, l
));
686 goto undefined_modifier
;
690 case 'c': /* Vendor-specific (CORE-V) operands. */
694 print (info
->stream
, dis_style_immediate
, "%d",
695 ((int) EXTRACT_CV_IS2_UIMM5 (l
)));
698 print (info
->stream
, dis_style_immediate
, "%d",
699 ((int) EXTRACT_CV_IS3_UIMM5 (l
)));
702 goto undefined_modifier
;
706 goto undefined_modifier
;
712 /* xgettext:c-format */
713 print (info
->stream
, dis_style_text
,
714 _("# internal error, undefined modifier (%c)"),
721 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
722 on using INFO. Returns length of the instruction, in bytes.
723 BIGENDIAN must be 1 if this is big-endian code, 0 if
724 this is little-endian code. */
727 riscv_disassemble_insn (bfd_vma memaddr
,
729 const bfd_byte
*packet
,
730 disassemble_info
*info
)
732 const struct riscv_opcode
*op
;
733 static bool init
= false;
734 static const struct riscv_opcode
*riscv_hash
[OP_MASK_OP
+ 1];
735 struct riscv_private_data
*pd
= info
->private_data
;
739 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
741 /* Build a hash table to shorten the search time. */
744 for (op
= riscv_opcodes
; op
->name
; op
++)
745 if (!riscv_hash
[OP_HASH_IDX (op
->match
)])
746 riscv_hash
[OP_HASH_IDX (op
->match
)] = op
;
751 insnlen
= riscv_insn_length (word
);
753 /* RISC-V instructions are always little-endian. */
754 info
->endian_code
= BFD_ENDIAN_LITTLE
;
756 info
->bytes_per_chunk
= insnlen
% 4 == 0 ? 4 : 2;
757 info
->bytes_per_line
= 8;
758 /* We don't support constant pools, so this must be code. */
759 info
->display_endian
= info
->endian_code
;
760 info
->insn_info_valid
= 1;
761 info
->branch_delay_insns
= 0;
763 info
->insn_type
= dis_nonbranch
;
767 op
= riscv_hash
[OP_HASH_IDX (word
)];
770 /* If XLEN is not known, get its value from the ELF class. */
771 if (info
->mach
== bfd_mach_riscv64
)
773 else if (info
->mach
== bfd_mach_riscv32
)
775 else if (info
->section
!= NULL
)
777 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (info
->section
->owner
);
778 xlen
= ehdr
->e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
781 /* If arch has the Zfinx extension, replace FPR with GPR. */
782 if (riscv_subset_supports (&riscv_rps_dis
, "zfinx"))
783 riscv_fpr_names
= riscv_gpr_names
;
785 riscv_fpr_names
= riscv_gpr_names
== riscv_gpr_names_abi
?
786 riscv_fpr_names_abi
: riscv_fpr_names_numeric
;
788 for (; op
->name
; op
++)
790 /* Ignore macro insns. */
791 if (op
->pinfo
== INSN_MACRO
)
793 /* Does the opcode match? */
794 if (! (op
->match_func
) (op
, word
))
796 /* Is this a pseudo-instruction and may we print it as such? */
797 if (no_aliases
&& (op
->pinfo
& INSN_ALIAS
))
799 /* Is this instruction restricted to a certain value of XLEN? */
800 if ((op
->xlen_requirement
!= 0) && (op
->xlen_requirement
!= xlen
))
802 /* Is this instruction supported by the current architecture? */
803 if (!riscv_multi_subset_supports (&riscv_rps_dis
, op
->insn_class
))
807 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_mnemonic
,
809 print_insn_args (op
->args
, word
, memaddr
, info
);
811 /* Try to disassemble multi-instruction addressing sequences. */
812 if (pd
->to_print_addr
)
814 info
->target
= pd
->print_addr
;
815 (*info
->fprintf_styled_func
)
816 (info
->stream
, dis_style_comment_start
, " # ");
817 (*info
->print_address_func
) (info
->target
, info
);
818 pd
->to_print_addr
= false;
821 /* Finish filling out insn_info fields. */
822 switch (op
->pinfo
& INSN_TYPE
)
825 info
->insn_type
= dis_branch
;
827 case INSN_CONDBRANCH
:
828 info
->insn_type
= dis_condbranch
;
831 info
->insn_type
= dis_jsr
;
834 info
->insn_type
= dis_dref
;
840 if (op
->pinfo
& INSN_DATA_SIZE
)
842 int size
= ((op
->pinfo
& INSN_DATA_SIZE
)
843 >> INSN_DATA_SIZE_SHIFT
);
844 info
->data_size
= 1 << (size
- 1);
851 /* We did not find a match, so just print the instruction bits in
852 the shape of an assembler .insn directive. */
853 info
->insn_type
= dis_noninsn
;
854 (*info
->fprintf_styled_func
)
855 (info
->stream
, dis_style_assembler_directive
, ".insn");
856 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
857 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
859 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, ", ");
860 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
, "0x");
861 for (i
= insnlen
, printed
= false; i
>= 2; )
864 word
= bfd_get_bits (packet
+ i
, 16, false);
865 if (!word
&& !printed
)
868 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
869 "%04x", (unsigned int) word
);
876 /* Return true if we find the suitable mapping symbol,
877 and also update the STATE. Otherwise, return false. */
880 riscv_get_map_state (int n
,
881 enum riscv_seg_mstate
*state
,
882 struct disassemble_info
*info
)
886 /* If the symbol is in a different section, ignore it. */
887 if (info
->section
!= NULL
888 && info
->section
!= info
->symtab
[n
]->section
)
891 name
= bfd_asymbol_name(info
->symtab
[n
]);
892 if (strcmp (name
, "$x") == 0)
894 else if (strcmp (name
, "$d") == 0)
896 else if (strncmp (name
, "$xrv", 4) == 0)
899 riscv_release_subset_list (&riscv_subsets
);
901 /* ISA mapping string may be numbered, suffixed with '.n'. Do not
902 consider this as part of the ISA string. */
903 char *suffix
= strchr (name
, '.');
906 int suffix_index
= (int)(suffix
- name
);
907 char *name_substr
= xmalloc (suffix_index
+ 1);
908 strncpy (name_substr
, name
, suffix_index
);
909 name_substr
[suffix_index
] = '\0';
910 riscv_parse_subset (&riscv_rps_dis
, name_substr
+ 2);
914 riscv_parse_subset (&riscv_rps_dis
, name
+ 2);
922 /* Check the sorted symbol table (sorted by the symbol value), find the
923 suitable mapping symbols. */
925 static enum riscv_seg_mstate
926 riscv_search_mapping_symbol (bfd_vma memaddr
,
927 struct disassemble_info
*info
)
929 enum riscv_seg_mstate mstate
;
930 bool from_last_map_symbol
;
935 /* Return the last map state if the address is still within the range of the
936 last mapping symbol. */
937 if (last_map_section
== info
->section
938 && (memaddr
< last_map_symbol_boundary
))
939 return last_map_state
;
941 last_map_section
= info
->section
;
943 /* Decide whether to print the data or instruction by default, in case
944 we can not find the corresponding mapping symbols. */
947 && info
->section
->flags
& SEC_CODE
)
951 if (info
->symtab_size
== 0
952 || bfd_asymbol_flavour (*info
->symtab
) != bfd_target_elf_flavour
)
955 /* Reset the last_map_symbol if we start to dump a new section. */
957 last_map_symbol
= -1;
959 /* If the last stop offset is different from the current one, then
960 don't use the last_map_symbol to search. We usually reset the
961 info->stop_offset when handling a new section. */
962 from_last_map_symbol
= (last_map_symbol
>= 0
963 && info
->stop_offset
== last_stop_offset
);
965 /* Start scanning at the start of the function, or wherever
966 we finished last time. */
967 n
= info
->symtab_pos
+ 1;
968 if (from_last_map_symbol
&& n
>= last_map_symbol
)
971 /* Find the suitable mapping symbol to dump. */
972 for (; n
< info
->symtab_size
; n
++)
974 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
975 /* We have searched all possible symbols in the range. */
978 if (riscv_get_map_state (n
, &mstate
, info
))
982 /* Do not stop searching, in case there are some mapping
983 symbols have the same value, but have different names.
988 /* We can not find the suitable mapping symbol above. Therefore, we
989 look forwards and try to find it again, but don't go past the start
990 of the section. Otherwise a data section without mapping symbols
991 can pick up a text mapping symbol of a preceeding section. */
994 n
= info
->symtab_pos
;
995 if (from_last_map_symbol
&& n
>= last_map_symbol
)
1000 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1001 /* We have searched all possible symbols in the range. */
1002 if (addr
< (info
->section
? info
->section
->vma
: 0))
1004 /* Stop searching once we find the closed mapping symbol. */
1005 if (riscv_get_map_state (n
, &mstate
, info
))
1016 /* Find the next mapping symbol to determine the boundary of this mapping
1019 bool found_next
= false;
1020 /* Try to found next mapping symbol. */
1021 for (n
= symbol
+ 1; n
< info
->symtab_size
; n
++)
1023 if (info
->symtab
[symbol
]->section
!= info
->symtab
[n
]->section
)
1026 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1027 const char *sym_name
= bfd_asymbol_name(info
->symtab
[n
]);
1028 if (sym_name
[0] == '$' && (sym_name
[1] == 'x' || sym_name
[1] == 'd'))
1030 /* The next mapping symbol has been found, and it represents the
1031 boundary of this mapping symbol. */
1033 last_map_symbol_boundary
= addr
;
1038 /* No further mapping symbol has been found, indicating that the boundary
1039 of the current mapping symbol is the end of this section. */
1041 last_map_symbol_boundary
= info
->section
->vma
+ info
->section
->size
;
1044 /* Save the information for next use. */
1045 last_map_symbol
= symbol
;
1046 last_stop_offset
= info
->stop_offset
;
1051 /* Decide which data size we should print. */
1054 riscv_data_length (bfd_vma memaddr
,
1055 disassemble_info
*info
)
1061 if (info
->symtab_size
!= 0
1062 && bfd_asymbol_flavour (*info
->symtab
) == bfd_target_elf_flavour
1063 && last_map_symbol
>= 0)
1066 enum riscv_seg_mstate m
= MAP_NONE
;
1067 for (n
= last_map_symbol
+ 1; n
< info
->symtab_size
; n
++)
1069 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1071 && riscv_get_map_state (n
, &m
, info
))
1073 if (addr
- memaddr
< length
)
1074 length
= addr
- memaddr
;
1082 /* Do not set the length which exceeds the section size. */
1083 bfd_vma offset
= info
->section
->vma
+ info
->section
->size
;
1085 length
= (offset
< length
) ? offset
: length
;
1087 length
= length
== 3 ? 2 : length
;
1091 /* Dump the data contents. */
1094 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED
,
1096 const bfd_byte
*packet ATTRIBUTE_UNUSED
,
1097 disassemble_info
*info
)
1099 info
->display_endian
= info
->endian
;
1101 switch (info
->bytes_per_chunk
)
1104 info
->bytes_per_line
= 6;
1105 (*info
->fprintf_styled_func
)
1106 (info
->stream
, dis_style_assembler_directive
, ".byte");
1107 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1108 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1109 "0x%02x", (unsigned)data
);
1112 info
->bytes_per_line
= 8;
1113 (*info
->fprintf_styled_func
)
1114 (info
->stream
, dis_style_assembler_directive
, ".short");
1115 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1116 (*info
->fprintf_styled_func
)
1117 (info
->stream
, dis_style_immediate
, "0x%04x", (unsigned) data
);
1120 info
->bytes_per_line
= 8;
1121 (*info
->fprintf_styled_func
)
1122 (info
->stream
, dis_style_assembler_directive
, ".word");
1123 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1124 (*info
->fprintf_styled_func
)
1125 (info
->stream
, dis_style_immediate
, "0x%08lx",
1126 (unsigned long) data
);
1129 info
->bytes_per_line
= 8;
1130 (*info
->fprintf_styled_func
)
1131 (info
->stream
, dis_style_assembler_directive
, ".dword");
1132 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1133 (*info
->fprintf_styled_func
)
1134 (info
->stream
, dis_style_immediate
, "0x%016llx",
1135 (unsigned long long) data
);
1140 return info
->bytes_per_chunk
;
1144 riscv_init_disasm_info (struct disassemble_info
*info
)
1148 struct riscv_private_data
*pd
=
1149 xcalloc (1, sizeof (struct riscv_private_data
));
1152 for (i
= 0; i
< (int) ARRAY_SIZE (pd
->hi_addr
); i
++)
1153 pd
->hi_addr
[i
] = -1;
1154 pd
->to_print_addr
= false;
1157 for (i
= 0; i
< info
->symtab_size
; i
++)
1159 asymbol
*sym
= info
->symtab
[i
];
1160 if (strcmp (bfd_asymbol_name (sym
), RISCV_GP_SYMBOL
) == 0)
1162 pd
->gp
= bfd_asymbol_value (sym
);
1167 info
->private_data
= pd
;
1172 print_insn_riscv (bfd_vma memaddr
, struct disassemble_info
*info
)
1174 bfd_byte packet
[RISCV_MAX_INSN_LEN
];
1178 enum riscv_seg_mstate mstate
;
1179 int (*riscv_disassembler
) (bfd_vma
, insn_t
, const bfd_byte
*,
1180 struct disassemble_info
*);
1182 if (info
->disassembler_options
!= NULL
)
1184 parse_riscv_dis_options (info
->disassembler_options
);
1185 /* Avoid repeatedly parsing the options. */
1186 info
->disassembler_options
= NULL
;
1188 else if (riscv_gpr_names
== NULL
)
1189 set_default_riscv_dis_options ();
1191 if (info
->private_data
== NULL
&& !riscv_init_disasm_info (info
))
1194 mstate
= riscv_search_mapping_symbol (memaddr
, info
);
1195 /* Save the last mapping state. */
1196 last_map_state
= mstate
;
1198 /* Set the size to dump. */
1199 if (mstate
== MAP_DATA
1200 && (info
->flags
& DISASSEMBLE_DATA
) == 0)
1202 dump_size
= riscv_data_length (memaddr
, info
);
1203 info
->bytes_per_chunk
= dump_size
;
1204 riscv_disassembler
= riscv_disassemble_data
;
1208 /* Get the first 2-bytes to check the lenghth of instruction. */
1209 status
= (*info
->read_memory_func
) (memaddr
, packet
, 2, info
);
1212 (*info
->memory_error_func
) (status
, memaddr
, info
);
1215 insn
= (insn_t
) bfd_getl16 (packet
);
1216 dump_size
= riscv_insn_length (insn
);
1217 riscv_disassembler
= riscv_disassemble_insn
;
1220 /* Fetch the instruction to dump. */
1221 status
= (*info
->read_memory_func
) (memaddr
, packet
, dump_size
, info
);
1224 (*info
->memory_error_func
) (status
, memaddr
, info
);
1227 insn
= (insn_t
) bfd_get_bits (packet
, dump_size
* 8, false);
1229 return (*riscv_disassembler
) (memaddr
, insn
, packet
, info
);
1233 riscv_get_disassembler (bfd
*abfd
)
1235 const char *default_arch
= "rv64gc";
1237 if (abfd
&& bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1239 const char *sec_name
= get_elf_backend_data (abfd
)->obj_attrs_section
;
1240 if (bfd_get_section_by_name (abfd
, sec_name
) != NULL
)
1242 obj_attribute
*attr
= elf_known_obj_attributes_proc (abfd
);
1243 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
1244 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
1245 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
1246 riscv_get_priv_spec_class_from_numbers (attr
[Tag_a
].i
,
1249 &default_priv_spec
);
1250 default_arch
= attr
[Tag_RISCV_arch
].s
;
1254 riscv_release_subset_list (&riscv_subsets
);
1255 riscv_parse_subset (&riscv_rps_dis
, default_arch
);
1256 return print_insn_riscv
;
1259 /* Prevent use of the fake labels that are generated as part of the DWARF
1260 and for relaxable relocations in the assembler. */
1263 riscv_symbol_is_valid (asymbol
* sym
,
1264 struct disassemble_info
* info ATTRIBUTE_UNUSED
)
1271 name
= bfd_asymbol_name (sym
);
1273 return (strcmp (name
, RISCV_FAKE_LABEL_NAME
) != 0
1274 && !riscv_elf_is_mapping_symbols (name
));
1278 /* Indices into option argument vector for options accepting an argument.
1279 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1283 RISCV_OPTION_ARG_NONE
= -1,
1284 RISCV_OPTION_ARG_PRIV_SPEC
,
1286 RISCV_OPTION_ARG_COUNT
1287 } riscv_option_arg_t
;
1289 /* Valid RISCV disassembler options. */
1294 const char *description
;
1295 riscv_option_arg_t arg
;
1299 N_("Print numeric register names, rather than ABI names."),
1300 RISCV_OPTION_ARG_NONE
},
1302 N_("Disassemble only into canonical instructions."),
1303 RISCV_OPTION_ARG_NONE
},
1305 N_("Print the CSR according to the chosen privilege spec."),
1306 RISCV_OPTION_ARG_PRIV_SPEC
}
1309 /* Build the structure representing valid RISCV disassembler options.
1310 This is done dynamically for maintenance ease purpose; a static
1311 initializer would be unreadable. */
1313 const disasm_options_and_args_t
*
1314 disassembler_options_riscv (void)
1316 static disasm_options_and_args_t
*opts_and_args
;
1318 if (opts_and_args
== NULL
)
1320 size_t num_options
= ARRAY_SIZE (riscv_options
);
1321 size_t num_args
= RISCV_OPTION_ARG_COUNT
;
1322 disasm_option_arg_t
*args
;
1323 disasm_options_t
*opts
;
1324 size_t i
, priv_spec_count
;
1326 args
= XNEWVEC (disasm_option_arg_t
, num_args
+ 1);
1328 args
[RISCV_OPTION_ARG_PRIV_SPEC
].name
= "SPEC";
1329 priv_spec_count
= PRIV_SPEC_CLASS_DRAFT
- PRIV_SPEC_CLASS_NONE
- 1;
1330 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
1331 = XNEWVEC (const char *, priv_spec_count
+ 1);
1332 for (i
= 0; i
< priv_spec_count
; i
++)
1333 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
]
1334 = riscv_priv_specs
[i
].name
;
1335 /* The array we return must be NULL terminated. */
1336 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
] = NULL
;
1338 /* The array we return must be NULL terminated. */
1339 args
[num_args
].name
= NULL
;
1340 args
[num_args
].values
= NULL
;
1342 opts_and_args
= XNEW (disasm_options_and_args_t
);
1343 opts_and_args
->args
= args
;
1345 opts
= &opts_and_args
->options
;
1346 opts
->name
= XNEWVEC (const char *, num_options
+ 1);
1347 opts
->description
= XNEWVEC (const char *, num_options
+ 1);
1348 opts
->arg
= XNEWVEC (const disasm_option_arg_t
*, num_options
+ 1);
1349 for (i
= 0; i
< num_options
; i
++)
1351 opts
->name
[i
] = riscv_options
[i
].name
;
1352 opts
->description
[i
] = _(riscv_options
[i
].description
);
1353 if (riscv_options
[i
].arg
!= RISCV_OPTION_ARG_NONE
)
1354 opts
->arg
[i
] = &args
[riscv_options
[i
].arg
];
1356 opts
->arg
[i
] = NULL
;
1358 /* The array we return must be NULL terminated. */
1359 opts
->name
[i
] = NULL
;
1360 opts
->description
[i
] = NULL
;
1361 opts
->arg
[i
] = NULL
;
1364 return opts_and_args
;
1368 print_riscv_disassembler_options (FILE *stream
)
1370 const disasm_options_and_args_t
*opts_and_args
;
1371 const disasm_option_arg_t
*args
;
1372 const disasm_options_t
*opts
;
1377 opts_and_args
= disassembler_options_riscv ();
1378 opts
= &opts_and_args
->options
;
1379 args
= opts_and_args
->args
;
1381 fprintf (stream
, _("\n\
1382 The following RISC-V specific disassembler options are supported for use\n\
1383 with the -M switch (multiple options should be separated by commas):\n"));
1384 fprintf (stream
, "\n");
1386 /* Compute the length of the longest option name. */
1387 for (i
= 0; opts
->name
[i
] != NULL
; i
++)
1389 size_t len
= strlen (opts
->name
[i
]);
1391 if (opts
->arg
[i
] != NULL
)
1392 len
+= strlen (opts
->arg
[i
]->name
);
1397 for (i
= 0, max_len
++; opts
->name
[i
] != NULL
; i
++)
1399 fprintf (stream
, " %s", opts
->name
[i
]);
1400 if (opts
->arg
[i
] != NULL
)
1401 fprintf (stream
, "%s", opts
->arg
[i
]->name
);
1402 if (opts
->description
[i
] != NULL
)
1404 size_t len
= strlen (opts
->name
[i
]);
1406 if (opts
->arg
!= NULL
&& opts
->arg
[i
] != NULL
)
1407 len
+= strlen (opts
->arg
[i
]->name
);
1408 fprintf (stream
, "%*c %s", (int) (max_len
- len
), ' ',
1409 opts
->description
[i
]);
1411 fprintf (stream
, "\n");
1414 for (i
= 0; args
[i
].name
!= NULL
; i
++)
1416 if (args
[i
].values
== NULL
)
1418 fprintf (stream
, _("\n\
1419 For the options above, the following values are supported for \"%s\":\n "),
1421 for (j
= 0; args
[i
].values
[j
] != NULL
; j
++)
1422 fprintf (stream
, " %s", args
[i
].values
[j
]);
1423 fprintf (stream
, _("\n"));
1426 fprintf (stream
, _("\n"));
1429 void disassemble_free_riscv (struct disassemble_info
*info ATTRIBUTE_UNUSED
)
1431 riscv_release_subset_list (&riscv_subsets
);