#include #include #include #include #include #include #include #include #include #include #include #include namespace Codegen { namespace { template auto _atFileOffset(const Domain::FileByteOffset base, const TOperation& operation) { try { return operation(); } catch (const CodegenException& e) { throw CodegenException(e.what(), base + e.FailureOffset); } } class Amd64OnlyConverter : public IAmd64OnlyConverter { public: [[nodiscard]] ConvertResult Convert( std::vector fileBytes, const std::vector& codeSegments ) const override; private: struct Pending { std::size_t Index; InstructionMatch Instruction; Amd64OnlyMatch Substitution; }; std::unique_ptr _matcher = MakeAmd64OnlyInstructionMatcher(); std::unique_ptr _scanner = MakeInstructionScanner(); void _convertSegment( std::vector& fileBytes, const Domain::ProgramHeader& ph, ConvertResult& result ) const; [[nodiscard]] static std::set _collectBranchTargets( const std::vector& seg, const std::vector& matches, const Domain::ProgramHeader& ph ); }; std::set Amd64OnlyConverter::_collectBranchTargets( const std::vector& seg, const std::vector& matches, const Domain::ProgramHeader& ph ) { const X64InstructionDecoder decoder; std::set targets; for (const auto& match : matches) { const auto info = decoder.DecodeInstruction(seg.data() + match.Offset, match.Length); if (!info.HasBranchTarget || info.HasRipRelativeDisp) continue; const auto target = static_cast(ph.MappedAddress + match.Offset + info.Length) + info.BranchDisp; if (target >= 0) targets.insert(static_cast(target)); } return targets; } void Amd64OnlyConverter::_convertSegment( std::vector& fileBytes, const Domain::ProgramHeader& ph, ConvertResult& result ) const { const auto segOffset = static_cast(ph.Offset); const auto segSize = static_cast(ph.FileSize); if (segOffset > fileBytes.size() || segSize > fileBytes.size() - segOffset) throw CodegenException("Code segment exceeds the file", ph.Offset); std::vector seg( fileBytes.begin() + static_cast(segOffset), fileBytes.begin() + static_cast(segOffset + segSize) ); const auto matches = _atFileOffset(ph.Offset, [&] { return _scanner->ScanCodeSection(seg, 0, seg.size()); }); std::vector pending; for (std::size_t index = 0; index < matches.size(); ++index) { const auto& match = matches[index]; auto substitution = _atFileOffset(ph.Offset + match.Offset, [&] { return _matcher->Match(seg.data() + match.Offset, match.Length); }); if (substitution.has_value()) pending.push_back({index, match, std::move(*substitution)}); } if (pending.empty()) return; const bool needsBranchTargets = std::any_of(pending.begin(), pending.end(), [](const Pending& item) { return item.Substitution.Lowering == Amd64OnlyLowering::Trampoline; }); const auto branchTargets = needsBranchTargets ? _collectBranchTargets(seg, matches, ph) : std::set{}; std::set consumed; for (const auto& item : pending) { if (consumed.contains(item.Index)) continue; const auto& match = item.Instruction; const auto& substitution = item.Substitution; const auto fileOffset = static_cast(ph.Offset + match.Offset); const auto address = static_cast(ph.MappedAddress + match.Offset); std::size_t replacementLength = 0; switch (substitution.Lowering) { case Amd64OnlyLowering::InPlace: { if (substitution.ReplacementBytes.size() != match.Length) throw CodegenException("Intel substitution changes the instruction length", fileOffset); std::copy(substitution.ReplacementBytes.begin(), substitution.ReplacementBytes.end(), seg.begin() + static_cast(match.Offset)); replacementLength = substitution.ReplacementBytes.size(); ++result.ReplacedCount; break; } case Amd64OnlyLowering::Trampoline: { std::vector taken; try { std::size_t siteLength = match.Length; auto stub = substitution; if (siteLength < Amd64OnlySubstitutionTable::kJmpRel32.Size) { const X64InstructionDecoder decoder; std::vector> sequence{std::span(seg.data() + match.Offset, match.Length)}; std::size_t trailingBytes = 0; for (auto next = item.Index + 1; siteLength < Amd64OnlySubstitutionTable::kJmpRel32.Size; ++next) { if (next >= matches.size() || matches[next].Offset != match.Offset + siteLength) throw CodegenException("AMD-only instruction too short for a jump and not followed by an instruction", fileOffset); const auto& following = matches[next]; const std::span bytes(seg.data() + following.Offset, following.Length); const auto info = decoder.DecodeInstruction(bytes.data(), bytes.size()); const bool amdOnly = _matcher->Match(bytes.data(), bytes.size()).has_value(); if (amdOnly && trailingBytes == 0) { sequence.push_back(bytes); taken.push_back(next); } else if (amdOnly || info.FlowKind != ControlFlowKind::Sequential || info.HasBranchTarget) { throw CodegenException("AMD-only instruction too short for a jump is followed by an instruction that cannot move", ph.Offset + following.Offset); } else { trailingBytes += following.Length; } siteLength += following.Length; } const std::span trailing(seg.data() + match.Offset + siteLength - trailingBytes, trailingBytes); auto relocated = _atFileOffset(fileOffset, [&] { return _matcher->MatchSequence(sequence, trailing); }); if (!relocated.has_value() || relocated->Lowering != Amd64OnlyLowering::Trampoline) throw CodegenException("AMD-only instruction sequence has no out-of-line lowering", fileOffset); stub = std::move(*relocated); } const auto hit = branchTargets.upper_bound(address); if (hit != branchTargets.end() && *hit < address + siteLength) throw CodegenException("Branch enters an AMD-only instruction", ph.Offset + (*hit - ph.MappedAddress)); const auto begin = seg.begin() + static_cast(match.Offset); result.Trampolines.push_back({ fileOffset, address, siteLength, std::vector(begin, begin + static_cast(siteLength)), stub.StubBody, stub.ReturnBranchOffset, stub.Relocations }); replacementLength = stub.StubBody.size(); consumed.insert(taken.begin(), taken.end()); } catch (const CodegenException&) { if (!substitution.Optional) throw; ++result.KeptCount; result.Reports.push_back({substitution.InstructionName, fileOffset, match.Length, 0, Amd64OnlyLowering::Kept}); continue; } break; } case Amd64OnlyLowering::Kept: case Amd64OnlyLowering::Unsupported: throw CodegenException("AMD-only instruction without Intel lowering: " + substitution.InstructionName, fileOffset); } result.Reports.push_back({substitution.InstructionName, fileOffset, match.Length, replacementLength, substitution.Lowering}); } if (seg.size() != segSize) throw CodegenException("Code segment size changed during Intel conversion", ph.Offset); std::copy(seg.begin(), seg.end(), fileBytes.begin() + static_cast(segOffset)); } ConvertResult Amd64OnlyConverter::Convert( std::vector fileBytes, const std::vector& codeSegments ) const { ConvertResult result{}; for (const auto& ph : codeSegments) _convertSegment(fileBytes, ph, result); result.Bytes = std::move(fileBytes); return result; } } std::unique_ptr MakeAmd64OnlyConverter() { return std::make_unique(); } }