/src/binutils-gdb/opcodes/riscv-dis.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* RISC-V disassembler |
2 | | Copyright (C) 2011-2025 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 | | /* The RISC-V disassembler produces styled output using |
36 | | disassemble_info::fprintf_styled_func. This define prevents use of |
37 | | disassemble_info::fprintf_func which is for unstyled output. */ |
38 | | #define fprintf_func please_use_fprintf_styled_func_instead |
39 | | |
40 | | /* The earliest privilege spec supported by disassembler. */ |
41 | 0 | #define PRIV_SPEC_EARLIEST PRIV_SPEC_CLASS_1P10 |
42 | | |
43 | | struct riscv_private_data |
44 | | { |
45 | | bfd_vma gp; |
46 | | bfd_vma print_addr; |
47 | | bfd_vma hi_addr[OP_MASK_RD + 1]; |
48 | | bool to_print_addr; |
49 | | bool has_gp; |
50 | | /* Current XLEN for the disassembler. */ |
51 | | unsigned xlen; |
52 | | /* Default ISA specification version. */ |
53 | | enum riscv_spec_class default_isa_spec; |
54 | | /* Default privileged specification. */ |
55 | | enum riscv_spec_class default_priv_spec; |
56 | | /* Used for architecture parser. */ |
57 | | riscv_parse_subset_t riscv_rps_dis; |
58 | | /* Default architecture string for the object file. It will be changed once |
59 | | elf architecture attribute exits. This is used for mapping symbol $x. */ |
60 | | const char* default_arch; |
61 | | /* Used for mapping symbols. */ |
62 | | int last_map_symbol; |
63 | | bfd_vma last_stop_offset; |
64 | | bfd_vma last_map_symbol_boundary; |
65 | | enum riscv_seg_mstate last_map_state; |
66 | | asection *last_map_section; |
67 | | /* Register names as used by the disassembler. */ |
68 | | const char (*riscv_gpr_names)[NRC]; |
69 | | const char (*riscv_fpr_names)[NRC]; |
70 | | /* If set, disassemble as most general instruction. */ |
71 | | bool no_aliases; |
72 | | /* If set, disassemble without checking architecture string, just like what |
73 | | we did at the beginning. */ |
74 | | bool all_ext; |
75 | | }; |
76 | | |
77 | | /* Set default RISC-V disassembler options. */ |
78 | | |
79 | | static void |
80 | | set_default_riscv_dis_options (struct disassemble_info *info) |
81 | 1.48k | { |
82 | 1.48k | struct riscv_private_data *pd = info->private_data; |
83 | 1.48k | pd->riscv_gpr_names = riscv_gpr_names_abi; |
84 | 1.48k | pd->riscv_fpr_names = riscv_fpr_names_abi; |
85 | 1.48k | pd->no_aliases = false; |
86 | 1.48k | pd->all_ext = false; |
87 | 1.48k | } |
88 | | |
89 | | /* Parse RISC-V disassembler option (without arguments). */ |
90 | | |
91 | | static bool |
92 | | parse_riscv_dis_option_without_args (const char *option, |
93 | | struct disassemble_info *info) |
94 | 0 | { |
95 | 0 | struct riscv_private_data *pd = info->private_data; |
96 | 0 | if (strcmp (option, "no-aliases") == 0) |
97 | 0 | pd->no_aliases = true; |
98 | 0 | else if (strcmp (option, "numeric") == 0) |
99 | 0 | { |
100 | 0 | pd->riscv_gpr_names = riscv_gpr_names_numeric; |
101 | 0 | pd->riscv_fpr_names = riscv_fpr_names_numeric; |
102 | 0 | } |
103 | 0 | else if (strcmp (option, "max") == 0) |
104 | 0 | pd->all_ext = true; |
105 | 0 | else |
106 | 0 | return false; |
107 | 0 | return true; |
108 | 0 | } |
109 | | |
110 | | /* Parse RISC-V disassembler option (possibly with arguments). */ |
111 | | |
112 | | static void |
113 | | parse_riscv_dis_option (const char *option, struct disassemble_info *info) |
114 | 0 | { |
115 | 0 | char *equal, *value; |
116 | |
|
117 | 0 | if (parse_riscv_dis_option_without_args (option, info)) |
118 | 0 | return; |
119 | | |
120 | 0 | equal = strchr (option, '='); |
121 | 0 | if (equal == NULL) |
122 | 0 | { |
123 | | /* The option without '=' should be defined above. */ |
124 | 0 | opcodes_error_handler (_("unrecognized disassembler option: %s"), option); |
125 | 0 | return; |
126 | 0 | } |
127 | 0 | if (equal == option |
128 | 0 | || *(equal + 1) == '\0') |
129 | 0 | { |
130 | | /* Invalid options with '=', no option name before '=', |
131 | | and no value after '='. */ |
132 | 0 | opcodes_error_handler (_("unrecognized disassembler option with '=': %s"), |
133 | 0 | option); |
134 | 0 | return; |
135 | 0 | } |
136 | | |
137 | 0 | *equal = '\0'; |
138 | 0 | value = equal + 1; |
139 | 0 | if (strcmp (option, "priv-spec") == 0) |
140 | 0 | { |
141 | 0 | struct riscv_private_data *pd = info->private_data; |
142 | 0 | enum riscv_spec_class priv_spec = PRIV_SPEC_CLASS_NONE; |
143 | 0 | const char *name = NULL; |
144 | |
|
145 | 0 | RISCV_GET_PRIV_SPEC_CLASS (value, priv_spec); |
146 | 0 | if (priv_spec < PRIV_SPEC_EARLIEST) |
147 | 0 | opcodes_error_handler (_("unknown privileged spec set by %s=%s"), |
148 | 0 | option, value); |
149 | 0 | else if (pd->default_priv_spec == PRIV_SPEC_CLASS_NONE) |
150 | 0 | pd->default_priv_spec = priv_spec; |
151 | 0 | else if (pd->default_priv_spec != priv_spec) |
152 | 0 | { |
153 | 0 | RISCV_GET_PRIV_SPEC_NAME (name, pd->default_priv_spec); |
154 | 0 | opcodes_error_handler (_("mis-matched privilege spec set by %s=%s, " |
155 | 0 | "the elf privilege attribute is %s"), |
156 | 0 | option, value, name); |
157 | 0 | } |
158 | 0 | } |
159 | 0 | else |
160 | 0 | { |
161 | | /* xgettext:c-format */ |
162 | 0 | opcodes_error_handler (_("unrecognized disassembler option: %s"), option); |
163 | 0 | } |
164 | 0 | } |
165 | | |
166 | | /* Parse RISC-V disassembler options. */ |
167 | | |
168 | | static void |
169 | | parse_riscv_dis_options (const char *opts_in, struct disassemble_info *info) |
170 | 0 | { |
171 | 0 | char *opts = xstrdup (opts_in), *opt = opts, *opt_end = opts; |
172 | |
|
173 | 0 | set_default_riscv_dis_options (info); |
174 | |
|
175 | 0 | for ( ; opt_end != NULL; opt = opt_end + 1) |
176 | 0 | { |
177 | 0 | if ((opt_end = strchr (opt, ',')) != NULL) |
178 | 0 | *opt_end = 0; |
179 | 0 | parse_riscv_dis_option (opt, info); |
180 | 0 | } |
181 | |
|
182 | 0 | free (opts); |
183 | 0 | } |
184 | | |
185 | | /* Print one argument from an array. */ |
186 | | |
187 | | static void |
188 | | arg_print (struct disassemble_info *info, unsigned long val, |
189 | | const char* const* array, size_t size) |
190 | 13.4k | { |
191 | 13.4k | const char *s = val >= size || array[val] == NULL ? "unknown" : array[val]; |
192 | 13.4k | (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s); |
193 | 13.4k | } |
194 | | |
195 | | /* If we need to print an address, set its value and state. */ |
196 | | |
197 | | static void |
198 | | maybe_print_address (struct riscv_private_data *pd, int base_reg, int offset, |
199 | | int wide) |
200 | 76.9k | { |
201 | 76.9k | if (pd->hi_addr[base_reg] != (bfd_vma)-1) |
202 | 10.4k | { |
203 | 10.4k | pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset; |
204 | 10.4k | pd->hi_addr[base_reg] = -1; |
205 | 10.4k | } |
206 | 66.4k | else if (base_reg == X_GP && pd->has_gp) |
207 | 0 | pd->print_addr = pd->gp + offset; |
208 | 66.4k | else if (base_reg == X_TP || base_reg == 0) |
209 | 7.51k | pd->print_addr = offset; |
210 | 58.9k | else |
211 | 58.9k | return; /* Don't print the address. */ |
212 | 17.9k | pd->to_print_addr = true; |
213 | | |
214 | | /* Sign-extend a 32-bit value to a 64-bit value. */ |
215 | 17.9k | if (wide) |
216 | 2.53k | pd->print_addr = (bfd_vma)(int32_t) pd->print_addr; |
217 | | |
218 | | /* Fit into a 32-bit value on RV32. */ |
219 | 17.9k | if (pd->xlen == 32) |
220 | 5.32k | pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr; |
221 | 17.9k | } |
222 | | |
223 | | /* Get Zcmp reg_list field. */ |
224 | | |
225 | | static void |
226 | | print_reg_list (disassemble_info *info, insn_t l) |
227 | 0 | { |
228 | 0 | struct riscv_private_data *pd = info->private_data; |
229 | 0 | bool numeric = pd->riscv_gpr_names == riscv_gpr_names_numeric; |
230 | 0 | unsigned reg_list = (int)EXTRACT_OPERAND (REG_LIST, l); |
231 | 0 | unsigned r_start = numeric ? X_S2 : X_S0; |
232 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
233 | 0 | "%s", pd->riscv_gpr_names[X_RA]); |
234 | |
|
235 | 0 | if (reg_list == 5) |
236 | 0 | { |
237 | 0 | info->fprintf_styled_func (info->stream, dis_style_text, ","); |
238 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
239 | 0 | "%s", pd->riscv_gpr_names[X_S0]); |
240 | 0 | } |
241 | 0 | else if (reg_list == 6 || (numeric && reg_list > 6)) |
242 | 0 | { |
243 | 0 | info->fprintf_styled_func (info->stream, dis_style_text, ","); |
244 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
245 | 0 | "%s", pd->riscv_gpr_names[X_S0]); |
246 | 0 | info->fprintf_styled_func (info->stream, dis_style_text, "-"); |
247 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
248 | 0 | "%s", pd->riscv_gpr_names[X_S1]); |
249 | 0 | } |
250 | |
|
251 | 0 | if (reg_list == 15) |
252 | 0 | { |
253 | 0 | info->fprintf_styled_func (info->stream, dis_style_text, ","); |
254 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
255 | 0 | "%s", pd->riscv_gpr_names[r_start]); |
256 | 0 | info->fprintf_styled_func (info->stream, dis_style_text, "-"); |
257 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
258 | 0 | "%s", pd->riscv_gpr_names[X_S11]); |
259 | 0 | } |
260 | 0 | else if (reg_list == 7 && numeric) |
261 | 0 | { |
262 | 0 | info->fprintf_styled_func (info->stream, dis_style_text, ","); |
263 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
264 | 0 | "%s", pd->riscv_gpr_names[X_S2]); |
265 | 0 | } |
266 | 0 | else if (reg_list > 6) |
267 | 0 | { |
268 | 0 | info->fprintf_styled_func (info->stream, dis_style_text, ","); |
269 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
270 | 0 | "%s", pd->riscv_gpr_names[r_start]); |
271 | 0 | info->fprintf_styled_func (info->stream, dis_style_text, "-"); |
272 | 0 | info->fprintf_styled_func (info->stream, dis_style_register, |
273 | 0 | "%s", pd->riscv_gpr_names[reg_list + 11]); |
274 | 0 | } |
275 | 0 | } |
276 | | |
277 | | /* Get Zcmp sp adjustment immediate. */ |
278 | | |
279 | | static int |
280 | | riscv_get_spimm (insn_t l, int xlen) |
281 | 0 | { |
282 | 0 | int spimm = riscv_get_sp_base(l, xlen); |
283 | 0 | spimm += EXTRACT_ZCMP_SPIMM (l); |
284 | 0 | if (((l ^ MATCH_CM_PUSH) & MASK_CM_PUSH) == 0) |
285 | 0 | spimm *= -1; |
286 | 0 | return spimm; |
287 | 0 | } |
288 | | |
289 | | /* Get s-register regno by using sreg number. |
290 | | e.g. the regno of s0 is 8, so |
291 | | riscv_zcmp_get_sregno (0) equals 8. */ |
292 | | |
293 | | static unsigned |
294 | | riscv_zcmp_get_sregno (unsigned sreg_idx) |
295 | 0 | { |
296 | 0 | return sreg_idx > 1 ? |
297 | 0 | sreg_idx + 16 : sreg_idx + 8; |
298 | 0 | } |
299 | | |
300 | | /* Print insn arguments for 32/64-bit code. */ |
301 | | |
302 | | static void |
303 | | print_insn_args (const char *oparg, insn_t l, bfd_vma pc, disassemble_info *info) |
304 | 1.09M | { |
305 | 1.09M | struct riscv_private_data *pd = info->private_data; |
306 | 1.09M | int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1; |
307 | 1.09M | int rd = (l >> OP_SH_RD) & OP_MASK_RD; |
308 | 1.09M | fprintf_styled_ftype print = info->fprintf_styled_func; |
309 | 1.09M | const char *opargStart; |
310 | | |
311 | 1.09M | if (*oparg != '\0') |
312 | 879k | print (info->stream, dis_style_text, "\t"); |
313 | | |
314 | 5.43M | for (; *oparg != '\0'; oparg++) |
315 | 4.33M | { |
316 | 4.33M | opargStart = oparg; |
317 | 4.33M | switch (*oparg) |
318 | 4.33M | { |
319 | 1.89M | case 'C': /* RVC */ |
320 | 1.89M | switch (*++oparg) |
321 | 1.89M | { |
322 | 291k | case 's': /* RS1 x8-x15. */ |
323 | 310k | case 'w': /* RS1 x8-x15. */ |
324 | 310k | print (info->stream, dis_style_register, "%s", |
325 | 310k | pd->riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]); |
326 | 310k | break; |
327 | 243k | case 't': /* RS2 x8-x15. */ |
328 | 243k | case 'x': /* RS2 x8-x15. */ |
329 | 243k | print (info->stream, dis_style_register, "%s", |
330 | 243k | pd->riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]); |
331 | 243k | break; |
332 | 108k | case 'U': /* RS1, constrained to equal RD. */ |
333 | 108k | print (info->stream, dis_style_register, |
334 | 108k | "%s", pd->riscv_gpr_names[rd]); |
335 | 108k | break; |
336 | 285k | case 'c': /* RS1, constrained to equal sp. */ |
337 | 285k | print (info->stream, dis_style_register, "%s", |
338 | 285k | pd->riscv_gpr_names[X_SP]); |
339 | 285k | break; |
340 | 59.3k | case 'V': /* RS2 */ |
341 | 59.3k | print (info->stream, dis_style_register, "%s", |
342 | 59.3k | pd->riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]); |
343 | 59.3k | break; |
344 | 51.7k | case 'o': |
345 | 103k | case 'j': |
346 | 103k | if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0) |
347 | 38.1k | maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0); |
348 | 103k | if (info->mach == bfd_mach_riscv64 |
349 | 103k | && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0) |
350 | 11.9k | maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1); |
351 | 103k | print (info->stream, dis_style_immediate, "%d", |
352 | 103k | (int)EXTRACT_CITYPE_IMM (l)); |
353 | 103k | break; |
354 | 81.5k | case 'k': |
355 | 81.5k | print (info->stream, dis_style_address_offset, "%d", |
356 | 81.5k | (int)EXTRACT_CLTYPE_LW_IMM (l)); |
357 | 81.5k | break; |
358 | 142k | case 'l': |
359 | 142k | print (info->stream, dis_style_address_offset, "%d", |
360 | 142k | (int)EXTRACT_CLTYPE_LD_IMM (l)); |
361 | 142k | break; |
362 | 30.9k | case 'm': |
363 | 30.9k | print (info->stream, dis_style_address_offset, "%d", |
364 | 30.9k | (int)EXTRACT_CITYPE_LWSP_IMM (l)); |
365 | 30.9k | break; |
366 | 52.2k | case 'n': |
367 | 52.2k | print (info->stream, dis_style_address_offset, "%d", |
368 | 52.2k | (int)EXTRACT_CITYPE_LDSP_IMM (l)); |
369 | 52.2k | break; |
370 | 116k | case 'K': |
371 | 116k | print (info->stream, dis_style_immediate, "%d", |
372 | 116k | (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l)); |
373 | 116k | break; |
374 | 11.3k | case 'L': |
375 | 11.3k | print (info->stream, dis_style_immediate, "%d", |
376 | 11.3k | (int)EXTRACT_CITYPE_ADDI16SP_IMM (l)); |
377 | 11.3k | break; |
378 | 27.9k | case 'M': |
379 | 27.9k | print (info->stream, dis_style_address_offset, "%d", |
380 | 27.9k | (int)EXTRACT_CSSTYPE_SWSP_IMM (l)); |
381 | 27.9k | break; |
382 | 35.0k | case 'N': |
383 | 35.0k | print (info->stream, dis_style_address_offset, "%d", |
384 | 35.0k | (int)EXTRACT_CSSTYPE_SDSP_IMM (l)); |
385 | 35.0k | break; |
386 | 46.3k | case 'p': |
387 | 46.3k | info->target = EXTRACT_CBTYPE_IMM (l) + pc; |
388 | 46.3k | (*info->print_address_func) (info->target, info); |
389 | 46.3k | break; |
390 | 27.3k | case 'a': |
391 | 27.3k | info->target = EXTRACT_CJTYPE_IMM (l) + pc; |
392 | 27.3k | (*info->print_address_func) (info->target, info); |
393 | 27.3k | break; |
394 | 30.7k | case 'u': |
395 | 30.7k | print (info->stream, dis_style_immediate, "0x%x", |
396 | 30.7k | (unsigned)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1))); |
397 | 30.7k | break; |
398 | 61.6k | case '>': |
399 | 61.6k | print (info->stream, dis_style_immediate, "0x%x", |
400 | 61.6k | (unsigned)EXTRACT_CITYPE_IMM (l) & 0x3f); |
401 | 61.6k | break; |
402 | 0 | case '<': |
403 | 0 | print (info->stream, dis_style_immediate, "0x%x", |
404 | 0 | (unsigned)EXTRACT_CITYPE_IMM (l) & 0x1f); |
405 | 0 | break; |
406 | 23.4k | case 'T': /* Floating-point RS2. */ |
407 | 23.4k | print (info->stream, dis_style_register, "%s", |
408 | 23.4k | pd->riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]); |
409 | 23.4k | break; |
410 | 100k | case 'D': /* Floating-point RS2 x8-x15. */ |
411 | 100k | print (info->stream, dis_style_register, "%s", |
412 | 100k | pd->riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]); |
413 | 100k | break; |
414 | 1.89M | } |
415 | 1.89M | break; |
416 | | |
417 | 1.89M | case 'V': /* RVV */ |
418 | 0 | switch (*++oparg) |
419 | 0 | { |
420 | 0 | case 'd': |
421 | 0 | case 'f': |
422 | 0 | print (info->stream, dis_style_register, "%s", |
423 | 0 | riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]); |
424 | 0 | break; |
425 | 0 | case 'e': |
426 | 0 | if (!EXTRACT_OPERAND (VWD, l)) |
427 | 0 | print (info->stream, dis_style_register, "%s", |
428 | 0 | pd->riscv_gpr_names[0]); |
429 | 0 | else |
430 | 0 | print (info->stream, dis_style_register, "%s", |
431 | 0 | riscv_vecr_names_numeric[EXTRACT_OPERAND (VD, l)]); |
432 | 0 | break; |
433 | 0 | case 's': |
434 | 0 | print (info->stream, dis_style_register, "%s", |
435 | 0 | riscv_vecr_names_numeric[EXTRACT_OPERAND (VS1, l)]); |
436 | 0 | break; |
437 | 0 | case 't': |
438 | 0 | case 'u': /* VS1 == VS2 already verified at this point. */ |
439 | 0 | case 'v': /* VD == VS1 == VS2 already verified at this point. */ |
440 | 0 | print (info->stream, dis_style_register, "%s", |
441 | 0 | riscv_vecr_names_numeric[EXTRACT_OPERAND (VS2, l)]); |
442 | 0 | break; |
443 | 0 | case '0': |
444 | 0 | print (info->stream, dis_style_register, "%s", |
445 | 0 | riscv_vecr_names_numeric[0]); |
446 | 0 | break; |
447 | 0 | case 'b': |
448 | 0 | case 'c': |
449 | 0 | { |
450 | 0 | int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l) |
451 | 0 | : EXTRACT_RVV_VC_IMM (l); |
452 | 0 | unsigned int imm_vlmul = EXTRACT_OPERAND (VLMUL, imm); |
453 | 0 | unsigned int imm_vsew = EXTRACT_OPERAND (VSEW, imm); |
454 | 0 | unsigned int imm_vta = EXTRACT_OPERAND (VTA, imm); |
455 | 0 | unsigned int imm_vma = EXTRACT_OPERAND (VMA, imm); |
456 | 0 | unsigned int imm_vtype_res = (imm >> 8); |
457 | |
|
458 | 0 | if (imm_vsew < ARRAY_SIZE (riscv_vsew) |
459 | 0 | && imm_vlmul < ARRAY_SIZE (riscv_vlmul) |
460 | 0 | && imm_vta < ARRAY_SIZE (riscv_vta) |
461 | 0 | && imm_vma < ARRAY_SIZE (riscv_vma) |
462 | 0 | && !imm_vtype_res |
463 | 0 | && riscv_vsew[imm_vsew] != NULL |
464 | 0 | && riscv_vlmul[imm_vlmul] != NULL) |
465 | 0 | print (info->stream, dis_style_text, "%s,%s,%s,%s", |
466 | 0 | riscv_vsew[imm_vsew], |
467 | 0 | riscv_vlmul[imm_vlmul], riscv_vta[imm_vta], |
468 | 0 | riscv_vma[imm_vma]); |
469 | 0 | else |
470 | 0 | print (info->stream, dis_style_immediate, "%d", imm); |
471 | 0 | } |
472 | 0 | break; |
473 | 0 | case 'i': |
474 | 0 | print (info->stream, dis_style_immediate, "%d", |
475 | 0 | (int)EXTRACT_RVV_VI_IMM (l)); |
476 | 0 | break; |
477 | 0 | case 'j': |
478 | 0 | print (info->stream, dis_style_immediate, "%d", |
479 | 0 | (int)EXTRACT_RVV_VI_UIMM (l)); |
480 | 0 | break; |
481 | 0 | case 'k': |
482 | 0 | print (info->stream, dis_style_immediate, "%d", |
483 | 0 | (int)EXTRACT_RVV_OFFSET (l)); |
484 | 0 | break; |
485 | 0 | case 'l': |
486 | 0 | print (info->stream, dis_style_immediate, "%d", |
487 | 0 | (int)EXTRACT_RVV_VI_UIMM6 (l)); |
488 | 0 | break; |
489 | 0 | case 'm': |
490 | 0 | if (!EXTRACT_OPERAND (VMASK, l)) |
491 | 0 | { |
492 | 0 | print (info->stream, dis_style_text, ","); |
493 | 0 | print (info->stream, dis_style_register, "%s", |
494 | 0 | riscv_vecm_names_numeric[0]); |
495 | 0 | } |
496 | 0 | break; |
497 | 0 | } |
498 | 0 | break; |
499 | | |
500 | 1.14M | case ',': |
501 | 1.53M | case '(': |
502 | 1.92M | case ')': |
503 | 1.92M | case '[': |
504 | 1.92M | case ']': |
505 | 1.92M | case '{': |
506 | 1.92M | case '}': |
507 | 1.92M | print (info->stream, dis_style_text, "%c", *oparg); |
508 | 1.92M | break; |
509 | | |
510 | 1.33k | case '0': |
511 | | /* Only print constant 0 if it is the last argument. */ |
512 | 1.33k | if (!oparg[1]) |
513 | 0 | print (info->stream, dis_style_immediate, "0"); |
514 | 1.33k | break; |
515 | | |
516 | 0 | case 'r': |
517 | 0 | print (info->stream, dis_style_register, "%s", |
518 | 0 | pd->riscv_gpr_names[EXTRACT_OPERAND (RS3, l)]); |
519 | 0 | break; |
520 | | |
521 | 38.4k | case 's': |
522 | 38.4k | if ((l & MASK_JALR) == MATCH_JALR) |
523 | 1.83k | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0); |
524 | 38.4k | print (info->stream, dis_style_register, "%s", |
525 | 38.4k | pd->riscv_gpr_names[rs1]); |
526 | 38.4k | break; |
527 | | |
528 | 11.9k | case 't': |
529 | 11.9k | print (info->stream, dis_style_register, "%s", |
530 | 11.9k | pd->riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]); |
531 | 11.9k | break; |
532 | | |
533 | 11.9k | case 'u': |
534 | 11.9k | print (info->stream, dis_style_immediate, "0x%x", |
535 | 11.9k | (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS); |
536 | 11.9k | break; |
537 | | |
538 | 12.1k | case 'm': |
539 | 12.1k | arg_print (info, EXTRACT_OPERAND (RM, l), |
540 | 12.1k | riscv_rm, ARRAY_SIZE (riscv_rm)); |
541 | 12.1k | break; |
542 | | |
543 | 651 | case 'P': |
544 | 651 | arg_print (info, EXTRACT_OPERAND (PRED, l), |
545 | 651 | riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ)); |
546 | 651 | break; |
547 | | |
548 | 651 | case 'Q': |
549 | 651 | arg_print (info, EXTRACT_OPERAND (SUCC, l), |
550 | 651 | riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ)); |
551 | 651 | break; |
552 | | |
553 | 15.0k | case 'o': |
554 | 15.0k | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0); |
555 | | /* Fall through. */ |
556 | 21.1k | case 'j': |
557 | 21.1k | if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0) |
558 | 21.1k | || (l & MASK_JALR) == MATCH_JALR) |
559 | 2.26k | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0); |
560 | 21.1k | if (info->mach == bfd_mach_riscv64 |
561 | 21.1k | && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0) |
562 | 1.45k | maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1); |
563 | 21.1k | print (info->stream, dis_style_immediate, "%d", |
564 | 21.1k | (int)EXTRACT_ITYPE_IMM (l)); |
565 | 21.1k | break; |
566 | | |
567 | 6.25k | case 'q': |
568 | 6.25k | maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0); |
569 | 6.25k | print (info->stream, dis_style_address_offset, "%d", |
570 | 6.25k | (int)EXTRACT_STYPE_IMM (l)); |
571 | 6.25k | break; |
572 | | |
573 | 8.04k | case 'a': |
574 | 8.04k | info->target = EXTRACT_JTYPE_IMM (l) + pc; |
575 | 8.04k | (*info->print_address_func) (info->target, info); |
576 | 8.04k | break; |
577 | | |
578 | 5.50k | case 'p': |
579 | 5.50k | info->target = EXTRACT_BTYPE_IMM (l) + pc; |
580 | 5.50k | (*info->print_address_func) (info->target, info); |
581 | 5.50k | break; |
582 | | |
583 | 289k | case 'd': |
584 | 289k | if ((l & MASK_AUIPC) == MATCH_AUIPC) |
585 | 5.39k | pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l); |
586 | 283k | else if ((l & MASK_LUI) == MATCH_LUI) |
587 | 6.56k | pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l); |
588 | 277k | else if ((l & MASK_C_LUI) == MATCH_C_LUI) |
589 | 30.7k | pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l); |
590 | 289k | print (info->stream, dis_style_register, "%s", |
591 | 289k | pd->riscv_gpr_names[rd]); |
592 | 289k | break; |
593 | | |
594 | 0 | case 'y': |
595 | 0 | print (info->stream, dis_style_immediate, "0x%x", |
596 | 0 | EXTRACT_OPERAND (BS, l)); |
597 | 0 | break; |
598 | | |
599 | 0 | case 'z': |
600 | 0 | print (info->stream, dis_style_register, "%s", |
601 | 0 | pd->riscv_gpr_names[0]); |
602 | 0 | break; |
603 | | |
604 | 237 | case '>': |
605 | 237 | print (info->stream, dis_style_immediate, "0x%x", |
606 | 237 | EXTRACT_OPERAND (SHAMT, l)); |
607 | 237 | break; |
608 | | |
609 | 134 | case '<': |
610 | 134 | print (info->stream, dis_style_immediate, "0x%x", |
611 | 134 | EXTRACT_OPERAND (SHAMTW, l)); |
612 | 134 | break; |
613 | | |
614 | 13.3k | case 'S': |
615 | 13.4k | case 'U': |
616 | 13.4k | print (info->stream, dis_style_register, "%s", |
617 | 13.4k | pd->riscv_fpr_names[rs1]); |
618 | 13.4k | break; |
619 | | |
620 | 15.3k | case 'T': |
621 | 15.3k | print (info->stream, dis_style_register, "%s", |
622 | 15.3k | pd->riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]); |
623 | 15.3k | break; |
624 | | |
625 | 52.9k | case 'D': |
626 | 52.9k | print (info->stream, dis_style_register, "%s", |
627 | 52.9k | pd->riscv_fpr_names[rd]); |
628 | 52.9k | break; |
629 | | |
630 | 12.4k | case 'R': |
631 | 12.4k | print (info->stream, dis_style_register, "%s", |
632 | 12.4k | pd->riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]); |
633 | 12.4k | break; |
634 | | |
635 | 7.42k | case 'E': |
636 | 7.42k | { |
637 | 7.42k | static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs. */ |
638 | 7.42k | static bool init_csr = false; |
639 | 7.42k | unsigned int csr = EXTRACT_OPERAND (CSR, l); |
640 | | |
641 | 7.42k | if (!init_csr) |
642 | 7.42k | { |
643 | 7.42k | unsigned int i; |
644 | 30.4M | for (i = 0; i < 4096; i++) |
645 | 30.4M | riscv_csr_hash[i] = NULL; |
646 | | |
647 | | /* Set to the newest privileged version. */ |
648 | 7.42k | if (pd->default_priv_spec == PRIV_SPEC_CLASS_NONE) |
649 | 649 | pd->default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1; |
650 | | |
651 | 7.42k | #define DECLARE_CSR(name, num, class, define_version, abort_version) \ |
652 | 3.45M | if (riscv_csr_hash[num] == NULL \ |
653 | 3.45M | && ((define_version == PRIV_SPEC_CLASS_NONE \ |
654 | 3.34M | && abort_version == PRIV_SPEC_CLASS_NONE) \ |
655 | 3.34M | || (pd->default_priv_spec >= define_version \ |
656 | 2.13M | && pd->default_priv_spec < abort_version))) \ |
657 | 3.45M | riscv_csr_hash[num] = #name; |
658 | 7.42k | #define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \ |
659 | 111k | DECLARE_CSR (name, num, class, define_version, abort_version) |
660 | 7.42k | #include "opcode/riscv-opc.h" |
661 | 7.42k | #undef DECLARE_CSR |
662 | 7.42k | } |
663 | | |
664 | 7.42k | if (riscv_csr_hash[csr] != NULL) |
665 | 1.58k | if (riscv_subset_supports (&pd->riscv_rps_dis, "xtheadvector") |
666 | 1.58k | && (csr == CSR_VSTART |
667 | 0 | || csr == CSR_VXSAT |
668 | 0 | || csr == CSR_VXRM |
669 | 0 | || csr == CSR_VL |
670 | 0 | || csr == CSR_VTYPE |
671 | 0 | || csr == CSR_VLENB)) |
672 | 0 | print (info->stream, dis_style_register, "%s", |
673 | 0 | concat ("th.", riscv_csr_hash[csr], NULL)); |
674 | 1.58k | else |
675 | 1.58k | print (info->stream, dis_style_register, "%s", |
676 | 1.58k | riscv_csr_hash[csr]); |
677 | 5.83k | else |
678 | 5.83k | print (info->stream, dis_style_immediate, "0x%x", csr); |
679 | 7.42k | break; |
680 | 13.3k | } |
681 | | |
682 | 0 | case 'Y': |
683 | 0 | print (info->stream, dis_style_immediate, "0x%x", |
684 | 0 | EXTRACT_OPERAND (RNUM, l)); |
685 | 0 | break; |
686 | | |
687 | 5.20k | case 'Z': |
688 | 5.20k | print (info->stream, dis_style_immediate, "%d", rs1); |
689 | 5.20k | break; |
690 | | |
691 | 0 | case 'W': /* Various operands for standard z extensions. */ |
692 | 0 | switch (*++oparg) |
693 | 0 | { |
694 | 0 | case 'i': |
695 | 0 | switch (*++oparg) |
696 | 0 | { |
697 | 0 | case 'f': |
698 | 0 | print (info->stream, dis_style_address_offset, "%d", |
699 | 0 | (int) EXTRACT_STYPE_IMM (l)); |
700 | 0 | break; |
701 | 0 | default: |
702 | 0 | goto undefined_modifier; |
703 | 0 | } |
704 | 0 | break; |
705 | 0 | case 'f': |
706 | 0 | switch (*++oparg) |
707 | 0 | { |
708 | 0 | case 'v': |
709 | 0 | if (riscv_fli_symval[rs1]) |
710 | 0 | print (info->stream, dis_style_text, "%s", |
711 | 0 | riscv_fli_symval[rs1]); |
712 | 0 | else |
713 | 0 | print (info->stream, dis_style_immediate, "%a", |
714 | 0 | riscv_fli_numval[rs1]); |
715 | 0 | break; |
716 | 0 | default: |
717 | 0 | goto undefined_modifier; |
718 | 0 | } |
719 | 0 | break; |
720 | 0 | case 'c': /* Zcb extension 16 bits length instruction fields. */ |
721 | 0 | switch (*++oparg) |
722 | 0 | { |
723 | 0 | case '1': |
724 | 0 | print (info->stream, dis_style_register, "%s", |
725 | 0 | pd->riscv_gpr_names[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG1, l))]); |
726 | 0 | break; |
727 | 0 | case '2': |
728 | 0 | print (info->stream, dis_style_register, "%s", |
729 | 0 | pd->riscv_gpr_names[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG2, l))]); |
730 | 0 | break; |
731 | 0 | case 'b': |
732 | 0 | print (info->stream, dis_style_immediate, "%d", |
733 | 0 | (int)EXTRACT_ZCB_BYTE_UIMM (l)); |
734 | 0 | break; |
735 | 0 | case 'h': |
736 | 0 | print (info->stream, dis_style_immediate, "%d", |
737 | 0 | (int)EXTRACT_ZCB_HALFWORD_UIMM (l)); |
738 | 0 | break; |
739 | 0 | case 'r': |
740 | 0 | print_reg_list (info, l); |
741 | 0 | break; |
742 | 0 | case 'p': |
743 | 0 | print (info->stream, dis_style_immediate, "%d", |
744 | 0 | riscv_get_spimm (l, pd->xlen)); |
745 | 0 | break; |
746 | 0 | case 'i': |
747 | 0 | case 'I': |
748 | 0 | print (info->stream, dis_style_address_offset, |
749 | 0 | "%" PRIu64, EXTRACT_ZCMT_INDEX (l)); |
750 | 0 | break; |
751 | 0 | default: |
752 | 0 | goto undefined_modifier; |
753 | 0 | } |
754 | 0 | break; |
755 | 0 | default: |
756 | 0 | goto undefined_modifier; |
757 | 0 | } |
758 | 0 | break; |
759 | | |
760 | 0 | case 'X': /* Vendor-specific operands. */ |
761 | 0 | switch (*++oparg) |
762 | 0 | { |
763 | 0 | case 't': /* Vendor-specific (T-head) operands. */ |
764 | 0 | { |
765 | 0 | size_t n; |
766 | 0 | size_t s; |
767 | 0 | bool sign; |
768 | 0 | switch (*++oparg) |
769 | 0 | { |
770 | 0 | case 'V': |
771 | 0 | ++oparg; |
772 | 0 | if (*oparg != 'c') |
773 | 0 | goto undefined_modifier; |
774 | | |
775 | 0 | int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l) |
776 | 0 | : EXTRACT_RVV_VC_IMM (l); |
777 | 0 | unsigned int imm_vediv = EXTRACT_OPERAND (XTHEADVEDIV, imm); |
778 | 0 | unsigned int imm_vlmul = EXTRACT_OPERAND (XTHEADVLMUL, imm); |
779 | 0 | unsigned int imm_vsew = EXTRACT_OPERAND (XTHEADVSEW, imm); |
780 | 0 | unsigned int imm_vtype_res |
781 | 0 | = EXTRACT_OPERAND (XTHEADVTYPE_RES, imm); |
782 | 0 | if (imm_vsew < ARRAY_SIZE (riscv_vsew) |
783 | 0 | && imm_vlmul < ARRAY_SIZE (riscv_th_vlen) |
784 | 0 | && imm_vediv < ARRAY_SIZE (riscv_th_vediv) |
785 | 0 | && ! imm_vtype_res) |
786 | 0 | print (info->stream, dis_style_text, "%s,%s,%s", |
787 | 0 | riscv_vsew[imm_vsew], riscv_th_vlen[imm_vlmul], |
788 | 0 | riscv_th_vediv[imm_vediv]); |
789 | 0 | else |
790 | 0 | print (info->stream, dis_style_immediate, "%d", imm); |
791 | 0 | break; |
792 | 0 | case 'l': /* Integer immediate, literal. */ |
793 | 0 | oparg++; |
794 | 0 | while (*oparg && *oparg != ',') |
795 | 0 | { |
796 | 0 | print (info->stream, dis_style_immediate, "%c", *oparg); |
797 | 0 | oparg++; |
798 | 0 | } |
799 | 0 | oparg--; |
800 | 0 | break; |
801 | 0 | case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S. */ |
802 | 0 | sign = true; |
803 | 0 | goto print_imm; |
804 | 0 | case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S. */ |
805 | 0 | sign = false; |
806 | 0 | goto print_imm; |
807 | 0 | print_imm: |
808 | 0 | n = strtol (oparg + 1, (char **)&oparg, 10); |
809 | 0 | if (*oparg != '@') |
810 | 0 | goto undefined_modifier; |
811 | 0 | s = strtol (oparg + 1, (char **)&oparg, 10); |
812 | 0 | oparg--; |
813 | |
|
814 | 0 | if (!sign) |
815 | 0 | print (info->stream, dis_style_immediate, "%lu", |
816 | 0 | (unsigned long)EXTRACT_U_IMM (n, s, l)); |
817 | 0 | else |
818 | 0 | print (info->stream, dis_style_immediate, "%li", |
819 | 0 | (signed long)EXTRACT_S_IMM (n, s, l)); |
820 | 0 | break; |
821 | 0 | default: |
822 | 0 | goto undefined_modifier; |
823 | 0 | } |
824 | 0 | } |
825 | 0 | break; |
826 | 0 | case 'c': /* Vendor-specific (CORE-V) operands. */ |
827 | 0 | switch (*++oparg) |
828 | 0 | { |
829 | 0 | case '2': |
830 | 0 | print (info->stream, dis_style_immediate, "%d", |
831 | 0 | ((int) EXTRACT_CV_IS2_UIMM5 (l))); |
832 | 0 | break; |
833 | 0 | case '3': |
834 | 0 | print (info->stream, dis_style_immediate, "%d", |
835 | 0 | ((int) EXTRACT_CV_IS3_UIMM5 (l))); |
836 | 0 | break; |
837 | 0 | case '4': |
838 | 0 | print (info->stream, dis_style_immediate, "%d", |
839 | 0 | ((int) EXTRACT_CV_BI_IMM5 (l))); |
840 | 0 | break; |
841 | 0 | case '5': |
842 | 0 | print (info->stream, dis_style_immediate, "%d", |
843 | 0 | ((int) EXTRACT_CV_SIMD_IMM6 (l))); |
844 | 0 | break; |
845 | 0 | case '6': |
846 | 0 | print (info->stream, dis_style_immediate, "%d", |
847 | 0 | ((int) EXTRACT_CV_BITMANIP_UIMM5 (l))); |
848 | 0 | break; |
849 | 0 | case '7': |
850 | 0 | print (info->stream, dis_style_immediate, "%d", |
851 | 0 | ((int) EXTRACT_CV_BITMANIP_UIMM2 (l))); |
852 | 0 | break; |
853 | 0 | case '8': |
854 | 0 | print (info->stream, dis_style_immediate, "%d", |
855 | 0 | ((int) EXTRACT_CV_SIMD_UIMM6 (l))); |
856 | 0 | ++oparg; |
857 | 0 | break; |
858 | 0 | default: |
859 | 0 | goto undefined_modifier; |
860 | 0 | } |
861 | 0 | break; |
862 | 0 | case 's': /* Vendor-specific (SiFive) operands. */ |
863 | 0 | switch (*++oparg) |
864 | 0 | { |
865 | | /* SiFive vector coprocessor interface. */ |
866 | 0 | case 'd': |
867 | 0 | print (info->stream, dis_style_register, "0x%x", |
868 | 0 | (unsigned) EXTRACT_OPERAND (RD, l)); |
869 | 0 | break; |
870 | 0 | case 't': |
871 | 0 | print (info->stream, dis_style_register, "0x%x", |
872 | 0 | (unsigned) EXTRACT_OPERAND (RS2, l)); |
873 | 0 | break; |
874 | 0 | case 'O': |
875 | 0 | switch (*++oparg) |
876 | 0 | { |
877 | 0 | case '2': |
878 | 0 | print (info->stream, dis_style_register, "0x%x", |
879 | 0 | (unsigned) EXTRACT_OPERAND (XSO2, l)); |
880 | 0 | break; |
881 | 0 | case '1': |
882 | 0 | print (info->stream, dis_style_register, "0x%x", |
883 | 0 | (unsigned) EXTRACT_OPERAND (XSO1, l)); |
884 | 0 | break; |
885 | 0 | } |
886 | 0 | break; |
887 | 0 | } |
888 | 0 | break; |
889 | 0 | case 'm': /* Vendor-specific (MIPS) operands. */ |
890 | 0 | switch (*++oparg) |
891 | 0 | { |
892 | 0 | case '@': |
893 | 0 | print (info->stream, dis_style_register, "0x%x", |
894 | 0 | (unsigned) EXTRACT_OPERAND (MIPS_HINT, l)); |
895 | 0 | break; |
896 | 0 | case '#': |
897 | 0 | print (info->stream, dis_style_register, "0x%x", |
898 | 0 | (unsigned) EXTRACT_OPERAND (MIPS_IMM9, l)); |
899 | 0 | break; |
900 | 0 | case '$': |
901 | 0 | print (info->stream, dis_style_immediate, "%d", |
902 | 0 | (unsigned)EXTRACT_MIPS_LDP_IMM (l)); |
903 | 0 | break; |
904 | 0 | case '%': |
905 | 0 | print (info->stream, dis_style_immediate, "%d", |
906 | 0 | (unsigned)EXTRACT_MIPS_LWP_IMM (l)); |
907 | 0 | break; |
908 | 0 | case '^': |
909 | 0 | print (info->stream, dis_style_immediate, "%d", |
910 | 0 | (unsigned)EXTRACT_MIPS_SDP_IMM (l)); |
911 | 0 | break; |
912 | 0 | case '&': |
913 | 0 | print (info->stream, dis_style_immediate, "%d", |
914 | 0 | (unsigned)EXTRACT_MIPS_SWP_IMM (l)); |
915 | 0 | break; |
916 | 0 | default: |
917 | 0 | goto undefined_modifier; |
918 | 0 | } |
919 | 0 | break; |
920 | 0 | default: |
921 | 0 | goto undefined_modifier; |
922 | 0 | } |
923 | 0 | break; |
924 | | |
925 | 0 | default: |
926 | 0 | undefined_modifier: |
927 | | /* xgettext:c-format */ |
928 | 0 | print (info->stream, dis_style_text, |
929 | 0 | _("# internal error, undefined modifier (%c)"), |
930 | 0 | *opargStart); |
931 | 0 | return; |
932 | 4.33M | } |
933 | 4.33M | } |
934 | 1.09M | } |
935 | | |
936 | | /* Print the RISC-V instruction at address MEMADDR in debugged memory, |
937 | | on using INFO. Returns length of the instruction, in bytes. |
938 | | BIGENDIAN must be 1 if this is big-endian code, 0 if |
939 | | this is little-endian code. */ |
940 | | |
941 | | static int |
942 | | riscv_disassemble_insn (bfd_vma memaddr, |
943 | | insn_t word, |
944 | | const bfd_byte *packet, |
945 | | disassemble_info *info) |
946 | 1.37M | { |
947 | 1.37M | const struct riscv_opcode *op; |
948 | 1.37M | static bool init = false; |
949 | 1.37M | static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1]; |
950 | 1.37M | struct riscv_private_data *pd = info->private_data; |
951 | 1.37M | int insnlen, i; |
952 | 1.37M | bool printed; |
953 | | |
954 | 1.38M | #define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP)) |
955 | | |
956 | | /* Build a hash table to shorten the search time. */ |
957 | 1.37M | if (! init) |
958 | 2 | { |
959 | 5.75k | for (op = riscv_opcodes; op->name; op++) |
960 | 5.75k | if (!riscv_hash[OP_HASH_IDX (op->match)]) |
961 | 60 | riscv_hash[OP_HASH_IDX (op->match)] = op; |
962 | | |
963 | 2 | init = true; |
964 | 2 | } |
965 | | |
966 | 1.37M | insnlen = riscv_insn_length (word); |
967 | | |
968 | | /* RISC-V instructions are always little-endian. */ |
969 | 1.37M | info->endian_code = BFD_ENDIAN_LITTLE; |
970 | | |
971 | 1.37M | info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2; |
972 | 1.37M | info->bytes_per_line = 8; |
973 | | /* We don't support constant pools, so this must be code. */ |
974 | 1.37M | info->display_endian = info->endian_code; |
975 | 1.37M | info->insn_info_valid = 1; |
976 | 1.37M | info->branch_delay_insns = 0; |
977 | 1.37M | info->data_size = 0; |
978 | 1.37M | info->insn_type = dis_nonbranch; |
979 | 1.37M | info->target = 0; |
980 | 1.37M | info->target2 = 0; |
981 | | |
982 | 1.37M | op = riscv_hash[OP_HASH_IDX (word)]; |
983 | 1.37M | if (op != NULL) |
984 | 1.33M | { |
985 | | /* If XLEN is not known, get its value from the ELF class. */ |
986 | 1.33M | if (info->mach == bfd_mach_riscv64) |
987 | 547k | pd->xlen = 64; |
988 | 786k | else if (info->mach == bfd_mach_riscv32) |
989 | 360k | pd->xlen = 32; |
990 | 425k | else if (info->section != NULL) |
991 | 0 | { |
992 | 0 | Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner); |
993 | 0 | pd->xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32; |
994 | 0 | } |
995 | | |
996 | | /* If arch has the Zfinx extension, replace FPR with GPR. */ |
997 | 1.33M | if (riscv_subset_supports (&pd->riscv_rps_dis, "zfinx")) |
998 | 0 | pd->riscv_fpr_names = pd->riscv_gpr_names; |
999 | 1.33M | else |
1000 | 1.33M | pd->riscv_fpr_names = pd->riscv_gpr_names == riscv_gpr_names_abi ? |
1001 | 1.33M | riscv_fpr_names_abi : riscv_fpr_names_numeric; |
1002 | | |
1003 | 782M | for (; op->name; op++) |
1004 | 781M | { |
1005 | | /* Ignore macro insns. */ |
1006 | 781M | if (op->pinfo == INSN_MACRO) |
1007 | 19.9M | continue; |
1008 | | /* Does the opcode match? */ |
1009 | 761M | if (! (op->match_func) (op, word)) |
1010 | 760M | continue; |
1011 | | /* Is this a pseudo-instruction and may we print it as such? */ |
1012 | 1.29M | if (pd->no_aliases && (op->pinfo & INSN_ALIAS)) |
1013 | 0 | continue; |
1014 | | /* Is this instruction restricted to a certain value of XLEN? */ |
1015 | 1.29M | if ((op->xlen_requirement != 0) |
1016 | 1.29M | && (op->xlen_requirement != pd->xlen)) |
1017 | 69.7k | continue; |
1018 | | /* Is this instruction supported by the current architecture? */ |
1019 | 1.23M | if (!pd->all_ext |
1020 | 1.23M | && !riscv_multi_subset_supports (&pd->riscv_rps_dis, |
1021 | 1.23M | op->insn_class)) |
1022 | 131k | continue; |
1023 | | |
1024 | | /* It's a match. */ |
1025 | 1.09M | (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic, |
1026 | 1.09M | "%s", op->name); |
1027 | 1.09M | print_insn_args (op->args, word, memaddr, info); |
1028 | | |
1029 | | /* Try to disassemble multi-instruction addressing sequences. */ |
1030 | 1.09M | if (pd->to_print_addr) |
1031 | 16.9k | { |
1032 | 16.9k | info->target = pd->print_addr; |
1033 | 16.9k | (*info->fprintf_styled_func) |
1034 | 16.9k | (info->stream, dis_style_comment_start, " # "); |
1035 | 16.9k | (*info->print_address_func) (info->target, info); |
1036 | 16.9k | pd->to_print_addr = false; |
1037 | 16.9k | } |
1038 | | |
1039 | | /* Finish filling out insn_info fields. */ |
1040 | 1.09M | switch (op->pinfo & INSN_TYPE) |
1041 | 1.09M | { |
1042 | 21.5k | case INSN_BRANCH: |
1043 | 21.5k | info->insn_type = dis_branch; |
1044 | 21.5k | break; |
1045 | 51.8k | case INSN_CONDBRANCH: |
1046 | 51.8k | info->insn_type = dis_condbranch; |
1047 | 51.8k | break; |
1048 | 16.3k | case INSN_JSR: |
1049 | 16.3k | info->insn_type = dis_jsr; |
1050 | 16.3k | break; |
1051 | 391k | case INSN_DREF: |
1052 | 391k | info->insn_type = dis_dref; |
1053 | 391k | break; |
1054 | 617k | default: |
1055 | 617k | break; |
1056 | 1.09M | } |
1057 | | |
1058 | 1.09M | if (op->pinfo & INSN_DATA_SIZE) |
1059 | 391k | { |
1060 | 391k | int size = ((op->pinfo & INSN_DATA_SIZE) |
1061 | 391k | >> INSN_DATA_SIZE_SHIFT); |
1062 | 391k | info->data_size = 1 << (size - 1); |
1063 | 391k | } |
1064 | | |
1065 | 1.09M | return insnlen; |
1066 | 1.09M | } |
1067 | 1.33M | } |
1068 | | |
1069 | | /* We did not find a match, so just print the instruction bits in |
1070 | | the shape of an assembler .insn directive. */ |
1071 | 277k | info->insn_type = dis_noninsn; |
1072 | 277k | (*info->fprintf_styled_func) |
1073 | 277k | (info->stream, dis_style_assembler_directive, ".insn"); |
1074 | 277k | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1075 | 277k | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, |
1076 | 277k | "%d", insnlen); |
1077 | 277k | (*info->fprintf_styled_func) (info->stream, dis_style_text, ", "); |
1078 | 277k | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x"); |
1079 | 841k | for (i = insnlen, printed = false; i >= 2; ) |
1080 | 563k | { |
1081 | 563k | i -= 2; |
1082 | 563k | word = bfd_get_bits (packet + i, 16, false); |
1083 | 563k | if (!word && !printed && i) |
1084 | 16.8k | continue; |
1085 | | |
1086 | 547k | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, |
1087 | 547k | "%04x", (unsigned int) word); |
1088 | 547k | printed = true; |
1089 | 547k | } |
1090 | | |
1091 | 277k | return insnlen; |
1092 | 1.37M | } |
1093 | | |
1094 | | /* Decide if we need to parse the architecture string again, also record the |
1095 | | string into the current subset list. */ |
1096 | | |
1097 | | static void |
1098 | | riscv_dis_parse_subset (struct disassemble_info *info, const char *arch_new) |
1099 | 1.48k | { |
1100 | 1.48k | struct riscv_private_data *pd = info->private_data; |
1101 | 1.48k | const char *arch_subset_list = pd->riscv_rps_dis.subset_list->arch_str; |
1102 | 1.48k | if (arch_subset_list == NULL || strcmp (arch_subset_list, arch_new) != 0) |
1103 | 1.48k | { |
1104 | 1.48k | riscv_release_subset_list (pd->riscv_rps_dis.subset_list); |
1105 | 1.48k | riscv_parse_subset (&pd->riscv_rps_dis, arch_new); |
1106 | 1.48k | riscv_arch_str (pd->xlen, pd->riscv_rps_dis.subset_list, |
1107 | 1.48k | true/* update */); |
1108 | 1.48k | } |
1109 | 1.48k | } |
1110 | | |
1111 | | /* If we find the suitable mapping symbol update the STATE. |
1112 | | Otherwise, do nothing. */ |
1113 | | |
1114 | | static void |
1115 | | riscv_update_map_state (int n, |
1116 | | enum riscv_seg_mstate *state, |
1117 | | struct disassemble_info *info) |
1118 | 0 | { |
1119 | 0 | struct riscv_private_data *pd = info->private_data; |
1120 | 0 | const char *name; |
1121 | | |
1122 | | /* If the symbol is in a different section, ignore it. */ |
1123 | 0 | if (info->section != NULL |
1124 | 0 | && info->section != info->symtab[n]->section) |
1125 | 0 | return; |
1126 | | |
1127 | 0 | name = bfd_asymbol_name(info->symtab[n]); |
1128 | 0 | if (strcmp (name, "$d") == 0) |
1129 | 0 | *state = MAP_DATA; |
1130 | 0 | else if (strcmp (name, "$x") == 0) |
1131 | 0 | { |
1132 | 0 | *state = MAP_INSN; |
1133 | 0 | riscv_dis_parse_subset (info, pd->default_arch); |
1134 | 0 | } |
1135 | 0 | else if (strncmp (name, "$xrv", 4) == 0) |
1136 | 0 | { |
1137 | 0 | *state = MAP_INSN; |
1138 | | |
1139 | | /* ISA mapping string may be numbered, suffixed with '.n'. Do not |
1140 | | consider this as part of the ISA string. */ |
1141 | 0 | char *suffix = strchr (name, '.'); |
1142 | 0 | if (suffix) |
1143 | 0 | { |
1144 | 0 | int suffix_index = (int)(suffix - name); |
1145 | 0 | char *name_substr = xmalloc (suffix_index + 1); |
1146 | 0 | strncpy (name_substr, name, suffix_index); |
1147 | 0 | name_substr[suffix_index] = '\0'; |
1148 | 0 | riscv_dis_parse_subset (info, name_substr + 2); |
1149 | 0 | free (name_substr); |
1150 | 0 | } |
1151 | 0 | else |
1152 | 0 | riscv_dis_parse_subset (info, name + 2); |
1153 | 0 | } |
1154 | 0 | } |
1155 | | |
1156 | | /* Return true if we find the suitable mapping symbol. |
1157 | | Otherwise, return false. */ |
1158 | | |
1159 | | static bool |
1160 | | riscv_is_valid_mapping_symbol (int n, |
1161 | | struct disassemble_info *info) |
1162 | 0 | { |
1163 | 0 | const char *name; |
1164 | | |
1165 | | /* If the symbol is in a different section, ignore it. */ |
1166 | 0 | if (info->section != NULL |
1167 | 0 | && info->section != info->symtab[n]->section) |
1168 | 0 | return false; |
1169 | | |
1170 | 0 | name = bfd_asymbol_name(info->symtab[n]); |
1171 | 0 | return riscv_elf_is_mapping_symbols (name); |
1172 | 0 | } |
1173 | | |
1174 | | /* Check the sorted symbol table (sorted by the symbol value), find the |
1175 | | suitable mapping symbols. */ |
1176 | | |
1177 | | static enum riscv_seg_mstate |
1178 | | riscv_search_mapping_symbol (bfd_vma memaddr, |
1179 | | struct disassemble_info *info) |
1180 | 1.37M | { |
1181 | 1.37M | struct riscv_private_data *pd = info->private_data; |
1182 | 1.37M | enum riscv_seg_mstate mstate; |
1183 | 1.37M | bool from_last_map_symbol; |
1184 | 1.37M | bool found = false; |
1185 | 1.37M | int symbol = -1; |
1186 | 1.37M | int n; |
1187 | | |
1188 | | /* Return the last map state if the address is still within the range of the |
1189 | | last mapping symbol. */ |
1190 | 1.37M | if (pd->last_map_section == info->section |
1191 | 1.37M | && (memaddr < pd->last_map_symbol_boundary)) |
1192 | 0 | return pd->last_map_state; |
1193 | | |
1194 | 1.37M | pd->last_map_section = info->section; |
1195 | | |
1196 | | /* Decide whether to print the data or instruction by default, in case |
1197 | | we can not find the corresponding mapping symbols. */ |
1198 | 1.37M | mstate = MAP_DATA; |
1199 | 1.37M | if ((info->section |
1200 | 1.37M | && info->section->flags & SEC_CODE) |
1201 | 1.37M | || !info->section) |
1202 | 704k | mstate = MAP_INSN; |
1203 | | |
1204 | 1.37M | if (info->symtab_size == 0 |
1205 | 1.37M | || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour) |
1206 | 1.37M | return mstate; |
1207 | | |
1208 | | /* Reset the last_map_symbol if we start to dump a new section. */ |
1209 | 0 | if (memaddr <= 0) |
1210 | 0 | pd->last_map_symbol = -1; |
1211 | | |
1212 | | /* If the last stop offset is different from the current one, then |
1213 | | don't use the last_map_symbol to search. We usually reset the |
1214 | | info->stop_offset when handling a new section. */ |
1215 | 0 | from_last_map_symbol = (pd->last_map_symbol >= 0 |
1216 | 0 | && info->stop_offset == pd->last_stop_offset); |
1217 | | |
1218 | | /* Start scanning from wherever we finished last time, or the start |
1219 | | of the function. */ |
1220 | 0 | n = from_last_map_symbol ? pd->last_map_symbol : info->symtab_pos + 1; |
1221 | | |
1222 | | /* Find the suitable mapping symbol to dump. */ |
1223 | 0 | for (; n < info->symtab_size; n++) |
1224 | 0 | { |
1225 | 0 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); |
1226 | | /* We have searched all possible symbols in the range. */ |
1227 | 0 | if (addr > memaddr) |
1228 | 0 | break; |
1229 | 0 | if (riscv_is_valid_mapping_symbol (n, info)) |
1230 | 0 | { |
1231 | 0 | symbol = n; |
1232 | 0 | found = true; |
1233 | | /* Do not stop searching, in case there are some mapping |
1234 | | symbols have the same value, but have different names. |
1235 | | Use the last one. */ |
1236 | 0 | } |
1237 | 0 | } |
1238 | | |
1239 | | /* We can not find the suitable mapping symbol above. Therefore, we |
1240 | | look forwards and try to find it again, but don't go past the start |
1241 | | of the section. Otherwise a data section without mapping symbols |
1242 | | can pick up a text mapping symbol of a preceeding section. */ |
1243 | 0 | if (!found) |
1244 | 0 | { |
1245 | 0 | n = from_last_map_symbol ? pd->last_map_symbol : info->symtab_pos; |
1246 | |
|
1247 | 0 | for (; n >= 0; n--) |
1248 | 0 | { |
1249 | 0 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); |
1250 | | /* We have searched all possible symbols in the range. */ |
1251 | 0 | if (addr < (info->section ? info->section->vma : 0)) |
1252 | 0 | break; |
1253 | | /* Stop searching once we find the closed mapping symbol. */ |
1254 | 0 | if (riscv_is_valid_mapping_symbol (n, info)) |
1255 | 0 | { |
1256 | 0 | symbol = n; |
1257 | 0 | found = true; |
1258 | 0 | break; |
1259 | 0 | } |
1260 | 0 | } |
1261 | 0 | } |
1262 | |
|
1263 | 0 | if (found) |
1264 | 0 | { |
1265 | 0 | riscv_update_map_state (symbol, &mstate, info); |
1266 | | |
1267 | | /* Find the next mapping symbol to determine the boundary of this mapping |
1268 | | symbol. */ |
1269 | |
|
1270 | 0 | bool found_next = false; |
1271 | | /* Try to found next mapping symbol. */ |
1272 | 0 | for (n = symbol + 1; n < info->symtab_size; n++) |
1273 | 0 | { |
1274 | 0 | if (info->symtab[symbol]->section != info->symtab[n]->section) |
1275 | 0 | continue; |
1276 | | |
1277 | 0 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); |
1278 | 0 | const char *sym_name = bfd_asymbol_name(info->symtab[n]); |
1279 | 0 | if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd')) |
1280 | 0 | { |
1281 | | /* The next mapping symbol has been found, and it represents the |
1282 | | boundary of this mapping symbol. */ |
1283 | 0 | found_next = true; |
1284 | 0 | pd->last_map_symbol_boundary = addr; |
1285 | 0 | break; |
1286 | 0 | } |
1287 | 0 | } |
1288 | | |
1289 | | /* No further mapping symbol has been found, indicating that the boundary |
1290 | | of the current mapping symbol is the end of this section. */ |
1291 | 0 | if (!found_next) |
1292 | 0 | pd->last_map_symbol_boundary = info->section->vma |
1293 | 0 | + info->section->size; |
1294 | 0 | } |
1295 | | |
1296 | | /* Save the information for next use. */ |
1297 | 0 | pd->last_map_symbol = symbol; |
1298 | 0 | pd->last_stop_offset = info->stop_offset; |
1299 | |
|
1300 | 0 | return mstate; |
1301 | 1.37M | } |
1302 | | |
1303 | | /* Decide which data size we should print. */ |
1304 | | |
1305 | | static bfd_vma |
1306 | | riscv_data_length (bfd_vma memaddr, |
1307 | | disassemble_info *info) |
1308 | 0 | { |
1309 | 0 | struct riscv_private_data *pd = info->private_data; |
1310 | 0 | bfd_vma length; |
1311 | 0 | bool found = false; |
1312 | |
|
1313 | 0 | length = 4; |
1314 | 0 | if (info->symtab_size != 0 |
1315 | 0 | && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour |
1316 | 0 | && pd->last_map_symbol >= 0) |
1317 | 0 | { |
1318 | 0 | int n; |
1319 | 0 | enum riscv_seg_mstate m = MAP_NONE; |
1320 | 0 | for (n = pd->last_map_symbol + 1; n < info->symtab_size; n++) |
1321 | 0 | { |
1322 | 0 | bfd_vma addr = bfd_asymbol_value (info->symtab[n]); |
1323 | 0 | if (addr > memaddr |
1324 | 0 | && riscv_is_valid_mapping_symbol (n, info)) |
1325 | 0 | { |
1326 | 0 | if (addr - memaddr < length) |
1327 | 0 | length = addr - memaddr; |
1328 | 0 | found = true; |
1329 | 0 | riscv_update_map_state (n, &m, info); |
1330 | 0 | break; |
1331 | 0 | } |
1332 | 0 | } |
1333 | 0 | } |
1334 | 0 | if (!found) |
1335 | 0 | { |
1336 | | /* Do not set the length which exceeds the section size. */ |
1337 | 0 | bfd_vma offset = info->section->vma + info->section->size; |
1338 | 0 | offset -= memaddr; |
1339 | 0 | length = (offset < length) ? offset : length; |
1340 | 0 | } |
1341 | 0 | length = length == 3 ? 2 : length; |
1342 | 0 | return length; |
1343 | 0 | } |
1344 | | |
1345 | | /* Dump the data contents. */ |
1346 | | |
1347 | | static int |
1348 | | riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED, |
1349 | | insn_t data, |
1350 | | const bfd_byte *packet ATTRIBUTE_UNUSED, |
1351 | | disassemble_info *info) |
1352 | 847 | { |
1353 | 847 | info->display_endian = info->endian; |
1354 | 847 | int i; |
1355 | | |
1356 | 847 | switch (info->bytes_per_chunk) |
1357 | 847 | { |
1358 | 540 | case 1: |
1359 | 540 | info->bytes_per_line = 6; |
1360 | 540 | (*info->fprintf_styled_func) |
1361 | 540 | (info->stream, dis_style_assembler_directive, ".byte"); |
1362 | 540 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1363 | 540 | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, |
1364 | 540 | "0x%02x", (unsigned)data); |
1365 | 540 | break; |
1366 | 87 | case 2: |
1367 | 87 | info->bytes_per_line = 8; |
1368 | 87 | (*info->fprintf_styled_func) |
1369 | 87 | (info->stream, dis_style_assembler_directive, ".short"); |
1370 | 87 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1371 | 87 | (*info->fprintf_styled_func) |
1372 | 87 | (info->stream, dis_style_immediate, "0x%04x", (unsigned) data); |
1373 | 87 | break; |
1374 | 29 | case 4: |
1375 | 29 | info->bytes_per_line = 8; |
1376 | 29 | (*info->fprintf_styled_func) |
1377 | 29 | (info->stream, dis_style_assembler_directive, ".word"); |
1378 | 29 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1379 | 29 | (*info->fprintf_styled_func) |
1380 | 29 | (info->stream, dis_style_immediate, "0x%08lx", |
1381 | 29 | (unsigned long) data); |
1382 | 29 | break; |
1383 | 32 | case 8: |
1384 | 32 | info->bytes_per_line = 8; |
1385 | 32 | (*info->fprintf_styled_func) |
1386 | 32 | (info->stream, dis_style_assembler_directive, ".dword"); |
1387 | 32 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1388 | 32 | (*info->fprintf_styled_func) |
1389 | 32 | (info->stream, dis_style_immediate, "0x%016llx", |
1390 | 32 | (unsigned long long) data); |
1391 | 32 | break; |
1392 | 159 | default: |
1393 | | /* Arbitrary data so just print the bits in the shape of an .<N>byte |
1394 | | directive. */ |
1395 | 159 | info->bytes_per_line = info->bytes_per_chunk; |
1396 | 159 | (*info->fprintf_styled_func) |
1397 | 159 | (info->stream, dis_style_assembler_directive, ".%dbyte", info->bytes_per_chunk); |
1398 | 159 | (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t"); |
1399 | 159 | (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x"); |
1400 | 1.15k | for (i = info->bytes_per_line; i > 0;) |
1401 | 997 | { |
1402 | 997 | i--; |
1403 | 997 | data = bfd_get_bits (packet + i, 8, false); |
1404 | 997 | (*info->fprintf_styled_func) |
1405 | 997 | (info->stream, dis_style_immediate, "%02x", |
1406 | 997 | (unsigned) data); |
1407 | 997 | } |
1408 | 159 | break; |
1409 | 847 | } |
1410 | 847 | return info->bytes_per_chunk; |
1411 | 847 | } |
1412 | | |
1413 | | static bool |
1414 | | riscv_init_disasm_info (struct disassemble_info *info) |
1415 | 1.48k | { |
1416 | 1.48k | int i; |
1417 | 1.48k | struct riscv_private_data *pd = |
1418 | 1.48k | xcalloc (1, sizeof (struct riscv_private_data)); |
1419 | 1.48k | pd->gp = 0; |
1420 | 1.48k | pd->print_addr = 0; |
1421 | 49.1k | for (i = 0; i < (int) ARRAY_SIZE (pd->hi_addr); i++) |
1422 | 47.6k | pd->hi_addr[i] = -1; |
1423 | 1.48k | pd->to_print_addr = false; |
1424 | | |
1425 | 1.48k | pd->has_gp = false; |
1426 | 1.48k | for (i = 0; i < info->symtab_size; i++) |
1427 | 0 | { |
1428 | 0 | asymbol *sym = info->symtab[i]; |
1429 | 0 | if (strcmp (bfd_asymbol_name (sym), RISCV_GP_SYMBOL) == 0) |
1430 | 0 | { |
1431 | 0 | pd->gp = bfd_asymbol_value (sym); |
1432 | 0 | pd->has_gp = true; |
1433 | 0 | } |
1434 | 0 | } |
1435 | | |
1436 | 1.48k | pd->xlen = 0; |
1437 | 1.48k | pd->default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1; |
1438 | 1.48k | pd->default_priv_spec = PRIV_SPEC_CLASS_NONE; |
1439 | | |
1440 | 1.48k | pd->riscv_rps_dis.subset_list = xcalloc (1, sizeof (riscv_parse_subset_t)); |
1441 | 1.48k | pd->riscv_rps_dis.error_handler = opcodes_error_handler; |
1442 | 1.48k | pd->riscv_rps_dis.xlen = &pd->xlen; |
1443 | 1.48k | pd->riscv_rps_dis.isa_spec = &pd->default_isa_spec; |
1444 | 1.48k | pd->riscv_rps_dis.check_unknown_prefixed_ext = false; |
1445 | 1.48k | pd->default_arch = "rv64gc"; |
1446 | 1.48k | if (info->section != NULL) |
1447 | 118 | { |
1448 | 118 | bfd *abfd = info->section->owner; |
1449 | 118 | if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour) |
1450 | 88 | { |
1451 | 88 | const char *sec_name = |
1452 | 88 | get_elf_backend_data (abfd)->obj_attrs_section; |
1453 | 88 | if (bfd_get_section_by_name (abfd, sec_name) != NULL) |
1454 | 0 | { |
1455 | 0 | obj_attribute *attr = elf_known_obj_attributes_proc (abfd); |
1456 | 0 | unsigned int Tag_a = Tag_RISCV_priv_spec; |
1457 | 0 | unsigned int Tag_b = Tag_RISCV_priv_spec_minor; |
1458 | 0 | unsigned int Tag_c = Tag_RISCV_priv_spec_revision; |
1459 | 0 | riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i, |
1460 | 0 | attr[Tag_b].i, |
1461 | 0 | attr[Tag_c].i, |
1462 | 0 | &pd->default_priv_spec); |
1463 | 0 | pd->default_arch = attr[Tag_RISCV_arch].s; |
1464 | 0 | } |
1465 | 88 | } |
1466 | 118 | } |
1467 | | |
1468 | 1.48k | pd->last_map_symbol = -1; |
1469 | 1.48k | pd->last_stop_offset = 0; |
1470 | 1.48k | pd->last_map_symbol_boundary = 0; |
1471 | 1.48k | pd->last_map_state = MAP_NONE; |
1472 | 1.48k | pd->last_map_section = NULL; |
1473 | 1.48k | pd->riscv_gpr_names = NULL; |
1474 | 1.48k | pd->riscv_fpr_names = NULL; |
1475 | 1.48k | pd->no_aliases = false; |
1476 | 1.48k | pd->all_ext = false; |
1477 | | |
1478 | 1.48k | info->private_data = pd; |
1479 | 1.48k | riscv_dis_parse_subset (info, pd->default_arch); |
1480 | 1.48k | return true; |
1481 | 1.48k | } |
1482 | | |
1483 | | /* Fetch an instruction. If only a partial instruction is able to be fetched, |
1484 | | return the number of accessible bytes. */ |
1485 | | |
1486 | | static bfd_vma |
1487 | | fetch_insn (bfd_vma memaddr, |
1488 | | bfd_byte *packet, |
1489 | | bfd_vma dump_size, |
1490 | | struct disassemble_info *info, |
1491 | | volatile int *status) |
1492 | 2.75M | { |
1493 | 2.75M | do |
1494 | 2.75M | { |
1495 | 2.75M | *status = (*info->read_memory_func) (memaddr, packet, dump_size, info); |
1496 | 2.75M | } |
1497 | 2.75M | while (*status != 0 && dump_size-- > 1); |
1498 | | |
1499 | 2.75M | return dump_size; |
1500 | 2.75M | } |
1501 | | |
1502 | | int |
1503 | | print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info) |
1504 | 1.37M | { |
1505 | 1.37M | bfd_byte packet[RISCV_MAX_INSN_LEN]; |
1506 | 1.37M | insn_t insn = 0; |
1507 | 1.37M | bfd_vma dump_size, bytes_fetched; |
1508 | 1.37M | int status; |
1509 | 1.37M | enum riscv_seg_mstate mstate; |
1510 | 1.37M | int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *, |
1511 | 1.37M | struct disassemble_info *); |
1512 | | |
1513 | 1.37M | if (info->private_data == NULL && !riscv_init_disasm_info (info)) |
1514 | 0 | return -1; |
1515 | | |
1516 | 1.37M | if (info->disassembler_options != NULL) |
1517 | 0 | { |
1518 | 0 | parse_riscv_dis_options (info->disassembler_options, info); |
1519 | | /* Avoid repeatedly parsing the options. */ |
1520 | 0 | info->disassembler_options = NULL; |
1521 | 0 | } |
1522 | 1.37M | else if (((struct riscv_private_data *) info->private_data)->riscv_gpr_names == NULL) |
1523 | 1.48k | set_default_riscv_dis_options (info); |
1524 | | |
1525 | 1.37M | mstate = riscv_search_mapping_symbol (memaddr, info); |
1526 | | /* Save the last mapping state. */ |
1527 | 1.37M | ((struct riscv_private_data *) info->private_data)->last_map_state = mstate; |
1528 | | |
1529 | | /* Set the size to dump. */ |
1530 | 1.37M | if (mstate == MAP_DATA |
1531 | 1.37M | && (info->flags & DISASSEMBLE_DATA) == 0) |
1532 | 0 | { |
1533 | 0 | dump_size = riscv_data_length (memaddr, info); |
1534 | 0 | info->bytes_per_chunk = dump_size; |
1535 | 0 | riscv_disassembler = riscv_disassemble_data; |
1536 | 0 | } |
1537 | 1.37M | else |
1538 | 1.37M | { |
1539 | | /* Get the first 2-bytes to check the lenghth of instruction. */ |
1540 | 1.37M | bytes_fetched = fetch_insn (memaddr, packet, 2, info, &status); |
1541 | 1.37M | if (status != 0) |
1542 | 3 | { |
1543 | 3 | (*info->memory_error_func) (status, memaddr, info); |
1544 | 3 | return -1; |
1545 | 3 | } |
1546 | 1.37M | else if (bytes_fetched != 2) |
1547 | 540 | { |
1548 | | /* Only the first byte was able to be read. Dump the partial |
1549 | | instruction. */ |
1550 | 540 | dump_size = bytes_fetched; |
1551 | 540 | info->bytes_per_chunk = dump_size; |
1552 | 540 | riscv_disassembler = riscv_disassemble_data; |
1553 | 540 | goto print; |
1554 | 540 | } |
1555 | 1.37M | insn = (insn_t) bfd_getl16 (packet); |
1556 | 1.37M | dump_size = riscv_insn_length (insn); |
1557 | 1.37M | riscv_disassembler = riscv_disassemble_insn; |
1558 | 1.37M | } |
1559 | | |
1560 | 1.37M | bytes_fetched = fetch_insn (memaddr, packet, dump_size, info, &status); |
1561 | | |
1562 | 1.37M | if (status != 0) |
1563 | 0 | { |
1564 | 0 | (*info->memory_error_func) (status, memaddr, info); |
1565 | 0 | return -1; |
1566 | 0 | } |
1567 | 1.37M | else if (bytes_fetched != dump_size) |
1568 | 307 | { |
1569 | 307 | dump_size = bytes_fetched; |
1570 | 307 | info->bytes_per_chunk = dump_size; |
1571 | 307 | riscv_disassembler = riscv_disassemble_data; |
1572 | 307 | } |
1573 | | |
1574 | 1.37M | print: |
1575 | | |
1576 | 1.37M | insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false); |
1577 | | |
1578 | 1.37M | return (*riscv_disassembler) (memaddr, insn, packet, info); |
1579 | 1.37M | } |
1580 | | |
1581 | | /* Prevent use of the fake labels that are generated as part of the DWARF |
1582 | | and for relaxable relocations in the assembler. */ |
1583 | | |
1584 | | bool |
1585 | | riscv_symbol_is_valid (asymbol * sym, |
1586 | | struct disassemble_info * info ATTRIBUTE_UNUSED) |
1587 | 0 | { |
1588 | 0 | const char * name; |
1589 | |
|
1590 | 0 | if (sym == NULL) |
1591 | 0 | return false; |
1592 | | |
1593 | 0 | name = bfd_asymbol_name (sym); |
1594 | |
|
1595 | 0 | return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0 |
1596 | 0 | && !riscv_elf_is_mapping_symbols (name)); |
1597 | 0 | } |
1598 | | |
1599 | | |
1600 | | /* Indices into option argument vector for options accepting an argument. |
1601 | | Use RISCV_OPTION_ARG_NONE for options accepting no argument. */ |
1602 | | |
1603 | | typedef enum |
1604 | | { |
1605 | | RISCV_OPTION_ARG_NONE = -1, |
1606 | | RISCV_OPTION_ARG_PRIV_SPEC, |
1607 | | |
1608 | | RISCV_OPTION_ARG_COUNT |
1609 | | } riscv_option_arg_t; |
1610 | | |
1611 | | /* Valid RISCV disassembler options. */ |
1612 | | |
1613 | | static struct |
1614 | | { |
1615 | | const char *name; |
1616 | | const char *description; |
1617 | | riscv_option_arg_t arg; |
1618 | | } riscv_options[] = |
1619 | | { |
1620 | | { "max", |
1621 | | N_("Disassemble without checking architecture string."), |
1622 | | RISCV_OPTION_ARG_NONE }, |
1623 | | { "numeric", |
1624 | | N_("Print numeric register names, rather than ABI names."), |
1625 | | RISCV_OPTION_ARG_NONE }, |
1626 | | { "no-aliases", |
1627 | | N_("Disassemble only into canonical instructions."), |
1628 | | RISCV_OPTION_ARG_NONE }, |
1629 | | { "priv-spec=", |
1630 | | N_("Print the CSR according to the chosen privilege spec."), |
1631 | | RISCV_OPTION_ARG_PRIV_SPEC } |
1632 | | }; |
1633 | | |
1634 | | /* Build the structure representing valid RISCV disassembler options. |
1635 | | This is done dynamically for maintenance ease purpose; a static |
1636 | | initializer would be unreadable. */ |
1637 | | |
1638 | | const disasm_options_and_args_t * |
1639 | | disassembler_options_riscv (void) |
1640 | 0 | { |
1641 | 0 | static disasm_options_and_args_t *opts_and_args; |
1642 | |
|
1643 | 0 | if (opts_and_args == NULL) |
1644 | 0 | { |
1645 | 0 | size_t num_options = ARRAY_SIZE (riscv_options); |
1646 | 0 | size_t num_args = RISCV_OPTION_ARG_COUNT; |
1647 | 0 | disasm_option_arg_t *args; |
1648 | 0 | disasm_options_t *opts; |
1649 | 0 | size_t i, priv_spec_count; |
1650 | |
|
1651 | 0 | args = XNEWVEC (disasm_option_arg_t, num_args + 1); |
1652 | |
|
1653 | 0 | args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC"; |
1654 | 0 | priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_EARLIEST; |
1655 | 0 | args[RISCV_OPTION_ARG_PRIV_SPEC].values |
1656 | 0 | = XNEWVEC (const char *, priv_spec_count + 1); |
1657 | 0 | for (i = 0; i < priv_spec_count; i++) |
1658 | 0 | args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] |
1659 | 0 | = riscv_priv_specs[PRIV_SPEC_EARLIEST - PRIV_SPEC_CLASS_NONE - 1 + i].name; |
1660 | | /* The array we return must be NULL terminated. */ |
1661 | 0 | args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL; |
1662 | | |
1663 | | /* The array we return must be NULL terminated. */ |
1664 | 0 | args[num_args].name = NULL; |
1665 | 0 | args[num_args].values = NULL; |
1666 | |
|
1667 | 0 | opts_and_args = XNEW (disasm_options_and_args_t); |
1668 | 0 | opts_and_args->args = args; |
1669 | |
|
1670 | 0 | opts = &opts_and_args->options; |
1671 | 0 | opts->name = XNEWVEC (const char *, num_options + 1); |
1672 | 0 | opts->description = XNEWVEC (const char *, num_options + 1); |
1673 | 0 | opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1); |
1674 | 0 | for (i = 0; i < num_options; i++) |
1675 | 0 | { |
1676 | 0 | opts->name[i] = riscv_options[i].name; |
1677 | 0 | opts->description[i] = _(riscv_options[i].description); |
1678 | 0 | if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE) |
1679 | 0 | opts->arg[i] = &args[riscv_options[i].arg]; |
1680 | 0 | else |
1681 | 0 | opts->arg[i] = NULL; |
1682 | 0 | } |
1683 | | /* The array we return must be NULL terminated. */ |
1684 | 0 | opts->name[i] = NULL; |
1685 | 0 | opts->description[i] = NULL; |
1686 | 0 | opts->arg[i] = NULL; |
1687 | 0 | } |
1688 | |
|
1689 | 0 | return opts_and_args; |
1690 | 0 | } |
1691 | | |
1692 | | void |
1693 | | print_riscv_disassembler_options (FILE *stream) |
1694 | 0 | { |
1695 | 0 | const disasm_options_and_args_t *opts_and_args; |
1696 | 0 | const disasm_option_arg_t *args; |
1697 | 0 | const disasm_options_t *opts; |
1698 | 0 | size_t max_len = 0; |
1699 | 0 | size_t i; |
1700 | 0 | size_t j; |
1701 | |
|
1702 | 0 | opts_and_args = disassembler_options_riscv (); |
1703 | 0 | opts = &opts_and_args->options; |
1704 | 0 | args = opts_and_args->args; |
1705 | |
|
1706 | 0 | fprintf (stream, _("\n\ |
1707 | 0 | The following RISC-V specific disassembler options are supported for use\n\ |
1708 | 0 | with the -M switch (multiple options should be separated by commas):\n")); |
1709 | 0 | fprintf (stream, "\n"); |
1710 | | |
1711 | | /* Compute the length of the longest option name. */ |
1712 | 0 | for (i = 0; opts->name[i] != NULL; i++) |
1713 | 0 | { |
1714 | 0 | size_t len = strlen (opts->name[i]); |
1715 | |
|
1716 | 0 | if (opts->arg[i] != NULL) |
1717 | 0 | len += strlen (opts->arg[i]->name); |
1718 | 0 | if (max_len < len) |
1719 | 0 | max_len = len; |
1720 | 0 | } |
1721 | |
|
1722 | 0 | for (i = 0, max_len++; opts->name[i] != NULL; i++) |
1723 | 0 | { |
1724 | 0 | fprintf (stream, " %s", opts->name[i]); |
1725 | 0 | if (opts->arg[i] != NULL) |
1726 | 0 | fprintf (stream, "%s", opts->arg[i]->name); |
1727 | 0 | if (opts->description[i] != NULL) |
1728 | 0 | { |
1729 | 0 | size_t len = strlen (opts->name[i]); |
1730 | |
|
1731 | 0 | if (opts->arg != NULL && opts->arg[i] != NULL) |
1732 | 0 | len += strlen (opts->arg[i]->name); |
1733 | 0 | fprintf (stream, "%*c %s", (int) (max_len - len), ' ', |
1734 | 0 | opts->description[i]); |
1735 | 0 | } |
1736 | 0 | fprintf (stream, "\n"); |
1737 | 0 | } |
1738 | |
|
1739 | 0 | for (i = 0; args[i].name != NULL; i++) |
1740 | 0 | { |
1741 | 0 | if (args[i].values == NULL) |
1742 | 0 | continue; |
1743 | 0 | fprintf (stream, _("\n\ |
1744 | 0 | For the options above, the following values are supported for \"%s\":\n "), |
1745 | 0 | args[i].name); |
1746 | 0 | for (j = 0; args[i].values[j] != NULL; j++) |
1747 | 0 | fprintf (stream, " %s", args[i].values[j]); |
1748 | 0 | fprintf (stream, _("\n")); |
1749 | 0 | } |
1750 | |
|
1751 | 0 | fprintf (stream, _("\n")); |
1752 | 0 | } |
1753 | | |
1754 | | void disassemble_free_riscv (struct disassemble_info *info ATTRIBUTE_UNUSED) |
1755 | 1.58k | { |
1756 | 1.58k | struct riscv_private_data *pd = info->private_data; |
1757 | 1.58k | if (pd) |
1758 | 1.48k | { |
1759 | 1.48k | riscv_release_subset_list (pd->riscv_rps_dis.subset_list); |
1760 | 1.48k | free (pd->riscv_rps_dis.subset_list); |
1761 | 1.48k | } |
1762 | 1.58k | } |