Skip to content

Commit 6a62c0a

Browse files
authored
Unrolled build for #145965
Rollup merge of #145965 - bjorn3:sanitize_symbol_export_improvements, r=lqd Move exporting of profiler and sanitizer symbols to the LLVM backend Only the LLVM backend needs those specific symbols exported and it only needs them to be exported for LTO, not from cdylibs in general.
2 parents 35d55b3 + 23e72ab commit 6a62c0a

File tree

3 files changed

+33
-54
lines changed

3 files changed

+33
-54
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
1515
use rustc_data_structures::fx::FxHashMap;
1616
use rustc_data_structures::memmap::Mmap;
1717
use rustc_errors::DiagCtxtHandle;
18+
use rustc_hir::attrs::SanitizerSet;
1819
use rustc_middle::bug;
1920
use rustc_middle::dep_graph::WorkProduct;
2021
use rustc_session::config::{self, Lto};
@@ -42,6 +43,37 @@ fn prepare_lto(
4243
.map(|symbol| CString::new(symbol.to_owned()).unwrap())
4344
.collect::<Vec<CString>>();
4445

46+
if cgcx.regular_module_config.instrument_coverage
47+
|| cgcx.regular_module_config.pgo_gen.enabled()
48+
{
49+
// These are weak symbols that point to the profile version and the
50+
// profile name, which need to be treated as exported so LTO doesn't nix
51+
// them.
52+
const PROFILER_WEAK_SYMBOLS: [&CStr; 2] =
53+
[c"__llvm_profile_raw_version", c"__llvm_profile_filename"];
54+
55+
symbols_below_threshold.extend(PROFILER_WEAK_SYMBOLS.iter().map(|&sym| sym.to_owned()));
56+
}
57+
58+
if cgcx.regular_module_config.sanitizer.contains(SanitizerSet::MEMORY) {
59+
let mut msan_weak_symbols = Vec::new();
60+
61+
// Similar to profiling, preserve weak msan symbol during LTO.
62+
if cgcx.regular_module_config.sanitizer_recover.contains(SanitizerSet::MEMORY) {
63+
msan_weak_symbols.push(c"__msan_keep_going");
64+
}
65+
66+
if cgcx.regular_module_config.sanitizer_memory_track_origins != 0 {
67+
msan_weak_symbols.push(c"__msan_track_origins");
68+
}
69+
70+
symbols_below_threshold.extend(msan_weak_symbols.into_iter().map(|sym| sym.to_owned()));
71+
}
72+
73+
// Preserve LLVM-injected, ASAN-related symbols.
74+
// See also https://github.com/rust-lang/rust/issues/113404.
75+
symbols_below_threshold.push(c"___asan_globals_registered".to_owned());
76+
4577
// __llvm_profile_counter_bias is pulled in at link time by an undefined reference to
4678
// __llvm_profile_runtime, therefore we won't know until link time if this symbol
4779
// should have default visibility.

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, Instance, SymbolNam
1515
use rustc_middle::util::Providers;
1616
use rustc_session::config::{CrateType, OomStrategy};
1717
use rustc_symbol_mangling::mangle_internal_symbol;
18-
use rustc_target::spec::{SanitizerSet, TlsModel};
18+
use rustc_target::spec::TlsModel;
1919
use tracing::debug;
2020

2121
use crate::base::allocator_kind_for_codegen;
@@ -242,53 +242,6 @@ fn exported_non_generic_symbols_provider_local<'tcx>(
242242
}
243243
}
244244

245-
if tcx.sess.instrument_coverage() || tcx.sess.opts.cg.profile_generate.enabled() {
246-
// These are weak symbols that point to the profile version and the
247-
// profile name, which need to be treated as exported so LTO doesn't nix
248-
// them.
249-
const PROFILER_WEAK_SYMBOLS: [&str; 2] =
250-
["__llvm_profile_raw_version", "__llvm_profile_filename"];
251-
252-
symbols.extend(PROFILER_WEAK_SYMBOLS.iter().map(|sym| {
253-
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
254-
(
255-
exported_symbol,
256-
SymbolExportInfo {
257-
level: SymbolExportLevel::C,
258-
kind: SymbolExportKind::Data,
259-
used: false,
260-
rustc_std_internal_symbol: false,
261-
},
262-
)
263-
}));
264-
}
265-
266-
if tcx.sess.opts.unstable_opts.sanitizer.contains(SanitizerSet::MEMORY) {
267-
let mut msan_weak_symbols = Vec::new();
268-
269-
// Similar to profiling, preserve weak msan symbol during LTO.
270-
if tcx.sess.opts.unstable_opts.sanitizer_recover.contains(SanitizerSet::MEMORY) {
271-
msan_weak_symbols.push("__msan_keep_going");
272-
}
273-
274-
if tcx.sess.opts.unstable_opts.sanitizer_memory_track_origins != 0 {
275-
msan_weak_symbols.push("__msan_track_origins");
276-
}
277-
278-
symbols.extend(msan_weak_symbols.into_iter().map(|sym| {
279-
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, sym));
280-
(
281-
exported_symbol,
282-
SymbolExportInfo {
283-
level: SymbolExportLevel::C,
284-
kind: SymbolExportKind::Data,
285-
used: false,
286-
rustc_std_internal_symbol: false,
287-
},
288-
)
289-
}));
290-
}
291-
292245
// Sort so we get a stable incr. comp. hash.
293246
symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
294247

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,12 +1226,6 @@ extern "C" void LLVMRustPrintPasses() {
12261226
extern "C" void LLVMRustRunRestrictionPass(LLVMModuleRef M, char **Symbols,
12271227
size_t Len) {
12281228
auto PreserveFunctions = [=](const GlobalValue &GV) {
1229-
// Preserve LLVM-injected, ASAN-related symbols.
1230-
// See also https://github.com/rust-lang/rust/issues/113404.
1231-
if (GV.getName() == "___asan_globals_registered") {
1232-
return true;
1233-
}
1234-
12351229
// Preserve symbols exported from Rust modules.
12361230
for (size_t I = 0; I < Len; I++) {
12371231
if (GV.getName() == Symbols[I]) {

0 commit comments

Comments
 (0)