Debian Codegraph @

bin/symbols-check.py

raw
1 #!/usr/bin/env python3
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 '_GLOBAL_OFFSET_TABLE_',
11 '_ITM_deregisterTMCloneTable',
12 '_ITM_registerTMCloneTable',
13 '__bss_end__',
14 '__bss_start',
15 '__bss_start__',
16 '__cxa_allocate_dependent_exception',
17 '__cxa_allocate_exception',
18 '__cxa_begin_catch',
19 '__cxa_call_unexpected',
20 '__cxa_current_exception_type',
21 '__cxa_current_primary_exception',
22 '__cxa_decrement_exception_refcount',
23 '__cxa_deleted_virtual',
24 '__cxa_demangle',
25 '__cxa_end_catch',
26 '__cxa_finalize',
27 '__cxa_free_dependent_exception',
28 '__cxa_free_exception',
29 '__cxa_get_exception_ptr',
30 '__cxa_get_globals',
31 '__cxa_get_globals_fast',
32 '__cxa_guard_abort',
33 '__cxa_guard_acquire',
34 '__cxa_guard_release',
35 '__cxa_increment_exception_refcount',
36 '__cxa_new_handler',
37 '__cxa_pure_virtual',
38 '__cxa_rethrow',
39 '__cxa_rethrow_primary_exception',
40 '__cxa_terminate_handler',
41 '__cxa_throw',
42 '__cxa_uncaught_exception',
43 '__cxa_uncaught_exceptions',
44 '__cxa_unexpected_handler',
45 '__deregister_frame_info',
46 '__deregister_frame_info_bases',
47 '__dynamic_cast',
48 '__emutls_get_address',
49 '__end__',
50 '__gmon_start__',
51 '__gxx_personality_v0',
52 '__odr_asan._glapi_Context',
53 '__odr_asan._glapi_Dispatch',
54 '__register_frame_info',
55 '__register_frame_info_bases',
56 '_bss_end__',
57 '_edata',
58 '_end',
59 '_fbss',
60 '_fdata',
61 '_fini',
62 '_ftext',
63 '_init',
64 'pthread_mutexattr_destroy',
65 'pthread_mutexattr_init',
66 'pthread_mutexattr_settype',
67 ]
68
69
70 def get_symbols_nm(nm, lib):
71 '''
72 List all the (non platform-specific) symbols exported by the library
73 using `nm`
74 '''
75 symbols = []
76 platform_name = platform.system()
77 output = subprocess.check_output([nm, '-gP', lib],
78 stderr=open(os.devnull, 'w')).decode("ascii")
79 for line in output.splitlines():
80 if line.startswith(' '):
81 continue
82 fields = line.split()
83 if len(fields) >= 2 and fields[1] == 'U':
84 continue
85 symbol_name = fields[0]
86 if platform_name == 'Linux' or platform_name == 'GNU' or platform_name.startswith('GNU/'):
87 if symbol_name.split('@')[0] in PLATFORM_SYMBOLS:
88 continue
89 elif platform_name == 'Darwin':
90 assert symbol_name[0] == '_'
91 symbol_name = symbol_name[1:]
92 symbols.append(symbol_name)
93 return symbols
94
95
96 def get_symbols_dumpbin(dumpbin, lib):
97 '''
98 List all the (non platform-specific) symbols exported by the library
99 using `dumpbin`
100 '''
101 symbols = []
102 output = subprocess.check_output([dumpbin, '/exports', lib],
103 stderr=open(os.devnull, 'w')).decode("ascii")
104 for line in output.splitlines():
105 fields = line.split()
106 # The lines with the symbols are made of at least 4 columns; see details below
107 if len(fields) < 4:
108 continue
109 try:
110 # Making sure the first 3 columns are a dec counter, a hex counter
111 # and a hex address
112 _ = int(fields[0], 10)
113 _ = int(fields[1], 16)
114 _ = int(fields[2], 16)
115 except ValueError:
116 continue
117 symbol_name = fields[3]
118 # De-mangle symbols
119 if symbol_name[0] == '_' and '@' in symbol_name:
120 symbol_name = symbol_name[1:].split('@')[0]
121 symbols.append(symbol_name)
122 return symbols
123
124 def get_symbols_gendef(gendef, lib):
125 '''
126 List all the (non platform-specific) symbols exported by the library
127 using `gendef -`
128 '''
129 symbols = []
130 output = subprocess.check_output([gendef, '-', lib],
131 stderr=open(os.devnull, 'w')).decode("ascii")
132 ordinal_table_found = False
133 for line in output.splitlines():
134 if not ordinal_table_found:
135 if line.strip() == 'EXPORTS':
136 ordinal_table_found = True
137 continue
138
139 symbols.append(line.strip())
140 return symbols
141
142 def main():
143 parser = argparse.ArgumentParser()
144 parser.add_argument('--symbols-file',
145 action='store',
146 required=True,
147 help='path to file containing symbols')
148 parser.add_argument('--lib',
149 action='store',
150 required=True,
151 help='path to library')
152 parser.add_argument('--nm',
153 action='store',
154 help='path to binary (or name in $PATH)')
155 parser.add_argument('--dumpbin',
156 action='store',
157 help='path to binary (or name in $PATH)')
158 parser.add_argument('--gendef',
159 action='store',
160 help='path to binary (or name in $PATH)')
161 parser.add_argument('--ignore-symbol',
162 action='append',
163 help='do not process this symbol')
164 args = parser.parse_args()
165
166 if platform.system() == 'Windows':
167 if args.dumpbin:
168 lib_symbols = get_symbols_dumpbin(args.dumpbin, args.lib)
169 elif args.gendef:
170 lib_symbols = get_symbols_gendef(args.gendef, args.lib)
171 else:
172 parser.error('--dumpbin is mandatory for msvc, --gendef is mandatory for mingw')
173 else:
174 if not args.nm:
175 parser.error('--nm is mandatory')
176 lib_symbols = get_symbols_nm(args.nm, args.lib)
177
178 mandatory_symbols = []
179 optional_symbols = []
180 with open(args.symbols_file) as symbols_file:
181 qualifier_optional = '(optional)'
182 for line in symbols_file.readlines():
183
184 # Strip comments
185 line = line.split('#')[0]
186 line = line.strip()
187 if not line:
188 continue
189
190 # Line format:
191 # [qualifier] symbol
192 qualifier = None
193 symbol = None
194
195 fields = line.split()
196 if len(fields) == 1:
197 symbol = fields[0]
198 elif len(fields) == 2:
199 qualifier = fields[0]
200 symbol = fields[1]
201 else:
202 print(args.symbols_file + ': invalid format: ' + line)
203 exit(1)
204
205 # The only supported qualifier is 'optional', which means the
206 # symbol doesn't have to be exported by the library
207 if qualifier and not qualifier == qualifier_optional:
208 print(args.symbols_file + ': invalid qualifier: ' + qualifier)
209 exit(1)
210
211 if qualifier == qualifier_optional:
212 optional_symbols.append(symbol)
213 else:
214 mandatory_symbols.append(symbol)
215
216 unknown_symbols = []
217 for symbol in lib_symbols:
218 if symbol in mandatory_symbols:
219 continue
220 if symbol in optional_symbols:
221 continue
222 if args.ignore_symbol and symbol in args.ignore_symbol:
223 continue
224 if symbol[:2] == '_Z':
225 # As ajax found out, the compiler intentionally exports symbols
226 # that we explicitly asked it not to export, and we can't do
227 # anything about it:
228 # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36022#c4
229 continue
230 unknown_symbols.append(symbol)
231
232 missing_symbols = [
233 sym for sym in mandatory_symbols if sym not in lib_symbols
234 ]
235
236 for symbol in unknown_symbols:
237 print(args.lib + ': unknown symbol exported: ' + symbol)
238
239 for symbol in missing_symbols:
240 print(args.lib + ': missing symbol: ' + symbol)
241
242 if unknown_symbols or missing_symbols:
243 exit(1)
244 exit(0)
245
246
247 if __name__ == '__main__':
248 main()