Skip to content

Commit 98edd1f

Browse files
committed
Auto merge of #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 #65447.
2 parents 9fdaeb3 + d9f29fd commit 98edd1f

File tree

9 files changed

+17
-32
lines changed

9 files changed

+17
-32
lines changed

Cargo.lock

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

16611661
[[package]]
16621662
name = "libc"
1663-
version = "0.2.77"
1663+
version = "0.2.79"
16641664
source = "registry+https://github.com/rust-lang/crates.io-index"
1665-
checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
1665+
checksum = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743"
16661666
dependencies = [
16671667
"rustc-std-workspace-core",
16681668
]

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.79", 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/std/src/sys/unix/process/process_unix.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -461,39 +461,19 @@ impl ExitStatus {
461461
}
462462

463463
fn exited(&self) -> bool {
464-
// On Linux-like OSes this function is safe, on others it is not. See
465-
// libc issue: https://github.com/rust-lang/libc/issues/1888.
466-
#[cfg_attr(
467-
any(target_os = "linux", target_os = "android", target_os = "emscripten"),
468-
allow(unused_unsafe)
469-
)]
470-
unsafe {
471-
libc::WIFEXITED(self.0)
472-
}
464+
libc::WIFEXITED(self.0)
473465
}
474466

475467
pub fn success(&self) -> bool {
476468
self.code() == Some(0)
477469
}
478470

479471
pub fn code(&self) -> Option<i32> {
480-
// On Linux-like OSes this function is safe, on others it is not. See
481-
// libc issue: https://github.com/rust-lang/libc/issues/1888.
482-
#[cfg_attr(
483-
any(target_os = "linux", target_os = "android", target_os = "emscripten"),
484-
allow(unused_unsafe)
485-
)]
486-
if self.exited() { Some(unsafe { libc::WEXITSTATUS(self.0) }) } else { None }
472+
if self.exited() { Some(libc::WEXITSTATUS(self.0)) } else { None }
487473
}
488474

489475
pub fn signal(&self) -> Option<i32> {
490-
// On Linux-like OSes this function is safe, on others it is not. See
491-
// libc issue: https://github.com/rust-lang/libc/issues/1888.
492-
#[cfg_attr(
493-
any(target_os = "linux", target_os = "android", target_os = "emscripten"),
494-
allow(unused_unsafe)
495-
)]
496-
if !self.exited() { Some(unsafe { libc::WTERMSIG(self.0) }) } else { None }
476+
if !self.exited() { Some(libc::WTERMSIG(self.0)) } else { None }
497477
}
498478
}
499479

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.79", 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)