Skip to content

Commit

Permalink
Updated example + export of target_lexcion (#117)
Browse files Browse the repository at this point in the history
* Updated example + export of target_lexcion
  • Loading branch information
Cr0a3 authored Apr 8, 2024
1 parent 04ea121 commit 8ac6e28
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 73 deletions.
154 changes: 81 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,87 @@
Emit some object files, at your leisure:

```rust
let name = "test.o";
let file = File::create(Path::new(name))?;
let mut obj = ArtifactBuilder::new(triple!("x86_64-unknown-unknown-unknown-elf"))
.name(name.to_owned())
.finish();

// first we declare our symbolic references;
// it is a runtime error to define a symbol _without_ declaring it first
obj.declarations(
[
("deadbeef", Decl::function().into()),
("main", Decl::function().global().into()),
("str.1", Decl::cstring().into()),
("DEADBEEF", Decl::data_import().into()),
("printf", Decl::function_import().into()),
].iter().cloned()
)?;

// we now define our local functions and data
// 0000000000000000 <deadbeef>:
// 0: 55 push %rbp
// 1: 48 89 e5 mov %rsp,%rbp
// 4: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax # b <deadbeef+0xb>
// 7: R_X86_64_GOTPCREL DEADBEEF-0x4
// b: 8b 08 mov (%rax),%ecx
// d: 83 c1 01 add $0x1,%ecx
// 10: 89 c8 mov %ecx,%eax
// 12: 5d pop %rbp
// 13: c3 retq
obj.define("deadbeef",
vec![0x55,
0x48, 0x89, 0xe5,
0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00,
0x8b, 0x08,
0x83, 0xc1, 0x01,
0x89, 0xc8,
0x5d,
0xc3])?;
// main:
// 55 push %rbp
// 48 89 e5 mov %rsp,%rbp
// b8 00 00 00 00 mov $0x0,%eax
// e8 00 00 00 00 callq 0x0 <deadbeef>
// 89 c6 mov %eax,%esi
// 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # will be: deadbeef: 0x%x\n
// b8 00 00 00 00 mov $0x0,%eax
// e8 00 00 00 00 callq 0x3f <main+33> # printf
// b8 00 00 00 00 mov $0x0,%eax
// 5d pop %rbp
// c3 retq
obj.define("main",
vec![0x55,
0x48, 0x89, 0xe5,
0xb8, 0x00, 0x00, 0x00, 0x00,
0xe8, 0x00, 0x00, 0x00, 0x00,
0x89, 0xc6,
0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
0xb8, 0x00, 0x00, 0x00, 0x00,
0xe8, 0x00, 0x00, 0x00, 0x00,
0xb8, 0x00, 0x00, 0x00, 0x00,
0x5d,
0xc3])?;
obj.define("str.1", b"deadbeef: 0x%x\n\0".to_vec())?;

// Next, we declare our relocations,
// which are _always_ relative to the `from` symbol
obj.link(Link { from: "main", to: "str.1", at: 19 })?;
obj.link(Link { from: "main", to: "printf", at: 29 })?;
obj.link(Link { from: "main", to: "deadbeef", at: 10 })?;
obj.link(Link { from: "deadbeef", to: "DEADBEEF", at: 7 })?;

// Finally, we write the object file
obj.write(file)?;
use faerie::{ArtifactBuilder, ArtifactError, triple, Link, Decl};
use std::path::Path;
use std::fs::File;

pub fn main() -> Result<(), ArtifactError> {
let name = "test.o";
let file = File::create(Path::new(name))?;
let mut obj = ArtifactBuilder::new(triple!("x86_64-unknown-unknown-unknown-elf"))
.name(name.to_owned())
.finish();

// first we declare our symbolic references;
// it is a runtime error to define a symbol _without_ declaring it first
obj.declarations(
[
("deadbeef", Decl::function().into()),
("main", Decl::function().global().into()),
("str.1", Decl::cstring().into()),
("DEADBEEF", Decl::data_import().into()),
("printf", Decl::function_import().into()),
].iter().cloned()
)?;

// we now define our local functions and data
// 0000000000000000 <deadbeef>:
// 0: 55 push %rbp
// 1: 48 89 e5 mov %rsp,%rbp
// 4: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax # b <deadbeef+0xb>
// 7: R_X86_64_GOTPCREL DEADBEEF-0x4
// b: 8b 08 mov (%rax),%ecx
// d: 83 c1 01 add $0x1,%ecx
// 10: 89 c8 mov %ecx,%eax
// 12: 5d pop %rbp
// 13: c3 retq
obj.define("deadbeef",
vec![0x55,
0x48, 0x89, 0xe5,
0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00,
0x8b, 0x08,
0x83, 0xc1, 0x01,
0x89, 0xc8,
0x5d,
0xc3])?;
// main:
// 55 push %rbp
// 48 89 e5 mov %rsp,%rbp
// b8 00 00 00 00 mov $0x0,%eax
// e8 00 00 00 00 callq 0x0 <deadbeef>
// 89 c6 mov %eax,%esi
// 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # will be: deadbeef: 0x%x\n
// b8 00 00 00 00 mov $0x0,%eax
// e8 00 00 00 00 callq 0x3f <main+33> # printf
// b8 00 00 00 00 mov $0x0,%eax
// 5d pop %rbp
// c3 retq
obj.define("main",
vec![0x55,
0x48, 0x89, 0xe5,
0xb8, 0x00, 0x00, 0x00, 0x00,
0xe8, 0x00, 0x00, 0x00, 0x00,
0x89, 0xc6,
0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
0xb8, 0x00, 0x00, 0x00, 0x00,
0xe8, 0x00, 0x00, 0x00, 0x00,
0xb8, 0x00, 0x00, 0x00, 0x00,
0x5d,
0xc3])?;
obj.define("str.1", b"deadbeef: 0x%x\n\0".to_vec())?;

// Next, we declare our relocations,
// which are _always_ relative to the `from` symbol
obj.link(Link { from: "main", to: "str.1", at: 19 })?;
obj.link(Link { from: "main", to: "printf", at: 29 })?;
obj.link(Link { from: "main", to: "deadbeef", at: 10 })?;
obj.link(Link { from: "deadbeef", to: "DEADBEEF", at: 7 })?;

// Finally, we write the object file
obj.write(file)?;

Ok(())
}
```

Will emit an object file like this:
Expand Down
83 changes: 83 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#![cfg_attr(rustfmt, rustfmt_skip)]

use faerie::{ArtifactBuilder, ArtifactError, triple, Link, Decl};
use std::path::Path;
use std::fs::File;

pub fn main() -> Result<(), ArtifactError> {
let name = "test.o";
let file = File::create(Path::new(name))?;
let mut obj = ArtifactBuilder::new(triple!("x86_64-unknown-unknown-unknown-elf"))
.name(name.to_owned())
.finish();

// first we declare our symbolic references;
// it is a runtime error to define a symbol _without_ declaring it first
obj.declarations(
[
("deadbeef", Decl::function().into()),
("main", Decl::function().global().into()),
("str.1", Decl::cstring().into()),
("DEADBEEF", Decl::data_import().into()),
("printf", Decl::function_import().into()),
].iter().cloned()
)?;

// we now define our local functions and data
// 0000000000000000 <deadbeef>:
// 0: 55 push %rbp
// 1: 48 89 e5 mov %rsp,%rbp
// 4: 48 8b 05 00 00 00 00 mov 0x0(%rip),%rax # b <deadbeef+0xb>
// 7: R_X86_64_GOTPCREL DEADBEEF-0x4
// b: 8b 08 mov (%rax),%ecx
// d: 83 c1 01 add $0x1,%ecx
// 10: 89 c8 mov %ecx,%eax
// 12: 5d pop %rbp
// 13: c3 retq
obj.define("deadbeef",
vec![0x55,
0x48, 0x89, 0xe5,
0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00,
0x8b, 0x08,
0x83, 0xc1, 0x01,
0x89, 0xc8,
0x5d,
0xc3])?;
// main:
// 55 push %rbp
// 48 89 e5 mov %rsp,%rbp
// b8 00 00 00 00 mov $0x0,%eax
// e8 00 00 00 00 callq 0x0 <deadbeef>
// 89 c6 mov %eax,%esi
// 48 8d 3d 00 00 00 00 lea 0x0(%rip),%rdi # will be: deadbeef: 0x%x\n
// b8 00 00 00 00 mov $0x0,%eax
// e8 00 00 00 00 callq 0x3f <main+33> # printf
// b8 00 00 00 00 mov $0x0,%eax
// 5d pop %rbp
// c3 retq
obj.define("main",
vec![0x55,
0x48, 0x89, 0xe5,
0xb8, 0x00, 0x00, 0x00, 0x00,
0xe8, 0x00, 0x00, 0x00, 0x00,
0x89, 0xc6,
0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
0xb8, 0x00, 0x00, 0x00, 0x00,
0xe8, 0x00, 0x00, 0x00, 0x00,
0xb8, 0x00, 0x00, 0x00, 0x00,
0x5d,
0xc3])?;
obj.define("str.1", b"deadbeef: 0x%x\n\0".to_vec())?;

// Next, we declare our relocations,
// which are _always_ relative to the `from` symbol
obj.link(Link { from: "main", to: "str.1", at: 19 })?;
obj.link(Link { from: "main", to: "printf", at: 29 })?;
obj.link(Link { from: "main", to: "deadbeef", at: 10 })?;
obj.link(Link { from: "deadbeef", to: "DEADBEEF", at: 7 })?;

// Finally, we write the object file
obj.write(file)?;

Ok(())
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ pub use crate::artifact::{
},
Artifact, ArtifactBuilder, ArtifactError, Data, ImportKind, Link, Reloc,
};

pub use target_lexicon::{
triple, Architecture, BinaryFormat, Environment, OperatingSystem, Triple, Vendor,
};

0 comments on commit 8ac6e28

Please sign in to comment.