| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
# make_sunver.pl |
| 4 |
# |
| 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: |
| 8 |
# |
| 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. |
| 14 |
# |
| 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 |
| 17 |
# GNU mangling style. |
| 18 |
|
| 19 |
use FileHandle; |
| 20 |
use File::Basename; |
| 21 |
use IPC::Open2; |
| 22 |
|
| 23 |
# Enforce C locale. |
| 24 |
$ENV{'LC_ALL'} = "C"; |
| 25 |
$ENV{'LANG'} = "C"; |
| 26 |
|
| 27 |
# Input version script, GNU style. |
| 28 |
my $symvers = shift; |
| 29 |
|
| 30 |
########## |
| 31 |
# Get all the symbols from the library, match them, and add them to a hash. |
| 32 |
|
| 33 |
my %sym_hash = (); |
| 34 |
|
| 35 |
# List of objects and archives to process. |
| 36 |
my @OBJECTS = (); |
| 37 |
|
| 38 |
# List of shared objects to omit from processing. |
| 39 |
my @SHAREDOBJS = (); |
| 40 |
|
| 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); |
| 47 |
# Skip libraries. |
| 48 |
} elsif ($file =~ /^-l/) { |
| 49 |
next; |
| 50 |
# Convert libtool object/archive names to underlying objects/archives. |
| 51 |
} elsif ($file =~ /\.l[ao]$/) { |
| 52 |
my ($name, $path, $suffix) = fileparse($file, ".l[ao]"); |
| 53 |
$suffix =~ s/l//; |
| 54 |
# Strip leading ./ prepended by fileparse. |
| 55 |
$path =~ s%^\./%%; |
| 56 |
push (@OBJECTS, "$path.libs/$name$suffix") |
| 57 |
} else { |
| 58 |
push (@OBJECTS, $file); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 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 |
| 66 |
# columns. |
| 67 |
|
| 68 |
# The path to elfdump. |
| 69 |
my $elfdump = "/usr/ccs/bin/elfdump"; |
| 70 |
|
| 71 |
if (-f $elfdump) { |
| 72 |
open ELFDUMP,$elfdump.' -s '.(join ' ',@OBJECTS).'|' or die $!; |
| 73 |
my $skip_arsym = 0; |
| 74 |
|
| 75 |
while (<ELFDUMP>) { |
| 76 |
chomp; |
| 77 |
|
| 78 |
# Ignore empty lines. |
| 79 |
if (/^$/) { |
| 80 |
# End of archive symbol table, stop skipping. |
| 81 |
$skip_arsym = 0 if $skip_arsym; |
| 82 |
next; |
| 83 |
} |
| 84 |
|
| 85 |
# Keep skipping until end of archive symbol table. |
| 86 |
next if ($skip_arsym); |
| 87 |
|
| 88 |
# Ignore object name header for individual objects and archives. |
| 89 |
next if (/:$/); |
| 90 |
|
| 91 |
# Ignore table header lines. |
| 92 |
next if (/^Symbol Table Section:/); |
| 93 |
next if (/index.*value.*size/); |
| 94 |
|
| 95 |
# Start of archive symbol table: start skipping. |
| 96 |
if (/^Symbol Table: \(archive/) { |
| 97 |
$skip_arsym = 1; |
| 98 |
next; |
| 99 |
} |
| 100 |
|
| 101 |
# Split table. |
| 102 |
(undef, undef, undef, undef, $bind, $oth, undef, $shndx, $name) = split; |
| 103 |
|
| 104 |
# Error out for unknown input. |
| 105 |
die "unknown input line:\n$_" unless defined($bind); |
| 106 |
|
| 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$_"; |
| 116 |
} |
| 117 |
|
| 118 |
# Remember symbol. |
| 119 |
$sym_hash{$name}++; |
| 120 |
} |
| 121 |
close ELFDUMP or die "$elfdump error"; |
| 122 |
} else { |
| 123 |
open READELF, 'readelf -s -W '.(join ' ',@OBJECTS).'|' or die $!; |
| 124 |
# Process each symbol. |
| 125 |
while (<READELF>) { |
| 126 |
chomp; |
| 127 |
|
| 128 |
# Ignore empty lines. |
| 129 |
next if (/^$/); |
| 130 |
|
| 131 |
# Ignore object name header. |
| 132 |
next if (/^File: .*$/); |
| 133 |
|
| 134 |
# Ignore table header lines. |
| 135 |
next if (/^Symbol table.*contains.*:/); |
| 136 |
next if (/Num:.*Value.*Size/); |
| 137 |
|
| 138 |
# Split table. |
| 139 |
(undef, undef, undef, undef, $bind, $vis, $ndx, $name) = split; |
| 140 |
|
| 141 |
# Error out for unknown input. |
| 142 |
die "unknown input line:\n$_" unless defined($bind); |
| 143 |
|
| 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$_"; |
| 153 |
} |
| 154 |
|
| 155 |
# Remember symbol. |
| 156 |
$sym_hash{$name}++; |
| 157 |
} |
| 158 |
close READELF or die "readelf error"; |
| 159 |
} |
| 160 |
|
| 161 |
########## |
| 162 |
# The various types of glob patterns. |
| 163 |
# |
| 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'. |
| 167 |
|
| 168 |
# The type of the current pattern. |
| 169 |
my $glob = 'glob'; |
| 170 |
|
| 171 |
# We're currently inside `extern "C++"', which Sun ld doesn't understand. |
| 172 |
my $in_extern = 0; |
| 173 |
|
| 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"; |
| 177 |
|
| 178 |
# The current version name. |
| 179 |
my $current_version = ""; |
| 180 |
|
| 181 |
# Was there any attempt to match a symbol to this version? |
| 182 |
my $matches_attempted; |
| 183 |
|
| 184 |
# The number of versions which matched this symbol. |
| 185 |
my $matched_symbols; |
| 186 |
|
| 187 |
open F,$symvers or die $!; |
| 188 |
|
| 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; |
| 195 |
print "#\n\n"; |
| 196 |
|
| 197 |
while (<F>) { |
| 198 |
# Lines of the form '};' |
| 199 |
if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) { |
| 200 |
$glob = 'glob'; |
| 201 |
if ($in_extern) { |
| 202 |
$in_extern--; |
| 203 |
print "$1##$2\n"; |
| 204 |
} else { |
| 205 |
print; |
| 206 |
} |
| 207 |
next; |
| 208 |
} |
| 209 |
|
| 210 |
# Lines of the form '} SOME_VERSION_NAME_1.0;' |
| 211 |
if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) { |
| 212 |
$glob = 'glob'; |
| 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) { |
| 216 |
print " hidden:\n"; |
| 217 |
print " .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n"; |
| 218 |
} |
| 219 |
print; next; |
| 220 |
} |
| 221 |
|
| 222 |
# Comment and blank lines |
| 223 |
if (/^[ \t]*\#/) { print; next; } |
| 224 |
if (/^[ \t]*$/) { print; next; } |
| 225 |
|
| 226 |
# Lines of the form '{' |
| 227 |
if (/^([ \t]*)\{$/) { |
| 228 |
if ($in_extern) { |
| 229 |
print "$1##{\n"; |
| 230 |
} else { |
| 231 |
print; |
| 232 |
} |
| 233 |
next; |
| 234 |
} |
| 235 |
|
| 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; |
| 243 |
print; |
| 244 |
next; |
| 245 |
} |
| 246 |
|
| 247 |
# Ignore 'global:' |
| 248 |
if (/^[ \t]*global:$/) { print; next; } |
| 249 |
|
| 250 |
# After 'local:', globs should be ignored, they won't be exported. |
| 251 |
if (/^[ \t]*local:$/) { |
| 252 |
$glob = 'ign'; |
| 253 |
print; |
| 254 |
next; |
| 255 |
} |
| 256 |
|
| 257 |
# After 'extern "C++"', globs are C++ patterns |
| 258 |
if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) { |
| 259 |
$in_extern++; |
| 260 |
$glob = 'cxx'; |
| 261 |
# Need to comment, Sun ld cannot handle this. |
| 262 |
print "$1##$2\n"; next; |
| 263 |
} |
| 264 |
|
| 265 |
# Chomp newline now we're done with passing through the input file. |
| 266 |
chomp; |
| 267 |
|
| 268 |
# Catch globs. Note that '{}' is not allowed in globs by this script, |
| 269 |
# so only '*' and '[]' are available. |
| 270 |
if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) { |
| 271 |
my $ws = $1; |
| 272 |
my $ptn = $2; |
| 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; |
| 277 |
|
| 278 |
if ($glob eq 'ign') { |
| 279 |
# We're in a local: * section; just continue. |
| 280 |
print "$_\n"; |
| 281 |
next; |
| 282 |
} |
| 283 |
|
| 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++; |
| 288 |
|
| 289 |
if ($glob eq 'glob') { |
| 290 |
my %ptn_syms = (); |
| 291 |
|
| 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 |
| 295 |
# the .o file. |
| 296 |
$ptn_syms{$sym}++ if ($sym =~ /^$pattern$/); |
| 297 |
} |
| 298 |
|
| 299 |
foreach my $sym (sort keys(%ptn_syms)) { |
| 300 |
$matched_symbols++; |
| 301 |
print "$ws$sym;\n"; |
| 302 |
} |
| 303 |
} elsif ($glob eq 'cxx') { |
| 304 |
my %dem_syms = (); |
| 305 |
|
| 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"; |
| 310 |
|
| 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 $!; |
| 315 |
|
| 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 |
| 319 |
# patterns. |
| 320 |
printf FILTOUT "%s\n",$sym; |
| 321 |
my $dem = <FILTIN>; |
| 322 |
chomp $dem; |
| 323 |
$dem_syms{$sym}++ if ($dem =~ /^$pattern$/); |
| 324 |
} |
| 325 |
|
| 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. |
| 329 |
waitpid $pid, 0; |
| 330 |
|
| 331 |
foreach my $sym (sort keys(%dem_syms)) { |
| 332 |
$matched_symbols++; |
| 333 |
print "$ws$sym;\n"; |
| 334 |
} |
| 335 |
} else { |
| 336 |
# No? Well, then ignore it. |
| 337 |
} |
| 338 |
next; |
| 339 |
} |
| 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 `$_'"; |
| 343 |
} |
| 344 |
close F; |