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