Mapping WebAssembly binary to its source code

Viewed 91

Compiling C/C++ code with the -g flag results in debug information in the produced binary file. In particular, there is a mapping of source code to binary code:

add:     file format elf64-x86-64


Disassembly of section .text:

0000000000000000 <add>:
int add(int a, int b) {
   0:   55                      push   %rbp
   1:   48 89 e5                mov    %rsp,%rbp
   4:   89 7d fc                mov    %edi,-0x4(%rbp)
   7:   89 75 f8                mov    %esi,-0x8(%rbp)
        return a + b;
   a:   8b 45 fc                mov    -0x4(%rbp),%eax
   d:   03 45 f8                add    -0x8(%rbp),%eax
  10:   5d                      pop    %rbp
  11:   c3                      retq

Is there something similar for compiling WebAssembly with clang or emcc? I tried the -g flag with these but it doesn't have the same behavior. Ultimately, I'd like a mapping of the .wat code to the C code.

1 Answers

llvm-objdump -S should work in the same way that it does for native object files.

If you are looking for nice display of code that lacks debug info you might also want to take a look at wasm-decompile which is part of the wabt project. Its able to do a much better job of making something readable than normal/native decompilers.

Related