Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/opcodes/wasm32-dis.c
Line
Count
Source
1
/* Opcode printing code for the WebAssembly target
2
   Copyright (C) 2017-2026 Free Software Foundation, Inc.
3
4
   This file is part of libopcodes.
5
6
   This library is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   It is distributed in the hope that it will be useful, but WITHOUT
12
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14
   License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "disassemble.h"
23
#include "opintl.h"
24
#include "safe-ctype.h"
25
#include "floatformat.h"
26
#include "libiberty.h"
27
#include "elf-bfd.h"
28
#include "elf/internal.h"
29
#include "elf/wasm32.h"
30
#include <stdint.h>
31
32
#include <limits.h>
33
#ifndef CHAR_BIT
34
#define CHAR_BIT 8
35
#endif
36
37
/* Type names for blocks and signatures.  */
38
5
#define BLOCK_TYPE_NONE              0x40
39
139
#define BLOCK_TYPE_I32               0x7f
40
50
#define BLOCK_TYPE_I64               0x7e
41
21
#define BLOCK_TYPE_F32               0x7d
42
41
#define BLOCK_TYPE_F64               0x7c
43
44
enum wasm_class
45
{
46
  wasm_typed,
47
  wasm_special,
48
  wasm_break,
49
  wasm_break_if,
50
  wasm_break_table,
51
  wasm_return,
52
  wasm_call,
53
  wasm_call_import,
54
  wasm_call_indirect,
55
  wasm_get_local,
56
  wasm_set_local,
57
  wasm_tee_local,
58
  wasm_drop,
59
  wasm_constant_i32,
60
  wasm_constant_i64,
61
  wasm_constant_f32,
62
  wasm_constant_f64,
63
  wasm_unary,
64
  wasm_binary,
65
  wasm_conv,
66
  wasm_load,
67
  wasm_store,
68
  wasm_select,
69
  wasm_relational,
70
  wasm_eqz,
71
  wasm_current_memory,
72
  wasm_grow_memory,
73
  wasm_signature
74
};
75
76
struct wasm32_private_data
77
{
78
  bool print_registers;
79
  bool print_well_known_globals;
80
81
  /* Limit valid symbols to those with a given prefix.  */
82
  const char *section_prefix;
83
};
84
85
typedef struct
86
{
87
  const char *name;
88
  const char *description;
89
} wasm32_options_t;
90
91
static const wasm32_options_t options[] =
92
{
93
  { "registers", N_("Disassemble \"register\" names") },
94
  { "globals",   N_("Name well-known globals") },
95
};
96
97
#define WASM_OPCODE(opcode, name, intype, outtype, clas, signedness)     \
98
  { name, wasm_ ## clas, opcode },
99
100
struct wasm32_opcode_s
101
{
102
  const char *name;
103
  enum wasm_class clas;
104
  unsigned char opcode;
105
} wasm32_opcodes[] =
106
{
107
#include "opcode/wasm.h"
108
  { NULL, 0, 0 }
109
};
110
111
/* Parse the disassembler options in OPTS and initialize INFO.  */
112
113
static void
114
parse_wasm32_disassembler_options (struct disassemble_info *info,
115
                                   const char *opts)
116
0
{
117
0
  struct wasm32_private_data *private = info->private_data;
118
119
0
  while (opts != NULL)
120
0
    {
121
0
      if (startswith (opts, "registers"))
122
0
        private->print_registers = true;
123
0
      else if (startswith (opts, "globals"))
124
0
        private->print_well_known_globals = true;
125
126
0
      opts = strchr (opts, ',');
127
0
      if (opts)
128
0
        opts++;
129
0
    }
130
0
}
131
132
/* Check whether SYM is valid.  Special-case absolute symbols, which
133
   are unhelpful to print, and arguments to a "call" insn, which we
134
   want to be in a section matching a given prefix.  */
135
136
static bool
137
wasm32_symbol_is_valid (asymbol *sym,
138
                        struct disassemble_info *info)
139
0
{
140
0
  struct wasm32_private_data *private_data = info->private_data;
141
142
0
  if (sym == NULL)
143
0
    return false;
144
145
0
  if (strcmp(sym->section->name, "*ABS*") == 0)
146
0
    return false;
147
148
0
  if (private_data && private_data->section_prefix != NULL
149
0
      && strncmp (sym->section->name, private_data->section_prefix,
150
0
                  strlen (private_data->section_prefix)))
151
0
    return false;
152
153
0
  return true;
154
0
}
155
156
/* Initialize the disassembler structures for INFO.  */
157
158
void
159
disassemble_init_wasm32 (struct disassemble_info *info)
160
323
{
161
323
  if (info->private_data == NULL)
162
323
    {
163
323
      static struct wasm32_private_data private;
164
165
323
      private.print_registers = false;
166
323
      private.print_well_known_globals = false;
167
323
      private.section_prefix = NULL;
168
169
323
      info->private_data = &private;
170
323
    }
171
172
323
  if (info->disassembler_options)
173
0
    {
174
0
      parse_wasm32_disassembler_options (info, info->disassembler_options);
175
176
0
      info->disassembler_options = NULL;
177
0
    }
178
179
323
  info->symbol_is_valid = wasm32_symbol_is_valid;
180
323
}
181
182
/* Read an LEB128-encoded integer from INFO at address PC, reading one
183
   byte at a time.  Set ERROR_RETURN if no complete integer could be
184
   read, LENGTH_RETURN to the number oof bytes read (including bytes
185
   in incomplete numbers).  SIGN means interpret the number as
186
   SLEB128.  Unfortunately, this is a duplicate of wasm-module.c's
187
   wasm_read_leb128 ().  */
188
189
static uint64_t
190
wasm_read_leb128 (bfd_vma pc,
191
                  struct disassemble_info *info,
192
                  bool *error_return,
193
                  unsigned int *length_return,
194
                  bool sign)
195
18.0k
{
196
18.0k
  uint64_t result = 0;
197
18.0k
  unsigned int num_read = 0;
198
18.0k
  unsigned int shift = 0;
199
18.0k
  unsigned char byte = 0;
200
18.0k
  unsigned char lost, mask;
201
18.0k
  int status = 1;
202
203
25.1k
  while (info->read_memory_func (pc + num_read, &byte, 1, info) == 0)
204
25.0k
    {
205
25.0k
      num_read++;
206
207
25.0k
      if (shift < CHAR_BIT * sizeof (result))
208
22.6k
  {
209
22.6k
    result |= ((uint64_t) (byte & 0x7f)) << shift;
210
    /* These bits overflowed.  */
211
22.6k
    lost = byte ^ (result >> shift);
212
    /* And this is the mask of possible overflow bits.  */
213
22.6k
    mask = 0x7f ^ ((uint64_t) 0x7f << shift >> shift);
214
22.6k
    shift += 7;
215
22.6k
  }
216
2.39k
      else
217
2.39k
  {
218
2.39k
    lost = byte;
219
2.39k
    mask = 0x7f;
220
2.39k
  }
221
25.0k
      if ((lost & mask) != (sign && (int64_t) result < 0 ? mask : 0))
222
2.02k
  status |= 2;
223
224
25.0k
      if ((byte & 0x80) == 0)
225
18.0k
  {
226
18.0k
    status &= ~1;
227
18.0k
    if (sign && shift < CHAR_BIT * sizeof (result) && (byte & 0x40))
228
442
      result |= -((uint64_t) 1 << shift);
229
18.0k
    break;
230
18.0k
  }
231
25.0k
    }
232
233
18.0k
  if (length_return != NULL)
234
18.0k
    *length_return = num_read;
235
18.0k
  if (error_return != NULL)
236
18.0k
    *error_return = status != 0;
237
238
18.0k
  return result;
239
18.0k
}
240
241
/* Read a 32-bit IEEE float from PC using INFO, convert it to a host
242
   double, and store it at VALUE.  */
243
244
static int
245
read_f32 (double *value, bfd_vma pc, struct disassemble_info *info)
246
465
{
247
465
  bfd_byte buf[4];
248
249
465
  if (info->read_memory_func (pc, buf, sizeof (buf), info))
250
1
    return -1;
251
252
464
  floatformat_to_double (&floatformat_ieee_single_little, buf,
253
464
                         value);
254
255
464
  return sizeof (buf);
256
465
}
257
258
/* Read a 64-bit IEEE float from PC using INFO, convert it to a host
259
   double, and store it at VALUE.  */
260
261
static int
262
read_f64 (double *value, bfd_vma pc, struct disassemble_info *info)
263
282
{
264
282
  bfd_byte buf[8];
265
266
282
  if (info->read_memory_func (pc, buf, sizeof (buf), info))
267
4
    return -1;
268
269
278
  floatformat_to_double (&floatformat_ieee_double_little, buf,
270
278
                         value);
271
272
278
  return sizeof (buf);
273
282
}
274
275
/* Main disassembly routine.  Disassemble insn at PC using INFO.  */
276
277
int
278
print_insn_wasm32 (bfd_vma pc, struct disassemble_info *info)
279
59.8k
{
280
59.8k
  unsigned char opcode;
281
59.8k
  struct wasm32_opcode_s *op;
282
59.8k
  bfd_byte buffer[16];
283
59.8k
  void *stream = info->stream;
284
59.8k
  fprintf_ftype prin = info->fprintf_func;
285
59.8k
  struct wasm32_private_data *private_data = info->private_data;
286
59.8k
  uint64_t val;
287
59.8k
  int len;
288
59.8k
  unsigned int bytes_read;
289
59.8k
  bool error;
290
291
59.8k
  if (info->read_memory_func (pc, buffer, 1, info))
292
0
    return -1;
293
294
59.8k
  opcode = buffer[0];
295
296
4.17M
  for (op = wasm32_opcodes; op->name; op++)
297
4.15M
    if (op->opcode == opcode)
298
45.0k
      break;
299
300
59.8k
  if (!op->name)
301
14.7k
    {
302
14.7k
      prin (stream, "\t.byte 0x%02x\n", buffer[0]);
303
14.7k
      return 1;
304
14.7k
    }
305
306
45.0k
  len = 1;
307
308
45.0k
  prin (stream, "\t");
309
45.0k
  prin (stream, "%s", op->name);
310
311
45.0k
  if (op->clas == wasm_typed)
312
278
    {
313
278
      val = wasm_read_leb128 (pc + len, info, &error, &bytes_read, false);
314
278
      if (error)
315
8
  return -1;
316
270
      len += bytes_read;
317
270
      switch (val)
318
270
  {
319
5
  case BLOCK_TYPE_NONE:
320
5
    prin (stream, "[]");
321
5
    break;
322
139
  case BLOCK_TYPE_I32:
323
139
    prin (stream, "[i]");
324
139
    break;
325
50
  case BLOCK_TYPE_I64:
326
50
    prin (stream, "[l]");
327
50
    break;
328
21
  case BLOCK_TYPE_F32:
329
21
    prin (stream, "[f]");
330
21
    break;
331
41
  case BLOCK_TYPE_F64:
332
41
    prin (stream, "[d]");
333
41
    break;
334
14
  default:
335
14
    return -1;
336
270
  }
337
270
    }
338
339
45.0k
  switch (op->clas)
340
45.0k
    {
341
22.6k
    case wasm_special:
342
22.6k
    case wasm_eqz:
343
29.5k
    case wasm_binary:
344
30.2k
    case wasm_unary:
345
31.5k
    case wasm_conv:
346
35.6k
    case wasm_relational:
347
35.6k
    case wasm_drop:
348
35.6k
    case wasm_signature:
349
35.6k
    case wasm_call_import:
350
35.9k
    case wasm_typed:
351
36.0k
    case wasm_select:
352
36.0k
      break;
353
354
210
    case wasm_break_table:
355
210
      {
356
210
  uint32_t target_count, i;
357
210
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
358
210
        false);
359
210
  target_count = val;
360
210
  if (error || target_count != val || target_count == (uint32_t) -1)
361
10
    return -1;
362
200
  len += bytes_read;
363
200
  prin (stream, " %u", target_count);
364
5.84k
  for (i = 0; i < target_count + 1; i++)
365
5.68k
    {
366
5.68k
      uint32_t target;
367
5.68k
      val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
368
5.68k
            false);
369
5.68k
      target = val;
370
5.68k
      if (error || target != val)
371
39
        return -1;
372
5.64k
      len += bytes_read;
373
5.64k
      prin (stream, " %u", target);
374
5.64k
    }
375
200
      }
376
161
      break;
377
378
601
    case wasm_break:
379
792
    case wasm_break_if:
380
792
      {
381
792
  uint32_t depth;
382
792
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
383
792
        false);
384
792
  depth = val;
385
792
  if (error || depth != val)
386
28
    return -1;
387
764
  len += bytes_read;
388
764
  prin (stream, " %u", depth);
389
764
      }
390
0
      break;
391
392
173
    case wasm_return:
393
173
      break;
394
395
191
    case wasm_constant_i32:
396
558
    case wasm_constant_i64:
397
558
      val = wasm_read_leb128 (pc + len, info, &error, &bytes_read, true);
398
558
      if (error)
399
12
  return -1;
400
546
      len += bytes_read;
401
546
      prin (stream, " %" PRId64, val);
402
546
      break;
403
404
465
    case wasm_constant_f32:
405
465
      {
406
465
  double fconstant;
407
465
  int ret;
408
  /* This appears to be the best we can do, even though we're
409
     using host doubles for WebAssembly floats.  */
410
465
  ret = read_f32 (&fconstant, pc + len, info);
411
465
  if (ret < 0)
412
1
    return -1;
413
464
  len += ret;
414
464
  prin (stream, " %.9g", fconstant);
415
464
      }
416
0
      break;
417
418
282
    case wasm_constant_f64:
419
282
      {
420
282
  double fconstant;
421
282
  int ret;
422
282
  ret = read_f64 (&fconstant, pc + len, info);
423
282
  if (ret < 0)
424
4
    return -1;
425
278
  len += ret;
426
278
  prin (stream, " %.17g", fconstant);
427
278
      }
428
0
      break;
429
430
460
    case wasm_call:
431
460
      {
432
460
  uint32_t function_index;
433
460
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
434
460
        false);
435
460
  function_index = val;
436
460
  if (error || function_index != val)
437
12
    return -1;
438
448
  len += bytes_read;
439
448
  prin (stream, " ");
440
448
  private_data->section_prefix = ".space.function_index";
441
448
  (*info->print_address_func) ((bfd_vma) function_index, info);
442
448
  private_data->section_prefix = NULL;
443
448
      }
444
0
      break;
445
446
302
    case wasm_call_indirect:
447
302
      {
448
302
  uint32_t type_index, xtra_index;
449
302
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
450
302
        false);
451
302
  type_index = val;
452
302
  if (error || type_index != val)
453
10
    return -1;
454
292
  len += bytes_read;
455
292
  prin (stream, " %u", type_index);
456
292
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
457
292
        false);
458
292
  xtra_index = val;
459
292
  if (error || xtra_index != val)
460
10
    return -1;
461
282
  len += bytes_read;
462
282
  prin (stream, " %u", xtra_index);
463
282
      }
464
0
      break;
465
466
556
    case wasm_get_local:
467
1.00k
    case wasm_set_local:
468
1.21k
    case wasm_tee_local:
469
1.21k
      {
470
1.21k
  uint32_t local_index;
471
1.21k
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
472
1.21k
        false);
473
1.21k
  local_index = val;
474
1.21k
  if (error || local_index != val)
475
28
    return -1;
476
1.18k
  len += bytes_read;
477
1.18k
  prin (stream, " %u", local_index);
478
1.18k
  if (strcmp (op->name + 4, "local") == 0)
479
534
    {
480
534
      static const char *locals[] =
481
534
        {
482
534
    "$dpc", "$sp1", "$r0", "$r1", "$rpc", "$pc0",
483
534
    "$rp", "$fp", "$sp",
484
534
    "$r2", "$r3", "$r4", "$r5", "$r6", "$r7",
485
534
    "$i0", "$i1", "$i2", "$i3", "$i4", "$i5", "$i6", "$i7",
486
534
    "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
487
534
        };
488
534
      if (private_data->print_registers
489
0
    && local_index < ARRAY_SIZE (locals))
490
0
        prin (stream, " <%s>", locals[local_index]);
491
534
    }
492
654
  else
493
654
    {
494
654
      static const char *globals[] =
495
654
        {
496
654
    "$got", "$plt", "$gpo"
497
654
        };
498
654
      if (private_data->print_well_known_globals
499
0
    && local_index < ARRAY_SIZE (globals))
500
0
        prin (stream, " <%s>", globals[local_index]);
501
654
    }
502
1.18k
      }
503
0
      break;
504
505
223
    case wasm_grow_memory:
506
792
    case wasm_current_memory:
507
792
      {
508
792
  uint32_t reserved_size;
509
792
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
510
792
        false);
511
792
  reserved_size = val;
512
792
  if (error || reserved_size != val)
513
15
    return -1;
514
777
  len += bytes_read;
515
777
  prin (stream, " %u", reserved_size);
516
777
      }
517
0
      break;
518
519
1.76k
    case wasm_load:
520
3.75k
    case wasm_store:
521
3.75k
      {
522
3.75k
  uint32_t flags, offset;
523
3.75k
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
524
3.75k
        false);
525
3.75k
  flags = val;
526
3.75k
  if (error || flags != val)
527
26
    return -1;
528
3.72k
  len += bytes_read;
529
3.72k
  val = wasm_read_leb128 (pc + len, info, &error, &bytes_read,
530
3.72k
        false);
531
3.72k
  offset = val;
532
3.72k
  if (error || offset != val)
533
32
    return -1;
534
3.69k
  len += bytes_read;
535
3.69k
  prin (stream, " a=%u %u", flags, offset);
536
3.69k
      }
537
0
      break;
538
45.0k
    }
539
44.7k
  return len;
540
45.0k
}
541
542
/* Print valid disassembler options to STREAM.  */
543
544
void
545
print_wasm32_disassembler_options (FILE *stream)
546
0
{
547
0
  unsigned int i, max_len = 0;
548
549
0
  fprintf (stream, _("\
550
0
The following WebAssembly-specific disassembler options are supported for use\n\
551
0
with the -M switch:\n"));
552
553
0
  for (i = 0; i < ARRAY_SIZE (options); i++)
554
0
    {
555
0
      unsigned int len = strlen (options[i].name);
556
557
0
      if (max_len < len)
558
0
  max_len = len;
559
0
    }
560
561
0
  for (i = 0, max_len++; i < ARRAY_SIZE (options); i++)
562
0
    fprintf (stream, "  %s%*c %s\n",
563
0
       options[i].name,
564
0
       (int)(max_len - strlen (options[i].name)), ' ',
565
       _(options[i].description));
566
0
}