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