From 34e4513c696487c7660c643153b3f2f8bbb162af Mon Sep 17 00:00:00 2001 From: LagoLunatic Date: Wed, 6 Aug 2025 16:09:18 -0400 Subject: [PATCH] Fix data section not showing when there is no section on the other side --- objdiff-core/src/diff/data.rs | 26 ++++++++ objdiff-core/src/diff/mod.rs | 120 +++++++++++++++++++++------------- 2 files changed, 102 insertions(+), 44 deletions(-) diff --git a/objdiff-core/src/diff/data.rs b/objdiff-core/src/diff/data.rs index a47474f..ecf87f2 100644 --- a/objdiff-core/src/diff/data.rs +++ b/objdiff-core/src/diff/data.rs @@ -127,6 +127,28 @@ fn diff_data_relocs_for_range<'left, 'right>( diffs } +pub fn no_diff_data_section(obj: &Object, section_idx: usize) -> Result { + let section = &obj.sections[section_idx]; + let len = section.data.len(); + let data = §ion.data[0..len]; + + let data_diff = + vec![DataDiff { data: data.to_vec(), kind: DataDiffKind::None, len, ..Default::default() }]; + + let mut reloc_diffs = Vec::new(); + for reloc in section.relocations.iter() { + let reloc_len = obj.arch.data_reloc_size(reloc.flags); + let range = reloc.address as usize..reloc.address as usize + reloc_len; + reloc_diffs.push(DataRelocationDiff { + reloc: reloc.clone(), + kind: DataDiffKind::None, + range, + }); + } + + Ok(SectionDiff { match_percent: Some(0.0), data_diff, reloc_diff: reloc_diffs }) +} + /// Compare the data sections of two object files. pub fn diff_data_section( left_obj: &Object, @@ -415,6 +437,10 @@ pub fn diff_generic_section( )) } +pub fn no_diff_bss_section() -> Result { + Ok(SectionDiff { match_percent: Some(0.0), data_diff: vec![], reloc_diff: vec![] }) +} + /// Compare the addresses and sizes of each symbol in the BSS sections. pub fn diff_bss_section( left_obj: &Object, diff --git a/objdiff-core/src/diff/mod.rs b/objdiff-core/src/diff/mod.rs index 870db9c..dd10148 100644 --- a/objdiff-core/src/diff/mod.rs +++ b/objdiff-core/src/diff/mod.rs @@ -13,7 +13,7 @@ use crate::{ code::{diff_code, no_diff_code}, data::{ diff_bss_section, diff_bss_symbol, diff_data_section, diff_data_symbol, - diff_generic_section, + diff_generic_section, no_diff_bss_section, no_diff_data_section, }, }, obj::{InstructionRef, Object, Relocation, SectionKind, Symbol, SymbolFlag}, @@ -288,52 +288,84 @@ pub fn diff_objs( } for section_match in section_matches { - if let SectionMatch { - left: Some(left_section_idx), - right: Some(right_section_idx), - section_kind, - } = section_match - { - let (left_obj, left_out) = left.as_mut().unwrap(); - let (right_obj, right_out) = right.as_mut().unwrap(); - match section_kind { - SectionKind::Code => { - let (left_diff, right_diff) = diff_generic_section( - left_obj, - right_obj, - left_out, - right_out, - left_section_idx, - right_section_idx, - )?; - left_out.sections[left_section_idx] = left_diff; - right_out.sections[right_section_idx] = right_diff; + match section_match { + SectionMatch { + left: Some(left_section_idx), + right: Some(right_section_idx), + section_kind, + } => { + let (left_obj, left_out) = left.as_mut().unwrap(); + let (right_obj, right_out) = right.as_mut().unwrap(); + match section_kind { + SectionKind::Code => { + let (left_diff, right_diff) = diff_generic_section( + left_obj, + right_obj, + left_out, + right_out, + left_section_idx, + right_section_idx, + )?; + left_out.sections[left_section_idx] = left_diff; + right_out.sections[right_section_idx] = right_diff; + } + SectionKind::Data => { + let (left_diff, right_diff) = diff_data_section( + left_obj, + right_obj, + left_out, + right_out, + left_section_idx, + right_section_idx, + )?; + left_out.sections[left_section_idx] = left_diff; + right_out.sections[right_section_idx] = right_diff; + } + SectionKind::Bss | SectionKind::Common => { + let (left_diff, right_diff) = diff_bss_section( + left_obj, + right_obj, + left_out, + right_out, + left_section_idx, + right_section_idx, + )?; + left_out.sections[left_section_idx] = left_diff; + right_out.sections[right_section_idx] = right_diff; + } + SectionKind::Unknown => unreachable!(), } - SectionKind::Data => { - let (left_diff, right_diff) = diff_data_section( - left_obj, - right_obj, - left_out, - right_out, - left_section_idx, - right_section_idx, - )?; - left_out.sections[left_section_idx] = left_diff; - right_out.sections[right_section_idx] = right_diff; + } + SectionMatch { left: Some(left_section_idx), right: None, section_kind } => { + let (left_obj, left_out) = left.as_mut().unwrap(); + match section_kind { + SectionKind::Code => {} + SectionKind::Data => { + left_out.sections[left_section_idx] = + no_diff_data_section(left_obj, left_section_idx)?; + } + SectionKind::Bss | SectionKind::Common => { + left_out.sections[left_section_idx] = no_diff_bss_section()?; + } + SectionKind::Unknown => unreachable!(), } - SectionKind::Bss | SectionKind::Common => { - let (left_diff, right_diff) = diff_bss_section( - left_obj, - right_obj, - left_out, - right_out, - left_section_idx, - right_section_idx, - )?; - left_out.sections[left_section_idx] = left_diff; - right_out.sections[right_section_idx] = right_diff; + } + SectionMatch { left: None, right: Some(right_section_idx), section_kind } => { + let (right_obj, right_out) = right.as_mut().unwrap(); + match section_kind { + SectionKind::Code => {} + SectionKind::Data => { + right_out.sections[right_section_idx] = + no_diff_data_section(right_obj, right_section_idx)?; + } + SectionKind::Bss | SectionKind::Common => { + right_out.sections[right_section_idx] = no_diff_bss_section()?; + } + SectionKind::Unknown => unreachable!(), } - SectionKind::Unknown => unreachable!(), + } + SectionMatch { left: None, right: None, .. } => { + // Should not happen } } }