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 <noreply@anthropic.com>
This commit is contained in:
QDarkRex
2026-09-27 12:24:15 +07:00
parent 7ac1198e74
commit 908befe81d
9 changed files with 67 additions and 7 deletions
+3 -1
View File
@@ -79,8 +79,10 @@ if(BUILD_TESTING)
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_optional_plt.py $<TARGET_FILE:relinker>)
add_test(NAME empty_tls COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_empty_tls.py $<TARGET_FILE:relinker>)
add_test(NAME windows_gui COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_gui.py $<TARGET_FILE:relinker>)
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
+1
View File
@@ -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;
+6 -1
View File
@@ -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 <path>] [--lazy-binding] [--autorun] <input.elf> <output.elf>\n"
"Usage: relinker [--windows] [--windows-diagnostics] [--windows-gui] [--skip-syscall-check] [--to-intel] [unused-filter=0|1|2] [--registry] [--rpath <path>] [--lazy-binding] [--autorun] <input.elf> <output.elf>\n"
"Example: relinker input.elf output.elf"
);
@@ -7,7 +7,12 @@ namespace Elfpatcher::Windows {
class WindowsPePatcher : public IElfPatcher {
public:
explicit WindowsPePatcher(bool windowsGui = false);
std::vector<std::uint8_t> Patch(const std::vector<std::uint8_t>& sourceElf, const std::vector<Domain::ProgramHeader>& originalHeaders, const Domain::SysVDynamicSection& dynamicSection, std::uint64_t originalPltGotVaddr, const std::string& runPath, bool lazyBinding, bool dependencyDiagnostics) override;
private:
bool _windowsGui;
};
}
@@ -7,7 +7,7 @@ namespace Elfpatcher::Windows {
class WindowsPeWriter {
public:
std::vector<std::uint8_t> Write(const std::vector<PeSection>& sections, std::uint32_t entryRva, const std::array<PeDirectory, 16>& directories) const;
std::vector<std::uint8_t> Write(const std::vector<PeSection>& sections, std::uint32_t entryRva, const std::array<PeDirectory, 16>& directories, bool windowsGui = false) const;
};
}
@@ -39,6 +39,9 @@ void writeGotStub(std::vector<PeSection>& sections, const std::uint32_t targetRv
}
WindowsPePatcher::WindowsPePatcher(const bool windowsGui) : _windowsGui(windowsGui) {
}
std::vector<std::uint8_t> WindowsPePatcher::Patch(const std::vector<std::uint8_t>& sourceElf, const std::vector<Domain::ProgramHeader>& 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<std::uint8_t> WindowsPePatcher::Patch(const std::vector<std::uint8_t
sections.push_back(std::move(entry.Code));
for (const auto& lazyStub : entry.LazyStubs)
writeGotStub(sections, lazyStub.TargetRva, lazyStub.StubRva);
return WindowsPeWriter().Write(sections, entryRva, directories);
return WindowsPeWriter().Write(sections, entryRva, directories, _windowsGui);
}
}
@@ -5,7 +5,7 @@
namespace Elfpatcher::Windows {
std::vector<std::uint8_t> WindowsPeWriter::Write(const std::vector<PeSection>& sections, const std::uint32_t entryRva, const std::array<PeDirectory, 16>& directories) const {
std::vector<std::uint8_t> WindowsPeWriter::Write(const std::vector<PeSection>& sections, const std::uint32_t entryRva, const std::array<PeDirectory, 16>& 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<std::uint8_t> WindowsPeWriter::Write(const std::vector<PeSection>& 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);
+1 -1
View File
@@ -82,7 +82,7 @@ int main(const int argc, char* argv[]) {
std::shared_ptr<Elfpatcher::IElfPatcher> patcher;
if (args.toWindows) {
patcher = std::make_shared<Elfpatcher::Windows::WindowsPePatcher>();
patcher = std::make_shared<Elfpatcher::Windows::WindowsPePatcher>(args.windowsGui);
} else {
patcher = std::make_shared<Elfpatcher::Linux::LinuxElfPatcher>(
std::make_shared<Elfpatcher::EntryStubBuilder>(),
@@ -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("<I", pe, 0x3c)[0]
return struct.unpack_from("<H", pe, pe_offset + 24 + 68)[0]
def main():
relinker = Path(sys.argv[1]).resolve()
with tempfile.TemporaryDirectory(prefix="anyps5-gui-") as directory:
work = Path(directory)
source = work / "input.elf"
source.write_bytes(fixture())
cui_output = work / "cui.exe"
result = subprocess.run([str(relinker), "--windows", str(source), str(cui_output)],
capture_output=True, text=True, timeout=20)
assert result.returncode == 0, (result.stdout, result.stderr)
assert subsystem(cui_output.read_bytes()) == 3, "expected IMAGE_SUBSYSTEM_WINDOWS_CUI by default"
gui_output = work / "gui.exe"
result = subprocess.run([str(relinker), "--windows", "--windows-gui", str(source), str(gui_output)],
capture_output=True, text=True, timeout=20)
assert result.returncode == 0, (result.stdout, result.stderr)
assert subsystem(gui_output.read_bytes()) == 2, "expected IMAGE_SUBSYSTEM_WINDOWS_GUI with --windows-gui"
bad_output = work / "bad.exe"
result = subprocess.run([str(relinker), "--windows-gui", str(source), str(bad_output)],
capture_output=True, text=True, timeout=20)
assert result.returncode != 0 and not bad_output.exists(), result
print("Windows GUI subsystem integration tests passed")
if __name__ == "__main__":
main()