Skip to content

Commit

Permalink
Don't try to apply patchelf to non-ELF binaries
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Feb 18, 2016
1 parent bf63de1 commit d71a485
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
8 changes: 2 additions & 6 deletions pkgs/build-support/setup-hooks/separate-debug-info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ _separateDebugInfo() {
# Find executables and dynamic libraries.
local i magic
while IFS= read -r -d $'\0' i; do
# Skip non-ELF files.
exec {fd}< "$i"
read -n 4 -u $fd magic
exec {fd}<&-
if ! [[ "$magic" =~ ELF ]]; then continue; fi
if ! isELF "$i"; then continue; fi

# Extract the Build ID. FIXME: there's probably a cleaner way.
local id="$(readelf -n "$i" | sed 's/.*Build ID: \([0-9a-f]*\).*/\1/; t; d')"
Expand All @@ -34,7 +30,7 @@ _separateDebugInfo() {

# Also a create a symlink <original-name>.debug.
ln -sfn ".build-id/${id:0:2}/${id:2}.debug" "$dst/../$(basename "$i")"
done < <(find "$prefix" -type f -a \( -perm /0100 -o -name "*.so" -o -name "*.so.*" \) -print0)
done < <(find "$prefix" -type f -print0)
}

# - We might prefer to compress the debug info during link-time already,
Expand Down
17 changes: 10 additions & 7 deletions pkgs/development/tools/misc/patchelf/setup-hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
fixupOutputHooks+=('if [ -z "$dontPatchELF" ]; then patchELF "$prefix"; fi')

patchELF() {
header "patching ELF executables and libraries in $prefix"
if [ -e "$prefix" ]; then

This comment has been minimized.

Copy link
@layus

layus May 9, 2016

Member

Removing this line breaks the build when $prefix does not exit yet. This may happen if the output is created in a postFixup script, like nss does. find will fail if the searched path does not exist.

find "$prefix" \( \
\( -type f -a -name "*.so*" \) -o \
\( -type f -a -perm -0100 \) \
\) -print -exec patchelf --shrink-rpath '{}' \;
fi
header "shrinking RPATHs of ELF executables and libraries in $prefix"

local i
while IFS= read -r -d $'\0' i; do
if [[ "$i" =~ .build-id ]]; then continue; fi
if ! isELF "$i"; then continue; fi
echo "shrinking $i"
patchelf --shrink-rpath "$i" || true
done < <(find "$prefix" -type f -print0)

stopNest
}
11 changes: 11 additions & 0 deletions pkgs/stdenv/generic/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,17 @@ installBin() {
}


# Return success if the specified file is an ELF object.
isELF() {
local fn="$1"
local magic
exec {fd}< "$fn"
read -n 4 -u $fd magic
exec {fd}<&-
if [[ "$magic" =~ ELF ]]; then return 0; else return 1; fi
}


######################################################################
# Initialisation.

Expand Down

4 comments on commit d71a485

@dezgeg
Copy link
Contributor

@dezgeg dezgeg commented on d71a485 Feb 20, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives:

/nix/store/n3abdjfmnv7rbnxlm2m3f7wjxvrcm0pb-patchelf-0.8/nix-support/setup-hook: line 16: syntax error near unexpected token `<'
/nix/store/n3abdjfmnv7rbnxlm2m3f7wjxvrcm0pb-patchelf-0.8/nix-support/setup-hook: line 16: syntax error near unexpected token `<'

@zimbatm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that the patchelf setup hook is executed by "sh" because it's in the bootstrap.
We could try a different approach: https://github.com/koalaman/shellcheck/wiki/SC2044#for-posix

@zimbatm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I don't know how the fixupOutputHooks works, arrays are not POSIX compatible

@zimbatm
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#13395 should fix the issue. Still building locally.

Please sign in to comment.