WinElfLoader

Struct WinElfLoader 

Source
pub struct WinElfLoader { /* private fields */ }
Expand description

elf loader

Implementations§

Source§

impl WinElfLoader

Source

pub fn new() -> Self

Examples found in repository?
examples/load.rs (line 13)
4fn main() {
5    extern "sysv64" fn print(s: *const i8) {
6        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
7        println!("{}", s);
8    }
9    // Symbols required by dynamic library liba.so
10    let mut map = HashMap::new();
11    map.insert("print", print as _);
12    let pre_find = |name: &str| -> Option<*const ()> { map.get(name).copied() };
13    let mut loader: WinElfLoader = WinElfLoader::new();
14    // Load and relocate dynamic library liba.so
15    let liba = loader
16        .load_file(r".\crates\windows-elf-loader\example_dylib\liba.so")
17        .unwrap()
18        .easy_relocate([], &pre_find)
19        .unwrap();
20    // Call function a in liba.so
21    let f = unsafe { liba.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
22    println!("{}", f());
23}
More examples
Hide additional examples
examples/from_memory.rs (line 14)
5fn main() {
6    extern "sysv64" fn print(s: *const i8) {
7        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
8        println!("{}", s);
9    }
10
11    let mut map = HashMap::new();
12    map.insert("print", print as _);
13    let pre_find = |name: &str| -> Option<*const ()> { map.get(name).copied() };
14    let mut loader = WinElfLoader::new();
15    let liba = loader
16        .load_dylib("liba", include_bytes!("../example_dylib/liba.so"))
17        .unwrap();
18    let libb = loader
19        .load_dylib("libb", include_bytes!("../example_dylib/libb.so"))
20        .unwrap();
21    let libc = loader
22        .load_dylib("libc", include_bytes!("../example_dylib/libc.so"))
23        .unwrap();
24    let a = liba.easy_relocate([], &pre_find).unwrap();
25    let f = unsafe { a.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
26    assert!(f() == 1);
27    let b = libb.easy_relocate([&a], &pre_find).unwrap();
28    let f = unsafe { b.get::<extern "sysv64" fn() -> i32>("b").unwrap() };
29    assert!(f() == 2);
30    let c = libc.easy_relocate([&b], &pre_find).unwrap();
31    let f = unsafe { c.get::<extern "sysv64" fn() -> i32>("c").unwrap() };
32    assert!(f() == 3);
33}
examples/relocate.rs (line 14)
5fn main() {
6    extern "sysv64" fn print(s: *const i8) {
7        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
8        println!("{}", s);
9    }
10
11    let mut map = HashMap::new();
12    map.insert("print", print as _);
13    let pre_find = |name: &str| -> Option<*const ()> { map.get(name).copied() };
14    let mut loader = WinElfLoader::new();
15    let liba = loader
16        .load_file(r".\crates\windows-elf-loader\example_dylib\liba.so")
17        .unwrap();
18    let libb = loader
19        .load_file(r".\crates\windows-elf-loader\example_dylib\libb.so")
20        .unwrap();
21    let libc = loader
22        .load_file(r".\crates\windows-elf-loader\example_dylib\libc.so")
23        .unwrap();
24    let a = liba.easy_relocate([], &pre_find).unwrap();
25    let f = unsafe { a.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
26    assert!(f() == 1);
27    let b = libb.easy_relocate([&a], &pre_find).unwrap();
28    let f = unsafe { b.get::<extern "sysv64" fn() -> i32>("b").unwrap() };
29    assert!(f() == 2);
30    let c = libc.easy_relocate([&b], &pre_find).unwrap();
31    let f = unsafe { c.get::<extern "sysv64" fn() -> i32>("c").unwrap() };
32    assert!(f() == 3);
33}
Source

pub fn load_dylib( &mut self, name: &str, bytes: impl AsRef<[u8]>, ) -> Result<ElfDylib, Error>

Examples found in repository?
examples/from_memory.rs (line 16)
5fn main() {
6    extern "sysv64" fn print(s: *const i8) {
7        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
8        println!("{}", s);
9    }
10
11    let mut map = HashMap::new();
12    map.insert("print", print as _);
13    let pre_find = |name: &str| -> Option<*const ()> { map.get(name).copied() };
14    let mut loader = WinElfLoader::new();
15    let liba = loader
16        .load_dylib("liba", include_bytes!("../example_dylib/liba.so"))
17        .unwrap();
18    let libb = loader
19        .load_dylib("libb", include_bytes!("../example_dylib/libb.so"))
20        .unwrap();
21    let libc = loader
22        .load_dylib("libc", include_bytes!("../example_dylib/libc.so"))
23        .unwrap();
24    let a = liba.easy_relocate([], &pre_find).unwrap();
25    let f = unsafe { a.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
26    assert!(f() == 1);
27    let b = libb.easy_relocate([&a], &pre_find).unwrap();
28    let f = unsafe { b.get::<extern "sysv64" fn() -> i32>("b").unwrap() };
29    assert!(f() == 2);
30    let c = libc.easy_relocate([&b], &pre_find).unwrap();
31    let f = unsafe { c.get::<extern "sysv64" fn() -> i32>("c").unwrap() };
32    assert!(f() == 3);
33}
Source

pub fn load_file(&mut self, name: &str) -> Result<ElfDylib, Error>

Examples found in repository?
examples/load.rs (line 16)
4fn main() {
5    extern "sysv64" fn print(s: *const i8) {
6        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
7        println!("{}", s);
8    }
9    // Symbols required by dynamic library liba.so
10    let mut map = HashMap::new();
11    map.insert("print", print as _);
12    let pre_find = |name: &str| -> Option<*const ()> { map.get(name).copied() };
13    let mut loader: WinElfLoader = WinElfLoader::new();
14    // Load and relocate dynamic library liba.so
15    let liba = loader
16        .load_file(r".\crates\windows-elf-loader\example_dylib\liba.so")
17        .unwrap()
18        .easy_relocate([], &pre_find)
19        .unwrap();
20    // Call function a in liba.so
21    let f = unsafe { liba.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
22    println!("{}", f());
23}
More examples
Hide additional examples
examples/relocate.rs (line 16)
5fn main() {
6    extern "sysv64" fn print(s: *const i8) {
7        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
8        println!("{}", s);
9    }
10
11    let mut map = HashMap::new();
12    map.insert("print", print as _);
13    let pre_find = |name: &str| -> Option<*const ()> { map.get(name).copied() };
14    let mut loader = WinElfLoader::new();
15    let liba = loader
16        .load_file(r".\crates\windows-elf-loader\example_dylib\liba.so")
17        .unwrap();
18    let libb = loader
19        .load_file(r".\crates\windows-elf-loader\example_dylib\libb.so")
20        .unwrap();
21    let libc = loader
22        .load_file(r".\crates\windows-elf-loader\example_dylib\libc.so")
23        .unwrap();
24    let a = liba.easy_relocate([], &pre_find).unwrap();
25    let f = unsafe { a.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
26    assert!(f() == 1);
27    let b = libb.easy_relocate([&a], &pre_find).unwrap();
28    let f = unsafe { b.get::<extern "sysv64" fn() -> i32>("b").unwrap() };
29    assert!(f() == 2);
30    let c = libc.easy_relocate([&b], &pre_find).unwrap();
31    let f = unsafe { c.get::<extern "sysv64" fn() -> i32>("c").unwrap() };
32    assert!(f() == 3);
33}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.