dlopen dlsym dlfunc dlerror dlclose - programmatic interface to the dynamic linker
Lb libc
The
dlopen ();
function
provides access to the shared object in
Fa path ,
returning a descriptor that can be used for later
references to the object in calls to
dlsym ();
and
dlclose (.);
If
Fa path
was not in the address space prior to the call to
dlopen (,);
it is placed in the address space.
When an object is first loaded into the address space in this way, its
function
_init (,);
if any, is called by the dynamic linker.
If
Fa path
has already been placed in the address space in a previous call to
dlopen (,);
it is not added a second time, although a reference count of
dlopen ();
operations on
Fa path
is maintained.
A null pointer supplied for
Fa path
is interpreted as a reference to the main
executable of the process.
The
Fa mode
argument
controls the way in which external function references from the
loaded object are bound to their referents.
It must contain one of the following values, possibly ORed with
additional flags which will be described subsequently:
RTLD_LAZY
is normally preferred, for reasons of efficiency.
However,
RTLD_NOW
is useful to ensure that any undefined symbols are discovered during the
call to
dlopen (.);
One of the following flags may be ORed into the Fa mode argument:
If
dlopen ();
fails, it returns a null pointer, and sets an error condition which may
be interrogated with
dlerror (.);
The
dlsym ();
function
returns the address binding of the symbol described in the null-terminated
character string
Fa symbol ,
as it occurs in the shared object identified by
Fa handle .
The symbols exported by objects added to the address space by
dlopen ();
can be accessed only through calls to
dlsym (.);
Such symbols do not supersede any definition of those symbols already present
in the address space when the object is loaded, nor are they available to
satisfy normal dynamic linking references.
If
dlsym ();
is called with the special
Fa handle
NULL
it is interpreted as a reference to the executable or shared object
from which the call
is being made.
Thus a shared object can reference its own symbols.
If
dlsym ();
is called with the special
Fa handle
RTLD_DEFAULT
the search for the symbol follows the algorithm used for resolving
undefined symbols when objects are loaded.
The objects searched are
as follows, in the given order:
If
dlsym ();
is called with the special
Fa handle
RTLD_NEXT
then the search for the symbol is limited to the shared objects
which were loaded after the one issuing the call to
dlsym (.);
Thus, if the function is called from the main program, all
the shared libraries are searched.
If it is called from a shared library, all subsequent shared
libraries are searched.
RTLD_NEXT
is useful for implementing wrappers around library functions.
For example, a wrapper function
getpid ();
could access the
``real''
getpid ();
with
dlsym(RTLD_NEXT, getpid)
(Actually, the
dlfunc ();
interface, below, should be used, since
getpid ();
is a function and not a data object.)
If
dlsym ();
is called with the special
Fa handle
RTLD_SELF
then the search for the symbol is limited to the shared object
issuing the call to
dlsym ();
and those shared objects which were loaded after it.
The
dlsym ();
function
returns a null pointer if the symbol cannot be found, and sets an error
condition which may be queried with
dlerror (.);
The
dlfunc ();
function
implements all of the behavior of
dlsym (,);
but has a return type which can be cast to a function pointer without
triggering compiler diagnostics.
(The
dlsym ();
function
returns a data pointer; in the C standard, conversions between
data and function pointer types are undefined.
Some compilers and
lint(1)
utilities warn about such casts.)
The precise return type of
dlfunc ();
is unspecified; applications must cast it to an appropriate function pointer
type.
The
dlerror ();
function
returns a null-terminated character string describing the last error that
occurred during a call to
dlopen (,);
dladdr (,);
dlinfo (,);
dlsym (,);
dlfunc (,);
or
dlclose (.);
If no such error has occurred,
dlerror ();
returns a null pointer.
At each call to
dlerror (,);
the error indication is reset.
Thus in the case of two calls
to
dlerror (,);
where the second call follows the first immediately, the second call
will always return a null pointer.
The
dlclose ();
function
deletes a reference to the shared object referenced by
Fa handle .
If the reference count drops to 0, the object is removed from the
address space, and
Fa handle
is rendered invalid.
Just before removing a shared object in this way, the dynamic linker
calls the object's
_fini ();
function, if such a function is defined by the object.
If
dlclose ();
is successful, it returns a value of 0.
Otherwise it returns -1, and sets an error condition that can be
interrogated with
dlerror (.);
The object-intrinsic functions
_init ();
and
_fini ();
are called with no arguments, and are not expected to return values.
In previous implementations, it was necessary to prepend an underscore to all external symbols in order to gain symbol compatibility with object code compiled from the C language. This is still the case when using the (obsolete) -aout option to the C language compiler.
Закладки на сайте Проследить за страницей |
Created 1996-2024 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |