Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linking error in rustc_llvm when using rustc_interface #87733

Open
gheoan opened this issue Aug 3, 2021 · 3 comments
Open

Linking error in rustc_llvm when using rustc_interface #87733

gheoan opened this issue Aug 3, 2021 · 3 comments
Labels
A-driver Area: rustc_driver that ties everything together into the `rustc` compiler A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gheoan
Copy link
Contributor

gheoan commented Aug 3, 2021

When executing rustc test.rs on the code below, linking fails.

// 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 #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.

@gheoan
Copy link
Contributor Author

gheoan commented Aug 3, 2021

Here is lld's output when executing rustc -C linker=lld-link.exe test.rs:

error: linking with `lld-link.exe` failed: exit code: 1
  |
  = note: "lld-link.exe" [..]
  = note: lld-link: error: undefined symbol: __declspec(dllimport) __std_init_once_begin_initialize
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(X86TargetMachine.cpp.obj):(void __cdecl llvm::initializeX86ExecutionDomainFixPass(class llvm::PassRegistry &))
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(ARMTargetMachine.cpp.obj):(void __cdecl llvm::initializeARMExecutionDomainFixPass(class llvm::PassRegistry &))
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(X86LowerAMXType.cpp.obj):(void __cdecl llvm::initializeX86LowerAMXTypeLegacyPassPass(class llvm::PassRegistry &))
          >>> referenced 483 more times

          lld-link: error: undefined symbol: __declspec(dllimport) __std_init_once_complete
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(X86TargetMachine.cpp.obj):(void __cdecl llvm::initializeX86ExecutionDomainFixPass(class llvm::PassRegistry &))
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(ARMTargetMachine.cpp.obj):(void __cdecl llvm::initializeARMExecutionDomainFixPass(class llvm::PassRegistry &))
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(X86LowerAMXType.cpp.obj):(void __cdecl llvm::initializeX86LowerAMXTypeLegacyPassPass(class llvm::PassRegistry &))
          >>> referenced 483 more times

          lld-link: error: undefined symbol: __std_system_error_allocate_message
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(X86LoadValueInjectionLoadHardening.cpp.obj):(public: virtual class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> __cdecl std::_System_error_category::message(int) const)
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(ErrorHandling.cpp.obj)
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(Error.cpp.obj)

          lld-link: error: undefined symbol: __std_system_error_deallocate_message
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(X86LoadValueInjectionLoadHardening.cpp.obj):(public: virtual class std::basic_string<char, struct std::char_traits<char>, class std::allocator<char>> __cdecl std::_System_error_category::message(int) const)
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(ErrorHandling.cpp.obj)
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(Error.cpp.obj)

          lld-link: error: undefined symbol: __std_reverse_copy_trivially_copyable_1
          >>> referenced by librustc_llvm-b2b2c2603e5831d1.rlib(GCOVProfiling.cpp.obj):(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 &)>))

@gheoan
Copy link
Contributor Author

gheoan commented Aug 3, 2021

@rustbot modify labels: +T-compiler +requires-nightly +A-driver

@rustbot
Copy link
Collaborator

rustbot commented Aug 3, 2021

Error: Label B-unstable can only be set by Rust team members

Please let @rust-lang/release know if you're having trouble with this bot.

@rustbot rustbot added A-driver Area: rustc_driver that ties everything together into the `rustc` compiler requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 3, 2021
@workingjubilee workingjubilee added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-driver Area: rustc_driver that ties everything together into the `rustc` compiler A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants