refactor(relinker): read the executable's DT_NEEDED entries in one place #1403

Merged
overlordxrz-source merged 1 commit from upstream-pr/1403 into main 2026-10-08 13:00:53 +00:00
overlordxrz-source commented 2026-10-08 02:31:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-08T02:31:40Z.


What

Follow-up to the note on #716: GuestModuleBuilder::Build read the executable's DT_NEEDED table twice, once to find needed modules outside the module directories and once to add the host dependencies, with the size, tag, string offset and terminator checks copied in both loops.

Both now use ReadNeededNames (GuestModuleBuilder.cpp), which validates the table and returns the names. The table is read once, before the modules are scanned, and the list is reused for the host dependencies. That is safe because nothing in between changes DynamicSegmentData, and the symbol renames only append to DynStrData, so the offsets it holds don't move. The error messages are unchanged.

One difference: the first loop used to skip entries whose tag is not DT_NEEDED, and only the second one rejected them with "Unexpected executable dependency tag". Now the shared read rejects them, so such a table also fails when no guest module is found. SysVDynamicSectionBuilder writes only DT_NEEDED entries into this table, so no input reaches that case.

Tested

Not tested with a title. The host is aarch64 Linux (Debian 13), where the full project can't be built.

  • Built the relinker target natively on aarch64 (Zig 0.17 clang with its libc++, -std=c++20) from main and from this branch. All 23 Python relinker tests (core/relinker/relinker/tests/test_*.py, including guest_needed_modules, guest_module_directories, guest_host_libc and guest_symbol_names) pass with both binaries.
  • With the fixtures from test_guest_needed_modules.py (needed module under Media/Modules, in sce_module, missing, and an executable without DT_NEEDED), for both Linux and --windows output, the two binaries give the same exit code, the same stdout and stderr, and byte-identical output files.
  • GuestModuleBuilder.cpp compiles for x86_64-linux-gnu with -Wall -Wextra -Wpedantic and no warnings.
  • python3 tools/check_conventions.py --base origin/main passes, also with this description.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4780980265 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1403). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-08T02:31:40Z. --- ### What Follow-up to the note on #716: `GuestModuleBuilder::Build` read the executable's `DT_NEEDED` table twice, once to find needed modules outside the module directories and once to add the host dependencies, with the size, tag, string offset and terminator checks copied in both loops. Both now use `ReadNeededNames` (`GuestModuleBuilder.cpp`), which validates the table and returns the names. The table is read once, before the modules are scanned, and the list is reused for the host dependencies. That is safe because nothing in between changes `DynamicSegmentData`, and the symbol renames only append to `DynStrData`, so the offsets it holds don't move. The error messages are unchanged. One difference: the first loop used to skip entries whose tag is not `DT_NEEDED`, and only the second one rejected them with "Unexpected executable dependency tag". Now the shared read rejects them, so such a table also fails when no guest module is found. `SysVDynamicSectionBuilder` writes only `DT_NEEDED` entries into this table, so no input reaches that case. ### Tested Not tested with a title. The host is aarch64 Linux (Debian 13), where the full project can't be built. - Built the `relinker` target natively on aarch64 (Zig 0.17 clang with its libc++, `-std=c++20`) from `main` and from this branch. All 23 Python relinker tests (`core/relinker/relinker/tests/test_*.py`, including `guest_needed_modules`, `guest_module_directories`, `guest_host_libc` and `guest_symbol_names`) pass with both binaries. - With the fixtures from `test_guest_needed_modules.py` (needed module under `Media/Modules`, in `sce_module`, missing, and an executable without `DT_NEEDED`), for both Linux and `--windows` output, the two binaries give the same exit code, the same stdout and stderr, and byte-identical output files. - `GuestModuleBuilder.cpp` compiles for x86_64-linux-gnu with `-Wall -Wextra -Wpedantic` and no warnings. - `python3 tools/check_conventions.py --base origin/main` passes, also with this description. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
Dextroid17 commented 2026-10-08 03:17:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T03:17:39Z.

Review: COMMENTED


Built it and ran the suite, then traced the one behavioural difference the dedup introduces.

Linux (CachyOS, GCC, Release):

cmake --build build --target relinker --parallel 16
ctest -R 'needed|relinker|module'   →  6/6 passed      (incl. guest_needed_modules)
full suite, ctest -j 16             →  410/410 passed   (28.75 s)

The refactor is a real dedup. The same eight-line walk appeared twice in the file; it is now one ReadNeededNames. guest_needed_modules is the test that exercises this path and it passes.

One behavioural difference, and I traced it far enough to say it cannot fire today.

- if (Io::ReadU64(dynamic.DynamicSegmentData, offset) != 1) continue;      // skip a non-DT_NEEDED tag
+ if (Io::ReadU64(dynamic.DynamicSegmentData, offset) != 1) throw Domain::RelinkerException("Unexpected executable dependency tag");

A reader that skipped other tags is now a reader that rejects them. That is only safe because of an invariant elsewhere, and it holds: the sole writer of DynamicSegmentData is this same file, and it clear()s the container before appending tag-1 pairs only:

dynamic.DynamicSegmentData.clear();
Io::AppendU64(dynamic.DynamicSegmentData, 1);                    // DT_NEEDED
Io::AppendU64(dynamic.DynamicSegmentData, dynamic.DynStrData.size());

So the container is always dependency-only, the old continue was unreachable, and the new throw is a defensive assertion rather than a narrowing users can hit. Worth a sentence in the body all the same: the reader now depends on that invariant, so a future producer that hands it a raw .dynamic — which a real executable fills with DT_STRTAB, DT_SYMTAB, DT_HASH and the rest — would hard-fail where the old code quietly worked. If the invariant is meant to be permanent, saying so here is the cheap way to keep it.

The validation you added around it is the right kind: the % 16 check, the string-offset bound and the unterminated-string case are all conditions a truncated or hostile input reaches, and they throw with distinct messages rather than reading past the end.

Nothing blocking. The dedup is worth having on its own.

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5450943853 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1403#pullrequestreview-5450943853). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T03:17:39Z. **Review:** COMMENTED --- Built it and ran the suite, then traced the one behavioural difference the dedup introduces. **Linux (CachyOS, GCC, Release):** ``` cmake --build build --target relinker --parallel 16 ctest -R 'needed|relinker|module' → 6/6 passed (incl. guest_needed_modules) full suite, ctest -j 16 → 410/410 passed (28.75 s) ``` **The refactor is a real dedup.** The same eight-line walk appeared twice in the file; it is now one `ReadNeededNames`. `guest_needed_modules` is the test that exercises this path and it passes. **One behavioural difference, and I traced it far enough to say it cannot fire today.** ```diff - if (Io::ReadU64(dynamic.DynamicSegmentData, offset) != 1) continue; // skip a non-DT_NEEDED tag + if (Io::ReadU64(dynamic.DynamicSegmentData, offset) != 1) throw Domain::RelinkerException("Unexpected executable dependency tag"); ``` A reader that skipped other tags is now a reader that rejects them. That is only safe because of an invariant elsewhere, and it holds: the sole writer of `DynamicSegmentData` is this same file, and it `clear()`s the container before appending tag-1 pairs only: ```cpp dynamic.DynamicSegmentData.clear(); Io::AppendU64(dynamic.DynamicSegmentData, 1); // DT_NEEDED Io::AppendU64(dynamic.DynamicSegmentData, dynamic.DynStrData.size()); ``` So the container is always dependency-only, the old `continue` was unreachable, and the new `throw` is a defensive assertion rather than a narrowing users can hit. Worth a sentence in the body all the same: the reader now *depends* on that invariant, so a future producer that hands it a raw `.dynamic` — which a real executable fills with `DT_STRTAB`, `DT_SYMTAB`, `DT_HASH` and the rest — would hard-fail where the old code quietly worked. If the invariant is meant to be permanent, saying so here is the cheap way to keep it. The validation you added around it is the right kind: the `% 16` check, the string-offset bound and the unterminated-string case are all conditions a truncated or hostile input reaches, and they throw with distinct messages rather than reading past the end. Nothing blocking. The dedup is worth having on its own. This is a comment, not an approval — I have no merge rights here. Written with AI assistance.
adriadam10 commented 2026-10-08 13:00:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T13:00:49Z.


Checked on Linux with this merged onto current main: build + ctest, the guest relinker tests (guest_needed_modules, guest_module_directories, guest_host_libc, guest_symbol_names) pass; Linux, Windows and conventions CI green. The only behaviour change (non-DT_NEEDED tag now throws in the first read too) can't be reached: the table is only written by SysVDynamicSectionBuilder.cpp:155 and GuestModuleBuilder.cpp:237, both DT_NEEDED-only, and later string appends don't move the offsets. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6060434555 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1403#issuecomment-6060434555). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T13:00:49Z. --- Checked on Linux with this merged onto current main: build + ctest, the guest relinker tests (`guest_needed_modules`, `guest_module_directories`, `guest_host_libc`, `guest_symbol_names`) pass; Linux, Windows and conventions CI green. The only behaviour change (non-`DT_NEEDED` tag now throws in the first read too) can't be reached: the table is only written by `SysVDynamicSectionBuilder.cpp:155` and `GuestModuleBuilder.cpp:237`, both `DT_NEEDED`-only, and later string appends don't move the offsets. Merging.
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!1403
No description provided.