[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
This commit is contained in:
Vitaly Buka
2023-09-08 11:06:51 -07:00
parent f0f548a65a
commit 9dc5d8df16
4 changed files with 38 additions and 28 deletions

View File

@@ -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)

View File

@@ -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()

View File

@@ -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()

View File

@@ -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!"