From ac2c00e10fe959190bfba685e9615527ef3c12f5 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Sun, 27 Apr 2025 13:34:55 -0700 Subject: [PATCH] gcc: stop passing --with-ld when cross compiling Passing --with-ld causes problems when injecting a custom linker wrapper (similar to useMoldLinker) because it causes collect2 to check the --with-ld path (which is a hardcoded path to the standard linker wrapper) before $PATH, whereas with custom linker wrappers we want gcc to search $PATH. Because binutils-wrapper is not guaranteed to be present in $PATH, prepend it to $PATH in cc-wrapper. I verified that -print-prog-name=ld still prints an appropriate value. For example, when cross compiling for x86_64 on an aarch64 machine, the following is printed by the util-linux configure script (which uses -print-prog-name=ld [1]): checking for ld used by x86_64-unknown-linux-gnu-gcc... x86_64-unknown-linux-gnu-ld We still pass --with-as because the gcc spec file relies on this to find the assembler. This is probably fine for now as it is less common to want to override the assembler than the linker. [1] https://sources.debian.org/src/util-linux/2.41-4/m4/lib-ld.m4/?hl=59#L59 --- pkgs/build-support/cc-wrapper/add-flags.sh | 9 ++++----- pkgs/build-support/cc-wrapper/default.nix | 1 - .../development/compilers/gcc/common/configure-flags.nix | 1 - 3 files changed, 4 insertions(+), 7 deletions(-) diff --git a/pkgs/build-support/cc-wrapper/add-flags.sh b/pkgs/build-support/cc-wrapper/add-flags.sh index 90b6d9b04adc..a86f65d03f97 100644 --- a/pkgs/build-support/cc-wrapper/add-flags.sh +++ b/pkgs/build-support/cc-wrapper/add-flags.sh @@ -27,11 +27,10 @@ for var in "${var_templates_bool[@]}"; do mangleVarBool "$var" ${role_suffixes[@]+"${role_suffixes[@]}"} done -# Arocc does not support "-B" -if [[ -z "@isArocc@" ]]; then - # `-B@bintools@/bin' forces cc to use ld-wrapper.sh when calling ld. - NIX_CFLAGS_COMPILE_@suffixSalt@="-B@bintools@/bin/@bintools_targetPrefix@ $NIX_CFLAGS_COMPILE_@suffixSalt@" -fi +# Prepending `@bintools@/bin' to $PATH forces cc to use ld-wrapper.sh when calling ld. +# $path_backup is where cc-wrapper.sh stores the $PATH that will be used for the +# compiler invocation. +path_backup="@bintools@/bin:$path_backup" # Export and assign separately in order that a failing $(..) will fail # the script. diff --git a/pkgs/build-support/cc-wrapper/default.nix b/pkgs/build-support/cc-wrapper/default.nix index 3138498e3682..692474d48c42 100644 --- a/pkgs/build-support/cc-wrapper/default.nix +++ b/pkgs/build-support/cc-wrapper/default.nix @@ -872,7 +872,6 @@ stdenvNoCC.mkDerivation { cc = optionalString (!nativeTools) cc; wrapperName = "CC_WRAPPER"; inherit suffixSalt coreutils_bin bintools; - bintools_targetPrefix = bintools.targetPrefix; inherit libc_bin libc_dev libc_lib; inherit darwinPlatformForCC; default_hardening_flags_str = builtins.toString defaultHardeningFlags; diff --git a/pkgs/development/compilers/gcc/common/configure-flags.nix b/pkgs/development/compilers/gcc/common/configure-flags.nix index 3b09398dc678..3382e6939973 100644 --- a/pkgs/development/compilers/gcc/common/configure-flags.nix +++ b/pkgs/development/compilers/gcc/common/configure-flags.nix @@ -69,7 +69,6 @@ let "--with-as=${ if targetPackages.stdenv.cc.bintools.isLLVM then binutils else targetPackages.stdenv.cc.bintools }/bin/${targetPlatform.config}-as" - "--with-ld=${targetPackages.stdenv.cc.bintools}/bin/${targetPlatform.config}-ld" ] ++ ( if withoutTargetLibc then