在C语言中,是怎样的main()方法最初叫什么名字? [英] In C, how is the main() method initially called?

查看:147
本文介绍了在C语言中,是怎样的main()方法最初叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何做一个C程序开始?

How does a C program get started?

推荐答案

操作系统调用的main()功能。事实上,它通常调用别的名为像 _init 一件奇怪的事。 C编译器链接标准库的每一个提供定义切入点这个操作系统的应用程序,然后调用的main()

The operating system calls the main() function. Actually, it usually calls something else named a strange thing like _init. The C compiler links a standard library to every application which provides this operating system defined entry point and then calls main().

编辑:很明显,这不是详述,对于某些人来说足够正确

Obviously that was not detailed and correct enough for some people.

借助可执行和链接格式(ELF)其中许多Unix操作系统的使用定义的入口点地址。这就是程序开始运行OS完成其执行exec()调用之后。在Linux系统上这是_init。

The Executable and Linkable Format (ELF) which many Unix OS's use defines an entry point address. That is where the program begins to run after the OS finishes its exec() call. On a Linux system this is _init.

从objdump的-d:

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    

从readelf -d:

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

您可以看到INIT等于_init的地址。

You can see that INIT is equal to the address of _init.

在code为frame_dummy和__do_global_ctors_aux是一组命名crtbegin.o和crtend.o文件(这些名字的变体)的。这是GCC的一部分。这code做必要的各种事情像设置标准输入,标准输出,全局和静态变量和其他东西的C程序。

The code for frame_dummy and __do_global_ctors_aux is in a set of files named crtbegin.o and crtend.o (and variants of those names). These are part of GCC. That code does various things necessary for a C program like setting up stdin, stdout, global and static variables and other things.

下面的文章描述相当不错了它在Linux确实(从答案用更少的票数低于拍摄):<一href=\"http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html\">http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html

The following article describes quite well what it does in Linux (taken from an answer below with less votes): http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html

我相信已经描述别人的答案是什么Windows那样。

I believe someone else's answer already described what Windows does.

这篇关于在C语言中,是怎样的main()方法最初叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆