2 Copyright (C) 2011-2025 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 /* The RISC-V disassembler produces styled output using
36 disassemble_info::fprintf_styled_func. This define prevents use of
37 disassemble_info::fprintf_func which is for unstyled output. */
38 #define fprintf_func please_use_fprintf_styled_func_instead
40 /* The earliest privilege spec supported by disassembler. */
41 #define PRIV_SPEC_EARLIEST PRIV_SPEC_CLASS_1P10
43 struct riscv_private_data
47 bfd_vma hi_addr
[OP_MASK_RD
+ 1];
50 /* Current XLEN for the disassembler. */
52 /* Default ISA specification version. */
53 enum riscv_spec_class default_isa_spec
;
54 /* Default privileged specification. */
55 enum riscv_spec_class default_priv_spec
;
56 /* Used for architecture parser. */
57 riscv_parse_subset_t riscv_rps_dis
;
58 /* Default architecture string for the object file. It will be changed once
59 elf architecture attribute exits. This is used for mapping symbol $x. */
60 const char* default_arch
;
61 /* Used for mapping symbols. */
63 bfd_vma last_stop_offset
;
64 bfd_vma last_map_symbol_boundary
;
65 enum riscv_seg_mstate last_map_state
;
66 asection
*last_map_section
;
67 /* Register names as used by the disassembler. */
68 const char (*riscv_gpr_names
)[NRC
];
69 const char (*riscv_fpr_names
)[NRC
];
70 /* If set, disassemble as most general instruction. */
72 /* If set, disassemble without checking architecture string, just like what
73 we did at the beginning. */
77 /* Set default RISC-V disassembler options. */
80 set_default_riscv_dis_options (struct disassemble_info
*info
)
82 struct riscv_private_data
*pd
= info
->private_data
;
83 pd
->riscv_gpr_names
= riscv_gpr_names_abi
;
84 pd
->riscv_fpr_names
= riscv_fpr_names_abi
;
85 pd
->no_aliases
= false;
89 /* Parse RISC-V disassembler option (without arguments). */
92 parse_riscv_dis_option_without_args (const char *option
,
93 struct disassemble_info
*info
)
95 struct riscv_private_data
*pd
= info
->private_data
;
96 if (strcmp (option
, "no-aliases") == 0)
97 pd
->no_aliases
= true;
98 else if (strcmp (option
, "numeric") == 0)
100 pd
->riscv_gpr_names
= riscv_gpr_names_numeric
;
101 pd
->riscv_fpr_names
= riscv_fpr_names_numeric
;
103 else if (strcmp (option
, "max") == 0)
110 /* Parse RISC-V disassembler option (possibly with arguments). */
113 parse_riscv_dis_option (const char *option
, struct disassemble_info
*info
)
117 if (parse_riscv_dis_option_without_args (option
, info
))
120 equal
= strchr (option
, '=');
123 /* The option without '=' should be defined above. */
124 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
128 || *(equal
+ 1) == '\0')
130 /* Invalid options with '=', no option name before '=',
131 and no value after '='. */
132 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
139 if (strcmp (option
, "priv-spec") == 0)
141 struct riscv_private_data
*pd
= info
->private_data
;
142 enum riscv_spec_class priv_spec
= PRIV_SPEC_CLASS_NONE
;
143 const char *name
= NULL
;
145 RISCV_GET_PRIV_SPEC_CLASS (value
, priv_spec
);
146 if (priv_spec
< PRIV_SPEC_EARLIEST
)
147 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
149 else if (pd
->default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
150 pd
->default_priv_spec
= priv_spec
;
151 else if (pd
->default_priv_spec
!= priv_spec
)
153 RISCV_GET_PRIV_SPEC_NAME (name
, pd
->default_priv_spec
);
154 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
155 "the elf privilege attribute is %s"),
156 option
, value
, name
);
161 /* xgettext:c-format */
162 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
166 /* Parse RISC-V disassembler options. */
169 parse_riscv_dis_options (const char *opts_in
, struct disassemble_info
*info
)
171 char *opts
= xstrdup (opts_in
), *opt
= opts
, *opt_end
= opts
;
173 set_default_riscv_dis_options (info
);
175 for ( ; opt_end
!= NULL
; opt
= opt_end
+ 1)
177 if ((opt_end
= strchr (opt
, ',')) != NULL
)
179 parse_riscv_dis_option (opt
, info
);
185 /* Print one argument from an array. */
188 arg_print (struct disassemble_info
*info
, unsigned long val
,
189 const char* const* array
, size_t size
)
191 const char *s
= val
>= size
|| array
[val
] == NULL
? "unknown" : array
[val
];
192 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "%s", s
);
195 /* If we need to print an address, set its value and state. */
198 maybe_print_address (struct riscv_private_data
*pd
, int base_reg
, int offset
,
201 if (pd
->hi_addr
[base_reg
] != (bfd_vma
)-1)
203 pd
->print_addr
= (base_reg
!= 0 ? pd
->hi_addr
[base_reg
] : 0) + offset
;
204 pd
->hi_addr
[base_reg
] = -1;
206 else if (base_reg
== X_GP
&& pd
->has_gp
)
207 pd
->print_addr
= pd
->gp
+ offset
;
208 else if (base_reg
== X_TP
|| base_reg
== 0)
209 pd
->print_addr
= offset
;
211 return; /* Don't print the address. */
212 pd
->to_print_addr
= true;
214 /* Sign-extend a 32-bit value to a 64-bit value. */
216 pd
->print_addr
= (bfd_vma
)(int32_t) pd
->print_addr
;
218 /* Fit into a 32-bit value on RV32. */
220 pd
->print_addr
= (bfd_vma
)(uint32_t)pd
->print_addr
;
223 /* Get Zcmp reg_list field. */
226 print_reg_list (disassemble_info
*info
, insn_t l
)
228 struct riscv_private_data
*pd
= info
->private_data
;
229 bool numeric
= pd
->riscv_gpr_names
== riscv_gpr_names_numeric
;
230 unsigned reg_list
= (int)EXTRACT_OPERAND (REG_LIST
, l
);
231 unsigned r_start
= numeric
? X_S2
: X_S0
;
232 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
233 "%s", pd
->riscv_gpr_names
[X_RA
]);
237 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
238 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
239 "%s", pd
->riscv_gpr_names
[X_S0
]);
241 else if (reg_list
== 6 || (numeric
&& reg_list
> 6))
243 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
244 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
245 "%s", pd
->riscv_gpr_names
[X_S0
]);
246 info
->fprintf_styled_func (info
->stream
, dis_style_text
, "-");
247 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
248 "%s", pd
->riscv_gpr_names
[X_S1
]);
253 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
254 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
255 "%s", pd
->riscv_gpr_names
[r_start
]);
256 info
->fprintf_styled_func (info
->stream
, dis_style_text
, "-");
257 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
258 "%s", pd
->riscv_gpr_names
[X_S11
]);
260 else if (reg_list
== 7 && numeric
)
262 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
263 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
264 "%s", pd
->riscv_gpr_names
[X_S2
]);
266 else if (reg_list
> 6)
268 info
->fprintf_styled_func (info
->stream
, dis_style_text
, ",");
269 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
270 "%s", pd
->riscv_gpr_names
[r_start
]);
271 info
->fprintf_styled_func (info
->stream
, dis_style_text
, "-");
272 info
->fprintf_styled_func (info
->stream
, dis_style_register
,
273 "%s", pd
->riscv_gpr_names
[reg_list
+ 11]);
277 /* Get Zcmp sp adjustment immediate. */
280 riscv_get_spimm (insn_t l
, int xlen
)
282 int spimm
= riscv_get_sp_base(l
, xlen
);
283 spimm
+= EXTRACT_ZCMP_SPIMM (l
);
284 if (((l
^ MATCH_CM_PUSH
) & MASK_CM_PUSH
) == 0)
289 /* Get s-register regno by using sreg number.
290 e.g. the regno of s0 is 8, so
291 riscv_zcmp_get_sregno (0) equals 8. */
294 riscv_zcmp_get_sregno (unsigned sreg_idx
)
296 return sreg_idx
> 1 ?
297 sreg_idx
+ 16 : sreg_idx
+ 8;
300 /* Print insn arguments for 32/64-bit code. */
303 print_insn_args (const char *oparg
, insn_t l
, bfd_vma pc
, disassemble_info
*info
)
305 struct riscv_private_data
*pd
= info
->private_data
;
306 int rs1
= (l
>> OP_SH_RS1
) & OP_MASK_RS1
;
307 int rd
= (l
>> OP_SH_RD
) & OP_MASK_RD
;
308 fprintf_styled_ftype print
= info
->fprintf_styled_func
;
309 const char *opargStart
;
312 print (info
->stream
, dis_style_text
, "\t");
314 for (; *oparg
!= '\0'; oparg
++)
322 case 's': /* RS1 x8-x15. */
323 case 'w': /* RS1 x8-x15. */
324 print (info
->stream
, dis_style_register
, "%s",
325 pd
->riscv_gpr_names
[EXTRACT_OPERAND (CRS1S
, l
) + 8]);
327 case 't': /* RS2 x8-x15. */
328 case 'x': /* RS2 x8-x15. */
329 print (info
->stream
, dis_style_register
, "%s",
330 pd
->riscv_gpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
332 case 'U': /* RS1, constrained to equal RD. */
333 print (info
->stream
, dis_style_register
,
334 "%s", pd
->riscv_gpr_names
[rd
]);
336 case 'c': /* RS1, constrained to equal sp. */
337 print (info
->stream
, dis_style_register
, "%s",
338 pd
->riscv_gpr_names
[X_SP
]);
341 print (info
->stream
, dis_style_register
, "%s",
342 pd
->riscv_gpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
346 if (((l
& MASK_C_ADDI
) == MATCH_C_ADDI
) && rd
!= 0)
347 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 0);
348 if (info
->mach
== bfd_mach_riscv64
349 && ((l
& MASK_C_ADDIW
) == MATCH_C_ADDIW
) && rd
!= 0)
350 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 1);
351 print (info
->stream
, dis_style_immediate
, "%d",
352 (int)EXTRACT_CITYPE_IMM (l
));
355 print (info
->stream
, dis_style_address_offset
, "%d",
356 (int)EXTRACT_CLTYPE_LW_IMM (l
));
359 print (info
->stream
, dis_style_address_offset
, "%d",
360 (int)EXTRACT_CLTYPE_LD_IMM (l
));
363 print (info
->stream
, dis_style_address_offset
, "%d",
364 (int)EXTRACT_CITYPE_LWSP_IMM (l
));
367 print (info
->stream
, dis_style_address_offset
, "%d",
368 (int)EXTRACT_CITYPE_LDSP_IMM (l
));
371 print (info
->stream
, dis_style_immediate
, "%d",
372 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l
));
375 print (info
->stream
, dis_style_immediate
, "%d",
376 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l
));
379 print (info
->stream
, dis_style_address_offset
, "%d",
380 (int)EXTRACT_CSSTYPE_SWSP_IMM (l
));
383 print (info
->stream
, dis_style_address_offset
, "%d",
384 (int)EXTRACT_CSSTYPE_SDSP_IMM (l
));
387 info
->target
= EXTRACT_CBTYPE_IMM (l
) + pc
;
388 (*info
->print_address_func
) (info
->target
, info
);
391 info
->target
= EXTRACT_CJTYPE_IMM (l
) + pc
;
392 (*info
->print_address_func
) (info
->target
, info
);
395 print (info
->stream
, dis_style_immediate
, "0x%x",
396 (unsigned)(EXTRACT_CITYPE_IMM (l
) & (RISCV_BIGIMM_REACH
-1)));
399 print (info
->stream
, dis_style_immediate
, "0x%x",
400 (unsigned)EXTRACT_CITYPE_IMM (l
) & 0x3f);
403 print (info
->stream
, dis_style_immediate
, "0x%x",
404 (unsigned)EXTRACT_CITYPE_IMM (l
) & 0x1f);
406 case 'T': /* Floating-point RS2. */
407 print (info
->stream
, dis_style_register
, "%s",
408 pd
->riscv_fpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
410 case 'D': /* Floating-point RS2 x8-x15. */
411 print (info
->stream
, dis_style_register
, "%s",
412 pd
->riscv_fpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
422 print (info
->stream
, dis_style_register
, "%s",
423 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
426 if (!EXTRACT_OPERAND (VWD
, l
))
427 print (info
->stream
, dis_style_register
, "%s",
428 pd
->riscv_gpr_names
[0]);
430 print (info
->stream
, dis_style_register
, "%s",
431 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
434 print (info
->stream
, dis_style_register
, "%s",
435 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS1
, l
)]);
438 case 'u': /* VS1 == VS2 already verified at this point. */
439 case 'v': /* VD == VS1 == VS2 already verified at this point. */
440 print (info
->stream
, dis_style_register
, "%s",
441 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS2
, l
)]);
444 print (info
->stream
, dis_style_register
, "%s",
445 riscv_vecr_names_numeric
[0]);
450 int imm
= (*oparg
== 'b') ? EXTRACT_RVV_VB_IMM (l
)
451 : EXTRACT_RVV_VC_IMM (l
);
452 unsigned int imm_vlmul
= EXTRACT_OPERAND (VLMUL
, imm
);
453 unsigned int imm_vsew
= EXTRACT_OPERAND (VSEW
, imm
);
454 unsigned int imm_vta
= EXTRACT_OPERAND (VTA
, imm
);
455 unsigned int imm_vma
= EXTRACT_OPERAND (VMA
, imm
);
456 unsigned int imm_vtype_res
= (imm
>> 8);
458 if (imm_vsew
< ARRAY_SIZE (riscv_vsew
)
459 && imm_vlmul
< ARRAY_SIZE (riscv_vlmul
)
460 && imm_vta
< ARRAY_SIZE (riscv_vta
)
461 && imm_vma
< ARRAY_SIZE (riscv_vma
)
463 && riscv_vsew
[imm_vsew
] != NULL
464 && riscv_vlmul
[imm_vlmul
] != NULL
)
465 print (info
->stream
, dis_style_text
, "%s,%s,%s,%s",
466 riscv_vsew
[imm_vsew
],
467 riscv_vlmul
[imm_vlmul
], riscv_vta
[imm_vta
],
470 print (info
->stream
, dis_style_immediate
, "%d", imm
);
474 print (info
->stream
, dis_style_immediate
, "%d",
475 (int)EXTRACT_RVV_VI_IMM (l
));
478 print (info
->stream
, dis_style_immediate
, "%d",
479 (int)EXTRACT_RVV_VI_UIMM (l
));
482 print (info
->stream
, dis_style_immediate
, "%d",
483 (int)EXTRACT_RVV_OFFSET (l
));
486 print (info
->stream
, dis_style_immediate
, "%d",
487 (int)EXTRACT_RVV_VI_UIMM6 (l
));
490 if (!EXTRACT_OPERAND (VMASK
, l
))
492 print (info
->stream
, dis_style_text
, ",");
493 print (info
->stream
, dis_style_register
, "%s",
494 riscv_vecm_names_numeric
[0]);
507 print (info
->stream
, dis_style_text
, "%c", *oparg
);
511 /* Only print constant 0 if it is the last argument. */
513 print (info
->stream
, dis_style_immediate
, "0");
517 print (info
->stream
, dis_style_register
, "%s",
518 pd
->riscv_gpr_names
[EXTRACT_OPERAND (RS3
, l
)]);
522 if ((l
& MASK_JALR
) == MATCH_JALR
)
523 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
524 print (info
->stream
, dis_style_register
, "%s",
525 pd
->riscv_gpr_names
[rs1
]);
529 print (info
->stream
, dis_style_register
, "%s",
530 pd
->riscv_gpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
534 print (info
->stream
, dis_style_immediate
, "0x%x",
535 (unsigned)EXTRACT_UTYPE_IMM (l
) >> RISCV_IMM_BITS
);
539 arg_print (info
, EXTRACT_OPERAND (RM
, l
),
540 riscv_rm
, ARRAY_SIZE (riscv_rm
));
544 arg_print (info
, EXTRACT_OPERAND (PRED
, l
),
545 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
549 arg_print (info
, EXTRACT_OPERAND (SUCC
, l
),
550 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
554 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
557 if (((l
& MASK_ADDI
) == MATCH_ADDI
&& rs1
!= 0)
558 || (l
& MASK_JALR
) == MATCH_JALR
)
559 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
560 if (info
->mach
== bfd_mach_riscv64
561 && ((l
& MASK_ADDIW
) == MATCH_ADDIW
) && rs1
!= 0)
562 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 1);
563 print (info
->stream
, dis_style_immediate
, "%d",
564 (int)EXTRACT_ITYPE_IMM (l
));
568 maybe_print_address (pd
, rs1
, EXTRACT_STYPE_IMM (l
), 0);
569 print (info
->stream
, dis_style_address_offset
, "%d",
570 (int)EXTRACT_STYPE_IMM (l
));
574 info
->target
= EXTRACT_JTYPE_IMM (l
) + pc
;
575 (*info
->print_address_func
) (info
->target
, info
);
579 info
->target
= EXTRACT_BTYPE_IMM (l
) + pc
;
580 (*info
->print_address_func
) (info
->target
, info
);
584 if ((l
& MASK_AUIPC
) == MATCH_AUIPC
)
585 pd
->hi_addr
[rd
] = pc
+ EXTRACT_UTYPE_IMM (l
);
586 else if ((l
& MASK_LUI
) == MATCH_LUI
)
587 pd
->hi_addr
[rd
] = EXTRACT_UTYPE_IMM (l
);
588 else if ((l
& MASK_C_LUI
) == MATCH_C_LUI
)
589 pd
->hi_addr
[rd
] = EXTRACT_CITYPE_LUI_IMM (l
);
590 print (info
->stream
, dis_style_register
, "%s",
591 pd
->riscv_gpr_names
[rd
]);
595 print (info
->stream
, dis_style_immediate
, "0x%x",
596 EXTRACT_OPERAND (BS
, l
));
600 print (info
->stream
, dis_style_register
, "%s",
601 pd
->riscv_gpr_names
[0]);
605 print (info
->stream
, dis_style_immediate
, "0x%x",
606 EXTRACT_OPERAND (SHAMT
, l
));
610 print (info
->stream
, dis_style_immediate
, "0x%x",
611 EXTRACT_OPERAND (SHAMTW
, l
));
616 print (info
->stream
, dis_style_register
, "%s",
617 pd
->riscv_fpr_names
[rs1
]);
621 print (info
->stream
, dis_style_register
, "%s",
622 pd
->riscv_fpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
626 print (info
->stream
, dis_style_register
, "%s",
627 pd
->riscv_fpr_names
[rd
]);
631 print (info
->stream
, dis_style_register
, "%s",
632 pd
->riscv_fpr_names
[EXTRACT_OPERAND (RS3
, l
)]);
637 static const char *riscv_csr_hash
[4096]; /* Total 2^12 CSRs. */
638 static bool init_csr
= false;
639 unsigned int csr
= EXTRACT_OPERAND (CSR
, l
);
644 for (i
= 0; i
< 4096; i
++)
645 riscv_csr_hash
[i
] = NULL
;
647 /* Set to the newest privileged version. */
648 if (pd
->default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
649 pd
->default_priv_spec
= PRIV_SPEC_CLASS_DRAFT
- 1;
651 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
652 if (riscv_csr_hash[num] == NULL \
653 && ((define_version == PRIV_SPEC_CLASS_NONE \
654 && abort_version == PRIV_SPEC_CLASS_NONE) \
655 || (pd->default_priv_spec >= define_version \
656 && pd->default_priv_spec < abort_version))) \
657 riscv_csr_hash[num] = #name;
658 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
659 DECLARE_CSR (name, num, class, define_version, abort_version)
660 #include "opcode/riscv-opc.h"
664 if (riscv_csr_hash
[csr
] != NULL
)
665 if (riscv_subset_supports (&pd
->riscv_rps_dis
, "xtheadvector")
666 && (csr
== CSR_VSTART
671 || csr
== CSR_VLENB
))
672 print (info
->stream
, dis_style_register
, "%s",
673 concat ("th.", riscv_csr_hash
[csr
], NULL
));
675 print (info
->stream
, dis_style_register
, "%s",
676 riscv_csr_hash
[csr
]);
678 print (info
->stream
, dis_style_immediate
, "0x%x", csr
);
683 print (info
->stream
, dis_style_immediate
, "0x%x",
684 EXTRACT_OPERAND (RNUM
, l
));
688 print (info
->stream
, dis_style_immediate
, "%d", rs1
);
691 case 'W': /* Various operands for standard z extensions. */
698 print (info
->stream
, dis_style_address_offset
, "%d",
699 (int) EXTRACT_STYPE_IMM (l
));
702 goto undefined_modifier
;
709 if (riscv_fli_symval
[rs1
])
710 print (info
->stream
, dis_style_text
, "%s",
711 riscv_fli_symval
[rs1
]);
713 print (info
->stream
, dis_style_immediate
, "%a",
714 riscv_fli_numval
[rs1
]);
717 goto undefined_modifier
;
720 case 'c': /* Zcb extension 16 bits length instruction fields. */
724 print (info
->stream
, dis_style_register
, "%s",
725 pd
->riscv_gpr_names
[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG1
, l
))]);
728 print (info
->stream
, dis_style_register
, "%s",
729 pd
->riscv_gpr_names
[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG2
, l
))]);
732 print (info
->stream
, dis_style_immediate
, "%d",
733 (int)EXTRACT_ZCB_BYTE_UIMM (l
));
736 print (info
->stream
, dis_style_immediate
, "%d",
737 (int)EXTRACT_ZCB_HALFWORD_UIMM (l
));
740 print_reg_list (info
, l
);
743 print (info
->stream
, dis_style_immediate
, "%d",
744 riscv_get_spimm (l
, pd
->xlen
));
748 print (info
->stream
, dis_style_address_offset
,
749 "%" PRIu64
, EXTRACT_ZCMT_INDEX (l
));
752 goto undefined_modifier
;
756 goto undefined_modifier
;
760 case 'X': /* Vendor-specific operands. */
763 case 't': /* Vendor-specific (T-head) operands. */
773 goto undefined_modifier
;
775 int imm
= (*oparg
== 'b') ? EXTRACT_RVV_VB_IMM (l
)
776 : EXTRACT_RVV_VC_IMM (l
);
777 unsigned int imm_vediv
= EXTRACT_OPERAND (XTHEADVEDIV
, imm
);
778 unsigned int imm_vlmul
= EXTRACT_OPERAND (XTHEADVLMUL
, imm
);
779 unsigned int imm_vsew
= EXTRACT_OPERAND (XTHEADVSEW
, imm
);
780 unsigned int imm_vtype_res
781 = EXTRACT_OPERAND (XTHEADVTYPE_RES
, imm
);
782 if (imm_vsew
< ARRAY_SIZE (riscv_vsew
)
783 && imm_vlmul
< ARRAY_SIZE (riscv_th_vlen
)
784 && imm_vediv
< ARRAY_SIZE (riscv_th_vediv
)
786 print (info
->stream
, dis_style_text
, "%s,%s,%s",
787 riscv_vsew
[imm_vsew
], riscv_th_vlen
[imm_vlmul
],
788 riscv_th_vediv
[imm_vediv
]);
790 print (info
->stream
, dis_style_immediate
, "%d", imm
);
792 case 'l': /* Integer immediate, literal. */
794 while (*oparg
&& *oparg
!= ',')
796 print (info
->stream
, dis_style_immediate
, "%c", *oparg
);
801 case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */
804 case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */
808 n
= strtol (oparg
+ 1, (char **)&oparg
, 10);
810 goto undefined_modifier
;
811 s
= strtol (oparg
+ 1, (char **)&oparg
, 10);
815 print (info
->stream
, dis_style_immediate
, "%lu",
816 (unsigned long)EXTRACT_U_IMM (n
, s
, l
));
818 print (info
->stream
, dis_style_immediate
, "%li",
819 (signed long)EXTRACT_S_IMM (n
, s
, l
));
822 goto undefined_modifier
;
826 case 'c': /* Vendor-specific (CORE-V) operands. */
830 print (info
->stream
, dis_style_immediate
, "%d",
831 ((int) EXTRACT_CV_IS2_UIMM5 (l
)));
834 print (info
->stream
, dis_style_immediate
, "%d",
835 ((int) EXTRACT_CV_IS3_UIMM5 (l
)));
838 print (info
->stream
, dis_style_immediate
, "%d",
839 ((int) EXTRACT_CV_BI_IMM5 (l
)));
842 print (info
->stream
, dis_style_immediate
, "%d",
843 ((int) EXTRACT_CV_SIMD_IMM6 (l
)));
846 print (info
->stream
, dis_style_immediate
, "%d",
847 ((int) EXTRACT_CV_BITMANIP_UIMM5 (l
)));
850 print (info
->stream
, dis_style_immediate
, "%d",
851 ((int) EXTRACT_CV_BITMANIP_UIMM2 (l
)));
854 print (info
->stream
, dis_style_immediate
, "%d",
855 ((int) EXTRACT_CV_SIMD_UIMM6 (l
)));
859 goto undefined_modifier
;
862 case 's': /* Vendor-specific (SiFive) operands. */
865 /* SiFive vector coprocessor interface. */
867 print (info
->stream
, dis_style_register
, "0x%x",
868 (unsigned) EXTRACT_OPERAND (RD
, l
));
871 print (info
->stream
, dis_style_register
, "0x%x",
872 (unsigned) EXTRACT_OPERAND (RS2
, l
));
878 print (info
->stream
, dis_style_register
, "0x%x",
879 (unsigned) EXTRACT_OPERAND (XSO2
, l
));
882 print (info
->stream
, dis_style_register
, "0x%x",
883 (unsigned) EXTRACT_OPERAND (XSO1
, l
));
889 case 'm': /* Vendor-specific (MIPS) operands. */
893 print (info
->stream
, dis_style_register
, "0x%x",
894 (unsigned) EXTRACT_OPERAND (MIPS_HINT
, l
));
897 print (info
->stream
, dis_style_register
, "0x%x",
898 (unsigned) EXTRACT_OPERAND (MIPS_IMM9
, l
));
901 print (info
->stream
, dis_style_immediate
, "%d",
902 (unsigned)EXTRACT_MIPS_LDP_IMM (l
));
905 print (info
->stream
, dis_style_immediate
, "%d",
906 (unsigned)EXTRACT_MIPS_LWP_IMM (l
));
909 print (info
->stream
, dis_style_immediate
, "%d",
910 (unsigned)EXTRACT_MIPS_SDP_IMM (l
));
913 print (info
->stream
, dis_style_immediate
, "%d",
914 (unsigned)EXTRACT_MIPS_SWP_IMM (l
));
917 goto undefined_modifier
;
921 goto undefined_modifier
;
927 /* xgettext:c-format */
928 print (info
->stream
, dis_style_text
,
929 _("# internal error, undefined modifier (%c)"),
936 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
937 on using INFO. Returns length of the instruction, in bytes.
938 BIGENDIAN must be 1 if this is big-endian code, 0 if
939 this is little-endian code. */
942 riscv_disassemble_insn (bfd_vma memaddr
,
944 const bfd_byte
*packet
,
945 disassemble_info
*info
)
947 const struct riscv_opcode
*op
;
948 static bool init
= false;
949 static const struct riscv_opcode
*riscv_hash
[OP_MASK_OP
+ 1];
950 struct riscv_private_data
*pd
= info
->private_data
;
954 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
956 /* Build a hash table to shorten the search time. */
959 for (op
= riscv_opcodes
; op
->name
; op
++)
960 if (!riscv_hash
[OP_HASH_IDX (op
->match
)])
961 riscv_hash
[OP_HASH_IDX (op
->match
)] = op
;
966 insnlen
= riscv_insn_length (word
);
968 /* RISC-V instructions are always little-endian. */
969 info
->endian_code
= BFD_ENDIAN_LITTLE
;
971 info
->bytes_per_chunk
= insnlen
% 4 == 0 ? 4 : 2;
972 info
->bytes_per_line
= 8;
973 /* We don't support constant pools, so this must be code. */
974 info
->display_endian
= info
->endian_code
;
975 info
->insn_info_valid
= 1;
976 info
->branch_delay_insns
= 0;
978 info
->insn_type
= dis_nonbranch
;
982 op
= riscv_hash
[OP_HASH_IDX (word
)];
985 /* If XLEN is not known, get its value from the ELF class. */
986 if (info
->mach
== bfd_mach_riscv64
)
988 else if (info
->mach
== bfd_mach_riscv32
)
990 else if (info
->section
!= NULL
)
992 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (info
->section
->owner
);
993 pd
->xlen
= ehdr
->e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
996 /* If arch has the Zfinx extension, replace FPR with GPR. */
997 if (riscv_subset_supports (&pd
->riscv_rps_dis
, "zfinx"))
998 pd
->riscv_fpr_names
= pd
->riscv_gpr_names
;
1000 pd
->riscv_fpr_names
= pd
->riscv_gpr_names
== riscv_gpr_names_abi
?
1001 riscv_fpr_names_abi
: riscv_fpr_names_numeric
;
1003 for (; op
->name
; op
++)
1005 /* Ignore macro insns. */
1006 if (op
->pinfo
== INSN_MACRO
)
1008 /* Does the opcode match? */
1009 if (! (op
->match_func
) (op
, word
))
1011 /* Is this a pseudo-instruction and may we print it as such? */
1012 if (pd
->no_aliases
&& (op
->pinfo
& INSN_ALIAS
))
1014 /* Is this instruction restricted to a certain value of XLEN? */
1015 if ((op
->xlen_requirement
!= 0)
1016 && (op
->xlen_requirement
!= pd
->xlen
))
1018 /* Is this instruction supported by the current architecture? */
1020 && !riscv_multi_subset_supports (&pd
->riscv_rps_dis
,
1025 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_mnemonic
,
1027 print_insn_args (op
->args
, word
, memaddr
, info
);
1029 /* Try to disassemble multi-instruction addressing sequences. */
1030 if (pd
->to_print_addr
)
1032 info
->target
= pd
->print_addr
;
1033 (*info
->fprintf_styled_func
)
1034 (info
->stream
, dis_style_comment_start
, " # ");
1035 (*info
->print_address_func
) (info
->target
, info
);
1036 pd
->to_print_addr
= false;
1039 /* Finish filling out insn_info fields. */
1040 switch (op
->pinfo
& INSN_TYPE
)
1043 info
->insn_type
= dis_branch
;
1045 case INSN_CONDBRANCH
:
1046 info
->insn_type
= dis_condbranch
;
1049 info
->insn_type
= dis_jsr
;
1052 info
->insn_type
= dis_dref
;
1058 if (op
->pinfo
& INSN_DATA_SIZE
)
1060 int size
= ((op
->pinfo
& INSN_DATA_SIZE
)
1061 >> INSN_DATA_SIZE_SHIFT
);
1062 info
->data_size
= 1 << (size
- 1);
1069 /* We did not find a match, so just print the instruction bits in
1070 the shape of an assembler .insn directive. */
1071 info
->insn_type
= dis_noninsn
;
1072 (*info
->fprintf_styled_func
)
1073 (info
->stream
, dis_style_assembler_directive
, ".insn");
1074 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1075 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1077 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, ", ");
1078 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
, "0x");
1079 for (i
= insnlen
, printed
= false; i
>= 2; )
1082 word
= bfd_get_bits (packet
+ i
, 16, false);
1083 if (!word
&& !printed
&& i
)
1086 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1087 "%04x", (unsigned int) word
);
1094 /* Decide if we need to parse the architecture string again, also record the
1095 string into the current subset list. */
1098 riscv_dis_parse_subset (struct disassemble_info
*info
, const char *arch_new
)
1100 struct riscv_private_data
*pd
= info
->private_data
;
1101 const char *arch_subset_list
= pd
->riscv_rps_dis
.subset_list
->arch_str
;
1102 if (arch_subset_list
== NULL
|| strcmp (arch_subset_list
, arch_new
) != 0)
1104 riscv_release_subset_list (pd
->riscv_rps_dis
.subset_list
);
1105 riscv_parse_subset (&pd
->riscv_rps_dis
, arch_new
);
1106 riscv_arch_str (pd
->xlen
, pd
->riscv_rps_dis
.subset_list
,
1111 /* If we find the suitable mapping symbol update the STATE.
1112 Otherwise, do nothing. */
1115 riscv_update_map_state (int n
,
1116 enum riscv_seg_mstate
*state
,
1117 struct disassemble_info
*info
)
1119 struct riscv_private_data
*pd
= info
->private_data
;
1122 /* If the symbol is in a different section, ignore it. */
1123 if (info
->section
!= NULL
1124 && info
->section
!= info
->symtab
[n
]->section
)
1127 name
= bfd_asymbol_name(info
->symtab
[n
]);
1128 if (strcmp (name
, "$d") == 0)
1130 else if (strcmp (name
, "$x") == 0)
1133 riscv_dis_parse_subset (info
, pd
->default_arch
);
1135 else if (strncmp (name
, "$xrv", 4) == 0)
1139 /* ISA mapping string may be numbered, suffixed with '.n'. Do not
1140 consider this as part of the ISA string. */
1141 char *suffix
= strchr (name
, '.');
1144 int suffix_index
= (int)(suffix
- name
);
1145 char *name_substr
= xmalloc (suffix_index
+ 1);
1146 strncpy (name_substr
, name
, suffix_index
);
1147 name_substr
[suffix_index
] = '\0';
1148 riscv_dis_parse_subset (info
, name_substr
+ 2);
1152 riscv_dis_parse_subset (info
, name
+ 2);
1156 /* Return true if we find the suitable mapping symbol.
1157 Otherwise, return false. */
1160 riscv_is_valid_mapping_symbol (int n
,
1161 struct disassemble_info
*info
)
1165 /* If the symbol is in a different section, ignore it. */
1166 if (info
->section
!= NULL
1167 && info
->section
!= info
->symtab
[n
]->section
)
1170 name
= bfd_asymbol_name(info
->symtab
[n
]);
1171 return riscv_elf_is_mapping_symbols (name
);
1174 /* Check the sorted symbol table (sorted by the symbol value), find the
1175 suitable mapping symbols. */
1177 static enum riscv_seg_mstate
1178 riscv_search_mapping_symbol (bfd_vma memaddr
,
1179 struct disassemble_info
*info
)
1181 struct riscv_private_data
*pd
= info
->private_data
;
1182 enum riscv_seg_mstate mstate
;
1183 bool from_last_map_symbol
;
1188 /* Return the last map state if the address is still within the range of the
1189 last mapping symbol. */
1190 if (pd
->last_map_section
== info
->section
1191 && (memaddr
< pd
->last_map_symbol_boundary
))
1192 return pd
->last_map_state
;
1194 pd
->last_map_section
= info
->section
;
1196 /* Decide whether to print the data or instruction by default, in case
1197 we can not find the corresponding mapping symbols. */
1200 && info
->section
->flags
& SEC_CODE
)
1204 if (info
->symtab_size
== 0
1205 || bfd_asymbol_flavour (*info
->symtab
) != bfd_target_elf_flavour
)
1208 /* Reset the last_map_symbol if we start to dump a new section. */
1210 pd
->last_map_symbol
= -1;
1212 /* If the last stop offset is different from the current one, then
1213 don't use the last_map_symbol to search. We usually reset the
1214 info->stop_offset when handling a new section. */
1215 from_last_map_symbol
= (pd
->last_map_symbol
>= 0
1216 && info
->stop_offset
== pd
->last_stop_offset
);
1218 /* Start scanning from wherever we finished last time, or the start
1220 n
= from_last_map_symbol
? pd
->last_map_symbol
: info
->symtab_pos
+ 1;
1222 /* Find the suitable mapping symbol to dump. */
1223 for (; n
< info
->symtab_size
; n
++)
1225 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1226 /* We have searched all possible symbols in the range. */
1229 if (riscv_is_valid_mapping_symbol (n
, info
))
1233 /* Do not stop searching, in case there are some mapping
1234 symbols have the same value, but have different names.
1235 Use the last one. */
1239 /* We can not find the suitable mapping symbol above. Therefore, we
1240 look forwards and try to find it again, but don't go past the start
1241 of the section. Otherwise a data section without mapping symbols
1242 can pick up a text mapping symbol of a preceeding section. */
1245 n
= from_last_map_symbol
? pd
->last_map_symbol
: info
->symtab_pos
;
1249 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1250 /* We have searched all possible symbols in the range. */
1251 if (addr
< (info
->section
? info
->section
->vma
: 0))
1253 /* Stop searching once we find the closed mapping symbol. */
1254 if (riscv_is_valid_mapping_symbol (n
, info
))
1265 riscv_update_map_state (symbol
, &mstate
, info
);
1267 /* Find the next mapping symbol to determine the boundary of this mapping
1270 bool found_next
= false;
1271 /* Try to found next mapping symbol. */
1272 for (n
= symbol
+ 1; n
< info
->symtab_size
; n
++)
1274 if (info
->symtab
[symbol
]->section
!= info
->symtab
[n
]->section
)
1277 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1278 const char *sym_name
= bfd_asymbol_name(info
->symtab
[n
]);
1279 if (sym_name
[0] == '$' && (sym_name
[1] == 'x' || sym_name
[1] == 'd'))
1281 /* The next mapping symbol has been found, and it represents the
1282 boundary of this mapping symbol. */
1284 pd
->last_map_symbol_boundary
= addr
;
1289 /* No further mapping symbol has been found, indicating that the boundary
1290 of the current mapping symbol is the end of this section. */
1292 pd
->last_map_symbol_boundary
= info
->section
->vma
1293 + info
->section
->size
;
1296 /* Save the information for next use. */
1297 pd
->last_map_symbol
= symbol
;
1298 pd
->last_stop_offset
= info
->stop_offset
;
1303 /* Decide which data size we should print. */
1306 riscv_data_length (bfd_vma memaddr
,
1307 disassemble_info
*info
)
1309 struct riscv_private_data
*pd
= info
->private_data
;
1314 if (info
->symtab_size
!= 0
1315 && bfd_asymbol_flavour (*info
->symtab
) == bfd_target_elf_flavour
1316 && pd
->last_map_symbol
>= 0)
1319 enum riscv_seg_mstate m
= MAP_NONE
;
1320 for (n
= pd
->last_map_symbol
+ 1; n
< info
->symtab_size
; n
++)
1322 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
1324 && riscv_is_valid_mapping_symbol (n
, info
))
1326 if (addr
- memaddr
< length
)
1327 length
= addr
- memaddr
;
1329 riscv_update_map_state (n
, &m
, info
);
1336 /* Do not set the length which exceeds the section size. */
1337 bfd_vma offset
= info
->section
->vma
+ info
->section
->size
;
1339 length
= (offset
< length
) ? offset
: length
;
1341 length
= length
== 3 ? 2 : length
;
1345 /* Dump the data contents. */
1348 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED
,
1350 const bfd_byte
*packet ATTRIBUTE_UNUSED
,
1351 disassemble_info
*info
)
1353 info
->display_endian
= info
->endian
;
1356 switch (info
->bytes_per_chunk
)
1359 info
->bytes_per_line
= 6;
1360 (*info
->fprintf_styled_func
)
1361 (info
->stream
, dis_style_assembler_directive
, ".byte");
1362 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1363 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
1364 "0x%02x", (unsigned)data
);
1367 info
->bytes_per_line
= 8;
1368 (*info
->fprintf_styled_func
)
1369 (info
->stream
, dis_style_assembler_directive
, ".short");
1370 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1371 (*info
->fprintf_styled_func
)
1372 (info
->stream
, dis_style_immediate
, "0x%04x", (unsigned) data
);
1375 info
->bytes_per_line
= 8;
1376 (*info
->fprintf_styled_func
)
1377 (info
->stream
, dis_style_assembler_directive
, ".word");
1378 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1379 (*info
->fprintf_styled_func
)
1380 (info
->stream
, dis_style_immediate
, "0x%08lx",
1381 (unsigned long) data
);
1384 info
->bytes_per_line
= 8;
1385 (*info
->fprintf_styled_func
)
1386 (info
->stream
, dis_style_assembler_directive
, ".dword");
1387 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1388 (*info
->fprintf_styled_func
)
1389 (info
->stream
, dis_style_immediate
, "0x%016llx",
1390 (unsigned long long) data
);
1393 /* Arbitrary data so just print the bits in the shape of an .<N>byte
1395 info
->bytes_per_line
= info
->bytes_per_chunk
;
1396 (*info
->fprintf_styled_func
)
1397 (info
->stream
, dis_style_assembler_directive
, ".%dbyte", info
->bytes_per_chunk
);
1398 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "\t");
1399 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
, "0x");
1400 for (i
= info
->bytes_per_line
; i
> 0;)
1403 data
= bfd_get_bits (packet
+ i
, 8, false);
1404 (*info
->fprintf_styled_func
)
1405 (info
->stream
, dis_style_immediate
, "%02x",
1410 return info
->bytes_per_chunk
;
1414 riscv_init_disasm_info (struct disassemble_info
*info
)
1417 struct riscv_private_data
*pd
=
1418 xcalloc (1, sizeof (struct riscv_private_data
));
1421 for (i
= 0; i
< (int) ARRAY_SIZE (pd
->hi_addr
); i
++)
1422 pd
->hi_addr
[i
] = -1;
1423 pd
->to_print_addr
= false;
1426 for (i
= 0; i
< info
->symtab_size
; i
++)
1428 asymbol
*sym
= info
->symtab
[i
];
1429 if (strcmp (bfd_asymbol_name (sym
), RISCV_GP_SYMBOL
) == 0)
1431 pd
->gp
= bfd_asymbol_value (sym
);
1437 pd
->default_isa_spec
= ISA_SPEC_CLASS_DRAFT
- 1;
1438 pd
->default_priv_spec
= PRIV_SPEC_CLASS_NONE
;
1440 pd
->riscv_rps_dis
.subset_list
= xcalloc (1, sizeof (riscv_parse_subset_t
));
1441 pd
->riscv_rps_dis
.error_handler
= opcodes_error_handler
;
1442 pd
->riscv_rps_dis
.xlen
= &pd
->xlen
;
1443 pd
->riscv_rps_dis
.isa_spec
= &pd
->default_isa_spec
;
1444 pd
->riscv_rps_dis
.check_unknown_prefixed_ext
= false;
1445 pd
->default_arch
= "rv64gc";
1446 if (info
->section
!= NULL
)
1448 bfd
*abfd
= info
->section
->owner
;
1449 if (abfd
&& bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1451 const char *sec_name
=
1452 get_elf_backend_data (abfd
)->obj_attrs_section
;
1453 if (bfd_get_section_by_name (abfd
, sec_name
) != NULL
)
1455 obj_attribute
*attr
= elf_known_obj_attributes_proc (abfd
);
1456 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
1457 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
1458 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
1459 riscv_get_priv_spec_class_from_numbers (attr
[Tag_a
].i
,
1462 &pd
->default_priv_spec
);
1463 pd
->default_arch
= attr
[Tag_RISCV_arch
].s
;
1468 pd
->last_map_symbol
= -1;
1469 pd
->last_stop_offset
= 0;
1470 pd
->last_map_symbol_boundary
= 0;
1471 pd
->last_map_state
= MAP_NONE
;
1472 pd
->last_map_section
= NULL
;
1473 pd
->riscv_gpr_names
= NULL
;
1474 pd
->riscv_fpr_names
= NULL
;
1475 pd
->no_aliases
= false;
1476 pd
->all_ext
= false;
1478 info
->private_data
= pd
;
1479 riscv_dis_parse_subset (info
, pd
->default_arch
);
1483 /* Fetch an instruction. If only a partial instruction is able to be fetched,
1484 return the number of accessible bytes. */
1487 fetch_insn (bfd_vma memaddr
,
1490 struct disassemble_info
*info
,
1491 volatile int *status
)
1495 *status
= (*info
->read_memory_func
) (memaddr
, packet
, dump_size
, info
);
1497 while (*status
!= 0 && dump_size
-- > 1);
1503 print_insn_riscv (bfd_vma memaddr
, struct disassemble_info
*info
)
1505 bfd_byte packet
[RISCV_MAX_INSN_LEN
];
1507 bfd_vma dump_size
, bytes_fetched
;
1509 enum riscv_seg_mstate mstate
;
1510 int (*riscv_disassembler
) (bfd_vma
, insn_t
, const bfd_byte
*,
1511 struct disassemble_info
*);
1513 if (info
->private_data
== NULL
&& !riscv_init_disasm_info (info
))
1516 if (info
->disassembler_options
!= NULL
)
1518 parse_riscv_dis_options (info
->disassembler_options
, info
);
1519 /* Avoid repeatedly parsing the options. */
1520 info
->disassembler_options
= NULL
;
1522 else if (((struct riscv_private_data
*) info
->private_data
)->riscv_gpr_names
== NULL
)
1523 set_default_riscv_dis_options (info
);
1525 mstate
= riscv_search_mapping_symbol (memaddr
, info
);
1526 /* Save the last mapping state. */
1527 ((struct riscv_private_data
*) info
->private_data
)->last_map_state
= mstate
;
1529 /* Set the size to dump. */
1530 if (mstate
== MAP_DATA
1531 && (info
->flags
& DISASSEMBLE_DATA
) == 0)
1533 dump_size
= riscv_data_length (memaddr
, info
);
1534 info
->bytes_per_chunk
= dump_size
;
1535 riscv_disassembler
= riscv_disassemble_data
;
1539 /* Get the first 2-bytes to check the lenghth of instruction. */
1540 bytes_fetched
= fetch_insn (memaddr
, packet
, 2, info
, &status
);
1543 (*info
->memory_error_func
) (status
, memaddr
, info
);
1546 else if (bytes_fetched
!= 2)
1548 /* Only the first byte was able to be read. Dump the partial
1550 dump_size
= bytes_fetched
;
1551 info
->bytes_per_chunk
= dump_size
;
1552 riscv_disassembler
= riscv_disassemble_data
;
1555 insn
= (insn_t
) bfd_getl16 (packet
);
1556 dump_size
= riscv_insn_length (insn
);
1557 riscv_disassembler
= riscv_disassemble_insn
;
1560 bytes_fetched
= fetch_insn (memaddr
, packet
, dump_size
, info
, &status
);
1564 (*info
->memory_error_func
) (status
, memaddr
, info
);
1567 else if (bytes_fetched
!= dump_size
)
1569 dump_size
= bytes_fetched
;
1570 info
->bytes_per_chunk
= dump_size
;
1571 riscv_disassembler
= riscv_disassemble_data
;
1576 insn
= (insn_t
) bfd_get_bits (packet
, dump_size
* 8, false);
1578 return (*riscv_disassembler
) (memaddr
, insn
, packet
, info
);
1581 /* Prevent use of the fake labels that are generated as part of the DWARF
1582 and for relaxable relocations in the assembler. */
1585 riscv_symbol_is_valid (asymbol
* sym
,
1586 struct disassemble_info
* info ATTRIBUTE_UNUSED
)
1593 name
= bfd_asymbol_name (sym
);
1595 return (strcmp (name
, RISCV_FAKE_LABEL_NAME
) != 0
1596 && !riscv_elf_is_mapping_symbols (name
));
1600 /* Indices into option argument vector for options accepting an argument.
1601 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1605 RISCV_OPTION_ARG_NONE
= -1,
1606 RISCV_OPTION_ARG_PRIV_SPEC
,
1608 RISCV_OPTION_ARG_COUNT
1609 } riscv_option_arg_t
;
1611 /* Valid RISCV disassembler options. */
1616 const char *description
;
1617 riscv_option_arg_t arg
;
1621 N_("Disassemble without checking architecture string."),
1622 RISCV_OPTION_ARG_NONE
},
1624 N_("Print numeric register names, rather than ABI names."),
1625 RISCV_OPTION_ARG_NONE
},
1627 N_("Disassemble only into canonical instructions."),
1628 RISCV_OPTION_ARG_NONE
},
1630 N_("Print the CSR according to the chosen privilege spec."),
1631 RISCV_OPTION_ARG_PRIV_SPEC
}
1634 /* Build the structure representing valid RISCV disassembler options.
1635 This is done dynamically for maintenance ease purpose; a static
1636 initializer would be unreadable. */
1638 const disasm_options_and_args_t
*
1639 disassembler_options_riscv (void)
1641 static disasm_options_and_args_t
*opts_and_args
;
1643 if (opts_and_args
== NULL
)
1645 size_t num_options
= ARRAY_SIZE (riscv_options
);
1646 size_t num_args
= RISCV_OPTION_ARG_COUNT
;
1647 disasm_option_arg_t
*args
;
1648 disasm_options_t
*opts
;
1649 size_t i
, priv_spec_count
;
1651 args
= XNEWVEC (disasm_option_arg_t
, num_args
+ 1);
1653 args
[RISCV_OPTION_ARG_PRIV_SPEC
].name
= "SPEC";
1654 priv_spec_count
= PRIV_SPEC_CLASS_DRAFT
- PRIV_SPEC_EARLIEST
;
1655 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
1656 = XNEWVEC (const char *, priv_spec_count
+ 1);
1657 for (i
= 0; i
< priv_spec_count
; i
++)
1658 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
]
1659 = riscv_priv_specs
[PRIV_SPEC_EARLIEST
- PRIV_SPEC_CLASS_NONE
- 1 + i
].name
;
1660 /* The array we return must be NULL terminated. */
1661 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
] = NULL
;
1663 /* The array we return must be NULL terminated. */
1664 args
[num_args
].name
= NULL
;
1665 args
[num_args
].values
= NULL
;
1667 opts_and_args
= XNEW (disasm_options_and_args_t
);
1668 opts_and_args
->args
= args
;
1670 opts
= &opts_and_args
->options
;
1671 opts
->name
= XNEWVEC (const char *, num_options
+ 1);
1672 opts
->description
= XNEWVEC (const char *, num_options
+ 1);
1673 opts
->arg
= XNEWVEC (const disasm_option_arg_t
*, num_options
+ 1);
1674 for (i
= 0; i
< num_options
; i
++)
1676 opts
->name
[i
] = riscv_options
[i
].name
;
1677 opts
->description
[i
] = _(riscv_options
[i
].description
);
1678 if (riscv_options
[i
].arg
!= RISCV_OPTION_ARG_NONE
)
1679 opts
->arg
[i
] = &args
[riscv_options
[i
].arg
];
1681 opts
->arg
[i
] = NULL
;
1683 /* The array we return must be NULL terminated. */
1684 opts
->name
[i
] = NULL
;
1685 opts
->description
[i
] = NULL
;
1686 opts
->arg
[i
] = NULL
;
1689 return opts_and_args
;
1693 print_riscv_disassembler_options (FILE *stream
)
1695 const disasm_options_and_args_t
*opts_and_args
;
1696 const disasm_option_arg_t
*args
;
1697 const disasm_options_t
*opts
;
1702 opts_and_args
= disassembler_options_riscv ();
1703 opts
= &opts_and_args
->options
;
1704 args
= opts_and_args
->args
;
1706 fprintf (stream
, _("\n\
1707 The following RISC-V specific disassembler options are supported for use\n\
1708 with the -M switch (multiple options should be separated by commas):\n"));
1709 fprintf (stream
, "\n");
1711 /* Compute the length of the longest option name. */
1712 for (i
= 0; opts
->name
[i
] != NULL
; i
++)
1714 size_t len
= strlen (opts
->name
[i
]);
1716 if (opts
->arg
[i
] != NULL
)
1717 len
+= strlen (opts
->arg
[i
]->name
);
1722 for (i
= 0, max_len
++; opts
->name
[i
] != NULL
; i
++)
1724 fprintf (stream
, " %s", opts
->name
[i
]);
1725 if (opts
->arg
[i
] != NULL
)
1726 fprintf (stream
, "%s", opts
->arg
[i
]->name
);
1727 if (opts
->description
[i
] != NULL
)
1729 size_t len
= strlen (opts
->name
[i
]);
1731 if (opts
->arg
!= NULL
&& opts
->arg
[i
] != NULL
)
1732 len
+= strlen (opts
->arg
[i
]->name
);
1733 fprintf (stream
, "%*c %s", (int) (max_len
- len
), ' ',
1734 opts
->description
[i
]);
1736 fprintf (stream
, "\n");
1739 for (i
= 0; args
[i
].name
!= NULL
; i
++)
1741 if (args
[i
].values
== NULL
)
1743 fprintf (stream
, _("\n\
1744 For the options above, the following values are supported for \"%s\":\n "),
1746 for (j
= 0; args
[i
].values
[j
] != NULL
; j
++)
1747 fprintf (stream
, " %s", args
[i
].values
[j
]);
1748 fprintf (stream
, _("\n"));
1751 fprintf (stream
, _("\n"));
1754 void disassemble_free_riscv (struct disassemble_info
*info ATTRIBUTE_UNUSED
)
1756 struct riscv_private_data
*pd
= info
->private_data
;
1759 riscv_release_subset_list (pd
->riscv_rps_dis
.subset_list
);
1760 free (pd
->riscv_rps_dis
.subset_list
);