When same library is linked and used with dlopen, same function (sqrt in this example) has different memory addresses. Can you please explain why it is so? Is there some indirection?
# cat dl-test.c
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <math.h>
#include <inttypes.h>
int main()
{
void *dl, *dl_sqrt;
dl = dlopen("/lib/x86_64-linux-gnu/libm.so.6", RTLD_LAZY);
if (!dl) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
dl_sqrt = dlsym(dl,"sqrt");
if (!dl_sqrt) {
fprintf(stderr, "%s\n", dlerror());
exit(1);
}
printf("Address of sqrt %p\n", (void*) sqrt);
printf("Address of (dl)sqrt %p\n", (void*) dl_sqrt);
return 0;
}
#
# gcc dl-test.c -lm -ldl -o dl-test
# ldd dl-test
linux-vdso.so.1 => (0x00007fff9132b000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4caee90000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4caec8c000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4cae8c1000)
/lib64/ld-linux-x86-64.so.2 (0x0000559a02daa000)
# ./dl-test
Address of sqrt 0x4006d0
Address of (dl)sqrt 0x7fa7ce6f7250
#