mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
Merge pull request #614 from nicolasabosch/fix/relinker-function-aliases
fix(relinker): allow function aliases with different sizes
This commit is contained in:
@@ -80,7 +80,7 @@ std::set<Domain::VirtualAddress> CodeInstructionCollector::Collect(const std::ve
|
||||
std::set<std::uint64_t> roots;
|
||||
std::map<std::uint64_t, std::uint64_t> functions;
|
||||
const auto addRoot = [&](std::uint64_t address) { if (isCode(address)) roots.insert(address); };
|
||||
const auto addFunction = [&](std::uint64_t begin, std::uint64_t size) {
|
||||
const auto addFunction = [&](std::uint64_t begin, std::uint64_t size, bool symbolAlias) {
|
||||
if (size == 0) return;
|
||||
if (!isCode(begin) || size > std::numeric_limits<std::uint64_t>::max() - begin)
|
||||
throw Domain::RelinkerException("Code analysis: invalid function range", begin);
|
||||
@@ -95,8 +95,9 @@ std::set<Domain::VirtualAddress> CodeInstructionCollector::Collect(const std::ve
|
||||
}
|
||||
if (!mapped) throw Domain::RelinkerException("Code analysis: function is not file-backed", begin);
|
||||
const auto [position, inserted] = functions.emplace(begin, begin + size);
|
||||
if (!inserted && position->second != begin + size)
|
||||
if (!inserted && !symbolAlias && position->second != begin + size)
|
||||
throw Domain::RelinkerException("Code analysis: conflicting function ranges", begin);
|
||||
position->second = std::max(position->second, begin + size);
|
||||
roots.insert(begin);
|
||||
};
|
||||
range(0, 64);
|
||||
@@ -117,7 +118,7 @@ std::set<Domain::VirtualAddress> CodeInstructionCollector::Collect(const std::ve
|
||||
if ((bytes[offset + 4] & 15) == 2 && Io::ReadU16(bytes, offset + 6) != 0) {
|
||||
const auto address = Io::ReadU64(bytes, offset + 8);
|
||||
addRoot(address);
|
||||
addFunction(address, Io::ReadU64(bytes, offset + 16));
|
||||
addFunction(address, Io::ReadU64(bytes, offset + 16), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -169,7 +170,7 @@ std::set<Domain::VirtualAddress> CodeInstructionCollector::Collect(const std::ve
|
||||
}
|
||||
for (const auto& function : UnusedNidFilter::ReadExceptionFunctions(bytes, headers, pointers.Values, importSlots)) {
|
||||
if (function.End <= function.Begin) throw Domain::RelinkerException("Code analysis: invalid unwind function range", function.Begin);
|
||||
addFunction(function.Begin, function.End - function.Begin);
|
||||
addFunction(function.Begin, function.End - function.Begin, false);
|
||||
for (const auto target : function.ExtraTargets) addRoot(target);
|
||||
}
|
||||
if (roots.empty()) throw Domain::RelinkerException("Code analysis: no code entry points");
|
||||
|
||||
@@ -197,6 +197,12 @@ def pe_bytes_at(pe, rva, size):
|
||||
raise AssertionError(f"Unmapped PE RVA {rva:#x}")
|
||||
|
||||
|
||||
def add_alias(image, size):
|
||||
struct.pack_into("<IBBHQQ", image, 0x650, 0, 0x12, 0, 1, 0x1200, size)
|
||||
struct.pack_into("<IIIII", image, 0x680, 1, 3, 1, 0, 0)
|
||||
return image
|
||||
|
||||
|
||||
def main():
|
||||
relinker = Path(sys.argv[1]).resolve()
|
||||
with tempfile.TemporaryDirectory(prefix="anyps5-tls-coverage-") as directory:
|
||||
@@ -232,6 +238,17 @@ def main():
|
||||
convert(metadata + "-" + transfer, make_image(transfer, metadata))
|
||||
convert(metadata + "-truncated", make_image("register", metadata, 0x46), "Code analysis:")
|
||||
convert(metadata + "-outside", make_image("register", metadata, 0x1000), "Code analysis: function exceeds executable segment")
|
||||
for first, second in ((0x47, 0x51), (0x51, 0x47), (0, 0x51), (0x51, 0)):
|
||||
convert(f"symbol-alias-{first:x}-{second:x}",
|
||||
add_alias(make_image("register", "symbol", first), second))
|
||||
for first, second in ((0x47, 0x1000), (0x1000, 0x47)):
|
||||
convert(f"symbol-alias-outside-{first:x}-{second:x}",
|
||||
add_alias(make_image("register", "symbol", first), second),
|
||||
"Code analysis: function exceeds executable segment")
|
||||
alias_tail = add_alias(make_image("register", "symbol", 0x51), 0x60)
|
||||
alias_tail[0x1258:0x1260] = bytes.fromhex("64 8b 04 25 28 00 00 00")
|
||||
convert("symbol-alias-unreachable-tls-tail", alias_tail,
|
||||
"Unsupported Windows guest TLS instruction", error_offset=0x1258)
|
||||
overlapping = make_image("register", "unwind")
|
||||
overlapping[0x1200:0x1205] = b"\xe9" + struct.pack("<i", 0x1245 - 0x1205)
|
||||
convert("overlapping-entry", overlapping, "Code analysis: overlapping instruction boundaries")
|
||||
|
||||
Reference in New Issue
Block a user