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