Skip to content

PPC: Detect unpooled string literal references #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion objdiff-core/src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,13 @@ pub trait Arch: Send + Sync + Debug {
SymbolFlagSet::default()
}

fn guess_data_type(&self, _resolved: ResolvedInstructionRef) -> Option<DataType> { None }
fn guess_data_type(
&self,
_resolved: ResolvedInstructionRef,
_bytes: &[u8],
) -> Option<DataType> {
None
}

fn symbol_hover(&self, _obj: &Object, _symbol_index: usize) -> Vec<HoverItem> { Vec::new() }

Expand Down
13 changes: 11 additions & 2 deletions objdiff-core/src/arch/ppc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,21 @@ impl Arch for ArchPpc {
}
}

fn guess_data_type(&self, resolved: ResolvedInstructionRef) -> Option<DataType> {
fn guess_data_type(&self, resolved: ResolvedInstructionRef, bytes: &[u8]) -> Option<DataType> {
if resolved.relocation.is_some_and(|r| r.symbol.name.starts_with("@stringBase")) {
// Pooled string.
return Some(DataType::String);
}
let opcode = ppc750cl::Opcode::from(resolved.ins_ref.opcode as u8);
guess_data_type_from_load_store_inst_op(opcode)
if let Some(ty) = guess_data_type_from_load_store_inst_op(opcode) {
// Numeric type.
return Some(ty);
}
if bytes.len() >= 2 && bytes.iter().position(|&c| c == b'\0') == Some(bytes.len() - 1) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if bytes.len() >= 2 && bytes.iter().position(|&c| c == b'\0') == Some(bytes.len() - 1) {
if bytes.len() >= 2 && bytes.iter().position(|&c| c == b'\0') == bytes.last() {

I think this should work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

last would return the last byte in the slice, I think? This is comparing to the last index (len-1), not the value, since all the values were already read inside the loop.

The compiler also doesn't like last for some reason:

can't compare `std::option::Option<usize>` with `std::option::Option<&u8>`
the trait `PartialEq<std::option::Option<&u8>>` is not implemented for `std::option::Option<usize>`
but trait `PartialEq<deranged::OptionRangedUsize<_, _>>` is implemented for it
for that trait implementation, expected `deranged::OptionRangedUsize<_, _>`, found `std::option::Option<&u8>`

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think my brain was melted by C++ iterators, nevermind

// It may be an unpooled string if the symbol contains exactly one null byte at the end of the symbol.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't have to be this PR, just thinking aloud, this logic could easily live in common arch code.

return Some(DataType::String);
}
None
}

fn symbol_hover(&self, _obj: &Object, symbol_index: usize) -> Vec<HoverItem> {
Expand Down
1 change: 1 addition & 0 deletions objdiff-core/src/diff/code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ fn reloc_eq(
|| address_eq(left_reloc, right_reloc))
&& (diff_config.function_reloc_diffs == FunctionRelocDiffs::NameAddress
|| left_reloc.symbol.kind != SymbolKind::Object
|| right_reloc.symbol.size == 0 // Likely a pool symbol like ...data, don't treat this as a diff
|| display_ins_data_literals(left_obj, left_ins)
== display_ins_data_literals(right_obj, right_ins))
}
Expand Down
7 changes: 4 additions & 3 deletions objdiff-core/src/diff/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ pub fn instruction_hover(
if let Some(reloc) = resolved.relocation {
out.push(HoverItem::Separator);
out.append(&mut relocation_hover(obj, reloc, None));
if let Some(ty) = obj.arch.guess_data_type(resolved) {
let bytes = obj.symbol_data(reloc.relocation.target_symbol).unwrap_or(&[]);
if let Some(ty) = obj.arch.guess_data_type(resolved, bytes) {
let literals = display_ins_data_literals(obj, resolved);
if !literals.is_empty() {
out.push(HoverItem::Separator);
Expand Down Expand Up @@ -758,7 +759,7 @@ pub fn display_ins_data_labels(obj: &Object, resolved: ResolvedInstructionRef) -
};
let bytes = &data[reloc.relocation.addend as usize..];
obj.arch
.guess_data_type(resolved)
.guess_data_type(resolved, bytes)
.map(|ty| ty.display_labels(obj.endianness, bytes))
.unwrap_or_default()
}
Expand All @@ -775,7 +776,7 @@ pub fn display_ins_data_literals(obj: &Object, resolved: ResolvedInstructionRef)
};
let bytes = &data[reloc.relocation.addend as usize..];
obj.arch
.guess_data_type(resolved)
.guess_data_type(resolved, bytes)
.map(|ty| ty.display_literals(obj.endianness, bytes))
.unwrap_or_default()
}
Loading