Crt0
   HOME

TheInfoList



OR:

(also known as ) 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 In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments. To start a program's execution, the loader or operating system passes contr ...
.


Form and usage

Crt0 generally takes the form of an
object file An object file is a computer file containing object code, that is, machine code output of an assembler or compiler. The object code is usually relocatable, and not usually directly executable. There are various formats for object files, and the ...
called , often written in
assembly language In computer programming, assembly language (or assembler language, or symbolic machine code), often referred to simply as Assembly and commonly abbreviated as ASM or asm, is any low-level programming language with a very strong correspondence b ...
, which is automatically included by the linker into every executable file it builds. contains the most basic parts of the
runtime library In computer programming, a runtime library is a set of low-level routines used by a compiler to invoke some of the behaviors of a runtime environment, by inserting calls to the runtime library into compiled executable binary. The runtime enviro ...
. As such, the exact work it performs depends on the program's compiler, operating system and
C standard library The C standard library or libc is the standard library for the C programming language, as specified in the ISO C standard. ISO/IEC (2018). '' ISO/IEC 9899:2018(E): Programming Languages - C ยง7'' Starting from the original ANSI C standard, it was ...
implementation. Beside the initialization work required by the environment and toolchain, can perform additional operations defined by the programmer, such as executing C++ global constructors and C functions carrying GCC's 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 are available for special usage scenarios; for example, to enable profiling with gprof, programs must be compiled with instead.


Example crt0.s

This example is for
Linux Linux ( or ) is a family of open-source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged as a Linux distribution, whi ...
x86-64 x86-64 (also known as x64, x86_64, AMD64, and Intel 64) is a 64-bit version of the x86 instruction set, first released in 1999. It introduced two new modes of operation, 64-bit mode and compatibility mode, along with a new 4-level paging ...
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

*
Entry point In computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programmin ...
* Runtime system


References

{{Reflist


External links


crt0.o vs crt1.o



Hello from a libc-free world! (Part 1)
March 16, 2010
glibc x86_64 start.S
C standard library C programming language family