okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8aeefca2d
main
1"""tools/hip/dll_closure.py - which DLLs a DLL needs, read from the PE import tables (no pefile, no objdump). 2 3python tools/hip/dll_closure.py DIR amdhip64_7.dll amd_comgr.dll # prints the closure inside DIR 4 5Why: the Windows HIP engine puts amdhip64_7.dll and amd_comgr.dll next to strata.exe so that Windows binds THEM and not 6an AMD driver's copy in System32 (#468, #461). But amdhip64_7.dll itself imports rocm_kpack.dll, which imports the 7MSVC runtime; with only the two DLLs beside the exe the full-path load fails (error 126, ERROR_MOD_NOT_FOUND), Windows 8falls back to System32's copy, and the first big prompt dies with hipErrorInvalidDeviceFunction (#461, maraa081). So 9what goes beside the exe is the DLLs' whole dependency closure, computed here from the files actually shipped rather 10than from a list of names that goes stale with the next ROCm. 11 12A name is followed only when the DLL exists in `search_dir` (the bundle's rocm/bin). Everything else - kernel32, 13user32, setupapi, the api-ms-win-* forwarders - is a Windows system DLL and is left alone. 14""" 15from __future__import annotations 16 17import struct 18import sys 19from pathlib import Path 20 21 22def pe_imports (path )-> list [str ]: 23"""The DLL names in a PE file's import table and delay-import table, in file order. [] for a file that is not a 24PE image (or is truncated): the caller treats it as a leaf.""" 25try : 26data = Path (path ).read_bytes () 27except OSError : 28return [] 29try : 30if data [:2 ]!= b"MZ" : 31return [] 32pe = struct .unpack_from ("<I" ,data ,0x3C )[0 ] 33if data [pe :pe + 4 ]!= b"PE\0\0" : 34return [] 35nsec ,= struct .unpack_from ("<H" ,data ,pe + 6 ) 36opt_size ,= struct .unpack_from ("<H" ,data ,pe + 20 ) 37opt = pe + 24 38magic ,= struct .unpack_from ("<H" ,data ,opt ) 39dd = opt + (112 if magic == 0x20B else 96 )# PE32+ : 112, PE32 : 96 40secs = [] 41sec0 = opt + opt_size 42for i in range (nsec ): 43o = sec0 + 40 * i 44vsize ,va ,rsize ,raw = struct .unpack_from ("<IIII" ,data ,o + 8 ) 45secs .append ((va ,max (vsize ,rsize ),raw )) 46 47def off (rva ): 48for va ,size ,raw in secs : 49if va <= rva < va + size : 50return rva - va + raw 51return None 52 53def cstr (o ): 54e = data .index (b"\0" ,o ) 55return data [o :e ].decode ("ascii" ,"replace" ) 56 57names :list [str ]= [] 58imp_rva ,= struct .unpack_from ("<I" ,data ,dd + 8 * 1 )# directory 1: imports 59o = off (imp_rva )if imp_rva else None 60while o is not None : 61name_rva = struct .unpack_from ("<I" ,data ,o + 12 )[0 ] 62if name_rva == 0 : 63break 64n = off (name_rva ) 65if n is not None : 66names .append (cstr (n )) 67o += 20 68dly_rva ,= struct .unpack_from ("<I" ,data ,dd + 8 * 13 )# directory 13: delay imports 69o = off (dly_rva )if dly_rva else None 70while o is not None : 71attrs ,name_rva = struct .unpack_from ("<II" ,data ,o ) 72if name_rva == 0 : 73break 74n = off (name_rva )# (RVA form, attrs bit 0) 75if n is not None : 76names .append (cstr (n )) 77o += 32 78return names 79except (struct .error ,ValueError ,IndexError ): 80return [] 81 82 83def dll_closure (search_dir ,roots ,imports = pe_imports )-> list [str ]: 84"""`roots` (file names inside `search_dir`) plus every DLL in `search_dir` they import, followed recursively. 85The names are returned as spelled on disk, roots first, the rest in discovery order. A root that is not in the 86directory is skipped; an import that is not in the directory (a Windows system DLL) is not followed.""" 87d = Path (search_dir ) 88on_disk = {p .name .lower ():p .name for p in d .iterdir ()if p .is_file ()and p .suffix .lower ()== ".dll" } \ 89if d .is_dir ()else {} 90out :list [str ]= [] 91todo = [r for r in roots ] 92seen :set [str ]= set () 93while todo : 94name = todo .pop (0 ) 95low = name .lower () 96if low in seen or low not in on_disk : 97continue 98seen .add (low ) 99real = on_disk [low ] 100out .append (real ) 101for dep in imports (d / real ): 102if dep .lower ()not in seen : 103todo .append (dep ) 104return out 105 106 107def main ()-> int : 108if len (sys .argv )< 3 : 109__doc__ ) 110return 2 111for n in dll_closure (sys .argv [1 ],sys .argv [2 :]): 112n ) 113return 0 114 115 116if __name__ == "__main__" : 117raise SystemExit (main ())