vlk-analysis 0.3.0

Lifting, SSA and simplification passes of the vlk decompiler
//! What loading a file needs of each instruction set.

use vlk_base::target::Isa;
use vlk_loader::loader::Image;
use vlk_loader::loader::discover::{Decode, Scan, Step, read_word};

mod aarch64;
#[cfg(test)]
mod tests;
mod x86;

struct X86(u32);

impl Scan for X86 {
    fn function_starts(&self, _: &Image, bytes: &[u8], base: u64) -> Vec<u64> {
        x86::function_starts(bytes, base, self.0)
    }

    fn opens(&self, bytes: &[u8]) -> bool {
        bytes
            .first()
            .is_some_and(|byte| !matches!(byte, 0x90 | 0xcc | 0x00))
    }

    fn decoder(&self) -> Option<&dyn Decode> {
        Some(self)
    }

    fn startup_main(&self, image: &Image) -> Option<u64> {
        if self.0 == 32 {
            x86::startup_main_32(image)
        } else {
            x86::startup_main_64(image)
        }
    }
}

impl Decode for X86 {
    fn step(&self, bytes: &[u8], address: u64) -> Option<Step> {
        x86::step(bytes, address, self.0)
    }
}

struct AArch64;

impl Scan for AArch64 {
    fn function_starts(&self, image: &Image, bytes: &[u8], base: u64) -> Vec<u64> {
        aarch64::function_starts(bytes, base, &image.relocated_branches())
    }

    fn opens(&self, bytes: &[u8]) -> bool {
        read_word(bytes, 0).is_some_and(|word| !aarch64::is_padding(word))
    }

    fn startup_main(&self, image: &Image) -> Option<u64> {
        aarch64::startup_main(image)
    }
}

struct RiscV64;

impl Scan for RiscV64 {
    // RISC-V and MIPS images often carry no unwind entries, so the names
    // they export do not stand for the functions inside.
    fn scans_beside_exports(&self) -> bool {
        true
    }

    fn function_starts(&self, _: &Image, bytes: &[u8], base: u64) -> Vec<u64> {
        super::riscv64::function_starts(bytes, base)
    }

    fn opens(&self, bytes: &[u8]) -> bool {
        super::riscv64::opens(bytes)
    }

    fn global_pointer(&self, image: &Image) -> Option<u64> {
        super::riscv64::global_pointer_at_entry(image)
    }
}

struct Mips32;

impl Scan for Mips32 {
    // RISC-V and MIPS images often carry no unwind entries, so the names
    // they export do not stand for the functions inside.
    fn scans_beside_exports(&self) -> bool {
        true
    }

    fn function_starts(&self, image: &Image, bytes: &[u8], base: u64) -> Vec<u64> {
        super::mips32::function_starts(
            bytes,
            base,
            image.byte_order() == Some(object::Endianness::Big),
            &image.code_addresses_in_data(),
        )
    }

    fn opens(&self, bytes: &[u8]) -> bool {
        bytes
            .first()
            .is_some_and(|byte| !matches!(byte, 0x90 | 0xcc | 0x00))
    }
}

/// What finds the functions in code of `isa`; none for 32-bit ARM, whose
/// code is not scanned.
#[must_use]
pub fn scan(isa: Isa) -> Option<&'static dyn Scan> {
    match isa {
        Isa::X86_64 => Some(&X86(64)),
        Isa::I386 => Some(&X86(32)),
        Isa::AArch64 => Some(&AArch64),
        Isa::RiscV64 => Some(&RiscV64),
        Isa::Mips32 => Some(&Mips32),
        Isa::Arm32 | Isa::Unsupported => None,
    }
}