5
if ($ARGV[0] eq '--version') {
6
print "scandeps version 1.0\n";
7
print "Copright (c) 2005 Hongli Lai\n";
8
print "Licensed under the GNU General Public License v3.\n";
10
} elsif ($ARGV[0] eq '--help') {
11
print "Usage: scandeps [FILES...]\n";
12
print "This script scans ELF binaries' library dependancies and prints a nice report\n" .
13
"of the dependancies.\n";
18
# First, generate a list of binaries. Either scan user-specified files,
19
# or scan all files in the current directory (including subfolders).
23
# Scan user-specified files
26
# Scan directory recursively
27
@files = scandir(".");
30
# Now get the dependancies for all binaries
31
# %fileDeps: hash which contains a list of dependancies. The filename is the key.
32
# %depFiles: hash which contains a list of files. The dependancy is the key.
33
my (%fileDeps, %depFiles);
34
getdeps(\%fileDeps, \%depFiles, \@files);
36
print "Common dependancies:\n";
37
foreach my $dep (keys %depFiles) {
38
if (@{$depFiles{$dep}} == @files) {
44
print "All dependancies:\n";
45
my @keys = keys %depFiles;
46
@keys = sort { @{$depFiles{$b}} - @{$depFiles{$a}} } @keys;
47
foreach my $dep (@keys) {
48
my $num = scalar(@{$depFiles{$dep}});
49
my $word = ($num > 1) ? "files" : "file";
50
printf " %-40s (used by %3d $word)\n", $dep, $num;
54
print "Dependancies and associated binaries:\n";
55
foreach my $dep (@keys) {
57
if (@{$depFiles{$dep}} == @files) {
58
print " All files depend on this library.\n";
60
foreach my $file (@{$depFiles{$dep}}) {
68
##################################
78
if (!open($f, $_[0])) {
79
print STDERR "Warning: cannot open file '$_[0]' for reading.\n";
86
return $data eq "\177ELF";
92
if (!opendir($d, $_[0])) {
93
print STDERR "Warning: cannot open directory '$_[0]' for reading.\n";
98
foreach my $file (readdir($d)) {
99
next if ($file eq "." || $file eq "..");
101
$file = "$_[0]/$file";
103
# Recurse into subdirectory
104
push @files, scandir("$file");
106
} elsif (-f $file && -x $file && isELF($file)) {
115
my ($fileDeps, $depFiles, $files) = @_;
117
if (!open2($r, $w, 'objdump', '-p', @{$files})) {
118
fatal("Cannot communicate with objdump.\n");
123
foreach my $line (<$r>) {
124
if ($line =~ /^(.+):[ \t]+file format /) {
126
$currentFile =~ s/^.\///;
128
} elsif ($line =~ /NEEDED[ \t]+(.+)$/) {
129
$fileDeps->{$currentFile} = [] if (!exists $fileDeps->{$currentFile});
130
push @{$fileDeps->{$currentFile}}, $1;
132
$depFiles->{$1} = [] if (!exists $depFiles->{$1});
133
push @{$depFiles->{$1}}, $currentFile;