将自动化项目编译为LLVM Bitcode时出现链接器错误

时间:2015-12-18 15:32:18

标签: makefile linker llvm gnu-coreutils gold-linker

我按照http://gbalats.github.io/2015/12/10/compiling-autotooled-projects-to-LLVM-bitcode.html#compiling-llvm-from-source-with-gold-plugin上的说明尝试编译各种项目,包括coreutils到LLVM IR。

我尝试使用Clang 3.4测试我在Ubuntu 14.04上的安装以及一个带有

的简单测试文件
clang -flto -fuse-ld=gold -Wl,-plugin-opt=also-emit-llvm test.o
如上所述

失败了
test.o: file not recognized: File format not recognized
clang: error: linker command failed with exit code 1 (use -v to see invocation)

但是,添加参数-B/usr/lib/gold-ld后,我可以编译测试文件。

仍然,编译coreutils失败并出现以下错误:

/usr/lib/gold-ld/ld: error: src/libver.a: no archive symbol table (run ranlib)
/usr/lib/gold-ld/ld: error: lib/libcoreutils.a: no archive symbol table (run ranlib)
/usr/lib/gold-ld/ld: error: lib/libcoreutils.a: no archive symbol table (run ranlib)
/tmp/lto-llvm-dc31af.o:ld-temp.o:function usage: error: undefined reference to 'program_name'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function usage: error: undefined reference to 'quote'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function usage: error: undefined reference to 'program_name'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'set_program_name'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'exit_failure'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'close_stdout'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'parse_user_spec'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'xgetgroups'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'quote'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'parse_user_spec'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'xgetgroups'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'quote'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'Version'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'version_etc'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function main: error: undefined reference to 'quote'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function parse_additional_groups: error: undefined reference to 'xstrdup'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function parse_additional_groups: error: undefined reference to 'xstrtoul'
/tmp/lto-llvm-dc31af.o:ld-temp.o:function parse_additional_groups: error: undefined reference to 'x2nrealloc'

执行的最后一个命令是:

clang -flto -B/usr/lib/gold-ld  -Wl,--as-needed -flto  -B/usr/lib/gold-ld -fuse-ld=gold -Wl,-plugin-opt=also-emit-llvm  -o src/chroot src/chroot.o src/libver.a lib/libcoreutils.a  lib/libcoreutils.a 

在libver.a和libcoreutils.a上手动执行llvm-ranlib不会改变结果。执行llvm-nm -s src/libver.a会打印以下内容:

 D Version

但是,对于正常构建,输出如下:

Archive map
Version in version.o

version.o:
00000000 D Version

llvm-ranlib有问题吗?您对如何修复链接器错误有任何建议吗?

1 个答案:

答案 0 :(得分:0)

您尝试过onlu ranlib吗?您需要llvm-ar(设置(CMAKE_AR llvm-ar)),可能还需要llvm-nm。 llvm -ar代替ar对我来说效果很好。