# 可移植可执行文件

**可移植可执行文件**（Portable Executable，PE）格式是[微软 Windows](https://www.edgepedia.org/zh/microsoft-windows) 系列操作系统下可执行映像（如 EXE 和 DLL 文件）以及对象文件的文件格式；对象文件这一变体称为通用对象文件格式（Common Object File Format，COFF）。这一名称的由来在于该格式并非针对特定架构：Windows 支持过的每一种 CPU 家族都使用同一种格式。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> PE 于 1993 年随 [Windows NT 3.1](https://www.edgepedia.org/zh/windows-nt-3-1) 一同问世，当时微软用它取代了 16 位的 New Executable（NE）格式，其设计借鉴了 COFF 对象文件格式。<sup>[2](https://linuxvox.com/blog/what-happens-when-you-run-a-program/)</sup>

| 关键事实 | 详情 |
|---|---|
| 适用范围 | Windows 系列可执行（映像）文件与对象文件的结构；映像用 PE，对象用 COFF<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> |
| 签名 | 4 字节签名“PE\0\0”，位于偏移 0x3c 处所存储的文件偏移位置上，即 MS-DOS 存根之后<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> |
| 起源 | 源自 VAX/VMS 上的 COFF；最初的 Windows NT 团队中许多人来自数字设备公司<sup>[3](https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail)</sup> |
| 64 位形式 | PE32+ 将某些字段从 32 位加宽到 64 位，未新增字段，并删去了一个字段<sup>[3](https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail)</sup> |
| 节数量限制 | Windows 加载器将节的数量限制为 96 个<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> |
| 导出限制 | 导出序号是 16 位字段，因此导出符号的数量上限为 64K<sup>[4](https://github.com/llvm/llvm-blog-www/blob/main/content/posts/2024-11-23-gsoc-windows-plugins.md)</sup> |
| 同类格式 | Linux/Unix 上的 ELF 与 macOS/iOS 上的 Mach-O 是 Windows 之外对应的可执行文件格式<sup>[5](https://filetypedb.com/executable/exe/)</sup> |

## PE 文件的结构

PE 映像文件以一个兼容 MS-DOS 2.0 的 EXE 头和一个存根程序开头，这是一段仅为兼容 MS-DOS 而保留的遗留区域。存根之后，在偏移 0x3c 处所指定的文件偏移位置上，是 4 字节的“PE\0\0”签名，用以标识该文件为 PE 映像。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> PE 头本身按 8 字节边界对齐，其后是节头和映像页，其中存放导入信息、导出信息、基址重定位和资源信息。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup>

在映像中，COFF 文件头紧随签名之后；在对象文件中，它则位于文件开头。其字段包括 Machine（标识目标 CPU 类型的编号）、NumberOfSections（节的数量）、TimeDateStamp（自 1970 年 1 月 1 日 00:00 起秒数的低 32 位）以及 Characteristics（指示文件属性的标志）。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> 映像文件还带有一个可选头，可执行文件必须有此头，对象文件则不需要，其魔数决定映像是 PE32 还是 PE32+。<sup>[6](https://learn.microsoft.com/en-gb/windows/win32/debug/pe-format)</sup>

节是 PE 或 COFF 文件中代码或数据的基本单位。节中的所有原始数据必须连续加载，映像中可以包含 .tls 或 .reloc 等专用节。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> 映像内部的地址是相对虚拟地址（relative virtual address，RVA），即加载后某一项的地址减去映像基址；将 RVA 转换为文件偏移需要经过节表。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> ImageBase 字段给出映像加载到内存时首字节的首选地址，必须是 64 K 的倍数。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup>

## EXE、DLL 与加载

EXE 和 DLL 文件使用完全相同的 PE 格式；区别仅在于一个比特，指示该文件应被当作 EXE 还是 DLL 处理，甚至连 DLL 这一文件扩展名也是人为的，因为 .OCX 控件和控制面板小程序都是 DLL。<sup>[3](https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail)</sup> 当 Windows 加载器将 PE 文件映射到内存时，内存中的版本称为模块，其起始地址即 HMODULE。加载器决定映射文件的哪些部分；重定位信息可能被读取但不被映射，置于文件末尾的调试信息则可能完全不被映射。<sup>[3](https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail)</sup>

这种设计的一个实际结果是：磁盘上的数据结构与内存中使用的结构相同，因此像 IMAGE_NT_HEADERS 这样的结构在磁盘上和内存中完全一致；如果你知道如何在 PE 文件中找到某项内容，几乎肯定也能在已加载的模块中找到同样的信息。<sup>[3](https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail)</sup>

## 导入、导出与签名

PE 维护着导入和导出符号的显式列表。暴露给其他模块的符号用 `__declspec(dllexport)` 修饰，从其他模块导入的符号用 `__declspec(dllimport)` 修饰，这与 ELF 和 [Mach-O](https://www.edgepedia.org/zh/mach-o) 目标不同，后者跨模块边界隐式导出符号。<sup>[4](https://github.com/llvm/llvm-blog-www/blob/main/content/posts/2024-11-23-gsoc-windows-plugins.md)</sup> 导出表为每个公开符号分配一个称为序号的数字标识，以 16 位字段存储，因此导出符号的数量上限为 64K。<sup>[4](https://github.com/llvm/llvm-blog-www/blob/main/content/posts/2024-11-23-gsoc-windows-plugins.md)</sup>

该格式还支持属性证书，它将可验证的声明与映像关联起来；其中最有用的是软件制造商关于映像应具有的消息摘要的声明，可通过公钥或私钥密码学加以验证。<sup>[1](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)</sup> Authenticode 签名即使用这一机制：可选头数据目录的第五项若已填写，则指向一个证书列表，通常附加在文件末尾，映像摘要按微软 Authenticode 规范中“创建 PE 映像哈希”一节计算。<sup>[7](https://uefi.org/specs/UEFI/2.10/32_Secure_Boot_and_Driver_Signing.html?highlight=authenticated+variable)</sup>

## Windows 之外：固件与 Arm 变体

该格式的触角延伸到了固件领域。UEFI 平台初始化规范定义了简洁可执行文件（Terse Executable，TE）映像格式，旨在减少 PE32/PE32+ 映像中 PE/COFF 头的开销；TE 头只保留 PI 架构中执行所需的字段，完全不需要 MS-DOS 存根和 PE 签名。<sup>[8](https://uefi.org/sites/default/files/resources/UEFI_PI_Spec_Final_Draft_1.9.pdf)</sup>

近期 [Windows on Arm](https://www.edgepedia.org/zh/windows-on-arm) 的工作是对 PE 的扩展而非取代。Arm64EC（“Emulation Compatible”，仿真兼容）是 [Windows 11](https://www.edgepedia.org/zh/windows-11) on Arm 的一种 ABI，其中 Arm64 代码遵循 x64 软件约定，包括调用约定和数据结构布局，因此 Arm64EC 与 x64 代码可在同一进程中互操作。<sup>[9](https://learn.microsoft.com/en-us/windows/arm/arm64ec)</sup> 中间对象文件使用机器值 0xA641，这是 MSVC 的内部标识符，未在 winnt.h 中定义；而最终的 EXE 或 DLL 映像带有 8664（x64）（ARM64X）或 AA64（ARM64）（ARM64X）机器值。<sup>[9](https://learn.microsoft.com/en-us/windows/arm/arm64ec)</sup> Arm64X 二进制文件是一种同时包含经典 Arm64 和 Arm64EC 代码的 PE 文件，Windows 11 on Arm 和 [Windows 10](https://www.edgepedia.org/zh/windows-10) on Arm 均可加载。<sup>[10](https://learn.microsoft.com/en-ca/windows/arm/arm64x-pe)</sup>

## 解析差异与安全

PE 文件是恶意软件研究的主要目标。根据 2021 年一项调查中引用的[卡巴斯基实验室](https://www.edgepedia.org/zh/kaspersky-lab)统计，2020 年底平均每天检测到 36 万个恶意软件文件，其中超过 90% 是 [Windows PE](https://www.edgepedia.org/zh/windows-pe) 恶意软件。<sup>[11](https://arxiv.org/abs/2112.12310)</sup>

该格式的复杂性带来了实际的安全问题：处理可执行文件的程序，如操作系统加载器、逆向工程工具和杀毒软件，往往对同一输入文件的解释略有不同。Eurecom 的研究人员及其合作者在 RAID 2021 上发表的一项研究首次对 PE 解析器进行了系统分析，对 [Windows XP](https://www.edgepedia.org/zh/windows-xp)、7 和 10 的加载器以及若干逆向工程和杀毒工具进行了建模。研究表明，流行的分析工具可以被完全绕过，它们提取的信息可以被操纵，恶意软件可以被构造为在某一 Windows 版本上加载而在另一版本上不加载；作者得出结论：解析 PE 文件并不存在唯一正确的方式。<sup>[12](https://www.eurecom.fr/en/publication/6603)</sup> 已观察到恶意软件作者篡改可执行文件格式，使二进制文件在目标设备上正确执行，却被大多数分析工具视为格式错误而丢弃。<sup>[13](https://reyammer.io/publications/2021_blackhateu_peloaderbheu.pdf)</sup>

## 与 ELF 和 Mach-O 的比较

PE、Linux 和 Unix 上的 ELF（Executable and Linkable Format，[可执行与可链接格式](https://www.edgepedia.org/zh/executable-and-linkable-format)）以及 macOS 和 iOS 上的 Mach-O 是三种主流的可执行文件格式，分别对应 Windows、Linux/BSD/Android 和 Apple 生态系统。<sup>[14](https://www.zerodaycyberacademy.com/ressources/pentest/elf-pe-mach-o-differences)</sup> 它们共享由可执行文件管理的共同挑战所塑造的基本概念，一个交叉编译的构建树通常三者兼有。<sup>[15](https://www.inventivehq.com/blog/executable-file-formats-guide)</sup> 在 Windows 上，PE 服务于扩展名为 .exe、.dll 和 .sys 的可执行文件，而 COFF 服务于扩展名为 .obj 的对象文件。<sup>[5](https://filetypedb.com/executable/exe/)</sup>

## References

1. [PE Format, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format)
2. [What Happens When You Run a Program? PE, ELF, Mach-O Explained, LinuxVox](https://linuxvox.com/blog/what-happens-when-you-run-a-program/)
3. [Inside Windows: Win32 Portable Executable File Format in Detail, Matt Pietrek, MSDN Magazine (February 2002), Microsoft Learn archive](https://learn.microsoft.com/en-us/archive/msdn-magazine/2002/february/inside-windows-win32-portable-executable-file-format-in-detail)
4. [Adding plugin support for LLVM and Clang on Windows, LLVM Blog (2024)](https://github.com/llvm/llvm-blog-www/blob/main/content/posts/2024-11-23-gsoc-windows-plugins.md)
5. [The Portable Executable (PE) Format: Anatomy of Windows EXE Files, FileTypeDB](https://filetypedb.com/executable/exe/)
6. [PE Format, Microsoft Learn (en-gb)](https://learn.microsoft.com/en-gb/windows/win32/debug/pe-format)
7. [Secure Boot and Driver Signing, UEFI Specification 2.10](https://uefi.org/specs/UEFI/2.10/32_Secure_Boot_and_Driver_Signing.html?highlight=authenticated+variable)
8. [UEFI Platform Initialization Specification, Release 1.9, Chapter 19: TE Image](https://uefi.org/sites/default/files/resources/UEFI_PI_Spec_Final_Draft_1.9.pdf)
9. [Arm64EC: Build and port apps for native performance on Arm, Microsoft Learn](https://learn.microsoft.com/en-us/windows/arm/arm64ec)
10. [Arm64X PE Files, Microsoft Learn](https://learn.microsoft.com/en-ca/windows/arm/arm64x-pe)
11. [Adversarial Attacks against Windows PE Malware Detection: A Survey of the State-of-the-Art, arXiv:2112.12310](https://arxiv.org/abs/2112.12310)
12. [Lost in the loader: The many faces of the Windows PE file format, Nisi, Graziano, Fratantonio, Balzarotti, RAID 2021, Eurecom](https://www.eurecom.fr/en/publication/6603)
13. [Lost in the Loader: The Many Faces of the Windows PE File Format, Black Hat EU 2021 slides](https://reyammer.io/publications/2021_blackhateu_peloaderbheu.pdf)
14. [ELF, PE, Mach-O : différences et analyse reverse, Zeroday Cyber Academy](https://www.zerodaycyberacademy.com/ressources/pentest/elf-pe-mach-o-differences)
15. [Understanding PE, ELF, and Mach-O, InventiveHQ](https://www.inventivehq.com/blog/executable-file-formats-guide)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 17, 2026; Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Data formats and serialization › Text, encodings, and archives

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/portable-executable

English original: https://www.edgepedia.org/portable-executable
