use crate::{Arch, BinaryFormat, Endian, Section};
pub const OBJ_EXECUTABLE: u32 = 0x0004;
pub const OBJ_WRITABLE: u32 = 0x0002;
pub const OBJ_READABLE: u32 = 0x0001;
pub const OBJ_BIG: u32 = 0x2000;
#[derive(Debug, Clone)]
pub struct LeObject {
pub number: u32,
pub base: u64,
pub virtual_size: u64,
pub flags: u32,
pub data: Vec<u8>,
}
impl LeObject {
pub fn executable(&self) -> bool {
self.flags & OBJ_EXECUTABLE != 0
}
pub fn writable(&self) -> bool {
self.flags & OBJ_WRITABLE != 0
}
pub fn big(&self) -> bool {
self.flags & OBJ_BIG != 0
}
fn end(&self) -> u64 {
self.base + self.virtual_size
}
fn contains(&self, addr: u64) -> bool {
addr >= self.base && addr < self.end()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LeFixupKind {
Byte,
Selector16,
Pointer16_16,
Offset16,
Pointer16_32,
Offset32,
Relative32,
}
impl LeFixupKind {
fn from_nibble(n: u8) -> Option<Self> {
Some(match n {
0x0 => LeFixupKind::Byte,
0x2 => LeFixupKind::Selector16,
0x3 => LeFixupKind::Pointer16_16,
0x5 => LeFixupKind::Offset16,
0x6 => LeFixupKind::Pointer16_32,
0x7 => LeFixupKind::Offset32,
0x8 => LeFixupKind::Relative32,
_ => return None,
})
}
pub fn has_selector(self) -> bool {
matches!(
self,
LeFixupKind::Selector16 | LeFixupKind::Pointer16_16 | LeFixupKind::Pointer16_32
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LeFixupTarget {
Internal { object: u32, offset: u32 },
ImportOrdinal {
module: u16,
ordinal: u32,
additive: u32,
},
ImportName {
module: u16,
name: String,
additive: u32,
},
Entry { ordinal: u16, additive: u32 },
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LeFixup {
pub address: u64,
pub kind: LeFixupKind,
pub target: LeFixupTarget,
pub alias: bool,
}
#[derive(Debug, Clone)]
pub struct LeBinary {
pub cpu: u16,
pub os_type: u16,
pub module_flags: u32,
pub page_size: u32,
pub module_name: Option<String>,
pub import_modules: Vec<String>,
pub objects: Vec<LeObject>,
pub fixups: Vec<LeFixup>,
pub entrypoint: Option<u64>,
pub initial_stack: Option<u64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LeError {
NotLe,
Lx,
BigEndian,
Truncated(&'static str),
UnsupportedPage { page: u32, flags: u8 },
BadFixup { offset: usize },
BadObject(u32),
}
impl std::fmt::Display for LeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LeError::NotLe => write!(f, "not an LE executable"),
LeError::Lx => write!(f, "LX executables are not supported"),
LeError::BigEndian => write!(f, "big-endian LE executables are not supported"),
LeError::Truncated(what) => write!(f, "LE {what} lies past the end of the file"),
LeError::UnsupportedPage { page, flags } => {
write!(f, "LE page {page} has unsupported type {flags}")
}
LeError::BadFixup { offset } => {
write!(f, "malformed LE fixup record at file offset 0x{offset:x}")
}
LeError::BadObject(n) => write!(f, "LE refers to missing object {n}"),
}
}
}
impl std::error::Error for LeError {}
pub(crate) fn header_offset(bytes: &[u8]) -> Option<(usize, [u8; 2])> {
if !bytes.starts_with(b"MZ") {
return None;
}
let at = u32::from_le_bytes(bytes.get(0x3c..0x40)?.try_into().ok()?) as usize;
let sig: [u8; 2] = bytes.get(at..at + 2)?.try_into().ok()?;
matches!(&sig, b"LE" | b"LX").then_some((at, sig))
}
struct Reader<'a> {
bytes: &'a [u8],
}
impl Reader<'_> {
fn u8(&self, at: usize, what: &'static str) -> Result<u8, LeError> {
self.bytes.get(at).copied().ok_or(LeError::Truncated(what))
}
fn u16(&self, at: usize, what: &'static str) -> Result<u16, LeError> {
let b = self.bytes.get(at..at + 2).ok_or(LeError::Truncated(what))?;
Ok(u16::from_le_bytes([b[0], b[1]]))
}
fn u32(&self, at: usize, what: &'static str) -> Result<u32, LeError> {
let b = self.bytes.get(at..at + 4).ok_or(LeError::Truncated(what))?;
Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
}
fn pstr(&self, at: usize, what: &'static str) -> Result<String, LeError> {
let len = self.u8(at, what)? as usize;
let b = self
.bytes
.get(at + 1..at + 1 + len)
.ok_or(LeError::Truncated(what))?;
Ok(String::from_utf8_lossy(b).into_owned())
}
}
impl LeBinary {
pub fn parse(bytes: &[u8]) -> Result<Self, LeError> {
let (le, sig) = header_offset(bytes).ok_or(LeError::NotLe)?;
if &sig == b"LX" {
return Err(LeError::Lx);
}
let r = Reader { bytes };
let h8 = |off: usize| r.u8(le + off, "header");
let h16 = |off: usize| r.u16(le + off, "header");
let h32 = |off: usize| r.u32(le + off, "header");
if h8(0x02)? != 0 || h8(0x03)? != 0 {
return Err(LeError::BigEndian);
}
let module_pages = h32(0x14)?;
let page_size = h32(0x28)?;
let last_page_size = h32(0x2c)?;
let object_table = le + h32(0x40)? as usize;
let object_count = h32(0x44)?;
let page_map = le + h32(0x48)? as usize;
let resident_names = h32(0x58)?;
let fixup_pages = le + h32(0x68)? as usize;
let fixup_records = le + h32(0x6c)? as usize;
let import_module_table = le + h32(0x70)? as usize;
let import_module_count = h32(0x74)?;
let import_procs = le + h32(0x78)? as usize;
let data_pages = h32(0x80)? as usize;
let module_name = if resident_names != 0 {
Some(r.pstr(le + resident_names as usize, "resident name table")?)
.filter(|n| !n.is_empty())
} else {
None
};
let mut import_modules = Vec::new();
let mut at = import_module_table;
for _ in 0..import_module_count {
let name = r.pstr(at, "import module table")?;
at += 1 + name.len();
import_modules.push(name);
}
let mut objects = Vec::new();
let mut pages: Vec<(u32, u64)> = Vec::new(); for i in 0..object_count {
let e = object_table + 24 * i as usize;
let virtual_size = r.u32(e, "object table")? as u64;
let base = r.u32(e + 4, "object table")? as u64;
let flags = r.u32(e + 8, "object table")?;
let first_page = r.u32(e + 12, "object table")?;
let page_count = r.u32(e + 16, "object table")?;
let mut data = Vec::new();
for j in 0..page_count {
let logical = first_page + j;
let pm = page_map + 4 * (logical as usize - 1);
let number = (r.u8(pm, "page map")? as u32) << 16
| (r.u8(pm + 1, "page map")? as u32) << 8
| r.u8(pm + 2, "page map")? as u32;
let kind = r.u8(pm + 3, "page map")?;
data.resize((j * page_size) as usize, 0);
match kind {
0 => {
let size = if number == module_pages {
last_page_size
} else {
page_size
};
let start = data_pages + (number as usize - 1) * page_size as usize;
let page = bytes
.get(start..start + size as usize)
.ok_or(LeError::Truncated("page"))?;
data.extend_from_slice(page);
}
2 | 3 => data.resize(((j + 1) * page_size) as usize, 0),
flags => {
return Err(LeError::UnsupportedPage {
page: logical,
flags,
});
}
}
pages.push((logical, base + (j * page_size) as u64));
}
data.truncate(virtual_size as usize);
objects.push(LeObject {
number: i + 1,
base,
virtual_size,
flags,
data,
});
}
let mut fixups = Vec::new();
for &(logical, page_va) in &pages {
let slot = fixup_pages + 4 * (logical as usize - 1);
let start = fixup_records + r.u32(slot, "fixup page table")? as usize;
let end = fixup_records + r.u32(slot + 4, "fixup page table")? as usize;
let mut at = start;
while at < end {
at = parse_fixup(&r, at, page_va, import_procs, &mut fixups)?;
}
}
fixups.sort_by_key(|f| f.address);
let mut binary = LeBinary {
cpu: h16(0x08)?,
os_type: h16(0x0a)?,
module_flags: h32(0x10)?,
page_size,
module_name,
import_modules,
objects,
fixups: Vec::new(),
entrypoint: None,
initial_stack: None,
};
binary.entrypoint = binary.linear(h32(0x18)?, h32(0x1c)?)?;
binary.initial_stack = binary.linear(h32(0x20)?, h32(0x24)?)?;
for f in &fixups {
binary.apply(f)?;
}
binary.fixups = fixups;
Ok(binary)
}
fn linear(&self, object: u32, offset: u32) -> Result<Option<u64>, LeError> {
if object == 0 {
return Ok(None);
}
let obj = self.object(object)?;
Ok(Some(obj.base + offset as u64))
}
fn object(&self, number: u32) -> Result<&LeObject, LeError> {
(number as usize)
.checked_sub(1)
.and_then(|i| self.objects.get(i))
.ok_or(LeError::BadObject(number))
}
pub fn fixup_target(&self, fixup: &LeFixup) -> Option<u64> {
match fixup.target {
LeFixupTarget::Internal { object, offset } => {
self.linear(object, offset).ok().flatten()
}
_ => None,
}
}
fn apply(&mut self, fixup: &LeFixup) -> Result<(), LeError> {
let LeFixupTarget::Internal { object, .. } = fixup.target else {
return Ok(());
};
let target = self.fixup_target(fixup).ok_or(LeError::BadObject(object))?;
let at = fixup.address;
let value: Vec<u8> = match fixup.kind {
LeFixupKind::Selector16 => return Ok(()),
LeFixupKind::Byte => vec![target as u8],
LeFixupKind::Offset16 | LeFixupKind::Pointer16_16 => {
(target as u16).to_le_bytes().to_vec()
}
LeFixupKind::Offset32 | LeFixupKind::Pointer16_32 => {
(target as u32).to_le_bytes().to_vec()
}
LeFixupKind::Relative32 => (target.wrapping_sub(at + 4) as u32).to_le_bytes().to_vec(),
};
if let Some(obj) = self.objects.iter_mut().find(|o| o.contains(at)) {
let off = (at - obj.base) as usize;
let end = (off + value.len()).min(obj.virtual_size as usize);
if obj.data.len() < end {
obj.data.resize(end, 0);
}
for (i, b) in value.into_iter().enumerate() {
if let Some(slot) = obj.data.get_mut(off + i) {
*slot = b;
}
}
}
Ok(())
}
fn object_at(&self, addr: u64) -> Option<&LeObject> {
self.objects.iter().find(|o| o.contains(addr))
}
}
fn parse_fixup(
r: &Reader<'_>,
at: usize,
page_va: u64,
import_procs: usize,
out: &mut Vec<LeFixup>,
) -> Result<usize, LeError> {
let bad = LeError::BadFixup { offset: at };
let src = r.u8(at, "fixup record")?;
let flags = r.u8(at + 1, "fixup record")?;
let kind = LeFixupKind::from_nibble(src & 0x0f).ok_or(bad.clone())?;
let alias = src & 0x10 != 0;
let source_list = src & 0x20 != 0;
let mut p = at + 2;
let (first, count) = if source_list {
let n = r.u8(p, "fixup record")?;
p += 1;
(None, n)
} else {
let off = r.u16(p, "fixup record")? as i16;
p += 2;
(Some(off), 1)
};
let read_object = |p: &mut usize| -> Result<u16, LeError> {
if flags & 0x40 != 0 {
let v = r.u16(*p, "fixup record")?;
*p += 2;
Ok(v)
} else {
let v = r.u8(*p, "fixup record")? as u16;
*p += 1;
Ok(v)
}
};
let read_additive = |p: &mut usize| -> Result<u32, LeError> {
if flags & 0x04 == 0 {
return Ok(0);
}
if flags & 0x20 != 0 {
let v = r.u32(*p, "fixup record")?;
*p += 4;
Ok(v)
} else {
let v = r.u16(*p, "fixup record")? as u32;
*p += 2;
Ok(v)
}
};
let read_offset = |p: &mut usize| -> Result<u32, LeError> {
if flags & 0x10 != 0 {
let v = r.u32(*p, "fixup record")?;
*p += 4;
Ok(v)
} else {
let v = r.u16(*p, "fixup record")? as u32;
*p += 2;
Ok(v)
}
};
let target = match flags & 0x03 {
0 => {
let object = read_object(&mut p)? as u32;
let offset = if kind == LeFixupKind::Selector16 {
0
} else {
read_offset(&mut p)?
};
LeFixupTarget::Internal { object, offset }
}
1 => {
let module = read_object(&mut p)?;
let ordinal = if flags & 0x80 != 0 {
let v = r.u8(p, "fixup record")? as u32;
p += 1;
v
} else {
read_offset(&mut p)?
};
let additive = read_additive(&mut p)?;
LeFixupTarget::ImportOrdinal {
module,
ordinal,
additive,
}
}
2 => {
let module = read_object(&mut p)?;
let name_at = read_offset(&mut p)? as usize;
let name = r.pstr(import_procs + name_at, "import procedure name table")?;
let additive = read_additive(&mut p)?;
LeFixupTarget::ImportName {
module,
name,
additive,
}
}
_ => {
let ordinal = read_object(&mut p)?;
let additive = read_additive(&mut p)?;
LeFixupTarget::Entry { ordinal, additive }
}
};
let mut push = |off: i16| {
out.push(LeFixup {
address: page_va.wrapping_add_signed(off as i64),
kind,
target: target.clone(),
alias,
})
};
match first {
Some(off) => push(off),
None => {
for _ in 0..count {
push(r.u16(p, "fixup record")? as i16);
p += 2;
}
}
}
Ok(p)
}
impl BinaryFormat for LeBinary {
fn load_address(&self) -> u64 {
self.objects.iter().map(|o| o.base).min().unwrap_or(0)
}
fn architecture(&self) -> Arch {
Arch::I386
}
fn endianness(&self) -> Endian {
Endian::Little
}
fn bits(&self) -> u32 {
let code = match self.entrypoint.and_then(|e| self.object_at(e)) {
Some(o) => o.big(),
None => self.objects.iter().any(|o| o.executable() && o.big()),
};
if code { 32 } else { 16 }
}
fn sections(&self) -> Vec<Section> {
self.objects
.iter()
.map(|o| Section {
name: format!("object{}", o.number),
address: o.base,
size: o.virtual_size,
writable: o.writable(),
executable: o.executable(),
})
.collect()
}
fn linked_libraries(&self) -> Vec<String> {
self.import_modules.clone()
}
fn byte_at(&self, addr: u64) -> Option<u8> {
let o = self.object_at(addr)?;
Some(o.data.get((addr - o.base) as usize).copied().unwrap_or(0))
}
fn bytes_at(&self, addr: u64) -> Option<&[u8]> {
let o = self.object_at(addr)?;
o.data.get((addr - o.base) as usize..)
}
fn mapped_regions(&self) -> Vec<(u64, Vec<u8>, bool, bool)> {
self.objects
.iter()
.map(|o| {
let mut bytes = o.data.clone();
bytes.resize(o.virtual_size as usize, 0);
(o.base, bytes, o.executable(), o.writable())
})
.collect()
}
fn is_executable(&self, addr: u64) -> bool {
self.object_at(addr).is_some_and(|o| o.executable())
}
fn is_known_writable(&self, addr: u64) -> bool {
self.object_at(addr).is_some_and(|o| o.writable())
}
fn is_known_read_only(&self, _addr: u64) -> bool {
false
}
fn segment_bounds(&self, addr: u64) -> Option<(u64, u64)> {
self.object_at(addr).map(|o| (o.base, o.end()))
}
fn entry_points(&self) -> Vec<u64> {
self.entrypoint.into_iter().collect()
}
fn entrypoint(&self) -> Option<u64> {
self.entrypoint
}
fn symbol_name(&self, addr: u64) -> Option<&str> {
(Some(addr) == self.entrypoint).then_some("_start")
}
fn symbol_address(&self, name: &str) -> Option<u64> {
(name == "_start").then_some(self.entrypoint).flatten()
}
}
#[cfg(test)]
mod tests {
use super::*;
pub(crate) fn sample() -> Vec<u8> {
let le = 0x40usize;
let page = 0x200u32;
let mut f = vec![0u8; 0x600];
f[..2].copy_from_slice(b"MZ");
f[0x3c..0x40].copy_from_slice(&(le as u32).to_le_bytes());
let put32 =
|f: &mut Vec<u8>, at: usize, v: u32| f[at..at + 4].copy_from_slice(&v.to_le_bytes());
f[le..le + 2].copy_from_slice(b"LE");
f[le + 0x08] = 2; f[le + 0x0a] = 1;
put32(&mut f, le + 0x14, 2); put32(&mut f, le + 0x18, 1); put32(&mut f, le + 0x1c, 0x0); put32(&mut f, le + 0x20, 2); put32(&mut f, le + 0x24, 0x100); put32(&mut f, le + 0x28, page);
put32(&mut f, le + 0x2c, 0x20); put32(&mut f, le + 0x40, 0xc4); put32(&mut f, le + 0x44, 2);
put32(&mut f, le + 0x48, 0xf4); put32(&mut f, le + 0x58, 0xfc); put32(&mut f, le + 0x68, 0x104); put32(&mut f, le + 0x6c, 0x110); put32(&mut f, le + 0x70, 0x160); put32(&mut f, le + 0x78, 0x160); put32(&mut f, le + 0x80, 0x200);
let ot = le + 0xc4;
for (i, (vs, base, flags, first)) in [
(
0x40u32,
0x10000u32,
OBJ_READABLE | OBJ_EXECUTABLE | OBJ_BIG,
1u32,
),
(0x100, 0x20000, OBJ_READABLE | OBJ_WRITABLE | OBJ_BIG, 2),
]
.into_iter()
.enumerate()
{
let e = ot + 24 * i;
put32(&mut f, e, vs);
put32(&mut f, e + 4, base);
put32(&mut f, e + 8, flags);
put32(&mut f, e + 12, first);
put32(&mut f, e + 16, 1);
}
f[le + 0xf4..le + 0xf8].copy_from_slice(&[0, 0, 1, 0]);
f[le + 0xf8..le + 0xfc].copy_from_slice(&[0, 0, 2, 0]);
f[le + 0xfc] = 4;
f[le + 0xfd..le + 0x101].copy_from_slice(b"demo");
let recs: Vec<u8> = [
vec![0x07, 0x00, 0x01, 0x00, 0x02, 0x10, 0x00],
vec![0x08, 0x00, 0x06, 0x00, 0x01, 0x20, 0x00],
vec![0x06, 0x00, 0x0b, 0x00, 0x02, 0x04, 0x00],
]
.concat();
let page2: Vec<u8> = vec![
0x27, 0x10, 0x03, 0x01, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x1e, 0x00,
];
let fr = le + 0x110;
f[fr..fr + recs.len()].copy_from_slice(&recs);
f[fr + recs.len()..fr + recs.len() + page2.len()].copy_from_slice(&page2);
put32(&mut f, le + 0x104, 0);
put32(&mut f, le + 0x108, recs.len() as u32);
put32(&mut f, le + 0x10c, (recs.len() + page2.len()) as u32);
let code: [u8; 17] = [
0xa1, 0x10, 0, 0, 0, 0xe8, 0, 0, 0, 0, 0x90, 0, 0, 0, 0, 0, 0, ];
f[0x200..0x200 + code.len()].copy_from_slice(&code);
f[0x200 + page as usize..0x200 + page as usize + 0x20].fill(0xcc);
f.truncate(0x200 + page as usize + 0x20);
f
}
#[test]
fn parses_objects_and_applies_internal_fixups() {
let b = LeBinary::parse(&sample()).unwrap();
assert_eq!(b.module_name.as_deref(), Some("demo"));
assert_eq!(b.objects.len(), 2);
assert_eq!(b.entrypoint, Some(0x10000));
assert_eq!(b.initial_stack, Some(0x20100));
assert_eq!(b.bits(), 32);
assert_eq!(b.architecture(), Arch::I386);
assert_eq!(b.read_uint(0x10001, 4), Some(0x20010));
assert_eq!(b.read_uint(0x10006, 4), Some(0x10020 - 0x1000a));
assert_eq!(b.read_uint(0x1000b, 4), Some(0x20004));
assert_eq!(b.read_uint(0x20000, 4), Some(0x10020));
assert_eq!(b.read_uint(0x20008, 4), Some(0x10020));
assert_eq!(b.read_uint(0x2001e, 4), Some(0x10020));
assert_eq!(b.byte_at(0x20004), Some(0xcc));
assert_eq!(b.byte_at(0x200ff), Some(0));
assert_eq!(b.byte_at(0x20100), None);
}
#[test]
fn keeps_every_fixup_with_its_target() {
let b = LeBinary::parse(&sample()).unwrap();
let rows: Vec<_> = b
.fixups
.iter()
.map(|f| (f.address, f.kind, b.fixup_target(f)))
.collect();
assert_eq!(
rows,
vec![
(0x10001, LeFixupKind::Offset32, Some(0x20010)),
(0x10006, LeFixupKind::Relative32, Some(0x10020)),
(0x1000b, LeFixupKind::Pointer16_32, Some(0x20004)),
(0x20000, LeFixupKind::Offset32, Some(0x10020)),
(0x20008, LeFixupKind::Offset32, Some(0x10020)),
(0x2001e, LeFixupKind::Offset32, Some(0x10020)),
]
);
assert!(LeFixupKind::Pointer16_32.has_selector());
}
#[test]
fn reports_object_permissions() {
let b = LeBinary::parse(&sample()).unwrap();
assert!(b.is_executable(0x10000));
assert!(!b.is_executable(0x20000));
assert!(b.is_known_writable(0x20000));
assert!(!b.is_known_read_only(0x10000));
let names: Vec<_> = b.sections().into_iter().map(|s| s.name).collect();
assert_eq!(names, ["object1", "object2"]);
}
#[test]
fn rejects_lx_and_bare_mz() {
let mut f = sample();
f[0x40..0x42].copy_from_slice(b"LX");
assert_eq!(LeBinary::parse(&f).unwrap_err(), LeError::Lx);
f[0x40..0x42].copy_from_slice(b"NE");
assert_eq!(LeBinary::parse(&f).unwrap_err(), LeError::NotLe);
}
}