feat(libs): no patch already exported from libc.prx symbols

This commit is contained in:
boykopovar
2026-09-22 18:21:08 +03:00
parent e3a960e4d1
commit a43f73163d
11 changed files with 194 additions and 15 deletions
+3 -2
View File
@@ -2,6 +2,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/ExportExclusions.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
@@ -91,8 +92,8 @@ foreach(LIB_DIR ${LIBS_DIRS})
add_custom_command(
OUTPUT ${PATCHED_FILE}
COMMAND ${CMAKE_COMMAND} -E copy ${UNPATCHED_FILE} ${PATCHED_FILE}
COMMAND $<TARGET_FILE:nid_patcher> ${LIB_NAME} ${PATCHED_FILE}
DEPENDS nid_patcher ${LIB_NAME} ${UNPATCHED_FILE}
COMMAND $<TARGET_FILE:nid_patcher> ${LIB_NAME} --preserve-exports "${LIBC_UNPATCHED}" "${PATCHED_FILE}"
DEPENDS nid_patcher ${LIB_NAME} ${UNPATCHED_FILE} ${LIBC_UNPATCHED}
COMMENT "Patching NIDs for ${LIB_NAME}"
)
+1 -1
View File
@@ -95,7 +95,7 @@ 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;
void PatchNids(std::vector<std::uint8_t>& binary, const std::string& libraryName, const std::unordered_set<std::string>& excludedExports) const override;
};
}
@@ -0,0 +1,14 @@
#ifndef NID_EXPORTEXCLUSIONS_HPP
#define NID_EXPORTEXCLUSIONS_HPP
#include <string>
#include <unordered_set>
namespace Nid {
std::string NormalizeExportName(const std::string& name);
std::unordered_set<std::string> ReadExportExclusions(const std::string& path);
}
#endif
+2 -1
View File
@@ -3,6 +3,7 @@
#include <cstdint>
#include <string>
#include <unordered_set>
#include <vector>
namespace Nid {
@@ -11,7 +12,7 @@ class IBinaryPatcher {
public:
virtual ~IBinaryPatcher() = default;
virtual void PatchNids(std::vector<std::uint8_t>& binary, const std::string& libraryName) const = 0;
virtual void PatchNids(std::vector<std::uint8_t>& binary, const std::string& libraryName, const std::unordered_set<std::string>& excludedExports) const = 0;
};
}
+2 -1
View File
@@ -3,13 +3,14 @@
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace Nid {
std::string ResolveOneName(const std::string& funcName);
std::unordered_map<std::string, std::string> ResolveNids(const std::vector<std::string>& exportedNames, const std::string& libraryName);
std::unordered_map<std::string, std::string> ResolveNids(const std::vector<std::string>& exportedNames, const std::string& libraryName, const std::unordered_set<std::string>& excludedExports);
}
+1 -1
View File
@@ -39,7 +39,7 @@ struct PeSectionHeader {
class PeNidPatcher final : public IBinaryPatcher {
public:
void PatchNids(std::vector<std::uint8_t>& binary, const std::string& libraryName) const override;
void PatchNids(std::vector<std::uint8_t>& binary, const std::string& libraryName, const std::unordered_set<std::string>& excludedExports) const override;
};
}
+2 -2
View File
@@ -308,7 +308,7 @@ std::vector<std::uint32_t> CollectVerneedNameOffsets(const std::vector<std::uint
}
void ElfNidPatcher::PatchNids(std::vector<std::uint8_t>& elf, const std::string& libraryName) const {
void ElfNidPatcher::PatchNids(std::vector<std::uint8_t>& elf, const std::string& libraryName, const std::unordered_set<std::string>& excludedExports) const {
using namespace Internal;
if (elf.size() < sizeof(Elf64_Ehdr)) throw std::runtime_error("file too small");
@@ -379,7 +379,7 @@ void ElfNidPatcher::PatchNids(std::vector<std::uint8_t>& elf, const std::string&
exportedNames.push_back(symName);
}
const auto nidMap = ResolveNids(exportedNames, libraryName);
const auto nidMap = ResolveNids(exportedNames, libraryName, excludedExports);
struct SymbolNameUse {
std::size_t symIndex;
+137
View File
@@ -0,0 +1,137 @@
#include <nid/ExportExclusions.hpp>
#include <nid/ElfPatcher.hpp>
#include <nid/PeNidPatcher.hpp>
#include <nid/NidPatcherUtils.hpp>
#include <bit>
#include <cstring>
#include <fstream>
#include <stdexcept>
namespace Nid {
namespace {
void RequireRange(const std::vector<std::uint8_t>& binary, std::uint64_t offset, std::uint64_t size) {
if (offset > binary.size() || size > binary.size() - offset) throw std::runtime_error("export reference range out of bounds");
}
template<typename TValue>
TValue ReadValue(const std::vector<std::uint8_t>& binary, std::uint64_t offset) {
RequireRange(binary, offset, sizeof(TValue));
TValue value;
std::memcpy(&value, binary.data() + offset, sizeof(value));
return value;
}
std::string ReadName(const std::vector<std::uint8_t>& binary, std::uint64_t offset, std::uint64_t size) {
RequireRange(binary, offset, size);
if (size == 0) throw std::runtime_error("empty export reference name range");
const auto start = reinterpret_cast<const char*>(binary.data() + offset);
const auto end = static_cast<const char*>(std::memchr(start, 0, size));
if (!end || end == start) throw std::runtime_error("invalid export reference name");
return {start, end};
}
std::unordered_set<std::string> ReadPeExports(const std::vector<std::uint8_t>& binary) {
const std::uint64_t header = ReadValue<std::uint32_t>(binary, 0x3c);
if (ReadValue<std::uint32_t>(binary, header) != 0x4550) throw std::runtime_error("invalid reference PE signature");
const auto sectionCount = ReadValue<std::uint16_t>(binary, header + 6);
const auto optionalSize = ReadValue<std::uint16_t>(binary, header + 20);
const auto optional = header + 24;
RequireRange(binary, optional, optionalSize);
const auto magic = ReadValue<std::uint16_t>(binary, optional);
if (magic != 0x20b && magic != 0x10b) throw std::runtime_error("unsupported reference PE optional header");
const std::uint64_t directoryOffset = magic == 0x20b ? 112 : 96;
if (optionalSize < directoryOffset + sizeof(PeDataDirectory)) throw std::runtime_error("missing reference PE export directory");
if (ReadValue<std::uint32_t>(binary, optional + directoryOffset - 4) == 0) throw std::runtime_error("missing reference PE data directories");
const auto directory = ReadValue<PeDataDirectory>(binary, optional + directoryOffset);
if (!directory.VirtualAddress || directory.Size < sizeof(PeExportDirectory)) throw std::runtime_error("missing reference PE exports");
const auto sections = optional + optionalSize;
RequireRange(binary, sections, std::uint64_t{sectionCount} * sizeof(PeSectionHeader));
const auto mapRva = [&](std::uint32_t rva, std::uint64_t size) -> std::uint64_t {
for (std::uint16_t index = 0; index < sectionCount; ++index) {
const auto section = ReadValue<PeSectionHeader>(binary, sections + std::uint64_t{index} * sizeof(PeSectionHeader));
if (rva < section.VirtualAddress) continue;
const std::uint64_t relative = rva - section.VirtualAddress;
if (relative >= section.SizeOfRawData || size > section.SizeOfRawData - relative) continue;
const auto offset = std::uint64_t{section.PointerToRawData} + relative;
RequireRange(binary, offset, size);
return offset;
}
throw std::runtime_error("unmapped reference PE export RVA");
};
const auto table = ReadValue<PeExportDirectory>(binary, mapRva(directory.VirtualAddress, sizeof(PeExportDirectory)));
if (!table.NumberOfNames) throw std::runtime_error("reference PE has no named exports");
const auto names = mapRva(table.AddressOfNames, std::uint64_t{table.NumberOfNames} * 4);
std::unordered_set<std::string> result;
for (std::uint32_t index = 0; index < table.NumberOfNames; ++index) {
const auto rva = ReadValue<std::uint32_t>(binary, names + std::uint64_t{index} * 4);
const auto offset = mapRva(rva, 1);
auto name = ReadName(binary, offset, binary.size() - offset);
mapRva(rva, name.size() + 1);
if (!result.insert(name).second) throw std::runtime_error("duplicate reference export: " + name);
}
return result;
}
std::unordered_set<std::string> ReadElfExports(const std::vector<std::uint8_t>& binary) {
const auto header = ReadValue<Elf64_Ehdr>(binary, 0);
if (header.e_ident[4] != 2 || header.e_ident[5] != 1 || header.e_shentsize != sizeof(Elf64_Shdr)) throw std::runtime_error("unsupported reference ELF layout");
RequireRange(binary, header.e_shoff, std::uint64_t{header.e_shnum} * sizeof(Elf64_Shdr));
std::unordered_set<std::string> result;
for (std::uint16_t index = 0; index < header.e_shnum; ++index) {
const auto section = ReadValue<Elf64_Shdr>(binary, header.e_shoff + std::uint64_t{index} * sizeof(Elf64_Shdr));
if (section.sh_type != kShtDynsym) continue;
if (section.sh_entsize != sizeof(Elf64_Sym) || section.sh_size % sizeof(Elf64_Sym) || section.sh_link >= header.e_shnum) throw std::runtime_error("invalid reference ELF symbol table");
RequireRange(binary, section.sh_offset, section.sh_size);
const auto strings = ReadValue<Elf64_Shdr>(binary, header.e_shoff + std::uint64_t{section.sh_link} * sizeof(Elf64_Shdr));
if (strings.sh_type != 3) throw std::runtime_error("invalid reference ELF string table");
RequireRange(binary, strings.sh_offset, strings.sh_size);
for (std::uint64_t offset = 0; offset < section.sh_size; offset += sizeof(Elf64_Sym)) {
const auto symbol = ReadValue<Elf64_Sym>(binary, section.sh_offset + offset);
const auto binding = symbol.st_info >> 4;
const auto visibility = symbol.st_other & 3;
if (symbol.st_shndx == kShnUndef || (binding != 1 && binding != 2 && binding != 10) || (visibility != 0 && visibility != 3)) continue;
if (symbol.st_name >= strings.sh_size) throw std::runtime_error("invalid reference ELF export name");
result.insert(ReadName(binary, strings.sh_offset + symbol.st_name, strings.sh_size - symbol.st_name));
}
}
if (result.empty()) throw std::runtime_error("reference ELF has no exports");
return result;
}
}
std::string NormalizeExportName(const std::string& name) {
if (name.ends_with(Internal::kNidPostfix)) return name.substr(0, name.size() - Internal::kNidPostfixLen);
return name;
}
std::unordered_set<std::string> ReadExportExclusions(const std::string& path) {
if constexpr (std::endian::native != std::endian::little) throw std::runtime_error("export references require a little-endian host");
std::ifstream input(path, std::ios::binary | std::ios::ate);
if (!input) throw std::runtime_error("cannot open export reference: " + path);
const auto size = input.tellg();
if (size <= 0) throw std::runtime_error("empty or unreadable export reference: " + path);
std::vector<std::uint8_t> binary(static_cast<std::size_t>(size));
input.seekg(0);
if (!input.read(reinterpret_cast<char*>(binary.data()), static_cast<std::streamsize>(binary.size()))) throw std::runtime_error("cannot read export reference: " + path);
std::unordered_set<std::string> exports;
if (binary.size() >= 2 && binary[0] == 'M' && binary[1] == 'Z') {
exports = ReadPeExports(binary);
} else if (binary.size() >= 4 && binary[0] == 0x7f && binary[1] == 'E' && binary[2] == 'L' && binary[3] == 'F') {
exports = ReadElfExports(binary);
} else {
throw std::runtime_error("unrecognized export reference format: " + path);
}
std::unordered_set<std::string> result;
result.reserve(exports.size());
for (const auto& name : exports) {
auto normalized = NormalizeExportName(name);
if (normalized.empty()) throw std::runtime_error("empty normalized export reference name");
result.insert(std::move(normalized));
}
return result;
}
}
+6 -1
View File
@@ -1,4 +1,5 @@
#include <nid/NidResolver.hpp>
#include <nid/ExportExclusions.hpp>
#include <nid/NidCompute.hpp>
#include <nid/NidPatcherUtils.hpp>
#include <cstdint>
@@ -19,7 +20,7 @@ std::string ResolveOneName(const std::string& funcName) {
return ComputeNid(StripNidPostfix(funcName), "");
}
std::unordered_map<std::string, std::string> ResolveNids(const std::vector<std::string>& exportedNames, const std::string& libraryName) {
std::unordered_map<std::string, std::string> ResolveNids(const std::vector<std::string>& exportedNames, const std::string& libraryName, const std::unordered_set<std::string>& excludedExports) {
using namespace Internal;
std::unordered_set<std::string> nameSet(exportedNames.begin(), exportedNames.end());
@@ -37,6 +38,10 @@ std::unordered_map<std::string, std::string> ResolveNids(const std::vector<std::
result.reserve(exportedNames.size());
for (const std::string& name : exportedNames) {
if (excludedExports.contains(NormalizeExportName(name))) {
result[name] = name;
continue;
}
if (IsNidNoPatchCut(name)) {
result[name] = StripNidNoPatchCut(name);
continue;
+2 -2
View File
@@ -35,7 +35,7 @@ std::size_t RvaToOffset(const std::vector<std::uint8_t>& pe, std::uint32_t rva,
}
void PeNidPatcher::PatchNids(std::vector<std::uint8_t>& pe, const std::string& libraryName) const {
void PeNidPatcher::PatchNids(std::vector<std::uint8_t>& pe, const std::string& libraryName, const std::unordered_set<std::string>& excludedExports) const {
using namespace Internal;
if (pe.size() < 0x40) throw std::runtime_error("file too small");
@@ -100,7 +100,7 @@ void PeNidPatcher::PatchNids(std::vector<std::uint8_t>& pe, const std::string& l
names[i] = name;
}
const auto nidMap = ResolveNids(names, libraryName);
const auto nidMap = ResolveNids(names, libraryName, excludedExports);
const std::size_t edataSectionOffset = FindSectionOffsetByRva(pe, exportDir.VirtualAddress, peHeaderOffset, numberOfSections, sizeOfOptionalHeader);
const auto edataSection = Read<PeSectionHeader>(pe, edataSectionOffset);
+24 -4
View File
@@ -1,4 +1,5 @@
#include <nid/BinaryPatcherFactory.hpp>
#include <nid/ExportExclusions.hpp>
#include <fstream>
#include <iostream>
#include <stdexcept>
@@ -23,18 +24,37 @@ void _writeFile(const std::string& path, const std::vector<std::uint8_t>& data)
int main(int argc, char* argv[]) {
if (argc < 3) {
std::cerr << "Usage: nid_patcher <library_name> <file.so> [file2.so ...]\n";
std::cerr << "Usage: nid_patcher <library_name> [--preserve-exports <unpatched-library>] <file> [file2 ...]\n";
return 1;
}
const std::string libraryName = argv[1];
std::unordered_set<std::string> excludedExports;
std::vector<std::string> paths;
try {
bool hasReference = false;
for (int i = 2; i < argc; ++i) {
const std::string argument = argv[i];
if (argument == "--preserve-exports") {
if (hasReference || i + 1 == argc) throw std::runtime_error("--preserve-exports requires exactly one reference library");
hasReference = true;
excludedExports = Nid::ReadExportExclusions(argv[++i]);
} else {
if (argument.starts_with("--")) throw std::runtime_error("unknown option: " + argument);
paths.push_back(argument);
}
}
if (paths.empty()) throw std::runtime_error("no files to patch");
} catch (const std::exception& e) {
std::cerr << "FAIL: " << e.what() << '\n';
return 2;
}
for (int i = 2; i < argc; ++i) {
const std::string path = argv[i];
for (const std::string& path : paths) {
try {
auto binary = _readFile(path);
const auto patcher = Nid::MakePatcher(binary);
patcher->PatchNids(binary, libraryName);
patcher->PatchNids(binary, libraryName, excludedExports);
_writeFile(path, binary);
std::cout << "OK: " << path << "\n";
} catch (const std::exception& e) {