IMPORTS WORKING

This commit is contained in:
2026-06-09 00:08:26 -04:00
parent 6bc502d284
commit e4acaf40aa
11 changed files with 205 additions and 115 deletions
+10 -8
View File
@@ -9,18 +9,20 @@ pub struct Asm {
#[derive(Clone, Copy)]
pub enum Instr {
Mov { dst: RegMode, src: RegModeImm },
Mov { dst: RegMode, src: RegImmMem },
Int(u8),
Call(Symbol),
CallMem(Symbol),
Ret,
Syscall,
Lea { dst: RegMode, sym: Symbol },
Push(RegImm),
Pop(Reg),
Sub,
}
#[derive(Clone, Copy)]
pub enum RegModeImm {
pub enum RegImmMem {
Reg(RegMode),
Imm(u64),
}
@@ -31,13 +33,13 @@ pub enum RegImm {
Imm(u64),
}
impl From<RegMode> for RegModeImm {
impl From<RegMode> for RegImmMem {
fn from(value: RegMode) -> Self {
Self::Reg(value)
}
}
impl From<u64> for RegModeImm {
impl From<u64> for RegImmMem {
fn from(value: u64) -> Self {
Self::Imm(value)
}
@@ -45,7 +47,7 @@ impl From<u64> for RegModeImm {
mod fns {
use super::*;
pub fn mov(dst: RegMode, src: impl Into<RegModeImm>) -> Instr {
pub fn mov(dst: RegMode, src: impl Into<RegImmMem>) -> Instr {
Instr::Mov {
dst,
src: src.into(),
@@ -56,14 +58,14 @@ mod fns {
Instr::Lea { dst, sym }
}
pub fn push(reg: impl Into<RegModeImm>) -> Instr {
pub fn push(reg: impl Into<RegImmMem>) -> Instr {
Instr::Push(match reg.into() {
RegModeImm::Reg(reg) => {
RegImmMem::Reg(reg) => {
assert_eq!(reg.width, BitWidth::B64);
assert!(!reg.high);
RegImm::Reg(reg.reg)
}
RegModeImm::Imm(imm) => RegImm::Imm(imm),
RegImmMem::Imm(imm) => RegImm::Imm(imm),
})
}
+47 -13
View File
@@ -1,14 +1,18 @@
use super::*;
use crate::backend::{LinkedProgram, SymTable, Symbol};
use std::collections::HashMap;
pub struct Encoder {
use super::*;
use crate::backend::{LibImport, LinkedProgram, SymImport, SymTable, Symbol};
pub struct Encoder<'a> {
pub data: Vec<u8>,
pub sym_tab: SymTable<u64>,
pub missing: Vec<(usize, Symbol)>,
pub sym_refs: HashMap<Symbol, Vec<usize>>,
pub program: &'a Program<X86_64>,
}
pub fn compile(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, CompilerMsg> {
let mut encoder = Encoder::new(p.sym_count());
let mut encoder = Encoder::new(p);
p.encode_data(&mut encoder.data, &mut encoder.sym_tab);
@@ -28,9 +32,26 @@ pub fn compile(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, CompilerMsg> {
encoder.data[pos..pos + 4].copy_from_slice(&addr_offset(pos, addr))
}
let imports = p
.external
.iter()
.map(|e| LibImport {
name: e.file.clone(),
syms: e
.syms
.iter()
.map(|&s| SymImport {
name: p.sym_info(s).name.clone(),
usages: encoder.sym_refs.entry(s).or_default().clone(),
})
.collect(),
})
.collect();
Ok(LinkedProgram {
code: encoder.data,
entry: p.entry.and_then(|e| encoder.sym_tab.get(e)),
imports,
})
}
@@ -47,10 +68,10 @@ fn compile_instr(encoder: &mut Encoder, instr: &BInstr) -> Result<(), CompilerMs
Ok(())
}
impl Encoder {
impl Encoder<'_> {
// assembly
pub fn mov(&mut self, dst: RegMode, src: impl Into<RegModeImm>) -> Result<(), CompilerMsg> {
pub fn mov(&mut self, dst: RegMode, src: impl Into<RegImmMem>) -> Result<(), CompilerMsg> {
let src = src.into();
let width = dst.width;
if width == BitWidth::B16 {
@@ -59,7 +80,7 @@ impl Encoder {
let dst8 = dst.gt8();
let b64 = width == BitWidth::B64;
let b8 = width == BitWidth::B8;
let src8 = if let RegModeImm::Reg(src) = src {
let src8 = if let RegImmMem::Reg(src) = src {
src.gt8()
} else {
false
@@ -70,7 +91,7 @@ impl Encoder {
.push(0x40 | dst8 as u8 | ((b64 as u8) << 3) | ((src8 as u8) << 2));
}
match src {
RegModeImm::Reg(src) => {
RegImmMem::Reg(src) => {
if dst.width != src.width {
return Err("src and dst are not the same size".into());
}
@@ -78,7 +99,7 @@ impl Encoder {
let modrm = 0b11_000_000 | (src.base() << 3) | dst.base();
self.data.push(modrm);
}
RegModeImm::Imm(imm) => {
RegImmMem::Imm(imm) => {
if imm > width.max() {
return Err("immediate cannot fit in register".into());
}
@@ -111,6 +132,11 @@ impl Encoder {
self.sym_offset4(sym);
}
pub fn call_mem(&mut self, sym: Symbol) {
self.data.extend([0xff, 0x15]);
self.sym_offset4(sym);
}
pub fn ret(&mut self) {
self.data.push(0xc3);
}
@@ -145,7 +171,11 @@ impl Encoder {
let Some(addr) = self.sym_tab.get(sym) else {
let pos = self.data.len();
self.data.extend([0; 4]);
self.missing.push((pos, sym));
if self.program.sym_info(sym).external {
self.sym_refs.entry(sym).or_default().push(pos);
} else {
self.missing.push((pos, sym));
}
return;
};
self.data.extend(addr_offset(self.data.len(), addr));
@@ -164,6 +194,8 @@ impl Encoder {
RegImm::Imm(imm) => self.push_imm(imm),
},
Instr::Pop(reg) => self.pop(reg),
Instr::CallMem(sym) => self.call_mem(sym),
Instr::Sub => self.data.extend([0x48, 0x83, 0xec, 0x28]),
}
Ok(())
}
@@ -176,12 +208,14 @@ fn addr_offset(pos: usize, addr: u64) -> [u8; 4] {
offset.to_le_bytes()
}
impl Encoder {
pub fn new(sym_count: usize) -> Self {
impl<'a> Encoder<'a> {
pub fn new(program: &'a Program<X86_64>) -> Self {
Self {
data: Default::default(),
sym_tab: SymTable::new(sym_count),
sym_tab: SymTable::new(program.sym_count()),
missing: Default::default(),
sym_refs: Default::default(),
program,
}
}
}
+50 -50
View File
@@ -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 -1
View File
@@ -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);
}