Reprinted from: http://bbs.csdn.net/topics/300103318#r_78088969
Before the main function--the actual function execution entry or start
An explanation:
In fact, after the executable is loaded, control is immediately handed to the start function, which is inserted by the compiler, which prepares the following global variables:
_osver component number of the operating system
Major version number of the _winmajor operating system
_winminor the minor version number of the operating system
_winver operating System full version number
__ARGC number of command line arguments
__ARGV an array of pointers to the parameter string
_environ array of pointers to environment variable strings
The start function initializes the heap and calls the main function. After the Mian function returns, the START function calls the Exit function to end the process.
Start the source of the function start in:
CRT0.C Microsoft Visual C + +
C0w.asm BORLADN C + +
An alternative explanation
Some of the stuff that have to happen before main ():
Set up initial stack pointer
Initialize static and global data
Zero out uninitialized data
Run Global constructors
Some of this comes with the runtime library's CRT0.O file or its __start () function. Some of it you need to do yourself.
Crt0 is a synonym for the C runtime library.
Depending on the system you ' re using the the follwing is incomplete, but it should give your idea. Using newlib-1.9.0/libgloss/m68k/crt0. S as an outline, the steps is:
1. Set stack pointer to value of __stack if Set
2. Set the initial value of the frame pointer
3. Clear. BSS (where all the values of the start at zero go)
4. Call indirect of Hardware_init_hook if set to initialize hardware
5. Call indirect of Software_init_hook if set to initialize software
6. Add __do_global_dtors and __fini_section__ to the atexit function so destructors and other cleanup functions is called When the program exits is either returning from main, or calling exit
7. Setup the paramters for argc, argv, ARGP and call main
8. Call Exit if main returns
The third explanation: the "C" language (three): Who called my main?
Now the most important thing is to keep up with the trend, so the more fashionable, who moved my cheese. Who called my main? But as a computer worker, I advise you still do not fashion, today's Java hot, tomorrow. NET popular, what fashionable to learn what. I mean to spend a few years to learn the basic skills, and so you are fashionable when also good. Don't say much nonsense.
We have all heard a word: "Main is the entrance to the C language." I still don't understand why I'm saying that. It's like if someone says, "Make money is a chick," it's sure countless bricks are coming. This sentence should be "money is a condition for girls, but this condition is particularly important". Then the above sentence should be "main is a C language symbol, but this symbol is more special." ”
Let's look at the following example:
/* File name TEST00.C */
int main (int argc, char* argv)
{
return 0;
}
Compile the link to it:
CC Test00.c-o Test.exe
Will generate Test.exe
But we add this option:-nostdlib (not linked to the standard library)
CC Test00.c-nostdlib-o Test.exe
The linker will error:
Undefined symbol: __start
Other words:
1. The compiler defaults to finding the __start symbol instead of the main
2. __start This symbol is the starting point of the program
3. Main is a symbol called by the standard library
Consider one more question:
We write programs, such as a module, usually have initialize and de-initialize, but when we write C program, why do some modules do not have these two processes? For example, our program can be malloc,free from main, but we do not initialize the heap in main. For example, in main can be directly printf, but we did not open the standard output file AH. (Do not know what is stdin,stdout,stderr and the relationship between printf and stdout please first look at the concept of the file in C).
Some people say that these things do not need to be initialized. If you really think so, please don't look down, I personally think the computer software is not suitable for you.
The wise people will think, must have done something before main. Enables these functions to be called directly without initialization. In general, we will find a file with a name similar to CRT0.O in the context of the compiler, which contains the __start symbol we have just described. (CRT is probably the abbreviation of C Runtime, please help to confirm.) )
So what does the real crt0.s look like? Here we give some pseudo-code:
///////////////////////////////////////////////////////
Section. Text:
__start:
:
Init stack;
init heap;
Open stdin;
Open stdout;
Open stderr;
:
Push argv;
Push argc;
Call _main; (Call Main)
:
Destory Heap;
Close stdin;
Close stdout;
Close stderr;
:
Call __exit;
////////////////////////////////////////////////////
There may actually be a lot of initialization work, because all are related to the operating system, the author is not listed.
Attention:
1. Different compilers, not necessarily the default symbols are __start.
2. The _main inside the assembly is the C language main, because the assembler and the C compiler have different names for the symbols (usually a bad underscore ' _ ').
3. There are two main branches of the operating system structure: micro-kernel and macro kernel. The advantage of micro-core is that the structure is clear, simple, the kernel components are less, easy to maintain, the disadvantage is that there is more communication between processes, the program frequently enters and exits the kernel, the efficiency is low. The macro kernel is just the opposite. I mean, what is this? The purpose is: there is no way to guarantee that each component is initialized in user space (standard library functions), that some components do not initialize, and that the operating system is in kernel space when the process is created. This depends on the specific implementation of the operating system, such as the heap, the macro kernel structure may be in the kernel initialization, micro-kernel structure in user space, even if the same micro-kernel, this thing may also be taken to the kernel space initialization.
With the development of CPU technology, the rapid expansion of storage capacity, the complexity of code increases, micro-core is more and more adopted. Will you increase the complexity of your code for 10% efficiency? Be aware that the CPU will double every 18 months. So my requirement for programmers is that I first don't want your code to be efficient, I first need your code to make 80% of people quickly understand and can be maintained.
Summarize:
Before the main function executes, it basically initializes the system-related resources:
1. Set the stack pointer
2. Initialize static and global variables, that is, the contents of the data segment
3. Assign the uninitialized part to the initial value: The numeric type short,int,long, such as 0,bool to False, the pointer is null, and so on, that is, the contents of the. BSS segment
4. Run the global constructor, which is estimated to be a constructor in C + +.
5. Pass the main function's arguments, ARGC,ARGV, and so on to the main function before actually running the main function
C + + main function before/after execution