mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
chore(relinker): protect .rela.plt + static jump_slot
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define RELINKER_ANALYSIS_UNUSED_NID_FILTER_ICONTROLFLOWGRAPH_HPP
|
||||
|
||||
#include <relinker/domain/Types.hpp>
|
||||
#include <relinker/analysis/UnusedNidFilter/IRelativeRelocationIndex.hpp>
|
||||
#include <unordered_set>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
@@ -19,7 +20,8 @@ std::unique_ptr<IControlFlowGraph> BuildControlFlowGraph(
|
||||
const std::vector<std::uint8_t>& text,
|
||||
VirtualAddress textVaddr,
|
||||
VirtualAddress entryVaddr,
|
||||
const std::vector<VirtualAddress>& extraEntries
|
||||
const std::vector<VirtualAddress>& extraEntries,
|
||||
const IRelativeRelocationIndex& relativeRelocations
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef RELINKER_ANALYSIS_UNUSED_NID_FILTER_IRELATIVERELOCATIONINDEX_HPP
|
||||
#define RELINKER_ANALYSIS_UNUSED_NID_FILTER_IRELATIVERELOCATIONINDEX_HPP
|
||||
|
||||
#include <relinker/domain/Types.hpp>
|
||||
#include <optional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Relinker::UnusedNidFilter {
|
||||
|
||||
class IRelativeRelocationIndex {
|
||||
public:
|
||||
virtual ~IRelativeRelocationIndex() = default;
|
||||
virtual std::optional<VirtualAddress> TargetOfSlot(VirtualAddress slotVaddr) const = 0;
|
||||
};
|
||||
|
||||
std::unique_ptr<IRelativeRelocationIndex> BuildRelativeRelocationIndex(
|
||||
const std::vector<std::uint8_t>& elfBytes
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -12,7 +12,9 @@ public:
|
||||
|
||||
virtual SysVDynamicSection BuildDynamicSection(
|
||||
const std::vector<NidReference>& nidReferences,
|
||||
const std::vector<std::string>& neededLibraries
|
||||
const std::vector<std::string>& neededLibraries,
|
||||
FileByteOffset originalJmprelOffset,
|
||||
std::uint32_t originalJmprelCount
|
||||
) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -9,7 +9,9 @@ class SysVDynamicSectionBuilder : public ISysVDynamicSectionBuilder {
|
||||
public:
|
||||
SysVDynamicSection BuildDynamicSection(
|
||||
const std::vector<NidReference>& nidReferences,
|
||||
const std::vector<std::string>& neededLibraries
|
||||
const std::vector<std::string>& neededLibraries,
|
||||
FileByteOffset originalJmprelOffset,
|
||||
std::uint32_t originalJmprelCount
|
||||
) override;
|
||||
|
||||
private:
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include <relinker/analysis/UnusedNidFilter/IEntryPointCollector.hpp>
|
||||
#include <relinker/analysis/UnusedNidFilter/IControlFlowGraph.hpp>
|
||||
#include <relinker/analysis/UnusedNidFilter/IGotAccessIndex.hpp>
|
||||
#include <relinker/analysis/UnusedNidFilter/IRelativeRelocationIndex.hpp>
|
||||
|
||||
namespace Relinker {
|
||||
|
||||
@@ -23,7 +24,8 @@ public:
|
||||
VirtualAddress primary = entries[0];
|
||||
std::vector<VirtualAddress> 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<NidReference> result;
|
||||
|
||||
@@ -22,7 +22,8 @@ std::unique_ptr<IControlFlowGraph> BuildControlFlowGraph(
|
||||
const std::vector<std::uint8_t>& text,
|
||||
VirtualAddress textVaddr,
|
||||
VirtualAddress entryVaddr,
|
||||
const std::vector<VirtualAddress>& extraEntries
|
||||
const std::vector<VirtualAddress>& extraEntries,
|
||||
const IRelativeRelocationIndex& relativeRelocations
|
||||
) {
|
||||
const Codegen::X64InstructionDecoder decoder;
|
||||
std::unordered_set<VirtualAddress> reachable;
|
||||
@@ -67,17 +68,29 @@ std::unique_ptr<IControlFlowGraph> BuildControlFlowGraph(
|
||||
if (info.HasRipRelativeDisp) {
|
||||
std::int32_t disp = 0;
|
||||
std::memcpy(&disp, text.data() + bufOff + info.RipRelativeDispOffset, 4);
|
||||
VirtualAddress target = static_cast<VirtualAddress>(
|
||||
VirtualAddress slotVaddr = static_cast<VirtualAddress>(
|
||||
static_cast<std::int64_t>(nextVaddr) + disp
|
||||
);
|
||||
enqueue(target);
|
||||
auto resolved = relativeRelocations.TargetOfSlot(slotVaddr);
|
||||
if (resolved.has_value())
|
||||
enqueue(*resolved);
|
||||
} else if (info.HasBranchTarget) {
|
||||
enqueue(static_cast<VirtualAddress>(static_cast<std::int64_t>(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<VirtualAddress>(
|
||||
static_cast<std::int64_t>(nextVaddr) + disp
|
||||
);
|
||||
auto resolved = relativeRelocations.TargetOfSlot(slotVaddr);
|
||||
if (resolved.has_value())
|
||||
enqueue(*resolved);
|
||||
} else if (info.HasBranchTarget) {
|
||||
enqueue(static_cast<VirtualAddress>(static_cast<std::int64_t>(nextVaddr) + info.BranchDisp));
|
||||
}
|
||||
enqueue(nextVaddr);
|
||||
break;
|
||||
case IndirectCall:
|
||||
|
||||
@@ -27,20 +27,6 @@ bool inText(VirtualAddress va, VirtualAddress textVaddr, std::size_t textSize) {
|
||||
return va >= textVaddr && va < textVaddr + static_cast<VirtualAddress>(textSize);
|
||||
}
|
||||
|
||||
struct LoadSegment {
|
||||
VirtualAddress vaddr;
|
||||
std::uint64_t fileOffset;
|
||||
std::uint64_t fileSize;
|
||||
};
|
||||
|
||||
std::uint64_t vaddrToFileOffset(const std::vector<LoadSegment>& 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<LoadSegment> loads;
|
||||
for (std::uint16_t i = 0; i < phCount; ++i) {
|
||||
std::size_t phPos = static_cast<std::size_t>(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<std::size_t>(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<std::size_t>(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<std::size_t>(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<std::uint32_t>(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<std::uint8_t>& elfBytes,
|
||||
VirtualAddress textVaddr,
|
||||
std::size_t textSize,
|
||||
VirtualAddress gotSlotVaddr,
|
||||
std::vector<VirtualAddress>& 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<std::size_t>(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<std::size_t>(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<VirtualAddress>(
|
||||
static_cast<std::int64_t>(instrEnd) + disp
|
||||
);
|
||||
if (target == gotSlotVaddr)
|
||||
entries.push_back(textVaddr + off);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
std::unique_ptr<IEntryPointCollector> MakeEntryPointCollector() {
|
||||
|
||||
@@ -0,0 +1,167 @@
|
||||
#include <relinker/analysis/UnusedNidFilter/IRelativeRelocationIndex.hpp>
|
||||
#include <unordered_map>
|
||||
#include <cstring>
|
||||
|
||||
namespace Relinker::UnusedNidFilter {
|
||||
|
||||
namespace {
|
||||
|
||||
std::uint16_t read16(const std::vector<std::uint8_t>& 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<std::uint8_t>& 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<std::uint8_t>& 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<LoadSegment>& 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<std::uint8_t>& elfBytes,
|
||||
const std::vector<LoadSegment>& loads,
|
||||
VirtualAddress tableVaddr,
|
||||
std::uint64_t tableSize,
|
||||
std::unordered_map<VirtualAddress, VirtualAddress>& 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<std::size_t>(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<std::int64_t>(read64(elfBytes, pos + 16));
|
||||
std::uint32_t relType = static_cast<std::uint32_t>(rInfo & 0xffffffff);
|
||||
|
||||
if (relType != R_X86_64_RELATIVE) continue;
|
||||
|
||||
out[static_cast<VirtualAddress>(rOffset)] = static_cast<VirtualAddress>(rAddend);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class RelativeRelocationIndex : public IRelativeRelocationIndex {
|
||||
public:
|
||||
explicit RelativeRelocationIndex(std::unordered_map<VirtualAddress, VirtualAddress> targets)
|
||||
: _targets(std::move(targets)) {}
|
||||
|
||||
std::optional<VirtualAddress> TargetOfSlot(VirtualAddress slotVaddr) const override {
|
||||
auto it = _targets.find(slotVaddr);
|
||||
if (it == _targets.end()) return std::nullopt;
|
||||
return it->second;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<VirtualAddress, VirtualAddress> _targets;
|
||||
};
|
||||
|
||||
std::unique_ptr<IRelativeRelocationIndex> BuildRelativeRelocationIndex(
|
||||
const std::vector<std::uint8_t>& 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<LoadSegment> loads;
|
||||
for (std::uint16_t i = 0; i < phCount; ++i) {
|
||||
std::size_t phPos = static_cast<std::size_t>(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<VirtualAddress, VirtualAddress> targets;
|
||||
|
||||
for (std::uint16_t i = 0; i < phCount; ++i) {
|
||||
std::size_t phPos = static_cast<std::size_t>(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<std::size_t>(segOff + off);
|
||||
std::int64_t tag = static_cast<std::int64_t>(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<RelativeRelocationIndex>(std::move(targets));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -60,7 +60,9 @@ void SysVDynamicSectionBuilder::_appendRela(
|
||||
|
||||
SysVDynamicSection SysVDynamicSectionBuilder::BuildDynamicSection(
|
||||
const std::vector<NidReference>& nidReferences,
|
||||
const std::vector<std::string>& neededLibraries)
|
||||
const std::vector<std::string>& 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<const NidReference*> pltSlots(originalJmprelCount, nullptr);
|
||||
std::vector<const NidReference*> 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<std::uint8_t>((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<std::uint64_t>(symIdx) << 32) | R_X86_64_JUMP_SLOT;
|
||||
_appendRela(result.RelaPltData, ref.RelocationAddress, relaInfo, ref.Addend);
|
||||
++symIdx;
|
||||
}
|
||||
|
||||
const std::uint64_t relaInfo = (static_cast<std::uint64_t>(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<std::uint8_t>((STB_GLOBAL << 4) | STT_FUNC);
|
||||
_appendElfSym(result.DynSymData, nameOff, info, STV_DEFAULT, 0, 0, 0);
|
||||
|
||||
const std::uint64_t relaInfo = (static_cast<std::uint64_t>(symIdx) << 32) | ref.RelocationTypeValue;
|
||||
_appendRela(result.RelaData, ref.RelocationAddress, relaInfo, ref.Addend);
|
||||
++symIdx;
|
||||
}
|
||||
|
||||
|
||||
@@ -199,10 +199,28 @@ RelinkResult RelinkerPipeline::Relink(const std::vector<std::uint8_t>& 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<NidReference> pltRefs;
|
||||
std::vector<NidReference> 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<std::uint32_t>(dynJmpRelSize / relaEntSize));
|
||||
|
||||
static constexpr std::uint32_t R_X86_64_RELATIVE = 8;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user