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

Statically link against glibc #65447

Closed
NilsIrl opened this issue Oct 15, 2019 · 10 comments · Fixed by #77386
Closed

Statically link against glibc #65447

NilsIrl opened this issue Oct 15, 2019 · 10 comments · Fixed by #77386
Labels
A-linkage Area: linking into static, shared libraries and binaries A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@NilsIrl
Copy link

NilsIrl commented Oct 15, 2019

Reasons for:

  1. Performance. According to this reddit message, musl caused a program to run 9x slower. Musl is also known to be slower in certain configurations in C. This varies a lot with the use case and some might even see a performance increase with musl.
  2. Smaller docker containers using FROM scratch. example using musl

It is possible to link glibc statically as demonstrated here.

From what I can tell, on the user facing side, this would be enable using -C target-feature=+crt-static as described in the reference.

@jonas-schievink jonas-schievink added A-linkage Area: linking into static, shared libraries and binaries A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Oct 15, 2019
@the8472
Copy link
Member

the8472 commented Oct 15, 2019

The reddit thread mentions libm. And another source of speedups might be the malloc implementation (glibc 2.26 introduced thread-local caches). One can already use custom global allocators, maybe it would make sense to link a custom libm too?

@mati865
Copy link
Contributor

mati865 commented Oct 15, 2019

First blocker is the license. Glibc is licensed under LGPL, it has dynamic linking exception so one can freely link dynamically to it. Static linking doesn't fall under this exception. I'm not a lawyer but I think Rust would have to be licensed as (L)GPL to fit it.

Another thing is glibc doesn't really support static linking, sure it works well for small software but bites you when you need more of it's features.

@NilsIrl
Copy link
Author

NilsIrl commented Oct 15, 2019

First blocker is the license. Glibc is licensed under LGPL, it has dynamic linking exception so one can freely link dynamically to it. Static linking doesn't fall under this exception. I'm not a lawyer but I think Rust would have to be licensed as (L)GPL to fit it.

(Obviously not a lawyer but), I'm pretty sure that as long as the code it is linked against is (L)GPLed, it should work.

@NilsIrl
Copy link
Author

NilsIrl commented Oct 15, 2019

https://www.gnu.org/licenses/gpl-faq.html#LGPLStaticVsDynamic

Also as long as the program it is linked against is licensed under a GPL compatible license, it is allowed. The MIT license, is compatible. All this information can be confirmed by looking at the above link.

You can even link proprietary code against LGPL if you provide the object files.

@haraldh
Copy link
Contributor

haraldh commented Jan 24, 2020

@joshtriplett
Copy link
Member

@haraldh The former should be doable. The latter is a tradeoff that people will need to understand if they statically link glibc, and it's something they already have to deal with if they use gcc -static.

@petrochenkov
Copy link
Contributor

Implementation instructions:

@petrochenkov
Copy link
Contributor

@joshtriplett

I'd be happy to submit the necessary changes, if you can outline what needs doing. (#72274 (comment))

See the comment above.

@joshtriplett
Copy link
Member

@petrochenkov Thanks!

@joshtriplett
Copy link
Member

I have this successfully working; just waiting on a release of the libc crate containing rust-lang/libc#1899 .

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Oct 3, 2020
…henkov

Support static linking with glibc and target-feature=+crt-static

With this change, it's possible to build on a linux-gnu target and pass
RUSTFLAGS='-C target-feature=+crt-static' or the equivalent via a
`.cargo/config.toml` file, and get a statically linked executable.

Update to libc 0.2.78, which adds support for static linking with glibc.

Add `crt_static_respected` to the `linux_base` target spec.

Update `android_base` and `linux_musl_base` accordingly. Avoid enabling
crt_static_respected on Android platforms, since that hasn't been
tested.

Closes rust-lang#65447.
@bors bors closed this as completed in 98edd1f Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries A-runtime Area: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflows C-feature-request Category: A feature request, i.e: not implemented / a PR. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants