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
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.
10 use File
::Glob
':glob';
14 # The glob patterns that are to be applied to the demangled name
16 # The glob patterns that apply directly to the name in the .o files
18 # The patterns for local variables (usually just '*').
22 # Fill in the various glob arrays.
24 # The next pattern will go into this array.
28 open F
,$symvers or die $!;
32 # Lines of the form '} SOME_VERSION_NAME_1.0;'
33 if (/^[ \t]*\}[ \tA-Z0-9_.a-z]*;[ \t]*$/) {
37 # Comment and blank lines
38 next if (/^[ \t]*\#/);
40 # Lines of the form 'SOME_VERSION_NAME_1.1 {'
41 next if (/^[A-Z0-9_. \t]*{$/);
43 next if (/^[ \t]*global:$/);
44 # After 'local:', globs should be ignored, they won't be exported.
45 if (/^[ \t]*local:$/) {
49 # After 'extern "C++"', globs are C++ patterns
50 if (/^[ \t]*extern \"C\+\+\"[ \t]*$/) {
54 # Catch globs. Note that '{}' is not allowed in globs by this script,
55 # so only '*' and '[]' are available.
56 if (/^[ \t]*([^ \t;{}#]+);?[ \t]*$/) {
58 # Turn the glob into a regex by replacing '*' with '.*'.
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 `$_'";
69 # Make 'if (1)' for debugging.
72 (printf "%s\n",$_) foreach (@cxx_globs);
74 (printf "%s\n", $_) foreach (@globs);
76 (printf "%s\n", $_) foreach (@ignored);
80 # Combine the arrays into single regular expressions
81 # This cuts the time required from about 30 seconds to about 0.5 seconds.
83 my $glob_regex = '^_(' . (join '|',@globs) . ')$';
84 my $cxx_regex = (join '|',@cxx_globs);
87 # Get all the symbols from the library, match them, and add them to a hash.
90 my $nm = $ENV{'NM_FOR_TARGET'} || "nm";
91 # Process each symbol.
92 print STDERR
$nm.' -P '.(join ' ',@ARGV).'|';
93 open NM
,$nm.' -P '.(join ' ',@ARGV).'|' or die $!;
94 # Talk to c++filt through a pair of file descriptors.
95 open2
(*FILTIN
, *FILTOUT
, "c++filt -_") or die $!;
100 # nm prints out stuff at the start, ignore it.
103 # Ignore undefined and local symbols.
104 next if (/^([^ ]+) [Ua-z] /);
106 # $sym is the name of the symbol, $noeh_sym is the same thing with
107 # any '.eh' suffix removed.
108 die "unknown nm output $_" if (! /^([^ ]+) [A-Z] /);
111 $noeh_sym =~ s/\.eh$//;
113 # Maybe it matches one of the patterns based on the symbol in the .o file.
114 if ($noeh_sym =~ /$glob_regex/) {
115 $export_hash{$sym} = 1;
119 # No? Well, maybe its demangled form matches one of those patterns.
120 printf FILTOUT
"%s\n",$noeh_sym;
123 if ($dem =~ /$cxx_regex/) {
124 $export_hash{$sym} = 2;
128 # No? Well, then ignore it.
130 close NM
or die "nm error";
131 close FILTOUT
or die "c++filt error";
132 close FILTIN
or die "c++filt error";
135 # Print out the export file
137 # Print information about generating this file
138 print "# This is a generated file.\n";
139 print "# It was generated by:\n";
140 printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
142 foreach my $i (keys %export_hash) {
143 printf "%s\n",$i or die;