简体   繁体   English

了解动态加载库中的地址

[英]understanding of addresses in dynamically loaded library

I have a foo2 function defined in foo.so , when I dynamic loaded into the main program, I try to understand the base address and function foo 's address. 我在foo.so定义了一个foo2函数,当我动态加载到主程序中时,我尝试了解基地址和函数foo的地址。 The code is the following: 代码如下:

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);

I expect the diff printout should also be constant (given a fixed shared object .so ). 我希望diff打印输出也应该是恒定的(给定一个固定的共享库.so )。 But the print out is sth like below; 但是打印输出如下: the address diff is not constant? 地址diff不是恒定的?

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

Updates: 更新:

After turning off ASLR, the diff is static, but also is the base address. 关闭ASLR后,差异是静态的,但也是base址。 Is it normal that .so object is always loaded to a fixed position/section in the main program's address space? .so对象总是被加载到主程序地址空间中的固定位置/节段是正常的吗?

动态加载库中包含的函数和变量的地址是随机分配的,以避免违反安全性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM