Fix section ordering with many same-named sections

Before, this was comparing, for example, `.text-2`
with `.text-10` with standard string comparison,
yielding `.text-10` before `.text-2`.

Instead, this simply uses a stable sort by name,
which preserves the relative ordering of sections.
This commit is contained in:
Luke Street 2025-03-10 21:51:54 -06:00
parent ffb38d1bb0
commit 5898d7aebf
4 changed files with 227 additions and 2 deletions

View File

@ -701,7 +701,7 @@ pub fn display_sections(
}); });
} }
} }
sections.sort_by(|a, b| a.id.cmp(&b.id)); sections.sort_by(|a, b| a.name.cmp(&b.name));
sections sections
} }

View File

@ -1,4 +1,4 @@
use objdiff_core::{diff, obj}; use objdiff_core::{diff, diff::display::SymbolFilter, obj};
mod common; mod common;
@ -40,3 +40,18 @@ fn read_x86_64() {
let output = common::display_diff(&obj, &diff, symbol_idx, &diff_config); let output = common::display_diff(&obj, &diff, symbol_idx, &diff_config);
insta::assert_snapshot!(output); insta::assert_snapshot!(output);
} }
#[test]
#[cfg(feature = "x86")]
fn display_section_ordering() {
let diff_config = diff::DiffObjConfig::default();
let obj = obj::read::parse(include_object!("data/x86/basenode.obj"), &diff_config).unwrap();
let obj_diff =
diff::diff_objs(Some(&obj), None, None, &diff_config, &diff::MappingConfig::default())
.unwrap()
.left
.unwrap();
let section_display =
diff::display::display_sections(&obj, &obj_diff, SymbolFilter::None, false, false, false);
insta::assert_debug_snapshot!(section_display);
}

Binary file not shown.

View File

@ -0,0 +1,210 @@
---
source: objdiff-core/tests/arch_x86.rs
expression: section_display
---
[
SectionDisplay {
id: ".text-0",
name: ".text",
size: 47,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 28,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-1",
name: ".text",
size: 65,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 29,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-2",
name: ".text",
size: 5,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 30,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-3",
name: ".text",
size: 141,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 31,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-4",
name: ".text",
size: 120,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 34,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-5",
name: ".text",
size: 378,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 37,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-6",
name: ".text",
size: 130,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 38,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-7",
name: ".text",
size: 123,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 39,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-8",
name: ".text",
size: 70,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 40,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-9",
name: ".text",
size: 90,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 41,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-10",
name: ".text",
size: 82,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 42,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-11",
name: ".text",
size: 336,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 43,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-12",
name: ".text",
size: 193,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 50,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-13",
name: ".text",
size: 544,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 53,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-14",
name: ".text",
size: 250,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 56,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-15",
name: ".text",
size: 89,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 57,
is_mapping_symbol: false,
},
],
},
SectionDisplay {
id: ".text-16",
name: ".text",
size: 119,
match_percent: None,
symbols: [
SectionDisplaySymbol {
symbol: 60,
is_mapping_symbol: false,
},
],
},
]