diff --git a/docs/how/cmake_bgfx/CMakeLists.txt b/docs/how/cmake_bgfx/CMakeLists.txt index 66d75a905..4e90792a9 100644 --- a/docs/how/cmake_bgfx/CMakeLists.txt +++ b/docs/how/cmake_bgfx/CMakeLists.txt @@ -80,10 +80,8 @@ if(TSLANG_MEMORY_MODEL STREQUAL "gc") list(APPEND TSLANG_LINK_LIBS gc) endif() -# legacy_stdio_definitions: printf, sprintf_s and the rest of stdio as functions compiled code can -# call (the UCRT has them only as inline wrappers in its headers) if(WIN32) - list(APPEND TSLANG_LINK_LIBS ntdll legacy_stdio_definitions) + list(APPEND TSLANG_LINK_LIBS ntdll) else() find_library(TSLANG_TINFO_LIB NAMES tinfo) list(APPEND TSLANG_LINK_LIBS LLVMDemangle stdc++ m pthread dl rt) diff --git a/docs/how/cmake_dll/CMakeLists.txt b/docs/how/cmake_dll/CMakeLists.txt index c039456cf..12b1b0525 100644 --- a/docs/how/cmake_dll/CMakeLists.txt +++ b/docs/how/cmake_dll/CMakeLists.txt @@ -64,11 +64,9 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc") list(APPEND TSLANG_LINK_LIBS "gcdll/gc") endif() -# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows; legacy_stdio_definitions -# printf, sprintf_s and the rest of stdio as functions compiled code can call (the UCRT has them only -# as inline wrappers in its headers) +# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows if(WIN32) - list(APPEND TSLANG_LINK_LIBS "ntdll" "legacy_stdio_definitions") + list(APPEND TSLANG_LINK_LIBS "ntdll") else() list(APPEND TSLANG_LINK_LIBS "LLVMDemangle" "stdc++" "m" "pthread" "tinfo" "dl" "rt") endif() diff --git a/docs/how/cmake_tslang/CMakeLists.txt b/docs/how/cmake_tslang/CMakeLists.txt index 7812870d7..6654562c5 100644 --- a/docs/how/cmake_tslang/CMakeLists.txt +++ b/docs/how/cmake_tslang/CMakeLists.txt @@ -82,11 +82,9 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc") endif() endif() -# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows; legacy_stdio_definitions -# printf, sprintf_s and the rest of stdio as functions compiled code can call (the UCRT has them only -# as inline wrappers in its headers) +# ntdll provides RtlGetLastNtStatus (pulled in by LLVMSupport) on Windows if(WIN32) - list(APPEND TSLANG_LINK_LIBS "ntdll" "legacy_stdio_definitions") + list(APPEND TSLANG_LINK_LIBS "ntdll") else() list(APPEND TSLANG_LINK_LIBS "LLVMDemangle" "stdc++" "m" "pthread" "tinfo" "dl" "rt") endif() diff --git a/docs/how/cmake_winapp/CMakeLists.txt b/docs/how/cmake_winapp/CMakeLists.txt index 1f467214f..5dc58e0e9 100644 --- a/docs/how/cmake_winapp/CMakeLists.txt +++ b/docs/how/cmake_winapp/CMakeLists.txt @@ -29,9 +29,7 @@ target_link_directories( ${GCLIBPATH} ) -# legacy_stdio_definitions: printf, sprintf_s and the rest of stdio as functions compiled code can -# call (the UCRT has them only as inline wrappers in its headers) -set (LIBS "gcmt-lib" "legacy_stdio_definitions") +set (LIBS "gcmt-lib") target_link_libraries( ${PROJECT_NAME} diff --git a/tslang/include/TypeScript/VSCodeTemplate/Files.h b/tslang/include/TypeScript/VSCodeTemplate/Files.h index 565b32bcf..48199bbfb 100644 --- a/tslang/include/TypeScript/VSCodeTemplate/Files.h +++ b/tslang/include/TypeScript/VSCodeTemplate/Files.h @@ -352,11 +352,7 @@ if (TSLANG_MEMORY_MODEL STREQUAL "gc") list(APPEND TSLANG_LINK_LIBS "gc") endif() -# printf, sprintf_s and the rest of stdio as functions compiled code can call: the UCRT has them only -# as inline wrappers in its headers, which code tslang compiles never sees. -if(WIN32) - list(APPEND TSLANG_LINK_LIBS "legacy_stdio_definitions") -else() +if(NOT WIN32) list(APPEND TSLANG_LINK_LIBS "stdc++" "m" "pthread" "dl" "rt") endif() diff --git a/tslang/lib/TypeScriptAsyncRuntime/CMakeLists.txt b/tslang/lib/TypeScriptAsyncRuntime/CMakeLists.txt index 1bc05383c..67c076cca 100644 --- a/tslang/lib/TypeScriptAsyncRuntime/CMakeLists.txt +++ b/tslang/lib/TypeScriptAsyncRuntime/CMakeLists.txt @@ -5,6 +5,7 @@ add_mlir_library(TypeScriptAsyncRuntime ProcessHeap.cpp DynamicRuntime.cpp UncaughtException.cpp + StdioDefinitions.cpp EXCLUDE_FROM_LIBMLIR diff --git a/tslang/lib/TypeScriptAsyncRuntime/StdioDefinitions.cpp b/tslang/lib/TypeScriptAsyncRuntime/StdioDefinitions.cpp new file mode 100644 index 000000000..c8f7c432b --- /dev/null +++ b/tslang/lib/TypeScriptAsyncRuntime/StdioDefinitions.cpp @@ -0,0 +1,92 @@ +// printf, sprintf_s, scanf and the rest of stdio, as functions a compiled program can call. +// +// The UCRT has them only as inline functions in its headers, over __stdio_common_vfprintf and the +// like: its libraries define none of them (x64's libucrt has printf alone, x86's nothing). A C or C++ +// caller gets its own copy from the header; code tslang compiles never sees the header, so a +// `@varargs declare function printf` - or the sprintf_s a number's conversion to text calls - linked +// only when something else happened to bring a copy in. Microsoft's answer is +// legacy_stdio_definitions.lib, one more library in every link line. +// +// Instead, this file takes the address of each one, which makes the compiler emit the header's own +// definitions here, as linkable COMDATs (`selectany`), and the runtime every program links already +// carries them. The same kind of COMDAT a C or C++ object holds when it calls one: two copies are one, +// so a program whose other objects have a copy of their own links as it did. The file is its own +// member of the archive: the linker takes it only for a function nothing else defined. +#if defined(_WIN32) + +#include +#include +#include + +#define TSLANG_STDIO(name, type) reinterpret_cast(static_cast(&name)) + +extern "C" const void *const __tslang_stdio_definitions[] = { + // printf + TSLANG_STDIO(printf, int (*)(const char *, ...)), + TSLANG_STDIO(fprintf, int (*)(FILE *, const char *, ...)), + TSLANG_STDIO(sprintf, int (*)(char *, const char *, ...)), + TSLANG_STDIO(snprintf, int (*)(char *, size_t, const char *, ...)), + TSLANG_STDIO(_snprintf, int (*)(char *, size_t, const char *, ...)), + TSLANG_STDIO(_scprintf, int (*)(const char *, ...)), + TSLANG_STDIO(vprintf, int (*)(const char *, va_list)), + TSLANG_STDIO(vfprintf, int (*)(FILE *, const char *, va_list)), + TSLANG_STDIO(vsprintf, int (*)(char *, const char *, va_list)), + TSLANG_STDIO(vsnprintf, int (*)(char *, size_t, const char *, va_list)), + TSLANG_STDIO(_vsnprintf, int (*)(char *, size_t, const char *, va_list)), + TSLANG_STDIO(_vscprintf, int (*)(const char *, va_list)), + TSLANG_STDIO(printf_s, int (*)(const char *, ...)), + TSLANG_STDIO(fprintf_s, int (*)(FILE *, const char *, ...)), + TSLANG_STDIO(sprintf_s, int (*)(char *, size_t, const char *, ...)), + TSLANG_STDIO(_snprintf_s, int (*)(char *, size_t, size_t, const char *, ...)), + TSLANG_STDIO(vprintf_s, int (*)(const char *, va_list)), + TSLANG_STDIO(vfprintf_s, int (*)(FILE *, const char *, va_list)), + TSLANG_STDIO(vsprintf_s, int (*)(char *, size_t, const char *, va_list)), + TSLANG_STDIO(vsnprintf_s, int (*)(char *, size_t, size_t, const char *, va_list)), + TSLANG_STDIO(_vsnprintf_s, int (*)(char *, size_t, size_t, const char *, va_list)), + + // scanf + TSLANG_STDIO(scanf, int (*)(const char *, ...)), + TSLANG_STDIO(fscanf, int (*)(FILE *, const char *, ...)), + TSLANG_STDIO(sscanf, int (*)(const char *, const char *, ...)), + TSLANG_STDIO(vscanf, int (*)(const char *, va_list)), + TSLANG_STDIO(vfscanf, int (*)(FILE *, const char *, va_list)), + TSLANG_STDIO(vsscanf, int (*)(const char *, const char *, va_list)), + TSLANG_STDIO(scanf_s, int (*)(const char *, ...)), + TSLANG_STDIO(fscanf_s, int (*)(FILE *, const char *, ...)), + TSLANG_STDIO(sscanf_s, int (*)(const char *, const char *, ...)), + TSLANG_STDIO(vscanf_s, int (*)(const char *, va_list)), + TSLANG_STDIO(vfscanf_s, int (*)(FILE *, const char *, va_list)), + TSLANG_STDIO(vsscanf_s, int (*)(const char *, const char *, va_list)), + + // wprintf + TSLANG_STDIO(wprintf, int (*)(const wchar_t *, ...)), + TSLANG_STDIO(fwprintf, int (*)(FILE *, const wchar_t *, ...)), + TSLANG_STDIO(swprintf, int (*)(wchar_t *, size_t, const wchar_t *, ...)), + TSLANG_STDIO(_snwprintf, int (*)(wchar_t *, size_t, const wchar_t *, ...)), + TSLANG_STDIO(vwprintf, int (*)(const wchar_t *, va_list)), + TSLANG_STDIO(vfwprintf, int (*)(FILE *, const wchar_t *, va_list)), + TSLANG_STDIO(vswprintf, int (*)(wchar_t *, size_t, const wchar_t *, va_list)), + TSLANG_STDIO(_vsnwprintf, int (*)(wchar_t *, size_t, const wchar_t *, va_list)), + TSLANG_STDIO(wprintf_s, int (*)(const wchar_t *, ...)), + TSLANG_STDIO(fwprintf_s, int (*)(FILE *, const wchar_t *, ...)), + TSLANG_STDIO(swprintf_s, int (*)(wchar_t *, size_t, const wchar_t *, ...)), + TSLANG_STDIO(vwprintf_s, int (*)(const wchar_t *, va_list)), + TSLANG_STDIO(vfwprintf_s, int (*)(FILE *, const wchar_t *, va_list)), + TSLANG_STDIO(vswprintf_s, int (*)(wchar_t *, size_t, const wchar_t *, va_list)), + + // wscanf + TSLANG_STDIO(wscanf, int (*)(const wchar_t *, ...)), + TSLANG_STDIO(fwscanf, int (*)(FILE *, const wchar_t *, ...)), + TSLANG_STDIO(swscanf, int (*)(const wchar_t *, const wchar_t *, ...)), + TSLANG_STDIO(vwscanf, int (*)(const wchar_t *, va_list)), + TSLANG_STDIO(vfwscanf, int (*)(FILE *, const wchar_t *, va_list)), + TSLANG_STDIO(vswscanf, int (*)(const wchar_t *, const wchar_t *, va_list)), + TSLANG_STDIO(wscanf_s, int (*)(const wchar_t *, ...)), + TSLANG_STDIO(fwscanf_s, int (*)(FILE *, const wchar_t *, ...)), + TSLANG_STDIO(swscanf_s, int (*)(const wchar_t *, const wchar_t *, ...)), + TSLANG_STDIO(vwscanf_s, int (*)(const wchar_t *, va_list)), + TSLANG_STDIO(vfwscanf_s, int (*)(FILE *, const wchar_t *, va_list)), + TSLANG_STDIO(vswscanf_s, int (*)(const wchar_t *, const wchar_t *, va_list)), +}; + +#endif diff --git a/tslang/runtime/CMakeLists.txt b/tslang/runtime/CMakeLists.txt index 91714cb6b..0df1c1644 100644 --- a/tslang/runtime/CMakeLists.txt +++ b/tslang/runtime/CMakeLists.txt @@ -26,12 +26,14 @@ set(SRC "${CMAKE_CURRENT_SOURCE_DIR}/../lib/TypeScriptAsyncRuntime") # program using async does not link (GC_enable_threads); ProcessHeap.cpp is empty off Windows. Without # UncaughtException.cpp an x86 program that threw what nothing caught ended in abort()'s message box, # which an unattended run waits on forever (exe.cpp takes the file only when the runtime defines it). +# Without StdioDefinitions.cpp a program calling printf or sprintf_s does not link on Windows. add_library(TypeScriptAsyncRuntime STATIC "${SRC}/AsyncRuntime.cpp" "${SRC}/AsyncGCThreads.cpp" "${SRC}/ProcessHeap.cpp" "${SRC}/DynamicRuntime.cpp" - "${SRC}/UncaughtException.cpp") + "${SRC}/UncaughtException.cpp" + "${SRC}/StdioDefinitions.cpp") target_include_directories(TypeScriptAsyncRuntime PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../include" "${CMAKE_CURRENT_SOURCE_DIR}/../lib" diff --git a/tslang/test/tester/test-runner.cpp b/tslang/test/tester/test-runner.cpp index e2d431788..94fe97ca5 100644 --- a/tslang/test/tester/test-runner.cpp +++ b/tslang/test/tester/test-runner.cpp @@ -13,9 +13,9 @@ #define TYPESCRIPT_LIB "TypeScriptAsyncRuntime.lib " //#define LIBS "msvcrt" _D_ ".lib ucrt" _D_ ".lib " // static CRT (/MT[d]) to match LLVM/TypeScript runtime libs and gc.lib; mixing static+dynamic CRT crashes at startup -// legacy_stdio_definitions: printf and the rest of stdio as functions a program can call, which the -// UCRT has only as inline wrappers - x86's libucrt defines none of them (x64's happens to have printf) -#define LIBS "libcmt" _D_ ".lib libvcruntime" _D_ ".lib libucrt" _D_ ".lib legacy_stdio_definitions.lib " +// printf and the rest of stdio, which the UCRT has only as inline wrappers, come from TYPESCRIPT_LIB +// (TypeScriptAsyncRuntime/StdioDefinitions.cpp) +#define LIBS "libcmt" _D_ ".lib libvcruntime" _D_ ".lib libucrt" _D_ ".lib " #else // for Ubuntu 20.04 add -ldl and optionally -rdynamic #define LIBS "-frtti -fexceptions -lstdc++ -lrt -ldl -lpthread -lm" diff --git a/tslang/tslang/exe.cpp b/tslang/tslang/exe.cpp index 141dde9d4..bb028fbae 100644 --- a/tslang/tslang/exe.cpp +++ b/tslang/tslang/exe.cpp @@ -1095,12 +1095,6 @@ int linkBinary(const char *argv0, const LinkRequest &request, CompileOptions &co args.push_back("-Wl,-nodefaultlib:libcmt"); } - // printf and the rest of stdio as functions a program can call (a `@varargs declare function - // printf`): the UCRT has them only as inline wrappers in its headers. x86's libucrt defines none, and - // a 32-bit program calling printf failed to link (undefined symbol _printf); x64's libucrt happens - // to define printf, which it still takes, being ahead of this. - args.push_back("-llegacy_stdio_definitions"); - // Windows asks for elevation before starting a 32-bit program that has no // requestedExecutionLevel and whose name contains "setup", "install", "update" or "patch" // (installer detection), so `my_setup.exe` or `dispatch.exe` would never start. Link an