1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
.\" -*- nroff -*-
.\" Copyright 1995 Yggdrasil Computing, Incorporated.
.\" written by Adam J. Richter (adam@yggdrasil.com),
.\" with typesetting help from Daniel Quinlan (quinlan@yggdrasil.com).
.\"
.\" This is free documentation; you can redistribute it and/or
.\" modify it under the terms of the GNU General Public License as
.\" published by the Free Software Foundation; either version 2 of
.\" the License, or (at your option) any later version.
.\"
.\" The GNU General Public License's references to "object code"
.\" and "executables" are to be interpreted as the output of any
.\" document formatting or typesetting system, including
.\" intermediate and printed output.
.\"
.\" This manual is distributed in the hope that it will be useful,
.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
.\" GNU General Public License for more details.
.\"
.\" You should have received a copy of the GNU General Public
.\" License along with this manual; if not, write to the Free
.\" Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
.\" USA.
.\"
.TH DLOPEN 3 "16 May 1995" "Linux" "Linux Programmer's Manual"
.SH NAME
dlclose, dlerror, dlopen, dlsym \- Programming interface to dynamic linking loader.
.SH SYNOPSIS
.B #include <dlfcn.h>
.sp
.BI "void *dlopen (const char *" "filename" ", int " flag ");
.br
.BI "const char *dlerror(void);"
.br
.BI "void *dlsym(void *"handle ", char *"symbol ");"
.br
.BI "int dladdr(void *"address ", Dl_info *"dlip ");"
.br
.BI "int dlclose (void *"handle ");
.sp
Special symbols:
.BR "_init" ", " "_fini" ". "
.SH DESCRIPTION
.B dlopen
loads a dynamic library from the file named by the null terminated
string
.I filename
and returns an opaque "handle" for the dynamic library.
If
.I filename
is not an absolute path (i.e., it does not begin with a "/"), then the
file is searched for in the following locations:
.RS
.PP
A colon-separated list of directories in the user's
\fBLD_LIBRARY\fP path environment variable.
.PP
The list of libraries specified in \fI/etc/ld.so.cache\fP.
.PP
\fI/usr/lib\fP, followed by \fI/lib\fP.
.RE
.PP
If
.I filename
is a NULL pointer, then the returned handle is for the main program.
.PP
External references in the library are resolved using the libraries
in that library's dependency list and any other libraries previously
opened with the
.B RTLD_GLOBAL
flag.
If the executable was linked
with the flag "-rdynamic", then the global symbols in the executable
will also be used to resolve references in a dynamically loaded
library.
.PP
.I flag
must be either
.BR RTLD_LAZY ,
meaning resolve undefined symbols as code from the dynamic library is
executed, or
.BR RTLD_NOW ,
meaning resolve all undefined symbols before
.B dlopen
returns, and fail if this cannot be done.
Optionally,
.B RTLD_GLOBAL
may be or'ed with
.IR flag,
in which case the external symbols defined in the library will be
made available to subsequently loaded libraries.
.PP
If the library exports a routine named
.BR _init ,
then that code is executed before dlopen returns.
If the same library is loaded twice with
.BR dlopen() ,
the same file handle is returned. The dl library maintains link
counts for dynamic file handles, so a dynamic library is not
deallocated until
.B dlclose
has been called on it as many times as
.B dlopen
has succeeded on it.
.PP
If
.B dlopen
fails for any reason, it returns NULL.
A human readable string describing the most recent error that occurred
from any of the dl routines (dlopen, dlsym or dlclose) can be
extracted with
.BR dlerror() .
.B dlerror
returns NULL if no errors have occurred since initialization or since
it was last called. (Calling
.B dlerror()
twice consecutively, will always result in the second call returning
NULL.)
.B dlsym
takes a "handle" of a dynamic library returned by dlopen and the null
terminated symbol name, returning the address where that symbol is
loaded. If the symbol is not found,
.B dlsym
returns NULL; however, the correct way to test for an error from
.B dlsym
is to save the result of
.B dlerror
into a variable, and then check if saved value is not NULL.
This is because the value of the symbol could actually be NULL.
It is also necessary to save the results of
.B dlerror
into a variable because if
.B dlerror
is called again, it will return NULL.
.PP
.B dladdr
returns information about the shared library containing the memory
location specified by
.IR address .
.B dladdr
returns zero on success and non-zero on error.
.PP
.B dlclose
decrements the reference count on the dynamic library handle
.IR handle .
If the reference count drops to zero and no other loaded libraries use
symbols in it, then the dynamic library is unloaded. If the dynamic
library exports a routine named
.BR _fini ,
then that routine is called just before the library is unloaded.
.SH EXAMPLES
.B Load the math library, and print the cosine of 2.0:
.RS
.nf
.if t .ft CW
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle = dlopen ("/lib/libm.so", RTLD_LAZY);
double (*cosine)(double) = dlsym(handle, "cos");
printf ("%f\\n", (*cosine)(2.0));
dlclose(handle);
}
.if t .ft P
.fi
.PP
If this program were in a file named "foo.c", you would build the program
with the following command:
.RS
.LP
gcc -rdynamic -o foo foo.c -ldl
.RE
.RE
.LP
.B Do the same thing, but check for errors at every step:
.RS
.nf
.if t .ft CW
#include <stdio.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
double (*cosine)(double);
char *error;
handle = dlopen ("/lib/libm.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
cosine = dlsym(handle, "cos");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
printf ("%f\\n", (*cosine)(2.0));
dlclose(handle);
}
.if t .ft P
.fi
.RE
.SH ACKNOWLEDGEMENTS
The dlopen interface standard comes from Solaris.
The Linux dlopen implementation was primarily written by
Eric Youngdale with help from Mitch D'Souza, David Engel,
Hongjiu Lu, Andreas Schwab and others.
The manual page was written by Adam Richter.
.SH SEE ALSO
.BR ld(1) ,
.BR ld.so(8) ,
.BR ldconfig(8) ,
.BR ldd(1) ,
.BR ld.so.info .
|