2017-08-02 17:48:51 +01:00
|
|
|
#! @shell@
|
2017-10-16 22:31:44 +01:00
|
|
|
set -eu -o pipefail +o posix
|
2017-07-26 10:30:00 +01:00
|
|
|
shopt -s nullglob
|
2017-08-02 17:48:51 +01:00
|
|
|
|
2017-09-20 16:35:58 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 7 )); then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
2016-01-25 08:54:10 +00:00
|
|
|
path_backup="$PATH"
|
2017-08-02 17:48:51 +01:00
|
|
|
|
|
|
|
# phase separation makes this look useless
|
|
|
|
# shellcheck disable=SC2157
|
2016-02-01 13:42:33 +00:00
|
|
|
if [ -n "@coreutils_bin@" ]; then
|
2017-08-02 17:48:51 +01:00
|
|
|
PATH="@coreutils_bin@/bin"
|
2015-12-17 22:02:40 +00:00
|
|
|
fi
|
2008-06-26 12:07:46 +01:00
|
|
|
|
2018-05-07 18:15:34 +01:00
|
|
|
source @out@/nix-support/utils.bash
|
2017-08-31 19:43:09 +01:00
|
|
|
|
2020-04-28 05:08:48 +01:00
|
|
|
if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
|
2017-08-03 20:34:23 +01:00
|
|
|
source @out@/nix-support/add-flags.sh
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
|
|
|
|
2020-12-25 20:52:42 +00:00
|
|
|
setDynamicLinker=1
|
2008-06-26 12:07:46 +01:00
|
|
|
|
|
|
|
# Optionally filter out paths not refering to the store.
|
2016-10-30 13:41:41 +00:00
|
|
|
expandResponseParams "$@"
|
2017-08-08 07:56:25 +01:00
|
|
|
if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "${NIX_STORE:-}"
|
2020-04-28 05:08:48 +01:00
|
|
|
&& ( -z "$NIX_IGNORE_LD_THROUGH_GCC_@suffixSalt@" || -z "${NIX_LDFLAGS_SET_@suffixSalt@:-}" ) ]]; then
|
2008-06-26 12:07:46 +01:00
|
|
|
rest=()
|
2017-07-26 10:30:00 +01:00
|
|
|
nParams=${#params[@]}
|
|
|
|
declare -i n=0
|
2017-08-14 19:34:12 +01:00
|
|
|
while (( "$n" < "$nParams" )); do
|
2008-06-26 12:07:46 +01:00
|
|
|
p=${params[n]}
|
2017-08-03 20:34:23 +01:00
|
|
|
p2=${params[n+1]:-} # handle `p` being last one
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
|
2017-07-26 10:30:00 +01:00
|
|
|
skip "${p:2}"
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -L ] && badPath "$p2"; then
|
2017-07-26 10:30:00 +01:00
|
|
|
n+=1; skip "$p2"
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -rpath ] && badPath "$p2"; then
|
2017-07-26 10:30:00 +01:00
|
|
|
n+=1; skip "$p2"
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -dynamic-linker ] && badPath "$p2"; then
|
2017-07-26 10:30:00 +01:00
|
|
|
n+=1; skip "$p2"
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "${p:0:1}" = / ] && badPath "$p"; then
|
2008-06-26 12:07:46 +01:00
|
|
|
# We cannot skip this; barf.
|
|
|
|
echo "impure path \`$p' used in link" >&2
|
|
|
|
exit 1
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "${p:0:9}" = --sysroot ]; then
|
2014-06-30 15:39:39 +01:00
|
|
|
# Our ld is not built with sysroot support (Can we fix that?)
|
|
|
|
:
|
2008-06-26 12:07:46 +01:00
|
|
|
else
|
2020-12-25 20:52:42 +00:00
|
|
|
if [[ "$p" = -static || "$p" = -static-pie ]]; then
|
|
|
|
# Using a dynamic linker for static binaries can lead to crashes.
|
|
|
|
# This was observed for rust binaries.
|
|
|
|
setDynamicLinker=0
|
|
|
|
fi
|
2017-07-26 10:30:00 +01:00
|
|
|
rest+=("$p")
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
2017-07-26 10:30:00 +01:00
|
|
|
n+=1
|
2008-06-26 12:07:46 +01:00
|
|
|
done
|
2017-08-14 19:34:12 +01:00
|
|
|
# Old bash empty array hack
|
|
|
|
params=(${rest+"${rest[@]}"})
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
|
|
|
|
2016-02-26 17:38:15 +00:00
|
|
|
source @out@/nix-support/add-hardening.sh
|
2008-06-26 12:07:46 +01:00
|
|
|
|
2018-04-11 19:00:13 +01:00
|
|
|
extraAfter=()
|
|
|
|
extraBefore=(${hardeningLDFlags[@]+"${hardeningLDFlags[@]}"})
|
2008-06-26 12:07:46 +01:00
|
|
|
|
2020-04-28 05:08:48 +01:00
|
|
|
if [ -z "${NIX_LDFLAGS_SET_@suffixSalt@:-}" ]; then
|
|
|
|
extraAfter+=($NIX_LDFLAGS_@suffixSalt@)
|
|
|
|
extraBefore+=($NIX_LDFLAGS_BEFORE_@suffixSalt@)
|
2020-12-25 20:52:42 +00:00
|
|
|
# By adding dynamic linker to extraBefore we allow the users set their
|
|
|
|
# own dynamic linker as NIX_LD_FLAGS will override earlier set flags
|
|
|
|
if [[ "$setDynamicLinker" = 1 && -n "$NIX_DYNAMIC_LINKER_@suffixSalt@" ]]; then
|
|
|
|
extraBefore+=("-dynamic-linker" "$NIX_DYNAMIC_LINKER_@suffixSalt@")
|
|
|
|
fi
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
|
|
|
|
2020-04-28 05:08:48 +01:00
|
|
|
extraAfter+=($NIX_LDFLAGS_AFTER_@suffixSalt@)
|
2014-07-02 13:55:44 +01:00
|
|
|
|
2017-11-27 05:12:13 +00:00
|
|
|
# Specify the target emulation if nothing is passed in ("-m" overrides this
|
|
|
|
# environment variable). Ensures we never blindly fallback on targeting the host
|
|
|
|
# platform.
|
|
|
|
: ${LDEMULATION:=@emulation@}
|
|
|
|
|
2017-08-30 19:57:20 +01:00
|
|
|
# Three tasks:
|
|
|
|
#
|
|
|
|
# 1. Find all -L... switches for rpath
|
|
|
|
#
|
|
|
|
# 2. Find relocatable flag for build id.
|
|
|
|
#
|
|
|
|
# 3. Choose 32-bit dynamic linker if needed
|
2017-07-26 10:30:00 +01:00
|
|
|
declare -a libDirs
|
|
|
|
declare -A libs
|
2017-08-30 19:57:20 +01:00
|
|
|
declare -i relocatable=0 link32=0
|
2017-07-26 10:30:00 +01:00
|
|
|
|
2017-08-30 19:57:20 +01:00
|
|
|
if
|
2020-04-28 05:08:48 +01:00
|
|
|
[ "$NIX_DONT_SET_RPATH_@suffixSalt@" != 1 ] \
|
|
|
|
|| [ "$NIX_SET_BUILD_ID_@suffixSalt@" = 1 ] \
|
2017-08-30 19:57:20 +01:00
|
|
|
|| [ -e @out@/nix-support/dynamic-linker-m32 ]
|
|
|
|
then
|
2017-07-26 10:30:00 +01:00
|
|
|
prev=
|
2017-08-14 19:34:12 +01:00
|
|
|
# Old bash thinks empty arrays are undefined, ugh.
|
|
|
|
for p in \
|
|
|
|
${extraBefore+"${extraBefore[@]}"} \
|
|
|
|
${params+"${params[@]}"} \
|
|
|
|
${extraAfter+"${extraAfter[@]}"}
|
|
|
|
do
|
2017-07-26 10:30:00 +01:00
|
|
|
case "$prev" in
|
|
|
|
-L)
|
|
|
|
libDirs+=("$p")
|
|
|
|
;;
|
|
|
|
-l)
|
|
|
|
libs["lib${p}.so"]=1
|
|
|
|
;;
|
2017-08-30 19:57:20 +01:00
|
|
|
-m)
|
|
|
|
# Presumably only the last `-m` flag has any effect.
|
|
|
|
case "$p" in
|
|
|
|
elf_i386) link32=1;;
|
|
|
|
*) link32=0;;
|
|
|
|
esac
|
|
|
|
;;
|
2017-08-01 09:27:50 +01:00
|
|
|
-dynamic-linker | -plugin)
|
|
|
|
# Ignore this argument, or it will match *.so and be added to rpath.
|
2017-07-26 10:30:00 +01:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
case "$p" in
|
|
|
|
-L/*)
|
|
|
|
libDirs+=("${p:2}")
|
|
|
|
;;
|
|
|
|
-l?*)
|
|
|
|
libs["lib${p:2}.so"]=1
|
|
|
|
;;
|
2017-08-08 07:56:25 +01:00
|
|
|
"${NIX_STORE:-}"/*.so | "${NIX_STORE:-}"/*.so.*)
|
2017-08-01 09:27:50 +01:00
|
|
|
# This is a direct reference to a shared library.
|
|
|
|
libDirs+=("${p%/*}")
|
|
|
|
libs["${p##*/}"]=1
|
2017-07-26 10:30:00 +01:00
|
|
|
;;
|
|
|
|
-r | --relocatable | -i)
|
|
|
|
relocatable=1
|
|
|
|
esac
|
2008-06-26 12:07:46 +01:00
|
|
|
;;
|
|
|
|
esac
|
2017-07-26 10:30:00 +01:00
|
|
|
prev="$p"
|
2008-06-26 12:07:46 +01:00
|
|
|
done
|
2017-07-26 10:30:00 +01:00
|
|
|
fi
|
|
|
|
|
2020-12-25 20:52:42 +00:00
|
|
|
if [[ "$link32" = "1" && "$setDynamicLinker" = 1 && -e "@out@/nix-support/dynamic-linker-m32" ]]; then
|
2017-08-30 19:57:20 +01:00
|
|
|
# We have an alternate 32-bit linker and we're producing a 32-bit ELF, let's
|
|
|
|
# use it.
|
|
|
|
extraAfter+=(
|
|
|
|
'-dynamic-linker'
|
|
|
|
"$(< @out@/nix-support/dynamic-linker-m32)"
|
|
|
|
)
|
|
|
|
fi
|
2008-06-26 12:07:46 +01:00
|
|
|
|
2017-07-26 10:30:00 +01:00
|
|
|
# Add all used dynamic libraries to the rpath.
|
2020-04-28 05:08:48 +01:00
|
|
|
if [ "$NIX_DONT_SET_RPATH_@suffixSalt@" != 1 ]; then
|
2017-07-26 10:30:00 +01:00
|
|
|
# For each directory in the library search path (-L...),
|
2009-02-01 21:26:28 +00:00
|
|
|
# see if it contains a dynamic library used by a -l... flag. If
|
|
|
|
# so, add the directory to the rpath.
|
2010-12-14 15:22:25 +00:00
|
|
|
# It's important to add the rpath in the order of -L..., so
|
|
|
|
# the link time chosen objects will be those of runtime linking.
|
2017-08-01 09:27:50 +01:00
|
|
|
declare -A rpaths
|
2017-08-19 22:16:35 +01:00
|
|
|
for dir in ${libDirs+"${libDirs[@]}"}; do
|
2017-07-26 10:30:00 +01:00
|
|
|
if [[ "$dir" =~ [/.][/.] ]] && dir2=$(readlink -f "$dir"); then
|
|
|
|
dir="$dir2"
|
|
|
|
fi
|
2017-08-08 07:56:25 +01:00
|
|
|
if [ -n "${rpaths[$dir]:-}" ] || [[ "$dir" != "${NIX_STORE:-}"/* ]]; then
|
2017-07-26 10:30:00 +01:00
|
|
|
# If the path is not in the store, don't add it to the rpath.
|
|
|
|
# This typically happens for libraries in /tmp that are later
|
|
|
|
# copied to $out/lib. If not, we're screwed.
|
|
|
|
continue
|
|
|
|
fi
|
2017-08-07 16:06:44 +01:00
|
|
|
for path in "$dir"/*; do
|
2017-07-26 10:30:00 +01:00
|
|
|
file="${path##*/}"
|
2017-08-03 20:34:23 +01:00
|
|
|
if [ "${libs[$file]:-}" ]; then
|
2017-08-09 10:02:55 +01:00
|
|
|
# This library may have been provided by a previous directory,
|
|
|
|
# but if that library file is inside an output of the current
|
|
|
|
# derivation, it can be deleted after this compilation and
|
|
|
|
# should be found in a later directory, so we add all
|
|
|
|
# directories that contain any of the libraries to rpath.
|
|
|
|
rpaths["$dir"]=1
|
|
|
|
extraAfter+=(-rpath "$dir")
|
|
|
|
break
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
2009-02-01 21:26:28 +00:00
|
|
|
done
|
2008-06-26 12:07:46 +01:00
|
|
|
done
|
2017-07-21 19:29:18 +01:00
|
|
|
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
|
|
|
|
2017-10-06 06:26:19 +01:00
|
|
|
# This is outside the DONT_SET_RPATH branch because it's more targeted and we
|
|
|
|
# usually want it (on Darwin) even if DONT_SET_RPATH is set.
|
|
|
|
if [ -n "${NIX_COREFOUNDATION_RPATH:-}" ]; then
|
|
|
|
extraAfter+=(-rpath $NIX_COREFOUNDATION_RPATH)
|
|
|
|
fi
|
2008-06-26 12:07:46 +01:00
|
|
|
|
2016-02-28 01:57:37 +00:00
|
|
|
# Only add --build-id if this is a final link. FIXME: should build gcc
|
|
|
|
# with --enable-linker-build-id instead?
|
2020-04-28 05:08:48 +01:00
|
|
|
if [ "$NIX_SET_BUILD_ID_@suffixSalt@" = 1 ] && ! (( "$relocatable" )); then
|
2017-08-02 17:48:51 +01:00
|
|
|
extraAfter+=(--build-id)
|
2016-02-28 01:57:37 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2008-06-26 12:07:46 +01:00
|
|
|
# Optionally print debug info.
|
2017-09-20 00:10:49 +01:00
|
|
|
if (( "${NIX_DEBUG:-0}" >= 1 )); then
|
2017-08-14 19:34:12 +01:00
|
|
|
# Old bash workaround, see above.
|
2017-08-02 17:48:51 +01:00
|
|
|
echo "extra flags before to @prog@:" >&2
|
2017-08-14 19:34:12 +01:00
|
|
|
printf " %q\n" ${extraBefore+"${extraBefore[@]}"} >&2
|
2017-07-26 10:30:00 +01:00
|
|
|
echo "original flags to @prog@:" >&2
|
2017-08-14 19:34:12 +01:00
|
|
|
printf " %q\n" ${params+"${params[@]}"} >&2
|
2017-08-02 17:48:51 +01:00
|
|
|
echo "extra flags after to @prog@:" >&2
|
2017-08-14 19:34:12 +01:00
|
|
|
printf " %q\n" ${extraAfter+"${extraAfter[@]}"} >&2
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
|
|
|
|
2016-01-25 08:54:10 +00:00
|
|
|
PATH="$path_backup"
|
2017-08-14 19:34:12 +01:00
|
|
|
# Old bash workaround, see above.
|
|
|
|
exec @prog@ \
|
|
|
|
${extraBefore+"${extraBefore[@]}"} \
|
|
|
|
${params+"${params[@]}"} \
|
|
|
|
${extraAfter+"${extraAfter[@]}"}
|