From 30f2ab26fe55137d1c8604880b52951bb6c46804 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ci=C4=99=C5=BCarkiewicz?= Date: Sun, 24 Sep 2023 15:15:45 -0700 Subject: [PATCH 1/2] mold: wrap so it can find system libraries Add a wrapped version as `mold` as `mold-wrapped`. Currently `mold` mostly works, but the moment users start to need to link system libraries they get linking errors. It's been reported on github: Re #242816 and on user forums, e.g.: https://discourse.nixos.org/t/using-mold-as-linker-prevents-libraries-from-being-found --- pkgs/development/tools/mold/default.nix | 2 +- pkgs/top-level/all-packages.nix | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pkgs/development/tools/mold/default.nix b/pkgs/development/tools/mold/default.nix index 4ba0bfdd2a7c8..52844300f0875 100644 --- a/pkgs/development/tools/mold/default.nix +++ b/pkgs/development/tools/mold/default.nix @@ -48,7 +48,7 @@ stdenv.mkDerivation rec { }; meta = with lib; { - description = "A faster drop-in replacement for existing Unix linkers"; + description = "A faster drop-in replacement for existing Unix linkers (unwrapped)"; longDescription = '' mold is a faster drop-in replacement for existing Unix linkers. It is several times faster than the LLVM lld linker. mold is designed to diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index 23059a6c9b372..a72eef15e85aa 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -19647,6 +19647,13 @@ with pkgs; mold = callPackage ../development/tools/mold { }; + mold-wrapped = wrapBintoolsWith { + bintools = mold; + extraBuildCommands = '' + wrap mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${mold}/bin/ld.mold + ''; + }; + mommy = callPackage ../tools/misc/mommy { }; moon = callPackage ../development/tools/build-managers/moon/default.nix { }; From 77bf1395d6c5bb99a33c2bf76b2c6943b31ac85b Mon Sep 17 00:00:00 2001 From: Artturin Date: Fri, 13 Oct 2023 03:35:26 +0300 Subject: [PATCH 2/2] mold: add tests for `mold-wrapped` and `useMoldLinker` adapter Make both ready for cross with prefixes Currently `pkgsCross.aarch64-multiplatform.mold.passthru.tests.{wrapped,adapter}` fail with ``` Testing running the 'hello' binary which should be linked with 'mold' Hello, world! Checking for mold in the '.comment' section No mention of 'mold' detected in the '.comment' section The command was: aarch64-unknown-linux-gnu-readelf -p .comment ...bin/hello The output was: String dump of section '.comment': [ 0] GCC: (GNU) 12.3.0 ``` --- pkgs/development/tools/mold/default.nix | 47 ++++++++++++++++++++++++- pkgs/stdenv/adapters.nix | 4 +-- pkgs/top-level/all-packages.nix | 3 +- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/pkgs/development/tools/mold/default.nix b/pkgs/development/tools/mold/default.nix index 52844300f0875..84a7509c528c3 100644 --- a/pkgs/development/tools/mold/default.nix +++ b/pkgs/development/tools/mold/default.nix @@ -9,6 +9,11 @@ , testers , mold , nix-update-script +, runCommandCC +, mold-wrapped +, hello +, buildPackages +, useMoldLinker }: stdenv.mkDerivation rec { @@ -44,7 +49,47 @@ stdenv.mkDerivation rec { passthru = { updateScript = nix-update-script { }; - tests.version = testers.testVersion { package = mold; }; + tests = + let + helloTest = name: helloMold: + let + command = "$READELF -p .comment ${lib.getExe helloMold}"; + emulator = stdenv.hostPlatform.emulator buildPackages; + in + runCommandCC "mold-${name}-test" { passthru = { inherit helloMold; }; } + '' + echo "Testing running the 'hello' binary which should be linked with 'mold'" >&2 + ${emulator} ${lib.getExe helloMold} + + echo "Checking for mold in the '.comment' section" >&2 + if output=$(${command} 2>&1); then + if grep -Fw -- "mold" - <<< "$output"; then + touch $out + else + echo "No mention of 'mold' detected in the '.comment' section" >&2 + echo "The command was:" >&2 + echo "${command}" >&2 + echo "The output was:" >&2 + echo "$output" >&2 + exit 1 + fi + else + echo -n "${command}" >&2 + echo " returned a non-zero exit code." >&2 + echo "$output" >&2 + exit 1 + fi + '' + ; + in + { + version = testers.testVersion { package = mold; }; + wrapped = helloTest "wrapped" (hello.overrideAttrs (previousAttrs: { + nativeBuildInputs = (previousAttrs.nativeBuildInputs or [ ]) ++ [ mold-wrapped ]; + NIX_CFLAGS_LINK = toString (previousAttrs.NIX_CFLAGS_LINK or "") + " -fuse-ld=mold"; + })); + adapter = helloTest "adapter" (hello.override (old: { stdenv = useMoldLinker old.stdenv; })); + }; }; meta = with lib; { diff --git a/pkgs/stdenv/adapters.nix b/pkgs/stdenv/adapters.nix index f29bdf671c8c6..9b4ee16a003d1 100644 --- a/pkgs/stdenv/adapters.nix +++ b/pkgs/stdenv/adapters.nix @@ -192,8 +192,8 @@ rec { useMoldLinker = stdenv: let bintools = stdenv.cc.bintools.override { extraBuildCommands = '' - wrap ld.mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold - wrap ld ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold + wrap ${stdenv.cc.bintools.targetPrefix}ld.mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold + wrap ${stdenv.cc.bintools.targetPrefix}ld ${../build-support/bintools-wrapper/ld-wrapper.sh} ${pkgs.mold}/bin/ld.mold ''; }; in stdenv.override (old: { diff --git a/pkgs/top-level/all-packages.nix b/pkgs/top-level/all-packages.nix index a72eef15e85aa..adfa5861e7511 100644 --- a/pkgs/top-level/all-packages.nix +++ b/pkgs/top-level/all-packages.nix @@ -19650,7 +19650,8 @@ with pkgs; mold-wrapped = wrapBintoolsWith { bintools = mold; extraBuildCommands = '' - wrap mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${mold}/bin/ld.mold + wrap ${targetPackages.stdenv.cc.bintools.targetPrefix}ld.mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${mold}/bin/ld.mold + wrap ${targetPackages.stdenv.cc.bintools.targetPrefix}mold ${../build-support/bintools-wrapper/ld-wrapper.sh} ${mold}/bin/mold ''; };