use core::ffi::c_void;
use crate::sys::{LibFlags, SceSize};
pub use crate::sys::module::ModuleAttributes;
pub const STUB_LIBRARY_ENTRY_TABLE_OLD_LEN: u8 = 6;
pub const STUB_LIBRARY_ENTRY_TABLE_NEW_LEN: u8 = 7;
pub const RESI_LIBRARY_ENTRY_TABLE_OLD_LEN: u8 = 4;
pub const RESI_LIBRARY_ENTRY_TABLE_NEW_LEN: u8 = 5;
#[repr(C)]
#[doc(alias("SceStubLibraryEntryTable", "SceLibraryStubTable", "SceStubLibraryEntry"))]
pub struct StubLibraryEntry {
pub name: *const u8,
pub version: (u8, u8),
pub flags: LibFlags,
pub len: u8,
pub var_stub_count: u8,
pub func_stub_count: u16,
pub nid_table: *const u32,
pub func_stub_table: *const FunctionStub,
pub var_stub_table: *const VariableStub,
pub unk: u32,
}
#[repr(C)]
#[derive(Clone, Copy)]
#[doc(alias("SceStub"))]
pub union FunctionStub {
pub direct_call: FunctionDirectCall,
pub syscall: FunctionSyscall,
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FunctionDirectCall {
pub call: SceSize,
pub delay_slot: SceSize,
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FunctionSyscall {
pub return_addr: SceSize,
pub syscall: SceSize,
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[doc(alias("SceVariableStub"))]
pub struct VariableStub {
pub addr: SceSize,
pub nid: u32,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub union ResidentLibraryEntryItem {
nid: u32,
func: *const (),
var: *const c_void,
}
#[repr(C)]
#[doc(alias("SceResidentLibraryEntryTable", "SceLibraryEntryTable", "SceStubLibraryEntry"))]
pub struct ResidentLibraryEntry {
pub name: *const u8,
pub version: (u8, u8),
pub flags: LibFlags,
pub len: u8,
pub var_exp_count: u8,
pub func_exp_count: u16,
pub entry_table: *const ResidentLibraryEntryItem,
pub unk1: u16,
pub unk2: u8,
pub unk3: u8,
}
#[repr(C)]
#[derive(Debug, Clone)]
#[doc(alias("SceModuleInfo"))]
pub struct ModuleInfo {
pub attributes: ModuleAttributes,
pub version: (u8, u8),
pub name: [u8; 27],
pub terminal_char: u8,
pub gp: *const u8,
pub entry_top: *const u8,
pub entry_end: *const u8,
pub stub_top: *const u8,
pub stub_end: *const u8,
}
impl ModuleInfo {
#[doc(hidden)]
pub const fn name_from_str(s: &str) -> [u8; 27] {
let bytes = s.as_bytes();
let mut result = [0; 27];
let mut i = 0;
while i < bytes.len() {
result[i] = bytes[i];
i += 1;
}
result
}
}
unsafe impl Sync for ModuleInfo {}
unsafe impl Sync for StubLibraryEntry {}
unsafe impl Sync for ResidentLibraryEntryItem {}
unsafe impl Sync for ResidentLibraryEntry {}
impl ResidentLibraryEntryItem {
pub const fn new_nid(nid: u32) -> Self {
Self { nid }
}
pub const fn new_fn(func: *const ()) -> Self {
Self { func }
}
pub const fn new_var(var: *const c_void) -> Self {
Self { var }
}
}
impl ModuleAttributes {
#[inline]
#[doc(hidden)]
pub const fn base_default() -> Self {
if cfg!(feature = "kernel") {
Self::Kernel
} else {
Self::User
}
}
}