Debian Codegraph @

pTk/why

raw
1 #!/usr/local/bin/perl -w
2
3 use strict;
4
5 my %ref;
6 my %def;
7 my %file;
8
9 sub read_object
10 {
11 my $obj = shift;
12 open(NM,"nm -p $obj|") || die "Cannot open nm $obj:$!";
13 $def{$obj} = {};
14 $file{$obj} = {};
15 my @refs = ();
16 while (<NM>)
17 {
18 if (/\b([A-Z])\b\s*_?(.*)$/)
19 {
20 my ($kind,$name) = ($1,$2);
21 $file{$obj}{$name} = $kind;
22 if ($kind ne 'U')
23 {
24 $def{$obj}{$name} = $kind;
25 }
26 else
27 {
28 $ref{$name} = [] unless (exists $ref{$name});
29 push(@{$ref{$name}},$obj);
30 push(@refs,$name);
31 }
32 }
33 }
34 close(NM);
35 return @refs;
36 }
37
38 warn "Scanning object files\n";
39 foreach my $obj (<*.o>,<../*.o>)
40 {
41 # next if $obj =~ /_f\.o$/;
42 read_object($obj);
43 }
44
45 warn "Analysing...\n";
46 if (@ARGV)
47 {
48 FILE:
49 foreach my $file (@ARGV)
50 {
51 if (exists $file{$file})
52 {
53 foreach my $sym (keys %{$def{$file}})
54 {
55 if (exists $ref{$sym})
56 {
57 my @files = grep(!/_f\.o$/,@{$ref{$sym}});
58 if (@files)
59 {
60 print "$file defines $sym for ",join(',',@files),"\n";
61 next FILE;
62 }
63 }
64 }
65 warn "No good reason for $file!\n";
66 }
67 else
68 {
69 my %exc;
70 my @sym = read_object($file);
71 foreach my $sym (sort @sym)
72 {
73 next unless $sym =~ /^[TL]/;
74 if (exists $ref{$sym})
75 {
76 my @files = grep($_ ne $file,grep(!/_f\.o$/,@{$ref{$sym}}));
77 if (@files)
78 {
79 print "$sym needed by ",join(',',@files),"\n";
80 }
81 else
82 {
83 @files = grep(/_f\.o$/,@{$ref{$sym}});
84 if (@files)
85 {
86 warn "$sym only used in ",join(',',@files),"\n";
87 if (@files == 1)
88 {
89 my $exc = $files[0];
90 die "Strange $exc" unless $exc =~ s/_f\.o$/.nexc/;
91 $exc{$exc} = [] unless exists $exc{$exc};
92 push(@{$exc{$exc}},$sym);
93 }
94 }
95 }
96 }
97 }
98 foreach my $exc (keys %exc)
99 {
100 open(EXC,">$exc") || die "Cannot open $exc:$!";
101 print EXC join("\n",@{$exc{$exc}},'');
102 close(EXC);
103 }
104 }
105 }
106 }
107