Assume there are shared libraries lib1, lib2, etc. each declaring and using one of its own global variable g_lib1, g_lib2, etc. I have the addresses of g_lib1, g_lib2, etc., as well as the names of the global variables, but I do not know which variable came from which shared library. My aim is to write a function that can do that.
Using dladdr() on the addresses did not work as it errors out. Furthermore, using dlsym(RTLD_DEFAULT, "name") also reports it as invalid symbol (even though I can see them when using "nm -D" on the executable) .Furthermore, the addresses for all the global variables even if from different libraries are shown to be contiguous and not in the address space of any shared library, which I couldn't understand why.
Is there a way I can programmatically map the global variables to the shared libraries that defined them (like we can map functions to shared libraries with dladdr() using their return address)?