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 static enum riscv_spec_class default_isa_spec
= ISA_SPEC_CLASS_DRAFT
- 1;
36 static enum riscv_spec_class default_priv_spec
= PRIV_SPEC_CLASS_NONE
;
40 static riscv_subset_list_t riscv_subsets
;
41 static riscv_parse_subset_t riscv_rps_dis
=
43 &riscv_subsets
, /* subset_list. */
44 opcodes_error_handler
,/* error_handler. */
46 &default_isa_spec
, /* isa_spec. */
47 false, /* check_unknown_prefixed_ext. */
50 struct riscv_private_data
54 bfd_vma hi_addr
[OP_MASK_RD
+ 1];
59 /* Used for mapping symbols. */
60 static int last_map_symbol
= -1;
61 static bfd_vma last_stop_offset
= 0;
62 enum riscv_seg_mstate last_map_state
;
64 static const char * const *riscv_gpr_names
;
65 static const char * const *riscv_fpr_names
;
67 /* If set, disassemble as most general instruction. */
68 static int no_aliases
;
71 set_default_riscv_dis_options (void)
73 riscv_gpr_names
= riscv_gpr_names_abi
;
74 riscv_fpr_names
= riscv_fpr_names_abi
;
79 parse_riscv_dis_option_without_args (const char *option
)
81 if (strcmp (option
, "no-aliases") == 0)
83 else if (strcmp (option
, "numeric") == 0)
85 riscv_gpr_names
= riscv_gpr_names_numeric
;
86 riscv_fpr_names
= riscv_fpr_names_numeric
;
94 parse_riscv_dis_option (const char *option
)
98 if (parse_riscv_dis_option_without_args (option
))
101 equal
= strchr (option
, '=');
104 /* The option without '=' should be defined above. */
105 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
109 || *(equal
+ 1) == '\0')
111 /* Invalid options with '=', no option name before '=',
112 and no value after '='. */
113 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
120 if (strcmp (option
, "priv-spec") == 0)
122 enum riscv_spec_class priv_spec
= PRIV_SPEC_CLASS_NONE
;
123 const char *name
= NULL
;
125 RISCV_GET_PRIV_SPEC_CLASS (value
, priv_spec
);
126 if (priv_spec
== PRIV_SPEC_CLASS_NONE
)
127 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
129 else if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
130 default_priv_spec
= priv_spec
;
131 else if (default_priv_spec
!= priv_spec
)
133 RISCV_GET_PRIV_SPEC_NAME (name
, default_priv_spec
);
134 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
135 "the elf privilege attribute is %s"),
136 option
, value
, name
);
141 /* xgettext:c-format */
142 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
147 parse_riscv_dis_options (const char *opts_in
)
149 char *opts
= xstrdup (opts_in
), *opt
= opts
, *opt_end
= opts
;
151 set_default_riscv_dis_options ();
153 for ( ; opt_end
!= NULL
; opt
= opt_end
+ 1)
155 if ((opt_end
= strchr (opt
, ',')) != NULL
)
157 parse_riscv_dis_option (opt
);
163 /* Print one argument from an array. */
166 arg_print (struct disassemble_info
*info
, unsigned long val
,
167 const char* const* array
, size_t size
)
169 const char *s
= val
>= size
|| array
[val
] == NULL
? "unknown" : array
[val
];
170 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
, "%s", s
);
174 maybe_print_address (struct riscv_private_data
*pd
, int base_reg
, int offset
,
177 if (pd
->hi_addr
[base_reg
] != (bfd_vma
)-1)
179 pd
->print_addr
= (base_reg
!= 0 ? pd
->hi_addr
[base_reg
] : 0) + offset
;
180 pd
->hi_addr
[base_reg
] = -1;
182 else if (base_reg
== X_GP
&& pd
->has_gp
)
183 pd
->print_addr
= pd
->gp
+ offset
;
184 else if (base_reg
== X_TP
|| base_reg
== 0)
185 pd
->print_addr
= offset
;
187 return; /* Don't print the address. */
188 pd
->to_print_addr
= true;
190 /* Sign-extend a 32-bit value to a 64-bit value. */
192 pd
->print_addr
= (bfd_vma
)(int32_t) pd
->print_addr
;
194 /* Fit into a 32-bit value on RV32. */
196 pd
->print_addr
= (bfd_vma
)(uint32_t)pd
->print_addr
;
199 /* Print insn arguments for 32/64-bit code. */
202 print_insn_args (const char *oparg
, insn_t l
, bfd_vma pc
, disassemble_info
*info
)
204 struct riscv_private_data
*pd
= info
->private_data
;
205 int rs1
= (l
>> OP_SH_RS1
) & OP_MASK_RS1
;
206 int rd
= (l
>> OP_SH_RD
) & OP_MASK_RD
;
207 fprintf_styled_ftype print
= info
->fprintf_styled_func
;
208 const char *opargStart
;
211 print (info
->stream
, dis_style_text
, "\t");
213 for (; *oparg
!= '\0'; oparg
++)
221 case 's': /* RS1 x8-x15. */
222 case 'w': /* RS1 x8-x15. */
223 print (info
->stream
, dis_style_register
, "%s",
224 riscv_gpr_names
[EXTRACT_OPERAND (CRS1S
, l
) + 8]);
226 case 't': /* RS2 x8-x15. */
227 case 'x': /* RS2 x8-x15. */
228 print (info
->stream
, dis_style_register
, "%s",
229 riscv_gpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
231 case 'U': /* RS1, constrained to equal RD. */
232 print (info
->stream
, dis_style_register
,
233 "%s", riscv_gpr_names
[rd
]);
235 case 'c': /* RS1, constrained to equal sp. */
236 print (info
->stream
, dis_style_register
, "%s",
237 riscv_gpr_names
[X_SP
]);
240 print (info
->stream
, dis_style_register
, "%s",
241 riscv_gpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
245 if (((l
& MASK_C_ADDI
) == MATCH_C_ADDI
) && rd
!= 0)
246 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 0);
247 if (info
->mach
== bfd_mach_riscv64
248 && ((l
& MASK_C_ADDIW
) == MATCH_C_ADDIW
) && rd
!= 0)
249 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 1);
250 print (info
->stream
, dis_style_immediate
, "%d",
251 (int)EXTRACT_CITYPE_IMM (l
));
254 print (info
->stream
, dis_style_address_offset
, "%d",
255 (int)EXTRACT_CLTYPE_LW_IMM (l
));
258 print (info
->stream
, dis_style_address_offset
, "%d",
259 (int)EXTRACT_CLTYPE_LD_IMM (l
));
262 print (info
->stream
, dis_style_address_offset
, "%d",
263 (int)EXTRACT_CITYPE_LWSP_IMM (l
));
266 print (info
->stream
, dis_style_address_offset
, "%d",
267 (int)EXTRACT_CITYPE_LDSP_IMM (l
));
270 print (info
->stream
, dis_style_immediate
, "%d",
271 (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l
));
274 print (info
->stream
, dis_style_immediate
, "%d",
275 (int)EXTRACT_CITYPE_ADDI16SP_IMM (l
));
278 print (info
->stream
, dis_style_address_offset
, "%d",
279 (int)EXTRACT_CSSTYPE_SWSP_IMM (l
));
282 print (info
->stream
, dis_style_address_offset
, "%d",
283 (int)EXTRACT_CSSTYPE_SDSP_IMM (l
));
286 info
->target
= EXTRACT_CBTYPE_IMM (l
) + pc
;
287 (*info
->print_address_func
) (info
->target
, info
);
290 info
->target
= EXTRACT_CJTYPE_IMM (l
) + pc
;
291 (*info
->print_address_func
) (info
->target
, info
);
294 print (info
->stream
, dis_style_immediate
, "0x%x",
295 (int)(EXTRACT_CITYPE_IMM (l
) & (RISCV_BIGIMM_REACH
-1)));
298 print (info
->stream
, dis_style_immediate
, "0x%x",
299 (int)EXTRACT_CITYPE_IMM (l
) & 0x3f);
302 print (info
->stream
, dis_style_immediate
, "0x%x",
303 (int)EXTRACT_CITYPE_IMM (l
) & 0x1f);
305 case 'T': /* Floating-point RS2. */
306 print (info
->stream
, dis_style_register
, "%s",
307 riscv_fpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
309 case 'D': /* Floating-point RS2 x8-x15. */
310 print (info
->stream
, dis_style_register
, "%s",
311 riscv_fpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
321 print (info
->stream
, dis_style_register
, "%s",
322 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
325 if (!EXTRACT_OPERAND (VWD
, l
))
326 print (info
->stream
, dis_style_register
, "%s",
329 print (info
->stream
, dis_style_register
, "%s",
330 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
333 print (info
->stream
, dis_style_register
, "%s",
334 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS1
, l
)]);
337 case 'u': /* VS1 == VS2 already verified at this point. */
338 case 'v': /* VD == VS1 == VS2 already verified at this point. */
339 print (info
->stream
, dis_style_register
, "%s",
340 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS2
, l
)]);
343 print (info
->stream
, dis_style_register
, "%s",
344 riscv_vecr_names_numeric
[0]);
349 int imm
= (*oparg
== 'b') ? EXTRACT_RVV_VB_IMM (l
)
350 : EXTRACT_RVV_VC_IMM (l
);
351 unsigned int imm_vlmul
= EXTRACT_OPERAND (VLMUL
, imm
);
352 unsigned int imm_vsew
= EXTRACT_OPERAND (VSEW
, imm
);
353 unsigned int imm_vta
= EXTRACT_OPERAND (VTA
, imm
);
354 unsigned int imm_vma
= EXTRACT_OPERAND (VMA
, imm
);
355 unsigned int imm_vtype_res
= (imm
>> 8);
357 if (imm_vsew
< ARRAY_SIZE (riscv_vsew
)
358 && imm_vlmul
< ARRAY_SIZE (riscv_vlmul
)
359 && imm_vta
< ARRAY_SIZE (riscv_vta
)
360 && imm_vma
< ARRAY_SIZE (riscv_vma
)
362 && riscv_vsew
[imm_vsew
] != NULL
363 && riscv_vlmul
[imm_vlmul
] != NULL
)
364 print (info
->stream
, dis_style_text
, "%s,%s,%s,%s",
365 riscv_vsew
[imm_vsew
],
366 riscv_vlmul
[imm_vlmul
], riscv_vta
[imm_vta
],
369 print (info
->stream
, dis_style_immediate
, "%d", imm
);
373 print (info
->stream
, dis_style_immediate
, "%d",
374 (int)EXTRACT_RVV_VI_IMM (l
));
377 print (info
->stream
, dis_style_immediate
, "%d",
378 (int)EXTRACT_RVV_VI_UIMM (l
));
381 print (info
->stream
, dis_style_immediate
, "%d",
382 (int)EXTRACT_RVV_OFFSET (l
));
385 if (! EXTRACT_OPERAND (VMASK
, l
))
386 print (info
->stream
, dis_style_register
, ",%s",
387 riscv_vecm_names_numeric
[0]);
397 print (info
->stream
, dis_style_text
, "%c", *oparg
);
401 /* Only print constant 0 if it is the last argument. */
403 print (info
->stream
, dis_style_immediate
, "0");
408 if ((l
& MASK_JALR
) == MATCH_JALR
)
409 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
410 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[rs1
]);
414 print (info
->stream
, dis_style_register
, "%s",
415 riscv_gpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
419 print (info
->stream
, dis_style_immediate
, "0x%x",
420 (unsigned)EXTRACT_UTYPE_IMM (l
) >> RISCV_IMM_BITS
);
424 arg_print (info
, EXTRACT_OPERAND (RM
, l
),
425 riscv_rm
, ARRAY_SIZE (riscv_rm
));
429 arg_print (info
, EXTRACT_OPERAND (PRED
, l
),
430 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
434 arg_print (info
, EXTRACT_OPERAND (SUCC
, l
),
435 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
439 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
442 if (((l
& MASK_ADDI
) == MATCH_ADDI
&& rs1
!= 0)
443 || (l
& MASK_JALR
) == MATCH_JALR
)
444 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
445 if (info
->mach
== bfd_mach_riscv64
446 && ((l
& MASK_ADDIW
) == MATCH_ADDIW
) && rs1
!= 0)
447 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 1);
448 print (info
->stream
, dis_style_immediate
, "%d",
449 (int)EXTRACT_ITYPE_IMM (l
));
453 maybe_print_address (pd
, rs1
, EXTRACT_STYPE_IMM (l
), 0);
454 print (info
->stream
, dis_style_address_offset
, "%d",
455 (int)EXTRACT_STYPE_IMM (l
));
459 print (info
->stream
, dis_style_address_offset
, "%d",
460 (int)EXTRACT_STYPE_IMM (l
));
464 info
->target
= EXTRACT_JTYPE_IMM (l
) + pc
;
465 (*info
->print_address_func
) (info
->target
, info
);
469 info
->target
= EXTRACT_BTYPE_IMM (l
) + pc
;
470 (*info
->print_address_func
) (info
->target
, info
);
474 if ((l
& MASK_AUIPC
) == MATCH_AUIPC
)
475 pd
->hi_addr
[rd
] = pc
+ EXTRACT_UTYPE_IMM (l
);
476 else if ((l
& MASK_LUI
) == MATCH_LUI
)
477 pd
->hi_addr
[rd
] = EXTRACT_UTYPE_IMM (l
);
478 else if ((l
& MASK_C_LUI
) == MATCH_C_LUI
)
479 pd
->hi_addr
[rd
] = EXTRACT_CITYPE_LUI_IMM (l
);
480 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[rd
]);
484 print (info
->stream
, dis_style_text
, "0x%x",
485 (int)EXTRACT_OPERAND (BS
, l
));
489 print (info
->stream
, dis_style_register
, "%s", riscv_gpr_names
[0]);
493 print (info
->stream
, dis_style_immediate
, "0x%x",
494 (int)EXTRACT_OPERAND (SHAMT
, l
));
498 print (info
->stream
, dis_style_immediate
, "0x%x",
499 (int)EXTRACT_OPERAND (SHAMTW
, l
));
504 print (info
->stream
, dis_style_register
, "%s", riscv_fpr_names
[rs1
]);
508 print (info
->stream
, dis_style_register
, "%s",
509 riscv_fpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
513 print (info
->stream
, dis_style_register
, "%s", riscv_fpr_names
[rd
]);
517 print (info
->stream
, dis_style_register
, "%s",
518 riscv_fpr_names
[EXTRACT_OPERAND (RS3
, l
)]);
523 static const char *riscv_csr_hash
[4096]; /* Total 2^12 CSRs. */
524 static bool init_csr
= false;
525 unsigned int csr
= EXTRACT_OPERAND (CSR
, l
);
530 for (i
= 0; i
< 4096; i
++)
531 riscv_csr_hash
[i
] = NULL
;
533 /* Set to the newest privileged version. */
534 if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
535 default_priv_spec
= PRIV_SPEC_CLASS_DRAFT
- 1;
537 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
538 if (riscv_csr_hash[num] == NULL \
539 && ((define_version == PRIV_SPEC_CLASS_NONE \
540 && abort_version == PRIV_SPEC_CLASS_NONE) \
541 || (default_priv_spec >= define_version \
542 && default_priv_spec < abort_version))) \
543 riscv_csr_hash[num] = #name;
544 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
545 DECLARE_CSR (name, num, class, define_version, abort_version)
546 #include "opcode/riscv-opc.h"
550 if (riscv_csr_hash
[csr
] != NULL
)
551 print (info
->stream
, dis_style_text
, "%s", riscv_csr_hash
[csr
]);
553 print (info
->stream
, dis_style_text
, "0x%x", csr
);
558 print (info
->stream
, dis_style_text
, "0x%x",
559 (int) EXTRACT_OPERAND (RNUM
, l
));
563 print (info
->stream
, dis_style_text
, "%d", rs1
);
567 /* xgettext:c-format */
568 print (info
->stream
, dis_style_text
,
569 _("# internal error, undefined modifier (%c)"),
576 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
577 on using INFO. Returns length of the instruction, in bytes.
578 BIGENDIAN must be 1 if this is big-endian code, 0 if
579 this is little-endian code. */
582 riscv_disassemble_insn (bfd_vma memaddr
, insn_t word
, disassemble_info
*info
)
584 const struct riscv_opcode
*op
;
585 static bool init
= 0;
586 static const struct riscv_opcode
*riscv_hash
[OP_MASK_OP
+ 1];
587 struct riscv_private_data
*pd
;
590 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
592 /* Build a hash table to shorten the search time. */
595 for (op
= riscv_opcodes
; op
->name
; op
++)
596 if (!riscv_hash
[OP_HASH_IDX (op
->match
)])
597 riscv_hash
[OP_HASH_IDX (op
->match
)] = op
;
602 if (info
->private_data
== NULL
)
606 pd
= info
->private_data
= xcalloc (1, sizeof (struct riscv_private_data
));
609 for (i
= 0; i
< (int)ARRAY_SIZE (pd
->hi_addr
); i
++)
611 pd
->to_print_addr
= false;
614 for (i
= 0; i
< info
->symtab_size
; i
++)
615 if (strcmp (bfd_asymbol_name (info
->symtab
[i
]), RISCV_GP_SYMBOL
) == 0)
617 pd
->gp
= bfd_asymbol_value (info
->symtab
[i
]);
622 pd
= info
->private_data
;
624 insnlen
= riscv_insn_length (word
);
626 /* RISC-V instructions are always little-endian. */
627 info
->endian_code
= BFD_ENDIAN_LITTLE
;
629 info
->bytes_per_chunk
= insnlen
% 4 == 0 ? 4 : 2;
630 info
->bytes_per_line
= 8;
631 /* We don't support constant pools, so this must be code. */
632 info
->display_endian
= info
->endian_code
;
633 info
->insn_info_valid
= 1;
634 info
->branch_delay_insns
= 0;
636 info
->insn_type
= dis_nonbranch
;
640 op
= riscv_hash
[OP_HASH_IDX (word
)];
643 /* If XLEN is not known, get its value from the ELF class. */
644 if (info
->mach
== bfd_mach_riscv64
)
646 else if (info
->mach
== bfd_mach_riscv32
)
648 else if (info
->section
!= NULL
)
650 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (info
->section
->owner
);
651 xlen
= ehdr
->e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
654 /* If arch has ZFINX flags, use gpr for disassemble. */
655 if(riscv_subset_supports (&riscv_rps_dis
, "zfinx"))
656 riscv_fpr_names
= riscv_gpr_names
;
658 for (; op
->name
; op
++)
660 /* Does the opcode match? */
661 if (! (op
->match_func
) (op
, word
))
663 /* Is this a pseudo-instruction and may we print it as such? */
664 if (no_aliases
&& (op
->pinfo
& INSN_ALIAS
))
666 /* Is this instruction restricted to a certain value of XLEN? */
667 if ((op
->xlen_requirement
!= 0) && (op
->xlen_requirement
!= xlen
))
670 if (!riscv_multi_subset_supports (&riscv_rps_dis
, op
->insn_class
))
674 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_mnemonic
,
676 print_insn_args (op
->args
, word
, memaddr
, info
);
678 /* Try to disassemble multi-instruction addressing sequences. */
679 if (pd
->to_print_addr
)
681 info
->target
= pd
->print_addr
;
682 (*info
->fprintf_styled_func
)
683 (info
->stream
, dis_style_comment_start
, " # ");
684 (*info
->print_address_func
) (info
->target
, info
);
685 pd
->to_print_addr
= false;
688 /* Finish filling out insn_info fields. */
689 switch (op
->pinfo
& INSN_TYPE
)
692 info
->insn_type
= dis_branch
;
694 case INSN_CONDBRANCH
:
695 info
->insn_type
= dis_condbranch
;
698 info
->insn_type
= dis_jsr
;
701 info
->insn_type
= dis_dref
;
707 if (op
->pinfo
& INSN_DATA_SIZE
)
709 int size
= ((op
->pinfo
& INSN_DATA_SIZE
)
710 >> INSN_DATA_SIZE_SHIFT
);
711 info
->data_size
= 1 << (size
- 1);
718 /* We did not find a match, so just print the instruction bits. */
719 info
->insn_type
= dis_noninsn
;
725 (*info
->fprintf_styled_func
)
726 (info
->stream
, dis_style_assembler_directive
, ".%dbyte\t", insnlen
);
727 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
728 "0x%llx", (unsigned long long) word
);
733 (*info
->fprintf_styled_func
)
734 (info
->stream
, dis_style_assembler_directive
, ".byte\t");
735 for (i
= 0; i
< insnlen
; ++i
)
738 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_text
,
740 (*info
->fprintf_styled_func
) (info
->stream
, dis_style_immediate
,
742 (unsigned int) (word
& 0xff));
751 /* Return true if we find the suitable mapping symbol,
752 and also update the STATE. Otherwise, return false. */
755 riscv_get_map_state (int n
,
756 enum riscv_seg_mstate
*state
,
757 struct disassemble_info
*info
)
761 /* If the symbol is in a different section, ignore it. */
762 if (info
->section
!= NULL
763 && info
->section
!= info
->symtab
[n
]->section
)
766 name
= bfd_asymbol_name(info
->symtab
[n
]);
767 if (strcmp (name
, "$x") == 0)
769 else if (strcmp (name
, "$d") == 0)
777 /* Check the sorted symbol table (sorted by the symbol value), find the
778 suitable mapping symbols. */
780 static enum riscv_seg_mstate
781 riscv_search_mapping_symbol (bfd_vma memaddr
,
782 struct disassemble_info
*info
)
784 enum riscv_seg_mstate mstate
;
785 bool from_last_map_symbol
;
790 /* Decide whether to print the data or instruction by default, in case
791 we can not find the corresponding mapping symbols. */
794 && info
->section
->flags
& SEC_CODE
)
798 if (info
->symtab_size
== 0
799 || bfd_asymbol_flavour (*info
->symtab
) != bfd_target_elf_flavour
)
802 /* Reset the last_map_symbol if we start to dump a new section. */
804 last_map_symbol
= -1;
806 /* If the last stop offset is different from the current one, then
807 don't use the last_map_symbol to search. We usually reset the
808 info->stop_offset when handling a new section. */
809 from_last_map_symbol
= (last_map_symbol
>= 0
810 && info
->stop_offset
== last_stop_offset
);
812 /* Start scanning at the start of the function, or wherever
813 we finished last time. */
814 n
= info
->symtab_pos
+ 1;
815 if (from_last_map_symbol
&& n
>= last_map_symbol
)
818 /* Find the suitable mapping symbol to dump. */
819 for (; n
< info
->symtab_size
; n
++)
821 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
822 /* We have searched all possible symbols in the range. */
825 if (riscv_get_map_state (n
, &mstate
, info
))
829 /* Do not stop searching, in case there are some mapping
830 symbols have the same value, but have different names.
835 /* We can not find the suitable mapping symbol above. Therefore, we
836 look forwards and try to find it again, but don't go pass the start
837 of the section. Otherwise a data section without mapping symbols
838 can pick up a text mapping symbol of a preceeding section. */
841 n
= info
->symtab_pos
;
842 if (from_last_map_symbol
&& n
>= last_map_symbol
)
847 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
848 /* We have searched all possible symbols in the range. */
849 if (addr
< (info
->section
? info
->section
->vma
: 0))
851 /* Stop searching once we find the closed mapping symbol. */
852 if (riscv_get_map_state (n
, &mstate
, info
))
861 /* Save the information for next use. */
862 last_map_symbol
= symbol
;
863 last_stop_offset
= info
->stop_offset
;
868 /* Decide which data size we should print. */
871 riscv_data_length (bfd_vma memaddr
,
872 disassemble_info
*info
)
878 if (info
->symtab_size
!= 0
879 && bfd_asymbol_flavour (*info
->symtab
) == bfd_target_elf_flavour
880 && last_map_symbol
>= 0)
883 enum riscv_seg_mstate m
= MAP_NONE
;
884 for (n
= last_map_symbol
+ 1; n
< info
->symtab_size
; n
++)
886 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
888 && riscv_get_map_state (n
, &m
, info
))
890 if (addr
- memaddr
< length
)
891 length
= addr
- memaddr
;
899 /* Do not set the length which exceeds the section size. */
900 bfd_vma offset
= info
->section
->vma
+ info
->section
->size
;
902 length
= (offset
< length
) ? offset
: length
;
904 length
= length
== 3 ? 2 : length
;
908 /* Dump the data contents. */
911 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED
,
913 disassemble_info
*info
)
915 info
->display_endian
= info
->endian
;
917 switch (info
->bytes_per_chunk
)
920 info
->bytes_per_line
= 6;
921 (*info
->fprintf_styled_func
)
922 (info
->stream
, dis_style_assembler_directive
, ".byte\t");
923 (*info
->fprintf_styled_func
)
924 (info
->stream
, dis_style_assembler_directive
, "0x%02llx",
925 (unsigned long long) data
);
928 info
->bytes_per_line
= 8;
929 (*info
->fprintf_styled_func
)
930 (info
->stream
, dis_style_assembler_directive
, ".short\t");
931 (*info
->fprintf_styled_func
)
932 (info
->stream
, dis_style_immediate
, "0x%04llx",
933 (unsigned long long) data
);
936 info
->bytes_per_line
= 8;
937 (*info
->fprintf_styled_func
)
938 (info
->stream
, dis_style_assembler_directive
, ".word\t");
939 (*info
->fprintf_styled_func
)
940 (info
->stream
, dis_style_immediate
, "0x%08llx",
941 (unsigned long long) data
);
944 info
->bytes_per_line
= 8;
945 (*info
->fprintf_styled_func
)
946 (info
->stream
, dis_style_assembler_directive
, ".dword\t");
947 (*info
->fprintf_styled_func
)
948 (info
->stream
, dis_style_immediate
, "0x%016llx",
949 (unsigned long long) data
);
954 return info
->bytes_per_chunk
;
958 print_insn_riscv (bfd_vma memaddr
, struct disassemble_info
*info
)
964 enum riscv_seg_mstate mstate
;
965 int (*riscv_disassembler
) (bfd_vma
, insn_t
, struct disassemble_info
*);
967 if (info
->disassembler_options
!= NULL
)
969 parse_riscv_dis_options (info
->disassembler_options
);
970 /* Avoid repeatedly parsing the options. */
971 info
->disassembler_options
= NULL
;
973 else if (riscv_gpr_names
== NULL
)
974 set_default_riscv_dis_options ();
976 mstate
= riscv_search_mapping_symbol (memaddr
, info
);
977 /* Save the last mapping state. */
978 last_map_state
= mstate
;
980 /* Set the size to dump. */
981 if (mstate
== MAP_DATA
982 && (info
->flags
& DISASSEMBLE_DATA
) == 0)
984 dump_size
= riscv_data_length (memaddr
, info
);
985 info
->bytes_per_chunk
= dump_size
;
986 riscv_disassembler
= riscv_disassemble_data
;
990 /* Get the first 2-bytes to check the lenghth of instruction. */
991 status
= (*info
->read_memory_func
) (memaddr
, packet
, 2, info
);
994 (*info
->memory_error_func
) (status
, memaddr
, info
);
997 insn
= (insn_t
) bfd_getl16 (packet
);
998 dump_size
= riscv_insn_length (insn
);
999 riscv_disassembler
= riscv_disassemble_insn
;
1002 /* Fetch the instruction to dump. */
1003 status
= (*info
->read_memory_func
) (memaddr
, packet
, dump_size
, info
);
1006 (*info
->memory_error_func
) (status
, memaddr
, info
);
1009 insn
= (insn_t
) bfd_get_bits (packet
, dump_size
* 8, false);
1011 return (*riscv_disassembler
) (memaddr
, insn
, info
);
1015 riscv_get_disassembler (bfd
*abfd
)
1017 const char *default_arch
= "rv64gc";
1019 if (abfd
&& bfd_get_flavour (abfd
) == bfd_target_elf_flavour
)
1021 const char *sec_name
= get_elf_backend_data (abfd
)->obj_attrs_section
;
1022 if (bfd_get_section_by_name (abfd
, sec_name
) != NULL
)
1024 obj_attribute
*attr
= elf_known_obj_attributes_proc (abfd
);
1025 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
1026 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
1027 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
1028 riscv_get_priv_spec_class_from_numbers (attr
[Tag_a
].i
,
1031 &default_priv_spec
);
1032 default_arch
= attr
[Tag_RISCV_arch
].s
;
1036 riscv_release_subset_list (&riscv_subsets
);
1037 riscv_parse_subset (&riscv_rps_dis
, default_arch
);
1038 return print_insn_riscv
;
1041 /* Prevent use of the fake labels that are generated as part of the DWARF
1042 and for relaxable relocations in the assembler. */
1045 riscv_symbol_is_valid (asymbol
* sym
,
1046 struct disassemble_info
* info ATTRIBUTE_UNUSED
)
1053 name
= bfd_asymbol_name (sym
);
1055 return (strcmp (name
, RISCV_FAKE_LABEL_NAME
) != 0
1056 && !riscv_elf_is_mapping_symbols (name
));
1060 /* Indices into option argument vector for options accepting an argument.
1061 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1065 RISCV_OPTION_ARG_NONE
= -1,
1066 RISCV_OPTION_ARG_PRIV_SPEC
,
1068 RISCV_OPTION_ARG_COUNT
1069 } riscv_option_arg_t
;
1071 /* Valid RISCV disassembler options. */
1076 const char *description
;
1077 riscv_option_arg_t arg
;
1081 N_("Print numeric register names, rather than ABI names."),
1082 RISCV_OPTION_ARG_NONE
},
1084 N_("Disassemble only into canonical instructions."),
1085 RISCV_OPTION_ARG_NONE
},
1087 N_("Print the CSR according to the chosen privilege spec."),
1088 RISCV_OPTION_ARG_PRIV_SPEC
}
1091 /* Build the structure representing valid RISCV disassembler options.
1092 This is done dynamically for maintenance ease purpose; a static
1093 initializer would be unreadable. */
1095 const disasm_options_and_args_t
*
1096 disassembler_options_riscv (void)
1098 static disasm_options_and_args_t
*opts_and_args
;
1100 if (opts_and_args
== NULL
)
1102 size_t num_options
= ARRAY_SIZE (riscv_options
);
1103 size_t num_args
= RISCV_OPTION_ARG_COUNT
;
1104 disasm_option_arg_t
*args
;
1105 disasm_options_t
*opts
;
1106 size_t i
, priv_spec_count
;
1108 args
= XNEWVEC (disasm_option_arg_t
, num_args
+ 1);
1110 args
[RISCV_OPTION_ARG_PRIV_SPEC
].name
= "SPEC";
1111 priv_spec_count
= PRIV_SPEC_CLASS_DRAFT
- PRIV_SPEC_CLASS_NONE
- 1;
1112 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
1113 = XNEWVEC (const char *, priv_spec_count
+ 1);
1114 for (i
= 0; i
< priv_spec_count
; i
++)
1115 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
]
1116 = riscv_priv_specs
[i
].name
;
1117 /* The array we return must be NULL terminated. */
1118 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
] = NULL
;
1120 /* The array we return must be NULL terminated. */
1121 args
[num_args
].name
= NULL
;
1122 args
[num_args
].values
= NULL
;
1124 opts_and_args
= XNEW (disasm_options_and_args_t
);
1125 opts_and_args
->args
= args
;
1127 opts
= &opts_and_args
->options
;
1128 opts
->name
= XNEWVEC (const char *, num_options
+ 1);
1129 opts
->description
= XNEWVEC (const char *, num_options
+ 1);
1130 opts
->arg
= XNEWVEC (const disasm_option_arg_t
*, num_options
+ 1);
1131 for (i
= 0; i
< num_options
; i
++)
1133 opts
->name
[i
] = riscv_options
[i
].name
;
1134 opts
->description
[i
] = _(riscv_options
[i
].description
);
1135 if (riscv_options
[i
].arg
!= RISCV_OPTION_ARG_NONE
)
1136 opts
->arg
[i
] = &args
[riscv_options
[i
].arg
];
1138 opts
->arg
[i
] = NULL
;
1140 /* The array we return must be NULL terminated. */
1141 opts
->name
[i
] = NULL
;
1142 opts
->description
[i
] = NULL
;
1143 opts
->arg
[i
] = NULL
;
1146 return opts_and_args
;
1150 print_riscv_disassembler_options (FILE *stream
)
1152 const disasm_options_and_args_t
*opts_and_args
;
1153 const disasm_option_arg_t
*args
;
1154 const disasm_options_t
*opts
;
1159 opts_and_args
= disassembler_options_riscv ();
1160 opts
= &opts_and_args
->options
;
1161 args
= opts_and_args
->args
;
1163 fprintf (stream
, _("\n\
1164 The following RISC-V specific disassembler options are supported for use\n\
1165 with the -M switch (multiple options should be separated by commas):\n"));
1166 fprintf (stream
, "\n");
1168 /* Compute the length of the longest option name. */
1169 for (i
= 0; opts
->name
[i
] != NULL
; i
++)
1171 size_t len
= strlen (opts
->name
[i
]);
1173 if (opts
->arg
[i
] != NULL
)
1174 len
+= strlen (opts
->arg
[i
]->name
);
1179 for (i
= 0, max_len
++; opts
->name
[i
] != NULL
; i
++)
1181 fprintf (stream
, " %s", opts
->name
[i
]);
1182 if (opts
->arg
[i
] != NULL
)
1183 fprintf (stream
, "%s", opts
->arg
[i
]->name
);
1184 if (opts
->description
[i
] != NULL
)
1186 size_t len
= strlen (opts
->name
[i
]);
1188 if (opts
->arg
!= NULL
&& opts
->arg
[i
] != NULL
)
1189 len
+= strlen (opts
->arg
[i
]->name
);
1190 fprintf (stream
, "%*c %s", (int) (max_len
- len
), ' ',
1191 opts
->description
[i
]);
1193 fprintf (stream
, "\n");
1196 for (i
= 0; args
[i
].name
!= NULL
; i
++)
1198 if (args
[i
].values
== NULL
)
1200 fprintf (stream
, _("\n\
1201 For the options above, the following values are supported for \"%s\":\n "),
1203 for (j
= 0; args
[i
].values
[j
] != NULL
; j
++)
1204 fprintf (stream
, " %s", args
[i
].values
[j
]);
1205 fprintf (stream
, _("\n"));
1208 fprintf (stream
, _("\n"));