feat(relinker): emit DT_GNU_HASH and exclude undefined symbols from DT_HASH #10917
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10917"
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 slugzero on 2026-10-25T09:54:25Z.
What
LinuxElfPatcheremitted only a single-bucket SysVDT_HASHtable (nbucket = 1) that chained every symbol in.dynsym—including all undefined imported symbols (SHN_UNDEF)—sequentially into bucket 0 (1 -> 2 -> ... -> N-1). Because the main executable stands at the head of glibc's global lookup scope (l_searchlist),ld.sowalked that entire chain ofSHN_UNDEFentries in the main executable whenever a lookup was not rejected by a bloom filter, turning startup symbol resolution acrossNimports into anO(N^2)linear scan.DT_GNU_HASH = 0x6ffffef5toElfConstants.hpp.LinuxElfPatcher::Patchto emit both a SysVDT_HASHtable and aDT_GNU_HASHtable (with a 64-bit bloom filter) containing only defined dynamic symbols (st_shndx != SHN_UNDEF).GnuHash(name) % numDefined, and build bothDT_HASHandDT_GNU_HASHfrom the final post-sort symbol order.definedSymbols.empty()), emit an empty 1-bucketDT_HASH(buckets[0] = 0) and an emptyDT_GNU_HASH(symoffset = symbolCount,bloom[0] = 0,buckets[0] = 0) sold.soskips the main executable inO(1)during symbol lookup.test_linux_fixed_base.pyto expectDT_GNU_HASHalongsideDT_HASH, and expandtest_linux_executable_exports.pywith a 4-export fixture (Ae,Ad,Ac,Abhashing to distinctElfHash % 4andGnuHash % 4buckets) verified under both normalDT_GNU_HASHexecution andDT_HASHfallback (retaggingDT_GNU_HASHto0x6ffffdf8).Tested
Linux x86_64 (kernel 7.0.0-31-generic, GCC, Ninja, Release). Both arms were built in the same relinker-only build directory (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release):main(f06d7fdcee) and this PR (e46c9ef7f6).LD_DEBUG=statistics): Extendedtest_linux_load_alignment.fixture()withN \in \{1024, 2048, 4096\}undefinedR_X86_64_GLOB_DATimports resolved against a generated shared library (cc -shared), converted the fixture withrelinker --skip-sce-moduleonmainand on this PR, and ran each output 11 times underLD_DEBUG=statistics:SHN_UNDEF)main(f06d7fdcee) relocation cycles (median of 11)e46c9ef7f6) relocation cycles (median of 11)maintotal loader cyclesctest), includinglinux_executable_exports(1-export and 4-exportAe,Ad,Ac,Abfixtures under bothDT_GNU_HASHandDT_HASHfallback0x6ffffdf8),linux_fixed_base,linux_trace_imports, andelf_header.python3 tools/check_conventions.py --base origin/mainpasses. Verified that #7532 is an unrelated PR and not a dependency, and thatgit merge-tree --write-tree HEAD refs/pull/7532/headexits 0 (2ada440ae4, clean automatic merge).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T10:02:18Z.
Review: APPROVED
Reviewed exact head
14c32bf8f7a1951b124c9122cefa9de593a5e033. The SysV and GNU hash tables are generated from defined symbols; reordering is confined to the defined-symbol suffix, so undefined-symbol indices used by relocations remain unchanged. The added Linux executable-export test exercisesdlsymin both ET_DYN and fixed-base output, and the author reports a real Minecraft launch. I independently rangit diff --check upstream/main...HEADandpython3 tools/check_conventions.py --base upstream/main --head HEAD; both passed. Hosted relinker checks for Ubuntu, Windows, macOS arm64 and macOS x86_64 plus ASan all pass. Product build jobs are skipped by repository path policy; I did not run the Linux executable-export test locally because this checkout has no built relinker binary.Imported from boykopovar/AnyPS5. Originally posted by ahmadmuj on 2026-10-25T11:09:27Z.
Review: COMMENTED
Reviewed head
14c32bf8f7a1, merged locally onto mainb91a6a60c5ab(clean). Linux x86_64, relinker-only build of both main and the merged tree.What I ran:
ctest -R linuxon the merged build: 17/17 pass.test_linux_load_alignment.fixture(), exportsAe, Ad, Ac, Ab,unused-filter=0) through main's relinker and this PR's, then a probe that dlsyms every export and checks the relocated slots. Both rc 42.The GNU hash side looks right: all 4 exports resolve through it, and glibc loads the output fine.
DT_HASH goes stale once there are 2+ exports. The SysV buckets and chains are filled with the pre-sort indices (
LinuxElfPatcher.cpp, thesysvChains[symIdx]loop), then thestable_sort+ memcpy moves the exports, and DT_HASH is never rebuilt.Example: exports at dynsym 4..7 in order
Ae, Ad, Ac, Ab. ELF hash % 4 is 1, 0, 3, 2, so DT_HASH getsbuckets=[5,4,7,6]with all chains 0. GNU hash % 4 is 3, 2, 1, 0, so the sort turns 4..7 intoAb, Ac, Ad, Ae. Now bucket 0 (meant forAd) points at index 5, which isAc, and all 4 DT_HASH lookups miss.glibc doesn't notice because it prefers DT_GNU_HASH. If I retag DT_GNU_HASH as DT_CHECKSUM on the PR's output so ld.so falls back to DT_HASH, it fails:
symbol lookup error: ./sysv-only.elf: undefined symbol: Ad, rc 127. So anything that still reads DT_HASH gets wrong answers. Building the SysV table after the sort, from the final order, fixes it.Smaller notes:
SysVDynamicSectionBuilder.cppemits the UNDEF imports first,RelinkerPipeline.cppappends exports last) and no relocation names an export. Both hold today, butPatchdoesn't check either. CallingPatchdirectly with dynsym[null, Ae, Imp(UNDEF), Ac]gives[null, Ae, Ac, Ac], and the GLOB_DAT againstImpnow namesAc. A throw when the defined indices aren't contiguous, or when a relocation's r_sym falls in the defined range, would stop a later change from mis-binding silently.Ae, Ad, Ac, Abcase above plus a DT_HASH lookup would catch this.--trace-importsthe UNDEFTraceImport_nid_no_patchsits after the exports, so it's past symoffset but outside the GNU chain array. glibc doesn't care; tools that count dynsym from DT_GNU_HASH will come up one short.Limits: synthetic fixtures only, no game dumps, so I can't confirm the Minecraft startup numbers.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T13:41:20Z.
Review: APPROVED
Re-reviewed exact updated head
1ad89aa8e7da35fd51389525dfe0b1229fec317f, including the follow-up SysV hash fix. Defined dynamic symbols are contiguous after the undefined prefix; the code explicitly verifies that and rejects relocations referencing the region before reordering, so existing undefined-symbol indices remain stable. The common sorted export order now feeds both SysV chains/buckets and GNU buckets/chains, avoiding a mismatch between the two lookup formats. The added multi-export fixture deliberately uses names whose bucket ordering differs from input order, checks exported lookup through normal dual-hash output, then changes the dynamic tag to force the SysV-only path and runs the executable again.check_conventions.pyandgit diff --checkpass locally. Hosted Ubuntu, Windows, macOS ARM/x86_64, and ASan relinker jobs are green; product jobs are skipped by policy. The earlier independent Linux review also exercised executable exports on a merge build.