确定 Linux 二进制文件的直接共享对象依赖项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6242761/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:02:33  来源:igfitidea点击:

Determine direct shared object dependencies of a Linux binary?

linuxelf

提问by Free Wildebeest

How can I easily find out the direct shared object dependencies of a Linux binary in ELF format?

如何轻松找出 ELF 格式的 Linux 二进制文件的直接共享对象依赖项?

I'm aware of the ldd tool, but that appears to output all dependencies of a binary, including the dependencies of any shared objects that binary is dependent on.

我知道 ldd 工具,但这似乎输出了二进制文件的所有依赖项,包括二进制文件所依赖的任何共享对象的依赖项。

采纳答案by Mat

You can use readelfto explore the ELF headers. readelf -dwill list the direct dependencies as NEEDEDsections.

您可以使用readelf来探索 ELF 标头。readelf -d将直接依赖项列为NEEDED部分。

 $ readelf -d elfbin

Dynamic section at offset 0xe30 contains 22 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libssl.so.1.0.0]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000c (INIT)               0x400520
 0x000000000000000d (FINI)               0x400758
 ...

回答by Free Wildebeest

The objdumptool can tell you this information. If you invoke objdumpwith the -xoption, to get it to output all headers then you'll find the shared object dependencies right at the start in the "Dynamic Section".

objdump工具可以告诉您这些信息。如果您objdump使用该-x选项调用,以使其输出所有标头,那么您将在“动态部分”的开头找到共享对象依赖项。

For example running objdump -x /usr/lib/libXpm.so.4on my system gives the following information in the "Dynamic Section":

例如,objdump -x /usr/lib/libXpm.so.4在我的系统上运行会在“动态部分”中提供以下信息:

Dynamic Section:
  NEEDED               libX11.so.6
  NEEDED               libc.so.6
  SONAME               libXpm.so.4
  INIT                 0x0000000000002450
  FINI                 0x000000000000e0e8
  GNU_HASH             0x00000000000001f0
  STRTAB               0x00000000000011a8
  SYMTAB               0x0000000000000470
  STRSZ                0x0000000000000813
  SYMENT               0x0000000000000018
  PLTGOT               0x000000000020ffe8
  PLTRELSZ             0x00000000000005e8
  PLTREL               0x0000000000000007
  JMPREL               0x0000000000001e68
  RELA                 0x0000000000001b38
  RELASZ               0x0000000000000330
  RELAENT              0x0000000000000018
  VERNEED              0x0000000000001ad8
  VERNEEDNUM           0x0000000000000001
  VERSYM               0x00000000000019bc
  RELACOUNT            0x000000000000001b

The direct shared object dependencies are listing as 'NEEDED' values. So in the example above, libXpm.so.4on my system just needs libX11.so.6and libc.so.6.

直接共享对象依赖项列为“NEEDED”值。所以在上面的例子中,libXpm.so.4在我的系统上只需要libX11.so.6libc.so.6

It's important to note that this doesn't mean that all the symbols needed by the binary being passed to objdumpwill be present in the libraries, but it does at least show what libraries the loader will try to load when loading the binary.

需要注意的是,这并不意味着传递给的二进制文件所需的所有符号objdump都将出现在库中,但它至少显示了加载程序在加载二进制文件时将尝试加载哪些库。

回答by Serge C

If you want to find dependencies recursively(including dependencies of dependencies, dependencies of dependencies of dependencies and so on)…

如果要递归查找依赖(包括依赖的依赖,依赖的依赖的依赖等等)……

You may use lddcommand. ldd - print shared library dependencies

您可以使用ldd命令。 ldd - 打印共享库依赖项

回答by Hannes

ldd -v prints the dependency tree under "Version information:' section. The first block in that section are the direct dependencies of the binary.

ldd -v 打印“版本信息:”部分下的依赖树。该部分中的第一个块是二进制文件的直接依赖关系。

See Hierarchical ldd(1)

请参见分层 ldd(1)