2008-06-26 12:07:46 +01:00
|
|
|
#! @shell@ -e
|
2016-01-25 08:54:10 +00:00
|
|
|
path_backup="$PATH"
|
2016-02-01 13:42:33 +00:00
|
|
|
if [ -n "@coreutils_bin@" ]; then
|
|
|
|
PATH="@coreutils_bin@/bin"
|
2015-12-17 22:02:40 +00:00
|
|
|
fi
|
2008-06-26 12:07:46 +01:00
|
|
|
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ -n "$NIX_LD_WRAPPER_START_HOOK" ]; then
|
2008-06-26 12:07:46 +01:00
|
|
|
source "$NIX_LD_WRAPPER_START_HOOK"
|
|
|
|
fi
|
|
|
|
|
2014-12-17 18:11:30 +00:00
|
|
|
if [ -z "$NIX_CC_WRAPPER_FLAGS_SET" ]; then
|
2008-06-26 12:07:46 +01:00
|
|
|
source @out@/nix-support/add-flags.sh
|
|
|
|
fi
|
|
|
|
|
|
|
|
source @out@/nix-support/utils.sh
|
|
|
|
|
|
|
|
|
|
|
|
# Optionally filter out paths not refering to the store.
|
|
|
|
params=("$@")
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ "$NIX_ENFORCE_PURITY" = 1 -a -n "$NIX_STORE" \
|
|
|
|
-a \( -z "$NIX_IGNORE_LD_THROUGH_GCC" -o -z "$NIX_LDFLAGS_SET" \) ]; then
|
2008-06-26 12:07:46 +01:00
|
|
|
rest=()
|
|
|
|
n=0
|
2014-10-10 13:25:23 +01:00
|
|
|
while [ $n -lt ${#params[*]} ]; do
|
2008-06-26 12:07:46 +01:00
|
|
|
p=${params[n]}
|
|
|
|
p2=${params[$((n+1))]}
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
|
2008-06-26 12:07:46 +01:00
|
|
|
skip $p
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -L ] && badPath "$p2"; then
|
2008-06-26 12:07:46 +01:00
|
|
|
n=$((n + 1)); skip $p2
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -rpath ] && badPath "$p2"; then
|
2008-06-26 12:07:46 +01:00
|
|
|
n=$((n + 1)); skip $p2
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -dynamic-linker ] && badPath "$p2"; then
|
2008-06-26 12:07:46 +01:00
|
|
|
n=$((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
|
|
|
|
rest=("${rest[@]}" "$p")
|
|
|
|
fi
|
|
|
|
n=$((n + 1))
|
|
|
|
done
|
|
|
|
params=("${rest[@]}")
|
|
|
|
fi
|
|
|
|
|
2016-03-07 20:39:26 +00:00
|
|
|
LD=@prog@
|
2016-02-26 17:38:15 +00:00
|
|
|
source @out@/nix-support/add-hardening.sh
|
2008-06-26 12:07:46 +01:00
|
|
|
|
2016-02-26 17:38:15 +00:00
|
|
|
extra=(${hardeningLDFlags[@]})
|
2008-06-26 12:07:46 +01:00
|
|
|
extraBefore=()
|
|
|
|
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ -z "$NIX_LDFLAGS_SET" ]; then
|
2014-07-02 13:55:44 +01:00
|
|
|
extra+=($NIX_LDFLAGS)
|
2014-07-01 14:53:30 +01:00
|
|
|
extraBefore+=($NIX_LDFLAGS_BEFORE)
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
|
|
|
|
2016-02-26 17:38:15 +00:00
|
|
|
extra+=($NIX_LDFLAGS_AFTER $NIX_LDFLAGS_HARDEN)
|
2014-07-02 13:55:44 +01:00
|
|
|
|
2008-06-26 12:07:46 +01:00
|
|
|
|
|
|
|
# Add all used dynamic libraries to the rpath.
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ "$NIX_DONT_SET_RPATH" != 1 ]; then
|
2008-06-26 12:07:46 +01:00
|
|
|
|
|
|
|
libPath=""
|
|
|
|
addToLibPath() {
|
|
|
|
local path="$1"
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ "${path:0:1}" != / ]; then return 0; fi
|
2008-06-26 12:07:46 +01:00
|
|
|
case "$path" in
|
|
|
|
*..*|*./*|*/.*|*//*)
|
|
|
|
local path2
|
|
|
|
if path2=$(readlink -f "$path"); then
|
|
|
|
path="$path2"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
case $libPath in
|
|
|
|
*\ $path\ *) return 0 ;;
|
|
|
|
esac
|
|
|
|
libPath="$libPath $path "
|
|
|
|
}
|
2014-10-10 13:25:23 +01:00
|
|
|
|
2010-01-26 22:42:43 +00:00
|
|
|
addToRPath() {
|
|
|
|
# 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.
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ "${1:0:${#NIX_STORE}}" != "$NIX_STORE" ]; then return 0; fi
|
2010-01-26 22:42:43 +00:00
|
|
|
case $rpath in
|
|
|
|
*\ $1\ *) return 0 ;;
|
|
|
|
esac
|
|
|
|
rpath="$rpath $1 "
|
|
|
|
}
|
|
|
|
|
2010-05-25 21:51:01 +01:00
|
|
|
libs=""
|
|
|
|
addToLibs() {
|
|
|
|
libs="$libs $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
rpath=""
|
|
|
|
|
2010-01-26 22:42:43 +00:00
|
|
|
# First, find all -L... switches.
|
|
|
|
allParams=("${params[@]}" ${extra[@]})
|
2008-06-26 12:07:46 +01:00
|
|
|
n=0
|
2014-10-10 13:25:23 +01:00
|
|
|
while [ $n -lt ${#allParams[*]} ]; do
|
2008-06-26 12:07:46 +01:00
|
|
|
p=${allParams[n]}
|
|
|
|
p2=${allParams[$((n+1))]}
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ "${p:0:3}" = -L/ ]; then
|
2008-06-26 12:07:46 +01:00
|
|
|
addToLibPath ${p:2}
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -L ]; then
|
2008-06-26 12:07:46 +01:00
|
|
|
addToLibPath ${p2}
|
|
|
|
n=$((n + 1))
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -l ]; then
|
2010-05-25 21:51:01 +01:00
|
|
|
addToLibs ${p2}
|
|
|
|
n=$((n + 1))
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "${p:0:2}" = -l ]; then
|
2010-05-25 21:51:01 +01:00
|
|
|
addToLibs ${p:2}
|
2014-10-10 13:25:23 +01:00
|
|
|
elif [ "$p" = -dynamic-linker ]; then
|
|
|
|
# Ignore the dynamic linker argument, or it
|
2010-12-13 22:46:21 +00:00
|
|
|
# will get into the next 'elif'. We don't want
|
|
|
|
# the dynamic linker path rpath to go always first.
|
|
|
|
n=$((n + 1))
|
2010-01-26 22:42:43 +00:00
|
|
|
elif [[ "$p" =~ ^[^-].*\.so($|\.) ]]; then
|
|
|
|
# This is a direct reference to a shared library, so add
|
|
|
|
# its directory to the rpath.
|
2009-12-12 18:48:07 +00:00
|
|
|
path="$(dirname "$p")";
|
2010-01-26 22:42:43 +00:00
|
|
|
addToRPath "${path}"
|
2008-06-26 12:07:46 +01:00
|
|
|
fi
|
|
|
|
n=$((n + 1))
|
|
|
|
done
|
|
|
|
|
2009-02-01 21:26:28 +00:00
|
|
|
# Second, for each directory in the library search path (-L...),
|
|
|
|
# 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.
|
2014-10-10 13:25:23 +01:00
|
|
|
|
2010-12-14 15:22:25 +00:00
|
|
|
for i in $libPath; do
|
|
|
|
for j in $libs; do
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ -f "$i/lib$j.so" ]; then
|
2010-12-14 15:22:25 +00:00
|
|
|
addToRPath $i
|
2009-02-01 21:26:28 +00:00
|
|
|
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
|
2014-10-10 13:25:23 +01:00
|
|
|
|
2008-06-26 12:07:46 +01:00
|
|
|
|
|
|
|
# Finally, add `-rpath' switches.
|
|
|
|
for i in $rpath; do
|
2016-02-28 01:57:37 +00:00
|
|
|
extra+=(-rpath $i)
|
2008-06-26 12:07:46 +01:00
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
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?
|
|
|
|
if [ "$NIX_SET_BUILD_ID" = 1 ]; then
|
|
|
|
for p in "${params[@]}"; do
|
|
|
|
if [ "$p" = "-r" -o "$p" = "--relocatable" -o "$p" = "-i" ]; then
|
|
|
|
relocatable=1
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ -z "$relocatable" ]; then
|
|
|
|
extra+=(--build-id)
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2008-06-26 12:07:46 +01:00
|
|
|
# Optionally print debug info.
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ -n "$NIX_DEBUG" ]; then
|
2014-10-10 12:49:26 +01:00
|
|
|
echo "original flags to @prog@:" >&2
|
2008-06-26 12:07:46 +01:00
|
|
|
for i in "${params[@]}"; do
|
|
|
|
echo " $i" >&2
|
|
|
|
done
|
2014-10-10 12:49:26 +01:00
|
|
|
echo "extra flags to @prog@:" >&2
|
2008-06-26 12:07:46 +01:00
|
|
|
for i in ${extra[@]}; do
|
|
|
|
echo " $i" >&2
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2014-10-10 13:25:23 +01:00
|
|
|
if [ -n "$NIX_LD_WRAPPER_EXEC_HOOK" ]; then
|
2008-06-26 12:07:46 +01:00
|
|
|
source "$NIX_LD_WRAPPER_EXEC_HOOK"
|
|
|
|
fi
|
|
|
|
|
2016-01-25 08:54:10 +00:00
|
|
|
PATH="$path_backup"
|
2014-10-10 12:49:26 +01:00
|
|
|
exec @prog@ ${extraBefore[@]} "${params[@]}" ${extra[@]}
|