2 Copyright (C) 2011-2022 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;
68 /* Register names as used by the disassembler. */
69 static const char * const *riscv_gpr_names
;
70 static const char * const *riscv_fpr_names
;
72 /* If set, disassemble as most general instruction. */
73 static bool no_aliases
= false;
76 /* Set default RISC-V disassembler options. */
79 set_default_riscv_dis_options (void)
81 riscv_gpr_names
= riscv_gpr_names_abi
;
82 riscv_fpr_names
= riscv_fpr_names_abi
;
86 /* Parse RISC-V disassembler option (without arguments). */
89 parse_riscv_dis_option_without_args (const char *option
)
91 if (strcmp (option
, "no-aliases") == 0)
93 else if (strcmp (option
, "numeric") == 0)
95 riscv_gpr_names
= riscv_gpr_names_numeric
;
96 riscv_fpr_names
= riscv_fpr_names_numeric
;
103 /* Parse RISC-V disassembler option (possibly with arguments). */
106 parse_riscv_dis_option (const char *option
)
110 if (parse_riscv_dis_option_without_args (option
))
113 equal
= strchr (option
, '=');
116 /* The option without '=' should be defined above. */
117 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
121 || *(equal
+ 1) == '\0')
123 /* Invalid options with '=', no option name before '=',
124 and no value after '='. */
125 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
132 if (strcmp (option
, "priv-spec") == 0)
134 enum riscv_spec_class priv_spec
= PRIV_SPEC_CLASS_NONE
;
135 const char *name
= NULL
;
137 RISCV_GET_PRIV_SPEC_CLASS (value
, priv_spec
);
138 if (priv_spec
== PRIV_SPEC_CLASS_NONE
)
139 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
141 else if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
142 default_priv_spec
= priv_spec
;
143 else if (default_priv_spec
!= priv_spec
)
145 RISCV_GET_PRIV_SPEC_NAME (name
, default_priv_spec
);
146 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
147 "the elf privilege attribute is %s"),
148 option
, value
, name
);
153 /* xgettext:c-format */
154 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
158 /* Parse RISC-V disassembler options. */
161 parse_riscv_dis_options (const char *opts_in
)
163 char *opts
= xstrdup (opts_in
), *opt
= opts
, *opt_end
= opts
;
165 set_default_riscv_dis_options ();
167 for ( ; opt_end
!= NULL
; opt
= opt_end
+ 1)
169 if ((opt_end
= strchr (opt
, ',')) != NULL
)
171 parse_riscv_dis_option (opt
);
177 /* Print one argument from an array. */
180 arg_print (struct disassemble_info
*info
, unsigned long val
,
181 const char* const* array
, size_t size
)
183 const char *s
= val
>= size
|| array
[val
] == NULL
? "unknown" : array
[val
];
184 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "%s", s
);
187 /* If we need to print an address, set its value and state. */
190 maybe_print_address (struct riscv_private_data
*pd
, int base_reg
, int offset
,
193 if (pd
->hi_addr
[base_reg
] != (bfd_vma
)-1)
195 pd
->print_addr
= (base_reg
!= 0 ? pd
->hi_addr
[base_reg
] : 0) + offset
;
196 pd
->hi_addr
[base_reg
] = -1;
198 else if (base_reg
== X_GP
&& pd
->has_gp
)
199 pd
->print_addr
= pd
->gp
+ offset
;
200 else if (base_reg
== X_TP
|| base_reg
== 0)
201 pd
->print_addr
= offset
;
203 return; /* Don't print the address. */
204 pd
->to_print_addr
= true;
206 /* Sign-extend a 32-bit value to a 64-bit value. */
208 pd
->print_addr
= (bfd_vma
)(int32_t) pd
->print_addr
;
210 /* Fit into a 32-bit value on RV32. */
212 pd
->print_addr
= (bfd_vma
)(uint32_t)pd
->print_addr
;
215 /* Print insn arguments for 32/64-bit code. */
218 print_insn_args (const char *oparg
, insn_t l
, bfd_vma pc
, disassemble_info
*info
)
220 struct riscv_private_data
*pd
= info
->private_data
;
221 int rs1
= (l
>> OP_SH_RS1
) & OP_MASK_RS1
;
222 int rd
= (l
>> OP_SH_RD
) & OP_MASK_RD
;
223 fprintf_styled_ftype print
= info
->fprintf_styled_func
;
224 const char *opargStart
;
227 print (info
->stream
, dis_style_text
, "\t");
229 for (; *oparg
!= '\0'; oparg
++)
237 case 's': /* RS1 x8-x15. */
238 case 'w': /* RS1 x8-x15. */
239 print (info
->stream
, dis_style_register
, "%s",
240 riscv_gpr_names
[EXTRACT_OPERAND (CRS1S
, l
) + 8]);
242 case 't': /* RS2 x8-x15. */
243 case 'x': /* RS2 x8-x15. */
244 print (info
->stream
, dis_style_register
, "%s",
245 riscv_gpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
247 case 'U': /* RS1, constrained to equal RD. */
248 print (info
->stream
, dis_style_register
,
249 "%s", riscv_gpr_names
[rd
]);
251 case 'c': /* RS1, constrained to equal sp. */
252 print (info
->stream
, dis_style_register
, "%s",
253 riscv_gpr_names
[X_SP
]);
256 print (info
->stream
, dis_style_register
, "%s",
257 riscv_gpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
261 if (((l
& MASK_C_ADDI
) == MATCH_C_ADDI
) && rd
!= 0)
262 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 0);
263 if (info
->mach
== bfd_mach_riscv64
264 && ((l
& MASK_C_ADDIW
) == MATCH_C_ADDIW
) && rd
!= 0)
265 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 1);
266 print (info
->stream
, dis_style_immediate
, "%d",
267 (int)EXTRACT_CITYPE_IMM (l
));
270 print (info
->stream
, dis_style_address_offset
, "%d",
271 (int)EXTRACT_CLTYPE_LW_IMM (l
));
274 print (info
->stream
, dis_style_address_offset
, "%d",
275 (int)EXTRACT_CLTYPE_LD_IMM (l
));
278 print (info
->stream
, dis_style_address_offset
, "%d",
279 (int)EXTRACT_CITYPE_LWSP_IMM (l
));
282 print (info
->stream
, dis_style_address_offset
, "%d",
283 (int)EXTRACT_CITYPE_LDSP_IMM (l
));
286 print (info
->stream
, dis_style_immediate
, "%d",
287 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l
));
290 print (info
->stream
, dis_style_immediate
, "%d",
291 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l
));
294 print (info
->stream
, dis_style_address_offset
, "%d",
295 (int)EXTRACT_CSSTYPE_SWSP_IMM (l
));
298 print (info
->stream
, dis_style_address_offset
, "%d",
299 (int)EXTRACT_CSSTYPE_SDSP_IMM (l
));
302 info
->target
= EXTRACT_CBTYPE_IMM (l
) + pc
;
303 (*info
->print_address_func
) (info
->target
, info
);
306 info
->target
= EXTRACT_CJTYPE_IMM (l
) + pc
;
307 (*info
->print_address_func
) (info
->target
, info
);
310 print (info
->stream
, dis_style_immediate
, "0x%x",
311 (unsigned)(EXTRACT_CITYPE_IMM (l
) & (RISCV_BIGIMM_REACH
-1)));
314 print (info
->stream
, dis_style_immediate
, "0x%x",
315 (unsigned)EXTRACT_CITYPE_IMM (l
) & 0x3f);
318 print (info
->stream
, dis_style_immediate
, "0x%x",
319 (unsigned)EXTRACT_CITYPE_IMM (l
) & 0x1f);
321 case 'T': /* Floating-point RS2. */
322 print (info
->stream
, dis_style_register
, "%s",
323 riscv_fpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
325 case 'D': /* Floating-point RS2 x8-x15. */
326 print (info
->stream
, dis_style_register
, "%s",
327 riscv_fpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
337 print (info
->stream
, dis_style_register
, "%s",
338 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
341 if (!EXTRACT_OPERAND (VWD
, l
))
342 print (info
->stream
, dis_style_register
, "%s",
345 print (info
->stream
, dis_style_register
, "%s",
346 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
349 print (info
->stream
, dis_style_register
, "%s",
350 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS1
, l
)]);
353 case 'u': /* VS1 == VS2 already verified at this point. */
354 case 'v': /* VD == VS1 == VS2 already verified at this point. */
355 print (info
->stream
, dis_style_register
, "%s",
356 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS2
, l
)]);
359 print (info
->stream
, dis_style_register
, "%s",
360 riscv_vecr_names_numeric
[0]);
365 int imm
= (*oparg
== 'b') ? EXTRACT_RVV_VB_IMM (l
)
366 : EXTRACT_RVV_VC_IMM (l
);
367 unsigned int imm_vlmul
= EXTRACT_OPERAND (VLMUL
, imm
);
368 unsigned int imm_vsew
= EXTRACT_OPERAND (VSEW
, imm
);
369 unsigned int imm_vta
= EXTRACT_OPERAND (VTA
, imm
);
370 unsigned int imm_vma
= EXTRACT_OPERAND (VMA
, imm
);
371 unsigned int imm_vtype_res
= (imm
>> 8);
373 if (imm_vsew
< ARRAY_SIZE (riscv_vsew
)
374 && imm_vlmul
< ARRAY_SIZE (riscv_vlmul
)
375 && imm_vta
< ARRAY_SIZE (riscv_vta
)
376 && imm_vma
< ARRAY_SIZE (riscv_vma
)
378 && riscv_vsew
[imm_vsew
] != NULL
379 && riscv_vlmul
[imm_vlmul
] != NULL
)
380 print (info
->stream
, dis_style_text
, "%s,%s,%s,%s",
381 riscv_vsew
[imm_vsew
],
382 riscv_vlmul
[imm_vlmul
], riscv_vta
[imm_vta
],
385 print (info
->stream
, dis_style_immediate
, "%d", imm
);
389 print (info
->stream
, dis_style_immediate
, "%d",
390 (int)EXTRACT_RVV_VI_IMM (l
));
393 print (info
->stream
, dis_style_immediate
, "%d",
394 (int)EXTRACT_RVV_VI_UIMM (l
));
397 print (info
->stream
, dis_style_immediate
, "%d",
398 (int)EXTRACT_RVV_OFFSET (l
));
401 if (!EXTRACT_OPERAND (VMASK
, l
))
403 print (info
->stream
, dis_style_text
, ",");
404 print (info
->stream
, dis_style_register
, "%s",
405 riscv_vecm_names_numeric
[0]);
416 print (info
->stream
, dis_style_text
, "%c", *oparg
);
420 /* Only print constant 0 if it is the last argument. */
422 print (info
->stream
, dis_style_immediate
, "0");
426 if ((l
& MASK_JALR
) == MATCH_JALR
)
427 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
428 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[rs1
]);
432 print (info
->stream
, dis_style_register
, "%s",
433 riscv_gpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
437 print (info
->stream
, dis_style_immediate
, "0x%x",
438 (unsigned)EXTRACT_UTYPE_IMM (l
) >> RISCV_IMM_BITS
);
442 arg_print (info
, EXTRACT_OPERAND (RM
, l
),
443 riscv_rm
, ARRAY_SIZE (riscv_rm
));
447 arg_print (info
, EXTRACT_OPERAND (PRED
, l
),
448 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
452 arg_print (info
, EXTRACT_OPERAND (SUCC
, l
),
453 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
457 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
460 if (((l
& MASK_ADDI
) == MATCH_ADDI
&& rs1
!= 0)
461 || (l
& MASK_JALR
) == MATCH_JALR
)
462 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
463 if (info
->mach
== bfd_mach_riscv64
464 && ((l
& MASK_ADDIW
) == MATCH_ADDIW
) && rs1
!= 0)
465 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 1);
466 print (info
->stream
, dis_style_immediate
, "%d",
467 (int)EXTRACT_ITYPE_IMM (l
));
471 maybe_print_address (pd
, rs1
, EXTRACT_STYPE_IMM (l
), 0);
472 print (info
->stream
, dis_style_address_offset
, "%d",
473 (int)EXTRACT_STYPE_IMM (l
));
477 print (info
->stream
, dis_style_address_offset
, "%d",
478 (int)EXTRACT_STYPE_IMM (l
));
482 info
->target
= EXTRACT_JTYPE_IMM (l
) + pc
;
483 (*info
->print_address_func
) (info
->target
, info
);
487 info
->target
= EXTRACT_BTYPE_IMM (l
) + pc
;
488 (*info
->print_address_func
) (info
->target
, info
);
492 if ((l
& MASK_AUIPC
) == MATCH_AUIPC
)
493 pd
->hi_addr
[rd
] = pc
+ EXTRACT_UTYPE_IMM (l
);
494 else if ((l
& MASK_LUI
) == MATCH_LUI
)
495 pd
->hi_addr
[rd
] = EXTRACT_UTYPE_IMM (l
);
496 else if ((l
& MASK_C_LUI
) == MATCH_C_LUI
)
497 pd
->hi_addr
[rd
] = EXTRACT_CITYPE_LUI_IMM (l
);
498 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[rd
]);
502 print (info
->stream
, dis_style_immediate
, "0x%x",
503 (unsigned)EXTRACT_OPERAND (BS
, l
));
507 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[0]);
511 print (info
->stream
, dis_style_immediate
, "0x%x",
512 (unsigned)EXTRACT_OPERAND (SHAMT
, l
));
516 print (info
->stream
, dis_style_immediate
, "0x%x",
517 (unsigned)EXTRACT_OPERAND (SHAMTW
, l
));
522 print (info
->stream
, dis_style_register
, "%s", riscv_fpr_names
[rs1
]);
526 print (info
->stream
, dis_style_register
, "%s",
527 riscv_fpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
531 print (info
->stream
, dis_style_register
, "%s", riscv_fpr_names
[rd
]);
535 print (info
->stream
, dis_style_register
, "%s",
536 riscv_fpr_names
[EXTRACT_OPERAND (RS3
, l
)]);
541 static const char *riscv_csr_hash
[4096]; /* Total 2^12 CSRs. */
542 static bool init_csr
= false;
543 unsigned int csr
= EXTRACT_OPERAND (CSR
, l
);
548 for (i
= 0; i
< 4096; i
++)
549 riscv_csr_hash
[i
] = NULL
;
551 /* Set to the newest privileged version. */
552 if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
553 default_priv_spec
= PRIV_SPEC_CLASS_DRAFT
- 1;
555 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
556 if (riscv_csr_hash[num] == NULL \
557 && ((define_version == PRIV_SPEC_CLASS_NONE \
558 && abort_version == PRIV_SPEC_CLASS_NONE) \
559 || (default_priv_spec >= define_version \
560 && default_priv_spec < abort_version))) \
561 riscv_csr_hash[num] = #name;
562 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
563 DECLARE_CSR (name, num, class, define_version, abort_version)
564 #include "opcode/riscv-opc.h"
568 if (riscv_csr_hash
[csr
] != NULL
)
569 print (info
->stream
, dis_style_register
, "%s",
570 riscv_csr_hash
[csr
]);
572 print (info
->stream
, dis_style_immediate
, "0x%x", csr
);
577 print (info
->stream
, dis_style_immediate
, "0x%x",
578 (unsigned) EXTRACT_OPERAND (RNUM
, l
));
582 print (info
->stream
, dis_style_immediate
, "%d", rs1
);
585 case 'X': /* Integer immediate. */
593 case 'l': /* Literal. */
595 while (*oparg
&& *oparg
!= ',')
597 print (info
->stream
, dis_style_immediate
, "%c", *oparg
);
602 case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */
605 case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */
609 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
611 goto undefined_modifier
;
612 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
616 print (info
->stream
, dis_style_immediate
, "%lu",
617 (unsigned long)EXTRACT_U_IMM (n
, s
, l
));
619 print (info
->stream
, dis_style_immediate
, "%li",
620 (signed long)EXTRACT_S_IMM (n
, s
, l
));
623 goto undefined_modifier
;
629 /* xgettext:c-format */
630 print (info
->stream
, dis_style_text
,
631 _("# internal error, undefined modifier (%c)"),
638 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
639 on using INFO. Returns length of the instruction, in bytes.
640 BIGENDIAN must be 1 if this is big-endian code, 0 if
641 this is little-endian code. */
644 riscv_disassemble_insn (bfd_vma memaddr
, insn_t word
, disassemble_info
*info
)
646 const struct riscv_opcode
*op
;
647 static bool init
= false;
648 static const struct riscv_opcode
*riscv_hash
[OP_MASK_OP
+ 1];
649 struct riscv_private_data
*pd
;
652 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
654 /* Build a hash table to shorten the search time. */
657 for (op
= riscv_opcodes
; op
->name
; op
++)
658 if (!riscv_hash
[OP_HASH_IDX (op
->match
)])
659 riscv_hash
[OP_HASH_IDX (op
->match
)] = op
;
664 if (info
->private_data
== NULL
)
668 pd
= info
->private_data
= xcalloc (1, sizeof (struct riscv_private_data
));
671 for (i
= 0; i
< (int)ARRAY_SIZE (pd
->hi_addr
); i
++)
673 pd
->to_print_addr
= false;
676 for (i
= 0; i
< info
->symtab_size
; i
++)
677 if (strcmp (bfd_asymbol_name (info
->symtab
[i
]), RISCV_GP_SYMBOL
) == 0)
679 pd
->gp
= bfd_asymbol_value (info
->symtab
[i
]);
684 pd
= info
->private_data
;
686 insnlen
= riscv_insn_length (word
);
688 /* RISC-V instructions are always little-endian. */
689 info
->endian_code
= BFD_ENDIAN_LITTLE
;
691 info
->bytes_per_chunk
= insnlen
% 4 == 0 ? 4 : 2;
692 info
->bytes_per_line
= 8;
693 /* We don't support constant pools, so this must be code. */
694 info
->display_endian
= info
->endian_code
;
695 info
->insn_info_valid
= 1;
696 info
->branch_delay_insns
= 0;
698 info
->insn_type
= dis_nonbranch
;
702 op
= riscv_hash
[OP_HASH_IDX (word
)];
705 /* If XLEN is not known, get its value from the ELF class. */
706 if (info
->mach
== bfd_mach_riscv64
)
708 else if (info
->mach
== bfd_mach_riscv32
)
710 else if (info
->section
!= NULL
)
712 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (info
->section
->owner
);
713 xlen
= ehdr
->e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
716 /* If arch has the Zfinx extension, replace FPR with GPR. */
717 if (riscv_subset_supports (&riscv_rps_dis
, "zfinx"))
718 riscv_fpr_names
= riscv_gpr_names
;
720 riscv_fpr_names
= riscv_gpr_names
== riscv_gpr_names_abi
?
721 riscv_fpr_names_abi
: riscv_fpr_names_numeric
;
723 for (; op
->name
; op
++)
725 /* Does the opcode match? */
726 if (! (op
->match_func
) (op
, word
))
728 /* Is this a pseudo-instruction and may we print it as such? */
729 if (no_aliases
&& (op
->pinfo
& INSN_ALIAS
))
731 /* Is this instruction restricted to a certain value of XLEN? */
732 if ((op
->xlen_requirement
!= 0) && (op
->xlen_requirement
!= xlen
))
734 /* Is this instruction supported by the current architecture? */
735 if (!riscv_multi_subset_supports (&riscv_rps_dis
, op
->insn_class
))
739 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_mnemonic
,
741 print_insn_args (op
->args
, word
, memaddr
, info
);
743 /* Try to disassemble multi-instruction addressing sequences. */
744 if (pd
->to_print_addr
)
746 info
->target
= pd
->print_addr
;
747 (*info
->fprintf_styled_func
)
748 (info
->stream
, dis_style_comment_start
, " # ");
749 (*info
->print_address_func
) (info
->target
, info
);
750 pd
->to_print_addr
= false;
753 /* Finish filling out insn_info fields. */
754 switch (op
->pinfo
& INSN_TYPE
)
757 info
->insn_type
= dis_branch
;
759 case INSN_CONDBRANCH
:
760 info
->insn_type
= dis_condbranch
;
763 info
->insn_type
= dis_jsr
;
766 info
->insn_type
= dis_dref
;
772 if (op
->pinfo
& INSN_DATA_SIZE
)
774 int size
= ((op
->pinfo
& INSN_DATA_SIZE
)
775 >> INSN_DATA_SIZE_SHIFT
);
776 info
->data_size
= 1 << (size
- 1);
783 /* We did not find a match, so just print the instruction bits. */
784 info
->insn_type
= dis_noninsn
;
790 (*info
->fprintf_styled_func
)
791 (info
->stream
, dis_style_assembler_directive
, ".%dbyte", insnlen
);
792 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
793 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
794 "0x%llx", (unsigned long long) word
);
799 (*info
->fprintf_styled_func
)
800 (info
->stream
, dis_style_assembler_directive
, ".byte");
801 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
802 for (i
= 0; i
< insnlen
; ++i
)
805 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
,
807 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
809 (unsigned int) (word
& 0xff));
818 /* Return true if we find the suitable mapping symbol,
819 and also update the STATE. Otherwise, return false. */
822 riscv_get_map_state (int n
,
823 enum riscv_seg_mstate
*state
,
824 struct disassemble_info
*info
)
828 /* If the symbol is in a different section, ignore it. */
829 if (info
->section
!= NULL
830 && info
->section
!= info
->symtab
[n
]->section
)
833 name
= bfd_asymbol_name(info
->symtab
[n
]);
834 if (strcmp (name
, "$x") == 0)
836 else if (strcmp (name
, "$d") == 0)
838 else if (strncmp (name
, "$xrv", 4) == 0)
841 riscv_release_subset_list (&riscv_subsets
);
842 riscv_parse_subset (&riscv_rps_dis
, name
+ 2);
850 /* Check the sorted symbol table (sorted by the symbol value), find the
851 suitable mapping symbols. */
853 static enum riscv_seg_mstate
854 riscv_search_mapping_symbol (bfd_vma memaddr
,
855 struct disassemble_info
*info
)
857 enum riscv_seg_mstate mstate
;
858 bool from_last_map_symbol
;
863 /* Decide whether to print the data or instruction by default, in case
864 we can not find the corresponding mapping symbols. */
867 && info
->section
->flags
& SEC_CODE
)
871 if (info
->symtab_size
== 0
872 || bfd_asymbol_flavour (*info
->symtab
) != bfd_target_elf_flavour
)
875 /* Reset the last_map_symbol if we start to dump a new section. */
877 last_map_symbol
= -1;
879 /* If the last stop offset is different from the current one, then
880 don't use the last_map_symbol to search. We usually reset the
881 info->stop_offset when handling a new section. */
882 from_last_map_symbol
= (last_map_symbol
>= 0
883 && info
->stop_offset
== last_stop_offset
);
885 /* Start scanning at the start of the function, or wherever
886 we finished last time. */
887 n
= info
->symtab_pos
+ 1;
888 if (from_last_map_symbol
&& n
>= last_map_symbol
)
891 /* Find the suitable mapping symbol to dump. */
892 for (; n
< info
->symtab_size
; n
++)
894 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
895 /* We have searched all possible symbols in the range. */
898 if (riscv_get_map_state (n
, &mstate
, info
))
902 /* Do not stop searching, in case there are some mapping
903 symbols have the same value, but have different names.
908 /* We can not find the suitable mapping symbol above. Therefore, we
909 look forwards and try to find it again, but don't go pass the start
910 of the section. Otherwise a data section without mapping symbols
911 can pick up a text mapping symbol of a preceeding section. */
914 n
= info
->symtab_pos
;
915 if (from_last_map_symbol
&& n
>= last_map_symbol
)
920 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
921 /* We have searched all possible symbols in the range. */
922 if (addr
< (info
->section
? info
->section
->vma
: 0))
924 /* Stop searching once we find the closed mapping symbol. */
925 if (riscv_get_map_state (n
, &mstate
, info
))
934 /* Save the information for next use. */
935 last_map_symbol
= symbol
;
936 last_stop_offset
= info
->stop_offset
;
941 /* Decide which data size we should print. */
944 riscv_data_length (bfd_vma memaddr
,
945 disassemble_info
*info
)
951 if (info
->symtab_size
!= 0
952 && bfd_asymbol_flavour (*info
->symtab
) == bfd_target_elf_flavour
953 && last_map_symbol
>= 0)
956 enum riscv_seg_mstate m
= MAP_NONE
;
957 for (n
= last_map_symbol
+ 1; n
< info
->symtab_size
; n
++)
959 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
961 && riscv_get_map_state (n
, &m
, info
))
963 if (addr
- memaddr
< length
)
964 length
= addr
- memaddr
;
972 /* Do not set the length which exceeds the section size. */
973 bfd_vma offset
= info
->section
->vma
+ info
->section
->size
;
975 length
= (offset
< length
) ? offset
: length
;
977 length
= length
== 3 ? 2 : length
;
981 /* Dump the data contents. */
984 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED
,
986 disassemble_info
*info
)
988 info
->display_endian
= info
->endian
;
990 switch (info
->bytes_per_chunk
)
993 info
->bytes_per_line
= 6;
994 (*info
->fprintf_styled_func
)
995 (info
->stream
, dis_style_assembler_directive
, ".byte");
996 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
997 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
998 "0x%02x", (unsigned)data
);
1001 info
->bytes_per_line
= 8;
1002 (*info
->fprintf_styled_func
)
1003 (info
->stream
, dis_style_assembler_directive
, ".short");
1004 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1005 (*info
->fprintf_styled_func
)
1006 (info
->stream
, dis_style_immediate
, "0x%04x", (unsigned) data
);
1009 info
->bytes_per_line
= 8;
1010 (*info
->fprintf_styled_func
)
1011 (info
->stream
, dis_style_assembler_directive
, ".word");
1012 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1013 (*info
->fprintf_styled_func
)
1014 (info
->stream
, dis_style_immediate
, "0x%08lx",
1015 (unsigned long) data
);
1018 info
->bytes_per_line
= 8;
1019 (*info
->fprintf_styled_func
)
1020 (info
->stream
, dis_style_assembler_directive
, ".dword");
1021 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1022 (*info
->fprintf_styled_func
)
1023 (info
->stream
, dis_style_immediate
, "0x%016llx",
1024 (unsigned long long) data
);
1029 return info
->bytes_per_chunk
;
1033 print_insn_riscv (bfd_vma memaddr
, struct disassemble_info
*info
)
1035 bfd_byte packet
[RISCV_MAX_INSN_LEN
];
1039 enum riscv_seg_mstate mstate
;
1040 int (*riscv_disassembler
) (bfd_vma
, insn_t
, struct disassemble_info
*);
1042 if (info
->disassembler_options
!= NULL
)
1044 parse_riscv_dis_options (info
->disassembler_options
);
1045 /* Avoid repeatedly parsing the options. */
1046 info
->disassembler_options
= NULL
;
1048 else if (riscv_gpr_names
== NULL
)
1049 set_default_riscv_dis_options ();
1051 mstate
= riscv_search_mapping_symbol (memaddr
, info
);
1053 /* Set the size to dump. */
1054 if (mstate
== MAP_DATA
1055 && (info
->flags
& DISASSEMBLE_DATA
) == 0)
1057 dump_size
= riscv_data_length (memaddr
, info
);
1058 info
->bytes_per_chunk
= dump_size
;
1059 riscv_disassembler
= riscv_disassemble_data
;
1063 /* Get the first 2-bytes to check the lenghth of instruction. */
1064 status
= (*info
->read_memory_func
) (memaddr
, packet
, 2, info
);
1067 (*info
->memory_error_func
) (status
, memaddr
, info
);
1070 insn
= (insn_t
) bfd_getl16 (packet
);
1071 dump_size
= riscv_insn_length (insn
);
1072 riscv_disassembler
= riscv_disassemble_insn
;
1075 /* Fetch the instruction to dump. */
1076 status
= (*info
->read_memory_func
) (memaddr
, packet
, dump_size
, info
);
1079 (*info
->memory_error_func
) (status
, memaddr
, info
);
1082 insn
= (insn_t
) bfd_get_bits (packet
, dump_size
* 8, false);
1084 return (*riscv_disassembler
) (memaddr
, insn
, info
);
1088 riscv_get_disassembler (bfd
*abfd
)
1090 const char *default_arch
= "rv64gc";
1092 if (abfd
&& bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1094 const char *sec_name
= get_elf_backend_data (abfd
)->obj_attrs_section
;
1095 if (bfd_get_section_by_name (abfd
, sec_name
) != NULL
)
1097 obj_attribute
*attr
= elf_known_obj_attributes_proc (abfd
);
1098 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
1099 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
1100 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
1101 riscv_get_priv_spec_class_from_numbers (attr
[Tag_a
].i
,
1104 &default_priv_spec
);
1105 default_arch
= attr
[Tag_RISCV_arch
].s
;
1109 riscv_release_subset_list (&riscv_subsets
);
1110 riscv_parse_subset (&riscv_rps_dis
, default_arch
);
1111 return print_insn_riscv
;
1114 /* Prevent use of the fake labels that are generated as part of the DWARF
1115 and for relaxable relocations in the assembler. */
1118 riscv_symbol_is_valid (asymbol
* sym
,
1119 struct disassemble_info
* info ATTRIBUTE_UNUSED
)
1126 name
= bfd_asymbol_name (sym
);
1128 return (strcmp (name
, RISCV_FAKE_LABEL_NAME
) != 0
1129 && !riscv_elf_is_mapping_symbols (name
));
1133 /* Indices into option argument vector for options accepting an argument.
1134 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1138 RISCV_OPTION_ARG_NONE
= -1,
1139 RISCV_OPTION_ARG_PRIV_SPEC
,
1141 RISCV_OPTION_ARG_COUNT
1142 } riscv_option_arg_t
;
1144 /* Valid RISCV disassembler options. */
1149 const char *description
;
1150 riscv_option_arg_t arg
;
1154 N_("Print numeric register names, rather than ABI names."),
1155 RISCV_OPTION_ARG_NONE
},
1157 N_("Disassemble only into canonical instructions."),
1158 RISCV_OPTION_ARG_NONE
},
1160 N_("Print the CSR according to the chosen privilege spec."),
1161 RISCV_OPTION_ARG_PRIV_SPEC
}
1164 /* Build the structure representing valid RISCV disassembler options.
1165 This is done dynamically for maintenance ease purpose; a static
1166 initializer would be unreadable. */
1168 const disasm_options_and_args_t
*
1169 disassembler_options_riscv (void)
1171 static disasm_options_and_args_t
*opts_and_args
;
1173 if (opts_and_args
== NULL
)
1175 size_t num_options
= ARRAY_SIZE (riscv_options
);
1176 size_t num_args
= RISCV_OPTION_ARG_COUNT
;
1177 disasm_option_arg_t
*args
;
1178 disasm_options_t
*opts
;
1179 size_t i
, priv_spec_count
;
1181 args
= XNEWVEC (disasm_option_arg_t
, num_args
+ 1);
1183 args
[RISCV_OPTION_ARG_PRIV_SPEC
].name
= "SPEC";
1184 priv_spec_count
= PRIV_SPEC_CLASS_DRAFT
- PRIV_SPEC_CLASS_NONE
- 1;
1185 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
1186 = XNEWVEC (const char *, priv_spec_count
+ 1);
1187 for (i
= 0; i
< priv_spec_count
; i
++)
1188 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
]
1189 = riscv_priv_specs
[i
].name
;
1190 /* The array we return must be NULL terminated. */
1191 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
] = NULL
;
1193 /* The array we return must be NULL terminated. */
1194 args
[num_args
].name
= NULL
;
1195 args
[num_args
].values
= NULL
;
1197 opts_and_args
= XNEW (disasm_options_and_args_t
);
1198 opts_and_args
->args
= args
;
1200 opts
= &opts_and_args
->options
;
1201 opts
->name
= XNEWVEC (const char *, num_options
+ 1);
1202 opts
->description
= XNEWVEC (const char *, num_options
+ 1);
1203 opts
->arg
= XNEWVEC (const disasm_option_arg_t
*, num_options
+ 1);
1204 for (i
= 0; i
< num_options
; i
++)
1206 opts
->name
[i
] = riscv_options
[i
].name
;
1207 opts
->description
[i
] = _(riscv_options
[i
].description
);
1208 if (riscv_options
[i
].arg
!= RISCV_OPTION_ARG_NONE
)
1209 opts
->arg
[i
] = &args
[riscv_options
[i
].arg
];
1211 opts
->arg
[i
] = NULL
;
1213 /* The array we return must be NULL terminated. */
1214 opts
->name
[i
] = NULL
;
1215 opts
->description
[i
] = NULL
;
1216 opts
->arg
[i
] = NULL
;
1219 return opts_and_args
;
1223 print_riscv_disassembler_options (FILE *stream
)
1225 const disasm_options_and_args_t
*opts_and_args
;
1226 const disasm_option_arg_t
*args
;
1227 const disasm_options_t
*opts
;
1232 opts_and_args
= disassembler_options_riscv ();
1233 opts
= &opts_and_args
->options
;
1234 args
= opts_and_args
->args
;
1236 fprintf (stream
, _("\n\
1237 The following RISC-V specific disassembler options are supported for use\n\
1238 with the -M switch (multiple options should be separated by commas):\n"));
1239 fprintf (stream
, "\n");
1241 /* Compute the length of the longest option name. */
1242 for (i
= 0; opts
->name
[i
] != NULL
; i
++)
1244 size_t len
= strlen (opts
->name
[i
]);
1246 if (opts
->arg
[i
] != NULL
)
1247 len
+= strlen (opts
->arg
[i
]->name
);
1252 for (i
= 0, max_len
++; opts
->name
[i
] != NULL
; i
++)
1254 fprintf (stream
, " %s", opts
->name
[i
]);
1255 if (opts
->arg
[i
] != NULL
)
1256 fprintf (stream
, "%s", opts
->arg
[i
]->name
);
1257 if (opts
->description
[i
] != NULL
)
1259 size_t len
= strlen (opts
->name
[i
]);
1261 if (opts
->arg
!= NULL
&& opts
->arg
[i
] != NULL
)
1262 len
+= strlen (opts
->arg
[i
]->name
);
1263 fprintf (stream
, "%*c %s", (int) (max_len
- len
), ' ',
1264 opts
->description
[i
]);
1266 fprintf (stream
, "\n");
1269 for (i
= 0; args
[i
].name
!= NULL
; i
++)
1271 if (args
[i
].values
== NULL
)
1273 fprintf (stream
, _("\n\
1274 For the options above, the following values are supported for \"%s\":\n "),
1276 for (j
= 0; args
[i
].values
[j
] != NULL
; j
++)
1277 fprintf (stream
, " %s", args
[i
].values
[j
]);
1278 fprintf (stream
, _("\n"));
1281 fprintf (stream
, _("\n"));