From e96e9c18cc3dcc9e3d8aca9fd0133f9cbf4a0210 Mon Sep 17 00:00:00 2001 From: Nicolas Abosch <88494836+nicolasabosch@users.noreply.github.com> Date: Mon, 5 Oct 2026 22:47:35 -0300 Subject: [PATCH] fix(relinker): map module name aliases to declared dependencies --- .../domain/include/domain/ImportModule.hpp | 20 ++++++- .../relinker/src/guest/GuestImageReader.cpp | 4 +- .../relinker/src/guest/GuestModuleBuilder.cpp | 4 +- .../src/pipeline/RelinkerPipeline.cpp | 2 +- .../tests/test_guest_module_directories.py | 2 +- .../tests/test_windows_import_modules.py | 52 ++++++++++++++++--- 6 files changed, 70 insertions(+), 14 deletions(-) diff --git a/core/relinker/domain/include/domain/ImportModule.hpp b/core/relinker/domain/include/domain/ImportModule.hpp index fb78c079a..8da345c27 100644 --- a/core/relinker/domain/include/domain/ImportModule.hpp +++ b/core/relinker/domain/include/domain/ImportModule.hpp @@ -2,11 +2,21 @@ #define DOMAIN_IMPORTMODULE_HPP #include +#include +#include #include namespace Domain { -inline std::string ImportModule(const std::string& symbol, const std::map& 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& modules, + std::span 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 importModules; for (const auto value : moduleImports) if (!importModules.emplace(value >> 48, string(value & 0xffffffffu)).second) fail("Duplicate import module ID"); + std::vector neededLibraries; + for (const auto offset : needed) neededLibraries.push_back(string(offset)); std::set 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]; diff --git a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp index 9010692d3..e433f47b0 100644 --- a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp +++ b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp @@ -117,8 +117,8 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path } std::vector> 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) { diff --git a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp index 90f154767..1cb1f6c22 100644 --- a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp +++ b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp @@ -211,7 +211,7 @@ RelinkResult RelinkerPipeline::Relink(const std::vector& 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}); } }; diff --git a/core/relinker/relinker/tests/test_guest_module_directories.py b/core/relinker/relinker/tests/test_guest_module_directories.py index e1a1c0aae..e01b5dce0 100644 --- a/core/relinker/relinker/tests/test_guest_module_directories.py +++ b/core/relinker/relinker/tests/test_guest_module_directories.py @@ -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") diff --git a/core/relinker/relinker/tests/test_windows_import_modules.py b/core/relinker/relinker/tests/test_windows_import_modules.py index a83858e5c..8b25b1ee9 100644 --- a/core/relinker/relinker/tests/test_windows_import_modules.py +++ b/core/relinker/relinker/tests/test_windows_import_modules.py @@ -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('