@@ -16,7 +16,7 @@ use rustc_middle::ty::{self, SymbolName, TyCtxt};
1616use rustc_middle:: ty:: { GenericArgKind , GenericArgsRef } ;
1717use rustc_middle:: util:: Providers ;
1818use rustc_session:: config:: { CrateType , OomStrategy } ;
19- use rustc_target:: spec:: SanitizerSet ;
19+ use rustc_target:: spec:: { SanitizerSet , TlsModel } ;
2020
2121pub fn threshold ( tcx : TyCtxt < ' _ > ) -> SymbolExportLevel {
2222 crates_export_threshold ( tcx. crate_types ( ) )
@@ -552,6 +552,12 @@ pub fn linking_symbol_name_for_instance_in_crate<'tcx>(
552552
553553 let mut undecorated = symbol_name_for_instance_in_crate ( tcx, symbol, instantiating_crate) ;
554554
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+
555561 let target = & tcx. sess . target ;
556562 if !target. is_like_windows {
557563 // 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>(
612618 format ! ( "{prefix}{undecorated}{suffix}{args_in_bytes}" )
613619}
614620
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+
615647fn wasm_import_module_map ( tcx : TyCtxt < ' _ > , cnum : CrateNum ) -> FxHashMap < DefId , String > {
616648 // Build up a map from DefId to a `NativeLib` structure, where
617649 // `NativeLib` internally contains information about
0 commit comments