Initialization and Termination Routines
Dynamic objects can supply code that provides for runtime initialization and termination processing. The initialization code of a dynamic object is executed once each time the dynamic object is loaded in a process. The termination code of a dynamic object is executed once each time the dynamic object is unloaded from a process or at process termination.
Before transferring control to an application, the runtime
linker processes any initialization sections found in the application and
any loaded dependencies. If new dynamic objects are loaded during process
execution, their initialization sections are processed as part of loading
the object. The initialization sections .preinit_array
,
.init_array
, and .init
are
created by the link-editor when a dynamic object is built.
The runtime linker executes functions whose addresses are contained in the
.preinit_array
and .init_array
sections. These functions are executed in the same order in which their
addresses appear in the array. The runtime linker executes an
.init
section as an individual function. If an
object contains both .init
and
.init_array
sections, the
.init
section is processed before the functions
defined by the .init_array
section for that
object.
An executable can provide pre-initialization functions
in a .preinit_array
section. These functions are executed
after the runtime linker has built the process image and performed
relocations but before any other initialization functions.
Pre-initialization functions are not permitted in
shared objects.
Note:
Any.init
section within the executable is
called from the application by the process startup mechanism
supplied by the compiler driver. The .init
section within the executable is called last, after all
dependency initialization sections are executed.
Dynamic objects can also provide termination sections. The
termination sections .fini_array
and
.fini
are created by the link-editor when a
dynamic object is built.
Any termination sections are passed to
atexit
(3C). These termination routines are called when
the process calls
exit
(2). Termination sections are also called when
objects are removed from the running process with
dlclose
(3C).
The runtime linker executes functions whose addresses are contained in the
.fini_array
section. These functions are executed
in the reverse order in which their addresses appear in the array. The
runtime linker executes a .fini
section as an individual
function. If an object contains both .fini
and
.fini_array
sections, the functions defined by
the .fini_array
section are processed before the
.fini
section for that object.
Note:
Any.fini
section within the executable is
called from the application by the process termination mechanism
supplied by the compiler driver. The .fini
section of the executable is called first, before all
dependency termination sections are executed.
For more information on the creation of initialization and termination sections by the link-editor see Initialization and Termination Sections.