|  | @@ -0,0 +1,73 @@
 | 
		
	
		
			
			|  | 1 | +#!/usr/bin/perl -w
 | 
		
	
		
			
			|  | 2 | +
 | 
		
	
		
			
			|  | 3 | +use strict;
 | 
		
	
		
			
			|  | 4 | +use warnings;
 | 
		
	
		
			
			|  | 5 | +
 | 
		
	
		
			
			|  | 6 | +my $symbols = {};
 | 
		
	
		
			
			|  | 7 | +
 | 
		
	
		
			
			|  | 8 | +# Scan output of "nm -o -g bin/blib.a" and build up symbol cross-ref table
 | 
		
	
		
			
			|  | 9 | +#
 | 
		
	
		
			
			|  | 10 | +while ( <> ) {
 | 
		
	
		
			
			|  | 11 | +  chomp;
 | 
		
	
		
			
			|  | 12 | +  ( my $object, my $type, my $symbol ) = /^.*?:(.*?\.o):.*?\s(\S)\s(.*)$/;
 | 
		
	
		
			
			|  | 13 | +  my $category = $type eq 'U' ? "requires" : "provides";
 | 
		
	
		
			
			|  | 14 | +  $symbols->{$symbol}->{$category}->{$object} = 1;
 | 
		
	
		
			
			|  | 15 | +}
 | 
		
	
		
			
			|  | 16 | +
 | 
		
	
		
			
			|  | 17 | +# Add symbols that we know will be generated or required by the linker
 | 
		
	
		
			
			|  | 18 | +#
 | 
		
	
		
			
			|  | 19 | +while ( ( my $symbol, my $info ) = each %$symbols ) {
 | 
		
	
		
			
			|  | 20 | +  $info->{requires}->{LINKER} = 1 if $symbol =~ /^obj_/;
 | 
		
	
		
			
			|  | 21 | +}
 | 
		
	
		
			
			|  | 22 | +$symbols->{$_}->{provides}->{LINKER} = 1
 | 
		
	
		
			
			|  | 23 | +    foreach qw ( _prefix _eprefix _decompress _edecompress _text
 | 
		
	
		
			
			|  | 24 | +		 _etext _data _edata _bss _ebss _end device_drivers
 | 
		
	
		
			
			|  | 25 | +		 device_drivers_end bus_drivers bus_drivers_end
 | 
		
	
		
			
			|  | 26 | +		 type_drivers type_drivers_end console_drivers
 | 
		
	
		
			
			|  | 27 | +		 console_drivers_end post_reloc_fns post_reloc_fns_end
 | 
		
	
		
			
			|  | 28 | +		 init_fns init_fns_end );
 | 
		
	
		
			
			|  | 29 | +
 | 
		
	
		
			
			|  | 30 | +# Check for multiply defined, never-defined and unused symbols
 | 
		
	
		
			
			|  | 31 | +#
 | 
		
	
		
			
			|  | 32 | +my $problems = {};
 | 
		
	
		
			
			|  | 33 | +while ( ( my $symbol, my $info ) = each %$symbols ) {
 | 
		
	
		
			
			|  | 34 | +  my @provides = keys %{$info->{provides}};
 | 
		
	
		
			
			|  | 35 | +  my @requires = keys %{$info->{requires}};
 | 
		
	
		
			
			|  | 36 | +
 | 
		
	
		
			
			|  | 37 | +  if ( @provides == 0 ) {
 | 
		
	
		
			
			|  | 38 | +    # No object provides this symbol
 | 
		
	
		
			
			|  | 39 | +    $problems->{$_}->{nonexistent}->{$symbol} = 1 foreach @requires;
 | 
		
	
		
			
			|  | 40 | +  } elsif ( @provides > 1 ) {
 | 
		
	
		
			
			|  | 41 | +    # Symbol defined in multiple objects
 | 
		
	
		
			
			|  | 42 | +    $problems->{$_}->{multiples}->{$symbol} = 1 foreach @requires;
 | 
		
	
		
			
			|  | 43 | +  }
 | 
		
	
		
			
			|  | 44 | +  if ( @requires == 0 ) {
 | 
		
	
		
			
			|  | 45 | +    # Symbol not required
 | 
		
	
		
			
			|  | 46 | +    $problems->{$_}->{unused}->{$symbol} = 1 foreach @provides;
 | 
		
	
		
			
			|  | 47 | +  }
 | 
		
	
		
			
			|  | 48 | +}
 | 
		
	
		
			
			|  | 49 | +
 | 
		
	
		
			
			|  | 50 | +# Print out error messages
 | 
		
	
		
			
			|  | 51 | +#
 | 
		
	
		
			
			|  | 52 | +my $errors = 0;
 | 
		
	
		
			
			|  | 53 | +my $warnings = 0;
 | 
		
	
		
			
			|  | 54 | +foreach my $object ( sort keys %$problems ) {
 | 
		
	
		
			
			|  | 55 | +  my @nonexistent = sort keys %{$problems->{$object}->{nonexistent}};
 | 
		
	
		
			
			|  | 56 | +  my @multiples = sort keys %{$problems->{$object}->{multiples}};
 | 
		
	
		
			
			|  | 57 | +  my @unused = sort keys %{$problems->{$object}->{unused}};
 | 
		
	
		
			
			|  | 58 | +
 | 
		
	
		
			
			|  | 59 | +  print "WARN $object provides unused symbol $_\n" foreach @unused;
 | 
		
	
		
			
			|  | 60 | +  $warnings += @unused;
 | 
		
	
		
			
			|  | 61 | +  print "ERR  $object requires non-existent symbol $_\n" foreach @nonexistent;
 | 
		
	
		
			
			|  | 62 | +  $errors += @nonexistent;
 | 
		
	
		
			
			|  | 63 | +  foreach my $symbol ( @multiples ) {
 | 
		
	
		
			
			|  | 64 | +    my @other_objects = sort grep { $_ ne $object }
 | 
		
	
		
			
			|  | 65 | +		        keys %{$symbols->{$symbol}->{provides}};
 | 
		
	
		
			
			|  | 66 | +    print "ERR  $object provides symbol $symbol"
 | 
		
	
		
			
			|  | 67 | +	." (also provided by @other_objects)\n";
 | 
		
	
		
			
			|  | 68 | +  }
 | 
		
	
		
			
			|  | 69 | +  $errors += @multiples;
 | 
		
	
		
			
			|  | 70 | +}
 | 
		
	
		
			
			|  | 71 | +
 | 
		
	
		
			
			|  | 72 | +print "$errors error(s), $warnings warning(s)\n";
 | 
		
	
		
			
			|  | 73 | +exit ( $errors ? 1 : 0 );
 |