Unable to build shared Wasm lib because core is not built with -fPIC?

Reproducer is here: https://github.com/osa1/wasm_test/tree/master/rs_nostd_core_lib_2

To reproduce locally make sure you have clang-10 installed and then run ./build.sh. The output is as shown in the README. As also described in the README, I think the problem is core library for wasm32 (tried with both wasm32-unknown-emscripten and wasm32-wasi) is not built with PIC relocation model, so as soon as I start using stuff from core I get "recompile with -fPIC" errors. If you look at build.sh, there's a version of the same commands for native compilation, and it works, so my guess is the problem is specific to wasm32 libraries.

My questions are:

  • Am I right that there's an issue in core for the wasm32 targets (emscripten and wasi).
  • If yes, is there an easy way for me to build rustc with core (and possibly std too) built with PIC relocation model?
  • If no, do you know what the problem may be? It's interesting that I can build this just fine unless I use functions from core. Note that using types is not a problem, e.g. I can use PanicInfo just fine.

Thanks.

EDIT: This isn't related to no_std, if I remove no_std the build fails the same way (though symbol names change IIRC).

In case anyone's wondering, with help from rust-lang zulip I solved this. Basically I need to build with xargo with this .cargo/config:

[build]
rustflags = ["-Crelocation-model=pic"]

then build the package with

xargo rustc --target=wasm32-unknown-emscripten -v -- -Crelocation-model=pic

With this both the package and core/std have PIC relocation model and it all works!

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.