Crt0

crt0(也叫做c0)是连接到C程序上的一组执行启动例程,它进行在调用这个程序的主函数之前所需要的任何初始化工作。它一般的都采用叫做crt0.o的目标文件形式,经常采用汇编语言编写,链接器自动的将它包括入它所建造的所有可执行文件中。

简介
crt0包含大多数运行时库的基本部分。因此,它进行的确切工作依赖于程序的编译器、操作系统和C标准库实现。

“crt”表示“C runtime”,“0”表示“最开始”。然而,当程序使用GCC来编译的时候,它也用于在C之外的语言,对于特殊使用场景可获得crt0的可替代版本,例如,分析器gprof要求它的程序同gcrt0一起编译。

样例 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

参见

  • 入口点
  • 运行时系统

引用
外部链接

评论 (0)

  • 还没有评论,来抢沙发吧。