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 19source @out@/nix-support/darwin-sdk-setup.bash 20 21if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then 22 source @out@/nix-support/add-flags.sh 23fi 24 25 26# Optionally filter out paths not refering to the store. 27expandResponseParams "$@" 28 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. 32if [[ -n "${NIX_LINK_TYPE_@suffixSalt@:-}" ]]; then 33 linkType=$NIX_LINK_TYPE_@suffixSalt@ 34else 35 linkType=$(checkLinkType "${params[@]}") 36fi 37 38if [[ "${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 43 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?) 68 : 69 else 70 rest+=("$p") 71 fi 72 n+=1 73 done 74 # Old bash empty array hack 75 params=(${rest+"${rest[@]}"}) 76fi 77 78 79source @out@/nix-support/add-hardening.sh 80 81extraAfter=() 82extraBefore=(${hardeningLDFlags[@]+"${hardeningLDFlags[@]}"}) 83 84if [ -z "${NIX_LINK_TYPE_@suffixSalt@:-}" ]; then 85 extraAfter+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_@suffixSalt@)) 86 extraBefore+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_BEFORE_@suffixSalt@)) 87 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@") 92 fi 93fi 94 95extraAfter+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_AFTER_@suffixSalt@)) 96 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. 100if [ -e @out@/nix-support/add-local-ldflags-before.sh ]; then 101 source @out@/nix-support/add-local-ldflags-before.sh 102fi 103 104 105# Three tasks: 106# 107# 1. Find all -L... switches for rpath 108# 109# 2. Find relocatable flag for build id. 110# 111# 3. Choose 32-bit dynamic linker if needed 112declare -a libDirs 113declare -A libs 114declare -i relocatable=0 link32=0 115 116linkerOutput="a.out" 117 118if 119 [ "$NIX_DONT_SET_RPATH_@suffixSalt@" != 1 ] \ 120 || [ "$NIX_SET_BUILD_ID_@suffixSalt@" = 1 ] \ 121 || [ -e @out@/nix-support/dynamic-linker-m32 ] 122then 123 prev= 124 # Old bash thinks empty arrays are undefined, ugh. 125 for p in \ 126 ${extraBefore+"${extraBefore[@]}"} \ 127 ${params+"${params[@]}"} \ 128 ${extraAfter+"${extraAfter[@]}"} 129 do 130 case "$prev" in 131 -L) 132 libDirs+=("$p") 133 ;; 134 -l) 135 libs["lib${p}.so"]=1 136 ;; 137 -m) 138 # Presumably only the last `-m` flag has any effect. 139 case "$p" in 140 elf_i386) link32=1;; 141 *) link32=0;; 142 esac 143 ;; 144 -dynamic-linker | -plugin) 145 # Ignore this argument, or it will match *.so and be added to rpath. 146 ;; 147 *) 148 case "$p" in 149 -L/*) 150 libDirs+=("${p:2}") 151 ;; 152 -l?*) 153 libs["lib${p:2}.so"]=1 154 ;; 155 "${NIX_STORE:-}"/*.so | "${NIX_STORE:-}"/*.so.*) 156 # This is a direct reference to a shared library. 157 libDirs+=("${p%/*}") 158 libs["${p##*/}"]=1 159 ;; 160 -r | --relocatable | -i) 161 relocatable=1 162 esac 163 ;; 164 esac 165 prev="$p" 166 done 167fi 168 169# Determine linkerOutput 170prev= 171for p in \ 172 ${extraBefore+"${extraBefore[@]}"} \ 173 ${params+"${params[@]}"} \ 174 ${extraAfter+"${extraAfter[@]}"} 175do 176 case "$prev" in 177 -o) 178 # Informational for post-link-hook 179 linkerOutput="$p" 180 ;; 181 *) 182 ;; 183 esac 184 prev="$p" 185done 186 187if [[ "$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)" 193 ) 194fi 195 196# Add all used dynamic libraries to the rpath. 197if [[ "$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" 207 fi 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 213 fi 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 225 fi 226 done 227 done 228 229fi 230 231# Only add --build-id if this is a final link. FIXME: should build gcc 232# with --enable-linker-build-id instead? 233# 234# Note: `lld` interprets `--build-id` to mean `--build-id=fast`; GNU ld defaults 235# to SHA1. 236if [ "$NIX_SET_BUILD_ID_@suffixSalt@" = 1 ] && ! (( "$relocatable" )); then 237 extraAfter+=(--build-id="${NIX_BUILD_ID_STYLE:-sha1}") 238fi 239 240# if a ld-wrapper-hook exists, run it. 241if [[ -e @out@/nix-support/ld-wrapper-hook ]]; then 242 linker=@prog@ 243 source @out@/nix-support/ld-wrapper-hook 244fi 245 246# Optionally print debug info. 247if (( "${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 255fi 256 257PATH="$path_backup" 258# Old bash workaround, see above. 259 260if (( "${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" 268else 269 @prog@ \ 270 ${extraBefore+"${extraBefore[@]}"} \ 271 ${params+"${params[@]}"} \ 272 ${extraAfter+"${extraAfter[@]}"} 273fi 274 275if [ -e "@out@/nix-support/post-link-hook" ]; then 276 source @out@/nix-support/post-link-hook 277fi