From llvm-dev Sun Aug 10 09:04:00 2014 From: Dimitar Dobrev Date: Sun, 10 Aug 2014 09:04:00 +0000 To: llvm-dev Subject: [LLVMdev] How to get dependencies (linked libraries) from a shared object (*.so)? Message-Id: <1407661440.85010.YahooMailNeo () web122405 ! mail ! ne1 ! yahoo ! com> X-MARC-Message: https://marc.info/?l=llvm-dev&m=140767179025265 MIME-Version: 1 Content-Type: multipart/mixed; boundary="--===============7769649667663491161==" --===============7769649667663491161== Content-Type: multipart/alternative; boundary="-2030432097-591938986-1407661440=:85010" ---2030432097-591938986-1407661440=:85010 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable =A0=A0=A0 =0A=0A=A0=A0=A0 Hello all,=0A=0A=A0=A0=A0 I need to get all depen= dencies of a .so file. I tried using needed_library_begin/needed_library_en= d but they seem not to give the dependencies but something else (if anythin= g) 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 f= or one of them I can remember libc. Then I tried this code:=0A=A0=A0=A0 =0A= =0A=A0=A0=A0=A0=A0=A0=A0 for (auto dep =3D ObjectFile->needed_library_begin= (); dep !=3D ObjectFile->needed_library_end(); ++dep)=0A=A0=A0=A0=A0=A0=A0= =A0 {=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 llvm::StringRef Path;=0A=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0 if (!dep->getPath(Path))=0A=A0=A0=A0 =A0=A0=A0 =A0= =A0=A0 =A0=A0=A0 ...=0A=0A=A0=A0=A0=A0=A0=A0=A0 }=0A=0A=A0=A0=A0 where Obje= ctFile represents the opened *.so - this pointer is guaranteed to be correc= t 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, binari= es, but their dependencies can actually be seen as text fragments inside. S= o Clang should be able to read them in some manner, I just don't know what.= This is why I'll appreciate your help.=0A=A0=A0=A0 As a final, hopefully h= elpful, reference, I'm applying the code I use to successfully get the depe= ndencies of a Windows DLL:=0A=0A=A0=A0=A0=A0=A0=A0=A0 if (auto COFFObjectFi= le =3D llvm::dyn_cast(ObjectFile))=0A=A0=A0= =A0=A0=A0=A0=A0 {=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 for (auto dep =3D COF= FObjectFile->import_directory_begin(); dep !=3D COFFObjectFile->import_dire= ctory_end(); ++dep)=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 {=0A=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 llvm::StringRef Name;=0A=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0=A0=A0=A0=A0 if (!dep->getName(Name) && (Name.endswith(".dll= ") || Name.endswith(".DLL")))=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0=A0=A0=A0=A0 ...=0A=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }=0A=A0=A0=A0=A0=A0= =A0=A0 }=0A=0A=A0=A0=A0=A0=A0=A0=A0 =0A=0A=A0=A0=A0 Regards,=0A=A0=A0=A0 Di= mitar Dobrev ---2030432097-591938986-1407661440=:85010 Content-Type: text/html; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable
&n= bsp;  
    Hello all,

<= span style=3D"" class=3D"yiv2823908015">
    I need to get all dependencies of a .so file. I tri= ed using needed_library_begin/needed_library_end but they seem not to give = the dependencies but something else (if anything) I cannot quite fig= ure out what. I tried testing with Linux's ls. I ran "ldd ls" (ldd is Linux= -only) and got about six dependenc= ies - as an example for one of them I can remember libc. Then I tried this = code:
   
=         for (auto dep =3D ObjectFile->= ;needed_library_begin(); dep !=3D ObjectFile->needed_library_end(); ++de= p)
     &nbs= p;  {
    &n= bsp;       llvm::StringRef Path;
        &= nbsp;   if (!dep->getPath(Path))
    &= nbsp;       =     ...
=
      &n= bsp; }
    where ObjectFile represents t= he opened *.so - this pointer is guaranteed to be correct because I read al= l exported symbols from it in the same function. Further, I opened both ls = and *.so with a text editor. They=0A are, of course, binaries, but their de= pendencies can actually be seen as text fragments inside. So Clang should b= e able to read them in some manner, I just don't know what. This is why I'l= l appreciate your help.
  &nb= sp; As a final, hopefully helpful, reference, I'm applying the code I use t= o successfully get the dependencies of a Windows DLL:

     = ;   if (auto COFFObjectFile =3D llvm::dyn_cast<llvm::object::C= OFFObjectFile>(ObjectFile))
 =        {
            for (a= uto dep =3D COFFObjectFile->import_directory_begin(); dep !=3D COFFObjec= tFile->import_directory_end(); ++dep)
            {      &nb= sp;         llvm::StringRef Name;        &n= bsp;       if (!dep->getName(Name) &&a= mp; (Name.endswith(".dll") || Name.endswith(".DLL")))
         &n= bsp;          ...
        &= nbsp;   }
   = ;     }
       
   = ; Regards,
    Dimitar= Dobrev
  &= nbsp;




---2030432097-591938986-1407661440=:85010-- --===============7769649667663491161== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ LLVM Developers mailing list LLVMdev@cs.uiuc.edu http://llvm.cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev --===============7769649667663491161==--