Reputation: 6730
I'm trying to create a share object which has its own dependencies statically linked.
Here is an example:
/* File: add.c */
#include <stdio.h>
int add(int a, int b) {
printf("Adding: %d %d\n", a, b);
return a + b;
}
When I try to compile I get the following error:
$ gcc -g -shared -fPIC -o libadd.so add.c -Wl,-Bstatic -lc -Wl,-Bdynamic
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libc.a(malloc.o): relocation R_X86_64_TPOFF32 against `tcache' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libc.a(libc_fatal.o): relocation R_X86_64_PC32 against symbol `_dl_pagesize' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
Running on Ubuntu 18.04.4 LTS
with gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
and GNU ld (GNU Binutils for Ubuntu) 2.30
.
My questions:
libc
s run simultaneously (one statically linked in my shared object and one from a hypothetical executable that will link against my shared object and libc)?I also found this article here highlighting a similar issue but without a solution.
Upvotes: 2
Views: 441
Reputation: 213496
How can I make the above work?
Normal build of libc.a
does not have -fPIC
. On i386
, you can actually link non-PIC object files into a .so
, but the result would be an .so
which requires text relocations (.text
of such .so
can't be shared between processes).
But on x86_64
you can't (relocations only have 4 bytes in the medium memory model, and you would need to relocate .text
by more than +/-2GiB).
So to make this work, you must rebuild libc.a
with -fPIC
.
Would it be an issue if 2 libcs run simultaneously (one statically linked in my shared object and one from a hypothetical executable that will link against my shared object and libc)?
Most things will work, but TLS may cause complications.
Upvotes: 2