make_exports.pl 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #!/usr/bin/perl -w
  2. # This script takes two arguments, a version script and a dynamic library
  3. # (in that order), and prints a list of symbols to be exported from the
  4. # library.
  5. # It expects a 'nm' with the POSIX '-P' option, but everyone has one of
  6. # those, right? It also expects that symbol names have a leading underscore,
  7. # which is somewhat less likely.
  8. use File::Glob ':glob';
  9. use FileHandle;
  10. use IPC::Open2;
  11. # The glob patterns that are to be applied to the demangled name
  12. my @cxx_globs = ();
  13. # The glob patterns that apply directly to the name in the .o files
  14. my @globs = ();
  15. # The patterns for local variables (usually just '*').
  16. my @ignored = ();
  17. ##########
  18. # Fill in the various glob arrays.
  19. # The next pattern will go into this array.
  20. my $glob = \@globs;
  21. my $symvers = shift;
  22. open F,$symvers or die $!;
  23. while (<F>) {
  24. chomp;
  25. # Lines of the form '} SOME_VERSION_NAME_1.0;'
  26. if (/^[ \t]*\}[ \tA-Z0-9_.a-z]*;[ \t]*$/) {
  27. $glob = \@globs;
  28. next;
  29. }
  30. # Comment and blank lines
  31. next if (/^[ \t]*\#/);
  32. next if (/^[ \t]*$/);
  33. # Lines of the form 'SOME_VERSION_NAME_1.1 {'
  34. next if (/^[A-Z0-9_. \t]*{$/);
  35. # Ignore 'global:'
  36. next if (/^[ \t]*global:$/);
  37. # After 'local:', globs should be ignored, they won't be exported.
  38. if (/^[ \t]*local:$/) {
  39. $glob = \@ignored;
  40. next;
  41. }
  42. # After 'extern "C++"', globs are C++ patterns
  43. if (/^[ \t]*extern \"C\+\+\"[ \t]*$/) {
  44. $glob = \@cxx_globs;
  45. next;
  46. }
  47. # Catch globs. Note that '{}' is not allowed in globs by this script,
  48. # so only '*' and '?' and '[]' are available.
  49. if (/^[ \t]*([^ \t;{}#]+);?[ \t]*$/) {
  50. my $ptn = $1;
  51. # Turn the glob into a regex by replacing '*' with '.*'.
  52. $ptn =~ s/\*/.*/g;
  53. # And replacing '?' with '.'.
  54. $ptn =~ s/\?/./g;
  55. push @$glob,$ptn;
  56. next;
  57. }
  58. # Important sanity check. This script can't handle lots of formats
  59. # that GNU ld can, so be sure to error out if one is seen!
  60. die "strange line `$_'";
  61. }
  62. close F;
  63. # Make 'if (1)' for debugging.
  64. if (0) {
  65. print "cxx:\n";
  66. (printf "%s\n",$_) foreach (@cxx_globs);
  67. print "globs:\n";
  68. (printf "%s\n", $_) foreach (@globs);
  69. print "ignored:\n";
  70. (printf "%s\n", $_) foreach (@ignored);
  71. }
  72. ##########
  73. # Combine the arrays into single regular expressions
  74. # This cuts the time required from about 30 seconds to about 0.5 seconds.
  75. my $glob_regex = '^_(' . (join '|',@globs) . ')$';
  76. my $cxx_regex = (join '|',@cxx_globs);
  77. ##########
  78. # Get all the symbols from the library, match them, and add them to a hash.
  79. my %export_hash = ();
  80. my $nm = $ENV{'NM_FOR_TARGET'} || "nm";
  81. # Process each symbol.
  82. print STDERR $nm.' -P '.(join ' ',@ARGV).'|';
  83. open NM,$nm.' -P '.(join ' ',@ARGV).'|' or die $!;
  84. # Talk to c++filt through a pair of file descriptors.
  85. open2(*FILTIN, *FILTOUT, "c++filt -_") or die $!;
  86. NAME: while (<NM>) {
  87. my $i;
  88. chomp;
  89. # nm prints out stuff at the start, ignore it.
  90. next if (/^$/);
  91. next if (/:$/);
  92. # Ignore undefined and local symbols.
  93. next if (/^([^ ]+) [Ua-z] /);
  94. # GCC does not export construction vtables from shared libraries.
  95. # However the symbols are marked hidden, for Darwin that makes them
  96. # also external "private_extern", which means that they show up in
  97. # this list. When ld64 encounters them it generates a warning that
  98. # they cannot be exported, so trim them from the set now.
  99. next if (/^construction vtable.*$/);
  100. next if (/^__ZTC.*$/);
  101. # $sym is the name of the symbol, $noeh_sym is the same thing with
  102. # any '.eh' suffix removed.
  103. die "unknown nm output $_" if (! /^([^ ]+) [A-Z] /);
  104. my $sym = $1;
  105. my $noeh_sym = $sym;
  106. $noeh_sym =~ s/\.eh$//;
  107. # Maybe it matches one of the patterns based on the symbol in the .o file.
  108. if ($noeh_sym =~ /$glob_regex/) {
  109. $export_hash{$sym} = 1;
  110. next NAME;
  111. }
  112. # No? Well, maybe its demangled form matches one of those patterns.
  113. printf FILTOUT "%s\n",$noeh_sym;
  114. my $dem = <FILTIN>;
  115. chomp $dem;
  116. if ($dem =~ /$cxx_regex/) {
  117. $export_hash{$sym} = 2;
  118. next NAME;
  119. }
  120. # No? Well, then ignore it.
  121. }
  122. close NM or die "nm error";
  123. close FILTOUT or die "c++filt error";
  124. close FILTIN or die "c++filt error";
  125. ##########
  126. # Print out the export file
  127. # Print information about generating this file
  128. print "# This is a generated file.\n";
  129. print "# It was generated by:\n";
  130. printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
  131. foreach my $i (keys %export_hash) {
  132. printf "%s\n",$i or die;
  133. }