Compiler Drivers+Static Linking

kking_edc發表於2020-12-01

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.

相關文章