无法在 Linux 上编译为 .wasm

发布于 2025-01-16 03:38:26 字数 3357 浏览 0 评论 0原文

总结

(编辑:删除了所有不必要的信息,添加了Windows10体验,将Ubuntu上的CLANG升级到相同版本,由于旧的CLANG版本而删除了Debian10)

我创建了一个非常简单的WASM模块,

  • 在MacOS上编译,有效 ✅,
  • 在 MS-Windows10 上编译,有效 ✅,
  • 在 Ubuntu21.10 上编译:生成 318 字节的零 ❌,

详细信息

症状

在 Ubuntu21 上,链接器生成带有以下内容的文件长度正确,但全是零:

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

编译器还会生成一个名为 inc.wasm.tmp611a2df 的临时文件,它与结果 inc.wasm 相同,大小相同,零字节内容。

构建命令

编译命令:

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

它可以在任何平台上生成良好的目标文件(在 Ubuntu 上编译,然后在 MS-Windows10 上链接:有效)。

链接命令是(在 MS-Windows10 上,使用插入符“^”而不是反斜杠“”):

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

版本

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:

编译器版本:

$ 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

链接器版本:

$ wasm-ld --version
Ubuntu LLD 14.0.0

底线

在 Debian10 上使用 strace 和 CLANG 版本 7, 创建结果文件后它挂起,我在此处复制 strace 输出,但请记住,它是不同的版本:

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

我不知道发生了什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿忘初心 2025-01-23 03:38:27

我只是运气不好,有两个 Linux 虚拟机。一位朋友在他的 Arch Linux 上安装了 wasm-ld,并且它开箱即用。

我已将 Debian10 计算机升级到 Debian11,安装了 wasm-ld-13 并且它可以正常工作。

更新:创建了一个小项目,它展示了如何一步编译为 wasm: https://github.com/ern0/howto-wasm-minimal

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文