refactor(relinker): read the executable's DT_NEEDED entries in one place #1403
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1403"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-08T02:31:40Z.
What
Follow-up to the note on #716:
GuestModuleBuilder::Buildread the executable'sDT_NEEDEDtable 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 changesDynamicSegmentData, and the symbol renames only append toDynStrData, 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.SysVDynamicSectionBuilderwrites onlyDT_NEEDEDentries 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.
relinkertarget natively on aarch64 (Zig 0.17 clang with its libc++,-std=c++20) frommainand from this branch. All 23 Python relinker tests (core/relinker/relinker/tests/test_*.py, includingguest_needed_modules,guest_module_directories,guest_host_libcandguest_symbol_names) pass with both binaries.test_guest_needed_modules.py(needed module underMedia/Modules, insce_module, missing, and an executable withoutDT_NEEDED), for both Linux and--windowsoutput, the two binaries give the same exit code, the same stdout and stderr, and byte-identical output files.GuestModuleBuilder.cppcompiles for x86_64-linux-gnu with-Wall -Wextra -Wpedanticand no warnings.python3 tools/check_conventions.py --base origin/mainpasses, also with this description.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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):
The refactor is a real dedup. The same eight-line walk appeared twice in the file; it is now one
ReadNeededNames.guest_needed_modulesis the test that exercises this path and it passes.One behavioural difference, and I traced it far enough to say it cannot fire today.
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
DynamicSegmentDatais this same file, and itclear()s the container before appending tag-1 pairs only:So the container is always dependency-only, the old
continuewas unreachable, and the newthrowis 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 withDT_STRTAB,DT_SYMTAB,DT_HASHand 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
% 16check, 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.
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_NEEDEDtag now throws in the first read too) can't be reached: the table is only written bySysVDynamicSectionBuilder.cpp:155andGuestModuleBuilder.cpp:237, bothDT_NEEDED-only, and later string appends don't move the offsets. Merging.