GitCorner

Code

ossartcraftcraftanything🌐
642d5a256791
//! An executable image: the parsed container plus its bytes, with address-level reads and
//! instruction decoding.

use crate::DisasmError;
use crate::decode::{Insn, Mode, decode};
use craft_anything_formats::{Arch, Binary, Format, SymbolKind};
use std::collections::BTreeMap;
use std::sync::Arc;

#[derive(Debug, Clone)]
pub struct Image {
    pub bin: Binary,
    pub data: Arc<[u8]>,
    /// Decoding mode; `None` when the architecture isn't supported by the built-in decoder.
    pub mode: Option<Mode>,
}

/// Select the built-in decoding mode for `bin`. Entryless ARM images (such as `.so` shared
/// libraries) inspect distinct executable function symbols, exports, and function starts so Thumb
/// libraries (`addr & 1 == 1`) are not decoded as 32-bit A32 code.
pub fn mode_for(bin: &Binary) -> Option<Mode> {
    match bin.arch {
        Arch::X86_16 => Some(Mode::X16),
        Arch::X86 => Some(Mode::X32),
        Arch::X86_64 => Some(Mode::X64),
        Arch::Aarch64 => Some(Mode::A64),
        Arch::Arm => match bin.entry {
            Some(e) if e & 1 == 1 => Some(Mode::T32),
            Some(_) => Some(Mode::A32),
            None => {
                let mut seen = BTreeMap::new();
                let mut count = |addr: u64| {
                    let clean = addr & !1;
                    if addr != 0 && bin.is_exec(clean) {
                        if addr & 1 == 1 {
                            seen.insert(clean, true);
                        } else if addr & 3 == 0 {
                            seen.entry(clean).or_insert(false);
                        }
                    }
                };
                for s in &bin.symbols {
                    if s.defined && s.kind == SymbolKind::Function {
                        count(s.addr);
                    }
                }
                for e in &bin.exports {
                    count(e.addr);
                }
                for &f in &bin.function_starts {
                    count(f);
                }
                let thumb = seen.values().filter(|&&is_thumb| is_thumb).count();
                let arm = seen.len().saturating_sub(thumb);
                Some(if thumb > arm { Mode::T32 } else { Mode::A32 })
            }
        },
        Arch::Other => None,
    }
}

impl Image {
    /// Parse any supported container.
    pub fn parse(data: Arc<[u8]>) -> Result<Image, DisasmError> {
        let bin = craft_anything_formats::parse_binary(&data).map_err(DisasmError::Format)?;
        Ok(Image::from_binary(data, bin))
    }

    pub fn from_binary(data: Arc<[u8]>, bin: Binary) -> Image {
        let mode = mode_for(&bin);
        Image { bin, data, mode }
    }

    /// A DOS .COM program (16-bit, loaded at 0x100).
    pub fn dos_com(data: Arc<[u8]>) -> Result<Image, DisasmError> {
        let bin = craft_anything_formats::dos::com_binary(&data).map_err(DisasmError::Format)?;
        Ok(Image::from_binary(data, bin))
    }

    /// Raw machine code for `arch`, loaded at `base`.
    pub fn raw(data: Arc<[u8]>, arch: Arch, base: u64) -> Image {
        let bin = Binary::raw(arch, base, data.len() as u64, 0);
        Image::from_binary(data, bin)
    }

    pub fn format(&self) -> Format {
        self.bin.format
    }

    /// File-backed bytes at `va` (never crossing the end of the section).
    pub fn read(&self, va: u64, len: usize) -> Option<&[u8]> {
        self.bin.read(&self.data, va, len)
    }

    pub fn is_exec(&self, va: u64) -> bool {
        self.bin.is_exec(va)
    }

    pub fn is_mapped(&self, va: u64) -> bool {
        self.bin.is_mapped(va)
    }

    /// Decode the instruction at `va`.
    pub fn decode(&self, va: u64, fmt: bool) -> Option<Insn> {
        let mode = self.mode?;
        let bytes = self.read(va, mode.max_insn_len())?;
        decode(mode, bytes, va, fmt)
    }

    pub fn pointer_size(&self) -> u64 {
        if self.bin.bits == 64 {
            8
        } else if self.bin.bits == 16 {
            2
        } else {
            4
        }
    }
}