Go to main content

マニュアルページ セクション 7: 標準、環境、マクロ、文字セット、その他

印刷ビューの終了

更新: 2022年7月27日
 
 

crtn.o(7)

名前

crt1.o, crti.o, crtn.o, __start_crt_compiler - Core OS C Runtime Objects (CRT)

形式

Executable

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

Shared Object

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

Optional crt1.o Extension Function

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).

Compiler-Specific CRT Objects

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 __start_crt_compiler() function is reserved for the exclusive use of the compiler. Any other use is unsupported. Such use can result in undefined and non-portable behavior. Applications requiring code to execute at startup have a variety of supported options. The startup code can be called early in the main() function. Many compilers support the #pragma init directive to create init functions that run as part of program startup. Alternatively, some languages expose the concept of init functions in terms of portable language features, such as C++ static constructors.

使用例 1 Simple Executable.

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:

ATTRIBUTE TYPE
ATTRIBUTE VALUE
Availability
system/linker
Interface Stability
Committed
MT-Level
Safe

関連項目

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.