xref: /freebsd/lib/libc/gen/dlopen.3 (revision 645c4be38a40ae297fc27d00d66da5599fe60eb6)
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
33645c4be3SJohn Polstra.Dd September 24, 1989
34645c4be3SJohn Polstra.Os FreeBSD
35645c4be3SJohn Polstra.Dt DLOPEN 3
36645c4be3SJohn Polstra.Sh NAME
37645c4be3SJohn Polstra.Nm dlopen, dlsym, dlerror, dlclose
38645c4be3SJohn Polstra.Nd programmatic interface to the dynamic linker
39645c4be3SJohn Polstra.Sh SYNOPSIS
40645c4be3SJohn Polstra.Fd #include <dlfcn.h>
41645c4be3SJohn Polstra.Ft void *
42645c4be3SJohn Polstra.Fn dlopen "const char *path" "int mode"
43645c4be3SJohn Polstra.Ft void *
44645c4be3SJohn Polstra.Fn dlsym "void *handle" "const char *symbol"
45645c4be3SJohn Polstra.Ft const char *
46645c4be3SJohn Polstra.Fn dlerror "void"
47645c4be3SJohn Polstra.Ft int
48645c4be3SJohn Polstra.Fn dlclose "void *handle"
49645c4be3SJohn Polstra.Sh DESCRIPTION
50645c4be3SJohn PolstraThese functions provide a simple programmatic interface to the services of the
51645c4be3SJohn Polstradynamic linker.
52645c4be3SJohn PolstraOperations are provided to add new shared objects to a
53645c4be3SJohn Polstraprogram's address space, to obtain the address bindings of symbols
54645c4be3SJohn Polstradefined by such
55645c4be3SJohn Polstraobjects, and to remove such objects when their use is no longer required.
56645c4be3SJohn Polstra.Pp
57645c4be3SJohn Polstra.Fn dlopen
58645c4be3SJohn Polstraprovides access to the shared object in
59645c4be3SJohn Polstra.Fa path ,
60645c4be3SJohn Polstrareturning a descriptor that can be used for later
61645c4be3SJohn Polstrareferences to the object in calls to
62645c4be3SJohn Polstra.Fn dlsym
63645c4be3SJohn Polstraand
64645c4be3SJohn Polstra.Fn dlclose .
65645c4be3SJohn PolstraIf
66645c4be3SJohn Polstra.Fa path
67645c4be3SJohn Polstrawas not in the address space prior to the call to
68645c4be3SJohn Polstra.Fn dlopen ,
69645c4be3SJohn Polstrait is placed in the address space.
70645c4be3SJohn PolstraWhen an object is first loaded into the address space in this way, its
71645c4be3SJohn Polstrafunction
72645c4be3SJohn Polstra.Fn _init ,
73645c4be3SJohn Polstraif any, is called by the dynamic linker.
74645c4be3SJohn Polstra(Note that
75645c4be3SJohn Polstra.Ql _init
76645c4be3SJohn Polstrais the name as expressed in the C language.
77645c4be3SJohn PolstraFrom assembly language, the name would appear as
78645c4be3SJohn Polstra.Ql __init
79645c4be3SJohn Polstrainstead.)
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.
96645c4be3SJohn PolstraIt must contains one of the following values:
97645c4be3SJohn Polstra.Bl -tag -width RTLD_LAZYX
98645c4be3SJohn Polstra.It Dv RTLD_LAZY
99645c4be3SJohn PolstraEach external function reference is resolved when the function is first
100645c4be3SJohn Polstracalled.
101645c4be3SJohn Polstra.It Dv RTLD_NOW
102645c4be3SJohn PolstraAll external function references are bound immediately by
103645c4be3SJohn Polstra.Fn dlopen .
104645c4be3SJohn Polstra.El
105645c4be3SJohn Polstra.Pp
106645c4be3SJohn Polstra.Dv RTLD_LAZY
107645c4be3SJohn Polstrais normally preferred, for reasons of efficiency.
108645c4be3SJohn PolstraHowever,
109645c4be3SJohn Polstra.Dv RTLD_NOW
110645c4be3SJohn Polstrais useful to ensure that any undefined symbols are discovered during the
111645c4be3SJohn Polstracall to
112645c4be3SJohn Polstra.Fn dlopen .
113645c4be3SJohn PolstraIf
114645c4be3SJohn Polstra.Fn dlopen
115645c4be3SJohn Polstrafails, it returns a null pointer, and sets an error condition which may
116645c4be3SJohn Polstrabe interrogated with
117645c4be3SJohn Polstra.Fn dlerror .
118645c4be3SJohn Polstra.Pp
119645c4be3SJohn Polstra.Fn dlsym
120645c4be3SJohn Polstrareturns the address binding of the symbol described in the null-terminated
121645c4be3SJohn Polstracharacter string
122645c4be3SJohn Polstra.Fa symbol ,
123645c4be3SJohn Polstraas it occurs in the shared object identified by
124645c4be3SJohn Polstra.Fa handle .
125645c4be3SJohn PolstraNote that
126645c4be3SJohn Polstra.Fa symbol
127645c4be3SJohn Polstrais the assembly language representation of the symbol name.
128645c4be3SJohn PolstraThe assembly language representation of a C language symbol contains an
129645c4be3SJohn Polstraextra underscore at the beginning.
130645c4be3SJohn PolstraFor example, the symbol
131645c4be3SJohn Polstra.Ql foo
132645c4be3SJohn Polstrain C would appear as
133645c4be3SJohn Polstra.Ql _foo
134645c4be3SJohn Polstrain assembly language, and in the
135645c4be3SJohn Polstra.Fa symbol
136645c4be3SJohn Polstraargument to
137645c4be3SJohn Polstra.Fn dlsym .
138645c4be3SJohn PolstraThe symbols exported by objects added to the address space by
139645c4be3SJohn Polstra.Fn dlopen
140645c4be3SJohn Polstracan be accessed only through calls to
141645c4be3SJohn Polstra.Fn dlsym .
142645c4be3SJohn PolstraSuch symbols do not supersede any definition of those symbols already present
143645c4be3SJohn Polstrain the address space when the object is loaded, nor are they available to
144645c4be3SJohn Polstrasatisfy normal dynamic linking references.
145645c4be3SJohn PolstraA null pointer supplied as the value of
146645c4be3SJohn Polstra.Fa handle
147645c4be3SJohn Polstrais interpreted as a reference to the executable from which the call to
148645c4be3SJohn Polstra.Fn dlsym
149645c4be3SJohn Polstrais being made.  Thus a shared object can reference its own symbols.
150645c4be3SJohn Polstra.Fn dlsym
151645c4be3SJohn Polstrareturns a null pointer if the symbol cannot be found, and sets an error
152645c4be3SJohn Polstracondition which may be queried with
153645c4be3SJohn Polstra.Fn dlerror .
154645c4be3SJohn Polstra.Pp
155645c4be3SJohn PolstraIf
156645c4be3SJohn Polstra.Fn dlsym
157645c4be3SJohn Polstrais called with the special
158645c4be3SJohn Polstra.Fa handle
159645c4be3SJohn Polstra.Dv RTLD_NEXT ,
160645c4be3SJohn Polstrathen the search for the symbol is limited to the shared objects
161645c4be3SJohn Polstrawhich were loaded after the one issuing the call to
162645c4be3SJohn Polstra.Fn dlsym .
163645c4be3SJohn PolstraThus, if the function is called from the main program, all
164645c4be3SJohn Polstrathe shared libraries are searched.
165645c4be3SJohn PolstraIf it is called from a shared library, all subsequent shared
166645c4be3SJohn Polstralibraries are searched.
167645c4be3SJohn Polstra.Dv RTLD_NEXT
168645c4be3SJohn Polstrais useful for implementing wrappers around library functions.
169645c4be3SJohn PolstraFor example, a wrapper function
170645c4be3SJohn Polstra.Fn getpid
171645c4be3SJohn Polstracould access the
172645c4be3SJohn Polstra.Dq real
173645c4be3SJohn Polstra.Fn getpid
174645c4be3SJohn Polstrawith
175645c4be3SJohn Polstra.Li dlsym(RTLD_NEXT, \&"_getpid\&") .
176645c4be3SJohn Polstra.Pp
177645c4be3SJohn Polstra.Fn dlerror
178645c4be3SJohn Polstrareturns a null-terminated character string describing the last error that
179645c4be3SJohn Polstraoccurred during a call to
180645c4be3SJohn Polstra.Fn dlopen ,
181645c4be3SJohn Polstra.Fn dlsym ,
182645c4be3SJohn Polstraor
183645c4be3SJohn Polstra.Fn dlclose .
184645c4be3SJohn PolstraIf no such error has occurred,
185645c4be3SJohn Polstra.Fn dlerror
186645c4be3SJohn Polstrareturns a null pointer.
187645c4be3SJohn PolstraAt each call to
188645c4be3SJohn Polstra.Fn dlerror ,
189645c4be3SJohn Polstrathe error indication is reset.  Thus in the case of two calls
190645c4be3SJohn Polstrato
191645c4be3SJohn Polstra.Fn dlerror ,
192645c4be3SJohn Polstrawhere the second call follows the first immediately, the second call
193645c4be3SJohn Polstrawill always return a null pointer.
194645c4be3SJohn Polstra.Pp
195645c4be3SJohn Polstra.Fn dlclose
196645c4be3SJohn Polstradeletes a reference to the shared object referenced by
197645c4be3SJohn Polstra.Fa handle .
198645c4be3SJohn PolstraIf the reference count drops to 0, the object is removed from the
199645c4be3SJohn Polstraaddress space, and
200645c4be3SJohn Polstra.Fa handle
201645c4be3SJohn Polstrais rendered invalid.
202645c4be3SJohn PolstraJust before removing a shared object in this way, the dynamic linker
203645c4be3SJohn Polstracalls the object's
204645c4be3SJohn Polstra.Fn _fini
205645c4be3SJohn Polstrafunction, if such a function is defined by the object.
206645c4be3SJohn PolstraAs with
207645c4be3SJohn Polstra.Ql _init ,
208645c4be3SJohn Polstra.Ql _fini
209645c4be3SJohn Polstrais the C language name of the function.
210645c4be3SJohn PolstraIf
211645c4be3SJohn Polstra.Fn dlclose
212645c4be3SJohn Polstrais successful, it returns a value of 0.
213645c4be3SJohn PolstraOtherwise it returns -1, and sets an error condition that can be
214645c4be3SJohn Polstrainterrogated with
215645c4be3SJohn Polstra.Fn dlerror .
216645c4be3SJohn Polstra.Pp
217645c4be3SJohn PolstraThe object-intrinsic functions
218645c4be3SJohn Polstra.Fn _init
219645c4be3SJohn Polstraand
220645c4be3SJohn Polstra.Fn _fini
221645c4be3SJohn Polstraare called with no arguments, and are not expected to return values.
222645c4be3SJohn Polstra.Sh ERRORS
223645c4be3SJohn Polstra.Fn dlopen
224645c4be3SJohn Polstraand
225645c4be3SJohn Polstra.Fn dlsym
226645c4be3SJohn Polstrareturn the null pointer in the event of errors.
227645c4be3SJohn Polstra.Fn dlclose
228645c4be3SJohn Polstrareturns 0 on success, or -1 if an error occurred.
229645c4be3SJohn PolstraWhenever an error has been detected, a message detailing it can be
230645c4be3SJohn Polstraretrieved via a call to
231645c4be3SJohn Polstra.Fn dlerror .
232645c4be3SJohn Polstra.Sh SEE ALSO
233645c4be3SJohn Polstra.Xr ld 1 ,
234645c4be3SJohn Polstra.Xr rtld 1 ,
235645c4be3SJohn Polstra.Xr link 5
236645c4be3SJohn Polstra
237