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(void *addr, Dl_info *info);
13
14 int dladdr1(void *addr, Dl_info *info, void **extra_info, 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
22 object and symbol that overlaps addr. This information is returned in
23 a 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
46 extra_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 file name
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
127 object, then these functions return 0. In this case, an error message
128 is 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
136 attributes(7).
137
138 ┌────────────────────┬───────────────┬─────────┐
139 │Interface │ Attribute │ Value │
140 ├────────────────────┼───────────────┼─────────┤
141 │dladdr(), dladdr1() │ Thread safety │ MT-Safe │
142 └────────────────────┴───────────────┴─────────┘
144 These functions are nonstandard GNU extensions that are also present on
145 Solaris.
146
148 Sometimes, the function pointers you pass to dladdr() may surprise you.
149 On some architectures (notably i386 and x86-64), dli_fname and
150 dli_fbase may end up pointing back at the object from which you called
151 dladdr(), even if the function used as an argument should come from a
152 dynamically linked library.
153
154 The problem is that the function pointer will still be resolved at com‐
155 pile time, but merely point to the plt (Procedure Linkage Table) sec‐
156 tion of the original object (which dispatches the call after asking the
157 dynamic linker to resolve the symbol). To work around this, you can
158 try to compile the code to be position-independent: then, the compiler
159 cannot prepare the pointer at compile time any more and gcc(1) will
160 generate code that just loads the final symbol address from the got
161 (Global Offset Table) at run time before passing it to dladdr().
162
164 dl_iterate_phdr(3), dlinfo(3), dlopen(3), dlsym(3), ld.so(8)
165
167 This page is part of release 4.16 of the Linux man-pages project. A
168 description of the project, information about reporting bugs, and the
169 latest version of this page, can be found at
170 https://www.kernel.org/doc/man-pages/.
171
172
173
174Linux 2017-09-15 DLADDR(3)