2 .\" Copyright 1995 Yggdrasil Computing, Incorporated.
3 .\" written by Adam J. Richter (adam@yggdrasil.com),
4 .\" with typesetting help from Daniel Quinlan (quinlan@yggdrasil.com).
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, see
23 .\" <http://www.gnu.org/licenses/>.
25 .TH DLOPEN 3 "16 May 1995" "Linux" "Linux Programmer's Manual"
27 dlclose, dlerror, dlopen, dlsym \- Programming interface to dynamic linking loader.
31 .BI "void *dlopen (const char *" "filename" ", int " flag ");
33 .BI "const char *dlerror(void);"
35 .BI "void *dlsym(void *"handle ", char *"symbol ");"
37 .BI "int dladdr(void *"address ", Dl_info *"dlip ");"
39 .BI "int dlclose (void *"handle ");
42 .BR "_init" ", " "_fini" ". "
45 loads a dynamic library from the file named by the null terminated
48 and returns an opaque "handle" for the dynamic library.
51 is not an absolute path (i.e., it does not begin with a "/"), then the
52 file is searched for in the following locations:
55 A colon-separated list of directories in the user's
56 \fBLD_LIBRARY\fP path environment variable.
58 The list of libraries specified in \fI/etc/ld.so.cache\fP.
60 \fI/usr/lib\fP, followed by \fI/lib\fP.
65 is a NULL pointer, then the returned handle is for the main program.
67 External references in the library are resolved using the libraries
68 in that library's dependency list and any other libraries previously
72 If the executable was linked
73 with the flag "-rdynamic", then the global symbols in the executable
74 will also be used to resolve references in a dynamically loaded
80 meaning resolve undefined symbols as code from the dynamic library is
83 meaning resolve all undefined symbols before
85 returns, and fail if this cannot be done.
90 in which case the external symbols defined in the library will be
91 made available to subsequently loaded libraries.
93 If the library exports a routine named
95 then that code is executed before dlopen returns.
96 If the same library is loaded twice with
98 the same file handle is returned. The dl library maintains link
99 counts for dynamic file handles, so a dynamic library is not
102 has been called on it as many times as
108 fails for any reason, it returns NULL.
109 A human readable string describing the most recent error that occurred
110 from any of the dl routines (dlopen, dlsym or dlclose) can be
114 returns NULL if no errors have occurred since initialization or since
115 it was last called. (Calling
117 twice consecutively, will always result in the second call returning
121 takes a "handle" of a dynamic library returned by dlopen and the null
122 terminated symbol name, returning the address where that symbol is
123 loaded. If the symbol is not found,
125 returns NULL; however, the correct way to test for an error from
127 is to save the result of
129 into a variable, and then check if saved value is not NULL.
130 This is because the value of the symbol could actually be NULL.
131 It is also necessary to save the results of
133 into a variable because if
135 is called again, it will return NULL.
138 returns information about the shared library containing the memory
139 location specified by
142 returns zero on success and non-zero on error.
145 decrements the reference count on the dynamic library handle
147 If the reference count drops to zero and no other loaded libraries use
148 symbols in it, then the dynamic library is unloaded. If the dynamic
149 library exports a routine named
151 then that routine is called just before the library is unloaded.
153 .B Load the math library, and print the cosine of 2.0:
159 int main(int argc, char **argv) {
160 void *handle = dlopen ("/lib/libm.so", RTLD_LAZY);
161 double (*cosine)(double) = dlsym(handle, "cos");
162 printf ("%f\\n", (*cosine)(2.0));
168 If this program were in a file named "foo.c", you would build the program
169 with the following command:
172 gcc -rdynamic -o foo foo.c -ldl
176 .B Do the same thing, but check for errors at every step:
183 int main(int argc, char **argv) {
185 double (*cosine)(double);
188 handle = dlopen ("/lib/libm.so", RTLD_LAZY);
190 fputs (dlerror(), stderr);
194 cosine = dlsym(handle, "cos");
195 if ((error = dlerror()) != NULL) {
196 fputs(error, stderr);
200 printf ("%f\\n", (*cosine)(2.0));
207 The dlopen interface standard comes from Solaris.
208 The Linux dlopen implementation was primarily written by
209 Eric Youngdale with help from Mitch D'Souza, David Engel,
210 Hongjiu Lu, Andreas Schwab and others.
211 The manual page was written by Adam Richter.