Reputation:
Assume this C code:
int main(){
return 0;
}
Would look like this in assembly:
main:
pushq %rbp
movq %rsp, %rbp
movl $0, %eax
popq %rbp
ret
I know that Frame pointer fp
needs to be saved in the start of functions by pushq %rbp
since it needs to be restored when returnning to the caller function.
My question is why do so in main
? what's the parent caller of main
? Isn't fp
pointing to a virtual address, meaning when main
terminates the address doesn't mean anything anymore to the next program, correct?
Are fp
(or even sp) values persistent between different programs and their address space?
Upvotes: 3
Views: 514
Reputation: 23802
what's the parent caller of main?
In linux main
is called by __libc_start_main
witch in term is called by _start
, in windows I'm not so sure but there is also a _start
.
In fact a neat trick is to start a C program without main
:
#include <stdio.h>
#include <stdlib.h>
void _start()
{
printf("No main function!\n");
exit(0);
}
compile with:
gcc main.c -nostartfiles
For Windows(10, gcc 8.1.0) and Ubuntu(18.04, gcc 9.2.0)
clang -Wl,-e,-Wl,__start main.c
For MacOS (10.14.6, Xcode 11.3)
Here is an article that talks about Linux x86 Program Start Up
Upvotes: 4