From 91960900f86ebfe2bf5f48af5363cdef02b5d005 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Tue, 5 May 2015 20:13:39 +0000 Subject: [PATCH] Build ASan runtime library with -z global on Android. llvm-svn: 236537 --- compiler-rt/cmake/Modules/AddCompilerRT.cmake | 4 ++-- compiler-rt/cmake/config-ix.cmake | 12 ++++++++++++ compiler-rt/lib/asan/CMakeLists.txt | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/compiler-rt/cmake/Modules/AddCompilerRT.cmake b/compiler-rt/cmake/Modules/AddCompilerRT.cmake index a7782a194847..a4b4df356de7 100644 --- a/compiler-rt/cmake/Modules/AddCompilerRT.cmake +++ b/compiler-rt/cmake/Modules/AddCompilerRT.cmake @@ -47,13 +47,13 @@ endmacro() # OUTPUT_NAME ) macro(add_compiler_rt_runtime name arch type) if(CAN_TARGET_${arch}) - parse_arguments(LIB "SOURCES;CFLAGS;DEFS;OUTPUT_NAME" "" ${ARGN}) + parse_arguments(LIB "SOURCES;CFLAGS;LINKFLAGS;DEFS;OUTPUT_NAME" "" ${ARGN}) add_library(${name} ${type} ${LIB_SOURCES}) # Setup compile flags and definitions. set_target_compile_flags(${name} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) set_target_link_flags(${name} - ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) + ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS} ${LIB_LINKFLAGS}) set_property(TARGET ${name} APPEND PROPERTY COMPILE_DEFINITIONS ${LIB_DEFS}) # Setup correct output directory in the build tree. diff --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake index 5574dc2a3e9b..e62715ea6f8f 100644 --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -4,6 +4,13 @@ include(CheckLibraryExists) include(CheckSymbolExists) include(TestBigEndian) +function(check_linker_flag flag out_var) + cmake_push_check_state() + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${flag}") + check_cxx_compiler_flag("" ${out_var}) + cmake_pop_check_state() +endfunction() + # CodeGen options. check_cxx_compiler_flag(-fPIC COMPILER_RT_HAS_FPIC_FLAG) check_cxx_compiler_flag(-fPIE COMPILER_RT_HAS_FPIE_FLAG) @@ -59,6 +66,11 @@ check_library_exists(m pow "" COMPILER_RT_HAS_LIBM) check_library_exists(pthread pthread_create "" COMPILER_RT_HAS_LIBPTHREAD) check_library_exists(stdc++ __cxa_throw "" COMPILER_RT_HAS_LIBSTDCXX) +# Linker flags. +if(ANDROID) + check_linker_flag("-Wl,-z,global" COMPILER_RT_HAS_Z_GLOBAL) +endif() + # Architectures. # List of all architectures we can target. diff --git a/compiler-rt/lib/asan/CMakeLists.txt b/compiler-rt/lib/asan/CMakeLists.txt index e09c94d8d2d7..d9bddb3c67d9 100644 --- a/compiler-rt/lib/asan/CMakeLists.txt +++ b/compiler-rt/lib/asan/CMakeLists.txt @@ -37,9 +37,22 @@ append_no_rtti_flag(ASAN_CFLAGS) set(ASAN_COMMON_DEFINITIONS ASAN_HAS_EXCEPTIONS=1) +set(ASAN_DYNAMIC_LINK_FLAGS) + if(ANDROID) list(APPEND ASAN_COMMON_DEFINITIONS ASAN_LOW_MEMORY=1) +# On Android, -z global does not do what it is documented to do. +# On Android, -z global moves the library ahead in the lookup order, +# placing it right after the LD_PRELOADs. This is used to compensate for the fact +# that Android linker does not look at the dependencies of the main executable +# that aren't dependencies of the current DSO when resolving symbols from said DSO. +# As a net result, this allows running ASan executables without LD_PRELOAD-ing the +# ASan runtime library. +# The above is applicable to L MR1 or newer. + if (COMPILER_RT_HAS_Z_GLOBAL) + list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global) + endif() endif() set(ASAN_DYNAMIC_DEFINITIONS @@ -143,6 +156,7 @@ else() $ ${ASAN_COMMON_RUNTIME_OBJECTS} CFLAGS ${ASAN_DYNAMIC_CFLAGS} + LINKFLAGS ${ASAN_DYNAMIC_LINK_FLAGS} DEFS ${ASAN_DYNAMIC_DEFINITIONS}) target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS}) add_dependencies(asan clang_rt.asan-dynamic-${arch})