diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index bf8951bda..64601945e 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -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 $ 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 $) + set_tests_properties(guest_dynamic_loader PROPERTIES + WORKING_DIRECTORY "$") + if(WIN32) + set_tests_properties(guest_dynamic_loader PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + 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) diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 2a8d35118..08098fd1e 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -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) diff --git a/core/libs/prx/libkernel/Module/src/DynamicLoader.cpp b/core/libs/prx/libkernel/Module/src/DynamicLoader.cpp new file mode 100644 index 000000000..e1091def8 --- /dev/null +++ b/core/libs/prx/libkernel/Module/src/DynamicLoader.cpp @@ -0,0 +1,126 @@ +#include "prx/libc/include/General.hpp" +#include +#include +#include +#include +#include +#include +#include +#ifdef _WIN32 +#define NOMINMAX +#include +#else +#include +#endif + +namespace { +thread_local std::array 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(native)); +#else + ::dlclose(native); +#endif + } + } +}; +std::mutex modulesMutex; +std::map> modules; +std::uintptr_t nextHandle = 0x20000000; +void* Symbol(Module& module, const char* name) { +#ifdef _WIN32 + return reinterpret_cast(GetProcAddress(static_cast(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->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(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> search; + { + std::lock_guard lock(modulesMutex); + if (handle == reinterpret_cast(static_cast(-2))) { + for (const auto& [key, module] : modules) if (module->global) search.push_back(module); + } else { + auto found = modules.find(reinterpret_cast(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; + { + std::lock_guard lock(modulesMutex); + auto found = modules.find(reinterpret_cast(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; +} +} diff --git a/core/libs/tests/GuestDynamicLoader.cpp b/core/libs/tests/GuestDynamicLoader.cpp new file mode 100644 index 000000000..2fa27e917 --- /dev/null +++ b/core/libs/tests/GuestDynamicLoader.cpp @@ -0,0 +1,40 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +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(dlsym_nid_postfix(module, "GuestModuleAdd")); + Require(add && add(17, 25) == 42); + Require(dlsym_nid_postfix(reinterpret_cast(-2), "GuestModuleAdd") == reinterpret_cast(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(dlsym_nid_postfix(second, "GuestModuleAdd")); + Require(add && add(2, 3) == 5); + Require(dlclose_nid_postfix(second) == 0); + Require(dlclose_nid_postfix(second) == -1); +} diff --git a/core/libs/tests/GuestModuleFixture.cpp b/core/libs/tests/GuestModuleFixture.cpp new file mode 100644 index 000000000..e8f8521d6 --- /dev/null +++ b/core/libs/tests/GuestModuleFixture.cpp @@ -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; +} diff --git a/docs/YAMAGI_QUAKE2.md b/docs/YAMAGI_QUAKE2.md index bd3753009..0d26407ae 100644 --- a/docs/YAMAGI_QUAKE2.md +++ b/docs/YAMAGI_QUAKE2.md @@ -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.