Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program: __start [duplicate]

Tags:

c

linux

unix

gcc

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?

like image 839
San Avatar asked Apr 10 '13 07:04

San


People also ask

Can C variable start with underscore?

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.

What is the starting point of c program?

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.

WHAT IS &N in C?

&n writes the address of n . The address of a variable points to the value of that variable.

What does_ start do?

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.


2 Answers

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

like image 144
ulidtko Avatar answered Oct 31 '22 22:10

ulidtko


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");
}
like image 26
debug Avatar answered Oct 31 '22 21:10

debug