@@ -1186,15 +1186,22 @@ mod win {
1186
1186
}
1187
1187
}
1188
1188
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
+ ) {
1190
1195
// On macOS the runtimes are distributed as dylibs which should be linked to
1191
1196
// both executables and dynamic shared objects. Everywhere else the runtimes
1192
1197
// are currently distributed as static libraries which should be linked to
1193
1198
// executables only.
1194
1199
let needs_runtime = !sess. target . is_like_android
1195
1200
&& match crate_type {
1196
1201
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
+ }
1198
1205
CrateType :: Rlib | CrateType :: Staticlib => false ,
1199
1206
} ;
1200
1207
@@ -1204,26 +1211,31 @@ fn add_sanitizer_libraries(sess: &Session, crate_type: CrateType, linker: &mut d
1204
1211
1205
1212
let sanitizer = sess. opts . unstable_opts . sanitizer ;
1206
1213
if sanitizer. contains ( SanitizerSet :: ADDRESS ) {
1207
- link_sanitizer_runtime ( sess, linker, "asan" ) ;
1214
+ link_sanitizer_runtime ( sess, flavor , linker, "asan" ) ;
1208
1215
}
1209
1216
if sanitizer. contains ( SanitizerSet :: LEAK ) {
1210
- link_sanitizer_runtime ( sess, linker, "lsan" ) ;
1217
+ link_sanitizer_runtime ( sess, flavor , linker, "lsan" ) ;
1211
1218
}
1212
1219
if sanitizer. contains ( SanitizerSet :: MEMORY ) {
1213
- link_sanitizer_runtime ( sess, linker, "msan" ) ;
1220
+ link_sanitizer_runtime ( sess, flavor , linker, "msan" ) ;
1214
1221
}
1215
1222
if sanitizer. contains ( SanitizerSet :: THREAD ) {
1216
- link_sanitizer_runtime ( sess, linker, "tsan" ) ;
1223
+ link_sanitizer_runtime ( sess, flavor , linker, "tsan" ) ;
1217
1224
}
1218
1225
if sanitizer. contains ( SanitizerSet :: HWADDRESS ) {
1219
- link_sanitizer_runtime ( sess, linker, "hwasan" ) ;
1226
+ link_sanitizer_runtime ( sess, flavor , linker, "hwasan" ) ;
1220
1227
}
1221
1228
if sanitizer. contains ( SanitizerSet :: SAFESTACK ) {
1222
- link_sanitizer_runtime ( sess, linker, "safestack" ) ;
1229
+ link_sanitizer_runtime ( sess, flavor , linker, "safestack" ) ;
1223
1230
}
1224
1231
}
1225
1232
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
+ ) {
1227
1239
fn find_sanitizer_runtime ( sess : & Session , filename : & str ) -> PathBuf {
1228
1240
let session_tlib =
1229
1241
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) {
1254
1266
let rpath = path. to_str ( ) . expect ( "non-utf8 component in path" ) ;
1255
1267
linker. args ( & [ "-Wl,-rpath" , "-Xlinker" , rpath] ) ;
1256
1268
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" ) ;
1257
1273
} else {
1258
1274
let filename = format ! ( "librustc{channel}_rt.{name}.a" ) ;
1259
1275
let path = find_sanitizer_runtime ( sess, & filename) . join ( & filename) ;
@@ -2076,7 +2092,7 @@ fn linker_with_args<'a>(
2076
2092
) ;
2077
2093
2078
2094
// Sanitizer libraries.
2079
- add_sanitizer_libraries ( sess, crate_type, cmd) ;
2095
+ add_sanitizer_libraries ( sess, flavor , crate_type, cmd) ;
2080
2096
2081
2097
// Object code from the current crate.
2082
2098
// Take careful note of the ordering of the arguments we pass to the linker
0 commit comments