From 4a1f72721adb4e9040b3390220f44d3f386d50f5 Mon Sep 17 00:00:00 2001 From: pablogonz12 Date: Tue, 24 Nov 2026 01:34:04 +0100 Subject: [PATCH] TLS: xcl.exe built with MinGW and lld keeps its TLS directory XCL's TLS callback (.CRT$XLY in main.rs) and the title's slot 0 need xcl.exe's TLS directory. MSVC's linker always emits one; with MinGW it is _tls_used in the CRT's tlssup.o, which lld (llvm-mingw) leaves out when nothing refers to it. The x86_64-pc-windows-gnu build then had no TLS directory: the title kept slot 4 while its /GA code reads slot 0 and faulted on its second thread. own_tls_index() now refers to _tls_used on target_env = "gnu", which keeps the directory; a unit test checks the executable has one. --- crates/xcl/src/tls.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/crates/xcl/src/tls.rs b/crates/xcl/src/tls.rs index c09e1e295..66db0a13c 100644 --- a/crates/xcl/src/tls.rs +++ b/crates/xcl/src/tls.rs @@ -252,9 +252,19 @@ static XCL_SLOT: AtomicU32 = AtomicU32::new(NO_INDEX); /// callback, if it still comes, leaves them as they are); held during the exchange static MOVED: std::sync::Mutex> = std::sync::Mutex::new(Vec::new()); +// With MinGW the TLS directory is `_tls_used` in the CRT's tlssup.o. MSVC's linker always keeps +// it; LLVM's lld (llvm-mingw) leaves it out unless something refers to it, and then xcl.exe has +// no TLS directory: no TLS callback (main.rs) and no slot to give the title. +#[cfg(target_env = "gnu")] +extern "C" { + static _tls_used: u8; +} + /// xcl.exe's `_tls_index` (its TLS directory's AddressOfIndex) unsafe fn own_tls_index() -> Option<*mut u32> { use windows_sys::Win32::System::LibraryLoader::GetModuleHandleW; + #[cfg(target_env = "gnu")] + std::hint::black_box(std::ptr::addr_of!(_tls_used)); let base = GetModuleHandleW(std::ptr::null()) as usize; if base == 0 { return None; @@ -537,6 +547,12 @@ mod tests { assert!(pe.size_of_image >= DATA_RVA + OFF_TEMPLATE + 0x3000); } + #[test] + fn the_executable_has_a_tls_directory() { + // the title's slot and XCL's TLS callback need it (MinGW + lld used to leave it out) + assert!(unsafe { own_tls_index() }.is_some()); + } + #[test] fn template_size_rounding() { assert_eq!(carrier_template_size_for(0), 16);