Skip to content

Commit 4bf1377

Browse files
committed
[MachO] Fix relocations from chained fixups not respecting addends
The addends were correctly stored in the relocation info and displayed as offsets in linear view, but the relocation handlers never applied them. Reading from an address containing such a relocation would give an incorrect value.
1 parent 2b1fc96 commit 4bf1377

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

arch/arm64/arch_arm64.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2787,8 +2787,8 @@ class Arm64MachoRelocationHandler : public RelocationHandler
27872787
// printf("reloc->GetAddress(): 0x%llX\n", reloc->GetAddress());
27882788

27892789
if (info.nativeType == BINARYNINJA_MANUAL_RELOCATION)
2790-
{ // Magic number defined in MachOView.cpp for tagged pointers
2791-
*(uint64_t*)dest = info.target;
2790+
{ // Magic number defined in MachOView.cpp for chained fixups
2791+
*(uint64_t*)dest = info.target + info.addend;
27922792
}
27932793
else if (info.nativeType == ARM64_RELOC_PAGE21)
27942794
{

arch/armv7/arch_armv7.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2684,8 +2684,8 @@ class ArmMachORelocationHandler: public RelocationHandler
26842684
{
26852685
auto info = reloc->GetInfo();
26862686
if (info.nativeType == BINARYNINJA_MANUAL_RELOCATION)
2687-
{ // Magic number defined in MachOView.cpp for tagged pointers
2688-
*(uint32_t*)dest = (uint32_t)info.target;
2687+
{ // Magic number defined in MachOView.cpp for chained fixups
2688+
*(uint32_t*)dest = (uint32_t)(info.target + info.addend);
26892689
}
26902690

26912691
return true;

arch/x86/arch_x86.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4117,10 +4117,10 @@ class x86MachoRelocationHandler: public RelocationHandler
41174117
case (uint64_t)-1: // Magic number defined in MachOView.cpp
41184118
// We need to write a jump absolute `jmp target`
41194119
dest[0] = '\xe9';
4120-
((uint32_t*)&dest[1])[0] = target - (uint32_t)reloc->GetAddress() - 5;
4120+
((uint32_t*)&dest[1])[0] = target + (uint32_t)info.addend - (uint32_t)reloc->GetAddress() - 5;
41214121
break;
41224122
case (uint64_t)-2: // Magic number defined in MachOView.cpp
4123-
dest32[0] = target;
4123+
dest32[0] = target + (uint32_t)info.addend;
41244124
break;
41254125
case GENERIC_RELOC_VANILLA:
41264126
switch (info.size)
@@ -4307,7 +4307,7 @@ class x64MachoRelocationHandler: public RelocationHandler
43074307
dest64[0] = dest64[0] + info.next->target - target;
43084308
break;
43094309
case (uint64_t) -2:
4310-
dest64[0] = reloc->GetTarget();
4310+
dest64[0] = info.target + info.addend;
43114311
break;
43124312
}
43134313
return true;

0 commit comments

Comments
 (0)