5 # Copyright (C) 2010, 2011, 2012, 2013
6 # Free Software Foundation, Inc.
8 # This file is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; see the file COPYING.GPLv3. If not see
20 # <http://www.gnu.org/licenses/>.
22 # This script takes at least two arguments, a GNU style version script and
23 # a list of object and archive files, and generates a corresponding Sun
24 # style version script as follows:
26 # Each glob pattern, C++ mangled pattern or literal in the input script is
27 # matched against all global symbols in the input objects, emitting those
28 # that matched (or nothing if no match was found).
29 # A comment with the original pattern and its type is left in the output
30 # file to make it easy to understand the matches.
32 # It uses elfdump when present (native), GNU readelf otherwise.
33 # It depends on the GNU version of c++filt, since it must understand the
43 # Input version script, GNU style.
47 # Get all the symbols from the library, match them, and add them to a hash.
51 # List of objects and archives to process.
54 # List of shared objects to omit from processing.
57 # Filter out those input archives that have corresponding shared objects to
58 # avoid adding all symbols matched in the archive to the output map.
59 foreach $file (@ARGV) {
60 if (($so = $file) =~ s/\.a$/.so/ && -e $so) {
61 printf STDERR "omitted $file -> $so\n";
62 push (@SHAREDOBJS, $so);
64 push (@OBJECTS, $file);
68 # We need to detect and ignore hidden symbols. Solaris nm can only detect
69 # this in the harder to parse default output format, and GNU nm not at all,
70 # so use elfdump -s in the native case and GNU readelf -s otherwise.
71 # GNU objdump -t cannot be used since it produces a variable number of
74 # The path to elfdump.
75 my $elfdump = "/usr/ccs/bin/elfdump";
78 open ELFDUMP,$elfdump.' -s '.(join ' ',@OBJECTS).'|' or die $!;
86 # End of archive symbol table, stop skipping.
87 $skip_arsym = 0 if $skip_arsym;
91 # Keep skipping until end of archive symbol table.
92 next if ($skip_arsym);
94 # Ignore object name header for individual objects and archives.
97 # Ignore table header lines.
98 next if (/^Symbol Table Section:/);
99 next if (/index.*value.*size/);
101 # Start of archive symbol table: start skipping.
102 if (/^Symbol Table: \(archive/) {
108 (undef, undef, undef, undef, $bind, $oth, undef, $shndx, $name) = split;
110 # Error out for unknown input.
111 die "unknown input line:\n$_" unless defined($bind);
113 # Ignore local symbols.
114 next if ($bind eq "LOCL");
115 # Ignore hidden symbols.
116 next if ($oth eq "H");
117 # Ignore undefined symbols.
118 next if ($shndx eq "UNDEF");
119 # Error out for unhandled cases.
120 if ($bind !~ /^(GLOB|WEAK)/ or $oth ne "D") {
121 die "unhandled symbol:\n$_";
127 close ELFDUMP or die "$elfdump error";
129 open READELF, 'readelf -s -W '.(join ' ',@OBJECTS).'|' or die $!;
130 # Process each symbol.
134 # Ignore empty lines.
137 # Ignore object name header.
138 next if (/^File: .*$/);
140 # Ignore table header lines.
141 next if (/^Symbol table.*contains.*:/);
142 next if (/Num:.*Value.*Size/);
145 (undef, undef, undef, undef, $bind, $vis, $ndx, $name) = split;
147 # Error out for unknown input.
148 die "unknown input line:\n$_" unless defined($bind);
150 # Ignore local symbols.
151 next if ($bind eq "LOCAL");
152 # Ignore hidden symbols.
153 next if ($vis eq "HIDDEN");
154 # Ignore undefined symbols.
155 next if ($ndx eq "UND");
156 # Error out for unhandled cases.
157 if ($bind !~ /^(GLOBAL|WEAK)/ or $vis ne "DEFAULT") {
158 die "unhandled symbol:\n$_";
164 close READELF or die "readelf error";
168 # The various types of glob patterns.
170 # A glob pattern that is to be applied to the demangled name: 'cxx'.
171 # A glob patterns that applies directly to the name in the .o files: 'glob'.
172 # This pattern is ignored; used for local variables (usually just '*'): 'ign'.
174 # The type of the current pattern.
177 # We're currently inside `extern "C++"', which Sun ld doesn't understand.
180 # The c++filt command to use. This *must* be GNU c++filt; the Sun Studio
181 # c++filt doesn't handle the GNU mangling style.
182 my $cxxfilt = $ENV{'CXXFILT'} || "c++filt";
184 # The current version name.
185 my $current_version = "";
187 # Was there any attempt to match a symbol to this version?
188 my $matches_attempted;
190 # The number of versions which matched this symbol.
193 open F,$symvers or die $!;
195 # Print information about generating this file
196 print "# This file was generated by make_sunver.pl. DO NOT EDIT!\n";
197 print "# It was generated by:\n";
198 printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
199 printf "# Omitted archives with corresponding shared libraries: %s\n",
200 (join ' ', @SHAREDOBJS) if $#SHAREDOBJS >= 0;
203 print "\$mapfile_version 2\n";
206 # Lines of the form '};'
207 if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) {
218 # Lines of the form '} SOME_VERSION_NAME_1.0;'
219 if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) {
221 # We tried to match symbols agains this version, but none matched.
222 # Emit dummy hidden symbol to avoid marking this version WEAK.
223 if ($matches_attempted && $matched_symbols == 0) {
225 print " .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n";
230 # Comment and blank lines
231 if (/^[ \t]*\#/) { print; next; }
232 if (/^[ \t]*$/) { print; next; }
234 # Lines of the form '{'
244 # Lines of the form 'SOME_VERSION_NAME_1.1 {'
245 if (/^([A-Z0-9_.]+)[ \t]+{$/) {
246 # Record version name.
247 $current_version = $1;
248 # Reset match attempts, #matched symbols for this version.
249 $matches_attempted = 0;
250 $matched_symbols = 0;
251 print "SYMBOL_VERSION $1 {\n";
256 if (/^[ \t]*global:$/) { print; next; }
258 # After 'local:', globs should be ignored, they won't be exported.
259 if (/^[ \t]*local:$/) {
265 # After 'extern "C++"', globs are C++ patterns
266 if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) {
269 # Need to comment, Sun ld cannot handle this.
270 print "$1##$2\n"; next;
273 # Chomp newline now we're done with passing through the input file.
276 # Catch globs. Note that '{}' is not allowed in globs by this script,
277 # so only '*' and '[]' are available.
278 if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) {
281 # Turn the glob into a regex by replacing '*' with '.*', '?' with '.'.
282 # Keep $ptn so we can still print the original form.
283 ($pattern = $ptn) =~ s/\*/\.\*/g;
284 $pattern =~ s/\?/\./g;
286 if ($glob eq 'ign') {
287 # We're in a local: * section; just continue.
292 # Print the glob commented for human readers.
293 print "$ws##$ptn ($glob)\n";
294 # We tried to match a symbol to this version.
295 $matches_attempted++;
297 if ($glob eq 'glob') {
300 # Match ptn against symbols in %sym_hash.
301 foreach my $sym (keys %sym_hash) {
302 # Maybe it matches one of the patterns based on the symbol in
304 $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
307 foreach my $sym (sort keys(%ptn_syms)) {
311 } elsif ($glob eq 'cxx') {
314 # Verify that we're actually using GNU c++filt. Other versions
315 # most likely cannot handle GNU style symbol mangling.
316 my $cxxout = `$cxxfilt --version 2>&1`;
317 $cxxout =~ m/GNU/ or die "$0 requires GNU c++filt to function";
319 # Talk to c++filt through a pair of file descriptors.
320 # Need to start a fresh instance per pattern, otherwise the
321 # process grows to 500+ MB.
322 my $pid = open2(*FILTIN, *FILTOUT, $cxxfilt) or die $!;
324 # Match ptn against symbols in %sym_hash.
325 foreach my $sym (keys %sym_hash) {
326 # No? Well, maybe its demangled form matches one of those
328 printf FILTOUT "%s\n",$sym;
331 $dem_syms{$sym}++ if ($dem =~ /^$pattern$/);
334 close FILTOUT or die "c++filt error";
335 close FILTIN or die "c++filt error";
336 # Need to wait for the c++filt process to avoid lots of zombies.
339 foreach my $sym (sort keys(%dem_syms)) {
344 # No? Well, then ignore it.
348 # Important sanity check. This script can't handle lots of formats
349 # that GNU ld can, so be sure to error out if one is seen!
350 die "strange line `$_'";