refactor(libc): separate platform and compiler specifics in libc

This commit is contained in:
boykopovar
2026-09-06 12:41:33 +03:00
parent 7e61d94b30
commit 1752e1d78e
20 changed files with 162 additions and 86 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
# About
Converter for native execution of PlayStation 5 ELF binaries on Linux through binary format conversion and ABI compatibility. The implementation uses only the C++20 standard library and performs deterministic binary transformation without heuristics or interpretation.
Converter for native execution of PlayStation 5 ELF binaries on Linux through binary format conversion and ABI compatibility. The relinker implementation uses only the C++20 standard library and performs deterministic binary transformation without heuristics or interpretation.
## Status
+2
View File
@@ -2,6 +2,8 @@
#include <cstddef>
#include "SceTypes.hpp"
#include "src/specifics/gcc/StackGuard.cpp"
#include "src/specifics/linux/DlIteratePhdr.cpp"
#include "src/Strings.cpp"
#include "src/Math.cpp"
#include "src/Time.cpp"
+4 -3
View File
@@ -4,12 +4,13 @@
#include <cstdarg>
#include <cstdint>
#include "SceTypes.hpp"
#include "prx/libc/include/specifics/x86_64/SimdTypes.hpp"
namespace LibcDetail {
struct RegSaveArea {
std::uint64_t gp[6];
__m128 fp[8];
X86_64::Xmm fp[8];
};
struct VaListLayout {
@@ -23,8 +24,8 @@ inline void FillRegSaveArea(
RegSaveArea& regs,
std::uint64_t gp0, std::uint64_t gp1, std::uint64_t gp2,
std::uint64_t gp3, std::uint64_t gp4, std::uint64_t gp5,
__m128 fp0, __m128 fp1, __m128 fp2, __m128 fp3,
__m128 fp4, __m128 fp5, __m128 fp6, __m128 fp7
X86_64::Xmm fp0, X86_64::Xmm fp1, X86_64::Xmm fp2, X86_64::Xmm fp3,
X86_64::Xmm fp4, X86_64::Xmm fp5, X86_64::Xmm fp6, X86_64::Xmm fp7
) {
regs.gp[0] = gp0;
regs.gp[1] = gp1;
@@ -1,10 +1,10 @@
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_EXCEPTIONS_RUNTIME_HPP
#define CORE_LIBS_PRX_LIBC_INCLUDE_EXCEPTIONS_RUNTIME_HPP
#include "Unwind.hpp"
#include "prx/libc/include/exceptions/Unwind.hpp"
#include <atomic>
#include <typeinfo>
#include <cxxabi.h>
#include "prx/libc/include/specifics/itanium/CxxAbi.hpp"
namespace LibcException {
struct Header {
@@ -5,7 +5,7 @@
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <unwind.h>
#include "prx/libc/include/specifics/itanium/UnwindAbi.hpp"
struct _Unwind_Context {
std::uintptr_t registers[17] {};
@@ -0,0 +1,12 @@
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_GCC_ATOMICOPS_HPP
#define CORE_LIBS_PRX_LIBC_INCLUDE_GCC_ATOMICOPS_HPP
inline unsigned int GccAtomicFetchAdd(volatile unsigned int* target, unsigned int value) {
return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST);
}
inline unsigned int GccAtomicFetchSub(volatile unsigned int* target, unsigned int value) {
return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST);
}
#endif
@@ -0,0 +1,9 @@
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_GCC_SYMBOLALIAS_HPP
#define CORE_LIBS_PRX_LIBC_INCLUDE_GCC_SYMBOLALIAS_HPP
#define GCC_LOCAL_ALIAS(alias, target) \
asm(".local " #alias "\n.set " #alias "," #target "\n")
#define GCC_HIDDEN_FN __attribute__((visibility("hidden")))
#endif
@@ -0,0 +1,6 @@
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_ITANIUM_CXXABI_HPP
#define CORE_LIBS_PRX_LIBC_INCLUDE_ITANIUM_CXXABI_HPP
#include <cxxabi.h>
#endif
@@ -0,0 +1,6 @@
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_ITANIUM_UNWINDABI_HPP
#define CORE_LIBS_PRX_LIBC_INCLUDE_ITANIUM_UNWINDABI_HPP
#include <unwind.h>
#endif
@@ -0,0 +1,10 @@
#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_X86_64_SIMDTYPES_HPP
#define CORE_LIBS_PRX_LIBC_INCLUDE_X86_64_SIMDTYPES_HPP
#include <xmmintrin.h>
namespace X86_64 {
using Xmm = __m128;
}
#endif
+1 -19
View File
@@ -4,8 +4,6 @@
#include <cerrno>
#include "SceTypes.hpp"
std::uintptr_t __stack_chk_guard = 0xDEADBEEFCAFEBABEull;
extern "C" {
void exit_nid_postfix(int code) {
@@ -39,22 +37,6 @@ int atexit_nid_postfix(atexit_func_t func) {
return std::atexit(func);
}
int setenv_nid_postfix(const char* name, const char* value, int overwrite) {
if (overwrite == 0 && std::getenv(name) != nullptr) {
return 0;
}
const size_t nameLength = std::strlen(name);
const size_t valueLength = std::strlen(value);
char* entry = static_cast<char*>(std::malloc(nameLength + valueLength + 2));
if (entry == nullptr) {
errno = ENOMEM;
return -1;
}
std::memcpy(entry, name, nameLength);
entry[nameLength] = '=';
std::memcpy(entry + nameLength + 1, value, valueLength);
entry[nameLength + 1 + valueLength] = '\0';
return ::putenv(entry);
}
}
#include "prx/libc/src/specifics/linux/PosixProcess.cpp"
+3 -2
View File
@@ -6,6 +6,7 @@
#include <mutex>
#include <vector>
#include <utility>
#include "prx/libc/include/specifics/gcc/AtomicOps.hpp"
namespace {
@@ -41,12 +42,12 @@ int __cxa_atexit_nid_postfix(void (*func)(void*), void* arg, void* dsoHandle) {
unsigned int _Atomic_fetch_add_4_nid_postfix(volatile unsigned int* target, unsigned int value, int memoryOrder) {
(void)memoryOrder;
return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST);
return GccAtomicFetchAdd(target, value);
}
unsigned int _Atomic_fetch_sub_4_nid_postfix(volatile unsigned int* target, unsigned int value, int memoryOrder) {
(void)memoryOrder;
return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST);
return GccAtomicFetchSub(target, value);
}
unsigned long _Stoul_nid_postfix(const char* str, char** endptr, int base) {
+2 -13
View File
@@ -3,7 +3,6 @@
#include <cstdlib>
#include <cwchar>
#include <stdexcept>
#include <strings.h>
extern "C" {
@@ -36,14 +35,6 @@ int strncmp_nid_postfix(const char* s1, const char* s2, size_t n) {
return std::strncmp(s1, s2, n);
}
int strcasecmp_nid_postfix(const char* s1, const char* s2) {
return ::strcasecmp(s1, s2);
}
int strncasecmp_nid_postfix(const char* s1, const char* s2, size_t n) {
return ::strncasecmp(s1, s2, n);
}
size_t strlen_nid_postfix(const char* s) {
return std::strlen(s);
}
@@ -72,10 +63,6 @@ char* strstr_nid_postfix(const char* haystack, const char* needle) {
return std::strstr(const_cast<char*>(haystack), needle);
}
char* strdup_nid_postfix(const char* s) {
return ::strdup(s);
}
size_t strlcpy_nid_postfix(char* dest, const char* src, size_t size) {
const size_t srcLen = std::strlen(src);
if (size != 0u) {
@@ -127,3 +114,5 @@ wchar_t* wmemmove_nid_postfix(wchar_t* dest, const wchar_t* src, size_t n) {
}
}
#include "prx/libc/src/specifics/linux/PosixStrings.cpp"
+14 -14
View File
@@ -5,18 +5,18 @@
#include "GuardSupport.cpp"
#include "Personality.cpp"
extern "C" [[noreturn]] __attribute__((visibility("hidden"))) void LibcRuntimeAssertion(const char*, int, const char*, const char*) { std::abort(); }
#include "prx/libc/include/specifics/gcc/SymbolAlias.hpp"
asm(
".local _ZSt21__glibcxx_assert_failPKciS0_S0_\n.set _ZSt21__glibcxx_assert_failPKciS0_S0_,LibcRuntimeAssertion\n"
".local __cxa_allocate_exception\n.set __cxa_allocate_exception,__cxa_allocate_exception_nid_postfix\n"
".local __cxa_free_exception\n.set __cxa_free_exception,__cxa_free_exception_nid_postfix\n"
".local __cxa_throw\n.set __cxa_throw,__cxa_throw_nid_postfix\n"
".local __cxa_begin_catch\n.set __cxa_begin_catch,__cxa_begin_catch_nid_postfix\n"
".local __cxa_end_catch\n.set __cxa_end_catch,__cxa_end_catch_nid_postfix\n"
".local __cxa_rethrow\n.set __cxa_rethrow,__cxa_rethrow_nid_postfix\n"
".local __gxx_personality_v0\n.set __gxx_personality_v0,__gxx_personality_v0_nid_postfix\n"
".local _Unwind_Resume\n.set _Unwind_Resume,_Unwind_Resume_nid_postfix\n"
".local _ZSt9terminatev\n.set _ZSt9terminatev,_ZSt9terminatev_nid_postfix\n"
".local __cxa_call_terminate\n.set __cxa_call_terminate,__cxa_call_terminate_nid_postfix\n"
);
extern "C" [[noreturn]] GCC_HIDDEN_FN void LibcRuntimeAssertion(const char*, int, const char*, const char*) { std::abort(); }
GCC_LOCAL_ALIAS(_ZSt21__glibcxx_assert_failPKciS0_S0_, LibcRuntimeAssertion);
GCC_LOCAL_ALIAS(__cxa_allocate_exception, __cxa_allocate_exception_nid_postfix);
GCC_LOCAL_ALIAS(__cxa_free_exception, __cxa_free_exception_nid_postfix);
GCC_LOCAL_ALIAS(__cxa_throw, __cxa_throw_nid_postfix);
GCC_LOCAL_ALIAS(__cxa_begin_catch, __cxa_begin_catch_nid_postfix);
GCC_LOCAL_ALIAS(__cxa_end_catch, __cxa_end_catch_nid_postfix);
GCC_LOCAL_ALIAS(__cxa_rethrow, __cxa_rethrow_nid_postfix);
GCC_LOCAL_ALIAS(__gxx_personality_v0, __gxx_personality_v0_nid_postfix);
GCC_LOCAL_ALIAS(_Unwind_Resume, _Unwind_Resume_nid_postfix);
GCC_LOCAL_ALIAS(_ZSt9terminatev, _ZSt9terminatev_nid_postfix);
GCC_LOCAL_ALIAS(__cxa_call_terminate, __cxa_call_terminate_nid_postfix);
+1 -31
View File
@@ -1,35 +1,7 @@
#include "prx/libc/include/exceptions/Unwind.hpp"
#include "prx/libc/src/specifics/x86_64/RegisterContext.cpp"
#include <link.h>
#if !defined(__linux__) || !defined(__x86_64__)
#error Unsupported unwind platform
#endif
extern "C" void LibcCaptureRegisters(std::uintptr_t*) __attribute__((visibility("hidden")));
extern "C" [[noreturn]] void LibcRestoreRegisters(const std::uintptr_t*) __attribute__((visibility("hidden")));
asm(
".text\n"
".hidden LibcCaptureRegisters\n"
".type LibcCaptureRegisters,@function\n"
"LibcCaptureRegisters:\n"
"movq %rax,0(%rdi)\nmovq %rdx,8(%rdi)\nmovq %rcx,16(%rdi)\nmovq %rbx,24(%rdi)\n"
"movq %rsi,32(%rdi)\nmovq %rdi,40(%rdi)\nmovq %rbp,48(%rdi)\n"
"leaq 8(%rsp),%rax\nmovq %rax,56(%rdi)\n"
"movq %r8,64(%rdi)\nmovq %r9,72(%rdi)\nmovq %r10,80(%rdi)\nmovq %r11,88(%rdi)\n"
"movq %r12,96(%rdi)\nmovq %r13,104(%rdi)\nmovq %r14,112(%rdi)\nmovq %r15,120(%rdi)\n"
"movq (%rsp),%rax\nmovq %rax,128(%rdi)\nret\n"
".size LibcCaptureRegisters,.-LibcCaptureRegisters\n"
".hidden LibcRestoreRegisters\n.type LibcRestoreRegisters,@function\n"
"LibcRestoreRegisters:\n"
"movq %rdi,%r10\nmovq 128(%r10),%r11\n"
"movq 0(%r10),%rax\nmovq 8(%r10),%rdx\nmovq 16(%r10),%rcx\nmovq 24(%r10),%rbx\n"
"movq 32(%r10),%rsi\nmovq 40(%r10),%rdi\nmovq 48(%r10),%rbp\nmovq 56(%r10),%rsp\n"
"movq 64(%r10),%r8\nmovq 72(%r10),%r9\nmovq 96(%r10),%r12\n"
"movq 104(%r10),%r13\nmovq 112(%r10),%r14\nmovq 120(%r10),%r15\njmp *%r11\n"
".size LibcRestoreRegisters,.-LibcRestoreRegisters\n"
);
namespace LibcUnwind {
struct Lookup { Word pc; const Byte* fde {}; Word text {}; Word data {}; };
@@ -402,6 +374,4 @@ _Unwind_Reason_Code _Unwind_Backtrace_nid_postfix(_Unwind_Trace_Fn trace, void*
}
return _URC_FATAL_PHASE1_ERROR;
}
int dl_iterate_phdr_nid_postfix(int (*callback)(dl_phdr_info*, std::size_t, void*), void* data) { return dl_iterate_phdr(callback, data); }
}
@@ -0,0 +1,4 @@
#include <cstdint>
#include <cstdlib>
std::uintptr_t __stack_chk_guard = 0xDEADBEEFCAFEBABEull;
@@ -0,0 +1,10 @@
#include <link.h>
#include <cstddef>
extern "C" {
int dl_iterate_phdr_nid_postfix(int (*callback)(dl_phdr_info*, std::size_t, void*), void* data) {
return dl_iterate_phdr(callback, data);
}
}
@@ -0,0 +1,25 @@
#include <cstdlib>
#include <cerrno>
#include <cstring>
extern "C" {
int setenv_nid_postfix(const char* name, const char* value, int overwrite) {
if (overwrite == 0 && std::getenv(name) != nullptr) {
return 0;
}
const size_t nameLength = std::strlen(name);
const size_t valueLength = std::strlen(value);
char* entry = static_cast<char*>(std::malloc(nameLength + valueLength + 2));
if (entry == nullptr) {
errno = ENOMEM;
return -1;
}
std::memcpy(entry, name, nameLength);
entry[nameLength] = '=';
std::memcpy(entry + nameLength + 1, value, valueLength);
entry[nameLength + 1 + valueLength] = '\0';
return ::putenv(entry);
}
}
@@ -0,0 +1,19 @@
#include <strings.h>
#include <cstdlib>
#include <cstring>
extern "C" {
int strcasecmp_nid_postfix(const char* s1, const char* s2) {
return ::strcasecmp(s1, s2);
}
int strncasecmp_nid_postfix(const char* s1, const char* s2, size_t n) {
return ::strncasecmp(s1, s2, n);
}
char* strdup_nid_postfix(const char* s) {
return ::strdup(s);
}
}
@@ -0,0 +1,30 @@
#if !defined(__linux__) || !defined(__x86_64__)
#error "This file is x86_64 Linux only"
#endif
#include <cstdint>
extern "C" void LibcCaptureRegisters(std::uintptr_t*) __attribute__((visibility("hidden")));
extern "C" [[noreturn]] void LibcRestoreRegisters(const std::uintptr_t*) __attribute__((visibility("hidden")));
asm(
".text\n"
".hidden LibcCaptureRegisters\n"
".type LibcCaptureRegisters,@function\n"
"LibcCaptureRegisters:\n"
"movq %rax,0(%rdi)\nmovq %rdx,8(%rdi)\nmovq %rcx,16(%rdi)\nmovq %rbx,24(%rdi)\n"
"movq %rsi,32(%rdi)\nmovq %rdi,40(%rdi)\nmovq %rbp,48(%rdi)\n"
"leaq 8(%rsp),%rax\nmovq %rax,56(%rdi)\n"
"movq %r8,64(%rdi)\nmovq %r9,72(%rdi)\nmovq %r10,80(%rdi)\nmovq %r11,88(%rdi)\n"
"movq %r12,96(%rdi)\nmovq %r13,104(%rdi)\nmovq %r14,112(%rdi)\nmovq %r15,120(%rdi)\n"
"movq (%rsp),%rax\nmovq %rax,128(%rdi)\nret\n"
".size LibcCaptureRegisters,.-LibcCaptureRegisters\n"
".hidden LibcRestoreRegisters\n.type LibcRestoreRegisters,@function\n"
"LibcRestoreRegisters:\n"
"movq %rdi,%r10\nmovq 128(%r10),%r11\n"
"movq 0(%r10),%rax\nmovq 8(%r10),%rdx\nmovq 16(%r10),%rcx\nmovq 24(%r10),%rbx\n"
"movq 32(%r10),%rsi\nmovq 40(%r10),%rdi\nmovq 48(%r10),%rbp\nmovq 56(%r10),%rsp\n"
"movq 64(%r10),%r8\nmovq 72(%r10),%r9\nmovq 96(%r10),%r12\n"
"movq 104(%r10),%r13\nmovq 112(%r10),%r14\nmovq 120(%r10),%r15\njmp *%r11\n"
".size LibcRestoreRegisters,.-LibcRestoreRegisters\n"
);