Reconstruction layout: generated-src/ (generated) and src/ (curated), CMake, the first takeover: RNG
The decompiled C++ tree moves to generated-src/ (classes.h, C++20); src/ is for code written by hand. A curated function defines the generated declaration and starts its comment with its address, and source_tree.py stops emitting that body. src/rng.cpp takes over RNG (SetSeed, the constructor, Next, RandomInt, RandomFloat, Shuffle) with the shift table read from the exe as a constexpr array; src/log.h names KAGE_LogMessage's levels (1 INFO, 2 NET, 4 WARN, 8 ERROR, 16 ASSERT, from its prefix jump table). CMakeLists.txt builds src/*.cpp against generated-src/classes.h (clean with -Wall -Wextra). structs.hpp and headers.py are retired: scripts/merge_fields.py writes the merged field list, classes.h checks the layout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2ff2d5d87f
commit
3ce74c6e2d
331 files changed
+485
-7835
No files matched your search
+12
-8
@@ -1,9 +1,10 @@
|
||||
# Generates the reconstructed C++ tree from what the build knows: RTTI classes (classes.tsv: size from the deleting
|
||||
# destructor, direct bases, vtable slots), confirmed fields (dump/zhl_fields.tsv) and every decompiled function (dump/all.c).
|
||||
# <out>/classes.hpp one class per type: real bases, the vptr implied, own fields at their exact offsets, padding up to
|
||||
# <out>/classes.h one class per type: real bases, the vptr implied, own fields at their exact offsets, padding up to
|
||||
# the real size, virtual methods in slot order and the other methods as declarations
|
||||
# <out>/<class>.cpp that class's method bodies as decompiled; Ghidra's Class::method(obj, args) calls become
|
||||
# ((Class *)(obj))->Class::method(args) (a direct call, as in the binary)
|
||||
# A function src/ took over (a curated file with its address in a comment, // 0040ed50) is declared but not emitted.
|
||||
# Targets 64-bit Linux. The original 32-bit layout is kept as LAYOUT32 asserts, checked with -m32.
|
||||
# --check compiles every .cpp for x86_64, reports how many methods compile and how many raw offset accesses remain
|
||||
# (each one is a 64-bit hazard until it becomes a named field).
|
||||
@@ -17,7 +18,7 @@ POINTER_LIKE = {'EntityPtr'} # REPENTOGON's smart pointer, a raw Entity * in t
|
||||
LIBRARY = re.compile(r'^(std|rapidxml|theoraplayer|luabridge|stb_vorbis|Concurrency)(::|$)') # third party: declared, not reconstructed
|
||||
IDENT = re.compile(r'^~?[A-Za-z_]\w*$')
|
||||
INTRINSIC = re.compile(r'\b(CONCAT|SUB|ZEXT|SEXT|CARRY|SCARRY|SBORROW)(\d+)\(')
|
||||
CLANG = ['clang++', '--target=x86_64-linux-gnu', '-std=c++17', '-fsyntax-only', '-ferror-limit=0', '-nostdinc++',
|
||||
CLANG = ['clang++', '--target=x86_64-linux-gnu', '-std=c++20', '-fsyntax-only', '-ferror-limit=0', '-nostdinc++',
|
||||
'-Wno-invalid-offsetof', '-Wno-pragma-once-outside-header', '-w']
|
||||
PRIMITIVE = {'bool': 1, 'char': 1, 'byte': 1, 'undefined': 1, 'undefined1': 1, 'short': 2, 'ushort': 2, 'word': 2, 'undefined2': 2,
|
||||
'int': 4, 'uint': 4, 'float': 4, 'dword': 4, 'undefined4': 4, 'long': 4, 'ulong': 4, 'double': 8, 'Vector': 8,
|
||||
@@ -455,7 +456,7 @@ globalDecls += [f'extern {"code" if n.startswith("LAB_") else "undefined4"} {n};
|
||||
forward = sorted(t for t in used | {c for c in done if '::' not in c} if '::' not in t and t not in PRELUDE_TYPES and t not in enums and t not in typedefs)
|
||||
declarations = ''.join(f'namespace {ns} {{\n' + '\n'.join(d) + '\n}\n' if ns else '\n'.join(d) + '\n' for ns, d in sorted(free.items()))
|
||||
os.makedirs(outDir, exist_ok=True)
|
||||
with open(outDir + '/classes.hpp', 'w') as o:
|
||||
with open(outDir + '/classes.h', 'w') as o:
|
||||
o.write(PRELUDE + ''.join(intrinsic_define(n) for n in intrinsics) + '\n#if __SIZEOF_POINTER__ == 4\n#pragma pack(push, 1)\n#endif\n'
|
||||
+ ''.join(f'struct {t};\n' for t in forward) + '\n' + '\n'.join(aliases) + '\n\n' + '\n'.join(order) + '#if __SIZEOF_POINTER__ == 4\n#pragma pack(pop)\n#endif\n\n'
|
||||
+ declarations + '\n' + '\n'.join(globalDecls) + '\n')
|
||||
@@ -463,10 +464,13 @@ with open(outDir + '/classes.hpp', 'w') as o:
|
||||
# One .cpp per class: each body under the declaration it was declared with; a renamed `this` gets an alias.
|
||||
members = {f'{f[0]}::{f[1]}' for f in funcs.values() if f[3] == '__thiscall' and f[0] in done}
|
||||
spans, files = collections.defaultdict(list), {}
|
||||
srcDir = os.path.join(os.path.dirname(os.path.abspath(outDir)), 'src')
|
||||
curated = {int(a, 16) for root, _, names_ in os.walk(srcDir) for n in names_ if n.endswith(('.cpp', '.h'))
|
||||
for a in re.findall(r'//\s*([0-9a-f]{8})\b', open(os.path.join(root, n), errors='replace').read())}
|
||||
for cls in [c for c in done if not LIBRARY.match(c)]:
|
||||
parts, line = ['#include "classes.hpp"\n'], 2
|
||||
parts, line = ['#include "classes.h"\n'], 2
|
||||
for addr, (_, method, ret, cc, params) in methods.get(cls, []):
|
||||
if addr not in defined: continue
|
||||
if addr not in defined or addr in curated: continue
|
||||
decl = re.sub(r'^(virtual|static) |\s*override(?=;)|;$', '', defined[addr])
|
||||
name = re.search(r'(~?\w+)\(', decl).group(1)
|
||||
thisParam = funcs[addr][5]
|
||||
@@ -482,10 +486,10 @@ for cls in [c for c in done if not LIBRARY.match(c)]:
|
||||
files[cls] = path
|
||||
os.makedirs(os.path.dirname(f'{outDir}/{path}') or outDir, exist_ok=True)
|
||||
open(f'{outDir}/{path}', 'w').write(''.join(parts))
|
||||
print(f'source_tree: {len(done)} classes, {sum(1 for c in done if size.get(c))} sized, {sum(len(v) for v in methods.values())} methods,'
|
||||
print(f'source_tree: {len(curated)} functions taken over by src/, {len(done)} classes, {sum(1 for c in done if size.get(c))} sized, {sum(len(v) for v in methods.values())} methods,'
|
||||
f' {sum(len(s) for s in spans.values())} bodies in {len(files)} .cpp files')
|
||||
|
||||
if isCheck: # per method: does its body compile against classes.hpp?
|
||||
if isCheck: # per method: does its body compile against classes.h?
|
||||
def file_check(cls):
|
||||
result = subprocess.run(CLANG + ['-I', outDir, f'{outDir}/{files[cls]}'], capture_output=True, text=True)
|
||||
bad = collections.Counter()
|
||||
@@ -498,7 +502,7 @@ if isCheck: # per method: does its body compile against classes.hpp?
|
||||
with concurrent.futures.ThreadPoolExecutor(os.cpu_count()) as pool: results = list(pool.map(file_check, files))
|
||||
total = sum(len(spans[c]) for c in files)
|
||||
clean = sum(1 for c, bad in results for lo, hi, a in spans[c] if not bad.get(a))
|
||||
layout = subprocess.run(CLANG[:1] + ['--target=i686-linux-gnu'] + CLANG[2:] + ['-x', 'c++', f'{outDir}/classes.hpp'], capture_output=True, text=True)
|
||||
layout = subprocess.run(CLANG[:1] + ['--target=i686-linux-gnu'] + CLANG[2:] + ['-x', 'c++', f'{outDir}/classes.h'], capture_output=True, text=True)
|
||||
hazards = sum(len(re.findall(r'GHIDRA_CAST<char \*>\([^;]*?\) \+ (?:0x[0-9a-f]+|\d+)\)', open(f'{outDir}/{f}').read())) for f in files.values())
|
||||
print(f'source_tree --check: {clean}/{total} method bodies compile for x86_64 ({100 * clean / max(total, 1):.1f}%),'
|
||||
f' {hazards} raw offset accesses, 32-bit layout asserts {"hold" if layout.returncode == 0 else "FAIL"}')
|
||||
|
||||
Reference in new issue
Block a user