From bd750b59544f5867f25ebbad7317ce6214f788e2 Mon Sep 17 00:00:00 2001 From: abderahmane-ai Date: Thu, 8 Oct 2026 08:59:33 +0100 Subject: [PATCH] feat(relinker): write a registry for each converted bundled module --registry only described the imports of the executable, so a title whose own modules import system functions (PSN.prx, libc.prx and others) could not be audited completely. With --registry the relinker now also writes ..registry.json for every converted guest module. An entry is written for each relocation against an undefined symbol that no other bundled module exports, the same test the module builder uses to decide that a dependency is satisfied by a sibling. Entries carry nid, library and targetOffset; the symbol name no longer has the # suffix by the time the module is read, so the NID is the bare 11 characters. The executable's registry is unchanged. The test converts two modules where one is provided by its sibling, for Linux and Windows output, covers the DT_RELA and DT_JMPREL tables, checks that the same module imports the sibling's function once the sibling is absent, and that nothing is written without --registry. Registered in CTest as guest_registry. --- core/relinker/CMakeLists.txt | 2 + core/relinker/main.cpp | 7 +- .../relinker/domain/ICallRegistryWriter.hpp | 1 + .../include/relinker/guest/GuestImage.hpp | 1 + .../relinker/output/CallRegistryWriter.hpp | 1 + .../relinker/src/guest/GuestModuleBuilder.cpp | 16 +++- .../src/output/CallRegistryWriter.cpp | 19 +++++ .../relinker/tests/test_guest_registry.py | 77 +++++++++++++++++++ docs/user/USAGE.md | 8 +- 9 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 core/relinker/relinker/tests/test_guest_registry.py diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index f1d7705e2..875a463f4 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -235,6 +235,8 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_module_directories.py $) add_test(NAME guest_symbol_names COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_symbol_names.py $) + add_test(NAME guest_registry COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_registry.py $) add_test(NAME guest_host_libc COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_host_libc.py $) add_test(NAME guest_absolute_symbols COMMAND ${Python3_EXECUTABLE} diff --git a/core/relinker/main.cpp b/core/relinker/main.cpp index 1b4db2e0d..2134eef0d 100644 --- a/core/relinker/main.cpp +++ b/core/relinker/main.cpp @@ -97,7 +97,12 @@ int main(const int argc, char* argv[]) { if (args.writeRegistry) { const std::filesystem::path outFsPath(absPath); const std::string registryPath = (outFsPath.parent_path() / (outFsPath.stem().string() + ".registry.json")).string(); - fileWriter.Write(registryPath, std::make_shared()->WriteCallRegistry(result.RegistryEntries)); + const auto registryWriter = std::make_shared(); + fileWriter.Write(registryPath, registryWriter->WriteCallRegistry(result.RegistryEntries)); + for (const auto& artifact : guestArtifacts) { + const auto modulePath = outFsPath.parent_path() / (outFsPath.stem().string() + "." + artifact.Path.filename().string() + ".registry.json"); + fileWriter.Write(modulePath.string(), registryWriter->WriteModuleImports(artifact.Imports)); + } } auto byteWriter = std::make_shared(); diff --git a/core/relinker/relinker/include/relinker/domain/ICallRegistryWriter.hpp b/core/relinker/relinker/include/relinker/domain/ICallRegistryWriter.hpp index cc88e831c..d528667e9 100644 --- a/core/relinker/relinker/include/relinker/domain/ICallRegistryWriter.hpp +++ b/core/relinker/relinker/include/relinker/domain/ICallRegistryWriter.hpp @@ -12,6 +12,7 @@ public: virtual ~ICallRegistryWriter() = default; virtual std::string WriteCallRegistry(const std::vector& entries) = 0; + virtual std::string WriteModuleImports(const std::vector& entries) = 0; }; } diff --git a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp index fe34092b5..85f8b2ae7 100644 --- a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp +++ b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp @@ -50,6 +50,7 @@ public: struct GuestArtifact { std::filesystem::path Path; std::vector Bytes; + std::vector Imports; }; class GuestModuleBuilder { diff --git a/core/relinker/relinker/include/relinker/output/CallRegistryWriter.hpp b/core/relinker/relinker/include/relinker/output/CallRegistryWriter.hpp index f6b1c64e7..79b196f86 100644 --- a/core/relinker/relinker/include/relinker/output/CallRegistryWriter.hpp +++ b/core/relinker/relinker/include/relinker/output/CallRegistryWriter.hpp @@ -8,6 +8,7 @@ namespace Relinker { class CallRegistryWriter : public ICallRegistryWriter { public: std::string WriteCallRegistry(const std::vector& entries) override; + std::string WriteModuleImports(const std::vector& entries) override; }; } diff --git a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp index b2cd06d9a..051c506f4 100644 --- a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp +++ b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp @@ -171,6 +171,7 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path if (!windows && exports.contains(name)) rename(dynamic.DynSymData, dynamic.DynStrData, offset / 24, name); } std::vector> dependencies(images.size()); + std::vector> systemImports(images.size()); for (auto& image : images) { image.UsePlatformTlsResolver = !windows ? !exports.contains("vNe1w4diLCs") : std::none_of(image.Symbols.begin(), image.Symbols.end(), [](const auto& symbol) { return symbol.Name == "vNe1w4diLCs" && symbol.Section != 0 && symbol.Section != AbsoluteSection && (symbol.Info >> 4) != 0 && symbol.Visibility != 1 && symbol.Visibility != 2; @@ -181,7 +182,8 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path const auto found = findGuest(name); if (found != guestNames.end() && found->second != index) dependencies[index].insert(found->second); } - for (const auto& symbol : images[index].Symbols) { + for (std::size_t symbolIndex = 0; symbolIndex < images[index].Symbols.size(); ++symbolIndex) { + const auto& symbol = images[index].Symbols[symbolIndex]; if (symbol.Section != 0 || symbol.Name.empty()) continue; rejectSharedImport(symbol.Name, images[index].SourcePath.string()); const auto found = exports.find(symbol.Name); @@ -199,6 +201,7 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path if (exported == provider.Symbols.end() || ((symbol.Info & 15) != 0 && (symbol.Info & 15) != (exported->Info & 15))) throw Domain::RelinkerException("Guest import/export type mismatch: " + symbol.Name); if (providers.front() != index) dependencies[index].insert(providers.front()); } else if (windows && (symbol.Info & 15) == 6) throw Domain::RelinkerException("Windows guest TLS import requires a guest TLS export: " + symbol.Name); + if (providers.empty()) systemImports[index].insert(symbolIndex); } } if (!windows) { @@ -274,8 +277,17 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path if (provider == index && std::find(runtime.Names.begin(), runtime.Names.end(), name) == runtime.Names.end()) runtime.Names.push_back(name); } } + std::vector imports; + for (const auto* table : {&image.Dynamic.RelaData, &image.Dynamic.RelaPltData}) { + for (std::size_t position = 0; position < table->size(); position += 24) { + const auto symbolIndex = static_cast(Io::ReadU64(*table, position + 8) >> 32); + if (!systemImports[index].contains(symbolIndex)) continue; + const auto& symbol = image.Symbols[symbolIndex]; + imports.push_back({symbol.Name, symbol.Library, {}, 0, {}, Io::ReadU64(*table, position), {}, false}); + } + } dynamic.GuestModules.push_back(std::move(runtime)); - artifacts.push_back({target, std::move(output)}); + artifacts.push_back({target, std::move(output), std::move(imports)}); } return artifacts; } diff --git a/core/relinker/relinker/src/output/CallRegistryWriter.cpp b/core/relinker/relinker/src/output/CallRegistryWriter.cpp index 39ca268aa..e598056d6 100644 --- a/core/relinker/relinker/src/output/CallRegistryWriter.cpp +++ b/core/relinker/relinker/src/output/CallRegistryWriter.cpp @@ -62,4 +62,23 @@ std::string CallRegistryWriter::WriteCallRegistry(const std::vector& entries) { + std::ostringstream out; + out << "[\n"; + + for (std::size_t i = 0; i < entries.size(); ++i) { + const auto& e = entries[i]; + out << " {\n"; + out << " \"nid\": " << _jsonString(e.Nid) << ",\n"; + out << " \"library\": " << _jsonString(e.Library) << ",\n"; + out << " \"targetOffset\": " << _hexOffset(e.TargetOffset) << "\n"; + out << " }"; + if (i + 1 < entries.size()) out << ","; + out << "\n"; + } + + out << "]\n"; + return out.str(); +} + } diff --git a/core/relinker/relinker/tests/test_guest_registry.py b/core/relinker/relinker/tests/test_guest_registry.py new file mode 100644 index 000000000..4699f65f7 --- /dev/null +++ b/core/relinker/relinker/tests/test_guest_registry.py @@ -0,0 +1,77 @@ +import json +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from test_guest_intel_trampolines import main_fixture +from test_guest_symbol_names import guest_module, importing_executable + +RELA = 0x900 +RELASZ = 0x668 +PLT = 0x940 +GLOB_DAT = 6 +JUMP_SLOT = 7 + + +def libc_module(): + image = guest_module([("AAAAAAAAAAA#A#A", True), ("BBBBBBBBBBB#B#B", False), ("CCCCCCCCCCC#C#C", False), ("EEEEEEEEEEE", True)]) + relocations = [(0x2380, 2, GLOB_DAT), (0x2388, 3, GLOB_DAT)] + for index, (target, symbol, kind) in enumerate(relocations): + struct.pack_into(".registry.json` beside the output executable. | +| `--registry` | Write `.registry.json` beside the output executable, and `..registry.json` for each converted bundled module. | | `--rpath ` | Set the system library search path. Quote `$ORIGIN` to prevent shell expansion, for example `--rpath '$ORIGIN/libs'` in Bash or PowerShell. Linux guest modules require an absolute path or a path beginning with `$ORIGIN`. Windows requires a nonempty ASCII path and supports `$ORIGIN` as the executable directory. | | `--autorun` | Run the output after conversion, print its exit code, and wait for Enter. Adds executable permissions for Linux output. Requires the target OS and prepared runtime layout. | | `--skip-sce-module` | Deprecated. Skip all bundled module processing. | @@ -105,10 +105,10 @@ The game runs on the first Vulkan 1.1 device with graphics and compute queues an ```sh relinker --registry source/input.elf app.elf -python3 tools/import_audit.py app.registry.json --libs build/core/libs/libs --modules source/sce_module +python3 tools/import_audit.py app.registry.json app.libc.prx.guest.prx.registry.json --libs build/core/libs/libs --modules source/sce_module ``` -`--registry` writes `app.registry.json` beside the output. `--libs` is the directory the `libs` target fills ([build instructions](../dev/BUILD.md)); the exports are read from the built `.prx` files, so the result matches what the loader finds. Imports are counted once per NID and library, and every reference lands in exactly one class: +`--registry` writes `app.registry.json` for the executable and one `app..registry.json` for each converted bundled module; pass all of them. `--libs` is the directory the `libs` target fills ([build instructions](../dev/BUILD.md)); the exports are read from the built `.prx` files, so the result matches what the loader finds. Imports are counted once per NID and library, and every reference lands in exactly one class: | Class | Meaning | |-------|---------| @@ -129,4 +129,4 @@ The report also lists needed libraries that have no file in `--libs` or `--modul Exit codes: `0` nothing blocks loading, `1` there are `absent` imports or needed libraries without a file, `2` an input could not be read; the message names the file and the value. -The registry lists the imports of the executable, not of its bundled modules. +A module registry lists `nid`, `library` and `targetOffset` for each relocation against a function that no other bundled module exports, without the `#` suffix of the executable's NIDs.