fix(tools): keep p_align for segments unwrap_self copies outside load segments #7820

Merged
parl0v merged 1 commit from upstream-pr/7820 into main 2026-10-20 02:18:47 +00:00
parl0v commented 2026-10-19 08:59:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by parl0v on 2026-10-19T08:59:04Z.


What

tools/unwrap_self.py copies a segment that does not sit inside a PT_LOAD into its own region, but that region was laid out with alignment 1. On real dumps the comment segment (PT_SCE_COMMENT, p_align 0x10) therefore lands right after the last load segment, and the version segment (PT_SCE_VERSION) right after it, at offsets that break p_offset % p_align == p_vaddr % p_align. In the eboot of one title the comment segment goes to 0x1c92418, while the inner ELF has it at 0x1c8e420. The module docstring already says every region is copied to an offset that satisfies the rule.

These regions now use the segment's own p_vaddr and p_align, like the load segments (a zero or non-power-of-two p_align still lays out with alignment 1, as before). Load segment placement and bytes do not change; only the comment and version offsets move and the file grows by a few bytes of padding. The relinker does not read either segment (GuestImageReader skips them and SegmentFilter drops them), so its output does not change.

Tested

Windows 10 x64, Python 3.13.

  • python -m unittest tools.tests.test_unwrap_self: 20 tests pass. The new test_segments_copied_outside_the_load_segments_keep_their_alignment fails on main in both subcases (8 != 0 : 0x6fffff00 for a comment segment after a 0x88-byte load, 0 != 4 : 0x2 for a recorded segment with a vaddr).
  • I unwrapped the eboot, 7 sce_module libraries and 15 bundled modules of one title, plus the eboot and sce_module/libc.prx of PPSA15552. On main, 16 of the 25 outputs have a comment or version segment that breaks the rule (20 headers). With this PR every program header with p_align > 1 satisfies it. Every load and dynamic header and every segment's bytes are identical to main's output.
  • I relinked that title's eboot with its 7 sce_module libraries (--windows --to-intel) from main's output and from this PR's output with the same relinker build. Both runs succeed, and the executable and all 7 guest modules are byte-identical.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch reports no overlap with open pull requests.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4918422433 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7820). Originally posted by [parl0v](https://github.com/parl0v) on 2026-10-19T08:59:04Z. --- ### What `tools/unwrap_self.py` copies a segment that does not sit inside a PT_LOAD into its own region, but that region was laid out with alignment 1. On real dumps the comment segment (`PT_SCE_COMMENT`, `p_align` 0x10) therefore lands right after the last load segment, and the version segment (`PT_SCE_VERSION`) right after it, at offsets that break `p_offset % p_align == p_vaddr % p_align`. In the eboot of one title the comment segment goes to 0x1c92418, while the inner ELF has it at 0x1c8e420. The module docstring already says every region is copied to an offset that satisfies the rule. These regions now use the segment's own `p_vaddr` and `p_align`, like the load segments (a zero or non-power-of-two `p_align` still lays out with alignment 1, as before). Load segment placement and bytes do not change; only the comment and version offsets move and the file grows by a few bytes of padding. The relinker does not read either segment (`GuestImageReader` skips them and `SegmentFilter` drops them), so its output does not change. ### Tested Windows 10 x64, Python 3.13. - `python -m unittest tools.tests.test_unwrap_self`: 20 tests pass. The new `test_segments_copied_outside_the_load_segments_keep_their_alignment` fails on `main` in both subcases (`8 != 0 : 0x6fffff00` for a comment segment after a 0x88-byte load, `0 != 4 : 0x2` for a recorded segment with a vaddr). - I unwrapped the eboot, 7 `sce_module` libraries and 15 bundled modules of one title, plus the eboot and `sce_module/libc.prx` of PPSA15552. On `main`, 16 of the 25 outputs have a comment or version segment that breaks the rule (20 headers). With this PR every program header with `p_align` > 1 satisfies it. Every load and dynamic header and every segment's bytes are identical to `main`'s output. - I relinked that title's eboot with its 7 `sce_module` libraries (`--windows --to-intel`) from `main`'s output and from this PR's output with the same relinker build. Both runs succeed, and the executable and all 7 guest modules are byte-identical. - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` reports no overlap with open pull requests. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
adriadam10 commented 2026-10-20 02:19:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-20T02:19:25Z.


Checked the placement loop: regions for segments outside PT_LOAD now use the segment's p_vaddr/p_align, and zero or non-power-of-two alignment still falls back to 1. The new test pins the offset/vaddr rule and each segment's bytes in both layouts; merged.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6281808857 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7820#issuecomment-6281808857). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-20T02:19:25Z. --- Checked the placement loop: regions for segments outside PT_LOAD now use the segment's `p_vaddr`/`p_align`, and zero or non-power-of-two alignment still falls back to 1. The new test pins the offset/vaddr rule and each segment's bytes in both layouts; merged.
Sign in to join this conversation.
No reviewers
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!7820
No description provided.