use std::ffi::{c_uint, CStr, CString};
use std::mem::MaybeUninit;
use crate::ffi::plthook_enum_with_prot;
#[derive(Debug)]
pub struct Symbol {
pub name: CString,
pub func_address: *const fn(),
pub protection: std::ffi::c_int,
}
pub(crate) fn iterator(object: &crate::ObjectFile) -> SymbolIterator<'_> {
SymbolIterator { pos: 0, object }
}
pub(crate) struct SymbolIterator<'a> {
pos: c_uint,
object: &'a crate::ObjectFile,
}
impl Iterator for SymbolIterator<'_> {
type Item = Symbol;
fn next(&mut self) -> Option<Symbol> {
let mut name = MaybeUninit::uninit();
let mut func_address = MaybeUninit::uninit();
let mut protection = 0;
let ret = unsafe {
plthook_enum_with_prot(
self.object.0.c_object,
&mut self.pos,
name.as_mut_ptr(),
func_address.as_mut_ptr() as *mut _,
&mut protection,
)
};
if ret != 0 {
return None;
}
let name = unsafe { CStr::from_ptr(name.assume_init()).into() };
let func_address = unsafe { func_address.assume_init() };
Some(Symbol {
name,
func_address,
protection,
})
}
}