mod callers;
pub(crate) mod inference;
use vlk_base::target::Isa;
use vlk_ir::ir::{BinOp, Expr, Stmt};
use vlk_loader::loader::{Image, NameSource};
use vlk_program::program::Program;
use vlk_types::types::CType;
pub const PARAMETER_BASE: u16 = vlk_ir::ir::TEMP_BASE + 0x60;
pub(crate) const TEMPORARIES: [u16; 4] = [
vlk_ir::ir::TEMP_BASE + 0x50,
vlk_ir::ir::TEMP_BASE + 0x51,
vlk_ir::ir::TEMP_BASE + 0x52,
vlk_ir::ir::TEMP_BASE + 0x53,
];
pub(crate) fn types(program: &Program, address: u64) -> Option<(Vec<CType>, CType)> {
let image = &program.image;
declared_types(image, address).or_else(|| inference::types(program, address))
}
pub(crate) fn definition_types(program: &Program, address: u64) -> Option<(Vec<CType>, CType)> {
let image = &program.image;
let Some(types) = types(program, address) else {
return None;
};
if image
.functions
.get(&address)
.is_some_and(|function| function.sources.contains(&NameSource::Plt))
&& !wide(&types.1)
{
return None;
}
Some(types)
}
fn declared_types(image: &Image, address: u64) -> Option<(Vec<CType>, CType)> {
if vlk_arch::arch::pointer_bytes(image.isa) != 4 {
return None;
}
if let Some(types) = vlk_loader::loader::dwarf::function_types(image, address) {
if types.variadic || crate::analysis::abi::has_aggregate(&types) {
return None;
}
let params: Option<Vec<CType>> = types.params.into_iter().map(|(_, ty)| ty).collect();
let (Some(params), Some(ret)) = (params, types.ret) else {
return None;
};
if params.iter().chain([&ret]).all(supported) {
return Some((params, ret));
}
return None;
}
let Some(function) = image.functions.get(&address) else {
return None;
};
let Some(name) = function.name.as_deref() else {
return None;
};
let prototype = if function.sources.contains(&NameSource::Plt) {
vlk_prototypes::prototypes::for_image(name, image)
.or_else(|| vlk_prototypes::prototypes::runtime(name))
} else {
vlk_prototypes::prototypes::runtime(name)
};
if let Some(prototype) = prototype
&& !prototype.variadic
&& prototype
.params
.iter()
.chain([&prototype.ret])
.all(supported)
{
return Some((prototype.params.clone(), prototype.ret.clone()));
}
None
}
fn supported(ty: &CType) -> bool {
ty.is_pointer() || *ty == CType::Void || ty.integer().is_some_and(|(bits, _)| bits <= 64)
}
pub fn wide(ty: &CType) -> bool {
if let CType::Named(name) = ty {
let name = name.trim();
let longs = name
.split_whitespace()
.filter(|word| *word == "long")
.count();
return ty.integer().is_some_and(|(bits, _)| bits == 64)
&& (longs == 2
|| name.ends_with("64_t")
|| matches!(
name,
"i64" | "u64" | "intmax_t" | "uintmax_t" | "__int64" | "unsigned __int64"
));
}
ty.integer().is_some_and(|(bits, _)| bits == 64)
}
#[derive(Clone, Copy)]
pub(crate) enum Location {
Register(u16),
Stack(i64),
}
pub(super) fn register_words(isa: Isa) -> u16 {
if isa == Isa::I386 { 0 } else { 4 }
}
pub(super) fn argument_register(isa: Isa, word: u16) -> u16 {
if isa == Isa::Mips32 { word + 2 } else { word }
}
pub(super) fn stack_first(isa: Isa) -> i64 {
match isa {
Isa::I386 => 4,
Isa::Mips32 => 16,
_ => 0,
}
}
pub(super) fn aligned_pairs(isa: Isa) -> bool {
isa != Isa::I386
}
pub(crate) fn locations(isa: Isa, params: &[CType]) -> Option<Vec<(Location, bool)>> {
let mut register = 0u16;
let mut stack = stack_first(isa);
let mut result = Vec::new();
for ty in params {
if !supported(ty) || *ty == CType::Void {
return None;
}
let pair = wide(ty);
if aligned_pairs(isa) && pair {
register = (register + 1) & !1;
}
let words = if pair { 2 } else { 1 };
if register < register_words(isa) {
result.push((Location::Register(argument_register(isa, register)), pair));
register += words;
} else {
if aligned_pairs(isa) && pair {
let Some(aligned) = stack.checked_add(7) else {
return None;
};
stack = aligned & !7;
}
result.push((Location::Stack(stack), pair));
let Some(next) = stack.checked_add(i64::from(words) * 4) else {
return None;
};
stack = next;
}
}
Some(result)
}
pub(crate) fn high_first(image: &Image) -> bool {
image.byte_order() == Some(object::Endianness::Big)
}
pub(crate) fn result_registers(image: &Image) -> (u16, u16) {
result_pair(image.isa, high_first(image))
}
fn result_pair(isa: Isa, high_first: bool) -> (u16, u16) {
match (isa, high_first) {
(Isa::I386, _) => (0, 2),
(_, true) => (1, 0),
(_, false) => (0, 1),
}
}
pub fn join(low: Expr, high: Expr) -> Expr {
Expr::bin(
BinOp::Or,
super::arm32::low(low),
Expr::bin(BinOp::Shl, super::arm32::low(high), Expr::constant(32)),
)
}
pub(super) fn call(program: &Program, target: Option<u64>, stmt: &mut Stmt) -> bool {
let image = &program.image;
let Some(target) = target else { return false };
if image.isa == Isa::I386
&& matches!(stmt, Stmt::Call { args: Some(args), .. }
if args.iter().any(|arg| matches!(arg, Expr::Reg { .. })))
{
return false;
}
let inferred = types(program, target);
let (params, ret) = match inferred {
Some((params, ret)) => (params, Some(ret)),
None => match inference::parameters(program, target) {
Some(params) => (params, None),
None => return false,
},
};
let Some(locations) = locations(image.isa, ¶ms) else {
return false;
};
let Stmt::Call {
kind,
convention,
args,
..
} = stmt
else {
return false;
};
if !matches!(
*convention,
vlk_types::types::Convention::Cdecl
| vlk_types::types::Convention::Aapcs32
| vlk_types::types::Convention::O32
) {
return false;
}
let mut values = Vec::new();
for (location, pair) in locations {
let value = match location {
Location::Register(register) => {
if pair {
let (first, second) = (Expr::reg(register, 32), Expr::reg(register + 1, 32));
if high_first(image) {
join(second, first)
} else {
join(first, second)
}
} else {
Expr::reg(register, 32)
}
}
Location::Stack(offset) => {
let Some(offset) = kind.stack_offset(image.isa, *convention, offset) else {
return false;
};
if pair {
let Some(high) = offset.checked_add(4) else {
return false;
};
let (first, second) =
(stack_word(image.isa, offset), stack_word(image.isa, high));
if high_first(image) {
join(second, first)
} else {
join(first, second)
}
} else {
stack_word(image.isa, offset)
}
}
};
values.push(value);
}
*args = Some(values);
if ret.as_ref().is_some_and(wide) {
logical_result(stmt, 64);
return true;
}
false
}
fn stack_word(isa: Isa, offset: i64) -> Expr {
Expr::Load {
addr: Box::new(Expr::bin(
BinOp::Add,
Expr::reg(vlk_arch::arch::stack_pointer(isa), 64),
Expr::constant(offset as u64),
)),
width: 4,
signed: false,
}
}
fn logical_result(stmt: &mut Stmt, width: u8) {
let Stmt::Call {
outputs, clobbers, ..
} = stmt
else {
return;
};
if let Some(clobbers) = clobbers {
clobbers.insert(0);
}
outputs.retain(|output| output.register == 0);
if let Some(output) = outputs.first_mut() {
output.dest.index = TEMPORARIES[3];
output.dest.width = width;
} else {
outputs.push(vlk_ir::ir::CallOutput {
register: 0,
dest: vlk_ir::ir::RegRef {
index: TEMPORARIES[3],
width,
},
});
}
}
pub(super) fn word_remainder(image: &Image, target: Option<u64>, stmt: &mut Stmt) -> bool {
if image.isa != Isa::Arm32 {
return false;
}
let name = target
.and_then(|target| image.functions.get(&target))
.and_then(|function| function.name.as_deref());
if !matches!(name, Some("__aeabi_idivmod" | "__aeabi_uidivmod")) {
return false;
}
logical_result(stmt, 32);
true
}
pub(super) fn split_word_result() -> [Stmt; 2] {
[
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: 0,
width: 64,
},
value: super::arm32::low(Expr::reg(TEMPORARIES[3], 64)),
at: None,
},
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: 1,
width: 64,
},
value: super::arm32::low(Expr::reg(TEMPORARIES[2], 64)),
at: None,
},
]
}
pub(super) fn split_result(isa: Isa, high_first: bool) -> [Stmt; 2] {
let (low, high) = result_pair(isa, high_first);
[
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: high,
width: 64,
},
value: Expr::bin(
BinOp::Shr,
Expr::reg(TEMPORARIES[3], 64),
Expr::constant(32),
),
at: None,
},
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: low,
width: 64,
},
value: super::arm32::low(Expr::reg(TEMPORARIES[3], 64)),
at: None,
},
]
}
pub(super) fn remainder_arguments(
image: &Image,
target: Option<u64>,
stmt: &mut Stmt,
) -> Option<[Stmt; 2]> {
let Some(target) = target else { return None };
let Some(name) = image
.functions
.get(&target)
.and_then(|function| function.name.as_deref())
else {
return None;
};
if !matches!(
name,
"__aeabi_ldivmod" | "__aeabi_uldivmod" | "__aeabi_idivmod" | "__aeabi_uidivmod"
) {
return None;
}
let Stmt::Call {
args: Some(args), ..
} = stmt
else {
return None;
};
let [left, right] = args.as_mut_slice() else {
return None;
};
let captures = [
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: TEMPORARIES[0],
width: 64,
},
value: std::mem::replace(left, Expr::reg(TEMPORARIES[0], 64)),
at: None,
},
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: TEMPORARIES[1],
width: 64,
},
value: std::mem::replace(right, Expr::reg(TEMPORARIES[1], 64)),
at: None,
},
];
Some(captures)
}
pub(super) fn remainder_value() -> Stmt {
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: TEMPORARIES[2],
width: 64,
},
value: Expr::bin(
BinOp::Sub,
Expr::reg(TEMPORARIES[0], 64),
Expr::bin(
BinOp::Mul,
Expr::reg(TEMPORARIES[3], 64),
Expr::reg(TEMPORARIES[1], 64),
),
),
at: None,
}
}
pub(super) fn split_remainder() -> [Stmt; 2] {
[
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: 2,
width: 64,
},
value: super::arm32::low(Expr::reg(TEMPORARIES[2], 64)),
at: None,
},
Stmt::Assign {
dest: vlk_ir::ir::RegRef {
index: 3,
width: 64,
},
value: Expr::bin(
BinOp::Shr,
Expr::reg(TEMPORARIES[2], 64),
Expr::constant(32),
),
at: None,
},
]
}