Bind delay imports through the resolver in pe_map #126

Merged
dcastell1 merged 2 commits from upstream-pr/127 into main 2026-11-21 17:55:55 +00:00
dcastell1 commented 2026-11-21 16:40:29 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by dcastell1 on 2026-11-21T16:40:29Z.


Change

pe_map_image rejected every image with a delay-import directory. It now resolves each IMAGE_DELAYLOAD_DESCRIPTOR entry through the same host resolver as ordinary imports and writes the bound address into the descriptor's IAT.

The PE loader does not resolve delay imports; the image's own helper does, with LoadLibrary and GetProcAddress. pe_map has neither and must bind through the host resolver, so it binds at map time. This is a deliberate difference from Windows: an absent provider fails the map with MAP_UNRESOLVED_IMPORT, where Windows maps and fails on the first call. The test documents it. Descriptors must set dlattrRva (bit 0); old-style virtual-address descriptors are rejected as unsupported.

The delay directory is validated before use: address and size in the image, at least one descriptor, DLL name inside the image, name table and IAT present, each thunk and IAT slot inside the image. A normal import IAT write may not overlap the delay directory, because the delay walk happens after the normal bindings.

Test

New cpu_native_delay with delay_route, which imports delayed_value by name and delayed_ordinal by ordinal 2 from cpu_delay_provider.dll using /DELAYLOAD. It compares the mapped calls with the Windows delay helper, checks both IAT slots before the calls, binds an altered provider to change both results, and maps without the provider to check the rejection. Damaged copies clear dlattrRva, move the DLL name outside the image, zero the name table, move the IAT outside the image, or shrink the directory to zero or below one descriptor; all are rejected before the provider is requested.

On Windows 11 build 26300.9974, VS 2022 17.14 (MSVC 14.44), Python 3.13:

ctest --preset dev --no-tests=error -R '^(cpu_native_map|cpu_native_seh|cpu_native_tls|cpu_native_delay)$' -V

4/4 pass.

Refs #46.

Note for #103: this removes the delay imports rejection line that #103 also edits. A zero-size delay directory is rejected here as MAP_FORMAT with "delay import directory smaller than one descriptor", which is more specific than the unsupported status. The TLS zero-size rule in #103 is independent.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531550021 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/127). Originally posted by [dcastell1](https://github.com/dcastell1) on 2026-11-21T16:40:29Z. --- ## Change `pe_map_image` rejected every image with a delay-import directory. It now resolves each `IMAGE_DELAYLOAD_DESCRIPTOR` entry through the same host resolver as ordinary imports and writes the bound address into the descriptor's IAT. The PE loader does not resolve delay imports; the image's own helper does, with `LoadLibrary` and `GetProcAddress`. `pe_map` has neither and must bind through the host resolver, so it binds at map time. This is a deliberate difference from Windows: an absent provider fails the map with `MAP_UNRESOLVED_IMPORT`, where Windows maps and fails on the first call. The test documents it. Descriptors must set `dlattrRva` (bit 0); old-style virtual-address descriptors are rejected as unsupported. The delay directory is validated before use: address and size in the image, at least one descriptor, DLL name inside the image, name table and IAT present, each thunk and IAT slot inside the image. A normal import IAT write may not overlap the delay directory, because the delay walk happens after the normal bindings. ## Test New `cpu_native_delay` with `delay_route`, which imports `delayed_value` by name and `delayed_ordinal` by ordinal 2 from `cpu_delay_provider.dll` using `/DELAYLOAD`. It compares the mapped calls with the Windows delay helper, checks both IAT slots before the calls, binds an altered provider to change both results, and maps without the provider to check the rejection. Damaged copies clear `dlattrRva`, move the DLL name outside the image, zero the name table, move the IAT outside the image, or shrink the directory to zero or below one descriptor; all are rejected before the provider is requested. On Windows 11 build 26300.9974, VS 2022 17.14 (MSVC 14.44), Python 3.13: `ctest --preset dev --no-tests=error -R '^(cpu_native_map|cpu_native_seh|cpu_native_tls|cpu_native_delay)$' -V` 4/4 pass. Refs #46. Note for #103: this removes the `delay imports` rejection line that #103 also edits. A zero-size delay directory is rejected here as `MAP_FORMAT` with "delay import directory smaller than one descriptor", which is more specific than the unsupported status. The TLS zero-size rule in #103 is independent.
upstream-sync closed this pull request 2026-11-24 06:41:26 +00:00
R0Y00 commented 2026-11-24 06:47:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:16:40Z.


Please sync with main and reconcile the delay-directory handling from #103. Preserve its empty-directory and malformed-directory controls and add the new binding cases alongside them.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066352288 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/127#issuecomment-7066352288). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:16:40Z. --- Please sync with main and reconcile the delay-directory handling from #103. Preserve its empty-directory and malformed-directory controls and add the new binding cases alongside them.
R0Y00 commented 2026-11-24 06:47:42 +00:00 (Migrated from github.com)

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


Thanks for incorporating the #103 path in 56184a1. GitHub still reports a merge conflict against current main. Please reconcile with the merged ordinary import walk from #85 and preserve its walked-descriptor IAT overlap protection, then run the normal and delay-import tests on the same head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066444999 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/127#issuecomment-7066444999). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:23:22Z. --- Thanks for incorporating the #103 path in 56184a1. GitHub still reports a merge conflict against current main. Please reconcile with the merged ordinary import walk from #85 and preserve its walked-descriptor IAT overlap protection, then run the normal and delay-import tests on the same head.
R0Y00 commented 2026-11-24 06:47:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:26:55Z.


The latest 2dc4694 does preserve both walked descriptor ranges. One remaining validation issue in the new delay path: a named thunk is masked with 0x7fffffff before the RVA bounds check, so an out-of-range value can alias a valid low RVA. Please reject invalid high bits before narrowing and cover the rejection with a source-built control.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066489087 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/127#issuecomment-7066489087). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:26:55Z. --- The latest 2dc4694 does preserve both walked descriptor ranges. One remaining validation issue in the new delay path: a named thunk is masked with `0x7fffffff` before the RVA bounds check, so an out-of-range value can alias a valid low RVA. Please reject invalid high bits before narrowing and cover the rejection with a source-built control.
dcastell1 commented 2026-11-24 06:47:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by dcastell1 on 2026-11-21T17:32:15Z.


Handled in ee269b8: name_rva keeps all 64 bits until name_rva >> 31 rejects bits 31..62, then image.at<WORD> bounds-checks the low RVA. The new control sets bit 40 in the named thunk of the source-built delay fixture and expects MAP_FORMAT "delay import name for cpu_delay_provider.dll outside image". cpu_native_map, cpu_native_seh, cpu_native_tls and cpu_native_delay pass on the same head (Windows 11 build 26300.9974, MSVC 14.44).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066555772 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/127#issuecomment-7066555772). Originally posted by [dcastell1](https://github.com/dcastell1) on 2026-11-21T17:32:15Z. --- Handled in ee269b8: `name_rva` keeps all 64 bits until `name_rva >> 31` rejects bits 31..62, then `image.at<WORD>` bounds-checks the low RVA. The new control sets bit 40 in the named thunk of the source-built delay fixture and expects `MAP_FORMAT` "delay import name for cpu_delay_provider.dll outside image". `cpu_native_map`, `cpu_native_seh`, `cpu_native_tls` and `cpu_native_delay` pass on the same head (Windows 11 build 26300.9974, MSVC 14.44).
Sign in to join this conversation.
No reviewers
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!126
No description provided.