Skip to content

Commit fbd41d6

Browse files
authored
Fix as.exe crash on Windows (#20)
Rewrote the GNU `as` wrapper to make it work properly when compiled by MSVC. The main problem was that handling of command line argument stopped working after switching compilers. Before that, the wrapper was built using MinGW which used a POSIX compatibility mode, and that allowed us to use nearly exactly the same code on Unix as on Windows. However, after switching to MSVC, we encountered a problem where command line arguments which had UTF/Unicode characters stopped being processed properly: the strings where cut just before the first non-ASCII character and the application would crash. After spending a good deal of time on this, it turned out the problem was in the code which converted the strings from multibyte (e.g. UTF-8) encoding which we were given before, in MinGW-compiled code, to wide character encodings used by Windows. Interestingly, the problem was present even in code copied from Microsoft documentation for Win32 APIs that were used to do the conversion and to obtain the command line arguments. While looking for the cause, the code got modified to use `std::widestring` on Windows, in hope that this would fix the issue. Unfortunately, that wasn't the case - the conversion from multi-byte to wide character set still failed in the same ways. Eventually, I discovered that MSVC accepts the `/utf-8` argument which causes it to switch both the source file encoding and the execution encoding (encoding used for strings at the application run time) to UTF-8 - apparently what MinGW had done before. This fixed the issue in that the arguments passed to `main` can now be assumed to be `UTF-8` and most of the code on both Windows and Unix can be shared again. The wide string conversion **still** fails, though, and for that reason the current code iteration uses only multi-byte `UTF-8` strings. The infrastructure to handle different types of strings, depending on the platform, is still present, just in case if we try to get wide character strings to work again some day.
1 parent 534baa2 commit fbd41d6

22 files changed

+640
-960
lines changed

src/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ else() # UNIX
144144
/guard:cf
145145
/Zi
146146
/W3
147+
/utf-8
147148
)
148149

149150
set(COMMON_LINKER_ARGS
@@ -213,6 +214,7 @@ add_link_options("$<$<COMPILE_LANGUAGE:CXX>:${XA_UTILS_CXX_LINKER_FLAGS}>")
213214
add_link_options("$<$<COMPILE_LANGUAGE:C>:${XA_UTILS_C_LINKER_FLAGS}>")
214215

215216
if(WIN32)
217+
add_compile_definitions(_UNICODE)
216218
include_directories(compat)
217219
endif()
218220

0 commit comments

Comments
 (0)