Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/core/libraries/libc_internal/libc_internal_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,51 @@ 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);
}
Comment on lines +33 to +35
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more ideal to properly reimplement these instead of using host functions.
Most of these boil down to kernel memory functions if I remember right.


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* 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,
Expand All @@ -40,6 +85,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
23 changes: 23 additions & 0 deletions src/core/libraries/libc_internal/libc_internal_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,28 @@ 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* 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
13 changes: 12 additions & 1 deletion src/core/linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -49,7 +50,17 @@ 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 = &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;

Expand Down