feat(core): implement Bionic TLS block allocation and TCB self-pointer setup
This commit is contained in:
parent
df210a88a1
commit
4702aaa37c
3 changed files with 51 additions and 0 deletions
|
|
@ -4,4 +4,5 @@ pub mod elf;
|
|||
pub mod mmap;
|
||||
pub mod reloc;
|
||||
pub mod symbols;
|
||||
pub mod tls;
|
||||
pub mod trampoline;
|
||||
|
|
|
|||
41
crates/molotov-core/src/tls.rs
Normal file
41
crates/molotov-core/src/tls.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
use std::alloc::{alloc_zeroed, dealloc, Layout};
|
||||
|
||||
pub const BIONIC_TLS_SLOTS: usize = 128;
|
||||
|
||||
pub struct BionicTls {
|
||||
ptr: *mut u64,
|
||||
layout: Layout,
|
||||
}
|
||||
|
||||
impl BionicTls {
|
||||
pub fn allocate() -> Result<Self, String> {
|
||||
let layout = Layout::from_size_align(BIONIC_TLS_SLOTS * std::mem::size_of::<u64>(), 64)
|
||||
.map_err(|e| e.to_string())?;
|
||||
|
||||
let ptr = unsafe { alloc_zeroed(layout) as *mut u64 };
|
||||
if ptr.is_null() {
|
||||
Err("Failed to allocate memory for Bionic TLS".to_string())
|
||||
} else {
|
||||
// Android x86_64: slot 0 points to the thread control block itself
|
||||
unsafe {
|
||||
*ptr = ptr as u64;
|
||||
}
|
||||
Ok(Self { ptr, layout })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn tcb_ptr(&self) -> *mut u64 {
|
||||
self.ptr
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for BionicTls {
|
||||
fn drop(&mut self) {
|
||||
if !self.ptr.is_null() {
|
||||
unsafe {
|
||||
dealloc(self.ptr as *mut u8, self.layout);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
9
crates/molotov-core/tests/tls_test.rs
Normal file
9
crates/molotov-core/tests/tls_test.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
use molotov_core::tls::BionicTls;
|
||||
|
||||
#[test]
|
||||
fn test_bionic_tls_allocation() {
|
||||
let tls = BionicTls::allocate().expect("Bionic TLS block should allocate cleanly");
|
||||
assert!(!tls.tcb_ptr().is_null());
|
||||
assert_eq!(tls.tcb_ptr() as usize % 64, 0, "TLS pointer must be 64-byte aligned");
|
||||
}
|
||||
Reference in a new issue