Skip to content

Commit 042fa9e

Browse files
committed
Move hack to resolve placeholder symbol into process_code_symbol
1 parent a51a5ac commit 042fa9e

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed

objdiff-core/src/arch/ppc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,8 @@ impl ObjArch for ObjArchPpc {
211211
// Assume that any addi instruction that references a local symbol is loading a string.
212212
// This hack is not ideal and results in tons of false positives where it will show
213213
// garbage strings (e.g. misinterpreting arrays, float literals, etc).
214-
// But there isn't much other choice as not all strings are in the @stringBase pool.
215-
// And even those that are would be missed by the target.name.starts_with("@stringBase")
216-
// hack above for fake pooled relocations, as they have an empty string placeholder for
217-
// the target symbol name.
214+
// But not all strings are in the @stringBase pool, so the condition above that checks
215+
// the target symbol name would miss some.
218216
Some(DataType::String)
219217
} else {
220218
None

objdiff-core/src/diff/code.rs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
DiffObjConfig, ObjInsArgDiff, ObjInsBranchFrom, ObjInsBranchTo, ObjInsDiff, ObjInsDiffKind,
1010
ObjSymbolDiff,
1111
},
12-
obj::{ObjInfo, ObjInsArg, ObjReloc, ObjSymbolFlags, SymbolRef},
12+
obj::{ObjInfo, ObjInsArg, ObjReloc, ObjSection, ObjSymbol, ObjSymbolFlags, SymbolRef},
1313
};
1414

1515
pub fn process_code_symbol(
@@ -21,14 +21,30 @@ pub fn process_code_symbol(
2121
let section = section.ok_or_else(|| anyhow!("Code symbol section not found"))?;
2222
let code = &section.data
2323
[symbol.section_address as usize..(symbol.section_address + symbol.size) as usize];
24-
obj.arch.process_code(
24+
let mut res = obj.arch.process_code(
2525
symbol.address,
2626
code,
2727
section.orig_index,
2828
&section.relocations,
2929
&section.line_info,
3030
config,
31-
)
31+
)?;
32+
33+
for inst in res.insts.iter_mut() {
34+
if let Some(reloc) = &mut inst.fake_pool_reloc {
35+
if reloc.target.size == 0 && reloc.target.name.is_empty() {
36+
// Fake target symbol we added as a placeholder. We need to find the real one.
37+
if let Some(real_target) =
38+
find_symbol_matching_fake_symbol_in_sections(&reloc.target, &obj.sections)
39+
{
40+
reloc.addend = (reloc.target.address - real_target.address) as i64;
41+
reloc.target = real_target;
42+
}
43+
}
44+
}
45+
}
46+
47+
Ok(res)
3248
}
3349

3450
pub fn no_diff_code(out: &ProcessCodeResult, symbol_ref: SymbolRef) -> Result<ObjSymbolDiff> {
@@ -369,3 +385,16 @@ fn compare_ins(
369385
}
370386
Ok(result)
371387
}
388+
389+
fn find_symbol_matching_fake_symbol_in_sections(
390+
fake_symbol: &ObjSymbol,
391+
sections: &[ObjSection],
392+
) -> Option<ObjSymbol> {
393+
let orig_section_index = fake_symbol.orig_section_index?;
394+
let section = sections.iter().find(|s| s.orig_index == orig_section_index)?;
395+
let real_symbol = section
396+
.symbols
397+
.iter()
398+
.find(|s| s.size > 0 && (s.address..s.address + s.size).contains(&fake_symbol.address))?;
399+
Some(real_symbol.clone())
400+
}

objdiff-gui/src/views/function_diff.rs

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,6 @@ impl FunctionViewState {
7474
}
7575
}
7676

77-
fn find_symbol_matching_fake_symbol_in_sections(
78-
fake_symbol: &ObjSymbol,
79-
sections: &[ObjSection],
80-
) -> Option<ObjSymbol> {
81-
let orig_section_index = fake_symbol.orig_section_index?;
82-
let section = sections.iter().find(|s| s.orig_index == orig_section_index)?;
83-
let real_symbol = section
84-
.symbols
85-
.iter()
86-
.find(|s| s.size > 0 && (s.address..s.address + s.size).contains(&fake_symbol.address))?;
87-
Some(real_symbol.clone())
88-
}
89-
9077
fn ins_hover_ui(
9178
ui: &mut egui::Ui,
9279
obj: &ObjInfo,
@@ -132,29 +119,17 @@ fn ins_hover_ui(
132119
}
133120

134121
if let Some(reloc) = ins.reloc.as_ref().or(ins.fake_pool_reloc.as_ref()) {
135-
let mut target = reloc.target.clone();
136-
let mut addend = reloc.addend;
137-
if target.size == 0 && target.name.is_empty() {
138-
// Fake target symbol we added as a placeholder. We need to find the real one.
139-
if let Some(real_target) =
140-
find_symbol_matching_fake_symbol_in_sections(&target, &obj.sections)
141-
{
142-
target = real_target;
143-
addend = (reloc.target.address - target.address) as i64;
144-
}
145-
}
146-
147122
ui.label(format!("Relocation type: {}", obj.arch.display_reloc(reloc.flags)));
148-
let addend_str = match addend.cmp(&0i64) {
149-
Ordering::Greater => format!("+{:x}", addend),
150-
Ordering::Less => format!("-{:x}", -addend),
123+
let addend_str = match reloc.addend.cmp(&0i64) {
124+
Ordering::Greater => format!("+{:x}", reloc.addend),
125+
Ordering::Less => format!("-{:x}", -reloc.addend),
151126
_ => "".to_string(),
152127
};
153128
ui.colored_label(
154129
appearance.highlight_color,
155-
format!("Name: {}{}", target.name, addend_str),
130+
format!("Name: {}{}", reloc.target.name, addend_str),
156131
);
157-
if let Some(orig_section_index) = target.orig_section_index {
132+
if let Some(orig_section_index) = reloc.target.orig_section_index {
158133
if let Some(section) =
159134
obj.sections.iter().find(|s| s.orig_index == orig_section_index)
160135
{
@@ -165,12 +140,15 @@ fn ins_hover_ui(
165140
}
166141
ui.colored_label(
167142
appearance.highlight_color,
168-
format!("Address: {:x}{}", target.address, addend_str),
143+
format!("Address: {:x}{}", reloc.target.address, addend_str),
144+
);
145+
ui.colored_label(
146+
appearance.highlight_color,
147+
format!("Size: {:x}", reloc.target.size),
169148
);
170-
ui.colored_label(appearance.highlight_color, format!("Size: {:x}", target.size));
171-
if addend >= 0 && target.bytes.len() > addend as usize {
149+
if reloc.addend >= 0 && reloc.target.bytes.len() > reloc.addend as usize {
172150
if let Some(s) = obj.arch.guess_data_type(ins).and_then(|ty| {
173-
obj.arch.display_data_type(ty, &target.bytes[addend as usize..])
151+
obj.arch.display_data_type(ty, &reloc.target.bytes[reloc.addend as usize..])
174152
}) {
175153
ui.colored_label(appearance.highlight_color, s);
176154
}

0 commit comments

Comments
 (0)