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
22 # Input version script, GNU style.
26 # Get all the symbols from the library, match them, and add them to a hash.
30 # List of objects and archives to process.
33 # List of shared objects to omit from processing.
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);
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
53 # The path to elfdump.
54 my $elfdump = "/usr/ccs/bin/elfdump";
57 open ELFDUMP
,$elfdump.' -s '.(join ' ',@OBJECTS).'|' or die $!;
65 # End of archive symbol table, stop skipping.
66 $skip_arsym = 0 if $skip_arsym;
70 # Keep skipping until end of archive symbol table.
71 next if ($skip_arsym);
73 # Ignore object name header for individual objects and archives.
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/) {
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$_";
106 close ELFDUMP
or die "$elfdump error";
108 open READELF
, 'readelf -s -W '.(join ' ',@OBJECTS).'|' or die $!;
109 # Process each symbol.
113 # Ignore empty lines.
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/);
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$_";
143 close READELF
or die "readelf error";
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.
156 # We're currently inside `extern "C++"', which Sun ld doesn't understand.
159 # We're currently inside a conditional section: just skip it.
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.
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;
186 # End of skipped section.
187 if (/^[ \t]*\#endif/) {
192 # Just skip a conditional section.
193 if ($in_ifdef) { next; }
195 # Lines of the form '};'
196 if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) {
207 # Lines of the form '} SOME_VERSION_NAME_1.0;'
208 if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) {
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) {
214 print " .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n";
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/) {
228 # Comment and blank lines
229 if (/^[ \t]*\#/) { print; next; }
230 if (/^[ \t]*$/) { print; next; }
232 # Lines of the form '{'
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;
254 if (/^[ \t]*global:$/) { print; next; }
256 # After 'local:', globs should be ignored, they won't be exported.
257 if (/^[ \t]*local:$/) {
263 # After 'extern "C++"', globs are C++ patterns
264 if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) {
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.
274 # Catch globs. Note that '{}' is not allowed in globs by this script,
275 # so only '*' and '[]' are available.
276 if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) {
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.
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') {
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
301 $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
304 foreach my $sym (sort keys(%ptn_syms)) {
308 } elsif ($glob eq 'cxx') {
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
325 printf FILTOUT
"%s\n",$sym;
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.
336 foreach my $sym (sort keys(%dem_syms)) {
341 # No? Well, then ignore it.
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 `$_'";