Talk about the function stack under arm

Source: Internet
Author: User
Introduction

This article briefly describes how a function is passed into a parameter, and we all know that when a function call uses a small number of parameters (less than or equal to 4 on arm), the parameter is passed through the register (arm is through R0,R1,R2,R3), and when there are more than 4 parameters, The extra parameters will be pushed into the stack to pass (in fact, in the function call process will also put the parameters of the R0,R1,R2,R3 passed into the stack), what is the implementation of it, we look at.


function stacks
First we need to understand how the memory address space of the next Linux process is laid out, in Linux, the virtual address of 0~3G is process-owned, 3G~4G is used by the kernel, each process has its own independent 0~3g memory address space. When a process makes a function call, we all know that the arguments to the called function are manipulated through the stack, and here we simply need to understand that the stack in the memory address space of Linux is growing from top to bottom, that is, the stack is at a high address and the top of the stack is at a low address.




OK, after a simple understanding of the memory address space stack, we also need to understand the EBP and ESP two registers, EBP is used to save the low address of the stack, and ESP to save the top address of the stack, and each function call will involve a stack frame, stack frame structure as shown below


Give an example to illustrate the characteristics of a function frame, such as

/* b is called by a
* Parameters: Data1, data2, data3
* Local variables: S1, s2, S3 */
void B (int data1, int data2, int data3)
{
int b_s1;
int b_s2;
int b_s3;
}

/* A calls the B function */
void A (void)
{
int a_s1;
int a_s2;
int a_s3;

B (1, 2, 3);
printf ("1\n");
}


In the above example, the stack frame should look like the figure below




As you can see from the legend, when the A function does not call the B function, the stack frame of the A function only holds local variables, while EBP (the bottom pointer) points to the function stack frame header of the A function, and when a function calls the B function, the A function pushes the parameters required by the B function from right to left into the stack (in the example, it is 3, then 2 The last is 1), after a call after B will be required to run the first command to press into the stack, at this time to create a B stack frame, the specific process:


Pressing the required parameters of the B function into the stack from right to left
Press the address of the first instruction after executing the B function
Create a B stack frame
Press the stack bottom of a stack frame
Register for Press-in B function protection
Local variables pressed into the B function



Summary

In fact, each processor architecture used in different ways, on arm I have a few parameters and the situation of the indefinite parameters through the assembly code view is not the same, after the disassembly after the study will publish a blog post specifically said this, now this article as a primer.

http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4771280&uid=26772321

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.