0

I thought that only linker does the symbol relocation so that once we get an Executable Object File (a.out), the symbol's logical address won't change. However, it seems that it is not the case based on my experiment:

I wrote a simple code:

#include <stdio.h>                                                              
                                                                                
int main(void) {                                                                
    printf("address of main: %p\n", &main);                                     
}  

And compiled it to an executable a.out.

Every time I ran the same executable, I got a different result, here are some samples:

address of main: 0x55bb9c80064a
address of main: 0x55940760064a

So I'm wondering if it means that the loader will do another symbol relocation when loading the program into the memory?

My environment: Ubuntu 18.04.5 LTS + gcc.

Changda Li
  • 123
  • 1
  • 8
  • Oh, just found a similar question: https://stackoverflow.com/questions/2846637/why-address-of-a-variable-change-after-each-execution-in-c#:~:text=4%20Answers&text=It%20signifies%20that%20your%20program,of%20most%20modern%20operating%20systems.&text=That's%20how%20operating%20systems%20work. – Changda Li Nov 30 '20 at 06:27
  • And this one: https://stackoverflow.com/questions/59683246/why-is-the-address-of-a-function-different-on-each-execution – Changda Li Nov 30 '20 at 06:40

0 Answers0