crt1.o, crti.o, crtn.o, __start_crt_compiler - Core OS C Runtime Objects (CRT)
ld /usr/lib/crt1.o [compiler-crt-objects]... /usr/lib/crti.o ld-arguments... /usr/lib/crtn.o
ld /usr/lib/64/crt1.o [compiler-crt-objects]... /usr/lib/64/crti.o ld-arguments... /usr/lib/64/crtn.o
ld -G [compiler-crt-objects]... /usr/lib/crti.o ld-arguments... /usr/lib/crtn.o
ld -G [compiler-crt-objects]... /usr/lib/64/crti.o ld-arguments... /usr/lib/64/crtn.o
int __start_crt_compiler(int argc, char *argv[])
The crt1.o, crti.o, and crtn.o objects comprise the core CRT (C RunTime) objects required to enable basic C programs to start and run. CRT objects are typically added by compiler drivers when building executables and shared objects in a manner described by the above SYNOPSIS.
crt1.o provides the _start symbol that the runtime linker, ld.so.1, jumps to in order to pass control to the executable, and is responsible for providing ABI mandated symbols and other process initialization, for calling main(), and ultimately, exit(). crti.o and crtn.o provide prologue and epilogue .init and .fini sections to encapsulate ELF init and fini code.
crt1.o is only used when building executables. crti.o and crtn.o are used by executables and shared objects.
These CRT objects are compatible with position independent (PIC), and position dependent (non-PIC) code, including both normal and position independent executables (PIE).
Compilers may supply additional CRT objects to provide compiler or language specific initialization. If the compiler provides a relocatable object containing a __start_crt_compiler() function, then crt1.o calls __start_crt_compiler() immediately before calling main(), with the same arguments that main() receives. If __start_crt_compiler() returns a value of 0, then execution continues on to call main(). If __start_crt_compiler() returns a non-zero value, then that value is passed to exit(), and main() is not called. The __start_crt_compiler() is optional, and may be omitted if not needed.
The following example builds a simple executable that contains both init and fini functions. The program prints the number of arguments on the command line, and the number of environment variables.
#include <stdio.h> extern char **environ; #pragma init(main_init) static void main_init(void) { (void) printf("main_init\n"); } #pragma fini(main_fini) static void main_fini(void) { (void) printf("main_fini\n"); } int main(int argc, char **argv) { char **envp = environ; int envcnt = 0; for (; *envp; envp++) envcnt++; (void) printf("main: argc=%d, envcnt=%d\n", argc, envcnt); }
Normally, a compiler is used to compile and link a program in a single step. To illustrate CRT use, this example uses the link-editor directly to build the program from compiled objects.
example% cc -c main.c example% ld /usr/lib/crt1.o /usr/lib/crti.o main.o -lc /usr/lib/crtn.o example% ./a.out main_init main: argc=1, envcnt=49 main_fini
See attributes(7) for descriptions of the following attributes:
|
ld(1), ld.so.1(1), exec(2), exit(3C)
Oracle Solaris 11.4 Linkers and Libraries Guide
The reference to the C programming language in the term CRT is historical. The CRT objects described here are required by all dynamic objects.