
Crt0
跳到导航
跳到搜索
程序执行 |
---|
基础概念 |
代码类型 |
编译策略 |
|
知名运行环境 |
著名编译器及工具链 |
crt0(也叫做c0)是连接到C程序上的一组执行启动例程,它进行在调用这个程序的主函数之前所需要的任何初始化工作。它一般的都采用叫做crt0.o的目标文件形式,经常采用汇编语言编写,链接器自动的将它包括入它所建造的所有可执行文件中[1]。
简介[编辑]
crt0包含大多数运行时库的基本部分。因此,它进行的确切工作依赖于程序的编译器、操作系统和C标准库实现[1]。除了运行时环境和工具链所需要的初始化工作,crt可以进行编程者定义额外操作,比如执行C++全局构造器和携带GCC的((constructor))属性的C函数[2][3],此时通常将其改称为crt1。
“crt”表示“C runtime”,“0”表示“最开始”。然而,当程序使用GCC来编译的时候,它也用于在C之外的语言,对于特殊使用场景可获得crt0的可替代版本,例如,分析器gprof要求它的程序同gcrt0一起编译[4]。
样例 crt0.s[编辑]
下面是针对linux x86_64的采用at&t语法的例子。
.text
.globl _start
_start: # _start is the entry point known to the linker
mov %rsp, %rbp # setup a new stack frame
mov 0(%rbp), %rdi # get argc from the stack
lea 8(%rbp), %rsi # get argv from the stack
call main # %rdi, %rsi are the first two args to main
mov %rax, %rdi # mov the return of main to the first argument
call exit # terminate the program
参见[编辑]
引用[编辑]
- ^ 1.0 1.1 The C Runtime Initialization, crt0.o. embecosm.com. 2010 [2013-12-30]. (原始内容存档于2013-12-30).
- ^ Program initialization: Creating a C library. osdev.org. 2014-02-25 [2014-04-21]. (原始内容存档于2014-04-23).
- ^ Calling Global Constructors. osdev.org. 2014-04-08 [2014-04-21]. (原始内容存档于2014-04-23).
- ^ Compiling a Program for Profiling: GNU gprof. sourceware.org. [2013-12-30]. (原始内容存档于2013-12-31).