crt0(也叫做c0)是连接到C程序上的一组执行启动例程,它进行在调用这个程序的主函数之前所需要的任何初始化工作。它一般的都采用叫做crt0.o的目标文件形式,经常采用汇编语言编写,链接器自动的将它包括入它所建造的所有可执行文件中[1]。
简介
crt0包含大多数运行时库的基本部分。因此,它进行的确切工作依赖于程序的编译器、操作系统和C标准库实现[1]。除了运行时环境和工具链所需要的初始化工作,crt可以进行编程者定义额外操作,比如执行C++全局构造器和携带GCC的((constructor))
属性的C函数[2][3]。
“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
xor %ebp, %ebp # effectively RBP := 0, mark the end of stack frames
mov (%rsp), %edi # get argc from the stack (implicitly zero-extended to 64-bit)
lea 8(%rsp), %rsi # take the address of argv from the stack
lea 16(%rsp,%rdi,8), %rdx # take the address of envp from the stack
xor %eax, %eax # per ABI and compatibility with icc
call main # %edi, %rsi, %rdx are the three args (of which first two are C standard) to main
mov %eax, %edi # transfer the return of main to the first argument of _exit
xor %eax, %eax # per ABI and compatibility with icc
call _exit # terminate the program
参见
引用
外部链接
Wikiwand in your browser!
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.