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

ThinLTO breaks MSVC `_imp_` symbols #45347

Closed
alexcrichton opened this Issue Oct 17, 2017 · 1 comment

Comments

Projects
None yet
1 participant
@alexcrichton
Copy link
Member

alexcrichton commented Oct 17, 2017

I believe this is a minimization of this reported issue:

$ cat bar.rs
pub static A: u32 = 43;

pub mod a {
    pub static A: u32 = 43;
}
$ cat foo.rs
extern crate bar;

fn main() {
    println!("{}", bar::A);
}
$ rustc +nightly bar.rs -C prefer-dynamic -Z thinlto -C codegen-units=1000 --crate-type dylib,rlib -O
$ rustc +nightly foo.rs -L . -Z thinlto -Z print-link-args
error: linking with `link.exe` failed: exit code: 1120
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Tools\\MSVC\\14.10.25017\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "foo.foo0-8cd878b7c8d78940dfe6697baf5b88ec-exe.rs.rust-cgu.o" "foo.foo1-8cd878b7c8d78940dfe6697baf5b88ec-exe.rs.rust-cgu.o" "/OUT:foo.exe" "foo.crate.allocator.rust-cgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\etc\\libcore.natvis" "/LIBPATH:." "/LIBPATH:C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\msys64\\code\\rustfmt\\libbar.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-2c973d468aa52913.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-10dcb2bb73715353.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-eac2a22c7ef68dc5.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-6232007b55cca9f2.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc_system-968b8d6a8dcae5d4.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-bba7ff6cfd33a952.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd_unicode-6001d57e726caccd.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librand-d98f06e2ac3721a9.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-15daeee4c05dda88.rlib" "C:\\Users\\alex\\.rustup\\toolchains\\nightly-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-b02255a955089c1d.rlib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "shell32.lib" "msvcrt.lib"
  = note: foo.foo0-8cd878b7c8d78940dfe6697baf5b88ec-exe.rs.rust-cgu.o : error LNK2019: unresolved external symbol __imp__ZN3bar1A17h8c3f932ba2a49b51E referenced in function _ZN3foo4main17hd45fff29dc617b3fE.llvm.C758FA11
          foo.exe : fatal error LNK1120: 1 unresolved externals


error: aborting due to previous error
@alexcrichton

This comment has been minimized.

Copy link
Member Author

alexcrichton commented Oct 17, 2017

I've got a fix for this in the works.

alexcrichton added a commit to alexcrichton/rust that referenced this issue Oct 18, 2017

rustc: Add `_imp_` symbols later in compilation
On MSVC targets rustc will add symbols prefixed with `_imp_` to LLVM modules to
"emulate" dllexported statics as that workaround is still in place after rust-lang#27438
hasn't been solved otherwise. These statics, however, were getting gc'd by
ThinLTO accidentally which later would cause linking failures.

This commit updates the location we add such symbols to happen just before
codegen to ensure that (a) they're not eliminated by the optimizer and (b) the
optimizer doesn't even worry about them.

Closes rust-lang#45347

alexcrichton added a commit to alexcrichton/rust that referenced this issue Oct 18, 2017

rustc: Add `_imp_` symbols later in compilation
On MSVC targets rustc will add symbols prefixed with `_imp_` to LLVM modules to
"emulate" dllexported statics as that workaround is still in place after rust-lang#27438
hasn't been solved otherwise. These statics, however, were getting gc'd by
ThinLTO accidentally which later would cause linking failures.

This commit updates the location we add such symbols to happen just before
codegen to ensure that (a) they're not eliminated by the optimizer and (b) the
optimizer doesn't even worry about them.

Closes rust-lang#45347

bors added a commit that referenced this issue Oct 20, 2017

Auto merge of #45348 - alexcrichton:thinlto-timp, r=michaelwoerister
rustc: Add `_imp_` symbols later in compilation

On MSVC targets rustc will add symbols prefixed with `_imp_` to LLVM modules to
"emulate" dllexported statics as that workaround is still in place after #27438
hasn't been solved otherwise. These statics, however, were getting gc'd by
ThinLTO accidentally which later would cause linking failures.

This commit updates the location we add such symbols to happen just before
codegen to ensure that (a) they're not eliminated by the optimizer and (b) the
optimizer doesn't even worry about them.

Closes #45347

bors added a commit that referenced this issue Oct 20, 2017

Auto merge of #45348 - alexcrichton:thinlto-timp, r=michaelwoerister
rustc: Add `_imp_` symbols later in compilation

On MSVC targets rustc will add symbols prefixed with `_imp_` to LLVM modules to
"emulate" dllexported statics as that workaround is still in place after #27438
hasn't been solved otherwise. These statics, however, were getting gc'd by
ThinLTO accidentally which later would cause linking failures.

This commit updates the location we add such symbols to happen just before
codegen to ensure that (a) they're not eliminated by the optimizer and (b) the
optimizer doesn't even worry about them.

Closes #45347

@bors bors closed this in #45348 Oct 21, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.