在静态lib配置中使用Clang清理程序配置autotools项目?

jww*_*jww 6 autotools configure clang static-libraries sanitizer

编辑:如果它的TLDR,只是跳到底部.我问的地方:如何配置autotools项目以使用静态库?

我正在使用几个开源库,我正在尝试在Clang的清洁剂下运行他们的测试套件.要在Clang清理程序下运行,我们需要(1)指定一些选项,以及(2)根据需要链接Clang的Compiler-RT中的静态库.注意:没有动态库或共享对象.

设置选项很简单:

export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/lib/clang/3.3/lib/darwin/
export CC=/usr/local/bin/clang
export CXX=/usr/local/bin/clang++
export CFLAGS="-g3 -fsanitize=address -fsanitize=undefined"
export CXXFLAGS="-g3 -fsanitize=address -fsanitize=undefined -fno-sanitize=vptr"

./configure
Run Code Online (Sandbox Code Playgroud)

但是,这将生成一些存档警告(AR运行时)和链接错误(LD运行时)和未定义的符号.该消息类似于:

libjpeg.a(jmemmgr.o): In function `do_sarray_io':
/home/jwalton/jpeg-6b/jmemmgr.c:695: undefined reference to
`__ubsan_handle_type_mismatch'
/home/jwalton/jpeg-6b/jmemmgr.c:695: undefined reference to
`__ubsan_handle_type_mismatch'
/home/jwalton/jpeg-6b/jmemmgr.c:696: undefined reference to
`__ubsan_handle_type_mismatch'
Run Code Online (Sandbox Code Playgroud)

我知道需要链接的库.对于我使用的消毒剂,他们是libclang_rt.asan_osx.alibclang_rt.ubsan_osx.a(或libclang_rt.full-x86_64.alibclang_rt.ubsan-x86_64.a在Linux上).

为了提供库,我然后导出以下内容.注意:它是LIBS,而不是LDLIBS大多数其他make相关工具所预期的.

export LIBS="/usr/local/lib/clang/3.3/lib/darwin/libclang_rt.asan_osx.a \
             /usr/local/lib/clang/3.3/lib/darwin/libclang_rt.ubsan_osx.a"
Run Code Online (Sandbox Code Playgroud)

这导致以下configure问题:

configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
...
Run Code Online (Sandbox Code Playgroud)

看着config.log,它看起来像两个问题发生.首先,通过改变/usr/local/...来改变路径/Users/jwalton/....第二,通过从静态库更改为动态库来篡改文件名:

configure:3346: ./conftest
dyld: Library not loaded: /Users/jwalton/clang-llvm/llvm-3.3.src/Release+Asserts/lib/clang/3.3/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
  Referenced from: /Users/jwalton/libpng-1.6.7/./conftest
Reason: image not found
Run Code Online (Sandbox Code Playgroud)

在另一次尝试中,我尝试使用LDFLAGS:

export LDFLAGS="-L/usr/local/lib/clang/3.3/lib/darwin/"
export LIBS="libclang_rt.asan_osx.a libclang_rt.ubsan_osx.a"
Run Code Online (Sandbox Code Playgroud)

这会导致类似的错误:

configure: error: in `/Users/jwalton/libpng-1.6.7':
configure: error: C compiler cannot create executables
Run Code Online (Sandbox Code Playgroud)

而且config.log:

configure:3209: /usr/local/bin/clang -g3 -fsanitize=address -fsanitize=undefined  -L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c libclang_rt.asan_osx.a libclang_rt.ubsan_osx.a >&5
clang: error: no such file or directory: 'libclang_rt.asan_osx.a'
clang: error: no such file or directory: 'libclang_rt.ubsan_osx.a'
Run Code Online (Sandbox Code Playgroud)

并从结果中删除lib前缀和.a后缀LIBS:

configure:3209: /usr/local/bin/clang -g3 -fsanitize=address -fsanitize=undefined  -L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c clang_rt.asan_osx clang_rt.ubsan_osx >&5
clang: error: no such file or directory: 'clang_rt.asan_osx'
clang: error: no such file or directory: 'clang_rt.ubsan_osx'
Run Code Online (Sandbox Code Playgroud)

并将结果添加-lLIBS:

configure:3335: /usr/local/bin/clang -o conftest -g3 -fsanitize=address -fsanitize=undefined  
    -L/usr/local/lib/clang/3.3/lib/darwin/ conftest.c -lclang_rt.asan_osx -lclang_rt.ubsan_osx >&5
configure:3339: $? = 0
configure:3346: ./conftest
dyld: could not load inserted library: /Users/jwalton/libpng-1.6.7/./conftest

./configure: line 3348: 38224 Trace/BPT trap: 5       ./conftest$ac_cv_exeext
Run Code Online (Sandbox Code Playgroud)

最后,-L论证是有效的:

$ ls /usr/local/lib/clang/3.3/lib/darwin/
libclang_rt.10.4.a          libclang_rt.ios.a
libclang_rt.asan_osx.a          libclang_rt.osx.a
libclang_rt.asan_osx_dynamic.dylib  libclang_rt.profile_ios.a
libclang_rt.cc_kext.a           libclang_rt.profile_osx.a
libclang_rt.cc_kext_ios5.a      libclang_rt.ubsan_osx.a
libclang_rt.eprintf.a
Run Code Online (Sandbox Code Playgroud)

毕竟背景:如何配置autotools项目以使用静态库?

奖励积分:为什么这么容易变得如此困难?

Ben*_*ope 5

您还需要将-fsanitize标志添加到LDFLAGS.


Jer*_*oia 3

GNU libtool 的发布版本不会将 -fsanitize=... 参数传递给链接器。您需要使用http://savannah.gnu.org/patch/?8775中的补丁更新 libtool 具体来说:

diff --git a/build-aux/ltmain.in b/build-aux/ltmain.in
index 0c40da0..b99b0cd 100644
--- a/build-aux/ltmain.in
+++ b/build-aux/ltmain.in
@@ -5362,10 +5362,11 @@ func_mode_link ()
       # -O*, -g*, -flto*, -fwhopr*, -fuse-linker-plugin GCC link-time optimization
       # -specs=*             GCC specs files
       # -stdlib=*            select c++ std lib with clang
+      # -fsanitize=*         Clang memory and address sanitizer
       -64|-mips[0-9]|-r[0-9][0-9]*|-xarch=*|-xtarget=*|+DA*|+DD*|-q*|-m*| \
       -t[45]*|-txscale*|-p|-pg|--coverage|-fprofile-*|-F*|@*|-tp=*|--sysroot=*| \
       -O*|-g*|-flto*|-fwhopr*|-fuse-linker-plugin|-fstack-protector*|-stdlib=*| \
-      -specs=*)
+      -specs=*|-fsanitize=*)
         func_quote_for_eval "$arg"
    arg=$func_quote_for_eval_result
         func_append compile_command " $arg"
Run Code Online (Sandbox Code Playgroud)

至于让 libtool 接受静态库,您应该能够在命令行上包含静态库的完整路径,或者您可以使用-L/usr/local/lib/clang/3.3/lib/darwin/ -lclang_rt.ubsan_osx. 但是,一旦 libtool 告诉 cfe 使用 -fsanitize=... 进行链接,cfe 就应该为您处理库魔法。