Skip to content

Commit

Permalink
[runtimes] Don't link against compiler-rt explicitly when we use -nos…
Browse files Browse the repository at this point in the history
…tdlib++ (#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
  • Loading branch information
ldionne committed Dec 13, 2023
1 parent 2fd7657 commit a4336f2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
18 changes: 10 additions & 8 deletions libcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -677,15 +677,17 @@ function(cxx_link_system_libraries target)
target_add_link_flags_if_supported(${target} PRIVATE "--unwindlib=none")
endif()

if (LIBCXX_USE_COMPILER_RT)
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
if (LIBCXX_BUILTINS_LIBRARY)
target_link_libraries(${target} PRIVATE "${LIBCXX_BUILTINS_LIBRARY}")
if (MSVC)
if (LIBCXX_USE_COMPILER_RT)
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
if (LIBCXX_BUILTINS_LIBRARY)
target_link_libraries(${target} PRIVATE "${LIBCXX_BUILTINS_LIBRARY}")
endif()
elseif (LIBCXX_HAS_GCC_LIB)
target_link_libraries(${target} PRIVATE gcc)
elseif (LIBCXX_HAS_GCC_S_LIB)
target_link_libraries(${target} PRIVATE gcc_s)
endif()
elseif (LIBCXX_HAS_GCC_LIB)
target_link_libraries(${target} PRIVATE gcc)
elseif (LIBCXX_HAS_GCC_S_LIB)
target_link_libraries(${target} PRIVATE gcc_s)
endif()

if (LIBCXX_HAS_ATOMIC_LIB)
Expand Down
7 changes: 6 additions & 1 deletion libcxx/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ else()
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
# Only link against compiler-rt manually if we use -nodefaultlibs, since
# otherwise the compiler will do the right thing on its own.
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (LIBCXX_USE_COMPILER_RT)
include(HandleCompilerRT)
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
Expand Down Expand Up @@ -73,6 +75,9 @@ if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
moldname mingwex msvcrt)
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
endif ()
Expand Down
7 changes: 6 additions & 1 deletion libcxxabi/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ else()
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
# Only link against compiler-rt manually if we use -nodefaultlibs, since
# otherwise the compiler will do the right thing on its own.
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (LIBCXXABI_HAS_C_LIB)
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
endif ()
Expand Down Expand Up @@ -67,6 +69,9 @@ if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
moldname mingwex msvcrt)
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
endif ()
Expand Down
5 changes: 4 additions & 1 deletion libcxxabi/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ if (LIBCXXABI_USE_LLVM_UNWINDER)
target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared)
endif()
endif()
target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_BUILTINS_LIBRARY} ${LIBCXXABI_LIBRARIES})
target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
endif()
target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)
set_target_properties(cxxabi_shared_objects
PROPERTIES
Expand Down
7 changes: 6 additions & 1 deletion libunwind/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ else()
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
# Only link against compiler-rt manually if we use -nodefaultlibs, since
# otherwise the compiler will do the right thing on its own.
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (LIBUNWIND_HAS_C_LIB)
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
endif ()
Expand Down Expand Up @@ -78,6 +80,9 @@ if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
moldname mingwex msvcrt)
list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
endif()
endif()

if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
endif ()
Expand Down

0 comments on commit a4336f2

Please sign in to comment.