3 # Run as: CLANG=bin/clang ZLIB_SRC=src/zlib \
4 # build_symbolizer.sh runtime_build/lib/clang/4.0.0/lib/linux/
5 # zlib can be downloaded from from http://www.zlib.net.
7 # Script compiles self-contained object file with symbolization code and injects
8 # it into the given set of runtime libraries. Script updates only libraries
9 # which has unresolved __sanitizer_symbolize_* symbols and matches architecture.
10 # Object file is be compiled from LLVM sources with dependencies like libc++ and
11 # zlib. Then it internalizes symbols in the file, so that it can be linked
12 # into arbitrary programs, avoiding conflicts with the program own symbols and
13 # avoiding dependencies on any program symbols. The only acceptable dependencies
14 # are libc and __sanitizer::internal_* from sanitizer runtime.
16 # Symbols exported by the object file will be used by Sanitizer runtime
17 # libraries to symbolize code/data in-process.
19 # The script will modify the output directory which is given as the first
20 # argument to the script.
22 # FIXME: We should really be using a simpler approach to building this object
23 # file, and it should be available as a regular cmake rule. Conceptually, we
24 # want to be doing "ld -r" followed by "objcopy -G" to create a relocatable
25 # object file with only our entry points exposed. However, this does not work at
26 # present, see PR30750.
28 SCRIPT_DIR
="$(cd "$
(dirname "$0")" && pwd)"
29 SRC_DIR
=$
(readlink
-f $SCRIPT_DIR/..
)
30 TARGE_DIR
=$
(readlink
-f $1)
32 LLVM_SRC
="${LLVM_SRC:-$SCRIPT_DIR/../../../../../..}"
33 LLVM_SRC
=$
(readlink
-f $LLVM_SRC)
35 if [[ ! -d "${LLVM_SRC}/projects/libcxxabi" ||
36 ! -d "${LLVM_SRC}/projects/libcxx" ]]; then
37 echo "Missing or incomplete LLVM_SRC"
41 if [[ "$ZLIB_SRC" == "" ||
42 ! -x "${ZLIB_SRC}/configure" ||
43 ! -f "${ZLIB_SRC}/zlib.h" ]]; then
44 echo "Missing or incomplete ZLIB_SRC"
47 ZLIB_SRC
=$
(readlink
-f $ZLIB_SRC)
51 CLANG
="${CLANG:-`which clang`}"
52 CLANG_DIR
=$
(readlink
-f $
(dirname "$CLANG"))
54 BUILD_DIR
=$
(readlink
-f .
/symbolizer
)
59 CXX
=$CLANG_DIR/clang
++
60 TBLGEN
=$CLANG_DIR/llvm-tblgen
61 LINK
=$CLANG_DIR/llvm-link
65 for F
in $CC $CXX $TBLGEN $LINK $OPT $AR; do
66 if [[ ! -x "$F" ]]; then
72 ZLIB_BUILD
=${BUILD_DIR}/zlib
73 LIBCXX_BUILD
=${BUILD_DIR}/libcxx
74 LLVM_BUILD
=${BUILD_DIR}/llvm
75 SYMBOLIZER_BUILD
=${BUILD_DIR}/symbolizer
78 FLAGS
="$FLAGS -fPIC -flto -Os -g0 -DNDEBUG"
81 mkdir
-p ${ZLIB_BUILD}
84 CC
=$CC CFLAGS
="$FLAGS" RANLIB
=/bin
/true .
/configure
--static
87 # Build and install libcxxabi and libcxx.
88 if [[ ! -d ${LIBCXX_BUILD} ]]; then
89 mkdir
-p ${LIBCXX_BUILD}
91 LIBCXX_FLAGS
="${FLAGS} -Wno-macro-redefined -I${LLVM_SRC}/projects/libcxxabi/include"
93 -DCMAKE_BUILD_TYPE=Release \
94 -DCMAKE_C_COMPILER=$CC \
95 -DCMAKE_CXX_COMPILER=$CXX \
96 -DCMAKE_C_FLAGS_RELEASE="${LIBCXX_FLAGS}" \
97 -DCMAKE_CXX_FLAGS_RELEASE="${LIBCXX_FLAGS}" \
98 -DLIBCXXABI_ENABLE_ASSERTIONS=OFF \
99 -DLIBCXXABI_ENABLE_EXCEPTIONS=OFF \
100 -DLIBCXXABI_ENABLE_SHARED=OFF \
101 -DLIBCXXABI_ENABLE_THREADS=OFF \
102 -DLIBCXX_ENABLE_ASSERTIONS=OFF \
103 -DLIBCXX_ENABLE_EXCEPTIONS=OFF \
104 -DLIBCXX_ENABLE_RTTI=OFF \
105 -DLIBCXX_ENABLE_SHARED=OFF \
106 -DLIBCXX_ENABLE_THREADS=OFF \
112 FLAGS
="${FLAGS} -fno-rtti -fno-exceptions"
115 if [[ ! -d ${LLVM_BUILD} ]]; then
116 mkdir
-p ${LLVM_BUILD}
118 LLVM_FLAGS
="${FLAGS} -I${ZLIB_BUILD} -I${LIBCXX_BUILD}/include/c++/v1"
120 -DCMAKE_BUILD_TYPE=Release \
121 -DCMAKE_C_COMPILER=$CC \
122 -DCMAKE_CXX_COMPILER=$CXX \
123 -DCMAKE_C_FLAGS_RELEASE="${LLVM_FLAGS}" \
124 -DCMAKE_CXX_FLAGS_RELEASE="${LLVM_FLAGS}" \
125 -DLLVM_TABLEGEN=$TBLGEN \
126 -DLLVM_ENABLE_ZLIB=ON \
127 -DLLVM_ENABLE_TERMINFO=OFF \
128 -DLLVM_ENABLE_THREADS=OFF \
132 ninja LLVMSymbolize LLVMObject LLVMDebugInfoDWARF LLVMSupport LLVMDebugInfoPDB LLVMMC
135 rm -rf ${SYMBOLIZER_BUILD}
136 mkdir
${SYMBOLIZER_BUILD}
137 cd ${SYMBOLIZER_BUILD}
139 for A
in $LIBCXX_BUILD/lib
/libc
++.a \
140 $LIBCXX_BUILD/lib
/libc
++abi.a \
141 $LLVM_BUILD/lib
/libLLVMSymbolize.a \
142 $LLVM_BUILD/lib
/libLLVMObject.a \
143 $LLVM_BUILD/lib
/libLLVMDebugInfoDWARF.a \
144 $LLVM_BUILD/lib
/libLLVMSupport.a \
145 $LLVM_BUILD/lib
/libLLVMDebugInfoPDB.a \
146 $LLVM_BUILD/lib
/libLLVMMC.a \
147 $ZLIB_BUILD/libz.a
; do
148 for O
in $
($AR t
$A); do
150 mv -f $O "$(basename $A).$O" # Rename to avoid collisions between libs.
155 SYMBOLIZER_FLAGS
="$FLAGS -std=c++11 -I${LLVM_SRC}/include -I${LLVM_BUILD}/include -I${LIBCXX_BUILD}/include/c++/v1"
156 $CXX $SYMBOLIZER_FLAGS ${SRC_DIR}/sanitizer_symbolize.cc
${SRC_DIR}/sanitizer_wrappers.cc
-c
158 SYMBOLIZER_API_LIST
=__sanitizer_symbolize_code
,__sanitizer_symbolize_data
,__sanitizer_symbolize_flush
,__sanitizer_symbolize_demangle
160 # Merge all the object files together and copy the resulting library back.
163 $OPT -internalize -internalize-public-api-list=${SYMBOLIZER_API_LIST} all.
bc -o opt.
bc
164 $CC $FLAGS -fno-lto -c opt.
bc -o symbolizer.o
166 echo "Checking undefined symbols..."
167 nm
-f posix
-g symbolizer.o | cut
-f 1,2 -d \ | LC_COLLATE
=C
sort -u > undefined.new
168 (diff -u $SCRIPT_DIR/global_symbols.txt undefined.new |
grep -E "^\+[^+]") && \
169 (echo "Failed: unexpected symbols"; exit 1)
172 objdump
-f $1 |
grep -m1 -Po "(?<=file format ).*$"
175 SYMBOLIZER_FORMAT
=$
(arch symbolizer.o
)
176 echo "Injecting $SYMBOLIZER_FORMAT symbolizer..."
177 for A
in $TARGE_DIR/libclang_rt.
*san
*.a
; do
179 if [[ "$A_FORMAT" != "$SYMBOLIZER_FORMAT" ]] ; then
182 (nm
-u $A 2>/dev
/null |
grep -E "__sanitizer_symbolize_code" >/dev
/null
) ||
continue
184 $AR rcs
$A symbolizer.o