xref: /freebsd/lib/libc/gen/dlopen.3 (revision 2008043f386721d58158e37e0d7e50df8095942d)
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.\"
34.Dd May 14, 2020
35.Dt DLOPEN 3
36.Os
37.Sh NAME
38.Nm dlopen ,
39.Nm fdlopen ,
40.Nm dlsym ,
41.Nm dlvsym ,
42.Nm dlfunc ,
43.Nm dlerror ,
44.Nm dlclose
45.Nd programmatic interface to the dynamic linker
46.Sh LIBRARY
47.Lb libc
48.Sh SYNOPSIS
49.In dlfcn.h
50.Ft void *
51.Fn dlopen "const char *path" "int mode"
52.Ft void *
53.Fn fdlopen "int fd" "int mode"
54.Ft void *
55.Fn dlsym "void * restrict handle" "const char * restrict symbol"
56.Ft void *
57.Fn dlvsym "void * restrict handle" "const char * restrict symbol" "const char * restrict version"
58.Ft dlfunc_t
59.Fn dlfunc "void * restrict handle" "const char * restrict symbol"
60.Ft char *
61.Fn dlerror "void"
62.Ft int
63.Fn dlclose "void *handle"
64.Sh DESCRIPTION
65These functions provide a simple programmatic interface to the services of the
66dynamic linker.
67Operations are provided to add new shared objects to a
68program's address space, to obtain the address bindings of symbols
69defined by such
70objects, and to remove such objects when their use is no longer required.
71.Pp
72The
73.Fn dlopen
74function
75provides access to the shared object in
76.Fa path ,
77returning a descriptor that can be used for later
78references to the object in calls to
79.Fn dlsym ,
80.Fn dlvsym
81and
82.Fn dlclose .
83If
84.Fa path
85was not in the address space prior to the call to
86.Fn dlopen ,
87it is placed in the address space.
88When an object is first loaded into the address space in this way, its
89function
90.Fn _init ,
91if any, is called by the dynamic linker.
92If
93.Fa path
94has already been placed in the address space in a previous call to
95.Fn dlopen ,
96it is not added a second time, although a reference count of
97.Fn dlopen
98operations on
99.Fa path
100is maintained.
101A null pointer supplied for
102.Fa path
103is interpreted as a reference to the main
104executable of the process.
105The
106.Fa mode
107argument
108controls the way in which external function references from the
109loaded object are bound to their referents.
110It must contain one of the following values, possibly ORed with
111additional flags which will be described subsequently:
112.Bl -tag -width RTLD_LAZYX
113.It Dv RTLD_LAZY
114Each external function reference is resolved when the function is first
115called.
116.It Dv RTLD_NOW
117All external function references are bound immediately by
118.Fn dlopen .
119.El
120.Pp
121.Dv RTLD_LAZY
122is normally preferred, for reasons of efficiency.
123However,
124.Dv RTLD_NOW
125is useful to ensure that any undefined symbols are discovered during the
126call to
127.Fn dlopen .
128.Pp
129One of the following flags may be ORed into the
130.Fa mode
131argument:
132.Bl -tag -width RTLD_NODELETE
133.It Dv RTLD_GLOBAL
134Symbols from this shared object and its directed acyclic graph (DAG)
135of needed objects will be available for resolving undefined references
136from all other shared objects.
137.It Dv RTLD_LOCAL
138Symbols in this shared object and its DAG of needed objects will be
139available for resolving undefined references only from other objects
140in the same DAG.
141This is the default, but it may be specified
142explicitly with this flag.
143.It Dv RTLD_TRACE
144When set, causes dynamic linker to exit after loading all objects
145needed by this shared object and printing a summary which includes
146the absolute pathnames of all objects, to standard output.
147With this flag
148.Fn dlopen
149will return to the caller only in the case of error.
150.It Dv RTLD_NODELETE
151Prevents unload of the loaded object on
152.Fn dlclose .
153The same behaviour may be requested by
154.Fl "z nodelete"
155option of the static linker
156.Xr ld 1 .
157.It Dv RTLD_NOLOAD
158Only return valid handle for the object if it is already loaded in
159the process address space, otherwise
160.Dv NULL
161is returned.
162Other mode flags may be specified, which will be applied for promotion
163for the found object.
164.It Dv RTLD_DEEPBIND
165Symbols from the loaded library are put before global symbols when
166resolving symbolic references originated from the library.
167.El
168.Pp
169If
170.Fn dlopen
171fails, it returns a null pointer, and sets an error condition which may
172be interrogated with
173.Fn dlerror .
174.Pp
175The
176.Fn fdlopen
177function is similar to
178.Fn dlopen ,
179but it takes the file descriptor argument
180.Fa fd ,
181which is used for the file operations needed to load an object
182into the address space.
183The file descriptor
184.Fa fd
185is not closed by the function regardless a result of execution,
186but a duplicate of the file descriptor is.
187This may be important if a
188.Xr lockf 3
189lock is held on the passed descriptor.
190The
191.Fa fd
192argument -1 is interpreted as a reference to the main
193executable of the process, similar to
194.Va NULL
195value for the
196.Fa name
197argument to
198.Fn dlopen .
199The
200.Fn fdlopen
201function can be used by the code that needs to perform
202additional checks on the loaded objects, to prevent races with
203symlinking or renames.
204.Pp
205The
206.Fn dlsym
207function
208returns the address binding of the symbol described in the null-terminated
209character string
210.Fa symbol ,
211as it occurs in the shared object identified by
212.Fa handle .
213The symbols exported by objects added to the address space by
214.Fn dlopen
215can be accessed only through calls to
216.Fn dlsym .
217Such symbols do not supersede any definition of those symbols already present
218in the address space when the object is loaded, nor are they available to
219satisfy normal dynamic linking references.
220.Pp
221If
222.Fn dlsym
223is called with the special
224.Fa handle
225.Dv NULL ,
226it is interpreted as a reference to the executable or shared object
227from which the call
228is being made.
229Thus a shared object can reference its own symbols.
230.Pp
231If
232.Fn dlsym
233is called with the special
234.Fa handle
235.Dv RTLD_DEFAULT ,
236the search for the symbol follows the algorithm used for resolving
237undefined symbols when objects are loaded.
238The objects searched are
239as follows, in the given order:
240.Bl -enum
241.It
242The referencing object itself (or the object from which the call to
243.Fn dlsym
244is made), if that object was linked using the
245.Fl Bsymbolic
246option to
247.Xr ld 1 .
248.It
249All objects loaded at program start-up.
250.It
251All objects loaded via
252.Fn dlopen
253with the
254.Dv RTLD_GLOBAL
255flag set in the
256.Fa mode
257argument.
258.It
259All objects loaded via
260.Fn dlopen
261which are in needed-object DAGs that also contain the referencing object.
262.El
263.Pp
264If
265.Fn dlsym
266is called with the special
267.Fa handle
268.Dv RTLD_NEXT ,
269then the search for the symbol is limited to the shared objects
270which were loaded after the one issuing the call to
271.Fn dlsym .
272Thus, if the function is called from the main program, all
273the shared libraries are searched.
274If it is called from a shared library, all subsequent shared
275libraries are searched.
276.Dv RTLD_NEXT
277is useful for implementing wrappers around library functions.
278For example, a wrapper function
279.Fn getpid
280could access the
281.Dq real
282.Fn getpid
283with
284.Li dlsym(RTLD_NEXT, \&"getpid\&") .
285(Actually, the
286.Fn dlfunc
287interface, below, should be used, since
288.Fn getpid
289is a function and not a data object.)
290.Pp
291If
292.Fn dlsym
293is called with the special
294.Fa handle
295.Dv RTLD_SELF ,
296then the search for the symbol is limited to the shared object
297issuing the call to
298.Fn dlsym
299and those shared objects which were loaded after it.
300.Pp
301The
302.Fn dlsym
303function
304returns a null pointer if the symbol cannot be found, and sets an error
305condition which may be queried with
306.Fn dlerror .
307.Pp
308The
309.Fn dlvsym
310function behaves like
311.Fn dlsym ,
312but takes an extra argument
313.Fa version :
314a null-terminated character string which is used to request a specific version
315of
316.Fa symbol .
317.Pp
318The
319.Fn dlfunc
320function
321implements all of the behavior of
322.Fn dlsym ,
323but has a return type which can be cast to a function pointer without
324triggering compiler diagnostics.
325(The
326.Fn dlsym
327function
328returns an object pointer; in the C standard, conversions between
329object and function pointer types are undefined.
330Some compilers and lint utilities warn about such casts.)
331The precise return type of
332.Fn dlfunc
333is unspecified; applications must cast it to an appropriate function pointer
334type.
335.Pp
336The
337.Fn dlerror
338function
339returns a null-terminated character string describing the last error that
340occurred during a call to
341.Fn dlopen ,
342.Fn dladdr ,
343.Fn dlinfo ,
344.Fn dlsym ,
345.Fn dlvsym ,
346.Fn dlfunc ,
347or
348.Fn dlclose .
349If no such error has occurred,
350.Fn dlerror
351returns a null pointer.
352At each call to
353.Fn dlerror ,
354the error indication is reset.
355Thus in the case of two calls
356to
357.Fn dlerror ,
358where the second call follows the first immediately, the second call
359will always return a null pointer.
360.Pp
361The
362.Fn dlclose
363function
364deletes a reference to the shared object referenced by
365.Fa handle .
366If the reference count drops to 0, the object is removed from the
367address space, and
368.Fa handle
369is rendered invalid.
370Just before removing a shared object in this way, the dynamic linker
371calls the object's
372.Fn _fini
373function, if such a function is defined by the object.
374If
375.Fn dlclose
376is successful, it returns a value of 0.
377Otherwise it returns -1, and sets an error condition that can be
378interrogated with
379.Fn dlerror .
380.Pp
381The object-intrinsic functions
382.Fn _init
383and
384.Fn _fini
385are called with no arguments, and are not expected to return values.
386.Sh NOTES
387ELF executables need to be linked
388using the
389.Fl export-dynamic
390option to
391.Xr ld 1
392for symbols defined in the executable to become visible to
393.Fn dlsym ,
394.Fn dlvsym
395or
396.Fn dlfunc
397.Pp
398Other ELF platforms require linking with
399.Lb libdl
400to provide
401.Fn dlopen
402and other functions.
403.Fx
404does not require linking with the library, but supports it for compatibility.
405.Pp
406In previous implementations, it was necessary to prepend an underscore
407to all external symbols in order to gain symbol
408compatibility with object code compiled from the C language.
409This is
410still the case when using the (obsolete)
411.Fl aout
412option to the C language compiler.
413.Sh ERRORS
414The
415.Fn dlopen ,
416.Fn fdlopen ,
417.Fn dlsym ,
418.Fn dlvsym ,
419and
420.Fn dlfunc
421functions
422return a null pointer in the event of errors.
423The
424.Fn dlclose
425function
426returns 0 on success, or -1 if an error occurred.
427Whenever an error has been detected, a message detailing it can be
428retrieved via a call to
429.Fn dlerror .
430.Sh SEE ALSO
431.Xr ld 1 ,
432.Xr rtld 1 ,
433.Xr dladdr 3 ,
434.Xr dlinfo 3 ,
435.Xr link 5
436