From 908befe81d06c7d1b7233e830db79bd22a082116 Mon Sep 17 00:00:00 2001 From: QDarkRex Date: Sun, 27 Sep 2026 12:24:15 +0700 Subject: [PATCH] feat(relinker): add --windows-gui to emit a GUI subsystem executable Generated executables always used the console subsystem and opened a console window on launch. The opt-in flag writes IMAGE_SUBSYSTEM_WINDOWS_GUI. Co-Authored-By: Claude Opus 5.5 --- core/relinker/CMakeLists.txt | 4 +- core/relinker/cli/include/Cli.hpp | 1 + core/relinker/cli/src/CliArgs.cpp | 7 ++- .../elfpatcher/windows/WindowsElfPatcher.hpp | 5 +++ .../elfpatcher/windows/WindowsPeWriter.hpp | 2 +- .../src/windows/WindowsPePatcher.cpp | 5 ++- .../src/windows/WindowsPeWriter.cpp | 4 +- core/relinker/main.cpp | 2 +- .../relinker/tests/test_windows_gui.py | 44 +++++++++++++++++++ 9 files changed, 67 insertions(+), 7 deletions(-) create mode 100644 core/relinker/relinker/tests/test_windows_gui.py diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index a48325819..60005a6f8 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -79,8 +79,10 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_optional_plt.py $) add_test(NAME empty_tls COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_empty_tls.py $) + add_test(NAME windows_gui COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_gui.py $) else() - message(STATUS "Python3 interpreter not found; skipping optional_plt and empty_tls tests") + message(STATUS "Python3 interpreter not found; skipping optional_plt, empty_tls and windows_gui tests") endif() add_executable(strict_nid_filter_tests ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/StrictNidFilterTests.cpp diff --git a/core/relinker/cli/include/Cli.hpp b/core/relinker/cli/include/Cli.hpp index 369cb7f60..642d5d809 100644 --- a/core/relinker/cli/include/Cli.hpp +++ b/core/relinker/cli/include/Cli.hpp @@ -14,6 +14,7 @@ struct Args { bool lazyBinding = false; bool autorun = false; bool windowsDiagnostics = false; + bool windowsGui = false; std::uint32_t unusedFilterLevel = 0; std::string inputPath; std::string outputPath; diff --git a/core/relinker/cli/src/CliArgs.cpp b/core/relinker/cli/src/CliArgs.cpp index 12b1631f0..f5be7b56e 100644 --- a/core/relinker/cli/src/CliArgs.cpp +++ b/core/relinker/cli/src/CliArgs.cpp @@ -35,6 +35,8 @@ Args ParseArgs(int argc, char* argv[]) { args.autorun = true; } else if (arg == "--windows-diagnostics") { args.windowsDiagnostics = true; + } else if (arg == "--windows-gui") { + args.windowsGui = true; } else if (arg.rfind("--", 0) == 0 || arg == "unused-filter") { throw std::runtime_error("unknown option: " + arg); } else if (args.inputPath.empty()) { @@ -49,9 +51,12 @@ Args ParseArgs(int argc, char* argv[]) { if (args.windowsDiagnostics && !args.toWindows) throw std::runtime_error("--windows-diagnostics requires --windows"); + if (args.windowsGui && !args.toWindows) + throw std::runtime_error("--windows-gui requires --windows"); + if (args.inputPath.empty() || args.outputPath.empty()) throw std::runtime_error( - "Usage: relinker [--windows] [--windows-diagnostics] [--skip-syscall-check] [--to-intel] [unused-filter=0|1|2] [--registry] [--rpath ] [--lazy-binding] [--autorun] \n" + "Usage: relinker [--windows] [--windows-diagnostics] [--windows-gui] [--skip-syscall-check] [--to-intel] [unused-filter=0|1|2] [--registry] [--rpath ] [--lazy-binding] [--autorun] \n" "Example: relinker input.elf output.elf" ); diff --git a/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp b/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp index 2ee1017a0..409286e46 100644 --- a/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp +++ b/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsElfPatcher.hpp @@ -7,7 +7,12 @@ namespace Elfpatcher::Windows { class WindowsPePatcher : public IElfPatcher { public: + explicit WindowsPePatcher(bool windowsGui = false); + std::vector Patch(const std::vector& sourceElf, const std::vector& originalHeaders, const Domain::SysVDynamicSection& dynamicSection, std::uint64_t originalPltGotVaddr, const std::string& runPath, bool lazyBinding, bool dependencyDiagnostics) override; + +private: + bool _windowsGui; }; } diff --git a/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsPeWriter.hpp b/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsPeWriter.hpp index bfcc3d24c..9692b150b 100644 --- a/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsPeWriter.hpp +++ b/core/relinker/elfpatcher/include/elfpatcher/windows/WindowsPeWriter.hpp @@ -7,7 +7,7 @@ namespace Elfpatcher::Windows { class WindowsPeWriter { public: - std::vector Write(const std::vector& sections, std::uint32_t entryRva, const std::array& directories) const; + std::vector Write(const std::vector& sections, std::uint32_t entryRva, const std::array& directories, bool windowsGui = false) const; }; } diff --git a/core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp b/core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp index 0910e7aad..2b07c6444 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsPePatcher.cpp @@ -39,6 +39,9 @@ void writeGotStub(std::vector& sections, const std::uint32_t targetRv } +WindowsPePatcher::WindowsPePatcher(const bool windowsGui) : _windowsGui(windowsGui) { +} + std::vector WindowsPePatcher::Patch(const std::vector& sourceElf, const std::vector& originalHeaders, const Domain::SysVDynamicSection& dynamicSection, const std::uint64_t originalPltGotVaddr, const std::string& runPath, const bool lazyBinding, const bool dependencyDiagnostics) { WindowsLoadImage image(sourceElf, originalHeaders); if (originalPltGotVaddr != 0) @@ -89,7 +92,7 @@ std::vector WindowsPePatcher::Patch(const std::vector WindowsPeWriter::Write(const std::vector& sections, const std::uint32_t entryRva, const std::array& directories) const { +std::vector WindowsPeWriter::Write(const std::vector& sections, const std::uint32_t entryRva, const std::array& directories, const bool windowsGui) const { constexpr std::size_t peOffset = 0x80; constexpr std::size_t optionalOffset = peOffset + 24; constexpr std::size_t sectionTable = optionalOffset + 240; @@ -29,7 +29,7 @@ std::vector WindowsPeWriter::Write(const std::vector& s Io::WriteU16(result, optionalOffset + 48, 6); Io::WriteU16(result, optionalOffset + 50, 2); Io::WriteU32(result, optionalOffset + 60, LoadRva); - Io::WriteU16(result, optionalOffset + 68, 3); + Io::WriteU16(result, optionalOffset + 68, windowsGui ? 2 : 3); Io::WriteU16(result, optionalOffset + 70, directories[5].Size == 0 ? 0x100 : 0x160); Io::WriteU64(result, optionalOffset + 72, 0x210000); Io::WriteU64(result, optionalOffset + 80, 0x200000); diff --git a/core/relinker/main.cpp b/core/relinker/main.cpp index 90f087e60..86441ebab 100644 --- a/core/relinker/main.cpp +++ b/core/relinker/main.cpp @@ -82,7 +82,7 @@ int main(const int argc, char* argv[]) { std::shared_ptr patcher; if (args.toWindows) { - patcher = std::make_shared(); + patcher = std::make_shared(args.windowsGui); } else { patcher = std::make_shared( std::make_shared(), diff --git a/core/relinker/relinker/tests/test_windows_gui.py b/core/relinker/relinker/tests/test_windows_gui.py new file mode 100644 index 000000000..12698e829 --- /dev/null +++ b/core/relinker/relinker/tests/test_windows_gui.py @@ -0,0 +1,44 @@ +"""Validate the --windows-gui PE subsystem flag through the real ELF-to-PE pipeline.""" + +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from test_optional_plt import fixture + + +def subsystem(pe): + pe_offset = struct.unpack_from("