diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 87c1a9841..ee69f44a7 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -31,6 +31,7 @@ add_executable(relinker ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/UnusedNidFilter/GotAccessIndex.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/analysis/ValidationPolicy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/output/SysVDynamicSectionBuilder.cpp ${CMAKE_CURRENT_SOURCE_DIR}/relinker/src/output/CallRegistryWriter.cpp diff --git a/core/relinker/include/relinker/analysis/UnusedNidFilter/IControlFlowGraph.hpp b/core/relinker/include/relinker/analysis/UnusedNidFilter/IControlFlowGraph.hpp index 9f34f3336..421029d69 100644 --- a/core/relinker/include/relinker/analysis/UnusedNidFilter/IControlFlowGraph.hpp +++ b/core/relinker/include/relinker/analysis/UnusedNidFilter/IControlFlowGraph.hpp @@ -2,6 +2,7 @@ #define RELINKER_ANALYSIS_UNUSED_NID_FILTER_ICONTROLFLOWGRAPH_HPP #include +#include #include #include #include @@ -19,7 +20,8 @@ std::unique_ptr BuildControlFlowGraph( const std::vector& text, VirtualAddress textVaddr, VirtualAddress entryVaddr, - const std::vector& extraEntries + const std::vector& extraEntries, + const IRelativeRelocationIndex& relativeRelocations ); } diff --git a/core/relinker/include/relinker/analysis/UnusedNidFilter/IRelativeRelocationIndex.hpp b/core/relinker/include/relinker/analysis/UnusedNidFilter/IRelativeRelocationIndex.hpp new file mode 100644 index 000000000..5a3a44742 --- /dev/null +++ b/core/relinker/include/relinker/analysis/UnusedNidFilter/IRelativeRelocationIndex.hpp @@ -0,0 +1,24 @@ +#ifndef RELINKER_ANALYSIS_UNUSED_NID_FILTER_IRELATIVERELOCATIONINDEX_HPP +#define RELINKER_ANALYSIS_UNUSED_NID_FILTER_IRELATIVERELOCATIONINDEX_HPP + +#include +#include +#include +#include +#include + +namespace Relinker::UnusedNidFilter { + +class IRelativeRelocationIndex { +public: + virtual ~IRelativeRelocationIndex() = default; + virtual std::optional TargetOfSlot(VirtualAddress slotVaddr) const = 0; +}; + +std::unique_ptr BuildRelativeRelocationIndex( + const std::vector& elfBytes +); + +} + +#endif diff --git a/core/relinker/include/relinker/domain/ISysVDynamicSectionBuilder.hpp b/core/relinker/include/relinker/domain/ISysVDynamicSectionBuilder.hpp index 78337d32b..3297dd609 100644 --- a/core/relinker/include/relinker/domain/ISysVDynamicSectionBuilder.hpp +++ b/core/relinker/include/relinker/domain/ISysVDynamicSectionBuilder.hpp @@ -12,7 +12,9 @@ public: virtual SysVDynamicSection BuildDynamicSection( const std::vector& nidReferences, - const std::vector& neededLibraries + const std::vector& neededLibraries, + FileByteOffset originalJmprelOffset, + std::uint32_t originalJmprelCount ) = 0; }; diff --git a/core/relinker/include/relinker/output/SysVDynamicSectionBuilder.hpp b/core/relinker/include/relinker/output/SysVDynamicSectionBuilder.hpp index c4e6d6c21..7816d126c 100644 --- a/core/relinker/include/relinker/output/SysVDynamicSectionBuilder.hpp +++ b/core/relinker/include/relinker/output/SysVDynamicSectionBuilder.hpp @@ -9,7 +9,9 @@ class SysVDynamicSectionBuilder : public ISysVDynamicSectionBuilder { public: SysVDynamicSection BuildDynamicSection( const std::vector& nidReferences, - const std::vector& neededLibraries + const std::vector& neededLibraries, + FileByteOffset originalJmprelOffset, + std::uint32_t originalJmprelCount ) override; private: diff --git a/core/relinker/src/analysis/UnusedNidFilter.cpp b/core/relinker/src/analysis/UnusedNidFilter.cpp index 0fc89f16f..e6b796348 100644 --- a/core/relinker/src/analysis/UnusedNidFilter.cpp +++ b/core/relinker/src/analysis/UnusedNidFilter.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace Relinker { @@ -23,7 +24,8 @@ public: VirtualAddress primary = entries[0]; std::vector extra(entries.begin() + 1, entries.end()); - auto cfg = UnusedNidFilter::BuildControlFlowGraph(textSection, textVAddr, primary, extra); + auto relativeRelocations = UnusedNidFilter::BuildRelativeRelocationIndex(elfBytes); + auto cfg = UnusedNidFilter::BuildControlFlowGraph(textSection, textVAddr, primary, extra, *relativeRelocations); auto index = UnusedNidFilter::BuildGotAccessIndex(*cfg, textSection, textVAddr); std::vector result; diff --git a/core/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp b/core/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp index 695d83d9f..ce1c4a906 100644 --- a/core/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp +++ b/core/relinker/src/analysis/UnusedNidFilter/ControlFlowGraph.cpp @@ -22,7 +22,8 @@ std::unique_ptr BuildControlFlowGraph( const std::vector& text, VirtualAddress textVaddr, VirtualAddress entryVaddr, - const std::vector& extraEntries + const std::vector& extraEntries, + const IRelativeRelocationIndex& relativeRelocations ) { const Codegen::X64InstructionDecoder decoder; std::unordered_set reachable; @@ -67,17 +68,29 @@ std::unique_ptr BuildControlFlowGraph( if (info.HasRipRelativeDisp) { std::int32_t disp = 0; std::memcpy(&disp, text.data() + bufOff + info.RipRelativeDispOffset, 4); - VirtualAddress target = static_cast( + VirtualAddress slotVaddr = static_cast( static_cast(nextVaddr) + disp ); - enqueue(target); + auto resolved = relativeRelocations.TargetOfSlot(slotVaddr); + if (resolved.has_value()) + enqueue(*resolved); } else if (info.HasBranchTarget) { enqueue(static_cast(static_cast(nextVaddr) + info.BranchDisp)); } break; case Call: - if (info.HasBranchTarget) + if (info.HasRipRelativeDisp) { + std::int32_t disp = 0; + std::memcpy(&disp, text.data() + bufOff + info.RipRelativeDispOffset, 4); + VirtualAddress slotVaddr = static_cast( + static_cast(nextVaddr) + disp + ); + auto resolved = relativeRelocations.TargetOfSlot(slotVaddr); + if (resolved.has_value()) + enqueue(*resolved); + } else if (info.HasBranchTarget) { enqueue(static_cast(static_cast(nextVaddr) + info.BranchDisp)); + } enqueue(nextVaddr); break; case IndirectCall: diff --git a/core/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp b/core/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp index 333118d9c..c2225509f 100644 --- a/core/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp +++ b/core/relinker/src/analysis/UnusedNidFilter/EntryPointCollector.cpp @@ -27,20 +27,6 @@ bool inText(VirtualAddress va, VirtualAddress textVaddr, std::size_t textSize) { return va >= textVaddr && va < textVaddr + static_cast(textSize); } -struct LoadSegment { - VirtualAddress vaddr; - std::uint64_t fileOffset; - std::uint64_t fileSize; -}; - -std::uint64_t vaddrToFileOffset(const std::vector& loads, VirtualAddress va) { - for (const auto& seg : loads) { - if (va >= seg.vaddr && va < seg.vaddr + seg.fileSize) - return seg.fileOffset + (va - seg.vaddr); - } - throw RelinkerException("Cannot translate virtual address to file offset", va); -} - } class EntryPointCollector : public IEntryPointCollector { @@ -73,20 +59,6 @@ public: if (phEntSize < 56) throw RelinkerException("ELF program header entry too small"); - std::vector loads; - for (std::uint16_t i = 0; i < phCount; ++i) { - 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); - seg.vaddr = read64(elfBytes, phPos + 16); - seg.fileSize = read64(elfBytes, phPos + 32); - loads.push_back(seg); - } - for (std::uint16_t i = 0; i < phCount; ++i) { std::size_t phPos = static_cast(phOff) + i * phEntSize; if (phPos + 56 > elfBytes.size()) throw RelinkerException("Program header out of bounds"); @@ -108,10 +80,6 @@ public: 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_JMPREL = 0x17; - constexpr std::int64_t DT_PLTRELSZ = 2; - constexpr std::int64_t DT_SYMTAB = 6; - constexpr std::int64_t DT_SYMENT = 11; 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; @@ -123,10 +91,6 @@ public: std::uint64_t initArraySz = 0; VirtualAddress finiArrayVa = 0; std::uint64_t finiArraySz = 0; - VirtualAddress jmprelVa = 0; - std::uint64_t pltrelsz = 0; - VirtualAddress symtabVa = 0; - std::uint64_t syment = 24; for (std::uint64_t off = 0; off + 16 <= segSz; off += 16) { std::size_t pos = static_cast(segOff + off); @@ -140,10 +104,6 @@ public: if (tag == DT_INIT_ARRAYSZ || tag == DT_OS_INIT_ARRAYSZ) initArraySz = val; if (tag == DT_FINI_ARRAY || tag == DT_OS_FINI_ARRAY) finiArrayVa = val; if (tag == DT_FINI_ARRAYSZ || tag == DT_OS_FINI_ARRAYSZ) finiArraySz = val; - if (tag == DT_JMPREL) jmprelVa = val; - if (tag == DT_PLTRELSZ) pltrelsz = val; - if (tag == DT_SYMTAB) symtabVa = val; - if (tag == DT_SYMENT) syment = val; } auto collectArray = [&](VirtualAddress arrayVa, std::uint64_t arraySz) { @@ -163,25 +123,6 @@ public: collectArray(initArrayVa, initArraySz); collectArray(finiArrayVa, finiArraySz); - if (jmprelVa != 0 && pltrelsz != 0 && symtabVa != 0 && syment != 0) { - constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7; - constexpr std::size_t relaEntSize = 24; - std::uint64_t jmprelFileOff = vaddrToFileOffset(loads, jmprelVa); - std::uint64_t symtabFileOff = vaddrToFileOffset(loads, symtabVa); - - for (std::uint64_t off = 0; off + relaEntSize <= pltrelsz; off += relaEntSize) { - std::size_t pos = static_cast(jmprelFileOff + off); - if (pos + relaEntSize > elfBytes.size()) break; - std::uint64_t rOffset = read64(elfBytes, pos); - std::uint64_t rInfo = read64(elfBytes, pos + 8); - std::uint32_t relType = static_cast(rInfo & 0xffffffff); - if (relType != R_X86_64_JUMP_SLOT) continue; - - collectPltThunkForGotSlot(elfBytes, textVaddr, textSize, rOffset, entries); - (void)symtabFileOff; - } - } - break; } @@ -247,53 +188,6 @@ public: if (entries.empty()) throw RelinkerException("No entry points found in text segment"); return entries; } - -private: - void collectPltThunkForGotSlot( - const std::vector& elfBytes, - VirtualAddress textVaddr, - std::size_t textSize, - VirtualAddress gotSlotVaddr, - std::vector& entries - ) const { - const std::uint8_t* text = elfBytes.data(); - std::size_t textFileOff = 0; - - std::uint64_t phOff = read64(elfBytes, 32); - std::uint16_t phEntSize = read16(elfBytes, 54); - std::uint16_t phCount = read16(elfBytes, 56); - - for (std::uint16_t i = 0; i < phCount; ++i) { - std::size_t phPos = static_cast(phOff) + i * phEntSize; - if (phPos + 56 > elfBytes.size()) break; - std::uint32_t type = read32(elfBytes, phPos); - constexpr std::uint32_t PT_LOAD = 1; - constexpr std::uint32_t PF_X = 1; - if (type != PT_LOAD) continue; - std::uint32_t flags = read32(elfBytes, phPos + 4); - if ((flags & PF_X) == 0) continue; - std::uint64_t pFileOff = read64(elfBytes, phPos + 8); - std::uint64_t pVaddr = read64(elfBytes, phPos + 16); - std::uint64_t pFileSz = read64(elfBytes, phPos + 32); - if (pVaddr != textVaddr) continue; - textFileOff = static_cast(pFileOff); - (void)pFileSz; - break; - } - - for (std::size_t off = 0; off + 6 <= textSize; ++off) { - const std::uint8_t* p = text + textFileOff + off; - if (p[0] != 0xFF || p[1] != 0x25) continue; - std::int32_t disp = 0; - std::memcpy(&disp, p + 2, 4); - VirtualAddress instrEnd = textVaddr + off + 6; - VirtualAddress target = static_cast( - static_cast(instrEnd) + disp - ); - if (target == gotSlotVaddr) - entries.push_back(textVaddr + off); - } - } }; std::unique_ptr MakeEntryPointCollector() { diff --git a/core/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp b/core/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp new file mode 100644 index 000000000..9af486615 --- /dev/null +++ b/core/relinker/src/analysis/UnusedNidFilter/RelativeRelocationIndex.cpp @@ -0,0 +1,167 @@ +#include +#include +#include + +namespace Relinker::UnusedNidFilter { + +namespace { + +std::uint16_t read16(const std::vector& b, std::size_t off) { + std::uint16_t v = 0; + std::memcpy(&v, b.data() + off, 2); + return v; +} + +std::uint32_t read32(const std::vector& b, std::size_t off) { + std::uint32_t v = 0; + std::memcpy(&v, b.data() + off, 4); + return v; +} + +std::uint64_t read64(const std::vector& b, std::size_t off) { + std::uint64_t v = 0; + std::memcpy(&v, b.data() + off, 8); + return v; +} + +struct LoadSegment { + VirtualAddress vaddr; + std::uint64_t fileOffset; + std::uint64_t fileSize; +}; + +std::uint64_t vaddrToFileOffset(const std::vector& loads, VirtualAddress va) { + for (const auto& seg : loads) { + if (va >= seg.vaddr && va < seg.vaddr + seg.fileSize) + return seg.fileOffset + (va - seg.vaddr); + } + 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( + const std::vector& elfBytes, + const std::vector& loads, + VirtualAddress tableVaddr, + std::uint64_t tableSize, + std::unordered_map& out +) { + if (tableVaddr == 0 || tableSize == 0) return; + std::uint64_t tableFileOff = vaddrToFileOffset(loads, tableVaddr); + + for (std::uint64_t off = 0; off + RelaEntSize <= tableSize; off += RelaEntSize) { + std::size_t pos = static_cast(tableFileOff + off); + if (pos + RelaEntSize > elfBytes.size()) + throw RelinkerException("Relocation entry out of bounds", pos); + + std::uint64_t rOffset = read64(elfBytes, pos); + std::uint64_t rInfo = read64(elfBytes, pos + 8); + std::int64_t rAddend = static_cast(read64(elfBytes, pos + 16)); + std::uint32_t relType = static_cast(rInfo & 0xffffffff); + + if (relType != R_X86_64_RELATIVE) continue; + + out[static_cast(rOffset)] = static_cast(rAddend); + } +} + +} + +class RelativeRelocationIndex : public IRelativeRelocationIndex { +public: + explicit RelativeRelocationIndex(std::unordered_map targets) + : _targets(std::move(targets)) {} + + std::optional TargetOfSlot(VirtualAddress slotVaddr) const override { + auto it = _targets.find(slotVaddr); + if (it == _targets.end()) return std::nullopt; + return it->second; + } + +private: + std::unordered_map _targets; +}; + +std::unique_ptr BuildRelativeRelocationIndex( + const std::vector& elfBytes +) { + if (elfBytes.size() < 64) throw RelinkerException("ELF too small for header"); + if (elfBytes[0] != 0x7f || elfBytes[1] != 'E' || + elfBytes[2] != 'L' || elfBytes[3] != 'F') { + throw RelinkerException("Not an ELF file"); + } + if (elfBytes[4] != 2) throw RelinkerException("Only ELF64 supported"); + + std::uint64_t phOff = read64(elfBytes, 32); + std::uint16_t phEntSize = read16(elfBytes, 54); + std::uint16_t phCount = read16(elfBytes, 56); + + if (phEntSize < 56) throw RelinkerException("ELF program header entry too small"); + + std::vector loads; + for (std::uint16_t i = 0; i < phCount; ++i) { + 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); + seg.vaddr = read64(elfBytes, phPos + 16); + seg.fileSize = read64(elfBytes, phPos + 32); + loads.push_back(seg); + } + + std::unordered_map targets; + + for (std::uint16_t i = 0; i < phCount; ++i) { + 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_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; + std::uint64_t pltrelsz = 0; + + for (std::uint64_t off = 0; off + 16 <= segSz; off += 16) { + std::size_t pos = static_cast(segOff + off); + std::int64_t tag = static_cast(read64(elfBytes, pos)); + std::uint64_t val = read64(elfBytes, pos + 8); + + if (tag == DT_NULL) break; + if (tag == DT_RELA || tag == DT_OS_RELA) relaVa = val; + if (tag == DT_RELASZ || tag == DT_OS_RELASZ) relaSz = val; + if (tag == DT_JMPREL || tag == DT_OS_JMPREL) jmprelVa = val; + if (tag == DT_PLTRELSZ || tag == DT_OS_PLTRELSZ) pltrelsz = val; + } + + collectRelativeEntries(elfBytes, loads, relaVa, relaSz, targets); + collectRelativeEntries(elfBytes, loads, jmprelVa, pltrelsz, targets); + + break; + } + + return std::make_unique(std::move(targets)); +} + +} diff --git a/core/relinker/src/output/SysVDynamicSectionBuilder.cpp b/core/relinker/src/output/SysVDynamicSectionBuilder.cpp index 1490ee65a..3e29923b9 100644 --- a/core/relinker/src/output/SysVDynamicSectionBuilder.cpp +++ b/core/relinker/src/output/SysVDynamicSectionBuilder.cpp @@ -60,7 +60,9 @@ void SysVDynamicSectionBuilder::_appendRela( SysVDynamicSection SysVDynamicSectionBuilder::BuildDynamicSection( const std::vector& nidReferences, - const std::vector& neededLibraries) + const std::vector& neededLibraries, + FileByteOffset originalJmprelOffset, + std::uint32_t originalJmprelCount) { SysVDynamicSection result; result.DynStrData.push_back(0); @@ -77,22 +79,71 @@ SysVDynamicSection SysVDynamicSectionBuilder::BuildDynamicSection( return value.substr(0, hashPos); }; - std::uint32_t symIdx = 1; + static constexpr std::size_t kRelaEntSize = 24; + + std::vector pltSlots(originalJmprelCount, nullptr); + std::vector nonPltRefs; + for (const auto& ref : nidReferences) { + std::uint32_t relType = ref.RelocationTypeValue; + if (relType == 0) relType = R_X86_64_JUMP_SLOT; + + if (relType != R_X86_64_JUMP_SLOT) { + nonPltRefs.push_back(&ref); + continue; + } + + if (ref.RelocationTableOffset < originalJmprelOffset) + throw RelinkerException( + "JUMP_SLOT relocation lies before the original .rela.plt table", + ref.RelocationTableOffset); + + const std::uint64_t byteDelta = ref.RelocationTableOffset - originalJmprelOffset; + if (byteDelta % kRelaEntSize != 0) + throw RelinkerException( + "JUMP_SLOT relocation is not aligned to the original .rela.plt entry size", + ref.RelocationTableOffset); + + const std::uint64_t slotIndex = byteDelta / kRelaEntSize; + if (slotIndex >= originalJmprelCount) + throw RelinkerException( + "JUMP_SLOT relocation index exceeds the original .rela.plt table size", + ref.RelocationTableOffset); + + if (pltSlots[slotIndex] != nullptr) + throw RelinkerException( + "Duplicate JUMP_SLOT relocation for the same original .rela.plt slot", + ref.RelocationTableOffset); + + pltSlots[slotIndex] = &ref; + } + + std::uint32_t symIdx = 1; + + for (const NidReference* slot : pltSlots) { + if (slot == nullptr) + throw RelinkerException( + "Original .rela.plt slot has no corresponding JUMP_SLOT relocation; " + "PLT thunks cannot be filtered without patching their hard-coded reloc index"); + + const NidReference& ref = *slot; const std::uint32_t nameOff = _appendStr(result.DynStrData, stripHashSuffix(ref.Nid)); const auto info = static_cast((STB_GLOBAL << 4) | STT_FUNC); _appendElfSym(result.DynSymData, nameOff, info, STV_DEFAULT, 0, 0, 0); - std::uint32_t relType = ref.RelocationTypeValue; - if (relType == 0) relType = R_X86_64_JUMP_SLOT; + const std::uint64_t relaInfo = (static_cast(symIdx) << 32) | R_X86_64_JUMP_SLOT; + _appendRela(result.RelaPltData, ref.RelocationAddress, relaInfo, ref.Addend); + ++symIdx; + } - const std::uint64_t relaInfo = (static_cast(symIdx) << 32) | relType; - - if (relType == R_X86_64_JUMP_SLOT) - _appendRela(result.RelaPltData, ref.RelocationAddress, relaInfo, ref.Addend); - else - _appendRela(result.RelaData, ref.RelocationAddress, relaInfo, ref.Addend); + for (const NidReference* slotPtr : nonPltRefs) { + const NidReference& ref = *slotPtr; + const std::uint32_t nameOff = _appendStr(result.DynStrData, stripHashSuffix(ref.Nid)); + const auto info = static_cast((STB_GLOBAL << 4) | STT_FUNC); + _appendElfSym(result.DynSymData, nameOff, info, STV_DEFAULT, 0, 0, 0); + const std::uint64_t relaInfo = (static_cast(symIdx) << 32) | ref.RelocationTypeValue; + _appendRela(result.RelaData, ref.RelocationAddress, relaInfo, ref.Addend); ++symIdx; } diff --git a/core/relinker/src/pipeline/RelinkerPipeline.cpp b/core/relinker/src/pipeline/RelinkerPipeline.cpp index 09b67f86a..ef484e6c7 100644 --- a/core/relinker/src/pipeline/RelinkerPipeline.cpp +++ b/core/relinker/src/pipeline/RelinkerPipeline.cpp @@ -199,10 +199,28 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& sourceElf _validationPolicy->ValidateSyscallAbsence(); - if (_filterUnusedNids) - nidRefs = _unusedNidFilter->Filter(nidRefs, raw, textSection, textVAddr); + static constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7; - auto dynSection = _dynamicSectionBuilder->BuildDynamicSection(nidRefs, neededLibraries); + if (_filterUnusedNids) { + std::vector pltRefs; + std::vector nonPltRefs; + for (const auto& ref : nidRefs) { + if (ref.RelocationTypeValue == R_X86_64_JUMP_SLOT) + pltRefs.push_back(ref); + else + nonPltRefs.push_back(ref); + } + + nonPltRefs = _unusedNidFilter->Filter(nonPltRefs, raw, textSection, textVAddr); + + nidRefs.clear(); + nidRefs.reserve(pltRefs.size() + nonPltRefs.size()); + for (auto& ref : pltRefs) nidRefs.push_back(std::move(ref)); + for (auto& ref : nonPltRefs) nidRefs.push_back(std::move(ref)); + } + + auto dynSection = _dynamicSectionBuilder->BuildDynamicSection( + nidRefs, neededLibraries, dynJmpRelOffset, static_cast(dynJmpRelSize / relaEntSize)); static constexpr std::uint32_t R_X86_64_RELATIVE = 8;