我想知道我的系统上的可执行文件使用了哪些库。更具体地说,我想对使用最多的库以及使用它们的二进制文件进行排名。我该怎么做呢?


当前回答

我发现这篇文章非常有用,因为我需要调查来自第三方提供的库的依赖关系(32位执行路径vs 64位执行路径)。

我在RHEL 6发行版上基于'readelf -d'建议组合了一个Q&D递归bash脚本。

它是非常基本的,每次都会测试每个依赖项,即使之前可能已经测试过了(即非常冗长)。输出也是非常基本的。

#! /bin/bash

recurse ()
# Param 1 is the nuumber of spaces that the output will be prepended with
# Param 2 full path to library
{
#Use 'readelf -d' to find dependencies
dependencies=$(readelf -d ${2} | grep NEEDED | awk '{ print $5 }' | tr -d '[]')
for d in $dependencies; do
   echo "${1}${d}"
   nm=${d##*/}
   #libstdc++ hack for the '+'-s
   nm1=${nm//"+"/"\+"}
   # /lib /lib64 /usr/lib and /usr/lib are searched
   children=$(locate ${d} | grep -E "(^/(lib|lib64|usr/lib|usr/lib64)/${nm1})")
   rc=$?
   #at least locate... didn't fail
   if [ ${rc} == "0" ] ; then
      #we have at least one dependency
      if [ ${#children[@]} -gt 0 ]; then
         #check the dependeny's dependencies
         for c in $children; do
          recurse "  ${1}" ${c}
         done
      else
         echo "${1}no children found"
      fi
   else
      echo "${1}locate failed for ${d}"
   fi
done
}
# Q&D -- recurse needs 2 params could/should be supplied from cmdline
recurse "" !!full path to library you want to investigate!!

将输出重定向到一个文件并grep 'found'或'failed'

使用和修改,当然风险由你自己承担,如你所愿。

其他回答

在ubuntu上 打印与可执行文件相关的包

ldd executable_name|awk '{print $3}'|xargs dpkg -S |awk -F  ":"  '{print $1}'

在Linux上我使用:

lsof -P -T -p Application_PID

当可执行文件使用非默认加载器时,这比ldd工作得更好

如果你不关心可执行文件的路径-

ldd `which <executable>` # back quotes, not single quotes

我发现这篇文章非常有用,因为我需要调查来自第三方提供的库的依赖关系(32位执行路径vs 64位执行路径)。

我在RHEL 6发行版上基于'readelf -d'建议组合了一个Q&D递归bash脚本。

它是非常基本的,每次都会测试每个依赖项,即使之前可能已经测试过了(即非常冗长)。输出也是非常基本的。

#! /bin/bash

recurse ()
# Param 1 is the nuumber of spaces that the output will be prepended with
# Param 2 full path to library
{
#Use 'readelf -d' to find dependencies
dependencies=$(readelf -d ${2} | grep NEEDED | awk '{ print $5 }' | tr -d '[]')
for d in $dependencies; do
   echo "${1}${d}"
   nm=${d##*/}
   #libstdc++ hack for the '+'-s
   nm1=${nm//"+"/"\+"}
   # /lib /lib64 /usr/lib and /usr/lib are searched
   children=$(locate ${d} | grep -E "(^/(lib|lib64|usr/lib|usr/lib64)/${nm1})")
   rc=$?
   #at least locate... didn't fail
   if [ ${rc} == "0" ] ; then
      #we have at least one dependency
      if [ ${#children[@]} -gt 0 ]; then
         #check the dependeny's dependencies
         for c in $children; do
          recurse "  ${1}" ${c}
         done
      else
         echo "${1}no children found"
      fi
   else
      echo "${1}locate failed for ${d}"
   fi
done
}
# Q&D -- recurse needs 2 params could/should be supplied from cmdline
recurse "" !!full path to library you want to investigate!!

将输出重定向到一个文件并grep 'found'或'failed'

使用和修改,当然风险由你自己承担,如你所愿。

检查程序可执行文件的共享库依赖关系

要找出特定可执行文件所依赖的库,可以使用ldd命令。这个命令调用动态链接器来查找可执行文件的库依赖关系。

>$ldd /path/to/program

注意,不建议使用任何不受信任的第三方可执行文件运行ldd,因为某些版本的ldd可能直接调用可执行文件来识别其库依赖关系,这可能存在安全风险。

相反,更安全的显示未知应用程序二进制程序的库依赖关系的方法是使用以下命令。

$ objdump -p /path/to/program | grep需要

更多信息