1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | |
---|
6 | sub cmd2arr($\@) { |
---|
7 | my ($cmd, $arr_r) = @_; |
---|
8 | print STDERR "[Action: '$cmd']\n"; |
---|
9 | open(CMD, $cmd.'|') || die "failed to fork '$cmd' (Reason: $!)"; |
---|
10 | my $line; |
---|
11 | while (defined($line=<CMD>)) { |
---|
12 | chomp($line); |
---|
13 | push @$arr_r, $line; |
---|
14 | } |
---|
15 | close(CMD) || die "failed to execute '$cmd' (Reason: $! exitcode=$?)"; |
---|
16 | } |
---|
17 | |
---|
18 | my $COMMON_NM_OPTIONS = "--demangle --format=posix"; |
---|
19 | |
---|
20 | sub list_undefined($\@) { |
---|
21 | my ($source, $arr_r) = @_; |
---|
22 | my $cmd = "nm $COMMON_NM_OPTIONS --undefined-only $source"; |
---|
23 | cmd2arr($cmd,@$arr_r); |
---|
24 | } |
---|
25 | |
---|
26 | sub list_defined($\@) { |
---|
27 | my ($source, $arr_r) = @_; |
---|
28 | my $cmd = "nm $COMMON_NM_OPTIONS --defined-only --extern-only $source"; |
---|
29 | cmd2arr($cmd,@$arr_r); |
---|
30 | } |
---|
31 | |
---|
32 | sub die_usage($) { |
---|
33 | my ($msg) = @_; |
---|
34 | |
---|
35 | print STDERR "Usage: filter_defined_symbols.pl OBJECT1 OBJECT2 [ ... OBJECTN ]\n"; |
---|
36 | print STDERR "Test whether OBJECT2 ... OBJECTN define all symbols which are required by (=undefined in) OBJECT1\n"; |
---|
37 | print STDERR "List all symbols which are required by (=undefined in) OBJECT1 and\n"; |
---|
38 | print STDERR "are not defined in OBJECT2 ... OBJECTN\n"; |
---|
39 | |
---|
40 | die $msg."\n"; |
---|
41 | } |
---|
42 | |
---|
43 | sub eliminate_defined(\@\@\@) { |
---|
44 | my ($undefined_r, $defined_r, $notresolved_r) = @_; |
---|
45 | |
---|
46 | my %undefined = map { if (/ U /o) { $` => $_; } else { ; } } @$undefined_r; |
---|
47 | die "nothing undefined (probably broken)" if not scalar(%undefined); |
---|
48 | |
---|
49 | my %defined = (); |
---|
50 | foreach (@$defined_r) { |
---|
51 | if (/ [TB] /o) { |
---|
52 | my $sym = $`; |
---|
53 | if (exists $undefined{$sym}) { |
---|
54 | $defined{$sym} = 1; |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | foreach (keys %undefined) { |
---|
60 | if (not exists $defined{$_}) { |
---|
61 | push @$notresolved_r, $undefined{$_}; |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | } |
---|
66 | |
---|
67 | sub filter_defined_symbols() { |
---|
68 | my $args = scalar(@ARGV); |
---|
69 | if ($args < 2) { |
---|
70 | die_usage("Missing arguments"); |
---|
71 | } |
---|
72 | else { |
---|
73 | my $OBJECT1 = shift @ARGV; |
---|
74 | my $OBJECT2 = shift @ARGV; |
---|
75 | |
---|
76 | my @undefined = (); # undefined symbols from OBJECT1 |
---|
77 | my @defined = (); # defined symbols from OBJECT2 |
---|
78 | |
---|
79 | list_undefined($OBJECT1, @undefined); |
---|
80 | list_defined($OBJECT2, @defined); |
---|
81 | |
---|
82 | my @notresolved = (); |
---|
83 | |
---|
84 | eliminate_defined(@undefined, @defined, @notresolved); |
---|
85 | |
---|
86 | while (scalar(@ARGV)) { |
---|
87 | my $OBJECTN = shift @ARGV; |
---|
88 | |
---|
89 | if (not scalar(@notresolved)) { |
---|
90 | die "no notresolved symbols left (before considering '$OBJECTN')"; |
---|
91 | } |
---|
92 | |
---|
93 | @undefined = @notresolved; |
---|
94 | @defined = (); |
---|
95 | |
---|
96 | list_defined($OBJECTN, @defined); |
---|
97 | |
---|
98 | @notresolved = (); |
---|
99 | eliminate_defined(@undefined, @defined, @notresolved); |
---|
100 | } |
---|
101 | |
---|
102 | foreach (sort @notresolved) { |
---|
103 | print $_."\n"; |
---|
104 | } |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | filter_defined_symbols(); |
---|