relinker: Linux extra PT_LOAD can share a page with the tail of the last guest segment #4913

Closed
opened 2026-10-13 03:48:24 +00:00 by sudocharlinho · 5 comments
sudocharlinho commented 2026-10-13 03:48:24 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by sudocharlinho on 2026-10-13T03:48:24Z.


On Linux output, ProgramHeaderLayoutBuilder::ComputeExtraBlockVaddr can place the relinker's extra RWX PT_LOAD in the same 4 KiB page as the tail of the highest guest PT_LOAD. The loader then maps that page from the extra block's file page, so the guest reads file bytes where the end of its .data/.bss should be.

Cause

core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp (main 6124bf063):

const std::uint64_t alignedFloor = highestVaddrEnd & ~(align - 1);
std::uint64_t vaddr = alignedFloor | offsetRemainder;
if (vaddr < highestVaddrEnd)
    vaddr += align;

The comparison is on byte addresses, not pages. With E = highest kept PT_LOAD end (p_vaddr + p_memsz) and F = extra block file offset (the unpadded input size, LinuxElfPatcher.cpp), the block shares the last guest page whenever E % 0x1000 != 0 and F % 0x1000 >= E % 0x1000.

Example: E = 0x2800, F = 0x8A00 gives 0x2A00. The kernel maps page 0x2000 again, MAP_FIXED, from file page 0x8000, after the earlier segment's bss was zero-filled.

LinuxGuestModuleWriter.cpp does not have this: it aligns both the file and the address up to 0x4000 before appending.

Reach

  • Not expected with the retail PS5 layout where the highest PT_LOAD is the file-backed dynlib-data segment ending next to end of file (both mappings then show the same file page).
  • Expected in about half of inputs whose highest PT_LOAD ends in .bss with other data after it in the file: PS4-layout executables and lld-linked homebrew.
  • Never when the file size or the segment end is 4 KiB aligned, which is why current fixtures pass (ProgramLayoutRangeTests.cpp uses end 0x2000; the Python Linux fixtures have page-aligned sizes).

Status

Found by reading the code with AI assistance. Not observed on a title: no real input was relinked, so I have no title ID or log. The arithmetic is confirmed by a unit case: on main, ComputeExtraBlockVaddr with a load 0x1000..0x2800 and offset 0x8A00 returns 0x2A00.

Possible fix

-    const std::uint64_t alignedFloor = highestVaddrEnd & ~(align - 1);
-    std::uint64_t vaddr = alignedFloor | offsetRemainder;
-    if (vaddr < highestVaddrEnd)
-        vaddr += align;
-    if (vaddr < highestVaddrEnd)
+    if (highestVaddrEnd > std::numeric_limits<std::uint64_t>::max() - (align - 1) - offsetRemainder)
         throw Domain::RelinkerException("Extra block vaddr computation overflowed");
-    return vaddr;
+    const std::uint64_t alignedCeiling = (highestVaddrEnd + (align - 1)) & ~(align - 1);
+    return alignedCeiling + offsetRemainder;

With this and two added cases in ProgramLayoutRangeTests.cpp (0x8A00 -> 0x3A00, 0x8100 -> 0x3100), the relinker-only build passes 134/134 tests on macOS (Apple silicon, AppleClang 21); the added cases fail on main. Existing expectations are unchanged (0x2000 with offset 0x1003 still gives 0x2003).

I am not opening a pull request; anyone is welcome to take this.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5824173474 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4913). Originally posted by [sudocharlinho](https://github.com/sudocharlinho) on 2026-10-13T03:48:24Z. --- On Linux output, `ProgramHeaderLayoutBuilder::ComputeExtraBlockVaddr` can place the relinker's extra RWX `PT_LOAD` in the same 4 KiB page as the tail of the highest guest `PT_LOAD`. The loader then maps that page from the extra block's file page, so the guest reads file bytes where the end of its `.data`/`.bss` should be. ### Cause `core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp` (main `6124bf063`): ```cpp const std::uint64_t alignedFloor = highestVaddrEnd & ~(align - 1); std::uint64_t vaddr = alignedFloor | offsetRemainder; if (vaddr < highestVaddrEnd) vaddr += align; ``` The comparison is on byte addresses, not pages. With `E` = highest kept `PT_LOAD` end (`p_vaddr + p_memsz`) and `F` = extra block file offset (the unpadded input size, `LinuxElfPatcher.cpp`), the block shares the last guest page whenever `E % 0x1000 != 0` and `F % 0x1000 >= E % 0x1000`. Example: `E = 0x2800`, `F = 0x8A00` gives `0x2A00`. The kernel maps page `0x2000` again, `MAP_FIXED`, from file page `0x8000`, after the earlier segment's bss was zero-filled. `LinuxGuestModuleWriter.cpp` does not have this: it aligns both the file and the address up to `0x4000` before appending. ### Reach - Not expected with the retail PS5 layout where the highest `PT_LOAD` is the file-backed dynlib-data segment ending next to end of file (both mappings then show the same file page). - Expected in about half of inputs whose highest `PT_LOAD` ends in `.bss` with other data after it in the file: PS4-layout executables and lld-linked homebrew. - Never when the file size or the segment end is 4 KiB aligned, which is why current fixtures pass (`ProgramLayoutRangeTests.cpp` uses end `0x2000`; the Python Linux fixtures have page-aligned sizes). ### Status Found by reading the code with AI assistance. Not observed on a title: no real input was relinked, so I have no title ID or log. The arithmetic is confirmed by a unit case: on main, `ComputeExtraBlockVaddr` with a load `0x1000..0x2800` and offset `0x8A00` returns `0x2A00`. ### Possible fix ```diff - const std::uint64_t alignedFloor = highestVaddrEnd & ~(align - 1); - std::uint64_t vaddr = alignedFloor | offsetRemainder; - if (vaddr < highestVaddrEnd) - vaddr += align; - if (vaddr < highestVaddrEnd) + if (highestVaddrEnd > std::numeric_limits<std::uint64_t>::max() - (align - 1) - offsetRemainder) throw Domain::RelinkerException("Extra block vaddr computation overflowed"); - return vaddr; + const std::uint64_t alignedCeiling = (highestVaddrEnd + (align - 1)) & ~(align - 1); + return alignedCeiling + offsetRemainder; ``` With this and two added cases in `ProgramLayoutRangeTests.cpp` (`0x8A00 -> 0x3A00`, `0x8100 -> 0x3100`), the relinker-only build passes 134/134 tests on macOS (Apple silicon, AppleClang 21); the added cases fail on main. Existing expectations are unchanged (`0x2000` with offset `0x1003` still gives `0x2003`). I am not opening a pull request; anyone is welcome to take this.
LeandroSSB commented 2026-10-13 04:01:59 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by LeandroSSB on 2026-10-13T04:01:59Z.


Taken: LeandroSSB/AnyPS5:fix/relinker-extra-ptload-page (one commit, c3aa504ab, on current main). Your diff applied as-is; the two regression cases (0x8A00 → 0x3A00, 0x8100 → 0x3100) are in ProgramLayoutRangeTests.cpp, and the existing expectations are unchanged.

Validated on Linux (GCC 13.3, relinker-only build, BUILD_TESTING=ON): 143/143 tests pass, including the two new cases; tools/check_conventions.py --base origin/main passes. Not opened as a PR because of the intake pause; a maintainer is welcome to carry the commit.

AI-assisted (Claude Code); I ran the validation above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6137951153 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4913#issuecomment-6137951153). Originally posted by [LeandroSSB](https://github.com/LeandroSSB) on 2026-10-13T04:01:59Z. --- Taken: [`LeandroSSB/AnyPS5:fix/relinker-extra-ptload-page`](https://github.com/LeandroSSB/AnyPS5/tree/fix/relinker-extra-ptload-page) (one commit, `c3aa504ab`, on current `main`). Your diff applied as-is; the two regression cases (`0x8A00 → 0x3A00`, `0x8100 → 0x3100`) are in `ProgramLayoutRangeTests.cpp`, and the existing expectations are unchanged. **Validated** on Linux (GCC 13.3, relinker-only build, `BUILD_TESTING=ON`): **143/143 tests pass**, including the two new cases; `tools/check_conventions.py --base origin/main` passes. Not opened as a PR because of the intake pause; a maintainer is welcome to carry the commit. AI-assisted (Claude Code); I ran the validation above on my machine.
mariuszkreft commented 2026-10-13 23:01:43 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by mariuszkreft on 2026-10-13T23:01:43Z.


Opened a pull request from this issue: https://github.com/boykopovar/AnyPS5/pull/5534

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157026322 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4913#issuecomment-6157026322). Originally posted by [mariuszkreft](https://github.com/mariuszkreft) on 2026-10-13T23:01:43Z. --- Opened a pull request from this issue: https://github.com/boykopovar/AnyPS5/pull/5534
mariuszkreft commented 2026-10-13 23:06:44 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by mariuszkreft on 2026-10-13T23:06:44Z.


Opened a pull request from this issue: https://github.com/boykopovar/AnyPS5/pull/5535

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157093184 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4913#issuecomment-6157093184). Originally posted by [mariuszkreft](https://github.com/mariuszkreft) on 2026-10-13T23:06:44Z. --- Opened a pull request from this issue: https://github.com/boykopovar/AnyPS5/pull/5535
mariuszkreft commented 2026-10-13 23:11:26 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by mariuszkreft on 2026-10-13T23:11:26Z.


Opened a pull request from this issue: https://github.com/boykopovar/AnyPS5/pull/5535

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6157153899 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4913#issuecomment-6157153899). Originally posted by [mariuszkreft](https://github.com/mariuszkreft) on 2026-10-13T23:11:26Z. --- Opened a pull request from this issue: https://github.com/boykopovar/AnyPS5/pull/5535
oneandonlydean commented 2026-10-14 06:53:52 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:53:52Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162704252 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4913#issuecomment-6162704252). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:53:52Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
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#4913
No description provided.