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