From 221086beb607cd8b506a98454f1eebb3cd9b7f2b Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Fri, 25 Sep 2026 05:32:49 +1000 Subject: [PATCH] fix(relinker): support ELF images without PLT relocations --- core/relinker/CMakeLists.txt | 3 + .../src/pipeline/RelinkerPipeline.cpp | 31 ++++++--- .../relinker/tests/test_optional_plt.py | 69 +++++++++++++++++++ 3 files changed, 92 insertions(+), 11 deletions(-) create mode 100644 core/relinker/relinker/tests/test_optional_plt.py diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index c556e72ad..2daa44c99 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -73,6 +73,9 @@ endif() if(BUILD_TESTING) + find_package(Python3 REQUIRED COMPONENTS Interpreter) + add_test(NAME optional_plt COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_optional_plt.py $) add_executable(strict_nid_filter_tests ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/StrictNidFilterTests.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/UnusedNidFilter/StrictNidFilter.cpp diff --git a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp index 7bacccdd1..f53becebc 100644 --- a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp +++ b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp @@ -99,10 +99,14 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf return hasTag(osTag) ? getTagValue(osTag) : getTagValue(sysvTag); }; - gotVAddr = requireExactlyOneOf(DT_OS_PLTGOT, DT_PLTGOT, "DT_PLTGOT") - ? getTagValue(DT_OS_PLTGOT) - : getTagValue(DT_PLTGOT); - gotSize = readAsSize(DT_OS_PLTRELSZ, DT_PLTRELSZ, "DT_PLTRELSZ"); + const bool hasPltRelocations = hasTag(DT_OS_PLTRELSZ) || hasTag(DT_PLTRELSZ) + || hasTag(DT_OS_PLTREL) || hasTag(DT_PLTREL) + || hasTag(DT_OS_JMPREL) || hasTag(DT_JMPREL); + if (hasPltRelocations || hasTag(DT_OS_PLTGOT) || hasTag(DT_PLTGOT)) { + gotVAddr = requireExactlyOneOf(DT_OS_PLTGOT, DT_PLTGOT, "DT_PLTGOT") + ? getTagValue(DT_OS_PLTGOT) + : getTagValue(DT_PLTGOT); + } const FileByteOffset dynStrTabOffset = readAsOffset(DT_OS_STRTAB, DT_STRTAB, "DT_STRTAB"); requireExactlyOneOf(DT_OS_STRSZ, DT_STRSZ, "DT_STRSZ"); @@ -112,13 +116,18 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf if (readAsSize(DT_OS_SYMENT, DT_SYMENT, "DT_SYMENT") != symEntSize) throw RelinkerException("Unsupported DT_SYMENT value"); - const std::int64_t jmprelType = requireExactlyOneOf(DT_OS_PLTREL, DT_PLTREL, "DT_PLTREL") - ? getTagValue(DT_OS_PLTREL) - : getTagValue(DT_PLTREL); - if (jmprelType != DT_RELA) - throw RelinkerException("Unsupported DT_PLTREL type"); - - const FileByteOffset dynJmpRelOffset = readAsOffset(DT_OS_JMPREL, DT_JMPREL, "DT_JMPREL"); + FileByteOffset dynJmpRelOffset = 0; + if (hasPltRelocations) { + gotSize = readAsSize(DT_OS_PLTRELSZ, DT_PLTRELSZ, "DT_PLTRELSZ"); + const std::int64_t jmprelType = requireExactlyOneOf(DT_OS_PLTREL, DT_PLTREL, "DT_PLTREL") + ? getTagValue(DT_OS_PLTREL) + : getTagValue(DT_PLTREL); + if (jmprelType != DT_RELA) + throw RelinkerException("Unsupported DT_PLTREL type"); + dynJmpRelOffset = readAsOffset(DT_OS_JMPREL, DT_JMPREL, "DT_JMPREL"); + if (gotSize % 24 != 0) + throw RelinkerException("Invalid DT_PLTRELSZ value"); + } const ByteCount dynJmpRelSize = gotSize; const FileByteOffset dynRelaOffset = readAsOffset(DT_OS_RELA, DT_RELA, "DT_RELA"); diff --git a/core/relinker/relinker/tests/test_optional_plt.py b/core/relinker/relinker/tests/test_optional_plt.py new file mode 100644 index 000000000..894b47a7b --- /dev/null +++ b/core/relinker/relinker/tests/test_optional_plt.py @@ -0,0 +1,69 @@ +"""Exercise optional PLT metadata through the real ELF-to-PE pipeline.""" + +import os +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + + +def fixture(extra_tags=()): + image = bytearray(0x1000) + image[:16] = b"\x7fELF\x02\x01\x01" + bytes(9) + struct.pack_into("