Skip to content

Commit

Permalink
[compiler-rt][CMake] Set --unwindlib=none when using LLVM libunwind
Browse files Browse the repository at this point in the history
We already link libunwind explicitly so avoid trying to link toolchain's
default libunwind which may be missing. This matches what we already do
for libcxx and libcxxabi.

Differential Revision: https://reviews.llvm.org/D129472
  • Loading branch information
petrhosek committed Jul 15, 2022
1 parent 5da5483 commit 6699f55
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 9 deletions.
15 changes: 13 additions & 2 deletions compiler-rt/CMakeLists.txt
Expand Up @@ -474,6 +474,8 @@ append_list_if(COMPILER_RT_HAS_WD4800_FLAG /wd4800 SANITIZER_COMMON_CFLAGS)
append_list_if(MINGW -fms-extensions SANITIZER_COMMON_CFLAGS)

# Set common link flags.
# TODO: We should consider using the same model as libc++, that is use either
# -nostdlib++ and --unwindlib=none if supported, or -nodefaultlibs otherwise.
append_list_if(C_SUPPORTS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMON_LINK_FLAGS)
append_list_if(COMPILER_RT_HAS_Z_TEXT -Wl,-z,text SANITIZER_COMMON_LINK_FLAGS)

Expand Down Expand Up @@ -533,10 +535,14 @@ string(REPLACE " " ";" COMPILER_RT_UNITTEST_CFLAGS "${COMPILER_RT_TEST_COMPILER_
set(COMPILER_RT_UNITTEST_LINK_FLAGS ${COMPILER_RT_UNITTEST_CFLAGS})

if(COMPILER_RT_USE_LLVM_UNWINDER)
# We're linking directly against the libunwind that we're building so don't
# try to link in the toolchain's default libunwind which may be missing.
append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none COMPILER_RT_COMMON_LINK_FLAGS)
append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none COMPILER_RT_UNITTEST_LINK_FLAGS)
if (COMPILER_RT_ENABLE_STATIC_UNWINDER)
list(APPEND SANITIZER_CXX_ABI_LIBRARIES "$<TARGET_LINKER_FILE:unwind_static>")
list(APPEND COMPILER_RT_UNWINDER_LINK_LIBS "$<TARGET_LINKER_FILE:unwind_static>")
else()
list(APPEND SANITIZER_CXX_ABI_LIBRARIES "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:unwind_shared>,unwind_shared,unwind_static>>")
list(APPEND COMPILER_RT_UNWINDER_LINK_LIBS "$<TARGET_LINKER_FILE:$<IF:$<TARGET_EXISTS:unwind_shared>,unwind_shared,unwind_static>>")
endif()
endif()

Expand All @@ -561,6 +567,11 @@ endif()

macro(append_libcxx_libs var)
if (${var}_INTREE)
# TODO: We don't need to add --unwindlib=none to SANITIZER_COMMON_LINK_FLAGS
# because we added -nodefaultlibs there earlier, and adding would result in
# a warning, but if we switch to -nostdlib++, we would need to add it here.
# append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none SANITIZER_COMMON_LINK_FLAGS)

if (SANITIZER_USE_STATIC_LLVM_UNWINDER AND (TARGET unwind_static OR HAVE_LIBUNWIND))
list(APPEND ${var}_LIBRARIES unwind_static)
elseif (TARGET unwind_shared OR HAVE_LIBUNWIND)
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/asan/tests/CMakeLists.txt
Expand Up @@ -38,6 +38,7 @@ append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros ASAN_U
# during cross compilation
set(ASAN_UNITTEST_COMMON_LINK_FLAGS
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})

# -gline-tables-only must be enough for ASan, so use it if possible.
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/gwp_asan/tests/CMakeLists.txt
Expand Up @@ -36,6 +36,7 @@ set_target_properties(GwpAsanUnitTests PROPERTIES FOLDER "Compiler-RT Tests")

set(GWP_ASAN_UNITTEST_LINK_FLAGS
${COMPILER_RT_UNITTEST_LINK_FLAGS} -ldl
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})
list(APPEND GWP_ASAN_UNITTEST_LINK_FLAGS --driver-mode=g++)
if(NOT WIN32)
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/interception/tests/CMakeLists.txt
Expand Up @@ -23,6 +23,7 @@ set(INTERCEPTION_TEST_CFLAGS_COMMON

set(INTERCEPTION_TEST_LINK_FLAGS_COMMON
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})

# -gline-tables-only must be enough for these tests, so use it if possible.
Expand Down
4 changes: 3 additions & 1 deletion compiler-rt/lib/memprof/tests/CMakeLists.txt
Expand Up @@ -40,7 +40,9 @@ if(NOT WIN32)
list(APPEND MEMPROF_UNITTEST_LINK_FLAGS -pthread)
endif()

set(MEMPROF_UNITTEST_LINK_LIBRARIES ${SANITIZER_TEST_CXX_LIBRARIES})
set(MEMPROF_UNITTEST_LINK_LIBRARIES
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})
list(APPEND MEMPROF_UNITTEST_LINK_LIBRARIES "dl")

if(COMPILER_RT_DEFAULT_TARGET_ARCH IN_LIST MEMPROF_SUPPORTED_ARCH)
Expand Down
4 changes: 3 additions & 1 deletion compiler-rt/lib/orc/CMakeLists.txt
Expand Up @@ -53,7 +53,9 @@ set(ORC_CFLAGS
${COMPILER_RT_COMMON_CFLAGS}
${COMPILER_RT_CXX_CFLAGS})
set(ORC_LINK_FLAGS ${COMPILER_RT_COMMON_LINK_FLAGS})
set(ORC_LINK_LIBS ${COMPILER_RT_CXX_LINK_LIBS})
set(ORC_LINK_LIBS
${COMPILER_RT_UNWINDER_LINK_LIBS}
${COMPILER_RT_CXX_LINK_LIBS})

# Allow the ORC runtime to reference LLVM headers.
foreach (DIR ${LLVM_INCLUDE_DIR} ${LLVM_MAIN_INCLUDE_DIR})
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/orc/unittests/CMakeLists.txt
Expand Up @@ -37,8 +37,8 @@ set(ORC_TEST_ARCH ${ORC_SUPPORTED_ARCH})
set(ORC_UNITTEST_LINK_FLAGS
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${CMAKE_THREAD_LIBS_INIT}
${COMPILER_RT_CXX_LINK_LIBS}
)
${COMPILER_RT_UNWINDER_LINK_LIBS}
${COMPILER_RT_CXX_LINK_LIBS})

if(APPLE)
darwin_filter_host_archs(ORC_SUPPORTED_ARCH ORC_TEST_ARCH)
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/sanitizer_common/tests/CMakeLists.txt
Expand Up @@ -74,6 +74,7 @@ set(SANITIZER_TEST_CFLAGS_COMMON

set(SANITIZER_TEST_LINK_FLAGS_COMMON
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})

# -gline-tables-only must be enough for these tests, so use it if possible.
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt
Expand Up @@ -36,6 +36,7 @@ set(SCUDO_TEST_ARCH ${SCUDO_STANDALONE_SUPPORTED_ARCH})
# gtests requires c++
set(SCUDO_UNITTEST_LINK_FLAGS
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})
list(APPEND SCUDO_UNITTEST_LINK_FLAGS -pthread -no-pie)
# Linking against libatomic is required with some compilers
Expand Down
1 change: 1 addition & 0 deletions compiler-rt/lib/tsan/tests/CMakeLists.txt
Expand Up @@ -27,6 +27,7 @@ set(TSAN_TEST_ARCH ${TSAN_SUPPORTED_ARCH})

set(TSAN_UNITTEST_LINK_FLAGS
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})

if(APPLE)
Expand Down
4 changes: 3 additions & 1 deletion compiler-rt/lib/xray/CMakeLists.txt
Expand Up @@ -150,7 +150,9 @@ append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format XRAY_CFLAGS)
append_rtti_flag(OFF XRAY_CFLAGS)

set(XRAY_LINK_FLAGS ${COMPILER_RT_COMMON_LINK_FLAGS})
set(XRAY_LINK_LIBS ${COMPILER_RT_CXX_LINK_LIBS})
set(XRAY_LINK_LIBS
${COMPILER_RT_UNWINDER_LINK_LIBS}
${COMPILER_RT_CXX_LINK_LIBS})

append_list_if(
COMPILER_RT_HAS_XRAY_COMPILER_FLAG XRAY_SUPPORTED=1 XRAY_COMMON_DEFINITIONS)
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/xray/tests/CMakeLists.txt
Expand Up @@ -50,8 +50,8 @@ set(XRAY_TEST_ARCH ${XRAY_SUPPORTED_ARCH})
set(XRAY_UNITTEST_LINK_FLAGS
${COMPILER_RT_UNITTEST_LINK_FLAGS}
${CMAKE_THREAD_LIBS_INIT}
${COMPILER_RT_CXX_LINK_LIBS}
)
${COMPILER_RT_UNWINDER_LINK_LIBS}
${COMPILER_RT_CXX_LINK_LIBS})

if (NOT APPLE)
# Needed by LLVMSupport.
Expand Down

0 comments on commit 6699f55

Please sign in to comment.