I have a simple HelloWorld.c program (so, I'm put putting it here), that I am trying to build with clang version 3.3 with LTO enabled that is throwing strange errors. I am building it with cmake
using the -DCMAKE_TOOLCHAIN_FILE
. I can successfully build and link this program without -flto
, but I need to be able to use this qualifier. The toolchain is for TileGX (hence the need to stay at clang version 3.3). So here is my environment:
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)
Here is my CMakeLists.txt
file:
cmake_minimum_required (VERSION 3.9)
project (HelloWorld)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -v -flto -static")
add_executable(HelloWorld HelloWorld.c)
I added the -v
flag so see what was happening in clang.
The output from clang trying to link is (reformatted for easier reading):
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
At first I thought it was an issue of using a shareable object (LLVMgold.so
) built on Ubuntu 16.04 with a linker built on 14.04. The linker would call the shareable object, which would then call back into the linker. But, now that I have a linker built on 16.04, I'm still getting an error.
Since I build LLVMgold.so
I put some code to track what was going on when the linker called the shareable object, but everything there looks fine. I can see that ld
cannot find an oddly named file, but I'm not sure which file it may be in the set of them being provided.
I have tried everything I can imagine so that I can use -flto
in this environment. Has anyone seen this before? Is there a solution to resolve it? Are there other thing I can or should be doing?
OK, this is a case of a class having a pointer to a string, which it copies to a local variable. When the class is destroyed, the pointer also goes away, so the local variable is now effectively pointing to random memory. The fix was to duplicate the string into the local variable and then remember to free that up when I am done with it.
For anyone who cares, calling strdup in the local variable into itself is sufficient. The only code change required is in gold-plugin.cpp
.