relinker: --linux-fixed-base rebases the stale program header table when the table has to grow #6393

Closed
opened 2026-10-15 13:56:06 +00:00 by midasmatterhorn · 1 comment
midasmatterhorn commented 2026-10-15 13:56:06 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by midasmatterhorn on 2026-10-15T13:56:06Z.


When the input's program header table has to grow (fewer free slots than the output needs), Patch relocates the table to the end of the file (tableOff = buf.size(), e_phoff rewritten at core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp:311-319). The --linux-fixed-base pass then rebases the wrong table: RebaseProgramHeaders (line 339) still receives the input's phOff, so

  • the relocated output table is never rebased — PT_LOAD p_vaddr/p_paddr keep un-rebased values while e_entry and the DT_* tags are rebased, producing a silently inconsistent ET_EXEC whose entry point lies outside every PT_LOAD; and
  • the pass instead walks the stale input table, so it also fails on input headers that SegmentFilter dropped from the output (FAIL: Program header address ... lies outside the Linux user address space at fixed base 0x400000 for a PT_DYNAMIC address that exists only in the input).

Reproduced with a relinker-only build of main 92cdc7b62 on macOS 27.0.1 arm64, on a minimal ET_EXEC fixture (one PT_LOAD with code, a PT_DYNAMIC, extra PT_SCE_VERSION headers to control e_phnum):

fixture options result
e_phnum = 3 (table fits) none e_phoff=0x40, entry mapped
e_phnum = 3 (table grows to 0x8150) none e_phoff=0x8150, entry=0x1118, entry mapped
e_phnum = 3 (table grows) --linux-fixed-base 0x400000 e_phoff=0x8150, entry=0x401118, entry not mapped — loads still at 0x0/0x1000/0x4000
e_phnum = 6 (table fits) --linux-fixed-base 0x400000 loads rebased to 0x400000..., entry mapped (correct)
e_phnum = 3, input PT_DYNAMIC at 0x7ffffffffff0 (dropped from output) none succeeds
same --linux-fixed-base 0x400000 FAIL: Program header address 0x7ffffffffff0 lies outside the Linux user address space at fixed base 0x400000

test_linux_fixed_base.py covers only e_phnum == RequiredProgramHeaderCount, where the table never moves, so this is untested.

Suggested fix: pass the relocated table position to the rebase pass — RebaseProgramHeaders(buf, tableOff, phEntSize, writtenPh, base); — and rebase only the headers that were written.

Found by an AI-assisted audit (DeepSeek V4.1 Flash via OpenCode); every row above was executed against the real relinker binary. I can send the fix with regression tests covering the grown-table case.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5867513854 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6393). Originally posted by [midasmatterhorn](https://github.com/midasmatterhorn) on 2026-10-15T13:56:06Z. --- When the input's program header table has to grow (fewer free slots than the output needs), `Patch` relocates the table to the end of the file (`tableOff = buf.size()`, `e_phoff` rewritten at core/relinker/elfpatcher/src/linux/LinuxElfPatcher.cpp:311-319). The `--linux-fixed-base` pass then rebases the wrong table: `RebaseProgramHeaders` (line 339) still receives the **input's** `phOff`, so - the relocated output table is never rebased — `PT_LOAD` `p_vaddr`/`p_paddr` keep un-rebased values while `e_entry` and the `DT_*` tags are rebased, producing a silently inconsistent `ET_EXEC` whose entry point lies outside every `PT_LOAD`; and - the pass instead walks the stale input table, so it also fails on input headers that `SegmentFilter` dropped from the output (`FAIL: Program header address ... lies outside the Linux user address space at fixed base 0x400000` for a `PT_DYNAMIC` address that exists only in the input). Reproduced with a relinker-only build of `main` `92cdc7b62` on macOS 27.0.1 arm64, on a minimal `ET_EXEC` fixture (one `PT_LOAD` with code, a `PT_DYNAMIC`, extra `PT_SCE_VERSION` headers to control `e_phnum`): | fixture | options | result | | --- | --- | --- | | `e_phnum = 3` (table fits) | none | `e_phoff=0x40`, entry mapped | | `e_phnum = 3` (table grows to `0x8150`) | none | `e_phoff=0x8150`, `entry=0x1118`, entry mapped | | `e_phnum = 3` (table grows) | `--linux-fixed-base 0x400000` | `e_phoff=0x8150`, `entry=0x401118`, **entry not mapped** — loads still at `0x0/0x1000/0x4000` | | `e_phnum = 6` (table fits) | `--linux-fixed-base 0x400000` | loads rebased to `0x400000...`, entry mapped (correct) | | `e_phnum = 3`, input `PT_DYNAMIC` at `0x7ffffffffff0` (dropped from output) | none | succeeds | | same | `--linux-fixed-base 0x400000` | `FAIL: Program header address 0x7ffffffffff0 lies outside the Linux user address space at fixed base 0x400000` | `test_linux_fixed_base.py` covers only `e_phnum == RequiredProgramHeaderCount`, where the table never moves, so this is untested. Suggested fix: pass the relocated table position to the rebase pass — `RebaseProgramHeaders(buf, tableOff, phEntSize, writtenPh, base);` — and rebase only the headers that were written. Found by an AI-assisted audit (DeepSeek V4.1 Flash via OpenCode); every row above was executed against the real `relinker` binary. I can send the fix with regression tests covering the grown-table case.
MarcZX10 commented 2026-10-15 14:10:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-15T14:10:03Z.


The growth case reproduces on current main: with a three-entry input table, the fixed-base output puts the entry at 0x401118 outside every PT_LOAD. PR #6403 rebases the output table at its new offset and adds a regression check that fails on the unmodified main binary and passes with the fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6194105464 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/6393#issuecomment-6194105464). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-15T14:10:03Z. --- The growth case reproduces on current main: with a three-entry input table, the fixed-base output puts the entry at `0x401118` outside every PT_LOAD. [PR #6403](https://github.com/boykopovar/AnyPS5/pull/6403) rebases the output table at its new offset and adds a regression check that fails on the unmodified main binary and passes with the fix.
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#6393
No description provided.