llvm-gcc with static libraries

Hello,

While linking programs against static libraries built using llvm-ar and llvm-ranlib, I see an error message:

could not read symbols: Archive has no index; run ranlib to add one
collect2: ld returned 1 exit status

Using either /usr/bin/nm or llvm-nm (depending on the use of --emit-llvm flag to llvm-gcc), one of them is able to list the symbols in the archive file. However, the link step fails. Two sample runs follow. Am I missing any options? The source code was obtained from: http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

== Without --emit-llvm ==

$ llvm-gcc -c ctest1.c ctest2.c

$ file *.o
ctest1.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped
ctest2.o: ELF 64-bit LSB relocatable, x86-64, version 1 (SYSV), not stripped

$ llvm-ar cru libctest.a *.o

$ ls
ctest1.c ctest1.o ctest2.c ctest2.o libctest.a prog.c

$ llvm-ar t libctest.a
ctest1.o
ctest2.o

$ llvm-nm libctest.a

$ nm libctest.a
nm: #LLVM_SYM_TAB: File format not recognized

ctest1.o:
0000000000000000 T ctest1

ctest2.o:
0000000000000000 T ctest2

$ llvm-ranlib libctest.a

$ llvm-gcc prog.c libctest.a -o p
libctest.a: could not read symbols: Archive has no index; run ranlib to add one
collect2: ld returned 1 exit status

== With --emit-llvm ==

$ llvm-gcc --emit-llvm -c ctest1.c ctest2.c

$ file *.o
ctest1.o: LLVM bitcode
ctest2.o: LLVM bitcode

$ llvm-ar cru libctest.a *.o

$ ls
ctest1.c ctest1.o ctest2.c ctest2.o libctest.a prog.c

$ llvm-ar t libctest.a
ctest1.o
ctest2.o

$ llvm-nm libctest.a

libctest.a(ctest1.o):
T ctest1

libctest.a(ctest2.o):
T ctest2

$ nm libctest.a
nm: #LLVM_SYM_TAB: File format not recognized
nm: ctest1.o: File format not recognized
nm: ctest2.o: File format not recognized

$ llvm-ranlib libctest.a

$ llvm-gcc prog.c libctest.a -o p
libctest.a: could not read symbols: Archive has no index; run ranlib to add one
collect2: ld returned 1 exit status

http://llvm.org/docs/GoldPlugin.html . (I'm not completely sure
that's up-to-date, but should point you in the right direction.)

-Eli

`/usr/bin/ld -v’ says it is not running gold and I suppose fixing that would require place LLVMgold.so into /usr/lib/bfd-plugins. Is there any way to make this work without requiring root access to the machine?

Also, just to confirm, is the gold plugin required even when all of the files have been compiled/built using llvm-gcc and llvm-ar?

Ashay

`/usr/bin/ld -v' says it is not running gold and I suppose fixing that would
require place LLVMgold.so into /usr/lib/bfd-plugins. Is there any way to
make this work without requiring root access to the machine?

If you install your own version of binutils into the same prefix as llvm-gcc,
it should get used instead of the one in /usr/bin/.

Also, just to confirm, is the gold plugin required even when all of the
files have been compiled/built using llvm-gcc and llvm-ar?

You can try llvm-ld... it isn't a fully general linker, but it might
be good enough for your purposes.

-Eli

Hi,

Thanks for the pointers. I built binutils and installed them in the same place as llvm-gcc. However, I see the following error at the link step:

$ llvm-gcc prog.c libctest.a
collect2: ld terminated with signal 6 [Aborted]
/tmp/llvm-gcc4.2-2.9-x86_64-linux/bin/…/lib/gcc/x86_64-unknown-linux-gnu/4.2.1/…/…/…/…/x86_64-unknown-linux-gnu/bin/ld: error: libctest.a: malformed archive header name at 8
terminate called after throwing an instance of ‘std::length_error’
what(): basic_string::assign

I have copied over ld.gold to ld:
$ diff ld.gold ld
$

If I use -use-gold-plugin, llvm-gcc says:
llvm-gcc: -use-gold-plugin, but LLVMgold.so not found.

I created the following shell script (called it `ld’) and placed it into the same directory as llvm-gcc:
$ cat ld

#!/bin/sh

/tmp/llvm-gcc4.2-2.9-x86_64-linux/bin/ld.gold -plugin ${HOME}/apps/llvm/LLVMgold.so $*

$

But the output from llvm-gcc does not change. Any clue whats wrong?

There is no /usr/lib/bfd-plugins nor is there a lib/bfd-plugins in the location where the new binutils was installed. Where is this file supposed to be copied?

Ashay

Has libctest.a been created by llvm-ar? If I remember correctly, llvm-ar uses a different index format that only llvm-ld understands :frowning:

You should use the regular bfd ar, just make sure it is using the plugin too.

Cheers,
Rafael