6 use constant WARNING_SIZE => 512;
10 # Scan output of "objdump -w -t bin/blib.a" and build up symbol table
15 if ( /^In archive/ ) {
19 } elsif ( /^(\S+\.o):\s+file format/ ) {
21 } elsif ( /^SYMBOL TABLE:/ ) {
23 } elsif ( /^([0-9a-fA-F]+)\s(l|g|\s)......\s(\S+)\s+([0-9a-fA-F]+)\s+(\S+)$/ ) {
29 $symtab->{$object}->{$symbol} = {
30 global => ( $scope ne "l" ),
31 section => ( $section eq "*UND*" ? undef : $section ),
32 value => ( $value ? hex ( $value ) : 0 ),
33 size => ( $size ? hex ( $size ) : 0 ),
36 die "Unrecognized line \"$_\"";
40 # Add symbols that we know will be generated or required by the linker
42 foreach my $object ( keys %$symtab ) {
43 my $obj_symbol = "obj_$object";
44 $obj_symbol =~ s/\.o$//;
45 $obj_symbol =~ s/\W/_/g;
46 $symtab->{LINKER}->{$obj_symbol} = {
53 foreach my $link_sym qw ( __prefix _prefix _prefix_load_offset
54 _prefix_size _prefix_progbits_size _prefix_size_pgh
55 __text16 _text16 _text16_load_offset
56 _text16_size _text16_progbits_size _text16_size_pgh
57 __data16 _data16 _data16_load_offset
58 _data16_size _data16_progbits_size _data16_size_pgh
59 __text _text __data _data _textdata_load_offset
60 _textdata_size _textdata_progbits_size
62 _payload_offset _max_align
63 _load_size _load_size_pgh _load_size_sect
64 pci_vendor_id pci_device_id ) {
65 $symtab->{LINKER}->{$link_sym} = {
73 # Build up requires, provides and shares symbol tables for global
77 while ( ( my $object, my $symbols ) = each %$symtab ) {
78 while ( ( my $symbol, my $info ) = each %$symbols ) {
79 if ( $info->{global} ) {
81 if ( ! defined $info->{section} ) {
82 $category = "requires";
83 } elsif ( $info->{section} eq "*COM*" ) {
86 $category = "provides";
88 $globals->{$symbol}->{$category}->{$object} = $info->{section};
93 # Check for multiply defined, never-defined and unused global symbols
96 while ( ( my $symbol, my $info ) = each %$globals ) {
97 my @provides = keys %{$info->{provides}};
98 my @requires = keys %{$info->{requires}};
99 my @shares = keys %{$info->{shares}};
101 if ( ( @requires > 0 ) && ( @provides == 0 ) && ( @shares == 0 ) ) {
102 # No object provides this symbol, but some objects require it.
103 $problems->{$_}->{nonexistent}->{$symbol} = 1 foreach @requires;
106 if ( ( @requires == 0 ) && ( @provides > 0 ) ) {
107 # No object requires this symbol, but some objects provide it.
108 foreach my $provide ( @provides ) {
109 if ( $provide eq "LINKER" ) {
110 # Linker-provided symbols are exempt from this check.
111 } elsif ( $info->{provides}->{$provide} =~ /^\.tbl\./ ) {
112 # Linker tables are exempt from this check.
114 $problems->{$provide}->{unused}->{$symbol} = 1;
119 if ( ( @shares > 0 ) && ( @provides > 0 ) ) {
120 # A shared symbol is being initialised by an object
121 $problems->{$_}->{shared}->{$symbol} = 1 foreach @provides;
124 if ( @provides > 1 ) {
125 # A non-shared symbol is defined in multiple objects
126 $problems->{$_}->{multiples}->{$symbol} = 1 foreach @provides;
130 # Check for excessively large local symbols. Text and rodata symbols
131 # are exempt from this check
133 while ( ( my $object, my $symbols ) = each %$symtab ) {
134 while ( ( my $symbol, my $info ) = each %$symbols ) {
135 if ( ( ! $info->{global} ) &&
136 ( ( defined $info->{section} ) &&
137 ! ( $info->{section} =~ /^(\.text|\.rodata)/ ) ) &&
138 ( $info->{size} >= WARNING_SIZE ) ) {
139 $problems->{$object}->{large}->{$symbol} = 1;
144 # Print out error messages
148 foreach my $object ( sort keys %$problems ) {
149 my @nonexistent = sort keys %{$problems->{$object}->{nonexistent}};
150 my @multiples = sort keys %{$problems->{$object}->{multiples}};
151 my @unused = sort keys %{$problems->{$object}->{unused}};
152 my @shared = sort keys %{$problems->{$object}->{shared}};
153 my @large = sort keys %{$problems->{$object}->{large}};
155 print "WARN $object provides unused symbol $_\n" foreach @unused;
156 $warnings += @unused;
157 print "WARN $object has large static symbol $_\n" foreach @large;
159 print "ERR $object requires non-existent symbol $_\n" foreach @nonexistent;
160 $errors += @nonexistent;
161 foreach my $symbol ( @multiples ) {
162 my @other_objects = sort grep { $_ ne $object }
163 keys %{$globals->{$symbol}->{provides}};
164 print "ERR $object provides symbol $symbol"
165 ." (also provided by @other_objects)\n";
167 $errors += @multiples;
168 print "ERR $object misuses shared symbol $_\n" foreach @shared;
171 print "$errors error(s), $warnings warning(s)\n";
172 exit ( $errors ? 1 : 0 );