What's the best tool for converting PE binaries to ELF binaries?
Following is a brief motivation for this question:
- Suppose I have a simple C program.
- I compiled it using gcc for linux(this gives ELF), and using 'i586-mingw32msvc-gcc' for Windows(this gives a PE binary).
- I want to analyze these two binaries for similarities, using Bitblaze's static analysis tool - vine(http://bitblaze.cs.berkeley.edu/vine.html)
- Now vine doesn't have a good support for PE binaries, so I wanted to convert PE->ELF, and then carry on with my comparison/analysis.
Since all the analysis has to run on Linux, I would prefer a utility/tool that runs on Linux.
Thanks
I've found a simpler way to do this. Use the strip command.
Example
The
-N xxxxxxx
option tells strip to only strip the symbol namedxxxxxxx
, which hopefully isn't in the file. The-O elf32-i386
has it write out the file in that format.To see supported formats run
I am using the strip command from mxe, which on my system is actually named
/opt/mxe/usr/bin/i686-w64-mingw32.static-strip
.I don't know whether this totally fits your needs, but is it an option for you to cross-compile with your MinGW version of gcc?
I mean do say: does it suit your needs to have i586-mingw32msvc-gcc compile direct to ELF format binaries (instead of the PEs you're currently getting). A description of how to do things in the other direction can be found here - I imagine it will be a little hacky but entirely possible to make this work for you in the other direction (I must admit I haven't tried it).
It is possible to rebuild an EXE as an ELF binary, but the resulting binary will segfault very soon after loading, due to the missing operating system.
Here's one method of doing it.
Summary
ld
with the linker script to produce the ELF file.Detailed Example
Dump the section headers of the EXE file. I'm using
objdump
from themingw
cross compiler package to do this.Use
dd
(or a hex editor) to extract the raw section data from the EXE. Here, I'm just going to copy the code and data sections (named AUTO and DGROUP in this example). You may want to copy additional sections though.Note, I've converted the file offsets and section sizes from hex to decimal to use as
skip
andcount
, but I'm using a block size of 512 bytes indd
to speed up the process (example: 0x0400 = 1024 bytes = 2 blocks @ 512 bytes).Encapsulate the raw section data in GNU ld linker scripts snippets (using the BYTE directive). This will be used to populate the sections.
Write a linker script to build an ELF binary, including those scripts from the previous step. Note I've also set aside space for the uninitialized data (.bss) section.
Run the linker script with GNU
ld
to produce the ELF file. Note I have to use an emulation modeelf_i386
since I'm using 64-bit Linux, otherwise a 64-bit ELF would be produced.Run the new program, and watch it segfault as it's not running on Windows.
IDA Pro output for that location:
For porting binaries to Linux, this is kind of pointless, given the Wine project. For situations like the OP's, it may be appropriate.