@@ -9,30 +9,56 @@ function(optimize_static_target TARGET_NAME)
99
1010 # Platform-specific optimizations
1111 if (UNIX AND NOT APPLE )
12- # Linux-specific optimizations
1312 target_link_options (${TARGET_NAME} PRIVATE
14- -static
15- -pthread
16- # Clang + static glibc: libstdc++'s gthr-posix.h uses __weakref__ aliases
17- # for pthread functions. GCC resolves these at link time, but Clang leaves
18- # them as null, causing segfaults when std::thread calls through them.
19- # Force the linker to pull in the real symbols from libc.a.
13+ -static-libgcc
14+ -static-libstdc++
15+ -Wl,--as-needed
16+ -Wl,-O2
17+ -Wl,--strip-all
18+ # glibc 2.34+ merged libpthread into libc; libpthread.a is an
19+ # empty stub. libstdc++.a references ALL pthread functions via
20+ # weak aliases (__gthrw_), and glibc defines them as WEAK (W)
21+ # in libc.a. LLD will not pull a weak-defined archive member to
22+ # satisfy a weak-undefined reference, leaving every pthread
23+ # symbol at address 0 and crashing at runtime.
24+ # Fix: add a strong undefined reference (-u) for each symbol.
25+ # This forces LLD to pull in each object from libc.a, making
26+ # the weak-defined symbol available for the weak refs to bind to.
27+ -Wl,-u,pthread_once
28+ -Wl,-u,pthread_getspecific
29+ -Wl,-u,pthread_setspecific
2030 -Wl,-u,pthread_create
2131 -Wl,-u,pthread_join
22- -Wl,-u,pthread_cancel
32+ -Wl,-u,pthread_equal
33+ -Wl,-u,pthread_self
2334 -Wl,-u,pthread_detach
35+ -Wl,-u,pthread_cancel
2436 -Wl,-u,pthread_mutex_lock
37+ -Wl,-u,pthread_mutex_trylock
2538 -Wl,-u,pthread_mutex_unlock
26- -Wl,-u,pthread_once
39+ -Wl,-u,pthread_mutex_init
40+ -Wl,-u,pthread_mutex_destroy
41+ -Wl,-u,pthread_cond_init
42+ -Wl,-u,pthread_cond_broadcast
43+ -Wl,-u,pthread_cond_signal
44+ -Wl,-u,pthread_cond_wait
45+ -Wl,-u,pthread_cond_timedwait
46+ -Wl,-u,pthread_cond_destroy
2747 -Wl,-u,pthread_key_create
28- -Wl,--gc-sections # Remove unused sections
29- -Wl,--as-needed # Only link libraries that are actually used
30- -Wl,-O2 # Optimize at link time
31- -Wl,--strip-all # Strip all symbols
48+ -Wl,-u,pthread_key_delete
49+ -Wl,-u,pthread_mutexattr_init
50+ -Wl,-u,pthread_mutexattr_settype
51+ -Wl,-u,pthread_mutexattr_destroy
52+ -Wl,-u,pthread_attr_init
53+ -Wl,-u,pthread_attr_destroy
54+ -Wl,-u,pthread_attr_setdetachstate
55+ -Wl,-u,pthread_exit
56+ -Wl,-u,__pthread_key_create
3257 )
58+ target_link_libraries (${TARGET_NAME} PRIVATE pthread m dl )
3359 target_compile_options (${TARGET_NAME} PRIVATE
34- -ffunction-sections # Put each function in its own section
35- -fdata-sections # Put each data item in its own section
60+ -ffunction-sections
61+ -fdata-sections
3662 )
3763 elseif (APPLE )
3864 # macOS-specific optimizations
0 commit comments