diff --git a/core/elfpatcher/include/elfpatcher/general/IElfPatcher.hpp b/core/elfpatcher/include/elfpatcher/general/IElfPatcher.hpp index 6d5ed82d9..e7c2996e7 100644 --- a/core/elfpatcher/include/elfpatcher/general/IElfPatcher.hpp +++ b/core/elfpatcher/include/elfpatcher/general/IElfPatcher.hpp @@ -14,7 +14,8 @@ public: virtual std::vector Patch( const std::vector& sourceElf, const std::vector& originalHeaders, - const Domain::SysVDynamicSection& dynamicSection + const Domain::SysVDynamicSection& dynamicSection, + std::uint64_t originalPltGotVaddr ) = 0; }; diff --git a/core/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp b/core/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp index 030092ee2..c81e380f9 100644 --- a/core/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp +++ b/core/elfpatcher/include/elfpatcher/linux/LinuxElfPatcher.hpp @@ -22,7 +22,8 @@ public: std::vector Patch( const std::vector& sourceElf, const std::vector& originalHeaders, - const Domain::SysVDynamicSection& dynamicSection + const Domain::SysVDynamicSection& dynamicSection, + std::uint64_t originalPltGotVaddr ) override; private: diff --git a/core/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp b/core/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp index 77199ca21..5cc46fa6b 100644 --- a/core/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp +++ b/core/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp @@ -22,7 +22,8 @@ public: std::vector Patch( const std::vector& sourceElf, const std::vector& originalHeaders, - const Domain::SysVDynamicSection& dynamicSection + const Domain::SysVDynamicSection& dynamicSection, + std::uint64_t originalPltGotVaddr ) override; private: diff --git a/core/elfpatcher/src/linux/LinuxElfPatcher.cpp b/core/elfpatcher/src/linux/LinuxElfPatcher.cpp index 8358682a9..50a287def 100644 --- a/core/elfpatcher/src/linux/LinuxElfPatcher.cpp +++ b/core/elfpatcher/src/linux/LinuxElfPatcher.cpp @@ -26,7 +26,8 @@ void LinuxElfPatcher::_appendDynEntry(std::vector& buf, std::int64 std::vector LinuxElfPatcher::Patch( const std::vector& sourceElf, const std::vector& originalHeaders, - const Domain::SysVDynamicSection& dynSection) + const Domain::SysVDynamicSection& dynSection, + const std::uint64_t originalPltGotVaddr) { std::vector buf = sourceElf; @@ -67,11 +68,6 @@ std::vector LinuxElfPatcher::Patch( buf.push_back(b); alignBuf(buf, kRelaPltAlignment); - alignBuf(buf, kGotAlignment); - const auto gotOff = static_cast(buf.size()); - for (std::size_t i = 0; i < kGotReservedSlots; ++i) - _byteWriter->AppendU64(buf, 0); - const std::uint64_t extraBlockOff = dynStrOff; const std::uint64_t extraBlockVaddr = _programHeaderLayoutBuilder->ComputeExtraBlockVaddr(originalHeaders, extraBlockOff); @@ -97,7 +93,7 @@ std::vector LinuxElfPatcher::Patch( _appendDynEntry(dynSegBuf, DT_JMPREL, vaddrOfExtraBlockOffset(relaPltOff)); _appendDynEntry(dynSegBuf, DT_PLTRELSZ, dynSection.RelaPltData.size()); _appendDynEntry(dynSegBuf, DT_PLTREL, static_cast(DT_RELA)); - _appendDynEntry(dynSegBuf, DT_PLTGOT, vaddrOfExtraBlockOffset(gotOff)); + _appendDynEntry(dynSegBuf, DT_PLTGOT, originalPltGotVaddr); } _appendDynEntry(dynSegBuf, DT_NULL, 0); diff --git a/core/elfpatcher/src/windows/WindowsElfPatcher.cpp b/core/elfpatcher/src/windows/WindowsElfPatcher.cpp index 1e5a4d8ca..8f46d5dd4 100644 --- a/core/elfpatcher/src/windows/WindowsElfPatcher.cpp +++ b/core/elfpatcher/src/windows/WindowsElfPatcher.cpp @@ -19,7 +19,8 @@ WindowsElfPatcher::WindowsElfPatcher( std::vector WindowsElfPatcher::Patch( const std::vector& sourceElf, const std::vector& originalHeaders, - const Domain::SysVDynamicSection& dynamicSection) + const Domain::SysVDynamicSection& dynamicSection, + std::uint64_t originalPltGotVaddr) { throw Domain::RelinkerException("Not implemented"); } diff --git a/core/main.cpp b/core/main.cpp index 5b35186f5..841672ff1 100644 --- a/core/main.cpp +++ b/core/main.cpp @@ -93,7 +93,7 @@ int main(const int argc, char* argv[]) { std::make_shared(byteWriter), byteWriter ); - auto patchedElf = elfPatcher.Patch(sourceBytes, result.OriginalHeaders, result.DynamicSection); + auto patchedElf = elfPatcher.Patch(sourceBytes, result.OriginalHeaders, result.DynamicSection, result.OriginalPltGotVaddr); fileWriter.Write(outputElfPath, patchedElf); } catch (const Domain::RelinkerException& e) { diff --git a/core/relinker/include/relinker/domain/RelinkResult.hpp b/core/relinker/include/relinker/domain/RelinkResult.hpp index 088bdb697..b7942a1ca 100644 --- a/core/relinker/include/relinker/domain/RelinkResult.hpp +++ b/core/relinker/include/relinker/domain/RelinkResult.hpp @@ -10,6 +10,7 @@ struct RelinkResult { std::vector RegistryEntries; std::vector OriginalHeaders; SysVDynamicSection DynamicSection; + VirtualAddress OriginalPltGotVaddr; }; } diff --git a/core/relinker/src/pipeline/RelinkerPipeline.cpp b/core/relinker/src/pipeline/RelinkerPipeline.cpp index 25689e773..d2ba16aeb 100644 --- a/core/relinker/src/pipeline/RelinkerPipeline.cpp +++ b/core/relinker/src/pipeline/RelinkerPipeline.cpp @@ -215,7 +215,7 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf std::cout << "OK: " << entries.size() << " NID references processed\n"; - return RelinkResult{std::move(entries), std::move(programHeaders), std::move(dynSection)}; + return RelinkResult{std::move(entries), std::move(programHeaders), std::move(dynSection), gotVAddr}; } }