使用Clang编译cpp源码得到IR
首先使用编译器前端clang执行如下命令即可得到中间语言IR,
clang -emit-llvm -S ori_cpp_file -o out_ir_file
其中的ori_cpp_file表示原始的cpp文件,out_ir_file表示编译后输出的IR文件。执行该命令后即会在out_ir_file路径下生成源cpp文件的llvm的汇编代码。该命令等同于执行如下命令:
clang -emit-llvm -S -c ori_cpp_file -o out_ir_file
如果不加-S参数那么生成的会是源cpp文件的二进制代码。
IR语法介绍
为了了解IR的语法,我们可以写一段简单的cpp源文件,然后使用clang执行前面说的命令来对比查看下生成的IR文件的内容从而来熟悉IR的语法,为了能够尽可能多的覆盖IR的语法,cpp源文件原本需要尽量包含更多变量类型和程序结构,比如字符串,整数,数组,条件分支,for/while循环结构等。但是为了更容易上手,我们可以把这些结构拆分开来每次只放到一个单独的cpp文件中,然后逐个编译,而不推荐一开始就混在一个cpp文件中,因为即使是一个简单的hello word程序其IR的代码量也是很大的,所以对于初学者而言,混在一起反而不容易上手,等基本语法都熟悉了可以在写个复杂的涵盖程序所有变量类型和程序结构的cpp文件一次性编译查看。
这里是一个及其简单的c++源码示例,
#include<iostream>
using namespace std;
int main() {
int i;
return 0;
}
其对应的IR代码如下:
; ModuleID = 'IR.cpp'
source_filename = "IR.cpp"
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.0.24215"
; Function Attrs: noinline norecurse nounwind optnone uwtable
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%i = alloca i32, align 4
store i32 0, i32* %retval, align 4
ret i32 0
}
attributes #0 = { noinline norecurse nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.linker.options = !{!0, !1, !2, !3, !4}
!llvm.module.flags = !{!5, !6}
!llvm.ident = !{!7}
!0 = !{!"/FAILIFMISMATCH:\22_MSC_VER=1900\22"}
!1 = !{!"/FAILIFMISMATCH:\22_ITERATOR_DEBUG_LEVEL=0\22"}
!2 = !{!"/FAILIFMISMATCH:\22RuntimeLibrary=MT_StaticRelease\22"}
!3 = !{!"/DEFAULTLIB:libcpmt.lib"}
!4 = !{!"/FAILIFMISMATCH:\22_CRT_STDIO_ISO_WIDE_SPECIFIERS=0\22"}
!5 = !{i32 1, !"wchar_size", i32 2}
!6 = !{i32 7, !"PIC Level", i32 2}
!7 = !{!"clang version 6.0.0 (tags/RELEASE_600/final)"}
一个cpp源文件代码中一般会包含源文件(类或者整个源代码),函数声明,函数定义实现部分,注释等,可以看到IR也同样包含对应的结构,比如模块Module(类似源文件的概念)和注释(在IR中使用英文;表示)比如第一行的注释ModuleID表面该IR属于hello_IR.cpp源文件,函数Function(使用define定义或者declare声明,和c语言类似),基本块Basic Block(类似cpp源文件中的函数体代码实现,不过基本块是一个更小的单位,只具备单入口和单出口,必须以终止指令结尾),指令Instruction,所以整个IR的组成结构大概可以看作是如下结构:
Module->Function->Basic Block->Instruction,也就是Module下包含很多函数,函数中包含很多基本块,基本块中包含很多具体的指令,之所以强调这个结构,是因为后面我们使用LLVM框架提供的类和API接口进行相应操作的时候基本上是按照这个结构来的,而我们之所以去了解IR的语法也是为了为基于llvm框架进行开发做准备。
这里先写一个简单的包含整型/浮点型/字符型和字符串类型,全局/局部变量的示例如下:
#include<iostream>
using namespace std;
short s=1;
int main() {
short s;int i;long l;//整型
float m;double d;//浮点型
char c='c';//字符型
//字符串
string str = "Hello IR";
char * c_str = "this is a c str test";
char c_char_aary[] = "this is a c char array test";
return 0;
}
然后其对应的IR表示如下:
; ModuleID = 'hello_IR.cpp'
source_filename = "hello_IR.cpp"
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-macosx10.13.0"
%"class.std::__1::allocator" = type { i8 }
%"struct.std::__1::__compressed_pair_elem.1" = type { i8 }
%"struct.std::__1::__compressed_pair_elem" = type { %"struct.std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep" }
%"struct.std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep" = type { %union.anon }
%union.anon = type { %"struct.std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__long" }
%"struct.std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__long" = type { i64, i64, i8* }
%"class.std::__1::__compressed_pair" = type { %"struct.std::__1::__compressed_pair_elem" }
%"class.std::__1::basic_string" = type { %"class.std::__1::__compressed_pair" }
%"class.std::__1::__basic_string_common" = type { i8 }
@s = global i16 1, align 2
@.str = private unnamed_addr constant [9 x i8] c"Hello IR\00", align 1
@.str.1 = private unnamed_addr constant [21 x i8] c"this is a c str test\00", align 1
@_ZZ4mainE11c_char_aary = private unnamed_addr constant [28 x i8] c"this is a c char array test\00", align 16
; Function Attrs: noinline norecurse optnone ssp uwtable
define i32 @main() #0 {
%1 = alloca %"class.std::__1::allocator"*, align 8
%2 = alloca %"struct.std::__1::__compressed_pair_elem.1"*, align 8
%3 = alloca %"struct.std::__1::__compressed_pair_elem"*, align 8
%4 = alloca %"class.std::__1::__compressed_pair"*, align 8
%5 = alloca %"class.std::__1::__compressed_pair"*, align 8
%6 = alloca %"class.std::__1::basic_string"*, align 8
%7 = alloca i8*, align 8
%8 = alloca %"class.std::__1::basic_string"*, align 8
%9 = alloca i8*, align 8
%10 = alloca i32, align 4
%11 = alloca i16, align 2
%12 = alloca i32, align 4
%13 = alloca i64, align 8
%14 = alloca float, align 4
%15 = alloca double, align 8
%16 = alloca i8, align 1
%17 = alloca %"class.std::__1::basic_string", align 8
%18 = alloca i8*, align 8
%19 = alloca [28 x i8], align 16
store i32 0, i32* %10, align 4
store i8 99, i8* %16, align 1
store %"class.std::__1::basic_string"* %17, %"class.std::__1::basic_string"** %8, align 8
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8** %9, align 8
%20 = load %"class.std::__1::basic_string"*, %"class.std::__1::basic_string"** %8, align 8
%21 = load i8*, i8** %9, align 8
store %"class.std::__1::basic_string"* %20, %"class.std::__1::basic_string"** %6, align 8
store i8* %21, i8** %7, align 8
%22 = load %"class.std::__1::basic_string"*, %"class.std::__1::basic_string"** %6, align 8
%23 = bitcast %"class.std::__1::basic_string"* %22 to %"class.std::__1::__basic_string_common"*
%24 = getelementptr inbounds %"class.std::__1::basic_string", %"class.std::__1::basic_string"* %22, i32 0, i32 0
store %"class.std::__1::__compressed_pair"* %24, %"class.std::__1::__compressed_pair"** %5, align 8
%25 = load %"class.std::__1::__compressed_pair"*, %"class.std::__1::__compressed_pair"** %5, align 8
store %"class.std::__1::__compressed_pair"* %25, %"class.std::__1::__compressed_pair"** %4, align 8
%26 = load %"class.std::__1::__compressed_pair"*, %"class.std::__1::__compressed_pair"** %4, align 8
%27 = bitcast %"class.std::__1::__compressed_pair"* %26 to %"struct.std::__1::__compressed_pair_elem"*
store %"struct.std::__1::__compressed_pair_elem"* %27, %"struct.std::__1::__compressed_pair_elem"** %3, align 8
%28 = load %"struct.std::__1::__compressed_pair_elem"*, %"struct.std::__1::__compressed_pair_elem"** %3, align 8
%29 = getelementptr inbounds %"struct.std::__1::__compressed_pair_elem", %"struct.std::__1::__compressed_pair_elem"* %28, i32 0, i32 0
%30 = bitcast %"struct.std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__rep"* %29 to i8*
call void @llvm.memset.p0i8.i64(i8* %30, i8 0, i64 24, i32 8, i1 false) #5
%31 = bitcast %"class.std::__1::__compressed_pair"* %26 to %"struct.std::__1::__compressed_pair_elem.1"*
store %"struct.std::__1::__compressed_pair_elem.1"* %31, %"struct.std::__1::__compressed_pair_elem.1"** %2, align 8
%32 = load %"struct.std::__1::__compressed_pair_elem.1"*, %"struct.std::__1::__compressed_pair_elem.1"** %2, align 8
%33 = bitcast %"struct.std::__1::__compressed_pair_elem.1"* %32 to %"class.std::__1::allocator"*
store %"class.std::__1::allocator"* %33, %"class.std::__1::allocator"** %1, align 8
%34 = load %"class.std::__1::allocator"*, %"class.std::__1::allocator"** %1, align 8
%35 = load i8*, i8** %7, align 8
%36 = load i8*, i8** %7, align 8
%37 = call i64 @_ZNSt3__111char_traitsIcE6lengthEPKc(i8* %36) #5
call void @_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm(%"class.std::__1::basic_string"* %22, i8* %35, i64 %37)
store i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i32 0, i32 0), i8** %18, align 8
%38 = bitcast [28 x i8]* %19 to i8*
call void @llvm.memcpy.p0i8.p0i8.i64(i8* %38, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @_ZZ4mainE11c_char_aary, i32 0, i32 0), i64 28, i32 16, i1 false)
store i32 0, i32* %10, align 4
call void @_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev(%"class.std::__1::basic_string"* %17)
%39 = load i32, i32* %10, align 4
ret i32 %39
}
; Function Attrs: argmemonly nounwind
declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture writeonly, i8* nocapture readonly, i64, i32, i1) #1
declare void @_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEED1Ev(%"class.std::__1::basic_string"*) unnamed_addr #2
declare void @_ZNSt3__112basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEE6__initEPKcm(%"class.std::__1::basic_string"*, i8*, i64) #2
; Function Attrs: noinline nounwind optnone ssp uwtable
define linkonce_odr i64 @_ZNSt3__111char_traitsIcE6lengthEPKc(i8*) #3 align 2 {
%2 = alloca i8*, align 8
store i8* %0, i8** %2, align 8
%3 = load i8*, i8** %2, align 8
%4 = call i64 @strlen(i8* %3) #5
ret i64 %4
}
; Function Attrs: argmemonly nounwind
declare void @llvm.memset.p0i8.i64(i8* nocapture writeonly, i8, i64, i32, i1) #1
; Function Attrs: nounwind
declare i64 @strlen(i8*) #4
attributes #0 = { noinline norecurse optnone ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { argmemonly nounwind }
attributes #2 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #3 = { noinline nounwind optnone ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #4 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #5 = { nounwind }
!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"PIC Level", i32 2}
!2 = !{!"Apple LLVM version 9.1.0 (clang-902.0.39.2)"}
从上面的示例中可以简单总结出以下几点语法规则
- 注释使用英文分号:,比如IR文件的第一行一般使用注释表面该IR属于哪个Module
- 函数的定义使用define,函数在Module内属于全局值(该概念对比与llvm中的GlobleValue,包含全局变量GlobalVariable和函数Function),函数名前以@开头
- 函数的声明使用declare
- 全局变量使用@开头,后面紧接着的是变量名称,全局常量使用constant修饰符修饰,全局变量使用global 修饰符修饰,函数内定义的字符串常量会转为全局常量,
比如示例中main函数中定义的字符串:
string str = "Hello IR"; char * c_str = "this is a c str test"; char c_char_aary[] = "this is a c char array test";
转化为了全局常量:
@.str = private unnamed_addr constant [9 x i8] c"Hello IR\00", align 1 @.str.1 = private unnamed_addr constant [21 x i8] c"this is a c str test\00", align 1 @_ZZ4mainE11c_char_aary = private unnamed_addr constant [28 x i8] c"this is a c char array test\00", align 16
- 局部变量(这里指的是IR中的局部变量而不是源cpp中的局部变量)使用%开头,后面是局部变量的名称,比如源cpp文件中的int i=0;中int i的定义在IR中的表示为 %10 = alloca i32, align 4 ,表示定义一个局部变量%10,为其分配一个i32大小内存,以4字节对齐,即int i
- 所有的整数数据类型(包括短整型short,整型int,长整型long)和char类型都用ix表示,其中x表示该数据类型的占位数,比如int/long类型是i32表示4字节32位,short是i16表示2字节16位,char类型是i8表示一个字节8位。
- 浮点数据类型float和double用floate和double表示
- 数组类型用[count x ix]表示,其中count表示数组的大小,ix表示数组中每一个元素对应的数据类型,参见上面的一条,比如字符串"Hello IR"表示为[9 x i8],9表示该字符串包含9个元素(末尾包含一个\0),每个元素大小为i8即c语言中的char类型大小
- 使用c++中string和c语言中的char *定义的字符串会以全局常量@.str开头,后面以数字结尾,比如第一个是@.str,第二个是@.str.1,第3个是@str.2,依次类推。而使用char []字符数组定义的字符串也会转为全局常量但是不会以.str.开头
如下是一个简单的包含if分支大于等于和小于判断的源cpp文件:
#include<iostream>
using namespace std;
int main() {
int i=0;
if(i>0){
}else if(i==0){
}else if(i<0){
}
return 0;
}
其对应的IR代码如下:
; ModuleID = 'if_IR.cpp'
source_filename = "5Cif_IR.cpp"
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.0.24215"
; Function Attrs: noinline norecurse nounwind optnone uwtable
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%i = alloca i32, align 4
store i32 0, i32* %retval, align 4
store i32 0, i32* %i, align 4
%0 = load i32, i32* %i, align 4
%cmp = icmp sgt i32 %0, 0
br i1 %cmp, label %if.then, label %if.else
if.then: ; preds = %entry
br label %if.end7
if.else: ; preds = %entry
%1 = load i32, i32* %i, align 4
%cmp1 = icmp eq i32 %1, 0
br i1 %cmp1, label %if.then2, label %if.else3
if.then2: ; preds = %if.else
br label %if.end6
if.else3: ; preds = %if.else
%2 = load i32, i32* %i, align 4
%cmp4 = icmp slt i32 %2, 0
br i1 %cmp4, label %if.then5, label %if.end
if.then5: ; preds = %if.else3
br label %if.end
if.end: ; preds = %if.then5, %if.else3
br label %if.end6
if.end6: ; preds = %if.end, %if.then2
br label %if.end7
if.end7: ; preds = %if.end6, %if.then
ret i32 0
}
attributes #0 = { noinline norecurse nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.linker.options = !{!0, !1, !2, !3, !4}
!llvm.module.flags = !{!5, !6}
!llvm.ident = !{!7}
!0 = !{!"/FAILIFMISMATCH:\22_MSC_VER=1900\22"}
!1 = !{!"/FAILIFMISMATCH:\22_ITERATOR_DEBUG_LEVEL=0\22"}
!2 = !{!"/FAILIFMISMATCH:\22RuntimeLibrary=MT_StaticRelease\22"}
!3 = !{!"/DEFAULTLIB:libcpmt.lib"}
!4 = !{!"/FAILIFMISMATCH:\22_CRT_STDIO_ISO_WIDE_SPECIFIERS=0\22"}
!5 = !{i32 1, !"wchar_size", i32 2}
!6 = !{i32 7, !"PIC Level", i32 2}
!7 = !{!"clang version 6.0.0 (tags/RELEASE_600/final)"}
可以很清晰的看到if语句在IR中是使用比较指令icmp和跳转指令br来实现的,根据比较指令icmp的比较结果跳转至相应的基本块执行。
其中比较指令icmp的语法格式如下:
<result> = icmp <cond> <ty> <op1>, <op2> ; yields i1 or <N x i1>:result
cond表示比较条件,比如大于还是小于,ty表示比较的操作数的类型,op1,op2表示比较指令的操作数,以上面示例为例:
%cmp4 = icmp slt i32 %2, 0
比较条件是slt表示有符号数小于,i32表示比较的操作数是32位,%2,0表示将局部变量%2和数值0进行小于比较,也就是if(i<0)这条语句。其中比较指令参数包括如下一些情况:
- eq: equal ,即==
- ne: not equal,即!=
- ugt: unsigned greater than ,即无符号数>
- uge: unsigned greater or equal,即无符号数>=
- ult: unsigned less than,即无符号数<
- ule: unsigned less or equal,即无符号数<=
- sgt: signed greater than ,即有符号数>
- sge: signed greater or equal,即有符号数>=
- slt: signed less than,即有符号数<
- sle: signed less or equal,即有符号数<=
除了整形比较指令icmp外还有浮点型比较指令fcmp,和icmp参数几乎完全一样,关于这2个指令的详细介绍,大家可以参考官方文档进行查阅:http://llvm.org/docs/LangRef.html#icmp-instruction
跳转指令br语法如下:
br i1 <cond>, label <iftrue>, label <iffalse> br label <dest> ; Unconditional branch
可以看到br跳转包括无条件跳转和有条件跳转,即br il和br。cond表示跳转条件,第一个lable表示如果条件位true要跳转到哪一个基本块的标签(用来标记该基本块的入口),第二个label表示如果比较条件为false要跳转的基本块。还是以上面的示例为例:
br i1 %cmp4, label %if.then5, label %if.end
表示如果局部变量%cmp4的值为真,则跳转到标签为if.then5的基本块执行,否则跳转到标签为if.end的基本块执行。
至于无条件跳转br指令就很容易理解了,直接跳转至标签为dest的基本块执行。
同样的写个简单的包含for/while循环的示例代码:
#include<iostream>
using namespace std;
int main() {
for(int i=0;i<7;i++){}
int i=0;
while(i<7){
i++;
}
return 0;
}
其对应的IR代码如下:
; ModuleID = 'for_while_IR.cpp'
source_filename = "for_while_IR.cpp"
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.0.24215"
; Function Attrs: noinline norecurse nounwind optnone uwtable
define i32 @main() #0 {
entry:
%retval = alloca i32, align 4
%i = alloca i32, align 4
%i1 = alloca i32, align 4
store i32 0, i32* %retval, align 4
store i32 0, i32* %i, align 4
br label %for.cond
for.cond: ; preds = %for.inc, %entry
%0 = load i32, i32* %i, align 4
%cmp = icmp slt i32 %0, 7
br i1 %cmp, label %for.body, label %for.end
for.body: ; preds = %for.cond
br label %for.inc
for.inc: ; preds = %for.body
%1 = load i32, i32* %i, align 4
%inc = add nsw i32 %1, 1
store i32 %inc, i32* %i, align 4
br label %for.cond
for.end: ; preds = %for.cond
store i32 0, i32* %i1, align 4
br label %while.cond
while.cond: ; preds = %while.body, %for.end
%2 = load i32, i32* %i1, align 4
%cmp2 = icmp slt i32 %2, 7
br i1 %cmp2, label %while.body, label %while.end
while.body: ; preds = %while.cond
%3 = load i32, i32* %i1, align 4
%inc3 = add nsw i32 %3, 1
store i32 %inc3, i32* %i1, align 4
br label %while.cond
while.end: ; preds = %while.cond
ret i32 0
}
attributes #0 = { noinline norecurse nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.linker.options = !{!0, !1, !2, !3, !4}
!llvm.module.flags = !{!5, !6}
!llvm.ident = !{!7}
!0 = !{!"/FAILIFMISMATCH:\22_MSC_VER=1900\22"}
!1 = !{!"/FAILIFMISMATCH:\22_ITERATOR_DEBUG_LEVEL=0\22"}
!2 = !{!"/FAILIFMISMATCH:\22RuntimeLibrary=MT_StaticRelease\22"}
!3 = !{!"/DEFAULTLIB:libcpmt.lib"}
!4 = !{!"/FAILIFMISMATCH:\22_CRT_STDIO_ISO_WIDE_SPECIFIERS=0\22"}
!5 = !{i32 1, !"wchar_size", i32 2}
!6 = !{i32 7, !"PIC Level", i32 2}
!7 = !{!"clang version 6.0.0 (tags/RELEASE_600/final)"}
可以看到for/while循环仍然时借助比较指令icmp和跳转指令br来完成的,只不过增加了一个对计数器的加法指令add,加法指令add语法如下:
<result> = add <ty> <op1>, <op2> ; yields ty:result <result> = add nuw <ty> <op1>, <op2> ; yields ty:result <result> = add nsw <ty> <op1>, <op2> ; yields ty:result <result> = add nuw nsw <ty> <op1>, <op2> ; yields ty:result
其中的nuw和nsw表示“No Unsigned Wrap” and “No Signed Wrap”,ty表示操作数类型,op1和op2表示操作数,还是以上面示例为例,在for混淆中存在对计数器i的i++语法,这部分IR代码如下:
for.inc: ; preds = %for.body %1 = load i32, i32* %i, align 4 %inc = add nsw i32 %1, 1 store i32 %inc, i32* %i, align 4 br label %for.cond
add nsw i32 %1,1表示将局部变量1的值加1之后赋给局部变量inc。除了整型加法指令add外,还包括浮点型加法指令fadd,和add指令参数几乎完全一样,关于这2个指令的详细介绍,大家可以参考官方文档进行查阅:http://llvm.org/docs/LangRef.html#add-instruction
所以现在来看for/while循环的实现逻辑就是这样的:
首先跳转至for/while循环的条件比较基本块for.cond:/while.cond: 中进行判断是否满足循环条件,如果满足跳转值for/while循环逻辑执行基本块for.body:/while.body: 也就是对应源cpp文件中的大花括号{}中的代码块,在该基本块中会对循环计数器进行+1操作,然后跳转至条件比较基本块for.cond:/while.cond:,重复上述过程直至条件比较基本块比较结果为false,则跳转至混淆结束基本块for.end: /while.end:结束循环。
IR指令对应的llvm开发框架下的类
文章评论