|Types of code|
|Notable compilers & toolchains|
Form and usage
crt0 contains the most basic parts of the runtime library. As such, the exact work it performs depends on the program's compiler, operating system and C standard library implementation. Beside the initialization work required by the environment and toolchain, crt0 can perform additional operations defined by the programmer, such as executing C++ global constructors and C functions carrying GCC's ((constructor)) attribute.
"crt" stands for "C runtime", and the zero stands for "the very beginning". However, when programs are compiled using GCC, it is also used for languages other than C. Alternative versions of crt0 are available for special usage scenarios; for example, to enable profiling with gprof, programs must be compiled with gcrt0 instead.
.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
- "The C Runtime Initialization, crt0.o". embecosm.com. 2010. Retrieved 2013-12-30.
- "Program initialization: Creating a C library". osdev.org. 2014-02-25. Retrieved 2014-04-21.
- "Calling Global Constructors". osdev.org. 2014-04-08. Retrieved 2014-04-21.
- "Compiling a Program for Profiling: GNU gprof". sourceware.org. Retrieved 2013-12-30.