#!/usr/bin/env python3
"""Own resolver (va-imagebase works on this dump). Map imp -> ObjC method."""
import struct, sys
import lief

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


def u64(va):
    o = va - base
    if o < 0 or o + 8 > len(d):
        return None
    return struct.unpack_from('<Q', d, o)[0]


def ptr(va):
    v = u64(va)
    if v is None:
        return None
    return v & 0xFFFFFFFFF


def cstr(va):
    if va is None:
        return '?'
    o = va - base
    if o < 0 or o >= len(d):
        return '?'
    e = d.find(b'\0', o)
    return d[o:e].decode('utf-8', 'replace')


def clsname(cls):
    if not cls:
        return '?'
    data = ptr(cls + 0x20)
    if not data:
        return '?'
    return cstr(ptr(data + 0x18))


def walk_methods(data_va, label, out):
    if not data_va:
        return
    ml = ptr(data_va + 0x20)
    if not ml:
        return
    o = ml - base
    if o < 0 or o + 8 > len(d):
        return
    entsize = struct.unpack_from('<I', d, o)[0]
    count = struct.unpack_from('<I', d, o + 4)[0]
    if count > 100000:
        return
    rel = bool(entsize & 0x80000000)
    esz = entsize & 0xFFFC
    p = o + 8
    for j in range(count):
        e = p + j * esz
        try:
            if rel:
                sel_va = e + base + struct.unpack_from('<i', d, e)[0]
                imp = e + base + 8 + struct.unpack_from('<i', d, e + 8)[0]
            else:
                sel_va = ptr(e + base)
                imp = ptr(e + base + 16)
        except Exception:
            continue
        if imp:
            out.setdefault(imp, []).append(label + cstr(sel_va))


impmap = {}
cl = b.get_section('__objc_classlist')
for i in range(cl.size // 8):
    cls = ptr(cl.virtual_address + i * 8)
    if not cls:
        continue
    name = clsname(cls)
    data = ptr(cls + 0x20)
    meta = ptr(cls)
    walk_methods(meta, name + ' +', impmap)
    walk_methods(cls, name + ' -', impmap)

for secname in ('__objc_catlist', '__objc_catlist2'):
    cs = b.get_section(secname)
    if not cs:
        continue
    for i in range(cs.size // 8):
        cat = ptr(cs.virtual_address + i * 8)
        if not cat:
            continue
        cname = cstr(ptr(cat))
        cls = ptr(cat + 8)
        base_name = clsname(cls) if cls else '?'
        walk_methods(cat, '%s(%s) +' % (base_name, cname), impmap)

print('impmap entries', len(impmap))
print('--- ShieldAssetReader info ---')
for i in range(cl.size // 8):
    cls = ptr(cl.virtual_address + i * 8)
    if not cls:
        continue
    nm = clsname(cls)
    if 'Shield' in nm or 'Banned' in nm:
        sup = ptr(cls + 8)
        print('  %-40s cls=%#x super=%s' % (nm, cls, clsname(sup) if sup else '?'))

print('--- lookup around call sites ---')
for t in (0x10000800c, 0x100166ee4, 0x1004cfd00, 0x1004cff00, 0x1004f2700,
          0x10032a000, 0x10032b000):
    hits = [(imp, names) for imp, names in impmap.items() if t <= imp < t + 0x400]
    print('around %#x:' % t)
    for imp, names in sorted(hits):
        print('   %#x %s' % (imp, names))
