rust-cross / cargo-zigbuild

Compile Cargo project with zig as linker
MIT License
1.56k stars 57 forks source link

error: unsupported linker arg: --undefined #162

Open NobodyXu opened 1 year ago

NobodyXu commented 1 year ago

In CI of https://github.com/cargo-bins/cargo-quickinstall/pull/225 :

error: linking with `/home/runner/.cache/cargo-zigbuild/0.17.1/zigcc-aarch64-unknown-linux-gnu.sh` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/usr/lib/llvm-17/bin:/home/runner/.local/bin:/opt/pipx_bin:/home/runner/.cargo/bin:/home/runner/.config/composer/vendor/bin:/usr/local/.ghcup/bin:/home/runner/.dotnet/tools:/snap/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" VSLANG="1033" "/home/runner/.cache/cargo-zigbuild/0.17.1/zigcc-aarch64-unknown-linux-gnu.sh" "/tmp/rustcyQOQeV/symbols.o" "/tmp/cargo-installYEv6Xb/aarch64-unknown-linux-gnu/release/deps/cargo_quickinstall-f5fa84c441e696c6.cargo_quickinstall.b0e05ecc437846b8-cgu.0.rcgu.o" "-Wl,--as-needed" "-L" "/tmp/cargo-installYEv6Xb/aarch64-unknown-linux-gnu/release/deps" "-L" "/tmp/cargo-installYEv6Xb/release/deps" "-L" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib/libcompiler_builtins-c1be0fe9bd14e073.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib" "-o" "/tmp/cargo-installYEv6Xb/aarch64-unknown-linux-gnu/release/deps/cargo_quickinstall-f5fa84c441e696c6" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs" "/tmp/cargo-installYEv6Xb/aarch64-unknown-linux-gnu/release/deps/cargo_quickinstall_audit_data.o" "-Wl,--undefined=AUDITABLE_VERSION_INFO"
  = note: error: unsupported linker arg: --undefined

It runs the command:

export CARGO_ROOT=/tmp/tmp.NcnCOgGQVC
export CARGO_PROFILE_RELEASE_CODEGEN_UNITS=1
export CARGO_PROFILE_RELEASE_LTO=fat
export OPENSSL_STATIC=1
export CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse

cargo-auditable auditable install cargo-quickinstall --version 0.2.9 --target aarch64-unknown-linux-gnu --root /tmp/tmp.NcnCOgGQVC
messense commented 1 year ago

See https://github.com/ziglang/zig/pull/11906, consider open a PR to filter it out like https://github.com/rust-cross/cargo-zigbuild/pull/97.

messense commented 1 year ago

So the flag is added by cargo-auditable, simply remove it might have other unintended effects?

https://github.com/rust-secure-code/cargo-auditable/blob/da85607fb1a09435d77288ccf05a92b2e8ec3f71/cargo-auditable/src/rustc_wrapper.rs#L56

NobodyXu commented 1 year ago

Hmmm, it seems like so.

Not sure how this to approach this problem, cc @Shnatsel

Shnatsel commented 1 year ago

The flag is needed to make sure the audit info is not removed by the linker. Without --undefined the linker would notice that the symbol is never used and remove it.

It does seem like an instance of https://github.com/ziglang/zig/pull/11906. The flag should be simply forwarded to the linker as-is.

NobodyXu commented 9 months ago

Is there any workaround? cc @Shnatsel @messense

We want to update ziglang v0.11 https://github.com/cargo-bins/cargo-binstall/pull/1564 but failed.

Shnatsel commented 9 months ago

I believe this is an issue with Zig, and it needs to be modified to recognize this flag and pass it along to the linker. Other similar PRs dealing with linker flags:

https://github.com/ziglang/zig/pull/11679 https://github.com/ziglang/zig/pull/11875 https://github.com/ziglang/zig/pull/11874

I am not aware of any workarounds.

NobodyXu commented 9 months ago

I will probably have to stay on ziglang v0.10 and instead zlib-ng on aarch64 to fix the error mentioned in #210