why does memory address change in Ubuntu and not in Redhat

Linh Phan :

I have this program:

double t;
main() {
}

On Ubuntu, I run:

% gdb a.out
(gdb) p &t
$1 = (double *) 0x4010 <t>
(gdb) run
Starting program: /home/phan/a.out 
[Inferior 1 (process 95930) exited normally]
(gdb) p &t
$2 = (double *) 0x555555558010 <t>

Why did the address change from 0x4010 to 0x555555558010. Is there someway to prevent this? On Redhat, it doesn't do that:

% gdb a.out
(gdb) p &t
$1 = (double *) 0x601038 <t>
(gdb) r
Starting program: /home/phan/a.out 
[Inferior 1 (process 23337) exited normally]
(gdb) p &t
$2 = (double *) 0x601038 <t>

BTW, this only occurs in Ubuntu 18.04. In Ubuntu 16.04, it works exactly as Redhat, ie the address is the same before and after.

that other guy :

You are presumably seeing pre and post-relocation addresses for the .bss segment.

You can avoid this by disabling position independent executables, thus making gcc choose the final address of the .bss register up front:

gcc -no-pie foo.c

-static would have the effect.

I don't know why there'd be a difference between Ubuntu and Redhat though.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=6679&siteId=1