Program C: __start

Can you help me understand how

__start

used inside C?

Is this an exact copy of the function, mainor is it the entry point to the compiled program?

Just wondering how to use it?

+5
source share
3 answers

Here is a good overview of what happens during program startup before main. In particular, this shows what __startis the actual entry point into your program from the OS point of view.

This is the very first address from which the instruction pointer starts counting in your program.

C, , main, exit main.


:

C runtime startup diagram

+12

_start - .... ... ( , , ) this_start main, CPU

0

According to the C / C ++ standard, it main()is the starting point for a program. If you use GCC, the function _startis the entry point of the C program that makes the call main(). The main objective of the function _start()is to perform several 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 record can also be compiled using the switch -ein 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");
}
0
source

All Articles