Coverage Report

Created: 2026-07-12 09:22

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.14k
{
82
1.14k
  struct riscv_private_data *pd = info->private_data;
83
1.14k
  pd->riscv_gpr_names = riscv_gpr_names_abi;
84
1.14k
  pd->riscv_fpr_names = riscv_fpr_names_abi;
85
1.14k
  pd->no_aliases = false;
86
1.14k
  pd->all_ext = false;
87
1.14k
}
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
15.0k
{
207
15.0k
  const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
208
15.0k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s);
209
15.0k
}
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
111k
{
217
111k
  if (pd->hi_addr[base_reg] != (bfd_vma)-1)
218
12.6k
    {
219
12.6k
      pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
220
12.6k
      pd->hi_addr[base_reg] = -1;
221
12.6k
    }
222
98.3k
  else if (base_reg == X_GP && pd->has_gp)
223
0
    pd->print_addr = pd->gp + offset;
224
98.3k
  else if (base_reg == X_TP || base_reg == 0)
225
10.0k
    pd->print_addr = offset;
226
88.3k
  else
227
88.3k
    return;  /* Don't print the address.  */
228
22.6k
  pd->to_print_addr = true;
229
230
  /* Sign-extend a 32-bit value to a 64-bit value.  */
231
22.6k
  if (wide)
232
5.92k
    pd->print_addr = (bfd_vma)(int32_t) pd->print_addr;
233
234
  /* Fit into a 32-bit value on RV32.  */
235
22.6k
  if (pd->xlen == 32)
236
0
    pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr;
237
22.6k
}
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.24M
{
321
1.24M
  struct riscv_private_data *pd = info->private_data;
322
1.24M
  int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
323
1.24M
  int rd = (l >> OP_SH_RD) & OP_MASK_RD;
324
1.24M
  fprintf_styled_ftype print = info->fprintf_styled_func;
325
1.24M
  const char *opargStart;
326
327
1.24M
  if (*oparg != '\0')
328
984k
    print (info->stream, dis_style_text, "\t");
329
330
6.15M
  for (; *oparg != '\0'; oparg++)
331
4.91M
    {
332
4.91M
      opargStart = oparg;
333
4.91M
      switch (*oparg)
334
4.91M
  {
335
2.15M
  case 'C': /* RVC */
336
2.15M
    switch (*++oparg)
337
2.15M
      {
338
330k
      case 's': /* RS1 x8-x15.  */
339
357k
      case 'w': /* RS1 x8-x15.  */
340
357k
        print (info->stream, dis_style_register, "%s",
341
357k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
342
357k
        break;
343
302k
      case 't': /* RS2 x8-x15.  */
344
302k
      case 'x': /* RS2 x8-x15.  */
345
302k
        print (info->stream, dis_style_register, "%s",
346
302k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
347
302k
        break;
348
131k
      case 'U': /* RS1, constrained to equal RD.  */
349
131k
        print (info->stream, dis_style_register,
350
131k
         "%s", pd->riscv_gpr_names[rd]);
351
131k
        break;
352
310k
      case 'c': /* RS1, constrained to equal sp.  */
353
310k
        print (info->stream, dis_style_register, "%s",
354
310k
         pd->riscv_gpr_names[X_SP]);
355
310k
        break;
356
73.4k
      case 'V': /* RS2 */
357
73.4k
        print (info->stream, dis_style_register, "%s",
358
73.4k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
359
73.4k
        break;
360
64.5k
      case 'o':
361
129k
      case 'j':
362
129k
        if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0)
363
47.0k
    maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0);
364
129k
        if (pd->xlen == 64
365
129k
      && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0)
366
33.3k
    maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1);
367
129k
        print (info->stream, dis_style_immediate, "%d",
368
129k
         (int)EXTRACT_CITYPE_IMM (l));
369
129k
        break;
370
68.7k
      case 'k':
371
68.7k
        print (info->stream, dis_style_address_offset, "%d",
372
68.7k
         (int)EXTRACT_CLTYPE_LW_IMM (l));
373
68.7k
        break;
374
184k
      case 'l':
375
184k
        print (info->stream, dis_style_address_offset, "%d",
376
184k
         (int)EXTRACT_CLTYPE_LD_IMM (l));
377
184k
        break;
378
29.2k
      case 'm':
379
29.2k
        print (info->stream, dis_style_address_offset, "%d",
380
29.2k
         (int)EXTRACT_CITYPE_LWSP_IMM (l));
381
29.2k
        break;
382
62.1k
      case 'n':
383
62.1k
        print (info->stream, dis_style_address_offset, "%d",
384
62.1k
         (int)EXTRACT_CITYPE_LDSP_IMM (l));
385
62.1k
        break;
386
132k
      case 'K':
387
132k
        print (info->stream, dis_style_immediate, "%d",
388
132k
         (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
389
132k
        break;
390
7.39k
      case 'L':
391
7.39k
        print (info->stream, dis_style_immediate, "%d",
392
7.39k
         (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
393
7.39k
        break;
394
27.4k
      case 'M':
395
27.4k
        print (info->stream, dis_style_address_offset, "%d",
396
27.4k
         (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
397
27.4k
        break;
398
44.5k
      case 'N':
399
44.5k
        print (info->stream, dis_style_address_offset, "%d",
400
44.5k
         (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
401
44.5k
        break;
402
47.9k
      case 'p':
403
47.9k
        info->target = EXTRACT_CBTYPE_IMM (l) + pc;
404
47.9k
        (*info->print_address_func) (info->target, info);
405
47.9k
        break;
406
18.0k
      case 'a':
407
18.0k
        info->target = EXTRACT_CJTYPE_IMM (l) + pc;
408
18.0k
        (*info->print_address_func) (info->target, info);
409
18.0k
        break;
410
35.4k
      case 'u':
411
35.4k
        print (info->stream, dis_style_immediate, "0x%x",
412
35.4k
         (unsigned)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
413
35.4k
        break;
414
86.2k
      case '>':
415
86.2k
        print (info->stream, dis_style_immediate, "0x%x",
416
86.2k
         (unsigned)EXTRACT_CITYPE_IMM (l) & 0x3f);
417
86.2k
        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
17.8k
      case 'T': /* Floating-point RS2.  */
423
17.8k
        print (info->stream, dis_style_register, "%s",
424
17.8k
         pd->riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
425
17.8k
        break;
426
86.2k
      case 'D': /* Floating-point RS2 x8-x15.  */
427
86.2k
        print (info->stream, dis_style_register, "%s",
428
86.2k
         pd->riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
429
86.2k
        break;
430
2.15M
      }
431
2.15M
    break;
432
433
2.15M
  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.30M
  case ',':
538
1.74M
  case '(':
539
2.18M
  case ')':
540
2.18M
  case '[':
541
2.18M
  case ']':
542
2.18M
  case '{':
543
2.18M
  case '}':
544
2.18M
    print (info->stream, dis_style_text, "%c", *oparg);
545
2.18M
    break;
546
547
1.34k
  case '0':
548
    /* Only print constant 0 if it is the last argument.  */
549
1.34k
    if (!oparg[1])
550
0
      print (info->stream, dis_style_immediate, "0");
551
1.34k
    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
41.4k
  case 's':
559
41.4k
    if ((l & MASK_JALR) == MATCH_JALR)
560
3.19k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
561
41.4k
    print (info->stream, dis_style_register, "%s",
562
41.4k
     pd->riscv_gpr_names[rs1]);
563
41.4k
    break;
564
565
14.4k
  case 't':
566
14.4k
    print (info->stream, dis_style_register, "%s",
567
14.4k
     pd->riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
568
14.4k
    break;
569
570
13.4k
  case 'u':
571
13.4k
    print (info->stream, dis_style_immediate, "0x%x",
572
13.4k
     (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
573
13.4k
    break;
574
575
13.7k
  case 'm':
576
13.7k
    arg_print (info, EXTRACT_OPERAND (RM, l),
577
13.7k
         riscv_rm, ARRAY_SIZE (riscv_rm));
578
13.7k
    break;
579
580
670
  case 'P':
581
670
    arg_print (info, EXTRACT_OPERAND (PRED, l),
582
670
         riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
583
670
    break;
584
585
670
  case 'Q':
586
670
    arg_print (info, EXTRACT_OPERAND (SUCC, l),
587
670
         riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
588
670
    break;
589
590
14.1k
  case 'o':
591
14.1k
    maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
592
    /* Fall through.  */
593
19.9k
  case 'j':
594
19.9k
    if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
595
18.9k
        || (l & MASK_JALR) == MATCH_JALR)
596
3.29k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
597
19.9k
    if (pd->xlen == 64
598
19.9k
        && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0)
599
1.86k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1);
600
19.9k
    print (info->stream, dis_style_immediate, "%d",
601
19.9k
     (int)EXTRACT_ITYPE_IMM (l));
602
19.9k
    break;
603
604
8.11k
  case 'q':
605
8.11k
    maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0);
606
8.11k
    print (info->stream, dis_style_address_offset, "%d",
607
8.11k
     (int)EXTRACT_STYPE_IMM (l));
608
8.11k
    break;
609
610
8.22k
  case 'a':
611
8.22k
    info->target = EXTRACT_JTYPE_IMM (l) + pc;
612
8.22k
    (*info->print_address_func) (info->target, info);
613
8.22k
    break;
614
615
6.50k
  case 'p':
616
6.50k
    info->target = EXTRACT_BTYPE_IMM (l) + pc;
617
6.50k
    (*info->print_address_func) (info->target, info);
618
6.50k
    break;
619
620
342k
  case 'd':
621
342k
    if ((l & MASK_AUIPC) == MATCH_AUIPC)
622
5.82k
      pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
623
336k
    else if ((l & MASK_LUI) == MATCH_LUI)
624
7.65k
      pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
625
328k
    else if ((l & MASK_C_LUI) == MATCH_C_LUI)
626
35.4k
      pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
627
342k
    print (info->stream, dis_style_register, "%s",
628
342k
     pd->riscv_gpr_names[rd]);
629
342k
    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
176
  case '>':
642
176
    print (info->stream, dis_style_immediate, "0x%x",
643
176
     EXTRACT_OPERAND (SHAMT, l));
644
176
    break;
645
646
244
  case '<':
647
244
    print (info->stream, dis_style_immediate, "0x%x",
648
244
     EXTRACT_OPERAND (SHAMTW, l));
649
244
    break;
650
651
14.8k
  case 'S':
652
15.0k
  case 'U':
653
15.0k
    print (info->stream, dis_style_register, "%s",
654
15.0k
     pd->riscv_fpr_names[rs1]);
655
15.0k
    break;
656
657
17.7k
  case 'T':
658
17.7k
    print (info->stream, dis_style_register, "%s",
659
17.7k
     pd->riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
660
17.7k
    break;
661
662
45.4k
  case 'D':
663
45.4k
    print (info->stream, dis_style_register, "%s",
664
45.4k
     pd->riscv_fpr_names[rd]);
665
45.4k
    break;
666
667
13.8k
  case 'R':
668
13.8k
    print (info->stream, dis_style_register, "%s",
669
13.8k
     pd->riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
670
13.8k
    break;
671
672
7.94k
  case 'E':
673
7.94k
    {
674
7.94k
      static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs.  */
675
7.94k
      static bool init_csr = false;
676
7.94k
      unsigned int csr = EXTRACT_OPERAND (CSR, l);
677
678
7.94k
      if (!init_csr)
679
7.94k
        {
680
7.94k
    unsigned int i;
681
32.5M
    for (i = 0; i < 4096; i++)
682
32.5M
      riscv_csr_hash[i] = NULL;
683
684
    /* Set to the newest privileged version.  */
685
7.94k
    if (pd->default_priv_spec == PRIV_SPEC_CLASS_NONE)
686
654
      pd->default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1;
687
688
7.94k
#define DECLARE_CSR(name, num, class, define_version, abort_version)  \
689
3.70M
    if (riscv_csr_hash[num] == NULL      \
690
3.70M
        && ((define_version == PRIV_SPEC_CLASS_NONE  \
691
3.58M
       && abort_version == PRIV_SPEC_CLASS_NONE) \
692
3.58M
      || (pd->default_priv_spec >= define_version  \
693
2.28M
          && pd->default_priv_spec < abort_version))) \
694
3.70M
      riscv_csr_hash[num] = #name;
695
7.94k
#define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
696
119k
    DECLARE_CSR (name, num, class, define_version, abort_version)
697
7.94k
#include "opcode/riscv-opc.h"
698
7.94k
#undef DECLARE_CSR
699
7.94k
        }
700
701
7.94k
      if (riscv_csr_hash[csr] != NULL)
702
1.77k
        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
1.77k
        else
712
1.77k
    print (info->stream, dis_style_register, "%s",
713
1.77k
           riscv_csr_hash[csr]);
714
6.16k
      else
715
6.16k
        print (info->stream, dis_style_immediate, "0x%x", csr);
716
7.94k
      break;
717
14.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
5.64k
  case 'Z':
725
5.64k
    print (info->stream, dis_style_immediate, "%d", rs1);
726
5.64k
    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
4.91M
  }
1043
4.91M
    }
1044
1.24M
}
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.57M
{
1057
1.57M
  const struct riscv_opcode *op;
1058
1.57M
  static bool init = false;
1059
1.57M
  static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
1060
1.57M
  struct riscv_private_data *pd = info->private_data;
1061
1.57M
  int insnlen, i;
1062
1.57M
  bool printed;
1063
1064
1.58M
#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.57M
  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.57M
  insnlen = riscv_insn_length (word);
1077
1078
  /* RISC-V instructions are always little-endian.  */
1079
1.57M
  info->endian_code = BFD_ENDIAN_LITTLE;
1080
1081
1.57M
  info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
1082
1.57M
  info->bytes_per_line = 8;
1083
  /* We don't support constant pools, so this must be code.  */
1084
1.57M
  info->display_endian = info->endian_code;
1085
1.57M
  info->insn_info_valid = 1;
1086
1.57M
  info->branch_delay_insns = 0;
1087
1.57M
  info->data_size = 0;
1088
1.57M
  info->insn_type = dis_nonbranch;
1089
1.57M
  info->target = 0;
1090
1.57M
  info->target2 = 0;
1091
1092
1.57M
  op = riscv_hash[OP_HASH_IDX (word)];
1093
1.57M
  if (op != NULL)
1094
1.52M
    {
1095
      /* If XLEN is not known, get its value from the ELF class.  */
1096
1.52M
      if (pd->xlen != 0)
1097
1.52M
  ;
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.52M
      if (riscv_subset_supports (&pd->riscv_rps_dis, "zfinx"))
1110
0
  pd->riscv_fpr_names = pd->riscv_gpr_names;
1111
1.52M
      else
1112
1.52M
  pd->riscv_fpr_names = pd->riscv_gpr_names == riscv_gpr_names_abi ?
1113
1.52M
            riscv_fpr_names_abi : riscv_fpr_names_numeric;
1114
1115
944M
      for (; op->name; op++)
1116
943M
  {
1117
    /* Ignore macro insns.  */
1118
943M
    if (op->pinfo == INSN_MACRO)
1119
23.0M
      continue;
1120
    /* Does the opcode match?  */
1121
920M
    if (! (op->match_func) (op, word))
1122
919M
      continue;
1123
    /* Is this a pseudo-instruction and may we print it as such?  */
1124
1.41M
    if (pd->no_aliases && (op->pinfo & INSN_ALIAS))
1125
0
      continue;
1126
    /* Is this instruction restricted to a certain value of XLEN?  */
1127
1.41M
    if ((op->xlen_requirement != 0)
1128
244k
        && (op->xlen_requirement != pd->xlen))
1129
42.7k
      continue;
1130
    /* Is this instruction supported by the current architecture?  */
1131
1.36M
    if (!pd->all_ext
1132
1.36M
        && !riscv_multi_subset_supports (&pd->riscv_rps_dis,
1133
1.36M
                 op->insn_class))
1134
124k
      continue;
1135
1136
    /* It's a match.  */
1137
1.24M
    (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
1138
1.24M
          "%s", op->name);
1139
1.24M
    print_insn_args (op->args, word, memaddr, info);
1140
1141
    /* Try to disassemble multi-instruction addressing sequences.  */
1142
1.24M
    if (pd->to_print_addr)
1143
20.7k
      {
1144
20.7k
        info->target = pd->print_addr;
1145
20.7k
        (*info->fprintf_styled_func)
1146
20.7k
    (info->stream, dis_style_comment_start, " # ");
1147
20.7k
        (*info->print_address_func) (info->target, info);
1148
20.7k
        pd->to_print_addr = false;
1149
20.7k
      }
1150
1151
    /* Finish filling out insn_info fields.  */
1152
1.24M
    switch (op->pinfo & INSN_TYPE)
1153
1.24M
      {
1154
22.7k
      case INSN_BRANCH:
1155
22.7k
        info->insn_type = dis_branch;
1156
22.7k
        break;
1157
54.5k
      case INSN_CONDBRANCH:
1158
54.5k
        info->insn_type = dis_condbranch;
1159
54.5k
        break;
1160
9.06k
      case INSN_JSR:
1161
9.06k
        info->insn_type = dis_jsr;
1162
9.06k
        break;
1163
437k
      case INSN_DREF:
1164
437k
        info->insn_type = dis_dref;
1165
437k
        break;
1166
718k
      default:
1167
718k
        break;
1168
1.24M
      }
1169
1170
1.24M
    if (op->pinfo & INSN_DATA_SIZE)
1171
437k
      {
1172
437k
        int size = ((op->pinfo & INSN_DATA_SIZE)
1173
437k
        >> INSN_DATA_SIZE_SHIFT);
1174
437k
        info->data_size = 1 << (size - 1);
1175
437k
      }
1176
1177
1.24M
    return insnlen;
1178
1.24M
  }
1179
1.52M
    }
1180
1181
  /* We did not find a match, so just print the instruction bits in
1182
     the shape of an assembler .insn directive.  */
1183
331k
  info->insn_type = dis_noninsn;
1184
331k
  (*info->fprintf_styled_func)
1185
331k
    (info->stream, dis_style_assembler_directive, ".insn");
1186
331k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1187
331k
  (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1188
331k
        "%d", insnlen);
1189
331k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, ", ");
1190
331k
  (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x");
1191
982k
  for (i = insnlen, printed = false; i >= 2; )
1192
650k
    {
1193
650k
      i -= 2;
1194
650k
      word = bfd_get_bits (packet + i, 16, false);
1195
650k
      if (!word && !printed && i)
1196
19.1k
  continue;
1197
1198
631k
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1199
631k
            "%04x", (unsigned int) word);
1200
631k
      printed = true;
1201
631k
    }
1202
1203
331k
  return insnlen;
1204
1.57M
}
1205
1206
/* Decide if we need to parse the architecture string again, also record the
1207
   string into the current subset list.  */
1208
1209
static void
1210
riscv_dis_parse_subset (struct disassemble_info *info, const char *arch_new)
1211
1.14k
{
1212
1.14k
  struct riscv_private_data *pd = info->private_data;
1213
1.14k
  const char *arch_subset_list = pd->riscv_rps_dis.subset_list->arch_str;
1214
1.14k
  if (arch_subset_list == NULL || strcmp (arch_subset_list, arch_new) != 0)
1215
1.14k
    {
1216
1.14k
      riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
1217
1.14k
      riscv_parse_subset (&pd->riscv_rps_dis, arch_new);
1218
1.14k
      riscv_arch_str (pd->xlen, pd->riscv_rps_dis.subset_list,
1219
1.14k
          true/* update */);
1220
1.14k
    }
1221
1.14k
}
1222
1223
/* If we find the suitable mapping symbol update the STATE.
1224
   Otherwise, do nothing.  */
1225
1226
static void
1227
riscv_update_map_state (int n,
1228
      enum riscv_seg_mstate *state,
1229
      struct disassemble_info *info)
1230
0
{
1231
0
  struct riscv_private_data *pd = info->private_data;
1232
0
  const char *name;
1233
1234
  /* If the symbol is in a different section, ignore it.  */
1235
0
  if (info->section != NULL
1236
0
      && info->section != info->symtab[n]->section)
1237
0
    return;
1238
1239
0
  name = bfd_asymbol_name(info->symtab[n]);
1240
0
  if (strcmp (name, "$d") == 0)
1241
0
    *state = MAP_DATA;
1242
0
  else if (strcmp (name, "$x") == 0)
1243
0
    {
1244
0
      *state = MAP_INSN;
1245
0
      riscv_dis_parse_subset (info, pd->default_arch);
1246
0
    }
1247
0
  else if (strncmp (name, "$xrv", 4) == 0)
1248
0
    {
1249
0
      *state = MAP_INSN;
1250
1251
      /* ISA mapping string may be numbered, suffixed with '.n'. Do not
1252
   consider this as part of the ISA string.  */
1253
0
      const char *suffix = strchr (name, '.');
1254
0
      if (suffix)
1255
0
  {
1256
0
    int suffix_index = (int)(suffix - name);
1257
0
    char *name_substr = xmalloc (suffix_index + 1);
1258
0
    strncpy (name_substr, name, suffix_index);
1259
0
    name_substr[suffix_index] = '\0';
1260
0
    riscv_dis_parse_subset (info, name_substr + 2);
1261
0
    free (name_substr);
1262
0
  }
1263
0
      else
1264
0
  riscv_dis_parse_subset (info, name + 2);
1265
0
    }
1266
0
}
1267
1268
/* Return true if we find the suitable mapping symbol.
1269
   Otherwise, return false.  */
1270
1271
static bool
1272
riscv_is_valid_mapping_symbol (int n,
1273
           struct disassemble_info *info)
1274
0
{
1275
0
  const char *name;
1276
1277
  /* If the symbol is in a different section, ignore it.  */
1278
0
  if (info->section != NULL
1279
0
      && info->section != info->symtab[n]->section)
1280
0
    return false;
1281
1282
0
  name = bfd_asymbol_name(info->symtab[n]);
1283
0
  return riscv_elf_is_mapping_symbols (name);
1284
0
}
1285
1286
/* Check the sorted symbol table (sorted by the symbol value), find the
1287
   suitable mapping symbols.  */
1288
1289
static enum riscv_seg_mstate
1290
riscv_search_mapping_symbol (bfd_vma memaddr,
1291
           struct disassemble_info *info)
1292
1.57M
{
1293
1.57M
  struct riscv_private_data *pd = info->private_data;
1294
1.57M
  enum riscv_seg_mstate mstate;
1295
1.57M
  bool from_last_map_symbol;
1296
1.57M
  bool found = false;
1297
1.57M
  int symbol = -1;
1298
1.57M
  int n;
1299
1300
  /* Return the last map state if the address is still within the range of the
1301
     last mapping symbol.  */
1302
1.57M
  if (pd->last_map_section == info->section
1303
1.57M
      && (memaddr < pd->last_map_symbol_boundary))
1304
0
    return pd->last_map_state;
1305
1306
1.57M
  pd->last_map_section = info->section;
1307
1308
  /* Decide whether to print the data or instruction by default, in case
1309
     we can not find the corresponding mapping symbols.  */
1310
1.57M
  mstate = MAP_DATA;
1311
1.57M
  if ((info->section
1312
883k
       && info->section->flags & SEC_CODE)
1313
1.57M
      || !info->section)
1314
692k
    mstate = MAP_INSN;
1315
1316
1.57M
  if (info->symtab_size == 0
1317
0
      || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
1318
1.57M
    return mstate;
1319
1320
  /* Reset the last_map_symbol if we start to dump a new section.  */
1321
0
  if (memaddr <= 0)
1322
0
    pd->last_map_symbol = -1;
1323
1324
  /* If the last stop offset is different from the current one, then
1325
     don't use the last_map_symbol to search.  We usually reset the
1326
     info->stop_offset when handling a new section.  */
1327
0
  from_last_map_symbol = (pd->last_map_symbol >= 0
1328
0
        && info->stop_offset == pd->last_stop_offset);
1329
1330
  /* Start scanning from wherever we finished last time, or the start
1331
     of the function.  */
1332
0
  n = from_last_map_symbol ? pd->last_map_symbol : info->symtab_pos + 1;
1333
1334
  /* Find the suitable mapping symbol to dump.  */
1335
0
  for (; n < info->symtab_size; n++)
1336
0
    {
1337
0
      bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1338
      /* We have searched all possible symbols in the range.  */
1339
0
      if (addr > memaddr)
1340
0
  break;
1341
0
      if (riscv_is_valid_mapping_symbol (n, info))
1342
0
  {
1343
0
    symbol = n;
1344
0
    found = true;
1345
    /* Do not stop searching, in case there are some mapping
1346
       symbols have the same value, but have different names.
1347
       Use the last one.  */
1348
0
  }
1349
0
    }
1350
1351
  /* We can not find the suitable mapping symbol above.  Therefore, we
1352
     look forwards and try to find it again, but don't go past the start
1353
     of the section.  Otherwise a data section without mapping symbols
1354
     can pick up a text mapping symbol of a preceeding section.  */
1355
0
  if (!found)
1356
0
    {
1357
0
      n = from_last_map_symbol ? pd->last_map_symbol : info->symtab_pos;
1358
1359
0
      for (; n >= 0; n--)
1360
0
  {
1361
0
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1362
    /* We have searched all possible symbols in the range.  */
1363
0
    if (addr < (info->section ? info->section->vma : 0))
1364
0
      break;
1365
    /* Stop searching once we find the closed mapping symbol.  */
1366
0
    if (riscv_is_valid_mapping_symbol (n, info))
1367
0
      {
1368
0
        symbol = n;
1369
0
        found = true;
1370
0
        break;
1371
0
      }
1372
0
  }
1373
0
    }
1374
1375
0
  if (found)
1376
0
    {
1377
0
      riscv_update_map_state (symbol, &mstate, info);
1378
1379
      /* Find the next mapping symbol to determine the boundary of this mapping
1380
   symbol.  */
1381
1382
0
      bool found_next = false;
1383
      /* Try to found next mapping symbol.  */
1384
0
      for (n = symbol + 1; n < info->symtab_size; n++)
1385
0
  {
1386
0
    if (info->symtab[symbol]->section != info->symtab[n]->section)
1387
0
      continue;
1388
1389
0
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1390
0
    const char *sym_name = bfd_asymbol_name(info->symtab[n]);
1391
0
    if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd'))
1392
0
      {
1393
        /* The next mapping symbol has been found, and it represents the
1394
     boundary of this mapping symbol.  */
1395
0
        found_next = true;
1396
0
        pd->last_map_symbol_boundary = addr;
1397
0
        break;
1398
0
      }
1399
0
  }
1400
1401
      /* No further mapping symbol has been found, indicating that the boundary
1402
   of the current mapping symbol is the end of this section.  */
1403
0
      if (!found_next)
1404
0
  pd->last_map_symbol_boundary = info->section->vma
1405
0
               + info->section->size;
1406
0
    }
1407
1408
  /* Save the information for next use.  */
1409
0
  pd->last_map_symbol = symbol;
1410
0
  pd->last_stop_offset = info->stop_offset;
1411
1412
0
  return mstate;
1413
1.57M
}
1414
1415
/* Decide which data size we should print.  */
1416
1417
static bfd_vma
1418
riscv_data_length (bfd_vma memaddr,
1419
       disassemble_info *info)
1420
0
{
1421
0
  struct riscv_private_data *pd = info->private_data;
1422
0
  bfd_vma length;
1423
0
  bool found = false;
1424
1425
0
  length = 4;
1426
0
  if (info->symtab_size != 0
1427
0
      && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
1428
0
      && pd->last_map_symbol >= 0)
1429
0
    {
1430
0
      int n;
1431
0
      enum riscv_seg_mstate m = MAP_NONE;
1432
0
      for (n = pd->last_map_symbol + 1; n < info->symtab_size; n++)
1433
0
  {
1434
0
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1435
0
    if (addr > memaddr
1436
0
        && riscv_is_valid_mapping_symbol (n, info))
1437
0
      {
1438
0
        if (addr - memaddr < length)
1439
0
    length = addr - memaddr;
1440
0
        found = true;
1441
0
        riscv_update_map_state (n, &m, info);
1442
0
        break;
1443
0
      }
1444
0
  }
1445
0
    }
1446
0
  if (!found)
1447
0
    {
1448
      /* Do not set the length which exceeds the section size.  */
1449
0
      bfd_vma offset = info->section->vma + info->section->size;
1450
0
      offset -= memaddr;
1451
0
      length = (offset < length) ? offset : length;
1452
0
    }
1453
0
  length = length == 3 ? 2 : length;
1454
0
  return length;
1455
0
}
1456
1457
/* Dump the data contents.  */
1458
1459
static int
1460
riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
1461
      insn_t data,
1462
      const bfd_byte *packet ATTRIBUTE_UNUSED,
1463
      disassemble_info *info)
1464
708
{
1465
708
  info->display_endian = info->endian;
1466
708
  int i;
1467
1468
708
  switch (info->bytes_per_chunk)
1469
708
    {
1470
375
    case 1:
1471
375
      info->bytes_per_line = 6;
1472
375
      (*info->fprintf_styled_func)
1473
375
  (info->stream, dis_style_assembler_directive, ".byte");
1474
375
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1475
375
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1476
375
            "0x%02x", (unsigned)data);
1477
375
      break;
1478
116
    case 2:
1479
116
      info->bytes_per_line = 8;
1480
116
      (*info->fprintf_styled_func)
1481
116
  (info->stream, dis_style_assembler_directive, ".short");
1482
116
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1483
116
      (*info->fprintf_styled_func)
1484
116
  (info->stream, dis_style_immediate, "0x%04x", (unsigned) data);
1485
116
      break;
1486
19
    case 4:
1487
19
      info->bytes_per_line = 8;
1488
19
      (*info->fprintf_styled_func)
1489
19
  (info->stream, dis_style_assembler_directive, ".word");
1490
19
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1491
19
      (*info->fprintf_styled_func)
1492
19
  (info->stream, dis_style_immediate, "0x%08lx",
1493
19
   (unsigned long) data);
1494
19
      break;
1495
45
    case 8:
1496
45
      info->bytes_per_line = 8;
1497
45
      (*info->fprintf_styled_func)
1498
45
  (info->stream, dis_style_assembler_directive, ".dword");
1499
45
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1500
45
      (*info->fprintf_styled_func)
1501
45
  (info->stream, dis_style_immediate, "0x%016llx",
1502
45
   (unsigned long long) data);
1503
45
      break;
1504
153
    default:
1505
      /* Arbitrary data so just print the bits in the shape of an .<N>byte
1506
   directive.  */
1507
153
      info->bytes_per_line = info->bytes_per_chunk;
1508
153
      (*info->fprintf_styled_func)
1509
153
  (info->stream, dis_style_assembler_directive, ".%dbyte", info->bytes_per_chunk);
1510
153
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1511
153
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x");
1512
1.16k
      for (i = info->bytes_per_line; i > 0;)
1513
1.01k
  {
1514
1.01k
    i--;
1515
1.01k
    data = bfd_get_bits (packet + i, 8, false);
1516
1.01k
    (*info->fprintf_styled_func)
1517
1.01k
      (info->stream, dis_style_immediate, "%02x",
1518
1.01k
        (unsigned) data);
1519
1.01k
  }
1520
153
      break;
1521
708
    }
1522
708
  return info->bytes_per_chunk;
1523
708
}
1524
1525
static bool
1526
riscv_init_disasm_info (struct disassemble_info *info)
1527
1.14k
{
1528
1.14k
  int i;
1529
1.14k
  struct riscv_private_data *pd =
1530
1.14k
  xcalloc (1, sizeof (struct riscv_private_data));
1531
1.14k
  pd->gp = 0;
1532
1.14k
  pd->print_addr = 0;
1533
37.6k
  for (i = 0; i < (int) ARRAY_SIZE (pd->hi_addr); i++)
1534
36.5k
    pd->hi_addr[i] = -1;
1535
1.14k
  pd->to_print_addr = false;
1536
1537
1.14k
  pd->has_gp = false;
1538
1.14k
  for (i = 0; i < info->symtab_size; i++)
1539
0
    {
1540
0
      asymbol *sym = info->symtab[i];
1541
0
      if (strcmp (bfd_asymbol_name (sym), RISCV_GP_SYMBOL) == 0)
1542
0
  {
1543
0
    pd->gp = bfd_asymbol_value (sym);
1544
0
    pd->has_gp = true;
1545
0
  }
1546
0
    }
1547
1548
1.14k
  pd->xlen = 0;
1549
1.14k
  pd->default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
1550
1.14k
  pd->default_priv_spec = PRIV_SPEC_CLASS_NONE;
1551
1552
1.14k
  pd->riscv_rps_dis.subset_list = xcalloc (1, sizeof (riscv_parse_subset_t));
1553
1.14k
  pd->riscv_rps_dis.error_handler = opcodes_error_handler;
1554
1.14k
  pd->riscv_rps_dis.xlen = &pd->xlen;
1555
1.14k
  pd->riscv_rps_dis.isa_spec = &pd->default_isa_spec;
1556
1.14k
  pd->riscv_rps_dis.check_unknown_prefixed_ext = false;
1557
1.14k
  pd->default_arch = "rv64gc";
1558
1.14k
  if (info->section != NULL)
1559
234
    {
1560
234
      bfd *abfd = info->section->owner;
1561
234
      if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1562
234
  {
1563
234
    const char *sec_name =
1564
234
    get_elf_backend_data (abfd)->obj_attrs_section;
1565
234
    if (bfd_get_section_by_name (abfd, sec_name) != NULL)
1566
0
      {
1567
0
        obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
1568
0
        unsigned int Tag_a = Tag_RISCV_priv_spec;
1569
0
        unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
1570
0
        unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
1571
0
        riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i,
1572
0
                  attr[Tag_b].i,
1573
0
                  attr[Tag_c].i,
1574
0
                  &pd->default_priv_spec);
1575
0
        pd->default_arch = attr[Tag_RISCV_arch].s;
1576
0
      }
1577
234
  }
1578
234
    }
1579
1580
1.14k
  pd->last_map_symbol = -1;
1581
1.14k
  pd->last_stop_offset = 0;
1582
1.14k
  pd->last_map_symbol_boundary = 0;
1583
1.14k
  pd->last_map_state = MAP_NONE;
1584
1.14k
  pd->last_map_section = NULL;
1585
1.14k
  pd->riscv_gpr_names = NULL;
1586
1.14k
  pd->riscv_fpr_names = NULL;
1587
1.14k
  pd->no_aliases = false;
1588
1.14k
  pd->all_ext = false;
1589
1590
1.14k
  info->private_data = pd;
1591
1.14k
  riscv_dis_parse_subset (info, pd->default_arch);
1592
1.14k
  return true;
1593
1.14k
}
1594
1595
/* Fetch an instruction. If only a partial instruction is able to be fetched,
1596
   return the number of accessible bytes.  */
1597
1598
static bfd_vma
1599
fetch_insn (bfd_vma memaddr,
1600
      bfd_byte *packet,
1601
      bfd_vma dump_size,
1602
      struct disassemble_info *info,
1603
      volatile int *status)
1604
3.14M
{
1605
3.14M
  do
1606
3.15M
    {
1607
3.15M
      *status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
1608
3.15M
    }
1609
3.15M
  while (*status != 0 && dump_size-- > 1);
1610
1611
3.14M
  return dump_size;
1612
3.14M
}
1613
1614
int
1615
print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
1616
1.57M
{
1617
1.57M
  bfd_byte packet[RISCV_MAX_INSN_LEN];
1618
1.57M
  insn_t insn = 0;
1619
1.57M
  bfd_vma dump_size, bytes_fetched;
1620
1.57M
  int status;
1621
1.57M
  enum riscv_seg_mstate mstate;
1622
1.57M
  int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *,
1623
1.57M
           struct disassemble_info *);
1624
1625
1.57M
  if (info->private_data == NULL && !riscv_init_disasm_info (info))
1626
0
    return -1;
1627
1628
1.57M
  if (info->disassembler_options != NULL)
1629
0
    {
1630
0
      parse_riscv_dis_options (info->disassembler_options, info);
1631
      /* Avoid repeatedly parsing the options.  */
1632
0
      info->disassembler_options = NULL;
1633
0
    }
1634
1.57M
  else if (((struct riscv_private_data *) info->private_data)->riscv_gpr_names == NULL)
1635
1.14k
    set_default_riscv_dis_options (info);
1636
1637
1.57M
  mstate = riscv_search_mapping_symbol (memaddr, info);
1638
  /* Save the last mapping state.  */
1639
1.57M
  ((struct riscv_private_data *) info->private_data)->last_map_state = mstate;
1640
1641
  /* Set the size to dump.  */
1642
1.57M
  if (mstate == MAP_DATA
1643
883k
      && (info->flags & DISASSEMBLE_DATA) == 0)
1644
0
    {
1645
0
      dump_size = riscv_data_length (memaddr, info);
1646
0
      info->bytes_per_chunk = dump_size;
1647
0
      riscv_disassembler = riscv_disassemble_data;
1648
0
    }
1649
1.57M
  else
1650
1.57M
    {
1651
      /* Get the first 2-bytes to check the lenghth of instruction.  */
1652
1.57M
      bytes_fetched = fetch_insn (memaddr, packet, 2, info, &status);
1653
1.57M
      if (status != 0)
1654
3
  {
1655
3
    (*info->memory_error_func) (status, memaddr, info);
1656
3
    return -1;
1657
3
  }
1658
1.57M
      else if (bytes_fetched != 2)
1659
375
       {
1660
    /* Only the first byte was able to be read.  Dump the partial
1661
       instruction.  */
1662
375
    dump_size = bytes_fetched;
1663
375
    info->bytes_per_chunk = dump_size;
1664
375
    riscv_disassembler = riscv_disassemble_data;
1665
375
    goto print;
1666
375
       }
1667
1.57M
      insn = (insn_t) bfd_getl16 (packet);
1668
1.57M
      dump_size = riscv_insn_length (insn);
1669
1.57M
      riscv_disassembler = riscv_disassemble_insn;
1670
1.57M
    }
1671
1672
1.57M
  bytes_fetched = fetch_insn (memaddr, packet, dump_size, info, &status);
1673
1674
1.57M
  if (status != 0)
1675
0
    {
1676
0
      (*info->memory_error_func) (status, memaddr, info);
1677
0
      return -1;
1678
0
    }
1679
1.57M
  else if (bytes_fetched != dump_size)
1680
333
    {
1681
333
      dump_size = bytes_fetched;
1682
333
      info->bytes_per_chunk = dump_size;
1683
333
      riscv_disassembler = riscv_disassemble_data;
1684
333
    }
1685
1686
1.57M
 print:
1687
1688
1.57M
  insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
1689
1690
1.57M
  return (*riscv_disassembler) (memaddr, insn, packet, info);
1691
1.57M
}
1692
1693
/* Prevent use of the fake labels that are generated as part of the DWARF
1694
   and for relaxable relocations in the assembler.  */
1695
1696
bool
1697
riscv_symbol_is_valid (asymbol * sym,
1698
                       struct disassemble_info * info ATTRIBUTE_UNUSED)
1699
0
{
1700
0
  const char * name;
1701
1702
0
  if (sym == NULL)
1703
0
    return false;
1704
1705
0
  name = bfd_asymbol_name (sym);
1706
1707
0
  return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
1708
0
    && !riscv_elf_is_mapping_symbols (name));
1709
0
}
1710

1711
1712
/* Indices into option argument vector for options accepting an argument.
1713
   Use RISCV_OPTION_ARG_NONE for options accepting no argument.  */
1714
1715
typedef enum
1716
{
1717
  RISCV_OPTION_ARG_NONE = -1,
1718
  RISCV_OPTION_ARG_PRIV_SPEC,
1719
1720
  RISCV_OPTION_ARG_COUNT
1721
} riscv_option_arg_t;
1722
1723
/* Valid RISCV disassembler options.  */
1724
1725
static const struct
1726
{
1727
  const char *name;
1728
  const char *description;
1729
  riscv_option_arg_t arg;
1730
} riscv_options[] =
1731
{
1732
  { "max",
1733
    N_("Disassemble without checking architecture string."),
1734
    RISCV_OPTION_ARG_NONE },
1735
  { "numeric",
1736
    N_("Print numeric register names, rather than ABI names."),
1737
    RISCV_OPTION_ARG_NONE },
1738
  { "no-aliases",
1739
    N_("Disassemble only into canonical instructions."),
1740
    RISCV_OPTION_ARG_NONE },
1741
  { "priv-spec=",
1742
    N_("Print the CSR according to the chosen privilege spec."),
1743
    RISCV_OPTION_ARG_PRIV_SPEC }
1744
};
1745
1746
/* Build the structure representing valid RISCV disassembler options.
1747
   This is done dynamically for maintenance ease purpose; a static
1748
   initializer would be unreadable.  */
1749
1750
const disasm_options_and_args_t *
1751
disassembler_options_riscv (void)
1752
0
{
1753
0
  static disasm_options_and_args_t *opts_and_args;
1754
1755
0
  if (opts_and_args == NULL)
1756
0
    {
1757
0
      size_t num_options = ARRAY_SIZE (riscv_options);
1758
0
      size_t num_args = RISCV_OPTION_ARG_COUNT;
1759
0
      disasm_option_arg_t *args;
1760
0
      disasm_options_t *opts;
1761
0
      size_t i, priv_spec_count;
1762
1763
0
      args = XNEWVEC (disasm_option_arg_t, num_args + 1);
1764
1765
0
      args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC";
1766
0
      priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_EARLIEST;
1767
0
      args[RISCV_OPTION_ARG_PRIV_SPEC].values
1768
0
        = XNEWVEC (const char *, priv_spec_count + 1);
1769
0
      for (i = 0; i < priv_spec_count; i++)
1770
0
  args[RISCV_OPTION_ARG_PRIV_SPEC].values[i]
1771
0
    = riscv_priv_specs[PRIV_SPEC_EARLIEST - PRIV_SPEC_CLASS_NONE - 1 + i].name;
1772
      /* The array we return must be NULL terminated.  */
1773
0
      args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL;
1774
1775
      /* The array we return must be NULL terminated.  */
1776
0
      args[num_args].name = NULL;
1777
0
      args[num_args].values = NULL;
1778
1779
0
      opts_and_args = XNEW (disasm_options_and_args_t);
1780
0
      opts_and_args->args = args;
1781
1782
0
      opts = &opts_and_args->options;
1783
0
      opts->name = XNEWVEC (const char *, num_options + 1);
1784
0
      opts->description = XNEWVEC (const char *, num_options + 1);
1785
0
      opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1);
1786
0
      for (i = 0; i < num_options; i++)
1787
0
  {
1788
0
    opts->name[i] = riscv_options[i].name;
1789
0
    opts->description[i] = _(riscv_options[i].description);
1790
0
    if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE)
1791
0
      opts->arg[i] = &args[riscv_options[i].arg];
1792
0
    else
1793
0
      opts->arg[i] = NULL;
1794
0
  }
1795
      /* The array we return must be NULL terminated.  */
1796
0
      opts->name[i] = NULL;
1797
0
      opts->description[i] = NULL;
1798
0
      opts->arg[i] = NULL;
1799
0
    }
1800
1801
0
  return opts_and_args;
1802
0
}
1803
1804
void
1805
print_riscv_disassembler_options (FILE *stream)
1806
0
{
1807
0
  const disasm_options_and_args_t *opts_and_args;
1808
0
  const disasm_option_arg_t *args;
1809
0
  const disasm_options_t *opts;
1810
0
  size_t max_len = 0;
1811
0
  size_t i;
1812
0
  size_t j;
1813
1814
0
  opts_and_args = disassembler_options_riscv ();
1815
0
  opts = &opts_and_args->options;
1816
0
  args = opts_and_args->args;
1817
1818
0
  fprintf (stream, _("\n\
1819
0
The following RISC-V specific disassembler options are supported for use\n\
1820
0
with the -M switch (multiple options should be separated by commas):\n"));
1821
0
  fprintf (stream, "\n");
1822
1823
  /* Compute the length of the longest option name.  */
1824
0
  for (i = 0; opts->name[i] != NULL; i++)
1825
0
    {
1826
0
      size_t len = strlen (opts->name[i]);
1827
1828
0
      if (opts->arg[i] != NULL)
1829
0
  len += strlen (opts->arg[i]->name);
1830
0
      if (max_len < len)
1831
0
  max_len = len;
1832
0
    }
1833
1834
0
  for (i = 0, max_len++; opts->name[i] != NULL; i++)
1835
0
    {
1836
0
      fprintf (stream, "  %s", opts->name[i]);
1837
0
      if (opts->arg[i] != NULL)
1838
0
  fprintf (stream, "%s", opts->arg[i]->name);
1839
0
      if (opts->description[i] != NULL)
1840
0
  {
1841
0
    size_t len = strlen (opts->name[i]);
1842
1843
0
    if (opts->arg != NULL && opts->arg[i] != NULL)
1844
0
      len += strlen (opts->arg[i]->name);
1845
0
    fprintf (stream, "%*c %s", (int) (max_len - len), ' ',
1846
0
                   opts->description[i]);
1847
0
  }
1848
0
      fprintf (stream, "\n");
1849
0
    }
1850
1851
0
  for (i = 0; args[i].name != NULL; i++)
1852
0
    {
1853
0
      if (args[i].values == NULL)
1854
0
  continue;
1855
0
      fprintf (stream, _("\n\
1856
0
  For the options above, the following values are supported for \"%s\":\n   "),
1857
0
         args[i].name);
1858
0
      for (j = 0; args[i].values[j] != NULL; j++)
1859
0
  fprintf (stream, " %s", args[i].values[j]);
1860
0
      fprintf (stream, _("\n"));
1861
0
    }
1862
1863
0
  fprintf (stream, _("\n"));
1864
0
}
1865
1866
void disassemble_free_riscv (struct disassemble_info *info ATTRIBUTE_UNUSED)
1867
1.19k
{
1868
1.19k
  struct riscv_private_data *pd = info->private_data;
1869
1.19k
  if (pd)
1870
1.14k
    {
1871
1.14k
      riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
1872
1.14k
      free (pd->riscv_rps_dis.subset_list);
1873
1.14k
    }
1874
1.19k
}