Stream: general

Topic: importing modules


view this post on Zulip Adam Seering (Apr 10 2020 at 22:10):

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;
}

view this post on Zulip Yury Delendik (Apr 10 2020 at 22:15):

Hmm, just checked your code with changed FN_NAME. @Adam Seering what do you mean by linking and can you post error message?

view this post on Zulip Yury Delendik (Apr 10 2020 at 22:19):

FWIW the code looks right, and the attributes work as defined at https://clang.llvm.org/docs/AttributeReference.html#import-module

view this post on Zulip Adam Seering (Apr 10 2020 at 22:20):

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)

view this post on Zulip Adam Seering (Apr 10 2020 at 22:21):

but I just tried reproducing outside of my simple Makefile and I no longer get the error. So something odd is going on.

view this post on Zulip Adam Seering (Apr 10 2020 at 22:23):

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

view this post on Zulip Adam Seering (Apr 10 2020 at 22:28):

in case it matters:

$ clang --version
clang version 10.0.0-2ubuntu2
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

view this post on Zulip Yury Delendik (Apr 10 2020 at 22:31):

/me uses clang 11 and relatively new wasi-sdk (so does not need --target --sysroot)

view this post on Zulip Yury Delendik (Apr 10 2020 at 22:35):

so $WASI_PREFIX/bin/clang t2.c -v --target=wasm32-wasi --sysroot $WASI_PREFIX/share/wasi-sysroot works

view this post on Zulip Adam Seering (Apr 10 2020 at 22:39):

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.

view this post on Zulip Adam Seering (Apr 10 2020 at 22:40):

what build of clang are you using? Is it conveniently available somewhere?

view this post on Zulip Yury Delendik (Apr 10 2020 at 22:42):

I'm building from https://github.com/WebAssembly/wasi-sdk

WASI-enabled WebAssembly C/C++ toolchain. Contribute to WebAssembly/wasi-sdk development by creating an account on GitHub.

view this post on Zulip Adam Seering (Apr 10 2020 at 22:45):

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.

WASI-enabled WebAssembly C/C++ toolchain. Contribute to WebAssembly/wasi-sdk development by creating an account on GitHub.

view this post on Zulip Adam Seering (Apr 10 2020 at 22:46):

(I'll have to try in a few minutes; my machine is busy building something else right now and I'm short on RAM :-) )

view this post on Zulip Yury Delendik (Apr 10 2020 at 22:48):

@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

…fined at link time. Differential Revision: https://reviews.llvm.org/D74110

view this post on Zulip Yury Delendik (Apr 10 2020 at 22:49):

use -Wl,--allow-undefined-file=myimports.txt , I guess

view this post on Zulip Adam Seering (Apr 10 2020 at 22:53):

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.

view this post on Zulip Yury Delendik (Apr 10 2020 at 22:57):

Probably not, based on https://github.com/llvm/llvm-project/commits/release/10.x/lld/wasm

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Note: the repository does not accept github pull requests at this moment. Please submit your patches at...

view this post on Zulip Adam Seering (Apr 11 2020 at 15:14):

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.

view this post on Zulip Adam Seering (Apr 11 2020 at 15:14):

(still building, will be for a while)

view this post on Zulip Yury Delendik (Apr 11 2020 at 15:26):

I wonder if adding --allow-undefined-file linker option will help with older llvm's

view this post on Zulip Adam Seering (Apr 11 2020 at 15:52):

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?

view this post on Zulip Adam Seering (Apr 11 2020 at 16:07):

anyway, -Wl,--allow-undefined-file=<file> does work with existing compilers/builds. Thanks for the suggestion!

view this post on Zulip Yury Delendik (Apr 11 2020 at 16:36):

Locally I have wasi-sdk@79e5760710d which pointed to my local somewhat recent (cannot tell) llvm-project

view this post on Zulip Adam Seering (Apr 11 2020 at 22:28):

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