DLL call conventions and name modification (2)

Source: Internet
Author: User
4 , ThiscallThe thiscall call convention is the default call Convention for non-static class member functions in C ++. Thiscall can only be used by the compiler without corresponding keywords, so it cannot be specified by programmers. When thiscall is used, function parameters are pushed to the stack in the order from right to left. The called function clears the stack of the transfer parameter before returning, it only transfers an additional parameter through the ECX register: This pointer. In this example, a class is defined and a member function is defined in the class. The Code is as follows: Class csum {public: int add (int A, int B) {return (a + B) ;}}; void main () {csum sum; sum. add (1, 2);} assembly code of function call:; csum sum; sum. add (1, 2); push 2; parameter from right to left into Stack, first press 2 Push 1; press 1 Lea ECx, [ebp-4]; ECX stores the this pointer call @ ILT + 5 (csum: add) (0040100a); call the function to implement partial assembly code:; int add (int A, int B) push ebpmov EBP, espsub ESP, 44 h; Use A 4 bytes space to store this pointer push ebxpush esipush edipush ecxlea EDI, [ebp-44h] mov ECx, 11 hmov eax, 0 cccccccchrep STOs dword ptr [EDI] Pop ecxmov dword ptr [ebp-4], ECx; 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 5 And naked attributesWhen a function enters the function using the four call conventions described above, the compiler generates code to save the values in the ESI, EDI, EBX, and EBP registers, when you exit the function, the code is generated to restore the content of these registers. For a function that defines the naked attribute, the compiler will not automatically generate such code. You need to manually use embedded assembly to control the stack Management in function implementation. Because the naked attribute is not a type modifier, it must be used together with _ declspec. The following code defines a function using the naked attribute and Its Implementation: _ declspec (naked) func () {int I; Int J; _ ASM {push EBP mov EBP, ESP sub ESP, _ local_size} _ ASM {mov ESP, EBP pop ebp ret} attributes have little to do with this section. For details, refer to msdn. 6 , WinapiIt is also worth mentioning that the winapi macro can be translated into appropriate call conventions for the function to use. This macro is defined in windef. h. The following is in windef. some content in H: # define cdecl _ cdecl # define winapi cdecl # define callback _ stdcall # define winapi _ stdcall # define apientry winapi, functions of macros such as winapi, callback, and apientry. 2. Name Decoration)C or C ++ functions are identified internally (by compiling and linking) by the decoration name. The function modifier name is a string generated by the compiler when compiling the function definition or prototype. The compiler modifies the function name when creating the. OBJ file. In some cases, modifying the name of a function is necessary, for example, specifying the Output C ++ overload function, constructor, and destructor in the module definition file, for example, Calling C or C ++ functions in assembly code. In VC ++, the function modifier name is determined by the compilation type (C or C ++), function name, class name, call conventions, return type, parameters, and other factors. The following describes C compilation, C ++ compilation (non-class member functions), c ++ class, and member function Compilation: 1 Function Name modification during C CompilationWhen a function is called with _ cdecl, the compiler only adds an underline prefix before the original function name in the format of _ functionname. For example, the int _ cdecl add (int A, int B) function is output as follows: _ add. When the function is called with _ stdcall, the compiler adds an underline prefix before the original function name, followed by a @ symbol and the number of bytes of the function parameter, in the format of _ functionname @ number. For example, the int _ stdcall add (int A, int B) function outputs: _ add @ 8. When the function is called with _ fastcall, the compiler adds a @ symbol before the original function name, followed by a @ symbol and the number of bytes of the function parameter, the format is @ functionname @ number. For example, the int _ fastcall add (int A, int B) function outputs @ add @ 8. The case sensitivity of the original function name is not changed.

 

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.