Fix unaligned x86-64 BSS loading under Rosetta
A writable x86-64 PT_LOAD with zero file bytes can start partway through a page. Linux supplies zero-filled memory from the start of the segment. On an Apple M5, a statically linked fixture faults under Rosetta, and an affected glibc PIE exits with status 139. Linux ELF loader
elfuse now stages an affected main executable before handing it to Rosetta. The private copy extends the segment back to the page boundary and keeps its original end. APFS uses a copy-on-write clone. Cross-volume staging and filesystems without clone support use a data copy. The guest can reopen the staged image by file descriptor even with a sysroot and a short host temporary path.
On Linux, AT_PHDR and glibc's dl_iterate_phdr expose the original header. On Rosetta, both expose the staged layout. Rosetta loads the interpreter named by PT_INTERP independently. An interpreter with its own affected segment can still fault during startup.
Verification
- All 87 checks in the Rosetta matrix passed on an Apple M5. With the translator unavailable, seven checks passed and ten dependent suites skipped. The eight BSS regressions also passed with the address, undefined-behavior and thread sanitizers.
- Compared read-only, executable and writable segments with an x86-64 Linux kernel under QEMU. Verified zero initialization, writes at both ends, guest-visible program headers, and an affected dynamically linked glibc executable with its standard interpreter.
- Ran the full BSS suite on APFS, across APFS and HFS+, and on HFS+. Tested fork, repeated exec, concurrent launches, a full destination volume, and a successful retry in the original process after
ENOMEM. - Forced a fatal overlay teardown between staging and image handoff. Both staged files and their directory were removed on exit. Skipping directory removal in a test build made the regression fail in both the macOS user temporary directory and /tmp. Eight concurrent test runs verified that each run accounts for its own directories.
- With the shim change in #556, the AArch64 matrix recorded 327 passes, 11 failures and 10 skips. All 11 failures also occurred on the unchanged base with the same Mac and fixtures.
Summary by cubic
Fixes x86-64 executables with a writable, zero-filled PT_LOAD that starts mid-page, which faulted under Rosetta with status 133. elfuse now stages a temporary aligned copy that starts the segment at a page boundary and preserves the original end address and the original file.
- Staged copies use copy-on-write on APFS and fall back to a data copy when cloning is unsupported.
- The staged image is published through a guest fd even when the path fits the VZ_CAPS field, so Rosetta opens the host-owned temporary file.
- The staging directory falls back to
DARWIN_USER_TEMP_DIRwhenTMPDIRis unset; a staging failure reportsENOMEMwhile keeping the host diagnostic. - Only the bootstrap thread and exec leader stage images; fork children keep their original exit path.
- The guest observes the aligned start and enlarged memory size via
AT_PHDRanddl_iterate_phdr. Rosetta loadsPT_INTERPitself, so an interpreter with its own affected segment can still fail at startup.
Verification
- New
test-rosetta-bsscovers zero initialization and writes at segment ends, read-only and executable variants, execve chains, fork and child exec, sysroots, short or unsetTMPDIR, and recovery after staging failure. - The test confirms the original ELF is unmodified and no staged copies remain after exit.
- The suite raises the x86-64 pass floor to 79 and ran on an Apple M5.