APP Startup Process

After the App starts, the Kernel first creates a process. Next, load the executable file. (An executable is a mach-O file, which is a collection of all the.o files in your App.) At this point, you get the path to dyld (Dyld is Apple’s dynamic linker). Then, load dyLD, mainly divided into 4 steps: 1. Load dylibs: At this stage dyLD analyzes the dylib on which the application depends, finds its Mach-o files, opens and reads these files and verifies their validity, then finds the code signature to register with the kernel, and finally calls mmap() for each segment of dylib. 2. Rebase /bind: Rebase pointer adjustment and bind symbol. 3. ObjC Setup: Runtime initialization. Including registration of ObjC related classes, category registration, selector uniqueness check, and so on. Initializers: call the +load method of each ObjC class and class, call attribute((((constructor)) modified functions, and create C++ static global variables.

APP Compilation process

www.jianshu.com/p/d94696155…

Compile the preceding Clang

  1. Preprocessing stage: macro expansion, header file import
  2. Compilation stage ① lexical analysis ② syntax analysis (abstract syntax tree AST) ③ static analysis, will make errors to the syntax tree, such as method is called but not defined, defined but not used variables, and type check (AST -> IR) ④ generate intermediate code IR

Let’s go to the compiler back end LLVM

  1. The generated assembly code assembler LLVM converts the assembly code into machine code. The code in this case is the.o file, or binary file
  2. Runtime linking: The linker takes the compiled. O files and the (dylib,a, TBD) files to create a Mach-o file. Mach-o file-level executables. The compilation process is complete and the executable file Mach -o is generated

Pretreatment: Clang preprocessing your code, such as the macro embedded in the corresponding position, annotations are deleted, conditional compilation is processed lexical analysis: lexical analyzer to read the source file character stream, will they said meaningful morphemes (lexeme) sequence, for each morpheme, the lexical analyzer generated lexical unit (token) as output. And it will use Loc to record the location. Parsing: This step parses the stream of tokens generated by parsing into an abstract syntax tree (AST), in which each node is also marked for its position in the source code. AST is an abstract syntax tree that is structurally leaner than code and faster to traverse, so using AST allows for faster static checking. Static analysis: After converting the source code into an abstract syntax tree, the compiler can statically analyze the tree. Static analysis improves code quality by checking for errors such as methods that are called but not defined, variables that are defined but not used, etc. Of course, you can also perform manual analysis using Xcode’s own static analysis tool (Product -> Analyze). Finally, THE AST generates IR, which is a language that is more similar to machine code, except that it is platform-independent and multiple machine codes can be generated from IR for different platforms. The static analysis phase does type checking, such as setting a property to an object that does not match its own type, and the compiler gives a warning that it may be used incorrectly. This phase also checks for unused variables and so on. Intermediate code generation and optimization: LLVM will compile and optimize the code at this stage, such as global variable optimization, loop optimization, tail recursive optimization, etc., and finally output assembly code xx.ll file. Generate assembly code: the assembler LLVM converts the assembly code to machine code. The code in this case is the.o file, or binary file. Linker takes the compiled. O files and the (dylib,a, TBD) files to create a Mach-o file. Mach-o file-level executables. The compilation process is complete and the executable file Mach -o is generated