Skip to content

Commit 34e4513

Browse files
committed
Fix data section not showing when there is no section on the other side
1 parent a015971 commit 34e4513

File tree

2 files changed

+102
-44
lines changed

2 files changed

+102
-44
lines changed

objdiff-core/src/diff/data.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,28 @@ fn diff_data_relocs_for_range<'left, 'right>(
127127
diffs
128128
}
129129

130+
pub fn no_diff_data_section(obj: &Object, section_idx: usize) -> Result<SectionDiff> {
131+
let section = &obj.sections[section_idx];
132+
let len = section.data.len();
133+
let data = &section.data[0..len];
134+
135+
let data_diff =
136+
vec![DataDiff { data: data.to_vec(), kind: DataDiffKind::None, len, ..Default::default() }];
137+
138+
let mut reloc_diffs = Vec::new();
139+
for reloc in section.relocations.iter() {
140+
let reloc_len = obj.arch.data_reloc_size(reloc.flags);
141+
let range = reloc.address as usize..reloc.address as usize + reloc_len;
142+
reloc_diffs.push(DataRelocationDiff {
143+
reloc: reloc.clone(),
144+
kind: DataDiffKind::None,
145+
range,
146+
});
147+
}
148+
149+
Ok(SectionDiff { match_percent: Some(0.0), data_diff, reloc_diff: reloc_diffs })
150+
}
151+
130152
/// Compare the data sections of two object files.
131153
pub fn diff_data_section(
132154
left_obj: &Object,
@@ -415,6 +437,10 @@ pub fn diff_generic_section(
415437
))
416438
}
417439

440+
pub fn no_diff_bss_section() -> Result<SectionDiff> {
441+
Ok(SectionDiff { match_percent: Some(0.0), data_diff: vec![], reloc_diff: vec![] })
442+
}
443+
418444
/// Compare the addresses and sizes of each symbol in the BSS sections.
419445
pub fn diff_bss_section(
420446
left_obj: &Object,

objdiff-core/src/diff/mod.rs

Lines changed: 76 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
code::{diff_code, no_diff_code},
1414
data::{
1515
diff_bss_section, diff_bss_symbol, diff_data_section, diff_data_symbol,
16-
diff_generic_section,
16+
diff_generic_section, no_diff_bss_section, no_diff_data_section,
1717
},
1818
},
1919
obj::{InstructionRef, Object, Relocation, SectionKind, Symbol, SymbolFlag},
@@ -288,52 +288,84 @@ pub fn diff_objs(
288288
}
289289

290290
for section_match in section_matches {
291-
if let SectionMatch {
292-
left: Some(left_section_idx),
293-
right: Some(right_section_idx),
294-
section_kind,
295-
} = section_match
296-
{
297-
let (left_obj, left_out) = left.as_mut().unwrap();
298-
let (right_obj, right_out) = right.as_mut().unwrap();
299-
match section_kind {
300-
SectionKind::Code => {
301-
let (left_diff, right_diff) = diff_generic_section(
302-
left_obj,
303-
right_obj,
304-
left_out,
305-
right_out,
306-
left_section_idx,
307-
right_section_idx,
308-
)?;
309-
left_out.sections[left_section_idx] = left_diff;
310-
right_out.sections[right_section_idx] = right_diff;
291+
match section_match {
292+
SectionMatch {
293+
left: Some(left_section_idx),
294+
right: Some(right_section_idx),
295+
section_kind,
296+
} => {
297+
let (left_obj, left_out) = left.as_mut().unwrap();
298+
let (right_obj, right_out) = right.as_mut().unwrap();
299+
match section_kind {
300+
SectionKind::Code => {
301+
let (left_diff, right_diff) = diff_generic_section(
302+
left_obj,
303+
right_obj,
304+
left_out,
305+
right_out,
306+
left_section_idx,
307+
right_section_idx,
308+
)?;
309+
left_out.sections[left_section_idx] = left_diff;
310+
right_out.sections[right_section_idx] = right_diff;
311+
}
312+
SectionKind::Data => {
313+
let (left_diff, right_diff) = diff_data_section(
314+
left_obj,
315+
right_obj,
316+
left_out,
317+
right_out,
318+
left_section_idx,
319+
right_section_idx,
320+
)?;
321+
left_out.sections[left_section_idx] = left_diff;
322+
right_out.sections[right_section_idx] = right_diff;
323+
}
324+
SectionKind::Bss | SectionKind::Common => {
325+
let (left_diff, right_diff) = diff_bss_section(
326+
left_obj,
327+
right_obj,
328+
left_out,
329+
right_out,
330+
left_section_idx,
331+
right_section_idx,
332+
)?;
333+
left_out.sections[left_section_idx] = left_diff;
334+
right_out.sections[right_section_idx] = right_diff;
335+
}
336+
SectionKind::Unknown => unreachable!(),
311337
}
312-
SectionKind::Data => {
313-
let (left_diff, right_diff) = diff_data_section(
314-
left_obj,
315-
right_obj,
316-
left_out,
317-
right_out,
318-
left_section_idx,
319-
right_section_idx,
320-
)?;
321-
left_out.sections[left_section_idx] = left_diff;
322-
right_out.sections[right_section_idx] = right_diff;
338+
}
339+
SectionMatch { left: Some(left_section_idx), right: None, section_kind } => {
340+
let (left_obj, left_out) = left.as_mut().unwrap();
341+
match section_kind {
342+
SectionKind::Code => {}
343+
SectionKind::Data => {
344+
left_out.sections[left_section_idx] =
345+
no_diff_data_section(left_obj, left_section_idx)?;
346+
}
347+
SectionKind::Bss | SectionKind::Common => {
348+
left_out.sections[left_section_idx] = no_diff_bss_section()?;
349+
}
350+
SectionKind::Unknown => unreachable!(),
323351
}
324-
SectionKind::Bss | SectionKind::Common => {
325-
let (left_diff, right_diff) = diff_bss_section(
326-
left_obj,
327-
right_obj,
328-
left_out,
329-
right_out,
330-
left_section_idx,
331-
right_section_idx,
332-
)?;
333-
left_out.sections[left_section_idx] = left_diff;
334-
right_out.sections[right_section_idx] = right_diff;
352+
}
353+
SectionMatch { left: None, right: Some(right_section_idx), section_kind } => {
354+
let (right_obj, right_out) = right.as_mut().unwrap();
355+
match section_kind {
356+
SectionKind::Code => {}
357+
SectionKind::Data => {
358+
right_out.sections[right_section_idx] =
359+
no_diff_data_section(right_obj, right_section_idx)?;
360+
}
361+
SectionKind::Bss | SectionKind::Common => {
362+
right_out.sections[right_section_idx] = no_diff_bss_section()?;
363+
}
364+
SectionKind::Unknown => unreachable!(),
335365
}
336-
SectionKind::Unknown => unreachable!(),
366+
}
367+
SectionMatch { left: None, right: None, .. } => {
368+
// Should not happen
337369
}
338370
}
339371
}

0 commit comments

Comments
 (0)