Map the title image natively and bind imports to shims alongside per-function lifting #46

Closed
opened 2026-11-21 13:31:48 +00:00 by Leyukaka · 3 comments
Leyukaka commented 2026-11-21 13:31:48 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-11-21T13:31:48Z.


Question

Scarlett title code is already x64 in a PE32+ image. Could mapping the image with our own loader and binding its imports to Windows shims sit alongside per-function lifting, so that title code runs natively and static translation is kept for the functions that need patching or checking?

Most of the current CPU translators cover one function shape with a fixed prologue or ABI. The version-query lowerer in tools/ffx_version_query.py handles backward branches and a few xmm/ymm instructions, but it tracks only ZF/CF and works on hand-bound slices, with absolute addresses rebound through globals_. To lift a whole executable we would also need full flags, the rest of SSE/AVX, relocations, SEH/unwind and an indirect-call dispatcher.

With a custom loader the CPU executes the original instructions. The loader would then own:

  • section mapping, relocations at a non-preferred base, TLS callbacks, and .pdata registration (RtlAddFunctionTable) so that SEH and C++ exceptions unwind through the mapped image;
  • import binding for kernelx, xmem, d3d12_xs and so on to shim implementations, reusing the inventory and resolution chains from pe_inventory.py and dependency_paths.py;
  • a scan for instructions the desktop CPU may lack. Scarlett is Zen 2, so besides AVX2/BMI2 this means AMD-only ones such as SSE4a and CLZERO on Intel hosts.

Static translation stays useful for patching individual functions, for instrumenting guest code, and as a reference when checking a shim against the original behavior.

Evidence so far

None measured. This is a proposal. No existing issue or section of docs/experiments.md covers native mapping. XWine1 is publicly described as a translation layer for Xbox One titles, but its source is private. Its public DLL subset, SlimEra, states that on its own it does not run games, so I have not confirmed how XWine1 loads title images.

Relevant inputs already in the repo: the source-built import fixtures in tests/cpu/ (import.def, import_provider.cpp, named_provider.def, ordinal_provider.def) and the intake and dependency reports.

Completion condition

A CTest case, cpu_native_load or similar, that:

  1. maps the existing source-built import fixture image with a small loader (no LoadLibrary for the image itself), applies relocations at a non-preferred base and registers its .pdata;
  2. resolves its named and ordinal imports to a stand-in shim DLL;
  3. calls the same exports as cpu_aot_import_call / cpu_aot_ordinal_import and gets the same results as the lifted DLLs and the native originals;
  4. raises and catches an SEH exception inside a mapped image. The current fixtures have no handler, so this needs a new one.

A negative control should fail clearly when an import is left unresolved.

I can take parts 1 to 3 using synthetic fixtures only. If static translation is a hard requirement for reasons not covered in the README, I'd be glad to hear them before starting.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6529417407 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/46). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-11-21T13:31:48Z. --- ## Question Scarlett title code is already x64 in a PE32+ image. Could mapping the image with our own loader and binding its imports to Windows shims sit alongside per-function lifting, so that title code runs natively and static translation is kept for the functions that need patching or checking? Most of the current CPU translators cover one function shape with a fixed prologue or ABI. The version-query lowerer in `tools/ffx_version_query.py` handles backward branches and a few xmm/ymm instructions, but it tracks only ZF/CF and works on hand-bound slices, with absolute addresses rebound through `globals_`. To lift a whole executable we would also need full flags, the rest of SSE/AVX, relocations, SEH/unwind and an indirect-call dispatcher. With a custom loader the CPU executes the original instructions. The loader would then own: - section mapping, relocations at a non-preferred base, TLS callbacks, and `.pdata` registration (`RtlAddFunctionTable`) so that SEH and C++ exceptions unwind through the mapped image; - import binding for kernelx, xmem, d3d12_xs and so on to shim implementations, reusing the inventory and resolution chains from `pe_inventory.py` and `dependency_paths.py`; - a scan for instructions the desktop CPU may lack. Scarlett is Zen 2, so besides AVX2/BMI2 this means AMD-only ones such as SSE4a and CLZERO on Intel hosts. Static translation stays useful for patching individual functions, for instrumenting guest code, and as a reference when checking a shim against the original behavior. ## Evidence so far None measured. This is a proposal. No existing issue or section of `docs/experiments.md` covers native mapping. XWine1 is publicly described as a translation layer for Xbox One titles, but its source is private. Its public DLL subset, SlimEra, states that on its own it does not run games, so I have not confirmed how XWine1 loads title images. Relevant inputs already in the repo: the source-built import fixtures in `tests/cpu/` (`import.def`, `import_provider.cpp`, `named_provider.def`, `ordinal_provider.def`) and the intake and dependency reports. ## Completion condition A CTest case, `cpu_native_load` or similar, that: 1. maps the existing source-built import fixture image with a small loader (no `LoadLibrary` for the image itself), applies relocations at a non-preferred base and registers its `.pdata`; 2. resolves its named and ordinal imports to a stand-in shim DLL; 3. calls the same exports as `cpu_aot_import_call` / `cpu_aot_ordinal_import` and gets the same results as the lifted DLLs and the native originals; 4. raises and catches an SEH exception inside a mapped image. The current fixtures have no handler, so this needs a new one. A negative control should fail clearly when an import is left unresolved. I can take parts 1 to 3 using synthetic fixtures only. If static translation is a hard requirement for reasons not covered in the README, I'd be glad to hear them before starting. _AI-assisted._
R0Y00 commented 2026-11-24 06:47:33 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T13:42:00Z.


Thanks for offering to take this on, @Leyukaka. Please go ahead with items 1–3 in a first PR using the import fixtures built from this repository. Static recompilation remains the project's main direction. The native run gives us an independent reference for checking the translated calls and the shim.

For the named and ordinal cases, please run the same inputs through the Windows-loaded original, your fixture path and the recompiled DLL. Compare both the return value and the output written by the callee, and include the unresolved-import case in CTest. Please leave item 4 open for a separate fixture that actually raises and catches an exception. Looking forward to the comparison.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7063503414 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/46#issuecomment-7063503414). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T13:42:00Z. --- Thanks for offering to take this on, @Leyukaka. Please go ahead with items 1–3 in a first PR using the import fixtures built from this repository. Static recompilation remains the project's main direction. The native run gives us an independent reference for checking the translated calls and the shim. For the named and ordinal cases, please run the same inputs through the Windows-loaded original, your fixture path and the recompiled DLL. Compare both the return value and the output written by the callee, and include the unresolved-import case in CTest. Please leave item 4 open for a separate fixture that actually raises and catches an exception. Looking forward to the comparison.
R0Y00 commented 2026-11-24 06:47:33 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T14:38:30Z.


Implementation update: @Leyukaka’s #49 covers the mapping, import binding and comparison work for items 1–3. CI passed on b102794. The review comment asks for two error-path fixes and tests before merge. Keeping this issue open for the separate exception/unwind fixture; registering .pdata alone does not exercise an exception across a mapped frame.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7064241128 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/46#issuecomment-7064241128). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T14:38:30Z. --- Implementation update: @Leyukaka’s #49 covers the mapping, import binding and comparison work for items 1–3. CI passed on b102794. The [review comment](https://github.com/R0Y00/Xbox2Windows/pull/49#issuecomment-7063766093) asks for two error-path fixes and tests before merge. Keeping this issue open for the separate exception/unwind fixture; registering .pdata alone does not exercise an exception across a mapped frame.
R0Y00 commented 2026-11-24 06:47:34 +00:00 (Migrated from github.com)
Author
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T15:22:03Z.


Thanks for both contributions. #49 verified manual mapping, named and ordinal imports, relocations and the unresolved-import control. #65 has now merged after passing Windows CI and verifies exception recovery and propagation through a mapped fixture. Together they cover the four completion criteria.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7064818438 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/46#issuecomment-7064818438). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T15:22:03Z. --- Thanks for both contributions. #49 verified manual mapping, named and ordinal imports, relocations and the unresolved-import control. #65 has now merged after passing Windows CI and verifies exception recovery and propagation through a mapped fixture. Together they cover the four completion criteria.
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows#46
No description provided.