Search code examples
clinuxelfreadelf

what is the "Value" field in output of readelf -s


Here is my code:

#include <stdio.h>
int variable;
int main(){
    printf("%p", &variable);
}

Output in couple of runs:

~ % ./a.out
0x559bae5c4030
 ~ % ./a.out
0x55b9d1038030
 ~ % 

as you can see, there's a "30" at the end of both addresses. and the symbol table:

 ~ % readelf -s a.out | grep variable
   Num:    Value          Size Type    Bind   Vis      Ndx Name
    51: 0000000000004030     4 OBJECT  GLOBAL DEFAULT   23 variable
 ~ % 

again there's this "30" at the end of Value field. My question is, what exactly is that value field and what does it have to do with the output of code? and why the last two digits are preserved in every run?

sorry for my poor english


Solution

  • The Value field from readelf corresponds to the address of the variable in the executable a.out.

    What you see in the output is the actual loaded address of variable at runtime. So your executable is loaded at (starting address) 0x559bae5c0000 in the first run ( = 0x559bae5c4030 - 0x4030). And is loaded at 0x55b9d1034000 in the second run (0x55b9d1038030 - 0x4030).

    You can see this by inspecting /proc/<PID>/maps of the executable a.out when running.

    The load address changes from run to run because of Address Space Layout Randomization on Linux.