2 set -eu -o pipefail
+o posix
5 if (( "${NIX_DEBUG:-0}" >= 7 )); then
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@
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
44 while (( "$n" < "$nParams" )); do
46 p2
=${params[n+1]:-} # handle `p` being last one
47 if [ "${p:0:3}" = -L/ ] && badPathWithDarwinSdk
"${p:2}"; then
49 elif [ "$p" = -L ] && badPathWithDarwinSdk
"$p2"; then
51 elif [ "$p" = -rpath ] && badPath
"$p2"; then
53 elif [ "$p" = -dynamic-linker ] && badPath
"$p2"; then
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.
60 elif [ "${p:0:10}" = /LIBPATH
:/ ] && badPath
"${p:9}"; then
62 # We need to not match LINK.EXE-style flags like
63 # /NOLOGO or /LIBPATH:/nix/store/foo
64 elif [[ $p =~ ^
/[^
:]*/ ]] && badPath
"$p"; then
66 elif [ "${p:0:9}" = --sysroot ]; then
67 # Our ld is not built with sysroot support (Can we fix that?)
74 # Old bash empty array hack
75 params
=(${rest+"${rest[@]}"})
79 source @out@
/nix-support
/add-hardening.sh
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
100 if [ -e @out@
/nix-support
/add-local-ldflags-before.sh
]; then
101 source @out@
/nix-support
/add-local-ldflags-before.sh
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
114 declare -i relocatable
=0 link32
=0
119 [ "$NIX_DONT_SET_RPATH_@suffixSalt@" != 1 ] \
120 ||
[ "$NIX_SET_BUILD_ID_@suffixSalt@" = 1 ] \
121 ||
[ -e @out@
/nix-support
/dynamic-linker-m32
]
124 # Old bash thinks empty arrays are undefined, ugh.
126 ${extraBefore+"${extraBefore[@]}"} \
127 ${params+"${params[@]}"} \
128 ${extraAfter+"${extraAfter[@]}"}
138 # Presumably only the last `-m` flag has any effect.
144 -dynamic-linker |
-plugin)
145 # Ignore this argument, or it will match *.so and be added to rpath.
153 libs
["lib${p:2}.so"]=1
155 "${NIX_STORE:-}"/*.so |
"${NIX_STORE:-}"/*.so.
*)
156 # This is a direct reference to a shared library.
160 -r |
--relocatable |
-i)
169 # Determine linkerOutput
172 ${extraBefore+"${extraBefore[@]}"} \
173 ${params+"${params[@]}"} \
174 ${extraAfter+"${extraAfter[@]}"}
178 # Informational for post-link-hook
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
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.
204 for dir
in ${libDirs+"${libDirs[@]}"}; do
205 if [[ "$dir" =~
[/.
][/.
] ]] && dir2
=$
(readlink
-f "$dir"); then
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.
214 for path
in "$dir"/*; do
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.
223 extraAfter
+=(-rpath "$dir")
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
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
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
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
264 ${extraBefore+"${extraBefore[@]}"} \
265 ${params+"${params[@]}"} \
266 ${extraAfter+"${extraAfter[@]}"} > "$responseFile"
267 @prog@
"@$responseFile"
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