Clone of https://github.com/NixOS/nixpkgs.git (to stress-test knotserver)
1#! @shell@
2set -eu -o pipefail +o posix
3shopt -s nullglob
4
5if (( "${NIX_DEBUG:-0}" >= 7 )); then
6 set -x
7fi
8
9path_backup="$PATH"
10
11# phase separation makes this look useless
12# shellcheck disable=SC2157
13if [ -n "@coreutils_bin@" ]; then
14 PATH="@coreutils_bin@/bin"
15fi
16
17source @out@/nix-support/utils.bash
18
19if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then
20 source @out@/nix-support/add-flags.sh
21fi
22
23
24# Optionally filter out paths not refering to the store.
25expandResponseParams "$@"
26
27# NIX_LINK_TYPE is set if ld has been called through our cc wrapper. We take
28# advantage of this to avoid both recalculating it, and also repeating other
29# processing cc wrapper has already done.
30if [[ -n "${NIX_LINK_TYPE_@suffixSalt@:-}" ]]; then
31 linkType=$NIX_LINK_TYPE_@suffixSalt@
32else
33 linkType=$(checkLinkType "${params[@]}")
34fi
35
36if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "${NIX_STORE:-}"
37 && ( -z "$NIX_IGNORE_LD_THROUGH_GCC_@suffixSalt@" || -z "${NIX_LINK_TYPE_@suffixSalt@:-}" ) ]]; then
38 rest=()
39 nParams=${#params[@]}
40 declare -i n=0
41
42 while (( "$n" < "$nParams" )); do
43 p=${params[n]}
44 p2=${params[n+1]:-} # handle `p` being last one
45 if [ "${p:0:3}" = -L/ ] && badPath "${p:2}"; then
46 skip "${p:2}"
47 elif [ "$p" = -L ] && badPath "$p2"; then
48 n+=1; skip "$p2"
49 elif [ "$p" = -rpath ] && badPath "$p2"; then
50 n+=1; skip "$p2"
51 elif [ "$p" = -dynamic-linker ] && badPath "$p2"; then
52 n+=1; skip "$p2"
53 elif [ "$p" = -syslibroot ] && [ $p2 == // ]; then
54 # When gcc is built on darwin --with-build-sysroot=/
55 # produces '-syslibroot //' linker flag. It's a no-op,
56 # which does not introduce impurities.
57 n+=1; skip "$p2"
58 elif [ "${p:0:10}" = /LIBPATH:/ ] && badPath "${p:9}"; then
59 reject "${p:9}"
60 # We need to not match LINK.EXE-style flags like
61 # /NOLOGO or /LIBPATH:/nix/store/foo
62 elif [[ $p =~ ^/[^:]*/ ]] && badPath "$p"; then
63 reject "$p"
64 elif [ "${p:0:9}" = --sysroot ]; then
65 # Our ld is not built with sysroot support (Can we fix that?)
66 :
67 else
68 rest+=("$p")
69 fi
70 n+=1
71 done
72 # Old bash empty array hack
73 params=(${rest+"${rest[@]}"})
74fi
75
76
77source @out@/nix-support/add-hardening.sh
78
79extraAfter=()
80extraBefore=(${hardeningLDFlags[@]+"${hardeningLDFlags[@]}"})
81
82if [ -z "${NIX_LINK_TYPE_@suffixSalt@:-}" ]; then
83 extraAfter+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_@suffixSalt@))
84 extraBefore+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_BEFORE_@suffixSalt@))
85
86 # By adding dynamic linker to extraBefore we allow the users set their
87 # own dynamic linker as NIX_LD_FLAGS will override earlier set flags
88 if [[ "$linkType" == dynamic && -n "$NIX_DYNAMIC_LINKER_@suffixSalt@" ]]; then
89 extraBefore+=("-dynamic-linker" "$NIX_DYNAMIC_LINKER_@suffixSalt@")
90 fi
91fi
92
93extraAfter+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_AFTER_@suffixSalt@))
94
95# These flags *must not* be pulled up to -Wl, flags, so they can't go in
96# add-flags.sh. They must always be set, so must not be disabled by
97# NIX_LDFLAGS_SET.
98if [ -e @out@/nix-support/add-local-ldflags-before.sh ]; then
99 source @out@/nix-support/add-local-ldflags-before.sh
100fi
101
102
103# Three tasks:
104#
105# 1. Find all -L... switches for rpath
106#
107# 2. Find relocatable flag for build id.
108#
109# 3. Choose 32-bit dynamic linker if needed
110declare -a libDirs
111declare -A libs
112declare -i relocatable=0 link32=0
113
114linkerOutput="a.out"
115
116if
117 [ "$NIX_DONT_SET_RPATH_@suffixSalt@" != 1 ] \
118 || [ "$NIX_SET_BUILD_ID_@suffixSalt@" = 1 ] \
119 || [ -e @out@/nix-support/dynamic-linker-m32 ]
120then
121 prev=
122 # Old bash thinks empty arrays are undefined, ugh.
123 for p in \
124 ${extraBefore+"${extraBefore[@]}"} \
125 ${params+"${params[@]}"} \
126 ${extraAfter+"${extraAfter[@]}"}
127 do
128 case "$prev" in
129 -L)
130 libDirs+=("$p")
131 ;;
132 -l)
133 libs["lib${p}.so"]=1
134 ;;
135 -m)
136 # Presumably only the last `-m` flag has any effect.
137 case "$p" in
138 elf_i386) link32=1;;
139 *) link32=0;;
140 esac
141 ;;
142 -dynamic-linker | -plugin)
143 # Ignore this argument, or it will match *.so and be added to rpath.
144 ;;
145 *)
146 case "$p" in
147 -L/*)
148 libDirs+=("${p:2}")
149 ;;
150 -l?*)
151 libs["lib${p:2}.so"]=1
152 ;;
153 "${NIX_STORE:-}"/*.so | "${NIX_STORE:-}"/*.so.*)
154 # This is a direct reference to a shared library.
155 libDirs+=("${p%/*}")
156 libs["${p##*/}"]=1
157 ;;
158 -r | --relocatable | -i)
159 relocatable=1
160 esac
161 ;;
162 esac
163 prev="$p"
164 done
165fi
166
167# Determine linkerOutput
168prev=
169for p in \
170 ${extraBefore+"${extraBefore[@]}"} \
171 ${params+"${params[@]}"} \
172 ${extraAfter+"${extraAfter[@]}"}
173do
174 case "$prev" in
175 -o)
176 # Informational for post-link-hook
177 linkerOutput="$p"
178 ;;
179 *)
180 ;;
181 esac
182 prev="$p"
183done
184
185if [[ "$link32" == "1" && "$linkType" == dynamic && -e "@out@/nix-support/dynamic-linker-m32" ]]; then
186 # We have an alternate 32-bit linker and we're producing a 32-bit ELF, let's
187 # use it.
188 extraAfter+=(
189 '-dynamic-linker'
190 "$(< @out@/nix-support/dynamic-linker-m32)"
191 )
192fi
193
194# Add all used dynamic libraries to the rpath.
195if [[ "$NIX_DONT_SET_RPATH_@suffixSalt@" != 1 && "$linkType" != static-pie ]]; then
196 # For each directory in the library search path (-L...),
197 # see if it contains a dynamic library used by a -l... flag. If
198 # so, add the directory to the rpath.
199 # It's important to add the rpath in the order of -L..., so
200 # the link time chosen objects will be those of runtime linking.
201 declare -A rpaths
202 for dir in ${libDirs+"${libDirs[@]}"}; do
203 if [[ "$dir" =~ [/.][/.] ]] && dir2=$(readlink -f "$dir"); then
204 dir="$dir2"
205 fi
206 if [ -n "${rpaths[$dir]:-}" ] || [[ "$dir" != "${NIX_STORE:-}"/* ]]; then
207 # If the path is not in the store, don't add it to the rpath.
208 # This typically happens for libraries in /tmp that are later
209 # copied to $out/lib. If not, we're screwed.
210 continue
211 fi
212 for path in "$dir"/*; do
213 file="${path##*/}"
214 if [ "${libs[$file]:-}" ]; then
215 # This library may have been provided by a previous directory,
216 # but if that library file is inside an output of the current
217 # derivation, it can be deleted after this compilation and
218 # should be found in a later directory, so we add all
219 # directories that contain any of the libraries to rpath.
220 rpaths["$dir"]=1
221 extraAfter+=(-rpath "$dir")
222 break
223 fi
224 done
225 done
226
227fi
228
229# This is outside the DONT_SET_RPATH branch because it's more targeted and we
230# usually want it (on Darwin) even if DONT_SET_RPATH is set.
231if [ -n "${NIX_COREFOUNDATION_RPATH:-}" ]; then
232 extraAfter+=(-rpath $NIX_COREFOUNDATION_RPATH)
233fi
234
235# Only add --build-id if this is a final link. FIXME: should build gcc
236# with --enable-linker-build-id instead?
237#
238# Note: `lld` interprets `--build-id` to mean `--build-id=fast`; GNU ld defaults
239# to SHA1.
240if [ "$NIX_SET_BUILD_ID_@suffixSalt@" = 1 ] && ! (( "$relocatable" )); then
241 extraAfter+=(--build-id="${NIX_BUILD_ID_STYLE:-sha1}")
242fi
243
244
245# Optionally print debug info.
246if (( "${NIX_DEBUG:-0}" >= 1 )); then
247 # Old bash workaround, see above.
248 echo "extra flags before to @prog@:" >&2
249 printf " %q\n" ${extraBefore+"${extraBefore[@]}"} >&2
250 echo "original flags to @prog@:" >&2
251 printf " %q\n" ${params+"${params[@]}"} >&2
252 echo "extra flags after to @prog@:" >&2
253 printf " %q\n" ${extraAfter+"${extraAfter[@]}"} >&2
254fi
255
256PATH="$path_backup"
257# Old bash workaround, see above.
258
259if (( "${NIX_LD_USE_RESPONSE_FILE:-@use_response_file_by_default@}" >= 1 )); then
260 @prog@ @<(printf "%q\n" \
261 ${extraBefore+"${extraBefore[@]}"} \
262 ${params+"${params[@]}"} \
263 ${extraAfter+"${extraAfter[@]}"})
264else
265 @prog@ \
266 ${extraBefore+"${extraBefore[@]}"} \
267 ${params+"${params[@]}"} \
268 ${extraAfter+"${extraAfter[@]}"}
269fi
270
271if [ -e "@out@/nix-support/post-link-hook" ]; then
272 source @out@/nix-support/post-link-hook
273fi