diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index 6f1f81363..55807aa68 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -483,8 +483,43 @@ if(BUILD_TESTING) set_tests_properties(guest_export_lookup PROPERTIES TIMEOUT 30) add_test(NAME guest_needed_nested_modules COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_guest_needed_nested_modules.py $) + set(windows_stdio_host_arguments) + if(WIN32) + if(MINGW) + add_library(windows_stdio_host_ucrt SHARED elfpatcher/tests/WindowsStdioHost.cpp) + target_compile_options(windows_stdio_host_ucrt PRIVATE -mcrtdll=ucrt) + target_link_options(windows_stdio_host_ucrt PRIVATE -mcrtdll=ucrt -static-libgcc -static-libstdc++) + set_target_properties(windows_stdio_host_ucrt PROPERTIES PREFIX "" WINDOWS_EXPORT_ALL_SYMBOLS ON) + list(APPEND windows_stdio_host_arguments $) + + get_filename_component(testCompilerDirectory "${CMAKE_CXX_COMPILER}" DIRECTORY) + set(mingwDlltoolExecutable "${testCompilerDirectory}/dlltool.exe") + if(NOT EXISTS "${mingwDlltoolExecutable}") + message(FATAL_ERROR "Cannot locate MinGW DLL tool: ${mingwDlltoolExecutable}") + endif() + set(msvcrtImportLibrary "${CMAKE_CURRENT_BINARY_DIR}/windows_stdio_msvcrt_import.a") + add_custom_command(OUTPUT "${msvcrtImportLibrary}" + COMMAND "${mingwDlltoolExecutable}" -d "${CMAKE_CURRENT_SOURCE_DIR}/elfpatcher/tests/WindowsMsvcrt.def" + -l "${msvcrtImportLibrary}" -D msvcrt.dll + DEPENDS elfpatcher/tests/WindowsMsvcrt.def + VERBATIM) + add_custom_target(windows_stdio_msvcrt_import DEPENDS "${msvcrtImportLibrary}") + add_library(windows_stdio_host_msvcrt SHARED elfpatcher/tests/WindowsMsvcrtStdioHost.cpp) + add_dependencies(windows_stdio_host_msvcrt windows_stdio_msvcrt_import) + target_link_libraries(windows_stdio_host_msvcrt PRIVATE "${msvcrtImportLibrary}") + target_compile_options(windows_stdio_host_msvcrt PRIVATE -fno-builtin) + target_link_options(windows_stdio_host_msvcrt PRIVATE -nostdlib -Wl,--no-undefined -Wl,--entry,0) + set_target_properties(windows_stdio_host_msvcrt PROPERTIES PREFIX "" WINDOWS_EXPORT_ALL_SYMBOLS ON) + list(APPEND windows_stdio_host_arguments $) + else() + add_library(windows_stdio_host SHARED elfpatcher/tests/WindowsStdioHost.cpp) + set_target_properties(windows_stdio_host PROPERTIES PREFIX "" WINDOWS_EXPORT_ALL_SYMBOLS ON) + list(APPEND windows_stdio_host_arguments $) + endif() + endif() add_test(NAME windows_import_modules COMMAND ${Python3_EXECUTABLE} - ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_import_modules.py $) + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_import_modules.py + $ ${windows_stdio_host_arguments}) add_test(NAME elf_header COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_elf_header.py $) add_test(NAME weak_imports COMMAND ${Python3_EXECUTABLE} diff --git a/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp index dae65fc68..058f64d14 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp @@ -102,6 +102,9 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con const auto argumentCount = reserve(4); const auto wideArguments = reserve(8); const auto shellHandle = reserve(8); + const auto logHandle = reserve(8); + const std::u16string logFileName = u"anyps5.log"; + const auto logName = addWideString(logFileName); const auto shellLibrary = addString("shell32.dll"); const auto parseArguments = addString("CommandLineToArgvW"); const auto handles = reserve(libraries.size() * 8); @@ -327,6 +330,48 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con code.PatchBranch(converted, code.GetRva()); }; + const auto logMissingHandle = [&](const std::uint32_t standardHandle) { + const auto units = CheckedRva(logFileName.size() + 1); + code.Emit({0xb9}); + code.U32(standardHandle); + call("GetStdHandle"); + code.Emit({0x48, 0x85, 0xc0}); + const auto missing = code.Branch({0x0f, 0x84}); + code.Emit({0x48, 0x83, 0xf8, 0xff}); + const auto present = code.Branch({0x0f, 0x85}); + code.PatchBranch(missing, code.GetRva()); + code.Rip({0x48, 0x8b, 0x05}, logHandle); + code.Emit({0x48, 0x85, 0xc0}); + const auto opened = code.Branch({0x0f, 0x85}); + code.Emit({0x4c, 0x89, 0xe0, 0x48, 0x29, 0xd8, 0x48, 0x05}); + code.U32(units * 2); + code.Emit({0x48, 0x3d}); + code.U32(PathCapacity * 2); + const auto tooLong = code.Branch({0x0f, 0x87}); + code.Emit({0x4c, 0x89, 0xe7}); + code.Rip({0x48, 0x8d, 0x35}, logName); + code.Emit({0xb9}); + code.U32(units); + code.Emit({0x66, 0xf3, 0xa5}); + code.Rip({0x48, 0x8d, 0x0d}, modulePath); + code.Emit({0xba, 0, 0, 0, 0x40, 0x41, 0xb8, 3, 0, 0, 0, 0x45, 0x31, 0xc9}); + code.Emit({0x48, 0xc7, 0x44, 0x24, 0x20, 2, 0, 0, 0, 0x48, 0xc7, 0x44, 0x24, 0x28, 0x80, 0, 0, 0, 0x48, 0xc7, 0x44, 0x24, 0x30, 0, 0, 0, 0}); + call("CreateFileW"); + code.Emit({0x66, 0x41, 0xc7, 0x04, 0x24, 0x00, 0x00}); + code.Emit({0x48, 0x83, 0xf8, 0xff}); + const auto failed = code.Branch({0x0f, 0x84}); + code.Rip({0x48, 0x89, 0x05}, logHandle); + code.PatchBranch(opened, code.GetRva()); + code.Emit({0xb9}); + code.U32(standardHandle); + code.Emit({0x48, 0x89, 0xc2}); + call("SetStdHandle"); + const auto done = code.GetRva(); + code.PatchBranch(present, done); + code.PatchBranch(tooLong, done); + code.PatchBranch(failed, done); + }; + code.Emit({0x53, 0x55, 0x56, 0x57, 0x41, 0x54, 0x48, 0x83, 0xec, 0x60}); code.Emit({0x31, 0xc9}); code.Rip({0x48, 0x8d, 0x15}, programPath); @@ -353,6 +398,8 @@ WindowsEntryStub WindowsEntryStubBuilder::Build(const std::uint32_t dataRva, con code.Rip({0x0f, 0x85}, findSeparator); code.Emit({0x49, 0x83, 0xc4, 0x02}); code.Emit({0x66, 0x41, 0xc7, 0x04, 0x24, 0x00, 0x00}); + logMissingHandle(0xfffffff5u); + logMissingHandle(0xfffffff4u); code.Rip({0x48, 0x8d, 0x0d}, modulePath); call("SetCurrentDirectoryW"); requireNonzero(directoryError, 0xc000000du); diff --git a/core/relinker/elfpatcher/src/windows/WindowsImportBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsImportBuilder.cpp index 30cb39ecd..71ce4f2ba 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", "FreeLibrary", "GetCommandLineW", "GetFileAttributesW", "GetLastError", "GetModuleFileNameW", "GetModuleHandleA", "GetModuleHandleW", "GetProcAddress", "GetStdHandle", "GetSystemDirectoryW", "LoadLibraryExA", "LoadLibraryExW", "LocalFree", "MultiByteToWideChar", "RaiseException", "SetCurrentDirectoryW", "VirtualAlloc", "WideCharToMultiByte", "WriteFile", "lstrcatA", "lstrcmpA", "lstrcmpiA", "lstrcpyA", "lstrlenA"}; + const std::vector names = {"CreateFileW", "ExitProcess", "FormatMessageA", "FreeLibrary", "GetCommandLineW", "GetFileAttributesW", "GetLastError", "GetModuleFileNameW", "GetModuleHandleA", "GetModuleHandleW", "GetProcAddress", "GetStdHandle", "GetSystemDirectoryW", "LoadLibraryExA", "LoadLibraryExW", "LocalFree", "MultiByteToWideChar", "RaiseException", "SetCurrentDirectoryW", "SetStdHandle", "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(); diff --git a/core/relinker/elfpatcher/tests/WindowsMsvcrt.def b/core/relinker/elfpatcher/tests/WindowsMsvcrt.def new file mode 100644 index 000000000..c163db8f4 --- /dev/null +++ b/core/relinker/elfpatcher/tests/WindowsMsvcrt.def @@ -0,0 +1,4 @@ +LIBRARY msvcrt.dll +EXPORTS + __iob_func + fputs diff --git a/core/relinker/elfpatcher/tests/WindowsMsvcrtStdioHost.cpp b/core/relinker/elfpatcher/tests/WindowsMsvcrtStdioHost.cpp new file mode 100644 index 000000000..4c370e092 --- /dev/null +++ b/core/relinker/elfpatcher/tests/WindowsMsvcrtStdioHost.cpp @@ -0,0 +1,22 @@ +struct _iobuf { + char* ptr; + int count; + char* base; + int flags; + int file; + int charbuf; + int bufferSize; + char* tempName; +}; + +static_assert(sizeof(_iobuf) == 48); + +extern "C" _iobuf* __cdecl __iob_func(); +extern "C" int __cdecl fputs(const char*, _iobuf*); + +extern "C" int shared() { + auto* streams = __iob_func(); + fputs("Windows DLL stdout probe\n", streams + 1); + fputs("Windows DLL stderr probe\n", streams + 2); + return 11; +} diff --git a/core/relinker/elfpatcher/tests/WindowsStdioHost.cpp b/core/relinker/elfpatcher/tests/WindowsStdioHost.cpp new file mode 100644 index 000000000..72a29373f --- /dev/null +++ b/core/relinker/elfpatcher/tests/WindowsStdioHost.cpp @@ -0,0 +1,7 @@ +#include + +extern "C" int shared() { + std::fputs("Windows DLL stdout probe\n", stdout); + std::fputs("Windows DLL stderr probe\n", stderr); + return 11; +} diff --git a/core/relinker/relinker/tests/test_windows_import_modules.py b/core/relinker/relinker/tests/test_windows_import_modules.py index 2cfcf774a..97b31835f 100644 --- a/core/relinker/relinker/tests/test_windows_import_modules.py +++ b/core/relinker/relinker/tests/test_windows_import_modules.py @@ -77,6 +77,41 @@ def check_native_load_paths(output, libraries): f'{library}: a present optional provider does not continue to LoadLibraryExW' +def imported_symbols_by_dll(path): + pe = path.read_bytes() + header = struct.unpack_from('