From 3c5fe188b7f7cf926019df1f25bfb70c75d24f9c Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Tue, 29 Sep 2026 06:32:04 +1000 Subject: [PATCH 1/7] fix(relinker): forward Windows arguments to guest startup --- .../src/windows/WindowsEntryStubBuilder.cpp | 61 ++++++++++++++++++- .../src/windows/WindowsImportBuilder.cpp | 2 +- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp index 9fdd57d9c..13b4caeb3 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp @@ -12,6 +12,8 @@ namespace { constexpr std::uint32_t PathCapacity = 32768; constexpr std::uint32_t ErrorMessageCapacity = 32768; +constexpr std::uint32_t ArgumentCapacity = 32768; +constexpr std::uint32_t ArgumentTextCapacity = ArgumentCapacity * 3; std::string normalizeRunPath(std::string path) { if (path.empty() || std::any_of(path.begin(), path.end(), [](const unsigned char value) { return value == 0 || value >= 128; })) @@ -54,6 +56,12 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con const auto programPath = reserve(PathCapacity); const auto modulePath = reserve(PathCapacity); + const auto argumentBlock = reserve(8); + const auto argumentCount = reserve(4); + const auto wideArguments = reserve(8); + const auto shellHandle = reserve(8); + const auto shellLibrary = addString("shell32.dll"); + const auto parseArguments = addString("CommandLineToArgvW"); const auto handles = reserve(libraries.size() * 8); const auto guestFinished = reserve(4); const WindowsGuestStartup guestStartup; @@ -98,6 +106,8 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con std::vector errors = {"FAIL: cannot obtain executable path\n", "FAIL: executable or library path is too long\n", "FAIL: executable path has no directory\n"}; for (const auto& import : imports) errors.push_back("FAIL: unresolved ELF import " + import.Name + "\n"); + const auto argumentError = errors.size(); + errors.push_back("FAIL: cannot prepare command-line arguments\n"); std::vector errorRvas; for (const auto& error : errors) errorRvas.push_back(addString(error)); @@ -327,9 +337,54 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con guestStartup.Initialize(code, guestModules, handles); writeString(enteringElf); - code.Emit({0x48, 0xc7, 0x44, 0x24, 0x40, 1, 0, 0, 0}); - code.Rip({0x48, 0x8d, 0x05}, programPath); - code.Emit({0x48, 0x89, 0x44, 0x24, 0x48, 0x31, 0xc0, 0x48, 0x89, 0x44, 0x24, 0x50, 0x48, 0x89, 0x44, 0x24, 0x58, 0x48, 0x8d, 0x7c, 0x24, 0x40}); + code.Rip({0x48, 0x8d, 0x0d}, shellLibrary); + code.Emit({0x31, 0xd2, 0x41, 0xb8, 0, 8, 0, 0}); + call("LoadLibraryExA"); + requireNonzero(argumentError, 0xc0000135u); + code.Rip({0x48, 0x89, 0x05}, shellHandle); + code.Emit({0x48, 0x89, 0xc1}); + code.Rip({0x48, 0x8d, 0x15}, parseArguments); + call("GetProcAddress"); + requireNonzero(argumentError, 0xc0000139u); + code.Emit({0x48, 0x89, 0xc3}); + call("GetCommandLineW"); + code.Emit({0x48, 0x89, 0xc1}); + code.Rip({0x48, 0x8d, 0x15}, argumentCount); + code.Emit({0xff, 0xd3}); + requireNonzero(argumentError, 0xc000000du); + code.Rip({0x48, 0x89, 0x05}, wideArguments); + code.Emit({0x48, 0x89, 0xc3}); + code.Rip({0x8b, 0x2d}, argumentCount); + code.Emit({0x8d, 0x45, 0xff, 0x3d}); + code.U32(ArgumentCapacity - 1); + const auto countFits = code.Branch({0x0f, 0x82}); + fail(argumentError, 0xc000000du); + code.PatchBranch(countFits, code.GetRva()); + code.Emit({0x31, 0xc9, 0xba}); + code.U32((ArgumentCapacity + 3) * 8 + ArgumentTextCapacity); + code.Emit({0x41, 0xb8, 0, 0x30, 0, 0, 0x41, 0xb9, 4, 0, 0, 0}); + call("VirtualAlloc"); + requireNonzero(argumentError, 0xc0000017u); + code.Rip({0x48, 0x89, 0x05}, argumentBlock); + code.Emit({0x48, 0x89, 0x28, 0x48, 0x8d, 0x70, 8, 0x48, 0x8d, 0xb8}); + code.U32((ArgumentCapacity + 3) * 8); + code.Emit({0x41, 0xbc}); + code.U32(ArgumentTextCapacity); + const auto convertArgument = code.GetRva(); + code.Emit({0x48, 0x89, 0x3e, 0xb9}); + code.U32(65001); + code.Emit({0xba, 0x80, 0, 0, 0, 0x4c, 0x8b, 0x03, 0x41, 0xb9, 0xff, 0xff, 0xff, 0xff}); + code.Emit({0x48, 0x89, 0x7c, 0x24, 0x20, 0x44, 0x89, 0x64, 0x24, 0x28}); + code.Emit({0x48, 0xc7, 0x44, 0x24, 0x30, 0, 0, 0, 0, 0x48, 0xc7, 0x44, 0x24, 0x38, 0, 0, 0, 0}); + call("WideCharToMultiByte"); + requireNonzero(argumentError, 0xc000000du); + code.Emit({0x48, 0x01, 0xc7, 0x41, 0x29, 0xc4, 0x48, 0x83, 0xc3, 8, 0x48, 0x83, 0xc6, 8, 0xff, 0xcd}); + code.Rip({0x0f, 0x85}, convertArgument); + code.Rip({0x48, 0x8b, 0x0d}, wideArguments); + call("LocalFree"); + code.Rip({0x48, 0x8b, 0x0d}, shellHandle); + call("FreeLibrary"); + code.Rip({0x48, 0x8b, 0x3d}, argumentBlock); const auto exitCallback = code.Branch({0x48, 0x8d, 0x35}); code.Rip({0xe8}, entryRva); code.Emit({0x89, 0x44, 0x24, 0x58}); diff --git a/core/relinker/elfpatcher/src/windows/WindowsImportBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsImportBuilder.cpp index 7a449830c..26a775b87 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsImportBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsImportBuilder.cpp @@ -5,7 +5,7 @@ namespace Elfpatcher::Windows { WindowsImports WindowsImportBuilder::Build(const std::uint32_t sectionRva) const { - const std::vector names = {"ExitProcess", "FormatMessageA", "GetFileAttributesA", "GetLastError", "GetModuleFileNameA", "GetModuleHandleA", "GetProcAddress", "GetStdHandle", "GetSystemDirectoryA", "LoadLibraryExA", "RaiseException", "VirtualAlloc", "WriteFile", "lstrcatA", "lstrcmpA", "lstrcmpiA", "lstrcpyA", "lstrlenA"}; + const std::vector names = {"ExitProcess", "FormatMessageA", "FreeLibrary", "GetCommandLineW", "GetFileAttributesA", "GetLastError", "GetModuleFileNameA", "GetModuleHandleA", "GetProcAddress", "GetStdHandle", "GetSystemDirectoryA", "LoadLibraryExA", "LocalFree", "RaiseException", "VirtualAlloc", "WideCharToMultiByte", "WriteFile", "lstrcatA", "lstrcmpA", "lstrcmpiA", "lstrcpyA", "lstrlenA"}; WindowsImports result{{".idata", sectionRva, SectionRead | SectionWrite | 0x40u, std::vector(40)}, {sectionRva, 40}, {}, {}}; auto& bytes = result.Section.Data; const auto lookupOffset = bytes.size(); From ad6b37ed42160d309de1bcb7286e9993fc831918 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Tue, 29 Sep 2026 06:32:04 +1000 Subject: [PATCH 2/7] fix(relinker): resolve guest dependencies and lifecycle arrays --- .../domain/include/domain/GuestRuntime.hpp | 2 + .../windows/WindowsGuestStartup.hpp | 2 +- .../elfpatcher/windows/WindowsLoadImage.hpp | 4 +- .../src/linux/LinuxGuestModuleWriter.cpp | 2 + .../src/windows/WindowsGuestModuleWriter.cpp | 4 +- .../src/windows/WindowsGuestStartup.cpp | 23 +++++++++-- .../src/windows/WindowsLoadImage.cpp | 3 +- .../include/relinker/guest/GuestImage.hpp | 3 ++ .../relinker/src/guest/GuestImageReader.cpp | 39 ++++++++++++++++++- .../relinker/src/guest/GuestModuleBuilder.cpp | 13 +++++++ 10 files changed, 84 insertions(+), 11 deletions(-) diff --git a/core/relinker/domain/include/domain/GuestRuntime.hpp b/core/relinker/domain/include/domain/GuestRuntime.hpp index 43aae70b0..a90225b54 100644 --- a/core/relinker/domain/include/domain/GuestRuntime.hpp +++ b/core/relinker/domain/include/domain/GuestRuntime.hpp @@ -17,6 +17,8 @@ struct GuestImport { struct GuestRuntime { std::string Path; std::vector Imports; + std::vector InitArrayRvas; + std::vector FiniArrayRvas; std::uint32_t InitRva = 0; std::uint32_t FiniRva = 0; bool UsePlatformTlsResolver = true; diff --git a/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsGuestStartup.hpp b/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsGuestStartup.hpp index 209c8d00b..690739103 100644 --- a/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsGuestStartup.hpp +++ b/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsGuestStartup.hpp @@ -13,7 +13,7 @@ public: void Finalize(WindowsStubEmitter& code, const std::vector& modules, std::uint32_t handles, std::uint32_t finished) const; std::uint32_t EmitTlsResolver(WindowsStubEmitter& code) const; private: - void callLifecycle(WindowsStubEmitter& code, std::uint32_t handle, std::uint32_t rva) const; + void callLifecycle(WindowsStubEmitter& code, std::uint32_t handle, std::uint32_t rva, bool indirect = false) const; }; } diff --git a/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsLoadImage.hpp b/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsLoadImage.hpp index 6e331f166..8bd9ff6ec 100644 --- a/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsLoadImage.hpp +++ b/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsLoadImage.hpp @@ -8,7 +8,7 @@ namespace Elfpatcher::Windows { class WindowsLoadImage { public: - WindowsLoadImage(const std::vector& source, const std::vector& headers); + WindowsLoadImage(const std::vector& source, const std::vector& headers, bool requireEntry = true); std::uint32_t GetRva(std::uint64_t address, std::uint64_t size = 1) const; std::uint32_t GetEndRva() const; std::uint32_t GetEntryRva() const; @@ -22,7 +22,7 @@ private: std::vector data; std::vector pageFlags; std::uint64_t firstAddress; - std::uint32_t entryRva; + std::uint32_t entryRva = 0; }; } diff --git a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp index 590b34d5d..46fc14810 100644 --- a/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp @@ -96,6 +96,8 @@ std::vector GuestModuleWriter::WriteLinux(const Relinker::GuestIma tag(20, 7); tag(3, image.Got); } + if (!image.InitArray.empty()) { tag(25, image.InitArray.front()); tag(27, image.InitArray.size() * 8); } + if (!image.FiniArray.empty()) { tag(26, image.FiniArray.front()); tag(28, image.FiniArray.size() * 8); } if (init != 0) tag(12, init); if (fini != 0) tag(13, fini); tag(30, 8); diff --git a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp index 2623629ec..42a6e723f 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsGuestModuleWriter.cpp @@ -14,7 +14,7 @@ namespace Elfpatcher { std::vector GuestModuleWriter::WriteWindows(const Relinker::GuestImage& guest, Domain::GuestRuntime& runtime) const { using namespace Windows; - WindowsLoadImage image(guest.Bytes, guest.Headers); + WindowsLoadImage image(guest.Bytes, guest.Headers, false); std::vector relocations; std::vector> tlsModules; std::map targets; @@ -164,6 +164,8 @@ std::vector GuestModuleWriter::WriteWindows(const Relinker::GuestI } const auto entry = nextRva; sections.push_back({".dllmain", entry, SectionRead | SectionExecute | 0x20u, {0xb8, 1, 0, 0, 0, 0xc3}}); + for (const auto slot : guest.InitArray) runtime.InitArrayRvas.push_back(image.GetRva(slot, 8)); + for (const auto slot : guest.FiniArray) runtime.FiniArrayRvas.push_back(image.GetRva(slot, 8)); runtime.InitRva = guest.Init == 0 ? 0 : image.GetRva(guest.Init); runtime.FiniRva = guest.Fini == 0 ? 0 : image.GetRva(guest.Fini); auto result = WindowsPeWriter().Write(sections, entry, directories); diff --git a/core/relinker/elfpatcher/src/windows/WindowsGuestStartup.cpp b/core/relinker/elfpatcher/src/windows/WindowsGuestStartup.cpp index b5e212bc9..0d6068306 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsGuestStartup.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsGuestStartup.cpp @@ -13,16 +13,26 @@ void WindowsGuestStartup::WriteImport(WindowsStubEmitter& code, const PeImport& code.Emit({0x4a, 0x89, 0x04, 0x02}); } -void WindowsGuestStartup::callLifecycle(WindowsStubEmitter& code, const std::uint32_t handle, const std::uint32_t rva) const { +void WindowsGuestStartup::callLifecycle(WindowsStubEmitter& code, const std::uint32_t handle, const std::uint32_t rva, const bool indirect) const { if (rva == 0) return; code.Rip({0x48, 0x8b, 0x05}, handle); code.Emit({0xb9}); code.U32(rva); - code.Emit({0x48, 0x01, 0xc8, 0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0xff, 0xd0}); + code.Emit({0x48, 0x01, 0xc8}); + if (indirect) { + code.Emit({0x48, 0x8b, 0x00, 0x48, 0x85, 0xc0}); + const auto skip = code.Branch({0x0f, 0x84}); + code.Emit({0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0xff, 0xd0}); + code.PatchBranch(skip, code.GetRva()); + } else code.Emit({0x31, 0xff, 0x31, 0xf6, 0x31, 0xd2, 0xff, 0xd0}); } void WindowsGuestStartup::Initialize(WindowsStubEmitter& code, const std::vector& modules, const std::uint32_t handles) const { - for (std::size_t index = 0; index < modules.size(); ++index) callLifecycle(code, CheckedRva(handles + index * 8), modules[index].InitRva); + for (std::size_t index = 0; index < modules.size(); ++index) { + const auto handle = CheckedRva(handles + index * 8); + callLifecycle(code, handle, modules[index].InitRva); + for (const auto slot : modules[index].InitArrayRvas) callLifecycle(code, handle, slot, true); + } } void WindowsGuestStartup::Finalize(WindowsStubEmitter& code, const std::vector& modules, const std::uint32_t handles, const std::uint32_t finished) const { @@ -30,7 +40,12 @@ void WindowsGuestStartup::Finalize(WindowsStubEmitter& code, const std::vector 0; --index) callLifecycle(code, CheckedRva(handles + (index - 1) * 8), modules[index - 1].FiniRva); + for (std::size_t index = modules.size(); index > 0; --index) { + const auto handle = CheckedRva(handles + (index - 1) * 8); + const auto& module = modules[index - 1]; + for (auto slot = module.FiniArrayRvas.rbegin(); slot != module.FiniArrayRvas.rend(); ++slot) callLifecycle(code, handle, *slot, true); + callLifecycle(code, handle, module.FiniRva); + } code.PatchBranch(skip, code.GetRva()); } diff --git a/core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp b/core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp index 6b28f2c99..ea4158313 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp @@ -5,7 +5,7 @@ namespace Elfpatcher::Windows { -WindowsLoadImage::WindowsLoadImage(const std::vector& source, const std::vector& headers) { +WindowsLoadImage::WindowsLoadImage(const std::vector& source, const std::vector& headers, const bool requireEntry) { if (source.size() < 64 || Io::ReadU32(source, 0) != 0x464c457f || source[4] != 2 || source[5] != 1 || source[6] != 1 || Io::ReadU16(source, 18) != 62) throw Domain::RelinkerException("Windows output requires a little-endian ELF64 x86-64 image"); const auto type = Io::ReadU16(source, 16); @@ -50,6 +50,7 @@ WindowsLoadImage::WindowsLoadImage(const std::vector& source, cons pageFlags[page] |= flags; } const auto entry = Io::ReadU64(source, 24); + if (!requireEntry && entry == 0) return; entryRva = GetRva(entry); if ((pageFlags[(entryRva - LoadRva) / SectionAlignment] & SectionExecute) == 0) throw Domain::RelinkerException("ELF entry point is not executable", entry); diff --git a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp index fb2646f44..8325e8208 100644 --- a/core/relinker/relinker/include/relinker/guest/GuestImage.hpp +++ b/core/relinker/relinker/include/relinker/guest/GuestImage.hpp @@ -24,12 +24,15 @@ struct GuestSymbol { struct GuestImage { std::filesystem::path SourcePath; std::string OutputName; + std::string Soname; std::vector Bytes; std::vector Trampolines; std::vector Headers; std::vector Symbols; std::vector Dependencies; Domain::SysVDynamicSection Dynamic; + std::vector InitArray; + std::vector FiniArray; std::uint64_t Init = 0; std::uint64_t Fini = 0; std::uint64_t Got = 0; diff --git a/core/relinker/relinker/src/guest/GuestImageReader.cpp b/core/relinker/relinker/src/guest/GuestImageReader.cpp index 8dec3cf8a..4e8e5cf88 100644 --- a/core/relinker/relinker/src/guest/GuestImageReader.cpp +++ b/core/relinker/relinker/src/guest/GuestImageReader.cpp @@ -146,10 +146,14 @@ GuestImage GuestImageReader::Read(const std::filesystem::path& path, std::vector Io::AppendString(image.Dynamic.DynStrData, name); image.Symbols.push_back({name, info, visibility, section, value, size}); } + if (tags.contains(14)) { + image.Soname = string(tags.at(14)); + if (image.Soname.empty() || image.Soname.find_first_of("\\:$\r\n") != std::string::npos) fail("Invalid guest SONAME: " + image.Soname); + } std::set dependencies; for (const auto offset : needed) { const auto name = string(offset); - if (name.empty() || name.find_first_of("/\\:$") != std::string::npos || !dependencies.insert(name).second) fail("Invalid or duplicate dependency: " + name); + if (name.empty() || name.find_first_of("\\:$\r\n") != std::string::npos || !dependencies.insert(name).second) fail("Invalid or duplicate dependency: " + name); image.Dependencies.push_back(name); } std::map relocationTargets; @@ -192,7 +196,38 @@ GuestImage GuestImageReader::Read(const std::filesystem::path& path, std::vector if (tags.contains(13)) image.Fini = tags.at(13); if (image.Init != 0) mapped(image.Init, 1, 1); if (image.Fini != 0) mapped(image.Fini, 1, 1); - if ((tags.contains(27) && tags.at(27) != 0) || (tags.contains(28) && tags.at(28) != 0)) fail("Separate guest INIT_ARRAY/FINI_ARRAY is not supported"); + const auto readLifecycleArray = [&](std::uint64_t addressTag, std::uint64_t sizeTag, std::vector& entries) { + if (!tags.contains(addressTag) && !tags.contains(sizeTag)) return; + if (!tags.contains(sizeTag) || (tags.at(sizeTag) != 0 && !tags.contains(addressTag))) fail("Incomplete guest lifecycle array"); + const auto size = tags.at(sizeTag); + if (size == 0) return; + if (size % 8 != 0) fail("Unaligned guest lifecycle array size"); + const auto address = tags.at(addressTag); + mapped(address, size, 4); + const auto fileOffset = translate(address, size); + for (std::uint64_t offset = 0; offset < size; offset += 8) { + bool relocated = false; + for (const auto* table : {&image.Dynamic.RelaData, &image.Dynamic.RelaPltData}) { + for (std::size_t position = 0; position < table->size(); position += 24) { + if (Io::ReadU64(*table, position) != address + offset) continue; + relocated = true; + const auto info = Io::ReadU64(*table, position + 8); + const auto addend = Io::ReadU64(*table, position + 16); + const auto type = static_cast(info); + if (type == 8) mapped(addend, 1, 1); + else if (type == 1) { + const auto& symbol = image.Symbols.at(info >> 32); + if ((symbol.Info & 15) != 2 || addend != 0) fail("Invalid guest lifecycle function relocation"); + if (symbol.Section != 0) mapped(symbol.Value, 1, 1); + } else fail("Unsupported guest lifecycle relocation"); + } + } + if (!relocated && Io::ReadU64(bytes, fileOffset + offset) != 0) fail("Unrelocated guest lifecycle pointer"); + entries.push_back(address + offset); + } + }; + readLifecycleArray(25, 27, image.InitArray); + readLifecycleArray(26, 28, image.FiniArray); if (tags.contains(33) && tags.at(33) != 0 && image.Init == 0) fail("Guest PREINIT_ARRAY has no module initializer"); image.Bytes = std::move(bytes); return image; diff --git a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp index 831ce2012..27bd97b78 100644 --- a/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp +++ b/core/relinker/relinker/src/guest/GuestModuleBuilder.cpp @@ -71,9 +71,21 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path } images.push_back(std::move(image)); } + std::map guestNames; + for (std::size_t index = 0; index < images.size(); ++index) { + for (const auto& name : {images[index].SourcePath.filename().string(), images[index].Soname}) { + if (name.empty()) continue; + const auto [found, inserted] = guestNames.emplace(name, index); + if (!inserted && found->second != index) throw Domain::RelinkerException("Ambiguous guest dependency name: " + name); + } + } std::vector> dependencies(images.size()); for (auto& image : images) image.UsePlatformTlsResolver = !exports.contains("vNe1w4diLCs"); for (std::size_t index = 0; index < images.size(); ++index) { + for (const auto& name : images[index].Dependencies) { + const auto found = guestNames.find(name); + if (found != guestNames.end() && found->second != index) dependencies[index].insert(found->second); + } for (const auto& symbol : images[index].Symbols) { if (symbol.Section != 0 || symbol.Name.empty()) continue; const auto found = exports.find(symbol.Name); @@ -99,6 +111,7 @@ std::vector GuestModuleBuilder::Build(const std::filesystem::path std::vector hostLibraries; std::set uniqueHosts; const auto addHost = [&](const std::string& name) { + if (guestNames.contains(name)) return; if (name.empty() || name.find_first_of("/\\:$") != std::string::npos) throw Domain::RelinkerException("Invalid host dependency: " + name); if (uniqueHosts.insert(name).second) hostLibraries.push_back(name); }; From 7f834fc415d2f9357535103b118a9d58775c0b76 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Tue, 29 Sep 2026 06:32:04 +1000 Subject: [PATCH 3/7] feat(libc): add C locale conversion and sample runtime exports --- core/libs/prx/libc/CMakeLists.txt | 1 + core/libs/prx/libc/src/Multibyte.cpp | 66 +++++++++++++++++++ core/libs/prx/libc/src/Strings.cpp | 31 +++++++++ .../libs/prx/libkernel/Pthread/Posix/Cond.cpp | 5 ++ 4 files changed, 103 insertions(+) create mode 100644 core/libs/prx/libc/src/Multibyte.cpp diff --git a/core/libs/prx/libc/CMakeLists.txt b/core/libs/prx/libc/CMakeLists.txt index 3fbadd044..5d8e197fb 100644 --- a/core/libs/prx/libc/CMakeLists.txt +++ b/core/libs/prx/libc/CMakeLists.txt @@ -7,6 +7,7 @@ target_sources(libc PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/Strings.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/AllocatingStrings.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/WideConversion.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/Multibyte.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/StringExtensions.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Math.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Time.cpp diff --git a/core/libs/prx/libc/src/Multibyte.cpp b/core/libs/prx/libc/src/Multibyte.cpp new file mode 100644 index 000000000..0ea78acb3 --- /dev/null +++ b/core/libs/prx/libc/src/Multibyte.cpp @@ -0,0 +1,66 @@ +#include "prx/libc/include/general/VabiMacros.hpp" +#include +#include +#include +#include + +extern "C" { + +int __mb_cur_max_nid_postfix = 1; + +int APS5_VABI ___mb_cur_max_nid_postfix() { + return 1; +} + +int APS5_VABI mbsinit_nid_postfix(const void*) { + return 1; +} + +std::size_t APS5_VABI mbrtowc_nid_postfix(std::uint16_t* destination, const char* source, std::size_t count, void*) { + if (!source) return 0; + if (!count) return static_cast(-2); + const auto value = static_cast(*source); + if (destination) *destination = value; + return value != 0; +} + +std::size_t APS5_VABI mbrlen_nid_postfix(const char* source, std::size_t count, void* state) { + return mbrtowc_nid_postfix(nullptr, source, count, state); +} + +int APS5_VABI mbtowc_nid_postfix(std::uint16_t* destination, const char* source, std::size_t count) { + const auto result = mbrtowc_nid_postfix(destination, source, count, nullptr); + if (result == static_cast(-2)) { + errno = 86; + return -1; + } + return static_cast(result); +} + +std::size_t APS5_VABI wcrtomb_nid_postfix(char* destination, std::uint16_t value, void*) { + if (!destination) return 1; + if (value > 255) { + errno = 86; + return static_cast(-1); + } + *destination = static_cast(value); + return 1; +} + +std::size_t APS5_VABI mbsrtowcs_nid_postfix(std::uint16_t* destination, const char** source, std::size_t capacity, void*) { + if (!destination) return std::strlen(*source); + std::size_t converted = 0; + while (converted < capacity) { + const auto value = static_cast((*source)[converted]); + destination[converted] = value; + if (value == 0) { + *source = nullptr; + return converted; + } + ++converted; + } + *source += converted; + return converted; +} + +} diff --git a/core/libs/prx/libc/src/Strings.cpp b/core/libs/prx/libc/src/Strings.cpp index 1f70ddc44..a7b7a1478 100644 --- a/core/libs/prx/libc/src/Strings.cpp +++ b/core/libs/prx/libc/src/Strings.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include "prx/libc/include/General.hpp" @@ -15,6 +16,10 @@ void* APS5_VABI memset_nid_postfix(void* s, int c, size_t n) { return std::memset(s, c, n); } +void APS5_VABI bzero_nid_postfix(void* destination, size_t count) { + std::memset(destination, 0, count); +} + void* APS5_VABI memcpy_nid_postfix(void* dest, const void* src, size_t n) { return std::memcpy(dest, src, n); } @@ -317,6 +322,12 @@ float APS5_VABI wcstof_nid_postfix(const wchar_t* str, wchar_t** endptr) { return std::wcstof(str, endptr); } +long double APS5_VABI wcstold_nid_postfix(const wchar_t* str, wchar_t** endptr) { + static_assert(sizeof(long double) == 16); + static_assert(std::numeric_limits::digits == 64); + return std::wcstold(str, endptr); +} + long long APS5_VABI wcstol_nid_postfix(const wchar_t* str, wchar_t** endptr, int base) { return std::wcstoll(str, endptr, base); } @@ -325,4 +336,24 @@ long long APS5_VABI wcstoll_nid_postfix(const wchar_t* str, wchar_t** endptr, in return std::wcstoll(str, endptr, base); } +unsigned long long APS5_VABI wcstoul_nid_postfix(const wchar_t* str, wchar_t** endptr, int base) { + return std::wcstoull(str, endptr, base); +} + +unsigned long long APS5_VABI wcstoull_nid_postfix(const wchar_t* str, wchar_t** endptr, int base) { + return std::wcstoull(str, endptr, base); +} + +int APS5_VABI wcscoll_nid_postfix(const wchar_t* first, const wchar_t* second) { + return std::wcscoll(first, second); +} + +size_t APS5_VABI wcsxfrm_nid_postfix(wchar_t* destination, const wchar_t* source, size_t count) { + return std::wcsxfrm(destination, source, count); +} + +size_t APS5_VABI strxfrm_nid_postfix(char* destination, const char* source, size_t count) { + return std::strxfrm(destination, source, count); +} + } diff --git a/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp b/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp index 4a77cf00f..b9a7b211c 100644 --- a/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp +++ b/core/libs/prx/libkernel/Pthread/Posix/Cond.cpp @@ -19,6 +19,11 @@ int toPosix(int result) { extern "C" { +int APS5_VABI pthread_cond_destroy_nid_postfix(PthreadCond* cond) { + if (!cond) return PosixThread::GUEST_EINVAL; + return toPosix(scePthreadCondDestroy(cond)); +} + int APS5_VABI pthread_cond_broadcast_nid_postfix(PthreadCond* cond) { return toPosix(scePthreadCondBroadcast(cond)); } From 3a651055bb9a61fd425b555b748c8629e3be36a6 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Tue, 29 Sep 2026 06:32:04 +1000 Subject: [PATCH 4/7] feat(libc): support C locale wide stream input --- core/libs/prx/libc/include/FileStream.hpp | 15 ++++++++++++++- core/libs/prx/libc/src/StandardStreams.cpp | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/core/libs/prx/libc/include/FileStream.hpp b/core/libs/prx/libc/include/FileStream.hpp index 00c9ebcb9..9c9c4e87a 100644 --- a/core/libs/prx/libc/include/FileStream.hpp +++ b/core/libs/prx/libc/include/FileStream.hpp @@ -39,6 +39,7 @@ class FileStream { std::byte _reserved[256 - sizeof(GuestFilePrefix)]{}; std::FILE* _handle; bool _dynamic; + bool encodingError = false; public: explicit FileStream(std::FILE* handle, bool dynamic = false) : _handle(handle), _dynamic(dynamic) { @@ -68,6 +69,7 @@ public: bool Reopen(const char* filename, const char* mode) { auto* previous = GetHandle(); _guest = {}; + encodingError = false; _handle = std::freopen(filename, mode, previous); if (!_handle) return false; _guest.flags = 0x10; @@ -83,7 +85,18 @@ public: _guest.readRemaining = 0; _guest.writeRemaining = 0; _guest.flags = static_cast((_guest.flags & ~0x60) | - (std::feof(GetHandle()) ? 0x20 : 0) | (std::ferror(GetHandle()) ? 0x40 : 0)); + (std::feof(GetHandle()) ? 0x20 : 0) | (std::ferror(GetHandle()) || encodingError ? 0x40 : 0)); + } + + void SetEncodingError() { + encodingError = true; + SyncStatus(); + } + + void ClearError() { + std::clearerr(GetHandle()); + encodingError = false; + SyncStatus(); } void Close() { diff --git a/core/libs/prx/libc/src/StandardStreams.cpp b/core/libs/prx/libc/src/StandardStreams.cpp index ac9f10191..530bccdf3 100644 --- a/core/libs/prx/libc/src/StandardStreams.cpp +++ b/core/libs/prx/libc/src/StandardStreams.cpp @@ -1,5 +1,6 @@ #include "prx/libc/include/FileStream.hpp" #include "prx/libc/include/general/VabiMacros.hpp" +#include #ifdef _WIN32 #include #else @@ -18,6 +19,21 @@ int APS5_VABI fgetc_nid_postfix(FileStream* stream) { stream->SyncStatus(); return result; } +std::int32_t APS5_VABI fgetwc_nid_postfix(FileStream* stream) { + return fgetc_nid_postfix(stream); +} + +std::int32_t APS5_VABI ungetwc_nid_postfix(std::int32_t value, FileStream* stream) { + if (value == -1) return -1; + if (value < 0 || value > 255) { + errno = 86; + stream->SetEncodingError(); + return -1; + } + const int result = std::ungetc(value, GetNativeStream(stream)); + stream->SyncStatus(); + return result; +} int APS5_VABI getc_nid_postfix(FileStream* stream) { return fgetc_nid_postfix(stream); } int APS5_VABI __srget_nid_postfix(FileStream* stream) { return fgetc_nid_postfix(stream); } int APS5_VABI getchar_nid_postfix() { return fgetc_nid_postfix(__stdinp_nid_postfix); } @@ -48,8 +64,7 @@ int APS5_VABI ferror_nid_postfix(FileStream* stream) { return (stream->GuestState().flags & 0x40) != 0; } void APS5_VABI clearerr_nid_postfix(FileStream* stream) { - std::clearerr(GetNativeStream(stream)); - stream->SyncStatus(); + stream->ClearError(); } int APS5_VABI fileno_nid_postfix(FileStream* stream) { #ifdef _WIN32 From d9d34a47ac61e4235bab917bcf56ea803ee34d10 Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Tue, 29 Sep 2026 06:32:04 +1000 Subject: [PATCH 5/7] fix(libc): translate guest scanning and wide formatting arguments --- .../libs/prx/libc/include/WindowsScanning.hpp | 48 ++++++++++ .../libc/include/WindowsWideFormatting.hpp | 94 +++++++++++++++++++ core/libs/prx/libc/src/Formatting.cpp | 73 ++++++++++++-- 3 files changed, 205 insertions(+), 10 deletions(-) create mode 100644 core/libs/prx/libc/include/WindowsScanning.hpp create mode 100644 core/libs/prx/libc/include/WindowsWideFormatting.hpp diff --git a/core/libs/prx/libc/include/WindowsScanning.hpp b/core/libs/prx/libc/include/WindowsScanning.hpp new file mode 100644 index 000000000..fbcded502 --- /dev/null +++ b/core/libs/prx/libc/include/WindowsScanning.hpp @@ -0,0 +1,48 @@ +#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_WINDOWSSCANNING_HPP +#define CORE_LIBS_PRX_LIBC_INCLUDE_WINDOWSSCANNING_HPP + +#include "WindowsFormatting.hpp" +#include + +namespace LibcDetail { + +inline int ScanWindows(const char* input, const char* format, const void* source) { + if (!input || !format || !source) { errno = 22; return EOF; } + std::string translated; + std::vector pointers; + FormatArguments args(source); + while (*format) { + const char value = *format++; + translated += value; + if (value != '%') continue; + if (*format == '%') { translated += *format++; continue; } + const bool suppressed = *format == '*'; + if (suppressed) translated += *format++; + while (*format >= '0' && *format <= '9') translated += *format++; + std::string length; + if (*format && std::strchr("hljztL", *format)) { + length += *format++; + if ((length == "h" && *format == 'h') || (length == "l" && *format == 'l')) length += *format++; + } + const char conversion = *format; + if (!conversion || !std::strchr("diouxXaAeEfFgGcspn[", conversion)) { errno = 22; return EOF; } + ++format; + if (std::strchr("diouxXn", conversion) && (length == "l" || length == "j" || length == "z" || length == "t")) + translated += "ll"; + else translated += length; + translated += conversion; + if (conversion == '[') { + if (*format == '^') translated += *format++; + if (*format == ']') translated += *format++; + while (*format && *format != ']') translated += *format++; + if (*format != ']') { errno = 22; return EOF; } + translated += *format++; + } + if (!suppressed) pointers.push_back(args.Next()); + } + return std::vsscanf(input, translated.c_str(), reinterpret_cast(pointers.data())); +} + +} + +#endif diff --git a/core/libs/prx/libc/include/WindowsWideFormatting.hpp b/core/libs/prx/libc/include/WindowsWideFormatting.hpp new file mode 100644 index 000000000..ce7fba99a --- /dev/null +++ b/core/libs/prx/libc/include/WindowsWideFormatting.hpp @@ -0,0 +1,94 @@ +#ifndef CORE_LIBS_PRX_LIBC_INCLUDE_WINDOWSWIDEFORMATTING_HPP +#define CORE_LIBS_PRX_LIBC_INCLUDE_WINDOWSWIDEFORMATTING_HPP + +#include "WindowsFormatting.hpp" +#include +#include +#include + +namespace LibcDetail { + +inline int FormatWideWindows(wchar_t* output, size_t capacity, const wchar_t* format, const void* source) { + if (!capacity) return -1; + if (!output || !format || !source) { errno = 22; return -1; } + static_assert(sizeof(wchar_t) == 2); + FormatArguments args(source); + std::wstring translated; + std::wstring result; + std::vector slots; + std::deque extended; + auto append = [&slots](auto value) { + static_assert(sizeof(value) <= 8); + std::uint64_t bits = 0; + std::memcpy(&bits, &value, sizeof(value)); + slots.push_back(bits); + }; + while (*format) { + const wchar_t value = *format++; + if (value != L'%') { result += value; continue; } + if (*format == L'%') { result += *format++; continue; } + translated = L"%"; + slots.clear(); + extended.clear(); + bool nullCharacter = false; + while (*format && std::wcschr(L"-+ #0", *format)) translated += *format++; + if (*format == L'*') { translated += *format++; append(args.Next()); } + else while (*format >= L'0' && *format <= L'9') translated += *format++; + if (*format == L'.') { + translated += *format++; + if (*format == L'*') { translated += *format++; append(args.Next()); } + else while (*format >= L'0' && *format <= L'9') translated += *format++; + } + std::wstring length; + if (*format && std::wcschr(L"hljztL", *format)) { + length += *format++; + if ((length == L"h" && *format == L'h') || (length == L"l" && *format == L'l')) length += *format++; + } + const wchar_t conversion = *format; + if (!conversion || !std::wcschr(L"diouxXaAeEfFgGcspn", conversion)) { errno = 22; return -1; } + ++format; + if (std::wcschr(L"diouxXn", conversion) && (length == L"l" || length == L"j" || length == L"z" || length == L"t")) + translated += L"ll"; + else if (conversion == L's' && length.empty()) translated += L"h"; + else if (conversion == L'c' && length.empty()) translated += L"l"; + else translated += length; + translated += conversion; + if (conversion == L'n') { + void* pointer = args.Next(); + if (length == L"hh") *static_cast(pointer) = static_cast(result.size()); + else if (length == L"h") *static_cast(pointer) = static_cast(result.size()); + else if (length.empty()) *static_cast(pointer) = static_cast(result.size()); + else *static_cast(pointer) = static_cast(result.size()); + continue; + } + if (conversion == L's' || conversion == L'p') append(args.Next()); + else if (conversion == L'c') { + const int character = args.Next(); + const int converted = length.empty() ? static_cast(static_cast(character)) : character; + nullCharacter = converted == 0; + append(nullCharacter ? 1 : converted); + } + else if (std::wcschr(L"aAeEfFgG", conversion)) { + if (length == L"L") { + extended.push_back(args.Next()); + append(&extended.back()); + } else append(args.Next()); + } else if (length.empty() || length == L"h" || length == L"hh") append(args.Next()); + else append(args.Next()); + if (result.size() >= capacity) return -1; + std::vector part(capacity - result.size()); + const int count = std::vswprintf(part.data(), part.size(), translated.c_str(), reinterpret_cast(slots.data())); + if (count < 0 || static_cast(count) >= part.size()) return -1; + if (nullCharacter) { + for (int i = 0; i < count; ++i) if (part[i] == 1) part[i] = 0; + } + result.append(part.data(), static_cast(count)); + } + if (result.size() >= capacity || result.size() > INT_MAX) return -1; + std::memcpy(output, result.c_str(), (result.size() + 1) * sizeof(wchar_t)); + return static_cast(result.size()); +} + +} + +#endif diff --git a/core/libs/prx/libc/src/Formatting.cpp b/core/libs/prx/libc/src/Formatting.cpp index 75af2e643..0cc9b6e63 100644 --- a/core/libs/prx/libc/src/Formatting.cpp +++ b/core/libs/prx/libc/src/Formatting.cpp @@ -6,14 +6,20 @@ #include #include #include +#include +#include +#include #include "prx/libc/include/General.hpp" #include "SceTypes.hpp" #include "prx/libc/include/VarArgsAbi.hpp" #include "prx/libc/include/FileStream.hpp" +#include "prx/libc/include/ApplicationHeap.hpp" #ifdef _WIN32 #include "prx/libc/include/WindowsFormatting.hpp" +#include "prx/libc/include/WindowsScanning.hpp" +#include "prx/libc/include/WindowsWideFormatting.hpp" #endif namespace { @@ -125,6 +131,47 @@ int ScanGuest(const char* buffer, const char* format, bool secure, NextPointer n extern "C" { +int APS5_VABI swprintf_nid_postfix(wchar_t* output, size_t capacity, const wchar_t* format, ...) { +#ifdef _WIN32 + __builtin_sysv_va_list args; + __builtin_sysv_va_start(args, format); + const int result = LibcDetail::FormatWideWindows(output, capacity, format, args); + __builtin_sysv_va_end(args); +#else + std::va_list args; + va_start(args, format); + const int result = std::vswprintf(output, capacity, format, args); + va_end(args); +#endif + return result; +} + +int APS5_VABI vasprintf_nid_postfix(char** destination, const char* format, VaList* args) { + if (!destination) { errno = 22; return -1; } + *destination = nullptr; + if (!format || !args) { errno = 22; return -1; } + try { +#ifdef _WIN32 + std::string text; + const int count = LibcDetail::FormatWindows(nullptr, 0, format, args, &text); + const char* source = text.c_str(); +#else + char* text = nullptr; + const int count = ::vasprintf(&text, format, *reinterpret_cast(args)); + std::unique_ptr owner(text, std::free); + if (count < 0) return -1; + const char* source = text; +#endif + auto* output = static_cast(ApplicationHeapAllocate_nid_no_patch(static_cast(count) + 1)); + std::memcpy(output, source, static_cast(count) + 1); + *destination = output; + return count; + } catch (const std::bad_alloc&) { + errno = 12; + return -1; + } +} + int APS5_VABI vfprintf_nid_postfix(FileStream* stream, const char* format, VaList* args) { auto* native = GetNativeStream(stream); #ifdef _WIN32 @@ -259,8 +306,23 @@ int APS5_VABI sprintf_nid_postfix(VA_ARGS) { #endif -#ifndef _WIN32 +int APS5_VABI vsscanf_nid_postfix(const char* input, const char* format, VaList* args) { +#ifdef _WIN32 + return LibcDetail::ScanWindows(input, format, args); +#else + return std::vsscanf(input, format, *reinterpret_cast(args)); +#endif +} +#ifdef _WIN32 +int APS5_VABI sscanf_nid_postfix(const char* input, const char* format, ...) { + __builtin_sysv_va_list args; + __builtin_sysv_va_start(args, format); + const int result = vsscanf_nid_postfix(input, format, reinterpret_cast(args)); + __builtin_sysv_va_end(args); + return result; +} +#else int APS5_VABI sscanf_nid_postfix(VA_ARGS) { LibcDetail::RegSaveArea regs; LibcDetail::FillRegSaveArea(regs, rdx, rcx, r8, r9, 0, 0, @@ -274,19 +336,10 @@ int APS5_VABI sscanf_nid_postfix(VA_ARGS) { *va ); } - #endif #ifdef _WIN32 -int APS5_VABI sscanf_nid_postfix(const char* buffer, const char* format, ...) { - __builtin_sysv_va_list args; - __builtin_sysv_va_start(args, format); - const int result = ScanGuest(buffer, format, false, [&] { return __builtin_va_arg(args, void*); }, [&] { return __builtin_va_arg(args, unsigned int); }); - __builtin_sysv_va_end(args); - return result; -} - int APS5_VABI sscanf_s_nid_postfix(const char* buffer, const char* format, ...) { __builtin_sysv_va_list args; __builtin_sysv_va_start(args, format); From 4e2b200a91c8ed4d5e06fcf27c084e463be4343d Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Tue, 29 Sep 2026 06:36:22 +1000 Subject: [PATCH 6/7] fix(libc): invoke guest exit callbacks with the SysV ABI --- core/libs/prx/libc/src/Process.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libc/src/Process.cpp b/core/libs/prx/libc/src/Process.cpp index 98f0d2636..48526ffff 100644 --- a/core/libs/prx/libc/src/Process.cpp +++ b/core/libs/prx/libc/src/Process.cpp @@ -27,6 +27,19 @@ std::exception_ptr shutdownFailure; std::stop_source shutdownSource; std::atomic exitRequested{false}; std::atomic exitStarted{false}; +using GuestExitCallback = void (APS5_VABI*)(); +std::mutex exitCallbackMutex; +std::vector exitCallbacks; + +void runGuestExitCallback() { + GuestExitCallback callback; + { + std::lock_guard lock(exitCallbackMutex); + callback = exitCallbacks.back(); + exitCallbacks.pop_back(); + } + callback(); +} } @@ -139,10 +152,14 @@ int* APS5_VABI __error_nid_postfix() { std::abort(); } -int APS5_VABI atexit_nid_postfix(atexit_func_t func) { +int APS5_VABI atexit_nid_postfix(GuestExitCallback func) { if (func == nullptr) return 0; - return std::atexit(func); + std::lock_guard lock(exitCallbackMutex); + exitCallbacks.push_back(func); + const int result = std::atexit(runGuestExitCallback); + if (result != 0) exitCallbacks.pop_back(); + return result; } } From a6ea90b9193d2be7dbd2aff3747a2d9e61d637fc Mon Sep 17 00:00:00 2001 From: SamG-Coder <13449963+SamG-Coder@users.noreply.github.com> Date: Tue, 29 Sep 2026 06:36:38 +1000 Subject: [PATCH 7/7] fix(libc): reject unsupported format extensions explicitly --- core/libs/prx/libc/include/WindowsScanning.hpp | 7 ++++++- core/libs/prx/libc/include/WindowsWideFormatting.hpp | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/core/libs/prx/libc/include/WindowsScanning.hpp b/core/libs/prx/libc/include/WindowsScanning.hpp index fbcded502..ff7701281 100644 --- a/core/libs/prx/libc/include/WindowsScanning.hpp +++ b/core/libs/prx/libc/include/WindowsScanning.hpp @@ -2,6 +2,7 @@ #define CORE_LIBS_PRX_LIBC_INCLUDE_WINDOWSSCANNING_HPP #include "WindowsFormatting.hpp" +#include "General.hpp" #include namespace LibcDetail { @@ -25,7 +26,11 @@ inline int ScanWindows(const char* input, const char* format, const void* source if ((length == "h" && *format == 'h') || (length == "l" && *format == 'l')) length += *format++; } const char conversion = *format; - if (!conversion || !std::strchr("diouxXaAeEfFgGcspn[", conversion)) { errno = 22; return EOF; } + if (!conversion) { errno = 22; return EOF; } + if (!std::strchr("diouxXaAeEfFgGcspn[", conversion)) { + NotImplemented_nid_no_patch("vsscanf format conversion"); + return EOF; + } ++format; if (std::strchr("diouxXn", conversion) && (length == "l" || length == "j" || length == "z" || length == "t")) translated += "ll"; diff --git a/core/libs/prx/libc/include/WindowsWideFormatting.hpp b/core/libs/prx/libc/include/WindowsWideFormatting.hpp index ce7fba99a..8d4baba34 100644 --- a/core/libs/prx/libc/include/WindowsWideFormatting.hpp +++ b/core/libs/prx/libc/include/WindowsWideFormatting.hpp @@ -2,6 +2,7 @@ #define CORE_LIBS_PRX_LIBC_INCLUDE_WINDOWSWIDEFORMATTING_HPP #include "WindowsFormatting.hpp" +#include "General.hpp" #include #include #include @@ -45,7 +46,11 @@ inline int FormatWideWindows(wchar_t* output, size_t capacity, const wchar_t* fo if ((length == L"h" && *format == L'h') || (length == L"l" && *format == L'l')) length += *format++; } const wchar_t conversion = *format; - if (!conversion || !std::wcschr(L"diouxXaAeEfFgGcspn", conversion)) { errno = 22; return -1; } + if (!conversion) { errno = 22; return -1; } + if (!std::wcschr(L"diouxXaAeEfFgGcspn", conversion)) { + NotImplemented_nid_no_patch("swprintf format conversion"); + return -1; + } ++format; if (std::wcschr(L"diouxXn", conversion) && (length == L"l" || length == L"j" || length == L"z" || length == L"t")) translated += L"ll";