coreutils icon indicating copy to clipboard operation
coreutils copied to clipboard

0.2.0 prebuilt musl binaries are not statically linked

Open vazub opened this issue 2 months ago • 1 comments

Tested with https://github.com/uutils/coreutils/releases/download/0.2.0/coreutils-0.2.0-x86_64-unknown-linux-musl.tar.gz

> file coreutils
coreutils: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-x86_64.so.1, stripped

This appears to be a regression, or a change lacking explicit communication, as the previous builds, namely 0.1.0 were statically linked proper.

vazub avatar Sep 07 '25 20:09 vazub

Something is wrong...

$ ldd coreutils
./coreutils: error while loading shared libraries: /lib64/libc.so: invalid ELF header

yangyingchao avatar Sep 08 '25 01:09 yangyingchao

https://github.com/uutils/coreutils/compare/0.1.0...0.2.0

There were changes made to .cargo/config.toml:

libstdbuf must be a shared library, so musl libc can't be linked statically

Building without the new rustflags and without the stdbuf feature should allow you to statically link the binary

zdiff avatar Sep 08 '25 04:09 zdiff

@Ecordonnier ^^

note: this is why we should have a CI for this

sylvestre avatar Sep 08 '25 06:09 sylvestre

Should they be statically linked? With libstdbuf.so static linking can't really work.

The fallback of injecting libstdbuf.so into the binary and writing it to /tmp at runtime is a hack IMO

Ecordonnier avatar Sep 08 '25 07:09 Ecordonnier

we could disable libstdbuf for this platform

sylvestre avatar Sep 08 '25 07:09 sylvestre

+1 for disable libstdbuf.

yangyingchao avatar Sep 08 '25 07:09 yangyingchao

we could disable libstdbuf for this platform

Where does the requirement to static-link everything come from? Will something break with dynamic linking on musl? Musl does support dynamic linking afaik.

Ecordonnier avatar Sep 08 '25 07:09 Ecordonnier

IMO, statically linked apps can get rid of the glibc version, meaning we can run them on operating systems like CentOS 7, which has a very old version of glibc.

yangyingchao avatar Sep 08 '25 08:09 yangyingchao

we could disable libstdbuf for this platform

Where does the requirement to static-link everything come from? Will something break with dynamic linking on musl? Musl does support dynamic linking afaik.

I don't think there is any particular requirement per se, but this is a bit of a misleading question to ask. In the context of musl, users are typically expecting statically-linked tools, as this is one of musl's strong suits and one of the biggest reasons to use it instead of glibc (that is not static-link-friendly for reasons).

So any new feature or introduced dependency, that breaks this convention, is not really expected or welcomed. And especially not in the context of fundamental userspace tools, that would be used in many situations as a rescue mechanism in case of system failure, or otherwise minimal environments that don't need/want to have a dynamic libc present on a system.

vazub avatar Sep 08 '25 08:09 vazub

So any new feature or introduced dependency, that breaks this convention, is not really expected or welcomed.

well, it is an open source project. Please provides tests to make sure we don't regress if you care about this feature :)

sylvestre avatar Sep 08 '25 08:09 sylvestre

So any new feature or introduced dependency, that breaks this convention, is not really expected or welcomed.

well, it is an open source project. Please provides tests to make sure we don't regress if you care about this feature :)

That is a valid point in general, for any OSS project, but it is rather unrelated to the fact that there was a major packaging change for this release, that was not explicitly communicated in the release notes, breaking customer expectations/assumptions.

vazub avatar Sep 08 '25 08:09 vazub

It is a bug... if we had known, we would have communicated on this ... (and fix it)

sylvestre avatar Sep 08 '25 08:09 sylvestre

Something is wrong...

$ ldd coreutils
./coreutils: error while loading shared libraries: /lib64/libc.so: invalid ELF header

ldd doesn't work with binaries compiled with musl libc, so the error about invalid ELF header is normal. You need to use e.g. /lib/ld-musl-x86_64.so.1 --list coreutils or readelf -d coreutils | grep NEEDED.

Ecordonnier avatar Sep 09 '25 11:09 Ecordonnier

Something is wrong...

$ ldd coreutils
./coreutils: error while loading shared libraries: /lib64/libc.so: invalid ELF header

ldd doesn't work with binaries compiled with musl libc, so the error about invalid ELF header is normal. You need to use e.g. /lib/ld-musl-x86_64.so.1 --list coreutils or readelf -d coreutils | grep NEEDED.

Not exactly...

When call ldd with binaries compiled with musl libc, it should report statically linked instead of report invalid ELF header.

For example:

# https://github.com/BurntSushi/ripgrep
$ ldd rg       
	statically linked
# https://github.com/sharkdp/fd    
$ ldd fd  
	statically linked

yangyingchao avatar Sep 10 '25 01:09 yangyingchao

@yangyingchao ldd is just a shell script, that actually calls the system's dynamic linker under the hood. And by default, in most distros it assumes glibc linker by default. And glibc dynamic linker assumes glibc conventions, meaning it will report errors or wrong info when targeting binaries built against musl. Therefore, the errors you described are expected.

To properly check the binaries built against musl you need to use the approach suggested by @Ecordonnier.

More info: https://wiki.musl-libc.org/faq

vazub avatar Sep 10 '25 06:09 vazub

0.2.2 should fix this issue. Please let me know if it does not

sylvestre avatar Sep 10 '25 06:09 sylvestre

@vazub Thanks for the reply.

I understand, and my point is that there was something wrong in v0.2.0, which has already been fixed now.

I tried ldd on lots of statically linked programs; some were written in C or Rust and linked with musl, while others were written in Go. ldd always reported "statically linked" without error. This also applies to coreutils-v0.2.2...

@sylvestre yes, verified.

yangyingchao avatar Sep 10 '25 06:09 yangyingchao

Thanks folks @Ecordonnier added a test to make sure we don't regress in the future

sylvestre avatar Sep 10 '25 06:09 sylvestre