The operating system calls the main() function. In fact, it usually causes something else strange as _init . The C compiler associates a standard library with each application that provides this operating system with a specific entry point, and then calls main() .
Edit: Obviously, this was not detailed enough and correct for some people.
The executable and related format (ELF) , which is used on many Unix operating systems, defines the address of the entry point. This is where the program starts to run after the completion of the OS call to exec() . On Linux, this is _init.
From objdump -d:
Disassembly of section .init: 08049f08 <_init>: 8049f08: 55 push %ebp 8049f09: 89 e5 mov %esp,%ebp 8049f0b: 83 ec 08 sub $0x8,%esp 8049f0e: e8 a1 05 00 00 call 804a4b4 <call_gmon_start> 8049f13: e8 f8 05 00 00 call 804a510 <frame_dummy> 8049f18: e8 d3 50 00 00 call 804eff0 <__do_global_ctors_aux> 8049f1d: c9 leave 8049f1e: c3 ret
From readelf -d:
0x00000001 (NEEDED) Shared library: [libstdc++.so.6] 0x00000001 (NEEDED) Shared library: [libm.so.6] 0x00000001 (NEEDED) Shared library: [libgcc_s.so.1] 0x00000001 (NEEDED) Shared library: [libpthread.so.0] 0x00000001 (NEEDED) Shared library: [libc.so.6] 0x0000000c (INIT) 0x8049f08 0x0000000d (FINI) 0x804f018 0x00000004 (HASH) 0x8048168 0x00000005 (STRTAB) 0x8048d8c 0x00000006 (SYMTAB) 0x804867c 0x0000000a (STRSZ) 3313 (bytes) 0x0000000b (SYMENT) 16 (bytes) 0x00000015 (DEBUG) 0x0 0x00000003 (PLTGOT) 0x8059114 0x00000002 (PLTRELSZ) 688 (bytes) 0x00000014 (PLTREL) REL 0x00000017 (JMPREL) 0x8049c58 0x00000011 (REL) 0x8049be0 0x00000012 (RELSZ) 120 (bytes) 0x00000013 (RELENT) 8 (bytes) 0x6ffffffe (VERNEED) 0x8049b60 0x6fffffff (VERNEEDNUM) 3 0x6ffffff0 (VERSYM) 0x8049a7e 0x00000000 (NULL) 0x0
You can see that INIT is equal to the address _init.
The code for frame_dummy and __do_global_ctors_aux is in a set of files called crtbegin.o and crtend.o (and variations of these names). This is part of the GCC. This code does the various things necessary for a C program, for example, to configure stdin, stdout, global and static variables, and other things.
The following article describes quite well what it does on Linux (taken from the answer below with fewer votes): http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html
I believe that someone else has already described what Windows does.
Zan Lynx Aug 12 '10 at 16:42 2010-08-12 16:42
source share