If you are on a Linux system with a GNU libc, and if the variable is a known name inside the dynamic symbol table of some dynamically linked library, i.e. ELF shared object, and if you can change the code of the main program (or some shared object dynamically linked by it, perhaps playing LD_PRELOAD
tricks) you could use the dladdr(3) function (given a pointer, dladdr
gives you a Dl_info
structure with symbol and shared object names close to the given pointer).
Since dynamically linked shared object libraries are often mmap(2)-ed at non predictable addresses (e.g. because of ASLR) you need to do that at runtime. (See also /proc/self/maps
from inside your process; read proc(5) etc…)
Read Drepper’s paper: How to write Shared Libraries; be aware of VDSO…
Notice that a given *.so
file has several mmap
-ed segments, and that some of its (file) data is not mmap
-ed! Use pmap(1) to find out.
3
solved Finding address of variable in shared library