Files
kdm-re/docs/pipeline.md
T
ulw-execute a176c63736 docs: scope spec + RE to the official clean release only
Reference only the official KDM-2016021900 game: the 9 Konami modules in
contents/modules/ plus data/arkdata/prop. Remove community/PC-hardware glue
(deahook/DEALibacio, spice2x/spicetools, launcher.exe, gamestart.bat,
bemanitools, NVENC/CUDA stubs) and non-release modules (kmd/ksm/kuc/libinput/
libacio_mbr) as game evidence. Correct the "22-DLL" framing to the real
9-module set; reframe the boot chain on arkkdm.dll's own dll_entry_init/
dll_entry_main -> gameMain binding. Kinect10.dll referenced only as gamekdm's
external Microsoft NUI runtime dependency. Fix ea3-config.xml -> eamuse-config.xml.
Per-byte sizes/RVAs from the earlier contaminated install marked UNVERIFIED
pending re-check against the clean-release binary.
2026-11-28 20:37:18 -05:00

9.8 KiB

Build and verification pipeline

Zero-to-built, zero-to-verified guide for gamekdm.dll on a standard Linux machine. It takes a clean checkout to a rebuilt 32-bit Windows DLL plus a full pass of the verification harness, with no sudo and no system package install.

Everything here was dry-run on a clean clone before it was written; the recorded transcript is in .omo/evidence/gamekdm-decomp-linux/task-18.log.

Who this is for

Someone who is not the author, on their own Linux box, who wants to reproduce the build and confirm the reconstruction matches the shipping binary. You do not need root. You do need the few host prerequisites below.

0. Host prerequisites

These are assumed already present on the machine (they are not part of the no-sudo toolchain and are not installed by this guide):

  • git, bash, cmake (>= 3.16), and a C/C++ host compiler.
  • User-space Nix with flakes, reachable at ~/.nix-profile. If Nix is not yet installed, use the single-user (rootless) installer; it needs no root after setup. See https://nixos.org/download (single-user install).
  • uv (the Python environment manager) on PATH. Install no-sudo with curl -LsSf https://astral.sh/uv/install.sh | sh, which drops it in ~/.local/bin. uv is used instead of python3 -m venv because it bundles its own pip and its own pinned CPython, so it works even when the system Python has no ensurepip.
  • The shipping original gamekdm.dll, readable at /home/athapa/ghidra/gamekdm.dll. The Unicorn differential (step 5) emulates this binary. If your copy lives elsewhere, edit the DLL = Path(...) line near the top of tools/unicorn_diff/prove_t11.py.
  • The legally-possessed game content folder, for the companion DLLs that step 2 reads. The default is /mnt/f/Users/Aaron Thapa/Downloads/KDM-2016021900/contents. Pass your own path as the first argument to tools/gen_importlibs.sh if it differs.

All toolchain versions are pinned in toolchain.lock (nixpkgs rev 619c39f6eac5d1ed6b8f073a9414baf7034b2356, GCC 16.2.0 / mingw-w64 14.0.0, wine 11.0).

1. Get a clean checkout

git clone <this-repo> kdm-re
cd kdm-re

2. Install the pinned no-sudo toolchain (T1)

Source the user-space Nix profile, then install the exact pinned set. All of it lands in ~/.nix-profile; nothing touches the system.

source ~/.nix-profile/etc/profile.d/nix.sh

NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install \
  github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#pkgsCross.mingw32.buildPackages.binutils
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install --priority 11 \
  github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#pkgsCross.mingw32.buildPackages.gcc
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install \
  github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#gendef
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install \
  github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#pkgsCross.mingw32.windows.mcfgthreads
NIX_CONFIG="extra-experimental-features = nix-command flakes" \
nix profile install \
  github:NixOS/nixpkgs/619c39f6eac5d1ed6b8f073a9414baf7034b2356#wine

gcc installs last with --priority 11 on purpose: it and binutils both ship a conflicting i686-w64-mingw32-nm symlink, so binutils' copy (default priority 10) must win while gcc's unique files still link in.

Now put the cross toolchain on PATH. Source this once per shell:

source ./activate.sh

activate.sh sources the Nix profile, prepends ~/.nix-profile/bin and a generated worktree-local bin/ to PATH, and creates the bare dlltool, gendef, and objdump aliases that point at their target-prefixed binaries. On success it is silent and returns 0. If a tool is missing it prints exact recovery steps and returns non-zero, so a broken toolchain never looks like a clean activation. Verify:

command -v i686-w64-mingw32-g++ gendef dlltool wine

3. Generate companion import libraries (T3)

The reconstructed slice links against MinGW import libs derived from the game's own companion DLLs. Regenerate them (the committed tree ships the .def files but not the .dll.a archives, which are gitignored):

tools/gen_importlibs.sh
# or, if your game content lives elsewhere:
# tools/gen_importlibs.sh "/path/to/KDM-.../contents"

This runs gendef then i686-w64-mingw32-dlltool for the seven shipped Konami modules in its current map and the external Microsoft Kinect10.dll NUI runtime dependency of gamekdm.dll, writing re/importlibs/lib<name>.dll.a. arkkdm and external Kinect10 get --kill-at; see docs/companion-conventions.md for why. The script currently expects all inputs in one directory, so these historical commands need adjustment for the clean-release layout: the nine Konami modules are in contents/modules/ and the external NUI runtime is supplied separately. See the official inventory.

4. Build the proxy and reconstructed slice (T4 + T11)

Configure against the pinned cross toolchain file, then build:

cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain-mingw-i686.cmake
cmake --build build

Output is build/gamekdm.dll, a PE32 (i386) DLL. The toolchain file links -static -static-libgcc -static-libstdc++ so the rebuild imports zero MinGW runtime DLLs, matching the original's import shape. Confirm the export table is exactly gameMain at ordinal 1:

i686-w64-mingw32-objdump -p build/gamekdm.dll | grep -A6 'Export Address Table'

5. Run the verification harness

5a. Unicorn differential (T8 + T11)

This is the universal gate: for each reconstructed function it emulates the real routine inside the shipping gamekdm.dll (Unicorn) and runs the compiled C++ reconstruction under wine on the same inputs, then asserts behavioural equality. It also injects a known bug and confirms the differential catches it (non-vacuity).

Create the Python environment with uv and install the pinned deps:

uv venv tools/unicorn_diff/.venv
VIRTUAL_ENV=tools/unicorn_diff/.venv uv pip install -r tools/unicorn_diff/requirements.txt

Build the reconstruction harness into a 32-bit wine executable. (Keep these flags exact: -mfpmath=sse -msse2 -ffp-contract=off -fno-fast-math reproduce the original's float semantics.)

i686-w64-mingw32-g++ -std=c++20 -O2 -mfpmath=sse -msse2 -ffp-contract=off -fno-fast-math \
  -Iinclude -Isrc -Isrc/game \
  tools/unicorn_diff/recon_harness.cpp \
  src/game/box_distance.cpp src/game/validity.cpp src/game/nui_elevation.cpp \
  -L/nix/store/g72bh3mrbh6gkh59axd2199js9kccxfs-mcfgthread-i686-w64-mingw32-2.5.1/lib \
  -o tools/unicorn_diff/recon.exe -static -static-libgcc -static-libstdc++

Run the proof (expect ALL DIFFERENTIALS PASS, exit 0):

tools/unicorn_diff/.venv/bin/python tools/unicorn_diff/prove_t11.py

5b. Golden I/O harness (T9)

A standalone, stdlib-only supplement that replays committed input/output tuples against an independent reconstruction and proves the assertions can fail (perturbation guard). No game binary, no network, no wine.

tests/golden/run.sh

Expect GOLDEN HARNESS OK: clean run passed and every perturbation failed., exit 0.

5c. Wine load-smoke

Confirm the rebuilt DLL actually loads under wine and still exports gameMain. Build a tiny loader, drop it beside the rebuilt DLL, and run it:

MCFG=/nix/store/g72bh3mrbh6gkh59axd2199js9kccxfs-mcfgthread-i686-w64-mingw32-2.5.1/lib
SM=$(mktemp -d)
cat > "$SM/loadsmoke.c" <<'EOF'
#include <windows.h>
#include <stdio.h>
int main(void){
  HMODULE h = LoadLibraryA("gamekdm.dll");
  if(!h){ printf("LOAD_SMOKE: LoadLibraryA failed err=%lu\n",(unsigned long)GetLastError()); return 1; }
  FARPROC p = GetProcAddress(h, "gameMain");
  if(!p) p = GetProcAddress(h, (LPCSTR)1);
  printf("LOAD_SMOKE: loaded gamekdm.dll at %p, gameMain=%p\n",(void*)h,(void*)p);
  return p ? 0 : 2;
}
EOF
i686-w64-mingw32-gcc "$SM/loadsmoke.c" -L"$MCFG" -o "$SM/loadsmoke.exe"
cp build/gamekdm.dll "$SM/gamekdm.dll"
( cd "$SM" && WINEDEBUG=-all wine loadsmoke.exe )
rm -rf "$SM"

Expect a LOAD_SMOKE: loaded gamekdm.dll at ... line with a non-null gameMain, exit 0.

The Phase-0 gameMain is a forwarding proxy: it only resolves the original gamekdm_orig.dll when actually called, so loading the rebuilt DLL on its own is a valid smoke test and needs no original present.

6. Deploy to the cabinet/original-binary runtime (optional, deferred)

In-game QA on the cabinet/original-binary runtime is a later integration milestone and is not part of the reproducible Linux build above. When it lands: rename build/gamekdm.dll for the reconstruction cutover, keep the original beside it as gamekdm_orig.dll for the proxy to forward into, and load it through the cabinet/original-binary runtime. Treat this as deferred; the steps 2 through 5 chain is the complete, self-contained verification.

One-shot reference

With the toolchain already installed (step 2), a full rebuild-and-verify from a clean checkout is:

source ~/.nix-profile/etc/profile.d/nix.sh
source ./activate.sh
tools/gen_importlibs.sh
cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=cmake/toolchain-mingw-i686.cmake
cmake --build build
uv venv tools/unicorn_diff/.venv
VIRTUAL_ENV=tools/unicorn_diff/.venv uv pip install -r tools/unicorn_diff/requirements.txt
i686-w64-mingw32-g++ -std=c++20 -O2 -mfpmath=sse -msse2 -ffp-contract=off -fno-fast-math \
  -Iinclude -Isrc -Isrc/game \
  tools/unicorn_diff/recon_harness.cpp \
  src/game/box_distance.cpp src/game/validity.cpp src/game/nui_elevation.cpp \
  -L/nix/store/g72bh3mrbh6gkh59axd2199js9kccxfs-mcfgthread-i686-w64-mingw32-2.5.1/lib \
  -o tools/unicorn_diff/recon.exe -static -static-libgcc -static-libstdc++
tools/unicorn_diff/.venv/bin/python tools/unicorn_diff/prove_t11.py
tests/golden/run.sh