mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
feat(kernel): add tracked dynamic module loading and NID lookup
This commit is contained in:
@@ -104,6 +104,24 @@ endforeach()
|
||||
add_custom_target(libs DEPENDS ${PATCHED_OUTPUTS})
|
||||
|
||||
if(BUILD_TESTING)
|
||||
add_library(guest_module_fixture SHARED tests/GuestModuleFixture.cpp)
|
||||
target_include_directories(guest_module_fixture PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
add_custom_command(TARGET guest_module_fixture POST_BUILD
|
||||
COMMAND $<TARGET_FILE:nid_patcher> guest_module_fixture $<TARGET_FILE:guest_module_fixture>
|
||||
VERBATIM)
|
||||
add_dependencies(guest_module_fixture nid_patcher)
|
||||
add_executable(guest_dynamic_loader_tests tests/GuestDynamicLoader.cpp)
|
||||
target_include_directories(guest_dynamic_loader_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_dynamic_loader_tests PRIVATE libkernel libc)
|
||||
configure_windows_unwind(guest_dynamic_loader_tests)
|
||||
add_dependencies(guest_dynamic_loader_tests guest_module_fixture)
|
||||
add_test(NAME guest_dynamic_loader COMMAND guest_dynamic_loader_tests $<TARGET_FILE_NAME:guest_module_fixture>)
|
||||
set_tests_properties(guest_dynamic_loader PROPERTIES
|
||||
WORKING_DIRECTORY "$<TARGET_FILE_DIR:guest_module_fixture>")
|
||||
if(WIN32)
|
||||
set_tests_properties(guest_dynamic_loader PROPERTIES ENVIRONMENT_MODIFICATION
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
|
||||
endif()
|
||||
add_executable(guest_filesystem_tests tests/GuestFilesystem.cpp)
|
||||
target_include_directories(guest_filesystem_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
target_link_libraries(guest_filesystem_tests PRIVATE libc)
|
||||
|
||||
@@ -29,6 +29,9 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Socket/src/AddressConversion.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/EventFlag/src/EventFlag.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Module/src/Module.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Module/src/DynamicLoader.cpp
|
||||
${LIBS_INCLUDE_DIR}/nid/src/NidCompute.cpp
|
||||
${LIBS_INCLUDE_DIR}/nid/src/Sha1.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Aio/src/Aio.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/System/src/Coredump.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/System/src/Exception.cpp
|
||||
@@ -43,6 +46,8 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL
|
||||
)
|
||||
|
||||
target_include_directories(libkernel PRIVATE ${LIBS_INCLUDE_DIR})
|
||||
target_include_directories(libkernel PRIVATE ${LIBS_INCLUDE_DIR}/nid/include)
|
||||
target_link_libraries(libkernel PRIVATE ${CMAKE_DL_LIBS})
|
||||
target_link_libraries(libkernel PRIVATE libc)
|
||||
if(WIN32)
|
||||
target_link_libraries(libkernel PRIVATE ws2_32)
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include <nid/NidCompute.hpp>
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <vector>
|
||||
#ifdef _WIN32
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
thread_local std::array<char, 512> loaderError{};
|
||||
thread_local bool pendingError = false;
|
||||
void Error(const char* message) {
|
||||
std::snprintf(loaderError.data(), loaderError.size(), "%s", message);
|
||||
pendingError = true;
|
||||
}
|
||||
struct Module {
|
||||
void* native = nullptr;
|
||||
bool owned = true;
|
||||
bool global = false;
|
||||
~Module() {
|
||||
if (owned && native) {
|
||||
#ifdef _WIN32
|
||||
FreeLibrary(static_cast<HMODULE>(native));
|
||||
#else
|
||||
::dlclose(native);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
};
|
||||
std::mutex modulesMutex;
|
||||
std::map<std::uintptr_t, std::shared_ptr<Module>> modules;
|
||||
std::uintptr_t nextHandle = 0x20000000;
|
||||
void* Symbol(Module& module, const char* name) {
|
||||
#ifdef _WIN32
|
||||
return reinterpret_cast<void*>(GetProcAddress(static_cast<HMODULE>(module.native), name));
|
||||
#else
|
||||
return ::dlsym(module.native, name);
|
||||
#endif
|
||||
}
|
||||
void* FindSymbol(Module& module, const char* name) {
|
||||
if (auto* symbol = Symbol(module, name)) return symbol;
|
||||
const auto nid = Nid::ComputeNid(name, "");
|
||||
return Symbol(module, nid.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
char* APS5_VABI dlerror_nid_postfix() {
|
||||
if (!pendingError) return nullptr;
|
||||
pendingError = false;
|
||||
return loaderError.data();
|
||||
}
|
||||
void* APS5_VABI dlopen_nid_postfix(const char* path, int flags) {
|
||||
if ((flags & ~0x103) || (flags & 3) == 0 || (flags & 3) == 3) {
|
||||
Error("dlopen: unsupported flags"); return nullptr;
|
||||
}
|
||||
try {
|
||||
auto module = std::make_shared<Module>();
|
||||
module->global = (flags & 0x100) != 0 || !path;
|
||||
#ifdef _WIN32
|
||||
if (!path) {
|
||||
module->native = GetModuleHandleW(nullptr);
|
||||
module->owned = false;
|
||||
} else {
|
||||
if (!*path) { Error("dlopen: empty module path"); return nullptr; }
|
||||
const auto resolved = ResolvePath_nid_no_patch(path);
|
||||
module->native = LoadLibraryExW(resolved.c_str(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH);
|
||||
}
|
||||
if (!module->native) {
|
||||
char message[128];
|
||||
std::snprintf(message, sizeof(message), "dlopen: Windows loader error %lu (module must be host-compatible)", GetLastError());
|
||||
Error(message); return nullptr;
|
||||
}
|
||||
#else
|
||||
const auto resolved = path ? ResolvePath_nid_no_patch(path).string() : std::string{};
|
||||
const int nativeFlags = ((flags & 3) == 1 ? RTLD_LAZY : RTLD_NOW) |
|
||||
((flags & 0x100) ? RTLD_GLOBAL : RTLD_LOCAL);
|
||||
module->native = ::dlopen(path ? resolved.c_str() : nullptr, nativeFlags);
|
||||
if (!module->native) { Error(::dlerror()); return nullptr; }
|
||||
#endif
|
||||
std::lock_guard lock(modulesMutex);
|
||||
const auto handle = nextHandle++;
|
||||
modules.emplace(handle, std::move(module));
|
||||
return reinterpret_cast<void*>(handle);
|
||||
} catch (const std::exception& error) { Error(error.what()); return nullptr; }
|
||||
}
|
||||
void* APS5_VABI dlsym_nid_postfix(void* handle, const char* name) {
|
||||
if (!name || !*name) { Error("dlsym: empty symbol name"); return nullptr; }
|
||||
try {
|
||||
std::vector<std::shared_ptr<Module>> search;
|
||||
{
|
||||
std::lock_guard lock(modulesMutex);
|
||||
if (handle == reinterpret_cast<void*>(static_cast<std::intptr_t>(-2))) {
|
||||
for (const auto& [key, module] : modules) if (module->global) search.push_back(module);
|
||||
} else {
|
||||
auto found = modules.find(reinterpret_cast<std::uintptr_t>(handle));
|
||||
if (found == modules.end()) { Error("dlsym: invalid or unsupported module handle"); return nullptr; }
|
||||
search.push_back(found->second);
|
||||
}
|
||||
}
|
||||
for (const auto& module : search) if (auto* result = FindSymbol(*module, name)) return result;
|
||||
Error("dlsym: symbol not found in supported module scope");
|
||||
return nullptr;
|
||||
} catch (const std::exception& error) { Error(error.what()); return nullptr; }
|
||||
}
|
||||
int APS5_VABI dlclose_nid_postfix(void* handle) {
|
||||
std::shared_ptr<Module> module;
|
||||
{
|
||||
std::lock_guard lock(modulesMutex);
|
||||
auto found = modules.find(reinterpret_cast<std::uintptr_t>(handle));
|
||||
if (found == modules.end()) { Error("dlclose: invalid module handle"); return -1; }
|
||||
module = std::move(found->second);
|
||||
modules.erase(found);
|
||||
}
|
||||
// Unload outside the registry lock: module destructors may call loader APIs.
|
||||
module.reset();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#include <cstdlib>
|
||||
#include <thread>
|
||||
extern "C" {
|
||||
void* APS5_VABI dlopen_nid_postfix(const char*, int);
|
||||
void* APS5_VABI dlsym_nid_postfix(void*, const char*);
|
||||
int APS5_VABI dlclose_nid_postfix(void*);
|
||||
char* APS5_VABI dlerror_nid_postfix();
|
||||
}
|
||||
static void Require(bool value) { if (!value) std::abort(); }
|
||||
int main(int argc, char** argv) {
|
||||
Require(argc == 2);
|
||||
Require(dlerror_nid_postfix() == nullptr);
|
||||
Require(dlopen_nid_postfix(argv[1], 0x2000) == nullptr);
|
||||
Require(dlerror_nid_postfix() != nullptr);
|
||||
Require(dlerror_nid_postfix() == nullptr);
|
||||
Require(dlopen_nid_postfix("anyps5-missing-module-for-test.prx", 2) == nullptr);
|
||||
Require(dlerror_nid_postfix() != nullptr);
|
||||
void* executable = dlopen_nid_postfix(nullptr, 2);
|
||||
Require(executable != nullptr && dlclose_nid_postfix(executable) == 0);
|
||||
void* module = dlopen_nid_postfix(argv[1], 2 | 0x100);
|
||||
Require(module != nullptr);
|
||||
using Add = int (APS5_VABI *)(int, int);
|
||||
auto add = reinterpret_cast<Add>(dlsym_nid_postfix(module, "GuestModuleAdd"));
|
||||
Require(add && add(17, 25) == 42);
|
||||
Require(dlsym_nid_postfix(reinterpret_cast<void*>(-2), "GuestModuleAdd") == reinterpret_cast<void*>(add));
|
||||
Require(dlsym_nid_postfix(module, "missing_symbol") == nullptr);
|
||||
std::thread other([] { Require(dlerror_nid_postfix() == nullptr); });
|
||||
other.join();
|
||||
Require(dlerror_nid_postfix() != nullptr);
|
||||
Require(dlerror_nid_postfix() == nullptr);
|
||||
void* second = dlopen_nid_postfix(argv[1], 1);
|
||||
Require(second && second != module);
|
||||
Require(dlclose_nid_postfix(module) == 0);
|
||||
Require(dlsym_nid_postfix(module, "GuestModuleAdd") == nullptr);
|
||||
add = reinterpret_cast<Add>(dlsym_nid_postfix(second, "GuestModuleAdd"));
|
||||
Require(add && add(2, 3) == 5);
|
||||
Require(dlclose_nid_postfix(second) == 0);
|
||||
Require(dlclose_nid_postfix(second) == -1);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "prx/libc/include/general/VabiMacros.hpp"
|
||||
#ifdef _WIN32
|
||||
#define MODULE_EXPORT __declspec(dllexport)
|
||||
#else
|
||||
#define MODULE_EXPORT __attribute__((visibility("default")))
|
||||
#endif
|
||||
extern "C" MODULE_EXPORT int APS5_VABI GuestModuleAdd_nid_postfix(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
@@ -288,3 +288,27 @@ PS5 mount semantics, cross-volume behavior, or replacement of existing directori
|
||||
The unchanged converted game resolves remove and rename. Startup now stops at
|
||||
`biY+kcVB5D4` (`dlsym`), requiring dynamic-module symbol lookup. It still has not
|
||||
executed the guest entry point or displayed a frame.
|
||||
|
||||
## Follow-up: dynamic-module lookup
|
||||
|
||||
Added dlopen, dlsym, dlclose, and dlerror to libkernel. Handles are tracked rather
|
||||
than exposing native loader handles. Symbol lookup tries the literal export and
|
||||
then its PS5 NID, reusing the project's existing hash implementation. Repeated
|
||||
opens hold separate loader references. Error messages are thread-local and are
|
||||
consumed by dlerror. Native unload runs outside the module registry lock.
|
||||
|
||||
A test builds and NID-patches a separate shared module, loads it, finds an export
|
||||
by its original name, and calls it through the guest SysV ABI. It also checks
|
||||
global visibility for explicitly opened modules, invalid/closed handles,
|
||||
repeated opens, missing symbols, unsupported flags, and error isolation.
|
||||
All eighteen suites pass.
|
||||
|
||||
This is host-compatible module loading, not raw PS5 ELF loading on Windows.
|
||||
RTLD_LAZY/NOW and GLOBAL are accepted; Windows binds eagerly. Default lookup
|
||||
currently searches explicitly registered global modules only. Automatic lookup
|
||||
across the startup dependency graph, RTLD_NEXT/SELF, other flags, and console
|
||||
load/start entry points remain unsupported. The guest path resolver is reused;
|
||||
full console library-search and mount semantics are not established.
|
||||
|
||||
Latest unchanged-game startup resolves dlsym and stops at `DYivN1nO-JQ`
|
||||
(`getcwd`). No guest entry-point execution or rendered frame has been observed.
|
||||
|
||||
Reference in New Issue
Block a user