1#!/usr/bin/perl -w 2# find a list of fns and variables in the code that could be static 3# usually called with something like this: 4# findstatic.pl `find . -name "*.o"` 5# Andrew Tridgell <tridge@samba.org> 6 7use strict; 8 9# use nm to find the symbols 10my($saved_delim) = $/; 11undef $/; 12my($syms) = `nm -o @ARGV`; 13$/ = $saved_delim; 14 15my(@lines) = split(/\n/s, $syms); 16 17my(%def); 18my(%undef); 19my(%stype); 20 21my(%typemap) = ( 22 "T" => "function", 23 "C" => "uninitialised variable", 24 "D" => "initialised variable" 25 ); 26 27 28# parse the symbols into defined and undefined 29for (my($i)=0; $i <= $#{@lines}; $i++) { 30 my($line) = $lines[$i]; 31 if ($line =~ /(.*):[a-f0-9]* ([TCD]) (.*)/) { 32 my($fname) = $1; 33 my($symbol) = $3; 34 push(@{$def{$fname}}, $symbol); 35 $stype{$symbol} = $2; 36 } 37 if ($line =~ /(.*):\s* U (.*)/) { 38 my($fname) = $1; 39 my($symbol) = $2; 40 push(@{$undef{$fname}}, $symbol); 41 } 42} 43 44# look for defined symbols that are never referenced outside the place they 45# are defined 46foreach my $f (keys %def) { 47 print "Checking $f\n"; 48 my($found_one) = 0; 49 foreach my $s (@{$def{$f}}) { 50 my($found) = 0; 51 foreach my $f2 (keys %undef) { 52 if ($f2 ne $f) { 53 foreach my $s2 (@{$undef{$f2}}) { 54 if ($s2 eq $s) { 55 $found = 1; 56 $found_one = 1; 57 } 58 } 59 } 60 } 61 if ($found == 0) { 62 my($t) = $typemap{$stype{$s}}; 63 print " '$s' is unique to $f ($t)\n"; 64 } 65 } 66 if ($found_one == 0) { 67 print " all symbols in '$f' are unused (main program?)\n"; 68 } 69} 70 71