ft: initial disasm of example data

This only contains two instructions of which I know
the correct output.
This commit is contained in:
2025-05-07 15:48:44 +09:00
commit 2af4578c8b
8 changed files with 804 additions and 0 deletions

62
src/aout.rs Normal file
View File

@@ -0,0 +1,62 @@
use std::ffi::{c_uchar, c_ushort};
#[allow(non_camel_case_types)]
pub type c_long = i32; // we use a a.out with 32 byte
#[derive(Debug)]
#[allow(dead_code)]
pub struct Aout {
pub header: Header,
pub text: Vec<u8>,
pub data: Vec<u8>,
}
impl Aout {
pub fn new(buf: Vec<u8>) -> Self {
let hdr = Header {
magic: [buf[0], buf[1]],
flags: buf[2],
cpu: buf[3],
hdrlen: buf[4],
unused: buf[5],
version: c_ushort::from_be_bytes([buf[6], buf[7]]),
text: c_long::from_le_bytes([buf[8], buf[9], buf[10], buf[11]]),
data: c_long::from_le_bytes([buf[12], buf[13], buf[14], buf[15]]),
bss: c_long::from_le_bytes([buf[16], buf[17], buf[18], buf[19]]),
entry: c_long::from_le_bytes([buf[20], buf[21], buf[22], buf[23]]),
total: c_long::from_le_bytes([buf[24], buf[25], buf[26], buf[27]]),
syms: c_long::from_le_bytes([buf[28], buf[29], buf[30], buf[31]]),
};
let text_start = hdr.hdrlen as usize;
let text_end = text_start + hdr.text as usize;
let data_start = text_end + 1;
let data_end = data_start + hdr.data as usize;
let text_section = &buf[text_start..text_end];
let data_section = &buf[data_start..data_end];
Aout {
header: hdr,
text: Vec::from(text_section),
data: Vec::from(data_section),
}
}
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct Header {
pub magic: [c_uchar; 2], // magic number
pub flags: c_uchar, // flags, see below
pub cpu: c_uchar, // cpu id
pub hdrlen: c_uchar, // length of header
pub unused: c_uchar, // reserved for future use
pub version: c_ushort, // version stamp
pub text: c_long, // size of text segment in bytes
pub data: c_long, // size of data segment in bytes
pub bss: c_long, // size of bss segment in bytes
pub entry: c_long, // entry point
pub total: c_long, // total memory allocated
pub syms: c_long, // size of symbol table
}