unrefglobals.pl 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/perl
  2. # Copyright (c) 2003, Pavel Roskin
  3. # The Midnight Commander is free software: you can redistribute it
  4. # and/or modify it under the terms of the GNU General Public License as
  5. # published by the Free Software Foundation, either version 3 of the License,
  6. # or (at your option) any later version.
  7. #
  8. # The Midnight Commander is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. # Run this script on the map with cross-reference generated by GNU ld,
  16. # and it will generate a list of symbols that don't need to be global.
  17. # To create the map, run something like this:
  18. # make LDFLAGS=-Wl,-Map,output.map,--cref
  19. use strict;
  20. my %symbols;
  21. my %syms;
  22. my %objs;
  23. if ($#ARGV != 0) {
  24. print "Usage: unrefglobals.pl mapfile\n";
  25. exit 1;
  26. }
  27. if (!open (MAP, "$ARGV[0]")) {
  28. print "Cannot open file \"$ARGV[0]\"\n";
  29. exit 1;
  30. }
  31. my $line;
  32. my $next_line = <MAP>;
  33. while (1) {
  34. last unless $next_line;
  35. $line = $next_line;
  36. $next_line = <MAP>;
  37. next unless ($line =~ m{^[A-Za-z_][A-Za-z0-9_]* +[^ /][^ ]+\.o$} or
  38. $line =~ m{^[A-Za-z_][A-Za-z0-9_]* +[^ /][^ ]+\.a\([^ ]+\.o\)$});
  39. if (!$next_line or ($next_line !~ /^ /)) {
  40. my @arr = split (' ', $line);
  41. $symbols{$arr[0]} = $arr[1];
  42. $syms{$arr[0]} = 1;
  43. $objs{$arr[1]} = 1;
  44. }
  45. }
  46. close(MAP);
  47. foreach my $obj (sort keys %objs) {
  48. print "$obj\n";
  49. foreach my $sym (sort keys %syms) {
  50. print "\t$sym\n" if ($symbols{$sym} eq $obj);
  51. }
  52. }