【iOS】Compilation and linking process

Preface

Computer languages ​​are divided into: machine language, assembly language and high-level language.
High-level languages ​​can be divided into: editing languages ​​and interpreted languages.

interpreted language

Every time a program written in an interpreted language is run, the program needs to be dynamically interpreted and executed by the interpreter, that is, one code is interpreted and one code is executed.

Advantages: It is portable, because it only needs an interpreter for various systems to run, and does not require messy system library support.

Disadvantages: The execution speed is slow because there is an extra translation process compared to direct execution.

Typical languages: php, javascript

compiled language

As long as a compiled language program is compiled by a compiler, it can be run directly every time the program is run without the need to "translate" it again.

Advantages: fast execution

Disadvantages: Poor portability, because compilation requires linking to the operating system libraries, so specific system libraries need to be used when the program is running.

Typical languages: objective-c, C++

File extension

During the iOS compilation and linking process, different file extensions represent different file types and intermediate processes.

`.i `文件:`.i `文件是预处理器处理源代码之后生成的文件,其中包含了宏展开、条件编译等预处理操作后的代码。

`.s` 文件:`.s` 文件是汇编器生成的汇编代码文件。它将预处理器生成的 .i 文件翻译成机器指令的文本表示形式,每个汇编指令对应一条机器指令。

`.o` 文件:`.o` 文件是编译器生成的目标文件,也被称为对象文件。它包含了汇编器生成的机器指令以及一些符号表、重定位信息和其他调试信息。

`.m` 文件:`.m` 文件是 Objective-C 源代码文件的常见扩展名。它包含了 Objective-C 的代码,可以与 C 和 C++ 代码混合使用。

These files are generated at different stages in the compilation and linking process, and are converted and passed to each other throughout the process, ultimately generating executable files or library files.

Compilation and linking process

OC is a compiled language that uses the Low Level Virtual Machine's compiler development tool suite (LLVM) to generate machine code. The machine code can be executed directly on the CPU and is more efficient.

LLVM is a project that provides a broad set of tools that can compile code in any high-level language into machine code that can be run on any architecture of CPU. It classifies the entire compilation process into three modules: front-end, public optimizer, and back-end. clang is a front-end of LLVM. Its function is to compile for languages ​​​​of the C language family, such as c, c++, and Objective-C. Swift implements its own front-end for Swift compilation, and the optimizer and back-end are still completed using LLVM.

  1. Preprocessing: processing macro macros (such as #define), import header file replacement and processing other precompilation instructions to generate .i files. (all start with #)
  2. Compilation: Perform a series of lexical, grammatical, and semantic analyzes on a series of preprocessed files, and generate corresponding assembly code after optimization to generate .s files.
  3. Assembly: The assembler generates machine instructions from the assembly code, outputs the object file, and generates the .o file. (Just translate them one by one according to the comparison table between assembly instructions and machine instructions)
  4. Linking: There may be other files in one file. Therefore, it is also necessary to combine the target files generated by compilation and the files provided by the system. This process is linking. After linking, an executable file is finally generated.

After compilation and linking, the written code is converted into binary instructions that the computer can recognize.

Detailed explanation

The entire compilation and linking process of iOS is as shown in the figure
Insert image description here

preprocessing

Process macro macros, replace import header files and process other precompilation instructions to generate .i files (all starting with #!). The rules are as follows:

  1. "#define" deletes and expands the corresponding macro definition.
  2. Handles all conditional precompilation directives. Such as #if/#ifdef/#else/#endif.
  3. Files included with "#include/#import" are recursively inserted here.
  4. Remove all comments "// or /**/".
  5. Add line number and file name identification, which will be used for compilation and debugging.

compile

This process is to perform the above main.i file: lexical analysis, syntax analysis, static analysis, optimize and generate the corresponding assembly code, and finally generate the main.s file.

  1. Lexical analysis: The task of lexical analysis is to read the input characters from the source code, divide them into a series of words (tokens) according to the word formation rules, and submit them to grammatical analysis. The analysis result of the lexical analyzer, that is, the output is a token sequence, where the token contains two parts, namely token = type enumeration + attribute value. Token is also called a "morpheme", which refers to a single meaningful element.
  2. Syntax analysis: Grammar analysis takes the Token stream output by the lexical analysis program as input, analyzes the grammatical structure of the source program, determines whether it is a legal program of the corresponding programming language, and finally generates a syntax analysis tree that conforms to the specific grammar.
  3. Static analysis: uses the information in the syntax tree and symbol table to check whether the source program is consistent with the semantics defined by the language. It also collects type information and stores this information in the syntax tree or symbol table for subsequent intermediate code generation. use.
  4. Code generation: LLVM will compile and optimize the code, such as global variable optimization, loop optimization, tail recursion optimization, etc., and finally output assembly code. Then generate assembly language that depends on the specific machine based on the intermediate language, and optimize the assembly language.

compilation

Compile the main.s file into the main.o file. (That is what we often call the target file)

This process is to translate the assembly instructions in the main.s file obtained above into machine instructions, and finally generate main.o.

Link

This process is to compile main.o into the corresponding Mach-O file, which is what we often call an executable file. The essence of linking is to combine one or more target files and the required libraries (static libraries/dynamic libraries, if necessary) into one file (Mach-O executable file).

Guess you like

Origin blog.csdn.net/m0_63852285/article/details/131760234