fix(nid): reject ELF exports that patch to different symbols under one name #5849

Merged
wachi35676 merged 1 commit from upstream-pr/5849 into main 2026-10-16 19:24:02 +00:00
wachi35676 commented 2026-10-14 13:45:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-14T13:45:17Z.


What

ElfNidPatcher::PatchNids (core/libs/nid/src/ElfNidPatcher.cpp) renames each defined export to its NID but never checked that the new names are unique. Two exports at different addresses that map to the same name, such as foo and foo_nid_disambig1, foo_nid_postfix and foo_nid_disambig12, or X_nid_no_patch and X_nid_no_patch_nid_no_patch_cut, were both written as one name. The patcher printed OK and exited 0, leaving two defined dynamic symbols of the same name where a lookup reaches only one. The PE and Mach-O back ends already reject this (PeNidPatcher.cpp:172-173, MachONidPatcher.cpp:307).

The ELF patcher now remembers the address (st_value, st_shndx) of each patched export name and throws duplicate patched export name: <name> only when the same name comes from a different address. Aliases of one address are kept, because a lookup of the name resolves to the same code either way. The Linux libc has six such groups from GCC_GLOBAL_ALIAS(X_nid_no_patch_cut, X_nid_postfix) next to GCC_GLOBAL_ALIAS(X, X_nid_postfix) in core/libs/prx/libc/src/exception/Exports.cpp: __cxa_throw, __cxa_allocate_exception, __cxa_begin_catch, __cxa_end_catch, __cxa_rethrow and _Unwind_Resume. Removing those aliases instead would change libc's export list, which every other library reads through --preserve-export-list.

The Linux libc also had one real collision, already present in the patched libc on main. __gxx_personality_v0 was exported twice at different addresses: the forwarding function in Personality.cpp:105, and the __gxx_personality_v0_nid_no_patch_cut alias of the guest personality (Exports.cpp:21). That alias is removed:

  • GCC_GLOBAL_ALIAS only emits it on Linux, and nothing in the tree references the name;
  • the forwarding function passes its arguments straight to __gxx_personality_v0_nid_postfix, and Unwind.cpp:106 accepts either pointer;
  • the PE libc already exports exactly this pair, __gxx_personality_v0 and the NID.

PE and Mach-O never get these aliases, because GCC_GLOBAL_ALIAS is Linux-only and the _WIN32 block defines separate forwarding functions. That is why their stricter checks pass.

New core/libs/nid/tests/test_elf_duplicates.py (ctest nid_elf_duplicates):

  • three real collisions must fail with exit 2 and leave the file byte-identical;
  • X, X_nid_postfix and X_nid_no_patch_cut at one address must patch, giving {X, X, nid(X)} at that address;
  • the same names with _nid_no_patch_cut at another address must be rejected;
  • a control with distinct names still patches.

Tested

Rebased on main 44df21632.

  • Linux (WSL Ubuntu 24.04, GCC 13.3, Release):
    • Full build of the tree succeeds (2505/2505 steps; nid_patcher runs for all 126 libraries). With the previous commit of this PR, Patching NIDs for libc fails with duplicate patched export name: __cxa_throw.
    • ctest -E "agc_|video_out": 647 of 648 pass, including nid_elf_duplicates and the other nid_* tests. The one failure, guest_named_shm_concurrency ("unlink of a read-only shared object did not return EACCES"), comes from running as root in WSL. The same binary passes as a normal user, and this change does not touch shared memory.
    • test_elf_duplicates.py against main's patcher: 4 failures (the 3 collisions and the different-address case). Against the previous commit: 1 failure (the same-address case). With this change: 4/4 pass.
  • Windows (WinLibs GCC 15.2.0): full build (the only failure is agc_driver_gpu_indirect_arguments_misaligned_writer_ahead_tests, which fails to compile on main with MinGW, "requested alignment 65536 exceeds object file maximum 8192", and is not touched here); 424 of 424 guest_*, libkernel*, nid*, libc* and stream tests pass, including nid_elf_duplicates and patched_libraries.
  • python tools/check_conventions.py --base origin/main passes.

The first version of this PR said no library had such a pair. That was wrong for the Linux libc: I had only built it on Windows. Thanks to @adriadam10 for catching it.

Checklist

  • Based on current main; no other open PR implements the same functions (#4721 touches other files in core/libs/nid)
  • 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 under Silent stubs in their library's technical debt file
  • 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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4858852602 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5849). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-14T13:45:17Z. --- ### What `ElfNidPatcher::PatchNids` (`core/libs/nid/src/ElfNidPatcher.cpp`) renames each defined export to its NID but never checked that the new names are unique. Two exports at different addresses that map to the same name, such as `foo` and `foo_nid_disambig1`, `foo_nid_postfix` and `foo_nid_disambig12`, or `X_nid_no_patch` and `X_nid_no_patch_nid_no_patch_cut`, were both written as one name. The patcher printed `OK` and exited 0, leaving two defined dynamic symbols of the same name where a lookup reaches only one. The PE and Mach-O back ends already reject this (`PeNidPatcher.cpp:172-173`, `MachONidPatcher.cpp:307`). The ELF patcher now remembers the address (`st_value`, `st_shndx`) of each patched export name and throws `duplicate patched export name: <name>` only when the same name comes from a different address. Aliases of one address are kept, because a lookup of the name resolves to the same code either way. The Linux libc has six such groups from `GCC_GLOBAL_ALIAS(X_nid_no_patch_cut, X_nid_postfix)` next to `GCC_GLOBAL_ALIAS(X, X_nid_postfix)` in `core/libs/prx/libc/src/exception/Exports.cpp`: `__cxa_throw`, `__cxa_allocate_exception`, `__cxa_begin_catch`, `__cxa_end_catch`, `__cxa_rethrow` and `_Unwind_Resume`. Removing those aliases instead would change libc's export list, which every other library reads through `--preserve-export-list`. The Linux libc also had one real collision, already present in the patched libc on `main`. `__gxx_personality_v0` was exported twice at different addresses: the forwarding function in `Personality.cpp:105`, and the `__gxx_personality_v0_nid_no_patch_cut` alias of the guest personality (`Exports.cpp:21`). That alias is removed: - `GCC_GLOBAL_ALIAS` only emits it on Linux, and nothing in the tree references the name; - the forwarding function passes its arguments straight to `__gxx_personality_v0_nid_postfix`, and `Unwind.cpp:106` accepts either pointer; - the PE libc already exports exactly this pair, `__gxx_personality_v0` and the NID. PE and Mach-O never get these aliases, because `GCC_GLOBAL_ALIAS` is Linux-only and the `_WIN32` block defines separate forwarding functions. That is why their stricter checks pass. New `core/libs/nid/tests/test_elf_duplicates.py` (ctest `nid_elf_duplicates`): - three real collisions must fail with exit 2 and leave the file byte-identical; - `X`, `X_nid_postfix` and `X_nid_no_patch_cut` at one address must patch, giving {`X`, `X`, nid(`X`)} at that address; - the same names with `_nid_no_patch_cut` at another address must be rejected; - a control with distinct names still patches. ### Tested Rebased on `main` `44df21632`. - **Linux** (WSL Ubuntu 24.04, GCC 13.3, Release): - Full build of the tree succeeds (2505/2505 steps; `nid_patcher` runs for all 126 libraries). With the previous commit of this PR, `Patching NIDs for libc` fails with `duplicate patched export name: __cxa_throw`. - `ctest -E "agc_|video_out"`: 647 of 648 pass, including `nid_elf_duplicates` and the other `nid_*` tests. The one failure, `guest_named_shm_concurrency` ("unlink of a read-only shared object did not return EACCES"), comes from running as root in WSL. The same binary passes as a normal user, and this change does not touch shared memory. - `test_elf_duplicates.py` against `main`'s patcher: 4 failures (the 3 collisions and the different-address case). Against the previous commit: 1 failure (the same-address case). With this change: 4/4 pass. - **Windows** (WinLibs GCC 15.2.0): full build (the only failure is `agc_driver_gpu_indirect_arguments_misaligned_writer_ahead_tests`, which fails to compile on `main` with MinGW, "requested alignment 65536 exceeds object file maximum 8192", and is not touched here); 424 of 424 `guest_*`, `libkernel*`, `nid*`, `libc*` and stream tests pass, including `nid_elf_duplicates` and `patched_libraries`. - `python tools/check_conventions.py --base origin/main` passes. The first version of this PR said no library had such a pair. That was wrong for the Linux libc: I had only built it on Windows. Thanks to @adriadam10 for catching it. ### 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 (#4721 touches other files in `core/libs/nid`) - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Dextroid17 commented 2026-10-15 02:09:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-15T02:09:47Z.


I ran this PR's test on Linux.

Setup: this PR's diff applied to main at 8cd937e29 (the PR's base is 485 commits behind it), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), clean environment. ctest -R '^nid_elf_duplicates$', 3 runs each. The test did not open a GPU device, so this is a CPU-only run.

  • with this PR: 3 of 3 pass.
  • with nid/src/ElfNidPatcher.cpp put back to main's version (the new test kept): 3 of 3 fail. All three name sets in test_exports_patched_to_one_name_are_rejected fail with AssertionError: 0 != 2 (the patcher exits 0 with no error instead of rejecting the duplicate).

So the new test catches the old behaviour here. I did not check Windows or macOS.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6182792861 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5849#issuecomment-6182792861). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-15T02:09:47Z. --- I ran this PR's test on Linux. Setup: this PR's diff applied to main at 8cd937e29 (the PR's base is 485 commits behind it), default CMake build (CMAKE_BUILD_TYPE empty, unoptimized), clean environment. `ctest -R '^nid_elf_duplicates$'`, 3 runs each. The test did not open a GPU device, so this is a CPU-only run. - with this PR: 3 of 3 pass. - with nid/src/ElfNidPatcher.cpp put back to main's version (the new test kept): 3 of 3 fail. All three name sets in `test_exports_patched_to_one_name_are_rejected` fail with `AssertionError: 0 != 2` (the patcher exits 0 with no error instead of rejecting the duplicate). So the new test catches the old behaviour here. I did not check Windows or macOS. AI-assisted: yes
github-actions[bot] commented 2026-10-15 12:10:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T12:10:26Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6104 core/libs/prx/libc/src/exception/Exports.cpp L16-22

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/CMakeLists.txt
PR 5962 core/libs/CMakeLists.txt
PR 6044 core/libs/CMakeLists.txt
PR 6222 core/libs/CMakeLists.txt
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191718476 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5849#issuecomment-6191718476). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T12:10:26Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 6104](https://www.github.com/boykopovar/AnyPS5/pull/6104) | `core/libs/prx/libc/src/exception/Exports.cpp` [L16-22](https://github.com/boykopovar/AnyPS5/blob/c84249186528cf14356f9fee51022c3f165ff262/core/libs/prx/libc/src/exception/Exports.cpp#L16-L22) | | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/CMakeLists.txt` | | [PR 5962](https://www.github.com/boykopovar/AnyPS5/pull/5962) | `core/libs/CMakeLists.txt` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/CMakeLists.txt` | | [PR 6222](https://www.github.com/boykopovar/AnyPS5/pull/6222) | `core/libs/CMakeLists.txt` |
adriadam10 commented 2026-10-15 13:43:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T13:43:05Z.

Review: CHANGES_REQUESTED


Thanks for the check, the idea matches the PE and Mach-O back ends. But it breaks the Linux build of libc on current main:

  • Patching NIDs for libc fails with duplicate patched export name: __cxa_throw. libc defines __cxa_throw, __cxa_throw_nid_postfix and __cxa_throw_nid_no_patch_cut as aliases of one symbol (core/libs/prx/libc/src/exception/Exports.cpp:17 and :27), and all three patch to __cxa_throw. The "no library in the tree has such a pair" claim in the description doesn't hold for the Linux libc (nm -D on libs/unpatched/libc.prx shows the three at 0x969b0).
  • Please build the full tree on Linux (nid_patcher runs for every library) and fix the fallout: either remove the redundant alias, or make the check skip exports that resolve to the same address (same st_value) since those are not ambiguous lookups. Say which one you choose and why, and keep the test cases for the real collisions plus one for same-address aliases.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5534082856 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5849#pullrequestreview-5534082856). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T13:43:05Z. **Review:** CHANGES_REQUESTED --- Thanks for the check, the idea matches the PE and Mach-O back ends. But it breaks the Linux build of libc on current main: - `Patching NIDs for libc` fails with `duplicate patched export name: __cxa_throw`. libc defines `__cxa_throw`, `__cxa_throw_nid_postfix` and `__cxa_throw_nid_no_patch_cut` as aliases of one symbol (`core/libs/prx/libc/src/exception/Exports.cpp:17` and `:27`), and all three patch to `__cxa_throw`. The "no library in the tree has such a pair" claim in the description doesn't hold for the Linux libc (`nm -D` on `libs/unpatched/libc.prx` shows the three at `0x969b0`). - Please build the full tree on Linux (`nid_patcher` runs for every library) and fix the fallout: either remove the redundant alias, or make the check skip exports that resolve to the same address (same `st_value`) since those are not ambiguous lookups. Say which one you choose and why, and keep the test cases for the real collisions plus one for same-address aliases.
wachi35676 commented 2026-10-15 19:27:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-15T19:27:29Z.


Thanks, you're right. I had only built the libraries on Windows, and the description's claim was wrong for the Linux libc. Small correction to the symbol list: __cxa_throw and __cxa_throw_nid_no_patch_cut both become __cxa_throw, while __cxa_throw_nid_postfix becomes its NID (vkuuLfhnSZI).

I chose the same-address rule. The check now keeps st_value and st_shndx for each patched name and throws only when the name comes from a different address. Aliases of one address resolve to the same code, and removing them would change libc's export list, which the other libraries read through --preserve-export-list.

That alone wasn't enough, though. The Linux libc also exported __gxx_personality_v0 twice at different addresses, and the patched libc on main has both today:

  • the forwarding function in Personality.cpp:105;
  • the __gxx_personality_v0_nid_no_patch_cut alias of the guest personality (Exports.cpp:21).

So I also removed that one alias. It is Linux-only and unreferenced, the forwarding function passes its arguments straight through, Unwind.cpp:106 accepts either pointer, and the PE libc already exports exactly this pair.

Tests. test_elf_duplicates.py covers the three real collisions, a same-address alias case that must patch, and a different-address case that must be rejected.

Patcher Result
main 4 tests fail
previous commit 1 test fails (the same-address case)
new commit 7d9a30804 all 4 pass

Linux (WSL Ubuntu 24.04, GCC 13.3), rebased on 44df21632:

  • The full tree builds, 2505/2505 steps, with nid_patcher run on all 126 libraries.
  • ctest -E "agc_|video_out": 647/648 pass. The exception, guest_named_shm_concurrency, fails only because WSL runs as root; the same binary passes as a normal user.
  • The only same-name groups left in the patched libraries are the six one-address aliases in libc.

Windows: 424/424 related tests pass.

I've rewritten the description to match.

AI-assisted: yes (Claude Code); results above were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6200509073 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5849#issuecomment-6200509073). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-15T19:27:29Z. --- Thanks, you're right. I had only built the libraries on Windows, and the description's claim was wrong for the Linux libc. Small correction to the symbol list: `__cxa_throw` and `__cxa_throw_nid_no_patch_cut` both become `__cxa_throw`, while `__cxa_throw_nid_postfix` becomes its NID (`vkuuLfhnSZI`). I chose the same-address rule. The check now keeps `st_value` and `st_shndx` for each patched name and throws only when the name comes from a different address. Aliases of one address resolve to the same code, and removing them would change libc's export list, which the other libraries read through `--preserve-export-list`. That alone wasn't enough, though. The Linux libc also exported `__gxx_personality_v0` twice at different addresses, and the patched libc on `main` has both today: - the forwarding function in `Personality.cpp:105`; - the `__gxx_personality_v0_nid_no_patch_cut` alias of the guest personality (`Exports.cpp:21`). So I also removed that one alias. It is Linux-only and unreferenced, the forwarding function passes its arguments straight through, `Unwind.cpp:106` accepts either pointer, and the PE libc already exports exactly this pair. **Tests.** `test_elf_duplicates.py` covers the three real collisions, a same-address alias case that must patch, and a different-address case that must be rejected. | Patcher | Result | |---|---| | `main` | 4 tests fail | | previous commit | 1 test fails (the same-address case) | | new commit `7d9a30804` | all 4 pass | **Linux** (WSL Ubuntu 24.04, GCC 13.3), rebased on `44df21632`: - The full tree builds, 2505/2505 steps, with `nid_patcher` run on all 126 libraries. - `ctest -E "agc_|video_out"`: 647/648 pass. The exception, `guest_named_shm_concurrency`, fails only because WSL runs as root; the same binary passes as a normal user. - The only same-name groups left in the patched libraries are the six one-address aliases in libc. **Windows:** 424/424 related tests pass. I've rewritten the description to match. AI-assisted: yes (Claude Code); results above were run on this machine.
adriadam10 commented 2026-10-16 18:30:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-16T18:30:13Z.

Review: APPROVED


Re-checked the new head: the same-address rule fixes the libc failure (Patching NIDs for libc no longer trips on the __cxa_* alias groups), and dropping __gxx_personality_v0_nid_no_patch_cut removes the one real collision. Core/libs build and ctest pass on Linux (incl. nid_elf_duplicates); the test fails without the patcher change. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5552098452 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5849#pullrequestreview-5552098452). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-16T18:30:13Z. **Review:** APPROVED --- Re-checked the new head: the same-address rule fixes the libc failure (`Patching NIDs for libc` no longer trips on the `__cxa_*` alias groups), and dropping `__gxx_personality_v0_nid_no_patch_cut` removes the one real collision. Core/libs build and ctest pass on Linux (incl. `nid_elf_duplicates`); the test fails without the patcher change. Merging once the full Build is green.
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!5849
No description provided.