8

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?

ulidtko
  • 14,740
  • 10
  • 56
  • 88
San
  • 905
  • 3
  • 16
  • 33

3 Answers3

17

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:

C runtime startup diagram

ulidtko
  • 14,740
  • 10
  • 56
  • 88
  • Awesome link!!! +1, is it possible to quote the main things in it, in case the site will be down some day, hopefully not. – 0x90 Apr 16 '13 at 09:07
  • In case the article dies some day in future — perhaps the Wayback Machine [might help](https://web.archive.org/web/20170822123701/http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html). – ulidtko Jan 09 '18 at 10:23
2

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");
}
debug
  • 991
  • 10
  • 14
-1

_start is a operating system function....which is entry point for any program...as our compiler knows about main(main is not pre defined function it is user defined but all the compiler knows about them) this _start function will call main and from that point our program enters in CPU

umang2203
  • 78
  • 5