带有 TileGX 工具链和 LTO 的 LLVM 3.3 不工作

LLVM 3.3 with a TileGX toolchain and LTO not working

我有一个简单的 HelloWorld.c 程序(所以,我把它放在这里),我正在尝试使用启用了 LTO 的 clang 版本 3.3 构建它,但会抛出奇怪的错误。我正在使用 -DCMAKE_TOOLCHAIN_FILE 使用 cmake 构建它。我可以在没有 -flto 的情况下成功构建和 link 这个程序,但我需要能够使用这个限定符。该工具链适用于 TileGX(因此需要保留 clang 版本 3.3)。所以这是我的环境:

Ubuntu 16.04
cmake Version 3.9.4
I built the TileGX toolchain on 16.04, as well as 14.04
I build clang Version 3.3 on 16.04 (can't build it on 14.04)

这是我的 CMakeLists.txt 文件:

cmake_minimum_required (VERSION 3.9)
project (HelloWorld)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -v -flto -static")

add_executable(HelloWorld HelloWorld.c)

我添加了 -v 标志,看看 clang 中发生了什么。

clang 尝试 link 的输出是(重新格式化以便于阅读):

clang version 3.3 
Target: tilegx-unknown-linux-gnu
Thread model: posix
./toolchains/univ_tilegx/usr/bin/tilegx-unknown-linux-gnu-ld -m elf64tilegx \
  -static -o HelloWorld \
  ./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/sys-root/usr/lib/crt1.o \
  ./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/sys-root/usr/lib/crti.o \
  ./toolchains/univ_tilegx/usr/lib/gcc/tilegx-unknown-linux-gnu/4.9.2/crtbeginT.o \
  -L./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/lib \
  -L./third_party/toolchains/univ_tilegx/usr/lib/gcc/tilegx-unknown-linux-gnu/4.9.2/ \
  -L./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/lib \
  -L./toolchains/univ_tilegx/usr/lib/gcc/tilegx-unknown-linux-gnu/4.9.2/ \
  -L/lib/../lib64 -L/lib -L/usr/lib -plugin ../lib/LLVMgold.so \
    CMakeFiles/HelloWorld.dir/HelloWorld.c.o \
  --start-group \
    -lgcc -lgcc_eh -lc 
  --end-group \
  ./toolchains/univ_tilegx/usr/lib/gcc/tilegx-unknown-linux-gnu/4.9.2/crtend.o \
  ./toolchains/univ_tilegx/usr/tilegx-unknown-linux-gnu/sys-root/usr/lib/crtn.o
./toolchains/univ_tilegx/usr/bin/tilegx-unknown-linux-gnu-ld: cannot find 0�: \
  No such file or directory
./toolchains/univ_tilegx/usr/bin/tilegx-unknown-linux-gnu-ld: error: 
  Failed to delete '0�': 0�: can't get status of file: No such file or directory
clang-3.3: error: linker command failed with exit code 1 (use -v to see invocation)
CMakeFiles/HelloWorld.dir/build.make:94: recipe for target 'HelloWorld' failed
make[2]: *** [HelloWorld] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/HelloWorld.dir/all' failed
make[1]: *** [CMakeFiles/HelloWorld.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

起初我认为这是使用基于 Ubuntu 16.04 构建的可共享对象 (LLVMgold.so) 和基于 14.04 构建的 linker 的问题。 linker 将调用可共享对象,然后该对象将回调 linker。但是,现在我有一个基于 16.04 构建的 linker,我仍然遇到错误。

自从我构建 LLVMgold.so 以来,我放置了一些代码来跟踪 link 用户调用可共享对象时发生的情况,但那里的一切看起来都很好。我可以看到 ld 找不到名称奇怪的文件,但我不确定它可能在所提供的文件集中是哪个文件。

为了能够在这个环境下使用-flto,我已经尝试了所有我能想到的。谁看过这个吗?有解决办法吗?还有其他我可以或应该做的事吗?

好的,这是一个 class 的例子,它有一个指向字符串的指针,它复制到一个局部变量。当 class 被销毁时,指针也消失了,所以局部变量现在有效地指向随机内存。解决方法是将字符串复制到局部变量中,然后记得在完成后释放它。

对于任何关心的人,在局部变量中调用strdup into itself就足够了。唯一需要更改的代码是 gold-plugin.cpp.