mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
fix(relinker): map module name aliases to declared dependencies
This commit is contained in:
@@ -2,11 +2,21 @@
|
||||
#define DOMAIN_IMPORTMODULE_HPP
|
||||
|
||||
#include <domain/Types.hpp>
|
||||
#include <algorithm>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
namespace Domain {
|
||||
|
||||
inline std::string ImportModule(const std::string& symbol, const std::map<std::uint64_t, std::string>& modules) {
|
||||
inline std::string ImportModuleName(std::string name) {
|
||||
if (name.ends_with(".prx")) name.resize(name.size() - 4);
|
||||
if (name.ends_with("-module")) name.resize(name.size() - 7);
|
||||
std::replace(name.begin(), name.end(), '.', '_');
|
||||
return name;
|
||||
}
|
||||
|
||||
inline std::string ImportModule(const std::string& symbol, const std::map<std::uint64_t, std::string>& modules,
|
||||
std::span<const std::string> dependencies = {}) {
|
||||
if (modules.empty()) return {};
|
||||
const auto first = symbol.find('#');
|
||||
if (first == std::string::npos) return {};
|
||||
@@ -27,6 +37,14 @@ inline std::string ImportModule(const std::string& symbol, const std::map<std::u
|
||||
if (name.empty() || name.find_first_of("/\\:$\r\n") != std::string::npos)
|
||||
throw RelinkerException("Invalid import module name: " + name);
|
||||
if (!name.ends_with(".prx")) name += ".prx";
|
||||
if (std::find(dependencies.begin(), dependencies.end(), name) != dependencies.end()) return name;
|
||||
std::string matched;
|
||||
for (const auto& dependency : dependencies) {
|
||||
if (ImportModuleName(dependency) != ImportModuleName(name)) continue;
|
||||
if (!matched.empty() && matched != dependency) throw RelinkerException("Ambiguous import module dependency: " + name);
|
||||
matched = dependency;
|
||||
}
|
||||
if (!matched.empty()) return matched;
|
||||
return name;
|
||||
}
|
||||
|
||||
|
||||
@@ -124,10 +124,12 @@ GuestImage GuestImageReader::Read(const std::filesystem::path& path, std::vector
|
||||
std::map<std::uint64_t, std::string> importModules;
|
||||
for (const auto value : moduleImports)
|
||||
if (!importModules.emplace(value >> 48, string(value & 0xffffffffu)).second) fail("Duplicate import module ID");
|
||||
std::vector<std::string> neededLibraries;
|
||||
for (const auto offset : needed) neededLibraries.push_back(string(offset));
|
||||
std::set<std::string> exports;
|
||||
for (std::uint64_t offset = 0; offset < symSize; offset += 24) {
|
||||
auto name = string(Io::ReadU32(bytes, symOffset + offset));
|
||||
const auto library = Io::ReadU16(bytes, symOffset + offset + 6) == 0 ? Domain::ImportModule(name, importModules) : std::string{};
|
||||
const auto library = Io::ReadU16(bytes, symOffset + offset + 6) == 0 ? Domain::ImportModule(name, importModules, neededLibraries) : std::string{};
|
||||
name = name.substr(0, name.find('#'));
|
||||
const auto info = bytes[symOffset + offset + 4];
|
||||
const auto visibility = bytes[symOffset + offset + 5];
|
||||
|
||||
@@ -117,8 +117,8 @@ std::vector<GuestArtifact> GuestModuleBuilder::Build(const std::filesystem::path
|
||||
}
|
||||
std::vector<std::set<std::size_t>> dependencies(images.size());
|
||||
for (auto& image : images) {
|
||||
image.UsePlatformTlsResolver = std::none_of(image.Symbols.begin(), image.Symbols.end(), [](const auto& symbol) {
|
||||
return symbol.Name == "vNe1w4diLCs" && symbol.Section != 0 && (symbol.Info >> 4) != 0 && symbol.Visibility != 1 && symbol.Visibility != 2;
|
||||
image.UsePlatformTlsResolver = !windows ? !exports.contains("vNe1w4diLCs") : std::none_of(image.Symbols.begin(), image.Symbols.end(), [](const auto& symbol) {
|
||||
return symbol.Name == "vNe1w4diLCs" && symbol.Section != 0 && symbol.Section != AbsoluteSection && (symbol.Info >> 4) != 0 && symbol.Visibility != 1 && symbol.Visibility != 2;
|
||||
});
|
||||
}
|
||||
for (std::size_t index = 0; index < images.size(); ++index) {
|
||||
|
||||
@@ -211,7 +211,7 @@ RelinkResult RelinkerPipeline::Relink(const std::vector<std::uint8_t>& sourceElf
|
||||
std::memcpy(&nameOff, raw.data() + symOff, 4);
|
||||
|
||||
const auto name = readCStr(nameOff);
|
||||
nidRefs.push_back({name, Domain::ImportModule(name, importModules), relType, pos, rOffset, rAddend});
|
||||
nidRefs.push_back({name, Domain::ImportModule(name, importModules, neededLibraries), relType, pos, rOffset, rAddend});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -131,7 +131,7 @@ def main():
|
||||
(case / name / f"{name}.prx").write_bytes(module_with_symbol(True))
|
||||
(case / "prx" / "consumer.prx").write_bytes(module_with_symbol(False))
|
||||
result, output = convert(case, windows)
|
||||
assert result.returncode == 2 and "Ambiguous guest import shared" in result.stderr, result.stderr
|
||||
assert result.returncode == 2 and "Ambiguous guest import" in result.stderr and "shared" in result.stderr, result.stderr
|
||||
assert not output.exists() and not (case / "app0").exists(), output
|
||||
print("Guest module directory integration tests passed")
|
||||
|
||||
|
||||
@@ -29,29 +29,42 @@ def provider(value, soname=None):
|
||||
return image
|
||||
|
||||
|
||||
def consumer(owner, symbol='shared#A#B', expected=22):
|
||||
def consumer(owner, symbol='shared#A#B', expected=22, module_name=None):
|
||||
image = module_with_symbol(False)
|
||||
strings = b'\0' + symbol.encode() + b'\0' + owner.encode() + b'\0'
|
||||
owner_offset = len(symbol) + 2
|
||||
module_offset = owner_offset
|
||||
if module_name is not None:
|
||||
module_offset = len(strings)
|
||||
strings += module_name.encode() + b'\0'
|
||||
image[0x800:0x800 + len(strings)] = strings
|
||||
tags = [(5, 0x2200), (10, len(strings)), (6, 0x2280), (11, 24),
|
||||
(4, 0x2240), (7, 0x2300), (8, 24), (9, 24), (12, 0x1010),
|
||||
(1, owner_offset), (0x61000045, (1 << 48) | owner_offset)]
|
||||
(1, owner_offset), (0x61000045, (1 << 48) | module_offset)]
|
||||
dynamic(image, 0x600, 176, tags)
|
||||
code = b'\xff\x15' + struct.pack('<i', 0x2320 - 0x1016) + bytes([0x83, 0xf8, expected, 0x74, 2, 0x0f, 0x0b, 0xc3])
|
||||
image[0x410:0x410 + len(code)] = code
|
||||
return image
|
||||
|
||||
|
||||
def executable(owner, symbol='shared#A#B'):
|
||||
def executable(owner, symbol='shared#A#B', module_name=None, extra_dependencies=()):
|
||||
image = main_fixture()
|
||||
struct.pack_into('<I', image, 68, 7)
|
||||
strings = b'\0' + symbol.encode() + b'\0' + owner.encode() + b'\0'
|
||||
owner_offset = len(symbol) + 2
|
||||
module_offset = owner_offset
|
||||
if module_name is not None:
|
||||
module_offset = len(strings)
|
||||
strings += module_name.encode() + b'\0'
|
||||
dependency_offsets = []
|
||||
for dependency in extra_dependencies:
|
||||
dependency_offsets.append(len(strings))
|
||||
strings += dependency.encode() + b'\0'
|
||||
image[0x4800:0x4800 + len(strings)] = strings
|
||||
tags = [(5, 0x4800), (10, len(strings)), (6, 0x4880), (11, 24),
|
||||
(4, 0x4840), (7, 0x4900), (8, 24), (9, 24),
|
||||
(1, owner_offset), (0x61000045, (1 << 48) | owner_offset)]
|
||||
(1, owner_offset), (0x61000045, (1 << 48) | module_offset)]
|
||||
tags.extend((1, offset) for offset in dependency_offsets)
|
||||
dynamic(image, 0x4600, 120, tags)
|
||||
struct.pack_into('<IIIII', image, 0x4840, 1, 2, 1, 0, 0)
|
||||
struct.pack_into('<IBBHQQ', image, 0x4898, 1, 0x12, 0, 0, 0, 0)
|
||||
@@ -65,19 +78,21 @@ def main():
|
||||
with tempfile.TemporaryDirectory(prefix='anyps5-import-modules-') as directory:
|
||||
work = Path(directory)
|
||||
|
||||
def convert(name, owner, symbol='shared#A#B', guest_owner=None):
|
||||
def convert(name, owner, symbol='shared#A#B', guest_owner=None, module_name=None, provider_name='b.prx', extra_dependencies=()):
|
||||
case = work / name
|
||||
modules = case / 'prx'
|
||||
modules.mkdir(parents=True)
|
||||
(modules / 'a.prx').write_bytes(provider(11))
|
||||
(modules / 'b.prx').write_bytes(provider(22, 'alias.prx'))
|
||||
(modules / provider_name).write_bytes(provider(22, 'alias.prx'))
|
||||
other = provider(33)
|
||||
other[0x800:0x80c] = b'\0other#A#B\0\0'
|
||||
(modules / 'c.prx').write_bytes(other)
|
||||
if guest_owner:
|
||||
(modules / 'consumer.prx').write_bytes(consumer(guest_owner, symbol))
|
||||
(modules / 'consumer.prx').write_bytes(consumer(guest_owner, symbol, module_name=module_name))
|
||||
for dependency in extra_dependencies:
|
||||
(modules / dependency).write_bytes(provider(44))
|
||||
source = case / 'input.elf'
|
||||
source.write_bytes(executable(owner, symbol))
|
||||
source.write_bytes(executable(owner, symbol, module_name, extra_dependencies))
|
||||
output = case / 'output.exe'
|
||||
result = subprocess.run([str(relinker), '--windows', str(source), str(output)], capture_output=True, text=True, timeout=30)
|
||||
return result, output
|
||||
@@ -89,6 +104,27 @@ def main():
|
||||
run = subprocess.run([str(output)], capture_output=True, text=True, timeout=30)
|
||||
assert run.returncode == (11 if owner == 'a.prx' else 22), (run.returncode, run.stdout, run.stderr)
|
||||
|
||||
for filename, module_name in [('foo.native.prx', 'foo_native'),
|
||||
('libSceFont-module.prx', 'libSceFont')]:
|
||||
result, output = convert(module_name, filename, guest_owner=filename,
|
||||
module_name=module_name, provider_name=filename)
|
||||
assert result.returncode == 0, (result.stdout, result.stderr)
|
||||
if os.name == 'nt':
|
||||
run = subprocess.run([str(output)], capture_output=True, text=True, timeout=30)
|
||||
assert run.returncode == 22, (run.returncode, run.stdout, run.stderr)
|
||||
|
||||
result, output = convert('ambiguous-alias', 'foo.native.prx', module_name='foo_native',
|
||||
provider_name='foo.native.prx', extra_dependencies=('foo.native-module.prx',))
|
||||
assert result.returncode == 2 and 'Ambiguous import module dependency' in result.stderr, result.stderr
|
||||
assert not output.exists()
|
||||
|
||||
result, output = convert('exact-before-alias', 'foo.native.prx', module_name='foo.native',
|
||||
provider_name='foo.native.prx', extra_dependencies=('foo_native.prx',))
|
||||
assert result.returncode == 0, result.stderr
|
||||
if os.name == 'nt':
|
||||
run = subprocess.run([str(output)], capture_output=True, text=True, timeout=30)
|
||||
assert run.returncode == 22, (run.returncode, run.stdout, run.stderr)
|
||||
|
||||
for symbol, message in [('shared#A#C', 'Unknown import module ID'),
|
||||
('shared#A#', 'Invalid qualified import'),
|
||||
('shared#A#?', 'Invalid import module ID'),
|
||||
|
||||
Reference in New Issue
Block a user