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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
.\" $OpenBSD: dlfcn.3,v 1.31 2021/06/02 07:29:03 semarie Exp $
.\" $NetBSD: dlfcn.3,v 1.3 1996/01/09 19:43:34 pk Exp $
.\"
.\" Copyright (c) 1995 Paul Kranenburg
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by Paul Kranenburg.
.\" 3. The name of the author may not be used to endorse or promote products
.\" derived from this software without specific prior written permission
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd $Mdocdate: June 2 2021 $
.Dt DLOPEN 3
.Os
.Sh NAME
.Nm dlopen ,
.Nm dlclose ,
.Nm dlsym ,
.Nm dladdr ,
.Nm dlctl ,
.Nm dlerror
.Nd dynamic link interface
.Sh SYNOPSIS
.In dlfcn.h
.Ft "void *"
.Fn dlopen "const char *path" "int mode"
.Ft "int"
.Fn dlclose "void *handle"
.Ft "void *"
.Fn dlsym "void *handle" "const char *symbol"
.Ft "int"
.Fn dladdr "const void *addr" "Dl_info *info"
.Ft "int"
.Fn dlctl "void *handle" "int cmd" "void *data"
.Ft "char *"
.Fn dlerror "void"
.Sh DESCRIPTION
These functions provide an interface to the run-time linker
.Xr ld.so 1 .
They allow new shared objects to be loaded into a process's address space
under program control.
.Pp
The
.Fn dlopen
function takes the name of a shared object as its first argument.
The shared object is mapped into the address space, relocated, and its external
references are resolved in the same way as is done with the implicitly loaded
shared libraries at program startup.
.Pp
The
.Fa path
parameter can be specified as either an absolute pathname to a shared library
or just the name of the shared library itself.
When an absolute pathname is specified,
only the path provided will be searched for the shared library.
When just a shared library is specified,
the same paths will be searched that are used for
.Dq intrinsic
shared library searches.
.Pp
Shared libraries take the following form:
.Pp
.Dl lib<name>.so[.xx[.yy]]
.Pp
When a shared library is specified without a version or with a partial version,
the same library search rules apply that are used for
.Dq intrinsic
shared library searches.
A null pointer supplied for
.Fa path
will return a special
.Fa handle
that behaves the same as the
.Dv RTLD_DEFAULT
special
.Fa handle .
.Pp
The
.Fa mode
parameter specifies symbol resolution time and symbol visibility.
One of the following values may be used to specify symbol resolution time:
.Bl -tag -width "RTLD_LAZYXX" -offset indent
.It Sy RTLD_NOW
Symbols are resolved immediately.
.It Sy RTLD_LAZY
Symbols are resolved when they are first referred to.
This is the default value if resolution time is unspecified.
.El
.Pp
One of the following values may be used to specify symbol visibility:
.Pp
.Bl -tag -width "RTLD_GLOBAL" -compact -offset indent
.It Sy RTLD_GLOBAL
The object's symbols and the symbols of its dependencies will be visible to
other objects.
.It Sy RTLD_LOCAL
The object's symbols and the symbols of its dependencies will not be visible to
other objects.
This is the default value if visibility is unspecified.
.El
.Pp
To specify both resolution time and visibility, bitwise inclusive OR one of
each of the above values together.
If an object was opened with RTLD_LOCAL and later opened with RTLD_GLOBAL,
then it is promoted to RTLD_GLOBAL.
.Pp
Additionally, the following flag may be ORed into the mode argument:
.Pp
.Bl -tag -width "RTLD_NODELETE" -compact -offset indent
.It Sy RTLD_NODELETE
Prevents unload of the loaded object on
.Fn dlclose .
.El
.Pp
The main executable's symbols are normally invisible to
.Fn dlopen
symbol resolution.
Those symbols will be visible if linking is done with
.Xr gcc 1
.Fl rdynamic ,
which is equivalent to
.Xr ld 1
.Fl -export-dynamic .
.Pp
All shared objects loaded at program startup are globally visible.
.Pp
.Fn dlopen
returns a
.Fa handle
to be used in calls to
.Fn dlclose ,
.Fn dlsym ,
and
.Fn dlctl .
If the named shared object has already been loaded by a previous call to
.Fn dlopen
and not yet unloaded by
.Fn dlclose ,
a
.Fa handle
referring to the resident copy is returned.
.Pp
.Fn dlclose
unlinks and removes the object referred to by
.Fa handle
from the process address space.
If multiple calls to
.Fn dlopen
have been done on this object or the object is a dependency of another object
then the object is removed when its reference count drops to zero.
.Fn dlclose
returns 0 on success and non-zero on failure.
.Pp
.Fn dlsym
searches for a definition of
.Fa symbol
in the object designated by
.Fa handle
and all shared objects that it depends on.
The symbol's address is returned.
If the symbol cannot be resolved,
.Dv NULL
is returned.
.Pp
.Fn dlsym
may also be called with special
.Fa handles .
.Fn dlsym
respects symbol visibility as specified by the
.Fn dlopen
.Fa mode
parameter.
However, the symbols of an object's dependencies are always visible to it.
The following special
.Fa handles
may be used with
.Fn dlsym :
.Bl -tag -width "RTLD_DEFAULTXX" -offset indent
.It Sy NULL
Interpreted as a reference to the executable or shared object
from which the call is being made.
Thus an object can reference its own symbols and the symbols of its
dependencies without calling
.Fn dlopen .
.It Sy RTLD_DEFAULT
All the visible shared objects and the executable will be searched in the order they
were loaded.
.It Sy RTLD_NEXT
The search for
.Fa symbol
is limited to the visible shared objects which were loaded after the one issuing the
call to
.Fn dlsym .
Thus, if
.Fn dlsym
is called from the main program, all the visible shared libraries are searched.
If it is called from a shared library, all subsequently visible shared
libraries are searched.
.It Sy RTLD_SELF
The search for
.Fa symbol
is limited to the shared object issuing the call to
.Fn dlsym
and those shared objects which were loaded after it that are visible.
.El
.Pp
.Fn dladdr
queries the dynamic linker for information about the shared object
containing the address
.Fa addr .
The information is returned in the structure specified by
.Fa info .
The structure contains at least the following members:
.Bl -tag -width "XXXconst char *dli_fname"
.It Li "const char *dli_fname"
The pathname of the shared object containing the address
.Fa addr .
.It Li "void *dli_fbase"
The base address at which the shared object is mapped into the
address space of the calling process.
.It Li "const char *dli_sname"
The name of the nearest run-time symbol with an address less than or
equal to
.Fa addr .
.Pp
If no symbol with a suitable address is found, both this field and
.Va dli_saddr
are set to
.Dv NULL .
.It Li "void *dli_saddr"
The address of the symbol returned in
.Va dli_sname .
.El
.Pp
If a mapped shared object containing
.Fa addr
cannot be found,
.Fn dladdr
returns 0.
In that case, a message detailing the failure can be retrieved by
calling
.Fn dlerror .
On success, a non-zero value is returned.
Note: both strings pointed at by
.Va dli_fname
and
.Va dli_sname
reside in memory private to the run-time linker module and should not
be modified by the caller.
.Pp
In dynamically linked programs, the address of a global function will
point to its program linkage table entry, rather than to the entry
point of the function itself.
This causes most global functions to appear to be defined within the
main executable, rather than in the shared libraries where the actual
code resides.
.Pp
.Fn dlctl
provides an interface similar to
.Xr ioctl 2
to control several aspects of the run-time linker's operation.
This interface is currently under development.
.Pp
.Fn dlerror
returns a character string representing the most recent error that has
occurred while processing one of the other functions described here.
If no dynamic linking errors have occurred since the last invocation of
.Fn dlerror ,
.Fn dlerror
returns
.Dv NULL .
Thus, invoking
.Fn dlerror
a second time, immediately following a prior invocation, will result in
.Dv NULL
being returned.
.Sh SEE ALSO
.Xr ld 1 ,
.Xr ld.so 1 ,
.Xr elf 5
.Sh HISTORY
Some of the
.Nm dl*
functions first appeared in SunOS 4.
|