Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
55 Views

LLD cannot output functional shared object with object file generated by ICPC

Here is the sample:

~$ cat lib.h
#ifndef LIB_H_
#define LIB_H_
void foo();
#endif
~$ cat lib.cpp
#include <iostream>
#include "lib.h"
void foo() {
    std::cout << "Hello, world!" << std::endl;
}
~$ cat main.cpp
#include "lib.h"
int main() {
    foo();
    return 0;
}
~$ icpc -fPIC -c lib.cpp
~$ icpc -fuse-ld=lld -shared -o libmylib.so lib.o
ld.lld: error: can't create dynamic relocation R_X86_64_64 against symbol: __gxx_personality_v0 in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
>>> defined in /usr/lib/gcc/x86_64-linux-gnu/7/libstdc++.so
>>> referenced by lib.cpp
>>>               lib.o:(.gnu.linkonce.d.DW.ref.__gxx_personality_v0+0x0)
~$ icpc -fuse-ld=lld -shared -o libmylib.so lib.o -Wl,-z,notext
~$ icpc main.o -L . -lmylib
~$ ./a.out 
Segmentation fault (core dumped)
~$ gdb a.out core
[New LWP 50478]
Core was generated by `./a.out'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x00007f2ffdbe7526 in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007f2ffdbe7526 in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00007f2ffdbe7ba8 in std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007f2ffdbe8027 in std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007f2ffe07f0f4 in foo() () from ./libmylib.so
#4  0x0000000000400ddf in main ()
~$ g++ -c -fPIC lib.cpp
~$ icpc -fuse-ld=lld -shared -o libmylib.so lib.o
~$ ./a.out
Hello, world!
~$

LLD does not link the object file produced by ICPC, even I just compiled the object file with -fPIC option present. If I add -Wl,-z-notext option to linker as prompted by LLD, it links, but the generated libmylib.so library does not work and segfaults in libstdc++. But if I compile the object file with G++ and all other things remain the same, it works.

0 Kudos
1 Reply
Highlighted
Moderator
55 Views

I don't icpc supports LLVM linker though.

0 Kudos