fix(relinker): load Windows guest TLS values at signed displacements #340
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/340"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-10-02T09:45:03Z.
What
The relinker rejects
mov r64, fs:[disp32]when the displacement is nonzero, including the canary load reported in #217.Accept the absolute disp32 form for every destination except
rsp. Preserve the existing thread-pointer result for displacement zero. For nonzero displacements, read a 64-bit value at the thread pointer plus the signed displacement. Reuse the same thread pointer as the existing dword store atfs:0x28. Preserve every other register, flags, stack position and red zone.After scanning the accesses and determining the aligned TLS block size, accept a nonzero load only if its displacement lies in the TLS block (
-blockSizeto-1) or is the stack guard at0x28. Loads atfs:1throughfs:0x27and beyond fail the relink: on the FreeBSD layout they read fields such astcb_dtv(fs:8) andtcb_thread(fs:0x10), which are not modelled. The relink error names the signed displacement and reports the instruction's input-file offset; no stub is emitted for a rejected load.Only the fs:0 self pointer is initialized in the thread-control block. The fs:0x28 stack-protector canary reads as zero unless guest code stores it; loads and stores at fs:0x28 agree, so canary checks pass but do not detect corruption.
Follow-up work: FS
cmp,sub,addandxoroperands, 32-bit loads, FS register-address forms, GS accesses and loads of the other thread-control block fields remain rejected.Tested
Base:
e1654a97; independent of the diagnostics branch.Windows 11 Home 10.0.26300, MinGW-w64 GCC 15.2.0, Release, Python 3.14.6: all 15 relinker tests pass. The extended TLS coverage test executes 180 relinked fixtures: every allowed destination, two flag patterns, displacements 0, 40, -64, -40 and -8, and a dword-store/64-bit-load round trip at 40. Fixtures verify loaded values, all other general registers, flags, stack position and the red zone.
Twenty-two bounds fixtures cover displacements 1, 8, 0x10, 0x20, 0x27, 0x29, 0x2c, 0x30, -65 for a 64-byte aligned block and the signed disp32 extremes. Each requires the displacement-specific diagnostic and
(offset 0x1240), the instruction's file offset. The extended test fails against the previous revision of this PR (508a971), which accepted fs:1 through fs:0x27, and against102e042, which reported the image RVA0x11240. Rejection guards coverrsp, REX.B/REX.X variants and the out-of-scope forms.Non-Windows runs verify relinking and stub bytes; loaded values are verified by executing the relinked fixtures on Windows. Each nonzero-displacement case checks for
48 8b 80followed by the little-endian displacement in its generated stub. Bounds cases also cover a PT_TLS header appearing after the executable segment.Linux: Ubuntu 24.04, GCC 13.3, Release: all 15 relinker tests pass.
Synthetic fixtures only; NHL 27 was not tested.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:41:55Z.
This is careful work and the tests are great. One thing on exactness: loads at fs:8 through fs:0x27 are now accepted and return zero, but on FreeBSD amd64 those are tcb_dtv (fs:8) and tcb_thread (fs:0x10), see
struct tcbin sys/x86/include/tls.h and_get_curthreadin libthr. Code that inlines a curthread read would get null at run time instead of failing the relink. Could you limit the accepted range to the TLS block (negative displacements), 0 and the canary at 0x28, and keep rejecting 8 to 0x27 until those fields are modelled?Imported from boykopovar/AnyPS5. Originally posted by QDarkRex on 2026-10-04T07:16:03Z.
Thanks, that's a good catch: returning zero there is worse than failing. Pushed: nonzero loads are now accepted only inside the TLS block or at the stack guard 0x28, and everything from fs:1 through fs:0x27 (and past 0x28) fails the relink with the displacement and file offset. The bounds tests now cover 1, 8, 0x10, 0x20, 0x27, 0x29 and 0x2c; the executed fixtures at 8 and 0x10 are gone, and the fs:0 case now checks its pointer through 0x28. Relinker tests pass on Windows and Linux, and the updated test fails against the previous revision. I've updated the description to match.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T07:39:48Z.
Thanks, that's exactly what I meant: nonzero loads now only reach the TLS block or the stack guard at 0x28, and the bounds cases pin down 1 through 0x27 and past 0x28. It builds and passes the relinker tests here merged with current main, so I'll merge it.