This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: dlinfo for the calling shared library
On 7/10/19 9:06 AM, Zack Weinberg wrote:
Do we have a supported way to make dlinfo() queries for the shared
library from which the call originates, without the library having to
know its own soname? (If it knows its own soname, it can call dlopen
on itself, but I'm looking into a scenario where that information is
not available.)
It is not easy.
If I had to do it I would do:
(a) Call dladdr() to find your base address.
(b) Call dl_iterate_phdr() to find the object by matching
struct dl_phdr_info -> dlpi_addr == Dl_info -> dli_fbase
(c) Via struct dl_phdr_info work through PT_DYNAMIC to find
DT_SONAME and extract soname.
(d) Call dlopen() with SONAME to get handle.
(e) Use handle with dlinfo() to get data.
A library can retrieve its own "base address" using dladdr(), but it
is not clear to me whether that is the same thing as the handle
expected by dlsym and dlinfo.
It is not. Internally the handle is a pointer to the identifying
struct link_map for the shared object.
What we want here is, unsuprisingly, Solaris's RTLD_SELF.
https://docs.oracle.com/cd/E19683-01/816-0213/6m6ne37t3/index.html
Though we should extend it such that dlopen accepts RTLD_SELF and
returns your associated link_map by doing much of the above internally.
--
Cheers,
Carlos.