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