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

Provide statically linked binaries with musl for linux #2968

Closed
jirutka opened this issue Aug 6, 2016 · 16 comments
Closed

Provide statically linked binaries with musl for linux #2968

jirutka opened this issue Aug 6, 2016 · 16 comments

Comments

@jirutka
Copy link
Contributor

jirutka commented Aug 6, 2016

Binaries for *-linux-gnu works only on glibc-based systems and nowhere else. When you link it statically with musl libc, then it will work on every Linux, regardless of which libc library it uses (musl, uClibc, glibc, …).

@japaric has already added support for musl in #2532.

I’ve successfully built cargo using cargo-bootstrap (well, with many ad-hoc workarounds because of cargo-bootstrap flaws) on Alpine Linux that is based on musl libc, the only problem is #2937 (my workaround.patch). However, I’ve linked it only dynamically, because of issue with rustc on musl.

@alexcrichton
Copy link
Member

I'd love to do this! It should be quite easy to just cross-compile to musl, we just need to take care of rust-lang-deprecated/rust-buildbot#96. This basically will just require buildbot shenanigans.

@joshtriplett
Copy link
Member

When adding support for this, please do not disregard the ability to statically link with glibc. Yes, it isn't a completely self-contained static link if you reference NSS, but it's still useful.

@jirutka
Copy link
Contributor Author

jirutka commented Aug 8, 2016

Why would like to statically link with glibc? That doesn't make much sense for me.

@joshtriplett
Copy link
Member

Any number of reasons. For instance, when shipping a program that actually wants to resolve names and respect any configured NSS modules; a binary linked to musl will fail to do so. Or, when shipping a program that needs to work with any resolv.conf file, including those musl's limited resolv.conf handling doesn't like. Or, when statically linking to a C library that depends on glibc.

@jirutka
Copy link
Contributor Author

jirutka commented Aug 8, 2016

Well, that makes sense. However, the result binary will be probably very big.

@joshtriplett
Copy link
Member

Potentially, though static linking allows discarding unused parts of the library.

In any case, I just wanted to make sure that no assumptions like "static linking always uses musl" got hardcoded.

@jirutka
Copy link
Contributor Author

jirutka commented Aug 8, 2016

I just wanted to make sure that no assumptions like "static linking always uses musl" got hardcoded.

Totally agree, this decision that musl equals static linking was very wrong and it made porting rustc to Alpine Linux much harder.

@jirutka
Copy link
Contributor Author

jirutka commented Aug 8, 2016

Potentially, though static linking allows discarding unused parts of the library.

That’s actually interesting question – does rustc currently discard unused code?

@japaric
Copy link
Member

japaric commented Aug 8, 2016

That’s actually interesting question – does rustc currently discard unused code?

That would be mainly the linker's job since it's about eliminating glibc's code rather than rustc generated code.

@aidanhs
Copy link
Member

aidanhs commented Aug 9, 2016

Statically linking glibc isn't something that rust itself really supports right now. It's possible (I was doing it pre-musl) but it requires hacking around with the arguments rustc passes to the system linker - I'm not sure the cargo build process is the right place to start talking about it, there's likely a large number of assumptions already in rustc itself!

FWIW, I'm vaguely in favour of the ability to statically link glibc, but I'm pretty sure it's a bad idea to distribute static binaries which try to use NSS (like Cargo) - if you compile a C program like this it will tell you that you need the same version of glibc at runtime as you had at compile time. This is because have the static libc in your binary and you have the dynamic libc linked to NSS modules which could create interesting problems like the two libcs

  • clobbering the heap because they don't understand how to cooperate with each other
  • causing segfaults because you're implicitly relying on private details of libc that were never intended to be stable, like the layout of FILE, by passing data between them

I'd be interested to hear of statically linked binaries being distributed and using NSS on target machines successfully. I'm not sure how you could do that reliably?

@joshtriplett
Copy link
Member

Honestly, I'd be just as happy with a program that statically links everything except glibc, dynamically linking libc.so.6 and nothing else.

@zoumi
Copy link

zoumi commented Oct 25, 2016

@alexcrichton
Copy link
Member

Done!

@jirutka
Copy link
Contributor Author

jirutka commented Dec 29, 2016

Thanks a lot, but why it’s not available in https://static.rust-lang.org/cargo-dist/ as nightly builds for other platforms?

@alexcrichton
Copy link
Member

@jirutka
Copy link
Contributor Author

jirutka commented Dec 29, 2016

Then you’ve forgotten to update URIs in the README.

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

6 participants