了解动态加载库中的地址

understanding of addresses in dynamically loaded library

我在foo.so中定义了一个foo2函数,当我动态加载到主程序中时,我试图了解基地址和函数foo的地址。代码如下:

void (* foo2)(void) = (void (*)(void))dlsym(loaded_so_handle, "foo2");
Dl_info info;
dladdr(&foo2, &info);
void * baseaddr = info.dli_fbase;
printf("base:%p, foo:%p, diff: %p\n", baseaddr, foo2,  (long)foo2 - (long)baseaddr);

我希望 diff 打印输出也应该是不变的(给定一个固定的共享对象 .so)。但是打印出来的东西如下;地址 diff 不是常量?

$ ./a.out
base: 0x238c660, foo:0x2af0350ad860, diff: 0x2af032d21200
$ ./a.out
base: 0x1de4660, foo:0x2ac564cd7860, diff: 0x2ac562ef3200

更新:

关闭ASLR后,diff是静态的,也是base地址。 .so对象总是加载到主程序地址space中固定的position/section是否正常?

动态加载库中包含的函数和变量的地址是随机的,以避免安全漏洞。