Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.
7929 Discussions

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();
~$ cat lib.cpp
#include <iostream>
#include "lib.h"
void foo() {
    std::cout << "Hello, world!" << std::endl;
~$ cat main.cpp
#include "lib.h"
int main() {
    return 0;
~$ icpc -fPIC -c lib.cpp
~$ icpc -fuse-ld=lld -shared -o 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/
>>> referenced by lib.cpp
>>>               lib.o:(.gnu.linkonce.d.DW.ref.__gxx_personality_v0+0x0)
~$ icpc -fuse-ld=lld -shared -o 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/
(gdb) bt
#0  0x00007f2ffdbe7526 in std::ostream::sentry::sentry(std::ostream&) () from /usr/lib/x86_64-linux-gnu/
#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/
#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/
#3  0x00007f2ffe07f0f4 in foo() () from ./
#4  0x0000000000400ddf in main ()
~$ g++ -c -fPIC lib.cpp
~$ icpc -fuse-ld=lld -shared -o 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 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

I don't icpc supports LLVM linker though.

0 Kudos