riscv-dis.c revision 1.3.14.2 1 /* RISC-V disassembler
2 Copyright (C) 2011-2018 Free Software Foundation, Inc.
3
4 Contributed by Andrew Waterman (andrew (at) 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
31 #include <stdint.h>
32
33 #include <ctype.h>
34
35 struct riscv_private_data
36 {
37 bfd_vma gp;
38 bfd_vma print_addr;
39 bfd_vma hi_addr[OP_MASK_RD + 1];
40 };
41
42 static const char * const *riscv_gpr_names;
43 static const char * const *riscv_fpr_names;
44
45 /* Other options. */
46 static int no_aliases; /* If set disassemble as most general inst. */
47
48 static void
49 set_default_riscv_dis_options (void)
50 {
51 riscv_gpr_names = riscv_gpr_names_abi;
52 riscv_fpr_names = riscv_fpr_names_abi;
53 no_aliases = 0;
54 }
55
56 static void
57 parse_riscv_dis_option (const char *option)
58 {
59 if (strcmp (option, "no-aliases") == 0)
60 no_aliases = 1;
61 else if (strcmp (option, "numeric") == 0)
62 {
63 riscv_gpr_names = riscv_gpr_names_numeric;
64 riscv_fpr_names = riscv_fpr_names_numeric;
65 }
66 else
67 {
68 /* xgettext:c-format */
69 opcodes_error_handler (_("unrecognized disassembler option: %s"), option);
70 }
71 }
72
73 static void
74 parse_riscv_dis_options (const char *opts_in)
75 {
76 char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts;
77
78 set_default_riscv_dis_options ();
79
80 for ( ; opt_end != NULL; opt = opt_end + 1)
81 {
82 if ((opt_end = strchr (opt, ',')) != NULL)
83 *opt_end = 0;
84 parse_riscv_dis_option (opt);
85 }
86
87 free (opts);
88 }
89
90 /* Print one argument from an array. */
91
92 static void
93 arg_print (struct disassemble_info *info, unsigned long val,
94 const char* const* array, size_t size)
95 {
96 const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
97 (*info->fprintf_func) (info->stream, "%s", s);
98 }
99
100 static void
101 maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset)
102 {
103 if (pd->hi_addr[base_reg] != (bfd_vma)-1)
104 {
105 pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
106 pd->hi_addr[base_reg] = -1;
107 }
108 else if (base_reg == X_GP && pd->gp != (bfd_vma)-1)
109 pd->print_addr = pd->gp + offset;
110 else if (base_reg == X_TP || base_reg == 0)
111 pd->print_addr = offset;
112 }
113
114 /* Print insn arguments for 32/64-bit code. */
115
116 static void
117 print_insn_args (const char *d, insn_t l, bfd_vma pc, disassemble_info *info)
118 {
119 struct riscv_private_data *pd = info->private_data;
120 int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
121 int rd = (l >> OP_SH_RD) & OP_MASK_RD;
122 fprintf_ftype print = info->fprintf_func;
123
124 if (*d != '\0')
125 print (info->stream, "\t");
126
127 for (; *d != '\0'; d++)
128 {
129 switch (*d)
130 {
131 case 'C': /* RVC */
132 switch (*++d)
133 {
134 case 's': /* RS1 x8-x15 */
135 case 'w': /* RS1 x8-x15 */
136 print (info->stream, "%s",
137 riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
138 break;
139 case 't': /* RS2 x8-x15 */
140 case 'x': /* RS2 x8-x15 */
141 print (info->stream, "%s",
142 riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
143 break;
144 case 'U': /* RS1, constrained to equal RD */
145 print (info->stream, "%s", riscv_gpr_names[rd]);
146 break;
147 case 'c': /* RS1, constrained to equal sp */
148 print (info->stream, "%s", riscv_gpr_names[X_SP]);
149 break;
150 case 'V': /* RS2 */
151 print (info->stream, "%s",
152 riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
153 break;
154 case 'i':
155 print (info->stream, "%d", (int)EXTRACT_RVC_SIMM3 (l));
156 break;
157 case 'o':
158 case 'j':
159 print (info->stream, "%d", (int)EXTRACT_RVC_IMM (l));
160 break;
161 case 'k':
162 print (info->stream, "%d", (int)EXTRACT_RVC_LW_IMM (l));
163 break;
164 case 'l':
165 print (info->stream, "%d", (int)EXTRACT_RVC_LD_IMM (l));
166 break;
167 case 'm':
168 print (info->stream, "%d", (int)EXTRACT_RVC_LWSP_IMM (l));
169 break;
170 case 'n':
171 print (info->stream, "%d", (int)EXTRACT_RVC_LDSP_IMM (l));
172 break;
173 case 'K':
174 print (info->stream, "%d", (int)EXTRACT_RVC_ADDI4SPN_IMM (l));
175 break;
176 case 'L':
177 print (info->stream, "%d", (int)EXTRACT_RVC_ADDI16SP_IMM (l));
178 break;
179 case 'M':
180 print (info->stream, "%d", (int)EXTRACT_RVC_SWSP_IMM (l));
181 break;
182 case 'N':
183 print (info->stream, "%d", (int)EXTRACT_RVC_SDSP_IMM (l));
184 break;
185 case 'p':
186 info->target = EXTRACT_RVC_B_IMM (l) + pc;
187 (*info->print_address_func) (info->target, info);
188 break;
189 case 'a':
190 info->target = EXTRACT_RVC_J_IMM (l) + pc;
191 (*info->print_address_func) (info->target, info);
192 break;
193 case 'u':
194 print (info->stream, "0x%x",
195 (int)(EXTRACT_RVC_IMM (l) & (RISCV_BIGIMM_REACH-1)));
196 break;
197 case '>':
198 print (info->stream, "0x%x", (int)EXTRACT_RVC_IMM (l) & 0x3f);
199 break;
200 case '<':
201 print (info->stream, "0x%x", (int)EXTRACT_RVC_IMM (l) & 0x1f);
202 break;
203 case 'T': /* floating-point RS2 */
204 print (info->stream, "%s",
205 riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
206 break;
207 case 'D': /* floating-point RS2 x8-x15 */
208 print (info->stream, "%s",
209 riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
210 break;
211 }
212 break;
213
214 case ',':
215 case '(':
216 case ')':
217 case '[':
218 case ']':
219 print (info->stream, "%c", *d);
220 break;
221
222 case '0':
223 /* Only print constant 0 if it is the last argument */
224 if (!d[1])
225 print (info->stream, "0");
226 break;
227
228 case 'b':
229 case 's':
230 if ((l & MASK_JALR) == MATCH_JALR)
231 maybe_print_address (pd, rs1, 0);
232 print (info->stream, "%s", riscv_gpr_names[rs1]);
233 break;
234
235 case 't':
236 print (info->stream, "%s",
237 riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
238 break;
239
240 case 'u':
241 print (info->stream, "0x%x",
242 (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
243 break;
244
245 case 'm':
246 arg_print (info, EXTRACT_OPERAND (RM, l),
247 riscv_rm, ARRAY_SIZE (riscv_rm));
248 break;
249
250 case 'P':
251 arg_print (info, EXTRACT_OPERAND (PRED, l),
252 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
253 break;
254
255 case 'Q':
256 arg_print (info, EXTRACT_OPERAND (SUCC, l),
257 riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
258 break;
259
260 case 'o':
261 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l));
262 /* Fall through. */
263 case 'j':
264 if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
265 || (l & MASK_JALR) == MATCH_JALR)
266 maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l));
267 print (info->stream, "%d", (int)EXTRACT_ITYPE_IMM (l));
268 break;
269
270 case 'q':
271 maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l));
272 print (info->stream, "%d", (int)EXTRACT_STYPE_IMM (l));
273 break;
274
275 case 'a':
276 info->target = EXTRACT_UJTYPE_IMM (l) + pc;
277 (*info->print_address_func) (info->target, info);
278 break;
279
280 case 'p':
281 info->target = EXTRACT_SBTYPE_IMM (l) + pc;
282 (*info->print_address_func) (info->target, info);
283 break;
284
285 case 'd':
286 if ((l & MASK_AUIPC) == MATCH_AUIPC)
287 pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
288 else if ((l & MASK_LUI) == MATCH_LUI)
289 pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
290 else if ((l & MASK_C_LUI) == MATCH_C_LUI)
291 pd->hi_addr[rd] = EXTRACT_RVC_LUI_IMM (l);
292 print (info->stream, "%s", riscv_gpr_names[rd]);
293 break;
294
295 case 'z':
296 print (info->stream, "%s", riscv_gpr_names[0]);
297 break;
298
299 case '>':
300 print (info->stream, "0x%x", (int)EXTRACT_OPERAND (SHAMT, l));
301 break;
302
303 case '<':
304 print (info->stream, "0x%x", (int)EXTRACT_OPERAND (SHAMTW, l));
305 break;
306
307 case 'S':
308 case 'U':
309 print (info->stream, "%s", riscv_fpr_names[rs1]);
310 break;
311
312 case 'T':
313 print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
314 break;
315
316 case 'D':
317 print (info->stream, "%s", riscv_fpr_names[rd]);
318 break;
319
320 case 'R':
321 print (info->stream, "%s", riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
322 break;
323
324 case 'E':
325 {
326 const char* csr_name = NULL;
327 unsigned int csr = EXTRACT_OPERAND (CSR, l);
328 switch (csr)
329 {
330 #define DECLARE_CSR(name, num) case num: csr_name = #name; break;
331 #include "opcode/riscv-opc.h"
332 #undef DECLARE_CSR
333 }
334 if (csr_name)
335 print (info->stream, "%s", csr_name);
336 else
337 print (info->stream, "0x%x", csr);
338 break;
339 }
340
341 case 'Z':
342 print (info->stream, "%d", rs1);
343 break;
344
345 default:
346 /* xgettext:c-format */
347 print (info->stream, _("# internal error, undefined modifier (%c)"),
348 *d);
349 return;
350 }
351 }
352 }
353
354 /* Print the RISC-V instruction at address MEMADDR in debugged memory,
355 on using INFO. Returns length of the instruction, in bytes.
356 BIGENDIAN must be 1 if this is big-endian code, 0 if
357 this is little-endian code. */
358
359 static int
360 riscv_disassemble_insn (bfd_vma memaddr, insn_t word, disassemble_info *info)
361 {
362 const struct riscv_opcode *op;
363 static bfd_boolean init = 0;
364 static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
365 struct riscv_private_data *pd;
366 int insnlen;
367
368 #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
369
370 /* Build a hash table to shorten the search time. */
371 if (! init)
372 {
373 for (op = riscv_opcodes; op->name; op++)
374 if (!riscv_hash[OP_HASH_IDX (op->match)])
375 riscv_hash[OP_HASH_IDX (op->match)] = op;
376
377 init = 1;
378 }
379
380 if (info->private_data == NULL)
381 {
382 int i;
383
384 pd = info->private_data = xcalloc (1, sizeof (struct riscv_private_data));
385 pd->gp = -1;
386 pd->print_addr = -1;
387 for (i = 0; i < (int)ARRAY_SIZE (pd->hi_addr); i++)
388 pd->hi_addr[i] = -1;
389
390 for (i = 0; i < info->symtab_size; i++)
391 if (strcmp (bfd_asymbol_name (info->symtab[i]), RISCV_GP_SYMBOL) == 0)
392 pd->gp = bfd_asymbol_value (info->symtab[i]);
393 }
394 else
395 pd = info->private_data;
396
397 insnlen = riscv_insn_length (word);
398
399 info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
400 info->bytes_per_line = 8;
401 info->display_endian = info->endian;
402 info->insn_info_valid = 1;
403 info->branch_delay_insns = 0;
404 info->data_size = 0;
405 info->insn_type = dis_nonbranch;
406 info->target = 0;
407 info->target2 = 0;
408
409 op = riscv_hash[OP_HASH_IDX (word)];
410 if (op != NULL)
411 {
412 int xlen = 0;
413
414 /* If XLEN is not known, get its value from the ELF class. */
415 if (info->mach == bfd_mach_riscv64)
416 xlen = 64;
417 else if (info->mach == bfd_mach_riscv32)
418 xlen = 32;
419 else if (info->section != NULL)
420 {
421 Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
422 xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
423 }
424
425 for (; op->name; op++)
426 {
427 /* Does the opcode match? */
428 if (! (op->match_func) (op, word))
429 continue;
430 /* Is this a pseudo-instruction and may we print it as such? */
431 if (no_aliases && (op->pinfo & INSN_ALIAS))
432 continue;
433 /* Is this instruction restricted to a certain value of XLEN? */
434 if (isdigit (op->subset[0]) && atoi (op->subset) != xlen)
435 continue;
436
437 /* It's a match. */
438 (*info->fprintf_func) (info->stream, "%s", op->name);
439 print_insn_args (op->args, word, memaddr, info);
440
441 /* Try to disassemble multi-instruction addressing sequences. */
442 if (pd->print_addr != (bfd_vma)-1)
443 {
444 info->target = pd->print_addr;
445 (*info->fprintf_func) (info->stream, " # ");
446 (*info->print_address_func) (info->target, info);
447 pd->print_addr = -1;
448 }
449
450 return insnlen;
451 }
452 }
453
454 /* We did not find a match, so just print the instruction bits. */
455 info->insn_type = dis_noninsn;
456 (*info->fprintf_func) (info->stream, "0x%llx", (unsigned long long)word);
457 return insnlen;
458 }
459
460 int
461 print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
462 {
463 bfd_byte packet[2];
464 insn_t insn = 0;
465 bfd_vma n;
466 int status;
467
468 if (info->disassembler_options != NULL)
469 {
470 parse_riscv_dis_options (info->disassembler_options);
471 /* Avoid repeatedly parsing the options. */
472 info->disassembler_options = NULL;
473 }
474 else if (riscv_gpr_names == NULL)
475 set_default_riscv_dis_options ();
476
477 /* Instructions are a sequence of 2-byte packets in little-endian order. */
478 for (n = 0; n < sizeof (insn) && n < riscv_insn_length (insn); n += 2)
479 {
480 status = (*info->read_memory_func) (memaddr + n, packet, 2, info);
481 if (status != 0)
482 {
483 /* Don't fail just because we fell off the end. */
484 if (n > 0)
485 break;
486 (*info->memory_error_func) (status, memaddr, info);
487 return status;
488 }
489
490 insn |= ((insn_t) bfd_getl16 (packet)) << (8 * n);
491 }
492
493 return riscv_disassemble_insn (memaddr, insn, info);
494 }
495
496 void
497 print_riscv_disassembler_options (FILE *stream)
498 {
499 fprintf (stream, _("\n\
500 The following RISC-V-specific disassembler options are supported for use\n\
501 with the -M switch (multiple options should be separated by commas):\n"));
502
503 fprintf (stream, _("\n\
504 numeric Print numeric register names, rather than ABI names.\n"));
505
506 fprintf (stream, _("\n\
507 no-aliases Disassemble only into canonical instructions, rather\n\
508 than into pseudoinstructions.\n"));
509
510 fprintf (stream, _("\n"));
511 }
512