From 3a5a467be3d62906ef431579a5f1ad9078e405bf Mon Sep 17 00:00:00 2001 From: Julius Michaelis Date: Sun, 14 Jul 2024 23:27:56 +0900 Subject: [PATCH] rust: make linking on wasi32 work Rust assumes the linker is lld-flavored (unless it's named clang), and passes arguments accordingly. Two things are required to make that work: * Set the linker to ld.lld * disable linker hardening flags (they'd be passed befor the -flavor arg, breakin that, on top of having no effect) --- pkgs/build-support/bintools-wrapper/default.nix | 2 +- pkgs/build-support/rust/lib/default.nix | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/bintools-wrapper/default.nix b/pkgs/build-support/bintools-wrapper/default.nix index e7fcf173c6026..bba00f0adbec0 100644 --- a/pkgs/build-support/bintools-wrapper/default.nix +++ b/pkgs/build-support/bintools-wrapper/default.nix @@ -334,7 +334,7 @@ stdenvNoCC.mkDerivation { hardening_unsupported_flags+=" pic" '' - + optionalString (targetPlatform.isAvr || targetPlatform.isWindows) '' + + optionalString (targetPlatform.isAvr || targetPlatform.isWindows || targetPlatform.isWasi) '' hardening_unsupported_flags+=" relro bindnow" '' diff --git a/pkgs/build-support/rust/lib/default.nix b/pkgs/build-support/rust/lib/default.nix index e09f913bfbd39..777bcb21fd181 100644 --- a/pkgs/build-support/rust/lib/default.nix +++ b/pkgs/build-support/rust/lib/default.nix @@ -23,8 +23,10 @@ rec { ccForHost = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}cc"; cxxForHost = "${stdenv.cc}/bin/${stdenv.cc.targetPrefix}c++"; - linkerForHost = if shouldUseLLD stdenv.targetPlatform - && !stdenv.cc.bintools.isLLVM + # For wasi: Rust recognizes "clang" or "gcc" as compilers, but not "cc" as in ccForHost. + # So it defaults to expecting lld and passes arguments accordingly. + linkerForHost = if stdenv.targetPlatform.isWasi + || (shouldUseLLD stdenv.targetPlatform && !stdenv.cc.bintools.isLLVM) then "${pkgsBuildHost.llvmPackages.bintools}/bin/${stdenv.cc.targetPrefix}ld.lld" else ccForHost;