DLL call conventions and name modification (1)

Source: Internet
Author: User
Calling convention is a protocol established in programming languages to implement function calls. This Protocol specifies the parameter transfer mode in the function of the language, whether the parameter is variable, and who handles the stack. Different Languages define different call conventions. In C ++, in order to allow Operator Overloading and function overloading, the C ++ compiler often rewrite the symbol names of each entry point according to certain rules, so that the same name (with different parameter types or different scopes) can have multiple usages without breaking the existing C-based linker. This technique is often called name mangling or name decoration ). Many C ++ compiler vendors have chosen their own name modification schemes. Therefore, to enable modules written in other languages (such as Visual Basic applications, Pascal or Fortran applications) to call the DLL functions written in C/C ++, you must use the correct call conventions to export the function, and do not have the compiler modify the name of the function to be exported. 1. calling convention)Call conventions are used to process the order of incoming and outgoing stacks when function parameters are transferred (by the caller or by the caller to bring the parameter to the stack ), the compiler is used to identify the name modification conventions of function names. Microsoft Vc ++ 6.0 defines the following call conventions. We will analyze them one by one based on the assembly language: 1 , _ Cdecl_ Cdecl is the call Convention used by C/C ++ and MFC programs by default. You can also add the _ cdecl keyword when declaring a function. When _ cdecl is used, function parameters are pushed to the stack in the order from right to left, and the caller pops up the parameter stack to clear the stack. Therefore, a function that implements variable parameters can only use this call convention. Every function using the _ cdecl Convention must contain the code for clearing the stack, so the size of the executable file generated will be relatively large. _ Cdecl can be written as _ cdecl. The following uses a specific example to analyze the _ cdecl Convention: Create a Win32 console project in VC ++ and name it 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 function call is as follows :; add (1, 2); push 2; the parameter is pushed from right to left into the stack. Press 2 Push 1 first; press 1 call @ ILT + 0 (ADD) (00401005 ); call the function to implement add ESP, 8; call the function to clear the stack 2 , _ Stdcall_ Stdcall is used to call Win32 API functions. When _ stdcal is used, function parameters are written to the stack in the order from right to left. The called function clears the stack of the transfer parameter before returning, and the number of function parameters is fixed. Because the function body knows the number of passed parameters, the called function can directly clear the stack of the passed parameters with a RET n command before returning the result. _ Stdcall can be written as _ stdcall. In this example, replace _ cdecl with _ stdcall: int _ stdcall add (int A, int B) {return (a + B );} disassembly code of function call:; add (1, 2); push 2; the parameter is pushed from right to left into the stack, 2 Push 1 first; 1 call @ ILT + 10 (ADD) (0040100f); call the disassembly code of the function implementation part:; int _ stdcall add (int A, int B) Push ebpmov EBP, espsub ESP, 40 hpush ebxpush esipush edilea EDI, [ebp-40h] mov ECx, 10 hmov eax, 0 cccccccchrep STOs dword ptr [EDI]; Return (a + B); MoV eax, dword ptr [EBP + 8] add eax, dword ptr [EBP + 0ch] Pop edipop esipop ebxmov ESP, ebppop ebpret 8; clear Stack 3 , _ Fastcall_ Fastcall conventions are used in scenarios with high performance requirements. _ Fastcall: The two DWORD parameters starting from the left of the function are placed in the ECX and EDX registers respectively, the other parameters are still transmitted from the right to the left pressure stack. The called function clears the stack of the transfer parameter before returning. _ Fastcall can be written as _ fastcall. In this case, the function call convention is _ fastcall. The number of function parameters is increased by two: int _ fastcall add (int A, double B, int C, int D) {return (A + B + C + D);} assembly code of the function call part:; add (1, 2, 3, 4); push 4; the last two parameters are pushed to the stack from right to left, 4 mov EDX and 3 are pushed first; 3 of int type is pushed to edxpush 40000000 h; 2 of double type is pushed to 0mov ECx and 1; put 1 of the int type into ecxcall @ ILT + 0 (ADD) (00401005); call the disassembly code of the function implementation part:; int _ fastcall add (int, double B, int C, int d) Push ebpmov EBP, espsub ESP, 48 hpush ebxpush ESI Push edipush ecxlea EDI, [ebp-48h] mov ECx, 12 hmov eax, 0 cccccccchrep STOs dword ptr [EDI] Pop ecxmov dword ptr [ebp-8], edxmov 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 + 10 h] Call _ ftol (004011b8) pop edipop esipop ebxmov ESP, ebppop ebpret 0ch; clear stack keywords _ cdecl, _ stdcall, and _ fastcall can be directly added before the function to be output, it can also be set in the compiling environment... -> C/ C ++-> code generation item selection. Their corresponding command line parameters are/GD,/GZ, And/GR. The default status is/GD, Which is _ cdecl. When the keywords added before the output function are different from those selected in the compiling environment, the keywords directly added before the output function are valid.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.