Repository: rust
Issue: Rustc tries to link the Linux ASAN runtime when linker flavour is MSVC+LLD
Labels: T-compiler,O-windows-msvc,A-sanitizers,C-bug

When linking with `lld-link`, which provides the MSVC flavour linker with LLD subtype, the ASAN runtime selection falls through to the Linux branch and tries to link the non-existent `librustc-dev_rt.asan.a`library while targeting Windows.

A repro can be seen on Linux, cross-compiling for Windows. It needs a Windows sysroot and a clang/lld build:
```
% cargo init
    Creating binary (application) package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
```
```
% RUSTFLAGS="-Zsanitizer=address -Clinker=../c/src/third_party/llvm-build/Release+Asserts/bin/lld-link -Clink-arg=/libpath:../c/src/third_party/llvm-build/Release+Asserts/lib/clang/20/lib/windows -Clink-arg=/winsysroot:../c/src/third_party/depot_tools/win_toolchain/vs_files/7393122652" cargo build -Zbuild-std --target x86_64-pc-windows-msvc
   Compiling compiler_builtins v0.1.109
   Compiling core v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
   Compiling std v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std)
   Compiling rustc-std-workspace-core v1.99.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
   Compiling alloc v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc)
   Compiling cfg-if v1.0.0
   Compiling rustc-demangle v0.1.24
   Compiling unwind v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/unwind)
   Compiling rustc-std-workspace-alloc v1.99.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-alloc)
   Compiling panic_unwind v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/panic_unwind)
   Compiling panic_abort v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/panic_abort)
   Compiling hashbrown v0.14.5
   Compiling std_detect v0.1.5 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/stdarch/crates/std_detect)
   Compiling proc_macro v0.0.0 (/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/proc_macro)
   Compiling win-asan-repro v0.1.0 (/home/danakj/s/win-asan-repro)
error: linking with `../c/src/third_party/llvm-build/Release+Asserts/bin/lld-link` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/danakj/local/bin:/home/danakj/s/gsutil:/home/danakj/s/depot_tools:/home/danakj/s/ninja:/home/danakj/s/arcanist/bin:/home/danakj/s/brew/bin:/opt/firefox:/home/danakj/.cargo/bin:/bin:/usr/bin:/home/danakj/s/quickopen:/home/danakj/.cargo/bin" VSLANG="1033" "../c/src/third_party/llvm-build/Release+Asserts/bin/lld-link" "-flavor" "link" "/NOLOGO" "/tmp/rustcCJDwzU/symbols.o" "/WHOLEARCHIVE:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc/lib/librustc-nightly_rt.asan.a" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.0tdtfeujdyhjtaeasbm0d4fkx.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.199i99wnv1nalvuu3t1v5kp3t.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.20h9jqk82sswuuobak2qzucv5.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.3t56hhym87ffrajopna60eke0.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.6vz5im55mbl3ovk6o6y9izcq6.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.8i9ptnb83exgtut8kf28gftgy.rcgu.o" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.0vi8a9ni8tzhrhb9j22qz2uyj.rcgu.o" "/LIBPATH:/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps" "/LIBPATH:/home/danakj/s/win-asan-repro/target/debug/deps" "/LIBPATH:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc/lib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libstd-34f4d1dc3afbf24b.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libpanic_unwind-edca54b671061ee7.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/librustc_demangle-05840c8895460fd0.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libstd_detect-fb81dcb01cb8e1fb.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libhashbrown-2af4722702436af9.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/librustc_std_workspace_alloc-0c0095e7aff20699.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libunwind-dc807fd73b64a6ed.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libcfg_if-300e1ea37d4d7a24.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/liballoc-e1954f2d1c71f747.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/librustc_std_workspace_core-a5e34549c1c04a14.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libcore-259c5404709a9645.rlib" "/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/libcompiler_builtins-dda445e51fa2ca93.rlib" "kernel32.lib" "advapi32.lib" "kernel32.lib" "ntdll.lib" "userenv.lib" "ws2_32.lib" "kernel32.lib" "ws2_32.lib" "kernel32.lib" "/defaultlib:msvcrt" "/NXCOMPAT" "/LIBPATH:/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc/lib" "/OUT:/home/danakj/s/win-asan-repro/target/x86_64-pc-windows-msvc/debug/deps/win_asan_repro.exe" "/OPT:REF,NOICF" "/DEBUG" "/PDBALTPATH:%_PDB%" "/libpath:../c/src/third_party/llvm-build/Release+Asserts/lib/clang/20/lib/windows" "/winsysroot:../c/src/third_party/depot_tools/win_toolchain/vs_files/7393122652"
  = note: lld-link: error: could not open '/home/danakj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-pc-windows-msvc/lib/librustc-nightly_rt.asan.a': No such file or directory
```

The relevant parts of the command line are:
* Rustc flags: `-Zsanitizer=address -Clinker=../c/src/third_party/llvm-build/Release+Asserts/bin/lld-link"`
* Cargo flags: `--target x86_64-pc-windows-msvc`

The library choice is made here: https://github.com/rust-lang/rust/blob/0b16baa570d26224612ea27f76d68e4c6ca135cc/compiler/rustc_codegen_ssa/src/back/link.rs#L1349-L1351

The branch above would normally be used for MSVC flavour, but it's not used for LLD, so it falls down to the else branch which is for Linux.

Edited to hide the sysroot stuff, this seems okay and unrelated:
<details>
## --sysroot

The code here is _also ignoring `--sysroot`_. If we include a bogus but relative path `--sysroot=a/b/c` in `RUSTFLAGS`, the ASAN library should be specified as`a/b/c/lib/rustlib/x86_64-pc-windows-msvc/lib/` but it is always using the absolute path to the default sysroot.
</details>

Chromium tracking issue: crbug.com/371512562

cc: @rcvalle