[prev in list] [next in list] [prev in thread] [next in thread] 

List:       llvm-dev
Subject:    [LLVMdev] How to get dependencies (linked libraries) from a shared object (*.so)?
From:       Dimitar Dobrev <dpldobrev () yahoo ! com>
Date:       2014-08-10 9:04:00
Message-ID: 1407661440.85010.YahooMailNeo () web122405 ! mail ! ne1 ! yahoo ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


    

    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


[Attachment #5 (text/html)]

<html><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, \
Helvetica Neue, Helvetica, Arial, Lucida Grande, \
sans-serif;font-size:12pt"><div><span></span><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; <br style=""></span></div><div \
style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, \
sans-serif; font-size: 12pt;"><div class="y_msg_container"><div \
id="yiv2823908015"><div><div \
style="color:#000;background-color:#fff;font-family:HelveticaNeue, Helvetica Neue, \
Helvetica, Arial, Lucida Grande, sans-serif;font-size:12pt;"><div \
class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; Hello all,</span></div><div \
class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue,  Helvetica Neue, Helvetica, Arial, \
Lucida Grande, sans-serif;background-color:transparent;font-style:normal;"><br \
style="" class="yiv2823908015"><span style="" class="yiv2823908015"></span></div><div \
class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent; font-style:normal;"><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; 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)</span> I cannot quite figure out \
what. I tried testing with Linux's ls. I ran "ldd ls" (ldd is Linux-only)<span \
style="" class="yiv2823908015"> and got about six dependencies - as an example for \
one of them I can remember libc. Then I tried this code:</span></div><div \
class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; <br style="" \
class="yiv2823908015"></span></div><div class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande,  sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015"></span><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (auto dep = \
ObjectFile-&gt;needed_library_begin(); dep != ObjectFile-&gt;needed_library_end(); \
++dep)<br style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
{<br style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
llvm::StringRef Path;<br style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
if (!dep-&gt;getPath(Path))</span></div><div class="yiv2823908015" \
style="color:rgb(0, 0, 0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, \
Helvetica, Arial, Lucida Grande, \
sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; </span><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp;  </span><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; </span><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; ...</span><span style="" \
class="yiv2823908015"><br style="" class="yiv2823908015"></span></div><div \
class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span></div><div \
class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent;font-style:normal;"><br style="" \
class="yiv2823908015"><span style="" class="yiv2823908015"></span></div><div \
class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande,  sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; 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.</span></div><div \
class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; As a final, hopefully helpful, reference, \
I'm applying the code I use to successfully get the dependencies of a Windows \
DLL:</span></div><div class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015"><br></span></div><div class="yiv2823908015" style="color:rgb(0, \
0,  0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, \
Lucida Grande, sans-serif;background-color:transparent;font-style:normal;"><span \
style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (auto \
COFFObjectFile = llvm::dyn_cast&lt;llvm::object::COFFObjectFile&gt;(ObjectFile))<br \
style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br \
style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
for (auto dep = COFFObjectFile-&gt;import_directory_begin(); dep != \
COFFObjectFile-&gt;import_directory_end(); ++dep)<br style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
{<br style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
llvm::StringRef Name;<br style=""  \
class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
if (!dep-&gt;getName(Name) &amp;&amp; (Name.endswith(".dll") || \
Name.endswith(".DLL")))<br style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
...<br style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
}<br style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br \
style="" class="yiv2823908015"></span></div><div class="yiv2823908015" \
style="color:rgb(0, 0, 0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, \
Helvetica, Arial, Lucida Grande, \
sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="" \
class="yiv2823908015">&nbsp;&nbsp;&nbsp; </span><span style=""  \
class="yiv2823908015"><br></span></div><div class="yiv2823908015" style="color:rgb(0, \
0, 0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, \
Lucida Grande, sans-serif;background-color:transparent;font-style:normal;"><span \
class="yiv2823908015tab">&nbsp;&nbsp;&nbsp; </span>Regards,</div><div \
class="yiv2823908015" style="color:rgb(0, 0, 0); \
font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent;font-style:normal;"><span \
class="yiv2823908015tab">&nbsp;&nbsp;&nbsp; </span>Dimitar Dobrev<br><span style="" \
class="yiv2823908015"></span></div><div class="yiv2823908015" style="color:rgb(0, 0, \
0);font-size:16px;font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida \
Grande, sans-serif;background-color:transparent;font-style:normal;"><span style="" \
class="yiv2823908015"></span><span style="" class="yiv2823908015">&nbsp;&nbsp;&nbsp; \
<br style="" class="yiv2823908015"></span></div></div></div></div><br><br></div>  \
</div><div class="qtdSeparateBR"><br><br></div><div style="display: block;" \
class="yahoo_quoted"><div style="font-family: HelveticaNeue, Helvetica Neue, \
Helvetica, Arial, Lucida Grande, sans-serif; font-size: 12pt;"> </div>  </div> \
</div></body></html>



_______________________________________________
LLVM Developers mailing list
LLVMdev@cs.uiuc.edu         http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic