loader: refresh imports after dynamic module loads #511

Manually merged
github merged 1 commit from gh-pr/511/head into gh-pr/511/base 2026-11-26 09:24:28 +00:00
Owner

@⁠OldKrab on GitHub · 2026-09-08 13:57 UTC · original
OldKrab:shushakov/castlevania-module-reload → main · +1 −1 · 1 files · 1 commits · merged 2026-09-23 17:04 UTC

Castlevania Dominus Collection crashes after unloading and reloading dra03.prx because the main executable retains an invalid address for an imported object.

UnloadProgram() already calls RelocateAll(). Once the module is removed, this replaces its object's address in the main executable's GOT with an invalid-object placeholder. Loading the module again currently relocates only the newly loaded module, leaving that GOT entry unchanged.

I propose calling RelocateAll() from RelocateProgram() as well, so existing imports receive the loaded module's addresses before it starts.

Validation: Linux build passed. Order of Ecclesia passed the original crash point and ran for roughly five minutes without another crash. Windows and audio remain untested.

Fixes #307.

**@⁠OldKrab** on GitHub · 2026-09-08 13:57 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/511) `OldKrab:shushakov/castlevania-module-reload` → `main` · +1 −1 · 1 files · 1 commits · merged 2026-09-23 17:04 UTC Castlevania Dominus Collection crashes after unloading and reloading `dra03.prx` because the main executable retains an invalid address for an imported object. `UnloadProgram()` already calls `RelocateAll()`. Once the module is removed, this replaces its object's address in the main executable's GOT with an invalid-object placeholder. Loading the module again currently relocates only the newly loaded module, leaving that GOT entry unchanged. I propose calling `RelocateAll()` from `RelocateProgram()` as well, so existing imports receive the loaded module's addresses before it starts. Validation: Linux build passed. Order of Ecclesia passed the original crash point and ran for roughly five minutes without another crash. Windows and audio remain untested. Fixes #307.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:24:25 +00:00
github manually merged commit 0e2d667d2b into gh-pr/511/base 2026-11-26 09:24:28 +00:00
Author
Owner

@⁠francoisatt on GitHub · 2026-09-08 17:30 UTC · original

Built and tested on Windows (clang-cl 19.1.5, Ninja, clean build) at 0e2d667.
35/36 tests pass — the only failure is kernel_file_system, which fails on
clean main too (#505). No regression from this change.

I can't run Castlevania, so I can't confirm the fix itself — only that it
builds and doesn't break the suite on the platform you couldn't test.

Two things a reviewer will probably ask about, worth addressing up front:

  • Cost: RelocateAll() re-runs over every program on each dynamic module load.
    GTA V has ~171k relocations in the main executable alone, so a title that
    loads modules frequently might notice.
  • Overwriting: #343 (closed) tackled the same problem and explicitly avoided
    RelocateAll on the grounds that it "would rewrite every live import slot" —
    it added a classifier so only linker-owned pending values were touched. Your
    approach is much simpler; the question is whether any import slot can
    legitimately be modified by guest code and get clobbered here.

Also worth noting: SharpEmu does a dedicated pass for this and logs it as
Imported data rebind: rebound=11, unresolved=0, so the general idea is
sound — it's the scope that differs between the two approaches.

**@⁠francoisatt** on GitHub · 2026-09-08 17:30 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/511#issuecomment-5589218320) Built and tested on Windows (clang-cl 19.1.5, Ninja, clean build) at 0e2d667. 35/36 tests pass — the only failure is `kernel_file_system`, which fails on clean main too (#505). No regression from this change. I can't run Castlevania, so I can't confirm the fix itself — only that it builds and doesn't break the suite on the platform you couldn't test. Two things a reviewer will probably ask about, worth addressing up front: - Cost: RelocateAll() re-runs over every program on each dynamic module load. GTA V has ~171k relocations in the main executable alone, so a title that loads modules frequently might notice. - Overwriting: #343 (closed) tackled the same problem and explicitly avoided RelocateAll on the grounds that it "would rewrite every live import slot" — it added a classifier so only linker-owned pending values were touched. Your approach is much simpler; the question is whether any import slot can legitimately be modified by guest code and get clobbered here. Also worth noting: SharpEmu does a dedicated pass for this and logs it as `Imported data rebind: rebound=11, unresolved=0`, so the general idea is sound — it's the scope that differs between the two approaches.
This discussion has been locked. Commenting is limited to contributors.
No description provided.