2 Copyright (C) 2011-2021 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];
57 /* Used for mapping symbols. */
58 static int last_map_symbol
= -1;
59 static bfd_vma last_stop_offset
= 0;
60 enum riscv_seg_mstate last_map_state
;
62 static const char * const *riscv_gpr_names
;
63 static const char * const *riscv_fpr_names
;
65 /* If set, disassemble as most general instruction. */
66 static int no_aliases
;
69 set_default_riscv_dis_options (void)
71 riscv_gpr_names
= riscv_gpr_names_abi
;
72 riscv_fpr_names
= riscv_fpr_names_abi
;
77 parse_riscv_dis_option_without_args (const char *option
)
79 if (strcmp (option
, "no-aliases") == 0)
81 else if (strcmp (option
, "numeric") == 0)
83 riscv_gpr_names
= riscv_gpr_names_numeric
;
84 riscv_fpr_names
= riscv_fpr_names_numeric
;
92 parse_riscv_dis_option (const char *option
)
96 if (parse_riscv_dis_option_without_args (option
))
99 equal
= strchr (option
, '=');
102 /* The option without '=' should be defined above. */
103 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
107 || *(equal
+ 1) == '\0')
109 /* Invalid options with '=', no option name before '=',
110 and no value after '='. */
111 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
118 if (strcmp (option
, "priv-spec") == 0)
120 enum riscv_spec_class priv_spec
= PRIV_SPEC_CLASS_NONE
;
121 const char *name
= NULL
;
123 RISCV_GET_PRIV_SPEC_CLASS (value
, priv_spec
);
124 if (priv_spec
== PRIV_SPEC_CLASS_NONE
)
125 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
127 else if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
128 default_priv_spec
= priv_spec
;
129 else if (default_priv_spec
!= priv_spec
)
131 RISCV_GET_PRIV_SPEC_NAME (name
, default_priv_spec
);
132 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
133 "the elf privilege attribute is %s"),
134 option
, value
, name
);
139 /* xgettext:c-format */
140 opcodes_error_handler (_("unrecognized disassembler option: %s"), option
);
145 parse_riscv_dis_options (const char *opts_in
)
147 char *opts
= xstrdup (opts_in
), *opt
= opts
, *opt_end
= opts
;
149 set_default_riscv_dis_options ();
151 for ( ; opt_end
!= NULL
; opt
= opt_end
+ 1)
153 if ((opt_end
= strchr (opt
, ',')) != NULL
)
155 parse_riscv_dis_option (opt
);
161 /* Print one argument from an array. */
164 arg_print (struct disassemble_info
*info
, unsigned long val
,
165 const char* const* array
, size_t size
)
167 const char *s
= val
>= size
|| array
[val
] == NULL
? "unknown" : array
[val
];
168 (*info
->fprintf_func
) (info
->stream
, "%s", s
);
172 maybe_print_address (struct riscv_private_data
*pd
, int base_reg
, int offset
,
175 if (pd
->hi_addr
[base_reg
] != (bfd_vma
)-1)
177 pd
->print_addr
= (base_reg
!= 0 ? pd
->hi_addr
[base_reg
] : 0) + offset
;
178 pd
->hi_addr
[base_reg
] = -1;
180 else if (base_reg
== X_GP
&& pd
->gp
!= (bfd_vma
)-1)
181 pd
->print_addr
= pd
->gp
+ offset
;
182 else if (base_reg
== X_TP
|| base_reg
== 0)
183 pd
->print_addr
= offset
;
185 /* Sign-extend a 32-bit value to a 64-bit value. */
187 pd
->print_addr
= (bfd_vma
)(int32_t) pd
->print_addr
;
190 /* Print insn arguments for 32/64-bit code. */
193 print_insn_args (const char *oparg
, insn_t l
, bfd_vma pc
, disassemble_info
*info
)
195 struct riscv_private_data
*pd
= info
->private_data
;
196 int rs1
= (l
>> OP_SH_RS1
) & OP_MASK_RS1
;
197 int rd
= (l
>> OP_SH_RD
) & OP_MASK_RD
;
198 fprintf_ftype print
= info
->fprintf_func
;
199 const char *opargStart
;
202 print (info
->stream
, "\t");
204 for (; *oparg
!= '\0'; oparg
++)
212 case 's': /* RS1 x8-x15. */
213 case 'w': /* RS1 x8-x15. */
214 print (info
->stream
, "%s",
215 riscv_gpr_names
[EXTRACT_OPERAND (CRS1S
, l
) + 8]);
217 case 't': /* RS2 x8-x15. */
218 case 'x': /* RS2 x8-x15. */
219 print (info
->stream
, "%s",
220 riscv_gpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
222 case 'U': /* RS1, constrained to equal RD. */
223 print (info
->stream
, "%s", riscv_gpr_names
[rd
]);
225 case 'c': /* RS1, constrained to equal sp. */
226 print (info
->stream
, "%s", riscv_gpr_names
[X_SP
]);
229 print (info
->stream
, "%s",
230 riscv_gpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
234 if (((l
& MASK_C_ADDI
) == MATCH_C_ADDI
) && rd
!= 0)
235 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 0);
236 if (info
->mach
== bfd_mach_riscv64
237 && ((l
& MASK_C_ADDIW
) == MATCH_C_ADDIW
) && rd
!= 0)
238 maybe_print_address (pd
, rd
, EXTRACT_CITYPE_IMM (l
), 1);
239 print (info
->stream
, "%d", (int)EXTRACT_CITYPE_IMM (l
));
242 print (info
->stream
, "%d", (int)EXTRACT_CLTYPE_LW_IMM (l
));
245 print (info
->stream
, "%d", (int)EXTRACT_CLTYPE_LD_IMM (l
));
248 print (info
->stream
, "%d", (int)EXTRACT_CITYPE_LWSP_IMM (l
));
251 print (info
->stream
, "%d", (int)EXTRACT_CITYPE_LDSP_IMM (l
));
254 print (info
->stream
, "%d", (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l
));
257 print (info
->stream
, "%d", (int)EXTRACT_CITYPE_ADDI16SP_IMM (l
));
260 print (info
->stream
, "%d", (int)EXTRACT_CSSTYPE_SWSP_IMM (l
));
263 print (info
->stream
, "%d", (int)EXTRACT_CSSTYPE_SDSP_IMM (l
));
266 info
->target
= EXTRACT_CBTYPE_IMM (l
) + pc
;
267 (*info
->print_address_func
) (info
->target
, info
);
270 info
->target
= EXTRACT_CJTYPE_IMM (l
) + pc
;
271 (*info
->print_address_func
) (info
->target
, info
);
274 print (info
->stream
, "0x%x",
275 (int)(EXTRACT_CITYPE_IMM (l
) & (RISCV_BIGIMM_REACH
-1)));
278 print (info
->stream
, "0x%x", (int)EXTRACT_CITYPE_IMM (l
) & 0x3f);
281 print (info
->stream
, "0x%x", (int)EXTRACT_CITYPE_IMM (l
) & 0x1f);
283 case 'T': /* Floating-point RS2. */
284 print (info
->stream
, "%s",
285 riscv_fpr_names
[EXTRACT_OPERAND (CRS2
, l
)]);
287 case 'D': /* Floating-point RS2 x8-x15. */
288 print (info
->stream
, "%s",
289 riscv_fpr_names
[EXTRACT_OPERAND (CRS2S
, l
) + 8]);
299 print (info
->stream
, "%s",
300 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
303 if (!EXTRACT_OPERAND (VWD
, l
))
304 print (info
->stream
, "%s", riscv_gpr_names
[0]);
306 print (info
->stream
, "%s",
307 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VD
, l
)]);
310 print (info
->stream
, "%s",
311 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS1
, l
)]);
314 case 'u': /* VS1 == VS2 already verified at this point. */
315 case 'v': /* VD == VS1 == VS2 already verified at this point. */
316 print (info
->stream
, "%s",
317 riscv_vecr_names_numeric
[EXTRACT_OPERAND (VS2
, l
)]);
320 print (info
->stream
, "%s", riscv_vecr_names_numeric
[0]);
325 int imm
= (*oparg
== 'b') ? EXTRACT_RVV_VB_IMM (l
)
326 : EXTRACT_RVV_VC_IMM (l
);
327 unsigned int imm_vlmul
= EXTRACT_OPERAND (VLMUL
, imm
);
328 unsigned int imm_vsew
= EXTRACT_OPERAND (VSEW
, imm
);
329 unsigned int imm_vta
= EXTRACT_OPERAND (VTA
, imm
);
330 unsigned int imm_vma
= EXTRACT_OPERAND (VMA
, imm
);
331 unsigned int imm_vtype_res
= (imm
>> 8);
333 if (imm_vsew
< ARRAY_SIZE (riscv_vsew
)
334 && imm_vlmul
< ARRAY_SIZE (riscv_vlmul
)
335 && imm_vta
< ARRAY_SIZE (riscv_vta
)
336 && imm_vma
< ARRAY_SIZE (riscv_vma
)
338 && riscv_vsew
[imm_vsew
] != NULL
339 && riscv_vlmul
[imm_vlmul
] != NULL
)
340 print (info
->stream
, "%s,%s,%s,%s", riscv_vsew
[imm_vsew
],
341 riscv_vlmul
[imm_vlmul
], riscv_vta
[imm_vta
],
344 print (info
->stream
, "%d", imm
);
348 print (info
->stream
, "%d", (int)EXTRACT_RVV_VI_IMM (l
));
351 print (info
->stream
, "%d", (int)EXTRACT_RVV_VI_UIMM (l
));
354 print (info
->stream
, "%d", (int)EXTRACT_RVV_OFFSET (l
));
357 if (! EXTRACT_OPERAND (VMASK
, l
))
358 print (info
->stream
, ",%s", riscv_vecm_names_numeric
[0]);
368 print (info
->stream
, "%c", *oparg
);
372 /* Only print constant 0 if it is the last argument. */
374 print (info
->stream
, "0");
379 if ((l
& MASK_JALR
) == MATCH_JALR
)
380 maybe_print_address (pd
, rs1
, 0, 0);
381 print (info
->stream
, "%s", riscv_gpr_names
[rs1
]);
385 print (info
->stream
, "%s",
386 riscv_gpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
390 print (info
->stream
, "0x%x",
391 (unsigned)EXTRACT_UTYPE_IMM (l
) >> RISCV_IMM_BITS
);
395 arg_print (info
, EXTRACT_OPERAND (RM
, l
),
396 riscv_rm
, ARRAY_SIZE (riscv_rm
));
400 arg_print (info
, EXTRACT_OPERAND (PRED
, l
),
401 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
405 arg_print (info
, EXTRACT_OPERAND (SUCC
, l
),
406 riscv_pred_succ
, ARRAY_SIZE (riscv_pred_succ
));
410 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
413 if (((l
& MASK_ADDI
) == MATCH_ADDI
&& rs1
!= 0)
414 || (l
& MASK_JALR
) == MATCH_JALR
)
415 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 0);
416 if (info
->mach
== bfd_mach_riscv64
417 && ((l
& MASK_ADDIW
) == MATCH_ADDIW
) && rs1
!= 0)
418 maybe_print_address (pd
, rs1
, EXTRACT_ITYPE_IMM (l
), 1);
419 print (info
->stream
, "%d", (int)EXTRACT_ITYPE_IMM (l
));
423 maybe_print_address (pd
, rs1
, EXTRACT_STYPE_IMM (l
), 0);
424 print (info
->stream
, "%d", (int)EXTRACT_STYPE_IMM (l
));
428 info
->target
= EXTRACT_JTYPE_IMM (l
) + pc
;
429 (*info
->print_address_func
) (info
->target
, info
);
433 info
->target
= EXTRACT_BTYPE_IMM (l
) + pc
;
434 (*info
->print_address_func
) (info
->target
, info
);
438 if ((l
& MASK_AUIPC
) == MATCH_AUIPC
)
439 pd
->hi_addr
[rd
] = pc
+ EXTRACT_UTYPE_IMM (l
);
440 else if ((l
& MASK_LUI
) == MATCH_LUI
)
441 pd
->hi_addr
[rd
] = EXTRACT_UTYPE_IMM (l
);
442 else if ((l
& MASK_C_LUI
) == MATCH_C_LUI
)
443 pd
->hi_addr
[rd
] = EXTRACT_CITYPE_LUI_IMM (l
);
444 print (info
->stream
, "%s", riscv_gpr_names
[rd
]);
448 print (info
->stream
, "0x%x", (int)EXTRACT_OPERAND (BS
, l
));
452 print (info
->stream
, "%s", riscv_gpr_names
[0]);
456 print (info
->stream
, "0x%x", (int)EXTRACT_OPERAND (SHAMT
, l
));
460 print (info
->stream
, "0x%x", (int)EXTRACT_OPERAND (SHAMTW
, l
));
465 print (info
->stream
, "%s", riscv_fpr_names
[rs1
]);
469 print (info
->stream
, "%s", riscv_fpr_names
[EXTRACT_OPERAND (RS2
, l
)]);
473 print (info
->stream
, "%s", riscv_fpr_names
[rd
]);
477 print (info
->stream
, "%s", riscv_fpr_names
[EXTRACT_OPERAND (RS3
, l
)]);
482 static const char *riscv_csr_hash
[4096]; /* Total 2^12 CSRs. */
483 static bool init_csr
= false;
484 unsigned int csr
= EXTRACT_OPERAND (CSR
, l
);
489 for (i
= 0; i
< 4096; i
++)
490 riscv_csr_hash
[i
] = NULL
;
492 /* Set to the newest privileged version. */
493 if (default_priv_spec
== PRIV_SPEC_CLASS_NONE
)
494 default_priv_spec
= PRIV_SPEC_CLASS_DRAFT
- 1;
496 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
497 if (riscv_csr_hash[num] == NULL \
498 && ((define_version == PRIV_SPEC_CLASS_NONE \
499 && abort_version == PRIV_SPEC_CLASS_NONE) \
500 || (default_priv_spec >= define_version \
501 && default_priv_spec < abort_version))) \
502 riscv_csr_hash[num] = #name;
503 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
504 DECLARE_CSR (name, num, class, define_version, abort_version)
505 #include "opcode/riscv-opc.h"
509 if (riscv_csr_hash
[csr
] != NULL
)
510 print (info
->stream
, "%s", riscv_csr_hash
[csr
]);
512 print (info
->stream
, "0x%x", csr
);
517 print (info
->stream
, "0x%x", (int)EXTRACT_OPERAND (RNUM
, l
));
521 print (info
->stream
, "%d", rs1
);
525 /* xgettext:c-format */
526 print (info
->stream
, _("# internal error, undefined modifier (%c)"),
533 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
534 on using INFO. Returns length of the instruction, in bytes.
535 BIGENDIAN must be 1 if this is big-endian code, 0 if
536 this is little-endian code. */
539 riscv_disassemble_insn (bfd_vma memaddr
, insn_t word
, disassemble_info
*info
)
541 const struct riscv_opcode
*op
;
542 static bool init
= 0;
543 static const struct riscv_opcode
*riscv_hash
[OP_MASK_OP
+ 1];
544 struct riscv_private_data
*pd
;
547 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
549 /* Build a hash table to shorten the search time. */
552 for (op
= riscv_opcodes
; op
->name
; op
++)
553 if (!riscv_hash
[OP_HASH_IDX (op
->match
)])
554 riscv_hash
[OP_HASH_IDX (op
->match
)] = op
;
559 if (info
->private_data
== NULL
)
563 pd
= info
->private_data
= xcalloc (1, sizeof (struct riscv_private_data
));
566 for (i
= 0; i
< (int)ARRAY_SIZE (pd
->hi_addr
); i
++)
569 for (i
= 0; i
< info
->symtab_size
; i
++)
570 if (strcmp (bfd_asymbol_name (info
->symtab
[i
]), RISCV_GP_SYMBOL
) == 0)
571 pd
->gp
= bfd_asymbol_value (info
->symtab
[i
]);
574 pd
= info
->private_data
;
576 insnlen
= riscv_insn_length (word
);
578 /* RISC-V instructions are always little-endian. */
579 info
->endian_code
= BFD_ENDIAN_LITTLE
;
581 info
->bytes_per_chunk
= insnlen
% 4 == 0 ? 4 : 2;
582 info
->bytes_per_line
= 8;
583 /* We don't support constant pools, so this must be code. */
584 info
->display_endian
= info
->endian_code
;
585 info
->insn_info_valid
= 1;
586 info
->branch_delay_insns
= 0;
588 info
->insn_type
= dis_nonbranch
;
592 op
= riscv_hash
[OP_HASH_IDX (word
)];
595 /* If XLEN is not known, get its value from the ELF class. */
596 if (info
->mach
== bfd_mach_riscv64
)
598 else if (info
->mach
== bfd_mach_riscv32
)
600 else if (info
->section
!= NULL
)
602 Elf_Internal_Ehdr
*ehdr
= elf_elfheader (info
->section
->owner
);
603 xlen
= ehdr
->e_ident
[EI_CLASS
] == ELFCLASS64
? 64 : 32;
606 /* If arch has ZFINX flags, use gpr for disassemble. */
607 if(riscv_subset_supports (&riscv_rps_dis
, "zfinx"))
608 riscv_fpr_names
= riscv_gpr_names_abi
;
610 for (; op
->name
; op
++)
612 /* Does the opcode match? */
613 if (! (op
->match_func
) (op
, word
))
615 /* Is this a pseudo-instruction and may we print it as such? */
616 if (no_aliases
&& (op
->pinfo
& INSN_ALIAS
))
618 /* Is this instruction restricted to a certain value of XLEN? */
619 if ((op
->xlen_requirement
!= 0) && (op
->xlen_requirement
!= xlen
))
622 if (!riscv_multi_subset_supports (&riscv_rps_dis
, op
->insn_class
))
626 (*info
->fprintf_func
) (info
->stream
, "%s", op
->name
);
627 print_insn_args (op
->args
, word
, memaddr
, info
);
629 /* Try to disassemble multi-instruction addressing sequences. */
630 if (pd
->print_addr
!= (bfd_vma
)-1)
632 info
->target
= pd
->print_addr
;
633 (*info
->fprintf_func
) (info
->stream
, " # ");
634 (*info
->print_address_func
) (info
->target
, info
);
638 /* Finish filling out insn_info fields. */
639 switch (op
->pinfo
& INSN_TYPE
)
642 info
->insn_type
= dis_branch
;
644 case INSN_CONDBRANCH
:
645 info
->insn_type
= dis_condbranch
;
648 info
->insn_type
= dis_jsr
;
651 info
->insn_type
= dis_dref
;
657 if (op
->pinfo
& INSN_DATA_SIZE
)
659 int size
= ((op
->pinfo
& INSN_DATA_SIZE
)
660 >> INSN_DATA_SIZE_SHIFT
);
661 info
->data_size
= 1 << (size
- 1);
668 /* We did not find a match, so just print the instruction bits. */
669 info
->insn_type
= dis_noninsn
;
675 (*info
->fprintf_func
) (info
->stream
, ".%dbyte\t0x%llx",
676 insnlen
, (unsigned long long) word
);
681 (*info
->fprintf_func
) (info
->stream
, ".byte\t");
682 for (i
= 0; i
< insnlen
; ++i
)
685 (*info
->fprintf_func
) (info
->stream
, ", ");
686 (*info
->fprintf_func
) (info
->stream
, "0x%02x",
687 (unsigned int) (word
& 0xff));
696 /* Return true if we find the suitable mapping symbol,
697 and also update the STATE. Otherwise, return false. */
700 riscv_get_map_state (int n
,
701 enum riscv_seg_mstate
*state
,
702 struct disassemble_info
*info
)
706 /* If the symbol is in a different section, ignore it. */
707 if (info
->section
!= NULL
708 && info
->section
!= info
->symtab
[n
]->section
)
711 name
= bfd_asymbol_name(info
->symtab
[n
]);
712 if (strcmp (name
, "$x") == 0)
714 else if (strcmp (name
, "$d") == 0)
722 /* Check the sorted symbol table (sorted by the symbol value), find the
723 suitable mapping symbols. */
725 static enum riscv_seg_mstate
726 riscv_search_mapping_symbol (bfd_vma memaddr
,
727 struct disassemble_info
*info
)
729 enum riscv_seg_mstate mstate
;
730 bool from_last_map_symbol
;
735 /* Decide whether to print the data or instruction by default, in case
736 we can not find the corresponding mapping symbols. */
739 && info
->section
->flags
& SEC_CODE
)
743 if (info
->symtab_size
== 0
744 || bfd_asymbol_flavour (*info
->symtab
) != bfd_target_elf_flavour
)
747 /* Reset the last_map_symbol if we start to dump a new section. */
749 last_map_symbol
= -1;
751 /* If the last stop offset is different from the current one, then
752 don't use the last_map_symbol to search. We usually reset the
753 info->stop_offset when handling a new section. */
754 from_last_map_symbol
= (last_map_symbol
>= 0
755 && info
->stop_offset
== last_stop_offset
);
757 /* Start scanning at the start of the function, or wherever
758 we finished last time. */
759 n
= info
->symtab_pos
+ 1;
760 if (from_last_map_symbol
&& n
>= last_map_symbol
)
763 /* Find the suitable mapping symbol to dump. */
764 for (; n
< info
->symtab_size
; n
++)
766 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
767 /* We have searched all possible symbols in the range. */
770 if (riscv_get_map_state (n
, &mstate
, info
))
774 /* Do not stop searching, in case there are some mapping
775 symbols have the same value, but have different names.
780 /* We can not find the suitable mapping symbol above. Therefore, we
781 look forwards and try to find it again, but don't go pass the start
782 of the section. Otherwise a data section without mapping symbols
783 can pick up a text mapping symbol of a preceeding section. */
786 n
= info
->symtab_pos
;
787 if (from_last_map_symbol
&& n
>= last_map_symbol
)
792 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
793 /* We have searched all possible symbols in the range. */
794 if (addr
< (info
->section
? info
->section
->vma
: 0))
796 /* Stop searching once we find the closed mapping symbol. */
797 if (riscv_get_map_state (n
, &mstate
, info
))
806 /* Save the information for next use. */
807 last_map_symbol
= symbol
;
808 last_stop_offset
= info
->stop_offset
;
813 /* Decide which data size we should print. */
816 riscv_data_length (bfd_vma memaddr
,
817 disassemble_info
*info
)
823 if (info
->symtab_size
!= 0
824 && bfd_asymbol_flavour (*info
->symtab
) == bfd_target_elf_flavour
825 && last_map_symbol
>= 0)
828 enum riscv_seg_mstate m
= MAP_NONE
;
829 for (n
= last_map_symbol
+ 1; n
< info
->symtab_size
; n
++)
831 bfd_vma addr
= bfd_asymbol_value (info
->symtab
[n
]);
833 && riscv_get_map_state (n
, &m
, info
))
835 if (addr
- memaddr
< length
)
836 length
= addr
- memaddr
;
844 /* Do not set the length which exceeds the section size. */
845 bfd_vma offset
= info
->section
->vma
+ info
->section
->size
;
847 length
= (offset
< length
) ? offset
: length
;
849 length
= length
== 3 ? 2 : length
;
853 /* Dump the data contents. */
856 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED
,
858 disassemble_info
*info
)
860 info
->display_endian
= info
->endian
;
862 switch (info
->bytes_per_chunk
)
865 info
->bytes_per_line
= 6;
866 (*info
->fprintf_func
) (info
->stream
, ".byte\t0x%02llx",
867 (unsigned long long) data
);
870 info
->bytes_per_line
= 8;
871 (*info
->fprintf_func
) (info
->stream
, ".short\t0x%04llx",
872 (unsigned long long) data
);
875 info
->bytes_per_line
= 8;
876 (*info
->fprintf_func
) (info
->stream
, ".word\t0x%08llx",
877 (unsigned long long) data
);
880 info
->bytes_per_line
= 8;
881 (*info
->fprintf_func
) (info
->stream
, ".dword\t0x%016llx",
882 (unsigned long long) data
);
887 return info
->bytes_per_chunk
;
891 print_insn_riscv (bfd_vma memaddr
, struct disassemble_info
*info
)
897 enum riscv_seg_mstate mstate
;
898 int (*riscv_disassembler
) (bfd_vma
, insn_t
, struct disassemble_info
*);
900 if (info
->disassembler_options
!= NULL
)
902 parse_riscv_dis_options (info
->disassembler_options
);
903 /* Avoid repeatedly parsing the options. */
904 info
->disassembler_options
= NULL
;
906 else if (riscv_gpr_names
== NULL
)
907 set_default_riscv_dis_options ();
909 mstate
= riscv_search_mapping_symbol (memaddr
, info
);
910 /* Save the last mapping state. */
911 last_map_state
= mstate
;
913 /* Set the size to dump. */
914 if (mstate
== MAP_DATA
915 && (info
->flags
& DISASSEMBLE_DATA
) == 0)
917 dump_size
= riscv_data_length (memaddr
, info
);
918 info
->bytes_per_chunk
= dump_size
;
919 riscv_disassembler
= riscv_disassemble_data
;
923 /* Get the first 2-bytes to check the lenghth of instruction. */
924 status
= (*info
->read_memory_func
) (memaddr
, packet
, 2, info
);
927 (*info
->memory_error_func
) (status
, memaddr
, info
);
930 insn
= (insn_t
) bfd_getl16 (packet
);
931 dump_size
= riscv_insn_length (insn
);
932 riscv_disassembler
= riscv_disassemble_insn
;
935 /* Fetch the instruction to dump. */
936 status
= (*info
->read_memory_func
) (memaddr
, packet
, dump_size
, info
);
939 (*info
->memory_error_func
) (status
, memaddr
, info
);
942 insn
= (insn_t
) bfd_get_bits (packet
, dump_size
* 8, false);
944 return (*riscv_disassembler
) (memaddr
, insn
, info
);
948 riscv_get_disassembler (bfd
*abfd
)
950 const char *default_arch
= "rv64gc";
954 const struct elf_backend_data
*ebd
= get_elf_backend_data (abfd
);
957 const char *sec_name
= ebd
->obj_attrs_section
;
958 if (bfd_get_section_by_name (abfd
, sec_name
) != NULL
)
960 obj_attribute
*attr
= elf_known_obj_attributes_proc (abfd
);
961 unsigned int Tag_a
= Tag_RISCV_priv_spec
;
962 unsigned int Tag_b
= Tag_RISCV_priv_spec_minor
;
963 unsigned int Tag_c
= Tag_RISCV_priv_spec_revision
;
964 riscv_get_priv_spec_class_from_numbers (attr
[Tag_a
].i
,
968 default_arch
= attr
[Tag_RISCV_arch
].s
;
973 riscv_release_subset_list (&riscv_subsets
);
974 riscv_parse_subset (&riscv_rps_dis
, default_arch
);
975 return print_insn_riscv
;
978 /* Prevent use of the fake labels that are generated as part of the DWARF
979 and for relaxable relocations in the assembler. */
982 riscv_symbol_is_valid (asymbol
* sym
,
983 struct disassemble_info
* info ATTRIBUTE_UNUSED
)
990 name
= bfd_asymbol_name (sym
);
992 return (strcmp (name
, RISCV_FAKE_LABEL_NAME
) != 0
993 && !riscv_elf_is_mapping_symbols (name
));
997 /* Indices into option argument vector for options accepting an argument.
998 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1002 RISCV_OPTION_ARG_NONE
= -1,
1003 RISCV_OPTION_ARG_PRIV_SPEC
,
1005 RISCV_OPTION_ARG_COUNT
1006 } riscv_option_arg_t
;
1008 /* Valid RISCV disassembler options. */
1013 const char *description
;
1014 riscv_option_arg_t arg
;
1018 N_("Print numeric register names, rather than ABI names."),
1019 RISCV_OPTION_ARG_NONE
},
1021 N_("Disassemble only into canonical instructions."),
1022 RISCV_OPTION_ARG_NONE
},
1024 N_("Print the CSR according to the chosen privilege spec."),
1025 RISCV_OPTION_ARG_PRIV_SPEC
}
1028 /* Build the structure representing valid RISCV disassembler options.
1029 This is done dynamically for maintenance ease purpose; a static
1030 initializer would be unreadable. */
1032 const disasm_options_and_args_t
*
1033 disassembler_options_riscv (void)
1035 static disasm_options_and_args_t
*opts_and_args
;
1037 if (opts_and_args
== NULL
)
1039 size_t num_options
= ARRAY_SIZE (riscv_options
);
1040 size_t num_args
= RISCV_OPTION_ARG_COUNT
;
1041 disasm_option_arg_t
*args
;
1042 disasm_options_t
*opts
;
1043 size_t i
, priv_spec_count
;
1045 args
= XNEWVEC (disasm_option_arg_t
, num_args
+ 1);
1047 args
[RISCV_OPTION_ARG_PRIV_SPEC
].name
= "SPEC";
1048 priv_spec_count
= PRIV_SPEC_CLASS_DRAFT
- PRIV_SPEC_CLASS_NONE
- 1;
1049 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
1050 = XNEWVEC (const char *, priv_spec_count
+ 1);
1051 for (i
= 0; i
< priv_spec_count
; i
++)
1052 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
]
1053 = riscv_priv_specs
[i
].name
;
1054 /* The array we return must be NULL terminated. */
1055 args
[RISCV_OPTION_ARG_PRIV_SPEC
].values
[i
] = NULL
;
1057 /* The array we return must be NULL terminated. */
1058 args
[num_args
].name
= NULL
;
1059 args
[num_args
].values
= NULL
;
1061 opts_and_args
= XNEW (disasm_options_and_args_t
);
1062 opts_and_args
->args
= args
;
1064 opts
= &opts_and_args
->options
;
1065 opts
->name
= XNEWVEC (const char *, num_options
+ 1);
1066 opts
->description
= XNEWVEC (const char *, num_options
+ 1);
1067 opts
->arg
= XNEWVEC (const disasm_option_arg_t
*, num_options
+ 1);
1068 for (i
= 0; i
< num_options
; i
++)
1070 opts
->name
[i
] = riscv_options
[i
].name
;
1071 opts
->description
[i
] = _(riscv_options
[i
].description
);
1072 if (riscv_options
[i
].arg
!= RISCV_OPTION_ARG_NONE
)
1073 opts
->arg
[i
] = &args
[riscv_options
[i
].arg
];
1075 opts
->arg
[i
] = NULL
;
1077 /* The array we return must be NULL terminated. */
1078 opts
->name
[i
] = NULL
;
1079 opts
->description
[i
] = NULL
;
1080 opts
->arg
[i
] = NULL
;
1083 return opts_and_args
;
1087 print_riscv_disassembler_options (FILE *stream
)
1089 const disasm_options_and_args_t
*opts_and_args
;
1090 const disasm_option_arg_t
*args
;
1091 const disasm_options_t
*opts
;
1096 opts_and_args
= disassembler_options_riscv ();
1097 opts
= &opts_and_args
->options
;
1098 args
= opts_and_args
->args
;
1100 fprintf (stream
, _("\n\
1101 The following RISC-V specific disassembler options are supported for use\n\
1102 with the -M switch (multiple options should be separated by commas):\n"));
1103 fprintf (stream
, "\n");
1105 /* Compute the length of the longest option name. */
1106 for (i
= 0; opts
->name
[i
] != NULL
; i
++)
1108 size_t len
= strlen (opts
->name
[i
]);
1110 if (opts
->arg
[i
] != NULL
)
1111 len
+= strlen (opts
->arg
[i
]->name
);
1116 for (i
= 0, max_len
++; opts
->name
[i
] != NULL
; i
++)
1118 fprintf (stream
, " %s", opts
->name
[i
]);
1119 if (opts
->arg
[i
] != NULL
)
1120 fprintf (stream
, "%s", opts
->arg
[i
]->name
);
1121 if (opts
->description
[i
] != NULL
)
1123 size_t len
= strlen (opts
->name
[i
]);
1125 if (opts
->arg
!= NULL
&& opts
->arg
[i
] != NULL
)
1126 len
+= strlen (opts
->arg
[i
]->name
);
1127 fprintf (stream
, "%*c %s", (int) (max_len
- len
), ' ',
1128 opts
->description
[i
]);
1130 fprintf (stream
, "\n");
1133 for (i
= 0; args
[i
].name
!= NULL
; i
++)
1135 fprintf (stream
, _("\n\
1136 For the options above, the following values are supported for \"%s\":\n "),
1138 for (j
= 0; args
[i
].values
[j
] != NULL
; j
++)
1139 fprintf (stream
, " %s", args
[i
].values
[j
]);
1140 fprintf (stream
, _("\n"));
1143 fprintf (stream
, _("\n"));