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
This commit is contained in:
Peter Collingbourne
2025-04-27 13:52:02 -07:00
parent 5a1dd4fefb
commit ac2c00e10f
3 changed files with 4 additions and 7 deletions
+4 -5
View File
@@ -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.
@@ -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;
@@ -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