mirror of
https://github.com/encounter/objdiff.git
synced 2025-12-13 07:06:28 +00:00
Improve ARM function size inference
This allows 2-byte padding to be trimmed in ARM functions. Resolves #253
This commit is contained in:
@@ -460,12 +460,16 @@ impl Arch for ArchArm {
|
||||
section: &Section,
|
||||
mut next_address: u64,
|
||||
) -> Result<u64> {
|
||||
// Trim any trailing 4-byte zeroes from the end (padding)
|
||||
while next_address >= symbol.address + 4
|
||||
&& let Some(data) = section.data_range(next_address - 4, 4)
|
||||
&& data == [0u8; 4]
|
||||
// TODO: This should probably check the disasm mode and trim accordingly,
|
||||
// but self.disasm_modes isn't populated until post_init, so it needs a refactor.
|
||||
|
||||
// Trim any trailing 2-byte zeroes from the end (padding)
|
||||
while next_address >= symbol.address + 2
|
||||
&& let Some(data) = section.data_range(next_address - 2, 2)
|
||||
&& data == [0u8; 2]
|
||||
&& section.relocation_at(next_address - 2, 2).is_none()
|
||||
{
|
||||
next_address -= 4;
|
||||
next_address -= 2;
|
||||
}
|
||||
Ok(next_address.saturating_sub(symbol.address))
|
||||
}
|
||||
|
||||
@@ -355,6 +355,7 @@ impl Arch for ArchMips {
|
||||
while new_address >= symbol.address + 4
|
||||
&& let Some(data) = section.data_range(new_address - 4, 4)
|
||||
&& data == [0u8; 4]
|
||||
&& section.relocation_at(next_address - 4, 4).is_none()
|
||||
{
|
||||
new_address -= 4;
|
||||
}
|
||||
|
||||
@@ -457,6 +457,7 @@ impl Arch for ArchPpc {
|
||||
while next_address >= symbol.address + 4
|
||||
&& let Some(data) = section.data_range(next_address - 4, 4)
|
||||
&& data == [0u8; 4]
|
||||
&& section.relocation_at(next_address - 4, 4).is_none()
|
||||
{
|
||||
next_address -= 4;
|
||||
}
|
||||
|
||||
@@ -107,32 +107,33 @@ impl Section {
|
||||
// The alignment to use when "Combine data/text sections" is enabled.
|
||||
pub fn combined_alignment(&self) -> u64 {
|
||||
const MIN_ALIGNMENT: u64 = 4;
|
||||
self.align.map(|align| align.get().max(MIN_ALIGNMENT)).unwrap_or(MIN_ALIGNMENT)
|
||||
self.align.map_or(MIN_ALIGNMENT, |align| align.get().max(MIN_ALIGNMENT))
|
||||
}
|
||||
|
||||
pub fn relocation_at<'obj>(
|
||||
&'obj self,
|
||||
obj: &'obj Object,
|
||||
ins_ref: InstructionRef,
|
||||
) -> Option<ResolvedRelocation<'obj>> {
|
||||
match self.relocations.binary_search_by_key(&ins_ref.address, |r| r.address) {
|
||||
pub fn relocation_at(&self, address: u64, size: u8) -> Option<&Relocation> {
|
||||
match self.relocations.binary_search_by_key(&address, |r| r.address) {
|
||||
Ok(mut i) => {
|
||||
// Find the first relocation at the address
|
||||
while i
|
||||
.checked_sub(1)
|
||||
.and_then(|n| self.relocations.get(n))
|
||||
.is_some_and(|r| r.address == ins_ref.address)
|
||||
.is_some_and(|r| r.address == address)
|
||||
{
|
||||
i -= 1;
|
||||
}
|
||||
self.relocations.get(i)
|
||||
}
|
||||
Err(i) => self
|
||||
.relocations
|
||||
.get(i)
|
||||
.filter(|r| r.address < ins_ref.address + ins_ref.size as u64),
|
||||
Err(i) => self.relocations.get(i).filter(|r| r.address < address + size as u64),
|
||||
}
|
||||
.and_then(|relocation| {
|
||||
}
|
||||
|
||||
pub fn resolve_relocation_at<'obj>(
|
||||
&'obj self,
|
||||
obj: &'obj Object,
|
||||
address: u64,
|
||||
size: u8,
|
||||
) -> Option<ResolvedRelocation<'obj>> {
|
||||
self.relocation_at(address, size).and_then(|relocation| {
|
||||
let symbol = obj.symbols.get(relocation.target_symbol)?;
|
||||
Some(ResolvedRelocation { relocation, symbol })
|
||||
})
|
||||
@@ -316,7 +317,7 @@ impl Object {
|
||||
let section = self.sections.get(section_index)?;
|
||||
let offset = ins_ref.address.checked_sub(section.address)?;
|
||||
let code = section.data.get(offset as usize..offset as usize + ins_ref.size as usize)?;
|
||||
let relocation = section.relocation_at(self, ins_ref);
|
||||
let relocation = section.resolve_relocation_at(self, ins_ref.address, ins_ref.size);
|
||||
Some(ResolvedInstructionRef {
|
||||
ins_ref,
|
||||
symbol_index,
|
||||
|
||||
Reference in New Issue
Block a user