mingw-ldd.py 1.6 KB

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