2017-08-02 18:48:51 +02:00
|
|
|
#! @shell@
|
2017-06-26 06:43:06 +02:00
|
|
|
set -eu -o pipefail
|
2017-07-26 11:30:00 +02:00
|
|
|
shopt -s nullglob
|
2017-08-02 18:48:51 +02:00
|
|
|
|
2016-01-25 09:54:10 +01:00
|
|
|
path_backup="$PATH"
|
2017-08-02 18:48:51 +02:00
|
|
|
|
|
|
|
# phase separation makes this look useless
|
|
|
|
# shellcheck disable=SC2157
|
2016-02-01 14:42:33 +01:00
|
|
|
if [ -n "@coreutils_bin@" ]; then
|
2017-08-02 18:48:51 +02:00
|
|
|
PATH="@coreutils_bin@/bin"
|
2015-12-17 23:02:40 +01:00
|
|
|
fi
|
2008-06-26 13:07:46 +02:00
|
|
|
|
2017-06-26 06:43:06 +02:00
|
|
|
if [ -n "$NIX_LD_WRAPPER_@infixSalt@_START_HOOK" ]; then
|
|
|
|
source "$NIX_LD_WRAPPER_@infixSalt@_START_HOOK"
|
2008-06-26 13:07:46 +02:00
|
|
|
fi
|
|
|
|
|
2017-06-26 06:43:06 +02:00
|
|
|
if [ -z "$NIX_CC_WRAPPER_@infixSalt@_FLAGS_SET" ]; then
|
2008-06-26 13:07:46 +02:00
|
|
|
source @out@/nix-support/add-flags.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
source @out@/nix-support/utils.sh
|
|
|
|
|
|
|
|
|
|
|
|
# Optionally filter out paths not refering to the store.
|
2016-10-30 14:41:41 +01:00
|
|
|
expandResponseParams "$@"
|
2017-08-02 18:48:51 +02:00
|
|
|
if [[ "$NIX_ENFORCE_PURITY" = 1 && -n "$NIX_STORE"
|
2017-06-26 06:43:06 +02:00
|
|
|
&& ( -z "$NIX_@infixSalt@_IGNORE_LD_THROUGH_GCC" || -z "$NIX_@infixSalt@_LDFLAGS_SET" ) ]]; then
|
2008-06-26 13:07:46 +02:00
|
|
|
rest=()
|
2017-07-26 11:30:00 +02:00
|
|
|
nParams=${#params[@]}
|
|
|
|
declare -i n=0
|
2017-08-02 18:48:51 +02:00
|
|
|
while [ "$n" -lt "$nParams" ]; do
|
2008-06-26 13:07:46 +02:00
|
|
|
p=${params[n]}
|
2017-08-02 18:48:51 +02:00
|
|
|
p2=${params[n+1]}
|
2014-10-10 14:25:23 +02:00
|
|
|
if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
|
2017-07-26 11:30:00 +02:00
|
|
|
skip "${p:2}"
|
2014-10-10 14:25:23 +02:00
|
|
|
elif [ "$p" = -L ] && badPath "$p2"; then
|
2017-07-26 11:30:00 +02:00
|
|
|
n+=1; skip "$p2"
|
2014-10-10 14:25:23 +02:00
|
|
|
elif [ "$p" = -rpath ] && badPath "$p2"; then
|
2017-07-26 11:30:00 +02:00
|
|
|
n+=1; skip "$p2"
|
2014-10-10 14:25:23 +02:00
|
|
|
elif [ "$p" = -dynamic-linker ] && badPath "$p2"; then
|
2017-07-26 11:30:00 +02:00
|
|
|
n+=1; skip "$p2"
|
2014-10-10 14:25:23 +02:00
|
|
|
elif [ "${p:0:1}" = / ] && badPath "$p"; then
|
2008-06-26 13:07:46 +02:00
|
|
|
# We cannot skip this; barf.
|
|
|
|
echo "impure path \`$p' used in link" >&2
|
|
|
|
exit 1
|
2014-10-10 14:25:23 +02:00
|
|
|
elif [ "${p:0:9}" = --sysroot ]; then
|
2014-06-30 16:39:39 +02:00
|
|
|
# Our ld is not built with sysroot support (Can we fix that?)
|
|
|
|
:
|
2008-06-26 13:07:46 +02:00
|
|
|
else
|
2017-07-26 11:30:00 +02:00
|
|
|
rest+=("$p")
|
2008-06-26 13:07:46 +02:00
|
|
|
fi
|
2017-07-26 11:30:00 +02:00
|
|
|
n+=1
|
2008-06-26 13:07:46 +02:00
|
|
|
done
|
|
|
|
params=("${rest[@]}")
|
|
|
|
fi
|
|
|
|
|
2016-02-26 18:38:15 +01:00
|
|
|
source @out@/nix-support/add-hardening.sh
|
2008-06-26 13:07:46 +02:00
|
|
|
|
2017-08-02 18:48:51 +02:00
|
|
|
extraAfter=("${hardeningLDFlags[@]}")
|
2008-06-26 13:07:46 +02:00
|
|
|
extraBefore=()
|
|
|
|
|
2017-06-26 06:43:06 +02:00
|
|
|
if [ -z "$NIX_@infixSalt@_LDFLAGS_SET" ]; then
|
|
|
|
extraAfter+=($NIX_@infixSalt@_LDFLAGS)
|
|
|
|
extraBefore+=($NIX_@infixSalt@_LDFLAGS_BEFORE)
|
2008-06-26 13:07:46 +02:00
|
|
|
fi
|
|
|
|
|
2017-06-26 06:43:06 +02:00
|
|
|
extraAfter+=($NIX_@infixSalt@_LDFLAGS_AFTER $NIX_@infixSalt@_LDFLAGS_HARDEN)
|
2014-07-02 14:55:44 +02:00
|
|
|
|
2017-07-26 11:30:00 +02:00
|
|
|
declare -a libDirs
|
|
|
|
declare -A libs
|
|
|
|
relocatable=
|
|
|
|
|
|
|
|
# Find all -L... switches for rpath, and relocatable flags for build id.
|
2017-06-26 06:43:06 +02:00
|
|
|
if [ "$NIX_@infixSalt@_DONT_SET_RPATH" != 1 ] || [ "$NIX_@infixSalt@_SET_BUILD_ID" = 1 ]; then
|
2017-07-26 11:30:00 +02:00
|
|
|
prev=
|
2017-08-02 18:48:51 +02:00
|
|
|
for p in "${extraBefore[@]}" "${params[@]}" "${extraAfter[@]}"; do
|
2017-07-26 11:30:00 +02:00
|
|
|
case "$prev" in
|
|
|
|
-L)
|
|
|
|
libDirs+=("$p")
|
|
|
|
;;
|
|
|
|
-l)
|
|
|
|
libs["lib${p}.so"]=1
|
|
|
|
;;
|
2017-08-01 10:27:50 +02:00
|
|
|
-dynamic-linker | -plugin)
|
|
|
|
# Ignore this argument, or it will match *.so and be added to rpath.
|
2017-07-26 11:30:00 +02:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
case "$p" in
|
|
|
|
-L/*)
|
|
|
|
libDirs+=("${p:2}")
|
|
|
|
;;
|
|
|
|
-l?*)
|
|
|
|
libs["lib${p:2}.so"]=1
|
|
|
|
;;
|
|
|
|
"$NIX_STORE"/*.so | "$NIX_STORE"/*.so.*)
|
2017-08-01 10:27:50 +02:00
|
|
|
# This is a direct reference to a shared library.
|
|
|
|
libDirs+=("${p%/*}")
|
|
|
|
libs["${p##*/}"]=1
|
2017-07-26 11:30:00 +02:00
|
|
|
;;
|
|
|
|
-r | --relocatable | -i)
|
|
|
|
relocatable=1
|
|
|
|
esac
|
2008-06-26 13:07:46 +02:00
|
|
|
;;
|
|
|
|
esac
|
2017-07-26 11:30:00 +02:00
|
|
|
prev="$p"
|
2008-06-26 13:07:46 +02:00
|
|
|
done
|
2017-07-26 11:30:00 +02:00
|
|
|
fi
|
|
|
|
|
2008-06-26 13:07:46 +02:00
|
|
|
|
2017-07-26 11:30:00 +02:00
|
|
|
# Add all used dynamic libraries to the rpath.
|
2017-06-26 06:43:06 +02:00
|
|
|
if [ "$NIX_@infixSalt@_DONT_SET_RPATH" != 1 ]; then
|
2017-07-26 11:30:00 +02:00
|
|
|
# For each directory in the library search path (-L...),
|
2009-02-01 22:26:28 +01:00
|
|
|
# see if it contains a dynamic library used by a -l... flag. If
|
|
|
|
# so, add the directory to the rpath.
|
2010-12-14 16:22:25 +01: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 10:27:50 +02:00
|
|
|
declare -A rpaths
|
2017-07-26 11:30:00 +02:00
|
|
|
for dir in "${libDirs[@]}"; do
|
|
|
|
if [[ "$dir" =~ [/.][/.] ]] && dir2=$(readlink -f "$dir"); then
|
|
|
|
dir="$dir2"
|
|
|
|
fi
|
|
|
|
if [ "${rpaths[$dir]}" ] || [[ "$dir" != "$NIX_STORE"/* ]]; then
|
|
|
|
# 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
|
|
|
|
for path in "$dir"/lib*.so; do
|
|
|
|
file="${path##*/}"
|
|
|
|
if [ "${libs[$file]}" ]; then
|
|
|
|
libs["$file"]=
|
|
|
|
if [ ! "${rpaths[$dir]}" ]; then
|
|
|
|
rpaths["$dir"]=1
|
2017-08-02 18:48:51 +02:00
|
|
|
extraAfter+=(-rpath "$dir")
|
2017-07-26 11:30:00 +02:00
|
|
|
fi
|
2008-06-26 13:07:46 +02:00
|
|
|
fi
|
2009-02-01 22:26:28 +01:00
|
|
|
done
|
2008-06-26 13:07:46 +02:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2016-02-28 02:57:37 +01:00
|
|
|
# Only add --build-id if this is a final link. FIXME: should build gcc
|
|
|
|
# with --enable-linker-build-id instead?
|
2017-06-26 06:43:06 +02:00
|
|
|
if [ "$NIX_@infixSalt@_SET_BUILD_ID" = 1 ] && [ ! "$relocatable" ]; then
|
2017-08-02 18:48:51 +02:00
|
|
|
extraAfter+=(--build-id)
|
2016-02-28 02:57:37 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
2008-06-26 13:07:46 +02:00
|
|
|
# Optionally print debug info.
|
2014-10-10 14:25:23 +02:00
|
|
|
if [ -n "$NIX_DEBUG" ]; then
|
2017-08-02 18:48:51 +02:00
|
|
|
echo "extra flags before to @prog@:" >&2
|
|
|
|
printf " %q\n" "${extraBefore[@]}" >&2
|
2017-07-26 11:30:00 +02:00
|
|
|
echo "original flags to @prog@:" >&2
|
|
|
|
printf " %q\n" "${params[@]}" >&2
|
2017-08-02 18:48:51 +02:00
|
|
|
echo "extra flags after to @prog@:" >&2
|
|
|
|
printf " %q\n" "${extraAfter[@]}" >&2
|
2008-06-26 13:07:46 +02:00
|
|
|
fi
|
|
|
|
|
2017-06-26 06:43:06 +02:00
|
|
|
if [ -n "$NIX_LD_WRAPPER_@infixSalt@_EXEC_HOOK" ]; then
|
|
|
|
source "$NIX_LD_WRAPPER_@infixSalt@_EXEC_HOOK"
|
2008-06-26 13:07:46 +02:00
|
|
|
fi
|
|
|
|
|
2016-01-25 09:54:10 +01:00
|
|
|
PATH="$path_backup"
|
2017-08-02 18:48:51 +02:00
|
|
|
exec @prog@ "${extraBefore[@]}" "${params[@]}" "${extraAfter[@]}"
|