LLVM Clang前端编译与调试
LLVM Clang前端編譯與調試
iOS 關于編譯
o 一、Objective-C 編譯過程
o 為什么需要重新編譯?
o 編譯步驟
o 二、編譯步驟的詳細說明
o 1.預處理
o 2.編譯
o 詞法分析
o 語法分析
o clang static analyzer
o 3.生成
o LLVM IR
o LLVM Backend
o 三、編譯完成生成的文件
o Link Map File
o dSYM 文件
o Mach-O
o 四、總結
o 五、推薦學習
技術學習有兩種方向,一種是不斷向前,了解前沿和趨勢;另一種是不斷向下,理解通用的底層技術和設計思想。這兩種方法沒有優劣之分,更應該是一種不斷交替使用的方式。
編譯器相關的內容,在不斷學習底層知識的過程中梳理一下內容。
一、Objective-C 編譯過程
在說編譯之前,先說明幾個概念:
? LLVM:Low Level Virtual Machine,由 Chris Lattner(Swift 作者) 用于 Objective-C 和 Swift 的編譯,后來加上了很多功能可用于常規編譯器、JIT 編譯器、調試器、靜態分析工具等??偨Y來說,LLVM 是工具鏈技術與一個模塊化和可重用的編譯器的集合。
? Clang:是 LLVM 的子項目,可以對 C、C++和 Objective-C 進行快速編譯,編譯速度比 GCC 快 3 倍。Clang 可以認為是 Objective-C 的編譯前端,LLVM 是編譯后端,前端調用后端接口完成任務。Swift有編譯前端 SIL optimizer,編譯后端同樣用的是 LLVM。
? AST:抽象語法樹,按照層級關系排列。
? IR:中間語言,具有與語言無關的特性,整體結構為 Module(一個文件)–Function–Basic Block–Instruction(指令)。
? 編譯器:編譯器用于把代碼編譯成機器碼,機器碼可以直接在 CPU 上面運行。好處是運行效率高,壞處是調試周期長,需要重新編譯一次(OC 改完代碼需要重新運行)。
? 解釋器:解釋器會在運行時解釋執行代碼,會一段一段的把代碼翻譯成目標代碼,然后執行目標代碼。好處是具有動態性,調試及時(類似 Flutter、Playground),壞處是執行效率低。平時在調試代碼的時候,使用解釋器會提供效率。
為什么需要重新編譯?
首先先來問個問題,為什么 Objective-C 代碼每次修改之后,都要重新編譯才能運行到機子上,JavaScript可以做到動態調試,不需要重新編譯?
答案是 Objective-C 使用編譯器的方式生成機器碼,JavaScript使用解釋器的方式。因為蘋果公司希望 iPhone 的執行效率更高、運行速度能達到最快,選擇犧牲調試周期,放棄動態性。
是不是真的不能動態調試了?不是,Objective-C 有一種加載動態庫的機制,這就為動態調試留下了機會,具體的例子可以看這里https://github.com/johnno1962/InjectionIII。Injection 原理是將代碼打包成動態庫,如果發現代碼有做更改,使用 dlopen 重新進行動態庫的加載。
編譯步驟
接下來,說一下編譯的過程,將這個過程簡單分成以下 3 步:
- 預處理:編譯開始時,LLVM 會預處理代碼,比如宏替換、頭文件導入;
- 編譯:預處理完后,LLVM 會對代碼進行詞法分析和語法分析(Clang),生成 AST。AST是抽象語法樹,結構上比代碼更精簡,遍歷更快,使用 AST 能夠更快速地進行靜態檢查,更快地生成 IR。
- 生成:最后 AST 會生成 IR,IR 是一種更接近機器碼的語言,區別在于和平臺無關,通過 IR 可以生成多份適合不同平臺的機器碼。對于 iOS 系統,IR 生成的可執行文件就是 Mach-O。
用代碼來詳細解釋一下這 3 個過程。
二、編譯步驟的詳細說明
1.預處理
新建一個工程,在 mian 中寫出如下代碼: - #import <Foundation/Foundation.h>
- #define DefineEight 8
- int main(int argc, char * argv[]) {
-
@autoreleasepool { -
int i = DefineEight; -
int j = 6; -
NSString *string = [[NSString alloc] initWithUTF8String:"clang"]; -
int rank = i + j; -
NSLog(@"%@ rank %d",string, rank); -
} -
return 0; - }
編譯的預處理階段會替換宏,導入頭文件等,上面的main.m預處理到底做了什么。在命令行中輸入:
- clang -E main.m
執行之后控制臺輸出:
-
1 “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/FoundationLegacySwiftCompatibility.h” 1 3
-
193 “/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h” 2 3
-
10 “main.m” 2
- int main(int argc, char * argv[]) {
-
@autoreleasepool { -
int i = 8; -
int j = 6; -
NSString *string = [[NSString alloc] initWithUTF8String:"clang"]; -
int rank = i + j; -
NSLog(@"%@ rank %d",string, rank); -
} -
return 0; - }
宏已經替換修改,頭文件引入變成了相關文件地址。
2.編譯
詞法分析
編譯階段,Clang先進行詞法分析,在命令行輸入:
- clang -fmodules -fsyntax-only -Xclang -dump-tokens main.m
- clang -fmodules -E -Xclang -dump-tokens main.m
打印信息如下:
- annot_module_include '#import <Foundation/Foundation.h>
- #define DefineEight 8
- int main(int argc, char * argv[]) {
-
@autoreleasepool { -
int i = DefineEight; -
int' Loc=<main.m:9:1> - int ‘int’ [StartOfLine] Loc=main.m:12:1
- identifier ‘main’ [LeadingSpace] Loc=main.m:12:5
- l_paren ‘(’ Loc=main.m:12:9
- int ‘int’ Loc=main.m:12:10
- identifier ‘argc’ [LeadingSpace] Loc=main.m:12:14
- comma ‘,’ Loc=main.m:12:18
- char ‘char’ [LeadingSpace] Loc=main.m:12:20
- star ‘*’ [LeadingSpace] Loc=main.m:12:25
- identifier ‘argv’ [LeadingSpace] Loc=main.m:12:27
- l_square ‘[’ Loc=main.m:12:31
- r_square ‘]’ Loc=main.m:12:32
- r_paren ‘)’ Loc=main.m:12:33
- l_brace ‘{’ [LeadingSpace] Loc=main.m:12:35
- at ‘@’ [StartOfLine] [LeadingSpace] Loc=main.m:14:5
- identifier ‘autoreleasepool’ Loc=main.m:14:6
- l_brace ‘{’ [LeadingSpace] Loc=main.m:14:22
- int ‘int’ [StartOfLine] [LeadingSpace] Loc=main.m:15:9
- identifier ‘i’ [LeadingSpace] Loc=main.m:15:13
- equal ‘=’ [LeadingSpace] Loc=main.m:15:15
- numeric_constant ‘8’ [LeadingSpace] Loc=<main.m:15:17 <Spelling=main.m:10:22>>
- semi ‘;’ Loc=main.m:15:28
- int ‘int’ [StartOfLine] [LeadingSpace] Loc=main.m:16:9
- identifier ‘j’ [LeadingSpace] Loc=main.m:16:13
- equal ‘=’ [LeadingSpace] Loc=main.m:16:15
- numeric_constant ‘6’ [LeadingSpace] Loc=main.m:16:17
- semi ‘;’ Loc=main.m:16:18
- identifier ‘NSString’ [StartOfLine] [LeadingSpace] Loc=main.m:17:9
- star ‘*’ [LeadingSpace] Loc=main.m:17:18
- identifier ‘string’ Loc=main.m:17:19
- equal ‘=’ [LeadingSpace] Loc=main.m:17:26
- l_square ‘[’ [LeadingSpace] Loc=main.m:17:28
- l_square ‘[’ Loc=main.m:17:29
- identifier ‘NSString’ Loc=main.m:17:30
- identifier ‘alloc’ [LeadingSpace] Loc=main.m:17:39
- r_square ‘]’ Loc=main.m:17:44
- identifier ‘initWithUTF8String’ [LeadingSpace] Loc=main.m:17:46
- colon ‘:’ Loc=main.m:17:64
- string_literal ‘“clang”’ Loc=main.m:17:65
- r_square ‘]’ Loc=main.m:17:72
- semi ‘;’ Loc=main.m:17:73
- int ‘int’ [StartOfLine] [LeadingSpace] Loc=main.m:18:9
- identifier ‘rank’ [LeadingSpace] Loc=main.m:18:13
- equal ‘=’ [LeadingSpace] Loc=main.m:18:18
- identifier ‘i’ [LeadingSpace] Loc=main.m:18:20
- plus ‘+’ [LeadingSpace] Loc=main.m:18:22
- identifier ‘j’ [LeadingSpace] Loc=main.m:18:24
- semi ‘;’ Loc=main.m:18:25
- identifier ‘NSLog’ [StartOfLine] [LeadingSpace] Loc=main.m:19:9
- l_paren ‘(’ Loc=main.m:19:14
- at ‘@’ Loc=main.m:19:15
- string_literal ‘"%@ rank %d"’ Loc=main.m:19:16
- comma ‘,’ Loc=main.m:19:28
- identifier ‘string’ Loc=main.m:19:29
- comma ‘,’ Loc=main.m:19:35
- identifier ‘rank’ [LeadingSpace] Loc=main.m:19:37
- r_paren ‘)’ Loc=main.m:19:41
- semi ‘;’ Loc=main.m:19:42
- r_brace ‘}’ [StartOfLine] [LeadingSpace] Loc=main.m:20:5
- return ‘return’ [StartOfLine] [LeadingSpace] Loc=main.m:21:5
- numeric_constant ‘0’ [LeadingSpace] Loc=main.m:21:12
- semi ‘;’ Loc=main.m:21:13
- r_brace ‘}’ [StartOfLine] Loc=main.m:22:1
- eof ‘’ Loc=main.m:22:2
Clang 在進行詞法分析時,將代碼切分成 一個一個Token,比如大小括號、等于號和字符串等。上面打印的信息,可以看到每個 Token ,里面有類型、值和位置。Clang 定義的 Token 類型,可以分為以下 4 類:
- 關鍵字:語法中的關鍵字,比如 if、else、while、for 等;
- 標識符:變量名;
- 字面量:值、數字、字符串;
- 特殊符號:加減乘除等符號;
所有的 Token 類型可以查看https://opensource.apple.com//source/lldb/lldb-69/llvm/tools/clang/include/clang/Basic/TokenKinds.def。
語法分析
詞法分析完成后,進行語法分析,驗證語法是否正確,將輸出的 Token 先按照語法組合成語義生成節點,將這些節點按照層級關系組成抽象語法樹(AST)。
在命令行中輸入: - clang -fmodules -fsyntax-only -Xclang -ast-dump main.m
輸出信息如下:
- TranslationUnitDecl 0x7ff3e4015608 <>
- |-TypedefDecl 0x7ff3e4015ea0 <> implicit __int128_t ‘__int128’
- | `-BuiltinType 0x7ff3e4015ba0 ‘__int128’
- |-TypedefDecl 0x7ff3e4015f10 <> implicit __uint128_t ‘unsigned __int128’
- | `-BuiltinType 0x7ff3e4015bc0 ‘unsigned __int128’
- |-TypedefDecl 0x7ff3e4015fb0 <> implicit SEL ‘SEL *’
- | `-PointerType 0x7ff3e4015f70 ‘SEL *’ imported
- | `-BuiltinType 0x7ff3e4015e00 ‘SEL’
- |-TypedefDecl 0x7ff3e4016098 <> implicit id ‘id’
- | `-ObjCObjectPointerType 0x7ff3e4016040 ‘id’ imported
- | `-ObjCObjectType 0x7ff3e4016010 ‘id’ imported
- |-TypedefDecl 0x7ff3e4016178 <> implicit Class ‘Class’
- | `-ObjCObjectPointerType 0x7ff3e4016120 ‘Class’ imported
- | `-ObjCObjectType 0x7ff3e40160f0 ‘Class’ imported
- |-ObjCInterfaceDecl 0x7ff3e40161d0 <> implicit Protocol
- |-TypedefDecl 0x7ff3e4016548 <> implicit __NSConstantString ‘struct __NSConstantString_tag’
- | `-RecordType 0x7ff3e4016340 ‘struct __NSConstantString_tag’
- | `-Record 0x7ff3e40162a0 ‘__NSConstantString_tag’
- |-TypedefDecl 0x7ff3e4052c00 <> implicit __builtin_ms_va_list ‘char *’
- | `-PointerType 0x7ff3e40165a0 ‘char *’
- | `-BuiltinType 0x7ff3e40156a0 ‘char’
- |-TypedefDecl 0x7ff3e4052ee8 <> implicit __builtin_va_list ‘struct __va_list_tag [1]’
- | `-ConstantArrayType 0x7ff3e4052e90 ‘struct __va_list_tag [1]’ 1
- | `-RecordType 0x7ff3e4052cf0 ‘struct __va_list_tag’
- | `-Record 0x7ff3e4052c58 ‘__va_list_tag’
- |-ImportDecl 0x7ff3e42ce778 main.m:9:1 col:1 implicit Foundation
- `-FunctionDecl 0x7ff3e42cea40 <line:12:1, line:22:1> line:12:5 main ‘int (int, char **)’
- |-ParmVarDecl 0x7ff3e42ce7d0 <col:10, col:14> col:14 argc ‘int’
- |-ParmVarDecl 0x7ff3e42ce8f0 <col:20, col:32> col:27 argv ‘char **’:‘char **’
- `-CompoundStmt 0x7ff3e390be60 <col:35, line:22:1>
-
|-ObjCAutoreleasePoolStmt 0x7ff3e390be18 <line:14:5, line:20:5> -
| `-CompoundStmt 0x7ff3e390bde0 <line:14:22, line:20:5> -
| |-DeclStmt 0x7ff3e42cebf0 <line:15:9, col:28> -
| | `-VarDecl 0x7ff3e42ceb68 <col:9, line:10:22> line:15:13 used i 'int' cinit -
| | `-IntegerLiteral 0x7ff3e42cebd0 <line:10:22> 'int' 8 -
| |-DeclStmt 0x7ff3e5023750 <line:16:9, col:18> -
| | `-VarDecl 0x7ff3e42cec20 <col:9, col:17> col:13 used j 'int' cinit -
| | `-IntegerLiteral 0x7ff3e42cec88 <col:17> 'int' 6 -
| |-DeclStmt 0x7ff3e390b5e8 <line:17:9, col:73> -
| | `-VarDecl 0x7ff3e50237b0 <col:9, col:72> col:19 used string 'NSString *' cinit -
| | `-ObjCMessageExpr 0x7ff3e3820390 <col:28, col:72> 'NSString * _Nullable':'NSString *' selector=initWithUTF8String: -
| | |-ObjCMessageExpr 0x7ff3e5023b98 <col:29, col:44> 'NSString *' selector=alloc class='NSString' -
| | `-ImplicitCastExpr 0x7ff3e3820378 <col:65> 'const char * _Nonnull':'const char *' <NoOp> -
| | `-ImplicitCastExpr 0x7ff3e3820360 <col:65> 'char *' <ArrayToPointerDecay> -
| | `-StringLiteral 0x7ff3e5023c08 <col:65> 'char [6]' lvalue "clang" -
| |-DeclStmt 0x7ff3e390bbc8 <line:18:9, col:25> -
| | `-VarDecl 0x7ff3e390b618 <col:9, col:24> col:13 used rank 'int' cinit -
| | `-BinaryOperator 0x7ff3e390b720 <col:20, col:24> 'int' '+' -
| | |-ImplicitCastExpr 0x7ff3e390b6f0 <col:20> 'int' <LValueToRValue> -
| | | `-DeclRefExpr 0x7ff3e390b680 <col:20> 'int' lvalue Var 0x7ff3e42ceb68 'i' 'int' -
| | `-ImplicitCastExpr 0x7ff3e390b708 <col:24> 'int' <LValueToRValue> -
| | `-DeclRefExpr 0x7ff3e390b6b8 <col:24> 'int' lvalue Var 0x7ff3e42cec20 'j' 'int' -
| `-CallExpr 0x7ff3e390bd60 <line:19:9, col:41> 'void' -
| |-ImplicitCastExpr 0x7ff3e390bd48 <col:9> 'void (*)(id, ...)' <FunctionToPointerDecay> -
| | `-DeclRefExpr 0x7ff3e390bbe0 <col:9> 'void (id, ...)' Function 0x7ff3e390b748 'NSLog' 'void (id, ...)' -
| |-ImplicitCastExpr 0x7ff3e390bd98 <col:15, col:16> 'id':'id' <BitCast> -
| | `-ObjCStringLiteral 0x7ff3e390bc60 <col:15, col:16> 'NSString *' -
| | `-StringLiteral 0x7ff3e390bc38 <col:16> 'char [11]' lvalue "%@ rank %d" -
| |-ImplicitCastExpr 0x7ff3e390bdb0 <col:29> 'NSString *' <LValueToRValue> -
| | `-DeclRefExpr 0x7ff3e390bc80 <col:29> 'NSString *' lvalue Var 0x7ff3e50237b0 'string' 'NSString *' -
| `-ImplicitCastExpr 0x7ff3e390bdc8 <col:37> 'int' <LValueToRValue> -
| `-DeclRefExpr 0x7ff3e390bcb8 <col:37> 'int' lvalue Var 0x7ff3e390b618 'rank' 'int' -
`-ReturnStmt 0x7ff3e390be50 <line:21:5, col:12> -
`-IntegerLiteral 0x7ff3e390be30 <col:12> 'int' 0
TranslationUnitDecl 是根節點,表示一個編譯單元;Decl 表示一個聲明;Expr 表示表達式;Literal 表示字面量,一個特殊的 Expr;Stmt 表示語句。
clang static analyzer
這個階段重點說一個工具,就是 clang static analyzer。這是一個靜態代碼分析工具,可用于查找 C、C++和 Objective-C 程序中的 bug。clang static analyzer 包括 analyzer core 和 checker 兩部分,所有的 checker 都是基于底層的 analyzer core,通過 analyzer core 提高的功能,能夠編寫 checker。
每執行一條語句,analyzer core 就會遍歷所有 checker 中的回調函數,所以 checker 越多,語句執行速度越慢。通過命令行查看當前 Clang 版本下的 checker:
- clang -cc1 -analyzer-checker-help
通過編譯的這個過程,就可以做很多事情了,比如自定義檢查規則、自動混淆代碼甚至將代碼轉換成另一種語言等。
3.生成
LLVM IR
在編譯過程中,可以把 Clang 解析出 IR 的過程稱為 LLVM Frontend,把 IR 轉成目標機器碼的過程稱為 LLVM Backend。LLVM IR 是 Frontend 的輸出,也是 Backend 的輸入,前后端的橋接語言。借用一張圖說明:
前端 Clang 負責解析,驗證和診斷輸入代碼中的錯誤,將解析的代碼轉換為 LLVM IR,后端 LLVM 編譯把 IR 通過一系列改進代碼的分析和優化,發送到代碼生成器,生成本機機器代碼。
不管編譯的是 Objective-C 或者是 Swift,也不管對應的硬件平臺是什么類型的,LLVM 里唯一不變的就是中間語言 LLVM IR,與何種語言開發無關。如果開發一個新語言,只需要在完成語法解析后,通過 LLVM 提高的接口生成 IR,可以直接在各個不同的平臺上運行了。
LLVM IR 有三種表示格式:第一種 .bc 后綴,想 Bitcode 的存儲格式;第二種是可讀的 .ll;第三種是用于開發時操作 IR 的內存格式。在命令行中輸入:
- clang -S -emit-llvm main.m -o main.ll
在這個目錄下會看到一個main.ll文件,用 xcode 打開:
- ; ModuleID = ‘main.m’
- source_filename = “main.m”
- target datalayout = “e-m:o-i64:64-f80:128-n8:16:32:64-S128”
- target triple = “x86_64-apple-macosx10.15.0”
- %0 = type opaque
- %struct._class_t = type { %struct._class_t*, %struct._class_t*, %struct._objc_cache*, i8* (i8*, i8*)**, %struct._class_ro_t* }
- %struct._objc_cache = type opaque
- %struct._class_ro_t = type { i32, i32, i32, i8*, i8*, %struct.__method_list_t*, %struct._objc_protocol_list*, %struct._ivar_list_t*, i8*, %struct._prop_list_t* }
- %struct.__method_list_t = type { i32, i32, [0 x %struct._objc_method] }
- %struct._objc_method = type { i8*, i8*, i8* }
- %struct._objc_protocol_list = type { i64, [0 x %struct._protocol_t*] }
- %struct._protocol_t = type { i8*, i8*, %struct._objc_protocol_list*, %struct.__method_list_t*, %struct.__method_list_t*, %struct.__method_list_t*, %struct.__method_list_t*, %struct._prop_list_t*, i32, i32, i8**, i8*, %struct._prop_list_t* }
- %struct._ivar_list_t = type { i32, i32, [0 x %struct._ivar_t] }
- %struct._ivar_t = type { i64*, i8*, i8*, i32, i32 }
- %struct._prop_list_t = type { i32, i32, [0 x %struct._prop_t] }
- %struct._prop_t = type { i8*, i8* }
- %struct.__NSConstantString_tag = type { i32*, i32, i8*, i64 }
- @“OBJC_CLASS_$_NSString” = external global %struct._class_t
- @“OBJC_CLASSLIST_REFERENCES_"=internalglobal_" = internal global %struct._class_t* @"OBJC_CLASS_"?=internalglobal_NSString”, section “__DATA,__objc_classrefs,regular,no_dead_strip”, align 8
- @.str = private unnamed_addr constant [6 x i8] c"clang\00", align 1
- @OBJC_METH_VAR_NAME_ = private unnamed_addr constant [20 x i8] c"initWithUTF8String:\00", section “__TEXT,__objc_methname,cstring_literals”, align 1
- @OBJC_SELECTOR_REFERENCES_ = internal externally_initialized global i8* getelementptr inbounds ([20 x i8], [20 x i8]* @OBJC_METH_VAR_NAME_, i32 0, i32 0), section “__DATA,__objc_selrefs,literal_pointers,no_dead_strip”, align 8
- @__CFConstantStringClassReference = external global [0 x i32]
- @.str.1 = private unnamed_addr constant [11 x i8] c"%@ rank %d\00", section “__TEXT,__cstring,cstring_literals”, align 1
- @unnamed_cfstring = private global %struct.__NSConstantString_tag { i32* getelementptr inbounds ([0 x i32], [0 x i32]* @__CFConstantStringClassReference, i32 0, i32 0), i32 1992, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i32 0, i32 0), i64 10 }, section “__DATA,__cfstring”, align 8 #0
- @llvm.compiler.used = appending global [3 x i8*] [i8* bitcast (%struct.class_t** @"OBJC_CLASSLIST_REFERENCES$" to i8*), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @OBJC_METH_VAR_NAME, i32 0, i32 0), i8* bitcast (i8** @OBJC_SELECTOR_REFERENCES_ to i8*)], section “llvm.metadata”
- ; Function Attrs: noinline optnone ssp uwtable
- define i32 @main(i32, i8**) #1 {
- %3 = alloca i32, align 4
- %4 = alloca i32, align 4
- %5 = alloca i8**, align 8
- %6 = alloca i32, align 4
- %7 = alloca i32, align 4
- %8 = alloca %0*, align 8
- %9 = alloca i32, align 4
- store i32 0, i32* %3, align 4
- store i32 %0, i32* %4, align 4
- store i8** %1, i8*** %5, align 8
- %10 = call i8* @llvm.objc.autoreleasePoolPush() #2
- store i32 8, i32* %6, align 4
- store i32 6, i32* %7, align 4
- %11 = load %struct.class_t*, %struct.class_t** @"OBJC_CLASSLIST_REFERENCES$", align 8
- %12 = bitcast %struct._class_t* %11 to i8*
- %13 = call i8* @objc_alloc(i8* %12)
- %14 = bitcast i8* %13 to %0*
- %15 = load i8*, i8** @OBJC_SELECTOR_REFERENCES_, align 8, !invariant.load !9
- %16 = bitcast %0* %14 to i8*
-
%17 = call i8* bitcast (i8* (i8*, i8*, ...)* @objc_msgSend to i8* (i8*, i8*, i8*)*)(i8* %16, i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0)) -
%18 = bitcast i8* %17 to %0* -
store %0* %18, %0** %8, align 8 -
%19 = load i32, i32* %6, align 4 -
+ = load i32, i32* %7, align 4 -
%21 = add nsw i32 %19, + -
store i32 %21, i32* %9, align 4 -
%22 = load %0*, %0** %8, align 8 -
%23 = load i32, i32* %9, align 4 -
notail call void (i8*, ...) @NSLog(i8* bitcast (%struct.__NSConstantString_tag* @_unnamed_cfstring_ to i8*), %0* %22, i32 %23) -
call void @llvm.objc.autoreleasePoolPop(i8* %10) -
ret i32 0 - }
- ; Function Attrs: nounwind
- declare i8* @llvm.objc.autoreleasePoolPush() #2
- declare i8* @objc_alloc(i8*)
- ; Function Attrs: nonlazybind
- declare i8* @objc_msgSend(i8*, i8*, …) #3
- declare void @NSLog(i8*, …) #4
- ; Function Attrs: nounwind
- declare void @llvm.objc.autoreleasePoolPop(i8*) #2
- attributes #0 = { “objc_arc_inert” }
- attributes #1 = { noinline optnone ssp uwtable “correctly-rounded-divide-sqrt-fp-math”=“false” “darwin-stkchk-strong-link” “disable-tail-calls”=“false” “frame-pointer”=“all” “less-precise-fpmad”=“false” “min-legal-vector-width”=“0” “no-infs-fp-math”=“false” “no-jump-tables”=“false” “no-nans-fp-math”=“false” “no-signed-zeros-fp-math”=“false” “no-trapping-math”=“false” “probe-stack”="___chkstk_darwin" “stack-protector-buffer-size”=“8” “target-cpu”=“penryn” “target-features”="+cx16,+cx8,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" “unsafe-fp-math”=“false” “use-soft-float”=“false” }
- attributes #2 = { nounwind }
- attributes #3 = { nonlazybind }
- attributes #4 = { “correctly-rounded-divide-sqrt-fp-math”=“false” “darwin-stkchk-strong-link” “disable-tail-calls”=“false” “frame-pointer”=“all” “less-precise-fpmad”=“false” “no-infs-fp-math”=“false” “no-nans-fp-math”=“false” “no-signed-zeros-fp-math”=“false” “no-trapping-math”=“false” “probe-stack”="___chkstk_darwin" “stack-protector-buffer-size”=“8” “target-cpu”=“penryn” “target-features”="+cx16,+cx8,+fxsr,+mmx,+sahf,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" “unsafe-fp-math”=“false” “use-soft-float”=“false” }
- !llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6, !7}
- !llvm.ident = !{!8}
- !0 = !{i32 2, !“SDK Version”, [3 x i32] [i32 10, i32 15, i32 4]}
- !1 = !{i32 1, !“Objective-C Version”, i32 2}
- !2 = !{i32 1, !“Objective-C Image Info Version”, i32 0}
- !3 = !{i32 1, !“Objective-C Image Info Section”, !"__DATA,__objc_imageinfo,regular,no_dead_strip"}
- !4 = !{i32 4, !“Objective-C Garbage Collection”, i32 0}
- !5 = !{i32 1, !“Objective-C Class Properties”, i32 64}
- !6 = !{i32 1, !“wchar_size”, i32 4}
- !7 = !{i32 7, !“PIC Level”, i32 2}
- !8 = !{!“Apple clang version 11.0.3 (clang-1103.0.32.29)”}
- !9 = !{}
LLVM IR 的指令介紹:
? %:局部變量
? @:全局變量
? alloca:為當前執行的函數分配內存,當該函數執行完畢時自動釋放內存
? i32:整數占位,i32 就代辦 32 位
? align:對齊,向 4 個字節對齊,即便數據沒有占用 4 個字節,也要為其分配 4 個字節
? load:讀出
? store:寫入
? icmp:兩個整數值比較,返回布爾值
? br:選擇分支,根據條件跳轉對應的 label
? label:代碼標簽
? call:調用
LLVM Backend
完整的 LLVM Backend 階段稱為 CodeGen,整個過程可以分成以下幾個階段:
? Instruction Selection:指令選擇,將 IR 轉化成由目標平臺指令組成的 DAG,選擇能完成指定操作,執行時間最短的指令;
? Scheduling and Formation:調度與排序,讀取 DAG,將 DAG 的指令排成 MachineInstr 隊列。根據指令間的依賴進行指令重排,更好地利用 CPU 的功能單元;
? SSA 優化:由多個基于 SSA 的 Pass 組成;
? Register allocation:寄存器分配,將 Virtual Register 映射到 Physical Register 或者內存地址上;
? Prolog/Epilo 的生成:函數體指令生成后,可以確定函數所需要的堆棧大小了;
? Machine Code:機器碼晚期優化,這是最后一次進行優化的機會;
? Code Emission:代碼發射,輸出代碼,可以選擇輸出匯編程序或二進制機器碼;
三、編譯完成生成的文件
編譯完成后,生成一些文件,主要介紹三種:
? 二進制內容 Link Map File
? dSYM 文件
? Mach-O
Link Map File
Link Map FIle 文件內容包含三個部分:
? Object file:.m 文件編譯后的 .o 文件和需要連接的 .a 文件,包括文件編號和文件路徑;
? Section:描述每個 Section 在可執行文件中的位置和大小;
? Symbol:Symbol 對 Section 進行了再劃分,描述了method、ivar、string,以及對應的 address、size 和 file number 信息;
可以在 Build Settings 中查看路徑,如下圖:
如果使用二進制重排提升 APP 的啟動速度,用到 Link Map File 了,參考鏈接:https://juejin.cn/post/6844904130406793224。
dSYM 文件
dSYM 文件里面存儲了函數地址映射的信息,調用棧的地址,可以通過 dSYM 映射表獲得具體的函數信息。通??梢宰?crash 的文件符號化,將 crash 時保存的調用棧信息轉化為相應的函數。這就是為什么友盟或者 bugly,都需要上傳 dSYM 文件的原因。
Mach-O
Mach-O 文件是用于記錄編譯后的可執行文件、對象代碼、共享庫、動態加載代碼和內存轉儲的文件格式。Mach-O 里面的 _CodeSignature 包含了程序代碼的簽名,保證里面的文件不能直接更改。如果用過企業版重簽名的功能,其實還是有些東西可以改的,只不過改完后,要重新生成簽名文件。
Mach-O 文件,主要包含三個部分:
? Mach-O Header:包含字節順序、魔數、CPU 類型、加載指令的數量等;
? Load Command:包括區域的位置、符號表、動態符號表等。每個加載指令包含一個元信息,比如指令類型、名稱以及在二進制中的位置等;
? Data:內容最多的部分,包含了代碼、數據。比如符號表、動態符號表等;
四、小結
接下來來歸納一下:
- iOS 使用編譯器而不是解釋器來處理代碼,為了獲得更快的運行速度;
- iOS 的編譯過程是通過 LLVM 編譯工具生成語法樹 AST,把 AST 轉換成 IR,最后把 IR 生成平臺的機器碼。
- 編譯成功生成的幾個文件 Link Map File、dSYM 和 Mach-O。
Clang 代碼規范檢查插件
? Clang 代碼規范檢查插件
o 什么是 LLVM 和 Clang
o 自定義插件
o 下載編譯 LLVM
o 自定義插件開發
o 1.新增開發工程
o 2.功能開發
o Xcode 運行
o 1. 添加 Plugin 路徑
o 2. 添加 Clang 路徑
o 3. 關閉索引建立
o 問題
? 引用:
Clang 代碼規范檢查插件
使用 LLVM 和 Clang,寫一個代碼規范檢查插件。
什么是 LLVM 和 Clang
LLVM 是一個模塊化和可重用的編譯器和工具鏈技術的集合,其實是一個代碼工程名。早期說到 LLVM 其實是指它的核心庫,可以對源碼進行平臺無關的優化,生成不同平臺的機器碼?,F在LLVM指整個工具鏈技術集合。
LLVM 工程包含核心庫、Clang、LLDB、LLD 等相關項目。Clang 就是 LLVM 的一個子項目,包括 C,C++ 和 Objective-C 的編譯器,可以提供驚人的快速編譯,比 GCC 快 3 倍。clang static analyzer 主要是進行語法分析,語義分析和生成中間代碼。在語義分析階段,對語法樹進行代碼靜態分析,在靜態分析過程中,能加上代碼規范檢查了。
自定義插件
Clang 插件制作步驟如下:
- 下載 LLVM ,生成 Xcode 工程,編譯項目
- 新增 Clang 插件,自定義插件開發,編譯出 dylib
- Xcode 添加編譯設置,接入插件
下載編譯 LLVM
先下載 LLVM 工程,在本地新建一個文件夾 LLVM,打開命令行 cd 到該目錄下,輸入命令: - git clone https://github.com/llvm/llvm-project.git
- cd llvm-project
- mkdir build
- cd build
- cmake -G Xcode -DLLVM_ENABLE_PROJECTS=clang …/llvm
其中 cmake -G Xcode -DLLVM_ENABLE_PROJECTS=clang …/llvm ,生成 LLVM 的 Xcode 編譯工程,可以看到本地目錄如下:
目錄中 clang 是類 C 語言編譯器的代碼目錄;llvm 目錄的代碼包含兩部分,一部分是對源碼進行平臺無關優化的優化器代碼,另一部分是生成平臺相關匯編代碼的生成器代碼;lldb 目錄里是調試器的代碼;lld 里是鏈接器代碼。
編譯工程,雙擊打開 LLVM.Xcodeproj ,選擇 Autocreat Schemes,添加 schemes All_BUILD:
點擊 Running,等待編譯完成,預計要大半個小時:
編譯完成后,可以在目錄下看到 Clang 的可執行文件:
這里 clang 和 clang++ 這兩個文件的路徑記下來,后面會用到。
由于 Xcode 自帶的 clang 可能跟工程編譯出來的版本不同,在使用自定義插件時,最好把 clang 依賴改成編譯出來的版本。
自定義插件開發
1.新增開發工程
接下來開始開發插件,先在 llvm-project — clang — tools 目錄下新建一個文件夾 CodeStandardsPlugin,在文件夾里面新建兩個文件:CMakeLists.txt 和 CodeStandardsPlugin.cpp。
這里的 CodeStandardsPlugin 表示插件名稱,可以自主命名,后續所有用到 CodeStandardsPlugin,都用命名替換即可。
在 llvm-project — clang — tools — CMakeLists.txt 文件的末尾,添加一行代碼:
- add_clang_subdirectory(CodeStandardsPlugin)
重新生成 LLVM 編譯工程,在終端運行:
- cmake -G Xcode -DLLVM_ENABLE_PROJECTS=clang …/llvm
重新打開 LLVM.xcodeproj ,按照下圖點擊 + 號,輸入 CodeStandardsPlugin,可以添加自定義的 scheme 了。
在工程文件列表中,搜索 CodeStandardsPlugin,可以看到新建的兩個文件:
2.功能開發
在 CodeStandardsPlugin/CMakeLists.txt 中,添加如下代碼:
- add_llvm_library(CodeStandardsPlugin MODULE CodeStandardsPlugin.cpp PLUGIN_TOOL clang)
- if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN))
- target_link_libraries(CodeStandardsPlugin PRIVATE
-
clangAST -
clangBasic -
clangFrontend -
LLVMSupport -
) - endif()
CodeStandardsPlugin.cpp 文件,用來存放開發代碼的,源碼已經放在https://github.com/YakirLove/CodeStandardsPlugin/blob/master/CodeStandardsPlugin.cpp。
運行工程,在 Products 中,可以看到打包出來的執行文件:
找到 CodeStandardsPlugin.dylib ,拷貝出來,或者絕對路徑記錄下來。
Xcode 運行
在 Xcode 上運行自定義的編譯插件,需要用到 XcodeHacking 這個工具來 hook Xcode。不過現在已經不需要那么麻煩了,在 Xcode10后,只需要添加幾個編譯配置。
- 添加 Plugin 路徑
在 Targets — Build Setting — AppleClang-Custom Compiler Flags — Other C Flags 中,添加如下語句: - -Xclang -load -Xclang (插件 dylib 絕對路徑)-Xclang -add-plugin -Xclang ( Plugin 名字)
示例如下:
- -Xclang -load -Xclang /Users/wuyanji/Desktop/YourClang/CodeStandardsPlugin.dylib -Xclang -add-plugin -Xclang CodeStandardsPlugin
最后,可以看到結果如下圖所示:
- 添加 Clang 路徑
插件需要相應的 Clang 版本加載,需要添加依賴的 Clang 路徑,在 Xcode 中新增兩項自定義設置,分別為 CC 和 CCX:
第一步 llvm 編譯后得到的 clang 和 clang++路徑,就是用在這里的。CC 表示 clang 路徑,CCX 表示 clang++ 路徑。
3. 關閉索引建立
編譯可能會遇到問題:
可以通過關閉編譯建立索引,解決這個問題,將 Index-Wihle-Building Functionality 設置為 NO
編譯工程,可以看到結果如下:
問題
由于將 Index-Wihle-Building Functionality 設置為 NO 了,導致 Xcode 的自動補全功能和代碼顏色提醒失效,目前沒有太好的解決辦法。也許可以將插件做成工具形式,需要時再執行。
https://github.com/YakirLove/CodeStandardsPlugin/tree/master
參考鏈接:
https://xiaozhuanlan.com/topic/4352601789
https://xiaozhuanlan.com/topic/8960517324
總結
以上是生活随笔為你收集整理的LLVM Clang前端编译与调试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux 交叉编译简介
- 下一篇: 硬件专业化和软件映射的敏捷框架