说明
本文是个人学习用的非官方中英对照译文,原文:https://diveintosystems.org/book/C2-C_depth/advanced_assembly.html。
原书采用 CC BY-NC-ND 4.0 许可(禁止演绎),请勿公开传播本译文。
标为「译注」「现代更新」的内容是译者补充,不代表原书作者观点;代码与原文逐字一致,未作改动。
2.9.7. Compiling C to Assembly, and Compiling and Linking Assembly and C Code
编译器可以把 C 代码编译成汇编代码,也可以把汇编代码编译成二进制形式,再链接成二进制可执行程序。我们以 IA32 汇编和 gcc 作为示例汇编语言和编译器,但任何 C 编译器都支持这一功能,而且大多数编译器支持编译成多种不同的汇编语言。关于汇编代码和汇编编程的细节,请参见第 8 章。
A compiler can compile C code to assembly code, and it can compile assembly code into a binary form that links into a binary executable program. We use IA32 assembly and
gccas our example assembly language and compiler, but this functionality is supported by any C compiler, and most compilers support compiling to a number of different assembly languages. See Chapter 8 for details about assembly code and assembly programming.
来看这个非常简单的 C 程序:
Consider this very simple C program:
simpleops.c
simpleops.c
1 | int main(void) { |
使用 -S 命令行选项指定编译成汇编,并用 -m32 命令行选项指定生成 IA32 汇编,gcc 编译器就会把它编译成一个 IA32 汇编文本文件(.s):
The
gcccompiler will compile it into an IA32 assembly text file (.s) using the-Scommand line option to specify compiling to assembly and the-m32command line option to specify generating IA32 assembly:
译注
代码注释说 -S「运行汇编器生成 .s 文本文件」,严格说不对:-S 让编译在生成汇编代码之后就停止,汇编器(把 .s 变成 .o 的那一步)并没有运行。
在 Apple Silicon Mac 上:-m32 不会生成 IA32 代码。译者用 Apple Clang 试过:gcc -m32 -S 生成的是 32 位 ARM 汇编,而 gcc -m32 -o ... 会因为系统没有 32 位库而链接失败。如果只想看 x86 汇编,可以让 Clang 交叉生成:gcc -S -target i386-linux-gnu simpleops.c 得到 IA32 汇编,-target x86_64-linux-gnu 得到 x86-64 汇编;但要链接和运行,仍需要真正的 x86 Linux 环境。
1 | $ gcc -m32 -S simpleops.c # runs the assembler to create a .s text file |
这条命令会生成一个名为 simpleops.s 的文件,内容是编译器把 C 代码翻译成的 IA32 汇编。由于 .s 文件是文本文件,用户可以用任意文本编辑器查看(和编辑)它。例如:
This command creates a file named
simpleops.swith the compiler’s IA32 assembly translation of the C code. Because the.sfile is a text file, a user can view it (and edit it) using any text editor. For example:
1 | $ vim simpleops.s |
传入额外的编译器标志,可以指示 gcc 在把 C 翻译成 IA32 汇编代码时使用某些特性或优化。
Passing additional compiler flags provides directions to
gccthat it should use certain features or optimizations in its translation of C to IA32 assembly code.
汇编代码文件,无论是由 gcc 生成的,还是程序员手写的,都可以用 -c 选项由 gcc 编译成二进制机器码形式:
An assembly code file, either one generated from
gccor one written by hand by a programmer, can be compiled bygccinto binary machine code form using the-coption:
1 | $ gcc -m32 -c simpleops.s # compiles to a relocatable object binary file (.o) |
生成的 simpleops.o 文件随后可以链接成二进制可执行文件(注意:这要求你的系统上安装了 32 位版本的系统库):
The resulting
simpleops.ofile can then be linked into a binary executable file (note: this requires that the 32-bit version of the system libraries are installed on your system):
1 | $ gcc -m32 -o simpleops simpleops.o # creates a 32-bit executable file |
这条命令生成一个适用于 IA32(以及 x86-64)体系结构的二进制可执行文件 simpleops。
This command creates a binary executable file,
simpleops, for IA32 (and x86-64) architectures.
构建可执行文件的 gcc 命令行中可以同时包含 .o 和 .c 文件,它们会被编译并链接在一起,生成单个二进制可执行文件。
The
gcccommand line to build an executable file can include.oand.cfiles that will be compiled and linked together to create the single binary executable.
系统提供了让用户查看二进制文件的工具。例如,objdump 可以显示 .o 文件中机器码与汇编代码的对应关系:
Systems provide utilities that allow users to view binary files. For example,
objdumpdisplays the machine code and assembly code mappings in.ofiles:
1 | $ objdump -d simpleops.o |
可以把这一输出与汇编文件对比:
This output can be compared to the assembly file:
1 | $ cat simpleops.s |
你应该会看到类似下面的内容(我们给部分汇编代码标注了对应的 C 程序代码):
You should see something like this (we’ve annotated some of the assembly code with its corresponding code from the C program):
1 | .file "simpleops.c" |
编写和编译汇编代码
Writing and Compiling Assembly Code
程序员可以手写自己的汇编代码,再用 gcc 编译成二进制可执行程序。例如,要用汇编实现一个函数,就把代码写进一个 .s 文件,然后用 gcc 编译。下面的例子展示了 IA32 汇编中函数的基本结构。对于原型为 int myfunc(int param); 的函数,这样的代码会写在一个文件中(例如 myfunc.s)。参数更多或需要更多局部变量空间的函数,其开头的准备代码(preamble)可能会略有不同。
Programmers can write their own assembly code by hand and compile it with
gccinto a binary executable program. For example, to implement a function in assembly, add code to a.sfile and usegccto compile it. The following example shows the basic structure of a function in IA32 assembly. Such code would be written in a file (e.g.,myfunc.s) for a function with the prototypeint myfunc(int param);. Functions with more parameters or needing more space for local variables may differ slightly in their preamble code.
1 | .text # this file contains instruction code |
想要调用这个函数的 C 程序需要包含它的函数原型:
A C program that wanted to call this function would need to include its function prototype:
1 | #include <stdio.h> |
下面的 gcc 命令用 myfunc.s 和 main.c 两个源文件构建出可执行文件(myprog):
The following
gcccommands build an executable file (myprog) frommyfunc.sandmain.csource files:
1 | $ gcc -m32 -c myfunc.s |
原书旁注 · 手写汇编代码(Handwritten Assembly Code)
C 是一种高级语言,可以在各种各样的系统上编译和运行;汇编代码则不同,它非常底层,并且特定于某种硬件体系结构。程序员可能会为底层函数或对软件性能至关重要的代码序列手写汇编代码。程序员有时能写出比编译器优化后的 C 翻译结果运行得更快的汇编代码;有时 C 程序员也想在代码中访问底层体系结构的某些部分(比如特定的寄存器)。出于这些原因,操作系统代码中有一小部分常用汇编实现。不过,由于 C 是可移植的语言,又比汇编语言高级得多,绝大部分操作系统代码是用 C 编写的,依靠优秀的优化编译器生成性能良好的机器码。
尽管大多数系统程序员很少编写汇编代码,但能读懂并理解程序的汇编代码,是深入理解程序做了什么、如何执行的一项重要技能。它还有助于理解程序的性能,以及发现和理解程序中的安全漏洞。
Unlike C, which is a high-level language that can be compiled and run on a wide variety of systems, assembly code is very low level and specific to a particular hardware architecture. Programmers may handwrite assembly code for low-level functions or for code sequences that are crucial to the performance of their software. A programmer can sometimes write assembly code that runs faster than the compiler-optimized assembly translation of C, and sometimes a C programmer wants to access low-level parts of the underlying architecture (such as specific registers) in their code. Small parts of operating system code are often implemented in assembly code for these reasons. However, because C is a portable language and is much higher level than assembly languages, the vast majority of operating system code is written in C, relying on good optimizing compilers to produce machine code that performs well.
Although most systems programmers rarely write assembly code, being able to read and understand a program’s assembly code is an important skill for obtaining a deeper understanding of what a program does and how it gets executed. It can also help with understanding a program’s performance and with discovering and understanding security vulnerabilities in programs.