Skip to content

Commit

Permalink
Implement diffing relocations within data sections (#154)
Browse files Browse the repository at this point in the history
* Data view: Show data bytes with differing relocations as a diff

* Data view: Show differing relocations on hover

* Symbol list view: Adjust symbol/section match %s when relocations differ

* Improve data reloc diffing logic

* Don't make reloc diffs cause bytes to show as red or green

* Properly detect byte size of each relocation

* Data view: Add context menu for copying relocation target symbols

* Also show already-matching relocations on hover/right click

* Change font color for nonmatching relocs on hover
  • Loading branch information
LagoLunatic authored Jan 18, 2025
1 parent 2876be3 commit a4fdb61
Show file tree
Hide file tree
Showing 10 changed files with 426 additions and 24 deletions.
13 changes: 13 additions & 0 deletions objdiff-core/src/arch/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,19 @@ impl ObjArch for ObjArchArm {
fn display_reloc(&self, flags: RelocationFlags) -> Cow<'static, str> {
Cow::Owned(format!("<{flags:?}>"))
}

fn get_reloc_byte_size(&self, flags: RelocationFlags) -> usize {
match flags {
RelocationFlags::Elf { r_type } => match r_type {
elf::R_ARM_ABS32 => 4,
elf::R_ARM_REL32 => 4,
elf::R_ARM_ABS16 => 2,
elf::R_ARM_ABS8 => 1,
_ => 1,
},
_ => 1,
}
}
}

#[derive(Clone, Copy, Debug)]
Expand Down
15 changes: 15 additions & 0 deletions objdiff-core/src/arch/arm64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ impl ObjArch for ObjArchArm64 {
_ => Cow::Owned(format!("<{flags:?}>")),
}
}

fn get_reloc_byte_size(&self, flags: RelocationFlags) -> usize {
match flags {
RelocationFlags::Elf { r_type } => match r_type {
elf::R_AARCH64_ABS64 => 8,
elf::R_AARCH64_ABS32 => 4,
elf::R_AARCH64_ABS16 => 2,
elf::R_AARCH64_PREL64 => 8,
elf::R_AARCH64_PREL32 => 4,
elf::R_AARCH64_PREL16 => 2,
_ => 1,
},
_ => 1,
}
}
}

struct DisplayCtx<'a> {
Expand Down
11 changes: 11 additions & 0 deletions objdiff-core/src/arch/mips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ impl ObjArch for ObjArchMips {
_ => Cow::Owned(format!("<{flags:?}>")),
}
}

fn get_reloc_byte_size(&self, flags: RelocationFlags) -> usize {
match flags {
RelocationFlags::Elf { r_type } => match r_type {
elf::R_MIPS_16 => 2,
elf::R_MIPS_32 => 4,
_ => 1,
},
_ => 1,
}
}
}

fn push_reloc(args: &mut Vec<ObjInsArg>, reloc: &ObjReloc) -> Result<()> {
Expand Down
2 changes: 2 additions & 0 deletions objdiff-core/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ pub trait ObjArch: Send + Sync {

fn display_reloc(&self, flags: RelocationFlags) -> Cow<'static, str>;

fn get_reloc_byte_size(&self, flags: RelocationFlags) -> usize;

fn symbol_address(&self, symbol: &Symbol) -> u64 { symbol.address() }

fn guess_data_type(&self, _instruction: &ObjIns) -> Option<DataType> { None }
Expand Down
11 changes: 11 additions & 0 deletions objdiff-core/src/arch/ppc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,17 @@ impl ObjArch for ObjArchPpc {
}
}

fn get_reloc_byte_size(&self, flags: RelocationFlags) -> usize {
match flags {
RelocationFlags::Elf { r_type } => match r_type {
elf::R_PPC_ADDR32 => 4,
elf::R_PPC_UADDR32 => 4,
_ => 1,
},
_ => 1,
}
}

fn guess_data_type(&self, instruction: &ObjIns) -> Option<super::DataType> {
if instruction.reloc.as_ref().is_some_and(|r| r.target.name.starts_with("@stringBase")) {
return Some(DataType::String);
Expand Down
13 changes: 13 additions & 0 deletions objdiff-core/src/arch/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ impl ObjArch for ObjArchX86 {
_ => Cow::Owned(format!("<{flags:?}>")),
}
}

fn get_reloc_byte_size(&self, flags: RelocationFlags) -> usize {
match flags {
RelocationFlags::Coff { typ } => match typ {
pe::IMAGE_REL_I386_DIR16 => 2,
pe::IMAGE_REL_I386_REL16 => 2,
pe::IMAGE_REL_I386_DIR32 => 4,
pe::IMAGE_REL_I386_REL32 => 4,
_ => 1,
},
_ => 1,
}
}
}

fn replace_arg(
Expand Down
2 changes: 1 addition & 1 deletion objdiff-core/src/diff/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ fn address_eq(left: &ObjReloc, right: &ObjReloc) -> bool {
left.target.address as i64 + left.addend == right.target.address as i64 + right.addend
}

fn section_name_eq(
pub fn section_name_eq(
left_obj: &ObjInfo,
right_obj: &ObjInfo,
left_orig_section_index: usize,
Expand Down
Loading

0 comments on commit a4fdb61

Please sign in to comment.