Files
objdiff/objdiff-core/src/obj/mod.rs
T

180 lines
4.7 KiB
Rust
Raw Normal View History

2024-03-16 23:30:27 -06:00
pub mod read;
pub mod split_meta;
2022-09-11 13:52:55 -04:00
use std::{borrow::Cow, collections::BTreeMap, fmt, path::PathBuf};
2022-09-08 17:19:20 -04:00
2023-10-07 14:48:34 -04:00
use filetime::FileTime;
2022-09-08 17:19:20 -04:00
use flagset::{flags, FlagSet};
use object::RelocationFlags;
use split_meta::SplitMeta;
2022-09-08 17:19:20 -04:00
use crate::{arch::ObjArch, util::ReallySigned};
2022-09-08 17:19:20 -04:00
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum ObjSectionKind {
Code,
Data,
Bss,
}
flags! {
pub enum ObjSymbolFlags: u8 {
Global,
Local,
Weak,
Common,
Hidden,
2024-09-09 19:43:10 -06:00
/// Has extra data associated with the symbol
/// (e.g. exception table entry)
HasExtra,
2022-09-08 17:19:20 -04:00
}
}
#[derive(Debug, Copy, Clone, Default)]
pub struct ObjSymbolFlagSet(pub FlagSet<ObjSymbolFlags>);
2022-09-08 17:19:20 -04:00
#[derive(Debug, Clone)]
pub struct ObjSection {
pub name: String,
pub kind: ObjSectionKind,
pub address: u64,
pub size: u64,
pub data: Vec<u8>,
pub orig_index: usize,
2022-09-08 17:19:20 -04:00
pub symbols: Vec<ObjSymbol>,
pub relocations: Vec<ObjReloc>,
pub virtual_address: Option<u64>,
/// Line number info (.line or .debug_line section)
2024-08-20 21:40:32 -06:00
pub line_info: BTreeMap<u64, u32>,
2022-09-08 17:19:20 -04:00
}
2023-10-05 23:40:45 -04:00
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ObjInsArgValue {
2024-03-16 23:30:27 -06:00
Signed(i64),
Unsigned(u64),
Opaque(Cow<'static, str>),
}
impl ObjInsArgValue {
pub fn loose_eq(&self, other: &ObjInsArgValue) -> bool {
match (self, other) {
(ObjInsArgValue::Signed(a), ObjInsArgValue::Signed(b)) => a == b,
(ObjInsArgValue::Unsigned(a), ObjInsArgValue::Unsigned(b)) => a == b,
(ObjInsArgValue::Signed(a), ObjInsArgValue::Unsigned(b))
2024-03-16 23:30:27 -06:00
| (ObjInsArgValue::Unsigned(b), ObjInsArgValue::Signed(a)) => *a as u64 == *b,
(ObjInsArgValue::Opaque(a), ObjInsArgValue::Opaque(b)) => a == b,
_ => false,
}
}
}
impl fmt::Display for ObjInsArgValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ObjInsArgValue::Signed(v) => write!(f, "{:#x}", ReallySigned(*v)),
ObjInsArgValue::Unsigned(v) => write!(f, "{:#x}", v),
ObjInsArgValue::Opaque(v) => write!(f, "{}", v),
}
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
2022-09-08 17:19:20 -04:00
pub enum ObjInsArg {
PlainText(Cow<'static, str>),
Arg(ObjInsArgValue),
2022-09-08 17:19:20 -04:00
Reloc,
2024-03-16 23:30:27 -06:00
BranchDest(u64),
2022-09-08 17:19:20 -04:00
}
2023-10-05 23:40:45 -04:00
impl ObjInsArg {
pub fn loose_eq(&self, other: &ObjInsArg) -> bool {
match (self, other) {
(ObjInsArg::Arg(a), ObjInsArg::Arg(b)) => a.loose_eq(b),
(ObjInsArg::Reloc, ObjInsArg::Reloc) => true,
2024-03-16 23:30:27 -06:00
(ObjInsArg::BranchDest(a), ObjInsArg::BranchDest(b)) => a == b,
_ => false,
}
}
}
2022-09-08 17:19:20 -04:00
#[derive(Debug, Clone)]
pub struct ObjIns {
2024-03-16 23:30:27 -06:00
pub address: u64,
pub size: u8,
pub op: u16,
2022-09-08 17:19:20 -04:00
pub mnemonic: String,
pub args: Vec<ObjInsArg>,
pub reloc: Option<ObjReloc>,
2024-03-16 23:30:27 -06:00
pub branch_dest: Option<u64>,
/// Line number
2024-08-20 21:40:32 -06:00
pub line: Option<u32>,
/// Formatted instruction
pub formatted: String,
2023-10-05 23:40:45 -04:00
/// Original (unsimplified) instruction
pub orig: Option<String>,
2022-09-08 17:19:20 -04:00
}
2024-10-09 21:44:18 -06:00
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Default)]
pub enum ObjSymbolKind {
#[default]
Unknown,
Function,
Object,
Section,
}
2022-09-08 17:19:20 -04:00
#[derive(Debug, Clone)]
pub struct ObjSymbol {
pub name: String,
pub demangled_name: Option<String>,
pub address: u64,
2022-09-11 13:52:55 -04:00
pub section_address: u64,
2022-09-08 17:19:20 -04:00
pub size: u64,
pub size_known: bool,
2024-10-09 21:44:18 -06:00
pub kind: ObjSymbolKind,
2022-09-08 17:19:20 -04:00
pub flags: ObjSymbolFlagSet,
pub orig_section_index: Option<usize>,
2024-03-04 18:06:21 -07:00
/// Original virtual address (from .note.split section)
pub virtual_address: Option<u64>,
2024-09-09 19:43:10 -06:00
/// Original index in object symbol table
pub original_index: Option<usize>,
pub bytes: Vec<u8>,
2024-07-22 00:25:54 -04:00
}
2022-09-08 17:19:20 -04:00
pub struct ObjInfo {
pub arch: Box<dyn ObjArch>,
2024-08-20 21:40:32 -06:00
pub path: Option<PathBuf>,
pub timestamp: Option<FileTime>,
2022-09-08 17:19:20 -04:00
pub sections: Vec<ObjSection>,
/// Common BSS symbols
2022-09-08 17:19:20 -04:00
pub common: Vec<ObjSymbol>,
2024-03-04 18:06:21 -07:00
/// Split object metadata (.note.split section)
pub split_meta: Option<SplitMeta>,
2022-09-08 17:19:20 -04:00
}
2022-09-08 17:19:20 -04:00
#[derive(Debug, Clone)]
pub struct ObjReloc {
pub flags: RelocationFlags,
2022-09-08 17:19:20 -04:00
pub address: u64,
pub target: ObjSymbol,
pub addend: i64,
2022-09-08 17:19:20 -04:00
}
2024-03-18 22:56:13 -06:00
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
2024-03-18 22:56:13 -06:00
pub struct SymbolRef {
pub section_idx: usize,
pub symbol_idx: usize,
}
2024-10-28 17:54:49 -06:00
pub const SECTION_COMMON: usize = usize::MAX - 1;
2024-03-18 22:56:13 -06:00
impl ObjInfo {
pub fn section_symbol(&self, symbol_ref: SymbolRef) -> (Option<&ObjSection>, &ObjSymbol) {
2024-10-28 17:54:49 -06:00
if symbol_ref.section_idx == SECTION_COMMON {
let symbol = &self.common[symbol_ref.symbol_idx];
return (None, symbol);
}
2024-03-18 22:56:13 -06:00
let section = &self.sections[symbol_ref.section_idx];
let symbol = &section.symbols[symbol_ref.symbol_idx];
(Some(section), symbol)
2024-03-18 22:56:13 -06:00
}
}