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