Merge pull request #7 from Pacuka/fix/linux-runtime

fix: Linux build and runtime fixes found while booting a retail UE title
This commit is contained in:
boykopovar
2026-09-26 13:04:53 +03:00
committed by GitHub
10 changed files with 69 additions and 15 deletions
+3
View File
@@ -28,7 +28,10 @@ set(SDL_HAPTIC OFF CACHE BOOL "" FORCE)
set(SDL_HIDAPI OFF CACHE BOOL "" FORCE)
set(SDL_SENSOR OFF CACHE BOOL "" FORCE)
set(SDL_STATIC_PIC ON CACHE BOOL "" FORCE)
set(ANYPS5_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "")
add_subdirectory(3rdparty/SDL2)
set(CMAKE_EXE_LINKER_FLAGS "${ANYPS5_EXE_LINKER_FLAGS}")
add_subdirectory(core/shader/recompiler)
add_subdirectory(core/libs)
+5
View File
@@ -1,3 +1,8 @@
if(NOT WIN32)
string(REPLACE "-static " "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ")
string(STRIP "${CMAKE_EXE_LINKER_FLAGS}" CMAKE_EXE_LINKER_FLAGS)
endif()
add_executable(nid_patcher
${CMAKE_CURRENT_SOURCE_DIR}/nid/src/Sha1.cpp
${CMAKE_CURRENT_SOURCE_DIR}/nid/src/NidCompute.cpp
+1 -1
View File
@@ -38,7 +38,7 @@ std::uint32_t* APS5_VABI sceAgcCbReleaseMem(CommandBuffer* buf, std::uint8_t act
Agc::Command::Require(data == 0, __func__, "GDS release cannot use immediate data");
value = gdsOffset | (static_cast<std::uint64_t>(gdsSize) << 16u);
} else {
Agc::Command::Require(gdsOffset == 0 && gdsSize == 0, __func__, "GDS parameters supplied for a non-GDS release");
Agc::Command::Require(gdsOffset == 0 && gdsSize <= 1, __func__, "GDS parameters supplied for a non-GDS release");
}
if (dataSelect != 0 && interrupt != 4) {
Agc::Command::CheckAddress(guestAddress, dataSelect == 2 || dataSelect == 3 ? 8 : 4, __func__);
@@ -29,7 +29,7 @@ private:
void removeSubclass() noexcept;
void applyAspectRatio(void* hwnd, std::uintptr_t edge, void* rect) const;
static std::intptr_t __stdcall windowProc(void* hwnd, unsigned int message, std::uintptr_t wParam, std::intptr_t lParam, std::uintptr_t subclassId, std::uintptr_t referenceData);
static std::intptr_t windowProc(void* hwnd, unsigned int message, std::uintptr_t wParam, std::intptr_t lParam, std::uintptr_t subclassId, std::uintptr_t referenceData);
SDL_Window* window = nullptr;
std::uint32_t aspectWidth = 0;
@@ -158,7 +158,7 @@ void DisplayWindow::applyAspectRatio(void* hwnd, std::uintptr_t edge, void* rect
#endif
}
std::intptr_t __stdcall DisplayWindow::windowProc(void* hwnd, unsigned int message, std::uintptr_t wParam, std::intptr_t lParam, std::uintptr_t subclassId, std::uintptr_t referenceData) {
std::intptr_t DisplayWindow::windowProc(void* hwnd, unsigned int message, std::uintptr_t wParam, std::intptr_t lParam, std::uintptr_t subclassId, std::uintptr_t referenceData) {
#ifdef _WIN32
static_cast<void>(subclassId);
if (message == WM_SIZING) {
@@ -24,9 +24,7 @@ using Lease = std::vector<std::shared_ptr<const Range>>;
extern "C" {
void* GuestAllocationsBegin_nid_postfix();
#ifdef _WIN32
void GuestAllocationsRegisterMainImage_nid_postfix(void* mutation);
#endif
void GuestAllocationsEnd_nid_postfix(void* mutation) noexcept;
void GuestAllocationsAdd_nid_postfix(void* mutation, void* pointer, std::size_t bytes, bool readable, bool writable);
void GuestAllocationsRequireUnpinned_nid_postfix(void* mutation, const void* pointer, std::size_t bytes);
@@ -44,9 +42,7 @@ public:
~Mutation() { GuestAllocationsEnd_nid_postfix(handle); }
Mutation(const Mutation&) = delete;
Mutation& operator=(const Mutation&) = delete;
#ifdef _WIN32
void RegisterMainImage() { GuestAllocationsRegisterMainImage_nid_postfix(handle); }
#endif
void Add(void* pointer, std::size_t bytes, bool readable, bool writable) { GuestAllocationsAdd_nid_postfix(handle, pointer, bytes, readable, writable); }
void RequireUnpinned(const void* pointer, std::size_t bytes) const { GuestAllocationsRequireUnpinned_nid_postfix(handle, pointer, bytes); }
void RequireAvailable(const void* pointer, std::size_t bytes) const { GuestAllocationsRequireAvailable_nid_postfix(handle, pointer, bytes); }
+2 -2
View File
@@ -142,7 +142,7 @@ void* ApplicationHeapReallocate_nid_no_patch(void* pointer, std::size_t bytes) {
ApplicationHeapFree_nid_no_patch(pointer);
return nullptr;
}
const auto reallocate = callback<Reallocate>(2);
const auto reallocate = callback<Reallocate>(3);
CallbackScope scope;
return requireAllocation(reallocate(pointer, bytes));
}
@@ -158,7 +158,7 @@ void* ApplicationHeapAlign_nid_no_patch(std::size_t alignment, std::size_t bytes
void* ApplicationHeapCalloc_nid_no_patch(std::size_t count, std::size_t bytes) {
if (bytes != 0 && count > std::numeric_limits<std::size_t>::max() / bytes) throw std::length_error("application heap: calloc size overflow");
const auto calloc = callback<Calloc>(3);
const auto calloc = callback<Calloc>(2);
CallbackScope scope;
return requireAllocation(calloc(count, bytes));
}
@@ -9,6 +9,9 @@
#define NOMINMAX
#endif
#include <windows.h>
#else
#include <link.h>
#include <unistd.h>
#endif
namespace GuestAllocations {
@@ -75,6 +78,51 @@ void GuestAllocationsRegisterMainImage_nid_postfix(void*) {
state.ranges.swap(replacement);
state.mainImageRegistered = true;
}
#else
void GuestAllocationsRegisterMainImage_nid_postfix(void*) {
auto& state = registry();
if (state.mainImageRegistered) return;
struct Page {
bool readable = false;
bool writable = false;
};
std::map<std::uint64_t, Page> pages;
const auto pageSize = static_cast<std::uint64_t>(::sysconf(_SC_PAGESIZE));
std::pair<std::map<std::uint64_t, Page>*, std::uint64_t> collection{&pages, pageSize};
dl_iterate_phdr([](dl_phdr_info* image, std::size_t, void* data) {
auto& collected = *static_cast<std::pair<std::map<std::uint64_t, Page>*, std::uint64_t>*>(data);
for (int index = 0; index < image->dlpi_phnum; ++index) {
const auto& header = image->dlpi_phdr[index];
if (header.p_type != PT_LOAD || header.p_memsz == 0) continue;
const auto start = (image->dlpi_addr + header.p_vaddr) & ~(collected.second - 1);
const auto end = (image->dlpi_addr + header.p_vaddr + header.p_memsz + collected.second - 1) & ~(collected.second - 1);
for (auto page = start; page < end; page += collected.second) {
auto& entry = (*collected.first)[page];
entry.readable = entry.readable || (header.p_flags & (PF_R | PF_W)) != 0;
entry.writable = entry.writable || (header.p_flags & PF_W) != 0;
}
}
return 1;
}, &collection);
require(!pages.empty(), "main guest image has no loadable segments");
auto replacement = state.ranges;
for (auto page = pages.begin(); page != pages.end();) {
auto last = page;
while (std::next(last) != pages.end() && std::next(last)->first == last->first + pageSize && std::next(last)->second.readable == page->second.readable && std::next(last)->second.writable == page->second.writable) ++last;
const auto address = page->first;
const auto bytes = static_cast<std::size_t>(last->first + pageSize - address);
const auto next = replacement.lower_bound(address);
require(next == replacement.end() || address + bytes <= next->first, "guest image overlaps a registered allocation");
if (next != replacement.begin()) {
const auto& previous = *std::prev(next)->second;
require(previous.address + previous.bytes <= address, "guest image overlaps a registered allocation");
}
replacement.emplace(address, std::make_shared<const Range>(Range{address, bytes, page->second.readable, page->second.writable, address, bytes, false}));
page = std::next(last);
}
state.ranges.swap(replacement);
state.mainImageRegistered = true;
}
#endif
void GuestAllocationsAdd_nid_postfix(void*, void* pointer, std::size_t bytes, bool readable, bool writable) {
@@ -61,14 +61,14 @@ void ValidateRange(const void* addr, size_t len, size_t alignment) {
}
int LinuxProtFromSce(int prot) {
if ((prot & ~0x37) != 0) {
if ((prot & ~0xF7) != 0) {
// return SCE_KERNEL_ERROR_EINVAL;
throw std::invalid_argument("Unsupported memory protection bits");
}
int result = PROT_NONE;
if (prot & 1) result |= PROT_READ;
if (prot & 2) result |= PROT_WRITE;
if (prot & 4) result |= PROT_EXEC;
if (prot & 0x13) result |= PROT_READ;
if (prot & 0x22) result |= PROT_READ | PROT_WRITE;
if (prot & 4) result |= PROT_READ | PROT_EXEC;
return result;
}
@@ -149,6 +149,8 @@ int DoMprotect(const void* addr, size_t len, int prot) {
if (memory.AllocationBase != GetModuleHandleW(nullptr)) throw std::invalid_argument("Memory protection of a foreign image is not supported");
mutation.RegisterMainImage();
}
#else
mutation.RegisterMainImage();
#endif
mutation.Protect(pointer, bytes, (prot & 3) != 0, (prot & 2) != 0, [&] {
if (mprotect(const_cast<void*>(pointer), bytes, nativeProtection) != 0) throw std::system_error(errno, std::generic_category(), "mprotect failed");
+2 -2
View File
@@ -87,8 +87,8 @@ int main(int argc, char**) {
write(replacement, 0x18, &finalize);
write(replacement, 0x20, &allocate);
write(replacement, 0x28, &release);
write(replacement, 0x30, &reallocate);
write(replacement, 0x38, &allocateZeroed);
write(replacement, 0x30, &allocateZeroed);
write(replacement, 0x38, &reallocate);
write(replacement, 0x40, &align);
write(replacement, 0x48, &reallocate);
write(replacement, 0x50, &posixAlign);