Coverage Report

Created: 2026-07-25 10:20

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