Skip to content

Commit a5561ce

Browse files
committed
rustc: Don't export builtins/panic/alloc syms
This hides symbols from various unstable and implementation-detail crates of the standard library. Although typically transitive exported `pub extern` functions are exported from cdylibs, these crates aren't necessary as they're all implementation details. Closes #34493
1 parent 025fb7d commit a5561ce

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/liballoc_jemalloc/build.rs

+11
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,15 @@ fn main() {
181181
} else if !target.contains("windows") && !target.contains("musl") {
182182
println!("cargo:rustc-link-lib=pthread");
183183
}
184+
185+
// The pthread_atfork symbols is used by jemalloc on android but the really
186+
// old android we're building on doesn't have them defined, so just make
187+
// sure the symbols are available.
188+
if target.contains("androideabi") {
189+
println!("cargo:rerun-if-changed=pthread_atfork_dummy.c");
190+
gcc::Config::new()
191+
.flag("-fvisibility=hidden")
192+
.file("pthread_atfork_dummy.c")
193+
.compile("libpthread_atfork_dummy.a");
194+
}
184195
}

src/liballoc_jemalloc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ mod imp {
143143
// we're building on doesn't have them defined, so just make sure the symbols
144144
// are available.
145145
#[no_mangle]
146-
#[cfg(target_os = "android")]
146+
#[cfg(all(target_os = "android", not(cargobuild)))]
147147
pub extern "C" fn pthread_atfork(_prefork: *mut u8,
148148
_postfork_parent: *mut u8,
149149
_postfork_child: *mut u8)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// See comments in build.rs for why this exists
2+
int pthread_atfork(void* prefork,
3+
void* postfork_parent,
4+
void* postfork_child) {
5+
return 0;
6+
}

src/librustc_trans/back/symbol_export.rs

+32-1
Original file line numberDiff line numberDiff line change
@@ -81,19 +81,50 @@ impl ExportedSymbols {
8181
for cnum in scx.sess().cstore.crates() {
8282
debug_assert!(cnum != LOCAL_CRATE);
8383

84+
// If this crate is a plugin and/or a custom derive crate, then
85+
// we're not even going to link those in so we skip those crates.
8486
if scx.sess().cstore.plugin_registrar_fn(cnum).is_some() ||
8587
scx.sess().cstore.derive_registrar_fn(cnum).is_some() {
8688
continue;
8789
}
8890

91+
// Check to see if this crate is a "special runtime crate". These
92+
// crates, implementation details of the standard library, typically
93+
// have a bunch of `pub extern` and `#[no_mangle]` functions as the
94+
// ABI between them. We don't want their symbols to have a `C`
95+
// export level, however, as they're just implementation details.
96+
// Down below we'll hardwire all of the symbols to the `Rust` export
97+
// level instead.
98+
let special_runtime_crate =
99+
scx.sess().cstore.is_allocator(cnum) ||
100+
scx.sess().cstore.is_panic_runtime(cnum) ||
101+
scx.sess().cstore.is_compiler_builtins(cnum);
102+
89103
let crate_exports = scx
90104
.sess()
91105
.cstore
92106
.exported_symbols(cnum)
93107
.iter()
94108
.map(|&def_id| {
95109
let name = Instance::mono(scx, def_id).symbol_name(scx);
96-
let export_level = export_level(scx, def_id);
110+
let export_level = if special_runtime_crate {
111+
// We can probably do better here by just ensuring that
112+
// it has hidden visibility rather than public
113+
// visibility, as this is primarily here to ensure it's
114+
// not stripped during LTO.
115+
//
116+
// In general though we won't link right if these
117+
// symbols are stripped, and LTO currently strips them.
118+
if name == "rust_eh_personality" ||
119+
name == "rust_eh_register_frames" ||
120+
name == "rust_eh_unregister_frames" {
121+
SymbolExportLevel::C
122+
} else {
123+
SymbolExportLevel::Rust
124+
}
125+
} else {
126+
export_level(scx, def_id)
127+
};
97128
debug!("EXPORTED SYMBOL (re-export): {} ({:?})", name, export_level);
98129
(name, export_level)
99130
})

0 commit comments

Comments
 (0)