PE 文件可选 Header 幻数

PE File Optional Header Magic Number

我目前正在编写一个从 PE 文件读取和写入的库。据我从规范中了解到,Optional Header 通常不在 object 文件中使用。但是,它包含指定 PE32 和 PE32+ 变体之间的字段。 PE 是否允许 PE32+ object 文件?

规范具体说:

An object file may have an optional header, but generally this header has no function in an object file except to increase size.

这对我来说似乎有点模棱两可。我倾向于将其读作 "the optional header is ignored in object files and only bloats the file." 但是,它可以读作 "the optional header can be used in object files to increase the size from 32 to 64 bits." 这是什么意思?

编辑:根据规范,0x10b 值表示 32 位,0x20b 值表示 64 位,0x107 值表示 ROM 映像。 ROM 映像总是 32 位的吗?

the Optional Header is not generally used in object files

没错

However, it contains the field that specifies between PE32 and PE32+ variants.

也正确

Does PE allow for PE32+ object files?

如果您的意思是可以将 PE32 .obj 和 link 编译为 PE32+ 应用程序,由于指针大小不同,无论如何这样做都是错误的。

I'm inclined to read this as "the optional header is ignored in object files and only bloats the file."

我相信这是正确的。

可选 header 是图像必须的(a.k.a.exe 或 dll)。

您可以通过对 .obj 或 .exe 文件执行 dumpbin /header 操作自行收集所有这些信息。 Dumpbin 随每个 VC++ 安装分发。比如小文项目

dumpbin.exe /headers PE.exe | findstr PE

会产生

Dump of file PE.exe
PE signature found
             20B magic # (PE32+)

但是在 .obj 文件上使用 dumpbin 不会产生任何结果,没有可选的 Header。

Are ROM images always 32-bit?

我怀疑是这样,但坦率地说,我不知道,从来没有使用过 ROM 映像

PS 我相信以上所有内容对于使用 Visual C++ 编译的文件都是正确的