@@ -15,6 +15,7 @@ use rustc_codegen_ssa::{ModuleCodegen, ModuleKind, looks_like_rust_object_file};
1515use rustc_data_structures:: fx:: FxHashMap ;
1616use rustc_data_structures:: memmap:: Mmap ;
1717use rustc_errors:: DiagCtxtHandle ;
18+ use rustc_hir:: attrs:: SanitizerSet ;
1819use rustc_middle:: bug;
1920use rustc_middle:: dep_graph:: WorkProduct ;
2021use 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.
0 commit comments