3.6.6 Output Section Keywords
There are a couple of keywords which can appear as output section commands.
-
CREATE_OBJECT_SYMBOLS -
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 the
CREATE_OBJECT_SYMBOLScommand appears.This is conventional for the a.out object file format. It is not normally used for any other object file format.
-
CONSTRUCTORS -
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 theCONSTRUCTORScommand appears. TheCONSTRUCTORScommand is ignored for other object file formats.The symbol
__CTOR_LIST__marks the start of the global constructors, and the symbol__CTOR_END__marks the end. Similarly,__DTOR_LIST__and__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 formain. gnu C++ normally runs destructors either by usingatexit, or directly from the functionexit.For object file formats such as
COFForELFwhich support arbitrary section names, gnu C++ will normally arrange to put the addresses of global constructors and destructors into the.ctorsand.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.ctorsand.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.