Skip to content

Commit d76cff3

Browse files
committed
Only export the sanitizer symbols for LTO and move export code to cg_llvm
Don't export them from cdylibs. There is no need to do so and it complicates exported_non_generic_symbols. In addition the GCC backend likely uses different symbols and may potentially not even need us to explicitly tell it to export the symbols it needs.
1 parent 43a2166 commit d76cff3

File tree

2 files changed

+29
-48
lines changed

2 files changed

+29
-48
lines changed

compiler/rustc_codegen_llvm/src/back/lto.rs

Lines changed: 28 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,33 @@ 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+
4573
// __llvm_profile_counter_bias is pulled in at link time by an undefined reference to
4674
// __llvm_profile_runtime, therefore we won't know until link time if this symbol
4775
// 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

0 commit comments

Comments
 (0)