Coverage Report

Created: 2026-09-14 08:07

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.55k
{
82
1.55k
  struct riscv_private_data *pd = info->private_data;
83
1.55k
  pd->riscv_gpr_names = riscv_gpr_names_abi;
84
1.55k
  pd->riscv_fpr_names = riscv_fpr_names_abi;
85
1.55k
  pd->no_aliases = false;
86
1.55k
  pd->all_ext = false;
87
1.55k
}
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
18.0k
{
207
18.0k
  const char *s = val >= size || array[val] == NULL ? "unknown" : array[val];
208
18.0k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, "%s", s);
209
18.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
125k
{
217
125k
  if (pd->hi_addr[base_reg] != (bfd_vma)-1)
218
14.6k
    {
219
14.6k
      pd->print_addr = (base_reg != 0 ? pd->hi_addr[base_reg] : 0) + offset;
220
14.6k
      pd->hi_addr[base_reg] = -1;
221
14.6k
    }
222
110k
  else if (base_reg == X_GP && pd->has_gp)
223
0
    pd->print_addr = pd->gp + offset;
224
110k
  else if (base_reg == X_TP || base_reg == 0)
225
11.5k
    pd->print_addr = offset;
226
99.0k
  else
227
99.0k
    return;  /* Don't print the address.  */
228
26.1k
  pd->to_print_addr = true;
229
230
  /* Sign-extend a 32-bit value to a 64-bit value.  */
231
26.1k
  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
26.1k
  if (pd->xlen == 32)
236
0
    pd->print_addr = (bfd_vma)(uint32_t)pd->print_addr;
237
26.1k
}
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.37M
{
321
1.37M
  struct riscv_private_data *pd = info->private_data;
322
1.37M
  int rs1 = (l >> OP_SH_RS1) & OP_MASK_RS1;
323
1.37M
  int rd = (l >> OP_SH_RD) & OP_MASK_RD;
324
1.37M
  fprintf_styled_ftype print = info->fprintf_styled_func;
325
1.37M
  const char *opargStart;
326
327
1.37M
  if (*oparg != '\0')
328
1.10M
    print (info->stream, dis_style_text, "\t");
329
330
6.83M
  for (; *oparg != '\0'; oparg++)
331
5.46M
    {
332
5.46M
      opargStart = oparg;
333
5.46M
      switch (*oparg)
334
5.46M
  {
335
2.37M
  case 'C': /* RVC */
336
2.37M
    switch (*++oparg)
337
2.37M
      {
338
362k
      case 's': /* RS1 x8-x15.  */
339
385k
      case 'w': /* RS1 x8-x15.  */
340
385k
        print (info->stream, dis_style_register, "%s",
341
385k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS1S, l) + 8]);
342
385k
        break;
343
331k
      case 't': /* RS2 x8-x15.  */
344
331k
      case 'x': /* RS2 x8-x15.  */
345
331k
        print (info->stream, dis_style_register, "%s",
346
331k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
347
331k
        break;
348
150k
      case 'U': /* RS1, constrained to equal RD.  */
349
150k
        print (info->stream, dis_style_register,
350
150k
         "%s", pd->riscv_gpr_names[rd]);
351
150k
        break;
352
340k
      case 'c': /* RS1, constrained to equal sp.  */
353
340k
        print (info->stream, dis_style_register, "%s",
354
340k
         pd->riscv_gpr_names[X_SP]);
355
340k
        break;
356
82.9k
      case 'V': /* RS2 */
357
82.9k
        print (info->stream, dis_style_register, "%s",
358
82.9k
         pd->riscv_gpr_names[EXTRACT_OPERAND (CRS2, l)]);
359
82.9k
        break;
360
73.6k
      case 'o':
361
147k
      case 'j':
362
147k
        if (((l & MASK_C_ADDI) == MATCH_C_ADDI) && rd != 0)
363
54.4k
    maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 0);
364
147k
        if (pd->xlen == 64
365
147k
      && ((l & MASK_C_ADDIW) == MATCH_C_ADDIW) && rd != 0)
366
37.2k
    maybe_print_address (pd, rd, EXTRACT_CITYPE_IMM (l), 1);
367
147k
        print (info->stream, dis_style_immediate, "%d",
368
147k
         (int)EXTRACT_CITYPE_IMM (l));
369
147k
        break;
370
73.5k
      case 'k':
371
73.5k
        print (info->stream, dis_style_address_offset, "%d",
372
73.5k
         (int)EXTRACT_CLTYPE_LW_IMM (l));
373
73.5k
        break;
374
206k
      case 'l':
375
206k
        print (info->stream, dis_style_address_offset, "%d",
376
206k
         (int)EXTRACT_CLTYPE_LD_IMM (l));
377
206k
        break;
378
32.2k
      case 'm':
379
32.2k
        print (info->stream, dis_style_address_offset, "%d",
380
32.2k
         (int)EXTRACT_CITYPE_LWSP_IMM (l));
381
32.2k
        break;
382
67.3k
      case 'n':
383
67.3k
        print (info->stream, dis_style_address_offset, "%d",
384
67.3k
         (int)EXTRACT_CITYPE_LDSP_IMM (l));
385
67.3k
        break;
386
146k
      case 'K':
387
146k
        print (info->stream, dis_style_immediate, "%d",
388
146k
         (int)EXTRACT_CIWTYPE_ADDI4SPN_IMM (l));
389
146k
        break;
390
8.69k
      case 'L':
391
8.69k
        print (info->stream, dis_style_immediate, "%d",
392
8.69k
         (int)EXTRACT_CITYPE_ADDI16SP_IMM (l));
393
8.69k
        break;
394
28.9k
      case 'M':
395
28.9k
        print (info->stream, dis_style_address_offset, "%d",
396
28.9k
         (int)EXTRACT_CSSTYPE_SWSP_IMM (l));
397
28.9k
        break;
398
48.8k
      case 'N':
399
48.8k
        print (info->stream, dis_style_address_offset, "%d",
400
48.8k
         (int)EXTRACT_CSSTYPE_SDSP_IMM (l));
401
48.8k
        break;
402
56.0k
      case 'p':
403
56.0k
        info->target = EXTRACT_CBTYPE_IMM (l) + pc;
404
56.0k
        (*info->print_address_func) (info->target, info);
405
56.0k
        break;
406
21.4k
      case 'a':
407
21.4k
        info->target = EXTRACT_CJTYPE_IMM (l) + pc;
408
21.4k
        (*info->print_address_func) (info->target, info);
409
21.4k
        break;
410
41.4k
      case 'u':
411
41.4k
        print (info->stream, dis_style_immediate, "0x%x",
412
41.4k
         (unsigned)(EXTRACT_CITYPE_IMM (l) & (RISCV_BIGIMM_REACH-1)));
413
41.4k
        break;
414
91.8k
      case '>':
415
91.8k
        print (info->stream, dis_style_immediate, "0x%x",
416
91.8k
         (unsigned)EXTRACT_CITYPE_IMM (l) & 0x3f);
417
91.8k
        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
18.3k
      case 'T': /* Floating-point RS2.  */
423
18.3k
        print (info->stream, dis_style_register, "%s",
424
18.3k
         pd->riscv_fpr_names[EXTRACT_OPERAND (CRS2, l)]);
425
18.3k
        break;
426
99.3k
      case 'D': /* Floating-point RS2 x8-x15.  */
427
99.3k
        print (info->stream, dis_style_register, "%s",
428
99.3k
         pd->riscv_fpr_names[EXTRACT_OPERAND (CRS2S, l) + 8]);
429
99.3k
        break;
430
2.37M
      }
431
2.37M
    break;
432
433
2.37M
  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.44M
  case ',':
538
1.92M
  case '(':
539
2.41M
  case ')':
540
2.41M
  case '[':
541
2.41M
  case ']':
542
2.41M
  case '{':
543
2.41M
  case '}':
544
2.41M
    print (info->stream, dis_style_text, "%c", *oparg);
545
2.41M
    break;
546
547
1.22k
  case '0':
548
    /* Only print constant 0 if it is the last argument.  */
549
1.22k
    if (!oparg[1])
550
0
      print (info->stream, dis_style_immediate, "0");
551
1.22k
    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
48.1k
  case 's':
559
48.1k
    if ((l & MASK_JALR) == MATCH_JALR)
560
3.01k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
561
48.1k
    print (info->stream, dis_style_register, "%s",
562
48.1k
     pd->riscv_gpr_names[rs1]);
563
48.1k
    break;
564
565
17.8k
  case 't':
566
17.8k
    print (info->stream, dis_style_register, "%s",
567
17.8k
     pd->riscv_gpr_names[EXTRACT_OPERAND (RS2, l)]);
568
17.8k
    break;
569
570
16.6k
  case 'u':
571
16.6k
    print (info->stream, dis_style_immediate, "0x%x",
572
16.6k
     (unsigned)EXTRACT_UTYPE_IMM (l) >> RISCV_IMM_BITS);
573
16.6k
    break;
574
575
18.0k
  case 'm':
576
18.0k
    if (EXTRACT_OPERAND (RM, l) == OP_MASK_RM)
577
1.56k
      break;
578
16.4k
    print (info->stream, dis_style_text, ",");
579
    /* Fall through. */
580
16.4k
  case 'M':
581
16.4k
    arg_print (info, EXTRACT_OPERAND (RM, l),
582
16.4k
         riscv_rm, ARRAY_SIZE (riscv_rm));
583
16.4k
    break;
584
585
793
  case 'P':
586
793
    arg_print (info, EXTRACT_OPERAND (PRED, l),
587
793
         riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
588
793
    break;
589
590
793
  case 'Q':
591
793
    arg_print (info, EXTRACT_OPERAND (SUCC, l),
592
793
         riscv_pred_succ, ARRAY_SIZE (riscv_pred_succ));
593
793
    break;
594
595
15.3k
  case 'o':
596
15.3k
    maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
597
    /* Fall through.  */
598
22.3k
  case 'j':
599
22.3k
    if (((l & MASK_ADDI) == MATCH_ADDI && rs1 != 0)
600
21.0k
        || (l & MASK_JALR) == MATCH_JALR)
601
3.45k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 0);
602
22.3k
    if (pd->xlen == 64
603
22.3k
        && ((l & MASK_ADDIW) == MATCH_ADDIW) && rs1 != 0)
604
2.09k
      maybe_print_address (pd, rs1, EXTRACT_ITYPE_IMM (l), 1);
605
22.3k
    print (info->stream, dis_style_immediate, "%d",
606
22.3k
     (int)EXTRACT_ITYPE_IMM (l));
607
22.3k
    break;
608
609
9.72k
  case 'q':
610
9.72k
    maybe_print_address (pd, rs1, EXTRACT_STYPE_IMM (l), 0);
611
9.72k
    print (info->stream, dis_style_address_offset, "%d",
612
9.72k
     (int)EXTRACT_STYPE_IMM (l));
613
9.72k
    break;
614
615
9.39k
  case 'a':
616
9.39k
    info->target = EXTRACT_JTYPE_IMM (l) + pc;
617
9.39k
    (*info->print_address_func) (info->target, info);
618
9.39k
    break;
619
620
8.74k
  case 'p':
621
8.74k
    info->target = EXTRACT_BTYPE_IMM (l) + pc;
622
8.74k
    (*info->print_address_func) (info->target, info);
623
8.74k
    break;
624
625
392k
  case 'd':
626
392k
    if ((l & MASK_AUIPC) == MATCH_AUIPC)
627
7.37k
      pd->hi_addr[rd] = pc + EXTRACT_UTYPE_IMM (l);
628
384k
    else if ((l & MASK_LUI) == MATCH_LUI)
629
9.25k
      pd->hi_addr[rd] = EXTRACT_UTYPE_IMM (l);
630
375k
    else if ((l & MASK_C_LUI) == MATCH_C_LUI)
631
41.4k
      pd->hi_addr[rd] = EXTRACT_CITYPE_LUI_IMM (l);
632
392k
    print (info->stream, dis_style_register, "%s",
633
392k
     pd->riscv_gpr_names[rd]);
634
392k
    break;
635
636
0
  case 'y':
637
0
    print (info->stream, dis_style_immediate, "0x%x",
638
0
     EXTRACT_OPERAND (BS, l));
639
0
    break;
640
641
0
  case 'z':
642
0
    print (info->stream, dis_style_register, "%s",
643
0
     pd->riscv_gpr_names[0]);
644
0
    break;
645
646
204
  case '>':
647
204
    print (info->stream, dis_style_immediate, "0x%x",
648
204
     EXTRACT_OPERAND (SHAMT, l));
649
204
    break;
650
651
439
  case '<':
652
439
    print (info->stream, dis_style_immediate, "0x%x",
653
439
     EXTRACT_OPERAND (SHAMTW, l));
654
439
    break;
655
656
18.1k
  case 'S':
657
18.2k
  case 'U':
658
18.2k
    print (info->stream, dis_style_register, "%s",
659
18.2k
     pd->riscv_fpr_names[rs1]);
660
18.2k
    break;
661
662
21.5k
  case 'T':
663
21.5k
    print (info->stream, dis_style_register, "%s",
664
21.5k
     pd->riscv_fpr_names[EXTRACT_OPERAND (RS2, l)]);
665
21.5k
    break;
666
667
50.3k
  case 'D':
668
50.3k
    print (info->stream, dis_style_register, "%s",
669
50.3k
     pd->riscv_fpr_names[rd]);
670
50.3k
    break;
671
672
16.7k
  case 'R':
673
16.7k
    print (info->stream, dis_style_register, "%s",
674
16.7k
     pd->riscv_fpr_names[EXTRACT_OPERAND (RS3, l)]);
675
16.7k
    break;
676
677
8.94k
  case 'E':
678
8.94k
    {
679
8.94k
      static const char *riscv_csr_hash[4096]; /* Total 2^12 CSRs.  */
680
8.94k
      static bool init_csr = false;
681
8.94k
      unsigned int csr = EXTRACT_OPERAND (CSR, l);
682
683
8.94k
      if (!init_csr)
684
8.94k
        {
685
8.94k
    unsigned int i;
686
36.6M
    for (i = 0; i < 4096; i++)
687
36.6M
      riscv_csr_hash[i] = NULL;
688
689
    /* Set to the newest privileged version.  */
690
8.94k
    if (pd->default_priv_spec == PRIV_SPEC_CLASS_NONE)
691
831
      pd->default_priv_spec = PRIV_SPEC_CLASS_DRAFT - 1;
692
693
8.94k
#define DECLARE_CSR(name, num, class, define_version, abort_version)  \
694
4.16M
    if (riscv_csr_hash[num] == NULL      \
695
4.16M
        && ((define_version == PRIV_SPEC_CLASS_NONE  \
696
4.03M
       && abort_version == PRIV_SPEC_CLASS_NONE) \
697
4.03M
      || (pd->default_priv_spec >= define_version  \
698
2.56M
          && pd->default_priv_spec < abort_version))) \
699
4.16M
      riscv_csr_hash[num] = #name;
700
8.94k
#define DECLARE_CSR_ALIAS(name, num, class, define_version, abort_version) \
701
134k
    DECLARE_CSR (name, num, class, define_version, abort_version)
702
8.94k
#include "opcode/riscv-opc.h"
703
8.94k
#undef DECLARE_CSR
704
8.94k
        }
705
706
8.94k
      if (riscv_csr_hash[csr] != NULL)
707
2.06k
        if (riscv_subset_supports (&pd->riscv_rps_dis, "xtheadvector")
708
0
      && (csr == CSR_VSTART
709
0
          || csr == CSR_VXSAT
710
0
          || csr == CSR_VXRM
711
0
          || csr == CSR_VL
712
0
          || csr == CSR_VTYPE
713
0
          || csr == CSR_VLENB))
714
0
    print (info->stream, dis_style_register, "%s",
715
0
           concat ("th.", riscv_csr_hash[csr], NULL));
716
2.06k
        else
717
2.06k
    print (info->stream, dis_style_register, "%s",
718
2.06k
           riscv_csr_hash[csr]);
719
6.87k
      else
720
6.87k
        print (info->stream, dis_style_immediate, "0x%x", csr);
721
8.94k
      break;
722
18.1k
    }
723
724
0
  case 'Y':
725
0
    print (info->stream, dis_style_immediate, "0x%x",
726
0
     EXTRACT_OPERAND (RNUM, l));
727
0
    break;
728
729
6.23k
  case 'Z':
730
6.23k
    print (info->stream, dis_style_immediate, "%d", rs1);
731
6.23k
    break;
732
733
0
  case 'W': /* Various operands for standard z extensions.  */
734
0
    switch (*++oparg)
735
0
      {
736
0
      case 'i':
737
0
        switch (*++oparg)
738
0
    {
739
0
    case 'f':
740
0
      print (info->stream, dis_style_address_offset, "%d",
741
0
       (int) EXTRACT_STYPE_IMM (l));
742
0
      break;
743
0
    default:
744
0
      goto undefined_modifier;
745
0
    }
746
0
        break;
747
0
      case 'f':
748
0
        switch (*++oparg)
749
0
    {
750
0
    case 'v':
751
0
      if (riscv_fli_symval[rs1])
752
0
        print (info->stream, dis_style_text, "%s",
753
0
         riscv_fli_symval[rs1]);
754
0
      else
755
0
        print (info->stream, dis_style_immediate, "%a",
756
0
         riscv_fli_numval[rs1]);
757
0
      break;
758
0
    default:
759
0
      goto undefined_modifier;
760
0
    }
761
0
        break;
762
0
      case 'c': /* Zcb extension 16 bits length instruction fields. */
763
0
        switch (*++oparg)
764
0
    {
765
0
    case '1':
766
0
        print (info->stream, dis_style_register, "%s",
767
0
          pd->riscv_gpr_names[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG1, l))]);
768
0
        break;
769
0
    case '2':
770
0
        print (info->stream, dis_style_register, "%s",
771
0
          pd->riscv_gpr_names[riscv_zcmp_get_sregno (EXTRACT_OPERAND (SREG2, l))]);
772
0
        break;
773
0
    case 'b':
774
0
      print (info->stream, dis_style_immediate, "%d",
775
0
       (int)EXTRACT_ZCB_BYTE_UIMM (l));
776
0
      break;
777
0
    case 'h':
778
0
      print (info->stream, dis_style_immediate, "%d",
779
0
       (int)EXTRACT_ZCB_HALFWORD_UIMM (l));
780
0
      break;
781
0
    case 'r':
782
0
      print_reg_list (info, l);
783
0
      break;
784
0
    case 'p':
785
0
      print (info->stream, dis_style_immediate, "%d",
786
0
       riscv_get_spimm (l, pd->xlen));
787
0
      break;
788
0
    case 'i':
789
0
    case 'I':
790
0
      print (info->stream, dis_style_address_offset,
791
0
       "%" PRIu64, EXTRACT_ZCMT_INDEX (l));
792
0
      break;
793
0
    default:
794
0
      goto undefined_modifier;
795
0
    }
796
0
        break;
797
0
      default:
798
0
        goto undefined_modifier;
799
0
      }
800
0
    break;
801
802
0
  case 'X': /* Vendor-specific operands.  */
803
0
    switch (*++oparg)
804
0
      {
805
0
      case 't': /* Vendor-specific (T-head) operands.  */
806
0
        {
807
0
    size_t n;
808
0
    size_t s;
809
0
    bool sign;
810
0
    switch (*++oparg)
811
0
      {
812
0
      case 'V':
813
0
       ++oparg;
814
0
       if (*oparg != 'c')
815
0
          goto undefined_modifier;
816
817
0
        int imm = (*oparg == 'b') ? EXTRACT_RVV_VB_IMM (l)
818
0
                : EXTRACT_RVV_VC_IMM (l);
819
0
        unsigned int imm_vediv = EXTRACT_OPERAND (XTHEADVEDIV, imm);
820
0
        unsigned int imm_vlmul = EXTRACT_OPERAND (XTHEADVLMUL, imm);
821
0
        unsigned int imm_vsew = EXTRACT_OPERAND (XTHEADVSEW, imm);
822
0
        unsigned int imm_vtype_res
823
0
          = EXTRACT_OPERAND (XTHEADVTYPE_RES, imm);
824
0
        if (imm_vsew < ARRAY_SIZE (riscv_vsew)
825
0
      && imm_vlmul < ARRAY_SIZE (riscv_th_vlen)
826
0
      && imm_vediv < ARRAY_SIZE (riscv_th_vediv)
827
0
      && ! imm_vtype_res)
828
0
          print (info->stream, dis_style_text, "%s,%s,%s",
829
0
           riscv_vsew[imm_vsew], riscv_th_vlen[imm_vlmul],
830
0
           riscv_th_vediv[imm_vediv]);
831
0
        else
832
0
          print (info->stream, dis_style_immediate, "%d", imm);
833
0
        break;
834
0
      case 'l': /* Integer immediate, literal.  */
835
0
        oparg++;
836
0
        while (*oparg && *oparg != ',')
837
0
          {
838
0
      print (info->stream, dis_style_immediate, "%c", *oparg);
839
0
      oparg++;
840
0
          }
841
0
        oparg--;
842
0
        break;
843
0
      case 's': /* Integer immediate, 'XsN@S' ... N-bit signed immediate at bit S.  */
844
0
        sign = true;
845
0
        goto print_imm;
846
0
      case 'u': /* Integer immediate, 'XuN@S' ... N-bit unsigned immediate at bit S.  */
847
0
        sign = false;
848
0
        goto print_imm;
849
0
      print_imm:
850
0
        n = strtol (oparg + 1, (char **)&oparg, 10);
851
0
        if (*oparg != '@')
852
0
          goto undefined_modifier;
853
0
        s = strtol (oparg + 1, (char **)&oparg, 10);
854
0
        oparg--;
855
856
0
        if (!sign)
857
0
          print (info->stream, dis_style_immediate, "%lu",
858
0
           (unsigned long)EXTRACT_U_IMM (n, s, l));
859
0
        else
860
0
          print (info->stream, dis_style_immediate, "%li",
861
0
           (signed long)EXTRACT_S_IMM (n, s, l));
862
0
        break;
863
0
      default:
864
0
        goto undefined_modifier;
865
0
      }
866
0
        }
867
0
        break;
868
0
      case 'c': /* Vendor-specific (CORE-V) operands.  */
869
0
        switch (*++oparg)
870
0
    {
871
0
      case '2':
872
0
        print (info->stream, dis_style_immediate, "%d",
873
0
      ((int) EXTRACT_CV_IS2_UIMM5 (l)));
874
0
        break;
875
0
      case '3':
876
0
        print (info->stream, dis_style_immediate, "%d",
877
0
      ((int) EXTRACT_CV_IS3_UIMM5 (l)));
878
0
        break;
879
0
      case '4':
880
0
        print (info->stream, dis_style_immediate, "%d",
881
0
         ((int) EXTRACT_CV_BI_IMM5 (l)));
882
0
        break;
883
0
      case '5':
884
0
        print (info->stream, dis_style_immediate, "%d",
885
0
         ((int) EXTRACT_CV_SIMD_IMM6 (l)));
886
0
        break;
887
0
      case '6':
888
0
        print (info->stream, dis_style_immediate, "%d",
889
0
         ((int) EXTRACT_CV_BITMANIP_UIMM5 (l)));
890
0
        break;
891
0
      case '7':
892
0
        print (info->stream, dis_style_immediate, "%d",
893
0
         ((int) EXTRACT_CV_BITMANIP_UIMM2 (l)));
894
0
        break;
895
0
      case '8':
896
0
        print (info->stream, dis_style_immediate, "%d",
897
0
         ((int) EXTRACT_CV_SIMD_UIMM6 (l)));
898
0
        ++oparg;
899
0
        break;
900
0
      default:
901
0
        goto undefined_modifier;
902
0
    }
903
0
        break;
904
0
      case 's': /* Vendor-specific (SiFive) operands.  */
905
0
        switch (*++oparg)
906
0
    {
907
    /* SiFive vector coprocessor interface.  */
908
0
    case 'd':
909
0
      print (info->stream, dis_style_register, "0x%x",
910
0
       (unsigned) EXTRACT_OPERAND (RD, l));
911
0
      break;
912
0
    case 't':
913
0
      print (info->stream, dis_style_register, "0x%x",
914
0
       (unsigned) EXTRACT_OPERAND (RS2, l));
915
0
      break;
916
0
    case 'O':
917
0
      switch (*++oparg)
918
0
        {
919
0
        case '2':
920
0
          print (info->stream, dis_style_register, "0x%x",
921
0
           (unsigned) EXTRACT_OPERAND (XSO2, l));
922
0
          break;
923
0
        case '1':
924
0
          print (info->stream, dis_style_register, "0x%x",
925
0
           (unsigned) EXTRACT_OPERAND (XSO1, l));
926
0
          break;
927
0
        }
928
0
      break;
929
0
    }
930
0
        break;
931
0
      case 'm': /* Vendor-specific (MIPS) operands.  */
932
0
        switch (*++oparg)
933
0
    {
934
0
    case '@':
935
0
      print (info->stream, dis_style_register, "0x%x",
936
0
       (unsigned) EXTRACT_OPERAND (MIPS_HINT, l));
937
0
      break;
938
0
    case '#':
939
0
      print (info->stream, dis_style_register, "0x%x",
940
0
       (unsigned) EXTRACT_OPERAND (MIPS_IMM9, l));
941
0
      break;
942
0
    case '$':
943
0
      print (info->stream, dis_style_immediate, "%d",
944
0
       (unsigned)EXTRACT_MIPS_LDP_IMM (l));
945
0
      break;
946
0
    case '%':
947
0
      print (info->stream, dis_style_immediate, "%d",
948
0
       (unsigned)EXTRACT_MIPS_LWP_IMM (l));
949
0
      break;
950
0
    case '^':
951
0
      print (info->stream, dis_style_immediate, "%d",
952
0
       (unsigned)EXTRACT_MIPS_SDP_IMM (l));
953
0
      break;
954
0
    case '&':
955
0
      print (info->stream, dis_style_immediate, "%d",
956
0
       (unsigned)EXTRACT_MIPS_SWP_IMM (l));
957
0
      break;
958
0
    default:
959
0
      goto undefined_modifier;
960
0
    }
961
0
        break;
962
0
      case 'p': /* Vendor-specific (SpacemiT) operands.  */
963
0
        switch (*++oparg)
964
0
    {
965
0
    case 'V':
966
0
      switch (*++oparg)
967
0
        {
968
0
        case 'd':
969
0
          {
970
0
      unsigned vd = EXTRACT_OPERAND (SPACEMIT_IME_VD, l) * 2;
971
0
      print (info->stream, dis_style_register, "%s",
972
0
             riscv_vecr_names_numeric[vd]);
973
0
          }
974
0
          break;
975
0
        case 's':
976
0
          {
977
0
      unsigned vs = EXTRACT_OPERAND (SPACEMIT_IME_VS1, l) * 2;
978
0
      print (info->stream, dis_style_register, "%s",
979
0
             riscv_vecr_names_numeric[vs]);
980
0
          }
981
0
          break;
982
0
        case 'm':
983
0
          {
984
0
      unsigned vm = EXTRACT_OPERAND (SPACEMIT_IME_VMASK, l);
985
0
      print (info->stream, dis_style_register, "%s",
986
0
             riscv_vecr_names_numeric[vm]);
987
0
          }
988
0
          break;
989
0
        default:
990
0
          goto undefined_modifier;
991
0
        }
992
0
      break;
993
0
    case 'n': /* Xpn/Xpb: uimm2 stride, encoded across bits 7,15.  */
994
0
    case 'b':
995
0
      {
996
0
        unsigned sp = ((l >> 7) & 1) | (((l >> 15) & 1) << 1);
997
0
        print (info->stream, dis_style_immediate, "%u", sp);
998
0
      }
999
0
      break;
1000
0
    case 'u': /* XpuN@S: N-bit unsigned immediate at bit S.  */
1001
0
      {
1002
0
        long n = strtol (oparg + 1, (char **)&oparg, 10);
1003
0
        if (*oparg != '@')
1004
0
          goto undefined_modifier;
1005
0
        long s = strtol (oparg + 1, (char **)&oparg, 10);
1006
0
        oparg--;
1007
0
        unsigned val = (l >> s) & ((1U << n) - 1);
1008
0
        print (info->stream, dis_style_immediate, "%u", val);
1009
0
      }
1010
0
      break;
1011
0
    case 'w':
1012
      /* Xpw: optional data-width suffix, i8 only.  WI==3 (i8)
1013
         is the default and is omitted from the output.  */
1014
0
      {
1015
0
        unsigned wi = EXTRACT_OPERAND (SPACEMIT_IME_WI, l);
1016
0
        if (wi != 3)
1017
0
          goto undefined_modifier;
1018
0
      }
1019
0
      break;
1020
0
    case 'x':
1021
      /* Xpx: optional data-width suffix, i4 or i8.  WI==3 (i8)
1022
         is the default and is omitted from the output.  */
1023
0
      {
1024
0
        unsigned wi = EXTRACT_OPERAND (SPACEMIT_IME_WI, l);
1025
0
        if (wi == 2)
1026
0
          print (info->stream, dis_style_text, ",i4");
1027
0
        else if (wi != 3)
1028
0
          goto undefined_modifier;
1029
0
      }
1030
0
      break;
1031
0
    default:
1032
0
      goto undefined_modifier;
1033
0
    }
1034
0
        break;
1035
0
      default:
1036
0
        goto undefined_modifier;
1037
0
      }
1038
0
    break;
1039
1040
0
  default:
1041
0
  undefined_modifier:
1042
    /* xgettext:c-format */
1043
0
    print (info->stream, dis_style_text,
1044
0
     _("# internal error, undefined modifier (%c)"),
1045
0
     *opargStart);
1046
0
    return;
1047
5.46M
  }
1048
5.46M
    }
1049
1.37M
}
1050
1051
/* Print the RISC-V instruction at address MEMADDR in debugged memory,
1052
   on using INFO.  Returns length of the instruction, in bytes.
1053
   BIGENDIAN must be 1 if this is big-endian code, 0 if
1054
   this is little-endian code.  */
1055
1056
static int
1057
riscv_disassemble_insn (bfd_vma memaddr,
1058
      insn_t word,
1059
      const bfd_byte *packet,
1060
      disassemble_info *info)
1061
1.74M
{
1062
1.74M
  const struct riscv_opcode *op;
1063
1.74M
  static bool init = false;
1064
1.74M
  static const struct riscv_opcode *riscv_hash[OP_MASK_OP + 1];
1065
1.74M
  struct riscv_private_data *pd = info->private_data;
1066
1.74M
  int insnlen, i;
1067
1.74M
  bool printed;
1068
1069
1.75M
#define OP_HASH_IDX(i) ((i) & (riscv_insn_length (i) == 2 ? 0x3 : OP_MASK_OP))
1070
1071
  /* Build a hash table to shorten the search time.  */
1072
1.74M
  if (! init)
1073
2
    {
1074
5.84k
      for (op = riscv_opcodes; op->name; op++)
1075
5.84k
  if (!riscv_hash[OP_HASH_IDX (op->match)])
1076
60
    riscv_hash[OP_HASH_IDX (op->match)] = op;
1077
1078
2
      init = true;
1079
2
    }
1080
1081
1.74M
  insnlen = riscv_insn_length (word);
1082
1083
  /* RISC-V instructions are always little-endian.  */
1084
1.74M
  info->endian_code = BFD_ENDIAN_LITTLE;
1085
1086
1.74M
  info->bytes_per_chunk = insnlen % 4 == 0 ? 4 : 2;
1087
1.74M
  info->bytes_per_line = 8;
1088
  /* We don't support constant pools, so this must be code.  */
1089
1.74M
  info->display_endian = info->endian_code;
1090
1.74M
  info->insn_info_valid = 1;
1091
1.74M
  info->branch_delay_insns = 0;
1092
1.74M
  info->data_size = 0;
1093
1.74M
  info->insn_type = dis_nonbranch;
1094
1.74M
  info->target = 0;
1095
1.74M
  info->target2 = 0;
1096
1097
1.74M
  op = riscv_hash[OP_HASH_IDX (word)];
1098
1.74M
  if (op != NULL)
1099
1.69M
    {
1100
      /* If XLEN is not known, get its value from the ELF class.  */
1101
1.69M
      if (pd->xlen != 0)
1102
1.69M
  ;
1103
0
      else if (info->mach == bfd_mach_riscv64)
1104
0
  pd->xlen = 64;
1105
0
      else if (info->mach == bfd_mach_riscv32)
1106
0
  pd->xlen = 32;
1107
0
      else if (info->section != NULL)
1108
0
  {
1109
0
    Elf_Internal_Ehdr *ehdr = elf_elfheader (info->section->owner);
1110
0
    pd->xlen = ehdr->e_ident[EI_CLASS] == ELFCLASS64 ? 64 : 32;
1111
0
  }
1112
1113
      /* If arch has the Zfinx extension, replace FPR with GPR.  */
1114
1.69M
      if (riscv_subset_supports (&pd->riscv_rps_dis, "zfinx"))
1115
0
  pd->riscv_fpr_names = pd->riscv_gpr_names;
1116
1.69M
      else
1117
1.69M
  pd->riscv_fpr_names = pd->riscv_gpr_names == riscv_gpr_names_abi ?
1118
1.69M
            riscv_fpr_names_abi : riscv_fpr_names_numeric;
1119
1120
1.03G
      for (; op->name; op++)
1121
1.03G
  {
1122
    /* Ignore macro insns.  */
1123
1.03G
    if (op->pinfo == INSN_MACRO)
1124
25.5M
      continue;
1125
    /* Does the opcode match?  */
1126
1.00G
    if (! (op->match_func) (op, word))
1127
1.00G
      continue;
1128
    /* Is this a pseudo-instruction and may we print it as such?  */
1129
1.55M
    if (pd->no_aliases && (op->pinfo & INSN_ALIAS))
1130
0
      continue;
1131
    /* Is this instruction restricted to a certain value of XLEN?  */
1132
1.55M
    if ((op->xlen_requirement != 0)
1133
270k
        && (op->xlen_requirement != pd->xlen))
1134
47.6k
      continue;
1135
    /* Is this instruction supported by the current architecture?  */
1136
1.50M
    if (!pd->all_ext)
1137
1.50M
      {
1138
1.50M
        if (!riscv_multi_subset_supports (&pd->riscv_rps_dis,
1139
1.50M
            op->insn_class))
1140
132k
    continue;
1141
1142
1.37M
        if (pd->xlen == 32
1143
0
      && riscv_subset_supports (&pd->riscv_rps_dis, "zdinx"))
1144
0
    {
1145
0
      if ((op->pinfo & INSN_RV32_EVEN_D)
1146
0
          && (word & (1u << OP_SH_RD)))
1147
0
        continue;
1148
0
      if ((op->pinfo & INSN_RV32_EVEN_S)
1149
0
          && (word & (1u << OP_SH_RS1)))
1150
0
        continue;
1151
0
      if ((op->pinfo & INSN_RV32_EVEN_T)
1152
0
          && (word & (1u << OP_SH_RS2)))
1153
0
        continue;
1154
0
      if ((op->pinfo & INSN_RV32_EVEN_R)
1155
0
          && (word & (1u << OP_SH_RS3)))
1156
0
        continue;
1157
0
    }
1158
1159
1.37M
        if (pd->xlen <= 64
1160
1.37M
      && riscv_subset_supports (&pd->riscv_rps_dis, "zqinx"))
1161
0
    {
1162
0
      unsigned int mask = pd->xlen == 32 ? 3 : 1;
1163
1164
0
      if ((op->pinfo & INSN_RV64_EVEN_D)
1165
0
          && (word & (mask << OP_SH_RD)))
1166
0
        continue;
1167
0
      if ((op->pinfo & INSN_RV64_EVEN_S)
1168
0
          && (word & (mask << OP_SH_RS1)))
1169
0
        continue;
1170
0
      if ((op->pinfo & INSN_RV64_EVEN_T)
1171
0
          && (word & (mask << OP_SH_RS2)))
1172
0
        continue;
1173
0
      if ((op->pinfo & INSN_RV64_EVEN_R)
1174
0
          && (word & (mask << OP_SH_RS3)))
1175
0
        continue;
1176
0
    }
1177
1.37M
      }
1178
1179
    /* It's a match.  */
1180
1.37M
    (*info->fprintf_styled_func) (info->stream, dis_style_mnemonic,
1181
1.37M
          "%s", op->name);
1182
1.37M
    print_insn_args (op->args, word, memaddr, info);
1183
1184
    /* Try to disassemble multi-instruction addressing sequences.  */
1185
1.37M
    if (pd->to_print_addr)
1186
24.2k
      {
1187
24.2k
        info->target = pd->print_addr;
1188
24.2k
        (*info->fprintf_styled_func)
1189
24.2k
    (info->stream, dis_style_comment_start, " # ");
1190
24.2k
        (*info->print_address_func) (info->target, info);
1191
24.2k
        pd->to_print_addr = false;
1192
24.2k
      }
1193
1194
    /* Finish filling out insn_info fields.  */
1195
1.37M
    switch (op->pinfo & INSN_TYPE)
1196
1.37M
      {
1197
25.2k
      case INSN_BRANCH:
1198
25.2k
        info->insn_type = dis_branch;
1199
25.2k
        break;
1200
64.8k
      case INSN_CONDBRANCH:
1201
64.8k
        info->insn_type = dis_condbranch;
1202
64.8k
        break;
1203
10.3k
      case INSN_JSR:
1204
10.3k
        info->insn_type = dis_jsr;
1205
10.3k
        break;
1206
481k
      case INSN_DREF:
1207
481k
        info->insn_type = dis_dref;
1208
481k
        break;
1209
794k
      default:
1210
794k
        break;
1211
1.37M
      }
1212
1213
1.37M
    if (op->pinfo & INSN_DATA_SIZE)
1214
481k
      {
1215
481k
        int size = ((op->pinfo & INSN_DATA_SIZE)
1216
481k
        >> INSN_DATA_SIZE_SHIFT);
1217
481k
        info->data_size = 1 << (size - 1);
1218
481k
      }
1219
1220
1.37M
    return insnlen;
1221
1.37M
  }
1222
1.69M
    }
1223
1224
  /* We did not find a match, so just print the instruction bits in
1225
     the shape of an assembler .insn directive.  */
1226
368k
  info->insn_type = dis_noninsn;
1227
368k
  (*info->fprintf_styled_func)
1228
368k
    (info->stream, dis_style_assembler_directive, ".insn");
1229
368k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1230
368k
  (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1231
368k
        "%d", insnlen);
1232
368k
  (*info->fprintf_styled_func) (info->stream, dis_style_text, ", ");
1233
368k
  (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x");
1234
1.09M
  for (i = insnlen, printed = false; i >= 2; )
1235
723k
    {
1236
723k
      i -= 2;
1237
723k
      word = bfd_get_bits (packet + i, 16, false);
1238
723k
      if (!word && !printed && i)
1239
21.2k
  continue;
1240
1241
702k
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1242
702k
            "%04x", (unsigned int) word);
1243
702k
      printed = true;
1244
702k
    }
1245
1246
368k
  return insnlen;
1247
1.74M
}
1248
1249
/* Decide if we need to parse the architecture string again, also record the
1250
   string into the current subset list.  */
1251
1252
static void
1253
riscv_dis_parse_subset (struct disassemble_info *info, const char *arch_new)
1254
1.55k
{
1255
1.55k
  struct riscv_private_data *pd = info->private_data;
1256
1.55k
  const char *arch_subset_list = pd->riscv_rps_dis.subset_list->arch_str;
1257
1.55k
  if (arch_subset_list == NULL || strcmp (arch_subset_list, arch_new) != 0)
1258
1.55k
    {
1259
1.55k
      riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
1260
1.55k
      riscv_parse_subset (&pd->riscv_rps_dis, arch_new);
1261
1.55k
      riscv_arch_str (pd->xlen, pd->riscv_rps_dis.subset_list,
1262
1.55k
          true/* update */);
1263
1.55k
    }
1264
1.55k
}
1265
1266
/* If we find the suitable mapping symbol update the STATE.
1267
   Otherwise, do nothing.  */
1268
1269
static void
1270
riscv_update_map_state (int n,
1271
      enum riscv_seg_mstate *state,
1272
      struct disassemble_info *info)
1273
0
{
1274
0
  struct riscv_private_data *pd = info->private_data;
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;
1281
1282
0
  name = bfd_asymbol_name(info->symtab[n]);
1283
0
  if (strcmp (name, "$d") == 0)
1284
0
    *state = MAP_DATA;
1285
0
  else if (strcmp (name, "$x") == 0)
1286
0
    {
1287
0
      *state = MAP_INSN;
1288
0
      riscv_dis_parse_subset (info, pd->default_arch);
1289
0
    }
1290
0
  else if (strncmp (name, "$xrv", 4) == 0)
1291
0
    {
1292
0
      *state = MAP_INSN;
1293
1294
      /* ISA mapping string may be numbered, suffixed with '.n'. Do not
1295
   consider this as part of the ISA string.  */
1296
0
      const char *suffix = strchr (name, '.');
1297
0
      if (suffix)
1298
0
  {
1299
0
    int suffix_index = (int)(suffix - name);
1300
0
    char *name_substr = xmalloc (suffix_index + 1);
1301
0
    strncpy (name_substr, name, suffix_index);
1302
0
    name_substr[suffix_index] = '\0';
1303
0
    riscv_dis_parse_subset (info, name_substr + 2);
1304
0
    free (name_substr);
1305
0
  }
1306
0
      else
1307
0
  riscv_dis_parse_subset (info, name + 2);
1308
0
    }
1309
0
}
1310
1311
/* Return true if we find the suitable mapping symbol.
1312
   Otherwise, return false.  */
1313
1314
static bool
1315
riscv_is_valid_mapping_symbol (int n,
1316
           struct disassemble_info *info)
1317
1.18k
{
1318
1.18k
  const char *name;
1319
1320
  /* If the symbol is in a different section, ignore it.  */
1321
1.18k
  if (info->section != NULL
1322
1.18k
      && info->section != info->symtab[n]->section)
1323
562
    return false;
1324
1325
624
  name = bfd_asymbol_name(info->symtab[n]);
1326
624
  return riscv_elf_is_mapping_symbols (name);
1327
1.18k
}
1328
1329
/* Check the sorted symbol table (sorted by the symbol value), find the
1330
   suitable mapping symbols.  */
1331
1332
static enum riscv_seg_mstate
1333
riscv_search_mapping_symbol (bfd_vma memaddr,
1334
           struct disassemble_info *info)
1335
1.74M
{
1336
1.74M
  struct riscv_private_data *pd = info->private_data;
1337
1.74M
  enum riscv_seg_mstate mstate;
1338
1.74M
  bool from_last_map_symbol;
1339
1.74M
  bool found = false;
1340
1.74M
  int symbol = -1;
1341
1.74M
  int n;
1342
1343
  /* Return the last map state if the address is still within the range of the
1344
     last mapping symbol.  */
1345
1.74M
  if (pd->last_map_section == info->section
1346
1.74M
      && (memaddr < pd->last_map_symbol_boundary))
1347
0
    return pd->last_map_state;
1348
1349
1.74M
  pd->last_map_section = info->section;
1350
1351
  /* Decide whether to print the data or instruction by default, in case
1352
     we can not find the corresponding mapping symbols.  */
1353
1.74M
  mstate = MAP_DATA;
1354
1.74M
  if ((info->section
1355
1.08M
       && info->section->flags & SEC_CODE)
1356
1.74M
      || !info->section)
1357
656k
    mstate = MAP_INSN;
1358
1359
1.74M
  if (info->symtab_size == 0
1360
482
      || bfd_asymbol_flavour (*info->symtab) != bfd_target_elf_flavour)
1361
1.74M
    return mstate;
1362
1363
  /* Reset the last_map_symbol if we start to dump a new section.  */
1364
482
  if (memaddr <= 0)
1365
7
    pd->last_map_symbol = -1;
1366
1367
  /* If the last stop offset is different from the current one, then
1368
     don't use the last_map_symbol to search.  We usually reset the
1369
     info->stop_offset when handling a new section.  */
1370
482
  from_last_map_symbol = (pd->last_map_symbol >= 0
1371
0
        && info->stop_offset == pd->last_stop_offset);
1372
1373
  /* Start scanning from wherever we finished last time, or the start
1374
     of the function.  */
1375
482
  n = from_last_map_symbol ? pd->last_map_symbol : info->symtab_pos + 1;
1376
1377
  /* Find the suitable mapping symbol to dump.  */
1378
1.15k
  for (; n < info->symtab_size; n++)
1379
943
    {
1380
943
      bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1381
      /* We have searched all possible symbols in the range.  */
1382
943
      if (addr > memaddr)
1383
268
  break;
1384
675
      if (riscv_is_valid_mapping_symbol (n, info))
1385
0
  {
1386
0
    symbol = n;
1387
0
    found = true;
1388
    /* Do not stop searching, in case there are some mapping
1389
       symbols have the same value, but have different names.
1390
       Use the last one.  */
1391
0
  }
1392
675
    }
1393
1394
  /* We can not find the suitable mapping symbol above.  Therefore, we
1395
     look forwards and try to find it again, but don't go past the start
1396
     of the section.  Otherwise a data section without mapping symbols
1397
     can pick up a text mapping symbol of a preceeding section.  */
1398
482
  if (!found)
1399
482
    {
1400
482
      n = from_last_map_symbol ? pd->last_map_symbol : info->symtab_pos;
1401
1402
993
      for (; n >= 0; n--)
1403
511
  {
1404
511
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1405
    /* We have searched all possible symbols in the range.  */
1406
511
    if (addr < (info->section ? info->section->vma : 0))
1407
0
      break;
1408
    /* Stop searching once we find the closed mapping symbol.  */
1409
511
    if (riscv_is_valid_mapping_symbol (n, info))
1410
0
      {
1411
0
        symbol = n;
1412
0
        found = true;
1413
0
        break;
1414
0
      }
1415
511
  }
1416
482
    }
1417
1418
482
  if (found)
1419
0
    {
1420
0
      riscv_update_map_state (symbol, &mstate, info);
1421
1422
      /* Find the next mapping symbol to determine the boundary of this mapping
1423
   symbol.  */
1424
1425
0
      bool found_next = false;
1426
      /* Try to found next mapping symbol.  */
1427
0
      for (n = symbol + 1; n < info->symtab_size; n++)
1428
0
  {
1429
0
    if (info->symtab[symbol]->section != info->symtab[n]->section)
1430
0
      continue;
1431
1432
0
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1433
0
    const char *sym_name = bfd_asymbol_name(info->symtab[n]);
1434
0
    if (sym_name[0] == '$' && (sym_name[1] == 'x' || sym_name[1] == 'd'))
1435
0
      {
1436
        /* The next mapping symbol has been found, and it represents the
1437
     boundary of this mapping symbol.  */
1438
0
        found_next = true;
1439
0
        pd->last_map_symbol_boundary = addr;
1440
0
        break;
1441
0
      }
1442
0
  }
1443
1444
      /* No further mapping symbol has been found, indicating that the boundary
1445
   of the current mapping symbol is the end of this section.  */
1446
0
      if (!found_next)
1447
0
  pd->last_map_symbol_boundary = info->section->vma
1448
0
               + info->section->size;
1449
0
    }
1450
1451
  /* Save the information for next use.  */
1452
482
  pd->last_map_symbol = symbol;
1453
482
  pd->last_stop_offset = info->stop_offset;
1454
1455
482
  return mstate;
1456
1.74M
}
1457
1458
/* Decide which data size we should print.  */
1459
1460
static bfd_vma
1461
riscv_data_length (bfd_vma memaddr,
1462
       disassemble_info *info)
1463
0
{
1464
0
  struct riscv_private_data *pd = info->private_data;
1465
0
  bfd_vma length;
1466
0
  bool found = false;
1467
1468
0
  length = 4;
1469
0
  if (info->symtab_size != 0
1470
0
      && bfd_asymbol_flavour (*info->symtab) == bfd_target_elf_flavour
1471
0
      && pd->last_map_symbol >= 0)
1472
0
    {
1473
0
      int n;
1474
0
      enum riscv_seg_mstate m = MAP_NONE;
1475
0
      for (n = pd->last_map_symbol + 1; n < info->symtab_size; n++)
1476
0
  {
1477
0
    bfd_vma addr = bfd_asymbol_value (info->symtab[n]);
1478
0
    if (addr > memaddr
1479
0
        && riscv_is_valid_mapping_symbol (n, info))
1480
0
      {
1481
0
        if (addr - memaddr < length)
1482
0
    length = addr - memaddr;
1483
0
        found = true;
1484
0
        riscv_update_map_state (n, &m, info);
1485
0
        break;
1486
0
      }
1487
0
  }
1488
0
    }
1489
0
  if (!found)
1490
0
    {
1491
      /* Do not set the length which exceeds the section size.  */
1492
0
      bfd_vma offset = info->section->vma + info->section->size;
1493
0
      offset -= memaddr;
1494
0
      length = (offset < length) ? offset : length;
1495
0
    }
1496
0
  length = length == 3 ? 2 : length;
1497
0
  return length;
1498
0
}
1499
1500
/* Dump the data contents.  */
1501
1502
static int
1503
riscv_disassemble_data (bfd_vma memaddr ATTRIBUTE_UNUSED,
1504
      insn_t data,
1505
      const bfd_byte *packet ATTRIBUTE_UNUSED,
1506
      disassemble_info *info)
1507
848
{
1508
848
  info->display_endian = info->endian;
1509
848
  int i;
1510
1511
848
  switch (info->bytes_per_chunk)
1512
848
    {
1513
413
    case 1:
1514
413
      info->bytes_per_line = 6;
1515
413
      (*info->fprintf_styled_func)
1516
413
  (info->stream, dis_style_assembler_directive, ".byte");
1517
413
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1518
413
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate,
1519
413
            "0x%02x", (unsigned)data);
1520
413
      break;
1521
157
    case 2:
1522
157
      info->bytes_per_line = 8;
1523
157
      (*info->fprintf_styled_func)
1524
157
  (info->stream, dis_style_assembler_directive, ".short");
1525
157
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1526
157
      (*info->fprintf_styled_func)
1527
157
  (info->stream, dis_style_immediate, "0x%04x", (unsigned) data);
1528
157
      break;
1529
31
    case 4:
1530
31
      info->bytes_per_line = 8;
1531
31
      (*info->fprintf_styled_func)
1532
31
  (info->stream, dis_style_assembler_directive, ".word");
1533
31
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1534
31
      (*info->fprintf_styled_func)
1535
31
  (info->stream, dis_style_immediate, "0x%08lx",
1536
31
   (unsigned long) data);
1537
31
      break;
1538
26
    case 8:
1539
26
      info->bytes_per_line = 8;
1540
26
      (*info->fprintf_styled_func)
1541
26
  (info->stream, dis_style_assembler_directive, ".dword");
1542
26
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1543
26
      (*info->fprintf_styled_func)
1544
26
  (info->stream, dis_style_immediate, "0x%016llx",
1545
26
   (unsigned long long) data);
1546
26
      break;
1547
221
    default:
1548
      /* Arbitrary data so just print the bits in the shape of an .<N>byte
1549
   directive.  */
1550
221
      info->bytes_per_line = info->bytes_per_chunk;
1551
221
      (*info->fprintf_styled_func)
1552
221
  (info->stream, dis_style_assembler_directive, ".%dbyte", info->bytes_per_chunk);
1553
221
      (*info->fprintf_styled_func) (info->stream, dis_style_text, "\t");
1554
221
      (*info->fprintf_styled_func) (info->stream, dis_style_immediate, "0x");
1555
1.76k
      for (i = info->bytes_per_line; i > 0;)
1556
1.54k
  {
1557
1.54k
    i--;
1558
1.54k
    data = bfd_get_bits (packet + i, 8, false);
1559
1.54k
    (*info->fprintf_styled_func)
1560
1.54k
      (info->stream, dis_style_immediate, "%02x",
1561
1.54k
        (unsigned) data);
1562
1.54k
  }
1563
221
      break;
1564
848
    }
1565
848
  return info->bytes_per_chunk;
1566
848
}
1567
1568
static bool
1569
riscv_init_disasm_info (struct disassemble_info *info)
1570
1.55k
{
1571
1.55k
  int i;
1572
1.55k
  struct riscv_private_data *pd =
1573
1.55k
  xcalloc (1, sizeof (struct riscv_private_data));
1574
1.55k
  pd->gp = 0;
1575
1.55k
  pd->print_addr = 0;
1576
51.1k
  for (i = 0; i < (int) ARRAY_SIZE (pd->hi_addr); i++)
1577
49.6k
    pd->hi_addr[i] = -1;
1578
1.55k
  pd->to_print_addr = false;
1579
1580
1.55k
  pd->has_gp = false;
1581
1.57k
  for (i = 0; i < info->symtab_size; i++)
1582
24
    {
1583
24
      asymbol *sym = info->symtab[i];
1584
24
      if (strcmp (bfd_asymbol_name (sym), RISCV_GP_SYMBOL) == 0)
1585
0
  {
1586
0
    pd->gp = bfd_asymbol_value (sym);
1587
0
    pd->has_gp = true;
1588
0
  }
1589
24
    }
1590
1591
1.55k
  pd->xlen = 0;
1592
1.55k
  pd->default_isa_spec = ISA_SPEC_CLASS_DRAFT - 1;
1593
1.55k
  pd->default_priv_spec = PRIV_SPEC_CLASS_NONE;
1594
1595
1.55k
  pd->riscv_rps_dis.subset_list = xcalloc (1, sizeof (riscv_parse_subset_t));
1596
1.55k
  pd->riscv_rps_dis.error_handler = opcodes_error_handler;
1597
1.55k
  pd->riscv_rps_dis.xlen = &pd->xlen;
1598
1.55k
  pd->riscv_rps_dis.isa_spec = &pd->default_isa_spec;
1599
1.55k
  pd->riscv_rps_dis.check_unknown_prefixed_ext = false;
1600
1.55k
  pd->default_arch = "rv64gc";
1601
1.55k
  if (info->section != NULL)
1602
457
    {
1603
457
      bfd *abfd = info->section->owner;
1604
457
      if (abfd && bfd_get_flavour (abfd) == bfd_target_elf_flavour)
1605
456
  {
1606
456
    const char *sec_name =
1607
456
    get_elf_backend_data (abfd)->obj_attrs_section;
1608
456
    if (bfd_get_section_by_name (abfd, sec_name) != NULL)
1609
0
      {
1610
0
        obj_attribute *attr = elf_known_obj_attributes_proc (abfd);
1611
0
        unsigned int Tag_a = Tag_RISCV_priv_spec;
1612
0
        unsigned int Tag_b = Tag_RISCV_priv_spec_minor;
1613
0
        unsigned int Tag_c = Tag_RISCV_priv_spec_revision;
1614
0
        riscv_get_priv_spec_class_from_numbers (attr[Tag_a].i,
1615
0
                  attr[Tag_b].i,
1616
0
                  attr[Tag_c].i,
1617
0
                  &pd->default_priv_spec);
1618
0
        pd->default_arch = attr[Tag_RISCV_arch].s;
1619
0
      }
1620
456
  }
1621
457
    }
1622
1623
1.55k
  pd->last_map_symbol = -1;
1624
1.55k
  pd->last_stop_offset = 0;
1625
1.55k
  pd->last_map_symbol_boundary = 0;
1626
1.55k
  pd->last_map_state = MAP_NONE;
1627
1.55k
  pd->last_map_section = NULL;
1628
1.55k
  pd->riscv_gpr_names = NULL;
1629
1.55k
  pd->riscv_fpr_names = NULL;
1630
1.55k
  pd->no_aliases = false;
1631
1.55k
  pd->all_ext = false;
1632
1633
1.55k
  info->private_data = pd;
1634
1.55k
  riscv_dis_parse_subset (info, pd->default_arch);
1635
1.55k
  return true;
1636
1.55k
}
1637
1638
/* Fetch an instruction. If only a partial instruction is able to be fetched,
1639
   return the number of accessible bytes.  */
1640
1641
static bfd_vma
1642
fetch_insn (bfd_vma memaddr,
1643
      bfd_byte *packet,
1644
      bfd_vma dump_size,
1645
      struct disassemble_info *info,
1646
      volatile int *status)
1647
3.48M
{
1648
3.48M
  do
1649
3.49M
    {
1650
3.49M
      *status = (*info->read_memory_func) (memaddr, packet, dump_size, info);
1651
3.49M
    }
1652
3.49M
  while (*status != 0 && dump_size-- > 1);
1653
1654
3.48M
  return dump_size;
1655
3.48M
}
1656
1657
int
1658
print_insn_riscv (bfd_vma memaddr, struct disassemble_info *info)
1659
1.74M
{
1660
1.74M
  bfd_byte packet[RISCV_MAX_INSN_LEN];
1661
1.74M
  insn_t insn = 0;
1662
1.74M
  bfd_vma dump_size, bytes_fetched;
1663
1.74M
  int status;
1664
1.74M
  enum riscv_seg_mstate mstate;
1665
1.74M
  int (*riscv_disassembler) (bfd_vma, insn_t, const bfd_byte *,
1666
1.74M
           struct disassemble_info *);
1667
1668
1.74M
  if (info->private_data == NULL && !riscv_init_disasm_info (info))
1669
0
    return -1;
1670
1671
1.74M
  if (info->disassembler_options != NULL)
1672
0
    {
1673
0
      parse_riscv_dis_options (info->disassembler_options, info);
1674
      /* Avoid repeatedly parsing the options.  */
1675
0
      info->disassembler_options = NULL;
1676
0
    }
1677
1.74M
  else if (((struct riscv_private_data *) info->private_data)->riscv_gpr_names == NULL)
1678
1.55k
    set_default_riscv_dis_options (info);
1679
1680
1.74M
  mstate = riscv_search_mapping_symbol (memaddr, info);
1681
  /* Save the last mapping state.  */
1682
1.74M
  ((struct riscv_private_data *) info->private_data)->last_map_state = mstate;
1683
1684
  /* Set the size to dump.  */
1685
1.74M
  if (mstate == MAP_DATA
1686
1.08M
      && (info->flags & DISASSEMBLE_DATA) == 0)
1687
0
    {
1688
0
      dump_size = riscv_data_length (memaddr, info);
1689
0
      info->bytes_per_chunk = dump_size;
1690
0
      riscv_disassembler = riscv_disassemble_data;
1691
0
    }
1692
1.74M
  else
1693
1.74M
    {
1694
      /* Get the first 2-bytes to check the lenghth of instruction.  */
1695
1.74M
      bytes_fetched = fetch_insn (memaddr, packet, 2, info, &status);
1696
1.74M
      if (status != 0)
1697
3
  {
1698
3
    (*info->memory_error_func) (status, memaddr, info);
1699
3
    return -1;
1700
3
  }
1701
1.74M
      else if (bytes_fetched != 2)
1702
413
       {
1703
    /* Only the first byte was able to be read.  Dump the partial
1704
       instruction.  */
1705
413
    dump_size = bytes_fetched;
1706
413
    info->bytes_per_chunk = dump_size;
1707
413
    riscv_disassembler = riscv_disassemble_data;
1708
413
    goto print;
1709
413
       }
1710
1.74M
      insn = (insn_t) bfd_getl16 (packet);
1711
1.74M
      dump_size = riscv_insn_length (insn);
1712
1.74M
      riscv_disassembler = riscv_disassemble_insn;
1713
1.74M
    }
1714
1715
1.74M
  bytes_fetched = fetch_insn (memaddr, packet, dump_size, info, &status);
1716
1717
1.74M
  if (status != 0)
1718
0
    {
1719
0
      (*info->memory_error_func) (status, memaddr, info);
1720
0
      return -1;
1721
0
    }
1722
1.74M
  else if (bytes_fetched != dump_size)
1723
435
    {
1724
435
      dump_size = bytes_fetched;
1725
435
      info->bytes_per_chunk = dump_size;
1726
435
      riscv_disassembler = riscv_disassemble_data;
1727
435
    }
1728
1729
1.74M
 print:
1730
1731
1.74M
  insn = (insn_t) bfd_get_bits (packet, dump_size * 8, false);
1732
1733
1.74M
  return (*riscv_disassembler) (memaddr, insn, packet, info);
1734
1.74M
}
1735
1736
/* Prevent use of the fake labels that are generated as part of the DWARF
1737
   and for relaxable relocations in the assembler.  */
1738
1739
bool
1740
riscv_symbol_is_valid (asymbol * sym,
1741
                       struct disassemble_info * info ATTRIBUTE_UNUSED)
1742
33
{
1743
33
  const char * name;
1744
1745
33
  if (sym == NULL)
1746
0
    return false;
1747
1748
33
  name = bfd_asymbol_name (sym);
1749
1750
33
  return (strcmp (name, RISCV_FAKE_LABEL_NAME) != 0
1751
33
    && !riscv_elf_is_mapping_symbols (name));
1752
33
}
1753

1754
1755
/* Indices into option argument vector for options accepting an argument.
1756
   Use RISCV_OPTION_ARG_NONE for options accepting no argument.  */
1757
1758
typedef enum
1759
{
1760
  RISCV_OPTION_ARG_NONE = -1,
1761
  RISCV_OPTION_ARG_PRIV_SPEC,
1762
1763
  RISCV_OPTION_ARG_COUNT
1764
} riscv_option_arg_t;
1765
1766
/* Valid RISCV disassembler options.  */
1767
1768
static const struct
1769
{
1770
  const char *name;
1771
  const char *description;
1772
  riscv_option_arg_t arg;
1773
} riscv_options[] =
1774
{
1775
  { "max",
1776
    N_("Disassemble without checking architecture string."),
1777
    RISCV_OPTION_ARG_NONE },
1778
  { "numeric",
1779
    N_("Print numeric register names, rather than ABI names."),
1780
    RISCV_OPTION_ARG_NONE },
1781
  { "no-aliases",
1782
    N_("Disassemble only into canonical instructions."),
1783
    RISCV_OPTION_ARG_NONE },
1784
  { "priv-spec=",
1785
    N_("Print the CSR according to the chosen privilege spec."),
1786
    RISCV_OPTION_ARG_PRIV_SPEC }
1787
};
1788
1789
/* Build the structure representing valid RISCV disassembler options.
1790
   This is done dynamically for maintenance ease purpose; a static
1791
   initializer would be unreadable.  */
1792
1793
const disasm_options_and_args_t *
1794
disassembler_options_riscv (void)
1795
0
{
1796
0
  static disasm_options_and_args_t *opts_and_args;
1797
1798
0
  if (opts_and_args == NULL)
1799
0
    {
1800
0
      size_t num_options = ARRAY_SIZE (riscv_options);
1801
0
      size_t num_args = RISCV_OPTION_ARG_COUNT;
1802
0
      disasm_option_arg_t *args;
1803
0
      disasm_options_t *opts;
1804
0
      size_t i, priv_spec_count;
1805
1806
0
      args = XNEWVEC (disasm_option_arg_t, num_args + 1);
1807
1808
0
      args[RISCV_OPTION_ARG_PRIV_SPEC].name = "SPEC";
1809
0
      priv_spec_count = PRIV_SPEC_CLASS_DRAFT - PRIV_SPEC_EARLIEST;
1810
0
      args[RISCV_OPTION_ARG_PRIV_SPEC].values
1811
0
        = XNEWVEC (const char *, priv_spec_count + 1);
1812
0
      for (i = 0; i < priv_spec_count; i++)
1813
0
  args[RISCV_OPTION_ARG_PRIV_SPEC].values[i]
1814
0
    = riscv_priv_specs[PRIV_SPEC_EARLIEST - PRIV_SPEC_CLASS_NONE - 1 + i].name;
1815
      /* The array we return must be NULL terminated.  */
1816
0
      args[RISCV_OPTION_ARG_PRIV_SPEC].values[i] = NULL;
1817
1818
      /* The array we return must be NULL terminated.  */
1819
0
      args[num_args].name = NULL;
1820
0
      args[num_args].values = NULL;
1821
1822
0
      opts_and_args = XNEW (disasm_options_and_args_t);
1823
0
      opts_and_args->args = args;
1824
1825
0
      opts = &opts_and_args->options;
1826
0
      opts->name = XNEWVEC (const char *, num_options + 1);
1827
0
      opts->description = XNEWVEC (const char *, num_options + 1);
1828
0
      opts->arg = XNEWVEC (const disasm_option_arg_t *, num_options + 1);
1829
0
      for (i = 0; i < num_options; i++)
1830
0
  {
1831
0
    opts->name[i] = riscv_options[i].name;
1832
0
    opts->description[i] = _(riscv_options[i].description);
1833
0
    if (riscv_options[i].arg != RISCV_OPTION_ARG_NONE)
1834
0
      opts->arg[i] = &args[riscv_options[i].arg];
1835
0
    else
1836
0
      opts->arg[i] = NULL;
1837
0
  }
1838
      /* The array we return must be NULL terminated.  */
1839
0
      opts->name[i] = NULL;
1840
0
      opts->description[i] = NULL;
1841
0
      opts->arg[i] = NULL;
1842
0
    }
1843
1844
0
  return opts_and_args;
1845
0
}
1846
1847
void
1848
print_riscv_disassembler_options (FILE *stream)
1849
0
{
1850
0
  const disasm_options_and_args_t *opts_and_args;
1851
0
  const disasm_option_arg_t *args;
1852
0
  const disasm_options_t *opts;
1853
0
  size_t max_len = 0;
1854
0
  size_t i;
1855
0
  size_t j;
1856
1857
0
  opts_and_args = disassembler_options_riscv ();
1858
0
  opts = &opts_and_args->options;
1859
0
  args = opts_and_args->args;
1860
1861
0
  fprintf (stream, _("\n\
1862
0
The following RISC-V specific disassembler options are supported for use\n\
1863
0
with the -M switch (multiple options should be separated by commas):\n"));
1864
0
  fprintf (stream, "\n");
1865
1866
  /* Compute the length of the longest option name.  */
1867
0
  for (i = 0; opts->name[i] != NULL; i++)
1868
0
    {
1869
0
      size_t len = strlen (opts->name[i]);
1870
1871
0
      if (opts->arg[i] != NULL)
1872
0
  len += strlen (opts->arg[i]->name);
1873
0
      if (max_len < len)
1874
0
  max_len = len;
1875
0
    }
1876
1877
0
  for (i = 0, max_len++; opts->name[i] != NULL; i++)
1878
0
    {
1879
0
      fprintf (stream, "  %s", opts->name[i]);
1880
0
      if (opts->arg[i] != NULL)
1881
0
  fprintf (stream, "%s", opts->arg[i]->name);
1882
0
      if (opts->description[i] != NULL)
1883
0
  {
1884
0
    size_t len = strlen (opts->name[i]);
1885
1886
0
    if (opts->arg != NULL && opts->arg[i] != NULL)
1887
0
      len += strlen (opts->arg[i]->name);
1888
0
    fprintf (stream, "%*c %s", (int) (max_len - len), ' ',
1889
0
                   opts->description[i]);
1890
0
  }
1891
0
      fprintf (stream, "\n");
1892
0
    }
1893
1894
0
  for (i = 0; args[i].name != NULL; i++)
1895
0
    {
1896
0
      if (args[i].values == NULL)
1897
0
  continue;
1898
0
      fprintf (stream, _("\n\
1899
0
  For the options above, the following values are supported for \"%s\":\n   "),
1900
0
         args[i].name);
1901
0
      for (j = 0; args[i].values[j] != NULL; j++)
1902
0
  fprintf (stream, " %s", args[i].values[j]);
1903
0
      fprintf (stream, _("\n"));
1904
0
    }
1905
1906
0
  fprintf (stream, _("\n"));
1907
0
}
1908
1909
void disassemble_free_riscv (struct disassemble_info *info ATTRIBUTE_UNUSED)
1910
1.62k
{
1911
1.62k
  struct riscv_private_data *pd = info->private_data;
1912
1.62k
  if (pd)
1913
1.55k
    {
1914
1.55k
      riscv_release_subset_list (pd->riscv_rps_dis.subset_list);
1915
1.55k
      free (pd->riscv_rps_dis.subset_list);
1916
1.55k
    }
1917
1.62k
}