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