Skip to content

Commit

Permalink
Option to relax relocation diffs
Browse files Browse the repository at this point in the history
Ignores differences in relocation targets. (Address, name, etc)

Resolves #34
  • Loading branch information
encounter committed Jan 22, 2024
1 parent 02f521a commit e88a58b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 9 deletions.
12 changes: 12 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub struct AppConfig {
pub code_alg: DiffAlg,
#[serde(default)]
pub data_alg: DiffAlg,
#[serde(default)]
pub relax_reloc_diffs: bool,

#[serde(skip)]
pub objects: Vec<ProjectObject>,
Expand Down Expand Up @@ -147,6 +149,7 @@ impl Default for AppConfig {
recent_projects: vec![],
code_alg: Default::default(),
data_alg: Default::default(),
relax_reloc_diffs: false,
objects: vec![],
object_nodes: vec![],
watcher_change: false,
Expand Down Expand Up @@ -492,6 +495,15 @@ impl eframe::App for App {
&mut diff_state.symbol_state.show_hidden_symbols,
"Show hidden symbols",
);
if ui
.checkbox(&mut config.relax_reloc_diffs, "Relax relocation diffs")
.on_hover_text(
"Ignores differences in relocation targets. (Address, name, etc)",
)
.changed()
{
config.queue_reload = true;
}
});
});
});
Expand Down
23 changes: 17 additions & 6 deletions src/diff/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use similar::{capture_diff_slices_deadline, Algorithm};
use crate::{
diff::{
editops::{editops_find, LevEditType},
DiffAlg, ProcessCodeResult,
DiffAlg, DiffObjConfig, ProcessCodeResult,
},
obj::{
mips, ppc, ObjArchitecture, ObjInfo, ObjInsArg, ObjInsArgDiff, ObjInsBranchFrom,
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn no_diff_code(

#[allow(clippy::too_many_arguments)]
pub fn diff_code(
alg: DiffAlg,
config: &DiffObjConfig,
arch: ObjArchitecture,
left_data: &[u8],
right_data: &[u8],
Expand Down Expand Up @@ -89,7 +89,7 @@ pub fn diff_code(

let mut left_diff = Vec::<ObjInsDiff>::new();
let mut right_diff = Vec::<ObjInsDiff>::new();
match alg {
match config.code_alg {
DiffAlg::Levenshtein => {
diff_instructions_lev(
&mut left_diff,
Expand Down Expand Up @@ -134,7 +134,7 @@ pub fn diff_code(

let mut diff_state = InsDiffState::default();
for (left, right) in left_diff.iter_mut().zip(right_diff.iter_mut()) {
let result = compare_ins(left, right, &mut diff_state)?;
let result = compare_ins(config, left, right, &mut diff_state)?;
left.kind = result.kind;
right.kind = result.kind;
left.arg_diff = result.left_args_diff;
Expand Down Expand Up @@ -322,13 +322,20 @@ fn address_eq(left: &ObjSymbol, right: &ObjSymbol) -> bool {
left.address as i64 + left.addend == right.address as i64 + right.addend
}

fn reloc_eq(left_reloc: Option<&ObjReloc>, right_reloc: Option<&ObjReloc>) -> bool {
fn reloc_eq(
config: &DiffObjConfig,
left_reloc: Option<&ObjReloc>,
right_reloc: Option<&ObjReloc>,
) -> bool {
let (Some(left), Some(right)) = (left_reloc, right_reloc) else {
return false;
};
if left.kind != right.kind {
return false;
}
if config.relax_reloc_diffs {
return true;
}

let name_matches = left.target.name == right.target.name;
match (&left.target_section, &right.target_section) {
Expand All @@ -346,6 +353,7 @@ fn reloc_eq(left_reloc: Option<&ObjReloc>, right_reloc: Option<&ObjReloc>) -> bo
}

fn arg_eq(
config: &DiffObjConfig,
left: &ObjInsArg,
right: &ObjInsArg,
left_diff: &ObjInsDiff,
Expand All @@ -359,13 +367,15 @@ fn arg_eq(
ObjInsArg::Reloc => {
matches!(right, ObjInsArg::Reloc)
&& reloc_eq(
config,
left_diff.ins.as_ref().and_then(|i| i.reloc.as_ref()),
right_diff.ins.as_ref().and_then(|i| i.reloc.as_ref()),
)
}
ObjInsArg::RelocWithBase => {
matches!(right, ObjInsArg::RelocWithBase)
&& reloc_eq(
config,
left_diff.ins.as_ref().and_then(|i| i.reloc.as_ref()),
right_diff.ins.as_ref().and_then(|i| i.reloc.as_ref()),
)
Expand Down Expand Up @@ -398,6 +408,7 @@ struct InsDiffResult {
}

fn compare_ins(
config: &DiffObjConfig,
left: &ObjInsDiff,
right: &ObjInsDiff,
state: &mut InsDiffState,
Expand All @@ -416,7 +427,7 @@ fn compare_ins(
state.diff_count += 1;
}
for (a, b) in left_ins.args.iter().zip(&right_ins.args) {
if arg_eq(a, b, left, right) {
if arg_eq(config, a, b, left, right) {
result.left_args_diff.push(None);
result.right_args_diff.push(None);
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/diff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum DiffAlg {
pub struct DiffObjConfig {
pub code_alg: DiffAlg,
pub data_alg: DiffAlg,
pub relax_reloc_diffs: bool,
}

pub struct ProcessCodeResult {
Expand All @@ -51,7 +52,7 @@ pub fn diff_objs(
left_symbol.diff_symbol = Some(right_symbol.name.clone());
right_symbol.diff_symbol = Some(left_symbol.name.clone());
diff_code(
config.code_alg,
config,
left.architecture,
&left_section.data,
&right_section.data,
Expand Down
8 changes: 7 additions & 1 deletion src/jobs/objdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct ObjDiffConfig {
pub selected_wsl_distro: Option<String>,
pub code_alg: DiffAlg,
pub data_alg: DiffAlg,
pub relax_reloc_diffs: bool,
}

impl ObjDiffConfig {
Expand All @@ -55,6 +56,7 @@ impl ObjDiffConfig {
selected_wsl_distro: config.selected_wsl_distro.clone(),
code_alg: config.code_alg,
data_alg: config.data_alg,
relax_reloc_diffs: config.relax_reloc_diffs,
}
}
}
Expand Down Expand Up @@ -225,7 +227,11 @@ fn run_build(
};

update_status(context, "Performing diff".to_string(), 4, total, &cancel)?;
let diff_config = DiffObjConfig { code_alg: config.code_alg, data_alg: config.data_alg };
let diff_config = DiffObjConfig {
code_alg: config.code_alg,
data_alg: config.data_alg,
relax_reloc_diffs: config.relax_reloc_diffs,
};
diff_objs(&diff_config, first_obj.as_mut(), second_obj.as_mut())?;

update_status(context, "Complete".to_string(), total, total, &cancel)?;
Expand Down
3 changes: 2 additions & 1 deletion src/views/symbol_diff.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::mem::take;

use egui::{
text::LayoutJob, Align, CollapsingHeader, Color32, Layout, ScrollArea, SelectableLabel,
text::LayoutJob, Align, CollapsingHeader, Color32, Id, Layout, ScrollArea, SelectableLabel,
TextEdit, Ui, Vec2, Widget,
};
use egui_extras::{Size, StripBuilder};
Expand Down Expand Up @@ -234,6 +234,7 @@ fn symbol_list_ui(

for section in &obj.sections {
CollapsingHeader::new(format!("{} ({:x})", section.name, section.size))
.id_source(Id::new(section.name.clone()).with(section.index))
.default_open(true)
.show(ui, |ui| {
if section.kind == ObjSectionKind::Code && state.reverse_fn_order {
Expand Down

0 comments on commit e88a58b

Please sign in to comment.