From c917cad5f07d8231989327037c6f2069d4a2b084 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Tue, 29 Jul 2025 21:20:47 -0600 Subject: [PATCH] Strip zeros from end of inferred function sizes Resolves #3 --- Cargo.toml | 2 +- objdiff-core/src/obj/read.rs | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6bc0ad4..ff86f37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,4 @@ authors = ["Luke Street "] edition = "2024" license = "MIT OR Apache-2.0" repository = "https://github.com/encounter/objdiff" -rust-version = "1.85" +rust-version = "1.88" diff --git a/objdiff-core/src/obj/read.rs b/objdiff-core/src/obj/read.rs index d443ace..30798b3 100644 --- a/objdiff-core/src/obj/read.rs +++ b/objdiff-core/src/obj/read.rs @@ -205,10 +205,18 @@ fn infer_symbol_sizes(symbols: &mut [Symbol], sections: &[Section]) { } iter_idx += 1; }; - let next_address = next_symbol.map(|s| s.address).unwrap_or_else(|| { - let section = §ions[section_idx]; - section.address + section.size - }); + let section = §ions[section_idx]; + let mut next_address = + 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); if new_size > 0 { 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 if symbol.kind == SymbolKind::Unknown { - symbol.kind = match sections[section_idx].kind { + symbol.kind = match section.kind { SectionKind::Code => SymbolKind::Function, SectionKind::Data | SectionKind::Bss => SymbolKind::Object, _ => SymbolKind::Unknown,