Coverage Report

Created: 2026-10-02 09:53

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