Skip to content

Commit 9b1205d

Browse files
committed
Revert ObjArch API changes, add fake target symbol hack
Because we no longer have access to the actual symbol name via sections, guess_data_type can no longer detect the String data type for pooled references.
1 parent 507b988 commit 9b1205d

File tree

8 files changed

+62
-39
lines changed

8 files changed

+62
-39
lines changed

objdiff-core/src/arch/arm.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ impl ObjArch for ObjArchArm {
113113
relocations: &[ObjReloc],
114114
line_info: &BTreeMap<u64, u32>,
115115
config: &DiffObjConfig,
116-
_sections: &[ObjSection],
117116
) -> Result<ProcessCodeResult> {
118117
let start_addr = address as u32;
119118
let end_addr = start_addr + code.len() as u32;

objdiff-core/src/arch/arm64.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ impl ObjArch for ObjArchArm64 {
2929
relocations: &[ObjReloc],
3030
line_info: &BTreeMap<u64, u32>,
3131
config: &DiffObjConfig,
32-
_sections: &[ObjSection],
3332
) -> Result<ProcessCodeResult> {
3433
let start_address = address;
3534
let end_address = address + code.len() as u64;

objdiff-core/src/arch/mips.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ impl ObjArch for ObjArchMips {
8787
relocations: &[ObjReloc],
8888
line_info: &BTreeMap<u64, u32>,
8989
config: &DiffObjConfig,
90-
_sections: &[ObjSection],
9190
) -> Result<ProcessCodeResult> {
9291
let _guard = RABBITIZER_MUTEX.lock().map_err(|e| anyhow!("Failed to lock mutex: {e}"))?;
9392
configure_rabbitizer(match config.mips_abi {

objdiff-core/src/arch/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ impl DataType {
126126
}
127127

128128
pub trait ObjArch: Send + Sync {
129-
#[expect(clippy::too_many_arguments)]
130129
fn process_code(
131130
&self,
132131
address: u64,
@@ -135,7 +134,6 @@ pub trait ObjArch: Send + Sync {
135134
relocations: &[ObjReloc],
136135
line_info: &BTreeMap<u64, u32>,
137136
config: &DiffObjConfig,
138-
sections: &[ObjSection],
139137
) -> Result<ProcessCodeResult>;
140138

141139
fn implcit_addend(

objdiff-core/src/arch/ppc.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ impl ObjArch for ObjArchPpc {
4848
relocations: &[ObjReloc],
4949
line_info: &BTreeMap<u64, u32>,
5050
config: &DiffObjConfig,
51-
sections: &[ObjSection],
5251
) -> Result<ProcessCodeResult> {
5352
let ins_count = code.len() / 4;
5453
let mut ops = Vec::<u16>::with_capacity(ins_count);
5554
let mut insts = Vec::<ObjIns>::with_capacity(ins_count);
5655
let fake_pool_reloc_for_addr =
57-
generate_fake_pool_reloc_for_addr_mapping(address, code, relocations, sections);
56+
generate_fake_pool_reloc_for_addr_mapping(address, code, relocations);
5857
for (cur_addr, mut ins) in InsIter::new(code, address as u32) {
5958
let reloc = relocations.iter().find(|r| (r.address as u32 & !3) == cur_addr);
6059
if let Some(reloc) = reloc {
@@ -453,26 +452,38 @@ fn get_offset_and_addr_gpr_for_possible_pool_reference(
453452
// there isn't really a relocation here, as copying the pool relocation's type wouldn't make sense.
454453
// Also, if this instruction is accessing the middle of a symbol instead of the start, we add an
455454
// addend to indicate that.
456-
fn make_fake_pool_reloc(
457-
offset: i16,
458-
cur_addr: u32,
459-
pool_reloc: &ObjReloc,
460-
sections: &[ObjSection],
461-
) -> Option<ObjReloc> {
455+
fn make_fake_pool_reloc(offset: i16, cur_addr: u32, pool_reloc: &ObjReloc) -> Option<ObjReloc> {
462456
let offset_from_pool = pool_reloc.addend + offset as i64;
463457
let target_address = pool_reloc.target.address.checked_add_signed(offset_from_pool)?;
464458
let orig_section_index = pool_reloc.target.orig_section_index?;
465-
let section = sections.iter().find(|s| s.orig_index == orig_section_index)?;
466-
let target_symbol = section
467-
.symbols
468-
.iter()
469-
.find(|s| s.size > 0 && (s.address..s.address + s.size).contains(&target_address))?;
470-
let addend = (target_address - target_symbol.address) as i64;
459+
// We also need to create a fake target symbol to go inside our fake relocation.
460+
// This is because we don't have access to list of all symbols in this section, so we can't find
461+
// the real symbol yet. Instead we make a placeholder that has the correct `orig_section_index`
462+
// and `address` fields, and then later on when this information is displayed to the user, we
463+
// can find the real symbol by searching through the object's section's symbols for one that
464+
// contains this address.
465+
let fake_target_symbol = ObjSymbol {
466+
name: "".to_string(),
467+
demangled_name: None,
468+
address: target_address,
469+
section_address: 0,
470+
size: 0,
471+
size_known: false,
472+
kind: Default::default(),
473+
flags: Default::default(),
474+
orig_section_index: Some(orig_section_index),
475+
virtual_address: None,
476+
original_index: None,
477+
bytes: vec![],
478+
};
479+
// The addend is also fake because we don't know yet if the `target_address` here is the exact
480+
// start of the symbol or if it's in the middle of it.
481+
let fake_addend = 0;
471482
Some(ObjReloc {
472483
flags: RelocationFlags::Elf { r_type: elf::R_PPC_NONE },
473484
address: cur_addr as u64,
474-
target: target_symbol.clone(),
475-
addend,
485+
target: fake_target_symbol,
486+
addend: fake_addend,
476487
})
477488
}
478489

@@ -491,7 +502,6 @@ fn generate_fake_pool_reloc_for_addr_mapping(
491502
address: u64,
492503
code: &[u8],
493504
relocations: &[ObjReloc],
494-
sections: &[ObjSection],
495505
) -> HashMap<u32, ObjReloc> {
496506
let mut active_pool_relocs = HashMap::new();
497507
let mut pool_reloc_for_addr = HashMap::new();
@@ -538,9 +548,7 @@ fn generate_fake_pool_reloc_for_addr_mapping(
538548
// This instruction doesn't have a real relocation, so it may be a reference to one of
539549
// the already-loaded pools.
540550
if let Some(pool_reloc) = active_pool_relocs.get(&addr_src_gpr.0) {
541-
if let Some(fake_pool_reloc) =
542-
make_fake_pool_reloc(offset, cur_addr, pool_reloc, sections)
543-
{
551+
if let Some(fake_pool_reloc) = make_fake_pool_reloc(offset, cur_addr, pool_reloc) {
544552
pool_reloc_for_addr.insert(cur_addr, fake_pool_reloc);
545553
}
546554
if let Some(addr_dst_gpr) = addr_dst_gpr {

objdiff-core/src/arch/x86.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ impl ObjArch for ObjArchX86 {
3434
relocations: &[ObjReloc],
3535
line_info: &BTreeMap<u64, u32>,
3636
config: &DiffObjConfig,
37-
_sections: &[ObjSection],
3837
) -> Result<ProcessCodeResult> {
3938
let mut result = ProcessCodeResult { ops: Vec::new(), insts: Vec::new() };
4039
let mut decoder = Decoder::with_ip(self.bits, code, address, DecoderOptions::NONE);

objdiff-core/src/diff/code.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub fn process_code_symbol(
2828
&section.relocations,
2929
&section.line_info,
3030
config,
31-
&obj.sections,
3231
)
3332
}
3433

objdiff-gui/src/views/function_diff.rs

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ 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+
7790
fn ins_hover_ui(
7891
ui: &mut egui::Ui,
7992
obj: &ObjInfo,
@@ -119,17 +132,29 @@ fn ins_hover_ui(
119132
}
120133

121134
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+
122147
ui.label(format!("Relocation type: {}", obj.arch.display_reloc(reloc.flags)));
123-
let addend_str = match reloc.addend.cmp(&0i64) {
124-
Ordering::Greater => format!("+{:x}", reloc.addend),
125-
Ordering::Less => format!("-{:x}", -reloc.addend),
148+
let addend_str = match addend.cmp(&0i64) {
149+
Ordering::Greater => format!("+{:x}", addend),
150+
Ordering::Less => format!("-{:x}", -addend),
126151
_ => "".to_string(),
127152
};
128153
ui.colored_label(
129154
appearance.highlight_color,
130-
format!("Name: {}{}", reloc.target.name, addend_str),
155+
format!("Name: {}{}", target.name, addend_str),
131156
);
132-
if let Some(orig_section_index) = reloc.target.orig_section_index {
157+
if let Some(orig_section_index) = target.orig_section_index {
133158
if let Some(section) =
134159
obj.sections.iter().find(|s| s.orig_index == orig_section_index)
135160
{
@@ -140,15 +165,12 @@ fn ins_hover_ui(
140165
}
141166
ui.colored_label(
142167
appearance.highlight_color,
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),
168+
format!("Address: {:x}{}", target.address, addend_str),
148169
);
149-
if reloc.addend >= 0 && reloc.target.bytes.len() > reloc.addend as usize {
170+
ui.colored_label(appearance.highlight_color, format!("Size: {:x}", target.size));
171+
if addend >= 0 && target.bytes.len() > addend as usize {
150172
if let Some(s) = obj.arch.guess_data_type(ins).and_then(|ty| {
151-
obj.arch.display_data_type(ty, &reloc.target.bytes[reloc.addend as usize..])
173+
obj.arch.display_data_type(ty, &target.bytes[addend as usize..])
152174
}) {
153175
ui.colored_label(appearance.highlight_color, s);
154176
}

0 commit comments

Comments
 (0)