pub mod resolve;
use std::collections::BTreeMap;
use binbun_aarch64::{AArch64, Mode as AArch64Mode};
use binbun_loader::{EntryFrame, Format, HandlerTable, Image, Machine, ScopeAction, SymbolKind};
use binbun_ssa::constant::ReadOnly as _;
use binbun_typedb::{LibraryError, Mode as Space, TypeLibrary};
use binbun_x86::{Mode, X86};
use crate::analysis::resolve::TableResolver;
use crate::cfg::{Callee, Callees, Frame, Options, Program, Returns, Seed, SeedKind, recover};
use crate::corrections::Corrections;
use crate::memory::ImageMemory;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Seeds {
pub stated: bool,
pub pointers: bool,
}
impl Seeds {
pub const ENTRY: Self = Self {
stated: false,
pointers: true,
};
pub const ALL: Self = Self {
stated: true,
pointers: true,
};
}
pub fn seeds(image: &Image<'_>, seeds: Seeds) -> Vec<Seed> {
let mut list = Vec::new();
list.extend(
image
.entry
.map(|entry| Seed::Function(entry, SeedKind::Entry)),
);
for symbol in &image.symbols {
let exported = matches!(
symbol.source,
binbun_loader::SymbolSource::Dynamic | binbun_loader::SymbolSource::Export
);
if symbol.kind == SymbolKind::Function
&& image.is_code(symbol.address)
&& (exported || seeds.stated)
{
let kind = if exported {
SeedKind::Export
} else {
SeedKind::Symbol
};
list.push(Seed::Function(symbol.address, kind));
}
}
if seeds.stated {
list.extend(
image
.unwind
.iter()
.filter(|entry| !entry.fragment && image.is_code(entry.start))
.map(|entry| Seed::Function(entry.start, SeedKind::Unwind)),
);
list.extend(
image
.unwind
.iter()
.filter(|entry| !entry.fragment)
.map(|entry| Seed::Extent {
function: entry.start,
end: entry.end,
frame: match entry.frame {
EntryFrame::Call => Frame::Call,
EntryFrame::Inside => Frame::Inside,
EntryFrame::Outermost => Frame::Outermost,
},
}),
);
list.extend(
image
.unwind
.iter()
.filter(|entry| entry.fragment)
.map(|entry| Seed::Split {
start: entry.start,
end: entry.end,
owner: entry.split_from,
}),
);
list.extend(image.landing_pads.iter().map(|pad| Seed::Code {
function: pad.function,
address: pad.address,
}));
list.extend(
image
.exception_tables
.iter()
.flat_map(|table| &table.call_sites)
.filter_map(|site| {
Some(Seed::Unwind {
start: site.start,
end: site.end,
pad: site.landing_pad?,
})
}),
);
list.extend(image.funclets.iter().flat_map(|funclet| {
[
Seed::Function(funclet.address, SeedKind::Unwind),
Seed::Funclet {
function: funclet.function,
address: funclet.address,
},
]
}));
list.extend(seh_ranges(image));
list.extend(continuations(image));
}
if seeds.pointers {
list.extend(
image
.code_pointers
.iter()
.map(|pointer| Seed::Function(pointer.target, SeedKind::Pointer)),
);
}
list.sort_by_key(Seed::address);
list
}
fn seh_ranges(image: &Image<'_>) -> Vec<Seed> {
let mut seeds = Vec::new();
for table in &image.frame_handlers {
let HandlerTable::Scopes(scopes) = &table.table else {
continue;
};
let mut taken: Vec<(u64, u64)> = Vec::new();
for scope in scopes {
let ScopeAction::Except { target, .. } = scope.action else {
continue;
};
if taken
.iter()
.any(|&(start, end)| start < scope.end && scope.start < end)
{
continue;
}
taken.push((scope.start, scope.end));
seeds.push(Seed::Unwind {
start: scope.start,
end: scope.end,
pad: target,
});
}
}
seeds
}
fn continuations(image: &Image<'_>) -> Vec<Seed> {
let mut seeds = Vec::new();
for table in &image.frame_handlers {
let HandlerTable::Cxx(cxx) = &table.table else {
continue;
};
seeds.extend(
cxx.tries
.iter()
.flat_map(|block| &block.catches)
.flat_map(|catch| &catch.continuations)
.map(|&address| Seed::Code {
function: table.function,
address,
}),
);
}
seeds
}
#[derive(Clone, Debug)]
pub struct ImageCallees<'a> {
imports: BTreeMap<u64, Returns>,
functions: BTreeMap<u64, Returns>,
image: &'a Image<'a>,
memory: ImageMemory<'a>,
}
impl<'a> ImageCallees<'a> {
pub fn new(image: &'a Image<'a>, library: &TypeLibrary, corrections: &Corrections) -> Self {
let returns = |module: Option<&str>, name: &str| match library
.function(module, name)
.and_then(|d| d.no_return)
.map(|fact| fact.value)
{
Some(true) => Returns::No,
Some(false) => Returns::Yes,
None => Returns::Unknown,
};
let imports = image
.imports
.iter()
.map(|import| {
(
import.slot,
returns(import.library.as_deref(), &import.name),
)
})
.collect();
let mut functions: BTreeMap<u64, Returns> = image
.symbols
.iter()
.filter(|symbol| symbol.kind == SymbolKind::Function)
.map(|symbol| (symbol.address, returns(None, &symbol.name)))
.filter(|&(_, returns)| returns != Returns::Unknown)
.collect();
functions.extend(
corrections
.no_return()
.iter()
.map(|&entry| (entry, Returns::No)),
);
Self {
imports,
functions,
image,
memory: ImageMemory::new(image),
}
}
}
impl Callees for ImageCallees<'_> {
fn is_import(&self, slot: u64) -> bool {
self.imports.contains_key(&slot)
}
fn slot(&self, slot: u64, size: u8) -> Option<u64> {
self.memory
.read(slot, u16::from(size))
.map(|value| value.low_u64())
.filter(|&address| self.image.is_code(address))
}
fn returns(&self, callee: Callee) -> Returns {
match callee {
Callee::Import(slot) => self.imports.get(&slot).copied().unwrap_or(Returns::Unknown),
Callee::Address(address) => self
.functions
.get(&address)
.copied()
.unwrap_or(Returns::Unknown),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UnsupportedMachine(pub Machine);
pub fn type_library(image: &Image<'_>) -> Result<TypeLibrary, LibraryError> {
let arch = match image.machine {
Machine::X86 => "x86",
Machine::X86_64 => "x86_64",
Machine::Aarch64 => "aarch64",
Machine::Arm => "arm",
Machine::Other => return Ok(TypeLibrary::builtin()),
};
let os = match image.format {
Format::Elf => "linux",
Format::Pe => "windows",
Format::MachO => "macos",
};
let space = if image.kernel {
Space::Kernel
} else {
Space::User
};
TypeLibrary::for_mode(arch, os, space)
}
pub fn x86_mode(machine: Machine) -> Result<Mode, UnsupportedMachine> {
match machine {
Machine::X86 => Ok(Mode::Bits32),
Machine::X86_64 => Ok(Mode::Bits64),
Machine::Aarch64 | Machine::Arm | Machine::Other => Err(UnsupportedMachine(machine)),
}
}
pub fn analyse(
image: &Image<'_>,
seeds: Seeds,
library: &TypeLibrary,
) -> Result<Program, UnsupportedMachine> {
analyse_corrected(image, seeds, library, &Corrections::default())
}
pub fn analyse_corrected(
image: &Image<'_>,
seeds: Seeds,
library: &TypeLibrary,
corrections: &Corrections,
) -> Result<Program, UnsupportedMachine> {
let mut seeds = self::seeds(image, seeds);
if !corrections.is_empty() {
seeds.extend(corrections.seeds());
seeds.sort_by_key(Seed::address);
}
let callees = ImageCallees::new(image, library, corrections);
let memory = image.code_view();
let options = Options::default();
let mut resolver = TableResolver::new(image);
Ok(match image.machine {
Machine::Aarch64 => recover(
&AArch64,
AArch64Mode::A64,
&memory,
&seeds,
&callees,
&mut resolver,
&options,
),
Machine::X86 | Machine::X86_64 | Machine::Arm | Machine::Other => recover(
&X86,
x86_mode(image.machine)?,
&memory,
&seeds,
&callees,
&mut resolver,
&options,
),
})
}
pub fn names(image: &Image<'_>) -> BTreeMap<u64, String> {
let mut names = BTreeMap::new();
for symbol in &image.symbols {
names
.entry(symbol.address)
.or_insert_with(|| symbol.name.clone());
}
names
}