Can you help me to understand how
__start
is used in C internally?
Is it the exact replica of the main
function or is it the entry point to the compiled program?
Just wondering, how its getting used?
Rules for naming a variable A variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore.
Every C program has a primary function that must be named main . The main function serves as the starting point for program execution. It usually controls program execution by directing the calls to other functions in the program.
&n writes the address of n . The address of a variable points to the value of that variable.
In particular, it shows that __start is the actual entry point to your program from OS viewpoint. It is the very first address from which the instruction pointer will start counting in your program.
Here is a good overview of what happens during program startup before main
. In particular, it shows that __start
is the actual entry point to your program from OS viewpoint.
It is the very first address from which the instruction pointer will start counting in your program.
The code there invokes some C runtime library routines just to do some housekeeping, then call your main
, and then bring things down and call exit
with whatever exit code main
returned.
A picture is worth a thousand words:
As per C/C++ standard, main()
is the starting point of a program. If you're using GCC, _start
function is the entry point of a C program which makes a call to main()
. The main job of _start()
function is to perform a few initialization tasks.
// $ gcc program_entry.c -nostartfiles
// $ ./a.out
// custom program entry
#include <stdio.h>
#include <stdlib.h>
void program_entry(void);
void
_start(void)
{
program_entry();
}
void
program_entry(void)
{
printf("custom program entry\n");
exit(0);
}
If you want, the program entry can also be compiled with -e
switch in GCC.
// $ gcc program_entry.c -e __start
// $ ./a.out
// custom program entr
#include <stdio.h>
void program_entry(void);
void
_start(void)
{
program_entry();
}
void
program_entry(void)
{
printf("custom program entry\n");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With