Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bintools-wrapper: skip dynamic linker for static binaries #106908

Merged
merged 1 commit into from Dec 14, 2020

Conversation

Mic92
Copy link
Member

@Mic92 Mic92 commented Dec 14, 2020

Currently we set dynamic-linker unconditionally. This breaks
however some static binaries i.e. rust binaries linked against musl.
There is no reason we should set an elf interpreter for static binaries
hence this is skipped if -static or -static-pie is either passed to
our cc or ld wrapper.

Motivation for this change
Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS linux)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Ensured that relevant documentation is up to date
  • Fits CONTRIBUTING.md.

Currently we set dynamic-linker unconditionally. This breaks
however some static binaries i.e. rust binaries linked against musl.
There is no reason we should set an elf interpreter for static binaries
hence this is skipped if `-static` or `-static-pie` is either passed to
our cc or ld wrapper.
@@ -25,6 +26,10 @@ if [ -e @out@/nix-support/libc-ldflags ]; then
NIX_LDFLAGS_@suffixSalt@+=" $(< @out@/nix-support/libc-ldflags)"
fi

if [ -z "$NIX_DYNAMIC_LINKER_@suffixSalt@" ] && [ -e @out@/nix-support/dynamic-linker ]; then
Copy link
Member Author

Choose a reason for hiding this comment

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

This also allows users to specify their own link loader.

@Mic92
Copy link
Member Author

Mic92 commented Dec 14, 2020

Tested with glibc and musl:

$ nix-build -A pkgsMusl.tests.cc-wrapper -A pkgsMusl.tests.cc-wrapper-clang -A tests.cc-wrapper -A tests.cc-wrapper-clang

@@ -28,6 +30,16 @@ in stdenv.mkDerivation {
./core-foundation-check
''}

printf "checking whether compiler builds valid static C binaries... " >&2
Copy link
Member Author

Choose a reason for hiding this comment

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

@LnL7 do I need to disable this on darwin?

@@ -47,6 +48,11 @@ 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
Copy link
Member

Choose a reason for hiding this comment

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

There are a few other flags from the man page like -Bstatic, -dn, and -non_shared may be relevant (I'm not sure if any of them are used in practice though)

Copy link
Member Author

Choose a reason for hiding this comment

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

Interesting. Do they have the same semantic.

@Ericson2314 Ericson2314 merged commit 7a06f5d into NixOS:staging Dec 14, 2020
@Mic92 Mic92 deleted the fix-static-pie branch December 14, 2020 19:49
@@ -134,7 +145,7 @@ then
done
fi

if [ -e "@out@/nix-support/dynamic-linker-m32" ] && (( "$link32" )); then
if [[ "$link32" = "0" && "$setDynamicLinker" = 1 && -e "@out@/nix-support/dynamic-linker-m32" ]]; then
Copy link
Contributor

Choose a reason for hiding this comment

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

As pointed out in #107253, (( "$link32" )) is an arithmetic expansion that returns true (status 0) if $link32 is nonzero, not if it’s zero.

@thefloweringash
Copy link
Member

This is now passing -dynamic-linker on darwin where it's not supported. See comment / thread: #107086 (comment)

@vcunat
Copy link
Member

vcunat commented Dec 21, 2020

This PR was reverted in 363175c (for now). Unfortunately it isn't possible to reopen the original PR, but you can create a new one.

@Mic92
Copy link
Member Author

Mic92 commented Dec 25, 2020

reopened: #107596

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants