1DLADDR(3) Linux Programmer's Manual DLADDR(3)
2
3
4
6 dladdr, dladdr1 - translate address to symbolic information
7
9 #define _GNU_SOURCE
10 #include <dlfcn.h>
11
12 int dladdr(const void *addr, Dl_info *info);
13 int dladdr1(const void *addr, Dl_info *info, void **extra_info,
14 int flags);
15
16 Link with -ldl.
17
19 The function dladdr() determines whether the address specified in addr
20 is located in one of the shared objects loaded by the calling applica‐
21 tion. If it is, then dladdr() returns information about the shared ob‐
22 ject and symbol that overlaps addr. This information is returned in a
23 Dl_info structure:
24
25 typedef struct {
26 const char *dli_fname; /* Pathname of shared object that
27 contains address */
28 void *dli_fbase; /* Base address at which shared
29 object is loaded */
30 const char *dli_sname; /* Name of symbol whose definition
31 overlaps addr */
32 void *dli_saddr; /* Exact address of symbol named
33 in dli_sname */
34 } Dl_info;
35
36 If no symbol matching addr could be found, then dli_sname and dli_saddr
37 are set to NULL.
38
39 The function dladdr1() is like dladdr(), but returns additional infor‐
40 mation via the argument extra_info. The information returned depends
41 on the value specified in flags, which can have one of the following
42 values:
43
44 RTLD_DL_LINKMAP
45 Obtain a pointer to the link map for the matched file. The ex‐
46 tra_info argument points to a pointer to a link_map structure
47 (i.e., struct link_map **), defined in <link.h> as:
48
49 struct link_map {
50 ElfW(Addr) l_addr; /* Difference between the
51 address in the ELF file and
52 the address in memory */
53 char *l_name; /* Absolute pathname where
54 object was found */
55 ElfW(Dyn) *l_ld; /* Dynamic section of the
56 shared object */
57 struct link_map *l_next, *l_prev;
58 /* Chain of loaded objects */
59
60 /* Plus additional fields private to the
61 implementation */
62 };
63
64 RTLD_DL_SYMENT
65 Obtain a pointer to the ELF symbol table entry of the matching
66 symbol. The extra_info argument is a pointer to a symbol
67 pointer: const ElfW(Sym) **. The ElfW() macro definition turns
68 its argument into the name of an ELF data type suitable for the
69 hardware architecture. For example, on a 64-bit platform,
70 ElfW(Sym) yields the data type name Elf64_Sym, which is defined
71 in <elf.h> as:
72
73 typedef struct {
74 Elf64_Word st_name; /* Symbol name */
75 unsigned char st_info; /* Symbol type and binding */
76 unsigned char st_other; /* Symbol visibility */
77 Elf64_Section st_shndx; /* Section index */
78 Elf64_Addr st_value; /* Symbol value */
79 Elf64_Xword st_size; /* Symbol size */
80 } Elf64_Sym;
81
82 The st_name field is an index into the string table.
83
84 The st_info field encodes the symbol's type and binding. The
85 type can be extracted using the macro ELF64_ST_TYPE(st_info) (or
86 ELF32_ST_TYPE() on 32-bit platforms), which yields one of the
87 following values:
88
89 Value Description
90 STT_NOTYPE Symbol type is unspecified
91 STT_OBJECT Symbol is a data object
92 STT_FUNC Symbol is a code object
93 STT_SECTION Symbol associated with a section
94 STT_FILE Symbol's name is filename
95 STT_COMMON Symbol is a common data object
96 STT_TLS Symbol is thread-local data object
97 STT_GNU_IFUNC Symbol is indirect code object
98
99 The symbol binding can be extracted from the st_info field using
100 the macro ELF64_ST_BIND(st_info) (or ELF32_ST_BIND() on 32-bit
101 platforms), which yields one of the following values:
102
103 Value Description
104 STB_LOCAL Local symbol
105 STB_GLOBAL Global symbol
106 STB_WEAK Weak symbol
107 STB_GNU_UNIQUE Unique symbol
108
109 The st_other field contains the symbol's visibility, which can
110 be extracted using the macro ELF64_ST_VISIBILITY(st_info) (or
111 ELF32_ST_VISIBILITY() on 32-bit platforms), which yields one of
112 the following values:
113
114 Value Description
115 STV_DEFAULT Default symbol visibility rules
116 STV_INTERNAL Processor-specific hidden class
117 STV_HIDDEN Symbol unavailable in other modules
118 STV_PROTECTED Not preemptible, not exported
119
121 On success, these functions return a nonzero value. If the address
122 specified in addr could be matched to a shared object, but not to a
123 symbol in the shared object, then the info->dli_sname and
124 info->dli_saddr fields are set to NULL.
125
126 If the address specified in addr could not be matched to a shared ob‐
127 ject, then these functions return 0. In this case, an error message is
128 not available via dlerror(3).
129
131 dladdr() is present in glibc 2.0 and later. dladdr1() first appeared
132 in glibc 2.3.3.
133
135 For an explanation of the terms used in this section, see at‐
136 tributes(7).
137
138 ┌────────────────────────────────────────────┬───────────────┬─────────┐
139 │Interface │ Attribute │ Value │
140 ├────────────────────────────────────────────┼───────────────┼─────────┤
141 │dladdr(), dladdr1() │ Thread safety │ MT-Safe │
142 └────────────────────────────────────────────┴───────────────┴─────────┘
143
145 These functions are nonstandard GNU extensions that are also present on
146 Solaris.
147
149 Sometimes, the function pointers you pass to dladdr() may surprise you.
150 On some architectures (notably i386 and x86-64), dli_fname and
151 dli_fbase may end up pointing back at the object from which you called
152 dladdr(), even if the function used as an argument should come from a
153 dynamically linked library.
154
155 The problem is that the function pointer will still be resolved at com‐
156 pile time, but merely point to the plt (Procedure Linkage Table) sec‐
157 tion of the original object (which dispatches the call after asking the
158 dynamic linker to resolve the symbol). To work around this, you can
159 try to compile the code to be position-independent: then, the compiler
160 cannot prepare the pointer at compile time any more and gcc(1) will
161 generate code that just loads the final symbol address from the got
162 (Global Offset Table) at run time before passing it to dladdr().
163
165 dl_iterate_phdr(3), dlinfo(3), dlopen(3), dlsym(3), ld.so(8)
166
168 This page is part of release 5.13 of the Linux man-pages project. A
169 description of the project, information about reporting bugs, and the
170 latest version of this page, can be found at
171 https://www.kernel.org/doc/man-pages/.
172
173
174
175Linux 2021-03-22 DLADDR(3)