-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA256
I'm trying to implement parts of imagehlp's BindImageEx so that at least freezing Python scripts to .exe files (as done by cx_Freeze or py2exe) works -- see [1] (from whose comments #24 and #25 most of this message is copied), and their implementations of the StatusRoutine function [2, 3].
In both cases, BindImageEx is called with BIND_NO_BOUND_IMPORTS | BIND_NO_UPDATE | BIND_ALL_IMAGES as Flags, which I think means that no actual changes to the image's virtual address table are done, but they're only used to determine which DLLs are used by the image.
So for cx_Freeze, it would be apparently enough for Wine's BindImageEx stub to iterate over all DLLs whose functions are used by the image, i.e. the exe or dll passed as the ImageName parameter, and call StatusRoutine with the Reason parameter set to BindImportModule, and DllName set to the DLL, while the "Parameter" and "virtualAddress" arguments aren't required for it to work.
Py2exe also requires StatusRoutine called with BindImportProcedure as Reason, and will in that case check if Parameter is set to PyImport_ImportModule before it does something, so I think there should be another loop iterating over a DLL's functions, nested in the one mentioned above, and which invokes StatusRoutine with Status set to BindImportProcedure, and with the current function's name as Parameter.
I think I have found out the crucial function required to implement this to be ImageDirectoryEntryToDataEx [4] (with dir set to IMAGE_DIRECTORY_ENTRY_IMPORT). Similar iterations like the ones sketched here seem to be found in fixup_imports() (in dlls/ntdll/loader.c) or dump_dir_imported_functions() (in tools/winedump/pe.c, though that isn't using win API functions).
So I've tried to implement what I've described above, but unfortunately, I can't seem to figure out the very first part required: obtaining something that can be used as the "base" parameter for ImageDirectoryEntryToDataEx from the ImageName string passed to BindImageEx. I've tried LoadLibraryA(ImageName), LoadLibraryExA(ImageName, NULL, LOAD_LIBRARY_AS_DATAFILE), and MapAndLoad(), but unfortunately, all of them seem to either import all the PE's symbols in a fashion that collides with the already loaded ones (in particular in a python-freezing context, like running "wine C:\Python27\python.exe setup.py build" with the test case attached to [1]), or not load them in a way that can be used for ImageDirectoryEntryToDataEx. I'm hoping for someone to help me figure out how to do this...
Bernhard
[1] http://bugs.winehq.org/show_bug.cgi?id=3591 [2] https://bitbucket.org/anthony_tuininga/cx_freeze/src/de3e8865616a1048d17e951... [3] http://sourceforge.net/p/py2exe/svn/HEAD/tree/trunk/py2exe/source/py2exe_uti... [4] http://source.winehq.org/WineAPI/.html