Skip to content

Linking error in rustc_llvm when using rustc_interface #87733

@gheoan

Description

@gheoan
Contributor

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.

Activity

gheoan

gheoan commented on Aug 3, 2021

@gheoan
ContributorAuthor

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

gheoan commented on Aug 3, 2021

@gheoan
ContributorAuthor

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

rustbot

rustbot commented on Aug 3, 2021

@rustbot
Collaborator

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.

added
A-driverArea: rustc_driver that ties everything together into the `rustc` compiler
requires-nightlyThis issue requires a nightly compiler in some way.
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Aug 3, 2021
added
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
on Mar 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-driverArea: rustc_driver that ties everything together into the `rustc` compilerT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @gheoan@workingjubilee@rustbot

        Issue actions

          Linking error in rustc_llvm when using rustc_interface · Issue #87733 · rust-lang/rust