diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 840596b49..98eb77770 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -204,6 +204,8 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_elf_header.py $) add_test(NAME input_magic COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_input_magic.py $) + add_test(NAME executable_segments COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_executable_segments.py $) else() message(STATUS "Python3 interpreter not found; skipping Python relinker tests") endif() diff --git a/core/relinker/relinker/src/analysis/CallSiteResolver.cpp b/core/relinker/relinker/src/analysis/CallSiteResolver.cpp index 39e2049f2..8628e151c 100644 --- a/core/relinker/relinker/src/analysis/CallSiteResolver.cpp +++ b/core/relinker/relinker/src/analysis/CallSiteResolver.cpp @@ -69,14 +69,15 @@ public: std::vector ResolveCallSites(const std::vector& textSection, FileByteOffset textSectionVAddr, VirtualAddress targetGotOrPltAddress, ByteCount targetGotOrPltSize) override; private: const std::vector* _cachedTextPtr = nullptr; - std::map _cachedTargetToInstr; + std::multimap _cachedTargetToInstr; + FileByteOffset _cachedTextVAddr = 0; }; std::vector CallSiteResolver::ResolveCallSites(const std::vector& textSection, const FileByteOffset textSectionVAddr, const VirtualAddress targetGotOrPltAddress, const ByteCount targetGotOrPltSize) { if (textSection.empty()) return {}; - if (_cachedTextPtr != &textSection) { + if (_cachedTextPtr != &textSection || _cachedTextVAddr != textSectionVAddr) { auto scanner = Codegen::MakeInstructionScanner(); - auto instructions = scanner->ScanCodeSection(textSection, textSectionVAddr, textSection.size()); + auto instructions = scanner->ScanCodeSection(textSection, 0, textSection.size()); _cachedTargetToInstr.clear(); for (const auto& m : instructions) { std::size_t dispOff = 0; @@ -88,18 +89,19 @@ std::vector CallSiteResolver::ResolveCallSites(const std::vector continue; if (dispOff + 4 > textSection.size()) continue; std::int32_t disp = readDisp32(textSection, dispOff); - VirtualAddress instrEnd = m.Offset + static_cast(m.Length); + VirtualAddress instrEnd = textSectionVAddr + m.Offset + static_cast(m.Length); VirtualAddress target = static_cast(static_cast(instrEnd) + disp); _cachedTargetToInstr.emplace(target, m.Offset); } _cachedTextPtr = &textSection; + _cachedTextVAddr = textSectionVAddr; } std::vector sites; auto lower = _cachedTargetToInstr.lower_bound(targetGotOrPltAddress); auto upper = _cachedTargetToInstr.lower_bound(targetGotOrPltAddress + targetGotOrPltSize); for (auto it = lower; it != upper; ++it) - sites.push_back(it->second); + sites.push_back(textSectionVAddr + it->second); return sites; } diff --git a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp index f53becebc..295a798ec 100644 --- a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp +++ b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp @@ -38,14 +38,20 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf std::vector textSection; VirtualAddress textVAddr = 0; + std::vector, VirtualAddress>> executableSegments; VirtualAddress gotVAddr = 0; ByteCount gotSize = 0; for (const auto& ph : programHeaders) { if (ph.Type == PT_LOAD && (ph.Flags & PF_X) != 0) { - textSection = _elfReader->ReadSegment(ph); - textVAddr = ph.MappedAddress; - break; + auto segment = _elfReader->ReadSegment(ph); + if (!segment.empty()) { + if (textSection.empty()) { + textSection = segment; + textVAddr = ph.MappedAddress; + } + executableSegments.emplace_back(std::move(segment), ph.MappedAddress); + } } } @@ -199,8 +205,8 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf for (const auto& ref : nidRefs) _validationPolicy->ValidateRelocationTypeSupported(ref.RelocationTypeValue, ref.RelocationTableOffset); - if (!textSection.empty()) - _syscallScanner->ScanCodeSectionForSyscalls(textSection, textVAddr, textSection.size()); + for (const auto& [segment, segmentVAddr] : executableSegments) + _syscallScanner->ScanCodeSectionForSyscalls(segment, segmentVAddr, segment.size()); _validationPolicy->ValidateSyscallAbsence(); @@ -211,6 +217,8 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf std::cout << "NID input: " << originalNidCount << " references\n"; if (unusedFilterLevel == 2) { + if (executableSegments.size() > 1) + throw RelinkerException("Strict NID filtering does not support multiple executable segments"); nidRefs = _unusedNidFilter->Filter(nidRefs, raw, textSection, textVAddr); if (nidRefs.size() > originalNidCount) throw RelinkerException("Strict NID filter increased the reference count"); std::cout << "Strict filtering total: " << originalNidCount << " -> " << nidRefs.size() << "; filtered=" << originalNidCount - nidRefs.size() << "\n"; @@ -226,9 +234,14 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf std::cout << "PLT preservation: " << pltRefs.size() << " -> " << pltRefs.size() << "; filtered=0\n"; const std::size_t nonPltCount = nonPltRefs.size(); - nonPltRefs = _unusedNidFilter->Filter(nonPltRefs, raw, textSection, textVAddr); - if (nonPltRefs.size() > nonPltCount) throw RelinkerException("Unused NID filter increased the reference count"); - std::cout << "CFG/GOT filtering: " << nonPltCount << " -> " << nonPltRefs.size() << "; filtered=" << nonPltCount - nonPltRefs.size() << "\n"; + if (executableSegments.size() > 1) { + std::cout << "CFG/GOT filtering skipped: multiple executable segments are not modeled; " + << nonPltCount << " -> " << nonPltCount << "; filtered=0\n"; + } else { + nonPltRefs = _unusedNidFilter->Filter(nonPltRefs, raw, textSection, textVAddr); + if (nonPltRefs.size() > nonPltCount) throw RelinkerException("Unused NID filter increased the reference count"); + std::cout << "CFG/GOT filtering: " << nonPltCount << " -> " << nonPltRefs.size() << "; filtered=" << nonPltCount - nonPltRefs.size() << "\n"; + } nidRefs.clear(); nidRefs.reserve(pltRefs.size() + nonPltRefs.size()); @@ -285,8 +298,11 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf for (const auto& ref : nidRefs) { std::vector callSites; bool callSitesResolved = false; - if (!textSection.empty() && gotSize > 0) { - callSites = _callSiteResolver->ResolveCallSites(textSection, textVAddr, ref.RelocationAddress, 8); + if (!executableSegments.empty()) { + for (const auto& [segment, segmentVAddr] : executableSegments) { + auto segmentSites = _callSiteResolver->ResolveCallSites(segment, segmentVAddr, ref.RelocationAddress, 8); + callSites.insert(callSites.end(), segmentSites.begin(), segmentSites.end()); + } callSitesResolved = !callSites.empty(); } CallRegistryEntry entry; diff --git a/core/relinker/relinker/tests/test_executable_segments.py b/core/relinker/relinker/tests/test_executable_segments.py new file mode 100644 index 000000000..ea75606e6 --- /dev/null +++ b/core/relinker/relinker/tests/test_executable_segments.py @@ -0,0 +1,84 @@ +from pathlib import Path +import os +import json +import struct +import subprocess +import sys +import tempfile + + +def fixture(second_code: bytes, with_import: bool = False) -> bytearray: + image = bytearray(0x1000) + image[:16] = b"\x7fELF\x02\x01\x01" + bytes(9) + struct.pack_into("