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.)
- 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
- 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
- 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
- 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
- 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.
相關文章
- Android resource linking failedAndroidAI
- Content Linking元件[Asp]元件
- Snippet CompilerCompile
- iOS Deep Linking 最佳實踐iOS
- Compiler.phpCompilePHP
- aspnet_compilerCompile
- Algorithms for Compiler DesignGoCompile
- COMPILER simplified C programminCompile
- Design Compiler(一)Compile
- iOS Deep Linkin 和 Deferred Deep LinkingiOS
- React Native Linking跨app的通訊方法React NativeAPP
- HTML Compiler 2022HTMLCompile
- V8 之旅:FULL COMPILERCompile
- webpack 流程解析(1):建立compiler之前WebCompile
- Compiler Explorer(Godbolt) 使用經驗CompileGo
- 編譯程式(compiler)的簡單分析編譯Compile
- Building OpenNI using a cross-compilerUIROSCompile
- Pre-defined C/C++ Compiler MacrosC++CompileMacROS
- Design Compiler多時鐘約束Compile
- Use any C++ Compiler with Visual StudioC++Compile
- 解決Cannot find module '@angular/compiler-cli'AngularCompile
- Could not initialize class org.apache.xpath.compiler.FunctionTableApacheCompileFunction
- ASPNet_Compiler的編譯過程Compile編譯
- JDK6.0的新特性:使用Compiler APIJDKCompileAPI
- cgo: C compiler "gcc" not found 報錯安裝gccGoCompileGC
- configure: error: C++ compiler cannot create executablesErrorC++Compile
- AIX 6.1安裝apache報no acceptable C compiler foundAIApacheCompile
- java.lang.LinkageError: loader constraints violated when linking javax/el/ExpresJavaErrorAI
- 淺析webpack原始碼之Compiler.js模組(八)Web原始碼CompileJS
- Jx.Cms開發筆記(六)-重寫Compiler筆記Compile
- Maven 教程(21)— maven-compiler-plugin 外掛詳解MavenCompilePlugin
- 程式碼來構建一個簡單的compilerCompile
- maven-compiler-plugin外掛引數配置詳解MavenCompilePlugin
- JDK6.0的新特性之四:使用Compiler APIJDKCompileAPI
- 華為分析+App Linking:一站式解決拉新、留存、促活難APP
- Swoole Compiler 加密 Drupal 產生的一些問題Compile加密
- 【小生雜談】ASP.Net_Compiler的編譯過程ASP.NETCompile編譯
- TVM Compiler中文教程:TVM排程原語(Schedule Primitives)CompileMIT