User's Manual

#include <dlfcn.h>
If an error occurs when calling shared library management routines, the system error variable
errno is set to an appropriate error value. Constants are defined for these error values in
/usr/include/errno.h (see errno(2)). Thus, if a program checks for these error values, it
must include errno.h:
#include <errno.h>
Throughout this section, all examples are given in C. To learn how to call these routines from aC++
or Fortran, see the inter-language calling conventions described in the compiler documentation.
Using Shared Libraries with cc and ld Options
In IPF/PA-64 mode, you can access the shl_load and dlopen routines by specifying either
-ldld or -ldl on the command line. In PA-32 mode, you can access theshl_load family of
routines by specifying the -ldld option on the cc(1) or ld(1) command line. The default behavior
of the PA-64 and IPF linker is to export all symbols defined by a program. However, some PA-32
implementations do not, by default, export all symbols, instead exporting only those symbols
imported by a shared library seen at link time. In PA-32 compatibility mode, use the -E option to
ld to ensure that all symbols defined in the program are available to the loaded libraries. To create
shared libraries, compile source files with +z or +Z and link the resultant object files. In PA-32
mode, to create share libraries, compile source files with +Z or +Z and link the resultant object
files with -b.
Initializers for Shared Libraries
A shared library can have an initialization routine - known as an initializer - that is called when
the load module (a shared library or executable) is loaded (initializer) or explicitly unloaded
(finalizer or terminator). Typically, an initializer is used to initialize a shared library's data when
the library is loaded. When a program begins execution, its initializers are called before any other
user code is executed. This allows for setup at initialization and cleanup at termination. Also, when
a shared library is explicitly loaded using shl_load or dlopen or unloaded using shl_unload
or dlclose, its initializers and terminators are called at the appropriate time. In IPF/PA-64 mode,
you can specify initializers and terminators even for archive libraries or nonshared executables.
Styles of Initializers
The linker supports two different types of initializers and terminators:
Init/fini style. (Not supported in HP-UX 10.X)
HP-UX 10.X style.
Init/Fini Style Initializers
This style uses init and fini functions to handle initialization and finalization (terminator) operations.
Init Initializers (inits) are called before the user's code starts or when a shared library is loaded.
Functions specified with this option must not take arguments and return nothing (void functions).
The C compiler pragma "init" can be used to declare these functions. For example:
#pragma init "my_init"
void my_init() { ... do some initializations ... }
The ld command also supports the +init function option to specify the initializer. Use this option
while building a shared library, an incomplete executable, or fully bound executable. Use the
+init option to specify the initializer functions, to be invoked in reverse order, the order the
functions appear right to left on the command line. Initializers are called in depth-first order. For
example, when a shared library is loaded, the initializers in all its dependent libraries are called
first. Do not use +init with the -r option. (The linker ignores the +init option.) You can specify
more than one initializer function on the command line with multiple option-symbol pairs, that is,
each function you specify must be preceded by the +init option.
138 Shared Library Management Routines