arbitrary addr

This commit is contained in:
2026-06-06 21:19:09 -04:00
parent 4587f687b9
commit ef35509c98
8 changed files with 35 additions and 18 deletions
+3 -2
View File
@@ -1,5 +1,5 @@
use crate::{
backend::{LinkedProgram, Program},
backend::{Addr, LinkedProgram, Program},
io::CompilerMsg,
};
@@ -8,5 +8,6 @@ pub mod x86_64;
pub trait Arch: Sized {
const NAME: &str;
type Asm;
fn compile(p: &Program<Self>) -> Result<LinkedProgram, CompilerMsg>;
type Addr: Addr;
fn compile(p: &Program<Self>) -> Result<LinkedProgram<Self::Addr>, CompilerMsg>;
}
+4 -4
View File
@@ -1,13 +1,13 @@
use super::*;
use crate::backend::{Addr, LinkedProgram, SymTable, Symbol};
use crate::backend::{LinkedProgram, SymTable, Symbol};
pub struct Encoder {
pub data: Vec<u8>,
pub sym_tab: SymTable,
pub sym_tab: SymTable<u64>,
pub missing: Vec<(usize, Symbol)>,
}
pub fn encode_program(p: &Program<X86_64>) -> Result<LinkedProgram, CompilerMsg> {
pub fn encode_program(p: &Program<X86_64>) -> Result<LinkedProgram<u64>, CompilerMsg> {
let mut encoder = Encoder::new(p.sym_count());
p.encode_data(&mut encoder.data, &mut encoder.sym_tab);
@@ -124,7 +124,7 @@ impl Encoder {
}
/// assumes the next instruction is directly after
fn addr_offset(pos: usize, addr: Addr) -> [u8; 4] {
fn addr_offset(pos: usize, addr: u64) -> [u8; 4] {
let pos = (pos + 4) as i32;
let offset = addr as i32 - pos;
offset.to_le_bytes()
+2 -1
View File
@@ -19,7 +19,8 @@ pub struct X86_64;
impl Arch for X86_64 {
const NAME: &str = "x86_64";
type Asm = Asm;
fn compile(p: &Program<Self>) -> Result<LinkedProgram, CompilerMsg> {
type Addr = u64;
fn compile(p: &Program<Self>) -> Result<LinkedProgram<Self::Addr>, CompilerMsg> {
encode_program(p)
}
}
+1 -1
View File
@@ -74,7 +74,7 @@ pub enum EType {
}
// this is currently specialized for riscv64; obviously add params later
pub fn create(program: &[u8], start_offset: Addr) -> Vec<u8> {
pub fn create(program: &[u8], start_offset: u64) -> Vec<u8> {
let pie = true;
let addr_start = if pie { 0 } else { 0x400000 };
let page_size = 0x1000;
+15
View File
@@ -0,0 +1,15 @@
pub trait Addr: Clone + Copy {
fn from_len(len: usize) -> Self;
}
impl Addr for u64 {
fn from_len(len: usize) -> Self {
len as Self
}
}
impl Addr for u32 {
fn from_len(len: usize) -> Self {
len as Self
}
}
+5 -3
View File
@@ -1,4 +1,6 @@
mod addr;
mod symbol;
pub use addr::*;
pub use symbol::*;
use crate::{arch::Arch, backend::LinkedProgram, io::CompilerMsg};
@@ -30,9 +32,9 @@ pub type VarId = usize;
pub type FnId = usize;
impl<A: Arch> Program<A> {
pub fn encode_data(&self, data: &mut Vec<u8>, sym_tab: &mut SymTable) {
pub fn encode_data(&self, data: &mut Vec<u8>, sym_tab: &mut SymTable<A::Addr>) {
for d in &self.ro_data {
let addr = data.len() as u64;
let addr = A::Addr::from_len(data.len());
data.extend(&d.bytes);
sym_tab.insert(d.sym, addr);
}
@@ -58,7 +60,7 @@ impl<A: Arch> Program<A> {
res
}
pub fn compile(&self) -> Result<LinkedProgram, CompilerMsg> {
pub fn compile(&self) -> Result<LinkedProgram<A::Addr>, CompilerMsg> {
A::compile(self)
}
+2 -4
View File
@@ -1,10 +1,8 @@
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub struct Symbol(pub(super) usize);
pub type Addr = u64;
pub struct SymTable(Vec<Option<Addr>>);
impl SymTable {
pub struct SymTable<Addr>(Vec<Option<Addr>>);
impl<Addr: Clone + Copy> SymTable<Addr> {
pub fn new(len: usize) -> Self {
Self(vec![None; len])
}
+3 -3
View File
@@ -1,11 +1,11 @@
use crate::backend::{Addr, elf};
use crate::backend::elf;
pub struct LinkedProgram {
pub struct LinkedProgram<Addr> {
pub code: Vec<u8>,
pub entry: Option<Addr>,
}
impl LinkedProgram {
impl LinkedProgram<u64> {
pub fn to_elf(&self) -> Vec<u8> {
elf::create(&self.code, self.entry.expect("no start"))
}