Skip to content

Commit 8242ae2

Browse files
committed
scripts: only parse the binary once in symbol-check.py
1 parent 309eac9 commit 8242ae2

File tree

1 file changed

+15
-33
lines changed

1 file changed

+15
-33
lines changed

contrib/devtools/symbol-check.py

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
find ../path/to/binaries -type f -executable | xargs python3 contrib/devtools/symbol-check.py
1212
'''
1313
import sys
14-
from typing import List, Optional
14+
from typing import List
1515

1616
import lief
1717

@@ -144,9 +144,8 @@ def check_version(max_versions, version, arch) -> bool:
144144
else:
145145
return ver <= max_versions[lib][arch]
146146

147-
def check_imported_symbols(filename) -> bool:
147+
def check_imported_symbols(binary) -> bool:
148148
ok: bool = True
149-
binary = lief.parse(filename)
150149

151150
for symbol in binary.imported_symbols:
152151
if not symbol.imported:
@@ -161,62 +160,55 @@ def check_imported_symbols(filename) -> bool:
161160
ok = False
162161
return ok
163162

164-
def check_exported_symbols(filename) -> bool:
163+
def check_exported_symbols(binary) -> bool:
165164
ok: bool = True
166-
binary = lief.parse(filename)
167165

168166
for symbol in binary.dynamic_symbols:
169167
if not symbol.exported:
170168
continue
171169
name = symbol.name
172170
if binary.header.machine_type == LIEF_ELF_ARCH_RISCV or name in IGNORE_EXPORTS:
173171
continue
174-
print(f'{filename}: export of symbol {name} not allowed!')
172+
print(f'{binary.name}: export of symbol {name} not allowed!')
175173
ok = False
176174
return ok
177175

178-
def check_ELF_libraries(filename) -> bool:
176+
def check_ELF_libraries(binary) -> bool:
179177
ok: bool = True
180-
binary = lief.parse(filename)
181178
for library in binary.libraries:
182179
if library not in ELF_ALLOWED_LIBRARIES:
183180
print(f'{filename}: {library} is not in ALLOWED_LIBRARIES!')
184181
ok = False
185182
return ok
186183

187-
def check_MACHO_libraries(filename) -> bool:
184+
def check_MACHO_libraries(binary) -> bool:
188185
ok: bool = True
189-
binary = lief.parse(filename)
190186
for dylib in binary.libraries:
191187
split = dylib.name.split('/')
192188
if split[-1] not in MACHO_ALLOWED_LIBRARIES:
193189
print(f'{split[-1]} is not in ALLOWED_LIBRARIES!')
194190
ok = False
195191
return ok
196192

197-
def check_MACHO_min_os(filename) -> bool:
198-
binary = lief.parse(filename)
193+
def check_MACHO_min_os(binary) -> bool:
199194
if binary.build_version.minos == [10,15,0]:
200195
return True
201196
return False
202197

203-
def check_MACHO_sdk(filename) -> bool:
204-
binary = lief.parse(filename)
198+
def check_MACHO_sdk(binary) -> bool:
205199
if binary.build_version.sdk == [10, 15, 6]:
206200
return True
207201
return False
208202

209-
def check_PE_libraries(filename) -> bool:
203+
def check_PE_libraries(binary) -> bool:
210204
ok: bool = True
211-
binary = lief.parse(filename)
212205
for dylib in binary.libraries:
213206
if dylib not in PE_ALLOWED_LIBRARIES:
214207
print(f'{dylib} is not in ALLOWED_LIBRARIES!')
215208
ok = False
216209
return ok
217210

218-
def check_PE_subsystem_version(filename) -> bool:
219-
binary = lief.parse(filename)
211+
def check_PE_subsystem_version(binary) -> bool:
220212
major: int = binary.optional_header.major_subsystem_version
221213
minor: int = binary.optional_header.minor_subsystem_version
222214
if major == 6 and minor == 1:
@@ -240,30 +232,20 @@ def check_PE_subsystem_version(filename) -> bool:
240232
]
241233
}
242234

243-
def identify_executable(executable) -> Optional[str]:
244-
with open(filename, 'rb') as f:
245-
magic = f.read(4)
246-
if magic.startswith(b'MZ'):
247-
return 'PE'
248-
elif magic.startswith(b'\x7fELF'):
249-
return 'ELF'
250-
elif magic.startswith(b'\xcf\xfa'):
251-
return 'MACHO'
252-
return None
253-
254235
if __name__ == '__main__':
255236
retval: int = 0
256237
for filename in sys.argv[1:]:
257238
try:
258-
etype = identify_executable(filename)
259-
if etype is None:
260-
print(f'{filename}: unknown format')
239+
binary = lief.parse(filename)
240+
etype = binary.format.name
241+
if etype == lief.EXE_FORMATS.UNKNOWN:
242+
print(f'{filename}: unknown executable format')
261243
retval = 1
262244
continue
263245

264246
failed: List[str] = []
265247
for (name, func) in CHECKS[etype]:
266-
if not func(filename):
248+
if not func(binary):
267249
failed.append(name)
268250
if failed:
269251
print(f'{filename}: failed {" ".join(failed)}')

0 commit comments

Comments
 (0)