Skip to content
Open
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
13 changes: 10 additions & 3 deletions crates/xcl/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fn map_image_ex(path: &Path, is_main: bool) -> Result<Arc<MappedModule>, String>
let index_ptr = if t.address_of_index == 0 { std::ptr::null_mut() } else { adj(t.address_of_index) as *mut u32 };
unsafe {
// game DLLs loaded while its imports were resolved may point into it: its memory stays
tls_install(&module, template, template_len, t.total_len(), index_ptr).inspect_err(|_| modules::unregister_mapped(base))?;
tls_install(&module, template, template_len, t.total_len(), t.characteristics, index_ptr).inspect_err(|_| modules::unregister_mapped(base))?;
if t.address_of_callbacks != 0 {
let mut cb = adj(t.address_of_callbacks) as *const u64;
let mut list = module.tls_callbacks.lock().unwrap();
Expand Down Expand Up @@ -234,8 +234,15 @@ fn map_image_ex(path: &Path, is_main: bool) -> Result<Arc<MappedModule>, String>
Ok(module)
}

unsafe fn tls_install(m: &MappedModule, template: usize, len: usize, total: u64, index_ptr: *mut u32) -> Result<(), String> {
let k = crate::tls::install(&m.name, template, len, total, index_ptr, &cache_dir())?;
unsafe fn tls_install(
m: &MappedModule,
template: usize,
len: usize,
total: u64,
characteristics: u32,
index_ptr: *mut u32,
) -> Result<(), String> {
let k = crate::tls::install(&m.name, template, len, total, characteristics, index_ptr, &cache_dir())?;
// the title's executable: slot 0, as a process's executable has it
if !m.is_dll {
if let Err(e) = crate::tls::take_slot_zero(&m.name, index_ptr, k) {
Expand Down
78 changes: 78 additions & 0 deletions crates/xcl/src/pe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub const IMAGE_REL_BASED_DIR64: u16 = 10;
pub const SCN_MEM_EXECUTE: u32 = 0x2000_0000;
pub const SCN_MEM_READ: u32 = 0x4000_0000;
pub const SCN_MEM_WRITE: u32 = 0x8000_0000;
pub const SCN_ALIGN_MASK: u32 = 0x00F0_0000;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PeError {
Expand Down Expand Up @@ -139,6 +140,38 @@ impl TlsDir {
pub fn total_len(&self) -> u64 {
self.template_len() + self.size_of_zero_fill as u64
}
/// Alignment in bytes encoded in `Characteristics` bits 20..23
/// (`IMAGE_SCN_ALIGN_*BYTES`, nibble `1..=14` → `1..=8192` bytes), or `1`
/// when unspecified (`0`) or invalid (`15`).
pub fn alignment(&self) -> u32 {
tls_alignment_from_characteristics(self.characteristics)
}
/// `Characteristics` masked down to a valid `IMAGE_SCN_ALIGN_*BYTES` value
/// (nibble `1..=14`), clearing reserved bits and the invalid `0xF` mask value.
pub fn align_characteristics(&self) -> u32 {
sanitize_tls_characteristics(self.characteristics)
}
}

/// Decode `IMAGE_SCN_ALIGN_*BYTES` (bits 20..23) into a byte alignment (`1..=8192`).
pub fn tls_alignment_from_characteristics(characteristics: u32) -> u32 {
let nibble = (characteristics & SCN_ALIGN_MASK) >> 20;
if (1..=14).contains(&nibble) {
1u32 << (nibble - 1)
} else {
1
}
}

/// Mask `IMAGE_TLS_DIRECTORY64.Characteristics` to a valid `IMAGE_SCN_ALIGN_*BYTES`
/// value (nibble `1..=14`), clearing reserved bits and the invalid `0xF` mask value.
pub fn sanitize_tls_characteristics(characteristics: u32) -> u32 {
let nibble = (characteristics & SCN_ALIGN_MASK) >> 20;
if (1..=14).contains(&nibble) {
nibble << 20
} else {
0
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -799,5 +832,50 @@ mod tests {
let tls = pe.tls().unwrap().expect("rust binaries have a TLS directory");
assert!(tls.end_raw >= tls.start_raw);
assert!(pe.va_to_rva(tls.address_of_index).is_some());
assert!(tls.alignment().is_power_of_two());
}

#[test]
fn tls_characteristics_alignment_decoding() {
assert_eq!(tls_alignment_from_characteristics(0), 1);
assert_eq!(sanitize_tls_characteristics(0), 0);
for nibble in 1..=14u32 {
let expected_align = 1u32 << (nibble - 1);
let raw = (nibble << 20) | (0xDEAD_BEEF & !SCN_ALIGN_MASK); // include reserved noise bits outside mask
let dir = TlsDir { characteristics: raw, ..Default::default() };
assert_eq!(dir.alignment(), expected_align, "nibble {nibble}");
assert_eq!(dir.align_characteristics(), nibble << 20, "nibble {nibble}");
}
// Specific common PE alignment flags:
assert_eq!(tls_alignment_from_characteristics(0x0060_0000), 32);
assert_eq!(tls_alignment_from_characteristics(0x0070_0000), 64);
assert_eq!(tls_alignment_from_characteristics(0x00D0_0000), 4096);
assert_eq!(tls_alignment_from_characteristics(0x00E0_0000), 8192);
// Invalid nibble 0xF (IMAGE_SCN_ALIGN_MASK itself) maps to 1 / 0:
assert_eq!(tls_alignment_from_characteristics(SCN_ALIGN_MASK), 1);
assert_eq!(sanitize_tls_characteristics(SCN_ALIGN_MASK), 0);
}

#[test]
fn parses_tls_characteristics_from_pe() {
let mut bytes = crate::tls::build_carrier_dll_with(
0x100,
crate::tls::CarrierOpts { characteristics: 0x0070_0000, ..Default::default() },
);
let pe = Pe::parse(&bytes, Layout::File).unwrap();
let tls = pe.tls().unwrap().unwrap();
assert_eq!(tls.characteristics, 0x0070_0000);
assert_eq!(tls.alignment(), 64);
assert_eq!(tls.align_characteristics(), 0x0070_0000);

// Patch raw Characteristics field directly in the PE to verify Pe::tls reads offset +36 verbatim.
let tls_off = pe.rva_to_offset(pe.dir(DIR_TLS).rva).unwrap();
drop(pe);
bytes[tls_off + 36..tls_off + 40].copy_from_slice(&0x1260_00ABu32.to_le_bytes());
let pe = Pe::parse(&bytes, Layout::File).unwrap();
let tls = pe.tls().unwrap().unwrap();
assert_eq!(tls.characteristics, 0x1260_00AB);
assert_eq!(tls.alignment(), 32);
assert_eq!(tls.align_characteristics(), 0x0060_0000);
}
}
Loading
Loading