Chapter 6 239
Shared Library Management Routines
The shl_load Shared Library Management Routines
Description
To unload a shared library, use the shl_unload function. One reason to
do this is to free up the private copy of shared library data and swap
space allocated when the library was loaded with shl_load. (This is
done automatically when a process exits.)
Another reason for doing this occurs if a program needs to replace a
shared library. For example, suppose you implement some sort of shell or
interpreter, and you want to load and execute user “programs” which are
actually shared libraries. So you load one program, look up its entry
point, and call it. Now you want to run a different program. If you do not
unload the old one, its symbol definitions might get in the way of the new
library. So you should unload it before loading the new library.
Note that if shared library initializers have been declared for a shared
library, they will be called when the shared library is explicitly unloaded.
For details, see “Initializers for Shared Libraries”.
If unloading a C++ library, use the cxxshl_unload routine. This
ensures that destructors of nonlocal static objects are executed when the
library is unloaded. The syntax of cxxshl_unload is the same as that of
shl_unload.
Usage
When a library is unloaded, existing linkages to symbols in an unloaded
library are not invalidated. Therefore, the programmer must ensure that
the program does not reference symbols in an unloaded library as
undefined behavior will result. In general, this routine is recommended
only for experienced programmers.
In 32-bit mode the shl_unload routine unloads a shared library
irrespective of whether other shared libraries depend on it. In 64-bit
mode shl_unload unloads a shared library only if no other shared
library depend on it.