forked from open-mpi/ompi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol-hiding.pl
executable file
·154 lines (139 loc) · 4.01 KB
/
symbol-hiding.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/env perl
#
# Copyright (c) 2010-2014 Cisco Systems, Inc. All rights reserved.
# Copyright (c) 2015 Intel, Inc. All rights reserved.
# $COPYRIGHT$
use strict;
use Getopt::Long;
# globals
my $myfile;
my $mylib;
my $myprefix;
my $mysuffix;
my $mycapprefix;
# Set to true if the script should merely check for symbols in
# the library that are not in the provided output file - useful
# for determining if something has changed prior to doing an update
my $CHECK_ONLY = 0;
# Set to true to suppress most informational messages. Only missing
# symbols will be printed.
my $QUIET = 0;
# Set to true if we just want to see the help message
my $HELP = 0;
# Set to true if we want to reverse the hiding direction
my $REVERSE = 0;
GetOptions(
"help" => \$HELP,
"quiet" => \$QUIET,
"check-only" => \$CHECK_ONLY,
"prefix=s" => \$myprefix,
"suffix=s" => \$mysuffix,
"lib=s" => \$mylib,
"file=s" => \$myfile,
"reverse" => \$REVERSE,
) or die "unable to parse options, stopped";
if ($HELP) {
print <<EOT;
$0 [options]
--help | -h This help message
--quiet | -q Only output critical messages to stdout
--check-only Output the symbols we would have prefixed, but don't do anything
--prefix=NAME Add NAME to the front of all found symbols
--suffix=NAME Add NAME to the end of all found symbols
--lib=NAME Library containing symbols that are to be "hidden"
--file=NAME Output file for results, or existing file to be updated
--reverse Reverse the direction of hiding (i.e., #define prefix_foo to be foo)
EOT
exit(0);
}
#-------------------------------------------------------------------------------
# predeclare sub for print-like syntax
sub quiet_print {
unless ($QUIET) {
print @_;
}
}
#-------------------------------------------------------------------------------
$mycapprefix = uc $myprefix;
# get the symbol output for this lib
my $output = qx(nm $mylib);
# cycle across each line and tokenize it
my @symbols;
my $len = 0;
foreach my $line (split /[\r\n]+/, $output) {
quiet_print "$line\n";
my @values = split(' ',$line);
my $val = shift(@values);
# if the first character isn't a number, then
# we can ignore the line
my $str = substr($val,0,1);
if ("0" ne $str) {
quiet_print "skipping\n";
next;
}
# this is a line of interest - see if the
# next token indicates a public symbol by
# being a 'T' or a 'B'
$val = shift(@values);
if ("T" eq $val || "B" eq $val || "D" eq $val) {
$val = shift(@values);
# if this symbol contains a '.', then we
# need to ignore it
if (index($val, ".") != -1) {
quiet_print "skipping $val\n";
next;
}
quiet_print "GOT: " . $val . "\n";
push @symbols, $val;
if ($len < length($val)) {
$len = length($val);
}
}
}
$len = $len + 5;
if ($myfile ne "") {
open FILE, ">$myfile" || die "file could not be opened";
}
sub checkCase {
if ($_[0] =~ /^[[:upper:]]/) {
return 1;
}
else {
return 0;
}
}
foreach my $sym (@symbols) {
my $out;
if ($REVERSE) {
# if the first char is a cap, then use the cap prefix
if (checkCase($sym)) {
$out = "#define " . $mycapprefix . $sym . $mysuffix;
} else {
$out = "#define " . $myprefix . $sym . $mysuffix;
}
} else {
$out = "#define " . $sym;
}
my $diff = $len - length($sym);
for (my $i=0; $i < $diff; $i++) {
$out = $out . " ";
}
if ($REVERSE) {
$out = $out . $sym . "\n";
} else {
# if the first char is a cap, then use the cap prefix
if (checkCase($sym)) {
$out = $out . $mycapprefix . $sym . $mysuffix . "\n";
} else {
$out = $out . $myprefix . $sym . $mysuffix . "\n";
}
}
if ($myfile ne "") {
print FILE $out;
} else {
print $out;
}
}
if ($myfile ne "") {
close FILE;
}