This is conventional for the a.out object file format. It is not normally used for any other object file format.
CONSTRUCTORScommand tells the linker to place constructor information in the output section where the
CONSTRUCTORScommand appears. The
CONSTRUCTORScommand is ignored for other object file formats.
marks the start of the global constructors, and the symbol
marks the end. Similarly,
mark the start and end of the global destructors. The first word in the list is the number of entries, followed by the address of each constructor or destructor, followed by a zero word. The compiler must arrange to actually run the code. For these object file formats gnu
C++ normally calls constructors from a subroutine
; a call to
is automatically inserted into the startup code for
C++ normally runs destructors either by using
, or directly from the function
For object file formats such as
which support arbitrary section names, gnu
C++ will normally arrange to put the addresses of global constructors and destructors into the
sections. Placing the following sequence into your linker script will build the sort of table which the gnu
C++ runtime code expects to see.
__CTOR_LIST__ = .; LONG((__CTOR_END__ - __CTOR_LIST__) / 4 - 2) *(.ctors) LONG(0) __CTOR_END__ = .; __DTOR_LIST__ = .; LONG((__DTOR_END__ - __DTOR_LIST__) / 4 - 2) *(.dtors) LONG(0) __DTOR_END__ = .;
If you are using the gnu
C++ support for initialization priority, which provides some control over the order in which global constructors are run, you must sort the constructors at link time to ensure that they are executed in the correct order. When using the
command, use ‘
’ instead. When using the
sections, use ‘
’ and ‘
’ instead of just ‘
’ and ‘
Normally the compiler and linker will handle these issues automatically, and you will not need to concern yourself with them. However, you may need to consider this if you are using C++ and writing your own linker scripts.