2

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:

  1. How can I make the above work?
  2. 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)?

I also found this article here highlighting a similar issue but without a solution.

Anastasios Andronidis
  • 6,310
  • 4
  • 30
  • 53
  • You can link the static version of glibc to static executables only. See [this](https://stackoverflow.com/questions/53086636/linking-a-static-library-into-a-shared-library-without-fpic). Also read [this](https://stackoverflow.com/questions/57476533/why-is-statically-linking-glibc-discouraged). Whatever your problem is, static linking is probably not a solution. – n. m. could be an AI Jul 13 '20 at 15:15

1 Answers1

2

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.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • Thanks! Could you please elaborate a little bit further on what issue I might have with the TLS? Also, is there an easy way for to install `libc` with `-fPIC`? Is there a ready-made package I could install with `apt`? – Anastasios Andronidis Jul 23 '20 at 10:56