在指定目录中查找依赖于指定库的所有可执行文件



我的目标是编写一个使用"objdump-p";命令在指定目录中查找依赖于指定库的所有可执行文件。(OpenBSD(。我试着这样做:

find $1 -perm -111 -print0 | xargs -r0 objdump -p | grep -l "NEEDED      $2"

但这个解决方案不起作用,因为grep无法找出找到给定匹配项的文件名。困难在于确定grep在其中找到指定库的可执行文件的名称。有人能用";objdump-p";命令

诀窍是执行一个shell脚本,而不是执行一个命令来重用文件名。

finddepend() {
# Arg 1: The directory where to find
# Arg 2: The library name
basedir=$1
libname=$2
find "$basedir" 
( -perm -100 -o -perm -010 -o -perm -001 ) 
( -type f -o -type l ) 
-exec sh -c '
# Arg 0: Is a dummy _ for this inline script
# Arg 1: The executable file path
# Arg 2: The library name
filepath=$1
libname=$2
objdump -p "$filepath" 2>/dev/null |
if grep -qF "  NEEDED               $libname"; then
printf %s\n "${filepath##*/}"
fi
' _ {} "$libname" ;
}

示例用法:

finddepend /bin libselinux.so
mv
systemctl
tar
sed
udevadm
ls
mknod
systemd
mkdir
ss
dir
vdir
cp
systemd-hwdb
netstat

既然可以使用ldd(列出动态依赖项(,为什么要使用objdumpobjdump提供了一个完整的摘要,您需要对其进行处理才能获得您想要的信息,而ldd只提供这些信息。

最新更新