• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
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
7from __future__ import print_function
8import pefile
9import os
10import sys
11
12
13def get_dependency(filename):
14    deps = []
15    pe = pefile.PE(filename)
16    for imp in pe.DIRECTORY_ENTRY_IMPORT:
17        deps.append(imp.dll.decode())
18    return deps
19
20
21def dep_tree(root, prefix=None):
22    if not prefix:
23        arch = get_arch(root)
24        #print('Arch =', arch)
25        prefix = '/usr/'+arch+'-w64-mingw32/sys-root/mingw/bin'
26        #print('Using default prefix', prefix)
27    dep_dlls = dict()
28
29    def dep_tree_impl(root, prefix):
30        for dll in get_dependency(root):
31            if dll in dep_dlls:
32                continue
33            full_path = os.path.join(prefix, dll)
34            if os.path.exists(full_path):
35                dep_dlls[dll] = full_path
36                dep_tree_impl(full_path, prefix=prefix)
37            else:
38                dep_dlls[dll] = 'not found'
39
40    dep_tree_impl(root, prefix)
41    return (dep_dlls)
42
43
44def get_arch(filename):
45    type2arch= {pefile.OPTIONAL_HEADER_MAGIC_PE: 'i686',
46                pefile.OPTIONAL_HEADER_MAGIC_PE_PLUS: 'x86_64'}
47    pe = pefile.PE(filename)
48    try:
49        return type2arch[pe.PE_TYPE]
50    except KeyError:
51        sys.stderr.write('Error: unknown architecture')
52        sys.exit(1)
53
54if __name__ == '__main__':
55    filename = sys.argv[1]
56    for dll, full_path in dep_tree(filename).items():
57        print(' ' * 7, dll, '=>', full_path)
58
59