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