]> git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/lib/sanitizer_common/symbolizer/scripts/build_symbolizer.sh
New upstream version 1.20.0+dfsg1
[rustc.git] / src / libcompiler_builtins / compiler-rt / lib / sanitizer_common / symbolizer / scripts / build_symbolizer.sh
1 #!/bin/bash -eu
2 #
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.
6 #
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.
15 #
16 # Symbols exported by the object file will be used by Sanitizer runtime
17 # libraries to symbolize code/data in-process.
18 #
19 # The script will modify the output directory which is given as the first
20 # argument to the script.
21 #
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.
27
28 SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
29 SRC_DIR=$(readlink -f $SCRIPT_DIR/..)
30 TARGE_DIR=$(readlink -f $1)
31
32 LLVM_SRC="${LLVM_SRC:-$SCRIPT_DIR/../../../../../..}"
33 LLVM_SRC=$(readlink -f $LLVM_SRC)
34
35 if [[ ! -d "${LLVM_SRC}/projects/libcxxabi" ||
36 ! -d "${LLVM_SRC}/projects/libcxx" ]]; then
37 echo "Missing or incomplete LLVM_SRC"
38 exit 1
39 fi
40
41 if [[ "$ZLIB_SRC" == "" ||
42 ! -x "${ZLIB_SRC}/configure" ||
43 ! -f "${ZLIB_SRC}/zlib.h" ]]; then
44 echo "Missing or incomplete ZLIB_SRC"
45 exit 1
46 fi
47 ZLIB_SRC=$(readlink -f $ZLIB_SRC)
48
49 J="${J:-50}"
50
51 CLANG="${CLANG:-`which clang`}"
52 CLANG_DIR=$(readlink -f $(dirname "$CLANG"))
53
54 BUILD_DIR=$(readlink -f ./symbolizer)
55 mkdir -p $BUILD_DIR
56 cd $BUILD_DIR
57
58 CC=$CLANG_DIR/clang
59 CXX=$CLANG_DIR/clang++
60 TBLGEN=$CLANG_DIR/llvm-tblgen
61 LINK=$CLANG_DIR/llvm-link
62 OPT=$CLANG_DIR/opt
63 AR=$CLANG_DIR/llvm-ar
64
65 for F in $CC $CXX $TBLGEN $LINK $OPT $AR; do
66 if [[ ! -x "$F" ]]; then
67 echo "Missing $F"
68 exit 1
69 fi
70 done
71
72 ZLIB_BUILD=${BUILD_DIR}/zlib
73 LIBCXX_BUILD=${BUILD_DIR}/libcxx
74 LLVM_BUILD=${BUILD_DIR}/llvm
75 SYMBOLIZER_BUILD=${BUILD_DIR}/symbolizer
76
77 FLAGS=${FLAGS:-}
78 FLAGS="$FLAGS -fPIC -flto -Os -g0 -DNDEBUG"
79
80 # Build zlib.
81 mkdir -p ${ZLIB_BUILD}
82 cd ${ZLIB_BUILD}
83 cp -r ${ZLIB_SRC}/* .
84 CC=$CC CFLAGS="$FLAGS" RANLIB=/bin/true ./configure --static
85 make -j${J} libz.a
86
87 # Build and install libcxxabi and libcxx.
88 if [[ ! -d ${LIBCXX_BUILD} ]]; then
89 mkdir -p ${LIBCXX_BUILD}
90 cd ${LIBCXX_BUILD}
91 LIBCXX_FLAGS="${FLAGS} -Wno-macro-redefined -I${LLVM_SRC}/projects/libcxxabi/include"
92 cmake -GNinja \
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 \
107 $LLVM_SRC
108 fi
109 cd ${LIBCXX_BUILD}
110 ninja cxx cxxabi
111
112 FLAGS="${FLAGS} -fno-rtti -fno-exceptions"
113
114 # Build LLVM.
115 if [[ ! -d ${LLVM_BUILD} ]]; then
116 mkdir -p ${LLVM_BUILD}
117 cd ${LLVM_BUILD}
118 LLVM_FLAGS="${FLAGS} -I${ZLIB_BUILD} -I${LIBCXX_BUILD}/include/c++/v1"
119 cmake -GNinja \
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 \
129 $LLVM_SRC
130 fi
131 cd ${LLVM_BUILD}
132 ninja LLVMSymbolize LLVMObject LLVMDebugInfoDWARF LLVMSupport LLVMDebugInfoPDB LLVMMC
133
134 cd ${BUILD_DIR}
135 rm -rf ${SYMBOLIZER_BUILD}
136 mkdir ${SYMBOLIZER_BUILD}
137 cd ${SYMBOLIZER_BUILD}
138
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
149 $AR x $A $O
150 mv -f $O "$(basename $A).$O" # Rename to avoid collisions between libs.
151 done
152 done
153
154 echo "Compiling..."
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
157
158 SYMBOLIZER_API_LIST=__sanitizer_symbolize_code,__sanitizer_symbolize_data,__sanitizer_symbolize_flush,__sanitizer_symbolize_demangle
159
160 # Merge all the object files together and copy the resulting library back.
161 $LINK *.o -o all.bc
162 echo "Optimizing..."
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
165
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)
170
171 arch() {
172 objdump -f $1 | grep -m1 -Po "(?<=file format ).*$"
173 }
174
175 SYMBOLIZER_FORMAT=$(arch symbolizer.o)
176 echo "Injecting $SYMBOLIZER_FORMAT symbolizer..."
177 for A in $TARGE_DIR/libclang_rt.*san*.a; do
178 A_FORMAT=$(arch $A)
179 if [[ "$A_FORMAT" != "$SYMBOLIZER_FORMAT" ]] ; then
180 continue
181 fi
182 (nm -u $A 2>/dev/null | grep -E "__sanitizer_symbolize_code" >/dev/null) || continue
183 echo "$A"
184 $AR rcs $A symbolizer.o
185 done
186
187 echo "Success!"