ObjectBuilder

Struct ObjectBuilder 

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

A struct for building object files

Implementations§

Source§

impl ObjectBuilder

Source

pub fn new(path: &str) -> Self

Examples found in repository?
examples/simple.rs (line 4)
3pub fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut obj = ObjectBuilder::new("test.o");
5
6    obj.decls(vec![
7        ("callme", Decl::Function(Scope::Import)),
8        ("call", Decl::Function(Scope::Export)),
9        ("data", Decl::Data(Scope::Export)),
10    ]);
11
12    obj.define(
13        "call",
14        vec![
15            0xF3, 0x0F, 0x1E, 0xFA,         // endbr64
16            0x55,                           // push rbp
17            0x48, 0x89, 0xE5,               // mov rbp, rsp
18            0xE8, 0x00, 0x00, 0x00, 0x00,   // call callme
19            0x5D,                           // pop rbp
20            0xC3,                           // ret
21        ],
22    );
23
24    obj.define("data", 
25        b"Hello World".into()
26    );
27
28    obj.link(Link {
29        from: "call".into(),
30        to: "callme".into(),
31        at: 9,
32    });
33
34    obj.write(BinFormat::host(), Arch::host(), Endian::host())?;
35
36    Ok(())
37}
Source

pub fn decls(&mut self, decls: Vec<(&str, Decl)>)

Adds a list of decls

Examples found in repository?
examples/simple.rs (lines 6-10)
3pub fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut obj = ObjectBuilder::new("test.o");
5
6    obj.decls(vec![
7        ("callme", Decl::Function(Scope::Import)),
8        ("call", Decl::Function(Scope::Export)),
9        ("data", Decl::Data(Scope::Export)),
10    ]);
11
12    obj.define(
13        "call",
14        vec![
15            0xF3, 0x0F, 0x1E, 0xFA,         // endbr64
16            0x55,                           // push rbp
17            0x48, 0x89, 0xE5,               // mov rbp, rsp
18            0xE8, 0x00, 0x00, 0x00, 0x00,   // call callme
19            0x5D,                           // pop rbp
20            0xC3,                           // ret
21        ],
22    );
23
24    obj.define("data", 
25        b"Hello World".into()
26    );
27
28    obj.link(Link {
29        from: "call".into(),
30        to: "callme".into(),
31        at: 9,
32    });
33
34    obj.write(BinFormat::host(), Arch::host(), Endian::host())?;
35
36    Ok(())
37}
Source

pub fn add_decl(&mut self, name: &str, decl: Decl)

Adds a decl

Source

pub fn define(&mut self, sym: &str, data: Vec<u8>)

Defines a symbol

Examples found in repository?
examples/simple.rs (lines 12-22)
3pub fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut obj = ObjectBuilder::new("test.o");
5
6    obj.decls(vec![
7        ("callme", Decl::Function(Scope::Import)),
8        ("call", Decl::Function(Scope::Export)),
9        ("data", Decl::Data(Scope::Export)),
10    ]);
11
12    obj.define(
13        "call",
14        vec![
15            0xF3, 0x0F, 0x1E, 0xFA,         // endbr64
16            0x55,                           // push rbp
17            0x48, 0x89, 0xE5,               // mov rbp, rsp
18            0xE8, 0x00, 0x00, 0x00, 0x00,   // call callme
19            0x5D,                           // pop rbp
20            0xC3,                           // ret
21        ],
22    );
23
24    obj.define("data", 
25        b"Hello World".into()
26    );
27
28    obj.link(Link {
29        from: "call".into(),
30        to: "callme".into(),
31        at: 9,
32    });
33
34    obj.write(BinFormat::host(), Arch::host(), Endian::host())?;
35
36    Ok(())
37}

Adds an link to the object file

Examples found in repository?
examples/simple.rs (lines 28-32)
3pub fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut obj = ObjectBuilder::new("test.o");
5
6    obj.decls(vec![
7        ("callme", Decl::Function(Scope::Import)),
8        ("call", Decl::Function(Scope::Export)),
9        ("data", Decl::Data(Scope::Export)),
10    ]);
11
12    obj.define(
13        "call",
14        vec![
15            0xF3, 0x0F, 0x1E, 0xFA,         // endbr64
16            0x55,                           // push rbp
17            0x48, 0x89, 0xE5,               // mov rbp, rsp
18            0xE8, 0x00, 0x00, 0x00, 0x00,   // call callme
19            0x5D,                           // pop rbp
20            0xC3,                           // ret
21        ],
22    );
23
24    obj.define("data", 
25        b"Hello World".into()
26    );
27
28    obj.link(Link {
29        from: "call".into(),
30        to: "callme".into(),
31        at: 9,
32    });
33
34    obj.write(BinFormat::host(), Arch::host(), Endian::host())?;
35
36    Ok(())
37}
Source

pub fn write( &mut self, format: BinFormat, arch: Arch, endian: Endian, ) -> Result<(), Box<dyn Error>>

Writes all internaly saved symbols etc. to a object file

Args:

  • format - specifes the binary format of the object file
  • arch - specifes the architecture of the object file
  • endian - specifes the endian of the object file
Examples found in repository?
examples/simple.rs (line 34)
3pub fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut obj = ObjectBuilder::new("test.o");
5
6    obj.decls(vec![
7        ("callme", Decl::Function(Scope::Import)),
8        ("call", Decl::Function(Scope::Export)),
9        ("data", Decl::Data(Scope::Export)),
10    ]);
11
12    obj.define(
13        "call",
14        vec![
15            0xF3, 0x0F, 0x1E, 0xFA,         // endbr64
16            0x55,                           // push rbp
17            0x48, 0x89, 0xE5,               // mov rbp, rsp
18            0xE8, 0x00, 0x00, 0x00, 0x00,   // call callme
19            0x5D,                           // pop rbp
20            0xC3,                           // ret
21        ],
22    );
23
24    obj.define("data", 
25        b"Hello World".into()
26    );
27
28    obj.link(Link {
29        from: "call".into(),
30        to: "callme".into(),
31        at: 9,
32    });
33
34    obj.write(BinFormat::host(), Arch::host(), Endian::host())?;
35
36    Ok(())
37}

Trait Implementations§

Source§

impl Clone for ObjectBuilder

Source§

fn clone(&self) -> ObjectBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ObjectBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.