libctf, ld: diagnose corrupted CTF header cth_strlen
[binutils-gdb.git] / opcodes / riscv-dis.c
1 /* RISC-V disassembler
2 Copyright (C) 2011-2022 Free Software Foundation, Inc.
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"
24 #include "disassemble.h"
25 #include "libiberty.h"
26 #include "opcode/riscv.h"
27 #include "opintl.h"
28 #include "elf-bfd.h"
29 #include "elf/riscv.h"
30 #include "elfxx-riscv.h"
31
32 #include <stdint.h>
33 #include <ctype.h>
34
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;
37
38 unsigned xlen = 0;
39
40 static riscv_subset_list_t riscv_subsets;
41 static riscv_parse_subset_t riscv_rps_dis =
42 {
43 &riscv_subsets, /* subset_list. */
44 opcodes_error_handler,/* error_handler. */
45 &xlen, /* xlen. */
46 &default_isa_spec, /* isa_spec. */
47 false, /* check_unknown_prefixed_ext. */
48 };
49
50 struct riscv_private_data
51 {
52 bfd_vma gp;
53 bfd_vma print_addr;
54 bfd_vma hi_addr[OP_MASK_RD + 1];
55 };
56
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;
61
62 static const char * const *riscv_gpr_names;
63 static const char * const *riscv_fpr_names;
64
65 /* If set, disassemble as most general instruction. */
66 static int no_aliases;
67
68 static void
69 set_default_riscv_dis_options (void)
70 {
71 riscv_gpr_names = riscv_gpr_names_abi;
72 riscv_fpr_names = riscv_fpr_names_abi;
73 no_aliases = 0;
74 }
75
76 static bool
77 parse_riscv_dis_option_without_args (const char *option)
78 {
79 if (strcmp (option, "no-aliases") == 0)
80 no_aliases = 1;
81 else if (strcmp (option, "numeric") == 0)
82 {
83 riscv_gpr_names = riscv_gpr_names_numeric;
84 riscv_fpr_names = riscv_fpr_names_numeric;
85 }
86 else
87 return false;
88 return true;
89 }
90
91 static void
92 parse_riscv_dis_option (const char *option)
93 {
94 char *equal, *value;
95
96 if (parse_riscv_dis_option_without_args (option))
97 return;
98
99 equal = strchr (option, '=');
100 if (equal == NULL)
101 {
102 /* The option without '=' should be defined above. */
103 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
104 return;
105 }
106 if (equal == option
107 || *(equal + 1) == '\0')
108 {
109 /* Invalid options with '=', no option name before '=',
110 and no value after '='. */
111 opcodes_error_handler (_("unrecognized disassembler option with '=': %s"),
112 option);
113 return;
114 }
115
116 *equal = '\0';
117 value = equal + 1;
118 if (strcmp (option, "priv-spec") == 0)
119 {
120 enum riscv_spec_class priv_spec = PRIV_SPEC_CLASS_NONE;
121 const char *name = NULL;
122
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"),
126 option, value);
127 else if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
128 default_priv_spec = priv_spec;
129 else if (default_priv_spec != priv_spec)
130 {
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);
135 }
136 }
137 else
138 {
139 /* xgettext:c-format */
140 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
141 }
142 }
143
144 static void
145 parse_riscv_dis_options (const char *opts_in)
146 {
147 char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
148
149 set_default_riscv_dis_options ();
150
151 for ( ; opt_end != NULL; opt = opt_end + 1)
152 {
153 if ((opt_end = strchr (opt, ',')) != NULL)
154 *opt_end = 0;
155 parse_riscv_dis_option (opt);
156 }
157
158 free (opts);
159 }
160
161 /* Print one argument from an array. */
162
163 static void
164 arg_print (struct disassemble_info *info, unsigned long val,
165 const char* const* array, size_t size)
166 {
167 const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
168 (*info->fprintf_func) (info->stream, "%s", s);
169 }
170
171 static void
172 maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset,
173 int wide)
174 {
175 if (pd->hi_addr[base_reg] != (bfd_vma)-1)
176 {
177 pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
178 pd->hi_addr[base_reg] = -1;
179 }
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;
184
185 /* Sign-extend a 32-bit value to a 64-bit value. */
186 if (wide)
187 pd->print_addr = (bfd_vma)(int32_t) pd->print_addr;
188 }
189
190 /* Print insn arguments for 32/64-bit code. */
191
192 static void
193 print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info)
194 {
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;
200
201 if (*oparg != '\0')
202 print (info->stream, "\t");
203
204 for (; *oparg != '\0'; oparg++)
205 {
206 opargStart = oparg;
207 switch (*oparg)
208 {
209 case 'C': /* RVC */
210 switch (*++oparg)
211 {
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]);
216 break;
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]);
221 break;
222 case 'U': /* RS1, constrained to equal RD. */
223 print (info->stream, "%s", riscv_gpr_names[rd]);
224 break;
225 case 'c': /* RS1, constrained to equal sp. */
226 print (info->stream, "%s", riscv_gpr_names[X_SP]);
227 break;
228 case 'V': /* RS2 */
229 print (info->stream, "%s",
230 riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
231 break;
232 case 'o':
233 case 'j':
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));
240 break;
241 case 'k':
242 print (info->stream, "%d", (int)EXTRACT_CLTYPE_LW_IMM (l));
243 break;
244 case 'l':
245 print (info->stream, "%d", (int)EXTRACT_CLTYPE_LD_IMM (l));
246 break;
247 case 'm':
248 print (info->stream, "%d", (int)EXTRACT_CITYPE_LWSP_IMM (l));
249 break;
250 case 'n':
251 print (info->stream, "%d", (int)EXTRACT_CITYPE_LDSP_IMM (l));
252 break;
253 case 'K':
254 print (info->stream, "%d", (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
255 break;
256 case 'L':
257 print (info->stream, "%d", (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
258 break;
259 case 'M':
260 print (info->stream, "%d", (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
261 break;
262 case 'N':
263 print (info->stream, "%d", (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
264 break;
265 case 'p':
266 info->target = EXTRACT_CBTYPE_IMM (l) + pc;
267 (*info->print_address_func) (info->target, info);
268 break;
269 case 'a':
270 info->target = EXTRACT_CJTYPE_IMM (l) + pc;
271 (*info->print_address_func) (info->target, info);
272 break;
273 case 'u':
274 print (info->stream, "0x%x",
275 (int)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
276 break;
277 case '>':
278 print (info->stream, "0x%x", (int)EXTRACT_CITYPE_IMM (l) & 0x3f);
279 break;
280 case '<':
281 print (info->stream, "0x%x", (int)EXTRACT_CITYPE_IMM (l) & 0x1f);
282 break;
283 case 'T': /* Floating-point RS2. */
284 print (info->stream, "%s",
285 riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
286 break;
287 case 'D': /* Floating-point RS2 x8-x15. */
288 print (info->stream, "%s",
289 riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
290 break;
291 }
292 break;
293
294 case 'V': /* RVV */
295 switch (*++oparg)
296 {
297 case 'd':
298 case 'f':
299 print (info->stream, "%s",
300 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
301 break;
302 case 'e':
303 if (!EXTRACT_OPERAND (VWD, l))
304 print (info->stream, "%s", riscv_gpr_names[0]);
305 else
306 print (info->stream, "%s",
307 riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]);
308 break;
309 case 's':
310 print (info->stream, "%s",
311 riscv_vecr_names_numeric[EXTRACT_OPERAND (VS1, l)]);
312 break;
313 case 't':
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)]);
318 break;
319 case '0':
320 print (info->stream, "%s", riscv_vecr_names_numeric[0]);
321 break;
322 case 'b':
323 case 'c':
324 {
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);
332
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)
337 && !imm_vtype_res
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],
342 riscv_vma[imm_vma]);
343 else
344 print (info->stream, "%d", imm);
345 }
346 break;
347 case 'i':
348 print (info->stream, "%d", (int)EXTRACT_RVV_VI_IMM (l));
349 break;
350 case 'j':
351 print (info->stream, "%d", (int)EXTRACT_RVV_VI_UIMM (l));
352 break;
353 case 'k':
354 print (info->stream, "%d", (int)EXTRACT_RVV_OFFSET (l));
355 break;
356 case 'm':
357 if (! EXTRACT_OPERAND (VMASK, l))
358 print (info->stream, ",%s", riscv_vecm_names_numeric[0]);
359 break;
360 }
361 break;
362
363 case ',':
364 case '(':
365 case ')':
366 case '[':
367 case ']':
368 print (info->stream, "%c", *oparg);
369 break;
370
371 case '0':
372 /* Only print constant 0 if it is the last argument. */
373 if (!oparg[1])
374 print (info->stream, "0");
375 break;
376
377 case 'b':
378 case 's':
379 if ((l & MASK_JALR) == MATCH_JALR)
380 maybe_print_address (pd, rs1, 0, 0);
381 print (info->stream, "%s", riscv_gpr_names[rs1]);
382 break;
383
384 case 't':
385 print (info->stream, "%s",
386 riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
387 break;
388
389 case 'u':
390 print (info->stream, "0x%x",
391 (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
392 break;
393
394 case 'm':
395 arg_print (info, EXTRACT_OPERAND (RM, l),
396 riscv_rm, ARRAY_SIZE (riscv_rm));
397 break;
398
399 case 'P':
400 arg_print (info, EXTRACT_OPERAND (PRED, l),
401 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
402 break;
403
404 case 'Q':
405 arg_print (info, EXTRACT_OPERAND (SUCC, l),
406 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
407 break;
408
409 case 'o':
410 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
411 /* Fall through. */
412 case 'j':
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));
420 break;
421
422 case 'q':
423 maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0);
424 print (info->stream, "%d", (int)EXTRACT_STYPE_IMM (l));
425 break;
426
427 case 'f':
428 print (info->stream, "%d", (int)EXTRACT_STYPE_IMM (l));
429 break;
430
431 case 'a':
432 info->target = EXTRACT_JTYPE_IMM (l) + pc;
433 (*info->print_address_func) (info->target, info);
434 break;
435
436 case 'p':
437 info->target = EXTRACT_BTYPE_IMM (l) + pc;
438 (*info->print_address_func) (info->target, info);
439 break;
440
441 case 'd':
442 if ((l & MASK_AUIPC) == MATCH_AUIPC)
443 pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
444 else if ((l & MASK_LUI) == MATCH_LUI)
445 pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
446 else if ((l & MASK_C_LUI) == MATCH_C_LUI)
447 pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
448 print (info->stream, "%s", riscv_gpr_names[rd]);
449 break;
450
451 case 'y':
452 print (info->stream, "0x%x", (int)EXTRACT_OPERAND (BS, l));
453 break;
454
455 case 'z':
456 print (info->stream, "%s", riscv_gpr_names[0]);
457 break;
458
459 case '>':
460 print (info->stream, "0x%x", (int)EXTRACT_OPERAND (SHAMT, l));
461 break;
462
463 case '<':
464 print (info->stream, "0x%x", (int)EXTRACT_OPERAND (SHAMTW, l));
465 break;
466
467 case 'S':
468 case 'U':
469 print (info->stream, "%s", riscv_fpr_names[rs1]);
470 break;
471
472 case 'T':
473 print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
474 break;
475
476 case 'D':
477 print (info->stream, "%s", riscv_fpr_names[rd]);
478 break;
479
480 case 'R':
481 print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
482 break;
483
484 case 'E':
485 {
486 static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */
487 static bool init_csr = false;
488 unsigned int csr = EXTRACT_OPERAND (CSR, l);
489
490 if (!init_csr)
491 {
492 unsigned int i;
493 for (i = 0; i < 4096; i++)
494 riscv_csr_hash[i] = NULL;
495
496 /* Set to the newest privileged version. */
497 if (default_priv_spec == PRIV_SPEC_CLASS_NONE)
498 default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1;
499
500 #define DECLARE_CSR(name, num, class, define_version, abort_version) \
501 if (riscv_csr_hash[num] == NULL \
502 && ((define_version == PRIV_SPEC_CLASS_NONE \
503 && abort_version == PRIV_SPEC_CLASS_NONE) \
504 || (default_priv_spec >= define_version \
505 && default_priv_spec < abort_version))) \
506 riscv_csr_hash[num] = #name;
507 #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
508 DECLARE_CSR (name, num, class, define_version, abort_version)
509 #include "opcode/riscv-opc.h"
510 #undef DECLARE_CSR
511 }
512
513 if (riscv_csr_hash[csr] != NULL)
514 print (info->stream, "%s", riscv_csr_hash[csr]);
515 else
516 print (info->stream, "0x%x", csr);
517 break;
518 }
519
520 case 'Y':
521 print (info->stream, "0x%x", (int)EXTRACT_OPERAND (RNUM, l));
522 break;
523
524 case 'Z':
525 print (info->stream, "%d", rs1);
526 break;
527
528 default:
529 /* xgettext:c-format */
530 print (info->stream, _("# internal error, undefined modifier (%c)"),
531 *opargStart);
532 return;
533 }
534 }
535 }
536
537 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
538 on using INFO. Returns length of the instruction, in bytes.
539 BIGENDIAN must be 1 if this is big-endian code, 0 if
540 this is little-endian code. */
541
542 static int
543 riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
544 {
545 const struct riscv_opcode *op;
546 static bool init = 0;
547 static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
548 struct riscv_private_data *pd;
549 int insnlen;
550
551 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
552
553 /* Build a hash table to shorten the search time. */
554 if (! init)
555 {
556 for (op = riscv_opcodes; op->name; op++)
557 if (!riscv_hash[OP_HASH_IDX (op->match)])
558 riscv_hash[OP_HASH_IDX (op->match)] = op;
559
560 init = 1;
561 }
562
563 if (info->private_data == NULL)
564 {
565 int i;
566
567 pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
568 pd->gp = -1;
569 pd->print_addr = -1;
570 for (i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++)
571 pd->hi_addr[i] = -1;
572
573 for (i = 0; i < info->symtab_size; i++)
574 if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0)
575 pd->gp = bfd_asymbol_value (info->symtab[i]);
576 }
577 else
578 pd = info->private_data;
579
580 insnlen = riscv_insn_length (word);
581
582 /* RISC-V instructions are always little-endian. */
583 info->endian_code = BFD_ENDIAN_LITTLE;
584
585 info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
586 info->bytes_per_line = 8;
587 /* We don't support constant pools, so this must be code. */
588 info->display_endian = info->endian_code;
589 info->insn_info_valid = 1;
590 info->branch_delay_insns = 0;
591 info->data_size = 0;
592 info->insn_type = dis_nonbranch;
593 info->target = 0;
594 info->target2 = 0;
595
596 op = riscv_hash[OP_HASH_IDX (word)];
597 if (op != NULL)
598 {
599 /* If XLEN is not known, get its value from the ELF class. */
600 if (info->mach == bfd_mach_riscv64)
601 xlen = 64;
602 else if (info->mach == bfd_mach_riscv32)
603 xlen = 32;
604 else if (info->section != NULL)
605 {
606 Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
607 xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
608 }
609
610 /* If arch has ZFINX flags, use gpr for disassemble. */
611 if(riscv_subset_supports (&riscv_rps_dis, "zfinx"))
612 riscv_fpr_names = riscv_gpr_names_abi;
613
614 for (; op->name; op++)
615 {
616 /* Does the opcode match? */
617 if (! (op->match_func) (op, word))
618 continue;
619 /* Is this a pseudo-instruction and may we print it as such? */
620 if (no_aliases && (op->pinfo & INSN_ALIAS))
621 continue;
622 /* Is this instruction restricted to a certain value of XLEN? */
623 if ((op->xlen_requirement != 0) && (op->xlen_requirement != xlen))
624 continue;
625
626 if (!riscv_multi_subset_supports (&riscv_rps_dis, op->insn_class))
627 continue;
628
629 /* It's a match. */
630 (*info->fprintf_func) (info->stream, "%s", op->name);
631 print_insn_args (op->args, word, memaddr, info);
632
633 /* Try to disassemble multi-instruction addressing sequences. */
634 if (pd->print_addr != (bfd_vma)-1)
635 {
636 info->target = pd->print_addr;
637 (*info->fprintf_func) (info->stream, " # ");
638 (*info->print_address_func) (info->target, info);
639 pd->print_addr = -1;
640 }
641
642 /* Finish filling out insn_info fields. */
643 switch (op->pinfo & INSN_TYPE)
644 {
645 case INSN_BRANCH:
646 info->insn_type = dis_branch;
647 break;
648 case INSN_CONDBRANCH:
649 info->insn_type = dis_condbranch;
650 break;
651 case INSN_JSR:
652 info->insn_type = dis_jsr;
653 break;
654 case INSN_DREF:
655 info->insn_type = dis_dref;
656 break;
657 default:
658 break;
659 }
660
661 if (op->pinfo & INSN_DATA_SIZE)
662 {
663 int size = ((op->pinfo & INSN_DATA_SIZE)
664 >> INSN_DATA_SIZE_SHIFT);
665 info->data_size = 1 << (size - 1);
666 }
667
668 return insnlen;
669 }
670 }
671
672 /* We did not find a match, so just print the instruction bits. */
673 info->insn_type = dis_noninsn;
674 switch (insnlen)
675 {
676 case 2:
677 case 4:
678 case 8:
679 (*info->fprintf_func) (info->stream, ".%dbyte\t0x%llx",
680 insnlen, (unsigned long long) word);
681 break;
682 default:
683 {
684 int i;
685 (*info->fprintf_func) (info->stream, ".byte\t");
686 for (i = 0; i < insnlen; ++i)
687 {
688 if (i > 0)
689 (*info->fprintf_func) (info->stream, ", ");
690 (*info->fprintf_func) (info->stream, "0x%02x",
691 (unsigned int) (word & 0xff));
692 word >>= 8;
693 }
694 }
695 break;
696 }
697 return insnlen;
698 }
699
700 /* Return true if we find the suitable mapping symbol,
701 and also update the STATE. Otherwise, return false. */
702
703 static bool
704 riscv_get_map_state (int n,
705 enum riscv_seg_mstate *state,
706 struct disassemble_info *info)
707 {
708 const char *name;
709
710 /* If the symbol is in a different section, ignore it. */
711 if (info->section != NULL
712 && info->section != info->symtab[n]->section)
713 return false;
714
715 name = bfd_asymbol_name(info->symtab[n]);
716 if (strcmp (name, "$x") == 0)
717 *state = MAP_INSN;
718 else if (strcmp (name, "$d") == 0)
719 *state = MAP_DATA;
720 else
721 return false;
722
723 return true;
724 }
725
726 /* Check the sorted symbol table (sorted by the symbol value), find the
727 suitable mapping symbols. */
728
729 static enum riscv_seg_mstate
730 riscv_search_mapping_symbol (bfd_vma memaddr,
731 struct disassemble_info *info)
732 {
733 enum riscv_seg_mstate mstate;
734 bool from_last_map_symbol;
735 bool found = false;
736 int symbol = -1;
737 int n;
738
739 /* Decide whether to print the data or instruction by default, in case
740 we can not find the corresponding mapping symbols. */
741 mstate = MAP_DATA;
742 if ((info->section
743 && info->section->flags & SEC_CODE)
744 || !info->section)
745 mstate = MAP_INSN;
746
747 if (info->symtab_size == 0
748 || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
749 return mstate;
750
751 /* Reset the last_map_symbol if we start to dump a new section. */
752 if (memaddr <= 0)
753 last_map_symbol = -1;
754
755 /* If the last stop offset is different from the current one, then
756 don't use the last_map_symbol to search. We usually reset the
757 info->stop_offset when handling a new section. */
758 from_last_map_symbol = (last_map_symbol >= 0
759 && info->stop_offset == last_stop_offset);
760
761 /* Start scanning at the start of the function, or wherever
762 we finished last time. */
763 n = info->symtab_pos + 1;
764 if (from_last_map_symbol && n >= last_map_symbol)
765 n = last_map_symbol;
766
767 /* Find the suitable mapping symbol to dump. */
768 for (; n < info->symtab_size; n++)
769 {
770 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
771 /* We have searched all possible symbols in the range. */
772 if (addr > memaddr)
773 break;
774 if (riscv_get_map_state (n, &mstate, info))
775 {
776 symbol = n;
777 found = true;
778 /* Do not stop searching, in case there are some mapping
779 symbols have the same value, but have different names.
780 Use the last one. */
781 }
782 }
783
784 /* We can not find the suitable mapping symbol above. Therefore, we
785 look forwards and try to find it again, but don't go pass the start
786 of the section. Otherwise a data section without mapping symbols
787 can pick up a text mapping symbol of a preceeding section. */
788 if (!found)
789 {
790 n = info->symtab_pos;
791 if (from_last_map_symbol && n >= last_map_symbol)
792 n = last_map_symbol;
793
794 for (; n >= 0; n--)
795 {
796 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
797 /* We have searched all possible symbols in the range. */
798 if (addr < (info->section ? info->section->vma : 0))
799 break;
800 /* Stop searching once we find the closed mapping symbol. */
801 if (riscv_get_map_state (n, &mstate, info))
802 {
803 symbol = n;
804 found = true;
805 break;
806 }
807 }
808 }
809
810 /* Save the information for next use. */
811 last_map_symbol = symbol;
812 last_stop_offset = info->stop_offset;
813
814 return mstate;
815 }
816
817 /* Decide which data size we should print. */
818
819 static bfd_vma
820 riscv_data_length (bfd_vma memaddr,
821 disassemble_info *info)
822 {
823 bfd_vma length;
824 bool found = false;
825
826 length = 4;
827 if (info->symtab_size != 0
828 && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
829 && last_map_symbol >= 0)
830 {
831 int n;
832 enum riscv_seg_mstate m = MAP_NONE;
833 for (n = last_map_symbol + 1; n < info->symtab_size; n++)
834 {
835 bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
836 if (addr > memaddr
837 && riscv_get_map_state (n, &m, info))
838 {
839 if (addr - memaddr < length)
840 length = addr - memaddr;
841 found = true;
842 break;
843 }
844 }
845 }
846 if (!found)
847 {
848 /* Do not set the length which exceeds the section size. */
849 bfd_vma offset = info->section->vma + info->section->size;
850 offset -= memaddr;
851 length = (offset < length) ? offset : length;
852 }
853 length = length == 3 ? 2 : length;
854 return length;
855 }
856
857 /* Dump the data contents. */
858
859 static int
860 riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
861 insn_t data,
862 disassemble_info *info)
863 {
864 info->display_endian = info->endian;
865
866 switch (info->bytes_per_chunk)
867 {
868 case 1:
869 info->bytes_per_line = 6;
870 (*info->fprintf_func) (info->stream, ".byte\t0x%02llx",
871 (unsigned long long) data);
872 break;
873 case 2:
874 info->bytes_per_line = 8;
875 (*info->fprintf_func) (info->stream, ".short\t0x%04llx",
876 (unsigned long long) data);
877 break;
878 case 4:
879 info->bytes_per_line = 8;
880 (*info->fprintf_func) (info->stream, ".word\t0x%08llx",
881 (unsigned long long) data);
882 break;
883 case 8:
884 info->bytes_per_line = 8;
885 (*info->fprintf_func) (info->stream, ".dword\t0x%016llx",
886 (unsigned long long) data);
887 break;
888 default:
889 abort ();
890 }
891 return info->bytes_per_chunk;
892 }
893
894 int
895 print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
896 {
897 bfd_byte packet[8];
898 insn_t insn = 0;
899 bfd_vma dump_size;
900 int status;
901 enum riscv_seg_mstate mstate;
902 int (*riscv_disassembler) (bfd_vma, insn_t, struct disassemble_info *);
903
904 if (info->disassembler_options != NULL)
905 {
906 parse_riscv_dis_options (info->disassembler_options);
907 /* Avoid repeatedly parsing the options. */
908 info->disassembler_options = NULL;
909 }
910 else if (riscv_gpr_names == NULL)
911 set_default_riscv_dis_options ();
912
913 mstate = riscv_search_mapping_symbol (memaddr, info);
914 /* Save the last mapping state. */
915 last_map_state = mstate;
916
917 /* Set the size to dump. */
918 if (mstate == MAP_DATA
919 && (info->flags & DISASSEMBLE_DATA) == 0)
920 {
921 dump_size = riscv_data_length (memaddr, info);
922 info->bytes_per_chunk = dump_size;
923 riscv_disassembler = riscv_disassemble_data;
924 }
925 else
926 {
927 /* Get the first 2-bytes to check the lenghth of instruction. */
928 status = (*info->read_memory_func) (memaddr, packet, 2, info);
929 if (status != 0)
930 {
931 (*info->memory_error_func) (status, memaddr, info);
932 return status;
933 }
934 insn = (insn_t) bfd_getl16 (packet);
935 dump_size = riscv_insn_length (insn);
936 riscv_disassembler = riscv_disassemble_insn;
937 }
938
939 /* Fetch the instruction to dump. */
940 status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
941 if (status != 0)
942 {
943 (*info->memory_error_func) (status, memaddr, info);
944 return status;
945 }
946 insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
947
948 return (*riscv_disassembler) (memaddr, insn, info);
949 }
950
951 disassembler_ftype
952 riscv_get_disassembler (bfd *abfd)
953 {
954 const char *default_arch = "rv64gc";
955
956 if (abfd)
957 {
958 const struct elf_backend_data *ebd = get_elf_backend_data (abfd);
959 if (ebd)
960 {
961 const char *sec_name = ebd->obj_attrs_section;
962 if (bfd_get_section_by_name (abfd, sec_name) != NULL)
963 {
964 obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
965 unsigned int Tag_a = Tag_RISCV_priv_spec;
966 unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
967 unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
968 riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i,
969 attr[Tag_b].i,
970 attr[Tag_c].i,
971 &default_priv_spec);
972 default_arch = attr[Tag_RISCV_arch].s;
973 }
974 }
975 }
976
977 riscv_release_subset_list (&riscv_subsets);
978 riscv_parse_subset (&riscv_rps_dis, default_arch);
979 return print_insn_riscv;
980 }
981
982 /* Prevent use of the fake labels that are generated as part of the DWARF
983 and for relaxable relocations in the assembler. */
984
985 bool
986 riscv_symbol_is_valid (asymbol * sym,
987 struct disassemble_info * info ATTRIBUTE_UNUSED)
988 {
989 const char * name;
990
991 if (sym == NULL)
992 return false;
993
994 name = bfd_asymbol_name (sym);
995
996 return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
997 && !riscv_elf_is_mapping_symbols (name));
998 }
999 \f
1000
1001 /* Indices into option argument vector for options accepting an argument.
1002 Use RISCV_OPTION_ARG_NONE for options accepting no argument. */
1003
1004 typedef enum
1005 {
1006 RISCV_OPTION_ARG_NONE = -1,
1007 RISCV_OPTION_ARG_PRIV_SPEC,
1008
1009 RISCV_OPTION_ARG_COUNT
1010 } riscv_option_arg_t;
1011
1012 /* Valid RISCV disassembler options. */
1013
1014 static struct
1015 {
1016 const char *name;
1017 const char *description;
1018 riscv_option_arg_t arg;
1019 } riscv_options[] =
1020 {
1021 { "numeric",
1022 N_("Print numeric register names, rather than ABI names."),
1023 RISCV_OPTION_ARG_NONE },
1024 { "no-aliases",
1025 N_("Disassemble only into canonical instructions."),
1026 RISCV_OPTION_ARG_NONE },
1027 { "priv-spec=",
1028 N_("Print the CSR according to the chosen privilege spec."),
1029 RISCV_OPTION_ARG_PRIV_SPEC }
1030 };
1031
1032 /* Build the structure representing valid RISCV disassembler options.
1033 This is done dynamically for maintenance ease purpose; a static
1034 initializer would be unreadable. */
1035
1036 const disasm_options_and_args_t *
1037 disassembler_options_riscv (void)
1038 {
1039 static disasm_options_and_args_t *opts_and_args;
1040
1041 if (opts_and_args == NULL)
1042 {
1043 size_t num_options = ARRAY_SIZE (riscv_options);
1044 size_t num_args = RISCV_OPTION_ARG_COUNT;
1045 disasm_option_arg_t *args;
1046 disasm_options_t *opts;
1047 size_t i, priv_spec_count;
1048
1049 args = XNEWVEC (disasm_option_arg_t, num_args + 1);
1050
1051 args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC";
1052 priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_CLASS_NONE - 1;
1053 args[RISCV_OPTION_ARG_PRIV_SPEC].values
1054 = XNEWVEC (const char *, priv_spec_count + 1);
1055 for (i = 0; i < priv_spec_count; i++)
1056 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i]
1057 = riscv_priv_specs[i].name;
1058 /* The array we return must be NULL terminated. */
1059 args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL;
1060
1061 /* The array we return must be NULL terminated. */
1062 args[num_args].name = NULL;
1063 args[num_args].values = NULL;
1064
1065 opts_and_args = XNEW (disasm_options_and_args_t);
1066 opts_and_args->args = args;
1067
1068 opts = &opts_and_args->options;
1069 opts->name = XNEWVEC (const char *, num_options + 1);
1070 opts->description = XNEWVEC (const char *, num_options + 1);
1071 opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1);
1072 for (i = 0; i < num_options; i++)
1073 {
1074 opts->name[i] = riscv_options[i].name;
1075 opts->description[i] = _(riscv_options[i].description);
1076 if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE)
1077 opts->arg[i] = &args[riscv_options[i].arg];
1078 else
1079 opts->arg[i] = NULL;
1080 }
1081 /* The array we return must be NULL terminated. */
1082 opts->name[i] = NULL;
1083 opts->description[i] = NULL;
1084 opts->arg[i] = NULL;
1085 }
1086
1087 return opts_and_args;
1088 }
1089
1090 void
1091 print_riscv_disassembler_options (FILE *stream)
1092 {
1093 const disasm_options_and_args_t *opts_and_args;
1094 const disasm_option_arg_t *args;
1095 const disasm_options_t *opts;
1096 size_t max_len = 0;
1097 size_t i;
1098 size_t j;
1099
1100 opts_and_args = disassembler_options_riscv ();
1101 opts = &opts_and_args->options;
1102 args = opts_and_args->args;
1103
1104 fprintf (stream, _("\n\
1105 The following RISC-V specific disassembler options are supported for use\n\
1106 with the -M switch (multiple options should be separated by commas):\n"));
1107 fprintf (stream, "\n");
1108
1109 /* Compute the length of the longest option name. */
1110 for (i = 0; opts->name[i] != NULL; i++)
1111 {
1112 size_t len = strlen (opts->name[i]);
1113
1114 if (opts->arg[i] != NULL)
1115 len += strlen (opts->arg[i]->name);
1116 if (max_len < len)
1117 max_len = len;
1118 }
1119
1120 for (i = 0, max_len++; opts->name[i] != NULL; i++)
1121 {
1122 fprintf (stream, " %s", opts->name[i]);
1123 if (opts->arg[i] != NULL)
1124 fprintf (stream, "%s", opts->arg[i]->name);
1125 if (opts->description[i] != NULL)
1126 {
1127 size_t len = strlen (opts->name[i]);
1128
1129 if (opts->arg != NULL && opts->arg[i] != NULL)
1130 len += strlen (opts->arg[i]->name);
1131 fprintf (stream, "%*c %s", (int) (max_len - len), ' ',
1132 opts->description[i]);
1133 }
1134 fprintf (stream, "\n");
1135 }
1136
1137 for (i = 0; args[i].name != NULL; i++)
1138 {
1139 fprintf (stream, _("\n\
1140 For the options above, the following values are supported for \"%s\":\n "),
1141 args[i].name);
1142 for (j = 0; args[i].values[j] != NULL; j++)
1143 fprintf (stream, " %s", args[i].values[j]);
1144 fprintf (stream, _("\n"));
1145 }
1146
1147 fprintf (stream, _("\n"));
1148 }