@@ -1186,15 +1186,22 @@ mod win {
11861186 }
11871187}
11881188
1189- fn add_sanitizer_libraries ( sess : & Session , crate_type : CrateType , linker : & mut dyn Linker ) {
1189+ fn add_sanitizer_libraries (
1190+ sess : & Session ,
1191+ flavor : LinkerFlavor ,
1192+ crate_type : CrateType ,
1193+ linker : & mut dyn Linker ,
1194+ ) {
11901195 // On macOS the runtimes are distributed as dylibs which should be linked to
11911196 // both executables and dynamic shared objects. Everywhere else the runtimes
11921197 // are currently distributed as static libraries which should be linked to
11931198 // executables only.
11941199 let needs_runtime = !sess. target . is_like_android
11951200 && match crate_type {
11961201 CrateType :: Executable => true ,
1197- CrateType :: Dylib | CrateType :: Cdylib | CrateType :: ProcMacro => sess. target . is_like_osx ,
1202+ CrateType :: Dylib | CrateType :: Cdylib | CrateType :: ProcMacro => {
1203+ sess. target . is_like_osx || sess. target . is_like_msvc
1204+ }
11981205 CrateType :: Rlib | CrateType :: Staticlib => false ,
11991206 } ;
12001207
@@ -1204,26 +1211,31 @@ fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut d
12041211
12051212 let sanitizer = sess. opts . unstable_opts . sanitizer ;
12061213 if sanitizer. contains ( SanitizerSet :: ADDRESS ) {
1207- link_sanitizer_runtime ( sess, linker, "asan" ) ;
1214+ link_sanitizer_runtime ( sess, flavor , linker, "asan" ) ;
12081215 }
12091216 if sanitizer. contains ( SanitizerSet :: LEAK ) {
1210- link_sanitizer_runtime ( sess, linker, "lsan" ) ;
1217+ link_sanitizer_runtime ( sess, flavor , linker, "lsan" ) ;
12111218 }
12121219 if sanitizer. contains ( SanitizerSet :: MEMORY ) {
1213- link_sanitizer_runtime ( sess, linker, "msan" ) ;
1220+ link_sanitizer_runtime ( sess, flavor , linker, "msan" ) ;
12141221 }
12151222 if sanitizer. contains ( SanitizerSet :: THREAD ) {
1216- link_sanitizer_runtime ( sess, linker, "tsan" ) ;
1223+ link_sanitizer_runtime ( sess, flavor , linker, "tsan" ) ;
12171224 }
12181225 if sanitizer. contains ( SanitizerSet :: HWADDRESS ) {
1219- link_sanitizer_runtime ( sess, linker, "hwasan" ) ;
1226+ link_sanitizer_runtime ( sess, flavor , linker, "hwasan" ) ;
12201227 }
12211228 if sanitizer. contains ( SanitizerSet :: SAFESTACK ) {
1222- link_sanitizer_runtime ( sess, linker, "safestack" ) ;
1229+ link_sanitizer_runtime ( sess, flavor , linker, "safestack" ) ;
12231230 }
12241231}
12251232
1226- fn link_sanitizer_runtime ( sess : & Session , linker : & mut dyn Linker , name : & str ) {
1233+ fn link_sanitizer_runtime (
1234+ sess : & Session ,
1235+ flavor : LinkerFlavor ,
1236+ linker : & mut dyn Linker ,
1237+ name : & str ,
1238+ ) {
12271239 fn find_sanitizer_runtime ( sess : & Session , filename : & str ) -> PathBuf {
12281240 let session_tlib =
12291241 filesearch:: make_target_lib_path ( & sess. sysroot , sess. opts . target_triple . triple ( ) ) ;
@@ -1254,6 +1266,10 @@ fn link_sanitizer_runtime(sess: &Session, linker: &mut dyn Linker, name: &str) {
12541266 let rpath = path. to_str ( ) . expect ( "non-utf8 component in path" ) ;
12551267 linker. args ( & [ "-Wl,-rpath" , "-Xlinker" , rpath] ) ;
12561268 linker. link_dylib ( & filename, false , true ) ;
1269+ } else if sess. target . is_like_msvc && flavor == LinkerFlavor :: Msvc ( Lld :: No ) && name == "asan" {
1270+ // MSVC provides the `/INFERASANLIBS` argument to automatically find the
1271+ // compatible ASAN library.
1272+ linker. arg ( "/INFERASANLIBS" ) ;
12571273 } else {
12581274 let filename = format ! ( "librustc{channel}_rt.{name}.a" ) ;
12591275 let path = find_sanitizer_runtime ( sess, & filename) . join ( & filename) ;
@@ -2076,7 +2092,7 @@ fn linker_with_args<'a>(
20762092 ) ;
20772093
20782094 // Sanitizer libraries.
2079- add_sanitizer_libraries ( sess, crate_type, cmd) ;
2095+ add_sanitizer_libraries ( sess, flavor , crate_type, cmd) ;
20802096
20812097 // Object code from the current crate.
20822098 // Take careful note of the ordering of the arguments we pass to the linker
0 commit comments