@@ -16,7 +16,7 @@ use rustc_middle::ty::{self, SymbolName, TyCtxt};
16
16
use rustc_middle:: ty:: { GenericArgKind , GenericArgsRef } ;
17
17
use rustc_middle:: util:: Providers ;
18
18
use rustc_session:: config:: { CrateType , OomStrategy } ;
19
- use rustc_target:: spec:: SanitizerSet ;
19
+ use rustc_target:: spec:: { SanitizerSet , TlsModel } ;
20
20
21
21
pub fn threshold ( tcx : TyCtxt < ' _ > ) -> SymbolExportLevel {
22
22
crates_export_threshold ( tcx. crate_types ( ) )
@@ -552,6 +552,12 @@ pub fn linking_symbol_name_for_instance_in_crate<'tcx>(
552
552
553
553
let mut undecorated = symbol_name_for_instance_in_crate ( tcx, symbol, instantiating_crate) ;
554
554
555
+ // thread local will not be a function call,
556
+ // so it is safe to return before windows symbol decoration check.
557
+ if let Some ( name) = maybe_emutls_symbol_name ( tcx, symbol, & undecorated) {
558
+ return name;
559
+ }
560
+
555
561
let target = & tcx. sess . target ;
556
562
if !target. is_like_windows {
557
563
// Mach-O has a global "_" suffix and `object` crate will handle it.
@@ -612,6 +618,32 @@ pub fn linking_symbol_name_for_instance_in_crate<'tcx>(
612
618
format ! ( "{prefix}{undecorated}{suffix}{args_in_bytes}" )
613
619
}
614
620
621
+ pub fn exporting_symbol_name_for_instance_in_crate < ' tcx > (
622
+ tcx : TyCtxt < ' tcx > ,
623
+ symbol : ExportedSymbol < ' tcx > ,
624
+ cnum : CrateNum ,
625
+ ) -> String {
626
+ let undecorated = symbol_name_for_instance_in_crate ( tcx, symbol, cnum) ;
627
+ maybe_emutls_symbol_name ( tcx, symbol, & undecorated) . unwrap_or ( undecorated)
628
+ }
629
+
630
+ fn maybe_emutls_symbol_name < ' tcx > (
631
+ tcx : TyCtxt < ' tcx > ,
632
+ symbol : ExportedSymbol < ' tcx > ,
633
+ undecorated : & str ,
634
+ ) -> Option < String > {
635
+ if matches ! ( tcx. sess. tls_model( ) , TlsModel :: Emulated )
636
+ && let ExportedSymbol :: NonGeneric ( def_id) = symbol
637
+ && tcx. is_thread_local_static ( def_id)
638
+ {
639
+ // When using emutls, LLVM will add the `__emutls_v.` prefix to thread local symbols,
640
+ // and exported symbol name need to match this.
641
+ Some ( format ! ( "__emutls_v.{undecorated}" ) )
642
+ } else {
643
+ None
644
+ }
645
+ }
646
+
615
647
fn wasm_import_module_map ( tcx : TyCtxt < ' _ > , cnum : CrateNum ) -> FxHashMap < DefId , String > {
616
648
// Build up a map from DefId to a `NativeLib` structure, where
617
649
// `NativeLib` internally contains information about
0 commit comments