Strip zeros from end of inferred function sizes

Resolves #3
This commit is contained in:
Luke Street 2025-07-29 21:20:47 -06:00
parent dd653329f5
commit c917cad5f0
2 changed files with 14 additions and 6 deletions

View File

@ -19,4 +19,4 @@ authors = ["Luke Street <luke@street.dev>"]
edition = "2024" edition = "2024"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
repository = "https://github.com/encounter/objdiff" repository = "https://github.com/encounter/objdiff"
rust-version = "1.85" rust-version = "1.88"

View File

@ -205,10 +205,18 @@ fn infer_symbol_sizes(symbols: &mut [Symbol], sections: &[Section]) {
} }
iter_idx += 1; iter_idx += 1;
}; };
let next_address = next_symbol.map(|s| s.address).unwrap_or_else(|| { let section = &sections[section_idx];
let section = &sections[section_idx]; let mut next_address =
section.address + section.size next_symbol.map(|s| s.address).unwrap_or_else(|| section.address + section.size);
}); if section.kind == SectionKind::Code {
// For functions, trim any trailing 4-byte zeroes from the end (padding, nops)
while next_address > symbol.address + 4
&& let Some(data) = section.data_range(next_address - 4, 4)
&& data == [0u8; 4]
{
next_address -= 4;
}
}
let new_size = next_address.saturating_sub(symbol.address); let new_size = next_address.saturating_sub(symbol.address);
if new_size > 0 { if new_size > 0 {
let symbol = &mut symbols[symbol_idx]; let symbol = &mut symbols[symbol_idx];
@ -218,7 +226,7 @@ fn infer_symbol_sizes(symbols: &mut [Symbol], sections: &[Section]) {
} }
// Set symbol kind if unknown and size is non-zero // Set symbol kind if unknown and size is non-zero
if symbol.kind == SymbolKind::Unknown { if symbol.kind == SymbolKind::Unknown {
symbol.kind = match sections[section_idx].kind { symbol.kind = match section.kind {
SectionKind::Code => SymbolKind::Function, SectionKind::Code => SymbolKind::Function,
SectionKind::Data | SectionKind::Bss => SymbolKind::Object, SectionKind::Data | SectionKind::Bss => SymbolKind::Object,
_ => SymbolKind::Unknown, _ => SymbolKind::Unknown,