Search code examples
webassemblyobjdump

x86 equivalent of a WebAssembly Instruction


I have been playing around with a WebAssembly program . I wanted to get the x86 equivalent of the WebAssembly program . By googling I found that objdump can be used for doing so for any object file using the command

objdump -M intel <file_name>

However the disaasembler for wasm file wasm-objdump doesn't have the flag m for dissassembling the code in x86 format for the obvious reason that it is a web assembly application .

Is there a (easy) way for mapping a instruction given in WebAssembly to an equivalent x86 instruction without explicitly matching each instruction ?


Solution

  • The online WasmExplorer compiles C code to both WebAssembly and FireFox x86, using the SpiderMonkey compiler. Given the following simple function:

    int testFunction(int* input, int length) {
      int sum = 0;
      for (int i = 0; i < length; ++i) {
        sum += input[i];
      }
      return sum;
    }
    

    Here is the x86 output:

    wasm-function[0]:
      sub rsp, 8                            ; 0x000000 48 83 ec 08
      cmp esi, 1                            ; 0x000004 83 fe 01
      jge 0x14                              ; 0x000007 0f 8d 07 00 00 00
     0x00000d:                              
      xor eax, eax                          ; 0x00000d 33 c0
      jmp 0x26                              ; 0x00000f e9 12 00 00 00
     0x000014:                              
      xor eax, eax                          ; 0x000014 33 c0
     0x000016:                              ; 0x000016 from: [0x000024]
      mov ecx, dword ptr [r15 + rdi]        ; 0x000016 41 8b 0c 3f
      add eax, ecx                          ; 0x00001a 03 c1
      add edi, 4                            ; 0x00001c 83 c7 04
      add esi, -1                           ; 0x00001f 83 c6 ff
      test esi, esi                         ; 0x000022 85 f6
      jne 0x16                              ; 0x000024 75 f0
     0x000026:                              
      nop                                   ; 0x000026 66 90
      add rsp, 8                            ; 0x000028 48 83 c4 08
      ret 
    

    You can view this example online.

    WasmExplorer compiles code into wasm / x86 via a service - you can see the scripts that are run on Github - you should be able to use these to construct a command-line tool yourself.