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

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

问题描述

在解释词法分析器和解析器之后,所有关于如何创建编译器的文本都停止了.他们没有解释如何创建机器代码.我想了解端到端的流程.

目前我的理解是,Windows exe 文件格式被称为 Portable Executable.我阅读了它的标题,但尚未找到可以轻松解释这一点的资源.

我的下一个问题是,我没有看到任何解释机器代码如何存储在文件中的资源.是不是像 .text 段中一个接一个的存储 32 位定长指令一样?

是否有任何地方至少解释了如何创建一个什么都不做的 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天全站免登陆