pub struct ObjectBuilder { /* private fields */ }Expand description
A struct for building object files
Implementations§
Source§impl ObjectBuilder
impl ObjectBuilder
Sourcepub fn new(path: &str) -> Self
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}Sourcepub fn decls(&mut self, decls: Vec<(&str, Decl)>)
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}Sourcepub fn define(&mut self, sym: &str, data: Vec<u8>)
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}Sourcepub fn link(&mut self, link: Link)
pub fn link(&mut self, link: Link)
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}Sourcepub fn write(
&mut self,
format: BinFormat,
arch: Arch,
endian: Endian,
) -> Result<(), Box<dyn Error>>
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 filearch- specifes the architecture of the object fileendian- 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
impl Clone for ObjectBuilder
Source§fn clone(&self) -> ObjectBuilder
fn clone(&self) -> ObjectBuilder
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for ObjectBuilder
impl RefUnwindSafe for ObjectBuilder
impl Send for ObjectBuilder
impl Sync for ObjectBuilder
impl Unpin for ObjectBuilder
impl UnwindSafe for ObjectBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more