make_sunver.pl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. #!/usr/bin/perl -w
  2. # make_sunver.pl
  3. #
  4. # This script takes at least two arguments, a GNU style version script and
  5. # a list of object and archive files, and generates a corresponding Sun
  6. # style version script as follows:
  7. #
  8. # Each glob pattern, C++ mangled pattern or literal in the input script is
  9. # matched against all global symbols in the input objects, emitting those
  10. # that matched (or nothing if no match was found).
  11. # A comment with the original pattern and its type is left in the output
  12. # file to make it easy to understand the matches.
  13. #
  14. # It uses elfdump when present (native), GNU readelf otherwise.
  15. # It depends on the GNU version of c++filt, since it must understand the
  16. # GNU mangling style.
  17. use FileHandle;
  18. use IPC::Open2;
  19. # Enforce C locale.
  20. $ENV{'LC_ALL'} = "C";
  21. $ENV{'LANG'} = "C";
  22. # Input version script, GNU style.
  23. my $symvers = shift;
  24. ##########
  25. # Get all the symbols from the library, match them, and add them to a hash.
  26. my %sym_hash = ();
  27. # List of objects and archives to process.
  28. my @OBJECTS = ();
  29. # List of shared objects to omit from processing.
  30. my @SHAREDOBJS = ();
  31. # Filter out those input archives that have corresponding shared objects to
  32. # avoid adding all symbols matched in the archive to the output map.
  33. foreach $file (@ARGV) {
  34. if (($so = $file) =~ s/\.a$/.so/ && -e $so) {
  35. printf STDERR "omitted $file -> $so\n";
  36. push (@SHAREDOBJS, $so);
  37. } else {
  38. push (@OBJECTS, $file);
  39. }
  40. }
  41. # We need to detect and ignore hidden symbols. Solaris nm can only detect
  42. # this in the harder to parse default output format, and GNU nm not at all,
  43. # so use elfdump -s in the native case and GNU readelf -s otherwise.
  44. # GNU objdump -t cannot be used since it produces a variable number of
  45. # columns.
  46. # The path to elfdump.
  47. my $elfdump = "/usr/ccs/bin/elfdump";
  48. if (-f $elfdump) {
  49. open ELFDUMP,$elfdump.' -s '.(join ' ',@OBJECTS).'|' or die $!;
  50. my $skip_arsym = 0;
  51. while (<ELFDUMP>) {
  52. chomp;
  53. # Ignore empty lines.
  54. if (/^$/) {
  55. # End of archive symbol table, stop skipping.
  56. $skip_arsym = 0 if $skip_arsym;
  57. next;
  58. }
  59. # Keep skipping until end of archive symbol table.
  60. next if ($skip_arsym);
  61. # Ignore object name header for individual objects and archives.
  62. next if (/:$/);
  63. # Ignore table header lines.
  64. next if (/^Symbol Table Section:/);
  65. next if (/index.*value.*size/);
  66. # Start of archive symbol table: start skipping.
  67. if (/^Symbol Table: \(archive/) {
  68. $skip_arsym = 1;
  69. next;
  70. }
  71. # Split table.
  72. (undef, undef, undef, undef, $bind, $oth, undef, $shndx, $name) = split;
  73. # Error out for unknown input.
  74. die "unknown input line:\n$_" unless defined($bind);
  75. # Ignore local symbols.
  76. next if ($bind eq "LOCL");
  77. # Ignore hidden symbols.
  78. next if ($oth eq "H");
  79. # Ignore undefined symbols.
  80. next if ($shndx eq "UNDEF");
  81. # Error out for unhandled cases.
  82. if ($bind !~ /^(GLOB|WEAK)/ or $oth ne "D") {
  83. die "unhandled symbol:\n$_";
  84. }
  85. # Remember symbol.
  86. $sym_hash{$name}++;
  87. }
  88. close ELFDUMP or die "$elfdump error";
  89. } else {
  90. open READELF, 'readelf -s -W '.(join ' ',@OBJECTS).'|' or die $!;
  91. # Process each symbol.
  92. while (<READELF>) {
  93. chomp;
  94. # Ignore empty lines.
  95. next if (/^$/);
  96. # Ignore object name header.
  97. next if (/^File: .*$/);
  98. # Ignore table header lines.
  99. next if (/^Symbol table.*contains.*:/);
  100. next if (/Num:.*Value.*Size/);
  101. # Split table.
  102. (undef, undef, undef, undef, $bind, $vis, $ndx, $name) = split;
  103. # Error out for unknown input.
  104. die "unknown input line:\n$_" unless defined($bind);
  105. # Ignore local symbols.
  106. next if ($bind eq "LOCAL");
  107. # Ignore hidden symbols.
  108. next if ($vis eq "HIDDEN");
  109. # Ignore undefined symbols.
  110. next if ($ndx eq "UND");
  111. # Error out for unhandled cases.
  112. if ($bind !~ /^(GLOBAL|WEAK)/ or $vis ne "DEFAULT") {
  113. die "unhandled symbol:\n$_";
  114. }
  115. # Remember symbol.
  116. $sym_hash{$name}++;
  117. }
  118. close READELF or die "readelf error";
  119. }
  120. ##########
  121. # The various types of glob patterns.
  122. #
  123. # A glob pattern that is to be applied to the demangled name: 'cxx'.
  124. # A glob patterns that applies directly to the name in the .o files: 'glob'.
  125. # This pattern is ignored; used for local variables (usually just '*'): 'ign'.
  126. # The type of the current pattern.
  127. my $glob = 'glob';
  128. # We're currently inside `extern "C++"', which Sun ld doesn't understand.
  129. my $in_extern = 0;
  130. # The c++filt command to use. This *must* be GNU c++filt; the Sun Studio
  131. # c++filt doesn't handle the GNU mangling style.
  132. my $cxxfilt = $ENV{'CXXFILT'} || "c++filt";
  133. # The current version name.
  134. my $current_version = "";
  135. # Was there any attempt to match a symbol to this version?
  136. my $matches_attempted;
  137. # The number of versions which matched this symbol.
  138. my $matched_symbols;
  139. open F,$symvers or die $!;
  140. # Print information about generating this file
  141. print "# This file was generated by make_sunver.pl. DO NOT EDIT!\n";
  142. print "# It was generated by:\n";
  143. printf "# %s %s %s\n", $0, $symvers, (join ' ',@ARGV);
  144. printf "# Omitted archives with corresponding shared libraries: %s\n",
  145. (join ' ', @SHAREDOBJS) if $#SHAREDOBJS >= 0;
  146. print "#\n\n";
  147. while (<F>) {
  148. # Lines of the form '};'
  149. if (/^([ \t]*)(\}[ \t]*;[ \t]*)$/) {
  150. $glob = 'glob';
  151. if ($in_extern) {
  152. $in_extern--;
  153. print "$1##$2\n";
  154. } else {
  155. print;
  156. }
  157. next;
  158. }
  159. # Lines of the form '} SOME_VERSION_NAME_1.0;'
  160. if (/^[ \t]*\}[ \tA-Z0-9_.a-z]+;[ \t]*$/) {
  161. $glob = 'glob';
  162. # We tried to match symbols agains this version, but none matched.
  163. # Emit dummy hidden symbol to avoid marking this version WEAK.
  164. if ($matches_attempted && $matched_symbols == 0) {
  165. print " hidden:\n";
  166. print " .force_WEAK_off_$current_version = DATA S0x0 V0x0;\n";
  167. }
  168. print; next;
  169. }
  170. # Comment and blank lines
  171. if (/^[ \t]*\#/) { print; next; }
  172. if (/^[ \t]*$/) { print; next; }
  173. # Lines of the form '{'
  174. if (/^([ \t]*){$/) {
  175. if ($in_extern) {
  176. print "$1##{\n";
  177. } else {
  178. print;
  179. }
  180. next;
  181. }
  182. # Lines of the form 'SOME_VERSION_NAME_1.1 {'
  183. if (/^([A-Z0-9_.]+)[ \t]+{$/) {
  184. # Record version name.
  185. $current_version = $1;
  186. # Reset match attempts, #matched symbols for this version.
  187. $matches_attempted = 0;
  188. $matched_symbols = 0;
  189. print;
  190. next;
  191. }
  192. # Ignore 'global:'
  193. if (/^[ \t]*global:$/) { print; next; }
  194. # After 'local:', globs should be ignored, they won't be exported.
  195. if (/^[ \t]*local:$/) {
  196. $glob = 'ign';
  197. print;
  198. next;
  199. }
  200. # After 'extern "C++"', globs are C++ patterns
  201. if (/^([ \t]*)(extern \"C\+\+\"[ \t]*)$/) {
  202. $in_extern++;
  203. $glob = 'cxx';
  204. # Need to comment, Sun ld cannot handle this.
  205. print "$1##$2\n"; next;
  206. }
  207. # Chomp newline now we're done with passing through the input file.
  208. chomp;
  209. # Catch globs. Note that '{}' is not allowed in globs by this script,
  210. # so only '*' and '[]' are available.
  211. if (/^([ \t]*)([^ \t;{}#]+);?[ \t]*$/) {
  212. my $ws = $1;
  213. my $ptn = $2;
  214. # Turn the glob into a regex by replacing '*' with '.*', '?' with '.'.
  215. # Keep $ptn so we can still print the original form.
  216. ($pattern = $ptn) =~ s/\*/\.\*/g;
  217. $pattern =~ s/\?/\./g;
  218. if ($glob eq 'ign') {
  219. # We're in a local: * section; just continue.
  220. print "$_\n";
  221. next;
  222. }
  223. # Print the glob commented for human readers.
  224. print "$ws##$ptn ($glob)\n";
  225. # We tried to match a symbol to this version.
  226. $matches_attempted++;
  227. if ($glob eq 'glob') {
  228. my %ptn_syms = ();
  229. # Match ptn against symbols in %sym_hash.
  230. foreach my $sym (keys %sym_hash) {
  231. # Maybe it matches one of the patterns based on the symbol in
  232. # the .o file.
  233. $ptn_syms{$sym}++ if ($sym =~ /^$pattern$/);
  234. }
  235. foreach my $sym (sort keys(%ptn_syms)) {
  236. $matched_symbols++;
  237. print "$ws$sym;\n";
  238. }
  239. } elsif ($glob eq 'cxx') {
  240. my %dem_syms = ();
  241. # Verify that we're actually using GNU c++filt. Other versions
  242. # most likely cannot handle GNU style symbol mangling.
  243. my $cxxout = `$cxxfilt --version 2>&1`;
  244. $cxxout =~ m/GNU/ or die "$0 requires GNU c++filt to function";
  245. # Talk to c++filt through a pair of file descriptors.
  246. # Need to start a fresh instance per pattern, otherwise the
  247. # process grows to 500+ MB.
  248. my $pid = open2(*FILTIN, *FILTOUT, $cxxfilt) or die $!;
  249. # Match ptn against symbols in %sym_hash.
  250. foreach my $sym (keys %sym_hash) {
  251. # No? Well, maybe its demangled form matches one of those
  252. # patterns.
  253. printf FILTOUT "%s\n",$sym;
  254. my $dem = <FILTIN>;
  255. chomp $dem;
  256. $dem_syms{$sym}++ if ($dem =~ /^$pattern$/);
  257. }
  258. close FILTOUT or die "c++filt error";
  259. close FILTIN or die "c++filt error";
  260. # Need to wait for the c++filt process to avoid lots of zombies.
  261. waitpid $pid, 0;
  262. foreach my $sym (sort keys(%dem_syms)) {
  263. $matched_symbols++;
  264. print "$ws$sym;\n";
  265. }
  266. } else {
  267. # No? Well, then ignore it.
  268. }
  269. next;
  270. }
  271. # Important sanity check. This script can't handle lots of formats
  272. # that GNU ld can, so be sure to error out if one is seen!
  273. die "strange line `$_'";
  274. }
  275. close F;