Using HP-UX 10.X Style Initializers

The initializer is called for libraries that are loaded implicitly at program startup, or explicitly with shl_load or dlopen. When calling initializers for implicitly loaded libraries, the dynamic loader waits until all libraries have been loaded before calling the initializers. It calls the initializers in depth-first order - that is, the initializers are called in the reverse order in which the libraries are searched for symbols. All initializers are called before the main program begins execution. When calling the initializer for explicitly loaded libraries, the dynamic loader waits until any dependent libraries are loaded before calling the initializers. As with implicitly loaded libraries, initializers are called in depth-first order. Note that initializers can be disabled for explicitly loaded libraries with the BIND_NOSTART flag to shl_load. For more information, see The shl_load Summary. This section discusses the following topics:

“Declaring the Initializer with the +I Option” (page 143)

“Order of Execution of Multiple Initializers” (page 143)

“Initializer Syntax” (page 144)

“Example: An Initializer for Each Library” (page 144)

“Example: A Common Initializer for Multiple Libraries” (page 146)

Declaring the Initializer with the +I Option

To declare the name of the initializer, use the +I linker option when creating the shared library. The syntax of the +I option is:

+I initializer

where initializer is the initializer's name.

Multiple initializers may be called by repeating the +I initializer option.

For example, to create a shared library named libfoo.so that uses an initializer named init_foo, use this linker command line:

$ ld -b -o libfoo.so libfoo.o +I init_foo

Order of Execution of Multiple Initializers

Multiple initializers are executed in the same order that they appear on the command line; they are unloaded in reverse order. (This applies only to the calling order within a shared library, not across multiple shared libraries.)

NOTE: In PA-32 compatibility mode, initializers are not executed when unloading shared libraries which were implicitly loaded because the program exits without re-entering the dynamic loader to unload them. Initializers are called only during the explicit unloading of a shared library.

Initializers behave the same as other symbols; once they are bound they cannot be overridden with a new symbol through the use of shl_definesym() or by loading a more visible occurrence of the initializer symbol with the BIND_FIRST flag. What this means is that once the initializer is executed upon a load, it is guaranteed to be the same initializer that is called on an explicit unload.

Initializer Syntax

void initializer( shl_t handle, int loading )

Initializers for Shared Libraries 143

Page 143
Image 143
HP UX Software Transition Kit (STK) Using HP-UX 10.X Style Initializers, Declaring the Initializer with the +I Option