From aa7c21430c8fd712e565bd4a54fd4bb2736d65bf Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 14 Aug 2019 14:48:40 +0200 Subject: [PATCH 1/7] autoPatchelfHook: search a valid interpreter, or fail --- pkgs/build-support/setup-hooks/auto-patchelf.sh | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkgs/build-support/setup-hooks/auto-patchelf.sh b/pkgs/build-support/setup-hooks/auto-patchelf.sh index 6af8eb1aed994..c42a219482a07 100644 --- a/pkgs/build-support/setup-hooks/auto-patchelf.sh +++ b/pkgs/build-support/setup-hooks/auto-patchelf.sh @@ -101,8 +101,22 @@ findDependency() { autoPatchelfFile() { local dep rpath="" toPatch="$1" - local interpreter="$(< "$NIX_CC/nix-support/dynamic-linker")" if isExecutable "$toPatch"; then + # Find a suitable interpreter + echo "searching an interpreted for $toPatch" + local interpreter="$(< "$NIX_CC/nix-support/dynamic-linker")" + if [ "$(getSoArch "$toPatch")" != $(getSoArch "$interpreter") ]; then + # try to find another interpreter + local interpreter32="$(< "$NIX_CC/nix-support/dynamic-linker-m32")" + if [ -n $interpreter32 -a "$(getSoArch "$toPatch")" = $(getSoArch "$interpreter32") ]; then + interpreter=${interpreter32} + else + echo "No interpreter found for arch $(getSoArch "$toPatch") needed by '$dep'" >&2 + false + fi + fi + echo "found interpreter '$interpreter' for '$toPatch'" >&2 + patchelf --set-interpreter "$interpreter" "$toPatch" if [ -n "$runtimeDependencies" ]; then for dep in $runtimeDependencies; do From 9d1f085138cd0d2752f5126407698a6193ed0f89 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 14 Aug 2019 15:21:53 +0200 Subject: [PATCH 2/7] autoPatchelfHook: fixup mistakes --- pkgs/build-support/setup-hooks/auto-patchelf.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkgs/build-support/setup-hooks/auto-patchelf.sh b/pkgs/build-support/setup-hooks/auto-patchelf.sh index c42a219482a07..4de9ecd05bf51 100644 --- a/pkgs/build-support/setup-hooks/auto-patchelf.sh +++ b/pkgs/build-support/setup-hooks/auto-patchelf.sh @@ -103,15 +103,16 @@ autoPatchelfFile() { if isExecutable "$toPatch"; then # Find a suitable interpreter - echo "searching an interpreted for $toPatch" + echo "searching an interpreter for $toPatch" local interpreter="$(< "$NIX_CC/nix-support/dynamic-linker")" if [ "$(getSoArch "$toPatch")" != $(getSoArch "$interpreter") ]; then # try to find another interpreter local interpreter32="$(< "$NIX_CC/nix-support/dynamic-linker-m32")" - if [ -n $interpreter32 -a "$(getSoArch "$toPatch")" = $(getSoArch "$interpreter32") ]; then + if [ -n "$interpreter32" ] + && [ "$(getSoArch "$toPatch")" = "$(getSoArch "$interpreter32")" ]; then interpreter=${interpreter32} else - echo "No interpreter found for arch $(getSoArch "$toPatch") needed by '$dep'" >&2 + echo "No interpreter found for arch $(getSoArch "$toPatch") needed by '$toPatch'" >&2 false fi fi From b837c7feec3cfbd777434847628bc14c3fb8307c Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Wed, 14 Aug 2019 16:00:58 +0200 Subject: [PATCH 3/7] autoPatchelfHook: refactor implementation --- .../setup-hooks/auto-patchelf.sh | 40 ++++++++++++------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/pkgs/build-support/setup-hooks/auto-patchelf.sh b/pkgs/build-support/setup-hooks/auto-patchelf.sh index 4de9ecd05bf51..6cc2dc9908b0e 100644 --- a/pkgs/build-support/setup-hooks/auto-patchelf.sh +++ b/pkgs/build-support/setup-hooks/auto-patchelf.sh @@ -98,27 +98,37 @@ findDependency() { return 1 } +patchElfInterpreter() { + local toPatch=$1 + local paths=( \ + "$NIX_CC/nix-support/dynamic-linker" \ + "$NIX_CC/nix-support/dynamic-linker-m32" \ + ) + + echo "searching an '$(getSoArch "$toPatch")' interpreter for $toPatch" >&2 + for f in "${paths[@]}"; do + [ -f "$f" -a -r "$f" ] || continue + local interpreter=$(< "$f") + + [ -n "$interpreter" -a -f "$interpreter" ] || continue + [ "$(getSoArch "$toPatch")" = $(getSoArch "$interpreter") ] || continue + + echo "found an '$(getSoArch "$toPatch")' interpreter at '$interpreter'" >&2 + patchelf --set-interpreter "$interpreter" "$toPatch" + return + done + + echo "error: no interpreter found for arch $(getSoArch "$toPatch") needed by '$toPatch'" >&2 + false +} + autoPatchelfFile() { local dep rpath="" toPatch="$1" if isExecutable "$toPatch"; then # Find a suitable interpreter - echo "searching an interpreter for $toPatch" - local interpreter="$(< "$NIX_CC/nix-support/dynamic-linker")" - if [ "$(getSoArch "$toPatch")" != $(getSoArch "$interpreter") ]; then - # try to find another interpreter - local interpreter32="$(< "$NIX_CC/nix-support/dynamic-linker-m32")" - if [ -n "$interpreter32" ] - && [ "$(getSoArch "$toPatch")" = "$(getSoArch "$interpreter32")" ]; then - interpreter=${interpreter32} - else - echo "No interpreter found for arch $(getSoArch "$toPatch") needed by '$toPatch'" >&2 - false - fi - fi - echo "found interpreter '$interpreter' for '$toPatch'" >&2 + patchElfInterpreter "$toPatch" - patchelf --set-interpreter "$interpreter" "$toPatch" if [ -n "$runtimeDependencies" ]; then for dep in $runtimeDependencies; do rpath="$rpath${rpath:+:}$dep/lib" From b73d43122e6fdb49df2edd67fce159d76aefed7b Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Mon, 19 Aug 2019 09:06:15 +0200 Subject: [PATCH 4/7] autoPatchelfHook: find linkers in dependencies --- pkgs/build-support/setup-hooks/auto-patchelf.sh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pkgs/build-support/setup-hooks/auto-patchelf.sh b/pkgs/build-support/setup-hooks/auto-patchelf.sh index 6cc2dc9908b0e..9d846a8a0a2df 100644 --- a/pkgs/build-support/setup-hooks/auto-patchelf.sh +++ b/pkgs/build-support/setup-hooks/auto-patchelf.sh @@ -1,7 +1,14 @@ declare -a autoPatchelfLibs +declare -a autoPatchelfLinkers gatherLibraries() { autoPatchelfLibs+=("$1/lib") + if [ -f "$1/nix-support/dynamic-linker" ]; then + autoPatchelfLinkers+=("$1/nix-support/dynamic-linker") + fi + if [ -f "$1/nix-support/dynamic-linker-m32" ]; then + autoPatchelfLinkers+=("$1/nix-support/dynamic-linker-m32") + fi } addEnvHooks "$targetOffset" gatherLibraries @@ -100,9 +107,10 @@ findDependency() { patchElfInterpreter() { local toPatch=$1 - local paths=( \ + local linkers=( \ "$NIX_CC/nix-support/dynamic-linker" \ "$NIX_CC/nix-support/dynamic-linker-m32" \ + "${autoPatchelfLinkers[@]}" \ ) echo "searching an '$(getSoArch "$toPatch")' interpreter for $toPatch" >&2 @@ -118,7 +126,7 @@ patchElfInterpreter() { return done - echo "error: no interpreter found for arch $(getSoArch "$toPatch") needed by '$toPatch'" >&2 + echo "error: no '$(getSoArch "$toPatch")' interpreter found but one is required for '$toPatch'" >&2 false } From d2e0625d8727dba683bf76e4991072ed4eea2d32 Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Mon, 19 Aug 2019 09:07:04 +0200 Subject: [PATCH 5/7] androidenv: fix build-tools with new autoPatchelfHook --- pkgs/development/mobile/androidenv/build-tools.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkgs/development/mobile/androidenv/build-tools.nix b/pkgs/development/mobile/androidenv/build-tools.nix index 976ef14162757..1d64fbd1f83b1 100644 --- a/pkgs/development/mobile/androidenv/build-tools.nix +++ b/pkgs/development/mobile/androidenv/build-tools.nix @@ -6,6 +6,8 @@ deployAndroidPackage { lib.optional (os == "linux") [ pkgs.glibc pkgs.zlib pkgs.ncurses5 pkgs_i686.glibc pkgs_i686.zlib pkgs_i686.ncurses5 ]; patchInstructions = '' ${lib.optionalString (os == "linux") '' + rm -r $packageBaseDir/{i686,aarch64,mipsel,arm}-linux* + addAutoPatchelfSearchPath $packageBaseDir/lib addAutoPatchelfSearchPath $packageBaseDir/lib64 autoPatchelf --no-recurse $packageBaseDir/lib64 From 6ff9b14219012b7deace9c1c4894087c5e4ee1fb Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Mon, 19 Aug 2019 09:07:47 +0200 Subject: [PATCH 6/7] bitwig-studio: fixes for the new autoPatchelfHook --- pkgs/applications/audio/bitwig-studio/bitwig-studio1.nix | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pkgs/applications/audio/bitwig-studio/bitwig-studio1.nix b/pkgs/applications/audio/bitwig-studio/bitwig-studio1.nix index 31716fce1c211..b637435cf90d1 100644 --- a/pkgs/applications/audio/bitwig-studio/bitwig-studio1.nix +++ b/pkgs/applications/audio/bitwig-studio/bitwig-studio1.nix @@ -55,6 +55,10 @@ stdenv.mkDerivation rec { substitute usr/share/applications/bitwig-studio.desktop \ $out/share/applications/bitwig-studio.desktop \ --replace /usr/bin/bitwig-studio $out/bin/bitwig-studio + + # We only support x86_64-linux anyway, + # and these files cannot be correctly autoPatchelfHooked + rm -r $out/libexec/bin32 ''; postFixup = '' From 254214ed0ff51c4228272dc46a82a3cacffead8f Mon Sep 17 00:00:00 2001 From: Guillaume Maudoux Date: Mon, 19 Aug 2019 09:24:43 +0200 Subject: [PATCH 7/7] autoPatchelfHook: fix typo --- pkgs/build-support/setup-hooks/auto-patchelf.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/build-support/setup-hooks/auto-patchelf.sh b/pkgs/build-support/setup-hooks/auto-patchelf.sh index 9d846a8a0a2df..e7e79f46e8b42 100644 --- a/pkgs/build-support/setup-hooks/auto-patchelf.sh +++ b/pkgs/build-support/setup-hooks/auto-patchelf.sh @@ -114,7 +114,7 @@ patchElfInterpreter() { ) echo "searching an '$(getSoArch "$toPatch")' interpreter for $toPatch" >&2 - for f in "${paths[@]}"; do + for f in "${linkers[@]}"; do [ -f "$f" -a -r "$f" ] || continue local interpreter=$(< "$f")