hi all! Not sure if this is the right place to ask; feel free to tell me that it's not.
I'm trying to understand what the import_module
and import_name
attributes do (in C/clang, as related to clang's WASI support). I'm trying to understand why the following code works, but it fails to link with an undefined symbol error if I changed FN_NAME
to any other value:
#define FN_NAME __wasi_random_get unsigned short FN_NAME( unsigned char * buf, unsigned long buf_len ) __attribute__(( import_module("wasi_snapshot_preview1"), import_name("random_get") )); int main(int argv, const char** argc) { int value; FN_NAME((unsigned char*)&value, sizeof(value)); return value; }
Hmm, just checked your code with changed FN_NAME. @Adam Seering what do you mean by linking and can you post error message?
FWIW the code looks right, and the attributes work as defined at https://clang.llvm.org/docs/AttributeReference.html#import-module
hm... So, the error looks like this:
wasm-ld: error: /tmp/exec-f70f85.o: undefined symbol: __my_wasi_random_get clang: error: linker command failed with exit code 1 (use -v to see invocation)
but I just tried reproducing outside of my simple Makefile and I no longer get the error. So something odd is going on.
ok, here's a build command that reproduces the error if I change FN_NAME to __my_wasi_random_get
:
$ clang exec.c -o exec.wasm --target=wasm32-wasi --sysroot $SYSROOT
in case it matters:
$ clang --version clang version 10.0.0-2ubuntu2 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin
/me uses clang 11 and relatively new wasi-sdk (so does not need --target --sysroot)
so $WASI_PREFIX/bin/clang t2.c -v --target=wasm32-wasi --sysroot $WASI_PREFIX/share/wasi-sysroot
works
ah, ok. I'm using stock clang from Ubuntu 20.04. I just tried upgrading to clang 11 from apt.llvm.org and hit the same issue.
what build of clang are you using? Is it conveniently available somewhere?
I'm building from https://github.com/WebAssembly/wasi-sdk
I just tried clang from https://github.com/WebAssembly/wasi-sdk/releases/tag/wasi-sdk-10 (the .deb package). Same issue. I'll try building from source next.
(I'll have to try in a few minutes; my machine is busy building something else right now and I'm short on RAM :-) )
@Adam Seering maybe this one relevant, and looks like it was not present for 10
P.S. https://github.com/llvm/llvm-project/commit/06f1a5c9c283838b8ed1d16961e41462371cd61f#diff-abe4ab7e41e954d1782a8ea33d0504f9
use -Wl,--allow-undefined-file=myimports.txt
, I guess
ah, ok. Do you know offhand if that commit was in 11.0? I'm happy to upgrade but I still get the same error there.
Probably not, based on https://github.com/llvm/llvm-project/commits/release/10.x/lld/wasm
thanks @Yury Delendik for your help yesterday! I tried building wasi-sdk master from source but I realized that it references a commit from the llvm repo that doesn't contain the commit/changes above. I just tried bumping to llvm master.
(still building, will be for a while)
I wonder if adding --allow-undefined-file
linker option will help with older llvm's
hm... @Yury Delendik llvm latest master fails with:
wasm-ld: error: cannot open /opt/wasi-sdk/lib/clang/11.0.0/lib/wasi/libclang_rt.builtins-wasm32.a: No such file or directory clang-11: error: linker command failed with exit code 1 (use -v to see invocation)
I'm curious, what version did you build?
anyway, -Wl,--allow-undefined-file=<file>
does work with existing compilers/builds. Thanks for the suggestion!
Locally I have wasi-sdk@79e5760710d which pointed to my local somewhat recent (cannot tell) llvm-project
hm, for me that's this commit, is it the same for you? I just tried building it to see what happens:
wasi-sdk$ (cd src/llvm-project && git log -n 1) commit c1a0a213378a458fbea1a5c77b315c7dce08fd05 (HEAD, tag: llvmorg-9.0.1-rc3, tag: llvmorg-9.0.1, origin/release/9.x) Author: Tom Stellard <tstellar@redhat.com> Date: Wed Nov 27 19:44:20 2019 +0000 (...)
Last updated: Nov 22 2024 at 17:03 UTC