diff options
author | fanquake <fanquake@gmail.com> | 2021-07-02 14:37:06 +0800 |
---|---|---|
committer | fanquake <fanquake@gmail.com> | 2021-10-12 08:36:21 +0800 |
commit | 8242ae230e1ff362a4e16f1503e503317471c790 (patch) | |
tree | 7602eefa448f1ee81020d5727c7710ef1ead624a | |
parent | 309eac9019c224dfd79a78e381cfcb70fee190f3 (diff) | |
download | bitcoin-8242ae230e1ff362a4e16f1503e503317471c790.tar.xz |
scripts: only parse the binary once in symbol-check.py
-rwxr-xr-x | contrib/devtools/symbol-check.py | 48 |
1 files changed, 15 insertions, 33 deletions
diff --git a/contrib/devtools/symbol-check.py b/contrib/devtools/symbol-check.py index 36ac6faa81..c8703c53ba 100755 --- a/contrib/devtools/symbol-check.py +++ b/contrib/devtools/symbol-check.py @@ -11,7 +11,7 @@ Example usage: find ../path/to/binaries -type f -executable | xargs python3 contrib/devtools/symbol-check.py ''' import sys -from typing import List, Optional +from typing import List import lief @@ -144,9 +144,8 @@ def check_version(max_versions, version, arch) -> bool: else: return ver <= max_versions[lib][arch] -def check_imported_symbols(filename) -> bool: +def check_imported_symbols(binary) -> bool: ok: bool = True - binary = lief.parse(filename) for symbol in binary.imported_symbols: if not symbol.imported: @@ -161,9 +160,8 @@ def check_imported_symbols(filename) -> bool: ok = False return ok -def check_exported_symbols(filename) -> bool: +def check_exported_symbols(binary) -> bool: ok: bool = True - binary = lief.parse(filename) for symbol in binary.dynamic_symbols: if not symbol.exported: @@ -171,22 +169,20 @@ def check_exported_symbols(filename) -> bool: name = symbol.name if binary.header.machine_type == LIEF_ELF_ARCH_RISCV or name in IGNORE_EXPORTS: continue - print(f'{filename}: export of symbol {name} not allowed!') + print(f'{binary.name}: export of symbol {name} not allowed!') ok = False return ok -def check_ELF_libraries(filename) -> bool: +def check_ELF_libraries(binary) -> bool: ok: bool = True - binary = lief.parse(filename) for library in binary.libraries: if library not in ELF_ALLOWED_LIBRARIES: print(f'{filename}: {library} is not in ALLOWED_LIBRARIES!') ok = False return ok -def check_MACHO_libraries(filename) -> bool: +def check_MACHO_libraries(binary) -> bool: ok: bool = True - binary = lief.parse(filename) for dylib in binary.libraries: split = dylib.name.split('/') if split[-1] not in MACHO_ALLOWED_LIBRARIES: @@ -194,29 +190,25 @@ def check_MACHO_libraries(filename) -> bool: ok = False return ok -def check_MACHO_min_os(filename) -> bool: - binary = lief.parse(filename) +def check_MACHO_min_os(binary) -> bool: if binary.build_version.minos == [10,15,0]: return True return False -def check_MACHO_sdk(filename) -> bool: - binary = lief.parse(filename) +def check_MACHO_sdk(binary) -> bool: if binary.build_version.sdk == [10, 15, 6]: return True return False -def check_PE_libraries(filename) -> bool: +def check_PE_libraries(binary) -> bool: ok: bool = True - binary = lief.parse(filename) for dylib in binary.libraries: if dylib not in PE_ALLOWED_LIBRARIES: print(f'{dylib} is not in ALLOWED_LIBRARIES!') ok = False return ok -def check_PE_subsystem_version(filename) -> bool: - binary = lief.parse(filename) +def check_PE_subsystem_version(binary) -> bool: major: int = binary.optional_header.major_subsystem_version minor: int = binary.optional_header.minor_subsystem_version if major == 6 and minor == 1: @@ -240,30 +232,20 @@ CHECKS = { ] } -def identify_executable(executable) -> Optional[str]: - with open(filename, 'rb') as f: - magic = f.read(4) - if magic.startswith(b'MZ'): - return 'PE' - elif magic.startswith(b'\x7fELF'): - return 'ELF' - elif magic.startswith(b'\xcf\xfa'): - return 'MACHO' - return None - if __name__ == '__main__': retval: int = 0 for filename in sys.argv[1:]: try: - etype = identify_executable(filename) - if etype is None: - print(f'{filename}: unknown format') + binary = lief.parse(filename) + etype = binary.format.name + if etype == lief.EXE_FORMATS.UNKNOWN: + print(f'{filename}: unknown executable format') retval = 1 continue failed: List[str] = [] for (name, func) in CHECKS[etype]: - if not func(filename): + if not func(binary): failed.append(name) if failed: print(f'{filename}: failed {" ".join(failed)}') |