Richard
Richard

Reputation: 15602

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. 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). But the print out is sth like below; the address diff is not constant?

$ ./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. Is it normal that .so object is always loaded to a fixed position/section in the main program's address space?

Upvotes: 2

Views: 128

Answers (1)

Frère Chloé
Frère Chloé

Reputation: 101

Addresses of functions and variables contained in dynamic loaded libraries are randomized to avoid security breaches.

Upvotes: 3

Related Questions