Files
kdm-re/docs/conventions.md
T
ulw-execute 9f96ef8ffc feat(arch): portable-core seam + gk::fp dual-fidelity + i386-gated ABI + seam-symbol check
Introduce the core/platform/host header seam under include/gk/ (portable value
types in core/, Kinect/PE mirror layouts in platform/, the camera-elevation
service interface in host/); the existing include/gk/structs/* paths become
compatibility forwards so no definition is duplicated. Add include/gk/fp.h with
gk::fp::round32 (binary32 rounding barrier) and gk::fp::ExtendedAcc (long double
faithful / double portable), and a GK_FIDELITY=faithful|portable CMake knob
paired with -mfpmath; faithful stays the drop-in differential target. i386-gate
GK_CDECL/STDCALL/THISCALL so non-i386 GNU builds get empty macros (native
-Werror). Migrate the three proven slice functions onto the seam without
behaviour change: box_distance expresses FP through gk/fp.h while keeping the
volatile spill the differential pins; the bodies stay in src/game/*.cpp where
prove_t11.py compiles and greps them. Add tools/seam_symbol_check.py (G5) and
re/game/seam_symbols.json asserting each flagged seam RVA ships as a
non-inlined, addressable symbol.

Verified: prove_t11.py exit 0 / ALL DIFFERENTIALS PASS (faithful, exact-FP from
df1620e); native GK_FIDELITY=portable validity compile under -Werror; macro
cross-check; G5 tool reports all three seam symbols addressable.
2026-11-29 00:54:30 -05:00

8.3 KiB

gamekdm reconstruction conventions

This is the binding contract for reconstructing gamekdm.dll as MinGW-buildable C++. Every reconstructed translation unit follows these rules. The companion header include/gk/types.h defines the macros named here, and the macro sets in the two files must stay identical (see the grep cross-check at the bottom).

Module ownership and the portability seam

Reconstructed companion source belongs in src/<module>/; its struct database, RVA database, claims, and reverse-engineering evidence belong in re/<module>/. Use the existing module identifier (for example avs, not a second libavs tree). The historical re/avs/property_node.cpp is a legacy exception: new avs source belongs in src/avs/. Only that module's worker should migrate the legacy file. Game source continues to use src/game/ and the existing re/ database.

Within a module, separate core/ (portable logic and value types), platform/ (target ABI, PE/export glue, detours, and mirror layouts), and host/ (concrete service implementations). Shared headers follow the same split under include/gk/: core/, platform/, and host/. Host interfaces describe only services a reconstructed caller already needs; today that is camera elevation. Core may depend on those interfaces, but never on Windows headers, PE addresses, or mirror-layout structs. Platform code translates mirror state to core inputs.

The three T11 entry points keep their definitions in src/game/box_distance.cpp, validity.cpp, and nui_elevation.cpp, where the committed differential harness (tools/unicorn_diff/prove_t11.py) compiles and greps them unchanged. Those bodies consume the seam headers and express floating point through include/gk/fp.h. Existing include/gk/structs/ paths are compatibility includes; mirror definitions live under include/gk/platform/, value types under include/gk/core/, and the one host service interface under include/gk/host/. Platform offset assertions still check every original layout. Fidelity is typedef-selected (gk::fp::ExtendedAcc) and flag-selected (GK_FIDELITY); never make a second source copy. See include/gk/core/README.md for fidelity selection and native builds.

Before editing shared build files or headers, coordinate the exact shared paths with other module workers. A module worker owns src/<module>/ and re/<module>/, not another module's reconstruction or database.

Function naming: FUN_ to semantic name

Ghidra names every undecompiled routine FUN_<rva>, where <rva> is the lowercase hex relative virtual address (image base excluded), for example FUN_10023af0. Reconstruction renames it to a semantic name but keeps the rva traceable:

  • Pick a semantic name in the module's namespace, e.g. gk::audio::MixSample.
  • At the declaration and definition, pin the origin in a comment the differ can read back: // rva=0x10023af0 (orig FUN_10023af0).
  • Never drop the rva. One rva maps to exactly one semantic name; record the pair in the function's claim entry so the mapping is reversible.
  • If Ghidra already carries a symbol from the import/export table, keep that symbol as the semantic name and still record the rva.

Calling convention macros

include/gk/types.h defines GK_CDECL, GK_STDCALL, and GK_THISCALL. Each declaration states its convention explicitly so a reader (and a future clang-cl cross-check) never has to guess. Apply them by origin:

  • GK_CDECL: caller-cleaned C calling convention. Use for avs and afp interop (the game's avs2 / avfs style imports and callbacks) and for anything the original declared __cdecl, which is MSVC's default for free functions.
  • GK_STDCALL: callee-cleaned stdcall. Use for the Win32-style boundaries: the external Microsoft NUI runtime (Kinect10.dll), Direct3D 9 (d3d9.dll) entry points, and any WINAPI/CALLBACK the original went through.
  • GK_THISCALL: MSVC thiscall (first arg this in ecx, callee-cleaned). Use for internal non-virtual member functions reconstructed as free functions, and for any method whose disasm passes this in ecx and cleans its own stack.

When the convention can't be read off the import table, read it from the validity-gate disasm: a ret N (nonzero) is callee-cleanup (stdcall/thiscall); a bare ret with the caller fixing esp is cdecl; this arriving in ecx marks thiscall.

Struct packing: match MSVC layout

MSVC built the original with its default packing (natural alignment, equivalent to #pragma pack(8) on x86) unless a translation unit set a tighter pack. Any reconstructed struct that must match the original's binary layout either:

  1. relies on natural alignment, which already matches MSVC's default for structs of only int/float/pointer members on x86, or

  2. brackets itself with the pack macros GK_PACK_PUSH(n) / GK_PACK_POP (both expand through the GK_PRAGMA helper to #pragma pack), reproducing the exact pack state MSVC had at that point in the original source, e.g.

    GK_PACK_PUSH(1)
    struct SomePackedThing { /* ... */ };
    GK_PACK_POP
    

GCC/MinGW honor #pragma pack identically to MSVC, so no byte copying is needed. Comment every packed struct with the layout evidence, for example // offsets confirmed via Ghidra struct editor on orig/gamekdm.dll.

Floating point model: match the original

The reconstructed FP model must match the original binary's, and it is detected, not assumed. Read it from the validity-gate disasm:

  • x87 instructions (fld, fmul, fstp, fadd, ...) mean the original used the x87 FPU with 80-bit intermediates. Build with -mfpmath=387.
  • SSE scalar instructions (movss, mulss, addss, cvtsi2ss, ...) mean the original used SSE math. Build with -mfpmath=sse -msse2.

Default to -mfpmath=387 unless the disasm shows SSE, because VS2010 SP1 x86 defaults to x87 with 80-bit intermediates. Always build with -ffp-contract=off -fno-fast-math so the compiler never fuses or reorders FP ops away from the original's rounding. The actual per-module detection happens in T8; this file pins the rule and the default.

vtable and RTTI reconstruction

Declare the C++ classes and let MinGW regenerate the vtable and RTTI under the Itanium ABI. Never memcpy MSVC vtable or RTTI bytes: MSVC and Itanium disagree on the layout (MSVC stores the Complete Object Locator at vtable[-1], Itanium stores the type_info pointer at vtable[-1]), so copied bytes are wrong under MinGW.

  • Reconstruct the class hierarchy and virtual-function signatures from the Ghidra-recovered vtables.
  • Verify slot order with g++ -fdump-lang-class and compare the emitted vtable slot order against Ghidra's vtable. They must agree slot for slot.
  • Let the toolchain emit RTTI; do not hand-build type_info.

Global ownership: when forwarding is allowed

Forward a function (leave it as a call into the original image instead of reconstructing it) only if neither that function nor any of its callees writes a global that any non-forwarded code reads. If a forwarded routine and a reconstructed routine both touch the same global, the two copies of state drift, so that function must be reconstructed rather than forwarded.

Per-function reconstruction checklist

Each function goes through the same ordered steps, recorded in its claim entry:

  1. Claim the rva (one owner per rva; no two workers claim the same rva).
  2. Decompile it in Ghidra and read the validity-gate disasm.
  3. Clean the decompilation to MinGW C++: apply the naming, calling-convention, packing, FP, and vtable rules above.
  4. Differential: build with the pinned toolchain and diff behavior/output against the original (the differ compares the reconstructed routine to the real one).
  5. Mark verified in the claim entry once the differential passes.

Macro cross-check

The ABI and packing macro names in this file and in include/gk/types.h must match exactly. GK_FIDELITY / GK_FIDELITY_PORTABLE are build-configuration names (the CMake cache knob and the include/gk/fp.h fidelity define), not ABI macros, so they are excluded from the cross-check:

diff <(grep -o 'GK[_][A-Z_]*' docs/conventions.md    | grep -Ev '^GK_FIDELITY' | sort -u) \
     <(grep -o 'GK[_][A-Z_]*' include/gk/types.h | grep -Ev '^GK_FIDELITY' | sort -u)

The macros in scope are GK_CDECL, GK_STDCALL, GK_THISCALL, GK_PRAGMA, GK_PACK_PUSH, and GK_PACK_POP. The cross-check passes when the diff is empty.