Find TLS and delay-import directories by address in pe_map #103

Manually merged
github merged 3 commits from gh-pr/103/head into gh-pr/103/base 2026-11-26 21:27:06 +00:00
Owner

@⁠Vonsek on GitHub · 2026-11-21 15:59 UTC · original
Vonsek:pe-map-tls-delay-dirs → main · +112 −7 · 4 files · 3 commits · merged 2026-11-21 17:07 UTC

Change

pe_map_image tested only Size for the TLS and delay-import directories. Measured on Windows 11 build 26200, the loader finds the TLS directory by its address:

TLS directory (as built: Size 40) LoadLibrary pe_map before pe_map now
Size 0 callback, then entry point entry point only (callback skipped) callback, then entry point
Size 1 or 39 callback, then entry point format: "TLS directory outside image" callback, then entry point
address 0, Size 40 entry point only unsupported: "TLS data" (read the DOS header) entry point only
address past the image, or ending past it fails, error 998 format: "TLS directory outside image" same

The loader does not read the delay-import directory at all: copies of the named import fixture with an empty table, address 0 with Size 32, an address past the image, or a descriptor that names a DLL all load. pe_map now separates these cases instead of testing Size:

Delay-import directory LoadLibraryExW pe_map before pe_map now
empty table, Size 0 loads maps maps
empty table, Size 32 loads unsupported: "delay imports" maps
address 0, Size 32 loads unsupported: "delay imports" maps
address past the image loads unsupported (Size 32) or maps (Size 0) format: "delay import directory outside image"
descriptor naming a DLL, Size 0 loads maps unsupported: "delay imports"

So TLS is present when its address is set and must fit in the image; supported TLS callbacks keep running, and TLS data templates stay unsupported. Malformed directory metadata is a format error, distinct from supported TLS and from unsupported delay imports. Refs #46.

Verification

Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, at 01aa32f (on main a2a62ee, with #78):

ctest --preset dev --no-tests=error -R '^cpu_native_' -V
cpu_native_map: Windows loads every delay-import variant; pe_map maps an address of 0 and an empty table, and rejects a directory past the image (format) and a descriptor naming a DLL (unsupported)
cpu_native_tls: TLS directory with Size 0, 1 or 39: callback and entry point run under Windows and pe_map; address 0: entry point only under both; a directory past the image fails both loads
4/4 passed (cpu_native_map, cpu_native_seh, cpu_native_section, cpu_native_tls)

The existing #78 TLS checks (notification order, FALSE entry point, rejections, non-DLL copies, release after the callback list changes) pass unchanged. Against main's pe_map, cpu_native_tls fails at "TLS Size 0" (callback missing under pe_map) and cpu_native_map fails at "delay empty table, Size 32". The full ctest --preset dev --no-tests=error passed 78/78 (bvh_wave64_hardware and bvh_wave64_warp skipped, lane range 32..32).

Evidence and follow-up

No new fixtures. The TLS cases patch temporary copies of cpu_tls_original and compare LoadLibrary with pe_map in a child process per load, using the existing notification sink. The delay cases patch temporary copies of cpu_import_original, which has no entry point, so LoadLibraryExW runs nothing in the image. The empty table is the zero-filled tail of the .rdata page; the descriptor that names a DLL is the import descriptor read at offset 8, whose ForwarderChain and Name line up with Attributes and DllNameRVA.

Assumption: an empty delay table is recognised by a zero DllNameRVA in the first descriptor. #85 changes the ordinary import walk in the same file; the two touch different blocks, and whichever lands second needs a small rebase in tests/cpu/native_map_check.py. Docs: the native mapping and TLS paragraphs in docs/experiments.md.

**@⁠Vonsek** on GitHub · 2026-11-21 15:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/103) `Vonsek:pe-map-tls-delay-dirs` → `main` · +112 −7 · 4 files · 3 commits · merged 2026-11-21 17:07 UTC ## Change `pe_map_image` tested only `Size` for the TLS and delay-import directories. Measured on Windows 11 build 26200, the loader finds the TLS directory by its address: | TLS directory (as built: Size 40) | `LoadLibrary` | `pe_map` before | `pe_map` now | |---|---|---|---| | Size 0 | callback, then entry point | entry point only (callback skipped) | callback, then entry point | | Size 1 or 39 | callback, then entry point | format: "TLS directory outside image" | callback, then entry point | | address 0, Size 40 | entry point only | unsupported: "TLS data" (read the DOS header) | entry point only | | address past the image, or ending past it | fails, error 998 | format: "TLS directory outside image" | same | The loader does not read the delay-import directory at all: copies of the named import fixture with an empty table, address 0 with Size 32, an address past the image, or a descriptor that names a DLL all load. `pe_map` now separates these cases instead of testing `Size`: | Delay-import directory | `LoadLibraryExW` | `pe_map` before | `pe_map` now | |---|---|---|---| | empty table, Size 0 | loads | maps | maps | | empty table, Size 32 | loads | unsupported: "delay imports" | maps | | address 0, Size 32 | loads | unsupported: "delay imports" | maps | | address past the image | loads | unsupported (Size 32) or maps (Size 0) | format: "delay import directory outside image" | | descriptor naming a DLL, Size 0 | loads | maps | unsupported: "delay imports" | So TLS is present when its address is set and must fit in the image; supported TLS callbacks keep running, and TLS data templates stay unsupported. Malformed directory metadata is a format error, distinct from supported TLS and from unsupported delay imports. Refs #46. ## Verification Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, at 01aa32f (on main a2a62ee, with #78): ``` ctest --preset dev --no-tests=error -R '^cpu_native_' -V cpu_native_map: Windows loads every delay-import variant; pe_map maps an address of 0 and an empty table, and rejects a directory past the image (format) and a descriptor naming a DLL (unsupported) cpu_native_tls: TLS directory with Size 0, 1 or 39: callback and entry point run under Windows and pe_map; address 0: entry point only under both; a directory past the image fails both loads 4/4 passed (cpu_native_map, cpu_native_seh, cpu_native_section, cpu_native_tls) ``` The existing #78 TLS checks (notification order, FALSE entry point, rejections, non-DLL copies, release after the callback list changes) pass unchanged. Against main's `pe_map`, `cpu_native_tls` fails at "TLS Size 0" (callback missing under `pe_map`) and `cpu_native_map` fails at "delay empty table, Size 32". The full `ctest --preset dev --no-tests=error` passed 78/78 (`bvh_wave64_hardware` and `bvh_wave64_warp` skipped, lane range 32..32). ## Evidence and follow-up No new fixtures. The TLS cases patch temporary copies of `cpu_tls_original` and compare `LoadLibrary` with `pe_map` in a child process per load, using the existing notification sink. The delay cases patch temporary copies of `cpu_import_original`, which has no entry point, so `LoadLibraryExW` runs nothing in the image. The empty table is the zero-filled tail of the `.rdata` page; the descriptor that names a DLL is the import descriptor read at offset 8, whose `ForwarderChain` and `Name` line up with `Attributes` and `DllNameRVA`. Assumption: an empty delay table is recognised by a zero `DllNameRVA` in the first descriptor. #85 changes the ordinary import walk in the same file; the two touch different blocks, and whichever lands second needs a small rebase in `tests/cpu/native_map_check.py`. Docs: the native mapping and TLS paragraphs in `docs/experiments.md`.
pe_map tested only Size for the TLS and delay-import directories. The
Windows loader finds the TLS directory by its address: on build 26200 a
copy with Size 0, 1 or 39 still runs its callback, one with address 0
is ignored whatever its Size, and one past the image fails the load.
pe_map skipped the callback for Size 0, rejected Size 1 and 39, and read
the DOS header as TLS data when only the address was 0. The loader does
not read the delay-import directory at all.

TLS is now present when its address is set and must fit in the image;
supported callbacks keep running. A delay-import directory with address
0 or an empty table maps, one outside the image is a format error and
one that names a DLL stays unsupported.

cpu_native_tls compares the TLS variants with LoadLibrary in child
processes. cpu_native_map loads each delay-import variant with
LoadLibraryExW and checks the pe_map status.

Refs #46
# Conflicts:
#	docs/experiments.md
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:27:05 +00:00
github manually merged commit 5623001c1f into gh-pr/103/base 2026-11-26 21:27:06 +00:00
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-21 16:23 UTC · original

Thanks for finding the zero-size case. #78 has now landed. Please update on main and keep supported TLS callbacks working; distinguish malformed directory metadata from supported TLS and from unsupported delay imports. Include Windows comparisons for the disputed address/size cases.

**@⁠R0Y00** on GitHub · 2026-11-21 16:23 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/103#issuecomment-7065609475) Thanks for finding the zero-size case. #78 has now landed. Please update on main and keep supported TLS callbacks working; distinguish malformed directory metadata from supported TLS and from unsupported delay imports. Include Windows comparisons for the disputed address/size cases.
Author
Owner

@⁠Vonsek on GitHub · 2026-11-21 16:59 UTC · original

Updated on main with #78 (01aa32f):

  • Supported TLS callbacks keep working. The TLS directory is now found by its address, as the loader does: copies with Size 0, 1 or 39 run the callback and then the entry point under both LoadLibrary and pe_map, and with the address set to 0 only the entry point runs under both. Before, pe_map skipped the callback for Size 0, rejected Size 1 and 39, and reported "TLS data" for address 0 because it read the DOS header.
  • Malformed metadata is now a format error, separate from supported TLS and from unsupported delay imports: a TLS directory that does not fit in the image ("TLS directory outside image"; Windows fails with error 998) and a delay-import directory outside the image ("delay import directory outside image").
  • Delay imports: the loader does not read that directory at all. All five patched copies load under LoadLibraryExW. pe_map maps an address of 0 and an empty table, and keeps a descriptor that names a DLL unsupported.

The Windows comparisons for each address/size case are in cpu_native_tls and cpu_native_map, with before/after tables in the description. cpu_native_* 4/4 and the full suite 78/78 pass on Windows 11 26200.9974.

**@⁠Vonsek** on GitHub · 2026-11-21 16:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/103#issuecomment-7066117202) Updated on main with #78 (01aa32f): - Supported TLS callbacks keep working. The TLS directory is now found by its address, as the loader does: copies with `Size` 0, 1 or 39 run the callback and then the entry point under both `LoadLibrary` and `pe_map`, and with the address set to 0 only the entry point runs under both. Before, `pe_map` skipped the callback for Size 0, rejected Size 1 and 39, and reported "TLS data" for address 0 because it read the DOS header. - Malformed metadata is now a format error, separate from supported TLS and from unsupported delay imports: a TLS directory that does not fit in the image ("TLS directory outside image"; Windows fails with error 998) and a delay-import directory outside the image ("delay import directory outside image"). - Delay imports: the loader does not read that directory at all. All five patched copies load under `LoadLibraryExW`. `pe_map` maps an address of 0 and an empty table, and keeps a descriptor that names a DLL unsupported. The Windows comparisons for each address/size case are in `cpu_native_tls` and `cpu_native_map`, with before/after tables in the description. `cpu_native_*` 4/4 and the full suite 78/78 pass on Windows 11 26200.9974.
This discussion has been locked. Commenting is limited to contributors.
No description provided.