Skip to content

fix(relinker): resolve symbol-name imports through NIDs on Linux - #9234

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
mossyanwar:fix/relinker-linux-named-imports
Oct 22, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
mossyanwar:fix/relinker-linux-named-imports

Conversation

@mossyanwar

@mossyanwar mossyanwar commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

What

Homebrew built with the ps5-payload-dev SDK needs libkernel_web.sprx, libSceLibcInternal.sprx and so on, and imports by symbol name (memset, accept, __error). The libraries in libs/ are .prx files that export NIDs, so on Linux the converted executable stopped at the first .sprx dependency, and after that at the first symbol name (#9180). #6697 fixed the same thing on Windows.

Linux output now:

  • loads a .sprx, .suprx or .debug_prx dependency as the .prx library of the same stem (Domain::NeededLibraryName, as on Windows);
  • in an executable that names such a dependency, renames an import without a module that is named by symbol to the NID of that name. SysVDynamicSectionBuilder records these imports by relocation address in SysVDynamicSection::NamedImports, and LinuxElfPatcher renames them after the guest module builder has run, so a name that a bundled module exports still binds to that module (shared#guest). Qualified imports and imports with a module are unchanged.

Other executables keep their unqualified imports as written. Test fixtures import helpers such as LoadExecFirst_nid_no_patch by name from host libraries that export them by name.

Unlike Windows, there is no first lookup by the name as written: ld.so searches the global scope, which includes the host glibc and libstdc++ that every library in libs/ links, so malloc or pthread_create would bind to glibc. The SDK's own loader resolves these names by NID on the console (sprx_sym2addr). No DT_NEEDED is added for libc.prx or libkernel.prx, because every Linux library in libs/ already links both.

The call registry and needed.json are unchanged. The Windows debt entry now only mentions macOS, and a new Linux entry records what is still open: imports of bundled modules are still bound by name.

guest_needed_modules, guest_module_identity and guest_needed_nested_modules expected a .suprx or .debug_prx host dependency to be kept as written. It is now the .prx name.

Tested

WSL2 Ubuntu 24.04, GCC 13.3, Ninja Release, on main 99844c7ee.

  • New linux_named_imports test. A host library built at test time exports 9E8ipSX3c9g, the NID of shared. An executable that imports shared without a module:

    • exits with the provider's value through a DT_NEEDED of available.sprx, which now loads available.prx
    • keeps shared and binds to a provider exporting shared when it only needs available.prx
    • fails with undefined symbol: 9E8ipSX3c9g against a provider that only exports shared as written, when it needs available.sprx
    • still imports shared when the import is shared#A#B
    • still imports shared#guest when a bundled module exports shared

    On main it fails at the first case: the output still imports shared, and running it gives undefined symbol: shared.

  • Relinker-only build: ctest 239/239.

  • Full build with BUILD_TESTING=ON: loadexec_arguments, agc_driver_guest_memory_lifetime_converted, linux_named_imports, windows_named_imports, executable_tls_relocations, guest_needed_modules, guest_module_identity and guest_needed_nested_modules pass. With the relinker from the previous head 28b31a306, the first two fail with undefined symbol: dt+StXeB4JQ and undefined symbol: vIMLEVWjQbg, as @wail-arch reported. The rest of the full suite was not run.

  • python3 tools/check_conventions.py --base origin/main passes.

  • ftpsrv.elf from the pacbrew-repo v0.41.1 ps5-payload-daemons.zip (sha256 ab59eb744eccd197d68af4d4a0b882940861e3c45010e766c83704188ff753a5), relinked with --skip-syscall-check --skip-sce-module --registry. --skip-syscall-check is needed because its inline syscalls don't convert yet (relinker: the SDK SYS_mount stub is not trapped, so two public ps5-payload-dev payloads do not convert #8624).

    • main: needs libkernel_web.sprx, libSceLibcInternal.sprx and libSceNet.sprx, and 84 of its 87 imports are plain names.
    • This PR: needs the .prx names, and all 87 imports are NIDs (__error is 9BcDykPmo1I, atoi is fPxypibz2MY). The call registry is identical to main's.

@rocklouis055 and @simonbbby ran the previous head against full libs builds: SDK hello_world and LÖVE load with every dependency a .prx name and every import a NID, then stop in the SDK's _start, which expects a payload_args_t* in rdi. That entry is outside this PR.

Not tested: running on Windows or macOS. Those code paths are unchanged.

#9261, opened after this one, makes the same change and also covers imports of bundled modules.

Checklist

@simonbbby

Copy link
Copy Markdown
Contributor

Native Linux runtime check of exact head 28b31a306adb91e4e4a11c33b2736d44f3bad14e, following the request in #9180. The two import failures are fixed against real AnyPS5 libraries, but the unmodified SDK payload still stops in its bootstrap before main.

Ubuntu 24.04.3, GCC 13.3, Release. I built the official ps5-payload-dev/sdk v0.44 hello_world source using the v0.44 SDK release and clang/lld 18. This is a newly built ELF, not the reporter's binary (SHA256 59ed5e733f97631d1bc94422ea07432fcd79046bc5076bf886bd72888401dfef). Its three undefined imports are memset, strncpy, and sceKernelSendNotificationRequest.

I built the relinker separately at this head and at parent cecb540a63f614d2b06cbb557c6d1f6f02475fa3, and rebuilt the full libs target at the PR head, including NID patching. Both converted the same input with:

relinker --to-intel --skip-sce-module --trap-sdk-syscall-stubs --registry hello_world.elf app.elf

Each output used the same freshly built libs directory.

Relinker Actual runtime result
Parent Exit 127: missing libkernel_web.sprx. Providing isolated .sprx aliases then gives exit 127: undefined sceKernelSendNotificationRequest.
PR Needs the three .prx names with no aliases. LD_DEBUG=bindings confirms guest 6sJWiWSRuqk and 8zTFvBIAIN8 resolve to libc.prx, and zl7hupSO0C0 to libkernel.prx. Startup then exits 139 at address 1.

The remaining crash is in the SDK's __crt_syscall_init, at call *(%rbx). The SDK treats its argument as payload_args_t*, whose first member is sys_dynlib_dlsym. AnyPS5's entry stub passes the initial process stack in RDI; GDB shows RBX pointing there, with first qword 1 (argc), which becomes the call target. This occurs before the sample's main or notification call. Fresh libraries reproduce it, so the initial older-library result was not the cause.

As a separate import-path diagnostic, I copied the input and changed only ELF e_entry from _start (0x64) to main (0). After conversion by this PR, it executes the sample body and prints [notification] Hello, world!; GDB shows EAX=0 on return, followed by SIGILL at the generated entry stub's ud2. This bypasses the SDK CRT and is not a successful unmodified-payload run. It does exercise the sample's three imports against the real runtime libraries.

The PR's relinker-only CTest suite also passes 239/239. No repository source edits. This verifies the filename/NID fix and identifies a separate payload-entry ABI blocker; it does not establish that #9180's full SDK payload now runs.

@github-actions

github-actions Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

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 7822 docs/dev/debt/relinker.md L16-17

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/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 6044 core/relinker/CMakeLists.txt
PR 6879 core/relinker/CMakeLists.txt
core/relinker/relinker/src/output/SysVDynamicSectionBuilder.cpp
PR 7918 docs/dev/debt/relinker.md
PR 8076 docs/dev/debt/relinker.md
PR 9408 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md

@wail-arch

Copy link
Copy Markdown
Contributor

Tried this branch with a full build (not relinker-only) on cecb540a6, Ubuntu 24.04, GCC 13.3, as asked in #9180. Two tests that pass or skip on main fail with this relinker:

  • loadexec_arguments: undefined symbol: dt+StXeB4JQ (the NID of LoadExecFirst_nid_no_patch)
  • agc_driver_guest_memory_lifetime_converted: undefined symbol: vIMLEVWjQbg

Both fixtures import unqualified names that a host library exports by name, and both are registered only in the full build, so the relinker-only run stays green.

The executable side works on a real SDK binary: a probe built with the ps5-payload-dev SDK (own _start, printf/getpid/strncpy by name) runs, with the calls bound in libs/libc.prx and libs/libkernel.prx.

I have a branch that only rewrites images naming a .sprx/.suprx/.debug_prx dependency, and also rewrites the named imports of bundled guest modules: https://github.com/wail-arch/AnyPS5/tree/fix/linux-sdk-homebrew-imports. Full ctest gives 1795/1802 (the 7 failures are IPv6/sandbox/root issues in my container). I'm opening it as an alternative so the maintainers can compare; happy to close it if you take these fixes into yours.

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 21, 2026
@rocklouis055

Copy link
Copy Markdown
Contributor

Tried the branch (28b31a306) against a full libs build of main 7280a17ec: Linux x86-64, Ubuntu 26.04, GCC 15, Release, Intel Core Ultra 9 275HX, so --to-intel. The relinker was built from this branch (relinker-only build), the libraries from main.

Command, with no .sprx aliases in libs/:

relinker --to-intel --skip-sce-module --trap-sdk-syscall-stubs src/eboot.elf app.elf
Payload DT_NEEDED Imports Load Run
SDK samples/hello_world (v0.44, sha256 6fef8533…5d5e) 3, all .prx 3, all NIDs (8zTFvBIAIN8, 6sJWiWSRuqk, zl7hupSO0C0) loads SIGSEGV, exit 139
LÖVE (pacbrew-repo v0.41.1, sha256 10ccd3ed…693f) 10, all .prx 338, all NIDs loads past __stderrp SIGSEGV, exit 139

So both failures from #9180 are gone on Linux.

The crash is outside this PR. Both jump to 0x1 from the SDK's startup code (hello_world +0x463, in __crt_syscall_init). The SDK's _start (crt/crt.c, crt/payload.h) takes a payload_args_t* in rdi from the ELF loader and calls args->sys_dynlib_dlsym(...) first; run as a process, rdi is 1. crt/syscall.c then takes the syscall address as getpid + 0xa inside libkernel. Running these payloads needs an entry that supplies payload_args_t, which belongs in a separate issue.

Tested with AI assistance (Claude Code); the commands and outputs above were run locally.

Homebrew built with the ps5-payload-dev SDK names its system
libraries with .sprx and imports by symbol name, while the libraries
in libs/ are .prx files that export NIDs. On Linux such an executable
converted but stopped at load time on the first .sprx dependency and,
once that was found, on the first symbol name.

The Linux patcher now loads a dependency named with .sprx, .suprx or
.debug_prx as the .prx library of the same stem. In an executable that
names such a dependency, an import without a module that is named by
symbol gets the NID of that name. A name that a bundled module exports
still binds to that module. There is no lookup by the name as written,
because the host loader also searches the host C library. Other
executables keep their unqualified imports as written.
@mossyanwar
mossyanwar force-pushed the fix/relinker-linux-named-imports branch from 28b31a3 to be51ab7 Compare October 21, 2026 17:50
@mossyanwar

Copy link
Copy Markdown
Contributor Author

@wail-arch thanks, you're right. Rewriting every unqualified import broke fixtures that import *_nid_no_patch helpers by name. I've force-pushed a fix: unqualified imports are now only rewritten in an executable that names a .sprx, .suprx or .debug_prx dependency, the same condition your branch uses, and every other executable converts as on main. With a full build, loadexec_arguments and agc_driver_guest_memory_lifetime_converted fail with the previous head (undefined symbol: dt+StXeB4JQ, vIMLEVWjQbg) and pass now. Your branch also rewrites imports of bundled modules. I've kept that out of this one and recorded it in the debt entry.

@rocklouis055 @simonbbby thanks for running it against full libs builds. The payload_args_t entry is separate from this PR, so a new issue for it makes sense.

@wail-arch

Copy link
Copy Markdown
Contributor

@mossyanwar Thanks. On be51ab7, loadexec_arguments passes and agc_driver_guest_memory_lifetime_converted skips as on main (full build, Ubuntu 24.04), and the relinker-only suite passes.

I turned #9261 into the bundled-module follow-up, stacked on be51ab7 with Depends on: #9234: it applies the same rule to the modules an SDK homebrew ships next to its executable (their named imports and .sprx host dependencies), so it only needs review once this one is merged.

@rocklouis055

Copy link
Copy Markdown
Contributor

Re-ran on be51ab7 with the same setup as my earlier comment (full libs of main 7280a17ec, Linux, --to-intel):

  • SDK hello_world and LÖVE: as on 28b31a306. No .sprx dependency and no plain-name import is left (3 and 338 imports, all NIDs), both load, and both stop at the SDK startup's payload_args_t call, outside this PR.
  • An executable without a .sprx dependency (the gpu_cube example of ps5link-sdk, NID imports): nm -D --undefined-only output identical to main's, and it runs to its end, exit 0.

I'll open a separate issue for the payload_args_t entry.

@adriadam10 adriadam10 self-assigned this Oct 22, 2026
@adriadam10
adriadam10 merged commit 3a2ca40 into boykopovar:main Oct 22, 2026
11 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the .sprx/.suprx/.debug_prx → .prx mapping, the NID rename of unqualified imports (only in executables that name such a dependency, qualified, module and bundled-module imports untouched), linux_named_imports (fails on main: the output still imports shared), the three updated needed-library tests and the debt entries. Confirmed on a full build by @wail-arch, and on SDK hello_world/LÖVE by @rocklouis055 and @simonbbby; merged.

@mossyanwar
mossyanwar deleted the fix/relinker-linux-named-imports branch October 23, 2026 00:08
hugodearaujo added a commit to hugodearaujo/AnyPS5 that referenced this pull request Oct 24, 2026
…0x400000 base

Linux executables now load at 0x400000 by default (boykopovar#9234), so the stub's call reaches the guest entry moved by that base.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build docs fix error Closes an issue (Fixes #N) relinker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

relinker: ps5-payload-dev SDK ELFs convert but do not load: .sprx DT_NEEDED names and plain-name imports are kept

5 participants