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.
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.
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.
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.
26 .\" Sun Microsystems, Inc.
27 .\" 2550 Garcia Avenue
28 .\" Mountain View, California 94043
30 .\" Copyright (c) 1991 Sun Microsystems, Inc.
32 .\" $FreeBSD: release/8.1.0/lib/libc/gen/dlopen.3 205979 2010-03-31 13:51:31Z gahr $
40 .Nd shared object symbol lookup function
42 This function is not in a library.
43 It is included in every dynamically linked program automatically.
47 .Fn dlsym "void *handle" "const char *name"
49 .Fn dlfunc "void *handle" "const char *name"
54 returns the address binding of the symbol described in the null-terminated
57 as it occurs in the shared object identified by
59 The symbols exported by objects added to the address space by
61 can be accessed only through calls to
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.
69 is called with the special
72 it is interpreted as a reference to the executable or shared object
75 Thus a shared object can reference its own symbols.
79 is called with the special
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:
88 The referencing object itself (or the object from which the call to
90 is made), if that object was linked using the
95 All objects loaded at program start-up.
97 All objects loaded via
105 All objects loaded via
107 which are in needed-object DAGs that also contain the referencing object.
112 is called with the special
115 then the search for the symbol is limited to the shared objects
116 which were loaded after the one issuing the call to
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.
123 is useful for implementing wrappers around library functions.
124 For example, a wrapper function
130 .Li dlsym(RTLD_NEXT, \&"getpid\&") .
133 interface, below, should be used, since
135 is a function and not a data object.)
139 is called with the special
142 then the search for the symbol is limited to the shared object
145 and those shared objects which were loaded after it.
150 implements all of the behavior of
152 but has a return type which can be cast to a function pointer without
153 triggering compiler diagnostics.
157 returns a data pointer; in the C standard, conversions between
158 data and function pointer types are undefined.
159 Some compilers and code checkers warn about such casts.)
160 The precise return type of
162 is unspecified; applications must cast it to an appropriate function pointer
165 ELF executables need to be linked
170 for symbols defined in the executable to become visible to
178 return the address of the symbol unless the symbol can not be found.
179 In this case, they return a null pointer and set an error condition
180 which may be queried with
183 The following program will obtain a pointer to the cosine function using
184 dlsym, and then it will use it to print out the value of cosine (2.0).
191 main (int argc, char *argv[])
194 double (*func_cosine)(double x);
196 /* open the system shared math library */
197 handle = dlopen("libm.so", RTLD_LAZY);
199 fprintf (stderr, "%s\en", dlerror ());
203 /* get pointer to cosine function */
204 func_cosine = dlsym (handle, "cos");
205 if (func_cosine == NULL) {
206 fprintf (stderr, "%s function not found\en", "cos");
211 /* Calculate and display the cosine of 2.0 */
212 printf ("cosine of 2.0 = %f\en", func_cosine(2.0));