OSDN Git Service

Add Patrick Marlier to ChangeLog entry.
[pf3gnuchains/gcc-fork.git] / contrib / make_sunver.pl
1 #!/usr/bin/perl -w
2
3 # make_sunver.pl
4 #
5 # This script takes at least two arguments, a GNU style version script and
6 # a list of object and archive files, and generates a corresponding Sun
7 # style version script as follows:
8 #
9 # Each glob pattern, C++ mangled pattern or literal in the input script is
10 # matched against all global symbols in the input objects, emitting those
11 # that matched (or nothing if no match was found).
12 # A comment with the original pattern and its type is left in the output
13 # file to make it easy to understand the matches.
14 #
15 # It uses elfdump when present (native), GNU readelf otherwise.
16 # It depends on the GNU version of c++filt, since it must understand the
17 # GNU mangling style.
18
19 use FileHandle;
20 use IPC::Open2;
21
22 # Input version script, GNU style.
23 my $symvers = shift;
24
25 ##########
26 # Get all the symbols from the library, match them, and add them to a hash.
27
28 my %sym_hash = ();
29
30 # List of objects and archives to process.
31 my @OBJECTS = ();
32
33 # List of shared objects to omit from processing.
34 my @SHAREDOBJS = ();
35
36 # Filter out those input archives that have corresponding shared objects to
37 # avoid adding all symbols matched in the archive to the output map.
38 foreach $file (@ARGV) {
39     if (($so = $file) =~ s/\.a$/.so/ && -e $so) {
40         printf STDERR "omitted $file -> $so\n";
41         push (@SHAREDOBJS, $so);
42     } else {
43         push (@OBJECTS, $file);
44     }
45 }
46
47 # We need to detect and ignore hidden symbols.  Solaris nm can only detect
48 # this in the harder to parse default output format, and GNU nm not at all,
49 # so use elfdump -s in the native case and GNU readelf -s otherwise.
50 # GNU objdump -t cannot be used since it produces a variable number of
51 # columns.
52
53 # The path to elfdump.
54 my $elfdump = "/usr/ccs/bin/elfdump";
55
56 if (-f $elfdump) {
57     open ELFDUMP,$elfdump.' -s '.(join ' ',@OBJECTS).'|' or die $!;
58     my $skip_arsym = 0;
59
60     while (<ELFDUMP>) {
61         chomp;
62
63         # Ignore empty lines.
64         if (/^$/) {
65             # End of archive symbol table, stop skipping.
66             $skip_arsym = 0 if $skip_arsym;
67             next;
68         }
69
70         # Keep skipping until end of archive symbol table.
71         next if ($skip_arsym);
72
73         # Ignore object name header for individual objects and archives.
74         next if (/:$/);
75
76         # Ignore table header lines.
77         next if (/^Symbol Table Section:/);
78         next if (/index.*value.*size/);
79
80         # Start of archive symbol table: start skipping.
81         if (/^Symbol Table: \(archive/) {
82             $skip_arsym = 1;
83             next;
84         }
85
86         # Split table.
87         (undef, undef, undef, undef, $bind, $oth, undef, $shndx, $name) = split;
88
89         # Error out for unknown input.
90         die "unknown input line:\n$_" unless defined($bind);
91
92         # Ignore local symbols.
93         next if ($bind eq "LOCL");
94         # Ignore hidden symbols.
95         next if ($oth eq "H");
96         # Ignore undefined symbols.
97         next if ($shndx eq "UNDEF");
98         # Error out for unhandled cases.
99         if ($bind !~ /^(GLOB|WEAK)/ or $oth ne "D") {
100             die "unhandled symbol:\n$_";
101         }
102
103         # Remember symbol.
104         $sym_hash{$name}++;
105     }
106     close ELFDUMP or die "$elfdump error";
107 } else {
108     open READELF, 'readelf -s -W '.(join ' ',@OBJECTS).'|' or die $!;
109     # Process each symbol.
110     while (<READELF>) {
111         chomp;
112
113         # Ignore empty lines.
114         next if (/^$/);
115
116         # Ignore object name header.
117         next if (/^File: .*$/);
118
119         # Ignore table header lines.
120         next if (/^Symbol table.*contains.*:/);
121         next if (/Num:.*Value.*Size/);
122
123         # Split table.
124         (undef, undef, undef, undef, $bind, $vis, $ndx, $name) = split;
125
126         # Error out for unknown input.
127         die "unknown input line:\n$_" unless defined($bind);
128
129         # Ignore local symbols.
130         next if ($bind eq "LOCAL");
131         # Ignore hidden symbols.
132         next if ($vis eq "HIDDEN");
133         # Ignore undefined symbols.
134         next if ($ndx eq "UND");
135         # Error out for unhandled cases.
136         if ($bind !~ /^(GLOBAL|WEAK)/ or $vis ne "DEFAULT") {
137             die "unhandled symbol:\n$_";
138         }
139
140         # Remember symbol.
141         $sym_hash{$name}++;
142     }
143     close READELF or die "readelf error";
144 }
145
146 ##########
147 # The various types of glob patterns.
148 #
149 # A glob pattern that is to be applied to the demangled name: 'cxx'.
150 # A glob patterns that applies directly to the name in the .o files: 'glob'.
151 # This pattern is ignored; used for local variables (usually just '*'): 'ign'.
152
153 # The type of the current pattern.
154 my $glob = 'glob';
155
156 # We're currently inside `extern "C++"', which Sun ld doesn't understand.
157 my $in_extern = 0;
158
159 # We're currently inside a conditional section: just skip it.
160 my $in_ifdef = 0;
161
162 # The c++filt command to use.  This *must* be GNU c++filt; the Sun Studio
163 # c++filt doesn't handle the GNU mangling style.
164 my $cxxfilt = $ENV{'CXXFILT'} || "c++filt";
165
166 # The current version name.
167 my $current_version = "";
168
169 # Was there any attempt to match a symbol to this version?
170 my $matches_attempted;
171
172 # The number of versions which matched this symbol.
173 my $matched_symbols;
174
175 open F,$symvers or die $!;
176
177 # Print information about generating this file
178 print "# This file was generated by make_sunver.pl.  DO NOT EDIT!\n";
179 print "# It was generated by:\n";
180 printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
181 printf "# Omitted archives with corresponding shared libraries: %s\n",
182     (join ' ', @SHAREDOBJS) if $#SHAREDOBJS >= 0;
183 print "#\n\n";
184
185 while (<F>) {
186     # End of skipped section.
187     if (/^[ \t]*\#endif/) {
188         $in_ifdef = 0;
189         next;
190     }
191
192     # Just skip a conditional section.
193     if ($in_ifdef) { next; }
194
195     # Lines of the form '};'
196     if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) {
197         $glob = 'glob';
198         if ($in_extern) {
199             $in_extern--;
200             print "$1##$2";
201         } else {
202             print;
203         }
204         next;
205     }
206
207     # Lines of the form '} SOME_VERSION_NAME_1.0;'
208     if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) {
209         $glob = 'glob';
210         # We tried to match symbols agains this version, but none matched.
211         # Emit dummy hidden symbol to avoid marking this version WEAK.
212         if ($matches_attempted && $matched_symbols == 0) {
213             print "  hidden:\n";
214             print "    .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n";
215         }
216         print; next;
217     }
218
219     # Special comments that look like C preprocessor conditionals.
220     # Just skip the contents for now.
221     # FIXME: Allow passing in conditionals from the command line to really
222     # control the skipping.
223     if (/^[ \t]*\#ifdef/) {
224         $in_ifdef = 1;
225         next;
226     }
227
228     # Comment and blank lines
229     if (/^[ \t]*\#/) { print; next; }
230     if (/^[ \t]*$/) { print; next; }
231
232     # Lines of the form '{'
233     if (/^([ \t]*){$/) {
234         if ($in_extern) {
235             print "$1##{\n";
236         } else {
237             print;
238         }
239         next;
240     }
241
242     # Lines of the form 'SOME_VERSION_NAME_1.1 {'
243     if (/^([A-Z0-9_.]+)[ \t]+{$/) {
244         # Record version name.
245         $current_version = $1;
246         # Reset match attempts, #matched symbols for this version.
247         $matches_attempted = 0;
248         $matched_symbols = 0;
249         print;
250         next;
251     }
252
253     # Ignore 'global:'
254     if (/^[ \t]*global:$/) { print; next; }
255
256     # After 'local:', globs should be ignored, they won't be exported.
257     if (/^[ \t]*local:$/) {
258         $glob = 'ign';
259         print;
260         next;
261     }
262
263     # After 'extern "C++"', globs are C++ patterns
264     if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) {
265         $in_extern++;
266         $glob = 'cxx';
267         # Need to comment, Sun ld cannot handle this.
268         print "$1##$2\n"; next;
269     }
270
271     # Chomp newline now we're done with passing through the input file.
272     chomp;
273
274     # Catch globs.  Note that '{}' is not allowed in globs by this script,
275     # so only '*' and '[]' are available.
276     if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) {
277         my $ws = $1;
278         my $ptn = $2;
279         # Turn the glob into a regex by replacing '*' with '.*', '?' with '.'.
280         # Keep $ptn so we can still print the original form.
281         ($pattern = $ptn) =~ s/\*/\.\*/g;
282         $pattern =~ s/\?/\./g;
283
284         if ($glob eq 'ign') {
285             # We're in a local: * section; just continue.
286             print "$_\n";
287             next;
288         }
289
290         # Print the glob commented for human readers.
291         print "$ws##$ptn ($glob)\n";
292         # We tried to match a symbol to this version.
293         $matches_attempted++;
294
295         if ($glob eq 'glob') {
296             my %ptn_syms = ();
297
298             # Match ptn against symbols in %sym_hash.
299             foreach my $sym (keys %sym_hash) {
300                 # Maybe it matches one of the patterns based on the symbol in
301                 # the .o file.
302                 $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
303             }
304
305             foreach my $sym (sort keys(%ptn_syms)) {
306                 $matched_symbols++;
307                 print "$ws$sym;\n";
308             }
309         } elsif ($glob eq 'cxx') {
310             my %dem_syms = ();
311
312             # Verify that we're actually using GNU c++filt.  Other versions
313             # most likely cannot handle GNU style symbol mangling.
314             my $cxxout = `$cxxfilt --version 2>&1`;
315             $cxxout =~ m/GNU/ or die "$0 requires GNU c++filt to function";
316
317             # Talk to c++filt through a pair of file descriptors.
318             # Need to start a fresh instance per pattern, otherwise the
319             # process grows to 500+ MB.
320             my $pid = open2(*FILTIN, *FILTOUT, $cxxfilt) or die $!;
321
322             # Match ptn against symbols in %sym_hash.
323             foreach my $sym (keys %sym_hash) {
324                 # No?  Well, maybe its demangled form matches one of those
325                 # patterns.
326                 printf FILTOUT "%s\n",$sym;
327                 my $dem = <FILTIN>;
328                 chomp $dem;
329                 $dem_syms{$sym}++ if ($dem =~ /^$pattern$/);
330             }
331
332             close FILTOUT or die "c++filt error";
333             close FILTIN or die "c++filt error";
334             # Need to wait for the c++filt process to avoid lots of zombies.
335             waitpid $pid, 0;
336
337             foreach my $sym (sort keys(%dem_syms)) {
338                 $matched_symbols++;
339                 print "$ws$sym;\n";
340             }
341         } else {
342             # No?  Well, then ignore it.
343         }
344         next;
345     }
346     # Important sanity check.  This script can't handle lots of formats
347     # that GNU ld can, so be sure to error out if one is seen!
348     die "strange line `$_'";
349 }
350 close F;