在編寫共享庫時(shí),為保證ABI(app binary interface)兼容:
1 盡量使用C語言 2不要在接口類使用虛函數(shù)和模板; 3 不要改變成員函數(shù)的訪問權(quán)限; 4 不要使用STL 5 不要依賴使用虛擬析構(gòu)函數(shù),最好自己實(shí)現(xiàn),顯式調(diào)用;
6 不要在DLL里面申請內(nèi)存,DLL外釋放,DLL和APP可能不在同一個(gè)內(nèi)存堆;
可重入(reentrant)函數(shù)可以由多于一個(gè)任務(wù)并發(fā)使用,而不必?fù)?dān)心數(shù)據(jù)錯(cuò)誤。相反, 不可重入(non-reentrant)函數(shù)不能由超過一個(gè)任務(wù)所共享,除非能確保函數(shù)的互斥(或者使用信號(hào)量,或者在代碼的關(guān)鍵部分禁用中斷)。可重入函數(shù)可以在任意時(shí)刻被中斷,稍后再繼續(xù)運(yùn)行,不會(huì)丟失數(shù)據(jù)。可重入函數(shù)要么使用本地變量,要么在使用全局變量時(shí)保護(hù)自己的數(shù)據(jù)。
Reentrant Function:A function whose effect, when called by two or more threads,is guaranteed to be as if the threads each executed thefunction one after another in an undefined order, even ifthe actual execution is interleaved.
Thread-Safe Function:A function that may be safely invoked concurrently by multiple threads.
函數(shù)可重入的必要條件:
1 不使用任何(局部)靜態(tài)變量或者全局的非常量;
2 不返回任何局部靜態(tài)或者全局非常量指針;
3 僅依賴調(diào)用方的參數(shù);
4 不依賴任何單個(gè)資源的鎖;
5 不調(diào)用任何不可重入的函數(shù);
In classical OS, stack grows downwards. After each push operatation, the value of ebp becomes small, and vice versa.
esp is the top of the stack.
ebp is usually set to esp at the start of the function. Local variables are accessed by subtracting a constant offset from ebp. All x86 calling conventions define ebp as being preserved across function calls. ebp itself actually points to the previous frame's base pointer, which enables stack walking in a debugger and viewing other frames local variables to work.
Most function prologs look something like:
push ebp ; Preserve current frame pointer
mov ebp, esp ; Create new frame pointer pointing to current stack top
sub esp, 20 ; allocate 20 bytes worth of locals on stack.
Then later in the function you may have code like (presuming both local variables are 4 bytes)
mov [ebp-4], eax ; Store eax in first local
mov ebx, [ebp - 8] ; Load ebx from second local
objdump is a program for displaying various information about object files. For instance, it can be used as a disassembler to view executable in assembly form. It is part of the GNU Binutils for fine-grained control over executable and other binary data.
For example, to completely disassemble a binary:
objdump -Dslx file
posted on 2012-07-17 22:20
鷹擊長空 閱讀(316)
評(píng)論(0) 編輯 收藏 引用