6 use constant WARNING_SIZE => 512;
10 # Scan output of "nm -o -S bin/blib.a" and build up symbol table
14 ( my $object, undef, my $value, undef, my $size, my $type, my $symbol )
15 = /^.*?:(.*?\.o):((\S+)(\s+(\S+))?)?\s+(\S)\s+(\S+)$/;
16 $symtab->{$object}->{$symbol} = {
17 global => ( $type eq uc $type ),
19 value => ( $value ? hex ( $value ) : 0 ),
20 size => ( $size ? hex ( $size ) : 0 ),
24 # Add symbols that we know will be generated or required by the linker
26 foreach my $object ( keys %$symtab ) {
27 my $obj_symbol = "obj_$object";
28 $obj_symbol =~ s/\.o$//;
29 $obj_symbol =~ s/\W/_/g;
30 $symtab->{LINKER}->{$obj_symbol} = {
37 foreach my $link_sym qw ( _prefix _eprefix _decompress _edecompress _text
38 _etext _data _edata _bss _ebss _end ) {
39 $symtab->{LINKER}->{$link_sym} = {
47 # Build up requires, provides and shares symbol tables for global
51 while ( ( my $object, my $symbols ) = each %$symtab ) {
52 while ( ( my $symbol, my $info ) = each %$symbols ) {
53 if ( $info->{global} ) {
54 my $category = ( ( $info->{type} eq 'U' ? "requires" :
55 ( $info->{type} eq 'C' ? "shares" : "provides" ) ) );
56 $globals->{$symbol}->{$category}->{$object} = 1;
61 # Check for multiply defined, never-defined and unused global symbols
64 while ( ( my $symbol, my $info ) = each %$globals ) {
65 my @provides = keys %{$info->{provides}};
66 my @requires = keys %{$info->{requires}};
67 my @shares = keys %{$info->{shares}};
69 if ( ( @provides == 0 ) && ( @shares == 1 ) ) {
70 # A symbol "shared" by just a single file is actually being
71 # provided by that file; it just doesn't have an initialiser.
76 if ( ( @requires > 0 ) && ( @provides == 0 ) ) {
77 # No object provides this symbol, but some objects require it.
78 $problems->{$_}->{nonexistent}->{$symbol} = 1 foreach @requires;
81 if ( ( @requires == 0 ) && ( @provides > 0 ) ) {
82 # No object requires this symbol, but some objects provide it.
83 $problems->{$_}->{unused}->{$symbol} = 1 foreach @provides;
86 if ( ( @shares > 0 ) && ( @requires > 0 ) ) {
87 # A shared symbol is being referenced from another object
88 $problems->{$_}->{shared}->{$symbol} = 1 foreach @requires;
91 if ( ( @shares > 0 ) && ( @provides > 0 ) ) {
92 # A shared symbol is being initialised by an object
93 $problems->{$_}->{shared}->{$symbol} = 1 foreach @provides;
96 if ( ( @shares > 0 ) && ! ( $symbol =~ /^_shared_/ ) ) {
97 # A shared symbol is not declared via __shared
98 $problems->{$_}->{shared}->{$symbol} = 1 foreach @shares;
101 if ( @provides > 1 ) {
102 # A non-shared symbol is defined in multiple objects
103 $problems->{$_}->{multiples}->{$symbol} = 1 foreach @provides;
107 # Check for excessively large local symbols. Text and rodata symbols
108 # are exempt from this check
110 while ( ( my $object, my $symbols ) = each %$symtab ) {
111 while ( ( my $symbol, my $info ) = each %$symbols ) {
112 if ( ( ! $info->{global} ) &&
113 ( ! ( $info->{type} =~ /^(t|r)$/ ) ) &&
114 ( $info->{size} >= WARNING_SIZE ) ) {
115 $problems->{$object}->{large}->{$symbol} = 1;
120 # Print out error messages
124 foreach my $object ( sort keys %$problems ) {
125 my @nonexistent = sort keys %{$problems->{$object}->{nonexistent}};
126 my @multiples = sort keys %{$problems->{$object}->{multiples}};
127 my @unused = sort keys %{$problems->{$object}->{unused}};
128 my @shared = sort keys %{$problems->{$object}->{shared}};
129 my @large = sort keys %{$problems->{$object}->{large}};
131 print "WARN $object provides unused symbol $_\n" foreach @unused;
132 $warnings += @unused;
133 print "WARN $object has large static symbol $_\n" foreach @large;
135 print "ERR $object requires non-existent symbol $_\n" foreach @nonexistent;
136 $errors += @nonexistent;
137 foreach my $symbol ( @multiples ) {
138 my @other_objects = sort grep { $_ ne $object }
139 keys %{$globals->{$symbol}->{provides}};
140 print "ERR $object provides symbol $symbol"
141 ." (also provided by @other_objects)\n";
143 $errors += @multiples;
144 print "ERR $object misuses shared symbol $_\n" foreach @shared;
147 print "$errors error(s), $warnings warning(s)\n";
148 exit ( $errors ? 1 : 0 );