I'm on an Ubuntu ( 22.04.3) x86_64 system.
I know how virtual memory and pages work in general and I also know that systems can use ASLR. However I'd like to know what exactly happens in an Ubuntu system in this specific scenario. I have this little program in a file named test.c :
#include <stdio.h>
int main(){
int a = 10;
a++;
printf("%p\n",(void*)&main);
}
I've compiled this file with "gcc test.c -o test". Then I ran the command "objdump -d test" and I got this :
0000000000001149 <main>:
1149: f3 0f 1e fa endbr64
114d: 55 push %rbp
114e: 48 89 e5 mov %rsp,%rbp
1151: 48 83 ec 10 sub $0x10,%rsp
1155: c7 45 fc 0a 00 00 00 movl $0xa,-0x4(%rbp)
115c: 83 45 fc 01 addl $0x1,-0x4(%rbp)
1160: 48 8d 05 e2 ff ff ff lea -0x1e(%rip),%rax # 1149 <main>
1167: 48 89 c6 mov %rax,%rsi
116a: 48 8d 05 93 0e 00 00 lea 0xe93(%rip),%rax # 2004 <_IO_stdin_used+0x4>
1171: 48 89 c7 mov %rax,%rdi
1174: b8 00 00 00 00 mov $0x0,%eax
1179: e8 d2 fe ff ff call 1050 <printf@plt>
117e: b8 00 00 00 00 mov $0x0,%eax
1183: c9 leave
1184: c3 ret
As you can see main address is 0x1149. When I run the program the address of main is different, and it is different each time I run it. I've done it three times and I got three different results :
0x55b032770149
0x5615f1082149
0x557829668149
I can infer this has something to do with ASLR. Here are my questions :
How did we get from 0x1149 to for example 0x55b032770149 ? What is the whole process that happens under the hood ?
Are the addresses printed in a C program, like 0x55b032770149, the actual and definitive virtual addresses of our programs ?
Does the RIP register contain virtual addresses, like for example, 0x55b032770149 as the address of the next instruction to execute ?