# 二进制翻译

二进制翻译把为一种指令集架构（ISA）编写的可执行机器码转换为另一种指令集架构的等效代码，使为某一处理器编译的程序能够在另一种处理器上运行；它是一种高效的仿真形式。<sup>[1](https://www.complang.tuwien.ac.at/schani/papers/bintrans.pdf)</sup> 其产物取决于翻译器的类型：静态翻译器离线生成翻译后的二进制文件，动态翻译器在程序运行时生成并缓存翻译后的代码，而纯仿真器不进行缓存，直接解释执行。<sup>[2](https://ece.northeastern.edu/groups/nucar/publications/binarytrans.pdf)</sup> 在用户层面，动态翻译器直接在宿主操作系统上运行客户机二进制文件；在系统层面，它翻译整个客户机环境，包括虚拟地址转换和系统调用。<sup>[3](https://arxiv.org/html/2402.09688v1)</sup> 该技术支撑遗留软件迁移、程序行为分析和系统虚拟化。<sup>[4](https://www.mdpi.com/2079-9292/12/14/3025)</sup>

| 关键事实 | 详情 |
|---|---|
| 产生的产物 | 翻译后的二进制文件（静态），或运行时缓存的翻译代码（动态）<sup>[2](https://ece.northeastern.edu/groups/nucar/publications/binarytrans.pdf)</sup> |
| 代码膨胀下限 | 跨 ISA 翻译器每翻译一条客户机指令至少需要 1.46 条宿主指令，这一数据来自对八款商用和研究型 DBT 的测量<sup>[5](https://rrpress.utsa.edu/server/api/core/bitstreams/82e14411-c83e-4c7f-a149-4c154d982acd/content)</sup> |
| 典型减速幅度 | 从低于 7.5%（MAMBO-X64 在按序执行的 Cortex-A53 核心上）到超过 1.2 倍（DynamoRIO 和 Pin 即使原生运行 x86_64 客户机代码），再到平均 716% 的开销（QEMU，x86-64 到 x86-64）<sup>[6](https://www.dcs.gla.ac.uk/%7Ejosecr/pub/2016_iiswc.pdf)</sup><sup> • </sup><sup>[5](https://rrpress.utsa.edu/server/api/core/bitstreams/82e14411-c83e-4c7f-a149-4c154d982acd/content)</sup><sup> • </sup><sup>[7](https://home.cit.tum.de/~engelke/pubs/2104-vee.pdf)</sup> |
| 主要开销 | 间接分支：在 SPEC CPU 2017 整数基准中约占动态指令的 0.91%，但膨胀率超过 10<sup>[5](https://rrpress.utsa.edu/server/api/core/bitstreams/82e14411-c83e-4c7f-a149-4c154d982acd/content)</sup> |
| 主要用途 | 跨 ISA 仿真、遗留迁移、虚拟化、插桩和安全分析<sup>[4](https://www.mdpi.com/2079-9292/12/14/3025)</sup> |
| 当前平台 | 苹果（Apple）Rosetta 2、微软（Microsoft）Prism（Windows 11 24H2）、QEMU、Box64、DynamoRIO、Pin<sup>[8](https://learn.microsoft.com/en-us/windows/arm/apps-on-arm-x86-emulation)</sup><sup> • </sup><sup>[5](https://rrpress.utsa.edu/server/api/core/bitstreams/82e14411-c83e-4c7f-a149-4c154d982acd/content)</sup> |

## 工作原理

动态翻译器在客户机执行代码时发现代码。QEMU 首次遇到目标代码时，会将其翻译到下一个跳转处，或翻译到一条以翻译时无法推断的方式修改静态 CPU 状态的指令为止；这些单元称为翻译块（Translated Blocks，TB），存放在最近使用的 TB 缓存中，缓存满时会被完全清空。<sup>[9](https://static.usenix.org/publications/library/proceedings/usenix05/tech/freenix/full_papers/bellard/bellard.pdf)</sup> QEMU 分三个阶段处理翻译块：查询、翻译和执行；已经翻译过的块会跳过翻译，直接执行。<sup>[4](https://www.mdpi.com/2079-9292/12/14/3025)</sup>

可移植性来自中间表示。QEMU 的 TCG（Tiny Code Generator）先把目标代码翻译成 TCG 指令，再翻译成宿主原生代码；TCG 既有生成中间代码的前端操作，也有在宿主 CPU 上实现的后端操作。<sup>[4](https://www.mdpi.com/2079-9292/12/14/3025)</sup><sup> • </sup><sup>[10](https://airbus-seclab.github.io/qemu_blog/tcg_p1.html)</sup> IR 无法表达的行为，例如 MMU TLB 交互，由辅助函数处理。<sup>[11](https://www.cs.columbia.edu/~luca/research/cota_VEE19.pdf)</sup>

典型的动态翻译器会保存一张源地址到目标地址的映射表，当不存在映射条目时，由切换管理器引导解码；当某个块的执行次数达到阈值时，动态优化会为该热点生成更好的代码。<sup>[12](https://courses.cs.vt.edu/~cs5204/fall07-kafura/Papers/Virtualization/Binary-Translation.pdf)</sup> 缓存块之间的[控制流](https://www.edgepedia.org/zh/control-flow)通过链接（chaining）加速：QEMU 可以修补一个 TB，使其直接跳到下一个 TB，从而在 x86 和 [PowerPC](https://www.edgepedia.org/zh/powerpc) 等宿主上消除查找开销；并且可以通过发出直接或间接的宿主跳转指令，将一个 TB 链接到另一个 TB。<sup>[9](https://static.usenix.org/publications/library/proceedings/usenix05/tech/freenix/full_papers/bellard/bellard.pdf)</sup><sup> • </sup><sup>[13](https://github.com/qemu/qemu/blob/29c042c6/include/exec/translation-block.h)</sup>

## 实现方式

两种方法的区别在于翻译发生的时机以及它们能看到的代码范围。静态二进制翻译试图在执行前一次性翻译整个程序；动态二进制翻译则按需翻译，以基本块为粒度。<sup>[12](https://courses.cs.vt.edu/~cs5204/fall07-kafura/Papers/Virtualization/Binary-Translation.pdf)</sup> 静态方法生成高质量、执行效率高的代码，且其翻译时间不占用程序的执行时间，但它们需要解释器作为回退，并且无法处理自修改代码、代码挖掘或精确中断。<sup>[4](https://www.mdpi.com/2079-9292/12/14/3025)</sup>

对于代码和数据共享内存的冯·诺依曼架构，由于动态链接和自修改代码，静态翻译从来不是完整的解决方案；动态翻译克服了这些问题，却带来了新的问题，因为翻译器必须足够快，使翻译开销低于运行翻译后代码的成本。<sup>[1](https://www.complang.tuwien.ac.at/schani/papers/bintrans.pdf)</sup> 纯粹静态翻译在一般情况下是不可能的，还因为 JIT 编译代码和自修改代码无法处理，且间接跳转需要预测所有可能的目标。<sup>[14](https://db.in.tum.de/teaching/ws2324/codegen/lec12.pdf)</sup> 静态翻译很少用于仿真器，因为翻译计算机可能执行的成千上万个程序并不现实，尽管它适合街机等固定软件系统。<sup>[15](http://xsim.com/papers/Bario.2001.emubook.pdf)</sup>

摊销正是缓存的意义所在：翻译后的代码被存储在翻译缓存中并复用，因此，尽管翻译相比解释执行很慢，但当翻译后的代码执行很多次时，性能会提高，因为解码开销只支付一次。<sup>[15](http://xsim.com/papers/Bario.2001.emubook.pdf)</sup> 翻译本身可能代价高昂：据报道，UQBT 翻译器平均需要 180,000 个机器周期，才能将一字节 Pentium 代码翻译成 SPARC 代码。<sup>[1](https://www.complang.tuwien.ac.at/schani/papers/bintrans.pdf)</sup>

## 起源

二进制翻译源于仿真技术，其开发目的是为从遗留 CISC 机器迁移到更新的 RISC 机器提供一条路径。<sup>[12](https://courses.cs.vt.edu/~cs5204/fall07-kafura/Papers/Virtualization/Binary-Translation.pdf)</sup> DEC 翻译器由理查德·L. 赛茨（Richard L. Sites）及其同事于 1993 年在《ACM 通讯》（Communications of the ACM）上描述。<sup>[16](https://doi.org/10.1145/151220.151227)</sup> VEST 将 OpenVMS VAX 映像翻译为 OpenVMS AXP，mx 将 ULTRIX MIPS 映像翻译为 DEC OSF/1 AXP；二进制翻译只是迁移过程的一半，另一半是运行时环境。<sup>[17](https://web.stanford.edu/class/cs343/resources/binary-translation.pdf)</sup> [惠普](https://www.edgepedia.org/zh/hewlett-packard)（Hewlett-Packard）于 1987 年交付了最早的一批商用系统之一，将客户从 HP 3000 系列迁移到 Precision Architecture；1992 年，[数字设备公司](https://www.edgepedia.org/zh/digital-equipment-corporation)（Digital Equipment Corporation，DEC）交付了从 VAX/VMS、MIPS/Unix、Sparc/Unix 和 x86/NT 到 Alpha 的翻译器；1994 年，苹果（Apple）随 PowerMac 交付了摩托罗拉（Motorola）68000 解释器，后来升级为使用翻译；Code Morphing 软件则在不同底层硬件上翻译并运行 x86 代码。<sup>[2](https://ece.northeastern.edu/groups/nucar/publications/binarytrans.pdf)</sup> FX!32 由 A. 切尔诺夫（A. Chernoff）及其同事于 1998 年在《IEEE Micro》上描述，它最初对程序进行仿真，并在后台利用性能分析信息对其进行静态翻译。<sup>[18](https://doi.org/10.1109/40.671403)</sup><sup> • </sup><sup>[12](https://courses.cs.vt.edu/~cs5204/fall07-kafura/Papers/Virtualization/Binary-Translation.pdf)</sup> Embra 由埃米特·维切尔（Emmett Witchel）和门德尔·罗森布拉姆（Mendel Rosenblum）于 1996 年提出，它将 Shade 中开发的动态翻译技术应用于快速机器仿真。<sup>[12](https://courses.cs.vt.edu/~cs5204/fall07-kafura/Papers/Virtualization/Binary-Translation.pdf)</sup> 后来的里程碑包括全美达（Transmeta）的 Crusoe，一款原生 VLIW 微处理器，带有一个结合了解释器、动态二进制翻译器、优化器和运行时系统的软件层；<sup>[19](http://www.xsim.com/papers/transmeta-code-morphong-software.dehnert-cgo03.pdf)</sup> 英特尔（Intel）的 IA-32 执行层，用于在 Itanium 上运行 IA-32 应用；<sup>[20](https://static.usenix.org/events/vee05/full_papers/p89-jianhui.pdf)</sup> QEMU；<sup>[9](https://static.usenix.org/publications/library/proceedings/usenix05/tech/freenix/full_papers/bellard/bellard.pdf)</sup> 以及 MAMBO-X64，由阿马尼厄·德安特拉斯（Amanieu D'Antras）及其同事于 2017 年描述。<sup>[21](https://doi.org/10.1145/3140587.3062371)</sup>

## 变体

**QEMU** 是构建在 TCG 上的可移植跨 ISA 动态翻译器，可在用户模式和全系统模式下使用。<sup>[9](https://static.usenix.org/publications/library/proceedings/usenix05/tech/freenix/full_papers/bellard/bellard.pdf)</sup> **Dynamo** 是一个透明动态优化系统，其动机是观察到用解释执行的方式运行程序开销太大。<sup>[22](https://homes.cs.washington.edu/~bodik/ucb/cs703-2002/papers/dynamo-full.pdf)</sup> **FX!32** 最初对程序进行仿真，并在后台利用性能分析信息对其进行静态翻译。<sup>[18](https://doi.org/10.1109/40.671403)</sup><sup> • </sup><sup>[12](https://courses.cs.vt.edu/~cs5204/fall07-kafura/Papers/Virtualization/Binary-Translation.pdf)</sup> **IA-32 EL** 是英特尔的商用翻译器，使用三个阶段：解释阶段、快速翻译阶段和针对热点的优化阶段。<sup>[20](https://static.usenix.org/events/vee05/full_papers/p89-jianhui.pdf)</sup> **全美达 Crusoe** 采用软硬件协同设计，使用带硬件提交和回滚的激进推测以及自适应重翻译。<sup>[19](http://www.xsim.com/papers/transmeta-code-morphong-software.dehnert-cgo03.pdf)</sup>

搭载 Apple 芯片的 Mac 上的 **Rosetta 2** 提供两种翻译类型：即时（just-in-time）和提前（ahead-of-time，AOT）。在 AOT 流程中，一个特权用户态实体会使用由安全隔区（Secure Enclave）管理的设备专用密钥对翻译产物签名，翻译产物存储在数据保险库（Data Vault）中；AOT 流程是确定性的，对任意给定输入都会产生完全相同的输出。<sup>[23](https://support.apple.com/guide/security/rosetta-2-on-a-mac-with-apple-silicon-secebb113be1/1/web/1)</sup> **Prism** 的工作方式是将 x86 指令块即时编译为 Arm64 指令，并按模块缓存翻译块，以便其他应用首次启动时复用；它专门针对[高通骁龙](https://www.edgepedia.org/zh/qualcomm-snapdragon)（Qualcomm Snapdragon）处理器调优，部分功能需要骁龙 X 系列硬件。<sup>[8](https://learn.microsoft.com/en-us/windows/arm/apps-on-arm-x86-emulation)</sup>

**Valgrind、DynamoRIO 和 Pin** 是面向插桩的动态翻译器；DynamoRIO 和 Pin 在原生运行 x86_64 客户机代码时表现最好，但由于 DBT 机制本身，仍会产生超过 1.2 倍的减速。<sup>[5](https://rrpress.utsa.edu/server/api/core/bitstreams/82e14411-c83e-4c7f-a149-4c154d982acd/content)</sup> 研究系统包括 **HQEMU**，它在 QEMU 上增加了基于 LLVM 的优化；<sup>[24](https://www.cs.nthu.edu.tw/~ychung/Journal/2014-IEEE-TPDS-Hong.pdf)</sup> 基于 LLVM 的翻译器 **Instrew**；<sup>[7](https://home.cit.tum.de/~engelke/pubs/2104-vee.pdf)</sup> 面向 ARM 到 AArch64 的 **MAMBO-X64**；<sup>[6](https://www.dcs.gla.ac.uk/%7Ejosecr/pub/2016_iiswc.pdf)</sup> 以及 **Qelt**，一个基于 QEMU 的跨 ISA 全系统插桩工具，可扩展到 32 核。<sup>[11](https://www.cs.columbia.edu/~luca/research/cota_VEE19.pdf)</sup>

## 应用

二进制翻译用于跨 ISA 仿真和遗留迁移，例如 DEC、惠普、苹果和 IA-32 EL 系统，<sup>[2](https://ece.northeastern.edu/groups/nucar/publications/binarytrans.pdf)</sup><sup> • </sup><sup>[20](https://static.usenix.org/events/vee05/full_papers/p89-jianhui.pdf)</sup> 并在遗留软件迁移、程序行为分析和系统虚拟化中有应用。<sup>[4](https://www.mdpi.com/2079-9292/12/14/3025)</sup> 同一套机制通过 Valgrind、DynamoRIO、Pin 和 Qelt 服务于插桩和安全分析。<sup>[5](https://rrpress.utsa.edu/server/api/core/bitstreams/82e14411-c83e-4c7f-a149-4c154d982acd/content)</sup><sup> • </sup><sup>[11](https://www.cs.columbia.edu/~luca/research/cota_VEE19.pdf)</sup> 苹果通过 Virtualization.framework 将 [Rosetta 2](https://www.edgepedia.org/zh/rosetta-2) 扩展到 Linux 虚拟机，其中内核是 ARM64，而用户空间文件系统是 x86-64；Docker Desktop 和 OrbStack 使用这项技术在搭载 Apple 芯片的 Mac 上运行 x86-64 容器。<sup>[25](http://blog.inoki.cc/2026/02/28/Apple-Rosetta-Linux-VM-Secret-en/)</sup>

## 局限与替代方案

翻译后的程序比解释器或仿真器快得多，但比原生编译的程序慢；原生编译的程序若使用调优良好的优化编译器，可以比其他任何方案都快得多；与解释执行或仿真的代码不同，二进制翻译后的代码接近目标机器上原生代码的速度。<sup>[17](https://web.stanford.edu/class/cs343/resources/binary-translation.pdf)</sup><sup> • </sup><sup>[26](https://www.cs.tufts.edu/~nr/pubs/xtoplas-acm.pdf)</sup> 即使像延迟分支这样普通的 ISA 特性也会使翻译复杂化：沿后继边推入延迟槽指令的朴素变换，在延迟槽指令本身就是控制转移时会失败。<sup>[26](https://www.cs.tufts.edu/~nr/pubs/xtoplas-acm.pdf)</sup>

自修改代码是一个长期存在的危险：如果程序修改了已经翻译过的代码，而翻译器没有察觉，就会执行过时的翻译版本；在相同地址卸载并替换共享库是一种特殊情况。<sup>[1](https://www.complang.tuwien.ac.at/schani/papers/bintrans.pdf)</sup> 有界翻译在那些把许可证检查代码写入内存再跳转过去的程序上会彻底失败，这就是为什么 DEC 的 VEST 和 mx 是开放式、全自动的系统。<sup>[17](https://web.stanford.edu/class/cs343/resources/binary-translation.pdf)</sup>

正确仿真所有系统调用很困难，原因包括版本细节、结构布局，以及 /proc/self/* 和 cpuid 等庞大接口；应用还可以通过计时实验检测 DBT。<sup>[14](https://db.in.tum.de/teaching/ws2324/codegen/lec12.pdf)</sup> 内核模式代码是一道硬边界：[Windows on Arm](https://www.edgepedia.org/zh/windows-on-arm) 仿真仅支持用户模式，不支持驱动程序；驱动程序必须编译为 Arm64。<sup>[8](https://learn.microsoft.com/en-us/windows/arm/apps-on-arm-x86-emulation)</sup> 性能断崖来自间接分支、软浮点、MMU 仿真和系统调用密集的工作负载，而基于时序的检测意味着翻译后的代码在行为上并非不可见。<sup>[14](https://db.in.tum.de/teaching/ws2324/codegen/lec12.pdf)</sup>

## References

1. [Dynamic Binary Translation (Schani, TU Wien)](https://www.complang.tuwien.ac.at/schani/papers/bintrans.pdf)
2. [Welcome to the opportunities of binary translation (Computer, guest editors' introduction)](https://ece.northeastern.edu/groups/nucar/publications/binarytrans.pdf)
3. [A System-Level Dynamic Binary Translator using Automatically-Learned Translation Rules (arXiv, 2024)](https://arxiv.org/html/2402.09688v1)
4. [A Dynamic and Static Binary Translation Method Based on Branch Prediction (Electronics, MDPI, 2023)](https://www.mdpi.com/2079-9292/12/14/3025)
5. [An Instruction Inflation Analyzing Framework for Dynamic Binary Translators (ACM TACO 2024)](https://rrpress.utsa.edu/server/api/core/bitstreams/82e14411-c83e-4c7f-a149-4c154d982acd/content)
6. [Low overhead dynamic binary translation on ARM (MAMBO-X64) / Quantitative Characterization of a HW/SW Co-Designed Processor's TOL (dossiers label this URL as two different papers; see disagreements)](https://www.dcs.gla.ac.uk/%7Ejosecr/pub/2016_iiswc.pdf)
7. [Efficient LLVM-Based Dynamic Binary Translation (Instrew, VEE 2021)](https://home.cit.tum.de/~engelke/pubs/2104-vee.pdf)
8. [How emulation works on Arm (Microsoft Learn)](https://learn.microsoft.com/en-us/windows/arm/apps-on-arm-x86-emulation)
9. [QEMU, a Fast and Portable Dynamic Translator (Fabrice Bellard, USENIX 2005)](https://static.usenix.org/publications/library/proceedings/usenix05/tech/freenix/full_papers/bellard/bellard.pdf)
10. [A deep dive into QEMU: The Tiny Code Generator (TCG), part 1 (Airbus SECLab)](https://airbus-seclab.github.io/qemu_blog/tcg_p1.html)
11. [Cross-ISA Machine Instrumentation using Fast and Scalable Dynamic Binary Translation (Qelt, VEE 2019)](https://www.cs.columbia.edu/~luca/research/cota_VEE19.pdf)
12. [Machine-Adaptable Dynamic Binary Translation (UQBT paper)](https://courses.cs.vt.edu/~cs5204/fall07-kafura/Papers/Virtualization/Binary-Translation.pdf)
13. [qemu/qemu include/exec/translation-block.h](https://github.com/qemu/qemu/blob/29c042c6/include/exec/translation-block.h)
14. [Code Generation for Data Processing, Lecture 12: Binary Translation (TU Munich, WS 23/24)](https://db.in.tum.de/teaching/ws2324/codegen/lec12.pdf)
15. [Study of the Techniques for Emulation (Victor Moya del Barrio, 2001)](http://xsim.com/papers/Bario.2001.emubook.pdf)
16. [Richard L. Sites and colleagues (1993). Binary translation. Communications of the ACM.](https://doi.org/10.1145/151220.151227)
17. [Binary Translation (Sites, Chernoff, Kirk, Marks, Robinson; Digital Equipment Corporation, Communications of the ACM, February 1993)](https://web.stanford.edu/class/cs343/resources/binary-translation.pdf)
18. [A. Chernoff and colleagues (1998). FX!32 a profile-directed binary translator. IEEE Micro.](https://doi.org/10.1109/40.671403)
19. [The Transmeta Code Morphing Software (Dehnert et al., CGO 2003)](http://www.xsim.com/papers/transmeta-code-morphong-software.dehnert-cgo03.pdf)
20. [Module-aware Translation for Real-life Desktop Applications (IA-32 EL, VEE 2005)](https://static.usenix.org/events/vee05/full_papers/p89-jianhui.pdf)
21. [Amanieu D'Antras and colleagues (2017). Low overhead dynamic binary translation on ARM. ACM SIGPLAN Notices.](https://doi.org/10.1145/3140587.3062371)
22. [Transparent Dynamic Optimization: The Design and Implementation of Dynamo](https://homes.cs.washington.edu/~bodik/ucb/cs703-2002/papers/dynamo-full.pdf)
23. [Rosetta 2 on a Mac with Apple silicon - Apple Platform Security](https://support.apple.com/guide/security/rosetta-2-on-a-mac-with-apple-silicon-secebb113be1/1/web/1)
24. [Hybrid-QEMU (HQEMU), IEEE TPDS 2014](https://www.cs.nthu.edu.tw/~ychung/Journal/2014-IEEE-TPDS-Hong.pdf)
25. [Things You Might Want to Know About Apple's Rosetta 2 for Linux VMs](http://blog.inoki.cc/2026/02/28/Apple-Rosetta-Linux-VM-Secret-en/)
26. [A Transformational Approach to Binary Translation of Delayed Branches (Ramsey & Cifuentes, ACM TOPLAS)](https://www.cs.tufts.edu/~nr/pubs/xtoplas-acm.pdf)

---
*Initially written Sep 29, 2026 · Edited: Oct 15, 2026; Oct 21, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Compilers, interpreters, and toolchains

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/binary-translation

English original: https://www.edgepedia.org/binary-translation
