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

Released binary depends on libc and hardcodes the interpreter #7

Closed
balsoft opened this issue Feb 5, 2022 · 4 comments
Closed

Released binary depends on libc and hardcodes the interpreter #7

balsoft opened this issue Feb 5, 2022 · 4 comments

Comments

@balsoft
Copy link
Contributor

balsoft commented Feb 5, 2022

Typically, when people distribute standalone binaries for Linux, they are expected to be statically linked and have no external library dependencies. However, it is not the case for the binary distributed with the release now:

$ file ictree
ictree: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=559effd629008872ca992ef83c7b78f9ce73b2d7, for GNU/Linux 4.4.0, stripped
$ ldd ictree
	linux-vdso.so.1 (0x00007fff03b8b000)
	libc.so.6 => /nix/store/saw6nkqqqfx5xm1h5cpk7gxnxmw9wk47-glibc-2.33-62/lib/libc.so.6 (0x00007ff4a1968000)
	/lib64/ld-linux-x86-64.so.2 => /nix/store/saw6nkqqqfx5xm1h5cpk7gxnxmw9wk47-glibc-2.33-62/lib64/ld-linux-x86-64.so.2 (0x00007ff4a1b43000)

This may cause issues when running on musl systems, or on systems without /lib/ld-linux-x86-64.so.2 since then the hardcoded interpreter doesn't exist. One way to avoid such dependencies is to produce a statically linked executable linked to musl. E.g. with Nix:

nix-build -E 'with import <nixpkgs> {}; pkgsStatic.stdenv.mkDerivation { name = "ictree"; src = ./.; installPhase = "make install PREFIX=$out"; }'

The resulting executable will be a bit larger (since it carries the relevant parts of libc with it) but it should work on a much wider set of systems, and do so more reproducibly and consistently.

@NikitaIvanovV
Copy link
Owner

NikitaIvanovV commented Feb 5, 2022

@balsoft Thanks! I did not think about it... Will fix! One question: why do you suggest to use musl for static linking? Is it because it's more minimal and the executable will result in smaller size or because libc is not as portable?

@balsoft
Copy link
Contributor Author

balsoft commented Feb 5, 2022

Statically linking to glibc is discouraged and not supported by upstream: https://stackoverflow.com/questions/57476533/why-is-statically-linking-glibc-discouraged

@NikitaIvanovV
Copy link
Owner

NikitaIvanovV commented Feb 6, 2022

Just uploaded the statically linked binary to v0.2.2 release. I didn't use Nix, though, simply used musl wrapper for gcc:

make CC=musl-gcc LDFLAGS=-static

I assume this method works too because both ldd and file indicate that the resulting binary is statically linked.

@balsoft
Copy link
Contributor Author

balsoft commented Feb 6, 2022

Thanks!

@balsoft balsoft closed this as completed Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants