crt0
{{short description|Set of execution startup routines linked into a C program}}
{{Lowercase title}}
{{Program execution}}
{{mono|crt0}} (also known as {{mono|c0}}) is a set of execution startup routines linked into a C program that performs any initialization work required before calling the program's main function. After the main function completes the control returns to crt0, which calls the library function exit(0) to terminate the process.
Form and usage
Crt0 generally takes the form of an object file called {{mono|crt0.o}}, often written in assembly language, which is automatically included by the linker into every executable file it builds.{{cite web|url= https://www.embecosm.com/appnotes/ean9/html/ch05s02.html |title=The C Runtime Initialization, crt0.o |year=2010 |accessdate=2013-12-30 |website=embecosm.com}}
{{mono|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, {{mono|crt0}} can perform additional operations defined by the programmer, such as executing C++ global constructors and C functions carrying GCC's {{mono|((constructor))}} attribute.{{cite web|url=https://wiki.osdev.org/Creating_a_C_Library#Program_Initialization |title=Program initialization: Creating a C library |date=2014-02-25 |accessdate=2014-04-21 |website=osdev.org}}{{cite web|url=https://wiki.osdev.org/Calling_Global_Constructors |title=Calling Global Constructors |date=2014-04-08 |accessdate=2014-04-21 |website=osdev.org}}
"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 {{mono|crt0}} are available for special usage scenarios; for example, to enable profiling with gprof, programs must be compiled with {{mono|gcrt0}} instead.{{cite web|url=https://sourceware.org/binutils/docs-2.16/gprof/Compiling.html |title=Compiling a Program for Profiling: GNU gprof |accessdate=2013-12-30 |website=sourceware.org}}
Example crt0.s
This example is for Linux x86-64 with AT&T syntax, without an actual C runtime.
.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
See also
{{Portal|Computer programming}}
References
{{Reflist}}
External links
- [https://lists.uclibc.org/pipermail/uclibc/2002-December/025943.html crt0.o vs crt1.o]
- [http://dbp-consulting.com/tutorials/debugging/linuxProgramStartup.html Linux x86 program start-up]
- [https://blogs.oracle.com/linux/post/hello-from-a-libc-free-world-part-1 Hello from a libc-free world! (Part 1)], March 16, 2010
- [https://sourceware.org/git/?p=glibc.git;a=blob;f=sysdeps/x86_64/start.S;hb=b5367a08ae810e3c648fb036f2e5766204f9d83f glibc x86_64 start.S ]