/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 * const *riscv_gpr_names; |
73 | | static const char * const *riscv_fpr_names; |
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 | 2 | { |
84 | 2 | riscv_gpr_names = riscv_gpr_names_abi; |
85 | 2 | riscv_fpr_names = riscv_fpr_names_abi; |
86 | 2 | no_aliases = false; |
87 | 2 | } |
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 | 10.4k | { |
186 | 10.4k | const char *s = val >= size || array[val] == NULL ? "unknown" : array[val]; |
187 | 10.4k | (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s); |
188 | 10.4k | } |
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 | 69.3k | { |
196 | 69.3k | if (pd->hi_addr[base_reg] != (bfd_vma)-1) |
197 | 9.61k | { |
198 | 9.61k | pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset; |
199 | 9.61k | pd->hi_addr[base_reg] = -1; |
200 | 9.61k | } |
201 | 59.7k | else if (base_reg == X_GP && pd->has_gp) |
202 | 0 | pd->print_addr = pd->gp + offset; |
203 | 59.7k | else if (base_reg == X_TP || base_reg == 0) |
204 | 7.14k | pd->print_addr = offset; |
205 | 52.5k | else |
206 | 52.5k | return; /* Don't print the address. */ |
207 | 16.7k | pd->to_print_addr = true; |
208 | | |
209 | | /* Sign-extend a 32-bit value to a 64-bit value. */ |
210 | 16.7k | if (wide) |
211 | 1.68k | pd->print_addr = (bfd_vma)(int32_t) pd->print_addr; |
212 | | |
213 | | /* Fit into a 32-bit value on RV32. */ |
214 | 16.7k | if (xlen == 32) |
215 | 9.35k | pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr; |
216 | 16.7k | } |
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 | 1.04M | { |
223 | 1.04M | struct riscv_private_data *pd = info->private_data; |
224 | 1.04M | int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1; |
225 | 1.04M | int rd = (l >> OP_SH_RD) & OP_MASK_RD; |
226 | 1.04M | fprintf_styled_ftype print = info->fprintf_styled_func; |
227 | 1.04M | const char *opargStart; |
228 | | |
229 | 1.04M | if (*oparg != '\0') |
230 | 859k | print (info->stream, dis_style_text, "\t"); |
231 | | |
232 | 5.27M | for (; *oparg != '\0'; oparg++) |
233 | 4.22M | { |
234 | 4.22M | opargStart = oparg; |
235 | 4.22M | switch (*oparg) |
236 | 4.22M | { |
237 | 1.85M | case 'C': /* RVC */ |
238 | 1.85M | switch (*++oparg) |
239 | 1.85M | { |
240 | 309k | case 's': /* RS1 x8-x15. */ |
241 | 329k | case 'w': /* RS1 x8-x15. */ |
242 | 329k | print (info->stream, dis_style_register, "%s", |
243 | 329k | riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]); |
244 | 329k | break; |
245 | 190k | case 't': /* RS2 x8-x15. */ |
246 | 190k | case 'x': /* RS2 x8-x15. */ |
247 | 190k | print (info->stream, dis_style_register, "%s", |
248 | 190k | riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]); |
249 | 190k | break; |
250 | 93.1k | case 'U': /* RS1, constrained to equal RD. */ |
251 | 93.1k | print (info->stream, dis_style_register, |
252 | 93.1k | "%s", riscv_gpr_names[rd]); |
253 | 93.1k | break; |
254 | 266k | case 'c': /* RS1, constrained to equal sp. */ |
255 | 266k | print (info->stream, dis_style_register, "%s", |
256 | 266k | riscv_gpr_names[X_SP]); |
257 | 266k | break; |
258 | 54.9k | case 'V': /* RS2 */ |
259 | 54.9k | print (info->stream, dis_style_register, "%s", |
260 | 54.9k | riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]); |
261 | 54.9k | break; |
262 | 86.3k | case 'o': |
263 | 98.5k | case 'j': |
264 | 98.5k | if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0) |
265 | 39.7k | maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0); |
266 | 98.5k | if (info->mach == bfd_mach_riscv64 |
267 | 98.5k | && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0) |
268 | 8.07k | maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1); |
269 | 98.5k | print (info->stream, dis_style_immediate, "%d", |
270 | 98.5k | (int)EXTRACT_CITYPE_IMM (l)); |
271 | 98.5k | break; |
272 | 114k | case 'k': |
273 | 114k | print (info->stream, dis_style_address_offset, "%d", |
274 | 114k | (int)EXTRACT_CLTYPE_LW_IMM (l)); |
275 | 114k | break; |
276 | 117k | case 'l': |
277 | 117k | print (info->stream, dis_style_address_offset, "%d", |
278 | 117k | (int)EXTRACT_CLTYPE_LD_IMM (l)); |
279 | 117k | break; |
280 | 41.1k | case 'm': |
281 | 41.1k | print (info->stream, dis_style_address_offset, "%d", |
282 | 41.1k | (int)EXTRACT_CITYPE_LWSP_IMM (l)); |
283 | 41.1k | break; |
284 | 43.4k | case 'n': |
285 | 43.4k | print (info->stream, dis_style_address_offset, "%d", |
286 | 43.4k | (int)EXTRACT_CITYPE_LDSP_IMM (l)); |
287 | 43.4k | break; |
288 | 87.0k | case 'K': |
289 | 87.0k | print (info->stream, dis_style_immediate, "%d", |
290 | 87.0k | (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l)); |
291 | 87.0k | break; |
292 | 8.69k | case 'L': |
293 | 8.69k | print (info->stream, dis_style_immediate, "%d", |
294 | 8.69k | (int)EXTRACT_CITYPE_ADDI16SP_IMM (l)); |
295 | 8.69k | break; |
296 | 39.8k | case 'M': |
297 | 39.8k | print (info->stream, dis_style_address_offset, "%d", |
298 | 39.8k | (int)EXTRACT_CSSTYPE_SWSP_IMM (l)); |
299 | 39.8k | break; |
300 | 37.1k | case 'N': |
301 | 37.1k | print (info->stream, dis_style_address_offset, "%d", |
302 | 37.1k | (int)EXTRACT_CSSTYPE_SDSP_IMM (l)); |
303 | 37.1k | break; |
304 | 53.2k | case 'p': |
305 | 53.2k | info->target = EXTRACT_CBTYPE_IMM (l) + pc; |
306 | 53.2k | (*info->print_address_func) (info->target, info); |
307 | 53.2k | break; |
308 | 27.8k | case 'a': |
309 | 27.8k | info->target = EXTRACT_CJTYPE_IMM (l) + pc; |
310 | 27.8k | (*info->print_address_func) (info->target, info); |
311 | 27.8k | break; |
312 | 31.4k | case 'u': |
313 | 31.4k | print (info->stream, dis_style_immediate, "0x%x", |
314 | 31.4k | (unsigned)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1))); |
315 | 31.4k | break; |
316 | 51.2k | case '>': |
317 | 51.2k | print (info->stream, dis_style_immediate, "0x%x", |
318 | 51.2k | (unsigned)EXTRACT_CITYPE_IMM (l) & 0x3f); |
319 | 51.2k | 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 | 41.9k | case 'T': /* Floating-point RS2. */ |
325 | 41.9k | print (info->stream, dis_style_register, "%s", |
326 | 41.9k | riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]); |
327 | 41.9k | break; |
328 | 131k | case 'D': /* Floating-point RS2 x8-x15. */ |
329 | 131k | print (info->stream, dis_style_register, "%s", |
330 | 131k | riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]); |
331 | 131k | break; |
332 | 1.85M | } |
333 | 1.85M | break; |
334 | | |
335 | 1.85M | 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 'm': |
404 | 0 | if (!EXTRACT_OPERAND (VMASK, l)) |
405 | 0 | { |
406 | 0 | print (info->stream, dis_style_text, ","); |
407 | 0 | print (info->stream, dis_style_register, "%s", |
408 | 0 | riscv_vecm_names_numeric[0]); |
409 | 0 | } |
410 | 0 | break; |
411 | 0 | } |
412 | 0 | break; |
413 | | |
414 | 1.06M | case ',': |
415 | 1.47M | case '(': |
416 | 1.89M | case ')': |
417 | 1.89M | case '[': |
418 | 1.89M | case ']': |
419 | 1.89M | print (info->stream, dis_style_text, "%c", *oparg); |
420 | 1.89M | break; |
421 | | |
422 | 415 | case '0': |
423 | | /* Only print constant 0 if it is the last argument. */ |
424 | 415 | if (!oparg[1]) |
425 | 0 | print (info->stream, dis_style_immediate, "0"); |
426 | 415 | break; |
427 | | |
428 | 34.1k | case 's': |
429 | 34.1k | if ((l & MASK_JALR) == MATCH_JALR) |
430 | 1.31k | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0); |
431 | 34.1k | print (info->stream, dis_style_register, "%s", riscv_gpr_names[rs1]); |
432 | 34.1k | break; |
433 | | |
434 | 11.0k | case 't': |
435 | 11.0k | print (info->stream, dis_style_register, "%s", |
436 | 11.0k | riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]); |
437 | 11.0k | break; |
438 | | |
439 | 13.2k | case 'u': |
440 | 13.2k | print (info->stream, dis_style_immediate, "0x%x", |
441 | 13.2k | (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS); |
442 | 13.2k | break; |
443 | | |
444 | 8.93k | case 'm': |
445 | 8.93k | arg_print (info, EXTRACT_OPERAND (RM, l), |
446 | 8.93k | riscv_rm, ARRAY_SIZE (riscv_rm)); |
447 | 8.93k | break; |
448 | | |
449 | 736 | case 'P': |
450 | 736 | arg_print (info, EXTRACT_OPERAND (PRED, l), |
451 | 736 | riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ)); |
452 | 736 | break; |
453 | | |
454 | 736 | case 'Q': |
455 | 736 | arg_print (info, EXTRACT_OPERAND (SUCC, l), |
456 | 736 | riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ)); |
457 | 736 | break; |
458 | | |
459 | 11.6k | case 'o': |
460 | 11.6k | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0); |
461 | | /* Fall through. */ |
462 | 17.3k | case 'j': |
463 | 17.3k | if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0) |
464 | 17.3k | || (l & MASK_JALR) == MATCH_JALR) |
465 | 1.60k | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0); |
466 | 17.3k | if (info->mach == bfd_mach_riscv64 |
467 | 17.3k | && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0) |
468 | 524 | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1); |
469 | 17.3k | print (info->stream, dis_style_immediate, "%d", |
470 | 17.3k | (int)EXTRACT_ITYPE_IMM (l)); |
471 | 17.3k | break; |
472 | | |
473 | 6.43k | case 'q': |
474 | 6.43k | maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0); |
475 | 6.43k | print (info->stream, dis_style_address_offset, "%d", |
476 | 6.43k | (int)EXTRACT_STYPE_IMM (l)); |
477 | 6.43k | break; |
478 | | |
479 | 7.77k | case 'a': |
480 | 7.77k | info->target = EXTRACT_JTYPE_IMM (l) + pc; |
481 | 7.77k | (*info->print_address_func) (info->target, info); |
482 | 7.77k | break; |
483 | | |
484 | 6.48k | case 'p': |
485 | 6.48k | info->target = EXTRACT_BTYPE_IMM (l) + pc; |
486 | 6.48k | (*info->print_address_func) (info->target, info); |
487 | 6.48k | break; |
488 | | |
489 | 260k | case 'd': |
490 | 260k | if ((l & MASK_AUIPC) == MATCH_AUIPC) |
491 | 4.57k | pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l); |
492 | 255k | else if ((l & MASK_LUI) == MATCH_LUI) |
493 | 8.65k | pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l); |
494 | 246k | else if ((l & MASK_C_LUI) == MATCH_C_LUI) |
495 | 31.4k | pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l); |
496 | 260k | print (info->stream, dis_style_register, "%s", riscv_gpr_names[rd]); |
497 | 260k | break; |
498 | | |
499 | 0 | case 'y': |
500 | 0 | print (info->stream, dis_style_immediate, "0x%x", |
501 | 0 | (unsigned)EXTRACT_OPERAND (BS, l)); |
502 | 0 | break; |
503 | | |
504 | 0 | case 'z': |
505 | 0 | print (info->stream, dis_style_register, "%s", riscv_gpr_names[0]); |
506 | 0 | break; |
507 | | |
508 | 175 | case '>': |
509 | 175 | print (info->stream, dis_style_immediate, "0x%x", |
510 | 175 | (unsigned)EXTRACT_OPERAND (SHAMT, l)); |
511 | 175 | break; |
512 | | |
513 | 126 | case '<': |
514 | 126 | print (info->stream, dis_style_immediate, "0x%x", |
515 | 126 | (unsigned)EXTRACT_OPERAND (SHAMTW, l)); |
516 | 126 | break; |
517 | | |
518 | 10.0k | case 'S': |
519 | 10.1k | case 'U': |
520 | 10.1k | print (info->stream, dis_style_register, "%s", riscv_fpr_names[rs1]); |
521 | 10.1k | break; |
522 | | |
523 | 12.6k | case 'T': |
524 | 12.6k | print (info->stream, dis_style_register, "%s", |
525 | 12.6k | riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]); |
526 | 12.6k | break; |
527 | | |
528 | 61.2k | case 'D': |
529 | 61.2k | print (info->stream, dis_style_register, "%s", riscv_fpr_names[rd]); |
530 | 61.2k | break; |
531 | | |
532 | 9.25k | case 'R': |
533 | 9.25k | print (info->stream, dis_style_register, "%s", |
534 | 9.25k | riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]); |
535 | 9.25k | break; |
536 | | |
537 | 10.3k | case 'E': |
538 | 10.3k | { |
539 | 10.3k | static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */ |
540 | 10.3k | static bool init_csr = false; |
541 | 10.3k | unsigned int csr = EXTRACT_OPERAND (CSR, l); |
542 | | |
543 | 10.3k | if (!init_csr) |
544 | 10.3k | { |
545 | 10.3k | unsigned int i; |
546 | 42.3M | for (i = 0; i < 4096; i++) |
547 | 42.3M | riscv_csr_hash[i] = NULL; |
548 | | |
549 | | /* Set to the newest privileged version. */ |
550 | 10.3k | if (default_priv_spec == PRIV_SPEC_CLASS_NONE) |
551 | 2 | default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1; |
552 | | |
553 | 10.3k | #define DECLARE_CSR(name, num, class, define_version, abort_version) \ |
554 | 4.53M | if (riscv_csr_hash[num] == NULL \ |
555 | 4.53M | && ((define_version == PRIV_SPEC_CLASS_NONE \ |
556 | 4.40M | && abort_version == PRIV_SPEC_CLASS_NONE) \ |
557 | 4.40M | || (default_priv_spec >= define_version \ |
558 | 2.99M | && default_priv_spec < abort_version))) \ |
559 | 4.53M | riscv_csr_hash[num] = #name; |
560 | 10.3k | #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \ |
561 | 144k | DECLARE_CSR (name, num, class, define_version, abort_version) |
562 | 10.3k | #include "opcode/riscv-opc.h" |
563 | 10.3k | #undef DECLARE_CSR |
564 | 10.3k | } |
565 | | |
566 | 10.3k | if (riscv_csr_hash[csr] != NULL) |
567 | 1.06k | print (info->stream, dis_style_register, "%s", |
568 | 1.06k | riscv_csr_hash[csr]); |
569 | 9.27k | else |
570 | 9.27k | print (info->stream, dis_style_immediate, "0x%x", csr); |
571 | 10.3k | break; |
572 | 10.0k | } |
573 | | |
574 | 0 | case 'Y': |
575 | 0 | print (info->stream, dis_style_immediate, "0x%x", |
576 | 0 | (unsigned) EXTRACT_OPERAND (RNUM, l)); |
577 | 0 | break; |
578 | | |
579 | 7.96k | case 'Z': |
580 | 7.96k | print (info->stream, dis_style_immediate, "%d", rs1); |
581 | 7.96k | break; |
582 | | |
583 | 0 | case 'W': /* Various operands. */ |
584 | 0 | { |
585 | 0 | switch (*++oparg) |
586 | 0 | { |
587 | 0 | case 'i': |
588 | 0 | switch (*++oparg) |
589 | 0 | { |
590 | 0 | case 'f': |
591 | 0 | print (info->stream, dis_style_address_offset, "%d", |
592 | 0 | (int) EXTRACT_STYPE_IMM (l)); |
593 | 0 | break; |
594 | 0 | default: |
595 | 0 | goto undefined_modifier; |
596 | 0 | } |
597 | 0 | break; |
598 | 0 | default: |
599 | 0 | goto undefined_modifier; |
600 | 0 | } |
601 | 0 | } |
602 | 0 | break; |
603 | | |
604 | 0 | case 'X': /* Integer immediate. */ |
605 | 0 | { |
606 | 0 | size_t n; |
607 | 0 | size_t s; |
608 | 0 | bool sign; |
609 | |
|
610 | 0 | switch (*++oparg) |
611 | 0 | { |
612 | 0 | case 'l': /* Literal. */ |
613 | 0 | oparg++; |
614 | 0 | while (*oparg && *oparg != ',') |
615 | 0 | { |
616 | 0 | print (info->stream, dis_style_immediate, "%c", *oparg); |
617 | 0 | oparg++; |
618 | 0 | } |
619 | 0 | oparg--; |
620 | 0 | break; |
621 | 0 | case 's': /* 'XsN@S' ... N-bit signed immediate at bit S. */ |
622 | 0 | sign = true; |
623 | 0 | goto print_imm; |
624 | 0 | case 'u': /* 'XuN@S' ... N-bit unsigned immediate at bit S. */ |
625 | 0 | sign = false; |
626 | 0 | goto print_imm; |
627 | 0 | print_imm: |
628 | 0 | n = strtol (oparg + 1, (char **)&oparg, 10); |
629 | 0 | if (*oparg != '@') |
630 | 0 | goto undefined_modifier; |
631 | 0 | s = strtol (oparg + 1, (char **)&oparg, 10); |
632 | 0 | oparg--; |
633 | |
|
634 | 0 | if (!sign) |
635 | 0 | print (info->stream, dis_style_immediate, "%lu", |
636 | 0 | (unsigned long)EXTRACT_U_IMM (n, s, l)); |
637 | 0 | else |
638 | 0 | print (info->stream, dis_style_immediate, "%li", |
639 | 0 | (signed long)EXTRACT_S_IMM (n, s, l)); |
640 | 0 | break; |
641 | 0 | default: |
642 | 0 | goto undefined_modifier; |
643 | 0 | } |
644 | 0 | } |
645 | 0 | break; |
646 | 0 | default: |
647 | 0 | undefined_modifier: |
648 | | /* xgettext:c-format */ |
649 | 0 | print (info->stream, dis_style_text, |
650 | 0 | _("# internal error, undefined modifier (%c)"), |
651 | 0 | *opargStart); |
652 | 0 | return; |
653 | 4.22M | } |
654 | 4.22M | } |
655 | 1.04M | } |
656 | | |
657 | | /* Print the RISC-V instruction at address MEMADDR in debugged memory, |
658 | | on using INFO. Returns length of the instruction, in bytes. |
659 | | BIGENDIAN must be 1 if this is big-endian code, 0 if |
660 | | this is little-endian code. */ |
661 | | |
662 | | static int |
663 | | riscv_disassemble_insn (bfd_vma memaddr, |
664 | | insn_t word, |
665 | | const bfd_byte *packet, |
666 | | disassemble_info *info) |
667 | 1.32M | { |
668 | 1.32M | const struct riscv_opcode *op; |
669 | 1.32M | static bool init = false; |
670 | 1.32M | static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1]; |
671 | 1.32M | struct riscv_private_data *pd = info->private_data; |
672 | 1.32M | int insnlen, i; |
673 | 1.32M | bool printed; |
674 | | |
675 | 1.32M | #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP)) |
676 | | |
677 | | /* Build a hash table to shorten the search time. */ |
678 | 1.32M | if (! init) |
679 | 2 | { |
680 | 3.06k | for (op = riscv_opcodes; op->name; op++) |
681 | 3.06k | if (!riscv_hash[OP_HASH_IDX (op->match)]) |
682 | 54 | riscv_hash[OP_HASH_IDX (op->match)] = op; |
683 | | |
684 | 2 | init = true; |
685 | 2 | } |
686 | | |
687 | 1.32M | insnlen = riscv_insn_length (word); |
688 | | |
689 | | /* RISC-V instructions are always little-endian. */ |
690 | 1.32M | info->endian_code = BFD_ENDIAN_LITTLE; |
691 | | |
692 | 1.32M | info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2; |
693 | 1.32M | info->bytes_per_line = 8; |
694 | | /* We don't support constant pools, so this must be code. */ |
695 | 1.32M | info->display_endian = info->endian_code; |
696 | 1.32M | info->insn_info_valid = 1; |
697 | 1.32M | info->branch_delay_insns = 0; |
698 | 1.32M | info->data_size = 0; |
699 | 1.32M | info->insn_type = dis_nonbranch; |
700 | 1.32M | info->target = 0; |
701 | 1.32M | info->target2 = 0; |
702 | | |
703 | 1.32M | op = riscv_hash[OP_HASH_IDX (word)]; |
704 | 1.32M | if (op != NULL) |
705 | 1.26M | { |
706 | | /* If XLEN is not known, get its value from the ELF class. */ |
707 | 1.26M | if (info->mach == bfd_mach_riscv64) |
708 | 322k | xlen = 64; |
709 | 942k | else if (info->mach == bfd_mach_riscv32) |
710 | 629k | xlen = 32; |
711 | 312k | else if (info->section != NULL) |
712 | 0 | { |
713 | 0 | Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner); |
714 | 0 | xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32; |
715 | 0 | } |
716 | | |
717 | | /* If arch has the Zfinx extension, replace FPR with GPR. */ |
718 | 1.26M | if (riscv_subset_supports (&riscv_rps_dis, "zfinx")) |
719 | 0 | riscv_fpr_names = riscv_gpr_names; |
720 | 1.26M | else |
721 | 1.26M | riscv_fpr_names = riscv_gpr_names == riscv_gpr_names_abi ? |
722 | 1.26M | riscv_fpr_names_abi : riscv_fpr_names_numeric; |
723 | | |
724 | 465M | for (; op->name; op++) |
725 | 465M | { |
726 | | /* Does the opcode match? */ |
727 | 465M | if (! (op->match_func) (op, word)) |
728 | 463M | continue; |
729 | | /* Is this a pseudo-instruction and may we print it as such? */ |
730 | 1.23M | if (no_aliases && (op->pinfo & INSN_ALIAS)) |
731 | 0 | continue; |
732 | | /* Is this instruction restricted to a certain value of XLEN? */ |
733 | 1.23M | if ((op->xlen_requirement != 0) && (op->xlen_requirement != xlen)) |
734 | 125k | continue; |
735 | | /* Is this instruction supported by the current architecture? */ |
736 | 1.11M | if (!riscv_multi_subset_supports (&riscv_rps_dis, op->insn_class)) |
737 | 62.5k | continue; |
738 | | |
739 | | /* It's a match. */ |
740 | 1.04M | (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic, |
741 | 1.04M | "%s", op->name); |
742 | 1.04M | print_insn_args (op->args, word, memaddr, info); |
743 | | |
744 | | /* Try to disassemble multi-instruction addressing sequences. */ |
745 | 1.04M | if (pd->to_print_addr) |
746 | 16.5k | { |
747 | 16.5k | info->target = pd->print_addr; |
748 | 16.5k | (*info->fprintf_styled_func) |
749 | 16.5k | (info->stream, dis_style_comment_start, " # "); |
750 | 16.5k | (*info->print_address_func) (info->target, info); |
751 | 16.5k | pd->to_print_addr = false; |
752 | 16.5k | } |
753 | | |
754 | | /* Finish filling out insn_info fields. */ |
755 | 1.04M | switch (op->pinfo & INSN_TYPE) |
756 | 1.04M | { |
757 | 20.3k | case INSN_BRANCH: |
758 | 20.3k | info->insn_type = dis_branch; |
759 | 20.3k | break; |
760 | 59.7k | case INSN_CONDBRANCH: |
761 | 59.7k | info->insn_type = dis_condbranch; |
762 | 59.7k | break; |
763 | 17.8k | case INSN_JSR: |
764 | 17.8k | info->insn_type = dis_jsr; |
765 | 17.8k | break; |
766 | 411k | case INSN_DREF: |
767 | 411k | info->insn_type = dis_dref; |
768 | 411k | break; |
769 | 540k | default: |
770 | 540k | break; |
771 | 1.04M | } |
772 | | |
773 | 1.04M | if (op->pinfo & INSN_DATA_SIZE) |
774 | 411k | { |
775 | 411k | int size = ((op->pinfo & INSN_DATA_SIZE) |
776 | 411k | >> INSN_DATA_SIZE_SHIFT); |
777 | 411k | info->data_size = 1 << (size - 1); |
778 | 411k | } |
779 | | |
780 | 1.04M | return insnlen; |
781 | 1.04M | } |
782 | 1.26M | } |
783 | | |
784 | | /* We did not find a match, so just print the instruction bits in |
785 | | the shape of an assembler .insn directive. */ |
786 | 275k | info->insn_type = dis_noninsn; |
787 | 275k | (*info->fprintf_styled_func) |
788 | 275k | (info->stream, dis_style_assembler_directive, ".insn"); |
789 | 275k | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
790 | 275k | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, |
791 | 275k | "%d", insnlen); |
792 | 275k | (*info->fprintf_styled_func) (info->stream, dis_style_text, ", "); |
793 | 275k | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x"); |
794 | 841k | for (i = insnlen, printed = false; i >= 2; ) |
795 | 566k | { |
796 | 566k | i -= 2; |
797 | 566k | word = bfd_get_bits (packet + i, 16, false); |
798 | 566k | if (!word && !printed) |
799 | 14.6k | continue; |
800 | | |
801 | 551k | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, |
802 | 551k | "%04x", (unsigned int) word); |
803 | 551k | printed = true; |
804 | 551k | } |
805 | | |
806 | 275k | return insnlen; |
807 | 1.32M | } |
808 | | |
809 | | /* Return true if we find the suitable mapping symbol, |
810 | | and also update the STATE. Otherwise, return false. */ |
811 | | |
812 | | static bool |
813 | | riscv_get_map_state (int n, |
814 | | enum riscv_seg_mstate *state, |
815 | | struct disassemble_info *info) |
816 | 0 | { |
817 | 0 | const char *name; |
818 | | |
819 | | /* If the symbol is in a different section, ignore it. */ |
820 | 0 | if (info->section != NULL |
821 | 0 | && info->section != info->symtab[n]->section) |
822 | 0 | return false; |
823 | | |
824 | 0 | name = bfd_asymbol_name(info->symtab[n]); |
825 | 0 | if (strcmp (name, "$x") == 0) |
826 | 0 | *state = MAP_INSN; |
827 | 0 | else if (strcmp (name, "$d") == 0) |
828 | 0 | *state = MAP_DATA; |
829 | 0 | else if (strncmp (name, "$xrv", 4) == 0) |
830 | 0 | { |
831 | 0 | *state = MAP_INSN; |
832 | 0 | riscv_release_subset_list (&riscv_subsets); |
833 | 0 | riscv_parse_subset (&riscv_rps_dis, name + 2); |
834 | 0 | } |
835 | 0 | else |
836 | 0 | return false; |
837 | | |
838 | 0 | return true; |
839 | 0 | } |
840 | | |
841 | | /* Check the sorted symbol table (sorted by the symbol value), find the |
842 | | suitable mapping symbols. */ |
843 | | |
844 | | static enum riscv_seg_mstate |
845 | | riscv_search_mapping_symbol (bfd_vma memaddr, |
846 | | struct disassemble_info *info) |
847 | 1.32M | { |
848 | 1.32M | enum riscv_seg_mstate mstate; |
849 | 1.32M | bool from_last_map_symbol; |
850 | 1.32M | bool found = false; |
851 | 1.32M | int symbol = -1; |
852 | 1.32M | int n; |
853 | | |
854 | | /* Return the last map state if the address is still within the range of the |
855 | | last mapping symbol. */ |
856 | 1.32M | if (last_map_section == info->section |
857 | 1.32M | && (memaddr < last_map_symbol_boundary)) |
858 | 0 | return last_map_state; |
859 | | |
860 | 1.32M | last_map_section = info->section; |
861 | | |
862 | | /* Decide whether to print the data or instruction by default, in case |
863 | | we can not find the corresponding mapping symbols. */ |
864 | 1.32M | mstate = MAP_DATA; |
865 | 1.32M | if ((info->section |
866 | 1.32M | && info->section->flags & SEC_CODE) |
867 | 1.32M | || !info->section) |
868 | 746k | mstate = MAP_INSN; |
869 | | |
870 | 1.32M | if (info->symtab_size == 0 |
871 | 1.32M | || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour) |
872 | 1.32M | return mstate; |
873 | | |
874 | | /* Reset the last_map_symbol if we start to dump a new section. */ |
875 | 0 | if (memaddr <= 0) |
876 | 0 | last_map_symbol = -1; |
877 | | |
878 | | /* If the last stop offset is different from the current one, then |
879 | | don't use the last_map_symbol to search. We usually reset the |
880 | | info->stop_offset when handling a new section. */ |
881 | 0 | from_last_map_symbol = (last_map_symbol >= 0 |
882 | 0 | && info->stop_offset == last_stop_offset); |
883 | | |
884 | | /* Start scanning at the start of the function, or wherever |
885 | | we finished last time. */ |
886 | 0 | n = info->symtab_pos + 1; |
887 | 0 | if (from_last_map_symbol && n >= last_map_symbol) |
888 | 0 | n = last_map_symbol; |
889 | | |
890 | | /* Find the suitable mapping symbol to dump. */ |
891 | 0 | for (; n < info->symtab_size; n++) |
892 | 0 | { |
893 | 0 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); |
894 | | /* We have searched all possible symbols in the range. */ |
895 | 0 | if (addr > memaddr) |
896 | 0 | break; |
897 | 0 | if (riscv_get_map_state (n, &mstate, info)) |
898 | 0 | { |
899 | 0 | symbol = n; |
900 | 0 | found = true; |
901 | | /* Do not stop searching, in case there are some mapping |
902 | | symbols have the same value, but have different names. |
903 | | Use the last one. */ |
904 | 0 | } |
905 | 0 | } |
906 | | |
907 | | /* We can not find the suitable mapping symbol above. Therefore, we |
908 | | look forwards and try to find it again, but don't go pass the start |
909 | | of the section. Otherwise a data section without mapping symbols |
910 | | can pick up a text mapping symbol of a preceeding section. */ |
911 | 0 | if (!found) |
912 | 0 | { |
913 | 0 | n = info->symtab_pos; |
914 | 0 | if (from_last_map_symbol && n >= last_map_symbol) |
915 | 0 | n = last_map_symbol; |
916 | |
|
917 | 0 | for (; n >= 0; n--) |
918 | 0 | { |
919 | 0 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); |
920 | | /* We have searched all possible symbols in the range. */ |
921 | 0 | if (addr < (info->section ? info->section->vma : 0)) |
922 | 0 | break; |
923 | | /* Stop searching once we find the closed mapping symbol. */ |
924 | 0 | if (riscv_get_map_state (n, &mstate, info)) |
925 | 0 | { |
926 | 0 | symbol = n; |
927 | 0 | found = true; |
928 | 0 | break; |
929 | 0 | } |
930 | 0 | } |
931 | 0 | } |
932 | |
|
933 | 0 | if (found) |
934 | 0 | { |
935 | | /* Find the next mapping symbol to determine the boundary of this mapping |
936 | | symbol. */ |
937 | |
|
938 | 0 | bool found_next = false; |
939 | | /* Try to found next mapping symbol. */ |
940 | 0 | for (n = symbol + 1; n < info->symtab_size; n++) |
941 | 0 | { |
942 | 0 | if (info->symtab[symbol]->section != info->symtab[n]->section) |
943 | 0 | continue; |
944 | | |
945 | 0 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); |
946 | 0 | const char *sym_name = bfd_asymbol_name(info->symtab[n]); |
947 | 0 | if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')) |
948 | 0 | { |
949 | | /* The next mapping symbol has been found, and it represents the |
950 | | boundary of this mapping symbol. */ |
951 | 0 | found_next = true; |
952 | 0 | last_map_symbol_boundary = addr; |
953 | 0 | break; |
954 | 0 | } |
955 | 0 | } |
956 | | |
957 | | /* No further mapping symbol has been found, indicating that the boundary |
958 | | of the current mapping symbol is the end of this section. */ |
959 | 0 | if (!found_next) |
960 | 0 | last_map_symbol_boundary = info->section->vma + info->section->size; |
961 | 0 | } |
962 | | |
963 | | /* Save the information for next use. */ |
964 | 0 | last_map_symbol = symbol; |
965 | 0 | last_stop_offset = info->stop_offset; |
966 | |
|
967 | 0 | return mstate; |
968 | 1.32M | } |
969 | | |
970 | | /* Decide which data size we should print. */ |
971 | | |
972 | | static bfd_vma |
973 | | riscv_data_length (bfd_vma memaddr, |
974 | | disassemble_info *info) |
975 | 0 | { |
976 | 0 | bfd_vma length; |
977 | 0 | bool found = false; |
978 | |
|
979 | 0 | length = 4; |
980 | 0 | if (info->symtab_size != 0 |
981 | 0 | && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour |
982 | 0 | && last_map_symbol >= 0) |
983 | 0 | { |
984 | 0 | int n; |
985 | 0 | enum riscv_seg_mstate m = MAP_NONE; |
986 | 0 | for (n = last_map_symbol + 1; n < info->symtab_size; n++) |
987 | 0 | { |
988 | 0 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); |
989 | 0 | if (addr > memaddr |
990 | 0 | && riscv_get_map_state (n, &m, info)) |
991 | 0 | { |
992 | 0 | if (addr - memaddr < length) |
993 | 0 | length = addr - memaddr; |
994 | 0 | found = true; |
995 | 0 | break; |
996 | 0 | } |
997 | 0 | } |
998 | 0 | } |
999 | 0 | if (!found) |
1000 | 0 | { |
1001 | | /* Do not set the length which exceeds the section size. */ |
1002 | 0 | bfd_vma offset = info->section->vma + info->section->size; |
1003 | 0 | offset -= memaddr; |
1004 | 0 | length = (offset < length) ? offset : length; |
1005 | 0 | } |
1006 | 0 | length = length == 3 ? 2 : length; |
1007 | 0 | return length; |
1008 | 0 | } |
1009 | | |
1010 | | /* Dump the data contents. */ |
1011 | | |
1012 | | static int |
1013 | | riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED, |
1014 | | insn_t data, |
1015 | | const bfd_byte *packet ATTRIBUTE_UNUSED, |
1016 | | disassemble_info *info) |
1017 | 0 | { |
1018 | 0 | info->display_endian = info->endian; |
1019 | |
|
1020 | 0 | switch (info->bytes_per_chunk) |
1021 | 0 | { |
1022 | 0 | case 1: |
1023 | 0 | info->bytes_per_line = 6; |
1024 | 0 | (*info->fprintf_styled_func) |
1025 | 0 | (info->stream, dis_style_assembler_directive, ".byte"); |
1026 | 0 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1027 | 0 | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, |
1028 | 0 | "0x%02x", (unsigned)data); |
1029 | 0 | break; |
1030 | 0 | case 2: |
1031 | 0 | info->bytes_per_line = 8; |
1032 | 0 | (*info->fprintf_styled_func) |
1033 | 0 | (info->stream, dis_style_assembler_directive, ".short"); |
1034 | 0 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1035 | 0 | (*info->fprintf_styled_func) |
1036 | 0 | (info->stream, dis_style_immediate, "0x%04x", (unsigned) data); |
1037 | 0 | break; |
1038 | 0 | case 4: |
1039 | 0 | info->bytes_per_line = 8; |
1040 | 0 | (*info->fprintf_styled_func) |
1041 | 0 | (info->stream, dis_style_assembler_directive, ".word"); |
1042 | 0 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1043 | 0 | (*info->fprintf_styled_func) |
1044 | 0 | (info->stream, dis_style_immediate, "0x%08lx", |
1045 | 0 | (unsigned long) data); |
1046 | 0 | break; |
1047 | 0 | case 8: |
1048 | 0 | info->bytes_per_line = 8; |
1049 | 0 | (*info->fprintf_styled_func) |
1050 | 0 | (info->stream, dis_style_assembler_directive, ".dword"); |
1051 | 0 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1052 | 0 | (*info->fprintf_styled_func) |
1053 | 0 | (info->stream, dis_style_immediate, "0x%016llx", |
1054 | 0 | (unsigned long long) data); |
1055 | 0 | break; |
1056 | 0 | default: |
1057 | 0 | abort (); |
1058 | 0 | } |
1059 | 0 | return info->bytes_per_chunk; |
1060 | 0 | } |
1061 | | |
1062 | | static bool |
1063 | | riscv_init_disasm_info (struct disassemble_info *info) |
1064 | 690 | { |
1065 | 690 | int i; |
1066 | | |
1067 | 690 | struct riscv_private_data *pd = |
1068 | 690 | xcalloc (1, sizeof (struct riscv_private_data)); |
1069 | 690 | pd->gp = 0; |
1070 | 690 | pd->print_addr = 0; |
1071 | 22.7k | for (i = 0; i < (int) ARRAY_SIZE (pd->hi_addr); i++) |
1072 | 22.0k | pd->hi_addr[i] = -1; |
1073 | 690 | pd->to_print_addr = false; |
1074 | 690 | pd->has_gp = false; |
1075 | | |
1076 | 690 | for (i = 0; i < info->symtab_size; i++) |
1077 | 0 | { |
1078 | 0 | asymbol *sym = info->symtab[i]; |
1079 | 0 | if (strcmp (bfd_asymbol_name (sym), RISCV_GP_SYMBOL) == 0) |
1080 | 0 | { |
1081 | 0 | pd->gp = bfd_asymbol_value (sym); |
1082 | 0 | pd->has_gp = true; |
1083 | 0 | } |
1084 | 0 | } |
1085 | | |
1086 | 690 | info->private_data = pd; |
1087 | 690 | return true; |
1088 | 690 | } |
1089 | | |
1090 | | int |
1091 | | print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info) |
1092 | 1.32M | { |
1093 | 1.32M | bfd_byte packet[RISCV_MAX_INSN_LEN]; |
1094 | 1.32M | insn_t insn = 0; |
1095 | 1.32M | bfd_vma dump_size; |
1096 | 1.32M | int status; |
1097 | 1.32M | enum riscv_seg_mstate mstate; |
1098 | 1.32M | int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *, |
1099 | 1.32M | struct disassemble_info *); |
1100 | | |
1101 | 1.32M | if (info->disassembler_options != NULL) |
1102 | 0 | { |
1103 | 0 | parse_riscv_dis_options (info->disassembler_options); |
1104 | | /* Avoid repeatedly parsing the options. */ |
1105 | 0 | info->disassembler_options = NULL; |
1106 | 0 | } |
1107 | 1.32M | else if (riscv_gpr_names == NULL) |
1108 | 2 | set_default_riscv_dis_options (); |
1109 | | |
1110 | 1.32M | if (info->private_data == NULL && !riscv_init_disasm_info (info)) |
1111 | 0 | return -1; |
1112 | | |
1113 | 1.32M | mstate = riscv_search_mapping_symbol (memaddr, info); |
1114 | | /* Save the last mapping state. */ |
1115 | 1.32M | last_map_state = mstate; |
1116 | | |
1117 | | /* Set the size to dump. */ |
1118 | 1.32M | if (mstate == MAP_DATA |
1119 | 1.32M | && (info->flags & DISASSEMBLE_DATA) == 0) |
1120 | 0 | { |
1121 | 0 | dump_size = riscv_data_length (memaddr, info); |
1122 | 0 | info->bytes_per_chunk = dump_size; |
1123 | 0 | riscv_disassembler = riscv_disassemble_data; |
1124 | 0 | } |
1125 | 1.32M | else |
1126 | 1.32M | { |
1127 | | /* Get the first 2-bytes to check the lenghth of instruction. */ |
1128 | 1.32M | status = (*info->read_memory_func) (memaddr, packet, 2, info); |
1129 | 1.32M | if (status != 0) |
1130 | 272 | { |
1131 | 272 | (*info->memory_error_func) (status, memaddr, info); |
1132 | 272 | return -1; |
1133 | 272 | } |
1134 | 1.32M | insn = (insn_t) bfd_getl16 (packet); |
1135 | 1.32M | dump_size = riscv_insn_length (insn); |
1136 | 1.32M | riscv_disassembler = riscv_disassemble_insn; |
1137 | 1.32M | } |
1138 | | |
1139 | | /* Fetch the instruction to dump. */ |
1140 | 1.32M | status = (*info->read_memory_func) (memaddr, packet, dump_size, info); |
1141 | 1.32M | if (status != 0) |
1142 | 205 | { |
1143 | 205 | (*info->memory_error_func) (status, memaddr, info); |
1144 | 205 | return -1; |
1145 | 205 | } |
1146 | 1.32M | insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false); |
1147 | | |
1148 | 1.32M | return (*riscv_disassembler) (memaddr, insn, packet, info); |
1149 | 1.32M | } |
1150 | | |
1151 | | disassembler_ftype |
1152 | | riscv_get_disassembler (bfd *abfd) |
1153 | 704 | { |
1154 | 704 | const char *default_arch = "rv64gc"; |
1155 | | |
1156 | 704 | if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour) |
1157 | 203 | { |
1158 | 203 | const char *sec_name = get_elf_backend_data (abfd)->obj_attrs_section; |
1159 | 203 | if (bfd_get_section_by_name (abfd, sec_name) != NULL) |
1160 | 0 | { |
1161 | 0 | obj_attribute *attr = elf_known_obj_attributes_proc (abfd); |
1162 | 0 | unsigned int Tag_a = Tag_RISCV_priv_spec; |
1163 | 0 | unsigned int Tag_b = Tag_RISCV_priv_spec_minor; |
1164 | 0 | unsigned int Tag_c = Tag_RISCV_priv_spec_revision; |
1165 | 0 | riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i, |
1166 | 0 | attr[Tag_b].i, |
1167 | 0 | attr[Tag_c].i, |
1168 | 0 | &default_priv_spec); |
1169 | 0 | default_arch = attr[Tag_RISCV_arch].s; |
1170 | 0 | } |
1171 | 203 | } |
1172 | | |
1173 | 704 | riscv_release_subset_list (&riscv_subsets); |
1174 | 704 | riscv_parse_subset (&riscv_rps_dis, default_arch); |
1175 | 704 | return print_insn_riscv; |
1176 | 704 | } |
1177 | | |
1178 | | /* Prevent use of the fake labels that are generated as part of the DWARF |
1179 | | and for relaxable relocations in the assembler. */ |
1180 | | |
1181 | | bool |
1182 | | riscv_symbol_is_valid (asymbol * sym, |
1183 | | struct disassemble_info * info ATTRIBUTE_UNUSED) |
1184 | 0 | { |
1185 | 0 | const char * name; |
1186 | |
|
1187 | 0 | if (sym == NULL) |
1188 | 0 | return false; |
1189 | | |
1190 | 0 | name = bfd_asymbol_name (sym); |
1191 | |
|
1192 | 0 | return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0 |
1193 | 0 | && !riscv_elf_is_mapping_symbols (name)); |
1194 | 0 | } |
1195 | | |
1196 | | |
1197 | | /* Indices into option argument vector for options accepting an argument. |
1198 | | Use RISCV_OPTION_ARG_NONE for options accepting no argument. */ |
1199 | | |
1200 | | typedef enum |
1201 | | { |
1202 | | RISCV_OPTION_ARG_NONE = -1, |
1203 | | RISCV_OPTION_ARG_PRIV_SPEC, |
1204 | | |
1205 | | RISCV_OPTION_ARG_COUNT |
1206 | | } riscv_option_arg_t; |
1207 | | |
1208 | | /* Valid RISCV disassembler options. */ |
1209 | | |
1210 | | static struct |
1211 | | { |
1212 | | const char *name; |
1213 | | const char *description; |
1214 | | riscv_option_arg_t arg; |
1215 | | } riscv_options[] = |
1216 | | { |
1217 | | { "numeric", |
1218 | | N_("Print numeric register names, rather than ABI names."), |
1219 | | RISCV_OPTION_ARG_NONE }, |
1220 | | { "no-aliases", |
1221 | | N_("Disassemble only into canonical instructions."), |
1222 | | RISCV_OPTION_ARG_NONE }, |
1223 | | { "priv-spec=", |
1224 | | N_("Print the CSR according to the chosen privilege spec."), |
1225 | | RISCV_OPTION_ARG_PRIV_SPEC } |
1226 | | }; |
1227 | | |
1228 | | /* Build the structure representing valid RISCV disassembler options. |
1229 | | This is done dynamically for maintenance ease purpose; a static |
1230 | | initializer would be unreadable. */ |
1231 | | |
1232 | | const disasm_options_and_args_t * |
1233 | | disassembler_options_riscv (void) |
1234 | 0 | { |
1235 | 0 | static disasm_options_and_args_t *opts_and_args; |
1236 | |
|
1237 | 0 | if (opts_and_args == NULL) |
1238 | 0 | { |
1239 | 0 | size_t num_options = ARRAY_SIZE (riscv_options); |
1240 | 0 | size_t num_args = RISCV_OPTION_ARG_COUNT; |
1241 | 0 | disasm_option_arg_t *args; |
1242 | 0 | disasm_options_t *opts; |
1243 | 0 | size_t i, priv_spec_count; |
1244 | |
|
1245 | 0 | args = XNEWVEC (disasm_option_arg_t, num_args + 1); |
1246 | |
|
1247 | 0 | args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC"; |
1248 | 0 | priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_CLASS_NONE - 1; |
1249 | 0 | args[RISCV_OPTION_ARG_PRIV_SPEC].values |
1250 | 0 | = XNEWVEC (const char *, priv_spec_count + 1); |
1251 | 0 | for (i = 0; i < priv_spec_count; i++) |
1252 | 0 | args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] |
1253 | 0 | = riscv_priv_specs[i].name; |
1254 | | /* The array we return must be NULL terminated. */ |
1255 | 0 | args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL; |
1256 | | |
1257 | | /* The array we return must be NULL terminated. */ |
1258 | 0 | args[num_args].name = NULL; |
1259 | 0 | args[num_args].values = NULL; |
1260 | |
|
1261 | 0 | opts_and_args = XNEW (disasm_options_and_args_t); |
1262 | 0 | opts_and_args->args = args; |
1263 | |
|
1264 | 0 | opts = &opts_and_args->options; |
1265 | 0 | opts->name = XNEWVEC (const char *, num_options + 1); |
1266 | 0 | opts->description = XNEWVEC (const char *, num_options + 1); |
1267 | 0 | opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1); |
1268 | 0 | for (i = 0; i < num_options; i++) |
1269 | 0 | { |
1270 | 0 | opts->name[i] = riscv_options[i].name; |
1271 | 0 | opts->description[i] = _(riscv_options[i].description); |
1272 | 0 | if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE) |
1273 | 0 | opts->arg[i] = &args[riscv_options[i].arg]; |
1274 | 0 | else |
1275 | 0 | opts->arg[i] = NULL; |
1276 | 0 | } |
1277 | | /* The array we return must be NULL terminated. */ |
1278 | 0 | opts->name[i] = NULL; |
1279 | 0 | opts->description[i] = NULL; |
1280 | 0 | opts->arg[i] = NULL; |
1281 | 0 | } |
1282 | |
|
1283 | 0 | return opts_and_args; |
1284 | 0 | } |
1285 | | |
1286 | | void |
1287 | | print_riscv_disassembler_options (FILE *stream) |
1288 | 0 | { |
1289 | 0 | const disasm_options_and_args_t *opts_and_args; |
1290 | 0 | const disasm_option_arg_t *args; |
1291 | 0 | const disasm_options_t *opts; |
1292 | 0 | size_t max_len = 0; |
1293 | 0 | size_t i; |
1294 | 0 | size_t j; |
1295 | |
|
1296 | 0 | opts_and_args = disassembler_options_riscv (); |
1297 | 0 | opts = &opts_and_args->options; |
1298 | 0 | args = opts_and_args->args; |
1299 | |
|
1300 | 0 | fprintf (stream, _("\n\ |
1301 | 0 | The following RISC-V specific disassembler options are supported for use\n\ |
1302 | 0 | with the -M switch (multiple options should be separated by commas):\n")); |
1303 | 0 | fprintf (stream, "\n"); |
1304 | | |
1305 | | /* Compute the length of the longest option name. */ |
1306 | 0 | for (i = 0; opts->name[i] != NULL; i++) |
1307 | 0 | { |
1308 | 0 | size_t len = strlen (opts->name[i]); |
1309 | |
|
1310 | 0 | if (opts->arg[i] != NULL) |
1311 | 0 | len += strlen (opts->arg[i]->name); |
1312 | 0 | if (max_len < len) |
1313 | 0 | max_len = len; |
1314 | 0 | } |
1315 | |
|
1316 | 0 | for (i = 0, max_len++; opts->name[i] != NULL; i++) |
1317 | 0 | { |
1318 | 0 | fprintf (stream, " %s", opts->name[i]); |
1319 | 0 | if (opts->arg[i] != NULL) |
1320 | 0 | fprintf (stream, "%s", opts->arg[i]->name); |
1321 | 0 | if (opts->description[i] != NULL) |
1322 | 0 | { |
1323 | 0 | size_t len = strlen (opts->name[i]); |
1324 | |
|
1325 | 0 | if (opts->arg != NULL && opts->arg[i] != NULL) |
1326 | 0 | len += strlen (opts->arg[i]->name); |
1327 | 0 | fprintf (stream, "%*c %s", (int) (max_len - len), ' ', |
1328 | 0 | opts->description[i]); |
1329 | 0 | } |
1330 | 0 | fprintf (stream, "\n"); |
1331 | 0 | } |
1332 | |
|
1333 | 0 | for (i = 0; args[i].name != NULL; i++) |
1334 | 0 | { |
1335 | 0 | if (args[i].values == NULL) |
1336 | 0 | continue; |
1337 | 0 | fprintf (stream, _("\n\ |
1338 | 0 | For the options above, the following values are supported for \"%s\":\n "), |
1339 | 0 | args[i].name); |
1340 | 0 | for (j = 0; args[i].values[j] != NULL; j++) |
1341 | 0 | fprintf (stream, " %s", args[i].values[j]); |
1342 | 0 | fprintf (stream, _("\n")); |
1343 | 0 | } |
1344 | |
|
1345 | 0 | fprintf (stream, _("\n")); |
1346 | 0 | } |
1347 | | |
1348 | | void disassemble_free_riscv (struct disassemble_info *info ATTRIBUTE_UNUSED) |
1349 | 704 | { |
1350 | 704 | riscv_release_subset_list (&riscv_subsets); |
1351 | 704 | } |