Content-type: text/html; charset=UTF-8
#define _GNU_SOURCE #include <dlfcn.h> int dladdr(void *addr, Dl_info *info); int dladdr1(void *addr, Dl_info *info, void **extra_info, int flags); Link with -ldl.
typedef struct { const char *dli_fname; /* Pathname of shared object that contains address */ void *dli_fbase; /* Base address at which shared object is loaded */ const char *dli_sname; /* Name of symbol whose definition overlaps addr */ void *dli_saddr; /* Exact address of symbol named in dli_sname */ } Dl_info;
If no symbol matching addr could be found, then dli_sname and dli_saddr are set to NULL.
The function dladdr1() is like dladdr(), but returns additional information via the argument extra_info. The information returned depends on the value specified in flags, which can have one of the following values:
struct link_map { ElfW(Addr) l_addr; /* Difference between the address in the ELF file and the address in memory */ char *l_name; /* Absolute pathname where object was found */ ElfW(Dyn) *l_ld; /* Dynamic section of the shared object */ struct link_map *l_next, *l_prev; /* Chain of loaded objects */ /* Plus additional fields private to the implementation */ };
typedef struct { Elf64_Word st_name; /* Symbol name */ unsigned char st_info; /* Symbol type and binding */ unsigned char st_other; /* Symbol visibility */ Elf64_Section st_shndx; /* Section index */ Elf64_Addr st_value; /* Symbol value */ Elf64_Xword st_size; /* Symbol size */ } Elf64_Sym;
The st_name field is an index into the string table.
The st_info field encodes the symbol's type and binding. The type can be extracted using the macro ELF64_ST_TYPE(st_info) (or ELF32_ST_TYPE() on 32-bit platforms), which yields one of the following values:
Value | Description |
STT_NOTYPE | Symbol type is unspecified |
STT_OBJECT | Symbol is a data object |
STT_FUNC | Symbol is a code object |
STT_SECTION | Symbol associated with a section |
STT_FILE | Symbol's name is file name |
STT_COMMON | Symbol is a common data object |
STT_TLS | Symbol is thread-local data object |
STT_GNU_IFUNC | Symbol is indirect code object |
Value | Description |
STB_LOCAL | Local symbol |
STB_GLOBAL | Global symbol |
STB_WEAK | Weak symbol |
STB_GNU_UNIQUE | Unique symbol |
Value | Description |
STV_DEFAULT | Default symbol visibility rules |
STV_INTERNAL | Processor-specific hidden class |
STV_HIDDEN | Symbol unavailable in other modules |
STV_PROTECTED | Not preemptible, not exported |
If the address specified in addr could not be matched to a shared object, then these functions return 0. In this case, an error message is not available via dlerror(3).
Interface | Attribute | Value |
dladdr(), dladdr1() | Thread safety | MT-Safe |
The problem is that the function pointer will still be resolved at compile time, but merely point to the plt (Procedure Linkage Table) section of the original object (which dispatches the call after asking the dynamic linker to resolve the symbol). To work around this, you can try to compile the code to be position-independent: then, the compiler cannot prepare the pointer at compile time any more and gcc(1) will generate code that just loads the final symbol address from the got (Global Offset Table) at run time before passing it to dladdr().