1 /* Opcode printing code for the WebAssembly target
2 Copyright (C) 2017 Free Software Foundation, Inc.
4 This file is part of libopcodes.
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.
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.
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. */
24 #include "safe-ctype.h"
25 #include "floatformat.h"
27 #include "libiberty.h"
29 #include "elf/internal.h"
30 #include "elf/wasm32.h"
33 /* Type names for blocks and signatures. */
34 #define BLOCK_TYPE_NONE 0x40
35 #define BLOCK_TYPE_I32 0x7f
36 #define BLOCK_TYPE_I64 0x7e
37 #define BLOCK_TYPE_F32 0x7d
38 #define BLOCK_TYPE_F64 0x7c
72 struct wasm32_private_data
74 bfd_boolean print_registers
;
75 bfd_boolean print_well_known_globals
;
77 /* Limit valid symbols to those with a given prefix. */
78 const char *section_prefix
;
84 const char *description
;
87 static const wasm32_options_t options
[] =
89 { "registers", N_("Disassemble \"register\" names") },
90 { "globals", N_("Name well-known globals") },
93 #define WASM_OPCODE(opcode, name, intype, outtype, clas, signedness) \
94 { name, wasm_ ## clas, opcode },
96 struct wasm32_opcode_s
100 unsigned char opcode
;
103 #include "opcode/wasm.h"
107 /* Parse the disassembler options in OPTS and initialize INFO. */
110 parse_wasm32_disassembler_options (struct disassemble_info
*info
,
113 struct wasm32_private_data
*private = info
->private_data
;
117 if (CONST_STRNEQ (opts
, "registers"))
118 private->print_registers
= TRUE
;
119 else if (CONST_STRNEQ (opts
, "globals"))
120 private->print_well_known_globals
= TRUE
;
122 opts
= strchr (opts
, ',');
128 /* Check whether SYM is valid. Special-case absolute symbols, which
129 are unhelpful to print, and arguments to a "call" insn, which we
130 want to be in a section matching a given prefix. */
133 wasm32_symbol_is_valid (asymbol
*sym
,
134 struct disassemble_info
*info
)
136 struct wasm32_private_data
*private_data
= info
->private_data
;
141 if (strcmp(sym
->section
->name
, "*ABS*") == 0)
144 if (private_data
&& private_data
->section_prefix
!= NULL
145 && strncmp (sym
->section
->name
, private_data
->section_prefix
,
146 strlen (private_data
->section_prefix
)))
152 /* Initialize the disassembler structures for INFO. */
155 disassemble_init_wasm32 (struct disassemble_info
*info
)
157 if (info
->private_data
== NULL
)
159 static struct wasm32_private_data
private;
161 private.print_registers
= FALSE
;
162 private.print_well_known_globals
= FALSE
;
163 private.section_prefix
= NULL
;
165 info
->private_data
= &private;
168 if (info
->disassembler_options
)
170 parse_wasm32_disassembler_options (info
, info
->disassembler_options
);
172 info
->disassembler_options
= NULL
;
175 info
->symbol_is_valid
= wasm32_symbol_is_valid
;
178 /* Read an LEB128-encoded integer from INFO at address PC, reading one
179 byte at a time. Set ERROR_RETURN if no complete integer could be
180 read, LENGTH_RETURN to the number oof bytes read (including bytes
181 in incomplete numbers). SIGN means interpret the number as
182 SLEB128. Unfortunately, this is a duplicate of wasm-module.c's
183 wasm_read_leb128 (). */
186 wasm_read_leb128 (bfd_vma pc
,
187 struct disassemble_info
* info
,
188 bfd_boolean
* error_return
,
189 unsigned int * length_return
,
193 unsigned int num_read
= 0;
194 unsigned int shift
= 0;
195 unsigned char byte
= 0;
196 bfd_boolean success
= FALSE
;
198 while (info
->read_memory_func (pc
+ num_read
, &byte
, 1, info
) == 0)
202 result
|= ((bfd_vma
) (byte
& 0x7f)) << shift
;
205 if ((byte
& 0x80) == 0)
212 if (length_return
!= NULL
)
213 *length_return
= num_read
;
214 if (error_return
!= NULL
)
215 *error_return
= ! success
;
217 if (sign
&& (shift
< 8 * sizeof (result
)) && (byte
& 0x40))
218 result
|= -((uint64_t) 1 << shift
);
223 /* Read a 32-bit IEEE float from PC using INFO, convert it to a host
224 double, and store it at VALUE. */
227 read_f32 (double *value
, bfd_vma pc
, struct disassemble_info
*info
)
231 if (info
->read_memory_func (pc
, buf
, sizeof (buf
), info
))
234 floatformat_to_double (&floatformat_ieee_single_little
, buf
,
240 /* Read a 64-bit IEEE float from PC using INFO, convert it to a host
241 double, and store it at VALUE. */
244 read_f64 (double *value
, bfd_vma pc
, struct disassemble_info
*info
)
248 if (info
->read_memory_func (pc
, buf
, sizeof (buf
), info
))
251 floatformat_to_double (&floatformat_ieee_double_little
, buf
,
257 /* Main disassembly routine. Disassemble insn at PC using INFO. */
260 print_insn_wasm32 (bfd_vma pc
, struct disassemble_info
*info
)
262 unsigned char opcode
;
263 struct wasm32_opcode_s
*op
;
265 void *stream
= info
->stream
;
266 fprintf_ftype prin
= info
->fprintf_func
;
267 struct wasm32_private_data
*private_data
= info
->private_data
;
268 long long constant
= 0;
269 double fconstant
= 0.0;
274 long target_count
= 0;
278 unsigned int bytes_read
= 0;
280 const char *locals
[] =
282 "$dpc", "$sp1", "$r0", "$r1", "$rpc", "$pc0",
284 "$r2", "$r3", "$r4", "$r5", "$r6", "$r7",
285 "$i0", "$i1", "$i2", "$i3", "$i4", "$i5", "$i6", "$i7",
286 "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7",
288 int nlocals
= ARRAY_SIZE (locals
);
289 const char *globals
[] =
291 "$got", "$plt", "$gpo"
293 int nglobals
= ARRAY_SIZE (globals
);
294 bfd_boolean error
= FALSE
;
296 if (info
->read_memory_func (pc
, buffer
, 1, info
))
301 for (op
= wasm32_opcodes
; op
->name
; op
++)
302 if (op
->opcode
== opcode
)
307 prin (stream
, "\t.byte 0x%02x\n", buffer
[0]);
315 prin (stream
, "%s", op
->name
);
317 if (op
->clas
== wasm_typed
)
319 block_type
= wasm_read_leb128
320 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
326 case BLOCK_TYPE_NONE
:
330 prin (stream
, "[i]");
333 prin (stream
, "[l]");
336 prin (stream
, "[f]");
339 prin (stream
, "[d]");
351 case wasm_relational
:
354 case wasm_call_import
:
359 case wasm_break_table
:
360 target_count
= wasm_read_leb128
361 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
365 prin (stream
, " %ld", target_count
);
366 for (i
= 0; i
< target_count
+ 1; i
++)
369 target
= wasm_read_leb128
370 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
374 prin (stream
, " %ld", target
);
380 depth
= wasm_read_leb128
381 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
385 prin (stream
, " %ld", depth
);
391 case wasm_constant_i32
:
392 case wasm_constant_i64
:
393 constant
= wasm_read_leb128
394 (pc
+ len
, info
, &error
, &bytes_read
, TRUE
);
398 prin (stream
, " %lld", constant
);
401 case wasm_constant_f32
:
402 /* This appears to be the best we can do, even though we're
403 using host doubles for WebAssembly floats. */
404 ret
= read_f32 (&fconstant
, pc
+ len
, info
);
408 prin (stream
, " %.*g", DECIMAL_DIG
, fconstant
);
411 case wasm_constant_f64
:
412 ret
= read_f64 (&fconstant
, pc
+ len
, info
);
416 prin (stream
, " %.*g", DECIMAL_DIG
, fconstant
);
420 index
= wasm_read_leb128
421 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
426 private_data
->section_prefix
= ".space.function_index";
427 (*info
->print_address_func
) ((bfd_vma
) index
, info
);
428 private_data
->section_prefix
= NULL
;
431 case wasm_call_indirect
:
432 constant
= wasm_read_leb128
433 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
437 prin (stream
, " %lld", constant
);
438 constant
= wasm_read_leb128
439 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
443 prin (stream
, " %lld", constant
);
449 constant
= wasm_read_leb128
450 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
454 prin (stream
, " %lld", constant
);
455 if (strcmp (op
->name
+ 4, "local") == 0)
457 if (private_data
->print_registers
458 && constant
>= 0 && constant
< nlocals
)
459 prin (stream
, " <%s>", locals
[constant
]);
463 if (private_data
->print_well_known_globals
464 && constant
>= 0 && constant
< nglobals
)
465 prin (stream
, " <%s>", globals
[constant
]);
469 case wasm_grow_memory
:
470 case wasm_current_memory
:
471 constant
= wasm_read_leb128
472 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
476 prin (stream
, " %lld", constant
);
481 flags
= wasm_read_leb128
482 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
486 offset
= wasm_read_leb128
487 (pc
+ len
, info
, &error
, &bytes_read
, FALSE
);
491 prin (stream
, " a=%ld %ld", flags
, offset
);
497 /* Print valid disassembler options to STREAM. */
500 print_wasm32_disassembler_options (FILE *stream
)
502 unsigned int i
, max_len
= 0;
504 fprintf (stream
, _("\
505 The following WebAssembly-specific disassembler options are supported for use\n\
506 with the -M switch:\n"));
508 for (i
= 0; i
< ARRAY_SIZE (options
); i
++)
510 unsigned int len
= strlen (options
[i
].name
);
516 for (i
= 0, max_len
++; i
< ARRAY_SIZE (options
); i
++)
517 fprintf (stream
, " %s%*c %s\n",
519 (int)(max_len
- strlen (options
[i
].name
)), ' ',
520 _(options
[i
].description
));