How to get dependencies (linked libraries) from a shared object (*.so)?

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_castllvm::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

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_castllvm::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