我的測試環(huán)境:Fedora14
Gcc版本:gcc-4.5.1
內(nèi)核版本:2.6.38.1
C語言是一個強(qiáng)大的語言,特別是對于嵌入式開發(fā)過程中有時需要反匯編分析代碼中存在的問題,函數(shù)是C語言中的難點(diǎn),關(guān)于函數(shù)的調(diào)用也是很多人不能理解的,很多知道的也是一知半解。對C語言的調(diào)用有了一個比較清晰的認(rèn)識就能夠更清晰的分析代碼中存在的問題。我也是看了很多的資料,然后自己寫了一一段小代碼作為分析的測試代碼。首先記住在X86體系里很多的寄存器都有特殊的用途,其中ESP表示當(dāng)前函數(shù)堆棧的棧頂指針,而EBP則表示當(dāng)前函數(shù)堆棧的基地址。EBP是棧基址的指針,永遠(yuǎn)指向棧底(高地址),ESP是棧指針,永遠(yuǎn)指向棧頂(低地址)。
我的代碼如下:
我的代碼如下:
- #include<stdio.h>
- int pluss_a_and_b(int a,int b)
- {
- int c = -2;
- return (a + b - c);
- }
- int call_plus(int *a,int *b)
- {
- int c = *a;
- int d = *b;
- *a = d;
- *b = c;
- return pluss_a_and_b(c,d);
- }
- int main()
- {
- int c = 10;
- int d = 20;
- int g = call_plus(&c,&d);
- return 0;
- }
對上面的代碼進(jìn)行編譯和反匯編:
[gong@Gong-Computer deeplearn]$ gcc -g testcall.c -o testcall
[gong@Gong-Computer deeplearn]$ objdump -S -d testcall > testcall_s
然后對反匯編的代碼進(jìn)行分析:
- ...
- 8048393: c3 ret
- 08048394 <pluss_a_and_b>:
- #include<stdio.h>
- int pluss_a_and_b(int a,int b)
- {
- 8048394: 55 push %ebp
- 8048395: 89 e5 mov %esp,%ebp
- 8048397: 83 ec 10 sub $0x10,%esp
- int c = -2;
- 804839a: c7 45 fc fe ff ff ff movl $0xfffffffe,-0x4(%ebp)
- return (a + b - c);
- 80483a1: 8b 45 0c mov 0xc(%ebp),%eax
- 80483a4: 8b 55 08 mov 0x8(%ebp),%edx
- 80483a7: 8d 04 02 lea (%edx,%eax,1),%eax
- 80483aa: 2b 45 fc sub -0x4(%ebp),%eax
- }
- 80483ad: c9 leave
- 80483ae: c3 ret
- 080483af <call_plus>:
- int call_plus(int *a,int *b)
- {
- 80483af: 55 push %ebp
- 80483b0: 89 e5 mov %esp,%ebp
- 80483b2: 83 ec 18 sub $0x18,%esp
- int c = *a;
- 80483b5: 8b 45 08 mov 0x8(%ebp),%eax
- 80483b8: 8b 00 mov (%eax),%eax
- 80483ba: 89 45 fc mov %eax,-0x4(%ebp)
- int d = *b;
- 80483bd: 8b 45 0c mov 0xc(%ebp),%eax
- 80483c0: 8b 00 mov (%eax),%eax
- 80483c2: 89 45 f8 mov %eax,-0x8(%ebp)
- *a = d;
- 80483c5: 8b 45 08 mov 0x8(%ebp),%eax
- 80483c8: 8b 55 f8 mov -0x8(%ebp),%edx
- 80483cb: 89 10 mov %edx,(%eax)
- *b = c;
- 80483cd: 8b 45 0c mov 0xc(%ebp),%eax
- 80483d0: 8b 55 fc mov -0x4(%ebp),%edx
- 80483d3: 89 10 mov %edx,(%eax)
- return pluss_a_and_b(c,d);
- 80483d5: 8b 45 f8 mov -0x8(%ebp),%eax
- 80483d8: 89 44 24 04 mov %eax,0x4(%esp)
- 80483dc: 8b 45 fc mov -0x4(%ebp),%eax
- 80483df: 89 04 24 mov %eax,(%esp)
- 80483e2: e8 ad ff ff ff call 8048394 <pluss_a_and_b>
- }
- 80483e7: c9 leave
- 80483e8: c3 ret
- 080483e9 <main>:
- int main()
- {
- 80483e9: 55 push %ebp
- 80483ea: 89 e5 mov %esp,%ebp
- 80483ec: 83 ec 18 sub $0x18,%esp
- int c = 10;
- 80483ef: c7 45 f8 0a 00 00 00 movl $0xa,-0x8(%ebp)
- int d = 20;
- 80483f6: c7 45 f4 14 00 00 00 movl $0x14,-0xc(%ebp)
- int g = call_plus(&c,&d);
- 80483fd: 8d 45 f4 lea -0xc(%ebp),%eax
- 8048400: 89 44 24 04 mov %eax,0x4(%esp)
- 8048404: 8d 45 f8 lea -0x8(%ebp),%eax
- 8048407: 89 04 24 mov %eax,(%esp)
- 804840a: e8 a0 ff ff ff call 80483af <call_plus>
- 804840f: 89 45 fc mov %eax,-0x4(%ebp)
- return 0;
- 8048412: b8 00 00 00 00 mov $0x0,%eax
- }
- 8048417: c9 leave
- 8048418: c3 ret
- 8048419: 90 nop
- 804841a: 90 nop
- ...
首先,C語言的入口都是從main函數(shù)開始的,但是從反匯編代碼中可以發(fā)現(xiàn)并不是只有自己設(shè)計的代碼,還存在很多關(guān)于初始化等操作。這主要是因為C語言的運(yùn)行需要一些基本的環(huán)境和C-RunTime的一些基本函數(shù)。因此main 函數(shù)只是我們C語言的入口,但并不是一個程序的開始。因此main函數(shù)也需要堆棧的控制,也需要壓棧出棧等操作。
需要注意的是:
指令call用來調(diào)用一個函數(shù)或過程,這時下一條指令地址被壓入堆棧中,以備返回時能恢復(fù)執(zhí)行下條指令。sp=sp-1。通過下面的匯編代碼就可知道函數(shù)的返回地址。
80483e2: e8 ad ff ff ff call 8048394 <pluss_a_and_b>
}
80483e7: c9 leave
}
80483e7: c9 leave
可以知道指令call后的返回地址就是80483e7。而8048394則說明被調(diào)用函數(shù)的起始地址,這些數(shù)字可能在不同的系統(tǒng)中存在差別。
RET指令用來從一個函數(shù)或過程返回,之前CALL保存的下條指令地址會從棧內(nèi)彈出到EIP寄存器中,程序轉(zhuǎn)到CALL之前下條指令處執(zhí)行。
下面簡單的介紹幾個代碼:
80483e9: 55 push %ebp
80483ea: 89 e5 mov %esp,%ebp
80483ec: 83 ec 18 sub $0x18,%esp
80483ea: 89 e5 mov %esp,%ebp
80483ec: 83 ec 18 sub $0x18,%esp
首先push %ebp,是將調(diào)用函數(shù)的棧幀基地址壓入棧中,也就是保存調(diào)用函數(shù)的棧幀EBP。將其指向的地址壓入堆棧中。mov %esp,%ebp則是將ESP和EBP指向同一個地址,作為被調(diào)用函數(shù)的棧幀基地址。sub $0x18,%esp則是修改ESP的值,與EBP構(gòu)成當(dāng)前被調(diào)用函數(shù)的棧幀空間。
從圖中可以每個函數(shù)的棧空間都是相互獨(dú)立的,但是每一個棧空間的基本結(jié)構(gòu)都是相同的。都是該函數(shù)的EBP指針,然后是局部變量空間,然后是往下一個函數(shù)的傳遞參數(shù)空間,返回的EBP地址。這樣就能實現(xiàn)不同函數(shù)的調(diào)用,然后傳遞參數(shù)是采用基于EBP指針的相對位置實現(xiàn)的,并沒有絕對地址。
由此可以知道棧空間的分布是根據(jù)調(diào)用情況分析的,當(dāng)調(diào)用過多時就會導(dǎo)致溢出錯誤,因此并不是一味的迭代和遞歸。
關(guān)于函數(shù)調(diào)用的返回都是采用EAX寄存器實現(xiàn)的,但是當(dāng)返回的是結(jié)構(gòu)體以及聯(lián)合體時返回就不能采用EAX實現(xiàn)了,基本的實現(xiàn)方法也是基于堆棧的。
- #include<stdio.h>
- typedef struct {
- double d;
- float f;
- int i;
- char c;
- }return_value;
- return_value my_test_of_return()
- {
- return_value rv;
-
- rv.d = 12.56;
- rv.f = 3.1;
- rv.i = 10;
- rv.c = 'a';
- return rv;
- }
- int main()
- {
- return_value local = my_test_of_return();
- return 0;
- }
編譯以及反匯編以后得到如下的結(jié)果:
[gong@Gong-Computer deeplearn]$ gcc -g structpass.c -o structpass
[gong@Gong-Computer deeplearn]$ objdump -S -d structpass > structpass_s
[gong@Gong-Computer deeplearn]$ objdump -S -d structpass > structpass_s
- ...
- 08048394 <my_test_of_return>:
- char c;
- }return_value;
- return_value my_test_of_return()
- {
- 8048394: 55 push %ebp
- 8048395: 89 e5 mov %esp,%ebp
- 8048397: 83 ec 20 sub $0x20,%esp
- 804839a: 8b 45 08 mov 0x8(%ebp),%eax
- return_value rv;
- rv.d = 12.56;
- 804839d: dd 05 d8 84 04 08 fldl 0x80484d8
- 80483a3: dd 5d e8 fstpl -0x18(%ebp)
- rv.f = 3.1;
- 80483a6: ba 66 66 46 40 mov $0x40466666,%edx
- 80483ab: 89 55 f0 mov %edx,-0x10(%ebp)
- rv.i = 10;
- 80483ae: c7 45 f4 0a 00 00 00 movl $0xa,-0xc(%ebp)
- rv.c = 'a';
- 80483b5: c6 45 f8 61 movb $0x61,-0x8(%ebp)
- return rv;
- 80483b9: 8b 55 e8 mov -0x18(%ebp),%edx
- 80483bc: 89 10 mov %edx,(%eax)
- 80483be: 8b 55 ec mov -0x14(%ebp),%edx
- 80483c1: 89 50 04 mov %edx,0x4(%eax)
- 80483c4: 8b 55 f0 mov -0x10(%ebp),%edx
- 80483c7: 89 50 08 mov %edx,0x8(%eax)
- 80483ca: 8b 55 f4 mov -0xc(%ebp),%edx
- 80483cd: 89 50 0c mov %edx,0xc(%eax)
- 80483d0: 8b 55 f8 mov -0x8(%ebp),%edx
- 80483d3: 89 50 10 mov %edx,0x10(%eax)
- }
- 80483d6: c9 leave
- 80483d7: c2 04 00 ret $0x4
- 080483da <main>:
- int main()
- {
- 80483da: 8d 4c 24 04 lea 0x4(%esp),%ecx
- 80483de: 83 e4 f8 and $0xfffffff8,%esp
- 80483e1: ff 71 fc pushl -0x4(%ecx)
- 80483e4: 55 push %ebp
- 80483e5: 89 e5 mov %esp,%ebp
- 80483e7: 51 push %ecx
- 80483e8: 83 ec 2c sub $0x2c,%esp
- return_value local = my_test_of_return();
- 80483eb: 8d 45 e0 lea -0x20(%ebp),%eax
- 80483ee: 89 04 24 mov %eax,(%esp)
- 80483f1: e8 9e ff ff ff call 8048394 <my_test_of_return>
- 80483f6: 83 ec 04 sub $0x4,%esp
- return 0;
- 80483f9: b8 00 00 00 00 mov $0x0,%eax
- }
- 80483fe: 8b 4d fc mov -0x4(%ebp),%ecx
- 8048401: c9 leave
- 8048402: 8d 61 fc lea -0x4(%ecx),%esp
- ...
從上面的結(jié)果可以知道可以知道,返回的過程并不是一次通過EAX返回的,而是通過堆棧一個一個的傳遞出來,實現(xiàn)結(jié)果的返回。因此這也是我們需要注意的地方。
同樣對于結(jié)構(gòu)體的傳遞方式也是采用堆棧的方式進(jìn)行傳遞,基本的參看下面的分析。參數(shù)也是依據(jù)堆棧中的位置進(jìn)行控制的。
代碼:
- #include<stdio.h>
- typedef struct {
- double d;
- float f;
- int i;
- char c;
- }return_value;
- return_value my_test_pass(return_value pass)
- {
- return_value rv;
- rv.d = pass.d;
- rv.f = pass.f;
- rv.i = pass.i;
- rv.c = pass.c;
- return rv;
- }
- return_value my_test_of_return()
- {
- return_value rv;
-
- rv.d = 12.56;
- rv.f = 3.1;
- rv.i = 10;
- rv.c = 'a';
- return rv;
- }
- int main()
- {
- return_value local = my_test_of_return();
- return_value local1 = my_test_pass(local);
- return 0;
- }
編譯和反匯編過程:
[gong@Gong-Computer deeplearn]$ gcc -g structpass.c -o structpass
[gong@Gong-Computer deeplearn]$ objdump -S -d structpass > structpass_s
[gong@Gong-Computer deeplearn]$ objdump -S -d structpass > structpass_s
- ...
- int main()
- {
- 804841d: 8d 4c 24 04 lea 0x4(%esp),%ecx
- 8048421: 83 e4 f8 and $0xfffffff8,%esp
- 8048424: ff 71 fc pushl -0x4(%ecx)
- 8048427: 55 push %ebp
- 8048428: 89 e5 mov %esp,%ebp
- 804842a: 51 push %ecx
- 804842b: 83 ec 4c sub $0x4c,%esp
- return_value local = my_test_of_return();
- 804842e: 8d 45 e0 lea -0x20(%ebp),%eax
- 8048431: 89 04 24 mov %eax,(%esp)
- 8048434: e8 9e ff ff ff call 80483d7 <my_test_of_return>
- 8048439: 83 ec 04 sub $0x4,%esp
- return_value local1 = my_test_pass(local);
- 804843c: 8d 45 c8 lea -0x38(%ebp),%eax
- 804843f: 8b 55 e0 mov -0x20(%ebp),%edx
- 8048442: 89 54 24 04 mov %edx,0x4(%esp)
- 8048446: 8b 55 e4 mov -0x1c(%ebp),%edx
- 8048449: 89 54 24 08 mov %edx,0x8(%esp)
- 804844d: 8b 55 e8 mov -0x18(%ebp),%edx
- 8048450: 89 54 24 0c mov %edx,0xc(%esp)
- 8048454: 8b 55 ec mov -0x14(%ebp),%edx
- 8048457: 89 54 24 10 mov %edx,0x10(%esp)
- 804845b: 8b 55 f0 mov -0x10(%ebp),%edx
- 804845e: 89 54 24 14 mov %edx,0x14(%esp)
- 8048462: 89 04 24 mov %eax,(%esp)
- 8048465: e8 2a ff ff ff call 8048394 <my_test_pass>
- 804846a: 83 ec 04 sub $0x4,%esp
- return 0;
- 804846d: b8 00 00 00 00 mov $0x0,%eax
- }
由上面的反匯編代碼可以知道結(jié)構(gòu)體的傳遞參數(shù)是依據(jù)堆棧實現(xiàn)的。這也說明了多參數(shù)的傳遞過程并不是按著固定的模式實現(xiàn)的,這也是我們需要注意的問題。參數(shù)的傳遞需要根據(jù)實際情況分析。
總結(jié):
函數(shù)的調(diào)用是有一定的方式的,各個函數(shù)都有一定的堆棧空間,而且每一個堆棧空間的分布情況也是類似的,但是大小要根據(jù)實際的情況分析。一般一個函數(shù)的堆棧空間中包含下面幾個部分:1、棧幀(用來表示該堆棧空間的棧底,也就是指開始的地址EBP),局部變量的空間,下一個被調(diào)用函數(shù)的參數(shù)傳遞,最后是返回地址(實質(zhì)上也是一個EBP)。就是依據(jù)EBP和相對位置就能知道每一個函數(shù)的基本分布,而ESP就能知道堆棧空間的大小。
被調(diào)用參數(shù)的獲取主要是依據(jù)EBP指針的相對位置獲得,因為被調(diào)用函數(shù)的堆棧空間上一個堆棧空間就是調(diào)用函數(shù)的堆棧空間。根據(jù)函數(shù)的棧幀指針(EBP)和相對位置(-4,-8等)找到對應(yīng)的參數(shù),但是相對位置也是不固定的,這需要考慮結(jié)構(gòu)體的對齊等方式,具體的要在實際中計算。
返回值一般都是采用EAX返回的,但是對于結(jié)構(gòu)體等則是采用堆棧的方式一個元算一個元素的返回的,但是還是運(yùn)用了EAX的特性。
函數(shù)調(diào)用的分布打開如下:
從上面的分析我們可以發(fā)現(xiàn)匯編代碼是非常有用的,建議多參看匯編代碼分析具體的問題。