前言
由于应用在嵌入式设备运行的需要,尝试编译时加上asan,但是发现clang没有内置arm版本的asan库(libclang_rt.asan-arm.a等),所以需要自己编译。
编译过程
下载源码
# cd somewhere
# Get llvm, clang and compiler-rt
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd llvm
R=$(svn info | grep Revision: | awk '{print $2}')
(cd tools && svn co -r $R http://llvm.org/svn/llvm-project/cfe/trunk clang)
(cd projects && svn co -r $R http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt)
To switch your checkout to read-write mode:
CNAME=your_name_here
cd llvm
svn switch --relocate http://llvm.org/svn/llvm-project/llvm/trunk https://$CNAME@llvm.org/svn/llvm-project/llvm/trunk
(cd tools && svn switch --relocate http://llvm.org/svn/llvm-project/cfe/trunk https://$CNAME@llvm.org/svn/llvm-project/cfe/trunk clang)
(cd projects && svn switch --relocate http://llvm.org/svn/llvm-project/compiler-rt/trunk https://$CNAME@llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt)
交叉编译
首先进入源码里compiler-rt所在同级目录,创建asan_build目录。
cmake -DCOMPILER_RT_BUILD_BUILTINS=ON -DCOMPILER_RT_BUILD_SANITIZERS=ON -DCOMPILER_RT_BUILD_XRAY=ON -DCOMPILER_RT_BUILD_LIBFUZZER=ON -DCOMPILER_RT_BUILD_PROFILE=ON -DCMAKE_SYSROOT=/home/core/toolchain/arm-2019.09/arm-linux-gnueabi/libc/ -DCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN=/home/core/toolchain/arm-2019.09 -DCMAKE_ASM_COMPILER_TARGET="arm-linux-gnueabi" -DCMAKE_C_COMPILER_TARGET="arm-linux-gnueabi" -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON -DCMAKE_C_COMPILER=/home/core/toolchain/arm-2019.09/bin/arm-linux-gnueabi-gcc ../compiler-rt/
make
编译成功,可以看到lib/linux/下有asan相关的库。
clang_rt.crtbegin-arm.o libclang_rt.cfi-arm.a libclang_rt.scudo_minimal-arm.so libclang_rt.ubsan_standalone_cxx-arm.a
clang_rt.crtend-arm.o libclang_rt.cfi_diag-arm.a libclang_rt.stats-arm.a libclang_rt.ubsan_standalone_cxx-arm.a.syms
libclang_rt.asan-arm.a libclang_rt.lsan-arm.a libclang_rt.stats_client-arm.a libclang_rt.xray-arm.a
libclang_rt.asan-arm.a.syms libclang_rt.profile-arm.a libclang_rt.ubsan_minimal-arm.a libclang_rt.xray-basic-arm.a
libclang_rt.asan-arm.so libclang_rt.scudo-arm.a libclang_rt.ubsan_minimal-arm.a.syms libclang_rt.xray-fdr-arm.a
libclang_rt.asan_cxx-arm.a libclang_rt.scudo-arm.so libclang_rt.ubsan_minimal-arm.so libclang_rt.xray-profiling-arm.a
libclang_rt.asan_cxx-arm.a.syms libclang_rt.scudo_cxx-arm.a libclang_rt.ubsan_standalone-arm.a
libclang_rt.asan-preinit-arm.a libclang_rt.scudo_cxx_minimal-arm.a libclang_rt.ubsan_standalone-arm.a.syms
libclang_rt.builtins-arm.a libclang_rt.scudo_minimal-arm.a libclang_rt.ubsan_standalone-arm.so
参考链接
https://github.com/google/sanitizers/wiki/AddressSanitizerHowToBuild