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