You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

symcheck.pl 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use constant WARNING_SIZE => 512;
  5. my $symtab = {};
  6. # Scan output of "objdump -w -t bin/blib.a" and build up symbol table
  7. #
  8. my $object;
  9. while ( <> ) {
  10. chomp;
  11. if ( /^In archive/ ) {
  12. # Do nothing
  13. } elsif ( /^$/ ) {
  14. # Do nothing
  15. } elsif ( /^(\S+\.o):\s+file format/ ) {
  16. $object = $1;
  17. } elsif ( /^SYMBOL TABLE:/ ) {
  18. # Do nothing
  19. } elsif ( /^([0-9a-fA-F]+)\s(l|g|\s)......\s(\S+)\s+([0-9a-fA-F]+)\s+(\S+)$/ ) {
  20. my $value = $1;
  21. my $scope = $2;
  22. my $section = $3;
  23. my $size = $4;
  24. my $symbol = $5;
  25. $symtab->{$object}->{$symbol} = {
  26. global => ( $scope ne "l" ),
  27. section => ( $section eq "*UND*" ? undef : $section ),
  28. value => ( $value ? hex ( $value ) : 0 ),
  29. size => ( $size ? hex ( $size ) : 0 ),
  30. };
  31. } else {
  32. die "Unrecognized line \"$_\"";
  33. }
  34. }
  35. # Add symbols that we know will be generated or required by the linker
  36. #
  37. foreach my $object ( keys %$symtab ) {
  38. my $obj_symbol = "obj_$object";
  39. $obj_symbol =~ s/\.o$//;
  40. $obj_symbol =~ s/\W/_/g;
  41. $symtab->{LINKER}->{$obj_symbol} = {
  42. global => 1,
  43. section => undef,
  44. value => 0,
  45. size => 0,
  46. };
  47. }
  48. foreach my $link_sym qw ( __prefix _prefix _prefix_load_offset
  49. _prefix_size _prefix_progbits_size _prefix_size_pgh
  50. __text16 _text16 _text16_load_offset
  51. _text16_size _text16_progbits_size _text16_size_pgh
  52. __data16 _data16 _data16_load_offset
  53. _data16_size _data16_progbits_size _data16_size_pgh
  54. __text _text __data _data _textdata_load_offset
  55. _textdata_size _textdata_progbits_size
  56. __rodata __bss _end
  57. _payload_offset _max_align
  58. _load_size _load_size_pgh _load_size_sect
  59. pci_vendor_id pci_device_id ) {
  60. $symtab->{LINKER}->{$link_sym} = {
  61. global => 1,
  62. section => '*ABS*',
  63. value => 0,
  64. size => 0,
  65. };
  66. }
  67. # Add symbols that we know will be used by the debug system
  68. #
  69. foreach my $debug_sym qw ( dbg_autocolourise dbg_decolourise
  70. dbg_hex_dump_da ) {
  71. $symtab->{DEBUG}->{$debug_sym} = {
  72. global => 1,
  73. section => undef,
  74. value => 0,
  75. size => 0,
  76. };
  77. }
  78. # Build up requires, provides and shares symbol tables for global
  79. # symbols
  80. #
  81. my $globals = {};
  82. while ( ( my $object, my $symbols ) = each %$symtab ) {
  83. while ( ( my $symbol, my $info ) = each %$symbols ) {
  84. if ( $info->{global} ) {
  85. my $category;
  86. if ( ! defined $info->{section} ) {
  87. $category = "requires";
  88. } elsif ( $info->{section} eq "*COM*" ) {
  89. $category = "shares";
  90. } else {
  91. $category = "provides";
  92. }
  93. $globals->{$symbol}->{$category}->{$object} = 1;
  94. }
  95. }
  96. }
  97. # Check for multiply defined, never-defined and unused global symbols
  98. #
  99. my $problems = {};
  100. while ( ( my $symbol, my $info ) = each %$globals ) {
  101. my @provides = keys %{$info->{provides}};
  102. my @requires = keys %{$info->{requires}};
  103. my @shares = keys %{$info->{shares}};
  104. if ( ( @provides == 0 ) && ( @shares == 1 ) ) {
  105. # A symbol "shared" by just a single file is actually being
  106. # provided by that file; it just doesn't have an initialiser.
  107. @provides = @shares;
  108. @shares = ();
  109. }
  110. if ( ( @requires > 0 ) && ( @provides == 0 ) && ( @shares == 0 ) ) {
  111. # No object provides this symbol, but some objects require it.
  112. $problems->{$_}->{nonexistent}->{$symbol} = 1 foreach @requires;
  113. }
  114. if ( ( @requires == 0 ) && ( @provides > 0 ) ) {
  115. # No object requires this symbol, but some objects provide it.
  116. foreach my $provide ( @provides ) {
  117. if ( $provide eq "LINKER" ) {
  118. # Linker-provided symbols are exempt from this check.
  119. } elsif ( $symtab->{$provide}->{$symbol}->{section} =~ /^\.tbl\./ ) {
  120. # Linker tables are exempt from this check.
  121. } else {
  122. $problems->{$provide}->{unused}->{$symbol} = 1;
  123. }
  124. }
  125. }
  126. if ( ( @shares > 0 ) && ( @provides > 0 ) ) {
  127. # A shared symbol is being initialised by an object
  128. $problems->{$_}->{shared}->{$symbol} = 1 foreach @provides;
  129. }
  130. if ( @provides > 1 ) {
  131. # A non-shared symbol is defined in multiple objects
  132. $problems->{$_}->{multiples}->{$symbol} = 1 foreach @provides;
  133. }
  134. }
  135. # Check for excessively large local symbols. Text and rodata symbols
  136. # are exempt from this check
  137. #
  138. while ( ( my $object, my $symbols ) = each %$symtab ) {
  139. while ( ( my $symbol, my $info ) = each %$symbols ) {
  140. if ( ( ! $info->{global} ) &&
  141. ( ( defined $info->{section} ) &&
  142. ! ( $info->{section} =~ /^(\.text|\.rodata)/ ) ) &&
  143. ( $info->{size} >= WARNING_SIZE ) ) {
  144. $problems->{$object}->{large}->{$symbol} = 1;
  145. }
  146. }
  147. }
  148. # Print out error messages
  149. #
  150. my $errors = 0;
  151. my $warnings = 0;
  152. foreach my $object ( sort keys %$problems ) {
  153. my @nonexistent = sort keys %{$problems->{$object}->{nonexistent}};
  154. my @multiples = sort keys %{$problems->{$object}->{multiples}};
  155. my @unused = sort keys %{$problems->{$object}->{unused}};
  156. my @shared = sort keys %{$problems->{$object}->{shared}};
  157. my @large = sort keys %{$problems->{$object}->{large}};
  158. print "WARN $object provides unused symbol $_\n" foreach @unused;
  159. $warnings += @unused;
  160. print "WARN $object has large static symbol $_\n" foreach @large;
  161. $warnings += @large;
  162. print "ERR $object requires non-existent symbol $_\n" foreach @nonexistent;
  163. $errors += @nonexistent;
  164. foreach my $symbol ( @multiples ) {
  165. my @other_objects = sort grep { $_ ne $object }
  166. keys %{$globals->{$symbol}->{provides}};
  167. print "ERR $object provides symbol $symbol"
  168. ." (also provided by @other_objects)\n";
  169. }
  170. $errors += @multiples;
  171. print "ERR $object misuses shared symbol $_\n" foreach @shared;
  172. }
  173. print "$errors error(s), $warnings warning(s)\n";
  174. exit ( $errors ? 1 : 0 );