Skip to content

Commit eda4050

Browse files
authored
Rollup merge of rust-lang#77386 - joshtriplett:static-glibc, r=petrochenkov
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.
2 parents ac5debc + 2e8f705 commit eda4050

File tree

8 files changed

+14
-9
lines changed

8 files changed

+14
-9
lines changed

Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -1623,9 +1623,9 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
16231623

16241624
[[package]]
16251625
name = "libc"
1626-
version = "0.2.77"
1626+
version = "0.2.78"
16271627
source = "registry+https://github.com/rust-lang/crates.io-index"
1628-
checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
1628+
checksum = "aa7087f49d294270db4e1928fc110c976cd4b9e5a16348e0a1df09afa99e6c98"
16291629
dependencies = [
16301630
"rustc-std-workspace-core",
16311631
]

compiler/rustc_target/src/spec/android_base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ pub fn opts() -> TargetOptions {
1212
base.position_independent_executables = true;
1313
base.has_elf_tls = false;
1414
base.requires_uwtable = true;
15+
base.crt_static_respected = false;
1516
base
1617
}

compiler/rustc_target/src/spec/linux_base.rs

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub fn opts() -> TargetOptions {
2828
position_independent_executables: true,
2929
relro_level: RelroLevel::Full,
3030
has_elf_tls: true,
31+
crt_static_respected: true,
3132
..Default::default()
3233
}
3334
}

compiler/rustc_target/src/spec/linux_musl_base.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ pub fn opts() -> TargetOptions {
1010

1111
// These targets statically link libc by default
1212
base.crt_static_default = true;
13-
// These targets allow the user to choose between static and dynamic linking.
14-
base.crt_static_respected = true;
1513

1614
base
1715
}

library/std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ cfg-if = { version = "0.1.8", features = ['rustc-dep-of-std'] }
1616
panic_unwind = { path = "../panic_unwind", optional = true }
1717
panic_abort = { path = "../panic_abort" }
1818
core = { path = "../core" }
19-
libc = { version = "0.2.77", default-features = false, features = ['rustc-dep-of-std'] }
19+
libc = { version = "0.2.78", default-features = false, features = ['rustc-dep-of-std'] }
2020
compiler_builtins = { version = "0.1.35" }
2121
profiler_builtins = { path = "../profiler_builtins", optional = true }
2222
unwind = { path = "../unwind" }

library/unwind/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ doc = false
1414

1515
[dependencies]
1616
core = { path = "../core" }
17-
libc = { version = "0.2.51", features = ['rustc-dep-of-std'], default-features = false }
17+
libc = { version = "0.2.78", features = ['rustc-dep-of-std'], default-features = false }
1818
compiler_builtins = "0.1.0"
1919
cfg-if = "0.1.8"
2020

library/unwind/build.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ fn main() {
1212
} else if target.contains("x86_64-fortanix-unknown-sgx") {
1313
llvm_libunwind::compile();
1414
} else if target.contains("linux") {
15+
// linking for Linux is handled in lib.rs
1516
if target.contains("musl") {
16-
// linking for musl is handled in lib.rs
1717
llvm_libunwind::compile();
18-
} else if !target.contains("android") {
19-
println!("cargo:rustc-link-lib=gcc_s");
2018
}
2119
} else if target.contains("freebsd") {
2220
println!("cargo:rustc-link-lib=gcc_s");

library/unwind/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ cfg_if::cfg_if! {
4242
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
4343
extern "C" {}
4444

45+
// When building with crt-static, we get `gcc_eh` from the `libc` crate, since
46+
// glibc needs it, and needs it listed later on the linker command line. We
47+
// don't want to duplicate it here.
48+
#[cfg(all(target_os = "linux", target_env = "gnu", not(feature = "llvm-libunwind")))]
49+
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]
50+
extern "C" {}
51+
4552
#[cfg(target_os = "redox")]
4653
#[link(name = "gcc_eh", kind = "static-nobundle", cfg(target_feature = "crt-static"))]
4754
#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]

0 commit comments

Comments
 (0)