Home

dlsym

dlsym is a function provided by the dynamic linker on most Unix-like systems. It retrieves the address of a symbol from a loaded shared object. The function signature is void *dlsym(void *handle, const char *symbol); handle is obtained from dlopen or is one of the special handles RTLD_DEFAULT or RTLD_NEXT, and symbol is the name of the symbol to locate. The return value is a pointer to the symbol, or NULL if the symbol cannot be found or an error occurred. When an error occurs, additional information is available through dlerror.

dlsym is commonly used after loading a library with dlopen to resolve functions or data objects at

The symbol resolution facilities are declared in <dlfcn.h>, and linking typically requires -ldl on many systems

Error handling and portability considerations are important. Before calling dlsym, it is common to clear any

runtime.
Once
a
symbol
is
found,
its
address
can
be
converted
to
the
appropriate
function
or
data
pointer
type
before
use.
This
enables
dynamic
plugins
and
optional
components
that
may
not
be
linked
at
build
time.
If
the
symbol
is
a
function,
you
typically
cast
the
obtained
void
*
to
a
function
pointer
with
the
correct
signature
before
invoking
it.
(though
the
exact
requirements
may
vary
by
platform).
The
RTLD_DEFAULT
and
RTLD_NEXT
handles
provide
flexible
search
behavior
within
the
process’s
global
symbol
space
or
the
dynamic
linker's
search
order.
existing
error
by
invoking
dlerror(),
then
call
dlsym,
and
finally
call
dlerror()
again
to
check
for
errors.
The
per-thread
error
string
retrieved
by
dlerror
helps
diagnose
issues
in
multi-threaded
programs.
In
C++,
access
to
C
symbols
may
require
extern
"C"
to
prevent
name
mangling.