Revert wrong checkin
[official-gcc.git] / contrib / make_sunver.pl
blob292837402dbe3b93ac26597bb63830eebbdf3a9b
1 #!/usr/bin/perl -w
3 # make_sunver.pl
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:
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.
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.
19 use FileHandle;
20 use IPC::Open2;
22 # Input version script, GNU style.
23 my $symvers = shift;
25 ##########
26 # Get all the symbols from the library, match them, and add them to a hash.
28 my %sym_hash = ();
30 # List of objects and archives to process.
31 my @OBJECTS = ();
33 # List of shared objects to omit from processing.
34 my @SHAREDOBJS = ();
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);
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.
53 # The path to elfdump.
54 my $elfdump = "/usr/ccs/bin/elfdump";
56 if (-f $elfdump) {
57 open ELFDUMP,$elfdump.' -s '.(join ' ',@OBJECTS).'|' or die $!;
58 my $skip_arsym = 0;
60 while (<ELFDUMP>) {
61 chomp;
63 # Ignore empty lines.
64 if (/^$/) {
65 # End of archive symbol table, stop skipping.
66 $skip_arsym = 0 if $skip_arsym;
67 next;
70 # Keep skipping until end of archive symbol table.
71 next if ($skip_arsym);
73 # Ignore object name header for individual objects and archives.
74 next if (/:$/);
76 # Ignore table header lines.
77 next if (/^Symbol Table Section:/);
78 next if (/index.*value.*size/);
80 # Start of archive symbol table: start skipping.
81 if (/^Symbol Table: \(archive/) {
82 $skip_arsym = 1;
83 next;
86 # Split table.
87 (undef, undef, undef, undef, $bind, $oth, undef, $shndx, $name) = split;
89 # Error out for unknown input.
90 die "unknown input line:\n$_" unless defined($bind);
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$_";
103 # Remember symbol.
104 $sym_hash{$name}++;
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;
113 # Ignore empty lines.
114 next if (/^$/);
116 # Ignore object name header.
117 next if (/^File: .*$/);
119 # Ignore table header lines.
120 next if (/^Symbol table.*contains.*:/);
121 next if (/Num:.*Value.*Size/);
123 # Split table.
124 (undef, undef, undef, undef, $bind, $vis, $ndx, $name) = split;
126 # Error out for unknown input.
127 die "unknown input line:\n$_" unless defined($bind);
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$_";
140 # Remember symbol.
141 $sym_hash{$name}++;
143 close READELF or die "readelf error";
146 ##########
147 # The various types of glob patterns.
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'.
153 # The type of the current pattern.
154 my $glob = 'glob';
156 # We're currently inside `extern "C++"', which Sun ld doesn't understand.
157 my $in_extern = 0;
159 # We're currently inside a conditional section: just skip it.
160 my $in_ifdef = 0;
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";
166 # The current version name.
167 my $current_version = "";
169 # Was there any attempt to match a symbol to this version?
170 my $matches_attempted;
172 # The number of versions which matched this symbol.
173 my $matched_symbols;
175 open F,$symvers or die $!;
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";
185 while (<F>) {
186 # End of skipped section.
187 if (/^[ \t]*\#endif/) {
188 $in_ifdef = 0;
189 next;
192 # Just skip a conditional section.
193 if ($in_ifdef) { next; }
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;
204 next;
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";
216 print; next;
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;
228 # Comment and blank lines
229 if (/^[ \t]*\#/) { print; next; }
230 if (/^[ \t]*$/) { print; next; }
232 # Lines of the form '{'
233 if (/^([ \t]*){$/) {
234 if ($in_extern) {
235 print "$1##{\n";
236 } else {
237 print;
239 next;
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;
253 # Ignore 'global:'
254 if (/^[ \t]*global:$/) { print; next; }
256 # After 'local:', globs should be ignored, they won't be exported.
257 if (/^[ \t]*local:$/) {
258 $glob = 'ign';
259 print;
260 next;
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;
271 # Chomp newline now we're done with passing through the input file.
272 chomp;
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 '.*'.
280 # Keep $ptn so we can still print the original form.
281 ($pattern = $ptn) =~ s/\*/\.\*/g;
283 if ($glob eq 'ign') {
284 # We're in a local: * section; just continue.
285 print "$_\n";
286 next;
289 # Print the glob commented for human readers.
290 print "$ws##$ptn ($glob)\n";
291 # We tried to match a symbol to this version.
292 $matches_attempted++;
294 if ($glob eq 'glob') {
295 my %ptn_syms = ();
297 # Match ptn against symbols in %sym_hash.
298 foreach my $sym (keys %sym_hash) {
299 # Maybe it matches one of the patterns based on the symbol in
300 # the .o file.
301 $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
304 foreach my $sym (sort keys(%ptn_syms)) {
305 $matched_symbols++;
306 print "$ws$sym;\n";
308 } elsif ($glob eq 'cxx') {
309 my %dem_syms = ();
311 # Verify that we're actually using GNU c++filt. Other versions
312 # most likely cannot handle GNU style symbol mangling.
313 my $cxxout = `$cxxfilt --version 2>&1`;
314 $cxxout =~ m/GNU/ or die "$0 requires GNU c++filt to function";
316 # Talk to c++filt through a pair of file descriptors.
317 # Need to start a fresh instance per pattern, otherwise the
318 # process grows to 500+ MB.
319 my $pid = open2(*FILTIN, *FILTOUT, $cxxfilt) or die $!;
321 # Match ptn against symbols in %sym_hash.
322 foreach my $sym (keys %sym_hash) {
323 # No? Well, maybe its demangled form matches one of those
324 # patterns.
325 printf FILTOUT "%s\n",$sym;
326 my $dem = <FILTIN>;
327 chomp $dem;
328 $dem_syms{$sym}++ if ($dem =~ /^$pattern$/);
331 close FILTOUT or die "c++filt error";
332 close FILTIN or die "c++filt error";
333 # Need to wait for the c++filt process to avoid lots of zombies.
334 waitpid $pid, 0;
336 foreach my $sym (sort keys(%dem_syms)) {
337 $matched_symbols++;
338 print "$ws$sym;\n";
340 } else {
341 # No? Well, then ignore it.
343 next;
345 # Important sanity check. This script can't handle lots of formats
346 # that GNU ld can, so be sure to error out if one is seen!
347 die "strange line `$_'";
349 close F;