Ldd error "Bad section type" on trying to build rust's wasm-pack hello world example

I’m trying to follow these instructions Wasm By Example and at the build step wasm-pack build --target web fails with

error: linking with `lld` failed: exit code: 1 = note: lld: error: /home/oliver/languages/rust/moo/target/wasm32-unknown-unknown/release/deps/libwasm_bindgen-5ac5c850f6cafa91.rlib(wasm_bindgen-5ac5c850f6cafa91.wasm_bindgen.82hlkbcu-cgu.2.rcgu.o): Bad section type

Caused by: failed to execute cargo build: exited with exit code: 101

Rerunning the failing command verbosely cargo build --verbose --lib --release --target wasm32-unknown-unknown gives

Caused by: process didn't exit successfully: rustc --crate-name moo --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type cdylib --emit=dep-info,link -C opt-level=3 -Cembed-bitcode=no -C metadata=34a0842d797d0355 --out-dir /home/oliver/languages/rust/moo/target/wasm32-unknown-unknown/release/deps --target wasm32-unknown-unknown -L dependency=/home/oliver/languages/rust/moo/target/wasm32-unknown-unknown/release/deps -L dependency=/home/oliver/languages/rust/moo/target/release/deps --extern wasm_bindgen=/home/oliver/languages/rust/moo/target/wasm32-unknown-unknown/release/deps/libwasm_bindgen-5ac5c850f6cafa91.rlib -C linker=lld (exit code: 1)

Should i keep trying, or give up – has anyone managed to run wasm-pack successfully?

This is the shell.nix file i’m using, i assume the problem lies here? https://pastebin.com/MVunuFKB

Your linker version is quite old, maybe using llvmPackages_10.lld instead of llvmPackages_8.lld will resolve your issue. I haven’t tested it and am not super familiar with WASM, though.

If that doesn’t fix it, perhaps WASM requires a custom linker?

Also, where do you get wasm-pack from? If it’s from cargo install, consider using the version from nixpkgs instead.

1 Like

Actually, I just remembered that I have a wasm project which did build for me using nix laying around on my hard drive. I’ve pushed it to github under GitHub - casept/chip8-oxidized at nix now, maybe you can find something there that may help you.

The main differences to your setup is that I use nixpkgs.wasm-pack and Rust from the mozilla overlay with a wasm target added rather than from nixpkgs.


Switching to llvmPackages_10.lld lets the example finish building, although no output is produced (the pkg/ directory is empty) and it then fails while trying to link wasm-bindgen-cli v0.2.67.

I’m using nixpkgs.wasm-pack already, but will try the mozilla overlay you mention – i did see that in the manual but assumed it was only needed if one wanted to use the ‘nightly’ version of rust.

Thanks for confirming success is possible, at least!

Using the ‘nightly’ version of rustc get me back to one of the first errors i had:

[INFO]: Checking for the Wasm target... Error: wasm32-unknown-unknown target not found in sysroot: "/nix/store/3h59wdgdz5pmkhnscj30rwyqscqfx07n-rust-1.45.2-2020-07-31-d3fb005a3"

Used rustc from the following path: "/home/oliver/.nix-profile/bin/rustc" It looks like Rustup is not being used. For non-Rustup setups, the wasm32-unknown-unknown target needs to be installed manually. See https://rustwasm.github.io/wasm-pack/book/prerequisites/non-rustup-setups.html on how to do this.

I couldn’t work out how to follow the instructions there (i.e., even get as far as the person in this thread ), hence my original shell.nix file.

ETA: the problem is in copying the downloaded rust-std-1.45.2-wasm32-unknown-unknown into rustc's libs directory, because obviously that wants to be generated declaratively and i don’t know how:

cp: can't create directory '/nix/store/3h59wdgdz5pmkhnscj30rwyqscqfx07n-rust-1.45.2-2020-07-31-d3fb005a3/lib/rustlib/wasm32-unknown-unknown': Read-only file system

The mozilla overlay is basically the recommended way to install project-local Rust, because it gives you much better control over the version (the Rust stable in nixpkgs is often outdated) and allows for easy addition of targets (as demonstrated in the nix/rust.nix file in my repo).

Regarding your second post: Could you try simply copying over the nix/ directory and shell.nix file from my repo into yours and seeing whether your project builds in the nix-shell spawned by that?

Yes, copying your example works, thanks for the demo.

1 Like