make_exports.pl revision 169691
1226031Sstas#!/usr/bin/perl -w
2226031Sstas
3226031Sstas# This script takes two arguments, a version script and a dynamic library
4226031Sstas# (in that order), and prints a list of symbols to be exported from the
5226031Sstas# library.
6226031Sstas# It expects a 'nm' with the POSIX '-P' option, but everyone has one of
7226031Sstas# those, right?  It also expects that symbol names have a leading underscore,
8226031Sstas# which is somewhat less likely.
9226031Sstas
10226031Sstasuse File::Glob ':glob';
11226031Sstasuse FileHandle;
12226031Sstasuse IPC::Open2;
13226031Sstas
14226031Sstas# The glob patterns that are to be applied to the demangled name
15226031Sstasmy @cxx_globs = ();
16226031Sstas# The glob patterns that apply directly to the name in the .o files
17226031Sstasmy @globs = ();
18226031Sstas# The patterns for local variables (usually just '*').
19226031Sstasmy @ignored = ();
20226031Sstas
21226031Sstas##########
22226031Sstas# Fill in the various glob arrays.
23226031Sstas
24226031Sstas# The next pattern will go into this array.
25226031Sstasmy $glob = \@globs;
26226031Sstasmy $symvers = shift;
27226031Sstas
28226031Sstasopen F,$symvers or die $!;
29226031Sstas
30226031Sstaswhile (<F>) {
31226031Sstas    chomp;
32226031Sstas    # Lines of the form '} SOME_VERSION_NAME_1.0;'
33226031Sstas    if (/^[ \t]*\}[ \tA-Z0-9_.a-z]*;[ \t]*$/) {
34226031Sstas      $glob = \@globs;
35226031Sstas      next;
36226031Sstas    }
37226031Sstas    # Comment and blank lines
38226031Sstas    next if (/^[ \t]*\#/);
39226031Sstas    next if (/^[ \t]*$/);
40226031Sstas    # Lines of the form 'SOME_VERSION_NAME_1.1 {'
41226031Sstas    next if (/^[A-Z0-9_. \t]*{$/);
42226031Sstas    # Ignore 'global:'
43226031Sstas    next if (/^[ \t]*global:$/);
44226031Sstas    # After 'local:', globs should be ignored, they won't be exported.
45226031Sstas    if (/^[ \t]*local:$/) {
46226031Sstas	$glob = \@ignored;
47226031Sstas	next;
48226031Sstas    }
49226031Sstas    # After 'extern "C++"', globs are C++ patterns
50226031Sstas    if (/^[ \t]*extern \"C\+\+\"[ \t]*$/) {
51226031Sstas	$glob = \@cxx_globs;
52226031Sstas	next;
53226031Sstas    }
54226031Sstas    # Catch globs.  Note that '{}' is not allowed in globs by this script,
55226031Sstas    # so only '*' and '[]' are available.
56226031Sstas    if (/^[ \t]*([^ \t;{}#]+);?[ \t]*$/) {
57226031Sstas	my $ptn = $1;
58226031Sstas	# Turn the glob into a regex by replacing '*' with '.*'.
59226031Sstas	$ptn =~ s/\*/\.\*/g;
60226031Sstas	push @$glob,$ptn;
61226031Sstas	next;
62226031Sstas    }
63226031Sstas    # Important sanity check.  This script can't handle lots of formats
64226031Sstas    # that GNU ld can, so be sure to error out if one is seen!
65226031Sstas    die "strange line `$_'";
66226031Sstas}
67226031Sstasclose F;
68226031Sstas
69226031Sstas# Make 'if (1)' for debugging.
70226031Sstasif (0) {
71226031Sstas    print "cxx:\n";
72226031Sstas    (printf "%s\n",$_) foreach (@cxx_globs);
73226031Sstas    print "globs:\n";
74226031Sstas    (printf "%s\n", $_) foreach (@globs);
75226031Sstas    print "ignored:\n";
76226031Sstas    (printf "%s\n", $_) foreach (@ignored);
77226031Sstas}
78226031Sstas
79226031Sstas##########
80226031Sstas# Combine the arrays into single regular expressions
81226031Sstas# This cuts the time required from about 30 seconds to about 0.5 seconds.
82226031Sstas
83226031Sstasmy $glob_regex = '^_(' . (join '|',@globs) . ')$';
84226031Sstasmy $cxx_regex = (join '|',@cxx_globs);
85226031Sstas
86226031Sstas##########
87226031Sstas# Get all the symbols from the library, match them, and add them to a hash.
88226031Sstas
89226031Sstasmy %export_hash = ();
90226031Sstasmy $nm = $ENV{'NM_FOR_TARGET'} || "nm";
91226031Sstas# Process each symbol.
92226031Sstasprint STDERR $nm.' -P '.(join ' ',@ARGV).'|';
93226031Sstasopen NM,$nm.' -P '.(join ' ',@ARGV).'|' or die $!;
94226031Sstas# Talk to c++filt through a pair of file descriptors.
95226031Sstasopen2(*FILTIN, *FILTOUT, "c++filt -_") or die $!;
96226031SstasNAME: while (<NM>) {
97226031Sstas    my $i;
98226031Sstas    chomp;
99226031Sstas
100226031Sstas    # nm prints out stuff at the start, ignore it.
101226031Sstas    next if (/^$/);
102226031Sstas    next if (/:$/);
103226031Sstas    # Ignore undefined and local symbols.
104226031Sstas    next if (/^([^ ]+) [Ua-z] /);
105226031Sstas
106226031Sstas    # $sym is the name of the symbol, $noeh_sym is the same thing with
107226031Sstas    # any '.eh' suffix removed.
108226031Sstas    die "unknown nm output $_" if (! /^([^ ]+) [A-Z] /);
109226031Sstas    my $sym = $1;
110226031Sstas    my $noeh_sym = $sym;
111226031Sstas    $noeh_sym =~ s/\.eh$//;
112226031Sstas
113226031Sstas    # Maybe it matches one of the patterns based on the symbol in the .o file.
114226031Sstas    if ($noeh_sym =~ /$glob_regex/) {
115        $export_hash{$sym} = 1;
116	next NAME;
117    }
118
119    # No?  Well, maybe its demangled form matches one of those patterns.
120    printf FILTOUT "%s\n",$noeh_sym;
121    my $dem = <FILTIN>;
122    chomp $dem;
123    if ($dem =~ /$cxx_regex/) {
124        $export_hash{$sym} = 2;
125	next NAME;
126    }
127
128    # No?  Well, then ignore it.
129}
130close NM or die "nm error";
131close FILTOUT or die "c++filt error";
132close FILTIN or die "c++filt error";
133
134##########
135# Print out the export file
136
137# Print information about generating this file
138print "# This is a generated file.\n";
139print "# It was generated by:\n";
140printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
141
142foreach my $i (keys %export_hash) {
143  printf "%s\n",$i or die;
144}
145