[compiler-rt] 9dc5d8d - [sanitizer] Add CMake flag to build with internal symbolizer
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 8 16:57:03 PDT 2023
Author: Vitaly Buka
Date: 2023-09-08T16:56:57-07:00
New Revision: 9dc5d8df16030a63deb2a425c6122985b5a06891
URL: https://github.com/llvm/llvm-project/commit/9dc5d8df16030a63deb2a425c6122985b5a06891
DIFF: https://github.com/llvm/llvm-project/commit/9dc5d8df16030a63deb2a425c6122985b5a06891.diff
LOG: [sanitizer] Add CMake flag to build with internal symbolizer
This intermediate result in moving internal symbolizer build
from sh script to CMake rules.
The flag is supposed to be used with:
-DLLVM_ENABLE_PROJECTS="clang;lld;compiler-rt" -DLLVM_ENABLE_RUNTIMES="libunwind;libcxx;libcxxabi" -Sllvm-project/llvm
After converting sh script into cmake, we may add support for other build modes.
For https://github.com/llvm/llvm-project/issues/30098
Reviewed By: kstoimenov, MaskRay
Differential Revision: https://reviews.llvm.org/D157947
Added:
compiler-rt/lib/sanitizer_common/symbolizer/CMakeLists.txt
Modified:
compiler-rt/CMakeLists.txt
compiler-rt/lib/sanitizer_common/CMakeLists.txt
compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
Removed:
################################################################################
diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index 3888995811d8945..737ba774a172805 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -727,6 +727,9 @@ endif()
pythonize_bool(COMPILER_RT_HAS_LLD)
pythonize_bool(COMPILER_RT_TEST_USE_LLD)
+option(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER "Build Compiler RT linked with in LLVM symbolizer" OFF)
+mark_as_advanced(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER)
+
add_subdirectory(lib)
if(COMPILER_RT_INCLUDE_TESTS)
diff --git a/compiler-rt/lib/sanitizer_common/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/CMakeLists.txt
index d8517f3de06a493..25304b71c0c7061 100644
--- a/compiler-rt/lib/sanitizer_common/CMakeLists.txt
+++ b/compiler-rt/lib/sanitizer_common/CMakeLists.txt
@@ -362,3 +362,7 @@ foreach(arch ${SANITIZER_COMMON_SUPPORTED_ARCH})
add_library(RTSanitizerCommonSymbolizerInternal.${arch}
OBJECT IMPORTED GLOBAL)
endforeach()
+
+if (COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER)
+ add_subdirectory(symbolizer)
+endif()
diff --git a/compiler-rt/lib/sanitizer_common/symbolizer/CMakeLists.txt b/compiler-rt/lib/sanitizer_common/symbolizer/CMakeLists.txt
new file mode 100644
index 000000000000000..49ff201066bcf92
--- /dev/null
+++ b/compiler-rt/lib/sanitizer_common/symbolizer/CMakeLists.txt
@@ -0,0 +1,27 @@
+foreach(arch ${SANITIZER_COMMON_SUPPORTED_ARCH})
+ get_target_flags_for_arch(${arch} TARGET_CFLAGS)
+
+ set(RTSanitizerCommonSymbolizerInternalDir
+ "${CMAKE_CURRENT_BINARY_DIR}/RTSanitizerCommonSymbolizerInternal.${arch}")
+ add_custom_command(OUTPUT ${RTSanitizerCommonSymbolizerInternalDir}
+ COMMAND ${CMAKE_COMMAND} -E make_directory ${RTSanitizerCommonSymbolizerInternalDir})
+
+ add_custom_command(OUTPUT RTSanitizerCommonSymbolizerInternal.${arch}.o
+ DEPENDS ${RTSanitizerCommonSymbolizerInternalDir}
+ clang lld llvm-tblgen opt llvm-ar llvm-link llvm-ranlib llvm-symbolizer
+ sanitizer_wrappers.cpp
+ sanitizer_symbolize.cpp
+ scripts/build_symbolizer.sh
+ WORKING_DIRECTORY ${RTSanitizerCommonSymbolizerInternalDir}
+ COMMAND FLAGS=${TARGET_CFLAGS}
+ CLANG=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
+ ${CMAKE_CURRENT_SOURCE_DIR}/scripts/build_symbolizer.sh
+ ${CMAKE_CURRENT_BINARY_DIR}/RTSanitizerCommonSymbolizerInternal.${arch}.o
+ USES_TERMINAL)
+
+ add_custom_target(RTSanitizerCommonSymbolizerInternalObj.${arch}
+ DEPENDS RTSanitizerCommonSymbolizerInternal.${arch}.o)
+
+ set_property(TARGET RTSanitizerCommonSymbolizerInternal.${arch}
+ PROPERTY IMPORTED_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/RTSanitizerCommonSymbolizerInternal.${arch}.o)
+endforeach()
diff --git a/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh b/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
index ec3f73ab21fb4b2..524ddca1b9f3e4f 100755
--- a/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
+++ b/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
@@ -3,26 +3,16 @@
# Run as: CLANG=bin/clang build_symbolizer.sh out.o
# zlib can be downloaded from http://www.zlib.net.
#
-# Script compiles self-contained object file with symbolization code and injects
-# it into the given set of runtime libraries. Script updates only libraries
-# which has unresolved __sanitizer_symbolize_* symbols and matches architecture.
-# Object file is be compiled from LLVM sources with dependencies like libc++ and
-# zlib. Then it internalizes symbols in the file, so that it can be linked
-# into arbitrary programs, avoiding conflicts with the program own symbols and
-# avoiding dependencies on any program symbols. The only acceptable dependencies
-# are libc and __sanitizer::internal_* from sanitizer runtime.
+# Script compiles self-contained object file with symbolization code.
#
# Symbols exported by the object file will be used by Sanitizer runtime
# libraries to symbolize code/data in-process.
#
-# The script will modify the output directory which is given as the first
-# argument to the script.
-#
# FIXME: We should really be using a simpler approach to building this object
# file, and it should be available as a regular cmake rule. Conceptually, we
# want to be doing "ld -r" followed by "objcopy -G" to create a relocatable
# object file with only our entry points exposed. However, this does not work at
-# present, see PR30750.
+# present, see https://github.com/llvm/llvm-project/issues/30098.
set -x
set -e
@@ -30,7 +20,7 @@ set -u
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
SRC_DIR=$(readlink -f $SCRIPT_DIR/..)
-TARGE_DIR=$(readlink -f $1)
+OUTPUT=$(readlink -f $1)
COMPILER_RT_SRC=$(readlink -f ${SCRIPT_DIR}/../../../..)
LLVM_SRC=${LLVM_SRC:-${COMPILER_RT_SRC}/../llvm}
LLVM_SRC=$(readlink -f $LLVM_SRC)
@@ -186,20 +176,6 @@ nm -f posix -g symbolizer.o | cut -f 1,2 -d \ | LC_COLLATE=C sort -u > undefine
(
diff -u $SCRIPT_DIR/global_symbols.txt undefined.new | grep -E "^\+[^+]") && \
(echo "Failed: unexpected symbols"; exit 1)
-arch() {
- objdump -f $1 | grep -m1 -Po "(?<=file format ).*$"
-}
-
-SYMBOLIZER_FORMAT=$(arch symbolizer.o)
-echo "Injecting $SYMBOLIZER_FORMAT symbolizer..."
-for A in $TARGE_DIR/libclang_rt.*san*.a; do
- A_FORMAT=$(arch $A)
- if [[ "$A_FORMAT" != "$SYMBOLIZER_FORMAT" ]] ; then
- continue
- fi
- (nm -u $A 2>/dev/null | grep -E "__sanitizer_symbolize_code" >/dev/null) || continue
- echo "$A"
- $AR rcs $A symbolizer.o
-done
+cp -f symbolizer.o $OUTPUT
echo "Success!"
More information about the llvm-commits
mailing list