71

Since ldd lists only the dynamic libraries, is there a way to extract the information about the static libraries used to create the executable?

nobody
  • 19,814
  • 17
  • 56
  • 77
Saurabh
  • 807
  • 1
  • 8
  • 7

5 Answers5

79

ldd <exe filename> shows dynamically linked libraries

nm <exe filename> shows the symbols in the file.

To see which symbols come from static libraries requires running nm against those libraries to get a list of the symbols (functions, etc.) in them, then comparing them to what your list of symbols from nm <exe filename>.

You compare lists with the comm command. See man comm for details.

This was taken from this forum here.

prc
  • 860
  • 7
  • 16
DrAl
  • 70,428
  • 10
  • 106
  • 108
  • 4
    As @Goz and anon point out, this only works if the binary hasn't been stripped / contains debugging information. Names are not necessary (and are not even used) after a static library has been linked into an application - the calls are all by address. – nobody May 14 '14 at 23:23
  • 5
    This does not answer the question. "running nm against those libraries" is not possible if you do not know the libraries; and there are libraries used implicitly in linking. – kavadias Mar 02 '15 at 15:38
  • 2
    If its an unknown binary, we don't know what libraries are present. So "running nm against those libraries " sounds self defeating. – goldenmean Sep 29 '15 at 08:40
14

No, the names of the libraries are discarded during the linking process. However, if your executable contains debug information (i.e. it was compiled with the -g flag), you may be able to get information from that.

  • 1
    Is there no way to discern the RAW ASM, or would compiler optimization and flags also affect that? – MrMesees Dec 16 '17 at 16:36
10

If you have the source code and don't want to go through all the code for this, you can generate map file while compiling to know which static libraries are linked.

For example g++ -Xlinker -Map=a.map main.c, check the map file for linked static library information.

xzhangxa
  • 101
  • 1
  • 5
10

There is no way to get the list of static libraries inside some ELF executable.

Because for the linker, a static library is just used as a "lazy" set of members. So the resulting ELF executable would only contain the members needed to link it. So members like foo2.o of libfoo.a are linked as if object file foo2.o was linked into the executable (provided some symbol defined in foo2 is needed, i.e. is referenced somewhere).

Of course, using nm, or objdump, or readelf, or strings on some ELF executable may give some hints about what object files (including those coming from static libraries) are inside it, because you'll see symbols defined in (members of) those static libraries (or literal strings used inside them).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • 1
    what kind hints do you mean? Can you give examples? Can you point me to source where I can find more of these hints? – langlauf.io Jun 21 '16 at 09:29
  • 1
    Using readelf for example will show you functions, objects, symbols that are used in the binary. These can serve as hints to find the libraries that were used.For example you may see the Curl_http function in there and know that libcurl is most likely used by the binary and if it is not dynamically linked it has to be linked statically. – Marcell Oct 13 '17 at 10:03
7

Unless a given compiler stores some sort of meta data inside the binary then, no. A static library is code that is directly compiled into the binary.

Goz
  • 61,365
  • 24
  • 124
  • 204