Skip to main content

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 15)
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    // Symbols required by dynamic library liba.so
11    let host = SyntheticModule::new(
12        "__host",
13        [SyntheticSymbol::function("print", print as *const ())],
14    );
15    let mut loader: WinElfLoader = WinElfLoader::new();
16    // Load and relocate dynamic library liba.so
17    let liba = loader
18        .load_file(r".\crates\windows-elf-loader\example_dylib\liba.so")
19        .unwrap()
20        .relocator()
21        .scope([host])
22        .relocate()
23        .unwrap();
24    // Call function a in liba.so
25    let f = unsafe { liba.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
26    println!("{}", f());
27}
More examples
Hide additional examples
examples/from_memory.rs (line 16)
6fn main() {
7    extern "sysv64" fn print(s: *const i8) {
8        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
9        println!("{}", s);
10    }
11
12    let host = ModuleHandle::from(SyntheticModule::new(
13        "__host",
14        [SyntheticSymbol::function("print", print as *const ())],
15    ));
16    let mut loader = WinElfLoader::new();
17    let liba = loader
18        .load_dylib("liba", include_bytes!("../example_dylib/liba.so"))
19        .unwrap()
20        .relocator()
21        .scope([host.clone()])
22        .relocate()
23        .unwrap();
24    let libb = loader
25        .load_dylib("libb", include_bytes!("../example_dylib/libb.so"))
26        .unwrap()
27        .relocator()
28        .scope([host.clone(), ModuleHandle::from(&liba)])
29        .relocate()
30        .unwrap();
31    let libc = loader
32        .load_dylib("libc", include_bytes!("../example_dylib/libc.so"))
33        .unwrap()
34        .relocator()
35        .scope([host.clone(), ModuleHandle::from(&libb)])
36        .relocate()
37        .unwrap();
38    let f = unsafe { liba.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
39    assert!(f() == 1);
40    let f = unsafe { libb.get::<extern "sysv64" fn() -> i32>("b").unwrap() };
41    assert!(f() == 2);
42    let f = unsafe { libc.get::<extern "sysv64" fn() -> i32>("c").unwrap() };
43    assert!(f() == 3);
44}
examples/relocate.rs (line 16)
6fn main() {
7    extern "sysv64" fn print(s: *const i8) {
8        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
9        println!("{}", s);
10    }
11
12    let host = ModuleHandle::from(SyntheticModule::new(
13        "__host",
14        [SyntheticSymbol::function("print", print as *const ())],
15    ));
16    let mut loader = WinElfLoader::new();
17    let liba = loader
18        .load_file(r".\crates\windows-elf-loader\example_dylib\liba.so")
19        .unwrap()
20        .relocator()
21        .scope([host.clone()])
22        .relocate()
23        .unwrap();
24    let libb = loader
25        .load_file(r".\crates\windows-elf-loader\example_dylib\libb.so")
26        .unwrap()
27        .relocator()
28        .scope([host.clone(), ModuleHandle::from(&liba)])
29        .relocate()
30        .unwrap();
31    let libc = loader
32        .load_file(r".\crates\windows-elf-loader\example_dylib\libc.so")
33        .unwrap()
34        .relocator()
35        .scope([host.clone(), ModuleHandle::from(&libb)])
36        .relocate()
37        .unwrap();
38    let f = unsafe { liba.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
39    assert!(f() == 1);
40    let f = unsafe { libb.get::<extern "sysv64" fn() -> i32>("b").unwrap() };
41    assert!(f() == 2);
42    let f = unsafe { libc.get::<extern "sysv64" fn() -> i32>("c").unwrap() };
43    assert!(f() == 3);
44}
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 18)
6fn main() {
7    extern "sysv64" fn print(s: *const i8) {
8        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
9        println!("{}", s);
10    }
11
12    let host = ModuleHandle::from(SyntheticModule::new(
13        "__host",
14        [SyntheticSymbol::function("print", print as *const ())],
15    ));
16    let mut loader = WinElfLoader::new();
17    let liba = loader
18        .load_dylib("liba", include_bytes!("../example_dylib/liba.so"))
19        .unwrap()
20        .relocator()
21        .scope([host.clone()])
22        .relocate()
23        .unwrap();
24    let libb = loader
25        .load_dylib("libb", include_bytes!("../example_dylib/libb.so"))
26        .unwrap()
27        .relocator()
28        .scope([host.clone(), ModuleHandle::from(&liba)])
29        .relocate()
30        .unwrap();
31    let libc = loader
32        .load_dylib("libc", include_bytes!("../example_dylib/libc.so"))
33        .unwrap()
34        .relocator()
35        .scope([host.clone(), ModuleHandle::from(&libb)])
36        .relocate()
37        .unwrap();
38    let f = unsafe { liba.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
39    assert!(f() == 1);
40    let f = unsafe { libb.get::<extern "sysv64" fn() -> i32>("b").unwrap() };
41    assert!(f() == 2);
42    let f = unsafe { libc.get::<extern "sysv64" fn() -> i32>("c").unwrap() };
43    assert!(f() == 3);
44}
Source

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

Examples found in repository?
examples/load.rs (line 18)
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    // Symbols required by dynamic library liba.so
11    let host = SyntheticModule::new(
12        "__host",
13        [SyntheticSymbol::function("print", print as *const ())],
14    );
15    let mut loader: WinElfLoader = WinElfLoader::new();
16    // Load and relocate dynamic library liba.so
17    let liba = loader
18        .load_file(r".\crates\windows-elf-loader\example_dylib\liba.so")
19        .unwrap()
20        .relocator()
21        .scope([host])
22        .relocate()
23        .unwrap();
24    // Call function a in liba.so
25    let f = unsafe { liba.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
26    println!("{}", f());
27}
More examples
Hide additional examples
examples/relocate.rs (line 18)
6fn main() {
7    extern "sysv64" fn print(s: *const i8) {
8        let s = unsafe { CStr::from_ptr(s).to_str().unwrap() };
9        println!("{}", s);
10    }
11
12    let host = ModuleHandle::from(SyntheticModule::new(
13        "__host",
14        [SyntheticSymbol::function("print", print as *const ())],
15    ));
16    let mut loader = WinElfLoader::new();
17    let liba = loader
18        .load_file(r".\crates\windows-elf-loader\example_dylib\liba.so")
19        .unwrap()
20        .relocator()
21        .scope([host.clone()])
22        .relocate()
23        .unwrap();
24    let libb = loader
25        .load_file(r".\crates\windows-elf-loader\example_dylib\libb.so")
26        .unwrap()
27        .relocator()
28        .scope([host.clone(), ModuleHandle::from(&liba)])
29        .relocate()
30        .unwrap();
31    let libc = loader
32        .load_file(r".\crates\windows-elf-loader\example_dylib\libc.so")
33        .unwrap()
34        .relocator()
35        .scope([host.clone(), ModuleHandle::from(&libb)])
36        .relocate()
37        .unwrap();
38    let f = unsafe { liba.get::<extern "sysv64" fn() -> i32>("a").unwrap() };
39    assert!(f() == 1);
40    let f = unsafe { libb.get::<extern "sysv64" fn() -> i32>("b").unwrap() };
41    assert!(f() == 2);
42    let f = unsafe { libc.get::<extern "sysv64" fn() -> i32>("c").unwrap() };
43    assert!(f() == 3);
44}

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.