From 40383cd7e59d61220783f03d9fc5b78f2d813359 Mon Sep 17 00:00:00 2001 From: Queuse <58829992+Queuse@users.noreply.github.com> Date: Sun, 15 Nov 2026 06:41:07 -0300 Subject: [PATCH] fix(relinker): honor PS4 needed-module tags --- .../src/pipeline/RelinkerPipeline.cpp | 13 +- .../tests/test_windows_import_modules.py | 214 ++++++++++++++++++ 2 files changed, 224 insertions(+), 3 deletions(-) diff --git a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp index aab825ec7764..77233a82d6a1 100644 --- a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp +++ b/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp @@ -317,13 +317,20 @@ RelinkResult RelinkerPipeline::Relink() { neededLibraries.push_back(snd); } - std::map importModules; + std::vector moduleImports; + std::vector ps4ModuleImports; std::set ownModules; for (const auto& tag : dynTags) { if (tag.Tag == 0x61000043 || tag.Tag == 0x6100000d) ownModules.insert(tag.Value >> 48); - if (tag.Tag == 0x61000045 && !importModules.emplace(tag.Value >> 48, readCStr(tag.Value & 0xffffffffu)).second) - throw RelinkerException("Duplicate import module ID"); + else if (tag.Tag == 0x61000045) moduleImports.push_back(tag.Value); + else if (tag.Tag == 0x6100000f) ps4ModuleImports.push_back(tag.Value); } + if (!moduleImports.empty() && !ps4ModuleImports.empty()) + throw RelinkerException("Executable mixes PS4 and PS5 needed-module tags"); + std::map importModules; + for (const auto value : moduleImports.empty() ? ps4ModuleImports : moduleImports) + if (!importModules.emplace(value >> 48, readCStr(value & 0xffffffffu)).second) + throw RelinkerException("Duplicate import module ID"); auto relaEntryPos = [&](const FileByteOffset relaOff, const ByteCount off) -> FileByteOffset { if (relaOff > raw.size() || off > raw.size() - relaOff || relaEntSize > raw.size() - relaOff - off) diff --git a/core/relinker/relinker/tests/test_windows_import_modules.py b/core/relinker/relinker/tests/test_windows_import_modules.py index efeaad76db63..b992eae75325 100644 --- a/core/relinker/relinker/tests/test_windows_import_modules.py +++ b/core/relinker/relinker/tests/test_windows_import_modules.py @@ -593,6 +593,218 @@ class ProcessInformation(ctypes.Structure): kernel32.CloseHandle(handle) +def elf_dynamic_tags(image): + phoff, = struct.unpack_from('> 32: + names.add(elf_symbol_name(image, loads, tag_map, info >> 32)) + return names + + +def patch_executable_needed_modules(image, entries, strsz_delta=0): + out = bytearray(image) + loads, dynamic, tags = elf_dynamic_tags(out) + patched = [] + for tag, value in tags: + if tag == 0x61000045: + continue + if tag == 10 and strsz_delta: + value += strsz_delta + patched.append((tag, value)) + patched.extend(entries) + patched.append((0, 0)) + new_size = len(patched) * 16 + strtab = elf_file_offset(loads, dict(tags)[5]) + if dynamic[2] < strtab: + assert dynamic[2] + new_size <= strtab + if new_size > dynamic[5]: + assert not any(out[dynamic[2] + dynamic[5]:dynamic[2] + new_size]) + phoff, = struct.unpack_from('