Next: Output Section Discarding, Previous: Output Section Data, Up: SECTIONS
There are a couple of keywords which can appear as output section commands.
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.
__CTOR_LIST__ marks the start of the global
constructors, and the symbol
__CTOR_END__ marks the end.
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
__main; a call to
__main is automatically inserted into
the startup code for
main. gnu C++ normally runs
destructors either by using
atexit, or directly from the function
For object file formats such as
ELF which support
arbitrary section names, gnu C++ will normally arrange to put the
addresses of global constructors and destructors into the
.dtors 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 `SORT_BY_NAME(CONSTRUCTORS)' instead. When using the
.dtors sections, use `*(SORT_BY_NAME(.ctors))' and
`*(SORT_BY_NAME(.dtors))' instead of just `*(.ctors)' 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.