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
27 # Input version script, GNU style.
31 # Get all the symbols from the library, match them, and add them to a hash.
35 # List of objects and archives to process.
38 # List of shared objects to omit from processing.
41 foreach $file (@ARGV) {
42 # Filter out those input archives that have corresponding shared objects to
43 # avoid adding all symbols matched in the archive to the output map.
44 if (($so = $file) =~ s/\.a$/.so/ && -e
$so) {
45 printf STDERR
"omitted $file -> $so\n";
46 push (@SHAREDOBJS, $so);
48 } elsif ($file =~ /^-l/) {
50 # Convert libtool object/archive names to underlying objects/archives.
51 } elsif ($file =~ /\.l[ao]$/) {
52 my ($name, $path, $suffix) = fileparse
($file, ".l[ao]");
54 # Strip leading ./ prepended by fileparse.
56 push (@OBJECTS, "$path.libs/$name$suffix")
58 push (@OBJECTS, $file);
62 # We need to detect and ignore hidden symbols. Solaris nm can only detect
63 # this in the harder to parse default output format, and GNU nm not at all,
64 # so use elfdump -s in the native case and GNU readelf -s otherwise.
65 # GNU objdump -t cannot be used since it produces a variable number of
68 # The path to elfdump.
69 my $elfdump = "/usr/ccs/bin/elfdump";
72 open ELFDUMP
,$elfdump.' -s '.(join ' ',@OBJECTS).'|' or die $!;
80 # End of archive symbol table, stop skipping.
81 $skip_arsym = 0 if $skip_arsym;
85 # Keep skipping until end of archive symbol table.
86 next if ($skip_arsym);
88 # Ignore object name header for individual objects and archives.
91 # Ignore table header lines.
92 next if (/^Symbol Table Section:/);
93 next if (/index.*value.*size/);
95 # Start of archive symbol table: start skipping.
96 if (/^Symbol Table: \(archive/) {
102 (undef, undef, undef, undef, $bind, $oth, undef, $shndx, $name) = split;
104 # Error out for unknown input.
105 die "unknown input line:\n$_" unless defined($bind);
107 # Ignore local symbols.
108 next if ($bind eq "LOCL");
109 # Ignore hidden symbols.
110 next if ($oth eq "H");
111 # Ignore undefined symbols.
112 next if ($shndx eq "UNDEF");
113 # Error out for unhandled cases.
114 if ($bind !~ /^(GLOB|WEAK)/ or $oth ne "D") {
115 die "unhandled symbol:\n$_";
121 close ELFDUMP
or die "$elfdump error";
123 open READELF
, 'readelf -s -W '.(join ' ',@OBJECTS).'|' or die $!;
124 # Process each symbol.
128 # Ignore empty lines.
131 # Ignore object name header.
132 next if (/^File: .*$/);
134 # Ignore table header lines.
135 next if (/^Symbol table.*contains.*:/);
136 next if (/Num:.*Value.*Size/);
139 (undef, undef, undef, undef, $bind, $vis, $ndx, $name) = split;
141 # Error out for unknown input.
142 die "unknown input line:\n$_" unless defined($bind);
144 # Ignore local symbols.
145 next if ($bind eq "LOCAL");
146 # Ignore hidden symbols.
147 next if ($vis eq "HIDDEN");
148 # Ignore undefined symbols.
149 next if ($ndx eq "UND");
150 # Error out for unhandled cases.
151 if ($bind !~ /^(GLOBAL|WEAK)/ or $vis ne "DEFAULT") {
152 die "unhandled symbol:\n$_";
158 close READELF
or die "readelf error";
162 # The various types of glob patterns.
164 # A glob pattern that is to be applied to the demangled name: 'cxx'.
165 # A glob patterns that applies directly to the name in the .o files: 'glob'.
166 # This pattern is ignored; used for local variables (usually just '*'): 'ign'.
168 # The type of the current pattern.
171 # We're currently inside `extern "C++"', which Sun ld doesn't understand.
174 # The c++filt command to use. This *must* be GNU c++filt; the Sun Studio
175 # c++filt doesn't handle the GNU mangling style.
176 my $cxxfilt = $ENV{'CXXFILT'} || "c++filt";
178 # The current version name.
179 my $current_version = "";
181 # Was there any attempt to match a symbol to this version?
182 my $matches_attempted;
184 # The number of versions which matched this symbol.
187 open F
,$symvers or die $!;
189 # Print information about generating this file
190 print "# This file was generated by make_sunver.pl. DO NOT EDIT!\n";
191 print "# It was generated by:\n";
192 printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
193 printf "# Omitted archives with corresponding shared libraries: %s\n",
194 (join ' ', @SHAREDOBJS) if $#SHAREDOBJS >= 0;
198 # Lines of the form '};'
199 if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) {
210 # Lines of the form '} SOME_VERSION_NAME_1.0;'
211 if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) {
213 # We tried to match symbols agains this version, but none matched.
214 # Emit dummy hidden symbol to avoid marking this version WEAK.
215 if ($matches_attempted && $matched_symbols == 0) {
217 print " .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n";
222 # Comment and blank lines
223 if (/^[ \t]*\#/) { print; next; }
224 if (/^[ \t]*$/) { print; next; }
226 # Lines of the form '{'
227 if (/^([ \t]*)\{$/) {
236 # Lines of the form 'SOME_VERSION_NAME_1.1 {'
237 if (/^([A-Z0-9_.]+)[ \t]+{$/) {
238 # Record version name.
239 $current_version = $1;
240 # Reset match attempts, #matched symbols for this version.
241 $matches_attempted = 0;
242 $matched_symbols = 0;
248 if (/^[ \t]*global:$/) { print; next; }
250 # After 'local:', globs should be ignored, they won't be exported.
251 if (/^[ \t]*local:$/) {
257 # After 'extern "C++"', globs are C++ patterns
258 if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) {
261 # Need to comment, Sun ld cannot handle this.
262 print "$1##$2\n"; next;
265 # Chomp newline now we're done with passing through the input file.
268 # Catch globs. Note that '{}' is not allowed in globs by this script,
269 # so only '*' and '[]' are available.
270 if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) {
273 # Turn the glob into a regex by replacing '*' with '.*', '?' with '.'.
274 # Keep $ptn so we can still print the original form.
275 ($pattern = $ptn) =~ s/\*/\.\*/g;
276 $pattern =~ s/\?/\./g;
278 if ($glob eq 'ign') {
279 # We're in a local: * section; just continue.
284 # Print the glob commented for human readers.
285 print "$ws##$ptn ($glob)\n";
286 # We tried to match a symbol to this version.
287 $matches_attempted++;
289 if ($glob eq 'glob') {
292 # Match ptn against symbols in %sym_hash.
293 foreach my $sym (keys %sym_hash) {
294 # Maybe it matches one of the patterns based on the symbol in
296 $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
299 foreach my $sym (sort keys(%ptn_syms)) {
303 } elsif ($glob eq 'cxx') {
306 # Verify that we're actually using GNU c++filt. Other versions
307 # most likely cannot handle GNU style symbol mangling.
308 my $cxxout = `$cxxfilt --version 2>&1`;
309 $cxxout =~ m/GNU/ or die "$0 requires GNU c++filt to function";
311 # Talk to c++filt through a pair of file descriptors.
312 # Need to start a fresh instance per pattern, otherwise the
313 # process grows to 500+ MB.
314 my $pid = open2
(*FILTIN
, *FILTOUT
, $cxxfilt) or die $!;
316 # Match ptn against symbols in %sym_hash.
317 foreach my $sym (keys %sym_hash) {
318 # No? Well, maybe its demangled form matches one of those
320 printf FILTOUT
"%s\n",$sym;
323 $dem_syms{$sym}++ if ($dem =~ /^$pattern$/);
326 close FILTOUT
or die "c++filt error";
327 close FILTIN
or die "c++filt error";
328 # Need to wait for the c++filt process to avoid lots of zombies.
331 foreach my $sym (sort keys(%dem_syms)) {
336 # No? Well, then ignore it.
340 # Important sanity check. This script can't handle lots of formats
341 # that GNU ld can, so be sure to error out if one is seen!
342 die "strange line `$_'";