Skip to content

Commit 7c79621

Browse files
authored
Rollup merge of #129079 - Zoxc:thinlto_imp_symbols, r=wesleywiser
Create `_imp__` symbols also when doing ThinLTO When generating a rlib crate on Windows we create `dllimport` / `_imp__` symbols for each global. This effectively makes the rlib contain an import library for itself and allows them to both be dynamically and statically linked. However when doing ThinLTO we do not generate these and thus we end up with missing symbols. Microsoft's `link` can fix these up (and emits warnings), but `lld` seems to currently be unable to. This PR also does this generation for ThinLTO avoiding those issues with `lld` and also avoids the warnings on `link`. This is an workaround for #81408. cc `@lqd`
2 parents 484c8e7 + 562c0d1 commit 7c79621

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -2164,8 +2164,14 @@ fn msvc_imps_needed(tcx: TyCtxt<'_>) -> bool {
21642164
&& tcx.sess.opts.cg.prefer_dynamic)
21652165
);
21662166

2167+
// We need to generate _imp__ symbol if we are generating an rlib or we include one
2168+
// indirectly from ThinLTO. In theory these are not needed as ThinLTO could resolve
2169+
// these, but it currently does not do so.
2170+
let can_have_static_objects =
2171+
tcx.sess.lto() == Lto::Thin || tcx.crate_types().iter().any(|ct| *ct == CrateType::Rlib);
2172+
21672173
tcx.sess.target.is_like_windows &&
2168-
tcx.crate_types().iter().any(|ct| *ct == CrateType::Rlib) &&
2174+
can_have_static_objects &&
21692175
// ThinLTO can't handle this workaround in all cases, so we don't
21702176
// emit the `__imp_` symbols. Instead we make them unnecessary by disallowing
21712177
// dynamic linking when linker plugin LTO is enabled.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::sync::atomic::{AtomicPtr, Ordering};
2+
3+
#[inline(always)]
4+
pub fn memrchr() {
5+
fn detect() {}
6+
7+
static CROSS_CRATE_STATIC_ITEM: AtomicPtr<()> = AtomicPtr::new(detect as *mut ());
8+
9+
unsafe {
10+
let fun = CROSS_CRATE_STATIC_ITEM.load(Ordering::SeqCst);
11+
std::mem::transmute::<*mut (), fn()>(fun)()
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
extern crate issue_81408;
2+
3+
fn main() {
4+
issue_81408::memrchr();
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This is a non-regression test for issue #81408 involving an lld bug and ThinLTO, on windows.
2+
// MSVC's link.exe doesn't need any workarounds in rustc, but lld does, so we'll check that the
3+
// binary runs successfully instead of using a codegen test.
4+
5+
//@ only-x86_64-pc-windows-msvc
6+
//@ needs-rust-lld
7+
//@ ignore-cross-compile: the built binary is executed
8+
9+
use run_make_support::{run, rustc};
10+
11+
fn test_with_linker(linker: &str) {
12+
rustc().input("issue_81408.rs").crate_name("issue_81408").crate_type("lib").opt().run();
13+
rustc()
14+
.input("main.rs")
15+
.crate_type("bin")
16+
.arg("-Clto=thin")
17+
.opt()
18+
.arg(&format!("-Clinker={linker}"))
19+
.extern_("issue_81408", "libissue_81408.rlib")
20+
.run();
21+
22+
// To make possible failures clearer, print an intro that will only be shown if the test does
23+
// fail when running the binary.
24+
eprint!("Running binary linked with {linker}... ");
25+
run("main");
26+
eprintln!("ok");
27+
}
28+
29+
fn main() {
30+
// We want the reproducer to work when linked with both linkers.
31+
test_with_linker("link");
32+
test_with_linker("rust-lld");
33+
}

0 commit comments

Comments
 (0)