OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / tools / libclean.sh
1 #!/bin/sh
2 #
3 # find unused shared libraries and remove them, repeat until there are no
4 # more to remove - david.mccullough@accelecon.com
5 #
6
7 if [ $# -ne 1 ]; then
8         echo "usage: $0 <romfsdir>" >&2
9         exit 1
10 fi
11
12 ROMFS="${1:-romfs}"
13 if [ ! -d "$ROMFS" ]; then
14         echo "Target directory $ROMFS does nto exist." >&2
15         exit 1
16 fi
17
18 TMPF=/tmp/$$.find-unused
19 TMPS=/tmp/$$.find-so
20 TMPW=/tmp/$$.find-work
21 trap "rm -f $TMPW $TMPS $TMPF $TMPF.1; exit  0" 0
22
23 echo "Cleaning rootfs ($ROMFS) of unused .so files ..."
24
25 # find all the shared libs used bya a file
26 find "$ROMFS" -type f | while read t; do
27         readelf -d $t 2> /dev/null | grep 'Shared lib' |
28                 sed -e 's/^.*\[//' -e 's/]$//' | while read c
29                 do
30                         echo "$c: $t"
31                 done
32 done > $TMPF
33 find "$ROMFS" -type f -a \( -name '*.so' -o -name '*.so.*' \) > $TMPS
34
35 find_symlinks()
36 {
37         find=`basename "$1"`
38         find "$ROMFS" -type l -a \( -name '*.so' -o -name '*.so.*' \) | while read lnk
39         do
40                 rf=`readlink -f $lnk`
41                 #echo "`basename $rf` ---- $find"
42                 [ "`basename $rf`" = "$find" ] && echo "$lnk"
43         done
44 }
45
46 find_pattern_in_files()
47 {
48         for i in $*
49         do
50                 egrep "^$i: " $TMPF
51         done | awk '{ printf $2 }'
52         echo
53 }
54
55 while :; do
56         rm -f $TMPW
57         while read so; do
58                 [ -f "$so" ] || continue
59                 LNKS="`find_symlinks \"$so\"`"
60
61                 LOOK="`basename $so`"
62                 for i in $LNKS
63                 do
64                         LOOK="$LOOK `basename $i`"
65                 done
66
67                 # echo "checking $so - $LOOK ..."
68
69                 USERS=`find_pattern_in_files "$LOOK"`
70
71                 #echo "$so:" $USERS
72                 if [ -z "$USERS" ]
73                 then
74                         case "$so" in
75                         *plugin*)
76                                 echo "Skipping plugin file $so"
77                                 echo "`basename $so`: plugin" >> $TMPF
78                                 ;;
79                         *pam_*)
80                                 echo "Skipping PAM file $so"
81                                 echo "`basename $so`: PAM" >> $TMPF
82                                 ;;
83                         *netfilter*)
84                                 echo "Skipping netfilter file $so"
85                                 echo "`basename $so`: netfilter" >> $TMPF
86                                 ;;
87                         *)
88                                 touch $TMPW
89                                 echo "Removing $so ..."
90                                 rm -f "$so"
91                                 # remove reference this file has
92                                 egrep -v ": $so\$" $TMPF > $TMPF.1
93                                 cp $TMPF.1 $TMPF
94                                 rm -f $TMPF.1
95                                 ;;
96                         esac
97                 fi
98         done < $TMPS
99         [ -f "$TMPW" ] || break
100 done
101
102 exit 0