Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ std::set<Domain::VirtualAddress> CodeInstructionCollector::Collect(const std::ve
addFunction(function.Begin, function.End - function.Begin, false);
for (const auto target : function.ExtraTargets) addRoot(target);
}
if (sceBase) {
if (sceBase || !functions.empty()) {
const std::vector<std::pair<std::uint64_t, std::uint64_t>> ranges(functions.begin(), functions.end());
for (const auto& header : codeHeaders) {
if (header.Type != 1 || (header.Flags & 1) == 0) continue;
Expand Down
280 changes: 279 additions & 1 deletion core/relinker/relinker/src/analysis/RelativeJumpTables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <codegen/CodegenException.hpp>
#include <algorithm>
#include <cstring>
#include <optional>
#include <set>

namespace Relinker {

Expand Down Expand Up @@ -67,6 +69,183 @@ std::size_t TablesEnd(std::span<const std::uint8_t> code, std::size_t firstTable
return table;
}

bool IsInvalid64BitSingleByteOpcode(std::uint8_t opcode) {
switch (opcode) {
case 0x06:
case 0x07:
case 0x0e:
case 0x16:
case 0x17:
case 0x1e:
case 0x1f:
case 0x27:
case 0x2f:
case 0x37:
case 0x3f:
case 0x60:
case 0x61:
case 0x82:
case 0x9a:
case 0xce:
case 0xd4:
case 0xd5:
case 0xd6:
case 0xea:
return true;
default:
return false;
}
}

bool IsForbiddenSyscallInstruction(std::span<const std::uint8_t> code, std::size_t pos, const Codegen::DecodedInstructionInfo& info) {
const auto opPos = pos + info.OpcodeOffset;
if (opPos + 1 >= pos + info.Length || opPos + 1 >= code.size()) return false;
const auto b0 = code[opPos];
const auto b1 = code[opPos + 1];
return (b0 == 0x0f && (b1 == 0x05 || b1 == 0x34 || b1 == 0x07)) || (b0 == 0xcd && b1 == 0x80);
}

bool IsValid64BitInstruction(std::span<const std::uint8_t> code, std::size_t pos, const Codegen::DecodedInstructionInfo& info, VirtualAddress baseVAddr, VirtualAddress segmentVAddr, VirtualAddress segmentEnd) {
if (info.Length == 0 || info.Length > code.size() - pos) return false;
if (!info.IsTwoByteOpcode && IsInvalid64BitSingleByteOpcode(info.Opcode)) return false;
if (info.HasBranchTarget && !info.HasRipRelativeDisp) {
const auto target = static_cast<std::int64_t>(baseVAddr + pos + info.Length) + info.BranchDisp;
if (target < static_cast<std::int64_t>(segmentVAddr) || target >= static_cast<std::int64_t>(segmentEnd)) return false;
}
return true;
}

bool EndsControlFlow(const Codegen::DecodedInstructionInfo& info, bool isSyscall) {
return info.FlowKind == Codegen::ControlFlowKind::Return
|| info.FlowKind == Codegen::ControlFlowKind::UnconditionalJump
|| info.FlowKind == Codegen::ControlFlowKind::IndirectJump
|| info.FlowKind == Codegen::ControlFlowKind::Trap || isSyscall;
}

class GapAnalysis {
enum class Trailing { Invalid, Padding, Terminator, NonTerminator };

public:
GapAnalysis(std::span<const std::uint8_t> code, VirtualAddress baseVAddr, VirtualAddress segmentVAddr, VirtualAddress segmentEnd, const Codegen::X64InstructionDecoder& decoder)
: code(code), padding(code.size() + 1), trailing(code.size() + 1), syscalls(code.size() + 1) {
padding.back() = true;
trailing.back() = Trailing::Padding;
for (std::size_t end = code.size(); end != 0;) {
const auto pos = --end;
const bool bytePadding = code[pos] == 0x00 || code[pos] == 0xcc || code[pos] == 0x90;
if (bytePadding && padding[pos + 1]) {
padding[pos] = true;
trailing[pos] = Trailing::Padding;
continue;
}
Codegen::DecodedInstructionInfo info;
try {
info = decoder.DecodeInstruction(code.data() + pos, code.size() - pos);
} catch (const Codegen::CodegenException&) {
continue;
}
if (info.Length == 0 || info.Length > code.size() - pos) continue;
const bool nop = IsNop(code, pos, info.Length);
if (nop && padding[pos + info.Length]) {
padding[pos] = true;
trailing[pos] = Trailing::Padding;
continue;
}
if (!IsValid64BitInstruction(code, pos, info, baseVAddr, segmentVAddr, segmentEnd)) continue;
const auto next = pos + info.Length;
const bool isSyscall = IsForbiddenSyscallInstruction(code, pos, info);
syscalls[pos] = isSyscall || syscalls[next];
if (EndsControlFlow(info, isSyscall)) {
trailing[pos] = isSyscall || trailing[next] == Trailing::Padding ? Trailing::Terminator : Trailing::NonTerminator;
continue;
}
if (trailing[next] != Trailing::Padding) {
trailing[pos] = trailing[next];
continue;
}
trailing[pos] = nop || code[pos] == 0xcc || code[pos] == 0x00 ? Trailing::Padding : Trailing::NonTerminator;
}
}

bool OnlyPadding(std::size_t from) const { return padding[from]; }

bool DecodesAsTrailingCodeBlock(std::size_t from, bool* outHasSyscall = nullptr) const {
if (outHasSyscall != nullptr) *outHasSyscall = false;
if (from >= code.size() || code[from] == 0x00 || code[from] == 0xcc || code[from] == 0x90) return false;
if (outHasSyscall != nullptr) *outHasSyscall = syscalls[from];
return trailing[from] == Trailing::Terminator;
}

private:
std::span<const std::uint8_t> code;
std::vector<bool> padding;
std::vector<Trailing> trailing;
std::vector<bool> syscalls;
};

std::size_t FindEmbeddedDataTableEnd(std::span<const std::uint8_t> code, VirtualAddress tableVAddr, VirtualAddress segmentVAddr, VirtualAddress segmentEnd, const Codegen::X64InstructionDecoder& decoder) {
const GapAnalysis analysis(code, tableVAddr, segmentVAddr, segmentEnd, decoder);
std::optional<std::size_t> firstInvalidPos;
for (std::size_t pos = 0; pos < code.size();) {
if (analysis.OnlyPadding(pos)) break;
Codegen::DecodedInstructionInfo info;
try {
info = decoder.DecodeInstruction(code.data() + pos, code.size() - pos);
} catch (const Codegen::CodegenException&) {
firstInvalidPos = pos;
break;
}
if (!IsValid64BitInstruction(code, pos, info, tableVAddr, segmentVAddr, segmentEnd)) {
firstInvalidPos = pos;
break;
}
if (IsForbiddenSyscallInstruction(code, pos, info) || info.FlowKind != Codegen::ControlFlowKind::Sequential) return 0;
pos += info.Length;
}
if (!firstInvalidPos) return 0;
const bool duplicatedEightByteRow = code.size() >= 16 && std::memcmp(code.data(), code.data() + 8, 8) == 0;
if (*firstInvalidPos != 0 && (!duplicatedEightByteRow || *firstInvalidPos > 4)) return 0;

std::optional<std::size_t> firstDecodeFailure;
for (std::size_t pos = 0; pos < code.size();) {
if (analysis.OnlyPadding(pos)) break;
try {
const auto info = decoder.DecodeInstruction(code.data() + pos, code.size() - pos);
if (info.Length == 0 || info.Length > code.size() - pos) {
firstDecodeFailure = pos;
break;
}
if (IsForbiddenSyscallInstruction(code, pos, info)) return 0;
pos += info.Length;
} catch (const Codegen::CodegenException&) {
firstDecodeFailure = pos;
break;
}
}
if (!firstDecodeFailure) return 0;

for (std::size_t pos = *firstDecodeFailure + 1; pos < code.size();) {
if (code[pos] != 0xcc && code[pos] != 0x90 && code[pos] != 0x00) {
++pos;
continue;
}
const auto padStart = pos;
bool hasZeroPad = false;
while (pos < code.size() && (code[pos] == 0xcc || code[pos] == 0x90 || code[pos] == 0x00)) {
if (code[pos] == 0x00) hasZeroPad = true;
++pos;
}
if (pos < code.size() && analysis.DecodesAsTrailingCodeBlock(pos))
return hasZeroPad ? pos : padStart;
}
for (std::size_t pos = *firstDecodeFailure + 1; pos < code.size(); ++pos) {
bool hasSyscall = false;
if (analysis.DecodesAsTrailingCodeBlock(pos, &hasSyscall) && hasSyscall)
return pos;
}
return code.size();
}

}

std::size_t FindTrailingRelativeJumpTable(std::span<const std::uint8_t> code, VirtualAddress codeAddress, std::size_t* tableEnd, std::size_t unwindEnd) {
Expand Down Expand Up @@ -121,6 +300,68 @@ std::size_t FindTrailingRelativeJumpTable(std::span<const std::uint8_t> code, Vi
std::vector<RelativeJumpTable> FindRelativeJumpTables(std::span<const std::uint8_t> segment, VirtualAddress segmentVAddr, const std::vector<std::pair<VirtualAddress, VirtualAddress>>& functions) {
std::vector<RelativeJumpTable> tables;
const auto segmentEnd = segmentVAddr + segment.size();
const Codegen::X64InstructionDecoder decoder;
std::set<VirtualAddress> branchTargets;
for (const auto& [funcBegin, funcEnd] : functions) {
if (funcBegin < segmentVAddr || funcBegin >= segmentEnd || funcEnd <= funcBegin) continue;
const auto inspectFuncEnd = std::min(funcEnd, segmentEnd);
const auto funcCode = segment.subspan(funcBegin - segmentVAddr, inspectFuncEnd - funcBegin);
for (std::size_t pos = 0; pos < funcCode.size();) {
Codegen::DecodedInstructionInfo info;
try {
info = decoder.DecodeInstruction(funcCode.data() + pos, funcCode.size() - pos);
} catch (const Codegen::CodegenException&) {
break;
}
if (info.Length == 0 || info.Length > funcCode.size() - pos) break;
if (info.HasBranchTarget && !info.HasRipRelativeDisp) {
const auto target = static_cast<std::int64_t>(funcBegin + pos + info.Length) + info.BranchDisp;
if (target >= static_cast<std::int64_t>(segmentVAddr) && target < static_cast<std::int64_t>(segmentEnd))
branchTargets.insert(static_cast<VirtualAddress>(target));
}
pos += info.Length;
}
}
auto knownFunctions = functions;
std::erase_if(knownFunctions, [](const auto& range) { return range.first >= range.second; });
std::sort(knownFunctions.begin(), knownFunctions.end());
std::vector<std::pair<VirtualAddress, VirtualAddress>> mergedFunctions;
for (const auto& range : knownFunctions) {
if (!mergedFunctions.empty() && range.first <= mergedFunctions.back().second)
mergedFunctions.back().second = std::max(mergedFunctions.back().second, range.second);
else
mergedFunctions.emplace_back(range);
}
std::vector<VirtualAddress> pendingTargets(branchTargets.begin(), branchTargets.end());
std::set<VirtualAddress> exploredGaps;
while (!pendingTargets.empty()) {
const auto current = pendingTargets.back();
pendingTargets.pop_back();
if (current < segmentVAddr || current >= segmentEnd) continue;
const auto after = std::upper_bound(mergedFunctions.begin(), mergedFunctions.end(), current,
[](VirtualAddress value, const auto& range) { return value < range.first; });
const bool inKnownFunction = after != mergedFunctions.begin() && current < std::prev(after)->second;
if (inKnownFunction || !exploredGaps.insert(current).second) continue;
auto pos = static_cast<std::size_t>(current - segmentVAddr);
while (pos < segment.size()) {
Codegen::DecodedInstructionInfo info;
try {
info = decoder.DecodeInstruction(segment.data() + pos, segment.size() - pos);
} catch (const Codegen::CodegenException&) {
break;
}
if (info.Length == 0 || info.Length > segment.size() - pos) break;
if (info.HasBranchTarget && !info.HasRipRelativeDisp) {
const auto target = static_cast<std::int64_t>(segmentVAddr + pos + info.Length) + info.BranchDisp;
if (target >= static_cast<std::int64_t>(segmentVAddr) && target < static_cast<std::int64_t>(segmentEnd)) {
const auto targetVAddr = static_cast<VirtualAddress>(target);
if (branchTargets.insert(targetVAddr).second) pendingTargets.push_back(targetVAddr);
}
}
if (info.FlowKind == Codegen::ControlFlowKind::Return || info.FlowKind == Codegen::ControlFlowKind::IndirectJump || info.FlowKind == Codegen::ControlFlowKind::Trap) break;
pos += info.Length;
}
}
for (std::size_t index = 0; index < functions.size(); ++index) {
const auto [begin, end] = functions[index];
if (begin < segmentVAddr || begin >= segmentEnd || end <= begin) continue;
Expand All @@ -129,8 +370,45 @@ std::vector<RelativeJumpTable> FindRelativeJumpTables(std::span<const std::uint8
inspectEnd = std::min(functions[index + 1].first, segmentEnd);
const auto code = segment.subspan(begin - segmentVAddr, inspectEnd - begin);
std::size_t tableEnd = code.size();
if (const auto tail = FindTrailingRelativeJumpTable(code, begin, &tableEnd, std::min(end, segmentEnd) - begin); tail != 0 && tail < code.size())
if (const auto tail = FindTrailingRelativeJumpTable(code, begin, &tableEnd, std::min(end, segmentEnd) - begin); tail != 0 && tail < code.size()) {
tables.push_back({begin, begin + tail, begin + tableEnd});
continue;
}
if (inspectEnd <= end) continue;
const auto funcCode = segment.subspan(begin - segmentVAddr, end - begin);
std::optional<VirtualAddress> candidateTable;
bool sawIndirectJump = false;
for (std::size_t pos = 0; pos < funcCode.size();) {
Codegen::DecodedInstructionInfo info;
try {
info = decoder.DecodeInstruction(funcCode.data() + pos, funcCode.size() - pos);
} catch (const Codegen::CodegenException&) {
break;
}
if (info.Length == 0 || info.Length > funcCode.size() - pos) break;
if (info.FlowKind == Codegen::ControlFlowKind::IndirectJump) sawIndirectJump = true;
const auto next = pos + info.Length;
if (info.HasRipRelativeDisp && !info.IsTwoByteOpcode && info.OpcodeOffset < info.Length && info.Length >= 4 && funcCode[pos + info.OpcodeOffset] == 0x8d) {
std::int32_t displacement = 0;
std::memcpy(&displacement, funcCode.data() + next - 4, sizeof(displacement));
const auto target = static_cast<std::int64_t>(begin + next) + displacement;
if (target >= static_cast<std::int64_t>(end) && target < static_cast<std::int64_t>(inspectEnd)) {
const auto targetVAddr = static_cast<VirtualAddress>(target);
candidateTable = candidateTable ? std::min(*candidateTable, targetVAddr) : targetVAddr;
}
}
pos = next;
}
if (sawIndirectJump || !candidateTable) continue;
const auto tableVAddr = *candidateTable;
const auto paddingCode = segment.subspan(end - segmentVAddr, tableVAddr - end);
if (!OnlyPadding(paddingCode, 0, paddingCode.size(), decoder)) continue;
const auto it = branchTargets.lower_bound(tableVAddr);
if (it != branchTargets.end() && *it < inspectEnd) continue;
const auto tableCode = segment.subspan(tableVAddr - segmentVAddr, inspectEnd - tableVAddr);
const auto tableSize = FindEmbeddedDataTableEnd(tableCode, tableVAddr, segmentVAddr, segmentEnd, decoder);
if (tableSize == 0) continue;
tables.push_back({begin, tableVAddr, tableVAddr + tableSize});
}
return tables;
}
Expand Down
19 changes: 16 additions & 3 deletions core/relinker/relinker/src/guest/GuestModuleBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <relinker/guest/GuestImage.hpp>
#include <relinker/analysis/RelativeJumpTables.hpp>
#include <relinker/analysis/SceCodeExtent.hpp>
#include <relinker/analysis/UnusedNidFilter/EhFrameReader.hpp>
#include <elfpatcher/general/GuestModuleWriter.hpp>
Expand Down Expand Up @@ -395,14 +396,26 @@ std::vector<GuestArtifact> GuestModuleBuilder::Build(const std::filesystem::path
}
if (!image.PatchedRanges.empty()) GamePatch::CheckOverlaps(Io::Utf8Path(image.SourcePath), image.PatchedRanges, {.RelaData = image.Dynamic.RelaData, .RelaPltData = image.Dynamic.RelaPltData});
const bool sceImage = std::any_of(image.Headers.begin(), image.Headers.end(), [](const Domain::ProgramHeader& header) { return header.Type == 0x61000000; });
const auto unwindFunctions = sceImage ? UnusedNidFilter::ReadExceptionFunctions(image.Bytes, image.Headers, {}, {}) : std::vector<UnusedNidFilter::StrictCodeRegion>{};
const bool hasEhFrame = sceImage || std::any_of(image.Headers.begin(), image.Headers.end(), [](const Domain::ProgramHeader& header) { return header.Type == 0x6474e550; });
const auto unwindFunctions = hasEhFrame ? UnusedNidFilter::ReadExceptionFunctions(image.Bytes, image.Headers, {}, {}) : std::vector<UnusedNidFilter::StrictCodeRegion>{};
std::vector<std::pair<VirtualAddress, VirtualAddress>> unwindRanges;
for (const auto& function : unwindFunctions) unwindRanges.emplace_back(function.Begin, function.End);
std::sort(unwindRanges.begin(), unwindRanges.end());
for (const auto& header : codeHeaders) {
std::vector<std::uint8_t> code(image.Bytes.begin() + header.Offset, image.Bytes.begin() + header.Offset + header.FileSize);
if (sceImage)
if (const auto end = FindSceCodeEnd(code, header.MappedAddress, unwindFunctions, image.Got, image.Dynamic.RelaPltData.size()))
code.resize(*end - header.MappedAddress);
const auto unwindRanges = sceImage ? SceUnwindRanges(code, header.MappedAddress, unwindFunctions) : std::vector<CodeRange>{};
for (const auto offset : syscallScanner.ScanCodeSectionForSyscalls(code, header.MappedAddress, code.size(), Io::Utf8Path(image.SourcePath), unwindRanges)) {
auto scanCode = code;
for (const auto& table : FindRelativeJumpTables(code, header.MappedAddress, unwindRanges)) {
if (table.Begin >= header.MappedAddress && table.End >= table.Begin) {
const auto maskEnd = std::min<std::size_t>(table.End - header.MappedAddress, scanCode.size());
const auto maskBegin = std::min<std::size_t>(table.Begin - header.MappedAddress, maskEnd);
std::fill(scanCode.begin() + static_cast<std::ptrdiff_t>(maskBegin), scanCode.begin() + static_cast<std::ptrdiff_t>(maskEnd), std::uint8_t{0x90});
}
}
const auto scanUnwindRanges = sceImage ? SceUnwindRanges(code, header.MappedAddress, unwindFunctions) : std::vector<CodeRange>{};
for (const auto offset : syscallScanner.ScanCodeSectionForSyscalls(scanCode, header.MappedAddress, scanCode.size(), Io::Utf8Path(image.SourcePath), scanUnwindRanges)) {
if (offset > code.size() || code.size() - offset < 2u)
throw Domain::RelinkerException("Syscall trap patch exceeds guest executable segment", header.MappedAddress + offset);
if (!image.PatchedRanges.empty()) {
Expand Down
Loading
Loading