diff options
author | Jörg Thalheim <joerg@thalheim.io> | 2021-05-22 13:55:47 +0200 |
---|---|---|
committer | Jörg Thalheim <joerg@thalheim.io> | 2021-05-23 17:38:17 +0000 |
commit | 166948d4794be5c56c6279bf580ac416b1bcf022 (patch) | |
tree | aaad77fbf9922aa9fe767c638e4c41b06088b3d3 /pkgs/build-support/bintools-wrapper | |
parent | 61c74e1aee1b7fb35ca3c550f57286cf85999e96 (diff) |
cc-wrapper: don't set rpath on static-pie executables
Diffstat (limited to 'pkgs/build-support/bintools-wrapper')
-rw-r--r-- | pkgs/build-support/bintools-wrapper/ld-wrapper.sh | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/pkgs/build-support/bintools-wrapper/ld-wrapper.sh b/pkgs/build-support/bintools-wrapper/ld-wrapper.sh index e54dd6f471464..58ddc2aee9096 100644 --- a/pkgs/build-support/bintools-wrapper/ld-wrapper.sh +++ b/pkgs/build-support/bintools-wrapper/ld-wrapper.sh @@ -20,15 +20,23 @@ if [ -z "${NIX_BINTOOLS_WRAPPER_FLAGS_SET_@suffixSalt@:-}" ]; then source @out@/nix-support/add-flags.sh fi -setDynamicLinker=1 # Optionally filter out paths not refering to the store. expandResponseParams "$@" + +# NIX_LINK_TYPE is set if ld has been called through our cc wrapper +if [[ -n "${NIX_LINK_TYPE_@suffixSalt@:-}" ]]; then + linkType=$NIX_LINK_TYPE_@suffixSalt@ +else + linkType=$(checkLinkType "$@") +fi + if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "${NIX_STORE:-}" - && ( -z "$NIX_IGNORE_LD_THROUGH_GCC_@suffixSalt@" || -z "${NIX_LDFLAGS_SET_@suffixSalt@:-}" ) ]]; then + && ( -z "$NIX_IGNORE_LD_THROUGH_GCC_@suffixSalt@" || -z "${NIX_LINK_TYPE_@suffixSalt@:-}" ) ]]; then rest=() nParams=${#params[@]} declare -i n=0 + while (( "$n" < "$nParams" )); do p=${params[n]} p2=${params[n+1]:-} # handle `p` being last one @@ -48,11 +56,6 @@ if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "${NIX_STORE:-}" # Our ld is not built with sysroot support (Can we fix that?) : else - if [[ "$p" = -static || "$p" = -static-pie ]]; then - # Using a dynamic linker for static binaries can lead to crashes. - # This was observed for rust binaries. - setDynamicLinker=0 - fi rest+=("$p") fi n+=1 @@ -61,22 +64,24 @@ if [[ "${NIX_ENFORCE_PURITY:-}" = 1 && -n "${NIX_STORE:-}" params=(${rest+"${rest[@]}"}) fi + source @out@/nix-support/add-hardening.sh extraAfter=() extraBefore=(${hardeningLDFlags[@]+"${hardeningLDFlags[@]}"}) -if [ -z "${NIX_LDFLAGS_SET_@suffixSalt@:-}" ]; then - extraAfter+=($NIX_LDFLAGS_@suffixSalt@) - extraBefore+=($NIX_LDFLAGS_BEFORE_@suffixSalt@) +if [ -z "${NIX_LINK_TYPE_@suffixSalt@:-}" ]; then + extraAfter+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_@suffixSalt@)) + extraBefore+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_BEFORE_@suffixSalt@)) + # By adding dynamic linker to extraBefore we allow the users set their # own dynamic linker as NIX_LD_FLAGS will override earlier set flags - if [[ "$setDynamicLinker" = 1 && -n "$NIX_DYNAMIC_LINKER_@suffixSalt@" ]]; then + if [[ "$linkType" == dynamic && -n "$NIX_DYNAMIC_LINKER_@suffixSalt@" ]]; then extraBefore+=("-dynamic-linker" "$NIX_DYNAMIC_LINKER_@suffixSalt@") fi fi -extraAfter+=($NIX_LDFLAGS_AFTER_@suffixSalt@) +extraAfter+=($(filterRpathFlags "$linkType" $NIX_LDFLAGS_AFTER_@suffixSalt@)) # These flags *must not* be pulled up to -Wl, flags, so they can't go in # add-flags.sh. They must always be set, so must not be disabled by @@ -173,7 +178,7 @@ do prev="$p" done -if [[ "$link32" = "1" && "$setDynamicLinker" = 1 && -e "@out@/nix-support/dynamic-linker-m32" ]]; then +if [[ "$link32" == "1" && "$linkType" == dynamic && -e "@out@/nix-support/dynamic-linker-m32" ]]; then # We have an alternate 32-bit linker and we're producing a 32-bit ELF, let's # use it. extraAfter+=( @@ -183,7 +188,7 @@ if [[ "$link32" = "1" && "$setDynamicLinker" = 1 && -e "@out@/nix-support/dynami fi # Add all used dynamic libraries to the rpath. -if [ "$NIX_DONT_SET_RPATH_@suffixSalt@" != 1 ]; then +if [[ "$NIX_DONT_SET_RPATH_@suffixSalt@" != 1 && "$linkType" != static-pie ]]; then # 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. |