mirror of https://github.com/encounter/objdiff.git
211 lines
5.4 KiB
Rust
211 lines
5.4 KiB
Rust
pub mod elf;
|
|
pub mod mips;
|
|
pub mod ppc;
|
|
|
|
use std::{collections::BTreeMap, path::PathBuf};
|
|
|
|
use flagset::{flags, FlagSet};
|
|
|
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
|
pub enum ObjSectionKind {
|
|
Code,
|
|
Data,
|
|
Bss,
|
|
}
|
|
flags! {
|
|
pub enum ObjSymbolFlags: u8 {
|
|
Global,
|
|
Local,
|
|
Weak,
|
|
Common,
|
|
}
|
|
}
|
|
#[derive(Debug, Copy, Clone, Default)]
|
|
pub struct ObjSymbolFlagSet(pub(crate) FlagSet<ObjSymbolFlags>);
|
|
#[derive(Debug, Clone)]
|
|
pub struct ObjSection {
|
|
pub name: String,
|
|
pub kind: ObjSectionKind,
|
|
pub address: u64,
|
|
pub size: u64,
|
|
pub data: Vec<u8>,
|
|
pub index: usize,
|
|
pub symbols: Vec<ObjSymbol>,
|
|
pub relocations: Vec<ObjReloc>,
|
|
|
|
// Diff
|
|
pub data_diff: Vec<ObjDataDiff>,
|
|
pub match_percent: f32,
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum ObjInsArg {
|
|
PpcArg(ppc750cl::Argument),
|
|
MipsArg(String),
|
|
MipsArgWithBase(String),
|
|
Reloc,
|
|
RelocWithBase,
|
|
BranchOffset(i32),
|
|
}
|
|
|
|
// TODO derive PartialEq on ppc750cl::Argument so this isn't necessary
|
|
impl PartialEq for ObjInsArg {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
match (self, other) {
|
|
(ObjInsArg::PpcArg(a), ObjInsArg::PpcArg(b)) => {
|
|
use ppc750cl::Argument;
|
|
match (a, b) {
|
|
(Argument::GPR(a), Argument::GPR(b)) => a == b,
|
|
(Argument::FPR(a), Argument::FPR(b)) => a == b,
|
|
(Argument::SR(a), Argument::SR(b)) => a == b,
|
|
(Argument::SPR(a), Argument::SPR(b)) => a == b,
|
|
(Argument::CRField(a), Argument::CRField(b)) => a == b,
|
|
(Argument::CRBit(a), Argument::CRBit(b)) => a == b,
|
|
(Argument::GQR(a), Argument::GQR(b)) => a == b,
|
|
(Argument::Uimm(a), Argument::Uimm(b)) => a == b,
|
|
(Argument::Simm(a), Argument::Simm(b)) => a == b,
|
|
(Argument::Offset(a), Argument::Offset(b)) => a == b,
|
|
(Argument::BranchDest(a), Argument::BranchDest(b)) => a == b,
|
|
(Argument::Bit(a), Argument::Bit(b)) => a == b,
|
|
(Argument::OpaqueU(a), Argument::OpaqueU(b)) => a == b,
|
|
(_, _) => false,
|
|
}
|
|
}
|
|
(ObjInsArg::MipsArg(a), ObjInsArg::MipsArg(b)) => a == b,
|
|
(ObjInsArg::MipsArgWithBase(a), ObjInsArg::MipsArgWithBase(b)) => a == b,
|
|
(ObjInsArg::Reloc, ObjInsArg::Reloc) => true,
|
|
(ObjInsArg::RelocWithBase, ObjInsArg::RelocWithBase) => true,
|
|
(ObjInsArg::BranchOffset(a), ObjInsArg::BranchOffset(b)) => a == b,
|
|
(_, _) => false,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub struct ObjInsArgDiff {
|
|
/// Incrementing index for coloring
|
|
pub idx: usize,
|
|
}
|
|
#[derive(Debug, Clone)]
|
|
pub struct ObjInsBranchFrom {
|
|
/// Source instruction indices
|
|
pub ins_idx: Vec<usize>,
|
|
/// Incrementing index for coloring
|
|
pub branch_idx: usize,
|
|
}
|
|
#[derive(Debug, Clone)]
|
|
pub struct ObjInsBranchTo {
|
|
/// Target instruction index
|
|
pub ins_idx: usize,
|
|
/// Incrementing index for coloring
|
|
pub branch_idx: usize,
|
|
}
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
|
|
pub enum ObjInsDiffKind {
|
|
#[default]
|
|
None,
|
|
OpMismatch,
|
|
ArgMismatch,
|
|
Replace,
|
|
Delete,
|
|
Insert,
|
|
}
|
|
#[derive(Debug, Clone)]
|
|
pub struct ObjIns {
|
|
pub address: u32,
|
|
pub code: u32,
|
|
pub op: u8,
|
|
pub mnemonic: String,
|
|
pub args: Vec<ObjInsArg>,
|
|
pub reloc: Option<ObjReloc>,
|
|
pub branch_dest: Option<u32>,
|
|
/// Line info
|
|
pub line: Option<u32>,
|
|
/// Original (unsimplified) instruction
|
|
pub orig: Option<String>,
|
|
}
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct ObjInsDiff {
|
|
pub ins: Option<ObjIns>,
|
|
/// Diff kind
|
|
pub kind: ObjInsDiffKind,
|
|
/// Branches from instruction
|
|
pub branch_from: Option<ObjInsBranchFrom>,
|
|
/// Branches to instruction
|
|
pub branch_to: Option<ObjInsBranchTo>,
|
|
/// Arg diffs
|
|
pub arg_diff: Vec<Option<ObjInsArgDiff>>,
|
|
}
|
|
#[derive(Debug, Copy, Clone, Eq, PartialEq, Default)]
|
|
pub enum ObjDataDiffKind {
|
|
#[default]
|
|
None,
|
|
Replace,
|
|
Delete,
|
|
Insert,
|
|
}
|
|
#[derive(Debug, Clone, Default)]
|
|
pub struct ObjDataDiff {
|
|
pub data: Vec<u8>,
|
|
pub kind: ObjDataDiffKind,
|
|
pub len: usize,
|
|
pub symbol: String,
|
|
}
|
|
#[derive(Debug, Clone)]
|
|
pub struct ObjSymbol {
|
|
pub name: String,
|
|
pub demangled_name: Option<String>,
|
|
pub address: u64,
|
|
pub section_address: u64,
|
|
pub size: u64,
|
|
pub size_known: bool,
|
|
pub flags: ObjSymbolFlagSet,
|
|
pub addend: i64,
|
|
|
|
// Diff
|
|
pub diff_symbol: Option<String>,
|
|
pub instructions: Vec<ObjInsDiff>,
|
|
pub match_percent: Option<f32>,
|
|
}
|
|
#[derive(Debug, Copy, Clone)]
|
|
pub enum ObjArchitecture {
|
|
PowerPc,
|
|
Mips,
|
|
}
|
|
#[derive(Debug, Clone)]
|
|
pub struct ObjInfo {
|
|
pub architecture: ObjArchitecture,
|
|
pub path: PathBuf,
|
|
pub sections: Vec<ObjSection>,
|
|
pub common: Vec<ObjSymbol>,
|
|
pub line_info: Option<BTreeMap<u32, u32>>,
|
|
}
|
|
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
|
|
pub enum ObjRelocKind {
|
|
Absolute,
|
|
PpcAddr16Hi,
|
|
PpcAddr16Ha,
|
|
PpcAddr16Lo,
|
|
// PpcAddr32,
|
|
// PpcRel32,
|
|
// PpcAddr24,
|
|
PpcRel24,
|
|
// PpcAddr14,
|
|
PpcRel14,
|
|
PpcEmbSda21,
|
|
Mips26,
|
|
MipsHi16,
|
|
MipsLo16,
|
|
MipsGot16,
|
|
MipsCall16,
|
|
MipsGpRel16,
|
|
MipsGpRel32,
|
|
}
|
|
#[derive(Debug, Clone)]
|
|
pub struct ObjReloc {
|
|
pub kind: ObjRelocKind,
|
|
pub address: u64,
|
|
pub target: ObjSymbol,
|
|
pub target_section: Option<String>,
|
|
}
|