26 lines
1.1 KiB
Python
26 lines
1.1 KiB
Python
"""Check arm64 ELF LOAD segments in an AAR/APK for 16 KiB alignment."""
|
|
import struct
|
|
import sys
|
|
import zipfile
|
|
from pathlib import Path
|
|
path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(__file__).resolve().parents[1] / "app/libs/sherpa-onnx-2.0.1.aar"
|
|
failed = False
|
|
count = 0
|
|
with zipfile.ZipFile(path) as archive:
|
|
for name in archive.namelist():
|
|
if "arm64-v8a/" not in name or not name.endswith(".so"):
|
|
continue
|
|
count += 1
|
|
data = archive.read(name)
|
|
if data[:6] != b"\x7fELF\x02\x01":
|
|
raise SystemExit(f"Unsupported ELF: {name}")
|
|
start = struct.unpack_from("<Q", data, 32)[0]
|
|
size, number = struct.unpack_from("<HH", data, 54)
|
|
loads = [struct.unpack_from("<IIQQQQQQ", data, start+i*size) for i in range(number) if struct.unpack_from("<I",data,start+i*size)[0] == 1]
|
|
valid = bool(loads) and all(p[7] >= 16384 and p[2] % 16384 == p[3] % 16384 for p in loads)
|
|
print(f"{'PASS' if valid else 'FAIL'} {name}")
|
|
failed |= not valid
|
|
if not count:
|
|
raise SystemExit("No arm64 native libraries found")
|
|
raise SystemExit(1 if failed else 0)
|