Skip to content

runtime: opt-in compat-module directory (LEONIDA_COMPAT_DIR) for console DLLs with no shim - #8969

Closed
Rafa23xz wants to merge 1 commit into
ProjectLeonida:mainfrom
Rafa23xz:rt-compat-dir
Closed

Rafa23xz wants to merge 1 commit into
ProjectLeonida:mainfrom
Rafa23xz:rt-compat-dir

Conversation

@Rafa23xz

Copy link
Copy Markdown
Contributor

Claim: #8683 (comment) (new item: opt-in compat-module directory)

What

Loader::resolve gets one opt-in step. When LEONIDA_COMPAT_DIR names a folder and an imported DLL is a file there, Windows loads that DLL from its full path (LoadLibraryExW, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32, never the game folder) and its exports serve the import. Being a host load, it takes no slot of the mapper's static TLS.

  • The step comes before shims, bundled modules and the host, and the DLL owns every import of its module: a symbol it lacks binds to the unimplemented thunk, not to a shim, so one console DLL is never served half by a shim and half by the drop-in (they would not share state).
  • Bindings are reported as host; [rt] compat: <dll> -> <path> is printed once per DLL.
  • A file in the folder that Windows refuses to load is an error, not a silent skip.
  • Unset or empty: no behaviour change. Only static imports go through this step; run-time LoadLibrary of the same names does not yet (follow-up).
  • Documented in runtime/README.md, "Compat modules".

Why

On main, gta6rt --dry-run over the 1.0.2.1 executable leaves 20 imports unimplemented: AcpHal 9, chad 5, xapu 5, XCRDAPI 1 (matches #8280 minus the two xg_xs imports served since). omonkeyz/xbox-emulator (GPL-3.0-or-later) has full implementations of all 20 as cdylibs with the console names and exports. This PR only adds the loader seam and copies no code; whether to use those DLLs stays a separate choice.

Does not overlap #6885 / #6981 / #7009 (C++ stubs: with the variable unset they bind exactly as before), the GameInput ordinal PRs, or stop 13.

Measured (own 1.0.2.1 copy, nothing of it in this PR)

Run Result
--dry-run, variable unset shim=63 host=432 module=72 unimplemented=20 (unchanged from main)
--dry-run, folder with the four xbox-emulator crates built at f89e410 shim=63 host=452 module=72 unimplemented=0; the four package DLLs still map

Caveat, also in the README: with their defaults, those crates run static initializers at load (.CRT$XCU) and one of them hooks the process, after which the package DLLs mapped later fail to map (module=0). With TOMBOX_GDK_EARLY=0 TOMBOX_GDK_RUNTIME=0 TOMBOX_OSHOST=0 TOMBOX_PAD=0 TOMBOX_TIMER_1MS=0 they map; their network guard stays on and denies all traffic. --run still stops at the XMem stop of the retail boot chain (stops 1/2), with or without the folder; the crash address there varies run to run in both configurations.

Tests

  • loader_test: 93 checks, 0 failed, including five new cases with a synthetic OS-loadable DLL: unset leaves the import unimplemented; set binds and calls the export (returns 42); a compat DLL lacking a symbol does not fall back to its shim; a DLL absent from the folder still binds to its shim; an unloadable file is reported.
  • ctest (full rebuild): 112/113 pass. The one failure, root_signature_oracle, fails identically on main on this machine (Windows 10 19045's D3D12 serializer rejects root-signature flags 0xc7f).
  • scripts/check_no_game_data.py on the four changed files: clean.

Windows 10 Pro 19045, MSVC 2022 Build Tools (RelWithDebInfo), NVIDIA GTX 1070 Ti, driver 582.78 (no GPU code is touched).

AI-assisted (Claude Code).

When LEONIDA_COMPAT_DIR names a folder, an import whose DLL is a file
there binds to that DLL's export. Windows loads it from the full path
(LoadLibraryExW, DLL-load-dir + System32 search, never the game folder),
so it takes no manual static-TLS slot. The step comes before shims,
bundled modules and the host, and the DLL owns all imports of its
module: a missing symbol binds to the unimplemented thunk, not a shim.
Unset or empty: no change.

Measured on the 1.0.2.1 executable: --dry-run goes from 20 unimplemented
imports (AcpHal 9, chad 5, xapu 5, XCRDAPI 1) to 0 with the four
matching omonkeyz/xbox-emulator crates in the folder (and their
TOMBOX_* static initializers turned off; see runtime/README.md).

loader_test: five cases with a synthetic OS-loadable DLL (no game data).

AI-assisted (Claude Code).

@abnalstlll-byte abnalstlll-byte left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Windows host review (commit \7fc6fa72851362875b7105087030ac651db92b35)

What changed:


  • untime/src/pe_loader.cpp:
    • Introduces \LEONIDA_COMPAT_DIR\ environment variable check during \Loader::resolve.
    • When set, attempts to load matching console DLLs via \LoadLibraryExW\ with \LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32.
    • Strict contract preservation: if a compat DLL is loaded, it owns all entry points for that module (unresolved exports bind to the unimplemented stub rather than cross-polluting with a shim).

  • untime/test/loader_test.cpp:
    • Tests covering opt-in loading, missing DLL fallback, and shim isolation.

Verification:

  • Verified on Windows 11 host with MSVC / C++17.
  • \pe_loader\ contract remains intact; no state splitting between host and shim.
  • Clean diff: pure standard library / Win32 APIs, no game files.

LGTM!

@casper64114

Copy link
Copy Markdown

Reproduced a Windows failure for a relative LEONIDA_COMPAT_DIR:

  1. Use the existing synthetic testcompat.dll fixture in a compatibility folder.
  2. Set LEONIDA_COMPAT_DIR to that folder's path relative to the process working directory.
  3. Resolve a synthetic import of TestCompat.dll!Answer.

The file check succeeds, but LoadLibraryExW(..., LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32) fails with error 87. The same fixture succeeds when the folder path is absolute. I added this as a temporary regression case to loader_test; it failed on this PR, while the unmodified test suite passes 93/93 on Windows 11 with MSVC 19.44. Microsoft's API docs specify that LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR requires a fully qualified lpFileName. Resolving the configured directory to an absolute path before loading should preserve the safe search flags and make relative paths work.

@GeremC

GeremC commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested 7fc6fa7 merged with main 5d74c27 under Wine: pass

On: Linux, Ubuntu 24.04, cross-built with clang-cl 19.1.1 + lld-link + xwin SDK (RelWithDebInfo), run through Wine 9.0; CPU only, no game files.

  • ctest -R ^loader$: passed.
  • Negative control, runtime/src/pe_loader.cpp and .h from main with the PR's test kept: loader fails.

AI-assisted (Claude Code).

@sgonzalez-glitch

Copy link
Copy Markdown

Tested 7fc6fa728 (as part of #9158, #9093 and #8969 merged together onto main 6312c057f) on NVIDIA GeForce RTX 2080, driver 621.24 (32.0.16.2124), Windows 11, MSVC 19.44 RelWithDebInfo: pass.

AI-assisted: yes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants