mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
refactor(nid): separate nid resolving logic
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
add_executable(nid_patcher
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nid/src/Sha1.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nid/src/NidCompute.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nid/src/NidResolver.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nid/src/ElfNidPatcher.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nid/src/PeNidPatcher.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/nid/src/BinaryPatcherFactory.cpp
|
||||
|
||||
@@ -5,6 +5,93 @@
|
||||
|
||||
namespace Nid {
|
||||
|
||||
struct Elf64_Ehdr {
|
||||
std::uint8_t e_ident[16];
|
||||
std::uint16_t e_type;
|
||||
std::uint16_t e_machine;
|
||||
std::uint32_t e_version;
|
||||
std::uint64_t e_entry;
|
||||
std::uint64_t e_phoff;
|
||||
std::uint64_t e_shoff;
|
||||
std::uint32_t e_flags;
|
||||
std::uint16_t e_ehsize;
|
||||
std::uint16_t e_phentsize;
|
||||
std::uint16_t e_phnum;
|
||||
std::uint16_t e_shentsize;
|
||||
std::uint16_t e_shnum;
|
||||
std::uint16_t e_shstrndx;
|
||||
};
|
||||
|
||||
struct Elf64_Shdr {
|
||||
std::uint32_t sh_name;
|
||||
std::uint32_t sh_type;
|
||||
std::uint64_t sh_flags;
|
||||
std::uint64_t sh_addr;
|
||||
std::uint64_t sh_offset;
|
||||
std::uint64_t sh_size;
|
||||
std::uint32_t sh_link;
|
||||
std::uint32_t sh_info;
|
||||
std::uint64_t sh_addralign;
|
||||
std::uint64_t sh_entsize;
|
||||
};
|
||||
|
||||
struct Elf64_Sym {
|
||||
std::uint32_t st_name;
|
||||
std::uint8_t st_info;
|
||||
std::uint8_t st_other;
|
||||
std::uint16_t st_shndx;
|
||||
std::uint64_t st_value;
|
||||
std::uint64_t st_size;
|
||||
};
|
||||
|
||||
struct Elf64_Rela {
|
||||
std::uint64_t r_offset;
|
||||
std::uint64_t r_info;
|
||||
std::int64_t r_addend;
|
||||
};
|
||||
|
||||
struct Elf64_Dyn {
|
||||
std::int64_t d_tag;
|
||||
std::uint64_t d_val;
|
||||
};
|
||||
|
||||
struct Elf64_Verneed {
|
||||
std::uint16_t vn_version;
|
||||
std::uint16_t vn_cnt;
|
||||
std::uint32_t vn_file;
|
||||
std::uint32_t vn_aux;
|
||||
std::uint32_t vn_next;
|
||||
};
|
||||
|
||||
struct Elf64_Vernaux {
|
||||
std::uint32_t vna_hash;
|
||||
std::uint16_t vna_flags;
|
||||
std::uint16_t vna_other;
|
||||
std::uint32_t vna_name;
|
||||
std::uint32_t vna_next;
|
||||
};
|
||||
|
||||
struct GnuHashLayout {
|
||||
std::uint32_t NBuckets;
|
||||
std::uint32_t SymOffset;
|
||||
std::uint32_t BloomSize;
|
||||
std::uint32_t BloomShift;
|
||||
std::size_t BloomOffset;
|
||||
std::size_t BucketsOffset;
|
||||
std::size_t ChainOffset;
|
||||
};
|
||||
|
||||
constexpr std::int64_t kDtNeeded = 1;
|
||||
constexpr std::int64_t kDtSoname = 14;
|
||||
constexpr std::int64_t kDtNull = 0;
|
||||
constexpr std::uint32_t kShtDynamic = 6u;
|
||||
constexpr std::uint32_t kShtGnuVerneed = 0x6ffffffeu;
|
||||
constexpr std::uint32_t kShtDynsym = 11u;
|
||||
constexpr std::uint32_t kShtGnuHash = 0x6ffffff6u;
|
||||
constexpr std::uint32_t kShtRela = 4u;
|
||||
constexpr std::uint8_t kStbLocal = 0u;
|
||||
constexpr std::uint16_t kShnUndef = 0u;
|
||||
|
||||
class ElfNidPatcher final : public IBinaryPatcher {
|
||||
public:
|
||||
void PatchNids(std::vector<std::uint8_t>& binary, const std::string& libraryName) const override;
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
#ifndef NID_NIDRESOLVER_HPP
|
||||
#define NID_NIDRESOLVER_HPP
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
namespace Nid {
|
||||
|
||||
std::unordered_map<std::string, std::string> ResolveNids(const std::vector<std::string>& exportedNames, const std::string& libraryName);
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,95 +1,17 @@
|
||||
#include <nid/ElfPatcher.hpp>
|
||||
#include <nid/NidCompute.hpp>
|
||||
#include "nid/NidPatcherUtils.hpp"
|
||||
#include <nid/NidResolver.hpp>
|
||||
#include <nid/NidPatcherUtils.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
namespace Nid {
|
||||
|
||||
namespace {
|
||||
|
||||
struct Elf64_Ehdr {
|
||||
std::uint8_t e_ident[16];
|
||||
std::uint16_t e_type;
|
||||
std::uint16_t e_machine;
|
||||
std::uint32_t e_version;
|
||||
std::uint64_t e_entry;
|
||||
std::uint64_t e_phoff;
|
||||
std::uint64_t e_shoff;
|
||||
std::uint32_t e_flags;
|
||||
std::uint16_t e_ehsize;
|
||||
std::uint16_t e_phentsize;
|
||||
std::uint16_t e_phnum;
|
||||
std::uint16_t e_shentsize;
|
||||
std::uint16_t e_shnum;
|
||||
std::uint16_t e_shstrndx;
|
||||
};
|
||||
|
||||
struct Elf64_Shdr {
|
||||
std::uint32_t sh_name;
|
||||
std::uint32_t sh_type;
|
||||
std::uint64_t sh_flags;
|
||||
std::uint64_t sh_addr;
|
||||
std::uint64_t sh_offset;
|
||||
std::uint64_t sh_size;
|
||||
std::uint32_t sh_link;
|
||||
std::uint32_t sh_info;
|
||||
std::uint64_t sh_addralign;
|
||||
std::uint64_t sh_entsize;
|
||||
};
|
||||
|
||||
struct Elf64_Sym {
|
||||
std::uint32_t st_name;
|
||||
std::uint8_t st_info;
|
||||
std::uint8_t st_other;
|
||||
std::uint16_t st_shndx;
|
||||
std::uint64_t st_value;
|
||||
std::uint64_t st_size;
|
||||
};
|
||||
|
||||
struct Elf64_Rela {
|
||||
std::uint64_t r_offset;
|
||||
std::uint64_t r_info;
|
||||
std::int64_t r_addend;
|
||||
};
|
||||
|
||||
struct Elf64_Dyn {
|
||||
std::int64_t d_tag;
|
||||
std::uint64_t d_val;
|
||||
};
|
||||
|
||||
struct Elf64_Verneed {
|
||||
std::uint16_t vn_version;
|
||||
std::uint16_t vn_cnt;
|
||||
std::uint32_t vn_file;
|
||||
std::uint32_t vn_aux;
|
||||
std::uint32_t vn_next;
|
||||
};
|
||||
|
||||
struct Elf64_Vernaux {
|
||||
std::uint32_t vna_hash;
|
||||
std::uint16_t vna_flags;
|
||||
std::uint16_t vna_other;
|
||||
std::uint32_t vna_name;
|
||||
std::uint32_t vna_next;
|
||||
};
|
||||
|
||||
constexpr std::int64_t kDtNeeded = 1;
|
||||
constexpr std::int64_t kDtSoname = 14;
|
||||
constexpr std::int64_t kDtNull = 0;
|
||||
constexpr std::uint32_t kShtDynamic = 6u;
|
||||
constexpr std::uint32_t kShtGnuVerneed = 0x6ffffffeu;
|
||||
|
||||
constexpr std::uint32_t kShtDynsym = 11u;
|
||||
constexpr std::uint32_t kShtGnuHash = 0x6ffffff6u;
|
||||
constexpr std::uint32_t kShtRela = 4u;
|
||||
constexpr std::uint8_t kStbLocal = 0u;
|
||||
constexpr std::uint16_t kShnUndef = 0u;
|
||||
|
||||
std::uint32_t GnuHash(const std::string& name) {
|
||||
std::uint32_t h = 5381u;
|
||||
@@ -98,19 +20,9 @@ std::uint32_t GnuHash(const std::string& name) {
|
||||
return h;
|
||||
}
|
||||
|
||||
struct GnuHashLayout {
|
||||
std::uint32_t NBuckets;
|
||||
std::uint32_t SymOffset;
|
||||
std::uint32_t BloomSize;
|
||||
std::uint32_t BloomShift;
|
||||
std::size_t BloomOffset;
|
||||
std::size_t BucketsOffset;
|
||||
std::size_t ChainOffset;
|
||||
};
|
||||
|
||||
GnuHashLayout ReadGnuHashLayout(const std::vector<std::uint8_t>& elf, std::size_t sectionOffset) {
|
||||
using namespace Internal;
|
||||
|
||||
GnuHashLayout layout{};
|
||||
layout.NBuckets = Read<std::uint32_t>(elf, sectionOffset + 0u);
|
||||
layout.SymOffset = Read<std::uint32_t>(elf, sectionOffset + 4u);
|
||||
@@ -336,10 +248,7 @@ void RemapVerneedOffsets(
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::uint32_t> CollectDynamicNameOffsets(
|
||||
const std::vector<std::uint8_t>& elf,
|
||||
const Elf64_Ehdr& ehdr
|
||||
) {
|
||||
std::vector<std::uint32_t> CollectDynamicNameOffsets(const std::vector<std::uint8_t>& elf, const Elf64_Ehdr& ehdr) {
|
||||
using namespace Internal;
|
||||
|
||||
std::vector<std::uint32_t> offsets;
|
||||
@@ -363,10 +272,7 @@ std::vector<std::uint32_t> CollectDynamicNameOffsets(
|
||||
return offsets;
|
||||
}
|
||||
|
||||
std::vector<std::uint32_t> CollectVerneedNameOffsets(
|
||||
const std::vector<std::uint8_t>& elf,
|
||||
const Elf64_Ehdr& ehdr
|
||||
) {
|
||||
std::vector<std::uint32_t> CollectVerneedNameOffsets(const std::vector<std::uint8_t>& elf, const Elf64_Ehdr& ehdr) {
|
||||
using namespace Internal;
|
||||
|
||||
std::vector<std::uint32_t> offsets;
|
||||
@@ -457,13 +363,7 @@ void ElfNidPatcher::PatchNids(std::vector<std::uint8_t>& elf, const std::string&
|
||||
elf.begin() + static_cast<std::ptrdiff_t>(dynStrOffset + dynStrSize)
|
||||
);
|
||||
|
||||
struct SymbolNameUse {
|
||||
std::size_t symIndex;
|
||||
std::uint32_t oldNameOffset;
|
||||
std::string newValue;
|
||||
};
|
||||
|
||||
std::unordered_set<std::string> seenRawNames;
|
||||
std::vector<std::string> exportedNames;
|
||||
for (std::size_t i = 1u; i < symCount; ++i) {
|
||||
const std::size_t symOffset = dynSymOffset + i * sizeof(Elf64_Sym);
|
||||
const auto sym = Read<Elf64_Sym>(elf, symOffset);
|
||||
@@ -472,15 +372,21 @@ void ElfNidPatcher::PatchNids(std::vector<std::uint8_t>& elf, const std::string&
|
||||
if (binding == kStbLocal || sym.st_shndx == kShnUndef) continue;
|
||||
const std::string symName = ReadCStr(origDynStr, sym.st_name);
|
||||
if (symName.empty()) continue;
|
||||
if (!seenRawNames.insert(symName).second)
|
||||
throw std::runtime_error("duplicate exported symbol \"" + symName + "\" in library \"" + libraryName + "\"");
|
||||
exportedNames.push_back(symName);
|
||||
}
|
||||
|
||||
const auto nidMap = ResolveNids(exportedNames, libraryName);
|
||||
|
||||
struct SymbolNameUse {
|
||||
std::size_t symIndex;
|
||||
std::uint32_t oldNameOffset;
|
||||
std::string newValue;
|
||||
};
|
||||
|
||||
std::vector<SymbolNameUse> uses;
|
||||
for (std::size_t i = 1u; i < symCount; ++i) {
|
||||
const std::size_t symOffset = dynSymOffset + i * sizeof(Elf64_Sym);
|
||||
const auto sym = Read<Elf64_Sym>(elf, symOffset);
|
||||
|
||||
if (sym.st_name == 0u) continue;
|
||||
|
||||
const std::uint8_t binding = sym.st_info >> 4u;
|
||||
@@ -490,13 +396,10 @@ void ElfNidPatcher::PatchNids(std::vector<std::uint8_t>& elf, const std::string&
|
||||
if (symName.empty()) continue;
|
||||
|
||||
std::string newValue = symName;
|
||||
if (isPatchable && !IsNidNoPatch(symName)) {
|
||||
const std::string stripped = StripNidPostfix(symName);
|
||||
const bool hasPostfix = stripped != symName;
|
||||
if (!hasPostfix && seenRawNames.count(symName + "_nid_postfix"))
|
||||
newValue = symName;
|
||||
else
|
||||
newValue = ComputeNid(stripped, libraryName);
|
||||
if (isPatchable) {
|
||||
const auto it = nidMap.find(symName);
|
||||
if (it != nidMap.end())
|
||||
newValue = it->second;
|
||||
}
|
||||
|
||||
uses.push_back(SymbolNameUse{i, sym.st_name, newValue});
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <nid/NidResolver.hpp>
|
||||
#include <nid/NidCompute.hpp>
|
||||
#include <cstdint>
|
||||
#include <nid/NidPatcherUtils.hpp>
|
||||
#include <stdexcept>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace Nid {
|
||||
|
||||
std::unordered_map<std::string, std::string> ResolveNids(const std::vector<std::string>& exportedNames, const std::string& libraryName) {
|
||||
using namespace Internal;
|
||||
|
||||
std::unordered_set<std::string> nameSet(exportedNames.begin(), exportedNames.end());
|
||||
|
||||
if (nameSet.size() != exportedNames.size()) {
|
||||
for (std::size_t i = 0u; i < exportedNames.size(); ++i) {
|
||||
for (std::size_t j = i + 1u; j < exportedNames.size(); ++j) {
|
||||
if (exportedNames[i] == exportedNames[j])
|
||||
throw std::runtime_error("duplicate exported symbol \"" + exportedNames[i] + "\" in library \"" + libraryName + "\"");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::unordered_map<std::string, std::string> result;
|
||||
result.reserve(exportedNames.size());
|
||||
|
||||
for (const std::string& name : exportedNames) {
|
||||
if (IsNidNoPatch(name)) {
|
||||
result[name] = name;
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::string stripped = StripNidPostfix(name);
|
||||
const bool hasPostfix = stripped != name;
|
||||
|
||||
if (!hasPostfix && nameSet.count(name + kNidPostfix)) {
|
||||
result[name] = name;
|
||||
continue;
|
||||
}
|
||||
|
||||
result[name] = ComputeNid(stripped, libraryName);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <nid/PePatcher.hpp>
|
||||
#include <nid/NidCompute.hpp>
|
||||
#include "nid/NidPatcherUtils.hpp"
|
||||
#include <nid/NidResolver.hpp>
|
||||
#include <nid/NidPatcherUtils.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@@ -44,7 +44,7 @@ struct PeSectionHeader {
|
||||
std::uint32_t Characteristics;
|
||||
};
|
||||
|
||||
std::size_t _findSectionOffsetByRva(const std::vector<std::uint8_t>& pe, std::uint32_t rva, std::uint32_t peHeaderOffset, std::uint16_t numberOfSections, std::uint32_t sizeOfOptionalHeader) {
|
||||
std::size_t FindSectionOffsetByRva(const std::vector<std::uint8_t>& pe, std::uint32_t rva, std::uint32_t peHeaderOffset, std::uint16_t numberOfSections, std::uint32_t sizeOfOptionalHeader) {
|
||||
const std::size_t sectionTableOffset = static_cast<std::size_t>(peHeaderOffset) + 4u + 20u + sizeOfOptionalHeader;
|
||||
for (std::uint16_t i = 0u; i < numberOfSections; ++i) {
|
||||
const std::size_t sectionOffset = sectionTableOffset + i * sizeof(PeSectionHeader);
|
||||
@@ -57,8 +57,8 @@ std::size_t _findSectionOffsetByRva(const std::vector<std::uint8_t>& pe, std::ui
|
||||
throw std::runtime_error("rva not mapped to any section");
|
||||
}
|
||||
|
||||
std::size_t _rvaToOffset(const std::vector<std::uint8_t>& pe, std::uint32_t rva, std::uint32_t peHeaderOffset, std::uint16_t numberOfSections, std::uint32_t sizeOfOptionalHeader) {
|
||||
const std::size_t sectionOffset = _findSectionOffsetByRva(pe, rva, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
std::size_t RvaToOffset(const std::vector<std::uint8_t>& pe, std::uint32_t rva, std::uint32_t peHeaderOffset, std::uint16_t numberOfSections, std::uint32_t sizeOfOptionalHeader) {
|
||||
const std::size_t sectionOffset = FindSectionOffsetByRva(pe, rva, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const auto section = Internal::Read<PeSectionHeader>(pe, sectionOffset);
|
||||
return static_cast<std::size_t>(section.PointerToRawData) + (rva - section.VirtualAddress);
|
||||
}
|
||||
@@ -95,25 +95,27 @@ void PeNidPatcher::PatchNids(std::vector<std::uint8_t>& pe, const std::string& l
|
||||
const auto exportDir = Read<PeDataDirectory>(pe, dataDirectoryOffset);
|
||||
if (exportDir.VirtualAddress == 0u) throw std::runtime_error("no export directory");
|
||||
|
||||
const std::size_t exportDirOffset = _rvaToOffset(pe, exportDir.VirtualAddress, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const std::size_t exportDirOffset = RvaToOffset(pe, exportDir.VirtualAddress, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const auto exportTable = Read<PeExportDirectory>(pe, exportDirOffset);
|
||||
|
||||
if (exportTable.NumberOfNames == 0u) throw std::runtime_error("no exported names");
|
||||
|
||||
const std::size_t namesArrayOffset = _rvaToOffset(pe, exportTable.AddressOfNames, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const std::size_t namesArrayOffset = RvaToOffset(pe, exportTable.AddressOfNames, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
|
||||
std::vector<std::uint32_t> nameRvas(exportTable.NumberOfNames);
|
||||
std::vector<std::string> names(exportTable.NumberOfNames);
|
||||
for (std::uint32_t i = 0u; i < exportTable.NumberOfNames; ++i) {
|
||||
const auto nameRva = Read<std::uint32_t>(pe, namesArrayOffset + i * 4u);
|
||||
const std::size_t nameOffset = _rvaToOffset(pe, nameRva, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const std::size_t nameOffset = RvaToOffset(pe, nameRva, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const std::string name = ReadCStr(pe, nameOffset);
|
||||
if (name.empty()) throw std::runtime_error("empty exported name");
|
||||
nameRvas[i] = nameRva;
|
||||
names[i] = name;
|
||||
}
|
||||
|
||||
const std::size_t edataSectionOffset = _findSectionOffsetByRva(pe, exportDir.VirtualAddress, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const auto nidMap = ResolveNids(names, libraryName);
|
||||
|
||||
const std::size_t edataSectionOffset = FindSectionOffsetByRva(pe, exportDir.VirtualAddress, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const auto edataSection = Read<PeSectionHeader>(pe, edataSectionOffset);
|
||||
|
||||
const auto sectionAlignment = Read<std::uint32_t>(pe, optionalHeaderOffset + 32u);
|
||||
@@ -128,8 +130,11 @@ void PeNidPatcher::PatchNids(std::vector<std::uint8_t>& pe, const std::string& l
|
||||
usedEnd = std::max(usedEnd, nameRvas[i] - edataSection.VirtualAddress + static_cast<std::uint32_t>(names[i].size()) + 1u);
|
||||
|
||||
for (std::uint32_t i = 0u; i < exportTable.NumberOfNames; ++i) {
|
||||
const std::string nid = ComputeNid(StripNidPostfix(names[i]), libraryName);
|
||||
const std::size_t oldNameOffset = _rvaToOffset(pe, nameRvas[i], peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
const auto it = nidMap.find(names[i]);
|
||||
if (it == nidMap.end()) throw std::runtime_error("symbol not in nid map: " + names[i]);
|
||||
const std::string& nid = it->second;
|
||||
|
||||
const std::size_t oldNameOffset = RvaToOffset(pe, nameRvas[i], peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
|
||||
|
||||
if (nid.size() <= names[i].size()) {
|
||||
std::memcpy(pe.data() + oldNameOffset, nid.data(), nid.size());
|
||||
|
||||
Reference in New Issue
Block a user