llvm+clang 添加 LTO(Link Time Optimization) 支持
binutils
LTO 要求当前系统binutils
可使用 gold linker
或者ld.bfd
通过 plugin
支持 LTO, binutils
版本 >= 2.21.511.0.2
. 可通过ld -plugin
检查当前系统是否支持 plugin
.
RHEL/CentOS6
系统自带 binutils
不支持 plugin
.
$ ld -v
GNU ld version 2.20.51.0.2-5.47.el6_9.1 20100205
$ ld -plugin
ld: unrecognized option '-plugin'
ld: use the --help option for usage information
安装 devtoolset-7
后可以支持.
$ ld -v
GNU ld version 2.28-8.el6
$ ld -plugin
ld: -plugin: missing argument
ld: use the --help option for usage information
亦可通过重新编译binutils
安装到系统提供支持
$ git clone --depth 1 git://sourceware.org/git/binutils-gdb.git binutils
$ mkdir build
$ cd build
$ ../binutils/configure --disable-werror # ld.bfd includes plugin support by default
$ make all-ld
LLVM-CLANG
Full compile llvm-clang
重新配置 llvm-clang
运行 cmake
添加 -DLLVM_BINUTILS_INCDIR=/path/to/binutils/include
指定包含 plugin-api.h
目录参数 , make install/strip
即可.
LLVMgold plugin
安装 binutils
到 $PREFIX
目录.
cmake ... -DLLVM_BINUTILS_INCDIR=/path/to/binutils/include ../llvm_Release_500/
make -C tools/gold/
复制 /lib/LLVMgold.so
到 $PREFIX/lib/bfd-plugins/
Usage
clang -flto xxx.c -o xxx
autotool 工具链配置使用.
export CC="$PREFIX/bin/clang -flto"
export CXX="$PREFIX/bin/clang++ -flto"
export AR="$PREFIX/bin/ar"
export NM="$PREFIX/bin/nm"
export RANLIB=/bin/true #ranlib is not needed, and doesn't support .bc files in .a