Skip to content
Open
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
16 changes: 16 additions & 0 deletions crates/xcl/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<usize>> = 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;
Expand Down Expand Up @@ -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);
Expand Down
Loading