Gentoo Archives: gentoo-commits

From: "Mike Frysinger (vapier)" <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-projects commit in pax-utils: lddtree.sh
Date: Mon, 31 Dec 2007 23:33:32
Message-Id: E1J9U8N-0001GK-QX@stork.gentoo.org
1 vapier 07/12/31 23:33:27
2
3 Added: lddtree.sh
4 Log:
5 print the ELF dependency tree as a ......... tree
6
7 Revision Changes Path
8 1.1 pax-utils/lddtree.sh
9
10 file : http://sources.gentoo.org/viewcvs.py/gentoo-projects/pax-utils/lddtree.sh?rev=1.1&view=markup
11 plain: http://sources.gentoo.org/viewcvs.py/gentoo-projects/pax-utils/lddtree.sh?rev=1.1&content-type=text/plain
12
13 Index: lddtree.sh
14 ===================================================================
15 #!/bin/bash
16
17 argv0=${0##*/}
18
19 usage() {
20 cat <<-EOF
21 Display ELF dependencies as a tree
22
23 Usage: ${argv0} [options] <ELF file[s]>
24
25 Options:
26 -a Show all duplicated dependencies
27 -x Run with debugging
28 -h Show this help output
29 EOF
30 exit ${1:-0}
31 }
32
33 SHOW_ALL=false
34 SET_X=false
35
36 opts="hax"
37 getopt -Q -- "${opts}" "$@" || exit 1
38 eval set -- $(getopt -- "${opts}" "$@")
39 while [[ -n $1 ]] ; do
40 case $1 in
41 -a) SHOW_ALL=true;;
42 -x) SET_X=true;;
43 -h) usage;;
44 --) shift; break;;
45 -*) usage 1;;
46 esac
47 shift
48 done
49
50 ${SET_X} && set -x
51
52 ret=0
53 error() {
54 echo "${argv0}: $*" 1>&2
55 ret=1
56 return 1
57 }
58
59 find_elf() {
60 local elf=$1 needed_by=$2
61 if [[ ${elf} == */* ]] && [[ -e ${elf} ]] ; then
62 echo "${elf}"
63 return 0
64 else
65 check_paths() {
66 local elf=$1 ; shift
67 local path
68 for path in "$@" ; do
69 if [[ -e ${path}/${elf} ]] ; then
70 echo "${path}/${elf}"
71 return 0
72 fi
73 done
74 return 1
75 }
76 check_paths "${elf}" $(scanelf -qF '#F%r' "${needed_by}") && return 0
77 check_paths "${elf}" $(sed -e 's:^[[:space:]]*#.*::' /etc/ld.so.conf) && return 0
78 fi
79 return 1
80 }
81
82 show_elf() {
83 local elf=$1 indent=$2 parent_elfs=$3
84 local rlib lib libs
85 local interp resolved=$(find_elf "${elf}")
86 elf=${elf##*/}
87
88 printf "%${indent}s%s => " "" "${elf}"
89 if [[ ,${parent_elfs}, == *,${elf},* ]] ; then
90 printf "!!! circular loop !!!\n" ""
91 return
92 fi
93 parent_elfs="${parent_elfs},${elf}"
94 printf "${resolved:-not found}"
95 if [[ ${indent} -eq 0 ]] ; then
96 interp=$(scanelf -qF '#F%i' "${elf}")
97 printf " (interpreter => ${interp:-none})"
98 interp=${interp##*/}
99 fi
100 printf "\n"
101
102 [[ -z ${resolved} ]] && return
103
104 libs=$(scanelf -qF '#F%n' "${resolved}")
105
106 local my_allhits
107 if ! ${SHOW_ALL} ; then
108 my_allhits="${allhits}"
109 allhits="${allhits},${interp},${libs}"
110 fi
111
112 for lib in ${libs//,/ } ; do
113 lib=${lib##*/}
114 [[ ,${my_allhits}, == *,${lib},* ]] && continue
115 rlib=$(find_elf "${lib}" "${resolved}")
116 show_elf "${rlib:-${lib}}" $((indent + 4)) "${parent_elfs}"
117 done
118 }
119
120 for elf in "$@" ; do
121 if [[ ! -e ${elf} ]] ; then
122 error "${elf}: file does not exist"
123 elif [[ ! -r ${elf} ]] ; then
124 error "${elf}: file is not readable"
125 elif [[ -d ${elf} ]] ; then
126 error "${elf}: is a directory"
127 else
128 allhits=""
129 [[ ${elf} != */* ]] && elf="./${elf}"
130 show_elf "${elf}" 0 ""
131 fi
132 done
133
134 exit ${ret}
135
136
137
138 --
139 gentoo-commits@g.o mailing list