mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
refactor(libc, kernel): drop the libc exports the title takes from its own libc.prx
The C11 thread layer (_Mtx_*, _Cnd_*, _Thrd_*, _Xtime_get_ticks, std::_Pad) and the libc additions _FNan, _FDtest, _FExp, __udivti3, _Throw_Cpp_error, generic_category, localeconv, wcsncpy_s and the aligned operator new came from the --skip-sce-module route, where our libc stood in for the title's. With the title's sce_module loaded, its libc.prx exports every one of these NIDs and the relinker lists the guest modules ahead of the host prx, so the imports never reach our copies. Checked both by the NID tables (the executable imports them from libc, the guest libc.prx exports them) and by making the four std::_Pad functions throw: the title boots, reaches the menu and loads story mode without calling them. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
@@ -97,32 +97,4 @@ void APS5_VABI srand_nid_postfix(unsigned int seed) {
|
||||
g_randState = seed;
|
||||
}
|
||||
|
||||
|
||||
constexpr short FloatDenorm = -2;
|
||||
constexpr short FloatFinite = -1;
|
||||
constexpr short FloatInfinite = 1;
|
||||
constexpr short FloatNan = 2;
|
||||
|
||||
std::uint32_t _FNan_nid_postfix[4] = {0x7FC00000u, 0, 0, 0};
|
||||
|
||||
short APS5_VABI _FDtest_nid_postfix(const float* x) {
|
||||
switch (std::fpclassify(*x)) {
|
||||
case FP_NAN: return FloatNan;
|
||||
case FP_INFINITE: return FloatInfinite;
|
||||
case FP_ZERO: return 0;
|
||||
case FP_SUBNORMAL: return FloatDenorm;
|
||||
default: return FloatFinite;
|
||||
}
|
||||
}
|
||||
|
||||
short APS5_VABI _FExp_nid_postfix(float* x, float y, short eoff) {
|
||||
*x = static_cast<float>(std::ldexp(static_cast<double>(y) * std::exp(static_cast<double>(*x)), eoff));
|
||||
if (*x == 0.0f) return 0;
|
||||
return std::isinf(*x) ? FloatInfinite : FloatFinite;
|
||||
}
|
||||
|
||||
unsigned __int128 APS5_VABI __udivti3_nid_postfix(unsigned __int128 dividend, unsigned __int128 divisor) {
|
||||
return dividend / divisor;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -100,15 +100,6 @@ unsigned int APS5_VABI _ZSt14_Random_devicev_nid_postfix() {
|
||||
throw std::runtime_error("_Throw_C_error: C11 thread error " + std::to_string(code));
|
||||
}
|
||||
|
||||
[[noreturn]] void APS5_VABI _ZSt16_Throw_Cpp_errori_nid_postfix(int code) {
|
||||
throw std::runtime_error("_Throw_Cpp_error: C++ thread error " + std::to_string(code));
|
||||
}
|
||||
|
||||
const void* APS5_VABI _ZSt16generic_categoryv_nid_postfix() {
|
||||
NotImplemented_nid_no_patch(__func__);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void APS5_VABI _Lockfilelock_nid_postfix(FileStream* stream) {
|
||||
if (stream == nullptr) throw std::invalid_argument("_Lockfilelock: null stream");
|
||||
#ifdef _WIN32
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
#include <climits>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
@@ -51,26 +50,6 @@ int APS5_VABI sprintf_s_nid_postfix(char* buffer, size_t size, const char* forma
|
||||
std::abort();
|
||||
}
|
||||
|
||||
struct GuestLconv {
|
||||
const char* monetary[7];
|
||||
char monetaryFlags[14];
|
||||
const char* decimalPoint;
|
||||
const char* numeric[4];
|
||||
const char* falseName;
|
||||
const char* trueName;
|
||||
const char* no;
|
||||
const char* yes;
|
||||
};
|
||||
static_assert(sizeof(GuestLconv) == 0x90);
|
||||
|
||||
const GuestLconv* APS5_VABI localeconv_nid_postfix() {
|
||||
static const GuestLconv lconv{
|
||||
{"", "", "", "", "", "", ""},
|
||||
{CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX, CHAR_MAX},
|
||||
".", {"", "", "", ""}, "false", "true", "", ""};
|
||||
return &lconv;
|
||||
}
|
||||
|
||||
// Only the "C" locale exists.
|
||||
const char* APS5_VABI setlocale_nid_postfix(int category, const char* locale) {
|
||||
(void)category;
|
||||
@@ -122,10 +101,6 @@ void APS5_VABI _ZdaPvm_nid_postfix(void* pointer, std::size_t) {
|
||||
if (pointer != nullptr) ApplicationHeapFree_nid_no_patch(pointer);
|
||||
}
|
||||
|
||||
void* APS5_VABI _ZnwmSt11align_val_t_nid_postfix(std::size_t size, std::size_t alignment) {
|
||||
return GuestHeap::GuestHeapAlign_nid_postfix(alignment, size);
|
||||
}
|
||||
|
||||
void APS5_VABI _ZdlPvSt11align_val_t_nid_postfix(void* pointer, std::size_t alignment) {
|
||||
(void)alignment;
|
||||
GuestHeap::GuestHeapFree_nid_postfix(pointer);
|
||||
|
||||
@@ -188,25 +188,6 @@ int APS5_VABI strncpy_s_nid_postfix(char* dest, size_t destsz, const char* src,
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI wcsncpy_s_nid_postfix(wchar_t* dest, size_t destsz, const wchar_t* src, size_t count) {
|
||||
constexpr int GuestEinval = 22;
|
||||
constexpr int GuestErange = 34;
|
||||
if (!dest || destsz == 0) return GuestEinval;
|
||||
if (!src) {
|
||||
dest[0] = L'\0';
|
||||
return GuestEinval;
|
||||
}
|
||||
size_t length = 0;
|
||||
while (length < count && src[length] != L'\0') ++length;
|
||||
if (length >= destsz) {
|
||||
dest[0] = L'\0';
|
||||
return GuestErange;
|
||||
}
|
||||
std::wmemcpy(dest, src, length);
|
||||
dest[length] = L'\0';
|
||||
return 0;
|
||||
}
|
||||
|
||||
int APS5_VABI strcpy_s_nid_postfix(char* dest, size_t destsz, const char* src) {
|
||||
return strncpy_s_nid_postfix(dest, destsz, src, static_cast<size_t>(-1));
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Once.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Tsd.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Sem.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Xthreads.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Socket/src/Socket.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Socket/src/Udp.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/Socket/src/AddressConversion.cpp
|
||||
|
||||
@@ -1,151 +0,0 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include "SceTypes.hpp"
|
||||
#include "prx/libc/include/General.hpp"
|
||||
#include "../include/Pthread.hpp"
|
||||
#include "../include/Mutex.hpp"
|
||||
#include "../include/Cond.hpp"
|
||||
#include "../../Time/include/Time.hpp"
|
||||
#include "Common.hpp"
|
||||
|
||||
extern "C" {
|
||||
int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name);
|
||||
int APS5_VABI scePthreadJoin(Pthread thread, void** retval);
|
||||
Pthread APS5_VABI scePthreadSelf();
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int ThrdSuccess = 0;
|
||||
constexpr int ThrdNomem = 1;
|
||||
constexpr int ThrdTimedout = 2;
|
||||
constexpr int ThrdBusy = 3;
|
||||
constexpr int ThrdError = 4;
|
||||
constexpr int MtxPlain = 0x1;
|
||||
constexpr int MtxRecursive = 0x100;
|
||||
constexpr int SceMutexRecursive = 2;
|
||||
constexpr int SceEdeadlk = static_cast<int>(0x8002000Bu);
|
||||
constexpr int SceEnomem = static_cast<int>(0x8002000Cu);
|
||||
constexpr int SceEtimedout = static_cast<int>(0x8002003Cu);
|
||||
constexpr int GuestRealtimeClock = 0;
|
||||
|
||||
int initResult(int result) {
|
||||
return result == 0 ? ThrdSuccess : result == SceEnomem ? ThrdNomem : ThrdError;
|
||||
}
|
||||
|
||||
struct GuestPad {
|
||||
unsigned (APS5_VABI* const* vtable)(GuestPad*);
|
||||
PthreadCond cond;
|
||||
PthreadMutex mutex;
|
||||
bool started;
|
||||
};
|
||||
static_assert(offsetof(GuestPad, cond) == 0x8 && offsetof(GuestPad, mutex) == 0x10 && offsetof(GuestPad, started) == 0x18);
|
||||
|
||||
void* APS5_VABI padEntry(void* arg) {
|
||||
auto* pad = static_cast<GuestPad*>(arg);
|
||||
pad->vtable[0](pad);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void require(int result, const char* what) {
|
||||
if (result != ThrdSuccess) throw std::runtime_error(what);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
int APS5_VABI _Mtx_init_nid_postfix(PthreadMutex* mutex, int type) {
|
||||
*mutex = nullptr;
|
||||
PthreadMutexattr attr{};
|
||||
const int attrResult = scePthreadMutexattrInit(&attr);
|
||||
if (attrResult != 0) return initResult(attrResult);
|
||||
if ((type & MtxRecursive) != 0 && scePthreadMutexattrSettype(&attr, SceMutexRecursive) != 0) {
|
||||
scePthreadMutexattrDestroy(&attr);
|
||||
return ThrdError;
|
||||
}
|
||||
const int result = scePthreadMutexInit(mutex, &attr, nullptr);
|
||||
scePthreadMutexattrDestroy(&attr);
|
||||
return initResult(result);
|
||||
}
|
||||
|
||||
void APS5_VABI _Mtx_destroy_nid_postfix(PthreadMutex* mutex) {
|
||||
scePthreadMutexDestroy(mutex);
|
||||
}
|
||||
|
||||
int APS5_VABI _Mtx_lock_nid_postfix(PthreadMutex* mutex) {
|
||||
const int result = scePthreadMutexLock(mutex);
|
||||
return result == 0 ? ThrdSuccess : result == SceEdeadlk ? ThrdBusy : ThrdError;
|
||||
}
|
||||
|
||||
int APS5_VABI _Mtx_unlock_nid_postfix(PthreadMutex* mutex) {
|
||||
scePthreadMutexUnlock(mutex);
|
||||
return ThrdSuccess;
|
||||
}
|
||||
|
||||
int APS5_VABI _Cnd_init_nid_postfix(PthreadCond* cond) {
|
||||
return initResult(scePthreadCondInit(cond, nullptr, nullptr));
|
||||
}
|
||||
|
||||
void APS5_VABI _Cnd_destroy_nid_postfix(PthreadCond* cond) {
|
||||
scePthreadCondDestroy(cond);
|
||||
}
|
||||
|
||||
int APS5_VABI _Cnd_broadcast_nid_postfix(PthreadCond* cond) {
|
||||
scePthreadCondBroadcast(cond);
|
||||
return ThrdSuccess;
|
||||
}
|
||||
|
||||
int APS5_VABI _Cnd_timedwait_nid_postfix(PthreadCond* cond, PthreadMutex* mutex, const KernelTimespec* deadline) {
|
||||
KernelUseconds usec = 0;
|
||||
if (!PosixThread::RelativeMicroseconds(GuestRealtimeClock, deadline, &usec)) throw std::runtime_error("_Cnd_timedwait: invalid xtime");
|
||||
const int result = scePthreadCondTimedwait(cond, mutex, usec);
|
||||
return result == 0 ? ThrdSuccess : result == SceEtimedout ? ThrdTimedout : ThrdError;
|
||||
}
|
||||
|
||||
int APS5_VABI _Thrd_join_nid_postfix(Pthread thread, int* result) {
|
||||
void* value = nullptr;
|
||||
if (scePthreadJoin(thread, &value) != 0) return ThrdError;
|
||||
if (result) *result = static_cast<int>(reinterpret_cast<std::intptr_t>(value));
|
||||
return ThrdSuccess;
|
||||
}
|
||||
|
||||
Pthread APS5_VABI _Thrd_id_nid_postfix() {
|
||||
return scePthreadSelf();
|
||||
}
|
||||
|
||||
std::int64_t APS5_VABI _Xtime_get_ticks_nid_postfix() {
|
||||
KernelTimeval now{};
|
||||
gettimeofday_nid_postfix(&now, nullptr);
|
||||
return now.tv_sec * 1000000 + now.tv_usec;
|
||||
}
|
||||
|
||||
void APS5_VABI _ZNSt4_PadC2Ev_nid_postfix(GuestPad* pad) {
|
||||
require(_Cnd_init_nid_postfix(&pad->cond), "std::_Pad: condition variable init failed");
|
||||
require(_Mtx_init_nid_postfix(&pad->mutex, MtxPlain), "std::_Pad: mutex init failed");
|
||||
pad->started = false;
|
||||
require(_Mtx_lock_nid_postfix(&pad->mutex), "std::_Pad: mutex lock failed");
|
||||
}
|
||||
|
||||
void APS5_VABI _ZNSt4_PadD2Ev_nid_postfix(GuestPad* pad) {
|
||||
_Mtx_unlock_nid_postfix(&pad->mutex);
|
||||
_Mtx_destroy_nid_postfix(&pad->mutex);
|
||||
_Cnd_destroy_nid_postfix(&pad->cond);
|
||||
}
|
||||
|
||||
void APS5_VABI _ZNSt4_Pad7_LaunchEPP7pthread_nid_postfix(GuestPad* pad, Pthread* thread) {
|
||||
if (scePthreadCreate(thread, nullptr, padEntry, pad, nullptr) != 0) throw std::runtime_error("std::_Pad: thread start failed");
|
||||
while (!pad->started) {
|
||||
if (scePthreadCondWait(&pad->cond, &pad->mutex) != 0) throw std::runtime_error("std::_Pad: launch wait failed");
|
||||
}
|
||||
}
|
||||
|
||||
void APS5_VABI _ZNSt4_Pad8_ReleaseEv_nid_postfix(GuestPad* pad) {
|
||||
require(_Mtx_lock_nid_postfix(&pad->mutex), "std::_Pad: mutex lock failed");
|
||||
pad->started = true;
|
||||
scePthreadCondSignal(&pad->cond);
|
||||
_Mtx_unlock_nid_postfix(&pad->mutex);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user