From 595b23b46292fbce17489e2b90e8b6e24abb3d8a Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 25 Nov 2026 13:41:40 +0000 Subject: [PATCH] Win16: build without -fno-threadsafe-statics The guard of a function-local static calls __atomic_load_1. The i8086 compiler-rt builtins have it (i8086/atomic.c), so the flag is not necessary. The link error came from a sysroot with older builtins. - Remove the flag from the Win16 build and the Win16 test script. - win16wnd tests a function-local static object. - docs/win16.md says what to do with an older sysroot. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01B82eRRTV4u8c9f3gQUmqvq --- Makefile | 2 +- docs/win16.md | 10 ++++++---- tests/run-win16-tests.sh | 2 +- tests/win16wnd.cpp | 11 ++++++++++- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 87bcac3..096dbb6 100644 --- a/Makefile +++ b/Makefile @@ -94,7 +94,7 @@ SRCS = $(addprefix src/,object.cpp strcore.cpp coll.cpp file.cpp archive.cpp exc viewedit.cpp viewprnt.cpp \ viewprev.cpp) WIN16_FLAGS ?= -mcmodel=large -fexceptions -ALL_CXXFLAGS += $(WIN16_FLAGS) -frtti -fno-threadsafe-statics -ffunction-sections \ +ALL_CXXFLAGS += $(WIN16_FLAGS) -frtti -ffunction-sections \ -Iinclude/win16 -idirafter port/include STDLIB = endif diff --git a/docs/win16.md b/docs/win16.md index ec2aa19..bcb8a55 100644 --- a/docs/win16.md +++ b/docs/win16.md @@ -75,7 +75,7 @@ Use these flags: ```sh clang++ --target=i8086-pc-win16 -mcmodel=large -fexceptions -frtti \ - -fno-threadsafe-statics --sysroot=/path/to/sysroot \ + --sysroot=/path/to/sysroot \ -Ipath/to/mfc/include -Ipath/to/mfc/include/win16 \ -idirafter path/to/mfc/port/include \ app.cpp app.res -Lpath/to/mfc/build/i8086-pc-win16 -lmfc -o app.exe @@ -85,9 +85,11 @@ clang++ --target=i8086-pc-win16 -mcmodel=large -fexceptions -frtti \ headers send the OLE names to the OLE 2 headers of the 16-bit SDK. - `-idirafter port/include` must come last. The SDK has no common control headers. The headers of the portable layer give their types. -- `-fno-threadsafe-statics` is necessary. A function-local static - object otherwise needs `__atomic_load_1`, which the 16-bit libraries - do not have. A task has one thread, so the guard is not necessary. +- The guard of a function-local static object calls `__atomic_load_1`. + The compiler-rt builtins of the sysroot have this function + (`compiler-rt/lib/builtins/i8086/atomic.c`). A sysroot that does not + have it is too old: build its builtins again. A task has one thread, + so `-fno-threadsafe-statics` is safe, and it makes the program smaller. - Compile the resources with `llvm-rc -win16`. A program with `main()` and the core classes only can use `-mquickwin`, diff --git a/tests/run-win16-tests.sh b/tests/run-win16-tests.sh index a7ae537..7b1b104 100755 --- a/tests/run-win16-tests.sh +++ b/tests/run-win16-tests.sh @@ -25,7 +25,7 @@ fi T=$OUT/tests mkdir -p "$T" -CXX="$BIN/clang++ --target=i8086-pc-win16 -mcmodel=large -fexceptions -frtti -fno-threadsafe-statics" +CXX="$BIN/clang++ --target=i8086-pc-win16 -mcmodel=large -fexceptions -frtti" CXX="$CXX --sysroot=$SYSROOT -Iinclude -Iinclude/win16 -idirafter port/include -Itests" LIBS="-L$OUT -lmfc" npass=0 diff --git a/tests/win16wnd.cpp b/tests/win16wnd.cpp index 3a1f32b..6b160d3 100644 --- a/tests/win16wnd.cpp +++ b/tests/win16wnd.cpp @@ -2,7 +2,8 @@ // CFrameWnd with a message map, a menu command, a button notification, // WM_CTLCOLOR from a static control, timers, and a modal CDialog from a // resource with DDX. It prints ALL PASSED when each step worked, because -// Wine gives the exit code 0 whatever a 16-bit program returns. +// Wine gives the exit code 0 whatever a 16-bit program returns. A +// function-local static object tests the guard (__atomic_load_1). #include #include #include "win16wnd.h" @@ -41,6 +42,12 @@ protected: } }; +/// Gives a string that is made on the first call only. +static const CString& StaticName() { + static CString s_strName("static name"); + return s_strName; +} + class CMainFrame : public CFrameWnd { public: BOOL m_bCreated; @@ -126,6 +133,8 @@ protected: CHECK(pApp->GetProfileInt("Test", "Value", 0) == 1234); CHECK(pApp->WriteProfileString("Test", "Name", "win16")); CHECK(pApp->GetProfileString("Test", "Name") == "win16"); + CHECK(StaticName() == "static name"); + CHECK(&StaticName() == &StaticName()); g_bDone = TRUE; DestroyWindow(); break;