Goal
I want to create a shared library libsquare.so
which is statically linked to libmul.a
which itself is statically linked to libadd.a
Situation
I create libmul.a
by linking libadd.a
to it.
Then I create libsquare.so
by linking in the just created libmul.a
.
libmul.a
has the code for the add
-function.
Problem
When I link it to into a shared object, and looking into the symbol table, the add
-symbol is still there, but undefined (*UND*
), rather than in the .text
section.
I have a MWE here on Github demonstrating it.
Here a part of the objdump -t libsquare.so
libsquare.so: file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 *UND* 0000000000000000 add
0000000000001119 g F .text 000000000000001c square
0000000000001135 g F .text 000000000000003b mul
... // some symbols omitted here... See repo for entire table
Here the entire objdump -t libmul.a
mul.o: file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 mul.c
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 g F .text 000000000000003b mul
0000000000000000 *UND* 0000000000000000 _GLOBAL_OFFSET_TABLE_
0000000000000000 *UND* 0000000000000000 add
In nested archive libadd.a:
add.o: file format elf64-x86-64
SYMBOL TABLE:
0000000000000000 l df *ABS* 0000000000000000 add.c
0000000000000000 l d .text 0000000000000000 .text
0000000000000000 g F .text 0000000000000014 add
In essence the commands run are:
cc -fPIC -rdynamic -o add.o -c add.c
cc -fPIC -rdynamic -o mul.o -c mul.c
cc -fPIC -rdynamic -o square.o -c square.c
ar rcs libadd.a add.o
ranlib libadd.a
ar rcs libmul.a mul.o libadd.a
ranlib libmul.a
gcc -shared -Wl,-soname=square -o libsquare.so square.o libmul.a
cc -fPIC -rdynamic -o test-w-lib.o -c test-w-lib.c
cc -o test-w-lib libsquare.so test-w-lib.o -ldl
/usr/bin/ld: libsquare.so: undefined reference to `add'
collect2: error: ld returned 1 exit status