mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-11-04 13:07:32 +00:00
feat(relinker): report needed modules shipped outside the module directories
This commit is contained in:
1 parent
d6eafa54d0
commit
00d6b33126
5 files changed
+141
-17
No files matched your search
@@ -2,13 +2,46 @@
|
||||
#include <io/FileReader.hpp>
|
||||
#include <io/NativePath.hpp>
|
||||
#include <io/FileIdentity.hpp>
|
||||
#include <domain/ImportModule.hpp>
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <string_view>
|
||||
#include <tuple>
|
||||
|
||||
namespace Cli {
|
||||
|
||||
namespace {
|
||||
|
||||
struct CandidateHeader {
|
||||
bool Elf = false;
|
||||
bool Self = false;
|
||||
std::uint16_t Type = 0;
|
||||
};
|
||||
|
||||
CandidateHeader ReadCandidateHeader(const std::filesystem::path& path) {
|
||||
std::ifstream stream(path, std::ios::binary);
|
||||
if (!stream) throw Domain::RelinkerException("Cannot read guest candidate: " + Io::Utf8Path(path));
|
||||
char header[18]{};
|
||||
stream.read(header, sizeof header);
|
||||
if (stream.bad()) throw Domain::RelinkerException("Cannot read guest candidate magic: " + Io::Utf8Path(path));
|
||||
CandidateHeader result;
|
||||
if (stream.gcount() < 4) return result;
|
||||
const auto byte = [&](const std::size_t index) { return static_cast<unsigned char>(header[index]); };
|
||||
result.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);
|
||||
result.Elf = byte(0) == 0x7f && header[1] == 'E' && header[2] == 'L' && header[3] == 'F';
|
||||
if (result.Elf && stream.gcount() == sizeof header) result.Type = static_cast<std::uint16_t>(byte(16) | (byte(17) << 8));
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string_view ModuleStem(const std::string_view name) {
|
||||
return name.substr(0, name.size() - Domain::ModuleSuffix(name).size());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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);
|
||||
@@ -45,17 +78,6 @@ NativeModules ReadModules(const std::filesystem::path& inputPath, const std::fil
|
||||
}
|
||||
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)) {
|
||||
@@ -66,9 +88,9 @@ NativeModules ReadModules(const std::filesystem::path& inputPath, const std::fil
|
||||
}
|
||||
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)
|
||||
const auto header = ReadCandidateHeader(entry.path());
|
||||
if (header.Elf) paths.push_back(entry.path());
|
||||
else if (header.Self)
|
||||
std::cout << "Skipping SELF container: " << entry.path().lexically_relative(root).generic_string()
|
||||
<< " (unwrap it before relinking)\n";
|
||||
}
|
||||
@@ -77,6 +99,7 @@ NativeModules ReadModules(const std::filesystem::path& inputPath, const std::fil
|
||||
std::sort(paths.begin(), paths.end());
|
||||
NativeModules result;
|
||||
result.Root = root;
|
||||
result.Directories = std::move(directories);
|
||||
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;
|
||||
@@ -107,4 +130,37 @@ NativeModules ReadModules(const std::filesystem::path& inputPath, const std::fil
|
||||
return result;
|
||||
}
|
||||
|
||||
void ReportUnconvertedNeeded(const NativeModules& modules, const std::vector<std::string>& neededLibraries, std::ostream& out) {
|
||||
std::map<std::string, std::vector<std::string>> stems;
|
||||
for (const auto& name : neededLibraries)
|
||||
if (!Domain::ModuleSuffix(name).empty()) stems[std::string(ModuleStem(name))].push_back(name);
|
||||
if (stems.empty()) return;
|
||||
struct Found {
|
||||
std::filesystem::path Relative;
|
||||
std::string Name;
|
||||
bool Self;
|
||||
};
|
||||
std::vector<Found> found;
|
||||
for (auto entry = std::filesystem::recursive_directory_iterator(modules.Root, std::filesystem::directory_options::skip_permission_denied); entry != std::filesystem::recursive_directory_iterator(); ++entry) {
|
||||
if (entry->is_directory()) {
|
||||
if (std::any_of(modules.Directories.begin(), modules.Directories.end(), [&](const auto& directory) { return SameDirectory(directory, entry->path()); })) entry.disable_recursion_pending();
|
||||
continue;
|
||||
}
|
||||
if (!entry->is_regular_file()) continue;
|
||||
const auto filename = Io::Utf8Path(entry->path().filename());
|
||||
if (Domain::ModuleSuffix(filename).empty()) continue;
|
||||
const auto stem = stems.find(std::string(ModuleStem(filename)));
|
||||
if (stem == stems.end()) continue;
|
||||
const auto header = ReadCandidateHeader(entry->path());
|
||||
if (!header.Self && !(header.Elf && header.Type == 0xfe18)) continue;
|
||||
for (const auto& name : stem->second) found.push_back({entry->path().lexically_relative(modules.Root), name, header.Self});
|
||||
}
|
||||
std::sort(found.begin(), found.end(), [](const auto& left, const auto& right) { return std::tie(left.Relative, left.Name) < std::tie(right.Relative, right.Name); });
|
||||
for (const auto& [relative, name, self] : found) {
|
||||
const auto directory = relative.parent_path().generic_string();
|
||||
out << "Needed module not converted: " << name << " at " << relative.generic_string() << "; add --module-dir " << (directory.empty() ? "." : directory)
|
||||
<< (self ? " and unwrap the SELF container" : "") << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user