From f524a27a8f0d0626578f097854083e33730310bc Mon Sep 17 00:00:00 2001 From: Merijn Date: Mon, 5 Oct 2026 18:37:35 +0200 Subject: [PATCH] fix(relinker): accept absolute symbols in guest modules Guest modules may define symbols in SHN_ABS (0xfff1), for example the 151 library marker symbols of PPSA12544's Il2cppUserAssemblies.prx. The reader no longer rejects them or checks their value as a mapped address, relocations against them write the absolute value without a base relocation, and they are not exported: a PE export is an RVA, so an import of one fails at startup as an unresolved import. Other special sections are still rejected. Co-Authored-By: Claude Opus 5.5 --- core/relinker/CMakeLists.txt | 2 + .../src/windows/WindowsGuestModuleWriter.cpp | 5 +- .../include/relinker/guest/GuestImage.hpp | 1 + .../relinker/src/guest/GuestImageReader.cpp | 4 +- .../relinker/src/guest/GuestModuleBuilder.cpp | 4 +- .../tests/test_guest_absolute_symbols.py | 94 +++++++++++++++++++ 6 files changed, 104 insertions(+), 6 deletions(-) create mode 100644 core/relinker/relinker/tests/test_guest_absolute_symbols.py diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index a388ec7a3..9ba234ca5 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -212,6 +212,8 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_module_directories.py $) add_test(NAME guest_host_libc COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_host_libc.py $) + add_test(NAME guest_absolute_symbols COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_absolute_symbols.py $) add_test(NAME elf_header COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_elf_header.py $) add_test(NAME input_magic COMMAND ${Python3_EXECUTABLE} diff --git a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp index a05d32ab4..5cdb3ed73 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp @@ -62,7 +62,8 @@ std::vector GuestModuleWriter::WriteWindows(const Relinker::GuestI image.WritePointer(target, symbol.Value + addend - size); } else if (type == 1 || type == 6 || type == 7) { if (symbolIndex == 0 || (type != 1 && addend != 0) || (symbol.Info & 15) == 6) throw Domain::RelinkerException("Invalid guest symbol relocation", target); - if (symbol.Section != 0) { + if (symbol.Section == Relinker::AbsoluteSection) image.WritePointer(target, symbol.Value + addend); + else if (symbol.Section != 0) { if (symbol.Section >= 0xff00) throw Domain::RelinkerException("Unsupported special guest symbol section", target); image.WritePointer(target, image.GetRelocatedAddress(symbol.Value) + addend); relocations.push_back(rva); @@ -109,7 +110,7 @@ std::vector GuestModuleWriter::WriteWindows(const Relinker::GuestI std::map exports; PeSection tlsExports{".tlsrefs", nextRva, SectionRead | 0x40u, {}}; for (const auto& symbol : guest.Symbols) { - if (symbol.Section == 0 || (symbol.Info >> 4) == 0 || symbol.Visibility == 1 || symbol.Visibility == 2) continue; + if (symbol.Section == 0 || symbol.Section == Relinker::AbsoluteSection || (symbol.Info >> 4) == 0 || symbol.Visibility == 1 || symbol.Visibility == 2) continue; if (symbol.Section >= 0xff00) throw Domain::RelinkerException("Unsupported guest export section: " + symbol.Name); std::uint32_t rva = 0; if ((symbol.Info & 15) == 6) { diff --git a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp index 8325e8208..40e30aba5 100644 --- a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp +++ b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp @@ -11,6 +11,7 @@ namespace Relinker { inline constexpr char GuestModuleSuffix[] = ".guest.prx"; +inline constexpr std::uint16_t AbsoluteSection = 0xfff1; struct GuestSymbol { std::string Name; diff --git a/core/relinker/relinker/src/guest/GuestImageReader.cpp b/core/relinker/relinker/src/guest/GuestImageReader.cpp index 4e8e5cf88..837273006 100644 --- a/core/relinker/relinker/src/guest/GuestImageReader.cpp +++ b/core/relinker/relinker/src/guest/GuestImageReader.cpp @@ -137,7 +137,7 @@ GuestImage GuestImageReader::Read(const std::filesystem::path& path, std::vector if (name.empty() || !exports.insert(name).second) fail("Duplicate or empty export after stripping #: " + name); } if ((info & 15) == 6 && section != 0 && (tls == nullptr || value > tls->MemorySize || size > tls->MemorySize - value)) fail("TLS symbol exceeds TLS segment: " + name); - if (section != 0 && (info & 15) != 6) { + if (section != 0 && section != AbsoluteSection && (info & 15) != 6) { if (section >= 0xff00) fail("Unsupported special symbol section: " + name); mapped(value, std::max(size, 1), (info & 15) == 2 ? 1 : 0); } @@ -218,7 +218,7 @@ GuestImage GuestImageReader::Read(const std::filesystem::path& path, std::vector else if (type == 1) { const auto& symbol = image.Symbols.at(info >> 32); if ((symbol.Info & 15) != 2 || addend != 0) fail("Invalid guest lifecycle function relocation"); - if (symbol.Section != 0) mapped(symbol.Value, 1, 1); + if (symbol.Section != 0 && symbol.Section != AbsoluteSection) mapped(symbol.Value, 1, 1); } else fail("Unsupported guest lifecycle relocation"); } } diff --git a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp index eebb504ca..d882dcfbc 100644 --- a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp +++ b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp @@ -63,7 +63,7 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path } if (!outputNames.insert(folded).second) throw Domain::RelinkerException("Conflicting guest output filename: " + image.OutputName); for (const auto& symbol : image.Symbols) { - if (symbol.Section == 0 || (symbol.Info >> 4) == 0 || symbol.Visibility == 1 || symbol.Visibility == 2) continue; + if (symbol.Section == 0 || symbol.Section == AbsoluteSection || (symbol.Info >> 4) == 0 || symbol.Visibility == 1 || symbol.Visibility == 2) continue; const auto [existing, inserted] = exports.emplace(symbol.Name, images.size()); if (!inserted) throw Domain::RelinkerException("Duplicate guest export after stripping #: " + symbol.Name + " in " + images.at(existing->second).SourcePath.string() + " and " + path.string()); } @@ -100,7 +100,7 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path const auto found = exports.find(symbol.Name); if (found != exports.end()) { const auto& provider = images[found->second]; - const auto exported = std::find_if(provider.Symbols.begin(), provider.Symbols.end(), [&](const auto& candidate) { return candidate.Section != 0 && candidate.Name == symbol.Name && (candidate.Info >> 4) != 0 && candidate.Visibility != 1 && candidate.Visibility != 2; }); + const auto exported = std::find_if(provider.Symbols.begin(), provider.Symbols.end(), [&](const auto& candidate) { return candidate.Section != 0 && candidate.Section != AbsoluteSection && candidate.Name == symbol.Name && (candidate.Info >> 4) != 0 && candidate.Visibility != 1 && candidate.Visibility != 2; }); if (exported == provider.Symbols.end() || ((symbol.Info & 15) != 0 && (symbol.Info & 15) != (exported->Info & 15))) throw Domain::RelinkerException("Guest import/export type mismatch: " + symbol.Name); if (found->second != index) dependencies[index].insert(found->second); } else if (windows && (symbol.Info & 15) == 6) throw Domain::RelinkerException("Windows guest TLS import requires a guest TLS export: " + symbol.Name); diff --git a/core/relinker/relinker/tests/test_guest_absolute_symbols.py b/core/relinker/relinker/tests/test_guest_absolute_symbols.py new file mode 100644 index 000000000..f85802258 --- /dev/null +++ b/core/relinker/relinker/tests/test_guest_absolute_symbols.py @@ -0,0 +1,94 @@ +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from test_guest_intel_trampolines import elf_loads, main_fixture, pe_sections +from test_guest_module_directories import module_with_symbol + + +ABSOLUTE = 0xFFF1 +COMMON = 0xFFF2 +VALUE = 0x1234 +ADDEND = 0x10 +TARGET = 0x2320 + + +def module_with_section(section): + image = module_with_symbol(True) + struct.pack_into("> 32) * 24 + 6) + return info & 0xFFFFFFFF, section, value, addend + raise AssertionError("Relocation against the absolute symbol is missing") + + +def main(): + relinker = Path(sys.argv[1]).resolve() + with tempfile.TemporaryDirectory(prefix="anyps5-guest-absolute-") as directory: + work = Path(directory) + + def convert(name, windows, section): + case = work / f"{name}-{windows}" + (case / "sce_module").mkdir(parents=True) + (case / "sce_module" / "module.prx").write_bytes(module_with_section(section)) + source = case / "input.elf" + source.write_bytes(main_fixture()) + output = case / ("output.exe" if windows else "output.elf") + result = subprocess.run([str(relinker), *(["--windows"] if windows else []), str(source), str(output)], + capture_output=True, text=True, timeout=30) + return result, case / "app0" / "sce_module" / "module.prx.guest.prx" + + for windows in (False, True): + result, artifact = convert("absolute", windows, ABSOLUTE) + assert result.returncode == 0, (result.stdout, result.stderr) + data = artifact.read_bytes() + if windows: + assert any(value == VALUE + ADDEND for _, value in pe_qwords(data)), "absolute relocation value missing" + else: + assert elf_relocation(data) == (1, ABSOLUTE, VALUE, ADDEND) + + result, artifact = convert("common", windows, COMMON) + assert result.returncode == 2 and "Unsupported special symbol section" in result.stderr, result.stderr + assert not artifact.exists(), artifact + print("Guest absolute symbol tests passed") + + +if __name__ == "__main__": + main()