[LLVMdev] How to get dependencies (linked libraries) from a shared object (*.so)?
Dimitar Dobrev
dpldobrev at yahoo.com
Sun Aug 10 02:04:00 PDT 2014
Hello all,
I need to get all dependencies of a .so file. I tried using needed_library_begin/needed_library_end but they seem not to give the dependencies but something else (if anything) I cannot quite figure out what. I tried testing with Linux's ls. I ran "ldd ls" (ldd is Linux-only)and got about six dependencies - as an example for one of them I can remember libc. Then I tried this code:
for (auto dep = ObjectFile->needed_library_begin(); dep != ObjectFile->needed_library_end(); ++dep)
{
llvm::StringRef Path;
if (!dep->getPath(Path))
...
}
where ObjectFile represents the opened *.so - this pointer is guaranteed to be correct because I read all exported symbols from it in the same function. Further, I opened both ls and *.so with a text editor. They are, of course, binaries, but their dependencies can actually be seen as text fragments inside. So Clang should be able to read them in some manner, I just don't know what. This is why I'll appreciate your help.
As a final, hopefully helpful, reference, I'm applying the code I use to successfully get the dependencies of a Windows DLL:
if (auto COFFObjectFile = llvm::dyn_cast<llvm::object::COFFObjectFile>(ObjectFile))
{
for (auto dep = COFFObjectFile->import_directory_begin(); dep != COFFObjectFile->import_directory_end(); ++dep)
{
llvm::StringRef Name;
if (!dep->getName(Name) && (Name.endswith(".dll") || Name.endswith(".DLL")))
...
}
}
Regards,
Dimitar Dobrev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140810/abdc95c4/attachment.html>
More information about the llvm-dev
mailing list