HP-UX Linker and Libraries User's Guide
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 depth-first order of initializers. Do not use +fini with the -r option. (The linker
ignores the +fini option.) You can specify more than one terminator function on the command
line with multiple option-symbol pairs, that is, each function you specify must be preceded by the
+fini option.
HP-UX-10.X Style Initializers
HP-UX 10.X style initializers are the same type supported in all HP-UX 10.X releases. These are
called both, before the user's code is started or a shared library is loaded (using shl_load or
dlopen), and when the shared library is unloaded (using shl_unload or dlclose). The linker
option +I is used to declare this type of initializer. The function returns nothing but takes two
arguments. The first is a handle to the shared library being initialized. This handle can be used in
calling shl_load routines. The second is set to non-zero at startup and zero at program termination
or library unload.
$ ld -b foo.o +I my_10x_init -o libfoo.so
#include <dl.h>
void my_10x_init(shl_t handle, int loading)
{ /* handle is the shl_load API handle for */
/* the shared library being initialized. */
/* loading is non-zero at startup and zero at termination. */
if (loading) {
... do some initializations ...
else {
... do some clean up ...
}
}
NOTE: Unlike PA-32 mode, the PA-64 and IPF HP-UX 10.X style initializers are called when
unloading implicitly loaded shared libraries.
See Using HP-UX 10.X Style Initializers for more information on using these initializers.
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