gdabah / distorm

Powerful Disassembler Library For x86/AMD64
Other
1.26k stars 238 forks source link

Build fils when compiling with MinGW #21

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Compile distorm using MinGW:

C:\Users\tantalum\Documents\Research\distorm>python setup.py build 
--compiler=mingw32

2. Watch the build fail because the wrong arguments are passed to gcc:
running custom_build
running build_py
creating build
creating build\lib
creating build\lib\distorm3
copying python\distorm3\sample.py -> build\lib\distorm3
copying python\distorm3\__init__.py -> build\lib\distorm3
running build_clib
running custom_build_clib
building 'distorm3' library
creating build\temp.win-amd64-2.7
creating build\temp.win-amd64-2.7\src
C:\strawberry\c\bin\gcc.exe -mno-cygwin -mdll -O -Wall -Isrc -Iinclude -c 
src\decoder.c -o build\temp.win-amd64-2.7\src\decoder.o /Ox /Ob1 /Oy ""/D 
WIN32"" ""/D _DLL"" ""/D LIBDISTORM"" ""/D SUPPORT_64BIT_OFFSET"" ""/D _MBCS"" 
/GF /Gm /Zi /EHsc /MT /Gy /W4 /nologo /c /TC /Fdbuild\vc90.pdb
gcc: /Ox: No such file or directory
gcc: /Ob1: No such file or directory
gcc: /Oy: No such file or directory
gcc: /D: No such file or directory
gcc: WIN32: No such file or directory
gcc: /D: No such file or directory
gcc: _DLL: No such file or directory
gcc: /D: No such file or directory
gcc: LIBDISTORM: No such file or directory
gcc: /D: No such file or directory
gcc: SUPPORT_64BIT_OFFSET: No such file or directory
gcc: /D: No such file or directory
gcc: _MBCS: No such file or directory
gcc: /GF: No such file or directory
gcc: /Gm: No such file or directory
gcc: /Zi: No such file or directory
gcc: /EHsc: No such file or directory
gcc: /MT: No such file or directory
gcc: /Gy: No such file or directory
gcc: /W4: No such file or directory
gcc: /nologo: No such file or directory
gcc: /c: No such file or directory
gcc: /TC: No such file or directory
gcc: /Fdbuild\vc90.pdb: No such file or directory
error: command 'gcc' failed with exit status 1

What is the expected output? What do you see instead?
The expected outcome would be for the correct flags to be passed to gcc instead 
of just pretending that everything uses MSVC.

What version of the product are you using? On what operating system?
I'm using the svn HEAD version with Windows 7 on a 64-bit machine

So what's the core of the issue?
The problem lies in setup.py around line 180. The build selects the extra 
compiler flags depending on the operating system instead of the compiler being 
used. For windows the extra compiler flags are suitable for MSVC but cause gcc 
to fail.

    if 'windows' in system:
        libraries = [(
            'distorm3', dict(
            package='distorm3',
            sources=get_sources,
            include_dirs=['src', 'include'],
            extra_compile_args=['/Ox', '/Ob1', '/Oy', '"/D WIN32"',
                                '"/D _DLL"', '"/D LIBDISTORM"', '"/D SUPPORT_64BIT_OFFSET"',
                                '"/D _MBCS"', '/GF', '/Gm', '/Zi', '/EHsc',
                                '/MT', '/Gy', '/W4', '/nologo', '/c', '/TC',
                                '/Fdbuild\\vc90.pdb'],
            extra_link_args=['/MANIFEST']))]

Original issue reported on code.google.com by bli...@gmail.com on 6 Apr 2011 at 1:54

GoogleCodeExporter commented 9 years ago
Hey there,
thanks for the report.

Since I have never used MinGW before, I would appreciate further help here.
Can you try to compile it and give me the command line switches for the 
compiler?

Gil

Original comment by distorm@gmail.com on 12 Apr 2011 at 6:41

GoogleCodeExporter commented 9 years ago
I went through Microsofts documentation and came up with this summary of the 
args passed to MSVC:
/Ox -- Maximize Optimization
/Ob1 -- Expand only functions marked inline, enable inline expansion
/oy -- Omit frame pointer
/D WIN32 -- Declare WIN32
/D _DLL -- Declare _DLL
/D LIBDISTORM -- Declare LIBDISTORM
/D SUPPORT_64BIT_OFFSET
/D _MBCS 
/GF -- Remove Dulplicate Strings
/Gm -- Enable minimul rebuild
/Zi -- Generate debugging information
/EHsc -- Exception Handling: Catch C++ exceptions
/MT -- Use multithreaded staticly linked version of the standard library
/Gy -- Enable function level linking, automatically set by /Zi
/W4 -- Set warning level to 4
/nologo -- Duh!
/c -- Don't link
/TC -- Specify C source
/Fdbuild\\vc90.pdb --  Use the vc90 PDB file

I'v hacked the setup file to use these options instead of the MSVC options:
extra_compile_args=['-fPIC', '-O2', '-Wall',
                    '-DSUPPORT_64BIT_OFFSET', '-D_DLL',
                    '-DLIBDISTORM', '-DWIN32']))]

Building and installing with these options work.... but when you try to run the 
sample python complains that it can't load the dynamic lirary:
C:\Users\tantalum\Documents\Research\ian\distorm_test>python sample.py
Traceback (most recent call last):
  File "sample.py", line 6, in <module>
    import distorm3
  File "C:\Python27\lib\site-packages\distorm3\__init__.py", line 47, in <module>
    raise ImportError("Error loading the diStorm dynamic library")
ImportError: Error loading the diStorm dynamic library

I don't know what's causing this. Trying to link a C program also failes.
C:\Users\tantalum\Documents\Research\ian\distorm_test>gcc disassemble.c -o 
disassemble -ldistorm3 -L C:\Python27\Lib\site-packages\distorm3\
C:\Users\tantalum\AppData\Local\Temp\ccGvYPi3.o:disassemble.c:(.text+0x5a): 
undefined reference to `distorm_version'
C:\Users\tantalum\AppData\Local\Temp\ccGvYPi3.o:disassemble.c:(.text+0x3fd): 
undefined reference to `distorm_decode64'
collect2: ld returned 1 exit status

To make things even more fun I decided to see what 'dumpbin /EXPORTS' had to 
say about distorm3.dll and distorm3.lib, and they both export distorm_decode64 
and distorm_version:
c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>dumpbin /EXPORTS 
C:\Python27\Lib\site-packages\distorm3\distorm3.dll
Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file C:\Python27\Lib\site-packages\distorm3\distorm3.dll

File Type: DLL

  Section contains the following exports for distorm3.dll

    00000000 characteristics
    4DA77270 time date stamp Thu Apr 14 17:17:20 2011
        0.00 version
           1 ordinal base
          37 number of functions
          37 number of names

    ordinal hint RVA      name

          1    0 00007000 II_3dnow
          2    1 00011080 II_arpl
          3    2 0001108C II_movsxd
          4    3 000110A4 II_nop
          5    4 000110B0 II_pause
          6    5 00007050 Instructions
          7    6 00007044 Table_0F
          8    7 00007038 Table_0F_0F
          9    8 0000702C Table_0F_38
         10    9 00007020 Table_0F_3A
         11    A 00012160 _MNEMONICS
         12    B 00017E40 _REGISTERS
         13    C 00010760 _REGISTERTORCLASS
         14    D 000056E0 chrcat_WS
         15    E 000014B0 decode_internal
         16    F 00002C10 distorm_decode64
         17   10 00002230 distorm_decompose64
         18   11 000024B0 distorm_format64
         19   12 00002220 distorm_version
         20   13 00002F40 inst_lookup
         21   14 00002F00 inst_lookup_3dnow
         22   15 00003C80 operands_extract
         23   16 00005010 prefixes_decode
         24   17 00004EE0 prefixes_ignore
         25   18 00004F00 prefixes_ignore_all
         26   19 00004E50 prefixes_is_valid
         27   1A 00004F30 prefixes_set_unused_mask
         28   1B 00005380 prefixes_use_segment
         29   1C 00005640 str_code_hb
         30   1D 00005440 str_code_hdw
         31   1E 000054D0 str_code_hqw
         32   1F 00005690 str_hex_b
         33   20 00005590 str_off64
         34   21 00005700 strcat_WS
         35   22 00005740 strcatlen_WS
         36   23 000056C0 strclear_WS
         37   24 00005780 strcpylen_WS

  Summary

        1000 .CRT
        1000 .bss
        B000 .data
        1000 .edata
        1000 .idata
        7000 .rdata
        2000 .reloc
        6000 .text
        1000 .tls

c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC>dumpbin /EXPORTS 
C:\Python27\Lib\site-packages\distorm3\distorm3.lib
Microsoft (R) COFF/PE Dumper Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

Dump of file C:\Python27\Lib\site-packages\distorm3\distorm3.lib

File Type: LIBRARY

     Exports

       ordinal    name

                  distorm_decode64
                  distorm_decompose64
                  distorm_format64
                  distorm_version

  Summary

          C6 .debug$S
          14 .idata$2
          14 .idata$3
           8 .idata$4
           8 .idata$5
           E .idata$6

Any hints on how to move forward would be wonderful. I would very much like to 
see this working with mingw. Sorry if I miss something obvious, it's been a 
while since I worked with this kind of stuff on windows and you gotta love the 
way it 'keeps things orignized'.

Original comment by bli...@gmail.com on 14 Apr 2011 at 10:44

GoogleCodeExporter commented 9 years ago
Hey
I will check it out tomorrow and let you know.

Original comment by distorm@gmail.com on 15 Apr 2011 at 11:50

GoogleCodeExporter commented 9 years ago

Original comment by distorm@gmail.com on 16 Apr 2011 at 1:16

GoogleCodeExporter commented 9 years ago
From my test, I bet you compiled the extension as an x86 binary, and your 
Python is x64. Therefore you cannot load the DLL into the process, since it's 
incompatible. You will have to rebuild diStorm as x64 somehow. Or use Python 
for x86, err.
As for now it seems distutils still doesn't support MinGW well, and I'm not 
going to support it. Sorry.

Original comment by distorm@gmail.com on 16 Apr 2011 at 2:55

GoogleCodeExporter commented 9 years ago
FYI: I was able to make it working.
I installed MinGW64 from http://www.drangon.org/mingw
make sure you extract the file to c:\mingw
(rename the original 32bits version first, so not to override the files).
Or alternatively, you could set the path to the 64 bits version of gcc.exe of 
the new MinGW... probably doing that is smarter.

Then I tried to compile using: python setup.py --compiler=mingw32
(While having your patch to the setup.py file)
The build failed on the compilation of the .dll file itself, but the object 
files were compiled.
This is where I copy pasted that command line which failed and removed the part 
"-lmsvcr90" and re-ran that command line and it created the actual .dll file 
for x64. After that I copied that .dll to the site-packages\distorm3 and 
imported the distorm3 library in Python and it worked!

The command line I ran was:
c:\mingw\bin\gcc.exe -mno-cygwin -shared -s 
build\temp.win-amd64-2.7\src\decoder.o build\temp.win-amd64-2.7\src\distorm.o 
build\temp.win-amd64-2.7\src\instructions.o 
build\temp.win-amd64-2.7\src\insts.o build\temp.win-amd64-2.7\src\operands.o 
build\temp.win-amd64-2.7\src\prefix.o build\temp.win-amd64-2.7\src\textdefs.o 
build\temp.win-amd64-2.7\src\wstring.o build\temp.win-amd64-2.7\src\x86defs.o 
build\temp.win-amd64-2.7\include\mnemonics.o -lmsvcr90 -o 
build\lib\distorm3\distorm3.dll

Though it's possible on your env the files will be created in a different 
directory, but just so you get the idea.
Hope it solves your problem.

Original comment by distorm@gmail.com on 16 Apr 2011 at 3:30