Skip to content

Commit a4336f2

Browse files
authored
[runtimes] Don't link against compiler-rt explicitly when we use -nostdlib++ (#75089)
When we use the -nostdlib++ flag, we don't need to explicitly link against compiler-rt, since the compiler already links against it by default. This simplifies the flags that we need to use when building with Clang and GCC, and opens the door to further simplifications since most platforms won't need to detect whether libgcc and libgcc_s are supported anymore. Furthermore, on platforms where -nostdlib++ is used, this patch prevents manually linking compiler-rt *before* other system libraries. For example, Apple platforms have several compiler-rt symbols defined in libSystem.dylib. If we manually link against compiler-rt, we end up overriding the default link order preferred by the compiler and potentially using the symbols from the clang-provided libclang_rt.a library instead of the system provided one. Note that we don't touch how libunwind links against compiler-rt when it builds the .so/.a because libunwind currently doesn't use -nodefaultlibs and we want to avoid rocking the boat too much. rdar://119506163
1 parent 2fd7657 commit a4336f2

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

libcxx/CMakeLists.txt

+10-8
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,17 @@ function(cxx_link_system_libraries target)
677677
target_add_link_flags_if_supported(${target} PRIVATE "--unwindlib=none")
678678
endif()
679679

680-
if (LIBCXX_USE_COMPILER_RT)
681-
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
682-
if (LIBCXX_BUILTINS_LIBRARY)
683-
target_link_libraries(${target} PRIVATE "${LIBCXX_BUILTINS_LIBRARY}")
680+
if (MSVC)
681+
if (LIBCXX_USE_COMPILER_RT)
682+
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
683+
if (LIBCXX_BUILTINS_LIBRARY)
684+
target_link_libraries(${target} PRIVATE "${LIBCXX_BUILTINS_LIBRARY}")
685+
endif()
686+
elseif (LIBCXX_HAS_GCC_LIB)
687+
target_link_libraries(${target} PRIVATE gcc)
688+
elseif (LIBCXX_HAS_GCC_S_LIB)
689+
target_link_libraries(${target} PRIVATE gcc_s)
684690
endif()
685-
elseif (LIBCXX_HAS_GCC_LIB)
686-
target_link_libraries(${target} PRIVATE gcc)
687-
elseif (LIBCXX_HAS_GCC_S_LIB)
688-
target_link_libraries(${target} PRIVATE gcc_s)
689691
endif()
690692

691693
if (LIBCXX_HAS_ATOMIC_LIB)

libcxx/cmake/config-ix.cmake

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ else()
4545
endif()
4646
endif()
4747

48-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
48+
# Only link against compiler-rt manually if we use -nodefaultlibs, since
49+
# otherwise the compiler will do the right thing on its own.
50+
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
4951
if (LIBCXX_USE_COMPILER_RT)
5052
include(HandleCompilerRT)
5153
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
@@ -73,6 +75,9 @@ if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
7375
moldname mingwex msvcrt)
7476
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
7577
endif()
78+
endif()
79+
80+
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
7681
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
7782
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
7883
endif ()

libcxxabi/cmake/config-ix.cmake

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ else()
3333
endif()
3434
endif()
3535

36-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
36+
# Only link against compiler-rt manually if we use -nodefaultlibs, since
37+
# otherwise the compiler will do the right thing on its own.
38+
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
3739
if (LIBCXXABI_HAS_C_LIB)
3840
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
3941
endif ()
@@ -67,6 +69,9 @@ if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
6769
moldname mingwex msvcrt)
6870
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
6971
endif()
72+
endif()
73+
74+
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
7075
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
7176
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
7277
endif ()

libcxxabi/src/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,10 @@ if (LIBCXXABI_USE_LLVM_UNWINDER)
166166
target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared)
167167
endif()
168168
endif()
169-
target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_BUILTINS_LIBRARY} ${LIBCXXABI_LIBRARIES})
169+
target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
170+
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
171+
target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
172+
endif()
170173
target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)
171174
set_target_properties(cxxabi_shared_objects
172175
PROPERTIES

libunwind/cmake/config-ix.cmake

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ else()
4545
endif()
4646
endif()
4747

48-
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
48+
# Only link against compiler-rt manually if we use -nodefaultlibs, since
49+
# otherwise the compiler will do the right thing on its own.
50+
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
4951
if (LIBUNWIND_HAS_C_LIB)
5052
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
5153
endif ()
@@ -78,6 +80,9 @@ if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
7880
moldname mingwex msvcrt)
7981
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
8082
endif()
83+
endif()
84+
85+
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
8186
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
8287
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
8388
endif ()

0 commit comments

Comments
 (0)