Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libcr / gen / dlopen.3
1 .\" This source code is a product of Sun Microsystems, Inc. and is provided
2 .\" for unrestricted use provided that this legend is included on all tape
3 .\" media and as a part of the software program in whole or part.  Users
4 .\" may copy or modify this source code without charge, but are not authorized
5 .\" to license or distribute it to anyone else except as part of a product or
6 .\" program developed by the user.
7 .\"
8 .\" THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC.
9 .\" SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY
10 .\" OF SUCH SOURCE CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT
11 .\" EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  SUN MICROSYSTEMS, INC. DISCLAIMS
12 .\" ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED
13 .\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  IN
14 .\" NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT,
15 .\" INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
16 .\" FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY.
17 .\"
18 .\" This source code is provided with no support and without any obligation on
19 .\" the part of Sun Microsystems, Inc. to assist in its use, correction,
20 .\" modification or enhancement.
21 .\"
22 .\" SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
23 .\" INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
24 .\" SOURCE CODE OR ANY PART THEREOF.
25 .\"
26 .\" Sun Microsystems, Inc.
27 .\" 2550 Garcia Avenue
28 .\" Mountain View, California 94043
29 .\"
30 .\" Copyright (c) 1991 Sun Microsystems, Inc.
31 .\"
32 .\" @(#) dlopen.3 1.6 90/01/31 SMI
33 .\" $FreeBSD: src/lib/libc/gen/dlopen.3,v 1.8.2.10 2003/03/15 15:11:05 trhodes Exp $
34 .\"
35 .Dd September 24, 1989
36 .Os
37 .Dt DLOPEN 3
38 .Sh NAME
39 .Nm dlopen ,
40 .Nm dlsym ,
41 .Nm dlerror ,
42 .Nm dlclose
43 .Nd programmatic interface to the dynamic linker
44 .Sh LIBRARY
45 .Lb libc
46 .Sh SYNOPSIS
47 .In dlfcn.h
48 .Ft void *
49 .Fn dlopen "const char *path" "int mode"
50 .Ft void *
51 .Fn dlsym "void *handle" "const char *symbol"
52 .Ft const char *
53 .Fn dlerror "void"
54 .Ft int
55 .Fn dlclose "void *handle"
56 .Sh DESCRIPTION
57 These functions provide a simple programmatic interface to the services of the
58 dynamic linker.
59 Operations are provided to add new shared objects to a
60 program's address space, to obtain the address bindings of symbols
61 defined by such
62 objects, and to remove such objects when their use is no longer required.
63 .Pp
64 The
65 .Fn dlopen
66 function
67 provides access to the shared object in
68 .Fa path ,
69 returning a descriptor that can be used for later
70 references to the object in calls to
71 .Fn dlsym
72 and
73 .Fn dlclose .
74 If
75 .Fa path
76 was not in the address space prior to the call to
77 .Fn dlopen ,
78 it is placed in the address space.
79 When an object is first loaded into the address space in this way, its
80 function
81 .Fn _init ,
82 if any, is called by the dynamic linker.
83 If
84 .Fa path
85 has already been placed in the address space in a previous call to
86 .Fn dlopen ,
87 it is not added a second time, although a reference count of
88 .Fn dlopen
89 operations on
90 .Fa path
91 is maintained.
92 A null pointer supplied for
93 .Fa path
94 is interpreted as a reference to the main
95 executable of the process.
96 The
97 .Fa mode
98 argument
99 controls the way in which external function references from the
100 loaded object are bound to their referents.
101 It must contain one of the following values, possibly ORed with
102 additional flags which will be described subsequently:
103 .Bl -tag -width RTLD_LAZYX
104 .It Dv RTLD_LAZY
105 Each external function reference is resolved when the function is first
106 called.
107 .It Dv RTLD_NOW
108 All external function references are bound immediately by
109 .Fn dlopen .
110 .El
111 .Pp
112 .Dv RTLD_LAZY
113 is normally preferred, for reasons of efficiency.
114 However,
115 .Dv RTLD_NOW
116 is useful to ensure that any undefined symbols are discovered during the
117 call to
118 .Fn dlopen .
119 .Pp
120 One of the following flags may be ORed into the
121 .Fa mode
122 argument:
123 .Bl -tag -width RTLD_GLOBALX
124 .It Dv RTLD_GLOBAL
125 Symbols from this shared object and its directed acyclic graph (DAG)
126 of needed objects will be available for resolving undefined references
127 from all other shared objects.
128 .It Dv RTLD_LOCAL
129 Symbols in this shared object and its DAG of needed objects will be
130 available for resolving undefined references only from other objects
131 in the same DAG.
132 This is the default, but it may be specified
133 explicitly with this flag.
134 .It Dv RTLD_TRACE
135 When set, causes dynamic linker to exit after loading all objects
136 needed by this shared object and printing a summary which includes
137 the absolute pathnames of all objects, to standard output.
138 With this flag
139 .Fn dlopen
140 will return to the caller only in the case of error.
141 .El
142 .Pp
143 If
144 .Fn dlopen
145 fails, it returns a null pointer, and sets an error condition which may
146 be interrogated with
147 .Fn dlerror .
148 .Pp
149 The
150 .Fn dlsym
151 function
152 returns the address binding of the symbol described in the null-terminated
153 character string
154 .Fa symbol ,
155 as it occurs in the shared object identified by
156 .Fa handle .
157 The symbols exported by objects added to the address space by
158 .Fn dlopen
159 can be accessed only through calls to
160 .Fn dlsym .
161 Such symbols do not supersede any definition of those symbols already present
162 in the address space when the object is loaded, nor are they available to
163 satisfy normal dynamic linking references.
164 .Pp
165 If
166 .Fn dlsym
167 is called with the special
168 .Fa handle
169 .Dv NULL ,
170 it is interpreted as a reference to the executable or shared object
171 from which the call
172 is being made.
173 Thus a shared object can reference its own symbols.
174 .Pp
175 If
176 .Fn dlsym
177 is called with the special
178 .Fa handle
179 .Dv RTLD_DEFAULT ,
180 the search for the symbol follows the algorithm used for resolving
181 undefined symbols when objects are loaded.
182 The objects searched are
183 as follows, in the given order:
184 .Bl -enum
185 .It
186 The referencing object itself (or the object from which the call to
187 .Fn dlsym
188 is made), if that object was linked using the
189 .Fl Wsymbolic
190 option to
191 .Xr ld 1 .
192 .It
193 All objects loaded at program start-up.
194 .It
195 All objects loaded via
196 .Fn dlopen
197 which are in needed-object DAGs that also contain the referencing object.
198 .It
199 All objects loaded via
200 .Fn dlopen
201 with the
202 .Dv RTLD_GLOBAL
203 flag set in the
204 .Fa mode
205 argument.
206 .El
207 .Pp
208 If
209 .Fn dlsym
210 is called with the special
211 .Fa handle
212 .Dv RTLD_NEXT ,
213 then the search for the symbol is limited to the shared objects
214 which were loaded after the one issuing the call to
215 .Fn dlsym .
216 Thus, if the function is called from the main program, all
217 the shared libraries are searched.
218 If it is called from a shared library, all subsequent shared
219 libraries are searched.
220 .Dv RTLD_NEXT
221 is useful for implementing wrappers around library functions.
222 For example, a wrapper function
223 .Fn getpid
224 could access the
225 .Dq real
226 .Fn getpid
227 with
228 .Li dlsym(RTLD_NEXT, \&"getpid\&") .
229 .Pp
230 If
231 .Fn dlsym
232 is called with the special
233 .Fa handle
234 .Dv RTLD_SELF ,
235 then the search for the symbol is limited to the shared object
236 issuing the call to
237 .Fn dlsym
238 and those shared objects which were loaded after it.
239 .Pp
240 The
241 .Fn dlsym
242 function
243 returns a null pointer if the symbol cannot be found, and sets an error
244 condition which may be queried with
245 .Fn dlerror .
246 .Pp
247 The
248 .Fn dlerror
249 function
250 returns a null-terminated character string describing the last error that
251 occurred during a call to
252 .Fn dlopen ,
253 .Fn dladdr ,
254 .Fn dlinfo ,
255 .Fn dlsym ,
256 or
257 .Fn dlclose .
258 If no such error has occurred,
259 .Fn dlerror
260 returns a null pointer.
261 At each call to
262 .Fn dlerror ,
263 the error indication is reset.
264 Thus in the case of two calls
265 to
266 .Fn dlerror ,
267 where the second call follows the first immediately, the second call
268 will always return a null pointer.
269 .Pp
270 The
271 .Fn dlclose
272 function
273 deletes a reference to the shared object referenced by
274 .Fa handle .
275 If the reference count drops to 0, the object is removed from the
276 address space, and
277 .Fa handle
278 is rendered invalid.
279 Just before removing a shared object in this way, the dynamic linker
280 calls the object's
281 .Fn _fini
282 function, if such a function is defined by the object.
283 If
284 .Fn dlclose
285 is successful, it returns a value of 0.
286 Otherwise it returns -1, and sets an error condition that can be
287 interrogated with
288 .Fn dlerror .
289 .Pp
290 The object-intrinsic functions
291 .Fn _init
292 and
293 .Fn _fini
294 are called with no arguments, and are not expected to return values.
295 .Sh NOTES
296 ELF executables need to be linked
297 using the
298 .Fl export-dynamic
299 option to
300 .Xr ld 1
301 for symbols defined in the executable to become visible to
302 .Fn dlsym .
303 .Pp
304 In previous implementations, it was necessary to prepend an underscore
305 to all external symbols in order to gain symbol
306 compatibility with object code compiled from the C language.
307 This is
308 still the case when using the (obsolete)
309 .Fl aout
310 option to the C language compiler.
311 .Sh ERRORS
312 The
313 .Fn dlopen
314 and
315 .Fn dlsym
316 functions
317 return a null pointer in the event of errors.
318 The
319 .Fn dlclose
320 function
321 returns 0 on success, or -1 if an error occurred.
322 Whenever an error has been detected, a message detailing it can be
323 retrieved via a call to
324 .Fn dlerror .
325 .Sh SEE ALSO
326 .Xr ld 1 ,
327 .Xr rtld 1 ,
328 .Xr dladdr 3 ,
329 .Xr dlinfo 3 ,
330 .Xr link 5