mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #698 from merwie/fix/relinker-guest-absolute-symbols
fix(relinker): accept absolute symbols in guest modules
This commit is contained in:
@@ -222,6 +222,8 @@ if(BUILD_TESTING)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_symbol_names.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME guest_host_libc COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_host_libc.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME guest_absolute_symbols COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_absolute_symbols.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME elf_header COMMAND ${Python3_EXECUTABLE}
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_elf_header.py $<TARGET_FILE:relinker>)
|
||||
add_test(NAME input_magic COMMAND ${Python3_EXECUTABLE}
|
||||
|
||||
@@ -62,7 +62,8 @@ std::vector<std::uint8_t> 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<std::uint8_t> GuestModuleWriter::WriteWindows(const Relinker::GuestI
|
||||
std::map<std::string, std::uint32_t> 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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<std::uint64_t>(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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +65,7 @@ std::vector<GuestArtifact> 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<GuestArtifact> 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);
|
||||
|
||||
@@ -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("<HQQ", image, 0x898 + 6, section, VALUE, 0)
|
||||
struct.pack_into("<Q", image, 0x668, 24)
|
||||
struct.pack_into("<QQq", image, 0x900, TARGET, (1 << 32) | 1, ADDEND)
|
||||
return image
|
||||
|
||||
|
||||
def pe_qwords(data):
|
||||
for name, rva, raw_size, raw_offset, _ in pe_sections(data):
|
||||
for position in range(0, raw_size - 7, 8):
|
||||
yield rva + position, struct.unpack_from("<Q", data, raw_offset + position)[0]
|
||||
|
||||
|
||||
def elf_relocation(data):
|
||||
loads = elf_loads(data)
|
||||
phoff, = struct.unpack_from("<Q", data, 32)
|
||||
phsize, phcount = struct.unpack_from("<HH", data, 54)
|
||||
dynamic = next(struct.unpack_from("<IIQQQQQQ", data, phoff + index * phsize)
|
||||
for index in range(phcount)
|
||||
if struct.unpack_from("<I", data, phoff + index * phsize)[0] == 2)
|
||||
|
||||
def offset(address):
|
||||
for header in loads:
|
||||
if header[3] <= address < header[3] + header[5]:
|
||||
return header[2] + address - header[3]
|
||||
raise AssertionError(f"Unmapped address: {address:#x}")
|
||||
|
||||
tags = {}
|
||||
for position in range(dynamic[2], dynamic[2] + dynamic[5], 16):
|
||||
tag, value = struct.unpack_from("<qQ", data, position)
|
||||
if tag == 0:
|
||||
break
|
||||
tags.setdefault(tag, value)
|
||||
symbols = offset(tags[6])
|
||||
relocations = offset(tags[7])
|
||||
for position in range(relocations, relocations + tags[8], 24):
|
||||
target, info, addend = struct.unpack_from("<QQq", data, position)
|
||||
if target == TARGET:
|
||||
section, value = struct.unpack_from("<HQ", data, symbols + (info >> 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()
|
||||
Reference in New Issue
Block a user