Coverage Report

Created: 2026-03-10 08:46

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