NikitaIvanovV / ictree

Like tree but interactive
https://nikitaivanovv.github.io/ictree/
GNU General Public License v3.0
147 stars 7 forks source link

Released binary depends on libc and hardcodes the interpreter #7

Closed balsoft closed 2 years ago

balsoft commented 2 years ago

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 commented 2 years ago

@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 commented 2 years ago

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

NikitaIvanovV commented 2 years ago

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 commented 2 years ago

Thanks!