3.6.6 Output Section Keywords
- The command tells the linker to create a symbol for each input file.
The name of each symbol will be the name of the corresponding input
file. The section of each symbol will be the output section in which
This is conventional for the a.out object file format. It is not normally used for any other object file format.
- When linking using the a.out object file format, the linker uses an
unusual set construct to support C++ global constructors and
destructors. When linking object file formats which do not support
arbitrary sections, such as ECOFF and XCOFF, the linker will
automatically recognize C++ global constructors and destructors by name.
For these object file formats, the
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. Similarly,
__DTOR_END__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
__main; a call to
__mainis 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
ELFwhich support arbitrary section names, gnu C++ will normally arrange to put the addresses of global constructors and destructors into the
.dtorssections. 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
CONSTRUCTORScommand, use SORT_BY_NAME(CONSTRUCTORS) instead. When using the
.dtorssections, use *(SORT_BY_NAME(.ctors)) and *(SORT_BY_NAME(.dtors)) instead of just *(.ctors) and *(.dtors).
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.