PS5 ELF loader: handle DT_REL (legacy) relocation format #42
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!42
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/42/head"
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?
@abdulazizFarhan on GitHub · 2026-07-18 18:54 UTC · original
abdulazizFarhan:feature/dt-rel-loader-fix→main· +97 −10 · 3 files · 1 commits · closed without merging# PS5 ELF loader: handle DT_REL (legacy) relocation format
Summary
Kyty's ELF dynamic loader only accepted
DT_RELA(relocation entries withexplicit addends). Every PS5 SDK homebrew ELF (e.g. elfldr, ftpsrv, gdbsrv,
klogsrv, shsrv, websrv from issue #33) ships
DT_RELinstead, so the loadercrashed at
runtimeLinker.cpp:2120withNot implemented (true)before theguest program could ever start. This PR teaches the loader to expand
DT_RELrecords intoDT_RELAin-place, matching glibcld.so's behaviour.Root cause
ParseProgramDynamicInfohad three hard-coded exit points for ELFs thatwere not DT_RELA-only:
EXIT_NOT_IMPLEMENTED(jmprel_type != DT_RELA)— bailed on anyDT_PLTRELvalue other than 7.EXIT_NOT_IMPLEMENTED(!is_shared && !is_next_gen)— refused to loadany non-shared ELF whose abiversion was <2 (i.e. every legacy SDK ELF).
EXIT_NOT_IMPLEMENTED(program->dynamic_info->jmprela_table == nullptr)in
RelocateProgram— even after the table was correctly populated,this assertion ran before the call, so an empty PLT (legitimate
for ELFs without
.plt) aborted the load.Fix
Three small, surgical changes in
src/loader/:runtimeLinker.cpp: introduce a local
Elf64_Relmirror struct and aUpgradeRelToRela(rel_addr, rel_sz)helper. For eachElf64_Relrecordwe allocate a fresh
Elf64_Rela, copyr_offset/r_info, and resolvethe implicit addend by reading the current 64-bit value at the target
address. This is exactly what glibc
ld.sodoes inelf/dl-reloc.celf_dynamic_do_rel()for ELF64.The PLT branch (
DT_PLTREL == DT_REL) and the main relocation tablebranch both call the helper, then patch the corresponding
*_table_sizefields to match the new
Elf64_Relasize (24 bytes) instead ofElf64_Rel(16 bytes). A newjmprel_type == 0case handles ELFs withno
.plt(e.g. self-contained loader ELFs).The
RelocateProgramfunction now skips the PLT pass whenjmprela_table == nullptr.elf.h: define the missing
DT_RELENT(0x12) andDT_OS_RELENT(0x61000032) constants. Also fix the
GetDynamicData<T>template —it returned
nullptrin the "no data" branch, which is ill-formed fornon-pointer
T(e.g.uint64_t). Replaced withT{}which yields aproperly zero-initialised value for any
T.elf.cpp / elf.h (from a prior loader-fix pass): accept standard
ET_DYN(3) in addition to the Sony-specificET_DYNEXEC/ET_DYNAMICtypes. PS5 SDK ELFs ship as
ET_DYNso this is required just to reachthe relocation parser.
Evidence
Smoke tests against the 6 PS5 SDK test ELFs from issue #33, with the fix
applied:
Not implemented (true)at line 2060 (DT_REL)Not implemented (true)at line 2060Access violation: Execute [0x1]— i.e. jumps to a NULL stub (loader-level issue fully cleared)Not implemented (true)at line 2060All six ELFs are now past the DT_REL gate. The remaining failures are
unrelated loader-gate or Windows-memory issues, out of scope for this PR.
Out of scope / future work
EXIT_NOT_IMPLEMENTED(rel_entsz != 0 && rel_entsz != sizeof(Elf64_Rel))check is a guard rail: an ELF that advertises a non-zero DT_RELENT but
with a value different from 16 bytes will still abort. In practice every
ABI-compliant toolchain writes 16; we can soften this later if a real ELF
ever trips it.
CreateFileMapping 0x5AA) is a separate,pre-existing issue in
DirectMemoryBacking::SelfTest. Will be addressedin its own PR.
AI-assisted disclosure
This PR was authored with significant AI assistance (Claude / MiniMax-M3)
working from a user-supplied goal ("make one perfect PR, that realy
improve"). All code paths were smoke-tested against six real PS5 SDK ELFs.
Closes / unblocks kyty issue #33.
@Nmzik on GitHub · 2026-07-29 07:07 UTC · original · requested changes
Requesting changes for three blocking DT_REL correctness defects: the standard dynamic-tag constants are off by one, implicit addends are read without applying the program load bias, and the main relocation table is never actually loaded from DT_REL/DT_RELSZ. As written, ordinary rebased or multi-entry DT_REL images can still fail or read invalid memory. No CI run exists for this head.
@ -102,6 +103,8 @@ constexpr Elf64_Sxword DT_PREINIT_ARRAY = 0x00000020;constexpr Elf64_Sxword DT_PREINIT_ARRAYSZ = 0x00000021;constexpr Elf64_Sxword DT_REL = 0x00000011;constexpr Elf64_Sxword DT_RELA = 0x00000007;constexpr Elf64_Sxword DT_RELENT = 0x00000012;@Nmzik on GitHub · 2026-07-29 07:07 UTC · original
DT_RELENTis tag 19 (0x13); tag 18 (0x12) isDT_RELSZ. With this value, the lookup below reads the table's total byte size as the entry size, so a normal table containing more than one record fails thesizeof(Elf64_Rel)check. Define bothDT_RELSZ = 0x12andDT_RELENT = 0x13.@ -50,0 +74,4 @@// Implicit addend = current 64-bit value at the relocation target// (this is what ld.so does for ELF64 DT_REL records during// apply_relocations() when r_addend is not stored on-disk).rela[i].r_addend = static_cast<Elf64_Sxword>(*reinterpret_cast<uint64_t*>(rela[i].r_offset));@Nmzik on GitHub · 2026-07-29 07:07 UTC · original
For an
ET_DYNimage,r_offsetis relative to the load bias—the existing relocation path itself targetsprogram->base_vaddr + r_offset. Dereferencing the raw offset here therefore reads an unmapped or unrelated host address whenever the image was rebased. Pass the program base into this conversion and read the implicit addend frombase_vaddr + r_offset.@ -2064,2 +2134,4 @@EXIT_NOT_IMPLEMENTED(elf->HasDynValue(DT_OS_RELA) && elf->HasDynValue(DT_RELA));Elf64_Sxword rel_type = 0;GetDynValue(elf, &rel_type, DT_OS_RELA);@Nmzik on GitHub · 2026-07-29 07:07 UTC · original
The value stored in a
DT_RELAdynamic entry is the relocation-table address, not a relocation-format discriminator, sorel_type == DT_RELcannot correctly detect the main REL table. This block also never readsDT_REL/DT_RELSZ; on a DT_REL-only imagerela_tableremains null andRelocate()still aborts. Detect theDT_RELtag explicitly, fetch its address andDT_RELSZ, validateDT_RELENT, and convert that table.Pull request closed