Crt0

Last updated

crt0 (also known as 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.

Contents

Form and usage

Crt0 generally takes the form of an object file called crt0.o, often written in assembly language, which is automatically included by the linker into every executable file it builds. [1]

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. [1] 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. [2] [3]

"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. [4]

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 linkerxor%ebp,%ebp# effectively RBP := 0, mark the end of stack framesmov(%rsp),%edi# get argc from the stack (implicitly zero-extended to 64-bit)lea8(%rsp),%rsi# take the address of argv from the stacklea16(%rsp,%rdi,8),%rdx# take the address of envp from the stackxor%eax,%eax# per ABI and compatibility with icccallmain# %edi, %rsi, %rdx are the three args (of which first two are C standard) to mainmov%eax,%edi# transfer the return of main to the first argument of _exitxor%eax,%eax# per ABI and compatibility with icccall_exit# terminate the program

See also

Related Research Articles

Netwide Assembler Assembler for the Intel x86 architecture

The Netwide Assembler (NASM) is an assembler and disassembler for the Intel x86 architecture. It can be used to write 16-bit, 32-bit (IA-32) and 64-bit (x86-64) programs. NASM is considered to be one of the most popular assemblers for Linux.

Transmeta Corporation was an American fabless semiconductor company based in Santa Clara, California. It developed low power x86 compatible microprocessors based on a VLIW core and a software layer called Code Morphing Software.

A low-level programming language is a programming language that provides little or no abstraction from a computer's instruction set architecture—commands or functions in the language map that are structurally similar to processor's instructions. Generally, this refers to either machine code or assembly language. Because of the low abstraction between the language and machine language, low-level languages are sometimes described as being "close to the hardware". Programs written in low-level languages tend to be relatively non-portable, due to being optimized for a certain type of system architecture.

x86 assembly language is the name for the family of assembly languages which provide some level of backward compatibility with CPUs back to the Intel 8008 microprocessor, which was launched in April 1972. It is used to produce object code for the x86 class of processors.

Executable A file that causes a computer to follow indicated instructions

In computing, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instructions", as opposed to a data file that must be interpreted (parsed) by a program to be meaningful.

In software engineering, a spinlock is a lock that causes a thread trying to acquire it to simply wait in a loop ("spin") while repeatedly checking whether the lock is available. Since the thread remains active but is not performing a useful task, the use of such a lock is a kind of busy waiting. Once acquired, spinlocks will usually be held until they are explicitly released, although in some implementations they may be automatically released if the thread being waited on blocks or "goes to sleep".

In computing, a bus error is a fault raised by hardware, notifying an operating system (OS) that a process is trying to access memory that the CPU cannot physically address: an invalid address for the address bus, hence the name. In modern use on most architectures these are much rarer than segmentation faults, which occur primarily due to memory access violations: problems in the logical address or permissions.

A cross compiler is a compiler capable of creating executable code for a platform other than the one on which the compiler is running. For example, a compiler that runs on a PC but generates code that runs on an Android smartphone is a cross compiler.

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 environment implements the execution model, built-in functions, and other fundamental behaviors of a programming language. During execution of that computer program, execution of those calls to the runtime library cause communication between the executable binary and the runtime environment. A runtime library often includes built-in functions for memory management or exception handling. Therefore, a runtime library is always specific to the platform and compiler.

In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a higher-level language such as C or Ada.

In computer science, a calling convention is an implementation-level (low-level) scheme for how subroutines receive parameters from their caller and how they return a result. Differences in various implementations include where parameters, return values, return addresses and scope links are placed, and how the tasks of preparing for a function call and restoring the environment afterwards are divided between the caller and the callee.

In assembly language programming, the function prologue is a few lines of code at the beginning of a function, which prepare the stack and registers for use within the function. Similarly, the function epilogue appears at the end of the function, and restores the stack and registers to the state they were in before the function was called.

Tiny C Compiler

The Tiny C Compiler is an x86, X86-64 and ARM processor C compiler initially written by Fabrice Bellard. It is designed to work for slow computers with little disk space. Windows operating system support was added in version 0.9.23. TCC is distributed under the GNU Lesser General Public License.

In the x86 architecture, the CPUID instruction is a processor supplementary instruction allowing software to discover details of the processor. It was introduced by Intel in 1993 with the launch of the Pentium and SL-enhanced 486 processors.

On many computer operating systems, a computer process terminates its execution by making an exit system call. More generally, an exit in a multithreading environment means that a thread of execution has stopped running. For resource management, the operating system reclaims resources that were used by the process. The process is said to be a dead process after it terminates.

This article describes the calling conventions used when programming x86 architecture microprocessors.

A decompiler is a computer program that translates an executable file to a high-level source file which can be recompiled successfully. It is therefore the opposite of a compiler, which translates a source file in to an executable. Decompilers are usually unable to perfectly reconstruct the original source code, thus frequently will produce obfuscated code. Nonetheless, decompilers remain an important tool in the reverse engineering of computer software.

JIT spraying is a class of computer security exploit that circumvents the protection of address space layout randomization (ASLR) and data execution prevention (DEP) by exploiting the behavior of just-in-time compilation. It has been used to exploit PDF format and Adobe Flash.

LuaJIT Just-in-time compiler for the Lua programming language

LuaJIT is a just in time compiler for the Lua programming language. It is generally a hard fork of Lua 5.1, although it does feature several backports from Lua 5.2.

Mingw-w64 is a free and open source software development environment to create Microsoft Windows PE applications. It was forked in 2005–2010 from MinGW.

References

  1. 1 2 "The C Runtime Initialization, crt0.o". embecosm.com. 2010. Retrieved 2013-12-30.
  2. "Program initialization: Creating a C library". osdev.org. 2014-02-25. Retrieved 2014-04-21.
  3. "Calling Global Constructors". osdev.org. 2014-04-08. Retrieved 2014-04-21.
  4. "Compiling a Program for Profiling: GNU gprof". sourceware.org. Retrieved 2013-12-30.