#!/usr/bin/env python3
"""Scan the memory __LINKEDIT of the dumped Driver for real symbol-name content."""
import sys, re, collections

sys.stdout.reconfigure(encoding='utf-8')
d = open(r'E:\ios\work319\dump319\Driver.bin', 'rb').read()
lo, hi = 0x20a8000, 0x2313fa0
seg = d[lo:hi]

pats = [
    (rb'_OBJC_CLASS_\$__', 'ObjC classes'),
    (rb'\$s[0-9A-Za-z_]{10,}', 'Swift mangled'),
    (rb'_OBJC_METACLASS_\$__', 'ObjC metaclasses'),
]
for pat, lab in pats:
    hits = [m.start() for m in re.finditer(pat, seg)]
    print('%-18s : %d hits' % (lab, len(hits)))
    for h in hits[:4]:
        s = seg[h:h + 64]
        print('    @mem 0x%08x  %r' % (lo + h, s))

print()
c = collections.Counter()
for i in range(0, len(seg), 0x10000):
    nz = sum(1 for x in seg[i:i + 0x10000] if x)
    c[i // 0x10000] = nz
print('non-zero per 64KB of memory __LINKEDIT:')
for k in sorted(c):
    if c[k]:
        print('   mem 0x%08x  non-zero %d' % (lo + (k << 16), c[k]))
