1DLSYM(3)                   Linux Programmer's Manual                  DLSYM(3)
2
3
4

NAME

6       dlsym,  dlvsym  - obtain address of a symbol in a shared object or exe‐
7       cutable
8

SYNOPSIS

10       #include <dlfcn.h>
11
12       void *dlsym(void *handle, const char *symbol);
13
14       #define _GNU_SOURCE
15       #include <dlfcn.h>
16
17       void *dlvsym(void *handle, char *symbol, char *version);
18
19       Link with -ldl.
20

DESCRIPTION

22       The function dlsym() takes a "handle" of a dynamic loaded shared object
23       returned by dlopen(3) along with a null-terminated symbol name, and re‐
24       turns the address where that symbol is loaded into memory.  If the sym‐
25       bol  is not found, in the specified object or any of the shared objects
26       that were automatically  loaded  by  dlopen(3)  when  that  object  was
27       loaded,  dlsym()  returns  NULL.   (The  search performed by dlsym() is
28       breadth first through the dependency tree of these shared objects.)
29
30       In unusual cases (see NOTES) the value of the symbol could actually  be
31       NULL.   Therefore,  a NULL return from dlsym() need not indicate an er‐
32       ror.  The correct way to distinguish an error from a symbol whose value
33       is  NULL  is to call dlerror(3) to clear any old error conditions, then
34       call dlsym(), and then call dlerror(3) again, saving its  return  value
35       into a variable, and check whether this saved value is not NULL.
36
37       There are two special pseudo-handles that may be specified in handle:
38
39       RTLD_DEFAULT
40              Find  the  first  occurrence of the desired symbol using the de‐
41              fault shared object  search  order.   The  search  will  include
42              global  symbols  in the executable and its dependencies, as well
43              as symbols in shared objects that were dynamically  loaded  with
44              the RTLD_GLOBAL flag.
45
46       RTLD_NEXT
47              Find the next occurrence of the desired symbol in the search or‐
48              der after the current object.  This  allows  one  to  provide  a
49              wrapper around a function in another shared object, so that, for
50              example, the definition of a function in a preloaded shared  ob‐
51              ject (see LD_PRELOAD in ld.so(8)) can find and invoke the "real"
52              function provided in another shared object (or for that  matter,
53              the  "next"  definition of the function in cases where there are
54              multiple layers of preloading).
55
56       The _GNU_SOURCE feature test macro must be defined in order  to  obtain
57       the definitions of RTLD_DEFAULT and RTLD_NEXT from <dlfcn.h>.
58
59       The  function  dlvsym()  does  the  same as dlsym() but takes a version
60       string as an additional argument.
61

RETURN VALUE

63       On success, these functions return the address associated with  symbol.
64       On  failure,  they return NULL; the cause of the error can be diagnosed
65       using dlerror(3).
66

VERSIONS

68       dlsym() is present in glibc 2.0 and later.  dlvsym() first appeared  in
69       glibc 2.1.
70

ATTRIBUTES

72       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
73       tributes(7).
74
75       ┌──────────────────┬───────────────┬─────────┐
76Interface         Attribute     Value   
77       ├──────────────────┼───────────────┼─────────┤
78dlsym(), dlvsym() │ Thread safety │ MT-Safe │
79       └──────────────────┴───────────────┴─────────┘

CONFORMING TO

81       POSIX.1-2001 describes dlsym().  The dlvsym() function is a GNU  exten‐
82       sion.
83

NOTES

85       There  are  several  scenarios  when  the address of a global symbol is
86       NULL.  For example, a symbol can be  placed  at  zero  address  by  the
87       linker, via a linker script or with --defsym command-line option. Unde‐
88       fined weak symbols also have NULL value.  Finally, the symbol value may
89       be the result of a GNU indirect function (IFUNC) resolver function that
90       returns NULL as the resolved value. In the latter  case,  dlsym()  also
91       returns  NULL  without error. However, in the former two cases, the be‐
92       havior of GNU dynamic linker  is  inconsistent:  relocation  processing
93       succeeds and the symbol can be observed to have NULL value, but dlsym()
94       fails and dlerror() indicates a lookup error.
95
96   History
97       The dlsym() function is part of the dlopen  API,  derived  from  SunOS.
98       That system does not have dlvsym().
99

EXAMPLES

101       See dlopen(3).
102

SEE ALSO

104       dl_iterate_phdr(3),   dladdr(3),   dlerror(3),   dlinfo(3),  dlopen(3),
105       ld.so(8)
106

COLOPHON

108       This page is part of release 5.10 of the Linux  man-pages  project.   A
109       description  of  the project, information about reporting bugs, and the
110       latest    version    of    this    page,    can     be     found     at
111       https://www.kernel.org/doc/man-pages/.
112
113
114
115Linux                             2020-06-09                          DLSYM(3)
Impressum