rewrote libc_cxa

This commit is contained in:
georgemoralis 2023-10-16 20:49:52 +03:00
parent f916fd48b0
commit 044628ab13
5 changed files with 164 additions and 58 deletions

View file

@ -5,6 +5,7 @@
#include "../Loader/Elf.h"
#include "Emulator/HLE/Libraries/LibC/libc.h"
#include "Emulator/HLE/Libraries/LibC/libc_cxa.h"
#include "ErrorCodes.h"
#include "Libs.h"
@ -17,60 +18,6 @@ static PS4_SYSV_ABI void init_env() // every game/demo should probably
// dummy no need atm
}
static pthread_mutex_t __guard_mutex;
static pthread_once_t __once_control = PTHREAD_ONCE_INIT;
static void recursiveMutex() {
pthread_mutexattr_t recursiveMutexAttr;
pthread_mutexattr_init(&recursiveMutexAttr);
pthread_mutexattr_settype(&recursiveMutexAttr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&__guard_mutex, &recursiveMutexAttr);
}
static pthread_mutex_t* mutex_quard() {
pthread_once(&__once_control, &recursiveMutex);
return &__guard_mutex;
}
int PS4_SYSV_ABI __cxa_guard_acquire(u64* guard_object) {
if ((*((uint8_t*)guard_object) != 0)) // low 8 bits checks if its already init
{
return 0;
}
int result = ::pthread_mutex_lock(mutex_quard());
if (result != 0) {
BREAKPOINT();
}
// Check if another thread has completed initializer run
if ((*((uint8_t*)guard_object) != 0)) { // check again if other thread init it
int result = ::pthread_mutex_unlock(mutex_quard());
if (result != 0) {
BREAKPOINT();
}
return 0;
}
if (((uint8_t*)guard_object)[1] != 0) { // the second lowest byte marks if it's being used by a thread
BREAKPOINT();
}
// mark this guard object as being in use
((uint8_t*)guard_object)[1] = 1;
return 1;
}
void PS4_SYSV_ABI __cxa_guard_release(u64* guard_object) {
*((uint8_t*)guard_object) = 1; // mark it as done
// release global mutex
int result = ::pthread_mutex_unlock(mutex_quard());
if (result != 0) {
BREAKPOINT();
}
}
static PS4_SYSV_ABI void catchReturnFromMain(int status) {
// dummy
}
@ -110,8 +57,9 @@ void PS4_SYSV_ABI qsort(void* ptr, size_t count,size_t size, int(PS4_SYSV_ABI* c
void LibC_Register(SymbolsResolver* sym) {
LIB_FUNCTION("bzQExy189ZI", "libc", 1, "libc", 1, 1, init_env);
LIB_FUNCTION("3GPpjQdAMTw", "libc", 1, "libc", 1, 1, __cxa_guard_acquire);
LIB_FUNCTION("9rAeANT2tyE", "libc", 1, "libc", 1, 1, __cxa_guard_release);
LIB_FUNCTION("3GPpjQdAMTw", "libc", 1, "libc", 1, 1, Emulator::HLE::Libraries::LibC::Cxa::__cxa_guard_acquire);
LIB_FUNCTION("9rAeANT2tyE", "libc", 1, "libc", 1, 1, Emulator::HLE::Libraries::LibC::Cxa::__cxa_guard_release);
LIB_FUNCTION("2emaaluWzUw", "libc", 1, "libc", 1, 1, Emulator::HLE::Libraries::LibC::Cxa::__cxa_guard_abort);
LIB_FUNCTION("DfivPArhucg", "libc", 1, "libc", 1, 1, Emulator::HLE::Libraries::LibC::memcmp);
LIB_FUNCTION("Q3VBxCXhUHs", "libc", 1, "libc", 1, 1, Emulator::HLE::Libraries::LibC::memcpy);
LIB_FUNCTION("8zTFvBIAIN8", "libc", 1, "libc", 1, 1, Emulator::HLE::Libraries::LibC::memset);

View file

@ -9,7 +9,5 @@ namespace HLE::Libs::LibC {
static PS4_SYSV_ABI void init_env();
static PS4_SYSV_ABI void _Assert();
static PS4_SYSV_ABI void catchReturnFromMain(int status);
int PS4_SYSV_ABI __cxa_guard_acquire(u64* guard_object);
void PS4_SYSV_ABI __cxa_guard_release(u64* guard_object);
};