classes.tsv (vtable.py --classes): 194 RTTI classes with vftable, direct bases (offsets from the PMD), vtable slots and, for 176, the exact size from the deleting destructor's sized delete. Sizes flow into structs.hpp (static_assert sizeof) and, next build, into Ghidra's structs (ApplyTypes 'size' rows) so this[1]._x stops standing in for the tail. scripts/source_tree.py generates src/game/classes.hpp: 293 classes with real bases, vtables in slot order (overrides, REPENTOGON slot names, vfunc_N placeholders), fields at their offsets, 2400 method declarations; it compiles with clang --target=i686-pc-windows-msvc and all 174 sizeof / 1899 offsetof asserts hold. Round 26's prototypes are held. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
77 lines
4.7 KiB
Python
77 lines
4.7 KiB
Python
# MSVC RTTI vtables read straight from the exe (TypeDescriptor -> CompleteObjectLocator -> vftable, bases from the
|
|
# ClassHierarchyDescriptor). Usage:
|
|
# vtable.py exe functions.tsv zhl_vtables.tsv CLASS [maxslots] print CLASS's slots with their current names
|
|
# vtable.py exe functions.tsv zhl_vtables.tsv names.tsv lines for still-FUN_ slots whose slot name a base
|
|
# class declares in REPENTOGON (zhl_vtables.tsv)
|
|
import struct, sys, re, collections
|
|
exe, fns_path, zv_path = sys.argv[1:4]
|
|
data = open(exe, 'rb').read()
|
|
names = {int(p[0], 16): p[1] for p in (l.rstrip('\n').split('\t') for l in open(fns_path)) if len(p) >= 2}
|
|
zv = collections.defaultdict(dict)
|
|
for l in open(zv_path):
|
|
c, s, n = l.rstrip('\n').split('\t'); zv[c][int(s)] = n
|
|
pe = struct.unpack_from('<I', data, 0x3c)[0]; optsz = struct.unpack_from('<H', data, pe + 20)[0]; base = struct.unpack_from('<I', data, pe + 52)[0]
|
|
secs = [struct.unpack_from('<III', data, pe + 24 + optsz + 40 * i + 12) for i in range(struct.unpack_from('<H', data, pe + 6)[0])]
|
|
def va2off(va):
|
|
for sva, rsz, raw in secs:
|
|
if sva <= va - base < sva + rsz: return raw + va - base - sva
|
|
def off2va(o):
|
|
for sva, rsz, raw in secs:
|
|
if raw <= o < raw + rsz: return base + sva + o - raw
|
|
u32 = lambda o: struct.unpack_from('<I', data, o)[0]
|
|
def tdname(tdva):
|
|
o = va2off(tdva); m = re.match(rb'\.\?AV([\w@]+?)@@', data[o + 8:o + 200]);
|
|
if not m: return None
|
|
parts = m.group(1).decode().split('@'); parts = [p for p in parts if p != 'IsaacRepentancePlus']
|
|
return '::'.join(reversed(parts))
|
|
vt = {} # class -> (vtable offset, bases list nearest first)
|
|
for m in re.finditer(rb'\.\?AV[\w@]+?@@\x00', data):
|
|
tdva = off2va(m.start() - 8); cls = tdname(tdva)
|
|
if not cls or cls in vt: continue
|
|
for k in re.finditer(re.escape(struct.pack('<I', tdva)), data):
|
|
colo = k.start() - 12
|
|
if colo < 0 or u32(colo) != 0 or u32(colo + 4) != 0: continue
|
|
colva = off2va(colo); chd = va2off(u32(colo + 16))
|
|
if colva is None or chd is None: continue
|
|
n, arr = u32(chd + 8), va2off(u32(chd + 12))
|
|
bcds = [va2off(u32(arr + 4 * i)) for i in range(n)] if arr else []
|
|
bases = [tdname(u32(b)) for b in bcds[1:]]
|
|
direct, i = [], 1 # a BaseClassDescriptor is followed by its own numContainedBases bases; PMD.mdisp = offset in cls
|
|
while i < len(bcds):
|
|
direct.append((tdname(u32(bcds[i])), u32(bcds[i] + 8))); i += 1 + u32(bcds[i] + 4)
|
|
for k2 in re.finditer(re.escape(struct.pack('<I', colva)), data):
|
|
vt[cls] = (k2.start() + 4, bases, direct, u32(chd + 4)); break
|
|
if cls in vt: break
|
|
def slots(cls):
|
|
o = vt[cls][0]; out = []
|
|
while True:
|
|
f = u32(o + 4 * len(out))
|
|
if va2off(f) is None or not 0x401000 <= f <= 0xb20000 or len(out) > 200: return out
|
|
out.append(f)
|
|
if len(sys.argv) > 4 and sys.argv[4] == '--classes': # class table: size from the deleting destructor's sized delete
|
|
SIZED_DELETE = re.compile(r'operator_delete\(\s*\w+\s*,\s*(0x[0-9a-f]+|\d+)\s*\)')
|
|
dtors = {slots(c)[0] for c in vt if slots(c)}
|
|
sizes, cur = {}, None
|
|
for line in open(sys.argv[5], errors='replace'): # dump/all.c
|
|
if line.startswith('//@@ '): cur = int(line.split()[1], 16); continue
|
|
m = cur in dtors and cur not in sizes and SIZED_DELETE.search(line)
|
|
if m: sizes[cur] = int(m.group(1), 0)
|
|
print('# class\tvftable\tslots\tsize (deleting destructor; - unknown)\tdirect bases (name@offset)\tCHD attributes (1 multiple, 2 virtual inheritance)\tslot addresses')
|
|
for cls, (o, _, direct, attrs) in sorted(vt.items()):
|
|
s = slots(cls); size = sizes.get(s[0]) if s else None
|
|
print(f'{cls}\t{off2va(o):08x}\t{len(s)}\t{"%#x" % size if size else "-"}\t{",".join(f"{b}@{d:#x}" for b, d in direct) or "-"}\t{attrs}\t{",".join(f"{f:08x}" for f in s)}')
|
|
sys.exit()
|
|
if len(sys.argv) > 4:
|
|
for cls in [c for c in vt if c == sys.argv[4] or c.endswith('::' + sys.argv[4])]:
|
|
print(f'{cls} vftable @ {off2va(vt[cls][0]):08x}, bases: {", ".join(vt[cls][1]) or "-"}')
|
|
for i, f in enumerate(slots(cls)[:int(sys.argv[5]) if len(sys.argv) > 5 else 60]): print(f' {i:2d} {f:08x} {names.get(f, "?")}')
|
|
sys.exit()
|
|
seen = set()
|
|
for cls, (o, bases) in sorted(vt.items()):
|
|
for i, f in enumerate(slots(cls)):
|
|
if not names.get(f, '').startswith('FUN_') or f in seen: continue
|
|
for b in [cls] + bases:
|
|
nm = zv.get(b, {}).get(i) # REPENTOGON's declared layout only: names on base stubs are often ICF-folded
|
|
if nm:
|
|
print(f'{f:08x}\t{cls}::{nm}\tvtable slot {i} of {cls}; slot name from {b}'); seen.add(f); break
|