Index: cmake/Modules/SanitizerUtils.cmake =================================================================== --- cmake/Modules/SanitizerUtils.cmake +++ cmake/Modules/SanitizerUtils.cmake @@ -46,6 +46,15 @@ endforeach() endmacro() +function(add_weak_symbols libname linkflags) + file(STRINGS "${COMPILER_RT_SOURCE_DIR}/lib/${libname}/weak_symbols.txt" WEAK_SYMBOLS) + set(local_linkflags ${${linkflags}}) + foreach(SYMBOL ${WEAK_SYMBOLS}) + set(local_linkflags ${local_linkflags} -Wl,-U,${SYMBOL}) + endforeach() + set(${linkflags} ${local_linkflags} PARENT_SCOPE) +endfunction() + macro(add_sanitizer_rt_version_list name) set(vers ${CMAKE_CURRENT_BINARY_DIR}/${name}.vers) cmake_parse_arguments(ARG "" "" "LIBS;EXTRA" ${ARGN}) Index: lib/asan/CMakeLists.txt =================================================================== --- lib/asan/CMakeLists.txt +++ lib/asan/CMakeLists.txt @@ -106,6 +106,10 @@ add_compiler_rt_component(asan) if(APPLE) + add_weak_symbols("asan" WEAK_SYMBOL_LINKFLAGS) + add_weak_symbols("ubsan" WEAK_SYMBOL_LINKFLAGS) + add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINKFLAGS) + add_compiler_rt_runtime(clang_rt.asan SHARED OS ${SANITIZER_COMMON_SUPPORTED_OS} @@ -117,6 +121,7 @@ RTLSanCommon RTUbsan CFLAGS ${ASAN_DYNAMIC_CFLAGS} + LINKFLAGS ${WEAK_SYMBOL_LINKFLAGS} DEFS ${ASAN_DYNAMIC_DEFINITIONS} PARENT_TARGET asan) else() Index: lib/asan/tests/CMakeLists.txt =================================================================== --- lib/asan/tests/CMakeLists.txt +++ lib/asan/tests/CMakeLists.txt @@ -59,6 +59,11 @@ if(APPLE) list(APPEND ASAN_UNITTEST_COMMON_CFLAGS ${DARWIN_osx_CFLAGS}) list(APPEND ASAN_UNITTEST_COMMON_LINKFLAGS ${DARWIN_osx_LINKFLAGS}) + + add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS) + add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS) + add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS) + list(APPEND ASAN_UNITTEST_COMMON_LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}) endif() if(MSVC) Index: lib/asan/weak_symbols.txt =================================================================== --- /dev/null +++ lib/asan/weak_symbols.txt @@ -0,0 +1,3 @@ +___asan_default_options +___asan_default_suppressions +___asan_on_error Index: lib/sanitizer_common/sanitizer_internal_defs.h =================================================================== --- lib/sanitizer_common/sanitizer_internal_defs.h +++ lib/sanitizer_common/sanitizer_internal_defs.h @@ -32,7 +32,7 @@ # define SANITIZER_WEAK_ATTRIBUTE __attribute__((weak)) #endif -#if (SANITIZER_LINUX || SANITIZER_WINDOWS) && !SANITIZER_GO +#if (SANITIZER_LINUX || SANITIZER_MAC || SANITIZER_WINDOWS) && !SANITIZER_GO # define SANITIZER_SUPPORTS_WEAK_HOOKS 1 #else # define SANITIZER_SUPPORTS_WEAK_HOOKS 0 Index: lib/sanitizer_common/tests/CMakeLists.txt =================================================================== --- lib/sanitizer_common/tests/CMakeLists.txt +++ lib/sanitizer_common/tests/CMakeLists.txt @@ -81,6 +81,9 @@ if(APPLE) list(APPEND SANITIZER_TEST_CFLAGS_COMMON ${DARWIN_osx_CFLAGS}) list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON ${DARWIN_osx_LINKFLAGS}) + + add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS) + list(APPEND SANITIZER_TEST_LINK_FLAGS_COMMON ${WEAK_SYMBOL_LINK_FLAGS}) endif() # MSVC linker is allocating 1M for the stack by default, which is not Index: lib/sanitizer_common/weak_symbols.txt =================================================================== --- /dev/null +++ lib/sanitizer_common/weak_symbols.txt @@ -0,0 +1,6 @@ +___sanitizer_free_hook +___sanitizer_malloc_hook +___sanitizer_symbolize_code +___sanitizer_symbolize_data +___sanitizer_symbolize_demangle +___sanitizer_symbolize_flush Index: lib/stats/CMakeLists.txt =================================================================== --- lib/stats/CMakeLists.txt +++ lib/stats/CMakeLists.txt @@ -5,8 +5,14 @@ if(APPLE) set(STATS_LIB_FLAVOR SHARED) + + add_weak_symbols("asan" WEAK_SYMBOL_LINKFLAGS) + add_weak_symbols("ubsan" WEAK_SYMBOL_LINKFLAGS) + add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINKFLAGS) else() set(STATS_LIB_FLAVOR STATIC) + + set(WEAK_SYMBOL_LINKFLAGS) endif() add_compiler_rt_runtime(clang_rt.stats @@ -17,6 +23,7 @@ OBJECT_LIBS RTSanitizerCommon RTSanitizerCommonLibc CFLAGS ${SANITIZER_COMMON_CFLAGS} + LINKFLAGS ${WEAK_SYMBOL_LINKFLAGS} PARENT_TARGET stats) add_compiler_rt_runtime(clang_rt.stats_client @@ -25,4 +32,5 @@ OS ${SANITIZER_COMMON_SUPPORTED_OS} SOURCES stats_client.cc CFLAGS ${SANITIZER_COMMON_CFLAGS} + LINKFLAGS ${WEAK_SYMBOL_LINKFLAGS} PARENT_TARGET stats) Index: lib/tsan/CMakeLists.txt =================================================================== --- lib/tsan/CMakeLists.txt +++ lib/tsan/CMakeLists.txt @@ -107,6 +107,10 @@ # Pass ASM file directly to the C++ compiler. set_source_files_properties(${TSAN_ASM_SOURCES} PROPERTIES LANGUAGE C) endif() + + add_weak_symbols("ubsan" WEAK_SYMBOL_LINKFLAGS) + add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINKFLAGS) + add_compiler_rt_runtime(clang_rt.tsan SHARED OS ${TSAN_SUPPORTED_OS} @@ -117,6 +121,7 @@ RTSanitizerCommonLibc RTUbsan CFLAGS ${TSAN_RTL_CFLAGS} + LINKFLAGS ${WEAK_SYMBOL_LINKFLAGS} PARENT_TARGET tsan) add_compiler_rt_object_libraries(RTTsan_dynamic OS ${TSAN_SUPPORTED_OS} Index: lib/tsan/tests/CMakeLists.txt =================================================================== --- lib/tsan/tests/CMakeLists.txt +++ lib/tsan/tests/CMakeLists.txt @@ -76,14 +76,18 @@ ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}) list(APPEND TEST_OBJECTS lib${TSAN_TEST_RUNTIME}.a) list(APPEND TEST_DEPS ${TSAN_TEST_RUNTIME}) + + add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS) + add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS) + # Intentionally do *not* link with `-fsanitize=thread`. We already link # against a static version of the runtime, and we don't want the dynamic # one. add_compiler_rt_test(TsanUnitTests "${testname}-${arch}-Test" OBJECTS ${TEST_OBJECTS} DEPS ${TEST_DEPS} - LINK_FLAGS ${TARGET_LINK_FLAGS} ${DARWIN_osx_LINKFLAGS} - -lc++) + LINK_FLAGS ${TARGET_LINK_FLAGS} ${DARWIN_osx_LINK_FLAGS} + ${WEAK_SYMBOL_LINK_FLAGS} -lc++) endif() endforeach() endif() Index: lib/ubsan/CMakeLists.txt =================================================================== --- lib/ubsan/CMakeLists.txt +++ lib/ubsan/CMakeLists.txt @@ -56,6 +56,9 @@ SOURCES ${UBSAN_STANDALONE_SOURCES} CFLAGS ${UBSAN_STANDALONE_CFLAGS}) + add_weak_symbols("ubsan" WEAK_SYMBOL_LINKFLAGS) + add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINKFLAGS) + add_compiler_rt_runtime(clang_rt.ubsan SHARED OS ${SANITIZER_COMMON_SUPPORTED_OS} @@ -64,6 +67,7 @@ RTUbsan_standalone RTSanitizerCommon RTSanitizerCommonLibc + LINKFLAGS ${WEAK_SYMBOL_LINKFLAGS} PARENT_TARGET ubsan) endif() Index: lib/ubsan/weak_symbols.txt =================================================================== --- /dev/null +++ lib/ubsan/weak_symbols.txt @@ -0,0 +1 @@ +___ubsan_default_options