Calling agreement and name modification in DLL (1)
Calling Convention refers to a protocol established in the programming language in order to implement function calls. This protocol specifies the parameter transfer mode in the function of the language, whether the parameters can be variable and who will handle the stack. Different languages define different call conventions.
In C , in order to allow operator overload and function overload, the C compiler often overwrites the symbol name of each entry point in accordance with some rules to allow the same name (having different parameter types or different scopes). A use method without breaking the existing C-based linker. This technique is often referred to as name mangling or name decoration (Name Decoration). Many C compiler vendors have chosen their name modification.
Therefore, in order to make the modules (such as Visual Basic Applications, Pascal or Fortran applications, such as Visual Basic applications, Pascal or Fortran applications, etc.), you must use the correct call agreement to export functions, and do not let the compiler Do any name modified for functions to be exported.
1. Calling convention
Call the convention to process the order of the stack and out of the stack when the determination function parameter is transmitted (the argument popping the argument popping arguments), and the compiler is used to identify the name modification of the function name. In Microsoft VC 6.0, the following call agreements are defined, we will analyze them one by one by part:
1, __ cdecl
__cdecl is a call agreement for the default use of the C / C and MFC programs, or the __cdecl keyword can be manually specified when the function declaration is declared. When using the __cdecl convention, the function parameters are in the order from the right to left, and the parameter pop-up stack is made by the calling function to clean the stack. Therefore, functions that implement variable parameters can only be used to use this call. Since each of the functions of the __cdecl agreed, you need to include the code of the cleaning stack, the executable file size will be relatively large. __cdecl can be written _cdecl.
The following will be analyzed by a specific example:
Create a Win32 Console project in VC , named CDECL. The code is as follows:
INT __CDECL Add (Int A, INT B); // Function Declaration
void main ()
{
Add (1,2); // function call
}
INT __CDECL Add (INT A, INT B) // Function Implementation
{
Return (A B);
}
The disassembly code at the function call is as follows:
Add (1, 2);
Push 2; parameter from right to left in the stack, first press 2
Push 1; press 1
Call @ ilt 0 (add) (00401005); call function implementation
Add ESP, 8; Call the clear stack by the function
2, __ stdcall
__stdcall call convention is used to call the Win32 API function. When using the __stdcal convention, the function parameters are in the order from the right to left, and the function being called is fixed before returning the stack of the transfer parameter, and the function parameters are fixed. Since the function body itself knows the number of parameters passed, the called function can be directly cleaned up with a RET N instruction directly before returning to the stack of the passing parameters. __stdcall can write _stdcall. Or that example, will be replaced with __cdecl to __stdcall:
INT __STDCALL ADD (int A, int b)
{
Return (A B);
}
Function call disassembly code:
Add (1, 2);
Push 2; parameter from right to left in the stack, first press 2
Push 1; press 1
Call @ ilt 10 (add) (ADD)
0040100F
); Call function implementation
Function implementation part of the disassembly code:
INT __STDCALL Add (int A, INT B)
Push EBP
MOV EBP, ESP
SUB ESP, 40H
Push EBX
PUSH ESI
Push EDI
Lea EDI, [EBP-40H]
Mov ECX, 10h
Mov Eax, 0cccccccch
Rep Stos DWORD PTR [EDI]
RETURN (A B);
MOV EAX, DWORD PTR [EBP 8]
Add Eax, DWORD PTR [EBP 0CH]
POP EDI
POP ESI
POP EBX
MOV ESP, EBP
POP EBP
RET 8; Qing stack
3, __ fastcall
__fastcall agres to be used in applications with very high performance requirements. __fastcall agre, put the two sizes of the function from the left to the left of the two sizes in the ECX and EDX registers, and the rest of the parameters are still transmitted from left to the left, and the function called before returning. Clean the stack of transfer parameters. __fastcall can write _fastcall.
It is still similar example. At this time, the function call is agreed to __fastcall, the number of function parameters increased:
INT __FASTCALL Add (int A, Double B, INT C, INT D)
{
RETURN (A B C D);
}
Compilation code for function call parts:
Add (1, 2, 3, 4);
Push 4; After two parameters from right to left in the stack, first press 4
Mov Edx, 3; put the Int type 3 into EDX
Push 40000000H; 2 2 Double Type 2
PUSH 0
MOV ECX, 1; put the int type 1 into ECXCall @ ilt 0 (00401005); call function implementation
Function implementation part of the disassembly code:
INT __FASTCALL Add (int A, Double B, INT C, INT D)
Push EBP
MOV EBP, ESP
SUB ESP, 48H
Push EBX
PUSH ESI
Push EDI
Push ECX
Lea Edi, [EBP-48H]
MOV ECX, 12H
Mov Eax, 0cccccccch
Rep Stos DWORD PTR [EDI]
POP ECX
MOV DWORD PTR [EBP-8], EDX
MOV DWORD PTR [EBP-4], ECX
RETURN (A B C D);
Fild DWORD PTR [EBP-4]
Fadd Qword PTR [EBP 8]
FIADD DWORD PTR [EBP-8]
FIADD DWORD PTR [EBP 10h]
Call __ftol (004011B8)
POP EDI
POP ESI
POP EBX
MOV ESP, EBP
POP EBP
RET 0CH; Qing stack
Keywords __cdecl, __ stdcall, and __fastcall can be added directly before the function you want to output, you can also select the setting environment of Setting ...-> C / C -> Code Generation item. Their corresponding command line parameters are / gd, / gz, and / gr. The default state is / gd, ie __cdecl. When the keyword before the output function is not in the compilation environment, the keyword before the output function is added directly before the output function is valid.
Subsequent: DLL calls the agreement and name modification (2)
Modification and name modification in DLL (3)