unrefglobals.pl 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/perl
  2. # Copyright (c) 2003, Pavel Roskin
  3. # This script is Free Software, and it can be copied, distributed and
  4. # modified under the terms of GNU General Public License, version 2.
  5. # Run this script on the map with cross-reference generated by GNU ld,
  6. # and it will generate a list of symbols that don't need to be global.
  7. # To create the map, run something like this:
  8. # make LDFLAGS=-Wl,-Map,output.map,--cref
  9. use strict;
  10. my %symbols;
  11. my %syms;
  12. my %objs;
  13. if ($#ARGV != 0) {
  14. print "Usage: unrefglobals.pl mapfile\n";
  15. exit 1;
  16. }
  17. if (!open (MAP, "$ARGV[0]")) {
  18. print "Cannot open file \"$ARGV[0]\"\n";
  19. exit 1;
  20. }
  21. my $line;
  22. my $next_line = <MAP>;
  23. while (1) {
  24. last unless $next_line;
  25. $line = $next_line;
  26. $next_line = <MAP>;
  27. next unless ($line =~ m{^[A-Za-z_][A-Za-z0-9_]* +[^ /][^ ]+\.o$} or
  28. $line =~ m{^[A-Za-z_][A-Za-z0-9_]* +[^ /][^ ]+\.a\([^ ]+\.o\)$});
  29. if (!$next_line or ($next_line !~ /^ /)) {
  30. my @arr = split (' ', $line);
  31. $symbols{$arr[0]} = $arr[1];
  32. $syms{$arr[0]} = 1;
  33. $objs{$arr[1]} = 1;
  34. }
  35. }
  36. close(MAP);
  37. foreach my $obj (sort keys %objs) {
  38. print "$obj\n";
  39. foreach my $sym (sort keys %syms) {
  40. print "\t$sym\n" if ($symbols{$sym} eq $obj);
  41. }
  42. }