IMPORTS WORKING
This commit is contained in:
+50
-50
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
arch::x86_64::*,
|
||||
backend::{Instr as BInstr, Program, pe::LibImport},
|
||||
backend::{Instr as BInstr, Program},
|
||||
};
|
||||
use std::{fs::OpenOptions, io::Write, os::unix::fs::OpenOptionsExt, process::Command};
|
||||
|
||||
@@ -12,34 +12,40 @@ pub fn run() {
|
||||
fn linux() {
|
||||
let mut program = Program::<X86_64>::default();
|
||||
let text = b"Hello world!\n";
|
||||
let text_sym = program.ro_data(text);
|
||||
let text_sym = program.ro_data("hello_en", text);
|
||||
let text2 = "世界、こんにちは!\n";
|
||||
let text_sym2 = program.ro_data(text2);
|
||||
let hello2 = program.func([BInstr::Asm(Asm {
|
||||
instrs: vec![
|
||||
mov(ax, 1),
|
||||
mov(di, 1),
|
||||
lea(rsi, text_sym2),
|
||||
mov(dx, text2.len() as u64),
|
||||
Instr::Syscall,
|
||||
Instr::Ret,
|
||||
],
|
||||
})]);
|
||||
let entry = program.func([BInstr::Asm(Asm {
|
||||
instrs: vec![
|
||||
mov(di, 39),
|
||||
push(rdi),
|
||||
mov(ax, 1),
|
||||
mov(di, 1),
|
||||
lea(rsi, text_sym),
|
||||
mov(dx, text.len() as u64),
|
||||
Instr::Syscall,
|
||||
Instr::Call(hello2),
|
||||
mov(ax, 0x3c),
|
||||
pop(rdi),
|
||||
Instr::Syscall,
|
||||
],
|
||||
})]);
|
||||
let text_sym2 = program.ro_data("hello_jp", text2);
|
||||
let hello2 = program.func(
|
||||
"hello2",
|
||||
[BInstr::Asm(Asm {
|
||||
instrs: vec![
|
||||
mov(ax, 1),
|
||||
mov(di, 1),
|
||||
lea(rsi, text_sym2),
|
||||
mov(dx, text2.len() as u64),
|
||||
Instr::Syscall,
|
||||
Instr::Ret,
|
||||
],
|
||||
})],
|
||||
);
|
||||
let entry = program.func(
|
||||
"main",
|
||||
[BInstr::Asm(Asm {
|
||||
instrs: vec![
|
||||
mov(di, 39),
|
||||
push(rdi),
|
||||
mov(ax, 1),
|
||||
mov(di, 1),
|
||||
lea(rsi, text_sym),
|
||||
mov(dx, text.len() as u64),
|
||||
Instr::Syscall,
|
||||
Instr::Call(hello2),
|
||||
mov(ax, 0x3c),
|
||||
pop(rdi),
|
||||
Instr::Syscall,
|
||||
],
|
||||
})],
|
||||
);
|
||||
program.entry = Some(entry);
|
||||
let linked = program.compile().expect("failed to compile");
|
||||
let binary = linked.to_elf();
|
||||
@@ -71,34 +77,28 @@ fn linux() {
|
||||
|
||||
fn windows() {
|
||||
let mut program = Program::<X86_64>::default();
|
||||
let entry = program.func([BInstr::Asm(Asm {
|
||||
instrs: vec![
|
||||
push(39),
|
||||
pop(rax),
|
||||
Instr::Ret
|
||||
],
|
||||
})]);
|
||||
let [get_std_handle, write_file, exit_process] =
|
||||
program.external("KERNEL32.dll", ["GetStdHandle", "WriteFile", "ExitProcess"]);
|
||||
let entry = program.func(
|
||||
"main",
|
||||
[BInstr::Asm(Asm {
|
||||
instrs: vec![Instr::Sub, mov(ecx, 40), Instr::CallMem(exit_process)],
|
||||
})],
|
||||
);
|
||||
program.entry = Some(entry);
|
||||
let linked = program.compile().expect("failed to compile");
|
||||
|
||||
let imports = &[LibImport {
|
||||
name: "KERNEL32.dll".to_string(),
|
||||
syms: ["GetStdHandle", "WriteFile", "ExitProcess"]
|
||||
.map(String::from)
|
||||
.to_vec(),
|
||||
}];
|
||||
|
||||
let binary = linked.to_pe(imports);
|
||||
let binary = linked.to_pe();
|
||||
let path = "./x86_64_test.exe";
|
||||
write(path, &binary);
|
||||
|
||||
// let mut cmd = Command::new("wine");
|
||||
// cmd.arg("x86_64_test");
|
||||
// let mut proc = cmd.spawn().expect("failed to run");
|
||||
// let status = proc.wait().expect("failed to wait");
|
||||
// if let Some(code) = status.code() {
|
||||
// std::process::exit(code);
|
||||
// }
|
||||
let mut cmd = Command::new("wine");
|
||||
cmd.arg("x86_64_test");
|
||||
let mut proc = cmd.spawn().expect("failed to run");
|
||||
let status = proc.wait().expect("failed to wait");
|
||||
if let Some(code) = status.code() {
|
||||
std::process::exit(code);
|
||||
}
|
||||
}
|
||||
|
||||
fn write(path: &str, binary: &[u8]) {
|
||||
|
||||
@@ -2,7 +2,8 @@ use super::*;
|
||||
|
||||
fn eq(expected: impl AsRef<[u8]>, asm: Instr) {
|
||||
let expected = expected.as_ref();
|
||||
let mut encoder = Encoder::new(0);
|
||||
let program = Program::default();
|
||||
let mut encoder = Encoder::new(&program);
|
||||
if let Err(e) = encoder.asm(asm) {
|
||||
panic!("expected {expected:x?}, failed to compile: {}", e.msg);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user