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