From 6d42f3a9103f09bf1510c9fb5c47853498176f20 Mon Sep 17 00:00:00 2001 From: overlordxrz-source Date: Fri, 9 Oct 2026 05:15:57 +0000 Subject: [PATCH 1/2] feat(libkernel): implement sceKernelGetModuleList --- .../prx/libkernel/Module/src/ModuleInfo.cpp | 73 ++++++++++++++++++- core/libs/tests/GuestModuleInfo.cpp | 22 ++++++ docs/dev/TechnicalDebt.md | 1 + 3 files changed, 93 insertions(+), 3 deletions(-) diff --git a/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp b/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp index 4be19e83280a..dc1f1c3ee36a 100644 --- a/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp +++ b/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" #include "prx/libkernel/KernelErrors.hpp" @@ -14,6 +16,7 @@ #define NOMINMAX #endif #include +#include #include #else #include @@ -118,6 +121,32 @@ int FindImage(dl_phdr_info* image, std::size_t, void* data) { search.found = true; return 1; } + +int CollectGuestImage(dl_phdr_info* image, std::size_t, void* data) { + const std::string_view name = image->dlpi_name ? image->dlpi_name : ""; + if (!name.empty() && !name.ends_with(".prx")) return 0; + for (std::uint16_t i = 0; i < image->dlpi_phnum; ++i) { + const auto& header = image->dlpi_phdr[i]; + if (header.p_type != PT_LOAD) continue; + static_cast*>(data)->push_back(image->dlpi_addr + header.p_vaddr); + return 0; + } + return 0; +} + +std::vector GuestImages() { + std::vector addresses; + dl_iterate_phdr(CollectGuestImage, &addresses); + std::vector images; + for (const auto address : addresses) { + Dl_info symbol{}; + link_map* native = nullptr; + if (!dladdr1(reinterpret_cast(address), &symbol, reinterpret_cast(&native), RTLD_DL_LINKMAP) || !native) + throw std::runtime_error("sceKernelGetModuleList: no link map for a loaded image"); + images.push_back(native); + } + return images; +} #else bool SectionNamed(const IMAGE_SECTION_HEADER& section, const char* name) { return std::strncmp(reinterpret_cast(section.Name), name, IMAGE_SIZEOF_SHORT_NAME) == 0; @@ -128,17 +157,21 @@ std::int32_t SectionProtection(const IMAGE_SECTION_HEADER& section) { return ((characteristics & IMAGE_SCN_MEM_READ) ? ProtRead : 0) | ((characteristics & IMAGE_SCN_MEM_WRITE) ? ProtWrite : 0) | ((characteristics & IMAGE_SCN_MEM_EXECUTE) ? ProtExecute : 0); } -std::string ImageName(HMODULE module) { +std::wstring ModulePath(HMODULE module) { std::wstring path(MAX_PATH, L'\0'); for (;;) { const auto length = GetModuleFileNameW(module, path.data(), static_cast(path.size())); - if (length == 0) throw std::runtime_error("sceKernelGetModuleInfoFromAddr: cannot resolve the module path"); + if (length == 0) throw std::runtime_error("module info: GetModuleFileNameW failed with error " + std::to_string(GetLastError())); if (length < path.size()) { path.resize(length); - break; + return path; } path.resize(path.size() * 2); } +} + +std::string ImageName(HMODULE module) { + const auto path = ModulePath(module); const auto fileName = std::filesystem::path(path).filename().u8string(); return ModuleName(std::string(fileName.begin(), fileName.end())); } @@ -187,6 +220,31 @@ bool Fill(std::uintptr_t address, ModuleInfoEx& info) { info.ref_count = 1; return true; } + +std::vector GuestImages() { + const HANDLE process = GetCurrentProcess(); + std::vector modules(64); + for (;;) { + DWORD needed = 0; + const auto size = static_cast(modules.size() * sizeof(HMODULE)); + if (!EnumProcessModules(process, modules.data(), size, &needed)) + throw std::runtime_error("sceKernelGetModuleList: EnumProcessModules failed with error " + std::to_string(GetLastError())); + modules.resize(needed / sizeof(HMODULE)); + if (needed <= size) break; + } + const HMODULE executable = GetModuleHandleW(nullptr); + constexpr std::wstring_view suffix = L".prx"; + std::vector images; + for (const auto module : modules) { + if (module != executable) { + const auto path = ModulePath(module); + if (path.size() < suffix.size() || CompareStringOrdinal(path.data() + path.size() - suffix.size(), static_cast(suffix.size()), suffix.data(), static_cast(suffix.size()), TRUE) != CSTR_EQUAL) + continue; + } + images.push_back(module); + } + return images; +} #endif } @@ -216,4 +274,13 @@ int APS5_VABI sceKernelGetModuleInfoFromAddr(std::uint64_t address, int flags, M return 0; } +int APS5_VABI sceKernelGetModuleList(KernelModule* handles, std::size_t count, std::size_t* actual) { + if (!handles || !actual) return SCE_KERNEL_ERROR_EFAULT; + const auto images = GuestImages(); + for (std::size_t i = 0; i < images.size() && i < count; ++i) handles[i] = ModuleIdForImage_nid_no_patch(images[i]); + if (images.size() > count) return SCE_KERNEL_ERROR_ENOMEM; + *actual = images.size(); + return 0; +} + } diff --git a/core/libs/tests/GuestModuleInfo.cpp b/core/libs/tests/GuestModuleInfo.cpp index 797064fa943a..2b2b5721871b 100644 --- a/core/libs/tests/GuestModuleInfo.cpp +++ b/core/libs/tests/GuestModuleInfo.cpp @@ -1,15 +1,19 @@ #include "SceTypes.hpp" #include "prx/libkernel/KernelErrors.hpp" +#include +#include #include #include #include #include #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*); int APS5_VABI sceKernelGetModuleInfoFromAddr(std::uint64_t, int, ModuleInfoEx*); +int APS5_VABI sceKernelGetModuleList(KernelModule*, std::size_t, std::size_t*); } static void Require(bool value) { if (!value) std::abort(); } template @@ -55,6 +59,24 @@ int main(int argc, char** argv) { void* second = dlopen_nid_postfix(argv[1], 2); Require(second && Query(add, 0).id == info.id); Require(dlclose_nid_postfix(second) == 0); + std::vector handles(512, -1); + std::size_t count = 0; + Require(sceKernelGetModuleList(handles.data(), handles.size(), &count) == 0); + Require(count >= 2 && count < handles.size() && handles[count] == -1); + handles.resize(count); + Require(handles.front() == self.id); + const auto kernel = Query(reinterpret_cast(&sceKernelGetModuleList), 0).id; + Require(std::count(handles.begin(), handles.end(), kernel) == 1); + Require(std::count(handles.begin(), handles.end(), info.id) == 0); + auto sorted = handles; + std::sort(sorted.begin(), sorted.end()); + Require(std::adjacent_find(sorted.begin(), sorted.end()) == sorted.end()); + std::vector shorter(count, -1); + std::size_t untouched = 99; + Require(sceKernelGetModuleList(shorter.data(), count - 1, &untouched) == SCE_KERNEL_ERROR_ENOMEM); + Require(untouched == 99 && shorter.back() == -1 && std::equal(handles.begin(), handles.end() - 1, shorter.begin())); + Require(sceKernelGetModuleList(nullptr, 0, &untouched) == SCE_KERNEL_ERROR_EFAULT); + Require(sceKernelGetModuleList(handles.data(), handles.size(), nullptr) == SCE_KERNEL_ERROR_EFAULT); int local = 0; Query(&local, SCE_KERNEL_ERROR_ESRCH); Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), 2, nullptr) == SCE_KERNEL_ERROR_EFAULT); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 9f241ef7201f..ae815e77b73a 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -124,6 +124,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceKernelInternalMemoryGetModuleSegmentInfo](../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) - unknown signature - [sceKernelGetModuleInfoForUnwind](../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) - `eh_frame_size` is assumed to cover the records up to, not including, the zero terminator. LLVM libunwind's `CFI_Parser::findFDE` stops at the terminator or the length, whichever comes first, so it finds the same FDEs either way; what the console reports is not known. - [sceKernelGetModuleInfoFromAddr](../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the 0x1A8-byte info layout follows the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation; a title's `libc.prx` reads only `id` (PPSA14632). The meaning of `flags` is unknown; only 2 is accepted, as is only the full `st_size`. The id is the `sceKernelLoadStartModule` handle of an image opened through the loader and otherwise a stable id from the same range. The name is the host file name without `.guest.prx`. An image with more than 4 load segments (the converted executable has 7) reports its first 4. `tls_offset` is 0 and `ref_count` is 1, as the host loader exposes neither. On Windows the module is the PE image holding the address: a relinked image reports its `.elf` sections as segments and the eh_frame_hdr recorded in `.ehmeta`, any other image its first 4 sections with memory access and the `.ehfram` section without an eh_frame_hdr; the TLS fields and `init_proc_addr`/`fini_proc_addr` are 0 +- [sceKernelGetModuleList](../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - lists the main program and every loaded `.prx` image in load order; host runtime libraries are left out. The handles are the ids `sceKernelGetModuleInfoFromAddr` reports, not the console's. The `(int32_t* handles, size_t count, size_t* actual)` signature and returning `ENOMEM` without writing `actual` after filling the first `count` handles when more images are loaded follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/0fe263a4760dfbfa973366890061749b4af0de97/src/core/libraries/kernel/process.cpp#L244-L264); whether the console writes any handles in that case is unknown - [sceKernelReleaseFlexibleMemory](../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - unmaps the range like sceKernelMunmap, as fpPS4 and prosper do; whether the console keeps the virtual range reserved after releasing its pages is unverified - [Memory protection bits 0x100 and 0x200](../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) - taken as audio coprocessor (ACP) read and write, as in the [KytyPS5](https://github.com/KytyPS5/KytyPS5) and [etaHEN](https://github.com/etaHEN/etaHEN) headers; the coprocessor work runs on the host CPU, so they grant host read and read/write access like the GPU bits - [sceKernelSyncOnAddressWait](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - the only known caller passes a null timeout and a name string as the fourth argument; the timeout is assumed to point to microseconds like the other kernel waits, and the name is ignored From 8a41d280af91658ca9bd3e9dc35cd31160059d47 Mon Sep 17 00:00:00 2001 From: overlordxrz-source Date: Fri, 9 Oct 2026 22:57:56 +0000 Subject: [PATCH 2/2] feat(libkernel): implement sceKernelGetModuleInfo Fill takes either info struct, so sceKernelGetModuleInfo reports the name and segments sceKernelGetModuleInfoFromAddr reports for the same image, on Linux and Windows. --- core/libs/SceTypes.hpp | 13 ++ .../prx/libkernel/Module/src/ModuleInfo.cpp | 132 ++++++++++++------ core/libs/tests/GuestModuleInfo.cpp | 28 ++++ docs/dev/TechnicalDebt.md | 1 + 4 files changed, 129 insertions(+), 45 deletions(-) diff --git a/core/libs/SceTypes.hpp b/core/libs/SceTypes.hpp index 23df8543e481..99211616a57f 100644 --- a/core/libs/SceTypes.hpp +++ b/core/libs/SceTypes.hpp @@ -205,6 +205,19 @@ struct ModuleSegmentInfo { std::int32_t prot; }; +struct ModuleInfo { + std::uint64_t st_size; + char name[256]; + ModuleSegmentInfo segments[4]; + std::uint32_t segment_count; + std::uint8_t fingerprint[20]; +}; +static_assert(offsetof(ModuleInfo, name) == 0x8); +static_assert(offsetof(ModuleInfo, segments) == 0x108); +static_assert(offsetof(ModuleInfo, segment_count) == 0x148); +static_assert(offsetof(ModuleInfo, fingerprint) == 0x14C); +static_assert(sizeof(ModuleInfo) == 0x160); + struct ModuleInfoEx { std::uint64_t st_size; char name[256]; diff --git a/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp b/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp index dc1f1c3ee36a..2a8d8a5ac385 100644 --- a/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp +++ b/core/libs/prx/libkernel/Module/src/ModuleInfo.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include "SceTypes.hpp" #include "prx/libc/include/General.hpp" @@ -35,9 +36,12 @@ constexpr std::int32_t ProtRead = 1; constexpr std::int32_t ProtWrite = 2; constexpr std::int32_t ProtExecute = 4; +template +constexpr bool Extended = std::is_same_v; + std::uint32_t ToU32(std::uint64_t value, const char* field) { if (value > std::numeric_limits::max()) - throw std::runtime_error(std::string("sceKernelGetModuleInfoFromAddr: ") + field + " exceeds 32 bits"); + throw std::runtime_error(std::string("module info: ") + field + " exceeds 32 bits"); return static_cast(value); } @@ -47,16 +51,23 @@ std::string ModuleName(std::string fileName) { return fileName; } -void SetName(ModuleInfoEx& info, const std::string& name) { +template +void SetName(TInfo& info, const std::string& name) { if (name.size() >= sizeof(info.name)) - throw std::runtime_error("sceKernelGetModuleInfoFromAddr: module name too long: " + name); + throw std::runtime_error("module info: module name too long: " + name); std::memcpy(info.name, name.c_str(), name.size() + 1); } +struct GuestImage { + const void* native; + std::uintptr_t address; +}; + #ifndef _WIN32 +template struct ImageSearch { std::uintptr_t address; - ModuleInfoEx* info; + TInfo* info; bool found; }; @@ -81,10 +92,10 @@ std::string ImageName(const dl_phdr_info& image) { return ModuleName(path.substr(path.find_last_of('/') + 1)); } -void Fill(const dl_phdr_info& image, ModuleInfoEx& info) { - const auto contains = [&](std::uintptr_t begin, std::uint64_t size) { return Contains(image, begin, size); }; +template +void Fill(const dl_phdr_info& image, TInfo& info) { SetName(info, ImageName(image)); - info.tls_index = ToU32(image.dlpi_tls_modid, "TLS module index"); + if constexpr (Extended) info.tls_index = ToU32(image.dlpi_tls_modid, "TLS module index"); for (std::uint16_t i = 0; i < image.dlpi_phnum; ++i) { const auto& header = image.dlpi_phdr[i]; const std::uintptr_t address = image.dlpi_addr + header.p_vaddr; @@ -93,29 +104,33 @@ void Fill(const dl_phdr_info& image, ModuleInfoEx& info) { segment.address = address; segment.size = ToU32(header.p_memsz, "segment size"); segment.prot = ((header.p_flags & PF_R) ? ProtRead : 0) | ((header.p_flags & PF_W) ? ProtWrite : 0) | ((header.p_flags & PF_X) ? ProtExecute : 0); - } else if (header.p_type == PT_TLS) { - info.tls_init_addr = address; - info.tls_init_size = ToU32(header.p_filesz, "TLS image size"); - info.tls_size = ToU32(header.p_memsz, "TLS size"); - info.tls_align = ToU32(header.p_align, "TLS alignment"); - } else if (header.p_type == PT_GNU_EH_FRAME) { - const auto tables = EhFrame::ReadTables(contains, address, Caller); - info.eh_frame_hdr_addr = address; - info.eh_frame_hdr_size = ToU32(header.p_memsz, "eh_frame_hdr size"); - info.eh_frame_addr = tables.frames; - info.eh_frame_size = ToU32(tables.framesSize + TerminatorSize, "eh_frame size"); - } else if (header.p_type == PT_DYNAMIC) { - for (const auto* entry = reinterpret_cast(address); entry->d_tag != DT_NULL; ++entry) { - if (entry->d_tag == DT_INIT) info.init_proc_addr = image.dlpi_addr + entry->d_un.d_ptr; - else if (entry->d_tag == DT_FINI) info.fini_proc_addr = image.dlpi_addr + entry->d_un.d_ptr; + } else if constexpr (Extended) { + if (header.p_type == PT_TLS) { + info.tls_init_addr = address; + info.tls_init_size = ToU32(header.p_filesz, "TLS image size"); + info.tls_size = ToU32(header.p_memsz, "TLS size"); + info.tls_align = ToU32(header.p_align, "TLS alignment"); + } else if (header.p_type == PT_GNU_EH_FRAME) { + const auto contains = [&](std::uintptr_t begin, std::uint64_t size) { return Contains(image, begin, size); }; + const auto tables = EhFrame::ReadTables(contains, address, Caller); + info.eh_frame_hdr_addr = address; + info.eh_frame_hdr_size = ToU32(header.p_memsz, "eh_frame_hdr size"); + info.eh_frame_addr = tables.frames; + info.eh_frame_size = ToU32(tables.framesSize + TerminatorSize, "eh_frame size"); + } else if (header.p_type == PT_DYNAMIC) { + for (const auto* entry = reinterpret_cast(address); entry->d_tag != DT_NULL; ++entry) { + if (entry->d_tag == DT_INIT) info.init_proc_addr = image.dlpi_addr + entry->d_un.d_ptr; + else if (entry->d_tag == DT_FINI) info.fini_proc_addr = image.dlpi_addr + entry->d_un.d_ptr; + } } } } - info.ref_count = 1; + if constexpr (Extended) info.ref_count = 1; } +template int FindImage(dl_phdr_info* image, std::size_t, void* data) { - auto& search = *static_cast(data); + auto& search = *static_cast*>(data); if (!Contains(*image, search.address, 1)) return 0; Fill(*image, *search.info); search.found = true; @@ -134,16 +149,16 @@ int CollectGuestImage(dl_phdr_info* image, std::size_t, void* data) { return 0; } -std::vector GuestImages() { +std::vector GuestImages() { std::vector addresses; dl_iterate_phdr(CollectGuestImage, &addresses); - std::vector images; + std::vector images; for (const auto address : addresses) { Dl_info symbol{}; link_map* native = nullptr; if (!dladdr1(reinterpret_cast(address), &symbol, reinterpret_cast(&native), RTLD_DL_LINKMAP) || !native) throw std::runtime_error("sceKernelGetModuleList: no link map for a loaded image"); - images.push_back(native); + images.push_back({native, address}); } return images; } @@ -176,7 +191,8 @@ std::string ImageName(HMODULE module) { return ModuleName(std::string(fileName.begin(), fileName.end())); } -bool Fill(std::uintptr_t address, ModuleInfoEx& info) { +template +bool Fill(std::uintptr_t address, TInfo& info) { MEMORY_BASIC_INFORMATION memory{}; if (VirtualQuery(reinterpret_cast(address), &memory, sizeof(memory)) != sizeof(memory) || memory.Type != MEM_IMAGE) return false; const auto base = reinterpret_cast(memory.AllocationBase); @@ -184,8 +200,6 @@ bool Fill(std::uintptr_t address, ModuleInfoEx& info) { if (dos->e_magic != IMAGE_DOS_SIGNATURE) return false; const auto* nt = reinterpret_cast(base + static_cast(dos->e_lfanew)); if (nt->Signature != IMAGE_NT_SIGNATURE || nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR64_MAGIC) return false; - const std::uint64_t imageSize = nt->OptionalHeader.SizeOfImage; - const auto contains = [&](std::uintptr_t begin, std::uint64_t size) { return begin >= base && begin - base <= imageSize && size <= imageSize - (begin - base); }; SetName(info, ImageName(reinterpret_cast(base))); const auto* sections = IMAGE_FIRST_SECTION(nt); const auto sectionCount = nt->FileHeader.NumberOfSections; @@ -193,19 +207,25 @@ bool Fill(std::uintptr_t address, ModuleInfoEx& info) { for (unsigned i = 0; i < sectionCount; ++i) relinked = relinked || SectionNamed(sections[i], ".elf0"); EhFrame::Tables tables; const bool hasEhmeta = EhFrame::ReadEhmeta(base, *nt, Caller, tables); - if (hasEhmeta) { - info.eh_frame_hdr_addr = tables.header; - info.eh_frame_hdr_size = ToU32(tables.headerSize, "eh_frame_hdr size"); - info.eh_frame_addr = tables.frames; - info.eh_frame_size = ToU32(tables.framesSize + TerminatorSize, "eh_frame size"); + if constexpr (Extended) { + if (hasEhmeta) { + info.eh_frame_hdr_addr = tables.header; + info.eh_frame_hdr_size = ToU32(tables.headerSize, "eh_frame_hdr size"); + info.eh_frame_addr = tables.frames; + info.eh_frame_size = ToU32(tables.framesSize + TerminatorSize, "eh_frame size"); + } } for (unsigned i = 0; i < sectionCount; ++i) { const auto& section = sections[i]; const auto start = base + section.VirtualAddress; if (SectionNamed(section, ".ehmeta")) continue; if (SectionNamed(section, ".ehfram") && !hasEhmeta) { - info.eh_frame_addr = start; - info.eh_frame_size = ToU32(EhFrame::FramesSize(contains, start, Caller) + TerminatorSize, "eh_frame size"); + if constexpr (Extended) { + const std::uint64_t imageSize = nt->OptionalHeader.SizeOfImage; + const auto contains = [&](std::uintptr_t begin, std::uint64_t size) { return begin >= base && begin - base <= imageSize && size <= imageSize - (begin - base); }; + info.eh_frame_addr = start; + info.eh_frame_size = ToU32(EhFrame::FramesSize(contains, start, Caller) + TerminatorSize, "eh_frame size"); + } continue; } if (relinked && std::strncmp(reinterpret_cast(section.Name), ".elf", 4) != 0) continue; @@ -216,12 +236,14 @@ bool Fill(std::uintptr_t address, ModuleInfoEx& info) { segment.size = ToU32(section.Misc.VirtualSize, "segment size"); segment.prot = prot; } - info.id = ModuleIdForImage_nid_no_patch(reinterpret_cast(base)); - info.ref_count = 1; + if constexpr (Extended) { + info.id = ModuleIdForImage_nid_no_patch(reinterpret_cast(base)); + info.ref_count = 1; + } return true; } -std::vector GuestImages() { +std::vector GuestImages() { const HANDLE process = GetCurrentProcess(); std::vector modules(64); for (;;) { @@ -234,14 +256,14 @@ std::vector GuestImages() { } const HMODULE executable = GetModuleHandleW(nullptr); constexpr std::wstring_view suffix = L".prx"; - std::vector images; + std::vector images; for (const auto module : modules) { if (module != executable) { const auto path = ModulePath(module); if (path.size() < suffix.size() || CompareStringOrdinal(path.data() + path.size() - suffix.size(), static_cast(suffix.size()), suffix.data(), static_cast(suffix.size()), TRUE) != CSTR_EQUAL) continue; } - images.push_back(module); + images.push_back({module, reinterpret_cast(module)}); } return images; } @@ -265,8 +287,8 @@ int APS5_VABI sceKernelGetModuleInfoFromAddr(std::uint64_t address, int flags, M link_map* native = nullptr; if (!dladdr1(reinterpret_cast(address), &symbol, reinterpret_cast(&native), RTLD_DL_LINKMAP) || !native) return SCE_KERNEL_ERROR_ESRCH; - ImageSearch search{static_cast(address), &result, false}; - dl_iterate_phdr(FindImage, &search); + ImageSearch search{static_cast(address), &result, false}; + dl_iterate_phdr(FindImage, &search); if (!search.found) return SCE_KERNEL_ERROR_ESRCH; result.id = ModuleIdForImage_nid_no_patch(native); #endif @@ -277,10 +299,30 @@ int APS5_VABI sceKernelGetModuleInfoFromAddr(std::uint64_t address, int flags, M int APS5_VABI sceKernelGetModuleList(KernelModule* handles, std::size_t count, std::size_t* actual) { if (!handles || !actual) return SCE_KERNEL_ERROR_EFAULT; const auto images = GuestImages(); - for (std::size_t i = 0; i < images.size() && i < count; ++i) handles[i] = ModuleIdForImage_nid_no_patch(images[i]); + for (std::size_t i = 0; i < images.size() && i < count; ++i) handles[i] = ModuleIdForImage_nid_no_patch(images[i].native); if (images.size() > count) return SCE_KERNEL_ERROR_ENOMEM; *actual = images.size(); return 0; } +int APS5_VABI sceKernelGetModuleInfo(KernelModule handle, ModuleInfo* info) { + if (!info) return SCE_KERNEL_ERROR_EFAULT; + if (info->st_size != sizeof(ModuleInfo)) return SCE_KERNEL_ERROR_EINVAL; + for (const auto& image : GuestImages()) { + if (ModuleIdForImage_nid_no_patch(image.native) != handle) continue; + ModuleInfo result{}; + result.st_size = sizeof(ModuleInfo); +#ifdef _WIN32 + if (!Fill(image.address, result)) return SCE_KERNEL_ERROR_ESRCH; +#else + ImageSearch search{image.address, &result, false}; + dl_iterate_phdr(FindImage, &search); + if (!search.found) return SCE_KERNEL_ERROR_ESRCH; +#endif + *info = result; + return 0; + } + return SCE_KERNEL_ERROR_ESRCH; +} + } diff --git a/core/libs/tests/GuestModuleInfo.cpp b/core/libs/tests/GuestModuleInfo.cpp index 2b2b5721871b..9cfc631e0a04 100644 --- a/core/libs/tests/GuestModuleInfo.cpp +++ b/core/libs/tests/GuestModuleInfo.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -14,6 +15,7 @@ void* APS5_VABI dlsym_nid_postfix(void*, const char*); int APS5_VABI dlclose_nid_postfix(void*); int APS5_VABI sceKernelGetModuleInfoFromAddr(std::uint64_t, int, ModuleInfoEx*); int APS5_VABI sceKernelGetModuleList(KernelModule*, std::size_t, std::size_t*); +int APS5_VABI sceKernelGetModuleInfo(KernelModule, ModuleInfo*); } static void Require(bool value) { if (!value) std::abort(); } template @@ -77,6 +79,32 @@ int main(int argc, char** argv) { Require(untouched == 99 && shorter.back() == -1 && std::equal(handles.begin(), handles.end() - 1, shorter.begin())); Require(sceKernelGetModuleList(nullptr, 0, &untouched) == SCE_KERNEL_ERROR_EFAULT); Require(sceKernelGetModuleList(handles.data(), handles.size(), nullptr) == SCE_KERNEL_ERROR_EFAULT); + const auto kernelEx = Query(reinterpret_cast(&sceKernelGetModuleInfo), 0); + ModuleInfo kernelInfo{}; + kernelInfo.st_size = sizeof(ModuleInfo); + Require(sceKernelGetModuleInfo(kernel, &kernelInfo) == 0); + Require(kernelInfo.st_size == sizeof(ModuleInfo) && std::string(kernelInfo.name) == kernelEx.name); + Require(kernelInfo.segment_count == kernelEx.segment_count && kernelInfo.segment_count > 0); + for (std::uint32_t i = 0; i < kernelInfo.segment_count; ++i) + Require(kernelInfo.segments[i].address == kernelEx.segments[i].address && kernelInfo.segments[i].size == kernelEx.segments[i].size && kernelInfo.segments[i].prot == kernelEx.segments[i].prot); + Require(std::all_of(std::begin(kernelInfo.fingerprint), std::end(kernelInfo.fingerprint), [](std::uint8_t byte) { return byte == 0; })); + for (const auto handle : handles) { + ModuleInfo listed{}; + listed.st_size = sizeof(ModuleInfo); + Require(sceKernelGetModuleInfo(handle, &listed) == 0 && listed.name[0] != '\0'); + if (handle == self.id) Require(std::string(listed.name) == self.name); + } + for (const std::uint64_t size : {0x158, 0x1a8, 0x1b0, 0x200, 0}) { + ModuleInfo sized{}; + sized.st_size = size; + sized.name[0] = 'x'; + Require(sceKernelGetModuleInfo(kernel, &sized) == SCE_KERNEL_ERROR_EINVAL && sized.st_size == size && sized.name[0] == 'x'); + } + Require(sceKernelGetModuleInfo(kernel, nullptr) == SCE_KERNEL_ERROR_EFAULT); + ModuleInfo unknown{}; + unknown.st_size = sizeof(ModuleInfo); + Require(sceKernelGetModuleInfo(info.id, &unknown) == SCE_KERNEL_ERROR_ESRCH); + Require(sceKernelGetModuleInfo(-1, &unknown) == SCE_KERNEL_ERROR_ESRCH && unknown.name[0] == '\0'); int local = 0; Query(&local, SCE_KERNEL_ERROR_ESRCH); Require(sceKernelGetModuleInfoFromAddr(reinterpret_cast(add), 2, nullptr) == SCE_KERNEL_ERROR_EFAULT); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index ae815e77b73a..5ef8dd62a7d5 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -125,6 +125,7 @@ Throughout the project, every function at every stage either **does exactly what - [sceKernelGetModuleInfoForUnwind](../../core/libs/prx/libkernel/Module/src/Module.cpp) (libkernel) - `eh_frame_size` is assumed to cover the records up to, not including, the zero terminator. LLVM libunwind's `CFI_Parser::findFDE` stops at the terminator or the length, whichever comes first, so it finds the same FDEs either way; what the console reports is not known. - [sceKernelGetModuleInfoFromAddr](../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the 0x1A8-byte info layout follows the [shadPS4](https://github.com/shadps4-emu/shadPS4) reimplementation; a title's `libc.prx` reads only `id` (PPSA14632). The meaning of `flags` is unknown; only 2 is accepted, as is only the full `st_size`. The id is the `sceKernelLoadStartModule` handle of an image opened through the loader and otherwise a stable id from the same range. The name is the host file name without `.guest.prx`. An image with more than 4 load segments (the converted executable has 7) reports its first 4. `tls_offset` is 0 and `ref_count` is 1, as the host loader exposes neither. On Windows the module is the PE image holding the address: a relinked image reports its `.elf` sections as segments and the eh_frame_hdr recorded in `.ehmeta`, any other image its first 4 sections with memory access and the `.ehfram` section without an eh_frame_hdr; the TLS fields and `init_proc_addr`/`fini_proc_addr` are 0 - [sceKernelGetModuleList](../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - lists the main program and every loaded `.prx` image in load order; host runtime libraries are left out. The handles are the ids `sceKernelGetModuleInfoFromAddr` reports, not the console's. The `(int32_t* handles, size_t count, size_t* actual)` signature and returning `ENOMEM` without writing `actual` after filling the first `count` handles when more images are loaded follow [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/0fe263a4760dfbfa973366890061749b4af0de97/src/core/libraries/kernel/process.cpp#L244-L264); whether the console writes any handles in that case is unknown +- [sceKernelGetModuleInfo](../../core/libs/prx/libkernel/Module/src/ModuleInfo.cpp) (libkernel) - the 0x160-byte layout (`st_size`, `name[256]`, 4 segments, `segment_count`, `fingerprint[20]`) follows [fpPS4](https://github.com/red-prig/fpPS4/blob/04cefd43e6fddd1ab033e7980cd356d14c964905/sys/sys_types.pas#L319-L337) and [shadPS4](https://github.com/shadps4-emu/shadPS4/blob/0fe263a4760dfbfa973366890061749b4af0de97/src/core/module.h#L15-L31). Only that `st_size` is accepted, as in shadPS4; other sizes return `EINVAL`, a null pointer `EFAULT` and a handle `sceKernelGetModuleList` does not list `ESRCH`. Name and segments are the ones `sceKernelGetModuleInfoFromAddr` reports, on Windows from the same PE sections, so `prot` uses the read/write/execute bits 1/2/4, while fpPS4 and shadPS4 store the ELF `p_flags`. The fingerprint is left zero, as the host image does not carry it - [sceKernelReleaseFlexibleMemory](../../core/libs/prx/libkernel/DirectMemory/Export.cpp) (libkernel) - unmaps the range like sceKernelMunmap, as fpPS4 and prosper do; whether the console keeps the virtual range reserved after releasing its pages is unverified - [Memory protection bits 0x100 and 0x200](../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp) (libkernel) - taken as audio coprocessor (ACP) read and write, as in the [KytyPS5](https://github.com/KytyPS5/KytyPS5) and [etaHEN](https://github.com/etaHEN/etaHEN) headers; the coprocessor work runs on the host CPU, so they grant host read and read/write access like the GPU bits - [sceKernelSyncOnAddressWait](../../core/libs/prx/libkernel/SyncOnAddress/src/SyncOnAddress.cpp) (libkernel) - the only known caller passes a null timeout and a name string as the fourth argument; the timeout is assumed to point to microseconds like the other kernel waits, and the name is ignored