From a function in a shared library, inside a running process (written in C), how do I discover where that shared library was loaded from?
All of the answers I've found involve using things such as ldd
at the command line, or by peeking in /proc/self/maps
.
On Win32, I'd just use GetModuleFileName(GetModuleHandle("foo.dll"), szPath, COUNTOF(szPath))
. What's the Linux equivalent?
Bonus question: I need the same information in OS X.
You could use dl_iterate_phdr to iterate all loaded libraries and their segments (similar functionality is available for OSX, see e.g. this question). But most of the projects just parse /proc/self/maps
.
As a side note, keep in mind that mappings may change dynamically (if libraries are loaded via dlopen
) so reading them at startup may not be enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With