Skip to content

Fix Access Violation when using lld & ThinLTO on windows-msvc #103353

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

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/rustc_codegen_llvm/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rustc_middle::mir::mono::MonoItem;
use rustc_middle::ty::layout::LayoutOf;
use rustc_middle::ty::{self, Instance, Ty};
use rustc_middle::{bug, span_bug};
use rustc_session::config::Lto;
use rustc_target::abi::{
AddressSpace, Align, HasDataLayout, Primitive, Scalar, Size, WrappingRange,
};
Expand Down Expand Up @@ -303,7 +304,8 @@ impl<'ll> CodegenCx<'ll, '_> {
// ThinLTO can't handle this workaround in all cases, so we don't
// emit the attrs. Instead we make them unnecessary by disallowing
// dynamic linking when linker plugin based LTO is enabled.
!self.tcx.sess.opts.cg.linker_plugin_lto.enabled();
!self.tcx.sess.opts.cg.linker_plugin_lto.enabled() &&
self.tcx.sess.lto() != Lto::Thin;

// If this assertion triggers, there's something wrong with commandline
// argument validation.
Expand Down
13 changes: 13 additions & 0 deletions src/test/codegen/auxiliary/static_dllimport_aux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use std::sync::atomic::{AtomicPtr, Ordering};

#[inline(always)]
pub fn memrchr() {
fn detect() {}

static CROSS_CRATE_STATIC_ITEM: AtomicPtr<()> = AtomicPtr::new(detect as *mut ());

unsafe {
let fun = CROSS_CRATE_STATIC_ITEM.load(Ordering::SeqCst);
std::mem::transmute::<*mut (), fn()>(fun)()
}
}
15 changes: 15 additions & 0 deletions src/test/codegen/issue-81408-dllimport-thinlto-windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// compile-flags: -O -C lto=thin -C prefer-dynamic=no
// only-windows
// aux-build:static_dllimport_aux.rs

// Test that on Windows, when performing ThinLTO, we do not mark cross-crate static items with
// dllimport because lld does not fix the symbol names for us.

extern crate static_dllimport_aux;

// CHECK-LABEL: @{{.+}}CROSS_CRATE_STATIC_ITEM{{.+}} =
// CHECK-SAME: external local_unnamed_addr global %"{{.+}}AtomicPtr

pub fn main() {
static_dllimport_aux::memrchr();
}