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 # 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";
163 # The current version name.
164 my $current_version = "";
166 # Was there any attempt to match a symbol to this version?
167 my $matches_attempted;
169 # The number of versions which matched this symbol.
172 open F
,$symvers or die $!;
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;
183 # Lines of the form '};'
184 if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) {
195 # Lines of the form '} SOME_VERSION_NAME_1.0;'
196 if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) {
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) {
202 print " .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n";
207 # Comment and blank lines
208 if (/^[ \t]*\#/) { print; next; }
209 if (/^[ \t]*$/) { print; next; }
211 # Lines of the form '{'
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;
233 if (/^[ \t]*global:$/) { print; next; }
235 # After 'local:', globs should be ignored, they won't be exported.
236 if (/^[ \t]*local:$/) {
242 # After 'extern "C++"', globs are C++ patterns
243 if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) {
246 # Need to comment, Sun ld cannot handle this.
247 print "$1##$2\n"; next;
250 # Chomp newline now we're done with passing through the input file.
253 # Catch globs. Note that '{}' is not allowed in globs by this script,
254 # so only '*' and '[]' are available.
255 if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) {
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;
263 if ($glob eq 'ign') {
264 # We're in a local: * section; just continue.
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++;
274 if ($glob eq 'glob') {
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
281 $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
284 foreach my $sym (sort keys(%ptn_syms)) {
288 } elsif ($glob eq 'cxx') {
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";
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 $!;
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
305 printf FILTOUT
"%s\n",$sym;
308 $dem_syms{$sym}++ if ($dem =~ /^$pattern$/);
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.
316 foreach my $sym (sort keys(%dem_syms)) {
321 # No? Well, then ignore it.
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 `$_'";