From 84bbf90ce1d91bcc4af4eee3c09db8c8ad8f8b05 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 3 Jul 2025 10:52:43 -0600 Subject: [PATCH] Fix ___tls_get_addr on x86 --- src/header/dl-tls/mod.rs | 27 +++++++++++++++++++-------- src/ld_so/linker.rs | 4 ++-- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/header/dl-tls/mod.rs b/src/header/dl-tls/mod.rs index 5cc9611360..31ca6662d6 100644 --- a/src/header/dl-tls/mod.rs +++ b/src/header/dl-tls/mod.rs @@ -2,7 +2,7 @@ #![deny(unsafe_op_in_unsafe_fn)] -use core::alloc::Layout; +use core::{alloc::Layout, arch::global_asm}; use alloc::alloc::alloc_zeroed; @@ -10,8 +10,8 @@ use crate::{ld_so::tcb::Tcb, platform::types::*}; #[repr(C)] pub struct dl_tls_index { - pub ti_module: u64, - pub ti_offset: u64, + pub ti_module: usize, + pub ti_offset: usize, } #[no_mangle] @@ -71,9 +71,20 @@ pub unsafe extern "C" fn __tls_get_addr(ti: *mut dl_tls_index) -> *mut c_void { ptr.cast::() } -// x86 can define a version that does not require stack alignment +// x86 can define a version that passes a pointer to dl_tls_index in eax #[cfg(target_arch = "x86")] -#[no_mangle] -pub unsafe extern "C" fn ___tls_get_addr(ti: *mut dl_tls_index) -> *mut c_void { - unsafe { __tls_get_addr(ti) } -} +global_asm!( + " + .globl ___tls_get_addr + .type ___tls_get_addr, @function +___tls_get_addr: + push ebp + mov ebp, esp + push eax + call __tls_get_addr + add esp, 4 + pop ebp + ret + .size ___tls_get_addr, . - ___tls_get_addr +" +); diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index 2ef100a873..b60a188c52 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -520,8 +520,8 @@ impl Linker { symbol.as_ptr() } else { let mut tls_index = dl_tls_index { - ti_module: obj.tls_module_id as u64, - ti_offset: symbol.value as u64, + ti_module: obj.tls_module_id, + ti_offset: symbol.value, }; unsafe { __tls_get_addr(&mut tls_index) }