Repository: rust
Issue: staticlib for wasm32 targets have missing dependencies
Labels: A-linkage,T-compiler,O-wasm,C-bug

```
~ $ rustc --version --verbose
rustc 1.46.0-nightly (346aec9b0 2020-07-11)
binary: rustc
commit-hash: 346aec9b02f3c74f3fce97fd6bda24709d220e49
commit-date: 2020-07-11
host: x86_64-unknown-linux-gnu
release: 1.46.0-nightly
LLVM version: 10.0

~ $ cargo --version --verbose
cargo 1.46.0-nightly (4f74d9b2a 2020-07-08)
release: 1.46.0
commit-hash: 4f74d9b2a771c58b7ef4906b2668afd075bc8081
commit-date: 2020-07-08

~ $ xargo --version --verbose
xargo 0.3.22
+ "cargo" "--version" "--verbose"
cargo 1.46.0-nightly (4f74d9b2a 2020-07-08)
release: 1.46.0
commit-hash: 4f74d9b2a771c58b7ef4906b2668afd075bc8081
commit-date: 2020-07-08
```

(I asked this on Zulip 4 days ago but got no answers, and I think this is a bug somewhere in the toolchain, so posting here with more details)

A "staticlib" should include all dependencies of the Rust crate and should be linkable with libraries and executables generated by other languages. However it seems like this currently does not hold.

Here's an [example][1]. To run, make sure you have xargo, llvm-10, and [wabt][2] installed (xargo is needed because core and std for wasm32 are not built with PIC relocation model and can't be used for creating shared libraries) and run `./build.sh`. It'll generate a `libfinal.wat` file. If you open it in your editor you'll see this line:

```
(import "GOT.func" "_ZN4core3fmt3num3imp52_$LT$impl$u20$core..fmt..Display$u20$for$u20$i32$GT$3fmt17h9ba9fea9cadf7bd5E" (global (;3;) (mut i32)))
```

This is a Rust dependency that is not included in the final library and imported. Because we're using "staticlib" crate type this should not happen: all dependencies should be in the library already and there shouldn't be any imports.

I should also note that I can do the same (compile Rust crate to staticlib, link it with C code to a shared library) for x86_64 target.

I tried to understand where does this import come from. If I unpack the generated `.a` file and generate `.wat` for the `.o` files I see that the function is defined in a file called `core-b5d07d1a52bf60be.core.au5sshbb-cgu.4.rcgu.o`. I guess the "core" here means it's defined in "core" rather than in my code. Then there's another file called `rs_nostd_core_lib_1-4e8f00c808934bbd.rs_nostd_core_lib_1.dk7ycmpp-cgu.2.rcgu.o.wat`, which I think is generated for my code, which imports this function both from "env" (correct) and from "func.GOT" (incorrect). The relevant lines from the file:

```wat
(module
  ...
  (import "env" "_ZN4core3fmt3num3imp52_$LT$impl$u20$core..fmt..Display$u20$for$u20$i32$GT$3fmt17h9ba9fea9cadf7bd5E" (func (;4;) (type 1)))
  ...
  (import "GOT.func" "_ZN4core3fmt3num3imp52_$LT$impl$u20$core..fmt..Display$u20$for$u20$i32$GT$3fmt17h9ba9fea9cadf7bd5E" (global (;2;) (mut i32)))
  ...)
```

This is the same function imported twice, from different modules. I think the bug is in the `GOT.func` line.

I'd be happy to help debugging or debug it myself if anyone gives me pointers on where to start.

[1]: https://github.com/osa1/wasm_test/tree/master/rs_nostd_core_lib_2
[2]: https://github.com/WebAssembly/wabt