HP-UX Linker and Libraries User's Guide
Initializer Syntax
The name of the initializer as specified with the +I linker option.initializer
The initializer is called with this parameter set to the handle of the shared library for which
it was invoked.
handle
The initializer is called with this parameter set to 1 (true) when the shared library is loaded
and 0 (false) when the library is unloaded.
loading
The initializers cannot be defined as local definitions. Initializers cannot be hidden through the use
of the -h option when building a shared library. HP recommends that initializers be defined with
names which do not cause name collisions with other user-defined names in order to avoid overriding
behavior of shared library symbol binding.
Accessing Initializers' Addresses
Prior to the HP-UX 10.0 release, initializer's addresses could be accessed through the initializer
field of the shared library descriptor which is returned from a call to shl_get(). To support
multiple initializers, the shl_getsymbols() routine is enhanced to support the return of the
initializer's address. If only one initializer is specified for a given library, its address is still available
through the initializer field of a shared library descriptor. If more than one initializer is specified,
the initializer field is set to NO_INITIALIZER. Access to multiple initializers can then be
accomplished through the use of shl_getsymbols(). (The shl_getsymbols() routine can
also access a single initializer.)
NOTE: The shl_getsymbols() routine may not return the initializer which was invoked for a
given library if a more visible initializer symbol is defined after the library being queried has been
loaded. This can occur through the use of shl_definesym() and by explicitly loading a more
visible symbol using the BIND_FIRST flag upon loading.
To access initializers, use the new flag, INITIALIZERS, in the shl_getsymbols() routine.
This flag can be ORed with the NO_VALUES and GLOBAL_VALUES flags. For example,
shl_getsymbols(handle,
TYPE_PROCEDURE,
INITIALIZERS | GLOBAL_VALUES,
malloc,
&symbol_array);
If the GLOBAL_VALUES modifier is not used and the initializer is defined in another shared library
or in the program file, shl_getsymbols() does not find the initializer for the requested library
because it is not defined within the library. For more information on the usage of
shl_getsymbols(), see The shl_getsymbols Routine.
Example: An Initializer for Each Library
One way to use initializers is to define a unique initializer for each library. For instance, the
following example shows the source code for a library named libfoo.so that contains an
initializer named init_foo:
C Source for libfoo.c
#include <stdio.h>
#include <dl.h>
/*
* This is the local initializer that is called when the libfoo.so
* is loaded and unloaded:
*/
void init_foo(shl_t hndl, int loading)
{
if (loading)
printf("libfoo loaded\n");
else
printf("libfoo unloaded\n");
144 Shared Library Management Routines