Fini Finalizers (finis) are called after the user's code terminates by either calling the libc exit function, returning from the main or _start functions, or when the shared library which contains the fini is unloaded from memory. Like init functions, functions specified with this option must not take arguments and return nothing (void functions). The C compiler pragma "fini" can be used to create them. For example:
#pragma fini "my_fini"
void my_fini() { ... do some clean up ... }
The ld command also supports the +fini function option to specify the terminator. Use this option while building a shared library, an incomplete executable, or fully bound executable. Use the +fini option to specify the terminator (finalizer) functions, to be invoked in forward order, the order the functions appear left to right on the command line. The terminator functions are called in reverse of the
$ ld
void my_10x_init(shl_t handle, int loading)
{/* handle is the shl_load API handle for */ /* the shared library being initialized. */
/* loading is
if (loading) {
... do some initializations ...
else {
... do some clean up ...
}
}
NOTE: Unlike
See Using
Using Init/Fini Initializers
This section describes use of init/fini initializer and provides examples:
•“Init and Fini Usage Example” (page 139)
•“Ordering Within an Executable or Shared Library” (page 141)
•“Ordering Among Executables and Shared Libraries” (page 142)
Init and Fini Usage Example
This example consists of three shared libraries lib1.so, lib2.so, and lib3.so. The lib1.so depends on lib3.so. The main program (a.out) depends on lib1.so and lib2.so. Each shared library has an init style initializer and a fini style terminator. The lib1.so and lib2.so
Initializers for Shared Libraries 139