relinker: on Windows, a short AMD-only site can take an fs: access that WindowsTlsBuilder rewrites ("AMD-only site bytes changed before patching") #27643

Closed
opened 2026-11-26 21:09:49 +00:00 by GiannisG1 · 0 comments
GiannisG1 commented 2026-11-26 21:09:49 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by GiannisG1 on 2026-11-26T21:09:49Z.


Problem

A Windows --to-intel relink fails with

FAIL: AMD-only site bytes changed before patching

when a short AMD-only instruction, too short for the 5-byte jump to its stub, absorbs a following fs: or gs: access. Found on PPSA38500 in #27564: @akkadbakkad89's dump (issuecomment-7202722298) shows a 13-byte site at RVA 0xfe10fd. Its original bytes are c5 f8 53 e2 64 48 8b 04 25 00 00 00 00 (vrcpps xmm4, xmm2, the optional RCPPS lowering, plus mov rax, fs:[0]), and the section held c5 f8 53 e2 e9 8f bc 71 0e 90 90 90 90 when the site was checked.

Cause

  • The forward absorption in Amd64OnlyConverter.cpp refuses an FS/GS-prefixed neighbour only when the converter keeps segment accesses: (_keepSegmentAccesses && info.SegmentPrefix != 0). WindowPlacer's segment fallback (#7309) depends on the same flag.
  • Conversion.cpp (executable) and GuestModuleBuilder.cpp (guest modules) set it for macOS only (#25971: "Linux and Windows output … are unchanged").
  • On Windows, WindowsPePatcher and WindowsGuestModuleWriter run WindowsTlsBuilder::Build, which rewrites guest fs:/gs: accesses in the section bytes, before WindowsTrampolineBuilder::Build checks each site's OriginalBytes. An fs: access absorbed into a site is therefore rewritten under the site, and the check fails. A segment access moved into a stub cannot work on Windows anyway: the stub would read the Windows fs:, not the guest TLS. Linux runs guest FS accesses natively, so this does not apply there.

Reproduction

An executable whose code is vrcpps xmm4, xmm2; mov rax, fs:[0]; ret (with a PT_TLS header), converted with --to-intel for Windows, fails on main 7c5d920df6 with the message above. The same conversion for Linux succeeds.

Proposed fix

Create the converter with segment accesses kept on Windows as on macOS, so that WindowsTlsBuilder alone owns those instructions. An optional lowering without room (RCPPS) is then kept, and a required one without room is refused at conversion time, as on macOS. A pull request with this change and a conversion test follows.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6635130936 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/27643). Originally posted by [GiannisG1](https://github.com/GiannisG1) on 2026-11-26T21:09:49Z. --- ### Problem A Windows `--to-intel` relink fails with ``` FAIL: AMD-only site bytes changed before patching ``` when a short AMD-only instruction, too short for the 5-byte jump to its stub, absorbs a following `fs:` or `gs:` access. Found on PPSA38500 in #27564: @akkadbakkad89's dump (issuecomment-7202722298) shows a 13-byte site at RVA `0xfe10fd`. Its original bytes are `c5 f8 53 e2 64 48 8b 04 25 00 00 00 00` (`vrcpps xmm4, xmm2`, the optional RCPPS lowering, plus `mov rax, fs:[0]`), and the section held `c5 f8 53 e2 e9 8f bc 71 0e 90 90 90 90` when the site was checked. ### Cause - The forward absorption in `Amd64OnlyConverter.cpp` refuses an FS/GS-prefixed neighbour only when the converter keeps segment accesses: `(_keepSegmentAccesses && info.SegmentPrefix != 0)`. `WindowPlacer`'s segment fallback (#7309) depends on the same flag. - `Conversion.cpp` (executable) and `GuestModuleBuilder.cpp` (guest modules) set it for macOS only (#25971: "Linux and Windows output … are unchanged"). - On Windows, `WindowsPePatcher` and `WindowsGuestModuleWriter` run `WindowsTlsBuilder::Build`, which rewrites guest `fs:`/`gs:` accesses in the section bytes, before `WindowsTrampolineBuilder::Build` checks each site's `OriginalBytes`. An `fs:` access absorbed into a site is therefore rewritten under the site, and the check fails. A segment access moved into a stub cannot work on Windows anyway: the stub would read the Windows `fs:`, not the guest TLS. Linux runs guest FS accesses natively, so this does not apply there. ### Reproduction An executable whose code is `vrcpps xmm4, xmm2; mov rax, fs:[0]; ret` (with a `PT_TLS` header), converted with `--to-intel` for Windows, fails on `main` `7c5d920df6` with the message above. The same conversion for Linux succeeds. ### Proposed fix Create the converter with segment accesses kept on Windows as on macOS, so that `WindowsTlsBuilder` alone owns those instructions. An optional lowering without room (RCPPS) is then kept, and a required one without room is refused at conversion time, as on macOS. A pull request with this change and a conversion test follows.
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
boykopovar/AnyPS5#27643
No description provided.