@@ -9,17 +9,67 @@ function(optimize_static_target TARGET_NAME)
99
1010 # Platform-specific optimizations
1111 if (UNIX AND NOT APPLE )
12- # Linux-specific optimizations
13- target_link_options (${TARGET_NAME} PRIVATE
14- -static
15- -Wl,--gc-sections # Remove unused sections
16- -Wl,--as-needed # Only link libraries that are actually used
17- -Wl,-O2 # Optimize at link time
18- -Wl,--strip-all # Strip all symbols
19- )
12+ if (MUSL_BUILD)
13+ target_link_options (${TARGET_NAME} PRIVATE
14+ -static
15+ -Wl,--gc-sections
16+ -Wl,--as-needed
17+ -Wl,-O2
18+ -Wl,--strip-all
19+ )
20+ target_link_libraries (${TARGET_NAME} PRIVATE pthread m )
21+ else ()
22+ target_link_options (${TARGET_NAME} PRIVATE
23+ -static-libgcc
24+ -static-libstdc++
25+ -Wl,--as-needed
26+ -Wl,-O2
27+ -Wl,--strip-all
28+ # glibc 2.34+ merged libpthread into libc; libpthread.a is an
29+ # empty stub. libstdc++.a references ALL pthread functions via
30+ # weak aliases (__gthrw_), and glibc defines them as WEAK (W)
31+ # in libc.a. LLD will not pull a weak-defined archive member to
32+ # satisfy a weak-undefined reference, leaving every pthread
33+ # symbol at address 0 and crashing at runtime.
34+ # Fix: add a strong undefined reference (-u) for each symbol.
35+ # This forces LLD to pull in each object from libc.a, making
36+ # the weak-defined symbol available for the weak refs to bind to.
37+ -Wl,-u,pthread_once
38+ -Wl,-u,pthread_getspecific
39+ -Wl,-u,pthread_setspecific
40+ -Wl,-u,pthread_create
41+ -Wl,-u,pthread_join
42+ -Wl,-u,pthread_equal
43+ -Wl,-u,pthread_self
44+ -Wl,-u,pthread_detach
45+ -Wl,-u,pthread_cancel
46+ -Wl,-u,pthread_mutex_lock
47+ -Wl,-u,pthread_mutex_trylock
48+ -Wl,-u,pthread_mutex_unlock
49+ -Wl,-u,pthread_mutex_init
50+ -Wl,-u,pthread_mutex_destroy
51+ -Wl,-u,pthread_cond_init
52+ -Wl,-u,pthread_cond_broadcast
53+ -Wl,-u,pthread_cond_signal
54+ -Wl,-u,pthread_cond_wait
55+ -Wl,-u,pthread_cond_timedwait
56+ -Wl,-u,pthread_cond_destroy
57+ -Wl,-u,pthread_key_create
58+ -Wl,-u,pthread_key_delete
59+ -Wl,-u,pthread_mutexattr_init
60+ -Wl,-u,pthread_mutexattr_settype
61+ -Wl,-u,pthread_mutexattr_destroy
62+ -Wl,-u,pthread_attr_init
63+ -Wl,-u,pthread_attr_destroy
64+ -Wl,-u,pthread_attr_setdetachstate
65+ -Wl,-u,pthread_exit
66+ -Wl,-u,__pthread_key_create
67+ )
68+ target_link_libraries (${TARGET_NAME} PRIVATE pthread m dl )
69+ endif ()
2070 target_compile_options (${TARGET_NAME} PRIVATE
21- -ffunction-sections # Put each function in its own section
22- -fdata-sections # Put each data item in its own section
71+ -ffunction-sections
72+ -fdata-sections
2373 )
2474 elseif (APPLE )
2575 # macOS-specific optimizations
0 commit comments