Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/bin/symbols-check.py
4545 views
1
#!/usr/bin/env python
2
3
import argparse
4
import os
5
import platform
6
import subprocess
7
8
# This list contains symbols that _might_ be exported for some platforms
9
PLATFORM_SYMBOLS = [
10
'__bss_end__',
11
'__bss_start__',
12
'__bss_start',
13
'__cxa_guard_abort',
14
'__cxa_guard_acquire',
15
'__cxa_guard_release',
16
'__end__',
17
'__odr_asan._glapi_Context',
18
'__odr_asan._glapi_Dispatch',
19
'_bss_end__',
20
'_edata',
21
'_end',
22
'_fini',
23
'_init',
24
]
25
26
def get_symbols_nm(nm, lib):
27
'''
28
List all the (non platform-specific) symbols exported by the library
29
using `nm`
30
'''
31
symbols = []
32
platform_name = platform.system()
33
output = subprocess.check_output([nm, '-gP', lib],
34
stderr=open(os.devnull, 'w')).decode("ascii")
35
for line in output.splitlines():
36
fields = line.split()
37
if len(fields) == 2 or fields[1] == 'U':
38
continue
39
symbol_name = fields[0]
40
if platform_name == 'Linux':
41
if symbol_name in PLATFORM_SYMBOLS:
42
continue
43
elif platform_name == 'Darwin':
44
assert symbol_name[0] == '_'
45
symbol_name = symbol_name[1:]
46
symbols.append(symbol_name)
47
return symbols
48
49
50
def get_symbols_dumpbin(dumpbin, lib):
51
'''
52
List all the (non platform-specific) symbols exported by the library
53
using `dumpbin`
54
'''
55
symbols = []
56
output = subprocess.check_output([dumpbin, '/exports', lib],
57
stderr=open(os.devnull, 'w')).decode("ascii")
58
for line in output.splitlines():
59
fields = line.split()
60
# The lines with the symbols are made of at least 4 columns; see details below
61
if len(fields) < 4:
62
continue
63
try:
64
# Making sure the first 3 columns are a dec counter, a hex counter
65
# and a hex address
66
_ = int(fields[0], 10)
67
_ = int(fields[1], 16)
68
_ = int(fields[2], 16)
69
except ValueError:
70
continue
71
symbol_name = fields[3]
72
# De-mangle symbols
73
if symbol_name[0] == '_':
74
symbol_name = symbol_name[1:].split('@')[0]
75
symbols.append(symbol_name)
76
return symbols
77
78
79
def main():
80
parser = argparse.ArgumentParser()
81
parser.add_argument('--symbols-file',
82
action='store',
83
required=True,
84
help='path to file containing symbols')
85
parser.add_argument('--lib',
86
action='store',
87
required=True,
88
help='path to library')
89
parser.add_argument('--nm',
90
action='store',
91
help='path to binary (or name in $PATH)')
92
parser.add_argument('--dumpbin',
93
action='store',
94
help='path to binary (or name in $PATH)')
95
parser.add_argument('--ignore-symbol',
96
action='append',
97
help='do not process this symbol')
98
args = parser.parse_args()
99
100
try:
101
if platform.system() == 'Windows':
102
if not args.dumpbin:
103
parser.error('--dumpbin is mandatory')
104
lib_symbols = get_symbols_dumpbin(args.dumpbin, args.lib)
105
else:
106
if not args.nm:
107
parser.error('--nm is mandatory')
108
lib_symbols = get_symbols_nm(args.nm, args.lib)
109
except:
110
# We can't run this test, but we haven't technically failed it either
111
# Return the GNU "skip" error code
112
exit(77)
113
mandatory_symbols = []
114
optional_symbols = []
115
with open(args.symbols_file) as symbols_file:
116
qualifier_optional = '(optional)'
117
for line in symbols_file.readlines():
118
119
# Strip comments
120
line = line.split('#')[0]
121
line = line.strip()
122
if not line:
123
continue
124
125
# Line format:
126
# [qualifier] symbol
127
qualifier = None
128
symbol = None
129
130
fields = line.split()
131
if len(fields) == 1:
132
symbol = fields[0]
133
elif len(fields) == 2:
134
qualifier = fields[0]
135
symbol = fields[1]
136
else:
137
print(args.symbols_file + ': invalid format: ' + line)
138
exit(1)
139
140
# The only supported qualifier is 'optional', which means the
141
# symbol doesn't have to be exported by the library
142
if qualifier and not qualifier == qualifier_optional:
143
print(args.symbols_file + ': invalid qualifier: ' + qualifier)
144
exit(1)
145
146
if qualifier == qualifier_optional:
147
optional_symbols.append(symbol)
148
else:
149
mandatory_symbols.append(symbol)
150
151
unknown_symbols = []
152
for symbol in lib_symbols:
153
if symbol in mandatory_symbols:
154
continue
155
if symbol in optional_symbols:
156
continue
157
if args.ignore_symbol and symbol in args.ignore_symbol:
158
continue
159
if symbol[:2] == '_Z':
160
# As ajax found out, the compiler intentionally exports symbols
161
# that we explicitely asked it not to export, and we can't do
162
# anything about it:
163
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36022#c4
164
continue
165
unknown_symbols.append(symbol)
166
167
missing_symbols = [
168
sym for sym in mandatory_symbols if sym not in lib_symbols
169
]
170
171
for symbol in unknown_symbols:
172
print(args.lib + ': unknown symbol exported: ' + symbol)
173
174
for symbol in missing_symbols:
175
print(args.lib + ': missing symbol: ' + symbol)
176
177
if unknown_symbols or missing_symbols:
178
exit(1)
179
exit(0)
180
181
182
if __name__ == '__main__':
183
main()
184
185