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
19 use File
::Glob
':glob';
23 # Input version script, GNU style.
27 # Get all the symbols from the library, match them, and add them to a hash.
31 # List of objects and archives to process.
34 # List of shared objects to omit from processing.
37 # Filter out those input archives that have corresponding shared objects to
38 # avoid adding all symbols matched in the archive to the output map.
39 foreach $file (@ARGV) {
40 if (($so = $file) =~ s/\.a$/.so/ && -e
$so) {
41 printf STDERR
"omitted $file -> $so\n";
42 push (@SHAREDOBJS, $so);
44 push (@OBJECTS, $file);
48 # We need to detect and ignore hidden symbols. Solaris nm can only detect
49 # this in the harder to parse default output format, and GNU nm not at all,
50 # so use elfdump -s in the native case and GNU readelf -s otherwise.
51 # GNU objdump -t cannot be used since it produces a variable number of
54 # The path to elfdump.
55 my $elfdump = "/usr/ccs/bin/elfdump";
58 open ELFDUMP
,$elfdump.' -s '.(join ' ',@OBJECTS).'|' or die $!;
66 # End of archive symbol table, stop skipping.
67 $skip_arsym = 0 if $skip_arsym;
71 # Keep skipping until end of archive symbol table.
72 next if ($skip_arsym);
74 # Ignore object name header for individual objects and archives.
77 # Ignore table header lines.
78 next if (/^Symbol Table Section:/);
79 next if (/index.*value.*size/);
81 # Start of archive symbol table: start skipping.
82 if (/^Symbol Table: \(archive/) {
88 (undef, undef, undef, undef, $bind, $oth, undef, $shndx, $name) = split;
90 # Error out for unknown input.
91 die "unknown input line:\n$_" unless defined($bind);
93 # Ignore local symbols.
94 next if ($bind eq "LOCL");
95 # Ignore hidden symbols.
96 next if ($oth eq "H");
97 # Ignore undefined symbols.
98 next if ($shndx eq "UNDEF");
99 # Error out for unhandled cases.
100 if ($bind !~ /^(GLOB|WEAK)/ or $oth ne "D") {
101 die "unhandled symbol:\n$_";
107 close ELFDUMP
or die "$elfdump error";
109 open READELF
, 'readelf -s -W '.(join ' ',@OBJECTS).'|' or die $!;
110 # Process each symbol.
114 # Ignore empty lines.
117 # Ignore object name header.
118 next if (/^File: .*$/);
120 # Ignore table header lines.
121 next if (/^Symbol table.*contains.*:/);
122 next if (/Num:.*Value.*Size/);
125 (undef, undef, undef, undef, $bind, $vis, $ndx, $name) = split;
127 # Error out for unknown input.
128 die "unknown input line:\n$_" unless defined($bind);
130 # Ignore local symbols.
131 next if ($bind eq "LOCAL");
132 # Ignore hidden symbols.
133 next if ($vis eq "HIDDEN");
134 # Ignore undefined symbols.
135 next if ($ndx eq "UND");
136 # Error out for unhandled cases.
137 if ($bind !~ /^(GLOBAL|WEAK)/ or $vis ne "DEFAULT") {
138 die "unhandled symbol:\n$_";
144 close READELF
or die "readelf error";
148 # The various types of glob patterns.
150 # A glob pattern that is to be applied to the demangled name: 'cxx'.
151 # A glob patterns that applies directly to the name in the .o files: 'glob'.
152 # This pattern is ignored; used for local variables (usually just '*'): 'ign'.
154 # The type of the current pattern.
157 # We're currently inside `extern "C++"', which Sun ld doesn't understand.
160 # We're currently inside a conditional section: just skip it.
163 # The c++filt command to use. This *must* be GNU c++filt; the Sun Studio
164 # c++filt doesn't handle the GNU mangling style.
165 my $cxxfilt = $ENV{'CXXFILT'} || "c++filt";
167 # The current version name.
168 my $current_version = "";
170 # Was there any attempt to match a symbol to this version?
171 my $matches_attempted;
173 # The number of versions which matched this symbol.
176 open F
,$symvers or die $!;
178 # Print information about generating this file
179 print "# This file was generated by make_sunver.pl. DO NOT EDIT!\n";
180 print "# It was generated by:\n";
181 printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
182 printf "# Omitted archives with corresponding shared libraries: %s\n",
183 (join ' ', @SHAREDOBJS) if $#SHAREDOBJS >= 0;
187 # End of skipped section.
188 if (/^[ \t]*\#endif/) {
193 # Just skip a conditional section.
194 if ($in_ifdef) { next; }
196 # Lines of the form '};'
197 if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) {
208 # Lines of the form '} SOME_VERSION_NAME_1.0;'
209 if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) {
211 # We tried to match symbols agains this version, but none matched.
212 # Emit dummy hidden symbol to avoid marking this version WEAK.
213 if ($matches_attempted && $matched_symbols == 0) {
215 print " .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n";
220 # Special comments that look like C preprocessor conditionals.
221 # Just skip the contents for now.
222 # FIXME: Allow passing in conditionals from the command line to really
223 # control the skipping.
224 if (/^[ \t]*\#ifdef/) {
229 # Comment and blank lines
230 if (/^[ \t]*\#/) { print; next; }
231 if (/^[ \t]*$/) { print; next; }
233 # Lines of the form '{'
243 # Lines of the form 'SOME_VERSION_NAME_1.1 {'
244 if (/^([A-Z0-9_.]+)[ \t]+{$/) {
245 # Record version name.
246 $current_version = $1;
247 # Reset match attempts, #matched symbols for this version.
248 $matches_attempted = 0;
249 $matched_symbols = 0;
255 if (/^[ \t]*global:$/) { print; next; }
257 # After 'local:', globs should be ignored, they won't be exported.
258 if (/^[ \t]*local:$/) {
264 # After 'extern "C++"', globs are C++ patterns
265 if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) {
268 # Need to comment, Sun ld cannot handle this.
269 print "$1##$2\n"; next;
272 # Chomp newline now we're done with passing through the input file.
275 # Catch globs. Note that '{}' is not allowed in globs by this script,
276 # so only '*' and '[]' are available.
277 if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) {
280 # Turn the glob into a regex by replacing '*' with '.*'.
281 # Keep $ptn so we can still print the original form.
282 ($pattern = $ptn) =~ s/\*/\.\*/g;
284 if ($glob eq 'ign') {
285 # We're in a local: * section; just continue.
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++;
295 if ($glob eq 'glob') {
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
302 $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
305 foreach my $sym (sort keys(%ptn_syms)) {
309 } elsif ($glob eq 'cxx') {
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";
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 $!;
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
326 printf FILTOUT
"%s\n",$sym;
329 $dem_syms{$sym}++ if ($dem =~ /^$pattern$/);
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.
337 foreach my $sym (sort keys(%dem_syms)) {
342 # No? Well, then ignore it.
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 `$_'";