C program: __start [duplicate]

我只是一个虾纸丫 提交于 2020-02-02 02:15:32

问题


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?


回答1:


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:




回答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");
}



回答3:


_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



来源:https://stackoverflow.com/questions/15919356/c-program-start

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!