From 90e1d969acfc5d6c3e8b2261e11e1acd0c2fb596 Mon Sep 17 00:00:00 2001 From: iphydf Date: Sun, 17 Jun 2018 23:56:58 +0000 Subject: [PATCH] Fix linking with address sanitizer. `-fsanitize=address` also needs to be passed to the linker. --- CMakeLists.txt | 16 ++++++---------- cmake/AddCompilerFlag.cmake | 1 + 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 411d75d6..a12631c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -191,18 +191,14 @@ endif() option(ASAN "Enable address-sanitizer to detect invalid memory accesses" OFF) if(ASAN) - set(SAFE_CMAKE_REQUIRED_LIBRARIES "${CMAKE_REQUIRED_LIBRARIES}") - set(CMAKE_REQUIRED_LIBRARIES "-fsanitize=address") add_cflag("-fsanitize=address") - set(CMAKE_REQUIRED_LIBRARIES "${SAFE_CMAKE_REQUIRED_LIBRARIES}") + add_dllflag("-fsanitize=address") +endif() + +if(LINUX) + add_dllflag("-Wl,-z,defs") else() - # Forbid undefined symbols in shared libraries. This is incompatible with - # asan, so it's in the else branch here. - if(LINUX) - add_dllflag("-Wl,-z,defs") - else() - add_dllflag("-undefined error") - endif() + add_dllflag("-undefined error") endif() option(USE_IPV6 "Use IPv6 in tests" ON) diff --git a/cmake/AddCompilerFlag.cmake b/cmake/AddCompilerFlag.cmake index 78102dcc..6a4a3f56 100644 --- a/cmake/AddCompilerFlag.cmake +++ b/cmake/AddCompilerFlag.cmake @@ -37,6 +37,7 @@ function(add_dllflag flag) check_c_compiler_flag("${flag}" HAVE_LD${var} QUIET) if(HAVE_LD${var}) set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${flag}" PARENT_SCOPE) + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}" PARENT_SCOPE) endif() endfunction()