Linker 链路时间优化不';我不能在Fedora 18上正常工作

Linker 链路时间优化不';我不能在Fedora 18上正常工作,linker,clang,fedora,clang++,lto,Linker,Clang,Fedora,Clang++,Lto,我是一个新来的叮当声,所以很可能我在做一些愚蠢的事情。但我已经花了几个小时寻找解决方案,包括在这里搜索,在这里我没有找到问题解决方法——使用发行版提供的软件包解决flto。这个描述的细节是针对Fedora18的,但是我在Ubuntu13.04上遇到了类似的问题,所以这个问题不是针对Fedora的。不是我就是叮当 问题:我正在尝试使用clang++-flto编译一个简单的hello-world程序,以获得链接时间优化的好处。没有-flto,它工作得很好。使用-flto时,它无法链接。调用ascla

我是一个新来的叮当声,所以很可能我在做一些愚蠢的事情。但我已经花了几个小时寻找解决方案,包括在这里搜索,在这里我没有找到问题解决方法——使用发行版提供的软件包解决flto。这个描述的细节是针对Fedora18的,但是我在Ubuntu13.04上遇到了类似的问题,所以这个问题不是针对Fedora的。不是我就是叮当

问题:我正在尝试使用
clang++-flto
编译一个简单的hello-world程序,以获得链接时间优化的好处。没有-flto,它工作得很好。使用-flto时,它无法链接。调用as
clang-flto-o hello.o-v
查看完整的链接器命令行,我得到:

$ clang++ -flto -o hello hello.o -v
clang version 3.2 (tags/RELEASE_32/final)
Target: x86_64-redhat-linux-gnu
Thread model: posix
 "/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o hello /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtbegin.o -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../.. -L/lib -L/usr/lib -plugin /usr/bin/../lib/LLVMgold.so hello.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/lib/gcc/x86_64-redhat-linux/4.7.2/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.7.2/../../../../lib64/crtn.o
/usr/bin/ld: /usr/bin/../lib/LLVMgold.so: error loading plugin
/usr/bin/ld: /usr/bin/../lib/LLVMgold.so: error in plugin cleanup (ignored)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
似乎有两个问题:

  • clang++以
    /usr/bin/ld
    的形式调用链接器,而这不是黄金链接器。Fedora18将黄金安装为
    /usr/bin/ld.gold
    。我尝试创建一个从
    /usr/local/bin/ld
    /usr/bin/ld.gold
    的符号链接,验证了
    哪个ld
    表示
    /usr/local/bin/ld
    ,但clang++没有使用它。它似乎是硬连接到/usr/bin/ld的

  • clang++使用
    -plugin/usr/bin/./lib/LLVMgold.so
    调用链接器。这是错误的,因为clang的Fedora发行版将其置于
    /usr/lib64/llvm/LLVMgold.so

  • 我尝试通过以下调整手动调用上面的链接器行:

    • -plugin/usr/bin/./lib/LLVMgold.so
      替换为
      -plugin/usr/lib64/llvm/LLVMgold.so
      。这将产生错误消息
      hello.o:file not recognized:file format not recognized
      。因此,非黄金链接器似乎知道插件,但不会接受包含LLVM位代码的.o

    • /usr/bin/ld
      替换为
      /usr/bin/ld.gold
      。这样做可以生成一个按预期运行的可执行文件

    • 以上两种方法都使用
      -plugin
      而不是
      -plugin
      。这一变化没有什么区别

    那么,对于那些喜欢使用系统提供的包来使用clang-flto的人来说,什么是最好的方法呢?我希望有一个配置文件,或未记录的选项或环境变量,可以让我覆盖这些。或者更好的是,我缺少一个包,而“yum安装…”将修复它

    我不希望直接调用链接器,因为我的makefile需要知道它们应该不知道的系统对象和库(例如crt1.o、crtbegin.o、crtend.o)。我也可以自己构建clang,但是我在它的配置脚本中没有看到任何东西可以让我配置链接器和插件的路径

    我要戴18号软呢帽。计算机上唯一的非发行版软件包是google chrome和VMware Tools(它是VMware Fusion中的客户)。相关Fedora软件包的版本(截至今天,2013年4月29日,整个计算机已“百胜更新”):


    Fedora中有一个实用工具
    alternatives
    ,它允许在系统级用另一个链接器替换一个链接器:

    $ sudo alternatives --display ld
    ld - status is auto.
    link currently points to /usr/bin/ld.bfd
    /usr/bin/ld.bfd - priority 50
    /usr/bin/ld.gold - priority 30
    Current `best' version is /usr/bin/ld.bfd.
    $ sudo alternatives --set ld /usr/bin/ld.gold
    
    关于LLVMgold.so位置您只能在Fedora Bugzilla中报告一个bug,因为路径是内置于clang sources中的:

    lib/Driver/Tools.cpp:std::string Plugin=ToolChain.getDriver().Dir+“/../lib/LLVMgold.so”

    Fedora的家伙可能会对Clang的源程序包应用补丁,或者创建一个指向LLVMgold.so的符号链接。
    即使在Fedora 20中也没有变化。

    正如您所看到的,只是为了节省一些时间,它正在查看
    lib
    ,而不是
    lib64
    。对我有效的符号链接是
    sudo ln-s/usr/lib64/llvm/LLVMgold.so/usr/lib/LLVMgold.so
    $ sudo alternatives --display ld
    ld - status is auto.
    link currently points to /usr/bin/ld.bfd
    /usr/bin/ld.bfd - priority 50
    /usr/bin/ld.gold - priority 30
    Current `best' version is /usr/bin/ld.bfd.
    $ sudo alternatives --set ld /usr/bin/ld.gold