xref: /freebsd/lib/libc/gen/dlopen.3 (revision fda2301943c10ee6e10a1c26d835bbff867adc1a)
1645c4be3SJohn Polstra.\" This source code is a product of Sun Microsystems, Inc. and is provided
2645c4be3SJohn Polstra.\" for unrestricted use provided that this legend is included on all tape
3645c4be3SJohn Polstra.\" media and as a part of the software program in whole or part.  Users
4645c4be3SJohn Polstra.\" may copy or modify this source code without charge, but are not authorized
5645c4be3SJohn Polstra.\" to license or distribute it to anyone else except as part of a product or
6645c4be3SJohn Polstra.\" program developed by the user.
7645c4be3SJohn Polstra.\"
8645c4be3SJohn Polstra.\" THIS PROGRAM CONTAINS SOURCE CODE COPYRIGHTED BY SUN MICROSYSTEMS, INC.
9645c4be3SJohn Polstra.\" SUN MICROSYSTEMS, INC., MAKES NO REPRESENTATIONS ABOUT THE SUITABLITY
10645c4be3SJohn Polstra.\" OF SUCH SOURCE CODE FOR ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT
11645c4be3SJohn Polstra.\" EXPRESS OR IMPLIED WARRANTY OF ANY KIND.  SUN MICROSYSTEMS, INC. DISCLAIMS
12645c4be3SJohn Polstra.\" ALL WARRANTIES WITH REGARD TO SUCH SOURCE CODE, INCLUDING ALL IMPLIED
13645c4be3SJohn Polstra.\" WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  IN
14645c4be3SJohn Polstra.\" NO EVENT SHALL SUN MICROSYSTEMS, INC. BE LIABLE FOR ANY SPECIAL, INDIRECT,
15645c4be3SJohn Polstra.\" INCIDENTAL, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
16645c4be3SJohn Polstra.\" FROM USE OF SUCH SOURCE CODE, REGARDLESS OF THE THEORY OF LIABILITY.
17645c4be3SJohn Polstra.\"
18645c4be3SJohn Polstra.\" This source code is provided with no support and without any obligation on
19645c4be3SJohn Polstra.\" the part of Sun Microsystems, Inc. to assist in its use, correction,
20645c4be3SJohn Polstra.\" modification or enhancement.
21645c4be3SJohn Polstra.\"
22645c4be3SJohn Polstra.\" SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
23645c4be3SJohn Polstra.\" INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS
24645c4be3SJohn Polstra.\" SOURCE CODE OR ANY PART THEREOF.
25645c4be3SJohn Polstra.\"
26645c4be3SJohn Polstra.\" Sun Microsystems, Inc.
27645c4be3SJohn Polstra.\" 2550 Garcia Avenue
28645c4be3SJohn Polstra.\" Mountain View, California 94043
29645c4be3SJohn Polstra.\"
30645c4be3SJohn Polstra.\" Copyright (c) 1991 Sun Microsystems, Inc.
31645c4be3SJohn Polstra.\"
32645c4be3SJohn Polstra.\" @(#) dlopen.3 1.6 90/01/31 SMI
337f3dea24SPeter Wemm.\" $FreeBSD$
34fbc400a6SNik Clayton.\"
35dc12134aSGarrett Wollman.Dd May 28, 2002
36a307d598SRuslan Ermilov.Os
37645c4be3SJohn Polstra.Dt DLOPEN 3
38645c4be3SJohn Polstra.Sh NAME
39dc12134aSGarrett Wollman.Nm dlopen , dlsym , dlfunc , dlerror , dlclose
40645c4be3SJohn Polstra.Nd programmatic interface to the dynamic linker
4125bb73e0SAlexey Zelkin.Sh LIBRARY
4225bb73e0SAlexey Zelkin.Lb libc
43645c4be3SJohn Polstra.Sh SYNOPSIS
4432eef9aeSRuslan Ermilov.In dlfcn.h
45645c4be3SJohn Polstra.Ft void *
46645c4be3SJohn Polstra.Fn dlopen "const char *path" "int mode"
47645c4be3SJohn Polstra.Ft void *
48645c4be3SJohn Polstra.Fn dlsym "void *handle" "const char *symbol"
49fda23019SGarrett Wollman.Ft dlfunc_t
50dc12134aSGarrett Wollman.Fn dlfunc "void *handle" "const char *symbol"
51645c4be3SJohn Polstra.Ft const char *
52645c4be3SJohn Polstra.Fn dlerror "void"
53645c4be3SJohn Polstra.Ft int
54645c4be3SJohn Polstra.Fn dlclose "void *handle"
55645c4be3SJohn Polstra.Sh DESCRIPTION
56645c4be3SJohn PolstraThese functions provide a simple programmatic interface to the services of the
57645c4be3SJohn Polstradynamic linker.
58645c4be3SJohn PolstraOperations are provided to add new shared objects to a
59645c4be3SJohn Polstraprogram's address space, to obtain the address bindings of symbols
60645c4be3SJohn Polstradefined by such
61645c4be3SJohn Polstraobjects, and to remove such objects when their use is no longer required.
62645c4be3SJohn Polstra.Pp
63645c4be3SJohn Polstra.Fn dlopen
64645c4be3SJohn Polstraprovides access to the shared object in
65645c4be3SJohn Polstra.Fa path ,
66645c4be3SJohn Polstrareturning a descriptor that can be used for later
67645c4be3SJohn Polstrareferences to the object in calls to
68645c4be3SJohn Polstra.Fn dlsym
69645c4be3SJohn Polstraand
70645c4be3SJohn Polstra.Fn dlclose .
71645c4be3SJohn PolstraIf
72645c4be3SJohn Polstra.Fa path
73645c4be3SJohn Polstrawas not in the address space prior to the call to
74645c4be3SJohn Polstra.Fn dlopen ,
75645c4be3SJohn Polstrait is placed in the address space.
76645c4be3SJohn PolstraWhen an object is first loaded into the address space in this way, its
77645c4be3SJohn Polstrafunction
78645c4be3SJohn Polstra.Fn _init ,
79645c4be3SJohn Polstraif any, is called by the dynamic linker.
80645c4be3SJohn PolstraIf
81645c4be3SJohn Polstra.Fa path
82645c4be3SJohn Polstrahas already been placed in the address space in a previous call to
83645c4be3SJohn Polstra.Fn dlopen ,
84645c4be3SJohn Polstrait is not added a second time, although a reference count of
85645c4be3SJohn Polstra.Fn dlopen
86645c4be3SJohn Polstraoperations on
87645c4be3SJohn Polstra.Fa path
88645c4be3SJohn Polstrais maintained.
89645c4be3SJohn PolstraA null pointer supplied for
90645c4be3SJohn Polstra.Fa path
91645c4be3SJohn Polstrais interpreted as a reference to the main
92645c4be3SJohn Polstraexecutable of the process.
93645c4be3SJohn Polstra.Fa mode
94645c4be3SJohn Polstracontrols the way in which external function references from the
95645c4be3SJohn Polstraloaded object are bound to their referents.
9607f643a5SJohn PolstraIt must contain one of the following values, possibly ORed with
9707f643a5SJohn Polstraadditional flags which will be described subsequently:
98645c4be3SJohn Polstra.Bl -tag -width RTLD_LAZYX
99645c4be3SJohn Polstra.It Dv RTLD_LAZY
100645c4be3SJohn PolstraEach external function reference is resolved when the function is first
101645c4be3SJohn Polstracalled.
102645c4be3SJohn Polstra.It Dv RTLD_NOW
103645c4be3SJohn PolstraAll external function references are bound immediately by
104645c4be3SJohn Polstra.Fn dlopen .
105645c4be3SJohn Polstra.El
106645c4be3SJohn Polstra.Pp
107645c4be3SJohn Polstra.Dv RTLD_LAZY
108645c4be3SJohn Polstrais normally preferred, for reasons of efficiency.
109645c4be3SJohn PolstraHowever,
110645c4be3SJohn Polstra.Dv RTLD_NOW
111645c4be3SJohn Polstrais useful to ensure that any undefined symbols are discovered during the
112645c4be3SJohn Polstracall to
113645c4be3SJohn Polstra.Fn dlopen .
11407f643a5SJohn Polstra.Pp
11507f643a5SJohn PolstraOne of the following flags may be ORed into the
11607f643a5SJohn Polstra.Fa mode
11707f643a5SJohn Polstraargument:
11807f643a5SJohn Polstra.Bl -tag -width RTLD_GLOBALX
11907f643a5SJohn Polstra.It Dv RTLD_GLOBAL
12007f643a5SJohn PolstraSymbols from this shared object and its directed acyclic graph (DAG)
12107f643a5SJohn Polstraof needed objects will be available for resolving undefined references
12207f643a5SJohn Polstrafrom all other shared objects.
12307f643a5SJohn Polstra.It Dv RTLD_LOCAL
12407f643a5SJohn PolstraSymbols in this shared object and its DAG of needed objects will be
12507f643a5SJohn Polstraavailable for resolving undefined references only from other objects
12607f643a5SJohn Polstrain the same DAG.  This is the default, but it may be specified
12707f643a5SJohn Polstraexplicitly with this flag.
128c6de4ce7SMaxim Sobolev.It Dv RTLD_TRACE
129c6de4ce7SMaxim SobolevWhen set, causes dynamic linker to exit after loading all objects
130c6de4ce7SMaxim Sobolevneeded by this shared object and printing a summary which includes
131c6de4ce7SMaxim Sobolevthe absolute pathnames of all objects, to standard output.
132c6de4ce7SMaxim SobolevWith this flag
133c6de4ce7SMaxim Sobolev.Fn dlopen
134c6de4ce7SMaxim Sobolevwill return to the caller only in the case of error.
13507f643a5SJohn Polstra.El
13607f643a5SJohn Polstra.Pp
137645c4be3SJohn PolstraIf
138645c4be3SJohn Polstra.Fn dlopen
139645c4be3SJohn Polstrafails, it returns a null pointer, and sets an error condition which may
140645c4be3SJohn Polstrabe interrogated with
141645c4be3SJohn Polstra.Fn dlerror .
142645c4be3SJohn Polstra.Pp
143645c4be3SJohn Polstra.Fn dlsym
144645c4be3SJohn Polstrareturns the address binding of the symbol described in the null-terminated
145645c4be3SJohn Polstracharacter string
146645c4be3SJohn Polstra.Fa symbol ,
147645c4be3SJohn Polstraas it occurs in the shared object identified by
148645c4be3SJohn Polstra.Fa handle .
149645c4be3SJohn PolstraThe symbols exported by objects added to the address space by
150645c4be3SJohn Polstra.Fn dlopen
151645c4be3SJohn Polstracan be accessed only through calls to
152645c4be3SJohn Polstra.Fn dlsym .
153645c4be3SJohn PolstraSuch symbols do not supersede any definition of those symbols already present
154645c4be3SJohn Polstrain the address space when the object is loaded, nor are they available to
155645c4be3SJohn Polstrasatisfy normal dynamic linking references.
1567f244df8SJohn Polstra.Pp
1577f244df8SJohn PolstraIf
1587f244df8SJohn Polstra.Fn dlsym
1597f244df8SJohn Polstrais called with the special
160645c4be3SJohn Polstra.Fa handle
1617f244df8SJohn Polstra.Dv NULL ,
1627f244df8SJohn Polstrait is interpreted as a reference to the executable or shared object
1637f244df8SJohn Polstrafrom which the call
164645c4be3SJohn Polstrais being made.  Thus a shared object can reference its own symbols.
1657f244df8SJohn Polstra.Pp
1667f244df8SJohn PolstraIf
167645c4be3SJohn Polstra.Fn dlsym
1687f244df8SJohn Polstrais called with the special
1697f244df8SJohn Polstra.Fa handle
1707f244df8SJohn Polstra.Dv RTLD_DEFAULT ,
1717f244df8SJohn Polstrathe search for the symbol follows the algorithm used for resolving
1727f244df8SJohn Polstraundefined symbols when objects are loaded.  The objects searched are
1737f244df8SJohn Polstraas follows, in the given order:
1747f244df8SJohn Polstra.Bl -enum
1757f244df8SJohn Polstra.It
1767f244df8SJohn PolstraThe referencing object itself (or the object from which the call to
1777f244df8SJohn Polstra.Fn dlsym
1787f244df8SJohn Polstrais made), if that object was linked using the
1797f244df8SJohn Polstra.Fl Wsymbolic
1807f244df8SJohn Polstraoption to
1817f244df8SJohn Polstra.Xr ld 1 .
1827f244df8SJohn Polstra.It
1837f244df8SJohn PolstraAll objects loaded at program start-up.
1847f244df8SJohn Polstra.It
1857f244df8SJohn PolstraAll objects loaded via
1867f244df8SJohn Polstra.Fn dlopen
1877f244df8SJohn Polstrawhich are in needed-object DAGs that also contain the referencing object.
1887f244df8SJohn Polstra.It
1897f244df8SJohn PolstraAll objects loaded via
1907f244df8SJohn Polstra.Fn dlopen
1917f244df8SJohn Polstrawith the
1927f244df8SJohn Polstra.Dv RTLD_GLOBAL
1937f244df8SJohn Polstraflag set in the
1947f244df8SJohn Polstra.Fa mode
1957f244df8SJohn Polstraargument.
1967f244df8SJohn Polstra.El
197645c4be3SJohn Polstra.Pp
198645c4be3SJohn PolstraIf
199645c4be3SJohn Polstra.Fn dlsym
200645c4be3SJohn Polstrais called with the special
201645c4be3SJohn Polstra.Fa handle
202645c4be3SJohn Polstra.Dv RTLD_NEXT ,
203645c4be3SJohn Polstrathen the search for the symbol is limited to the shared objects
204645c4be3SJohn Polstrawhich were loaded after the one issuing the call to
205645c4be3SJohn Polstra.Fn dlsym .
206645c4be3SJohn PolstraThus, if the function is called from the main program, all
207645c4be3SJohn Polstrathe shared libraries are searched.
208645c4be3SJohn PolstraIf it is called from a shared library, all subsequent shared
209645c4be3SJohn Polstralibraries are searched.
210645c4be3SJohn Polstra.Dv RTLD_NEXT
211645c4be3SJohn Polstrais useful for implementing wrappers around library functions.
212645c4be3SJohn PolstraFor example, a wrapper function
213645c4be3SJohn Polstra.Fn getpid
214645c4be3SJohn Polstracould access the
215645c4be3SJohn Polstra.Dq real
216645c4be3SJohn Polstra.Fn getpid
217645c4be3SJohn Polstrawith
218c786c636SJoerg Wunsch.Li dlsym(RTLD_NEXT, \&"getpid\&") .
219dc12134aSGarrett Wollman(Actually, the
220dc12134aSGarrett Wollman.Fn dlfunc
221dc12134aSGarrett Wollmaninterface, below, should be used, since
222dc12134aSGarrett Wollman.Fn getpid
223dc12134aSGarrett Wollmanis a function and not a data object.)
224645c4be3SJohn Polstra.Pp
2257f244df8SJohn Polstra.Fn dlsym
2267f244df8SJohn Polstrareturns a null pointer if the symbol cannot be found, and sets an error
2277f244df8SJohn Polstracondition which may be queried with
2287f244df8SJohn Polstra.Fn dlerror .
2297f244df8SJohn Polstra.Pp
230dc12134aSGarrett Wollman.Fn dlfunc
231dc12134aSGarrett Wollmanimplements all of the behavior of
232dc12134aSGarrett Wollman.Fn dlsym ,
233dc12134aSGarrett Wollmanbut has a return type which can be cast to a function pointer without
234dc12134aSGarrett Wollmantriggering compiler diagnostics.
235dc12134aSGarrett Wollman.Po Fn dlsym
236dc12134aSGarrett Wollmanreturns a data pointer; in the C standard, conversions between
237dc12134aSGarrett Wollmandata and function pointer types are undefined.  Some compilers and
238dc12134aSGarrett Wollman.Xr lint 1
239dc12134aSGarrett Wollmanutilities warn about such casts.
240dc12134aSGarrett Wollman.Pc
241dc12134aSGarrett WollmanThe precise return type of
242dc12134aSGarrett Wollman.Fn dlfunc
243dc12134aSGarrett Wollmanis unspecified; applications must cast it to an appropriate function pointer
244dc12134aSGarrett Wollmantype.
245dc12134aSGarrett Wollman.Pp
246645c4be3SJohn Polstra.Fn dlerror
247645c4be3SJohn Polstrareturns a null-terminated character string describing the last error that
248645c4be3SJohn Polstraoccurred during a call to
249645c4be3SJohn Polstra.Fn dlopen ,
250645c4be3SJohn Polstra.Fn dlsym ,
251dc12134aSGarrett Wollman.Fn dlfunc ,
252645c4be3SJohn Polstraor
253645c4be3SJohn Polstra.Fn dlclose .
254645c4be3SJohn PolstraIf no such error has occurred,
255645c4be3SJohn Polstra.Fn dlerror
256645c4be3SJohn Polstrareturns a null pointer.
257645c4be3SJohn PolstraAt each call to
258645c4be3SJohn Polstra.Fn dlerror ,
259645c4be3SJohn Polstrathe error indication is reset.  Thus in the case of two calls
260645c4be3SJohn Polstrato
261645c4be3SJohn Polstra.Fn dlerror ,
262645c4be3SJohn Polstrawhere the second call follows the first immediately, the second call
263645c4be3SJohn Polstrawill always return a null pointer.
264645c4be3SJohn Polstra.Pp
265645c4be3SJohn Polstra.Fn dlclose
266645c4be3SJohn Polstradeletes a reference to the shared object referenced by
267645c4be3SJohn Polstra.Fa handle .
268645c4be3SJohn PolstraIf the reference count drops to 0, the object is removed from the
269645c4be3SJohn Polstraaddress space, and
270645c4be3SJohn Polstra.Fa handle
271645c4be3SJohn Polstrais rendered invalid.
272645c4be3SJohn PolstraJust before removing a shared object in this way, the dynamic linker
273645c4be3SJohn Polstracalls the object's
274645c4be3SJohn Polstra.Fn _fini
275645c4be3SJohn Polstrafunction, if such a function is defined by the object.
276645c4be3SJohn PolstraIf
277645c4be3SJohn Polstra.Fn dlclose
278645c4be3SJohn Polstrais successful, it returns a value of 0.
279645c4be3SJohn PolstraOtherwise it returns -1, and sets an error condition that can be
280645c4be3SJohn Polstrainterrogated with
281645c4be3SJohn Polstra.Fn dlerror .
282645c4be3SJohn Polstra.Pp
283645c4be3SJohn PolstraThe object-intrinsic functions
284645c4be3SJohn Polstra.Fn _init
285645c4be3SJohn Polstraand
286645c4be3SJohn Polstra.Fn _fini
287645c4be3SJohn Polstraare called with no arguments, and are not expected to return values.
288f28529d7SJoseph Koshy.Sh NOTES
289f28529d7SJoseph KoshyELF executables need to be linked
290f28529d7SJoseph Koshyusing the
291f28529d7SJoseph Koshy.Fl export-dynamic
292f28529d7SJoseph Koshyoption to
293f28529d7SJoseph Koshy.Xr ld 1
294f28529d7SJoseph Koshyfor symbols defined in the executable to become visible to
295f28529d7SJoseph Koshy.Fn dlsym .
296c786c636SJoerg Wunsch.Pp
297c786c636SJoerg WunschIn previous implementations, it was necessary to prepend an underscore
298c786c636SJoerg Wunschto all external symbols in order to gain symbol
299c786c636SJoerg Wunschcompatibility with object code compiled from the C language.  This is
300c786c636SJoerg Wunschstill the case when using the (obsolete)
301c786c636SJoerg Wunsch.Fl aout
302c786c636SJoerg Wunschoption to the C language compiler.
303645c4be3SJohn Polstra.Sh ERRORS
304dc12134aSGarrett Wollman.Fn dlopen ,
305dc12134aSGarrett Wollman.Fn dlsym ,
306645c4be3SJohn Polstraand
307dc12134aSGarrett Wollman.Fn dlfunc
308dc12134aSGarrett Wollmanreturn a null pointer in the event of errors.
309645c4be3SJohn Polstra.Fn dlclose
310645c4be3SJohn Polstrareturns 0 on success, or -1 if an error occurred.
311645c4be3SJohn PolstraWhenever an error has been detected, a message detailing it can be
312645c4be3SJohn Polstraretrieved via a call to
313645c4be3SJohn Polstra.Fn dlerror .
314645c4be3SJohn Polstra.Sh SEE ALSO
315645c4be3SJohn Polstra.Xr ld 1 ,
316645c4be3SJohn Polstra.Xr rtld 1 ,
3174088069cSDima Dorfman.Xr dladdr 3 ,
318645c4be3SJohn Polstra.Xr link 5
319