From 7689e85d8f28928b27befd776651e18580a8a237 Mon Sep 17 00:00:00 2001 From: boykopovar Date: Tue, 4 Aug 2026 10:26:33 +0300 Subject: [PATCH] refactor(patcher): folders structure for elfpatcher + byte writing di --- core/CMakeLists.txt | 5 + .../include/elfpatcher/ElfPatcher.hpp | 78 +---- .../elfpatcher/domain/ElfConstants.hpp | 46 +++ .../elfpatcher/domain/IEntryStubBuilder.hpp | 21 ++ .../domain/IProgramHeaderLayoutBuilder.hpp | 22 ++ .../domain/ISectionHeaderTableBuilder.hpp | 22 ++ .../elfpatcher/domain/ISegmentFilter.hpp | 17 + .../domain/ProgramHeaderLayoutRequest.hpp | 25 ++ .../domain/SectionHeaderTableRequest.hpp | 21 ++ .../elfpatcher/filtering/SegmentFilter.hpp | 19 + .../layout/ProgramHeaderLayoutBuilder.hpp | 38 ++ .../sections/SectionHeaderTableBuilder.hpp | 40 +++ .../elfpatcher/stub/EntryStubBuilder.hpp | 18 + core/elfpatcher/src/ElfPatcher.cpp | 329 +++--------------- .../src/filtering/SegmentFilter.cpp | 25 ++ .../src/layout/ProgramHeaderLayoutBuilder.cpp | 180 ++++++++++ .../sections/SectionHeaderTableBuilder.cpp | 70 ++++ core/elfpatcher/src/stub/EntryStubBuilder.cpp | 29 ++ core/io/include/io/ByteWriter.hpp | 19 + core/io/include/io/IByteWriter.hpp | 22 ++ core/io/src/ByteWriter.cpp | 28 ++ core/main.cpp | 16 +- 22 files changed, 751 insertions(+), 339 deletions(-) create mode 100644 core/elfpatcher/include/elfpatcher/domain/ElfConstants.hpp create mode 100644 core/elfpatcher/include/elfpatcher/domain/IEntryStubBuilder.hpp create mode 100644 core/elfpatcher/include/elfpatcher/domain/IProgramHeaderLayoutBuilder.hpp create mode 100644 core/elfpatcher/include/elfpatcher/domain/ISectionHeaderTableBuilder.hpp create mode 100644 core/elfpatcher/include/elfpatcher/domain/ISegmentFilter.hpp create mode 100644 core/elfpatcher/include/elfpatcher/domain/ProgramHeaderLayoutRequest.hpp create mode 100644 core/elfpatcher/include/elfpatcher/domain/SectionHeaderTableRequest.hpp create mode 100644 core/elfpatcher/include/elfpatcher/filtering/SegmentFilter.hpp create mode 100644 core/elfpatcher/include/elfpatcher/layout/ProgramHeaderLayoutBuilder.hpp create mode 100644 core/elfpatcher/include/elfpatcher/sections/SectionHeaderTableBuilder.hpp create mode 100644 core/elfpatcher/include/elfpatcher/stub/EntryStubBuilder.hpp create mode 100644 core/elfpatcher/src/filtering/SegmentFilter.cpp create mode 100644 core/elfpatcher/src/layout/ProgramHeaderLayoutBuilder.cpp create mode 100644 core/elfpatcher/src/sections/SectionHeaderTableBuilder.cpp create mode 100644 core/elfpatcher/src/stub/EntryStubBuilder.cpp create mode 100644 core/io/include/io/ByteWriter.hpp create mode 100644 core/io/include/io/IByteWriter.hpp create mode 100644 core/io/src/ByteWriter.cpp diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index f916312af..f6151965a 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -1,6 +1,11 @@ add_executable(relinker ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/ElfPatcher.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 + ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/src/sections/SectionHeaderTableBuilder.cpp + ${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}/relinker/src/parsing/ElfReader.cpp diff --git a/core/elfpatcher/include/elfpatcher/ElfPatcher.hpp b/core/elfpatcher/include/elfpatcher/ElfPatcher.hpp index 2d6357f72..cbf0f28b6 100644 --- a/core/elfpatcher/include/elfpatcher/ElfPatcher.hpp +++ b/core/elfpatcher/include/elfpatcher/ElfPatcher.hpp @@ -2,11 +2,23 @@ #define ELFPATCHER_ELFPATCHER_HPP #include +#include +#include +#include +#include +#include namespace Elfpatcher { class ElfPatcher : public IElfPatcher { public: + ElfPatcher( + 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, @@ -14,70 +26,12 @@ public: ) override; private: - static constexpr std::uint32_t PT_LOAD = 1; - static constexpr std::uint32_t PT_DYNAMIC = 2; - static constexpr std::uint32_t PT_INTERP = 3; - static constexpr std::uint32_t PT_NOTE = 4; - static constexpr std::uint32_t PT_PHDR = 6; - static constexpr std::uint32_t PT_GNU_RELRO = 0x6474e552; - static constexpr std::uint32_t PF_X = 0x1; - static constexpr std::uint32_t PF_W = 0x2; - static constexpr std::uint32_t PF_R = 0x4; - static constexpr std::uint32_t ET_DYN = 3; - static constexpr std::uint32_t PT_SCE_DYNLIBDATA = 0x61000000; - static constexpr std::uint32_t PT_OS_PROCPARAM = 0x61000001; - static constexpr std::uint32_t PT_OS_RELRO = 0x61000010; + std::shared_ptr _entryStubBuilder; + std::shared_ptr _programHeaderLayoutBuilder; + std::shared_ptr _sectionHeaderTableBuilder; + std::shared_ptr _byteWriter; - static constexpr std::int64_t DT_NULL = 0; - static constexpr std::int64_t DT_NEEDED = 1; - static constexpr std::int64_t DT_STRTAB = 5; - static constexpr std::int64_t DT_SYMTAB = 6; - static constexpr std::int64_t DT_RELA = 7; - static constexpr std::int64_t DT_RELASZ = 8; - static constexpr std::int64_t DT_RELAENT = 9; - static constexpr std::int64_t DT_STRSZ = 10; - static constexpr std::int64_t DT_SYMENT = 11; - static constexpr std::int64_t DT_JMPREL = 23; - static constexpr std::int64_t DT_PLTRELSZ = 2; - static constexpr std::int64_t DT_PLTREL = 20; - static constexpr std::uint32_t SHT_NULL = 0; - static constexpr std::uint32_t SHT_PROGBITS = 1; - static constexpr std::uint32_t SHT_DYNSYM = 11; - static constexpr std::uint32_t SHT_STRTAB = 3; - static constexpr std::uint32_t SHT_DYNAMIC = 6; - static constexpr std::uint32_t SHF_ALLOC = 0x2; - static constexpr std::uint32_t SHF_WRITE = 0x1; - static constexpr std::uint32_t SHF_EXECINSTR = 0x4; - - void _writeSectionHeader( - std::vector& buf, - std::size_t offset, - std::uint32_t nameOff, - std::uint32_t type, - std::uint64_t flags, - std::uint64_t addr, - std::uint64_t fileOffset, - std::uint64_t size, - std::uint32_t link, - std::uint32_t info, - std::uint64_t align, - std::uint64_t entSize - ) const; - [[nodiscard]] bool _isSceSpecificSegment(std::uint32_t type) const; - [[nodiscard]] bool _isNullPageLoad(const Domain::ProgramHeader& ph) const; - [[nodiscard]] bool _shouldSkip(const Domain::ProgramHeader& ph) const; - [[nodiscard]] Domain::ProgramHeader _makeLoadHeader(std::uint64_t offset, std::uint64_t size) const; - [[nodiscard]] Domain::ProgramHeader _makeHeaderBlockLoad(std::uint64_t vaddr, std::uint64_t size, std::uint64_t align) const; - [[nodiscard]] Domain::ProgramHeader _makePhdrHeader(std::uint64_t offset, std::uint64_t vaddr, std::uint64_t size) const; - [[nodiscard]] std::vector _buildEntryStub(std::uint64_t stubVaddr, std::uint64_t realEntryVaddr) const; - void _writeU16(std::vector& buf, std::size_t offset, std::uint16_t v) const; - void _writeU32(std::vector& buf, std::size_t offset, std::uint32_t v) const; - void _writeU64(std::vector& buf, std::size_t offset, std::uint64_t v) const; - void _appendU64(std::vector& buf, std::uint64_t v) const; - void _appendI64(std::vector& buf, std::int64_t v) const; void _appendDynEntry(std::vector& buf, std::int64_t tag, std::uint64_t val) const; - void _writeProgramHeader(std::vector& buf, std::size_t offset, const Domain::ProgramHeader& ph) const; - [[nodiscard]] std::uint32_t _fixLoadFlags(std::uint32_t originalFlags) const; }; } diff --git a/core/elfpatcher/include/elfpatcher/domain/ElfConstants.hpp b/core/elfpatcher/include/elfpatcher/domain/ElfConstants.hpp new file mode 100644 index 000000000..7a84f27cf --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/domain/ElfConstants.hpp @@ -0,0 +1,46 @@ +#ifndef ELFPATCHER_DOMAIN_ELFCONSTANTS_HPP +#define ELFPATCHER_DOMAIN_ELFCONSTANTS_HPP + +#include + +namespace Elfpatcher { + +inline constexpr std::uint32_t PT_LOAD = 1; +inline constexpr std::uint32_t PT_DYNAMIC = 2; +inline constexpr std::uint32_t PT_INTERP = 3; +inline constexpr std::uint32_t PT_NOTE = 4; +inline constexpr std::uint32_t PT_PHDR = 6; +inline constexpr std::uint32_t PT_GNU_RELRO = 0x6474e552; +inline constexpr std::uint32_t PF_X = 0x1; +inline constexpr std::uint32_t PF_W = 0x2; +inline constexpr std::uint32_t PF_R = 0x4; +inline constexpr std::uint32_t ET_DYN = 3; +inline constexpr std::uint32_t PT_SCE_DYNLIBDATA = 0x61000000; +inline constexpr std::uint32_t PT_OS_PROCPARAM = 0x61000001; +inline constexpr std::uint32_t PT_OS_RELRO = 0x61000010; + +inline constexpr std::int64_t DT_NULL = 0; +inline constexpr std::int64_t DT_NEEDED = 1; +inline constexpr std::int64_t DT_PLTRELSZ = 2; +inline constexpr std::int64_t DT_STRTAB = 5; +inline constexpr std::int64_t DT_SYMTAB = 6; +inline constexpr std::int64_t DT_RELA = 7; +inline constexpr std::int64_t DT_RELASZ = 8; +inline constexpr std::int64_t DT_RELAENT = 9; +inline constexpr std::int64_t DT_STRSZ = 10; +inline constexpr std::int64_t DT_SYMENT = 11; +inline constexpr std::int64_t DT_PLTREL = 20; +inline constexpr std::int64_t DT_JMPREL = 23; + +inline constexpr std::uint32_t SHT_NULL = 0; +inline constexpr std::uint32_t SHT_PROGBITS = 1; +inline constexpr std::uint32_t SHT_STRTAB = 3; +inline constexpr std::uint32_t SHT_DYNAMIC = 6; +inline constexpr std::uint32_t SHT_DYNSYM = 11; +inline constexpr std::uint32_t SHF_WRITE = 0x1; +inline constexpr std::uint32_t SHF_ALLOC = 0x2; +inline constexpr std::uint32_t SHF_EXECINSTR = 0x4; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/domain/IEntryStubBuilder.hpp b/core/elfpatcher/include/elfpatcher/domain/IEntryStubBuilder.hpp new file mode 100644 index 000000000..6a4799e93 --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/domain/IEntryStubBuilder.hpp @@ -0,0 +1,21 @@ +#ifndef ELFPATCHER_DOMAIN_IENTRYSTUBBUILDER_HPP +#define ELFPATCHER_DOMAIN_IENTRYSTUBBUILDER_HPP + +#include +#include + +namespace Elfpatcher { + +class IEntryStubBuilder { +public: + virtual ~IEntryStubBuilder() = default; + + virtual std::vector BuildEntryStub( + std::uint64_t stubVaddr, + std::uint64_t realEntryVaddr + ) const = 0; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/domain/IProgramHeaderLayoutBuilder.hpp b/core/elfpatcher/include/elfpatcher/domain/IProgramHeaderLayoutBuilder.hpp new file mode 100644 index 000000000..6484d2dd7 --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/domain/IProgramHeaderLayoutBuilder.hpp @@ -0,0 +1,22 @@ +#ifndef ELFPATCHER_DOMAIN_IPROGRAMHEADERLAYOUTBUILDER_HPP +#define ELFPATCHER_DOMAIN_IPROGRAMHEADERLAYOUTBUILDER_HPP + +#include +#include +#include + +namespace Elfpatcher { + +class IProgramHeaderLayoutBuilder { +public: + virtual ~IProgramHeaderLayoutBuilder() = default; + + virtual std::uint16_t WriteLayout( + std::vector& buf, + const ProgramHeaderLayoutRequest& request + ) const = 0; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/domain/ISectionHeaderTableBuilder.hpp b/core/elfpatcher/include/elfpatcher/domain/ISectionHeaderTableBuilder.hpp new file mode 100644 index 000000000..bdca4c450 --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/domain/ISectionHeaderTableBuilder.hpp @@ -0,0 +1,22 @@ +#ifndef ELFPATCHER_DOMAIN_ISECTIONHEADERTABLEBUILDER_HPP +#define ELFPATCHER_DOMAIN_ISECTIONHEADERTABLEBUILDER_HPP + +#include +#include +#include + +namespace Elfpatcher { + +class ISectionHeaderTableBuilder { +public: + virtual ~ISectionHeaderTableBuilder() = default; + + virtual void WriteTable( + std::vector& buf, + const SectionHeaderTableRequest& request + ) const = 0; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/domain/ISegmentFilter.hpp b/core/elfpatcher/include/elfpatcher/domain/ISegmentFilter.hpp new file mode 100644 index 000000000..7d806651f --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/domain/ISegmentFilter.hpp @@ -0,0 +1,17 @@ +#ifndef ELFPATCHER_DOMAIN_ISEGMENTFILTER_HPP +#define ELFPATCHER_DOMAIN_ISEGMENTFILTER_HPP + +#include + +namespace Elfpatcher { + +class ISegmentFilter { +public: + virtual ~ISegmentFilter() = default; + + virtual bool ShouldSkip(const Domain::ProgramHeader& ph) const = 0; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/domain/ProgramHeaderLayoutRequest.hpp b/core/elfpatcher/include/elfpatcher/domain/ProgramHeaderLayoutRequest.hpp new file mode 100644 index 000000000..8cb591f22 --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/domain/ProgramHeaderLayoutRequest.hpp @@ -0,0 +1,25 @@ +#ifndef ELFPATCHER_DOMAIN_PROGRAMHEADERLAYOUTREQUEST_HPP +#define ELFPATCHER_DOMAIN_PROGRAMHEADERLAYOUTREQUEST_HPP + +#include +#include +#include + +namespace Elfpatcher { + +struct ProgramHeaderLayoutRequest { + std::uint64_t PhOff; + std::uint16_t PhEntSize; + std::uint16_t PhNum; + std::vector OriginalHeaders; + std::uint64_t ExtraBlockOffset; + std::uint64_t ExtraBlockSize; + std::uint64_t DynamicSegmentOffset; + std::uint64_t DynamicSegmentSize; + std::uint64_t InterpOffset; + std::uint64_t InterpSize; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/domain/SectionHeaderTableRequest.hpp b/core/elfpatcher/include/elfpatcher/domain/SectionHeaderTableRequest.hpp new file mode 100644 index 000000000..df5e479b1 --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/domain/SectionHeaderTableRequest.hpp @@ -0,0 +1,21 @@ +#ifndef ELFPATCHER_DOMAIN_SECTIONHEADERTABLEREQUEST_HPP +#define ELFPATCHER_DOMAIN_SECTIONHEADERTABLEREQUEST_HPP + +#include + +namespace Elfpatcher { + +struct SectionHeaderTableRequest { + std::uint64_t DynStrOffset; + std::uint64_t DynStrSize; + std::uint64_t DynSymOffset; + std::uint64_t DynSymSize; + std::uint64_t DynamicSegmentOffset; + std::uint64_t DynamicSegmentSize; + std::uint64_t StubOffset; + std::uint64_t StubSize; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/filtering/SegmentFilter.hpp b/core/elfpatcher/include/elfpatcher/filtering/SegmentFilter.hpp new file mode 100644 index 000000000..b3bba073a --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/filtering/SegmentFilter.hpp @@ -0,0 +1,19 @@ +#ifndef ELFPATCHER_FILTERING_SEGMENTFILTER_HPP +#define ELFPATCHER_FILTERING_SEGMENTFILTER_HPP + +#include + +namespace Elfpatcher { + +class SegmentFilter : public ISegmentFilter { +public: + bool ShouldSkip(const Domain::ProgramHeader& ph) const override; + +private: + [[nodiscard]] bool _isSceSpecificSegment(std::uint32_t type) const; + [[nodiscard]] bool _isNullPageLoad(const Domain::ProgramHeader& ph) const; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/layout/ProgramHeaderLayoutBuilder.hpp b/core/elfpatcher/include/elfpatcher/layout/ProgramHeaderLayoutBuilder.hpp new file mode 100644 index 000000000..a7bf4f651 --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/layout/ProgramHeaderLayoutBuilder.hpp @@ -0,0 +1,38 @@ +#ifndef ELFPATCHER_LAYOUT_PROGRAMHEADERLAYOUTBUILDER_HPP +#define ELFPATCHER_LAYOUT_PROGRAMHEADERLAYOUTBUILDER_HPP + +#include +#include +#include +#include + +namespace Elfpatcher { + +class ProgramHeaderLayoutBuilder : public IProgramHeaderLayoutBuilder { +public: + ProgramHeaderLayoutBuilder( + std::shared_ptr segmentFilter, + std::shared_ptr byteWriter + ); + + std::uint16_t WriteLayout( + std::vector& buf, + const ProgramHeaderLayoutRequest& request + ) const override; + +private: + std::shared_ptr _segmentFilter; + std::shared_ptr _byteWriter; + + [[nodiscard]] Domain::ProgramHeader _makeLoadHeader(std::uint64_t offset, std::uint64_t size) const; + [[nodiscard]] Domain::ProgramHeader _makeHeaderBlockLoad(std::uint64_t vaddr, std::uint64_t size, std::uint64_t align) const; + [[nodiscard]] Domain::ProgramHeader _makePhdrHeader(std::uint64_t offset, std::uint64_t vaddr, std::uint64_t size) const; + [[nodiscard]] Domain::ProgramHeader _makeDynamicHeader(std::uint64_t offset, std::uint64_t size) const; + [[nodiscard]] Domain::ProgramHeader _makeInterpHeader(std::uint64_t offset, std::uint64_t size) const; + [[nodiscard]] std::uint32_t _fixLoadFlags(std::uint32_t originalFlags) const; + void _writeProgramHeader(std::vector& buf, std::size_t offset, const Domain::ProgramHeader& ph) const; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/sections/SectionHeaderTableBuilder.hpp b/core/elfpatcher/include/elfpatcher/sections/SectionHeaderTableBuilder.hpp new file mode 100644 index 000000000..0296cd3aa --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/sections/SectionHeaderTableBuilder.hpp @@ -0,0 +1,40 @@ +#ifndef ELFPATCHER_SECTIONS_SECTIONHEADERTABLEBUILDER_HPP +#define ELFPATCHER_SECTIONS_SECTIONHEADERTABLEBUILDER_HPP + +#include +#include +#include + +namespace Elfpatcher { + +class SectionHeaderTableBuilder : public ISectionHeaderTableBuilder { +public: + explicit SectionHeaderTableBuilder(std::shared_ptr byteWriter); + + void WriteTable( + std::vector& buf, + const SectionHeaderTableRequest& request + ) const override; + +private: + std::shared_ptr _byteWriter; + + void _writeSectionHeader( + std::vector& buf, + std::size_t offset, + std::uint32_t nameOff, + std::uint32_t type, + std::uint64_t flags, + std::uint64_t addr, + std::uint64_t fileOffset, + std::uint64_t size, + std::uint32_t link, + std::uint32_t info, + std::uint64_t align, + std::uint64_t entSize + ) const; +}; + +} + +#endif diff --git a/core/elfpatcher/include/elfpatcher/stub/EntryStubBuilder.hpp b/core/elfpatcher/include/elfpatcher/stub/EntryStubBuilder.hpp new file mode 100644 index 000000000..ecef4eb34 --- /dev/null +++ b/core/elfpatcher/include/elfpatcher/stub/EntryStubBuilder.hpp @@ -0,0 +1,18 @@ +#ifndef ELFPATCHER_STUB_ENTRYSTUBBUILDER_HPP +#define ELFPATCHER_STUB_ENTRYSTUBBUILDER_HPP + +#include + +namespace Elfpatcher { + +class EntryStubBuilder : public IEntryStubBuilder { +public: + std::vector BuildEntryStub( + std::uint64_t stubVaddr, + std::uint64_t realEntryVaddr + ) const override; +}; + +} + +#endif diff --git a/core/elfpatcher/src/ElfPatcher.cpp b/core/elfpatcher/src/ElfPatcher.cpp index ca0b8f1f7..cb58b0b33 100644 --- a/core/elfpatcher/src/ElfPatcher.cpp +++ b/core/elfpatcher/src/ElfPatcher.cpp @@ -1,153 +1,26 @@ #include -#include +#include +#include +#include namespace Elfpatcher { -bool ElfPatcher::_isSceSpecificSegment(std::uint32_t type) const { - return type == PT_SCE_DYNLIBDATA - || type == PT_OS_PROCPARAM - || type == PT_OS_RELRO - || (type >= 0x61000000 && type <= 0x6fffffff); -} - -bool ElfPatcher::_isNullPageLoad(const Domain::ProgramHeader& ph) const { - return ph.Type == PT_LOAD && ph.MappedAddress == 0; -} - -void ElfPatcher::_writeU16(std::vector& buf, std::size_t offset, std::uint16_t v) const { - std::memcpy(buf.data() + offset, &v, 2); -} - -void ElfPatcher::_writeU32(std::vector& buf, const std::size_t offset, std::uint32_t v) const { - std::memcpy(buf.data() + offset, &v, 4); -} - -void ElfPatcher::_writeU64(std::vector& buf, const std::size_t offset, std::uint64_t v) const { - std::memcpy(buf.data() + offset, &v, 8); -} - -void ElfPatcher::_appendU64(std::vector& buf, std::uint64_t v) const { - std::size_t pos = buf.size(); - buf.resize(pos + 8); - std::memcpy(buf.data() + pos, &v, 8); -} - -void ElfPatcher::_appendI64(std::vector& buf, std::int64_t v) const { - _appendU64(buf, static_cast(v)); +ElfPatcher::ElfPatcher( + 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)) +{ } void ElfPatcher::_appendDynEntry(std::vector& buf, std::int64_t tag, std::uint64_t val) const { - _appendI64(buf, tag); - _appendU64(buf, val); -} - -void ElfPatcher::_writeProgramHeader(std::vector& buf, std::size_t offset, const Domain::ProgramHeader& ph) const { - _writeU32(buf, offset + 0, ph.Type); - _writeU32(buf, offset + 4, ph.Flags); - _writeU64(buf, offset + 8, ph.Offset); - _writeU64(buf, offset + 16, ph.MappedAddress); - _writeU64(buf, offset + 24, ph.PhysicalAddress); - _writeU64(buf, offset + 32, ph.FileSize); - _writeU64(buf, offset + 40, ph.MemorySize); - _writeU64(buf, offset + 48, ph.Alignment); -} - -Domain::ProgramHeader ElfPatcher::_makeLoadHeader(std::uint64_t offset, std::uint64_t size) const { - Domain::ProgramHeader ph{}; - ph.Type = PT_LOAD; - ph.Flags = PF_R | PF_W; - ph.Offset = offset; - ph.MappedAddress = offset; - ph.PhysicalAddress = offset; - ph.FileSize = size; - ph.MemorySize = size; - ph.Alignment = 0x1000; - return ph; -} - -Domain::ProgramHeader ElfPatcher::_makeHeaderBlockLoad(std::uint64_t vaddr, std::uint64_t size, std::uint64_t align) const { - Domain::ProgramHeader ph{}; - ph.Type = PT_LOAD; - ph.Flags = PF_R; - ph.Offset = 0; - ph.MappedAddress = vaddr; - ph.PhysicalAddress = vaddr; - ph.FileSize = size; - ph.MemorySize = size; - ph.Alignment = align; - return ph; -} - -Domain::ProgramHeader ElfPatcher::_makePhdrHeader(std::uint64_t offset, std::uint64_t vaddr, std::uint64_t size) const { - Domain::ProgramHeader ph{}; - ph.Type = PT_PHDR; - ph.Flags = PF_R; - ph.Offset = offset; - ph.MappedAddress = vaddr; - ph.PhysicalAddress = vaddr; - ph.FileSize = size; - ph.MemorySize = size; - ph.Alignment = 8; - return ph; -} - -std::vector ElfPatcher::_buildEntryStub(const std::uint64_t stubVaddr, const std::uint64_t realEntryVaddr) const { - std::vector s; - s.push_back(0x58); - s.push_back(0x48); s.push_back(0x89); s.push_back(0xe3); - s.push_back(0x48); s.push_back(0x83); s.push_back(0xec); s.push_back(0x30); - s.push_back(0x48); s.push_back(0x83); s.push_back(0xe4); s.push_back(0xf0); - s.push_back(0x89); s.push_back(0x04); s.push_back(0x24); - s.push_back(0x48); s.push_back(0x89); s.push_back(0x5c); s.push_back(0x24); s.push_back(0x08); - s.push_back(0x48); s.push_back(0x89); s.push_back(0xe7); - s.push_back(0x48); s.push_back(0x31); s.push_back(0xf6); - const std::uint64_t jmpInsnVaddr = stubVaddr + s.size(); - const std::uint64_t jmpNextVaddr = jmpInsnVaddr + 5; - const std::int32_t rel32 = static_cast(realEntryVaddr - jmpNextVaddr); - s.push_back(0xe9); - s.push_back(static_cast(rel32 & 0xff)); - s.push_back(static_cast((rel32 >> 8) & 0xff)); - s.push_back(static_cast((rel32 >> 16) & 0xff)); - s.push_back(static_cast((rel32 >> 24) & 0xff)); - return s; -} - -std::uint32_t ElfPatcher::_fixLoadFlags(std::uint32_t originalFlags) const { - return originalFlags | PF_R; -} - -void ElfPatcher::_writeSectionHeader( - std::vector& buf, - const std::size_t offset, - const std::uint32_t nameOff, - const std::uint32_t type, - const std::uint64_t flags, - const std::uint64_t addr, - const std::uint64_t fileOffset, - const std::uint64_t size, - const std::uint32_t link, - const std::uint32_t info, - const std::uint64_t align, - const std::uint64_t entSize -) const { - _writeU32(buf, offset + 0, nameOff); - _writeU32(buf, offset + 4, type); - _writeU64(buf, offset + 8, flags); - _writeU64(buf, offset + 16, addr); - _writeU64(buf, offset + 24, fileOffset); - _writeU64(buf, offset + 32, size); - _writeU32(buf, offset + 40, link); - _writeU32(buf, offset + 44, info); - _writeU64(buf, offset + 48, align); - _writeU64(buf, offset + 56, entSize); -} - -bool ElfPatcher::_shouldSkip(const Domain::ProgramHeader& ph) const { - if (_isSceSpecificSegment(ph.Type)) return true; - if (ph.Type == PT_DYNAMIC) return true; - if (_isNullPageLoad(ph)) return true; - if (ph.Type == PT_NOTE && ph.MappedAddress == 0 && ph.FileSize > 0) return true; - return false; + _byteWriter->AppendI64(buf, tag); + _byteWriter->AppendU64(buf, val); } std::vector ElfPatcher::Patch( @@ -161,9 +34,9 @@ std::vector ElfPatcher::Patch( buf[8] = 0; buf[0x10] = static_cast(ET_DYN & 0xFF); buf[0x11] = static_cast((ET_DYN >> 8) & 0xFF); - _writeU64(buf, 40, 0); - _writeU16(buf, 60, 0); - _writeU16(buf, 62, 0); + _byteWriter->WriteU64(buf, 40, 0); + _byteWriter->WriteU16(buf, 60, 0); + _byteWriter->WriteU16(buf, 62, 0); const std::uint64_t phOff = *reinterpret_cast(buf.data() + 32); const std::uint16_t phEntSize = *reinterpret_cast(buf.data() + 54); @@ -174,22 +47,22 @@ std::vector ElfPatcher::Patch( b.push_back(0); }; - const std::uint64_t dynStrOff = static_cast(buf.size()); + const auto dynStrOff = static_cast(buf.size()); for (std::uint8_t b : dynSection.DynStrData) buf.push_back(b); alignBuf(buf, 24); - const std::uint64_t dynSymOff = static_cast(buf.size()); + const auto dynSymOff = static_cast(buf.size()); for (std::uint8_t b : dynSection.DynSymData) buf.push_back(b); alignBuf(buf, 24); - const std::uint64_t relaOff = static_cast(buf.size()); + const auto relaOff = static_cast(buf.size()); for (std::uint8_t b : dynSection.RelaData) buf.push_back(b); alignBuf(buf, 24); - const std::uint64_t relaPltOff = static_cast(buf.size()); + const auto relaPltOff = static_cast(buf.size()); for (std::uint8_t b : dynSection.RelaPltData) buf.push_back(b); alignBuf(buf, 8); @@ -213,148 +86,52 @@ std::vector ElfPatcher::Patch( } _appendDynEntry(dynSegBuf, DT_NULL, 0); - const std::uint64_t dynSegOff = static_cast(buf.size()); + const auto dynSegOff = static_cast(buf.size()); for (std::uint8_t b : dynSegBuf) buf.push_back(b); const std::uint64_t realEntryVaddr = *reinterpret_cast(buf.data() + 24); - const std::uint64_t stubOff = static_cast(buf.size()); - const auto stubBytes = _buildEntryStub(stubOff, realEntryVaddr); + const auto stubOff = static_cast(buf.size()); + const auto stubBytes = _entryStubBuilder->BuildEntryStub(stubOff, realEntryVaddr); for (std::uint8_t b : stubBytes) buf.push_back(b); - _writeU64(buf, 24, stubOff); + _byteWriter->WriteU64(buf, 24, stubOff); static constexpr char kInterp[] = "/lib64/ld-linux-x86-64.so.2"; - const std::uint64_t interpOff = static_cast(buf.size()); + const auto interpOff = static_cast(buf.size()); constexpr std::uint64_t interpSize = sizeof(kInterp); - for (std::size_t i = 0; i < interpSize; i++) - buf.push_back(static_cast(kInterp[i])); - - std::uint16_t keptCount = 0; - std::uint64_t keptMinVaddr = UINT64_MAX; - std::uint64_t keptMinVaddrAlign = 0x1000; - for (const auto& ph : originalHeaders) { - if (_shouldSkip(ph)) - continue; - keptCount++; - if (ph.Type == PT_LOAD && ph.MappedAddress < keptMinVaddr) { - keptMinVaddr = ph.MappedAddress; - keptMinVaddrAlign = ph.Alignment; - } - } - if (keptMinVaddr == UINT64_MAX) - throw Domain::RelinkerException("No kept PT_LOAD segment to anchor the header block against"); - if (keptMinVaddrAlign == 0) - throw Domain::RelinkerException("Lowest kept PT_LOAD has zero alignment"); - - const std::uint16_t neededPh = keptCount + 4; - if (neededPh > phNum) - throw Domain::RelinkerException( - "Not enough program header slots: need " + std::to_string(neededPh) + - ", available " + std::to_string(phNum)); - - const std::uint64_t headerBlockSize = phOff + static_cast(neededPh) * phEntSize; - if (headerBlockSize >= keptMinVaddr) - throw Domain::RelinkerException( - "Header block does not fit below the lowest kept PT_LOAD: need " + - std::to_string(headerBlockSize) + " bytes, only " + std::to_string(keptMinVaddr) + " available"); - - const std::uint64_t headerBlockVaddr = (keptMinVaddr - headerBlockSize) & ~(keptMinVaddrAlign - 1); - if (headerBlockVaddr + headerBlockSize > keptMinVaddr) - throw Domain::RelinkerException("Header block would overlap the lowest kept PT_LOAD after alignment"); + for (char i : kInterp) + buf.push_back(static_cast(i)); const std::uint64_t extraBlockOff = dynStrOff; const std::uint64_t extraBlockSize = static_cast(buf.size()) - extraBlockOff; - std::uint16_t writtenPh = 0; + ProgramHeaderLayoutRequest layoutRequest{}; + layoutRequest.PhOff = phOff; + layoutRequest.PhEntSize = phEntSize; + layoutRequest.PhNum = phNum; + layoutRequest.OriginalHeaders = originalHeaders; + layoutRequest.ExtraBlockOffset = extraBlockOff; + layoutRequest.ExtraBlockSize = extraBlockSize; + layoutRequest.DynamicSegmentOffset = dynSegOff; + layoutRequest.DynamicSegmentSize = dynSegBuf.size(); + layoutRequest.InterpOffset = interpOff; + layoutRequest.InterpSize = interpSize; - const std::size_t phdrEntOff = static_cast(phOff) + writtenPh * phEntSize; - _writeProgramHeader(buf, phdrEntOff, _makePhdrHeader(phOff, headerBlockVaddr + phOff, static_cast(neededPh) * phEntSize)); - writtenPh++; + const std::uint16_t writtenPh = _programHeaderLayoutBuilder->WriteLayout(buf, layoutRequest); + _byteWriter->WriteU16(buf, 56, writtenPh); - const std::size_t headerLoadEntOff = static_cast(phOff) + writtenPh * phEntSize; - _writeProgramHeader(buf, headerLoadEntOff, _makeHeaderBlockLoad(headerBlockVaddr, headerBlockSize, keptMinVaddrAlign)); - writtenPh++; + SectionHeaderTableRequest sectionRequest{}; + sectionRequest.DynStrOffset = dynStrOff; + sectionRequest.DynStrSize = dynSection.DynStrData.size(); + sectionRequest.DynSymOffset = dynSymOff; + sectionRequest.DynSymSize = dynSection.DynSymData.size(); + sectionRequest.DynamicSegmentOffset = dynSegOff; + sectionRequest.DynamicSegmentSize = dynSegBuf.size(); + sectionRequest.StubOffset = stubOff; + sectionRequest.StubSize = stubBytes.size(); - for (const auto& ph : originalHeaders) { - if (_shouldSkip(ph)) - continue; - const std::size_t phEntOff = static_cast(phOff) + writtenPh * phEntSize; - if (ph.Type == PT_LOAD) { - Domain::ProgramHeader fixed = ph; - fixed.Flags = _fixLoadFlags(ph.Flags); - _writeProgramHeader(buf, phEntOff, fixed); - } else { - _writeProgramHeader(buf, phEntOff, ph); - } - writtenPh++; - } - - { - const std::size_t phEntOff = static_cast(phOff) + writtenPh * phEntSize; - _writeProgramHeader(buf, phEntOff, _makeLoadHeader(extraBlockOff, extraBlockSize)); - writtenPh++; - } - - { - const std::size_t phEntOff = static_cast(phOff) + writtenPh * phEntSize; - Domain::ProgramHeader dynPh{}; - dynPh.Type = PT_DYNAMIC; - dynPh.Flags = PF_R | PF_W; - dynPh.Offset = dynSegOff; - dynPh.MappedAddress = dynSegOff; - dynPh.PhysicalAddress = dynSegOff; - dynPh.FileSize = dynSegBuf.size(); - dynPh.MemorySize = dynSegBuf.size(); - dynPh.Alignment = 8; - _writeProgramHeader(buf, phEntOff, dynPh); - writtenPh++; - } - - { - const std::size_t phEntOff = static_cast(phOff) + writtenPh * phEntSize; - Domain::ProgramHeader interpPh{}; - interpPh.Type = PT_INTERP; - interpPh.Flags = PF_R; - interpPh.Offset = interpOff; - interpPh.MappedAddress = interpOff; - interpPh.PhysicalAddress = interpOff; - interpPh.FileSize = interpSize; - interpPh.MemorySize = interpSize; - interpPh.Alignment = 1; - _writeProgramHeader(buf, phEntOff, interpPh); - writtenPh++; - } - - _writeU16(buf, 56, writtenPh); - - static constexpr char kShStrTab[] = "\0.shstrtab\0.dynstr\0.dynsym\0.dynamic\0.text"; - constexpr std::uint32_t nameShStrTab = 1; - constexpr std::uint32_t nameDynStr = 11; - constexpr std::uint32_t nameDynSym = 19; - constexpr std::uint32_t nameDynamic = 27; - constexpr std::uint32_t nameText = 36; - const std::uint64_t shStrTabOff = static_cast(buf.size()); - constexpr std::uint64_t shStrTabSize = sizeof(kShStrTab); - for (std::size_t i = 0; i < shStrTabSize; i++) - buf.push_back(static_cast(kShStrTab[i])); - - const std::uint64_t shOff = static_cast(buf.size()); - constexpr std::size_t shEntSize = 64; - constexpr std::size_t shNum = 6; - buf.resize(buf.size() + shEntSize * shNum, 0); - - _writeSectionHeader(buf, shOff + shEntSize * 0, 0, SHT_NULL, 0, 0, 0, 0, 0, 0, 0, 0); - _writeSectionHeader(buf, shOff + shEntSize * 1, nameShStrTab, SHT_STRTAB, 0, shStrTabOff, shStrTabOff, shStrTabSize, 0, 0, 1, 0); - _writeSectionHeader(buf, shOff + shEntSize * 2, nameDynStr, SHT_STRTAB, SHF_ALLOC, dynStrOff, dynStrOff, dynSection.DynStrData.size(), 0, 0, 1, 0); - _writeSectionHeader(buf, shOff + shEntSize * 3, nameDynSym, SHT_DYNSYM, SHF_ALLOC, dynSymOff, dynSymOff, dynSection.DynSymData.size(), 2, 1, 8, 24); - _writeSectionHeader(buf, shOff + shEntSize * 4, nameDynamic, SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE, dynSegOff, dynSegOff, dynSegBuf.size(), 2, 0, 8, 16); - _writeSectionHeader(buf, shOff + shEntSize * 5, nameText, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, stubOff, stubOff, stubBytes.size(), 0, 0, 1, 0); - - _writeU64(buf, 40, shOff); - _writeU16(buf, 58, static_cast(shEntSize)); - _writeU16(buf, 60, static_cast(shNum)); - _writeU16(buf, 62, 1); + _sectionHeaderTableBuilder->WriteTable(buf, sectionRequest); return buf; } diff --git a/core/elfpatcher/src/filtering/SegmentFilter.cpp b/core/elfpatcher/src/filtering/SegmentFilter.cpp new file mode 100644 index 000000000..314508714 --- /dev/null +++ b/core/elfpatcher/src/filtering/SegmentFilter.cpp @@ -0,0 +1,25 @@ +#include +#include + +namespace Elfpatcher { + +bool SegmentFilter::_isSceSpecificSegment(const std::uint32_t type) const { + return type == PT_SCE_DYNLIBDATA + || type == PT_OS_PROCPARAM + || type == PT_OS_RELRO + || (type >= 0x61000000 && type <= 0x6fffffff); +} + +bool SegmentFilter::_isNullPageLoad(const Domain::ProgramHeader& ph) const { + return ph.Type == PT_LOAD && ph.MappedAddress == 0; +} + +bool SegmentFilter::ShouldSkip(const Domain::ProgramHeader& ph) const { + if (_isSceSpecificSegment(ph.Type)) return true; + if (ph.Type == PT_DYNAMIC) return true; + if (_isNullPageLoad(ph)) return true; + if (ph.Type == PT_NOTE && ph.MappedAddress == 0 && ph.FileSize > 0) return true; + return false; +} + +} diff --git a/core/elfpatcher/src/layout/ProgramHeaderLayoutBuilder.cpp b/core/elfpatcher/src/layout/ProgramHeaderLayoutBuilder.cpp new file mode 100644 index 000000000..c8c605b00 --- /dev/null +++ b/core/elfpatcher/src/layout/ProgramHeaderLayoutBuilder.cpp @@ -0,0 +1,180 @@ +#include +#include +#include +#include +#include + +namespace Elfpatcher { + +ProgramHeaderLayoutBuilder::ProgramHeaderLayoutBuilder( + std::shared_ptr segmentFilter, + std::shared_ptr byteWriter +) + : _segmentFilter(std::move(segmentFilter)) + , _byteWriter(std::move(byteWriter)) +{ +} + +void ProgramHeaderLayoutBuilder::_writeProgramHeader(std::vector& buf, std::size_t offset, const Domain::ProgramHeader& ph) const { + _byteWriter->WriteU32(buf, offset + 0, ph.Type); + _byteWriter->WriteU32(buf, offset + 4, ph.Flags); + _byteWriter->WriteU64(buf, offset + 8, ph.Offset); + _byteWriter->WriteU64(buf, offset + 16, ph.MappedAddress); + _byteWriter->WriteU64(buf, offset + 24, ph.PhysicalAddress); + _byteWriter->WriteU64(buf, offset + 32, ph.FileSize); + _byteWriter->WriteU64(buf, offset + 40, ph.MemorySize); + _byteWriter->WriteU64(buf, offset + 48, ph.Alignment); +} + +Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makeLoadHeader(std::uint64_t offset, std::uint64_t size) const { + Domain::ProgramHeader ph{}; + ph.Type = PT_LOAD; + ph.Flags = PF_R | PF_W; + ph.Offset = offset; + ph.MappedAddress = offset; + ph.PhysicalAddress = offset; + ph.FileSize = size; + ph.MemorySize = size; + ph.Alignment = 0x1000; + return ph; +} + +Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makeHeaderBlockLoad(std::uint64_t vaddr, std::uint64_t size, std::uint64_t align) const { + Domain::ProgramHeader ph{}; + ph.Type = PT_LOAD; + ph.Flags = PF_R; + ph.Offset = 0; + ph.MappedAddress = vaddr; + ph.PhysicalAddress = vaddr; + ph.FileSize = size; + ph.MemorySize = size; + ph.Alignment = align; + return ph; +} + +Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makePhdrHeader(std::uint64_t offset, std::uint64_t vaddr, std::uint64_t size) const { + Domain::ProgramHeader ph{}; + ph.Type = PT_PHDR; + ph.Flags = PF_R; + ph.Offset = offset; + ph.MappedAddress = vaddr; + ph.PhysicalAddress = vaddr; + ph.FileSize = size; + ph.MemorySize = size; + ph.Alignment = 8; + return ph; +} + +Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makeDynamicHeader(std::uint64_t offset, std::uint64_t size) const { + Domain::ProgramHeader ph{}; + ph.Type = PT_DYNAMIC; + ph.Flags = PF_R | PF_W; + ph.Offset = offset; + ph.MappedAddress = offset; + ph.PhysicalAddress = offset; + ph.FileSize = size; + ph.MemorySize = size; + ph.Alignment = 8; + return ph; +} + +Domain::ProgramHeader ProgramHeaderLayoutBuilder::_makeInterpHeader(std::uint64_t offset, std::uint64_t size) const { + Domain::ProgramHeader ph{}; + ph.Type = PT_INTERP; + ph.Flags = PF_R; + ph.Offset = offset; + ph.MappedAddress = offset; + ph.PhysicalAddress = offset; + ph.FileSize = size; + ph.MemorySize = size; + ph.Alignment = 1; + return ph; +} + +std::uint32_t ProgramHeaderLayoutBuilder::_fixLoadFlags(std::uint32_t originalFlags) const { + return originalFlags | PF_R; +} + +std::uint16_t ProgramHeaderLayoutBuilder::WriteLayout( + std::vector& buf, + const ProgramHeaderLayoutRequest& request +) const { + std::uint16_t keptCount = 0; + std::uint64_t keptMinVaddr = UINT64_MAX; + std::uint64_t keptMinVaddrAlign = 0x1000; + for (const auto& ph : request.OriginalHeaders) { + if (_segmentFilter->ShouldSkip(ph)) + continue; + keptCount++; + if (ph.Type == PT_LOAD && ph.MappedAddress < keptMinVaddr) { + keptMinVaddr = ph.MappedAddress; + keptMinVaddrAlign = ph.Alignment; + } + } + if (keptMinVaddr == UINT64_MAX) + throw Domain::RelinkerException("No kept PT_LOAD segment to anchor the header block against"); + if (keptMinVaddrAlign == 0) + throw Domain::RelinkerException("Lowest kept PT_LOAD has zero alignment"); + + const std::uint16_t neededPh = keptCount + 4; + if (neededPh > request.PhNum) + throw Domain::RelinkerException( + "Not enough program header slots: need " + std::to_string(neededPh) + + ", available " + std::to_string(request.PhNum)); + + const std::uint64_t headerBlockSize = request.PhOff + static_cast(neededPh) * request.PhEntSize; + if (headerBlockSize >= keptMinVaddr) + throw Domain::RelinkerException( + "Header block does not fit below the lowest kept PT_LOAD: need " + + std::to_string(headerBlockSize) + " bytes, only " + std::to_string(keptMinVaddr) + " available"); + + const std::uint64_t headerBlockVaddr = (keptMinVaddr - headerBlockSize) & ~(keptMinVaddrAlign - 1); + if (headerBlockVaddr + headerBlockSize > keptMinVaddr) + throw Domain::RelinkerException("Header block would overlap the lowest kept PT_LOAD after alignment"); + + std::uint16_t writtenPh = 0; + + const std::size_t phdrEntOff = static_cast(request.PhOff) + writtenPh * request.PhEntSize; + _writeProgramHeader(buf, phdrEntOff, _makePhdrHeader(request.PhOff, headerBlockVaddr + request.PhOff, static_cast(neededPh) * request.PhEntSize)); + writtenPh++; + + const std::size_t headerLoadEntOff = static_cast(request.PhOff) + writtenPh * request.PhEntSize; + _writeProgramHeader(buf, headerLoadEntOff, _makeHeaderBlockLoad(headerBlockVaddr, headerBlockSize, keptMinVaddrAlign)); + writtenPh++; + + for (const auto& ph : request.OriginalHeaders) { + if (_segmentFilter->ShouldSkip(ph)) + continue; + const std::size_t phEntOff = static_cast(request.PhOff) + writtenPh * request.PhEntSize; + if (ph.Type == PT_LOAD) { + Domain::ProgramHeader fixed = ph; + fixed.Flags = _fixLoadFlags(ph.Flags); + _writeProgramHeader(buf, phEntOff, fixed); + } else { + _writeProgramHeader(buf, phEntOff, ph); + } + writtenPh++; + } + + { + const std::size_t phEntOff = static_cast(request.PhOff) + writtenPh * request.PhEntSize; + _writeProgramHeader(buf, phEntOff, _makeLoadHeader(request.ExtraBlockOffset, request.ExtraBlockSize)); + writtenPh++; + } + + { + const std::size_t phEntOff = static_cast(request.PhOff) + writtenPh * request.PhEntSize; + _writeProgramHeader(buf, phEntOff, _makeDynamicHeader(request.DynamicSegmentOffset, request.DynamicSegmentSize)); + writtenPh++; + } + + { + const std::size_t phEntOff = static_cast(request.PhOff) + writtenPh * request.PhEntSize; + _writeProgramHeader(buf, phEntOff, _makeInterpHeader(request.InterpOffset, request.InterpSize)); + writtenPh++; + } + + return writtenPh; +} + +} diff --git a/core/elfpatcher/src/sections/SectionHeaderTableBuilder.cpp b/core/elfpatcher/src/sections/SectionHeaderTableBuilder.cpp new file mode 100644 index 000000000..4686ff101 --- /dev/null +++ b/core/elfpatcher/src/sections/SectionHeaderTableBuilder.cpp @@ -0,0 +1,70 @@ +#include +#include + +namespace Elfpatcher { + +SectionHeaderTableBuilder::SectionHeaderTableBuilder(std::shared_ptr byteWriter) + : _byteWriter(std::move(byteWriter)) +{ +} + +void SectionHeaderTableBuilder::_writeSectionHeader( + std::vector& buf, + const std::size_t offset, + const std::uint32_t nameOff, + const std::uint32_t type, + const std::uint64_t flags, + const std::uint64_t addr, + const std::uint64_t fileOffset, + const std::uint64_t size, + const std::uint32_t link, + const std::uint32_t info, + const std::uint64_t align, + const std::uint64_t entSize +) const { + _byteWriter->WriteU32(buf, offset + 0, nameOff); + _byteWriter->WriteU32(buf, offset + 4, type); + _byteWriter->WriteU64(buf, offset + 8, flags); + _byteWriter->WriteU64(buf, offset + 16, addr); + _byteWriter->WriteU64(buf, offset + 24, fileOffset); + _byteWriter->WriteU64(buf, offset + 32, size); + _byteWriter->WriteU32(buf, offset + 40, link); + _byteWriter->WriteU32(buf, offset + 44, info); + _byteWriter->WriteU64(buf, offset + 48, align); + _byteWriter->WriteU64(buf, offset + 56, entSize); +} + +void SectionHeaderTableBuilder::WriteTable( + std::vector& buf, + const SectionHeaderTableRequest& request +) const { + static constexpr char kShStrTab[] = "\0.shstrtab\0.dynstr\0.dynsym\0.dynamic\0.text"; + constexpr std::uint32_t nameShStrTab = 1; + constexpr std::uint32_t nameDynStr = 11; + constexpr std::uint32_t nameDynSym = 19; + constexpr std::uint32_t nameDynamic = 27; + constexpr std::uint32_t nameText = 36; + const auto shStrTabOff = static_cast(buf.size()); + constexpr std::uint64_t shStrTabSize = sizeof(kShStrTab); + for (const char i : kShStrTab) + buf.push_back(static_cast(i)); + + const auto shOff = static_cast(buf.size()); + constexpr std::size_t shEntSize = 64; + constexpr std::size_t shNum = 6; + buf.resize(buf.size() + shEntSize * shNum, 0); + + _writeSectionHeader(buf, shOff + shEntSize * 0, 0, SHT_NULL, 0, 0, 0, 0, 0, 0, 0, 0); + _writeSectionHeader(buf, shOff + shEntSize * 1, nameShStrTab, SHT_STRTAB, 0, shStrTabOff, shStrTabOff, shStrTabSize, 0, 0, 1, 0); + _writeSectionHeader(buf, shOff + shEntSize * 2, nameDynStr, SHT_STRTAB, SHF_ALLOC, request.DynStrOffset, request.DynStrOffset, request.DynStrSize, 0, 0, 1, 0); + _writeSectionHeader(buf, shOff + shEntSize * 3, nameDynSym, SHT_DYNSYM, SHF_ALLOC, request.DynSymOffset, request.DynSymOffset, request.DynSymSize, 2, 1, 8, 24); + _writeSectionHeader(buf, shOff + shEntSize * 4, nameDynamic, SHT_DYNAMIC, SHF_ALLOC | SHF_WRITE, request.DynamicSegmentOffset, request.DynamicSegmentOffset, request.DynamicSegmentSize, 2, 0, 8, 16); + _writeSectionHeader(buf, shOff + shEntSize * 5, nameText, SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR, request.StubOffset, request.StubOffset, request.StubSize, 0, 0, 1, 0); + + _byteWriter->WriteU64(buf, 40, shOff); + _byteWriter->WriteU16(buf, 58, static_cast(shEntSize)); + _byteWriter->WriteU16(buf, 60, static_cast(shNum)); + _byteWriter->WriteU16(buf, 62, 1); +} + +} diff --git a/core/elfpatcher/src/stub/EntryStubBuilder.cpp b/core/elfpatcher/src/stub/EntryStubBuilder.cpp new file mode 100644 index 000000000..d4b452e00 --- /dev/null +++ b/core/elfpatcher/src/stub/EntryStubBuilder.cpp @@ -0,0 +1,29 @@ +#include + +namespace Elfpatcher { + +std::vector EntryStubBuilder::BuildEntryStub( + std::uint64_t stubVaddr, + std::uint64_t realEntryVaddr +) const { + std::vector s; + s.push_back(0x58); + s.push_back(0x48); s.push_back(0x89); s.push_back(0xe3); + s.push_back(0x48); s.push_back(0x83); s.push_back(0xec); s.push_back(0x30); + s.push_back(0x48); s.push_back(0x83); s.push_back(0xe4); s.push_back(0xf0); + s.push_back(0x89); s.push_back(0x04); s.push_back(0x24); + s.push_back(0x48); s.push_back(0x89); s.push_back(0x5c); s.push_back(0x24); s.push_back(0x08); + s.push_back(0x48); s.push_back(0x89); s.push_back(0xe7); + s.push_back(0x48); s.push_back(0x31); s.push_back(0xf6); + const std::uint64_t jmpInsnVaddr = stubVaddr + s.size(); + const std::uint64_t jmpNextVaddr = jmpInsnVaddr + 5; + const std::int32_t rel32 = static_cast(realEntryVaddr - jmpNextVaddr); + s.push_back(0xe9); + s.push_back(static_cast(rel32 & 0xff)); + s.push_back(static_cast((rel32 >> 8) & 0xff)); + s.push_back(static_cast((rel32 >> 16) & 0xff)); + s.push_back(static_cast((rel32 >> 24) & 0xff)); + return s; +} + +} diff --git a/core/io/include/io/ByteWriter.hpp b/core/io/include/io/ByteWriter.hpp new file mode 100644 index 000000000..df485e5c0 --- /dev/null +++ b/core/io/include/io/ByteWriter.hpp @@ -0,0 +1,19 @@ +#ifndef IO_BYTEWRITER_HPP +#define IO_BYTEWRITER_HPP + +#include + +namespace Io { + +class ByteWriter : public IByteWriter { +public: + void WriteU16(std::vector& buf, std::size_t offset, std::uint16_t v) const override; + void WriteU32(std::vector& buf, std::size_t offset, std::uint32_t v) const override; + void WriteU64(std::vector& buf, std::size_t offset, std::uint64_t v) const override; + void AppendU64(std::vector& buf, std::uint64_t v) const override; + void AppendI64(std::vector& buf, std::int64_t v) const override; +}; + +} + +#endif diff --git a/core/io/include/io/IByteWriter.hpp b/core/io/include/io/IByteWriter.hpp new file mode 100644 index 000000000..c53c06d95 --- /dev/null +++ b/core/io/include/io/IByteWriter.hpp @@ -0,0 +1,22 @@ +#ifndef IO_IBYTEWRITER_HPP +#define IO_IBYTEWRITER_HPP + +#include +#include + +namespace Io { + +class IByteWriter { +public: + virtual ~IByteWriter() = default; + + virtual void WriteU16(std::vector& buf, std::size_t offset, std::uint16_t v) const = 0; + virtual void WriteU32(std::vector& buf, std::size_t offset, std::uint32_t v) const = 0; + virtual void WriteU64(std::vector& buf, std::size_t offset, std::uint64_t v) const = 0; + virtual void AppendU64(std::vector& buf, std::uint64_t v) const = 0; + virtual void AppendI64(std::vector& buf, std::int64_t v) const = 0; +}; + +} + +#endif diff --git a/core/io/src/ByteWriter.cpp b/core/io/src/ByteWriter.cpp new file mode 100644 index 000000000..f242f70ca --- /dev/null +++ b/core/io/src/ByteWriter.cpp @@ -0,0 +1,28 @@ +#include +#include + +namespace Io { + +void ByteWriter::WriteU16(std::vector& buf, std::size_t offset, std::uint16_t v) const { + std::memcpy(buf.data() + offset, &v, 2); +} + +void ByteWriter::WriteU32(std::vector& buf, std::size_t offset, std::uint32_t v) const { + std::memcpy(buf.data() + offset, &v, 4); +} + +void ByteWriter::WriteU64(std::vector& buf, std::size_t offset, std::uint64_t v) const { + std::memcpy(buf.data() + offset, &v, 8); +} + +void ByteWriter::AppendU64(std::vector& buf, std::uint64_t v) const { + const std::size_t pos = buf.size(); + buf.resize(pos + 8); + std::memcpy(buf.data() + pos, &v, 8); +} + +void ByteWriter::AppendI64(std::vector& buf, std::int64_t v) const { + AppendU64(buf, static_cast(v)); +} + +} diff --git a/core/main.cpp b/core/main.cpp index 6ddb34387..f49c7e96b 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -2,6 +2,11 @@ #include #include #include +#include +#include +#include +#include +#include #include #include #include @@ -76,7 +81,16 @@ int main(const int argc, char* argv[]) { fileWriter.Write(*registryPath, callRegistryWriter->WriteCallRegistry(result.RegistryEntries)); } - Elfpatcher::ElfPatcher elfPatcher; + auto byteWriter = std::make_shared(); + Elfpatcher::ElfPatcher elfPatcher( + std::make_shared(), + std::make_shared( + std::make_shared(), + byteWriter + ), + std::make_shared(byteWriter), + byteWriter + ); auto patchedElf = elfPatcher.Patch(sourceBytes, result.OriginalHeaders, result.DynamicSection); fileWriter.Write(outputElfPath, patchedElf);