1DLSYM(3P) POSIX Programmer's Manual DLSYM(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 dlsym - obtain the address of a symbol from a dlopen object
13
15 #include <dlfcn.h>
16
17 void *dlsym(void *restrict handle, const char *restrict name);
18
19
21 The dlsym() function shall obtain the address of a symbol defined
22 within an object made accessible through a dlopen() call. The handle
23 argument is the value returned from a call to dlopen() (and which has
24 not since been released via a call to dlclose()), and name is the sym‐
25 bol's name as a character string.
26
27 The dlsym() function shall search for the named symbol in all objects
28 loaded automatically as a result of loading the object referenced by
29 handle (see dlopen()). Load ordering is used in dlsym() operations upon
30 the global symbol object. The symbol resolution algorithm used shall be
31 dependency order as described in dlopen().
32
33 The RTLD_DEFAULT and RTLD_NEXT flags are reserved for future use.
34
36 If handle does not refer to a valid object opened by dlopen(), or if
37 the named symbol cannot be found within any of the objects associated
38 with handle, dlsym() shall return NULL. More detailed diagnostic infor‐
39 mation shall be available through dlerror() .
40
42 No errors are defined.
43
44 The following sections are informative.
45
47 The following example shows how dlopen() and dlsym() can be used to
48 access either function or data objects. For simplicity, error checking
49 has been omitted.
50
51
52 void *handle;
53 int *iptr, (*fptr)(int);
54
55
56 /* open the needed object */
57 handle = dlopen("/usr/home/me/libfoo.so", RTLD_LOCAL | RTLD_LAZY);
58
59
60 /* find the address of function and data objects */
61 *(void **)(&fptr) = dlsym(handle, "my_function");
62 iptr = (int *)dlsym(handle, "my_object");
63
64
65 /* invoke function, passing value of integer as a parameter */
66 (*fptr)(*iptr);
67
69 Special purpose values for handle are reserved for future use. These
70 values and their meanings are:
71
72 RTLD_DEFAULT
73 The symbol lookup happens in the normal global scope; that is, a
74 search for a symbol using this handle would find the same defi‐
75 nition as a direct use of this symbol in the program code.
76
77 RTLD_NEXT
78 Specifies the next object after this one that defines name.
79 This one refers to the object containing the invocation of
80 dlsym(). The next object is the one found upon the application
81 of a load order symbol resolution algorithm (see dlopen()). The
82 next object is either one of global scope (because it was intro‐
83 duced as part of the original process image or because it was
84 added with a dlopen() operation including the RTLD_GLOBAL flag),
85 or is an object that was included in the same dlopen() operation
86 that loaded this one.
87
88 The RTLD_NEXT flag is useful to navigate an intentionally created hier‐
89 archy of multiply-defined symbols created through interposition. For
90 example, if a program wished to create an implementation of malloc()
91 that embedded some statistics gathering about memory allocations, such
92 an implementation could use the real malloc() definition to perform the
93 memory allocation-and itself only embed the necessary logic to imple‐
94 ment the statistics gathering function.
95
96
98 The ISO C standard does not require that pointers to functions can be
99 cast back and forth to pointers to data. Indeed, the ISO C standard
100 does not require that an object of type void * can hold a pointer to a
101 function. Implementations supporting the XSI extension, however, do
102 require that an object of type void * can hold a pointer to a function.
103 The result of converting a pointer to a function into a pointer to
104 another data type (except void *) is still undefined, however. Note
105 that compilers conforming to the ISO C standard are required to gener‐
106 ate a warning if a conversion from a void * pointer to a function
107 pointer is attempted as in:
108
109
110 fptr = (int (*)(int))dlsym(handle, "my_function");
111
112 Due to the problem noted here, a future version may either add a new
113 function to return function pointers, or the current interface may be
114 deprecated in favor of two new functions: one that returns data point‐
115 ers and the other that returns function pointers.
116
118 None.
119
121 dlclose(), dlerror(), dlopen(), the Base Definitions volume of
122 IEEE Std 1003.1-2001, <dlfcn.h>
123
125 Portions of this text are reprinted and reproduced in electronic form
126 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
127 -- Portable Operating System Interface (POSIX), The Open Group Base
128 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
129 Electrical and Electronics Engineers, Inc and The Open Group. In the
130 event of any discrepancy between this version and the original IEEE and
131 The Open Group Standard, the original IEEE and The Open Group Standard
132 is the referee document. The original Standard can be obtained online
133 at http://www.opengroup.org/unix/online.html .
134
135
136
137IEEE/The Open Group 2003 DLSYM(3P)