<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -w

use strict;
use warnings;

use constant WARNING_SIZE =&gt; 512;

my $symtab = {};

# Scan output of "objdump -w -t bin/blib.a" and build up symbol table
#
my $object;
while ( &lt;&gt; ) {
  chomp;
  if ( /^In archive/ ) {
    # Do nothing
  } elsif ( /^$/ ) {
    # Do nothing
  } elsif ( /^(\S+\.o):\s+file format/ ) {
    $object = $1;
  } elsif ( /^SYMBOL TABLE:/ ) {
    # Do nothing
  } elsif ( /^([0-9a-fA-F]+)\s(l|g|\s)......\s(\S+)\s+([0-9a-fA-F]+)\s+(\S+)$/ ) {
    my $value = $1;
    my $scope = $2;
    my $section = $3;
    my $size = $4;
    my $symbol = $5;
    $symtab-&gt;{$object}-&gt;{$symbol} = {
      global	=&gt; ( $scope ne "l" ),
      section	=&gt; ( $section eq "*UND*" ? undef : $section ),
      value	=&gt; ( $value ? hex ( $value ) : 0 ),
      size	=&gt; ( $size ? hex ( $size ) : 0 ),
    };
  } else {
    die "Unrecognized line \"$_\"";
  }
}

# Add symbols that we know will be generated or required by the linker
#
foreach my $object ( keys %$symtab ) {
  my $obj_symbol = "obj_$object";
  $obj_symbol =~ s/\.o$//;
  $obj_symbol =~ s/\W/_/g;
  $symtab-&gt;{LINKER}-&gt;{$obj_symbol} = {
    global	=&gt; 1,
    section	=&gt; undef,
    value	=&gt; 0,
    size	=&gt; 0,
  };
}
foreach my $link_sym qw ( __prefix _prefix _prefix_load_offset
			  _prefix_size _prefix_progbits_size _prefix_size_pgh
			  __text16 _text16 _text16_load_offset
			  _text16_size _text16_progbits_size _text16_size_pgh
			  __data16 _data16 _data16_load_offset
			  _data16_size _data16_progbits_size _data16_size_pgh
			  __text _text __data _data _textdata_load_offset
			  _textdata_size _textdata_progbits_size
			  __rodata __bss _end
			  _payload_offset _max_align
			  _load_size _load_size_pgh _load_size_sect
			  pci_vendor_id pci_device_id ) {
  $symtab-&gt;{LINKER}-&gt;{$link_sym} = {
    global	=&gt; 1,
    section	=&gt; '*ABS*',
    value	=&gt; 0,
    size	=&gt; 0,
  };
}

# Add symbols that we know will be used by the debug system
#
foreach my $debug_sym qw ( dbg_autocolourise dbg_decolourise
			   dbg_hex_dump_da ) {
  $symtab-&gt;{DEBUG}-&gt;{$debug_sym} = {
    global	=&gt; 1,
    section	=&gt; undef,
    value	=&gt; 0,
    size	=&gt; 0,
  };
}

# Build up requires, provides and shares symbol tables for global
# symbols
#
my $globals = {};
while ( ( my $object, my $symbols ) = each %$symtab ) {
  while ( ( my $symbol, my $info ) = each %$symbols ) {
    if ( $info-&gt;{global} ) {
      my $category;
      if ( ! defined $info-&gt;{section} ) {
	$category = "requires";
      } elsif ( $info-&gt;{section} eq "*COM*" ) {
	$category = "shares";
      } else {
	$category = "provides";
      }
      $globals-&gt;{$symbol}-&gt;{$category}-&gt;{$object} = 1;
    }
  }
}

# Check for multiply defined, never-defined and unused global symbols
#
my $problems = {};
while ( ( my $symbol, my $info ) = each %$globals ) {
  my @provides = keys %{$info-&gt;{provides}};
  my @requires = keys %{$info-&gt;{requires}};
  my @shares = keys %{$info-&gt;{shares}};

  if ( ( @provides == 0 ) &amp;&amp; ( @shares == 1 ) ) {
    # A symbol "shared" by just a single file is actually being
    # provided by that file; it just doesn't have an initialiser.
    @provides = @shares;
    @shares = ();
  }

  if ( ( @requires &gt; 0 ) &amp;&amp; ( @provides == 0 ) &amp;&amp; ( @shares == 0 ) ) {
    # No object provides this symbol, but some objects require it.
    $problems-&gt;{$_}-&gt;{nonexistent}-&gt;{$symbol} = 1 foreach @requires;
  }

  if ( ( @requires == 0 ) &amp;&amp; ( @provides &gt; 0 ) ) {
    # No object requires this symbol, but some objects provide it.
    foreach my $provide ( @provides ) {
      if ( $provide eq "LINKER" ) {
	# Linker-provided symbols are exempt from this check.
      } elsif ( $symtab-&gt;{$provide}-&gt;{$symbol}-&gt;{section} =~ /^\.tbl\./ ) {
	# Linker tables are exempt from this check.
      } else {
	$problems-&gt;{$provide}-&gt;{unused}-&gt;{$symbol} = 1;
      }
    }
  }

  if ( ( @shares &gt; 0 ) &amp;&amp; ( @provides &gt; 0 ) ) {
    # A shared symbol is being initialised by an object
    $problems-&gt;{$_}-&gt;{shared}-&gt;{$symbol} = 1 foreach @provides;
  }

  if ( @provides &gt; 1 ) {
    # A non-shared symbol is defined in multiple objects
    $problems-&gt;{$_}-&gt;{multiples}-&gt;{$symbol} = 1 foreach @provides;
  }
}

# Check for excessively large local symbols.  Text and rodata symbols
# are exempt from this check
#
while ( ( my $object, my $symbols ) = each %$symtab ) {
  while ( ( my $symbol, my $info ) = each %$symbols ) {
    if ( ( ! $info-&gt;{global} ) &amp;&amp;
	 ( ( defined $info-&gt;{section} ) &amp;&amp;
	   ! ( $info-&gt;{section} =~ /^(\.text|\.rodata)/ ) ) &amp;&amp;
	 ( $info-&gt;{size} &gt;= WARNING_SIZE ) ) {
      $problems-&gt;{$object}-&gt;{large}-&gt;{$symbol} = 1;
    }
  }
}

# Print out error messages
#
my $errors = 0;
my $warnings = 0;
foreach my $object ( sort keys %$problems ) {
  my @nonexistent = sort keys %{$problems-&gt;{$object}-&gt;{nonexistent}};
  my @multiples = sort keys %{$problems-&gt;{$object}-&gt;{multiples}};
  my @unused = sort keys %{$problems-&gt;{$object}-&gt;{unused}};
  my @shared = sort keys %{$problems-&gt;{$object}-&gt;{shared}};
  my @large = sort keys %{$problems-&gt;{$object}-&gt;{large}};

  print "WARN $object provides unused symbol $_\n" foreach @unused;
  $warnings += @unused;
  print "WARN $object has large static symbol $_\n" foreach @large;
  $warnings += @large;
  print "ERR  $object requires non-existent symbol $_\n" foreach @nonexistent;
  $errors += @nonexistent;
  foreach my $symbol ( @multiples ) {
    my @other_objects = sort grep { $_ ne $object }
		        keys %{$globals-&gt;{$symbol}-&gt;{provides}};
    print "ERR  $object provides symbol $symbol"
	." (also provided by @other_objects)\n";
  }
  $errors += @multiples;
  print "ERR  $object misuses shared symbol $_\n" foreach @shared;
}

print "$errors error(s), $warnings warning(s)\n";
exit ( $errors ? 1 : 0 );
</pre></body></html>