mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
refactor(relinker): use the shared ELF constants
The relinker redefined 61 ELF constants 93 times across seven files, 38 of them already in elfpatcher's ElfConstants.hpp. Add the missing dynamic tags, relocation types and symbol constants to that header and remove the local copies; every value is unchanged. strict_nid_filter_tests compiles these sources and now gets the elfpatcher include directory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MBXJqzgohLeVAeHsn4Jpm7
This commit is contained in:
@@ -265,6 +265,7 @@ if(BUILD_TESTING)
|
||||
)
|
||||
target_include_directories(strict_nid_filter_tests PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/domain/include
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/include
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/codegen/include
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/relinker/include
|
||||
)
|
||||
|
||||
@@ -87,12 +87,59 @@ inline constexpr std::int64_t DT_STRSZ = 10;
|
||||
inline constexpr std::int64_t DT_SYMENT = 11;
|
||||
inline constexpr std::int64_t DT_PLTREL = 20;
|
||||
inline constexpr std::int64_t DT_JMPREL = 23;
|
||||
inline constexpr std::int64_t DT_INIT = 12;
|
||||
inline constexpr std::int64_t DT_FINI = 13;
|
||||
inline constexpr std::int64_t DT_INIT_ARRAY = 25;
|
||||
inline constexpr std::int64_t DT_FINI_ARRAY = 26;
|
||||
inline constexpr std::int64_t DT_INIT_ARRAYSZ = 27;
|
||||
inline constexpr std::int64_t DT_FINI_ARRAYSZ = 28;
|
||||
inline constexpr std::int64_t DT_RUNPATH = 29;
|
||||
inline constexpr std::int64_t DT_FLAGS = 30;
|
||||
inline constexpr std::uint64_t DF_BIND_NOW = 0x8;
|
||||
|
||||
inline constexpr std::int64_t DT_OS_INIT = 0x6000000c;
|
||||
inline constexpr std::int64_t DT_OS_FINI = 0x6000000d;
|
||||
inline constexpr std::int64_t DT_OS_INIT_ARRAY = 0x60000019;
|
||||
inline constexpr std::int64_t DT_OS_FINI_ARRAY = 0x6000001a;
|
||||
inline constexpr std::int64_t DT_OS_INIT_ARRAYSZ = 0x6000001b;
|
||||
inline constexpr std::int64_t DT_OS_FINI_ARRAYSZ = 0x6000001c;
|
||||
inline constexpr std::int64_t DT_OS_PLTGOT = 0x61000027;
|
||||
inline constexpr std::int64_t DT_OS_JMPREL = 0x61000029;
|
||||
inline constexpr std::int64_t DT_OS_PLTREL = 0x6100002b;
|
||||
inline constexpr std::int64_t DT_OS_PLTRELSZ = 0x6100002d;
|
||||
inline constexpr std::int64_t DT_OS_RELA = 0x6100002f;
|
||||
inline constexpr std::int64_t DT_OS_RELASZ = 0x61000031;
|
||||
inline constexpr std::int64_t DT_OS_RELAENT = 0x61000033;
|
||||
inline constexpr std::int64_t DT_OS_STRTAB = 0x61000035;
|
||||
inline constexpr std::int64_t DT_OS_STRSZ = 0x61000037;
|
||||
inline constexpr std::int64_t DT_OS_SYMTAB = 0x61000039;
|
||||
inline constexpr std::int64_t DT_OS_SYMENT = 0x6100003b;
|
||||
inline constexpr std::int64_t DT_OS_SYMTABSZ = 0x6100003f;
|
||||
|
||||
inline constexpr std::uint32_t R_X86_64_NONE = 0;
|
||||
inline constexpr std::uint32_t R_X86_64_64 = 1;
|
||||
inline constexpr std::uint32_t R_X86_64_PC32 = 2;
|
||||
inline constexpr std::uint32_t R_X86_64_GOT32 = 3;
|
||||
inline constexpr std::uint32_t R_X86_64_PLT32 = 4;
|
||||
inline constexpr std::uint32_t R_X86_64_COPY = 5;
|
||||
inline constexpr std::uint32_t R_X86_64_GLOB_DAT = 6;
|
||||
inline constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7;
|
||||
inline constexpr std::uint32_t R_X86_64_RELATIVE = 8;
|
||||
inline constexpr std::uint32_t R_X86_64_GOTPCREL = 9;
|
||||
inline constexpr std::uint32_t R_X86_64_32 = 10;
|
||||
inline constexpr std::uint32_t R_X86_64_32S = 11;
|
||||
inline constexpr std::uint32_t R_X86_64_GOTPCRELX = 41;
|
||||
inline constexpr std::uint32_t R_X86_64_REX_GOTPCRELX = 42;
|
||||
|
||||
inline constexpr std::uint8_t STB_GLOBAL = 1;
|
||||
inline constexpr std::uint8_t STB_WEAK = 2;
|
||||
inline constexpr std::uint8_t STT_FUNC = 2;
|
||||
inline constexpr std::uint8_t STV_DEFAULT = 0;
|
||||
inline constexpr std::uint16_t SHN_UNDEF = 0;
|
||||
|
||||
inline constexpr std::uint32_t SHT_NULL = 0;
|
||||
inline constexpr std::uint32_t SHT_PROGBITS = 1;
|
||||
inline constexpr std::uint32_t SHT_SYMTAB = 2;
|
||||
inline constexpr std::uint32_t SHT_STRTAB = 3;
|
||||
inline constexpr std::uint32_t SHT_DYNAMIC = 6;
|
||||
inline constexpr std::uint32_t SHT_DYNSYM = 11;
|
||||
|
||||
@@ -15,25 +15,6 @@ public:
|
||||
) override;
|
||||
|
||||
private:
|
||||
static constexpr std::uint32_t STB_GLOBAL = 1;
|
||||
static constexpr std::uint32_t STT_FUNC = 2;
|
||||
static constexpr std::uint32_t STV_DEFAULT = 0;
|
||||
static constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7;
|
||||
static constexpr std::uint32_t R_X86_64_GLOB_DAT = 6;
|
||||
|
||||
static constexpr std::int64_t DT_NEEDED = 1;
|
||||
static constexpr std::int64_t DT_SYMTAB = 6;
|
||||
static constexpr std::int64_t DT_STRTAB = 5;
|
||||
static constexpr std::int64_t DT_STRSZ = 10;
|
||||
static constexpr std::int64_t DT_RELA = 7;
|
||||
static constexpr std::int64_t DT_RELASZ = 8;
|
||||
static constexpr std::int64_t DT_RELAENT = 9;
|
||||
static constexpr std::int64_t DT_JMPREL = 23;
|
||||
static constexpr std::int64_t DT_PLTRELSZ = 2;
|
||||
static constexpr std::int64_t DT_PLTREL = 20;
|
||||
static constexpr std::int64_t DT_SYMENT = 11;
|
||||
static constexpr std::int64_t DT_NULL = 0;
|
||||
|
||||
void _appendU64(std::vector<std::uint8_t>& buf, std::uint64_t v) const;
|
||||
void _appendI64(std::vector<std::uint8_t>& buf, std::int64_t v) const;
|
||||
void _appendDynEntry(std::vector<std::uint8_t>& buf, std::int64_t tag, std::uint64_t val) const;
|
||||
|
||||
@@ -27,36 +27,6 @@ private:
|
||||
std::shared_ptr<IUnusedNidFilter> _unusedNidFilter;
|
||||
std::uint32_t unusedFilterLevel;
|
||||
|
||||
static constexpr std::uint32_t PT_LOAD = 1;
|
||||
static constexpr std::uint32_t PT_DYNAMIC = 2;
|
||||
static constexpr std::uint32_t PF_X = 0x1;
|
||||
|
||||
static constexpr std::int64_t DT_NEEDED = 0x00000001;
|
||||
static constexpr std::int64_t DT_PLTRELSZ = 0x00000002;
|
||||
static constexpr std::int64_t DT_PLTGOT = 0x00000003;
|
||||
static constexpr std::int64_t DT_STRTAB = 0x00000005;
|
||||
static constexpr std::int64_t DT_SYMTAB = 0x00000006;
|
||||
static constexpr std::int64_t DT_RELA = 0x00000007;
|
||||
static constexpr std::int64_t DT_RELASZ = 0x00000008;
|
||||
static constexpr std::int64_t DT_RELAENT = 0x00000009;
|
||||
static constexpr std::int64_t DT_STRSZ = 0x0000000a;
|
||||
static constexpr std::int64_t DT_SYMENT = 0x0000000b;
|
||||
static constexpr std::int64_t DT_PLTREL = 0x00000014;
|
||||
static constexpr std::int64_t DT_JMPREL = 0x00000017;
|
||||
|
||||
static constexpr std::int64_t DT_OS_PLTGOT = 0x61000027;
|
||||
static constexpr std::int64_t DT_OS_PLTREL = 0x6100002b;
|
||||
static constexpr std::int64_t DT_OS_PLTRELSZ = 0x6100002d;
|
||||
static constexpr std::int64_t DT_OS_STRTAB = 0x61000035;
|
||||
static constexpr std::int64_t DT_OS_STRSZ = 0x61000037;
|
||||
static constexpr std::int64_t DT_OS_SYMTAB = 0x61000039;
|
||||
static constexpr std::int64_t DT_OS_SYMENT = 0x6100003b;
|
||||
static constexpr std::int64_t DT_OS_SYMTABSZ = 0x6100003f;
|
||||
static constexpr std::int64_t DT_OS_RELA = 0x6100002f;
|
||||
static constexpr std::int64_t DT_OS_RELASZ = 0x61000031;
|
||||
static constexpr std::int64_t DT_OS_RELAENT = 0x61000033;
|
||||
static constexpr std::int64_t DT_OS_JMPREL = 0x61000029;
|
||||
|
||||
static std::string _relocationTypeName(std::uint32_t type);
|
||||
};
|
||||
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include <relinker/analysis/UnusedNidFilter/IEntryPointCollector.hpp>
|
||||
#include <elfpatcher/general/ElfConstants.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace Relinker::UnusedNidFilter {
|
||||
|
||||
using namespace Elfpatcher;
|
||||
|
||||
namespace {
|
||||
|
||||
std::uint16_t read16(const std::vector<std::uint8_t>& b, std::size_t off) {
|
||||
@@ -65,7 +68,6 @@ public:
|
||||
|
||||
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);
|
||||
@@ -73,20 +75,6 @@ public:
|
||||
|
||||
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_INIT = 12;
|
||||
constexpr std::int64_t DT_FINI = 13;
|
||||
constexpr std::int64_t DT_INIT_ARRAY = 25;
|
||||
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_OS_INIT_ARRAY = 0x60000019;
|
||||
constexpr std::int64_t DT_OS_INIT_ARRAYSZ = 0x6000001b;
|
||||
constexpr std::int64_t DT_OS_FINI_ARRAY = 0x6000001a;
|
||||
constexpr std::int64_t DT_OS_FINI_ARRAYSZ = 0x6000001c;
|
||||
constexpr std::int64_t DT_OS_INIT = 0x6000000c;
|
||||
constexpr std::int64_t DT_OS_FINI = 0x6000000d;
|
||||
|
||||
VirtualAddress initArrayVa = 0;
|
||||
std::uint64_t initArraySz = 0;
|
||||
VirtualAddress finiArrayVa = 0;
|
||||
@@ -142,8 +130,6 @@ public:
|
||||
if (shPos + 64 > elfBytes.size()) break;
|
||||
std::uint32_t shType = read32(elfBytes, shPos + 4);
|
||||
|
||||
constexpr std::uint32_t SHT_DYNSYM = 11;
|
||||
constexpr std::uint32_t SHT_SYMTAB = 2;
|
||||
if (shType != SHT_DYNSYM && shType != SHT_SYMTAB) continue;
|
||||
|
||||
std::uint64_t symOff = read64(elfBytes, shPos + 24);
|
||||
@@ -172,10 +158,6 @@ public:
|
||||
std::uint8_t stType = info & 0xF;
|
||||
std::uint16_t shndx = read16(elfBytes, sPos + 6);
|
||||
std::uint64_t symVal = read64(elfBytes, sPos + 8);
|
||||
constexpr std::uint8_t STB_GLOBAL = 1;
|
||||
constexpr std::uint8_t STB_WEAK = 2;
|
||||
constexpr std::uint8_t STT_FUNC = 2;
|
||||
constexpr std::uint16_t SHN_UNDEF = 0;
|
||||
if ((stBind == STB_GLOBAL || stBind == STB_WEAK) &&
|
||||
stType == STT_FUNC && shndx != SHN_UNDEF) {
|
||||
addIfInText(symVal);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
#include <relinker/analysis/UnusedNidFilter/IRelativeRelocationIndex.hpp>
|
||||
#include <elfpatcher/general/ElfConstants.hpp>
|
||||
#include <unordered_map>
|
||||
#include <cstring>
|
||||
|
||||
namespace Relinker::UnusedNidFilter {
|
||||
|
||||
using namespace Elfpatcher;
|
||||
|
||||
namespace {
|
||||
|
||||
std::uint16_t read16(const std::vector<std::uint8_t>& b, std::size_t off) {
|
||||
@@ -38,7 +41,6 @@ std::uint64_t vaddrToFileOffset(const std::vector<LoadSegment>& loads, VirtualAd
|
||||
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(
|
||||
@@ -105,7 +107,6 @@ std::unique_ptr<IRelativeRelocationIndex> BuildRelativeRelocationIndex(
|
||||
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);
|
||||
@@ -121,23 +122,12 @@ std::unique_ptr<IRelativeRelocationIndex> BuildRelativeRelocationIndex(
|
||||
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;
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include "relinker/analysis//ValidationPolicy.hpp"
|
||||
#include <elfpatcher/general/ElfConstants.hpp>
|
||||
#include <sstream>
|
||||
|
||||
namespace Relinker {
|
||||
|
||||
using namespace Elfpatcher;
|
||||
|
||||
static constexpr std::uint8_t SYSCALL_BYTE0 = 0x0F;
|
||||
static constexpr std::uint8_t SYSCALL_BYTE1 = 0x05;
|
||||
static constexpr std::uint8_t INT80_BYTE0 = 0xCD;
|
||||
@@ -12,21 +15,6 @@ static constexpr std::uint8_t SYSENTER_BYTE1 = 0x34;
|
||||
static constexpr std::uint8_t SYSRET_BYTE0 = 0x0F;
|
||||
static constexpr std::uint8_t SYSRET_BYTE1 = 0x07;
|
||||
|
||||
static constexpr std::uint32_t R_X86_64_NONE = 0;
|
||||
static constexpr std::uint32_t R_X86_64_64 = 1;
|
||||
static constexpr std::uint32_t R_X86_64_PC32 = 2;
|
||||
static constexpr std::uint32_t R_X86_64_GOT32 = 3;
|
||||
static constexpr std::uint32_t R_X86_64_PLT32 = 4;
|
||||
static constexpr std::uint32_t R_X86_64_COPY = 5;
|
||||
static constexpr std::uint32_t R_X86_64_GLOB_DAT = 6;
|
||||
static constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7;
|
||||
static constexpr std::uint32_t R_X86_64_RELATIVE = 8;
|
||||
static constexpr std::uint32_t R_X86_64_GOTPCREL = 9;
|
||||
static constexpr std::uint32_t R_X86_64_32 = 10;
|
||||
static constexpr std::uint32_t R_X86_64_32S = 11;
|
||||
static constexpr std::uint32_t R_X86_64_GOTPCRELX = 41;
|
||||
static constexpr std::uint32_t R_X86_64_REX_GOTPCRELX = 42;
|
||||
|
||||
void ValidationPolicy::_initializeSupportedRelocationTypes() {
|
||||
_supportedRelocationTypes = {
|
||||
R_X86_64_NONE,
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include <relinker/output/SysVDynamicSectionBuilder.hpp>
|
||||
#include <elfpatcher/general/ElfConstants.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace Relinker {
|
||||
|
||||
using namespace Elfpatcher;
|
||||
|
||||
void SysVDynamicSectionBuilder::_appendU64(std::vector<std::uint8_t>& buf, std::uint64_t v) const {
|
||||
std::size_t pos = buf.size();
|
||||
buf.resize(pos + 8);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
#include <relinker/parsing/ElfReader.hpp>
|
||||
#include <elfpatcher/general/ElfConstants.hpp>
|
||||
#include <relinker/domain/Types.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace Relinker {
|
||||
|
||||
using namespace Elfpatcher;
|
||||
|
||||
namespace {
|
||||
|
||||
bool _rangeFits(const std::uint64_t offset, const std::uint64_t length, const std::size_t size) {
|
||||
@@ -201,8 +204,6 @@ FileByteOffset ElfReader::TranslateVirtualAddress(VirtualAddress address) const
|
||||
const VirtualAddress segVAddr = _readU64At(offset + 0x10);
|
||||
const ByteCount segFileSize = _readU64At(offset + 0x20);
|
||||
|
||||
static constexpr std::uint32_t PT_LOAD = 1;
|
||||
|
||||
if (type == PT_LOAD && address >= segVAddr && address < segVAddr + segFileSize) {
|
||||
return segOffset + (address - segVAddr);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <relinker/pipeline/RelinkerPipeline.hpp>
|
||||
#include <elfpatcher/general/ElfConstants.hpp>
|
||||
#include <relinker/analysis/ValidationPolicy.hpp>
|
||||
#include <relinker/analysis/UnusedNidFilter/PltCompactor.hpp>
|
||||
#include <sstream>
|
||||
@@ -7,6 +8,8 @@
|
||||
|
||||
namespace Relinker {
|
||||
|
||||
using namespace Elfpatcher;
|
||||
|
||||
RelinkerPipeline::RelinkerPipeline(std::shared_ptr<IElfReader> elfReader, std::shared_ptr<ISyscallScanner> syscallScanner, std::shared_ptr<ICallSiteResolver> callSiteResolver, std::shared_ptr<IValidationPolicy> validationPolicy, std::shared_ptr<ISysVDynamicSectionBuilder> dynamicSectionBuilder, std::shared_ptr<IUnusedNidFilter> unusedNidFilter, std::uint32_t unusedFilterLevel)
|
||||
: _elfReader(std::move(elfReader))
|
||||
, _syscallScanner(std::move(syscallScanner))
|
||||
@@ -218,8 +221,6 @@ RelinkResult RelinkerPipeline::Relink(const std::vector<std::uint8_t>& sourceElf
|
||||
|
||||
_validationPolicy->ValidateSyscallAbsence();
|
||||
|
||||
static constexpr std::uint32_t R_X86_64_JUMP_SLOT = 7;
|
||||
|
||||
const std::size_t originalNidCount = nidRefs.size();
|
||||
const auto originalNidRefs = nidRefs;
|
||||
std::cout << "NID input: " << originalNidCount << " references\n";
|
||||
@@ -273,8 +274,6 @@ RelinkResult RelinkerPipeline::Relink(const std::vector<std::uint8_t>& sourceElf
|
||||
}
|
||||
auto dynSection = _dynamicSectionBuilder->BuildDynamicSection(dynamicRefs, neededLibraries, dynJmpRelOffset, pltCount);
|
||||
|
||||
static constexpr std::uint32_t R_X86_64_RELATIVE = 8;
|
||||
|
||||
auto appendRela = [&](std::vector<std::uint8_t>& buf, std::uint64_t offset, std::uint64_t info, std::int64_t addend) {
|
||||
std::size_t pos = buf.size();
|
||||
buf.resize(pos + 24);
|
||||
|
||||
Reference in New Issue
Block a user