pe: pe_map zero-fills raw section data past VirtualSize that Windows maps up to the page end #2525

Closed
opened 2026-11-25 20:14:30 +00:00 by Asimmimx · 0 comments
Asimmimx commented 2026-11-25 20:14:30 +00:00 (Migrated from github.com)
Contributor

Imported from R0Y00/Xbox2Windows. Originally posted by Asimmimx on 2026-11-25T20:14:30Z.


Commit or build

main at 66da8c4f, dev preset, Windows 11 build 26300.

Summary

When a section's SizeOfRawData is larger than its VirtualSize, the Windows loader maps the section's file sectors up to the end of the section's last page, so raw bytes past VirtualSize can be read at their RVAs. pe_map_image (tools/pe_map.cpp) stops the copy at VirtualSize and zero-fills the rest of that page. For such an image, the mapped section bytes differ from a LoadLibraryExW load of the same file. This breaks the byte-for-byte section equality with the Windows loader that cpu_native_section checks.

Linkers zero-pad the bytes past VirtualSize, so ordinary linker output is not affected. The difference appears when those bytes are nonzero: a post-processed or packed image, a tool that trims VirtualSize, or a VirtualSize of 0 with an unaligned SizeOfRawData. Two existing cases in tests/cpu/native_section_check.py ("start inside a sector" and "start in the first sector") already differ in the page tail. The test compares only up to VirtualSize, so it does not see the difference.

Reproduction

Build pe_map and cpu_register_probe (cmake --preset dev).

  1. Copy cpu_register_probe.dll and patch one header field: .rdata VirtualSize 0x23a -> 0x40. Its SizeOfRawData stays 0x400.
  2. Load the copy with LoadLibraryExW(path, NULL, 0) and with pe_map_image.
  3. Compare the bytes from RVA 0x2000 to 0x3000.

Observed

.rdata differs at byte 0x44. Windows has the file bytes 0d000000b0000000; pe_map has zeros. Other patched copies, compared over the whole page:

  • Nonzero padding written past VirtualSize 0x23a: the loads differ at 0x23a..0x3ff. Windows shows ab…, pe_map shows 00….
  • VirtualSize 0 with SizeOfRawData 0x10: Windows maps the whole 0x200 sector; pe_map maps 0x10 bytes.
  • .text with VirtualSize 0x80 and SizeOfRawData 0x2000, in a file with appended data: Windows fills the whole first page of .text from the file and leaves the next section's page unchanged.

Expected

The bytes Windows maps. That is the sector-rounded raw data (which pe_map already models), cut at the end of the page that holds the end of VirtualSize (VirtualSize rounded up to the page), not at VirtualSize itself.

Reference

The PE/COFF specification's section table says VirtualSize is the loaded size and that zero padding applies only when VirtualSize > SizeOfRawData. It does not say what is mapped past VirtualSize. The expected value is therefore defined by the Windows loader as measured, the same oracle cpu_native_section uses.

Not established

  • Whether any shipped image relies on bytes past VirtualSize.
  • Behaviour with a page size other than 4 KiB.
  • With SectionAlignment 0x2000 and a short .text, Windows leaves the second page reserved and uncommitted, while pe_map commits it read-write. This is a separate difference.
  • Whether Windows refuses overlapping or out-of-order section tables, which pe_map accepts.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6612876689 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/issues/2533). Originally posted by [Asimmimx](https://github.com/Asimmimx) on 2026-11-25T20:14:30Z. --- ### Commit or build `main` at `66da8c4f`, dev preset, Windows 11 build 26300. ### Summary When a section's `SizeOfRawData` is larger than its `VirtualSize`, the Windows loader maps the section's file sectors up to the end of the section's last page, so raw bytes past `VirtualSize` can be read at their RVAs. `pe_map_image` (`tools/pe_map.cpp`) stops the copy at `VirtualSize` and zero-fills the rest of that page. For such an image, the mapped section bytes differ from a `LoadLibraryExW` load of the same file. This breaks the byte-for-byte section equality with the Windows loader that `cpu_native_section` checks. Linkers zero-pad the bytes past `VirtualSize`, so ordinary linker output is not affected. The difference appears when those bytes are nonzero: a post-processed or packed image, a tool that trims `VirtualSize`, or a `VirtualSize` of 0 with an unaligned `SizeOfRawData`. Two existing cases in `tests/cpu/native_section_check.py` ("start inside a sector" and "start in the first sector") already differ in the page tail. The test compares only up to `VirtualSize`, so it does not see the difference. ### Reproduction Build `pe_map` and `cpu_register_probe` (`cmake --preset dev`). 1. Copy `cpu_register_probe.dll` and patch one header field: `.rdata` `VirtualSize` 0x23a -> 0x40. Its `SizeOfRawData` stays 0x400. 2. Load the copy with `LoadLibraryExW(path, NULL, 0)` and with `pe_map_image`. 3. Compare the bytes from RVA 0x2000 to 0x3000. ### Observed `.rdata` differs at byte 0x44. Windows has the file bytes `0d000000b0000000`; pe_map has zeros. Other patched copies, compared over the whole page: - Nonzero padding written past `VirtualSize` 0x23a: the loads differ at 0x23a..0x3ff. Windows shows `ab…`, pe_map shows `00…`. - `VirtualSize` 0 with `SizeOfRawData` 0x10: Windows maps the whole 0x200 sector; pe_map maps 0x10 bytes. - `.text` with `VirtualSize` 0x80 and `SizeOfRawData` 0x2000, in a file with appended data: Windows fills the whole first page of `.text` from the file and leaves the next section's page unchanged. ### Expected The bytes Windows maps. That is the sector-rounded raw data (which pe_map already models), cut at the end of the page that holds the end of `VirtualSize` (`VirtualSize` rounded up to the page), not at `VirtualSize` itself. ### Reference The PE/COFF specification's section table says `VirtualSize` is the loaded size and that zero padding applies only when `VirtualSize > SizeOfRawData`. It does not say what is mapped past `VirtualSize`. The expected value is therefore defined by the Windows loader as measured, the same oracle `cpu_native_section` uses. ### Not established - Whether any shipped image relies on bytes past `VirtualSize`. - Behaviour with a page size other than 4 KiB. - With `SectionAlignment` 0x2000 and a short `.text`, Windows leaves the second page reserved and uncommitted, while pe_map commits it read-write. This is a separate difference. - Whether Windows refuses overlapping or out-of-order section tables, which pe_map accepts. AI-assisted (Claude Code).
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#2525
No description provided.