Common function call stack specifications in Linux

Source: Internet
Author: User

 

We should all know that the function calling process in advanced languages has the concept of "stack". The local variables of called functions are stored in the stack, function call parameters are also passed through the stack. Then, how does a function call push all kinds of data into the stack? How does the called function perform operations on the stack to obtain necessary data? Who is responsible for clearing the stack after the function call is completed? This uses the function call stack specification!

The function call stack specification refers to a "Convention" in the compiler, which specifies how the caller transmits parameters, how the caller obtains parameters, how to clear the stack after the call, and how to pass the return value. The compiler follows this specification when compiling the program, so that the program can be correctly executed. For different compilers and different advanced languages, this specification is different.

In the Linux Kernel on the X86 platform, common function call specifications include C, fastcall, Pascal, etc. The following briefly introduces these specifications:

1. C Specification

The caller is required to push the parameter from right to left, and the return value is passed through the eax register. If the return value exceeds 32 bits, it uses edX: eax for transfer, and finally the caller is responsible for clearing the stack. This specification is observed by most C compilers.

We need to note that the caller is responsible for clearing the stack and supports variable parameter functions, such as the familiar printf function. Let's take an example:

printf("%d, %d, %d\n", i, j, k);

The purpose of this statement is to call a printf function. The called function does not know how many functions will be passed during compilation, but the caller knows. After this statement is compiled, it looks like the following:

 

Pushl $ K // pseudo assembly, put K into the stack pushl $ jpushl $ ipush $ ADDR // the address of the statement where ADDR is the first parameter call printfaddl $0x10, % ESP

Or translate:

Sub $0x10, % ESP // first apply for stack space mov $ ADDR, (% ESP) mov $ I, 0x4 (% ESP) mov $ J, 0x8 (% ESP) mov $ K, 0xc (% ESP) Call printfaddl $0x10, % ESP

In fact, the second type of translation is basically the same as the first type of completed function, because the parameter on the right is closer to the bottom of the stack, because the stack increases from the high address to the low address, the higher the address. After the printf function is called, read the parameter at the top of the stack, that is, "% d, % d, % d \ n", and then determine the number of other parameters based on this parameter, and search for the high address.

From the assembly code above, we can see that,

addl   $0x10, %esp

 

This command is responsible for clearing the stack. It is located in the caller's class and he knows how much stack he should clean. In the called
ret   $n
This command is used to clear the stack. Because printf does not know how many parameters will be passed during compilation, this n cannot be determined, so it is impossible to clear the stack in the caller.

2. fastcall

As the name suggests, fastcall means fast call. Because the stack operation must be accessed, some overhead may occur for some simple small functions with frequently called parameters. Therefore, parameters can be passed through registers. Take GCC as an example. When GCC uses fastcall, the first two parameters from left to right are passed through the ECX and EDX registers by default. Other parameters are passed through the stack, however, you can use _ attribute _ (regparm (N) to control the number of registers that can be used. For example, regparm (3) indicates that the first three parameters are transmitted using registers, the default registers are eax, ECx, and EDX. The return value transfer and stack cleanup are the same as the C Specification.

This specification should be a supplement and optimization of the C specification, which is often used in Linux kernel, such as system calls.

3. Pascal

Parameters are written from left to right into the stack. The caller clears the stack and the returned values are transmitted through eax or edX: eax.

References: Linux kernel source code Guide

 

From: www.w.geliu.com

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.