Files
2026-04-23 22:01:27 +02:00

109 lines
2.6 KiB
C++

#include "platform.h"
#ifdef PLATFORM_PS4
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
extern "C" {
typedef int32_t OrbisKernelModule;
struct OrbisKernelModuleSegmentInfo {
uint64_t address;
uint32_t size;
uint32_t prot; // 1=R, 2=W, 4=X
};
struct OrbisKernelModuleInfo {
uint64_t size;
OrbisKernelModule handle;
char name[256];
OrbisKernelModuleSegmentInfo segmentInfo[4];
uint32_t numSegments;
uint8_t fingerprint[20];
};
int sceKernelGetModuleList(int flag, OrbisKernelModule* pArray, int numMax, int* pNumModules);
int sceKernelGetModuleInfo(OrbisKernelModule handle, OrbisKernelModuleInfo* pInfo);
} // extern "C"
static uintptr_t s_base = 0;
static size_t s_size = 0;
static void CacheModuleBase() {
if (s_base != 0) return;
OrbisKernelModule handles[256];
int count = 0;
if (sceKernelGetModuleList(0, handles, 256, &count) != 0) return;
for (int i = 0; i < count; i++) {
OrbisKernelModuleInfo info;
info.size = sizeof(info);
if (sceKernelGetModuleInfo(handles[i], &info) != 0) continue;
bool isMain = (strstr(info.name, "eboot") != nullptr) || (i == 0);
if (!isMain) continue;
for (uint32_t s = 0; s < info.numSegments && s < 4; s++) {
if (info.segmentInfo[s].prot & 0x04) {
s_base = static_cast<uintptr_t>(info.segmentInfo[s].address);
s_size = static_cast<size_t>(info.segmentInfo[s].size);
return;
}
}
}
}
namespace Platform {
void Sleep(unsigned int seconds) {
for (unsigned int i = 0; i < seconds; i++)
usleep(1000000u);
}
void SleepMs(unsigned int ms) {
usleep(ms * 1000u);
}
void* AllocateExecutableMemory(size_t size) {
#include <sys/mman.h>
void* mem = mmap(nullptr, size,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANON, -1, 0);
return (mem == reinterpret_cast<void*>(-1)) ? nullptr : mem;
}
void FreeExecutableMemory(void* ptr, size_t size) {
#include <sys/mman.h>
if (ptr && ptr != reinterpret_cast<void*>(-1))
munmap(ptr, size);
}
void Log(const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
// sceKernelDebugOutText alternative:
// char buf[512]; vsnprintf(buf, sizeof(buf), fmt, ap);
// sceKernelDebugOutText(0, buf);
}
uintptr_t GetImageBase() {
CacheModuleBase();
return s_base;
}
size_t GetImageSize() {
CacheModuleBase();
return s_size;
}
} // namespace Platform
#endif // PLATFORM_PS4