Skip to content

Commit c72c09e

Browse files
committed
link libssp via TBB_COMMON_LINK_LIBS rather than the shared linker flags
TBB compiles with -fstack-protector-strong and _FORTIFY_SOURCE=2, and on mingw the helpers those need (__stack_chk_fail, __stack_chk_guard, __strncpy_chk, ...) live in libssp rather than libgcc. Rtools42 (gcc 10) does not pull it in implicitly, so the link fails with undefined references to them. Passing -lssp through CMAKE_SHARED_LINKER_FLAGS was not enough: those flags are emitted before the objects, and GNU ld only resolves symbols that are already undefined when it reaches a library, so it was discarded. tbb and tbbmalloc happened to survive that because tbb_handle_ipo gives them LTO; tbbmalloc_proxy has no such call and failed to link. Putting ssp in TBB_COMMON_LINK_LIBS routes it through target_link_libraries instead, which lands after the objects, and covers all three targets.
1 parent 89b8c7c commit c72c09e

5 files changed

Lines changed: 58 additions & 13 deletions

File tree

patches/mingw_clang_cmake.diff

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--- a/src/tbb/cmake/compilers/Clang.cmake
22
+++ b/src/tbb/cmake/compilers/Clang.cmake
3-
@@ -65,8 +65,12 @@ endif()
3+
@@ -65,13 +65,27 @@
44
# Clang flags to prevent compiler from optimizing out security checks
55
set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} -Wformat -Wformat-security -Werror=format-security -fPIC $<$<NOT:$<BOOL:${EMSCRIPTEN}>>:-fstack-protector-strong>)
66

@@ -15,3 +15,18 @@
1515
set(TBB_LIB_LINK_FLAGS ${TBB_LIB_LINK_FLAGS} -Wl,-z,relro,-z,now)
1616
endif()
1717

18+
set(TBB_COMMON_LINK_LIBS ${CMAKE_DL_LIBS})
19+
20+
+# Local addition: on mingw the helpers for -fstack-protector-strong and
21+
+# _FORTIFY_SOURCE (__stack_chk_fail, __stack_chk_guard, __strncpy_chk, ...)
22+
+# live in libssp rather than libgcc, and older toolchains (Rtools42 / gcc 10)
23+
+# do not pull it in implicitly. It has to go here, with the link libraries,
24+
+# rather than in the linker flags: those are emitted before the objects, and
25+
+# GNU ld only resolves symbols already undefined when it reaches a library.
26+
+if (MINGW)
27+
+ set(TBB_COMMON_LINK_LIBS ${TBB_COMMON_LINK_LIBS} ssp)
28+
+endif()
29+
+
30+
if (NOT CMAKE_CXX_FLAGS MATCHES "_FORTIFY_SOURCE")
31+
set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} $<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=2>)
32+
endif ()

patches/mingw_gnu_cmake.diff

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
diff --git a/src/tbb/cmake/compilers/GNU.cmake b/src/tbb/cmake/compilers/GNU.cmake
2-
index cf6d8bdb..8f9f0b59 100644
31
--- a/src/tbb/cmake/compilers/GNU.cmake
42
+++ b/src/tbb/cmake/compilers/GNU.cmake
5-
@@ -47,19 +47,44 @@ endif()
3+
@@ -47,19 +47,44 @@
64
# >=2.31.1). Capturing the output in CMake can be done like below. The version
75
# information is written to either stdout or stderr. To not make any
86
# assumptions, both are captured.
@@ -48,19 +46,37 @@ index cf6d8bdb..8f9f0b59 100644
4846
message(TRACE "Extracted GNU assembler version: major=${_tbb_gnu_asm_major_version} minor=${_tbb_gnu_asm_minor_version}")
4947

5048
math(EXPR _tbb_gnu_asm_version_number "${_tbb_gnu_asm_major_version} * 1000 + ${_tbb_gnu_asm_minor_version}")
51-
@@ -81,7 +106,14 @@ endif()
49+
@@ -73,6 +98,16 @@
50+
51+
set(TBB_COMMON_LINK_LIBS ${CMAKE_DL_LIBS})
52+
53+
+# Local addition: on mingw the helpers for -fstack-protector-strong and
54+
+# _FORTIFY_SOURCE (__stack_chk_fail, __stack_chk_guard, __strncpy_chk, ...)
55+
+# live in libssp rather than libgcc, and older toolchains (Rtools42 / gcc 10)
56+
+# do not pull it in implicitly. It has to go here, with the link libraries,
57+
+# rather than in the linker flags: those are emitted before the objects, and
58+
+# GNU ld only resolves symbols already undefined when it reaches a library.
59+
+if (MINGW)
60+
+ set(TBB_COMMON_LINK_LIBS ${TBB_COMMON_LINK_LIBS} ssp)
61+
+endif()
62+
+
63+
# Ignore -Werror set through add_compile_options() or added to CMAKE_CXX_FLAGS if TBB_STRICT is disabled.
64+
if (NOT TBB_STRICT AND COMMAND tbb_remove_compile_flag)
65+
tbb_remove_compile_flag(-Werror)
66+
@@ -81,8 +116,15 @@
5267
if (NOT ${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
5368
# gcc 6.0 and later have -flifetime-dse option that controls elimination of stores done outside the object lifetime
5469
set(TBB_DSE_FLAG $<$<NOT:$<VERSION_LESS:${CMAKE_CXX_COMPILER_VERSION},6.0>>:-flifetime-dse=1>)
5570
- set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} $<$<NOT:$<VERSION_LESS:${CMAKE_CXX_COMPILER_VERSION},8.0>>:-fstack-clash-protection>)
56-
+
71+
5772
+ # -fstack-clash-protection is broken on mingw's SEH targets: compiling
5873
+ # task_dispatcher.h makes GCC 10 abort with "internal compiler error: in
5974
+ # seh_emit_stackalloc, at config/i386/winnt.c". It is a hardening flag
6075
+ # rather than a correctness one, so simply leave it off there.
6176
+ if (NOT MINGW)
6277
+ set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} $<$<NOT:$<VERSION_LESS:${CMAKE_CXX_COMPILER_VERSION},8.0>>:-fstack-clash-protection>)
6378
+ endif()
64-
79+
+
6580
# Suppress GCC 12.x-13.x warning here that to_wait_node(n)->my_is_in_list might have size 0
6681
set(TBB_COMMON_LINK_FLAGS ${TBB_COMMON_LINK_FLAGS} $<$<AND:$<NOT:$<VERSION_LESS:${CMAKE_CXX_COMPILER_VERSION},12.0>>,$<VERSION_LESS:${CMAKE_CXX_COMPILER_VERSION},14.0>>:-Wno-stringop-overflow>)
82+
endif()

src/install.libs.R

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,6 @@ useBundledTbb <- function() {
315315
if (.Platform$OS.type == "windows") {
316316
cmakeFlags <- c(
317317
"-G", "MSYS Makefiles",
318-
# TBB compiles with -fstack-protector-strong and _FORTIFY_SOURCE, and
319-
# on mingw the helpers those need (__stack_chk_fail, __strncpy_chk and
320-
# friends) live in libssp rather than in libgcc. Newer toolchains pull
321-
# it in implicitly; Rtools42 (gcc 10) does not, and the TBB link then
322-
# fails with undefined references to them
323-
"-DCMAKE_SHARED_LINKER_FLAGS=-lssp",
324318
cmakeFlags
325319
)
326320
}

src/tbb/cmake/compilers/Clang.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ endif()
7676

7777
set(TBB_COMMON_LINK_LIBS ${CMAKE_DL_LIBS})
7878

79+
# Local addition: on mingw the helpers for -fstack-protector-strong and
80+
# _FORTIFY_SOURCE (__stack_chk_fail, __stack_chk_guard, __strncpy_chk, ...)
81+
# live in libssp rather than libgcc, and older toolchains (Rtools42 / gcc 10)
82+
# do not pull it in implicitly. It has to go here, with the link libraries,
83+
# rather than in the linker flags: those are emitted before the objects, and
84+
# GNU ld only resolves symbols already undefined when it reaches a library.
85+
if (MINGW)
86+
set(TBB_COMMON_LINK_LIBS ${TBB_COMMON_LINK_LIBS} ssp)
87+
endif()
88+
7989
if (NOT CMAKE_CXX_FLAGS MATCHES "_FORTIFY_SOURCE")
8090
set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} $<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=2>)
8191
endif ()

src/tbb/cmake/compilers/GNU.cmake

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ endif()
9898

9999
set(TBB_COMMON_LINK_LIBS ${CMAKE_DL_LIBS})
100100

101+
# Local addition: on mingw the helpers for -fstack-protector-strong and
102+
# _FORTIFY_SOURCE (__stack_chk_fail, __stack_chk_guard, __strncpy_chk, ...)
103+
# live in libssp rather than libgcc, and older toolchains (Rtools42 / gcc 10)
104+
# do not pull it in implicitly. It has to go here, with the link libraries,
105+
# rather than in the linker flags: those are emitted before the objects, and
106+
# GNU ld only resolves symbols already undefined when it reaches a library.
107+
if (MINGW)
108+
set(TBB_COMMON_LINK_LIBS ${TBB_COMMON_LINK_LIBS} ssp)
109+
endif()
110+
101111
# Ignore -Werror set through add_compile_options() or added to CMAKE_CXX_FLAGS if TBB_STRICT is disabled.
102112
if (NOT TBB_STRICT AND COMMAND tbb_remove_compile_flag)
103113
tbb_remove_compile_flag(-Werror)

0 commit comments

Comments
 (0)