Skip to content

Commit 8367fb7

Browse files
committed
Auto merge of #39252 - alexcrichton:less-exports, r=nrc
Hide a few more standard library symbols These commits touch up some of the symbol visibility rules for some crates related to the standard library, notably: * Symbols that are `pub extern` and `#[no_mangle]` which are internal-to-rust ABI things are no longer at the `C` export level, but the `Rust` export level. This includes allocators, panic runtimes, and compiler builtins. * The libbacktrace library is now compiled with `-fvisibility=hidden` to ensure that we don't export those symbols.
2 parents fece9c7 + 3d6f263 commit 8367fb7

File tree

5 files changed

+63
-4
lines changed

5 files changed

+63
-4
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,16 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// See comments in build.rs for why this exists
12+
int pthread_atfork(void* prefork,
13+
void* postfork_parent,
14+
void* postfork_child) {
15+
return 0;
16+
}

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
})

src/libstd/build.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ fn build_libbacktrace(host: &str, target: &str) {
8787
let compiler = gcc::Config::new().get_compiler();
8888
// only msvc returns None for ar so unwrap is okay
8989
let ar = build_helper::cc2ar(compiler.path(), target).unwrap();
90-
let cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
91-
.collect::<Vec<_>>().join(" ");
90+
let mut cflags = compiler.args().iter().map(|s| s.to_str().unwrap())
91+
.collect::<Vec<_>>().join(" ");
92+
cflags.push_str(" -fvisibility=hidden");
9293
run(Command::new("sh")
9394
.current_dir(&build_dir)
9495
.arg(src_dir.join("configure").to_str().unwrap()

0 commit comments

Comments
 (0)