Initializer Syntax
initializer | The name of the initializer as specified with the +I linker option. |
|
|
handle | The initializer is called with this parameter set to the handle of the shared library for which |
| it was invoked. |
|
|
loading | 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. |
|
|
The initializers cannot be defined as local definitions. Initializers cannot be hidden through the use of the
Accessing Initializers' Addresses
Prior to the
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