#!/usr/bin/env python3
"""Route 1: all reference shapes for the BannedViewController class + name strings."""
import struct, sys
import lief

sys.stdout.reconfigure(encoding='utf-8')
PATH = r'E:\ios\work319\patch_src\Payload\Driver.app\Driver'
d = open(PATH, 'rb').read()
b = lief.MachO.parse(PATH).at(0)
base = b.imagebase

CLSREF_SLOT = 0x1eca208           # file offset of the classref slot
slot_va = base + CLSREF_SLOT
cls = struct.unpack('<Q', d[CLSREF_SLOT:CLSREF_SLOT + 8])[0]
print('slot VA %#x value %#x' % (slot_va, cls))

# data refs: any 8-byte pointer equal to the slot VA
pat = struct.pack('<Q', slot_va)
hits = []
i = 0
while True:
    i = d.find(pat, i)
    if i < 0:
        break
    hits.append(i)
    i += 1
print('data refs to slot:', [hex(h) for h in hits])

# data refs to the class object itself (already done for 0x101f99ff0 -> 0x1eca208)
# data refs to the two name strings
for name_va in (0x101afbcd0, 0x1019daf6d):
    pat = struct.pack('<Q', name_va)
    hits = []
    i = 0
    while True:
        i = d.find(pat, i)
        if i < 0:
            break
        hits.append(i)
        i += 1
    print('data refs to %#x:' % name_va, [hex(h) for h in hits])
