c1f201c81b3724cd80471467c4f2b42a3ab044aa
[dragonfly.git] / lib / libc / gen / dlsym.3
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 .\" $FreeBSD: release/8.1.0/lib/libc/gen/dlopen.3 205979 2010-03-31 13:51:31Z gahr $
33 .\"
34 .Dd April 28, 2011
35 .Dt DLSYM 3
36 .Os
37 .Sh NAME
38 .Nm dlsym ,
39 .Nm dlfunc
40 .Nd shared object symbol lookup function
41 .Sh LIBRARY
42 This function is not in a library.
43 It is included in every dynamically linked program automatically.
44 .Sh SYNOPSIS
45 .In dlfcn.h
46 .Ft void *
47 .Fn dlsym "void *handle" "const char *name"
48 .Ft dlfunc_t
49 .Fn dlfunc "void *handle" "const char *name"
50 .Sh DESCRIPTION
51 The
52 .Fn dlsym
53 function
54 returns the address binding of the symbol described in the null-terminated
55 character string
56 .Fa symbol ,
57 as it occurs in the shared object identified by
58 .Fa handle .
59 The symbols exported by objects added to the address space by
60 .Fn dlopen
61 can be accessed only through calls to
62 .Fn dlsym .
63 Such symbols do not supersede any definition of those symbols already present
64 in the address space when the object is loaded, nor are they available to
65 satisfy normal dynamic linking references.
66 .Pp
67 If
68 .Fn dlsym
69 is called with the special
70 .Fa handle
71 .Dv NULL ,
72 it is interpreted as a reference to the executable or shared object
73 from which the call
74 is being made.
75 Thus a shared object can reference its own symbols.
76 .Pp
77 If
78 .Fn dlsym
79 is called with the special
80 .Fa handle
81 .Dv RTLD_DEFAULT ,
82 the search for the symbol follows the algorithm used for resolving
83 undefined symbols when objects are loaded.
84 The objects searched are
85 as follows, in the given order:
86 .Bl -enum
87 .It
88 The referencing object itself (or the object from which the call to
89 .Fn dlsym
90 is made), if that object was linked using the
91 .Fl Wsymbolic
92 option to
93 .Xr ld 1 .
94 .It
95 All objects loaded at program start-up.
96 .It
97 All objects loaded via
98 .Fn dlopen
99 with the
100 .Dv RTLD_GLOBAL
101 flag set in the
102 .Fa mode
103 argument.
104 .It
105 All objects loaded via
106 .Fn dlopen
107 which are in needed-object DAGs that also contain the referencing object.
108 .El
109 .Pp
110 If
111 .Fn dlsym
112 is called with the special
113 .Fa handle
114 .Dv RTLD_NEXT ,
115 then the search for the symbol is limited to the shared objects
116 which were loaded after the one issuing the call to
117 .Fn dlsym .
118 Thus, if the function is called from the main program, all
119 the shared libraries are searched.
120 If it is called from a shared library, all subsequent shared
121 libraries are searched.
122 .Dv RTLD_NEXT
123 is useful for implementing wrappers around library functions.
124 For example, a wrapper function
125 .Fn getpid
126 could access the
127 .Dq real
128 .Fn getpid
129 with
130 .Li dlsym(RTLD_NEXT, \&"getpid\&") .
131 (Actually, the
132 .Fn dlfunc
133 interface, below, should be used, since
134 .Fn getpid
135 is a function and not a data object.)
136 .Pp
137 If
138 .Fn dlsym
139 is called with the special
140 .Fa handle
141 .Dv RTLD_SELF ,
142 then the search for the symbol is limited to the shared object
143 issuing the call to
144 .Fn dlsym
145 and those shared objects which were loaded after it.
146 .Pp
147 The
148 .Fn dlfunc
149 function
150 implements all of the behavior of
151 .Fn dlsym ,
152 but has a return type which can be cast to a function pointer without
153 triggering compiler diagnostics.
154 (The
155 .Fn dlsym
156 function
157 returns a data pointer; in the C standard, conversions between
158 data and function pointer types are undefined.
159 Some compilers and
160 .Xr lint 1
161 utilities warn about such casts.)
162 The precise return type of
163 .Fn dlfunc
164 is unspecified; applications must cast it to an appropriate function pointer
165 type.
166 .Sh NOTES
167 ELF executables need to be linked
168 using the
169 .Fl export-dynamic
170 option to
171 .Xr ld 1
172 for symbols defined in the executable to become visible to
173 .Fn dlsym .
174 .Sh RETURN VALUE
175 The
176 .Fn dlsym
177 and
178 .Fn dlfunc
179 functions
180 return the address of the symbol unless the symbol can not be found.
181 In this case, they return a null pointer and set an error condition
182 which may be queried with
183 .Fn dlerror .
184 .Sh EXAMPLE
185 The following program will obtain a pointer to the cosine function using
186 dlsym, and then it will use it to print out the value of cosine (2.0).
187 .Bd -literal
188 #include <dlfcn.h>
189 #include <stdlib.h>
190 #include <stdio.h>
191
192 int
193 main (int argc, char *argv[])
194 {
195     void       *handle;
196     double     (*func_cosine)(double x);
197
198     /* open the system shared math library */
199     handle = dlopen("libm.so", RTLD_LAZY);
200     if (!handle) {
201        fprintf (stderr, "%s\en", dlerror ());
202        exit (EXIT_FAILURE);
203     }
204
205     /* get pointer to cosine function */
206     func_cosine = dlsym (handle, "cos");
207     if (func_cosine == NULL) {
208        fprintf (stderr, "%s function not found\en", "cos");
209        dlclose (handle);
210        exit (EXIT_FAILURE);
211     }
212
213     /* Calculate and display the cosine of 2.0 */
214     printf ("cosine of 2.0 = %f\en", func_cosine(2.0));
215     dlclose (handle);
216
217     exit(EXIT_SUCCESS);
218 }
219 .Ed
220 .Sh SEE ALSO
221 .Xr rtld 1 ,
222 .Xr dlfcn 3 ,
223 .Xr dlopen 3 ,
224 .Xr dlvsym 3