ASLR happens mostly at mmap(2) time. The stack segment of the main thread is allocated at execve(2) time (of your program) -but may be "randomly" located. The initial stack pointer of your main
also depends on various factors (notably your environment - see environ(7)).
The stack pointer is set at execve
time. It is passed to the crt0.o
startup object file (which calls your main
) by conventions defined in e.g. the x86-64 ABI specifications.
The address of main
is fixed inside the ELF executable file. Unless your code is position independent code (i.e. compiled with -fPIE
or -fPIC
etc...), it cannot be moved (because that would require specific relocation). Use objdump -f badnack
on your badnack
executable to find out. Also pmap
on your process. And PIC has a small cost (It uses more registers).