From a43f73163d33218fc9fc4ec5af1ca9425b4d3584 Mon Sep 17 00:00:00 2001 From: boykopovar Date: Tue, 22 Sep 2026 18:21:08 +0300 Subject: [PATCH] feat(libs): no patch already exported from libc.prx symbols --- core/libs/CMakeLists.txt | 5 +- core/libs/nid/include/nid/ElfPatcher.hpp | 2 +- .../libs/nid/include/nid/ExportExclusions.hpp | 14 ++ core/libs/nid/include/nid/IBinaryPatcher.hpp | 3 +- core/libs/nid/include/nid/NidResolver.hpp | 3 +- core/libs/nid/include/nid/PeNidPatcher.hpp | 2 +- core/libs/nid/src/ElfNidPatcher.cpp | 4 +- core/libs/nid/src/ExportExclusions.cpp | 137 ++++++++++++++++++ core/libs/nid/src/NidResolver.cpp | 7 +- core/libs/nid/src/PeNidPatcher.cpp | 4 +- core/libs/nid/src/main.cpp | 28 +++- 11 files changed, 194 insertions(+), 15 deletions(-) create mode 100644 core/libs/nid/include/nid/ExportExclusions.hpp create mode 100644 core/libs/nid/src/ExportExclusions.cpp diff --git a/core/libs/CMakeLists.txt b/core/libs/CMakeLists.txt index 55f0e8ed6..120978760 100644 --- a/core/libs/CMakeLists.txt +++ b/core/libs/CMakeLists.txt @@ -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 $ ${LIB_NAME} ${PATCHED_FILE} - DEPENDS nid_patcher ${LIB_NAME} ${UNPATCHED_FILE} + COMMAND $ ${LIB_NAME} --preserve-exports "${LIBC_UNPATCHED}" "${PATCHED_FILE}" + DEPENDS nid_patcher ${LIB_NAME} ${UNPATCHED_FILE} ${LIBC_UNPATCHED} COMMENT "Patching NIDs for ${LIB_NAME}" ) diff --git a/core/libs/nid/include/nid/ElfPatcher.hpp b/core/libs/nid/include/nid/ElfPatcher.hpp index fae3dc35c..dd70791c7 100644 --- a/core/libs/nid/include/nid/ElfPatcher.hpp +++ b/core/libs/nid/include/nid/ElfPatcher.hpp @@ -95,7 +95,7 @@ constexpr std::uint16_t kShnUndef = 0u; class ElfNidPatcher final : public IBinaryPatcher { public: - void PatchNids(std::vector& binary, const std::string& libraryName) const override; + void PatchNids(std::vector& binary, const std::string& libraryName, const std::unordered_set& excludedExports) const override; }; } diff --git a/core/libs/nid/include/nid/ExportExclusions.hpp b/core/libs/nid/include/nid/ExportExclusions.hpp new file mode 100644 index 000000000..93442d156 --- /dev/null +++ b/core/libs/nid/include/nid/ExportExclusions.hpp @@ -0,0 +1,14 @@ +#ifndef NID_EXPORTEXCLUSIONS_HPP +#define NID_EXPORTEXCLUSIONS_HPP + +#include +#include + +namespace Nid { + +std::string NormalizeExportName(const std::string& name); +std::unordered_set ReadExportExclusions(const std::string& path); + +} + +#endif diff --git a/core/libs/nid/include/nid/IBinaryPatcher.hpp b/core/libs/nid/include/nid/IBinaryPatcher.hpp index e3bb7535a..0fbcb8850 100644 --- a/core/libs/nid/include/nid/IBinaryPatcher.hpp +++ b/core/libs/nid/include/nid/IBinaryPatcher.hpp @@ -3,6 +3,7 @@ #include #include +#include #include namespace Nid { @@ -11,7 +12,7 @@ class IBinaryPatcher { public: virtual ~IBinaryPatcher() = default; - virtual void PatchNids(std::vector& binary, const std::string& libraryName) const = 0; + virtual void PatchNids(std::vector& binary, const std::string& libraryName, const std::unordered_set& excludedExports) const = 0; }; } diff --git a/core/libs/nid/include/nid/NidResolver.hpp b/core/libs/nid/include/nid/NidResolver.hpp index f884377b3..df144b451 100644 --- a/core/libs/nid/include/nid/NidResolver.hpp +++ b/core/libs/nid/include/nid/NidResolver.hpp @@ -3,13 +3,14 @@ #include #include +#include #include namespace Nid { std::string ResolveOneName(const std::string& funcName); -std::unordered_map ResolveNids(const std::vector& exportedNames, const std::string& libraryName); +std::unordered_map ResolveNids(const std::vector& exportedNames, const std::string& libraryName, const std::unordered_set& excludedExports); } diff --git a/core/libs/nid/include/nid/PeNidPatcher.hpp b/core/libs/nid/include/nid/PeNidPatcher.hpp index 2e67c830f..c674c3a80 100644 --- a/core/libs/nid/include/nid/PeNidPatcher.hpp +++ b/core/libs/nid/include/nid/PeNidPatcher.hpp @@ -39,7 +39,7 @@ struct PeSectionHeader { class PeNidPatcher final : public IBinaryPatcher { public: - void PatchNids(std::vector& binary, const std::string& libraryName) const override; + void PatchNids(std::vector& binary, const std::string& libraryName, const std::unordered_set& excludedExports) const override; }; } diff --git a/core/libs/nid/src/ElfNidPatcher.cpp b/core/libs/nid/src/ElfNidPatcher.cpp index 63c8d3d61..74c5bfd73 100644 --- a/core/libs/nid/src/ElfNidPatcher.cpp +++ b/core/libs/nid/src/ElfNidPatcher.cpp @@ -308,7 +308,7 @@ std::vector CollectVerneedNameOffsets(const std::vector& elf, const std::string& libraryName) const { +void ElfNidPatcher::PatchNids(std::vector& elf, const std::string& libraryName, const std::unordered_set& 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& 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; diff --git a/core/libs/nid/src/ExportExclusions.cpp b/core/libs/nid/src/ExportExclusions.cpp new file mode 100644 index 000000000..64d8fa747 --- /dev/null +++ b/core/libs/nid/src/ExportExclusions.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +namespace Nid { + +namespace { + +void RequireRange(const std::vector& 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 +TValue ReadValue(const std::vector& 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& 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(binary.data() + offset); + const auto end = static_cast(std::memchr(start, 0, size)); + if (!end || end == start) throw std::runtime_error("invalid export reference name"); + return {start, end}; +} + +std::unordered_set ReadPeExports(const std::vector& binary) { + const std::uint64_t header = ReadValue(binary, 0x3c); + if (ReadValue(binary, header) != 0x4550) throw std::runtime_error("invalid reference PE signature"); + const auto sectionCount = ReadValue(binary, header + 6); + const auto optionalSize = ReadValue(binary, header + 20); + const auto optional = header + 24; + RequireRange(binary, optional, optionalSize); + const auto magic = ReadValue(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(binary, optional + directoryOffset - 4) == 0) throw std::runtime_error("missing reference PE data directories"); + const auto directory = ReadValue(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(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(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 result; + for (std::uint32_t index = 0; index < table.NumberOfNames; ++index) { + const auto rva = ReadValue(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 ReadElfExports(const std::vector& binary) { + const auto header = ReadValue(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 result; + for (std::uint16_t index = 0; index < header.e_shnum; ++index) { + const auto section = ReadValue(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(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(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 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 binary(static_cast(size)); + input.seekg(0); + if (!input.read(reinterpret_cast(binary.data()), static_cast(binary.size()))) throw std::runtime_error("cannot read export reference: " + path); + std::unordered_set 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 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; +} + +} diff --git a/core/libs/nid/src/NidResolver.cpp b/core/libs/nid/src/NidResolver.cpp index 368f89e98..200565a6a 100644 --- a/core/libs/nid/src/NidResolver.cpp +++ b/core/libs/nid/src/NidResolver.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -19,7 +20,7 @@ std::string ResolveOneName(const std::string& funcName) { return ComputeNid(StripNidPostfix(funcName), ""); } -std::unordered_map ResolveNids(const std::vector& exportedNames, const std::string& libraryName) { +std::unordered_map ResolveNids(const std::vector& exportedNames, const std::string& libraryName, const std::unordered_set& excludedExports) { using namespace Internal; std::unordered_set nameSet(exportedNames.begin(), exportedNames.end()); @@ -37,6 +38,10 @@ std::unordered_map ResolveNids(const std::vector& pe, std::uint32_t rva, } -void PeNidPatcher::PatchNids(std::vector& pe, const std::string& libraryName) const { +void PeNidPatcher::PatchNids(std::vector& pe, const std::string& libraryName, const std::unordered_set& 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& 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(pe, edataSectionOffset); diff --git a/core/libs/nid/src/main.cpp b/core/libs/nid/src/main.cpp index 83aa90bf4..2f91413fd 100644 --- a/core/libs/nid/src/main.cpp +++ b/core/libs/nid/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -23,18 +24,37 @@ void _writeFile(const std::string& path, const std::vector& data) int main(int argc, char* argv[]) { if (argc < 3) { - std::cerr << "Usage: nid_patcher [file2.so ...]\n"; + std::cerr << "Usage: nid_patcher [--preserve-exports ] [file2 ...]\n"; return 1; } const std::string libraryName = argv[1]; + std::unordered_set excludedExports; + std::vector 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) {