Reduce Windows DLL dependencies and package release 0.2.1
This commit is contained in:
1 parent
fc0edd433f
commit
efaf16de18
17 files changed
+153
-21
No files matched your search
@@ -0,0 +1,82 @@
|
||||
"""Resolve only required app-local MSVC runtime DLLs, or fail if missing.
|
||||
Uses PE import and delay-import tables; no third-party Python modules required.
|
||||
"""
|
||||
import re
|
||||
import struct
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def imports(path):
|
||||
data = path.read_bytes()
|
||||
if data[:2] != b'MZ':
|
||||
raise ValueError(f'Not PE: {path}')
|
||||
pe = struct.unpack_from('<I', data, 60)[0]
|
||||
if data[pe:pe+4] != b'PE\0\0':
|
||||
raise ValueError(f'Invalid PE: {path}')
|
||||
count, optional_size = struct.unpack_from('<H12xH', data, pe+6)
|
||||
optional = pe+24
|
||||
plus = struct.unpack_from('<H', data, optional)[0] == 0x20b
|
||||
directories = optional + (112 if plus else 96)
|
||||
image_base = struct.unpack_from('<Q' if plus else '<I', data, optional+(24 if plus else 28))[0]
|
||||
sections = []
|
||||
for i in range(count):
|
||||
size, rva, raw_size, raw = struct.unpack_from('<IIII', data, optional+optional_size+i*40+8)
|
||||
sections.append((rva, max(size, raw_size), raw))
|
||||
def offset(rva):
|
||||
for base, size, raw in sections:
|
||||
if base <= rva < base+size:
|
||||
return raw+rva-base
|
||||
raise ValueError(f'Unmapped RVA in {path}')
|
||||
result = []
|
||||
for index, stride in ((1, 20), (13, 32)):
|
||||
rva, size = struct.unpack_from('<II', data, directories+index*8)
|
||||
if not rva:
|
||||
continue
|
||||
start = offset(rva)
|
||||
for at in range(start, start+size, stride):
|
||||
row = data[at:at+stride]
|
||||
if not any(row):
|
||||
break
|
||||
name = struct.unpack_from('<I', row, 12 if index == 1 else 4)[0]
|
||||
if index == 13 and not (struct.unpack_from('<I', row)[0] & 1):
|
||||
name -= image_base
|
||||
at_name = offset(name)
|
||||
result.append(data[at_name:data.index(b'\0', at_name)].decode('ascii').lower())
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
import shutil
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('folder', type=Path)
|
||||
parser.add_argument('--runtime-dir', type=Path)
|
||||
args = parser.parse_args()
|
||||
folder = args.folder
|
||||
pending = sorted([*folder.glob('*.exe'), *folder.glob('*.dll')])
|
||||
if not pending:
|
||||
raise ValueError('No Windows binaries to validate')
|
||||
checked = set()
|
||||
runtime = set()
|
||||
while pending:
|
||||
binary = pending.pop()
|
||||
if binary.name.lower() in checked:
|
||||
continue
|
||||
checked.add(binary.name.lower())
|
||||
for name in imports(binary):
|
||||
if not re.match(r'(msvcp|vcruntime|vccorlib|concrt)\d', name):
|
||||
continue
|
||||
runtime.add(name)
|
||||
target = folder / name
|
||||
if not target.exists():
|
||||
provider = next((p for p in args.runtime_dir.glob('*.dll') if p.name.lower() == name), None) if args.runtime_dir else None
|
||||
if provider is None:
|
||||
raise SystemExit(f'Missing MSVC runtime: {binary.name} requires {name}')
|
||||
shutil.copyfile(provider, target)
|
||||
pending.append(target)
|
||||
print(f'PASS: {len(checked)} root PE files checked; required app-local CRT: {", ".join(sorted(runtime)) or "none"}')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in new issue
Block a user