make_sunver.pl 10 KB

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