I want to find the shared libs in order to find the rpm packages related to the path.
That information should also be found in the Makefile (or the tool that generates it), at least the basic library name.
Also, as documented in the ldd man page, objdump -p can be used to extract the names of shared libraries from the binary, without risking to accidentally executing it. What you get is a list of library names. Search CPAN, I would be surprised if no one has written a module to extract that information from an ELF file without running objdump.
Now that you got a list of library names, read the ld.so man page to find out how to create a list of possible file names from each library. ld.so reads /etc/ld.so.cache, which is binary noise containing a list of directories to search and an ordered list of candidate libraries. It is generated by ldconfig, which also has a man page. ldconfig reads /etc/ld.so.conf, the format of that file is documented in the ldconfig man page. Assuming that ld.so.conf was not changed after the last run of ldconfig, you don't need to read ld.so.cache at all, and can simply read ld.so.conf. That, combined with the rules and the substitued tokens explained in the ld.so man page, is all you need to re-implement in perl. Essentially, just a little bit of string operations and some calls to stat (in form of -f -x $candidate).
And now, the really bad news:
That's not sufficient. Nor is ldd.
Programms can load libraries at runtime. And they do, often without telling you. You don't really want to know what happens when Name Service Switch (see man 5 nss) or PAM (see man 8 pam) start doing their jobs. But the worst part: You can not extract that information from the binary, because it is not (always) contained in the binary. NSS and PAM highly depend on the system configuration. And NSS and PAM are just two examples. Perl loads libraries at runtime every time you use an XS module.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
|