michaeleisel/zld

Support Rust wasm linking? `note: "rust-lld" "-flavor" "wasm"`

Closed this issue · 5 comments

Hello, I'm using zld with my rust setup, so far so good I've this in my ~/.cargo/config.toml file

[target.x86_64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/Users/shady/.cargo/bin/zld"]

when building one of my projects, that also generates wasm code, it fails with the following error:

Compiling wasm-test v1.0.0 (/var/folders/1q/rvg2xqc1049dtn0vkt3b7m8w0000gn/T/.tmpRi8Y6v)
  error: linking with `rust-lld` failed: exit status: 1
    |
    = note: "rust-lld" "-flavor" "wasm" "--rsp-quoting=posix" "--export=__heap_base" "--export=__data_end" "-z" "stack-size=1048576" "--stack-first" "--allow-undefined" "--fatal-warnings" "--no-demangle" "--no-entry" "--export-dynamic" "/var/folders/1q/rvg2xqc1049dtn0vkt3b7m8w0000gn/T/.tmpRi8Y6v/target/wasm32-unknown-unknown/debug/deps/wasm_test.2lzlz8aqyfr4a48y.rcgu.o" "/var/folders/1q/rvg2xqc1049dtn0vkt3b7m8w0000gn/T/.tmpRi8Y6v/target/wasm32-unknown-unknown/debug/deps/wasm_test.4hg0komcjtottglt.rcgu.o" "-L" "/var/folders/1q/rvg2xqc1049dtn0vkt3b7m8w0000gn/T/.tmpRi8Y6v/target/wasm32-unknown-unknown/debug/deps" "-L" "/var/folders/1q/rvg2xqc1049dtn0vkt3b7m8w0000gn/T/.tmpRi8Y6v/target/debug/deps" "-L" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libstd-b236a57c21387b16.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libpanic_abort-0592031b65d9b9dc.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libdlmalloc-bcaeb89600b2d568.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libstd_detect-3a4a7eb791f893eb.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/librustc_demangle-ed5d049f80c21cf0.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libhashbrown-29401705131a5d1e.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/librustc_std_workspace_alloc-2449ff2aab411d5b.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libunwind-4e0f5eaccad6e16f.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libcfg_if-8a6fc3c84b246983.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/liblibc-c64e21091c940b4a.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/liballoc-6bbf5d485df4d727.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/librustc_std_workspace_core-10bf4b0039d4de0f.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libcore-3365b6c50e94718e.rlib" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/libcompiler_builtins-4c6e1466f9f1beb8.rlib" "-L" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib" "-L" "/Users/shady/.rustup/toolchains/nightly-2021-09-10-x86_64-apple-darwin/lib/rustlib/wasm32-unknown-unknown/lib/self-contained" "-o" "/var/folders/1q/rvg2xqc1049dtn0vkt3b7m8w0000gn/T/.tmpRi8Y6v/target/wasm32-unknown-unknown/debug/deps/wasm_test.wasm" "--gc-sections" "--no-entry" "-O0" "-fuse-ld=/Users/shady/.cargo/bin/zld"
    = note: rust-lld: error: unknown argument: -fuse-ld=/Users/shady/.cargo/bin/zld
            

  error: could not compile `wasm-test` due to previous error

Any idea?

Thank you.

Zld is a Mach-O linker. Wasm uses a fundamentally different object format which requires an entirely different linker. By default rustc uses the lld shipped with rustc (rust-lld) for it. I don't know why your -fuse-ld argument is passed to rust-lld on wasm. Your config.toml should only pass it for macOS.

Yes, that's what is confusing me to right now, and no idea what is going on to be honest.

The wasm build btw happens from the build.rs file here: https://github.com/webb-tools/protocol-substrate/blob/main/standalone/runtime/build.rs which uses https://lib.rs/crates/substrate-wasm-builder in it.

I will try to override the linker in the build.rs and see if that would work for me

It seems like substrate-wasm-builder inherits the RUSTFLAGS used for building the host crates when building the wasm crate. Looking at it's source, it seems like it intentionally unsets RUSTFLAGS to prevent exactly this though.

I'm currently looking into this method, where I can add rust-flags here https://docs.rs/substrate-wasm-builder/4.0.0/substrate_wasm_builder/struct.WasmBuilder.html#method.append_to_rust_flags

by calling append_to_rust_flags("-C link-arg=-fuse-ld=ld") before the .build() to overwrite it, but no luck.

Any idea?

it's fixed upstream now, thank you.