2
0

mknum.pl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #! /usr/bin/env perl
  2. # Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. use strict;
  9. use warnings;
  10. use Getopt::Long;
  11. use FindBin;
  12. use lib "$FindBin::Bin/perl";
  13. use OpenSSL::Ordinals;
  14. use OpenSSL::ParseC;
  15. my $ordinals_file = undef; # the ordinals file to use
  16. my $symhacks_file = undef; # a symbol hacking file (optional)
  17. my $version = undef; # the version to use for added symbols
  18. my $checkexist = 0; # (unsure yet)
  19. my $warnings = 1;
  20. my $renumber = 0;
  21. my $verbose = 0;
  22. my $debug = 0;
  23. GetOptions('ordinals=s' => \$ordinals_file,
  24. 'symhacks=s' => \$symhacks_file,
  25. 'version=s' => \$version,
  26. 'exist' => \$checkexist,
  27. 'renumber' => \$renumber,
  28. 'warnings!' => \$warnings,
  29. 'verbose' => \$verbose,
  30. 'debug' => \$debug)
  31. or die "Error in command line arguments\n";
  32. die "Please supply ordinals file\n"
  33. unless $ordinals_file;
  34. my $ordinals = OpenSSL::Ordinals->new(from => $ordinals_file,
  35. warnings => $warnings,
  36. verbose => $verbose,
  37. debug => $debug);
  38. $ordinals->set_version($version);
  39. my %orig_names = ();
  40. %orig_names = map { $_->name() => 1 }
  41. $ordinals->items(comparator => sub { $_[0] cmp $_[1] },
  42. filter => sub { $_->exists() })
  43. if $checkexist;
  44. # Invalidate all entries, they get revalidated when we re-check below
  45. $ordinals->invalidate();
  46. foreach my $f (($symhacks_file // (), @ARGV)) {
  47. print STDERR $f," ","-" x (69 - length($f)),"\n" if $verbose;
  48. open IN, $f or die "Couldn't open $f: $!\n";
  49. foreach (parse(<IN>, { filename => $f,
  50. warnings => $warnings,
  51. verbose => $verbose,
  52. debug => $debug })) {
  53. $_->{value} = $_->{value}||"";
  54. next if grep { $_ eq 'CONST_STRICT' } @{$_->{conds}};
  55. printf STDERR "%s> %s%s : %s\n",
  56. $_->{type},
  57. $_->{name},
  58. ($_->{type} eq 'M' && defined $symhacks_file && $f eq $symhacks_file
  59. ? ' = ' . $_->{value}
  60. : ''),
  61. join(', ', @{$_->{conds}})
  62. if $verbose;
  63. if ($_->{type} eq 'M'
  64. && defined $symhacks_file
  65. && $f eq $symhacks_file
  66. && $_->{value} =~ /^\w(?:\w|\d)*/) {
  67. $ordinals->add_alias($f, $_->{value}, $_->{name}, @{$_->{conds}});
  68. } else {
  69. next if $_->{returntype} =~ /\b(?:ossl_)inline/;
  70. my $type = {
  71. F => 'FUNCTION',
  72. V => 'VARIABLE',
  73. } -> {$_->{type}};
  74. if ($type) {
  75. $ordinals->add($f, $_->{name}, $type, @{$_->{conds}});
  76. }
  77. }
  78. }
  79. close IN;
  80. }
  81. $ordinals->renumber() if $renumber;
  82. if ($checkexist) {
  83. my %new_names = map { $_->name() => 1 }
  84. $ordinals->items(comparator => sub { $_[0] cmp $_[1] },
  85. filter => sub { $_->exists() });
  86. # Eliminate common names
  87. foreach (keys %orig_names) {
  88. next unless exists $new_names{$_};
  89. delete $orig_names{$_};
  90. delete $new_names{$_};
  91. }
  92. if (%orig_names) {
  93. print "The following symbols do not seem to exist in code:\n";
  94. foreach (sort keys %orig_names) {
  95. print "\t$_\n";
  96. }
  97. }
  98. if (%new_names) {
  99. print "The following existing symbols are not in ordinals file:\n";
  100. foreach (sort keys %new_names) {
  101. print "\t$_\n";
  102. }
  103. }
  104. } else {
  105. my $dropped = 0;
  106. my $unassigned;
  107. my $filter = sub {
  108. my $item = shift;
  109. my $result = $item->number() ne '?' || $item->exists();
  110. $dropped++ unless $result;
  111. return $result;
  112. };
  113. $ordinals->rewrite(filter => $filter);
  114. my %stats = $ordinals->stats();
  115. print STDERR
  116. "${ordinals_file}: $stats{modified} old symbols have updated info\n"
  117. if $stats{modified};
  118. if ($stats{new}) {
  119. print STDERR "${ordinals_file}: Added $stats{new} new symbols\n";
  120. } else {
  121. print STDERR "${ordinals_file}: No new symbols added\n";
  122. }
  123. if ($dropped) {
  124. print STDERR "${ordinals_file}: Dropped $dropped new symbols\n";
  125. }
  126. $stats{unassigned} = 0 unless defined $stats{unassigned};
  127. $unassigned = $stats{unassigned} - $dropped;
  128. if ($unassigned) {
  129. my $symbol = $unassigned == 1 ? "symbol" : "symbols";
  130. my $is = $unassigned == 1 ? "is" : "are";
  131. print STDERR "${ordinals_file}: $unassigned $symbol $is without ordinal number\n";
  132. }
  133. }