I encounter something strange. Consider this file (main.zig):
export fn add(a: i32, b: i32) i32 {
return a + b;
}
If I compile it with:
zig build-obj main.zig -target x86_64-freestanding
dwarfdump gives me this line number result:
Address Line Column File ISA Discriminator Flags
------------------ ------ ------ ------ --- ------------- -------------
0x0000000000000000 1 0 4 0 0 is_stmt
0x0000000000000008 2 3 4 0 0 is_stmt prologue_end
0x0000000000000040 767 0 1 0 0 is_stmt
0x0000000000000044 788 17 1 0 0 is_stmt prologue_end
0x0000000000000047 788 17 1 0 0 is_stmt end_sequence
Looking at objdump:
0: 55 push %rbp
1: 48 89 e5 mov %rsp,%rbp
4: 48 83 ec 10 sub $0x10,%rsp
8: 01 f7 add %esi,%edi
a: 89 7d f8 mov %edi,-0x8(%rbp)
d: 0f 90 45 fc seto -0x4(%rbp)
This seems about right. The addition is right at address 0x8.
However, if I compile with:
zig build-lib main.zig -target wasm32-freestanding -dynamic
Here is the line number result:
Address Line Column File ISA Discriminator Flags
------------------ ------ ------ ------ --- ------------- -------------
0x0000000000000003 1 0 5 0 0 is_stmt
0x0000000000000025 2 3 5 0 0 is_stmt prologue_end
0x0000000000000098 0 3 5 0 0
0x0000000000000099 2 3 5 0 0
0x00000000000000b6 0 3 5 0 0
0x00000000000000b7 2 3 5 0 0
0x00000000000000d5 2 3 5 0 0 end_sequence
0x00000000000000d6 767 0 1 0 0 is_stmt
0x00000000000000d7 788 17 1 0 0 is_stmt prologue_end
0x00000000000000dc 0 17 1 0 0
0x00000000000000de 0 17 1 0 0 end_sequence
Looking at the dump with wasm-objdump:
000092: 21 0b | local.set 11
000094: 20 04 | local.get 4
000096: 20 0b | local.get 11
000098: 3a 00 0c | i32.store8 0 12
00009b: 20 04 | local.get 4
00009d: 2d 00 0c | i32.load8_u 0 12
0000a0: 21 0c | local.set 12
0000a2: 41 00 | i32.const 0
0000a4: 21 0d | local.set 13
Address 0x99 does not seems to fit. The actual add
instruction is actually at address 0x71.
It gets worst if I start doing more complicated things like:
const std = @import("std");
export fn add(a: i32, b: i32) i32 {
return a + b;
}
The return statement in the line number result is pointing to code in the panic handler. I am trying to make Firefox debug WASM code and it’s having trouble. I believe this is because of some bugs in the DWARF symbols for WASM. Am I way off here? Did I just misunderstood how this works?