Skip to content

Commit

Permalink
rust: make linking on wasi32 work
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
jcaesar committed Jul 15, 2024
1 parent 92669f8 commit 3a5a467
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkgs/build-support/bintools-wrapper/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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"
''

Expand Down
6 changes: 4 additions & 2 deletions pkgs/build-support/rust/lib/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 3a5a467

Please sign in to comment.