Ebrahim Byagowi | 8d19907 | 2020-02-19 14:56:55 +0330 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Behdad Esfahbod | f93a5e6 | 2019-05-24 17:02:38 -0400 | [diff] [blame] | 2 | |
| 3 | # Copied from https://github.com/xantares/mingw-ldd/blob/master/mingw-ldd.py |
| 4 | # Modified to point to right prefix location on Fedora. |
| 5 | |
| 6 | # WTFPL - Do What the Fuck You Want to Public License |
Behdad Esfahbod | f93a5e6 | 2019-05-24 17:02:38 -0400 | [diff] [blame] | 7 | import pefile |
| 8 | import os |
| 9 | import sys |
| 10 | |
| 11 | |
| 12 | def get_dependency(filename): |
| 13 | deps = [] |
| 14 | pe = pefile.PE(filename) |
| 15 | for imp in pe.DIRECTORY_ENTRY_IMPORT: |
| 16 | deps.append(imp.dll.decode()) |
| 17 | return deps |
| 18 | |
| 19 | |
| 20 | def dep_tree(root, prefix=None): |
| 21 | if not prefix: |
| 22 | arch = get_arch(root) |
| 23 | #print('Arch =', arch) |
| 24 | prefix = '/usr/'+arch+'-w64-mingw32/sys-root/mingw/bin' |
| 25 | #print('Using default prefix', prefix) |
| 26 | dep_dlls = dict() |
| 27 | |
| 28 | def dep_tree_impl(root, prefix): |
| 29 | for dll in get_dependency(root): |
| 30 | if dll in dep_dlls: |
| 31 | continue |
| 32 | full_path = os.path.join(prefix, dll) |
| 33 | if os.path.exists(full_path): |
| 34 | dep_dlls[dll] = full_path |
| 35 | dep_tree_impl(full_path, prefix=prefix) |
| 36 | else: |
| 37 | dep_dlls[dll] = 'not found' |
| 38 | |
| 39 | dep_tree_impl(root, prefix) |
| 40 | return (dep_dlls) |
| 41 | |
| 42 | |
| 43 | def get_arch(filename): |
| 44 | type2arch= {pefile.OPTIONAL_HEADER_MAGIC_PE: 'i686', |
| 45 | pefile.OPTIONAL_HEADER_MAGIC_PE_PLUS: 'x86_64'} |
| 46 | pe = pefile.PE(filename) |
| 47 | try: |
| 48 | return type2arch[pe.PE_TYPE] |
| 49 | except KeyError: |
| 50 | sys.stderr.write('Error: unknown architecture') |
| 51 | sys.exit(1) |
| 52 | |
| 53 | if __name__ == '__main__': |
| 54 | filename = sys.argv[1] |
| 55 | for dll, full_path in dep_tree(filename).items(): |
| 56 | print(' ' * 7, dll, '=>', full_path) |
| 57 | |