diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index d6663c6c8..efe370fc6 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -9,6 +9,7 @@ add_executable(relinker ${CMAKE_CURRENT_SOURCE_DIR}/io/src/ByteWriter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/io/src/FileReader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/io/src/FileWriter.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/Amd64OnlyConverter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/InstructionScanner.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/X64InstructionDecoder.cpp ${CMAKE_CURRENT_SOURCE_DIR}/codegen/src/x86/X64InstructionRewriter.cpp diff --git a/core/codegen/include/codegen/CodegenTypes.hpp b/core/codegen/include/codegen/CodegenTypes.hpp index b15fd3180..77cdfc6cf 100644 --- a/core/codegen/include/codegen/CodegenTypes.hpp +++ b/core/codegen/include/codegen/CodegenTypes.hpp @@ -28,6 +28,11 @@ struct RewriteResult { std::vector Adjustments; }; +struct ConvertResult { + std::vector Bytes; + std::size_t ReplacedCount; +}; + } #endif diff --git a/core/codegen/include/codegen/IAmd64OnlyConverter.hpp b/core/codegen/include/codegen/IAmd64OnlyConverter.hpp index c2d28c602..1bc42caf8 100644 --- a/core/codegen/include/codegen/IAmd64OnlyConverter.hpp +++ b/core/codegen/include/codegen/IAmd64OnlyConverter.hpp @@ -1,8 +1,8 @@ #ifndef CODEGEN_IAMD64ONLYCONVERTER_HPP #define CODEGEN_IAMD64ONLYCONVERTER_HPP +#include #include -#include #include #include @@ -12,7 +12,7 @@ class IAmd64OnlyConverter { public: virtual ~IAmd64OnlyConverter() = default; - [[nodiscard]] virtual std::vector Convert( + [[nodiscard]] virtual ConvertResult Convert( std::vector fileBytes, const std::vector& codeSegments ) const = 0; diff --git a/core/codegen/src/Amd64OnlyConverter.cpp b/core/codegen/src/Amd64OnlyConverter.cpp index c29401550..74f624611 100644 --- a/core/codegen/src/Amd64OnlyConverter.cpp +++ b/core/codegen/src/Amd64OnlyConverter.cpp @@ -10,7 +10,7 @@ namespace { class Amd64OnlyConverter : public IAmd64OnlyConverter { public: - [[nodiscard]] std::vector Convert( + [[nodiscard]] ConvertResult Convert( std::vector fileBytes, const std::vector& codeSegments ) const override; @@ -20,13 +20,13 @@ private: std::unique_ptr _matcher = MakeAmd64OnlyInstructionMatcher(); std::unique_ptr _scanner = MakeInstructionScanner(); - void _convertSegment( + std::size_t _convertSegment( std::vector& fileBytes, const Domain::ProgramHeader& ph ) const; }; -void Amd64OnlyConverter::_convertSegment( +std::size_t Amd64OnlyConverter::_convertSegment( std::vector& fileBytes, const Domain::ProgramHeader& ph ) const { @@ -40,6 +40,7 @@ void Amd64OnlyConverter::_convertSegment( const auto matches = _scanner->ScanCodeSection(seg, 0, seg.size()); std::int64_t shift = 0; + std::size_t count = 0; for (const auto& match : matches) { const auto shiftedOffset = static_cast(match.Offset) + shift; @@ -56,20 +57,23 @@ void Amd64OnlyConverter::_convertSegment( shift += static_cast(result->ReplacementBytes.size()) - static_cast(match.Length); seg = std::move(rewriteResult.Bytes); + ++count; } const auto copySize = std::min(seg.size(), segSize); std::copy(seg.begin(), seg.begin() + copySize, fileBytes.begin() + segOffset); + return count; } -std::vector Amd64OnlyConverter::Convert( +ConvertResult Amd64OnlyConverter::Convert( std::vector fileBytes, const std::vector& codeSegments ) const { + std::size_t total = 0; for (const auto& ph : codeSegments) { - _convertSegment(fileBytes, ph); + total += _convertSegment(fileBytes, ph); } - return fileBytes; + return {std::move(fileBytes), total}; } } diff --git a/core/main.cpp b/core/main.cpp index b93fa75f5..5b35186f5 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -56,7 +56,9 @@ int main(const int argc, char* argv[]) { if (toIntel) { const Relinker::ElfReader elfReader(sourceBytes); const auto converter = Codegen::MakeAmd64OnlyConverter(); - fileWriter.Write(outputElfPath, converter->Convert(std::move(sourceBytes), elfReader.ReadCodeSegments())); + auto result = converter->Convert(std::move(sourceBytes), elfReader.ReadCodeSegments()); + fileWriter.Write(outputElfPath, std::move(result.Bytes)); + std::cout << "OK: " << result.ReplacedCount << " instructions replaced\n"; return 0; }