Search code examples
linuxclangllvmwebassembly

Can't compile to .wasm on Linux


Summary

(Edit: removed all unnecessary information, added Windows10 experience, upgraded CLANG on Ubuntu to the same version, removed Debian10 due to old CLANG version)

I've created a pretty simple WASM module,

  • compiled on MacOS, works ✅,
  • compiled on MS-Windows10, works ✅,
  • compiled on Ubuntu21.10: produces 318 bytes of zeros ❌,

Details

Symptom

On Ubuntu21, the linker produces the file with correct length, but full of zeros:

$ hexdump inc.wasm
0000000 0000 0000 0000 0000 0000 0000 0000 0000
*
0000130 0000 0000 0000 0000 0000 0000 0000
000013e

The compiler also produces a temporary file named like inc.wasm.tmp611a2df, which is identical to the result inc.wasm, same size, zero bytes content.

Build commands

The compile command:

clang++ \
    --target=wasm32 \
    -nostdlib \
    -O3 \
    -o /tmp/inc.o \
    -c \
    inc.cpp

It produces good object file on any platform (compiled on Ubuntu, then linked on MS-Windows10: works).

The link command is (on MS-Windows10, use to caret "^" instead of backslash ""):

wasm-ld \ 
    --no-entry \
    --export-all \
    --lto-O3 \
    --allow-undefined \
    --import-memory \
    /tmp/inc.o \
    -o inc.wasm

Versions

MacOS:

$ clang --version
Homebrew clang version 13.0.1
Target: x86_64-apple-darwin21.3.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

Ubuntu 21.10:

Compiler version:

$ clang --version
Ubuntu clang version 14.0.0-++20220316013357+add3ab7f4c8a-1~exp1~20220316133449.102
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Linker version:

$ wasm-ld --version
Ubuntu LLD 14.0.0

Bottom line

Used strace on Debian10 with CLANG version 7, it was hanging after creating the result file, I copy here the strace output, but remember, it's different version:

futex(0x7f514f8dc428, FUTEX_WAKE_PRIVATE, 2147483647) = 0
futex(0x7f514f8dc0c8, FUTEX_WAKE_PRIVATE, 2147483647) = 0
futex(0x7f514f8dc9d0, FUTEX_WAKE_PRIVATE, 2147483647) = 0
futex(0x7f514f8dc9d8, FUTEX_WAKE_PRIVATE, 2147483647) = 0
openat(AT_FDCWD, "/tmp/inc.o", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=1556, ...}) = 0
pread64(3, "\0asm\1\0\0\0\1\224\200\200\200\0\4`\1\177\1\177`\0\0`\2\177\177\0`\3\177\177"..., 1556, 0) = 1556
close(3)                                = 0
brk(0x1048000)                          = 0x1048000
brk(0x1046000)                          = 0x1046000
stat("inc.wasm", 0x7ffc2e13ec08)        = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/dev/urandom", O_RDONLY) = 3
read(3, "6'\242\256", 4)                = 4
close(3)                                = 0
openat(AT_FDCWD, "inc.wasm.tmp0b96c6e", O_RDWR|O_CREAT|O_EXCL|O_CLOEXEC, 0777) = 3
fallocate(3, 0, 0, 1507)                = 0
ftruncate(3, 1507)                      = 0
mmap(NULL, 1507, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0) = 0x7f514f918000
sched_getaffinity(0, 128, [0])          = 64
openat(AT_FDCWD, "/sys/devices/system/cpu/online", O_RDONLY|O_CLOEXEC) = 4
read(4, "0\n", 8192)                    = 2
close(4)                                = 0
mmap(NULL, 8392704, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_STACK, -1, 0) = 0x7f514af2b000
mprotect(0x7f514af2c000, 8388608, PROT_READ|PROT_WRITE) = 0
clone(child_stack=0x7f514b72afb0, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0x7f514b72b9d0, tls=0x7f514b72b700, child_tidptr=0x7f514b72b9d0) = 23331
futex(0x7ffc2e13eee8, FUTEX_WAIT_PRIVATE, 0, NULL

I have no idea, what's going on.


Solution

  • I was just unlucky with two of my Linux VMs. A friend installed wasm-ld on his Arch Linux, and it worked out-of-the-box.

    I've upgraded my Debian10 machine to Debian11, installed wasm-ld-13 and it just works.

    Update: created a small project, it shows how to compile to wasm in a single step: https://github.com/ern0/howto-wasm-minimal