Commit Graph
71 Commits
Author SHA1 Message Date
Dean 613a877ab9 Merge pull request #11232 from MarcZX10/marc/fix-relinker-island-overflow
fix(relinker): place 256-bit store islands across 2^63 without signed overflow
2026-10-27 10:39:59 +13:00
MarcZX10 9da4efcced test(relinker): lower a 256-bit store through an island across 2^63 2026-10-25 22:13:34 +00:00
Dean Galvin e27a9d3e01 test: restore the function layout of the rosetta in-place execution tests 2026-10-26 10:37:03 +13:00
Dean Galvin 099b865f82 Merge branch 'main' into test/rosetta-inplace-execution 2026-10-26 10:32:38 +13:00
Adrià 6bc1139698 Merge pull request #9184 from javierUDP/test/scalar-compare-execution
test(relinker): execute the moved scalar compare
2026-10-25 20:36:37 +01:00
Dean d7493d7226 Merge pull request #10713 from Filip722/fix/reject-lock-movnts-20261024
fix(relinker): reject LOCK-prefixed MOVNTSS and MOVNTSD
2026-10-26 05:57:30 +13:00
javierUDP 65a98e7f31 test(relinker): execute the moved scalar compare
The stub that moves a hazard scalar compare out of line is checked
through its bytes but never run, so nothing confirms that the compare
still produces the mask it would inline. The x86-64 execution section
now calls the stub for both outcomes of a CMPSS with an unsafe predicate
and checks the destination's lanes.
2026-10-25 11:28:03 +01:00
Dean a512780d58 Merge pull request #10340 from Filip722/submit/lock-20261023
fix(relinker): reject LOCK-prefixed SIMD lowerings
2026-10-25 13:28:53 +13:00
Filip Michalski 50dc1c93c8 fix(relinker): reject LOCK-prefixed MOVNTSS and MOVNTSD 2026-10-24 20:43:16 +02:00
Filip Michalski cfa8c10d21 fix(relinker): clarify LOCK SHA failures and cover conversion 2026-10-24 20:43:14 +02:00
Karim Masmoudi ce6c2a0c66 fix(relinker): rewrite XSAVEOPT to XSAVE under --to-rosetta
XSAVEOPT raises an illegal instruction under Rosetta on an M5 running
macOS 27.0.1, as XSAVEC does, while XSAVE and XRSTOR save and restore
x87, SSE and AVX state in the standard format. The PS5's Zen 2 has
XSAVEOPT, so code built for it can use the instruction without a CPUID
check.

XSAVEOPT differs from XSAVE only in the init and modified optimizations:
it may skip writing state that is in its initial configuration or
unchanged since the last XRSTOR from the same area. XRSTOR restores the
same state from the area XSAVE writes, so --to-rosetta rewrites
XSAVEOPT (NP 0F AE /6, memory operand) to XSAVE (/4) in place, keeping
the prefixes and the operand, as RDSEED becomes RDRAND. CLWB (66 0F AE
/6), MFENCE, UMONITOR, UMWAIT and the Intel target are left alone.
XSAVEC is not converted: it writes the compacted format.

The converter tests check the rewrite for the plain, REX.W,
RSP-relative, extended-base, RIP-relative and FS-relative forms, check
that the neighbouring 0F AE encodings are not taken, and execute the
rewritten XSAVEOPT64 followed by XRSTOR on YMM3 in the x86-64 build.
The --to-rosetta integration test now covers XSAVEOPT as well.
2026-10-24 11:01:44 +02:00
Filip Michalski 1e00b460b9 fix(relinker): reject LOCK-prefixed SIMD lowerings 2026-10-23 20:16:51 +02:00
Nciibi 191266170b fix(relinker): move a following in-place replacement into a 256-bit store's stub
A 4-byte 256-bit store at the start of a segment followed by an AMD-only
site that can only be emitted in place (RDSEED, CLWB, MOVNTSS, ...) had no
preceding instruction to move, so WindowPlacer gave up with "No room for a
jump to the stub of 256-bit store within its neighbours or a nearby island".

Try a last-resort window placement that allows EmitOutOfLine to use the
in-place lowering for the follower. Preferred preceding and following moves
are unchanged.
2026-10-23 16:11:00 +01:00
Dean 3b6613eb58 Merge pull request #8597 from javierUDP/test/wide-store-execution
test(relinker): execute the split 256-bit store
2026-10-24 03:28:26 +13:00
Kang, Harry Zixuan 4e9854ea2b test(relinker): report SHA-1 preservation failures precisely 2026-10-21 20:29:55 -07:00
javierUDP 5702e1f938 test(relinker): include algorithm for the converter test std::all_of
The new checks call std::all_of, which the file reached only through
another header.
2026-10-20 18:25:33 -03:00
javierUDP eab9b23a52 test(relinker): execute the split 256-bit store
The stub that splits an unaligned 256-bit store is checked through its
bytes but never run, so nothing confirms that the two halves land where
the original store would, including across a page boundary, which is the
case the split exists for. The x86-64 execution section now runs the
stub at three operand offsets and checks the 32 bytes it writes and the
pages around them.
2026-10-20 14:54:02 -03:00
Dean b9731811ba Merge pull request #8001 from mopga/fix/relinker-amd-only-claimed-followers
fix(relinker): do not take instructions another AMD-only stub moved as a short instruction's followers
2026-10-21 06:30:39 +13:00
Dean 989b6ea794 Merge pull request #7999 from mopga/fix/relinker-amd-only-sequence-in-place
fix(relinker): move in-place AMD-only replacements into a short instruction's stub
2026-10-21 06:19:51 +13:00
Adrià 8afd6ea478 Merge pull request #7660 from savvaniss/fix/sce-code-gaps
fix(relinker): resume scans after undecodable bytes between PS4 unwind functions
2026-10-20 13:17:15 +02:00
javierUDP d5886fce88 test(relinker): execute the in-place Rosetta replacements
RDPID, RDSEED and CLWB are checked through their replacement bytes but
never run, although all three raise an illegal instruction on a Rosetta
host, which is what the replacements exist for. The x86-64 execution
section now calls the RDPID stub and the RDSEED and CLWB replacements
and checks the value, the carry flag and the registers they leave.
2026-10-20 02:22:14 -03:00
Dean d85b7c759a Merge pull request #2780 from SuarezPM/ai/relinker-hardening
feat(relinker): lower MCOMMIT and RDPRU for Intel with execution tests
2026-10-20 16:22:49 +13:00
Adrià 34b1b77abf Merge pull request #7651 from DavidSamir/fix/relinker-amd-only-branch-targets
fix(relinker): ignore direct branch targets inside an instruction past its prefixes
2026-10-20 02:13:47 +02:00
SuarezPM bf47697ec0 feat(relinker): lower MCOMMIT and RDPRU for Intel with execution tests
MCOMMIT becomes STC plus NOP padding and RDPRU goes through a
runtime-checked stub returning the host TSC for MPERF/APERF while
other selectors yield zero, with converter and execution tests.

docs/dev/debt/relinker.md records the new lowering and its unverified
assumptions.
2026-10-19 19:19:31 -03:00
mopgobopotandClaude Opus 5.5 e3e05d9171 fix(relinker): do not take instructions another AMD-only stub moved as a short instruction's followers
A short AMD-only instruction takes the following instructions into its stub until its site fits a jump. The loop did not check which instructions an earlier 256-bit store island had already claimed, so with --to-rosetta two trampolines covered the same bytes: for je; jmp; vmovups [rdi],ymm0; rcpps xmm1,xmm2 followed by adds, the RCPPS site at +8 overlapped the island at +11, the patched code crashed, and the real patchers failed with 'AMD-only site bytes changed before patching'. A claimed follower now ends the attempt: an optional instruction such as RCPPS or RDPID is kept, and any other instruction fails with an error naming the moved follower.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 20:07:03 +03:00
mopgobopotandClaude Opus 5.5 d37221a00a fix(relinker): move in-place AMD-only replacements into a short instruction's stub
A short AMD-only instruction lowered through a stub takes the following instructions with it. When one of them is an AMD-only instruction that is replaced in place (MOVNTSS, MOVNTSD, MONITORX, MWAITX, and RDSEED and CLWB for Rosetta), MatchSequence found no out-of-line lowering for it and the conversion failed with 'AMD-only instruction sequence has no out-of-line lowering', for example on extrq xmm0,xmm1 followed by movntss [rdi],xmm0. A following instruction with an in-place replacement now has that replacement moved into the stub, with its RIP-relative displacement relocated. Out-of-line emission for 256-bit store windows is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 20:01:41 +03:00
plokmklo c1866af79b fix(relinker): synthesize PT_GNU_STACK for Linux outputs without one
Outputs whose input carries no PT_GNU_STACK get one appended with
PF_R|PF_W and alignment 16 so the kernel does not fall back to an
executable stack marking.

RequiredProgramHeaderCount and WriteLayout now derive from one shared
header list, so the reserved slot and the written header cannot
disagree at any PhNum boundary, when a free input slot should be
reused, or when the RELRO coverage guard reduces the kept count.
Space exhaustion beyond the 16-bit header limit fails loudly instead
of silently skipping the header.
2026-10-19 07:30:56 +00:00
plokmklo d4cdfe0639 test(relinker): locate Linux stub segments via e_phoff
The program header table moves when the layout needs more slots than
the input provides, so the stub lookup must follow the output e_phoff
instead of assuming the table stays at file offset 64.
2026-10-19 07:30:56 +00:00
Savvanis SpyrosandClaude Opus 5.5 45a2e3dd4a fix(relinker): resume scans after undecodable bytes between PS4 unwind functions
In PS4 executables and modules the bytes between unwind functions are
16-byte slots of function tails and padding, and a tail can begin inside
an instruction, such as a lone REX byte before a 15-byte NOP. The
syscall and call-site scans decode the executable segment linearly, so
such a slot failed the relink with "Cannot decode instruction".

For images with a PT_SCE_DYNLIBDATA segment the scans now receive the
unwind function ranges. Outside every range an undecodable byte is
skipped and decoding resumes at the next byte, and a decoding that would
cross into a range ends at its start, so gap bytes cannot change how a
function is decoded. Inside a range a decode failure still fails the
relink, and images without unwind ranges are scanned as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-18 23:45:49 +03:00
David 5c2062cc0a test(relinker): cover branches into converted instructions past their prefixes 2026-10-19 00:10:07 +04:00
Karim Masmoudi 57e52c1f03 fix(relinker): report processor 0 from RDTSCP under --to-rosetta
Rosetta sets RDTSCP's ECX to its constant TSC_AUX, 0xAAAAAAAB, which is
not a processor index. --to-rosetta already makes RDPID return processor
0 for the same reason, so code reading the processor through RDTSCP saw
a different value from code reading it through RDPID.

--to-rosetta now moves RDTSCP into an optional stub that runs the
instruction unchanged and then loads 0 into ECX with a MOV, which
leaves the counter in EDX:EAX and the flags as they were. A site with no
movable instruction after it for the jump is kept, as for RDPID. The
Intel target and a plain relink are unchanged.
2026-10-18 18:27:15 +02:00
Morten HolmandClaude Sonnet 5.5 cf9e5a2567 test(relinker): keep an FS or GS access out of the window of a 256-bit store
Covers a thread-pointer read (FS and GS form) after the store with a movable
instruction before it, and the fallback to the following access when there is
no other neighbour.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-10-18 00:40:41 +02:00
javierUDP d67e7b7389 test(relinker): link the converter tests with a small macOS zero page
The CLZERO case that needs a 32-bit address found no mapping because a
64-bit Mach-O executable reserves 4 GiB as `__PAGEZERO`, not because
Rosetta withholds low memory: the same `mmap` succeeds once the
executable is linked with a smaller zero page. The Apple x86-64 build
links the test that way and the case runs with the other three, so the
skip it had is gone and the Linux mapping failure stays fatal. The arm64
build keeps the 4 GiB zero page, which it requires to start.
2026-10-15 17:10:59 -03:00
javierUDP 6f100d3f61 test(relinker): execute the AMD64-only conversion stubs on macOS
The execution part of the AMD64-only converter tests was compiled for
Linux x86-64 only, so the SSE4A, SHA-1, SHA-256, CLZERO and reciprocal
stubs were never run on a macOS x86-64 build, unlike the ANDN lowering
tests. Rosetta maps no memory below 4 GiB, so the CLZERO case that needs
a 32-bit address is left out when that mapping is unavailable.
2026-10-15 15:40:26 -03:00
Alexej Szpevák ba4b2db238 fix(relinker): move legacy CMPSS and CMPSD out of line for Rosetta
The Rosetta split covered only VEX scalar compares. #5642 measured the
same lost load after legacy CMPSS and CMPSD, and the decoder already
hands them to the matcher with the right length and RIP offset.

Recognise F3 or F2, segment overrides and an optional REX before
0F C2 /r ib, with the same predicate rule as the VEX forms. A 66, 67 or
LOCK prefix, or both F2 and F3, keeps the instruction inline as before.
2026-10-14 11:48:11 +02:00
Leandro Soares 8b400afc5d fix(relinker): move hazard scalar compares out of line for Rosetta 2026-10-14 06:19:58 +00:00
fablevoid 1541244b78 fix(relinker): treat jump table targets as branch targets when moving instructions
The AMD-only converter moves the instructions next to a short AMD-only
instruction into its stub, and refuses to move one that a branch
enters. It knew only direct jump and call targets, so a switch case
entered through a jump table could land inside the jump written over
the moved instructions.

The converter now also reads relative jump tables: a RIP-relative LEA
whose address lies in a loaded segment starts a table of 32-bit
offsets from that address, read while each target starts an
instruction of the segment being converted. To read them, Convert
takes the program headers and converts their executable load
segments; the relinker passes all headers, and
ElfReader::ReadCodeSegments, now unused, is removed.

The converter test builds a dispatch through such a table whose second
case enters the instruction after a 4-byte 256-bit store, and checks
that the store takes the instruction before it instead. It also checks
that reading stops at an entry that does not start an instruction,
that only a LEA forms a table address, that tables are read only
inside loaded segments, that only executable load segments are
converted, and that a segment past the end of the file fails the
conversion.

PPSA12544: one 256-bit store in Il2cppUserAssemblies moved the
instruction after it, which is case 1 of a switch; it now goes through
an island. The other relinked files are unchanged.

AI-assisted: yes
2026-10-12 13:06:23 +00:00
fablevoid c763a78b67 feat(relinker): split unaligned 256-bit stores for Rosetta
Rosetta 2 ends the process when an unaligned 256-bit store crosses into
a page the store may not write ("rosetta error: unexpectedly need to
EmulateForward on a synchronous exception") instead of raising the
fault. A store inside such a page, and a 128-bit store across one,
fault normally. The macOS write watch keeps watched pages read-only
between collections, so a guest that stores 32 bytes across the start
of a watched page ends the process.

--to-rosetta now lowers VMOVUPS, VMOVUPD and VMOVDQU with a 256-bit
memory destination to a stub that stores the low half with the same
instruction at VEX.L=0 and the high half with VEXTRACTF128 at the
operand plus 16, so each half faults like any other store. RIP-relative
operands are relocated from the instruction's place in the site. A
store whose upper half has no 32-bit displacement fails the conversion.
Aligned stores (VMOVAPS, VMOVAPD, VMOVDQA and the non-temporal forms)
cannot cross a page and are left alone.

Every store is lowered. A 4-byte store has no room for the jump to its
stub, so its stub also takes one neighbouring instruction. The
following one: a return ends the stub, a call pushes its original
return address and jumps, a jump or conditional branch is re-encoded
with a 32-bit displacement, and another 256-bit store is lowered in the
same stub. Otherwise the preceding one, unless it is a call, a jump, a
return or a trap. Register calls, LOOP, JRCXZ and branches with a
prefix that changes them do not move. When neither neighbour can move,
the store becomes a short jump to an island: a run of movable
instructions within reach is moved to its own stub, and the bytes it
frees hold the jump to the store's stub. A store with neither fails the
conversion.

The converter tests compare the halves with clang's encodings for base,
SIB, disp8 overflowing to disp32, disp32, RSP-relative, FS-absolute,
r13-based and RIP-relative operands, and check that aligned stores,
loads, register forms and masked stores are not split. They check the
stub of a short store followed by a return, a call (direct, with an
address-size prefix, and through a RIP-relative pointer), short, near
and RIP-relative jumps, short and near conditional branches and another
store; that a following register call, LOOP, JRCXZ, prefixed branch or
CLWB makes the store take the instruction before it; islands past
instructions already moved, a store that needs its own stub, a branch
target, a register call and the short-jump reach; and that a store with
no room fails.

PPSA12544: 55295 stores split in the executable, 278 of them in the
stub of the store before them; every module converts with none kept.

AI-assisted: yes
2026-10-12 02:48:33 +00:00
Scramblrrrr 363d17985e fix(relinker): lower legacy packed reciprocal register instructions 2026-10-11 10:58:14 -03:00
mugurc 92b20608e8 feat(relinker): convert for Rosetta with --to-rosetta
Rosetta runs AVX2, FMA, BMI1/2, F16C and MOVBE, but, unlike an Intel
host, it has neither RDSEED, CLWB nor RDPID. --to-rosetta converts
everything --to-intel does and lowers these three as well:

- RDSEED becomes RDRAND (the /6 form of the same opcode), which also
  returns a random value and sets CF.
- CLWB only writes a cache line back, so it becomes a NOP of the same
  length.
- RDPID becomes a load of 0 through an optional stub: Rosetta's TSC_AUX
  is a constant, not a processor index, so every thread reports
  processor 0. A site with no movable instruction after it for the jump
  is kept.

The flag implies --to-intel and conflicts with --windows. It is
independent of --macos: it only changes what the conversion produces, so
--macos alone leaves these instructions as they are, as on an Intel Mac,
and a title for Rosetta is relinked with --macos --to-rosetta. Intel
output is unchanged: the target is a parameter of the converter, Intel by
default.
2026-10-10 22:46:45 +03:00
Dean 6f262eb0f4 Merge pull request #2568 from oneandonlydean/fix/relinker-decode-xop-2559
fix(relinker): decode XOP instructions
2026-10-10 19:47:50 +13:00
Achraf ReyaniandClaude Opus 5.5 ce8dbc977f fix(relinker): decode XOP instructions
The x86 decoder had no XOP (AMD 8F escape) support and read 8F E8.. as
POP r/m, losing instruction boundaries. Since #2238 roots code addresses
taken by RIP-relative LEA, code analysis reaches CPU-dispatched XOP
paths, such as the vpperm routines in Stremio Plus (PPSA74126), OpenNOW
(PPSA99082) and PPSA99420, and their Windows conversion failed with
"Code analysis: overlapping instruction boundaries". Decode 8F followed
by a map of 8 or more as a three-byte XOP prefix (8F can only be POP r/m
with ModRM.reg 0) with a ModRM operand, an 8-bit immediate in map 8 and
a 32-bit immediate in map 0Ah, in both the length and operand decoders.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-10 19:27:24 +13:00
jweidenh 25984a728c fix(relinker): count all synthetic Linux program headers 2026-10-10 01:38:07 -04:00
Adrià 9e2ffe0da4 Merge pull request #1457 from drk1rd/fix/relinker-ud1-trap
fix(relinker): decode UD1 as a trap
2026-10-08 16:17:06 +02:00
xsvm 22ebf5e608 fix(relinker): classify rewrite reference sites with the instruction decoder
ClassifyInstruction assumed a ModRM byte right after every opcode that
was not a near branch. A length-changing rewrite therefore adjusted
immediates such as MOVABS imm64 that looked like a RIP-relative ModRM,
and missed RIP-relative operands of VEX and 0F 38 / 0F 3A instructions
and the rel8 targets of LOOP and JRCXZ. Take the RIP-relative
displacement and branch target from DecodeInstruction instead.
2026-10-08 18:20:14 +08:00
Suryansh Prajapati 7a3ec9f015 fix(relinker): decode UD1 as a trap
UD1 (0F B9 /r) always raises #UD, like UD2, but DecodeInstruction
reported it as sequential. Reachability analysis then followed the
bytes after it, so an import reachable only through that fallthrough
was kept by the strict unused NID filter. UD1 keeps its ModRM, so a
RIP-relative operand is still reported.
2026-10-08 15:26:54 +05:30
Adrià 73ac9ef7f4 Merge pull request #1218 from hashaam1217/feat/relinker-rip-relative-stubs
feat(relinker): relocate RIP-relative operands into --to-intel stubs
2026-10-08 11:40:17 +02:00
Dean 9e5ade70fb Merge pull request #1381 from overlordxrz-source/fix/relinker-decoder-0f-no-modrm
fix(relinker): decode 0F A8-AA without a ModRM and MOV CR/DR without a memory operand
2026-10-08 22:35:44 +13:00
Dean a753d0de45 Merge pull request #1303 from overlordxrz-source/fix/relinker-evex-grp3-immediate
fix(relinker): do not add the F6/F7 TEST immediate to EVEX instructions
2026-10-08 22:35:39 +13:00
Hashaam ZafarandClaude Opus 5.5 a571a7f100 feat(relinker): relocate RIP-relative operands into --to-intel stubs
SHA-1 and SHA-256 instructions with a [rip+disp32] operand, and
instructions with one that a short site absorbs, now move into the
stub. The stub records each displacement as a relocation relative to
the site, and the Linux, guest and Windows placements resolve it once
the stub address is known. Indirect branches through [rip+disp32]
still fail the relink.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-08 11:10:38 +02:00