]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - opcodes/riscv-dis.c
sim: ppc: align format string settings with common code
[thirdparty/binutils-gdb.git] / opcodes / riscv-dis.c
CommitLineData
e23eba97 1/* RISC-V disassembler
250d07de 2 Copyright (C) 2011-2021 Free Software Foundation, Inc.
e23eba97
NC
3
4 Contributed by Andrew Waterman (andrew@sifive.com).
5 Based on MIPS target.
6
7 This file is part of the GNU opcodes library.
8
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)
12 any later version.
13
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.
18
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/>. */
22
23#include "sysdep.h"
88c1242d 24#include "disassemble.h"
e23eba97
NC
25#include "libiberty.h"
26#include "opcode/riscv.h"
27#include "opintl.h"
28#include "elf-bfd.h"
29#include "elf/riscv.h"
3d73d29e 30#include "cpu-riscv.h"
e23eba97 31
3dfb1b6d 32#include <stdint.h>
e23eba97
NC
33#include <ctype.h>
34
3d73d29e 35static enum riscv_spec_class default_priv_spec = PRIV_SPEC_CLASS_NONE;
8f595e9b 36
e23eba97
NC
37struct riscv_private_data
38{
39 bfd_vma gp;
40 bfd_vma print_addr;
41 bfd_vma hi_addr[OP_MASK_RD + 1];
42};
43
9b9b1092
NC
44/* Used for mapping symbols. */
45static int last_map_symbol = -1;
46static bfd_vma last_stop_offset = 0;
47enum riscv_seg_mstate last_map_state;
48
e23eba97
NC
49static const char * const *riscv_gpr_names;
50static const char * const *riscv_fpr_names;
51
dcd709e0
NC
52/* If set, disassemble as most general instruction. */
53static int no_aliases;
e23eba97
NC
54
55static void
56set_default_riscv_dis_options (void)
57{
58 riscv_gpr_names = riscv_gpr_names_abi;
59 riscv_fpr_names = riscv_fpr_names_abi;
60 no_aliases = 0;
61}
62
78933a4a 63static bool
8f595e9b 64parse_riscv_dis_option_without_args (const char *option)
e23eba97
NC
65{
66 if (strcmp (option, "no-aliases") == 0)
67 no_aliases = 1;
68 else if (strcmp (option, "numeric") == 0)
69 {
70 riscv_gpr_names = riscv_gpr_names_numeric;
71 riscv_fpr_names = riscv_fpr_names_numeric;
72 }
8f595e9b 73 else
78933a4a
AM
74 return false;
75 return true;
8f595e9b
NC
76}
77
78static void
79parse_riscv_dis_option (const char *option)
80{
81 char *equal, *value;
82
83 if (parse_riscv_dis_option_without_args (option))
84 return;
85
86 equal = strchr (option, '=');
87 if (equal == NULL)
88 {
89 /* The option without '=' should be defined above. */
90 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
91 return;
92 }
93 if (equal == option
94 || *(equal + 1) == '\0')
95 {
96 /* Invalid options with '=', no option name before '=',
97 and no value after '='. */
98 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
99 option);
100 return;
101 }
102
103 *equal = '\0';
104 value = equal + 1;
105 if (strcmp (option, "priv-spec") == 0)
106 {
3d73d29e
NC
107 enum riscv_spec_class priv_spec = PRIV_SPEC_CLASS_NONE;
108 const char *name = NULL;
109
110 RISCV_GET_PRIV_SPEC_CLASS (value, priv_spec);
111 if (priv_spec == PRIV_SPEC_CLASS_NONE)
b800637e 112 opcodes_error_handler (_("unknown privileged spec set by %s=%s"),
8152e040
NC
113 option, value);
114 else if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
115 default_priv_spec = priv_spec;
116 else if (default_priv_spec != priv_spec)
3d73d29e
NC
117 {
118 RISCV_GET_PRIV_SPEC_NAME (name, default_priv_spec);
119 opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, "
120 "the elf privilege attribute is %s"),
121 option, value, name);
122 }
8f595e9b 123 }
e23eba97
NC
124 else
125 {
a6743a54
AM
126 /* xgettext:c-format */
127 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
e23eba97
NC
128 }
129}
130
131static void
132parse_riscv_dis_options (const char *opts_in)
133{
134 char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
135
136 set_default_riscv_dis_options ();
137
138 for ( ; opt_end != NULL; opt = opt_end + 1)
139 {
140 if ((opt_end = strchr (opt, ',')) != NULL)
141 *opt_end = 0;
142 parse_riscv_dis_option (opt);
143 }
144
145 free (opts);
146}
147
148/* Print one argument from an array. */
149
150static void
151arg_print (struct disassemble_info *info, unsigned long val,
152 const char* const* array, size_t size)
153{
154 const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
155 (*info->fprintf_func) (info->stream, "%s", s);
156}
157
158static void
159maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset)
160{
161 if (pd->hi_addr[base_reg] != (bfd_vma)-1)
162 {
35fd2b2b 163 pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
e23eba97
NC
164 pd->hi_addr[base_reg] = -1;
165 }
166 else if (base_reg == X_GP && pd->gp != (bfd_vma)-1)
167 pd->print_addr = pd->gp + offset;
168 else if (base_reg == X_TP || base_reg == 0)
169 pd->print_addr = offset;
170}
171
172/* Print insn arguments for 32/64-bit code. */
173
174static void
175print_insn_args (const char *d, insn_t l, bfd_vma pc, disassemble_info *info)
176{
177 struct riscv_private_data *pd = info->private_data;
178 int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
179 int rd = (l >> OP_SH_RD) & OP_MASK_RD;
180 fprintf_ftype print = info->fprintf_func;
181
182 if (*d != '\0')
183 print (info->stream, "\t");
184
185 for (; *d != '\0'; d++)
186 {
187 switch (*d)
188 {
189 case 'C': /* RVC */
190 switch (*++d)
191 {
dcd709e0
NC
192 case 's': /* RS1 x8-x15. */
193 case 'w': /* RS1 x8-x15. */
e23eba97
NC
194 print (info->stream, "%s",
195 riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
196 break;
dcd709e0
NC
197 case 't': /* RS2 x8-x15. */
198 case 'x': /* RS2 x8-x15. */
e23eba97
NC
199 print (info->stream, "%s",
200 riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
201 break;
dcd709e0 202 case 'U': /* RS1, constrained to equal RD. */
e23eba97
NC
203 print (info->stream, "%s", riscv_gpr_names[rd]);
204 break;
dcd709e0 205 case 'c': /* RS1, constrained to equal sp. */
e23eba97
NC
206 print (info->stream, "%s", riscv_gpr_names[X_SP]);
207 break;
208 case 'V': /* RS2 */
209 print (info->stream, "%s",
210 riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
211 break;
f91d48de 212 case 'o':
e23eba97 213 case 'j':
5a9f5403 214 print (info->stream, "%d", (int)EXTRACT_CITYPE_IMM (l));
e23eba97
NC
215 break;
216 case 'k':
5a9f5403 217 print (info->stream, "%d", (int)EXTRACT_CLTYPE_LW_IMM (l));
e23eba97
NC
218 break;
219 case 'l':
5a9f5403 220 print (info->stream, "%d", (int)EXTRACT_CLTYPE_LD_IMM (l));
e23eba97
NC
221 break;
222 case 'm':
5a9f5403 223 print (info->stream, "%d", (int)EXTRACT_CITYPE_LWSP_IMM (l));
e23eba97
NC
224 break;
225 case 'n':
5a9f5403 226 print (info->stream, "%d", (int)EXTRACT_CITYPE_LDSP_IMM (l));
e23eba97
NC
227 break;
228 case 'K':
5a9f5403 229 print (info->stream, "%d", (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
e23eba97
NC
230 break;
231 case 'L':
5a9f5403 232 print (info->stream, "%d", (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
e23eba97
NC
233 break;
234 case 'M':
5a9f5403 235 print (info->stream, "%d", (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
e23eba97
NC
236 break;
237 case 'N':
5a9f5403 238 print (info->stream, "%d", (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
e23eba97
NC
239 break;
240 case 'p':
5a9f5403 241 info->target = EXTRACT_CBTYPE_IMM (l) + pc;
e23eba97
NC
242 (*info->print_address_func) (info->target, info);
243 break;
244 case 'a':
5a9f5403 245 info->target = EXTRACT_CJTYPE_IMM (l) + pc;
e23eba97
NC
246 (*info->print_address_func) (info->target, info);
247 break;
248 case 'u':
249 print (info->stream, "0x%x",
5a9f5403 250 (int)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
e23eba97
NC
251 break;
252 case '>':
5a9f5403 253 print (info->stream, "0x%x", (int)EXTRACT_CITYPE_IMM (l) & 0x3f);
e23eba97
NC
254 break;
255 case '<':
5a9f5403 256 print (info->stream, "0x%x", (int)EXTRACT_CITYPE_IMM (l) & 0x1f);
e23eba97 257 break;
dcd709e0 258 case 'T': /* Floating-point RS2. */
e23eba97
NC
259 print (info->stream, "%s",
260 riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
261 break;
dcd709e0 262 case 'D': /* Floating-point RS2 x8-x15. */
e23eba97
NC
263 print (info->stream, "%s",
264 riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
265 break;
266 }
267 break;
268
269 case ',':
270 case '(':
271 case ')':
272 case '[':
273 case ']':
274 print (info->stream, "%c", *d);
275 break;
276
277 case '0':
dcd709e0 278 /* Only print constant 0 if it is the last argument. */
e23eba97
NC
279 if (!d[1])
280 print (info->stream, "0");
281 break;
282
283 case 'b':
284 case 's':
35eeb78f
JW
285 if ((l & MASK_JALR) == MATCH_JALR)
286 maybe_print_address (pd, rs1, 0);
e23eba97
NC
287 print (info->stream, "%s", riscv_gpr_names[rs1]);
288 break;
289
290 case 't':
291 print (info->stream, "%s",
292 riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
293 break;
294
295 case 'u':
296 print (info->stream, "0x%x",
297 (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
298 break;
299
300 case 'm':
301 arg_print (info, EXTRACT_OPERAND (RM, l),
302 riscv_rm, ARRAY_SIZE (riscv_rm));
303 break;
304
305 case 'P':
306 arg_print (info, EXTRACT_OPERAND (PRED, l),
307 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
308 break;
309
310 case 'Q':
311 arg_print (info, EXTRACT_OPERAND (SUCC, l),
312 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
313 break;
314
315 case 'o':
316 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l));
b52d3cfc 317 /* Fall through. */
e23eba97
NC
318 case 'j':
319 if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
320 || (l & MASK_JALR) == MATCH_JALR)
321 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l));
322 print (info->stream, "%d", (int)EXTRACT_ITYPE_IMM (l));
323 break;
324
325 case 'q':
326 maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l));
327 print (info->stream, "%d", (int)EXTRACT_STYPE_IMM (l));
328 break;
329
330 case 'a':
5a9f5403 331 info->target = EXTRACT_JTYPE_IMM (l) + pc;
e23eba97
NC
332 (*info->print_address_func) (info->target, info);
333 break;
334
335 case 'p':
5a9f5403 336 info->target = EXTRACT_BTYPE_IMM (l) + pc;
e23eba97
NC
337 (*info->print_address_func) (info->target, info);
338 break;
339
340 case 'd':
341 if ((l & MASK_AUIPC) == MATCH_AUIPC)
342 pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
343 else if ((l & MASK_LUI) == MATCH_LUI)
344 pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
345 else if ((l & MASK_C_LUI) == MATCH_C_LUI)
5a9f5403 346 pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
e23eba97
NC
347 print (info->stream, "%s", riscv_gpr_names[rd]);
348 break;
349
350 case 'z':
351 print (info->stream, "%s", riscv_gpr_names[0]);
352 break;
353
354 case '>':
355 print (info->stream, "0x%x", (int)EXTRACT_OPERAND (SHAMT, l));
356 break;
357
358 case '<':
359 print (info->stream, "0x%x", (int)EXTRACT_OPERAND (SHAMTW, l));
360 break;
361
362 case 'S':
363 case 'U':
364 print (info->stream, "%s", riscv_fpr_names[rs1]);
365 break;
366
367 case 'T':
368 print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
369 break;
370
371 case 'D':
372 print (info->stream, "%s", riscv_fpr_names[rd]);
373 break;
374
375 case 'R':
376 print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
377 break;
378
379 case 'E':
380 {
dcd709e0 381 static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */
78933a4a 382 static bool init_csr = false;
e23eba97 383 unsigned int csr = EXTRACT_OPERAND (CSR, l);
8f595e9b
NC
384
385 if (!init_csr)
e23eba97 386 {
8f595e9b
NC
387 unsigned int i;
388 for (i = 0; i < 4096; i++)
389 riscv_csr_hash[i] = NULL;
390
dcd709e0 391 /* Set to the newest privileged version. */
8f595e9b
NC
392 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
393 default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1;
394
08ccfccf
NC
395#define DECLARE_CSR(name, num, class, define_version, abort_version) \
396 if (riscv_csr_hash[num] == NULL \
397 && ((define_version == PRIV_SPEC_CLASS_NONE \
398 && abort_version == PRIV_SPEC_CLASS_NONE) \
399 || (default_priv_spec >= define_version \
400 && default_priv_spec < abort_version))) \
8f595e9b
NC
401 riscv_csr_hash[num] = #name;
402#define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
403 DECLARE_CSR (name, num, class, define_version, abort_version)
e23eba97
NC
404#include "opcode/riscv-opc.h"
405#undef DECLARE_CSR
406 }
8f595e9b
NC
407
408 if (riscv_csr_hash[csr] != NULL)
409 print (info->stream, "%s", riscv_csr_hash[csr]);
e23eba97
NC
410 else
411 print (info->stream, "0x%x", csr);
412 break;
413 }
414
415 case 'Z':
416 print (info->stream, "%d", rs1);
417 break;
418
419 default:
420 /* xgettext:c-format */
421 print (info->stream, _("# internal error, undefined modifier (%c)"),
422 *d);
423 return;
424 }
425 }
426}
427
428/* Print the RISC-V instruction at address MEMADDR in debugged memory,
429 on using INFO. Returns length of the instruction, in bytes.
430 BIGENDIAN must be 1 if this is big-endian code, 0 if
431 this is little-endian code. */
432
433static int
434riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
435{
436 const struct riscv_opcode *op;
78933a4a 437 static bool init = 0;
e23eba97
NC
438 static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
439 struct riscv_private_data *pd;
440 int insnlen;
441
442#define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
443
444 /* Build a hash table to shorten the search time. */
445 if (! init)
446 {
447 for (op = riscv_opcodes; op->name; op++)
448 if (!riscv_hash[OP_HASH_IDX (op->match)])
449 riscv_hash[OP_HASH_IDX (op->match)] = op;
450
451 init = 1;
452 }
453
454 if (info->private_data == NULL)
455 {
456 int i;
457
458 pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
459 pd->gp = -1;
460 pd->print_addr = -1;
461 for (i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++)
462 pd->hi_addr[i] = -1;
463
464 for (i = 0; i < info->symtab_size; i++)
b5292032 465 if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0)
e23eba97
NC
466 pd->gp = bfd_asymbol_value (info->symtab[i]);
467 }
468 else
469 pd = info->private_data;
470
471 insnlen = riscv_insn_length (word);
472
d7560e2d
JW
473 /* RISC-V instructions are always little-endian. */
474 info->endian_code = BFD_ENDIAN_LITTLE;
475
e23eba97
NC
476 info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
477 info->bytes_per_line = 8;
d7560e2d
JW
478 /* We don't support constant pools, so this must be code. */
479 info->display_endian = info->endian_code;
e23eba97
NC
480 info->insn_info_valid = 1;
481 info->branch_delay_insns = 0;
482 info->data_size = 0;
483 info->insn_type = dis_nonbranch;
484 info->target = 0;
485 info->target2 = 0;
486
487 op = riscv_hash[OP_HASH_IDX (word)];
488 if (op != NULL)
489 {
1080bf78 490 unsigned xlen = 0;
e23eba97 491
2922d21d
AW
492 /* If XLEN is not known, get its value from the ELF class. */
493 if (info->mach == bfd_mach_riscv64)
494 xlen = 64;
495 else if (info->mach == bfd_mach_riscv32)
496 xlen = 32;
497 else if (info->section != NULL)
e23eba97
NC
498 {
499 Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
500 xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
501 }
502
503 for (; op->name; op++)
504 {
505 /* Does the opcode match? */
506 if (! (op->match_func) (op, word))
507 continue;
508 /* Is this a pseudo-instruction and may we print it as such? */
509 if (no_aliases && (op->pinfo & INSN_ALIAS))
510 continue;
511 /* Is this instruction restricted to a certain value of XLEN? */
43135d3b 512 if ((op->xlen_requirement != 0) && (op->xlen_requirement != xlen))
e23eba97
NC
513 continue;
514
515 /* It's a match. */
516 (*info->fprintf_func) (info->stream, "%s", op->name);
517 print_insn_args (op->args, word, memaddr, info);
518
519 /* Try to disassemble multi-instruction addressing sequences. */
520 if (pd->print_addr != (bfd_vma)-1)
521 {
522 info->target = pd->print_addr;
523 (*info->fprintf_func) (info->stream, " # ");
524 (*info->print_address_func) (info->target, info);
525 pd->print_addr = -1;
526 }
527
eb41b248
JW
528 /* Finish filling out insn_info fields. */
529 switch (op->pinfo & INSN_TYPE)
530 {
531 case INSN_BRANCH:
532 info->insn_type = dis_branch;
533 break;
534 case INSN_CONDBRANCH:
535 info->insn_type = dis_condbranch;
536 break;
537 case INSN_JSR:
538 info->insn_type = dis_jsr;
539 break;
540 case INSN_DREF:
541 info->insn_type = dis_dref;
542 break;
543 default:
544 break;
545 }
546
547 if (op->pinfo & INSN_DATA_SIZE)
548 {
549 int size = ((op->pinfo & INSN_DATA_SIZE)
550 >> INSN_DATA_SIZE_SHIFT);
551 info->data_size = 1 << (size - 1);
552 }
553
e23eba97
NC
554 return insnlen;
555 }
556 }
557
558 /* We did not find a match, so just print the instruction bits. */
559 info->insn_type = dis_noninsn;
560 (*info->fprintf_func) (info->stream, "0x%llx", (unsigned long long)word);
561 return insnlen;
562}
563
9b9b1092
NC
564/* Return true if we find the suitable mapping symbol,
565 and also update the STATE. Otherwise, return false. */
566
567static bool
568riscv_get_map_state (int n,
569 enum riscv_seg_mstate *state,
570 struct disassemble_info *info)
571{
572 const char *name;
573
574 /* If the symbol is in a different section, ignore it. */
575 if (info->section != NULL
576 && info->section != info->symtab[n]->section)
577 return false;
578
579 name = bfd_asymbol_name(info->symtab[n]);
580 if (strcmp (name, "$x") == 0)
581 *state = MAP_INSN;
582 else if (strcmp (name, "$d") == 0)
583 *state = MAP_DATA;
584 else
585 return false;
586
587 return true;
588}
589
590/* Check the sorted symbol table (sorted by the symbol value), find the
591 suitable mapping symbols. */
592
593static enum riscv_seg_mstate
594riscv_search_mapping_symbol (bfd_vma memaddr,
595 struct disassemble_info *info)
596{
597 enum riscv_seg_mstate mstate;
598 bool from_last_map_symbol;
599 bool found = false;
600 int symbol = -1;
601 int n;
602
603 /* Decide whether to print the data or instruction by default, in case
604 we can not find the corresponding mapping symbols. */
605 mstate = MAP_DATA;
606 if ((info->section
607 && info->section->flags & SEC_CODE)
608 || !info->section)
609 mstate = MAP_INSN;
610
611 if (info->symtab_size == 0
612 || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
613 return mstate;
614
615 /* Reset the last_map_symbol if we start to dump a new section. */
616 if (memaddr <= 0)
617 last_map_symbol = -1;
618
619 /* If the last stop offset is different from the current one, then
620 don't use the last_map_symbol to search. We usually reset the
621 info->stop_offset when handling a new section. */
622 from_last_map_symbol = (last_map_symbol >= 0
623 && info->stop_offset == last_stop_offset);
624
625 /* Start scanning at the start of the function, or wherever
626 we finished last time. */
627 n = info->symtab_pos + 1;
628 if (from_last_map_symbol && n >= last_map_symbol)
629 n = last_map_symbol;
630
631 /* Find the suitable mapping symbol to dump. */
632 for (; n < info->symtab_size; n++)
633 {
634 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
635 /* We have searched all possible symbols in the range. */
636 if (addr > memaddr)
637 break;
638 if (riscv_get_map_state (n, &mstate, info))
639 {
640 symbol = n;
641 found = true;
642 /* Do not stop searching, in case there are some mapping
643 symbols have the same value, but have different names.
644 Use the last one. */
645 }
646 }
647
648 /* We can not find the suitable mapping symbol above. Therefore, we
649 look forwards and try to find it again, but don't go pass the start
650 of the section. Otherwise a data section without mapping symbols
651 can pick up a text mapping symbol of a preceeding section. */
652 if (!found)
653 {
654 n = info->symtab_pos;
655 if (from_last_map_symbol && n >= last_map_symbol)
656 n = last_map_symbol;
657
658 for (; n >= 0; n--)
659 {
660 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
661 /* We have searched all possible symbols in the range. */
662 if (addr < (info->section ? info->section->vma : 0))
663 break;
664 /* Stop searching once we find the closed mapping symbol. */
665 if (riscv_get_map_state (n, &mstate, info))
666 {
667 symbol = n;
668 found = true;
669 break;
670 }
671 }
672 }
673
674 /* Save the information for next use. */
675 last_map_symbol = symbol;
676 last_stop_offset = info->stop_offset;
677
678 return mstate;
679}
680
681/* Decide which data size we should print. */
682
683static bfd_vma
684riscv_data_length (bfd_vma memaddr,
685 disassemble_info *info)
686{
687 bfd_vma length;
688 bool found = false;
689
690 length = 4;
691 if (info->symtab_size != 0
692 && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
693 && last_map_symbol >= 0)
694 {
695 int n;
696 enum riscv_seg_mstate m = MAP_NONE;
697 for (n = last_map_symbol + 1; n < info->symtab_size; n++)
698 {
699 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
700 if (addr > memaddr
701 && riscv_get_map_state (n, &m, info))
702 {
703 if (addr - memaddr < length)
704 length = addr - memaddr;
705 found = true;
706 break;
707 }
708 }
709 }
710 if (!found)
711 {
712 /* Do not set the length which exceeds the section size. */
713 bfd_vma offset = info->section->vma + info->section->size;
714 offset -= memaddr;
715 length = (offset < length) ? offset : length;
716 }
717 length = length == 3 ? 2 : length;
718 return length;
719}
720
721/* Dump the data contents. */
722
723static int
724riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
725 insn_t data,
726 disassemble_info *info)
727{
728 info->display_endian = info->endian;
729
730 switch (info->bytes_per_chunk)
731 {
732 case 1:
733 info->bytes_per_line = 6;
734 (*info->fprintf_func) (info->stream, ".byte\t0x%02llx",
735 (unsigned long long) data);
736 break;
737 case 2:
738 info->bytes_per_line = 8;
739 (*info->fprintf_func) (info->stream, ".short\t0x%04llx",
740 (unsigned long long) data);
741 break;
742 case 4:
743 info->bytes_per_line = 8;
744 (*info->fprintf_func) (info->stream, ".word\t0x%08llx",
745 (unsigned long long) data);
746 break;
747 case 8:
748 info->bytes_per_line = 8;
749 (*info->fprintf_func) (info->stream, ".dword\t0x%016llx",
750 (unsigned long long) data);
751 break;
752 default:
753 abort ();
754 }
755 return info->bytes_per_chunk;
756}
757
e23eba97
NC
758int
759print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
760{
9b9b1092 761 bfd_byte packet[8];
e23eba97 762 insn_t insn = 0;
9b9b1092 763 bfd_vma dump_size;
e23eba97 764 int status;
9b9b1092
NC
765 enum riscv_seg_mstate mstate;
766 int (*riscv_disassembler) (bfd_vma, insn_t, struct disassemble_info *);
e23eba97
NC
767
768 if (info->disassembler_options != NULL)
769 {
770 parse_riscv_dis_options (info->disassembler_options);
771 /* Avoid repeatedly parsing the options. */
772 info->disassembler_options = NULL;
773 }
774 else if (riscv_gpr_names == NULL)
775 set_default_riscv_dis_options ();
776
9b9b1092
NC
777 mstate = riscv_search_mapping_symbol (memaddr, info);
778 /* Save the last mapping state. */
779 last_map_state = mstate;
780
781 /* Set the size to dump. */
782 if (mstate == MAP_DATA
783 && (info->flags & DISASSEMBLE_DATA) == 0)
784 {
785 dump_size = riscv_data_length (memaddr, info);
786 info->bytes_per_chunk = dump_size;
787 riscv_disassembler = riscv_disassemble_data;
788 }
789 else
e23eba97 790 {
9b9b1092
NC
791 /* Get the first 2-bytes to check the lenghth of instruction. */
792 status = (*info->read_memory_func) (memaddr, packet, 2, info);
e23eba97
NC
793 if (status != 0)
794 {
e23eba97 795 (*info->memory_error_func) (status, memaddr, info);
685bb4e8 796 return status;
e23eba97 797 }
9b9b1092
NC
798 insn = (insn_t) bfd_getl16 (packet);
799 dump_size = riscv_insn_length (insn);
800 riscv_disassembler = riscv_disassemble_insn;
801 }
e23eba97 802
9b9b1092
NC
803 /* Fetch the instruction to dump. */
804 status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
805 if (status != 0)
806 {
807 (*info->memory_error_func) (status, memaddr, info);
685bb4e8 808 return status;
e23eba97 809 }
9b9b1092 810 insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
e23eba97 811
9b9b1092 812 return (*riscv_disassembler) (memaddr, insn, info);
e23eba97
NC
813}
814
8152e040
NC
815disassembler_ftype
816riscv_get_disassembler (bfd *abfd)
817{
8152e040
NC
818 if (abfd)
819 {
113bb761
JN
820 const struct elf_backend_data *ebd = get_elf_backend_data (abfd);
821 if (ebd)
8152e040 822 {
113bb761
JN
823 const char *sec_name = ebd->obj_attrs_section;
824 if (bfd_get_section_by_name (abfd, sec_name) != NULL)
825 {
826 obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
827 unsigned int Tag_a = Tag_RISCV_priv_spec;
828 unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
829 unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
830 riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i,
831 attr[Tag_b].i,
832 attr[Tag_c].i,
833 &default_priv_spec);
834 }
8152e040
NC
835 }
836 }
837 return print_insn_riscv;
838}
839
884b49e3
AB
840/* Prevent use of the fake labels that are generated as part of the DWARF
841 and for relaxable relocations in the assembler. */
842
78933a4a 843bool
884b49e3
AB
844riscv_symbol_is_valid (asymbol * sym,
845 struct disassemble_info * info ATTRIBUTE_UNUSED)
846{
847 const char * name;
848
849 if (sym == NULL)
78933a4a 850 return false;
884b49e3
AB
851
852 name = bfd_asymbol_name (sym);
853
9b9b1092
NC
854 return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
855 && !riscv_elf_is_mapping_symbols (name));
884b49e3
AB
856}
857
e23eba97
NC
858void
859print_riscv_disassembler_options (FILE *stream)
860{
861 fprintf (stream, _("\n\
862The following RISC-V-specific disassembler options are supported for use\n\
863with the -M switch (multiple options should be separated by commas):\n"));
864
865 fprintf (stream, _("\n\
8f595e9b
NC
866 numeric Print numeric register names, rather than ABI names.\n"));
867
868 fprintf (stream, _("\n\
869 no-aliases Disassemble only into canonical instructions, rather\n\
870 than into pseudoinstructions.\n"));
e23eba97
NC
871
872 fprintf (stream, _("\n\
8f595e9b
NC
873 priv-spec=PRIV Print the CSR according to the chosen privilege spec\n\
874 (1.9, 1.9.1, 1.10, 1.11).\n"));
e23eba97
NC
875
876 fprintf (stream, _("\n"));
877}