C Function Call procedure __ function

Source: Internet
Author: User


These days looking at GCC Inline Assembly, embedding some assembly code through ASM or __asm__ in C code, such as making system calls, using registers to improve performance, and requiring stack frames in function calls, CPU registers, GCC Inlie assembly and so on at his fingertips. Now let's look at the function call procedure.


1. Linux Process Virtual address space

For an example of a 32-bit operating system, the following is the Linux process address space layout:



The high 1GB space of the 32-bit virtual address space is left to the operating system kernel, the stack grows from the high address to the low address, and the heap grows up from the low address to the high address.

Memory allocated in C, such as malloc, is allocated in the heap. The static and global variables initialized are placed in the data section. Uninitialized global variables and local static variables are placed in the BSS section, and more precisely, space is reserved for them in the BSS segment. Non-static local variables are staged on the stack during function calls.


2. Stack frame of function

Stack plays an important role in the program operation. Most importantly, the stack holds the maintenance information needed for a function call, known as a stackframe, and a stack frame of a function (the called function) generally includes the following elements:

(1) Function parameters, the default invocation convention, in the Order of the right and left to press the parameters into the stack in turn. Executed by the function caller.

(2) The return address of the function, that is, the address of the next instruction of the caller calling this function, such as Call func1. The function caller (call instruction) executes.

(3) Save the EBP register of the caller function, press the EBP of the caller function onto the stack, and make the EBP point to the address in this stack: PUSHL%ebp; Movl%esp,%ebp. Performed by the modulated function.

(4) Context: a register (a function caller's) that needs to be kept unchanged during a function call, such as Ebx,esi,edi. Performed by the modulated function.

(5) A temporary variable, such as a non-static local variable.

The following is a stack frame diagram of a function:


The process of pressing function arguments and returning addresses is pushed by the function caller into the stack before calling the function. After each function executes first is to carry on the function caller's EBP register presses into the stack, after is in the stack to open up some space to store the local variable, finally put the register which wants to save presses into the stack. The basic form of the instruction sequence for i386 standard function entry and exit is as follows: (GCC inline assembly description)

PUSHL%ebp
movl%esp,%ebp
subl $x,%esp
[PUSHL reg1]
...
[PUSHL Regn]
The actual content of the function
[popl regn]
...
[Popl REG1]
MOVL%ebp,%esp
opol%ebp
ret

3. Examples

Here's a code analysis:

/* stackframe.c*/                                                                                                            
#include <stdio.h>

void func1 (int a, int b);
void Func2 (int a, int b, int c);

int main (void)
{
    int a = 1;
    Func1 (A, 2);
    return 0;
}

void Func1 (int a, int b)
{
    int  a1 = 3;
    Char B1 = ' 4 '-' 0 '; 4
    Func2 (A1, B1, 5);
}

void Func2 (int a, int b, int c)
{
    char a1 = ' a ';
}

The compiled code is as follows, Gcc-o stackframe.s-s stackframe.c

    . File "Funcstack.c" 
    . text. GLOBL Main. Type main, @function MAIN:PUSHL%ebp movl%esp,%ebp andl $-16,%esp Subl $32,%esp movl $ (%ESP) MOVL $, 4 (%ESP) MOVL (%ESP),%eax movl%eax, (%esp Call Func1 MOVL $,%eax//function return value sent to EAX leave ret. Size main,.-main globl          C1. Type func1, @function FUNC1:PUSHL%EBP//pressed into the function caller's ebp,old EBP movl%esp,%EBP Make the current EBP point to the top of the stack, at which point the top of the stack points to old EBP Subl $,%ESP///Open some space on the stack, store local variables Movl $ -16 (%EBP)//func1 
    The storage location of the middle integer variable a1, ebp-16 Movb $ -9 (%EBP)//func1 where the character variable b1 is stored, ebp-9 movsbl-9 (%EBP),%EAX//Will be B1 sent to EAX
    MOVL $8 (%ESP)//will invoke Func2 function parameter 5 on stack, esp+8 movl%eax, 4 (%ESP)//push parameter B1 into stack, esp+4 Movl-16 (%EBP),%eaX//Send A1 into EAX movl%eax, (%ESP)///push parameter A1 to stack, esp+0 call FUNC2//Invoke Func2, Next The return address of the instructions into the stack (EIP into the stack), the internal completion, no assembly code leave RET//will return address to the EIP. Size func1,.-func1 globl func 2. Type Func2, @function func2:pushl%ebp movl%esp,%ebp subl $16,%esp movb $97,-1 (%e    BP) leave Ret. Size Func2,.-func2. Ident "GCC: (GNU) 4.4.4 20100726 (Red Hat 4.4.4-13)". Section . Note. 
 Gnu-stack, "", @progbits

4. Examples

The code is as follows:

#include <stdio.h> 

void test1 (int a, int b);
void Test2 (int a, int b, int c);

int main (void)
{
    test1 (1, 2);
    return 0;
}

void Test1 (int a, int b)
{
    char *ebp = NULL;

    ASM ("Movl%%ebp,%0\n\t"
        : "=r" (EBP));

    printf ("TEST1:EBP =%p\n", EBP); EBP Test2 of the Test1 function
    (3, 4, 5);
}

void Test2 (int a, int b, int c)
{
    int *ebp = NULL;

    ASM ("Movl%%ebp,%0\n\t"
        : "=r" (EBP));

    printf ("test2: *EBP =%p\n", (char *) (*EBP)); The EBP point of the Test2 (old EBP) is test1 EBP
    printf ("* (EBP + 4)  =%d\n", * ((char *) EBP + 4));  Return address
    printf ("* (EBP + 8)  =%d\n", * ((char *) EBP + 8));  The first parameter
    is 3 printf ("* (EBP +) =%d\n", * ((char *) EBP + 12));//second parameter 4
    printf ("* (EBP +) =%d\n", * (char *) EB P + 16)); Third parameter 5
}     

The results are as follows:



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.