title: understanding of addresses in dynamically loaded library
body: <p>I have a <code>foo2</code> function defined in <code>foo.so</code>, when I dynamic loaded into the main program, I try to understand the base address and function <code>foo</code>'s address. The code is the following:</p>
  
  <pre><code>void (* foo2)(void) = (void (*)(void))dlsym(loaded_so_handle, "foo2");
  Dl_info info;
  dladdr(&amp;foo2, &amp;info);
  void * baseaddr = info.dli_fbase;
  printf("base:%p, foo:%p, diff: %p\n", baseaddr, foo2,  (long)foo2 - (long)baseaddr);
  </code></pre>
  
  <p>I expect the <code>diff</code> printout should also be constant (given a fixed shared object <code>.so</code>). But the print out is sth like below; the address <code>diff</code> is not constant?</p>
  
  <pre><code>$ ./a.out
  base: 0x238c660, foo:0x2af0350ad860, diff: 0x2af032d21200
  $ ./a.out
  base: 0x1de4660, foo:0x2ac564cd7860, diff: 0x2ac562ef3200
  </code></pre>
  
  <p>Updates:</p>
  
  <p>After turning off ASLR, the diff is static, but also is the <code>base</code> address. Is it normal that .so object is always loaded to a fixed position/section in the main program's address space?</p>