'Why shared library linked with static library behaves like it was linked with dynamic one?
It is unclear to me why final shared .so library calls functions from the static .a library as:
callq 1050 <add@plt>
I expect to see there something like:
callq 115b <add>
Let me explain by example.
Suppose we have following files.
- static_library.c
int add(int a, int b)
{
return a+b;
}
- shared_library.c
extern int add(int a, int b);
int use_add_from_shared_library_1(int a, int b)
{
return add(a, b);
}
int main() { return 0; }
Lets compile static library (for the convenience):
CC=gcc;
${CC} -c -fPIC static_library.c -o static_library.o;
ar rcs libstatic_library.a static_library.o;
Now lets compile shared library and link to it previously compiled static library:
CC=gcc;
${CC} -c -fPIC shared_library.c -o shared_library.o;
${CC} -shared shared_library.o -L./ -lstatic_library -o shared_library.so;
There will be no errors. However, "objdump" of the shared library will contain the disassembler code:
$ objdump -dS shared_library.so | grep "callq" | grep "add";
1135: e8 16 ff ff ff callq 1050 <add@plt>
P.S. Single-shell example:
echo '
int add(int a, int b)
{
return a+b;
}
' \
> static_library.c; \
\
echo '
extern int add(int a, int b);
int use_add_from_shared_library_1(int a, int b)
{
return add(a, b);
}
int main() { return 0; }
' \
> shared_library.c; \
\
CC=gcc; \
\
${CC} -c -fPIC static_library.c -o static_library.o; \
ar rcs libstatic_library.a static_library.o; \
\
${CC} -c -fPIC shared_library.c -o shared_library.o; \
${CC} -shared shared_library.o -L./ -lstatic_library -o shared_library.so; \
\
objdump -dS shared_library.so | grep "callq" | grep "add";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
