docs(re): gameMain caller-binding contract
This commit is contained in:
1 parent
349f835fe1
commit
dd362cb13f
1 file changed
+264
@@ -0,0 +1,264 @@
|
||||
# gamekdm.dll `gameMain` binding: how it is found and called, and the `hInstance` argument
|
||||
|
||||
Scope: the boot path from spice2x/arkkdm's launcher down to the single call into gamekdm.dll's
|
||||
exported `gameMain`. Findings are cross-referenced between the Linux disassembly of the shipped
|
||||
Windows binaries (`objdump -d/-p/-s`, i386 PE) and the spice2x source (`spice2x-master.tar.gz`,
|
||||
extracted from `src/` next to the binaries). Full raw transcript:
|
||||
`.omo/evidence/gamekdm-decomp-linux/task-5.log`.
|
||||
|
||||
Binaries analyzed (all from
|
||||
`/mnt/f/Users/Aaron Thapa/Downloads/KDM-2016021900/contents/`):
|
||||
|
||||
- `launcher.exe` (PE32, image base `0x01000000`) — the generic AVS launcher, invoked as
|
||||
`launcher -k deahook.dll arkkdm.dll` by `gamestart.bat`.
|
||||
- `arkkdm.dll` (PE32, image base `0x10000000`) — the DEA/KDM "ark" game-glue DLL that spice2x
|
||||
loads as the AVS game DLL (`avs::game::DLL_NAME`).
|
||||
- `deahook.dll` — a small hook DLL loaded by the launcher before `arkkdm.dll`; exports only
|
||||
`GetProcAddress`/`LoadLibraryA`/`LoadLibraryExW` forwarders per its import/export tables, not
|
||||
involved in the `gameMain` resolution itself.
|
||||
- `/home/athapa/ghidra/gamekdm.dll` — the original game DLL whose `gameMain` export is the
|
||||
target of this binding (used only to confirm the printed diagnostic string and parameter
|
||||
shape, not disassembled beyond its entry stub).
|
||||
|
||||
## (a) Name vs. ordinal: binding is by NAME at every hop, not by ordinal
|
||||
|
||||
There are two separate binding hops in this boot path, and both resolve by exported **name**
|
||||
string through `GetProcAddress`, never by ordinal:
|
||||
|
||||
**Hop 1 — spice2x (AVS launcher) → arkkdm.dll.** spice2x's `avs::game` layer only ever knows
|
||||
two entry point names, `"dll_entry_init"` and `"dll_entry_main"`:
|
||||
|
||||
```
|
||||
spice2x/avs/game.cpp:13 const char ENTRY_INIT_NAME[] = "dll_entry_init";
|
||||
spice2x/avs/game.cpp:14 const char ENTRY_MAIN_NAME[] = "dll_entry_main";
|
||||
spice2x/avs/game.cpp:98 dll_entry_init = (ENTRY_INIT_T) libutils::get_proc(DLL_INSTANCE, ENTRY_INIT_NAME);
|
||||
spice2x/avs/game.cpp:99 dll_entry_main = (ENTRY_MAIN_T) libutils::get_proc(DLL_INSTANCE, ENTRY_MAIN_NAME);
|
||||
spice2x/util/libutils.cpp:128 FARPROC libutils::get_proc(HMODULE module, LPCSTR proc) {
|
||||
spice2x/util/libutils.cpp:129 auto value = GetProcAddress(module, proc);
|
||||
```
|
||||
|
||||
`launcher/launcher.cpp:1191-1192` sets `avs::game::DLL_NAME = "arkkdm.dll"` once
|
||||
`check_dll("arkkdm.dll")` succeeds, and `launcher/launcher.cpp:1888` makes the single top-level
|
||||
call `avs::game::entry_main()` that triggers the chain below. `entry_init`/`entry_main` even
|
||||
re-resolve the proc address on every call and log `"... is hooked"` if it changed
|
||||
(`avs/game.cpp:103-125`), which is defensive **name** re-resolution, not ordinal lookup.
|
||||
|
||||
Confirmed on the arkkdm.dll side: `objdump -p arkkdm.dll` shows exactly two ordinal/name pairs
|
||||
at the tail of its export directory (ordinal base 1, Hint/Name table present — i.e. these are
|
||||
real by-name exports, not ordinal-only):
|
||||
|
||||
```
|
||||
[ 253] +base[ 254] 00fd dll_entry_init
|
||||
[ 254] +base[ 255] 00fe dll_entry_main
|
||||
```
|
||||
(Export Address Table RVAs: `dll_entry_init` = RVA `0x000767c0` → VA `0x100767c0`;
|
||||
`dll_entry_main` = RVA `0x000767b0` → VA `0x100767b0`, image base `0x10000000`.)
|
||||
|
||||
**Hop 2 — arkkdm.dll → gamekdm.dll's `gameMain`.** This hop is **entirely internal to
|
||||
arkkdm.dll**; spice2x never touches it (see part (c)). Disassembling the function arkkdm.dll's
|
||||
`dll_entry_main` ultimately calls (VA `0x10001340`) shows a literal `GetProcAddress` call
|
||||
passing the ASCII string `"gameMain"`:
|
||||
|
||||
```
|
||||
10001396: 68 dc 09 09 10 push 0x100909dc ; &"gameMain"
|
||||
1000139b: 56 push esi ; esi = HMODULE of the candidate library
|
||||
1000139c: ff 15 3c d1 07 10 call DWORD PTR ds:0x1007d13c ; import thunk = KERNEL32!GetProcAddress
|
||||
100013a2: 85 c0 test eax,eax
|
||||
100013a4: 75 0e jne 0x100013b4 ; found -> call it (see part (b))
|
||||
```
|
||||
|
||||
That the thunk at `0x1007d13c` really is `GetProcAddress` is independently confirmed by
|
||||
arkkdm.dll's import directory (`objdump -p`):
|
||||
|
||||
```
|
||||
DLL Name: KERNEL32.dll
|
||||
0007d050 <none> 033f LoadLibraryW
|
||||
0007d134 <none> 033c LoadLibraryA
|
||||
0007d13c <none> 0245 GetProcAddress
|
||||
```
|
||||
|
||||
and the string `"gameMain"` lives at VA `0x100909dc` in `.rdata`
|
||||
(`objdump -s --start-address=0x10090870` shows `...67616d65 4d61696e 00000000...` = `"gameMain\0"`
|
||||
right after a `"gameInit\0"` string at `0x100909d0`; file-offset cross-check: `strings -a -t x
|
||||
arkkdm.dll` independently reports `8f9dc gameMain`, and `0x100909dc − 0x1007d000 + 0x7c000 =
|
||||
0x8f9dc` — the `.rdata` section's VMA-to-file-offset delta — matches exactly).
|
||||
|
||||
**Conclusion:** both the launcher→arkkdm.dll and arkkdm.dll→gamekdm.dll bindings are plain
|
||||
`GetProcAddress`-by-name. There is no ordinal-only export or ordinal-based call anywhere on this
|
||||
path; the `(dll_entry_init, dll_entry_main)` pair happens to also carry low (253/254) sequential
|
||||
ordinals in arkkdm.dll's export table, but spice2x (and arkkdm.dll's own internal resolver for
|
||||
`gameMain`) always look them up by name string, never by ordinal number.
|
||||
|
||||
## (b) The `hInstance` argument: arkkdm.dll's own module HINSTANCE, not gamekdm.dll's
|
||||
|
||||
Tracing the single stack argument pushed into the resolved `gameMain` pointer
|
||||
(`10001340`-`100013d0`):
|
||||
|
||||
```
|
||||
10001329: 8b 4d fc mov ecx,DWORD PTR [ebp-0x4] ; saved earlier = *(DWORD*)[struct ptr]
|
||||
100013b4: 8b 4d fc mov ecx,DWORD PTR [ebp-0x4]
|
||||
100013b7: 51 push ecx ; the ONLY argument to gameMain
|
||||
100013b8: ff d0 call eax ; eax = resolved gameMain address
|
||||
```
|
||||
|
||||
`[ebp-0x4]` was populated a few instructions earlier by dereferencing offset 0 of a
|
||||
caller-supplied struct pointer (`ebx`, received as this function's only parameter):
|
||||
|
||||
```
|
||||
10001374: 8b 13 mov edx,DWORD PTR [ebx] ; struct->field0
|
||||
10001379: 89 55 fc mov DWORD PTR [ebp-0x4],edx
|
||||
```
|
||||
|
||||
The struct passed in is a fixed global at VA `0x1014b9f8` (the only caller of this function,
|
||||
`arkkdm.dll`'s exported `dll_entry_main` path, pushes it explicitly: `10002573: push
|
||||
0x1014b9f8`). The `.rdata` strings right next to the code that writes this global name it
|
||||
**`MainDllInfo`**, with field0 called `dllHMODULE`:
|
||||
|
||||
```
|
||||
10090884 (referenced at 10076800): "1st... MainDllInfo::setDllHMODULE : dllHMODULE %p, hModule %p\n"
|
||||
100908c0 (referenced at 10076813): "!!!WARNNING!!! MainDllInfo::setDllHMODULE : dllHMODULE %p, hModule %p\n"
|
||||
```
|
||||
|
||||
And the code that performs this first-time set, inside arkkdm.dll's exported `dll_entry_init`
|
||||
(VA `0x100767c0`, the function `objdump -p` lists as ordinal 253):
|
||||
|
||||
```
|
||||
100767eb: a1 f8 b9 14 10 mov eax,ds:0x1014b9f8 ; g_MainDllInfo.dllHMODULE (0 until first set)
|
||||
100767f0: 85 c0 test eax,eax
|
||||
100767f2: 75 13 jne 0x10076807 ; already set -> compare & warn path
|
||||
100767f4: a1 34 51 0f 10 mov eax,ds:0x100f5134 ; <- arkkdm.dll's own captured hInstDLL
|
||||
100767fb: a3 f8 b9 14 10 mov ds:0x1014b9f8,eax ; g_MainDllInfo.dllHMODULE = that value
|
||||
```
|
||||
|
||||
`0x100f5134` is populated by arkkdm.dll's own `DllMain`, which this disassembly shows checking
|
||||
`fdwReason == DLL_PROCESS_ATTACH (1)` and, only in that case, storing its first parameter
|
||||
(`hinstDLL`) there:
|
||||
|
||||
```
|
||||
10076793: 8b 45 0c mov eax,DWORD PTR [ebp+0xc] ; fdwReason (DllMain arg 2)
|
||||
10076796: 48 dec eax
|
||||
10076797: 75 08 jne 0x100767a1 ; skip unless fdwReason == 1
|
||||
10076799: 8b 45 08 mov eax,DWORD PTR [ebp+0x8] ; hinstDLL (DllMain arg 1)
|
||||
1007679c: a3 34 51 0f 10 mov ds:0x100f5134,eax ; stash it
|
||||
```
|
||||
|
||||
**Conclusion:** the `void* hInstance` that `gamekdm.dll`'s `gameMain(void* hInstance)` receives
|
||||
and logs as `gameMain hInstance = %p` is **arkkdm.dll's own `HINSTANCE`/`HMODULE`** — the handle
|
||||
the Windows loader assigned to `arkkdm.dll` when it was mapped, captured once in `DllMain` on
|
||||
`DLL_PROCESS_ATTACH` and handed down through the `dll_entry_init` → `g_MainDllInfo.dllHMODULE` →
|
||||
`dll_entry_main` → `gameMain` chain. It is **not** `gamekdm.dll`'s own module handle (gamekdm.dll
|
||||
never receives or computes its own `HMODULE` on this path) and it is **not** any
|
||||
spice2x/launcher-level handle — spice2x's `avs::game::DLL_INSTANCE` (the `HINSTANCE` of
|
||||
`arkkdm.dll` as seen by spice2x, set via `LoadLibraryW` at `avs/game.cpp:94`) and arkkdm.dll's
|
||||
own internally-captured `hInstDLL` are the same underlying OS handle value (both are "the
|
||||
HINSTANCE the loader gave arkkdm.dll"), but spice2x's copy of that value is never passed to
|
||||
`gameMain` directly — arkkdm.dll re-derives and forwards its own copy independently via the
|
||||
`DllMain` → `g_MainDllInfo` relay described above. The `fdwReason == 1` guard plus the
|
||||
"already set" warning string (`!!!WARNNING!!! ... dllHMODULE %p, hModule %p`) show this value is
|
||||
latched exactly once per process and is treated as a stable per-DLL identity handle, matching
|
||||
the Win32 convention that `HINSTANCE == HMODULE` for a mapped module.
|
||||
|
||||
## (c) Richer-than-plain-GetProcAddress mechanism: a 2-candidate module fallback, no custom protocol
|
||||
|
||||
spice2x's own role stops at loading `gamekdm.dll` so its hook DLL (`deahook.dll`/attached hooks)
|
||||
can bind to it early — it never resolves or calls `gameMain` itself:
|
||||
|
||||
```
|
||||
spice2x/games/dea/dea.cpp:16 libutils::try_library(MODULE_PATH / "gamekdm.dll");
|
||||
```
|
||||
|
||||
`libutils::try_library` is a bare `LoadLibraryW`/`LoadLibraryA` wrapper
|
||||
(`spice2x/util/libutils.cpp:82-88`) with no accompanying `get_proc`/`GetProcAddress` call for
|
||||
`gamekdm.dll` anywhere in the spice2x tree (`rg 'gameMain'` across the whole source tree matches
|
||||
only an unrelated comment in `spice2x/games/we/we.cpp:137` about a different game's `gameMain`
|
||||
sleeping — Winning Eleven, not DEA/KDM). All resolution and invocation of `gameMain` happens
|
||||
inside **arkkdm.dll** itself, inside the function at VA `0x10001340`. `dll_entry_main`
|
||||
(`100767b0`) reaches it with a plain, unconditional jump — not a call — straight into the
|
||||
function at `0x100024c0` (`100767b0: jmp 0x100024c0`); that function's own code then does the
|
||||
actual resolve-and-invoke, at `10002578: call 0x10001340`. (`0x10076800` is a separate,
|
||||
earlier code path inside `dll_entry_init`/`DllMain`'s `MainDllInfo` bookkeeping — it only pushes
|
||||
a diagnostic-log string and is not part of this call chain; likewise `0x10002440`, called later
|
||||
in `0x100767b0`'s own function at `1007682f`, is a separate initialization step unrelated to the
|
||||
`gameMain` resolve-and-call at `0x10002578`/`0x10001340`.)
|
||||
|
||||
That function is slightly richer than a single `LoadLibraryA`+`GetProcAddress` pair: it loops
|
||||
over a small fixed array of **two** candidate module-name strings before giving up
|
||||
(`10001369: mov edi,DWORD PTR [eax*4+0x100b7d60]` with loop bound `cmp eax,0x2`), reading them
|
||||
from a `.data` pointer table:
|
||||
|
||||
```
|
||||
.data @ 0x100b7d60: dd 0x1009032c, 0x10090338
|
||||
0x1009032c -> "gamekdm.dll" (confirmed: strings -a -t x arkkdm.dll -> `8f32c gamekdm.dll`;
|
||||
0x1009032c - 0x1007d000 + 0x7c000 = 0x8f32c)
|
||||
0x10090338 -> "arkkdmtest.dll" (objdump -s --start-address=0x10090330: "...arkkdmtest.dll\0")
|
||||
```
|
||||
|
||||
For each candidate it calls its own `LoadLibraryA` wrapper (`call 0x100013e0`, which itself
|
||||
calls the `0x1007d134` import thunk = `LoadLibraryA`), and only on a successful load does it
|
||||
call `GetProcAddress(hModule, "gameMain")` (`0x1007d13c`) and, on success, invoke the resolved
|
||||
pointer with the `hInstance` argument from part (b); on failure for both candidates it returns
|
||||
an error code without calling anything. The identical two-candidate loop pattern
|
||||
(`gamekdm.dll` / `arkkdmtest.dll`) is reused immediately beforehand to resolve a second,
|
||||
unrelated export named `"gameInit"` (VA `0x100012a0`-`0x10001338`, same `0x100b7d60` table),
|
||||
which this task did not need to trace further.
|
||||
|
||||
**Conclusion:** the "richer than plain `GetProcAddress`" element is a small build-variant
|
||||
fallback (production `gamekdm.dll` vs. a `arkkdmtest.dll` test build) baked into arkkdm.dll's own
|
||||
resolver, not a custom linking/export protocol layered over `GetProcAddress` — the underlying
|
||||
lookup and call are standard `LoadLibraryA` + `GetProcAddress` + indirect `call`.
|
||||
|
||||
## (d) `gameMain`'s calling convention: cdecl
|
||||
|
||||
**Calling convention: cdecl (verified, bare ret at 0x10009e91/0x10009ea5).** `gamekdm.dll`
|
||||
exports exactly one symbol, `gameMain`, at RVA `0x9e20` (VA `0x10009e20` in
|
||||
`/home/athapa/ghidra/gamekdm.dll`). Disassembling its full body
|
||||
(`objdump -d -M intel --start-address=0x10009e20 --stop-address=0x10009eb0`) shows both of its
|
||||
return paths ending in a **bare** `ret` with no immediate operand:
|
||||
|
||||
```
|
||||
10009e8d: 5f pop edi
|
||||
10009e8e: 33 c0 xor eax,eax
|
||||
10009e90: 5e pop esi
|
||||
10009e91: c3 ret
|
||||
...
|
||||
10009ea1: 5f pop edi
|
||||
10009ea2: 33 c0 xor eax,eax
|
||||
10009ea4: 5e pop esi
|
||||
10009ea5: c3 ret
|
||||
```
|
||||
|
||||
`gameMain` takes a single pointer argument (`hInstance`, read off `[esp+4]` at function entry:
|
||||
`10009e28: mov edi,DWORD PTR [esp+0xc]` after two earlier `push`es shift the frame, equivalent to
|
||||
the original `[esp+4]` at entry). A `ret` with no `N` operand pops only the return address and
|
||||
leaves argument cleanup to the **caller** — the defining trait of **cdecl**, as opposed to
|
||||
`stdcall`/`ret 4`, where the callee itself would pop the single 4-byte argument before
|
||||
returning. Correspondingly, the call site into the resolved `gameMain` pointer (part (b), `100013b4-100013b8:
|
||||
push ecx; call eax`) never issues an explicit `add esp,4` after the call to pop the argument;
|
||||
instead the caller's own frame-pointer-based epilogue (`100013bf: mov esp,ebp`) resets `esp`
|
||||
wholesale, discarding the pushed argument along with the rest of the local frame — the caller,
|
||||
not the callee, owns stack cleanup, consistent with cdecl. (`0x10001000` is unrelated code
|
||||
elsewhere in arkkdm.dll's resolver stub, not `gameMain`'s body — `gameMain` itself lives at
|
||||
`0x10009e20` in `gamekdm.dll`, a different binary.)
|
||||
|
||||
## Unresolved / deferred points
|
||||
|
||||
- This was all verified statically from the Linux-hosted `objdump` disassembly and the spice2x
|
||||
C++ source; no in-game Windows run was performed to confirm the live `%p` value printed by
|
||||
`gameMain hInstance = %p` in `/home/athapa/ghidra/gamekdm.dll` matches arkkdm.dll's load
|
||||
address at runtime (explicitly deferred/optional per task scope).
|
||||
- The exact caller chain from `dll_entry_main` (`0x100767b0: jmp 0x100024c0`) into the function
|
||||
at `0x100024c0`, whose own `10002578: call 0x10001340` reaches the `gameMain` resolver, was
|
||||
walked instruction-by-instruction (full addresses cited above and in the evidence log) but the
|
||||
surrounding static-initializer bookkeeping
|
||||
(`0x1014c1e8` flag, `0x10001d20`/`0x100562bf` calls) is generic AVS runtime boilerplate
|
||||
(locale/logging init guards) and was not further reverse engineered, as it does not affect the
|
||||
`gameMain` binding or argument semantics.
|
||||
- Whether `g_MainDllInfo.dllHMODULE` (`0x1014b9f8`) is ever reset or reused across multiple
|
||||
`dll_entry_main` invocations in one process lifetime was not traced beyond the single
|
||||
"set-once, warn-if-different-later" guard shown above; this task only needed the first,
|
||||
steady-state value.
|
||||
|
||||
## Evidence
|
||||
|
||||
Full `objdump -d` / `-p` / `-s` / `strings` output and the matching spice2x source excerpts are
|
||||
in `.omo/evidence/gamekdm-decomp-linux/task-5.log`.
|
||||
Reference in new issue
Block a user