Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an executable .exe PE file manually?

All texts on how to create a compiler stop after explaining lexers and parsers. They don't explain how to create the machine code. I want to understand the end-to-end process.

Currently what I understand is that, the Windows exe file formats are called Portable Executable. I read about the headers it has and am yet to find a resource which explains this easily.

My next issue is, I don't see any resource which explains how machine code is stored in the file. Is it like 32-bit fixed length instructions stored one after another in the .text section?

Is there any place which at least explains how to create an exe file which does nothing (it has a No Op instruction). My next step then would be linking to dll files to print to console.

like image 236
AppleGrew Avatar asked Oct 31 '11 18:10

AppleGrew


People also ask

What programming language creates EXE files?

In which language .exe windows files are made ?? Many languages can compile directly into .exe files (C,C++,Delphi,Fortran, VB6,VB.NET, Lua,C#,F#,J# and other . net supporting languages). Languages which cannot produce executables directly can also be used to create .exe files through some form of plugin/converter.

How do I create a simple EXE file in Notepad?

Type the “exe” file for the program you want to create from the programming language into Notepad. Type the file in C++ programming language. Use the “Alt” and the 3-digit combinations to create symbols that do not appear on the keyboard but that you need in the program.

What creates executable files?

The Compiler the supports generation of procedural COBOL code to Java . class files and also offers object-oriented extensions to the COBOL language. You produce executable code by compiling and linking in one step. An executable file has a filename extension of .exe.


1 Answers

Nice question! I don't have much expertise on this specific question, but this is how I would start:

  1. PE or ELF does not create pure machine code. It also contains some header info etc. Read more: Writing custom data to executable files in Windows and Linux

  2. I assume you are looking for how does ELF/PE file hold the machine code, you can get that from this question (using objdump): How do you extract only contents of an ELF section

  3. Now, if you want to know how the content part is generated in the first place, i.e. how is the machine code generated, then that's the task of the compiler's code generation.

  4. Try out some resource editor like ResourceEditor to understand the exe or simply ildasm.

PS: These are mostly Unix solutions, but I am sure, PE should be doing something fundamentally similar.

I think the best way to approach it will be first try to analyze how existing PE/ELFs work, basically reverse engineering. And to do that, Unix machine will be a good point to start. And then do your magic :)

Not same but a similar question here.

Update:

I generated an object dump out of a sample c code. Now, I assume that's what you are targeting right? You need to know do you generate this file (a.out)?

https://gist.github.com/1329947

Take a look at this image, a life time of a c code.

enter image description here

Source Now, just to be clear, you are looking to implement the final step, i.e. conversion of object code to executable code?

like image 83
zengr Avatar answered Sep 21 '22 06:09

zengr