docs(conventions): reconstruction ABI/naming/FP/vtable contract
This commit is contained in:
1 parent
e4a3405a4b
commit
6a74131ac9
2 files changed
+136
-4
No files matched your search
@@ -0,0 +1,125 @@
|
||||
# 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).
|
||||
|
||||
## Function naming: FUN_<rva> 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:
|
||||
Kinect10 (`Kinect10.dll`) and 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.
|
||||
|
||||
```cpp
|
||||
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 GK macro names in this file and in `include/gk/types.h` must match exactly:
|
||||
|
||||
```
|
||||
grep -o 'GK[_][A-Z_]*' docs/conventions.md include/gk/types.h | sort -u
|
||||
```
|
||||
|
||||
The macros in scope are `GK_CDECL`, `GK_STDCALL`, `GK_THISCALL`, `GK_PRAGMA`,
|
||||
`GK_PACK_PUSH`, and `GK_PACK_POP`.
|
||||
+11
-4
@@ -39,7 +39,14 @@
|
||||
// struct SomePackedThing { /* ... */ };
|
||||
// #pragma pack(pop)
|
||||
//
|
||||
// GCC/Clang both honor `#pragma pack` identically to MSVC, so no GK_PACK_*
|
||||
// macro is introduced here; use the pragma directly at each struct that
|
||||
// needs it, and comment the source of the packing evidence (e.g. "offsets
|
||||
// confirmed via Ghidra struct editor on orig/gamekdm.dll").
|
||||
// GCC/MinGW both honor `#pragma pack` identically to MSVC. Use the pack
|
||||
// macros below at each struct that needs it, and comment the source of the
|
||||
// packing evidence (e.g. "offsets confirmed via Ghidra struct editor on
|
||||
// orig/gamekdm.dll"). See docs/conventions.md ("Struct packing").
|
||||
//
|
||||
// GK_PRAGMA(x) stringizes x and emits it as a _Pragma.
|
||||
// GK_PACK_PUSH(n) emits `#pragma pack(push, n)` to match MSVC's pack state.
|
||||
// GK_PACK_POP emits `#pragma pack(pop)` to restore it.
|
||||
#define GK_PRAGMA(x) _Pragma(#x)
|
||||
#define GK_PACK_PUSH(n) GK_PRAGMA(pack(push, n))
|
||||
#define GK_PACK_POP GK_PRAGMA(pack(pop))
|
||||
Reference in new issue
Block a user