diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 006738d94..13be7368a 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -265,6 +265,7 @@ if(BUILD_TESTING) ) target_include_directories(strict_nid_filter_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/domain/include + ${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/include ${CMAKE_CURRENT_SOURCE_DIR}/codegen/include ${CMAKE_CURRENT_SOURCE_DIR}/relinker/include ) diff --git a/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp b/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp index c6f63f41d..c67ededd1 100644 --- a/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp +++ b/core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp @@ -87,12 +87,59 @@ 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::int64_t DT_INIT = 12; +inline constexpr std::int64_t DT_FINI = 13; +inline constexpr std::int64_t DT_INIT_ARRAY = 25; +inline constexpr std::int64_t DT_FINI_ARRAY = 26; +inline constexpr std::int64_t DT_INIT_ARRAYSZ = 27; +inline constexpr std::int64_t DT_FINI_ARRAYSZ = 28; inline constexpr std::int64_t DT_RUNPATH = 29; inline constexpr std::int64_t DT_FLAGS = 30; inline constexpr std::uint64_t DF_BIND_NOW = 0x8; +inline constexpr std::int64_t DT_OS_INIT = 0x6000000c; +inline constexpr std::int64_t DT_OS_FINI = 0x6000000d; +inline constexpr std::int64_t DT_OS_INIT_ARRAY = 0x60000019; +inline constexpr std::int64_t DT_OS_FINI_ARRAY = 0x6000001a; +inline constexpr std::int64_t DT_OS_INIT_ARRAYSZ = 0x6000001b; +inline constexpr std::int64_t DT_OS_FINI_ARRAYSZ = 0x6000001c; +inline constexpr std::int64_t DT_OS_PLTGOT = 0x61000027; +inline constexpr std::int64_t DT_OS_JMPREL = 0x61000029; +inline constexpr std::int64_t DT_OS_PLTREL = 0x6100002b; +inline constexpr std::int64_t DT_OS_PLTRELSZ = 0x6100002d; +inline constexpr std::int64_t DT_OS_RELA = 0x6100002f; +inline constexpr std::int64_t DT_OS_RELASZ = 0x61000031; +inline constexpr std::int64_t DT_OS_RELAENT = 0x61000033; +inline constexpr std::int64_t DT_OS_STRTAB = 0x61000035; +inline constexpr std::int64_t DT_OS_STRSZ = 0x61000037; +inline constexpr std::int64_t DT_OS_SYMTAB = 0x61000039; +inline constexpr std::int64_t DT_OS_SYMENT = 0x6100003b; +inline constexpr std::int64_t DT_OS_SYMTABSZ = 0x6100003f; + +inline constexpr std::uint32_t R_X86_64_NONE = 0; +inline constexpr std::uint32_t R_X86_64_64 = 1; +inline constexpr std::uint32_t R_X86_64_PC32 = 2; +inline constexpr std::uint32_t R_X86_64_GOT32 = 3; +inline constexpr std::uint32_t R_X86_64_PLT32 = 4; +inline constexpr std::uint32_t R_X86_64_COPY = 5; +inline constexpr std::uint32_t R_X86_64_GLOB_DAT = 6; +inline constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7; +inline constexpr std::uint32_t R_X86_64_RELATIVE = 8; +inline constexpr std::uint32_t R_X86_64_GOTPCREL = 9; +inline constexpr std::uint32_t R_X86_64_32 = 10; +inline constexpr std::uint32_t R_X86_64_32S = 11; +inline constexpr std::uint32_t R_X86_64_GOTPCRELX = 41; +inline constexpr std::uint32_t R_X86_64_REX_GOTPCRELX = 42; + +inline constexpr std::uint8_t STB_GLOBAL = 1; +inline constexpr std::uint8_t STB_WEAK = 2; +inline constexpr std::uint8_t STT_FUNC = 2; +inline constexpr std::uint8_t STV_DEFAULT = 0; +inline constexpr std::uint16_t SHN_UNDEF = 0; + inline constexpr std::uint32_t SHT_NULL = 0; inline constexpr std::uint32_t SHT_PROGBITS = 1; +inline constexpr std::uint32_t SHT_SYMTAB = 2; inline constexpr std::uint32_t SHT_STRTAB = 3; inline constexpr std::uint32_t SHT_DYNAMIC = 6; inline constexpr std::uint32_t SHT_DYNSYM = 11; diff --git a/core/relinker/relinker/include/relinker/output/SysVDynamicSectionBuilder.hpp b/core/relinker/relinker/include/relinker/output/SysVDynamicSectionBuilder.hpp index 7816d126c..c75274640 100644 --- a/core/relinker/relinker/include/relinker/output/SysVDynamicSectionBuilder.hpp +++ b/core/relinker/relinker/include/relinker/output/SysVDynamicSectionBuilder.hpp @@ -15,25 +15,6 @@ public: ) override; private: - static constexpr std::uint32_t STB_GLOBAL = 1; - static constexpr std::uint32_t STT_FUNC = 2; - static constexpr std::uint32_t STV_DEFAULT = 0; - static constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7; - static constexpr std::uint32_t R_X86_64_GLOB_DAT = 6; - - static constexpr std::int64_t DT_NEEDED = 1; - static constexpr std::int64_t DT_SYMTAB = 6; - static constexpr std::int64_t DT_STRTAB = 5; - static constexpr std::int64_t DT_STRSZ = 10; - 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_JMPREL = 23; - static constexpr std::int64_t DT_PLTRELSZ = 2; - static constexpr std::int64_t DT_PLTREL = 20; - static constexpr std::int64_t DT_SYMENT = 11; - static constexpr std::int64_t DT_NULL = 0; - 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; diff --git a/core/relinker/relinker/include/relinker/pipeline/RelinkerPipeline.hpp b/core/relinker/relinker/include/relinker/pipeline/RelinkerPipeline.hpp index 67be9ac4b..78554168b 100644 --- a/core/relinker/relinker/include/relinker/pipeline/RelinkerPipeline.hpp +++ b/core/relinker/relinker/include/relinker/pipeline/RelinkerPipeline.hpp @@ -27,36 +27,6 @@ private: std::shared_ptr _unusedNidFilter; std::uint32_t unusedFilterLevel; - static constexpr std::uint32_t PT_LOAD = 1; - static constexpr std::uint32_t PT_DYNAMIC = 2; - static constexpr std::uint32_t PF_X = 0x1; - - static constexpr std::int64_t DT_NEEDED = 0x00000001; - static constexpr std::int64_t DT_PLTRELSZ = 0x00000002; - static constexpr std::int64_t DT_PLTGOT = 0x00000003; - static constexpr std::int64_t DT_STRTAB = 0x00000005; - static constexpr std::int64_t DT_SYMTAB = 0x00000006; - static constexpr std::int64_t DT_RELA = 0x00000007; - static constexpr std::int64_t DT_RELASZ = 0x00000008; - static constexpr std::int64_t DT_RELAENT = 0x00000009; - static constexpr std::int64_t DT_STRSZ = 0x0000000a; - static constexpr std::int64_t DT_SYMENT = 0x0000000b; - static constexpr std::int64_t DT_PLTREL = 0x00000014; - static constexpr std::int64_t DT_JMPREL = 0x00000017; - - static constexpr std::int64_t DT_OS_PLTGOT = 0x61000027; - static constexpr std::int64_t DT_OS_PLTREL = 0x6100002b; - static constexpr std::int64_t DT_OS_PLTRELSZ = 0x6100002d; - static constexpr std::int64_t DT_OS_STRTAB = 0x61000035; - static constexpr std::int64_t DT_OS_STRSZ = 0x61000037; - static constexpr std::int64_t DT_OS_SYMTAB = 0x61000039; - static constexpr std::int64_t DT_OS_SYMENT = 0x6100003b; - static constexpr std::int64_t DT_OS_SYMTABSZ = 0x6100003f; - static constexpr std::int64_t DT_OS_RELA = 0x6100002f; - static constexpr std::int64_t DT_OS_RELASZ = 0x61000031; - static constexpr std::int64_t DT_OS_RELAENT = 0x61000033; - static constexpr std::int64_t DT_OS_JMPREL = 0x61000029; - static std::string _relocationTypeName(std::uint32_t type); }; diff --git a/core/relinker/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp b/core/relinker/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp index c2225509f..d24bcaf3d 100644 --- a/core/relinker/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp +++ b/core/relinker/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp @@ -1,8 +1,11 @@ #include +#include #include namespace Relinker::UnusedNidFilter { +using namespace Elfpatcher; + namespace { std::uint16_t read16(const std::vector& b, std::size_t off) { @@ -65,7 +68,6 @@ public: std::uint32_t type = read32(elfBytes, phPos); - constexpr std::uint32_t PT_DYNAMIC = 2; if (type != PT_DYNAMIC) continue; std::uint64_t segOff = read64(elfBytes, phPos + 8); @@ -73,20 +75,6 @@ public: if (segOff + segSz > elfBytes.size()) throw RelinkerException("PT_DYNAMIC segment out of bounds"); - constexpr std::int64_t DT_NULL = 0; - constexpr std::int64_t DT_INIT = 12; - constexpr std::int64_t DT_FINI = 13; - constexpr std::int64_t DT_INIT_ARRAY = 25; - constexpr std::int64_t DT_INIT_ARRAYSZ = 27; - constexpr std::int64_t DT_FINI_ARRAY = 26; - constexpr std::int64_t DT_FINI_ARRAYSZ = 28; - constexpr std::int64_t DT_OS_INIT_ARRAY = 0x60000019; - constexpr std::int64_t DT_OS_INIT_ARRAYSZ = 0x6000001b; - constexpr std::int64_t DT_OS_FINI_ARRAY = 0x6000001a; - constexpr std::int64_t DT_OS_FINI_ARRAYSZ = 0x6000001c; - constexpr std::int64_t DT_OS_INIT = 0x6000000c; - constexpr std::int64_t DT_OS_FINI = 0x6000000d; - VirtualAddress initArrayVa = 0; std::uint64_t initArraySz = 0; VirtualAddress finiArrayVa = 0; @@ -142,8 +130,6 @@ public: if (shPos + 64 > elfBytes.size()) break; std::uint32_t shType = read32(elfBytes, shPos + 4); - constexpr std::uint32_t SHT_DYNSYM = 11; - constexpr std::uint32_t SHT_SYMTAB = 2; if (shType != SHT_DYNSYM && shType != SHT_SYMTAB) continue; std::uint64_t symOff = read64(elfBytes, shPos + 24); @@ -172,10 +158,6 @@ public: std::uint8_t stType = info & 0xF; std::uint16_t shndx = read16(elfBytes, sPos + 6); std::uint64_t symVal = read64(elfBytes, sPos + 8); - constexpr std::uint8_t STB_GLOBAL = 1; - constexpr std::uint8_t STB_WEAK = 2; - constexpr std::uint8_t STT_FUNC = 2; - constexpr std::uint16_t SHN_UNDEF = 0; if ((stBind == STB_GLOBAL || stBind == STB_WEAK) && stType == STT_FUNC && shndx != SHN_UNDEF) { addIfInText(symVal); diff --git a/core/relinker/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp b/core/relinker/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp index 9af486615..fd508b987 100644 --- a/core/relinker/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp +++ b/core/relinker/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp @@ -1,9 +1,12 @@ #include +#include #include #include namespace Relinker::UnusedNidFilter { +using namespace Elfpatcher; + namespace { std::uint16_t read16(const std::vector& b, std::size_t off) { @@ -38,7 +41,6 @@ std::uint64_t vaddrToFileOffset(const std::vector& loads, VirtualAd throw RelinkerException("Cannot translate virtual address to file offset", va); } -constexpr std::uint32_t R_X86_64_RELATIVE = 8; constexpr std::size_t RelaEntSize = 24; void collectRelativeEntries( @@ -105,7 +107,6 @@ std::unique_ptr BuildRelativeRelocationIndex( std::size_t phPos = static_cast(phOff) + i * phEntSize; if (phPos + 56 > elfBytes.size()) throw RelinkerException("Program header out of bounds"); std::uint32_t type = read32(elfBytes, phPos); - constexpr std::uint32_t PT_LOAD = 1; if (type != PT_LOAD) continue; LoadSegment seg; seg.fileOffset = read64(elfBytes, phPos + 8); @@ -121,23 +122,12 @@ std::unique_ptr BuildRelativeRelocationIndex( if (phPos + 56 > elfBytes.size()) throw RelinkerException("Program header out of bounds"); std::uint32_t type = read32(elfBytes, phPos); - constexpr std::uint32_t PT_DYNAMIC = 2; if (type != PT_DYNAMIC) continue; std::uint64_t segOff = read64(elfBytes, phPos + 8); std::uint64_t segSz = read64(elfBytes, phPos + 32); if (segOff + segSz > elfBytes.size()) throw RelinkerException("PT_DYNAMIC segment out of bounds"); - constexpr std::int64_t DT_NULL = 0; - constexpr std::int64_t DT_RELA = 7; - constexpr std::int64_t DT_RELASZ = 8; - constexpr std::int64_t DT_JMPREL = 0x17; - constexpr std::int64_t DT_PLTRELSZ = 2; - constexpr std::int64_t DT_OS_RELA = 0x6100002f; - constexpr std::int64_t DT_OS_RELASZ = 0x61000031; - constexpr std::int64_t DT_OS_JMPREL = 0x61000029; - constexpr std::int64_t DT_OS_PLTRELSZ = 0x6100002d; - VirtualAddress relaVa = 0; std::uint64_t relaSz = 0; VirtualAddress jmprelVa = 0; diff --git a/core/relinker/relinker/src/analysis/ValidationPolicy.cpp b/core/relinker/relinker/src/analysis/ValidationPolicy.cpp index 4a0a6a7cf..2f2f4f91b 100644 --- a/core/relinker/relinker/src/analysis/ValidationPolicy.cpp +++ b/core/relinker/relinker/src/analysis/ValidationPolicy.cpp @@ -1,8 +1,11 @@ #include "relinker/analysis//ValidationPolicy.hpp" +#include #include namespace Relinker { +using namespace Elfpatcher; + static constexpr std::uint8_t SYSCALL_BYTE0 = 0x0F; static constexpr std::uint8_t SYSCALL_BYTE1 = 0x05; static constexpr std::uint8_t INT80_BYTE0 = 0xCD; @@ -12,21 +15,6 @@ static constexpr std::uint8_t SYSENTER_BYTE1 = 0x34; static constexpr std::uint8_t SYSRET_BYTE0 = 0x0F; static constexpr std::uint8_t SYSRET_BYTE1 = 0x07; -static constexpr std::uint32_t R_X86_64_NONE = 0; -static constexpr std::uint32_t R_X86_64_64 = 1; -static constexpr std::uint32_t R_X86_64_PC32 = 2; -static constexpr std::uint32_t R_X86_64_GOT32 = 3; -static constexpr std::uint32_t R_X86_64_PLT32 = 4; -static constexpr std::uint32_t R_X86_64_COPY = 5; -static constexpr std::uint32_t R_X86_64_GLOB_DAT = 6; -static constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7; -static constexpr std::uint32_t R_X86_64_RELATIVE = 8; -static constexpr std::uint32_t R_X86_64_GOTPCREL = 9; -static constexpr std::uint32_t R_X86_64_32 = 10; -static constexpr std::uint32_t R_X86_64_32S = 11; -static constexpr std::uint32_t R_X86_64_GOTPCRELX = 41; -static constexpr std::uint32_t R_X86_64_REX_GOTPCRELX = 42; - void ValidationPolicy::_initializeSupportedRelocationTypes() { _supportedRelocationTypes = { R_X86_64_NONE, diff --git a/core/relinker/relinker/src/output/SysVDynamicSectionBuilder.cpp b/core/relinker/relinker/src/output/SysVDynamicSectionBuilder.cpp index 3e29923b9..132d49e72 100644 --- a/core/relinker/relinker/src/output/SysVDynamicSectionBuilder.cpp +++ b/core/relinker/relinker/src/output/SysVDynamicSectionBuilder.cpp @@ -1,8 +1,11 @@ #include +#include #include namespace Relinker { +using namespace Elfpatcher; + void SysVDynamicSectionBuilder::_appendU64(std::vector& buf, std::uint64_t v) const { std::size_t pos = buf.size(); buf.resize(pos + 8); diff --git a/core/relinker/relinker/src/parsing/ElfReader.cpp b/core/relinker/relinker/src/parsing/ElfReader.cpp index ba8992b17..779dee5a1 100644 --- a/core/relinker/relinker/src/parsing/ElfReader.cpp +++ b/core/relinker/relinker/src/parsing/ElfReader.cpp @@ -1,9 +1,12 @@ #include +#include #include #include namespace Relinker { +using namespace Elfpatcher; + namespace { bool _rangeFits(const std::uint64_t offset, const std::uint64_t length, const std::size_t size) { @@ -201,8 +204,6 @@ FileByteOffset ElfReader::TranslateVirtualAddress(VirtualAddress address) const const VirtualAddress segVAddr = _readU64At(offset + 0x10); const ByteCount segFileSize = _readU64At(offset + 0x20); - static constexpr std::uint32_t PT_LOAD = 1; - if (type == PT_LOAD && address >= segVAddr && address < segVAddr + segFileSize) { return segOffset + (address - segVAddr); } diff --git a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp index 2438ce6b3..9e301e7eb 100644 --- a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp +++ b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -7,6 +8,8 @@ namespace Relinker { +using namespace Elfpatcher; + RelinkerPipeline::RelinkerPipeline(std::shared_ptr elfReader, std::shared_ptr syscallScanner, std::shared_ptr callSiteResolver, std::shared_ptr validationPolicy, std::shared_ptr dynamicSectionBuilder, std::shared_ptr unusedNidFilter, std::uint32_t unusedFilterLevel) : _elfReader(std::move(elfReader)) , _syscallScanner(std::move(syscallScanner)) @@ -218,8 +221,6 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf _validationPolicy->ValidateSyscallAbsence(); - static constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7; - const std::size_t originalNidCount = nidRefs.size(); const auto originalNidRefs = nidRefs; std::cout << "NID input: " << originalNidCount << " references\n"; @@ -273,8 +274,6 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf } auto dynSection = _dynamicSectionBuilder->BuildDynamicSection(dynamicRefs, neededLibraries, dynJmpRelOffset, pltCount); - static constexpr std::uint32_t R_X86_64_RELATIVE = 8; - auto appendRela = [&](std::vector& buf, std::uint64_t offset, std::uint64_t info, std::int64_t addend) { std::size_t pos = buf.size(); buf.resize(pos + 24);