Compiler Drivers+Static Linking

Compiler Drivers

Consider the C program in Figure 7.1. It consists of two source files, main.c and swap.c. Function main() calls swap, which swaps the two elements in the external global array buf. Granted, this is a strange way to swap two numbers, but it will serve as a small running example throughout this chapter that will allow us to make some important points about how linking works.

在这里插入图片描述
Most compilation systems provide a compiler driver that invokes the language preprocessor, compiler, assembler, and linker, as needed on behalf of the user. For example, to build the example program using the GNU compilation system, we might invoke the gcc driver by typing the following command to the shell:
unix> gcc -O2 -g -o p main.c swap.c

Figure 7.2 summarizes the activities of the driver as it translates the example program from an ASCII source file into an executable object file. (If you want to see these steps for yourself, run gcc with the -v option.)
在这里插入图片描述

  1. The driver first runs the C preprocessor (cpp), which translates the C source file main.c into an ASCII intermediate file main.i:
    cpp [other arguments] main.c /tmp/main.i
  2. Next, the driver runs the C compiler (cc1), which translates main.i into an ASCII assembly language file main.s.
    cc1 /tmp/main.i main.c -O2 [other arguments] -o /tmp/main.s
  3. Then, the driver runs the assembler (as), which translates main.s into a relocatable object file main.o:
    as [other arguments] -o /tmp/main.o /tmp/main.s
  4. The driver goes through the same process to generate swap.o. Finally, it runs the linker program ld, which combines main.o and swap.o, along with the necessary system object files, to create the executable object file p:ld -o p [system object files and args] /tmp/main.o /tmp/swap.o
  5. To run the executable p, we type its name on the Unix shell’s command line:
    unix> ./p
    The shell invokes a function in the operating system called the loader, which copies the code and data in the executable file p into memory, and then transfers control to the beginning of the program.

Static Linking

Static linkers such as the Unix ld program take as input a collection of relocatable object files and command-line arguments and generate as output a fully linked executable object file that can be loaded and run. The input relocatable object files consist of various code and data sections. Instructions are in one section, initialized global variables are in another section, and uninitialized variables are in yet another section.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值