-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
81 lines (70 loc) · 2.32 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#[cfg(feature = "asm")]
fn main() -> std::io::Result<()> {
use std::{env, fs::File, io::prelude::Write};
use tiny_elf::{
asm::{AsAsm, Program},
prelude::*,
program_header::Flags,
};
let word = env::args()
.nth(1)
.unwrap_or("Hello World, this is my tiny executable\n".into());
let word_len = word.len() as i32;
let upward_data = "We went upward\n";
let program = {
use tiny_elf::asm::{Memory, Mnemonic::*, Register::*};
Program::default()
.add(Mov(Rax, 8.into()))
.add(Mov(Rbx, 2.into()))
.add(IDiv(Rbx))
.add(Mov(Rdx, 4.into()))
.add(Cmp(Rax, Rdx.into()))
.add(Je("foo".into()))
.label("upward")
.insert_data("upward_data", upward_data)
.add(Mov(Rsi, Memory::from("upward_data").into()))
.add(Mov(Rdx, (upward_data.len() as i32).into()))
.add(Call("print".into()))
.add(Jmp("exit".into()))
.label("read")
.add(Mov(Rax, 0.into()))
.add(Mov(Rdi, 0.into()))
.add(Mov(Rsi, Memory::from("msg").into()))
.add(Mov(Rdx, word_len.into()))
.add(Syscall)
.label("foo")
.add(Mov(Rsi, Memory::from("msg").into()))
.add(Mov(Rdx, word_len.into()))
.add(Call("print".into()))
.add(Jmp("upward".into()))
// functions
.func("print")
.add(Mov(Rax, 1.into()))
.add(Mov(Rdi, 1.into()))
.add(Syscall)
.func_end()
.label("exit")
.add(Mov(Rax, 60.into()))
.add(Mov(Rdi, 0.into()))
.add(Syscall)
};
let program = program.insert_data("msg", &word);
let mut elf = Elf::new(program.clone());
elf.add_data(program.data(), Flags::all());
elf.backpatch();
{
let mut file = File::create("dump.asm").unwrap();
file.write_all(program.as_asm().as_bytes())?;
}
{
let mut file = File::create(env::args().nth(2).unwrap_or("bin".into()))?;
file.write_all(&elf.as_bytes())?;
}
Ok(())
}
#[cfg(not(feature = "asm"))]
fn main() -> std::process::ExitCode {
use std::process::ExitCode;
eprintln!("Enable 'asm' feature to run");
ExitCode::FAILURE
}