Skip to content

Commit 9dc5d8d

Browse files
committed
[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 #30098 Reviewed By: kstoimenov, MaskRay Differential Revision: https://reviews.llvm.org/D157947
1 parent f0f548a commit 9dc5d8d

File tree

4 files changed

+38
-28
lines changed

4 files changed

+38
-28
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,9 @@ endif()
727727
pythonize_bool(COMPILER_RT_HAS_LLD)
728728
pythonize_bool(COMPILER_RT_TEST_USE_LLD)
729729

730+
option(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER "Build Compiler RT linked with in LLVM symbolizer" OFF)
731+
mark_as_advanced(COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER)
732+
730733
add_subdirectory(lib)
731734

732735
if(COMPILER_RT_INCLUDE_TESTS)

compiler-rt/lib/sanitizer_common/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,7 @@ foreach(arch ${SANITIZER_COMMON_SUPPORTED_ARCH})
362362
add_library(RTSanitizerCommonSymbolizerInternal.${arch}
363363
OBJECT IMPORTED GLOBAL)
364364
endforeach()
365+
366+
if (COMPILER_RT_ENABLE_INTERNAL_SYMBOLIZER)
367+
add_subdirectory(symbolizer)
368+
endif()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
foreach(arch ${SANITIZER_COMMON_SUPPORTED_ARCH})
2+
get_target_flags_for_arch(${arch} TARGET_CFLAGS)
3+
4+
set(RTSanitizerCommonSymbolizerInternalDir
5+
"${CMAKE_CURRENT_BINARY_DIR}/RTSanitizerCommonSymbolizerInternal.${arch}")
6+
add_custom_command(OUTPUT ${RTSanitizerCommonSymbolizerInternalDir}
7+
COMMAND ${CMAKE_COMMAND} -E make_directory ${RTSanitizerCommonSymbolizerInternalDir})
8+
9+
add_custom_command(OUTPUT RTSanitizerCommonSymbolizerInternal.${arch}.o
10+
DEPENDS ${RTSanitizerCommonSymbolizerInternalDir}
11+
clang lld llvm-tblgen opt llvm-ar llvm-link llvm-ranlib llvm-symbolizer
12+
sanitizer_wrappers.cpp
13+
sanitizer_symbolize.cpp
14+
scripts/build_symbolizer.sh
15+
WORKING_DIRECTORY ${RTSanitizerCommonSymbolizerInternalDir}
16+
COMMAND FLAGS=${TARGET_CFLAGS}
17+
CLANG=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang
18+
${CMAKE_CURRENT_SOURCE_DIR}/scripts/build_symbolizer.sh
19+
${CMAKE_CURRENT_BINARY_DIR}/RTSanitizerCommonSymbolizerInternal.${arch}.o
20+
USES_TERMINAL)
21+
22+
add_custom_target(RTSanitizerCommonSymbolizerInternalObj.${arch}
23+
DEPENDS RTSanitizerCommonSymbolizerInternal.${arch}.o)
24+
25+
set_property(TARGET RTSanitizerCommonSymbolizerInternal.${arch}
26+
PROPERTY IMPORTED_OBJECTS ${CMAKE_CURRENT_BINARY_DIR}/RTSanitizerCommonSymbolizerInternal.${arch}.o)
27+
endforeach()

compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,24 @@
33
# Run as: CLANG=bin/clang build_symbolizer.sh out.o
44
# zlib can be downloaded from http://www.zlib.net.
55
#
6-
# Script compiles self-contained object file with symbolization code and injects
7-
# it into the given set of runtime libraries. Script updates only libraries
8-
# which has unresolved __sanitizer_symbolize_* symbols and matches architecture.
9-
# Object file is be compiled from LLVM sources with dependencies like libc++ and
10-
# zlib. Then it internalizes symbols in the file, so that it can be linked
11-
# into arbitrary programs, avoiding conflicts with the program own symbols and
12-
# avoiding dependencies on any program symbols. The only acceptable dependencies
13-
# are libc and __sanitizer::internal_* from sanitizer runtime.
6+
# Script compiles self-contained object file with symbolization code.
147
#
158
# Symbols exported by the object file will be used by Sanitizer runtime
169
# libraries to symbolize code/data in-process.
1710
#
18-
# The script will modify the output directory which is given as the first
19-
# argument to the script.
20-
#
2111
# FIXME: We should really be using a simpler approach to building this object
2212
# file, and it should be available as a regular cmake rule. Conceptually, we
2313
# want to be doing "ld -r" followed by "objcopy -G" to create a relocatable
2414
# object file with only our entry points exposed. However, this does not work at
25-
# present, see PR30750.
15+
# present, see https://github.com/llvm/llvm-project/issues/30098.
2616

2717
set -x
2818
set -e
2919
set -u
3020

3121
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
3222
SRC_DIR=$(readlink -f $SCRIPT_DIR/..)
33-
TARGE_DIR=$(readlink -f $1)
23+
OUTPUT=$(readlink -f $1)
3424
COMPILER_RT_SRC=$(readlink -f ${SCRIPT_DIR}/../../../..)
3525
LLVM_SRC=${LLVM_SRC:-${COMPILER_RT_SRC}/../llvm}
3626
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
186176
(diff -u $SCRIPT_DIR/global_symbols.txt undefined.new | grep -E "^\+[^+]") && \
187177
(echo "Failed: unexpected symbols"; exit 1)
188178

189-
arch() {
190-
objdump -f $1 | grep -m1 -Po "(?<=file format ).*$"
191-
}
192-
193-
SYMBOLIZER_FORMAT=$(arch symbolizer.o)
194-
echo "Injecting $SYMBOLIZER_FORMAT symbolizer..."
195-
for A in $TARGE_DIR/libclang_rt.*san*.a; do
196-
A_FORMAT=$(arch $A)
197-
if [[ "$A_FORMAT" != "$SYMBOLIZER_FORMAT" ]] ; then
198-
continue
199-
fi
200-
(nm -u $A 2>/dev/null | grep -E "__sanitizer_symbolize_code" >/dev/null) || continue
201-
echo "$A"
202-
$AR rcs $A symbolizer.o
203-
done
179+
cp -f symbolizer.o $OUTPUT
204180

205181
echo "Success!"

0 commit comments

Comments
 (0)