In C, what is the main () method called?

How does program C start?

+46
c operating-system
Aug 12 '10 at 16:39
source share
7 answers

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.

+38
Aug 12 '10 at 16:42
source share

After all, it is an operating system. Usually there is a certain environment between the real entry point and the main function, it is inserted by the compiler .

Some information (related to Windows): In a PE file named IMAGE_OPTIONAL_HEADER there is a header in which there is an AddressOfEntryPoint field, which in turn is the address of the first byte of code in the file that will be executed.

+23
Aug 12 '10 at 16:41
source share
+8
Aug 12 '10 at 16:45
source share

The operating system calls main. The moved executable will contain an address indicating the location of the main one (see Unix ABI for more information).

But , which invokes the operating system?

The central processor on the "RESET" signal (which is also confirmed when the power is turned on) will begin to search in some ROMs at the specified address (for example, 0xffff) for its instructions.

As a rule, the BIOS will have some kind of jumping instruction, which receives configured memory chips, loads the main hard disk drivers, etc. etc. Then the boot sector on the hard drive is read, and the next bootloader, which loads the file containing basic information on how to read, say, the NTFS partition and how to read the kernel file. The kernel environment will be configured, the kernel will boot, and then - and then! - the kernel will be bounced to run.

After doing this complex work, the kernel can continue downloading our software.

+6
Aug 12 '10 at 17:10
source share

The operating system calls the function included in the C runtime (CRT) and associated with your executable. Call this "core CRT."

CRT main does several things, the two most important of which, at least in C ++, must be launched through an array of C ++ global classes and call their constructors, as well as call the main () function and give the return value to the shell.

Visual C ++ CRT main does a few more things if memory serves. It sets up a memory allocator, which is important if you use Debug CRT to help you find memory leaks or bad treatment. It also calls main in the structured exception handler , which detects poor memory access and other crashes and displays them.

+5
Aug 12 '10 at 17:52
source share

Note that in addition to the answers already posted, you can also call main yourself. This is usually a bad idea reserved for obfuscating code.

+4
Aug 12 2018-10-12
source share

Probably the best information for your question can be found in the link below http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html , the best I've encountered so far.

+3
Feb 17 '14 at 5:30
source share



All Articles