Skip to content

Commit

Permalink
read/macho: handle ARM64_RELOC_ADDEND
Browse files Browse the repository at this point in the history
  • Loading branch information
philipc committed Oct 24, 2023
1 parent edd17a5 commit df35618
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 3 additions & 6 deletions crates/examples/testfiles/macho/reloc-aarch64.o.objdump
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ __text relocations
(20, Relocation { kind: MachO { value: 8, relative: true }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })
(1c, Relocation { kind: MachO { value: 6, relative: false }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })
(18, Relocation { kind: MachO { value: 5, relative: true }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })
(14, Relocation { kind: MachO { value: a, relative: false }, encoding: Generic, size: 20, target: Section(SectionIndex(14)), addend: 0, implicit_addend: true })
(14, Relocation { kind: MachO { value: 4, relative: false }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })
(10, Relocation { kind: MachO { value: a, relative: false }, encoding: Generic, size: 20, target: Section(SectionIndex(14)), addend: 0, implicit_addend: true })
(10, Relocation { kind: MachO { value: 3, relative: true }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })
(14, Relocation { kind: MachO { value: 4, relative: false }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 14, implicit_addend: false })
(10, Relocation { kind: MachO { value: 3, relative: true }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 14, implicit_addend: false })
(c, Relocation { kind: MachO { value: 4, relative: false }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })
(8, Relocation { kind: MachO { value: 3, relative: true }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })
(4, Relocation { kind: MachO { value: a, relative: false }, encoding: Generic, size: 20, target: Section(SectionIndex(14)), addend: 0, implicit_addend: true })
(4, Relocation { kind: MachO { value: 2, relative: true }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })
(4, Relocation { kind: MachO { value: 2, relative: true }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 14, implicit_addend: false })
(0, Relocation { kind: MachO { value: 2, relative: true }, encoding: Generic, size: 20, target: Symbol(SymbolIndex(3)), addend: 0, implicit_addend: true })

__data relocations
Expand Down
12 changes: 10 additions & 2 deletions src/read/macho/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ where
type Item = (u64, Relocation);

fn next(&mut self) -> Option<Self::Item> {
let mut paired_addend = 0;
loop {
let reloc = self.relocations.next()?;
let endian = self.file.endian;
Expand All @@ -56,6 +57,12 @@ where
macho::CPU_TYPE_ARM64 | macho::CPU_TYPE_ARM64_32 => {
match (reloc.r_type, reloc.r_pcrel) {
(macho::ARM64_RELOC_UNSIGNED, false) => RelocationKind::Absolute,
(macho::ARM64_RELOC_ADDEND, _) => {
paired_addend = i64::from(reloc.r_symbolnum)
.wrapping_shl(64 - 24)
.wrapping_shr(64 - 24);
continue;
}
_ => RelocationKind::MachO {
value: reloc.r_type,
relative: reloc.r_pcrel,
Expand Down Expand Up @@ -100,7 +107,8 @@ where
} else {
RelocationTarget::Section(SectionIndex(reloc.r_symbolnum as usize))
};
let mut addend = 0;
let implicit_addend = paired_addend == 0;
let mut addend = paired_addend;
if reloc.r_pcrel {
// For PC relative relocations on some architectures, the
// addend does not include the offset required due to the
Expand Down Expand Up @@ -132,7 +140,7 @@ where
size,
target,
addend,
implicit_addend: true,
implicit_addend,
},
));
}
Expand Down

0 comments on commit df35618

Please sign in to comment.