From 0a44083bbfa46b3a14c3c3f58be5ccf2ec9f40a6 Mon Sep 17 00:00:00 2001 From: boykopovar Date: Tue, 4 Aug 2026 14:29:32 +0300 Subject: [PATCH] refactor(patcher): separate linux and windows elf patchers --- core/CMakeLists.txt | 3 +- .../LinuxElfPatcher.hpp} | 10 ++--- .../elfpatcher/windows/WindowsElfPatcher.hpp | 37 +++++++++++++++++++ .../LinuxElfPatcher.cpp} | 10 ++--- .../src/windows/WindowsElfPatcher.cpp | 27 ++++++++++++++ core/main.cpp | 4 +- 6 files changed, 78 insertions(+), 13 deletions(-) rename core/elfpatcher/include/elfpatcher/{ElfPatcher.hpp => linux/LinuxElfPatcher.hpp} (86%) create mode 100644 core/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp rename core/elfpatcher/src/{ElfPatcher.cpp => linux/LinuxElfPatcher.cpp} (95%) create mode 100644 core/elfpatcher/src/windows/WindowsElfPatcher.cpp diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 8c009c2cb..a4384a099 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -1,6 +1,7 @@ add_executable(relinker ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/ElfPatcher.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/linux/LinuxElfPatcher.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/windows/WindowsElfPatcher.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/filtering/SegmentFilter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/stub/EntryStubBuilder.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/layout/ProgramHeaderLayoutBuilder.cpp diff --git a/core/elfpatcher/include/elfpatcher/ElfPatcher.hpp b/core/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp similarity index 86% rename from core/elfpatcher/include/elfpatcher/ElfPatcher.hpp rename to core/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp index cbf0f28b6..5f222b92c 100644 --- a/core/elfpatcher/include/elfpatcher/ElfPatcher.hpp +++ b/core/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp @@ -1,5 +1,5 @@ -#ifndef ELFPATCHER_ELFPATCHER_HPP -#define ELFPATCHER_ELFPATCHER_HPP +#ifndef ELFPATCHER_LINUX_LINUXELFPATCHER_HPP +#define ELFPATCHER_LINUX_LINUXELFPATCHER_HPP #include #include @@ -8,11 +8,11 @@ #include #include -namespace Elfpatcher { +namespace Elfpatcher::Linux { -class ElfPatcher : public IElfPatcher { +class LinuxElfPatcher : public IElfPatcher { public: - ElfPatcher( + LinuxElfPatcher( std::shared_ptr entryStubBuilder, std::shared_ptr programHeaderLayoutBuilder, std::shared_ptr sectionHeaderTableBuilder, diff --git a/core/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp b/core/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp new file mode 100644 index 000000000..f18e7cd78 --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp @@ -0,0 +1,37 @@ +#ifndef ELFPATCHER_WINDOWS_WINDOWSELFPATCHER_HPP +#define ELFPATCHER_WINDOWS_WINDOWSELFPATCHER_HPP + +#include +#include +#include +#include +#include +#include + +namespace Elfpatcher::Windows { + +class WindowsElfPatcher : public IElfPatcher { +public: + WindowsElfPatcher( + std::shared_ptr entryStubBuilder, + std::shared_ptr programHeaderLayoutBuilder, + std::shared_ptr sectionHeaderTableBuilder, + std::shared_ptr byteWriter + ); + + std::vector Patch( + const std::vector& sourceElf, + const std::vector& originalHeaders, + const Domain::SysVDynamicSection& dynamicSection + ) override; + +private: + std::shared_ptr _entryStubBuilder; + std::shared_ptr _programHeaderLayoutBuilder; + std::shared_ptr _sectionHeaderTableBuilder; + std::shared_ptr _byteWriter; +}; + +} + +#endif diff --git a/core/elfpatcher/src/ElfPatcher.cpp b/core/elfpatcher/src/linux/LinuxElfPatcher.cpp similarity index 95% rename from core/elfpatcher/src/ElfPatcher.cpp rename to core/elfpatcher/src/linux/LinuxElfPatcher.cpp index cb58b0b33..f5571d5a4 100644 --- a/core/elfpatcher/src/ElfPatcher.cpp +++ b/core/elfpatcher/src/linux/LinuxElfPatcher.cpp @@ -1,11 +1,11 @@ -#include +#include #include #include #include -namespace Elfpatcher { +namespace Elfpatcher::Linux { -ElfPatcher::ElfPatcher( +LinuxElfPatcher::LinuxElfPatcher( std::shared_ptr entryStubBuilder, std::shared_ptr programHeaderLayoutBuilder, std::shared_ptr sectionHeaderTableBuilder, @@ -18,12 +18,12 @@ ElfPatcher::ElfPatcher( { } -void ElfPatcher::_appendDynEntry(std::vector& buf, std::int64_t tag, std::uint64_t val) const { +void LinuxElfPatcher::_appendDynEntry(std::vector& buf, std::int64_t tag, std::uint64_t val) const { _byteWriter->AppendI64(buf, tag); _byteWriter->AppendU64(buf, val); } -std::vector ElfPatcher::Patch( +std::vector LinuxElfPatcher::Patch( const std::vector& sourceElf, const std::vector& originalHeaders, const Domain::SysVDynamicSection& dynSection) diff --git a/core/elfpatcher/src/windows/WindowsElfPatcher.cpp b/core/elfpatcher/src/windows/WindowsElfPatcher.cpp new file mode 100644 index 000000000..1e5a4d8ca --- /dev/null +++ b/core/elfpatcher/src/windows/WindowsElfPatcher.cpp @@ -0,0 +1,27 @@ +#include +#include + +namespace Elfpatcher::Windows { + +WindowsElfPatcher::WindowsElfPatcher( + std::shared_ptr entryStubBuilder, + std::shared_ptr programHeaderLayoutBuilder, + std::shared_ptr sectionHeaderTableBuilder, + std::shared_ptr byteWriter +) + : _entryStubBuilder(std::move(entryStubBuilder)) + , _programHeaderLayoutBuilder(std::move(programHeaderLayoutBuilder)) + , _sectionHeaderTableBuilder(std::move(sectionHeaderTableBuilder)) + , _byteWriter(std::move(byteWriter)) +{ +} + +std::vector WindowsElfPatcher::Patch( + const std::vector& sourceElf, + const std::vector& originalHeaders, + const Domain::SysVDynamicSection& dynamicSection) +{ + throw Domain::RelinkerException("Not implemented"); +} + +} diff --git a/core/main.cpp b/core/main.cpp index 0d4254da4..d2371056f 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -1,7 +1,7 @@ #include #include #include -#include +#include #include #include #include @@ -71,7 +71,7 @@ int main(const int argc, char* argv[]) { } auto byteWriter = std::make_shared(); - Elfpatcher::ElfPatcher elfPatcher( + Elfpatcher::Linux::LinuxElfPatcher elfPatcher( std::make_shared(), std::make_shared( std::make_shared(),