man-pages/man3/dlsym.3.html
2021-03-31 01:06:50 +01:00

273 lines
6.5 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of DLSYM</TITLE>
</HEAD><BODY>
<H1>DLSYM</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
dlsym, dlvsym - obtain address of a symbol in a shared object or executable
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<B>#include &lt;<A HREF="file:///usr/include/dlfcn.h">dlfcn.h</A>&gt;</B>
<P>
<B>void *dlsym(void *</B><I>handle</I><B>, const char *</B><I>symbol</I><B>);</B>
<P>
<B>#define _GNU_SOURCE</B>
<BR>
<B>#include &lt;<A HREF="file:///usr/include/dlfcn.h">dlfcn.h</A>&gt;</B>
<P>
<B>void *dlvsym(void *</B><I>handle</I><B>, char *</B><I>symbol</I><B>, char *</B><I>version</I><B>);</B>
<P>
Link with <I>-ldl</I>.
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The function
<B>dlsym</B>()
takes a &quot;handle&quot; of a dynamic loaded shared object returned by
<B><A HREF="/cgi-bin/man/man2html?3+dlopen">dlopen</A></B>(3)
along with a null-terminated symbol name,
and returns the address where that symbol is
loaded into memory.
If the symbol is not found, in the specified
object or any of the shared objects that were automatically loaded by
<B><A HREF="/cgi-bin/man/man2html?3+dlopen">dlopen</A></B>(3)
when that object was loaded,
<B>dlsym</B>()
returns NULL.
(The search performed by
<B>dlsym</B>()
is breadth first through the dependency tree of these shared objects.)
<P>
In unusual cases (see NOTES) the value of the symbol could actually be NULL.
Therefore, a NULL return from
<B>dlsym</B>()
need not indicate an error.
The correct way to distinguish an error from a symbol whose value is NULL
is to call
<B><A HREF="/cgi-bin/man/man2html?3+dlerror">dlerror</A></B>(3)
to clear any old error conditions, then call
<B>dlsym</B>(),
and then call
<B><A HREF="/cgi-bin/man/man2html?3+dlerror">dlerror</A></B>(3)
again, saving its return value into a variable, and check whether
this saved value is not NULL.
<P>
There are two special pseudo-handles that may be specified in
<I>handle</I>:
<DL COMPACT>
<DT id="1"><B>RTLD_DEFAULT</B>
<DD>
Find the first occurrence of the desired symbol
using the default shared object search order.
The search will include global symbols in the executable
and its dependencies,
as well as symbols in shared objects that were dynamically loaded with the
<B>RTLD_GLOBAL</B>
flag.
<DT id="2"><B>RTLD_NEXT</B>
<DD>
Find the next occurrence of the desired symbol in the search order
after the current object.
This allows one to provide a wrapper
around a function in another shared object, so that, for example,
the definition of a function in a preloaded shared object
(see
<B>LD_PRELOAD</B>
in
<B><A HREF="/cgi-bin/man/man2html?8+ld.so">ld.so</A></B>(8))
can find and invoke the &quot;real&quot; function provided in another shared object
(or for that matter, the &quot;next&quot; definition of the function in cases
where there are multiple layers of preloading).
</DL>
<P>
The
<B>_GNU_SOURCE</B>
feature test macro must be defined in order to obtain the
definitions of
<B>RTLD_DEFAULT</B>
and
<B>RTLD_NEXT</B>
from
<I>&lt;<A HREF="file:///usr/include/dlfcn.h">dlfcn.h</A>&gt;</I>.
<P>
<P>
The function
<B>dlvsym</B>()
does the same as
<B>dlsym</B>()
but takes a version string as an additional argument.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success,
these functions return the address associated with
<I>symbol</I>.
On failure, they return NULL;
the cause of the error can be diagnosed using
<B><A HREF="/cgi-bin/man/man2html?3+dlerror">dlerror</A></B>(3).
<A NAME="lbAF">&nbsp;</A>
<H2>VERSIONS</H2>
<B>dlsym</B>()
is present in glibc 2.0 and later.
<B>dlvsym</B>()
first appeared in glibc 2.1.
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>dlsym</B>(),
<B>dlvsym</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001 describes
<B>dlsym</B>().
The
<B>dlvsym</B>()
function is a GNU extension.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
The value of a symbol returned by
<B>dlsym</B>()
will never be NULL if the shared object is the result of normal compilation,
since a global symbol is never placed at the NULL address.
There are nevertheless cases where a lookup using
<B>dlsym</B>()
may return NULL as the value of a symbol.
For example, the symbol value may be the result of
a GNU indirect function (IFUNC) resolver function that returns
NULL as the resolved value.
<A NAME="lbAJ">&nbsp;</A>
<H3>History</H3>
The
<B>dlsym</B>()
function is part of the dlopen API, derived from SunOS.
That system does not have
<B>dlvsym</B>().
<A NAME="lbAK">&nbsp;</A>
<H2>EXAMPLE</H2>
See
<B><A HREF="/cgi-bin/man/man2html?3+dlopen">dlopen</A></B>(3).
<A NAME="lbAL">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?3+dl_iterate_phdr">dl_iterate_phdr</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+dladdr">dladdr</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+dlerror">dlerror</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+dlinfo">dlinfo</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+dlopen">dlopen</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?8+ld.so">ld.so</A></B>(8)
<A NAME="lbAM">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="3"><A HREF="#lbAB">NAME</A><DD>
<DT id="4"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="5"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="6"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="7"><A HREF="#lbAF">VERSIONS</A><DD>
<DT id="8"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="9"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="10"><A HREF="#lbAI">NOTES</A><DD>
<DL>
<DT id="11"><A HREF="#lbAJ">History</A><DD>
</DL>
<DT id="12"><A HREF="#lbAK">EXAMPLE</A><DD>
<DT id="13"><A HREF="#lbAL">SEE ALSO</A><DD>
<DT id="14"><A HREF="#lbAM">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:38 GMT, March 31, 2021
</BODY>
</HTML>