diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 26276046b..006738d94 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -222,6 +222,8 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_symbol_names.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 7f50aba5b..572234514 100644 --- a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp +++ b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp @@ -12,6 +12,7 @@ namespace Relinker { inline constexpr char GuestSymbolSuffix[] = "#guest"; 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 59e5b3289..fe15abdd9 100644 --- a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp +++ b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp @@ -65,7 +65,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) continue; if (windows || existing->second == images.size()) throw Domain::RelinkerException("Duplicate guest export after stripping #: " + symbol.Name + " in " + images.at(existing->second).SourcePath.string() + " and " + path.string()); @@ -131,7 +131,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()