Repository: rust
Issue: Linking error in rustc_llvm when using rustc_interface
Labels: A-LLVM,A-driver,T-compiler,requires-nightly

When executing `rustc test.rs` on the code below, linking fails.
```rust
// test.rs
#![crate_type = "cdylib"]
#![feature(rustc_private)]
extern crate rustc_interface; 
```
The linker error is:
```
error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "D:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\VC\\Tools\\MSVC\\14.21.27702\\bin\\HostX64\\x64\\link.exe" [..]
  = note:    Creating library test.dll.lib and object test.dll.exp
      msvcrt.lib(initializers.obj) : warning LNK4098: defaultlib 'libcmt.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
      librustc_llvm-b2b2c2603e5831d1.rlib(DataFlowSanitizer.cpp.obj) : error LNK2001: unresolved external symbol __imp___std_init_once_begin_initialize
      [..]
      librustc_llvm-b2b2c2603e5831d1.rlib(DataFlowSanitizer.cpp.obj) : error LNK2001: unresolved external symbol __imp___std_init_once_complete
      [..]
      librustc_llvm-b2b2c2603e5831d1.rlib(Magic.cpp.obj) : error LNK2001: unresolved external symbol __std_system_error_allocate_message
      [..]
      librustc_llvm-b2b2c2603e5831d1.rlib(Magic.cpp.obj) : error LNK2001: unresolved external symbol __std_system_error_deallocate_message
      [..]
      librustc_llvm-b2b2c2603e5831d1.rlib(GCOVProfiling.cpp.obj) : error LNK2019: unresolved external symbol __std_reverse_copy_trivially_copyable_1 referenced in function "private: bool __cdecl `anonymous namespace'::GCOVProfiler::emitProfileNotes(class llvm::NamedMDNode *,bool,class NamedMDNode::function_ref<class llvm::BlockFrequencyInfo * __cdecl(class llvm::Function &)>,class NamedMDNode::function_ref<class llvm::BranchProbabilityInfo * __cdecl(class llvm::Function &)>,class NamedMDNode::function_ref<class llvm::TargetLibraryInfo const & __cdecl(class llvm::Function &)>)" (?emitProfileNotes@GCOVProfiler@?A0x7391425A@@AEAA_NPEAVNamedMDNode@llvm@@_NV?$function_ref@$$A6APEAVBlockFrequencyInfo@llvm@@AEAVFunction@2@@Z@3@V?$function_ref@$$A6APEAVBranchProbabilityInfo@llvm@@AEAVFunction@2@@Z@3@V?$function_ref@$$A6AAEBVTargetLibraryInfo@llvm@@AEAVFunction@2@@Z@3@@Z)
      test.dll : fatal error LNK1120: 5 unresolved externals
```

I'm using the `nightly-x86_64-pc-windows-msvc` toolchain via rustup. The compiler version is `rustc 1.56.0-nightly (4e282795d 2021-07-31)`. The link.exe version is `14.21.27702.2`.

See my next comment for the error displayed by lld.

I can reproduce this error using the following crate types: `proc-macro`, `dylib` and `cdylib`. The `staticlib`, `rlib` and `bin` crate types do not fail.

Linking also fails with `extern crate rustc_llvm` instead of `rustc_interface`.

May be related to https://github.com/rust-lang/rust/issues/81381.

All the failing cases can be solved by an additional `extern crate rustc_driver`. Is it possible to use rustc_interface without linking with rustc_driver? If not, is there a way to suggest to the user to add a dependency on rustc_driver? It was difficult for me to figure out that the error does not manifest when rustc_driver is used.