use lief_ffi as ffi;
use crate::common::{FromFFI, into_optional};
use crate::macho::Binary;
use std::marker::PhantomData;
pub struct Dylib<'a> {
ptr: cxx::UniquePtr<ffi::dsc_Dylib>,
_owner: PhantomData<&'a ()>,
}
impl FromFFI<ffi::dsc_Dylib> for Dylib<'_> {
fn from_ffi(ptr: cxx::UniquePtr<ffi::dsc_Dylib>) -> Self {
Self {
ptr,
_owner: PhantomData,
}
}
}
impl Dylib<'_> {
pub fn path(&self) -> String {
self.ptr.path().to_string()
}
pub fn address(&self) -> u64 {
self.ptr.address()
}
pub fn modtime(&self) -> u64 {
self.ptr.modtime()
}
pub fn inode(&self) -> u64 {
self.ptr.inode()
}
pub fn padding(&self) -> u64 {
self.ptr.padding()
}
pub fn get(&self) -> Option<Binary> {
self.get_with_opt(&ExtractOpt::default())
}
pub fn get_with_opt(&self, opt: &ExtractOpt) -> Option<Binary> {
into_optional(self.ptr.get_macho(&opt.to_ffi()))
}
}
pub struct ExtractOpt {
pub pack: bool,
pub fix_branches: bool,
pub fix_memory: bool,
pub fix_relocations: bool,
pub fix_objc: bool,
pub create_dyld_chained_fixup_cmd: Option<bool>,
}
impl Default for ExtractOpt {
fn default() -> ExtractOpt {
ExtractOpt {
pack: true,
fix_branches: false,
fix_memory: true,
fix_relocations: true,
fix_objc: true,
create_dyld_chained_fixup_cmd: None,
}
}
}
impl ExtractOpt {
#[doc(hidden)]
fn to_ffi(&self) -> ffi::dsc_Dylib_extract_opt {
ffi::dsc_Dylib_extract_opt {
pack: self.pack,
fix_branches: self.fix_branches,
fix_memory: self.fix_memory,
fix_relocations: self.fix_relocations,
fix_objc: self.fix_objc,
create_dyld_chained_fixup_cmd: self.create_dyld_chained_fixup_cmd.unwrap_or(false),
create_dyld_chained_fixup_cmd_set: self.create_dyld_chained_fixup_cmd.is_some(),
}
}
}
impl std::fmt::Debug for Dylib<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Dylib")
.field("path", &self.path())
.field("address", &self.address())
.field("modtime", &self.modtime())
.field("inode", &self.inode())
.field("padding", &self.padding())
.finish()
}
}