Syntax
#include <dlfcn.h> |
int dladdr(void *address, struct Dl_info *dlip); |
The function __dladdr_ascii() with the same parameters must be used for the call in an ASCII environment.
Description
dladdr() determines if the specified address is located within one of the mapped objects that make up the current applications address space. An address is deemed to fall within a mapped object when it is between the base address and the end address of that object. If a mapped object fits these criteria, the symbol table made available to the dynamic linker is searched to locate the nearest symbol to the specified address. The nearest symbol is one that has a value less than or equal to the required address.
The Dl_info structure must be preallocated by the user. The structure members are filled in by dladdr() based on the specified address.
The Dl_info structure includes the following members:
const char * dli_fname; void * dli_fbase; const char * dli_sname; void * dli_saddr;
Descriptions of these members:
dli_fname
Contains a pointer to the filename of the containing object.
dli_fbase
Contains the base address of the containing object.
dli_sname
Contains a pointer to the symbol name nearest to the specified address. This symbol either has the same address or is the nearest symbol with a lower address.
dli_saddr
Contains the actual address of the above symbol.
Return value
If the specified address cannot be matched to a mapped object, a 0 is returned. Otherwise, a non-zero return is made and the associated Dl_info elements are filled.
The errno variable is not set. An error message (diagnostic information) will be available through dlerror().
Note
The Dl_info pointer elements point to addresses within the mapped objects. These addresses may become invalid if objects are removed prior to these elements being used (see dlclose()). If no symbol is found to describe the specified address, both the dli_sname and dli_saddr members are set to 0.
The element dli_fbase of the structure Dl_info is not set and always has the value 0.
Example
For reasons of simplicity error traps have been omitted.
void *handle; int symboladdr; int ret; struct Dl_info obj_info; /* Open library */ handle = dlopen("mydynlib.so", RTLD_NOW | RTLD_GLOBAL); /* Determine the address of the entry symbolname */ symboladdr = dlsym(handle, "symbolname"); /* What symbol is located at an offset of 8KB from the symboladdr? */ symboladdr += 8192; if((ret = dladdr((void *)symboladdr, (struct Dl_info *) &obj_info)) == 0) { /* error */ printf("dladdr() failed for address %08X\n", symboladdr); fprintf("dlerror(): %s\n", dlerror()); } else { /* success */ printf("dladdr:\n \tdli_fname %s\n \tdli_fbase %08X\n \tdli_sname %s\n \tdli_saddr %08X\n", obj_info.dli_fname, (int)obj_info.dli_fbase, obj_info.dli_sname, (int)obj_info.dli_saddr ); }
See also
dlclose(), dlerror(), dlopen()