OSDN Git Service

Use Pmode with stack_pointer_rtx
[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 # The c++filt command to use.  This *must* be GNU c++filt; the Sun Studio
160 # c++filt doesn't handle the GNU mangling style.
161 my $cxxfilt = $ENV{'CXXFILT'} || "c++filt";
162
163 # The current version name.
164 my $current_version = "";
165
166 # Was there any attempt to match a symbol to this version?
167 my $matches_attempted;
168
169 # The number of versions which matched this symbol.
170 my $matched_symbols;
171
172 open F,$symvers or die $!;
173
174 # Print information about generating this file
175 print "# This file was generated by make_sunver.pl.  DO NOT EDIT!\n";
176 print "# It was generated by:\n";
177 printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
178 printf "# Omitted archives with corresponding shared libraries: %s\n",
179     (join ' ', @SHAREDOBJS) if $#SHAREDOBJS >= 0;
180 print "#\n\n";
181
182 while (<F>) {
183     # Lines of the form '};'
184     if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) {
185         $glob = 'glob';
186         if ($in_extern) {
187             $in_extern--;
188             print "$1##$2";
189         } else {
190             print;
191         }
192         next;
193     }
194
195     # Lines of the form '} SOME_VERSION_NAME_1.0;'
196     if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) {
197         $glob = 'glob';
198         # We tried to match symbols agains this version, but none matched.
199         # Emit dummy hidden symbol to avoid marking this version WEAK.
200         if ($matches_attempted && $matched_symbols == 0) {
201             print "  hidden:\n";
202             print "    .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n";
203         }
204         print; next;
205     }
206
207     # Comment and blank lines
208     if (/^[ \t]*\#/) { print; next; }
209     if (/^[ \t]*$/) { print; next; }
210
211     # Lines of the form '{'
212     if (/^([ \t]*){$/) {
213         if ($in_extern) {
214             print "$1##{\n";
215         } else {
216             print;
217         }
218         next;
219     }
220
221     # Lines of the form 'SOME_VERSION_NAME_1.1 {'
222     if (/^([A-Z0-9_.]+)[ \t]+{$/) {
223         # Record version name.
224         $current_version = $1;
225         # Reset match attempts, #matched symbols for this version.
226         $matches_attempted = 0;
227         $matched_symbols = 0;
228         print;
229         next;
230     }
231
232     # Ignore 'global:'
233     if (/^[ \t]*global:$/) { print; next; }
234
235     # After 'local:', globs should be ignored, they won't be exported.
236     if (/^[ \t]*local:$/) {
237         $glob = 'ign';
238         print;
239         next;
240     }
241
242     # After 'extern "C++"', globs are C++ patterns
243     if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) {
244         $in_extern++;
245         $glob = 'cxx';
246         # Need to comment, Sun ld cannot handle this.
247         print "$1##$2\n"; next;
248     }
249
250     # Chomp newline now we're done with passing through the input file.
251     chomp;
252
253     # Catch globs.  Note that '{}' is not allowed in globs by this script,
254     # so only '*' and '[]' are available.
255     if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) {
256         my $ws = $1;
257         my $ptn = $2;
258         # Turn the glob into a regex by replacing '*' with '.*', '?' with '.'.
259         # Keep $ptn so we can still print the original form.
260         ($pattern = $ptn) =~ s/\*/\.\*/g;
261         $pattern =~ s/\?/\./g;
262
263         if ($glob eq 'ign') {
264             # We're in a local: * section; just continue.
265             print "$_\n";
266             next;
267         }
268
269         # Print the glob commented for human readers.
270         print "$ws##$ptn ($glob)\n";
271         # We tried to match a symbol to this version.
272         $matches_attempted++;
273
274         if ($glob eq 'glob') {
275             my %ptn_syms = ();
276
277             # Match ptn against symbols in %sym_hash.
278             foreach my $sym (keys %sym_hash) {
279                 # Maybe it matches one of the patterns based on the symbol in
280                 # the .o file.
281                 $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
282             }
283
284             foreach my $sym (sort keys(%ptn_syms)) {
285                 $matched_symbols++;
286                 print "$ws$sym;\n";
287             }
288         } elsif ($glob eq 'cxx') {
289             my %dem_syms = ();
290
291             # Verify that we're actually using GNU c++filt.  Other versions
292             # most likely cannot handle GNU style symbol mangling.
293             my $cxxout = `$cxxfilt --version 2>&1`;
294             $cxxout =~ m/GNU/ or die "$0 requires GNU c++filt to function";
295
296             # Talk to c++filt through a pair of file descriptors.
297             # Need to start a fresh instance per pattern, otherwise the
298             # process grows to 500+ MB.
299             my $pid = open2(*FILTIN, *FILTOUT, $cxxfilt) or die $!;
300
301             # Match ptn against symbols in %sym_hash.
302             foreach my $sym (keys %sym_hash) {
303                 # No?  Well, maybe its demangled form matches one of those
304                 # patterns.
305                 printf FILTOUT "%s\n",$sym;
306                 my $dem = <FILTIN>;
307                 chomp $dem;
308                 $dem_syms{$sym}++ if ($dem =~ /^$pattern$/);
309             }
310
311             close FILTOUT or die "c++filt error";
312             close FILTIN or die "c++filt error";
313             # Need to wait for the c++filt process to avoid lots of zombies.
314             waitpid $pid, 0;
315
316             foreach my $sym (sort keys(%dem_syms)) {
317                 $matched_symbols++;
318                 print "$ws$sym;\n";
319             }
320         } else {
321             # No?  Well, then ignore it.
322         }
323         next;
324     }
325     # Important sanity check.  This script can't handle lots of formats
326     # that GNU ld can, so be sure to error out if one is seen!
327     die "strange line `$_'";
328 }
329 close F;