【问题标题】:Configure an autotools project with Clang sanitizers in a static lib configuration?在静态库配置中使用 Clang sanitizers 配置 autotools 项目?
【发布时间】:2013-11-22 18:52:27
【问题描述】:

编辑:如果是 TLDR,就跳到底部。我要问的是:如何配置 autotools 项目以使用静态库?

我正在使用几个开源库,并尝试在 Clang 的消毒剂下运行他们的测试套件。要在 Clang sanitizers 下运行,我们需要 (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

但是,这会产生一些带有未定义符号的存档警告(当运行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'

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

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

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"

这会导致configure 问题:

configure: error: cannot run C compiled programs.
If you meant to cross compile, use `--host'.
...

查看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

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

export LDFLAGS="-L/usr/local/lib/clang/3.3/lib/darwin/"
export LIBS="libclang_rt.asan_osx.a libclang_rt.ubsan_osx.a"

这会导致类似的错误:

configure: error: in `/Users/jwalton/libpng-1.6.7':
configure: error: C compiler cannot create executables

还有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'

LIBS 中删除lib 前缀和.a 后缀会导致:

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'

-l 添加到LIBS 会导致:

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

最后,-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

了解所有背景:如何配置 autotools 项目以使用静态库?

奖励积分:为什么这么简单的事情变得这么困难?

【问题讨论】:

    标签: static-libraries clang configure autotools sanitizer


    【解决方案1】:

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

    【讨论】:

    • 一点提示:对我来说,当我这样做时,«libubsan» 是动态链接的,即使我明确指出静态链接它也是如此。解决方案不是在 LDFLAGS 中使用 -fsanitize,而是在其中添加静态库 libubsan.a 的完整路径。
    • @Hi-Angel 对于 GCC,规范的解决方案是使用 -static-libubsan
    【解决方案2】:

    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"
    

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

    【讨论】:

    • 啊,完美。非常感谢。我不会进行这些更改,但它清楚地表明了它的 Autotools 问题和消毒剂的解决方案。他们可能也应该检查“没有消毒剂”。例如,我对CXXFLAGS 使用以下内容:-fsanitize=undefined -fno-sanitize=vptr
    • 如果你使用 MacPorts,你可以只安装 libtool 端口。它已经有了补丁。您需要在项目中根据需要重新运行 autoreconfglibtoolize 以获取较新的版本,但这应该会有所帮助。
    【解决方案3】:

    至于未定义符号问题,如果您要链接 C++ 对象,则必须使用 clang++ 而不是 clang。否则会出现如下错误:

    /bin/bash libtool --tag=CC --mode=link clang-3.6 ... -fsanitize=undefined -o freeswitch ...
    libtool: link: clang-3.6 ... -fsanitize=undefined -o .libs/freeswitch ./.libs/libfreeswitch.so ...
    ./.libs/libfreeswitch.so: undefined reference to `__ubsan_vptr_type_cache'
    ./.libs/libfreeswitch.so: undefined reference to `__ubsan_handle_dynamic_type_cache_miss'
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    

    在上述情况下,残暴的libtool 使用了clang 而不是clang++,因为automake 认为你想链接一组C 对象(它不理解使用C++ 链接一个libtool 存档(libfreeswitch.la)表示需要 C++ 链接器(注意 --tag=CC)。另请参阅 https://lists.debian.org/debian-mentors/2003/06/msg00004.html

    要解决此问题,请按照https://www.gnu.org/software/automake/manual/html_node/Libtool-Convenience-Libraries.html 中的说明操作,并将(虚拟/真实)C++ 源代码添加到您的Makefile.am 中的源代码中:

    SUBDIRS = sub1 sub2 …
    lib_LTLIBRARIES = libtop.la
    libtop_la_SOURCES =
    # Dummy C++ source to cause C++ linking.
    nodist_EXTRA_libtop_la_SOURCES = dummy.cxx
    libtop_la_LIBADD = \
      sub1/libsub1.la \
      sub2/libsub2.la \
      …
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-12
      • 2013-06-03
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      相关资源
      最近更新 更多