From af0bdb89ed65e50e21557af5cfa393ab32fa4394 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Wed, 16 Jul 2025 11:59:26 +0200 Subject: [PATCH 1/5] Initialize the heap api --- src/core/linker.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/core/linker.cpp b/src/core/linker.cpp index 1f45caf12..f2c75c85c 100644 --- a/src/core/linker.cpp +++ b/src/core/linker.cpp @@ -49,7 +49,16 @@ static PS4_SYSV_ABI void* RunMainEntry [[noreturn]] (EntryParams* params) { } #endif -Linker::Linker() : memory{Memory::Instance()} {} +Linker::Linker() + : memory{Memory::Instance()}, heap_api{new HeapAPI{.heap_malloc = &std::malloc, + .heap_free = &std::free, + .heap_calloc = &std::calloc, + .heap_realloc = &std::realloc, + .heap_memalign = nullptr, + .heap_posix_memalign = nullptr, + .heap_reallocalign = nullptr, + .heap_malloc_stats = nullptr, + .heap_malloc_usable_size = nullptr}} {} Linker::~Linker() = default; From 25afeb36024eb46031be464eb3d394115981c400 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Wed, 16 Jul 2025 12:56:43 +0200 Subject: [PATCH 2/5] Add back memory functions that were already added once Co-authored-by: squidbus <175574877+squidbus@users.noreply.github.com> --- .../libc_internal/libc_internal_memory.cpp | 50 +++++++++++++++++++ .../libc_internal/libc_internal_memory.h | 19 +++++++ 2 files changed, 69 insertions(+) diff --git a/src/core/libraries/libc_internal/libc_internal_memory.cpp b/src/core/libraries/libc_internal/libc_internal_memory.cpp index 0d8c421af..47aa80908 100644 --- a/src/core/libraries/libc_internal/libc_internal_memory.cpp +++ b/src/core/libraries/libc_internal/libc_internal_memory.cpp @@ -30,6 +30,43 @@ s32 PS4_SYSV_ABI internal_memcmp(const void* s1, const void* s2, size_t n) { return std::memcmp(s1, s2, n); } +void* PS4_SYSV_ABI internal_malloc(size_t size) { + return std::malloc(size); +} + +void PS4_SYSV_ABI internal_free(void* ptr) { + std::free(ptr); +} + +void* PS4_SYSV_ABI internal_operator_new(size_t size) { + if (size == 0) { + // Size of 1 is used if 0 is provided. + size = 1; + } + void* ptr = std::malloc(size); + ASSERT_MSG(ptr, "Failed to allocate new object with size {}", size); + return ptr; +} + +void PS4_SYSV_ABI internal_operator_delete(void* ptr) { + if (ptr) { + std::free(ptr); + } +} + +int PS4_SYSV_ABI internal_posix_memalign(void** ptr, size_t alignment, size_t size) { +#ifdef _WIN64 + void* allocated = _aligned_malloc(size, alignment); + if (!allocated) { + return errno; + } + *ptr = allocated; + return 0; +#else + return posix_memalign(ptr, alignment, size); +#endif +} + void RegisterlibSceLibcInternalMemory(Core::Loader::SymbolsResolver* sym) { LIB_FUNCTION("NFLs+dRJGNg", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, @@ -40,6 +77,19 @@ void RegisterlibSceLibcInternalMemory(Core::Loader::SymbolsResolver* sym) { internal_memset); LIB_FUNCTION("DfivPArhucg", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, internal_memcmp); + LIB_FUNCTION("gQX+4GDQjpM", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, + internal_malloc); + LIB_FUNCTION("tIhsqj0qsFE", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, internal_free); + LIB_FUNCTION("fJnpuVVBbKk", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, + internal_operator_new); + LIB_FUNCTION("hdm0YfMa7TQ", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, + internal_operator_new); + LIB_FUNCTION("MLWl90SFWNE", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, + internal_operator_delete); + LIB_FUNCTION("z+P+xCnWLBk", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, + internal_operator_delete); + LIB_FUNCTION("cVSk9y8URbc", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, + internal_posix_memalign); } } // namespace Libraries::LibcInternal diff --git a/src/core/libraries/libc_internal/libc_internal_memory.h b/src/core/libraries/libc_internal/libc_internal_memory.h index 995de935b..4b1a5c407 100644 --- a/src/core/libraries/libc_internal/libc_internal_memory.h +++ b/src/core/libraries/libc_internal/libc_internal_memory.h @@ -10,5 +10,24 @@ class SymbolsResolver; } namespace Libraries::LibcInternal { + +void* PS4_SYSV_ABI internal_memset(void* s, int c, size_t n); + +void* PS4_SYSV_ABI internal_memcpy(void* dest, const void* src, size_t n); + +s32 PS4_SYSV_ABI internal_memcpy_s(void* dest, size_t destsz, const void* src, size_t count); + +s32 PS4_SYSV_ABI internal_memcmp(const void* s1, const void* s2, size_t n); + +void* PS4_SYSV_ABI internal_malloc(size_t size); + +void PS4_SYSV_ABI internal_free(void* ptr); + +void* PS4_SYSV_ABI internal_operator_new(size_t size); + +void PS4_SYSV_ABI internal_operator_delete(void* ptr); + +int PS4_SYSV_ABI internal_posix_memalign(void** ptr, size_t alignment, size_t size); + void RegisterlibSceLibcInternalMemory(Core::Loader::SymbolsResolver* sym); } // namespace Libraries::LibcInternal \ No newline at end of file From 91bde2cbfa9121b548145b413b49b5d9f31c60b2 Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Wed, 16 Jul 2025 12:57:45 +0200 Subject: [PATCH 3/5] Change heap API functions to the internal libc functions --- src/core/linker.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/core/linker.cpp b/src/core/linker.cpp index f2c75c85c..80c3f5ea3 100644 --- a/src/core/linker.cpp +++ b/src/core/linker.cpp @@ -15,6 +15,7 @@ #include "core/devtools/widget/module_list.h" #include "core/libraries/kernel/memory.h" #include "core/libraries/kernel/threads.h" +#include "core/libraries/libc_internal/libc_internal_memory.h" #include "core/linker.h" #include "core/memory.h" #include "core/tls.h" @@ -50,12 +51,12 @@ static PS4_SYSV_ABI void* RunMainEntry [[noreturn]] (EntryParams* params) { #endif Linker::Linker() - : memory{Memory::Instance()}, heap_api{new HeapAPI{.heap_malloc = &std::malloc, - .heap_free = &std::free, - .heap_calloc = &std::calloc, - .heap_realloc = &std::realloc, + : memory{Memory::Instance()}, heap_api{new HeapAPI{.heap_malloc = &Libraries::LibcInternal::internal_malloc, + .heap_free = &Libraries::LibcInternal::internal_free, + .heap_calloc = &Libraries::LibcInternal::internal_calloc, + .heap_realloc = &Libraries::LibcInternal::internal_realloc, .heap_memalign = nullptr, - .heap_posix_memalign = nullptr, + .heap_posix_memalign = Libraries::LibcInternal::internal_posix_memalign, .heap_reallocalign = nullptr, .heap_malloc_stats = nullptr, .heap_malloc_usable_size = nullptr}} {} From 3842a02d255ac53ab9eb722df5a29ea7e74ad29b Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Wed, 16 Jul 2025 13:00:53 +0200 Subject: [PATCH 4/5] Add calloc and realloc --- src/core/libraries/libc_internal/libc_internal_memory.cpp | 8 ++++++++ src/core/libraries/libc_internal/libc_internal_memory.h | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/src/core/libraries/libc_internal/libc_internal_memory.cpp b/src/core/libraries/libc_internal/libc_internal_memory.cpp index 47aa80908..23cddd80d 100644 --- a/src/core/libraries/libc_internal/libc_internal_memory.cpp +++ b/src/core/libraries/libc_internal/libc_internal_memory.cpp @@ -67,6 +67,14 @@ int PS4_SYSV_ABI internal_posix_memalign(void** ptr, size_t alignment, size_t si #endif } +void* PS4_SYSV_ABI internal_calloc(size_t num, size_t size) { + return std::calloc(num, size); +} + +void* PS4_SYSV_ABI internal_realloc(void* ptr, size_t new_size) { + return std::realloc(ptr, new_size); +} + void RegisterlibSceLibcInternalMemory(Core::Loader::SymbolsResolver* sym) { LIB_FUNCTION("NFLs+dRJGNg", "libSceLibcInternal", 1, "libSceLibcInternal", 1, 1, diff --git a/src/core/libraries/libc_internal/libc_internal_memory.h b/src/core/libraries/libc_internal/libc_internal_memory.h index 4b1a5c407..5746f7c2e 100644 --- a/src/core/libraries/libc_internal/libc_internal_memory.h +++ b/src/core/libraries/libc_internal/libc_internal_memory.h @@ -29,5 +29,9 @@ void PS4_SYSV_ABI internal_operator_delete(void* ptr); int PS4_SYSV_ABI internal_posix_memalign(void** ptr, size_t alignment, size_t size); +void* PS4_SYSV_ABI internal_calloc(size_t num, size_t size); + +void* PS4_SYSV_ABI internal_realloc(void* ptr, size_t new_size); + void RegisterlibSceLibcInternalMemory(Core::Loader::SymbolsResolver* sym); } // namespace Libraries::LibcInternal \ No newline at end of file From ca7cce079e9f9328b47104b10698103c7900a47b Mon Sep 17 00:00:00 2001 From: kalaposfos13 <153381648+kalaposfos13@users.noreply.github.com> Date: Wed, 16 Jul 2025 14:35:11 +0200 Subject: [PATCH 5/5] Fine clang, you win --- src/core/linker.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/core/linker.cpp b/src/core/linker.cpp index 80c3f5ea3..482a06e2c 100644 --- a/src/core/linker.cpp +++ b/src/core/linker.cpp @@ -51,15 +51,16 @@ static PS4_SYSV_ABI void* RunMainEntry [[noreturn]] (EntryParams* params) { #endif Linker::Linker() - : memory{Memory::Instance()}, heap_api{new HeapAPI{.heap_malloc = &Libraries::LibcInternal::internal_malloc, - .heap_free = &Libraries::LibcInternal::internal_free, - .heap_calloc = &Libraries::LibcInternal::internal_calloc, - .heap_realloc = &Libraries::LibcInternal::internal_realloc, - .heap_memalign = nullptr, - .heap_posix_memalign = Libraries::LibcInternal::internal_posix_memalign, - .heap_reallocalign = nullptr, - .heap_malloc_stats = nullptr, - .heap_malloc_usable_size = nullptr}} {} + : memory{Memory::Instance()}, + heap_api{new HeapAPI{.heap_malloc = &Libraries::LibcInternal::internal_malloc, + .heap_free = &Libraries::LibcInternal::internal_free, + .heap_calloc = &Libraries::LibcInternal::internal_calloc, + .heap_realloc = &Libraries::LibcInternal::internal_realloc, + .heap_memalign = nullptr, + .heap_posix_memalign = Libraries::LibcInternal::internal_posix_memalign, + .heap_reallocalign = nullptr, + .heap_malloc_stats = nullptr, + .heap_malloc_usable_size = nullptr}} {} Linker::~Linker() = default;