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. 1.0 1.1 The C Runtime Initialization, crt0.o. embecosm.com. 2010 [2013-12-30]. 
  2. Program initialization: Creating a C library. osdev.org. 2014-02-25 [2014-04-21]. 
  3. Calling Global Constructors. osdev.org. 2014-04-08 [2014-04-21]. 
  4. Compiling a Program for Profiling: GNU gprof. sourceware.org. [2013-12-30]. 

外部連結