clang -O4 -use-gold-plugin fails (LLVMgold.so: undefined symbol: lto_codegen_compile_to_file)

Bug #1026359 reported by Anders Kaseorg
22
This bug affects 4 people
Affects Status Importance Assigned to Milestone
LLVM
Fix Released
Wishlist
llvm-3.0 (Ubuntu)
Fix Released
Undecided
Unassigned
llvm-3.1 (Ubuntu)
Fix Released
Undecided
Unassigned

Bug Description

$ echo 'int main(){return 0;}' > foo.c
$ clang -O4 -use-gold-plugin foo.c
/usr/bin/ld.gold.real: error: /usr/bin/../lib/LLVMgold.so: could not load plugin library: /usr/bin/../lib/LLVMgold.so: undefined symbol: lto_codegen_compile_to_file
/usr/bin/ld.gold.real: error: /tmp/foo-KzG9X9.o:1:3: invalid character
/usr/bin/ld.gold.real: error: /tmp/foo-KzG9X9.o:1:3: syntax error, unexpected $end
/usr/bin/ld.gold.real: error: /tmp/foo-KzG9X9.o: not an object or archive
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o(.text+0x20): error: undefined reference to 'main'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$ ld.gold -v -plugin /usr/lib/llvm-3.0/lib/LLVMgold.so
GNU gold (GNU Binutils for Ubuntu 2.22.52.20120713) 1.11
/usr/bin/ld.gold.real: error: /usr/lib/llvm-3.0/lib/LLVMgold.so: could not load plugin library: /usr/lib/llvm-3.0/lib/LLVMgold.so: undefined symbol: lto_codegen_compile_to_file

It seems that LLVMgold.so is missing a dependency on libLTO.so. Things work if I set that as an LD_PRELOAD.

ProblemType: Bug
DistroRelease: Ubuntu 12.10
Package: llvm-3.0-dev 3.0-9
ProcVersionSignature: Ubuntu 3.5.0-4.4-generic 3.5.0-rc6
Uname: Linux 3.5.0-4-generic x86_64
NonfreeKernelModules: openafs nvidia
ApportVersion: 2.4-0ubuntu2
Architecture: amd64
Date: Wed Jul 18 18:20:18 2012
EcryptfsInUse: Yes
InstallationMedia: Ubuntu 11.04 "Natty Narwhal" - Alpha amd64 (20101202)
SourcePackage: llvm-3.0
UpgradeStatus: No upgrade log present (probably fresh install)

Revision history for this message
In , Felix Geyer (debfx) wrote :

Created attachment 8821
Patch to pass LTO flags to LDFLAGS and LIBS

LLVMgold.so is not linked against libLTO.so when ld defaults to --as-needed (which is the default on Ubuntu and afaik Fedora).

LLVMgold is built like this:
x86_64-linux-gnu-g++ [...] -lLTO [...] -o /build/buildd/llvm-3.1-3.1/build-llvm/Release/lib/LLVMgold.so /build/buildd/llvm-3.1-3.1/build-llvm/tools/gold/Release/gold-plugin.o [...] -lpthread -lffi -ldl -lm

-lLTO is passed before gold-plugin.o so it is ignored.

I'm attaching a patch that adds "-L$(SharedLibDir)/$(SharedPrefix)" to LDFLAGS and "-lLTO" to LIBS instead of appending both to CXXFLAGS.
Makefile.config overwrites LIBS so I've added that at the bottom of the Makefile.
Maybe there is a better solution for this?

With these changes LLVMgold is built like this:
x86_64-linux-gnu-g++ [...] -o /tmp/buildd/llvm-3.1-3.1/build-llvm/Release/lib/LLVMgold.so /tmp/buildd/llvm-3.1-3.1/build-llvm/tools/gold/Release/gold-plugin.o [...] -lpthread -lffi -ldl -lm -lLTO

Revision history for this message
In , Rafael Ávila de Espíndola (rafael-espindola) wrote :

cmake gets this right:

g++ -fPIC -fPIC -fvisibility-inlines-hidden -fno-rtti -O3 -DNDEBUG -shared -Wl,-soname,LLVMgold.so -o lib/LLVMgold.so tools/gold/CMakeFiles/LLVMgold.dir/gold-plugin.cpp.o ... -lpthread lib/libLTO.so ....

Revision history for this message
Anders Kaseorg (andersk) wrote :
Revision history for this message
Anders Kaseorg (andersk) wrote :

This is a difference between Debian’s llvm-3.0-dev binary and Ubuntu’s llvm-3.0-dev binary, even though they are the same version compiled from the same source.

$ diff -u \
    <(objdump -p llvm-3.0-dev_3.0-9_debian/usr/lib/llvm-3.0/lib/LLVMgold.so | grep NEEDED) \
    <(objdump -p llvm-3.0-dev_3.0-9_ubuntu/usr/lib/llvm-3.0/lib/LLVMgold.so | grep NEEDED)
--- /dev/fd/63 2012-07-18 18:35:22.399421588 -0400
+++ /dev/fd/62 2012-07-18 18:35:22.403421684 -0400
@@ -1,9 +1,4 @@
- NEEDED libLTO.so
   NEEDED libLLVM-3.0.so.1
   NEEDED libpthread.so.0
- NEEDED libffi.so.5
- NEEDED libdl.so.2
   NEEDED libstdc++.so.6
- NEEDED libm.so.6
- NEEDED libgcc_s.so.1
   NEEDED libc.so.6

Changed in llvm:
importance: Unknown → Wishlist
status: Unknown → Confirmed
Revision history for this message
In , Wladimir J. van der Laan (laanwj) wrote :

I also noticed this problem with latest SVN build (r162623) on Ubuntu 12.04. LLVMgold.so was not linked to libLTO.so, which caused problems when using it:

    /usr/bin/ld: error: /opt/clang/bin/../lib/LLVMgold.so: could not load plugin library: /opt/clang/bin/../lib/LLVMgold.so: undefined symbol: lto_codegen_add_must_preserve_symbol

Using the above patch solved the problem.

Revision history for this message
In , Rafael Ávila de Espíndola (rafael-espindola) wrote :

Fixed by applying your patch in 162652. Sorry it took so long.

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package llvm-3.0 - 3.0-9ubuntu1

---------------
llvm-3.0 (3.0-9ubuntu1) quantal; urgency=low

  * Link LLVMgold.so against libLTO which was broken due to ld --as-needed.
    (LP: #1026359)
    - Add 1001-link-llvmgold-lto.diff, cherry-picked from upstream.
 -- Felix Geyer <email address hidden> Mon, 27 Aug 2012 19:36:55 +0200

Changed in llvm-3.0 (Ubuntu):
status: New → Fix Released
Revision history for this message
Felix Geyer (debfx) wrote :

Please consider sponsoring the same fix for llvm-3.1.

Revision history for this message
Launchpad Janitor (janitor) wrote :

Status changed to 'Confirmed' because the bug affects multiple users.

Changed in llvm-3.1 (Ubuntu):
status: New → Confirmed
Revision history for this message
Dimitri John Ledkov (xnox) wrote :

Testing to sponsor this.

Revision history for this message
Dimitri John Ledkov (xnox) wrote :

While the patch does indeed links the plugin correctly the original example still does not work in multiple ways.

I have installed clang & llvm-3.0 packages:

$ dpkg-query -W llvm-3.0
llvm-3.0 3.0-9ubuntu1
$ dpkg-query -W clang
clang 3.0-6ubuntu3
$ cat foo.c
int main(){return 0;}
$ clang -O4 -use-gold-plugin foo.c
/usr/bin/ld.bfd.real: /usr/bin/../lib/LLVMgold.so: error loading plugin
/usr/bin/ld.bfd.real: /usr/bin/../lib/LLVMgold.so: error in plugin cleanup (ignored)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$ ld.gold -v -plugin /usr/lib/llvm-3.0/lib/LLVMgold.so
GNU gold (GNU Binutils for Ubuntu 2.22.90.20120816) 1.11
/usr/bin/ld.gold.real: error: /usr/lib/llvm-3.0/lib/LLVMgold.so: could not load plugin library: libLTO.so: cannot open shared object file: No such file or directory

So there is a missing dependency.... on llvm-dev (well current default llvm-3.0-dev)
To me this sounds a bit counter-intuitive, but maybe that's just the way to package llvm.
But why split the packages, if they still depend on each other....?

After installing llvm-dev I get slightly better results:

$ clang -O4 -use-gold-plugin foo.c -o foo
/tmp/foo-nf9h1L.o: file not recognized: File format not recognised
clang: error: linker command failed with exit code 1 (use -v to see invocation)

but...

$ clang -O3 -use-gold-plugin foo.c -o foo && echo $?
0

So is -O4 broken with gold linker?

This fix looks sane and I can test is by inspection with ldd, but can I tell clang to use a different LLVM? Cause clang is currently compiled against llvm-3.0, not llvm-3.1.

Revision history for this message
Dimitri John Ledkov (xnox) wrote :

To build the source package, I had to further modify the source file of the generated control file....

diff -Nru llvm-3.1-3.1/debian/control.in/source llvm-3.1-3.1/debian/control.in/source
--- llvm-3.1-3.1/debian/control.in/source 2012-04-19 19:45:00.000000000 +0100
+++ llvm-3.1-3.1/debian/control.in/source 2012-08-28 15:15:09.000000000 +0100
@@ -1,7 +1,8 @@
 Source: @PKGSOURCE@
 Section: devel
 Priority: optional
-Maintainer: LLVM Packaging Team <email address hidden>
+Maintainer: Ubuntu Developers <email address hidden>
+XSBC-Original-Maintainer: LLVM Packaging Team <email address hidden>
 Uploaders: Arthur Loiret <email address hidden>, Sylvestre Ledru <email address hidden>
 Build-Depends: debhelper (>= 8.1.3), flex, bison, dejagnu, tcl8.5, expect,
     autoconf, automake1.9, perl, libtool, doxygen, chrpath, texinfo, quilt,

Revision history for this message
Launchpad Janitor (janitor) wrote :

This bug was fixed in the package llvm-3.1 - 3.1-2ubuntu1

---------------
llvm-3.1 (3.1-2ubuntu1) quantal; urgency=low

  * Link LLVMgold.so against libLTO which was broken due to ld --as-needed.
    (LP: #1026359)
    - Add 1001-link-llvmgold-lto.diff, cherry-picked from upstream.
 -- Felix Geyer <email address hidden> Mon, 27 Aug 2012 21:17:48 +0200

Changed in llvm-3.1 (Ubuntu):
status: Confirmed → Fix Released
Revision history for this message
Dimitri John Ledkov (xnox) wrote :

I have also filed:
bug 1042822
bug 1042819

Revision history for this message
Felix Geyer (debfx) wrote :

Thanks.
I guess I shouldn't blindly trust update-maintainer. So many ways too preprocess the control file ...

Changed in llvm:
status: Confirmed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.