Create a rudimentary symbol scanning script
[openssl.git] / util / checkplatformsyms.pl
1 #! /usr/bin/env perl
2 # Copyright 2006-2023 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
9 use warnings;
10 use strict;
11 use Config;
12
13 my $expectedsyms=$ARGV[0];
14
15 shift(@ARGV);
16
17 my $objlist;
18 my $objfilelist = join(" ", @ARGV);
19 my $expsyms;
20 my $exps;
21 my $OBJFH;
22 my $cmd;
23
24 if ($Config{osname} eq "MSWin32") {
25         my $currentdll = "";
26         $cmd = "dumpbin /imports " . $objfilelist;
27         my @symlist;
28         open $expsyms, '<', $expectedsyms or die;
29         {
30             local $/;
31             $exps=<$expsyms>;
32         }
33         close($expsyms);
34         open($OBJFH, "$cmd|") or die "Cannot open process: $!";
35         while (<$OBJFH>)
36         {
37             chomp;
38             my $dllfile = $_;
39             $dllfile =~ s/( +)(.*)(\.dll)(.*)/DLLFILE \2/;
40             if (index($dllfile, "DLLFILE") >= 0) {
41                 $currentdll = substr($dllfile, 8);
42                 $currentdll =~ s/^\s+|s+$//g;
43             }
44             # filter imports from our own library
45             if ("$currentdll" ne "libcrypto-3-x64") {
46                 my $line = $_;
47                 $line =~ s/                          [0-9a-fA-F]{1,2} /SYMBOL /;
48                 if (index($line, "SYMBOL") != -1) {
49                     $line =~ s/.*SYMBOL //;
50                     push(@symlist, $line);
51                 }
52             }
53         }
54         foreach (@symlist) {
55             if (index($exps, $_) < 0) {
56                 print "Symbol $_ not in the allowed platform symbols list\n";
57                 exit 1;
58             }
59         }
60         exit 0;
61     }
62 else {
63         $cmd = "objdump -t " . $objfilelist . " | grep UND | grep -v \@OPENSSL";
64         $cmd = $cmd . " | awk '{print \$NF}' |";
65         $cmd = $cmd . " sed -e\"s/@.*\$//\" | sort | uniq";
66
67         open $expsyms, '<', $expectedsyms or die;
68         {
69             local $/;
70             $exps=<$expsyms>;
71         }
72         close($expsyms);
73
74         open($OBJFH, "$cmd|") or die "Cannot open process: $!";
75         while (<$OBJFH>)
76         {
77                 if (index($exps, $_) < 0) {
78                     print "Symbol $_ not in the allowed platform symbols list\n";
79                     exit 1;
80                 }
81         }
82         close($OBJFH);
83         exit 0;
84     }