[LLVM][IR] Add support for vector ConstantInt/FP to ConstandFolding:FoldBitCast....
[llvm-project.git] / compiler-rt / lib / sanitizer_common / symbolizer / scripts / build_symbolizer.sh
blobb4702339db59cc619108c269d1aef15848de9233
1 #!/usr/bin/env bash
3 # Run as: CLANG=bin/clang build_symbolizer.sh out.o
4 # If you want to use a local copy of zlib, set ZLIB_SRC.
5 # zlib can be downloaded from http://www.zlib.net.
7 # Script compiles self-contained object file with symbolization code.
9 # Symbols exported by the object file will be used by Sanitizer runtime
10 # libraries to symbolize code/data in-process.
12 # FIXME: We should really be using a simpler approach to building this object
13 # file, and it should be available as a regular cmake rule. Conceptually, we
14 # want to be doing "ld -r" followed by "objcopy -G" to create a relocatable
15 # object file with only our entry points exposed. However, this does not work at
16 # present, see https://github.com/llvm/llvm-project/issues/30098.
18 set -x
19 set -e
20 set -u
22 SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
23 SRC_DIR=$(readlink -f $SCRIPT_DIR/..)
25 if [[ $# -ne 1 ]]; then
26 echo "Missing output file"
27 exit 1
30 OUTPUT=$(readlink -f $1)
31 COMPILER_RT_SRC=$(readlink -f ${SCRIPT_DIR}/../../../..)
32 LLVM_SRC=${LLVM_SRC:-${COMPILER_RT_SRC}/../llvm}
33 LLVM_SRC=$(readlink -f $LLVM_SRC)
35 CLANG="${CLANG:-`which clang`}"
36 CLANG_DIR=$(readlink -f $(dirname "$CLANG"))
38 CC=$CLANG_DIR/clang
39 CXX=$CLANG_DIR/clang++
40 TBLGEN=$CLANG_DIR/llvm-tblgen
41 OPT=$CLANG_DIR/opt
42 AR=$CLANG_DIR/llvm-ar
43 LINK=$CLANG_DIR/llvm-link
45 for F in $CC $CXX $TBLGEN $LINK $OPT $AR; do
46 if [[ ! -x "$F" ]]; then
47 echo "Missing $F"
48 exit 1
50 done
52 BUILD_DIR=${PWD}/symbolizer
53 mkdir -p $BUILD_DIR
54 cd $BUILD_DIR
56 ZLIB_BUILD=${BUILD_DIR}/zlib
57 LIBCXX_BUILD=${BUILD_DIR}/libcxx
58 LLVM_BUILD=${BUILD_DIR}/llvm
59 SYMBOLIZER_BUILD=${BUILD_DIR}/symbolizer
61 FLAGS=${FLAGS:-}
62 ZLIB_SRC=${ZLIB_SRC:-}
63 TARGET_TRIPLE=$($CC -print-target-triple $FLAGS)
64 if [[ "$FLAGS" =~ "-m32" ]] ; then
65 # Avoid new wrappers.
66 FLAGS+=" -U_FILE_OFFSET_BITS"
68 FLAGS+=" -fPIC -flto -Oz -g0 -DNDEBUG -target $TARGET_TRIPLE -Wno-unused-command-line-argument"
69 FLAGS+=" -include ${SRC_DIR}/../sanitizer_redefine_builtins.h -DSANITIZER_COMMON_REDEFINE_BUILTINS_IN_STD -Wno-language-extension-token"
71 LINKFLAGS="-fuse-ld=lld -target $TARGET_TRIPLE"
73 # Build zlib.
74 if [[ ! -d ${ZLIB_BUILD} ]]; then
75 if [[ -z "${ZLIB_SRC}" ]]; then
76 git clone https://github.com/madler/zlib ${ZLIB_BUILD}
77 else
78 ZLIB_SRC=$(readlink -f $ZLIB_SRC)
79 mkdir -p ${ZLIB_BUILD}
80 cp -r ${ZLIB_SRC}/* ${ZLIB_BUILD}/
84 cd ${ZLIB_BUILD}
85 AR="${AR}" CC="${CC}" CFLAGS="$FLAGS -Wno-deprecated-non-prototype" RANLIB=/bin/true ./configure --static
86 make -j libz.a
88 # Build and install libcxxabi and libcxx.
89 if [[ ! -f ${LLVM_BUILD}/build.ninja ]]; then
90 rm -rf ${LIBCXX_BUILD}
91 mkdir -p ${LIBCXX_BUILD}
92 cd ${LIBCXX_BUILD}
93 LIBCXX_FLAGS="${FLAGS} -Wno-macro-redefined"
94 cmake -GNinja \
95 -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi" \
96 -DCMAKE_BUILD_TYPE=Release \
97 -DCMAKE_C_COMPILER_WORKS=ON \
98 -DCMAKE_CXX_COMPILER_WORKS=ON \
99 -DCMAKE_C_COMPILER=$CC \
100 -DCMAKE_CXX_COMPILER=$CXX \
101 -DLIBCXX_ABI_NAMESPACE=__InternalSymbolizer \
102 '-DLIBCXX_EXTRA_SITE_DEFINES=_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS;_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS' \
103 -DCMAKE_C_FLAGS_RELEASE="${LIBCXX_FLAGS}" \
104 -DCMAKE_CXX_FLAGS_RELEASE="${LIBCXX_FLAGS}" \
105 -DLIBCXXABI_ENABLE_ASSERTIONS=OFF \
106 -DLIBCXXABI_ENABLE_EXCEPTIONS=OFF \
107 -DLIBCXXABI_USE_LLVM_UNWINDER=OFF \
108 -DLIBCXX_ENABLE_ASSERTIONS=OFF \
109 -DLIBCXX_ENABLE_EXCEPTIONS=OFF \
110 -DLIBCXX_ENABLE_RTTI=OFF \
111 -DCMAKE_SHARED_LINKER_FLAGS="$LINKFLAGS" \
112 -DLIBCXX_ENABLE_SHARED=OFF \
113 -DLIBCXXABI_ENABLE_SHARED=OFF \
114 $LLVM_SRC/../runtimes
116 cd ${LIBCXX_BUILD}
117 ninja cxx cxxabi
119 FLAGS="${FLAGS} -fno-rtti -fno-exceptions"
120 LLVM_CFLAGS="${FLAGS} -Wno-global-constructors"
121 LLVM_CXXFLAGS="${LLVM_CFLAGS} -nostdinc++ -I${ZLIB_BUILD} -isystem ${LIBCXX_BUILD}/include -isystem ${LIBCXX_BUILD}/include/c++/v1"
123 # Build LLVM.
124 if [[ ! -f ${LLVM_BUILD}/build.ninja ]]; then
125 rm -rf ${LLVM_BUILD}
126 mkdir -p ${LLVM_BUILD}
127 cd ${LLVM_BUILD}
128 cmake -GNinja \
129 -DCMAKE_BUILD_TYPE=Release \
130 -DCMAKE_C_COMPILER_WORKS=ON \
131 -DCMAKE_CXX_COMPILER_WORKS=ON \
132 -DCMAKE_C_COMPILER=$CC \
133 -DCMAKE_CXX_COMPILER=$CXX \
134 -DLLVM_ENABLE_LIBCXX=ON \
135 -DCMAKE_C_FLAGS_RELEASE="${LLVM_CFLAGS}" \
136 -DCMAKE_CXX_FLAGS_RELEASE="${LLVM_CXXFLAGS}" \
137 -DCMAKE_EXE_LINKER_FLAGS="$LINKFLAGS -stdlib=libc++ -L${LIBCXX_BUILD}/lib" \
138 -DLLVM_TABLEGEN=$TBLGEN \
139 -DLLVM_INCLUDE_TESTS=OFF \
140 -DLLVM_ENABLE_ZLIB=ON \
141 -DLLVM_ENABLE_ZSTD=OFF \
142 -DLLVM_ENABLE_THREADS=OFF \
143 $LLVM_SRC
145 cd ${LLVM_BUILD}
146 ninja LLVMSymbolize LLVMObject LLVMBinaryFormat LLVMDebugInfoDWARF LLVMSupport LLVMDebugInfoPDB LLVMDebuginfod LLVMMC LLVMDemangle LLVMTextAPI LLVMTargetParser LLVMCore
148 cd ${BUILD_DIR}
149 rm -rf ${SYMBOLIZER_BUILD}
150 mkdir ${SYMBOLIZER_BUILD}
151 cd ${SYMBOLIZER_BUILD}
153 echo "Compiling..."
154 SYMBOLIZER_FLAGS="$LLVM_CXXFLAGS -I${LLVM_SRC}/include -I${LLVM_BUILD}/include -std=c++17"
155 $CXX $SYMBOLIZER_FLAGS ${SRC_DIR}/sanitizer_symbolize.cpp ${SRC_DIR}/sanitizer_wrappers.cpp -c
156 $AR rc symbolizer.a sanitizer_symbolize.o sanitizer_wrappers.o
158 SYMBOLIZER_API_LIST=__sanitizer_symbolize_code
159 SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_data
160 SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_frame
161 SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_flush
162 SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_demangle
163 SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_set_demangle
164 SYMBOLIZER_API_LIST+=,__sanitizer_symbolize_set_inline_frames
166 LIBCXX_ARCHIVE_DIR=$(dirname $(find $LIBCXX_BUILD -name libc++.a | head -n1))
168 # Merge all the object files together and copy the resulting library back.
169 $LINK $LIBCXX_ARCHIVE_DIR/libc++.a \
170 $LIBCXX_ARCHIVE_DIR/libc++abi.a \
171 $LLVM_BUILD/lib/libLLVMSymbolize.a \
172 $LLVM_BUILD/lib/libLLVMObject.a \
173 $LLVM_BUILD/lib/libLLVMBinaryFormat.a \
174 $LLVM_BUILD/lib/libLLVMDebugInfoDWARF.a \
175 $LLVM_BUILD/lib/libLLVMSupport.a \
176 $LLVM_BUILD/lib/libLLVMDebugInfoPDB.a \
177 $LLVM_BUILD/lib/libLLVMDebugInfoMSF.a \
178 $LLVM_BUILD/lib/libLLVMDebugInfoCodeView.a \
179 $LLVM_BUILD/lib/libLLVMDebuginfod.a \
180 $LLVM_BUILD/lib/libLLVMDemangle.a \
181 $LLVM_BUILD/lib/libLLVMMC.a \
182 $LLVM_BUILD/lib/libLLVMTextAPI.a \
183 $LLVM_BUILD/lib/libLLVMTargetParser.a \
184 $LLVM_BUILD/lib/libLLVMCore.a \
185 $ZLIB_BUILD/libz.a \
186 symbolizer.a \
187 -ignore-non-bitcode -o all.bc
189 echo "Optimizing..."
190 $OPT -passes=internalize -internalize-public-api-list=${SYMBOLIZER_API_LIST} all.bc -o opt.bc
191 $CC $FLAGS -fno-lto -c opt.bc -o symbolizer.o
193 echo "Checking undefined symbols..."
194 export LC_ALL=C
195 nm -f posix -g symbolizer.o | cut -f 1,2 -d \ | sort -u > undefined.new
196 grep -Ev "^#|^$" $SCRIPT_DIR/global_symbols.txt | sort -u > expected.new
197 (diff -u expected.new undefined.new | grep -E "^\+[^+]") && \
198 (echo "Failed: unexpected symbols"; exit 1)
200 cp -f symbolizer.o $OUTPUT
202 echo "Success!"