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]. (原始內容存檔於2013-12-30). 
  2. ^ Program initialization: Creating a C library. osdev.org. 2014-02-25 [2014-04-21]. (原始內容存檔於2014-04-23). 
  3. ^ Calling Global Constructors. osdev.org. 2014-04-08 [2014-04-21]. (原始內容存檔於2014-04-23). 
  4. ^ Compiling a Program for Profiling: GNU gprof. sourceware.org. [2013-12-30]. (原始內容存檔於2013-12-31). 

外部連結[編輯]