fix(relinker): accept weak duplicate guest exports #755

Closed
julienlgy wants to merge 1 commit from upstream-pr/755 into main
julienlgy commented 2026-10-05 20:41:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by julienlgy on 2026-10-05T20:41:56Z.


What

The relinker rejected games whose guest modules export the same NID as a weak symbol:

FAIL: Duplicate guest export after stripping #: HqLRpV9pAhc in ...\app0\prx\PSNCommon.prx and ...\app0\prx\PSNCore.prx

Cause

  • Both modules export HqLRpV9pAhc with STB_WEAK binding (st_info = 0x22, STV_PROTECTED), as HqLRpV9pAhc#C#A in PSNCommon.prx and HqLRpV9pAhc#Y#A in PSNCore.prx.
  • The 20 bytes of code behind both symbols are identical (48 8b 4e 10 48 89 f8 48 89 4f 10 c5 f8 10 06 c5 f8 11 07 c3, a 24-byte struct copy). This is ordinary C++ output: inline functions and template instantiations are emitted in every module that uses them and marked weak so that the linker keeps one copy.
  • On the console the two exports never collide because the #library#module suffix keeps them in separate namespaces. GuestModuleBuilder strips that suffix and puts every guest export in one global table, so any duplicate, weak or not, was a hard error.

Change (GuestModuleBuilder::Build)

Duplicate guest exports now follow ELF symbol binding rules:

Existing New Result
global global error (unchanged)
weak global the global definition becomes the provider
global weak the global definition stays the provider
weak weak the first definition (sorted path order) stays the provider
any any, different STT_* type error

The provider map only drives dependency ordering and the import/export type check. At run time the generated loader resolves an import with a linear search over the loaded modules, so the first definition wins, the same as ld.so without LD_DYNAMIC_WEAK. Each guest module still exports its own copy; WindowsGuestModuleWriter deduplicates exports per module only, so it needs no change.

test_guest_module_directories.py gets a weak flag on the fixture symbol and covers the weak/weak, weak/global and global/weak cases on Linux and Windows output. The existing global/global case still has to fail.

Tested

  • Windows 11, MinGW-w64 build of relinker on top of current main.
  • test_guest_module_directories.py, test_guest_tls.py, test_guest_host_libc.py and test_guest_intel_trampolines.py pass.
  • Retail Unity title with PSNCommon.prx / PSNCore.prx in app0/prx: the relinker no longer stops on the duplicate and produces eboot.exe plus all 8 guest modules. The game still exits early with 0xC0000139 (entry point not found), which is a separate missing-export issue outside this PR.

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. I investigated and wrote this with an AI assistant, then reviewed and tested every line myself.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4750533409 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/755). Originally posted by [julienlgy](https://github.com/julienlgy) on 2026-10-05T20:41:56Z. --- ### What The relinker rejected games whose guest modules export the same NID as a **weak** symbol: ``` FAIL: Duplicate guest export after stripping #: HqLRpV9pAhc in ...\app0\prx\PSNCommon.prx and ...\app0\prx\PSNCore.prx ``` **Cause** - Both modules export `HqLRpV9pAhc` with `STB_WEAK` binding (`st_info = 0x22`, `STV_PROTECTED`), as `HqLRpV9pAhc#C#A` in `PSNCommon.prx` and `HqLRpV9pAhc#Y#A` in `PSNCore.prx`. - The 20 bytes of code behind both symbols are identical (`48 8b 4e 10 48 89 f8 48 89 4f 10 c5 f8 10 06 c5 f8 11 07 c3`, a 24-byte struct copy). This is ordinary C++ output: inline functions and template instantiations are emitted in every module that uses them and marked weak so that the linker keeps one copy. - On the console the two exports never collide because the `#library#module` suffix keeps them in separate namespaces. `GuestModuleBuilder` strips that suffix and puts every guest export in one global table, so any duplicate, weak or not, was a hard error. **Change** (`GuestModuleBuilder::Build`) Duplicate guest exports now follow ELF symbol binding rules: | Existing | New | Result | |---|---|---| | global | global | error (unchanged) | | weak | global | the global definition becomes the provider | | global | weak | the global definition stays the provider | | weak | weak | the first definition (sorted path order) stays the provider | | any | any, different `STT_*` type | error | The provider map only drives dependency ordering and the import/export type check. At run time the generated loader resolves an import with a linear search over the loaded modules, so the first definition wins, the same as `ld.so` without `LD_DYNAMIC_WEAK`. Each guest module still exports its own copy; `WindowsGuestModuleWriter` deduplicates exports per module only, so it needs no change. `test_guest_module_directories.py` gets a `weak` flag on the fixture symbol and covers the weak/weak, weak/global and global/weak cases on Linux and Windows output. The existing global/global case still has to fail. ### Tested - Windows 11, MinGW-w64 build of `relinker` on top of current `main`. - `test_guest_module_directories.py`, `test_guest_tls.py`, `test_guest_host_libc.py` and `test_guest_intel_trampolines.py` pass. - Retail Unity title with `PSNCommon.prx` / `PSNCore.prx` in `app0/prx`: the relinker no longer stops on the duplicate and produces `eboot.exe` plus all 8 guest modules. The game still exits early with `0xC0000139` (entry point not found), which is a separate missing-export issue outside this PR. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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. I investigated and wrote this with an AI assistant, then reviewed and tested every line myself.
oneandonlydean commented 2026-10-05 22:56:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T22:56:55Z.


Thanks for this. #733 has landed and changed the same block in GuestModuleBuilder::Build, so this now conflicts with main. The two changes overlap in what they allow, so the rebase needs a small merge of the rules rather than taking one side:

  • On Linux, main now accepts any duplicate guest export that no module imports, and fails with "Ambiguous guest import" when one does (sharedExports and rejectSharedImport). Windows still fails on every duplicate.
  • With your change on top, a weak duplicate should pick its provider by your table and stay out of sharedExports. Otherwise an import of a weak symbol like HqLRpV9pAhc still fails as ambiguous on Linux. Two global definitions keep main's behaviour on both platforms.
  • Please keep main's existing->second == images.size() check (a duplicate inside one module) ahead of the find_if. In that case images.at(existing->second) is out of range, because the current image isn't in images yet.

Your weak test cases import the symbol through consumer.prx, so they cover the Linux import path once they run against main's code.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6004894889 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/755#issuecomment-6004894889). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T22:56:55Z. --- Thanks for this. #733 has landed and changed the same block in `GuestModuleBuilder::Build`, so this now conflicts with `main`. The two changes overlap in what they allow, so the rebase needs a small merge of the rules rather than taking one side: - On Linux, `main` now accepts any duplicate guest export that no module imports, and fails with "Ambiguous guest import" when one does (`sharedExports` and `rejectSharedImport`). Windows still fails on every duplicate. - With your change on top, a weak duplicate should pick its provider by your table and stay out of `sharedExports`. Otherwise an import of a weak symbol like `HqLRpV9pAhc` still fails as ambiguous on Linux. Two global definitions keep `main`'s behaviour on both platforms. - Please keep `main`'s `existing->second == images.size()` check (a duplicate inside one module) ahead of the `find_if`. In that case `images.at(existing->second)` is out of range, because the current image isn't in `images` yet. Your weak test cases import the symbol through `consumer.prx`, so they cover the Linux import path once they run against `main`'s code.

Pull request closed

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!755
No description provided.