如何手动创建可执行.exe PE文件? [英] How can I create an executable .exe PE file manually?

查看:254
本文介绍了如何手动创建可执行.exe PE文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在解释词法分析器和语法分析器之后,所有有关如何创建编译器的文章均停止。他们没有解释如何创建机器代码。我想了解端到端的过程。



目前,我了解的是Windows exe文件格式称为Portable Executable。我了解了它的标头,还没有找到一个可以轻松解释该问题的资源。



我的下一个问题是,我没有看到任何解释该机器如何运行的资源。代码存储在文件中。就像32位固定长度指令在 .text 节中一个接一个地存储吗?



有没有至少说明如何创建不执行任何操作的exe文件的地方(它具有No Op指令)。然后,我的下一步就是链接到dll文件以打印到控制台。

解决方案

很好的问题!我在这个特定问题上没有很多专业知识,但这就是我的开始方式。


  1. PE或ELF不会创建纯机器代码。它还包含一些标头信息等。更多信息:
    现在,为了清楚起见,您正在寻求实现最后一步,即将目标代码转换为可执行代码?


    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.

    解决方案

    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.

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

    这篇关于如何手动创建可执行.exe PE文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆