diff --git a/README.md b/README.md index 5c15a272b..4d969e52e 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,8 @@ The [shader recompiler](core/shader/recompiler/Recompiler.cpp) successfully prod The relinker uses only the C++20 standard library and should build with any conforming compiler. +On Intel hosts, pass `--to-intel` to the relinker to lower supported AMD-only instructions in the executable and bundled `sce_module`/`sce_modules` PRX files. Unsupported instructions or stub jumps outside the x86-64 relative branch range produce an error. + [libc.prx](core/libs/prx/libc) implementations contain compiler-specific code. Linux builds work with GCC; on Windows, MinGW-w64 GCC 15.2.0 (`winlibs-gcc15`, `x86_64-ucrt-posix-seh`) is currently required. The project targets maximum compiler portability. Support for additional compilers will be addressed after the first successful game launch. diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 8328500ae..6fc7a1c17 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -157,6 +157,8 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_linux_load_alignment.py $) add_test(NAME windows_gui COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_gui.py $) + add_test(NAME guest_intel_trampolines COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_intel_trampolines.py $) else() message(STATUS "Python3 interpreter not found; skipping Python relinker tests") endif() diff --git a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp index 11b2348ef..590b34d5d 100644 --- a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -100,6 +101,63 @@ std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestIma tag(30, 8); tag(0, 0); const auto dynamicSize = bytes.size() - dynamicOffset; + using namespace Codegen::Amd64OnlySubstitutionTable; + const auto checkedAdd = [](const std::uint64_t left, const std::uint64_t right, const std::uint64_t offset) { + if (right > std::numeric_limits::max() - left) + throw Domain::RelinkerException("AMD-only guest stub address overflow", offset); + return left + right; + }; + const auto displacement = [](const std::uint64_t target, const std::uint64_t next, const std::uint64_t offset) { + if (target >= next) { + const auto distance = target - next; + if (distance > static_cast(std::numeric_limits::max())) + throw Domain::RelinkerException("AMD-only guest stub exceeds rel32 range", offset); + return static_cast(distance); + } + const auto distance = next - target; + if (distance > static_cast(std::numeric_limits::max()) + 1) + throw Domain::RelinkerException("AMD-only guest stub exceeds rel32 range", offset); + return static_cast(-static_cast(distance)); + }; + for (const auto& site : image.Trampolines) { + if (site.Length < kJmpRel32.Size || site.OriginalBytes.size() != site.Length || + site.Body.size() < kJmpRel32.Size || + site.ReturnBranchOffset > site.Body.size() - kJmpRel32.Size || + site.Body[site.ReturnBranchOffset] != kJmpRel32.Bytes[0]) + throw Domain::RelinkerException("Invalid AMD-only guest trampoline site", site.Offset); + if (site.Offset > image.Bytes.size() || site.Length > image.Bytes.size() - site.Offset) + throw Domain::RelinkerException("AMD-only guest instruction is outside the original image", site.Offset); + const auto mapped = std::any_of(image.Headers.begin(), image.Headers.end(), [&](const auto& header) { + if (header.Type != 1 || (header.Flags & 1) == 0 || + site.Address < header.MappedAddress || site.Offset < header.Offset) + return false; + const auto addressDelta = site.Address - header.MappedAddress; + const auto offsetDelta = site.Offset - header.Offset; + return addressDelta == offsetDelta && addressDelta <= header.FileSize && + site.Length <= header.FileSize - addressDelta; + }); + if (!mapped) + throw Domain::RelinkerException("AMD-only guest instruction is outside an executable segment", site.Offset); + if (!std::equal(site.OriginalBytes.begin(), site.OriginalBytes.end(), + bytes.begin() + static_cast(site.Offset))) + throw Domain::RelinkerException("AMD-only guest site bytes changed before patching", site.Offset); + bytes.resize(Io::AlignUp(bytes.size(), kStubAlignment), kTrapFill); + const auto stubOffset = bytes.size(); + const auto stubAddress = checkedAdd(extraAddress, stubOffset - extraOffset, site.Offset); + checkedAdd(stubAddress, site.Body.size(), site.Offset); + const auto returnAddress = checkedAdd(site.Address, site.Length, site.Offset); + const auto stubReturn = checkedAdd(stubAddress, site.ReturnBranchOffset + kJmpRel32.Size, site.Offset); + const auto siteNext = checkedAdd(site.Address, kJmpRel32.Size, site.Offset); + const auto returnDisplacement = displacement(returnAddress, stubReturn, site.Offset); + const auto siteDisplacement = displacement(stubAddress, siteNext, site.Offset); + bytes.insert(bytes.end(), site.Body.begin(), site.Body.end()); + Io::WriteU32(bytes, stubOffset + site.ReturnBranchOffset + 1, + static_cast(returnDisplacement)); + std::fill_n(bytes.begin() + static_cast(site.Offset), site.Length, kNop1.Bytes[0]); + bytes[static_cast(site.Offset)] = kJmpRel32.Bytes[0]; + Io::WriteU32(bytes, static_cast(site.Offset + 1), + static_cast(siteDisplacement)); + } const auto phOffset = bytes.size(); const auto phCount = headers.size() + 2; if (phCount > std::numeric_limits::max()) throw Domain::RelinkerException("Too many guest program headers"); diff --git a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp index 78b7c1d83..2d6b7b3f1 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -79,6 +80,7 @@ std::vector GuestModuleWriter::WriteWindows(const Relinker::GuestI std::array directories{}; std::uint32_t tlsIndex = 0; directories[9] = WindowsTlsBuilder().Build(guest.Bytes, guest.Headers, image, sections, relocations, nextRva, &tlsIndex); + WindowsTrampolineBuilder().Build(guest.Trampolines, image, sections, nextRva); for (const auto& [target, rva] : tlsModules) { if (tlsIndex == 0) throw Domain::RelinkerException("Guest TLS relocation has no TLS block", target); bool written = false; diff --git a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp index f85029698..fb2646f44 100644 --- a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp +++ b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp @@ -1,6 +1,7 @@ #ifndef RELINKER_GUESTIMAGE_HPP #define RELINKER_GUESTIMAGE_HPP +#include #include #include #include @@ -24,6 +25,7 @@ struct GuestImage { std::filesystem::path SourcePath; std::string OutputName; std::vector Bytes; + std::vector Trampolines; std::vector Headers; std::vector Symbols; std::vector Dependencies; diff --git a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp index aa68ea39d..831ce2012 100644 --- a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp +++ b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp @@ -62,7 +62,7 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path for (const auto& header : image.Headers) if (header.Type == 1 && (header.Flags & 1) != 0) codeHeaders.push_back(header); if (toIntel) { auto converted = Codegen::MakeAmd64OnlyConverter()->Convert(std::move(image.Bytes), codeHeaders); - if (!converted.Trampolines.empty()) throw Domain::RelinkerException("--to-intel needs " + std::to_string(converted.Trampolines.size()) + " stub(s) in guest module " + path.string() + "; stub sections are not written for guest modules"); + image.Trampolines = std::move(converted.Trampolines); image.Bytes = std::move(converted.Bytes); } for (const auto& header : codeHeaders) { diff --git a/core/relinker/relinker/tests/test_guest_intel_trampolines.py b/core/relinker/relinker/tests/test_guest_intel_trampolines.py new file mode 100644 index 000000000..c6b2222c9 --- /dev/null +++ b/core/relinker/relinker/tests/test_guest_intel_trampolines.py @@ -0,0 +1,142 @@ +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from test_linux_load_alignment import fixture as executable_fixture + + +SITE = bytes.fromhex("f2 0f 78 db 08 08") +SITE_ADDRESSES = (0x1002, 0x1012) +PLAIN_SITE = b"\x90" * len(SITE) + + +def guest_fixture(site): + image = bytearray(0xA00) + image[:16] = b"\x7fELF\x02\x01\x01" + bytes(9) + struct.pack_into("