Files
AnyPS5/core/relinker/cli/src/NativeModules.cpp
T
mpratsakiandClaude Opus 5.5 763702c5ce fix(relinker): use the Windows file identity in module and executable checks
Guest module discovery skipped the input executable and the
--executable check rejected an executable converted twice through
std::filesystem::equivalent, which MinGW reports as false for hard links
whose names give different permissions, such as .bin and .exe. Move the
GetFileInformationByHandle comparison to Io::SameFile and use it for
these checks and for SameOutputFile.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 02:19:01 +03:00

111 lines
6.9 KiB
C++

#include <NativeModules.hpp>
#include <io/FileReader.hpp>
#include <io/NativePath.hpp>
#include <io/FileIdentity.hpp>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
namespace Cli {
bool SameDirectory(const std::filesystem::path& first, const std::filesystem::path& second) {
if (std::filesystem::exists(first) && std::filesystem::exists(second)) return std::filesystem::equivalent(first, second);
return std::filesystem::weakly_canonical(first) == std::filesystem::weakly_canonical(second);
}
NativeModules ReadModules(const std::filesystem::path& inputPath, const std::filesystem::path& sceModulePath, const std::set<std::string>& excludedModules, const std::vector<std::string>& moduleDirectories) {
using namespace Relinker;
const auto root = std::filesystem::absolute(sceModulePath).lexically_normal();
if (!std::filesystem::exists(root)) throw Domain::RelinkerException("Guest module parent directory does not exist: " + Io::Utf8Path(root));
if (!std::filesystem::is_directory(root)) throw Domain::RelinkerException("Guest module parent path is not a directory: " + Io::Utf8Path(root));
const auto missingModuleMessage = std::string(GuestModulePattern) + " was not found in: " + Io::Utf8Path(root) + ". Use --skip-sce-module to disable guest module processing.";
const auto singular = root / "sce_module";
const auto plural = root / "sce_modules";
const auto prx = root / "prx";
const bool hasSingular = std::filesystem::exists(singular);
const bool hasPlural = std::filesystem::exists(plural);
const bool hasPrx = std::filesystem::exists(prx);
if (hasSingular && hasPlural) throw Domain::RelinkerException("Both sce_module and sce_modules exist in the guest module parent directory");
if (!hasSingular && !hasPlural && !hasPrx && moduleDirectories.empty()) throw Domain::RelinkerException(missingModuleMessage);
std::vector<std::filesystem::path> directories;
if (hasSingular || hasPlural) directories.push_back(hasSingular ? singular : plural);
if (hasPrx) directories.push_back(prx);
std::vector<std::filesystem::path> runtimeDirectories;
for (const auto& name : moduleDirectories) {
const auto relative = Io::NativePath(name).lexically_normal();
if (relative.empty() || relative.has_root_path() || *relative.begin() == "..")
throw Domain::RelinkerException("--module-dir must be a directory inside the guest module parent directory: " + name);
auto directory = (root / relative).lexically_normal();
if (!directory.has_filename()) directory = directory.parent_path();
if (!std::filesystem::is_directory(directory)) throw Domain::RelinkerException("Guest module path is not a directory: " + Io::Utf8Path(directory));
if (std::any_of(directories.begin(), directories.end(), [&](const auto& existing) { return SameDirectory(existing, directory); })) throw Domain::RelinkerException("Duplicate guest module directory: " + Io::Utf8Path(directory));
directories.push_back(directory);
runtimeDirectories.push_back(directory);
}
std::vector<std::filesystem::path> paths;
std::set<std::string> unmatchedExclusions = excludedModules;
const auto isElf = [](const std::filesystem::path& path, bool& self) {
std::ifstream stream(path, std::ios::binary);
if (!stream) throw Domain::RelinkerException("Cannot read guest candidate: " + Io::Utf8Path(path));
char magic[4]{};
stream.read(magic, 4);
if (stream.bad()) throw Domain::RelinkerException("Cannot read guest candidate magic: " + Io::Utf8Path(path));
if (stream.gcount() != 4) return false;
const auto byte = [&](const std::size_t index) { return static_cast<unsigned char>(magic[index]); };
self = (byte(0) == 0x4f && byte(1) == 0x15 && byte(2) == 0x3d && byte(3) == 0x1d) || (byte(0) == 0x54 && byte(1) == 0x14 && byte(2) == 0xf5 && byte(3) == 0xee);
return byte(0) == 0x7f && magic[1] == 'E' && magic[2] == 'L' && magic[3] == 'F';
};
for (const auto& directory : directories) {
if (!std::filesystem::is_directory(directory)) throw Domain::RelinkerException("Guest module path is not a directory: " + Io::Utf8Path(directory));
for (const auto& entry : std::filesystem::directory_iterator(directory)) {
if (Io::Utf8Path(entry.path().filename()).ends_with(GuestModuleSuffix)) continue;
if (excludedModules.contains(Io::Utf8Path(entry.path().filename()))) {
unmatchedExclusions.erase(Io::Utf8Path(entry.path().filename()));
continue;
}
if (!entry.is_regular_file()) continue;
if (Io::SameFile(entry.path(), inputPath)) continue;
bool self = false;
if (isElf(entry.path(), self)) paths.push_back(entry.path());
else if (self)
std::cout << "Skipping SELF container: " << entry.path().lexically_relative(root).generic_string()
<< " (unwrap it before relinking)\n";
}
}
if (!unmatchedExclusions.empty()) throw Domain::RelinkerException("Excluded guest module file not found: " + *unmatchedExclusions.begin());
std::sort(paths.begin(), paths.end());
NativeModules result;
result.Root = root;
result.RuntimeDirectories = std::move(runtimeDirectories);
for (const auto& path : paths) result.Modules.push_back({path, Io::FileReader().Read(Io::Utf8Path(path))});
std::map<std::size_t, std::string> sonames;
const auto soname = [&](std::size_t index) -> const std::string& {
const auto [found, inserted] = sonames.try_emplace(index);
if (inserted) found->second = GuestImageReader().Read(result.Modules[index].Path, result.Modules[index].Bytes, true).Soname;
return found->second;
};
std::vector<bool> copies(result.Modules.size());
for (std::size_t copy = 0; copy < result.Modules.size(); ++copy) {
const auto& path = result.Modules[copy].Path;
const auto name = Io::Utf8Path(path.filename());
for (std::size_t original = 0; original < result.Modules.size(); ++original) {
const auto& kept = result.Modules[original].Path;
const auto prefix = Io::Utf8Path(kept.filename());
if (kept.parent_path() != path.parent_path() || prefix.size() >= name.size() || !name.starts_with(prefix)) continue;
if (soname(copy).empty() || soname(copy) != soname(original)) continue;
copies[copy] = true;
std::cout << "Skipping backup copy: " << path.lexically_relative(root).generic_string() << " (same SONAME as "
<< kept.lexically_relative(root).generic_string() << ")\n";
break;
}
}
std::vector<GuestModuleInput> modules;
for (std::size_t index = 0; index < result.Modules.size(); ++index)
if (!copies[index]) modules.push_back(std::move(result.Modules[index]));
result.Modules = std::move(modules);
return result;
}
}